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