blob: eed54507c4e99ab354ca2f0639157645aa66ad49 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/errno.h>
14#include <linux/time.h>
15#include <linux/aio_abi.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050016#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/syscalls.h>
Jens Axboeb9d128f2009-10-29 13:59:26 +010018#include <linux/backing-dev.h>
Badari Pulavarty027445c2006-09-30 23:28:46 -070019#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#define DEBUG 0
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>
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070028#include <linux/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#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>
Davide Libenzi9c3060b2007-05-10 22:23:21 -070035#include <linux/eventfd.h>
Jeff Moyercfb1e332009-10-02 18:57:36 -040036#include <linux/blkdev.h>
Jeff Moyer9d85cba2010-05-26 14:44:26 -070037#include <linux/compat.h>
Nick Desaulniers9dfa3772016-11-18 10:44:16 -080038#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/kmap_types.h>
41#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#if DEBUG > 1
44#define dprintk printk
45#else
46#define dprintk(x...) do { ; } while (0)
47#endif
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*------ sysctl variables----*/
Zach Brownd55b5fd2005-11-07 00:59:31 -080050static DEFINE_SPINLOCK(aio_nr_lock);
51unsigned long aio_nr; /* current system wide number of aio requests */
52unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*----end sysctl variables---*/
54
Christoph Lametere18b8902006-12-06 20:33:20 -080055static struct kmem_cache *kiocb_cachep;
56static struct kmem_cache *kioctx_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58static struct workqueue_struct *aio_wq;
59
60/* Used for rare fput completion. */
David Howells65f27f32006-11-22 14:55:48 +000061static void aio_fput_routine(struct work_struct *);
62static DECLARE_WORK(fput_work, aio_fput_routine);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64static DEFINE_SPINLOCK(fput_lock);
Adrian Bunk25ee7e32005-04-25 08:18:14 -070065static LIST_HEAD(fput_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
David Howells65f27f32006-11-22 14:55:48 +000067static void aio_kick_handler(struct work_struct *);
Sébastien Duguc016e222005-06-28 20:44:59 -070068static void aio_queue_work(struct kioctx *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/* aio_setup
71 * Creates the slab caches used by the aio routines, panic on
72 * failure as this is done early during the boot sequence.
73 */
74static int __init aio_setup(void)
75{
Christoph Lameter0a31bd52007-05-06 14:49:57 -070076 kiocb_cachep = KMEM_CACHE(kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
77 kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Tejun Heod37adaa2011-01-26 17:42:27 +010079 aio_wq = alloc_workqueue("aio", 0, 1); /* used to limit concurrency */
Jens Axboecf159002011-03-02 20:12:18 -050080 BUG_ON(!aio_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 pr_debug("aio_setup: sizeof(struct page) = %d\n", (int)sizeof(struct page));
83
84 return 0;
85}
H Hartley Sweeten385773e2009-09-22 16:43:53 -070086__initcall(aio_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88static void aio_free_ring(struct kioctx *ctx)
89{
90 struct aio_ring_info *info = &ctx->ring_info;
91 long i;
92
93 for (i=0; i<info->nr_pages; i++)
94 put_page(info->ring_pages[i]);
95
Al Viro936af152012-04-20 21:49:41 -040096 if (info->mmap_size) {
97 BUG_ON(ctx->mm != current->mm);
Al Virobfce2812012-04-20 21:57:04 -040098 vm_munmap(info->mmap_base, info->mmap_size);
Al Viro936af152012-04-20 21:49:41 -040099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 if (info->ring_pages && info->ring_pages != info->internal_pages)
102 kfree(info->ring_pages);
103 info->ring_pages = NULL;
104 info->nr = 0;
105}
106
107static int aio_setup_ring(struct kioctx *ctx)
108{
109 struct aio_ring *ring;
110 struct aio_ring_info *info = &ctx->ring_info;
111 unsigned nr_events = ctx->max_reqs;
112 unsigned long size;
113 int nr_pages;
114
Nick Desaulniers9dfa3772016-11-18 10:44:16 -0800115 if (current->personality & READ_IMPLIES_EXEC)
116 return -EPERM;
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 /* Compensate for the ring buffer's head/tail overlap entry */
119 nr_events += 2; /* 1 is required, 2 for good luck */
120
121 size = sizeof(struct aio_ring);
122 size += sizeof(struct io_event) * nr_events;
123 nr_pages = (size + PAGE_SIZE-1) >> PAGE_SHIFT;
124
125 if (nr_pages < 0)
126 return -EINVAL;
127
128 nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring)) / sizeof(struct io_event);
129
130 info->nr = 0;
131 info->ring_pages = info->internal_pages;
132 if (nr_pages > AIO_RING_PAGES) {
Oliver Neukum11b0b5a2006-03-25 03:08:13 -0800133 info->ring_pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (!info->ring_pages)
135 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137
138 info->mmap_size = nr_pages * PAGE_SIZE;
139 dprintk("attempting mmap of %lu bytes\n", info->mmap_size);
140 down_write(&ctx->mm->mmap_sem);
141 info->mmap_base = do_mmap(NULL, 0, info->mmap_size,
Robert P. J. Daye10a4432007-02-10 01:42:56 -0800142 PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 0);
144 if (IS_ERR((void *)info->mmap_base)) {
145 up_write(&ctx->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 info->mmap_size = 0;
147 aio_free_ring(ctx);
148 return -EAGAIN;
149 }
150
151 dprintk("mmap address: 0x%08lx\n", info->mmap_base);
152 info->nr_pages = get_user_pages(current, ctx->mm,
153 info->mmap_base, nr_pages,
154 1, 0, info->ring_pages, NULL);
155 up_write(&ctx->mm->mmap_sem);
156
157 if (unlikely(info->nr_pages != nr_pages)) {
158 aio_free_ring(ctx);
159 return -EAGAIN;
160 }
161
162 ctx->user_id = info->mmap_base;
163
164 info->nr = nr_events; /* trusted copy */
165
Cong Wange8e3c3d2011-11-25 23:14:27 +0800166 ring = kmap_atomic(info->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 ring->nr = nr_events; /* user copy */
168 ring->id = ctx->user_id;
169 ring->head = ring->tail = 0;
170 ring->magic = AIO_RING_MAGIC;
171 ring->compat_features = AIO_RING_COMPAT_FEATURES;
172 ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
173 ring->header_length = sizeof(struct aio_ring);
Cong Wange8e3c3d2011-11-25 23:14:27 +0800174 kunmap_atomic(ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 return 0;
177}
178
179
180/* aio_ring_event: returns a pointer to the event at the given index from
Cong Wange8e3c3d2011-11-25 23:14:27 +0800181 * kmap_atomic(). Release the pointer with put_aio_ring_event();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 */
183#define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
184#define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
185#define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
186
Cong Wange8e3c3d2011-11-25 23:14:27 +0800187#define aio_ring_event(info, nr) ({ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 unsigned pos = (nr) + AIO_EVENTS_OFFSET; \
189 struct io_event *__event; \
190 __event = kmap_atomic( \
Cong Wange8e3c3d2011-11-25 23:14:27 +0800191 (info)->ring_pages[pos / AIO_EVENTS_PER_PAGE]); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 __event += pos % AIO_EVENTS_PER_PAGE; \
193 __event; \
194})
195
Cong Wange8e3c3d2011-11-25 23:14:27 +0800196#define put_aio_ring_event(event) do { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 struct io_event *__event = (event); \
198 (void)__event; \
Cong Wange8e3c3d2011-11-25 23:14:27 +0800199 kunmap_atomic((void *)((unsigned long)__event & PAGE_MASK)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200} while(0)
201
Jens Axboeabf137d2008-12-09 08:11:22 +0100202static void ctx_rcu_free(struct rcu_head *head)
Adrian Bunkd5470b52008-04-29 00:58:57 -0700203{
Jens Axboeabf137d2008-12-09 08:11:22 +0100204 struct kioctx *ctx = container_of(head, struct kioctx, rcu_head);
Adrian Bunkd5470b52008-04-29 00:58:57 -0700205 kmem_cache_free(kioctx_cachep, ctx);
Adrian Bunkd5470b52008-04-29 00:58:57 -0700206}
207
Jens Axboeabf137d2008-12-09 08:11:22 +0100208/* __put_ioctx
209 * Called when the last user of an aio context has gone away,
210 * and the struct needs to be freed.
211 */
212static void __put_ioctx(struct kioctx *ctx)
213{
Al Viro2dd542b2012-03-10 23:10:35 -0500214 unsigned nr_events = ctx->max_reqs;
Jens Axboeabf137d2008-12-09 08:11:22 +0100215 BUG_ON(ctx->reqs_active);
216
Al Virobf507222012-03-11 00:58:40 -0500217 cancel_delayed_work_sync(&ctx->wq);
Jens Axboeabf137d2008-12-09 08:11:22 +0100218 aio_free_ring(ctx);
219 mmdrop(ctx->mm);
220 ctx->mm = NULL;
Al Viro2dd542b2012-03-10 23:10:35 -0500221 if (nr_events) {
222 spin_lock(&aio_nr_lock);
223 BUG_ON(aio_nr - nr_events > aio_nr);
224 aio_nr -= nr_events;
225 spin_unlock(&aio_nr_lock);
226 }
Jens Axboeabf137d2008-12-09 08:11:22 +0100227 pr_debug("__put_ioctx: freeing %p\n", ctx);
228 call_rcu(&ctx->rcu_head, ctx_rcu_free);
229}
230
Nick Piggin3bd9a5d2011-02-25 14:44:26 -0800231static inline int try_get_ioctx(struct kioctx *kioctx)
232{
233 return atomic_inc_not_zero(&kioctx->users);
234}
235
236static inline void put_ioctx(struct kioctx *kioctx)
237{
238 BUG_ON(atomic_read(&kioctx->users) <= 0);
239 if (unlikely(atomic_dec_and_test(&kioctx->users)))
240 __put_ioctx(kioctx);
241}
Adrian Bunkd5470b52008-04-29 00:58:57 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/* ioctx_alloc
244 * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
245 */
246static struct kioctx *ioctx_alloc(unsigned nr_events)
247{
248 struct mm_struct *mm;
249 struct kioctx *ctx;
Al Viroe23754f2012-03-06 14:33:22 -0500250 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /* Prevent overflows */
253 if ((nr_events > (0x10000000U / sizeof(struct io_event))) ||
254 (nr_events > (0x10000000U / sizeof(struct kiocb)))) {
255 pr_debug("ENOMEM: nr_events too high\n");
256 return ERR_PTR(-EINVAL);
257 }
258
Al Viro2dd542b2012-03-10 23:10:35 -0500259 if (!nr_events || (unsigned long)nr_events > aio_max_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return ERR_PTR(-EAGAIN);
261
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800262 ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (!ctx)
264 return ERR_PTR(-ENOMEM);
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 ctx->max_reqs = nr_events;
267 mm = ctx->mm = current->mm;
268 atomic_inc(&mm->mm_count);
269
Al Viro86b62a22012-03-07 05:16:35 +0000270 atomic_set(&ctx->users, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 spin_lock_init(&ctx->ctx_lock);
272 spin_lock_init(&ctx->ring_info.ring_lock);
273 init_waitqueue_head(&ctx->wait);
274
275 INIT_LIST_HEAD(&ctx->active_reqs);
276 INIT_LIST_HEAD(&ctx->run_list);
David Howells65f27f32006-11-22 14:55:48 +0000277 INIT_DELAYED_WORK(&ctx->wq, aio_kick_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (aio_setup_ring(ctx) < 0)
280 goto out_freectx;
281
282 /* limit the number of system wide aios */
Al Viro9fa1cb32012-03-10 23:14:05 -0500283 spin_lock(&aio_nr_lock);
Al Viro2dd542b2012-03-10 23:10:35 -0500284 if (aio_nr + nr_events > aio_max_nr ||
285 aio_nr + nr_events < aio_nr) {
Al Viro9fa1cb32012-03-10 23:14:05 -0500286 spin_unlock(&aio_nr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 goto out_cleanup;
Al Viro2dd542b2012-03-10 23:10:35 -0500288 }
289 aio_nr += ctx->max_reqs;
Al Viro9fa1cb32012-03-10 23:14:05 -0500290 spin_unlock(&aio_nr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Jeff Moyer39fa0032008-04-29 01:03:48 -0700292 /* now link into global list. */
Jens Axboeabf137d2008-12-09 08:11:22 +0100293 spin_lock(&mm->ioctx_lock);
294 hlist_add_head_rcu(&ctx->list, &mm->ioctx_list);
295 spin_unlock(&mm->ioctx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 dprintk("aio: allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
298 ctx, ctx->user_id, current->mm, ctx->ring_info.nr);
299 return ctx;
300
301out_cleanup:
Al Viroe23754f2012-03-06 14:33:22 -0500302 err = -EAGAIN;
303 aio_free_ring(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304out_freectx:
305 mmdrop(mm);
306 kmem_cache_free(kioctx_cachep, ctx);
Al Viroe23754f2012-03-06 14:33:22 -0500307 dprintk("aio: error allocating ioctx %d\n", err);
308 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
Al Viro06af1212012-03-20 16:26:24 -0400311/* kill_ctx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 * Cancels all outstanding aio requests on an aio context. Used
313 * when the processes owning a context have all exited to encourage
314 * the rapid destruction of the kioctx.
315 */
Al Viro06af1212012-03-20 16:26:24 -0400316static void kill_ctx(struct kioctx *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 int (*cancel)(struct kiocb *, struct io_event *);
Al Viro06af1212012-03-20 16:26:24 -0400319 struct task_struct *tsk = current;
320 DECLARE_WAITQUEUE(wait, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 struct io_event res;
Al Viro06af1212012-03-20 16:26:24 -0400322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 spin_lock_irq(&ctx->ctx_lock);
324 ctx->dead = 1;
325 while (!list_empty(&ctx->active_reqs)) {
326 struct list_head *pos = ctx->active_reqs.next;
327 struct kiocb *iocb = list_kiocb(pos);
328 list_del_init(&iocb->ki_list);
329 cancel = iocb->ki_cancel;
330 kiocbSetCancelled(iocb);
331 if (cancel) {
332 iocb->ki_users++;
333 spin_unlock_irq(&ctx->ctx_lock);
334 cancel(iocb, &res);
335 spin_lock_irq(&ctx->ctx_lock);
336 }
337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (!ctx->reqs_active)
Ken Chendee11c22007-02-03 01:13:45 -0800340 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 add_wait_queue(&ctx->wait, &wait);
343 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
344 while (ctx->reqs_active) {
Ken Chendee11c22007-02-03 01:13:45 -0800345 spin_unlock_irq(&ctx->ctx_lock);
Jeff Moyer41d10da2007-10-16 23:27:20 -0700346 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
Ken Chendee11c22007-02-03 01:13:45 -0800348 spin_lock_irq(&ctx->ctx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350 __set_task_state(tsk, TASK_RUNNING);
351 remove_wait_queue(&ctx->wait, &wait);
Ken Chendee11c22007-02-03 01:13:45 -0800352
353out:
354 spin_unlock_irq(&ctx->ctx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357/* wait_on_sync_kiocb:
358 * Waits on the given sync kiocb to complete.
359 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800360ssize_t wait_on_sync_kiocb(struct kiocb *iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 while (iocb->ki_users) {
363 set_current_state(TASK_UNINTERRUPTIBLE);
364 if (!iocb->ki_users)
365 break;
Jeff Moyer41d10da2007-10-16 23:27:20 -0700366 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368 __set_current_state(TASK_RUNNING);
369 return iocb->ki_user_data;
370}
H Hartley Sweeten385773e2009-09-22 16:43:53 -0700371EXPORT_SYMBOL(wait_on_sync_kiocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373/* exit_aio: called when the last user of mm goes away. At this point,
374 * there is no way for any new requests to be submited or any of the
375 * io_* syscalls to be called on the context. However, there may be
376 * outstanding requests which hold references to the context; as they
377 * go away, they will call put_ioctx and release any pinned memory
378 * associated with the request (held via struct page * references).
379 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800380void exit_aio(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Jens Axboeabf137d2008-12-09 08:11:22 +0100382 struct kioctx *ctx;
383
384 while (!hlist_empty(&mm->ioctx_list)) {
385 ctx = hlist_entry(mm->ioctx_list.first, struct kioctx, list);
386 hlist_del_rcu(&ctx->list);
387
Al Viro06af1212012-03-20 16:26:24 -0400388 kill_ctx(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 if (1 != atomic_read(&ctx->users))
391 printk(KERN_DEBUG
392 "exit_aio:ioctx still alive: %d %d %d\n",
393 atomic_read(&ctx->users), ctx->dead,
394 ctx->reqs_active);
Al Viro936af152012-04-20 21:49:41 -0400395 /*
396 * We don't need to bother with munmap() here -
397 * exit_mmap(mm) is coming and it'll unmap everything.
398 * Since aio_free_ring() uses non-zero ->mmap_size
399 * as indicator that it needs to unmap the area,
400 * just set it to 0; aio_free_ring() is the only
401 * place that uses ->mmap_size, so it's safe.
402 * That way we get all munmap done to current->mm -
403 * all other callers have ctx->mm == current->mm.
404 */
405 ctx->ring_info.mmap_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 put_ioctx(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/* aio_get_req
411 * Allocate a slot for an aio request. Increments the users count
412 * of the kioctx so that the kioctx stays around until all requests are
413 * complete. Returns NULL if no requests are free.
414 *
415 * Returns with kiocb->users set to 2. The io submit code path holds
416 * an extra reference while submitting the i/o.
417 * This prevents races between the aio code path referencing the
418 * req (after submitting it) and aio_complete() freeing the req.
419 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800420static struct kiocb *__aio_get_req(struct kioctx *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 struct kiocb *req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL);
425 if (unlikely(!req))
426 return NULL;
427
Zach Brown4faa5282005-10-17 16:43:33 -0700428 req->ki_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 req->ki_users = 2;
430 req->ki_key = 0;
431 req->ki_ctx = ctx;
432 req->ki_cancel = NULL;
433 req->ki_retry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 req->ki_dtor = NULL;
435 req->private = NULL;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700436 req->ki_iovec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 INIT_LIST_HEAD(&req->ki_run_list);
Davide Libenzi87c3a862009-03-18 17:04:19 -0700438 req->ki_eventfd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return req;
441}
442
Jeff Moyer080d6762011-11-02 13:40:10 -0700443/*
444 * struct kiocb's are allocated in batches to reduce the number of
445 * times the ctx lock is acquired and released.
446 */
447#define KIOCB_BATCH_SIZE 32L
448struct kiocb_batch {
449 struct list_head head;
450 long count; /* number of requests left to allocate */
451};
452
453static void kiocb_batch_init(struct kiocb_batch *batch, long total)
454{
455 INIT_LIST_HEAD(&batch->head);
456 batch->count = total;
457}
458
Gleb Natapov69e47472012-01-08 17:07:28 +0200459static void kiocb_batch_free(struct kioctx *ctx, struct kiocb_batch *batch)
Jeff Moyer080d6762011-11-02 13:40:10 -0700460{
461 struct kiocb *req, *n;
462
Gleb Natapov69e47472012-01-08 17:07:28 +0200463 if (list_empty(&batch->head))
464 return;
465
466 spin_lock_irq(&ctx->ctx_lock);
Jeff Moyer080d6762011-11-02 13:40:10 -0700467 list_for_each_entry_safe(req, n, &batch->head, ki_batch) {
468 list_del(&req->ki_batch);
Gleb Natapov69e47472012-01-08 17:07:28 +0200469 list_del(&req->ki_list);
Jeff Moyer080d6762011-11-02 13:40:10 -0700470 kmem_cache_free(kiocb_cachep, req);
Gleb Natapov69e47472012-01-08 17:07:28 +0200471 ctx->reqs_active--;
Jeff Moyer080d6762011-11-02 13:40:10 -0700472 }
Jeff Moyer880641b2012-03-05 14:59:12 -0800473 if (unlikely(!ctx->reqs_active && ctx->dead))
474 wake_up_all(&ctx->wait);
Gleb Natapov69e47472012-01-08 17:07:28 +0200475 spin_unlock_irq(&ctx->ctx_lock);
Jeff Moyer080d6762011-11-02 13:40:10 -0700476}
477
478/*
479 * Allocate a batch of kiocbs. This avoids taking and dropping the
480 * context lock a lot during setup.
481 */
482static int kiocb_batch_refill(struct kioctx *ctx, struct kiocb_batch *batch)
483{
484 unsigned short allocated, to_alloc;
485 long avail;
486 bool called_fput = false;
487 struct kiocb *req, *n;
488 struct aio_ring *ring;
489
490 to_alloc = min(batch->count, KIOCB_BATCH_SIZE);
491 for (allocated = 0; allocated < to_alloc; allocated++) {
492 req = __aio_get_req(ctx);
493 if (!req)
494 /* allocation failed, go with what we've got */
495 break;
496 list_add(&req->ki_batch, &batch->head);
497 }
498
499 if (allocated == 0)
500 goto out;
501
502retry:
503 spin_lock_irq(&ctx->ctx_lock);
504 ring = kmap_atomic(ctx->ring_info.ring_pages[0]);
505
506 avail = aio_ring_avail(&ctx->ring_info, ring) - ctx->reqs_active;
507 BUG_ON(avail < 0);
508 if (avail == 0 && !called_fput) {
509 /*
510 * Handle a potential starvation case. It is possible that
511 * we hold the last reference on a struct file, causing us
512 * to delay the final fput to non-irq context. In this case,
513 * ctx->reqs_active is artificially high. Calling the fput
514 * routine here may free up a slot in the event completion
515 * ring, allowing this allocation to succeed.
516 */
517 kunmap_atomic(ring);
518 spin_unlock_irq(&ctx->ctx_lock);
519 aio_fput_routine(NULL);
520 called_fput = true;
521 goto retry;
522 }
523
524 if (avail < allocated) {
525 /* Trim back the number of requests. */
526 list_for_each_entry_safe(req, n, &batch->head, ki_batch) {
527 list_del(&req->ki_batch);
528 kmem_cache_free(kiocb_cachep, req);
529 if (--allocated <= avail)
530 break;
531 }
532 }
533
534 batch->count -= allocated;
535 list_for_each_entry(req, &batch->head, ki_batch) {
536 list_add(&req->ki_list, &ctx->active_reqs);
537 ctx->reqs_active++;
538 }
539
540 kunmap_atomic(ring);
541 spin_unlock_irq(&ctx->ctx_lock);
542
543out:
544 return allocated;
545}
546
547static inline struct kiocb *aio_get_req(struct kioctx *ctx,
548 struct kiocb_batch *batch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 struct kiocb *req;
Jeff Moyer080d6762011-11-02 13:40:10 -0700551
552 if (list_empty(&batch->head))
553 if (kiocb_batch_refill(ctx, batch) == 0)
554 return NULL;
555 req = list_first_entry(&batch->head, struct kiocb, ki_batch);
556 list_del(&req->ki_batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return req;
558}
559
560static inline void really_put_req(struct kioctx *ctx, struct kiocb *req)
561{
Zach Brownd00689a2005-11-13 16:07:34 -0800562 assert_spin_locked(&ctx->ctx_lock);
563
Davide Libenzi13389012009-06-30 11:41:11 -0700564 if (req->ki_eventfd != NULL)
565 eventfd_ctx_put(req->ki_eventfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (req->ki_dtor)
567 req->ki_dtor(req);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700568 if (req->ki_iovec != &req->ki_inline_vec)
569 kfree(req->ki_iovec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 kmem_cache_free(kiocb_cachep, req);
571 ctx->reqs_active--;
572
573 if (unlikely(!ctx->reqs_active && ctx->dead))
Roland Dreiere91f90b2011-03-22 16:35:10 -0700574 wake_up_all(&ctx->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
David Howells65f27f32006-11-22 14:55:48 +0000577static void aio_fput_routine(struct work_struct *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 spin_lock_irq(&fput_lock);
580 while (likely(!list_empty(&fput_head))) {
581 struct kiocb *req = list_kiocb(fput_head.next);
582 struct kioctx *ctx = req->ki_ctx;
583
584 list_del(&req->ki_list);
585 spin_unlock_irq(&fput_lock);
586
Davide Libenzi87c3a862009-03-18 17:04:19 -0700587 /* Complete the fput(s) */
588 if (req->ki_filp != NULL)
Al Virod7065da2010-05-26 15:13:55 -0400589 fput(req->ki_filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 /* Link the iocb into the context's free list */
Al Viroc7b28552012-03-08 17:51:19 +0000592 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 spin_lock_irq(&ctx->ctx_lock);
594 really_put_req(ctx, req);
Al Viroc7b28552012-03-08 17:51:19 +0000595 /*
596 * at that point ctx might've been killed, but actual
597 * freeing is RCU'd
598 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 spin_unlock_irq(&ctx->ctx_lock);
Al Viroc7b28552012-03-08 17:51:19 +0000600 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 spin_lock_irq(&fput_lock);
603 }
604 spin_unlock_irq(&fput_lock);
605}
606
607/* __aio_put_req
608 * Returns true if this put was the last user of the request.
609 */
610static int __aio_put_req(struct kioctx *ctx, struct kiocb *req)
611{
Al Viro516e0cc2008-07-26 00:39:17 -0400612 dprintk(KERN_DEBUG "aio_put(%p): f_count=%ld\n",
613 req, atomic_long_read(&req->ki_filp->f_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Zach Brownd00689a2005-11-13 16:07:34 -0800615 assert_spin_locked(&ctx->ctx_lock);
616
Davide Libenzi87c3a862009-03-18 17:04:19 -0700617 req->ki_users--;
Eric Sesterhenn93e06b42006-11-30 05:29:23 +0100618 BUG_ON(req->ki_users < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (likely(req->ki_users))
620 return 0;
621 list_del(&req->ki_list); /* remove from active_reqs */
622 req->ki_cancel = NULL;
623 req->ki_retry = NULL;
624
Davide Libenzi87c3a862009-03-18 17:04:19 -0700625 /*
626 * Try to optimize the aio and eventfd file* puts, by avoiding to
Al Virod7065da2010-05-26 15:13:55 -0400627 * schedule work in case it is not final fput() time. In normal cases,
Davide Libenzi87c3a862009-03-18 17:04:19 -0700628 * we would not be holding the last reference to the file*, so
629 * this function will be executed w/out any aio kthread wakeup.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 */
Al Virod7065da2010-05-26 15:13:55 -0400631 if (unlikely(!fput_atomic(req->ki_filp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 spin_lock(&fput_lock);
633 list_add(&req->ki_list, &fput_head);
634 spin_unlock(&fput_lock);
Tejun Heod37adaa2011-01-26 17:42:27 +0100635 schedule_work(&fput_work);
Davide Libenzi13389012009-06-30 11:41:11 -0700636 } else {
637 req->ki_filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 really_put_req(ctx, req);
Davide Libenzi13389012009-06-30 11:41:11 -0700639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return 1;
641}
642
643/* aio_put_req
644 * Returns true if this put was the last user of the kiocb,
645 * false if the request is still in use.
646 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800647int aio_put_req(struct kiocb *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 struct kioctx *ctx = req->ki_ctx;
650 int ret;
651 spin_lock_irq(&ctx->ctx_lock);
652 ret = __aio_put_req(ctx, req);
653 spin_unlock_irq(&ctx->ctx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return ret;
655}
H Hartley Sweeten385773e2009-09-22 16:43:53 -0700656EXPORT_SYMBOL(aio_put_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Adrian Bunkd5470b52008-04-29 00:58:57 -0700658static struct kioctx *lookup_ioctx(unsigned long ctx_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Jens Axboeabf137d2008-12-09 08:11:22 +0100660 struct mm_struct *mm = current->mm;
Jeff Moyer65c24492009-03-18 17:04:21 -0700661 struct kioctx *ctx, *ret = NULL;
Jens Axboeabf137d2008-12-09 08:11:22 +0100662 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Jens Axboeabf137d2008-12-09 08:11:22 +0100664 rcu_read_lock();
665
666 hlist_for_each_entry_rcu(ctx, n, &mm->ioctx_list, list) {
Nick Piggin3bd9a5d2011-02-25 14:44:26 -0800667 /*
668 * RCU protects us against accessing freed memory but
669 * we have to be careful not to get a reference when the
670 * reference count already dropped to 0 (ctx->dead test
671 * is unreliable because of races).
672 */
673 if (ctx->user_id == ctx_id && !ctx->dead && try_get_ioctx(ctx)){
Jeff Moyer65c24492009-03-18 17:04:21 -0700674 ret = ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 break;
676 }
Jens Axboeabf137d2008-12-09 08:11:22 +0100677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Jens Axboeabf137d2008-12-09 08:11:22 +0100679 rcu_read_unlock();
Jeff Moyer65c24492009-03-18 17:04:21 -0700680 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
683/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 * Queue up a kiocb to be retried. Assumes that the kiocb
685 * has already been marked as kicked, and places it on
686 * the retry run list for the corresponding ioctx, if it
687 * isn't already queued. Returns 1 if it actually queued
688 * the kiocb (to tell the caller to activate the work
689 * queue to process it), or 0, if it found that it was
690 * already queued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 */
692static inline int __queue_kicked_iocb(struct kiocb *iocb)
693{
694 struct kioctx *ctx = iocb->ki_ctx;
695
Zach Brownd00689a2005-11-13 16:07:34 -0800696 assert_spin_locked(&ctx->ctx_lock);
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (list_empty(&iocb->ki_run_list)) {
699 list_add_tail(&iocb->ki_run_list,
700 &ctx->run_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return 1;
702 }
703 return 0;
704}
705
706/* aio_run_iocb
707 * This is the core aio execution routine. It is
708 * invoked both for initial i/o submission and
709 * subsequent retries via the aio_kick_handler.
710 * Expects to be invoked with iocb->ki_ctx->lock
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200711 * already held. The lock is released and reacquired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 * as needed during processing.
713 *
714 * Calls the iocb retry method (already setup for the
715 * iocb on initial submission) for operation specific
716 * handling, but takes care of most of common retry
717 * execution details for a given iocb. The retry method
718 * needs to be non-blocking as far as possible, to avoid
719 * holding up other iocbs waiting to be serviced by the
720 * retry kernel thread.
721 *
722 * The trickier parts in this code have to do with
723 * ensuring that only one retry instance is in progress
724 * for a given iocb at any time. Providing that guarantee
725 * simplifies the coding of individual aio operations as
726 * it avoids various potential races.
727 */
728static ssize_t aio_run_iocb(struct kiocb *iocb)
729{
730 struct kioctx *ctx = iocb->ki_ctx;
731 ssize_t (*retry)(struct kiocb *);
732 ssize_t ret;
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (!(retry = iocb->ki_retry)) {
735 printk("aio_run_iocb: iocb->ki_retry = NULL\n");
736 return 0;
737 }
738
739 /*
740 * We don't want the next retry iteration for this
741 * operation to start until this one has returned and
742 * updated the iocb state. However, wait_queue functions
743 * can trigger a kick_iocb from interrupt context in the
744 * meantime, indicating that data is available for the next
745 * iteration. We want to remember that and enable the
746 * next retry iteration _after_ we are through with
747 * this one.
748 *
749 * So, in order to be able to register a "kick", but
750 * prevent it from being queued now, we clear the kick
751 * flag, but make the kick code *think* that the iocb is
752 * still on the run list until we are actually done.
753 * When we are done with this iteration, we check if
754 * the iocb was kicked in the meantime and if so, queue
755 * it up afresh.
756 */
757
758 kiocbClearKicked(iocb);
759
760 /*
761 * This is so that aio_complete knows it doesn't need to
762 * pull the iocb off the run list (We can't just call
763 * INIT_LIST_HEAD because we don't want a kick_iocb to
764 * queue this on the run list yet)
765 */
766 iocb->ki_run_list.next = iocb->ki_run_list.prev = NULL;
767 spin_unlock_irq(&ctx->ctx_lock);
768
769 /* Quit retrying if the i/o has been cancelled */
770 if (kiocbIsCancelled(iocb)) {
771 ret = -EINTR;
772 aio_complete(iocb, ret, 0);
773 /* must not access the iocb after this */
774 goto out;
775 }
776
777 /*
778 * Now we are all set to call the retry method in async
Alexey Dobriyan6212e3a2007-10-18 03:04:56 -0700779 * context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 ret = retry(iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Jan Karaa0c42ba2010-09-22 13:05:03 -0700783 if (ret != -EIOCBRETRY && ret != -EIOCBQUEUED) {
784 /*
785 * There's no easy way to restart the syscall since other AIO's
786 * may be already running. Just fail this IO with EINTR.
787 */
788 if (unlikely(ret == -ERESTARTSYS || ret == -ERESTARTNOINTR ||
789 ret == -ERESTARTNOHAND || ret == -ERESTART_RESTARTBLOCK))
790 ret = -EINTR;
Zach Brown897f15f2005-09-30 11:58:55 -0700791 aio_complete(iocb, ret, 0);
Jan Karaa0c42ba2010-09-22 13:05:03 -0700792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793out:
794 spin_lock_irq(&ctx->ctx_lock);
795
796 if (-EIOCBRETRY == ret) {
797 /*
798 * OK, now that we are done with this iteration
799 * and know that there is more left to go,
800 * this is where we let go so that a subsequent
801 * "kick" can start the next iteration
802 */
803
804 /* will make __queue_kicked_iocb succeed from here on */
805 INIT_LIST_HEAD(&iocb->ki_run_list);
806 /* we must queue the next iteration ourselves, if it
807 * has already been kicked */
808 if (kiocbIsKicked(iocb)) {
809 __queue_kicked_iocb(iocb);
Sébastien Duguc016e222005-06-28 20:44:59 -0700810
811 /*
812 * __queue_kicked_iocb will always return 1 here, because
813 * iocb->ki_run_list is empty at this point so it should
814 * be safe to unconditionally queue the context into the
815 * work queue.
816 */
817 aio_queue_work(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819 }
820 return ret;
821}
822
823/*
824 * __aio_run_iocbs:
825 * Process all pending retries queued on the ioctx
826 * run list.
827 * Assumes it is operating within the aio issuer's mm
Zach Brownd00689a2005-11-13 16:07:34 -0800828 * context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 */
830static int __aio_run_iocbs(struct kioctx *ctx)
831{
832 struct kiocb *iocb;
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700833 struct list_head run_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Zach Brownd00689a2005-11-13 16:07:34 -0800835 assert_spin_locked(&ctx->ctx_lock);
836
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700837 list_replace_init(&ctx->run_list, &run_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 while (!list_empty(&run_list)) {
839 iocb = list_entry(run_list.next, struct kiocb,
840 ki_run_list);
841 list_del(&iocb->ki_run_list);
842 /*
843 * Hold an extra reference while retrying i/o.
844 */
845 iocb->ki_users++; /* grab extra reference */
846 aio_run_iocb(iocb);
Ken Chendee11c22007-02-03 01:13:45 -0800847 __aio_put_req(ctx, iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (!list_empty(&ctx->run_list))
850 return 1;
851 return 0;
852}
853
854static void aio_queue_work(struct kioctx * ctx)
855{
856 unsigned long timeout;
857 /*
858 * if someone is waiting, get the work started right
859 * away, otherwise, use a longer delay
860 */
861 smp_mb();
862 if (waitqueue_active(&ctx->wait))
863 timeout = 1;
864 else
865 timeout = HZ/10;
866 queue_delayed_work(aio_wq, &ctx->wq, timeout);
867}
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869/*
Jeff Moyerd3486f82011-01-12 17:01:08 -0800870 * aio_run_all_iocbs:
871 * Process all pending retries queued on the ioctx
872 * run list, and keep running them until the list
873 * stays empty.
874 * Assumes it is operating within the aio issuer's mm context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 */
876static inline void aio_run_all_iocbs(struct kioctx *ctx)
877{
878 spin_lock_irq(&ctx->ctx_lock);
879 while (__aio_run_iocbs(ctx))
880 ;
881 spin_unlock_irq(&ctx->ctx_lock);
882}
883
884/*
885 * aio_kick_handler:
886 * Work queue handler triggered to process pending
887 * retries on an ioctx. Takes on the aio issuer's
888 * mm context before running the iocbs, so that
889 * copy_xxx_user operates on the issuer's address
890 * space.
891 * Run on aiod's context.
892 */
David Howells65f27f32006-11-22 14:55:48 +0000893static void aio_kick_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
David Howells65f27f32006-11-22 14:55:48 +0000895 struct kioctx *ctx = container_of(work, struct kioctx, wq.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 mm_segment_t oldfs = get_fs();
Zach Brown1ebb1102006-12-29 16:47:02 -0800897 struct mm_struct *mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 int requeue;
899
900 set_fs(USER_DS);
901 use_mm(ctx->mm);
902 spin_lock_irq(&ctx->ctx_lock);
903 requeue =__aio_run_iocbs(ctx);
Zach Brown1ebb1102006-12-29 16:47:02 -0800904 mm = ctx->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 spin_unlock_irq(&ctx->ctx_lock);
Zach Brown1ebb1102006-12-29 16:47:02 -0800906 unuse_mm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 set_fs(oldfs);
908 /*
Al Viro9fcf03d2012-03-13 22:06:28 -0400909 * we're in a worker thread already; no point using non-zero delay
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 */
911 if (requeue)
David Howells52bad642006-11-22 14:54:01 +0000912 queue_delayed_work(aio_wq, &ctx->wq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915
916/*
917 * Called by kick_iocb to queue the kiocb for retry
918 * and if required activate the aio work queue to process
919 * it
920 */
Zach Brown998765e2005-09-30 11:58:54 -0700921static void try_queue_kicked_iocb(struct kiocb *iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 struct kioctx *ctx = iocb->ki_ctx;
924 unsigned long flags;
925 int run = 0;
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 spin_lock_irqsave(&ctx->ctx_lock, flags);
Zach Brown998765e2005-09-30 11:58:54 -0700928 /* set this inside the lock so that we can't race with aio_run_iocb()
929 * testing it and putting the iocb on the run list under the lock */
930 if (!kiocbTryKick(iocb))
931 run = __queue_kicked_iocb(iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
Ken Chen644d3a02005-05-01 08:59:15 -0700933 if (run)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 aio_queue_work(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937/*
938 * kick_iocb:
939 * Called typically from a wait queue callback context
Shaohua Lifac046a2009-12-15 16:47:47 -0800940 * to trigger a retry of the iocb.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 * The retry is usually executed by aio workqueue
942 * threads (See aio_kick_handler).
943 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800944void kick_iocb(struct kiocb *iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
946 /* sync iocbs are easy: they can only ever be executing from a
947 * single context. */
948 if (is_sync_kiocb(iocb)) {
949 kiocbSetKicked(iocb);
950 wake_up_process(iocb->ki_obj.tsk);
951 return;
952 }
953
Zach Brown998765e2005-09-30 11:58:54 -0700954 try_queue_kicked_iocb(iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956EXPORT_SYMBOL(kick_iocb);
957
958/* aio_complete
959 * Called when the io request on the given iocb is complete.
960 * Returns true if this is the last user of the request. The
961 * only other user of the request can be the cancellation code.
962 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800963int aio_complete(struct kiocb *iocb, long res, long res2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 struct kioctx *ctx = iocb->ki_ctx;
966 struct aio_ring_info *info;
967 struct aio_ring *ring;
968 struct io_event *event;
969 unsigned long flags;
970 unsigned long tail;
971 int ret;
972
Zach Brown20dcae32005-11-13 16:07:33 -0800973 /*
974 * Special case handling for sync iocbs:
975 * - events go directly into the iocb for fast handling
976 * - the sync task with the iocb in its stack holds the single iocb
977 * ref, no other paths have a way to get another ref
978 * - the sync task helpfully left a reference to itself in the iocb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 */
980 if (is_sync_kiocb(iocb)) {
Zach Brown20dcae32005-11-13 16:07:33 -0800981 BUG_ON(iocb->ki_users != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 iocb->ki_user_data = res;
Zach Brown20dcae32005-11-13 16:07:33 -0800983 iocb->ki_users = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 wake_up_process(iocb->ki_obj.tsk);
Zach Brown20dcae32005-11-13 16:07:33 -0800985 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987
988 info = &ctx->ring_info;
989
990 /* add a completion event to the ring buffer.
991 * must be done holding ctx->ctx_lock to prevent
992 * other code from messing with the tail
993 * pointer since we might be called from irq
994 * context.
995 */
996 spin_lock_irqsave(&ctx->ctx_lock, flags);
997
998 if (iocb->ki_run_list.prev && !list_empty(&iocb->ki_run_list))
999 list_del_init(&iocb->ki_run_list);
1000
1001 /*
1002 * cancelled requests don't get events, userland was given one
1003 * when the event got cancelled.
1004 */
1005 if (kiocbIsCancelled(iocb))
1006 goto put_rq;
1007
Cong Wange8e3c3d2011-11-25 23:14:27 +08001008 ring = kmap_atomic(info->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 tail = info->tail;
Cong Wange8e3c3d2011-11-25 23:14:27 +08001011 event = aio_ring_event(info, tail);
Ken Chen4bf69b22005-05-01 08:59:15 -07001012 if (++tail >= info->nr)
1013 tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 event->obj = (u64)(unsigned long)iocb->ki_obj.user;
1016 event->data = iocb->ki_user_data;
1017 event->res = res;
1018 event->res2 = res2;
1019
1020 dprintk("aio_complete: %p[%lu]: %p: %p %Lx %lx %lx\n",
1021 ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
1022 res, res2);
1023
1024 /* after flagging the request as done, we
1025 * must never even look at it again
1026 */
1027 smp_wmb(); /* make event visible before updating tail */
1028
1029 info->tail = tail;
1030 ring->tail = tail;
1031
Cong Wange8e3c3d2011-11-25 23:14:27 +08001032 put_aio_ring_event(event);
1033 kunmap_atomic(ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 pr_debug("added to ring %p at [%lu]\n", iocb, tail);
Davide Libenzi8d1c98b2008-04-10 21:29:19 -07001036
1037 /*
1038 * Check if the user asked us to deliver the result through an
1039 * eventfd. The eventfd_signal() function is safe to be called
1040 * from IRQ context.
1041 */
Davide Libenzi87c3a862009-03-18 17:04:19 -07001042 if (iocb->ki_eventfd != NULL)
Davide Libenzi8d1c98b2008-04-10 21:29:19 -07001043 eventfd_signal(iocb->ki_eventfd, 1);
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045put_rq:
1046 /* everything turned out well, dispose of the aiocb. */
1047 ret = __aio_put_req(ctx, iocb);
1048
Quentin Barnes6cb2a212008-03-19 17:00:39 -07001049 /*
1050 * We have to order our ring_info tail store above and test
1051 * of the wait list below outside the wait lock. This is
1052 * like in wake_up_bit() where clearing a bit has to be
1053 * ordered with the unlocked test.
1054 */
1055 smp_mb();
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (waitqueue_active(&ctx->wait))
1058 wake_up(&ctx->wait);
1059
Ken Chendee11c22007-02-03 01:13:45 -08001060 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return ret;
1062}
H Hartley Sweeten385773e2009-09-22 16:43:53 -07001063EXPORT_SYMBOL(aio_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065/* aio_read_evt
1066 * Pull an event off of the ioctx's event ring. Returns the number of
1067 * events fetched (0 or 1 ;-)
1068 * FIXME: make this use cmpxchg.
1069 * TODO: make the ringbuffer user mmap()able (requires FIXME).
1070 */
1071static int aio_read_evt(struct kioctx *ioctx, struct io_event *ent)
1072{
1073 struct aio_ring_info *info = &ioctx->ring_info;
1074 struct aio_ring *ring;
1075 unsigned long head;
1076 int ret = 0;
1077
Cong Wange8e3c3d2011-11-25 23:14:27 +08001078 ring = kmap_atomic(info->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 dprintk("in aio_read_evt h%lu t%lu m%lu\n",
1080 (unsigned long)ring->head, (unsigned long)ring->tail,
1081 (unsigned long)ring->nr);
1082
1083 if (ring->head == ring->tail)
1084 goto out;
1085
1086 spin_lock(&info->ring_lock);
1087
1088 head = ring->head % info->nr;
1089 if (head != ring->tail) {
Cong Wange8e3c3d2011-11-25 23:14:27 +08001090 struct io_event *evp = aio_ring_event(info, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 *ent = *evp;
1092 head = (head + 1) % info->nr;
1093 smp_mb(); /* finish reading the event before updatng the head */
1094 ring->head = head;
1095 ret = 1;
Cong Wange8e3c3d2011-11-25 23:14:27 +08001096 put_aio_ring_event(evp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098 spin_unlock(&info->ring_lock);
1099
1100out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 dprintk("leaving aio_read_evt: %d h%lu t%lu\n", ret,
1102 (unsigned long)ring->head, (unsigned long)ring->tail);
Zhao Hongjiang90445732013-04-26 11:03:53 +08001103 kunmap_atomic(ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return ret;
1105}
1106
1107struct aio_timeout {
1108 struct timer_list timer;
1109 int timed_out;
1110 struct task_struct *p;
1111};
1112
1113static void timeout_func(unsigned long data)
1114{
1115 struct aio_timeout *to = (struct aio_timeout *)data;
1116
1117 to->timed_out = 1;
1118 wake_up_process(to->p);
1119}
1120
1121static inline void init_timeout(struct aio_timeout *to)
1122{
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -07001123 setup_timer_on_stack(&to->timer, timeout_func, (unsigned long) to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 to->timed_out = 0;
1125 to->p = current;
1126}
1127
1128static inline void set_timeout(long start_jiffies, struct aio_timeout *to,
1129 const struct timespec *ts)
1130{
1131 to->timer.expires = start_jiffies + timespec_to_jiffies(ts);
1132 if (time_after(to->timer.expires, jiffies))
1133 add_timer(&to->timer);
1134 else
1135 to->timed_out = 1;
1136}
1137
1138static inline void clear_timeout(struct aio_timeout *to)
1139{
1140 del_singleshot_timer_sync(&to->timer);
1141}
1142
1143static int read_events(struct kioctx *ctx,
1144 long min_nr, long nr,
1145 struct io_event __user *event,
1146 struct timespec __user *timeout)
1147{
1148 long start_jiffies = jiffies;
1149 struct task_struct *tsk = current;
1150 DECLARE_WAITQUEUE(wait, tsk);
1151 int ret;
1152 int i = 0;
1153 struct io_event ent;
1154 struct aio_timeout to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 int retry = 0;
1156
1157 /* needed to zero any padding within an entry (there shouldn't be
1158 * any, but C is fun!
1159 */
1160 memset(&ent, 0, sizeof(ent));
1161retry:
1162 ret = 0;
1163 while (likely(i < nr)) {
1164 ret = aio_read_evt(ctx, &ent);
1165 if (unlikely(ret <= 0))
1166 break;
1167
1168 dprintk("read event: %Lx %Lx %Lx %Lx\n",
1169 ent.data, ent.obj, ent.res, ent.res2);
1170
1171 /* Could we split the check in two? */
1172 ret = -EFAULT;
1173 if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
1174 dprintk("aio: lost an event due to EFAULT.\n");
1175 break;
1176 }
1177 ret = 0;
1178
1179 /* Good, event copied to userland, update counts. */
1180 event ++;
1181 i ++;
1182 }
1183
1184 if (min_nr <= i)
1185 return i;
1186 if (ret)
1187 return ret;
1188
1189 /* End fast path */
1190
1191 /* racey check, but it gets redone */
1192 if (!retry && unlikely(!list_empty(&ctx->run_list))) {
1193 retry = 1;
1194 aio_run_all_iocbs(ctx);
1195 goto retry;
1196 }
1197
1198 init_timeout(&to);
1199 if (timeout) {
1200 struct timespec ts;
1201 ret = -EFAULT;
1202 if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
1203 goto out;
1204
1205 set_timeout(start_jiffies, &to, &ts);
1206 }
1207
1208 while (likely(i < nr)) {
1209 add_wait_queue_exclusive(&ctx->wait, &wait);
1210 do {
1211 set_task_state(tsk, TASK_INTERRUPTIBLE);
1212 ret = aio_read_evt(ctx, &ent);
1213 if (ret)
1214 break;
1215 if (min_nr <= i)
1216 break;
Jeff Moyere92adcb2008-04-28 02:12:04 -07001217 if (unlikely(ctx->dead)) {
1218 ret = -EINVAL;
1219 break;
1220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (to.timed_out) /* Only check after read evt */
1222 break;
Jeff Moyere00ba3d2007-12-04 23:45:02 -08001223 /* Try to only show up in io wait if there are ops
1224 * in flight */
1225 if (ctx->reqs_active)
1226 io_schedule();
1227 else
1228 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (signal_pending(tsk)) {
1230 ret = -EINTR;
1231 break;
1232 }
1233 /*ret = aio_read_evt(ctx, &ent);*/
1234 } while (1) ;
1235
1236 set_task_state(tsk, TASK_RUNNING);
1237 remove_wait_queue(&ctx->wait, &wait);
1238
1239 if (unlikely(ret <= 0))
1240 break;
1241
1242 ret = -EFAULT;
1243 if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
1244 dprintk("aio: lost an event due to EFAULT.\n");
1245 break;
1246 }
1247
1248 /* Good, event copied to userland, update counts. */
1249 event ++;
1250 i ++;
1251 }
1252
1253 if (timeout)
1254 clear_timeout(&to);
1255out:
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -07001256 destroy_timer_on_stack(&to.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return i ? i : ret;
1258}
1259
1260/* Take an ioctx and remove it from the list of ioctx's. Protects
1261 * against races with itself via ->dead.
1262 */
1263static void io_destroy(struct kioctx *ioctx)
1264{
1265 struct mm_struct *mm = current->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 int was_dead;
1267
1268 /* delete the entry from the list is someone else hasn't already */
Jens Axboeabf137d2008-12-09 08:11:22 +01001269 spin_lock(&mm->ioctx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 was_dead = ioctx->dead;
1271 ioctx->dead = 1;
Jens Axboeabf137d2008-12-09 08:11:22 +01001272 hlist_del_rcu(&ioctx->list);
1273 spin_unlock(&mm->ioctx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 dprintk("aio_release(%p)\n", ioctx);
1276 if (likely(!was_dead))
1277 put_ioctx(ioctx); /* twice for the list */
1278
Al Viro06af1212012-03-20 16:26:24 -04001279 kill_ctx(ioctx);
Jeff Moyere92adcb2008-04-28 02:12:04 -07001280
1281 /*
1282 * Wake up any waiters. The setting of ctx->dead must be seen
1283 * by other CPUs at this point. Right now, we rely on the
1284 * locking done by the above calls to ensure this consistency.
1285 */
Roland Dreiere91f90b2011-03-22 16:35:10 -07001286 wake_up_all(&ioctx->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
1289/* sys_io_setup:
1290 * Create an aio_context capable of receiving at least nr_events.
1291 * ctxp must not point to an aio_context that already exists, and
1292 * must be initialized to 0 prior to the call. On successful
1293 * creation of the aio_context, *ctxp is filled in with the resulting
1294 * handle. May fail with -EINVAL if *ctxp is not initialized,
1295 * if the specified nr_events exceeds internal limits. May fail
1296 * with -EAGAIN if the specified nr_events exceeds the user's limit
1297 * of available events. May fail with -ENOMEM if insufficient kernel
1298 * resources are available. May fail with -EFAULT if an invalid
1299 * pointer is passed for ctxp. Will fail with -ENOSYS if not
1300 * implemented.
1301 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001302SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 struct kioctx *ioctx = NULL;
1305 unsigned long ctx;
1306 long ret;
1307
1308 ret = get_user(ctx, ctxp);
1309 if (unlikely(ret))
1310 goto out;
1311
1312 ret = -EINVAL;
Zach Brownd55b5fd2005-11-07 00:59:31 -08001313 if (unlikely(ctx || nr_events == 0)) {
1314 pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
1315 ctx, nr_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 goto out;
1317 }
1318
1319 ioctx = ioctx_alloc(nr_events);
1320 ret = PTR_ERR(ioctx);
1321 if (!IS_ERR(ioctx)) {
1322 ret = put_user(ioctx->user_id, ctxp);
Al Viroa2e18592012-03-20 16:27:57 -04001323 if (ret)
1324 io_destroy(ioctx);
1325 put_ioctx(ioctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327
1328out:
1329 return ret;
1330}
1331
1332/* sys_io_destroy:
1333 * Destroy the aio_context specified. May cancel any outstanding
1334 * AIOs and block on completion. Will fail with -ENOSYS if not
Satoru Takeuchi642b5122010-08-05 11:23:11 -07001335 * implemented. May fail with -EINVAL if the context pointed to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 * is invalid.
1337 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001338SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 struct kioctx *ioctx = lookup_ioctx(ctx);
1341 if (likely(NULL != ioctx)) {
1342 io_destroy(ioctx);
Al Viroa2e18592012-03-20 16:27:57 -04001343 put_ioctx(ioctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return 0;
1345 }
1346 pr_debug("EINVAL: io_destroy: invalid context id\n");
1347 return -EINVAL;
1348}
1349
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001350static void aio_advance_iovec(struct kiocb *iocb, ssize_t ret)
1351{
1352 struct iovec *iov = &iocb->ki_iovec[iocb->ki_cur_seg];
1353
1354 BUG_ON(ret <= 0);
1355
1356 while (iocb->ki_cur_seg < iocb->ki_nr_segs && ret > 0) {
1357 ssize_t this = min((ssize_t)iov->iov_len, ret);
1358 iov->iov_base += this;
1359 iov->iov_len -= this;
1360 iocb->ki_left -= this;
1361 ret -= this;
1362 if (iov->iov_len == 0) {
1363 iocb->ki_cur_seg++;
1364 iov++;
1365 }
1366 }
1367
1368 /* the caller should not have done more io than what fit in
1369 * the remaining iovecs */
1370 BUG_ON(ret > 0 && iocb->ki_left == 0);
1371}
1372
1373static ssize_t aio_rw_vect_retry(struct kiocb *iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 struct file *file = iocb->ki_filp;
1376 struct address_space *mapping = file->f_mapping;
1377 struct inode *inode = mapping->host;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001378 ssize_t (*rw_op)(struct kiocb *, const struct iovec *,
1379 unsigned long, loff_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 ssize_t ret = 0;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001381 unsigned short opcode;
1382
1383 if ((iocb->ki_opcode == IOCB_CMD_PREADV) ||
1384 (iocb->ki_opcode == IOCB_CMD_PREAD)) {
1385 rw_op = file->f_op->aio_read;
1386 opcode = IOCB_CMD_PREADV;
1387 } else {
1388 rw_op = file->f_op->aio_write;
1389 opcode = IOCB_CMD_PWRITEV;
1390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Rusty Russellc2ec6682008-02-08 04:20:15 -08001392 /* This matches the pread()/pwrite() logic */
1393 if (iocb->ki_pos < 0)
1394 return -EINVAL;
1395
Zach Brown897f15f2005-09-30 11:58:55 -07001396 do {
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001397 ret = rw_op(iocb, &iocb->ki_iovec[iocb->ki_cur_seg],
1398 iocb->ki_nr_segs - iocb->ki_cur_seg,
1399 iocb->ki_pos);
1400 if (ret > 0)
1401 aio_advance_iovec(iocb, ret);
Badari Pulavarty027445c2006-09-30 23:28:46 -07001402
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001403 /* retry all partial writes. retry partial reads as long as its a
1404 * regular file. */
Zach Brown353fb072005-09-30 11:58:56 -07001405 } while (ret > 0 && iocb->ki_left > 0 &&
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001406 (opcode == IOCB_CMD_PWRITEV ||
1407 (!S_ISFIFO(inode->i_mode) && !S_ISSOCK(inode->i_mode))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 /* This means we must have transferred all that we could */
1410 /* No need to retry anymore */
1411 if ((ret == 0) || (iocb->ki_left == 0))
1412 ret = iocb->ki_nbytes - iocb->ki_left;
1413
Rusty Russell7adfa2f2008-02-08 04:20:14 -08001414 /* If we managed to write some out we return that, rather than
1415 * the eventual error. */
1416 if (opcode == IOCB_CMD_PWRITEV
1417 && ret < 0 && ret != -EIOCBQUEUED && ret != -EIOCBRETRY
1418 && iocb->ki_nbytes - iocb->ki_left)
1419 ret = iocb->ki_nbytes - iocb->ki_left;
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return ret;
1422}
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424static ssize_t aio_fdsync(struct kiocb *iocb)
1425{
1426 struct file *file = iocb->ki_filp;
1427 ssize_t ret = -EINVAL;
1428
1429 if (file->f_op->aio_fsync)
1430 ret = file->f_op->aio_fsync(iocb, 1);
1431 return ret;
1432}
1433
1434static ssize_t aio_fsync(struct kiocb *iocb)
1435{
1436 struct file *file = iocb->ki_filp;
1437 ssize_t ret = -EINVAL;
1438
1439 if (file->f_op->aio_fsync)
1440 ret = file->f_op->aio_fsync(iocb, 0);
1441 return ret;
1442}
1443
Jeff Moyer9d85cba2010-05-26 14:44:26 -07001444static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb, bool compat)
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001445{
1446 ssize_t ret;
1447
Jeff Moyer9d85cba2010-05-26 14:44:26 -07001448#ifdef CONFIG_COMPAT
1449 if (compat)
1450 ret = compat_rw_copy_check_uvector(type,
1451 (struct compat_iovec __user *)kiocb->ki_buf,
1452 kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
Christopher Yeohfcf63402011-10-31 17:06:39 -07001453 &kiocb->ki_iovec, 1);
Jeff Moyer9d85cba2010-05-26 14:44:26 -07001454 else
1455#endif
1456 ret = rw_copy_check_uvector(type,
1457 (struct iovec __user *)kiocb->ki_buf,
1458 kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
Christopher Yeohfcf63402011-10-31 17:06:39 -07001459 &kiocb->ki_iovec, 1);
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001460 if (ret < 0)
1461 goto out;
1462
jeron6fef9d02016-07-29 09:27:50 +08001463 ret = rw_verify_area(type, kiocb->ki_filp, &kiocb->ki_pos, ret);
1464 if (ret < 0)
1465 goto out;
1466
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001467 kiocb->ki_nr_segs = kiocb->ki_nbytes;
1468 kiocb->ki_cur_seg = 0;
1469 /* ki_nbytes/left now reflect bytes instead of segs */
1470 kiocb->ki_nbytes = ret;
1471 kiocb->ki_left = ret;
1472
1473 ret = 0;
1474out:
1475 return ret;
1476}
1477
jeron6fef9d02016-07-29 09:27:50 +08001478static ssize_t aio_setup_single_vector(int type, struct file * file, struct kiocb *kiocb)
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001479{
jeron6fef9d02016-07-29 09:27:50 +08001480 int bytes;
c.wang1f965332016-06-21 18:32:41 +08001481 size_t len = kiocb->ki_nbytes;
1482
jeron6fef9d02016-07-29 09:27:50 +08001483 bytes = rw_verify_area(type, file, &kiocb->ki_pos, kiocb->ki_left);
1484 if (bytes < 0)
1485 return bytes;
1486
c.wang1f965332016-06-21 18:32:41 +08001487 if (len > MAX_RW_COUNT)
1488 len = MAX_RW_COUNT;
1489
1490 if (unlikely(!access_ok(!rw, kiocb->ki_buf, len)))
1491 return -EFAULT;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001492 kiocb->ki_iovec = &kiocb->ki_inline_vec;
1493 kiocb->ki_iovec->iov_base = kiocb->ki_buf;
jeron6fef9d02016-07-29 09:27:50 +08001494 kiocb->ki_iovec->iov_len = bytes;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001495 kiocb->ki_nr_segs = 1;
1496 kiocb->ki_cur_seg = 0;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001497 return 0;
1498}
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500/*
1501 * aio_setup_iocb:
1502 * Performs the initial checks and aio retry method
1503 * setup for the kiocb at the time of io submission.
1504 */
Jeff Moyer9d85cba2010-05-26 14:44:26 -07001505static ssize_t aio_setup_iocb(struct kiocb *kiocb, bool compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 struct file *file = kiocb->ki_filp;
1508 ssize_t ret = 0;
1509
1510 switch (kiocb->ki_opcode) {
1511 case IOCB_CMD_PREAD:
1512 ret = -EBADF;
1513 if (unlikely(!(file->f_mode & FMODE_READ)))
1514 break;
1515 ret = -EFAULT;
1516 if (unlikely(!access_ok(VERIFY_WRITE, kiocb->ki_buf,
1517 kiocb->ki_left)))
1518 break;
jeron6fef9d02016-07-29 09:27:50 +08001519 ret = aio_setup_single_vector(READ, file, kiocb);
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001520 if (ret)
1521 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 ret = -EINVAL;
1523 if (file->f_op->aio_read)
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001524 kiocb->ki_retry = aio_rw_vect_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 break;
1526 case IOCB_CMD_PWRITE:
1527 ret = -EBADF;
1528 if (unlikely(!(file->f_mode & FMODE_WRITE)))
1529 break;
1530 ret = -EFAULT;
1531 if (unlikely(!access_ok(VERIFY_READ, kiocb->ki_buf,
1532 kiocb->ki_left)))
1533 break;
jeron6fef9d02016-07-29 09:27:50 +08001534 ret = aio_setup_single_vector(WRITE, file, kiocb);
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001535 if (ret)
1536 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 ret = -EINVAL;
1538 if (file->f_op->aio_write)
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001539 kiocb->ki_retry = aio_rw_vect_retry;
1540 break;
1541 case IOCB_CMD_PREADV:
1542 ret = -EBADF;
1543 if (unlikely(!(file->f_mode & FMODE_READ)))
1544 break;
Jeff Moyer9d85cba2010-05-26 14:44:26 -07001545 ret = aio_setup_vectored_rw(READ, kiocb, compat);
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001546 if (ret)
1547 break;
1548 ret = -EINVAL;
1549 if (file->f_op->aio_read)
1550 kiocb->ki_retry = aio_rw_vect_retry;
1551 break;
1552 case IOCB_CMD_PWRITEV:
1553 ret = -EBADF;
1554 if (unlikely(!(file->f_mode & FMODE_WRITE)))
1555 break;
Jeff Moyer9d85cba2010-05-26 14:44:26 -07001556 ret = aio_setup_vectored_rw(WRITE, kiocb, compat);
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001557 if (ret)
1558 break;
1559 ret = -EINVAL;
1560 if (file->f_op->aio_write)
1561 kiocb->ki_retry = aio_rw_vect_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 break;
1563 case IOCB_CMD_FDSYNC:
1564 ret = -EINVAL;
1565 if (file->f_op->aio_fsync)
1566 kiocb->ki_retry = aio_fdsync;
1567 break;
1568 case IOCB_CMD_FSYNC:
1569 ret = -EINVAL;
1570 if (file->f_op->aio_fsync)
1571 kiocb->ki_retry = aio_fsync;
1572 break;
1573 default:
1574 dprintk("EINVAL: io_submit: no operation provided\n");
1575 ret = -EINVAL;
1576 }
1577
1578 if (!kiocb->ki_retry)
1579 return ret;
1580
1581 return 0;
1582}
1583
Adrian Bunkd5470b52008-04-29 00:58:57 -07001584static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
Jeff Moyer080d6762011-11-02 13:40:10 -07001585 struct iocb *iocb, struct kiocb_batch *batch,
1586 bool compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
1588 struct kiocb *req;
1589 struct file *file;
1590 ssize_t ret;
1591
1592 /* enforce forwards compatibility on users */
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001593 if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 pr_debug("EINVAL: io_submit: reserve field set\n");
1595 return -EINVAL;
1596 }
1597
1598 /* prevent overflows */
1599 if (unlikely(
1600 (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
1601 (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
1602 ((ssize_t)iocb->aio_nbytes < 0)
1603 )) {
1604 pr_debug("EINVAL: io_submit: overflow check\n");
1605 return -EINVAL;
1606 }
1607
1608 file = fget(iocb->aio_fildes);
1609 if (unlikely(!file))
1610 return -EBADF;
1611
Jeff Moyer080d6762011-11-02 13:40:10 -07001612 req = aio_get_req(ctx, batch); /* returns with 2 references to req */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (unlikely(!req)) {
1614 fput(file);
1615 return -EAGAIN;
1616 }
Yan Zheng87e28312007-10-08 12:16:20 -07001617 req->ki_filp = file;
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001618 if (iocb->aio_flags & IOCB_FLAG_RESFD) {
1619 /*
1620 * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
1621 * instance of the file* now. The file descriptor must be
1622 * an eventfd() fd, and will be signaled for each completed
1623 * event using the eventfd_signal() function.
1624 */
Davide Libenzi13389012009-06-30 11:41:11 -07001625 req->ki_eventfd = eventfd_ctx_fdget((int) iocb->aio_resfd);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07001626 if (IS_ERR(req->ki_eventfd)) {
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001627 ret = PTR_ERR(req->ki_eventfd);
Davide Libenzi87c3a862009-03-18 17:04:19 -07001628 req->ki_eventfd = NULL;
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001629 goto out_put_req;
1630 }
1631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Ken Chen212079c2005-05-01 08:59:15 -07001633 ret = put_user(req->ki_key, &user_iocb->aio_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (unlikely(ret)) {
1635 dprintk("EFAULT: aio_key\n");
1636 goto out_put_req;
1637 }
1638
1639 req->ki_obj.user = user_iocb;
1640 req->ki_user_data = iocb->aio_data;
1641 req->ki_pos = iocb->aio_offset;
1642
1643 req->ki_buf = (char __user *)(unsigned long)iocb->aio_buf;
1644 req->ki_left = req->ki_nbytes = iocb->aio_nbytes;
1645 req->ki_opcode = iocb->aio_lio_opcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Jeff Moyer9d85cba2010-05-26 14:44:26 -07001647 ret = aio_setup_iocb(req, compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 if (ret)
1650 goto out_put_req;
1651
1652 spin_lock_irq(&ctx->ctx_lock);
Jan Kara7137c6bd2011-02-25 14:44:27 -08001653 /*
1654 * We could have raced with io_destroy() and are currently holding a
1655 * reference to ctx which should be destroyed. We cannot submit IO
1656 * since ctx gets freed as soon as io_submit() puts its reference. The
1657 * check here is reliable: io_destroy() sets ctx->dead before waiting
1658 * for outstanding IO and the barrier between these two is realized by
1659 * unlock of mm->ioctx_lock and lock of ctx->ctx_lock. Analogously we
1660 * increment ctx->reqs_active before checking for ctx->dead and the
1661 * barrier is realized by unlock and lock of ctx->ctx_lock. Thus if we
1662 * don't see ctx->dead set here, io_destroy() waits for our IO to
1663 * finish.
1664 */
1665 if (ctx->dead) {
1666 spin_unlock_irq(&ctx->ctx_lock);
1667 ret = -EINVAL;
1668 goto out_put_req;
1669 }
Benjamin LaHaiseac0b1bc2005-09-09 13:02:09 -07001670 aio_run_iocb(req);
Benjamin LaHaiseac0b1bc2005-09-09 13:02:09 -07001671 if (!list_empty(&ctx->run_list)) {
Ken Chen954d3e92005-05-01 08:59:16 -07001672 /* drain the run list */
1673 while (__aio_run_iocbs(ctx))
1674 ;
1675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 spin_unlock_irq(&ctx->ctx_lock);
Jeff Moyercfb1e332009-10-02 18:57:36 -04001677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 aio_put_req(req); /* drop extra ref to req */
1679 return 0;
1680
1681out_put_req:
1682 aio_put_req(req); /* drop extra ref to req */
1683 aio_put_req(req); /* drop i/o ref to req */
1684 return ret;
1685}
1686
Jeff Moyer9d85cba2010-05-26 14:44:26 -07001687long do_io_submit(aio_context_t ctx_id, long nr,
1688 struct iocb __user *__user *iocbpp, bool compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
1690 struct kioctx *ctx;
1691 long ret = 0;
Jeff Moyer080d6762011-11-02 13:40:10 -07001692 int i = 0;
Shaohua Li9f5b9422010-07-01 07:55:01 +02001693 struct blk_plug plug;
Jeff Moyer080d6762011-11-02 13:40:10 -07001694 struct kiocb_batch batch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 if (unlikely(nr < 0))
1697 return -EINVAL;
1698
Jeff Moyer75e1c702010-09-10 14:16:00 -07001699 if (unlikely(nr > LONG_MAX/sizeof(*iocbpp)))
1700 nr = LONG_MAX/sizeof(*iocbpp);
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
1703 return -EFAULT;
1704
1705 ctx = lookup_ioctx(ctx_id);
1706 if (unlikely(!ctx)) {
1707 pr_debug("EINVAL: io_submit: invalid context id\n");
1708 return -EINVAL;
1709 }
1710
Jeff Moyer080d6762011-11-02 13:40:10 -07001711 kiocb_batch_init(&batch, nr);
1712
Shaohua Li9f5b9422010-07-01 07:55:01 +02001713 blk_start_plug(&plug);
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 /*
1716 * AKPM: should this return a partial result if some of the IOs were
1717 * successfully submitted?
1718 */
1719 for (i=0; i<nr; i++) {
1720 struct iocb __user *user_iocb;
1721 struct iocb tmp;
1722
1723 if (unlikely(__get_user(user_iocb, iocbpp + i))) {
1724 ret = -EFAULT;
1725 break;
1726 }
1727
1728 if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
1729 ret = -EFAULT;
1730 break;
1731 }
1732
Jeff Moyer080d6762011-11-02 13:40:10 -07001733 ret = io_submit_one(ctx, user_iocb, &tmp, &batch, compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (ret)
1735 break;
1736 }
Shaohua Li9f5b9422010-07-01 07:55:01 +02001737 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Gleb Natapov69e47472012-01-08 17:07:28 +02001739 kiocb_batch_free(ctx, &batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 put_ioctx(ctx);
1741 return i ? i : ret;
1742}
1743
Jeff Moyer9d85cba2010-05-26 14:44:26 -07001744/* sys_io_submit:
1745 * Queue the nr iocbs pointed to by iocbpp for processing. Returns
1746 * the number of iocbs queued. May return -EINVAL if the aio_context
1747 * specified by ctx_id is invalid, if nr is < 0, if the iocb at
1748 * *iocbpp[0] is not properly initialized, if the operation specified
1749 * is invalid for the file descriptor in the iocb. May fail with
1750 * -EFAULT if any of the data structures point to invalid data. May
1751 * fail with -EBADF if the file descriptor specified in the first
1752 * iocb is invalid. May fail with -EAGAIN if insufficient resources
1753 * are available to queue any iocbs. Will return 0 if nr is 0. Will
1754 * fail with -ENOSYS if not implemented.
1755 */
1756SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
1757 struct iocb __user * __user *, iocbpp)
1758{
1759 return do_io_submit(ctx_id, nr, iocbpp, 0);
1760}
1761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762/* lookup_kiocb
1763 * Finds a given iocb for cancellation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 */
Adrian Bunk25ee7e32005-04-25 08:18:14 -07001765static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
1766 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
1768 struct list_head *pos;
Zach Brownd00689a2005-11-13 16:07:34 -08001769
1770 assert_spin_locked(&ctx->ctx_lock);
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 /* TODO: use a hash or array, this sucks. */
1773 list_for_each(pos, &ctx->active_reqs) {
1774 struct kiocb *kiocb = list_kiocb(pos);
1775 if (kiocb->ki_obj.user == iocb && kiocb->ki_key == key)
1776 return kiocb;
1777 }
1778 return NULL;
1779}
1780
1781/* sys_io_cancel:
1782 * Attempts to cancel an iocb previously passed to io_submit. If
1783 * the operation is successfully cancelled, the resulting event is
1784 * copied into the memory pointed to by result without being placed
1785 * into the completion queue and 0 is returned. May fail with
1786 * -EFAULT if any of the data structures pointed to are invalid.
1787 * May fail with -EINVAL if aio_context specified by ctx_id is
1788 * invalid. May fail with -EAGAIN if the iocb specified was not
1789 * cancelled. Will fail with -ENOSYS if not implemented.
1790 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001791SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
1792 struct io_event __user *, result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
1794 int (*cancel)(struct kiocb *iocb, struct io_event *res);
1795 struct kioctx *ctx;
1796 struct kiocb *kiocb;
1797 u32 key;
1798 int ret;
1799
1800 ret = get_user(key, &iocb->aio_key);
1801 if (unlikely(ret))
1802 return -EFAULT;
1803
1804 ctx = lookup_ioctx(ctx_id);
1805 if (unlikely(!ctx))
1806 return -EINVAL;
1807
1808 spin_lock_irq(&ctx->ctx_lock);
1809 ret = -EAGAIN;
1810 kiocb = lookup_kiocb(ctx, iocb, key);
1811 if (kiocb && kiocb->ki_cancel) {
1812 cancel = kiocb->ki_cancel;
1813 kiocb->ki_users ++;
1814 kiocbSetCancelled(kiocb);
1815 } else
1816 cancel = NULL;
1817 spin_unlock_irq(&ctx->ctx_lock);
1818
1819 if (NULL != cancel) {
1820 struct io_event tmp;
1821 pr_debug("calling cancel\n");
1822 memset(&tmp, 0, sizeof(tmp));
1823 tmp.obj = (u64)(unsigned long)kiocb->ki_obj.user;
1824 tmp.data = kiocb->ki_user_data;
1825 ret = cancel(kiocb, &tmp);
1826 if (!ret) {
1827 /* Cancellation succeeded -- copy the result
1828 * into the user's buffer.
1829 */
1830 if (copy_to_user(result, &tmp, sizeof(tmp)))
1831 ret = -EFAULT;
1832 }
1833 } else
Wendy Cheng8f582022005-09-09 13:02:08 -07001834 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836 put_ioctx(ctx);
1837
1838 return ret;
1839}
1840
1841/* io_getevents:
1842 * Attempts to read at least min_nr events and up to nr events from
Satoru Takeuchi642b5122010-08-05 11:23:11 -07001843 * the completion queue for the aio_context specified by ctx_id. If
1844 * it succeeds, the number of read events is returned. May fail with
1845 * -EINVAL if ctx_id is invalid, if min_nr is out of range, if nr is
1846 * out of range, if timeout is out of range. May fail with -EFAULT
1847 * if any of the memory specified is invalid. May return 0 or
1848 * < min_nr if the timeout specified by timeout has elapsed
1849 * before sufficient events are available, where timeout == NULL
1850 * specifies an infinite timeout. Note that the timeout pointed to by
1851 * timeout is relative and will be updated if not NULL and the
1852 * operation blocks. Will fail with -ENOSYS if not implemented.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001854SYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id,
1855 long, min_nr,
1856 long, nr,
1857 struct io_event __user *, events,
1858 struct timespec __user *, timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
1860 struct kioctx *ioctx = lookup_ioctx(ctx_id);
1861 long ret = -EINVAL;
1862
1863 if (likely(ioctx)) {
Namhyung Kim2e410252011-01-12 17:01:08 -08001864 if (likely(min_nr <= nr && min_nr >= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 ret = read_events(ioctx, min_nr, nr, events, timeout);
1866 put_ioctx(ioctx);
1867 }
1868
Roland McGrath598af052008-04-10 15:38:45 -07001869 asmlinkage_protect(5, ret, ctx_id, min_nr, nr, events, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 return ret;
1871}