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