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