blob: 0633cf3b325ce5dcbaba11d254ab33904f313a8f [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 */
Kent Overstreetcaf41672013-05-07 16:18:35 -070011#define pr_fmt(fmt) "%s: " fmt, __func__
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050018#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/syscalls.h>
Jens Axboeb9d128f2009-10-29 13:59:26 +010020#include <linux/backing-dev.h>
Badari Pulavarty027445c2006-09-30 23:28:46 -070021#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Ingo Molnar174cd4b2017-02-02 19:15:33 +010023#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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>
Kent Overstreete1bdd5f2013-04-26 10:58:39 +100029#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/slab.h>
31#include <linux/timer.h>
32#include <linux/aio.h>
33#include <linux/highmem.h>
34#include <linux/workqueue.h>
35#include <linux/security.h>
Davide Libenzi9c3060b2007-05-10 22:23:21 -070036#include <linux/eventfd.h>
Jeff Moyercfb1e332009-10-02 18:57:36 -040037#include <linux/blkdev.h>
Jeff Moyer9d85cba72010-05-26 14:44:26 -070038#include <linux/compat.h>
Gu Zheng36bc08c2013-07-16 17:56:16 +080039#include <linux/migrate.h>
40#include <linux/ramfs.h>
Kent Overstreet723be6e2013-05-28 15:14:48 -070041#include <linux/percpu-refcount.h>
Benjamin LaHaise71ad7492013-09-17 10:18:25 -040042#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <asm/kmap_types.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080045#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Al Viro68d70d02013-06-19 15:26:04 +040047#include "internal.h"
48
Christoph Hellwigf3a27522018-03-30 11:19:25 +020049#define KIOCB_KEY 0
50
Kent Overstreet4e179bc2013-05-07 16:18:33 -070051#define AIO_RING_MAGIC 0xa10a10a1
52#define AIO_RING_COMPAT_FEATURES 1
53#define AIO_RING_INCOMPAT_FEATURES 0
54struct aio_ring {
55 unsigned id; /* kernel internal index number */
56 unsigned nr; /* number of io_events */
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -040057 unsigned head; /* Written to by userland or under ring_lock
58 * mutex by aio_read_events_ring(). */
Kent Overstreet4e179bc2013-05-07 16:18:33 -070059 unsigned tail;
60
61 unsigned magic;
62 unsigned compat_features;
63 unsigned incompat_features;
64 unsigned header_length; /* size of aio_ring */
65
66
67 struct io_event io_events[0];
68}; /* 128 bytes + ring size */
69
70#define AIO_RING_PAGES 8
Kent Overstreet4e179bc2013-05-07 16:18:33 -070071
Benjamin LaHaisedb446a02013-07-30 12:54:40 -040072struct kioctx_table {
Tejun Heod0264c02018-03-14 12:10:17 -070073 struct rcu_head rcu;
74 unsigned nr;
75 struct kioctx __rcu *table[];
Benjamin LaHaisedb446a02013-07-30 12:54:40 -040076};
77
Kent Overstreete1bdd5f2013-04-26 10:58:39 +100078struct kioctx_cpu {
79 unsigned reqs_available;
80};
81
Jens Axboedc48e562015-04-15 11:17:23 -060082struct ctx_rq_wait {
83 struct completion comp;
84 atomic_t count;
85};
86
Kent Overstreet4e179bc2013-05-07 16:18:33 -070087struct kioctx {
Kent Overstreet723be6e2013-05-28 15:14:48 -070088 struct percpu_ref users;
Kent Overstreet36f55882013-05-07 16:18:41 -070089 atomic_t dead;
Kent Overstreet4e179bc2013-05-07 16:18:33 -070090
Kent Overstreete34ecee2013-10-10 19:31:47 -070091 struct percpu_ref reqs;
92
Kent Overstreet4e179bc2013-05-07 16:18:33 -070093 unsigned long user_id;
Kent Overstreet4e179bc2013-05-07 16:18:33 -070094
Kent Overstreete1bdd5f2013-04-26 10:58:39 +100095 struct __percpu kioctx_cpu *cpu;
96
97 /*
98 * For percpu reqs_available, number of slots we move to/from global
99 * counter at a time:
100 */
101 unsigned req_batch;
Kent Overstreet3e845ce2013-05-07 16:18:51 -0700102 /*
103 * This is what userspace passed to io_setup(), it's not used for
104 * anything but counting against the global max_reqs quota.
105 *
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700106 * The real limit is nr_events - 1, which will be larger (see
Kent Overstreet3e845ce2013-05-07 16:18:51 -0700107 * aio_setup_ring())
108 */
Kent Overstreet4e179bc2013-05-07 16:18:33 -0700109 unsigned max_reqs;
110
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700111 /* Size of ringbuffer, in units of struct io_event */
112 unsigned nr_events;
Kent Overstreet4e179bc2013-05-07 16:18:33 -0700113
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700114 unsigned long mmap_base;
115 unsigned long mmap_size;
116
117 struct page **ring_pages;
118 long nr_pages;
119
Tejun Heof7298632018-03-14 12:45:15 -0700120 struct rcu_work free_rwork; /* see free_ioctx() */
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700121
Anatol Pomozove02ba722014-04-15 11:31:33 -0700122 /*
123 * signals when all in-flight requests are done
124 */
Jens Axboedc48e562015-04-15 11:17:23 -0600125 struct ctx_rq_wait *rq_wait;
Anatol Pomozove02ba722014-04-15 11:31:33 -0700126
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700127 struct {
Kent Overstreet34e83fc2013-04-26 10:58:39 +1000128 /*
129 * This counts the number of available slots in the ringbuffer,
130 * so we avoid overflowing it: it's decremented (if positive)
131 * when allocating a kiocb and incremented when the resulting
132 * io_event is pulled off the ringbuffer.
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000133 *
134 * We batch accesses to it with a percpu version.
Kent Overstreet34e83fc2013-04-26 10:58:39 +1000135 */
136 atomic_t reqs_available;
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700137 } ____cacheline_aligned_in_smp;
138
139 struct {
140 spinlock_t ctx_lock;
141 struct list_head active_reqs; /* used for cancellation */
142 } ____cacheline_aligned_in_smp;
143
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700144 struct {
145 struct mutex ring_lock;
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700146 wait_queue_head_t wait;
147 } ____cacheline_aligned_in_smp;
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700148
149 struct {
150 unsigned tail;
Benjamin LaHaised856f322014-08-24 13:14:05 -0400151 unsigned completed_events;
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700152 spinlock_t completion_lock;
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700153 } ____cacheline_aligned_in_smp;
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700154
155 struct page *internal_pages[AIO_RING_PAGES];
Gu Zheng36bc08c2013-07-16 17:56:16 +0800156 struct file *aio_ring_file;
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400157
158 unsigned id;
Kent Overstreet4e179bc2013-05-07 16:18:33 -0700159};
160
Christoph Hellwiga3c0d432018-03-27 19:18:57 +0200161struct fsync_iocb {
162 struct work_struct work;
163 struct file *file;
164 bool datasync;
165};
166
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100167struct aio_kiocb {
Christoph Hellwig54843f82018-05-02 19:57:21 +0200168 union {
169 struct kiocb rw;
Christoph Hellwiga3c0d432018-03-27 19:18:57 +0200170 struct fsync_iocb fsync;
Christoph Hellwig54843f82018-05-02 19:57:21 +0200171 };
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100172
173 struct kioctx *ki_ctx;
174 kiocb_cancel_fn *ki_cancel;
175
176 struct iocb __user *ki_user_iocb; /* user's aiocb */
177 __u64 ki_user_data; /* user's data for completion */
178
179 struct list_head ki_list; /* the aio core uses this
180 * for cancellation */
181
182 /*
183 * If the aio_resfd field of the userspace iocb is not zero,
184 * this is the underlying eventfd context to deliver events to.
185 */
186 struct eventfd_ctx *ki_eventfd;
187};
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*------ sysctl variables----*/
Zach Brownd55b5fd2005-11-07 00:59:31 -0800190static DEFINE_SPINLOCK(aio_nr_lock);
191unsigned long aio_nr; /* current system wide number of aio requests */
192unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/*----end sysctl variables---*/
194
Christoph Lametere18b8902006-12-06 20:33:20 -0800195static struct kmem_cache *kiocb_cachep;
196static struct kmem_cache *kioctx_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400198static struct vfsmount *aio_mnt;
199
200static const struct file_operations aio_ring_fops;
201static const struct address_space_operations aio_ctx_aops;
202
203static struct file *aio_private_file(struct kioctx *ctx, loff_t nr_pages)
204{
205 struct qstr this = QSTR_INIT("[aio]", 5);
206 struct file *file;
207 struct path path;
208 struct inode *inode = alloc_anon_inode(aio_mnt->mnt_sb);
Dan Carpenter7f626562013-11-13 10:49:40 +0300209 if (IS_ERR(inode))
210 return ERR_CAST(inode);
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400211
212 inode->i_mapping->a_ops = &aio_ctx_aops;
213 inode->i_mapping->private_data = ctx;
214 inode->i_size = PAGE_SIZE * nr_pages;
215
216 path.dentry = d_alloc_pseudo(aio_mnt->mnt_sb, &this);
217 if (!path.dentry) {
218 iput(inode);
219 return ERR_PTR(-ENOMEM);
220 }
221 path.mnt = mntget(aio_mnt);
222
223 d_instantiate(path.dentry, inode);
224 file = alloc_file(&path, FMODE_READ | FMODE_WRITE, &aio_ring_fops);
225 if (IS_ERR(file)) {
226 path_put(&path);
227 return file;
228 }
229
230 file->f_flags = O_RDWR;
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400231 return file;
232}
233
234static struct dentry *aio_mount(struct file_system_type *fs_type,
235 int flags, const char *dev_name, void *data)
236{
237 static const struct dentry_operations ops = {
238 .d_dname = simple_dname,
239 };
Jann Horn22f6b4d2016-09-16 00:31:22 +0200240 struct dentry *root = mount_pseudo(fs_type, "aio:", NULL, &ops,
241 AIO_RING_MAGIC);
242
243 if (!IS_ERR(root))
244 root->d_sb->s_iflags |= SB_I_NOEXEC;
245 return root;
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/* aio_setup
249 * Creates the slab caches used by the aio routines, panic on
250 * failure as this is done early during the boot sequence.
251 */
252static int __init aio_setup(void)
253{
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400254 static struct file_system_type aio_fs = {
255 .name = "aio",
256 .mount = aio_mount,
257 .kill_sb = kill_anon_super,
258 };
259 aio_mnt = kern_mount(&aio_fs);
260 if (IS_ERR(aio_mnt))
261 panic("Failed to create aio fs mount.");
262
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100263 kiocb_cachep = KMEM_CACHE(aio_kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
Christoph Lameter0a31bd52007-05-06 14:49:57 -0700264 kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return 0;
266}
H Hartley Sweeten385773e2009-09-22 16:43:53 -0700267__initcall(aio_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400269static void put_aio_ring_file(struct kioctx *ctx)
270{
271 struct file *aio_ring_file = ctx->aio_ring_file;
Rasmus Villemoesde04e762016-09-15 00:25:03 +0200272 struct address_space *i_mapping;
273
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400274 if (aio_ring_file) {
Al Viro45063092016-12-04 18:24:56 -0500275 truncate_setsize(file_inode(aio_ring_file), 0);
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400276
277 /* Prevent further access to the kioctx from migratepages */
Al Viro45063092016-12-04 18:24:56 -0500278 i_mapping = aio_ring_file->f_mapping;
Rasmus Villemoesde04e762016-09-15 00:25:03 +0200279 spin_lock(&i_mapping->private_lock);
280 i_mapping->private_data = NULL;
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400281 ctx->aio_ring_file = NULL;
Rasmus Villemoesde04e762016-09-15 00:25:03 +0200282 spin_unlock(&i_mapping->private_lock);
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400283
284 fput(aio_ring_file);
285 }
286}
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288static void aio_free_ring(struct kioctx *ctx)
289{
Gu Zheng36bc08c2013-07-16 17:56:16 +0800290 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400292 /* Disconnect the kiotx from the ring file. This prevents future
293 * accesses to the kioctx from page migration.
294 */
295 put_aio_ring_file(ctx);
296
Gu Zheng36bc08c2013-07-16 17:56:16 +0800297 for (i = 0; i < ctx->nr_pages; i++) {
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500298 struct page *page;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800299 pr_debug("pid(%d) [%d] page->count=%d\n", current->pid, i,
300 page_count(ctx->ring_pages[i]));
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500301 page = ctx->ring_pages[i];
302 if (!page)
303 continue;
304 ctx->ring_pages[i] = NULL;
305 put_page(page);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Sasha Levinddb8c452013-11-19 17:33:03 -0500308 if (ctx->ring_pages && ctx->ring_pages != ctx->internal_pages) {
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700309 kfree(ctx->ring_pages);
Sasha Levinddb8c452013-11-19 17:33:03 -0500310 ctx->ring_pages = NULL;
311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
Oleg Nesterov5477e702015-09-04 15:48:04 -0700314static int aio_ring_mremap(struct vm_area_struct *vma)
Gu Zheng36bc08c2013-07-16 17:56:16 +0800315{
Oleg Nesterov5477e702015-09-04 15:48:04 -0700316 struct file *file = vma->vm_file;
Pavel Emelyanove4a0d3e2014-09-18 19:56:17 +0400317 struct mm_struct *mm = vma->vm_mm;
318 struct kioctx_table *table;
Al Virob2edffd2015-04-06 17:48:54 -0400319 int i, res = -EINVAL;
Pavel Emelyanove4a0d3e2014-09-18 19:56:17 +0400320
321 spin_lock(&mm->ioctx_lock);
322 rcu_read_lock();
323 table = rcu_dereference(mm->ioctx_table);
324 for (i = 0; i < table->nr; i++) {
325 struct kioctx *ctx;
326
Tejun Heod0264c02018-03-14 12:10:17 -0700327 ctx = rcu_dereference(table->table[i]);
Pavel Emelyanove4a0d3e2014-09-18 19:56:17 +0400328 if (ctx && ctx->aio_ring_file == file) {
Al Virob2edffd2015-04-06 17:48:54 -0400329 if (!atomic_read(&ctx->dead)) {
330 ctx->user_id = ctx->mmap_base = vma->vm_start;
331 res = 0;
332 }
Pavel Emelyanove4a0d3e2014-09-18 19:56:17 +0400333 break;
334 }
335 }
336
337 rcu_read_unlock();
338 spin_unlock(&mm->ioctx_lock);
Al Virob2edffd2015-04-06 17:48:54 -0400339 return res;
Pavel Emelyanove4a0d3e2014-09-18 19:56:17 +0400340}
341
Oleg Nesterov5477e702015-09-04 15:48:04 -0700342static const struct vm_operations_struct aio_ring_vm_ops = {
343 .mremap = aio_ring_mremap,
344#if IS_ENABLED(CONFIG_MMU)
345 .fault = filemap_fault,
346 .map_pages = filemap_map_pages,
347 .page_mkwrite = filemap_page_mkwrite,
348#endif
349};
350
351static int aio_ring_mmap(struct file *file, struct vm_area_struct *vma)
352{
353 vma->vm_flags |= VM_DONTEXPAND;
354 vma->vm_ops = &aio_ring_vm_ops;
355 return 0;
356}
357
Gu Zheng36bc08c2013-07-16 17:56:16 +0800358static const struct file_operations aio_ring_fops = {
359 .mmap = aio_ring_mmap,
360};
361
Benjamin LaHaise0c453552013-07-17 09:34:24 -0400362#if IS_ENABLED(CONFIG_MIGRATION)
Gu Zheng36bc08c2013-07-16 17:56:16 +0800363static int aio_migratepage(struct address_space *mapping, struct page *new,
364 struct page *old, enum migrate_mode mode)
365{
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400366 struct kioctx *ctx;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800367 unsigned long flags;
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400368 pgoff_t idx;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800369 int rc;
370
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700371 /*
372 * We cannot support the _NO_COPY case here, because copy needs to
373 * happen under the ctx->completion_lock. That does not work with the
374 * migration workflow of MIGRATE_SYNC_NO_COPY.
375 */
376 if (mode == MIGRATE_SYNC_NO_COPY)
377 return -EINVAL;
378
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500379 rc = 0;
380
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400381 /* mapping->private_lock here protects against the kioctx teardown. */
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500382 spin_lock(&mapping->private_lock);
383 ctx = mapping->private_data;
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400384 if (!ctx) {
385 rc = -EINVAL;
386 goto out;
387 }
388
389 /* The ring_lock mutex. The prevents aio_read_events() from writing
390 * to the ring's head, and prevents page migration from mucking in
391 * a partially initialized kiotx.
392 */
393 if (!mutex_trylock(&ctx->ring_lock)) {
394 rc = -EAGAIN;
395 goto out;
396 }
397
398 idx = old->index;
399 if (idx < (pgoff_t)ctx->nr_pages) {
400 /* Make sure the old page hasn't already been changed */
401 if (ctx->ring_pages[idx] != old)
402 rc = -EAGAIN;
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500403 } else
404 rc = -EINVAL;
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500405
406 if (rc != 0)
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400407 goto out_unlock;
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500408
Gu Zheng36bc08c2013-07-16 17:56:16 +0800409 /* Writeback must be complete */
410 BUG_ON(PageWriteback(old));
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500411 get_page(new);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800412
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500413 rc = migrate_page_move_mapping(mapping, new, old, NULL, mode, 1);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800414 if (rc != MIGRATEPAGE_SUCCESS) {
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500415 put_page(new);
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400416 goto out_unlock;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800417 }
418
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400419 /* Take completion_lock to prevent other writes to the ring buffer
420 * while the old page is copied to the new. This prevents new
421 * events from being lost.
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400422 */
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400423 spin_lock_irqsave(&ctx->completion_lock, flags);
424 migrate_page_copy(new, old);
425 BUG_ON(ctx->ring_pages[idx] != old);
426 ctx->ring_pages[idx] = new;
427 spin_unlock_irqrestore(&ctx->completion_lock, flags);
428
429 /* The old page is no longer accessible. */
430 put_page(old);
431
432out_unlock:
433 mutex_unlock(&ctx->ring_lock);
434out:
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400435 spin_unlock(&mapping->private_lock);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800436 return rc;
437}
Benjamin LaHaise0c453552013-07-17 09:34:24 -0400438#endif
Gu Zheng36bc08c2013-07-16 17:56:16 +0800439
440static const struct address_space_operations aio_ctx_aops = {
Gu Zheng835f2522014-11-06 17:46:21 +0800441 .set_page_dirty = __set_page_dirty_no_writeback,
Benjamin LaHaise0c453552013-07-17 09:34:24 -0400442#if IS_ENABLED(CONFIG_MIGRATION)
Gu Zheng36bc08c2013-07-16 17:56:16 +0800443 .migratepage = aio_migratepage,
Benjamin LaHaise0c453552013-07-17 09:34:24 -0400444#endif
Gu Zheng36bc08c2013-07-16 17:56:16 +0800445};
446
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300447static int aio_setup_ring(struct kioctx *ctx, unsigned int nr_events)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 struct aio_ring *ring;
Zach Brown41003a72013-05-07 16:18:25 -0700450 struct mm_struct *mm = current->mm;
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900451 unsigned long size, unused;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 int nr_pages;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800453 int i;
454 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 /* Compensate for the ring buffer's head/tail overlap entry */
457 nr_events += 2; /* 1 is required, 2 for good luck */
458
459 size = sizeof(struct aio_ring);
460 size += sizeof(struct io_event) * nr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Gu Zheng36bc08c2013-07-16 17:56:16 +0800462 nr_pages = PFN_UP(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (nr_pages < 0)
464 return -EINVAL;
465
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400466 file = aio_private_file(ctx, nr_pages);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800467 if (IS_ERR(file)) {
468 ctx->aio_ring_file = NULL;
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400469 return -ENOMEM;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Gu Zheng36bc08c2013-07-16 17:56:16 +0800472 ctx->aio_ring_file = file;
473 nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring))
474 / sizeof(struct io_event);
475
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700476 ctx->ring_pages = ctx->internal_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (nr_pages > AIO_RING_PAGES) {
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700478 ctx->ring_pages = kcalloc(nr_pages, sizeof(struct page *),
479 GFP_KERNEL);
Gu Zhengd1b94322013-12-04 18:19:06 +0800480 if (!ctx->ring_pages) {
481 put_aio_ring_file(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return -ENOMEM;
Gu Zhengd1b94322013-12-04 18:19:06 +0800483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900486 for (i = 0; i < nr_pages; i++) {
487 struct page *page;
Al Viro45063092016-12-04 18:24:56 -0500488 page = find_or_create_page(file->f_mapping,
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900489 i, GFP_HIGHUSER | __GFP_ZERO);
490 if (!page)
491 break;
492 pr_debug("pid(%d) page[%d]->count=%d\n",
493 current->pid, i, page_count(page));
494 SetPageUptodate(page);
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900495 unlock_page(page);
496
497 ctx->ring_pages[i] = page;
498 }
499 ctx->nr_pages = i;
500
501 if (unlikely(i != nr_pages)) {
502 aio_free_ring(ctx);
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400503 return -ENOMEM;
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900504 }
505
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700506 ctx->mmap_size = nr_pages * PAGE_SIZE;
507 pr_debug("attempting mmap of %lu bytes\n", ctx->mmap_size);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800508
Michal Hocko013373e2016-05-23 16:25:59 -0700509 if (down_write_killable(&mm->mmap_sem)) {
510 ctx->mmap_size = 0;
511 aio_free_ring(ctx);
512 return -EINTR;
513 }
514
Gu Zheng36bc08c2013-07-16 17:56:16 +0800515 ctx->mmap_base = do_mmap_pgoff(ctx->aio_ring_file, 0, ctx->mmap_size,
516 PROT_READ | PROT_WRITE,
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800517 MAP_SHARED, 0, &unused, NULL);
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900518 up_write(&mm->mmap_sem);
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700519 if (IS_ERR((void *)ctx->mmap_base)) {
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700520 ctx->mmap_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 aio_free_ring(ctx);
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400522 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700525 pr_debug("mmap address: 0x%08lx\n", ctx->mmap_base);
Benjamin LaHaised6c355c2013-09-09 11:57:59 -0400526
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700527 ctx->user_id = ctx->mmap_base;
528 ctx->nr_events = nr_events; /* trusted copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700530 ring = kmap_atomic(ctx->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 ring->nr = nr_events; /* user copy */
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400532 ring->id = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 ring->head = ring->tail = 0;
534 ring->magic = AIO_RING_MAGIC;
535 ring->compat_features = AIO_RING_COMPAT_FEATURES;
536 ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
537 ring->header_length = sizeof(struct aio_ring);
Cong Wange8e3c3d2011-11-25 23:14:27 +0800538 kunmap_atomic(ring);
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700539 flush_dcache_page(ctx->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 return 0;
542}
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544#define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
545#define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
546#define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
547
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100548void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel)
Kent Overstreet0460fef2013-05-07 16:18:49 -0700549{
Christoph Hellwig54843f82018-05-02 19:57:21 +0200550 struct aio_kiocb *req = container_of(iocb, struct aio_kiocb, rw);
Kent Overstreet0460fef2013-05-07 16:18:49 -0700551 struct kioctx *ctx = req->ki_ctx;
552 unsigned long flags;
553
Christoph Hellwig75321b52018-04-09 14:57:56 +0200554 if (WARN_ON_ONCE(!list_empty(&req->ki_list)))
555 return;
556
Kent Overstreet0460fef2013-05-07 16:18:49 -0700557 spin_lock_irqsave(&ctx->ctx_lock, flags);
Christoph Hellwig75321b52018-04-09 14:57:56 +0200558 list_add_tail(&req->ki_list, &ctx->active_reqs);
Kent Overstreet0460fef2013-05-07 16:18:49 -0700559 req->ki_cancel = cancel;
Kent Overstreet0460fef2013-05-07 16:18:49 -0700560 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
561}
562EXPORT_SYMBOL(kiocb_set_cancel_fn);
563
Tejun Heoa6d7cff2018-03-14 12:10:17 -0700564/*
565 * free_ioctx() should be RCU delayed to synchronize against the RCU
566 * protected lookup_ioctx() and also needs process context to call
Tejun Heof7298632018-03-14 12:45:15 -0700567 * aio_free_ring(). Use rcu_work.
Tejun Heoa6d7cff2018-03-14 12:10:17 -0700568 */
Kent Overstreete34ecee2013-10-10 19:31:47 -0700569static void free_ioctx(struct work_struct *work)
Kent Overstreet36f55882013-05-07 16:18:41 -0700570{
Tejun Heof7298632018-03-14 12:45:15 -0700571 struct kioctx *ctx = container_of(to_rcu_work(work), struct kioctx,
572 free_rwork);
Kent Overstreete34ecee2013-10-10 19:31:47 -0700573 pr_debug("freeing %p\n", ctx);
574
575 aio_free_ring(ctx);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000576 free_percpu(ctx->cpu);
Tejun Heo9a1049d2014-06-28 08:10:14 -0400577 percpu_ref_exit(&ctx->reqs);
578 percpu_ref_exit(&ctx->users);
Kent Overstreet36f55882013-05-07 16:18:41 -0700579 kmem_cache_free(kioctx_cachep, ctx);
580}
581
Kent Overstreete34ecee2013-10-10 19:31:47 -0700582static void free_ioctx_reqs(struct percpu_ref *ref)
583{
584 struct kioctx *ctx = container_of(ref, struct kioctx, reqs);
585
Anatol Pomozove02ba722014-04-15 11:31:33 -0700586 /* At this point we know that there are no any in-flight requests */
Jens Axboedc48e562015-04-15 11:17:23 -0600587 if (ctx->rq_wait && atomic_dec_and_test(&ctx->rq_wait->count))
588 complete(&ctx->rq_wait->comp);
Anatol Pomozove02ba722014-04-15 11:31:33 -0700589
Tejun Heoa6d7cff2018-03-14 12:10:17 -0700590 /* Synchronize against RCU protected table->table[] dereferences */
Tejun Heof7298632018-03-14 12:45:15 -0700591 INIT_RCU_WORK(&ctx->free_rwork, free_ioctx);
592 queue_rcu_work(system_wq, &ctx->free_rwork);
Kent Overstreete34ecee2013-10-10 19:31:47 -0700593}
594
Kent Overstreet36f55882013-05-07 16:18:41 -0700595/*
596 * When this function runs, the kioctx has been removed from the "hash table"
597 * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
598 * now it's safe to cancel any that need to be.
599 */
Kent Overstreete34ecee2013-10-10 19:31:47 -0700600static void free_ioctx_users(struct percpu_ref *ref)
Kent Overstreet36f55882013-05-07 16:18:41 -0700601{
Kent Overstreete34ecee2013-10-10 19:31:47 -0700602 struct kioctx *ctx = container_of(ref, struct kioctx, users);
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100603 struct aio_kiocb *req;
Kent Overstreet36f55882013-05-07 16:18:41 -0700604
605 spin_lock_irq(&ctx->ctx_lock);
606
607 while (!list_empty(&ctx->active_reqs)) {
608 req = list_first_entry(&ctx->active_reqs,
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100609 struct aio_kiocb, ki_list);
Christoph Hellwig888933f2018-05-23 14:11:02 +0200610 req->ki_cancel(&req->rw);
Al Viro4faa99962018-05-23 22:53:22 -0400611 list_del_init(&req->ki_list);
Kent Overstreet36f55882013-05-07 16:18:41 -0700612 }
613
614 spin_unlock_irq(&ctx->ctx_lock);
615
Kent Overstreete34ecee2013-10-10 19:31:47 -0700616 percpu_ref_kill(&ctx->reqs);
617 percpu_ref_put(&ctx->reqs);
Kent Overstreet36f55882013-05-07 16:18:41 -0700618}
619
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400620static int ioctx_add_table(struct kioctx *ctx, struct mm_struct *mm)
621{
622 unsigned i, new_nr;
623 struct kioctx_table *table, *old;
624 struct aio_ring *ring;
625
626 spin_lock(&mm->ioctx_lock);
Oleg Nesterov855ef0d2014-04-30 16:16:36 +0200627 table = rcu_dereference_raw(mm->ioctx_table);
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400628
629 while (1) {
630 if (table)
631 for (i = 0; i < table->nr; i++)
Tejun Heod0264c02018-03-14 12:10:17 -0700632 if (!rcu_access_pointer(table->table[i])) {
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400633 ctx->id = i;
Tejun Heod0264c02018-03-14 12:10:17 -0700634 rcu_assign_pointer(table->table[i], ctx);
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400635 spin_unlock(&mm->ioctx_lock);
636
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400637 /* While kioctx setup is in progress,
638 * we are protected from page migration
639 * changes ring_pages by ->ring_lock.
640 */
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400641 ring = kmap_atomic(ctx->ring_pages[0]);
642 ring->id = ctx->id;
643 kunmap_atomic(ring);
644 return 0;
645 }
646
647 new_nr = (table ? table->nr : 1) * 4;
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400648 spin_unlock(&mm->ioctx_lock);
649
650 table = kzalloc(sizeof(*table) + sizeof(struct kioctx *) *
651 new_nr, GFP_KERNEL);
652 if (!table)
653 return -ENOMEM;
654
655 table->nr = new_nr;
656
657 spin_lock(&mm->ioctx_lock);
Oleg Nesterov855ef0d2014-04-30 16:16:36 +0200658 old = rcu_dereference_raw(mm->ioctx_table);
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400659
660 if (!old) {
661 rcu_assign_pointer(mm->ioctx_table, table);
662 } else if (table->nr > old->nr) {
663 memcpy(table->table, old->table,
664 old->nr * sizeof(struct kioctx *));
665
666 rcu_assign_pointer(mm->ioctx_table, table);
667 kfree_rcu(old, rcu);
668 } else {
669 kfree(table);
670 table = old;
671 }
672 }
673}
674
Kent Overstreete34ecee2013-10-10 19:31:47 -0700675static void aio_nr_sub(unsigned nr)
676{
677 spin_lock(&aio_nr_lock);
678 if (WARN_ON(aio_nr - nr > aio_nr))
679 aio_nr = 0;
680 else
681 aio_nr -= nr;
682 spin_unlock(&aio_nr_lock);
683}
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685/* ioctx_alloc
686 * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
687 */
688static struct kioctx *ioctx_alloc(unsigned nr_events)
689{
Zach Brown41003a72013-05-07 16:18:25 -0700690 struct mm_struct *mm = current->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 struct kioctx *ctx;
Al Viroe23754f2012-03-06 14:33:22 -0500692 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000694 /*
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300695 * Store the original nr_events -- what userspace passed to io_setup(),
696 * for counting against the global limit -- before it changes.
697 */
698 unsigned int max_reqs = nr_events;
699
700 /*
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000701 * We keep track of the number of available ringbuffer slots, to prevent
702 * overflow (reqs_available), and we also use percpu counters for this.
703 *
704 * So since up to half the slots might be on other cpu's percpu counters
705 * and unavailable, double nr_events so userspace sees what they
706 * expected: additionally, we move req_batch slots to/from percpu
707 * counters at a time, so make sure that isn't 0:
708 */
709 nr_events = max(nr_events, num_possible_cpus() * 4);
710 nr_events *= 2;
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* Prevent overflows */
Al Viro08397ac2015-03-31 11:43:52 -0400713 if (nr_events > (0x10000000U / sizeof(struct io_event))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 pr_debug("ENOMEM: nr_events too high\n");
715 return ERR_PTR(-EINVAL);
716 }
717
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300718 if (!nr_events || (unsigned long)max_reqs > aio_max_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return ERR_PTR(-EAGAIN);
720
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800721 ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (!ctx)
723 return ERR_PTR(-ENOMEM);
724
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300725 ctx->max_reqs = max_reqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400727 spin_lock_init(&ctx->ctx_lock);
728 spin_lock_init(&ctx->completion_lock);
729 mutex_init(&ctx->ring_lock);
730 /* Protect against page migration throughout kiotx setup by keeping
731 * the ring_lock mutex held until setup is complete. */
732 mutex_lock(&ctx->ring_lock);
733 init_waitqueue_head(&ctx->wait);
734
735 INIT_LIST_HEAD(&ctx->active_reqs);
736
Tejun Heo2aad2a82014-09-24 13:31:50 -0400737 if (percpu_ref_init(&ctx->users, free_ioctx_users, 0, GFP_KERNEL))
Kent Overstreete34ecee2013-10-10 19:31:47 -0700738 goto err;
739
Tejun Heo2aad2a82014-09-24 13:31:50 -0400740 if (percpu_ref_init(&ctx->reqs, free_ioctx_reqs, 0, GFP_KERNEL))
Kent Overstreete34ecee2013-10-10 19:31:47 -0700741 goto err;
Kent Overstreet723be6e2013-05-28 15:14:48 -0700742
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000743 ctx->cpu = alloc_percpu(struct kioctx_cpu);
744 if (!ctx->cpu)
Kent Overstreete34ecee2013-10-10 19:31:47 -0700745 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300747 err = aio_setup_ring(ctx, nr_events);
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400748 if (err < 0)
Kent Overstreete34ecee2013-10-10 19:31:47 -0700749 goto err;
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000750
Kent Overstreet34e83fc2013-04-26 10:58:39 +1000751 atomic_set(&ctx->reqs_available, ctx->nr_events - 1);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000752 ctx->req_batch = (ctx->nr_events - 1) / (num_possible_cpus() * 4);
Benjamin LaHaise6878ea72013-07-31 10:34:18 -0400753 if (ctx->req_batch < 1)
754 ctx->req_batch = 1;
Kent Overstreet34e83fc2013-04-26 10:58:39 +1000755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /* limit the number of system wide aios */
Al Viro9fa1cb32012-03-10 23:14:05 -0500757 spin_lock(&aio_nr_lock);
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300758 if (aio_nr + ctx->max_reqs > aio_max_nr ||
759 aio_nr + ctx->max_reqs < aio_nr) {
Al Viro9fa1cb32012-03-10 23:14:05 -0500760 spin_unlock(&aio_nr_lock);
Kent Overstreete34ecee2013-10-10 19:31:47 -0700761 err = -EAGAIN;
Gu Zhengd1b94322013-12-04 18:19:06 +0800762 goto err_ctx;
Al Viro2dd542b2012-03-10 23:10:35 -0500763 }
764 aio_nr += ctx->max_reqs;
Al Viro9fa1cb32012-03-10 23:14:05 -0500765 spin_unlock(&aio_nr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Benjamin LaHaise18816862013-12-21 15:49:28 -0500767 percpu_ref_get(&ctx->users); /* io_setup() will drop this ref */
768 percpu_ref_get(&ctx->reqs); /* free_ioctx_users() will drop this */
Kent Overstreet723be6e2013-05-28 15:14:48 -0700769
Benjamin LaHaiseda903822013-08-05 13:21:43 -0400770 err = ioctx_add_table(ctx, mm);
771 if (err)
Kent Overstreete34ecee2013-10-10 19:31:47 -0700772 goto err_cleanup;
Benjamin LaHaiseda903822013-08-05 13:21:43 -0400773
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400774 /* Release the ring_lock mutex now that all setup is complete. */
775 mutex_unlock(&ctx->ring_lock);
776
Kent Overstreetcaf41672013-05-07 16:18:35 -0700777 pr_debug("allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700778 ctx, ctx->user_id, mm, ctx->nr_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return ctx;
780
Kent Overstreete34ecee2013-10-10 19:31:47 -0700781err_cleanup:
782 aio_nr_sub(ctx->max_reqs);
Gu Zhengd1b94322013-12-04 18:19:06 +0800783err_ctx:
Al Virodeeb8522015-04-06 17:57:44 -0400784 atomic_set(&ctx->dead, 1);
785 if (ctx->mmap_size)
786 vm_munmap(ctx->mmap_base, ctx->mmap_size);
Gu Zhengd1b94322013-12-04 18:19:06 +0800787 aio_free_ring(ctx);
Kent Overstreete34ecee2013-10-10 19:31:47 -0700788err:
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400789 mutex_unlock(&ctx->ring_lock);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000790 free_percpu(ctx->cpu);
Tejun Heo9a1049d2014-06-28 08:10:14 -0400791 percpu_ref_exit(&ctx->reqs);
792 percpu_ref_exit(&ctx->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 kmem_cache_free(kioctx_cachep, ctx);
Kent Overstreetcaf41672013-05-07 16:18:35 -0700794 pr_debug("error allocating ioctx %d\n", err);
Al Viroe23754f2012-03-06 14:33:22 -0500795 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
Kent Overstreet36f55882013-05-07 16:18:41 -0700798/* kill_ioctx
799 * Cancels all outstanding aio requests on an aio context. Used
800 * when the processes owning a context have all exited to encourage
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 * the rapid destruction of the kioctx.
802 */
Benjamin LaHaisefb2d4482014-04-29 12:45:17 -0400803static int kill_ioctx(struct mm_struct *mm, struct kioctx *ctx,
Jens Axboedc48e562015-04-15 11:17:23 -0600804 struct ctx_rq_wait *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400806 struct kioctx_table *table;
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400807
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400808 spin_lock(&mm->ioctx_lock);
Al Virob2edffd2015-04-06 17:48:54 -0400809 if (atomic_xchg(&ctx->dead, 1)) {
810 spin_unlock(&mm->ioctx_lock);
811 return -EINVAL;
812 }
813
Oleg Nesterov855ef0d2014-04-30 16:16:36 +0200814 table = rcu_dereference_raw(mm->ioctx_table);
Tejun Heod0264c02018-03-14 12:10:17 -0700815 WARN_ON(ctx != rcu_access_pointer(table->table[ctx->id]));
816 RCU_INIT_POINTER(table->table[ctx->id], NULL);
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400817 spin_unlock(&mm->ioctx_lock);
Kent Overstreet4fcc7122013-06-12 14:04:59 -0700818
Tejun Heoa6d7cff2018-03-14 12:10:17 -0700819 /* free_ioctx_reqs() will do the necessary RCU synchronization */
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400820 wake_up_all(&ctx->wait);
Kent Overstreet4fcc7122013-06-12 14:04:59 -0700821
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400822 /*
823 * It'd be more correct to do this in free_ioctx(), after all
824 * the outstanding kiocbs have finished - but by then io_destroy
825 * has already returned, so io_setup() could potentially return
826 * -EAGAIN with no ioctxs actually in use (as far as userspace
827 * could tell).
828 */
829 aio_nr_sub(ctx->max_reqs);
Benjamin LaHaisefb2d4482014-04-29 12:45:17 -0400830
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400831 if (ctx->mmap_size)
832 vm_munmap(ctx->mmap_base, ctx->mmap_size);
833
Jens Axboedc48e562015-04-15 11:17:23 -0600834 ctx->rq_wait = wait;
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400835 percpu_ref_kill(&ctx->users);
836 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Kent Overstreet36f55882013-05-07 16:18:41 -0700839/*
840 * exit_aio: called when the last user of mm goes away. At this point, there is
841 * no way for any new requests to be submited or any of the io_* syscalls to be
842 * called on the context.
843 *
844 * There may be outstanding kiocbs, but free_ioctx() will explicitly wait on
845 * them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800847void exit_aio(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200849 struct kioctx_table *table = rcu_dereference_raw(mm->ioctx_table);
Jens Axboedc48e562015-04-15 11:17:23 -0600850 struct ctx_rq_wait wait;
851 int i, skipped;
Jens Axboeabf137d2008-12-09 08:11:22 +0100852
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200853 if (!table)
854 return;
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400855
Jens Axboedc48e562015-04-15 11:17:23 -0600856 atomic_set(&wait.count, table->nr);
857 init_completion(&wait.comp);
858
859 skipped = 0;
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200860 for (i = 0; i < table->nr; ++i) {
Tejun Heod0264c02018-03-14 12:10:17 -0700861 struct kioctx *ctx =
862 rcu_dereference_protected(table->table[i], true);
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400863
Jens Axboedc48e562015-04-15 11:17:23 -0600864 if (!ctx) {
865 skipped++;
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200866 continue;
Jens Axboedc48e562015-04-15 11:17:23 -0600867 }
868
Al Viro936af152012-04-20 21:49:41 -0400869 /*
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200870 * We don't need to bother with munmap() here - exit_mmap(mm)
871 * is coming and it'll unmap everything. And we simply can't,
872 * this is not necessarily our ->mm.
873 * Since kill_ioctx() uses non-zero ->mmap_size as indicator
874 * that it needs to unmap the area, just set it to 0.
Al Viro936af152012-04-20 21:49:41 -0400875 */
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700876 ctx->mmap_size = 0;
Jens Axboedc48e562015-04-15 11:17:23 -0600877 kill_ioctx(mm, ctx, &wait);
878 }
Kent Overstreet36f55882013-05-07 16:18:41 -0700879
Jens Axboedc48e562015-04-15 11:17:23 -0600880 if (!atomic_sub_and_test(skipped, &wait.count)) {
Gu Zheng6098b452014-09-03 17:45:44 +0800881 /* Wait until all IO for the context are done. */
Jens Axboedc48e562015-04-15 11:17:23 -0600882 wait_for_completion(&wait.comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200884
885 RCU_INIT_POINTER(mm->ioctx_table, NULL);
886 kfree(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000889static void put_reqs_available(struct kioctx *ctx, unsigned nr)
890{
891 struct kioctx_cpu *kcpu;
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400892 unsigned long flags;
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000893
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400894 local_irq_save(flags);
Benjamin LaHaisebe6fb452014-07-22 09:56:56 -0400895 kcpu = this_cpu_ptr(ctx->cpu);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000896 kcpu->reqs_available += nr;
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400897
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000898 while (kcpu->reqs_available >= ctx->req_batch * 2) {
899 kcpu->reqs_available -= ctx->req_batch;
900 atomic_add(ctx->req_batch, &ctx->reqs_available);
901 }
902
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400903 local_irq_restore(flags);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000904}
905
906static bool get_reqs_available(struct kioctx *ctx)
907{
908 struct kioctx_cpu *kcpu;
909 bool ret = false;
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400910 unsigned long flags;
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000911
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400912 local_irq_save(flags);
Benjamin LaHaisebe6fb452014-07-22 09:56:56 -0400913 kcpu = this_cpu_ptr(ctx->cpu);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000914 if (!kcpu->reqs_available) {
915 int old, avail = atomic_read(&ctx->reqs_available);
916
917 do {
918 if (avail < ctx->req_batch)
919 goto out;
920
921 old = avail;
922 avail = atomic_cmpxchg(&ctx->reqs_available,
923 avail, avail - ctx->req_batch);
924 } while (avail != old);
925
926 kcpu->reqs_available += ctx->req_batch;
927 }
928
929 ret = true;
930 kcpu->reqs_available--;
931out:
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400932 local_irq_restore(flags);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000933 return ret;
934}
935
Benjamin LaHaised856f322014-08-24 13:14:05 -0400936/* refill_reqs_available
937 * Updates the reqs_available reference counts used for tracking the
938 * number of free slots in the completion ring. This can be called
939 * from aio_complete() (to optimistically update reqs_available) or
940 * from aio_get_req() (the we're out of events case). It must be
941 * called holding ctx->completion_lock.
942 */
943static void refill_reqs_available(struct kioctx *ctx, unsigned head,
944 unsigned tail)
945{
946 unsigned events_in_ring, completed;
947
948 /* Clamp head since userland can write to it. */
949 head %= ctx->nr_events;
950 if (head <= tail)
951 events_in_ring = tail - head;
952 else
953 events_in_ring = ctx->nr_events - (head - tail);
954
955 completed = ctx->completed_events;
956 if (events_in_ring < completed)
957 completed -= events_in_ring;
958 else
959 completed = 0;
960
961 if (!completed)
962 return;
963
964 ctx->completed_events -= completed;
965 put_reqs_available(ctx, completed);
966}
967
968/* user_refill_reqs_available
969 * Called to refill reqs_available when aio_get_req() encounters an
970 * out of space in the completion ring.
971 */
972static void user_refill_reqs_available(struct kioctx *ctx)
973{
974 spin_lock_irq(&ctx->completion_lock);
975 if (ctx->completed_events) {
976 struct aio_ring *ring;
977 unsigned head;
978
979 /* Access of ring->head may race with aio_read_events_ring()
980 * here, but that's okay since whether we read the old version
981 * or the new version, and either will be valid. The important
982 * part is that head cannot pass tail since we prevent
983 * aio_complete() from updating tail by holding
984 * ctx->completion_lock. Even if head is invalid, the check
985 * against ctx->completed_events below will make sure we do the
986 * safe/right thing.
987 */
988 ring = kmap_atomic(ctx->ring_pages[0]);
989 head = ring->head;
990 kunmap_atomic(ring);
991
992 refill_reqs_available(ctx, head, ctx->tail);
993 }
994
995 spin_unlock_irq(&ctx->completion_lock);
996}
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998/* aio_get_req
Kent Overstreet57282d82013-05-13 13:42:52 -0700999 * Allocate a slot for an aio request.
1000 * Returns NULL if no requests are free.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 */
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001002static inline struct aio_kiocb *aio_get_req(struct kioctx *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001004 struct aio_kiocb *req;
Kent Overstreeta1c8eae2013-05-07 16:18:53 -07001005
Benjamin LaHaised856f322014-08-24 13:14:05 -04001006 if (!get_reqs_available(ctx)) {
1007 user_refill_reqs_available(ctx);
1008 if (!get_reqs_available(ctx))
1009 return NULL;
1010 }
Kent Overstreeta1c8eae2013-05-07 16:18:53 -07001011
Kent Overstreet0460fef2013-05-07 16:18:49 -07001012 req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL|__GFP_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (unlikely(!req))
Kent Overstreeta1c8eae2013-05-07 16:18:53 -07001014 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Kent Overstreete34ecee2013-10-10 19:31:47 -07001016 percpu_ref_get(&ctx->reqs);
Christoph Hellwig75321b52018-04-09 14:57:56 +02001017 INIT_LIST_HEAD(&req->ki_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 req->ki_ctx = ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return req;
Kent Overstreeta1c8eae2013-05-07 16:18:53 -07001020out_put:
Kent Overstreete1bdd5f2013-04-26 10:58:39 +10001021 put_reqs_available(ctx, 1);
Kent Overstreeta1c8eae2013-05-07 16:18:53 -07001022 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
Adrian Bunkd5470b52008-04-29 00:58:57 -07001025static struct kioctx *lookup_ioctx(unsigned long ctx_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Benjamin LaHaisedb446a02013-07-30 12:54:40 -04001027 struct aio_ring __user *ring = (void __user *)ctx_id;
Jens Axboeabf137d2008-12-09 08:11:22 +01001028 struct mm_struct *mm = current->mm;
Jeff Moyer65c24492009-03-18 17:04:21 -07001029 struct kioctx *ctx, *ret = NULL;
Benjamin LaHaisedb446a02013-07-30 12:54:40 -04001030 struct kioctx_table *table;
1031 unsigned id;
1032
1033 if (get_user(id, &ring->id))
1034 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Jens Axboeabf137d2008-12-09 08:11:22 +01001036 rcu_read_lock();
Benjamin LaHaisedb446a02013-07-30 12:54:40 -04001037 table = rcu_dereference(mm->ioctx_table);
Jens Axboeabf137d2008-12-09 08:11:22 +01001038
Benjamin LaHaisedb446a02013-07-30 12:54:40 -04001039 if (!table || id >= table->nr)
1040 goto out;
1041
Tejun Heod0264c02018-03-14 12:10:17 -07001042 ctx = rcu_dereference(table->table[id]);
Benjamin LaHaisef30d7042013-08-07 18:23:48 -04001043 if (ctx && ctx->user_id == ctx_id) {
Al Virobaf10562018-05-20 16:46:23 -04001044 if (percpu_ref_tryget_live(&ctx->users))
1045 ret = ctx;
Jens Axboeabf137d2008-12-09 08:11:22 +01001046 }
Benjamin LaHaisedb446a02013-07-30 12:54:40 -04001047out:
Jens Axboeabf137d2008-12-09 08:11:22 +01001048 rcu_read_unlock();
Jeff Moyer65c24492009-03-18 17:04:21 -07001049 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052/* aio_complete
1053 * Called when the io request on the given iocb is complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 */
Christoph Hellwig54843f82018-05-02 19:57:21 +02001055static void aio_complete(struct aio_kiocb *iocb, long res, long res2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 struct kioctx *ctx = iocb->ki_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 struct aio_ring *ring;
Kent Overstreet21b40202013-05-07 16:18:47 -07001059 struct io_event *ev_page, *event;
Benjamin LaHaised856f322014-08-24 13:14:05 -04001060 unsigned tail, pos, head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Christoph Hellwig75321b52018-04-09 14:57:56 +02001063 if (!list_empty_careful(&iocb->ki_list)) {
Kent Overstreet0460fef2013-05-07 16:18:49 -07001064 unsigned long flags;
1065
1066 spin_lock_irqsave(&ctx->ctx_lock, flags);
1067 list_del(&iocb->ki_list);
1068 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
1069 }
Kent Overstreet11599eb2013-05-07 16:18:39 -07001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /*
Kent Overstreet0460fef2013-05-07 16:18:49 -07001072 * Add a completion event to the ring buffer. Must be done holding
Tang Chen4b30f072013-07-03 15:09:16 -07001073 * ctx->completion_lock to prevent other code from messing with the tail
Kent Overstreet0460fef2013-05-07 16:18:49 -07001074 * pointer since we might be called from irq context.
1075 */
1076 spin_lock_irqsave(&ctx->completion_lock, flags);
1077
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001078 tail = ctx->tail;
Kent Overstreet21b40202013-05-07 16:18:47 -07001079 pos = tail + AIO_EVENTS_OFFSET;
1080
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001081 if (++tail >= ctx->nr_events)
Ken Chen4bf69b22005-05-01 08:59:15 -07001082 tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001084 ev_page = kmap_atomic(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
Kent Overstreet21b40202013-05-07 16:18:47 -07001085 event = ev_page + pos % AIO_EVENTS_PER_PAGE;
1086
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001087 event->obj = (u64)(unsigned long)iocb->ki_user_iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 event->data = iocb->ki_user_data;
1089 event->res = res;
1090 event->res2 = res2;
1091
Kent Overstreet21b40202013-05-07 16:18:47 -07001092 kunmap_atomic(ev_page);
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001093 flush_dcache_page(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
Kent Overstreet21b40202013-05-07 16:18:47 -07001094
1095 pr_debug("%p[%u]: %p: %p %Lx %lx %lx\n",
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001096 ctx, tail, iocb, iocb->ki_user_iocb, iocb->ki_user_data,
Kent Overstreetcaf41672013-05-07 16:18:35 -07001097 res, res2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 /* after flagging the request as done, we
1100 * must never even look at it again
1101 */
1102 smp_wmb(); /* make event visible before updating tail */
1103
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001104 ctx->tail = tail;
Kent Overstreet21b40202013-05-07 16:18:47 -07001105
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001106 ring = kmap_atomic(ctx->ring_pages[0]);
Benjamin LaHaised856f322014-08-24 13:14:05 -04001107 head = ring->head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 ring->tail = tail;
Cong Wange8e3c3d2011-11-25 23:14:27 +08001109 kunmap_atomic(ring);
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001110 flush_dcache_page(ctx->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Benjamin LaHaised856f322014-08-24 13:14:05 -04001112 ctx->completed_events++;
1113 if (ctx->completed_events > 1)
1114 refill_reqs_available(ctx, head, tail);
Kent Overstreet0460fef2013-05-07 16:18:49 -07001115 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1116
Kent Overstreet21b40202013-05-07 16:18:47 -07001117 pr_debug("added to ring %p at [%u]\n", iocb, tail);
Davide Libenzi8d1c98b2008-04-10 21:29:19 -07001118
1119 /*
1120 * Check if the user asked us to deliver the result through an
1121 * eventfd. The eventfd_signal() function is safe to be called
1122 * from IRQ context.
1123 */
Christoph Hellwig54843f82018-05-02 19:57:21 +02001124 if (iocb->ki_eventfd) {
Davide Libenzi8d1c98b2008-04-10 21:29:19 -07001125 eventfd_signal(iocb->ki_eventfd, 1);
Christoph Hellwig54843f82018-05-02 19:57:21 +02001126 eventfd_ctx_put(iocb->ki_eventfd);
1127 }
Davide Libenzi8d1c98b2008-04-10 21:29:19 -07001128
Christoph Hellwig54843f82018-05-02 19:57:21 +02001129 kmem_cache_free(kiocb_cachep, iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Quentin Barnes6cb2a212008-03-19 17:00:39 -07001131 /*
1132 * We have to order our ring_info tail store above and test
1133 * of the wait list below outside the wait lock. This is
1134 * like in wake_up_bit() where clearing a bit has to be
1135 * ordered with the unlocked test.
1136 */
1137 smp_mb();
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (waitqueue_active(&ctx->wait))
1140 wake_up(&ctx->wait);
1141
Kent Overstreete34ecee2013-10-10 19:31:47 -07001142 percpu_ref_put(&ctx->reqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
Gu Zheng2be4e7d2014-07-23 18:03:53 +08001145/* aio_read_events_ring
Kent Overstreeta31ad382013-05-07 16:18:45 -07001146 * Pull an event off of the ioctx's event ring. Returns the number of
1147 * events fetched
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 */
Kent Overstreeta31ad382013-05-07 16:18:45 -07001149static long aio_read_events_ring(struct kioctx *ctx,
1150 struct io_event __user *event, long nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 struct aio_ring *ring;
Kent Overstreet5ffac122013-05-09 15:36:07 -07001153 unsigned head, tail, pos;
Kent Overstreeta31ad382013-05-07 16:18:45 -07001154 long ret = 0;
1155 int copy_ret;
1156
Dave Chinner9c9ce762015-02-03 19:29:05 -05001157 /*
1158 * The mutex can block and wake us up and that will cause
1159 * wait_event_interruptible_hrtimeout() to schedule without sleeping
1160 * and repeat. This should be rare enough that it doesn't cause
1161 * peformance issues. See the comment in read_events() for more detail.
1162 */
1163 sched_annotate_sleep();
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001164 mutex_lock(&ctx->ring_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -04001166 /* Access to ->ring_pages here is protected by ctx->ring_lock. */
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001167 ring = kmap_atomic(ctx->ring_pages[0]);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001168 head = ring->head;
Kent Overstreet5ffac122013-05-09 15:36:07 -07001169 tail = ring->tail;
Kent Overstreeta31ad382013-05-07 16:18:45 -07001170 kunmap_atomic(ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Jeff Moyer2ff396be2014-09-02 13:17:00 -04001172 /*
1173 * Ensure that once we've read the current tail pointer, that
1174 * we also see the events that were stored up to the tail.
1175 */
1176 smp_rmb();
1177
Kent Overstreet5ffac122013-05-09 15:36:07 -07001178 pr_debug("h%u t%u m%u\n", head, tail, ctx->nr_events);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001179
Kent Overstreet5ffac122013-05-09 15:36:07 -07001180 if (head == tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 goto out;
1182
Benjamin LaHaiseedfbbf32014-06-24 13:32:51 -04001183 head %= ctx->nr_events;
1184 tail %= ctx->nr_events;
1185
Kent Overstreeta31ad382013-05-07 16:18:45 -07001186 while (ret < nr) {
1187 long avail;
1188 struct io_event *ev;
1189 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Kent Overstreet5ffac122013-05-09 15:36:07 -07001191 avail = (head <= tail ? tail : ctx->nr_events) - head;
1192 if (head == tail)
Kent Overstreeta31ad382013-05-07 16:18:45 -07001193 break;
1194
1195 avail = min(avail, nr - ret);
1196 avail = min_t(long, avail, AIO_EVENTS_PER_PAGE -
1197 ((head + AIO_EVENTS_OFFSET) % AIO_EVENTS_PER_PAGE));
1198
1199 pos = head + AIO_EVENTS_OFFSET;
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001200 page = ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE];
Kent Overstreeta31ad382013-05-07 16:18:45 -07001201 pos %= AIO_EVENTS_PER_PAGE;
1202
1203 ev = kmap(page);
1204 copy_ret = copy_to_user(event + ret, ev + pos,
1205 sizeof(*ev) * avail);
1206 kunmap(page);
1207
1208 if (unlikely(copy_ret)) {
1209 ret = -EFAULT;
1210 goto out;
1211 }
1212
1213 ret += avail;
1214 head += avail;
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001215 head %= ctx->nr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001218 ring = kmap_atomic(ctx->ring_pages[0]);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001219 ring->head = head;
Zhao Hongjiang91d80a82013-04-26 11:03:53 +08001220 kunmap_atomic(ring);
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001221 flush_dcache_page(ctx->ring_pages[0]);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001222
Kent Overstreet5ffac122013-05-09 15:36:07 -07001223 pr_debug("%li h%u t%u\n", ret, head, tail);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001224out:
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001225 mutex_unlock(&ctx->ring_lock);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return ret;
1228}
1229
Kent Overstreeta31ad382013-05-07 16:18:45 -07001230static bool aio_read_events(struct kioctx *ctx, long min_nr, long nr,
1231 struct io_event __user *event, long *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Kent Overstreeta31ad382013-05-07 16:18:45 -07001233 long ret = aio_read_events_ring(ctx, event + *i, nr - *i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Kent Overstreeta31ad382013-05-07 16:18:45 -07001235 if (ret > 0)
1236 *i += ret;
1237
1238 if (unlikely(atomic_read(&ctx->dead)))
1239 ret = -EINVAL;
1240
1241 if (!*i)
1242 *i = ret;
1243
1244 return ret < 0 || *i >= min_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
Kent Overstreeta31ad382013-05-07 16:18:45 -07001247static long read_events(struct kioctx *ctx, long min_nr, long nr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 struct io_event __user *event,
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07001249 ktime_t until)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Kent Overstreeta31ad382013-05-07 16:18:45 -07001251 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Kent Overstreeta31ad382013-05-07 16:18:45 -07001253 /*
1254 * Note that aio_read_events() is being called as the conditional - i.e.
1255 * we're calling it after prepare_to_wait() has set task state to
1256 * TASK_INTERRUPTIBLE.
1257 *
1258 * But aio_read_events() can block, and if it blocks it's going to flip
1259 * the task state back to TASK_RUNNING.
1260 *
1261 * This should be ok, provided it doesn't flip the state back to
1262 * TASK_RUNNING and return 0 too much - that causes us to spin. That
1263 * will only happen if the mutex_lock() call blocks, and we then find
1264 * the ringbuffer empty. So in practice we should be ok, but it's
1265 * something to be aware of when touching this code.
1266 */
Thomas Gleixner2456e852016-12-25 11:38:40 +01001267 if (until == 0)
Fam Zheng5f785de2014-11-06 20:44:36 +08001268 aio_read_events(ctx, min_nr, nr, event, &ret);
1269 else
1270 wait_event_interruptible_hrtimeout(ctx->wait,
1271 aio_read_events(ctx, min_nr, nr, event, &ret),
1272 until);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001273 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276/* sys_io_setup:
1277 * Create an aio_context capable of receiving at least nr_events.
1278 * ctxp must not point to an aio_context that already exists, and
1279 * must be initialized to 0 prior to the call. On successful
1280 * creation of the aio_context, *ctxp is filled in with the resulting
1281 * handle. May fail with -EINVAL if *ctxp is not initialized,
1282 * if the specified nr_events exceeds internal limits. May fail
1283 * with -EAGAIN if the specified nr_events exceeds the user's limit
1284 * of available events. May fail with -ENOMEM if insufficient kernel
1285 * resources are available. May fail with -EFAULT if an invalid
1286 * pointer is passed for ctxp. Will fail with -ENOSYS if not
1287 * implemented.
1288 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001289SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 struct kioctx *ioctx = NULL;
1292 unsigned long ctx;
1293 long ret;
1294
1295 ret = get_user(ctx, ctxp);
1296 if (unlikely(ret))
1297 goto out;
1298
1299 ret = -EINVAL;
Zach Brownd55b5fd2005-11-07 00:59:31 -08001300 if (unlikely(ctx || nr_events == 0)) {
Kinglong Meeacd88d42015-02-04 21:15:59 +08001301 pr_debug("EINVAL: ctx %lu nr_events %u\n",
Zach Brownd55b5fd2005-11-07 00:59:31 -08001302 ctx, nr_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 goto out;
1304 }
1305
1306 ioctx = ioctx_alloc(nr_events);
1307 ret = PTR_ERR(ioctx);
1308 if (!IS_ERR(ioctx)) {
1309 ret = put_user(ioctx->user_id, ctxp);
Al Viroa2e18592012-03-20 16:27:57 -04001310 if (ret)
Anatol Pomozove02ba722014-04-15 11:31:33 -07001311 kill_ioctx(current->mm, ioctx, NULL);
Kent Overstreet723be6e2013-05-28 15:14:48 -07001312 percpu_ref_put(&ioctx->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
1314
1315out:
1316 return ret;
1317}
1318
Al Viroc00d2c72016-12-20 07:04:57 -05001319#ifdef CONFIG_COMPAT
1320COMPAT_SYSCALL_DEFINE2(io_setup, unsigned, nr_events, u32 __user *, ctx32p)
1321{
1322 struct kioctx *ioctx = NULL;
1323 unsigned long ctx;
1324 long ret;
1325
1326 ret = get_user(ctx, ctx32p);
1327 if (unlikely(ret))
1328 goto out;
1329
1330 ret = -EINVAL;
1331 if (unlikely(ctx || nr_events == 0)) {
1332 pr_debug("EINVAL: ctx %lu nr_events %u\n",
1333 ctx, nr_events);
1334 goto out;
1335 }
1336
1337 ioctx = ioctx_alloc(nr_events);
1338 ret = PTR_ERR(ioctx);
1339 if (!IS_ERR(ioctx)) {
1340 /* truncating is ok because it's a user address */
1341 ret = put_user((u32)ioctx->user_id, ctx32p);
1342 if (ret)
1343 kill_ioctx(current->mm, ioctx, NULL);
1344 percpu_ref_put(&ioctx->users);
1345 }
1346
1347out:
1348 return ret;
1349}
1350#endif
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352/* sys_io_destroy:
1353 * Destroy the aio_context specified. May cancel any outstanding
1354 * AIOs and block on completion. Will fail with -ENOSYS if not
Satoru Takeuchi642b5122010-08-05 11:23:11 -07001355 * implemented. May fail with -EINVAL if the context pointed to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 * is invalid.
1357 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001358SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
1360 struct kioctx *ioctx = lookup_ioctx(ctx);
1361 if (likely(NULL != ioctx)) {
Jens Axboedc48e562015-04-15 11:17:23 -06001362 struct ctx_rq_wait wait;
Benjamin LaHaisefb2d4482014-04-29 12:45:17 -04001363 int ret;
Anatol Pomozove02ba722014-04-15 11:31:33 -07001364
Jens Axboedc48e562015-04-15 11:17:23 -06001365 init_completion(&wait.comp);
1366 atomic_set(&wait.count, 1);
1367
Anatol Pomozove02ba722014-04-15 11:31:33 -07001368 /* Pass requests_done to kill_ioctx() where it can be set
1369 * in a thread-safe way. If we try to set it here then we have
1370 * a race condition if two io_destroy() called simultaneously.
1371 */
Jens Axboedc48e562015-04-15 11:17:23 -06001372 ret = kill_ioctx(current->mm, ioctx, &wait);
Kent Overstreet723be6e2013-05-28 15:14:48 -07001373 percpu_ref_put(&ioctx->users);
Anatol Pomozove02ba722014-04-15 11:31:33 -07001374
1375 /* Wait until all IO for the context are done. Otherwise kernel
1376 * keep using user-space buffers even if user thinks the context
1377 * is destroyed.
1378 */
Benjamin LaHaisefb2d4482014-04-29 12:45:17 -04001379 if (!ret)
Jens Axboedc48e562015-04-15 11:17:23 -06001380 wait_for_completion(&wait.comp);
Anatol Pomozove02ba722014-04-15 11:31:33 -07001381
Benjamin LaHaisefb2d4482014-04-29 12:45:17 -04001382 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
Kinglong Meeacd88d42015-02-04 21:15:59 +08001384 pr_debug("EINVAL: invalid context id\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return -EINVAL;
1386}
1387
Christoph Hellwig54843f82018-05-02 19:57:21 +02001388static void aio_complete_rw(struct kiocb *kiocb, long res, long res2)
1389{
1390 struct aio_kiocb *iocb = container_of(kiocb, struct aio_kiocb, rw);
1391
1392 if (kiocb->ki_flags & IOCB_WRITE) {
1393 struct inode *inode = file_inode(kiocb->ki_filp);
1394
1395 /*
1396 * Tell lockdep we inherited freeze protection from submission
1397 * thread.
1398 */
1399 if (S_ISREG(inode->i_mode))
1400 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
1401 file_end_write(kiocb->ki_filp);
1402 }
1403
1404 fput(kiocb->ki_filp);
1405 aio_complete(iocb, res, res2);
1406}
1407
1408static int aio_prep_rw(struct kiocb *req, struct iocb *iocb)
1409{
1410 int ret;
1411
1412 req->ki_filp = fget(iocb->aio_fildes);
1413 if (unlikely(!req->ki_filp))
1414 return -EBADF;
1415 req->ki_complete = aio_complete_rw;
1416 req->ki_pos = iocb->aio_offset;
1417 req->ki_flags = iocb_flags(req->ki_filp);
1418 if (iocb->aio_flags & IOCB_FLAG_RESFD)
1419 req->ki_flags |= IOCB_EVENTFD;
1420 req->ki_hint = file_write_hint(req->ki_filp);
1421 ret = kiocb_set_rw_flags(req, iocb->aio_rw_flags);
1422 if (unlikely(ret))
1423 fput(req->ki_filp);
1424 return ret;
1425}
1426
Christoph Hellwig89319d312016-10-30 11:42:03 -05001427static int aio_setup_rw(int rw, struct iocb *iocb, struct iovec **iovec,
1428 bool vectored, bool compat, struct iov_iter *iter)
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001429{
Christoph Hellwig89319d312016-10-30 11:42:03 -05001430 void __user *buf = (void __user *)(uintptr_t)iocb->aio_buf;
1431 size_t len = iocb->aio_nbytes;
1432
1433 if (!vectored) {
1434 ssize_t ret = import_single_range(rw, buf, len, *iovec, iter);
1435 *iovec = NULL;
1436 return ret;
1437 }
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001438#ifdef CONFIG_COMPAT
1439 if (compat)
Christoph Hellwig89319d312016-10-30 11:42:03 -05001440 return compat_import_iovec(rw, buf, len, UIO_FASTIOV, iovec,
1441 iter);
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001442#endif
Christoph Hellwig89319d312016-10-30 11:42:03 -05001443 return import_iovec(rw, buf, len, UIO_FASTIOV, iovec, iter);
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001444}
1445
Christoph Hellwig54843f82018-05-02 19:57:21 +02001446static inline ssize_t aio_rw_ret(struct kiocb *req, ssize_t ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Christoph Hellwig89319d312016-10-30 11:42:03 -05001448 switch (ret) {
1449 case -EIOCBQUEUED:
1450 return ret;
1451 case -ERESTARTSYS:
1452 case -ERESTARTNOINTR:
1453 case -ERESTARTNOHAND:
1454 case -ERESTART_RESTARTBLOCK:
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001455 /*
1456 * There's no easy way to restart the syscall since other AIO's
1457 * may be already running. Just fail this IO with EINTR.
1458 */
Christoph Hellwig89319d312016-10-30 11:42:03 -05001459 ret = -EINTR;
1460 /*FALLTHRU*/
1461 default:
Christoph Hellwig54843f82018-05-02 19:57:21 +02001462 aio_complete_rw(req, ret, 0);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001463 return 0;
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001464 }
Christoph Hellwig89319d312016-10-30 11:42:03 -05001465}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Christoph Hellwig89319d312016-10-30 11:42:03 -05001467static ssize_t aio_read(struct kiocb *req, struct iocb *iocb, bool vectored,
1468 bool compat)
1469{
Christoph Hellwig89319d312016-10-30 11:42:03 -05001470 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1471 struct iov_iter iter;
Christoph Hellwig54843f82018-05-02 19:57:21 +02001472 struct file *file;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001473 ssize_t ret;
1474
Christoph Hellwig54843f82018-05-02 19:57:21 +02001475 ret = aio_prep_rw(req, iocb);
1476 if (ret)
1477 return ret;
1478 file = req->ki_filp;
1479
1480 ret = -EBADF;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001481 if (unlikely(!(file->f_mode & FMODE_READ)))
Christoph Hellwig54843f82018-05-02 19:57:21 +02001482 goto out_fput;
1483 ret = -EINVAL;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001484 if (unlikely(!file->f_op->read_iter))
Christoph Hellwig54843f82018-05-02 19:57:21 +02001485 goto out_fput;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001486
1487 ret = aio_setup_rw(READ, iocb, &iovec, vectored, compat, &iter);
1488 if (ret)
Christoph Hellwig54843f82018-05-02 19:57:21 +02001489 goto out_fput;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001490 ret = rw_verify_area(READ, file, &req->ki_pos, iov_iter_count(&iter));
1491 if (!ret)
Christoph Hellwig54843f82018-05-02 19:57:21 +02001492 ret = aio_rw_ret(req, call_read_iter(file, req, &iter));
Christoph Hellwig89319d312016-10-30 11:42:03 -05001493 kfree(iovec);
Christoph Hellwig54843f82018-05-02 19:57:21 +02001494out_fput:
1495 if (unlikely(ret && ret != -EIOCBQUEUED))
1496 fput(file);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001497 return ret;
1498}
1499
1500static ssize_t aio_write(struct kiocb *req, struct iocb *iocb, bool vectored,
1501 bool compat)
1502{
Christoph Hellwig89319d312016-10-30 11:42:03 -05001503 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1504 struct iov_iter iter;
Christoph Hellwig54843f82018-05-02 19:57:21 +02001505 struct file *file;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001506 ssize_t ret;
1507
Christoph Hellwig54843f82018-05-02 19:57:21 +02001508 ret = aio_prep_rw(req, iocb);
1509 if (ret)
1510 return ret;
1511 file = req->ki_filp;
1512
1513 ret = -EBADF;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001514 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Christoph Hellwig54843f82018-05-02 19:57:21 +02001515 goto out_fput;
1516 ret = -EINVAL;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001517 if (unlikely(!file->f_op->write_iter))
Christoph Hellwig54843f82018-05-02 19:57:21 +02001518 goto out_fput;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001519
1520 ret = aio_setup_rw(WRITE, iocb, &iovec, vectored, compat, &iter);
1521 if (ret)
Christoph Hellwig54843f82018-05-02 19:57:21 +02001522 goto out_fput;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001523 ret = rw_verify_area(WRITE, file, &req->ki_pos, iov_iter_count(&iter));
1524 if (!ret) {
Jan Kara70fe2f42016-10-30 11:42:04 -05001525 /*
Christoph Hellwig92ce4722018-04-06 09:28:17 +02001526 * Open-code file_start_write here to grab freeze protection,
Christoph Hellwig54843f82018-05-02 19:57:21 +02001527 * which will be released by another thread in
1528 * aio_complete_rw(). Fool lockdep by telling it the lock got
1529 * released so that it doesn't complain about the held lock when
1530 * we return to userspace.
Jan Kara70fe2f42016-10-30 11:42:04 -05001531 */
Christoph Hellwig92ce4722018-04-06 09:28:17 +02001532 if (S_ISREG(file_inode(file)->i_mode)) {
1533 __sb_start_write(file_inode(file)->i_sb, SB_FREEZE_WRITE, true);
Shaohua Lia12f1ae2016-12-13 12:09:56 -08001534 __sb_writers_release(file_inode(file)->i_sb, SB_FREEZE_WRITE);
Christoph Hellwig92ce4722018-04-06 09:28:17 +02001535 }
1536 req->ki_flags |= IOCB_WRITE;
Christoph Hellwig54843f82018-05-02 19:57:21 +02001537 ret = aio_rw_ret(req, call_write_iter(file, req, &iter));
Christoph Hellwig89319d312016-10-30 11:42:03 -05001538 }
1539 kfree(iovec);
Christoph Hellwig54843f82018-05-02 19:57:21 +02001540out_fput:
1541 if (unlikely(ret && ret != -EIOCBQUEUED))
1542 fput(file);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001543 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
Christoph Hellwiga3c0d432018-03-27 19:18:57 +02001546static void aio_fsync_work(struct work_struct *work)
1547{
1548 struct fsync_iocb *req = container_of(work, struct fsync_iocb, work);
1549 int ret;
1550
1551 ret = vfs_fsync(req->file, req->datasync);
1552 fput(req->file);
1553 aio_complete(container_of(req, struct aio_kiocb, fsync), ret, 0);
1554}
1555
1556static int aio_fsync(struct fsync_iocb *req, struct iocb *iocb, bool datasync)
1557{
1558 if (unlikely(iocb->aio_buf || iocb->aio_offset || iocb->aio_nbytes ||
1559 iocb->aio_rw_flags))
1560 return -EINVAL;
1561
1562 req->file = fget(iocb->aio_fildes);
1563 if (unlikely(!req->file))
1564 return -EBADF;
1565 if (unlikely(!req->file->f_op->fsync)) {
1566 fput(req->file);
1567 return -EINVAL;
1568 }
1569
1570 req->datasync = datasync;
1571 INIT_WORK(&req->work, aio_fsync_work);
1572 schedule_work(&req->work);
1573 return -EIOCBQUEUED;
1574}
1575
Adrian Bunkd5470b52008-04-29 00:58:57 -07001576static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
Kent Overstreeta1c8eae2013-05-07 16:18:53 -07001577 struct iocb *iocb, bool compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001579 struct aio_kiocb *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 ssize_t ret;
1581
1582 /* enforce forwards compatibility on users */
Goldwyn Rodrigues9830f4b2017-06-20 07:05:42 -05001583 if (unlikely(iocb->aio_reserved2)) {
Kent Overstreetcaf41672013-05-07 16:18:35 -07001584 pr_debug("EINVAL: reserve field set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return -EINVAL;
1586 }
1587
1588 /* prevent overflows */
1589 if (unlikely(
1590 (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
1591 (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
1592 ((ssize_t)iocb->aio_nbytes < 0)
1593 )) {
Kinglong Meeacd88d42015-02-04 21:15:59 +08001594 pr_debug("EINVAL: overflow check\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return -EINVAL;
1596 }
1597
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001598 req = aio_get_req(ctx);
Kent Overstreet1d98ebf2013-05-07 16:18:37 -07001599 if (unlikely(!req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 return -EAGAIN;
Kent Overstreet1d98ebf2013-05-07 16:18:37 -07001601
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001602 if (iocb->aio_flags & IOCB_FLAG_RESFD) {
1603 /*
1604 * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
1605 * instance of the file* now. The file descriptor must be
1606 * an eventfd() fd, and will be signaled for each completed
1607 * event using the eventfd_signal() function.
1608 */
Davide Libenzi13389012009-06-30 11:41:11 -07001609 req->ki_eventfd = eventfd_ctx_fdget((int) iocb->aio_resfd);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07001610 if (IS_ERR(req->ki_eventfd)) {
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001611 ret = PTR_ERR(req->ki_eventfd);
Davide Libenzi87c3a862009-03-18 17:04:19 -07001612 req->ki_eventfd = NULL;
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001613 goto out_put_req;
1614 }
Goldwyn Rodrigues9830f4b2017-06-20 07:05:42 -05001615 }
1616
Kent Overstreet8a660892013-05-07 16:19:10 -07001617 ret = put_user(KIOCB_KEY, &user_iocb->aio_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (unlikely(ret)) {
Kent Overstreetcaf41672013-05-07 16:18:35 -07001619 pr_debug("EFAULT: aio_key\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 goto out_put_req;
1621 }
1622
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001623 req->ki_user_iocb = user_iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 req->ki_user_data = iocb->aio_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Christoph Hellwig89319d312016-10-30 11:42:03 -05001626 switch (iocb->aio_lio_opcode) {
1627 case IOCB_CMD_PREAD:
Christoph Hellwig54843f82018-05-02 19:57:21 +02001628 ret = aio_read(&req->rw, iocb, false, compat);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001629 break;
1630 case IOCB_CMD_PWRITE:
Christoph Hellwig54843f82018-05-02 19:57:21 +02001631 ret = aio_write(&req->rw, iocb, false, compat);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001632 break;
1633 case IOCB_CMD_PREADV:
Christoph Hellwig54843f82018-05-02 19:57:21 +02001634 ret = aio_read(&req->rw, iocb, true, compat);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001635 break;
1636 case IOCB_CMD_PWRITEV:
Christoph Hellwig54843f82018-05-02 19:57:21 +02001637 ret = aio_write(&req->rw, iocb, true, compat);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001638 break;
Christoph Hellwiga3c0d432018-03-27 19:18:57 +02001639 case IOCB_CMD_FSYNC:
1640 ret = aio_fsync(&req->fsync, iocb, false);
1641 break;
1642 case IOCB_CMD_FDSYNC:
1643 ret = aio_fsync(&req->fsync, iocb, true);
1644 break;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001645 default:
1646 pr_debug("invalid aio operation %d\n", iocb->aio_lio_opcode);
1647 ret = -EINVAL;
1648 break;
1649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Christoph Hellwig92ce4722018-04-06 09:28:17 +02001651 /*
1652 * If ret is -EIOCBQUEUED, ownership of the file reference acquired
1653 * above passed to the file system, which at this point might have
1654 * dropped the reference, so we must be careful to not reference it
1655 * once we have called into the file system.
1656 */
Christoph Hellwig89319d312016-10-30 11:42:03 -05001657 if (ret && ret != -EIOCBQUEUED)
1658 goto out_put_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660out_put_req:
Kent Overstreete1bdd5f2013-04-26 10:58:39 +10001661 put_reqs_available(ctx, 1);
Kent Overstreete34ecee2013-10-10 19:31:47 -07001662 percpu_ref_put(&ctx->reqs);
Christoph Hellwig54843f82018-05-02 19:57:21 +02001663 if (req->ki_eventfd)
1664 eventfd_ctx_put(req->ki_eventfd);
1665 kmem_cache_free(kiocb_cachep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return ret;
1667}
1668
Al Viroc00d2c72016-12-20 07:04:57 -05001669static long do_io_submit(aio_context_t ctx_id, long nr,
1670 struct iocb __user *__user *iocbpp, bool compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
1672 struct kioctx *ctx;
1673 long ret = 0;
Jeff Moyer080d6762011-11-02 13:40:10 -07001674 int i = 0;
Shaohua Li9f5b9422010-07-01 07:55:01 +02001675 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 if (unlikely(nr < 0))
1678 return -EINVAL;
1679
Jeff Moyer75e1c702010-09-10 14:16:00 -07001680 if (unlikely(nr > LONG_MAX/sizeof(*iocbpp)))
1681 nr = LONG_MAX/sizeof(*iocbpp);
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
1684 return -EFAULT;
1685
1686 ctx = lookup_ioctx(ctx_id);
1687 if (unlikely(!ctx)) {
Kent Overstreetcaf41672013-05-07 16:18:35 -07001688 pr_debug("EINVAL: invalid context id\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return -EINVAL;
1690 }
1691
Shaohua Li9f5b9422010-07-01 07:55:01 +02001692 blk_start_plug(&plug);
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 /*
1695 * AKPM: should this return a partial result if some of the IOs were
1696 * successfully submitted?
1697 */
1698 for (i=0; i<nr; i++) {
1699 struct iocb __user *user_iocb;
1700 struct iocb tmp;
1701
1702 if (unlikely(__get_user(user_iocb, iocbpp + i))) {
1703 ret = -EFAULT;
1704 break;
1705 }
1706
1707 if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
1708 ret = -EFAULT;
1709 break;
1710 }
1711
Kent Overstreeta1c8eae2013-05-07 16:18:53 -07001712 ret = io_submit_one(ctx, user_iocb, &tmp, compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if (ret)
1714 break;
1715 }
Shaohua Li9f5b9422010-07-01 07:55:01 +02001716 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Kent Overstreet723be6e2013-05-28 15:14:48 -07001718 percpu_ref_put(&ctx->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 return i ? i : ret;
1720}
1721
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001722/* sys_io_submit:
1723 * Queue the nr iocbs pointed to by iocbpp for processing. Returns
1724 * the number of iocbs queued. May return -EINVAL if the aio_context
1725 * specified by ctx_id is invalid, if nr is < 0, if the iocb at
1726 * *iocbpp[0] is not properly initialized, if the operation specified
1727 * is invalid for the file descriptor in the iocb. May fail with
1728 * -EFAULT if any of the data structures point to invalid data. May
1729 * fail with -EBADF if the file descriptor specified in the first
1730 * iocb is invalid. May fail with -EAGAIN if insufficient resources
1731 * are available to queue any iocbs. Will return 0 if nr is 0. Will
1732 * fail with -ENOSYS if not implemented.
1733 */
1734SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
1735 struct iocb __user * __user *, iocbpp)
1736{
1737 return do_io_submit(ctx_id, nr, iocbpp, 0);
1738}
1739
Al Viroc00d2c72016-12-20 07:04:57 -05001740#ifdef CONFIG_COMPAT
1741static inline long
1742copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
1743{
1744 compat_uptr_t uptr;
1745 int i;
1746
1747 for (i = 0; i < nr; ++i) {
1748 if (get_user(uptr, ptr32 + i))
1749 return -EFAULT;
1750 if (put_user(compat_ptr(uptr), ptr64 + i))
1751 return -EFAULT;
1752 }
1753 return 0;
1754}
1755
1756#define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *))
1757
1758COMPAT_SYSCALL_DEFINE3(io_submit, compat_aio_context_t, ctx_id,
1759 int, nr, u32 __user *, iocb)
1760{
1761 struct iocb __user * __user *iocb64;
1762 long ret;
1763
1764 if (unlikely(nr < 0))
1765 return -EINVAL;
1766
1767 if (nr > MAX_AIO_SUBMITS)
1768 nr = MAX_AIO_SUBMITS;
1769
1770 iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
1771 ret = copy_iocb(nr, iocb, iocb64);
1772 if (!ret)
1773 ret = do_io_submit(ctx_id, nr, iocb64, 1);
1774 return ret;
1775}
1776#endif
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778/* lookup_kiocb
1779 * Finds a given iocb for cancellation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 */
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001781static struct aio_kiocb *
Christoph Hellwigf3a27522018-03-30 11:19:25 +02001782lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001784 struct aio_kiocb *kiocb;
Zach Brownd00689a2005-11-13 16:07:34 -08001785
1786 assert_spin_locked(&ctx->ctx_lock);
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 /* TODO: use a hash or array, this sucks. */
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001789 list_for_each_entry(kiocb, &ctx->active_reqs, ki_list) {
1790 if (kiocb->ki_user_iocb == iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return kiocb;
1792 }
1793 return NULL;
1794}
1795
1796/* sys_io_cancel:
1797 * Attempts to cancel an iocb previously passed to io_submit. If
1798 * the operation is successfully cancelled, the resulting event is
1799 * copied into the memory pointed to by result without being placed
1800 * into the completion queue and 0 is returned. May fail with
1801 * -EFAULT if any of the data structures pointed to are invalid.
1802 * May fail with -EINVAL if aio_context specified by ctx_id is
1803 * invalid. May fail with -EAGAIN if the iocb specified was not
1804 * cancelled. Will fail with -ENOSYS if not implemented.
1805 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001806SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
1807 struct io_event __user *, result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 struct kioctx *ctx;
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001810 struct aio_kiocb *kiocb;
Christoph Hellwig888933f2018-05-23 14:11:02 +02001811 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 u32 key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Christoph Hellwigf3a27522018-03-30 11:19:25 +02001814 if (unlikely(get_user(key, &iocb->aio_key)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return -EFAULT;
Christoph Hellwigf3a27522018-03-30 11:19:25 +02001816 if (unlikely(key != KIOCB_KEY))
1817 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 ctx = lookup_ioctx(ctx_id);
1820 if (unlikely(!ctx))
1821 return -EINVAL;
1822
1823 spin_lock_irq(&ctx->ctx_lock);
Christoph Hellwigf3a27522018-03-30 11:19:25 +02001824 kiocb = lookup_kiocb(ctx, iocb);
Christoph Hellwig888933f2018-05-23 14:11:02 +02001825 if (kiocb) {
1826 ret = kiocb->ki_cancel(&kiocb->rw);
1827 list_del_init(&kiocb->ki_list);
1828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 spin_unlock_irq(&ctx->ctx_lock);
1830
Kent Overstreet906b9732013-05-07 16:18:31 -07001831 if (!ret) {
Kent Overstreetbec68faa2013-05-13 14:45:08 -07001832 /*
1833 * The result argument is no longer used - the io_event is
1834 * always delivered via the ring buffer. -EINPROGRESS indicates
1835 * cancellation is progress:
Kent Overstreet906b9732013-05-07 16:18:31 -07001836 */
Kent Overstreetbec68faa2013-05-13 14:45:08 -07001837 ret = -EINPROGRESS;
Kent Overstreet906b9732013-05-07 16:18:31 -07001838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Kent Overstreet723be6e2013-05-28 15:14:48 -07001840 percpu_ref_put(&ctx->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 return ret;
1843}
1844
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07001845static long do_io_getevents(aio_context_t ctx_id,
1846 long min_nr,
1847 long nr,
1848 struct io_event __user *events,
1849 struct timespec64 *ts)
1850{
1851 ktime_t until = ts ? timespec64_to_ktime(*ts) : KTIME_MAX;
1852 struct kioctx *ioctx = lookup_ioctx(ctx_id);
1853 long ret = -EINVAL;
1854
1855 if (likely(ioctx)) {
1856 if (likely(min_nr <= nr && min_nr >= 0))
1857 ret = read_events(ioctx, min_nr, nr, events, until);
1858 percpu_ref_put(&ioctx->users);
1859 }
1860
1861 return ret;
1862}
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864/* io_getevents:
1865 * Attempts to read at least min_nr events and up to nr events from
Satoru Takeuchi642b5122010-08-05 11:23:11 -07001866 * the completion queue for the aio_context specified by ctx_id. If
1867 * it succeeds, the number of read events is returned. May fail with
1868 * -EINVAL if ctx_id is invalid, if min_nr is out of range, if nr is
1869 * out of range, if timeout is out of range. May fail with -EFAULT
1870 * if any of the memory specified is invalid. May return 0 or
1871 * < min_nr if the timeout specified by timeout has elapsed
1872 * before sufficient events are available, where timeout == NULL
1873 * specifies an infinite timeout. Note that the timeout pointed to by
Jeff Moyer69008072013-05-24 15:55:24 -07001874 * timeout is relative. Will fail with -ENOSYS if not implemented.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001876SYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id,
1877 long, min_nr,
1878 long, nr,
1879 struct io_event __user *, events,
1880 struct timespec __user *, timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07001882 struct timespec64 ts;
Christoph Hellwig7a074e92018-05-02 19:51:00 +02001883 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Christoph Hellwig7a074e92018-05-02 19:51:00 +02001885 if (timeout && unlikely(get_timespec64(&ts, timeout)))
1886 return -EFAULT;
1887
1888 ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &ts : NULL);
1889 if (!ret && signal_pending(current))
1890 ret = -EINTR;
1891 return ret;
1892}
1893
1894SYSCALL_DEFINE6(io_pgetevents,
1895 aio_context_t, ctx_id,
1896 long, min_nr,
1897 long, nr,
1898 struct io_event __user *, events,
1899 struct timespec __user *, timeout,
1900 const struct __aio_sigset __user *, usig)
1901{
1902 struct __aio_sigset ksig = { NULL, };
1903 sigset_t ksigmask, sigsaved;
1904 struct timespec64 ts;
1905 int ret;
1906
1907 if (timeout && unlikely(get_timespec64(&ts, timeout)))
1908 return -EFAULT;
1909
1910 if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
1911 return -EFAULT;
1912
1913 if (ksig.sigmask) {
1914 if (ksig.sigsetsize != sizeof(sigset_t))
1915 return -EINVAL;
1916 if (copy_from_user(&ksigmask, ksig.sigmask, sizeof(ksigmask)))
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07001917 return -EFAULT;
Christoph Hellwig7a074e92018-05-02 19:51:00 +02001918 sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
1919 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07001921
Christoph Hellwig7a074e92018-05-02 19:51:00 +02001922 ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &ts : NULL);
1923 if (signal_pending(current)) {
1924 if (ksig.sigmask) {
1925 current->saved_sigmask = sigsaved;
1926 set_restore_sigmask();
1927 }
1928
1929 if (!ret)
1930 ret = -ERESTARTNOHAND;
1931 } else {
1932 if (ksig.sigmask)
1933 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1934 }
1935
1936 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937}
Al Viroc00d2c72016-12-20 07:04:57 -05001938
1939#ifdef CONFIG_COMPAT
1940COMPAT_SYSCALL_DEFINE5(io_getevents, compat_aio_context_t, ctx_id,
1941 compat_long_t, min_nr,
1942 compat_long_t, nr,
1943 struct io_event __user *, events,
1944 struct compat_timespec __user *, timeout)
1945{
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07001946 struct timespec64 t;
Christoph Hellwig7a074e92018-05-02 19:51:00 +02001947 int ret;
Al Viroc00d2c72016-12-20 07:04:57 -05001948
Christoph Hellwig7a074e92018-05-02 19:51:00 +02001949 if (timeout && compat_get_timespec64(&t, timeout))
1950 return -EFAULT;
1951
1952 ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &t : NULL);
1953 if (!ret && signal_pending(current))
1954 ret = -EINTR;
1955 return ret;
1956}
1957
1958
1959struct __compat_aio_sigset {
1960 compat_sigset_t __user *sigmask;
1961 compat_size_t sigsetsize;
1962};
1963
1964COMPAT_SYSCALL_DEFINE6(io_pgetevents,
1965 compat_aio_context_t, ctx_id,
1966 compat_long_t, min_nr,
1967 compat_long_t, nr,
1968 struct io_event __user *, events,
1969 struct compat_timespec __user *, timeout,
1970 const struct __compat_aio_sigset __user *, usig)
1971{
1972 struct __compat_aio_sigset ksig = { NULL, };
1973 sigset_t ksigmask, sigsaved;
1974 struct timespec64 t;
1975 int ret;
1976
1977 if (timeout && compat_get_timespec64(&t, timeout))
1978 return -EFAULT;
1979
1980 if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
1981 return -EFAULT;
1982
1983 if (ksig.sigmask) {
1984 if (ksig.sigsetsize != sizeof(compat_sigset_t))
1985 return -EINVAL;
1986 if (get_compat_sigset(&ksigmask, ksig.sigmask))
Al Viroc00d2c72016-12-20 07:04:57 -05001987 return -EFAULT;
Christoph Hellwig7a074e92018-05-02 19:51:00 +02001988 sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
1989 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
Al Viroc00d2c72016-12-20 07:04:57 -05001990 }
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07001991
Christoph Hellwig7a074e92018-05-02 19:51:00 +02001992 ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &t : NULL);
1993 if (signal_pending(current)) {
1994 if (ksig.sigmask) {
1995 current->saved_sigmask = sigsaved;
1996 set_restore_sigmask();
1997 }
1998 if (!ret)
1999 ret = -ERESTARTNOHAND;
2000 } else {
2001 if (ksig.sigmask)
2002 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2003 }
2004
2005 return ret;
Al Viroc00d2c72016-12-20 07:04:57 -05002006}
2007#endif