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