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