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