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> | |
47 | #include <linux/refcount.h> | |
48 | #include <linux/uio.h> | |
6b47ee6e | 49 | #include <linux/bits.h> |
2b188cc1 JA |
50 | |
51 | #include <linux/sched/signal.h> | |
52 | #include <linux/fs.h> | |
53 | #include <linux/file.h> | |
54 | #include <linux/fdtable.h> | |
55 | #include <linux/mm.h> | |
56 | #include <linux/mman.h> | |
57 | #include <linux/mmu_context.h> | |
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> |
2b188cc1 | 77 | |
c826bd7a DD |
78 | #define CREATE_TRACE_POINTS |
79 | #include <trace/events/io_uring.h> | |
80 | ||
2b188cc1 JA |
81 | #include <uapi/linux/io_uring.h> |
82 | ||
83 | #include "internal.h" | |
561fb04a | 84 | #include "io-wq.h" |
2b188cc1 | 85 | |
5277deaa | 86 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 87 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
65e19f54 JA |
88 | |
89 | /* | |
90 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs | |
91 | */ | |
92 | #define IORING_FILE_TABLE_SHIFT 9 | |
93 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) | |
94 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) | |
95 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) | |
2b188cc1 JA |
96 | |
97 | struct io_uring { | |
98 | u32 head ____cacheline_aligned_in_smp; | |
99 | u32 tail ____cacheline_aligned_in_smp; | |
100 | }; | |
101 | ||
1e84b97b | 102 | /* |
75b28aff HV |
103 | * This data is shared with the application through the mmap at offsets |
104 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
105 | * |
106 | * The offsets to the member fields are published through struct | |
107 | * io_sqring_offsets when calling io_uring_setup. | |
108 | */ | |
75b28aff | 109 | struct io_rings { |
1e84b97b SB |
110 | /* |
111 | * Head and tail offsets into the ring; the offsets need to be | |
112 | * masked to get valid indices. | |
113 | * | |
75b28aff HV |
114 | * The kernel controls head of the sq ring and the tail of the cq ring, |
115 | * and the application controls tail of the sq ring and the head of the | |
116 | * cq ring. | |
1e84b97b | 117 | */ |
75b28aff | 118 | struct io_uring sq, cq; |
1e84b97b | 119 | /* |
75b28aff | 120 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
121 | * ring_entries - 1) |
122 | */ | |
75b28aff HV |
123 | u32 sq_ring_mask, cq_ring_mask; |
124 | /* Ring sizes (constant, power of 2) */ | |
125 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
126 | /* |
127 | * Number of invalid entries dropped by the kernel due to | |
128 | * invalid index stored in array | |
129 | * | |
130 | * Written by the kernel, shouldn't be modified by the | |
131 | * application (i.e. get number of "new events" by comparing to | |
132 | * cached value). | |
133 | * | |
134 | * After a new SQ head value was read by the application this | |
135 | * counter includes all submissions that were dropped reaching | |
136 | * the new SQ head (and possibly more). | |
137 | */ | |
75b28aff | 138 | u32 sq_dropped; |
1e84b97b SB |
139 | /* |
140 | * Runtime flags | |
141 | * | |
142 | * Written by the kernel, shouldn't be modified by the | |
143 | * application. | |
144 | * | |
145 | * The application needs a full memory barrier before checking | |
146 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
147 | */ | |
75b28aff | 148 | u32 sq_flags; |
1e84b97b SB |
149 | /* |
150 | * Number of completion events lost because the queue was full; | |
151 | * this should be avoided by the application by making sure | |
0b4295b5 | 152 | * there are not more requests pending than there is space in |
1e84b97b SB |
153 | * the completion queue. |
154 | * | |
155 | * Written by the kernel, shouldn't be modified by the | |
156 | * application (i.e. get number of "new events" by comparing to | |
157 | * cached value). | |
158 | * | |
159 | * As completion events come in out of order this counter is not | |
160 | * ordered with any other data. | |
161 | */ | |
75b28aff | 162 | u32 cq_overflow; |
1e84b97b SB |
163 | /* |
164 | * Ring buffer of completion events. | |
165 | * | |
166 | * The kernel writes completion events fresh every time they are | |
167 | * produced, so the application is allowed to modify pending | |
168 | * entries. | |
169 | */ | |
75b28aff | 170 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
171 | }; |
172 | ||
edafccee JA |
173 | struct io_mapped_ubuf { |
174 | u64 ubuf; | |
175 | size_t len; | |
176 | struct bio_vec *bvec; | |
177 | unsigned int nr_bvecs; | |
178 | }; | |
179 | ||
65e19f54 JA |
180 | struct fixed_file_table { |
181 | struct file **files; | |
31b51510 JA |
182 | }; |
183 | ||
05f3fb3c JA |
184 | enum { |
185 | FFD_F_ATOMIC, | |
186 | }; | |
187 | ||
188 | struct fixed_file_data { | |
189 | struct fixed_file_table *table; | |
190 | struct io_ring_ctx *ctx; | |
191 | ||
192 | struct percpu_ref refs; | |
193 | struct llist_head put_llist; | |
194 | unsigned long state; | |
195 | struct work_struct ref_work; | |
196 | struct completion done; | |
197 | }; | |
198 | ||
2b188cc1 JA |
199 | struct io_ring_ctx { |
200 | struct { | |
201 | struct percpu_ref refs; | |
202 | } ____cacheline_aligned_in_smp; | |
203 | ||
204 | struct { | |
205 | unsigned int flags; | |
69b3e546 JA |
206 | int compat: 1; |
207 | int account_mem: 1; | |
208 | int cq_overflow_flushed: 1; | |
209 | int drain_next: 1; | |
f2842ab5 | 210 | int eventfd_async: 1; |
2b188cc1 | 211 | |
75b28aff HV |
212 | /* |
213 | * Ring buffer of indices into array of io_uring_sqe, which is | |
214 | * mmapped by the application using the IORING_OFF_SQES offset. | |
215 | * | |
216 | * This indirection could e.g. be used to assign fixed | |
217 | * io_uring_sqe entries to operations and only submit them to | |
218 | * the queue when needed. | |
219 | * | |
220 | * The kernel modifies neither the indices array nor the entries | |
221 | * array. | |
222 | */ | |
223 | u32 *sq_array; | |
2b188cc1 JA |
224 | unsigned cached_sq_head; |
225 | unsigned sq_entries; | |
226 | unsigned sq_mask; | |
6c271ce2 | 227 | unsigned sq_thread_idle; |
498ccd9e | 228 | unsigned cached_sq_dropped; |
206aefde | 229 | atomic_t cached_cq_overflow; |
ad3eb2c8 | 230 | unsigned long sq_check_overflow; |
de0617e4 JA |
231 | |
232 | struct list_head defer_list; | |
5262f567 | 233 | struct list_head timeout_list; |
1d7bb1d5 | 234 | struct list_head cq_overflow_list; |
fcb323cc JA |
235 | |
236 | wait_queue_head_t inflight_wait; | |
ad3eb2c8 | 237 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
238 | } ____cacheline_aligned_in_smp; |
239 | ||
206aefde JA |
240 | struct io_rings *rings; |
241 | ||
2b188cc1 | 242 | /* IO offload */ |
561fb04a | 243 | struct io_wq *io_wq; |
6c271ce2 | 244 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
2b188cc1 | 245 | struct mm_struct *sqo_mm; |
6c271ce2 | 246 | wait_queue_head_t sqo_wait; |
75b28aff | 247 | |
6b06314c JA |
248 | /* |
249 | * If used, fixed file set. Writers must ensure that ->refs is dead, | |
250 | * readers must ensure that ->refs is alive as long as the file* is | |
251 | * used. Only updated through io_uring_register(2). | |
252 | */ | |
05f3fb3c | 253 | struct fixed_file_data *file_data; |
6b06314c | 254 | unsigned nr_user_files; |
b14cca0c PB |
255 | int ring_fd; |
256 | struct file *ring_file; | |
6b06314c | 257 | |
edafccee JA |
258 | /* if used, fixed mapped user buffers */ |
259 | unsigned nr_user_bufs; | |
260 | struct io_mapped_ubuf *user_bufs; | |
261 | ||
2b188cc1 JA |
262 | struct user_struct *user; |
263 | ||
0b8c0ec7 | 264 | const struct cred *creds; |
181e448d | 265 | |
206aefde JA |
266 | /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */ |
267 | struct completion *completions; | |
268 | ||
0ddf92e8 JA |
269 | /* if all else fails... */ |
270 | struct io_kiocb *fallback_req; | |
271 | ||
206aefde JA |
272 | #if defined(CONFIG_UNIX) |
273 | struct socket *ring_sock; | |
274 | #endif | |
275 | ||
276 | struct { | |
277 | unsigned cached_cq_tail; | |
278 | unsigned cq_entries; | |
279 | unsigned cq_mask; | |
280 | atomic_t cq_timeouts; | |
ad3eb2c8 | 281 | unsigned long cq_check_overflow; |
206aefde JA |
282 | struct wait_queue_head cq_wait; |
283 | struct fasync_struct *cq_fasync; | |
284 | struct eventfd_ctx *cq_ev_fd; | |
285 | } ____cacheline_aligned_in_smp; | |
2b188cc1 JA |
286 | |
287 | struct { | |
288 | struct mutex uring_lock; | |
289 | wait_queue_head_t wait; | |
290 | } ____cacheline_aligned_in_smp; | |
291 | ||
292 | struct { | |
293 | spinlock_t completion_lock; | |
e94f141b JA |
294 | struct llist_head poll_llist; |
295 | ||
def596e9 JA |
296 | /* |
297 | * ->poll_list is protected by the ctx->uring_lock for | |
298 | * io_uring instances that don't use IORING_SETUP_SQPOLL. | |
299 | * For SQPOLL, only the single threaded io_sq_thread() will | |
300 | * manipulate the list, hence no extra locking is needed there. | |
301 | */ | |
302 | struct list_head poll_list; | |
78076bb6 JA |
303 | struct hlist_head *cancel_hash; |
304 | unsigned cancel_hash_bits; | |
e94f141b | 305 | bool poll_multi_file; |
31b51510 | 306 | |
fcb323cc JA |
307 | spinlock_t inflight_lock; |
308 | struct list_head inflight_list; | |
2b188cc1 | 309 | } ____cacheline_aligned_in_smp; |
2b188cc1 JA |
310 | }; |
311 | ||
09bb8394 JA |
312 | /* |
313 | * First field must be the file pointer in all the | |
314 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
315 | */ | |
221c5eb2 JA |
316 | struct io_poll_iocb { |
317 | struct file *file; | |
0969e783 JA |
318 | union { |
319 | struct wait_queue_head *head; | |
320 | u64 addr; | |
321 | }; | |
221c5eb2 | 322 | __poll_t events; |
8c838788 | 323 | bool done; |
221c5eb2 | 324 | bool canceled; |
392edb45 | 325 | struct wait_queue_entry wait; |
221c5eb2 JA |
326 | }; |
327 | ||
b5dba59e JA |
328 | struct io_close { |
329 | struct file *file; | |
330 | struct file *put_file; | |
331 | int fd; | |
332 | }; | |
333 | ||
ad8a48ac JA |
334 | struct io_timeout_data { |
335 | struct io_kiocb *req; | |
336 | struct hrtimer timer; | |
337 | struct timespec64 ts; | |
338 | enum hrtimer_mode mode; | |
cc42e0ac | 339 | u32 seq_offset; |
ad8a48ac JA |
340 | }; |
341 | ||
8ed8d3c3 JA |
342 | struct io_accept { |
343 | struct file *file; | |
344 | struct sockaddr __user *addr; | |
345 | int __user *addr_len; | |
346 | int flags; | |
347 | }; | |
348 | ||
349 | struct io_sync { | |
350 | struct file *file; | |
351 | loff_t len; | |
352 | loff_t off; | |
353 | int flags; | |
d63d1b5e | 354 | int mode; |
8ed8d3c3 JA |
355 | }; |
356 | ||
fbf23849 JA |
357 | struct io_cancel { |
358 | struct file *file; | |
359 | u64 addr; | |
360 | }; | |
361 | ||
b29472ee JA |
362 | struct io_timeout { |
363 | struct file *file; | |
364 | u64 addr; | |
365 | int flags; | |
26a61679 | 366 | unsigned count; |
b29472ee JA |
367 | }; |
368 | ||
9adbd45d JA |
369 | struct io_rw { |
370 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
371 | struct kiocb kiocb; | |
372 | u64 addr; | |
373 | u64 len; | |
374 | }; | |
375 | ||
3fbb51c1 JA |
376 | struct io_connect { |
377 | struct file *file; | |
378 | struct sockaddr __user *addr; | |
379 | int addr_len; | |
380 | }; | |
381 | ||
e47293fd JA |
382 | struct io_sr_msg { |
383 | struct file *file; | |
fddaface JA |
384 | union { |
385 | struct user_msghdr __user *msg; | |
386 | void __user *buf; | |
387 | }; | |
e47293fd | 388 | int msg_flags; |
fddaface | 389 | size_t len; |
e47293fd JA |
390 | }; |
391 | ||
15b71abe JA |
392 | struct io_open { |
393 | struct file *file; | |
394 | int dfd; | |
eddc7ef5 | 395 | union { |
eddc7ef5 JA |
396 | unsigned mask; |
397 | }; | |
15b71abe | 398 | struct filename *filename; |
eddc7ef5 | 399 | struct statx __user *buffer; |
c12cedf2 | 400 | struct open_how how; |
15b71abe JA |
401 | }; |
402 | ||
05f3fb3c JA |
403 | struct io_files_update { |
404 | struct file *file; | |
405 | u64 arg; | |
406 | u32 nr_args; | |
407 | u32 offset; | |
408 | }; | |
409 | ||
4840e418 JA |
410 | struct io_fadvise { |
411 | struct file *file; | |
412 | u64 offset; | |
413 | u32 len; | |
414 | u32 advice; | |
415 | }; | |
416 | ||
c1ca757b JA |
417 | struct io_madvise { |
418 | struct file *file; | |
419 | u64 addr; | |
420 | u32 len; | |
421 | u32 advice; | |
422 | }; | |
423 | ||
f499a021 JA |
424 | struct io_async_connect { |
425 | struct sockaddr_storage address; | |
426 | }; | |
427 | ||
03b1230c JA |
428 | struct io_async_msghdr { |
429 | struct iovec fast_iov[UIO_FASTIOV]; | |
430 | struct iovec *iov; | |
431 | struct sockaddr __user *uaddr; | |
432 | struct msghdr msg; | |
433 | }; | |
434 | ||
f67676d1 JA |
435 | struct io_async_rw { |
436 | struct iovec fast_iov[UIO_FASTIOV]; | |
437 | struct iovec *iov; | |
438 | ssize_t nr_segs; | |
439 | ssize_t size; | |
440 | }; | |
441 | ||
15b71abe JA |
442 | struct io_async_open { |
443 | struct filename *filename; | |
444 | }; | |
445 | ||
1a6b74fc | 446 | struct io_async_ctx { |
f67676d1 JA |
447 | union { |
448 | struct io_async_rw rw; | |
03b1230c | 449 | struct io_async_msghdr msg; |
f499a021 | 450 | struct io_async_connect connect; |
2d28390a | 451 | struct io_timeout_data timeout; |
15b71abe | 452 | struct io_async_open open; |
f67676d1 | 453 | }; |
1a6b74fc JA |
454 | }; |
455 | ||
6b47ee6e PB |
456 | enum { |
457 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
458 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
459 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
460 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
461 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
462 | ||
463 | REQ_F_LINK_NEXT_BIT, | |
464 | REQ_F_FAIL_LINK_BIT, | |
465 | REQ_F_INFLIGHT_BIT, | |
466 | REQ_F_CUR_POS_BIT, | |
467 | REQ_F_NOWAIT_BIT, | |
468 | REQ_F_IOPOLL_COMPLETED_BIT, | |
469 | REQ_F_LINK_TIMEOUT_BIT, | |
470 | REQ_F_TIMEOUT_BIT, | |
471 | REQ_F_ISREG_BIT, | |
472 | REQ_F_MUST_PUNT_BIT, | |
473 | REQ_F_TIMEOUT_NOSEQ_BIT, | |
474 | REQ_F_COMP_LOCKED_BIT, | |
475 | }; | |
476 | ||
477 | enum { | |
478 | /* ctx owns file */ | |
479 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
480 | /* drain existing IO first */ | |
481 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
482 | /* linked sqes */ | |
483 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
484 | /* doesn't sever on completion < 0 */ | |
485 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
486 | /* IOSQE_ASYNC */ | |
487 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
488 | ||
489 | /* already grabbed next link */ | |
490 | REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT), | |
491 | /* fail rest of links */ | |
492 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), | |
493 | /* on inflight list */ | |
494 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), | |
495 | /* read/write uses file position */ | |
496 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
497 | /* must not punt to workers */ | |
498 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
499 | /* polled IO has completed */ | |
500 | REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT), | |
501 | /* has linked timeout */ | |
502 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), | |
503 | /* timeout request */ | |
504 | REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT), | |
505 | /* regular file */ | |
506 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
507 | /* must be punted even for NONBLOCK */ | |
508 | REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT), | |
509 | /* no timeout sequence */ | |
510 | REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT), | |
511 | /* completion under lock */ | |
512 | REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT), | |
513 | }; | |
514 | ||
09bb8394 JA |
515 | /* |
516 | * NOTE! Each of the iocb union members has the file pointer | |
517 | * as the first entry in their struct definition. So you can | |
518 | * access the file pointer through any of the sub-structs, | |
519 | * or directly as just 'ki_filp' in this struct. | |
520 | */ | |
2b188cc1 | 521 | struct io_kiocb { |
221c5eb2 | 522 | union { |
09bb8394 | 523 | struct file *file; |
9adbd45d | 524 | struct io_rw rw; |
221c5eb2 | 525 | struct io_poll_iocb poll; |
8ed8d3c3 JA |
526 | struct io_accept accept; |
527 | struct io_sync sync; | |
fbf23849 | 528 | struct io_cancel cancel; |
b29472ee | 529 | struct io_timeout timeout; |
3fbb51c1 | 530 | struct io_connect connect; |
e47293fd | 531 | struct io_sr_msg sr_msg; |
15b71abe | 532 | struct io_open open; |
b5dba59e | 533 | struct io_close close; |
05f3fb3c | 534 | struct io_files_update files_update; |
4840e418 | 535 | struct io_fadvise fadvise; |
c1ca757b | 536 | struct io_madvise madvise; |
221c5eb2 | 537 | }; |
2b188cc1 | 538 | |
1a6b74fc | 539 | struct io_async_ctx *io; |
b14cca0c PB |
540 | /* |
541 | * llist_node is only used for poll deferred completions | |
542 | */ | |
543 | struct llist_node llist_node; | |
cf6fd4bd PB |
544 | bool has_user; |
545 | bool in_async; | |
546 | bool needs_fixed_file; | |
d625c6ee | 547 | u8 opcode; |
2b188cc1 JA |
548 | |
549 | struct io_ring_ctx *ctx; | |
eac406c6 JA |
550 | union { |
551 | struct list_head list; | |
78076bb6 | 552 | struct hlist_node hash_node; |
eac406c6 | 553 | }; |
9e645e11 | 554 | struct list_head link_list; |
2b188cc1 | 555 | unsigned int flags; |
c16361c1 | 556 | refcount_t refs; |
2b188cc1 | 557 | u64 user_data; |
9e645e11 | 558 | u32 result; |
de0617e4 | 559 | u32 sequence; |
2b188cc1 | 560 | |
fcb323cc JA |
561 | struct list_head inflight_entry; |
562 | ||
561fb04a | 563 | struct io_wq_work work; |
2b188cc1 JA |
564 | }; |
565 | ||
566 | #define IO_PLUG_THRESHOLD 2 | |
def596e9 | 567 | #define IO_IOPOLL_BATCH 8 |
2b188cc1 | 568 | |
9a56a232 JA |
569 | struct io_submit_state { |
570 | struct blk_plug plug; | |
571 | ||
2579f913 JA |
572 | /* |
573 | * io_kiocb alloc cache | |
574 | */ | |
575 | void *reqs[IO_IOPOLL_BATCH]; | |
576 | unsigned int free_reqs; | |
577 | unsigned int cur_req; | |
578 | ||
9a56a232 JA |
579 | /* |
580 | * File reference cache | |
581 | */ | |
582 | struct file *file; | |
583 | unsigned int fd; | |
584 | unsigned int has_refs; | |
585 | unsigned int used_refs; | |
586 | unsigned int ios_left; | |
587 | }; | |
588 | ||
d3656344 JA |
589 | struct io_op_def { |
590 | /* needs req->io allocated for deferral/async */ | |
591 | unsigned async_ctx : 1; | |
592 | /* needs current->mm setup, does mm access */ | |
593 | unsigned needs_mm : 1; | |
594 | /* needs req->file assigned */ | |
595 | unsigned needs_file : 1; | |
596 | /* needs req->file assigned IFF fd is >= 0 */ | |
597 | unsigned fd_non_neg : 1; | |
598 | /* hash wq insertion if file is a regular file */ | |
599 | unsigned hash_reg_file : 1; | |
600 | /* unbound wq insertion if file is a non-regular file */ | |
601 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
602 | /* opcode is not supported by this kernel */ |
603 | unsigned not_supported : 1; | |
d3656344 JA |
604 | }; |
605 | ||
606 | static const struct io_op_def io_op_defs[] = { | |
0463b6c5 PB |
607 | [IORING_OP_NOP] = {}, |
608 | [IORING_OP_READV] = { | |
d3656344 JA |
609 | .async_ctx = 1, |
610 | .needs_mm = 1, | |
611 | .needs_file = 1, | |
612 | .unbound_nonreg_file = 1, | |
613 | }, | |
0463b6c5 | 614 | [IORING_OP_WRITEV] = { |
d3656344 JA |
615 | .async_ctx = 1, |
616 | .needs_mm = 1, | |
617 | .needs_file = 1, | |
618 | .hash_reg_file = 1, | |
619 | .unbound_nonreg_file = 1, | |
620 | }, | |
0463b6c5 | 621 | [IORING_OP_FSYNC] = { |
d3656344 JA |
622 | .needs_file = 1, |
623 | }, | |
0463b6c5 | 624 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
625 | .needs_file = 1, |
626 | .unbound_nonreg_file = 1, | |
627 | }, | |
0463b6c5 | 628 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
629 | .needs_file = 1, |
630 | .hash_reg_file = 1, | |
631 | .unbound_nonreg_file = 1, | |
632 | }, | |
0463b6c5 | 633 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
634 | .needs_file = 1, |
635 | .unbound_nonreg_file = 1, | |
636 | }, | |
0463b6c5 PB |
637 | [IORING_OP_POLL_REMOVE] = {}, |
638 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 JA |
639 | .needs_file = 1, |
640 | }, | |
0463b6c5 | 641 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
642 | .async_ctx = 1, |
643 | .needs_mm = 1, | |
644 | .needs_file = 1, | |
645 | .unbound_nonreg_file = 1, | |
646 | }, | |
0463b6c5 | 647 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
648 | .async_ctx = 1, |
649 | .needs_mm = 1, | |
650 | .needs_file = 1, | |
651 | .unbound_nonreg_file = 1, | |
652 | }, | |
0463b6c5 | 653 | [IORING_OP_TIMEOUT] = { |
d3656344 JA |
654 | .async_ctx = 1, |
655 | .needs_mm = 1, | |
656 | }, | |
0463b6c5 PB |
657 | [IORING_OP_TIMEOUT_REMOVE] = {}, |
658 | [IORING_OP_ACCEPT] = { | |
d3656344 JA |
659 | .needs_mm = 1, |
660 | .needs_file = 1, | |
661 | .unbound_nonreg_file = 1, | |
662 | }, | |
0463b6c5 PB |
663 | [IORING_OP_ASYNC_CANCEL] = {}, |
664 | [IORING_OP_LINK_TIMEOUT] = { | |
d3656344 JA |
665 | .async_ctx = 1, |
666 | .needs_mm = 1, | |
667 | }, | |
0463b6c5 | 668 | [IORING_OP_CONNECT] = { |
d3656344 JA |
669 | .async_ctx = 1, |
670 | .needs_mm = 1, | |
671 | .needs_file = 1, | |
672 | .unbound_nonreg_file = 1, | |
673 | }, | |
0463b6c5 | 674 | [IORING_OP_FALLOCATE] = { |
d3656344 JA |
675 | .needs_file = 1, |
676 | }, | |
0463b6c5 | 677 | [IORING_OP_OPENAT] = { |
d3656344 JA |
678 | .needs_file = 1, |
679 | .fd_non_neg = 1, | |
680 | }, | |
0463b6c5 | 681 | [IORING_OP_CLOSE] = { |
d3656344 JA |
682 | .needs_file = 1, |
683 | }, | |
0463b6c5 | 684 | [IORING_OP_FILES_UPDATE] = { |
d3656344 JA |
685 | .needs_mm = 1, |
686 | }, | |
0463b6c5 | 687 | [IORING_OP_STATX] = { |
d3656344 JA |
688 | .needs_mm = 1, |
689 | .needs_file = 1, | |
690 | .fd_non_neg = 1, | |
691 | }, | |
0463b6c5 | 692 | [IORING_OP_READ] = { |
3a6820f2 JA |
693 | .needs_mm = 1, |
694 | .needs_file = 1, | |
695 | .unbound_nonreg_file = 1, | |
696 | }, | |
0463b6c5 | 697 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
698 | .needs_mm = 1, |
699 | .needs_file = 1, | |
700 | .unbound_nonreg_file = 1, | |
701 | }, | |
0463b6c5 | 702 | [IORING_OP_FADVISE] = { |
4840e418 JA |
703 | .needs_file = 1, |
704 | }, | |
0463b6c5 | 705 | [IORING_OP_MADVISE] = { |
c1ca757b JA |
706 | .needs_mm = 1, |
707 | }, | |
0463b6c5 | 708 | [IORING_OP_SEND] = { |
fddaface JA |
709 | .needs_mm = 1, |
710 | .needs_file = 1, | |
711 | .unbound_nonreg_file = 1, | |
712 | }, | |
0463b6c5 | 713 | [IORING_OP_RECV] = { |
fddaface JA |
714 | .needs_mm = 1, |
715 | .needs_file = 1, | |
716 | .unbound_nonreg_file = 1, | |
717 | }, | |
0463b6c5 | 718 | [IORING_OP_OPENAT2] = { |
cebdb986 JA |
719 | .needs_file = 1, |
720 | .fd_non_neg = 1, | |
721 | }, | |
d3656344 JA |
722 | }; |
723 | ||
561fb04a | 724 | static void io_wq_submit_work(struct io_wq_work **workptr); |
78e19bbe | 725 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
ec9c02ad | 726 | static void io_put_req(struct io_kiocb *req); |
978db57e | 727 | static void __io_double_put_req(struct io_kiocb *req); |
94ae5e77 JA |
728 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
729 | static void io_queue_linked_timeout(struct io_kiocb *req); | |
05f3fb3c JA |
730 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
731 | struct io_uring_files_update *ip, | |
732 | unsigned nr_args); | |
de0617e4 | 733 | |
2b188cc1 JA |
734 | static struct kmem_cache *req_cachep; |
735 | ||
736 | static const struct file_operations io_uring_fops; | |
737 | ||
738 | struct sock *io_uring_get_socket(struct file *file) | |
739 | { | |
740 | #if defined(CONFIG_UNIX) | |
741 | if (file->f_op == &io_uring_fops) { | |
742 | struct io_ring_ctx *ctx = file->private_data; | |
743 | ||
744 | return ctx->ring_sock->sk; | |
745 | } | |
746 | #endif | |
747 | return NULL; | |
748 | } | |
749 | EXPORT_SYMBOL(io_uring_get_socket); | |
750 | ||
751 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) | |
752 | { | |
753 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
754 | ||
206aefde | 755 | complete(&ctx->completions[0]); |
2b188cc1 JA |
756 | } |
757 | ||
758 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) | |
759 | { | |
760 | struct io_ring_ctx *ctx; | |
78076bb6 | 761 | int hash_bits; |
2b188cc1 JA |
762 | |
763 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
764 | if (!ctx) | |
765 | return NULL; | |
766 | ||
0ddf92e8 JA |
767 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
768 | if (!ctx->fallback_req) | |
769 | goto err; | |
770 | ||
206aefde JA |
771 | ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL); |
772 | if (!ctx->completions) | |
773 | goto err; | |
774 | ||
78076bb6 JA |
775 | /* |
776 | * Use 5 bits less than the max cq entries, that should give us around | |
777 | * 32 entries per hash list if totally full and uniformly spread. | |
778 | */ | |
779 | hash_bits = ilog2(p->cq_entries); | |
780 | hash_bits -= 5; | |
781 | if (hash_bits <= 0) | |
782 | hash_bits = 1; | |
783 | ctx->cancel_hash_bits = hash_bits; | |
784 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
785 | GFP_KERNEL); | |
786 | if (!ctx->cancel_hash) | |
787 | goto err; | |
788 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
789 | ||
21482896 | 790 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
791 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
792 | goto err; | |
2b188cc1 JA |
793 | |
794 | ctx->flags = p->flags; | |
795 | init_waitqueue_head(&ctx->cq_wait); | |
1d7bb1d5 | 796 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
206aefde JA |
797 | init_completion(&ctx->completions[0]); |
798 | init_completion(&ctx->completions[1]); | |
2b188cc1 JA |
799 | mutex_init(&ctx->uring_lock); |
800 | init_waitqueue_head(&ctx->wait); | |
801 | spin_lock_init(&ctx->completion_lock); | |
e94f141b | 802 | init_llist_head(&ctx->poll_llist); |
def596e9 | 803 | INIT_LIST_HEAD(&ctx->poll_list); |
de0617e4 | 804 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 805 | INIT_LIST_HEAD(&ctx->timeout_list); |
fcb323cc JA |
806 | init_waitqueue_head(&ctx->inflight_wait); |
807 | spin_lock_init(&ctx->inflight_lock); | |
808 | INIT_LIST_HEAD(&ctx->inflight_list); | |
2b188cc1 | 809 | return ctx; |
206aefde | 810 | err: |
0ddf92e8 JA |
811 | if (ctx->fallback_req) |
812 | kmem_cache_free(req_cachep, ctx->fallback_req); | |
206aefde | 813 | kfree(ctx->completions); |
78076bb6 | 814 | kfree(ctx->cancel_hash); |
206aefde JA |
815 | kfree(ctx); |
816 | return NULL; | |
2b188cc1 JA |
817 | } |
818 | ||
9d858b21 | 819 | static inline bool __req_need_defer(struct io_kiocb *req) |
7adf4eaf | 820 | { |
a197f664 JL |
821 | struct io_ring_ctx *ctx = req->ctx; |
822 | ||
498ccd9e JA |
823 | return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped |
824 | + atomic_read(&ctx->cached_cq_overflow); | |
7adf4eaf JA |
825 | } |
826 | ||
9d858b21 | 827 | static inline bool req_need_defer(struct io_kiocb *req) |
de0617e4 | 828 | { |
87987898 | 829 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) |
9d858b21 | 830 | return __req_need_defer(req); |
de0617e4 | 831 | |
9d858b21 | 832 | return false; |
de0617e4 JA |
833 | } |
834 | ||
7adf4eaf | 835 | static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx) |
de0617e4 JA |
836 | { |
837 | struct io_kiocb *req; | |
838 | ||
7adf4eaf | 839 | req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list); |
9d858b21 | 840 | if (req && !req_need_defer(req)) { |
de0617e4 JA |
841 | list_del_init(&req->list); |
842 | return req; | |
843 | } | |
844 | ||
845 | return NULL; | |
846 | } | |
847 | ||
5262f567 JA |
848 | static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx) |
849 | { | |
7adf4eaf JA |
850 | struct io_kiocb *req; |
851 | ||
852 | req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list); | |
93bd25bb JA |
853 | if (req) { |
854 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) | |
855 | return NULL; | |
fb4b3d3f | 856 | if (!__req_need_defer(req)) { |
93bd25bb JA |
857 | list_del_init(&req->list); |
858 | return req; | |
859 | } | |
7adf4eaf JA |
860 | } |
861 | ||
862 | return NULL; | |
5262f567 JA |
863 | } |
864 | ||
de0617e4 | 865 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
2b188cc1 | 866 | { |
75b28aff | 867 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 868 | |
07910158 PB |
869 | /* order cqe stores with ring update */ |
870 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); | |
2b188cc1 | 871 | |
07910158 PB |
872 | if (wq_has_sleeper(&ctx->cq_wait)) { |
873 | wake_up_interruptible(&ctx->cq_wait); | |
874 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
2b188cc1 JA |
875 | } |
876 | } | |
877 | ||
94ae5e77 JA |
878 | static inline bool io_prep_async_work(struct io_kiocb *req, |
879 | struct io_kiocb **link) | |
18d9be1a | 880 | { |
d3656344 | 881 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
561fb04a | 882 | bool do_hashed = false; |
54a91f3b | 883 | |
d3656344 JA |
884 | if (req->flags & REQ_F_ISREG) { |
885 | if (def->hash_reg_file) | |
3529d8c2 | 886 | do_hashed = true; |
d3656344 JA |
887 | } else { |
888 | if (def->unbound_nonreg_file) | |
3529d8c2 | 889 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
54a91f3b | 890 | } |
d3656344 | 891 | if (def->needs_mm) |
3529d8c2 | 892 | req->work.flags |= IO_WQ_WORK_NEEDS_USER; |
54a91f3b | 893 | |
94ae5e77 | 894 | *link = io_prep_linked_timeout(req); |
561fb04a JA |
895 | return do_hashed; |
896 | } | |
897 | ||
a197f664 | 898 | static inline void io_queue_async_work(struct io_kiocb *req) |
561fb04a | 899 | { |
a197f664 | 900 | struct io_ring_ctx *ctx = req->ctx; |
94ae5e77 JA |
901 | struct io_kiocb *link; |
902 | bool do_hashed; | |
903 | ||
904 | do_hashed = io_prep_async_work(req, &link); | |
561fb04a JA |
905 | |
906 | trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work, | |
907 | req->flags); | |
908 | if (!do_hashed) { | |
909 | io_wq_enqueue(ctx->io_wq, &req->work); | |
910 | } else { | |
911 | io_wq_enqueue_hashed(ctx->io_wq, &req->work, | |
912 | file_inode(req->file)); | |
913 | } | |
94ae5e77 JA |
914 | |
915 | if (link) | |
916 | io_queue_linked_timeout(link); | |
18d9be1a JA |
917 | } |
918 | ||
5262f567 JA |
919 | static void io_kill_timeout(struct io_kiocb *req) |
920 | { | |
921 | int ret; | |
922 | ||
2d28390a | 923 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
5262f567 JA |
924 | if (ret != -1) { |
925 | atomic_inc(&req->ctx->cq_timeouts); | |
842f9612 | 926 | list_del_init(&req->list); |
78e19bbe | 927 | io_cqring_fill_event(req, 0); |
ec9c02ad | 928 | io_put_req(req); |
5262f567 JA |
929 | } |
930 | } | |
931 | ||
932 | static void io_kill_timeouts(struct io_ring_ctx *ctx) | |
933 | { | |
934 | struct io_kiocb *req, *tmp; | |
935 | ||
936 | spin_lock_irq(&ctx->completion_lock); | |
937 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) | |
938 | io_kill_timeout(req); | |
939 | spin_unlock_irq(&ctx->completion_lock); | |
940 | } | |
941 | ||
de0617e4 JA |
942 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
943 | { | |
944 | struct io_kiocb *req; | |
945 | ||
5262f567 JA |
946 | while ((req = io_get_timeout_req(ctx)) != NULL) |
947 | io_kill_timeout(req); | |
948 | ||
de0617e4 JA |
949 | __io_commit_cqring(ctx); |
950 | ||
87987898 | 951 | while ((req = io_get_deferred_req(ctx)) != NULL) |
a197f664 | 952 | io_queue_async_work(req); |
de0617e4 JA |
953 | } |
954 | ||
2b188cc1 JA |
955 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
956 | { | |
75b28aff | 957 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
958 | unsigned tail; |
959 | ||
960 | tail = ctx->cached_cq_tail; | |
115e12e5 SB |
961 | /* |
962 | * writes to the cq entry need to come after reading head; the | |
963 | * control dependency is enough as we're using WRITE_ONCE to | |
964 | * fill the cq entry | |
965 | */ | |
75b28aff | 966 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
2b188cc1 JA |
967 | return NULL; |
968 | ||
969 | ctx->cached_cq_tail++; | |
75b28aff | 970 | return &rings->cqes[tail & ctx->cq_mask]; |
2b188cc1 JA |
971 | } |
972 | ||
f2842ab5 JA |
973 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
974 | { | |
975 | if (!ctx->eventfd_async) | |
976 | return true; | |
977 | return io_wq_current_is_worker() || in_interrupt(); | |
978 | } | |
979 | ||
1d7bb1d5 JA |
980 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
981 | { | |
982 | if (waitqueue_active(&ctx->wait)) | |
983 | wake_up(&ctx->wait); | |
984 | if (waitqueue_active(&ctx->sqo_wait)) | |
985 | wake_up(&ctx->sqo_wait); | |
f2842ab5 | 986 | if (ctx->cq_ev_fd && io_should_trigger_evfd(ctx)) |
1d7bb1d5 JA |
987 | eventfd_signal(ctx->cq_ev_fd, 1); |
988 | } | |
989 | ||
c4a2ed72 JA |
990 | /* Returns true if there are no backlogged entries after the flush */ |
991 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) | |
1d7bb1d5 JA |
992 | { |
993 | struct io_rings *rings = ctx->rings; | |
994 | struct io_uring_cqe *cqe; | |
995 | struct io_kiocb *req; | |
996 | unsigned long flags; | |
997 | LIST_HEAD(list); | |
998 | ||
999 | if (!force) { | |
1000 | if (list_empty_careful(&ctx->cq_overflow_list)) | |
c4a2ed72 | 1001 | return true; |
1d7bb1d5 JA |
1002 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
1003 | rings->cq_ring_entries)) | |
c4a2ed72 | 1004 | return false; |
1d7bb1d5 JA |
1005 | } |
1006 | ||
1007 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
1008 | ||
1009 | /* if force is set, the ring is going away. always drop after that */ | |
1010 | if (force) | |
69b3e546 | 1011 | ctx->cq_overflow_flushed = 1; |
1d7bb1d5 | 1012 | |
c4a2ed72 | 1013 | cqe = NULL; |
1d7bb1d5 JA |
1014 | while (!list_empty(&ctx->cq_overflow_list)) { |
1015 | cqe = io_get_cqring(ctx); | |
1016 | if (!cqe && !force) | |
1017 | break; | |
1018 | ||
1019 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, | |
1020 | list); | |
1021 | list_move(&req->list, &list); | |
1022 | if (cqe) { | |
1023 | WRITE_ONCE(cqe->user_data, req->user_data); | |
1024 | WRITE_ONCE(cqe->res, req->result); | |
1025 | WRITE_ONCE(cqe->flags, 0); | |
1026 | } else { | |
1027 | WRITE_ONCE(ctx->rings->cq_overflow, | |
1028 | atomic_inc_return(&ctx->cached_cq_overflow)); | |
1029 | } | |
1030 | } | |
1031 | ||
1032 | io_commit_cqring(ctx); | |
ad3eb2c8 JA |
1033 | if (cqe) { |
1034 | clear_bit(0, &ctx->sq_check_overflow); | |
1035 | clear_bit(0, &ctx->cq_check_overflow); | |
1036 | } | |
1d7bb1d5 JA |
1037 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
1038 | io_cqring_ev_posted(ctx); | |
1039 | ||
1040 | while (!list_empty(&list)) { | |
1041 | req = list_first_entry(&list, struct io_kiocb, list); | |
1042 | list_del(&req->list); | |
ec9c02ad | 1043 | io_put_req(req); |
1d7bb1d5 | 1044 | } |
c4a2ed72 JA |
1045 | |
1046 | return cqe != NULL; | |
1d7bb1d5 JA |
1047 | } |
1048 | ||
78e19bbe | 1049 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
2b188cc1 | 1050 | { |
78e19bbe | 1051 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1052 | struct io_uring_cqe *cqe; |
1053 | ||
78e19bbe | 1054 | trace_io_uring_complete(ctx, req->user_data, res); |
51c3ff62 | 1055 | |
2b188cc1 JA |
1056 | /* |
1057 | * If we can't get a cq entry, userspace overflowed the | |
1058 | * submission (by quite a lot). Increment the overflow count in | |
1059 | * the ring. | |
1060 | */ | |
1061 | cqe = io_get_cqring(ctx); | |
1d7bb1d5 | 1062 | if (likely(cqe)) { |
78e19bbe | 1063 | WRITE_ONCE(cqe->user_data, req->user_data); |
2b188cc1 | 1064 | WRITE_ONCE(cqe->res, res); |
c71ffb67 | 1065 | WRITE_ONCE(cqe->flags, 0); |
1d7bb1d5 | 1066 | } else if (ctx->cq_overflow_flushed) { |
498ccd9e JA |
1067 | WRITE_ONCE(ctx->rings->cq_overflow, |
1068 | atomic_inc_return(&ctx->cached_cq_overflow)); | |
1d7bb1d5 | 1069 | } else { |
ad3eb2c8 JA |
1070 | if (list_empty(&ctx->cq_overflow_list)) { |
1071 | set_bit(0, &ctx->sq_check_overflow); | |
1072 | set_bit(0, &ctx->cq_check_overflow); | |
1073 | } | |
1d7bb1d5 JA |
1074 | refcount_inc(&req->refs); |
1075 | req->result = res; | |
1076 | list_add_tail(&req->list, &ctx->cq_overflow_list); | |
2b188cc1 JA |
1077 | } |
1078 | } | |
1079 | ||
78e19bbe | 1080 | static void io_cqring_add_event(struct io_kiocb *req, long res) |
2b188cc1 | 1081 | { |
78e19bbe | 1082 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1083 | unsigned long flags; |
1084 | ||
1085 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
78e19bbe | 1086 | io_cqring_fill_event(req, res); |
2b188cc1 JA |
1087 | io_commit_cqring(ctx); |
1088 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
1089 | ||
8c838788 | 1090 | io_cqring_ev_posted(ctx); |
2b188cc1 JA |
1091 | } |
1092 | ||
0ddf92e8 JA |
1093 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
1094 | { | |
1095 | return req == (struct io_kiocb *) | |
1096 | ((unsigned long) req->ctx->fallback_req & ~1UL); | |
1097 | } | |
1098 | ||
1099 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) | |
1100 | { | |
1101 | struct io_kiocb *req; | |
1102 | ||
1103 | req = ctx->fallback_req; | |
1104 | if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req)) | |
1105 | return req; | |
1106 | ||
1107 | return NULL; | |
1108 | } | |
1109 | ||
2579f913 JA |
1110 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
1111 | struct io_submit_state *state) | |
2b188cc1 | 1112 | { |
fd6fab2c | 1113 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
2b188cc1 JA |
1114 | struct io_kiocb *req; |
1115 | ||
2579f913 | 1116 | if (!state) { |
fd6fab2c | 1117 | req = kmem_cache_alloc(req_cachep, gfp); |
2579f913 | 1118 | if (unlikely(!req)) |
0ddf92e8 | 1119 | goto fallback; |
2579f913 JA |
1120 | } else if (!state->free_reqs) { |
1121 | size_t sz; | |
1122 | int ret; | |
1123 | ||
1124 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); | |
fd6fab2c JA |
1125 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
1126 | ||
1127 | /* | |
1128 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1129 | * retry single alloc to be on the safe side. | |
1130 | */ | |
1131 | if (unlikely(ret <= 0)) { | |
1132 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
1133 | if (!state->reqs[0]) | |
0ddf92e8 | 1134 | goto fallback; |
fd6fab2c JA |
1135 | ret = 1; |
1136 | } | |
2579f913 JA |
1137 | state->free_reqs = ret - 1; |
1138 | state->cur_req = 1; | |
1139 | req = state->reqs[0]; | |
1140 | } else { | |
1141 | req = state->reqs[state->cur_req]; | |
1142 | state->free_reqs--; | |
1143 | state->cur_req++; | |
2b188cc1 JA |
1144 | } |
1145 | ||
0ddf92e8 | 1146 | got_it: |
1a6b74fc | 1147 | req->io = NULL; |
60c112b0 | 1148 | req->file = NULL; |
2579f913 JA |
1149 | req->ctx = ctx; |
1150 | req->flags = 0; | |
e65ef56d JA |
1151 | /* one is dropped after submission, the other at completion */ |
1152 | refcount_set(&req->refs, 2); | |
9e645e11 | 1153 | req->result = 0; |
561fb04a | 1154 | INIT_IO_WORK(&req->work, io_wq_submit_work); |
2579f913 | 1155 | return req; |
0ddf92e8 JA |
1156 | fallback: |
1157 | req = io_get_fallback_req(ctx); | |
1158 | if (req) | |
1159 | goto got_it; | |
6805b32e | 1160 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
1161 | return NULL; |
1162 | } | |
1163 | ||
2b85edfc PB |
1164 | static void __io_req_do_free(struct io_kiocb *req) |
1165 | { | |
1166 | if (likely(!io_is_fallback_req(req))) | |
1167 | kmem_cache_free(req_cachep, req); | |
1168 | else | |
1169 | clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req); | |
1170 | } | |
1171 | ||
c6ca97b3 | 1172 | static void __io_req_aux_free(struct io_kiocb *req) |
2b188cc1 | 1173 | { |
fcb323cc JA |
1174 | struct io_ring_ctx *ctx = req->ctx; |
1175 | ||
96fd84d8 | 1176 | kfree(req->io); |
05f3fb3c JA |
1177 | if (req->file) { |
1178 | if (req->flags & REQ_F_FIXED_FILE) | |
1179 | percpu_ref_put(&ctx->file_data->refs); | |
1180 | else | |
1181 | fput(req->file); | |
1182 | } | |
c6ca97b3 JA |
1183 | } |
1184 | ||
1185 | static void __io_free_req(struct io_kiocb *req) | |
1186 | { | |
1187 | __io_req_aux_free(req); | |
1188 | ||
fcb323cc | 1189 | if (req->flags & REQ_F_INFLIGHT) { |
c6ca97b3 | 1190 | struct io_ring_ctx *ctx = req->ctx; |
fcb323cc JA |
1191 | unsigned long flags; |
1192 | ||
1193 | spin_lock_irqsave(&ctx->inflight_lock, flags); | |
1194 | list_del(&req->inflight_entry); | |
1195 | if (waitqueue_active(&ctx->inflight_wait)) | |
1196 | wake_up(&ctx->inflight_wait); | |
1197 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); | |
1198 | } | |
2b85edfc PB |
1199 | |
1200 | percpu_ref_put(&req->ctx->refs); | |
1201 | __io_req_do_free(req); | |
e65ef56d JA |
1202 | } |
1203 | ||
c6ca97b3 JA |
1204 | struct req_batch { |
1205 | void *reqs[IO_IOPOLL_BATCH]; | |
1206 | int to_free; | |
1207 | int need_iter; | |
1208 | }; | |
1209 | ||
1210 | static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb) | |
1211 | { | |
10fef4be JA |
1212 | int fixed_refs = rb->to_free; |
1213 | ||
c6ca97b3 JA |
1214 | if (!rb->to_free) |
1215 | return; | |
1216 | if (rb->need_iter) { | |
1217 | int i, inflight = 0; | |
1218 | unsigned long flags; | |
1219 | ||
10fef4be | 1220 | fixed_refs = 0; |
c6ca97b3 JA |
1221 | for (i = 0; i < rb->to_free; i++) { |
1222 | struct io_kiocb *req = rb->reqs[i]; | |
1223 | ||
10fef4be | 1224 | if (req->flags & REQ_F_FIXED_FILE) { |
c6ca97b3 | 1225 | req->file = NULL; |
10fef4be JA |
1226 | fixed_refs++; |
1227 | } | |
c6ca97b3 JA |
1228 | if (req->flags & REQ_F_INFLIGHT) |
1229 | inflight++; | |
c6ca97b3 JA |
1230 | __io_req_aux_free(req); |
1231 | } | |
1232 | if (!inflight) | |
1233 | goto do_free; | |
1234 | ||
1235 | spin_lock_irqsave(&ctx->inflight_lock, flags); | |
1236 | for (i = 0; i < rb->to_free; i++) { | |
1237 | struct io_kiocb *req = rb->reqs[i]; | |
1238 | ||
10fef4be | 1239 | if (req->flags & REQ_F_INFLIGHT) { |
c6ca97b3 JA |
1240 | list_del(&req->inflight_entry); |
1241 | if (!--inflight) | |
1242 | break; | |
1243 | } | |
1244 | } | |
1245 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); | |
1246 | ||
1247 | if (waitqueue_active(&ctx->inflight_wait)) | |
1248 | wake_up(&ctx->inflight_wait); | |
1249 | } | |
1250 | do_free: | |
1251 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); | |
10fef4be JA |
1252 | if (fixed_refs) |
1253 | percpu_ref_put_many(&ctx->file_data->refs, fixed_refs); | |
c6ca97b3 | 1254 | percpu_ref_put_many(&ctx->refs, rb->to_free); |
c6ca97b3 JA |
1255 | rb->to_free = rb->need_iter = 0; |
1256 | } | |
1257 | ||
a197f664 | 1258 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
2665abfd | 1259 | { |
a197f664 | 1260 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd JA |
1261 | int ret; |
1262 | ||
2d28390a | 1263 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
2665abfd | 1264 | if (ret != -1) { |
78e19bbe | 1265 | io_cqring_fill_event(req, -ECANCELED); |
2665abfd JA |
1266 | io_commit_cqring(ctx); |
1267 | req->flags &= ~REQ_F_LINK; | |
ec9c02ad | 1268 | io_put_req(req); |
2665abfd JA |
1269 | return true; |
1270 | } | |
1271 | ||
1272 | return false; | |
e65ef56d JA |
1273 | } |
1274 | ||
ba816ad6 | 1275 | static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr) |
9e645e11 | 1276 | { |
2665abfd | 1277 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 1278 | bool wake_ev = false; |
9e645e11 | 1279 | |
4d7dd462 JA |
1280 | /* Already got next link */ |
1281 | if (req->flags & REQ_F_LINK_NEXT) | |
1282 | return; | |
1283 | ||
9e645e11 JA |
1284 | /* |
1285 | * The list should never be empty when we are called here. But could | |
1286 | * potentially happen if the chain is messed up, check to be on the | |
1287 | * safe side. | |
1288 | */ | |
4493233e PB |
1289 | while (!list_empty(&req->link_list)) { |
1290 | struct io_kiocb *nxt = list_first_entry(&req->link_list, | |
1291 | struct io_kiocb, link_list); | |
94ae5e77 | 1292 | |
4493233e PB |
1293 | if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) && |
1294 | (nxt->flags & REQ_F_TIMEOUT))) { | |
1295 | list_del_init(&nxt->link_list); | |
94ae5e77 | 1296 | wake_ev |= io_link_cancel_timeout(nxt); |
94ae5e77 JA |
1297 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
1298 | continue; | |
1299 | } | |
9e645e11 | 1300 | |
4493233e PB |
1301 | list_del_init(&req->link_list); |
1302 | if (!list_empty(&nxt->link_list)) | |
1303 | nxt->flags |= REQ_F_LINK; | |
b18fdf71 | 1304 | *nxtptr = nxt; |
94ae5e77 | 1305 | break; |
9e645e11 | 1306 | } |
2665abfd | 1307 | |
4d7dd462 | 1308 | req->flags |= REQ_F_LINK_NEXT; |
2665abfd JA |
1309 | if (wake_ev) |
1310 | io_cqring_ev_posted(ctx); | |
9e645e11 JA |
1311 | } |
1312 | ||
1313 | /* | |
1314 | * Called if REQ_F_LINK is set, and we fail the head request | |
1315 | */ | |
1316 | static void io_fail_links(struct io_kiocb *req) | |
1317 | { | |
2665abfd | 1318 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd JA |
1319 | unsigned long flags; |
1320 | ||
1321 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
9e645e11 JA |
1322 | |
1323 | while (!list_empty(&req->link_list)) { | |
4493233e PB |
1324 | struct io_kiocb *link = list_first_entry(&req->link_list, |
1325 | struct io_kiocb, link_list); | |
9e645e11 | 1326 | |
4493233e | 1327 | list_del_init(&link->link_list); |
c826bd7a | 1328 | trace_io_uring_fail_link(req, link); |
2665abfd JA |
1329 | |
1330 | if ((req->flags & REQ_F_LINK_TIMEOUT) && | |
d625c6ee | 1331 | link->opcode == IORING_OP_LINK_TIMEOUT) { |
a197f664 | 1332 | io_link_cancel_timeout(link); |
2665abfd | 1333 | } else { |
78e19bbe | 1334 | io_cqring_fill_event(link, -ECANCELED); |
978db57e | 1335 | __io_double_put_req(link); |
2665abfd | 1336 | } |
5d960724 | 1337 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
9e645e11 | 1338 | } |
2665abfd JA |
1339 | |
1340 | io_commit_cqring(ctx); | |
1341 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
1342 | io_cqring_ev_posted(ctx); | |
9e645e11 JA |
1343 | } |
1344 | ||
4d7dd462 | 1345 | static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt) |
9e645e11 | 1346 | { |
4d7dd462 | 1347 | if (likely(!(req->flags & REQ_F_LINK))) |
2665abfd | 1348 | return; |
2665abfd | 1349 | |
9e645e11 JA |
1350 | /* |
1351 | * If LINK is set, we have dependent requests in this chain. If we | |
1352 | * didn't fail this request, queue the first one up, moving any other | |
1353 | * dependencies to the next request. In case of failure, fail the rest | |
1354 | * of the chain. | |
1355 | */ | |
2665abfd JA |
1356 | if (req->flags & REQ_F_FAIL_LINK) { |
1357 | io_fail_links(req); | |
7c9e7f0f JA |
1358 | } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) == |
1359 | REQ_F_LINK_TIMEOUT) { | |
2665abfd JA |
1360 | struct io_ring_ctx *ctx = req->ctx; |
1361 | unsigned long flags; | |
1362 | ||
1363 | /* | |
1364 | * If this is a timeout link, we could be racing with the | |
1365 | * timeout timer. Grab the completion lock for this case to | |
7c9e7f0f | 1366 | * protect against that. |
2665abfd JA |
1367 | */ |
1368 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
1369 | io_req_link_next(req, nxt); | |
1370 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
1371 | } else { | |
1372 | io_req_link_next(req, nxt); | |
9e645e11 | 1373 | } |
4d7dd462 | 1374 | } |
9e645e11 | 1375 | |
c69f8dbe JL |
1376 | static void io_free_req(struct io_kiocb *req) |
1377 | { | |
944e58bf PB |
1378 | struct io_kiocb *nxt = NULL; |
1379 | ||
1380 | io_req_find_next(req, &nxt); | |
70cf9f32 | 1381 | __io_free_req(req); |
944e58bf PB |
1382 | |
1383 | if (nxt) | |
1384 | io_queue_async_work(nxt); | |
c69f8dbe JL |
1385 | } |
1386 | ||
ba816ad6 JA |
1387 | /* |
1388 | * Drop reference to request, return next in chain (if there is one) if this | |
1389 | * was the last reference to this request. | |
1390 | */ | |
f9bd67f6 | 1391 | __attribute__((nonnull)) |
ec9c02ad | 1392 | static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr) |
e65ef56d | 1393 | { |
f9bd67f6 | 1394 | io_req_find_next(req, nxtptr); |
4d7dd462 | 1395 | |
e65ef56d | 1396 | if (refcount_dec_and_test(&req->refs)) |
4d7dd462 | 1397 | __io_free_req(req); |
2b188cc1 JA |
1398 | } |
1399 | ||
e65ef56d JA |
1400 | static void io_put_req(struct io_kiocb *req) |
1401 | { | |
1402 | if (refcount_dec_and_test(&req->refs)) | |
1403 | io_free_req(req); | |
2b188cc1 JA |
1404 | } |
1405 | ||
978db57e JA |
1406 | /* |
1407 | * Must only be used if we don't need to care about links, usually from | |
1408 | * within the completion handling itself. | |
1409 | */ | |
1410 | static void __io_double_put_req(struct io_kiocb *req) | |
78e19bbe JA |
1411 | { |
1412 | /* drop both submit and complete references */ | |
1413 | if (refcount_sub_and_test(2, &req->refs)) | |
1414 | __io_free_req(req); | |
1415 | } | |
1416 | ||
978db57e JA |
1417 | static void io_double_put_req(struct io_kiocb *req) |
1418 | { | |
1419 | /* drop both submit and complete references */ | |
1420 | if (refcount_sub_and_test(2, &req->refs)) | |
1421 | io_free_req(req); | |
1422 | } | |
1423 | ||
1d7bb1d5 | 1424 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
a3a0e43f | 1425 | { |
84f97dc2 JA |
1426 | struct io_rings *rings = ctx->rings; |
1427 | ||
ad3eb2c8 JA |
1428 | if (test_bit(0, &ctx->cq_check_overflow)) { |
1429 | /* | |
1430 | * noflush == true is from the waitqueue handler, just ensure | |
1431 | * we wake up the task, and the next invocation will flush the | |
1432 | * entries. We cannot safely to it from here. | |
1433 | */ | |
1434 | if (noflush && !list_empty(&ctx->cq_overflow_list)) | |
1435 | return -1U; | |
1d7bb1d5 | 1436 | |
ad3eb2c8 JA |
1437 | io_cqring_overflow_flush(ctx, false); |
1438 | } | |
1d7bb1d5 | 1439 | |
a3a0e43f JA |
1440 | /* See comment at the top of this file */ |
1441 | smp_rmb(); | |
ad3eb2c8 | 1442 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
a3a0e43f JA |
1443 | } |
1444 | ||
fb5ccc98 PB |
1445 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
1446 | { | |
1447 | struct io_rings *rings = ctx->rings; | |
1448 | ||
1449 | /* make sure SQ entry isn't read before tail */ | |
1450 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
1451 | } | |
1452 | ||
8237e045 | 1453 | static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req) |
e94f141b | 1454 | { |
c6ca97b3 JA |
1455 | if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req)) |
1456 | return false; | |
e94f141b | 1457 | |
c6ca97b3 JA |
1458 | if (!(req->flags & REQ_F_FIXED_FILE) || req->io) |
1459 | rb->need_iter++; | |
1460 | ||
1461 | rb->reqs[rb->to_free++] = req; | |
1462 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) | |
1463 | io_free_req_many(req->ctx, rb); | |
1464 | return true; | |
e94f141b JA |
1465 | } |
1466 | ||
def596e9 JA |
1467 | /* |
1468 | * Find and free completed poll iocbs | |
1469 | */ | |
1470 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
1471 | struct list_head *done) | |
1472 | { | |
8237e045 | 1473 | struct req_batch rb; |
def596e9 | 1474 | struct io_kiocb *req; |
def596e9 | 1475 | |
c6ca97b3 | 1476 | rb.to_free = rb.need_iter = 0; |
def596e9 JA |
1477 | while (!list_empty(done)) { |
1478 | req = list_first_entry(done, struct io_kiocb, list); | |
1479 | list_del(&req->list); | |
1480 | ||
78e19bbe | 1481 | io_cqring_fill_event(req, req->result); |
def596e9 JA |
1482 | (*nr_events)++; |
1483 | ||
8237e045 JA |
1484 | if (refcount_dec_and_test(&req->refs) && |
1485 | !io_req_multi_free(&rb, req)) | |
1486 | io_free_req(req); | |
def596e9 | 1487 | } |
def596e9 | 1488 | |
09bb8394 | 1489 | io_commit_cqring(ctx); |
8237e045 | 1490 | io_free_req_many(ctx, &rb); |
def596e9 JA |
1491 | } |
1492 | ||
1493 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
1494 | long min) | |
1495 | { | |
1496 | struct io_kiocb *req, *tmp; | |
1497 | LIST_HEAD(done); | |
1498 | bool spin; | |
1499 | int ret; | |
1500 | ||
1501 | /* | |
1502 | * Only spin for completions if we don't have multiple devices hanging | |
1503 | * off our complete list, and we're under the requested amount. | |
1504 | */ | |
1505 | spin = !ctx->poll_multi_file && *nr_events < min; | |
1506 | ||
1507 | ret = 0; | |
1508 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { | |
9adbd45d | 1509 | struct kiocb *kiocb = &req->rw.kiocb; |
def596e9 JA |
1510 | |
1511 | /* | |
1512 | * Move completed entries to our local list. If we find a | |
1513 | * request that requires polling, break out and complete | |
1514 | * the done list first, if we have entries there. | |
1515 | */ | |
1516 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { | |
1517 | list_move_tail(&req->list, &done); | |
1518 | continue; | |
1519 | } | |
1520 | if (!list_empty(&done)) | |
1521 | break; | |
1522 | ||
1523 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
1524 | if (ret < 0) | |
1525 | break; | |
1526 | ||
1527 | if (ret && spin) | |
1528 | spin = false; | |
1529 | ret = 0; | |
1530 | } | |
1531 | ||
1532 | if (!list_empty(&done)) | |
1533 | io_iopoll_complete(ctx, nr_events, &done); | |
1534 | ||
1535 | return ret; | |
1536 | } | |
1537 | ||
1538 | /* | |
d195a66e | 1539 | * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a |
def596e9 JA |
1540 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
1541 | * as a non-spinning completion check. | |
1542 | */ | |
1543 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
1544 | long min) | |
1545 | { | |
08f5439f | 1546 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
def596e9 JA |
1547 | int ret; |
1548 | ||
1549 | ret = io_do_iopoll(ctx, nr_events, min); | |
1550 | if (ret < 0) | |
1551 | return ret; | |
1552 | if (!min || *nr_events >= min) | |
1553 | return 0; | |
1554 | } | |
1555 | ||
1556 | return 1; | |
1557 | } | |
1558 | ||
1559 | /* | |
1560 | * We can't just wait for polled events to come to us, we have to actively | |
1561 | * find and complete them. | |
1562 | */ | |
1563 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) | |
1564 | { | |
1565 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
1566 | return; | |
1567 | ||
1568 | mutex_lock(&ctx->uring_lock); | |
1569 | while (!list_empty(&ctx->poll_list)) { | |
1570 | unsigned int nr_events = 0; | |
1571 | ||
1572 | io_iopoll_getevents(ctx, &nr_events, 1); | |
08f5439f JA |
1573 | |
1574 | /* | |
1575 | * Ensure we allow local-to-the-cpu processing to take place, | |
1576 | * in this case we need to ensure that we reap all events. | |
1577 | */ | |
1578 | cond_resched(); | |
def596e9 JA |
1579 | } |
1580 | mutex_unlock(&ctx->uring_lock); | |
1581 | } | |
1582 | ||
2b2ed975 JA |
1583 | static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
1584 | long min) | |
def596e9 | 1585 | { |
2b2ed975 | 1586 | int iters = 0, ret = 0; |
500f9fba | 1587 | |
def596e9 JA |
1588 | do { |
1589 | int tmin = 0; | |
1590 | ||
a3a0e43f JA |
1591 | /* |
1592 | * Don't enter poll loop if we already have events pending. | |
1593 | * If we do, we can potentially be spinning for commands that | |
1594 | * already triggered a CQE (eg in error). | |
1595 | */ | |
1d7bb1d5 | 1596 | if (io_cqring_events(ctx, false)) |
a3a0e43f JA |
1597 | break; |
1598 | ||
500f9fba JA |
1599 | /* |
1600 | * If a submit got punted to a workqueue, we can have the | |
1601 | * application entering polling for a command before it gets | |
1602 | * issued. That app will hold the uring_lock for the duration | |
1603 | * of the poll right here, so we need to take a breather every | |
1604 | * now and then to ensure that the issue has a chance to add | |
1605 | * the poll to the issued list. Otherwise we can spin here | |
1606 | * forever, while the workqueue is stuck trying to acquire the | |
1607 | * very same mutex. | |
1608 | */ | |
1609 | if (!(++iters & 7)) { | |
1610 | mutex_unlock(&ctx->uring_lock); | |
1611 | mutex_lock(&ctx->uring_lock); | |
1612 | } | |
1613 | ||
def596e9 JA |
1614 | if (*nr_events < min) |
1615 | tmin = min - *nr_events; | |
1616 | ||
1617 | ret = io_iopoll_getevents(ctx, nr_events, tmin); | |
1618 | if (ret <= 0) | |
1619 | break; | |
1620 | ret = 0; | |
1621 | } while (min && !*nr_events && !need_resched()); | |
1622 | ||
2b2ed975 JA |
1623 | return ret; |
1624 | } | |
1625 | ||
1626 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, | |
1627 | long min) | |
1628 | { | |
1629 | int ret; | |
1630 | ||
1631 | /* | |
1632 | * We disallow the app entering submit/complete with polling, but we | |
1633 | * still need to lock the ring to prevent racing with polled issue | |
1634 | * that got punted to a workqueue. | |
1635 | */ | |
1636 | mutex_lock(&ctx->uring_lock); | |
1637 | ret = __io_iopoll_check(ctx, nr_events, min); | |
500f9fba | 1638 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
1639 | return ret; |
1640 | } | |
1641 | ||
491381ce | 1642 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 1643 | { |
491381ce JA |
1644 | /* |
1645 | * Tell lockdep we inherited freeze protection from submission | |
1646 | * thread. | |
1647 | */ | |
1648 | if (req->flags & REQ_F_ISREG) { | |
1649 | struct inode *inode = file_inode(req->file); | |
2b188cc1 | 1650 | |
491381ce | 1651 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
2b188cc1 | 1652 | } |
491381ce | 1653 | file_end_write(req->file); |
2b188cc1 JA |
1654 | } |
1655 | ||
4e88d6e7 JA |
1656 | static inline void req_set_fail_links(struct io_kiocb *req) |
1657 | { | |
1658 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) | |
1659 | req->flags |= REQ_F_FAIL_LINK; | |
1660 | } | |
1661 | ||
ba816ad6 | 1662 | static void io_complete_rw_common(struct kiocb *kiocb, long res) |
2b188cc1 | 1663 | { |
9adbd45d | 1664 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
2b188cc1 | 1665 | |
491381ce JA |
1666 | if (kiocb->ki_flags & IOCB_WRITE) |
1667 | kiocb_end_write(req); | |
2b188cc1 | 1668 | |
4e88d6e7 JA |
1669 | if (res != req->result) |
1670 | req_set_fail_links(req); | |
78e19bbe | 1671 | io_cqring_add_event(req, res); |
ba816ad6 JA |
1672 | } |
1673 | ||
1674 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
1675 | { | |
9adbd45d | 1676 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 JA |
1677 | |
1678 | io_complete_rw_common(kiocb, res); | |
e65ef56d | 1679 | io_put_req(req); |
2b188cc1 JA |
1680 | } |
1681 | ||
ba816ad6 JA |
1682 | static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res) |
1683 | { | |
9adbd45d | 1684 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ec9c02ad | 1685 | struct io_kiocb *nxt = NULL; |
ba816ad6 JA |
1686 | |
1687 | io_complete_rw_common(kiocb, res); | |
ec9c02ad JL |
1688 | io_put_req_find_next(req, &nxt); |
1689 | ||
1690 | return nxt; | |
2b188cc1 JA |
1691 | } |
1692 | ||
def596e9 JA |
1693 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
1694 | { | |
9adbd45d | 1695 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 1696 | |
491381ce JA |
1697 | if (kiocb->ki_flags & IOCB_WRITE) |
1698 | kiocb_end_write(req); | |
def596e9 | 1699 | |
4e88d6e7 JA |
1700 | if (res != req->result) |
1701 | req_set_fail_links(req); | |
9e645e11 | 1702 | req->result = res; |
def596e9 JA |
1703 | if (res != -EAGAIN) |
1704 | req->flags |= REQ_F_IOPOLL_COMPLETED; | |
1705 | } | |
1706 | ||
1707 | /* | |
1708 | * After the iocb has been issued, it's safe to be found on the poll list. | |
1709 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
1710 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
1711 | * accessing the kiocb cookie. | |
1712 | */ | |
1713 | static void io_iopoll_req_issued(struct io_kiocb *req) | |
1714 | { | |
1715 | struct io_ring_ctx *ctx = req->ctx; | |
1716 | ||
1717 | /* | |
1718 | * Track whether we have multiple files in our lists. This will impact | |
1719 | * how we do polling eventually, not spinning if we're on potentially | |
1720 | * different devices. | |
1721 | */ | |
1722 | if (list_empty(&ctx->poll_list)) { | |
1723 | ctx->poll_multi_file = false; | |
1724 | } else if (!ctx->poll_multi_file) { | |
1725 | struct io_kiocb *list_req; | |
1726 | ||
1727 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, | |
1728 | list); | |
9adbd45d | 1729 | if (list_req->file != req->file) |
def596e9 JA |
1730 | ctx->poll_multi_file = true; |
1731 | } | |
1732 | ||
1733 | /* | |
1734 | * For fast devices, IO may have already completed. If it has, add | |
1735 | * it to the front so we find it first. | |
1736 | */ | |
1737 | if (req->flags & REQ_F_IOPOLL_COMPLETED) | |
1738 | list_add(&req->list, &ctx->poll_list); | |
1739 | else | |
1740 | list_add_tail(&req->list, &ctx->poll_list); | |
1741 | } | |
1742 | ||
3d6770fb | 1743 | static void io_file_put(struct io_submit_state *state) |
9a56a232 | 1744 | { |
3d6770fb | 1745 | if (state->file) { |
9a56a232 JA |
1746 | int diff = state->has_refs - state->used_refs; |
1747 | ||
1748 | if (diff) | |
1749 | fput_many(state->file, diff); | |
1750 | state->file = NULL; | |
1751 | } | |
1752 | } | |
1753 | ||
1754 | /* | |
1755 | * Get as many references to a file as we have IOs left in this submission, | |
1756 | * assuming most submissions are for one file, or at least that each file | |
1757 | * has more than one submission. | |
1758 | */ | |
1759 | static struct file *io_file_get(struct io_submit_state *state, int fd) | |
1760 | { | |
1761 | if (!state) | |
1762 | return fget(fd); | |
1763 | ||
1764 | if (state->file) { | |
1765 | if (state->fd == fd) { | |
1766 | state->used_refs++; | |
1767 | state->ios_left--; | |
1768 | return state->file; | |
1769 | } | |
3d6770fb | 1770 | io_file_put(state); |
9a56a232 JA |
1771 | } |
1772 | state->file = fget_many(fd, state->ios_left); | |
1773 | if (!state->file) | |
1774 | return NULL; | |
1775 | ||
1776 | state->fd = fd; | |
1777 | state->has_refs = state->ios_left; | |
1778 | state->used_refs = 1; | |
1779 | state->ios_left--; | |
1780 | return state->file; | |
1781 | } | |
1782 | ||
2b188cc1 JA |
1783 | /* |
1784 | * If we tracked the file through the SCM inflight mechanism, we could support | |
1785 | * any file. For now, just ensure that anything potentially problematic is done | |
1786 | * inline. | |
1787 | */ | |
1788 | static bool io_file_supports_async(struct file *file) | |
1789 | { | |
1790 | umode_t mode = file_inode(file)->i_mode; | |
1791 | ||
10d59345 | 1792 | if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode)) |
2b188cc1 JA |
1793 | return true; |
1794 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) | |
1795 | return true; | |
1796 | ||
1797 | return false; | |
1798 | } | |
1799 | ||
3529d8c2 JA |
1800 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
1801 | bool force_nonblock) | |
2b188cc1 | 1802 | { |
def596e9 | 1803 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 1804 | struct kiocb *kiocb = &req->rw.kiocb; |
09bb8394 JA |
1805 | unsigned ioprio; |
1806 | int ret; | |
2b188cc1 | 1807 | |
09bb8394 JA |
1808 | if (!req->file) |
1809 | return -EBADF; | |
2b188cc1 | 1810 | |
491381ce JA |
1811 | if (S_ISREG(file_inode(req->file)->i_mode)) |
1812 | req->flags |= REQ_F_ISREG; | |
1813 | ||
2b188cc1 | 1814 | kiocb->ki_pos = READ_ONCE(sqe->off); |
ba04291e JA |
1815 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
1816 | req->flags |= REQ_F_CUR_POS; | |
1817 | kiocb->ki_pos = req->file->f_pos; | |
1818 | } | |
2b188cc1 JA |
1819 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
1820 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); | |
1821 | ||
1822 | ioprio = READ_ONCE(sqe->ioprio); | |
1823 | if (ioprio) { | |
1824 | ret = ioprio_check_cap(ioprio); | |
1825 | if (ret) | |
09bb8394 | 1826 | return ret; |
2b188cc1 JA |
1827 | |
1828 | kiocb->ki_ioprio = ioprio; | |
1829 | } else | |
1830 | kiocb->ki_ioprio = get_current_ioprio(); | |
1831 | ||
1832 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
1833 | if (unlikely(ret)) | |
09bb8394 | 1834 | return ret; |
8449eeda SB |
1835 | |
1836 | /* don't allow async punt if RWF_NOWAIT was requested */ | |
491381ce JA |
1837 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
1838 | (req->file->f_flags & O_NONBLOCK)) | |
8449eeda SB |
1839 | req->flags |= REQ_F_NOWAIT; |
1840 | ||
1841 | if (force_nonblock) | |
2b188cc1 | 1842 | kiocb->ki_flags |= IOCB_NOWAIT; |
8449eeda | 1843 | |
def596e9 | 1844 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
1845 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
1846 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 1847 | return -EOPNOTSUPP; |
2b188cc1 | 1848 | |
def596e9 JA |
1849 | kiocb->ki_flags |= IOCB_HIPRI; |
1850 | kiocb->ki_complete = io_complete_rw_iopoll; | |
6873e0bd | 1851 | req->result = 0; |
def596e9 | 1852 | } else { |
09bb8394 JA |
1853 | if (kiocb->ki_flags & IOCB_HIPRI) |
1854 | return -EINVAL; | |
def596e9 JA |
1855 | kiocb->ki_complete = io_complete_rw; |
1856 | } | |
9adbd45d | 1857 | |
3529d8c2 JA |
1858 | req->rw.addr = READ_ONCE(sqe->addr); |
1859 | req->rw.len = READ_ONCE(sqe->len); | |
9adbd45d JA |
1860 | /* we own ->private, reuse it for the buffer index */ |
1861 | req->rw.kiocb.private = (void *) (unsigned long) | |
3529d8c2 | 1862 | READ_ONCE(sqe->buf_index); |
2b188cc1 | 1863 | return 0; |
2b188cc1 JA |
1864 | } |
1865 | ||
1866 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
1867 | { | |
1868 | switch (ret) { | |
1869 | case -EIOCBQUEUED: | |
1870 | break; | |
1871 | case -ERESTARTSYS: | |
1872 | case -ERESTARTNOINTR: | |
1873 | case -ERESTARTNOHAND: | |
1874 | case -ERESTART_RESTARTBLOCK: | |
1875 | /* | |
1876 | * We can't just restart the syscall, since previously | |
1877 | * submitted sqes may already be in progress. Just fail this | |
1878 | * IO with EINTR. | |
1879 | */ | |
1880 | ret = -EINTR; | |
1881 | /* fall through */ | |
1882 | default: | |
1883 | kiocb->ki_complete(kiocb, ret, 0); | |
1884 | } | |
1885 | } | |
1886 | ||
ba816ad6 JA |
1887 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt, |
1888 | bool in_async) | |
1889 | { | |
ba04291e JA |
1890 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
1891 | ||
1892 | if (req->flags & REQ_F_CUR_POS) | |
1893 | req->file->f_pos = kiocb->ki_pos; | |
f9bd67f6 | 1894 | if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw) |
ba816ad6 JA |
1895 | *nxt = __io_complete_rw(kiocb, ret); |
1896 | else | |
1897 | io_rw_done(kiocb, ret); | |
1898 | } | |
1899 | ||
9adbd45d | 1900 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
7d009165 | 1901 | struct iov_iter *iter) |
edafccee | 1902 | { |
9adbd45d JA |
1903 | struct io_ring_ctx *ctx = req->ctx; |
1904 | size_t len = req->rw.len; | |
edafccee JA |
1905 | struct io_mapped_ubuf *imu; |
1906 | unsigned index, buf_index; | |
1907 | size_t offset; | |
1908 | u64 buf_addr; | |
1909 | ||
1910 | /* attempt to use fixed buffers without having provided iovecs */ | |
1911 | if (unlikely(!ctx->user_bufs)) | |
1912 | return -EFAULT; | |
1913 | ||
9adbd45d | 1914 | buf_index = (unsigned long) req->rw.kiocb.private; |
edafccee JA |
1915 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
1916 | return -EFAULT; | |
1917 | ||
1918 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); | |
1919 | imu = &ctx->user_bufs[index]; | |
9adbd45d | 1920 | buf_addr = req->rw.addr; |
edafccee JA |
1921 | |
1922 | /* overflow */ | |
1923 | if (buf_addr + len < buf_addr) | |
1924 | return -EFAULT; | |
1925 | /* not inside the mapped region */ | |
1926 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) | |
1927 | return -EFAULT; | |
1928 | ||
1929 | /* | |
1930 | * May not be a start of buffer, set size appropriately | |
1931 | * and advance us to the beginning. | |
1932 | */ | |
1933 | offset = buf_addr - imu->ubuf; | |
1934 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
1935 | |
1936 | if (offset) { | |
1937 | /* | |
1938 | * Don't use iov_iter_advance() here, as it's really slow for | |
1939 | * using the latter parts of a big fixed buffer - it iterates | |
1940 | * over each segment manually. We can cheat a bit here, because | |
1941 | * we know that: | |
1942 | * | |
1943 | * 1) it's a BVEC iter, we set it up | |
1944 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
1945 | * first and last bvec | |
1946 | * | |
1947 | * So just find our index, and adjust the iterator afterwards. | |
1948 | * If the offset is within the first bvec (or the whole first | |
1949 | * bvec, just use iov_iter_advance(). This makes it easier | |
1950 | * since we can just skip the first segment, which may not | |
1951 | * be PAGE_SIZE aligned. | |
1952 | */ | |
1953 | const struct bio_vec *bvec = imu->bvec; | |
1954 | ||
1955 | if (offset <= bvec->bv_len) { | |
1956 | iov_iter_advance(iter, offset); | |
1957 | } else { | |
1958 | unsigned long seg_skip; | |
1959 | ||
1960 | /* skip first vec */ | |
1961 | offset -= bvec->bv_len; | |
1962 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
1963 | ||
1964 | iter->bvec = bvec + seg_skip; | |
1965 | iter->nr_segs -= seg_skip; | |
99c79f66 | 1966 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 1967 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
1968 | } |
1969 | } | |
1970 | ||
5e559561 | 1971 | return len; |
edafccee JA |
1972 | } |
1973 | ||
cf6fd4bd PB |
1974 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
1975 | struct iovec **iovec, struct iov_iter *iter) | |
2b188cc1 | 1976 | { |
9adbd45d JA |
1977 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
1978 | size_t sqe_len = req->rw.len; | |
edafccee JA |
1979 | u8 opcode; |
1980 | ||
d625c6ee | 1981 | opcode = req->opcode; |
7d009165 | 1982 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 1983 | *iovec = NULL; |
9adbd45d | 1984 | return io_import_fixed(req, rw, iter); |
edafccee | 1985 | } |
2b188cc1 | 1986 | |
9adbd45d JA |
1987 | /* buffer index only valid with fixed read/write */ |
1988 | if (req->rw.kiocb.private) | |
1989 | return -EINVAL; | |
1990 | ||
3a6820f2 JA |
1991 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
1992 | ssize_t ret; | |
1993 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); | |
1994 | *iovec = NULL; | |
1995 | return ret; | |
1996 | } | |
1997 | ||
f67676d1 JA |
1998 | if (req->io) { |
1999 | struct io_async_rw *iorw = &req->io->rw; | |
2000 | ||
2001 | *iovec = iorw->iov; | |
2002 | iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size); | |
2003 | if (iorw->iov == iorw->fast_iov) | |
2004 | *iovec = NULL; | |
2005 | return iorw->size; | |
2006 | } | |
2007 | ||
cf6fd4bd | 2008 | if (!req->has_user) |
2b188cc1 JA |
2009 | return -EFAULT; |
2010 | ||
2011 | #ifdef CONFIG_COMPAT | |
cf6fd4bd | 2012 | if (req->ctx->compat) |
2b188cc1 JA |
2013 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
2014 | iovec, iter); | |
2015 | #endif | |
2016 | ||
2017 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); | |
2018 | } | |
2019 | ||
31b51510 | 2020 | /* |
32960613 JA |
2021 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
2022 | * by looping over ->read() or ->write() manually. | |
31b51510 | 2023 | */ |
32960613 JA |
2024 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
2025 | struct iov_iter *iter) | |
2026 | { | |
2027 | ssize_t ret = 0; | |
2028 | ||
2029 | /* | |
2030 | * Don't support polled IO through this interface, and we can't | |
2031 | * support non-blocking either. For the latter, this just causes | |
2032 | * the kiocb to be handled from an async context. | |
2033 | */ | |
2034 | if (kiocb->ki_flags & IOCB_HIPRI) | |
2035 | return -EOPNOTSUPP; | |
2036 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
2037 | return -EAGAIN; | |
2038 | ||
2039 | while (iov_iter_count(iter)) { | |
311ae9e1 | 2040 | struct iovec iovec; |
32960613 JA |
2041 | ssize_t nr; |
2042 | ||
311ae9e1 PB |
2043 | if (!iov_iter_is_bvec(iter)) { |
2044 | iovec = iov_iter_iovec(iter); | |
2045 | } else { | |
2046 | /* fixed buffers import bvec */ | |
2047 | iovec.iov_base = kmap(iter->bvec->bv_page) | |
2048 | + iter->iov_offset; | |
2049 | iovec.iov_len = min(iter->count, | |
2050 | iter->bvec->bv_len - iter->iov_offset); | |
2051 | } | |
2052 | ||
32960613 JA |
2053 | if (rw == READ) { |
2054 | nr = file->f_op->read(file, iovec.iov_base, | |
2055 | iovec.iov_len, &kiocb->ki_pos); | |
2056 | } else { | |
2057 | nr = file->f_op->write(file, iovec.iov_base, | |
2058 | iovec.iov_len, &kiocb->ki_pos); | |
2059 | } | |
2060 | ||
311ae9e1 PB |
2061 | if (iov_iter_is_bvec(iter)) |
2062 | kunmap(iter->bvec->bv_page); | |
2063 | ||
32960613 JA |
2064 | if (nr < 0) { |
2065 | if (!ret) | |
2066 | ret = nr; | |
2067 | break; | |
2068 | } | |
2069 | ret += nr; | |
2070 | if (nr != iovec.iov_len) | |
2071 | break; | |
2072 | iov_iter_advance(iter, nr); | |
2073 | } | |
2074 | ||
2075 | return ret; | |
2076 | } | |
2077 | ||
b7bb4f7d | 2078 | static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size, |
f67676d1 JA |
2079 | struct iovec *iovec, struct iovec *fast_iov, |
2080 | struct iov_iter *iter) | |
2081 | { | |
2082 | req->io->rw.nr_segs = iter->nr_segs; | |
2083 | req->io->rw.size = io_size; | |
2084 | req->io->rw.iov = iovec; | |
2085 | if (!req->io->rw.iov) { | |
2086 | req->io->rw.iov = req->io->rw.fast_iov; | |
2087 | memcpy(req->io->rw.iov, fast_iov, | |
2088 | sizeof(struct iovec) * iter->nr_segs); | |
2089 | } | |
2090 | } | |
2091 | ||
b7bb4f7d | 2092 | static int io_alloc_async_ctx(struct io_kiocb *req) |
f67676d1 | 2093 | { |
d3656344 JA |
2094 | if (!io_op_defs[req->opcode].async_ctx) |
2095 | return 0; | |
f67676d1 | 2096 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); |
06b76d44 | 2097 | return req->io == NULL; |
b7bb4f7d JA |
2098 | } |
2099 | ||
2100 | static void io_rw_async(struct io_wq_work **workptr) | |
2101 | { | |
2102 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); | |
2103 | struct iovec *iov = NULL; | |
2104 | ||
2105 | if (req->io->rw.iov != req->io->rw.fast_iov) | |
2106 | iov = req->io->rw.iov; | |
2107 | io_wq_submit_work(workptr); | |
2108 | kfree(iov); | |
2109 | } | |
2110 | ||
2111 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, | |
2112 | struct iovec *iovec, struct iovec *fast_iov, | |
2113 | struct iov_iter *iter) | |
2114 | { | |
980ad263 | 2115 | if (!io_op_defs[req->opcode].async_ctx) |
74566df3 | 2116 | return 0; |
b7bb4f7d JA |
2117 | if (!req->io && io_alloc_async_ctx(req)) |
2118 | return -ENOMEM; | |
2119 | ||
2120 | io_req_map_rw(req, io_size, iovec, fast_iov, iter); | |
2121 | req->work.func = io_rw_async; | |
2122 | return 0; | |
f67676d1 JA |
2123 | } |
2124 | ||
3529d8c2 JA |
2125 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2126 | bool force_nonblock) | |
f67676d1 | 2127 | { |
3529d8c2 JA |
2128 | struct io_async_ctx *io; |
2129 | struct iov_iter iter; | |
f67676d1 JA |
2130 | ssize_t ret; |
2131 | ||
3529d8c2 JA |
2132 | ret = io_prep_rw(req, sqe, force_nonblock); |
2133 | if (ret) | |
2134 | return ret; | |
f67676d1 | 2135 | |
3529d8c2 JA |
2136 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
2137 | return -EBADF; | |
f67676d1 | 2138 | |
3529d8c2 JA |
2139 | if (!req->io) |
2140 | return 0; | |
2141 | ||
2142 | io = req->io; | |
2143 | io->rw.iov = io->rw.fast_iov; | |
2144 | req->io = NULL; | |
2145 | ret = io_import_iovec(READ, req, &io->rw.iov, &iter); | |
2146 | req->io = io; | |
2147 | if (ret < 0) | |
2148 | return ret; | |
2149 | ||
2150 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); | |
2151 | return 0; | |
f67676d1 JA |
2152 | } |
2153 | ||
267bc904 | 2154 | static int io_read(struct io_kiocb *req, struct io_kiocb **nxt, |
8358e3a8 | 2155 | bool force_nonblock) |
2b188cc1 JA |
2156 | { |
2157 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 2158 | struct kiocb *kiocb = &req->rw.kiocb; |
2b188cc1 | 2159 | struct iov_iter iter; |
31b51510 | 2160 | size_t iov_count; |
f67676d1 | 2161 | ssize_t io_size, ret; |
2b188cc1 | 2162 | |
3529d8c2 | 2163 | ret = io_import_iovec(READ, req, &iovec, &iter); |
06b76d44 JA |
2164 | if (ret < 0) |
2165 | return ret; | |
2b188cc1 | 2166 | |
fd6c2e4c JA |
2167 | /* Ensure we clear previously set non-block flag */ |
2168 | if (!force_nonblock) | |
9adbd45d | 2169 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
fd6c2e4c | 2170 | |
797f3f53 | 2171 | req->result = 0; |
f67676d1 | 2172 | io_size = ret; |
9e645e11 | 2173 | if (req->flags & REQ_F_LINK) |
f67676d1 JA |
2174 | req->result = io_size; |
2175 | ||
2176 | /* | |
2177 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so | |
2178 | * we know to async punt it even if it was opened O_NONBLOCK | |
2179 | */ | |
9adbd45d | 2180 | if (force_nonblock && !io_file_supports_async(req->file)) { |
f67676d1 JA |
2181 | req->flags |= REQ_F_MUST_PUNT; |
2182 | goto copy_iov; | |
2183 | } | |
9e645e11 | 2184 | |
31b51510 | 2185 | iov_count = iov_iter_count(&iter); |
9adbd45d | 2186 | ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count); |
2b188cc1 JA |
2187 | if (!ret) { |
2188 | ssize_t ret2; | |
2189 | ||
9adbd45d JA |
2190 | if (req->file->f_op->read_iter) |
2191 | ret2 = call_read_iter(req->file, kiocb, &iter); | |
32960613 | 2192 | else |
9adbd45d | 2193 | ret2 = loop_rw_iter(READ, req->file, kiocb, &iter); |
32960613 | 2194 | |
9d93a3f5 | 2195 | /* Catch -EAGAIN return for forced non-blocking submission */ |
f67676d1 | 2196 | if (!force_nonblock || ret2 != -EAGAIN) { |
cf6fd4bd | 2197 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
f67676d1 JA |
2198 | } else { |
2199 | copy_iov: | |
b7bb4f7d | 2200 | ret = io_setup_async_rw(req, io_size, iovec, |
f67676d1 JA |
2201 | inline_vecs, &iter); |
2202 | if (ret) | |
2203 | goto out_free; | |
2204 | return -EAGAIN; | |
2205 | } | |
2b188cc1 | 2206 | } |
f67676d1 | 2207 | out_free: |
b7bb4f7d JA |
2208 | if (!io_wq_current_is_worker()) |
2209 | kfree(iovec); | |
2b188cc1 JA |
2210 | return ret; |
2211 | } | |
2212 | ||
3529d8c2 JA |
2213 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2214 | bool force_nonblock) | |
f67676d1 | 2215 | { |
3529d8c2 JA |
2216 | struct io_async_ctx *io; |
2217 | struct iov_iter iter; | |
f67676d1 JA |
2218 | ssize_t ret; |
2219 | ||
3529d8c2 JA |
2220 | ret = io_prep_rw(req, sqe, force_nonblock); |
2221 | if (ret) | |
2222 | return ret; | |
f67676d1 | 2223 | |
3529d8c2 JA |
2224 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
2225 | return -EBADF; | |
f67676d1 | 2226 | |
3529d8c2 JA |
2227 | if (!req->io) |
2228 | return 0; | |
2229 | ||
2230 | io = req->io; | |
2231 | io->rw.iov = io->rw.fast_iov; | |
2232 | req->io = NULL; | |
2233 | ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter); | |
2234 | req->io = io; | |
2235 | if (ret < 0) | |
2236 | return ret; | |
2237 | ||
2238 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); | |
2239 | return 0; | |
f67676d1 JA |
2240 | } |
2241 | ||
267bc904 | 2242 | static int io_write(struct io_kiocb *req, struct io_kiocb **nxt, |
8358e3a8 | 2243 | bool force_nonblock) |
2b188cc1 JA |
2244 | { |
2245 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 2246 | struct kiocb *kiocb = &req->rw.kiocb; |
2b188cc1 | 2247 | struct iov_iter iter; |
31b51510 | 2248 | size_t iov_count; |
f67676d1 | 2249 | ssize_t ret, io_size; |
2b188cc1 | 2250 | |
3529d8c2 | 2251 | ret = io_import_iovec(WRITE, req, &iovec, &iter); |
06b76d44 JA |
2252 | if (ret < 0) |
2253 | return ret; | |
2b188cc1 | 2254 | |
fd6c2e4c JA |
2255 | /* Ensure we clear previously set non-block flag */ |
2256 | if (!force_nonblock) | |
9adbd45d | 2257 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
fd6c2e4c | 2258 | |
797f3f53 | 2259 | req->result = 0; |
f67676d1 | 2260 | io_size = ret; |
9e645e11 | 2261 | if (req->flags & REQ_F_LINK) |
f67676d1 | 2262 | req->result = io_size; |
9e645e11 | 2263 | |
f67676d1 JA |
2264 | /* |
2265 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so | |
2266 | * we know to async punt it even if it was opened O_NONBLOCK | |
2267 | */ | |
2268 | if (force_nonblock && !io_file_supports_async(req->file)) { | |
2269 | req->flags |= REQ_F_MUST_PUNT; | |
2270 | goto copy_iov; | |
2271 | } | |
31b51510 | 2272 | |
10d59345 JA |
2273 | /* file path doesn't support NOWAIT for non-direct_IO */ |
2274 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
2275 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 2276 | goto copy_iov; |
31b51510 | 2277 | |
f67676d1 | 2278 | iov_count = iov_iter_count(&iter); |
9adbd45d | 2279 | ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count); |
2b188cc1 | 2280 | if (!ret) { |
9bf7933f RP |
2281 | ssize_t ret2; |
2282 | ||
2b188cc1 JA |
2283 | /* |
2284 | * Open-code file_start_write here to grab freeze protection, | |
2285 | * which will be released by another thread in | |
2286 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
2287 | * released so that it doesn't complain about the held lock when | |
2288 | * we return to userspace. | |
2289 | */ | |
491381ce | 2290 | if (req->flags & REQ_F_ISREG) { |
9adbd45d | 2291 | __sb_start_write(file_inode(req->file)->i_sb, |
2b188cc1 | 2292 | SB_FREEZE_WRITE, true); |
9adbd45d | 2293 | __sb_writers_release(file_inode(req->file)->i_sb, |
2b188cc1 JA |
2294 | SB_FREEZE_WRITE); |
2295 | } | |
2296 | kiocb->ki_flags |= IOCB_WRITE; | |
9bf7933f | 2297 | |
9adbd45d JA |
2298 | if (req->file->f_op->write_iter) |
2299 | ret2 = call_write_iter(req->file, kiocb, &iter); | |
32960613 | 2300 | else |
9adbd45d | 2301 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter); |
f67676d1 | 2302 | if (!force_nonblock || ret2 != -EAGAIN) { |
cf6fd4bd | 2303 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
f67676d1 JA |
2304 | } else { |
2305 | copy_iov: | |
b7bb4f7d | 2306 | ret = io_setup_async_rw(req, io_size, iovec, |
f67676d1 JA |
2307 | inline_vecs, &iter); |
2308 | if (ret) | |
2309 | goto out_free; | |
2310 | return -EAGAIN; | |
2311 | } | |
2b188cc1 | 2312 | } |
31b51510 | 2313 | out_free: |
b7bb4f7d JA |
2314 | if (!io_wq_current_is_worker()) |
2315 | kfree(iovec); | |
2b188cc1 JA |
2316 | return ret; |
2317 | } | |
2318 | ||
2319 | /* | |
2320 | * IORING_OP_NOP just posts a completion event, nothing else. | |
2321 | */ | |
78e19bbe | 2322 | static int io_nop(struct io_kiocb *req) |
2b188cc1 JA |
2323 | { |
2324 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 2325 | |
def596e9 JA |
2326 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
2327 | return -EINVAL; | |
2328 | ||
78e19bbe | 2329 | io_cqring_add_event(req, 0); |
e65ef56d | 2330 | io_put_req(req); |
2b188cc1 JA |
2331 | return 0; |
2332 | } | |
2333 | ||
3529d8c2 | 2334 | static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 2335 | { |
6b06314c | 2336 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 2337 | |
09bb8394 JA |
2338 | if (!req->file) |
2339 | return -EBADF; | |
c992fe29 | 2340 | |
6b06314c | 2341 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 2342 | return -EINVAL; |
edafccee | 2343 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
2344 | return -EINVAL; |
2345 | ||
8ed8d3c3 JA |
2346 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
2347 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
2348 | return -EINVAL; | |
2349 | ||
2350 | req->sync.off = READ_ONCE(sqe->off); | |
2351 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
2352 | return 0; |
2353 | } | |
2354 | ||
8ed8d3c3 JA |
2355 | static bool io_req_cancelled(struct io_kiocb *req) |
2356 | { | |
2357 | if (req->work.flags & IO_WQ_WORK_CANCEL) { | |
2358 | req_set_fail_links(req); | |
2359 | io_cqring_add_event(req, -ECANCELED); | |
2360 | io_put_req(req); | |
2361 | return true; | |
2362 | } | |
2363 | ||
2364 | return false; | |
2365 | } | |
2366 | ||
78912934 JA |
2367 | static void io_link_work_cb(struct io_wq_work **workptr) |
2368 | { | |
2369 | struct io_wq_work *work = *workptr; | |
2370 | struct io_kiocb *link = work->data; | |
2371 | ||
2372 | io_queue_linked_timeout(link); | |
2373 | work->func = io_wq_submit_work; | |
2374 | } | |
2375 | ||
2376 | static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt) | |
2377 | { | |
2378 | struct io_kiocb *link; | |
2379 | ||
2380 | io_prep_async_work(nxt, &link); | |
2381 | *workptr = &nxt->work; | |
2382 | if (link) { | |
2383 | nxt->work.flags |= IO_WQ_WORK_CB; | |
2384 | nxt->work.func = io_link_work_cb; | |
2385 | nxt->work.data = link; | |
2386 | } | |
2387 | } | |
2388 | ||
8ed8d3c3 JA |
2389 | static void io_fsync_finish(struct io_wq_work **workptr) |
2390 | { | |
2391 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); | |
2392 | loff_t end = req->sync.off + req->sync.len; | |
2393 | struct io_kiocb *nxt = NULL; | |
2394 | int ret; | |
2395 | ||
2396 | if (io_req_cancelled(req)) | |
2397 | return; | |
2398 | ||
9adbd45d | 2399 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
2400 | end > 0 ? end : LLONG_MAX, |
2401 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
2402 | if (ret < 0) | |
2403 | req_set_fail_links(req); | |
2404 | io_cqring_add_event(req, ret); | |
2405 | io_put_req_find_next(req, &nxt); | |
2406 | if (nxt) | |
78912934 | 2407 | io_wq_assign_next(workptr, nxt); |
8ed8d3c3 JA |
2408 | } |
2409 | ||
fc4df999 JA |
2410 | static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt, |
2411 | bool force_nonblock) | |
c992fe29 | 2412 | { |
8ed8d3c3 | 2413 | struct io_wq_work *work, *old_work; |
c992fe29 CH |
2414 | |
2415 | /* fsync always requires a blocking context */ | |
8ed8d3c3 JA |
2416 | if (force_nonblock) { |
2417 | io_put_req(req); | |
2418 | req->work.func = io_fsync_finish; | |
c992fe29 | 2419 | return -EAGAIN; |
8ed8d3c3 | 2420 | } |
c992fe29 | 2421 | |
8ed8d3c3 JA |
2422 | work = old_work = &req->work; |
2423 | io_fsync_finish(&work); | |
2424 | if (work && work != old_work) | |
2425 | *nxt = container_of(work, struct io_kiocb, work); | |
c992fe29 CH |
2426 | return 0; |
2427 | } | |
2428 | ||
d63d1b5e JA |
2429 | static void io_fallocate_finish(struct io_wq_work **workptr) |
2430 | { | |
2431 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); | |
2432 | struct io_kiocb *nxt = NULL; | |
2433 | int ret; | |
2434 | ||
2435 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, | |
2436 | req->sync.len); | |
2437 | if (ret < 0) | |
2438 | req_set_fail_links(req); | |
2439 | io_cqring_add_event(req, ret); | |
2440 | io_put_req_find_next(req, &nxt); | |
2441 | if (nxt) | |
2442 | io_wq_assign_next(workptr, nxt); | |
2443 | } | |
2444 | ||
2445 | static int io_fallocate_prep(struct io_kiocb *req, | |
2446 | const struct io_uring_sqe *sqe) | |
2447 | { | |
2448 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
2449 | return -EINVAL; | |
2450 | ||
2451 | req->sync.off = READ_ONCE(sqe->off); | |
2452 | req->sync.len = READ_ONCE(sqe->addr); | |
2453 | req->sync.mode = READ_ONCE(sqe->len); | |
2454 | return 0; | |
2455 | } | |
2456 | ||
2457 | static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt, | |
2458 | bool force_nonblock) | |
2459 | { | |
2460 | struct io_wq_work *work, *old_work; | |
2461 | ||
2462 | /* fallocate always requiring blocking context */ | |
2463 | if (force_nonblock) { | |
2464 | io_put_req(req); | |
2465 | req->work.func = io_fallocate_finish; | |
2466 | return -EAGAIN; | |
2467 | } | |
2468 | ||
2469 | work = old_work = &req->work; | |
2470 | io_fallocate_finish(&work); | |
2471 | if (work && work != old_work) | |
2472 | *nxt = container_of(work, struct io_kiocb, work); | |
2473 | ||
2474 | return 0; | |
2475 | } | |
2476 | ||
15b71abe JA |
2477 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2478 | { | |
f8748881 | 2479 | const char __user *fname; |
15b71abe JA |
2480 | int ret; |
2481 | ||
2482 | if (sqe->ioprio || sqe->buf_index) | |
2483 | return -EINVAL; | |
2484 | ||
2485 | req->open.dfd = READ_ONCE(sqe->fd); | |
c12cedf2 | 2486 | req->open.how.mode = READ_ONCE(sqe->len); |
f8748881 | 2487 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
c12cedf2 | 2488 | req->open.how.flags = READ_ONCE(sqe->open_flags); |
15b71abe | 2489 | |
f8748881 | 2490 | req->open.filename = getname(fname); |
15b71abe JA |
2491 | if (IS_ERR(req->open.filename)) { |
2492 | ret = PTR_ERR(req->open.filename); | |
2493 | req->open.filename = NULL; | |
2494 | return ret; | |
2495 | } | |
2496 | ||
2497 | return 0; | |
2498 | } | |
2499 | ||
cebdb986 JA |
2500 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2501 | { | |
2502 | struct open_how __user *how; | |
2503 | const char __user *fname; | |
2504 | size_t len; | |
2505 | int ret; | |
2506 | ||
2507 | if (sqe->ioprio || sqe->buf_index) | |
2508 | return -EINVAL; | |
2509 | ||
2510 | req->open.dfd = READ_ONCE(sqe->fd); | |
2511 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
2512 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
2513 | len = READ_ONCE(sqe->len); | |
2514 | ||
2515 | if (len < OPEN_HOW_SIZE_VER0) | |
2516 | return -EINVAL; | |
2517 | ||
2518 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, | |
2519 | len); | |
2520 | if (ret) | |
2521 | return ret; | |
2522 | ||
2523 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
2524 | req->open.how.flags |= O_LARGEFILE; | |
2525 | ||
2526 | req->open.filename = getname(fname); | |
2527 | if (IS_ERR(req->open.filename)) { | |
2528 | ret = PTR_ERR(req->open.filename); | |
2529 | req->open.filename = NULL; | |
2530 | return ret; | |
2531 | } | |
2532 | ||
2533 | return 0; | |
2534 | } | |
2535 | ||
2536 | static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt, | |
2537 | bool force_nonblock) | |
15b71abe JA |
2538 | { |
2539 | struct open_flags op; | |
15b71abe JA |
2540 | struct file *file; |
2541 | int ret; | |
2542 | ||
2543 | if (force_nonblock) { | |
2544 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; | |
2545 | return -EAGAIN; | |
2546 | } | |
2547 | ||
cebdb986 | 2548 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
2549 | if (ret) |
2550 | goto err; | |
2551 | ||
cebdb986 | 2552 | ret = get_unused_fd_flags(req->open.how.flags); |
15b71abe JA |
2553 | if (ret < 0) |
2554 | goto err; | |
2555 | ||
2556 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
2557 | if (IS_ERR(file)) { | |
2558 | put_unused_fd(ret); | |
2559 | ret = PTR_ERR(file); | |
2560 | } else { | |
2561 | fsnotify_open(file); | |
2562 | fd_install(ret, file); | |
2563 | } | |
2564 | err: | |
2565 | putname(req->open.filename); | |
2566 | if (ret < 0) | |
2567 | req_set_fail_links(req); | |
2568 | io_cqring_add_event(req, ret); | |
2569 | io_put_req_find_next(req, nxt); | |
2570 | return 0; | |
2571 | } | |
2572 | ||
cebdb986 JA |
2573 | static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt, |
2574 | bool force_nonblock) | |
2575 | { | |
2576 | req->open.how = build_open_how(req->open.how.flags, req->open.how.mode); | |
2577 | return io_openat2(req, nxt, force_nonblock); | |
2578 | } | |
2579 | ||
c1ca757b JA |
2580 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2581 | { | |
2582 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
2583 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
2584 | return -EINVAL; | |
2585 | ||
2586 | req->madvise.addr = READ_ONCE(sqe->addr); | |
2587 | req->madvise.len = READ_ONCE(sqe->len); | |
2588 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
2589 | return 0; | |
2590 | #else | |
2591 | return -EOPNOTSUPP; | |
2592 | #endif | |
2593 | } | |
2594 | ||
2595 | static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt, | |
2596 | bool force_nonblock) | |
2597 | { | |
2598 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
2599 | struct io_madvise *ma = &req->madvise; | |
2600 | int ret; | |
2601 | ||
2602 | if (force_nonblock) | |
2603 | return -EAGAIN; | |
2604 | ||
2605 | ret = do_madvise(ma->addr, ma->len, ma->advice); | |
2606 | if (ret < 0) | |
2607 | req_set_fail_links(req); | |
2608 | io_cqring_add_event(req, ret); | |
2609 | io_put_req_find_next(req, nxt); | |
2610 | return 0; | |
2611 | #else | |
2612 | return -EOPNOTSUPP; | |
2613 | #endif | |
2614 | } | |
2615 | ||
4840e418 JA |
2616 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2617 | { | |
2618 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
2619 | return -EINVAL; | |
2620 | ||
2621 | req->fadvise.offset = READ_ONCE(sqe->off); | |
2622 | req->fadvise.len = READ_ONCE(sqe->len); | |
2623 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
2624 | return 0; | |
2625 | } | |
2626 | ||
2627 | static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt, | |
2628 | bool force_nonblock) | |
2629 | { | |
2630 | struct io_fadvise *fa = &req->fadvise; | |
2631 | int ret; | |
2632 | ||
2633 | /* DONTNEED may block, others _should_ not */ | |
2634 | if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock) | |
2635 | return -EAGAIN; | |
2636 | ||
2637 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
2638 | if (ret < 0) | |
2639 | req_set_fail_links(req); | |
2640 | io_cqring_add_event(req, ret); | |
2641 | io_put_req_find_next(req, nxt); | |
2642 | return 0; | |
2643 | } | |
2644 | ||
eddc7ef5 JA |
2645 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2646 | { | |
f8748881 | 2647 | const char __user *fname; |
eddc7ef5 JA |
2648 | unsigned lookup_flags; |
2649 | int ret; | |
2650 | ||
2651 | if (sqe->ioprio || sqe->buf_index) | |
2652 | return -EINVAL; | |
2653 | ||
2654 | req->open.dfd = READ_ONCE(sqe->fd); | |
2655 | req->open.mask = READ_ONCE(sqe->len); | |
f8748881 | 2656 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
eddc7ef5 | 2657 | req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
c12cedf2 | 2658 | req->open.how.flags = READ_ONCE(sqe->statx_flags); |
eddc7ef5 | 2659 | |
c12cedf2 | 2660 | if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags)) |
eddc7ef5 JA |
2661 | return -EINVAL; |
2662 | ||
f8748881 | 2663 | req->open.filename = getname_flags(fname, lookup_flags, NULL); |
eddc7ef5 JA |
2664 | if (IS_ERR(req->open.filename)) { |
2665 | ret = PTR_ERR(req->open.filename); | |
2666 | req->open.filename = NULL; | |
2667 | return ret; | |
2668 | } | |
2669 | ||
2670 | return 0; | |
2671 | } | |
2672 | ||
2673 | static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt, | |
2674 | bool force_nonblock) | |
2675 | { | |
2676 | struct io_open *ctx = &req->open; | |
2677 | unsigned lookup_flags; | |
2678 | struct path path; | |
2679 | struct kstat stat; | |
2680 | int ret; | |
2681 | ||
2682 | if (force_nonblock) | |
2683 | return -EAGAIN; | |
2684 | ||
c12cedf2 | 2685 | if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags)) |
eddc7ef5 JA |
2686 | return -EINVAL; |
2687 | ||
2688 | retry: | |
2689 | /* filename_lookup() drops it, keep a reference */ | |
2690 | ctx->filename->refcnt++; | |
2691 | ||
2692 | ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path, | |
2693 | NULL); | |
2694 | if (ret) | |
2695 | goto err; | |
2696 | ||
c12cedf2 | 2697 | ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags); |
eddc7ef5 JA |
2698 | path_put(&path); |
2699 | if (retry_estale(ret, lookup_flags)) { | |
2700 | lookup_flags |= LOOKUP_REVAL; | |
2701 | goto retry; | |
2702 | } | |
2703 | if (!ret) | |
2704 | ret = cp_statx(&stat, ctx->buffer); | |
2705 | err: | |
2706 | putname(ctx->filename); | |
2707 | if (ret < 0) | |
2708 | req_set_fail_links(req); | |
2709 | io_cqring_add_event(req, ret); | |
2710 | io_put_req_find_next(req, nxt); | |
2711 | return 0; | |
2712 | } | |
2713 | ||
b5dba59e JA |
2714 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2715 | { | |
2716 | /* | |
2717 | * If we queue this for async, it must not be cancellable. That would | |
2718 | * leave the 'file' in an undeterminate state. | |
2719 | */ | |
2720 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; | |
2721 | ||
2722 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || | |
2723 | sqe->rw_flags || sqe->buf_index) | |
2724 | return -EINVAL; | |
2725 | if (sqe->flags & IOSQE_FIXED_FILE) | |
2726 | return -EINVAL; | |
2727 | ||
2728 | req->close.fd = READ_ONCE(sqe->fd); | |
2729 | if (req->file->f_op == &io_uring_fops || | |
b14cca0c | 2730 | req->close.fd == req->ctx->ring_fd) |
b5dba59e JA |
2731 | return -EBADF; |
2732 | ||
2733 | return 0; | |
2734 | } | |
2735 | ||
2736 | static void io_close_finish(struct io_wq_work **workptr) | |
2737 | { | |
2738 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); | |
2739 | struct io_kiocb *nxt = NULL; | |
2740 | ||
2741 | /* Invoked with files, we need to do the close */ | |
2742 | if (req->work.files) { | |
2743 | int ret; | |
2744 | ||
2745 | ret = filp_close(req->close.put_file, req->work.files); | |
2746 | if (ret < 0) { | |
2747 | req_set_fail_links(req); | |
2748 | } | |
2749 | io_cqring_add_event(req, ret); | |
2750 | } | |
2751 | ||
2752 | fput(req->close.put_file); | |
2753 | ||
2754 | /* we bypassed the re-issue, drop the submission reference */ | |
2755 | io_put_req(req); | |
2756 | io_put_req_find_next(req, &nxt); | |
2757 | if (nxt) | |
2758 | io_wq_assign_next(workptr, nxt); | |
2759 | } | |
2760 | ||
2761 | static int io_close(struct io_kiocb *req, struct io_kiocb **nxt, | |
2762 | bool force_nonblock) | |
2763 | { | |
2764 | int ret; | |
2765 | ||
2766 | req->close.put_file = NULL; | |
2767 | ret = __close_fd_get_file(req->close.fd, &req->close.put_file); | |
2768 | if (ret < 0) | |
2769 | return ret; | |
2770 | ||
2771 | /* if the file has a flush method, be safe and punt to async */ | |
2772 | if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) { | |
2773 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; | |
2774 | goto eagain; | |
2775 | } | |
2776 | ||
2777 | /* | |
2778 | * No ->flush(), safely close from here and just punt the | |
2779 | * fput() to async context. | |
2780 | */ | |
2781 | ret = filp_close(req->close.put_file, current->files); | |
2782 | ||
2783 | if (ret < 0) | |
2784 | req_set_fail_links(req); | |
2785 | io_cqring_add_event(req, ret); | |
2786 | ||
2787 | if (io_wq_current_is_worker()) { | |
2788 | struct io_wq_work *old_work, *work; | |
2789 | ||
2790 | old_work = work = &req->work; | |
2791 | io_close_finish(&work); | |
2792 | if (work && work != old_work) | |
2793 | *nxt = container_of(work, struct io_kiocb, work); | |
2794 | return 0; | |
2795 | } | |
2796 | ||
2797 | eagain: | |
2798 | req->work.func = io_close_finish; | |
2799 | return -EAGAIN; | |
2800 | } | |
2801 | ||
3529d8c2 | 2802 | static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
2803 | { |
2804 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 JA |
2805 | |
2806 | if (!req->file) | |
2807 | return -EBADF; | |
5d17b4a4 JA |
2808 | |
2809 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) | |
2810 | return -EINVAL; | |
2811 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
2812 | return -EINVAL; | |
2813 | ||
8ed8d3c3 JA |
2814 | req->sync.off = READ_ONCE(sqe->off); |
2815 | req->sync.len = READ_ONCE(sqe->len); | |
2816 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
2817 | return 0; |
2818 | } | |
2819 | ||
2820 | static void io_sync_file_range_finish(struct io_wq_work **workptr) | |
2821 | { | |
2822 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); | |
2823 | struct io_kiocb *nxt = NULL; | |
2824 | int ret; | |
2825 | ||
2826 | if (io_req_cancelled(req)) | |
2827 | return; | |
2828 | ||
9adbd45d | 2829 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
2830 | req->sync.flags); |
2831 | if (ret < 0) | |
2832 | req_set_fail_links(req); | |
2833 | io_cqring_add_event(req, ret); | |
2834 | io_put_req_find_next(req, &nxt); | |
2835 | if (nxt) | |
78912934 | 2836 | io_wq_assign_next(workptr, nxt); |
5d17b4a4 JA |
2837 | } |
2838 | ||
fc4df999 | 2839 | static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt, |
5d17b4a4 JA |
2840 | bool force_nonblock) |
2841 | { | |
8ed8d3c3 | 2842 | struct io_wq_work *work, *old_work; |
5d17b4a4 JA |
2843 | |
2844 | /* sync_file_range always requires a blocking context */ | |
8ed8d3c3 JA |
2845 | if (force_nonblock) { |
2846 | io_put_req(req); | |
2847 | req->work.func = io_sync_file_range_finish; | |
5d17b4a4 | 2848 | return -EAGAIN; |
8ed8d3c3 | 2849 | } |
5d17b4a4 | 2850 | |
8ed8d3c3 JA |
2851 | work = old_work = &req->work; |
2852 | io_sync_file_range_finish(&work); | |
2853 | if (work && work != old_work) | |
2854 | *nxt = container_of(work, struct io_kiocb, work); | |
5d17b4a4 JA |
2855 | return 0; |
2856 | } | |
2857 | ||
b7bb4f7d JA |
2858 | #if defined(CONFIG_NET) |
2859 | static void io_sendrecv_async(struct io_wq_work **workptr) | |
2860 | { | |
2861 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); | |
2862 | struct iovec *iov = NULL; | |
2863 | ||
2864 | if (req->io->rw.iov != req->io->rw.fast_iov) | |
2865 | iov = req->io->msg.iov; | |
2866 | io_wq_submit_work(workptr); | |
2867 | kfree(iov); | |
2868 | } | |
2869 | #endif | |
2870 | ||
3529d8c2 | 2871 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 2872 | { |
0fa03c62 | 2873 | #if defined(CONFIG_NET) |
e47293fd | 2874 | struct io_sr_msg *sr = &req->sr_msg; |
3529d8c2 | 2875 | struct io_async_ctx *io = req->io; |
03b1230c | 2876 | |
e47293fd JA |
2877 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
2878 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
fddaface | 2879 | sr->len = READ_ONCE(sqe->len); |
3529d8c2 | 2880 | |
fddaface | 2881 | if (!io || req->opcode == IORING_OP_SEND) |
3529d8c2 JA |
2882 | return 0; |
2883 | ||
d9688565 | 2884 | io->msg.iov = io->msg.fast_iov; |
3529d8c2 | 2885 | return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
e47293fd | 2886 | &io->msg.iov); |
03b1230c | 2887 | #else |
e47293fd | 2888 | return -EOPNOTSUPP; |
03b1230c JA |
2889 | #endif |
2890 | } | |
2891 | ||
fc4df999 JA |
2892 | static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
2893 | bool force_nonblock) | |
aa1fa28f | 2894 | { |
03b1230c | 2895 | #if defined(CONFIG_NET) |
0b416c3e | 2896 | struct io_async_msghdr *kmsg = NULL; |
0fa03c62 JA |
2897 | struct socket *sock; |
2898 | int ret; | |
2899 | ||
2900 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
2901 | return -EINVAL; | |
2902 | ||
2903 | sock = sock_from_file(req->file, &ret); | |
2904 | if (sock) { | |
b7bb4f7d | 2905 | struct io_async_ctx io; |
03b1230c | 2906 | struct sockaddr_storage addr; |
0fa03c62 JA |
2907 | unsigned flags; |
2908 | ||
03b1230c | 2909 | if (req->io) { |
0b416c3e JA |
2910 | kmsg = &req->io->msg; |
2911 | kmsg->msg.msg_name = &addr; | |
2912 | /* if iov is set, it's allocated already */ | |
2913 | if (!kmsg->iov) | |
2914 | kmsg->iov = kmsg->fast_iov; | |
2915 | kmsg->msg.msg_iter.iov = kmsg->iov; | |
03b1230c | 2916 | } else { |
3529d8c2 JA |
2917 | struct io_sr_msg *sr = &req->sr_msg; |
2918 | ||
0b416c3e JA |
2919 | kmsg = &io.msg; |
2920 | kmsg->msg.msg_name = &addr; | |
3529d8c2 JA |
2921 | |
2922 | io.msg.iov = io.msg.fast_iov; | |
2923 | ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg, | |
2924 | sr->msg_flags, &io.msg.iov); | |
03b1230c | 2925 | if (ret) |
3529d8c2 | 2926 | return ret; |
03b1230c | 2927 | } |
0fa03c62 | 2928 | |
e47293fd JA |
2929 | flags = req->sr_msg.msg_flags; |
2930 | if (flags & MSG_DONTWAIT) | |
2931 | req->flags |= REQ_F_NOWAIT; | |
2932 | else if (force_nonblock) | |
2933 | flags |= MSG_DONTWAIT; | |
2934 | ||
0b416c3e | 2935 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
03b1230c | 2936 | if (force_nonblock && ret == -EAGAIN) { |
b7bb4f7d JA |
2937 | if (req->io) |
2938 | return -EAGAIN; | |
2939 | if (io_alloc_async_ctx(req)) | |
2940 | return -ENOMEM; | |
2941 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); | |
2942 | req->work.func = io_sendrecv_async; | |
0b416c3e | 2943 | return -EAGAIN; |
03b1230c | 2944 | } |
441cdbd5 JA |
2945 | if (ret == -ERESTARTSYS) |
2946 | ret = -EINTR; | |
0fa03c62 JA |
2947 | } |
2948 | ||
b7bb4f7d | 2949 | if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov) |
0b416c3e | 2950 | kfree(kmsg->iov); |
78e19bbe | 2951 | io_cqring_add_event(req, ret); |
4e88d6e7 JA |
2952 | if (ret < 0) |
2953 | req_set_fail_links(req); | |
ec9c02ad | 2954 | io_put_req_find_next(req, nxt); |
5d17b4a4 | 2955 | return 0; |
03b1230c JA |
2956 | #else |
2957 | return -EOPNOTSUPP; | |
aa1fa28f | 2958 | #endif |
03b1230c | 2959 | } |
aa1fa28f | 2960 | |
fddaface JA |
2961 | static int io_send(struct io_kiocb *req, struct io_kiocb **nxt, |
2962 | bool force_nonblock) | |
2963 | { | |
2964 | #if defined(CONFIG_NET) | |
2965 | struct socket *sock; | |
2966 | int ret; | |
2967 | ||
2968 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
2969 | return -EINVAL; | |
2970 | ||
2971 | sock = sock_from_file(req->file, &ret); | |
2972 | if (sock) { | |
2973 | struct io_sr_msg *sr = &req->sr_msg; | |
2974 | struct msghdr msg; | |
2975 | struct iovec iov; | |
2976 | unsigned flags; | |
2977 | ||
2978 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, | |
2979 | &msg.msg_iter); | |
2980 | if (ret) | |
2981 | return ret; | |
2982 | ||
2983 | msg.msg_name = NULL; | |
2984 | msg.msg_control = NULL; | |
2985 | msg.msg_controllen = 0; | |
2986 | msg.msg_namelen = 0; | |
2987 | ||
2988 | flags = req->sr_msg.msg_flags; | |
2989 | if (flags & MSG_DONTWAIT) | |
2990 | req->flags |= REQ_F_NOWAIT; | |
2991 | else if (force_nonblock) | |
2992 | flags |= MSG_DONTWAIT; | |
2993 | ||
2994 | ret = __sys_sendmsg_sock(sock, &msg, flags); | |
2995 | if (force_nonblock && ret == -EAGAIN) | |
2996 | return -EAGAIN; | |
2997 | if (ret == -ERESTARTSYS) | |
2998 | ret = -EINTR; | |
2999 | } | |
3000 | ||
3001 | io_cqring_add_event(req, ret); | |
3002 | if (ret < 0) | |
3003 | req_set_fail_links(req); | |
3004 | io_put_req_find_next(req, nxt); | |
3005 | return 0; | |
3006 | #else | |
3007 | return -EOPNOTSUPP; | |
3008 | #endif | |
3009 | } | |
3010 | ||
3529d8c2 JA |
3011 | static int io_recvmsg_prep(struct io_kiocb *req, |
3012 | const struct io_uring_sqe *sqe) | |
aa1fa28f JA |
3013 | { |
3014 | #if defined(CONFIG_NET) | |
e47293fd | 3015 | struct io_sr_msg *sr = &req->sr_msg; |
3529d8c2 JA |
3016 | struct io_async_ctx *io = req->io; |
3017 | ||
3018 | sr->msg_flags = READ_ONCE(sqe->msg_flags); | |
3019 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
06b76d44 | 3020 | |
fddaface | 3021 | if (!io || req->opcode == IORING_OP_RECV) |
06b76d44 | 3022 | return 0; |
03b1230c | 3023 | |
d9688565 | 3024 | io->msg.iov = io->msg.fast_iov; |
3529d8c2 | 3025 | return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
e47293fd | 3026 | &io->msg.uaddr, &io->msg.iov); |
aa1fa28f | 3027 | #else |
e47293fd | 3028 | return -EOPNOTSUPP; |
aa1fa28f JA |
3029 | #endif |
3030 | } | |
3031 | ||
fc4df999 JA |
3032 | static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
3033 | bool force_nonblock) | |
aa1fa28f JA |
3034 | { |
3035 | #if defined(CONFIG_NET) | |
0b416c3e | 3036 | struct io_async_msghdr *kmsg = NULL; |
03b1230c JA |
3037 | struct socket *sock; |
3038 | int ret; | |
3039 | ||
3040 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3041 | return -EINVAL; | |
3042 | ||
3043 | sock = sock_from_file(req->file, &ret); | |
3044 | if (sock) { | |
b7bb4f7d | 3045 | struct io_async_ctx io; |
03b1230c | 3046 | struct sockaddr_storage addr; |
03b1230c JA |
3047 | unsigned flags; |
3048 | ||
03b1230c | 3049 | if (req->io) { |
0b416c3e JA |
3050 | kmsg = &req->io->msg; |
3051 | kmsg->msg.msg_name = &addr; | |
3052 | /* if iov is set, it's allocated already */ | |
3053 | if (!kmsg->iov) | |
3054 | kmsg->iov = kmsg->fast_iov; | |
3055 | kmsg->msg.msg_iter.iov = kmsg->iov; | |
03b1230c | 3056 | } else { |
3529d8c2 JA |
3057 | struct io_sr_msg *sr = &req->sr_msg; |
3058 | ||
0b416c3e JA |
3059 | kmsg = &io.msg; |
3060 | kmsg->msg.msg_name = &addr; | |
3529d8c2 JA |
3061 | |
3062 | io.msg.iov = io.msg.fast_iov; | |
3063 | ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg, | |
3064 | sr->msg_flags, &io.msg.uaddr, | |
3065 | &io.msg.iov); | |
03b1230c | 3066 | if (ret) |
3529d8c2 | 3067 | return ret; |
03b1230c JA |
3068 | } |
3069 | ||
e47293fd JA |
3070 | flags = req->sr_msg.msg_flags; |
3071 | if (flags & MSG_DONTWAIT) | |
3072 | req->flags |= REQ_F_NOWAIT; | |
3073 | else if (force_nonblock) | |
3074 | flags |= MSG_DONTWAIT; | |
3075 | ||
3076 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg, | |
3077 | kmsg->uaddr, flags); | |
03b1230c | 3078 | if (force_nonblock && ret == -EAGAIN) { |
b7bb4f7d JA |
3079 | if (req->io) |
3080 | return -EAGAIN; | |
3081 | if (io_alloc_async_ctx(req)) | |
3082 | return -ENOMEM; | |
3083 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); | |
3084 | req->work.func = io_sendrecv_async; | |
0b416c3e | 3085 | return -EAGAIN; |
03b1230c JA |
3086 | } |
3087 | if (ret == -ERESTARTSYS) | |
3088 | ret = -EINTR; | |
3089 | } | |
3090 | ||
b7bb4f7d | 3091 | if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov) |
0b416c3e | 3092 | kfree(kmsg->iov); |
03b1230c | 3093 | io_cqring_add_event(req, ret); |
4e88d6e7 JA |
3094 | if (ret < 0) |
3095 | req_set_fail_links(req); | |
03b1230c JA |
3096 | io_put_req_find_next(req, nxt); |
3097 | return 0; | |
0fa03c62 JA |
3098 | #else |
3099 | return -EOPNOTSUPP; | |
3100 | #endif | |
3101 | } | |
5d17b4a4 | 3102 | |
fddaface JA |
3103 | static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt, |
3104 | bool force_nonblock) | |
3105 | { | |
3106 | #if defined(CONFIG_NET) | |
3107 | struct socket *sock; | |
3108 | int ret; | |
3109 | ||
3110 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3111 | return -EINVAL; | |
3112 | ||
3113 | sock = sock_from_file(req->file, &ret); | |
3114 | if (sock) { | |
3115 | struct io_sr_msg *sr = &req->sr_msg; | |
3116 | struct msghdr msg; | |
3117 | struct iovec iov; | |
3118 | unsigned flags; | |
3119 | ||
3120 | ret = import_single_range(READ, sr->buf, sr->len, &iov, | |
3121 | &msg.msg_iter); | |
3122 | if (ret) | |
3123 | return ret; | |
3124 | ||
3125 | msg.msg_name = NULL; | |
3126 | msg.msg_control = NULL; | |
3127 | msg.msg_controllen = 0; | |
3128 | msg.msg_namelen = 0; | |
3129 | msg.msg_iocb = NULL; | |
3130 | msg.msg_flags = 0; | |
3131 | ||
3132 | flags = req->sr_msg.msg_flags; | |
3133 | if (flags & MSG_DONTWAIT) | |
3134 | req->flags |= REQ_F_NOWAIT; | |
3135 | else if (force_nonblock) | |
3136 | flags |= MSG_DONTWAIT; | |
3137 | ||
3138 | ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags); | |
3139 | if (force_nonblock && ret == -EAGAIN) | |
3140 | return -EAGAIN; | |
3141 | if (ret == -ERESTARTSYS) | |
3142 | ret = -EINTR; | |
3143 | } | |
3144 | ||
3145 | io_cqring_add_event(req, ret); | |
3146 | if (ret < 0) | |
3147 | req_set_fail_links(req); | |
3148 | io_put_req_find_next(req, nxt); | |
3149 | return 0; | |
3150 | #else | |
3151 | return -EOPNOTSUPP; | |
3152 | #endif | |
3153 | } | |
3154 | ||
3155 | ||
3529d8c2 | 3156 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 JA |
3157 | { |
3158 | #if defined(CONFIG_NET) | |
8ed8d3c3 JA |
3159 | struct io_accept *accept = &req->accept; |
3160 | ||
17f2fe35 JA |
3161 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
3162 | return -EINVAL; | |
8042d6ce | 3163 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
3164 | return -EINVAL; |
3165 | ||
d55e5f5b JA |
3166 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
3167 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 3168 | accept->flags = READ_ONCE(sqe->accept_flags); |
8ed8d3c3 JA |
3169 | return 0; |
3170 | #else | |
3171 | return -EOPNOTSUPP; | |
3172 | #endif | |
3173 | } | |
17f2fe35 | 3174 | |
8ed8d3c3 JA |
3175 | #if defined(CONFIG_NET) |
3176 | static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt, | |
3177 | bool force_nonblock) | |
3178 | { | |
3179 | struct io_accept *accept = &req->accept; | |
3180 | unsigned file_flags; | |
3181 | int ret; | |
3182 | ||
3183 | file_flags = force_nonblock ? O_NONBLOCK : 0; | |
3184 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, | |
3185 | accept->addr_len, accept->flags); | |
3186 | if (ret == -EAGAIN && force_nonblock) | |
17f2fe35 | 3187 | return -EAGAIN; |
8e3cca12 JA |
3188 | if (ret == -ERESTARTSYS) |
3189 | ret = -EINTR; | |
4e88d6e7 JA |
3190 | if (ret < 0) |
3191 | req_set_fail_links(req); | |
78e19bbe | 3192 | io_cqring_add_event(req, ret); |
ec9c02ad | 3193 | io_put_req_find_next(req, nxt); |
17f2fe35 | 3194 | return 0; |
8ed8d3c3 JA |
3195 | } |
3196 | ||
3197 | static void io_accept_finish(struct io_wq_work **workptr) | |
3198 | { | |
3199 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); | |
3200 | struct io_kiocb *nxt = NULL; | |
3201 | ||
3202 | if (io_req_cancelled(req)) | |
3203 | return; | |
3204 | __io_accept(req, &nxt, false); | |
3205 | if (nxt) | |
78912934 | 3206 | io_wq_assign_next(workptr, nxt); |
8ed8d3c3 JA |
3207 | } |
3208 | #endif | |
3209 | ||
3210 | static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt, | |
3211 | bool force_nonblock) | |
3212 | { | |
3213 | #if defined(CONFIG_NET) | |
3214 | int ret; | |
3215 | ||
8ed8d3c3 JA |
3216 | ret = __io_accept(req, nxt, force_nonblock); |
3217 | if (ret == -EAGAIN && force_nonblock) { | |
3218 | req->work.func = io_accept_finish; | |
3219 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; | |
3220 | io_put_req(req); | |
3221 | return -EAGAIN; | |
3222 | } | |
3223 | return 0; | |
0fa03c62 JA |
3224 | #else |
3225 | return -EOPNOTSUPP; | |
3226 | #endif | |
3227 | } | |
5d17b4a4 | 3228 | |
3529d8c2 | 3229 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 JA |
3230 | { |
3231 | #if defined(CONFIG_NET) | |
3529d8c2 JA |
3232 | struct io_connect *conn = &req->connect; |
3233 | struct io_async_ctx *io = req->io; | |
f499a021 | 3234 | |
3fbb51c1 JA |
3235 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
3236 | return -EINVAL; | |
3237 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
3238 | return -EINVAL; | |
3239 | ||
3529d8c2 JA |
3240 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
3241 | conn->addr_len = READ_ONCE(sqe->addr2); | |
3242 | ||
3243 | if (!io) | |
3244 | return 0; | |
3245 | ||
3246 | return move_addr_to_kernel(conn->addr, conn->addr_len, | |
3fbb51c1 | 3247 | &io->connect.address); |
f499a021 | 3248 | #else |
3fbb51c1 | 3249 | return -EOPNOTSUPP; |
f499a021 JA |
3250 | #endif |
3251 | } | |
3252 | ||
fc4df999 JA |
3253 | static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt, |
3254 | bool force_nonblock) | |
f8e85cf2 JA |
3255 | { |
3256 | #if defined(CONFIG_NET) | |
f499a021 | 3257 | struct io_async_ctx __io, *io; |
f8e85cf2 | 3258 | unsigned file_flags; |
3fbb51c1 | 3259 | int ret; |
f8e85cf2 | 3260 | |
f499a021 JA |
3261 | if (req->io) { |
3262 | io = req->io; | |
3263 | } else { | |
3529d8c2 JA |
3264 | ret = move_addr_to_kernel(req->connect.addr, |
3265 | req->connect.addr_len, | |
3266 | &__io.connect.address); | |
f499a021 JA |
3267 | if (ret) |
3268 | goto out; | |
3269 | io = &__io; | |
3270 | } | |
3271 | ||
3fbb51c1 JA |
3272 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
3273 | ||
3274 | ret = __sys_connect_file(req->file, &io->connect.address, | |
3275 | req->connect.addr_len, file_flags); | |
87f80d62 | 3276 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
b7bb4f7d JA |
3277 | if (req->io) |
3278 | return -EAGAIN; | |
3279 | if (io_alloc_async_ctx(req)) { | |
f499a021 JA |
3280 | ret = -ENOMEM; |
3281 | goto out; | |
3282 | } | |
b7bb4f7d | 3283 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
f8e85cf2 | 3284 | return -EAGAIN; |
f499a021 | 3285 | } |
f8e85cf2 JA |
3286 | if (ret == -ERESTARTSYS) |
3287 | ret = -EINTR; | |
f499a021 | 3288 | out: |
4e88d6e7 JA |
3289 | if (ret < 0) |
3290 | req_set_fail_links(req); | |
f8e85cf2 JA |
3291 | io_cqring_add_event(req, ret); |
3292 | io_put_req_find_next(req, nxt); | |
3293 | return 0; | |
3294 | #else | |
3295 | return -EOPNOTSUPP; | |
3296 | #endif | |
3297 | } | |
3298 | ||
221c5eb2 JA |
3299 | static void io_poll_remove_one(struct io_kiocb *req) |
3300 | { | |
3301 | struct io_poll_iocb *poll = &req->poll; | |
3302 | ||
3303 | spin_lock(&poll->head->lock); | |
3304 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
3305 | if (!list_empty(&poll->wait.entry)) { |
3306 | list_del_init(&poll->wait.entry); | |
a197f664 | 3307 | io_queue_async_work(req); |
221c5eb2 JA |
3308 | } |
3309 | spin_unlock(&poll->head->lock); | |
78076bb6 | 3310 | hash_del(&req->hash_node); |
221c5eb2 JA |
3311 | } |
3312 | ||
3313 | static void io_poll_remove_all(struct io_ring_ctx *ctx) | |
3314 | { | |
78076bb6 | 3315 | struct hlist_node *tmp; |
221c5eb2 | 3316 | struct io_kiocb *req; |
78076bb6 | 3317 | int i; |
221c5eb2 JA |
3318 | |
3319 | spin_lock_irq(&ctx->completion_lock); | |
78076bb6 JA |
3320 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
3321 | struct hlist_head *list; | |
3322 | ||
3323 | list = &ctx->cancel_hash[i]; | |
3324 | hlist_for_each_entry_safe(req, tmp, list, hash_node) | |
3325 | io_poll_remove_one(req); | |
221c5eb2 JA |
3326 | } |
3327 | spin_unlock_irq(&ctx->completion_lock); | |
3328 | } | |
3329 | ||
47f46768 JA |
3330 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
3331 | { | |
78076bb6 | 3332 | struct hlist_head *list; |
47f46768 JA |
3333 | struct io_kiocb *req; |
3334 | ||
78076bb6 JA |
3335 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
3336 | hlist_for_each_entry(req, list, hash_node) { | |
3337 | if (sqe_addr == req->user_data) { | |
eac406c6 JA |
3338 | io_poll_remove_one(req); |
3339 | return 0; | |
3340 | } | |
47f46768 JA |
3341 | } |
3342 | ||
3343 | return -ENOENT; | |
3344 | } | |
3345 | ||
3529d8c2 JA |
3346 | static int io_poll_remove_prep(struct io_kiocb *req, |
3347 | const struct io_uring_sqe *sqe) | |
0969e783 | 3348 | { |
0969e783 JA |
3349 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3350 | return -EINVAL; | |
3351 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
3352 | sqe->poll_events) | |
3353 | return -EINVAL; | |
3354 | ||
3355 | req->poll.addr = READ_ONCE(sqe->addr); | |
0969e783 JA |
3356 | return 0; |
3357 | } | |
3358 | ||
221c5eb2 JA |
3359 | /* |
3360 | * Find a running poll command that matches one specified in sqe->addr, | |
3361 | * and remove it if found. | |
3362 | */ | |
fc4df999 | 3363 | static int io_poll_remove(struct io_kiocb *req) |
221c5eb2 JA |
3364 | { |
3365 | struct io_ring_ctx *ctx = req->ctx; | |
0969e783 | 3366 | u64 addr; |
47f46768 | 3367 | int ret; |
221c5eb2 | 3368 | |
0969e783 | 3369 | addr = req->poll.addr; |
221c5eb2 | 3370 | spin_lock_irq(&ctx->completion_lock); |
0969e783 | 3371 | ret = io_poll_cancel(ctx, addr); |
221c5eb2 JA |
3372 | spin_unlock_irq(&ctx->completion_lock); |
3373 | ||
78e19bbe | 3374 | io_cqring_add_event(req, ret); |
4e88d6e7 JA |
3375 | if (ret < 0) |
3376 | req_set_fail_links(req); | |
e65ef56d | 3377 | io_put_req(req); |
221c5eb2 JA |
3378 | return 0; |
3379 | } | |
3380 | ||
b0dd8a41 | 3381 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) |
221c5eb2 | 3382 | { |
a197f664 JL |
3383 | struct io_ring_ctx *ctx = req->ctx; |
3384 | ||
8c838788 | 3385 | req->poll.done = true; |
b0dd8a41 JA |
3386 | if (error) |
3387 | io_cqring_fill_event(req, error); | |
3388 | else | |
3389 | io_cqring_fill_event(req, mangle_poll(mask)); | |
8c838788 | 3390 | io_commit_cqring(ctx); |
221c5eb2 JA |
3391 | } |
3392 | ||
561fb04a | 3393 | static void io_poll_complete_work(struct io_wq_work **workptr) |
221c5eb2 | 3394 | { |
561fb04a | 3395 | struct io_wq_work *work = *workptr; |
221c5eb2 JA |
3396 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
3397 | struct io_poll_iocb *poll = &req->poll; | |
3398 | struct poll_table_struct pt = { ._key = poll->events }; | |
3399 | struct io_ring_ctx *ctx = req->ctx; | |
89723d0b | 3400 | struct io_kiocb *nxt = NULL; |
221c5eb2 | 3401 | __poll_t mask = 0; |
b0dd8a41 | 3402 | int ret = 0; |
221c5eb2 | 3403 | |
b0dd8a41 | 3404 | if (work->flags & IO_WQ_WORK_CANCEL) { |
561fb04a | 3405 | WRITE_ONCE(poll->canceled, true); |
b0dd8a41 JA |
3406 | ret = -ECANCELED; |
3407 | } else if (READ_ONCE(poll->canceled)) { | |
3408 | ret = -ECANCELED; | |
3409 | } | |
561fb04a | 3410 | |
b0dd8a41 | 3411 | if (ret != -ECANCELED) |
221c5eb2 JA |
3412 | mask = vfs_poll(poll->file, &pt) & poll->events; |
3413 | ||
3414 | /* | |
3415 | * Note that ->ki_cancel callers also delete iocb from active_reqs after | |
3416 | * calling ->ki_cancel. We need the ctx_lock roundtrip here to | |
3417 | * synchronize with them. In the cancellation case the list_del_init | |
3418 | * itself is not actually needed, but harmless so we keep it in to | |
3419 | * avoid further branches in the fast path. | |
3420 | */ | |
3421 | spin_lock_irq(&ctx->completion_lock); | |
b0dd8a41 | 3422 | if (!mask && ret != -ECANCELED) { |
392edb45 | 3423 | add_wait_queue(poll->head, &poll->wait); |
221c5eb2 JA |
3424 | spin_unlock_irq(&ctx->completion_lock); |
3425 | return; | |
3426 | } | |
78076bb6 | 3427 | hash_del(&req->hash_node); |
b0dd8a41 | 3428 | io_poll_complete(req, mask, ret); |
221c5eb2 JA |
3429 | spin_unlock_irq(&ctx->completion_lock); |
3430 | ||
8c838788 | 3431 | io_cqring_ev_posted(ctx); |
89723d0b | 3432 | |
4e88d6e7 JA |
3433 | if (ret < 0) |
3434 | req_set_fail_links(req); | |
ec9c02ad | 3435 | io_put_req_find_next(req, &nxt); |
89723d0b | 3436 | if (nxt) |
78912934 | 3437 | io_wq_assign_next(workptr, nxt); |
221c5eb2 JA |
3438 | } |
3439 | ||
e94f141b JA |
3440 | static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes) |
3441 | { | |
e94f141b | 3442 | struct io_kiocb *req, *tmp; |
8237e045 | 3443 | struct req_batch rb; |
e94f141b | 3444 | |
c6ca97b3 | 3445 | rb.to_free = rb.need_iter = 0; |
e94f141b JA |
3446 | spin_lock_irq(&ctx->completion_lock); |
3447 | llist_for_each_entry_safe(req, tmp, nodes, llist_node) { | |
3448 | hash_del(&req->hash_node); | |
3449 | io_poll_complete(req, req->result, 0); | |
3450 | ||
8237e045 JA |
3451 | if (refcount_dec_and_test(&req->refs) && |
3452 | !io_req_multi_free(&rb, req)) { | |
3453 | req->flags |= REQ_F_COMP_LOCKED; | |
3454 | io_free_req(req); | |
e94f141b JA |
3455 | } |
3456 | } | |
3457 | spin_unlock_irq(&ctx->completion_lock); | |
3458 | ||
3459 | io_cqring_ev_posted(ctx); | |
8237e045 | 3460 | io_free_req_many(ctx, &rb); |
e94f141b JA |
3461 | } |
3462 | ||
3463 | static void io_poll_flush(struct io_wq_work **workptr) | |
3464 | { | |
3465 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); | |
3466 | struct llist_node *nodes; | |
3467 | ||
3468 | nodes = llist_del_all(&req->ctx->poll_llist); | |
3469 | if (nodes) | |
3470 | __io_poll_flush(req->ctx, nodes); | |
3471 | } | |
3472 | ||
221c5eb2 JA |
3473 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
3474 | void *key) | |
3475 | { | |
e944475e | 3476 | struct io_poll_iocb *poll = wait->private; |
221c5eb2 JA |
3477 | struct io_kiocb *req = container_of(poll, struct io_kiocb, poll); |
3478 | struct io_ring_ctx *ctx = req->ctx; | |
3479 | __poll_t mask = key_to_poll(key); | |
221c5eb2 JA |
3480 | |
3481 | /* for instances that support it check for an event match first: */ | |
8c838788 JA |
3482 | if (mask && !(mask & poll->events)) |
3483 | return 0; | |
221c5eb2 | 3484 | |
392edb45 | 3485 | list_del_init(&poll->wait.entry); |
221c5eb2 | 3486 | |
7c9e7f0f JA |
3487 | /* |
3488 | * Run completion inline if we can. We're using trylock here because | |
3489 | * we are violating the completion_lock -> poll wq lock ordering. | |
3490 | * If we have a link timeout we're going to need the completion_lock | |
3491 | * for finalizing the request, mark us as having grabbed that already. | |
3492 | */ | |
e94f141b JA |
3493 | if (mask) { |
3494 | unsigned long flags; | |
221c5eb2 | 3495 | |
e94f141b JA |
3496 | if (llist_empty(&ctx->poll_llist) && |
3497 | spin_trylock_irqsave(&ctx->completion_lock, flags)) { | |
3498 | hash_del(&req->hash_node); | |
3499 | io_poll_complete(req, mask, 0); | |
3500 | req->flags |= REQ_F_COMP_LOCKED; | |
3501 | io_put_req(req); | |
3502 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
3503 | ||
3504 | io_cqring_ev_posted(ctx); | |
3505 | req = NULL; | |
3506 | } else { | |
3507 | req->result = mask; | |
3508 | req->llist_node.next = NULL; | |
3509 | /* if the list wasn't empty, we're done */ | |
3510 | if (!llist_add(&req->llist_node, &ctx->poll_llist)) | |
3511 | req = NULL; | |
3512 | else | |
3513 | req->work.func = io_poll_flush; | |
3514 | } | |
221c5eb2 | 3515 | } |
e94f141b JA |
3516 | if (req) |
3517 | io_queue_async_work(req); | |
221c5eb2 | 3518 | |
221c5eb2 JA |
3519 | return 1; |
3520 | } | |
3521 | ||
3522 | struct io_poll_table { | |
3523 | struct poll_table_struct pt; | |
3524 | struct io_kiocb *req; | |
3525 | int error; | |
3526 | }; | |
3527 | ||
3528 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, | |
3529 | struct poll_table_struct *p) | |
3530 | { | |
3531 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
3532 | ||
3533 | if (unlikely(pt->req->poll.head)) { | |
3534 | pt->error = -EINVAL; | |
3535 | return; | |
3536 | } | |
3537 | ||
3538 | pt->error = 0; | |
3539 | pt->req->poll.head = head; | |
392edb45 | 3540 | add_wait_queue(head, &pt->req->poll.wait); |
221c5eb2 JA |
3541 | } |
3542 | ||
eac406c6 JA |
3543 | static void io_poll_req_insert(struct io_kiocb *req) |
3544 | { | |
3545 | struct io_ring_ctx *ctx = req->ctx; | |
78076bb6 JA |
3546 | struct hlist_head *list; |
3547 | ||
3548 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
3549 | hlist_add_head(&req->hash_node, list); | |
eac406c6 JA |
3550 | } |
3551 | ||
3529d8c2 | 3552 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
3553 | { |
3554 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 3555 | u16 events; |
221c5eb2 JA |
3556 | |
3557 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3558 | return -EINVAL; | |
3559 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
3560 | return -EINVAL; | |
09bb8394 JA |
3561 | if (!poll->file) |
3562 | return -EBADF; | |
221c5eb2 | 3563 | |
221c5eb2 JA |
3564 | events = READ_ONCE(sqe->poll_events); |
3565 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; | |
0969e783 JA |
3566 | return 0; |
3567 | } | |
3568 | ||
3569 | static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt) | |
3570 | { | |
3571 | struct io_poll_iocb *poll = &req->poll; | |
3572 | struct io_ring_ctx *ctx = req->ctx; | |
3573 | struct io_poll_table ipt; | |
3574 | bool cancel = false; | |
3575 | __poll_t mask; | |
0969e783 JA |
3576 | |
3577 | INIT_IO_WORK(&req->work, io_poll_complete_work); | |
78076bb6 | 3578 | INIT_HLIST_NODE(&req->hash_node); |
221c5eb2 | 3579 | |
221c5eb2 | 3580 | poll->head = NULL; |
8c838788 | 3581 | poll->done = false; |
221c5eb2 JA |
3582 | poll->canceled = false; |
3583 | ||
3584 | ipt.pt._qproc = io_poll_queue_proc; | |
3585 | ipt.pt._key = poll->events; | |
3586 | ipt.req = req; | |
3587 | ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */ | |
3588 | ||
3589 | /* initialized the list so that we can do list_empty checks */ | |
392edb45 JA |
3590 | INIT_LIST_HEAD(&poll->wait.entry); |
3591 | init_waitqueue_func_entry(&poll->wait, io_poll_wake); | |
3592 | poll->wait.private = poll; | |
221c5eb2 | 3593 | |
36703247 JA |
3594 | INIT_LIST_HEAD(&req->list); |
3595 | ||
221c5eb2 | 3596 | mask = vfs_poll(poll->file, &ipt.pt) & poll->events; |
221c5eb2 JA |
3597 | |
3598 | spin_lock_irq(&ctx->completion_lock); | |
8c838788 JA |
3599 | if (likely(poll->head)) { |
3600 | spin_lock(&poll->head->lock); | |
392edb45 | 3601 | if (unlikely(list_empty(&poll->wait.entry))) { |
8c838788 JA |
3602 | if (ipt.error) |
3603 | cancel = true; | |
3604 | ipt.error = 0; | |
3605 | mask = 0; | |
3606 | } | |
3607 | if (mask || ipt.error) | |
392edb45 | 3608 | list_del_init(&poll->wait.entry); |
8c838788 JA |
3609 | else if (cancel) |
3610 | WRITE_ONCE(poll->canceled, true); | |
3611 | else if (!poll->done) /* actually waiting for an event */ | |
eac406c6 | 3612 | io_poll_req_insert(req); |
8c838788 JA |
3613 | spin_unlock(&poll->head->lock); |
3614 | } | |
3615 | if (mask) { /* no async, we'd stolen it */ | |
221c5eb2 | 3616 | ipt.error = 0; |
b0dd8a41 | 3617 | io_poll_complete(req, mask, 0); |
221c5eb2 | 3618 | } |
221c5eb2 JA |
3619 | spin_unlock_irq(&ctx->completion_lock); |
3620 | ||
8c838788 JA |
3621 | if (mask) { |
3622 | io_cqring_ev_posted(ctx); | |
ec9c02ad | 3623 | io_put_req_find_next(req, nxt); |
221c5eb2 | 3624 | } |
8c838788 | 3625 | return ipt.error; |
221c5eb2 JA |
3626 | } |
3627 | ||
5262f567 JA |
3628 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
3629 | { | |
ad8a48ac JA |
3630 | struct io_timeout_data *data = container_of(timer, |
3631 | struct io_timeout_data, timer); | |
3632 | struct io_kiocb *req = data->req; | |
3633 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
3634 | unsigned long flags; |
3635 | ||
5262f567 JA |
3636 | atomic_inc(&ctx->cq_timeouts); |
3637 | ||
3638 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
ef03681a | 3639 | /* |
11365043 JA |
3640 | * We could be racing with timeout deletion. If the list is empty, |
3641 | * then timeout lookup already found it and will be handling it. | |
ef03681a | 3642 | */ |
842f9612 | 3643 | if (!list_empty(&req->list)) { |
11365043 | 3644 | struct io_kiocb *prev; |
5262f567 | 3645 | |
11365043 JA |
3646 | /* |
3647 | * Adjust the reqs sequence before the current one because it | |
d195a66e | 3648 | * will consume a slot in the cq_ring and the cq_tail |
11365043 JA |
3649 | * pointer will be increased, otherwise other timeout reqs may |
3650 | * return in advance without waiting for enough wait_nr. | |
3651 | */ | |
3652 | prev = req; | |
3653 | list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list) | |
3654 | prev->sequence++; | |
11365043 | 3655 | list_del_init(&req->list); |
11365043 | 3656 | } |
5262f567 | 3657 | |
78e19bbe | 3658 | io_cqring_fill_event(req, -ETIME); |
5262f567 JA |
3659 | io_commit_cqring(ctx); |
3660 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
3661 | ||
3662 | io_cqring_ev_posted(ctx); | |
4e88d6e7 | 3663 | req_set_fail_links(req); |
5262f567 JA |
3664 | io_put_req(req); |
3665 | return HRTIMER_NORESTART; | |
3666 | } | |
3667 | ||
47f46768 JA |
3668 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
3669 | { | |
3670 | struct io_kiocb *req; | |
3671 | int ret = -ENOENT; | |
3672 | ||
3673 | list_for_each_entry(req, &ctx->timeout_list, list) { | |
3674 | if (user_data == req->user_data) { | |
3675 | list_del_init(&req->list); | |
3676 | ret = 0; | |
3677 | break; | |
3678 | } | |
3679 | } | |
3680 | ||
3681 | if (ret == -ENOENT) | |
3682 | return ret; | |
3683 | ||
2d28390a | 3684 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
47f46768 JA |
3685 | if (ret == -1) |
3686 | return -EALREADY; | |
3687 | ||
4e88d6e7 | 3688 | req_set_fail_links(req); |
47f46768 JA |
3689 | io_cqring_fill_event(req, -ECANCELED); |
3690 | io_put_req(req); | |
3691 | return 0; | |
3692 | } | |
3693 | ||
3529d8c2 JA |
3694 | static int io_timeout_remove_prep(struct io_kiocb *req, |
3695 | const struct io_uring_sqe *sqe) | |
b29472ee | 3696 | { |
b29472ee JA |
3697 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3698 | return -EINVAL; | |
3699 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) | |
3700 | return -EINVAL; | |
3701 | ||
3702 | req->timeout.addr = READ_ONCE(sqe->addr); | |
3703 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); | |
3704 | if (req->timeout.flags) | |
3705 | return -EINVAL; | |
3706 | ||
b29472ee JA |
3707 | return 0; |
3708 | } | |
3709 | ||
11365043 JA |
3710 | /* |
3711 | * Remove or update an existing timeout command | |
3712 | */ | |
fc4df999 | 3713 | static int io_timeout_remove(struct io_kiocb *req) |
11365043 JA |
3714 | { |
3715 | struct io_ring_ctx *ctx = req->ctx; | |
47f46768 | 3716 | int ret; |
11365043 | 3717 | |
11365043 | 3718 | spin_lock_irq(&ctx->completion_lock); |
b29472ee | 3719 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
11365043 | 3720 | |
47f46768 | 3721 | io_cqring_fill_event(req, ret); |
11365043 JA |
3722 | io_commit_cqring(ctx); |
3723 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 3724 | io_cqring_ev_posted(ctx); |
4e88d6e7 JA |
3725 | if (ret < 0) |
3726 | req_set_fail_links(req); | |
ec9c02ad | 3727 | io_put_req(req); |
11365043 | 3728 | return 0; |
5262f567 JA |
3729 | } |
3730 | ||
3529d8c2 | 3731 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 3732 | bool is_timeout_link) |
5262f567 | 3733 | { |
ad8a48ac | 3734 | struct io_timeout_data *data; |
a41525ab | 3735 | unsigned flags; |
5262f567 | 3736 | |
ad8a48ac | 3737 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 3738 | return -EINVAL; |
ad8a48ac | 3739 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 3740 | return -EINVAL; |
2d28390a JA |
3741 | if (sqe->off && is_timeout_link) |
3742 | return -EINVAL; | |
a41525ab JA |
3743 | flags = READ_ONCE(sqe->timeout_flags); |
3744 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 3745 | return -EINVAL; |
bdf20073 | 3746 | |
26a61679 JA |
3747 | req->timeout.count = READ_ONCE(sqe->off); |
3748 | ||
3529d8c2 | 3749 | if (!req->io && io_alloc_async_ctx(req)) |
26a61679 JA |
3750 | return -ENOMEM; |
3751 | ||
3752 | data = &req->io->timeout; | |
ad8a48ac | 3753 | data->req = req; |
ad8a48ac JA |
3754 | req->flags |= REQ_F_TIMEOUT; |
3755 | ||
3756 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
3757 | return -EFAULT; |
3758 | ||
11365043 | 3759 | if (flags & IORING_TIMEOUT_ABS) |
ad8a48ac | 3760 | data->mode = HRTIMER_MODE_ABS; |
11365043 | 3761 | else |
ad8a48ac | 3762 | data->mode = HRTIMER_MODE_REL; |
11365043 | 3763 | |
ad8a48ac JA |
3764 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
3765 | return 0; | |
3766 | } | |
3767 | ||
fc4df999 | 3768 | static int io_timeout(struct io_kiocb *req) |
ad8a48ac JA |
3769 | { |
3770 | unsigned count; | |
3771 | struct io_ring_ctx *ctx = req->ctx; | |
3772 | struct io_timeout_data *data; | |
3773 | struct list_head *entry; | |
3774 | unsigned span = 0; | |
ad8a48ac | 3775 | |
2d28390a | 3776 | data = &req->io->timeout; |
93bd25bb | 3777 | |
5262f567 JA |
3778 | /* |
3779 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
3780 | * timeout event to be satisfied. If it isn't set, then this is |
3781 | * a pure timeout request, sequence isn't used. | |
5262f567 | 3782 | */ |
26a61679 | 3783 | count = req->timeout.count; |
93bd25bb JA |
3784 | if (!count) { |
3785 | req->flags |= REQ_F_TIMEOUT_NOSEQ; | |
3786 | spin_lock_irq(&ctx->completion_lock); | |
3787 | entry = ctx->timeout_list.prev; | |
3788 | goto add; | |
3789 | } | |
5262f567 JA |
3790 | |
3791 | req->sequence = ctx->cached_sq_head + count - 1; | |
2d28390a | 3792 | data->seq_offset = count; |
5262f567 JA |
3793 | |
3794 | /* | |
3795 | * Insertion sort, ensuring the first entry in the list is always | |
3796 | * the one we need first. | |
3797 | */ | |
5262f567 JA |
3798 | spin_lock_irq(&ctx->completion_lock); |
3799 | list_for_each_prev(entry, &ctx->timeout_list) { | |
3800 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); | |
5da0fb1a | 3801 | unsigned nxt_sq_head; |
3802 | long long tmp, tmp_nxt; | |
2d28390a | 3803 | u32 nxt_offset = nxt->io->timeout.seq_offset; |
5262f567 | 3804 | |
93bd25bb JA |
3805 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
3806 | continue; | |
3807 | ||
5da0fb1a | 3808 | /* |
3809 | * Since cached_sq_head + count - 1 can overflow, use type long | |
3810 | * long to store it. | |
3811 | */ | |
3812 | tmp = (long long)ctx->cached_sq_head + count - 1; | |
cc42e0ac PB |
3813 | nxt_sq_head = nxt->sequence - nxt_offset + 1; |
3814 | tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1; | |
5da0fb1a | 3815 | |
3816 | /* | |
3817 | * cached_sq_head may overflow, and it will never overflow twice | |
3818 | * once there is some timeout req still be valid. | |
3819 | */ | |
3820 | if (ctx->cached_sq_head < nxt_sq_head) | |
8b07a65a | 3821 | tmp += UINT_MAX; |
5da0fb1a | 3822 | |
a1f58ba4 | 3823 | if (tmp > tmp_nxt) |
5262f567 | 3824 | break; |
a1f58ba4 | 3825 | |
3826 | /* | |
3827 | * Sequence of reqs after the insert one and itself should | |
3828 | * be adjusted because each timeout req consumes a slot. | |
3829 | */ | |
3830 | span++; | |
3831 | nxt->sequence++; | |
5262f567 | 3832 | } |
a1f58ba4 | 3833 | req->sequence -= span; |
93bd25bb | 3834 | add: |
5262f567 | 3835 | list_add(&req->list, entry); |
ad8a48ac JA |
3836 | data->timer.function = io_timeout_fn; |
3837 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 3838 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
3839 | return 0; |
3840 | } | |
5262f567 | 3841 | |
62755e35 JA |
3842 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
3843 | { | |
3844 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
3845 | ||
3846 | return req->user_data == (unsigned long) data; | |
3847 | } | |
3848 | ||
e977d6d3 | 3849 | static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr) |
62755e35 | 3850 | { |
62755e35 | 3851 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
3852 | int ret = 0; |
3853 | ||
62755e35 JA |
3854 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr); |
3855 | switch (cancel_ret) { | |
3856 | case IO_WQ_CANCEL_OK: | |
3857 | ret = 0; | |
3858 | break; | |
3859 | case IO_WQ_CANCEL_RUNNING: | |
3860 | ret = -EALREADY; | |
3861 | break; | |
3862 | case IO_WQ_CANCEL_NOTFOUND: | |
3863 | ret = -ENOENT; | |
3864 | break; | |
3865 | } | |
3866 | ||
e977d6d3 JA |
3867 | return ret; |
3868 | } | |
3869 | ||
47f46768 JA |
3870 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
3871 | struct io_kiocb *req, __u64 sqe_addr, | |
b0dd8a41 | 3872 | struct io_kiocb **nxt, int success_ret) |
47f46768 JA |
3873 | { |
3874 | unsigned long flags; | |
3875 | int ret; | |
3876 | ||
3877 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); | |
3878 | if (ret != -ENOENT) { | |
3879 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
3880 | goto done; | |
3881 | } | |
3882 | ||
3883 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
3884 | ret = io_timeout_cancel(ctx, sqe_addr); | |
3885 | if (ret != -ENOENT) | |
3886 | goto done; | |
3887 | ret = io_poll_cancel(ctx, sqe_addr); | |
3888 | done: | |
b0dd8a41 JA |
3889 | if (!ret) |
3890 | ret = success_ret; | |
47f46768 JA |
3891 | io_cqring_fill_event(req, ret); |
3892 | io_commit_cqring(ctx); | |
3893 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
3894 | io_cqring_ev_posted(ctx); | |
3895 | ||
4e88d6e7 JA |
3896 | if (ret < 0) |
3897 | req_set_fail_links(req); | |
47f46768 JA |
3898 | io_put_req_find_next(req, nxt); |
3899 | } | |
3900 | ||
3529d8c2 JA |
3901 | static int io_async_cancel_prep(struct io_kiocb *req, |
3902 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 3903 | { |
fbf23849 | 3904 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 JA |
3905 | return -EINVAL; |
3906 | if (sqe->flags || sqe->ioprio || sqe->off || sqe->len || | |
3907 | sqe->cancel_flags) | |
3908 | return -EINVAL; | |
3909 | ||
fbf23849 JA |
3910 | req->cancel.addr = READ_ONCE(sqe->addr); |
3911 | return 0; | |
3912 | } | |
3913 | ||
3914 | static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt) | |
3915 | { | |
3916 | struct io_ring_ctx *ctx = req->ctx; | |
fbf23849 JA |
3917 | |
3918 | io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0); | |
5262f567 JA |
3919 | return 0; |
3920 | } | |
3921 | ||
05f3fb3c JA |
3922 | static int io_files_update_prep(struct io_kiocb *req, |
3923 | const struct io_uring_sqe *sqe) | |
3924 | { | |
3925 | if (sqe->flags || sqe->ioprio || sqe->rw_flags) | |
3926 | return -EINVAL; | |
3927 | ||
3928 | req->files_update.offset = READ_ONCE(sqe->off); | |
3929 | req->files_update.nr_args = READ_ONCE(sqe->len); | |
3930 | if (!req->files_update.nr_args) | |
3931 | return -EINVAL; | |
3932 | req->files_update.arg = READ_ONCE(sqe->addr); | |
3933 | return 0; | |
3934 | } | |
3935 | ||
3936 | static int io_files_update(struct io_kiocb *req, bool force_nonblock) | |
3937 | { | |
3938 | struct io_ring_ctx *ctx = req->ctx; | |
3939 | struct io_uring_files_update up; | |
3940 | int ret; | |
3941 | ||
3942 | if (force_nonblock) { | |
3943 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; | |
3944 | return -EAGAIN; | |
3945 | } | |
3946 | ||
3947 | up.offset = req->files_update.offset; | |
3948 | up.fds = req->files_update.arg; | |
3949 | ||
3950 | mutex_lock(&ctx->uring_lock); | |
3951 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); | |
3952 | mutex_unlock(&ctx->uring_lock); | |
3953 | ||
3954 | if (ret < 0) | |
3955 | req_set_fail_links(req); | |
3956 | io_cqring_add_event(req, ret); | |
3957 | io_put_req(req); | |
3958 | return 0; | |
3959 | } | |
3960 | ||
3529d8c2 JA |
3961 | static int io_req_defer_prep(struct io_kiocb *req, |
3962 | const struct io_uring_sqe *sqe) | |
f67676d1 | 3963 | { |
e781573e | 3964 | ssize_t ret = 0; |
f67676d1 | 3965 | |
d625c6ee | 3966 | switch (req->opcode) { |
e781573e JA |
3967 | case IORING_OP_NOP: |
3968 | break; | |
f67676d1 JA |
3969 | case IORING_OP_READV: |
3970 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 3971 | case IORING_OP_READ: |
3529d8c2 | 3972 | ret = io_read_prep(req, sqe, true); |
f67676d1 JA |
3973 | break; |
3974 | case IORING_OP_WRITEV: | |
3975 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 3976 | case IORING_OP_WRITE: |
3529d8c2 | 3977 | ret = io_write_prep(req, sqe, true); |
f67676d1 | 3978 | break; |
0969e783 | 3979 | case IORING_OP_POLL_ADD: |
3529d8c2 | 3980 | ret = io_poll_add_prep(req, sqe); |
0969e783 JA |
3981 | break; |
3982 | case IORING_OP_POLL_REMOVE: | |
3529d8c2 | 3983 | ret = io_poll_remove_prep(req, sqe); |
0969e783 | 3984 | break; |
8ed8d3c3 | 3985 | case IORING_OP_FSYNC: |
3529d8c2 | 3986 | ret = io_prep_fsync(req, sqe); |
8ed8d3c3 JA |
3987 | break; |
3988 | case IORING_OP_SYNC_FILE_RANGE: | |
3529d8c2 | 3989 | ret = io_prep_sfr(req, sqe); |
8ed8d3c3 | 3990 | break; |
03b1230c | 3991 | case IORING_OP_SENDMSG: |
fddaface | 3992 | case IORING_OP_SEND: |
3529d8c2 | 3993 | ret = io_sendmsg_prep(req, sqe); |
03b1230c JA |
3994 | break; |
3995 | case IORING_OP_RECVMSG: | |
fddaface | 3996 | case IORING_OP_RECV: |
3529d8c2 | 3997 | ret = io_recvmsg_prep(req, sqe); |
03b1230c | 3998 | break; |
f499a021 | 3999 | case IORING_OP_CONNECT: |
3529d8c2 | 4000 | ret = io_connect_prep(req, sqe); |
f499a021 | 4001 | break; |
2d28390a | 4002 | case IORING_OP_TIMEOUT: |
3529d8c2 | 4003 | ret = io_timeout_prep(req, sqe, false); |
b7bb4f7d | 4004 | break; |
b29472ee | 4005 | case IORING_OP_TIMEOUT_REMOVE: |
3529d8c2 | 4006 | ret = io_timeout_remove_prep(req, sqe); |
b29472ee | 4007 | break; |
fbf23849 | 4008 | case IORING_OP_ASYNC_CANCEL: |
3529d8c2 | 4009 | ret = io_async_cancel_prep(req, sqe); |
fbf23849 | 4010 | break; |
2d28390a | 4011 | case IORING_OP_LINK_TIMEOUT: |
3529d8c2 | 4012 | ret = io_timeout_prep(req, sqe, true); |
b7bb4f7d | 4013 | break; |
8ed8d3c3 | 4014 | case IORING_OP_ACCEPT: |
3529d8c2 | 4015 | ret = io_accept_prep(req, sqe); |
8ed8d3c3 | 4016 | break; |
d63d1b5e JA |
4017 | case IORING_OP_FALLOCATE: |
4018 | ret = io_fallocate_prep(req, sqe); | |
4019 | break; | |
15b71abe JA |
4020 | case IORING_OP_OPENAT: |
4021 | ret = io_openat_prep(req, sqe); | |
4022 | break; | |
b5dba59e JA |
4023 | case IORING_OP_CLOSE: |
4024 | ret = io_close_prep(req, sqe); | |
4025 | break; | |
05f3fb3c JA |
4026 | case IORING_OP_FILES_UPDATE: |
4027 | ret = io_files_update_prep(req, sqe); | |
4028 | break; | |
eddc7ef5 JA |
4029 | case IORING_OP_STATX: |
4030 | ret = io_statx_prep(req, sqe); | |
4031 | break; | |
4840e418 JA |
4032 | case IORING_OP_FADVISE: |
4033 | ret = io_fadvise_prep(req, sqe); | |
4034 | break; | |
c1ca757b JA |
4035 | case IORING_OP_MADVISE: |
4036 | ret = io_madvise_prep(req, sqe); | |
4037 | break; | |
cebdb986 JA |
4038 | case IORING_OP_OPENAT2: |
4039 | ret = io_openat2_prep(req, sqe); | |
4040 | break; | |
f67676d1 | 4041 | default: |
e781573e JA |
4042 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
4043 | req->opcode); | |
4044 | ret = -EINVAL; | |
b7bb4f7d | 4045 | break; |
f67676d1 JA |
4046 | } |
4047 | ||
b7bb4f7d | 4048 | return ret; |
f67676d1 JA |
4049 | } |
4050 | ||
3529d8c2 | 4051 | static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
de0617e4 | 4052 | { |
a197f664 | 4053 | struct io_ring_ctx *ctx = req->ctx; |
f67676d1 | 4054 | int ret; |
de0617e4 | 4055 | |
9d858b21 BL |
4056 | /* Still need defer if there is pending req in defer list. */ |
4057 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) | |
de0617e4 JA |
4058 | return 0; |
4059 | ||
3529d8c2 | 4060 | if (!req->io && io_alloc_async_ctx(req)) |
de0617e4 JA |
4061 | return -EAGAIN; |
4062 | ||
3529d8c2 | 4063 | ret = io_req_defer_prep(req, sqe); |
b7bb4f7d | 4064 | if (ret < 0) |
2d28390a | 4065 | return ret; |
2d28390a | 4066 | |
de0617e4 | 4067 | spin_lock_irq(&ctx->completion_lock); |
9d858b21 | 4068 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) { |
de0617e4 | 4069 | spin_unlock_irq(&ctx->completion_lock); |
de0617e4 JA |
4070 | return 0; |
4071 | } | |
4072 | ||
915967f6 | 4073 | trace_io_uring_defer(ctx, req, req->user_data); |
de0617e4 JA |
4074 | list_add_tail(&req->list, &ctx->defer_list); |
4075 | spin_unlock_irq(&ctx->completion_lock); | |
4076 | return -EIOCBQUEUED; | |
4077 | } | |
4078 | ||
3529d8c2 JA |
4079 | static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
4080 | struct io_kiocb **nxt, bool force_nonblock) | |
2b188cc1 | 4081 | { |
a197f664 | 4082 | struct io_ring_ctx *ctx = req->ctx; |
d625c6ee | 4083 | int ret; |
2b188cc1 | 4084 | |
d625c6ee | 4085 | switch (req->opcode) { |
2b188cc1 | 4086 | case IORING_OP_NOP: |
78e19bbe | 4087 | ret = io_nop(req); |
2b188cc1 JA |
4088 | break; |
4089 | case IORING_OP_READV: | |
edafccee | 4090 | case IORING_OP_READ_FIXED: |
3a6820f2 | 4091 | case IORING_OP_READ: |
3529d8c2 JA |
4092 | if (sqe) { |
4093 | ret = io_read_prep(req, sqe, force_nonblock); | |
4094 | if (ret < 0) | |
4095 | break; | |
4096 | } | |
267bc904 | 4097 | ret = io_read(req, nxt, force_nonblock); |
edafccee | 4098 | break; |
3529d8c2 | 4099 | case IORING_OP_WRITEV: |
edafccee | 4100 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 4101 | case IORING_OP_WRITE: |
3529d8c2 JA |
4102 | if (sqe) { |
4103 | ret = io_write_prep(req, sqe, force_nonblock); | |
4104 | if (ret < 0) | |
4105 | break; | |
4106 | } | |
267bc904 | 4107 | ret = io_write(req, nxt, force_nonblock); |
2b188cc1 | 4108 | break; |
c992fe29 | 4109 | case IORING_OP_FSYNC: |
3529d8c2 JA |
4110 | if (sqe) { |
4111 | ret = io_prep_fsync(req, sqe); | |
4112 | if (ret < 0) | |
4113 | break; | |
4114 | } | |
fc4df999 | 4115 | ret = io_fsync(req, nxt, force_nonblock); |
c992fe29 | 4116 | break; |
221c5eb2 | 4117 | case IORING_OP_POLL_ADD: |
3529d8c2 JA |
4118 | if (sqe) { |
4119 | ret = io_poll_add_prep(req, sqe); | |
4120 | if (ret) | |
4121 | break; | |
4122 | } | |
fc4df999 | 4123 | ret = io_poll_add(req, nxt); |
221c5eb2 JA |
4124 | break; |
4125 | case IORING_OP_POLL_REMOVE: | |
3529d8c2 JA |
4126 | if (sqe) { |
4127 | ret = io_poll_remove_prep(req, sqe); | |
4128 | if (ret < 0) | |
4129 | break; | |
4130 | } | |
fc4df999 | 4131 | ret = io_poll_remove(req); |
221c5eb2 | 4132 | break; |
5d17b4a4 | 4133 | case IORING_OP_SYNC_FILE_RANGE: |
3529d8c2 JA |
4134 | if (sqe) { |
4135 | ret = io_prep_sfr(req, sqe); | |
4136 | if (ret < 0) | |
4137 | break; | |
4138 | } | |
fc4df999 | 4139 | ret = io_sync_file_range(req, nxt, force_nonblock); |
5d17b4a4 | 4140 | break; |
0fa03c62 | 4141 | case IORING_OP_SENDMSG: |
fddaface | 4142 | case IORING_OP_SEND: |
3529d8c2 JA |
4143 | if (sqe) { |
4144 | ret = io_sendmsg_prep(req, sqe); | |
4145 | if (ret < 0) | |
4146 | break; | |
4147 | } | |
fddaface JA |
4148 | if (req->opcode == IORING_OP_SENDMSG) |
4149 | ret = io_sendmsg(req, nxt, force_nonblock); | |
4150 | else | |
4151 | ret = io_send(req, nxt, force_nonblock); | |
0fa03c62 | 4152 | break; |
aa1fa28f | 4153 | case IORING_OP_RECVMSG: |
fddaface | 4154 | case IORING_OP_RECV: |
3529d8c2 JA |
4155 | if (sqe) { |
4156 | ret = io_recvmsg_prep(req, sqe); | |
4157 | if (ret) | |
4158 | break; | |
4159 | } | |
fddaface JA |
4160 | if (req->opcode == IORING_OP_RECVMSG) |
4161 | ret = io_recvmsg(req, nxt, force_nonblock); | |
4162 | else | |
4163 | ret = io_recv(req, nxt, force_nonblock); | |
aa1fa28f | 4164 | break; |
5262f567 | 4165 | case IORING_OP_TIMEOUT: |
3529d8c2 JA |
4166 | if (sqe) { |
4167 | ret = io_timeout_prep(req, sqe, false); | |
4168 | if (ret) | |
4169 | break; | |
4170 | } | |
fc4df999 | 4171 | ret = io_timeout(req); |
5262f567 | 4172 | break; |
11365043 | 4173 | case IORING_OP_TIMEOUT_REMOVE: |
3529d8c2 JA |
4174 | if (sqe) { |
4175 | ret = io_timeout_remove_prep(req, sqe); | |
4176 | if (ret) | |
4177 | break; | |
4178 | } | |
fc4df999 | 4179 | ret = io_timeout_remove(req); |
11365043 | 4180 | break; |
17f2fe35 | 4181 | case IORING_OP_ACCEPT: |
3529d8c2 JA |
4182 | if (sqe) { |
4183 | ret = io_accept_prep(req, sqe); | |
4184 | if (ret) | |
4185 | break; | |
4186 | } | |
fc4df999 | 4187 | ret = io_accept(req, nxt, force_nonblock); |
17f2fe35 | 4188 | break; |
f8e85cf2 | 4189 | case IORING_OP_CONNECT: |
3529d8c2 JA |
4190 | if (sqe) { |
4191 | ret = io_connect_prep(req, sqe); | |
4192 | if (ret) | |
4193 | break; | |
4194 | } | |
fc4df999 | 4195 | ret = io_connect(req, nxt, force_nonblock); |
f8e85cf2 | 4196 | break; |
62755e35 | 4197 | case IORING_OP_ASYNC_CANCEL: |
3529d8c2 JA |
4198 | if (sqe) { |
4199 | ret = io_async_cancel_prep(req, sqe); | |
4200 | if (ret) | |
4201 | break; | |
4202 | } | |
fc4df999 | 4203 | ret = io_async_cancel(req, nxt); |
62755e35 | 4204 | break; |
d63d1b5e JA |
4205 | case IORING_OP_FALLOCATE: |
4206 | if (sqe) { | |
4207 | ret = io_fallocate_prep(req, sqe); | |
4208 | if (ret) | |
4209 | break; | |
4210 | } | |
4211 | ret = io_fallocate(req, nxt, force_nonblock); | |
4212 | break; | |
15b71abe JA |
4213 | case IORING_OP_OPENAT: |
4214 | if (sqe) { | |
4215 | ret = io_openat_prep(req, sqe); | |
4216 | if (ret) | |
4217 | break; | |
4218 | } | |
4219 | ret = io_openat(req, nxt, force_nonblock); | |
4220 | break; | |
b5dba59e JA |
4221 | case IORING_OP_CLOSE: |
4222 | if (sqe) { | |
4223 | ret = io_close_prep(req, sqe); | |
4224 | if (ret) | |
4225 | break; | |
4226 | } | |
4227 | ret = io_close(req, nxt, force_nonblock); | |
4228 | break; | |
05f3fb3c JA |
4229 | case IORING_OP_FILES_UPDATE: |
4230 | if (sqe) { | |
4231 | ret = io_files_update_prep(req, sqe); | |
4232 | if (ret) | |
4233 | break; | |
4234 | } | |
4235 | ret = io_files_update(req, force_nonblock); | |
4236 | break; | |
eddc7ef5 JA |
4237 | case IORING_OP_STATX: |
4238 | if (sqe) { | |
4239 | ret = io_statx_prep(req, sqe); | |
4240 | if (ret) | |
4241 | break; | |
4242 | } | |
4243 | ret = io_statx(req, nxt, force_nonblock); | |
4244 | break; | |
4840e418 JA |
4245 | case IORING_OP_FADVISE: |
4246 | if (sqe) { | |
4247 | ret = io_fadvise_prep(req, sqe); | |
4248 | if (ret) | |
4249 | break; | |
4250 | } | |
4251 | ret = io_fadvise(req, nxt, force_nonblock); | |
4252 | break; | |
c1ca757b JA |
4253 | case IORING_OP_MADVISE: |
4254 | if (sqe) { | |
4255 | ret = io_madvise_prep(req, sqe); | |
4256 | if (ret) | |
4257 | break; | |
4258 | } | |
4259 | ret = io_madvise(req, nxt, force_nonblock); | |
4260 | break; | |
cebdb986 JA |
4261 | case IORING_OP_OPENAT2: |
4262 | if (sqe) { | |
4263 | ret = io_openat2_prep(req, sqe); | |
4264 | if (ret) | |
4265 | break; | |
4266 | } | |
4267 | ret = io_openat2(req, nxt, force_nonblock); | |
4268 | break; | |
2b188cc1 JA |
4269 | default: |
4270 | ret = -EINVAL; | |
4271 | break; | |
4272 | } | |
4273 | ||
def596e9 JA |
4274 | if (ret) |
4275 | return ret; | |
4276 | ||
4277 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
11ba820b JA |
4278 | const bool in_async = io_wq_current_is_worker(); |
4279 | ||
9e645e11 | 4280 | if (req->result == -EAGAIN) |
def596e9 JA |
4281 | return -EAGAIN; |
4282 | ||
11ba820b JA |
4283 | /* workqueue context doesn't hold uring_lock, grab it now */ |
4284 | if (in_async) | |
4285 | mutex_lock(&ctx->uring_lock); | |
4286 | ||
def596e9 | 4287 | io_iopoll_req_issued(req); |
11ba820b JA |
4288 | |
4289 | if (in_async) | |
4290 | mutex_unlock(&ctx->uring_lock); | |
def596e9 JA |
4291 | } |
4292 | ||
4293 | return 0; | |
2b188cc1 JA |
4294 | } |
4295 | ||
561fb04a | 4296 | static void io_wq_submit_work(struct io_wq_work **workptr) |
2b188cc1 | 4297 | { |
561fb04a | 4298 | struct io_wq_work *work = *workptr; |
2b188cc1 | 4299 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
561fb04a JA |
4300 | struct io_kiocb *nxt = NULL; |
4301 | int ret = 0; | |
2b188cc1 | 4302 | |
0c9d5ccd JA |
4303 | /* if NO_CANCEL is set, we must still run the work */ |
4304 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == | |
4305 | IO_WQ_WORK_CANCEL) { | |
561fb04a | 4306 | ret = -ECANCELED; |
0c9d5ccd | 4307 | } |
31b51510 | 4308 | |
561fb04a | 4309 | if (!ret) { |
cf6fd4bd PB |
4310 | req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0; |
4311 | req->in_async = true; | |
561fb04a | 4312 | do { |
3529d8c2 | 4313 | ret = io_issue_sqe(req, NULL, &nxt, false); |
561fb04a JA |
4314 | /* |
4315 | * We can get EAGAIN for polled IO even though we're | |
4316 | * forcing a sync submission from here, since we can't | |
4317 | * wait for request slots on the block side. | |
4318 | */ | |
4319 | if (ret != -EAGAIN) | |
4320 | break; | |
4321 | cond_resched(); | |
4322 | } while (1); | |
4323 | } | |
31b51510 | 4324 | |
561fb04a | 4325 | /* drop submission reference */ |
ec9c02ad | 4326 | io_put_req(req); |
817869d2 | 4327 | |
561fb04a | 4328 | if (ret) { |
4e88d6e7 | 4329 | req_set_fail_links(req); |
78e19bbe | 4330 | io_cqring_add_event(req, ret); |
817869d2 | 4331 | io_put_req(req); |
edafccee | 4332 | } |
2b188cc1 | 4333 | |
561fb04a | 4334 | /* if a dependent link is ready, pass it back */ |
78912934 JA |
4335 | if (!ret && nxt) |
4336 | io_wq_assign_next(workptr, nxt); | |
2b188cc1 JA |
4337 | } |
4338 | ||
15b71abe | 4339 | static int io_req_needs_file(struct io_kiocb *req, int fd) |
09bb8394 | 4340 | { |
d3656344 | 4341 | if (!io_op_defs[req->opcode].needs_file) |
9e3aa61a | 4342 | return 0; |
d3656344 JA |
4343 | if (fd == -1 && io_op_defs[req->opcode].fd_non_neg) |
4344 | return 0; | |
4345 | return 1; | |
09bb8394 JA |
4346 | } |
4347 | ||
65e19f54 JA |
4348 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
4349 | int index) | |
4350 | { | |
4351 | struct fixed_file_table *table; | |
4352 | ||
05f3fb3c JA |
4353 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
4354 | return table->files[index & IORING_FILE_TABLE_MASK];; | |
65e19f54 JA |
4355 | } |
4356 | ||
3529d8c2 JA |
4357 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
4358 | const struct io_uring_sqe *sqe) | |
09bb8394 | 4359 | { |
a197f664 | 4360 | struct io_ring_ctx *ctx = req->ctx; |
09bb8394 | 4361 | unsigned flags; |
d3656344 | 4362 | int fd; |
09bb8394 | 4363 | |
3529d8c2 JA |
4364 | flags = READ_ONCE(sqe->flags); |
4365 | fd = READ_ONCE(sqe->fd); | |
09bb8394 | 4366 | |
d3656344 JA |
4367 | if (!io_req_needs_file(req, fd)) |
4368 | return 0; | |
09bb8394 JA |
4369 | |
4370 | if (flags & IOSQE_FIXED_FILE) { | |
05f3fb3c | 4371 | if (unlikely(!ctx->file_data || |
09bb8394 JA |
4372 | (unsigned) fd >= ctx->nr_user_files)) |
4373 | return -EBADF; | |
b7620121 | 4374 | fd = array_index_nospec(fd, ctx->nr_user_files); |
65e19f54 JA |
4375 | req->file = io_file_from_index(ctx, fd); |
4376 | if (!req->file) | |
08a45173 | 4377 | return -EBADF; |
09bb8394 | 4378 | req->flags |= REQ_F_FIXED_FILE; |
05f3fb3c | 4379 | percpu_ref_get(&ctx->file_data->refs); |
09bb8394 | 4380 | } else { |
cf6fd4bd | 4381 | if (req->needs_fixed_file) |
09bb8394 | 4382 | return -EBADF; |
c826bd7a | 4383 | trace_io_uring_file_get(ctx, fd); |
09bb8394 JA |
4384 | req->file = io_file_get(state, fd); |
4385 | if (unlikely(!req->file)) | |
4386 | return -EBADF; | |
4387 | } | |
4388 | ||
4389 | return 0; | |
4390 | } | |
4391 | ||
a197f664 | 4392 | static int io_grab_files(struct io_kiocb *req) |
fcb323cc JA |
4393 | { |
4394 | int ret = -EBADF; | |
a197f664 | 4395 | struct io_ring_ctx *ctx = req->ctx; |
fcb323cc | 4396 | |
b14cca0c | 4397 | if (!ctx->ring_file) |
b5dba59e JA |
4398 | return -EBADF; |
4399 | ||
fcb323cc JA |
4400 | rcu_read_lock(); |
4401 | spin_lock_irq(&ctx->inflight_lock); | |
4402 | /* | |
4403 | * We use the f_ops->flush() handler to ensure that we can flush | |
4404 | * out work accessing these files if the fd is closed. Check if | |
4405 | * the fd has changed since we started down this path, and disallow | |
4406 | * this operation if it has. | |
4407 | */ | |
b14cca0c | 4408 | if (fcheck(ctx->ring_fd) == ctx->ring_file) { |
fcb323cc JA |
4409 | list_add(&req->inflight_entry, &ctx->inflight_list); |
4410 | req->flags |= REQ_F_INFLIGHT; | |
4411 | req->work.files = current->files; | |
4412 | ret = 0; | |
4413 | } | |
4414 | spin_unlock_irq(&ctx->inflight_lock); | |
4415 | rcu_read_unlock(); | |
4416 | ||
4417 | return ret; | |
4418 | } | |
4419 | ||
2665abfd | 4420 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 4421 | { |
ad8a48ac JA |
4422 | struct io_timeout_data *data = container_of(timer, |
4423 | struct io_timeout_data, timer); | |
4424 | struct io_kiocb *req = data->req; | |
2665abfd JA |
4425 | struct io_ring_ctx *ctx = req->ctx; |
4426 | struct io_kiocb *prev = NULL; | |
4427 | unsigned long flags; | |
2665abfd JA |
4428 | |
4429 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
4430 | ||
4431 | /* | |
4432 | * We don't expect the list to be empty, that will only happen if we | |
4433 | * race with the completion of the linked work. | |
4434 | */ | |
4493233e PB |
4435 | if (!list_empty(&req->link_list)) { |
4436 | prev = list_entry(req->link_list.prev, struct io_kiocb, | |
4437 | link_list); | |
5d960724 | 4438 | if (refcount_inc_not_zero(&prev->refs)) { |
4493233e | 4439 | list_del_init(&req->link_list); |
5d960724 JA |
4440 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
4441 | } else | |
76a46e06 | 4442 | prev = NULL; |
2665abfd JA |
4443 | } |
4444 | ||
4445 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
4446 | ||
4447 | if (prev) { | |
4e88d6e7 | 4448 | req_set_fail_links(prev); |
b0dd8a41 JA |
4449 | io_async_find_and_cancel(ctx, req, prev->user_data, NULL, |
4450 | -ETIME); | |
76a46e06 | 4451 | io_put_req(prev); |
47f46768 JA |
4452 | } else { |
4453 | io_cqring_add_event(req, -ETIME); | |
4454 | io_put_req(req); | |
2665abfd | 4455 | } |
2665abfd JA |
4456 | return HRTIMER_NORESTART; |
4457 | } | |
4458 | ||
ad8a48ac | 4459 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 4460 | { |
76a46e06 | 4461 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 4462 | |
76a46e06 JA |
4463 | /* |
4464 | * If the list is now empty, then our linked request finished before | |
4465 | * we got a chance to setup the timer | |
4466 | */ | |
4467 | spin_lock_irq(&ctx->completion_lock); | |
4493233e | 4468 | if (!list_empty(&req->link_list)) { |
2d28390a | 4469 | struct io_timeout_data *data = &req->io->timeout; |
94ae5e77 | 4470 | |
ad8a48ac JA |
4471 | data->timer.function = io_link_timeout_fn; |
4472 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
4473 | data->mode); | |
2665abfd | 4474 | } |
76a46e06 | 4475 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 4476 | |
2665abfd | 4477 | /* drop submission reference */ |
76a46e06 JA |
4478 | io_put_req(req); |
4479 | } | |
2665abfd | 4480 | |
ad8a48ac | 4481 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd JA |
4482 | { |
4483 | struct io_kiocb *nxt; | |
4484 | ||
4485 | if (!(req->flags & REQ_F_LINK)) | |
4486 | return NULL; | |
4487 | ||
4493233e PB |
4488 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
4489 | link_list); | |
d625c6ee | 4490 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
76a46e06 | 4491 | return NULL; |
2665abfd | 4492 | |
76a46e06 | 4493 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 4494 | return nxt; |
2665abfd JA |
4495 | } |
4496 | ||
3529d8c2 | 4497 | static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 4498 | { |
4a0a7a18 | 4499 | struct io_kiocb *linked_timeout; |
f9bd67f6 | 4500 | struct io_kiocb *nxt = NULL; |
e0c5c576 | 4501 | int ret; |
2b188cc1 | 4502 | |
4a0a7a18 JA |
4503 | again: |
4504 | linked_timeout = io_prep_linked_timeout(req); | |
4505 | ||
3529d8c2 | 4506 | ret = io_issue_sqe(req, sqe, &nxt, true); |
491381ce JA |
4507 | |
4508 | /* | |
4509 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
4510 | * doesn't support non-blocking read/write attempts | |
4511 | */ | |
4512 | if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) || | |
4513 | (req->flags & REQ_F_MUST_PUNT))) { | |
86a761f8 | 4514 | punt: |
bbad27b2 PB |
4515 | if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) { |
4516 | ret = io_grab_files(req); | |
4517 | if (ret) | |
4518 | goto err; | |
2b188cc1 | 4519 | } |
bbad27b2 PB |
4520 | |
4521 | /* | |
4522 | * Queued up for async execution, worker will release | |
4523 | * submit reference when the iocb is actually submitted. | |
4524 | */ | |
4525 | io_queue_async_work(req); | |
4a0a7a18 | 4526 | goto done_req; |
2b188cc1 | 4527 | } |
e65ef56d | 4528 | |
fcb323cc | 4529 | err: |
76a46e06 | 4530 | /* drop submission reference */ |
ec9c02ad | 4531 | io_put_req(req); |
e65ef56d | 4532 | |
f9bd67f6 | 4533 | if (linked_timeout) { |
76a46e06 | 4534 | if (!ret) |
f9bd67f6 | 4535 | io_queue_linked_timeout(linked_timeout); |
76a46e06 | 4536 | else |
f9bd67f6 | 4537 | io_put_req(linked_timeout); |
76a46e06 JA |
4538 | } |
4539 | ||
e65ef56d | 4540 | /* and drop final reference, if we failed */ |
9e645e11 | 4541 | if (ret) { |
78e19bbe | 4542 | io_cqring_add_event(req, ret); |
4e88d6e7 | 4543 | req_set_fail_links(req); |
e65ef56d | 4544 | io_put_req(req); |
9e645e11 | 4545 | } |
4a0a7a18 JA |
4546 | done_req: |
4547 | if (nxt) { | |
4548 | req = nxt; | |
4549 | nxt = NULL; | |
86a761f8 PB |
4550 | |
4551 | if (req->flags & REQ_F_FORCE_ASYNC) | |
4552 | goto punt; | |
4a0a7a18 JA |
4553 | goto again; |
4554 | } | |
2b188cc1 JA |
4555 | } |
4556 | ||
3529d8c2 | 4557 | static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4fe2c963 JL |
4558 | { |
4559 | int ret; | |
4560 | ||
3529d8c2 | 4561 | ret = io_req_defer(req, sqe); |
4fe2c963 JL |
4562 | if (ret) { |
4563 | if (ret != -EIOCBQUEUED) { | |
1118591a | 4564 | fail_req: |
78e19bbe | 4565 | io_cqring_add_event(req, ret); |
4e88d6e7 | 4566 | req_set_fail_links(req); |
78e19bbe | 4567 | io_double_put_req(req); |
4fe2c963 | 4568 | } |
2550878f | 4569 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
1118591a PB |
4570 | ret = io_req_defer_prep(req, sqe); |
4571 | if (unlikely(ret < 0)) | |
4572 | goto fail_req; | |
ce35a47a JA |
4573 | /* |
4574 | * Never try inline submit of IOSQE_ASYNC is set, go straight | |
4575 | * to async execution. | |
4576 | */ | |
4577 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
4578 | io_queue_async_work(req); | |
4579 | } else { | |
3529d8c2 | 4580 | __io_queue_sqe(req, sqe); |
ce35a47a | 4581 | } |
4fe2c963 JL |
4582 | } |
4583 | ||
1b4a51b6 | 4584 | static inline void io_queue_link_head(struct io_kiocb *req) |
4fe2c963 | 4585 | { |
94ae5e77 | 4586 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
1b4a51b6 PB |
4587 | io_cqring_add_event(req, -ECANCELED); |
4588 | io_double_put_req(req); | |
4589 | } else | |
3529d8c2 | 4590 | io_queue_sqe(req, NULL); |
4fe2c963 JL |
4591 | } |
4592 | ||
4e88d6e7 | 4593 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
ce35a47a | 4594 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) |
9e645e11 | 4595 | |
3529d8c2 JA |
4596 | static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
4597 | struct io_submit_state *state, struct io_kiocb **link) | |
9e645e11 | 4598 | { |
a197f664 | 4599 | struct io_ring_ctx *ctx = req->ctx; |
32fe525b | 4600 | unsigned int sqe_flags; |
9e645e11 JA |
4601 | int ret; |
4602 | ||
32fe525b PB |
4603 | sqe_flags = READ_ONCE(sqe->flags); |
4604 | ||
9e645e11 | 4605 | /* enforce forwards compatibility on users */ |
32fe525b | 4606 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
9e645e11 | 4607 | ret = -EINVAL; |
196be95c | 4608 | goto err_req; |
9e645e11 | 4609 | } |
6b47ee6e PB |
4610 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
4611 | req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK| | |
4612 | IOSQE_ASYNC); | |
9e645e11 | 4613 | |
3529d8c2 | 4614 | ret = io_req_set_file(state, req, sqe); |
9e645e11 JA |
4615 | if (unlikely(ret)) { |
4616 | err_req: | |
78e19bbe JA |
4617 | io_cqring_add_event(req, ret); |
4618 | io_double_put_req(req); | |
2e6e1fde | 4619 | return false; |
9e645e11 JA |
4620 | } |
4621 | ||
9e645e11 JA |
4622 | /* |
4623 | * If we already have a head request, queue this one for async | |
4624 | * submittal once the head completes. If we don't have a head but | |
4625 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
4626 | * submitted sync once the chain is complete. If none of those | |
4627 | * conditions are true (normal request), then just queue it. | |
4628 | */ | |
4629 | if (*link) { | |
9d76377f | 4630 | struct io_kiocb *head = *link; |
9e645e11 | 4631 | |
8cdf2193 PB |
4632 | /* |
4633 | * Taking sequential execution of a link, draining both sides | |
4634 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all | |
4635 | * requests in the link. So, it drains the head and the | |
4636 | * next after the link request. The last one is done via | |
4637 | * drain_next flag to persist the effect across calls. | |
4638 | */ | |
711be031 PB |
4639 | if (sqe_flags & IOSQE_IO_DRAIN) { |
4640 | head->flags |= REQ_F_IO_DRAIN; | |
4641 | ctx->drain_next = 1; | |
4642 | } | |
b7bb4f7d | 4643 | if (io_alloc_async_ctx(req)) { |
9e645e11 JA |
4644 | ret = -EAGAIN; |
4645 | goto err_req; | |
4646 | } | |
4647 | ||
3529d8c2 | 4648 | ret = io_req_defer_prep(req, sqe); |
2d28390a | 4649 | if (ret) { |
4e88d6e7 | 4650 | /* fail even hard links since we don't submit */ |
9d76377f | 4651 | head->flags |= REQ_F_FAIL_LINK; |
f67676d1 | 4652 | goto err_req; |
2d28390a | 4653 | } |
9d76377f PB |
4654 | trace_io_uring_link(ctx, req, head); |
4655 | list_add_tail(&req->link_list, &head->link_list); | |
32fe525b PB |
4656 | |
4657 | /* last request of a link, enqueue the link */ | |
4658 | if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) { | |
4659 | io_queue_link_head(head); | |
4660 | *link = NULL; | |
4661 | } | |
9e645e11 | 4662 | } else { |
711be031 PB |
4663 | if (unlikely(ctx->drain_next)) { |
4664 | req->flags |= REQ_F_IO_DRAIN; | |
4665 | req->ctx->drain_next = 0; | |
4666 | } | |
4667 | if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) { | |
4668 | req->flags |= REQ_F_LINK; | |
711be031 PB |
4669 | INIT_LIST_HEAD(&req->link_list); |
4670 | ret = io_req_defer_prep(req, sqe); | |
4671 | if (ret) | |
4672 | req->flags |= REQ_F_FAIL_LINK; | |
4673 | *link = req; | |
4674 | } else { | |
4675 | io_queue_sqe(req, sqe); | |
4676 | } | |
9e645e11 | 4677 | } |
2e6e1fde PB |
4678 | |
4679 | return true; | |
9e645e11 JA |
4680 | } |
4681 | ||
9a56a232 JA |
4682 | /* |
4683 | * Batched submission is done, ensure local IO is flushed out. | |
4684 | */ | |
4685 | static void io_submit_state_end(struct io_submit_state *state) | |
4686 | { | |
4687 | blk_finish_plug(&state->plug); | |
3d6770fb | 4688 | io_file_put(state); |
2579f913 JA |
4689 | if (state->free_reqs) |
4690 | kmem_cache_free_bulk(req_cachep, state->free_reqs, | |
4691 | &state->reqs[state->cur_req]); | |
9a56a232 JA |
4692 | } |
4693 | ||
4694 | /* | |
4695 | * Start submission side cache. | |
4696 | */ | |
4697 | static void io_submit_state_start(struct io_submit_state *state, | |
22efde59 | 4698 | unsigned int max_ios) |
9a56a232 JA |
4699 | { |
4700 | blk_start_plug(&state->plug); | |
2579f913 | 4701 | state->free_reqs = 0; |
9a56a232 JA |
4702 | state->file = NULL; |
4703 | state->ios_left = max_ios; | |
4704 | } | |
4705 | ||
2b188cc1 JA |
4706 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
4707 | { | |
75b28aff | 4708 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 4709 | |
caf582c6 PB |
4710 | /* |
4711 | * Ensure any loads from the SQEs are done at this point, | |
4712 | * since once we write the new head, the application could | |
4713 | * write new data to them. | |
4714 | */ | |
4715 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
4716 | } |
4717 | ||
2b188cc1 | 4718 | /* |
3529d8c2 | 4719 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
2b188cc1 JA |
4720 | * that is mapped by userspace. This means that care needs to be taken to |
4721 | * ensure that reads are stable, as we cannot rely on userspace always | |
4722 | * being a good citizen. If members of the sqe are validated and then later | |
4723 | * used, it's important that those reads are done through READ_ONCE() to | |
4724 | * prevent a re-load down the line. | |
4725 | */ | |
3529d8c2 JA |
4726 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req, |
4727 | const struct io_uring_sqe **sqe_ptr) | |
2b188cc1 | 4728 | { |
75b28aff | 4729 | u32 *sq_array = ctx->sq_array; |
2b188cc1 JA |
4730 | unsigned head; |
4731 | ||
4732 | /* | |
4733 | * The cached sq head (or cq tail) serves two purposes: | |
4734 | * | |
4735 | * 1) allows us to batch the cost of updating the user visible | |
4736 | * head updates. | |
4737 | * 2) allows the kernel side to track the head on its own, even | |
4738 | * though the application is the one updating it. | |
4739 | */ | |
ee7d46d9 | 4740 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
9835d6fa | 4741 | if (likely(head < ctx->sq_entries)) { |
cf6fd4bd PB |
4742 | /* |
4743 | * All io need record the previous position, if LINK vs DARIN, | |
4744 | * it can be used to mark the position of the first IO in the | |
4745 | * link list. | |
4746 | */ | |
4747 | req->sequence = ctx->cached_sq_head; | |
3529d8c2 JA |
4748 | *sqe_ptr = &ctx->sq_sqes[head]; |
4749 | req->opcode = READ_ONCE((*sqe_ptr)->opcode); | |
4750 | req->user_data = READ_ONCE((*sqe_ptr)->user_data); | |
2b188cc1 JA |
4751 | ctx->cached_sq_head++; |
4752 | return true; | |
4753 | } | |
4754 | ||
4755 | /* drop invalid entries */ | |
4756 | ctx->cached_sq_head++; | |
498ccd9e | 4757 | ctx->cached_sq_dropped++; |
ee7d46d9 | 4758 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
2b188cc1 JA |
4759 | return false; |
4760 | } | |
4761 | ||
fb5ccc98 | 4762 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr, |
ae9428ca PB |
4763 | struct file *ring_file, int ring_fd, |
4764 | struct mm_struct **mm, bool async) | |
6c271ce2 JA |
4765 | { |
4766 | struct io_submit_state state, *statep = NULL; | |
9e645e11 | 4767 | struct io_kiocb *link = NULL; |
9e645e11 | 4768 | int i, submitted = 0; |
95a1b3ff | 4769 | bool mm_fault = false; |
6c271ce2 | 4770 | |
c4a2ed72 | 4771 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
ad3eb2c8 JA |
4772 | if (test_bit(0, &ctx->sq_check_overflow)) { |
4773 | if (!list_empty(&ctx->cq_overflow_list) && | |
4774 | !io_cqring_overflow_flush(ctx, false)) | |
4775 | return -EBUSY; | |
4776 | } | |
6c271ce2 | 4777 | |
ee7d46d9 PB |
4778 | /* make sure SQ entry isn't read before tail */ |
4779 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
9ef4f124 | 4780 | |
2b85edfc PB |
4781 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
4782 | return -EAGAIN; | |
4783 | ||
6c271ce2 | 4784 | if (nr > IO_PLUG_THRESHOLD) { |
22efde59 | 4785 | io_submit_state_start(&state, nr); |
6c271ce2 JA |
4786 | statep = &state; |
4787 | } | |
4788 | ||
b14cca0c PB |
4789 | ctx->ring_fd = ring_fd; |
4790 | ctx->ring_file = ring_file; | |
4791 | ||
6c271ce2 | 4792 | for (i = 0; i < nr; i++) { |
3529d8c2 | 4793 | const struct io_uring_sqe *sqe; |
196be95c | 4794 | struct io_kiocb *req; |
fb5ccc98 | 4795 | |
196be95c PB |
4796 | req = io_get_req(ctx, statep); |
4797 | if (unlikely(!req)) { | |
4798 | if (!submitted) | |
4799 | submitted = -EAGAIN; | |
fb5ccc98 | 4800 | break; |
196be95c | 4801 | } |
3529d8c2 | 4802 | if (!io_get_sqring(ctx, req, &sqe)) { |
2b85edfc | 4803 | __io_req_do_free(req); |
196be95c PB |
4804 | break; |
4805 | } | |
fb5ccc98 | 4806 | |
d3656344 JA |
4807 | /* will complete beyond this point, count as submitted */ |
4808 | submitted++; | |
4809 | ||
4810 | if (unlikely(req->opcode >= IORING_OP_LAST)) { | |
4811 | io_cqring_add_event(req, -EINVAL); | |
4812 | io_double_put_req(req); | |
4813 | break; | |
4814 | } | |
4815 | ||
4816 | if (io_op_defs[req->opcode].needs_mm && !*mm) { | |
95a1b3ff PB |
4817 | mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm); |
4818 | if (!mm_fault) { | |
4819 | use_mm(ctx->sqo_mm); | |
4820 | *mm = ctx->sqo_mm; | |
4821 | } | |
9e645e11 | 4822 | } |
9e645e11 | 4823 | |
cf6fd4bd PB |
4824 | req->has_user = *mm != NULL; |
4825 | req->in_async = async; | |
4826 | req->needs_fixed_file = async; | |
354420f7 JA |
4827 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
4828 | true, async); | |
3529d8c2 | 4829 | if (!io_submit_sqe(req, sqe, statep, &link)) |
2e6e1fde | 4830 | break; |
6c271ce2 JA |
4831 | } |
4832 | ||
2b85edfc PB |
4833 | if (submitted != nr) |
4834 | percpu_ref_put_many(&ctx->refs, nr - submitted); | |
9e645e11 | 4835 | if (link) |
1b4a51b6 | 4836 | io_queue_link_head(link); |
6c271ce2 JA |
4837 | if (statep) |
4838 | io_submit_state_end(&state); | |
4839 | ||
ae9428ca PB |
4840 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
4841 | io_commit_sqring(ctx); | |
4842 | ||
6c271ce2 JA |
4843 | return submitted; |
4844 | } | |
4845 | ||
4846 | static int io_sq_thread(void *data) | |
4847 | { | |
6c271ce2 JA |
4848 | struct io_ring_ctx *ctx = data; |
4849 | struct mm_struct *cur_mm = NULL; | |
181e448d | 4850 | const struct cred *old_cred; |
6c271ce2 JA |
4851 | mm_segment_t old_fs; |
4852 | DEFINE_WAIT(wait); | |
4853 | unsigned inflight; | |
4854 | unsigned long timeout; | |
c1edbf5f | 4855 | int ret; |
6c271ce2 | 4856 | |
206aefde | 4857 | complete(&ctx->completions[1]); |
a4c0b3de | 4858 | |
6c271ce2 JA |
4859 | old_fs = get_fs(); |
4860 | set_fs(USER_DS); | |
181e448d | 4861 | old_cred = override_creds(ctx->creds); |
6c271ce2 | 4862 | |
c1edbf5f | 4863 | ret = timeout = inflight = 0; |
2bbcd6d3 | 4864 | while (!kthread_should_park()) { |
fb5ccc98 | 4865 | unsigned int to_submit; |
6c271ce2 JA |
4866 | |
4867 | if (inflight) { | |
4868 | unsigned nr_events = 0; | |
4869 | ||
4870 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
2b2ed975 JA |
4871 | /* |
4872 | * inflight is the count of the maximum possible | |
4873 | * entries we submitted, but it can be smaller | |
4874 | * if we dropped some of them. If we don't have | |
4875 | * poll entries available, then we know that we | |
4876 | * have nothing left to poll for. Reset the | |
4877 | * inflight count to zero in that case. | |
4878 | */ | |
4879 | mutex_lock(&ctx->uring_lock); | |
4880 | if (!list_empty(&ctx->poll_list)) | |
4881 | __io_iopoll_check(ctx, &nr_events, 0); | |
4882 | else | |
4883 | inflight = 0; | |
4884 | mutex_unlock(&ctx->uring_lock); | |
6c271ce2 JA |
4885 | } else { |
4886 | /* | |
4887 | * Normal IO, just pretend everything completed. | |
4888 | * We don't have to poll completions for that. | |
4889 | */ | |
4890 | nr_events = inflight; | |
4891 | } | |
4892 | ||
4893 | inflight -= nr_events; | |
4894 | if (!inflight) | |
4895 | timeout = jiffies + ctx->sq_thread_idle; | |
4896 | } | |
4897 | ||
fb5ccc98 | 4898 | to_submit = io_sqring_entries(ctx); |
c1edbf5f JA |
4899 | |
4900 | /* | |
4901 | * If submit got -EBUSY, flag us as needing the application | |
4902 | * to enter the kernel to reap and flush events. | |
4903 | */ | |
4904 | if (!to_submit || ret == -EBUSY) { | |
6c271ce2 JA |
4905 | /* |
4906 | * We're polling. If we're within the defined idle | |
4907 | * period, then let us spin without work before going | |
c1edbf5f JA |
4908 | * to sleep. The exception is if we got EBUSY doing |
4909 | * more IO, we should wait for the application to | |
4910 | * reap events and wake us up. | |
6c271ce2 | 4911 | */ |
c1edbf5f JA |
4912 | if (inflight || |
4913 | (!time_after(jiffies, timeout) && ret != -EBUSY)) { | |
9831a90c | 4914 | cond_resched(); |
6c271ce2 JA |
4915 | continue; |
4916 | } | |
4917 | ||
4918 | /* | |
4919 | * Drop cur_mm before scheduling, we can't hold it for | |
4920 | * long periods (or over schedule()). Do this before | |
4921 | * adding ourselves to the waitqueue, as the unuse/drop | |
4922 | * may sleep. | |
4923 | */ | |
4924 | if (cur_mm) { | |
4925 | unuse_mm(cur_mm); | |
4926 | mmput(cur_mm); | |
4927 | cur_mm = NULL; | |
4928 | } | |
4929 | ||
4930 | prepare_to_wait(&ctx->sqo_wait, &wait, | |
4931 | TASK_INTERRUPTIBLE); | |
4932 | ||
4933 | /* Tell userspace we may need a wakeup call */ | |
75b28aff | 4934 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
0d7bae69 SB |
4935 | /* make sure to read SQ tail after writing flags */ |
4936 | smp_mb(); | |
6c271ce2 | 4937 | |
fb5ccc98 | 4938 | to_submit = io_sqring_entries(ctx); |
c1edbf5f | 4939 | if (!to_submit || ret == -EBUSY) { |
2bbcd6d3 | 4940 | if (kthread_should_park()) { |
6c271ce2 JA |
4941 | finish_wait(&ctx->sqo_wait, &wait); |
4942 | break; | |
4943 | } | |
4944 | if (signal_pending(current)) | |
4945 | flush_signals(current); | |
4946 | schedule(); | |
4947 | finish_wait(&ctx->sqo_wait, &wait); | |
4948 | ||
75b28aff | 4949 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
6c271ce2 JA |
4950 | continue; |
4951 | } | |
4952 | finish_wait(&ctx->sqo_wait, &wait); | |
4953 | ||
75b28aff | 4954 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
6c271ce2 JA |
4955 | } |
4956 | ||
8a4955ff | 4957 | mutex_lock(&ctx->uring_lock); |
1d7bb1d5 | 4958 | ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true); |
8a4955ff | 4959 | mutex_unlock(&ctx->uring_lock); |
1d7bb1d5 JA |
4960 | if (ret > 0) |
4961 | inflight += ret; | |
6c271ce2 JA |
4962 | } |
4963 | ||
4964 | set_fs(old_fs); | |
4965 | if (cur_mm) { | |
4966 | unuse_mm(cur_mm); | |
4967 | mmput(cur_mm); | |
4968 | } | |
181e448d | 4969 | revert_creds(old_cred); |
06058632 | 4970 | |
2bbcd6d3 | 4971 | kthread_parkme(); |
06058632 | 4972 | |
6c271ce2 JA |
4973 | return 0; |
4974 | } | |
4975 | ||
bda52162 JA |
4976 | struct io_wait_queue { |
4977 | struct wait_queue_entry wq; | |
4978 | struct io_ring_ctx *ctx; | |
4979 | unsigned to_wait; | |
4980 | unsigned nr_timeouts; | |
4981 | }; | |
4982 | ||
1d7bb1d5 | 4983 | static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush) |
bda52162 JA |
4984 | { |
4985 | struct io_ring_ctx *ctx = iowq->ctx; | |
4986 | ||
4987 | /* | |
d195a66e | 4988 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
4989 | * started waiting. For timeouts, we always want to return to userspace, |
4990 | * regardless of event count. | |
4991 | */ | |
1d7bb1d5 | 4992 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
bda52162 JA |
4993 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
4994 | } | |
4995 | ||
4996 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
4997 | int wake_flags, void *key) | |
4998 | { | |
4999 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
5000 | wq); | |
5001 | ||
1d7bb1d5 JA |
5002 | /* use noflush == true, as we can't safely rely on locking context */ |
5003 | if (!io_should_wake(iowq, true)) | |
bda52162 JA |
5004 | return -1; |
5005 | ||
5006 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
5007 | } | |
5008 | ||
2b188cc1 JA |
5009 | /* |
5010 | * Wait until events become available, if we don't already have some. The | |
5011 | * application must reap them itself, as they reside on the shared cq ring. | |
5012 | */ | |
5013 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
5014 | const sigset_t __user *sig, size_t sigsz) | |
5015 | { | |
bda52162 JA |
5016 | struct io_wait_queue iowq = { |
5017 | .wq = { | |
5018 | .private = current, | |
5019 | .func = io_wake_function, | |
5020 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
5021 | }, | |
5022 | .ctx = ctx, | |
5023 | .to_wait = min_events, | |
5024 | }; | |
75b28aff | 5025 | struct io_rings *rings = ctx->rings; |
e9ffa5c2 | 5026 | int ret = 0; |
2b188cc1 | 5027 | |
1d7bb1d5 | 5028 | if (io_cqring_events(ctx, false) >= min_events) |
2b188cc1 JA |
5029 | return 0; |
5030 | ||
5031 | if (sig) { | |
9e75ad5d AB |
5032 | #ifdef CONFIG_COMPAT |
5033 | if (in_compat_syscall()) | |
5034 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 5035 | sigsz); |
9e75ad5d AB |
5036 | else |
5037 | #endif | |
b772434b | 5038 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 5039 | |
2b188cc1 JA |
5040 | if (ret) |
5041 | return ret; | |
5042 | } | |
5043 | ||
bda52162 | 5044 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
c826bd7a | 5045 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 JA |
5046 | do { |
5047 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, | |
5048 | TASK_INTERRUPTIBLE); | |
1d7bb1d5 | 5049 | if (io_should_wake(&iowq, false)) |
bda52162 JA |
5050 | break; |
5051 | schedule(); | |
5052 | if (signal_pending(current)) { | |
e9ffa5c2 | 5053 | ret = -EINTR; |
bda52162 JA |
5054 | break; |
5055 | } | |
5056 | } while (1); | |
5057 | finish_wait(&ctx->wait, &iowq.wq); | |
5058 | ||
e9ffa5c2 | 5059 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 5060 | |
75b28aff | 5061 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
5062 | } |
5063 | ||
6b06314c JA |
5064 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
5065 | { | |
5066 | #if defined(CONFIG_UNIX) | |
5067 | if (ctx->ring_sock) { | |
5068 | struct sock *sock = ctx->ring_sock->sk; | |
5069 | struct sk_buff *skb; | |
5070 | ||
5071 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
5072 | kfree_skb(skb); | |
5073 | } | |
5074 | #else | |
5075 | int i; | |
5076 | ||
65e19f54 JA |
5077 | for (i = 0; i < ctx->nr_user_files; i++) { |
5078 | struct file *file; | |
5079 | ||
5080 | file = io_file_from_index(ctx, i); | |
5081 | if (file) | |
5082 | fput(file); | |
5083 | } | |
6b06314c JA |
5084 | #endif |
5085 | } | |
5086 | ||
05f3fb3c JA |
5087 | static void io_file_ref_kill(struct percpu_ref *ref) |
5088 | { | |
5089 | struct fixed_file_data *data; | |
5090 | ||
5091 | data = container_of(ref, struct fixed_file_data, refs); | |
5092 | complete(&data->done); | |
5093 | } | |
5094 | ||
6b06314c JA |
5095 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
5096 | { | |
05f3fb3c | 5097 | struct fixed_file_data *data = ctx->file_data; |
65e19f54 JA |
5098 | unsigned nr_tables, i; |
5099 | ||
05f3fb3c | 5100 | if (!data) |
6b06314c JA |
5101 | return -ENXIO; |
5102 | ||
05f3fb3c | 5103 | /* protect against inflight atomic switch, which drops the ref */ |
05f3fb3c | 5104 | percpu_ref_get(&data->refs); |
e46a7950 JA |
5105 | /* wait for existing switches */ |
5106 | flush_work(&data->ref_work); | |
05f3fb3c JA |
5107 | percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill); |
5108 | wait_for_completion(&data->done); | |
5109 | percpu_ref_put(&data->refs); | |
e46a7950 JA |
5110 | /* flush potential new switch */ |
5111 | flush_work(&data->ref_work); | |
05f3fb3c JA |
5112 | percpu_ref_exit(&data->refs); |
5113 | ||
6b06314c | 5114 | __io_sqe_files_unregister(ctx); |
65e19f54 JA |
5115 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
5116 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c JA |
5117 | kfree(data->table[i].files); |
5118 | kfree(data->table); | |
5119 | kfree(data); | |
5120 | ctx->file_data = NULL; | |
6b06314c JA |
5121 | ctx->nr_user_files = 0; |
5122 | return 0; | |
5123 | } | |
5124 | ||
6c271ce2 JA |
5125 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
5126 | { | |
5127 | if (ctx->sqo_thread) { | |
206aefde | 5128 | wait_for_completion(&ctx->completions[1]); |
2bbcd6d3 RP |
5129 | /* |
5130 | * The park is a bit of a work-around, without it we get | |
5131 | * warning spews on shutdown with SQPOLL set and affinity | |
5132 | * set to a single CPU. | |
5133 | */ | |
06058632 | 5134 | kthread_park(ctx->sqo_thread); |
6c271ce2 JA |
5135 | kthread_stop(ctx->sqo_thread); |
5136 | ctx->sqo_thread = NULL; | |
5137 | } | |
5138 | } | |
5139 | ||
6b06314c JA |
5140 | static void io_finish_async(struct io_ring_ctx *ctx) |
5141 | { | |
6c271ce2 JA |
5142 | io_sq_thread_stop(ctx); |
5143 | ||
561fb04a JA |
5144 | if (ctx->io_wq) { |
5145 | io_wq_destroy(ctx->io_wq); | |
5146 | ctx->io_wq = NULL; | |
6b06314c JA |
5147 | } |
5148 | } | |
5149 | ||
5150 | #if defined(CONFIG_UNIX) | |
6b06314c JA |
5151 | /* |
5152 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
5153 | * the io_uring can be safely unregistered on process exit, even if we have | |
5154 | * loops in the file referencing. | |
5155 | */ | |
5156 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
5157 | { | |
5158 | struct sock *sk = ctx->ring_sock->sk; | |
5159 | struct scm_fp_list *fpl; | |
5160 | struct sk_buff *skb; | |
08a45173 | 5161 | int i, nr_files; |
6b06314c JA |
5162 | |
5163 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { | |
5164 | unsigned long inflight = ctx->user->unix_inflight + nr; | |
5165 | ||
5166 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) | |
5167 | return -EMFILE; | |
5168 | } | |
5169 | ||
5170 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); | |
5171 | if (!fpl) | |
5172 | return -ENOMEM; | |
5173 | ||
5174 | skb = alloc_skb(0, GFP_KERNEL); | |
5175 | if (!skb) { | |
5176 | kfree(fpl); | |
5177 | return -ENOMEM; | |
5178 | } | |
5179 | ||
5180 | skb->sk = sk; | |
6b06314c | 5181 | |
08a45173 | 5182 | nr_files = 0; |
6b06314c JA |
5183 | fpl->user = get_uid(ctx->user); |
5184 | for (i = 0; i < nr; i++) { | |
65e19f54 JA |
5185 | struct file *file = io_file_from_index(ctx, i + offset); |
5186 | ||
5187 | if (!file) | |
08a45173 | 5188 | continue; |
65e19f54 | 5189 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
5190 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
5191 | nr_files++; | |
6b06314c JA |
5192 | } |
5193 | ||
08a45173 JA |
5194 | if (nr_files) { |
5195 | fpl->max = SCM_MAX_FD; | |
5196 | fpl->count = nr_files; | |
5197 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 5198 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
5199 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
5200 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 5201 | |
08a45173 JA |
5202 | for (i = 0; i < nr_files; i++) |
5203 | fput(fpl->fp[i]); | |
5204 | } else { | |
5205 | kfree_skb(skb); | |
5206 | kfree(fpl); | |
5207 | } | |
6b06314c JA |
5208 | |
5209 | return 0; | |
5210 | } | |
5211 | ||
5212 | /* | |
5213 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
5214 | * causes regular reference counting to break down. We rely on the UNIX | |
5215 | * garbage collection to take care of this problem for us. | |
5216 | */ | |
5217 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
5218 | { | |
5219 | unsigned left, total; | |
5220 | int ret = 0; | |
5221 | ||
5222 | total = 0; | |
5223 | left = ctx->nr_user_files; | |
5224 | while (left) { | |
5225 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
5226 | |
5227 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
5228 | if (ret) | |
5229 | break; | |
5230 | left -= this_files; | |
5231 | total += this_files; | |
5232 | } | |
5233 | ||
5234 | if (!ret) | |
5235 | return 0; | |
5236 | ||
5237 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
5238 | struct file *file = io_file_from_index(ctx, total); |
5239 | ||
5240 | if (file) | |
5241 | fput(file); | |
6b06314c JA |
5242 | total++; |
5243 | } | |
5244 | ||
5245 | return ret; | |
5246 | } | |
5247 | #else | |
5248 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
5249 | { | |
5250 | return 0; | |
5251 | } | |
5252 | #endif | |
5253 | ||
65e19f54 JA |
5254 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
5255 | unsigned nr_files) | |
5256 | { | |
5257 | int i; | |
5258 | ||
5259 | for (i = 0; i < nr_tables; i++) { | |
05f3fb3c | 5260 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
65e19f54 JA |
5261 | unsigned this_files; |
5262 | ||
5263 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); | |
5264 | table->files = kcalloc(this_files, sizeof(struct file *), | |
5265 | GFP_KERNEL); | |
5266 | if (!table->files) | |
5267 | break; | |
5268 | nr_files -= this_files; | |
5269 | } | |
5270 | ||
5271 | if (i == nr_tables) | |
5272 | return 0; | |
5273 | ||
5274 | for (i = 0; i < nr_tables; i++) { | |
05f3fb3c | 5275 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
65e19f54 JA |
5276 | kfree(table->files); |
5277 | } | |
5278 | return 1; | |
5279 | } | |
5280 | ||
05f3fb3c JA |
5281 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file) |
5282 | { | |
5283 | #if defined(CONFIG_UNIX) | |
5284 | struct sock *sock = ctx->ring_sock->sk; | |
5285 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
5286 | struct sk_buff *skb; | |
5287 | int i; | |
5288 | ||
5289 | __skb_queue_head_init(&list); | |
5290 | ||
5291 | /* | |
5292 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
5293 | * remove this entry and rearrange the file array. | |
5294 | */ | |
5295 | skb = skb_dequeue(head); | |
5296 | while (skb) { | |
5297 | struct scm_fp_list *fp; | |
5298 | ||
5299 | fp = UNIXCB(skb).fp; | |
5300 | for (i = 0; i < fp->count; i++) { | |
5301 | int left; | |
5302 | ||
5303 | if (fp->fp[i] != file) | |
5304 | continue; | |
5305 | ||
5306 | unix_notinflight(fp->user, fp->fp[i]); | |
5307 | left = fp->count - 1 - i; | |
5308 | if (left) { | |
5309 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
5310 | left * sizeof(struct file *)); | |
5311 | } | |
5312 | fp->count--; | |
5313 | if (!fp->count) { | |
5314 | kfree_skb(skb); | |
5315 | skb = NULL; | |
5316 | } else { | |
5317 | __skb_queue_tail(&list, skb); | |
5318 | } | |
5319 | fput(file); | |
5320 | file = NULL; | |
5321 | break; | |
5322 | } | |
5323 | ||
5324 | if (!file) | |
5325 | break; | |
5326 | ||
5327 | __skb_queue_tail(&list, skb); | |
5328 | ||
5329 | skb = skb_dequeue(head); | |
5330 | } | |
5331 | ||
5332 | if (skb_peek(&list)) { | |
5333 | spin_lock_irq(&head->lock); | |
5334 | while ((skb = __skb_dequeue(&list)) != NULL) | |
5335 | __skb_queue_tail(head, skb); | |
5336 | spin_unlock_irq(&head->lock); | |
5337 | } | |
5338 | #else | |
5339 | fput(file); | |
5340 | #endif | |
5341 | } | |
5342 | ||
5343 | struct io_file_put { | |
5344 | struct llist_node llist; | |
5345 | struct file *file; | |
5346 | struct completion *done; | |
5347 | }; | |
5348 | ||
5349 | static void io_ring_file_ref_switch(struct work_struct *work) | |
5350 | { | |
5351 | struct io_file_put *pfile, *tmp; | |
5352 | struct fixed_file_data *data; | |
5353 | struct llist_node *node; | |
5354 | ||
5355 | data = container_of(work, struct fixed_file_data, ref_work); | |
5356 | ||
5357 | while ((node = llist_del_all(&data->put_llist)) != NULL) { | |
5358 | llist_for_each_entry_safe(pfile, tmp, node, llist) { | |
5359 | io_ring_file_put(data->ctx, pfile->file); | |
5360 | if (pfile->done) | |
5361 | complete(pfile->done); | |
5362 | else | |
5363 | kfree(pfile); | |
5364 | } | |
5365 | } | |
5366 | ||
5367 | percpu_ref_get(&data->refs); | |
5368 | percpu_ref_switch_to_percpu(&data->refs); | |
5369 | } | |
5370 | ||
5371 | static void io_file_data_ref_zero(struct percpu_ref *ref) | |
5372 | { | |
5373 | struct fixed_file_data *data; | |
5374 | ||
5375 | data = container_of(ref, struct fixed_file_data, refs); | |
5376 | ||
5377 | /* we can't safely switch from inside this context, punt to wq */ | |
5378 | queue_work(system_wq, &data->ref_work); | |
5379 | } | |
5380 | ||
6b06314c JA |
5381 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
5382 | unsigned nr_args) | |
5383 | { | |
5384 | __s32 __user *fds = (__s32 __user *) arg; | |
65e19f54 | 5385 | unsigned nr_tables; |
05f3fb3c | 5386 | struct file *file; |
6b06314c JA |
5387 | int fd, ret = 0; |
5388 | unsigned i; | |
5389 | ||
05f3fb3c | 5390 | if (ctx->file_data) |
6b06314c JA |
5391 | return -EBUSY; |
5392 | if (!nr_args) | |
5393 | return -EINVAL; | |
5394 | if (nr_args > IORING_MAX_FIXED_FILES) | |
5395 | return -EMFILE; | |
5396 | ||
05f3fb3c JA |
5397 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
5398 | if (!ctx->file_data) | |
5399 | return -ENOMEM; | |
5400 | ctx->file_data->ctx = ctx; | |
5401 | init_completion(&ctx->file_data->done); | |
5402 | ||
65e19f54 | 5403 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
05f3fb3c JA |
5404 | ctx->file_data->table = kcalloc(nr_tables, |
5405 | sizeof(struct fixed_file_table), | |
65e19f54 | 5406 | GFP_KERNEL); |
05f3fb3c JA |
5407 | if (!ctx->file_data->table) { |
5408 | kfree(ctx->file_data); | |
5409 | ctx->file_data = NULL; | |
6b06314c | 5410 | return -ENOMEM; |
05f3fb3c JA |
5411 | } |
5412 | ||
5413 | if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero, | |
5414 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { | |
5415 | kfree(ctx->file_data->table); | |
5416 | kfree(ctx->file_data); | |
5417 | ctx->file_data = NULL; | |
5418 | return -ENOMEM; | |
5419 | } | |
5420 | ctx->file_data->put_llist.first = NULL; | |
5421 | INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch); | |
6b06314c | 5422 | |
65e19f54 | 5423 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
05f3fb3c JA |
5424 | percpu_ref_exit(&ctx->file_data->refs); |
5425 | kfree(ctx->file_data->table); | |
5426 | kfree(ctx->file_data); | |
5427 | ctx->file_data = NULL; | |
65e19f54 JA |
5428 | return -ENOMEM; |
5429 | } | |
5430 | ||
08a45173 | 5431 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
65e19f54 JA |
5432 | struct fixed_file_table *table; |
5433 | unsigned index; | |
5434 | ||
6b06314c JA |
5435 | ret = -EFAULT; |
5436 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) | |
5437 | break; | |
08a45173 JA |
5438 | /* allow sparse sets */ |
5439 | if (fd == -1) { | |
5440 | ret = 0; | |
5441 | continue; | |
5442 | } | |
6b06314c | 5443 | |
05f3fb3c | 5444 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
65e19f54 | 5445 | index = i & IORING_FILE_TABLE_MASK; |
05f3fb3c | 5446 | file = fget(fd); |
6b06314c JA |
5447 | |
5448 | ret = -EBADF; | |
05f3fb3c | 5449 | if (!file) |
6b06314c | 5450 | break; |
05f3fb3c | 5451 | |
6b06314c JA |
5452 | /* |
5453 | * Don't allow io_uring instances to be registered. If UNIX | |
5454 | * isn't enabled, then this causes a reference cycle and this | |
5455 | * instance can never get freed. If UNIX is enabled we'll | |
5456 | * handle it just fine, but there's still no point in allowing | |
5457 | * a ring fd as it doesn't support regular read/write anyway. | |
5458 | */ | |
05f3fb3c JA |
5459 | if (file->f_op == &io_uring_fops) { |
5460 | fput(file); | |
6b06314c JA |
5461 | break; |
5462 | } | |
6b06314c | 5463 | ret = 0; |
05f3fb3c | 5464 | table->files[index] = file; |
6b06314c JA |
5465 | } |
5466 | ||
5467 | if (ret) { | |
65e19f54 | 5468 | for (i = 0; i < ctx->nr_user_files; i++) { |
65e19f54 JA |
5469 | file = io_file_from_index(ctx, i); |
5470 | if (file) | |
5471 | fput(file); | |
5472 | } | |
5473 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c | 5474 | kfree(ctx->file_data->table[i].files); |
6b06314c | 5475 | |
05f3fb3c JA |
5476 | kfree(ctx->file_data->table); |
5477 | kfree(ctx->file_data); | |
5478 | ctx->file_data = NULL; | |
6b06314c JA |
5479 | ctx->nr_user_files = 0; |
5480 | return ret; | |
5481 | } | |
5482 | ||
5483 | ret = io_sqe_files_scm(ctx); | |
5484 | if (ret) | |
5485 | io_sqe_files_unregister(ctx); | |
5486 | ||
5487 | return ret; | |
5488 | } | |
5489 | ||
c3a31e60 JA |
5490 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
5491 | int index) | |
5492 | { | |
5493 | #if defined(CONFIG_UNIX) | |
5494 | struct sock *sock = ctx->ring_sock->sk; | |
5495 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
5496 | struct sk_buff *skb; | |
5497 | ||
5498 | /* | |
5499 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
5500 | * file set. If there's no room, fall back to allocating a new skb | |
5501 | * and filling it in. | |
5502 | */ | |
5503 | spin_lock_irq(&head->lock); | |
5504 | skb = skb_peek(head); | |
5505 | if (skb) { | |
5506 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
5507 | ||
5508 | if (fpl->count < SCM_MAX_FD) { | |
5509 | __skb_unlink(skb, head); | |
5510 | spin_unlock_irq(&head->lock); | |
5511 | fpl->fp[fpl->count] = get_file(file); | |
5512 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
5513 | fpl->count++; | |
5514 | spin_lock_irq(&head->lock); | |
5515 | __skb_queue_head(head, skb); | |
5516 | } else { | |
5517 | skb = NULL; | |
5518 | } | |
5519 | } | |
5520 | spin_unlock_irq(&head->lock); | |
5521 | ||
5522 | if (skb) { | |
5523 | fput(file); | |
5524 | return 0; | |
5525 | } | |
5526 | ||
5527 | return __io_sqe_files_scm(ctx, 1, index); | |
5528 | #else | |
5529 | return 0; | |
5530 | #endif | |
5531 | } | |
5532 | ||
05f3fb3c | 5533 | static void io_atomic_switch(struct percpu_ref *ref) |
c3a31e60 | 5534 | { |
05f3fb3c JA |
5535 | struct fixed_file_data *data; |
5536 | ||
5537 | data = container_of(ref, struct fixed_file_data, refs); | |
5538 | clear_bit(FFD_F_ATOMIC, &data->state); | |
5539 | } | |
5540 | ||
5541 | static bool io_queue_file_removal(struct fixed_file_data *data, | |
5542 | struct file *file) | |
5543 | { | |
5544 | struct io_file_put *pfile, pfile_stack; | |
5545 | DECLARE_COMPLETION_ONSTACK(done); | |
5546 | ||
5547 | /* | |
5548 | * If we fail allocating the struct we need for doing async reomval | |
5549 | * of this file, just punt to sync and wait for it. | |
5550 | */ | |
5551 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); | |
5552 | if (!pfile) { | |
5553 | pfile = &pfile_stack; | |
5554 | pfile->done = &done; | |
5555 | } | |
5556 | ||
5557 | pfile->file = file; | |
5558 | llist_add(&pfile->llist, &data->put_llist); | |
5559 | ||
5560 | if (pfile == &pfile_stack) { | |
5561 | if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) { | |
5562 | percpu_ref_put(&data->refs); | |
5563 | percpu_ref_switch_to_atomic(&data->refs, | |
5564 | io_atomic_switch); | |
5565 | } | |
5566 | wait_for_completion(&done); | |
5567 | flush_work(&data->ref_work); | |
5568 | return false; | |
5569 | } | |
5570 | ||
5571 | return true; | |
5572 | } | |
5573 | ||
5574 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, | |
5575 | struct io_uring_files_update *up, | |
5576 | unsigned nr_args) | |
5577 | { | |
5578 | struct fixed_file_data *data = ctx->file_data; | |
5579 | bool ref_switch = false; | |
5580 | struct file *file; | |
c3a31e60 JA |
5581 | __s32 __user *fds; |
5582 | int fd, i, err; | |
5583 | __u32 done; | |
5584 | ||
05f3fb3c | 5585 | if (check_add_overflow(up->offset, nr_args, &done)) |
c3a31e60 JA |
5586 | return -EOVERFLOW; |
5587 | if (done > ctx->nr_user_files) | |
5588 | return -EINVAL; | |
5589 | ||
5590 | done = 0; | |
05f3fb3c | 5591 | fds = u64_to_user_ptr(up->fds); |
c3a31e60 | 5592 | while (nr_args) { |
65e19f54 JA |
5593 | struct fixed_file_table *table; |
5594 | unsigned index; | |
5595 | ||
c3a31e60 JA |
5596 | err = 0; |
5597 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
5598 | err = -EFAULT; | |
5599 | break; | |
5600 | } | |
05f3fb3c JA |
5601 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
5602 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
65e19f54 JA |
5603 | index = i & IORING_FILE_TABLE_MASK; |
5604 | if (table->files[index]) { | |
05f3fb3c | 5605 | file = io_file_from_index(ctx, index); |
65e19f54 | 5606 | table->files[index] = NULL; |
05f3fb3c JA |
5607 | if (io_queue_file_removal(data, file)) |
5608 | ref_switch = true; | |
c3a31e60 JA |
5609 | } |
5610 | if (fd != -1) { | |
c3a31e60 JA |
5611 | file = fget(fd); |
5612 | if (!file) { | |
5613 | err = -EBADF; | |
5614 | break; | |
5615 | } | |
5616 | /* | |
5617 | * Don't allow io_uring instances to be registered. If | |
5618 | * UNIX isn't enabled, then this causes a reference | |
5619 | * cycle and this instance can never get freed. If UNIX | |
5620 | * is enabled we'll handle it just fine, but there's | |
5621 | * still no point in allowing a ring fd as it doesn't | |
5622 | * support regular read/write anyway. | |
5623 | */ | |
5624 | if (file->f_op == &io_uring_fops) { | |
5625 | fput(file); | |
5626 | err = -EBADF; | |
5627 | break; | |
5628 | } | |
65e19f54 | 5629 | table->files[index] = file; |
c3a31e60 JA |
5630 | err = io_sqe_file_register(ctx, file, i); |
5631 | if (err) | |
5632 | break; | |
5633 | } | |
5634 | nr_args--; | |
5635 | done++; | |
05f3fb3c JA |
5636 | up->offset++; |
5637 | } | |
5638 | ||
5639 | if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) { | |
5640 | percpu_ref_put(&data->refs); | |
5641 | percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch); | |
c3a31e60 JA |
5642 | } |
5643 | ||
5644 | return done ? done : err; | |
5645 | } | |
05f3fb3c JA |
5646 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
5647 | unsigned nr_args) | |
5648 | { | |
5649 | struct io_uring_files_update up; | |
5650 | ||
5651 | if (!ctx->file_data) | |
5652 | return -ENXIO; | |
5653 | if (!nr_args) | |
5654 | return -EINVAL; | |
5655 | if (copy_from_user(&up, arg, sizeof(up))) | |
5656 | return -EFAULT; | |
5657 | if (up.resv) | |
5658 | return -EINVAL; | |
5659 | ||
5660 | return __io_sqe_files_update(ctx, &up, nr_args); | |
5661 | } | |
c3a31e60 | 5662 | |
7d723065 JA |
5663 | static void io_put_work(struct io_wq_work *work) |
5664 | { | |
5665 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
5666 | ||
5667 | io_put_req(req); | |
5668 | } | |
5669 | ||
5670 | static void io_get_work(struct io_wq_work *work) | |
5671 | { | |
5672 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
5673 | ||
5674 | refcount_inc(&req->refs); | |
5675 | } | |
5676 | ||
6c271ce2 JA |
5677 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
5678 | struct io_uring_params *p) | |
2b188cc1 | 5679 | { |
576a347b | 5680 | struct io_wq_data data; |
561fb04a | 5681 | unsigned concurrency; |
2b188cc1 JA |
5682 | int ret; |
5683 | ||
6c271ce2 | 5684 | init_waitqueue_head(&ctx->sqo_wait); |
2b188cc1 JA |
5685 | mmgrab(current->mm); |
5686 | ctx->sqo_mm = current->mm; | |
5687 | ||
6c271ce2 | 5688 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
3ec482d1 JA |
5689 | ret = -EPERM; |
5690 | if (!capable(CAP_SYS_ADMIN)) | |
5691 | goto err; | |
5692 | ||
917257da JA |
5693 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
5694 | if (!ctx->sq_thread_idle) | |
5695 | ctx->sq_thread_idle = HZ; | |
5696 | ||
6c271ce2 | 5697 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 5698 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 5699 | |
917257da | 5700 | ret = -EINVAL; |
44a9bd18 JA |
5701 | if (cpu >= nr_cpu_ids) |
5702 | goto err; | |
7889f44d | 5703 | if (!cpu_online(cpu)) |
917257da JA |
5704 | goto err; |
5705 | ||
6c271ce2 JA |
5706 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
5707 | ctx, cpu, | |
5708 | "io_uring-sq"); | |
5709 | } else { | |
5710 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, | |
5711 | "io_uring-sq"); | |
5712 | } | |
5713 | if (IS_ERR(ctx->sqo_thread)) { | |
5714 | ret = PTR_ERR(ctx->sqo_thread); | |
5715 | ctx->sqo_thread = NULL; | |
5716 | goto err; | |
5717 | } | |
5718 | wake_up_process(ctx->sqo_thread); | |
5719 | } else if (p->flags & IORING_SETUP_SQ_AFF) { | |
5720 | /* Can't have SQ_AFF without SQPOLL */ | |
5721 | ret = -EINVAL; | |
5722 | goto err; | |
5723 | } | |
5724 | ||
576a347b JA |
5725 | data.mm = ctx->sqo_mm; |
5726 | data.user = ctx->user; | |
181e448d | 5727 | data.creds = ctx->creds; |
576a347b JA |
5728 | data.get_work = io_get_work; |
5729 | data.put_work = io_put_work; | |
5730 | ||
561fb04a JA |
5731 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
5732 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
576a347b | 5733 | ctx->io_wq = io_wq_create(concurrency, &data); |
975c99a5 JA |
5734 | if (IS_ERR(ctx->io_wq)) { |
5735 | ret = PTR_ERR(ctx->io_wq); | |
5736 | ctx->io_wq = NULL; | |
2b188cc1 JA |
5737 | goto err; |
5738 | } | |
5739 | ||
5740 | return 0; | |
5741 | err: | |
54a91f3b | 5742 | io_finish_async(ctx); |
2b188cc1 JA |
5743 | mmdrop(ctx->sqo_mm); |
5744 | ctx->sqo_mm = NULL; | |
5745 | return ret; | |
5746 | } | |
5747 | ||
5748 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) | |
5749 | { | |
5750 | atomic_long_sub(nr_pages, &user->locked_vm); | |
5751 | } | |
5752 | ||
5753 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) | |
5754 | { | |
5755 | unsigned long page_limit, cur_pages, new_pages; | |
5756 | ||
5757 | /* Don't allow more pages than we can safely lock */ | |
5758 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
5759 | ||
5760 | do { | |
5761 | cur_pages = atomic_long_read(&user->locked_vm); | |
5762 | new_pages = cur_pages + nr_pages; | |
5763 | if (new_pages > page_limit) | |
5764 | return -ENOMEM; | |
5765 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
5766 | new_pages) != cur_pages); | |
5767 | ||
5768 | return 0; | |
5769 | } | |
5770 | ||
5771 | static void io_mem_free(void *ptr) | |
5772 | { | |
52e04ef4 MR |
5773 | struct page *page; |
5774 | ||
5775 | if (!ptr) | |
5776 | return; | |
2b188cc1 | 5777 | |
52e04ef4 | 5778 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
5779 | if (put_page_testzero(page)) |
5780 | free_compound_page(page); | |
5781 | } | |
5782 | ||
5783 | static void *io_mem_alloc(size_t size) | |
5784 | { | |
5785 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
5786 | __GFP_NORETRY; | |
5787 | ||
5788 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
5789 | } | |
5790 | ||
75b28aff HV |
5791 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
5792 | size_t *sq_offset) | |
5793 | { | |
5794 | struct io_rings *rings; | |
5795 | size_t off, sq_array_size; | |
5796 | ||
5797 | off = struct_size(rings, cqes, cq_entries); | |
5798 | if (off == SIZE_MAX) | |
5799 | return SIZE_MAX; | |
5800 | ||
5801 | #ifdef CONFIG_SMP | |
5802 | off = ALIGN(off, SMP_CACHE_BYTES); | |
5803 | if (off == 0) | |
5804 | return SIZE_MAX; | |
5805 | #endif | |
5806 | ||
5807 | sq_array_size = array_size(sizeof(u32), sq_entries); | |
5808 | if (sq_array_size == SIZE_MAX) | |
5809 | return SIZE_MAX; | |
5810 | ||
5811 | if (check_add_overflow(off, sq_array_size, &off)) | |
5812 | return SIZE_MAX; | |
5813 | ||
5814 | if (sq_offset) | |
5815 | *sq_offset = off; | |
5816 | ||
5817 | return off; | |
5818 | } | |
5819 | ||
2b188cc1 JA |
5820 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
5821 | { | |
75b28aff | 5822 | size_t pages; |
2b188cc1 | 5823 | |
75b28aff HV |
5824 | pages = (size_t)1 << get_order( |
5825 | rings_size(sq_entries, cq_entries, NULL)); | |
5826 | pages += (size_t)1 << get_order( | |
5827 | array_size(sizeof(struct io_uring_sqe), sq_entries)); | |
2b188cc1 | 5828 | |
75b28aff | 5829 | return pages; |
2b188cc1 JA |
5830 | } |
5831 | ||
edafccee JA |
5832 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
5833 | { | |
5834 | int i, j; | |
5835 | ||
5836 | if (!ctx->user_bufs) | |
5837 | return -ENXIO; | |
5838 | ||
5839 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
5840 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
5841 | ||
5842 | for (j = 0; j < imu->nr_bvecs; j++) | |
27c4d3a3 | 5843 | put_user_page(imu->bvec[j].bv_page); |
edafccee JA |
5844 | |
5845 | if (ctx->account_mem) | |
5846 | io_unaccount_mem(ctx->user, imu->nr_bvecs); | |
d4ef6475 | 5847 | kvfree(imu->bvec); |
edafccee JA |
5848 | imu->nr_bvecs = 0; |
5849 | } | |
5850 | ||
5851 | kfree(ctx->user_bufs); | |
5852 | ctx->user_bufs = NULL; | |
5853 | ctx->nr_user_bufs = 0; | |
5854 | return 0; | |
5855 | } | |
5856 | ||
5857 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
5858 | void __user *arg, unsigned index) | |
5859 | { | |
5860 | struct iovec __user *src; | |
5861 | ||
5862 | #ifdef CONFIG_COMPAT | |
5863 | if (ctx->compat) { | |
5864 | struct compat_iovec __user *ciovs; | |
5865 | struct compat_iovec ciov; | |
5866 | ||
5867 | ciovs = (struct compat_iovec __user *) arg; | |
5868 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
5869 | return -EFAULT; | |
5870 | ||
d55e5f5b | 5871 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
5872 | dst->iov_len = ciov.iov_len; |
5873 | return 0; | |
5874 | } | |
5875 | #endif | |
5876 | src = (struct iovec __user *) arg; | |
5877 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
5878 | return -EFAULT; | |
5879 | return 0; | |
5880 | } | |
5881 | ||
5882 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, | |
5883 | unsigned nr_args) | |
5884 | { | |
5885 | struct vm_area_struct **vmas = NULL; | |
5886 | struct page **pages = NULL; | |
5887 | int i, j, got_pages = 0; | |
5888 | int ret = -EINVAL; | |
5889 | ||
5890 | if (ctx->user_bufs) | |
5891 | return -EBUSY; | |
5892 | if (!nr_args || nr_args > UIO_MAXIOV) | |
5893 | return -EINVAL; | |
5894 | ||
5895 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
5896 | GFP_KERNEL); | |
5897 | if (!ctx->user_bufs) | |
5898 | return -ENOMEM; | |
5899 | ||
5900 | for (i = 0; i < nr_args; i++) { | |
5901 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
5902 | unsigned long off, start, end, ubuf; | |
5903 | int pret, nr_pages; | |
5904 | struct iovec iov; | |
5905 | size_t size; | |
5906 | ||
5907 | ret = io_copy_iov(ctx, &iov, arg, i); | |
5908 | if (ret) | |
a278682d | 5909 | goto err; |
edafccee JA |
5910 | |
5911 | /* | |
5912 | * Don't impose further limits on the size and buffer | |
5913 | * constraints here, we'll -EINVAL later when IO is | |
5914 | * submitted if they are wrong. | |
5915 | */ | |
5916 | ret = -EFAULT; | |
5917 | if (!iov.iov_base || !iov.iov_len) | |
5918 | goto err; | |
5919 | ||
5920 | /* arbitrary limit, but we need something */ | |
5921 | if (iov.iov_len > SZ_1G) | |
5922 | goto err; | |
5923 | ||
5924 | ubuf = (unsigned long) iov.iov_base; | |
5925 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
5926 | start = ubuf >> PAGE_SHIFT; | |
5927 | nr_pages = end - start; | |
5928 | ||
5929 | if (ctx->account_mem) { | |
5930 | ret = io_account_mem(ctx->user, nr_pages); | |
5931 | if (ret) | |
5932 | goto err; | |
5933 | } | |
5934 | ||
5935 | ret = 0; | |
5936 | if (!pages || nr_pages > got_pages) { | |
5937 | kfree(vmas); | |
5938 | kfree(pages); | |
d4ef6475 | 5939 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
edafccee | 5940 | GFP_KERNEL); |
d4ef6475 | 5941 | vmas = kvmalloc_array(nr_pages, |
edafccee JA |
5942 | sizeof(struct vm_area_struct *), |
5943 | GFP_KERNEL); | |
5944 | if (!pages || !vmas) { | |
5945 | ret = -ENOMEM; | |
5946 | if (ctx->account_mem) | |
5947 | io_unaccount_mem(ctx->user, nr_pages); | |
5948 | goto err; | |
5949 | } | |
5950 | got_pages = nr_pages; | |
5951 | } | |
5952 | ||
d4ef6475 | 5953 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
edafccee JA |
5954 | GFP_KERNEL); |
5955 | ret = -ENOMEM; | |
5956 | if (!imu->bvec) { | |
5957 | if (ctx->account_mem) | |
5958 | io_unaccount_mem(ctx->user, nr_pages); | |
5959 | goto err; | |
5960 | } | |
5961 | ||
5962 | ret = 0; | |
5963 | down_read(¤t->mm->mmap_sem); | |
932f4a63 IW |
5964 | pret = get_user_pages(ubuf, nr_pages, |
5965 | FOLL_WRITE | FOLL_LONGTERM, | |
5966 | pages, vmas); | |
edafccee JA |
5967 | if (pret == nr_pages) { |
5968 | /* don't support file backed memory */ | |
5969 | for (j = 0; j < nr_pages; j++) { | |
5970 | struct vm_area_struct *vma = vmas[j]; | |
5971 | ||
5972 | if (vma->vm_file && | |
5973 | !is_file_hugepages(vma->vm_file)) { | |
5974 | ret = -EOPNOTSUPP; | |
5975 | break; | |
5976 | } | |
5977 | } | |
5978 | } else { | |
5979 | ret = pret < 0 ? pret : -EFAULT; | |
5980 | } | |
5981 | up_read(¤t->mm->mmap_sem); | |
5982 | if (ret) { | |
5983 | /* | |
5984 | * if we did partial map, or found file backed vmas, | |
5985 | * release any pages we did get | |
5986 | */ | |
27c4d3a3 JH |
5987 | if (pret > 0) |
5988 | put_user_pages(pages, pret); | |
edafccee JA |
5989 | if (ctx->account_mem) |
5990 | io_unaccount_mem(ctx->user, nr_pages); | |
d4ef6475 | 5991 | kvfree(imu->bvec); |
edafccee JA |
5992 | goto err; |
5993 | } | |
5994 | ||
5995 | off = ubuf & ~PAGE_MASK; | |
5996 | size = iov.iov_len; | |
5997 | for (j = 0; j < nr_pages; j++) { | |
5998 | size_t vec_len; | |
5999 | ||
6000 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
6001 | imu->bvec[j].bv_page = pages[j]; | |
6002 | imu->bvec[j].bv_len = vec_len; | |
6003 | imu->bvec[j].bv_offset = off; | |
6004 | off = 0; | |
6005 | size -= vec_len; | |
6006 | } | |
6007 | /* store original address for later verification */ | |
6008 | imu->ubuf = ubuf; | |
6009 | imu->len = iov.iov_len; | |
6010 | imu->nr_bvecs = nr_pages; | |
6011 | ||
6012 | ctx->nr_user_bufs++; | |
6013 | } | |
d4ef6475 MR |
6014 | kvfree(pages); |
6015 | kvfree(vmas); | |
edafccee JA |
6016 | return 0; |
6017 | err: | |
d4ef6475 MR |
6018 | kvfree(pages); |
6019 | kvfree(vmas); | |
edafccee JA |
6020 | io_sqe_buffer_unregister(ctx); |
6021 | return ret; | |
6022 | } | |
6023 | ||
9b402849 JA |
6024 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
6025 | { | |
6026 | __s32 __user *fds = arg; | |
6027 | int fd; | |
6028 | ||
6029 | if (ctx->cq_ev_fd) | |
6030 | return -EBUSY; | |
6031 | ||
6032 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
6033 | return -EFAULT; | |
6034 | ||
6035 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
6036 | if (IS_ERR(ctx->cq_ev_fd)) { | |
6037 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
6038 | ctx->cq_ev_fd = NULL; | |
6039 | return ret; | |
6040 | } | |
6041 | ||
6042 | return 0; | |
6043 | } | |
6044 | ||
6045 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
6046 | { | |
6047 | if (ctx->cq_ev_fd) { | |
6048 | eventfd_ctx_put(ctx->cq_ev_fd); | |
6049 | ctx->cq_ev_fd = NULL; | |
6050 | return 0; | |
6051 | } | |
6052 | ||
6053 | return -ENXIO; | |
6054 | } | |
6055 | ||
2b188cc1 JA |
6056 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
6057 | { | |
6b06314c | 6058 | io_finish_async(ctx); |
2b188cc1 JA |
6059 | if (ctx->sqo_mm) |
6060 | mmdrop(ctx->sqo_mm); | |
def596e9 JA |
6061 | |
6062 | io_iopoll_reap_events(ctx); | |
edafccee | 6063 | io_sqe_buffer_unregister(ctx); |
6b06314c | 6064 | io_sqe_files_unregister(ctx); |
9b402849 | 6065 | io_eventfd_unregister(ctx); |
def596e9 | 6066 | |
2b188cc1 | 6067 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
6068 | if (ctx->ring_sock) { |
6069 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 6070 | sock_release(ctx->ring_sock); |
355e8d26 | 6071 | } |
2b188cc1 JA |
6072 | #endif |
6073 | ||
75b28aff | 6074 | io_mem_free(ctx->rings); |
2b188cc1 | 6075 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
6076 | |
6077 | percpu_ref_exit(&ctx->refs); | |
6078 | if (ctx->account_mem) | |
6079 | io_unaccount_mem(ctx->user, | |
6080 | ring_pages(ctx->sq_entries, ctx->cq_entries)); | |
6081 | free_uid(ctx->user); | |
181e448d | 6082 | put_cred(ctx->creds); |
206aefde | 6083 | kfree(ctx->completions); |
78076bb6 | 6084 | kfree(ctx->cancel_hash); |
0ddf92e8 | 6085 | kmem_cache_free(req_cachep, ctx->fallback_req); |
2b188cc1 JA |
6086 | kfree(ctx); |
6087 | } | |
6088 | ||
6089 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
6090 | { | |
6091 | struct io_ring_ctx *ctx = file->private_data; | |
6092 | __poll_t mask = 0; | |
6093 | ||
6094 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
6095 | /* |
6096 | * synchronizes with barrier from wq_has_sleeper call in | |
6097 | * io_commit_cqring | |
6098 | */ | |
2b188cc1 | 6099 | smp_rmb(); |
75b28aff HV |
6100 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
6101 | ctx->rings->sq_ring_entries) | |
2b188cc1 | 6102 | mask |= EPOLLOUT | EPOLLWRNORM; |
daa5de54 | 6103 | if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail) |
2b188cc1 JA |
6104 | mask |= EPOLLIN | EPOLLRDNORM; |
6105 | ||
6106 | return mask; | |
6107 | } | |
6108 | ||
6109 | static int io_uring_fasync(int fd, struct file *file, int on) | |
6110 | { | |
6111 | struct io_ring_ctx *ctx = file->private_data; | |
6112 | ||
6113 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
6114 | } | |
6115 | ||
6116 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) | |
6117 | { | |
6118 | mutex_lock(&ctx->uring_lock); | |
6119 | percpu_ref_kill(&ctx->refs); | |
6120 | mutex_unlock(&ctx->uring_lock); | |
6121 | ||
5262f567 | 6122 | io_kill_timeouts(ctx); |
221c5eb2 | 6123 | io_poll_remove_all(ctx); |
561fb04a JA |
6124 | |
6125 | if (ctx->io_wq) | |
6126 | io_wq_cancel_all(ctx->io_wq); | |
6127 | ||
def596e9 | 6128 | io_iopoll_reap_events(ctx); |
15dff286 JA |
6129 | /* if we failed setting up the ctx, we might not have any rings */ |
6130 | if (ctx->rings) | |
6131 | io_cqring_overflow_flush(ctx, true); | |
206aefde | 6132 | wait_for_completion(&ctx->completions[0]); |
2b188cc1 JA |
6133 | io_ring_ctx_free(ctx); |
6134 | } | |
6135 | ||
6136 | static int io_uring_release(struct inode *inode, struct file *file) | |
6137 | { | |
6138 | struct io_ring_ctx *ctx = file->private_data; | |
6139 | ||
6140 | file->private_data = NULL; | |
6141 | io_ring_ctx_wait_and_kill(ctx); | |
6142 | return 0; | |
6143 | } | |
6144 | ||
fcb323cc JA |
6145 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
6146 | struct files_struct *files) | |
6147 | { | |
6148 | struct io_kiocb *req; | |
6149 | DEFINE_WAIT(wait); | |
6150 | ||
6151 | while (!list_empty_careful(&ctx->inflight_list)) { | |
768134d4 | 6152 | struct io_kiocb *cancel_req = NULL; |
fcb323cc JA |
6153 | |
6154 | spin_lock_irq(&ctx->inflight_lock); | |
6155 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { | |
768134d4 JA |
6156 | if (req->work.files != files) |
6157 | continue; | |
6158 | /* req is being completed, ignore */ | |
6159 | if (!refcount_inc_not_zero(&req->refs)) | |
6160 | continue; | |
6161 | cancel_req = req; | |
6162 | break; | |
fcb323cc | 6163 | } |
768134d4 | 6164 | if (cancel_req) |
fcb323cc | 6165 | prepare_to_wait(&ctx->inflight_wait, &wait, |
768134d4 | 6166 | TASK_UNINTERRUPTIBLE); |
fcb323cc JA |
6167 | spin_unlock_irq(&ctx->inflight_lock); |
6168 | ||
768134d4 JA |
6169 | /* We need to keep going until we don't find a matching req */ |
6170 | if (!cancel_req) | |
fcb323cc | 6171 | break; |
2f6d9b9d BL |
6172 | |
6173 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); | |
6174 | io_put_req(cancel_req); | |
fcb323cc JA |
6175 | schedule(); |
6176 | } | |
768134d4 | 6177 | finish_wait(&ctx->inflight_wait, &wait); |
fcb323cc JA |
6178 | } |
6179 | ||
6180 | static int io_uring_flush(struct file *file, void *data) | |
6181 | { | |
6182 | struct io_ring_ctx *ctx = file->private_data; | |
6183 | ||
6184 | io_uring_cancel_files(ctx, data); | |
1d7bb1d5 JA |
6185 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
6186 | io_cqring_overflow_flush(ctx, true); | |
fcb323cc | 6187 | io_wq_cancel_all(ctx->io_wq); |
1d7bb1d5 | 6188 | } |
fcb323cc JA |
6189 | return 0; |
6190 | } | |
6191 | ||
6c5c240e RP |
6192 | static void *io_uring_validate_mmap_request(struct file *file, |
6193 | loff_t pgoff, size_t sz) | |
2b188cc1 | 6194 | { |
2b188cc1 | 6195 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 6196 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
6197 | struct page *page; |
6198 | void *ptr; | |
6199 | ||
6200 | switch (offset) { | |
6201 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
6202 | case IORING_OFF_CQ_RING: |
6203 | ptr = ctx->rings; | |
2b188cc1 JA |
6204 | break; |
6205 | case IORING_OFF_SQES: | |
6206 | ptr = ctx->sq_sqes; | |
6207 | break; | |
2b188cc1 | 6208 | default: |
6c5c240e | 6209 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
6210 | } |
6211 | ||
6212 | page = virt_to_head_page(ptr); | |
a50b854e | 6213 | if (sz > page_size(page)) |
6c5c240e RP |
6214 | return ERR_PTR(-EINVAL); |
6215 | ||
6216 | return ptr; | |
6217 | } | |
6218 | ||
6219 | #ifdef CONFIG_MMU | |
6220 | ||
6221 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
6222 | { | |
6223 | size_t sz = vma->vm_end - vma->vm_start; | |
6224 | unsigned long pfn; | |
6225 | void *ptr; | |
6226 | ||
6227 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
6228 | if (IS_ERR(ptr)) | |
6229 | return PTR_ERR(ptr); | |
2b188cc1 JA |
6230 | |
6231 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
6232 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
6233 | } | |
6234 | ||
6c5c240e RP |
6235 | #else /* !CONFIG_MMU */ |
6236 | ||
6237 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
6238 | { | |
6239 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
6240 | } | |
6241 | ||
6242 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
6243 | { | |
6244 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
6245 | } | |
6246 | ||
6247 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
6248 | unsigned long addr, unsigned long len, | |
6249 | unsigned long pgoff, unsigned long flags) | |
6250 | { | |
6251 | void *ptr; | |
6252 | ||
6253 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
6254 | if (IS_ERR(ptr)) | |
6255 | return PTR_ERR(ptr); | |
6256 | ||
6257 | return (unsigned long) ptr; | |
6258 | } | |
6259 | ||
6260 | #endif /* !CONFIG_MMU */ | |
6261 | ||
2b188cc1 JA |
6262 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
6263 | u32, min_complete, u32, flags, const sigset_t __user *, sig, | |
6264 | size_t, sigsz) | |
6265 | { | |
6266 | struct io_ring_ctx *ctx; | |
6267 | long ret = -EBADF; | |
6268 | int submitted = 0; | |
6269 | struct fd f; | |
6270 | ||
6c271ce2 | 6271 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
2b188cc1 JA |
6272 | return -EINVAL; |
6273 | ||
6274 | f = fdget(fd); | |
6275 | if (!f.file) | |
6276 | return -EBADF; | |
6277 | ||
6278 | ret = -EOPNOTSUPP; | |
6279 | if (f.file->f_op != &io_uring_fops) | |
6280 | goto out_fput; | |
6281 | ||
6282 | ret = -ENXIO; | |
6283 | ctx = f.file->private_data; | |
6284 | if (!percpu_ref_tryget(&ctx->refs)) | |
6285 | goto out_fput; | |
6286 | ||
6c271ce2 JA |
6287 | /* |
6288 | * For SQ polling, the thread will do all submissions and completions. | |
6289 | * Just return the requested submit count, and wake the thread if | |
6290 | * we were asked to. | |
6291 | */ | |
b2a9eada | 6292 | ret = 0; |
6c271ce2 | 6293 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
c1edbf5f JA |
6294 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
6295 | io_cqring_overflow_flush(ctx, false); | |
6c271ce2 JA |
6296 | if (flags & IORING_ENTER_SQ_WAKEUP) |
6297 | wake_up(&ctx->sqo_wait); | |
6298 | submitted = to_submit; | |
b2a9eada | 6299 | } else if (to_submit) { |
ae9428ca | 6300 | struct mm_struct *cur_mm; |
2b188cc1 | 6301 | |
44d28279 JA |
6302 | if (current->mm != ctx->sqo_mm || |
6303 | current_cred() != ctx->creds) { | |
6304 | ret = -EPERM; | |
6305 | goto out; | |
6306 | } | |
6307 | ||
2b188cc1 | 6308 | mutex_lock(&ctx->uring_lock); |
ae9428ca PB |
6309 | /* already have mm, so io_submit_sqes() won't try to grab it */ |
6310 | cur_mm = ctx->sqo_mm; | |
6311 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd, | |
6312 | &cur_mm, false); | |
2b188cc1 | 6313 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
6314 | |
6315 | if (submitted != to_submit) | |
6316 | goto out; | |
2b188cc1 JA |
6317 | } |
6318 | if (flags & IORING_ENTER_GETEVENTS) { | |
def596e9 JA |
6319 | unsigned nr_events = 0; |
6320 | ||
2b188cc1 JA |
6321 | min_complete = min(min_complete, ctx->cq_entries); |
6322 | ||
def596e9 | 6323 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 | 6324 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
def596e9 JA |
6325 | } else { |
6326 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); | |
6327 | } | |
2b188cc1 JA |
6328 | } |
6329 | ||
7c504e65 | 6330 | out: |
6805b32e | 6331 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
6332 | out_fput: |
6333 | fdput(f); | |
6334 | return submitted ? submitted : ret; | |
6335 | } | |
6336 | ||
6337 | static const struct file_operations io_uring_fops = { | |
6338 | .release = io_uring_release, | |
fcb323cc | 6339 | .flush = io_uring_flush, |
2b188cc1 | 6340 | .mmap = io_uring_mmap, |
6c5c240e RP |
6341 | #ifndef CONFIG_MMU |
6342 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
6343 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
6344 | #endif | |
2b188cc1 JA |
6345 | .poll = io_uring_poll, |
6346 | .fasync = io_uring_fasync, | |
6347 | }; | |
6348 | ||
6349 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
6350 | struct io_uring_params *p) | |
6351 | { | |
75b28aff HV |
6352 | struct io_rings *rings; |
6353 | size_t size, sq_array_offset; | |
2b188cc1 | 6354 | |
75b28aff HV |
6355 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
6356 | if (size == SIZE_MAX) | |
6357 | return -EOVERFLOW; | |
6358 | ||
6359 | rings = io_mem_alloc(size); | |
6360 | if (!rings) | |
2b188cc1 JA |
6361 | return -ENOMEM; |
6362 | ||
75b28aff HV |
6363 | ctx->rings = rings; |
6364 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
6365 | rings->sq_ring_mask = p->sq_entries - 1; | |
6366 | rings->cq_ring_mask = p->cq_entries - 1; | |
6367 | rings->sq_ring_entries = p->sq_entries; | |
6368 | rings->cq_ring_entries = p->cq_entries; | |
6369 | ctx->sq_mask = rings->sq_ring_mask; | |
6370 | ctx->cq_mask = rings->cq_ring_mask; | |
6371 | ctx->sq_entries = rings->sq_ring_entries; | |
6372 | ctx->cq_entries = rings->cq_ring_entries; | |
2b188cc1 JA |
6373 | |
6374 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
6375 | if (size == SIZE_MAX) { |
6376 | io_mem_free(ctx->rings); | |
6377 | ctx->rings = NULL; | |
2b188cc1 | 6378 | return -EOVERFLOW; |
eb065d30 | 6379 | } |
2b188cc1 JA |
6380 | |
6381 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
6382 | if (!ctx->sq_sqes) { |
6383 | io_mem_free(ctx->rings); | |
6384 | ctx->rings = NULL; | |
2b188cc1 | 6385 | return -ENOMEM; |
eb065d30 | 6386 | } |
2b188cc1 | 6387 | |
2b188cc1 JA |
6388 | return 0; |
6389 | } | |
6390 | ||
6391 | /* | |
6392 | * Allocate an anonymous fd, this is what constitutes the application | |
6393 | * visible backing of an io_uring instance. The application mmaps this | |
6394 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
6395 | * we have to tie this fd to a socket for file garbage collection purposes. | |
6396 | */ | |
6397 | static int io_uring_get_fd(struct io_ring_ctx *ctx) | |
6398 | { | |
6399 | struct file *file; | |
6400 | int ret; | |
6401 | ||
6402 | #if defined(CONFIG_UNIX) | |
6403 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, | |
6404 | &ctx->ring_sock); | |
6405 | if (ret) | |
6406 | return ret; | |
6407 | #endif | |
6408 | ||
6409 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
6410 | if (ret < 0) | |
6411 | goto err; | |
6412 | ||
6413 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, | |
6414 | O_RDWR | O_CLOEXEC); | |
6415 | if (IS_ERR(file)) { | |
6416 | put_unused_fd(ret); | |
6417 | ret = PTR_ERR(file); | |
6418 | goto err; | |
6419 | } | |
6420 | ||
6421 | #if defined(CONFIG_UNIX) | |
6422 | ctx->ring_sock->file = file; | |
6423 | #endif | |
6424 | fd_install(ret, file); | |
6425 | return ret; | |
6426 | err: | |
6427 | #if defined(CONFIG_UNIX) | |
6428 | sock_release(ctx->ring_sock); | |
6429 | ctx->ring_sock = NULL; | |
6430 | #endif | |
6431 | return ret; | |
6432 | } | |
6433 | ||
6434 | static int io_uring_create(unsigned entries, struct io_uring_params *p) | |
6435 | { | |
6436 | struct user_struct *user = NULL; | |
6437 | struct io_ring_ctx *ctx; | |
6438 | bool account_mem; | |
6439 | int ret; | |
6440 | ||
8110c1a6 | 6441 | if (!entries) |
2b188cc1 | 6442 | return -EINVAL; |
8110c1a6 JA |
6443 | if (entries > IORING_MAX_ENTRIES) { |
6444 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
6445 | return -EINVAL; | |
6446 | entries = IORING_MAX_ENTRIES; | |
6447 | } | |
2b188cc1 JA |
6448 | |
6449 | /* | |
6450 | * Use twice as many entries for the CQ ring. It's possible for the | |
6451 | * application to drive a higher depth than the size of the SQ ring, | |
6452 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
6453 | * some flexibility in overcommitting a bit. If the application has |
6454 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
6455 | * of CQ ring entries manually. | |
2b188cc1 JA |
6456 | */ |
6457 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
6458 | if (p->flags & IORING_SETUP_CQSIZE) { |
6459 | /* | |
6460 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
6461 | * to a power-of-two, if it isn't already. We do NOT impose | |
6462 | * any cq vs sq ring sizing. | |
6463 | */ | |
8110c1a6 | 6464 | if (p->cq_entries < p->sq_entries) |
33a107f0 | 6465 | return -EINVAL; |
8110c1a6 JA |
6466 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
6467 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
6468 | return -EINVAL; | |
6469 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
6470 | } | |
33a107f0 JA |
6471 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
6472 | } else { | |
6473 | p->cq_entries = 2 * p->sq_entries; | |
6474 | } | |
2b188cc1 JA |
6475 | |
6476 | user = get_uid(current_user()); | |
6477 | account_mem = !capable(CAP_IPC_LOCK); | |
6478 | ||
6479 | if (account_mem) { | |
6480 | ret = io_account_mem(user, | |
6481 | ring_pages(p->sq_entries, p->cq_entries)); | |
6482 | if (ret) { | |
6483 | free_uid(user); | |
6484 | return ret; | |
6485 | } | |
6486 | } | |
6487 | ||
6488 | ctx = io_ring_ctx_alloc(p); | |
6489 | if (!ctx) { | |
6490 | if (account_mem) | |
6491 | io_unaccount_mem(user, ring_pages(p->sq_entries, | |
6492 | p->cq_entries)); | |
6493 | free_uid(user); | |
6494 | return -ENOMEM; | |
6495 | } | |
6496 | ctx->compat = in_compat_syscall(); | |
6497 | ctx->account_mem = account_mem; | |
6498 | ctx->user = user; | |
0b8c0ec7 | 6499 | ctx->creds = get_current_cred(); |
2b188cc1 JA |
6500 | |
6501 | ret = io_allocate_scq_urings(ctx, p); | |
6502 | if (ret) | |
6503 | goto err; | |
6504 | ||
6c271ce2 | 6505 | ret = io_sq_offload_start(ctx, p); |
2b188cc1 JA |
6506 | if (ret) |
6507 | goto err; | |
6508 | ||
2b188cc1 | 6509 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
6510 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
6511 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
6512 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
6513 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
6514 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
6515 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
6516 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
6517 | |
6518 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
6519 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
6520 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
6521 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
6522 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
6523 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
6524 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
ac90f249 | 6525 | |
044c1ab3 JA |
6526 | /* |
6527 | * Install ring fd as the very last thing, so we don't risk someone | |
6528 | * having closed it before we finish setup | |
6529 | */ | |
6530 | ret = io_uring_get_fd(ctx); | |
6531 | if (ret < 0) | |
6532 | goto err; | |
6533 | ||
da8c9690 | 6534 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
ba04291e | 6535 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS; |
c826bd7a | 6536 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
6537 | return ret; |
6538 | err: | |
6539 | io_ring_ctx_wait_and_kill(ctx); | |
6540 | return ret; | |
6541 | } | |
6542 | ||
6543 | /* | |
6544 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
6545 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
6546 | * params structure passed in. | |
6547 | */ | |
6548 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
6549 | { | |
6550 | struct io_uring_params p; | |
6551 | long ret; | |
6552 | int i; | |
6553 | ||
6554 | if (copy_from_user(&p, params, sizeof(p))) | |
6555 | return -EFAULT; | |
6556 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
6557 | if (p.resv[i]) | |
6558 | return -EINVAL; | |
6559 | } | |
6560 | ||
6c271ce2 | 6561 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 JA |
6562 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
6563 | IORING_SETUP_CLAMP)) | |
2b188cc1 JA |
6564 | return -EINVAL; |
6565 | ||
6566 | ret = io_uring_create(entries, &p); | |
6567 | if (ret < 0) | |
6568 | return ret; | |
6569 | ||
6570 | if (copy_to_user(params, &p, sizeof(p))) | |
6571 | return -EFAULT; | |
6572 | ||
6573 | return ret; | |
6574 | } | |
6575 | ||
6576 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
6577 | struct io_uring_params __user *, params) | |
6578 | { | |
6579 | return io_uring_setup(entries, params); | |
6580 | } | |
6581 | ||
66f4af93 JA |
6582 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
6583 | { | |
6584 | struct io_uring_probe *p; | |
6585 | size_t size; | |
6586 | int i, ret; | |
6587 | ||
6588 | size = struct_size(p, ops, nr_args); | |
6589 | if (size == SIZE_MAX) | |
6590 | return -EOVERFLOW; | |
6591 | p = kzalloc(size, GFP_KERNEL); | |
6592 | if (!p) | |
6593 | return -ENOMEM; | |
6594 | ||
6595 | ret = -EFAULT; | |
6596 | if (copy_from_user(p, arg, size)) | |
6597 | goto out; | |
6598 | ret = -EINVAL; | |
6599 | if (memchr_inv(p, 0, size)) | |
6600 | goto out; | |
6601 | ||
6602 | p->last_op = IORING_OP_LAST - 1; | |
6603 | if (nr_args > IORING_OP_LAST) | |
6604 | nr_args = IORING_OP_LAST; | |
6605 | ||
6606 | for (i = 0; i < nr_args; i++) { | |
6607 | p->ops[i].op = i; | |
6608 | if (!io_op_defs[i].not_supported) | |
6609 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
6610 | } | |
6611 | p->ops_len = i; | |
6612 | ||
6613 | ret = 0; | |
6614 | if (copy_to_user(arg, p, size)) | |
6615 | ret = -EFAULT; | |
6616 | out: | |
6617 | kfree(p); | |
6618 | return ret; | |
6619 | } | |
6620 | ||
edafccee JA |
6621 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
6622 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
6623 | __releases(ctx->uring_lock) |
6624 | __acquires(ctx->uring_lock) | |
edafccee JA |
6625 | { |
6626 | int ret; | |
6627 | ||
35fa71a0 JA |
6628 | /* |
6629 | * We're inside the ring mutex, if the ref is already dying, then | |
6630 | * someone else killed the ctx or is already going through | |
6631 | * io_uring_register(). | |
6632 | */ | |
6633 | if (percpu_ref_is_dying(&ctx->refs)) | |
6634 | return -ENXIO; | |
6635 | ||
05f3fb3c | 6636 | if (opcode != IORING_UNREGISTER_FILES && |
66f4af93 JA |
6637 | opcode != IORING_REGISTER_FILES_UPDATE && |
6638 | opcode != IORING_REGISTER_PROBE) { | |
05f3fb3c | 6639 | percpu_ref_kill(&ctx->refs); |
b19062a5 | 6640 | |
05f3fb3c JA |
6641 | /* |
6642 | * Drop uring mutex before waiting for references to exit. If | |
6643 | * another thread is currently inside io_uring_enter() it might | |
6644 | * need to grab the uring_lock to make progress. If we hold it | |
6645 | * here across the drain wait, then we can deadlock. It's safe | |
6646 | * to drop the mutex here, since no new references will come in | |
6647 | * after we've killed the percpu ref. | |
6648 | */ | |
6649 | mutex_unlock(&ctx->uring_lock); | |
c150368b | 6650 | ret = wait_for_completion_interruptible(&ctx->completions[0]); |
05f3fb3c | 6651 | mutex_lock(&ctx->uring_lock); |
c150368b JA |
6652 | if (ret) { |
6653 | percpu_ref_resurrect(&ctx->refs); | |
6654 | ret = -EINTR; | |
6655 | goto out; | |
6656 | } | |
05f3fb3c | 6657 | } |
edafccee JA |
6658 | |
6659 | switch (opcode) { | |
6660 | case IORING_REGISTER_BUFFERS: | |
6661 | ret = io_sqe_buffer_register(ctx, arg, nr_args); | |
6662 | break; | |
6663 | case IORING_UNREGISTER_BUFFERS: | |
6664 | ret = -EINVAL; | |
6665 | if (arg || nr_args) | |
6666 | break; | |
6667 | ret = io_sqe_buffer_unregister(ctx); | |
6668 | break; | |
6b06314c JA |
6669 | case IORING_REGISTER_FILES: |
6670 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
6671 | break; | |
6672 | case IORING_UNREGISTER_FILES: | |
6673 | ret = -EINVAL; | |
6674 | if (arg || nr_args) | |
6675 | break; | |
6676 | ret = io_sqe_files_unregister(ctx); | |
6677 | break; | |
c3a31e60 JA |
6678 | case IORING_REGISTER_FILES_UPDATE: |
6679 | ret = io_sqe_files_update(ctx, arg, nr_args); | |
6680 | break; | |
9b402849 | 6681 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 6682 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
6683 | ret = -EINVAL; |
6684 | if (nr_args != 1) | |
6685 | break; | |
6686 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
6687 | if (ret) |
6688 | break; | |
6689 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
6690 | ctx->eventfd_async = 1; | |
6691 | else | |
6692 | ctx->eventfd_async = 0; | |
9b402849 JA |
6693 | break; |
6694 | case IORING_UNREGISTER_EVENTFD: | |
6695 | ret = -EINVAL; | |
6696 | if (arg || nr_args) | |
6697 | break; | |
6698 | ret = io_eventfd_unregister(ctx); | |
6699 | break; | |
66f4af93 JA |
6700 | case IORING_REGISTER_PROBE: |
6701 | ret = -EINVAL; | |
6702 | if (!arg || nr_args > 256) | |
6703 | break; | |
6704 | ret = io_probe(ctx, arg, nr_args); | |
6705 | break; | |
edafccee JA |
6706 | default: |
6707 | ret = -EINVAL; | |
6708 | break; | |
6709 | } | |
6710 | ||
05f3fb3c JA |
6711 | |
6712 | if (opcode != IORING_UNREGISTER_FILES && | |
66f4af93 JA |
6713 | opcode != IORING_REGISTER_FILES_UPDATE && |
6714 | opcode != IORING_REGISTER_PROBE) { | |
05f3fb3c | 6715 | /* bring the ctx back to life */ |
05f3fb3c | 6716 | percpu_ref_reinit(&ctx->refs); |
c150368b JA |
6717 | out: |
6718 | reinit_completion(&ctx->completions[0]); | |
05f3fb3c | 6719 | } |
edafccee JA |
6720 | return ret; |
6721 | } | |
6722 | ||
6723 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
6724 | void __user *, arg, unsigned int, nr_args) | |
6725 | { | |
6726 | struct io_ring_ctx *ctx; | |
6727 | long ret = -EBADF; | |
6728 | struct fd f; | |
6729 | ||
6730 | f = fdget(fd); | |
6731 | if (!f.file) | |
6732 | return -EBADF; | |
6733 | ||
6734 | ret = -EOPNOTSUPP; | |
6735 | if (f.file->f_op != &io_uring_fops) | |
6736 | goto out_fput; | |
6737 | ||
6738 | ctx = f.file->private_data; | |
6739 | ||
6740 | mutex_lock(&ctx->uring_lock); | |
6741 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
6742 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
6743 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
6744 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
6745 | out_fput: |
6746 | fdput(f); | |
6747 | return ret; | |
6748 | } | |
6749 | ||
2b188cc1 JA |
6750 | static int __init io_uring_init(void) |
6751 | { | |
d3656344 | 6752 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
2b188cc1 JA |
6753 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
6754 | return 0; | |
6755 | }; | |
6756 | __initcall(io_uring_init); |