]>
Commit | Line | Data |
---|---|---|
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 | |
7 | * the application and kernel side. When the application reads the CQ ring | |
8 | * tail, it must use an appropriate smp_rmb() to order with the smp_wmb() | |
9 | * the kernel uses after writing the tail. Failure to do so could cause a | |
10 | * delay in when the application notices that completion events available. | |
11 | * This isn't a fatal condition. Likewise, the application must use an | |
12 | * appropriate smp_wmb() both before writing the SQ tail, and after writing | |
13 | * the SQ tail. The first one orders the sqe writes with the tail write, and | |
14 | * the latter is paired with the smp_rmb() the kernel will issue before | |
15 | * reading the SQ tail on submission. | |
16 | * | |
17 | * Also see the examples in the liburing library: | |
18 | * | |
19 | * git://git.kernel.dk/liburing | |
20 | * | |
21 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens | |
22 | * from data shared between the kernel and application. This is done both | |
23 | * for ordering purposes, but also to ensure that once a value is loaded from | |
24 | * data that the application could potentially modify, it remains stable. | |
25 | * | |
26 | * Copyright (C) 2018-2019 Jens Axboe | |
27 | * Copyright (c) 2018-2019 Christoph Hellwig | |
28 | */ | |
29 | #include <linux/kernel.h> | |
30 | #include <linux/init.h> | |
31 | #include <linux/errno.h> | |
32 | #include <linux/syscalls.h> | |
33 | #include <linux/compat.h> | |
34 | #include <linux/refcount.h> | |
35 | #include <linux/uio.h> | |
36 | ||
37 | #include <linux/sched/signal.h> | |
38 | #include <linux/fs.h> | |
39 | #include <linux/file.h> | |
40 | #include <linux/fdtable.h> | |
41 | #include <linux/mm.h> | |
42 | #include <linux/mman.h> | |
43 | #include <linux/mmu_context.h> | |
44 | #include <linux/percpu.h> | |
45 | #include <linux/slab.h> | |
46 | #include <linux/workqueue.h> | |
47 | #include <linux/kthread.h> | |
48 | #include <linux/blkdev.h> | |
49 | #include <linux/bvec.h> | |
50 | #include <linux/net.h> | |
51 | #include <net/sock.h> | |
52 | #include <net/af_unix.h> | |
53 | #include <net/scm.h> | |
54 | #include <linux/anon_inodes.h> | |
55 | #include <linux/sched/mm.h> | |
56 | #include <linux/uaccess.h> | |
57 | #include <linux/nospec.h> | |
58 | #include <linux/sizes.h> | |
59 | #include <linux/hugetlb.h> | |
60 | ||
61 | #include <uapi/linux/io_uring.h> | |
62 | ||
63 | #include "internal.h" | |
64 | ||
65 | #define IORING_MAX_ENTRIES 4096 | |
66 | #define IORING_MAX_FIXED_FILES 1024 | |
67 | ||
68 | struct io_uring { | |
69 | u32 head ____cacheline_aligned_in_smp; | |
70 | u32 tail ____cacheline_aligned_in_smp; | |
71 | }; | |
72 | ||
73 | struct io_sq_ring { | |
74 | struct io_uring r; | |
75 | u32 ring_mask; | |
76 | u32 ring_entries; | |
77 | u32 dropped; | |
78 | u32 flags; | |
79 | u32 array[]; | |
80 | }; | |
81 | ||
82 | struct io_cq_ring { | |
83 | struct io_uring r; | |
84 | u32 ring_mask; | |
85 | u32 ring_entries; | |
86 | u32 overflow; | |
87 | struct io_uring_cqe cqes[]; | |
88 | }; | |
89 | ||
90 | struct io_mapped_ubuf { | |
91 | u64 ubuf; | |
92 | size_t len; | |
93 | struct bio_vec *bvec; | |
94 | unsigned int nr_bvecs; | |
95 | }; | |
96 | ||
97 | struct async_list { | |
98 | spinlock_t lock; | |
99 | atomic_t cnt; | |
100 | struct list_head list; | |
101 | ||
102 | struct file *file; | |
103 | off_t io_end; | |
104 | size_t io_pages; | |
105 | }; | |
106 | ||
107 | struct io_ring_ctx { | |
108 | struct { | |
109 | struct percpu_ref refs; | |
110 | } ____cacheline_aligned_in_smp; | |
111 | ||
112 | struct { | |
113 | unsigned int flags; | |
114 | bool compat; | |
115 | bool account_mem; | |
116 | ||
117 | /* SQ ring */ | |
118 | struct io_sq_ring *sq_ring; | |
119 | unsigned cached_sq_head; | |
120 | unsigned sq_entries; | |
121 | unsigned sq_mask; | |
122 | unsigned sq_thread_idle; | |
123 | struct io_uring_sqe *sq_sqes; | |
124 | } ____cacheline_aligned_in_smp; | |
125 | ||
126 | /* IO offload */ | |
127 | struct workqueue_struct *sqo_wq; | |
128 | struct task_struct *sqo_thread; /* if using sq thread polling */ | |
129 | struct mm_struct *sqo_mm; | |
130 | wait_queue_head_t sqo_wait; | |
131 | unsigned sqo_stop; | |
132 | ||
133 | struct { | |
134 | /* CQ ring */ | |
135 | struct io_cq_ring *cq_ring; | |
136 | unsigned cached_cq_tail; | |
137 | unsigned cq_entries; | |
138 | unsigned cq_mask; | |
139 | struct wait_queue_head cq_wait; | |
140 | struct fasync_struct *cq_fasync; | |
141 | } ____cacheline_aligned_in_smp; | |
142 | ||
143 | /* | |
144 | * If used, fixed file set. Writers must ensure that ->refs is dead, | |
145 | * readers must ensure that ->refs is alive as long as the file* is | |
146 | * used. Only updated through io_uring_register(2). | |
147 | */ | |
148 | struct file **user_files; | |
149 | unsigned nr_user_files; | |
150 | ||
151 | /* if used, fixed mapped user buffers */ | |
152 | unsigned nr_user_bufs; | |
153 | struct io_mapped_ubuf *user_bufs; | |
154 | ||
155 | struct user_struct *user; | |
156 | ||
157 | struct completion ctx_done; | |
158 | ||
159 | struct { | |
160 | struct mutex uring_lock; | |
161 | wait_queue_head_t wait; | |
162 | } ____cacheline_aligned_in_smp; | |
163 | ||
164 | struct { | |
165 | spinlock_t completion_lock; | |
166 | bool poll_multi_file; | |
167 | /* | |
168 | * ->poll_list is protected by the ctx->uring_lock for | |
169 | * io_uring instances that don't use IORING_SETUP_SQPOLL. | |
170 | * For SQPOLL, only the single threaded io_sq_thread() will | |
171 | * manipulate the list, hence no extra locking is needed there. | |
172 | */ | |
173 | struct list_head poll_list; | |
174 | struct list_head cancel_list; | |
175 | } ____cacheline_aligned_in_smp; | |
176 | ||
177 | struct async_list pending_async[2]; | |
178 | ||
179 | #if defined(CONFIG_UNIX) | |
180 | struct socket *ring_sock; | |
181 | #endif | |
182 | }; | |
183 | ||
184 | struct sqe_submit { | |
185 | const struct io_uring_sqe *sqe; | |
186 | unsigned short index; | |
187 | bool has_user; | |
188 | bool needs_lock; | |
189 | bool needs_fixed_file; | |
190 | }; | |
191 | ||
192 | /* | |
193 | * First field must be the file pointer in all the | |
194 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
195 | */ | |
196 | struct io_poll_iocb { | |
197 | struct file *file; | |
198 | struct wait_queue_head *head; | |
199 | __poll_t events; | |
200 | bool woken; | |
201 | bool canceled; | |
202 | struct wait_queue_entry wait; | |
203 | }; | |
204 | ||
205 | /* | |
206 | * NOTE! Each of the iocb union members has the file pointer | |
207 | * as the first entry in their struct definition. So you can | |
208 | * access the file pointer through any of the sub-structs, | |
209 | * or directly as just 'ki_filp' in this struct. | |
210 | */ | |
211 | struct io_kiocb { | |
212 | union { | |
213 | struct file *file; | |
214 | struct kiocb rw; | |
215 | struct io_poll_iocb poll; | |
216 | }; | |
217 | ||
218 | struct sqe_submit submit; | |
219 | ||
220 | struct io_ring_ctx *ctx; | |
221 | struct list_head list; | |
222 | unsigned int flags; | |
223 | refcount_t refs; | |
224 | #define REQ_F_FORCE_NONBLOCK 1 /* inline submission attempt */ | |
225 | #define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */ | |
226 | #define REQ_F_FIXED_FILE 4 /* ctx owns file */ | |
227 | #define REQ_F_SEQ_PREV 8 /* sequential with previous */ | |
228 | #define REQ_F_PREPPED 16 /* prep already done */ | |
229 | u64 user_data; | |
230 | u64 error; | |
231 | ||
232 | struct work_struct work; | |
233 | }; | |
234 | ||
235 | #define IO_PLUG_THRESHOLD 2 | |
236 | #define IO_IOPOLL_BATCH 8 | |
237 | ||
238 | struct io_submit_state { | |
239 | struct blk_plug plug; | |
240 | ||
241 | /* | |
242 | * io_kiocb alloc cache | |
243 | */ | |
244 | void *reqs[IO_IOPOLL_BATCH]; | |
245 | unsigned int free_reqs; | |
246 | unsigned int cur_req; | |
247 | ||
248 | /* | |
249 | * File reference cache | |
250 | */ | |
251 | struct file *file; | |
252 | unsigned int fd; | |
253 | unsigned int has_refs; | |
254 | unsigned int used_refs; | |
255 | unsigned int ios_left; | |
256 | }; | |
257 | ||
258 | static struct kmem_cache *req_cachep; | |
259 | ||
260 | static const struct file_operations io_uring_fops; | |
261 | ||
262 | struct sock *io_uring_get_socket(struct file *file) | |
263 | { | |
264 | #if defined(CONFIG_UNIX) | |
265 | if (file->f_op == &io_uring_fops) { | |
266 | struct io_ring_ctx *ctx = file->private_data; | |
267 | ||
268 | return ctx->ring_sock->sk; | |
269 | } | |
270 | #endif | |
271 | return NULL; | |
272 | } | |
273 | EXPORT_SYMBOL(io_uring_get_socket); | |
274 | ||
275 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) | |
276 | { | |
277 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
278 | ||
279 | complete(&ctx->ctx_done); | |
280 | } | |
281 | ||
282 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) | |
283 | { | |
284 | struct io_ring_ctx *ctx; | |
285 | int i; | |
286 | ||
287 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
288 | if (!ctx) | |
289 | return NULL; | |
290 | ||
291 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, 0, GFP_KERNEL)) { | |
292 | kfree(ctx); | |
293 | return NULL; | |
294 | } | |
295 | ||
296 | ctx->flags = p->flags; | |
297 | init_waitqueue_head(&ctx->cq_wait); | |
298 | init_completion(&ctx->ctx_done); | |
299 | mutex_init(&ctx->uring_lock); | |
300 | init_waitqueue_head(&ctx->wait); | |
301 | for (i = 0; i < ARRAY_SIZE(ctx->pending_async); i++) { | |
302 | spin_lock_init(&ctx->pending_async[i].lock); | |
303 | INIT_LIST_HEAD(&ctx->pending_async[i].list); | |
304 | atomic_set(&ctx->pending_async[i].cnt, 0); | |
305 | } | |
306 | spin_lock_init(&ctx->completion_lock); | |
307 | INIT_LIST_HEAD(&ctx->poll_list); | |
308 | INIT_LIST_HEAD(&ctx->cancel_list); | |
309 | return ctx; | |
310 | } | |
311 | ||
312 | static void io_commit_cqring(struct io_ring_ctx *ctx) | |
313 | { | |
314 | struct io_cq_ring *ring = ctx->cq_ring; | |
315 | ||
316 | if (ctx->cached_cq_tail != READ_ONCE(ring->r.tail)) { | |
317 | /* order cqe stores with ring update */ | |
318 | smp_store_release(&ring->r.tail, ctx->cached_cq_tail); | |
319 | ||
320 | /* | |
321 | * Write sider barrier of tail update, app has read side. See | |
322 | * comment at the top of this file. | |
323 | */ | |
324 | smp_wmb(); | |
325 | ||
326 | if (wq_has_sleeper(&ctx->cq_wait)) { | |
327 | wake_up_interruptible(&ctx->cq_wait); | |
328 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
329 | } | |
330 | } | |
331 | } | |
332 | ||
333 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) | |
334 | { | |
335 | struct io_cq_ring *ring = ctx->cq_ring; | |
336 | unsigned tail; | |
337 | ||
338 | tail = ctx->cached_cq_tail; | |
339 | /* See comment at the top of the file */ | |
340 | smp_rmb(); | |
341 | if (tail + 1 == READ_ONCE(ring->r.head)) | |
342 | return NULL; | |
343 | ||
344 | ctx->cached_cq_tail++; | |
345 | return &ring->cqes[tail & ctx->cq_mask]; | |
346 | } | |
347 | ||
348 | static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data, | |
349 | long res, unsigned ev_flags) | |
350 | { | |
351 | struct io_uring_cqe *cqe; | |
352 | ||
353 | /* | |
354 | * If we can't get a cq entry, userspace overflowed the | |
355 | * submission (by quite a lot). Increment the overflow count in | |
356 | * the ring. | |
357 | */ | |
358 | cqe = io_get_cqring(ctx); | |
359 | if (cqe) { | |
360 | WRITE_ONCE(cqe->user_data, ki_user_data); | |
361 | WRITE_ONCE(cqe->res, res); | |
362 | WRITE_ONCE(cqe->flags, ev_flags); | |
363 | } else { | |
364 | unsigned overflow = READ_ONCE(ctx->cq_ring->overflow); | |
365 | ||
366 | WRITE_ONCE(ctx->cq_ring->overflow, overflow + 1); | |
367 | } | |
368 | } | |
369 | ||
370 | static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 ki_user_data, | |
371 | long res, unsigned ev_flags) | |
372 | { | |
373 | unsigned long flags; | |
374 | ||
375 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
376 | io_cqring_fill_event(ctx, ki_user_data, res, ev_flags); | |
377 | io_commit_cqring(ctx); | |
378 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
379 | ||
380 | if (waitqueue_active(&ctx->wait)) | |
381 | wake_up(&ctx->wait); | |
382 | if (waitqueue_active(&ctx->sqo_wait)) | |
383 | wake_up(&ctx->sqo_wait); | |
384 | } | |
385 | ||
386 | static void io_ring_drop_ctx_refs(struct io_ring_ctx *ctx, unsigned refs) | |
387 | { | |
388 | percpu_ref_put_many(&ctx->refs, refs); | |
389 | ||
390 | if (waitqueue_active(&ctx->wait)) | |
391 | wake_up(&ctx->wait); | |
392 | } | |
393 | ||
394 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, | |
395 | struct io_submit_state *state) | |
396 | { | |
397 | struct io_kiocb *req; | |
398 | ||
399 | if (!percpu_ref_tryget(&ctx->refs)) | |
400 | return NULL; | |
401 | ||
402 | if (!state) { | |
403 | req = kmem_cache_alloc(req_cachep, __GFP_NOWARN); | |
404 | if (unlikely(!req)) | |
405 | goto out; | |
406 | } else if (!state->free_reqs) { | |
407 | size_t sz; | |
408 | int ret; | |
409 | ||
410 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); | |
411 | ret = kmem_cache_alloc_bulk(req_cachep, __GFP_NOWARN, sz, | |
412 | state->reqs); | |
413 | if (unlikely(ret <= 0)) | |
414 | goto out; | |
415 | state->free_reqs = ret - 1; | |
416 | state->cur_req = 1; | |
417 | req = state->reqs[0]; | |
418 | } else { | |
419 | req = state->reqs[state->cur_req]; | |
420 | state->free_reqs--; | |
421 | state->cur_req++; | |
422 | } | |
423 | ||
424 | req->ctx = ctx; | |
425 | req->flags = 0; | |
426 | /* one is dropped after submission, the other at completion */ | |
427 | refcount_set(&req->refs, 2); | |
428 | return req; | |
429 | out: | |
430 | io_ring_drop_ctx_refs(ctx, 1); | |
431 | return NULL; | |
432 | } | |
433 | ||
434 | static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr) | |
435 | { | |
436 | if (*nr) { | |
437 | kmem_cache_free_bulk(req_cachep, *nr, reqs); | |
438 | io_ring_drop_ctx_refs(ctx, *nr); | |
439 | *nr = 0; | |
440 | } | |
441 | } | |
442 | ||
443 | static void io_free_req(struct io_kiocb *req) | |
444 | { | |
445 | if (req->file && !(req->flags & REQ_F_FIXED_FILE)) | |
446 | fput(req->file); | |
447 | io_ring_drop_ctx_refs(req->ctx, 1); | |
448 | kmem_cache_free(req_cachep, req); | |
449 | } | |
450 | ||
451 | static void io_put_req(struct io_kiocb *req) | |
452 | { | |
453 | if (refcount_dec_and_test(&req->refs)) | |
454 | io_free_req(req); | |
455 | } | |
456 | ||
457 | /* | |
458 | * Find and free completed poll iocbs | |
459 | */ | |
460 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
461 | struct list_head *done) | |
462 | { | |
463 | void *reqs[IO_IOPOLL_BATCH]; | |
464 | struct io_kiocb *req; | |
465 | int to_free; | |
466 | ||
467 | to_free = 0; | |
468 | while (!list_empty(done)) { | |
469 | req = list_first_entry(done, struct io_kiocb, list); | |
470 | list_del(&req->list); | |
471 | ||
472 | io_cqring_fill_event(ctx, req->user_data, req->error, 0); | |
473 | (*nr_events)++; | |
474 | ||
475 | if (refcount_dec_and_test(&req->refs)) { | |
476 | /* If we're not using fixed files, we have to pair the | |
477 | * completion part with the file put. Use regular | |
478 | * completions for those, only batch free for fixed | |
479 | * file. | |
480 | */ | |
481 | if (req->flags & REQ_F_FIXED_FILE) { | |
482 | reqs[to_free++] = req; | |
483 | if (to_free == ARRAY_SIZE(reqs)) | |
484 | io_free_req_many(ctx, reqs, &to_free); | |
485 | } else { | |
486 | io_free_req(req); | |
487 | } | |
488 | } | |
489 | } | |
490 | ||
491 | io_commit_cqring(ctx); | |
492 | io_free_req_many(ctx, reqs, &to_free); | |
493 | } | |
494 | ||
495 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
496 | long min) | |
497 | { | |
498 | struct io_kiocb *req, *tmp; | |
499 | LIST_HEAD(done); | |
500 | bool spin; | |
501 | int ret; | |
502 | ||
503 | /* | |
504 | * Only spin for completions if we don't have multiple devices hanging | |
505 | * off our complete list, and we're under the requested amount. | |
506 | */ | |
507 | spin = !ctx->poll_multi_file && *nr_events < min; | |
508 | ||
509 | ret = 0; | |
510 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { | |
511 | struct kiocb *kiocb = &req->rw; | |
512 | ||
513 | /* | |
514 | * Move completed entries to our local list. If we find a | |
515 | * request that requires polling, break out and complete | |
516 | * the done list first, if we have entries there. | |
517 | */ | |
518 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { | |
519 | list_move_tail(&req->list, &done); | |
520 | continue; | |
521 | } | |
522 | if (!list_empty(&done)) | |
523 | break; | |
524 | ||
525 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
526 | if (ret < 0) | |
527 | break; | |
528 | ||
529 | if (ret && spin) | |
530 | spin = false; | |
531 | ret = 0; | |
532 | } | |
533 | ||
534 | if (!list_empty(&done)) | |
535 | io_iopoll_complete(ctx, nr_events, &done); | |
536 | ||
537 | return ret; | |
538 | } | |
539 | ||
540 | /* | |
541 | * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a | |
542 | * non-spinning poll check - we'll still enter the driver poll loop, but only | |
543 | * as a non-spinning completion check. | |
544 | */ | |
545 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
546 | long min) | |
547 | { | |
548 | while (!list_empty(&ctx->poll_list)) { | |
549 | int ret; | |
550 | ||
551 | ret = io_do_iopoll(ctx, nr_events, min); | |
552 | if (ret < 0) | |
553 | return ret; | |
554 | if (!min || *nr_events >= min) | |
555 | return 0; | |
556 | } | |
557 | ||
558 | return 1; | |
559 | } | |
560 | ||
561 | /* | |
562 | * We can't just wait for polled events to come to us, we have to actively | |
563 | * find and complete them. | |
564 | */ | |
565 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) | |
566 | { | |
567 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
568 | return; | |
569 | ||
570 | mutex_lock(&ctx->uring_lock); | |
571 | while (!list_empty(&ctx->poll_list)) { | |
572 | unsigned int nr_events = 0; | |
573 | ||
574 | io_iopoll_getevents(ctx, &nr_events, 1); | |
575 | } | |
576 | mutex_unlock(&ctx->uring_lock); | |
577 | } | |
578 | ||
579 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, | |
580 | long min) | |
581 | { | |
582 | int ret = 0; | |
583 | ||
584 | do { | |
585 | int tmin = 0; | |
586 | ||
587 | if (*nr_events < min) | |
588 | tmin = min - *nr_events; | |
589 | ||
590 | ret = io_iopoll_getevents(ctx, nr_events, tmin); | |
591 | if (ret <= 0) | |
592 | break; | |
593 | ret = 0; | |
594 | } while (min && !*nr_events && !need_resched()); | |
595 | ||
596 | return ret; | |
597 | } | |
598 | ||
599 | static void kiocb_end_write(struct kiocb *kiocb) | |
600 | { | |
601 | if (kiocb->ki_flags & IOCB_WRITE) { | |
602 | struct inode *inode = file_inode(kiocb->ki_filp); | |
603 | ||
604 | /* | |
605 | * Tell lockdep we inherited freeze protection from submission | |
606 | * thread. | |
607 | */ | |
608 | if (S_ISREG(inode->i_mode)) | |
609 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); | |
610 | file_end_write(kiocb->ki_filp); | |
611 | } | |
612 | } | |
613 | ||
614 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
615 | { | |
616 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw); | |
617 | ||
618 | kiocb_end_write(kiocb); | |
619 | ||
620 | io_cqring_add_event(req->ctx, req->user_data, res, 0); | |
621 | io_put_req(req); | |
622 | } | |
623 | ||
624 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) | |
625 | { | |
626 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw); | |
627 | ||
628 | kiocb_end_write(kiocb); | |
629 | ||
630 | req->error = res; | |
631 | if (res != -EAGAIN) | |
632 | req->flags |= REQ_F_IOPOLL_COMPLETED; | |
633 | } | |
634 | ||
635 | /* | |
636 | * After the iocb has been issued, it's safe to be found on the poll list. | |
637 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
638 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
639 | * accessing the kiocb cookie. | |
640 | */ | |
641 | static void io_iopoll_req_issued(struct io_kiocb *req) | |
642 | { | |
643 | struct io_ring_ctx *ctx = req->ctx; | |
644 | ||
645 | /* | |
646 | * Track whether we have multiple files in our lists. This will impact | |
647 | * how we do polling eventually, not spinning if we're on potentially | |
648 | * different devices. | |
649 | */ | |
650 | if (list_empty(&ctx->poll_list)) { | |
651 | ctx->poll_multi_file = false; | |
652 | } else if (!ctx->poll_multi_file) { | |
653 | struct io_kiocb *list_req; | |
654 | ||
655 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, | |
656 | list); | |
657 | if (list_req->rw.ki_filp != req->rw.ki_filp) | |
658 | ctx->poll_multi_file = true; | |
659 | } | |
660 | ||
661 | /* | |
662 | * For fast devices, IO may have already completed. If it has, add | |
663 | * it to the front so we find it first. | |
664 | */ | |
665 | if (req->flags & REQ_F_IOPOLL_COMPLETED) | |
666 | list_add(&req->list, &ctx->poll_list); | |
667 | else | |
668 | list_add_tail(&req->list, &ctx->poll_list); | |
669 | } | |
670 | ||
671 | static void io_file_put(struct io_submit_state *state, struct file *file) | |
672 | { | |
673 | if (!state) { | |
674 | fput(file); | |
675 | } else if (state->file) { | |
676 | int diff = state->has_refs - state->used_refs; | |
677 | ||
678 | if (diff) | |
679 | fput_many(state->file, diff); | |
680 | state->file = NULL; | |
681 | } | |
682 | } | |
683 | ||
684 | /* | |
685 | * Get as many references to a file as we have IOs left in this submission, | |
686 | * assuming most submissions are for one file, or at least that each file | |
687 | * has more than one submission. | |
688 | */ | |
689 | static struct file *io_file_get(struct io_submit_state *state, int fd) | |
690 | { | |
691 | if (!state) | |
692 | return fget(fd); | |
693 | ||
694 | if (state->file) { | |
695 | if (state->fd == fd) { | |
696 | state->used_refs++; | |
697 | state->ios_left--; | |
698 | return state->file; | |
699 | } | |
700 | io_file_put(state, NULL); | |
701 | } | |
702 | state->file = fget_many(fd, state->ios_left); | |
703 | if (!state->file) | |
704 | return NULL; | |
705 | ||
706 | state->fd = fd; | |
707 | state->has_refs = state->ios_left; | |
708 | state->used_refs = 1; | |
709 | state->ios_left--; | |
710 | return state->file; | |
711 | } | |
712 | ||
713 | /* | |
714 | * If we tracked the file through the SCM inflight mechanism, we could support | |
715 | * any file. For now, just ensure that anything potentially problematic is done | |
716 | * inline. | |
717 | */ | |
718 | static bool io_file_supports_async(struct file *file) | |
719 | { | |
720 | umode_t mode = file_inode(file)->i_mode; | |
721 | ||
722 | if (S_ISBLK(mode) || S_ISCHR(mode)) | |
723 | return true; | |
724 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) | |
725 | return true; | |
726 | ||
727 | return false; | |
728 | } | |
729 | ||
730 | static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s, | |
731 | bool force_nonblock, struct io_submit_state *state) | |
732 | { | |
733 | const struct io_uring_sqe *sqe = s->sqe; | |
734 | struct io_ring_ctx *ctx = req->ctx; | |
735 | struct kiocb *kiocb = &req->rw; | |
736 | unsigned ioprio; | |
737 | int ret; | |
738 | ||
739 | if (!req->file) | |
740 | return -EBADF; | |
741 | /* For -EAGAIN retry, everything is already prepped */ | |
742 | if (req->flags & REQ_F_PREPPED) | |
743 | return 0; | |
744 | ||
745 | if (force_nonblock && !io_file_supports_async(req->file)) | |
746 | force_nonblock = false; | |
747 | ||
748 | kiocb->ki_pos = READ_ONCE(sqe->off); | |
749 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); | |
750 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); | |
751 | ||
752 | ioprio = READ_ONCE(sqe->ioprio); | |
753 | if (ioprio) { | |
754 | ret = ioprio_check_cap(ioprio); | |
755 | if (ret) | |
756 | return ret; | |
757 | ||
758 | kiocb->ki_ioprio = ioprio; | |
759 | } else | |
760 | kiocb->ki_ioprio = get_current_ioprio(); | |
761 | ||
762 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
763 | if (unlikely(ret)) | |
764 | return ret; | |
765 | if (force_nonblock) { | |
766 | kiocb->ki_flags |= IOCB_NOWAIT; | |
767 | req->flags |= REQ_F_FORCE_NONBLOCK; | |
768 | } | |
769 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
770 | if (!(kiocb->ki_flags & IOCB_DIRECT) || | |
771 | !kiocb->ki_filp->f_op->iopoll) | |
772 | return -EOPNOTSUPP; | |
773 | ||
774 | req->error = 0; | |
775 | kiocb->ki_flags |= IOCB_HIPRI; | |
776 | kiocb->ki_complete = io_complete_rw_iopoll; | |
777 | } else { | |
778 | if (kiocb->ki_flags & IOCB_HIPRI) | |
779 | return -EINVAL; | |
780 | kiocb->ki_complete = io_complete_rw; | |
781 | } | |
782 | req->flags |= REQ_F_PREPPED; | |
783 | return 0; | |
784 | } | |
785 | ||
786 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
787 | { | |
788 | switch (ret) { | |
789 | case -EIOCBQUEUED: | |
790 | break; | |
791 | case -ERESTARTSYS: | |
792 | case -ERESTARTNOINTR: | |
793 | case -ERESTARTNOHAND: | |
794 | case -ERESTART_RESTARTBLOCK: | |
795 | /* | |
796 | * We can't just restart the syscall, since previously | |
797 | * submitted sqes may already be in progress. Just fail this | |
798 | * IO with EINTR. | |
799 | */ | |
800 | ret = -EINTR; | |
801 | /* fall through */ | |
802 | default: | |
803 | kiocb->ki_complete(kiocb, ret, 0); | |
804 | } | |
805 | } | |
806 | ||
807 | static int io_import_fixed(struct io_ring_ctx *ctx, int rw, | |
808 | const struct io_uring_sqe *sqe, | |
809 | struct iov_iter *iter) | |
810 | { | |
811 | size_t len = READ_ONCE(sqe->len); | |
812 | struct io_mapped_ubuf *imu; | |
813 | unsigned index, buf_index; | |
814 | size_t offset; | |
815 | u64 buf_addr; | |
816 | ||
817 | /* attempt to use fixed buffers without having provided iovecs */ | |
818 | if (unlikely(!ctx->user_bufs)) | |
819 | return -EFAULT; | |
820 | ||
821 | buf_index = READ_ONCE(sqe->buf_index); | |
822 | if (unlikely(buf_index >= ctx->nr_user_bufs)) | |
823 | return -EFAULT; | |
824 | ||
825 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); | |
826 | imu = &ctx->user_bufs[index]; | |
827 | buf_addr = READ_ONCE(sqe->addr); | |
828 | ||
829 | /* overflow */ | |
830 | if (buf_addr + len < buf_addr) | |
831 | return -EFAULT; | |
832 | /* not inside the mapped region */ | |
833 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) | |
834 | return -EFAULT; | |
835 | ||
836 | /* | |
837 | * May not be a start of buffer, set size appropriately | |
838 | * and advance us to the beginning. | |
839 | */ | |
840 | offset = buf_addr - imu->ubuf; | |
841 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
842 | if (offset) | |
843 | iov_iter_advance(iter, offset); | |
844 | return 0; | |
845 | } | |
846 | ||
847 | static int io_import_iovec(struct io_ring_ctx *ctx, int rw, | |
848 | const struct sqe_submit *s, struct iovec **iovec, | |
849 | struct iov_iter *iter) | |
850 | { | |
851 | const struct io_uring_sqe *sqe = s->sqe; | |
852 | void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
853 | size_t sqe_len = READ_ONCE(sqe->len); | |
854 | u8 opcode; | |
855 | ||
856 | /* | |
857 | * We're reading ->opcode for the second time, but the first read | |
858 | * doesn't care whether it's _FIXED or not, so it doesn't matter | |
859 | * whether ->opcode changes concurrently. The first read does care | |
860 | * about whether it is a READ or a WRITE, so we don't trust this read | |
861 | * for that purpose and instead let the caller pass in the read/write | |
862 | * flag. | |
863 | */ | |
864 | opcode = READ_ONCE(sqe->opcode); | |
865 | if (opcode == IORING_OP_READ_FIXED || | |
866 | opcode == IORING_OP_WRITE_FIXED) { | |
867 | int ret = io_import_fixed(ctx, rw, sqe, iter); | |
868 | *iovec = NULL; | |
869 | return ret; | |
870 | } | |
871 | ||
872 | if (!s->has_user) | |
873 | return -EFAULT; | |
874 | ||
875 | #ifdef CONFIG_COMPAT | |
876 | if (ctx->compat) | |
877 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, | |
878 | iovec, iter); | |
879 | #endif | |
880 | ||
881 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); | |
882 | } | |
883 | ||
884 | /* | |
885 | * Make a note of the last file/offset/direction we punted to async | |
886 | * context. We'll use this information to see if we can piggy back a | |
887 | * sequential request onto the previous one, if it's still hasn't been | |
888 | * completed by the async worker. | |
889 | */ | |
890 | static void io_async_list_note(int rw, struct io_kiocb *req, size_t len) | |
891 | { | |
892 | struct async_list *async_list = &req->ctx->pending_async[rw]; | |
893 | struct kiocb *kiocb = &req->rw; | |
894 | struct file *filp = kiocb->ki_filp; | |
895 | off_t io_end = kiocb->ki_pos + len; | |
896 | ||
897 | if (filp == async_list->file && kiocb->ki_pos == async_list->io_end) { | |
898 | unsigned long max_pages; | |
899 | ||
900 | /* Use 8x RA size as a decent limiter for both reads/writes */ | |
901 | max_pages = filp->f_ra.ra_pages; | |
902 | if (!max_pages) | |
903 | max_pages = VM_MAX_READAHEAD >> (PAGE_SHIFT - 10); | |
904 | max_pages *= 8; | |
905 | ||
906 | /* If max pages are exceeded, reset the state */ | |
907 | len >>= PAGE_SHIFT; | |
908 | if (async_list->io_pages + len <= max_pages) { | |
909 | req->flags |= REQ_F_SEQ_PREV; | |
910 | async_list->io_pages += len; | |
911 | } else { | |
912 | io_end = 0; | |
913 | async_list->io_pages = 0; | |
914 | } | |
915 | } | |
916 | ||
917 | /* New file? Reset state. */ | |
918 | if (async_list->file != filp) { | |
919 | async_list->io_pages = 0; | |
920 | async_list->file = filp; | |
921 | } | |
922 | async_list->io_end = io_end; | |
923 | } | |
924 | ||
925 | static int io_read(struct io_kiocb *req, const struct sqe_submit *s, | |
926 | bool force_nonblock, struct io_submit_state *state) | |
927 | { | |
928 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
929 | struct kiocb *kiocb = &req->rw; | |
930 | struct iov_iter iter; | |
931 | struct file *file; | |
932 | size_t iov_count; | |
933 | int ret; | |
934 | ||
935 | ret = io_prep_rw(req, s, force_nonblock, state); | |
936 | if (ret) | |
937 | return ret; | |
938 | file = kiocb->ki_filp; | |
939 | ||
940 | if (unlikely(!(file->f_mode & FMODE_READ))) | |
941 | return -EBADF; | |
942 | if (unlikely(!file->f_op->read_iter)) | |
943 | return -EINVAL; | |
944 | ||
945 | ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter); | |
946 | if (ret) | |
947 | return ret; | |
948 | ||
949 | iov_count = iov_iter_count(&iter); | |
950 | ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count); | |
951 | if (!ret) { | |
952 | ssize_t ret2; | |
953 | ||
954 | /* Catch -EAGAIN return for forced non-blocking submission */ | |
955 | ret2 = call_read_iter(file, kiocb, &iter); | |
956 | if (!force_nonblock || ret2 != -EAGAIN) { | |
957 | io_rw_done(kiocb, ret2); | |
958 | } else { | |
959 | /* | |
960 | * If ->needs_lock is true, we're already in async | |
961 | * context. | |
962 | */ | |
963 | if (!s->needs_lock) | |
964 | io_async_list_note(READ, req, iov_count); | |
965 | ret = -EAGAIN; | |
966 | } | |
967 | } | |
968 | kfree(iovec); | |
969 | return ret; | |
970 | } | |
971 | ||
972 | static int io_write(struct io_kiocb *req, const struct sqe_submit *s, | |
973 | bool force_nonblock, struct io_submit_state *state) | |
974 | { | |
975 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
976 | struct kiocb *kiocb = &req->rw; | |
977 | struct iov_iter iter; | |
978 | struct file *file; | |
979 | size_t iov_count; | |
980 | int ret; | |
981 | ||
982 | ret = io_prep_rw(req, s, force_nonblock, state); | |
983 | if (ret) | |
984 | return ret; | |
985 | ||
986 | file = kiocb->ki_filp; | |
987 | if (unlikely(!(file->f_mode & FMODE_WRITE))) | |
988 | return -EBADF; | |
989 | if (unlikely(!file->f_op->write_iter)) | |
990 | return -EINVAL; | |
991 | ||
992 | ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter); | |
993 | if (ret) | |
994 | return ret; | |
995 | ||
996 | iov_count = iov_iter_count(&iter); | |
997 | ||
998 | ret = -EAGAIN; | |
999 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) { | |
1000 | /* If ->needs_lock is true, we're already in async context. */ | |
1001 | if (!s->needs_lock) | |
1002 | io_async_list_note(WRITE, req, iov_count); | |
1003 | goto out_free; | |
1004 | } | |
1005 | ||
1006 | ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count); | |
1007 | if (!ret) { | |
1008 | /* | |
1009 | * Open-code file_start_write here to grab freeze protection, | |
1010 | * which will be released by another thread in | |
1011 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
1012 | * released so that it doesn't complain about the held lock when | |
1013 | * we return to userspace. | |
1014 | */ | |
1015 | if (S_ISREG(file_inode(file)->i_mode)) { | |
1016 | __sb_start_write(file_inode(file)->i_sb, | |
1017 | SB_FREEZE_WRITE, true); | |
1018 | __sb_writers_release(file_inode(file)->i_sb, | |
1019 | SB_FREEZE_WRITE); | |
1020 | } | |
1021 | kiocb->ki_flags |= IOCB_WRITE; | |
1022 | io_rw_done(kiocb, call_write_iter(file, kiocb, &iter)); | |
1023 | } | |
1024 | out_free: | |
1025 | kfree(iovec); | |
1026 | return ret; | |
1027 | } | |
1028 | ||
1029 | /* | |
1030 | * IORING_OP_NOP just posts a completion event, nothing else. | |
1031 | */ | |
1032 | static int io_nop(struct io_kiocb *req, u64 user_data) | |
1033 | { | |
1034 | struct io_ring_ctx *ctx = req->ctx; | |
1035 | long err = 0; | |
1036 | ||
1037 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) | |
1038 | return -EINVAL; | |
1039 | ||
1040 | io_cqring_add_event(ctx, user_data, err, 0); | |
1041 | io_put_req(req); | |
1042 | return 0; | |
1043 | } | |
1044 | ||
1045 | static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
1046 | { | |
1047 | struct io_ring_ctx *ctx = req->ctx; | |
1048 | ||
1049 | if (!req->file) | |
1050 | return -EBADF; | |
1051 | /* Prep already done (EAGAIN retry) */ | |
1052 | if (req->flags & REQ_F_PREPPED) | |
1053 | return 0; | |
1054 | ||
1055 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) | |
1056 | return -EINVAL; | |
1057 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
1058 | return -EINVAL; | |
1059 | ||
1060 | req->flags |= REQ_F_PREPPED; | |
1061 | return 0; | |
1062 | } | |
1063 | ||
1064 | static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe, | |
1065 | bool force_nonblock) | |
1066 | { | |
1067 | loff_t sqe_off = READ_ONCE(sqe->off); | |
1068 | loff_t sqe_len = READ_ONCE(sqe->len); | |
1069 | loff_t end = sqe_off + sqe_len; | |
1070 | unsigned fsync_flags; | |
1071 | int ret; | |
1072 | ||
1073 | fsync_flags = READ_ONCE(sqe->fsync_flags); | |
1074 | if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC)) | |
1075 | return -EINVAL; | |
1076 | ||
1077 | ret = io_prep_fsync(req, sqe); | |
1078 | if (ret) | |
1079 | return ret; | |
1080 | ||
1081 | /* fsync always requires a blocking context */ | |
1082 | if (force_nonblock) | |
1083 | return -EAGAIN; | |
1084 | ||
1085 | ret = vfs_fsync_range(req->rw.ki_filp, sqe_off, | |
1086 | end > 0 ? end : LLONG_MAX, | |
1087 | fsync_flags & IORING_FSYNC_DATASYNC); | |
1088 | ||
1089 | io_cqring_add_event(req->ctx, sqe->user_data, ret, 0); | |
1090 | io_put_req(req); | |
1091 | return 0; | |
1092 | } | |
1093 | ||
1094 | static void io_poll_remove_one(struct io_kiocb *req) | |
1095 | { | |
1096 | struct io_poll_iocb *poll = &req->poll; | |
1097 | ||
1098 | spin_lock(&poll->head->lock); | |
1099 | WRITE_ONCE(poll->canceled, true); | |
1100 | if (!list_empty(&poll->wait.entry)) { | |
1101 | list_del_init(&poll->wait.entry); | |
1102 | queue_work(req->ctx->sqo_wq, &req->work); | |
1103 | } | |
1104 | spin_unlock(&poll->head->lock); | |
1105 | ||
1106 | list_del_init(&req->list); | |
1107 | } | |
1108 | ||
1109 | static void io_poll_remove_all(struct io_ring_ctx *ctx) | |
1110 | { | |
1111 | struct io_kiocb *req; | |
1112 | ||
1113 | spin_lock_irq(&ctx->completion_lock); | |
1114 | while (!list_empty(&ctx->cancel_list)) { | |
1115 | req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list); | |
1116 | io_poll_remove_one(req); | |
1117 | } | |
1118 | spin_unlock_irq(&ctx->completion_lock); | |
1119 | } | |
1120 | ||
1121 | /* | |
1122 | * Find a running poll command that matches one specified in sqe->addr, | |
1123 | * and remove it if found. | |
1124 | */ | |
1125 | static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
1126 | { | |
1127 | struct io_ring_ctx *ctx = req->ctx; | |
1128 | struct io_kiocb *poll_req, *next; | |
1129 | int ret = -ENOENT; | |
1130 | ||
1131 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
1132 | return -EINVAL; | |
1133 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
1134 | sqe->poll_events) | |
1135 | return -EINVAL; | |
1136 | ||
1137 | spin_lock_irq(&ctx->completion_lock); | |
1138 | list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) { | |
1139 | if (READ_ONCE(sqe->addr) == poll_req->user_data) { | |
1140 | io_poll_remove_one(poll_req); | |
1141 | ret = 0; | |
1142 | break; | |
1143 | } | |
1144 | } | |
1145 | spin_unlock_irq(&ctx->completion_lock); | |
1146 | ||
1147 | io_cqring_add_event(req->ctx, sqe->user_data, ret, 0); | |
1148 | io_put_req(req); | |
1149 | return 0; | |
1150 | } | |
1151 | ||
1152 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask) | |
1153 | { | |
1154 | io_cqring_add_event(req->ctx, req->user_data, mangle_poll(mask), 0); | |
1155 | io_put_req(req); | |
1156 | } | |
1157 | ||
1158 | static void io_poll_complete_work(struct work_struct *work) | |
1159 | { | |
1160 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
1161 | struct io_poll_iocb *poll = &req->poll; | |
1162 | struct poll_table_struct pt = { ._key = poll->events }; | |
1163 | struct io_ring_ctx *ctx = req->ctx; | |
1164 | __poll_t mask = 0; | |
1165 | ||
1166 | if (!READ_ONCE(poll->canceled)) | |
1167 | mask = vfs_poll(poll->file, &pt) & poll->events; | |
1168 | ||
1169 | /* | |
1170 | * Note that ->ki_cancel callers also delete iocb from active_reqs after | |
1171 | * calling ->ki_cancel. We need the ctx_lock roundtrip here to | |
1172 | * synchronize with them. In the cancellation case the list_del_init | |
1173 | * itself is not actually needed, but harmless so we keep it in to | |
1174 | * avoid further branches in the fast path. | |
1175 | */ | |
1176 | spin_lock_irq(&ctx->completion_lock); | |
1177 | if (!mask && !READ_ONCE(poll->canceled)) { | |
1178 | add_wait_queue(poll->head, &poll->wait); | |
1179 | spin_unlock_irq(&ctx->completion_lock); | |
1180 | return; | |
1181 | } | |
1182 | list_del_init(&req->list); | |
1183 | spin_unlock_irq(&ctx->completion_lock); | |
1184 | ||
1185 | io_poll_complete(req, mask); | |
1186 | } | |
1187 | ||
1188 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
1189 | void *key) | |
1190 | { | |
1191 | struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb, | |
1192 | wait); | |
1193 | struct io_kiocb *req = container_of(poll, struct io_kiocb, poll); | |
1194 | struct io_ring_ctx *ctx = req->ctx; | |
1195 | __poll_t mask = key_to_poll(key); | |
1196 | ||
1197 | poll->woken = true; | |
1198 | ||
1199 | /* for instances that support it check for an event match first: */ | |
1200 | if (mask) { | |
1201 | unsigned long flags; | |
1202 | ||
1203 | if (!(mask & poll->events)) | |
1204 | return 0; | |
1205 | ||
1206 | /* try to complete the iocb inline if we can: */ | |
1207 | if (spin_trylock_irqsave(&ctx->completion_lock, flags)) { | |
1208 | list_del(&req->list); | |
1209 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
1210 | ||
1211 | list_del_init(&poll->wait.entry); | |
1212 | io_poll_complete(req, mask); | |
1213 | return 1; | |
1214 | } | |
1215 | } | |
1216 | ||
1217 | list_del_init(&poll->wait.entry); | |
1218 | queue_work(ctx->sqo_wq, &req->work); | |
1219 | return 1; | |
1220 | } | |
1221 | ||
1222 | struct io_poll_table { | |
1223 | struct poll_table_struct pt; | |
1224 | struct io_kiocb *req; | |
1225 | int error; | |
1226 | }; | |
1227 | ||
1228 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, | |
1229 | struct poll_table_struct *p) | |
1230 | { | |
1231 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
1232 | ||
1233 | if (unlikely(pt->req->poll.head)) { | |
1234 | pt->error = -EINVAL; | |
1235 | return; | |
1236 | } | |
1237 | ||
1238 | pt->error = 0; | |
1239 | pt->req->poll.head = head; | |
1240 | add_wait_queue(head, &pt->req->poll.wait); | |
1241 | } | |
1242 | ||
1243 | static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
1244 | { | |
1245 | struct io_poll_iocb *poll = &req->poll; | |
1246 | struct io_ring_ctx *ctx = req->ctx; | |
1247 | struct io_poll_table ipt; | |
1248 | __poll_t mask; | |
1249 | u16 events; | |
1250 | ||
1251 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
1252 | return -EINVAL; | |
1253 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
1254 | return -EINVAL; | |
1255 | if (!poll->file) | |
1256 | return -EBADF; | |
1257 | ||
1258 | INIT_WORK(&req->work, io_poll_complete_work); | |
1259 | events = READ_ONCE(sqe->poll_events); | |
1260 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; | |
1261 | ||
1262 | poll->head = NULL; | |
1263 | poll->woken = false; | |
1264 | poll->canceled = false; | |
1265 | ||
1266 | ipt.pt._qproc = io_poll_queue_proc; | |
1267 | ipt.pt._key = poll->events; | |
1268 | ipt.req = req; | |
1269 | ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */ | |
1270 | ||
1271 | /* initialized the list so that we can do list_empty checks */ | |
1272 | INIT_LIST_HEAD(&poll->wait.entry); | |
1273 | init_waitqueue_func_entry(&poll->wait, io_poll_wake); | |
1274 | ||
1275 | mask = vfs_poll(poll->file, &ipt.pt) & poll->events; | |
1276 | if (unlikely(!poll->head)) { | |
1277 | /* we did not manage to set up a waitqueue, done */ | |
1278 | goto out; | |
1279 | } | |
1280 | ||
1281 | spin_lock_irq(&ctx->completion_lock); | |
1282 | spin_lock(&poll->head->lock); | |
1283 | if (poll->woken) { | |
1284 | /* wake_up context handles the rest */ | |
1285 | mask = 0; | |
1286 | ipt.error = 0; | |
1287 | } else if (mask || ipt.error) { | |
1288 | /* if we get an error or a mask we are done */ | |
1289 | WARN_ON_ONCE(list_empty(&poll->wait.entry)); | |
1290 | list_del_init(&poll->wait.entry); | |
1291 | } else { | |
1292 | /* actually waiting for an event */ | |
1293 | list_add_tail(&req->list, &ctx->cancel_list); | |
1294 | } | |
1295 | spin_unlock(&poll->head->lock); | |
1296 | spin_unlock_irq(&ctx->completion_lock); | |
1297 | ||
1298 | out: | |
1299 | if (unlikely(ipt.error)) { | |
1300 | /* | |
1301 | * Drop one of our refs to this req, __io_submit_sqe() will | |
1302 | * drop the other one since we're returning an error. | |
1303 | */ | |
1304 | io_put_req(req); | |
1305 | return ipt.error; | |
1306 | } | |
1307 | ||
1308 | if (mask) | |
1309 | io_poll_complete(req, mask); | |
1310 | return 0; | |
1311 | } | |
1312 | ||
1313 | static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, | |
1314 | const struct sqe_submit *s, bool force_nonblock, | |
1315 | struct io_submit_state *state) | |
1316 | { | |
1317 | int ret, opcode; | |
1318 | ||
1319 | if (unlikely(s->index >= ctx->sq_entries)) | |
1320 | return -EINVAL; | |
1321 | req->user_data = READ_ONCE(s->sqe->user_data); | |
1322 | ||
1323 | opcode = READ_ONCE(s->sqe->opcode); | |
1324 | switch (opcode) { | |
1325 | case IORING_OP_NOP: | |
1326 | ret = io_nop(req, req->user_data); | |
1327 | break; | |
1328 | case IORING_OP_READV: | |
1329 | if (unlikely(s->sqe->buf_index)) | |
1330 | return -EINVAL; | |
1331 | ret = io_read(req, s, force_nonblock, state); | |
1332 | break; | |
1333 | case IORING_OP_WRITEV: | |
1334 | if (unlikely(s->sqe->buf_index)) | |
1335 | return -EINVAL; | |
1336 | ret = io_write(req, s, force_nonblock, state); | |
1337 | break; | |
1338 | case IORING_OP_READ_FIXED: | |
1339 | ret = io_read(req, s, force_nonblock, state); | |
1340 | break; | |
1341 | case IORING_OP_WRITE_FIXED: | |
1342 | ret = io_write(req, s, force_nonblock, state); | |
1343 | break; | |
1344 | case IORING_OP_FSYNC: | |
1345 | ret = io_fsync(req, s->sqe, force_nonblock); | |
1346 | break; | |
1347 | case IORING_OP_POLL_ADD: | |
1348 | ret = io_poll_add(req, s->sqe); | |
1349 | break; | |
1350 | case IORING_OP_POLL_REMOVE: | |
1351 | ret = io_poll_remove(req, s->sqe); | |
1352 | break; | |
1353 | default: | |
1354 | ret = -EINVAL; | |
1355 | break; | |
1356 | } | |
1357 | ||
1358 | if (ret) | |
1359 | return ret; | |
1360 | ||
1361 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
1362 | if (req->error == -EAGAIN) | |
1363 | return -EAGAIN; | |
1364 | ||
1365 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
1366 | if (s->needs_lock) | |
1367 | mutex_lock(&ctx->uring_lock); | |
1368 | io_iopoll_req_issued(req); | |
1369 | if (s->needs_lock) | |
1370 | mutex_unlock(&ctx->uring_lock); | |
1371 | } | |
1372 | ||
1373 | return 0; | |
1374 | } | |
1375 | ||
1376 | static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx, | |
1377 | const struct io_uring_sqe *sqe) | |
1378 | { | |
1379 | switch (sqe->opcode) { | |
1380 | case IORING_OP_READV: | |
1381 | case IORING_OP_READ_FIXED: | |
1382 | return &ctx->pending_async[READ]; | |
1383 | case IORING_OP_WRITEV: | |
1384 | case IORING_OP_WRITE_FIXED: | |
1385 | return &ctx->pending_async[WRITE]; | |
1386 | default: | |
1387 | return NULL; | |
1388 | } | |
1389 | } | |
1390 | ||
1391 | static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe) | |
1392 | { | |
1393 | u8 opcode = READ_ONCE(sqe->opcode); | |
1394 | ||
1395 | return !(opcode == IORING_OP_READ_FIXED || | |
1396 | opcode == IORING_OP_WRITE_FIXED); | |
1397 | } | |
1398 | ||
1399 | static void io_sq_wq_submit_work(struct work_struct *work) | |
1400 | { | |
1401 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
1402 | struct io_ring_ctx *ctx = req->ctx; | |
1403 | struct mm_struct *cur_mm = NULL; | |
1404 | struct async_list *async_list; | |
1405 | LIST_HEAD(req_list); | |
1406 | mm_segment_t old_fs; | |
1407 | int ret; | |
1408 | ||
1409 | async_list = io_async_list_from_sqe(ctx, req->submit.sqe); | |
1410 | restart: | |
1411 | do { | |
1412 | struct sqe_submit *s = &req->submit; | |
1413 | const struct io_uring_sqe *sqe = s->sqe; | |
1414 | ||
1415 | /* Ensure we clear previously set forced non-block flag */ | |
1416 | req->flags &= ~REQ_F_FORCE_NONBLOCK; | |
1417 | req->rw.ki_flags &= ~IOCB_NOWAIT; | |
1418 | ||
1419 | ret = 0; | |
1420 | if (io_sqe_needs_user(sqe) && !cur_mm) { | |
1421 | if (!mmget_not_zero(ctx->sqo_mm)) { | |
1422 | ret = -EFAULT; | |
1423 | } else { | |
1424 | cur_mm = ctx->sqo_mm; | |
1425 | use_mm(cur_mm); | |
1426 | old_fs = get_fs(); | |
1427 | set_fs(USER_DS); | |
1428 | } | |
1429 | } | |
1430 | ||
1431 | if (!ret) { | |
1432 | s->has_user = cur_mm != NULL; | |
1433 | s->needs_lock = true; | |
1434 | do { | |
1435 | ret = __io_submit_sqe(ctx, req, s, false, NULL); | |
1436 | /* | |
1437 | * We can get EAGAIN for polled IO even though | |
1438 | * we're forcing a sync submission from here, | |
1439 | * since we can't wait for request slots on the | |
1440 | * block side. | |
1441 | */ | |
1442 | if (ret != -EAGAIN) | |
1443 | break; | |
1444 | cond_resched(); | |
1445 | } while (1); | |
1446 | ||
1447 | /* drop submission reference */ | |
1448 | io_put_req(req); | |
1449 | } | |
1450 | if (ret) { | |
1451 | io_cqring_add_event(ctx, sqe->user_data, ret, 0); | |
1452 | io_put_req(req); | |
1453 | } | |
1454 | ||
1455 | /* async context always use a copy of the sqe */ | |
1456 | kfree(sqe); | |
1457 | ||
1458 | if (!async_list) | |
1459 | break; | |
1460 | if (!list_empty(&req_list)) { | |
1461 | req = list_first_entry(&req_list, struct io_kiocb, | |
1462 | list); | |
1463 | list_del(&req->list); | |
1464 | continue; | |
1465 | } | |
1466 | if (list_empty(&async_list->list)) | |
1467 | break; | |
1468 | ||
1469 | req = NULL; | |
1470 | spin_lock(&async_list->lock); | |
1471 | if (list_empty(&async_list->list)) { | |
1472 | spin_unlock(&async_list->lock); | |
1473 | break; | |
1474 | } | |
1475 | list_splice_init(&async_list->list, &req_list); | |
1476 | spin_unlock(&async_list->lock); | |
1477 | ||
1478 | req = list_first_entry(&req_list, struct io_kiocb, list); | |
1479 | list_del(&req->list); | |
1480 | } while (req); | |
1481 | ||
1482 | /* | |
1483 | * Rare case of racing with a submitter. If we find the count has | |
1484 | * dropped to zero AND we have pending work items, then restart | |
1485 | * the processing. This is a tiny race window. | |
1486 | */ | |
1487 | if (async_list) { | |
1488 | ret = atomic_dec_return(&async_list->cnt); | |
1489 | while (!ret && !list_empty(&async_list->list)) { | |
1490 | spin_lock(&async_list->lock); | |
1491 | atomic_inc(&async_list->cnt); | |
1492 | list_splice_init(&async_list->list, &req_list); | |
1493 | spin_unlock(&async_list->lock); | |
1494 | ||
1495 | if (!list_empty(&req_list)) { | |
1496 | req = list_first_entry(&req_list, | |
1497 | struct io_kiocb, list); | |
1498 | list_del(&req->list); | |
1499 | goto restart; | |
1500 | } | |
1501 | ret = atomic_dec_return(&async_list->cnt); | |
1502 | } | |
1503 | } | |
1504 | ||
1505 | if (cur_mm) { | |
1506 | set_fs(old_fs); | |
1507 | unuse_mm(cur_mm); | |
1508 | mmput(cur_mm); | |
1509 | } | |
1510 | } | |
1511 | ||
1512 | /* | |
1513 | * See if we can piggy back onto previously submitted work, that is still | |
1514 | * running. We currently only allow this if the new request is sequential | |
1515 | * to the previous one we punted. | |
1516 | */ | |
1517 | static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req) | |
1518 | { | |
1519 | bool ret = false; | |
1520 | ||
1521 | if (!list) | |
1522 | return false; | |
1523 | if (!(req->flags & REQ_F_SEQ_PREV)) | |
1524 | return false; | |
1525 | if (!atomic_read(&list->cnt)) | |
1526 | return false; | |
1527 | ||
1528 | ret = true; | |
1529 | spin_lock(&list->lock); | |
1530 | list_add_tail(&req->list, &list->list); | |
1531 | if (!atomic_read(&list->cnt)) { | |
1532 | list_del_init(&req->list); | |
1533 | ret = false; | |
1534 | } | |
1535 | spin_unlock(&list->lock); | |
1536 | return ret; | |
1537 | } | |
1538 | ||
1539 | static bool io_op_needs_file(const struct io_uring_sqe *sqe) | |
1540 | { | |
1541 | int op = READ_ONCE(sqe->opcode); | |
1542 | ||
1543 | switch (op) { | |
1544 | case IORING_OP_NOP: | |
1545 | case IORING_OP_POLL_REMOVE: | |
1546 | return false; | |
1547 | default: | |
1548 | return true; | |
1549 | } | |
1550 | } | |
1551 | ||
1552 | static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s, | |
1553 | struct io_submit_state *state, struct io_kiocb *req) | |
1554 | { | |
1555 | unsigned flags; | |
1556 | int fd; | |
1557 | ||
1558 | flags = READ_ONCE(s->sqe->flags); | |
1559 | fd = READ_ONCE(s->sqe->fd); | |
1560 | ||
1561 | if (!io_op_needs_file(s->sqe)) { | |
1562 | req->file = NULL; | |
1563 | return 0; | |
1564 | } | |
1565 | ||
1566 | if (flags & IOSQE_FIXED_FILE) { | |
1567 | if (unlikely(!ctx->user_files || | |
1568 | (unsigned) fd >= ctx->nr_user_files)) | |
1569 | return -EBADF; | |
1570 | req->file = ctx->user_files[fd]; | |
1571 | req->flags |= REQ_F_FIXED_FILE; | |
1572 | } else { | |
1573 | if (s->needs_fixed_file) | |
1574 | return -EBADF; | |
1575 | req->file = io_file_get(state, fd); | |
1576 | if (unlikely(!req->file)) | |
1577 | return -EBADF; | |
1578 | } | |
1579 | ||
1580 | return 0; | |
1581 | } | |
1582 | ||
1583 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s, | |
1584 | struct io_submit_state *state) | |
1585 | { | |
1586 | struct io_kiocb *req; | |
1587 | int ret; | |
1588 | ||
1589 | /* enforce forwards compatibility on users */ | |
1590 | if (unlikely(s->sqe->flags & ~IOSQE_FIXED_FILE)) | |
1591 | return -EINVAL; | |
1592 | ||
1593 | req = io_get_req(ctx, state); | |
1594 | if (unlikely(!req)) | |
1595 | return -EAGAIN; | |
1596 | ||
1597 | ret = io_req_set_file(ctx, s, state, req); | |
1598 | if (unlikely(ret)) | |
1599 | goto out; | |
1600 | ||
1601 | ret = __io_submit_sqe(ctx, req, s, true, state); | |
1602 | if (ret == -EAGAIN) { | |
1603 | struct io_uring_sqe *sqe_copy; | |
1604 | ||
1605 | sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL); | |
1606 | if (sqe_copy) { | |
1607 | struct async_list *list; | |
1608 | ||
1609 | memcpy(sqe_copy, s->sqe, sizeof(*sqe_copy)); | |
1610 | s->sqe = sqe_copy; | |
1611 | ||
1612 | memcpy(&req->submit, s, sizeof(*s)); | |
1613 | list = io_async_list_from_sqe(ctx, s->sqe); | |
1614 | if (!io_add_to_prev_work(list, req)) { | |
1615 | if (list) | |
1616 | atomic_inc(&list->cnt); | |
1617 | INIT_WORK(&req->work, io_sq_wq_submit_work); | |
1618 | queue_work(ctx->sqo_wq, &req->work); | |
1619 | } | |
1620 | ||
1621 | /* | |
1622 | * Queued up for async execution, worker will release | |
1623 | * submit reference when the iocb is actually | |
1624 | * submitted. | |
1625 | */ | |
1626 | return 0; | |
1627 | } | |
1628 | } | |
1629 | ||
1630 | out: | |
1631 | /* drop submission reference */ | |
1632 | io_put_req(req); | |
1633 | ||
1634 | /* and drop final reference, if we failed */ | |
1635 | if (ret) | |
1636 | io_put_req(req); | |
1637 | ||
1638 | return ret; | |
1639 | } | |
1640 | ||
1641 | /* | |
1642 | * Batched submission is done, ensure local IO is flushed out. | |
1643 | */ | |
1644 | static void io_submit_state_end(struct io_submit_state *state) | |
1645 | { | |
1646 | blk_finish_plug(&state->plug); | |
1647 | io_file_put(state, NULL); | |
1648 | if (state->free_reqs) | |
1649 | kmem_cache_free_bulk(req_cachep, state->free_reqs, | |
1650 | &state->reqs[state->cur_req]); | |
1651 | } | |
1652 | ||
1653 | /* | |
1654 | * Start submission side cache. | |
1655 | */ | |
1656 | static void io_submit_state_start(struct io_submit_state *state, | |
1657 | struct io_ring_ctx *ctx, unsigned max_ios) | |
1658 | { | |
1659 | blk_start_plug(&state->plug); | |
1660 | state->free_reqs = 0; | |
1661 | state->file = NULL; | |
1662 | state->ios_left = max_ios; | |
1663 | } | |
1664 | ||
1665 | static void io_commit_sqring(struct io_ring_ctx *ctx) | |
1666 | { | |
1667 | struct io_sq_ring *ring = ctx->sq_ring; | |
1668 | ||
1669 | if (ctx->cached_sq_head != READ_ONCE(ring->r.head)) { | |
1670 | /* | |
1671 | * Ensure any loads from the SQEs are done at this point, | |
1672 | * since once we write the new head, the application could | |
1673 | * write new data to them. | |
1674 | */ | |
1675 | smp_store_release(&ring->r.head, ctx->cached_sq_head); | |
1676 | ||
1677 | /* | |
1678 | * write side barrier of head update, app has read side. See | |
1679 | * comment at the top of this file | |
1680 | */ | |
1681 | smp_wmb(); | |
1682 | } | |
1683 | } | |
1684 | ||
1685 | /* | |
1686 | * Undo last io_get_sqring() | |
1687 | */ | |
1688 | static void io_drop_sqring(struct io_ring_ctx *ctx) | |
1689 | { | |
1690 | ctx->cached_sq_head--; | |
1691 | } | |
1692 | ||
1693 | /* | |
1694 | * Fetch an sqe, if one is available. Note that s->sqe will point to memory | |
1695 | * that is mapped by userspace. This means that care needs to be taken to | |
1696 | * ensure that reads are stable, as we cannot rely on userspace always | |
1697 | * being a good citizen. If members of the sqe are validated and then later | |
1698 | * used, it's important that those reads are done through READ_ONCE() to | |
1699 | * prevent a re-load down the line. | |
1700 | */ | |
1701 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s) | |
1702 | { | |
1703 | struct io_sq_ring *ring = ctx->sq_ring; | |
1704 | unsigned head; | |
1705 | ||
1706 | /* | |
1707 | * The cached sq head (or cq tail) serves two purposes: | |
1708 | * | |
1709 | * 1) allows us to batch the cost of updating the user visible | |
1710 | * head updates. | |
1711 | * 2) allows the kernel side to track the head on its own, even | |
1712 | * though the application is the one updating it. | |
1713 | */ | |
1714 | head = ctx->cached_sq_head; | |
1715 | /* See comment at the top of this file */ | |
1716 | smp_rmb(); | |
1717 | if (head == READ_ONCE(ring->r.tail)) | |
1718 | return false; | |
1719 | ||
1720 | head = READ_ONCE(ring->array[head & ctx->sq_mask]); | |
1721 | if (head < ctx->sq_entries) { | |
1722 | s->index = head; | |
1723 | s->sqe = &ctx->sq_sqes[head]; | |
1724 | ctx->cached_sq_head++; | |
1725 | return true; | |
1726 | } | |
1727 | ||
1728 | /* drop invalid entries */ | |
1729 | ctx->cached_sq_head++; | |
1730 | ring->dropped++; | |
1731 | /* See comment at the top of this file */ | |
1732 | smp_wmb(); | |
1733 | return false; | |
1734 | } | |
1735 | ||
1736 | static int io_submit_sqes(struct io_ring_ctx *ctx, struct sqe_submit *sqes, | |
1737 | unsigned int nr, bool has_user, bool mm_fault) | |
1738 | { | |
1739 | struct io_submit_state state, *statep = NULL; | |
1740 | int ret, i, submitted = 0; | |
1741 | ||
1742 | if (nr > IO_PLUG_THRESHOLD) { | |
1743 | io_submit_state_start(&state, ctx, nr); | |
1744 | statep = &state; | |
1745 | } | |
1746 | ||
1747 | for (i = 0; i < nr; i++) { | |
1748 | if (unlikely(mm_fault)) { | |
1749 | ret = -EFAULT; | |
1750 | } else { | |
1751 | sqes[i].has_user = has_user; | |
1752 | sqes[i].needs_lock = true; | |
1753 | sqes[i].needs_fixed_file = true; | |
1754 | ret = io_submit_sqe(ctx, &sqes[i], statep); | |
1755 | } | |
1756 | if (!ret) { | |
1757 | submitted++; | |
1758 | continue; | |
1759 | } | |
1760 | ||
1761 | io_cqring_add_event(ctx, sqes[i].sqe->user_data, ret, 0); | |
1762 | } | |
1763 | ||
1764 | if (statep) | |
1765 | io_submit_state_end(&state); | |
1766 | ||
1767 | return submitted; | |
1768 | } | |
1769 | ||
1770 | static int io_sq_thread(void *data) | |
1771 | { | |
1772 | struct sqe_submit sqes[IO_IOPOLL_BATCH]; | |
1773 | struct io_ring_ctx *ctx = data; | |
1774 | struct mm_struct *cur_mm = NULL; | |
1775 | mm_segment_t old_fs; | |
1776 | DEFINE_WAIT(wait); | |
1777 | unsigned inflight; | |
1778 | unsigned long timeout; | |
1779 | ||
1780 | old_fs = get_fs(); | |
1781 | set_fs(USER_DS); | |
1782 | ||
1783 | timeout = inflight = 0; | |
1784 | while (!kthread_should_stop() && !ctx->sqo_stop) { | |
1785 | bool all_fixed, mm_fault = false; | |
1786 | int i; | |
1787 | ||
1788 | if (inflight) { | |
1789 | unsigned nr_events = 0; | |
1790 | ||
1791 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
1792 | /* | |
1793 | * We disallow the app entering submit/complete | |
1794 | * with polling, but we still need to lock the | |
1795 | * ring to prevent racing with polled issue | |
1796 | * that got punted to a workqueue. | |
1797 | */ | |
1798 | mutex_lock(&ctx->uring_lock); | |
1799 | io_iopoll_check(ctx, &nr_events, 0); | |
1800 | mutex_unlock(&ctx->uring_lock); | |
1801 | } else { | |
1802 | /* | |
1803 | * Normal IO, just pretend everything completed. | |
1804 | * We don't have to poll completions for that. | |
1805 | */ | |
1806 | nr_events = inflight; | |
1807 | } | |
1808 | ||
1809 | inflight -= nr_events; | |
1810 | if (!inflight) | |
1811 | timeout = jiffies + ctx->sq_thread_idle; | |
1812 | } | |
1813 | ||
1814 | if (!io_get_sqring(ctx, &sqes[0])) { | |
1815 | /* | |
1816 | * We're polling. If we're within the defined idle | |
1817 | * period, then let us spin without work before going | |
1818 | * to sleep. | |
1819 | */ | |
1820 | if (inflight || !time_after(jiffies, timeout)) { | |
1821 | cpu_relax(); | |
1822 | continue; | |
1823 | } | |
1824 | ||
1825 | /* | |
1826 | * Drop cur_mm before scheduling, we can't hold it for | |
1827 | * long periods (or over schedule()). Do this before | |
1828 | * adding ourselves to the waitqueue, as the unuse/drop | |
1829 | * may sleep. | |
1830 | */ | |
1831 | if (cur_mm) { | |
1832 | unuse_mm(cur_mm); | |
1833 | mmput(cur_mm); | |
1834 | cur_mm = NULL; | |
1835 | } | |
1836 | ||
1837 | prepare_to_wait(&ctx->sqo_wait, &wait, | |
1838 | TASK_INTERRUPTIBLE); | |
1839 | ||
1840 | /* Tell userspace we may need a wakeup call */ | |
1841 | ctx->sq_ring->flags |= IORING_SQ_NEED_WAKEUP; | |
1842 | smp_wmb(); | |
1843 | ||
1844 | if (!io_get_sqring(ctx, &sqes[0])) { | |
1845 | if (kthread_should_stop()) { | |
1846 | finish_wait(&ctx->sqo_wait, &wait); | |
1847 | break; | |
1848 | } | |
1849 | if (signal_pending(current)) | |
1850 | flush_signals(current); | |
1851 | schedule(); | |
1852 | finish_wait(&ctx->sqo_wait, &wait); | |
1853 | ||
1854 | ctx->sq_ring->flags &= ~IORING_SQ_NEED_WAKEUP; | |
1855 | smp_wmb(); | |
1856 | continue; | |
1857 | } | |
1858 | finish_wait(&ctx->sqo_wait, &wait); | |
1859 | ||
1860 | ctx->sq_ring->flags &= ~IORING_SQ_NEED_WAKEUP; | |
1861 | smp_wmb(); | |
1862 | } | |
1863 | ||
1864 | i = 0; | |
1865 | all_fixed = true; | |
1866 | do { | |
1867 | if (all_fixed && io_sqe_needs_user(sqes[i].sqe)) | |
1868 | all_fixed = false; | |
1869 | ||
1870 | i++; | |
1871 | if (i == ARRAY_SIZE(sqes)) | |
1872 | break; | |
1873 | } while (io_get_sqring(ctx, &sqes[i])); | |
1874 | ||
1875 | /* Unless all new commands are FIXED regions, grab mm */ | |
1876 | if (!all_fixed && !cur_mm) { | |
1877 | mm_fault = !mmget_not_zero(ctx->sqo_mm); | |
1878 | if (!mm_fault) { | |
1879 | use_mm(ctx->sqo_mm); | |
1880 | cur_mm = ctx->sqo_mm; | |
1881 | } | |
1882 | } | |
1883 | ||
1884 | inflight += io_submit_sqes(ctx, sqes, i, cur_mm != NULL, | |
1885 | mm_fault); | |
1886 | ||
1887 | /* Commit SQ ring head once we've consumed all SQEs */ | |
1888 | io_commit_sqring(ctx); | |
1889 | } | |
1890 | ||
1891 | set_fs(old_fs); | |
1892 | if (cur_mm) { | |
1893 | unuse_mm(cur_mm); | |
1894 | mmput(cur_mm); | |
1895 | } | |
1896 | return 0; | |
1897 | } | |
1898 | ||
1899 | static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit) | |
1900 | { | |
1901 | struct io_submit_state state, *statep = NULL; | |
1902 | int i, ret = 0, submit = 0; | |
1903 | ||
1904 | if (to_submit > IO_PLUG_THRESHOLD) { | |
1905 | io_submit_state_start(&state, ctx, to_submit); | |
1906 | statep = &state; | |
1907 | } | |
1908 | ||
1909 | for (i = 0; i < to_submit; i++) { | |
1910 | struct sqe_submit s; | |
1911 | ||
1912 | if (!io_get_sqring(ctx, &s)) | |
1913 | break; | |
1914 | ||
1915 | s.has_user = true; | |
1916 | s.needs_lock = false; | |
1917 | s.needs_fixed_file = false; | |
1918 | ||
1919 | ret = io_submit_sqe(ctx, &s, statep); | |
1920 | if (ret) { | |
1921 | io_drop_sqring(ctx); | |
1922 | break; | |
1923 | } | |
1924 | ||
1925 | submit++; | |
1926 | } | |
1927 | io_commit_sqring(ctx); | |
1928 | ||
1929 | if (statep) | |
1930 | io_submit_state_end(statep); | |
1931 | ||
1932 | return submit ? submit : ret; | |
1933 | } | |
1934 | ||
1935 | static unsigned io_cqring_events(struct io_cq_ring *ring) | |
1936 | { | |
1937 | return READ_ONCE(ring->r.tail) - READ_ONCE(ring->r.head); | |
1938 | } | |
1939 | ||
1940 | /* | |
1941 | * Wait until events become available, if we don't already have some. The | |
1942 | * application must reap them itself, as they reside on the shared cq ring. | |
1943 | */ | |
1944 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
1945 | const sigset_t __user *sig, size_t sigsz) | |
1946 | { | |
1947 | struct io_cq_ring *ring = ctx->cq_ring; | |
1948 | sigset_t ksigmask, sigsaved; | |
1949 | DEFINE_WAIT(wait); | |
1950 | int ret; | |
1951 | ||
1952 | /* See comment at the top of this file */ | |
1953 | smp_rmb(); | |
1954 | if (io_cqring_events(ring) >= min_events) | |
1955 | return 0; | |
1956 | ||
1957 | if (sig) { | |
1958 | ret = set_user_sigmask(sig, &ksigmask, &sigsaved, sigsz); | |
1959 | if (ret) | |
1960 | return ret; | |
1961 | } | |
1962 | ||
1963 | do { | |
1964 | prepare_to_wait(&ctx->wait, &wait, TASK_INTERRUPTIBLE); | |
1965 | ||
1966 | ret = 0; | |
1967 | /* See comment at the top of this file */ | |
1968 | smp_rmb(); | |
1969 | if (io_cqring_events(ring) >= min_events) | |
1970 | break; | |
1971 | ||
1972 | schedule(); | |
1973 | ||
1974 | ret = -EINTR; | |
1975 | if (signal_pending(current)) | |
1976 | break; | |
1977 | } while (1); | |
1978 | ||
1979 | finish_wait(&ctx->wait, &wait); | |
1980 | ||
1981 | if (sig) | |
1982 | restore_user_sigmask(sig, &sigsaved); | |
1983 | ||
1984 | return READ_ONCE(ring->r.head) == READ_ONCE(ring->r.tail) ? ret : 0; | |
1985 | } | |
1986 | ||
1987 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) | |
1988 | { | |
1989 | #if defined(CONFIG_UNIX) | |
1990 | if (ctx->ring_sock) { | |
1991 | struct sock *sock = ctx->ring_sock->sk; | |
1992 | struct sk_buff *skb; | |
1993 | ||
1994 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
1995 | kfree_skb(skb); | |
1996 | } | |
1997 | #else | |
1998 | int i; | |
1999 | ||
2000 | for (i = 0; i < ctx->nr_user_files; i++) | |
2001 | fput(ctx->user_files[i]); | |
2002 | #endif | |
2003 | } | |
2004 | ||
2005 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) | |
2006 | { | |
2007 | if (!ctx->user_files) | |
2008 | return -ENXIO; | |
2009 | ||
2010 | __io_sqe_files_unregister(ctx); | |
2011 | kfree(ctx->user_files); | |
2012 | ctx->user_files = NULL; | |
2013 | ctx->nr_user_files = 0; | |
2014 | return 0; | |
2015 | } | |
2016 | ||
2017 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) | |
2018 | { | |
2019 | if (ctx->sqo_thread) { | |
2020 | ctx->sqo_stop = 1; | |
2021 | mb(); | |
2022 | kthread_stop(ctx->sqo_thread); | |
2023 | ctx->sqo_thread = NULL; | |
2024 | } | |
2025 | } | |
2026 | ||
2027 | static void io_finish_async(struct io_ring_ctx *ctx) | |
2028 | { | |
2029 | io_sq_thread_stop(ctx); | |
2030 | ||
2031 | if (ctx->sqo_wq) { | |
2032 | destroy_workqueue(ctx->sqo_wq); | |
2033 | ctx->sqo_wq = NULL; | |
2034 | } | |
2035 | } | |
2036 | ||
2037 | #if defined(CONFIG_UNIX) | |
2038 | static void io_destruct_skb(struct sk_buff *skb) | |
2039 | { | |
2040 | struct io_ring_ctx *ctx = skb->sk->sk_user_data; | |
2041 | ||
2042 | io_finish_async(ctx); | |
2043 | unix_destruct_scm(skb); | |
2044 | } | |
2045 | ||
2046 | /* | |
2047 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
2048 | * the io_uring can be safely unregistered on process exit, even if we have | |
2049 | * loops in the file referencing. | |
2050 | */ | |
2051 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
2052 | { | |
2053 | struct sock *sk = ctx->ring_sock->sk; | |
2054 | struct scm_fp_list *fpl; | |
2055 | struct sk_buff *skb; | |
2056 | int i; | |
2057 | ||
2058 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { | |
2059 | unsigned long inflight = ctx->user->unix_inflight + nr; | |
2060 | ||
2061 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) | |
2062 | return -EMFILE; | |
2063 | } | |
2064 | ||
2065 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); | |
2066 | if (!fpl) | |
2067 | return -ENOMEM; | |
2068 | ||
2069 | skb = alloc_skb(0, GFP_KERNEL); | |
2070 | if (!skb) { | |
2071 | kfree(fpl); | |
2072 | return -ENOMEM; | |
2073 | } | |
2074 | ||
2075 | skb->sk = sk; | |
2076 | skb->destructor = io_destruct_skb; | |
2077 | ||
2078 | fpl->user = get_uid(ctx->user); | |
2079 | for (i = 0; i < nr; i++) { | |
2080 | fpl->fp[i] = get_file(ctx->user_files[i + offset]); | |
2081 | unix_inflight(fpl->user, fpl->fp[i]); | |
2082 | } | |
2083 | ||
2084 | fpl->max = fpl->count = nr; | |
2085 | UNIXCB(skb).fp = fpl; | |
2086 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); | |
2087 | skb_queue_head(&sk->sk_receive_queue, skb); | |
2088 | ||
2089 | for (i = 0; i < nr; i++) | |
2090 | fput(fpl->fp[i]); | |
2091 | ||
2092 | return 0; | |
2093 | } | |
2094 | ||
2095 | /* | |
2096 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
2097 | * causes regular reference counting to break down. We rely on the UNIX | |
2098 | * garbage collection to take care of this problem for us. | |
2099 | */ | |
2100 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
2101 | { | |
2102 | unsigned left, total; | |
2103 | int ret = 0; | |
2104 | ||
2105 | total = 0; | |
2106 | left = ctx->nr_user_files; | |
2107 | while (left) { | |
2108 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
2109 | int ret; | |
2110 | ||
2111 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
2112 | if (ret) | |
2113 | break; | |
2114 | left -= this_files; | |
2115 | total += this_files; | |
2116 | } | |
2117 | ||
2118 | if (!ret) | |
2119 | return 0; | |
2120 | ||
2121 | while (total < ctx->nr_user_files) { | |
2122 | fput(ctx->user_files[total]); | |
2123 | total++; | |
2124 | } | |
2125 | ||
2126 | return ret; | |
2127 | } | |
2128 | #else | |
2129 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
2130 | { | |
2131 | return 0; | |
2132 | } | |
2133 | #endif | |
2134 | ||
2135 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, | |
2136 | unsigned nr_args) | |
2137 | { | |
2138 | __s32 __user *fds = (__s32 __user *) arg; | |
2139 | int fd, ret = 0; | |
2140 | unsigned i; | |
2141 | ||
2142 | if (ctx->user_files) | |
2143 | return -EBUSY; | |
2144 | if (!nr_args) | |
2145 | return -EINVAL; | |
2146 | if (nr_args > IORING_MAX_FIXED_FILES) | |
2147 | return -EMFILE; | |
2148 | ||
2149 | ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL); | |
2150 | if (!ctx->user_files) | |
2151 | return -ENOMEM; | |
2152 | ||
2153 | for (i = 0; i < nr_args; i++) { | |
2154 | ret = -EFAULT; | |
2155 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) | |
2156 | break; | |
2157 | ||
2158 | ctx->user_files[i] = fget(fd); | |
2159 | ||
2160 | ret = -EBADF; | |
2161 | if (!ctx->user_files[i]) | |
2162 | break; | |
2163 | /* | |
2164 | * Don't allow io_uring instances to be registered. If UNIX | |
2165 | * isn't enabled, then this causes a reference cycle and this | |
2166 | * instance can never get freed. If UNIX is enabled we'll | |
2167 | * handle it just fine, but there's still no point in allowing | |
2168 | * a ring fd as it doesn't support regular read/write anyway. | |
2169 | */ | |
2170 | if (ctx->user_files[i]->f_op == &io_uring_fops) { | |
2171 | fput(ctx->user_files[i]); | |
2172 | break; | |
2173 | } | |
2174 | ctx->nr_user_files++; | |
2175 | ret = 0; | |
2176 | } | |
2177 | ||
2178 | if (ret) { | |
2179 | for (i = 0; i < ctx->nr_user_files; i++) | |
2180 | fput(ctx->user_files[i]); | |
2181 | ||
2182 | kfree(ctx->user_files); | |
2183 | ctx->nr_user_files = 0; | |
2184 | return ret; | |
2185 | } | |
2186 | ||
2187 | ret = io_sqe_files_scm(ctx); | |
2188 | if (ret) | |
2189 | io_sqe_files_unregister(ctx); | |
2190 | ||
2191 | return ret; | |
2192 | } | |
2193 | ||
2194 | static int io_sq_offload_start(struct io_ring_ctx *ctx, | |
2195 | struct io_uring_params *p) | |
2196 | { | |
2197 | int ret; | |
2198 | ||
2199 | init_waitqueue_head(&ctx->sqo_wait); | |
2200 | mmgrab(current->mm); | |
2201 | ctx->sqo_mm = current->mm; | |
2202 | ||
2203 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); | |
2204 | if (!ctx->sq_thread_idle) | |
2205 | ctx->sq_thread_idle = HZ; | |
2206 | ||
2207 | ret = -EINVAL; | |
2208 | if (!cpu_possible(p->sq_thread_cpu)) | |
2209 | goto err; | |
2210 | ||
2211 | if (ctx->flags & IORING_SETUP_SQPOLL) { | |
2212 | if (p->flags & IORING_SETUP_SQ_AFF) { | |
2213 | int cpu; | |
2214 | ||
2215 | cpu = array_index_nospec(p->sq_thread_cpu, NR_CPUS); | |
2216 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, | |
2217 | ctx, cpu, | |
2218 | "io_uring-sq"); | |
2219 | } else { | |
2220 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, | |
2221 | "io_uring-sq"); | |
2222 | } | |
2223 | if (IS_ERR(ctx->sqo_thread)) { | |
2224 | ret = PTR_ERR(ctx->sqo_thread); | |
2225 | ctx->sqo_thread = NULL; | |
2226 | goto err; | |
2227 | } | |
2228 | wake_up_process(ctx->sqo_thread); | |
2229 | } else if (p->flags & IORING_SETUP_SQ_AFF) { | |
2230 | /* Can't have SQ_AFF without SQPOLL */ | |
2231 | ret = -EINVAL; | |
2232 | goto err; | |
2233 | } | |
2234 | ||
2235 | /* Do QD, or 2 * CPUS, whatever is smallest */ | |
2236 | ctx->sqo_wq = alloc_workqueue("io_ring-wq", WQ_UNBOUND | WQ_FREEZABLE, | |
2237 | min(ctx->sq_entries - 1, 2 * num_online_cpus())); | |
2238 | if (!ctx->sqo_wq) { | |
2239 | ret = -ENOMEM; | |
2240 | goto err; | |
2241 | } | |
2242 | ||
2243 | return 0; | |
2244 | err: | |
2245 | io_sq_thread_stop(ctx); | |
2246 | mmdrop(ctx->sqo_mm); | |
2247 | ctx->sqo_mm = NULL; | |
2248 | return ret; | |
2249 | } | |
2250 | ||
2251 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) | |
2252 | { | |
2253 | atomic_long_sub(nr_pages, &user->locked_vm); | |
2254 | } | |
2255 | ||
2256 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) | |
2257 | { | |
2258 | unsigned long page_limit, cur_pages, new_pages; | |
2259 | ||
2260 | /* Don't allow more pages than we can safely lock */ | |
2261 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
2262 | ||
2263 | do { | |
2264 | cur_pages = atomic_long_read(&user->locked_vm); | |
2265 | new_pages = cur_pages + nr_pages; | |
2266 | if (new_pages > page_limit) | |
2267 | return -ENOMEM; | |
2268 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
2269 | new_pages) != cur_pages); | |
2270 | ||
2271 | return 0; | |
2272 | } | |
2273 | ||
2274 | static void io_mem_free(void *ptr) | |
2275 | { | |
2276 | struct page *page = virt_to_head_page(ptr); | |
2277 | ||
2278 | if (put_page_testzero(page)) | |
2279 | free_compound_page(page); | |
2280 | } | |
2281 | ||
2282 | static void *io_mem_alloc(size_t size) | |
2283 | { | |
2284 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
2285 | __GFP_NORETRY; | |
2286 | ||
2287 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
2288 | } | |
2289 | ||
2290 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) | |
2291 | { | |
2292 | struct io_sq_ring *sq_ring; | |
2293 | struct io_cq_ring *cq_ring; | |
2294 | size_t bytes; | |
2295 | ||
2296 | bytes = struct_size(sq_ring, array, sq_entries); | |
2297 | bytes += array_size(sizeof(struct io_uring_sqe), sq_entries); | |
2298 | bytes += struct_size(cq_ring, cqes, cq_entries); | |
2299 | ||
2300 | return (bytes + PAGE_SIZE - 1) / PAGE_SIZE; | |
2301 | } | |
2302 | ||
2303 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) | |
2304 | { | |
2305 | int i, j; | |
2306 | ||
2307 | if (!ctx->user_bufs) | |
2308 | return -ENXIO; | |
2309 | ||
2310 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
2311 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
2312 | ||
2313 | for (j = 0; j < imu->nr_bvecs; j++) | |
2314 | put_page(imu->bvec[j].bv_page); | |
2315 | ||
2316 | if (ctx->account_mem) | |
2317 | io_unaccount_mem(ctx->user, imu->nr_bvecs); | |
2318 | kfree(imu->bvec); | |
2319 | imu->nr_bvecs = 0; | |
2320 | } | |
2321 | ||
2322 | kfree(ctx->user_bufs); | |
2323 | ctx->user_bufs = NULL; | |
2324 | ctx->nr_user_bufs = 0; | |
2325 | return 0; | |
2326 | } | |
2327 | ||
2328 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
2329 | void __user *arg, unsigned index) | |
2330 | { | |
2331 | struct iovec __user *src; | |
2332 | ||
2333 | #ifdef CONFIG_COMPAT | |
2334 | if (ctx->compat) { | |
2335 | struct compat_iovec __user *ciovs; | |
2336 | struct compat_iovec ciov; | |
2337 | ||
2338 | ciovs = (struct compat_iovec __user *) arg; | |
2339 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
2340 | return -EFAULT; | |
2341 | ||
2342 | dst->iov_base = (void __user *) (unsigned long) ciov.iov_base; | |
2343 | dst->iov_len = ciov.iov_len; | |
2344 | return 0; | |
2345 | } | |
2346 | #endif | |
2347 | src = (struct iovec __user *) arg; | |
2348 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
2349 | return -EFAULT; | |
2350 | return 0; | |
2351 | } | |
2352 | ||
2353 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, | |
2354 | unsigned nr_args) | |
2355 | { | |
2356 | struct vm_area_struct **vmas = NULL; | |
2357 | struct page **pages = NULL; | |
2358 | int i, j, got_pages = 0; | |
2359 | int ret = -EINVAL; | |
2360 | ||
2361 | if (ctx->user_bufs) | |
2362 | return -EBUSY; | |
2363 | if (!nr_args || nr_args > UIO_MAXIOV) | |
2364 | return -EINVAL; | |
2365 | ||
2366 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
2367 | GFP_KERNEL); | |
2368 | if (!ctx->user_bufs) | |
2369 | return -ENOMEM; | |
2370 | ||
2371 | for (i = 0; i < nr_args; i++) { | |
2372 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
2373 | unsigned long off, start, end, ubuf; | |
2374 | int pret, nr_pages; | |
2375 | struct iovec iov; | |
2376 | size_t size; | |
2377 | ||
2378 | ret = io_copy_iov(ctx, &iov, arg, i); | |
2379 | if (ret) | |
2380 | break; | |
2381 | ||
2382 | /* | |
2383 | * Don't impose further limits on the size and buffer | |
2384 | * constraints here, we'll -EINVAL later when IO is | |
2385 | * submitted if they are wrong. | |
2386 | */ | |
2387 | ret = -EFAULT; | |
2388 | if (!iov.iov_base || !iov.iov_len) | |
2389 | goto err; | |
2390 | ||
2391 | /* arbitrary limit, but we need something */ | |
2392 | if (iov.iov_len > SZ_1G) | |
2393 | goto err; | |
2394 | ||
2395 | ubuf = (unsigned long) iov.iov_base; | |
2396 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
2397 | start = ubuf >> PAGE_SHIFT; | |
2398 | nr_pages = end - start; | |
2399 | ||
2400 | if (ctx->account_mem) { | |
2401 | ret = io_account_mem(ctx->user, nr_pages); | |
2402 | if (ret) | |
2403 | goto err; | |
2404 | } | |
2405 | ||
2406 | ret = 0; | |
2407 | if (!pages || nr_pages > got_pages) { | |
2408 | kfree(vmas); | |
2409 | kfree(pages); | |
2410 | pages = kmalloc_array(nr_pages, sizeof(struct page *), | |
2411 | GFP_KERNEL); | |
2412 | vmas = kmalloc_array(nr_pages, | |
2413 | sizeof(struct vm_area_struct *), | |
2414 | GFP_KERNEL); | |
2415 | if (!pages || !vmas) { | |
2416 | ret = -ENOMEM; | |
2417 | if (ctx->account_mem) | |
2418 | io_unaccount_mem(ctx->user, nr_pages); | |
2419 | goto err; | |
2420 | } | |
2421 | got_pages = nr_pages; | |
2422 | } | |
2423 | ||
2424 | imu->bvec = kmalloc_array(nr_pages, sizeof(struct bio_vec), | |
2425 | GFP_KERNEL); | |
2426 | ret = -ENOMEM; | |
2427 | if (!imu->bvec) { | |
2428 | if (ctx->account_mem) | |
2429 | io_unaccount_mem(ctx->user, nr_pages); | |
2430 | goto err; | |
2431 | } | |
2432 | ||
2433 | ret = 0; | |
2434 | down_read(¤t->mm->mmap_sem); | |
2435 | pret = get_user_pages_longterm(ubuf, nr_pages, FOLL_WRITE, | |
2436 | pages, vmas); | |
2437 | if (pret == nr_pages) { | |
2438 | /* don't support file backed memory */ | |
2439 | for (j = 0; j < nr_pages; j++) { | |
2440 | struct vm_area_struct *vma = vmas[j]; | |
2441 | ||
2442 | if (vma->vm_file && | |
2443 | !is_file_hugepages(vma->vm_file)) { | |
2444 | ret = -EOPNOTSUPP; | |
2445 | break; | |
2446 | } | |
2447 | } | |
2448 | } else { | |
2449 | ret = pret < 0 ? pret : -EFAULT; | |
2450 | } | |
2451 | up_read(¤t->mm->mmap_sem); | |
2452 | if (ret) { | |
2453 | /* | |
2454 | * if we did partial map, or found file backed vmas, | |
2455 | * release any pages we did get | |
2456 | */ | |
2457 | if (pret > 0) { | |
2458 | for (j = 0; j < pret; j++) | |
2459 | put_page(pages[j]); | |
2460 | } | |
2461 | if (ctx->account_mem) | |
2462 | io_unaccount_mem(ctx->user, nr_pages); | |
2463 | goto err; | |
2464 | } | |
2465 | ||
2466 | off = ubuf & ~PAGE_MASK; | |
2467 | size = iov.iov_len; | |
2468 | for (j = 0; j < nr_pages; j++) { | |
2469 | size_t vec_len; | |
2470 | ||
2471 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
2472 | imu->bvec[j].bv_page = pages[j]; | |
2473 | imu->bvec[j].bv_len = vec_len; | |
2474 | imu->bvec[j].bv_offset = off; | |
2475 | off = 0; | |
2476 | size -= vec_len; | |
2477 | } | |
2478 | /* store original address for later verification */ | |
2479 | imu->ubuf = ubuf; | |
2480 | imu->len = iov.iov_len; | |
2481 | imu->nr_bvecs = nr_pages; | |
2482 | ||
2483 | ctx->nr_user_bufs++; | |
2484 | } | |
2485 | kfree(pages); | |
2486 | kfree(vmas); | |
2487 | return 0; | |
2488 | err: | |
2489 | kfree(pages); | |
2490 | kfree(vmas); | |
2491 | io_sqe_buffer_unregister(ctx); | |
2492 | return ret; | |
2493 | } | |
2494 | ||
2495 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) | |
2496 | { | |
2497 | io_finish_async(ctx); | |
2498 | if (ctx->sqo_mm) | |
2499 | mmdrop(ctx->sqo_mm); | |
2500 | ||
2501 | io_iopoll_reap_events(ctx); | |
2502 | io_sqe_buffer_unregister(ctx); | |
2503 | io_sqe_files_unregister(ctx); | |
2504 | ||
2505 | #if defined(CONFIG_UNIX) | |
2506 | if (ctx->ring_sock) | |
2507 | sock_release(ctx->ring_sock); | |
2508 | #endif | |
2509 | ||
2510 | io_mem_free(ctx->sq_ring); | |
2511 | io_mem_free(ctx->sq_sqes); | |
2512 | io_mem_free(ctx->cq_ring); | |
2513 | ||
2514 | percpu_ref_exit(&ctx->refs); | |
2515 | if (ctx->account_mem) | |
2516 | io_unaccount_mem(ctx->user, | |
2517 | ring_pages(ctx->sq_entries, ctx->cq_entries)); | |
2518 | free_uid(ctx->user); | |
2519 | kfree(ctx); | |
2520 | } | |
2521 | ||
2522 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
2523 | { | |
2524 | struct io_ring_ctx *ctx = file->private_data; | |
2525 | __poll_t mask = 0; | |
2526 | ||
2527 | poll_wait(file, &ctx->cq_wait, wait); | |
2528 | /* See comment at the top of this file */ | |
2529 | smp_rmb(); | |
2530 | if (READ_ONCE(ctx->sq_ring->r.tail) + 1 != ctx->cached_sq_head) | |
2531 | mask |= EPOLLOUT | EPOLLWRNORM; | |
2532 | if (READ_ONCE(ctx->cq_ring->r.head) != ctx->cached_cq_tail) | |
2533 | mask |= EPOLLIN | EPOLLRDNORM; | |
2534 | ||
2535 | return mask; | |
2536 | } | |
2537 | ||
2538 | static int io_uring_fasync(int fd, struct file *file, int on) | |
2539 | { | |
2540 | struct io_ring_ctx *ctx = file->private_data; | |
2541 | ||
2542 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
2543 | } | |
2544 | ||
2545 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) | |
2546 | { | |
2547 | mutex_lock(&ctx->uring_lock); | |
2548 | percpu_ref_kill(&ctx->refs); | |
2549 | mutex_unlock(&ctx->uring_lock); | |
2550 | ||
2551 | io_poll_remove_all(ctx); | |
2552 | io_iopoll_reap_events(ctx); | |
2553 | wait_for_completion(&ctx->ctx_done); | |
2554 | io_ring_ctx_free(ctx); | |
2555 | } | |
2556 | ||
2557 | static int io_uring_release(struct inode *inode, struct file *file) | |
2558 | { | |
2559 | struct io_ring_ctx *ctx = file->private_data; | |
2560 | ||
2561 | file->private_data = NULL; | |
2562 | io_ring_ctx_wait_and_kill(ctx); | |
2563 | return 0; | |
2564 | } | |
2565 | ||
2566 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
2567 | { | |
2568 | loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT; | |
2569 | unsigned long sz = vma->vm_end - vma->vm_start; | |
2570 | struct io_ring_ctx *ctx = file->private_data; | |
2571 | unsigned long pfn; | |
2572 | struct page *page; | |
2573 | void *ptr; | |
2574 | ||
2575 | switch (offset) { | |
2576 | case IORING_OFF_SQ_RING: | |
2577 | ptr = ctx->sq_ring; | |
2578 | break; | |
2579 | case IORING_OFF_SQES: | |
2580 | ptr = ctx->sq_sqes; | |
2581 | break; | |
2582 | case IORING_OFF_CQ_RING: | |
2583 | ptr = ctx->cq_ring; | |
2584 | break; | |
2585 | default: | |
2586 | return -EINVAL; | |
2587 | } | |
2588 | ||
2589 | page = virt_to_head_page(ptr); | |
2590 | if (sz > (PAGE_SIZE << compound_order(page))) | |
2591 | return -EINVAL; | |
2592 | ||
2593 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
2594 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
2595 | } | |
2596 | ||
2597 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, | |
2598 | u32, min_complete, u32, flags, const sigset_t __user *, sig, | |
2599 | size_t, sigsz) | |
2600 | { | |
2601 | struct io_ring_ctx *ctx; | |
2602 | long ret = -EBADF; | |
2603 | int submitted = 0; | |
2604 | struct fd f; | |
2605 | ||
2606 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) | |
2607 | return -EINVAL; | |
2608 | ||
2609 | f = fdget(fd); | |
2610 | if (!f.file) | |
2611 | return -EBADF; | |
2612 | ||
2613 | ret = -EOPNOTSUPP; | |
2614 | if (f.file->f_op != &io_uring_fops) | |
2615 | goto out_fput; | |
2616 | ||
2617 | ret = -ENXIO; | |
2618 | ctx = f.file->private_data; | |
2619 | if (!percpu_ref_tryget(&ctx->refs)) | |
2620 | goto out_fput; | |
2621 | ||
2622 | /* | |
2623 | * For SQ polling, the thread will do all submissions and completions. | |
2624 | * Just return the requested submit count, and wake the thread if | |
2625 | * we were asked to. | |
2626 | */ | |
2627 | if (ctx->flags & IORING_SETUP_SQPOLL) { | |
2628 | if (flags & IORING_ENTER_SQ_WAKEUP) | |
2629 | wake_up(&ctx->sqo_wait); | |
2630 | submitted = to_submit; | |
2631 | goto out_ctx; | |
2632 | } | |
2633 | ||
2634 | ret = 0; | |
2635 | if (to_submit) { | |
2636 | to_submit = min(to_submit, ctx->sq_entries); | |
2637 | ||
2638 | mutex_lock(&ctx->uring_lock); | |
2639 | submitted = io_ring_submit(ctx, to_submit); | |
2640 | mutex_unlock(&ctx->uring_lock); | |
2641 | ||
2642 | if (submitted < 0) | |
2643 | goto out_ctx; | |
2644 | } | |
2645 | if (flags & IORING_ENTER_GETEVENTS) { | |
2646 | unsigned nr_events = 0; | |
2647 | ||
2648 | min_complete = min(min_complete, ctx->cq_entries); | |
2649 | ||
2650 | /* | |
2651 | * The application could have included the 'to_submit' count | |
2652 | * in how many events it wanted to wait for. If we failed to | |
2653 | * submit the desired count, we may need to adjust the number | |
2654 | * of events to poll/wait for. | |
2655 | */ | |
2656 | if (submitted < to_submit) | |
2657 | min_complete = min_t(unsigned, submitted, min_complete); | |
2658 | ||
2659 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
2660 | mutex_lock(&ctx->uring_lock); | |
2661 | ret = io_iopoll_check(ctx, &nr_events, min_complete); | |
2662 | mutex_unlock(&ctx->uring_lock); | |
2663 | } else { | |
2664 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); | |
2665 | } | |
2666 | } | |
2667 | ||
2668 | out_ctx: | |
2669 | io_ring_drop_ctx_refs(ctx, 1); | |
2670 | out_fput: | |
2671 | fdput(f); | |
2672 | return submitted ? submitted : ret; | |
2673 | } | |
2674 | ||
2675 | static const struct file_operations io_uring_fops = { | |
2676 | .release = io_uring_release, | |
2677 | .mmap = io_uring_mmap, | |
2678 | .poll = io_uring_poll, | |
2679 | .fasync = io_uring_fasync, | |
2680 | }; | |
2681 | ||
2682 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
2683 | struct io_uring_params *p) | |
2684 | { | |
2685 | struct io_sq_ring *sq_ring; | |
2686 | struct io_cq_ring *cq_ring; | |
2687 | size_t size; | |
2688 | ||
2689 | sq_ring = io_mem_alloc(struct_size(sq_ring, array, p->sq_entries)); | |
2690 | if (!sq_ring) | |
2691 | return -ENOMEM; | |
2692 | ||
2693 | ctx->sq_ring = sq_ring; | |
2694 | sq_ring->ring_mask = p->sq_entries - 1; | |
2695 | sq_ring->ring_entries = p->sq_entries; | |
2696 | ctx->sq_mask = sq_ring->ring_mask; | |
2697 | ctx->sq_entries = sq_ring->ring_entries; | |
2698 | ||
2699 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
2700 | if (size == SIZE_MAX) | |
2701 | return -EOVERFLOW; | |
2702 | ||
2703 | ctx->sq_sqes = io_mem_alloc(size); | |
2704 | if (!ctx->sq_sqes) { | |
2705 | io_mem_free(ctx->sq_ring); | |
2706 | return -ENOMEM; | |
2707 | } | |
2708 | ||
2709 | cq_ring = io_mem_alloc(struct_size(cq_ring, cqes, p->cq_entries)); | |
2710 | if (!cq_ring) { | |
2711 | io_mem_free(ctx->sq_ring); | |
2712 | io_mem_free(ctx->sq_sqes); | |
2713 | return -ENOMEM; | |
2714 | } | |
2715 | ||
2716 | ctx->cq_ring = cq_ring; | |
2717 | cq_ring->ring_mask = p->cq_entries - 1; | |
2718 | cq_ring->ring_entries = p->cq_entries; | |
2719 | ctx->cq_mask = cq_ring->ring_mask; | |
2720 | ctx->cq_entries = cq_ring->ring_entries; | |
2721 | return 0; | |
2722 | } | |
2723 | ||
2724 | /* | |
2725 | * Allocate an anonymous fd, this is what constitutes the application | |
2726 | * visible backing of an io_uring instance. The application mmaps this | |
2727 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
2728 | * we have to tie this fd to a socket for file garbage collection purposes. | |
2729 | */ | |
2730 | static int io_uring_get_fd(struct io_ring_ctx *ctx) | |
2731 | { | |
2732 | struct file *file; | |
2733 | int ret; | |
2734 | ||
2735 | #if defined(CONFIG_UNIX) | |
2736 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, | |
2737 | &ctx->ring_sock); | |
2738 | if (ret) | |
2739 | return ret; | |
2740 | #endif | |
2741 | ||
2742 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
2743 | if (ret < 0) | |
2744 | goto err; | |
2745 | ||
2746 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, | |
2747 | O_RDWR | O_CLOEXEC); | |
2748 | if (IS_ERR(file)) { | |
2749 | put_unused_fd(ret); | |
2750 | ret = PTR_ERR(file); | |
2751 | goto err; | |
2752 | } | |
2753 | ||
2754 | #if defined(CONFIG_UNIX) | |
2755 | ctx->ring_sock->file = file; | |
2756 | ctx->ring_sock->sk->sk_user_data = ctx; | |
2757 | #endif | |
2758 | fd_install(ret, file); | |
2759 | return ret; | |
2760 | err: | |
2761 | #if defined(CONFIG_UNIX) | |
2762 | sock_release(ctx->ring_sock); | |
2763 | ctx->ring_sock = NULL; | |
2764 | #endif | |
2765 | return ret; | |
2766 | } | |
2767 | ||
2768 | static int io_uring_create(unsigned entries, struct io_uring_params *p) | |
2769 | { | |
2770 | struct user_struct *user = NULL; | |
2771 | struct io_ring_ctx *ctx; | |
2772 | bool account_mem; | |
2773 | int ret; | |
2774 | ||
2775 | if (!entries || entries > IORING_MAX_ENTRIES) | |
2776 | return -EINVAL; | |
2777 | ||
2778 | /* | |
2779 | * Use twice as many entries for the CQ ring. It's possible for the | |
2780 | * application to drive a higher depth than the size of the SQ ring, | |
2781 | * since the sqes are only used at submission time. This allows for | |
2782 | * some flexibility in overcommitting a bit. | |
2783 | */ | |
2784 | p->sq_entries = roundup_pow_of_two(entries); | |
2785 | p->cq_entries = 2 * p->sq_entries; | |
2786 | ||
2787 | user = get_uid(current_user()); | |
2788 | account_mem = !capable(CAP_IPC_LOCK); | |
2789 | ||
2790 | if (account_mem) { | |
2791 | ret = io_account_mem(user, | |
2792 | ring_pages(p->sq_entries, p->cq_entries)); | |
2793 | if (ret) { | |
2794 | free_uid(user); | |
2795 | return ret; | |
2796 | } | |
2797 | } | |
2798 | ||
2799 | ctx = io_ring_ctx_alloc(p); | |
2800 | if (!ctx) { | |
2801 | if (account_mem) | |
2802 | io_unaccount_mem(user, ring_pages(p->sq_entries, | |
2803 | p->cq_entries)); | |
2804 | free_uid(user); | |
2805 | return -ENOMEM; | |
2806 | } | |
2807 | ctx->compat = in_compat_syscall(); | |
2808 | ctx->account_mem = account_mem; | |
2809 | ctx->user = user; | |
2810 | ||
2811 | ret = io_allocate_scq_urings(ctx, p); | |
2812 | if (ret) | |
2813 | goto err; | |
2814 | ||
2815 | ret = io_sq_offload_start(ctx, p); | |
2816 | if (ret) | |
2817 | goto err; | |
2818 | ||
2819 | ret = io_uring_get_fd(ctx); | |
2820 | if (ret < 0) | |
2821 | goto err; | |
2822 | ||
2823 | memset(&p->sq_off, 0, sizeof(p->sq_off)); | |
2824 | p->sq_off.head = offsetof(struct io_sq_ring, r.head); | |
2825 | p->sq_off.tail = offsetof(struct io_sq_ring, r.tail); | |
2826 | p->sq_off.ring_mask = offsetof(struct io_sq_ring, ring_mask); | |
2827 | p->sq_off.ring_entries = offsetof(struct io_sq_ring, ring_entries); | |
2828 | p->sq_off.flags = offsetof(struct io_sq_ring, flags); | |
2829 | p->sq_off.dropped = offsetof(struct io_sq_ring, dropped); | |
2830 | p->sq_off.array = offsetof(struct io_sq_ring, array); | |
2831 | ||
2832 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
2833 | p->cq_off.head = offsetof(struct io_cq_ring, r.head); | |
2834 | p->cq_off.tail = offsetof(struct io_cq_ring, r.tail); | |
2835 | p->cq_off.ring_mask = offsetof(struct io_cq_ring, ring_mask); | |
2836 | p->cq_off.ring_entries = offsetof(struct io_cq_ring, ring_entries); | |
2837 | p->cq_off.overflow = offsetof(struct io_cq_ring, overflow); | |
2838 | p->cq_off.cqes = offsetof(struct io_cq_ring, cqes); | |
2839 | return ret; | |
2840 | err: | |
2841 | io_ring_ctx_wait_and_kill(ctx); | |
2842 | return ret; | |
2843 | } | |
2844 | ||
2845 | /* | |
2846 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
2847 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
2848 | * params structure passed in. | |
2849 | */ | |
2850 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
2851 | { | |
2852 | struct io_uring_params p; | |
2853 | long ret; | |
2854 | int i; | |
2855 | ||
2856 | if (copy_from_user(&p, params, sizeof(p))) | |
2857 | return -EFAULT; | |
2858 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
2859 | if (p.resv[i]) | |
2860 | return -EINVAL; | |
2861 | } | |
2862 | ||
2863 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | | |
2864 | IORING_SETUP_SQ_AFF)) | |
2865 | return -EINVAL; | |
2866 | ||
2867 | ret = io_uring_create(entries, &p); | |
2868 | if (ret < 0) | |
2869 | return ret; | |
2870 | ||
2871 | if (copy_to_user(params, &p, sizeof(p))) | |
2872 | return -EFAULT; | |
2873 | ||
2874 | return ret; | |
2875 | } | |
2876 | ||
2877 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
2878 | struct io_uring_params __user *, params) | |
2879 | { | |
2880 | return io_uring_setup(entries, params); | |
2881 | } | |
2882 | ||
2883 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, | |
2884 | void __user *arg, unsigned nr_args) | |
2885 | { | |
2886 | int ret; | |
2887 | ||
2888 | percpu_ref_kill(&ctx->refs); | |
2889 | wait_for_completion(&ctx->ctx_done); | |
2890 | ||
2891 | switch (opcode) { | |
2892 | case IORING_REGISTER_BUFFERS: | |
2893 | ret = io_sqe_buffer_register(ctx, arg, nr_args); | |
2894 | break; | |
2895 | case IORING_UNREGISTER_BUFFERS: | |
2896 | ret = -EINVAL; | |
2897 | if (arg || nr_args) | |
2898 | break; | |
2899 | ret = io_sqe_buffer_unregister(ctx); | |
2900 | break; | |
2901 | case IORING_REGISTER_FILES: | |
2902 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
2903 | break; | |
2904 | case IORING_UNREGISTER_FILES: | |
2905 | ret = -EINVAL; | |
2906 | if (arg || nr_args) | |
2907 | break; | |
2908 | ret = io_sqe_files_unregister(ctx); | |
2909 | break; | |
2910 | default: | |
2911 | ret = -EINVAL; | |
2912 | break; | |
2913 | } | |
2914 | ||
2915 | /* bring the ctx back to life */ | |
2916 | reinit_completion(&ctx->ctx_done); | |
2917 | percpu_ref_reinit(&ctx->refs); | |
2918 | return ret; | |
2919 | } | |
2920 | ||
2921 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
2922 | void __user *, arg, unsigned int, nr_args) | |
2923 | { | |
2924 | struct io_ring_ctx *ctx; | |
2925 | long ret = -EBADF; | |
2926 | struct fd f; | |
2927 | ||
2928 | f = fdget(fd); | |
2929 | if (!f.file) | |
2930 | return -EBADF; | |
2931 | ||
2932 | ret = -EOPNOTSUPP; | |
2933 | if (f.file->f_op != &io_uring_fops) | |
2934 | goto out_fput; | |
2935 | ||
2936 | ctx = f.file->private_data; | |
2937 | ||
2938 | mutex_lock(&ctx->uring_lock); | |
2939 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
2940 | mutex_unlock(&ctx->uring_lock); | |
2941 | out_fput: | |
2942 | fdput(f); | |
2943 | return ret; | |
2944 | } | |
2945 | ||
2946 | static int __init io_uring_init(void) | |
2947 | { | |
2948 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); | |
2949 | return 0; | |
2950 | }; | |
2951 | __initcall(io_uring_init); |