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