]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - fs/aio.c
fs/aio: Add support to aio ring pages migration
[mirror_ubuntu-eoan-kernel.git] / fs / aio.c
1 /*
2 * An async IO implementation for Linux
3 * Written by Benjamin LaHaise <bcrl@kvack.org>
4 *
5 * Implements an efficient asynchronous io interface.
6 *
7 * Copyright 2000, 2001, 2002 Red Hat, Inc. All Rights Reserved.
8 *
9 * See ../COPYING for licensing terms.
10 */
11 #define pr_fmt(fmt) "%s: " fmt, __func__
12
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/errno.h>
16 #include <linux/time.h>
17 #include <linux/aio_abi.h>
18 #include <linux/export.h>
19 #include <linux/syscalls.h>
20 #include <linux/backing-dev.h>
21 #include <linux/uio.h>
22
23 #include <linux/sched.h>
24 #include <linux/fs.h>
25 #include <linux/file.h>
26 #include <linux/mm.h>
27 #include <linux/mman.h>
28 #include <linux/mmu_context.h>
29 #include <linux/slab.h>
30 #include <linux/timer.h>
31 #include <linux/aio.h>
32 #include <linux/highmem.h>
33 #include <linux/workqueue.h>
34 #include <linux/security.h>
35 #include <linux/eventfd.h>
36 #include <linux/blkdev.h>
37 #include <linux/compat.h>
38 #include <linux/anon_inodes.h>
39 #include <linux/migrate.h>
40 #include <linux/ramfs.h>
41
42 #include <asm/kmap_types.h>
43 #include <asm/uaccess.h>
44
45 #include "internal.h"
46
47 #define AIO_RING_MAGIC 0xa10a10a1
48 #define AIO_RING_COMPAT_FEATURES 1
49 #define AIO_RING_INCOMPAT_FEATURES 0
50 struct aio_ring {
51 unsigned id; /* kernel internal index number */
52 unsigned nr; /* number of io_events */
53 unsigned head;
54 unsigned tail;
55
56 unsigned magic;
57 unsigned compat_features;
58 unsigned incompat_features;
59 unsigned header_length; /* size of aio_ring */
60
61
62 struct io_event io_events[0];
63 }; /* 128 bytes + ring size */
64
65 #define AIO_RING_PAGES 8
66
67 struct kioctx {
68 atomic_t users;
69 atomic_t dead;
70
71 /* This needs improving */
72 unsigned long user_id;
73 struct hlist_node list;
74
75 /*
76 * This is what userspace passed to io_setup(), it's not used for
77 * anything but counting against the global max_reqs quota.
78 *
79 * The real limit is nr_events - 1, which will be larger (see
80 * aio_setup_ring())
81 */
82 unsigned max_reqs;
83
84 /* Size of ringbuffer, in units of struct io_event */
85 unsigned nr_events;
86
87 unsigned long mmap_base;
88 unsigned long mmap_size;
89
90 struct page **ring_pages;
91 long nr_pages;
92
93 struct rcu_head rcu_head;
94 struct work_struct rcu_work;
95
96 struct {
97 atomic_t reqs_active;
98 } ____cacheline_aligned_in_smp;
99
100 struct {
101 spinlock_t ctx_lock;
102 struct list_head active_reqs; /* used for cancellation */
103 } ____cacheline_aligned_in_smp;
104
105 struct {
106 struct mutex ring_lock;
107 wait_queue_head_t wait;
108 } ____cacheline_aligned_in_smp;
109
110 struct {
111 unsigned tail;
112 spinlock_t completion_lock;
113 } ____cacheline_aligned_in_smp;
114
115 struct page *internal_pages[AIO_RING_PAGES];
116 struct file *aio_ring_file;
117 };
118
119 /*------ sysctl variables----*/
120 static DEFINE_SPINLOCK(aio_nr_lock);
121 unsigned long aio_nr; /* current system wide number of aio requests */
122 unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
123 /*----end sysctl variables---*/
124
125 static struct kmem_cache *kiocb_cachep;
126 static struct kmem_cache *kioctx_cachep;
127
128 /* aio_setup
129 * Creates the slab caches used by the aio routines, panic on
130 * failure as this is done early during the boot sequence.
131 */
132 static int __init aio_setup(void)
133 {
134 kiocb_cachep = KMEM_CACHE(kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
135 kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
136
137 pr_debug("sizeof(struct page) = %zu\n", sizeof(struct page));
138
139 return 0;
140 }
141 __initcall(aio_setup);
142
143 static void aio_free_ring(struct kioctx *ctx)
144 {
145 int i;
146 struct file *aio_ring_file = ctx->aio_ring_file;
147
148 for (i = 0; i < ctx->nr_pages; i++) {
149 pr_debug("pid(%d) [%d] page->count=%d\n", current->pid, i,
150 page_count(ctx->ring_pages[i]));
151 put_page(ctx->ring_pages[i]);
152 }
153
154 if (ctx->ring_pages && ctx->ring_pages != ctx->internal_pages)
155 kfree(ctx->ring_pages);
156
157 if (aio_ring_file) {
158 truncate_setsize(aio_ring_file->f_inode, 0);
159 pr_debug("pid(%d) i_nlink=%u d_count=%d d_unhashed=%d i_count=%d\n",
160 current->pid, aio_ring_file->f_inode->i_nlink,
161 aio_ring_file->f_path.dentry->d_count,
162 d_unhashed(aio_ring_file->f_path.dentry),
163 atomic_read(&aio_ring_file->f_inode->i_count));
164 fput(aio_ring_file);
165 ctx->aio_ring_file = NULL;
166 }
167 }
168
169 static int aio_ring_mmap(struct file *file, struct vm_area_struct *vma)
170 {
171 vma->vm_ops = &generic_file_vm_ops;
172 return 0;
173 }
174
175 static const struct file_operations aio_ring_fops = {
176 .mmap = aio_ring_mmap,
177 };
178
179 static int aio_set_page_dirty(struct page *page)
180 {
181 return 0;
182 }
183
184 static int aio_migratepage(struct address_space *mapping, struct page *new,
185 struct page *old, enum migrate_mode mode)
186 {
187 struct kioctx *ctx = mapping->private_data;
188 unsigned long flags;
189 unsigned idx = old->index;
190 int rc;
191
192 /* Writeback must be complete */
193 BUG_ON(PageWriteback(old));
194 put_page(old);
195
196 rc = migrate_page_move_mapping(mapping, new, old, NULL, mode);
197 if (rc != MIGRATEPAGE_SUCCESS) {
198 get_page(old);
199 return rc;
200 }
201
202 get_page(new);
203
204 spin_lock_irqsave(&ctx->completion_lock, flags);
205 migrate_page_copy(new, old);
206 ctx->ring_pages[idx] = new;
207 spin_unlock_irqrestore(&ctx->completion_lock, flags);
208
209 return rc;
210 }
211
212 static const struct address_space_operations aio_ctx_aops = {
213 .set_page_dirty = aio_set_page_dirty,
214 .migratepage = aio_migratepage,
215 };
216
217 static int aio_setup_ring(struct kioctx *ctx)
218 {
219 struct aio_ring *ring;
220 unsigned nr_events = ctx->max_reqs;
221 struct mm_struct *mm = current->mm;
222 unsigned long size, populate;
223 int nr_pages;
224 int i;
225 struct file *file;
226
227 /* Compensate for the ring buffer's head/tail overlap entry */
228 nr_events += 2; /* 1 is required, 2 for good luck */
229
230 size = sizeof(struct aio_ring);
231 size += sizeof(struct io_event) * nr_events;
232
233 nr_pages = PFN_UP(size);
234 if (nr_pages < 0)
235 return -EINVAL;
236
237 file = anon_inode_getfile_private("[aio]", &aio_ring_fops, ctx, O_RDWR);
238 if (IS_ERR(file)) {
239 ctx->aio_ring_file = NULL;
240 return -EAGAIN;
241 }
242
243 file->f_inode->i_mapping->a_ops = &aio_ctx_aops;
244 file->f_inode->i_mapping->private_data = ctx;
245 file->f_inode->i_size = PAGE_SIZE * (loff_t)nr_pages;
246
247 for (i = 0; i < nr_pages; i++) {
248 struct page *page;
249 page = find_or_create_page(file->f_inode->i_mapping,
250 i, GFP_HIGHUSER | __GFP_ZERO);
251 if (!page)
252 break;
253 pr_debug("pid(%d) page[%d]->count=%d\n",
254 current->pid, i, page_count(page));
255 SetPageUptodate(page);
256 SetPageDirty(page);
257 unlock_page(page);
258 }
259 ctx->aio_ring_file = file;
260 nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring))
261 / sizeof(struct io_event);
262
263 ctx->ring_pages = ctx->internal_pages;
264 if (nr_pages > AIO_RING_PAGES) {
265 ctx->ring_pages = kcalloc(nr_pages, sizeof(struct page *),
266 GFP_KERNEL);
267 if (!ctx->ring_pages)
268 return -ENOMEM;
269 }
270
271 ctx->mmap_size = nr_pages * PAGE_SIZE;
272 pr_debug("attempting mmap of %lu bytes\n", ctx->mmap_size);
273
274 down_write(&mm->mmap_sem);
275 ctx->mmap_base = do_mmap_pgoff(ctx->aio_ring_file, 0, ctx->mmap_size,
276 PROT_READ | PROT_WRITE,
277 MAP_SHARED | MAP_POPULATE, 0, &populate);
278 if (IS_ERR((void *)ctx->mmap_base)) {
279 up_write(&mm->mmap_sem);
280 ctx->mmap_size = 0;
281 aio_free_ring(ctx);
282 return -EAGAIN;
283 }
284 up_write(&mm->mmap_sem);
285
286 mm_populate(ctx->mmap_base, populate);
287
288 pr_debug("mmap address: 0x%08lx\n", ctx->mmap_base);
289 ctx->nr_pages = get_user_pages(current, mm, ctx->mmap_base, nr_pages,
290 1, 0, ctx->ring_pages, NULL);
291 for (i = 0; i < ctx->nr_pages; i++)
292 put_page(ctx->ring_pages[i]);
293
294 if (unlikely(ctx->nr_pages != nr_pages)) {
295 aio_free_ring(ctx);
296 return -EAGAIN;
297 }
298
299 ctx->user_id = ctx->mmap_base;
300 ctx->nr_events = nr_events; /* trusted copy */
301
302 ring = kmap_atomic(ctx->ring_pages[0]);
303 ring->nr = nr_events; /* user copy */
304 ring->id = ctx->user_id;
305 ring->head = ring->tail = 0;
306 ring->magic = AIO_RING_MAGIC;
307 ring->compat_features = AIO_RING_COMPAT_FEATURES;
308 ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
309 ring->header_length = sizeof(struct aio_ring);
310 kunmap_atomic(ring);
311 flush_dcache_page(ctx->ring_pages[0]);
312
313 return 0;
314 }
315
316 #define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
317 #define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
318 #define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
319
320 void kiocb_set_cancel_fn(struct kiocb *req, kiocb_cancel_fn *cancel)
321 {
322 struct kioctx *ctx = req->ki_ctx;
323 unsigned long flags;
324
325 spin_lock_irqsave(&ctx->ctx_lock, flags);
326
327 if (!req->ki_list.next)
328 list_add(&req->ki_list, &ctx->active_reqs);
329
330 req->ki_cancel = cancel;
331
332 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
333 }
334 EXPORT_SYMBOL(kiocb_set_cancel_fn);
335
336 static int kiocb_cancel(struct kioctx *ctx, struct kiocb *kiocb,
337 struct io_event *res)
338 {
339 kiocb_cancel_fn *old, *cancel;
340 int ret = -EINVAL;
341
342 /*
343 * Don't want to set kiocb->ki_cancel = KIOCB_CANCELLED unless it
344 * actually has a cancel function, hence the cmpxchg()
345 */
346
347 cancel = ACCESS_ONCE(kiocb->ki_cancel);
348 do {
349 if (!cancel || cancel == KIOCB_CANCELLED)
350 return ret;
351
352 old = cancel;
353 cancel = cmpxchg(&kiocb->ki_cancel, old, KIOCB_CANCELLED);
354 } while (cancel != old);
355
356 atomic_inc(&kiocb->ki_users);
357 spin_unlock_irq(&ctx->ctx_lock);
358
359 memset(res, 0, sizeof(*res));
360 res->obj = (u64)(unsigned long)kiocb->ki_obj.user;
361 res->data = kiocb->ki_user_data;
362 ret = cancel(kiocb, res);
363
364 spin_lock_irq(&ctx->ctx_lock);
365
366 return ret;
367 }
368
369 static void free_ioctx_rcu(struct rcu_head *head)
370 {
371 struct kioctx *ctx = container_of(head, struct kioctx, rcu_head);
372 kmem_cache_free(kioctx_cachep, ctx);
373 }
374
375 /*
376 * When this function runs, the kioctx has been removed from the "hash table"
377 * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
378 * now it's safe to cancel any that need to be.
379 */
380 static void free_ioctx(struct kioctx *ctx)
381 {
382 struct aio_ring *ring;
383 struct io_event res;
384 struct kiocb *req;
385 unsigned head, avail;
386
387 spin_lock_irq(&ctx->ctx_lock);
388
389 while (!list_empty(&ctx->active_reqs)) {
390 req = list_first_entry(&ctx->active_reqs,
391 struct kiocb, ki_list);
392
393 list_del_init(&req->ki_list);
394 kiocb_cancel(ctx, req, &res);
395 }
396
397 spin_unlock_irq(&ctx->ctx_lock);
398
399 ring = kmap_atomic(ctx->ring_pages[0]);
400 head = ring->head;
401 kunmap_atomic(ring);
402
403 while (atomic_read(&ctx->reqs_active) > 0) {
404 wait_event(ctx->wait,
405 head != ctx->tail ||
406 atomic_read(&ctx->reqs_active) <= 0);
407
408 avail = (head <= ctx->tail ? ctx->tail : ctx->nr_events) - head;
409
410 atomic_sub(avail, &ctx->reqs_active);
411 head += avail;
412 head %= ctx->nr_events;
413 }
414
415 WARN_ON(atomic_read(&ctx->reqs_active) < 0);
416
417 aio_free_ring(ctx);
418
419 pr_debug("freeing %p\n", ctx);
420
421 /*
422 * Here the call_rcu() is between the wait_event() for reqs_active to
423 * hit 0, and freeing the ioctx.
424 *
425 * aio_complete() decrements reqs_active, but it has to touch the ioctx
426 * after to issue a wakeup so we use rcu.
427 */
428 call_rcu(&ctx->rcu_head, free_ioctx_rcu);
429 }
430
431 static void put_ioctx(struct kioctx *ctx)
432 {
433 if (unlikely(atomic_dec_and_test(&ctx->users)))
434 free_ioctx(ctx);
435 }
436
437 /* ioctx_alloc
438 * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
439 */
440 static struct kioctx *ioctx_alloc(unsigned nr_events)
441 {
442 struct mm_struct *mm = current->mm;
443 struct kioctx *ctx;
444 int err = -ENOMEM;
445
446 /* Prevent overflows */
447 if ((nr_events > (0x10000000U / sizeof(struct io_event))) ||
448 (nr_events > (0x10000000U / sizeof(struct kiocb)))) {
449 pr_debug("ENOMEM: nr_events too high\n");
450 return ERR_PTR(-EINVAL);
451 }
452
453 if (!nr_events || (unsigned long)nr_events > aio_max_nr)
454 return ERR_PTR(-EAGAIN);
455
456 ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
457 if (!ctx)
458 return ERR_PTR(-ENOMEM);
459
460 ctx->max_reqs = nr_events;
461
462 atomic_set(&ctx->users, 2);
463 atomic_set(&ctx->dead, 0);
464 spin_lock_init(&ctx->ctx_lock);
465 spin_lock_init(&ctx->completion_lock);
466 mutex_init(&ctx->ring_lock);
467 init_waitqueue_head(&ctx->wait);
468
469 INIT_LIST_HEAD(&ctx->active_reqs);
470
471 if (aio_setup_ring(ctx) < 0)
472 goto out_freectx;
473
474 /* limit the number of system wide aios */
475 spin_lock(&aio_nr_lock);
476 if (aio_nr + nr_events > aio_max_nr ||
477 aio_nr + nr_events < aio_nr) {
478 spin_unlock(&aio_nr_lock);
479 goto out_cleanup;
480 }
481 aio_nr += ctx->max_reqs;
482 spin_unlock(&aio_nr_lock);
483
484 /* now link into global list. */
485 spin_lock(&mm->ioctx_lock);
486 hlist_add_head_rcu(&ctx->list, &mm->ioctx_list);
487 spin_unlock(&mm->ioctx_lock);
488
489 pr_debug("allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
490 ctx, ctx->user_id, mm, ctx->nr_events);
491 return ctx;
492
493 out_cleanup:
494 err = -EAGAIN;
495 aio_free_ring(ctx);
496 out_freectx:
497 if (ctx->aio_ring_file)
498 fput(ctx->aio_ring_file);
499 kmem_cache_free(kioctx_cachep, ctx);
500 pr_debug("error allocating ioctx %d\n", err);
501 return ERR_PTR(err);
502 }
503
504 static void kill_ioctx_work(struct work_struct *work)
505 {
506 struct kioctx *ctx = container_of(work, struct kioctx, rcu_work);
507
508 wake_up_all(&ctx->wait);
509 put_ioctx(ctx);
510 }
511
512 static void kill_ioctx_rcu(struct rcu_head *head)
513 {
514 struct kioctx *ctx = container_of(head, struct kioctx, rcu_head);
515
516 INIT_WORK(&ctx->rcu_work, kill_ioctx_work);
517 schedule_work(&ctx->rcu_work);
518 }
519
520 /* kill_ioctx
521 * Cancels all outstanding aio requests on an aio context. Used
522 * when the processes owning a context have all exited to encourage
523 * the rapid destruction of the kioctx.
524 */
525 static void kill_ioctx(struct kioctx *ctx)
526 {
527 if (!atomic_xchg(&ctx->dead, 1)) {
528 hlist_del_rcu(&ctx->list);
529
530 /*
531 * It'd be more correct to do this in free_ioctx(), after all
532 * the outstanding kiocbs have finished - but by then io_destroy
533 * has already returned, so io_setup() could potentially return
534 * -EAGAIN with no ioctxs actually in use (as far as userspace
535 * could tell).
536 */
537 spin_lock(&aio_nr_lock);
538 BUG_ON(aio_nr - ctx->max_reqs > aio_nr);
539 aio_nr -= ctx->max_reqs;
540 spin_unlock(&aio_nr_lock);
541
542 if (ctx->mmap_size)
543 vm_munmap(ctx->mmap_base, ctx->mmap_size);
544
545 /* Between hlist_del_rcu() and dropping the initial ref */
546 call_rcu(&ctx->rcu_head, kill_ioctx_rcu);
547 }
548 }
549
550 /* wait_on_sync_kiocb:
551 * Waits on the given sync kiocb to complete.
552 */
553 ssize_t wait_on_sync_kiocb(struct kiocb *iocb)
554 {
555 while (atomic_read(&iocb->ki_users)) {
556 set_current_state(TASK_UNINTERRUPTIBLE);
557 if (!atomic_read(&iocb->ki_users))
558 break;
559 io_schedule();
560 }
561 __set_current_state(TASK_RUNNING);
562 return iocb->ki_user_data;
563 }
564 EXPORT_SYMBOL(wait_on_sync_kiocb);
565
566 /*
567 * exit_aio: called when the last user of mm goes away. At this point, there is
568 * no way for any new requests to be submited or any of the io_* syscalls to be
569 * called on the context.
570 *
571 * There may be outstanding kiocbs, but free_ioctx() will explicitly wait on
572 * them.
573 */
574 void exit_aio(struct mm_struct *mm)
575 {
576 struct kioctx *ctx;
577 struct hlist_node *n;
578
579 hlist_for_each_entry_safe(ctx, n, &mm->ioctx_list, list) {
580 if (1 != atomic_read(&ctx->users))
581 printk(KERN_DEBUG
582 "exit_aio:ioctx still alive: %d %d %d\n",
583 atomic_read(&ctx->users),
584 atomic_read(&ctx->dead),
585 atomic_read(&ctx->reqs_active));
586 /*
587 * We don't need to bother with munmap() here -
588 * exit_mmap(mm) is coming and it'll unmap everything.
589 * Since aio_free_ring() uses non-zero ->mmap_size
590 * as indicator that it needs to unmap the area,
591 * just set it to 0; aio_free_ring() is the only
592 * place that uses ->mmap_size, so it's safe.
593 */
594 ctx->mmap_size = 0;
595
596 kill_ioctx(ctx);
597 }
598 }
599
600 /* aio_get_req
601 * Allocate a slot for an aio request. Increments the ki_users count
602 * of the kioctx so that the kioctx stays around until all requests are
603 * complete. Returns NULL if no requests are free.
604 *
605 * Returns with kiocb->ki_users set to 2. The io submit code path holds
606 * an extra reference while submitting the i/o.
607 * This prevents races between the aio code path referencing the
608 * req (after submitting it) and aio_complete() freeing the req.
609 */
610 static inline struct kiocb *aio_get_req(struct kioctx *ctx)
611 {
612 struct kiocb *req;
613
614 if (atomic_read(&ctx->reqs_active) >= ctx->nr_events)
615 return NULL;
616
617 if (atomic_inc_return(&ctx->reqs_active) > ctx->nr_events - 1)
618 goto out_put;
619
620 req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL|__GFP_ZERO);
621 if (unlikely(!req))
622 goto out_put;
623
624 atomic_set(&req->ki_users, 2);
625 req->ki_ctx = ctx;
626
627 return req;
628 out_put:
629 atomic_dec(&ctx->reqs_active);
630 return NULL;
631 }
632
633 static void kiocb_free(struct kiocb *req)
634 {
635 if (req->ki_filp)
636 fput(req->ki_filp);
637 if (req->ki_eventfd != NULL)
638 eventfd_ctx_put(req->ki_eventfd);
639 if (req->ki_dtor)
640 req->ki_dtor(req);
641 if (req->ki_iovec != &req->ki_inline_vec)
642 kfree(req->ki_iovec);
643 kmem_cache_free(kiocb_cachep, req);
644 }
645
646 void aio_put_req(struct kiocb *req)
647 {
648 if (atomic_dec_and_test(&req->ki_users))
649 kiocb_free(req);
650 }
651 EXPORT_SYMBOL(aio_put_req);
652
653 static struct kioctx *lookup_ioctx(unsigned long ctx_id)
654 {
655 struct mm_struct *mm = current->mm;
656 struct kioctx *ctx, *ret = NULL;
657
658 rcu_read_lock();
659
660 hlist_for_each_entry_rcu(ctx, &mm->ioctx_list, list) {
661 if (ctx->user_id == ctx_id) {
662 atomic_inc(&ctx->users);
663 ret = ctx;
664 break;
665 }
666 }
667
668 rcu_read_unlock();
669 return ret;
670 }
671
672 /* aio_complete
673 * Called when the io request on the given iocb is complete.
674 */
675 void aio_complete(struct kiocb *iocb, long res, long res2)
676 {
677 struct kioctx *ctx = iocb->ki_ctx;
678 struct aio_ring *ring;
679 struct io_event *ev_page, *event;
680 unsigned long flags;
681 unsigned tail, pos;
682
683 /*
684 * Special case handling for sync iocbs:
685 * - events go directly into the iocb for fast handling
686 * - the sync task with the iocb in its stack holds the single iocb
687 * ref, no other paths have a way to get another ref
688 * - the sync task helpfully left a reference to itself in the iocb
689 */
690 if (is_sync_kiocb(iocb)) {
691 BUG_ON(atomic_read(&iocb->ki_users) != 1);
692 iocb->ki_user_data = res;
693 atomic_set(&iocb->ki_users, 0);
694 wake_up_process(iocb->ki_obj.tsk);
695 return;
696 }
697
698 /*
699 * Take rcu_read_lock() in case the kioctx is being destroyed, as we
700 * need to issue a wakeup after decrementing reqs_active.
701 */
702 rcu_read_lock();
703
704 if (iocb->ki_list.next) {
705 unsigned long flags;
706
707 spin_lock_irqsave(&ctx->ctx_lock, flags);
708 list_del(&iocb->ki_list);
709 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
710 }
711
712 /*
713 * cancelled requests don't get events, userland was given one
714 * when the event got cancelled.
715 */
716 if (unlikely(xchg(&iocb->ki_cancel,
717 KIOCB_CANCELLED) == KIOCB_CANCELLED)) {
718 atomic_dec(&ctx->reqs_active);
719 /* Still need the wake_up in case free_ioctx is waiting */
720 goto put_rq;
721 }
722
723 /*
724 * Add a completion event to the ring buffer. Must be done holding
725 * ctx->completion_lock to prevent other code from messing with the tail
726 * pointer since we might be called from irq context.
727 */
728 spin_lock_irqsave(&ctx->completion_lock, flags);
729
730 tail = ctx->tail;
731 pos = tail + AIO_EVENTS_OFFSET;
732
733 if (++tail >= ctx->nr_events)
734 tail = 0;
735
736 ev_page = kmap_atomic(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
737 event = ev_page + pos % AIO_EVENTS_PER_PAGE;
738
739 event->obj = (u64)(unsigned long)iocb->ki_obj.user;
740 event->data = iocb->ki_user_data;
741 event->res = res;
742 event->res2 = res2;
743
744 kunmap_atomic(ev_page);
745 flush_dcache_page(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
746
747 pr_debug("%p[%u]: %p: %p %Lx %lx %lx\n",
748 ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
749 res, res2);
750
751 /* after flagging the request as done, we
752 * must never even look at it again
753 */
754 smp_wmb(); /* make event visible before updating tail */
755
756 ctx->tail = tail;
757
758 ring = kmap_atomic(ctx->ring_pages[0]);
759 ring->tail = tail;
760 kunmap_atomic(ring);
761 flush_dcache_page(ctx->ring_pages[0]);
762
763 spin_unlock_irqrestore(&ctx->completion_lock, flags);
764
765 pr_debug("added to ring %p at [%u]\n", iocb, tail);
766
767 /*
768 * Check if the user asked us to deliver the result through an
769 * eventfd. The eventfd_signal() function is safe to be called
770 * from IRQ context.
771 */
772 if (iocb->ki_eventfd != NULL)
773 eventfd_signal(iocb->ki_eventfd, 1);
774
775 put_rq:
776 /* everything turned out well, dispose of the aiocb. */
777 aio_put_req(iocb);
778
779 /*
780 * We have to order our ring_info tail store above and test
781 * of the wait list below outside the wait lock. This is
782 * like in wake_up_bit() where clearing a bit has to be
783 * ordered with the unlocked test.
784 */
785 smp_mb();
786
787 if (waitqueue_active(&ctx->wait))
788 wake_up(&ctx->wait);
789
790 rcu_read_unlock();
791 }
792 EXPORT_SYMBOL(aio_complete);
793
794 /* aio_read_events
795 * Pull an event off of the ioctx's event ring. Returns the number of
796 * events fetched
797 */
798 static long aio_read_events_ring(struct kioctx *ctx,
799 struct io_event __user *event, long nr)
800 {
801 struct aio_ring *ring;
802 unsigned head, pos;
803 long ret = 0;
804 int copy_ret;
805
806 mutex_lock(&ctx->ring_lock);
807
808 ring = kmap_atomic(ctx->ring_pages[0]);
809 head = ring->head;
810 kunmap_atomic(ring);
811
812 pr_debug("h%u t%u m%u\n", head, ctx->tail, ctx->nr_events);
813
814 if (head == ctx->tail)
815 goto out;
816
817 while (ret < nr) {
818 long avail;
819 struct io_event *ev;
820 struct page *page;
821
822 avail = (head <= ctx->tail ? ctx->tail : ctx->nr_events) - head;
823 if (head == ctx->tail)
824 break;
825
826 avail = min(avail, nr - ret);
827 avail = min_t(long, avail, AIO_EVENTS_PER_PAGE -
828 ((head + AIO_EVENTS_OFFSET) % AIO_EVENTS_PER_PAGE));
829
830 pos = head + AIO_EVENTS_OFFSET;
831 page = ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE];
832 pos %= AIO_EVENTS_PER_PAGE;
833
834 ev = kmap(page);
835 copy_ret = copy_to_user(event + ret, ev + pos,
836 sizeof(*ev) * avail);
837 kunmap(page);
838
839 if (unlikely(copy_ret)) {
840 ret = -EFAULT;
841 goto out;
842 }
843
844 ret += avail;
845 head += avail;
846 head %= ctx->nr_events;
847 }
848
849 ring = kmap_atomic(ctx->ring_pages[0]);
850 ring->head = head;
851 kunmap_atomic(ring);
852 flush_dcache_page(ctx->ring_pages[0]);
853
854 pr_debug("%li h%u t%u\n", ret, head, ctx->tail);
855
856 atomic_sub(ret, &ctx->reqs_active);
857 out:
858 mutex_unlock(&ctx->ring_lock);
859
860 return ret;
861 }
862
863 static bool aio_read_events(struct kioctx *ctx, long min_nr, long nr,
864 struct io_event __user *event, long *i)
865 {
866 long ret = aio_read_events_ring(ctx, event + *i, nr - *i);
867
868 if (ret > 0)
869 *i += ret;
870
871 if (unlikely(atomic_read(&ctx->dead)))
872 ret = -EINVAL;
873
874 if (!*i)
875 *i = ret;
876
877 return ret < 0 || *i >= min_nr;
878 }
879
880 static long read_events(struct kioctx *ctx, long min_nr, long nr,
881 struct io_event __user *event,
882 struct timespec __user *timeout)
883 {
884 ktime_t until = { .tv64 = KTIME_MAX };
885 long ret = 0;
886
887 if (timeout) {
888 struct timespec ts;
889
890 if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
891 return -EFAULT;
892
893 until = timespec_to_ktime(ts);
894 }
895
896 /*
897 * Note that aio_read_events() is being called as the conditional - i.e.
898 * we're calling it after prepare_to_wait() has set task state to
899 * TASK_INTERRUPTIBLE.
900 *
901 * But aio_read_events() can block, and if it blocks it's going to flip
902 * the task state back to TASK_RUNNING.
903 *
904 * This should be ok, provided it doesn't flip the state back to
905 * TASK_RUNNING and return 0 too much - that causes us to spin. That
906 * will only happen if the mutex_lock() call blocks, and we then find
907 * the ringbuffer empty. So in practice we should be ok, but it's
908 * something to be aware of when touching this code.
909 */
910 wait_event_interruptible_hrtimeout(ctx->wait,
911 aio_read_events(ctx, min_nr, nr, event, &ret), until);
912
913 if (!ret && signal_pending(current))
914 ret = -EINTR;
915
916 return ret;
917 }
918
919 /* sys_io_setup:
920 * Create an aio_context capable of receiving at least nr_events.
921 * ctxp must not point to an aio_context that already exists, and
922 * must be initialized to 0 prior to the call. On successful
923 * creation of the aio_context, *ctxp is filled in with the resulting
924 * handle. May fail with -EINVAL if *ctxp is not initialized,
925 * if the specified nr_events exceeds internal limits. May fail
926 * with -EAGAIN if the specified nr_events exceeds the user's limit
927 * of available events. May fail with -ENOMEM if insufficient kernel
928 * resources are available. May fail with -EFAULT if an invalid
929 * pointer is passed for ctxp. Will fail with -ENOSYS if not
930 * implemented.
931 */
932 SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
933 {
934 struct kioctx *ioctx = NULL;
935 unsigned long ctx;
936 long ret;
937
938 ret = get_user(ctx, ctxp);
939 if (unlikely(ret))
940 goto out;
941
942 ret = -EINVAL;
943 if (unlikely(ctx || nr_events == 0)) {
944 pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
945 ctx, nr_events);
946 goto out;
947 }
948
949 ioctx = ioctx_alloc(nr_events);
950 ret = PTR_ERR(ioctx);
951 if (!IS_ERR(ioctx)) {
952 ret = put_user(ioctx->user_id, ctxp);
953 if (ret)
954 kill_ioctx(ioctx);
955 put_ioctx(ioctx);
956 }
957
958 out:
959 return ret;
960 }
961
962 /* sys_io_destroy:
963 * Destroy the aio_context specified. May cancel any outstanding
964 * AIOs and block on completion. Will fail with -ENOSYS if not
965 * implemented. May fail with -EINVAL if the context pointed to
966 * is invalid.
967 */
968 SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
969 {
970 struct kioctx *ioctx = lookup_ioctx(ctx);
971 if (likely(NULL != ioctx)) {
972 kill_ioctx(ioctx);
973 put_ioctx(ioctx);
974 return 0;
975 }
976 pr_debug("EINVAL: io_destroy: invalid context id\n");
977 return -EINVAL;
978 }
979
980 static void aio_advance_iovec(struct kiocb *iocb, ssize_t ret)
981 {
982 struct iovec *iov = &iocb->ki_iovec[iocb->ki_cur_seg];
983
984 BUG_ON(ret <= 0);
985
986 while (iocb->ki_cur_seg < iocb->ki_nr_segs && ret > 0) {
987 ssize_t this = min((ssize_t)iov->iov_len, ret);
988 iov->iov_base += this;
989 iov->iov_len -= this;
990 iocb->ki_left -= this;
991 ret -= this;
992 if (iov->iov_len == 0) {
993 iocb->ki_cur_seg++;
994 iov++;
995 }
996 }
997
998 /* the caller should not have done more io than what fit in
999 * the remaining iovecs */
1000 BUG_ON(ret > 0 && iocb->ki_left == 0);
1001 }
1002
1003 typedef ssize_t (aio_rw_op)(struct kiocb *, const struct iovec *,
1004 unsigned long, loff_t);
1005
1006 static ssize_t aio_rw_vect_retry(struct kiocb *iocb, int rw, aio_rw_op *rw_op)
1007 {
1008 struct file *file = iocb->ki_filp;
1009 struct address_space *mapping = file->f_mapping;
1010 struct inode *inode = mapping->host;
1011 ssize_t ret = 0;
1012
1013 /* This matches the pread()/pwrite() logic */
1014 if (iocb->ki_pos < 0)
1015 return -EINVAL;
1016
1017 if (rw == WRITE)
1018 file_start_write(file);
1019 do {
1020 ret = rw_op(iocb, &iocb->ki_iovec[iocb->ki_cur_seg],
1021 iocb->ki_nr_segs - iocb->ki_cur_seg,
1022 iocb->ki_pos);
1023 if (ret > 0)
1024 aio_advance_iovec(iocb, ret);
1025
1026 /* retry all partial writes. retry partial reads as long as its a
1027 * regular file. */
1028 } while (ret > 0 && iocb->ki_left > 0 &&
1029 (rw == WRITE ||
1030 (!S_ISFIFO(inode->i_mode) && !S_ISSOCK(inode->i_mode))));
1031 if (rw == WRITE)
1032 file_end_write(file);
1033
1034 /* This means we must have transferred all that we could */
1035 /* No need to retry anymore */
1036 if ((ret == 0) || (iocb->ki_left == 0))
1037 ret = iocb->ki_nbytes - iocb->ki_left;
1038
1039 /* If we managed to write some out we return that, rather than
1040 * the eventual error. */
1041 if (rw == WRITE
1042 && ret < 0 && ret != -EIOCBQUEUED
1043 && iocb->ki_nbytes - iocb->ki_left)
1044 ret = iocb->ki_nbytes - iocb->ki_left;
1045
1046 return ret;
1047 }
1048
1049 static ssize_t aio_setup_vectored_rw(int rw, struct kiocb *kiocb, bool compat)
1050 {
1051 ssize_t ret;
1052
1053 kiocb->ki_nr_segs = kiocb->ki_nbytes;
1054
1055 #ifdef CONFIG_COMPAT
1056 if (compat)
1057 ret = compat_rw_copy_check_uvector(rw,
1058 (struct compat_iovec __user *)kiocb->ki_buf,
1059 kiocb->ki_nr_segs, 1, &kiocb->ki_inline_vec,
1060 &kiocb->ki_iovec);
1061 else
1062 #endif
1063 ret = rw_copy_check_uvector(rw,
1064 (struct iovec __user *)kiocb->ki_buf,
1065 kiocb->ki_nr_segs, 1, &kiocb->ki_inline_vec,
1066 &kiocb->ki_iovec);
1067 if (ret < 0)
1068 return ret;
1069
1070 /* ki_nbytes now reflect bytes instead of segs */
1071 kiocb->ki_nbytes = ret;
1072 return 0;
1073 }
1074
1075 static ssize_t aio_setup_single_vector(int rw, struct kiocb *kiocb)
1076 {
1077 if (unlikely(!access_ok(!rw, kiocb->ki_buf, kiocb->ki_nbytes)))
1078 return -EFAULT;
1079
1080 kiocb->ki_iovec = &kiocb->ki_inline_vec;
1081 kiocb->ki_iovec->iov_base = kiocb->ki_buf;
1082 kiocb->ki_iovec->iov_len = kiocb->ki_nbytes;
1083 kiocb->ki_nr_segs = 1;
1084 return 0;
1085 }
1086
1087 /*
1088 * aio_setup_iocb:
1089 * Performs the initial checks and aio retry method
1090 * setup for the kiocb at the time of io submission.
1091 */
1092 static ssize_t aio_run_iocb(struct kiocb *req, bool compat)
1093 {
1094 struct file *file = req->ki_filp;
1095 ssize_t ret;
1096 int rw;
1097 fmode_t mode;
1098 aio_rw_op *rw_op;
1099
1100 switch (req->ki_opcode) {
1101 case IOCB_CMD_PREAD:
1102 case IOCB_CMD_PREADV:
1103 mode = FMODE_READ;
1104 rw = READ;
1105 rw_op = file->f_op->aio_read;
1106 goto rw_common;
1107
1108 case IOCB_CMD_PWRITE:
1109 case IOCB_CMD_PWRITEV:
1110 mode = FMODE_WRITE;
1111 rw = WRITE;
1112 rw_op = file->f_op->aio_write;
1113 goto rw_common;
1114 rw_common:
1115 if (unlikely(!(file->f_mode & mode)))
1116 return -EBADF;
1117
1118 if (!rw_op)
1119 return -EINVAL;
1120
1121 ret = (req->ki_opcode == IOCB_CMD_PREADV ||
1122 req->ki_opcode == IOCB_CMD_PWRITEV)
1123 ? aio_setup_vectored_rw(rw, req, compat)
1124 : aio_setup_single_vector(rw, req);
1125 if (ret)
1126 return ret;
1127
1128 ret = rw_verify_area(rw, file, &req->ki_pos, req->ki_nbytes);
1129 if (ret < 0)
1130 return ret;
1131
1132 req->ki_nbytes = ret;
1133 req->ki_left = ret;
1134
1135 ret = aio_rw_vect_retry(req, rw, rw_op);
1136 break;
1137
1138 case IOCB_CMD_FDSYNC:
1139 if (!file->f_op->aio_fsync)
1140 return -EINVAL;
1141
1142 ret = file->f_op->aio_fsync(req, 1);
1143 break;
1144
1145 case IOCB_CMD_FSYNC:
1146 if (!file->f_op->aio_fsync)
1147 return -EINVAL;
1148
1149 ret = file->f_op->aio_fsync(req, 0);
1150 break;
1151
1152 default:
1153 pr_debug("EINVAL: no operation provided\n");
1154 return -EINVAL;
1155 }
1156
1157 if (ret != -EIOCBQUEUED) {
1158 /*
1159 * There's no easy way to restart the syscall since other AIO's
1160 * may be already running. Just fail this IO with EINTR.
1161 */
1162 if (unlikely(ret == -ERESTARTSYS || ret == -ERESTARTNOINTR ||
1163 ret == -ERESTARTNOHAND ||
1164 ret == -ERESTART_RESTARTBLOCK))
1165 ret = -EINTR;
1166 aio_complete(req, ret, 0);
1167 }
1168
1169 return 0;
1170 }
1171
1172 static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
1173 struct iocb *iocb, bool compat)
1174 {
1175 struct kiocb *req;
1176 ssize_t ret;
1177
1178 /* enforce forwards compatibility on users */
1179 if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2)) {
1180 pr_debug("EINVAL: reserve field set\n");
1181 return -EINVAL;
1182 }
1183
1184 /* prevent overflows */
1185 if (unlikely(
1186 (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
1187 (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
1188 ((ssize_t)iocb->aio_nbytes < 0)
1189 )) {
1190 pr_debug("EINVAL: io_submit: overflow check\n");
1191 return -EINVAL;
1192 }
1193
1194 req = aio_get_req(ctx);
1195 if (unlikely(!req))
1196 return -EAGAIN;
1197
1198 req->ki_filp = fget(iocb->aio_fildes);
1199 if (unlikely(!req->ki_filp)) {
1200 ret = -EBADF;
1201 goto out_put_req;
1202 }
1203
1204 if (iocb->aio_flags & IOCB_FLAG_RESFD) {
1205 /*
1206 * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
1207 * instance of the file* now. The file descriptor must be
1208 * an eventfd() fd, and will be signaled for each completed
1209 * event using the eventfd_signal() function.
1210 */
1211 req->ki_eventfd = eventfd_ctx_fdget((int) iocb->aio_resfd);
1212 if (IS_ERR(req->ki_eventfd)) {
1213 ret = PTR_ERR(req->ki_eventfd);
1214 req->ki_eventfd = NULL;
1215 goto out_put_req;
1216 }
1217 }
1218
1219 ret = put_user(KIOCB_KEY, &user_iocb->aio_key);
1220 if (unlikely(ret)) {
1221 pr_debug("EFAULT: aio_key\n");
1222 goto out_put_req;
1223 }
1224
1225 req->ki_obj.user = user_iocb;
1226 req->ki_user_data = iocb->aio_data;
1227 req->ki_pos = iocb->aio_offset;
1228
1229 req->ki_buf = (char __user *)(unsigned long)iocb->aio_buf;
1230 req->ki_left = req->ki_nbytes = iocb->aio_nbytes;
1231 req->ki_opcode = iocb->aio_lio_opcode;
1232
1233 ret = aio_run_iocb(req, compat);
1234 if (ret)
1235 goto out_put_req;
1236
1237 aio_put_req(req); /* drop extra ref to req */
1238 return 0;
1239 out_put_req:
1240 atomic_dec(&ctx->reqs_active);
1241 aio_put_req(req); /* drop extra ref to req */
1242 aio_put_req(req); /* drop i/o ref to req */
1243 return ret;
1244 }
1245
1246 long do_io_submit(aio_context_t ctx_id, long nr,
1247 struct iocb __user *__user *iocbpp, bool compat)
1248 {
1249 struct kioctx *ctx;
1250 long ret = 0;
1251 int i = 0;
1252 struct blk_plug plug;
1253
1254 if (unlikely(nr < 0))
1255 return -EINVAL;
1256
1257 if (unlikely(nr > LONG_MAX/sizeof(*iocbpp)))
1258 nr = LONG_MAX/sizeof(*iocbpp);
1259
1260 if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
1261 return -EFAULT;
1262
1263 ctx = lookup_ioctx(ctx_id);
1264 if (unlikely(!ctx)) {
1265 pr_debug("EINVAL: invalid context id\n");
1266 return -EINVAL;
1267 }
1268
1269 blk_start_plug(&plug);
1270
1271 /*
1272 * AKPM: should this return a partial result if some of the IOs were
1273 * successfully submitted?
1274 */
1275 for (i=0; i<nr; i++) {
1276 struct iocb __user *user_iocb;
1277 struct iocb tmp;
1278
1279 if (unlikely(__get_user(user_iocb, iocbpp + i))) {
1280 ret = -EFAULT;
1281 break;
1282 }
1283
1284 if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
1285 ret = -EFAULT;
1286 break;
1287 }
1288
1289 ret = io_submit_one(ctx, user_iocb, &tmp, compat);
1290 if (ret)
1291 break;
1292 }
1293 blk_finish_plug(&plug);
1294
1295 put_ioctx(ctx);
1296 return i ? i : ret;
1297 }
1298
1299 /* sys_io_submit:
1300 * Queue the nr iocbs pointed to by iocbpp for processing. Returns
1301 * the number of iocbs queued. May return -EINVAL if the aio_context
1302 * specified by ctx_id is invalid, if nr is < 0, if the iocb at
1303 * *iocbpp[0] is not properly initialized, if the operation specified
1304 * is invalid for the file descriptor in the iocb. May fail with
1305 * -EFAULT if any of the data structures point to invalid data. May
1306 * fail with -EBADF if the file descriptor specified in the first
1307 * iocb is invalid. May fail with -EAGAIN if insufficient resources
1308 * are available to queue any iocbs. Will return 0 if nr is 0. Will
1309 * fail with -ENOSYS if not implemented.
1310 */
1311 SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
1312 struct iocb __user * __user *, iocbpp)
1313 {
1314 return do_io_submit(ctx_id, nr, iocbpp, 0);
1315 }
1316
1317 /* lookup_kiocb
1318 * Finds a given iocb for cancellation.
1319 */
1320 static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
1321 u32 key)
1322 {
1323 struct list_head *pos;
1324
1325 assert_spin_locked(&ctx->ctx_lock);
1326
1327 if (key != KIOCB_KEY)
1328 return NULL;
1329
1330 /* TODO: use a hash or array, this sucks. */
1331 list_for_each(pos, &ctx->active_reqs) {
1332 struct kiocb *kiocb = list_kiocb(pos);
1333 if (kiocb->ki_obj.user == iocb)
1334 return kiocb;
1335 }
1336 return NULL;
1337 }
1338
1339 /* sys_io_cancel:
1340 * Attempts to cancel an iocb previously passed to io_submit. If
1341 * the operation is successfully cancelled, the resulting event is
1342 * copied into the memory pointed to by result without being placed
1343 * into the completion queue and 0 is returned. May fail with
1344 * -EFAULT if any of the data structures pointed to are invalid.
1345 * May fail with -EINVAL if aio_context specified by ctx_id is
1346 * invalid. May fail with -EAGAIN if the iocb specified was not
1347 * cancelled. Will fail with -ENOSYS if not implemented.
1348 */
1349 SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
1350 struct io_event __user *, result)
1351 {
1352 struct io_event res;
1353 struct kioctx *ctx;
1354 struct kiocb *kiocb;
1355 u32 key;
1356 int ret;
1357
1358 ret = get_user(key, &iocb->aio_key);
1359 if (unlikely(ret))
1360 return -EFAULT;
1361
1362 ctx = lookup_ioctx(ctx_id);
1363 if (unlikely(!ctx))
1364 return -EINVAL;
1365
1366 spin_lock_irq(&ctx->ctx_lock);
1367
1368 kiocb = lookup_kiocb(ctx, iocb, key);
1369 if (kiocb)
1370 ret = kiocb_cancel(ctx, kiocb, &res);
1371 else
1372 ret = -EINVAL;
1373
1374 spin_unlock_irq(&ctx->ctx_lock);
1375
1376 if (!ret) {
1377 /* Cancellation succeeded -- copy the result
1378 * into the user's buffer.
1379 */
1380 if (copy_to_user(result, &res, sizeof(res)))
1381 ret = -EFAULT;
1382 }
1383
1384 put_ioctx(ctx);
1385
1386 return ret;
1387 }
1388
1389 /* io_getevents:
1390 * Attempts to read at least min_nr events and up to nr events from
1391 * the completion queue for the aio_context specified by ctx_id. If
1392 * it succeeds, the number of read events is returned. May fail with
1393 * -EINVAL if ctx_id is invalid, if min_nr is out of range, if nr is
1394 * out of range, if timeout is out of range. May fail with -EFAULT
1395 * if any of the memory specified is invalid. May return 0 or
1396 * < min_nr if the timeout specified by timeout has elapsed
1397 * before sufficient events are available, where timeout == NULL
1398 * specifies an infinite timeout. Note that the timeout pointed to by
1399 * timeout is relative. Will fail with -ENOSYS if not implemented.
1400 */
1401 SYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id,
1402 long, min_nr,
1403 long, nr,
1404 struct io_event __user *, events,
1405 struct timespec __user *, timeout)
1406 {
1407 struct kioctx *ioctx = lookup_ioctx(ctx_id);
1408 long ret = -EINVAL;
1409
1410 if (likely(ioctx)) {
1411 if (likely(min_nr <= nr && min_nr >= 0))
1412 ret = read_events(ioctx, min_nr, nr, events, timeout);
1413 put_ioctx(ioctx);
1414 }
1415 return ret;
1416 }