]>
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> | |
6c271ce2 | 60 | #include <linux/kthread.h> |
2b188cc1 | 61 | #include <linux/blkdev.h> |
edafccee | 62 | #include <linux/bvec.h> |
2b188cc1 JA |
63 | #include <linux/net.h> |
64 | #include <net/sock.h> | |
65 | #include <net/af_unix.h> | |
6b06314c | 66 | #include <net/scm.h> |
2b188cc1 JA |
67 | #include <linux/anon_inodes.h> |
68 | #include <linux/sched/mm.h> | |
69 | #include <linux/uaccess.h> | |
70 | #include <linux/nospec.h> | |
edafccee JA |
71 | #include <linux/sizes.h> |
72 | #include <linux/hugetlb.h> | |
aa4c3967 | 73 | #include <linux/highmem.h> |
15b71abe JA |
74 | #include <linux/namei.h> |
75 | #include <linux/fsnotify.h> | |
4840e418 | 76 | #include <linux/fadvise.h> |
3e4827b0 | 77 | #include <linux/eventpoll.h> |
ff002b30 | 78 | #include <linux/fs_struct.h> |
7d67af2c | 79 | #include <linux/splice.h> |
b41e9852 | 80 | #include <linux/task_work.h> |
bcf5a063 | 81 | #include <linux/pagemap.h> |
2b188cc1 | 82 | |
c826bd7a DD |
83 | #define CREATE_TRACE_POINTS |
84 | #include <trace/events/io_uring.h> | |
85 | ||
2b188cc1 JA |
86 | #include <uapi/linux/io_uring.h> |
87 | ||
88 | #include "internal.h" | |
561fb04a | 89 | #include "io-wq.h" |
2b188cc1 | 90 | |
5277deaa | 91 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 92 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
65e19f54 JA |
93 | |
94 | /* | |
95 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs | |
96 | */ | |
97 | #define IORING_FILE_TABLE_SHIFT 9 | |
98 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) | |
99 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) | |
100 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) | |
2b188cc1 JA |
101 | |
102 | struct io_uring { | |
103 | u32 head ____cacheline_aligned_in_smp; | |
104 | u32 tail ____cacheline_aligned_in_smp; | |
105 | }; | |
106 | ||
1e84b97b | 107 | /* |
75b28aff HV |
108 | * This data is shared with the application through the mmap at offsets |
109 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
110 | * |
111 | * The offsets to the member fields are published through struct | |
112 | * io_sqring_offsets when calling io_uring_setup. | |
113 | */ | |
75b28aff | 114 | struct io_rings { |
1e84b97b SB |
115 | /* |
116 | * Head and tail offsets into the ring; the offsets need to be | |
117 | * masked to get valid indices. | |
118 | * | |
75b28aff HV |
119 | * The kernel controls head of the sq ring and the tail of the cq ring, |
120 | * and the application controls tail of the sq ring and the head of the | |
121 | * cq ring. | |
1e84b97b | 122 | */ |
75b28aff | 123 | struct io_uring sq, cq; |
1e84b97b | 124 | /* |
75b28aff | 125 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
126 | * ring_entries - 1) |
127 | */ | |
75b28aff HV |
128 | u32 sq_ring_mask, cq_ring_mask; |
129 | /* Ring sizes (constant, power of 2) */ | |
130 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
131 | /* |
132 | * Number of invalid entries dropped by the kernel due to | |
133 | * invalid index stored in array | |
134 | * | |
135 | * Written by the kernel, shouldn't be modified by the | |
136 | * application (i.e. get number of "new events" by comparing to | |
137 | * cached value). | |
138 | * | |
139 | * After a new SQ head value was read by the application this | |
140 | * counter includes all submissions that were dropped reaching | |
141 | * the new SQ head (and possibly more). | |
142 | */ | |
75b28aff | 143 | u32 sq_dropped; |
1e84b97b | 144 | /* |
0d9b5b3a | 145 | * Runtime SQ flags |
1e84b97b SB |
146 | * |
147 | * Written by the kernel, shouldn't be modified by the | |
148 | * application. | |
149 | * | |
150 | * The application needs a full memory barrier before checking | |
151 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
152 | */ | |
75b28aff | 153 | u32 sq_flags; |
0d9b5b3a SG |
154 | /* |
155 | * Runtime CQ flags | |
156 | * | |
157 | * Written by the application, shouldn't be modified by the | |
158 | * kernel. | |
159 | */ | |
160 | u32 cq_flags; | |
1e84b97b SB |
161 | /* |
162 | * Number of completion events lost because the queue was full; | |
163 | * this should be avoided by the application by making sure | |
0b4295b5 | 164 | * there are not more requests pending than there is space in |
1e84b97b SB |
165 | * the completion queue. |
166 | * | |
167 | * Written by the kernel, shouldn't be modified by the | |
168 | * application (i.e. get number of "new events" by comparing to | |
169 | * cached value). | |
170 | * | |
171 | * As completion events come in out of order this counter is not | |
172 | * ordered with any other data. | |
173 | */ | |
75b28aff | 174 | u32 cq_overflow; |
1e84b97b SB |
175 | /* |
176 | * Ring buffer of completion events. | |
177 | * | |
178 | * The kernel writes completion events fresh every time they are | |
179 | * produced, so the application is allowed to modify pending | |
180 | * entries. | |
181 | */ | |
75b28aff | 182 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
183 | }; |
184 | ||
edafccee JA |
185 | struct io_mapped_ubuf { |
186 | u64 ubuf; | |
187 | size_t len; | |
188 | struct bio_vec *bvec; | |
189 | unsigned int nr_bvecs; | |
190 | }; | |
191 | ||
65e19f54 JA |
192 | struct fixed_file_table { |
193 | struct file **files; | |
31b51510 JA |
194 | }; |
195 | ||
05589553 XW |
196 | struct fixed_file_ref_node { |
197 | struct percpu_ref refs; | |
198 | struct list_head node; | |
199 | struct list_head file_list; | |
200 | struct fixed_file_data *file_data; | |
4a38aed2 | 201 | struct llist_node llist; |
05589553 XW |
202 | }; |
203 | ||
05f3fb3c JA |
204 | struct fixed_file_data { |
205 | struct fixed_file_table *table; | |
206 | struct io_ring_ctx *ctx; | |
207 | ||
05589553 | 208 | struct percpu_ref *cur_refs; |
05f3fb3c | 209 | struct percpu_ref refs; |
05f3fb3c | 210 | struct completion done; |
05589553 XW |
211 | struct list_head ref_list; |
212 | spinlock_t lock; | |
05f3fb3c JA |
213 | }; |
214 | ||
5a2e745d JA |
215 | struct io_buffer { |
216 | struct list_head list; | |
217 | __u64 addr; | |
218 | __s32 len; | |
219 | __u16 bid; | |
220 | }; | |
221 | ||
2b188cc1 JA |
222 | struct io_ring_ctx { |
223 | struct { | |
224 | struct percpu_ref refs; | |
225 | } ____cacheline_aligned_in_smp; | |
226 | ||
227 | struct { | |
228 | unsigned int flags; | |
e1d85334 | 229 | unsigned int compat: 1; |
aad5d8da | 230 | unsigned int limit_mem: 1; |
e1d85334 RD |
231 | unsigned int cq_overflow_flushed: 1; |
232 | unsigned int drain_next: 1; | |
233 | unsigned int eventfd_async: 1; | |
2b188cc1 | 234 | |
75b28aff HV |
235 | /* |
236 | * Ring buffer of indices into array of io_uring_sqe, which is | |
237 | * mmapped by the application using the IORING_OFF_SQES offset. | |
238 | * | |
239 | * This indirection could e.g. be used to assign fixed | |
240 | * io_uring_sqe entries to operations and only submit them to | |
241 | * the queue when needed. | |
242 | * | |
243 | * The kernel modifies neither the indices array nor the entries | |
244 | * array. | |
245 | */ | |
246 | u32 *sq_array; | |
2b188cc1 JA |
247 | unsigned cached_sq_head; |
248 | unsigned sq_entries; | |
249 | unsigned sq_mask; | |
6c271ce2 | 250 | unsigned sq_thread_idle; |
498ccd9e | 251 | unsigned cached_sq_dropped; |
206aefde | 252 | atomic_t cached_cq_overflow; |
ad3eb2c8 | 253 | unsigned long sq_check_overflow; |
de0617e4 JA |
254 | |
255 | struct list_head defer_list; | |
5262f567 | 256 | struct list_head timeout_list; |
1d7bb1d5 | 257 | struct list_head cq_overflow_list; |
fcb323cc JA |
258 | |
259 | wait_queue_head_t inflight_wait; | |
ad3eb2c8 | 260 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
261 | } ____cacheline_aligned_in_smp; |
262 | ||
206aefde JA |
263 | struct io_rings *rings; |
264 | ||
2b188cc1 | 265 | /* IO offload */ |
561fb04a | 266 | struct io_wq *io_wq; |
6c271ce2 | 267 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
2b188cc1 | 268 | struct mm_struct *sqo_mm; |
6c271ce2 | 269 | wait_queue_head_t sqo_wait; |
75b28aff | 270 | |
6b06314c JA |
271 | /* |
272 | * If used, fixed file set. Writers must ensure that ->refs is dead, | |
273 | * readers must ensure that ->refs is alive as long as the file* is | |
274 | * used. Only updated through io_uring_register(2). | |
275 | */ | |
05f3fb3c | 276 | struct fixed_file_data *file_data; |
6b06314c | 277 | unsigned nr_user_files; |
b14cca0c PB |
278 | int ring_fd; |
279 | struct file *ring_file; | |
6b06314c | 280 | |
edafccee JA |
281 | /* if used, fixed mapped user buffers */ |
282 | unsigned nr_user_bufs; | |
283 | struct io_mapped_ubuf *user_bufs; | |
284 | ||
2b188cc1 JA |
285 | struct user_struct *user; |
286 | ||
0b8c0ec7 | 287 | const struct cred *creds; |
181e448d | 288 | |
0f158b4c JA |
289 | struct completion ref_comp; |
290 | struct completion sq_thread_comp; | |
206aefde | 291 | |
0ddf92e8 JA |
292 | /* if all else fails... */ |
293 | struct io_kiocb *fallback_req; | |
294 | ||
206aefde JA |
295 | #if defined(CONFIG_UNIX) |
296 | struct socket *ring_sock; | |
297 | #endif | |
298 | ||
5a2e745d JA |
299 | struct idr io_buffer_idr; |
300 | ||
071698e1 JA |
301 | struct idr personality_idr; |
302 | ||
206aefde JA |
303 | struct { |
304 | unsigned cached_cq_tail; | |
305 | unsigned cq_entries; | |
306 | unsigned cq_mask; | |
307 | atomic_t cq_timeouts; | |
ad3eb2c8 | 308 | unsigned long cq_check_overflow; |
206aefde JA |
309 | struct wait_queue_head cq_wait; |
310 | struct fasync_struct *cq_fasync; | |
311 | struct eventfd_ctx *cq_ev_fd; | |
312 | } ____cacheline_aligned_in_smp; | |
2b188cc1 JA |
313 | |
314 | struct { | |
315 | struct mutex uring_lock; | |
316 | wait_queue_head_t wait; | |
317 | } ____cacheline_aligned_in_smp; | |
318 | ||
319 | struct { | |
320 | spinlock_t completion_lock; | |
e94f141b | 321 | |
def596e9 | 322 | /* |
540e32a0 | 323 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
324 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
325 | * For SQPOLL, only the single threaded io_sq_thread() will | |
326 | * manipulate the list, hence no extra locking is needed there. | |
327 | */ | |
540e32a0 | 328 | struct list_head iopoll_list; |
78076bb6 JA |
329 | struct hlist_head *cancel_hash; |
330 | unsigned cancel_hash_bits; | |
e94f141b | 331 | bool poll_multi_file; |
31b51510 | 332 | |
fcb323cc JA |
333 | spinlock_t inflight_lock; |
334 | struct list_head inflight_list; | |
2b188cc1 | 335 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 336 | |
4a38aed2 JA |
337 | struct delayed_work file_put_work; |
338 | struct llist_head file_put_llist; | |
339 | ||
85faa7b8 | 340 | struct work_struct exit_work; |
2b188cc1 JA |
341 | }; |
342 | ||
09bb8394 JA |
343 | /* |
344 | * First field must be the file pointer in all the | |
345 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
346 | */ | |
221c5eb2 JA |
347 | struct io_poll_iocb { |
348 | struct file *file; | |
0969e783 JA |
349 | union { |
350 | struct wait_queue_head *head; | |
351 | u64 addr; | |
352 | }; | |
221c5eb2 | 353 | __poll_t events; |
8c838788 | 354 | bool done; |
221c5eb2 | 355 | bool canceled; |
392edb45 | 356 | struct wait_queue_entry wait; |
221c5eb2 JA |
357 | }; |
358 | ||
b5dba59e JA |
359 | struct io_close { |
360 | struct file *file; | |
361 | struct file *put_file; | |
362 | int fd; | |
363 | }; | |
364 | ||
ad8a48ac JA |
365 | struct io_timeout_data { |
366 | struct io_kiocb *req; | |
367 | struct hrtimer timer; | |
368 | struct timespec64 ts; | |
369 | enum hrtimer_mode mode; | |
370 | }; | |
371 | ||
8ed8d3c3 JA |
372 | struct io_accept { |
373 | struct file *file; | |
374 | struct sockaddr __user *addr; | |
375 | int __user *addr_len; | |
376 | int flags; | |
09952e3e | 377 | unsigned long nofile; |
8ed8d3c3 JA |
378 | }; |
379 | ||
380 | struct io_sync { | |
381 | struct file *file; | |
382 | loff_t len; | |
383 | loff_t off; | |
384 | int flags; | |
d63d1b5e | 385 | int mode; |
8ed8d3c3 JA |
386 | }; |
387 | ||
fbf23849 JA |
388 | struct io_cancel { |
389 | struct file *file; | |
390 | u64 addr; | |
391 | }; | |
392 | ||
b29472ee JA |
393 | struct io_timeout { |
394 | struct file *file; | |
395 | u64 addr; | |
396 | int flags; | |
bfe68a22 PB |
397 | u32 off; |
398 | u32 target_seq; | |
135fcde8 | 399 | struct list_head list; |
b29472ee JA |
400 | }; |
401 | ||
9adbd45d JA |
402 | struct io_rw { |
403 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
404 | struct kiocb kiocb; | |
405 | u64 addr; | |
406 | u64 len; | |
407 | }; | |
408 | ||
3fbb51c1 JA |
409 | struct io_connect { |
410 | struct file *file; | |
411 | struct sockaddr __user *addr; | |
412 | int addr_len; | |
413 | }; | |
414 | ||
e47293fd JA |
415 | struct io_sr_msg { |
416 | struct file *file; | |
fddaface | 417 | union { |
270a5940 | 418 | struct user_msghdr __user *umsg; |
fddaface JA |
419 | void __user *buf; |
420 | }; | |
e47293fd | 421 | int msg_flags; |
bcda7baa | 422 | int bgid; |
fddaface | 423 | size_t len; |
bcda7baa | 424 | struct io_buffer *kbuf; |
e47293fd JA |
425 | }; |
426 | ||
15b71abe JA |
427 | struct io_open { |
428 | struct file *file; | |
429 | int dfd; | |
15b71abe | 430 | struct filename *filename; |
c12cedf2 | 431 | struct open_how how; |
4022e7af | 432 | unsigned long nofile; |
15b71abe JA |
433 | }; |
434 | ||
05f3fb3c JA |
435 | struct io_files_update { |
436 | struct file *file; | |
437 | u64 arg; | |
438 | u32 nr_args; | |
439 | u32 offset; | |
440 | }; | |
441 | ||
4840e418 JA |
442 | struct io_fadvise { |
443 | struct file *file; | |
444 | u64 offset; | |
445 | u32 len; | |
446 | u32 advice; | |
447 | }; | |
448 | ||
c1ca757b JA |
449 | struct io_madvise { |
450 | struct file *file; | |
451 | u64 addr; | |
452 | u32 len; | |
453 | u32 advice; | |
454 | }; | |
455 | ||
3e4827b0 JA |
456 | struct io_epoll { |
457 | struct file *file; | |
458 | int epfd; | |
459 | int op; | |
460 | int fd; | |
461 | struct epoll_event event; | |
e47293fd JA |
462 | }; |
463 | ||
7d67af2c PB |
464 | struct io_splice { |
465 | struct file *file_out; | |
466 | struct file *file_in; | |
467 | loff_t off_out; | |
468 | loff_t off_in; | |
469 | u64 len; | |
470 | unsigned int flags; | |
471 | }; | |
472 | ||
ddf0322d JA |
473 | struct io_provide_buf { |
474 | struct file *file; | |
475 | __u64 addr; | |
476 | __s32 len; | |
477 | __u32 bgid; | |
478 | __u16 nbufs; | |
479 | __u16 bid; | |
480 | }; | |
481 | ||
1d9e1288 BM |
482 | struct io_statx { |
483 | struct file *file; | |
484 | int dfd; | |
485 | unsigned int mask; | |
486 | unsigned int flags; | |
e62753e4 | 487 | const char __user *filename; |
1d9e1288 BM |
488 | struct statx __user *buffer; |
489 | }; | |
490 | ||
3ca405eb PB |
491 | struct io_completion { |
492 | struct file *file; | |
493 | struct list_head list; | |
0f7e466b | 494 | int cflags; |
3ca405eb PB |
495 | }; |
496 | ||
f499a021 JA |
497 | struct io_async_connect { |
498 | struct sockaddr_storage address; | |
499 | }; | |
500 | ||
03b1230c JA |
501 | struct io_async_msghdr { |
502 | struct iovec fast_iov[UIO_FASTIOV]; | |
503 | struct iovec *iov; | |
504 | struct sockaddr __user *uaddr; | |
505 | struct msghdr msg; | |
b537916c | 506 | struct sockaddr_storage addr; |
03b1230c JA |
507 | }; |
508 | ||
f67676d1 JA |
509 | struct io_async_rw { |
510 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
511 | const struct iovec *free_iovec; |
512 | struct iov_iter iter; | |
bcf5a063 | 513 | struct wait_page_queue wpq; |
f67676d1 JA |
514 | }; |
515 | ||
1a6b74fc | 516 | struct io_async_ctx { |
f67676d1 JA |
517 | union { |
518 | struct io_async_rw rw; | |
03b1230c | 519 | struct io_async_msghdr msg; |
f499a021 | 520 | struct io_async_connect connect; |
2d28390a | 521 | struct io_timeout_data timeout; |
f67676d1 | 522 | }; |
1a6b74fc JA |
523 | }; |
524 | ||
6b47ee6e PB |
525 | enum { |
526 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
527 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
528 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
529 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
530 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 531 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 532 | |
dea3b49c | 533 | REQ_F_LINK_HEAD_BIT, |
6b47ee6e PB |
534 | REQ_F_FAIL_LINK_BIT, |
535 | REQ_F_INFLIGHT_BIT, | |
536 | REQ_F_CUR_POS_BIT, | |
537 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 538 | REQ_F_LINK_TIMEOUT_BIT, |
6b47ee6e | 539 | REQ_F_ISREG_BIT, |
6b47ee6e | 540 | REQ_F_COMP_LOCKED_BIT, |
99bc4c38 | 541 | REQ_F_NEED_CLEANUP_BIT, |
2ca10259 | 542 | REQ_F_OVERFLOW_BIT, |
d7718a9d | 543 | REQ_F_POLLED_BIT, |
bcda7baa | 544 | REQ_F_BUFFER_SELECTED_BIT, |
5b0bbee4 | 545 | REQ_F_NO_FILE_TABLE_BIT, |
7cdaf587 | 546 | REQ_F_WORK_INITIALIZED_BIT, |
4dd2824d | 547 | REQ_F_TASK_PINNED_BIT, |
84557871 JA |
548 | |
549 | /* not a real bit, just to check we're not overflowing the space */ | |
550 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
551 | }; |
552 | ||
553 | enum { | |
554 | /* ctx owns file */ | |
555 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
556 | /* drain existing IO first */ | |
557 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
558 | /* linked sqes */ | |
559 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
560 | /* doesn't sever on completion < 0 */ | |
561 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
562 | /* IOSQE_ASYNC */ | |
563 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
564 | /* IOSQE_BUFFER_SELECT */ |
565 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 566 | |
dea3b49c PB |
567 | /* head of a link */ |
568 | REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT), | |
6b47ee6e PB |
569 | /* fail rest of links */ |
570 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), | |
571 | /* on inflight list */ | |
572 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), | |
573 | /* read/write uses file position */ | |
574 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
575 | /* must not punt to workers */ | |
576 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
6b47ee6e PB |
577 | /* has linked timeout */ |
578 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), | |
6b47ee6e PB |
579 | /* regular file */ |
580 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
6b47ee6e PB |
581 | /* completion under lock */ |
582 | REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT), | |
99bc4c38 PB |
583 | /* needs cleanup */ |
584 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
2ca10259 JA |
585 | /* in overflow list */ |
586 | REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT), | |
d7718a9d JA |
587 | /* already went through poll handler */ |
588 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
589 | /* buffer already selected */ |
590 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
5b0bbee4 JA |
591 | /* doesn't need file table for this request */ |
592 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), | |
7cdaf587 XW |
593 | /* io_wq_work is initialized */ |
594 | REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT), | |
4dd2824d PB |
595 | /* req->task is refcounted */ |
596 | REQ_F_TASK_PINNED = BIT(REQ_F_TASK_PINNED_BIT), | |
d7718a9d JA |
597 | }; |
598 | ||
599 | struct async_poll { | |
600 | struct io_poll_iocb poll; | |
807abcb0 | 601 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
602 | }; |
603 | ||
09bb8394 JA |
604 | /* |
605 | * NOTE! Each of the iocb union members has the file pointer | |
606 | * as the first entry in their struct definition. So you can | |
607 | * access the file pointer through any of the sub-structs, | |
608 | * or directly as just 'ki_filp' in this struct. | |
609 | */ | |
2b188cc1 | 610 | struct io_kiocb { |
221c5eb2 | 611 | union { |
09bb8394 | 612 | struct file *file; |
9adbd45d | 613 | struct io_rw rw; |
221c5eb2 | 614 | struct io_poll_iocb poll; |
8ed8d3c3 JA |
615 | struct io_accept accept; |
616 | struct io_sync sync; | |
fbf23849 | 617 | struct io_cancel cancel; |
b29472ee | 618 | struct io_timeout timeout; |
3fbb51c1 | 619 | struct io_connect connect; |
e47293fd | 620 | struct io_sr_msg sr_msg; |
15b71abe | 621 | struct io_open open; |
b5dba59e | 622 | struct io_close close; |
05f3fb3c | 623 | struct io_files_update files_update; |
4840e418 | 624 | struct io_fadvise fadvise; |
c1ca757b | 625 | struct io_madvise madvise; |
3e4827b0 | 626 | struct io_epoll epoll; |
7d67af2c | 627 | struct io_splice splice; |
ddf0322d | 628 | struct io_provide_buf pbuf; |
1d9e1288 | 629 | struct io_statx statx; |
3ca405eb PB |
630 | /* use only after cleaning per-op data, see io_clean_op() */ |
631 | struct io_completion compl; | |
221c5eb2 | 632 | }; |
2b188cc1 | 633 | |
1a6b74fc | 634 | struct io_async_ctx *io; |
d625c6ee | 635 | u8 opcode; |
65a6543d XW |
636 | /* polled IO has completed */ |
637 | u8 iopoll_completed; | |
2b188cc1 | 638 | |
4f4eeba8 | 639 | u16 buf_index; |
9cf7c104 | 640 | u32 result; |
4f4eeba8 | 641 | |
010e8e6b PB |
642 | struct io_ring_ctx *ctx; |
643 | unsigned int flags; | |
644 | refcount_t refs; | |
645 | struct task_struct *task; | |
646 | u64 user_data; | |
d7718a9d | 647 | |
010e8e6b | 648 | struct list_head link_list; |
fcb323cc | 649 | |
d21ffe7e PB |
650 | /* |
651 | * 1. used with ctx->iopoll_list with reads/writes | |
652 | * 2. to track reqs with ->files (see io_op_def::file_table) | |
653 | */ | |
010e8e6b PB |
654 | struct list_head inflight_entry; |
655 | ||
656 | struct percpu_ref *fixed_file_refs; | |
657 | struct callback_head task_work; | |
658 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ | |
659 | struct hlist_node hash_node; | |
660 | struct async_poll *apoll; | |
661 | struct io_wq_work work; | |
2b188cc1 | 662 | }; |
05589553 | 663 | |
27dc8338 PB |
664 | struct io_defer_entry { |
665 | struct list_head list; | |
666 | struct io_kiocb *req; | |
9cf7c104 | 667 | u32 seq; |
2b188cc1 JA |
668 | }; |
669 | ||
def596e9 | 670 | #define IO_IOPOLL_BATCH 8 |
2b188cc1 | 671 | |
013538bd JA |
672 | struct io_comp_state { |
673 | unsigned int nr; | |
674 | struct list_head list; | |
675 | struct io_ring_ctx *ctx; | |
676 | }; | |
677 | ||
9a56a232 JA |
678 | struct io_submit_state { |
679 | struct blk_plug plug; | |
680 | ||
2579f913 JA |
681 | /* |
682 | * io_kiocb alloc cache | |
683 | */ | |
684 | void *reqs[IO_IOPOLL_BATCH]; | |
6c8a3134 | 685 | unsigned int free_reqs; |
2579f913 | 686 | |
013538bd JA |
687 | /* |
688 | * Batch completion logic | |
689 | */ | |
690 | struct io_comp_state comp; | |
691 | ||
9a56a232 JA |
692 | /* |
693 | * File reference cache | |
694 | */ | |
695 | struct file *file; | |
696 | unsigned int fd; | |
697 | unsigned int has_refs; | |
9a56a232 JA |
698 | unsigned int ios_left; |
699 | }; | |
700 | ||
d3656344 JA |
701 | struct io_op_def { |
702 | /* needs req->io allocated for deferral/async */ | |
703 | unsigned async_ctx : 1; | |
704 | /* needs current->mm setup, does mm access */ | |
705 | unsigned needs_mm : 1; | |
706 | /* needs req->file assigned */ | |
707 | unsigned needs_file : 1; | |
fd2206e4 JA |
708 | /* don't fail if file grab fails */ |
709 | unsigned needs_file_no_error : 1; | |
d3656344 JA |
710 | /* hash wq insertion if file is a regular file */ |
711 | unsigned hash_reg_file : 1; | |
712 | /* unbound wq insertion if file is a non-regular file */ | |
713 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
714 | /* opcode is not supported by this kernel */ |
715 | unsigned not_supported : 1; | |
f86cd20c JA |
716 | /* needs file table */ |
717 | unsigned file_table : 1; | |
ff002b30 JA |
718 | /* needs ->fs */ |
719 | unsigned needs_fs : 1; | |
8a72758c JA |
720 | /* set if opcode supports polled "wait" */ |
721 | unsigned pollin : 1; | |
722 | unsigned pollout : 1; | |
bcda7baa JA |
723 | /* op supports buffer selection */ |
724 | unsigned buffer_select : 1; | |
57f1a649 | 725 | unsigned needs_fsize : 1; |
d3656344 JA |
726 | }; |
727 | ||
728 | static const struct io_op_def io_op_defs[] = { | |
0463b6c5 PB |
729 | [IORING_OP_NOP] = {}, |
730 | [IORING_OP_READV] = { | |
d3656344 JA |
731 | .async_ctx = 1, |
732 | .needs_mm = 1, | |
733 | .needs_file = 1, | |
734 | .unbound_nonreg_file = 1, | |
8a72758c | 735 | .pollin = 1, |
4d954c25 | 736 | .buffer_select = 1, |
d3656344 | 737 | }, |
0463b6c5 | 738 | [IORING_OP_WRITEV] = { |
d3656344 JA |
739 | .async_ctx = 1, |
740 | .needs_mm = 1, | |
741 | .needs_file = 1, | |
742 | .hash_reg_file = 1, | |
743 | .unbound_nonreg_file = 1, | |
8a72758c | 744 | .pollout = 1, |
57f1a649 | 745 | .needs_fsize = 1, |
d3656344 | 746 | }, |
0463b6c5 | 747 | [IORING_OP_FSYNC] = { |
d3656344 JA |
748 | .needs_file = 1, |
749 | }, | |
0463b6c5 | 750 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
751 | .needs_file = 1, |
752 | .unbound_nonreg_file = 1, | |
8a72758c | 753 | .pollin = 1, |
d3656344 | 754 | }, |
0463b6c5 | 755 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
756 | .needs_file = 1, |
757 | .hash_reg_file = 1, | |
758 | .unbound_nonreg_file = 1, | |
8a72758c | 759 | .pollout = 1, |
57f1a649 | 760 | .needs_fsize = 1, |
d3656344 | 761 | }, |
0463b6c5 | 762 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
763 | .needs_file = 1, |
764 | .unbound_nonreg_file = 1, | |
765 | }, | |
0463b6c5 PB |
766 | [IORING_OP_POLL_REMOVE] = {}, |
767 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 JA |
768 | .needs_file = 1, |
769 | }, | |
0463b6c5 | 770 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
771 | .async_ctx = 1, |
772 | .needs_mm = 1, | |
773 | .needs_file = 1, | |
774 | .unbound_nonreg_file = 1, | |
ff002b30 | 775 | .needs_fs = 1, |
8a72758c | 776 | .pollout = 1, |
d3656344 | 777 | }, |
0463b6c5 | 778 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
779 | .async_ctx = 1, |
780 | .needs_mm = 1, | |
781 | .needs_file = 1, | |
782 | .unbound_nonreg_file = 1, | |
ff002b30 | 783 | .needs_fs = 1, |
8a72758c | 784 | .pollin = 1, |
52de1fe1 | 785 | .buffer_select = 1, |
d3656344 | 786 | }, |
0463b6c5 | 787 | [IORING_OP_TIMEOUT] = { |
d3656344 JA |
788 | .async_ctx = 1, |
789 | .needs_mm = 1, | |
790 | }, | |
0463b6c5 PB |
791 | [IORING_OP_TIMEOUT_REMOVE] = {}, |
792 | [IORING_OP_ACCEPT] = { | |
d3656344 JA |
793 | .needs_mm = 1, |
794 | .needs_file = 1, | |
795 | .unbound_nonreg_file = 1, | |
f86cd20c | 796 | .file_table = 1, |
8a72758c | 797 | .pollin = 1, |
d3656344 | 798 | }, |
0463b6c5 PB |
799 | [IORING_OP_ASYNC_CANCEL] = {}, |
800 | [IORING_OP_LINK_TIMEOUT] = { | |
d3656344 JA |
801 | .async_ctx = 1, |
802 | .needs_mm = 1, | |
803 | }, | |
0463b6c5 | 804 | [IORING_OP_CONNECT] = { |
d3656344 JA |
805 | .async_ctx = 1, |
806 | .needs_mm = 1, | |
807 | .needs_file = 1, | |
808 | .unbound_nonreg_file = 1, | |
8a72758c | 809 | .pollout = 1, |
d3656344 | 810 | }, |
0463b6c5 | 811 | [IORING_OP_FALLOCATE] = { |
d3656344 | 812 | .needs_file = 1, |
57f1a649 | 813 | .needs_fsize = 1, |
d3656344 | 814 | }, |
0463b6c5 | 815 | [IORING_OP_OPENAT] = { |
f86cd20c | 816 | .file_table = 1, |
ff002b30 | 817 | .needs_fs = 1, |
d3656344 | 818 | }, |
0463b6c5 | 819 | [IORING_OP_CLOSE] = { |
fd2206e4 JA |
820 | .needs_file = 1, |
821 | .needs_file_no_error = 1, | |
f86cd20c | 822 | .file_table = 1, |
d3656344 | 823 | }, |
0463b6c5 | 824 | [IORING_OP_FILES_UPDATE] = { |
d3656344 | 825 | .needs_mm = 1, |
f86cd20c | 826 | .file_table = 1, |
d3656344 | 827 | }, |
0463b6c5 | 828 | [IORING_OP_STATX] = { |
d3656344 | 829 | .needs_mm = 1, |
ff002b30 | 830 | .needs_fs = 1, |
5b0bbee4 | 831 | .file_table = 1, |
d3656344 | 832 | }, |
0463b6c5 | 833 | [IORING_OP_READ] = { |
3a6820f2 JA |
834 | .needs_mm = 1, |
835 | .needs_file = 1, | |
836 | .unbound_nonreg_file = 1, | |
8a72758c | 837 | .pollin = 1, |
bcda7baa | 838 | .buffer_select = 1, |
3a6820f2 | 839 | }, |
0463b6c5 | 840 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
841 | .needs_mm = 1, |
842 | .needs_file = 1, | |
843 | .unbound_nonreg_file = 1, | |
8a72758c | 844 | .pollout = 1, |
57f1a649 | 845 | .needs_fsize = 1, |
3a6820f2 | 846 | }, |
0463b6c5 | 847 | [IORING_OP_FADVISE] = { |
4840e418 JA |
848 | .needs_file = 1, |
849 | }, | |
0463b6c5 | 850 | [IORING_OP_MADVISE] = { |
c1ca757b JA |
851 | .needs_mm = 1, |
852 | }, | |
0463b6c5 | 853 | [IORING_OP_SEND] = { |
fddaface JA |
854 | .needs_mm = 1, |
855 | .needs_file = 1, | |
856 | .unbound_nonreg_file = 1, | |
8a72758c | 857 | .pollout = 1, |
fddaface | 858 | }, |
0463b6c5 | 859 | [IORING_OP_RECV] = { |
fddaface JA |
860 | .needs_mm = 1, |
861 | .needs_file = 1, | |
862 | .unbound_nonreg_file = 1, | |
8a72758c | 863 | .pollin = 1, |
bcda7baa | 864 | .buffer_select = 1, |
fddaface | 865 | }, |
0463b6c5 | 866 | [IORING_OP_OPENAT2] = { |
f86cd20c | 867 | .file_table = 1, |
ff002b30 | 868 | .needs_fs = 1, |
cebdb986 | 869 | }, |
3e4827b0 JA |
870 | [IORING_OP_EPOLL_CTL] = { |
871 | .unbound_nonreg_file = 1, | |
872 | .file_table = 1, | |
873 | }, | |
7d67af2c PB |
874 | [IORING_OP_SPLICE] = { |
875 | .needs_file = 1, | |
876 | .hash_reg_file = 1, | |
877 | .unbound_nonreg_file = 1, | |
ddf0322d JA |
878 | }, |
879 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 880 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
881 | [IORING_OP_TEE] = { |
882 | .needs_file = 1, | |
883 | .hash_reg_file = 1, | |
884 | .unbound_nonreg_file = 1, | |
885 | }, | |
d3656344 JA |
886 | }; |
887 | ||
2e0464d4 BM |
888 | enum io_mem_account { |
889 | ACCT_LOCKED, | |
890 | ACCT_PINNED, | |
891 | }; | |
892 | ||
81b68a5c PB |
893 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
894 | struct io_comp_state *cs); | |
78e19bbe | 895 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
ec9c02ad | 896 | static void io_put_req(struct io_kiocb *req); |
c40f6379 | 897 | static void io_double_put_req(struct io_kiocb *req); |
978db57e | 898 | static void __io_double_put_req(struct io_kiocb *req); |
94ae5e77 | 899 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
7271ef3a | 900 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
94ae5e77 | 901 | static void io_queue_linked_timeout(struct io_kiocb *req); |
05f3fb3c JA |
902 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
903 | struct io_uring_files_update *ip, | |
904 | unsigned nr_args); | |
f56040b8 | 905 | static int io_prep_work_files(struct io_kiocb *req); |
3ca405eb | 906 | static void __io_clean_op(struct io_kiocb *req); |
b41e9852 JA |
907 | static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, |
908 | int fd, struct file **out_file, bool fixed); | |
909 | static void __io_queue_sqe(struct io_kiocb *req, | |
f13fad7b JA |
910 | const struct io_uring_sqe *sqe, |
911 | struct io_comp_state *cs); | |
4349f30e | 912 | static void io_file_put_work(struct work_struct *work); |
de0617e4 | 913 | |
b63534c4 JA |
914 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
915 | struct iovec **iovec, struct iov_iter *iter, | |
916 | bool needs_lock); | |
ff6165b2 JA |
917 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
918 | const struct iovec *fast_iov, | |
b63534c4 | 919 | struct iov_iter *iter); |
de0617e4 | 920 | |
2b188cc1 JA |
921 | static struct kmem_cache *req_cachep; |
922 | ||
923 | static const struct file_operations io_uring_fops; | |
924 | ||
925 | struct sock *io_uring_get_socket(struct file *file) | |
926 | { | |
927 | #if defined(CONFIG_UNIX) | |
928 | if (file->f_op == &io_uring_fops) { | |
929 | struct io_ring_ctx *ctx = file->private_data; | |
930 | ||
931 | return ctx->ring_sock->sk; | |
932 | } | |
933 | #endif | |
934 | return NULL; | |
935 | } | |
936 | EXPORT_SYMBOL(io_uring_get_socket); | |
937 | ||
4dd2824d PB |
938 | static void io_get_req_task(struct io_kiocb *req) |
939 | { | |
940 | if (req->flags & REQ_F_TASK_PINNED) | |
941 | return; | |
942 | get_task_struct(req->task); | |
943 | req->flags |= REQ_F_TASK_PINNED; | |
944 | } | |
945 | ||
3ca405eb PB |
946 | static inline void io_clean_op(struct io_kiocb *req) |
947 | { | |
0e1b6fe3 | 948 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
3ca405eb PB |
949 | __io_clean_op(req); |
950 | } | |
951 | ||
4dd2824d PB |
952 | /* not idempotent -- it doesn't clear REQ_F_TASK_PINNED */ |
953 | static void __io_put_req_task(struct io_kiocb *req) | |
954 | { | |
955 | if (req->flags & REQ_F_TASK_PINNED) | |
956 | put_task_struct(req->task); | |
957 | } | |
958 | ||
4349f30e | 959 | static void io_sq_thread_drop_mm(void) |
c40f6379 JA |
960 | { |
961 | struct mm_struct *mm = current->mm; | |
962 | ||
963 | if (mm) { | |
964 | kthread_unuse_mm(mm); | |
965 | mmput(mm); | |
966 | } | |
967 | } | |
968 | ||
969 | static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx) | |
970 | { | |
971 | if (!current->mm) { | |
cbcf7214 PB |
972 | if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) || |
973 | !mmget_not_zero(ctx->sqo_mm))) | |
c40f6379 JA |
974 | return -EFAULT; |
975 | kthread_use_mm(ctx->sqo_mm); | |
976 | } | |
977 | ||
978 | return 0; | |
979 | } | |
980 | ||
981 | static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx, | |
982 | struct io_kiocb *req) | |
983 | { | |
984 | if (!io_op_defs[req->opcode].needs_mm) | |
985 | return 0; | |
986 | return __io_sq_thread_acquire_mm(ctx); | |
987 | } | |
988 | ||
989 | static inline void req_set_fail_links(struct io_kiocb *req) | |
990 | { | |
991 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) | |
992 | req->flags |= REQ_F_FAIL_LINK; | |
993 | } | |
4a38aed2 | 994 | |
7cdaf587 XW |
995 | /* |
996 | * Note: must call io_req_init_async() for the first time you | |
997 | * touch any members of io_wq_work. | |
998 | */ | |
999 | static inline void io_req_init_async(struct io_kiocb *req) | |
1000 | { | |
1001 | if (req->flags & REQ_F_WORK_INITIALIZED) | |
1002 | return; | |
1003 | ||
1004 | memset(&req->work, 0, sizeof(req->work)); | |
1005 | req->flags |= REQ_F_WORK_INITIALIZED; | |
1006 | } | |
1007 | ||
0cdaf760 PB |
1008 | static inline bool io_async_submit(struct io_ring_ctx *ctx) |
1009 | { | |
1010 | return ctx->flags & IORING_SETUP_SQPOLL; | |
1011 | } | |
1012 | ||
2b188cc1 JA |
1013 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1014 | { | |
1015 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1016 | ||
0f158b4c | 1017 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1018 | } |
1019 | ||
8eb7e2d0 PB |
1020 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1021 | { | |
1022 | return !req->timeout.off; | |
1023 | } | |
1024 | ||
2b188cc1 JA |
1025 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1026 | { | |
1027 | struct io_ring_ctx *ctx; | |
78076bb6 | 1028 | int hash_bits; |
2b188cc1 JA |
1029 | |
1030 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1031 | if (!ctx) | |
1032 | return NULL; | |
1033 | ||
0ddf92e8 JA |
1034 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
1035 | if (!ctx->fallback_req) | |
1036 | goto err; | |
1037 | ||
78076bb6 JA |
1038 | /* |
1039 | * Use 5 bits less than the max cq entries, that should give us around | |
1040 | * 32 entries per hash list if totally full and uniformly spread. | |
1041 | */ | |
1042 | hash_bits = ilog2(p->cq_entries); | |
1043 | hash_bits -= 5; | |
1044 | if (hash_bits <= 0) | |
1045 | hash_bits = 1; | |
1046 | ctx->cancel_hash_bits = hash_bits; | |
1047 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1048 | GFP_KERNEL); | |
1049 | if (!ctx->cancel_hash) | |
1050 | goto err; | |
1051 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1052 | ||
21482896 | 1053 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1054 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1055 | goto err; | |
2b188cc1 JA |
1056 | |
1057 | ctx->flags = p->flags; | |
583863ed | 1058 | init_waitqueue_head(&ctx->sqo_wait); |
2b188cc1 | 1059 | init_waitqueue_head(&ctx->cq_wait); |
1d7bb1d5 | 1060 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c JA |
1061 | init_completion(&ctx->ref_comp); |
1062 | init_completion(&ctx->sq_thread_comp); | |
5a2e745d | 1063 | idr_init(&ctx->io_buffer_idr); |
071698e1 | 1064 | idr_init(&ctx->personality_idr); |
2b188cc1 JA |
1065 | mutex_init(&ctx->uring_lock); |
1066 | init_waitqueue_head(&ctx->wait); | |
1067 | spin_lock_init(&ctx->completion_lock); | |
540e32a0 | 1068 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1069 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1070 | INIT_LIST_HEAD(&ctx->timeout_list); |
fcb323cc JA |
1071 | init_waitqueue_head(&ctx->inflight_wait); |
1072 | spin_lock_init(&ctx->inflight_lock); | |
1073 | INIT_LIST_HEAD(&ctx->inflight_list); | |
4a38aed2 JA |
1074 | INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work); |
1075 | init_llist_head(&ctx->file_put_llist); | |
2b188cc1 | 1076 | return ctx; |
206aefde | 1077 | err: |
0ddf92e8 JA |
1078 | if (ctx->fallback_req) |
1079 | kmem_cache_free(req_cachep, ctx->fallback_req); | |
78076bb6 | 1080 | kfree(ctx->cancel_hash); |
206aefde JA |
1081 | kfree(ctx); |
1082 | return NULL; | |
2b188cc1 JA |
1083 | } |
1084 | ||
9cf7c104 | 1085 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1086 | { |
2bc9930e JA |
1087 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1088 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1089 | |
9cf7c104 | 1090 | return seq != ctx->cached_cq_tail |
31af27c7 | 1091 | + atomic_read(&ctx->cached_cq_overflow); |
2bc9930e | 1092 | } |
de0617e4 | 1093 | |
9d858b21 | 1094 | return false; |
de0617e4 JA |
1095 | } |
1096 | ||
de0617e4 | 1097 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
2b188cc1 | 1098 | { |
75b28aff | 1099 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 1100 | |
07910158 PB |
1101 | /* order cqe stores with ring update */ |
1102 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); | |
2b188cc1 | 1103 | |
07910158 PB |
1104 | if (wq_has_sleeper(&ctx->cq_wait)) { |
1105 | wake_up_interruptible(&ctx->cq_wait); | |
1106 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
2b188cc1 JA |
1107 | } |
1108 | } | |
1109 | ||
51a4cc11 JA |
1110 | /* |
1111 | * Returns true if we need to defer file table putting. This can only happen | |
1112 | * from the error path with REQ_F_COMP_LOCKED set. | |
1113 | */ | |
1114 | static bool io_req_clean_work(struct io_kiocb *req) | |
18d9be1a | 1115 | { |
7cdaf587 | 1116 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
51a4cc11 JA |
1117 | return false; |
1118 | ||
1119 | req->flags &= ~REQ_F_WORK_INITIALIZED; | |
7cdaf587 | 1120 | |
cccf0ee8 JA |
1121 | if (req->work.mm) { |
1122 | mmdrop(req->work.mm); | |
1123 | req->work.mm = NULL; | |
1124 | } | |
1125 | if (req->work.creds) { | |
1126 | put_cred(req->work.creds); | |
1127 | req->work.creds = NULL; | |
1128 | } | |
ff002b30 JA |
1129 | if (req->work.fs) { |
1130 | struct fs_struct *fs = req->work.fs; | |
1131 | ||
51a4cc11 JA |
1132 | if (req->flags & REQ_F_COMP_LOCKED) |
1133 | return true; | |
1134 | ||
ff002b30 JA |
1135 | spin_lock(&req->work.fs->lock); |
1136 | if (--fs->users) | |
1137 | fs = NULL; | |
1138 | spin_unlock(&req->work.fs->lock); | |
1139 | if (fs) | |
1140 | free_fs_struct(fs); | |
b65e0dd6 | 1141 | req->work.fs = NULL; |
ff002b30 | 1142 | } |
51a4cc11 JA |
1143 | |
1144 | return false; | |
561fb04a JA |
1145 | } |
1146 | ||
cbdcb435 | 1147 | static void io_prep_async_work(struct io_kiocb *req) |
18d9be1a | 1148 | { |
d3656344 | 1149 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
54a91f3b | 1150 | |
16d59803 PB |
1151 | io_req_init_async(req); |
1152 | ||
d3656344 JA |
1153 | if (req->flags & REQ_F_ISREG) { |
1154 | if (def->hash_reg_file) | |
8766dd51 | 1155 | io_wq_hash_work(&req->work, file_inode(req->file)); |
d3656344 JA |
1156 | } else { |
1157 | if (def->unbound_nonreg_file) | |
3529d8c2 | 1158 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
54a91f3b | 1159 | } |
dca9cf8b PB |
1160 | if (!req->work.mm && def->needs_mm) { |
1161 | mmgrab(current->mm); | |
1162 | req->work.mm = current->mm; | |
1163 | } | |
1164 | if (!req->work.creds) | |
1165 | req->work.creds = get_current_cred(); | |
1166 | if (!req->work.fs && def->needs_fs) { | |
1167 | spin_lock(¤t->fs->lock); | |
1168 | if (!current->fs->in_exec) { | |
1169 | req->work.fs = current->fs; | |
1170 | req->work.fs->users++; | |
1171 | } else { | |
1172 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1173 | } | |
1174 | spin_unlock(¤t->fs->lock); | |
1175 | } | |
57f1a649 PB |
1176 | if (def->needs_fsize) |
1177 | req->work.fsize = rlimit(RLIMIT_FSIZE); | |
1178 | else | |
1179 | req->work.fsize = RLIM_INFINITY; | |
561fb04a | 1180 | } |
cccf0ee8 | 1181 | |
cbdcb435 | 1182 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1183 | { |
cbdcb435 | 1184 | struct io_kiocb *cur; |
54a91f3b | 1185 | |
cbdcb435 PB |
1186 | io_prep_async_work(req); |
1187 | if (req->flags & REQ_F_LINK_HEAD) | |
1188 | list_for_each_entry(cur, &req->link_list, link_list) | |
1189 | io_prep_async_work(cur); | |
561fb04a JA |
1190 | } |
1191 | ||
7271ef3a | 1192 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
561fb04a | 1193 | { |
a197f664 | 1194 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1195 | struct io_kiocb *link = io_prep_linked_timeout(req); |
561fb04a | 1196 | |
8766dd51 PB |
1197 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1198 | &req->work, req->flags); | |
1199 | io_wq_enqueue(ctx->io_wq, &req->work); | |
7271ef3a | 1200 | return link; |
18d9be1a JA |
1201 | } |
1202 | ||
cbdcb435 PB |
1203 | static void io_queue_async_work(struct io_kiocb *req) |
1204 | { | |
7271ef3a JA |
1205 | struct io_kiocb *link; |
1206 | ||
cbdcb435 PB |
1207 | /* init ->work of the whole link before punting */ |
1208 | io_prep_async_link(req); | |
7271ef3a JA |
1209 | link = __io_queue_async_work(req); |
1210 | ||
1211 | if (link) | |
1212 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1213 | } |
1214 | ||
5262f567 JA |
1215 | static void io_kill_timeout(struct io_kiocb *req) |
1216 | { | |
1217 | int ret; | |
1218 | ||
2d28390a | 1219 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
5262f567 | 1220 | if (ret != -1) { |
01cec8c1 PB |
1221 | atomic_set(&req->ctx->cq_timeouts, |
1222 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1223 | list_del_init(&req->timeout.list); |
f0e20b89 | 1224 | req->flags |= REQ_F_COMP_LOCKED; |
78e19bbe | 1225 | io_cqring_fill_event(req, 0); |
ec9c02ad | 1226 | io_put_req(req); |
5262f567 JA |
1227 | } |
1228 | } | |
1229 | ||
1230 | static void io_kill_timeouts(struct io_ring_ctx *ctx) | |
1231 | { | |
1232 | struct io_kiocb *req, *tmp; | |
1233 | ||
1234 | spin_lock_irq(&ctx->completion_lock); | |
135fcde8 | 1235 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) |
5262f567 JA |
1236 | io_kill_timeout(req); |
1237 | spin_unlock_irq(&ctx->completion_lock); | |
1238 | } | |
1239 | ||
04518945 | 1240 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1241 | { |
04518945 | 1242 | do { |
27dc8338 PB |
1243 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1244 | struct io_defer_entry, list); | |
7271ef3a | 1245 | struct io_kiocb *link; |
de0617e4 | 1246 | |
9cf7c104 | 1247 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1248 | break; |
27dc8338 | 1249 | list_del_init(&de->list); |
cbdcb435 | 1250 | /* punt-init is done before queueing for defer */ |
7271ef3a JA |
1251 | link = __io_queue_async_work(de->req); |
1252 | if (link) { | |
1253 | __io_queue_linked_timeout(link); | |
1254 | /* drop submission reference */ | |
1255 | link->flags |= REQ_F_COMP_LOCKED; | |
1256 | io_put_req(link); | |
1257 | } | |
27dc8338 | 1258 | kfree(de); |
04518945 PB |
1259 | } while (!list_empty(&ctx->defer_list)); |
1260 | } | |
1261 | ||
360428f8 | 1262 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
de0617e4 | 1263 | { |
360428f8 PB |
1264 | while (!list_empty(&ctx->timeout_list)) { |
1265 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, | |
135fcde8 | 1266 | struct io_kiocb, timeout.list); |
de0617e4 | 1267 | |
8eb7e2d0 | 1268 | if (io_is_timeout_noseq(req)) |
360428f8 | 1269 | break; |
bfe68a22 PB |
1270 | if (req->timeout.target_seq != ctx->cached_cq_tail |
1271 | - atomic_read(&ctx->cq_timeouts)) | |
360428f8 | 1272 | break; |
bfe68a22 | 1273 | |
135fcde8 | 1274 | list_del_init(&req->timeout.list); |
5262f567 | 1275 | io_kill_timeout(req); |
360428f8 PB |
1276 | } |
1277 | } | |
5262f567 | 1278 | |
360428f8 PB |
1279 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
1280 | { | |
1281 | io_flush_timeouts(ctx); | |
de0617e4 JA |
1282 | __io_commit_cqring(ctx); |
1283 | ||
04518945 PB |
1284 | if (unlikely(!list_empty(&ctx->defer_list))) |
1285 | __io_queue_deferred(ctx); | |
de0617e4 JA |
1286 | } |
1287 | ||
2b188cc1 JA |
1288 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
1289 | { | |
75b28aff | 1290 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
1291 | unsigned tail; |
1292 | ||
1293 | tail = ctx->cached_cq_tail; | |
115e12e5 SB |
1294 | /* |
1295 | * writes to the cq entry need to come after reading head; the | |
1296 | * control dependency is enough as we're using WRITE_ONCE to | |
1297 | * fill the cq entry | |
1298 | */ | |
75b28aff | 1299 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
2b188cc1 JA |
1300 | return NULL; |
1301 | ||
1302 | ctx->cached_cq_tail++; | |
75b28aff | 1303 | return &rings->cqes[tail & ctx->cq_mask]; |
2b188cc1 JA |
1304 | } |
1305 | ||
f2842ab5 JA |
1306 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1307 | { | |
f0b493e6 JA |
1308 | if (!ctx->cq_ev_fd) |
1309 | return false; | |
7e55a19c SG |
1310 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1311 | return false; | |
f2842ab5 JA |
1312 | if (!ctx->eventfd_async) |
1313 | return true; | |
b41e9852 | 1314 | return io_wq_current_is_worker(); |
f2842ab5 JA |
1315 | } |
1316 | ||
b41e9852 | 1317 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 JA |
1318 | { |
1319 | if (waitqueue_active(&ctx->wait)) | |
1320 | wake_up(&ctx->wait); | |
1321 | if (waitqueue_active(&ctx->sqo_wait)) | |
1322 | wake_up(&ctx->sqo_wait); | |
b41e9852 | 1323 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 JA |
1324 | eventfd_signal(ctx->cq_ev_fd, 1); |
1325 | } | |
1326 | ||
46930143 PB |
1327 | static void io_cqring_mark_overflow(struct io_ring_ctx *ctx) |
1328 | { | |
1329 | if (list_empty(&ctx->cq_overflow_list)) { | |
1330 | clear_bit(0, &ctx->sq_check_overflow); | |
1331 | clear_bit(0, &ctx->cq_check_overflow); | |
1332 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; | |
1333 | } | |
1334 | } | |
1335 | ||
c4a2ed72 JA |
1336 | /* Returns true if there are no backlogged entries after the flush */ |
1337 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) | |
1d7bb1d5 JA |
1338 | { |
1339 | struct io_rings *rings = ctx->rings; | |
1340 | struct io_uring_cqe *cqe; | |
1341 | struct io_kiocb *req; | |
1342 | unsigned long flags; | |
1343 | LIST_HEAD(list); | |
1344 | ||
1345 | if (!force) { | |
1346 | if (list_empty_careful(&ctx->cq_overflow_list)) | |
c4a2ed72 | 1347 | return true; |
1d7bb1d5 JA |
1348 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
1349 | rings->cq_ring_entries)) | |
c4a2ed72 | 1350 | return false; |
1d7bb1d5 JA |
1351 | } |
1352 | ||
1353 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
1354 | ||
1355 | /* if force is set, the ring is going away. always drop after that */ | |
1356 | if (force) | |
69b3e546 | 1357 | ctx->cq_overflow_flushed = 1; |
1d7bb1d5 | 1358 | |
c4a2ed72 | 1359 | cqe = NULL; |
1d7bb1d5 JA |
1360 | while (!list_empty(&ctx->cq_overflow_list)) { |
1361 | cqe = io_get_cqring(ctx); | |
1362 | if (!cqe && !force) | |
1363 | break; | |
1364 | ||
1365 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, | |
40d8ddd4 PB |
1366 | compl.list); |
1367 | list_move(&req->compl.list, &list); | |
2ca10259 | 1368 | req->flags &= ~REQ_F_OVERFLOW; |
1d7bb1d5 JA |
1369 | if (cqe) { |
1370 | WRITE_ONCE(cqe->user_data, req->user_data); | |
1371 | WRITE_ONCE(cqe->res, req->result); | |
0f7e466b | 1372 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
1d7bb1d5 JA |
1373 | } else { |
1374 | WRITE_ONCE(ctx->rings->cq_overflow, | |
1375 | atomic_inc_return(&ctx->cached_cq_overflow)); | |
1376 | } | |
1377 | } | |
1378 | ||
1379 | io_commit_cqring(ctx); | |
46930143 PB |
1380 | io_cqring_mark_overflow(ctx); |
1381 | ||
1d7bb1d5 JA |
1382 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
1383 | io_cqring_ev_posted(ctx); | |
1384 | ||
1385 | while (!list_empty(&list)) { | |
40d8ddd4 PB |
1386 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
1387 | list_del(&req->compl.list); | |
ec9c02ad | 1388 | io_put_req(req); |
1d7bb1d5 | 1389 | } |
c4a2ed72 JA |
1390 | |
1391 | return cqe != NULL; | |
1d7bb1d5 JA |
1392 | } |
1393 | ||
bcda7baa | 1394 | static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags) |
2b188cc1 | 1395 | { |
78e19bbe | 1396 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1397 | struct io_uring_cqe *cqe; |
1398 | ||
78e19bbe | 1399 | trace_io_uring_complete(ctx, req->user_data, res); |
51c3ff62 | 1400 | |
2b188cc1 JA |
1401 | /* |
1402 | * If we can't get a cq entry, userspace overflowed the | |
1403 | * submission (by quite a lot). Increment the overflow count in | |
1404 | * the ring. | |
1405 | */ | |
1406 | cqe = io_get_cqring(ctx); | |
1d7bb1d5 | 1407 | if (likely(cqe)) { |
78e19bbe | 1408 | WRITE_ONCE(cqe->user_data, req->user_data); |
2b188cc1 | 1409 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1410 | WRITE_ONCE(cqe->flags, cflags); |
1d7bb1d5 | 1411 | } else if (ctx->cq_overflow_flushed) { |
498ccd9e JA |
1412 | WRITE_ONCE(ctx->rings->cq_overflow, |
1413 | atomic_inc_return(&ctx->cached_cq_overflow)); | |
1d7bb1d5 | 1414 | } else { |
ad3eb2c8 JA |
1415 | if (list_empty(&ctx->cq_overflow_list)) { |
1416 | set_bit(0, &ctx->sq_check_overflow); | |
1417 | set_bit(0, &ctx->cq_check_overflow); | |
6d5f9049 | 1418 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
ad3eb2c8 | 1419 | } |
40d8ddd4 | 1420 | io_clean_op(req); |
2ca10259 | 1421 | req->flags |= REQ_F_OVERFLOW; |
1d7bb1d5 | 1422 | req->result = res; |
0f7e466b | 1423 | req->compl.cflags = cflags; |
40d8ddd4 PB |
1424 | refcount_inc(&req->refs); |
1425 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); | |
2b188cc1 JA |
1426 | } |
1427 | } | |
1428 | ||
bcda7baa JA |
1429 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
1430 | { | |
1431 | __io_cqring_fill_event(req, res, 0); | |
1432 | } | |
1433 | ||
e1e16097 | 1434 | static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags) |
2b188cc1 | 1435 | { |
78e19bbe | 1436 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1437 | unsigned long flags; |
1438 | ||
1439 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
bcda7baa | 1440 | __io_cqring_fill_event(req, res, cflags); |
2b188cc1 JA |
1441 | io_commit_cqring(ctx); |
1442 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
1443 | ||
8c838788 | 1444 | io_cqring_ev_posted(ctx); |
2b188cc1 JA |
1445 | } |
1446 | ||
229a7b63 | 1447 | static void io_submit_flush_completions(struct io_comp_state *cs) |
bcda7baa | 1448 | { |
229a7b63 JA |
1449 | struct io_ring_ctx *ctx = cs->ctx; |
1450 | ||
1451 | spin_lock_irq(&ctx->completion_lock); | |
1452 | while (!list_empty(&cs->list)) { | |
1453 | struct io_kiocb *req; | |
1454 | ||
3ca405eb PB |
1455 | req = list_first_entry(&cs->list, struct io_kiocb, compl.list); |
1456 | list_del(&req->compl.list); | |
0f7e466b | 1457 | __io_cqring_fill_event(req, req->result, req->compl.cflags); |
229a7b63 JA |
1458 | if (!(req->flags & REQ_F_LINK_HEAD)) { |
1459 | req->flags |= REQ_F_COMP_LOCKED; | |
1460 | io_put_req(req); | |
1461 | } else { | |
1462 | spin_unlock_irq(&ctx->completion_lock); | |
1463 | io_put_req(req); | |
1464 | spin_lock_irq(&ctx->completion_lock); | |
1465 | } | |
1466 | } | |
1467 | io_commit_cqring(ctx); | |
1468 | spin_unlock_irq(&ctx->completion_lock); | |
1469 | ||
1470 | io_cqring_ev_posted(ctx); | |
1471 | cs->nr = 0; | |
1472 | } | |
1473 | ||
1474 | static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags, | |
1475 | struct io_comp_state *cs) | |
1476 | { | |
1477 | if (!cs) { | |
1478 | io_cqring_add_event(req, res, cflags); | |
1479 | io_put_req(req); | |
1480 | } else { | |
3ca405eb | 1481 | io_clean_op(req); |
229a7b63 | 1482 | req->result = res; |
0f7e466b | 1483 | req->compl.cflags = cflags; |
3ca405eb | 1484 | list_add_tail(&req->compl.list, &cs->list); |
229a7b63 JA |
1485 | if (++cs->nr >= 32) |
1486 | io_submit_flush_completions(cs); | |
1487 | } | |
e1e16097 JA |
1488 | } |
1489 | ||
1490 | static void io_req_complete(struct io_kiocb *req, long res) | |
bcda7baa | 1491 | { |
229a7b63 | 1492 | __io_req_complete(req, res, 0, NULL); |
bcda7baa JA |
1493 | } |
1494 | ||
0ddf92e8 JA |
1495 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
1496 | { | |
1497 | return req == (struct io_kiocb *) | |
1498 | ((unsigned long) req->ctx->fallback_req & ~1UL); | |
1499 | } | |
1500 | ||
1501 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) | |
1502 | { | |
1503 | struct io_kiocb *req; | |
1504 | ||
1505 | req = ctx->fallback_req; | |
dd461af6 | 1506 | if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req)) |
0ddf92e8 JA |
1507 | return req; |
1508 | ||
1509 | return NULL; | |
1510 | } | |
1511 | ||
0553b8bd PB |
1512 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx, |
1513 | struct io_submit_state *state) | |
2b188cc1 | 1514 | { |
fd6fab2c | 1515 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
2b188cc1 JA |
1516 | struct io_kiocb *req; |
1517 | ||
f6b6c7d6 | 1518 | if (!state->free_reqs) { |
2579f913 JA |
1519 | size_t sz; |
1520 | int ret; | |
1521 | ||
1522 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); | |
fd6fab2c JA |
1523 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
1524 | ||
1525 | /* | |
1526 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1527 | * retry single alloc to be on the safe side. | |
1528 | */ | |
1529 | if (unlikely(ret <= 0)) { | |
1530 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
1531 | if (!state->reqs[0]) | |
0ddf92e8 | 1532 | goto fallback; |
fd6fab2c JA |
1533 | ret = 1; |
1534 | } | |
2579f913 | 1535 | state->free_reqs = ret - 1; |
6c8a3134 | 1536 | req = state->reqs[ret - 1]; |
2579f913 | 1537 | } else { |
2579f913 | 1538 | state->free_reqs--; |
6c8a3134 | 1539 | req = state->reqs[state->free_reqs]; |
2b188cc1 JA |
1540 | } |
1541 | ||
2579f913 | 1542 | return req; |
0ddf92e8 | 1543 | fallback: |
0553b8bd | 1544 | return io_get_fallback_req(ctx); |
2b188cc1 JA |
1545 | } |
1546 | ||
8da11c19 PB |
1547 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
1548 | bool fixed) | |
1549 | { | |
1550 | if (fixed) | |
05589553 | 1551 | percpu_ref_put(req->fixed_file_refs); |
8da11c19 PB |
1552 | else |
1553 | fput(file); | |
1554 | } | |
1555 | ||
51a4cc11 | 1556 | static bool io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1557 | { |
3ca405eb | 1558 | io_clean_op(req); |
929a3af9 | 1559 | |
5acbbc8e JA |
1560 | if (req->io) |
1561 | kfree(req->io); | |
8da11c19 PB |
1562 | if (req->file) |
1563 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); | |
fcb323cc | 1564 | |
fcb323cc | 1565 | if (req->flags & REQ_F_INFLIGHT) { |
c6ca97b3 | 1566 | struct io_ring_ctx *ctx = req->ctx; |
fcb323cc JA |
1567 | unsigned long flags; |
1568 | ||
1569 | spin_lock_irqsave(&ctx->inflight_lock, flags); | |
1570 | list_del(&req->inflight_entry); | |
1571 | if (waitqueue_active(&ctx->inflight_wait)) | |
1572 | wake_up(&ctx->inflight_wait); | |
1573 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); | |
1574 | } | |
51a4cc11 JA |
1575 | |
1576 | return io_req_clean_work(req); | |
e65ef56d JA |
1577 | } |
1578 | ||
51a4cc11 | 1579 | static void __io_free_req_finish(struct io_kiocb *req) |
c6ca97b3 | 1580 | { |
51a4cc11 | 1581 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 1582 | |
5af1d13e | 1583 | __io_put_req_task(req); |
b1e50e54 PB |
1584 | if (likely(!io_is_fallback_req(req))) |
1585 | kmem_cache_free(req_cachep, req); | |
1586 | else | |
ecfc5177 PB |
1587 | clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req); |
1588 | percpu_ref_put(&ctx->refs); | |
e65ef56d JA |
1589 | } |
1590 | ||
51a4cc11 JA |
1591 | static void io_req_task_file_table_put(struct callback_head *cb) |
1592 | { | |
1593 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
1594 | struct fs_struct *fs = req->work.fs; | |
1595 | ||
1596 | spin_lock(&req->work.fs->lock); | |
1597 | if (--fs->users) | |
1598 | fs = NULL; | |
1599 | spin_unlock(&req->work.fs->lock); | |
1600 | if (fs) | |
1601 | free_fs_struct(fs); | |
1602 | req->work.fs = NULL; | |
1603 | __io_free_req_finish(req); | |
1604 | } | |
1605 | ||
1606 | static void __io_free_req(struct io_kiocb *req) | |
1607 | { | |
1608 | if (!io_dismantle_req(req)) { | |
1609 | __io_free_req_finish(req); | |
1610 | } else { | |
1611 | int ret; | |
1612 | ||
1613 | init_task_work(&req->task_work, io_req_task_file_table_put); | |
1614 | ret = task_work_add(req->task, &req->task_work, TWA_RESUME); | |
1615 | if (unlikely(ret)) { | |
1616 | struct task_struct *tsk; | |
1617 | ||
1618 | tsk = io_wq_get_task(req->ctx->io_wq); | |
1619 | task_work_add(tsk, &req->task_work, 0); | |
1620 | } | |
1621 | } | |
1622 | } | |
1623 | ||
a197f664 | 1624 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
2665abfd | 1625 | { |
a197f664 | 1626 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd JA |
1627 | int ret; |
1628 | ||
2d28390a | 1629 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
2665abfd | 1630 | if (ret != -1) { |
78e19bbe | 1631 | io_cqring_fill_event(req, -ECANCELED); |
2665abfd | 1632 | io_commit_cqring(ctx); |
dea3b49c | 1633 | req->flags &= ~REQ_F_LINK_HEAD; |
ec9c02ad | 1634 | io_put_req(req); |
2665abfd JA |
1635 | return true; |
1636 | } | |
1637 | ||
1638 | return false; | |
e65ef56d JA |
1639 | } |
1640 | ||
ab0b6451 | 1641 | static bool __io_kill_linked_timeout(struct io_kiocb *req) |
9e645e11 | 1642 | { |
7c86ffee | 1643 | struct io_kiocb *link; |
ab0b6451 | 1644 | bool wake_ev; |
7c86ffee PB |
1645 | |
1646 | if (list_empty(&req->link_list)) | |
ab0b6451 | 1647 | return false; |
7c86ffee PB |
1648 | link = list_first_entry(&req->link_list, struct io_kiocb, link_list); |
1649 | if (link->opcode != IORING_OP_LINK_TIMEOUT) | |
ab0b6451 | 1650 | return false; |
7c86ffee PB |
1651 | |
1652 | list_del_init(&link->link_list); | |
9b7adba9 | 1653 | link->flags |= REQ_F_COMP_LOCKED; |
7c86ffee PB |
1654 | wake_ev = io_link_cancel_timeout(link); |
1655 | req->flags &= ~REQ_F_LINK_TIMEOUT; | |
ab0b6451 JA |
1656 | return wake_ev; |
1657 | } | |
1658 | ||
1659 | static void io_kill_linked_timeout(struct io_kiocb *req) | |
9e645e11 | 1660 | { |
2665abfd | 1661 | struct io_ring_ctx *ctx = req->ctx; |
ab0b6451 | 1662 | bool wake_ev; |
9e645e11 | 1663 | |
ab0b6451 JA |
1664 | if (!(req->flags & REQ_F_COMP_LOCKED)) { |
1665 | unsigned long flags; | |
1666 | ||
1667 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
1668 | wake_ev = __io_kill_linked_timeout(req); | |
7c86ffee | 1669 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
ab0b6451 JA |
1670 | } else { |
1671 | wake_ev = __io_kill_linked_timeout(req); | |
1672 | } | |
1673 | ||
7c86ffee PB |
1674 | if (wake_ev) |
1675 | io_cqring_ev_posted(ctx); | |
1676 | } | |
1677 | ||
9b5f7bd9 | 1678 | static struct io_kiocb *io_req_link_next(struct io_kiocb *req) |
7c86ffee PB |
1679 | { |
1680 | struct io_kiocb *nxt; | |
4d7dd462 | 1681 | |
9e645e11 JA |
1682 | /* |
1683 | * The list should never be empty when we are called here. But could | |
1684 | * potentially happen if the chain is messed up, check to be on the | |
1685 | * safe side. | |
1686 | */ | |
7c86ffee | 1687 | if (unlikely(list_empty(&req->link_list))) |
9b5f7bd9 | 1688 | return NULL; |
2665abfd | 1689 | |
7c86ffee PB |
1690 | nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list); |
1691 | list_del_init(&req->link_list); | |
1692 | if (!list_empty(&nxt->link_list)) | |
1693 | nxt->flags |= REQ_F_LINK_HEAD; | |
9b5f7bd9 | 1694 | return nxt; |
9e645e11 JA |
1695 | } |
1696 | ||
1697 | /* | |
dea3b49c | 1698 | * Called if REQ_F_LINK_HEAD is set, and we fail the head request |
9e645e11 | 1699 | */ |
7c86ffee | 1700 | static void __io_fail_links(struct io_kiocb *req) |
9e645e11 | 1701 | { |
2665abfd | 1702 | struct io_ring_ctx *ctx = req->ctx; |
9e645e11 JA |
1703 | |
1704 | while (!list_empty(&req->link_list)) { | |
4493233e PB |
1705 | struct io_kiocb *link = list_first_entry(&req->link_list, |
1706 | struct io_kiocb, link_list); | |
9e645e11 | 1707 | |
4493233e | 1708 | list_del_init(&link->link_list); |
c826bd7a | 1709 | trace_io_uring_fail_link(req, link); |
2665abfd | 1710 | |
7c86ffee | 1711 | io_cqring_fill_event(link, -ECANCELED); |
9b7adba9 | 1712 | link->flags |= REQ_F_COMP_LOCKED; |
7c86ffee | 1713 | __io_double_put_req(link); |
5d960724 | 1714 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
9e645e11 | 1715 | } |
2665abfd JA |
1716 | |
1717 | io_commit_cqring(ctx); | |
2665abfd | 1718 | io_cqring_ev_posted(ctx); |
9e645e11 JA |
1719 | } |
1720 | ||
7c86ffee | 1721 | static void io_fail_links(struct io_kiocb *req) |
9e645e11 | 1722 | { |
7c86ffee | 1723 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 1724 | |
7c86ffee | 1725 | if (!(req->flags & REQ_F_COMP_LOCKED)) { |
2665abfd JA |
1726 | unsigned long flags; |
1727 | ||
2665abfd | 1728 | spin_lock_irqsave(&ctx->completion_lock, flags); |
7c86ffee | 1729 | __io_fail_links(req); |
2665abfd JA |
1730 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
1731 | } else { | |
7c86ffee | 1732 | __io_fail_links(req); |
9e645e11 JA |
1733 | } |
1734 | ||
2665abfd | 1735 | io_cqring_ev_posted(ctx); |
9e645e11 JA |
1736 | } |
1737 | ||
3fa5e0f3 | 1738 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 1739 | { |
9b0d911a | 1740 | req->flags &= ~REQ_F_LINK_HEAD; |
7c86ffee PB |
1741 | if (req->flags & REQ_F_LINK_TIMEOUT) |
1742 | io_kill_linked_timeout(req); | |
944e58bf | 1743 | |
9e645e11 JA |
1744 | /* |
1745 | * If LINK is set, we have dependent requests in this chain. If we | |
1746 | * didn't fail this request, queue the first one up, moving any other | |
1747 | * dependencies to the next request. In case of failure, fail the rest | |
1748 | * of the chain. | |
1749 | */ | |
9b5f7bd9 PB |
1750 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) |
1751 | return io_req_link_next(req); | |
1752 | io_fail_links(req); | |
1753 | return NULL; | |
4d7dd462 | 1754 | } |
9e645e11 | 1755 | |
3fa5e0f3 PB |
1756 | static struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
1757 | { | |
1758 | if (likely(!(req->flags & REQ_F_LINK_HEAD))) | |
1759 | return NULL; | |
1760 | return __io_req_find_next(req); | |
1761 | } | |
1762 | ||
c2c4c83c JA |
1763 | static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb) |
1764 | { | |
1765 | struct task_struct *tsk = req->task; | |
1766 | struct io_ring_ctx *ctx = req->ctx; | |
0ba9c9ed | 1767 | int ret, notify; |
c2c4c83c JA |
1768 | |
1769 | /* | |
0ba9c9ed JA |
1770 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
1771 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
1772 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
1773 | * will do the job. | |
c2c4c83c | 1774 | */ |
0ba9c9ed JA |
1775 | notify = 0; |
1776 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) | |
c2c4c83c JA |
1777 | notify = TWA_SIGNAL; |
1778 | ||
1779 | ret = task_work_add(tsk, cb, notify); | |
1780 | if (!ret) | |
1781 | wake_up_process(tsk); | |
0ba9c9ed | 1782 | |
c2c4c83c JA |
1783 | return ret; |
1784 | } | |
1785 | ||
c40f6379 JA |
1786 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
1787 | { | |
1788 | struct io_ring_ctx *ctx = req->ctx; | |
1789 | ||
1790 | spin_lock_irq(&ctx->completion_lock); | |
1791 | io_cqring_fill_event(req, error); | |
1792 | io_commit_cqring(ctx); | |
1793 | spin_unlock_irq(&ctx->completion_lock); | |
1794 | ||
1795 | io_cqring_ev_posted(ctx); | |
1796 | req_set_fail_links(req); | |
1797 | io_double_put_req(req); | |
1798 | } | |
1799 | ||
1800 | static void io_req_task_cancel(struct callback_head *cb) | |
1801 | { | |
1802 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
1803 | ||
1804 | __io_req_task_cancel(req, -ECANCELED); | |
1805 | } | |
1806 | ||
1807 | static void __io_req_task_submit(struct io_kiocb *req) | |
1808 | { | |
1809 | struct io_ring_ctx *ctx = req->ctx; | |
1810 | ||
c40f6379 JA |
1811 | if (!__io_sq_thread_acquire_mm(ctx)) { |
1812 | mutex_lock(&ctx->uring_lock); | |
1813 | __io_queue_sqe(req, NULL, NULL); | |
1814 | mutex_unlock(&ctx->uring_lock); | |
1815 | } else { | |
1816 | __io_req_task_cancel(req, -EFAULT); | |
1817 | } | |
1818 | } | |
1819 | ||
1820 | static void io_req_task_submit(struct callback_head *cb) | |
1821 | { | |
1822 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
6d816e08 | 1823 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 JA |
1824 | |
1825 | __io_req_task_submit(req); | |
6d816e08 | 1826 | percpu_ref_put(&ctx->refs); |
c40f6379 JA |
1827 | } |
1828 | ||
1829 | static void io_req_task_queue(struct io_kiocb *req) | |
1830 | { | |
c40f6379 JA |
1831 | int ret; |
1832 | ||
1833 | init_task_work(&req->task_work, io_req_task_submit); | |
6d816e08 | 1834 | percpu_ref_get(&req->ctx->refs); |
c40f6379 | 1835 | |
c2c4c83c | 1836 | ret = io_req_task_work_add(req, &req->task_work); |
c40f6379 | 1837 | if (unlikely(ret)) { |
c2c4c83c JA |
1838 | struct task_struct *tsk; |
1839 | ||
c40f6379 JA |
1840 | init_task_work(&req->task_work, io_req_task_cancel); |
1841 | tsk = io_wq_get_task(req->ctx->io_wq); | |
c2c4c83c JA |
1842 | task_work_add(tsk, &req->task_work, 0); |
1843 | wake_up_process(tsk); | |
c40f6379 | 1844 | } |
c40f6379 JA |
1845 | } |
1846 | ||
c3524383 | 1847 | static void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 1848 | { |
9b5f7bd9 | 1849 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
1850 | |
1851 | if (nxt) | |
906a8c3f | 1852 | io_req_task_queue(nxt); |
c69f8dbe JL |
1853 | } |
1854 | ||
c3524383 | 1855 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 1856 | { |
c3524383 PB |
1857 | io_queue_next(req); |
1858 | __io_free_req(req); | |
1859 | } | |
8766dd51 | 1860 | |
2d6500d4 PB |
1861 | struct req_batch { |
1862 | void *reqs[IO_IOPOLL_BATCH]; | |
1863 | int to_free; | |
7a743e22 | 1864 | |
5af1d13e PB |
1865 | struct task_struct *task; |
1866 | int task_refs; | |
2d6500d4 PB |
1867 | }; |
1868 | ||
5af1d13e PB |
1869 | static inline void io_init_req_batch(struct req_batch *rb) |
1870 | { | |
1871 | rb->to_free = 0; | |
1872 | rb->task_refs = 0; | |
1873 | rb->task = NULL; | |
1874 | } | |
1875 | ||
2d6500d4 PB |
1876 | static void __io_req_free_batch_flush(struct io_ring_ctx *ctx, |
1877 | struct req_batch *rb) | |
1878 | { | |
1879 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); | |
1880 | percpu_ref_put_many(&ctx->refs, rb->to_free); | |
1881 | rb->to_free = 0; | |
1882 | } | |
1883 | ||
1884 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, | |
1885 | struct req_batch *rb) | |
1886 | { | |
1887 | if (rb->to_free) | |
1888 | __io_req_free_batch_flush(ctx, rb); | |
5af1d13e PB |
1889 | if (rb->task) { |
1890 | put_task_struct_many(rb->task, rb->task_refs); | |
1891 | rb->task = NULL; | |
1892 | } | |
2d6500d4 PB |
1893 | } |
1894 | ||
1895 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req) | |
1896 | { | |
1897 | if (unlikely(io_is_fallback_req(req))) { | |
1898 | io_free_req(req); | |
1899 | return; | |
1900 | } | |
1901 | if (req->flags & REQ_F_LINK_HEAD) | |
1902 | io_queue_next(req); | |
1903 | ||
5af1d13e PB |
1904 | if (req->flags & REQ_F_TASK_PINNED) { |
1905 | if (req->task != rb->task) { | |
1906 | if (rb->task) | |
1907 | put_task_struct_many(rb->task, rb->task_refs); | |
1908 | rb->task = req->task; | |
1909 | rb->task_refs = 0; | |
1910 | } | |
1911 | rb->task_refs++; | |
1912 | req->flags &= ~REQ_F_TASK_PINNED; | |
1913 | } | |
1914 | ||
51a4cc11 | 1915 | WARN_ON_ONCE(io_dismantle_req(req)); |
2d6500d4 PB |
1916 | rb->reqs[rb->to_free++] = req; |
1917 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) | |
1918 | __io_req_free_batch_flush(req->ctx, rb); | |
7a743e22 PB |
1919 | } |
1920 | ||
ba816ad6 JA |
1921 | /* |
1922 | * Drop reference to request, return next in chain (if there is one) if this | |
1923 | * was the last reference to this request. | |
1924 | */ | |
9b5f7bd9 | 1925 | static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 1926 | { |
9b5f7bd9 PB |
1927 | struct io_kiocb *nxt = NULL; |
1928 | ||
2a44f467 | 1929 | if (refcount_dec_and_test(&req->refs)) { |
9b5f7bd9 | 1930 | nxt = io_req_find_next(req); |
4d7dd462 | 1931 | __io_free_req(req); |
2a44f467 | 1932 | } |
9b5f7bd9 | 1933 | return nxt; |
2b188cc1 JA |
1934 | } |
1935 | ||
e65ef56d JA |
1936 | static void io_put_req(struct io_kiocb *req) |
1937 | { | |
1938 | if (refcount_dec_and_test(&req->refs)) | |
1939 | io_free_req(req); | |
2b188cc1 JA |
1940 | } |
1941 | ||
f4db7182 | 1942 | static struct io_wq_work *io_steal_work(struct io_kiocb *req) |
7a743e22 | 1943 | { |
6df1db6b | 1944 | struct io_kiocb *nxt; |
f4db7182 | 1945 | |
7a743e22 | 1946 | /* |
f4db7182 PB |
1947 | * A ref is owned by io-wq in which context we're. So, if that's the |
1948 | * last one, it's safe to steal next work. False negatives are Ok, | |
1949 | * it just will be re-punted async in io_put_work() | |
7a743e22 | 1950 | */ |
f4db7182 PB |
1951 | if (refcount_read(&req->refs) != 1) |
1952 | return NULL; | |
7a743e22 | 1953 | |
9b5f7bd9 | 1954 | nxt = io_req_find_next(req); |
6df1db6b | 1955 | return nxt ? &nxt->work : NULL; |
7a743e22 PB |
1956 | } |
1957 | ||
978db57e JA |
1958 | /* |
1959 | * Must only be used if we don't need to care about links, usually from | |
1960 | * within the completion handling itself. | |
1961 | */ | |
1962 | static void __io_double_put_req(struct io_kiocb *req) | |
78e19bbe JA |
1963 | { |
1964 | /* drop both submit and complete references */ | |
1965 | if (refcount_sub_and_test(2, &req->refs)) | |
1966 | __io_free_req(req); | |
1967 | } | |
1968 | ||
978db57e JA |
1969 | static void io_double_put_req(struct io_kiocb *req) |
1970 | { | |
1971 | /* drop both submit and complete references */ | |
1972 | if (refcount_sub_and_test(2, &req->refs)) | |
1973 | io_free_req(req); | |
1974 | } | |
1975 | ||
1d7bb1d5 | 1976 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
a3a0e43f | 1977 | { |
84f97dc2 JA |
1978 | struct io_rings *rings = ctx->rings; |
1979 | ||
ad3eb2c8 JA |
1980 | if (test_bit(0, &ctx->cq_check_overflow)) { |
1981 | /* | |
1982 | * noflush == true is from the waitqueue handler, just ensure | |
1983 | * we wake up the task, and the next invocation will flush the | |
1984 | * entries. We cannot safely to it from here. | |
1985 | */ | |
1986 | if (noflush && !list_empty(&ctx->cq_overflow_list)) | |
1987 | return -1U; | |
1d7bb1d5 | 1988 | |
ad3eb2c8 JA |
1989 | io_cqring_overflow_flush(ctx, false); |
1990 | } | |
1d7bb1d5 | 1991 | |
a3a0e43f JA |
1992 | /* See comment at the top of this file */ |
1993 | smp_rmb(); | |
ad3eb2c8 | 1994 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
a3a0e43f JA |
1995 | } |
1996 | ||
fb5ccc98 PB |
1997 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
1998 | { | |
1999 | struct io_rings *rings = ctx->rings; | |
2000 | ||
2001 | /* make sure SQ entry isn't read before tail */ | |
2002 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2003 | } | |
2004 | ||
8ff069bf | 2005 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2006 | { |
8ff069bf | 2007 | unsigned int cflags; |
e94f141b | 2008 | |
bcda7baa JA |
2009 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2010 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2011 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2012 | kfree(kbuf); |
2013 | return cflags; | |
e94f141b JA |
2014 | } |
2015 | ||
8ff069bf | 2016 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2017 | { |
4d954c25 | 2018 | struct io_buffer *kbuf; |
bcda7baa | 2019 | |
4d954c25 | 2020 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2021 | return io_put_kbuf(req, kbuf); |
2022 | } | |
2023 | ||
4c6e277c JA |
2024 | static inline bool io_run_task_work(void) |
2025 | { | |
2026 | if (current->task_works) { | |
2027 | __set_current_state(TASK_RUNNING); | |
2028 | task_work_run(); | |
2029 | return true; | |
2030 | } | |
2031 | ||
2032 | return false; | |
bcda7baa JA |
2033 | } |
2034 | ||
bbde017a XW |
2035 | static void io_iopoll_queue(struct list_head *again) |
2036 | { | |
2037 | struct io_kiocb *req; | |
2038 | ||
2039 | do { | |
d21ffe7e PB |
2040 | req = list_first_entry(again, struct io_kiocb, inflight_entry); |
2041 | list_del(&req->inflight_entry); | |
81b68a5c | 2042 | __io_complete_rw(req, -EAGAIN, 0, NULL); |
bbde017a XW |
2043 | } while (!list_empty(again)); |
2044 | } | |
2045 | ||
def596e9 JA |
2046 | /* |
2047 | * Find and free completed poll iocbs | |
2048 | */ | |
2049 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2050 | struct list_head *done) | |
2051 | { | |
8237e045 | 2052 | struct req_batch rb; |
def596e9 | 2053 | struct io_kiocb *req; |
bbde017a XW |
2054 | LIST_HEAD(again); |
2055 | ||
2056 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2057 | smp_rmb(); | |
def596e9 | 2058 | |
5af1d13e | 2059 | io_init_req_batch(&rb); |
def596e9 | 2060 | while (!list_empty(done)) { |
bcda7baa JA |
2061 | int cflags = 0; |
2062 | ||
d21ffe7e | 2063 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
bbde017a XW |
2064 | if (READ_ONCE(req->result) == -EAGAIN) { |
2065 | req->iopoll_completed = 0; | |
d21ffe7e | 2066 | list_move_tail(&req->inflight_entry, &again); |
bbde017a XW |
2067 | continue; |
2068 | } | |
d21ffe7e | 2069 | list_del(&req->inflight_entry); |
def596e9 | 2070 | |
bcda7baa | 2071 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2072 | cflags = io_put_rw_kbuf(req); |
bcda7baa JA |
2073 | |
2074 | __io_cqring_fill_event(req, req->result, cflags); | |
def596e9 JA |
2075 | (*nr_events)++; |
2076 | ||
c3524383 | 2077 | if (refcount_dec_and_test(&req->refs)) |
2d6500d4 | 2078 | io_req_free_batch(&rb, req); |
def596e9 | 2079 | } |
def596e9 | 2080 | |
09bb8394 | 2081 | io_commit_cqring(ctx); |
32b2244a XW |
2082 | if (ctx->flags & IORING_SETUP_SQPOLL) |
2083 | io_cqring_ev_posted(ctx); | |
2d6500d4 | 2084 | io_req_free_batch_finish(ctx, &rb); |
581f9810 | 2085 | |
bbde017a XW |
2086 | if (!list_empty(&again)) |
2087 | io_iopoll_queue(&again); | |
581f9810 BM |
2088 | } |
2089 | ||
def596e9 JA |
2090 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
2091 | long min) | |
2092 | { | |
2093 | struct io_kiocb *req, *tmp; | |
2094 | LIST_HEAD(done); | |
2095 | bool spin; | |
2096 | int ret; | |
2097 | ||
2098 | /* | |
2099 | * Only spin for completions if we don't have multiple devices hanging | |
2100 | * off our complete list, and we're under the requested amount. | |
2101 | */ | |
2102 | spin = !ctx->poll_multi_file && *nr_events < min; | |
2103 | ||
2104 | ret = 0; | |
d21ffe7e | 2105 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2106 | struct kiocb *kiocb = &req->rw.kiocb; |
def596e9 JA |
2107 | |
2108 | /* | |
581f9810 BM |
2109 | * Move completed and retryable entries to our local lists. |
2110 | * If we find a request that requires polling, break out | |
2111 | * and complete those lists first, if we have entries there. | |
def596e9 | 2112 | */ |
65a6543d | 2113 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2114 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2115 | continue; |
2116 | } | |
2117 | if (!list_empty(&done)) | |
2118 | break; | |
2119 | ||
2120 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
2121 | if (ret < 0) | |
2122 | break; | |
2123 | ||
3aadc23e PB |
2124 | /* iopoll may have completed current req */ |
2125 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2126 | list_move_tail(&req->inflight_entry, &done); |
3aadc23e | 2127 | |
def596e9 JA |
2128 | if (ret && spin) |
2129 | spin = false; | |
2130 | ret = 0; | |
2131 | } | |
2132 | ||
2133 | if (!list_empty(&done)) | |
2134 | io_iopoll_complete(ctx, nr_events, &done); | |
2135 | ||
2136 | return ret; | |
2137 | } | |
2138 | ||
2139 | /* | |
d195a66e | 2140 | * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a |
def596e9 JA |
2141 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
2142 | * as a non-spinning completion check. | |
2143 | */ | |
2144 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2145 | long min) | |
2146 | { | |
540e32a0 | 2147 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
def596e9 JA |
2148 | int ret; |
2149 | ||
2150 | ret = io_do_iopoll(ctx, nr_events, min); | |
2151 | if (ret < 0) | |
2152 | return ret; | |
eba0a4dd | 2153 | if (*nr_events >= min) |
def596e9 JA |
2154 | return 0; |
2155 | } | |
2156 | ||
2157 | return 1; | |
2158 | } | |
2159 | ||
2160 | /* | |
2161 | * We can't just wait for polled events to come to us, we have to actively | |
2162 | * find and complete them. | |
2163 | */ | |
b2edc0a7 | 2164 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2165 | { |
2166 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2167 | return; | |
2168 | ||
2169 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2170 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2171 | unsigned int nr_events = 0; |
2172 | ||
b2edc0a7 | 2173 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2174 | |
b2edc0a7 PB |
2175 | /* let it sleep and repeat later if can't complete a request */ |
2176 | if (nr_events == 0) | |
2177 | break; | |
08f5439f JA |
2178 | /* |
2179 | * Ensure we allow local-to-the-cpu processing to take place, | |
2180 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2181 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2182 | */ |
3fcee5a6 PB |
2183 | if (need_resched()) { |
2184 | mutex_unlock(&ctx->uring_lock); | |
2185 | cond_resched(); | |
2186 | mutex_lock(&ctx->uring_lock); | |
2187 | } | |
def596e9 JA |
2188 | } |
2189 | mutex_unlock(&ctx->uring_lock); | |
2190 | } | |
2191 | ||
7668b92a | 2192 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2193 | { |
7668b92a | 2194 | unsigned int nr_events = 0; |
2b2ed975 | 2195 | int iters = 0, ret = 0; |
500f9fba | 2196 | |
c7849be9 XW |
2197 | /* |
2198 | * We disallow the app entering submit/complete with polling, but we | |
2199 | * still need to lock the ring to prevent racing with polled issue | |
2200 | * that got punted to a workqueue. | |
2201 | */ | |
2202 | mutex_lock(&ctx->uring_lock); | |
def596e9 | 2203 | do { |
a3a0e43f JA |
2204 | /* |
2205 | * Don't enter poll loop if we already have events pending. | |
2206 | * If we do, we can potentially be spinning for commands that | |
2207 | * already triggered a CQE (eg in error). | |
2208 | */ | |
1d7bb1d5 | 2209 | if (io_cqring_events(ctx, false)) |
a3a0e43f JA |
2210 | break; |
2211 | ||
500f9fba JA |
2212 | /* |
2213 | * If a submit got punted to a workqueue, we can have the | |
2214 | * application entering polling for a command before it gets | |
2215 | * issued. That app will hold the uring_lock for the duration | |
2216 | * of the poll right here, so we need to take a breather every | |
2217 | * now and then to ensure that the issue has a chance to add | |
2218 | * the poll to the issued list. Otherwise we can spin here | |
2219 | * forever, while the workqueue is stuck trying to acquire the | |
2220 | * very same mutex. | |
2221 | */ | |
2222 | if (!(++iters & 7)) { | |
2223 | mutex_unlock(&ctx->uring_lock); | |
4c6e277c | 2224 | io_run_task_work(); |
500f9fba JA |
2225 | mutex_lock(&ctx->uring_lock); |
2226 | } | |
2227 | ||
7668b92a | 2228 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
def596e9 JA |
2229 | if (ret <= 0) |
2230 | break; | |
2231 | ret = 0; | |
7668b92a | 2232 | } while (min && !nr_events && !need_resched()); |
def596e9 | 2233 | |
500f9fba | 2234 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2235 | return ret; |
2236 | } | |
2237 | ||
491381ce | 2238 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2239 | { |
491381ce JA |
2240 | /* |
2241 | * Tell lockdep we inherited freeze protection from submission | |
2242 | * thread. | |
2243 | */ | |
2244 | if (req->flags & REQ_F_ISREG) { | |
2245 | struct inode *inode = file_inode(req->file); | |
2b188cc1 | 2246 | |
491381ce | 2247 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
2b188cc1 | 2248 | } |
491381ce | 2249 | file_end_write(req->file); |
2b188cc1 JA |
2250 | } |
2251 | ||
a1d7c393 JA |
2252 | static void io_complete_rw_common(struct kiocb *kiocb, long res, |
2253 | struct io_comp_state *cs) | |
2b188cc1 | 2254 | { |
9adbd45d | 2255 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
bcda7baa | 2256 | int cflags = 0; |
2b188cc1 | 2257 | |
491381ce JA |
2258 | if (kiocb->ki_flags & IOCB_WRITE) |
2259 | kiocb_end_write(req); | |
2b188cc1 | 2260 | |
4e88d6e7 JA |
2261 | if (res != req->result) |
2262 | req_set_fail_links(req); | |
bcda7baa | 2263 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2264 | cflags = io_put_rw_kbuf(req); |
a1d7c393 | 2265 | __io_req_complete(req, res, cflags, cs); |
ba816ad6 JA |
2266 | } |
2267 | ||
b63534c4 JA |
2268 | #ifdef CONFIG_BLOCK |
2269 | static bool io_resubmit_prep(struct io_kiocb *req, int error) | |
2270 | { | |
2271 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
2272 | ssize_t ret = -ECANCELED; | |
2273 | struct iov_iter iter; | |
2274 | int rw; | |
2275 | ||
2276 | if (error) { | |
2277 | ret = error; | |
2278 | goto end_req; | |
2279 | } | |
2280 | ||
2281 | switch (req->opcode) { | |
2282 | case IORING_OP_READV: | |
2283 | case IORING_OP_READ_FIXED: | |
2284 | case IORING_OP_READ: | |
2285 | rw = READ; | |
2286 | break; | |
2287 | case IORING_OP_WRITEV: | |
2288 | case IORING_OP_WRITE_FIXED: | |
2289 | case IORING_OP_WRITE: | |
2290 | rw = WRITE; | |
2291 | break; | |
2292 | default: | |
2293 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", | |
2294 | req->opcode); | |
2295 | goto end_req; | |
2296 | } | |
2297 | ||
2298 | ret = io_import_iovec(rw, req, &iovec, &iter, false); | |
2299 | if (ret < 0) | |
2300 | goto end_req; | |
ff6165b2 | 2301 | ret = io_setup_async_rw(req, iovec, inline_vecs, &iter); |
b63534c4 JA |
2302 | if (!ret) |
2303 | return true; | |
2304 | kfree(iovec); | |
2305 | end_req: | |
b63534c4 | 2306 | req_set_fail_links(req); |
e1e16097 | 2307 | io_req_complete(req, ret); |
b63534c4 JA |
2308 | return false; |
2309 | } | |
2310 | ||
2311 | static void io_rw_resubmit(struct callback_head *cb) | |
2312 | { | |
2313 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2314 | struct io_ring_ctx *ctx = req->ctx; | |
2315 | int err; | |
2316 | ||
b63534c4 JA |
2317 | err = io_sq_thread_acquire_mm(ctx, req); |
2318 | ||
2319 | if (io_resubmit_prep(req, err)) { | |
2320 | refcount_inc(&req->refs); | |
2321 | io_queue_async_work(req); | |
2322 | } | |
6d816e08 JA |
2323 | |
2324 | percpu_ref_put(&ctx->refs); | |
b63534c4 JA |
2325 | } |
2326 | #endif | |
2327 | ||
2328 | static bool io_rw_reissue(struct io_kiocb *req, long res) | |
2329 | { | |
2330 | #ifdef CONFIG_BLOCK | |
b63534c4 JA |
2331 | int ret; |
2332 | ||
2333 | if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker()) | |
2334 | return false; | |
2335 | ||
b63534c4 | 2336 | init_task_work(&req->task_work, io_rw_resubmit); |
6d816e08 JA |
2337 | percpu_ref_get(&req->ctx->refs); |
2338 | ||
c2c4c83c JA |
2339 | ret = io_req_task_work_add(req, &req->task_work); |
2340 | if (!ret) | |
b63534c4 JA |
2341 | return true; |
2342 | #endif | |
2343 | return false; | |
2344 | } | |
2345 | ||
a1d7c393 JA |
2346 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
2347 | struct io_comp_state *cs) | |
2348 | { | |
2349 | if (!io_rw_reissue(req, res)) | |
2350 | io_complete_rw_common(&req->rw.kiocb, res, cs); | |
ba816ad6 JA |
2351 | } |
2352 | ||
2353 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2354 | { | |
9adbd45d | 2355 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2356 | |
a1d7c393 | 2357 | __io_complete_rw(req, res, res2, NULL); |
2b188cc1 JA |
2358 | } |
2359 | ||
def596e9 JA |
2360 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2361 | { | |
9adbd45d | 2362 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2363 | |
491381ce JA |
2364 | if (kiocb->ki_flags & IOCB_WRITE) |
2365 | kiocb_end_write(req); | |
def596e9 | 2366 | |
2d7d6792 | 2367 | if (res != -EAGAIN && res != req->result) |
4e88d6e7 | 2368 | req_set_fail_links(req); |
bbde017a XW |
2369 | |
2370 | WRITE_ONCE(req->result, res); | |
2371 | /* order with io_poll_complete() checking ->result */ | |
cd664b0e PB |
2372 | smp_wmb(); |
2373 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2374 | } |
2375 | ||
2376 | /* | |
2377 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2378 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
2379 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
2380 | * accessing the kiocb cookie. | |
2381 | */ | |
2382 | static void io_iopoll_req_issued(struct io_kiocb *req) | |
2383 | { | |
2384 | struct io_ring_ctx *ctx = req->ctx; | |
2385 | ||
2386 | /* | |
2387 | * Track whether we have multiple files in our lists. This will impact | |
2388 | * how we do polling eventually, not spinning if we're on potentially | |
2389 | * different devices. | |
2390 | */ | |
540e32a0 | 2391 | if (list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2392 | ctx->poll_multi_file = false; |
2393 | } else if (!ctx->poll_multi_file) { | |
2394 | struct io_kiocb *list_req; | |
2395 | ||
540e32a0 | 2396 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2397 | inflight_entry); |
9adbd45d | 2398 | if (list_req->file != req->file) |
def596e9 JA |
2399 | ctx->poll_multi_file = true; |
2400 | } | |
2401 | ||
2402 | /* | |
2403 | * For fast devices, IO may have already completed. If it has, add | |
2404 | * it to the front so we find it first. | |
2405 | */ | |
65a6543d | 2406 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2407 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2408 | else |
d21ffe7e | 2409 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab XW |
2410 | |
2411 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
2412 | wq_has_sleeper(&ctx->sqo_wait)) | |
2413 | wake_up(&ctx->sqo_wait); | |
def596e9 JA |
2414 | } |
2415 | ||
9f13c35b | 2416 | static void __io_state_file_put(struct io_submit_state *state) |
9a56a232 | 2417 | { |
06ef3608 PB |
2418 | if (state->has_refs) |
2419 | fput_many(state->file, state->has_refs); | |
9f13c35b PB |
2420 | state->file = NULL; |
2421 | } | |
2422 | ||
2423 | static inline void io_state_file_put(struct io_submit_state *state) | |
2424 | { | |
2425 | if (state->file) | |
2426 | __io_state_file_put(state); | |
9a56a232 JA |
2427 | } |
2428 | ||
2429 | /* | |
2430 | * Get as many references to a file as we have IOs left in this submission, | |
2431 | * assuming most submissions are for one file, or at least that each file | |
2432 | * has more than one submission. | |
2433 | */ | |
8da11c19 | 2434 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
9a56a232 JA |
2435 | { |
2436 | if (!state) | |
2437 | return fget(fd); | |
2438 | ||
2439 | if (state->file) { | |
2440 | if (state->fd == fd) { | |
06ef3608 | 2441 | state->has_refs--; |
9a56a232 JA |
2442 | state->ios_left--; |
2443 | return state->file; | |
2444 | } | |
9f13c35b | 2445 | __io_state_file_put(state); |
9a56a232 JA |
2446 | } |
2447 | state->file = fget_many(fd, state->ios_left); | |
2448 | if (!state->file) | |
2449 | return NULL; | |
2450 | ||
2451 | state->fd = fd; | |
9a56a232 | 2452 | state->ios_left--; |
06ef3608 | 2453 | state->has_refs = state->ios_left; |
9a56a232 JA |
2454 | return state->file; |
2455 | } | |
2456 | ||
4503b767 JA |
2457 | static bool io_bdev_nowait(struct block_device *bdev) |
2458 | { | |
2459 | #ifdef CONFIG_BLOCK | |
2460 | return !bdev || queue_is_mq(bdev_get_queue(bdev)); | |
2461 | #else | |
2462 | return true; | |
2463 | #endif | |
2464 | } | |
2465 | ||
2b188cc1 JA |
2466 | /* |
2467 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2468 | * any file. For now, just ensure that anything potentially problematic is done | |
2469 | * inline. | |
2470 | */ | |
af197f50 | 2471 | static bool io_file_supports_async(struct file *file, int rw) |
2b188cc1 JA |
2472 | { |
2473 | umode_t mode = file_inode(file)->i_mode; | |
2474 | ||
4503b767 JA |
2475 | if (S_ISBLK(mode)) { |
2476 | if (io_bdev_nowait(file->f_inode->i_bdev)) | |
2477 | return true; | |
2478 | return false; | |
2479 | } | |
2480 | if (S_ISCHR(mode) || S_ISSOCK(mode)) | |
2b188cc1 | 2481 | return true; |
4503b767 JA |
2482 | if (S_ISREG(mode)) { |
2483 | if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
2484 | file->f_op != &io_uring_fops) | |
2485 | return true; | |
2486 | return false; | |
2487 | } | |
2b188cc1 | 2488 | |
c5b85625 JA |
2489 | /* any ->read/write should understand O_NONBLOCK */ |
2490 | if (file->f_flags & O_NONBLOCK) | |
2491 | return true; | |
2492 | ||
af197f50 JA |
2493 | if (!(file->f_mode & FMODE_NOWAIT)) |
2494 | return false; | |
2495 | ||
2496 | if (rw == READ) | |
2497 | return file->f_op->read_iter != NULL; | |
2498 | ||
2499 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2500 | } |
2501 | ||
3529d8c2 JA |
2502 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2503 | bool force_nonblock) | |
2b188cc1 | 2504 | { |
def596e9 | 2505 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2506 | struct kiocb *kiocb = &req->rw.kiocb; |
09bb8394 JA |
2507 | unsigned ioprio; |
2508 | int ret; | |
2b188cc1 | 2509 | |
491381ce JA |
2510 | if (S_ISREG(file_inode(req->file)->i_mode)) |
2511 | req->flags |= REQ_F_ISREG; | |
2512 | ||
2b188cc1 | 2513 | kiocb->ki_pos = READ_ONCE(sqe->off); |
ba04291e JA |
2514 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
2515 | req->flags |= REQ_F_CUR_POS; | |
2516 | kiocb->ki_pos = req->file->f_pos; | |
2517 | } | |
2b188cc1 | 2518 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2519 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2520 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2521 | if (unlikely(ret)) | |
2522 | return ret; | |
2b188cc1 JA |
2523 | |
2524 | ioprio = READ_ONCE(sqe->ioprio); | |
2525 | if (ioprio) { | |
2526 | ret = ioprio_check_cap(ioprio); | |
2527 | if (ret) | |
09bb8394 | 2528 | return ret; |
2b188cc1 JA |
2529 | |
2530 | kiocb->ki_ioprio = ioprio; | |
2531 | } else | |
2532 | kiocb->ki_ioprio = get_current_ioprio(); | |
2533 | ||
8449eeda | 2534 | /* don't allow async punt if RWF_NOWAIT was requested */ |
c5b85625 | 2535 | if (kiocb->ki_flags & IOCB_NOWAIT) |
8449eeda SB |
2536 | req->flags |= REQ_F_NOWAIT; |
2537 | ||
b63534c4 JA |
2538 | if (kiocb->ki_flags & IOCB_DIRECT) |
2539 | io_get_req_task(req); | |
2540 | ||
8449eeda | 2541 | if (force_nonblock) |
2b188cc1 | 2542 | kiocb->ki_flags |= IOCB_NOWAIT; |
8449eeda | 2543 | |
def596e9 | 2544 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2545 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2546 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2547 | return -EOPNOTSUPP; |
2b188cc1 | 2548 | |
def596e9 JA |
2549 | kiocb->ki_flags |= IOCB_HIPRI; |
2550 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 2551 | req->iopoll_completed = 0; |
f3a6fa22 | 2552 | io_get_req_task(req); |
def596e9 | 2553 | } else { |
09bb8394 JA |
2554 | if (kiocb->ki_flags & IOCB_HIPRI) |
2555 | return -EINVAL; | |
def596e9 JA |
2556 | kiocb->ki_complete = io_complete_rw; |
2557 | } | |
9adbd45d | 2558 | |
3529d8c2 JA |
2559 | req->rw.addr = READ_ONCE(sqe->addr); |
2560 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2561 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2562 | return 0; |
2b188cc1 JA |
2563 | } |
2564 | ||
2565 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2566 | { | |
2567 | switch (ret) { | |
2568 | case -EIOCBQUEUED: | |
2569 | break; | |
2570 | case -ERESTARTSYS: | |
2571 | case -ERESTARTNOINTR: | |
2572 | case -ERESTARTNOHAND: | |
2573 | case -ERESTART_RESTARTBLOCK: | |
2574 | /* | |
2575 | * We can't just restart the syscall, since previously | |
2576 | * submitted sqes may already be in progress. Just fail this | |
2577 | * IO with EINTR. | |
2578 | */ | |
2579 | ret = -EINTR; | |
2580 | /* fall through */ | |
2581 | default: | |
2582 | kiocb->ki_complete(kiocb, ret, 0); | |
2583 | } | |
2584 | } | |
2585 | ||
a1d7c393 JA |
2586 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
2587 | struct io_comp_state *cs) | |
ba816ad6 | 2588 | { |
ba04291e JA |
2589 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
2590 | ||
2591 | if (req->flags & REQ_F_CUR_POS) | |
2592 | req->file->f_pos = kiocb->ki_pos; | |
bcaec089 | 2593 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
a1d7c393 | 2594 | __io_complete_rw(req, ret, 0, cs); |
ba816ad6 JA |
2595 | else |
2596 | io_rw_done(kiocb, ret); | |
2597 | } | |
2598 | ||
9adbd45d | 2599 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
7d009165 | 2600 | struct iov_iter *iter) |
edafccee | 2601 | { |
9adbd45d JA |
2602 | struct io_ring_ctx *ctx = req->ctx; |
2603 | size_t len = req->rw.len; | |
edafccee | 2604 | struct io_mapped_ubuf *imu; |
4f4eeba8 | 2605 | u16 index, buf_index; |
edafccee JA |
2606 | size_t offset; |
2607 | u64 buf_addr; | |
2608 | ||
2609 | /* attempt to use fixed buffers without having provided iovecs */ | |
2610 | if (unlikely(!ctx->user_bufs)) | |
2611 | return -EFAULT; | |
2612 | ||
4f4eeba8 | 2613 | buf_index = req->buf_index; |
edafccee JA |
2614 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
2615 | return -EFAULT; | |
2616 | ||
2617 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); | |
2618 | imu = &ctx->user_bufs[index]; | |
9adbd45d | 2619 | buf_addr = req->rw.addr; |
edafccee JA |
2620 | |
2621 | /* overflow */ | |
2622 | if (buf_addr + len < buf_addr) | |
2623 | return -EFAULT; | |
2624 | /* not inside the mapped region */ | |
2625 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) | |
2626 | return -EFAULT; | |
2627 | ||
2628 | /* | |
2629 | * May not be a start of buffer, set size appropriately | |
2630 | * and advance us to the beginning. | |
2631 | */ | |
2632 | offset = buf_addr - imu->ubuf; | |
2633 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2634 | |
2635 | if (offset) { | |
2636 | /* | |
2637 | * Don't use iov_iter_advance() here, as it's really slow for | |
2638 | * using the latter parts of a big fixed buffer - it iterates | |
2639 | * over each segment manually. We can cheat a bit here, because | |
2640 | * we know that: | |
2641 | * | |
2642 | * 1) it's a BVEC iter, we set it up | |
2643 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2644 | * first and last bvec | |
2645 | * | |
2646 | * So just find our index, and adjust the iterator afterwards. | |
2647 | * If the offset is within the first bvec (or the whole first | |
2648 | * bvec, just use iov_iter_advance(). This makes it easier | |
2649 | * since we can just skip the first segment, which may not | |
2650 | * be PAGE_SIZE aligned. | |
2651 | */ | |
2652 | const struct bio_vec *bvec = imu->bvec; | |
2653 | ||
2654 | if (offset <= bvec->bv_len) { | |
2655 | iov_iter_advance(iter, offset); | |
2656 | } else { | |
2657 | unsigned long seg_skip; | |
2658 | ||
2659 | /* skip first vec */ | |
2660 | offset -= bvec->bv_len; | |
2661 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
2662 | ||
2663 | iter->bvec = bvec + seg_skip; | |
2664 | iter->nr_segs -= seg_skip; | |
99c79f66 | 2665 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 2666 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
2667 | } |
2668 | } | |
2669 | ||
5e559561 | 2670 | return len; |
edafccee JA |
2671 | } |
2672 | ||
bcda7baa JA |
2673 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
2674 | { | |
2675 | if (needs_lock) | |
2676 | mutex_unlock(&ctx->uring_lock); | |
2677 | } | |
2678 | ||
2679 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
2680 | { | |
2681 | /* | |
2682 | * "Normal" inline submissions always hold the uring_lock, since we | |
2683 | * grab it from the system call. Same is true for the SQPOLL offload. | |
2684 | * The only exception is when we've detached the request and issue it | |
2685 | * from an async worker thread, grab the lock for that case. | |
2686 | */ | |
2687 | if (needs_lock) | |
2688 | mutex_lock(&ctx->uring_lock); | |
2689 | } | |
2690 | ||
2691 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
2692 | int bgid, struct io_buffer *kbuf, | |
2693 | bool needs_lock) | |
2694 | { | |
2695 | struct io_buffer *head; | |
2696 | ||
2697 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2698 | return kbuf; | |
2699 | ||
2700 | io_ring_submit_lock(req->ctx, needs_lock); | |
2701 | ||
2702 | lockdep_assert_held(&req->ctx->uring_lock); | |
2703 | ||
2704 | head = idr_find(&req->ctx->io_buffer_idr, bgid); | |
2705 | if (head) { | |
2706 | if (!list_empty(&head->list)) { | |
2707 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
2708 | list); | |
2709 | list_del(&kbuf->list); | |
2710 | } else { | |
2711 | kbuf = head; | |
2712 | idr_remove(&req->ctx->io_buffer_idr, bgid); | |
2713 | } | |
2714 | if (*len > kbuf->len) | |
2715 | *len = kbuf->len; | |
2716 | } else { | |
2717 | kbuf = ERR_PTR(-ENOBUFS); | |
2718 | } | |
2719 | ||
2720 | io_ring_submit_unlock(req->ctx, needs_lock); | |
2721 | ||
2722 | return kbuf; | |
2723 | } | |
2724 | ||
4d954c25 JA |
2725 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
2726 | bool needs_lock) | |
2727 | { | |
2728 | struct io_buffer *kbuf; | |
4f4eeba8 | 2729 | u16 bgid; |
4d954c25 JA |
2730 | |
2731 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 2732 | bgid = req->buf_index; |
4d954c25 JA |
2733 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
2734 | if (IS_ERR(kbuf)) | |
2735 | return kbuf; | |
2736 | req->rw.addr = (u64) (unsigned long) kbuf; | |
2737 | req->flags |= REQ_F_BUFFER_SELECTED; | |
2738 | return u64_to_user_ptr(kbuf->addr); | |
2739 | } | |
2740 | ||
2741 | #ifdef CONFIG_COMPAT | |
2742 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
2743 | bool needs_lock) | |
2744 | { | |
2745 | struct compat_iovec __user *uiov; | |
2746 | compat_ssize_t clen; | |
2747 | void __user *buf; | |
2748 | ssize_t len; | |
2749 | ||
2750 | uiov = u64_to_user_ptr(req->rw.addr); | |
2751 | if (!access_ok(uiov, sizeof(*uiov))) | |
2752 | return -EFAULT; | |
2753 | if (__get_user(clen, &uiov->iov_len)) | |
2754 | return -EFAULT; | |
2755 | if (clen < 0) | |
2756 | return -EINVAL; | |
2757 | ||
2758 | len = clen; | |
2759 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
2760 | if (IS_ERR(buf)) | |
2761 | return PTR_ERR(buf); | |
2762 | iov[0].iov_base = buf; | |
2763 | iov[0].iov_len = (compat_size_t) len; | |
2764 | return 0; | |
2765 | } | |
2766 | #endif | |
2767 | ||
2768 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
2769 | bool needs_lock) | |
2770 | { | |
2771 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
2772 | void __user *buf; | |
2773 | ssize_t len; | |
2774 | ||
2775 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
2776 | return -EFAULT; | |
2777 | ||
2778 | len = iov[0].iov_len; | |
2779 | if (len < 0) | |
2780 | return -EINVAL; | |
2781 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
2782 | if (IS_ERR(buf)) | |
2783 | return PTR_ERR(buf); | |
2784 | iov[0].iov_base = buf; | |
2785 | iov[0].iov_len = len; | |
2786 | return 0; | |
2787 | } | |
2788 | ||
2789 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
2790 | bool needs_lock) | |
2791 | { | |
dddb3e26 JA |
2792 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
2793 | struct io_buffer *kbuf; | |
2794 | ||
2795 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
2796 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
2797 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 2798 | return 0; |
dddb3e26 | 2799 | } |
4d954c25 JA |
2800 | if (!req->rw.len) |
2801 | return 0; | |
2802 | else if (req->rw.len > 1) | |
2803 | return -EINVAL; | |
2804 | ||
2805 | #ifdef CONFIG_COMPAT | |
2806 | if (req->ctx->compat) | |
2807 | return io_compat_import(req, iov, needs_lock); | |
2808 | #endif | |
2809 | ||
2810 | return __io_iov_buffer_select(req, iov, needs_lock); | |
2811 | } | |
2812 | ||
cf6fd4bd | 2813 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
bcda7baa JA |
2814 | struct iovec **iovec, struct iov_iter *iter, |
2815 | bool needs_lock) | |
2b188cc1 | 2816 | { |
9adbd45d JA |
2817 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
2818 | size_t sqe_len = req->rw.len; | |
4d954c25 | 2819 | ssize_t ret; |
edafccee JA |
2820 | u8 opcode; |
2821 | ||
ff6165b2 JA |
2822 | if (req->io) { |
2823 | struct io_async_rw *iorw = &req->io->rw; | |
2824 | ||
2825 | *iovec = NULL; | |
2826 | return iov_iter_count(&iorw->iter); | |
2827 | } | |
2828 | ||
d625c6ee | 2829 | opcode = req->opcode; |
7d009165 | 2830 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 2831 | *iovec = NULL; |
9adbd45d | 2832 | return io_import_fixed(req, rw, iter); |
edafccee | 2833 | } |
2b188cc1 | 2834 | |
bcda7baa | 2835 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 2836 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
2837 | return -EINVAL; |
2838 | ||
3a6820f2 | 2839 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 2840 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 JA |
2841 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
2842 | if (IS_ERR(buf)) { | |
bcda7baa | 2843 | *iovec = NULL; |
4d954c25 | 2844 | return PTR_ERR(buf); |
bcda7baa | 2845 | } |
3f9d6441 | 2846 | req->rw.len = sqe_len; |
bcda7baa JA |
2847 | } |
2848 | ||
3a6820f2 JA |
2849 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
2850 | *iovec = NULL; | |
3a901598 | 2851 | return ret < 0 ? ret : sqe_len; |
3a6820f2 JA |
2852 | } |
2853 | ||
4d954c25 JA |
2854 | if (req->flags & REQ_F_BUFFER_SELECT) { |
2855 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
3f9d6441 JA |
2856 | if (!ret) { |
2857 | ret = (*iovec)->iov_len; | |
2858 | iov_iter_init(iter, rw, *iovec, 1, ret); | |
2859 | } | |
4d954c25 JA |
2860 | *iovec = NULL; |
2861 | return ret; | |
2862 | } | |
2863 | ||
2b188cc1 | 2864 | #ifdef CONFIG_COMPAT |
cf6fd4bd | 2865 | if (req->ctx->compat) |
2b188cc1 JA |
2866 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
2867 | iovec, iter); | |
2868 | #endif | |
2869 | ||
2870 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); | |
2871 | } | |
2872 | ||
31b51510 | 2873 | /* |
32960613 JA |
2874 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
2875 | * by looping over ->read() or ->write() manually. | |
31b51510 | 2876 | */ |
32960613 JA |
2877 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
2878 | struct iov_iter *iter) | |
2879 | { | |
2880 | ssize_t ret = 0; | |
2881 | ||
2882 | /* | |
2883 | * Don't support polled IO through this interface, and we can't | |
2884 | * support non-blocking either. For the latter, this just causes | |
2885 | * the kiocb to be handled from an async context. | |
2886 | */ | |
2887 | if (kiocb->ki_flags & IOCB_HIPRI) | |
2888 | return -EOPNOTSUPP; | |
2889 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
2890 | return -EAGAIN; | |
2891 | ||
2892 | while (iov_iter_count(iter)) { | |
311ae9e1 | 2893 | struct iovec iovec; |
32960613 JA |
2894 | ssize_t nr; |
2895 | ||
311ae9e1 PB |
2896 | if (!iov_iter_is_bvec(iter)) { |
2897 | iovec = iov_iter_iovec(iter); | |
2898 | } else { | |
2899 | /* fixed buffers import bvec */ | |
2900 | iovec.iov_base = kmap(iter->bvec->bv_page) | |
2901 | + iter->iov_offset; | |
2902 | iovec.iov_len = min(iter->count, | |
2903 | iter->bvec->bv_len - iter->iov_offset); | |
2904 | } | |
2905 | ||
32960613 JA |
2906 | if (rw == READ) { |
2907 | nr = file->f_op->read(file, iovec.iov_base, | |
2908 | iovec.iov_len, &kiocb->ki_pos); | |
2909 | } else { | |
2910 | nr = file->f_op->write(file, iovec.iov_base, | |
2911 | iovec.iov_len, &kiocb->ki_pos); | |
2912 | } | |
2913 | ||
311ae9e1 PB |
2914 | if (iov_iter_is_bvec(iter)) |
2915 | kunmap(iter->bvec->bv_page); | |
2916 | ||
32960613 JA |
2917 | if (nr < 0) { |
2918 | if (!ret) | |
2919 | ret = nr; | |
2920 | break; | |
2921 | } | |
2922 | ret += nr; | |
2923 | if (nr != iovec.iov_len) | |
2924 | break; | |
2925 | iov_iter_advance(iter, nr); | |
2926 | } | |
2927 | ||
2928 | return ret; | |
2929 | } | |
2930 | ||
ff6165b2 JA |
2931 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
2932 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 2933 | { |
b64e3444 PB |
2934 | struct io_async_rw *rw = &req->io->rw; |
2935 | ||
ff6165b2 JA |
2936 | memcpy(&rw->iter, iter, sizeof(*iter)); |
2937 | rw->free_iovec = NULL; | |
2938 | /* can only be fixed buffers, no need to do anything */ | |
2939 | if (iter->type == ITER_BVEC) | |
2940 | return; | |
b64e3444 | 2941 | if (!iovec) { |
ff6165b2 JA |
2942 | unsigned iov_off = 0; |
2943 | ||
2944 | rw->iter.iov = rw->fast_iov; | |
2945 | if (iter->iov != fast_iov) { | |
2946 | iov_off = iter->iov - fast_iov; | |
2947 | rw->iter.iov += iov_off; | |
2948 | } | |
2949 | if (rw->fast_iov != fast_iov) | |
2950 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 2951 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 | 2952 | } else { |
ff6165b2 | 2953 | rw->free_iovec = iovec; |
99bc4c38 | 2954 | req->flags |= REQ_F_NEED_CLEANUP; |
f67676d1 JA |
2955 | } |
2956 | } | |
2957 | ||
3d9932a8 XW |
2958 | static inline int __io_alloc_async_ctx(struct io_kiocb *req) |
2959 | { | |
2960 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); | |
2961 | return req->io == NULL; | |
2962 | } | |
2963 | ||
b7bb4f7d | 2964 | static int io_alloc_async_ctx(struct io_kiocb *req) |
f67676d1 | 2965 | { |
d3656344 JA |
2966 | if (!io_op_defs[req->opcode].async_ctx) |
2967 | return 0; | |
3d9932a8 XW |
2968 | |
2969 | return __io_alloc_async_ctx(req); | |
b7bb4f7d JA |
2970 | } |
2971 | ||
ff6165b2 JA |
2972 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
2973 | const struct iovec *fast_iov, | |
b7bb4f7d JA |
2974 | struct iov_iter *iter) |
2975 | { | |
980ad263 | 2976 | if (!io_op_defs[req->opcode].async_ctx) |
74566df3 | 2977 | return 0; |
5d204bcf | 2978 | if (!req->io) { |
3d9932a8 | 2979 | if (__io_alloc_async_ctx(req)) |
5d204bcf | 2980 | return -ENOMEM; |
b7bb4f7d | 2981 | |
ff6165b2 | 2982 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 2983 | } |
b7bb4f7d | 2984 | return 0; |
f67676d1 JA |
2985 | } |
2986 | ||
c3e330a4 PB |
2987 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw, |
2988 | bool force_nonblock) | |
2989 | { | |
ff6165b2 | 2990 | struct io_async_rw *iorw = &req->io->rw; |
c3e330a4 PB |
2991 | ssize_t ret; |
2992 | ||
ff6165b2 JA |
2993 | iorw->iter.iov = iorw->fast_iov; |
2994 | /* reset ->io around the iovec import, we don't want to use it */ | |
c3e330a4 | 2995 | req->io = NULL; |
ff6165b2 JA |
2996 | ret = io_import_iovec(rw, req, (struct iovec **) &iorw->iter.iov, |
2997 | &iorw->iter, !force_nonblock); | |
2998 | req->io = container_of(iorw, struct io_async_ctx, rw); | |
c3e330a4 PB |
2999 | if (unlikely(ret < 0)) |
3000 | return ret; | |
3001 | ||
ff6165b2 | 3002 | io_req_map_rw(req, iorw->iter.iov, iorw->fast_iov, &iorw->iter); |
c3e330a4 PB |
3003 | return 0; |
3004 | } | |
3005 | ||
3529d8c2 JA |
3006 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
3007 | bool force_nonblock) | |
f67676d1 JA |
3008 | { |
3009 | ssize_t ret; | |
3010 | ||
3529d8c2 JA |
3011 | ret = io_prep_rw(req, sqe, force_nonblock); |
3012 | if (ret) | |
3013 | return ret; | |
f67676d1 | 3014 | |
3529d8c2 JA |
3015 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3016 | return -EBADF; | |
f67676d1 | 3017 | |
5f798bea PB |
3018 | /* either don't need iovec imported or already have it */ |
3019 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) | |
3529d8c2 | 3020 | return 0; |
c3e330a4 | 3021 | return io_rw_prep_async(req, READ, force_nonblock); |
f67676d1 JA |
3022 | } |
3023 | ||
c1dd91d1 JA |
3024 | /* |
3025 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3026 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3027 | * This gets called when the page is unlocked, and we generally expect that to | |
3028 | * happen when the page IO is completed and the page is now uptodate. This will | |
3029 | * queue a task_work based retry of the operation, attempting to copy the data | |
3030 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3031 | * do a thread based blocking retry of the operation. That's the unexpected | |
3032 | * slow path. | |
3033 | */ | |
bcf5a063 JA |
3034 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3035 | int sync, void *arg) | |
3036 | { | |
3037 | struct wait_page_queue *wpq; | |
3038 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3039 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3040 | int ret; |
3041 | ||
3042 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3043 | ||
cdc8fcb4 LT |
3044 | if (!wake_page_match(wpq, key)) |
3045 | return 0; | |
3046 | ||
bcf5a063 JA |
3047 | list_del_init(&wait->entry); |
3048 | ||
e7375122 | 3049 | init_task_work(&req->task_work, io_req_task_submit); |
6d816e08 JA |
3050 | percpu_ref_get(&req->ctx->refs); |
3051 | ||
bcf5a063 JA |
3052 | /* submit ref gets dropped, acquire a new one */ |
3053 | refcount_inc(&req->refs); | |
e7375122 | 3054 | ret = io_req_task_work_add(req, &req->task_work); |
bcf5a063 | 3055 | if (unlikely(ret)) { |
c2c4c83c JA |
3056 | struct task_struct *tsk; |
3057 | ||
bcf5a063 | 3058 | /* queue just for cancelation */ |
e7375122 | 3059 | init_task_work(&req->task_work, io_req_task_cancel); |
bcf5a063 | 3060 | tsk = io_wq_get_task(req->ctx->io_wq); |
e7375122 | 3061 | task_work_add(tsk, &req->task_work, 0); |
c2c4c83c | 3062 | wake_up_process(tsk); |
bcf5a063 | 3063 | } |
bcf5a063 JA |
3064 | return 1; |
3065 | } | |
3066 | ||
cdc8fcb4 LT |
3067 | static inline int kiocb_wait_page_queue_init(struct kiocb *kiocb, |
3068 | struct wait_page_queue *wait, | |
3069 | wait_queue_func_t func, | |
3070 | void *data) | |
3071 | { | |
3072 | /* Can't support async wakeup with polled IO */ | |
3073 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3074 | return -EINVAL; | |
3075 | if (kiocb->ki_filp->f_mode & FMODE_BUF_RASYNC) { | |
3076 | wait->wait.func = func; | |
3077 | wait->wait.private = data; | |
3078 | wait->wait.flags = 0; | |
3079 | INIT_LIST_HEAD(&wait->wait.entry); | |
3080 | kiocb->ki_flags |= IOCB_WAITQ; | |
3081 | kiocb->ki_waitq = wait; | |
3082 | return 0; | |
3083 | } | |
3084 | ||
3085 | return -EOPNOTSUPP; | |
f67676d1 JA |
3086 | } |
3087 | ||
c1dd91d1 JA |
3088 | /* |
3089 | * This controls whether a given IO request should be armed for async page | |
3090 | * based retry. If we return false here, the request is handed to the async | |
3091 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3092 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3093 | * will either succeed because the page is now uptodate and unlocked, or it | |
3094 | * will register a callback when the page is unlocked at IO completion. Through | |
3095 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3096 | * That retry will attempt the buffered read again. The retry will generally | |
3097 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3098 | * async worker threads for a blocking retry. | |
3099 | */ | |
ff6165b2 JA |
3100 | static bool io_rw_should_retry(struct io_kiocb *req, struct iovec *iovec, |
3101 | struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3102 | { |
bcf5a063 JA |
3103 | struct kiocb *kiocb = &req->rw.kiocb; |
3104 | int ret; | |
f67676d1 | 3105 | |
bcf5a063 JA |
3106 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3107 | if (req->flags & REQ_F_NOWAIT) | |
3108 | return false; | |
f67676d1 | 3109 | |
bcf5a063 JA |
3110 | /* already tried, or we're doing O_DIRECT */ |
3111 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_WAITQ)) | |
3112 | return false; | |
3113 | /* | |
3114 | * just use poll if we can, and don't attempt if the fs doesn't | |
3115 | * support callback based unlocks | |
3116 | */ | |
3117 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3118 | return false; | |
f67676d1 | 3119 | |
bcf5a063 JA |
3120 | /* |
3121 | * If request type doesn't require req->io to defer in general, | |
3122 | * we need to allocate it here | |
3123 | */ | |
ff6165b2 JA |
3124 | if (!req->io) { |
3125 | if (__io_alloc_async_ctx(req)) | |
3126 | return false; | |
3127 | io_req_map_rw(req, iovec, fast_iov, iter); | |
3128 | } | |
3529d8c2 | 3129 | |
bcf5a063 JA |
3130 | ret = kiocb_wait_page_queue_init(kiocb, &req->io->rw.wpq, |
3131 | io_async_buf_func, req); | |
3132 | if (!ret) { | |
3133 | io_get_req_task(req); | |
3134 | return true; | |
3135 | } | |
3529d8c2 | 3136 | |
bcf5a063 JA |
3137 | return false; |
3138 | } | |
3139 | ||
3140 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) | |
3141 | { | |
3142 | if (req->file->f_op->read_iter) | |
3143 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d GH |
3144 | else if (req->file->f_op->read) |
3145 | return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter); | |
3146 | else | |
3147 | return -EINVAL; | |
f67676d1 JA |
3148 | } |
3149 | ||
a1d7c393 JA |
3150 | static int io_read(struct io_kiocb *req, bool force_nonblock, |
3151 | struct io_comp_state *cs) | |
2b188cc1 JA |
3152 | { |
3153 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3154 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3155 | struct iov_iter __iter, *iter = &__iter; |
31b51510 | 3156 | size_t iov_count; |
ff6165b2 JA |
3157 | ssize_t io_size, ret, ret2 = 0; |
3158 | ||
3159 | if (req->io) | |
3160 | iter = &req->io->rw.iter; | |
2b188cc1 | 3161 | |
ff6165b2 | 3162 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); |
06b76d44 JA |
3163 | if (ret < 0) |
3164 | return ret; | |
fa15bafb PB |
3165 | io_size = ret; |
3166 | req->result = io_size; | |
2b188cc1 | 3167 | |
fd6c2e4c JA |
3168 | /* Ensure we clear previously set non-block flag */ |
3169 | if (!force_nonblock) | |
29de5f6a | 3170 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
fd6c2e4c | 3171 | |
24c74678 | 3172 | /* If the file doesn't support async, just async punt */ |
af197f50 | 3173 | if (force_nonblock && !io_file_supports_async(req->file, READ)) |
f67676d1 | 3174 | goto copy_iov; |
9e645e11 | 3175 | |
ff6165b2 | 3176 | iov_count = iov_iter_count(iter); |
9adbd45d | 3177 | ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count); |
fa15bafb PB |
3178 | if (unlikely(ret)) |
3179 | goto out_free; | |
2b188cc1 | 3180 | |
ff6165b2 | 3181 | ret2 = io_iter_do_read(req, iter); |
32960613 | 3182 | |
fa15bafb PB |
3183 | /* Catch -EAGAIN return for forced non-blocking submission */ |
3184 | if (!force_nonblock || (ret2 != -EAGAIN && ret2 != -EIO)) { | |
3185 | kiocb_done(kiocb, ret2, cs); | |
3186 | } else { | |
f67676d1 | 3187 | copy_iov: |
ff6165b2 | 3188 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter); |
fa15bafb PB |
3189 | if (ret) |
3190 | goto out_free; | |
3191 | /* it's copied and will be cleaned with ->io */ | |
3192 | iovec = NULL; | |
3193 | /* if we can retry, do so with the callbacks armed */ | |
ff6165b2 JA |
3194 | if (io_rw_should_retry(req, iovec, inline_vecs, iter)) { |
3195 | ret2 = io_iter_do_read(req, iter); | |
fa15bafb | 3196 | if (ret2 == -EIOCBQUEUED) { |
f67676d1 | 3197 | goto out_free; |
fa15bafb PB |
3198 | } else if (ret2 != -EAGAIN) { |
3199 | kiocb_done(kiocb, ret2, cs); | |
f67676d1 | 3200 | goto out_free; |
bcf5a063 | 3201 | } |
f67676d1 | 3202 | } |
fa15bafb PB |
3203 | kiocb->ki_flags &= ~IOCB_WAITQ; |
3204 | return -EAGAIN; | |
2b188cc1 | 3205 | } |
f67676d1 | 3206 | out_free: |
252917c3 | 3207 | if (iovec) |
6f2cc166 | 3208 | kfree(iovec); |
2b188cc1 JA |
3209 | return ret; |
3210 | } | |
3211 | ||
3529d8c2 JA |
3212 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
3213 | bool force_nonblock) | |
f67676d1 JA |
3214 | { |
3215 | ssize_t ret; | |
3216 | ||
3529d8c2 JA |
3217 | ret = io_prep_rw(req, sqe, force_nonblock); |
3218 | if (ret) | |
3219 | return ret; | |
f67676d1 | 3220 | |
3529d8c2 JA |
3221 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3222 | return -EBADF; | |
f67676d1 | 3223 | |
5f798bea PB |
3224 | /* either don't need iovec imported or already have it */ |
3225 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) | |
3529d8c2 | 3226 | return 0; |
c3e330a4 | 3227 | return io_rw_prep_async(req, WRITE, force_nonblock); |
f67676d1 JA |
3228 | } |
3229 | ||
a1d7c393 JA |
3230 | static int io_write(struct io_kiocb *req, bool force_nonblock, |
3231 | struct io_comp_state *cs) | |
2b188cc1 JA |
3232 | { |
3233 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3234 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3235 | struct iov_iter __iter, *iter = &__iter; |
31b51510 | 3236 | size_t iov_count; |
fa15bafb | 3237 | ssize_t ret, ret2, io_size; |
2b188cc1 | 3238 | |
ff6165b2 JA |
3239 | if (req->io) |
3240 | iter = &req->io->rw.iter; | |
3241 | ||
3242 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
06b76d44 JA |
3243 | if (ret < 0) |
3244 | return ret; | |
fa15bafb PB |
3245 | io_size = ret; |
3246 | req->result = io_size; | |
2b188cc1 | 3247 | |
fd6c2e4c JA |
3248 | /* Ensure we clear previously set non-block flag */ |
3249 | if (!force_nonblock) | |
9adbd45d | 3250 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
fd6c2e4c | 3251 | |
24c74678 | 3252 | /* If the file doesn't support async, just async punt */ |
af197f50 | 3253 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
f67676d1 | 3254 | goto copy_iov; |
31b51510 | 3255 | |
10d59345 JA |
3256 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3257 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3258 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3259 | goto copy_iov; |
31b51510 | 3260 | |
ff6165b2 | 3261 | iov_count = iov_iter_count(iter); |
9adbd45d | 3262 | ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count); |
fa15bafb PB |
3263 | if (unlikely(ret)) |
3264 | goto out_free; | |
4ed734b0 | 3265 | |
fa15bafb PB |
3266 | /* |
3267 | * Open-code file_start_write here to grab freeze protection, | |
3268 | * which will be released by another thread in | |
3269 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3270 | * released so that it doesn't complain about the held lock when | |
3271 | * we return to userspace. | |
3272 | */ | |
3273 | if (req->flags & REQ_F_ISREG) { | |
3274 | __sb_start_write(file_inode(req->file)->i_sb, | |
3275 | SB_FREEZE_WRITE, true); | |
3276 | __sb_writers_release(file_inode(req->file)->i_sb, | |
3277 | SB_FREEZE_WRITE); | |
3278 | } | |
3279 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3280 | |
fa15bafb | 3281 | if (req->file->f_op->write_iter) |
ff6165b2 | 3282 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3283 | else if (req->file->f_op->write) |
ff6165b2 | 3284 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter); |
2dd2111d GH |
3285 | else |
3286 | ret2 = -EINVAL; | |
4ed734b0 | 3287 | |
fa15bafb PB |
3288 | /* |
3289 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3290 | * retry them without IOCB_NOWAIT. | |
3291 | */ | |
3292 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3293 | ret2 = -EAGAIN; | |
3294 | if (!force_nonblock || ret2 != -EAGAIN) { | |
3295 | kiocb_done(kiocb, ret2, cs); | |
3296 | } else { | |
f67676d1 | 3297 | copy_iov: |
ff6165b2 JA |
3298 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter); |
3299 | if (!ret) | |
3300 | return -EAGAIN; | |
2b188cc1 | 3301 | } |
31b51510 | 3302 | out_free: |
252917c3 | 3303 | if (iovec) |
6f2cc166 | 3304 | kfree(iovec); |
2b188cc1 JA |
3305 | return ret; |
3306 | } | |
3307 | ||
f2a8d5c7 PB |
3308 | static int __io_splice_prep(struct io_kiocb *req, |
3309 | const struct io_uring_sqe *sqe) | |
7d67af2c PB |
3310 | { |
3311 | struct io_splice* sp = &req->splice; | |
3312 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; | |
3313 | int ret; | |
3314 | ||
3315 | if (req->flags & REQ_F_NEED_CLEANUP) | |
3316 | return 0; | |
3232dd02 PB |
3317 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3318 | return -EINVAL; | |
7d67af2c PB |
3319 | |
3320 | sp->file_in = NULL; | |
7d67af2c PB |
3321 | sp->len = READ_ONCE(sqe->len); |
3322 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3323 | ||
3324 | if (unlikely(sp->flags & ~valid_flags)) | |
3325 | return -EINVAL; | |
3326 | ||
3327 | ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in, | |
3328 | (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3329 | if (ret) | |
3330 | return ret; | |
3331 | req->flags |= REQ_F_NEED_CLEANUP; | |
3332 | ||
7cdaf587 XW |
3333 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
3334 | /* | |
3335 | * Splice operation will be punted aync, and here need to | |
3336 | * modify io_wq_work.flags, so initialize io_wq_work firstly. | |
3337 | */ | |
3338 | io_req_init_async(req); | |
7d67af2c | 3339 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
7cdaf587 | 3340 | } |
7d67af2c PB |
3341 | |
3342 | return 0; | |
3343 | } | |
3344 | ||
f2a8d5c7 PB |
3345 | static int io_tee_prep(struct io_kiocb *req, |
3346 | const struct io_uring_sqe *sqe) | |
3347 | { | |
3348 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3349 | return -EINVAL; | |
3350 | return __io_splice_prep(req, sqe); | |
3351 | } | |
3352 | ||
3353 | static int io_tee(struct io_kiocb *req, bool force_nonblock) | |
3354 | { | |
3355 | struct io_splice *sp = &req->splice; | |
3356 | struct file *in = sp->file_in; | |
3357 | struct file *out = sp->file_out; | |
3358 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3359 | long ret = 0; | |
3360 | ||
3361 | if (force_nonblock) | |
3362 | return -EAGAIN; | |
3363 | if (sp->len) | |
3364 | ret = do_tee(in, out, sp->len, flags); | |
3365 | ||
3366 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3367 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3368 | ||
f2a8d5c7 PB |
3369 | if (ret != sp->len) |
3370 | req_set_fail_links(req); | |
e1e16097 | 3371 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3372 | return 0; |
3373 | } | |
3374 | ||
3375 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3376 | { | |
3377 | struct io_splice* sp = &req->splice; | |
3378 | ||
3379 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3380 | sp->off_out = READ_ONCE(sqe->off); | |
3381 | return __io_splice_prep(req, sqe); | |
3382 | } | |
3383 | ||
014db007 | 3384 | static int io_splice(struct io_kiocb *req, bool force_nonblock) |
7d67af2c PB |
3385 | { |
3386 | struct io_splice *sp = &req->splice; | |
3387 | struct file *in = sp->file_in; | |
3388 | struct file *out = sp->file_out; | |
3389 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3390 | loff_t *poff_in, *poff_out; | |
c9687426 | 3391 | long ret = 0; |
7d67af2c | 3392 | |
2fb3e822 PB |
3393 | if (force_nonblock) |
3394 | return -EAGAIN; | |
7d67af2c PB |
3395 | |
3396 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3397 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3398 | |
948a7749 | 3399 | if (sp->len) |
c9687426 | 3400 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c PB |
3401 | |
3402 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3403 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3404 | ||
7d67af2c PB |
3405 | if (ret != sp->len) |
3406 | req_set_fail_links(req); | |
e1e16097 | 3407 | io_req_complete(req, ret); |
7d67af2c PB |
3408 | return 0; |
3409 | } | |
3410 | ||
2b188cc1 JA |
3411 | /* |
3412 | * IORING_OP_NOP just posts a completion event, nothing else. | |
3413 | */ | |
229a7b63 | 3414 | static int io_nop(struct io_kiocb *req, struct io_comp_state *cs) |
2b188cc1 JA |
3415 | { |
3416 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 3417 | |
def596e9 JA |
3418 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
3419 | return -EINVAL; | |
3420 | ||
229a7b63 | 3421 | __io_req_complete(req, 0, 0, cs); |
2b188cc1 JA |
3422 | return 0; |
3423 | } | |
3424 | ||
3529d8c2 | 3425 | static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 3426 | { |
6b06314c | 3427 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 3428 | |
09bb8394 JA |
3429 | if (!req->file) |
3430 | return -EBADF; | |
c992fe29 | 3431 | |
6b06314c | 3432 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 3433 | return -EINVAL; |
edafccee | 3434 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
3435 | return -EINVAL; |
3436 | ||
8ed8d3c3 JA |
3437 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
3438 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
3439 | return -EINVAL; | |
3440 | ||
3441 | req->sync.off = READ_ONCE(sqe->off); | |
3442 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
3443 | return 0; |
3444 | } | |
3445 | ||
ac45abc0 | 3446 | static int io_fsync(struct io_kiocb *req, bool force_nonblock) |
8ed8d3c3 | 3447 | { |
8ed8d3c3 | 3448 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
3449 | int ret; |
3450 | ||
ac45abc0 PB |
3451 | /* fsync always requires a blocking context */ |
3452 | if (force_nonblock) | |
3453 | return -EAGAIN; | |
3454 | ||
9adbd45d | 3455 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
3456 | end > 0 ? end : LLONG_MAX, |
3457 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
3458 | if (ret < 0) | |
3459 | req_set_fail_links(req); | |
e1e16097 | 3460 | io_req_complete(req, ret); |
c992fe29 CH |
3461 | return 0; |
3462 | } | |
3463 | ||
d63d1b5e JA |
3464 | static int io_fallocate_prep(struct io_kiocb *req, |
3465 | const struct io_uring_sqe *sqe) | |
3466 | { | |
3467 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
3468 | return -EINVAL; | |
3232dd02 PB |
3469 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3470 | return -EINVAL; | |
d63d1b5e JA |
3471 | |
3472 | req->sync.off = READ_ONCE(sqe->off); | |
3473 | req->sync.len = READ_ONCE(sqe->addr); | |
3474 | req->sync.mode = READ_ONCE(sqe->len); | |
3475 | return 0; | |
3476 | } | |
3477 | ||
014db007 | 3478 | static int io_fallocate(struct io_kiocb *req, bool force_nonblock) |
5d17b4a4 | 3479 | { |
ac45abc0 PB |
3480 | int ret; |
3481 | ||
d63d1b5e | 3482 | /* fallocate always requiring blocking context */ |
ac45abc0 | 3483 | if (force_nonblock) |
5d17b4a4 | 3484 | return -EAGAIN; |
ac45abc0 PB |
3485 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
3486 | req->sync.len); | |
ac45abc0 PB |
3487 | if (ret < 0) |
3488 | req_set_fail_links(req); | |
e1e16097 | 3489 | io_req_complete(req, ret); |
5d17b4a4 JA |
3490 | return 0; |
3491 | } | |
3492 | ||
ec65fea5 | 3493 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 3494 | { |
f8748881 | 3495 | const char __user *fname; |
15b71abe | 3496 | int ret; |
b7bb4f7d | 3497 | |
3232dd02 | 3498 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
15b71abe | 3499 | return -EINVAL; |
ec65fea5 | 3500 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 3501 | return -EINVAL; |
ec65fea5 | 3502 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 3503 | return -EBADF; |
03b1230c | 3504 | |
ec65fea5 PB |
3505 | /* open.how should be already initialised */ |
3506 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 3507 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 3508 | |
25e72d10 PB |
3509 | req->open.dfd = READ_ONCE(sqe->fd); |
3510 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 3511 | req->open.filename = getname(fname); |
15b71abe JA |
3512 | if (IS_ERR(req->open.filename)) { |
3513 | ret = PTR_ERR(req->open.filename); | |
3514 | req->open.filename = NULL; | |
3515 | return ret; | |
3516 | } | |
4022e7af | 3517 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 3518 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 3519 | return 0; |
03b1230c JA |
3520 | } |
3521 | ||
ec65fea5 PB |
3522 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
3523 | { | |
3524 | u64 flags, mode; | |
3525 | ||
3526 | if (req->flags & REQ_F_NEED_CLEANUP) | |
3527 | return 0; | |
3528 | mode = READ_ONCE(sqe->len); | |
3529 | flags = READ_ONCE(sqe->open_flags); | |
3530 | req->open.how = build_open_how(flags, mode); | |
3531 | return __io_openat_prep(req, sqe); | |
3532 | } | |
3533 | ||
cebdb986 | 3534 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 3535 | { |
cebdb986 | 3536 | struct open_how __user *how; |
cebdb986 | 3537 | size_t len; |
0fa03c62 JA |
3538 | int ret; |
3539 | ||
0bdbdd08 PB |
3540 | if (req->flags & REQ_F_NEED_CLEANUP) |
3541 | return 0; | |
cebdb986 JA |
3542 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
3543 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
3544 | if (len < OPEN_HOW_SIZE_VER0) |
3545 | return -EINVAL; | |
3529d8c2 | 3546 | |
cebdb986 JA |
3547 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
3548 | len); | |
3549 | if (ret) | |
3550 | return ret; | |
3529d8c2 | 3551 | |
ec65fea5 | 3552 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
3553 | } |
3554 | ||
014db007 | 3555 | static int io_openat2(struct io_kiocb *req, bool force_nonblock) |
15b71abe JA |
3556 | { |
3557 | struct open_flags op; | |
15b71abe JA |
3558 | struct file *file; |
3559 | int ret; | |
3560 | ||
f86cd20c | 3561 | if (force_nonblock) |
15b71abe | 3562 | return -EAGAIN; |
15b71abe | 3563 | |
cebdb986 | 3564 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
3565 | if (ret) |
3566 | goto err; | |
3567 | ||
4022e7af | 3568 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
15b71abe JA |
3569 | if (ret < 0) |
3570 | goto err; | |
3571 | ||
3572 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
3573 | if (IS_ERR(file)) { | |
3574 | put_unused_fd(ret); | |
3575 | ret = PTR_ERR(file); | |
3576 | } else { | |
3577 | fsnotify_open(file); | |
3578 | fd_install(ret, file); | |
3579 | } | |
3580 | err: | |
3581 | putname(req->open.filename); | |
8fef80bf | 3582 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe JA |
3583 | if (ret < 0) |
3584 | req_set_fail_links(req); | |
e1e16097 | 3585 | io_req_complete(req, ret); |
15b71abe JA |
3586 | return 0; |
3587 | } | |
3588 | ||
014db007 | 3589 | static int io_openat(struct io_kiocb *req, bool force_nonblock) |
cebdb986 | 3590 | { |
014db007 | 3591 | return io_openat2(req, force_nonblock); |
cebdb986 JA |
3592 | } |
3593 | ||
067524e9 JA |
3594 | static int io_remove_buffers_prep(struct io_kiocb *req, |
3595 | const struct io_uring_sqe *sqe) | |
3596 | { | |
3597 | struct io_provide_buf *p = &req->pbuf; | |
3598 | u64 tmp; | |
3599 | ||
3600 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) | |
3601 | return -EINVAL; | |
3602 | ||
3603 | tmp = READ_ONCE(sqe->fd); | |
3604 | if (!tmp || tmp > USHRT_MAX) | |
3605 | return -EINVAL; | |
3606 | ||
3607 | memset(p, 0, sizeof(*p)); | |
3608 | p->nbufs = tmp; | |
3609 | p->bgid = READ_ONCE(sqe->buf_group); | |
3610 | return 0; | |
3611 | } | |
3612 | ||
3613 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
3614 | int bgid, unsigned nbufs) | |
3615 | { | |
3616 | unsigned i = 0; | |
3617 | ||
3618 | /* shouldn't happen */ | |
3619 | if (!nbufs) | |
3620 | return 0; | |
3621 | ||
3622 | /* the head kbuf is the list itself */ | |
3623 | while (!list_empty(&buf->list)) { | |
3624 | struct io_buffer *nxt; | |
3625 | ||
3626 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
3627 | list_del(&nxt->list); | |
3628 | kfree(nxt); | |
3629 | if (++i == nbufs) | |
3630 | return i; | |
3631 | } | |
3632 | i++; | |
3633 | kfree(buf); | |
3634 | idr_remove(&ctx->io_buffer_idr, bgid); | |
3635 | ||
3636 | return i; | |
3637 | } | |
3638 | ||
229a7b63 JA |
3639 | static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock, |
3640 | struct io_comp_state *cs) | |
067524e9 JA |
3641 | { |
3642 | struct io_provide_buf *p = &req->pbuf; | |
3643 | struct io_ring_ctx *ctx = req->ctx; | |
3644 | struct io_buffer *head; | |
3645 | int ret = 0; | |
3646 | ||
3647 | io_ring_submit_lock(ctx, !force_nonblock); | |
3648 | ||
3649 | lockdep_assert_held(&ctx->uring_lock); | |
3650 | ||
3651 | ret = -ENOENT; | |
3652 | head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
3653 | if (head) | |
3654 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
3655 | ||
3656 | io_ring_submit_lock(ctx, !force_nonblock); | |
3657 | if (ret < 0) | |
3658 | req_set_fail_links(req); | |
229a7b63 | 3659 | __io_req_complete(req, ret, 0, cs); |
067524e9 JA |
3660 | return 0; |
3661 | } | |
3662 | ||
ddf0322d JA |
3663 | static int io_provide_buffers_prep(struct io_kiocb *req, |
3664 | const struct io_uring_sqe *sqe) | |
3665 | { | |
3666 | struct io_provide_buf *p = &req->pbuf; | |
3667 | u64 tmp; | |
3668 | ||
3669 | if (sqe->ioprio || sqe->rw_flags) | |
3670 | return -EINVAL; | |
3671 | ||
3672 | tmp = READ_ONCE(sqe->fd); | |
3673 | if (!tmp || tmp > USHRT_MAX) | |
3674 | return -E2BIG; | |
3675 | p->nbufs = tmp; | |
3676 | p->addr = READ_ONCE(sqe->addr); | |
3677 | p->len = READ_ONCE(sqe->len); | |
3678 | ||
efe68c1c | 3679 | if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs))) |
ddf0322d JA |
3680 | return -EFAULT; |
3681 | ||
3682 | p->bgid = READ_ONCE(sqe->buf_group); | |
3683 | tmp = READ_ONCE(sqe->off); | |
3684 | if (tmp > USHRT_MAX) | |
3685 | return -E2BIG; | |
3686 | p->bid = tmp; | |
3687 | return 0; | |
3688 | } | |
3689 | ||
3690 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
3691 | { | |
3692 | struct io_buffer *buf; | |
3693 | u64 addr = pbuf->addr; | |
3694 | int i, bid = pbuf->bid; | |
3695 | ||
3696 | for (i = 0; i < pbuf->nbufs; i++) { | |
3697 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
3698 | if (!buf) | |
3699 | break; | |
3700 | ||
3701 | buf->addr = addr; | |
3702 | buf->len = pbuf->len; | |
3703 | buf->bid = bid; | |
3704 | addr += pbuf->len; | |
3705 | bid++; | |
3706 | if (!*head) { | |
3707 | INIT_LIST_HEAD(&buf->list); | |
3708 | *head = buf; | |
3709 | } else { | |
3710 | list_add_tail(&buf->list, &(*head)->list); | |
3711 | } | |
3712 | } | |
3713 | ||
3714 | return i ? i : -ENOMEM; | |
3715 | } | |
3716 | ||
229a7b63 JA |
3717 | static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock, |
3718 | struct io_comp_state *cs) | |
ddf0322d JA |
3719 | { |
3720 | struct io_provide_buf *p = &req->pbuf; | |
3721 | struct io_ring_ctx *ctx = req->ctx; | |
3722 | struct io_buffer *head, *list; | |
3723 | int ret = 0; | |
3724 | ||
3725 | io_ring_submit_lock(ctx, !force_nonblock); | |
3726 | ||
3727 | lockdep_assert_held(&ctx->uring_lock); | |
3728 | ||
3729 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
3730 | ||
3731 | ret = io_add_buffers(p, &head); | |
3732 | if (ret < 0) | |
3733 | goto out; | |
3734 | ||
3735 | if (!list) { | |
3736 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, | |
3737 | GFP_KERNEL); | |
3738 | if (ret < 0) { | |
067524e9 | 3739 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d JA |
3740 | goto out; |
3741 | } | |
3742 | } | |
3743 | out: | |
3744 | io_ring_submit_unlock(ctx, !force_nonblock); | |
3745 | if (ret < 0) | |
3746 | req_set_fail_links(req); | |
229a7b63 | 3747 | __io_req_complete(req, ret, 0, cs); |
ddf0322d | 3748 | return 0; |
cebdb986 JA |
3749 | } |
3750 | ||
3e4827b0 JA |
3751 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
3752 | const struct io_uring_sqe *sqe) | |
3753 | { | |
3754 | #if defined(CONFIG_EPOLL) | |
3755 | if (sqe->ioprio || sqe->buf_index) | |
3756 | return -EINVAL; | |
3232dd02 PB |
3757 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3758 | return -EINVAL; | |
3e4827b0 JA |
3759 | |
3760 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
3761 | req->epoll.op = READ_ONCE(sqe->len); | |
3762 | req->epoll.fd = READ_ONCE(sqe->off); | |
3763 | ||
3764 | if (ep_op_has_event(req->epoll.op)) { | |
3765 | struct epoll_event __user *ev; | |
3766 | ||
3767 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3768 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
3769 | return -EFAULT; | |
3770 | } | |
3771 | ||
3772 | return 0; | |
3773 | #else | |
3774 | return -EOPNOTSUPP; | |
3775 | #endif | |
3776 | } | |
3777 | ||
229a7b63 JA |
3778 | static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock, |
3779 | struct io_comp_state *cs) | |
3e4827b0 JA |
3780 | { |
3781 | #if defined(CONFIG_EPOLL) | |
3782 | struct io_epoll *ie = &req->epoll; | |
3783 | int ret; | |
3784 | ||
3785 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
3786 | if (force_nonblock && ret == -EAGAIN) | |
3787 | return -EAGAIN; | |
3788 | ||
3789 | if (ret < 0) | |
3790 | req_set_fail_links(req); | |
229a7b63 | 3791 | __io_req_complete(req, ret, 0, cs); |
3e4827b0 JA |
3792 | return 0; |
3793 | #else | |
3794 | return -EOPNOTSUPP; | |
3795 | #endif | |
3796 | } | |
3797 | ||
c1ca757b JA |
3798 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
3799 | { | |
3800 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
3801 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
3802 | return -EINVAL; | |
3232dd02 PB |
3803 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3804 | return -EINVAL; | |
c1ca757b JA |
3805 | |
3806 | req->madvise.addr = READ_ONCE(sqe->addr); | |
3807 | req->madvise.len = READ_ONCE(sqe->len); | |
3808 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
3809 | return 0; | |
3810 | #else | |
3811 | return -EOPNOTSUPP; | |
3812 | #endif | |
3813 | } | |
3814 | ||
014db007 | 3815 | static int io_madvise(struct io_kiocb *req, bool force_nonblock) |
c1ca757b JA |
3816 | { |
3817 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
3818 | struct io_madvise *ma = &req->madvise; | |
3819 | int ret; | |
3820 | ||
3821 | if (force_nonblock) | |
3822 | return -EAGAIN; | |
3823 | ||
3824 | ret = do_madvise(ma->addr, ma->len, ma->advice); | |
3825 | if (ret < 0) | |
3826 | req_set_fail_links(req); | |
e1e16097 | 3827 | io_req_complete(req, ret); |
c1ca757b JA |
3828 | return 0; |
3829 | #else | |
3830 | return -EOPNOTSUPP; | |
3831 | #endif | |
3832 | } | |
3833 | ||
4840e418 JA |
3834 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
3835 | { | |
3836 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
3837 | return -EINVAL; | |
3232dd02 PB |
3838 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3839 | return -EINVAL; | |
4840e418 JA |
3840 | |
3841 | req->fadvise.offset = READ_ONCE(sqe->off); | |
3842 | req->fadvise.len = READ_ONCE(sqe->len); | |
3843 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
3844 | return 0; | |
3845 | } | |
3846 | ||
014db007 | 3847 | static int io_fadvise(struct io_kiocb *req, bool force_nonblock) |
4840e418 JA |
3848 | { |
3849 | struct io_fadvise *fa = &req->fadvise; | |
3850 | int ret; | |
3851 | ||
3e69426d JA |
3852 | if (force_nonblock) { |
3853 | switch (fa->advice) { | |
3854 | case POSIX_FADV_NORMAL: | |
3855 | case POSIX_FADV_RANDOM: | |
3856 | case POSIX_FADV_SEQUENTIAL: | |
3857 | break; | |
3858 | default: | |
3859 | return -EAGAIN; | |
3860 | } | |
3861 | } | |
4840e418 JA |
3862 | |
3863 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
3864 | if (ret < 0) | |
3865 | req_set_fail_links(req); | |
e1e16097 | 3866 | io_req_complete(req, ret); |
4840e418 JA |
3867 | return 0; |
3868 | } | |
3869 | ||
eddc7ef5 JA |
3870 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
3871 | { | |
3232dd02 PB |
3872 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3873 | return -EINVAL; | |
eddc7ef5 JA |
3874 | if (sqe->ioprio || sqe->buf_index) |
3875 | return -EINVAL; | |
9c280f90 | 3876 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 3877 | return -EBADF; |
eddc7ef5 | 3878 | |
1d9e1288 BM |
3879 | req->statx.dfd = READ_ONCE(sqe->fd); |
3880 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 3881 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
3882 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
3883 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
3884 | |
3885 | return 0; | |
3886 | } | |
3887 | ||
014db007 | 3888 | static int io_statx(struct io_kiocb *req, bool force_nonblock) |
eddc7ef5 | 3889 | { |
1d9e1288 | 3890 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
3891 | int ret; |
3892 | ||
5b0bbee4 JA |
3893 | if (force_nonblock) { |
3894 | /* only need file table for an actual valid fd */ | |
3895 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) | |
3896 | req->flags |= REQ_F_NO_FILE_TABLE; | |
eddc7ef5 | 3897 | return -EAGAIN; |
5b0bbee4 | 3898 | } |
eddc7ef5 | 3899 | |
e62753e4 BM |
3900 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
3901 | ctx->buffer); | |
eddc7ef5 | 3902 | |
eddc7ef5 JA |
3903 | if (ret < 0) |
3904 | req_set_fail_links(req); | |
e1e16097 | 3905 | io_req_complete(req, ret); |
eddc7ef5 JA |
3906 | return 0; |
3907 | } | |
3908 | ||
b5dba59e JA |
3909 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
3910 | { | |
3911 | /* | |
3912 | * If we queue this for async, it must not be cancellable. That would | |
7cdaf587 XW |
3913 | * leave the 'file' in an undeterminate state, and here need to modify |
3914 | * io_wq_work.flags, so initialize io_wq_work firstly. | |
b5dba59e | 3915 | */ |
7cdaf587 | 3916 | io_req_init_async(req); |
b5dba59e JA |
3917 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
3918 | ||
3232dd02 PB |
3919 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
3920 | return -EINVAL; | |
b5dba59e JA |
3921 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
3922 | sqe->rw_flags || sqe->buf_index) | |
3923 | return -EINVAL; | |
9c280f90 | 3924 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 3925 | return -EBADF; |
b5dba59e JA |
3926 | |
3927 | req->close.fd = READ_ONCE(sqe->fd); | |
fd2206e4 JA |
3928 | if ((req->file && req->file->f_op == &io_uring_fops) || |
3929 | req->close.fd == req->ctx->ring_fd) | |
3930 | return -EBADF; | |
b5dba59e | 3931 | |
3af73b28 | 3932 | req->close.put_file = NULL; |
b5dba59e | 3933 | return 0; |
b5dba59e JA |
3934 | } |
3935 | ||
229a7b63 JA |
3936 | static int io_close(struct io_kiocb *req, bool force_nonblock, |
3937 | struct io_comp_state *cs) | |
b5dba59e | 3938 | { |
3af73b28 | 3939 | struct io_close *close = &req->close; |
b5dba59e JA |
3940 | int ret; |
3941 | ||
3af73b28 PB |
3942 | /* might be already done during nonblock submission */ |
3943 | if (!close->put_file) { | |
3944 | ret = __close_fd_get_file(close->fd, &close->put_file); | |
3945 | if (ret < 0) | |
3946 | return (ret == -ENOENT) ? -EBADF : ret; | |
3947 | } | |
b5dba59e JA |
3948 | |
3949 | /* if the file has a flush method, be safe and punt to async */ | |
3af73b28 | 3950 | if (close->put_file->f_op->flush && force_nonblock) { |
24c74678 PB |
3951 | /* was never set, but play safe */ |
3952 | req->flags &= ~REQ_F_NOWAIT; | |
0bf0eefd | 3953 | /* avoid grabbing files - we don't need the files */ |
24c74678 | 3954 | req->flags |= REQ_F_NO_FILE_TABLE; |
0bf0eefd | 3955 | return -EAGAIN; |
a2100672 | 3956 | } |
b5dba59e | 3957 | |
3af73b28 PB |
3958 | /* No ->flush() or already async, safely close from here */ |
3959 | ret = filp_close(close->put_file, req->work.files); | |
3960 | if (ret < 0) | |
3961 | req_set_fail_links(req); | |
3af73b28 PB |
3962 | fput(close->put_file); |
3963 | close->put_file = NULL; | |
229a7b63 | 3964 | __io_req_complete(req, ret, 0, cs); |
1a417f4e | 3965 | return 0; |
b5dba59e JA |
3966 | } |
3967 | ||
3529d8c2 | 3968 | static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
3969 | { |
3970 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 JA |
3971 | |
3972 | if (!req->file) | |
3973 | return -EBADF; | |
5d17b4a4 JA |
3974 | |
3975 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) | |
3976 | return -EINVAL; | |
3977 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
3978 | return -EINVAL; | |
3979 | ||
8ed8d3c3 JA |
3980 | req->sync.off = READ_ONCE(sqe->off); |
3981 | req->sync.len = READ_ONCE(sqe->len); | |
3982 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
3983 | return 0; |
3984 | } | |
3985 | ||
ac45abc0 | 3986 | static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock) |
8ed8d3c3 | 3987 | { |
8ed8d3c3 JA |
3988 | int ret; |
3989 | ||
ac45abc0 PB |
3990 | /* sync_file_range always requires a blocking context */ |
3991 | if (force_nonblock) | |
3992 | return -EAGAIN; | |
3993 | ||
9adbd45d | 3994 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
3995 | req->sync.flags); |
3996 | if (ret < 0) | |
3997 | req_set_fail_links(req); | |
e1e16097 | 3998 | io_req_complete(req, ret); |
5d17b4a4 JA |
3999 | return 0; |
4000 | } | |
4001 | ||
469956e8 | 4002 | #if defined(CONFIG_NET) |
02d27d89 PB |
4003 | static int io_setup_async_msg(struct io_kiocb *req, |
4004 | struct io_async_msghdr *kmsg) | |
4005 | { | |
4006 | if (req->io) | |
4007 | return -EAGAIN; | |
4008 | if (io_alloc_async_ctx(req)) { | |
4009 | if (kmsg->iov != kmsg->fast_iov) | |
4010 | kfree(kmsg->iov); | |
4011 | return -ENOMEM; | |
4012 | } | |
4013 | req->flags |= REQ_F_NEED_CLEANUP; | |
4014 | memcpy(&req->io->msg, kmsg, sizeof(*kmsg)); | |
4015 | return -EAGAIN; | |
4016 | } | |
4017 | ||
2ae523ed PB |
4018 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4019 | struct io_async_msghdr *iomsg) | |
4020 | { | |
4021 | iomsg->iov = iomsg->fast_iov; | |
4022 | iomsg->msg.msg_name = &iomsg->addr; | |
4023 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, | |
4024 | req->sr_msg.msg_flags, &iomsg->iov); | |
4025 | } | |
4026 | ||
3529d8c2 | 4027 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4028 | { |
e47293fd | 4029 | struct io_sr_msg *sr = &req->sr_msg; |
3529d8c2 | 4030 | struct io_async_ctx *io = req->io; |
99bc4c38 | 4031 | int ret; |
03b1230c | 4032 | |
d2b6f48b PB |
4033 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4034 | return -EINVAL; | |
4035 | ||
e47293fd | 4036 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4037 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4038 | sr->len = READ_ONCE(sqe->len); |
3529d8c2 | 4039 | |
d8768362 JA |
4040 | #ifdef CONFIG_COMPAT |
4041 | if (req->ctx->compat) | |
4042 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4043 | #endif | |
4044 | ||
fddaface | 4045 | if (!io || req->opcode == IORING_OP_SEND) |
3529d8c2 | 4046 | return 0; |
5f798bea PB |
4047 | /* iovec is already imported */ |
4048 | if (req->flags & REQ_F_NEED_CLEANUP) | |
4049 | return 0; | |
3529d8c2 | 4050 | |
2ae523ed | 4051 | ret = io_sendmsg_copy_hdr(req, &io->msg); |
99bc4c38 PB |
4052 | if (!ret) |
4053 | req->flags |= REQ_F_NEED_CLEANUP; | |
4054 | return ret; | |
03b1230c JA |
4055 | } |
4056 | ||
229a7b63 JA |
4057 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
4058 | struct io_comp_state *cs) | |
aa1fa28f | 4059 | { |
6b754c8b | 4060 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4061 | struct socket *sock; |
7a7cacba | 4062 | unsigned flags; |
0fa03c62 JA |
4063 | int ret; |
4064 | ||
0fa03c62 | 4065 | sock = sock_from_file(req->file, &ret); |
7a7cacba PB |
4066 | if (unlikely(!sock)) |
4067 | return ret; | |
3529d8c2 | 4068 | |
7a7cacba PB |
4069 | if (req->io) { |
4070 | kmsg = &req->io->msg; | |
4071 | kmsg->msg.msg_name = &req->io->msg.addr; | |
4072 | /* if iov is set, it's allocated already */ | |
4073 | if (!kmsg->iov) | |
4074 | kmsg->iov = kmsg->fast_iov; | |
4075 | kmsg->msg.msg_iter.iov = kmsg->iov; | |
4076 | } else { | |
4077 | ret = io_sendmsg_copy_hdr(req, &iomsg); | |
4078 | if (ret) | |
4079 | return ret; | |
4080 | kmsg = &iomsg; | |
0fa03c62 | 4081 | } |
0fa03c62 | 4082 | |
7a7cacba PB |
4083 | flags = req->sr_msg.msg_flags; |
4084 | if (flags & MSG_DONTWAIT) | |
4085 | req->flags |= REQ_F_NOWAIT; | |
4086 | else if (force_nonblock) | |
4087 | flags |= MSG_DONTWAIT; | |
e47293fd | 4088 | |
7a7cacba PB |
4089 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
4090 | if (force_nonblock && ret == -EAGAIN) | |
4091 | return io_setup_async_msg(req, kmsg); | |
4092 | if (ret == -ERESTARTSYS) | |
4093 | ret = -EINTR; | |
0fa03c62 | 4094 | |
6b754c8b | 4095 | if (kmsg->iov != kmsg->fast_iov) |
0b416c3e | 4096 | kfree(kmsg->iov); |
99bc4c38 | 4097 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4098 | if (ret < 0) |
4099 | req_set_fail_links(req); | |
229a7b63 | 4100 | __io_req_complete(req, ret, 0, cs); |
5d17b4a4 | 4101 | return 0; |
03b1230c | 4102 | } |
aa1fa28f | 4103 | |
229a7b63 JA |
4104 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
4105 | struct io_comp_state *cs) | |
fddaface | 4106 | { |
7a7cacba PB |
4107 | struct io_sr_msg *sr = &req->sr_msg; |
4108 | struct msghdr msg; | |
4109 | struct iovec iov; | |
fddaface | 4110 | struct socket *sock; |
7a7cacba | 4111 | unsigned flags; |
fddaface JA |
4112 | int ret; |
4113 | ||
fddaface | 4114 | sock = sock_from_file(req->file, &ret); |
7a7cacba PB |
4115 | if (unlikely(!sock)) |
4116 | return ret; | |
fddaface | 4117 | |
7a7cacba PB |
4118 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4119 | if (unlikely(ret)) | |
14c32eee | 4120 | return ret;; |
fddaface | 4121 | |
7a7cacba PB |
4122 | msg.msg_name = NULL; |
4123 | msg.msg_control = NULL; | |
4124 | msg.msg_controllen = 0; | |
4125 | msg.msg_namelen = 0; | |
fddaface | 4126 | |
7a7cacba PB |
4127 | flags = req->sr_msg.msg_flags; |
4128 | if (flags & MSG_DONTWAIT) | |
4129 | req->flags |= REQ_F_NOWAIT; | |
4130 | else if (force_nonblock) | |
4131 | flags |= MSG_DONTWAIT; | |
fddaface | 4132 | |
7a7cacba PB |
4133 | msg.msg_flags = flags; |
4134 | ret = sock_sendmsg(sock, &msg); | |
4135 | if (force_nonblock && ret == -EAGAIN) | |
4136 | return -EAGAIN; | |
4137 | if (ret == -ERESTARTSYS) | |
4138 | ret = -EINTR; | |
fddaface | 4139 | |
fddaface JA |
4140 | if (ret < 0) |
4141 | req_set_fail_links(req); | |
229a7b63 | 4142 | __io_req_complete(req, ret, 0, cs); |
fddaface | 4143 | return 0; |
fddaface JA |
4144 | } |
4145 | ||
1400e697 PB |
4146 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4147 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4148 | { |
4149 | struct io_sr_msg *sr = &req->sr_msg; | |
4150 | struct iovec __user *uiov; | |
4151 | size_t iov_len; | |
4152 | int ret; | |
4153 | ||
1400e697 PB |
4154 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4155 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4156 | if (ret) |
4157 | return ret; | |
4158 | ||
4159 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4160 | if (iov_len > 1) | |
4161 | return -EINVAL; | |
1400e697 | 4162 | if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4163 | return -EFAULT; |
1400e697 PB |
4164 | sr->len = iomsg->iov[0].iov_len; |
4165 | iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1, | |
52de1fe1 | 4166 | sr->len); |
1400e697 | 4167 | iomsg->iov = NULL; |
52de1fe1 JA |
4168 | } else { |
4169 | ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV, | |
1400e697 | 4170 | &iomsg->iov, &iomsg->msg.msg_iter); |
52de1fe1 JA |
4171 | if (ret > 0) |
4172 | ret = 0; | |
4173 | } | |
4174 | ||
4175 | return ret; | |
4176 | } | |
4177 | ||
4178 | #ifdef CONFIG_COMPAT | |
4179 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4180 | struct io_async_msghdr *iomsg) |
52de1fe1 JA |
4181 | { |
4182 | struct compat_msghdr __user *msg_compat; | |
4183 | struct io_sr_msg *sr = &req->sr_msg; | |
4184 | struct compat_iovec __user *uiov; | |
4185 | compat_uptr_t ptr; | |
4186 | compat_size_t len; | |
4187 | int ret; | |
4188 | ||
270a5940 | 4189 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
1400e697 | 4190 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
52de1fe1 JA |
4191 | &ptr, &len); |
4192 | if (ret) | |
4193 | return ret; | |
4194 | ||
4195 | uiov = compat_ptr(ptr); | |
4196 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4197 | compat_ssize_t clen; | |
4198 | ||
4199 | if (len > 1) | |
4200 | return -EINVAL; | |
4201 | if (!access_ok(uiov, sizeof(*uiov))) | |
4202 | return -EFAULT; | |
4203 | if (__get_user(clen, &uiov->iov_len)) | |
4204 | return -EFAULT; | |
4205 | if (clen < 0) | |
4206 | return -EINVAL; | |
1400e697 PB |
4207 | sr->len = iomsg->iov[0].iov_len; |
4208 | iomsg->iov = NULL; | |
52de1fe1 JA |
4209 | } else { |
4210 | ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV, | |
1400e697 PB |
4211 | &iomsg->iov, |
4212 | &iomsg->msg.msg_iter); | |
52de1fe1 JA |
4213 | if (ret < 0) |
4214 | return ret; | |
4215 | } | |
4216 | ||
4217 | return 0; | |
4218 | } | |
4219 | #endif | |
4220 | ||
1400e697 PB |
4221 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4222 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4223 | { |
1400e697 PB |
4224 | iomsg->msg.msg_name = &iomsg->addr; |
4225 | iomsg->iov = iomsg->fast_iov; | |
52de1fe1 JA |
4226 | |
4227 | #ifdef CONFIG_COMPAT | |
4228 | if (req->ctx->compat) | |
1400e697 | 4229 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4230 | #endif |
52de1fe1 | 4231 | |
1400e697 | 4232 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4233 | } |
4234 | ||
bcda7baa | 4235 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4236 | bool needs_lock) |
bcda7baa JA |
4237 | { |
4238 | struct io_sr_msg *sr = &req->sr_msg; | |
4239 | struct io_buffer *kbuf; | |
4240 | ||
bcda7baa JA |
4241 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4242 | if (IS_ERR(kbuf)) | |
4243 | return kbuf; | |
4244 | ||
4245 | sr->kbuf = kbuf; | |
4246 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4247 | return kbuf; |
fddaface JA |
4248 | } |
4249 | ||
7fbb1b54 PB |
4250 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4251 | { | |
4252 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4253 | } | |
4254 | ||
3529d8c2 JA |
4255 | static int io_recvmsg_prep(struct io_kiocb *req, |
4256 | const struct io_uring_sqe *sqe) | |
aa1fa28f | 4257 | { |
e47293fd | 4258 | struct io_sr_msg *sr = &req->sr_msg; |
3529d8c2 | 4259 | struct io_async_ctx *io = req->io; |
99bc4c38 | 4260 | int ret; |
3529d8c2 | 4261 | |
d2b6f48b PB |
4262 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4263 | return -EINVAL; | |
4264 | ||
3529d8c2 | 4265 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4266 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4267 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4268 | sr->bgid = READ_ONCE(sqe->buf_group); |
06b76d44 | 4269 | |
d8768362 JA |
4270 | #ifdef CONFIG_COMPAT |
4271 | if (req->ctx->compat) | |
4272 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4273 | #endif | |
4274 | ||
fddaface | 4275 | if (!io || req->opcode == IORING_OP_RECV) |
06b76d44 | 4276 | return 0; |
5f798bea PB |
4277 | /* iovec is already imported */ |
4278 | if (req->flags & REQ_F_NEED_CLEANUP) | |
4279 | return 0; | |
03b1230c | 4280 | |
1400e697 | 4281 | ret = io_recvmsg_copy_hdr(req, &io->msg); |
99bc4c38 PB |
4282 | if (!ret) |
4283 | req->flags |= REQ_F_NEED_CLEANUP; | |
4284 | return ret; | |
aa1fa28f JA |
4285 | } |
4286 | ||
229a7b63 JA |
4287 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
4288 | struct io_comp_state *cs) | |
aa1fa28f | 4289 | { |
6b754c8b | 4290 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4291 | struct socket *sock; |
7fbb1b54 | 4292 | struct io_buffer *kbuf; |
7a7cacba | 4293 | unsigned flags; |
52de1fe1 | 4294 | int ret, cflags = 0; |
03b1230c | 4295 | |
03b1230c | 4296 | sock = sock_from_file(req->file, &ret); |
7a7cacba PB |
4297 | if (unlikely(!sock)) |
4298 | return ret; | |
3529d8c2 | 4299 | |
7a7cacba PB |
4300 | if (req->io) { |
4301 | kmsg = &req->io->msg; | |
4302 | kmsg->msg.msg_name = &req->io->msg.addr; | |
4303 | /* if iov is set, it's allocated already */ | |
4304 | if (!kmsg->iov) | |
4305 | kmsg->iov = kmsg->fast_iov; | |
4306 | kmsg->msg.msg_iter.iov = kmsg->iov; | |
4307 | } else { | |
4308 | ret = io_recvmsg_copy_hdr(req, &iomsg); | |
4309 | if (ret) | |
681fda8d | 4310 | return ret; |
7a7cacba PB |
4311 | kmsg = &iomsg; |
4312 | } | |
03b1230c | 4313 | |
bc02ef33 | 4314 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4315 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4316 | if (IS_ERR(kbuf)) |
52de1fe1 | 4317 | return PTR_ERR(kbuf); |
7a7cacba PB |
4318 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
4319 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov, | |
4320 | 1, req->sr_msg.len); | |
4321 | } | |
52de1fe1 | 4322 | |
7a7cacba PB |
4323 | flags = req->sr_msg.msg_flags; |
4324 | if (flags & MSG_DONTWAIT) | |
4325 | req->flags |= REQ_F_NOWAIT; | |
4326 | else if (force_nonblock) | |
4327 | flags |= MSG_DONTWAIT; | |
e47293fd | 4328 | |
7a7cacba PB |
4329 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4330 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4331 | if (force_nonblock && ret == -EAGAIN) |
4332 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4333 | if (ret == -ERESTARTSYS) |
4334 | ret = -EINTR; | |
03b1230c | 4335 | |
7fbb1b54 PB |
4336 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4337 | cflags = io_put_recv_kbuf(req); | |
6b754c8b | 4338 | if (kmsg->iov != kmsg->fast_iov) |
0b416c3e | 4339 | kfree(kmsg->iov); |
99bc4c38 | 4340 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4341 | if (ret < 0) |
4342 | req_set_fail_links(req); | |
229a7b63 | 4343 | __io_req_complete(req, ret, cflags, cs); |
03b1230c | 4344 | return 0; |
0fa03c62 | 4345 | } |
5d17b4a4 | 4346 | |
229a7b63 JA |
4347 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
4348 | struct io_comp_state *cs) | |
fddaface | 4349 | { |
6b754c8b | 4350 | struct io_buffer *kbuf; |
7a7cacba PB |
4351 | struct io_sr_msg *sr = &req->sr_msg; |
4352 | struct msghdr msg; | |
4353 | void __user *buf = sr->buf; | |
fddaface | 4354 | struct socket *sock; |
7a7cacba PB |
4355 | struct iovec iov; |
4356 | unsigned flags; | |
bcda7baa | 4357 | int ret, cflags = 0; |
fddaface | 4358 | |
fddaface | 4359 | sock = sock_from_file(req->file, &ret); |
7a7cacba PB |
4360 | if (unlikely(!sock)) |
4361 | return ret; | |
fddaface | 4362 | |
bc02ef33 | 4363 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4364 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4365 | if (IS_ERR(kbuf)) |
4366 | return PTR_ERR(kbuf); | |
7a7cacba | 4367 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4368 | } |
bcda7baa | 4369 | |
7a7cacba | 4370 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4371 | if (unlikely(ret)) |
4372 | goto out_free; | |
fddaface | 4373 | |
7a7cacba PB |
4374 | msg.msg_name = NULL; |
4375 | msg.msg_control = NULL; | |
4376 | msg.msg_controllen = 0; | |
4377 | msg.msg_namelen = 0; | |
4378 | msg.msg_iocb = NULL; | |
4379 | msg.msg_flags = 0; | |
fddaface | 4380 | |
7a7cacba PB |
4381 | flags = req->sr_msg.msg_flags; |
4382 | if (flags & MSG_DONTWAIT) | |
4383 | req->flags |= REQ_F_NOWAIT; | |
4384 | else if (force_nonblock) | |
4385 | flags |= MSG_DONTWAIT; | |
4386 | ||
4387 | ret = sock_recvmsg(sock, &msg, flags); | |
4388 | if (force_nonblock && ret == -EAGAIN) | |
4389 | return -EAGAIN; | |
4390 | if (ret == -ERESTARTSYS) | |
4391 | ret = -EINTR; | |
14c32eee | 4392 | out_free: |
7fbb1b54 PB |
4393 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4394 | cflags = io_put_recv_kbuf(req); | |
fddaface JA |
4395 | if (ret < 0) |
4396 | req_set_fail_links(req); | |
229a7b63 | 4397 | __io_req_complete(req, ret, cflags, cs); |
fddaface | 4398 | return 0; |
fddaface JA |
4399 | } |
4400 | ||
3529d8c2 | 4401 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 4402 | { |
8ed8d3c3 JA |
4403 | struct io_accept *accept = &req->accept; |
4404 | ||
17f2fe35 JA |
4405 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
4406 | return -EINVAL; | |
8042d6ce | 4407 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
4408 | return -EINVAL; |
4409 | ||
d55e5f5b JA |
4410 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4411 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 4412 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 4413 | accept->nofile = rlimit(RLIMIT_NOFILE); |
8ed8d3c3 | 4414 | return 0; |
8ed8d3c3 | 4415 | } |
17f2fe35 | 4416 | |
229a7b63 JA |
4417 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
4418 | struct io_comp_state *cs) | |
8ed8d3c3 JA |
4419 | { |
4420 | struct io_accept *accept = &req->accept; | |
ac45abc0 | 4421 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
8ed8d3c3 JA |
4422 | int ret; |
4423 | ||
e697deed JX |
4424 | if (req->file->f_flags & O_NONBLOCK) |
4425 | req->flags |= REQ_F_NOWAIT; | |
4426 | ||
8ed8d3c3 | 4427 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
09952e3e JA |
4428 | accept->addr_len, accept->flags, |
4429 | accept->nofile); | |
8ed8d3c3 | 4430 | if (ret == -EAGAIN && force_nonblock) |
17f2fe35 | 4431 | return -EAGAIN; |
ac45abc0 PB |
4432 | if (ret < 0) { |
4433 | if (ret == -ERESTARTSYS) | |
4434 | ret = -EINTR; | |
4e88d6e7 | 4435 | req_set_fail_links(req); |
ac45abc0 | 4436 | } |
229a7b63 | 4437 | __io_req_complete(req, ret, 0, cs); |
17f2fe35 | 4438 | return 0; |
8ed8d3c3 JA |
4439 | } |
4440 | ||
3529d8c2 | 4441 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 4442 | { |
3529d8c2 JA |
4443 | struct io_connect *conn = &req->connect; |
4444 | struct io_async_ctx *io = req->io; | |
f499a021 | 4445 | |
3fbb51c1 JA |
4446 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
4447 | return -EINVAL; | |
4448 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
4449 | return -EINVAL; | |
4450 | ||
3529d8c2 JA |
4451 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4452 | conn->addr_len = READ_ONCE(sqe->addr2); | |
4453 | ||
4454 | if (!io) | |
4455 | return 0; | |
4456 | ||
4457 | return move_addr_to_kernel(conn->addr, conn->addr_len, | |
3fbb51c1 | 4458 | &io->connect.address); |
f499a021 JA |
4459 | } |
4460 | ||
229a7b63 JA |
4461 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
4462 | struct io_comp_state *cs) | |
f8e85cf2 | 4463 | { |
f499a021 | 4464 | struct io_async_ctx __io, *io; |
f8e85cf2 | 4465 | unsigned file_flags; |
3fbb51c1 | 4466 | int ret; |
f8e85cf2 | 4467 | |
f499a021 JA |
4468 | if (req->io) { |
4469 | io = req->io; | |
4470 | } else { | |
3529d8c2 JA |
4471 | ret = move_addr_to_kernel(req->connect.addr, |
4472 | req->connect.addr_len, | |
4473 | &__io.connect.address); | |
f499a021 JA |
4474 | if (ret) |
4475 | goto out; | |
4476 | io = &__io; | |
4477 | } | |
4478 | ||
3fbb51c1 JA |
4479 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
4480 | ||
4481 | ret = __sys_connect_file(req->file, &io->connect.address, | |
4482 | req->connect.addr_len, file_flags); | |
87f80d62 | 4483 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
b7bb4f7d JA |
4484 | if (req->io) |
4485 | return -EAGAIN; | |
4486 | if (io_alloc_async_ctx(req)) { | |
f499a021 JA |
4487 | ret = -ENOMEM; |
4488 | goto out; | |
4489 | } | |
b7bb4f7d | 4490 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
f8e85cf2 | 4491 | return -EAGAIN; |
f499a021 | 4492 | } |
f8e85cf2 JA |
4493 | if (ret == -ERESTARTSYS) |
4494 | ret = -EINTR; | |
f499a021 | 4495 | out: |
4e88d6e7 JA |
4496 | if (ret < 0) |
4497 | req_set_fail_links(req); | |
229a7b63 | 4498 | __io_req_complete(req, ret, 0, cs); |
f8e85cf2 | 4499 | return 0; |
469956e8 Y |
4500 | } |
4501 | #else /* !CONFIG_NET */ | |
4502 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4503 | { | |
f8e85cf2 | 4504 | return -EOPNOTSUPP; |
f8e85cf2 JA |
4505 | } |
4506 | ||
1e16c2f9 RD |
4507 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
4508 | struct io_comp_state *cs) | |
469956e8 Y |
4509 | { |
4510 | return -EOPNOTSUPP; | |
4511 | } | |
4512 | ||
1e16c2f9 RD |
4513 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
4514 | struct io_comp_state *cs) | |
469956e8 Y |
4515 | { |
4516 | return -EOPNOTSUPP; | |
4517 | } | |
4518 | ||
4519 | static int io_recvmsg_prep(struct io_kiocb *req, | |
4520 | const struct io_uring_sqe *sqe) | |
4521 | { | |
4522 | return -EOPNOTSUPP; | |
4523 | } | |
4524 | ||
1e16c2f9 RD |
4525 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
4526 | struct io_comp_state *cs) | |
469956e8 Y |
4527 | { |
4528 | return -EOPNOTSUPP; | |
4529 | } | |
4530 | ||
1e16c2f9 RD |
4531 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
4532 | struct io_comp_state *cs) | |
469956e8 Y |
4533 | { |
4534 | return -EOPNOTSUPP; | |
4535 | } | |
4536 | ||
4537 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4538 | { | |
4539 | return -EOPNOTSUPP; | |
4540 | } | |
4541 | ||
1e16c2f9 RD |
4542 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
4543 | struct io_comp_state *cs) | |
469956e8 Y |
4544 | { |
4545 | return -EOPNOTSUPP; | |
4546 | } | |
ce593a6c | 4547 | |
469956e8 Y |
4548 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4549 | { | |
4550 | return -EOPNOTSUPP; | |
4551 | } | |
4552 | ||
1e16c2f9 RD |
4553 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
4554 | struct io_comp_state *cs) | |
469956e8 | 4555 | { |
f8e85cf2 | 4556 | return -EOPNOTSUPP; |
ce593a6c | 4557 | } |
469956e8 | 4558 | #endif /* CONFIG_NET */ |
f8e85cf2 | 4559 | |
d7718a9d JA |
4560 | struct io_poll_table { |
4561 | struct poll_table_struct pt; | |
4562 | struct io_kiocb *req; | |
4563 | int error; | |
4564 | }; | |
ce593a6c | 4565 | |
d7718a9d JA |
4566 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
4567 | __poll_t mask, task_work_func_t func) | |
4568 | { | |
aa96bf8a | 4569 | int ret; |
d7718a9d JA |
4570 | |
4571 | /* for instances that support it check for an event match first: */ | |
4572 | if (mask && !(mask & poll->events)) | |
4573 | return 0; | |
4574 | ||
4575 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
4576 | ||
4577 | list_del_init(&poll->wait.entry); | |
4578 | ||
d7718a9d JA |
4579 | req->result = mask; |
4580 | init_task_work(&req->task_work, func); | |
6d816e08 JA |
4581 | percpu_ref_get(&req->ctx->refs); |
4582 | ||
d7718a9d | 4583 | /* |
e3aabf95 JA |
4584 | * If this fails, then the task is exiting. When a task exits, the |
4585 | * work gets canceled, so just cancel this request as well instead | |
4586 | * of executing it. We can't safely execute it anyway, as we may not | |
4587 | * have the needed state needed for it anyway. | |
d7718a9d | 4588 | */ |
b7db41c9 | 4589 | ret = io_req_task_work_add(req, &req->task_work); |
aa96bf8a | 4590 | if (unlikely(ret)) { |
c2c4c83c JA |
4591 | struct task_struct *tsk; |
4592 | ||
e3aabf95 | 4593 | WRITE_ONCE(poll->canceled, true); |
aa96bf8a | 4594 | tsk = io_wq_get_task(req->ctx->io_wq); |
ce593a6c JA |
4595 | task_work_add(tsk, &req->task_work, 0); |
4596 | wake_up_process(tsk); | |
aa96bf8a | 4597 | } |
d7718a9d JA |
4598 | return 1; |
4599 | } | |
4600 | ||
74ce6ce4 JA |
4601 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
4602 | __acquires(&req->ctx->completion_lock) | |
4603 | { | |
4604 | struct io_ring_ctx *ctx = req->ctx; | |
4605 | ||
4606 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
4607 | struct poll_table_struct pt = { ._key = poll->events }; | |
4608 | ||
4609 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
4610 | } | |
4611 | ||
4612 | spin_lock_irq(&ctx->completion_lock); | |
4613 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
4614 | add_wait_queue(poll->head, &poll->wait); | |
4615 | return true; | |
4616 | } | |
4617 | ||
4618 | return false; | |
4619 | } | |
4620 | ||
807abcb0 | 4621 | static void io_poll_remove_double(struct io_kiocb *req, void *data) |
18bceab1 | 4622 | { |
807abcb0 | 4623 | struct io_poll_iocb *poll = data; |
18bceab1 JA |
4624 | |
4625 | lockdep_assert_held(&req->ctx->completion_lock); | |
4626 | ||
4627 | if (poll && poll->head) { | |
4628 | struct wait_queue_head *head = poll->head; | |
4629 | ||
4630 | spin_lock(&head->lock); | |
4631 | list_del_init(&poll->wait.entry); | |
4632 | if (poll->wait.private) | |
4633 | refcount_dec(&req->refs); | |
4634 | poll->head = NULL; | |
4635 | spin_unlock(&head->lock); | |
4636 | } | |
4637 | } | |
4638 | ||
4639 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) | |
4640 | { | |
4641 | struct io_ring_ctx *ctx = req->ctx; | |
4642 | ||
807abcb0 | 4643 | io_poll_remove_double(req, req->io); |
18bceab1 JA |
4644 | req->poll.done = true; |
4645 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); | |
4646 | io_commit_cqring(ctx); | |
4647 | } | |
4648 | ||
4649 | static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt) | |
4650 | { | |
4651 | struct io_ring_ctx *ctx = req->ctx; | |
4652 | ||
4653 | if (io_poll_rewait(req, &req->poll)) { | |
4654 | spin_unlock_irq(&ctx->completion_lock); | |
4655 | return; | |
4656 | } | |
4657 | ||
4658 | hash_del(&req->hash_node); | |
4659 | io_poll_complete(req, req->result, 0); | |
4660 | req->flags |= REQ_F_COMP_LOCKED; | |
9b5f7bd9 | 4661 | *nxt = io_put_req_find_next(req); |
18bceab1 JA |
4662 | spin_unlock_irq(&ctx->completion_lock); |
4663 | ||
4664 | io_cqring_ev_posted(ctx); | |
4665 | } | |
4666 | ||
4667 | static void io_poll_task_func(struct callback_head *cb) | |
4668 | { | |
4669 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
6d816e08 | 4670 | struct io_ring_ctx *ctx = req->ctx; |
18bceab1 JA |
4671 | struct io_kiocb *nxt = NULL; |
4672 | ||
4673 | io_poll_task_handler(req, &nxt); | |
ea1164e5 PB |
4674 | if (nxt) |
4675 | __io_req_task_submit(nxt); | |
6d816e08 | 4676 | percpu_ref_put(&ctx->refs); |
18bceab1 JA |
4677 | } |
4678 | ||
4679 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
4680 | int sync, void *key) | |
4681 | { | |
4682 | struct io_kiocb *req = wait->private; | |
807abcb0 | 4683 | struct io_poll_iocb *poll = req->apoll->double_poll; |
18bceab1 JA |
4684 | __poll_t mask = key_to_poll(key); |
4685 | ||
4686 | /* for instances that support it check for an event match first: */ | |
4687 | if (mask && !(mask & poll->events)) | |
4688 | return 0; | |
4689 | ||
807abcb0 | 4690 | if (poll && poll->head) { |
18bceab1 JA |
4691 | bool done; |
4692 | ||
807abcb0 JA |
4693 | spin_lock(&poll->head->lock); |
4694 | done = list_empty(&poll->wait.entry); | |
18bceab1 | 4695 | if (!done) |
807abcb0 JA |
4696 | list_del_init(&poll->wait.entry); |
4697 | spin_unlock(&poll->head->lock); | |
18bceab1 JA |
4698 | if (!done) |
4699 | __io_async_wake(req, poll, mask, io_poll_task_func); | |
4700 | } | |
4701 | refcount_dec(&req->refs); | |
4702 | return 1; | |
4703 | } | |
4704 | ||
4705 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
4706 | wait_queue_func_t wake_func) | |
4707 | { | |
4708 | poll->head = NULL; | |
4709 | poll->done = false; | |
4710 | poll->canceled = false; | |
4711 | poll->events = events; | |
4712 | INIT_LIST_HEAD(&poll->wait.entry); | |
4713 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
4714 | } | |
4715 | ||
4716 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
4717 | struct wait_queue_head *head, |
4718 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
4719 | { |
4720 | struct io_kiocb *req = pt->req; | |
4721 | ||
4722 | /* | |
4723 | * If poll->head is already set, it's because the file being polled | |
4724 | * uses multiple waitqueues for poll handling (eg one for read, one | |
4725 | * for write). Setup a separate io_poll_iocb if this happens. | |
4726 | */ | |
4727 | if (unlikely(poll->head)) { | |
4728 | /* already have a 2nd entry, fail a third attempt */ | |
807abcb0 | 4729 | if (*poll_ptr) { |
18bceab1 JA |
4730 | pt->error = -EINVAL; |
4731 | return; | |
4732 | } | |
4733 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); | |
4734 | if (!poll) { | |
4735 | pt->error = -ENOMEM; | |
4736 | return; | |
4737 | } | |
4738 | io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake); | |
4739 | refcount_inc(&req->refs); | |
4740 | poll->wait.private = req; | |
807abcb0 | 4741 | *poll_ptr = poll; |
18bceab1 JA |
4742 | } |
4743 | ||
4744 | pt->error = 0; | |
4745 | poll->head = head; | |
a31eb4a2 JX |
4746 | |
4747 | if (poll->events & EPOLLEXCLUSIVE) | |
4748 | add_wait_queue_exclusive(head, &poll->wait); | |
4749 | else | |
4750 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
4751 | } |
4752 | ||
4753 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
4754 | struct poll_table_struct *p) | |
4755 | { | |
4756 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 4757 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 4758 | |
807abcb0 | 4759 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
4760 | } |
4761 | ||
d7718a9d JA |
4762 | static void io_async_task_func(struct callback_head *cb) |
4763 | { | |
4764 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
4765 | struct async_poll *apoll = req->apoll; | |
4766 | struct io_ring_ctx *ctx = req->ctx; | |
4767 | ||
4768 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); | |
4769 | ||
74ce6ce4 | 4770 | if (io_poll_rewait(req, &apoll->poll)) { |
d7718a9d | 4771 | spin_unlock_irq(&ctx->completion_lock); |
6d816e08 | 4772 | percpu_ref_put(&ctx->refs); |
74ce6ce4 | 4773 | return; |
d7718a9d JA |
4774 | } |
4775 | ||
31067255 | 4776 | /* If req is still hashed, it cannot have been canceled. Don't check. */ |
0be0b0e3 | 4777 | if (hash_hashed(&req->hash_node)) |
74ce6ce4 | 4778 | hash_del(&req->hash_node); |
2bae047e | 4779 | |
807abcb0 | 4780 | io_poll_remove_double(req, apoll->double_poll); |
74ce6ce4 JA |
4781 | spin_unlock_irq(&ctx->completion_lock); |
4782 | ||
0be0b0e3 PB |
4783 | if (!READ_ONCE(apoll->poll.canceled)) |
4784 | __io_req_task_submit(req); | |
4785 | else | |
4786 | __io_req_task_cancel(req, -ECANCELED); | |
aa340845 | 4787 | |
6d816e08 | 4788 | percpu_ref_put(&ctx->refs); |
807abcb0 | 4789 | kfree(apoll->double_poll); |
31067255 | 4790 | kfree(apoll); |
d7718a9d JA |
4791 | } |
4792 | ||
4793 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
4794 | void *key) | |
4795 | { | |
4796 | struct io_kiocb *req = wait->private; | |
4797 | struct io_poll_iocb *poll = &req->apoll->poll; | |
4798 | ||
4799 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
4800 | key_to_poll(key)); | |
4801 | ||
4802 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
4803 | } | |
4804 | ||
4805 | static void io_poll_req_insert(struct io_kiocb *req) | |
4806 | { | |
4807 | struct io_ring_ctx *ctx = req->ctx; | |
4808 | struct hlist_head *list; | |
4809 | ||
4810 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
4811 | hlist_add_head(&req->hash_node, list); | |
4812 | } | |
4813 | ||
4814 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
4815 | struct io_poll_iocb *poll, | |
4816 | struct io_poll_table *ipt, __poll_t mask, | |
4817 | wait_queue_func_t wake_func) | |
4818 | __acquires(&ctx->completion_lock) | |
4819 | { | |
4820 | struct io_ring_ctx *ctx = req->ctx; | |
4821 | bool cancel = false; | |
4822 | ||
18bceab1 | 4823 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 4824 | poll->file = req->file; |
18bceab1 | 4825 | poll->wait.private = req; |
d7718a9d JA |
4826 | |
4827 | ipt->pt._key = mask; | |
4828 | ipt->req = req; | |
4829 | ipt->error = -EINVAL; | |
4830 | ||
d7718a9d JA |
4831 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
4832 | ||
4833 | spin_lock_irq(&ctx->completion_lock); | |
4834 | if (likely(poll->head)) { | |
4835 | spin_lock(&poll->head->lock); | |
4836 | if (unlikely(list_empty(&poll->wait.entry))) { | |
4837 | if (ipt->error) | |
4838 | cancel = true; | |
4839 | ipt->error = 0; | |
4840 | mask = 0; | |
4841 | } | |
4842 | if (mask || ipt->error) | |
4843 | list_del_init(&poll->wait.entry); | |
4844 | else if (cancel) | |
4845 | WRITE_ONCE(poll->canceled, true); | |
4846 | else if (!poll->done) /* actually waiting for an event */ | |
4847 | io_poll_req_insert(req); | |
4848 | spin_unlock(&poll->head->lock); | |
4849 | } | |
4850 | ||
4851 | return mask; | |
4852 | } | |
4853 | ||
4854 | static bool io_arm_poll_handler(struct io_kiocb *req) | |
4855 | { | |
4856 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
4857 | struct io_ring_ctx *ctx = req->ctx; | |
4858 | struct async_poll *apoll; | |
4859 | struct io_poll_table ipt; | |
4860 | __poll_t mask, ret; | |
4861 | ||
4862 | if (!req->file || !file_can_poll(req->file)) | |
4863 | return false; | |
24c74678 | 4864 | if (req->flags & REQ_F_POLLED) |
d7718a9d JA |
4865 | return false; |
4866 | if (!def->pollin && !def->pollout) | |
4867 | return false; | |
4868 | ||
4869 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
4870 | if (unlikely(!apoll)) | |
4871 | return false; | |
807abcb0 | 4872 | apoll->double_poll = NULL; |
d7718a9d JA |
4873 | |
4874 | req->flags |= REQ_F_POLLED; | |
4dd2824d | 4875 | io_get_req_task(req); |
d7718a9d JA |
4876 | req->apoll = apoll; |
4877 | INIT_HLIST_NODE(&req->hash_node); | |
4878 | ||
8755d97a | 4879 | mask = 0; |
d7718a9d | 4880 | if (def->pollin) |
8755d97a | 4881 | mask |= POLLIN | POLLRDNORM; |
d7718a9d JA |
4882 | if (def->pollout) |
4883 | mask |= POLLOUT | POLLWRNORM; | |
4884 | mask |= POLLERR | POLLPRI; | |
4885 | ||
4886 | ipt.pt._qproc = io_async_queue_proc; | |
4887 | ||
4888 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
4889 | io_async_wake); | |
a36da65c | 4890 | if (ret || ipt.error) { |
807abcb0 | 4891 | io_poll_remove_double(req, apoll->double_poll); |
d7718a9d | 4892 | spin_unlock_irq(&ctx->completion_lock); |
807abcb0 | 4893 | kfree(apoll->double_poll); |
d7718a9d JA |
4894 | kfree(apoll); |
4895 | return false; | |
4896 | } | |
4897 | spin_unlock_irq(&ctx->completion_lock); | |
4898 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, | |
4899 | apoll->poll.events); | |
4900 | return true; | |
4901 | } | |
4902 | ||
4903 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
4904 | struct io_poll_iocb *poll) | |
221c5eb2 | 4905 | { |
b41e9852 | 4906 | bool do_complete = false; |
221c5eb2 JA |
4907 | |
4908 | spin_lock(&poll->head->lock); | |
4909 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
4910 | if (!list_empty(&poll->wait.entry)) { |
4911 | list_del_init(&poll->wait.entry); | |
b41e9852 | 4912 | do_complete = true; |
221c5eb2 JA |
4913 | } |
4914 | spin_unlock(&poll->head->lock); | |
3bfa5bcb | 4915 | hash_del(&req->hash_node); |
d7718a9d JA |
4916 | return do_complete; |
4917 | } | |
4918 | ||
4919 | static bool io_poll_remove_one(struct io_kiocb *req) | |
4920 | { | |
4921 | bool do_complete; | |
4922 | ||
4923 | if (req->opcode == IORING_OP_POLL_ADD) { | |
807abcb0 | 4924 | io_poll_remove_double(req, req->io); |
d7718a9d JA |
4925 | do_complete = __io_poll_remove_one(req, &req->poll); |
4926 | } else { | |
3bfa5bcb JA |
4927 | struct async_poll *apoll = req->apoll; |
4928 | ||
807abcb0 JA |
4929 | io_poll_remove_double(req, apoll->double_poll); |
4930 | ||
d7718a9d | 4931 | /* non-poll requests have submit ref still */ |
3bfa5bcb JA |
4932 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
4933 | if (do_complete) { | |
d7718a9d | 4934 | io_put_req(req); |
807abcb0 | 4935 | kfree(apoll->double_poll); |
3bfa5bcb JA |
4936 | kfree(apoll); |
4937 | } | |
b1f573bd XW |
4938 | } |
4939 | ||
b41e9852 JA |
4940 | if (do_complete) { |
4941 | io_cqring_fill_event(req, -ECANCELED); | |
4942 | io_commit_cqring(req->ctx); | |
4943 | req->flags |= REQ_F_COMP_LOCKED; | |
f254ac04 | 4944 | req_set_fail_links(req); |
b41e9852 JA |
4945 | io_put_req(req); |
4946 | } | |
4947 | ||
4948 | return do_complete; | |
221c5eb2 JA |
4949 | } |
4950 | ||
4951 | static void io_poll_remove_all(struct io_ring_ctx *ctx) | |
4952 | { | |
78076bb6 | 4953 | struct hlist_node *tmp; |
221c5eb2 | 4954 | struct io_kiocb *req; |
8e2e1faf | 4955 | int posted = 0, i; |
221c5eb2 JA |
4956 | |
4957 | spin_lock_irq(&ctx->completion_lock); | |
78076bb6 JA |
4958 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
4959 | struct hlist_head *list; | |
4960 | ||
4961 | list = &ctx->cancel_hash[i]; | |
4962 | hlist_for_each_entry_safe(req, tmp, list, hash_node) | |
8e2e1faf | 4963 | posted += io_poll_remove_one(req); |
221c5eb2 JA |
4964 | } |
4965 | spin_unlock_irq(&ctx->completion_lock); | |
b41e9852 | 4966 | |
8e2e1faf JA |
4967 | if (posted) |
4968 | io_cqring_ev_posted(ctx); | |
221c5eb2 JA |
4969 | } |
4970 | ||
47f46768 JA |
4971 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
4972 | { | |
78076bb6 | 4973 | struct hlist_head *list; |
47f46768 JA |
4974 | struct io_kiocb *req; |
4975 | ||
78076bb6 JA |
4976 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
4977 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
4978 | if (sqe_addr != req->user_data) |
4979 | continue; | |
4980 | if (io_poll_remove_one(req)) | |
eac406c6 | 4981 | return 0; |
b41e9852 | 4982 | return -EALREADY; |
47f46768 JA |
4983 | } |
4984 | ||
4985 | return -ENOENT; | |
4986 | } | |
4987 | ||
3529d8c2 JA |
4988 | static int io_poll_remove_prep(struct io_kiocb *req, |
4989 | const struct io_uring_sqe *sqe) | |
0969e783 | 4990 | { |
0969e783 JA |
4991 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4992 | return -EINVAL; | |
4993 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
4994 | sqe->poll_events) | |
4995 | return -EINVAL; | |
4996 | ||
4997 | req->poll.addr = READ_ONCE(sqe->addr); | |
0969e783 JA |
4998 | return 0; |
4999 | } | |
5000 | ||
221c5eb2 JA |
5001 | /* |
5002 | * Find a running poll command that matches one specified in sqe->addr, | |
5003 | * and remove it if found. | |
5004 | */ | |
fc4df999 | 5005 | static int io_poll_remove(struct io_kiocb *req) |
221c5eb2 JA |
5006 | { |
5007 | struct io_ring_ctx *ctx = req->ctx; | |
0969e783 | 5008 | u64 addr; |
47f46768 | 5009 | int ret; |
221c5eb2 | 5010 | |
0969e783 | 5011 | addr = req->poll.addr; |
221c5eb2 | 5012 | spin_lock_irq(&ctx->completion_lock); |
0969e783 | 5013 | ret = io_poll_cancel(ctx, addr); |
221c5eb2 JA |
5014 | spin_unlock_irq(&ctx->completion_lock); |
5015 | ||
4e88d6e7 JA |
5016 | if (ret < 0) |
5017 | req_set_fail_links(req); | |
e1e16097 | 5018 | io_req_complete(req, ret); |
221c5eb2 JA |
5019 | return 0; |
5020 | } | |
5021 | ||
221c5eb2 JA |
5022 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5023 | void *key) | |
5024 | { | |
c2f2eb7d JA |
5025 | struct io_kiocb *req = wait->private; |
5026 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5027 | |
d7718a9d | 5028 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5029 | } |
5030 | ||
221c5eb2 JA |
5031 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5032 | struct poll_table_struct *p) | |
5033 | { | |
5034 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5035 | ||
807abcb0 | 5036 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->io); |
eac406c6 JA |
5037 | } |
5038 | ||
3529d8c2 | 5039 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5040 | { |
5041 | struct io_poll_iocb *poll = &req->poll; | |
5769a351 | 5042 | u32 events; |
221c5eb2 JA |
5043 | |
5044 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5045 | return -EINVAL; | |
5046 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
5047 | return -EINVAL; | |
09bb8394 JA |
5048 | if (!poll->file) |
5049 | return -EBADF; | |
221c5eb2 | 5050 | |
5769a351 JX |
5051 | events = READ_ONCE(sqe->poll32_events); |
5052 | #ifdef __BIG_ENDIAN | |
5053 | events = swahw32(events); | |
5054 | #endif | |
a31eb4a2 JX |
5055 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
5056 | (events & EPOLLEXCLUSIVE); | |
b41e9852 | 5057 | |
4dd2824d | 5058 | io_get_req_task(req); |
0969e783 JA |
5059 | return 0; |
5060 | } | |
5061 | ||
014db007 | 5062 | static int io_poll_add(struct io_kiocb *req) |
0969e783 JA |
5063 | { |
5064 | struct io_poll_iocb *poll = &req->poll; | |
5065 | struct io_ring_ctx *ctx = req->ctx; | |
5066 | struct io_poll_table ipt; | |
0969e783 | 5067 | __poll_t mask; |
0969e783 | 5068 | |
78076bb6 | 5069 | INIT_HLIST_NODE(&req->hash_node); |
d7718a9d | 5070 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5071 | |
d7718a9d JA |
5072 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5073 | io_poll_wake); | |
221c5eb2 | 5074 | |
8c838788 | 5075 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5076 | ipt.error = 0; |
b0dd8a41 | 5077 | io_poll_complete(req, mask, 0); |
221c5eb2 | 5078 | } |
221c5eb2 JA |
5079 | spin_unlock_irq(&ctx->completion_lock); |
5080 | ||
8c838788 JA |
5081 | if (mask) { |
5082 | io_cqring_ev_posted(ctx); | |
014db007 | 5083 | io_put_req(req); |
221c5eb2 | 5084 | } |
8c838788 | 5085 | return ipt.error; |
221c5eb2 JA |
5086 | } |
5087 | ||
5262f567 JA |
5088 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5089 | { | |
ad8a48ac JA |
5090 | struct io_timeout_data *data = container_of(timer, |
5091 | struct io_timeout_data, timer); | |
5092 | struct io_kiocb *req = data->req; | |
5093 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5094 | unsigned long flags; |
5095 | ||
5262f567 | 5096 | spin_lock_irqsave(&ctx->completion_lock, flags); |
01cec8c1 PB |
5097 | atomic_set(&req->ctx->cq_timeouts, |
5098 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
5099 | ||
ef03681a | 5100 | /* |
11365043 JA |
5101 | * We could be racing with timeout deletion. If the list is empty, |
5102 | * then timeout lookup already found it and will be handling it. | |
ef03681a | 5103 | */ |
135fcde8 PB |
5104 | if (!list_empty(&req->timeout.list)) |
5105 | list_del_init(&req->timeout.list); | |
5262f567 | 5106 | |
78e19bbe | 5107 | io_cqring_fill_event(req, -ETIME); |
5262f567 JA |
5108 | io_commit_cqring(ctx); |
5109 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5110 | ||
5111 | io_cqring_ev_posted(ctx); | |
4e88d6e7 | 5112 | req_set_fail_links(req); |
5262f567 JA |
5113 | io_put_req(req); |
5114 | return HRTIMER_NORESTART; | |
5115 | } | |
5116 | ||
f254ac04 JA |
5117 | static int __io_timeout_cancel(struct io_kiocb *req) |
5118 | { | |
5119 | int ret; | |
5120 | ||
5121 | list_del_init(&req->timeout.list); | |
5122 | ||
5123 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); | |
5124 | if (ret == -1) | |
5125 | return -EALREADY; | |
5126 | ||
5127 | req_set_fail_links(req); | |
5128 | req->flags |= REQ_F_COMP_LOCKED; | |
5129 | io_cqring_fill_event(req, -ECANCELED); | |
5130 | io_put_req(req); | |
5131 | return 0; | |
5132 | } | |
5133 | ||
47f46768 JA |
5134 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
5135 | { | |
5136 | struct io_kiocb *req; | |
5137 | int ret = -ENOENT; | |
5138 | ||
135fcde8 | 5139 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
47f46768 | 5140 | if (user_data == req->user_data) { |
47f46768 JA |
5141 | ret = 0; |
5142 | break; | |
5143 | } | |
5144 | } | |
5145 | ||
5146 | if (ret == -ENOENT) | |
5147 | return ret; | |
5148 | ||
f254ac04 | 5149 | return __io_timeout_cancel(req); |
47f46768 JA |
5150 | } |
5151 | ||
3529d8c2 JA |
5152 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5153 | const struct io_uring_sqe *sqe) | |
b29472ee | 5154 | { |
b29472ee JA |
5155 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5156 | return -EINVAL; | |
61710e43 DA |
5157 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5158 | return -EINVAL; | |
5159 | if (sqe->ioprio || sqe->buf_index || sqe->len) | |
b29472ee JA |
5160 | return -EINVAL; |
5161 | ||
5162 | req->timeout.addr = READ_ONCE(sqe->addr); | |
5163 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); | |
5164 | if (req->timeout.flags) | |
5165 | return -EINVAL; | |
5166 | ||
b29472ee JA |
5167 | return 0; |
5168 | } | |
5169 | ||
11365043 JA |
5170 | /* |
5171 | * Remove or update an existing timeout command | |
5172 | */ | |
fc4df999 | 5173 | static int io_timeout_remove(struct io_kiocb *req) |
11365043 JA |
5174 | { |
5175 | struct io_ring_ctx *ctx = req->ctx; | |
47f46768 | 5176 | int ret; |
11365043 | 5177 | |
11365043 | 5178 | spin_lock_irq(&ctx->completion_lock); |
b29472ee | 5179 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
11365043 | 5180 | |
47f46768 | 5181 | io_cqring_fill_event(req, ret); |
11365043 JA |
5182 | io_commit_cqring(ctx); |
5183 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 5184 | io_cqring_ev_posted(ctx); |
4e88d6e7 JA |
5185 | if (ret < 0) |
5186 | req_set_fail_links(req); | |
ec9c02ad | 5187 | io_put_req(req); |
11365043 | 5188 | return 0; |
5262f567 JA |
5189 | } |
5190 | ||
3529d8c2 | 5191 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5192 | bool is_timeout_link) |
5262f567 | 5193 | { |
ad8a48ac | 5194 | struct io_timeout_data *data; |
a41525ab | 5195 | unsigned flags; |
56080b02 | 5196 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5197 | |
ad8a48ac | 5198 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5199 | return -EINVAL; |
ad8a48ac | 5200 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 5201 | return -EINVAL; |
56080b02 | 5202 | if (off && is_timeout_link) |
2d28390a | 5203 | return -EINVAL; |
a41525ab JA |
5204 | flags = READ_ONCE(sqe->timeout_flags); |
5205 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5206 | return -EINVAL; |
bdf20073 | 5207 | |
bfe68a22 | 5208 | req->timeout.off = off; |
26a61679 | 5209 | |
3529d8c2 | 5210 | if (!req->io && io_alloc_async_ctx(req)) |
26a61679 JA |
5211 | return -ENOMEM; |
5212 | ||
5213 | data = &req->io->timeout; | |
ad8a48ac | 5214 | data->req = req; |
ad8a48ac JA |
5215 | |
5216 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5217 | return -EFAULT; |
5218 | ||
11365043 | 5219 | if (flags & IORING_TIMEOUT_ABS) |
ad8a48ac | 5220 | data->mode = HRTIMER_MODE_ABS; |
11365043 | 5221 | else |
ad8a48ac | 5222 | data->mode = HRTIMER_MODE_REL; |
11365043 | 5223 | |
ad8a48ac JA |
5224 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
5225 | return 0; | |
5226 | } | |
5227 | ||
fc4df999 | 5228 | static int io_timeout(struct io_kiocb *req) |
ad8a48ac | 5229 | { |
ad8a48ac | 5230 | struct io_ring_ctx *ctx = req->ctx; |
bfe68a22 | 5231 | struct io_timeout_data *data = &req->io->timeout; |
ad8a48ac | 5232 | struct list_head *entry; |
bfe68a22 | 5233 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5234 | |
733f5c95 | 5235 | spin_lock_irq(&ctx->completion_lock); |
93bd25bb | 5236 | |
5262f567 JA |
5237 | /* |
5238 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5239 | * timeout event to be satisfied. If it isn't set, then this is |
5240 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5241 | */ |
8eb7e2d0 | 5242 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5243 | entry = ctx->timeout_list.prev; |
5244 | goto add; | |
5245 | } | |
5262f567 | 5246 | |
bfe68a22 PB |
5247 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5248 | req->timeout.target_seq = tail + off; | |
5262f567 JA |
5249 | |
5250 | /* | |
5251 | * Insertion sort, ensuring the first entry in the list is always | |
5252 | * the one we need first. | |
5253 | */ | |
5262f567 | 5254 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5255 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5256 | timeout.list); | |
5262f567 | 5257 | |
8eb7e2d0 | 5258 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5259 | continue; |
bfe68a22 PB |
5260 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5261 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5262 | break; |
5263 | } | |
93bd25bb | 5264 | add: |
135fcde8 | 5265 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5266 | data->timer.function = io_timeout_fn; |
5267 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 5268 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
5269 | return 0; |
5270 | } | |
5262f567 | 5271 | |
62755e35 JA |
5272 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5273 | { | |
5274 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
5275 | ||
5276 | return req->user_data == (unsigned long) data; | |
5277 | } | |
5278 | ||
e977d6d3 | 5279 | static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr) |
62755e35 | 5280 | { |
62755e35 | 5281 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5282 | int ret = 0; |
5283 | ||
4f26bda1 | 5284 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false); |
62755e35 JA |
5285 | switch (cancel_ret) { |
5286 | case IO_WQ_CANCEL_OK: | |
5287 | ret = 0; | |
5288 | break; | |
5289 | case IO_WQ_CANCEL_RUNNING: | |
5290 | ret = -EALREADY; | |
5291 | break; | |
5292 | case IO_WQ_CANCEL_NOTFOUND: | |
5293 | ret = -ENOENT; | |
5294 | break; | |
5295 | } | |
5296 | ||
e977d6d3 JA |
5297 | return ret; |
5298 | } | |
5299 | ||
47f46768 JA |
5300 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
5301 | struct io_kiocb *req, __u64 sqe_addr, | |
014db007 | 5302 | int success_ret) |
47f46768 JA |
5303 | { |
5304 | unsigned long flags; | |
5305 | int ret; | |
5306 | ||
5307 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); | |
5308 | if (ret != -ENOENT) { | |
5309 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5310 | goto done; | |
5311 | } | |
5312 | ||
5313 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5314 | ret = io_timeout_cancel(ctx, sqe_addr); | |
5315 | if (ret != -ENOENT) | |
5316 | goto done; | |
5317 | ret = io_poll_cancel(ctx, sqe_addr); | |
5318 | done: | |
b0dd8a41 JA |
5319 | if (!ret) |
5320 | ret = success_ret; | |
47f46768 JA |
5321 | io_cqring_fill_event(req, ret); |
5322 | io_commit_cqring(ctx); | |
5323 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5324 | io_cqring_ev_posted(ctx); | |
5325 | ||
4e88d6e7 JA |
5326 | if (ret < 0) |
5327 | req_set_fail_links(req); | |
014db007 | 5328 | io_put_req(req); |
47f46768 JA |
5329 | } |
5330 | ||
3529d8c2 JA |
5331 | static int io_async_cancel_prep(struct io_kiocb *req, |
5332 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 5333 | { |
fbf23849 | 5334 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 5335 | return -EINVAL; |
61710e43 DA |
5336 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5337 | return -EINVAL; | |
5338 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) | |
e977d6d3 JA |
5339 | return -EINVAL; |
5340 | ||
fbf23849 JA |
5341 | req->cancel.addr = READ_ONCE(sqe->addr); |
5342 | return 0; | |
5343 | } | |
5344 | ||
014db007 | 5345 | static int io_async_cancel(struct io_kiocb *req) |
fbf23849 JA |
5346 | { |
5347 | struct io_ring_ctx *ctx = req->ctx; | |
fbf23849 | 5348 | |
014db007 | 5349 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
5262f567 JA |
5350 | return 0; |
5351 | } | |
5352 | ||
05f3fb3c JA |
5353 | static int io_files_update_prep(struct io_kiocb *req, |
5354 | const struct io_uring_sqe *sqe) | |
5355 | { | |
61710e43 DA |
5356 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5357 | return -EINVAL; | |
5358 | if (sqe->ioprio || sqe->rw_flags) | |
05f3fb3c JA |
5359 | return -EINVAL; |
5360 | ||
5361 | req->files_update.offset = READ_ONCE(sqe->off); | |
5362 | req->files_update.nr_args = READ_ONCE(sqe->len); | |
5363 | if (!req->files_update.nr_args) | |
5364 | return -EINVAL; | |
5365 | req->files_update.arg = READ_ONCE(sqe->addr); | |
5366 | return 0; | |
5367 | } | |
5368 | ||
229a7b63 JA |
5369 | static int io_files_update(struct io_kiocb *req, bool force_nonblock, |
5370 | struct io_comp_state *cs) | |
fbf23849 JA |
5371 | { |
5372 | struct io_ring_ctx *ctx = req->ctx; | |
05f3fb3c JA |
5373 | struct io_uring_files_update up; |
5374 | int ret; | |
fbf23849 | 5375 | |
f86cd20c | 5376 | if (force_nonblock) |
05f3fb3c | 5377 | return -EAGAIN; |
05f3fb3c JA |
5378 | |
5379 | up.offset = req->files_update.offset; | |
5380 | up.fds = req->files_update.arg; | |
5381 | ||
5382 | mutex_lock(&ctx->uring_lock); | |
5383 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); | |
5384 | mutex_unlock(&ctx->uring_lock); | |
5385 | ||
5386 | if (ret < 0) | |
5387 | req_set_fail_links(req); | |
229a7b63 | 5388 | __io_req_complete(req, ret, 0, cs); |
5262f567 JA |
5389 | return 0; |
5390 | } | |
5391 | ||
3529d8c2 JA |
5392 | static int io_req_defer_prep(struct io_kiocb *req, |
5393 | const struct io_uring_sqe *sqe) | |
f67676d1 | 5394 | { |
e781573e | 5395 | ssize_t ret = 0; |
f67676d1 | 5396 | |
f1d96a8f PB |
5397 | if (!sqe) |
5398 | return 0; | |
5399 | ||
327d6d96 PB |
5400 | if (io_alloc_async_ctx(req)) |
5401 | return -EAGAIN; | |
f56040b8 PB |
5402 | ret = io_prep_work_files(req); |
5403 | if (unlikely(ret)) | |
5404 | return ret; | |
cccf0ee8 | 5405 | |
d625c6ee | 5406 | switch (req->opcode) { |
e781573e JA |
5407 | case IORING_OP_NOP: |
5408 | break; | |
f67676d1 JA |
5409 | case IORING_OP_READV: |
5410 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 5411 | case IORING_OP_READ: |
3529d8c2 | 5412 | ret = io_read_prep(req, sqe, true); |
f67676d1 JA |
5413 | break; |
5414 | case IORING_OP_WRITEV: | |
5415 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 5416 | case IORING_OP_WRITE: |
3529d8c2 | 5417 | ret = io_write_prep(req, sqe, true); |
f67676d1 | 5418 | break; |
0969e783 | 5419 | case IORING_OP_POLL_ADD: |
3529d8c2 | 5420 | ret = io_poll_add_prep(req, sqe); |
0969e783 JA |
5421 | break; |
5422 | case IORING_OP_POLL_REMOVE: | |
3529d8c2 | 5423 | ret = io_poll_remove_prep(req, sqe); |
0969e783 | 5424 | break; |
8ed8d3c3 | 5425 | case IORING_OP_FSYNC: |
3529d8c2 | 5426 | ret = io_prep_fsync(req, sqe); |
8ed8d3c3 JA |
5427 | break; |
5428 | case IORING_OP_SYNC_FILE_RANGE: | |
3529d8c2 | 5429 | ret = io_prep_sfr(req, sqe); |
8ed8d3c3 | 5430 | break; |
03b1230c | 5431 | case IORING_OP_SENDMSG: |
fddaface | 5432 | case IORING_OP_SEND: |
3529d8c2 | 5433 | ret = io_sendmsg_prep(req, sqe); |
03b1230c JA |
5434 | break; |
5435 | case IORING_OP_RECVMSG: | |
fddaface | 5436 | case IORING_OP_RECV: |
3529d8c2 | 5437 | ret = io_recvmsg_prep(req, sqe); |
03b1230c | 5438 | break; |
f499a021 | 5439 | case IORING_OP_CONNECT: |
3529d8c2 | 5440 | ret = io_connect_prep(req, sqe); |
f499a021 | 5441 | break; |
2d28390a | 5442 | case IORING_OP_TIMEOUT: |
3529d8c2 | 5443 | ret = io_timeout_prep(req, sqe, false); |
b7bb4f7d | 5444 | break; |
b29472ee | 5445 | case IORING_OP_TIMEOUT_REMOVE: |
3529d8c2 | 5446 | ret = io_timeout_remove_prep(req, sqe); |
b29472ee | 5447 | break; |
fbf23849 | 5448 | case IORING_OP_ASYNC_CANCEL: |
3529d8c2 | 5449 | ret = io_async_cancel_prep(req, sqe); |
fbf23849 | 5450 | break; |
2d28390a | 5451 | case IORING_OP_LINK_TIMEOUT: |
3529d8c2 | 5452 | ret = io_timeout_prep(req, sqe, true); |
b7bb4f7d | 5453 | break; |
8ed8d3c3 | 5454 | case IORING_OP_ACCEPT: |
3529d8c2 | 5455 | ret = io_accept_prep(req, sqe); |
8ed8d3c3 | 5456 | break; |
d63d1b5e JA |
5457 | case IORING_OP_FALLOCATE: |
5458 | ret = io_fallocate_prep(req, sqe); | |
5459 | break; | |
15b71abe JA |
5460 | case IORING_OP_OPENAT: |
5461 | ret = io_openat_prep(req, sqe); | |
5462 | break; | |
b5dba59e JA |
5463 | case IORING_OP_CLOSE: |
5464 | ret = io_close_prep(req, sqe); | |
5465 | break; | |
05f3fb3c JA |
5466 | case IORING_OP_FILES_UPDATE: |
5467 | ret = io_files_update_prep(req, sqe); | |
5468 | break; | |
eddc7ef5 JA |
5469 | case IORING_OP_STATX: |
5470 | ret = io_statx_prep(req, sqe); | |
5471 | break; | |
4840e418 JA |
5472 | case IORING_OP_FADVISE: |
5473 | ret = io_fadvise_prep(req, sqe); | |
5474 | break; | |
c1ca757b JA |
5475 | case IORING_OP_MADVISE: |
5476 | ret = io_madvise_prep(req, sqe); | |
5477 | break; | |
cebdb986 JA |
5478 | case IORING_OP_OPENAT2: |
5479 | ret = io_openat2_prep(req, sqe); | |
5480 | break; | |
3e4827b0 JA |
5481 | case IORING_OP_EPOLL_CTL: |
5482 | ret = io_epoll_ctl_prep(req, sqe); | |
5483 | break; | |
7d67af2c PB |
5484 | case IORING_OP_SPLICE: |
5485 | ret = io_splice_prep(req, sqe); | |
5486 | break; | |
ddf0322d JA |
5487 | case IORING_OP_PROVIDE_BUFFERS: |
5488 | ret = io_provide_buffers_prep(req, sqe); | |
5489 | break; | |
067524e9 JA |
5490 | case IORING_OP_REMOVE_BUFFERS: |
5491 | ret = io_remove_buffers_prep(req, sqe); | |
5492 | break; | |
f2a8d5c7 PB |
5493 | case IORING_OP_TEE: |
5494 | ret = io_tee_prep(req, sqe); | |
5495 | break; | |
f67676d1 | 5496 | default: |
e781573e JA |
5497 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
5498 | req->opcode); | |
5499 | ret = -EINVAL; | |
b7bb4f7d | 5500 | break; |
f67676d1 JA |
5501 | } |
5502 | ||
b7bb4f7d | 5503 | return ret; |
f67676d1 JA |
5504 | } |
5505 | ||
9cf7c104 PB |
5506 | static u32 io_get_sequence(struct io_kiocb *req) |
5507 | { | |
5508 | struct io_kiocb *pos; | |
5509 | struct io_ring_ctx *ctx = req->ctx; | |
5510 | u32 total_submitted, nr_reqs = 1; | |
5511 | ||
5512 | if (req->flags & REQ_F_LINK_HEAD) | |
5513 | list_for_each_entry(pos, &req->link_list, link_list) | |
5514 | nr_reqs++; | |
5515 | ||
5516 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; | |
5517 | return total_submitted - nr_reqs; | |
5518 | } | |
5519 | ||
3529d8c2 | 5520 | static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
de0617e4 | 5521 | { |
a197f664 | 5522 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 5523 | struct io_defer_entry *de; |
f67676d1 | 5524 | int ret; |
9cf7c104 | 5525 | u32 seq; |
de0617e4 | 5526 | |
9d858b21 | 5527 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 PB |
5528 | if (likely(list_empty_careful(&ctx->defer_list) && |
5529 | !(req->flags & REQ_F_IO_DRAIN))) | |
5530 | return 0; | |
5531 | ||
5532 | seq = io_get_sequence(req); | |
5533 | /* Still a chance to pass the sequence check */ | |
5534 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
de0617e4 JA |
5535 | return 0; |
5536 | ||
650b5481 | 5537 | if (!req->io) { |
650b5481 | 5538 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 5539 | if (ret) |
650b5481 PB |
5540 | return ret; |
5541 | } | |
cbdcb435 | 5542 | io_prep_async_link(req); |
27dc8338 PB |
5543 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
5544 | if (!de) | |
5545 | return -ENOMEM; | |
2d28390a | 5546 | |
de0617e4 | 5547 | spin_lock_irq(&ctx->completion_lock); |
9cf7c104 | 5548 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
de0617e4 | 5549 | spin_unlock_irq(&ctx->completion_lock); |
27dc8338 | 5550 | kfree(de); |
ae34817b PB |
5551 | io_queue_async_work(req); |
5552 | return -EIOCBQUEUED; | |
de0617e4 JA |
5553 | } |
5554 | ||
915967f6 | 5555 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 5556 | de->req = req; |
9cf7c104 | 5557 | de->seq = seq; |
27dc8338 | 5558 | list_add_tail(&de->list, &ctx->defer_list); |
de0617e4 JA |
5559 | spin_unlock_irq(&ctx->completion_lock); |
5560 | return -EIOCBQUEUED; | |
5561 | } | |
5562 | ||
3ca405eb | 5563 | static void __io_clean_op(struct io_kiocb *req) |
99bc4c38 PB |
5564 | { |
5565 | struct io_async_ctx *io = req->io; | |
5566 | ||
0e1b6fe3 PB |
5567 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
5568 | switch (req->opcode) { | |
5569 | case IORING_OP_READV: | |
5570 | case IORING_OP_READ_FIXED: | |
5571 | case IORING_OP_READ: | |
bcda7baa | 5572 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
5573 | break; |
5574 | case IORING_OP_RECVMSG: | |
5575 | case IORING_OP_RECV: | |
bcda7baa | 5576 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
5577 | break; |
5578 | } | |
5579 | req->flags &= ~REQ_F_BUFFER_SELECTED; | |
99bc4c38 PB |
5580 | } |
5581 | ||
0e1b6fe3 PB |
5582 | if (req->flags & REQ_F_NEED_CLEANUP) { |
5583 | switch (req->opcode) { | |
5584 | case IORING_OP_READV: | |
5585 | case IORING_OP_READ_FIXED: | |
5586 | case IORING_OP_READ: | |
5587 | case IORING_OP_WRITEV: | |
5588 | case IORING_OP_WRITE_FIXED: | |
5589 | case IORING_OP_WRITE: | |
ff6165b2 JA |
5590 | if (io->rw.free_iovec) |
5591 | kfree(io->rw.free_iovec); | |
0e1b6fe3 PB |
5592 | break; |
5593 | case IORING_OP_RECVMSG: | |
5594 | case IORING_OP_SENDMSG: | |
5595 | if (io->msg.iov != io->msg.fast_iov) | |
5596 | kfree(io->msg.iov); | |
5597 | break; | |
5598 | case IORING_OP_SPLICE: | |
5599 | case IORING_OP_TEE: | |
5600 | io_put_file(req, req->splice.file_in, | |
5601 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); | |
5602 | break; | |
5603 | } | |
5604 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
99bc4c38 | 5605 | } |
99bc4c38 PB |
5606 | } |
5607 | ||
3529d8c2 | 5608 | static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
f13fad7b | 5609 | bool force_nonblock, struct io_comp_state *cs) |
2b188cc1 | 5610 | { |
a197f664 | 5611 | struct io_ring_ctx *ctx = req->ctx; |
d625c6ee | 5612 | int ret; |
2b188cc1 | 5613 | |
d625c6ee | 5614 | switch (req->opcode) { |
2b188cc1 | 5615 | case IORING_OP_NOP: |
229a7b63 | 5616 | ret = io_nop(req, cs); |
2b188cc1 JA |
5617 | break; |
5618 | case IORING_OP_READV: | |
edafccee | 5619 | case IORING_OP_READ_FIXED: |
3a6820f2 | 5620 | case IORING_OP_READ: |
3529d8c2 JA |
5621 | if (sqe) { |
5622 | ret = io_read_prep(req, sqe, force_nonblock); | |
5623 | if (ret < 0) | |
5624 | break; | |
5625 | } | |
a1d7c393 | 5626 | ret = io_read(req, force_nonblock, cs); |
edafccee | 5627 | break; |
3529d8c2 | 5628 | case IORING_OP_WRITEV: |
edafccee | 5629 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 5630 | case IORING_OP_WRITE: |
3529d8c2 JA |
5631 | if (sqe) { |
5632 | ret = io_write_prep(req, sqe, force_nonblock); | |
5633 | if (ret < 0) | |
5634 | break; | |
5635 | } | |
a1d7c393 | 5636 | ret = io_write(req, force_nonblock, cs); |
2b188cc1 | 5637 | break; |
c992fe29 | 5638 | case IORING_OP_FSYNC: |
3529d8c2 JA |
5639 | if (sqe) { |
5640 | ret = io_prep_fsync(req, sqe); | |
5641 | if (ret < 0) | |
5642 | break; | |
5643 | } | |
014db007 | 5644 | ret = io_fsync(req, force_nonblock); |
c992fe29 | 5645 | break; |
221c5eb2 | 5646 | case IORING_OP_POLL_ADD: |
3529d8c2 JA |
5647 | if (sqe) { |
5648 | ret = io_poll_add_prep(req, sqe); | |
5649 | if (ret) | |
5650 | break; | |
5651 | } | |
014db007 | 5652 | ret = io_poll_add(req); |
221c5eb2 JA |
5653 | break; |
5654 | case IORING_OP_POLL_REMOVE: | |
3529d8c2 JA |
5655 | if (sqe) { |
5656 | ret = io_poll_remove_prep(req, sqe); | |
5657 | if (ret < 0) | |
5658 | break; | |
5659 | } | |
fc4df999 | 5660 | ret = io_poll_remove(req); |
221c5eb2 | 5661 | break; |
5d17b4a4 | 5662 | case IORING_OP_SYNC_FILE_RANGE: |
3529d8c2 JA |
5663 | if (sqe) { |
5664 | ret = io_prep_sfr(req, sqe); | |
5665 | if (ret < 0) | |
5666 | break; | |
5667 | } | |
014db007 | 5668 | ret = io_sync_file_range(req, force_nonblock); |
5d17b4a4 | 5669 | break; |
0fa03c62 | 5670 | case IORING_OP_SENDMSG: |
fddaface | 5671 | case IORING_OP_SEND: |
3529d8c2 JA |
5672 | if (sqe) { |
5673 | ret = io_sendmsg_prep(req, sqe); | |
5674 | if (ret < 0) | |
5675 | break; | |
5676 | } | |
fddaface | 5677 | if (req->opcode == IORING_OP_SENDMSG) |
229a7b63 | 5678 | ret = io_sendmsg(req, force_nonblock, cs); |
fddaface | 5679 | else |
229a7b63 | 5680 | ret = io_send(req, force_nonblock, cs); |
0fa03c62 | 5681 | break; |
aa1fa28f | 5682 | case IORING_OP_RECVMSG: |
fddaface | 5683 | case IORING_OP_RECV: |
3529d8c2 JA |
5684 | if (sqe) { |
5685 | ret = io_recvmsg_prep(req, sqe); | |
5686 | if (ret) | |
5687 | break; | |
5688 | } | |
fddaface | 5689 | if (req->opcode == IORING_OP_RECVMSG) |
229a7b63 | 5690 | ret = io_recvmsg(req, force_nonblock, cs); |
fddaface | 5691 | else |
229a7b63 | 5692 | ret = io_recv(req, force_nonblock, cs); |
aa1fa28f | 5693 | break; |
5262f567 | 5694 | case IORING_OP_TIMEOUT: |
3529d8c2 JA |
5695 | if (sqe) { |
5696 | ret = io_timeout_prep(req, sqe, false); | |
5697 | if (ret) | |
5698 | break; | |
5699 | } | |
fc4df999 | 5700 | ret = io_timeout(req); |
5262f567 | 5701 | break; |
11365043 | 5702 | case IORING_OP_TIMEOUT_REMOVE: |
3529d8c2 JA |
5703 | if (sqe) { |
5704 | ret = io_timeout_remove_prep(req, sqe); | |
5705 | if (ret) | |
5706 | break; | |
5707 | } | |
fc4df999 | 5708 | ret = io_timeout_remove(req); |
11365043 | 5709 | break; |
17f2fe35 | 5710 | case IORING_OP_ACCEPT: |
3529d8c2 JA |
5711 | if (sqe) { |
5712 | ret = io_accept_prep(req, sqe); | |
5713 | if (ret) | |
5714 | break; | |
5715 | } | |
229a7b63 | 5716 | ret = io_accept(req, force_nonblock, cs); |
17f2fe35 | 5717 | break; |
f8e85cf2 | 5718 | case IORING_OP_CONNECT: |
3529d8c2 JA |
5719 | if (sqe) { |
5720 | ret = io_connect_prep(req, sqe); | |
5721 | if (ret) | |
5722 | break; | |
5723 | } | |
229a7b63 | 5724 | ret = io_connect(req, force_nonblock, cs); |
f8e85cf2 | 5725 | break; |
62755e35 | 5726 | case IORING_OP_ASYNC_CANCEL: |
3529d8c2 JA |
5727 | if (sqe) { |
5728 | ret = io_async_cancel_prep(req, sqe); | |
5729 | if (ret) | |
5730 | break; | |
5731 | } | |
014db007 | 5732 | ret = io_async_cancel(req); |
62755e35 | 5733 | break; |
d63d1b5e JA |
5734 | case IORING_OP_FALLOCATE: |
5735 | if (sqe) { | |
5736 | ret = io_fallocate_prep(req, sqe); | |
5737 | if (ret) | |
5738 | break; | |
5739 | } | |
014db007 | 5740 | ret = io_fallocate(req, force_nonblock); |
d63d1b5e | 5741 | break; |
15b71abe JA |
5742 | case IORING_OP_OPENAT: |
5743 | if (sqe) { | |
5744 | ret = io_openat_prep(req, sqe); | |
5745 | if (ret) | |
5746 | break; | |
5747 | } | |
014db007 | 5748 | ret = io_openat(req, force_nonblock); |
15b71abe | 5749 | break; |
b5dba59e JA |
5750 | case IORING_OP_CLOSE: |
5751 | if (sqe) { | |
5752 | ret = io_close_prep(req, sqe); | |
5753 | if (ret) | |
5754 | break; | |
5755 | } | |
229a7b63 | 5756 | ret = io_close(req, force_nonblock, cs); |
b5dba59e | 5757 | break; |
05f3fb3c JA |
5758 | case IORING_OP_FILES_UPDATE: |
5759 | if (sqe) { | |
5760 | ret = io_files_update_prep(req, sqe); | |
5761 | if (ret) | |
5762 | break; | |
5763 | } | |
229a7b63 | 5764 | ret = io_files_update(req, force_nonblock, cs); |
05f3fb3c | 5765 | break; |
eddc7ef5 JA |
5766 | case IORING_OP_STATX: |
5767 | if (sqe) { | |
5768 | ret = io_statx_prep(req, sqe); | |
5769 | if (ret) | |
5770 | break; | |
5771 | } | |
014db007 | 5772 | ret = io_statx(req, force_nonblock); |
eddc7ef5 | 5773 | break; |
4840e418 JA |
5774 | case IORING_OP_FADVISE: |
5775 | if (sqe) { | |
5776 | ret = io_fadvise_prep(req, sqe); | |
5777 | if (ret) | |
5778 | break; | |
5779 | } | |
014db007 | 5780 | ret = io_fadvise(req, force_nonblock); |
4840e418 | 5781 | break; |
c1ca757b JA |
5782 | case IORING_OP_MADVISE: |
5783 | if (sqe) { | |
5784 | ret = io_madvise_prep(req, sqe); | |
5785 | if (ret) | |
5786 | break; | |
5787 | } | |
014db007 | 5788 | ret = io_madvise(req, force_nonblock); |
c1ca757b | 5789 | break; |
cebdb986 JA |
5790 | case IORING_OP_OPENAT2: |
5791 | if (sqe) { | |
5792 | ret = io_openat2_prep(req, sqe); | |
5793 | if (ret) | |
5794 | break; | |
5795 | } | |
014db007 | 5796 | ret = io_openat2(req, force_nonblock); |
cebdb986 | 5797 | break; |
3e4827b0 JA |
5798 | case IORING_OP_EPOLL_CTL: |
5799 | if (sqe) { | |
5800 | ret = io_epoll_ctl_prep(req, sqe); | |
5801 | if (ret) | |
5802 | break; | |
5803 | } | |
229a7b63 | 5804 | ret = io_epoll_ctl(req, force_nonblock, cs); |
3e4827b0 | 5805 | break; |
7d67af2c PB |
5806 | case IORING_OP_SPLICE: |
5807 | if (sqe) { | |
5808 | ret = io_splice_prep(req, sqe); | |
5809 | if (ret < 0) | |
5810 | break; | |
5811 | } | |
014db007 | 5812 | ret = io_splice(req, force_nonblock); |
7d67af2c | 5813 | break; |
ddf0322d JA |
5814 | case IORING_OP_PROVIDE_BUFFERS: |
5815 | if (sqe) { | |
5816 | ret = io_provide_buffers_prep(req, sqe); | |
5817 | if (ret) | |
5818 | break; | |
5819 | } | |
229a7b63 | 5820 | ret = io_provide_buffers(req, force_nonblock, cs); |
ddf0322d | 5821 | break; |
067524e9 JA |
5822 | case IORING_OP_REMOVE_BUFFERS: |
5823 | if (sqe) { | |
5824 | ret = io_remove_buffers_prep(req, sqe); | |
5825 | if (ret) | |
5826 | break; | |
5827 | } | |
229a7b63 | 5828 | ret = io_remove_buffers(req, force_nonblock, cs); |
3e4827b0 | 5829 | break; |
f2a8d5c7 PB |
5830 | case IORING_OP_TEE: |
5831 | if (sqe) { | |
5832 | ret = io_tee_prep(req, sqe); | |
5833 | if (ret < 0) | |
5834 | break; | |
5835 | } | |
5836 | ret = io_tee(req, force_nonblock); | |
5837 | break; | |
2b188cc1 JA |
5838 | default: |
5839 | ret = -EINVAL; | |
5840 | break; | |
5841 | } | |
5842 | ||
def596e9 JA |
5843 | if (ret) |
5844 | return ret; | |
5845 | ||
b532576e JA |
5846 | /* If the op doesn't have a file, we're not polling for it */ |
5847 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { | |
11ba820b JA |
5848 | const bool in_async = io_wq_current_is_worker(); |
5849 | ||
11ba820b JA |
5850 | /* workqueue context doesn't hold uring_lock, grab it now */ |
5851 | if (in_async) | |
5852 | mutex_lock(&ctx->uring_lock); | |
5853 | ||
def596e9 | 5854 | io_iopoll_req_issued(req); |
11ba820b JA |
5855 | |
5856 | if (in_async) | |
5857 | mutex_unlock(&ctx->uring_lock); | |
def596e9 JA |
5858 | } |
5859 | ||
5860 | return 0; | |
2b188cc1 JA |
5861 | } |
5862 | ||
f4db7182 | 5863 | static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
5864 | { |
5865 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 5866 | struct io_kiocb *timeout; |
561fb04a | 5867 | int ret = 0; |
2b188cc1 | 5868 | |
6df1db6b PB |
5869 | timeout = io_prep_linked_timeout(req); |
5870 | if (timeout) | |
5871 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 5872 | |
0c9d5ccd JA |
5873 | /* if NO_CANCEL is set, we must still run the work */ |
5874 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == | |
5875 | IO_WQ_WORK_CANCEL) { | |
561fb04a | 5876 | ret = -ECANCELED; |
0c9d5ccd | 5877 | } |
31b51510 | 5878 | |
561fb04a | 5879 | if (!ret) { |
561fb04a | 5880 | do { |
f13fad7b | 5881 | ret = io_issue_sqe(req, NULL, false, NULL); |
561fb04a JA |
5882 | /* |
5883 | * We can get EAGAIN for polled IO even though we're | |
5884 | * forcing a sync submission from here, since we can't | |
5885 | * wait for request slots on the block side. | |
5886 | */ | |
5887 | if (ret != -EAGAIN) | |
5888 | break; | |
5889 | cond_resched(); | |
5890 | } while (1); | |
5891 | } | |
31b51510 | 5892 | |
561fb04a | 5893 | if (ret) { |
4e88d6e7 | 5894 | req_set_fail_links(req); |
e1e16097 | 5895 | io_req_complete(req, ret); |
edafccee | 5896 | } |
2b188cc1 | 5897 | |
f4db7182 | 5898 | return io_steal_work(req); |
2b188cc1 JA |
5899 | } |
5900 | ||
65e19f54 JA |
5901 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
5902 | int index) | |
5903 | { | |
5904 | struct fixed_file_table *table; | |
5905 | ||
05f3fb3c | 5906 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
84695089 | 5907 | return table->files[index & IORING_FILE_TABLE_MASK]; |
65e19f54 JA |
5908 | } |
5909 | ||
8da11c19 PB |
5910 | static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, |
5911 | int fd, struct file **out_file, bool fixed) | |
09bb8394 | 5912 | { |
a197f664 | 5913 | struct io_ring_ctx *ctx = req->ctx; |
8da11c19 | 5914 | struct file *file; |
09bb8394 | 5915 | |
8da11c19 | 5916 | if (fixed) { |
05f3fb3c | 5917 | if (unlikely(!ctx->file_data || |
09bb8394 JA |
5918 | (unsigned) fd >= ctx->nr_user_files)) |
5919 | return -EBADF; | |
b7620121 | 5920 | fd = array_index_nospec(fd, ctx->nr_user_files); |
8da11c19 | 5921 | file = io_file_from_index(ctx, fd); |
fd2206e4 JA |
5922 | if (file) { |
5923 | req->fixed_file_refs = ctx->file_data->cur_refs; | |
5924 | percpu_ref_get(req->fixed_file_refs); | |
5925 | } | |
09bb8394 | 5926 | } else { |
c826bd7a | 5927 | trace_io_uring_file_get(ctx, fd); |
8da11c19 | 5928 | file = __io_file_get(state, fd); |
09bb8394 JA |
5929 | } |
5930 | ||
fd2206e4 JA |
5931 | if (file || io_op_defs[req->opcode].needs_file_no_error) { |
5932 | *out_file = file; | |
5933 | return 0; | |
5934 | } | |
5935 | return -EBADF; | |
09bb8394 JA |
5936 | } |
5937 | ||
8da11c19 | 5938 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
63ff8223 | 5939 | int fd) |
8da11c19 | 5940 | { |
8da11c19 PB |
5941 | bool fixed; |
5942 | ||
63ff8223 | 5943 | fixed = (req->flags & REQ_F_FIXED_FILE) != 0; |
0cdaf760 | 5944 | if (unlikely(!fixed && io_async_submit(req->ctx))) |
8da11c19 PB |
5945 | return -EBADF; |
5946 | ||
5947 | return io_file_get(state, req, fd, &req->file, fixed); | |
5948 | } | |
5949 | ||
a197f664 | 5950 | static int io_grab_files(struct io_kiocb *req) |
fcb323cc JA |
5951 | { |
5952 | int ret = -EBADF; | |
a197f664 | 5953 | struct io_ring_ctx *ctx = req->ctx; |
fcb323cc | 5954 | |
f56040b8 PB |
5955 | io_req_init_async(req); |
5956 | ||
5b0bbee4 | 5957 | if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE)) |
f86cd20c | 5958 | return 0; |
b14cca0c | 5959 | if (!ctx->ring_file) |
b5dba59e JA |
5960 | return -EBADF; |
5961 | ||
fcb323cc JA |
5962 | rcu_read_lock(); |
5963 | spin_lock_irq(&ctx->inflight_lock); | |
5964 | /* | |
5965 | * We use the f_ops->flush() handler to ensure that we can flush | |
5966 | * out work accessing these files if the fd is closed. Check if | |
5967 | * the fd has changed since we started down this path, and disallow | |
5968 | * this operation if it has. | |
5969 | */ | |
b14cca0c | 5970 | if (fcheck(ctx->ring_fd) == ctx->ring_file) { |
fcb323cc JA |
5971 | list_add(&req->inflight_entry, &ctx->inflight_list); |
5972 | req->flags |= REQ_F_INFLIGHT; | |
5973 | req->work.files = current->files; | |
5974 | ret = 0; | |
5975 | } | |
5976 | spin_unlock_irq(&ctx->inflight_lock); | |
5977 | rcu_read_unlock(); | |
5978 | ||
5979 | return ret; | |
5980 | } | |
5981 | ||
f56040b8 PB |
5982 | static inline int io_prep_work_files(struct io_kiocb *req) |
5983 | { | |
5984 | if (!io_op_defs[req->opcode].file_table) | |
5985 | return 0; | |
5986 | return io_grab_files(req); | |
5987 | } | |
5988 | ||
2665abfd | 5989 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 5990 | { |
ad8a48ac JA |
5991 | struct io_timeout_data *data = container_of(timer, |
5992 | struct io_timeout_data, timer); | |
5993 | struct io_kiocb *req = data->req; | |
2665abfd JA |
5994 | struct io_ring_ctx *ctx = req->ctx; |
5995 | struct io_kiocb *prev = NULL; | |
5996 | unsigned long flags; | |
2665abfd JA |
5997 | |
5998 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5999 | ||
6000 | /* | |
6001 | * We don't expect the list to be empty, that will only happen if we | |
6002 | * race with the completion of the linked work. | |
6003 | */ | |
4493233e PB |
6004 | if (!list_empty(&req->link_list)) { |
6005 | prev = list_entry(req->link_list.prev, struct io_kiocb, | |
6006 | link_list); | |
5d960724 | 6007 | if (refcount_inc_not_zero(&prev->refs)) { |
4493233e | 6008 | list_del_init(&req->link_list); |
5d960724 JA |
6009 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
6010 | } else | |
76a46e06 | 6011 | prev = NULL; |
2665abfd JA |
6012 | } |
6013 | ||
6014 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
6015 | ||
6016 | if (prev) { | |
4e88d6e7 | 6017 | req_set_fail_links(prev); |
014db007 | 6018 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
76a46e06 | 6019 | io_put_req(prev); |
47f46768 | 6020 | } else { |
e1e16097 | 6021 | io_req_complete(req, -ETIME); |
2665abfd | 6022 | } |
2665abfd JA |
6023 | return HRTIMER_NORESTART; |
6024 | } | |
6025 | ||
7271ef3a | 6026 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6027 | { |
76a46e06 JA |
6028 | /* |
6029 | * If the list is now empty, then our linked request finished before | |
6030 | * we got a chance to setup the timer | |
6031 | */ | |
4493233e | 6032 | if (!list_empty(&req->link_list)) { |
2d28390a | 6033 | struct io_timeout_data *data = &req->io->timeout; |
94ae5e77 | 6034 | |
ad8a48ac JA |
6035 | data->timer.function = io_link_timeout_fn; |
6036 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6037 | data->mode); | |
2665abfd | 6038 | } |
7271ef3a JA |
6039 | } |
6040 | ||
6041 | static void io_queue_linked_timeout(struct io_kiocb *req) | |
6042 | { | |
6043 | struct io_ring_ctx *ctx = req->ctx; | |
6044 | ||
6045 | spin_lock_irq(&ctx->completion_lock); | |
6046 | __io_queue_linked_timeout(req); | |
76a46e06 | 6047 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 6048 | |
2665abfd | 6049 | /* drop submission reference */ |
76a46e06 JA |
6050 | io_put_req(req); |
6051 | } | |
2665abfd | 6052 | |
ad8a48ac | 6053 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd JA |
6054 | { |
6055 | struct io_kiocb *nxt; | |
6056 | ||
dea3b49c | 6057 | if (!(req->flags & REQ_F_LINK_HEAD)) |
2665abfd | 6058 | return NULL; |
6df1db6b | 6059 | if (req->flags & REQ_F_LINK_TIMEOUT) |
d7718a9d | 6060 | return NULL; |
2665abfd | 6061 | |
4493233e PB |
6062 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
6063 | link_list); | |
d625c6ee | 6064 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
76a46e06 | 6065 | return NULL; |
2665abfd | 6066 | |
76a46e06 | 6067 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 6068 | return nxt; |
2665abfd JA |
6069 | } |
6070 | ||
f13fad7b JA |
6071 | static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
6072 | struct io_comp_state *cs) | |
2b188cc1 | 6073 | { |
4a0a7a18 | 6074 | struct io_kiocb *linked_timeout; |
4bc4494e | 6075 | struct io_kiocb *nxt; |
193155c8 | 6076 | const struct cred *old_creds = NULL; |
e0c5c576 | 6077 | int ret; |
2b188cc1 | 6078 | |
4a0a7a18 JA |
6079 | again: |
6080 | linked_timeout = io_prep_linked_timeout(req); | |
6081 | ||
7cdaf587 XW |
6082 | if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds && |
6083 | req->work.creds != current_cred()) { | |
193155c8 JA |
6084 | if (old_creds) |
6085 | revert_creds(old_creds); | |
6086 | if (old_creds == req->work.creds) | |
6087 | old_creds = NULL; /* restored original creds */ | |
6088 | else | |
6089 | old_creds = override_creds(req->work.creds); | |
6090 | } | |
6091 | ||
f13fad7b | 6092 | ret = io_issue_sqe(req, sqe, true, cs); |
491381ce JA |
6093 | |
6094 | /* | |
6095 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6096 | * doesn't support non-blocking read/write attempts | |
6097 | */ | |
24c74678 | 6098 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
f063c547 | 6099 | if (!io_arm_poll_handler(req)) { |
86a761f8 | 6100 | punt: |
f063c547 PB |
6101 | ret = io_prep_work_files(req); |
6102 | if (unlikely(ret)) | |
bbad27b2 | 6103 | goto err; |
f063c547 PB |
6104 | /* |
6105 | * Queued up for async execution, worker will release | |
6106 | * submit reference when the iocb is actually submitted. | |
6107 | */ | |
6108 | io_queue_async_work(req); | |
2b188cc1 | 6109 | } |
bbad27b2 | 6110 | |
f063c547 PB |
6111 | if (linked_timeout) |
6112 | io_queue_linked_timeout(linked_timeout); | |
4bc4494e | 6113 | goto exit; |
2b188cc1 | 6114 | } |
e65ef56d | 6115 | |
652532ad | 6116 | if (unlikely(ret)) { |
fcb323cc | 6117 | err: |
652532ad PB |
6118 | /* un-prep timeout, so it'll be killed as any other linked */ |
6119 | req->flags &= ~REQ_F_LINK_TIMEOUT; | |
4e88d6e7 | 6120 | req_set_fail_links(req); |
e65ef56d | 6121 | io_put_req(req); |
e1e16097 | 6122 | io_req_complete(req, ret); |
652532ad | 6123 | goto exit; |
9e645e11 | 6124 | } |
652532ad PB |
6125 | |
6126 | /* drop submission reference */ | |
6127 | nxt = io_put_req_find_next(req); | |
6128 | if (linked_timeout) | |
6129 | io_queue_linked_timeout(linked_timeout); | |
6130 | ||
4a0a7a18 JA |
6131 | if (nxt) { |
6132 | req = nxt; | |
86a761f8 PB |
6133 | |
6134 | if (req->flags & REQ_F_FORCE_ASYNC) | |
6135 | goto punt; | |
4a0a7a18 JA |
6136 | goto again; |
6137 | } | |
4bc4494e | 6138 | exit: |
193155c8 JA |
6139 | if (old_creds) |
6140 | revert_creds(old_creds); | |
2b188cc1 JA |
6141 | } |
6142 | ||
f13fad7b JA |
6143 | static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
6144 | struct io_comp_state *cs) | |
4fe2c963 JL |
6145 | { |
6146 | int ret; | |
6147 | ||
3529d8c2 | 6148 | ret = io_req_defer(req, sqe); |
4fe2c963 JL |
6149 | if (ret) { |
6150 | if (ret != -EIOCBQUEUED) { | |
1118591a | 6151 | fail_req: |
4e88d6e7 | 6152 | req_set_fail_links(req); |
e1e16097 JA |
6153 | io_put_req(req); |
6154 | io_req_complete(req, ret); | |
4fe2c963 | 6155 | } |
2550878f | 6156 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
bd2ab18a | 6157 | if (!req->io) { |
bd2ab18a | 6158 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6159 | if (unlikely(ret)) |
bd2ab18a PB |
6160 | goto fail_req; |
6161 | } | |
6162 | ||
ce35a47a JA |
6163 | /* |
6164 | * Never try inline submit of IOSQE_ASYNC is set, go straight | |
6165 | * to async execution. | |
6166 | */ | |
3e863ea3 | 6167 | io_req_init_async(req); |
ce35a47a JA |
6168 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
6169 | io_queue_async_work(req); | |
6170 | } else { | |
f13fad7b | 6171 | __io_queue_sqe(req, sqe, cs); |
ce35a47a | 6172 | } |
4fe2c963 JL |
6173 | } |
6174 | ||
f13fad7b JA |
6175 | static inline void io_queue_link_head(struct io_kiocb *req, |
6176 | struct io_comp_state *cs) | |
4fe2c963 | 6177 | { |
94ae5e77 | 6178 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
e1e16097 JA |
6179 | io_put_req(req); |
6180 | io_req_complete(req, -ECANCELED); | |
1b4a51b6 | 6181 | } else |
f13fad7b | 6182 | io_queue_sqe(req, NULL, cs); |
4fe2c963 JL |
6183 | } |
6184 | ||
1d4240cc | 6185 | static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
f13fad7b | 6186 | struct io_kiocb **link, struct io_comp_state *cs) |
9e645e11 | 6187 | { |
a197f664 | 6188 | struct io_ring_ctx *ctx = req->ctx; |
ef4ff581 | 6189 | int ret; |
9e645e11 | 6190 | |
9e645e11 JA |
6191 | /* |
6192 | * If we already have a head request, queue this one for async | |
6193 | * submittal once the head completes. If we don't have a head but | |
6194 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6195 | * submitted sync once the chain is complete. If none of those | |
6196 | * conditions are true (normal request), then just queue it. | |
6197 | */ | |
6198 | if (*link) { | |
9d76377f | 6199 | struct io_kiocb *head = *link; |
4e88d6e7 | 6200 | |
8cdf2193 PB |
6201 | /* |
6202 | * Taking sequential execution of a link, draining both sides | |
6203 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all | |
6204 | * requests in the link. So, it drains the head and the | |
6205 | * next after the link request. The last one is done via | |
6206 | * drain_next flag to persist the effect across calls. | |
6207 | */ | |
ef4ff581 | 6208 | if (req->flags & REQ_F_IO_DRAIN) { |
711be031 PB |
6209 | head->flags |= REQ_F_IO_DRAIN; |
6210 | ctx->drain_next = 1; | |
6211 | } | |
3529d8c2 | 6212 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6213 | if (unlikely(ret)) { |
4e88d6e7 | 6214 | /* fail even hard links since we don't submit */ |
9d76377f | 6215 | head->flags |= REQ_F_FAIL_LINK; |
1d4240cc | 6216 | return ret; |
2d28390a | 6217 | } |
9d76377f | 6218 | trace_io_uring_link(ctx, req, head); |
c40f6379 | 6219 | io_get_req_task(req); |
9d76377f | 6220 | list_add_tail(&req->link_list, &head->link_list); |
32fe525b PB |
6221 | |
6222 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6223 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
f13fad7b | 6224 | io_queue_link_head(head, cs); |
32fe525b PB |
6225 | *link = NULL; |
6226 | } | |
9e645e11 | 6227 | } else { |
711be031 PB |
6228 | if (unlikely(ctx->drain_next)) { |
6229 | req->flags |= REQ_F_IO_DRAIN; | |
ef4ff581 | 6230 | ctx->drain_next = 0; |
711be031 | 6231 | } |
ef4ff581 | 6232 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
dea3b49c | 6233 | req->flags |= REQ_F_LINK_HEAD; |
711be031 | 6234 | INIT_LIST_HEAD(&req->link_list); |
f1d96a8f | 6235 | |
711be031 | 6236 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6237 | if (unlikely(ret)) |
711be031 PB |
6238 | req->flags |= REQ_F_FAIL_LINK; |
6239 | *link = req; | |
6240 | } else { | |
f13fad7b | 6241 | io_queue_sqe(req, sqe, cs); |
711be031 | 6242 | } |
9e645e11 | 6243 | } |
2e6e1fde | 6244 | |
1d4240cc | 6245 | return 0; |
9e645e11 JA |
6246 | } |
6247 | ||
9a56a232 JA |
6248 | /* |
6249 | * Batched submission is done, ensure local IO is flushed out. | |
6250 | */ | |
6251 | static void io_submit_state_end(struct io_submit_state *state) | |
6252 | { | |
f13fad7b JA |
6253 | if (!list_empty(&state->comp.list)) |
6254 | io_submit_flush_completions(&state->comp); | |
9a56a232 | 6255 | blk_finish_plug(&state->plug); |
9f13c35b | 6256 | io_state_file_put(state); |
2579f913 | 6257 | if (state->free_reqs) |
6c8a3134 | 6258 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
9a56a232 JA |
6259 | } |
6260 | ||
6261 | /* | |
6262 | * Start submission side cache. | |
6263 | */ | |
6264 | static void io_submit_state_start(struct io_submit_state *state, | |
013538bd | 6265 | struct io_ring_ctx *ctx, unsigned int max_ios) |
9a56a232 JA |
6266 | { |
6267 | blk_start_plug(&state->plug); | |
b63534c4 JA |
6268 | #ifdef CONFIG_BLOCK |
6269 | state->plug.nowait = true; | |
6270 | #endif | |
013538bd JA |
6271 | state->comp.nr = 0; |
6272 | INIT_LIST_HEAD(&state->comp.list); | |
6273 | state->comp.ctx = ctx; | |
2579f913 | 6274 | state->free_reqs = 0; |
9a56a232 JA |
6275 | state->file = NULL; |
6276 | state->ios_left = max_ios; | |
6277 | } | |
6278 | ||
2b188cc1 JA |
6279 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6280 | { | |
75b28aff | 6281 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6282 | |
caf582c6 PB |
6283 | /* |
6284 | * Ensure any loads from the SQEs are done at this point, | |
6285 | * since once we write the new head, the application could | |
6286 | * write new data to them. | |
6287 | */ | |
6288 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6289 | } |
6290 | ||
2b188cc1 | 6291 | /* |
3529d8c2 | 6292 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
2b188cc1 JA |
6293 | * that is mapped by userspace. This means that care needs to be taken to |
6294 | * ensure that reads are stable, as we cannot rely on userspace always | |
6295 | * being a good citizen. If members of the sqe are validated and then later | |
6296 | * used, it's important that those reads are done through READ_ONCE() to | |
6297 | * prevent a re-load down the line. | |
6298 | */ | |
709b302f | 6299 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6300 | { |
75b28aff | 6301 | u32 *sq_array = ctx->sq_array; |
2b188cc1 JA |
6302 | unsigned head; |
6303 | ||
6304 | /* | |
6305 | * The cached sq head (or cq tail) serves two purposes: | |
6306 | * | |
6307 | * 1) allows us to batch the cost of updating the user visible | |
6308 | * head updates. | |
6309 | * 2) allows the kernel side to track the head on its own, even | |
6310 | * though the application is the one updating it. | |
6311 | */ | |
ee7d46d9 | 6312 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
709b302f PB |
6313 | if (likely(head < ctx->sq_entries)) |
6314 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6315 | |
6316 | /* drop invalid entries */ | |
498ccd9e | 6317 | ctx->cached_sq_dropped++; |
ee7d46d9 | 6318 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
709b302f PB |
6319 | return NULL; |
6320 | } | |
6321 | ||
6322 | static inline void io_consume_sqe(struct io_ring_ctx *ctx) | |
6323 | { | |
6324 | ctx->cached_sq_head++; | |
2b188cc1 JA |
6325 | } |
6326 | ||
ef4ff581 PB |
6327 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
6328 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ | |
6329 | IOSQE_BUFFER_SELECT) | |
6330 | ||
6331 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, | |
6332 | const struct io_uring_sqe *sqe, | |
0cdaf760 | 6333 | struct io_submit_state *state) |
0553b8bd | 6334 | { |
ef4ff581 | 6335 | unsigned int sqe_flags; |
63ff8223 | 6336 | int id; |
ef4ff581 | 6337 | |
0553b8bd PB |
6338 | req->opcode = READ_ONCE(sqe->opcode); |
6339 | req->user_data = READ_ONCE(sqe->user_data); | |
6340 | req->io = NULL; | |
6341 | req->file = NULL; | |
6342 | req->ctx = ctx; | |
6343 | req->flags = 0; | |
6344 | /* one is dropped after submission, the other at completion */ | |
6345 | refcount_set(&req->refs, 2); | |
4dd2824d | 6346 | req->task = current; |
0553b8bd | 6347 | req->result = 0; |
ef4ff581 PB |
6348 | |
6349 | if (unlikely(req->opcode >= IORING_OP_LAST)) | |
6350 | return -EINVAL; | |
6351 | ||
9d8426a0 JA |
6352 | if (unlikely(io_sq_thread_acquire_mm(ctx, req))) |
6353 | return -EFAULT; | |
ef4ff581 PB |
6354 | |
6355 | sqe_flags = READ_ONCE(sqe->flags); | |
6356 | /* enforce forwards compatibility on users */ | |
6357 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) | |
6358 | return -EINVAL; | |
6359 | ||
6360 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
6361 | !io_op_defs[req->opcode].buffer_select) | |
6362 | return -EOPNOTSUPP; | |
6363 | ||
6364 | id = READ_ONCE(sqe->personality); | |
6365 | if (id) { | |
7cdaf587 | 6366 | io_req_init_async(req); |
ef4ff581 PB |
6367 | req->work.creds = idr_find(&ctx->personality_idr, id); |
6368 | if (unlikely(!req->work.creds)) | |
6369 | return -EINVAL; | |
6370 | get_cred(req->work.creds); | |
6371 | } | |
6372 | ||
6373 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
c11368a5 | 6374 | req->flags |= sqe_flags; |
ef4ff581 | 6375 | |
63ff8223 JA |
6376 | if (!io_op_defs[req->opcode].needs_file) |
6377 | return 0; | |
6378 | ||
6379 | return io_req_set_file(state, req, READ_ONCE(sqe->fd)); | |
0553b8bd PB |
6380 | } |
6381 | ||
fb5ccc98 | 6382 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr, |
0cdaf760 | 6383 | struct file *ring_file, int ring_fd) |
6c271ce2 | 6384 | { |
ac8691c4 | 6385 | struct io_submit_state state; |
9e645e11 | 6386 | struct io_kiocb *link = NULL; |
9e645e11 | 6387 | int i, submitted = 0; |
6c271ce2 | 6388 | |
c4a2ed72 | 6389 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
ad3eb2c8 JA |
6390 | if (test_bit(0, &ctx->sq_check_overflow)) { |
6391 | if (!list_empty(&ctx->cq_overflow_list) && | |
6392 | !io_cqring_overflow_flush(ctx, false)) | |
6393 | return -EBUSY; | |
6394 | } | |
6c271ce2 | 6395 | |
ee7d46d9 PB |
6396 | /* make sure SQ entry isn't read before tail */ |
6397 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
9ef4f124 | 6398 | |
2b85edfc PB |
6399 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6400 | return -EAGAIN; | |
6c271ce2 | 6401 | |
013538bd | 6402 | io_submit_state_start(&state, ctx, nr); |
6c271ce2 | 6403 | |
b14cca0c PB |
6404 | ctx->ring_fd = ring_fd; |
6405 | ctx->ring_file = ring_file; | |
6406 | ||
6c271ce2 | 6407 | for (i = 0; i < nr; i++) { |
3529d8c2 | 6408 | const struct io_uring_sqe *sqe; |
196be95c | 6409 | struct io_kiocb *req; |
1cb1edb2 | 6410 | int err; |
fb5ccc98 | 6411 | |
b1e50e54 PB |
6412 | sqe = io_get_sqe(ctx); |
6413 | if (unlikely(!sqe)) { | |
6414 | io_consume_sqe(ctx); | |
6415 | break; | |
6416 | } | |
ac8691c4 | 6417 | req = io_alloc_req(ctx, &state); |
196be95c PB |
6418 | if (unlikely(!req)) { |
6419 | if (!submitted) | |
6420 | submitted = -EAGAIN; | |
fb5ccc98 | 6421 | break; |
196be95c | 6422 | } |
fb5ccc98 | 6423 | |
ac8691c4 | 6424 | err = io_init_req(ctx, req, sqe, &state); |
709b302f | 6425 | io_consume_sqe(ctx); |
d3656344 JA |
6426 | /* will complete beyond this point, count as submitted */ |
6427 | submitted++; | |
6428 | ||
ef4ff581 | 6429 | if (unlikely(err)) { |
1cb1edb2 | 6430 | fail_req: |
e1e16097 JA |
6431 | io_put_req(req); |
6432 | io_req_complete(req, err); | |
196be95c PB |
6433 | break; |
6434 | } | |
fb5ccc98 | 6435 | |
354420f7 | 6436 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
0cdaf760 | 6437 | true, io_async_submit(ctx)); |
f13fad7b | 6438 | err = io_submit_sqe(req, sqe, &link, &state.comp); |
1d4240cc PB |
6439 | if (err) |
6440 | goto fail_req; | |
6c271ce2 JA |
6441 | } |
6442 | ||
9466f437 PB |
6443 | if (unlikely(submitted != nr)) { |
6444 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
6445 | ||
6446 | percpu_ref_put_many(&ctx->refs, nr - ref_used); | |
6447 | } | |
9e645e11 | 6448 | if (link) |
f13fad7b | 6449 | io_queue_link_head(link, &state.comp); |
ac8691c4 | 6450 | io_submit_state_end(&state); |
6c271ce2 | 6451 | |
ae9428ca PB |
6452 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6453 | io_commit_sqring(ctx); | |
6454 | ||
6c271ce2 JA |
6455 | return submitted; |
6456 | } | |
6457 | ||
23b3628e XW |
6458 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6459 | { | |
6460 | /* Tell userspace we may need a wakeup call */ | |
6461 | spin_lock_irq(&ctx->completion_lock); | |
6462 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; | |
6463 | spin_unlock_irq(&ctx->completion_lock); | |
6464 | } | |
6465 | ||
6466 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
6467 | { | |
6468 | spin_lock_irq(&ctx->completion_lock); | |
6469 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; | |
6470 | spin_unlock_irq(&ctx->completion_lock); | |
6471 | } | |
6472 | ||
6c271ce2 JA |
6473 | static int io_sq_thread(void *data) |
6474 | { | |
6c271ce2 | 6475 | struct io_ring_ctx *ctx = data; |
181e448d | 6476 | const struct cred *old_cred; |
6c271ce2 | 6477 | DEFINE_WAIT(wait); |
6c271ce2 | 6478 | unsigned long timeout; |
bdcd3eab | 6479 | int ret = 0; |
6c271ce2 | 6480 | |
0f158b4c | 6481 | complete(&ctx->sq_thread_comp); |
a4c0b3de | 6482 | |
181e448d | 6483 | old_cred = override_creds(ctx->creds); |
6c271ce2 | 6484 | |
bdcd3eab | 6485 | timeout = jiffies + ctx->sq_thread_idle; |
2bbcd6d3 | 6486 | while (!kthread_should_park()) { |
fb5ccc98 | 6487 | unsigned int to_submit; |
6c271ce2 | 6488 | |
540e32a0 | 6489 | if (!list_empty(&ctx->iopoll_list)) { |
6c271ce2 JA |
6490 | unsigned nr_events = 0; |
6491 | ||
bdcd3eab | 6492 | mutex_lock(&ctx->uring_lock); |
540e32a0 | 6493 | if (!list_empty(&ctx->iopoll_list) && !need_resched()) |
9dedd563 | 6494 | io_do_iopoll(ctx, &nr_events, 0); |
bdcd3eab | 6495 | else |
6c271ce2 | 6496 | timeout = jiffies + ctx->sq_thread_idle; |
bdcd3eab | 6497 | mutex_unlock(&ctx->uring_lock); |
6c271ce2 JA |
6498 | } |
6499 | ||
fb5ccc98 | 6500 | to_submit = io_sqring_entries(ctx); |
c1edbf5f JA |
6501 | |
6502 | /* | |
6503 | * If submit got -EBUSY, flag us as needing the application | |
6504 | * to enter the kernel to reap and flush events. | |
6505 | */ | |
b772f07a | 6506 | if (!to_submit || ret == -EBUSY || need_resched()) { |
7143b5ac SG |
6507 | /* |
6508 | * Drop cur_mm before scheduling, we can't hold it for | |
6509 | * long periods (or over schedule()). Do this before | |
6510 | * adding ourselves to the waitqueue, as the unuse/drop | |
6511 | * may sleep. | |
6512 | */ | |
4349f30e | 6513 | io_sq_thread_drop_mm(); |
7143b5ac | 6514 | |
6c271ce2 JA |
6515 | /* |
6516 | * We're polling. If we're within the defined idle | |
6517 | * period, then let us spin without work before going | |
c1edbf5f JA |
6518 | * to sleep. The exception is if we got EBUSY doing |
6519 | * more IO, we should wait for the application to | |
6520 | * reap events and wake us up. | |
6c271ce2 | 6521 | */ |
540e32a0 | 6522 | if (!list_empty(&ctx->iopoll_list) || need_resched() || |
df069d80 JA |
6523 | (!time_after(jiffies, timeout) && ret != -EBUSY && |
6524 | !percpu_ref_is_dying(&ctx->refs))) { | |
4c6e277c | 6525 | io_run_task_work(); |
9831a90c | 6526 | cond_resched(); |
6c271ce2 JA |
6527 | continue; |
6528 | } | |
6529 | ||
6c271ce2 JA |
6530 | prepare_to_wait(&ctx->sqo_wait, &wait, |
6531 | TASK_INTERRUPTIBLE); | |
6532 | ||
bdcd3eab XW |
6533 | /* |
6534 | * While doing polled IO, before going to sleep, we need | |
540e32a0 PB |
6535 | * to check if there are new reqs added to iopoll_list, |
6536 | * it is because reqs may have been punted to io worker | |
6537 | * and will be added to iopoll_list later, hence check | |
6538 | * the iopoll_list again. | |
bdcd3eab XW |
6539 | */ |
6540 | if ((ctx->flags & IORING_SETUP_IOPOLL) && | |
540e32a0 | 6541 | !list_empty_careful(&ctx->iopoll_list)) { |
bdcd3eab XW |
6542 | finish_wait(&ctx->sqo_wait, &wait); |
6543 | continue; | |
6544 | } | |
6545 | ||
23b3628e | 6546 | io_ring_set_wakeup_flag(ctx); |
6c271ce2 | 6547 | |
fb5ccc98 | 6548 | to_submit = io_sqring_entries(ctx); |
c1edbf5f | 6549 | if (!to_submit || ret == -EBUSY) { |
2bbcd6d3 | 6550 | if (kthread_should_park()) { |
6c271ce2 JA |
6551 | finish_wait(&ctx->sqo_wait, &wait); |
6552 | break; | |
6553 | } | |
4c6e277c | 6554 | if (io_run_task_work()) { |
10bea96d | 6555 | finish_wait(&ctx->sqo_wait, &wait); |
23b3628e | 6556 | io_ring_clear_wakeup_flag(ctx); |
b41e9852 JA |
6557 | continue; |
6558 | } | |
6c271ce2 JA |
6559 | if (signal_pending(current)) |
6560 | flush_signals(current); | |
6561 | schedule(); | |
6562 | finish_wait(&ctx->sqo_wait, &wait); | |
6563 | ||
23b3628e | 6564 | io_ring_clear_wakeup_flag(ctx); |
d4ae271d | 6565 | ret = 0; |
6c271ce2 JA |
6566 | continue; |
6567 | } | |
6568 | finish_wait(&ctx->sqo_wait, &wait); | |
6569 | ||
23b3628e | 6570 | io_ring_clear_wakeup_flag(ctx); |
6c271ce2 JA |
6571 | } |
6572 | ||
8a4955ff | 6573 | mutex_lock(&ctx->uring_lock); |
6b668c9b XW |
6574 | if (likely(!percpu_ref_is_dying(&ctx->refs))) |
6575 | ret = io_submit_sqes(ctx, to_submit, NULL, -1); | |
8a4955ff | 6576 | mutex_unlock(&ctx->uring_lock); |
bdcd3eab | 6577 | timeout = jiffies + ctx->sq_thread_idle; |
6c271ce2 JA |
6578 | } |
6579 | ||
4c6e277c | 6580 | io_run_task_work(); |
b41e9852 | 6581 | |
4349f30e | 6582 | io_sq_thread_drop_mm(); |
181e448d | 6583 | revert_creds(old_cred); |
06058632 | 6584 | |
2bbcd6d3 | 6585 | kthread_parkme(); |
06058632 | 6586 | |
6c271ce2 JA |
6587 | return 0; |
6588 | } | |
6589 | ||
bda52162 JA |
6590 | struct io_wait_queue { |
6591 | struct wait_queue_entry wq; | |
6592 | struct io_ring_ctx *ctx; | |
6593 | unsigned to_wait; | |
6594 | unsigned nr_timeouts; | |
6595 | }; | |
6596 | ||
1d7bb1d5 | 6597 | static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush) |
bda52162 JA |
6598 | { |
6599 | struct io_ring_ctx *ctx = iowq->ctx; | |
6600 | ||
6601 | /* | |
d195a66e | 6602 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
6603 | * started waiting. For timeouts, we always want to return to userspace, |
6604 | * regardless of event count. | |
6605 | */ | |
1d7bb1d5 | 6606 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
bda52162 JA |
6607 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
6608 | } | |
6609 | ||
6610 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
6611 | int wake_flags, void *key) | |
6612 | { | |
6613 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
6614 | wq); | |
6615 | ||
1d7bb1d5 JA |
6616 | /* use noflush == true, as we can't safely rely on locking context */ |
6617 | if (!io_should_wake(iowq, true)) | |
bda52162 JA |
6618 | return -1; |
6619 | ||
6620 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
6621 | } | |
6622 | ||
2b188cc1 JA |
6623 | /* |
6624 | * Wait until events become available, if we don't already have some. The | |
6625 | * application must reap them itself, as they reside on the shared cq ring. | |
6626 | */ | |
6627 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
6628 | const sigset_t __user *sig, size_t sigsz) | |
6629 | { | |
bda52162 JA |
6630 | struct io_wait_queue iowq = { |
6631 | .wq = { | |
6632 | .private = current, | |
6633 | .func = io_wake_function, | |
6634 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
6635 | }, | |
6636 | .ctx = ctx, | |
6637 | .to_wait = min_events, | |
6638 | }; | |
75b28aff | 6639 | struct io_rings *rings = ctx->rings; |
e9ffa5c2 | 6640 | int ret = 0; |
2b188cc1 | 6641 | |
b41e9852 JA |
6642 | do { |
6643 | if (io_cqring_events(ctx, false) >= min_events) | |
6644 | return 0; | |
4c6e277c | 6645 | if (!io_run_task_work()) |
b41e9852 | 6646 | break; |
b41e9852 | 6647 | } while (1); |
2b188cc1 JA |
6648 | |
6649 | if (sig) { | |
9e75ad5d AB |
6650 | #ifdef CONFIG_COMPAT |
6651 | if (in_compat_syscall()) | |
6652 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 6653 | sigsz); |
9e75ad5d AB |
6654 | else |
6655 | #endif | |
b772434b | 6656 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 6657 | |
2b188cc1 JA |
6658 | if (ret) |
6659 | return ret; | |
6660 | } | |
6661 | ||
bda52162 | 6662 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
c826bd7a | 6663 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 JA |
6664 | do { |
6665 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, | |
6666 | TASK_INTERRUPTIBLE); | |
ce593a6c | 6667 | /* make sure we run task_work before checking for signals */ |
4c6e277c JA |
6668 | if (io_run_task_work()) |
6669 | continue; | |
bda52162 | 6670 | if (signal_pending(current)) { |
b7db41c9 JA |
6671 | if (current->jobctl & JOBCTL_TASK_WORK) { |
6672 | spin_lock_irq(¤t->sighand->siglock); | |
6673 | current->jobctl &= ~JOBCTL_TASK_WORK; | |
6674 | recalc_sigpending(); | |
6675 | spin_unlock_irq(¤t->sighand->siglock); | |
6676 | continue; | |
6677 | } | |
6678 | ret = -EINTR; | |
bda52162 JA |
6679 | break; |
6680 | } | |
ce593a6c JA |
6681 | if (io_should_wake(&iowq, false)) |
6682 | break; | |
6683 | schedule(); | |
bda52162 JA |
6684 | } while (1); |
6685 | finish_wait(&ctx->wait, &iowq.wq); | |
6686 | ||
b7db41c9 | 6687 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 6688 | |
75b28aff | 6689 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
6690 | } |
6691 | ||
6b06314c JA |
6692 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
6693 | { | |
6694 | #if defined(CONFIG_UNIX) | |
6695 | if (ctx->ring_sock) { | |
6696 | struct sock *sock = ctx->ring_sock->sk; | |
6697 | struct sk_buff *skb; | |
6698 | ||
6699 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
6700 | kfree_skb(skb); | |
6701 | } | |
6702 | #else | |
6703 | int i; | |
6704 | ||
65e19f54 JA |
6705 | for (i = 0; i < ctx->nr_user_files; i++) { |
6706 | struct file *file; | |
6707 | ||
6708 | file = io_file_from_index(ctx, i); | |
6709 | if (file) | |
6710 | fput(file); | |
6711 | } | |
6b06314c JA |
6712 | #endif |
6713 | } | |
6714 | ||
05f3fb3c JA |
6715 | static void io_file_ref_kill(struct percpu_ref *ref) |
6716 | { | |
6717 | struct fixed_file_data *data; | |
6718 | ||
6719 | data = container_of(ref, struct fixed_file_data, refs); | |
6720 | complete(&data->done); | |
6721 | } | |
6722 | ||
6b06314c JA |
6723 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
6724 | { | |
05f3fb3c | 6725 | struct fixed_file_data *data = ctx->file_data; |
05589553 | 6726 | struct fixed_file_ref_node *ref_node = NULL; |
65e19f54 JA |
6727 | unsigned nr_tables, i; |
6728 | ||
05f3fb3c | 6729 | if (!data) |
6b06314c JA |
6730 | return -ENXIO; |
6731 | ||
6a4d07cd | 6732 | spin_lock(&data->lock); |
05589553 XW |
6733 | if (!list_empty(&data->ref_list)) |
6734 | ref_node = list_first_entry(&data->ref_list, | |
6735 | struct fixed_file_ref_node, node); | |
6a4d07cd | 6736 | spin_unlock(&data->lock); |
05589553 XW |
6737 | if (ref_node) |
6738 | percpu_ref_kill(&ref_node->refs); | |
6739 | ||
6740 | percpu_ref_kill(&data->refs); | |
6741 | ||
6742 | /* wait for all refs nodes to complete */ | |
4a38aed2 | 6743 | flush_delayed_work(&ctx->file_put_work); |
2faf852d | 6744 | wait_for_completion(&data->done); |
05f3fb3c | 6745 | |
6b06314c | 6746 | __io_sqe_files_unregister(ctx); |
65e19f54 JA |
6747 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
6748 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c JA |
6749 | kfree(data->table[i].files); |
6750 | kfree(data->table); | |
05589553 XW |
6751 | percpu_ref_exit(&data->refs); |
6752 | kfree(data); | |
05f3fb3c | 6753 | ctx->file_data = NULL; |
6b06314c JA |
6754 | ctx->nr_user_files = 0; |
6755 | return 0; | |
6756 | } | |
6757 | ||
6c271ce2 JA |
6758 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
6759 | { | |
6760 | if (ctx->sqo_thread) { | |
0f158b4c | 6761 | wait_for_completion(&ctx->sq_thread_comp); |
2bbcd6d3 RP |
6762 | /* |
6763 | * The park is a bit of a work-around, without it we get | |
6764 | * warning spews on shutdown with SQPOLL set and affinity | |
6765 | * set to a single CPU. | |
6766 | */ | |
06058632 | 6767 | kthread_park(ctx->sqo_thread); |
6c271ce2 JA |
6768 | kthread_stop(ctx->sqo_thread); |
6769 | ctx->sqo_thread = NULL; | |
6770 | } | |
6771 | } | |
6772 | ||
6b06314c JA |
6773 | static void io_finish_async(struct io_ring_ctx *ctx) |
6774 | { | |
6c271ce2 JA |
6775 | io_sq_thread_stop(ctx); |
6776 | ||
561fb04a JA |
6777 | if (ctx->io_wq) { |
6778 | io_wq_destroy(ctx->io_wq); | |
6779 | ctx->io_wq = NULL; | |
6b06314c JA |
6780 | } |
6781 | } | |
6782 | ||
6783 | #if defined(CONFIG_UNIX) | |
6b06314c JA |
6784 | /* |
6785 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
6786 | * the io_uring can be safely unregistered on process exit, even if we have | |
6787 | * loops in the file referencing. | |
6788 | */ | |
6789 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
6790 | { | |
6791 | struct sock *sk = ctx->ring_sock->sk; | |
6792 | struct scm_fp_list *fpl; | |
6793 | struct sk_buff *skb; | |
08a45173 | 6794 | int i, nr_files; |
6b06314c | 6795 | |
6b06314c JA |
6796 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
6797 | if (!fpl) | |
6798 | return -ENOMEM; | |
6799 | ||
6800 | skb = alloc_skb(0, GFP_KERNEL); | |
6801 | if (!skb) { | |
6802 | kfree(fpl); | |
6803 | return -ENOMEM; | |
6804 | } | |
6805 | ||
6806 | skb->sk = sk; | |
6b06314c | 6807 | |
08a45173 | 6808 | nr_files = 0; |
6b06314c JA |
6809 | fpl->user = get_uid(ctx->user); |
6810 | for (i = 0; i < nr; i++) { | |
65e19f54 JA |
6811 | struct file *file = io_file_from_index(ctx, i + offset); |
6812 | ||
6813 | if (!file) | |
08a45173 | 6814 | continue; |
65e19f54 | 6815 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
6816 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
6817 | nr_files++; | |
6b06314c JA |
6818 | } |
6819 | ||
08a45173 JA |
6820 | if (nr_files) { |
6821 | fpl->max = SCM_MAX_FD; | |
6822 | fpl->count = nr_files; | |
6823 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 6824 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
6825 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
6826 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 6827 | |
08a45173 JA |
6828 | for (i = 0; i < nr_files; i++) |
6829 | fput(fpl->fp[i]); | |
6830 | } else { | |
6831 | kfree_skb(skb); | |
6832 | kfree(fpl); | |
6833 | } | |
6b06314c JA |
6834 | |
6835 | return 0; | |
6836 | } | |
6837 | ||
6838 | /* | |
6839 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
6840 | * causes regular reference counting to break down. We rely on the UNIX | |
6841 | * garbage collection to take care of this problem for us. | |
6842 | */ | |
6843 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
6844 | { | |
6845 | unsigned left, total; | |
6846 | int ret = 0; | |
6847 | ||
6848 | total = 0; | |
6849 | left = ctx->nr_user_files; | |
6850 | while (left) { | |
6851 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
6852 | |
6853 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
6854 | if (ret) | |
6855 | break; | |
6856 | left -= this_files; | |
6857 | total += this_files; | |
6858 | } | |
6859 | ||
6860 | if (!ret) | |
6861 | return 0; | |
6862 | ||
6863 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
6864 | struct file *file = io_file_from_index(ctx, total); |
6865 | ||
6866 | if (file) | |
6867 | fput(file); | |
6b06314c JA |
6868 | total++; |
6869 | } | |
6870 | ||
6871 | return ret; | |
6872 | } | |
6873 | #else | |
6874 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
6875 | { | |
6876 | return 0; | |
6877 | } | |
6878 | #endif | |
6879 | ||
65e19f54 JA |
6880 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
6881 | unsigned nr_files) | |
6882 | { | |
6883 | int i; | |
6884 | ||
6885 | for (i = 0; i < nr_tables; i++) { | |
05f3fb3c | 6886 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
65e19f54 JA |
6887 | unsigned this_files; |
6888 | ||
6889 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); | |
6890 | table->files = kcalloc(this_files, sizeof(struct file *), | |
6891 | GFP_KERNEL); | |
6892 | if (!table->files) | |
6893 | break; | |
6894 | nr_files -= this_files; | |
6895 | } | |
6896 | ||
6897 | if (i == nr_tables) | |
6898 | return 0; | |
6899 | ||
6900 | for (i = 0; i < nr_tables; i++) { | |
05f3fb3c | 6901 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
65e19f54 JA |
6902 | kfree(table->files); |
6903 | } | |
6904 | return 1; | |
6905 | } | |
6906 | ||
05f3fb3c JA |
6907 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file) |
6908 | { | |
6909 | #if defined(CONFIG_UNIX) | |
6910 | struct sock *sock = ctx->ring_sock->sk; | |
6911 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
6912 | struct sk_buff *skb; | |
6913 | int i; | |
6914 | ||
6915 | __skb_queue_head_init(&list); | |
6916 | ||
6917 | /* | |
6918 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
6919 | * remove this entry and rearrange the file array. | |
6920 | */ | |
6921 | skb = skb_dequeue(head); | |
6922 | while (skb) { | |
6923 | struct scm_fp_list *fp; | |
6924 | ||
6925 | fp = UNIXCB(skb).fp; | |
6926 | for (i = 0; i < fp->count; i++) { | |
6927 | int left; | |
6928 | ||
6929 | if (fp->fp[i] != file) | |
6930 | continue; | |
6931 | ||
6932 | unix_notinflight(fp->user, fp->fp[i]); | |
6933 | left = fp->count - 1 - i; | |
6934 | if (left) { | |
6935 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
6936 | left * sizeof(struct file *)); | |
6937 | } | |
6938 | fp->count--; | |
6939 | if (!fp->count) { | |
6940 | kfree_skb(skb); | |
6941 | skb = NULL; | |
6942 | } else { | |
6943 | __skb_queue_tail(&list, skb); | |
6944 | } | |
6945 | fput(file); | |
6946 | file = NULL; | |
6947 | break; | |
6948 | } | |
6949 | ||
6950 | if (!file) | |
6951 | break; | |
6952 | ||
6953 | __skb_queue_tail(&list, skb); | |
6954 | ||
6955 | skb = skb_dequeue(head); | |
6956 | } | |
6957 | ||
6958 | if (skb_peek(&list)) { | |
6959 | spin_lock_irq(&head->lock); | |
6960 | while ((skb = __skb_dequeue(&list)) != NULL) | |
6961 | __skb_queue_tail(head, skb); | |
6962 | spin_unlock_irq(&head->lock); | |
6963 | } | |
6964 | #else | |
6965 | fput(file); | |
6966 | #endif | |
6967 | } | |
6968 | ||
6969 | struct io_file_put { | |
05589553 | 6970 | struct list_head list; |
05f3fb3c | 6971 | struct file *file; |
05f3fb3c JA |
6972 | }; |
6973 | ||
4a38aed2 | 6974 | static void __io_file_put_work(struct fixed_file_ref_node *ref_node) |
65e19f54 | 6975 | { |
4a38aed2 JA |
6976 | struct fixed_file_data *file_data = ref_node->file_data; |
6977 | struct io_ring_ctx *ctx = file_data->ctx; | |
05f3fb3c | 6978 | struct io_file_put *pfile, *tmp; |
05589553 XW |
6979 | |
6980 | list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) { | |
6a4d07cd | 6981 | list_del(&pfile->list); |
05589553 XW |
6982 | io_ring_file_put(ctx, pfile->file); |
6983 | kfree(pfile); | |
65e19f54 | 6984 | } |
05589553 | 6985 | |
6a4d07cd JA |
6986 | spin_lock(&file_data->lock); |
6987 | list_del(&ref_node->node); | |
6988 | spin_unlock(&file_data->lock); | |
05589553 XW |
6989 | |
6990 | percpu_ref_exit(&ref_node->refs); | |
6991 | kfree(ref_node); | |
6992 | percpu_ref_put(&file_data->refs); | |
2faf852d | 6993 | } |
65e19f54 | 6994 | |
4a38aed2 JA |
6995 | static void io_file_put_work(struct work_struct *work) |
6996 | { | |
6997 | struct io_ring_ctx *ctx; | |
6998 | struct llist_node *node; | |
6999 | ||
7000 | ctx = container_of(work, struct io_ring_ctx, file_put_work.work); | |
7001 | node = llist_del_all(&ctx->file_put_llist); | |
7002 | ||
7003 | while (node) { | |
7004 | struct fixed_file_ref_node *ref_node; | |
7005 | struct llist_node *next = node->next; | |
7006 | ||
7007 | ref_node = llist_entry(node, struct fixed_file_ref_node, llist); | |
7008 | __io_file_put_work(ref_node); | |
7009 | node = next; | |
7010 | } | |
7011 | } | |
7012 | ||
05589553 | 7013 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
2faf852d | 7014 | { |
05589553 | 7015 | struct fixed_file_ref_node *ref_node; |
4a38aed2 JA |
7016 | struct io_ring_ctx *ctx; |
7017 | bool first_add; | |
7018 | int delay = HZ; | |
65e19f54 | 7019 | |
05589553 | 7020 | ref_node = container_of(ref, struct fixed_file_ref_node, refs); |
4a38aed2 | 7021 | ctx = ref_node->file_data->ctx; |
05589553 | 7022 | |
4a38aed2 JA |
7023 | if (percpu_ref_is_dying(&ctx->file_data->refs)) |
7024 | delay = 0; | |
05589553 | 7025 | |
4a38aed2 JA |
7026 | first_add = llist_add(&ref_node->llist, &ctx->file_put_llist); |
7027 | if (!delay) | |
7028 | mod_delayed_work(system_wq, &ctx->file_put_work, 0); | |
7029 | else if (first_add) | |
7030 | queue_delayed_work(system_wq, &ctx->file_put_work, delay); | |
05f3fb3c | 7031 | } |
65e19f54 | 7032 | |
05589553 XW |
7033 | static struct fixed_file_ref_node *alloc_fixed_file_ref_node( |
7034 | struct io_ring_ctx *ctx) | |
05f3fb3c | 7035 | { |
05589553 | 7036 | struct fixed_file_ref_node *ref_node; |
05f3fb3c | 7037 | |
05589553 XW |
7038 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
7039 | if (!ref_node) | |
7040 | return ERR_PTR(-ENOMEM); | |
05f3fb3c | 7041 | |
05589553 XW |
7042 | if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero, |
7043 | 0, GFP_KERNEL)) { | |
7044 | kfree(ref_node); | |
7045 | return ERR_PTR(-ENOMEM); | |
7046 | } | |
7047 | INIT_LIST_HEAD(&ref_node->node); | |
7048 | INIT_LIST_HEAD(&ref_node->file_list); | |
05589553 XW |
7049 | ref_node->file_data = ctx->file_data; |
7050 | return ref_node; | |
05589553 XW |
7051 | } |
7052 | ||
7053 | static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node) | |
7054 | { | |
7055 | percpu_ref_exit(&ref_node->refs); | |
7056 | kfree(ref_node); | |
65e19f54 JA |
7057 | } |
7058 | ||
6b06314c JA |
7059 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
7060 | unsigned nr_args) | |
7061 | { | |
7062 | __s32 __user *fds = (__s32 __user *) arg; | |
65e19f54 | 7063 | unsigned nr_tables; |
05f3fb3c | 7064 | struct file *file; |
6b06314c JA |
7065 | int fd, ret = 0; |
7066 | unsigned i; | |
05589553 | 7067 | struct fixed_file_ref_node *ref_node; |
6b06314c | 7068 | |
05f3fb3c | 7069 | if (ctx->file_data) |
6b06314c JA |
7070 | return -EBUSY; |
7071 | if (!nr_args) | |
7072 | return -EINVAL; | |
7073 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7074 | return -EMFILE; | |
7075 | ||
05f3fb3c JA |
7076 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
7077 | if (!ctx->file_data) | |
7078 | return -ENOMEM; | |
7079 | ctx->file_data->ctx = ctx; | |
7080 | init_completion(&ctx->file_data->done); | |
05589553 | 7081 | INIT_LIST_HEAD(&ctx->file_data->ref_list); |
f7fe9346 | 7082 | spin_lock_init(&ctx->file_data->lock); |
05f3fb3c | 7083 | |
65e19f54 | 7084 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
05f3fb3c JA |
7085 | ctx->file_data->table = kcalloc(nr_tables, |
7086 | sizeof(struct fixed_file_table), | |
65e19f54 | 7087 | GFP_KERNEL); |
05f3fb3c JA |
7088 | if (!ctx->file_data->table) { |
7089 | kfree(ctx->file_data); | |
7090 | ctx->file_data = NULL; | |
6b06314c | 7091 | return -ENOMEM; |
05f3fb3c JA |
7092 | } |
7093 | ||
05589553 | 7094 | if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill, |
05f3fb3c JA |
7095 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
7096 | kfree(ctx->file_data->table); | |
7097 | kfree(ctx->file_data); | |
7098 | ctx->file_data = NULL; | |
6b06314c | 7099 | return -ENOMEM; |
05f3fb3c | 7100 | } |
6b06314c | 7101 | |
65e19f54 | 7102 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
05f3fb3c JA |
7103 | percpu_ref_exit(&ctx->file_data->refs); |
7104 | kfree(ctx->file_data->table); | |
7105 | kfree(ctx->file_data); | |
7106 | ctx->file_data = NULL; | |
65e19f54 JA |
7107 | return -ENOMEM; |
7108 | } | |
7109 | ||
08a45173 | 7110 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
65e19f54 JA |
7111 | struct fixed_file_table *table; |
7112 | unsigned index; | |
7113 | ||
6b06314c JA |
7114 | ret = -EFAULT; |
7115 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) | |
7116 | break; | |
08a45173 JA |
7117 | /* allow sparse sets */ |
7118 | if (fd == -1) { | |
7119 | ret = 0; | |
7120 | continue; | |
7121 | } | |
6b06314c | 7122 | |
05f3fb3c | 7123 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
65e19f54 | 7124 | index = i & IORING_FILE_TABLE_MASK; |
05f3fb3c | 7125 | file = fget(fd); |
6b06314c JA |
7126 | |
7127 | ret = -EBADF; | |
05f3fb3c | 7128 | if (!file) |
6b06314c | 7129 | break; |
05f3fb3c | 7130 | |
6b06314c JA |
7131 | /* |
7132 | * Don't allow io_uring instances to be registered. If UNIX | |
7133 | * isn't enabled, then this causes a reference cycle and this | |
7134 | * instance can never get freed. If UNIX is enabled we'll | |
7135 | * handle it just fine, but there's still no point in allowing | |
7136 | * a ring fd as it doesn't support regular read/write anyway. | |
7137 | */ | |
05f3fb3c JA |
7138 | if (file->f_op == &io_uring_fops) { |
7139 | fput(file); | |
6b06314c JA |
7140 | break; |
7141 | } | |
6b06314c | 7142 | ret = 0; |
05f3fb3c | 7143 | table->files[index] = file; |
6b06314c JA |
7144 | } |
7145 | ||
7146 | if (ret) { | |
65e19f54 | 7147 | for (i = 0; i < ctx->nr_user_files; i++) { |
65e19f54 JA |
7148 | file = io_file_from_index(ctx, i); |
7149 | if (file) | |
7150 | fput(file); | |
7151 | } | |
7152 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c | 7153 | kfree(ctx->file_data->table[i].files); |
6b06314c | 7154 | |
667e57da | 7155 | percpu_ref_exit(&ctx->file_data->refs); |
05f3fb3c JA |
7156 | kfree(ctx->file_data->table); |
7157 | kfree(ctx->file_data); | |
7158 | ctx->file_data = NULL; | |
6b06314c JA |
7159 | ctx->nr_user_files = 0; |
7160 | return ret; | |
7161 | } | |
7162 | ||
7163 | ret = io_sqe_files_scm(ctx); | |
05589553 | 7164 | if (ret) { |
6b06314c | 7165 | io_sqe_files_unregister(ctx); |
05589553 XW |
7166 | return ret; |
7167 | } | |
6b06314c | 7168 | |
05589553 XW |
7169 | ref_node = alloc_fixed_file_ref_node(ctx); |
7170 | if (IS_ERR(ref_node)) { | |
7171 | io_sqe_files_unregister(ctx); | |
7172 | return PTR_ERR(ref_node); | |
7173 | } | |
7174 | ||
7175 | ctx->file_data->cur_refs = &ref_node->refs; | |
6a4d07cd | 7176 | spin_lock(&ctx->file_data->lock); |
05589553 | 7177 | list_add(&ref_node->node, &ctx->file_data->ref_list); |
6a4d07cd | 7178 | spin_unlock(&ctx->file_data->lock); |
05589553 | 7179 | percpu_ref_get(&ctx->file_data->refs); |
6b06314c JA |
7180 | return ret; |
7181 | } | |
7182 | ||
c3a31e60 JA |
7183 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7184 | int index) | |
7185 | { | |
7186 | #if defined(CONFIG_UNIX) | |
7187 | struct sock *sock = ctx->ring_sock->sk; | |
7188 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7189 | struct sk_buff *skb; | |
7190 | ||
7191 | /* | |
7192 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7193 | * file set. If there's no room, fall back to allocating a new skb | |
7194 | * and filling it in. | |
7195 | */ | |
7196 | spin_lock_irq(&head->lock); | |
7197 | skb = skb_peek(head); | |
7198 | if (skb) { | |
7199 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7200 | ||
7201 | if (fpl->count < SCM_MAX_FD) { | |
7202 | __skb_unlink(skb, head); | |
7203 | spin_unlock_irq(&head->lock); | |
7204 | fpl->fp[fpl->count] = get_file(file); | |
7205 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7206 | fpl->count++; | |
7207 | spin_lock_irq(&head->lock); | |
7208 | __skb_queue_head(head, skb); | |
7209 | } else { | |
7210 | skb = NULL; | |
7211 | } | |
7212 | } | |
7213 | spin_unlock_irq(&head->lock); | |
7214 | ||
7215 | if (skb) { | |
7216 | fput(file); | |
7217 | return 0; | |
7218 | } | |
7219 | ||
7220 | return __io_sqe_files_scm(ctx, 1, index); | |
7221 | #else | |
7222 | return 0; | |
7223 | #endif | |
7224 | } | |
7225 | ||
a5318d3c | 7226 | static int io_queue_file_removal(struct fixed_file_data *data, |
05589553 | 7227 | struct file *file) |
05f3fb3c | 7228 | { |
a5318d3c | 7229 | struct io_file_put *pfile; |
05589553 XW |
7230 | struct percpu_ref *refs = data->cur_refs; |
7231 | struct fixed_file_ref_node *ref_node; | |
05f3fb3c | 7232 | |
05f3fb3c | 7233 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
a5318d3c HD |
7234 | if (!pfile) |
7235 | return -ENOMEM; | |
05f3fb3c | 7236 | |
05589553 | 7237 | ref_node = container_of(refs, struct fixed_file_ref_node, refs); |
05f3fb3c | 7238 | pfile->file = file; |
05589553 XW |
7239 | list_add(&pfile->list, &ref_node->file_list); |
7240 | ||
a5318d3c | 7241 | return 0; |
05f3fb3c JA |
7242 | } |
7243 | ||
7244 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, | |
7245 | struct io_uring_files_update *up, | |
7246 | unsigned nr_args) | |
7247 | { | |
7248 | struct fixed_file_data *data = ctx->file_data; | |
05589553 | 7249 | struct fixed_file_ref_node *ref_node; |
05f3fb3c | 7250 | struct file *file; |
c3a31e60 JA |
7251 | __s32 __user *fds; |
7252 | int fd, i, err; | |
7253 | __u32 done; | |
05589553 | 7254 | bool needs_switch = false; |
c3a31e60 | 7255 | |
05f3fb3c | 7256 | if (check_add_overflow(up->offset, nr_args, &done)) |
c3a31e60 JA |
7257 | return -EOVERFLOW; |
7258 | if (done > ctx->nr_user_files) | |
7259 | return -EINVAL; | |
7260 | ||
05589553 XW |
7261 | ref_node = alloc_fixed_file_ref_node(ctx); |
7262 | if (IS_ERR(ref_node)) | |
7263 | return PTR_ERR(ref_node); | |
7264 | ||
c3a31e60 | 7265 | done = 0; |
05f3fb3c | 7266 | fds = u64_to_user_ptr(up->fds); |
c3a31e60 | 7267 | while (nr_args) { |
65e19f54 JA |
7268 | struct fixed_file_table *table; |
7269 | unsigned index; | |
7270 | ||
c3a31e60 JA |
7271 | err = 0; |
7272 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
7273 | err = -EFAULT; | |
7274 | break; | |
7275 | } | |
05f3fb3c JA |
7276 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
7277 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
65e19f54 JA |
7278 | index = i & IORING_FILE_TABLE_MASK; |
7279 | if (table->files[index]) { | |
05f3fb3c | 7280 | file = io_file_from_index(ctx, index); |
a5318d3c HD |
7281 | err = io_queue_file_removal(data, file); |
7282 | if (err) | |
7283 | break; | |
65e19f54 | 7284 | table->files[index] = NULL; |
05589553 | 7285 | needs_switch = true; |
c3a31e60 JA |
7286 | } |
7287 | if (fd != -1) { | |
c3a31e60 JA |
7288 | file = fget(fd); |
7289 | if (!file) { | |
7290 | err = -EBADF; | |
7291 | break; | |
7292 | } | |
7293 | /* | |
7294 | * Don't allow io_uring instances to be registered. If | |
7295 | * UNIX isn't enabled, then this causes a reference | |
7296 | * cycle and this instance can never get freed. If UNIX | |
7297 | * is enabled we'll handle it just fine, but there's | |
7298 | * still no point in allowing a ring fd as it doesn't | |
7299 | * support regular read/write anyway. | |
7300 | */ | |
7301 | if (file->f_op == &io_uring_fops) { | |
7302 | fput(file); | |
7303 | err = -EBADF; | |
7304 | break; | |
7305 | } | |
65e19f54 | 7306 | table->files[index] = file; |
c3a31e60 | 7307 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae YY |
7308 | if (err) { |
7309 | fput(file); | |
c3a31e60 | 7310 | break; |
f3bd9dae | 7311 | } |
c3a31e60 JA |
7312 | } |
7313 | nr_args--; | |
7314 | done++; | |
05f3fb3c JA |
7315 | up->offset++; |
7316 | } | |
7317 | ||
05589553 XW |
7318 | if (needs_switch) { |
7319 | percpu_ref_kill(data->cur_refs); | |
6a4d07cd | 7320 | spin_lock(&data->lock); |
05589553 XW |
7321 | list_add(&ref_node->node, &data->ref_list); |
7322 | data->cur_refs = &ref_node->refs; | |
6a4d07cd | 7323 | spin_unlock(&data->lock); |
05589553 XW |
7324 | percpu_ref_get(&ctx->file_data->refs); |
7325 | } else | |
7326 | destroy_fixed_file_ref_node(ref_node); | |
c3a31e60 JA |
7327 | |
7328 | return done ? done : err; | |
7329 | } | |
05589553 | 7330 | |
05f3fb3c JA |
7331 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
7332 | unsigned nr_args) | |
7333 | { | |
7334 | struct io_uring_files_update up; | |
7335 | ||
7336 | if (!ctx->file_data) | |
7337 | return -ENXIO; | |
7338 | if (!nr_args) | |
7339 | return -EINVAL; | |
7340 | if (copy_from_user(&up, arg, sizeof(up))) | |
7341 | return -EFAULT; | |
7342 | if (up.resv) | |
7343 | return -EINVAL; | |
7344 | ||
7345 | return __io_sqe_files_update(ctx, &up, nr_args); | |
7346 | } | |
c3a31e60 | 7347 | |
e9fd9396 | 7348 | static void io_free_work(struct io_wq_work *work) |
7d723065 JA |
7349 | { |
7350 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
7351 | ||
e9fd9396 | 7352 | /* Consider that io_steal_work() relies on this ref */ |
7d723065 JA |
7353 | io_put_req(req); |
7354 | } | |
7355 | ||
24369c2e PB |
7356 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
7357 | struct io_uring_params *p) | |
7358 | { | |
7359 | struct io_wq_data data; | |
7360 | struct fd f; | |
7361 | struct io_ring_ctx *ctx_attach; | |
7362 | unsigned int concurrency; | |
7363 | int ret = 0; | |
7364 | ||
7365 | data.user = ctx->user; | |
e9fd9396 | 7366 | data.free_work = io_free_work; |
f5fa38c5 | 7367 | data.do_work = io_wq_submit_work; |
24369c2e PB |
7368 | |
7369 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { | |
7370 | /* Do QD, or 4 * CPUS, whatever is smallest */ | |
7371 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
7372 | ||
7373 | ctx->io_wq = io_wq_create(concurrency, &data); | |
7374 | if (IS_ERR(ctx->io_wq)) { | |
7375 | ret = PTR_ERR(ctx->io_wq); | |
7376 | ctx->io_wq = NULL; | |
7377 | } | |
7378 | return ret; | |
7379 | } | |
7380 | ||
7381 | f = fdget(p->wq_fd); | |
7382 | if (!f.file) | |
7383 | return -EBADF; | |
7384 | ||
7385 | if (f.file->f_op != &io_uring_fops) { | |
7386 | ret = -EINVAL; | |
7387 | goto out_fput; | |
7388 | } | |
7389 | ||
7390 | ctx_attach = f.file->private_data; | |
7391 | /* @io_wq is protected by holding the fd */ | |
7392 | if (!io_wq_get(ctx_attach->io_wq, &data)) { | |
7393 | ret = -EINVAL; | |
7394 | goto out_fput; | |
7395 | } | |
7396 | ||
7397 | ctx->io_wq = ctx_attach->io_wq; | |
7398 | out_fput: | |
7399 | fdput(f); | |
7400 | return ret; | |
7401 | } | |
7402 | ||
6c271ce2 JA |
7403 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
7404 | struct io_uring_params *p) | |
2b188cc1 JA |
7405 | { |
7406 | int ret; | |
7407 | ||
7408 | mmgrab(current->mm); | |
7409 | ctx->sqo_mm = current->mm; | |
7410 | ||
6c271ce2 | 7411 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
3ec482d1 JA |
7412 | ret = -EPERM; |
7413 | if (!capable(CAP_SYS_ADMIN)) | |
7414 | goto err; | |
7415 | ||
917257da JA |
7416 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
7417 | if (!ctx->sq_thread_idle) | |
7418 | ctx->sq_thread_idle = HZ; | |
7419 | ||
6c271ce2 | 7420 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 7421 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 7422 | |
917257da | 7423 | ret = -EINVAL; |
44a9bd18 JA |
7424 | if (cpu >= nr_cpu_ids) |
7425 | goto err; | |
7889f44d | 7426 | if (!cpu_online(cpu)) |
917257da JA |
7427 | goto err; |
7428 | ||
6c271ce2 JA |
7429 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
7430 | ctx, cpu, | |
7431 | "io_uring-sq"); | |
7432 | } else { | |
7433 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, | |
7434 | "io_uring-sq"); | |
7435 | } | |
7436 | if (IS_ERR(ctx->sqo_thread)) { | |
7437 | ret = PTR_ERR(ctx->sqo_thread); | |
7438 | ctx->sqo_thread = NULL; | |
7439 | goto err; | |
7440 | } | |
7441 | wake_up_process(ctx->sqo_thread); | |
7442 | } else if (p->flags & IORING_SETUP_SQ_AFF) { | |
7443 | /* Can't have SQ_AFF without SQPOLL */ | |
7444 | ret = -EINVAL; | |
7445 | goto err; | |
7446 | } | |
7447 | ||
24369c2e PB |
7448 | ret = io_init_wq_offload(ctx, p); |
7449 | if (ret) | |
2b188cc1 | 7450 | goto err; |
2b188cc1 JA |
7451 | |
7452 | return 0; | |
7453 | err: | |
54a91f3b | 7454 | io_finish_async(ctx); |
8eb06d7e PB |
7455 | if (ctx->sqo_mm) { |
7456 | mmdrop(ctx->sqo_mm); | |
7457 | ctx->sqo_mm = NULL; | |
7458 | } | |
2b188cc1 JA |
7459 | return ret; |
7460 | } | |
7461 | ||
a087e2b5 BM |
7462 | static inline void __io_unaccount_mem(struct user_struct *user, |
7463 | unsigned long nr_pages) | |
2b188cc1 JA |
7464 | { |
7465 | atomic_long_sub(nr_pages, &user->locked_vm); | |
7466 | } | |
7467 | ||
a087e2b5 BM |
7468 | static inline int __io_account_mem(struct user_struct *user, |
7469 | unsigned long nr_pages) | |
2b188cc1 JA |
7470 | { |
7471 | unsigned long page_limit, cur_pages, new_pages; | |
7472 | ||
7473 | /* Don't allow more pages than we can safely lock */ | |
7474 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
7475 | ||
7476 | do { | |
7477 | cur_pages = atomic_long_read(&user->locked_vm); | |
7478 | new_pages = cur_pages + nr_pages; | |
7479 | if (new_pages > page_limit) | |
7480 | return -ENOMEM; | |
7481 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
7482 | new_pages) != cur_pages); | |
7483 | ||
7484 | return 0; | |
7485 | } | |
7486 | ||
2e0464d4 BM |
7487 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
7488 | enum io_mem_account acct) | |
a087e2b5 | 7489 | { |
aad5d8da | 7490 | if (ctx->limit_mem) |
a087e2b5 | 7491 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 7492 | |
2e0464d4 BM |
7493 | if (ctx->sqo_mm) { |
7494 | if (acct == ACCT_LOCKED) | |
7495 | ctx->sqo_mm->locked_vm -= nr_pages; | |
7496 | else if (acct == ACCT_PINNED) | |
7497 | atomic64_sub(nr_pages, &ctx->sqo_mm->pinned_vm); | |
7498 | } | |
a087e2b5 BM |
7499 | } |
7500 | ||
2e0464d4 BM |
7501 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
7502 | enum io_mem_account acct) | |
a087e2b5 | 7503 | { |
30975825 BM |
7504 | int ret; |
7505 | ||
7506 | if (ctx->limit_mem) { | |
7507 | ret = __io_account_mem(ctx->user, nr_pages); | |
7508 | if (ret) | |
7509 | return ret; | |
7510 | } | |
7511 | ||
2e0464d4 BM |
7512 | if (ctx->sqo_mm) { |
7513 | if (acct == ACCT_LOCKED) | |
7514 | ctx->sqo_mm->locked_vm += nr_pages; | |
7515 | else if (acct == ACCT_PINNED) | |
7516 | atomic64_add(nr_pages, &ctx->sqo_mm->pinned_vm); | |
7517 | } | |
a087e2b5 BM |
7518 | |
7519 | return 0; | |
7520 | } | |
7521 | ||
2b188cc1 JA |
7522 | static void io_mem_free(void *ptr) |
7523 | { | |
52e04ef4 MR |
7524 | struct page *page; |
7525 | ||
7526 | if (!ptr) | |
7527 | return; | |
2b188cc1 | 7528 | |
52e04ef4 | 7529 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
7530 | if (put_page_testzero(page)) |
7531 | free_compound_page(page); | |
7532 | } | |
7533 | ||
7534 | static void *io_mem_alloc(size_t size) | |
7535 | { | |
7536 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
7537 | __GFP_NORETRY; | |
7538 | ||
7539 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
7540 | } | |
7541 | ||
75b28aff HV |
7542 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
7543 | size_t *sq_offset) | |
7544 | { | |
7545 | struct io_rings *rings; | |
7546 | size_t off, sq_array_size; | |
7547 | ||
7548 | off = struct_size(rings, cqes, cq_entries); | |
7549 | if (off == SIZE_MAX) | |
7550 | return SIZE_MAX; | |
7551 | ||
7552 | #ifdef CONFIG_SMP | |
7553 | off = ALIGN(off, SMP_CACHE_BYTES); | |
7554 | if (off == 0) | |
7555 | return SIZE_MAX; | |
7556 | #endif | |
7557 | ||
b36200f5 DV |
7558 | if (sq_offset) |
7559 | *sq_offset = off; | |
7560 | ||
75b28aff HV |
7561 | sq_array_size = array_size(sizeof(u32), sq_entries); |
7562 | if (sq_array_size == SIZE_MAX) | |
7563 | return SIZE_MAX; | |
7564 | ||
7565 | if (check_add_overflow(off, sq_array_size, &off)) | |
7566 | return SIZE_MAX; | |
7567 | ||
75b28aff HV |
7568 | return off; |
7569 | } | |
7570 | ||
2b188cc1 JA |
7571 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
7572 | { | |
75b28aff | 7573 | size_t pages; |
2b188cc1 | 7574 | |
75b28aff HV |
7575 | pages = (size_t)1 << get_order( |
7576 | rings_size(sq_entries, cq_entries, NULL)); | |
7577 | pages += (size_t)1 << get_order( | |
7578 | array_size(sizeof(struct io_uring_sqe), sq_entries)); | |
2b188cc1 | 7579 | |
75b28aff | 7580 | return pages; |
2b188cc1 JA |
7581 | } |
7582 | ||
edafccee JA |
7583 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
7584 | { | |
7585 | int i, j; | |
7586 | ||
7587 | if (!ctx->user_bufs) | |
7588 | return -ENXIO; | |
7589 | ||
7590 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
7591 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
7592 | ||
7593 | for (j = 0; j < imu->nr_bvecs; j++) | |
f1f6a7dd | 7594 | unpin_user_page(imu->bvec[j].bv_page); |
edafccee | 7595 | |
2e0464d4 | 7596 | io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED); |
d4ef6475 | 7597 | kvfree(imu->bvec); |
edafccee JA |
7598 | imu->nr_bvecs = 0; |
7599 | } | |
7600 | ||
7601 | kfree(ctx->user_bufs); | |
7602 | ctx->user_bufs = NULL; | |
7603 | ctx->nr_user_bufs = 0; | |
7604 | return 0; | |
7605 | } | |
7606 | ||
7607 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
7608 | void __user *arg, unsigned index) | |
7609 | { | |
7610 | struct iovec __user *src; | |
7611 | ||
7612 | #ifdef CONFIG_COMPAT | |
7613 | if (ctx->compat) { | |
7614 | struct compat_iovec __user *ciovs; | |
7615 | struct compat_iovec ciov; | |
7616 | ||
7617 | ciovs = (struct compat_iovec __user *) arg; | |
7618 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
7619 | return -EFAULT; | |
7620 | ||
d55e5f5b | 7621 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
7622 | dst->iov_len = ciov.iov_len; |
7623 | return 0; | |
7624 | } | |
7625 | #endif | |
7626 | src = (struct iovec __user *) arg; | |
7627 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
7628 | return -EFAULT; | |
7629 | return 0; | |
7630 | } | |
7631 | ||
7632 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, | |
7633 | unsigned nr_args) | |
7634 | { | |
7635 | struct vm_area_struct **vmas = NULL; | |
7636 | struct page **pages = NULL; | |
7637 | int i, j, got_pages = 0; | |
7638 | int ret = -EINVAL; | |
7639 | ||
7640 | if (ctx->user_bufs) | |
7641 | return -EBUSY; | |
7642 | if (!nr_args || nr_args > UIO_MAXIOV) | |
7643 | return -EINVAL; | |
7644 | ||
7645 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
7646 | GFP_KERNEL); | |
7647 | if (!ctx->user_bufs) | |
7648 | return -ENOMEM; | |
7649 | ||
7650 | for (i = 0; i < nr_args; i++) { | |
7651 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
7652 | unsigned long off, start, end, ubuf; | |
7653 | int pret, nr_pages; | |
7654 | struct iovec iov; | |
7655 | size_t size; | |
7656 | ||
7657 | ret = io_copy_iov(ctx, &iov, arg, i); | |
7658 | if (ret) | |
a278682d | 7659 | goto err; |
edafccee JA |
7660 | |
7661 | /* | |
7662 | * Don't impose further limits on the size and buffer | |
7663 | * constraints here, we'll -EINVAL later when IO is | |
7664 | * submitted if they are wrong. | |
7665 | */ | |
7666 | ret = -EFAULT; | |
7667 | if (!iov.iov_base || !iov.iov_len) | |
7668 | goto err; | |
7669 | ||
7670 | /* arbitrary limit, but we need something */ | |
7671 | if (iov.iov_len > SZ_1G) | |
7672 | goto err; | |
7673 | ||
7674 | ubuf = (unsigned long) iov.iov_base; | |
7675 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
7676 | start = ubuf >> PAGE_SHIFT; | |
7677 | nr_pages = end - start; | |
7678 | ||
2e0464d4 | 7679 | ret = io_account_mem(ctx, nr_pages, ACCT_PINNED); |
a087e2b5 BM |
7680 | if (ret) |
7681 | goto err; | |
edafccee JA |
7682 | |
7683 | ret = 0; | |
7684 | if (!pages || nr_pages > got_pages) { | |
a8c73c1a DE |
7685 | kvfree(vmas); |
7686 | kvfree(pages); | |
d4ef6475 | 7687 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
edafccee | 7688 | GFP_KERNEL); |
d4ef6475 | 7689 | vmas = kvmalloc_array(nr_pages, |
edafccee JA |
7690 | sizeof(struct vm_area_struct *), |
7691 | GFP_KERNEL); | |
7692 | if (!pages || !vmas) { | |
7693 | ret = -ENOMEM; | |
2e0464d4 | 7694 | io_unaccount_mem(ctx, nr_pages, ACCT_PINNED); |
edafccee JA |
7695 | goto err; |
7696 | } | |
7697 | got_pages = nr_pages; | |
7698 | } | |
7699 | ||
d4ef6475 | 7700 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
edafccee JA |
7701 | GFP_KERNEL); |
7702 | ret = -ENOMEM; | |
7703 | if (!imu->bvec) { | |
2e0464d4 | 7704 | io_unaccount_mem(ctx, nr_pages, ACCT_PINNED); |
edafccee JA |
7705 | goto err; |
7706 | } | |
7707 | ||
7708 | ret = 0; | |
d8ed45c5 | 7709 | mmap_read_lock(current->mm); |
2113b05d | 7710 | pret = pin_user_pages(ubuf, nr_pages, |
932f4a63 IW |
7711 | FOLL_WRITE | FOLL_LONGTERM, |
7712 | pages, vmas); | |
edafccee JA |
7713 | if (pret == nr_pages) { |
7714 | /* don't support file backed memory */ | |
7715 | for (j = 0; j < nr_pages; j++) { | |
7716 | struct vm_area_struct *vma = vmas[j]; | |
7717 | ||
7718 | if (vma->vm_file && | |
7719 | !is_file_hugepages(vma->vm_file)) { | |
7720 | ret = -EOPNOTSUPP; | |
7721 | break; | |
7722 | } | |
7723 | } | |
7724 | } else { | |
7725 | ret = pret < 0 ? pret : -EFAULT; | |
7726 | } | |
d8ed45c5 | 7727 | mmap_read_unlock(current->mm); |
edafccee JA |
7728 | if (ret) { |
7729 | /* | |
7730 | * if we did partial map, or found file backed vmas, | |
7731 | * release any pages we did get | |
7732 | */ | |
27c4d3a3 | 7733 | if (pret > 0) |
f1f6a7dd | 7734 | unpin_user_pages(pages, pret); |
2e0464d4 | 7735 | io_unaccount_mem(ctx, nr_pages, ACCT_PINNED); |
d4ef6475 | 7736 | kvfree(imu->bvec); |
edafccee JA |
7737 | goto err; |
7738 | } | |
7739 | ||
7740 | off = ubuf & ~PAGE_MASK; | |
7741 | size = iov.iov_len; | |
7742 | for (j = 0; j < nr_pages; j++) { | |
7743 | size_t vec_len; | |
7744 | ||
7745 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
7746 | imu->bvec[j].bv_page = pages[j]; | |
7747 | imu->bvec[j].bv_len = vec_len; | |
7748 | imu->bvec[j].bv_offset = off; | |
7749 | off = 0; | |
7750 | size -= vec_len; | |
7751 | } | |
7752 | /* store original address for later verification */ | |
7753 | imu->ubuf = ubuf; | |
7754 | imu->len = iov.iov_len; | |
7755 | imu->nr_bvecs = nr_pages; | |
7756 | ||
7757 | ctx->nr_user_bufs++; | |
7758 | } | |
d4ef6475 MR |
7759 | kvfree(pages); |
7760 | kvfree(vmas); | |
edafccee JA |
7761 | return 0; |
7762 | err: | |
d4ef6475 MR |
7763 | kvfree(pages); |
7764 | kvfree(vmas); | |
edafccee JA |
7765 | io_sqe_buffer_unregister(ctx); |
7766 | return ret; | |
7767 | } | |
7768 | ||
9b402849 JA |
7769 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
7770 | { | |
7771 | __s32 __user *fds = arg; | |
7772 | int fd; | |
7773 | ||
7774 | if (ctx->cq_ev_fd) | |
7775 | return -EBUSY; | |
7776 | ||
7777 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
7778 | return -EFAULT; | |
7779 | ||
7780 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
7781 | if (IS_ERR(ctx->cq_ev_fd)) { | |
7782 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
7783 | ctx->cq_ev_fd = NULL; | |
7784 | return ret; | |
7785 | } | |
7786 | ||
7787 | return 0; | |
7788 | } | |
7789 | ||
7790 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
7791 | { | |
7792 | if (ctx->cq_ev_fd) { | |
7793 | eventfd_ctx_put(ctx->cq_ev_fd); | |
7794 | ctx->cq_ev_fd = NULL; | |
7795 | return 0; | |
7796 | } | |
7797 | ||
7798 | return -ENXIO; | |
7799 | } | |
7800 | ||
5a2e745d JA |
7801 | static int __io_destroy_buffers(int id, void *p, void *data) |
7802 | { | |
7803 | struct io_ring_ctx *ctx = data; | |
7804 | struct io_buffer *buf = p; | |
7805 | ||
067524e9 | 7806 | __io_remove_buffers(ctx, buf, id, -1U); |
5a2e745d JA |
7807 | return 0; |
7808 | } | |
7809 | ||
7810 | static void io_destroy_buffers(struct io_ring_ctx *ctx) | |
7811 | { | |
7812 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); | |
7813 | idr_destroy(&ctx->io_buffer_idr); | |
7814 | } | |
7815 | ||
2b188cc1 JA |
7816 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
7817 | { | |
6b06314c | 7818 | io_finish_async(ctx); |
5dbcad51 | 7819 | io_sqe_buffer_unregister(ctx); |
30975825 | 7820 | if (ctx->sqo_mm) { |
2b188cc1 | 7821 | mmdrop(ctx->sqo_mm); |
30975825 BM |
7822 | ctx->sqo_mm = NULL; |
7823 | } | |
def596e9 | 7824 | |
6b06314c | 7825 | io_sqe_files_unregister(ctx); |
9b402849 | 7826 | io_eventfd_unregister(ctx); |
5a2e745d | 7827 | io_destroy_buffers(ctx); |
41726c9a | 7828 | idr_destroy(&ctx->personality_idr); |
def596e9 | 7829 | |
2b188cc1 | 7830 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
7831 | if (ctx->ring_sock) { |
7832 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 7833 | sock_release(ctx->ring_sock); |
355e8d26 | 7834 | } |
2b188cc1 JA |
7835 | #endif |
7836 | ||
75b28aff | 7837 | io_mem_free(ctx->rings); |
2b188cc1 | 7838 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
7839 | |
7840 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 7841 | free_uid(ctx->user); |
181e448d | 7842 | put_cred(ctx->creds); |
78076bb6 | 7843 | kfree(ctx->cancel_hash); |
0ddf92e8 | 7844 | kmem_cache_free(req_cachep, ctx->fallback_req); |
2b188cc1 JA |
7845 | kfree(ctx); |
7846 | } | |
7847 | ||
7848 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
7849 | { | |
7850 | struct io_ring_ctx *ctx = file->private_data; | |
7851 | __poll_t mask = 0; | |
7852 | ||
7853 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
7854 | /* |
7855 | * synchronizes with barrier from wq_has_sleeper call in | |
7856 | * io_commit_cqring | |
7857 | */ | |
2b188cc1 | 7858 | smp_rmb(); |
75b28aff HV |
7859 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
7860 | ctx->rings->sq_ring_entries) | |
2b188cc1 | 7861 | mask |= EPOLLOUT | EPOLLWRNORM; |
63e5d81f | 7862 | if (io_cqring_events(ctx, false)) |
2b188cc1 JA |
7863 | mask |= EPOLLIN | EPOLLRDNORM; |
7864 | ||
7865 | return mask; | |
7866 | } | |
7867 | ||
7868 | static int io_uring_fasync(int fd, struct file *file, int on) | |
7869 | { | |
7870 | struct io_ring_ctx *ctx = file->private_data; | |
7871 | ||
7872 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
7873 | } | |
7874 | ||
071698e1 JA |
7875 | static int io_remove_personalities(int id, void *p, void *data) |
7876 | { | |
7877 | struct io_ring_ctx *ctx = data; | |
7878 | const struct cred *cred; | |
7879 | ||
7880 | cred = idr_remove(&ctx->personality_idr, id); | |
7881 | if (cred) | |
7882 | put_cred(cred); | |
7883 | return 0; | |
7884 | } | |
7885 | ||
85faa7b8 JA |
7886 | static void io_ring_exit_work(struct work_struct *work) |
7887 | { | |
b2edc0a7 PB |
7888 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
7889 | exit_work); | |
85faa7b8 | 7890 | |
56952e91 JA |
7891 | /* |
7892 | * If we're doing polled IO and end up having requests being | |
7893 | * submitted async (out-of-line), then completions can come in while | |
7894 | * we're waiting for refs to drop. We need to reap these manually, | |
7895 | * as nobody else will be looking for them. | |
7896 | */ | |
b2edc0a7 | 7897 | do { |
56952e91 JA |
7898 | if (ctx->rings) |
7899 | io_cqring_overflow_flush(ctx, true); | |
b2edc0a7 PB |
7900 | io_iopoll_try_reap_events(ctx); |
7901 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); | |
85faa7b8 JA |
7902 | io_ring_ctx_free(ctx); |
7903 | } | |
7904 | ||
2b188cc1 JA |
7905 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
7906 | { | |
7907 | mutex_lock(&ctx->uring_lock); | |
7908 | percpu_ref_kill(&ctx->refs); | |
7909 | mutex_unlock(&ctx->uring_lock); | |
7910 | ||
5262f567 | 7911 | io_kill_timeouts(ctx); |
221c5eb2 | 7912 | io_poll_remove_all(ctx); |
561fb04a JA |
7913 | |
7914 | if (ctx->io_wq) | |
7915 | io_wq_cancel_all(ctx->io_wq); | |
7916 | ||
15dff286 JA |
7917 | /* if we failed setting up the ctx, we might not have any rings */ |
7918 | if (ctx->rings) | |
7919 | io_cqring_overflow_flush(ctx, true); | |
b2edc0a7 | 7920 | io_iopoll_try_reap_events(ctx); |
071698e1 | 7921 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
309fc03a JA |
7922 | |
7923 | /* | |
7924 | * Do this upfront, so we won't have a grace period where the ring | |
7925 | * is closed but resources aren't reaped yet. This can cause | |
7926 | * spurious failure in setting up a new ring. | |
7927 | */ | |
760618f7 JA |
7928 | io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries), |
7929 | ACCT_LOCKED); | |
309fc03a | 7930 | |
85faa7b8 JA |
7931 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
7932 | queue_work(system_wq, &ctx->exit_work); | |
2b188cc1 JA |
7933 | } |
7934 | ||
7935 | static int io_uring_release(struct inode *inode, struct file *file) | |
7936 | { | |
7937 | struct io_ring_ctx *ctx = file->private_data; | |
7938 | ||
7939 | file->private_data = NULL; | |
7940 | io_ring_ctx_wait_and_kill(ctx); | |
7941 | return 0; | |
7942 | } | |
7943 | ||
67c4d9e6 PB |
7944 | static bool io_wq_files_match(struct io_wq_work *work, void *data) |
7945 | { | |
7946 | struct files_struct *files = data; | |
7947 | ||
7948 | return work->files == files; | |
7949 | } | |
7950 | ||
f254ac04 JA |
7951 | /* |
7952 | * Returns true if 'preq' is the link parent of 'req' | |
7953 | */ | |
7954 | static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req) | |
7955 | { | |
7956 | struct io_kiocb *link; | |
7957 | ||
7958 | if (!(preq->flags & REQ_F_LINK_HEAD)) | |
7959 | return false; | |
7960 | ||
7961 | list_for_each_entry(link, &preq->link_list, link_list) { | |
7962 | if (link == req) | |
7963 | return true; | |
7964 | } | |
7965 | ||
7966 | return false; | |
7967 | } | |
7968 | ||
7969 | /* | |
7970 | * We're looking to cancel 'req' because it's holding on to our files, but | |
7971 | * 'req' could be a link to another request. See if it is, and cancel that | |
7972 | * parent request if so. | |
7973 | */ | |
7974 | static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req) | |
7975 | { | |
7976 | struct hlist_node *tmp; | |
7977 | struct io_kiocb *preq; | |
7978 | bool found = false; | |
7979 | int i; | |
7980 | ||
7981 | spin_lock_irq(&ctx->completion_lock); | |
7982 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
7983 | struct hlist_head *list; | |
7984 | ||
7985 | list = &ctx->cancel_hash[i]; | |
7986 | hlist_for_each_entry_safe(preq, tmp, list, hash_node) { | |
7987 | found = io_match_link(preq, req); | |
7988 | if (found) { | |
7989 | io_poll_remove_one(preq); | |
7990 | break; | |
7991 | } | |
7992 | } | |
7993 | } | |
7994 | spin_unlock_irq(&ctx->completion_lock); | |
7995 | return found; | |
7996 | } | |
7997 | ||
7998 | static bool io_timeout_remove_link(struct io_ring_ctx *ctx, | |
7999 | struct io_kiocb *req) | |
8000 | { | |
8001 | struct io_kiocb *preq; | |
8002 | bool found = false; | |
8003 | ||
8004 | spin_lock_irq(&ctx->completion_lock); | |
8005 | list_for_each_entry(preq, &ctx->timeout_list, timeout.list) { | |
8006 | found = io_match_link(preq, req); | |
8007 | if (found) { | |
8008 | __io_timeout_cancel(preq); | |
8009 | break; | |
8010 | } | |
8011 | } | |
8012 | spin_unlock_irq(&ctx->completion_lock); | |
8013 | return found; | |
8014 | } | |
8015 | ||
fcb323cc JA |
8016 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
8017 | struct files_struct *files) | |
8018 | { | |
67c4d9e6 PB |
8019 | if (list_empty_careful(&ctx->inflight_list)) |
8020 | return; | |
8021 | ||
8022 | /* cancel all at once, should be faster than doing it one by one*/ | |
8023 | io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true); | |
8024 | ||
fcb323cc | 8025 | while (!list_empty_careful(&ctx->inflight_list)) { |
d8f1b971 XW |
8026 | struct io_kiocb *cancel_req = NULL, *req; |
8027 | DEFINE_WAIT(wait); | |
fcb323cc JA |
8028 | |
8029 | spin_lock_irq(&ctx->inflight_lock); | |
8030 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { | |
768134d4 JA |
8031 | if (req->work.files != files) |
8032 | continue; | |
8033 | /* req is being completed, ignore */ | |
8034 | if (!refcount_inc_not_zero(&req->refs)) | |
8035 | continue; | |
8036 | cancel_req = req; | |
8037 | break; | |
fcb323cc | 8038 | } |
768134d4 | 8039 | if (cancel_req) |
fcb323cc | 8040 | prepare_to_wait(&ctx->inflight_wait, &wait, |
768134d4 | 8041 | TASK_UNINTERRUPTIBLE); |
fcb323cc JA |
8042 | spin_unlock_irq(&ctx->inflight_lock); |
8043 | ||
768134d4 JA |
8044 | /* We need to keep going until we don't find a matching req */ |
8045 | if (!cancel_req) | |
fcb323cc | 8046 | break; |
2f6d9b9d | 8047 | |
2ca10259 JA |
8048 | if (cancel_req->flags & REQ_F_OVERFLOW) { |
8049 | spin_lock_irq(&ctx->completion_lock); | |
40d8ddd4 | 8050 | list_del(&cancel_req->compl.list); |
2ca10259 | 8051 | cancel_req->flags &= ~REQ_F_OVERFLOW; |
2ca10259 | 8052 | |
46930143 | 8053 | io_cqring_mark_overflow(ctx); |
2ca10259 JA |
8054 | WRITE_ONCE(ctx->rings->cq_overflow, |
8055 | atomic_inc_return(&ctx->cached_cq_overflow)); | |
dd9dfcdf | 8056 | io_commit_cqring(ctx); |
b2bd1cf9 | 8057 | spin_unlock_irq(&ctx->completion_lock); |
2ca10259 JA |
8058 | |
8059 | /* | |
8060 | * Put inflight ref and overflow ref. If that's | |
8061 | * all we had, then we're done with this request. | |
8062 | */ | |
8063 | if (refcount_sub_and_test(2, &cancel_req->refs)) { | |
4518a3cc | 8064 | io_free_req(cancel_req); |
d8f1b971 | 8065 | finish_wait(&ctx->inflight_wait, &wait); |
2ca10259 JA |
8066 | continue; |
8067 | } | |
7b53d598 PB |
8068 | } else { |
8069 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); | |
f254ac04 JA |
8070 | /* could be a link, check and remove if it is */ |
8071 | if (!io_poll_remove_link(ctx, cancel_req)) | |
8072 | io_timeout_remove_link(ctx, cancel_req); | |
7b53d598 | 8073 | io_put_req(cancel_req); |
2ca10259 JA |
8074 | } |
8075 | ||
fcb323cc | 8076 | schedule(); |
d8f1b971 | 8077 | finish_wait(&ctx->inflight_wait, &wait); |
fcb323cc JA |
8078 | } |
8079 | } | |
8080 | ||
801dd57b | 8081 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
44e728b8 | 8082 | { |
801dd57b PB |
8083 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
8084 | struct task_struct *task = data; | |
44e728b8 | 8085 | |
801dd57b | 8086 | return req->task == task; |
44e728b8 PB |
8087 | } |
8088 | ||
fcb323cc JA |
8089 | static int io_uring_flush(struct file *file, void *data) |
8090 | { | |
8091 | struct io_ring_ctx *ctx = file->private_data; | |
8092 | ||
8093 | io_uring_cancel_files(ctx, data); | |
6ab23144 JA |
8094 | |
8095 | /* | |
8096 | * If the task is going away, cancel work it may have pending | |
8097 | */ | |
801dd57b PB |
8098 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) |
8099 | io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true); | |
6ab23144 | 8100 | |
fcb323cc JA |
8101 | return 0; |
8102 | } | |
8103 | ||
6c5c240e RP |
8104 | static void *io_uring_validate_mmap_request(struct file *file, |
8105 | loff_t pgoff, size_t sz) | |
2b188cc1 | 8106 | { |
2b188cc1 | 8107 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 8108 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
8109 | struct page *page; |
8110 | void *ptr; | |
8111 | ||
8112 | switch (offset) { | |
8113 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
8114 | case IORING_OFF_CQ_RING: |
8115 | ptr = ctx->rings; | |
2b188cc1 JA |
8116 | break; |
8117 | case IORING_OFF_SQES: | |
8118 | ptr = ctx->sq_sqes; | |
8119 | break; | |
2b188cc1 | 8120 | default: |
6c5c240e | 8121 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
8122 | } |
8123 | ||
8124 | page = virt_to_head_page(ptr); | |
a50b854e | 8125 | if (sz > page_size(page)) |
6c5c240e RP |
8126 | return ERR_PTR(-EINVAL); |
8127 | ||
8128 | return ptr; | |
8129 | } | |
8130 | ||
8131 | #ifdef CONFIG_MMU | |
8132 | ||
8133 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
8134 | { | |
8135 | size_t sz = vma->vm_end - vma->vm_start; | |
8136 | unsigned long pfn; | |
8137 | void *ptr; | |
8138 | ||
8139 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
8140 | if (IS_ERR(ptr)) | |
8141 | return PTR_ERR(ptr); | |
2b188cc1 JA |
8142 | |
8143 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
8144 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
8145 | } | |
8146 | ||
6c5c240e RP |
8147 | #else /* !CONFIG_MMU */ |
8148 | ||
8149 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
8150 | { | |
8151 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
8152 | } | |
8153 | ||
8154 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
8155 | { | |
8156 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
8157 | } | |
8158 | ||
8159 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
8160 | unsigned long addr, unsigned long len, | |
8161 | unsigned long pgoff, unsigned long flags) | |
8162 | { | |
8163 | void *ptr; | |
8164 | ||
8165 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
8166 | if (IS_ERR(ptr)) | |
8167 | return PTR_ERR(ptr); | |
8168 | ||
8169 | return (unsigned long) ptr; | |
8170 | } | |
8171 | ||
8172 | #endif /* !CONFIG_MMU */ | |
8173 | ||
2b188cc1 JA |
8174 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
8175 | u32, min_complete, u32, flags, const sigset_t __user *, sig, | |
8176 | size_t, sigsz) | |
8177 | { | |
8178 | struct io_ring_ctx *ctx; | |
8179 | long ret = -EBADF; | |
8180 | int submitted = 0; | |
8181 | struct fd f; | |
8182 | ||
4c6e277c | 8183 | io_run_task_work(); |
b41e9852 | 8184 | |
6c271ce2 | 8185 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
2b188cc1 JA |
8186 | return -EINVAL; |
8187 | ||
8188 | f = fdget(fd); | |
8189 | if (!f.file) | |
8190 | return -EBADF; | |
8191 | ||
8192 | ret = -EOPNOTSUPP; | |
8193 | if (f.file->f_op != &io_uring_fops) | |
8194 | goto out_fput; | |
8195 | ||
8196 | ret = -ENXIO; | |
8197 | ctx = f.file->private_data; | |
8198 | if (!percpu_ref_tryget(&ctx->refs)) | |
8199 | goto out_fput; | |
8200 | ||
6c271ce2 JA |
8201 | /* |
8202 | * For SQ polling, the thread will do all submissions and completions. | |
8203 | * Just return the requested submit count, and wake the thread if | |
8204 | * we were asked to. | |
8205 | */ | |
b2a9eada | 8206 | ret = 0; |
6c271ce2 | 8207 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
c1edbf5f JA |
8208 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
8209 | io_cqring_overflow_flush(ctx, false); | |
6c271ce2 JA |
8210 | if (flags & IORING_ENTER_SQ_WAKEUP) |
8211 | wake_up(&ctx->sqo_wait); | |
8212 | submitted = to_submit; | |
b2a9eada | 8213 | } else if (to_submit) { |
2b188cc1 | 8214 | mutex_lock(&ctx->uring_lock); |
0cdaf760 | 8215 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd); |
2b188cc1 | 8216 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
8217 | |
8218 | if (submitted != to_submit) | |
8219 | goto out; | |
2b188cc1 JA |
8220 | } |
8221 | if (flags & IORING_ENTER_GETEVENTS) { | |
8222 | min_complete = min(min_complete, ctx->cq_entries); | |
8223 | ||
32b2244a XW |
8224 | /* |
8225 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
8226 | * space applications don't need to do io completion events | |
8227 | * polling again, they can rely on io_sq_thread to do polling | |
8228 | * work, which can reduce cpu usage and uring_lock contention. | |
8229 | */ | |
8230 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
8231 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 8232 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 JA |
8233 | } else { |
8234 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); | |
8235 | } | |
2b188cc1 JA |
8236 | } |
8237 | ||
7c504e65 | 8238 | out: |
6805b32e | 8239 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
8240 | out_fput: |
8241 | fdput(f); | |
8242 | return submitted ? submitted : ret; | |
8243 | } | |
8244 | ||
bebdb65e | 8245 | #ifdef CONFIG_PROC_FS |
87ce955b JA |
8246 | static int io_uring_show_cred(int id, void *p, void *data) |
8247 | { | |
8248 | const struct cred *cred = p; | |
8249 | struct seq_file *m = data; | |
8250 | struct user_namespace *uns = seq_user_ns(m); | |
8251 | struct group_info *gi; | |
8252 | kernel_cap_t cap; | |
8253 | unsigned __capi; | |
8254 | int g; | |
8255 | ||
8256 | seq_printf(m, "%5d\n", id); | |
8257 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
8258 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
8259 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
8260 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
8261 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
8262 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
8263 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
8264 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
8265 | seq_puts(m, "\n\tGroups:\t"); | |
8266 | gi = cred->group_info; | |
8267 | for (g = 0; g < gi->ngroups; g++) { | |
8268 | seq_put_decimal_ull(m, g ? " " : "", | |
8269 | from_kgid_munged(uns, gi->gid[g])); | |
8270 | } | |
8271 | seq_puts(m, "\n\tCapEff:\t"); | |
8272 | cap = cred->cap_effective; | |
8273 | CAP_FOR_EACH_U32(__capi) | |
8274 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
8275 | seq_putc(m, '\n'); | |
8276 | return 0; | |
8277 | } | |
8278 | ||
8279 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
8280 | { | |
8281 | int i; | |
8282 | ||
8283 | mutex_lock(&ctx->uring_lock); | |
8284 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); | |
8285 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8286 | struct fixed_file_table *table; | |
8287 | struct file *f; | |
8288 | ||
8289 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
8290 | f = table->files[i & IORING_FILE_TABLE_MASK]; | |
8291 | if (f) | |
8292 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
8293 | else | |
8294 | seq_printf(m, "%5u: <none>\n", i); | |
8295 | } | |
8296 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
8297 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8298 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; | |
8299 | ||
8300 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, | |
8301 | (unsigned int) buf->len); | |
8302 | } | |
8303 | if (!idr_is_empty(&ctx->personality_idr)) { | |
8304 | seq_printf(m, "Personalities:\n"); | |
8305 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); | |
8306 | } | |
d7718a9d JA |
8307 | seq_printf(m, "PollList:\n"); |
8308 | spin_lock_irq(&ctx->completion_lock); | |
8309 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
8310 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
8311 | struct io_kiocb *req; | |
8312 | ||
8313 | hlist_for_each_entry(req, list, hash_node) | |
8314 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
8315 | req->task->task_works != NULL); | |
8316 | } | |
8317 | spin_unlock_irq(&ctx->completion_lock); | |
87ce955b JA |
8318 | mutex_unlock(&ctx->uring_lock); |
8319 | } | |
8320 | ||
8321 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
8322 | { | |
8323 | struct io_ring_ctx *ctx = f->private_data; | |
8324 | ||
8325 | if (percpu_ref_tryget(&ctx->refs)) { | |
8326 | __io_uring_show_fdinfo(ctx, m); | |
8327 | percpu_ref_put(&ctx->refs); | |
8328 | } | |
8329 | } | |
bebdb65e | 8330 | #endif |
87ce955b | 8331 | |
2b188cc1 JA |
8332 | static const struct file_operations io_uring_fops = { |
8333 | .release = io_uring_release, | |
fcb323cc | 8334 | .flush = io_uring_flush, |
2b188cc1 | 8335 | .mmap = io_uring_mmap, |
6c5c240e RP |
8336 | #ifndef CONFIG_MMU |
8337 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
8338 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
8339 | #endif | |
2b188cc1 JA |
8340 | .poll = io_uring_poll, |
8341 | .fasync = io_uring_fasync, | |
bebdb65e | 8342 | #ifdef CONFIG_PROC_FS |
87ce955b | 8343 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 8344 | #endif |
2b188cc1 JA |
8345 | }; |
8346 | ||
8347 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
8348 | struct io_uring_params *p) | |
8349 | { | |
75b28aff HV |
8350 | struct io_rings *rings; |
8351 | size_t size, sq_array_offset; | |
2b188cc1 | 8352 | |
bd740481 JA |
8353 | /* make sure these are sane, as we already accounted them */ |
8354 | ctx->sq_entries = p->sq_entries; | |
8355 | ctx->cq_entries = p->cq_entries; | |
8356 | ||
75b28aff HV |
8357 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
8358 | if (size == SIZE_MAX) | |
8359 | return -EOVERFLOW; | |
8360 | ||
8361 | rings = io_mem_alloc(size); | |
8362 | if (!rings) | |
2b188cc1 JA |
8363 | return -ENOMEM; |
8364 | ||
75b28aff HV |
8365 | ctx->rings = rings; |
8366 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
8367 | rings->sq_ring_mask = p->sq_entries - 1; | |
8368 | rings->cq_ring_mask = p->cq_entries - 1; | |
8369 | rings->sq_ring_entries = p->sq_entries; | |
8370 | rings->cq_ring_entries = p->cq_entries; | |
8371 | ctx->sq_mask = rings->sq_ring_mask; | |
8372 | ctx->cq_mask = rings->cq_ring_mask; | |
2b188cc1 JA |
8373 | |
8374 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
8375 | if (size == SIZE_MAX) { |
8376 | io_mem_free(ctx->rings); | |
8377 | ctx->rings = NULL; | |
2b188cc1 | 8378 | return -EOVERFLOW; |
eb065d30 | 8379 | } |
2b188cc1 JA |
8380 | |
8381 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
8382 | if (!ctx->sq_sqes) { |
8383 | io_mem_free(ctx->rings); | |
8384 | ctx->rings = NULL; | |
2b188cc1 | 8385 | return -ENOMEM; |
eb065d30 | 8386 | } |
2b188cc1 | 8387 | |
2b188cc1 JA |
8388 | return 0; |
8389 | } | |
8390 | ||
8391 | /* | |
8392 | * Allocate an anonymous fd, this is what constitutes the application | |
8393 | * visible backing of an io_uring instance. The application mmaps this | |
8394 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
8395 | * we have to tie this fd to a socket for file garbage collection purposes. | |
8396 | */ | |
8397 | static int io_uring_get_fd(struct io_ring_ctx *ctx) | |
8398 | { | |
8399 | struct file *file; | |
8400 | int ret; | |
8401 | ||
8402 | #if defined(CONFIG_UNIX) | |
8403 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, | |
8404 | &ctx->ring_sock); | |
8405 | if (ret) | |
8406 | return ret; | |
8407 | #endif | |
8408 | ||
8409 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
8410 | if (ret < 0) | |
8411 | goto err; | |
8412 | ||
8413 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, | |
8414 | O_RDWR | O_CLOEXEC); | |
8415 | if (IS_ERR(file)) { | |
8416 | put_unused_fd(ret); | |
8417 | ret = PTR_ERR(file); | |
8418 | goto err; | |
8419 | } | |
8420 | ||
8421 | #if defined(CONFIG_UNIX) | |
8422 | ctx->ring_sock->file = file; | |
8423 | #endif | |
8424 | fd_install(ret, file); | |
8425 | return ret; | |
8426 | err: | |
8427 | #if defined(CONFIG_UNIX) | |
8428 | sock_release(ctx->ring_sock); | |
8429 | ctx->ring_sock = NULL; | |
8430 | #endif | |
8431 | return ret; | |
8432 | } | |
8433 | ||
7f13657d XW |
8434 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
8435 | struct io_uring_params __user *params) | |
2b188cc1 JA |
8436 | { |
8437 | struct user_struct *user = NULL; | |
8438 | struct io_ring_ctx *ctx; | |
aad5d8da | 8439 | bool limit_mem; |
2b188cc1 JA |
8440 | int ret; |
8441 | ||
8110c1a6 | 8442 | if (!entries) |
2b188cc1 | 8443 | return -EINVAL; |
8110c1a6 JA |
8444 | if (entries > IORING_MAX_ENTRIES) { |
8445 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
8446 | return -EINVAL; | |
8447 | entries = IORING_MAX_ENTRIES; | |
8448 | } | |
2b188cc1 JA |
8449 | |
8450 | /* | |
8451 | * Use twice as many entries for the CQ ring. It's possible for the | |
8452 | * application to drive a higher depth than the size of the SQ ring, | |
8453 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
8454 | * some flexibility in overcommitting a bit. If the application has |
8455 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
8456 | * of CQ ring entries manually. | |
2b188cc1 JA |
8457 | */ |
8458 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
8459 | if (p->flags & IORING_SETUP_CQSIZE) { |
8460 | /* | |
8461 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
8462 | * to a power-of-two, if it isn't already. We do NOT impose | |
8463 | * any cq vs sq ring sizing. | |
8464 | */ | |
8110c1a6 | 8465 | if (p->cq_entries < p->sq_entries) |
33a107f0 | 8466 | return -EINVAL; |
8110c1a6 JA |
8467 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
8468 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
8469 | return -EINVAL; | |
8470 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
8471 | } | |
33a107f0 JA |
8472 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
8473 | } else { | |
8474 | p->cq_entries = 2 * p->sq_entries; | |
8475 | } | |
2b188cc1 JA |
8476 | |
8477 | user = get_uid(current_user()); | |
aad5d8da | 8478 | limit_mem = !capable(CAP_IPC_LOCK); |
2b188cc1 | 8479 | |
aad5d8da | 8480 | if (limit_mem) { |
a087e2b5 | 8481 | ret = __io_account_mem(user, |
2b188cc1 JA |
8482 | ring_pages(p->sq_entries, p->cq_entries)); |
8483 | if (ret) { | |
8484 | free_uid(user); | |
8485 | return ret; | |
8486 | } | |
8487 | } | |
8488 | ||
8489 | ctx = io_ring_ctx_alloc(p); | |
8490 | if (!ctx) { | |
aad5d8da | 8491 | if (limit_mem) |
a087e2b5 | 8492 | __io_unaccount_mem(user, ring_pages(p->sq_entries, |
2b188cc1 JA |
8493 | p->cq_entries)); |
8494 | free_uid(user); | |
8495 | return -ENOMEM; | |
8496 | } | |
8497 | ctx->compat = in_compat_syscall(); | |
2b188cc1 | 8498 | ctx->user = user; |
0b8c0ec7 | 8499 | ctx->creds = get_current_cred(); |
2b188cc1 | 8500 | |
f74441e6 JA |
8501 | /* |
8502 | * Account memory _before_ installing the file descriptor. Once | |
8503 | * the descriptor is installed, it can get closed at any time. Also | |
8504 | * do this before hitting the general error path, as ring freeing | |
8505 | * will un-account as well. | |
8506 | */ | |
8507 | io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries), | |
8508 | ACCT_LOCKED); | |
8509 | ctx->limit_mem = limit_mem; | |
8510 | ||
2b188cc1 JA |
8511 | ret = io_allocate_scq_urings(ctx, p); |
8512 | if (ret) | |
8513 | goto err; | |
8514 | ||
6c271ce2 | 8515 | ret = io_sq_offload_start(ctx, p); |
2b188cc1 JA |
8516 | if (ret) |
8517 | goto err; | |
8518 | ||
2b188cc1 | 8519 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
8520 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
8521 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
8522 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
8523 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
8524 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
8525 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
8526 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
8527 | |
8528 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
8529 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
8530 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
8531 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
8532 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
8533 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
8534 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 8535 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 8536 | |
7f13657d XW |
8537 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
8538 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 JX |
8539 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
8540 | IORING_FEAT_POLL_32BITS; | |
7f13657d XW |
8541 | |
8542 | if (copy_to_user(params, p, sizeof(*p))) { | |
8543 | ret = -EFAULT; | |
8544 | goto err; | |
8545 | } | |
d1719f70 | 8546 | |
044c1ab3 JA |
8547 | /* |
8548 | * Install ring fd as the very last thing, so we don't risk someone | |
8549 | * having closed it before we finish setup | |
8550 | */ | |
8551 | ret = io_uring_get_fd(ctx); | |
8552 | if (ret < 0) | |
8553 | goto err; | |
8554 | ||
c826bd7a | 8555 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
8556 | return ret; |
8557 | err: | |
8558 | io_ring_ctx_wait_and_kill(ctx); | |
8559 | return ret; | |
8560 | } | |
8561 | ||
8562 | /* | |
8563 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
8564 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
8565 | * params structure passed in. | |
8566 | */ | |
8567 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
8568 | { | |
8569 | struct io_uring_params p; | |
2b188cc1 JA |
8570 | int i; |
8571 | ||
8572 | if (copy_from_user(&p, params, sizeof(p))) | |
8573 | return -EFAULT; | |
8574 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
8575 | if (p.resv[i]) | |
8576 | return -EINVAL; | |
8577 | } | |
8578 | ||
6c271ce2 | 8579 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 8580 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
24369c2e | 8581 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ)) |
2b188cc1 JA |
8582 | return -EINVAL; |
8583 | ||
7f13657d | 8584 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
8585 | } |
8586 | ||
8587 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
8588 | struct io_uring_params __user *, params) | |
8589 | { | |
8590 | return io_uring_setup(entries, params); | |
8591 | } | |
8592 | ||
66f4af93 JA |
8593 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
8594 | { | |
8595 | struct io_uring_probe *p; | |
8596 | size_t size; | |
8597 | int i, ret; | |
8598 | ||
8599 | size = struct_size(p, ops, nr_args); | |
8600 | if (size == SIZE_MAX) | |
8601 | return -EOVERFLOW; | |
8602 | p = kzalloc(size, GFP_KERNEL); | |
8603 | if (!p) | |
8604 | return -ENOMEM; | |
8605 | ||
8606 | ret = -EFAULT; | |
8607 | if (copy_from_user(p, arg, size)) | |
8608 | goto out; | |
8609 | ret = -EINVAL; | |
8610 | if (memchr_inv(p, 0, size)) | |
8611 | goto out; | |
8612 | ||
8613 | p->last_op = IORING_OP_LAST - 1; | |
8614 | if (nr_args > IORING_OP_LAST) | |
8615 | nr_args = IORING_OP_LAST; | |
8616 | ||
8617 | for (i = 0; i < nr_args; i++) { | |
8618 | p->ops[i].op = i; | |
8619 | if (!io_op_defs[i].not_supported) | |
8620 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
8621 | } | |
8622 | p->ops_len = i; | |
8623 | ||
8624 | ret = 0; | |
8625 | if (copy_to_user(arg, p, size)) | |
8626 | ret = -EFAULT; | |
8627 | out: | |
8628 | kfree(p); | |
8629 | return ret; | |
8630 | } | |
8631 | ||
071698e1 JA |
8632 | static int io_register_personality(struct io_ring_ctx *ctx) |
8633 | { | |
8634 | const struct cred *creds = get_current_cred(); | |
8635 | int id; | |
8636 | ||
8637 | id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, | |
8638 | USHRT_MAX, GFP_KERNEL); | |
8639 | if (id < 0) | |
8640 | put_cred(creds); | |
8641 | return id; | |
8642 | } | |
8643 | ||
8644 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) | |
8645 | { | |
8646 | const struct cred *old_creds; | |
8647 | ||
8648 | old_creds = idr_remove(&ctx->personality_idr, id); | |
8649 | if (old_creds) { | |
8650 | put_cred(old_creds); | |
8651 | return 0; | |
8652 | } | |
8653 | ||
8654 | return -EINVAL; | |
8655 | } | |
8656 | ||
8657 | static bool io_register_op_must_quiesce(int op) | |
8658 | { | |
8659 | switch (op) { | |
8660 | case IORING_UNREGISTER_FILES: | |
8661 | case IORING_REGISTER_FILES_UPDATE: | |
8662 | case IORING_REGISTER_PROBE: | |
8663 | case IORING_REGISTER_PERSONALITY: | |
8664 | case IORING_UNREGISTER_PERSONALITY: | |
8665 | return false; | |
8666 | default: | |
8667 | return true; | |
8668 | } | |
8669 | } | |
8670 | ||
edafccee JA |
8671 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
8672 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
8673 | __releases(ctx->uring_lock) |
8674 | __acquires(ctx->uring_lock) | |
edafccee JA |
8675 | { |
8676 | int ret; | |
8677 | ||
35fa71a0 JA |
8678 | /* |
8679 | * We're inside the ring mutex, if the ref is already dying, then | |
8680 | * someone else killed the ctx or is already going through | |
8681 | * io_uring_register(). | |
8682 | */ | |
8683 | if (percpu_ref_is_dying(&ctx->refs)) | |
8684 | return -ENXIO; | |
8685 | ||
071698e1 | 8686 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 8687 | percpu_ref_kill(&ctx->refs); |
b19062a5 | 8688 | |
05f3fb3c JA |
8689 | /* |
8690 | * Drop uring mutex before waiting for references to exit. If | |
8691 | * another thread is currently inside io_uring_enter() it might | |
8692 | * need to grab the uring_lock to make progress. If we hold it | |
8693 | * here across the drain wait, then we can deadlock. It's safe | |
8694 | * to drop the mutex here, since no new references will come in | |
8695 | * after we've killed the percpu ref. | |
8696 | */ | |
8697 | mutex_unlock(&ctx->uring_lock); | |
0f158b4c | 8698 | ret = wait_for_completion_interruptible(&ctx->ref_comp); |
05f3fb3c | 8699 | mutex_lock(&ctx->uring_lock); |
c150368b JA |
8700 | if (ret) { |
8701 | percpu_ref_resurrect(&ctx->refs); | |
8702 | ret = -EINTR; | |
8703 | goto out; | |
8704 | } | |
05f3fb3c | 8705 | } |
edafccee JA |
8706 | |
8707 | switch (opcode) { | |
8708 | case IORING_REGISTER_BUFFERS: | |
8709 | ret = io_sqe_buffer_register(ctx, arg, nr_args); | |
8710 | break; | |
8711 | case IORING_UNREGISTER_BUFFERS: | |
8712 | ret = -EINVAL; | |
8713 | if (arg || nr_args) | |
8714 | break; | |
8715 | ret = io_sqe_buffer_unregister(ctx); | |
8716 | break; | |
6b06314c JA |
8717 | case IORING_REGISTER_FILES: |
8718 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
8719 | break; | |
8720 | case IORING_UNREGISTER_FILES: | |
8721 | ret = -EINVAL; | |
8722 | if (arg || nr_args) | |
8723 | break; | |
8724 | ret = io_sqe_files_unregister(ctx); | |
8725 | break; | |
c3a31e60 JA |
8726 | case IORING_REGISTER_FILES_UPDATE: |
8727 | ret = io_sqe_files_update(ctx, arg, nr_args); | |
8728 | break; | |
9b402849 | 8729 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 8730 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
8731 | ret = -EINVAL; |
8732 | if (nr_args != 1) | |
8733 | break; | |
8734 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
8735 | if (ret) |
8736 | break; | |
8737 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
8738 | ctx->eventfd_async = 1; | |
8739 | else | |
8740 | ctx->eventfd_async = 0; | |
9b402849 JA |
8741 | break; |
8742 | case IORING_UNREGISTER_EVENTFD: | |
8743 | ret = -EINVAL; | |
8744 | if (arg || nr_args) | |
8745 | break; | |
8746 | ret = io_eventfd_unregister(ctx); | |
8747 | break; | |
66f4af93 JA |
8748 | case IORING_REGISTER_PROBE: |
8749 | ret = -EINVAL; | |
8750 | if (!arg || nr_args > 256) | |
8751 | break; | |
8752 | ret = io_probe(ctx, arg, nr_args); | |
8753 | break; | |
071698e1 JA |
8754 | case IORING_REGISTER_PERSONALITY: |
8755 | ret = -EINVAL; | |
8756 | if (arg || nr_args) | |
8757 | break; | |
8758 | ret = io_register_personality(ctx); | |
8759 | break; | |
8760 | case IORING_UNREGISTER_PERSONALITY: | |
8761 | ret = -EINVAL; | |
8762 | if (arg) | |
8763 | break; | |
8764 | ret = io_unregister_personality(ctx, nr_args); | |
8765 | break; | |
edafccee JA |
8766 | default: |
8767 | ret = -EINVAL; | |
8768 | break; | |
8769 | } | |
8770 | ||
071698e1 | 8771 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 8772 | /* bring the ctx back to life */ |
05f3fb3c | 8773 | percpu_ref_reinit(&ctx->refs); |
c150368b | 8774 | out: |
0f158b4c | 8775 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 8776 | } |
edafccee JA |
8777 | return ret; |
8778 | } | |
8779 | ||
8780 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
8781 | void __user *, arg, unsigned int, nr_args) | |
8782 | { | |
8783 | struct io_ring_ctx *ctx; | |
8784 | long ret = -EBADF; | |
8785 | struct fd f; | |
8786 | ||
8787 | f = fdget(fd); | |
8788 | if (!f.file) | |
8789 | return -EBADF; | |
8790 | ||
8791 | ret = -EOPNOTSUPP; | |
8792 | if (f.file->f_op != &io_uring_fops) | |
8793 | goto out_fput; | |
8794 | ||
8795 | ctx = f.file->private_data; | |
8796 | ||
8797 | mutex_lock(&ctx->uring_lock); | |
8798 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
8799 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
8800 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
8801 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
8802 | out_fput: |
8803 | fdput(f); | |
8804 | return ret; | |
8805 | } | |
8806 | ||
2b188cc1 JA |
8807 | static int __init io_uring_init(void) |
8808 | { | |
d7f62e82 SM |
8809 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
8810 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
8811 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
8812 | } while (0) | |
8813 | ||
8814 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
8815 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
8816 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
8817 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
8818 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
8819 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
8820 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
8821 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
8822 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
8823 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 8824 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
8825 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
8826 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
8827 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
8828 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
8829 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
8830 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
8831 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
8832 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
8833 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
8834 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
8835 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
8836 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
8837 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
8838 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
8839 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 8840 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
8841 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
8842 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
8843 | BUILD_BUG_SQE_ELEM(42, __u16, personality); | |
7d67af2c | 8844 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
d7f62e82 | 8845 | |
d3656344 | 8846 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 8847 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
2b188cc1 JA |
8848 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
8849 | return 0; | |
8850 | }; | |
8851 | __initcall(io_uring_init); |