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