]>
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> | |
2b188cc1 | 72 | |
c826bd7a DD |
73 | #define CREATE_TRACE_POINTS |
74 | #include <trace/events/io_uring.h> | |
75 | ||
2b188cc1 JA |
76 | #include <uapi/linux/io_uring.h> |
77 | ||
78 | #include "internal.h" | |
561fb04a | 79 | #include "io-wq.h" |
2b188cc1 | 80 | |
5277deaa | 81 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 82 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
6b06314c | 83 | #define IORING_MAX_FIXED_FILES 1024 |
2b188cc1 JA |
84 | |
85 | struct io_uring { | |
86 | u32 head ____cacheline_aligned_in_smp; | |
87 | u32 tail ____cacheline_aligned_in_smp; | |
88 | }; | |
89 | ||
1e84b97b | 90 | /* |
75b28aff HV |
91 | * This data is shared with the application through the mmap at offsets |
92 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
93 | * |
94 | * The offsets to the member fields are published through struct | |
95 | * io_sqring_offsets when calling io_uring_setup. | |
96 | */ | |
75b28aff | 97 | struct io_rings { |
1e84b97b SB |
98 | /* |
99 | * Head and tail offsets into the ring; the offsets need to be | |
100 | * masked to get valid indices. | |
101 | * | |
75b28aff HV |
102 | * The kernel controls head of the sq ring and the tail of the cq ring, |
103 | * and the application controls tail of the sq ring and the head of the | |
104 | * cq ring. | |
1e84b97b | 105 | */ |
75b28aff | 106 | struct io_uring sq, cq; |
1e84b97b | 107 | /* |
75b28aff | 108 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
109 | * ring_entries - 1) |
110 | */ | |
75b28aff HV |
111 | u32 sq_ring_mask, cq_ring_mask; |
112 | /* Ring sizes (constant, power of 2) */ | |
113 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
114 | /* |
115 | * Number of invalid entries dropped by the kernel due to | |
116 | * invalid index stored in array | |
117 | * | |
118 | * Written by the kernel, shouldn't be modified by the | |
119 | * application (i.e. get number of "new events" by comparing to | |
120 | * cached value). | |
121 | * | |
122 | * After a new SQ head value was read by the application this | |
123 | * counter includes all submissions that were dropped reaching | |
124 | * the new SQ head (and possibly more). | |
125 | */ | |
75b28aff | 126 | u32 sq_dropped; |
1e84b97b SB |
127 | /* |
128 | * Runtime flags | |
129 | * | |
130 | * Written by the kernel, shouldn't be modified by the | |
131 | * application. | |
132 | * | |
133 | * The application needs a full memory barrier before checking | |
134 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
135 | */ | |
75b28aff | 136 | u32 sq_flags; |
1e84b97b SB |
137 | /* |
138 | * Number of completion events lost because the queue was full; | |
139 | * this should be avoided by the application by making sure | |
140 | * there are not more requests pending thatn there is space in | |
141 | * the completion queue. | |
142 | * | |
143 | * Written by the kernel, shouldn't be modified by the | |
144 | * application (i.e. get number of "new events" by comparing to | |
145 | * cached value). | |
146 | * | |
147 | * As completion events come in out of order this counter is not | |
148 | * ordered with any other data. | |
149 | */ | |
75b28aff | 150 | u32 cq_overflow; |
1e84b97b SB |
151 | /* |
152 | * Ring buffer of completion events. | |
153 | * | |
154 | * The kernel writes completion events fresh every time they are | |
155 | * produced, so the application is allowed to modify pending | |
156 | * entries. | |
157 | */ | |
75b28aff | 158 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
159 | }; |
160 | ||
edafccee JA |
161 | struct io_mapped_ubuf { |
162 | u64 ubuf; | |
163 | size_t len; | |
164 | struct bio_vec *bvec; | |
165 | unsigned int nr_bvecs; | |
166 | }; | |
167 | ||
2b188cc1 JA |
168 | struct io_ring_ctx { |
169 | struct { | |
170 | struct percpu_ref refs; | |
171 | } ____cacheline_aligned_in_smp; | |
172 | ||
173 | struct { | |
174 | unsigned int flags; | |
175 | bool compat; | |
176 | bool account_mem; | |
177 | ||
75b28aff HV |
178 | /* |
179 | * Ring buffer of indices into array of io_uring_sqe, which is | |
180 | * mmapped by the application using the IORING_OFF_SQES offset. | |
181 | * | |
182 | * This indirection could e.g. be used to assign fixed | |
183 | * io_uring_sqe entries to operations and only submit them to | |
184 | * the queue when needed. | |
185 | * | |
186 | * The kernel modifies neither the indices array nor the entries | |
187 | * array. | |
188 | */ | |
189 | u32 *sq_array; | |
2b188cc1 JA |
190 | unsigned cached_sq_head; |
191 | unsigned sq_entries; | |
192 | unsigned sq_mask; | |
6c271ce2 | 193 | unsigned sq_thread_idle; |
498ccd9e | 194 | unsigned cached_sq_dropped; |
2b188cc1 | 195 | struct io_uring_sqe *sq_sqes; |
de0617e4 JA |
196 | |
197 | struct list_head defer_list; | |
5262f567 | 198 | struct list_head timeout_list; |
2b188cc1 JA |
199 | } ____cacheline_aligned_in_smp; |
200 | ||
201 | /* IO offload */ | |
561fb04a | 202 | struct io_wq *io_wq; |
6c271ce2 | 203 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
2b188cc1 | 204 | struct mm_struct *sqo_mm; |
6c271ce2 | 205 | wait_queue_head_t sqo_wait; |
a4c0b3de | 206 | struct completion sqo_thread_started; |
2b188cc1 JA |
207 | |
208 | struct { | |
2b188cc1 | 209 | unsigned cached_cq_tail; |
498ccd9e | 210 | atomic_t cached_cq_overflow; |
2b188cc1 JA |
211 | unsigned cq_entries; |
212 | unsigned cq_mask; | |
213 | struct wait_queue_head cq_wait; | |
214 | struct fasync_struct *cq_fasync; | |
9b402849 | 215 | struct eventfd_ctx *cq_ev_fd; |
5262f567 | 216 | atomic_t cq_timeouts; |
2b188cc1 JA |
217 | } ____cacheline_aligned_in_smp; |
218 | ||
75b28aff HV |
219 | struct io_rings *rings; |
220 | ||
6b06314c JA |
221 | /* |
222 | * If used, fixed file set. Writers must ensure that ->refs is dead, | |
223 | * readers must ensure that ->refs is alive as long as the file* is | |
224 | * used. Only updated through io_uring_register(2). | |
225 | */ | |
226 | struct file **user_files; | |
227 | unsigned nr_user_files; | |
228 | ||
edafccee JA |
229 | /* if used, fixed mapped user buffers */ |
230 | unsigned nr_user_bufs; | |
231 | struct io_mapped_ubuf *user_bufs; | |
232 | ||
2b188cc1 JA |
233 | struct user_struct *user; |
234 | ||
235 | struct completion ctx_done; | |
236 | ||
237 | struct { | |
238 | struct mutex uring_lock; | |
239 | wait_queue_head_t wait; | |
240 | } ____cacheline_aligned_in_smp; | |
241 | ||
242 | struct { | |
243 | spinlock_t completion_lock; | |
def596e9 JA |
244 | bool poll_multi_file; |
245 | /* | |
246 | * ->poll_list is protected by the ctx->uring_lock for | |
247 | * io_uring instances that don't use IORING_SETUP_SQPOLL. | |
248 | * For SQPOLL, only the single threaded io_sq_thread() will | |
249 | * manipulate the list, hence no extra locking is needed there. | |
250 | */ | |
251 | struct list_head poll_list; | |
221c5eb2 | 252 | struct list_head cancel_list; |
2b188cc1 JA |
253 | } ____cacheline_aligned_in_smp; |
254 | ||
255 | #if defined(CONFIG_UNIX) | |
256 | struct socket *ring_sock; | |
257 | #endif | |
258 | }; | |
259 | ||
260 | struct sqe_submit { | |
261 | const struct io_uring_sqe *sqe; | |
8776f3fa | 262 | u32 sequence; |
2b188cc1 | 263 | bool has_user; |
ba5290cc | 264 | bool in_async; |
6c271ce2 | 265 | bool needs_fixed_file; |
2b188cc1 JA |
266 | }; |
267 | ||
09bb8394 JA |
268 | /* |
269 | * First field must be the file pointer in all the | |
270 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
271 | */ | |
221c5eb2 JA |
272 | struct io_poll_iocb { |
273 | struct file *file; | |
274 | struct wait_queue_head *head; | |
275 | __poll_t events; | |
8c838788 | 276 | bool done; |
221c5eb2 JA |
277 | bool canceled; |
278 | struct wait_queue_entry wait; | |
279 | }; | |
280 | ||
5262f567 JA |
281 | struct io_timeout { |
282 | struct file *file; | |
283 | struct hrtimer timer; | |
284 | }; | |
285 | ||
09bb8394 JA |
286 | /* |
287 | * NOTE! Each of the iocb union members has the file pointer | |
288 | * as the first entry in their struct definition. So you can | |
289 | * access the file pointer through any of the sub-structs, | |
290 | * or directly as just 'ki_filp' in this struct. | |
291 | */ | |
2b188cc1 | 292 | struct io_kiocb { |
221c5eb2 | 293 | union { |
09bb8394 | 294 | struct file *file; |
221c5eb2 JA |
295 | struct kiocb rw; |
296 | struct io_poll_iocb poll; | |
5262f567 | 297 | struct io_timeout timeout; |
221c5eb2 | 298 | }; |
2b188cc1 JA |
299 | |
300 | struct sqe_submit submit; | |
301 | ||
302 | struct io_ring_ctx *ctx; | |
303 | struct list_head list; | |
9e645e11 | 304 | struct list_head link_list; |
2b188cc1 | 305 | unsigned int flags; |
c16361c1 | 306 | refcount_t refs; |
8449eeda | 307 | #define REQ_F_NOWAIT 1 /* must not punt to workers */ |
def596e9 | 308 | #define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */ |
6b06314c | 309 | #define REQ_F_FIXED_FILE 4 /* ctx owns file */ |
31b51510 | 310 | #define REQ_F_SEQ_PREV 8 /* sequential with previous */ |
e2033e33 SB |
311 | #define REQ_F_IO_DRAIN 16 /* drain existing IO first */ |
312 | #define REQ_F_IO_DRAINED 32 /* drain done */ | |
9e645e11 | 313 | #define REQ_F_LINK 64 /* linked sqes */ |
f7b76ac9 ZL |
314 | #define REQ_F_LINK_DONE 128 /* linked sqes done */ |
315 | #define REQ_F_FAIL_LINK 256 /* fail rest of links */ | |
4fe2c963 | 316 | #define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */ |
5262f567 | 317 | #define REQ_F_TIMEOUT 1024 /* timeout request */ |
491381ce JA |
318 | #define REQ_F_ISREG 2048 /* regular file */ |
319 | #define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */ | |
2b188cc1 | 320 | u64 user_data; |
9e645e11 | 321 | u32 result; |
de0617e4 | 322 | u32 sequence; |
2b188cc1 | 323 | |
561fb04a | 324 | struct io_wq_work work; |
2b188cc1 JA |
325 | }; |
326 | ||
327 | #define IO_PLUG_THRESHOLD 2 | |
def596e9 | 328 | #define IO_IOPOLL_BATCH 8 |
2b188cc1 | 329 | |
9a56a232 JA |
330 | struct io_submit_state { |
331 | struct blk_plug plug; | |
332 | ||
2579f913 JA |
333 | /* |
334 | * io_kiocb alloc cache | |
335 | */ | |
336 | void *reqs[IO_IOPOLL_BATCH]; | |
337 | unsigned int free_reqs; | |
338 | unsigned int cur_req; | |
339 | ||
9a56a232 JA |
340 | /* |
341 | * File reference cache | |
342 | */ | |
343 | struct file *file; | |
344 | unsigned int fd; | |
345 | unsigned int has_refs; | |
346 | unsigned int used_refs; | |
347 | unsigned int ios_left; | |
348 | }; | |
349 | ||
561fb04a | 350 | static void io_wq_submit_work(struct io_wq_work **workptr); |
5262f567 JA |
351 | static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data, |
352 | long res); | |
4fe2c963 | 353 | static void __io_free_req(struct io_kiocb *req); |
de0617e4 | 354 | |
2b188cc1 JA |
355 | static struct kmem_cache *req_cachep; |
356 | ||
357 | static const struct file_operations io_uring_fops; | |
358 | ||
359 | struct sock *io_uring_get_socket(struct file *file) | |
360 | { | |
361 | #if defined(CONFIG_UNIX) | |
362 | if (file->f_op == &io_uring_fops) { | |
363 | struct io_ring_ctx *ctx = file->private_data; | |
364 | ||
365 | return ctx->ring_sock->sk; | |
366 | } | |
367 | #endif | |
368 | return NULL; | |
369 | } | |
370 | EXPORT_SYMBOL(io_uring_get_socket); | |
371 | ||
372 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) | |
373 | { | |
374 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
375 | ||
376 | complete(&ctx->ctx_done); | |
377 | } | |
378 | ||
379 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) | |
380 | { | |
381 | struct io_ring_ctx *ctx; | |
382 | ||
383 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
384 | if (!ctx) | |
385 | return NULL; | |
386 | ||
21482896 RG |
387 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
388 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { | |
2b188cc1 JA |
389 | kfree(ctx); |
390 | return NULL; | |
391 | } | |
392 | ||
393 | ctx->flags = p->flags; | |
394 | init_waitqueue_head(&ctx->cq_wait); | |
395 | init_completion(&ctx->ctx_done); | |
a4c0b3de | 396 | init_completion(&ctx->sqo_thread_started); |
2b188cc1 JA |
397 | mutex_init(&ctx->uring_lock); |
398 | init_waitqueue_head(&ctx->wait); | |
399 | spin_lock_init(&ctx->completion_lock); | |
def596e9 | 400 | INIT_LIST_HEAD(&ctx->poll_list); |
221c5eb2 | 401 | INIT_LIST_HEAD(&ctx->cancel_list); |
de0617e4 | 402 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 403 | INIT_LIST_HEAD(&ctx->timeout_list); |
2b188cc1 JA |
404 | return ctx; |
405 | } | |
406 | ||
7adf4eaf JA |
407 | static inline bool __io_sequence_defer(struct io_ring_ctx *ctx, |
408 | struct io_kiocb *req) | |
409 | { | |
498ccd9e JA |
410 | return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped |
411 | + atomic_read(&ctx->cached_cq_overflow); | |
7adf4eaf JA |
412 | } |
413 | ||
de0617e4 JA |
414 | static inline bool io_sequence_defer(struct io_ring_ctx *ctx, |
415 | struct io_kiocb *req) | |
416 | { | |
7adf4eaf | 417 | if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN) |
de0617e4 JA |
418 | return false; |
419 | ||
7adf4eaf | 420 | return __io_sequence_defer(ctx, req); |
de0617e4 JA |
421 | } |
422 | ||
7adf4eaf | 423 | static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx) |
de0617e4 JA |
424 | { |
425 | struct io_kiocb *req; | |
426 | ||
7adf4eaf JA |
427 | req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list); |
428 | if (req && !io_sequence_defer(ctx, req)) { | |
de0617e4 JA |
429 | list_del_init(&req->list); |
430 | return req; | |
431 | } | |
432 | ||
433 | return NULL; | |
434 | } | |
435 | ||
5262f567 JA |
436 | static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx) |
437 | { | |
7adf4eaf JA |
438 | struct io_kiocb *req; |
439 | ||
440 | req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list); | |
441 | if (req && !__io_sequence_defer(ctx, req)) { | |
442 | list_del_init(&req->list); | |
443 | return req; | |
444 | } | |
445 | ||
446 | return NULL; | |
5262f567 JA |
447 | } |
448 | ||
de0617e4 | 449 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
2b188cc1 | 450 | { |
75b28aff | 451 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 452 | |
75b28aff | 453 | if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) { |
2b188cc1 | 454 | /* order cqe stores with ring update */ |
75b28aff | 455 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
2b188cc1 | 456 | |
2b188cc1 JA |
457 | if (wq_has_sleeper(&ctx->cq_wait)) { |
458 | wake_up_interruptible(&ctx->cq_wait); | |
459 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
460 | } | |
461 | } | |
462 | } | |
463 | ||
561fb04a | 464 | static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe) |
18d9be1a | 465 | { |
561fb04a JA |
466 | u8 opcode = READ_ONCE(sqe->opcode); |
467 | ||
468 | return !(opcode == IORING_OP_READ_FIXED || | |
469 | opcode == IORING_OP_WRITE_FIXED); | |
470 | } | |
471 | ||
472 | static inline bool io_prep_async_work(struct io_kiocb *req) | |
473 | { | |
474 | bool do_hashed = false; | |
54a91f3b | 475 | |
6cc47d1d JA |
476 | if (req->submit.sqe) { |
477 | switch (req->submit.sqe->opcode) { | |
478 | case IORING_OP_WRITEV: | |
479 | case IORING_OP_WRITE_FIXED: | |
561fb04a | 480 | do_hashed = true; |
6cc47d1d JA |
481 | break; |
482 | } | |
561fb04a JA |
483 | if (io_sqe_needs_user(req->submit.sqe)) |
484 | req->work.flags |= IO_WQ_WORK_NEEDS_USER; | |
54a91f3b JA |
485 | } |
486 | ||
561fb04a JA |
487 | return do_hashed; |
488 | } | |
489 | ||
490 | static inline void io_queue_async_work(struct io_ring_ctx *ctx, | |
491 | struct io_kiocb *req) | |
492 | { | |
493 | bool do_hashed = io_prep_async_work(req); | |
494 | ||
495 | trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work, | |
496 | req->flags); | |
497 | if (!do_hashed) { | |
498 | io_wq_enqueue(ctx->io_wq, &req->work); | |
499 | } else { | |
500 | io_wq_enqueue_hashed(ctx->io_wq, &req->work, | |
501 | file_inode(req->file)); | |
502 | } | |
18d9be1a JA |
503 | } |
504 | ||
5262f567 JA |
505 | static void io_kill_timeout(struct io_kiocb *req) |
506 | { | |
507 | int ret; | |
508 | ||
509 | ret = hrtimer_try_to_cancel(&req->timeout.timer); | |
510 | if (ret != -1) { | |
511 | atomic_inc(&req->ctx->cq_timeouts); | |
512 | list_del(&req->list); | |
513 | io_cqring_fill_event(req->ctx, req->user_data, 0); | |
514 | __io_free_req(req); | |
515 | } | |
516 | } | |
517 | ||
518 | static void io_kill_timeouts(struct io_ring_ctx *ctx) | |
519 | { | |
520 | struct io_kiocb *req, *tmp; | |
521 | ||
522 | spin_lock_irq(&ctx->completion_lock); | |
523 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) | |
524 | io_kill_timeout(req); | |
525 | spin_unlock_irq(&ctx->completion_lock); | |
526 | } | |
527 | ||
de0617e4 JA |
528 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
529 | { | |
530 | struct io_kiocb *req; | |
531 | ||
5262f567 JA |
532 | while ((req = io_get_timeout_req(ctx)) != NULL) |
533 | io_kill_timeout(req); | |
534 | ||
de0617e4 JA |
535 | __io_commit_cqring(ctx); |
536 | ||
537 | while ((req = io_get_deferred_req(ctx)) != NULL) { | |
4fe2c963 JL |
538 | if (req->flags & REQ_F_SHADOW_DRAIN) { |
539 | /* Just for drain, free it. */ | |
540 | __io_free_req(req); | |
541 | continue; | |
542 | } | |
de0617e4 | 543 | req->flags |= REQ_F_IO_DRAINED; |
18d9be1a | 544 | io_queue_async_work(ctx, req); |
de0617e4 JA |
545 | } |
546 | } | |
547 | ||
2b188cc1 JA |
548 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
549 | { | |
75b28aff | 550 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
551 | unsigned tail; |
552 | ||
553 | tail = ctx->cached_cq_tail; | |
115e12e5 SB |
554 | /* |
555 | * writes to the cq entry need to come after reading head; the | |
556 | * control dependency is enough as we're using WRITE_ONCE to | |
557 | * fill the cq entry | |
558 | */ | |
75b28aff | 559 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
2b188cc1 JA |
560 | return NULL; |
561 | ||
562 | ctx->cached_cq_tail++; | |
75b28aff | 563 | return &rings->cqes[tail & ctx->cq_mask]; |
2b188cc1 JA |
564 | } |
565 | ||
566 | static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data, | |
c71ffb67 | 567 | long res) |
2b188cc1 JA |
568 | { |
569 | struct io_uring_cqe *cqe; | |
570 | ||
571 | /* | |
572 | * If we can't get a cq entry, userspace overflowed the | |
573 | * submission (by quite a lot). Increment the overflow count in | |
574 | * the ring. | |
575 | */ | |
576 | cqe = io_get_cqring(ctx); | |
577 | if (cqe) { | |
578 | WRITE_ONCE(cqe->user_data, ki_user_data); | |
579 | WRITE_ONCE(cqe->res, res); | |
c71ffb67 | 580 | WRITE_ONCE(cqe->flags, 0); |
2b188cc1 | 581 | } else { |
498ccd9e JA |
582 | WRITE_ONCE(ctx->rings->cq_overflow, |
583 | atomic_inc_return(&ctx->cached_cq_overflow)); | |
2b188cc1 JA |
584 | } |
585 | } | |
586 | ||
8c838788 JA |
587 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
588 | { | |
589 | if (waitqueue_active(&ctx->wait)) | |
590 | wake_up(&ctx->wait); | |
591 | if (waitqueue_active(&ctx->sqo_wait)) | |
592 | wake_up(&ctx->sqo_wait); | |
9b402849 JA |
593 | if (ctx->cq_ev_fd) |
594 | eventfd_signal(ctx->cq_ev_fd, 1); | |
8c838788 JA |
595 | } |
596 | ||
597 | static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data, | |
c71ffb67 | 598 | long res) |
2b188cc1 JA |
599 | { |
600 | unsigned long flags; | |
601 | ||
602 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
c71ffb67 | 603 | io_cqring_fill_event(ctx, user_data, res); |
2b188cc1 JA |
604 | io_commit_cqring(ctx); |
605 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
606 | ||
8c838788 | 607 | io_cqring_ev_posted(ctx); |
2b188cc1 JA |
608 | } |
609 | ||
2579f913 JA |
610 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
611 | struct io_submit_state *state) | |
2b188cc1 | 612 | { |
fd6fab2c | 613 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
2b188cc1 JA |
614 | struct io_kiocb *req; |
615 | ||
616 | if (!percpu_ref_tryget(&ctx->refs)) | |
617 | return NULL; | |
618 | ||
2579f913 | 619 | if (!state) { |
fd6fab2c | 620 | req = kmem_cache_alloc(req_cachep, gfp); |
2579f913 JA |
621 | if (unlikely(!req)) |
622 | goto out; | |
623 | } else if (!state->free_reqs) { | |
624 | size_t sz; | |
625 | int ret; | |
626 | ||
627 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); | |
fd6fab2c JA |
628 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
629 | ||
630 | /* | |
631 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
632 | * retry single alloc to be on the safe side. | |
633 | */ | |
634 | if (unlikely(ret <= 0)) { | |
635 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
636 | if (!state->reqs[0]) | |
637 | goto out; | |
638 | ret = 1; | |
639 | } | |
2579f913 JA |
640 | state->free_reqs = ret - 1; |
641 | state->cur_req = 1; | |
642 | req = state->reqs[0]; | |
643 | } else { | |
644 | req = state->reqs[state->cur_req]; | |
645 | state->free_reqs--; | |
646 | state->cur_req++; | |
2b188cc1 JA |
647 | } |
648 | ||
60c112b0 | 649 | req->file = NULL; |
2579f913 JA |
650 | req->ctx = ctx; |
651 | req->flags = 0; | |
e65ef56d JA |
652 | /* one is dropped after submission, the other at completion */ |
653 | refcount_set(&req->refs, 2); | |
9e645e11 | 654 | req->result = 0; |
561fb04a | 655 | INIT_IO_WORK(&req->work, io_wq_submit_work); |
2579f913 JA |
656 | return req; |
657 | out: | |
6805b32e | 658 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
659 | return NULL; |
660 | } | |
661 | ||
def596e9 JA |
662 | static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr) |
663 | { | |
664 | if (*nr) { | |
665 | kmem_cache_free_bulk(req_cachep, *nr, reqs); | |
6805b32e | 666 | percpu_ref_put_many(&ctx->refs, *nr); |
def596e9 JA |
667 | *nr = 0; |
668 | } | |
669 | } | |
670 | ||
9e645e11 | 671 | static void __io_free_req(struct io_kiocb *req) |
2b188cc1 | 672 | { |
09bb8394 JA |
673 | if (req->file && !(req->flags & REQ_F_FIXED_FILE)) |
674 | fput(req->file); | |
6805b32e | 675 | percpu_ref_put(&req->ctx->refs); |
e65ef56d JA |
676 | kmem_cache_free(req_cachep, req); |
677 | } | |
678 | ||
ba816ad6 | 679 | static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr) |
9e645e11 JA |
680 | { |
681 | struct io_kiocb *nxt; | |
682 | ||
683 | /* | |
684 | * The list should never be empty when we are called here. But could | |
685 | * potentially happen if the chain is messed up, check to be on the | |
686 | * safe side. | |
687 | */ | |
688 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list); | |
689 | if (nxt) { | |
690 | list_del(&nxt->list); | |
691 | if (!list_empty(&req->link_list)) { | |
692 | INIT_LIST_HEAD(&nxt->link_list); | |
693 | list_splice(&req->link_list, &nxt->link_list); | |
694 | nxt->flags |= REQ_F_LINK; | |
695 | } | |
696 | ||
f7b76ac9 | 697 | nxt->flags |= REQ_F_LINK_DONE; |
ba816ad6 JA |
698 | /* |
699 | * If we're in async work, we can continue processing the chain | |
700 | * in this context instead of having to queue up new async work. | |
701 | */ | |
561fb04a | 702 | if (nxtptr && current_work()) |
ba816ad6 | 703 | *nxtptr = nxt; |
561fb04a | 704 | else |
ba816ad6 | 705 | io_queue_async_work(req->ctx, nxt); |
9e645e11 JA |
706 | } |
707 | } | |
708 | ||
709 | /* | |
710 | * Called if REQ_F_LINK is set, and we fail the head request | |
711 | */ | |
712 | static void io_fail_links(struct io_kiocb *req) | |
713 | { | |
714 | struct io_kiocb *link; | |
715 | ||
716 | while (!list_empty(&req->link_list)) { | |
717 | link = list_first_entry(&req->link_list, struct io_kiocb, list); | |
718 | list_del(&link->list); | |
719 | ||
c826bd7a | 720 | trace_io_uring_fail_link(req, link); |
9e645e11 JA |
721 | io_cqring_add_event(req->ctx, link->user_data, -ECANCELED); |
722 | __io_free_req(link); | |
723 | } | |
724 | } | |
725 | ||
ba816ad6 | 726 | static void io_free_req(struct io_kiocb *req, struct io_kiocb **nxt) |
9e645e11 JA |
727 | { |
728 | /* | |
729 | * If LINK is set, we have dependent requests in this chain. If we | |
730 | * didn't fail this request, queue the first one up, moving any other | |
731 | * dependencies to the next request. In case of failure, fail the rest | |
732 | * of the chain. | |
733 | */ | |
734 | if (req->flags & REQ_F_LINK) { | |
735 | if (req->flags & REQ_F_FAIL_LINK) | |
736 | io_fail_links(req); | |
737 | else | |
ba816ad6 | 738 | io_req_link_next(req, nxt); |
9e645e11 JA |
739 | } |
740 | ||
741 | __io_free_req(req); | |
742 | } | |
743 | ||
ba816ad6 JA |
744 | /* |
745 | * Drop reference to request, return next in chain (if there is one) if this | |
746 | * was the last reference to this request. | |
747 | */ | |
748 | static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) | |
e65ef56d | 749 | { |
ba816ad6 JA |
750 | struct io_kiocb *nxt = NULL; |
751 | ||
e65ef56d | 752 | if (refcount_dec_and_test(&req->refs)) |
ba816ad6 JA |
753 | io_free_req(req, &nxt); |
754 | ||
755 | return nxt; | |
756 | } | |
757 | ||
758 | static void io_put_req(struct io_kiocb *req, struct io_kiocb **nxtptr) | |
759 | { | |
760 | struct io_kiocb *nxt; | |
761 | ||
762 | nxt = io_put_req_find_next(req); | |
763 | if (nxt) { | |
561fb04a | 764 | if (nxtptr) |
ba816ad6 | 765 | *nxtptr = nxt; |
561fb04a | 766 | else |
ba816ad6 | 767 | io_queue_async_work(nxt->ctx, nxt); |
ba816ad6 | 768 | } |
2b188cc1 JA |
769 | } |
770 | ||
75b28aff | 771 | static unsigned io_cqring_events(struct io_rings *rings) |
a3a0e43f JA |
772 | { |
773 | /* See comment at the top of this file */ | |
774 | smp_rmb(); | |
75b28aff | 775 | return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head); |
a3a0e43f JA |
776 | } |
777 | ||
fb5ccc98 PB |
778 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
779 | { | |
780 | struct io_rings *rings = ctx->rings; | |
781 | ||
782 | /* make sure SQ entry isn't read before tail */ | |
783 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
784 | } | |
785 | ||
def596e9 JA |
786 | /* |
787 | * Find and free completed poll iocbs | |
788 | */ | |
789 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
790 | struct list_head *done) | |
791 | { | |
792 | void *reqs[IO_IOPOLL_BATCH]; | |
793 | struct io_kiocb *req; | |
09bb8394 | 794 | int to_free; |
def596e9 | 795 | |
09bb8394 | 796 | to_free = 0; |
def596e9 JA |
797 | while (!list_empty(done)) { |
798 | req = list_first_entry(done, struct io_kiocb, list); | |
799 | list_del(&req->list); | |
800 | ||
9e645e11 | 801 | io_cqring_fill_event(ctx, req->user_data, req->result); |
def596e9 JA |
802 | (*nr_events)++; |
803 | ||
09bb8394 JA |
804 | if (refcount_dec_and_test(&req->refs)) { |
805 | /* If we're not using fixed files, we have to pair the | |
806 | * completion part with the file put. Use regular | |
807 | * completions for those, only batch free for fixed | |
9e645e11 | 808 | * file and non-linked commands. |
09bb8394 | 809 | */ |
9e645e11 JA |
810 | if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) == |
811 | REQ_F_FIXED_FILE) { | |
09bb8394 JA |
812 | reqs[to_free++] = req; |
813 | if (to_free == ARRAY_SIZE(reqs)) | |
814 | io_free_req_many(ctx, reqs, &to_free); | |
6b06314c | 815 | } else { |
ba816ad6 | 816 | io_free_req(req, NULL); |
6b06314c | 817 | } |
9a56a232 | 818 | } |
def596e9 | 819 | } |
def596e9 | 820 | |
09bb8394 | 821 | io_commit_cqring(ctx); |
def596e9 JA |
822 | io_free_req_many(ctx, reqs, &to_free); |
823 | } | |
824 | ||
825 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
826 | long min) | |
827 | { | |
828 | struct io_kiocb *req, *tmp; | |
829 | LIST_HEAD(done); | |
830 | bool spin; | |
831 | int ret; | |
832 | ||
833 | /* | |
834 | * Only spin for completions if we don't have multiple devices hanging | |
835 | * off our complete list, and we're under the requested amount. | |
836 | */ | |
837 | spin = !ctx->poll_multi_file && *nr_events < min; | |
838 | ||
839 | ret = 0; | |
840 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { | |
841 | struct kiocb *kiocb = &req->rw; | |
842 | ||
843 | /* | |
844 | * Move completed entries to our local list. If we find a | |
845 | * request that requires polling, break out and complete | |
846 | * the done list first, if we have entries there. | |
847 | */ | |
848 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { | |
849 | list_move_tail(&req->list, &done); | |
850 | continue; | |
851 | } | |
852 | if (!list_empty(&done)) | |
853 | break; | |
854 | ||
855 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
856 | if (ret < 0) | |
857 | break; | |
858 | ||
859 | if (ret && spin) | |
860 | spin = false; | |
861 | ret = 0; | |
862 | } | |
863 | ||
864 | if (!list_empty(&done)) | |
865 | io_iopoll_complete(ctx, nr_events, &done); | |
866 | ||
867 | return ret; | |
868 | } | |
869 | ||
870 | /* | |
871 | * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a | |
872 | * non-spinning poll check - we'll still enter the driver poll loop, but only | |
873 | * as a non-spinning completion check. | |
874 | */ | |
875 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
876 | long min) | |
877 | { | |
08f5439f | 878 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
def596e9 JA |
879 | int ret; |
880 | ||
881 | ret = io_do_iopoll(ctx, nr_events, min); | |
882 | if (ret < 0) | |
883 | return ret; | |
884 | if (!min || *nr_events >= min) | |
885 | return 0; | |
886 | } | |
887 | ||
888 | return 1; | |
889 | } | |
890 | ||
891 | /* | |
892 | * We can't just wait for polled events to come to us, we have to actively | |
893 | * find and complete them. | |
894 | */ | |
895 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) | |
896 | { | |
897 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
898 | return; | |
899 | ||
900 | mutex_lock(&ctx->uring_lock); | |
901 | while (!list_empty(&ctx->poll_list)) { | |
902 | unsigned int nr_events = 0; | |
903 | ||
904 | io_iopoll_getevents(ctx, &nr_events, 1); | |
08f5439f JA |
905 | |
906 | /* | |
907 | * Ensure we allow local-to-the-cpu processing to take place, | |
908 | * in this case we need to ensure that we reap all events. | |
909 | */ | |
910 | cond_resched(); | |
def596e9 JA |
911 | } |
912 | mutex_unlock(&ctx->uring_lock); | |
913 | } | |
914 | ||
2b2ed975 JA |
915 | static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
916 | long min) | |
def596e9 | 917 | { |
2b2ed975 | 918 | int iters = 0, ret = 0; |
500f9fba | 919 | |
def596e9 JA |
920 | do { |
921 | int tmin = 0; | |
922 | ||
a3a0e43f JA |
923 | /* |
924 | * Don't enter poll loop if we already have events pending. | |
925 | * If we do, we can potentially be spinning for commands that | |
926 | * already triggered a CQE (eg in error). | |
927 | */ | |
75b28aff | 928 | if (io_cqring_events(ctx->rings)) |
a3a0e43f JA |
929 | break; |
930 | ||
500f9fba JA |
931 | /* |
932 | * If a submit got punted to a workqueue, we can have the | |
933 | * application entering polling for a command before it gets | |
934 | * issued. That app will hold the uring_lock for the duration | |
935 | * of the poll right here, so we need to take a breather every | |
936 | * now and then to ensure that the issue has a chance to add | |
937 | * the poll to the issued list. Otherwise we can spin here | |
938 | * forever, while the workqueue is stuck trying to acquire the | |
939 | * very same mutex. | |
940 | */ | |
941 | if (!(++iters & 7)) { | |
942 | mutex_unlock(&ctx->uring_lock); | |
943 | mutex_lock(&ctx->uring_lock); | |
944 | } | |
945 | ||
def596e9 JA |
946 | if (*nr_events < min) |
947 | tmin = min - *nr_events; | |
948 | ||
949 | ret = io_iopoll_getevents(ctx, nr_events, tmin); | |
950 | if (ret <= 0) | |
951 | break; | |
952 | ret = 0; | |
953 | } while (min && !*nr_events && !need_resched()); | |
954 | ||
2b2ed975 JA |
955 | return ret; |
956 | } | |
957 | ||
958 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, | |
959 | long min) | |
960 | { | |
961 | int ret; | |
962 | ||
963 | /* | |
964 | * We disallow the app entering submit/complete with polling, but we | |
965 | * still need to lock the ring to prevent racing with polled issue | |
966 | * that got punted to a workqueue. | |
967 | */ | |
968 | mutex_lock(&ctx->uring_lock); | |
969 | ret = __io_iopoll_check(ctx, nr_events, min); | |
500f9fba | 970 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
971 | return ret; |
972 | } | |
973 | ||
491381ce | 974 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 975 | { |
491381ce JA |
976 | /* |
977 | * Tell lockdep we inherited freeze protection from submission | |
978 | * thread. | |
979 | */ | |
980 | if (req->flags & REQ_F_ISREG) { | |
981 | struct inode *inode = file_inode(req->file); | |
2b188cc1 | 982 | |
491381ce | 983 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
2b188cc1 | 984 | } |
491381ce | 985 | file_end_write(req->file); |
2b188cc1 JA |
986 | } |
987 | ||
ba816ad6 | 988 | static void io_complete_rw_common(struct kiocb *kiocb, long res) |
2b188cc1 JA |
989 | { |
990 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw); | |
991 | ||
491381ce JA |
992 | if (kiocb->ki_flags & IOCB_WRITE) |
993 | kiocb_end_write(req); | |
2b188cc1 | 994 | |
9e645e11 JA |
995 | if ((req->flags & REQ_F_LINK) && res != req->result) |
996 | req->flags |= REQ_F_FAIL_LINK; | |
c71ffb67 | 997 | io_cqring_add_event(req->ctx, req->user_data, res); |
ba816ad6 JA |
998 | } |
999 | ||
1000 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
1001 | { | |
1002 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw); | |
1003 | ||
1004 | io_complete_rw_common(kiocb, res); | |
1005 | io_put_req(req, NULL); | |
1006 | } | |
1007 | ||
1008 | static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res) | |
1009 | { | |
1010 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw); | |
1011 | ||
1012 | io_complete_rw_common(kiocb, res); | |
1013 | return io_put_req_find_next(req); | |
2b188cc1 JA |
1014 | } |
1015 | ||
def596e9 JA |
1016 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
1017 | { | |
1018 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw); | |
1019 | ||
491381ce JA |
1020 | if (kiocb->ki_flags & IOCB_WRITE) |
1021 | kiocb_end_write(req); | |
def596e9 | 1022 | |
9e645e11 JA |
1023 | if ((req->flags & REQ_F_LINK) && res != req->result) |
1024 | req->flags |= REQ_F_FAIL_LINK; | |
1025 | req->result = res; | |
def596e9 JA |
1026 | if (res != -EAGAIN) |
1027 | req->flags |= REQ_F_IOPOLL_COMPLETED; | |
1028 | } | |
1029 | ||
1030 | /* | |
1031 | * After the iocb has been issued, it's safe to be found on the poll list. | |
1032 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
1033 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
1034 | * accessing the kiocb cookie. | |
1035 | */ | |
1036 | static void io_iopoll_req_issued(struct io_kiocb *req) | |
1037 | { | |
1038 | struct io_ring_ctx *ctx = req->ctx; | |
1039 | ||
1040 | /* | |
1041 | * Track whether we have multiple files in our lists. This will impact | |
1042 | * how we do polling eventually, not spinning if we're on potentially | |
1043 | * different devices. | |
1044 | */ | |
1045 | if (list_empty(&ctx->poll_list)) { | |
1046 | ctx->poll_multi_file = false; | |
1047 | } else if (!ctx->poll_multi_file) { | |
1048 | struct io_kiocb *list_req; | |
1049 | ||
1050 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, | |
1051 | list); | |
1052 | if (list_req->rw.ki_filp != req->rw.ki_filp) | |
1053 | ctx->poll_multi_file = true; | |
1054 | } | |
1055 | ||
1056 | /* | |
1057 | * For fast devices, IO may have already completed. If it has, add | |
1058 | * it to the front so we find it first. | |
1059 | */ | |
1060 | if (req->flags & REQ_F_IOPOLL_COMPLETED) | |
1061 | list_add(&req->list, &ctx->poll_list); | |
1062 | else | |
1063 | list_add_tail(&req->list, &ctx->poll_list); | |
1064 | } | |
1065 | ||
3d6770fb | 1066 | static void io_file_put(struct io_submit_state *state) |
9a56a232 | 1067 | { |
3d6770fb | 1068 | if (state->file) { |
9a56a232 JA |
1069 | int diff = state->has_refs - state->used_refs; |
1070 | ||
1071 | if (diff) | |
1072 | fput_many(state->file, diff); | |
1073 | state->file = NULL; | |
1074 | } | |
1075 | } | |
1076 | ||
1077 | /* | |
1078 | * Get as many references to a file as we have IOs left in this submission, | |
1079 | * assuming most submissions are for one file, or at least that each file | |
1080 | * has more than one submission. | |
1081 | */ | |
1082 | static struct file *io_file_get(struct io_submit_state *state, int fd) | |
1083 | { | |
1084 | if (!state) | |
1085 | return fget(fd); | |
1086 | ||
1087 | if (state->file) { | |
1088 | if (state->fd == fd) { | |
1089 | state->used_refs++; | |
1090 | state->ios_left--; | |
1091 | return state->file; | |
1092 | } | |
3d6770fb | 1093 | io_file_put(state); |
9a56a232 JA |
1094 | } |
1095 | state->file = fget_many(fd, state->ios_left); | |
1096 | if (!state->file) | |
1097 | return NULL; | |
1098 | ||
1099 | state->fd = fd; | |
1100 | state->has_refs = state->ios_left; | |
1101 | state->used_refs = 1; | |
1102 | state->ios_left--; | |
1103 | return state->file; | |
1104 | } | |
1105 | ||
2b188cc1 JA |
1106 | /* |
1107 | * If we tracked the file through the SCM inflight mechanism, we could support | |
1108 | * any file. For now, just ensure that anything potentially problematic is done | |
1109 | * inline. | |
1110 | */ | |
1111 | static bool io_file_supports_async(struct file *file) | |
1112 | { | |
1113 | umode_t mode = file_inode(file)->i_mode; | |
1114 | ||
1115 | if (S_ISBLK(mode) || S_ISCHR(mode)) | |
1116 | return true; | |
1117 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) | |
1118 | return true; | |
1119 | ||
1120 | return false; | |
1121 | } | |
1122 | ||
6c271ce2 | 1123 | static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s, |
8358e3a8 | 1124 | bool force_nonblock) |
2b188cc1 | 1125 | { |
6c271ce2 | 1126 | const struct io_uring_sqe *sqe = s->sqe; |
def596e9 | 1127 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 1128 | struct kiocb *kiocb = &req->rw; |
09bb8394 JA |
1129 | unsigned ioprio; |
1130 | int ret; | |
2b188cc1 | 1131 | |
09bb8394 JA |
1132 | if (!req->file) |
1133 | return -EBADF; | |
2b188cc1 | 1134 | |
491381ce JA |
1135 | if (S_ISREG(file_inode(req->file)->i_mode)) |
1136 | req->flags |= REQ_F_ISREG; | |
1137 | ||
1138 | /* | |
1139 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so | |
1140 | * we know to async punt it even if it was opened O_NONBLOCK | |
1141 | */ | |
1142 | if (force_nonblock && !io_file_supports_async(req->file)) { | |
1143 | req->flags |= REQ_F_MUST_PUNT; | |
1144 | return -EAGAIN; | |
1145 | } | |
6b06314c | 1146 | |
2b188cc1 JA |
1147 | kiocb->ki_pos = READ_ONCE(sqe->off); |
1148 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); | |
1149 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); | |
1150 | ||
1151 | ioprio = READ_ONCE(sqe->ioprio); | |
1152 | if (ioprio) { | |
1153 | ret = ioprio_check_cap(ioprio); | |
1154 | if (ret) | |
09bb8394 | 1155 | return ret; |
2b188cc1 JA |
1156 | |
1157 | kiocb->ki_ioprio = ioprio; | |
1158 | } else | |
1159 | kiocb->ki_ioprio = get_current_ioprio(); | |
1160 | ||
1161 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
1162 | if (unlikely(ret)) | |
09bb8394 | 1163 | return ret; |
8449eeda SB |
1164 | |
1165 | /* don't allow async punt if RWF_NOWAIT was requested */ | |
491381ce JA |
1166 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
1167 | (req->file->f_flags & O_NONBLOCK)) | |
8449eeda SB |
1168 | req->flags |= REQ_F_NOWAIT; |
1169 | ||
1170 | if (force_nonblock) | |
2b188cc1 | 1171 | kiocb->ki_flags |= IOCB_NOWAIT; |
8449eeda | 1172 | |
def596e9 | 1173 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
1174 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
1175 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 1176 | return -EOPNOTSUPP; |
2b188cc1 | 1177 | |
def596e9 JA |
1178 | kiocb->ki_flags |= IOCB_HIPRI; |
1179 | kiocb->ki_complete = io_complete_rw_iopoll; | |
1180 | } else { | |
09bb8394 JA |
1181 | if (kiocb->ki_flags & IOCB_HIPRI) |
1182 | return -EINVAL; | |
def596e9 JA |
1183 | kiocb->ki_complete = io_complete_rw; |
1184 | } | |
2b188cc1 | 1185 | return 0; |
2b188cc1 JA |
1186 | } |
1187 | ||
1188 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
1189 | { | |
1190 | switch (ret) { | |
1191 | case -EIOCBQUEUED: | |
1192 | break; | |
1193 | case -ERESTARTSYS: | |
1194 | case -ERESTARTNOINTR: | |
1195 | case -ERESTARTNOHAND: | |
1196 | case -ERESTART_RESTARTBLOCK: | |
1197 | /* | |
1198 | * We can't just restart the syscall, since previously | |
1199 | * submitted sqes may already be in progress. Just fail this | |
1200 | * IO with EINTR. | |
1201 | */ | |
1202 | ret = -EINTR; | |
1203 | /* fall through */ | |
1204 | default: | |
1205 | kiocb->ki_complete(kiocb, ret, 0); | |
1206 | } | |
1207 | } | |
1208 | ||
ba816ad6 JA |
1209 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt, |
1210 | bool in_async) | |
1211 | { | |
1212 | if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw) | |
1213 | *nxt = __io_complete_rw(kiocb, ret); | |
1214 | else | |
1215 | io_rw_done(kiocb, ret); | |
1216 | } | |
1217 | ||
edafccee JA |
1218 | static int io_import_fixed(struct io_ring_ctx *ctx, int rw, |
1219 | const struct io_uring_sqe *sqe, | |
1220 | struct iov_iter *iter) | |
1221 | { | |
1222 | size_t len = READ_ONCE(sqe->len); | |
1223 | struct io_mapped_ubuf *imu; | |
1224 | unsigned index, buf_index; | |
1225 | size_t offset; | |
1226 | u64 buf_addr; | |
1227 | ||
1228 | /* attempt to use fixed buffers without having provided iovecs */ | |
1229 | if (unlikely(!ctx->user_bufs)) | |
1230 | return -EFAULT; | |
1231 | ||
1232 | buf_index = READ_ONCE(sqe->buf_index); | |
1233 | if (unlikely(buf_index >= ctx->nr_user_bufs)) | |
1234 | return -EFAULT; | |
1235 | ||
1236 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); | |
1237 | imu = &ctx->user_bufs[index]; | |
1238 | buf_addr = READ_ONCE(sqe->addr); | |
1239 | ||
1240 | /* overflow */ | |
1241 | if (buf_addr + len < buf_addr) | |
1242 | return -EFAULT; | |
1243 | /* not inside the mapped region */ | |
1244 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) | |
1245 | return -EFAULT; | |
1246 | ||
1247 | /* | |
1248 | * May not be a start of buffer, set size appropriately | |
1249 | * and advance us to the beginning. | |
1250 | */ | |
1251 | offset = buf_addr - imu->ubuf; | |
1252 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
1253 | |
1254 | if (offset) { | |
1255 | /* | |
1256 | * Don't use iov_iter_advance() here, as it's really slow for | |
1257 | * using the latter parts of a big fixed buffer - it iterates | |
1258 | * over each segment manually. We can cheat a bit here, because | |
1259 | * we know that: | |
1260 | * | |
1261 | * 1) it's a BVEC iter, we set it up | |
1262 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
1263 | * first and last bvec | |
1264 | * | |
1265 | * So just find our index, and adjust the iterator afterwards. | |
1266 | * If the offset is within the first bvec (or the whole first | |
1267 | * bvec, just use iov_iter_advance(). This makes it easier | |
1268 | * since we can just skip the first segment, which may not | |
1269 | * be PAGE_SIZE aligned. | |
1270 | */ | |
1271 | const struct bio_vec *bvec = imu->bvec; | |
1272 | ||
1273 | if (offset <= bvec->bv_len) { | |
1274 | iov_iter_advance(iter, offset); | |
1275 | } else { | |
1276 | unsigned long seg_skip; | |
1277 | ||
1278 | /* skip first vec */ | |
1279 | offset -= bvec->bv_len; | |
1280 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
1281 | ||
1282 | iter->bvec = bvec + seg_skip; | |
1283 | iter->nr_segs -= seg_skip; | |
99c79f66 | 1284 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 1285 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
1286 | } |
1287 | } | |
1288 | ||
edafccee JA |
1289 | return 0; |
1290 | } | |
1291 | ||
87e5e6da JA |
1292 | static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw, |
1293 | const struct sqe_submit *s, struct iovec **iovec, | |
1294 | struct iov_iter *iter) | |
2b188cc1 JA |
1295 | { |
1296 | const struct io_uring_sqe *sqe = s->sqe; | |
1297 | void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
1298 | size_t sqe_len = READ_ONCE(sqe->len); | |
edafccee JA |
1299 | u8 opcode; |
1300 | ||
1301 | /* | |
1302 | * We're reading ->opcode for the second time, but the first read | |
1303 | * doesn't care whether it's _FIXED or not, so it doesn't matter | |
1304 | * whether ->opcode changes concurrently. The first read does care | |
1305 | * about whether it is a READ or a WRITE, so we don't trust this read | |
1306 | * for that purpose and instead let the caller pass in the read/write | |
1307 | * flag. | |
1308 | */ | |
1309 | opcode = READ_ONCE(sqe->opcode); | |
1310 | if (opcode == IORING_OP_READ_FIXED || | |
1311 | opcode == IORING_OP_WRITE_FIXED) { | |
87e5e6da | 1312 | ssize_t ret = io_import_fixed(ctx, rw, sqe, iter); |
edafccee JA |
1313 | *iovec = NULL; |
1314 | return ret; | |
1315 | } | |
2b188cc1 JA |
1316 | |
1317 | if (!s->has_user) | |
1318 | return -EFAULT; | |
1319 | ||
1320 | #ifdef CONFIG_COMPAT | |
1321 | if (ctx->compat) | |
1322 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, | |
1323 | iovec, iter); | |
1324 | #endif | |
1325 | ||
1326 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); | |
1327 | } | |
1328 | ||
32960613 JA |
1329 | /* |
1330 | * For files that don't have ->read_iter() and ->write_iter(), handle them | |
1331 | * by looping over ->read() or ->write() manually. | |
1332 | */ | |
1333 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, | |
1334 | struct iov_iter *iter) | |
1335 | { | |
1336 | ssize_t ret = 0; | |
1337 | ||
1338 | /* | |
1339 | * Don't support polled IO through this interface, and we can't | |
1340 | * support non-blocking either. For the latter, this just causes | |
1341 | * the kiocb to be handled from an async context. | |
1342 | */ | |
1343 | if (kiocb->ki_flags & IOCB_HIPRI) | |
1344 | return -EOPNOTSUPP; | |
1345 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
1346 | return -EAGAIN; | |
1347 | ||
1348 | while (iov_iter_count(iter)) { | |
1349 | struct iovec iovec = iov_iter_iovec(iter); | |
1350 | ssize_t nr; | |
1351 | ||
1352 | if (rw == READ) { | |
1353 | nr = file->f_op->read(file, iovec.iov_base, | |
1354 | iovec.iov_len, &kiocb->ki_pos); | |
1355 | } else { | |
1356 | nr = file->f_op->write(file, iovec.iov_base, | |
1357 | iovec.iov_len, &kiocb->ki_pos); | |
1358 | } | |
1359 | ||
1360 | if (nr < 0) { | |
1361 | if (!ret) | |
1362 | ret = nr; | |
1363 | break; | |
1364 | } | |
1365 | ret += nr; | |
1366 | if (nr != iovec.iov_len) | |
1367 | break; | |
1368 | iov_iter_advance(iter, nr); | |
1369 | } | |
1370 | ||
1371 | return ret; | |
1372 | } | |
1373 | ||
e0c5c576 | 1374 | static int io_read(struct io_kiocb *req, const struct sqe_submit *s, |
ba816ad6 | 1375 | struct io_kiocb **nxt, bool force_nonblock) |
2b188cc1 JA |
1376 | { |
1377 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
1378 | struct kiocb *kiocb = &req->rw; | |
1379 | struct iov_iter iter; | |
1380 | struct file *file; | |
31b51510 | 1381 | size_t iov_count; |
9d93a3f5 | 1382 | ssize_t read_size, ret; |
2b188cc1 | 1383 | |
8358e3a8 | 1384 | ret = io_prep_rw(req, s, force_nonblock); |
2b188cc1 JA |
1385 | if (ret) |
1386 | return ret; | |
1387 | file = kiocb->ki_filp; | |
1388 | ||
2b188cc1 | 1389 | if (unlikely(!(file->f_mode & FMODE_READ))) |
09bb8394 | 1390 | return -EBADF; |
2b188cc1 JA |
1391 | |
1392 | ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter); | |
87e5e6da | 1393 | if (ret < 0) |
09bb8394 | 1394 | return ret; |
2b188cc1 | 1395 | |
9d93a3f5 | 1396 | read_size = ret; |
9e645e11 JA |
1397 | if (req->flags & REQ_F_LINK) |
1398 | req->result = read_size; | |
1399 | ||
31b51510 JA |
1400 | iov_count = iov_iter_count(&iter); |
1401 | ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count); | |
2b188cc1 JA |
1402 | if (!ret) { |
1403 | ssize_t ret2; | |
1404 | ||
32960613 JA |
1405 | if (file->f_op->read_iter) |
1406 | ret2 = call_read_iter(file, kiocb, &iter); | |
1407 | else | |
1408 | ret2 = loop_rw_iter(READ, file, kiocb, &iter); | |
1409 | ||
9d93a3f5 JA |
1410 | /* |
1411 | * In case of a short read, punt to async. This can happen | |
1412 | * if we have data partially cached. Alternatively we can | |
1413 | * return the short read, in which case the application will | |
1414 | * need to issue another SQE and wait for it. That SQE will | |
1415 | * need async punt anyway, so it's more efficient to do it | |
1416 | * here. | |
1417 | */ | |
491381ce JA |
1418 | if (force_nonblock && !(req->flags & REQ_F_NOWAIT) && |
1419 | (req->flags & REQ_F_ISREG) && | |
1420 | ret2 > 0 && ret2 < read_size) | |
9d93a3f5 JA |
1421 | ret2 = -EAGAIN; |
1422 | /* Catch -EAGAIN return for forced non-blocking submission */ | |
561fb04a | 1423 | if (!force_nonblock || ret2 != -EAGAIN) |
ba5290cc | 1424 | kiocb_done(kiocb, ret2, nxt, s->in_async); |
561fb04a | 1425 | else |
2b188cc1 JA |
1426 | ret = -EAGAIN; |
1427 | } | |
1428 | kfree(iovec); | |
2b188cc1 JA |
1429 | return ret; |
1430 | } | |
1431 | ||
e0c5c576 | 1432 | static int io_write(struct io_kiocb *req, const struct sqe_submit *s, |
ba816ad6 | 1433 | struct io_kiocb **nxt, bool force_nonblock) |
2b188cc1 JA |
1434 | { |
1435 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
1436 | struct kiocb *kiocb = &req->rw; | |
1437 | struct iov_iter iter; | |
1438 | struct file *file; | |
31b51510 | 1439 | size_t iov_count; |
87e5e6da | 1440 | ssize_t ret; |
2b188cc1 | 1441 | |
8358e3a8 | 1442 | ret = io_prep_rw(req, s, force_nonblock); |
2b188cc1 JA |
1443 | if (ret) |
1444 | return ret; | |
2b188cc1 | 1445 | |
2b188cc1 JA |
1446 | file = kiocb->ki_filp; |
1447 | if (unlikely(!(file->f_mode & FMODE_WRITE))) | |
09bb8394 | 1448 | return -EBADF; |
2b188cc1 JA |
1449 | |
1450 | ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter); | |
87e5e6da | 1451 | if (ret < 0) |
09bb8394 | 1452 | return ret; |
2b188cc1 | 1453 | |
9e645e11 JA |
1454 | if (req->flags & REQ_F_LINK) |
1455 | req->result = ret; | |
1456 | ||
31b51510 JA |
1457 | iov_count = iov_iter_count(&iter); |
1458 | ||
1459 | ret = -EAGAIN; | |
561fb04a | 1460 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) |
31b51510 | 1461 | goto out_free; |
31b51510 JA |
1462 | |
1463 | ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count); | |
2b188cc1 | 1464 | if (!ret) { |
9bf7933f RP |
1465 | ssize_t ret2; |
1466 | ||
2b188cc1 JA |
1467 | /* |
1468 | * Open-code file_start_write here to grab freeze protection, | |
1469 | * which will be released by another thread in | |
1470 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
1471 | * released so that it doesn't complain about the held lock when | |
1472 | * we return to userspace. | |
1473 | */ | |
491381ce | 1474 | if (req->flags & REQ_F_ISREG) { |
2b188cc1 JA |
1475 | __sb_start_write(file_inode(file)->i_sb, |
1476 | SB_FREEZE_WRITE, true); | |
1477 | __sb_writers_release(file_inode(file)->i_sb, | |
1478 | SB_FREEZE_WRITE); | |
1479 | } | |
1480 | kiocb->ki_flags |= IOCB_WRITE; | |
9bf7933f | 1481 | |
32960613 JA |
1482 | if (file->f_op->write_iter) |
1483 | ret2 = call_write_iter(file, kiocb, &iter); | |
1484 | else | |
1485 | ret2 = loop_rw_iter(WRITE, file, kiocb, &iter); | |
561fb04a | 1486 | if (!force_nonblock || ret2 != -EAGAIN) |
ba5290cc | 1487 | kiocb_done(kiocb, ret2, nxt, s->in_async); |
561fb04a | 1488 | else |
9bf7933f | 1489 | ret = -EAGAIN; |
2b188cc1 | 1490 | } |
31b51510 | 1491 | out_free: |
2b188cc1 | 1492 | kfree(iovec); |
2b188cc1 JA |
1493 | return ret; |
1494 | } | |
1495 | ||
1496 | /* | |
1497 | * IORING_OP_NOP just posts a completion event, nothing else. | |
1498 | */ | |
1499 | static int io_nop(struct io_kiocb *req, u64 user_data) | |
1500 | { | |
1501 | struct io_ring_ctx *ctx = req->ctx; | |
1502 | long err = 0; | |
1503 | ||
def596e9 JA |
1504 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
1505 | return -EINVAL; | |
1506 | ||
c71ffb67 | 1507 | io_cqring_add_event(ctx, user_data, err); |
ba816ad6 | 1508 | io_put_req(req, NULL); |
2b188cc1 JA |
1509 | return 0; |
1510 | } | |
1511 | ||
c992fe29 CH |
1512 | static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
1513 | { | |
6b06314c | 1514 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 1515 | |
09bb8394 JA |
1516 | if (!req->file) |
1517 | return -EBADF; | |
c992fe29 | 1518 | |
6b06314c | 1519 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 1520 | return -EINVAL; |
edafccee | 1521 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
1522 | return -EINVAL; |
1523 | ||
c992fe29 CH |
1524 | return 0; |
1525 | } | |
1526 | ||
1527 | static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe, | |
ba816ad6 | 1528 | struct io_kiocb **nxt, bool force_nonblock) |
c992fe29 CH |
1529 | { |
1530 | loff_t sqe_off = READ_ONCE(sqe->off); | |
1531 | loff_t sqe_len = READ_ONCE(sqe->len); | |
1532 | loff_t end = sqe_off + sqe_len; | |
1533 | unsigned fsync_flags; | |
1534 | int ret; | |
1535 | ||
1536 | fsync_flags = READ_ONCE(sqe->fsync_flags); | |
1537 | if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC)) | |
1538 | return -EINVAL; | |
1539 | ||
1540 | ret = io_prep_fsync(req, sqe); | |
1541 | if (ret) | |
1542 | return ret; | |
1543 | ||
1544 | /* fsync always requires a blocking context */ | |
1545 | if (force_nonblock) | |
1546 | return -EAGAIN; | |
1547 | ||
1548 | ret = vfs_fsync_range(req->rw.ki_filp, sqe_off, | |
1549 | end > 0 ? end : LLONG_MAX, | |
1550 | fsync_flags & IORING_FSYNC_DATASYNC); | |
1551 | ||
9e645e11 JA |
1552 | if (ret < 0 && (req->flags & REQ_F_LINK)) |
1553 | req->flags |= REQ_F_FAIL_LINK; | |
c71ffb67 | 1554 | io_cqring_add_event(req->ctx, sqe->user_data, ret); |
ba816ad6 | 1555 | io_put_req(req, nxt); |
c992fe29 CH |
1556 | return 0; |
1557 | } | |
1558 | ||
5d17b4a4 JA |
1559 | static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
1560 | { | |
1561 | struct io_ring_ctx *ctx = req->ctx; | |
1562 | int ret = 0; | |
1563 | ||
1564 | if (!req->file) | |
1565 | return -EBADF; | |
5d17b4a4 JA |
1566 | |
1567 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) | |
1568 | return -EINVAL; | |
1569 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
1570 | return -EINVAL; | |
1571 | ||
5d17b4a4 JA |
1572 | return ret; |
1573 | } | |
1574 | ||
1575 | static int io_sync_file_range(struct io_kiocb *req, | |
1576 | const struct io_uring_sqe *sqe, | |
ba816ad6 | 1577 | struct io_kiocb **nxt, |
5d17b4a4 JA |
1578 | bool force_nonblock) |
1579 | { | |
1580 | loff_t sqe_off; | |
1581 | loff_t sqe_len; | |
1582 | unsigned flags; | |
1583 | int ret; | |
1584 | ||
1585 | ret = io_prep_sfr(req, sqe); | |
1586 | if (ret) | |
1587 | return ret; | |
1588 | ||
1589 | /* sync_file_range always requires a blocking context */ | |
1590 | if (force_nonblock) | |
1591 | return -EAGAIN; | |
1592 | ||
1593 | sqe_off = READ_ONCE(sqe->off); | |
1594 | sqe_len = READ_ONCE(sqe->len); | |
1595 | flags = READ_ONCE(sqe->sync_range_flags); | |
1596 | ||
1597 | ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags); | |
1598 | ||
9e645e11 JA |
1599 | if (ret < 0 && (req->flags & REQ_F_LINK)) |
1600 | req->flags |= REQ_F_FAIL_LINK; | |
c71ffb67 | 1601 | io_cqring_add_event(req->ctx, sqe->user_data, ret); |
ba816ad6 | 1602 | io_put_req(req, nxt); |
5d17b4a4 JA |
1603 | return 0; |
1604 | } | |
1605 | ||
0fa03c62 | 1606 | #if defined(CONFIG_NET) |
aa1fa28f | 1607 | static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
ba816ad6 | 1608 | struct io_kiocb **nxt, bool force_nonblock, |
aa1fa28f JA |
1609 | long (*fn)(struct socket *, struct user_msghdr __user *, |
1610 | unsigned int)) | |
1611 | { | |
0fa03c62 JA |
1612 | struct socket *sock; |
1613 | int ret; | |
1614 | ||
1615 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
1616 | return -EINVAL; | |
1617 | ||
1618 | sock = sock_from_file(req->file, &ret); | |
1619 | if (sock) { | |
1620 | struct user_msghdr __user *msg; | |
1621 | unsigned flags; | |
1622 | ||
1623 | flags = READ_ONCE(sqe->msg_flags); | |
1624 | if (flags & MSG_DONTWAIT) | |
1625 | req->flags |= REQ_F_NOWAIT; | |
1626 | else if (force_nonblock) | |
1627 | flags |= MSG_DONTWAIT; | |
1628 | ||
1629 | msg = (struct user_msghdr __user *) (unsigned long) | |
1630 | READ_ONCE(sqe->addr); | |
1631 | ||
aa1fa28f | 1632 | ret = fn(sock, msg, flags); |
0fa03c62 JA |
1633 | if (force_nonblock && ret == -EAGAIN) |
1634 | return ret; | |
1635 | } | |
1636 | ||
c71ffb67 | 1637 | io_cqring_add_event(req->ctx, sqe->user_data, ret); |
ba816ad6 | 1638 | io_put_req(req, nxt); |
5d17b4a4 JA |
1639 | return 0; |
1640 | } | |
aa1fa28f JA |
1641 | #endif |
1642 | ||
1643 | static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe, | |
ba816ad6 | 1644 | struct io_kiocb **nxt, bool force_nonblock) |
aa1fa28f JA |
1645 | { |
1646 | #if defined(CONFIG_NET) | |
ba816ad6 JA |
1647 | return io_send_recvmsg(req, sqe, nxt, force_nonblock, |
1648 | __sys_sendmsg_sock); | |
aa1fa28f JA |
1649 | #else |
1650 | return -EOPNOTSUPP; | |
1651 | #endif | |
1652 | } | |
1653 | ||
1654 | static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe, | |
ba816ad6 | 1655 | struct io_kiocb **nxt, bool force_nonblock) |
aa1fa28f JA |
1656 | { |
1657 | #if defined(CONFIG_NET) | |
ba816ad6 JA |
1658 | return io_send_recvmsg(req, sqe, nxt, force_nonblock, |
1659 | __sys_recvmsg_sock); | |
0fa03c62 JA |
1660 | #else |
1661 | return -EOPNOTSUPP; | |
1662 | #endif | |
1663 | } | |
5d17b4a4 | 1664 | |
221c5eb2 JA |
1665 | static void io_poll_remove_one(struct io_kiocb *req) |
1666 | { | |
1667 | struct io_poll_iocb *poll = &req->poll; | |
1668 | ||
1669 | spin_lock(&poll->head->lock); | |
1670 | WRITE_ONCE(poll->canceled, true); | |
1671 | if (!list_empty(&poll->wait.entry)) { | |
1672 | list_del_init(&poll->wait.entry); | |
18d9be1a | 1673 | io_queue_async_work(req->ctx, req); |
221c5eb2 JA |
1674 | } |
1675 | spin_unlock(&poll->head->lock); | |
1676 | ||
1677 | list_del_init(&req->list); | |
1678 | } | |
1679 | ||
1680 | static void io_poll_remove_all(struct io_ring_ctx *ctx) | |
1681 | { | |
1682 | struct io_kiocb *req; | |
1683 | ||
1684 | spin_lock_irq(&ctx->completion_lock); | |
1685 | while (!list_empty(&ctx->cancel_list)) { | |
1686 | req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list); | |
1687 | io_poll_remove_one(req); | |
1688 | } | |
1689 | spin_unlock_irq(&ctx->completion_lock); | |
1690 | } | |
1691 | ||
1692 | /* | |
1693 | * Find a running poll command that matches one specified in sqe->addr, | |
1694 | * and remove it if found. | |
1695 | */ | |
1696 | static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
1697 | { | |
1698 | struct io_ring_ctx *ctx = req->ctx; | |
1699 | struct io_kiocb *poll_req, *next; | |
1700 | int ret = -ENOENT; | |
1701 | ||
1702 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
1703 | return -EINVAL; | |
1704 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
1705 | sqe->poll_events) | |
1706 | return -EINVAL; | |
1707 | ||
1708 | spin_lock_irq(&ctx->completion_lock); | |
1709 | list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) { | |
1710 | if (READ_ONCE(sqe->addr) == poll_req->user_data) { | |
1711 | io_poll_remove_one(poll_req); | |
1712 | ret = 0; | |
1713 | break; | |
1714 | } | |
1715 | } | |
1716 | spin_unlock_irq(&ctx->completion_lock); | |
1717 | ||
c71ffb67 | 1718 | io_cqring_add_event(req->ctx, sqe->user_data, ret); |
ba816ad6 | 1719 | io_put_req(req, NULL); |
221c5eb2 JA |
1720 | return 0; |
1721 | } | |
1722 | ||
8c838788 JA |
1723 | static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req, |
1724 | __poll_t mask) | |
221c5eb2 | 1725 | { |
8c838788 | 1726 | req->poll.done = true; |
c71ffb67 | 1727 | io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask)); |
8c838788 | 1728 | io_commit_cqring(ctx); |
221c5eb2 JA |
1729 | } |
1730 | ||
561fb04a | 1731 | static void io_poll_complete_work(struct io_wq_work **workptr) |
221c5eb2 | 1732 | { |
561fb04a | 1733 | struct io_wq_work *work = *workptr; |
221c5eb2 JA |
1734 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
1735 | struct io_poll_iocb *poll = &req->poll; | |
1736 | struct poll_table_struct pt = { ._key = poll->events }; | |
1737 | struct io_ring_ctx *ctx = req->ctx; | |
1738 | __poll_t mask = 0; | |
1739 | ||
561fb04a JA |
1740 | if (work->flags & IO_WQ_WORK_CANCEL) |
1741 | WRITE_ONCE(poll->canceled, true); | |
1742 | ||
221c5eb2 JA |
1743 | if (!READ_ONCE(poll->canceled)) |
1744 | mask = vfs_poll(poll->file, &pt) & poll->events; | |
1745 | ||
1746 | /* | |
1747 | * Note that ->ki_cancel callers also delete iocb from active_reqs after | |
1748 | * calling ->ki_cancel. We need the ctx_lock roundtrip here to | |
1749 | * synchronize with them. In the cancellation case the list_del_init | |
1750 | * itself is not actually needed, but harmless so we keep it in to | |
1751 | * avoid further branches in the fast path. | |
1752 | */ | |
1753 | spin_lock_irq(&ctx->completion_lock); | |
1754 | if (!mask && !READ_ONCE(poll->canceled)) { | |
1755 | add_wait_queue(poll->head, &poll->wait); | |
1756 | spin_unlock_irq(&ctx->completion_lock); | |
1757 | return; | |
1758 | } | |
1759 | list_del_init(&req->list); | |
8c838788 | 1760 | io_poll_complete(ctx, req, mask); |
221c5eb2 JA |
1761 | spin_unlock_irq(&ctx->completion_lock); |
1762 | ||
8c838788 | 1763 | io_cqring_ev_posted(ctx); |
ba816ad6 | 1764 | io_put_req(req, NULL); |
221c5eb2 JA |
1765 | } |
1766 | ||
1767 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
1768 | void *key) | |
1769 | { | |
1770 | struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb, | |
1771 | wait); | |
1772 | struct io_kiocb *req = container_of(poll, struct io_kiocb, poll); | |
1773 | struct io_ring_ctx *ctx = req->ctx; | |
1774 | __poll_t mask = key_to_poll(key); | |
8c838788 | 1775 | unsigned long flags; |
221c5eb2 JA |
1776 | |
1777 | /* for instances that support it check for an event match first: */ | |
8c838788 JA |
1778 | if (mask && !(mask & poll->events)) |
1779 | return 0; | |
221c5eb2 | 1780 | |
8c838788 | 1781 | list_del_init(&poll->wait.entry); |
221c5eb2 | 1782 | |
8c838788 JA |
1783 | if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) { |
1784 | list_del(&req->list); | |
1785 | io_poll_complete(ctx, req, mask); | |
1786 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
221c5eb2 | 1787 | |
8c838788 | 1788 | io_cqring_ev_posted(ctx); |
ba816ad6 | 1789 | io_put_req(req, NULL); |
8c838788 | 1790 | } else { |
18d9be1a | 1791 | io_queue_async_work(ctx, req); |
221c5eb2 JA |
1792 | } |
1793 | ||
221c5eb2 JA |
1794 | return 1; |
1795 | } | |
1796 | ||
1797 | struct io_poll_table { | |
1798 | struct poll_table_struct pt; | |
1799 | struct io_kiocb *req; | |
1800 | int error; | |
1801 | }; | |
1802 | ||
1803 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, | |
1804 | struct poll_table_struct *p) | |
1805 | { | |
1806 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
1807 | ||
1808 | if (unlikely(pt->req->poll.head)) { | |
1809 | pt->error = -EINVAL; | |
1810 | return; | |
1811 | } | |
1812 | ||
1813 | pt->error = 0; | |
1814 | pt->req->poll.head = head; | |
1815 | add_wait_queue(head, &pt->req->poll.wait); | |
1816 | } | |
1817 | ||
1818 | static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
1819 | { | |
1820 | struct io_poll_iocb *poll = &req->poll; | |
1821 | struct io_ring_ctx *ctx = req->ctx; | |
1822 | struct io_poll_table ipt; | |
8c838788 | 1823 | bool cancel = false; |
221c5eb2 JA |
1824 | __poll_t mask; |
1825 | u16 events; | |
221c5eb2 JA |
1826 | |
1827 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
1828 | return -EINVAL; | |
1829 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
1830 | return -EINVAL; | |
09bb8394 JA |
1831 | if (!poll->file) |
1832 | return -EBADF; | |
221c5eb2 | 1833 | |
6cc47d1d | 1834 | req->submit.sqe = NULL; |
561fb04a | 1835 | INIT_IO_WORK(&req->work, io_poll_complete_work); |
221c5eb2 JA |
1836 | events = READ_ONCE(sqe->poll_events); |
1837 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; | |
1838 | ||
221c5eb2 | 1839 | poll->head = NULL; |
8c838788 | 1840 | poll->done = false; |
221c5eb2 JA |
1841 | poll->canceled = false; |
1842 | ||
1843 | ipt.pt._qproc = io_poll_queue_proc; | |
1844 | ipt.pt._key = poll->events; | |
1845 | ipt.req = req; | |
1846 | ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */ | |
1847 | ||
1848 | /* initialized the list so that we can do list_empty checks */ | |
1849 | INIT_LIST_HEAD(&poll->wait.entry); | |
1850 | init_waitqueue_func_entry(&poll->wait, io_poll_wake); | |
1851 | ||
36703247 JA |
1852 | INIT_LIST_HEAD(&req->list); |
1853 | ||
221c5eb2 | 1854 | mask = vfs_poll(poll->file, &ipt.pt) & poll->events; |
221c5eb2 JA |
1855 | |
1856 | spin_lock_irq(&ctx->completion_lock); | |
8c838788 JA |
1857 | if (likely(poll->head)) { |
1858 | spin_lock(&poll->head->lock); | |
1859 | if (unlikely(list_empty(&poll->wait.entry))) { | |
1860 | if (ipt.error) | |
1861 | cancel = true; | |
1862 | ipt.error = 0; | |
1863 | mask = 0; | |
1864 | } | |
1865 | if (mask || ipt.error) | |
1866 | list_del_init(&poll->wait.entry); | |
1867 | else if (cancel) | |
1868 | WRITE_ONCE(poll->canceled, true); | |
1869 | else if (!poll->done) /* actually waiting for an event */ | |
1870 | list_add_tail(&req->list, &ctx->cancel_list); | |
1871 | spin_unlock(&poll->head->lock); | |
1872 | } | |
1873 | if (mask) { /* no async, we'd stolen it */ | |
221c5eb2 | 1874 | ipt.error = 0; |
8c838788 | 1875 | io_poll_complete(ctx, req, mask); |
221c5eb2 | 1876 | } |
221c5eb2 JA |
1877 | spin_unlock_irq(&ctx->completion_lock); |
1878 | ||
8c838788 JA |
1879 | if (mask) { |
1880 | io_cqring_ev_posted(ctx); | |
ba816ad6 | 1881 | io_put_req(req, NULL); |
221c5eb2 | 1882 | } |
8c838788 | 1883 | return ipt.error; |
221c5eb2 JA |
1884 | } |
1885 | ||
5262f567 JA |
1886 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
1887 | { | |
1888 | struct io_ring_ctx *ctx; | |
11365043 | 1889 | struct io_kiocb *req; |
5262f567 | 1890 | unsigned long flags; |
11365043 | 1891 | bool comp; |
5262f567 JA |
1892 | |
1893 | req = container_of(timer, struct io_kiocb, timeout.timer); | |
1894 | ctx = req->ctx; | |
1895 | atomic_inc(&ctx->cq_timeouts); | |
1896 | ||
1897 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
ef03681a | 1898 | /* |
11365043 JA |
1899 | * We could be racing with timeout deletion. If the list is empty, |
1900 | * then timeout lookup already found it and will be handling it. | |
ef03681a | 1901 | */ |
11365043 JA |
1902 | comp = !list_empty(&req->list); |
1903 | if (comp) { | |
1904 | struct io_kiocb *prev; | |
5262f567 | 1905 | |
11365043 JA |
1906 | /* |
1907 | * Adjust the reqs sequence before the current one because it | |
1908 | * will consume a slot in the cq_ring and the the cq_tail | |
1909 | * pointer will be increased, otherwise other timeout reqs may | |
1910 | * return in advance without waiting for enough wait_nr. | |
1911 | */ | |
1912 | prev = req; | |
1913 | list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list) | |
1914 | prev->sequence++; | |
1915 | ||
1916 | list_del_init(&req->list); | |
1917 | io_cqring_fill_event(ctx, req->user_data, -ETIME); | |
1918 | io_commit_cqring(ctx); | |
1919 | } | |
5262f567 JA |
1920 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
1921 | ||
11365043 JA |
1922 | if (comp) { |
1923 | io_cqring_ev_posted(ctx); | |
1924 | io_put_req(req, NULL); | |
1925 | } | |
1926 | return HRTIMER_NORESTART; | |
1927 | } | |
1928 | ||
1929 | /* | |
1930 | * Remove or update an existing timeout command | |
1931 | */ | |
1932 | static int io_timeout_remove(struct io_kiocb *req, | |
1933 | const struct io_uring_sqe *sqe) | |
1934 | { | |
1935 | struct io_ring_ctx *ctx = req->ctx; | |
1936 | struct io_kiocb *treq; | |
1937 | int ret = -ENOENT; | |
1938 | __u64 user_data; | |
1939 | unsigned flags; | |
1940 | ||
1941 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) | |
1942 | return -EINVAL; | |
1943 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) | |
1944 | return -EINVAL; | |
1945 | flags = READ_ONCE(sqe->timeout_flags); | |
1946 | if (flags) | |
1947 | return -EINVAL; | |
1948 | ||
1949 | user_data = READ_ONCE(sqe->addr); | |
1950 | spin_lock_irq(&ctx->completion_lock); | |
1951 | list_for_each_entry(treq, &ctx->timeout_list, list) { | |
1952 | if (user_data == treq->user_data) { | |
1953 | list_del_init(&treq->list); | |
1954 | ret = 0; | |
1955 | break; | |
1956 | } | |
1957 | } | |
1958 | ||
1959 | /* didn't find timeout */ | |
1960 | if (ret) { | |
1961 | fill_ev: | |
1962 | io_cqring_fill_event(ctx, req->user_data, ret); | |
1963 | io_commit_cqring(ctx); | |
1964 | spin_unlock_irq(&ctx->completion_lock); | |
1965 | io_cqring_ev_posted(ctx); | |
1966 | io_put_req(req, NULL); | |
1967 | return 0; | |
1968 | } | |
1969 | ||
1970 | ret = hrtimer_try_to_cancel(&treq->timeout.timer); | |
1971 | if (ret == -1) { | |
1972 | ret = -EBUSY; | |
1973 | goto fill_ev; | |
1974 | } | |
1975 | ||
1976 | io_cqring_fill_event(ctx, req->user_data, 0); | |
1977 | io_cqring_fill_event(ctx, treq->user_data, -ECANCELED); | |
1978 | io_commit_cqring(ctx); | |
1979 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 JA |
1980 | io_cqring_ev_posted(ctx); |
1981 | ||
11365043 | 1982 | io_put_req(treq, NULL); |
ba816ad6 | 1983 | io_put_req(req, NULL); |
11365043 | 1984 | return 0; |
5262f567 JA |
1985 | } |
1986 | ||
1987 | static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
1988 | { | |
5da0fb1a | 1989 | unsigned count; |
5262f567 JA |
1990 | struct io_ring_ctx *ctx = req->ctx; |
1991 | struct list_head *entry; | |
a41525ab | 1992 | enum hrtimer_mode mode; |
bdf20073 | 1993 | struct timespec64 ts; |
a1f58ba4 | 1994 | unsigned span = 0; |
a41525ab | 1995 | unsigned flags; |
5262f567 JA |
1996 | |
1997 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) | |
1998 | return -EINVAL; | |
a41525ab JA |
1999 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len != 1) |
2000 | return -EINVAL; | |
2001 | flags = READ_ONCE(sqe->timeout_flags); | |
2002 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 2003 | return -EINVAL; |
bdf20073 AB |
2004 | |
2005 | if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
2006 | return -EFAULT; |
2007 | ||
11365043 JA |
2008 | if (flags & IORING_TIMEOUT_ABS) |
2009 | mode = HRTIMER_MODE_ABS; | |
2010 | else | |
2011 | mode = HRTIMER_MODE_REL; | |
2012 | ||
2013 | hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, mode); | |
2014 | ||
5262f567 JA |
2015 | /* |
2016 | * sqe->off holds how many events that need to occur for this | |
2017 | * timeout event to be satisfied. | |
2018 | */ | |
2019 | count = READ_ONCE(sqe->off); | |
2020 | if (!count) | |
2021 | count = 1; | |
2022 | ||
2023 | req->sequence = ctx->cached_sq_head + count - 1; | |
5da0fb1a | 2024 | /* reuse it to store the count */ |
2025 | req->submit.sequence = count; | |
5262f567 JA |
2026 | req->flags |= REQ_F_TIMEOUT; |
2027 | ||
2028 | /* | |
2029 | * Insertion sort, ensuring the first entry in the list is always | |
2030 | * the one we need first. | |
2031 | */ | |
5262f567 JA |
2032 | spin_lock_irq(&ctx->completion_lock); |
2033 | list_for_each_prev(entry, &ctx->timeout_list) { | |
2034 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); | |
5da0fb1a | 2035 | unsigned nxt_sq_head; |
2036 | long long tmp, tmp_nxt; | |
5262f567 | 2037 | |
5da0fb1a | 2038 | /* |
2039 | * Since cached_sq_head + count - 1 can overflow, use type long | |
2040 | * long to store it. | |
2041 | */ | |
2042 | tmp = (long long)ctx->cached_sq_head + count - 1; | |
2043 | nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1; | |
2044 | tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1; | |
2045 | ||
2046 | /* | |
2047 | * cached_sq_head may overflow, and it will never overflow twice | |
2048 | * once there is some timeout req still be valid. | |
2049 | */ | |
2050 | if (ctx->cached_sq_head < nxt_sq_head) | |
8b07a65a | 2051 | tmp += UINT_MAX; |
5da0fb1a | 2052 | |
a1f58ba4 | 2053 | if (tmp > tmp_nxt) |
5262f567 | 2054 | break; |
a1f58ba4 | 2055 | |
2056 | /* | |
2057 | * Sequence of reqs after the insert one and itself should | |
2058 | * be adjusted because each timeout req consumes a slot. | |
2059 | */ | |
2060 | span++; | |
2061 | nxt->sequence++; | |
5262f567 | 2062 | } |
a1f58ba4 | 2063 | req->sequence -= span; |
5262f567 JA |
2064 | list_add(&req->list, entry); |
2065 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 2066 | req->timeout.timer.function = io_timeout_fn; |
a41525ab | 2067 | hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), mode); |
5262f567 JA |
2068 | return 0; |
2069 | } | |
2070 | ||
de0617e4 JA |
2071 | static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req, |
2072 | const struct io_uring_sqe *sqe) | |
2073 | { | |
2074 | struct io_uring_sqe *sqe_copy; | |
2075 | ||
2076 | if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) | |
2077 | return 0; | |
2078 | ||
2079 | sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL); | |
2080 | if (!sqe_copy) | |
2081 | return -EAGAIN; | |
2082 | ||
2083 | spin_lock_irq(&ctx->completion_lock); | |
2084 | if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) { | |
2085 | spin_unlock_irq(&ctx->completion_lock); | |
2086 | kfree(sqe_copy); | |
2087 | return 0; | |
2088 | } | |
2089 | ||
2090 | memcpy(sqe_copy, sqe, sizeof(*sqe_copy)); | |
2091 | req->submit.sqe = sqe_copy; | |
2092 | ||
c826bd7a | 2093 | trace_io_uring_defer(ctx, req, false); |
de0617e4 JA |
2094 | list_add_tail(&req->list, &ctx->defer_list); |
2095 | spin_unlock_irq(&ctx->completion_lock); | |
2096 | return -EIOCBQUEUED; | |
2097 | } | |
2098 | ||
2b188cc1 | 2099 | static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
ba816ad6 JA |
2100 | const struct sqe_submit *s, struct io_kiocb **nxt, |
2101 | bool force_nonblock) | |
2b188cc1 | 2102 | { |
e0c5c576 | 2103 | int ret, opcode; |
2b188cc1 | 2104 | |
9e645e11 JA |
2105 | req->user_data = READ_ONCE(s->sqe->user_data); |
2106 | ||
2b188cc1 JA |
2107 | opcode = READ_ONCE(s->sqe->opcode); |
2108 | switch (opcode) { | |
2109 | case IORING_OP_NOP: | |
2110 | ret = io_nop(req, req->user_data); | |
2111 | break; | |
2112 | case IORING_OP_READV: | |
edafccee JA |
2113 | if (unlikely(s->sqe->buf_index)) |
2114 | return -EINVAL; | |
ba816ad6 | 2115 | ret = io_read(req, s, nxt, force_nonblock); |
2b188cc1 JA |
2116 | break; |
2117 | case IORING_OP_WRITEV: | |
edafccee JA |
2118 | if (unlikely(s->sqe->buf_index)) |
2119 | return -EINVAL; | |
ba816ad6 | 2120 | ret = io_write(req, s, nxt, force_nonblock); |
edafccee JA |
2121 | break; |
2122 | case IORING_OP_READ_FIXED: | |
ba816ad6 | 2123 | ret = io_read(req, s, nxt, force_nonblock); |
edafccee JA |
2124 | break; |
2125 | case IORING_OP_WRITE_FIXED: | |
ba816ad6 | 2126 | ret = io_write(req, s, nxt, force_nonblock); |
2b188cc1 | 2127 | break; |
c992fe29 | 2128 | case IORING_OP_FSYNC: |
ba816ad6 | 2129 | ret = io_fsync(req, s->sqe, nxt, force_nonblock); |
c992fe29 | 2130 | break; |
221c5eb2 JA |
2131 | case IORING_OP_POLL_ADD: |
2132 | ret = io_poll_add(req, s->sqe); | |
2133 | break; | |
2134 | case IORING_OP_POLL_REMOVE: | |
2135 | ret = io_poll_remove(req, s->sqe); | |
2136 | break; | |
5d17b4a4 | 2137 | case IORING_OP_SYNC_FILE_RANGE: |
ba816ad6 | 2138 | ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock); |
5d17b4a4 | 2139 | break; |
0fa03c62 | 2140 | case IORING_OP_SENDMSG: |
ba816ad6 | 2141 | ret = io_sendmsg(req, s->sqe, nxt, force_nonblock); |
0fa03c62 | 2142 | break; |
aa1fa28f | 2143 | case IORING_OP_RECVMSG: |
ba816ad6 | 2144 | ret = io_recvmsg(req, s->sqe, nxt, force_nonblock); |
aa1fa28f | 2145 | break; |
5262f567 JA |
2146 | case IORING_OP_TIMEOUT: |
2147 | ret = io_timeout(req, s->sqe); | |
2148 | break; | |
11365043 JA |
2149 | case IORING_OP_TIMEOUT_REMOVE: |
2150 | ret = io_timeout_remove(req, s->sqe); | |
2151 | break; | |
2b188cc1 JA |
2152 | default: |
2153 | ret = -EINVAL; | |
2154 | break; | |
2155 | } | |
2156 | ||
def596e9 JA |
2157 | if (ret) |
2158 | return ret; | |
2159 | ||
2160 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
9e645e11 | 2161 | if (req->result == -EAGAIN) |
def596e9 JA |
2162 | return -EAGAIN; |
2163 | ||
2164 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
ba5290cc | 2165 | if (s->in_async) |
def596e9 JA |
2166 | mutex_lock(&ctx->uring_lock); |
2167 | io_iopoll_req_issued(req); | |
ba5290cc | 2168 | if (s->in_async) |
def596e9 JA |
2169 | mutex_unlock(&ctx->uring_lock); |
2170 | } | |
2171 | ||
2172 | return 0; | |
2b188cc1 JA |
2173 | } |
2174 | ||
561fb04a | 2175 | static void io_wq_submit_work(struct io_wq_work **workptr) |
2b188cc1 | 2176 | { |
561fb04a | 2177 | struct io_wq_work *work = *workptr; |
2b188cc1 | 2178 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
2b188cc1 | 2179 | struct io_ring_ctx *ctx = req->ctx; |
561fb04a JA |
2180 | struct sqe_submit *s = &req->submit; |
2181 | const struct io_uring_sqe *sqe = s->sqe; | |
2182 | struct io_kiocb *nxt = NULL; | |
2183 | int ret = 0; | |
2b188cc1 | 2184 | |
561fb04a JA |
2185 | /* Ensure we clear previously set non-block flag */ |
2186 | req->rw.ki_flags &= ~IOCB_NOWAIT; | |
2b188cc1 | 2187 | |
561fb04a JA |
2188 | if (work->flags & IO_WQ_WORK_CANCEL) |
2189 | ret = -ECANCELED; | |
31b51510 | 2190 | |
561fb04a JA |
2191 | if (!ret) { |
2192 | s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0; | |
2193 | s->in_async = true; | |
2194 | do { | |
2195 | ret = __io_submit_sqe(ctx, req, s, &nxt, false); | |
2196 | /* | |
2197 | * We can get EAGAIN for polled IO even though we're | |
2198 | * forcing a sync submission from here, since we can't | |
2199 | * wait for request slots on the block side. | |
2200 | */ | |
2201 | if (ret != -EAGAIN) | |
2202 | break; | |
2203 | cond_resched(); | |
2204 | } while (1); | |
2205 | } | |
31b51510 | 2206 | |
561fb04a JA |
2207 | /* drop submission reference */ |
2208 | io_put_req(req, NULL); | |
817869d2 | 2209 | |
561fb04a JA |
2210 | if (ret) { |
2211 | io_cqring_add_event(ctx, sqe->user_data, ret); | |
ba816ad6 | 2212 | io_put_req(req, NULL); |
2b188cc1 | 2213 | } |
31b51510 | 2214 | |
561fb04a JA |
2215 | /* async context always use a copy of the sqe */ |
2216 | kfree(sqe); | |
31b51510 | 2217 | |
561fb04a JA |
2218 | /* if a dependent link is ready, pass it back */ |
2219 | if (!ret && nxt) { | |
2220 | io_prep_async_work(nxt); | |
2221 | *workptr = &nxt->work; | |
31b51510 | 2222 | } |
2b188cc1 JA |
2223 | } |
2224 | ||
09bb8394 JA |
2225 | static bool io_op_needs_file(const struct io_uring_sqe *sqe) |
2226 | { | |
2227 | int op = READ_ONCE(sqe->opcode); | |
2228 | ||
2229 | switch (op) { | |
2230 | case IORING_OP_NOP: | |
2231 | case IORING_OP_POLL_REMOVE: | |
2232 | return false; | |
2233 | default: | |
2234 | return true; | |
2235 | } | |
2236 | } | |
2237 | ||
2238 | static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s, | |
2239 | struct io_submit_state *state, struct io_kiocb *req) | |
2240 | { | |
2241 | unsigned flags; | |
2242 | int fd; | |
2243 | ||
2244 | flags = READ_ONCE(s->sqe->flags); | |
2245 | fd = READ_ONCE(s->sqe->fd); | |
2246 | ||
4fe2c963 | 2247 | if (flags & IOSQE_IO_DRAIN) |
de0617e4 | 2248 | req->flags |= REQ_F_IO_DRAIN; |
4fe2c963 JL |
2249 | /* |
2250 | * All io need record the previous position, if LINK vs DARIN, | |
2251 | * it can be used to mark the position of the first IO in the | |
2252 | * link list. | |
2253 | */ | |
2254 | req->sequence = s->sequence; | |
de0617e4 | 2255 | |
60c112b0 | 2256 | if (!io_op_needs_file(s->sqe)) |
09bb8394 | 2257 | return 0; |
09bb8394 JA |
2258 | |
2259 | if (flags & IOSQE_FIXED_FILE) { | |
2260 | if (unlikely(!ctx->user_files || | |
2261 | (unsigned) fd >= ctx->nr_user_files)) | |
2262 | return -EBADF; | |
08a45173 JA |
2263 | if (!ctx->user_files[fd]) |
2264 | return -EBADF; | |
09bb8394 JA |
2265 | req->file = ctx->user_files[fd]; |
2266 | req->flags |= REQ_F_FIXED_FILE; | |
2267 | } else { | |
2268 | if (s->needs_fixed_file) | |
2269 | return -EBADF; | |
c826bd7a | 2270 | trace_io_uring_file_get(ctx, fd); |
09bb8394 JA |
2271 | req->file = io_file_get(state, fd); |
2272 | if (unlikely(!req->file)) | |
2273 | return -EBADF; | |
2274 | } | |
2275 | ||
2276 | return 0; | |
2277 | } | |
2278 | ||
4fe2c963 | 2279 | static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
bc808bce | 2280 | struct sqe_submit *s) |
2b188cc1 | 2281 | { |
e0c5c576 | 2282 | int ret; |
2b188cc1 | 2283 | |
ba816ad6 | 2284 | ret = __io_submit_sqe(ctx, req, s, NULL, true); |
491381ce JA |
2285 | |
2286 | /* | |
2287 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
2288 | * doesn't support non-blocking read/write attempts | |
2289 | */ | |
2290 | if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) || | |
2291 | (req->flags & REQ_F_MUST_PUNT))) { | |
2b188cc1 JA |
2292 | struct io_uring_sqe *sqe_copy; |
2293 | ||
954dab19 | 2294 | sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL); |
2b188cc1 | 2295 | if (sqe_copy) { |
2b188cc1 | 2296 | s->sqe = sqe_copy; |
2b188cc1 | 2297 | memcpy(&req->submit, s, sizeof(*s)); |
561fb04a | 2298 | io_queue_async_work(ctx, req); |
e65ef56d JA |
2299 | |
2300 | /* | |
2301 | * Queued up for async execution, worker will release | |
9e645e11 | 2302 | * submit reference when the iocb is actually submitted. |
e65ef56d JA |
2303 | */ |
2304 | return 0; | |
2b188cc1 JA |
2305 | } |
2306 | } | |
e65ef56d JA |
2307 | |
2308 | /* drop submission reference */ | |
ba816ad6 | 2309 | io_put_req(req, NULL); |
e65ef56d JA |
2310 | |
2311 | /* and drop final reference, if we failed */ | |
9e645e11 JA |
2312 | if (ret) { |
2313 | io_cqring_add_event(ctx, req->user_data, ret); | |
2314 | if (req->flags & REQ_F_LINK) | |
2315 | req->flags |= REQ_F_FAIL_LINK; | |
ba816ad6 | 2316 | io_put_req(req, NULL); |
9e645e11 | 2317 | } |
2b188cc1 JA |
2318 | |
2319 | return ret; | |
2320 | } | |
2321 | ||
4fe2c963 | 2322 | static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
bc808bce | 2323 | struct sqe_submit *s) |
4fe2c963 JL |
2324 | { |
2325 | int ret; | |
2326 | ||
2327 | ret = io_req_defer(ctx, req, s->sqe); | |
2328 | if (ret) { | |
2329 | if (ret != -EIOCBQUEUED) { | |
ba816ad6 | 2330 | io_free_req(req, NULL); |
4fe2c963 JL |
2331 | io_cqring_add_event(ctx, s->sqe->user_data, ret); |
2332 | } | |
2333 | return 0; | |
2334 | } | |
2335 | ||
bc808bce | 2336 | return __io_queue_sqe(ctx, req, s); |
4fe2c963 JL |
2337 | } |
2338 | ||
2339 | static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req, | |
bc808bce | 2340 | struct sqe_submit *s, struct io_kiocb *shadow) |
4fe2c963 JL |
2341 | { |
2342 | int ret; | |
2343 | int need_submit = false; | |
2344 | ||
2345 | if (!shadow) | |
bc808bce | 2346 | return io_queue_sqe(ctx, req, s); |
4fe2c963 JL |
2347 | |
2348 | /* | |
2349 | * Mark the first IO in link list as DRAIN, let all the following | |
2350 | * IOs enter the defer list. all IO needs to be completed before link | |
2351 | * list. | |
2352 | */ | |
2353 | req->flags |= REQ_F_IO_DRAIN; | |
2354 | ret = io_req_defer(ctx, req, s->sqe); | |
2355 | if (ret) { | |
2356 | if (ret != -EIOCBQUEUED) { | |
ba816ad6 | 2357 | io_free_req(req, NULL); |
7b20238d | 2358 | __io_free_req(shadow); |
4fe2c963 JL |
2359 | io_cqring_add_event(ctx, s->sqe->user_data, ret); |
2360 | return 0; | |
2361 | } | |
2362 | } else { | |
2363 | /* | |
2364 | * If ret == 0 means that all IOs in front of link io are | |
2365 | * running done. let's queue link head. | |
2366 | */ | |
2367 | need_submit = true; | |
2368 | } | |
2369 | ||
2370 | /* Insert shadow req to defer_list, blocking next IOs */ | |
2371 | spin_lock_irq(&ctx->completion_lock); | |
c826bd7a | 2372 | trace_io_uring_defer(ctx, shadow, true); |
4fe2c963 JL |
2373 | list_add_tail(&shadow->list, &ctx->defer_list); |
2374 | spin_unlock_irq(&ctx->completion_lock); | |
2375 | ||
2376 | if (need_submit) | |
bc808bce | 2377 | return __io_queue_sqe(ctx, req, s); |
4fe2c963 JL |
2378 | |
2379 | return 0; | |
2380 | } | |
2381 | ||
9e645e11 JA |
2382 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK) |
2383 | ||
2384 | static void io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s, | |
bc808bce | 2385 | struct io_submit_state *state, struct io_kiocb **link) |
9e645e11 JA |
2386 | { |
2387 | struct io_uring_sqe *sqe_copy; | |
2388 | struct io_kiocb *req; | |
2389 | int ret; | |
2390 | ||
2391 | /* enforce forwards compatibility on users */ | |
2392 | if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) { | |
2393 | ret = -EINVAL; | |
2394 | goto err; | |
2395 | } | |
2396 | ||
2397 | req = io_get_req(ctx, state); | |
2398 | if (unlikely(!req)) { | |
2399 | ret = -EAGAIN; | |
2400 | goto err; | |
2401 | } | |
2402 | ||
2403 | ret = io_req_set_file(ctx, s, state, req); | |
2404 | if (unlikely(ret)) { | |
2405 | err_req: | |
ba816ad6 | 2406 | io_free_req(req, NULL); |
9e645e11 JA |
2407 | err: |
2408 | io_cqring_add_event(ctx, s->sqe->user_data, ret); | |
2409 | return; | |
2410 | } | |
2411 | ||
84d55dc5 PB |
2412 | req->user_data = s->sqe->user_data; |
2413 | ||
9e645e11 JA |
2414 | /* |
2415 | * If we already have a head request, queue this one for async | |
2416 | * submittal once the head completes. If we don't have a head but | |
2417 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
2418 | * submitted sync once the chain is complete. If none of those | |
2419 | * conditions are true (normal request), then just queue it. | |
2420 | */ | |
2421 | if (*link) { | |
2422 | struct io_kiocb *prev = *link; | |
2423 | ||
2424 | sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL); | |
2425 | if (!sqe_copy) { | |
2426 | ret = -EAGAIN; | |
2427 | goto err_req; | |
2428 | } | |
2429 | ||
2430 | s->sqe = sqe_copy; | |
2431 | memcpy(&req->submit, s, sizeof(*s)); | |
c826bd7a | 2432 | trace_io_uring_link(ctx, req, prev); |
9e645e11 JA |
2433 | list_add_tail(&req->list, &prev->link_list); |
2434 | } else if (s->sqe->flags & IOSQE_IO_LINK) { | |
2435 | req->flags |= REQ_F_LINK; | |
2436 | ||
2437 | memcpy(&req->submit, s, sizeof(*s)); | |
2438 | INIT_LIST_HEAD(&req->link_list); | |
2439 | *link = req; | |
2440 | } else { | |
bc808bce | 2441 | io_queue_sqe(ctx, req, s); |
9e645e11 JA |
2442 | } |
2443 | } | |
2444 | ||
9a56a232 JA |
2445 | /* |
2446 | * Batched submission is done, ensure local IO is flushed out. | |
2447 | */ | |
2448 | static void io_submit_state_end(struct io_submit_state *state) | |
2449 | { | |
2450 | blk_finish_plug(&state->plug); | |
3d6770fb | 2451 | io_file_put(state); |
2579f913 JA |
2452 | if (state->free_reqs) |
2453 | kmem_cache_free_bulk(req_cachep, state->free_reqs, | |
2454 | &state->reqs[state->cur_req]); | |
9a56a232 JA |
2455 | } |
2456 | ||
2457 | /* | |
2458 | * Start submission side cache. | |
2459 | */ | |
2460 | static void io_submit_state_start(struct io_submit_state *state, | |
2461 | struct io_ring_ctx *ctx, unsigned max_ios) | |
2462 | { | |
2463 | blk_start_plug(&state->plug); | |
2579f913 | 2464 | state->free_reqs = 0; |
9a56a232 JA |
2465 | state->file = NULL; |
2466 | state->ios_left = max_ios; | |
2467 | } | |
2468 | ||
2b188cc1 JA |
2469 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
2470 | { | |
75b28aff | 2471 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 2472 | |
75b28aff | 2473 | if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) { |
2b188cc1 JA |
2474 | /* |
2475 | * Ensure any loads from the SQEs are done at this point, | |
2476 | * since once we write the new head, the application could | |
2477 | * write new data to them. | |
2478 | */ | |
75b28aff | 2479 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
2b188cc1 JA |
2480 | } |
2481 | } | |
2482 | ||
2b188cc1 JA |
2483 | /* |
2484 | * Fetch an sqe, if one is available. Note that s->sqe will point to memory | |
2485 | * that is mapped by userspace. This means that care needs to be taken to | |
2486 | * ensure that reads are stable, as we cannot rely on userspace always | |
2487 | * being a good citizen. If members of the sqe are validated and then later | |
2488 | * used, it's important that those reads are done through READ_ONCE() to | |
2489 | * prevent a re-load down the line. | |
2490 | */ | |
2491 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s) | |
2492 | { | |
75b28aff HV |
2493 | struct io_rings *rings = ctx->rings; |
2494 | u32 *sq_array = ctx->sq_array; | |
2b188cc1 JA |
2495 | unsigned head; |
2496 | ||
2497 | /* | |
2498 | * The cached sq head (or cq tail) serves two purposes: | |
2499 | * | |
2500 | * 1) allows us to batch the cost of updating the user visible | |
2501 | * head updates. | |
2502 | * 2) allows the kernel side to track the head on its own, even | |
2503 | * though the application is the one updating it. | |
2504 | */ | |
2505 | head = ctx->cached_sq_head; | |
e523a29c | 2506 | /* make sure SQ entry isn't read before tail */ |
75b28aff | 2507 | if (head == smp_load_acquire(&rings->sq.tail)) |
2b188cc1 JA |
2508 | return false; |
2509 | ||
75b28aff | 2510 | head = READ_ONCE(sq_array[head & ctx->sq_mask]); |
2b188cc1 | 2511 | if (head < ctx->sq_entries) { |
2b188cc1 | 2512 | s->sqe = &ctx->sq_sqes[head]; |
8776f3fa | 2513 | s->sequence = ctx->cached_sq_head; |
2b188cc1 JA |
2514 | ctx->cached_sq_head++; |
2515 | return true; | |
2516 | } | |
2517 | ||
2518 | /* drop invalid entries */ | |
2519 | ctx->cached_sq_head++; | |
498ccd9e JA |
2520 | ctx->cached_sq_dropped++; |
2521 | WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped); | |
2b188cc1 JA |
2522 | return false; |
2523 | } | |
2524 | ||
fb5ccc98 | 2525 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr, |
95a1b3ff | 2526 | struct mm_struct **mm) |
6c271ce2 JA |
2527 | { |
2528 | struct io_submit_state state, *statep = NULL; | |
9e645e11 | 2529 | struct io_kiocb *link = NULL; |
4fe2c963 | 2530 | struct io_kiocb *shadow_req = NULL; |
9e645e11 JA |
2531 | bool prev_was_link = false; |
2532 | int i, submitted = 0; | |
95a1b3ff | 2533 | bool mm_fault = false; |
6c271ce2 JA |
2534 | |
2535 | if (nr > IO_PLUG_THRESHOLD) { | |
2536 | io_submit_state_start(&state, ctx, nr); | |
2537 | statep = &state; | |
2538 | } | |
2539 | ||
2540 | for (i = 0; i < nr; i++) { | |
fb5ccc98 PB |
2541 | struct sqe_submit s; |
2542 | ||
2543 | if (!io_get_sqring(ctx, &s)) | |
2544 | break; | |
2545 | ||
95a1b3ff PB |
2546 | if (io_sqe_needs_user(s.sqe) && !*mm) { |
2547 | mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm); | |
2548 | if (!mm_fault) { | |
2549 | use_mm(ctx->sqo_mm); | |
2550 | *mm = ctx->sqo_mm; | |
2551 | } | |
2552 | } | |
2553 | ||
9e645e11 JA |
2554 | /* |
2555 | * If previous wasn't linked and we have a linked command, | |
2556 | * that's the end of the chain. Submit the previous link. | |
2557 | */ | |
2558 | if (!prev_was_link && link) { | |
bc808bce | 2559 | io_queue_link_head(ctx, link, &link->submit, shadow_req); |
9e645e11 | 2560 | link = NULL; |
5f5ad9ce | 2561 | shadow_req = NULL; |
9e645e11 | 2562 | } |
fb5ccc98 | 2563 | prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0; |
9e645e11 | 2564 | |
fb5ccc98 | 2565 | if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) { |
4fe2c963 JL |
2566 | if (!shadow_req) { |
2567 | shadow_req = io_get_req(ctx, NULL); | |
a1041c27 JL |
2568 | if (unlikely(!shadow_req)) |
2569 | goto out; | |
4fe2c963 JL |
2570 | shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN); |
2571 | refcount_dec(&shadow_req->refs); | |
2572 | } | |
fb5ccc98 | 2573 | shadow_req->sequence = s.sequence; |
4fe2c963 JL |
2574 | } |
2575 | ||
a1041c27 | 2576 | out: |
95a1b3ff PB |
2577 | s.has_user = *mm != NULL; |
2578 | s.in_async = true; | |
2579 | s.needs_fixed_file = true; | |
2580 | trace_io_uring_submit_sqe(ctx, true, true); | |
2581 | io_submit_sqe(ctx, &s, statep, &link); | |
2582 | submitted++; | |
6c271ce2 JA |
2583 | } |
2584 | ||
9e645e11 | 2585 | if (link) |
bc808bce | 2586 | io_queue_link_head(ctx, link, &link->submit, shadow_req); |
6c271ce2 JA |
2587 | if (statep) |
2588 | io_submit_state_end(&state); | |
2589 | ||
2590 | return submitted; | |
2591 | } | |
2592 | ||
2593 | static int io_sq_thread(void *data) | |
2594 | { | |
6c271ce2 JA |
2595 | struct io_ring_ctx *ctx = data; |
2596 | struct mm_struct *cur_mm = NULL; | |
2597 | mm_segment_t old_fs; | |
2598 | DEFINE_WAIT(wait); | |
2599 | unsigned inflight; | |
2600 | unsigned long timeout; | |
2601 | ||
a4c0b3de JL |
2602 | complete(&ctx->sqo_thread_started); |
2603 | ||
6c271ce2 JA |
2604 | old_fs = get_fs(); |
2605 | set_fs(USER_DS); | |
2606 | ||
2607 | timeout = inflight = 0; | |
2bbcd6d3 | 2608 | while (!kthread_should_park()) { |
fb5ccc98 | 2609 | unsigned int to_submit; |
6c271ce2 JA |
2610 | |
2611 | if (inflight) { | |
2612 | unsigned nr_events = 0; | |
2613 | ||
2614 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
2b2ed975 JA |
2615 | /* |
2616 | * inflight is the count of the maximum possible | |
2617 | * entries we submitted, but it can be smaller | |
2618 | * if we dropped some of them. If we don't have | |
2619 | * poll entries available, then we know that we | |
2620 | * have nothing left to poll for. Reset the | |
2621 | * inflight count to zero in that case. | |
2622 | */ | |
2623 | mutex_lock(&ctx->uring_lock); | |
2624 | if (!list_empty(&ctx->poll_list)) | |
2625 | __io_iopoll_check(ctx, &nr_events, 0); | |
2626 | else | |
2627 | inflight = 0; | |
2628 | mutex_unlock(&ctx->uring_lock); | |
6c271ce2 JA |
2629 | } else { |
2630 | /* | |
2631 | * Normal IO, just pretend everything completed. | |
2632 | * We don't have to poll completions for that. | |
2633 | */ | |
2634 | nr_events = inflight; | |
2635 | } | |
2636 | ||
2637 | inflight -= nr_events; | |
2638 | if (!inflight) | |
2639 | timeout = jiffies + ctx->sq_thread_idle; | |
2640 | } | |
2641 | ||
fb5ccc98 PB |
2642 | to_submit = io_sqring_entries(ctx); |
2643 | if (!to_submit) { | |
6c271ce2 JA |
2644 | /* |
2645 | * We're polling. If we're within the defined idle | |
2646 | * period, then let us spin without work before going | |
2647 | * to sleep. | |
2648 | */ | |
2649 | if (inflight || !time_after(jiffies, timeout)) { | |
9831a90c | 2650 | cond_resched(); |
6c271ce2 JA |
2651 | continue; |
2652 | } | |
2653 | ||
2654 | /* | |
2655 | * Drop cur_mm before scheduling, we can't hold it for | |
2656 | * long periods (or over schedule()). Do this before | |
2657 | * adding ourselves to the waitqueue, as the unuse/drop | |
2658 | * may sleep. | |
2659 | */ | |
2660 | if (cur_mm) { | |
2661 | unuse_mm(cur_mm); | |
2662 | mmput(cur_mm); | |
2663 | cur_mm = NULL; | |
2664 | } | |
2665 | ||
2666 | prepare_to_wait(&ctx->sqo_wait, &wait, | |
2667 | TASK_INTERRUPTIBLE); | |
2668 | ||
2669 | /* Tell userspace we may need a wakeup call */ | |
75b28aff | 2670 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
0d7bae69 SB |
2671 | /* make sure to read SQ tail after writing flags */ |
2672 | smp_mb(); | |
6c271ce2 | 2673 | |
fb5ccc98 PB |
2674 | to_submit = io_sqring_entries(ctx); |
2675 | if (!to_submit) { | |
2bbcd6d3 | 2676 | if (kthread_should_park()) { |
6c271ce2 JA |
2677 | finish_wait(&ctx->sqo_wait, &wait); |
2678 | break; | |
2679 | } | |
2680 | if (signal_pending(current)) | |
2681 | flush_signals(current); | |
2682 | schedule(); | |
2683 | finish_wait(&ctx->sqo_wait, &wait); | |
2684 | ||
75b28aff | 2685 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
6c271ce2 JA |
2686 | continue; |
2687 | } | |
2688 | finish_wait(&ctx->sqo_wait, &wait); | |
2689 | ||
75b28aff | 2690 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
6c271ce2 JA |
2691 | } |
2692 | ||
fb5ccc98 | 2693 | to_submit = min(to_submit, ctx->sq_entries); |
95a1b3ff | 2694 | inflight += io_submit_sqes(ctx, to_submit, &cur_mm); |
6c271ce2 JA |
2695 | |
2696 | /* Commit SQ ring head once we've consumed all SQEs */ | |
2697 | io_commit_sqring(ctx); | |
2698 | } | |
2699 | ||
2700 | set_fs(old_fs); | |
2701 | if (cur_mm) { | |
2702 | unuse_mm(cur_mm); | |
2703 | mmput(cur_mm); | |
2704 | } | |
06058632 | 2705 | |
2bbcd6d3 | 2706 | kthread_parkme(); |
06058632 | 2707 | |
6c271ce2 JA |
2708 | return 0; |
2709 | } | |
2710 | ||
bc808bce | 2711 | static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit) |
2b188cc1 | 2712 | { |
9a56a232 | 2713 | struct io_submit_state state, *statep = NULL; |
9e645e11 | 2714 | struct io_kiocb *link = NULL; |
4fe2c963 | 2715 | struct io_kiocb *shadow_req = NULL; |
9e645e11 | 2716 | bool prev_was_link = false; |
5c8b0b54 | 2717 | int i, submit = 0; |
2b188cc1 | 2718 | |
9a56a232 JA |
2719 | if (to_submit > IO_PLUG_THRESHOLD) { |
2720 | io_submit_state_start(&state, ctx, to_submit); | |
2721 | statep = &state; | |
2722 | } | |
2b188cc1 JA |
2723 | |
2724 | for (i = 0; i < to_submit; i++) { | |
2725 | struct sqe_submit s; | |
2726 | ||
2727 | if (!io_get_sqring(ctx, &s)) | |
2728 | break; | |
2729 | ||
9e645e11 JA |
2730 | /* |
2731 | * If previous wasn't linked and we have a linked command, | |
2732 | * that's the end of the chain. Submit the previous link. | |
2733 | */ | |
2734 | if (!prev_was_link && link) { | |
bc808bce | 2735 | io_queue_link_head(ctx, link, &link->submit, shadow_req); |
9e645e11 | 2736 | link = NULL; |
5f5ad9ce | 2737 | shadow_req = NULL; |
9e645e11 JA |
2738 | } |
2739 | prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0; | |
2740 | ||
4fe2c963 JL |
2741 | if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) { |
2742 | if (!shadow_req) { | |
2743 | shadow_req = io_get_req(ctx, NULL); | |
a1041c27 JL |
2744 | if (unlikely(!shadow_req)) |
2745 | goto out; | |
4fe2c963 JL |
2746 | shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN); |
2747 | refcount_dec(&shadow_req->refs); | |
2748 | } | |
2749 | shadow_req->sequence = s.sequence; | |
2750 | } | |
2751 | ||
a1041c27 | 2752 | out: |
2b188cc1 | 2753 | s.has_user = true; |
ba5290cc | 2754 | s.in_async = false; |
6c271ce2 | 2755 | s.needs_fixed_file = false; |
5c8b0b54 | 2756 | submit++; |
c826bd7a | 2757 | trace_io_uring_submit_sqe(ctx, true, false); |
bc808bce | 2758 | io_submit_sqe(ctx, &s, statep, &link); |
2b188cc1 | 2759 | } |
2b188cc1 | 2760 | |
9e645e11 | 2761 | if (link) |
bc808bce | 2762 | io_queue_link_head(ctx, link, &link->submit, shadow_req); |
9a56a232 JA |
2763 | if (statep) |
2764 | io_submit_state_end(statep); | |
2b188cc1 | 2765 | |
935d1e45 PB |
2766 | io_commit_sqring(ctx); |
2767 | ||
5c8b0b54 | 2768 | return submit; |
2b188cc1 JA |
2769 | } |
2770 | ||
bda52162 JA |
2771 | struct io_wait_queue { |
2772 | struct wait_queue_entry wq; | |
2773 | struct io_ring_ctx *ctx; | |
2774 | unsigned to_wait; | |
2775 | unsigned nr_timeouts; | |
2776 | }; | |
2777 | ||
2778 | static inline bool io_should_wake(struct io_wait_queue *iowq) | |
2779 | { | |
2780 | struct io_ring_ctx *ctx = iowq->ctx; | |
2781 | ||
2782 | /* | |
2783 | * Wake up if we have enough events, or if a timeout occured since we | |
2784 | * started waiting. For timeouts, we always want to return to userspace, | |
2785 | * regardless of event count. | |
2786 | */ | |
2787 | return io_cqring_events(ctx->rings) >= iowq->to_wait || | |
2788 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; | |
2789 | } | |
2790 | ||
2791 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
2792 | int wake_flags, void *key) | |
2793 | { | |
2794 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
2795 | wq); | |
2796 | ||
2797 | if (!io_should_wake(iowq)) | |
2798 | return -1; | |
2799 | ||
2800 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
2801 | } | |
2802 | ||
2b188cc1 JA |
2803 | /* |
2804 | * Wait until events become available, if we don't already have some. The | |
2805 | * application must reap them itself, as they reside on the shared cq ring. | |
2806 | */ | |
2807 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
2808 | const sigset_t __user *sig, size_t sigsz) | |
2809 | { | |
bda52162 JA |
2810 | struct io_wait_queue iowq = { |
2811 | .wq = { | |
2812 | .private = current, | |
2813 | .func = io_wake_function, | |
2814 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
2815 | }, | |
2816 | .ctx = ctx, | |
2817 | .to_wait = min_events, | |
2818 | }; | |
75b28aff | 2819 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
2820 | int ret; |
2821 | ||
75b28aff | 2822 | if (io_cqring_events(rings) >= min_events) |
2b188cc1 JA |
2823 | return 0; |
2824 | ||
2825 | if (sig) { | |
9e75ad5d AB |
2826 | #ifdef CONFIG_COMPAT |
2827 | if (in_compat_syscall()) | |
2828 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 2829 | sigsz); |
9e75ad5d AB |
2830 | else |
2831 | #endif | |
b772434b | 2832 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 2833 | |
2b188cc1 JA |
2834 | if (ret) |
2835 | return ret; | |
2836 | } | |
2837 | ||
bda52162 JA |
2838 | ret = 0; |
2839 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); | |
c826bd7a | 2840 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 JA |
2841 | do { |
2842 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, | |
2843 | TASK_INTERRUPTIBLE); | |
2844 | if (io_should_wake(&iowq)) | |
2845 | break; | |
2846 | schedule(); | |
2847 | if (signal_pending(current)) { | |
2848 | ret = -ERESTARTSYS; | |
2849 | break; | |
2850 | } | |
2851 | } while (1); | |
2852 | finish_wait(&ctx->wait, &iowq.wq); | |
2853 | ||
b772434b | 2854 | restore_saved_sigmask_unless(ret == -ERESTARTSYS); |
97abc889 ON |
2855 | if (ret == -ERESTARTSYS) |
2856 | ret = -EINTR; | |
2b188cc1 | 2857 | |
75b28aff | 2858 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
2859 | } |
2860 | ||
6b06314c JA |
2861 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
2862 | { | |
2863 | #if defined(CONFIG_UNIX) | |
2864 | if (ctx->ring_sock) { | |
2865 | struct sock *sock = ctx->ring_sock->sk; | |
2866 | struct sk_buff *skb; | |
2867 | ||
2868 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
2869 | kfree_skb(skb); | |
2870 | } | |
2871 | #else | |
2872 | int i; | |
2873 | ||
2874 | for (i = 0; i < ctx->nr_user_files; i++) | |
08a45173 JA |
2875 | if (ctx->user_files[i]) |
2876 | fput(ctx->user_files[i]); | |
6b06314c JA |
2877 | #endif |
2878 | } | |
2879 | ||
2880 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) | |
2881 | { | |
2882 | if (!ctx->user_files) | |
2883 | return -ENXIO; | |
2884 | ||
2885 | __io_sqe_files_unregister(ctx); | |
2886 | kfree(ctx->user_files); | |
2887 | ctx->user_files = NULL; | |
2888 | ctx->nr_user_files = 0; | |
2889 | return 0; | |
2890 | } | |
2891 | ||
6c271ce2 JA |
2892 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
2893 | { | |
2894 | if (ctx->sqo_thread) { | |
a4c0b3de | 2895 | wait_for_completion(&ctx->sqo_thread_started); |
2bbcd6d3 RP |
2896 | /* |
2897 | * The park is a bit of a work-around, without it we get | |
2898 | * warning spews on shutdown with SQPOLL set and affinity | |
2899 | * set to a single CPU. | |
2900 | */ | |
06058632 | 2901 | kthread_park(ctx->sqo_thread); |
6c271ce2 JA |
2902 | kthread_stop(ctx->sqo_thread); |
2903 | ctx->sqo_thread = NULL; | |
2904 | } | |
2905 | } | |
2906 | ||
6b06314c JA |
2907 | static void io_finish_async(struct io_ring_ctx *ctx) |
2908 | { | |
6c271ce2 JA |
2909 | io_sq_thread_stop(ctx); |
2910 | ||
561fb04a JA |
2911 | if (ctx->io_wq) { |
2912 | io_wq_destroy(ctx->io_wq); | |
2913 | ctx->io_wq = NULL; | |
6b06314c JA |
2914 | } |
2915 | } | |
2916 | ||
2917 | #if defined(CONFIG_UNIX) | |
2918 | static void io_destruct_skb(struct sk_buff *skb) | |
2919 | { | |
2920 | struct io_ring_ctx *ctx = skb->sk->sk_user_data; | |
8a997340 | 2921 | |
561fb04a JA |
2922 | if (ctx->io_wq) |
2923 | io_wq_flush(ctx->io_wq); | |
6b06314c | 2924 | |
6b06314c JA |
2925 | unix_destruct_scm(skb); |
2926 | } | |
2927 | ||
2928 | /* | |
2929 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
2930 | * the io_uring can be safely unregistered on process exit, even if we have | |
2931 | * loops in the file referencing. | |
2932 | */ | |
2933 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
2934 | { | |
2935 | struct sock *sk = ctx->ring_sock->sk; | |
2936 | struct scm_fp_list *fpl; | |
2937 | struct sk_buff *skb; | |
08a45173 | 2938 | int i, nr_files; |
6b06314c JA |
2939 | |
2940 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { | |
2941 | unsigned long inflight = ctx->user->unix_inflight + nr; | |
2942 | ||
2943 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) | |
2944 | return -EMFILE; | |
2945 | } | |
2946 | ||
2947 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); | |
2948 | if (!fpl) | |
2949 | return -ENOMEM; | |
2950 | ||
2951 | skb = alloc_skb(0, GFP_KERNEL); | |
2952 | if (!skb) { | |
2953 | kfree(fpl); | |
2954 | return -ENOMEM; | |
2955 | } | |
2956 | ||
2957 | skb->sk = sk; | |
6b06314c | 2958 | |
08a45173 | 2959 | nr_files = 0; |
6b06314c JA |
2960 | fpl->user = get_uid(ctx->user); |
2961 | for (i = 0; i < nr; i++) { | |
08a45173 JA |
2962 | if (!ctx->user_files[i + offset]) |
2963 | continue; | |
2964 | fpl->fp[nr_files] = get_file(ctx->user_files[i + offset]); | |
2965 | unix_inflight(fpl->user, fpl->fp[nr_files]); | |
2966 | nr_files++; | |
6b06314c JA |
2967 | } |
2968 | ||
08a45173 JA |
2969 | if (nr_files) { |
2970 | fpl->max = SCM_MAX_FD; | |
2971 | fpl->count = nr_files; | |
2972 | UNIXCB(skb).fp = fpl; | |
2973 | skb->destructor = io_destruct_skb; | |
2974 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); | |
2975 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 2976 | |
08a45173 JA |
2977 | for (i = 0; i < nr_files; i++) |
2978 | fput(fpl->fp[i]); | |
2979 | } else { | |
2980 | kfree_skb(skb); | |
2981 | kfree(fpl); | |
2982 | } | |
6b06314c JA |
2983 | |
2984 | return 0; | |
2985 | } | |
2986 | ||
2987 | /* | |
2988 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
2989 | * causes regular reference counting to break down. We rely on the UNIX | |
2990 | * garbage collection to take care of this problem for us. | |
2991 | */ | |
2992 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
2993 | { | |
2994 | unsigned left, total; | |
2995 | int ret = 0; | |
2996 | ||
2997 | total = 0; | |
2998 | left = ctx->nr_user_files; | |
2999 | while (left) { | |
3000 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
3001 | |
3002 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
3003 | if (ret) | |
3004 | break; | |
3005 | left -= this_files; | |
3006 | total += this_files; | |
3007 | } | |
3008 | ||
3009 | if (!ret) | |
3010 | return 0; | |
3011 | ||
3012 | while (total < ctx->nr_user_files) { | |
08a45173 JA |
3013 | if (ctx->user_files[total]) |
3014 | fput(ctx->user_files[total]); | |
6b06314c JA |
3015 | total++; |
3016 | } | |
3017 | ||
3018 | return ret; | |
3019 | } | |
3020 | #else | |
3021 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
3022 | { | |
3023 | return 0; | |
3024 | } | |
3025 | #endif | |
3026 | ||
3027 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, | |
3028 | unsigned nr_args) | |
3029 | { | |
3030 | __s32 __user *fds = (__s32 __user *) arg; | |
3031 | int fd, ret = 0; | |
3032 | unsigned i; | |
3033 | ||
3034 | if (ctx->user_files) | |
3035 | return -EBUSY; | |
3036 | if (!nr_args) | |
3037 | return -EINVAL; | |
3038 | if (nr_args > IORING_MAX_FIXED_FILES) | |
3039 | return -EMFILE; | |
3040 | ||
3041 | ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL); | |
3042 | if (!ctx->user_files) | |
3043 | return -ENOMEM; | |
3044 | ||
08a45173 | 3045 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
6b06314c JA |
3046 | ret = -EFAULT; |
3047 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) | |
3048 | break; | |
08a45173 JA |
3049 | /* allow sparse sets */ |
3050 | if (fd == -1) { | |
3051 | ret = 0; | |
3052 | continue; | |
3053 | } | |
6b06314c JA |
3054 | |
3055 | ctx->user_files[i] = fget(fd); | |
3056 | ||
3057 | ret = -EBADF; | |
3058 | if (!ctx->user_files[i]) | |
3059 | break; | |
3060 | /* | |
3061 | * Don't allow io_uring instances to be registered. If UNIX | |
3062 | * isn't enabled, then this causes a reference cycle and this | |
3063 | * instance can never get freed. If UNIX is enabled we'll | |
3064 | * handle it just fine, but there's still no point in allowing | |
3065 | * a ring fd as it doesn't support regular read/write anyway. | |
3066 | */ | |
3067 | if (ctx->user_files[i]->f_op == &io_uring_fops) { | |
3068 | fput(ctx->user_files[i]); | |
3069 | break; | |
3070 | } | |
6b06314c JA |
3071 | ret = 0; |
3072 | } | |
3073 | ||
3074 | if (ret) { | |
3075 | for (i = 0; i < ctx->nr_user_files; i++) | |
08a45173 JA |
3076 | if (ctx->user_files[i]) |
3077 | fput(ctx->user_files[i]); | |
6b06314c JA |
3078 | |
3079 | kfree(ctx->user_files); | |
25adf50f | 3080 | ctx->user_files = NULL; |
6b06314c JA |
3081 | ctx->nr_user_files = 0; |
3082 | return ret; | |
3083 | } | |
3084 | ||
3085 | ret = io_sqe_files_scm(ctx); | |
3086 | if (ret) | |
3087 | io_sqe_files_unregister(ctx); | |
3088 | ||
3089 | return ret; | |
3090 | } | |
3091 | ||
c3a31e60 JA |
3092 | static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index) |
3093 | { | |
3094 | #if defined(CONFIG_UNIX) | |
3095 | struct file *file = ctx->user_files[index]; | |
3096 | struct sock *sock = ctx->ring_sock->sk; | |
3097 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
3098 | struct sk_buff *skb; | |
3099 | int i; | |
3100 | ||
3101 | __skb_queue_head_init(&list); | |
3102 | ||
3103 | /* | |
3104 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
3105 | * remove this entry and rearrange the file array. | |
3106 | */ | |
3107 | skb = skb_dequeue(head); | |
3108 | while (skb) { | |
3109 | struct scm_fp_list *fp; | |
3110 | ||
3111 | fp = UNIXCB(skb).fp; | |
3112 | for (i = 0; i < fp->count; i++) { | |
3113 | int left; | |
3114 | ||
3115 | if (fp->fp[i] != file) | |
3116 | continue; | |
3117 | ||
3118 | unix_notinflight(fp->user, fp->fp[i]); | |
3119 | left = fp->count - 1 - i; | |
3120 | if (left) { | |
3121 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
3122 | left * sizeof(struct file *)); | |
3123 | } | |
3124 | fp->count--; | |
3125 | if (!fp->count) { | |
3126 | kfree_skb(skb); | |
3127 | skb = NULL; | |
3128 | } else { | |
3129 | __skb_queue_tail(&list, skb); | |
3130 | } | |
3131 | fput(file); | |
3132 | file = NULL; | |
3133 | break; | |
3134 | } | |
3135 | ||
3136 | if (!file) | |
3137 | break; | |
3138 | ||
3139 | __skb_queue_tail(&list, skb); | |
3140 | ||
3141 | skb = skb_dequeue(head); | |
3142 | } | |
3143 | ||
3144 | if (skb_peek(&list)) { | |
3145 | spin_lock_irq(&head->lock); | |
3146 | while ((skb = __skb_dequeue(&list)) != NULL) | |
3147 | __skb_queue_tail(head, skb); | |
3148 | spin_unlock_irq(&head->lock); | |
3149 | } | |
3150 | #else | |
3151 | fput(ctx->user_files[index]); | |
3152 | #endif | |
3153 | } | |
3154 | ||
3155 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, | |
3156 | int index) | |
3157 | { | |
3158 | #if defined(CONFIG_UNIX) | |
3159 | struct sock *sock = ctx->ring_sock->sk; | |
3160 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
3161 | struct sk_buff *skb; | |
3162 | ||
3163 | /* | |
3164 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
3165 | * file set. If there's no room, fall back to allocating a new skb | |
3166 | * and filling it in. | |
3167 | */ | |
3168 | spin_lock_irq(&head->lock); | |
3169 | skb = skb_peek(head); | |
3170 | if (skb) { | |
3171 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
3172 | ||
3173 | if (fpl->count < SCM_MAX_FD) { | |
3174 | __skb_unlink(skb, head); | |
3175 | spin_unlock_irq(&head->lock); | |
3176 | fpl->fp[fpl->count] = get_file(file); | |
3177 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
3178 | fpl->count++; | |
3179 | spin_lock_irq(&head->lock); | |
3180 | __skb_queue_head(head, skb); | |
3181 | } else { | |
3182 | skb = NULL; | |
3183 | } | |
3184 | } | |
3185 | spin_unlock_irq(&head->lock); | |
3186 | ||
3187 | if (skb) { | |
3188 | fput(file); | |
3189 | return 0; | |
3190 | } | |
3191 | ||
3192 | return __io_sqe_files_scm(ctx, 1, index); | |
3193 | #else | |
3194 | return 0; | |
3195 | #endif | |
3196 | } | |
3197 | ||
3198 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, | |
3199 | unsigned nr_args) | |
3200 | { | |
3201 | struct io_uring_files_update up; | |
3202 | __s32 __user *fds; | |
3203 | int fd, i, err; | |
3204 | __u32 done; | |
3205 | ||
3206 | if (!ctx->user_files) | |
3207 | return -ENXIO; | |
3208 | if (!nr_args) | |
3209 | return -EINVAL; | |
3210 | if (copy_from_user(&up, arg, sizeof(up))) | |
3211 | return -EFAULT; | |
3212 | if (check_add_overflow(up.offset, nr_args, &done)) | |
3213 | return -EOVERFLOW; | |
3214 | if (done > ctx->nr_user_files) | |
3215 | return -EINVAL; | |
3216 | ||
3217 | done = 0; | |
3218 | fds = (__s32 __user *) up.fds; | |
3219 | while (nr_args) { | |
3220 | err = 0; | |
3221 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
3222 | err = -EFAULT; | |
3223 | break; | |
3224 | } | |
3225 | i = array_index_nospec(up.offset, ctx->nr_user_files); | |
3226 | if (ctx->user_files[i]) { | |
3227 | io_sqe_file_unregister(ctx, i); | |
3228 | ctx->user_files[i] = NULL; | |
3229 | } | |
3230 | if (fd != -1) { | |
3231 | struct file *file; | |
3232 | ||
3233 | file = fget(fd); | |
3234 | if (!file) { | |
3235 | err = -EBADF; | |
3236 | break; | |
3237 | } | |
3238 | /* | |
3239 | * Don't allow io_uring instances to be registered. If | |
3240 | * UNIX isn't enabled, then this causes a reference | |
3241 | * cycle and this instance can never get freed. If UNIX | |
3242 | * is enabled we'll handle it just fine, but there's | |
3243 | * still no point in allowing a ring fd as it doesn't | |
3244 | * support regular read/write anyway. | |
3245 | */ | |
3246 | if (file->f_op == &io_uring_fops) { | |
3247 | fput(file); | |
3248 | err = -EBADF; | |
3249 | break; | |
3250 | } | |
3251 | ctx->user_files[i] = file; | |
3252 | err = io_sqe_file_register(ctx, file, i); | |
3253 | if (err) | |
3254 | break; | |
3255 | } | |
3256 | nr_args--; | |
3257 | done++; | |
3258 | up.offset++; | |
3259 | } | |
3260 | ||
3261 | return done ? done : err; | |
3262 | } | |
3263 | ||
6c271ce2 JA |
3264 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
3265 | struct io_uring_params *p) | |
2b188cc1 | 3266 | { |
561fb04a | 3267 | unsigned concurrency; |
2b188cc1 JA |
3268 | int ret; |
3269 | ||
6c271ce2 | 3270 | init_waitqueue_head(&ctx->sqo_wait); |
2b188cc1 JA |
3271 | mmgrab(current->mm); |
3272 | ctx->sqo_mm = current->mm; | |
3273 | ||
6c271ce2 | 3274 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
3ec482d1 JA |
3275 | ret = -EPERM; |
3276 | if (!capable(CAP_SYS_ADMIN)) | |
3277 | goto err; | |
3278 | ||
917257da JA |
3279 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
3280 | if (!ctx->sq_thread_idle) | |
3281 | ctx->sq_thread_idle = HZ; | |
3282 | ||
6c271ce2 | 3283 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 3284 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 3285 | |
917257da | 3286 | ret = -EINVAL; |
44a9bd18 JA |
3287 | if (cpu >= nr_cpu_ids) |
3288 | goto err; | |
7889f44d | 3289 | if (!cpu_online(cpu)) |
917257da JA |
3290 | goto err; |
3291 | ||
6c271ce2 JA |
3292 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
3293 | ctx, cpu, | |
3294 | "io_uring-sq"); | |
3295 | } else { | |
3296 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, | |
3297 | "io_uring-sq"); | |
3298 | } | |
3299 | if (IS_ERR(ctx->sqo_thread)) { | |
3300 | ret = PTR_ERR(ctx->sqo_thread); | |
3301 | ctx->sqo_thread = NULL; | |
3302 | goto err; | |
3303 | } | |
3304 | wake_up_process(ctx->sqo_thread); | |
3305 | } else if (p->flags & IORING_SETUP_SQ_AFF) { | |
3306 | /* Can't have SQ_AFF without SQPOLL */ | |
3307 | ret = -EINVAL; | |
3308 | goto err; | |
3309 | } | |
3310 | ||
561fb04a JA |
3311 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
3312 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
3313 | ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm); | |
3314 | if (!ctx->io_wq) { | |
2b188cc1 JA |
3315 | ret = -ENOMEM; |
3316 | goto err; | |
3317 | } | |
3318 | ||
3319 | return 0; | |
3320 | err: | |
54a91f3b | 3321 | io_finish_async(ctx); |
2b188cc1 JA |
3322 | mmdrop(ctx->sqo_mm); |
3323 | ctx->sqo_mm = NULL; | |
3324 | return ret; | |
3325 | } | |
3326 | ||
3327 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) | |
3328 | { | |
3329 | atomic_long_sub(nr_pages, &user->locked_vm); | |
3330 | } | |
3331 | ||
3332 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) | |
3333 | { | |
3334 | unsigned long page_limit, cur_pages, new_pages; | |
3335 | ||
3336 | /* Don't allow more pages than we can safely lock */ | |
3337 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
3338 | ||
3339 | do { | |
3340 | cur_pages = atomic_long_read(&user->locked_vm); | |
3341 | new_pages = cur_pages + nr_pages; | |
3342 | if (new_pages > page_limit) | |
3343 | return -ENOMEM; | |
3344 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
3345 | new_pages) != cur_pages); | |
3346 | ||
3347 | return 0; | |
3348 | } | |
3349 | ||
3350 | static void io_mem_free(void *ptr) | |
3351 | { | |
52e04ef4 MR |
3352 | struct page *page; |
3353 | ||
3354 | if (!ptr) | |
3355 | return; | |
2b188cc1 | 3356 | |
52e04ef4 | 3357 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
3358 | if (put_page_testzero(page)) |
3359 | free_compound_page(page); | |
3360 | } | |
3361 | ||
3362 | static void *io_mem_alloc(size_t size) | |
3363 | { | |
3364 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
3365 | __GFP_NORETRY; | |
3366 | ||
3367 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
3368 | } | |
3369 | ||
75b28aff HV |
3370 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
3371 | size_t *sq_offset) | |
3372 | { | |
3373 | struct io_rings *rings; | |
3374 | size_t off, sq_array_size; | |
3375 | ||
3376 | off = struct_size(rings, cqes, cq_entries); | |
3377 | if (off == SIZE_MAX) | |
3378 | return SIZE_MAX; | |
3379 | ||
3380 | #ifdef CONFIG_SMP | |
3381 | off = ALIGN(off, SMP_CACHE_BYTES); | |
3382 | if (off == 0) | |
3383 | return SIZE_MAX; | |
3384 | #endif | |
3385 | ||
3386 | sq_array_size = array_size(sizeof(u32), sq_entries); | |
3387 | if (sq_array_size == SIZE_MAX) | |
3388 | return SIZE_MAX; | |
3389 | ||
3390 | if (check_add_overflow(off, sq_array_size, &off)) | |
3391 | return SIZE_MAX; | |
3392 | ||
3393 | if (sq_offset) | |
3394 | *sq_offset = off; | |
3395 | ||
3396 | return off; | |
3397 | } | |
3398 | ||
2b188cc1 JA |
3399 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
3400 | { | |
75b28aff | 3401 | size_t pages; |
2b188cc1 | 3402 | |
75b28aff HV |
3403 | pages = (size_t)1 << get_order( |
3404 | rings_size(sq_entries, cq_entries, NULL)); | |
3405 | pages += (size_t)1 << get_order( | |
3406 | array_size(sizeof(struct io_uring_sqe), sq_entries)); | |
2b188cc1 | 3407 | |
75b28aff | 3408 | return pages; |
2b188cc1 JA |
3409 | } |
3410 | ||
edafccee JA |
3411 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
3412 | { | |
3413 | int i, j; | |
3414 | ||
3415 | if (!ctx->user_bufs) | |
3416 | return -ENXIO; | |
3417 | ||
3418 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
3419 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
3420 | ||
3421 | for (j = 0; j < imu->nr_bvecs; j++) | |
27c4d3a3 | 3422 | put_user_page(imu->bvec[j].bv_page); |
edafccee JA |
3423 | |
3424 | if (ctx->account_mem) | |
3425 | io_unaccount_mem(ctx->user, imu->nr_bvecs); | |
d4ef6475 | 3426 | kvfree(imu->bvec); |
edafccee JA |
3427 | imu->nr_bvecs = 0; |
3428 | } | |
3429 | ||
3430 | kfree(ctx->user_bufs); | |
3431 | ctx->user_bufs = NULL; | |
3432 | ctx->nr_user_bufs = 0; | |
3433 | return 0; | |
3434 | } | |
3435 | ||
3436 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
3437 | void __user *arg, unsigned index) | |
3438 | { | |
3439 | struct iovec __user *src; | |
3440 | ||
3441 | #ifdef CONFIG_COMPAT | |
3442 | if (ctx->compat) { | |
3443 | struct compat_iovec __user *ciovs; | |
3444 | struct compat_iovec ciov; | |
3445 | ||
3446 | ciovs = (struct compat_iovec __user *) arg; | |
3447 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
3448 | return -EFAULT; | |
3449 | ||
3450 | dst->iov_base = (void __user *) (unsigned long) ciov.iov_base; | |
3451 | dst->iov_len = ciov.iov_len; | |
3452 | return 0; | |
3453 | } | |
3454 | #endif | |
3455 | src = (struct iovec __user *) arg; | |
3456 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
3457 | return -EFAULT; | |
3458 | return 0; | |
3459 | } | |
3460 | ||
3461 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, | |
3462 | unsigned nr_args) | |
3463 | { | |
3464 | struct vm_area_struct **vmas = NULL; | |
3465 | struct page **pages = NULL; | |
3466 | int i, j, got_pages = 0; | |
3467 | int ret = -EINVAL; | |
3468 | ||
3469 | if (ctx->user_bufs) | |
3470 | return -EBUSY; | |
3471 | if (!nr_args || nr_args > UIO_MAXIOV) | |
3472 | return -EINVAL; | |
3473 | ||
3474 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
3475 | GFP_KERNEL); | |
3476 | if (!ctx->user_bufs) | |
3477 | return -ENOMEM; | |
3478 | ||
3479 | for (i = 0; i < nr_args; i++) { | |
3480 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
3481 | unsigned long off, start, end, ubuf; | |
3482 | int pret, nr_pages; | |
3483 | struct iovec iov; | |
3484 | size_t size; | |
3485 | ||
3486 | ret = io_copy_iov(ctx, &iov, arg, i); | |
3487 | if (ret) | |
a278682d | 3488 | goto err; |
edafccee JA |
3489 | |
3490 | /* | |
3491 | * Don't impose further limits on the size and buffer | |
3492 | * constraints here, we'll -EINVAL later when IO is | |
3493 | * submitted if they are wrong. | |
3494 | */ | |
3495 | ret = -EFAULT; | |
3496 | if (!iov.iov_base || !iov.iov_len) | |
3497 | goto err; | |
3498 | ||
3499 | /* arbitrary limit, but we need something */ | |
3500 | if (iov.iov_len > SZ_1G) | |
3501 | goto err; | |
3502 | ||
3503 | ubuf = (unsigned long) iov.iov_base; | |
3504 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
3505 | start = ubuf >> PAGE_SHIFT; | |
3506 | nr_pages = end - start; | |
3507 | ||
3508 | if (ctx->account_mem) { | |
3509 | ret = io_account_mem(ctx->user, nr_pages); | |
3510 | if (ret) | |
3511 | goto err; | |
3512 | } | |
3513 | ||
3514 | ret = 0; | |
3515 | if (!pages || nr_pages > got_pages) { | |
3516 | kfree(vmas); | |
3517 | kfree(pages); | |
d4ef6475 | 3518 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
edafccee | 3519 | GFP_KERNEL); |
d4ef6475 | 3520 | vmas = kvmalloc_array(nr_pages, |
edafccee JA |
3521 | sizeof(struct vm_area_struct *), |
3522 | GFP_KERNEL); | |
3523 | if (!pages || !vmas) { | |
3524 | ret = -ENOMEM; | |
3525 | if (ctx->account_mem) | |
3526 | io_unaccount_mem(ctx->user, nr_pages); | |
3527 | goto err; | |
3528 | } | |
3529 | got_pages = nr_pages; | |
3530 | } | |
3531 | ||
d4ef6475 | 3532 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
edafccee JA |
3533 | GFP_KERNEL); |
3534 | ret = -ENOMEM; | |
3535 | if (!imu->bvec) { | |
3536 | if (ctx->account_mem) | |
3537 | io_unaccount_mem(ctx->user, nr_pages); | |
3538 | goto err; | |
3539 | } | |
3540 | ||
3541 | ret = 0; | |
3542 | down_read(¤t->mm->mmap_sem); | |
932f4a63 IW |
3543 | pret = get_user_pages(ubuf, nr_pages, |
3544 | FOLL_WRITE | FOLL_LONGTERM, | |
3545 | pages, vmas); | |
edafccee JA |
3546 | if (pret == nr_pages) { |
3547 | /* don't support file backed memory */ | |
3548 | for (j = 0; j < nr_pages; j++) { | |
3549 | struct vm_area_struct *vma = vmas[j]; | |
3550 | ||
3551 | if (vma->vm_file && | |
3552 | !is_file_hugepages(vma->vm_file)) { | |
3553 | ret = -EOPNOTSUPP; | |
3554 | break; | |
3555 | } | |
3556 | } | |
3557 | } else { | |
3558 | ret = pret < 0 ? pret : -EFAULT; | |
3559 | } | |
3560 | up_read(¤t->mm->mmap_sem); | |
3561 | if (ret) { | |
3562 | /* | |
3563 | * if we did partial map, or found file backed vmas, | |
3564 | * release any pages we did get | |
3565 | */ | |
27c4d3a3 JH |
3566 | if (pret > 0) |
3567 | put_user_pages(pages, pret); | |
edafccee JA |
3568 | if (ctx->account_mem) |
3569 | io_unaccount_mem(ctx->user, nr_pages); | |
d4ef6475 | 3570 | kvfree(imu->bvec); |
edafccee JA |
3571 | goto err; |
3572 | } | |
3573 | ||
3574 | off = ubuf & ~PAGE_MASK; | |
3575 | size = iov.iov_len; | |
3576 | for (j = 0; j < nr_pages; j++) { | |
3577 | size_t vec_len; | |
3578 | ||
3579 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
3580 | imu->bvec[j].bv_page = pages[j]; | |
3581 | imu->bvec[j].bv_len = vec_len; | |
3582 | imu->bvec[j].bv_offset = off; | |
3583 | off = 0; | |
3584 | size -= vec_len; | |
3585 | } | |
3586 | /* store original address for later verification */ | |
3587 | imu->ubuf = ubuf; | |
3588 | imu->len = iov.iov_len; | |
3589 | imu->nr_bvecs = nr_pages; | |
3590 | ||
3591 | ctx->nr_user_bufs++; | |
3592 | } | |
d4ef6475 MR |
3593 | kvfree(pages); |
3594 | kvfree(vmas); | |
edafccee JA |
3595 | return 0; |
3596 | err: | |
d4ef6475 MR |
3597 | kvfree(pages); |
3598 | kvfree(vmas); | |
edafccee JA |
3599 | io_sqe_buffer_unregister(ctx); |
3600 | return ret; | |
3601 | } | |
3602 | ||
9b402849 JA |
3603 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
3604 | { | |
3605 | __s32 __user *fds = arg; | |
3606 | int fd; | |
3607 | ||
3608 | if (ctx->cq_ev_fd) | |
3609 | return -EBUSY; | |
3610 | ||
3611 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
3612 | return -EFAULT; | |
3613 | ||
3614 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
3615 | if (IS_ERR(ctx->cq_ev_fd)) { | |
3616 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
3617 | ctx->cq_ev_fd = NULL; | |
3618 | return ret; | |
3619 | } | |
3620 | ||
3621 | return 0; | |
3622 | } | |
3623 | ||
3624 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
3625 | { | |
3626 | if (ctx->cq_ev_fd) { | |
3627 | eventfd_ctx_put(ctx->cq_ev_fd); | |
3628 | ctx->cq_ev_fd = NULL; | |
3629 | return 0; | |
3630 | } | |
3631 | ||
3632 | return -ENXIO; | |
3633 | } | |
3634 | ||
2b188cc1 JA |
3635 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
3636 | { | |
6b06314c | 3637 | io_finish_async(ctx); |
2b188cc1 JA |
3638 | if (ctx->sqo_mm) |
3639 | mmdrop(ctx->sqo_mm); | |
def596e9 JA |
3640 | |
3641 | io_iopoll_reap_events(ctx); | |
edafccee | 3642 | io_sqe_buffer_unregister(ctx); |
6b06314c | 3643 | io_sqe_files_unregister(ctx); |
9b402849 | 3644 | io_eventfd_unregister(ctx); |
def596e9 | 3645 | |
2b188cc1 | 3646 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
3647 | if (ctx->ring_sock) { |
3648 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 3649 | sock_release(ctx->ring_sock); |
355e8d26 | 3650 | } |
2b188cc1 JA |
3651 | #endif |
3652 | ||
75b28aff | 3653 | io_mem_free(ctx->rings); |
2b188cc1 | 3654 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
3655 | |
3656 | percpu_ref_exit(&ctx->refs); | |
3657 | if (ctx->account_mem) | |
3658 | io_unaccount_mem(ctx->user, | |
3659 | ring_pages(ctx->sq_entries, ctx->cq_entries)); | |
3660 | free_uid(ctx->user); | |
3661 | kfree(ctx); | |
3662 | } | |
3663 | ||
3664 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
3665 | { | |
3666 | struct io_ring_ctx *ctx = file->private_data; | |
3667 | __poll_t mask = 0; | |
3668 | ||
3669 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
3670 | /* |
3671 | * synchronizes with barrier from wq_has_sleeper call in | |
3672 | * io_commit_cqring | |
3673 | */ | |
2b188cc1 | 3674 | smp_rmb(); |
75b28aff HV |
3675 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
3676 | ctx->rings->sq_ring_entries) | |
2b188cc1 | 3677 | mask |= EPOLLOUT | EPOLLWRNORM; |
daa5de54 | 3678 | if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail) |
2b188cc1 JA |
3679 | mask |= EPOLLIN | EPOLLRDNORM; |
3680 | ||
3681 | return mask; | |
3682 | } | |
3683 | ||
3684 | static int io_uring_fasync(int fd, struct file *file, int on) | |
3685 | { | |
3686 | struct io_ring_ctx *ctx = file->private_data; | |
3687 | ||
3688 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
3689 | } | |
3690 | ||
3691 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) | |
3692 | { | |
3693 | mutex_lock(&ctx->uring_lock); | |
3694 | percpu_ref_kill(&ctx->refs); | |
3695 | mutex_unlock(&ctx->uring_lock); | |
3696 | ||
5262f567 | 3697 | io_kill_timeouts(ctx); |
221c5eb2 | 3698 | io_poll_remove_all(ctx); |
561fb04a JA |
3699 | |
3700 | if (ctx->io_wq) | |
3701 | io_wq_cancel_all(ctx->io_wq); | |
3702 | ||
def596e9 | 3703 | io_iopoll_reap_events(ctx); |
2b188cc1 JA |
3704 | wait_for_completion(&ctx->ctx_done); |
3705 | io_ring_ctx_free(ctx); | |
3706 | } | |
3707 | ||
3708 | static int io_uring_release(struct inode *inode, struct file *file) | |
3709 | { | |
3710 | struct io_ring_ctx *ctx = file->private_data; | |
3711 | ||
3712 | file->private_data = NULL; | |
3713 | io_ring_ctx_wait_and_kill(ctx); | |
3714 | return 0; | |
3715 | } | |
3716 | ||
3717 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
3718 | { | |
3719 | loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT; | |
3720 | unsigned long sz = vma->vm_end - vma->vm_start; | |
3721 | struct io_ring_ctx *ctx = file->private_data; | |
3722 | unsigned long pfn; | |
3723 | struct page *page; | |
3724 | void *ptr; | |
3725 | ||
3726 | switch (offset) { | |
3727 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
3728 | case IORING_OFF_CQ_RING: |
3729 | ptr = ctx->rings; | |
2b188cc1 JA |
3730 | break; |
3731 | case IORING_OFF_SQES: | |
3732 | ptr = ctx->sq_sqes; | |
3733 | break; | |
2b188cc1 JA |
3734 | default: |
3735 | return -EINVAL; | |
3736 | } | |
3737 | ||
3738 | page = virt_to_head_page(ptr); | |
a50b854e | 3739 | if (sz > page_size(page)) |
2b188cc1 JA |
3740 | return -EINVAL; |
3741 | ||
3742 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
3743 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
3744 | } | |
3745 | ||
3746 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, | |
3747 | u32, min_complete, u32, flags, const sigset_t __user *, sig, | |
3748 | size_t, sigsz) | |
3749 | { | |
3750 | struct io_ring_ctx *ctx; | |
3751 | long ret = -EBADF; | |
3752 | int submitted = 0; | |
3753 | struct fd f; | |
3754 | ||
6c271ce2 | 3755 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
2b188cc1 JA |
3756 | return -EINVAL; |
3757 | ||
3758 | f = fdget(fd); | |
3759 | if (!f.file) | |
3760 | return -EBADF; | |
3761 | ||
3762 | ret = -EOPNOTSUPP; | |
3763 | if (f.file->f_op != &io_uring_fops) | |
3764 | goto out_fput; | |
3765 | ||
3766 | ret = -ENXIO; | |
3767 | ctx = f.file->private_data; | |
3768 | if (!percpu_ref_tryget(&ctx->refs)) | |
3769 | goto out_fput; | |
3770 | ||
6c271ce2 JA |
3771 | /* |
3772 | * For SQ polling, the thread will do all submissions and completions. | |
3773 | * Just return the requested submit count, and wake the thread if | |
3774 | * we were asked to. | |
3775 | */ | |
b2a9eada | 3776 | ret = 0; |
6c271ce2 JA |
3777 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
3778 | if (flags & IORING_ENTER_SQ_WAKEUP) | |
3779 | wake_up(&ctx->sqo_wait); | |
3780 | submitted = to_submit; | |
b2a9eada | 3781 | } else if (to_submit) { |
2b188cc1 JA |
3782 | to_submit = min(to_submit, ctx->sq_entries); |
3783 | ||
3784 | mutex_lock(&ctx->uring_lock); | |
bc808bce | 3785 | submitted = io_ring_submit(ctx, to_submit); |
2b188cc1 | 3786 | mutex_unlock(&ctx->uring_lock); |
2b188cc1 JA |
3787 | } |
3788 | if (flags & IORING_ENTER_GETEVENTS) { | |
def596e9 JA |
3789 | unsigned nr_events = 0; |
3790 | ||
2b188cc1 JA |
3791 | min_complete = min(min_complete, ctx->cq_entries); |
3792 | ||
def596e9 | 3793 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 | 3794 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
def596e9 JA |
3795 | } else { |
3796 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); | |
3797 | } | |
2b188cc1 JA |
3798 | } |
3799 | ||
6805b32e | 3800 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
3801 | out_fput: |
3802 | fdput(f); | |
3803 | return submitted ? submitted : ret; | |
3804 | } | |
3805 | ||
3806 | static const struct file_operations io_uring_fops = { | |
3807 | .release = io_uring_release, | |
3808 | .mmap = io_uring_mmap, | |
3809 | .poll = io_uring_poll, | |
3810 | .fasync = io_uring_fasync, | |
3811 | }; | |
3812 | ||
3813 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
3814 | struct io_uring_params *p) | |
3815 | { | |
75b28aff HV |
3816 | struct io_rings *rings; |
3817 | size_t size, sq_array_offset; | |
2b188cc1 | 3818 | |
75b28aff HV |
3819 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
3820 | if (size == SIZE_MAX) | |
3821 | return -EOVERFLOW; | |
3822 | ||
3823 | rings = io_mem_alloc(size); | |
3824 | if (!rings) | |
2b188cc1 JA |
3825 | return -ENOMEM; |
3826 | ||
75b28aff HV |
3827 | ctx->rings = rings; |
3828 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
3829 | rings->sq_ring_mask = p->sq_entries - 1; | |
3830 | rings->cq_ring_mask = p->cq_entries - 1; | |
3831 | rings->sq_ring_entries = p->sq_entries; | |
3832 | rings->cq_ring_entries = p->cq_entries; | |
3833 | ctx->sq_mask = rings->sq_ring_mask; | |
3834 | ctx->cq_mask = rings->cq_ring_mask; | |
3835 | ctx->sq_entries = rings->sq_ring_entries; | |
3836 | ctx->cq_entries = rings->cq_ring_entries; | |
2b188cc1 JA |
3837 | |
3838 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
3839 | if (size == SIZE_MAX) | |
3840 | return -EOVERFLOW; | |
3841 | ||
3842 | ctx->sq_sqes = io_mem_alloc(size); | |
52e04ef4 | 3843 | if (!ctx->sq_sqes) |
2b188cc1 | 3844 | return -ENOMEM; |
2b188cc1 | 3845 | |
2b188cc1 JA |
3846 | return 0; |
3847 | } | |
3848 | ||
3849 | /* | |
3850 | * Allocate an anonymous fd, this is what constitutes the application | |
3851 | * visible backing of an io_uring instance. The application mmaps this | |
3852 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
3853 | * we have to tie this fd to a socket for file garbage collection purposes. | |
3854 | */ | |
3855 | static int io_uring_get_fd(struct io_ring_ctx *ctx) | |
3856 | { | |
3857 | struct file *file; | |
3858 | int ret; | |
3859 | ||
3860 | #if defined(CONFIG_UNIX) | |
3861 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, | |
3862 | &ctx->ring_sock); | |
3863 | if (ret) | |
3864 | return ret; | |
3865 | #endif | |
3866 | ||
3867 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
3868 | if (ret < 0) | |
3869 | goto err; | |
3870 | ||
3871 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, | |
3872 | O_RDWR | O_CLOEXEC); | |
3873 | if (IS_ERR(file)) { | |
3874 | put_unused_fd(ret); | |
3875 | ret = PTR_ERR(file); | |
3876 | goto err; | |
3877 | } | |
3878 | ||
3879 | #if defined(CONFIG_UNIX) | |
3880 | ctx->ring_sock->file = file; | |
6b06314c | 3881 | ctx->ring_sock->sk->sk_user_data = ctx; |
2b188cc1 JA |
3882 | #endif |
3883 | fd_install(ret, file); | |
3884 | return ret; | |
3885 | err: | |
3886 | #if defined(CONFIG_UNIX) | |
3887 | sock_release(ctx->ring_sock); | |
3888 | ctx->ring_sock = NULL; | |
3889 | #endif | |
3890 | return ret; | |
3891 | } | |
3892 | ||
3893 | static int io_uring_create(unsigned entries, struct io_uring_params *p) | |
3894 | { | |
3895 | struct user_struct *user = NULL; | |
3896 | struct io_ring_ctx *ctx; | |
3897 | bool account_mem; | |
3898 | int ret; | |
3899 | ||
3900 | if (!entries || entries > IORING_MAX_ENTRIES) | |
3901 | return -EINVAL; | |
3902 | ||
3903 | /* | |
3904 | * Use twice as many entries for the CQ ring. It's possible for the | |
3905 | * application to drive a higher depth than the size of the SQ ring, | |
3906 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
3907 | * some flexibility in overcommitting a bit. If the application has |
3908 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
3909 | * of CQ ring entries manually. | |
2b188cc1 JA |
3910 | */ |
3911 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
3912 | if (p->flags & IORING_SETUP_CQSIZE) { |
3913 | /* | |
3914 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
3915 | * to a power-of-two, if it isn't already. We do NOT impose | |
3916 | * any cq vs sq ring sizing. | |
3917 | */ | |
3918 | if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES) | |
3919 | return -EINVAL; | |
3920 | p->cq_entries = roundup_pow_of_two(p->cq_entries); | |
3921 | } else { | |
3922 | p->cq_entries = 2 * p->sq_entries; | |
3923 | } | |
2b188cc1 JA |
3924 | |
3925 | user = get_uid(current_user()); | |
3926 | account_mem = !capable(CAP_IPC_LOCK); | |
3927 | ||
3928 | if (account_mem) { | |
3929 | ret = io_account_mem(user, | |
3930 | ring_pages(p->sq_entries, p->cq_entries)); | |
3931 | if (ret) { | |
3932 | free_uid(user); | |
3933 | return ret; | |
3934 | } | |
3935 | } | |
3936 | ||
3937 | ctx = io_ring_ctx_alloc(p); | |
3938 | if (!ctx) { | |
3939 | if (account_mem) | |
3940 | io_unaccount_mem(user, ring_pages(p->sq_entries, | |
3941 | p->cq_entries)); | |
3942 | free_uid(user); | |
3943 | return -ENOMEM; | |
3944 | } | |
3945 | ctx->compat = in_compat_syscall(); | |
3946 | ctx->account_mem = account_mem; | |
3947 | ctx->user = user; | |
3948 | ||
3949 | ret = io_allocate_scq_urings(ctx, p); | |
3950 | if (ret) | |
3951 | goto err; | |
3952 | ||
6c271ce2 | 3953 | ret = io_sq_offload_start(ctx, p); |
2b188cc1 JA |
3954 | if (ret) |
3955 | goto err; | |
3956 | ||
2b188cc1 | 3957 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
3958 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
3959 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
3960 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
3961 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
3962 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
3963 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
3964 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
3965 | |
3966 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
3967 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
3968 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
3969 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
3970 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
3971 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
3972 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
ac90f249 | 3973 | |
044c1ab3 JA |
3974 | /* |
3975 | * Install ring fd as the very last thing, so we don't risk someone | |
3976 | * having closed it before we finish setup | |
3977 | */ | |
3978 | ret = io_uring_get_fd(ctx); | |
3979 | if (ret < 0) | |
3980 | goto err; | |
3981 | ||
ac90f249 | 3982 | p->features = IORING_FEAT_SINGLE_MMAP; |
c826bd7a | 3983 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
3984 | return ret; |
3985 | err: | |
3986 | io_ring_ctx_wait_and_kill(ctx); | |
3987 | return ret; | |
3988 | } | |
3989 | ||
3990 | /* | |
3991 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
3992 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
3993 | * params structure passed in. | |
3994 | */ | |
3995 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
3996 | { | |
3997 | struct io_uring_params p; | |
3998 | long ret; | |
3999 | int i; | |
4000 | ||
4001 | if (copy_from_user(&p, params, sizeof(p))) | |
4002 | return -EFAULT; | |
4003 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
4004 | if (p.resv[i]) | |
4005 | return -EINVAL; | |
4006 | } | |
4007 | ||
6c271ce2 | 4008 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
33a107f0 | 4009 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE)) |
2b188cc1 JA |
4010 | return -EINVAL; |
4011 | ||
4012 | ret = io_uring_create(entries, &p); | |
4013 | if (ret < 0) | |
4014 | return ret; | |
4015 | ||
4016 | if (copy_to_user(params, &p, sizeof(p))) | |
4017 | return -EFAULT; | |
4018 | ||
4019 | return ret; | |
4020 | } | |
4021 | ||
4022 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
4023 | struct io_uring_params __user *, params) | |
4024 | { | |
4025 | return io_uring_setup(entries, params); | |
4026 | } | |
4027 | ||
edafccee JA |
4028 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
4029 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
4030 | __releases(ctx->uring_lock) |
4031 | __acquires(ctx->uring_lock) | |
edafccee JA |
4032 | { |
4033 | int ret; | |
4034 | ||
35fa71a0 JA |
4035 | /* |
4036 | * We're inside the ring mutex, if the ref is already dying, then | |
4037 | * someone else killed the ctx or is already going through | |
4038 | * io_uring_register(). | |
4039 | */ | |
4040 | if (percpu_ref_is_dying(&ctx->refs)) | |
4041 | return -ENXIO; | |
4042 | ||
edafccee | 4043 | percpu_ref_kill(&ctx->refs); |
b19062a5 JA |
4044 | |
4045 | /* | |
4046 | * Drop uring mutex before waiting for references to exit. If another | |
4047 | * thread is currently inside io_uring_enter() it might need to grab | |
4048 | * the uring_lock to make progress. If we hold it here across the drain | |
4049 | * wait, then we can deadlock. It's safe to drop the mutex here, since | |
4050 | * no new references will come in after we've killed the percpu ref. | |
4051 | */ | |
4052 | mutex_unlock(&ctx->uring_lock); | |
edafccee | 4053 | wait_for_completion(&ctx->ctx_done); |
b19062a5 | 4054 | mutex_lock(&ctx->uring_lock); |
edafccee JA |
4055 | |
4056 | switch (opcode) { | |
4057 | case IORING_REGISTER_BUFFERS: | |
4058 | ret = io_sqe_buffer_register(ctx, arg, nr_args); | |
4059 | break; | |
4060 | case IORING_UNREGISTER_BUFFERS: | |
4061 | ret = -EINVAL; | |
4062 | if (arg || nr_args) | |
4063 | break; | |
4064 | ret = io_sqe_buffer_unregister(ctx); | |
4065 | break; | |
6b06314c JA |
4066 | case IORING_REGISTER_FILES: |
4067 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
4068 | break; | |
4069 | case IORING_UNREGISTER_FILES: | |
4070 | ret = -EINVAL; | |
4071 | if (arg || nr_args) | |
4072 | break; | |
4073 | ret = io_sqe_files_unregister(ctx); | |
4074 | break; | |
c3a31e60 JA |
4075 | case IORING_REGISTER_FILES_UPDATE: |
4076 | ret = io_sqe_files_update(ctx, arg, nr_args); | |
4077 | break; | |
9b402849 JA |
4078 | case IORING_REGISTER_EVENTFD: |
4079 | ret = -EINVAL; | |
4080 | if (nr_args != 1) | |
4081 | break; | |
4082 | ret = io_eventfd_register(ctx, arg); | |
4083 | break; | |
4084 | case IORING_UNREGISTER_EVENTFD: | |
4085 | ret = -EINVAL; | |
4086 | if (arg || nr_args) | |
4087 | break; | |
4088 | ret = io_eventfd_unregister(ctx); | |
4089 | break; | |
edafccee JA |
4090 | default: |
4091 | ret = -EINVAL; | |
4092 | break; | |
4093 | } | |
4094 | ||
4095 | /* bring the ctx back to life */ | |
4096 | reinit_completion(&ctx->ctx_done); | |
4097 | percpu_ref_reinit(&ctx->refs); | |
4098 | return ret; | |
4099 | } | |
4100 | ||
4101 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
4102 | void __user *, arg, unsigned int, nr_args) | |
4103 | { | |
4104 | struct io_ring_ctx *ctx; | |
4105 | long ret = -EBADF; | |
4106 | struct fd f; | |
4107 | ||
4108 | f = fdget(fd); | |
4109 | if (!f.file) | |
4110 | return -EBADF; | |
4111 | ||
4112 | ret = -EOPNOTSUPP; | |
4113 | if (f.file->f_op != &io_uring_fops) | |
4114 | goto out_fput; | |
4115 | ||
4116 | ctx = f.file->private_data; | |
4117 | ||
4118 | mutex_lock(&ctx->uring_lock); | |
4119 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
4120 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
4121 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
4122 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
4123 | out_fput: |
4124 | fdput(f); | |
4125 | return ret; | |
4126 | } | |
4127 | ||
2b188cc1 JA |
4128 | static int __init io_uring_init(void) |
4129 | { | |
4130 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); | |
4131 | return 0; | |
4132 | }; | |
4133 | __initcall(io_uring_init); |