Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * An async IO implementation for Linux |
| 3 | * Written by Benjamin LaHaise <bcrl@kvack.org> |
| 4 | * |
| 5 | * Implements an efficient asynchronous io interface. |
| 6 | * |
| 7 | * Copyright 2000, 2001, 2002 Red Hat, Inc. All Rights Reserved. |
| 8 | * |
| 9 | * See ../COPYING for licensing terms. |
| 10 | */ |
Kent Overstreet | caf4167 | 2013-05-07 16:18:35 -0700 | [diff] [blame] | 11 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/time.h> |
| 17 | #include <linux/aio_abi.h> |
Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 18 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/syscalls.h> |
Jens Axboe | b9d128f | 2009-10-29 13:59:26 +0100 | [diff] [blame] | 20 | #include <linux/backing-dev.h> |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 21 | #include <linux/uio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 23 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/fs.h> |
| 25 | #include <linux/file.h> |
| 26 | #include <linux/mm.h> |
| 27 | #include <linux/mman.h> |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 28 | #include <linux/mmu_context.h> |
Kent Overstreet | e1bdd5f | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 29 | #include <linux/percpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #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 Libenzi | 9c3060b | 2007-05-10 22:23:21 -0700 | [diff] [blame] | 36 | #include <linux/eventfd.h> |
Jeff Moyer | cfb1e33 | 2009-10-02 18:57:36 -0400 | [diff] [blame] | 37 | #include <linux/blkdev.h> |
Jeff Moyer | 9d85cba7 | 2010-05-26 14:44:26 -0700 | [diff] [blame] | 38 | #include <linux/compat.h> |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 39 | #include <linux/migrate.h> |
| 40 | #include <linux/ramfs.h> |
Kent Overstreet | 723be6e | 2013-05-28 15:14:48 -0700 | [diff] [blame] | 41 | #include <linux/percpu-refcount.h> |
Benjamin LaHaise | 71ad749 | 2013-09-17 10:18:25 -0400 | [diff] [blame] | 42 | #include <linux/mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #include <asm/kmap_types.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 45 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Al Viro | 68d70d0 | 2013-06-19 15:26:04 +0400 | [diff] [blame] | 47 | #include "internal.h" |
| 48 | |
Kent Overstreet | 4e179bc | 2013-05-07 16:18:33 -0700 | [diff] [blame] | 49 | #define AIO_RING_MAGIC 0xa10a10a1 |
| 50 | #define AIO_RING_COMPAT_FEATURES 1 |
| 51 | #define AIO_RING_INCOMPAT_FEATURES 0 |
| 52 | struct aio_ring { |
| 53 | unsigned id; /* kernel internal index number */ |
| 54 | unsigned nr; /* number of io_events */ |
Benjamin LaHaise | fa8a53c | 2014-03-28 10:14:45 -0400 | [diff] [blame] | 55 | unsigned head; /* Written to by userland or under ring_lock |
| 56 | * mutex by aio_read_events_ring(). */ |
Kent Overstreet | 4e179bc | 2013-05-07 16:18:33 -0700 | [diff] [blame] | 57 | unsigned tail; |
| 58 | |
| 59 | unsigned magic; |
| 60 | unsigned compat_features; |
| 61 | unsigned incompat_features; |
| 62 | unsigned header_length; /* size of aio_ring */ |
| 63 | |
| 64 | |
| 65 | struct io_event io_events[0]; |
| 66 | }; /* 128 bytes + ring size */ |
| 67 | |
| 68 | #define AIO_RING_PAGES 8 |
Kent Overstreet | 4e179bc | 2013-05-07 16:18:33 -0700 | [diff] [blame] | 69 | |
Benjamin LaHaise | db446a0 | 2013-07-30 12:54:40 -0400 | [diff] [blame] | 70 | struct kioctx_table { |
Tejun Heo | d0264c0 | 2018-03-14 12:10:17 -0700 | [diff] [blame] | 71 | struct rcu_head rcu; |
| 72 | unsigned nr; |
| 73 | struct kioctx __rcu *table[]; |
Benjamin LaHaise | db446a0 | 2013-07-30 12:54:40 -0400 | [diff] [blame] | 74 | }; |
| 75 | |
Kent Overstreet | e1bdd5f | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 76 | struct kioctx_cpu { |
| 77 | unsigned reqs_available; |
| 78 | }; |
| 79 | |
Jens Axboe | dc48e56 | 2015-04-15 11:17:23 -0600 | [diff] [blame] | 80 | struct ctx_rq_wait { |
| 81 | struct completion comp; |
| 82 | atomic_t count; |
| 83 | }; |
| 84 | |
Kent Overstreet | 4e179bc | 2013-05-07 16:18:33 -0700 | [diff] [blame] | 85 | struct kioctx { |
Kent Overstreet | 723be6e | 2013-05-28 15:14:48 -0700 | [diff] [blame] | 86 | struct percpu_ref users; |
Kent Overstreet | 36f5588 | 2013-05-07 16:18:41 -0700 | [diff] [blame] | 87 | atomic_t dead; |
Kent Overstreet | 4e179bc | 2013-05-07 16:18:33 -0700 | [diff] [blame] | 88 | |
Kent Overstreet | e34ecee | 2013-10-10 19:31:47 -0700 | [diff] [blame] | 89 | struct percpu_ref reqs; |
| 90 | |
Kent Overstreet | 4e179bc | 2013-05-07 16:18:33 -0700 | [diff] [blame] | 91 | unsigned long user_id; |
Kent Overstreet | 4e179bc | 2013-05-07 16:18:33 -0700 | [diff] [blame] | 92 | |
Kent Overstreet | e1bdd5f | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 93 | struct __percpu kioctx_cpu *cpu; |
| 94 | |
| 95 | /* |
| 96 | * For percpu reqs_available, number of slots we move to/from global |
| 97 | * counter at a time: |
| 98 | */ |
| 99 | unsigned req_batch; |
Kent Overstreet | 3e845ce | 2013-05-07 16:18:51 -0700 | [diff] [blame] | 100 | /* |
| 101 | * This is what userspace passed to io_setup(), it's not used for |
| 102 | * anything but counting against the global max_reqs quota. |
| 103 | * |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 104 | * The real limit is nr_events - 1, which will be larger (see |
Kent Overstreet | 3e845ce | 2013-05-07 16:18:51 -0700 | [diff] [blame] | 105 | * aio_setup_ring()) |
| 106 | */ |
Kent Overstreet | 4e179bc | 2013-05-07 16:18:33 -0700 | [diff] [blame] | 107 | unsigned max_reqs; |
| 108 | |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 109 | /* Size of ringbuffer, in units of struct io_event */ |
| 110 | unsigned nr_events; |
Kent Overstreet | 4e179bc | 2013-05-07 16:18:33 -0700 | [diff] [blame] | 111 | |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 112 | unsigned long mmap_base; |
| 113 | unsigned long mmap_size; |
| 114 | |
| 115 | struct page **ring_pages; |
| 116 | long nr_pages; |
| 117 | |
Tejun Heo | f729863 | 2018-03-14 12:45:15 -0700 | [diff] [blame] | 118 | struct rcu_work free_rwork; /* see free_ioctx() */ |
Kent Overstreet | 4e23bca | 2013-05-07 16:18:56 -0700 | [diff] [blame] | 119 | |
Anatol Pomozov | e02ba72 | 2014-04-15 11:31:33 -0700 | [diff] [blame] | 120 | /* |
| 121 | * signals when all in-flight requests are done |
| 122 | */ |
Jens Axboe | dc48e56 | 2015-04-15 11:17:23 -0600 | [diff] [blame] | 123 | struct ctx_rq_wait *rq_wait; |
Anatol Pomozov | e02ba72 | 2014-04-15 11:31:33 -0700 | [diff] [blame] | 124 | |
Kent Overstreet | 4e23bca | 2013-05-07 16:18:56 -0700 | [diff] [blame] | 125 | struct { |
Kent Overstreet | 34e83fc | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 126 | /* |
| 127 | * This counts the number of available slots in the ringbuffer, |
| 128 | * so we avoid overflowing it: it's decremented (if positive) |
| 129 | * when allocating a kiocb and incremented when the resulting |
| 130 | * io_event is pulled off the ringbuffer. |
Kent Overstreet | e1bdd5f | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 131 | * |
| 132 | * We batch accesses to it with a percpu version. |
Kent Overstreet | 34e83fc | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 133 | */ |
| 134 | atomic_t reqs_available; |
Kent Overstreet | 4e23bca | 2013-05-07 16:18:56 -0700 | [diff] [blame] | 135 | } ____cacheline_aligned_in_smp; |
| 136 | |
| 137 | struct { |
| 138 | spinlock_t ctx_lock; |
| 139 | struct list_head active_reqs; /* used for cancellation */ |
| 140 | } ____cacheline_aligned_in_smp; |
| 141 | |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 142 | struct { |
| 143 | struct mutex ring_lock; |
Kent Overstreet | 4e23bca | 2013-05-07 16:18:56 -0700 | [diff] [blame] | 144 | wait_queue_head_t wait; |
| 145 | } ____cacheline_aligned_in_smp; |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 146 | |
| 147 | struct { |
| 148 | unsigned tail; |
Benjamin LaHaise | d856f32 | 2014-08-24 13:14:05 -0400 | [diff] [blame] | 149 | unsigned completed_events; |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 150 | spinlock_t completion_lock; |
Kent Overstreet | 4e23bca | 2013-05-07 16:18:56 -0700 | [diff] [blame] | 151 | } ____cacheline_aligned_in_smp; |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 152 | |
| 153 | struct page *internal_pages[AIO_RING_PAGES]; |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 154 | struct file *aio_ring_file; |
Benjamin LaHaise | db446a0 | 2013-07-30 12:54:40 -0400 | [diff] [blame] | 155 | |
| 156 | unsigned id; |
Kent Overstreet | 4e179bc | 2013-05-07 16:18:33 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
Christoph Hellwig | a3c0d43 | 2018-03-27 19:18:57 +0200 | [diff] [blame^] | 159 | struct fsync_iocb { |
| 160 | struct work_struct work; |
| 161 | struct file *file; |
| 162 | bool datasync; |
| 163 | }; |
| 164 | |
Christoph Hellwig | 04b2fa9 | 2015-02-02 14:49:06 +0100 | [diff] [blame] | 165 | /* |
| 166 | * We use ki_cancel == KIOCB_CANCELLED to indicate that a kiocb has been either |
| 167 | * cancelled or completed (this makes a certain amount of sense because |
| 168 | * successful cancellation - io_cancel() - does deliver the completion to |
| 169 | * userspace). |
| 170 | * |
| 171 | * And since most things don't implement kiocb cancellation and we'd really like |
| 172 | * kiocb completion to be lockless when possible, we use ki_cancel to |
| 173 | * synchronize cancellation and completion - we only set it to KIOCB_CANCELLED |
| 174 | * with xchg() or cmpxchg(), see batch_complete_aio() and kiocb_cancel(). |
| 175 | */ |
| 176 | #define KIOCB_CANCELLED ((void *) (~0ULL)) |
| 177 | |
| 178 | struct aio_kiocb { |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 179 | union { |
| 180 | struct kiocb rw; |
Christoph Hellwig | a3c0d43 | 2018-03-27 19:18:57 +0200 | [diff] [blame^] | 181 | struct fsync_iocb fsync; |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 182 | }; |
Christoph Hellwig | 04b2fa9 | 2015-02-02 14:49:06 +0100 | [diff] [blame] | 183 | |
| 184 | struct kioctx *ki_ctx; |
| 185 | kiocb_cancel_fn *ki_cancel; |
| 186 | |
| 187 | struct iocb __user *ki_user_iocb; /* user's aiocb */ |
| 188 | __u64 ki_user_data; /* user's data for completion */ |
| 189 | |
| 190 | struct list_head ki_list; /* the aio core uses this |
| 191 | * for cancellation */ |
| 192 | |
| 193 | /* |
| 194 | * If the aio_resfd field of the userspace iocb is not zero, |
| 195 | * this is the underlying eventfd context to deliver events to. |
| 196 | */ |
| 197 | struct eventfd_ctx *ki_eventfd; |
| 198 | }; |
| 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | /*------ sysctl variables----*/ |
Zach Brown | d55b5fd | 2005-11-07 00:59:31 -0800 | [diff] [blame] | 201 | static DEFINE_SPINLOCK(aio_nr_lock); |
| 202 | unsigned long aio_nr; /* current system wide number of aio requests */ |
| 203 | unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | /*----end sysctl variables---*/ |
| 205 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 206 | static struct kmem_cache *kiocb_cachep; |
| 207 | static struct kmem_cache *kioctx_cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Benjamin LaHaise | 71ad749 | 2013-09-17 10:18:25 -0400 | [diff] [blame] | 209 | static struct vfsmount *aio_mnt; |
| 210 | |
| 211 | static const struct file_operations aio_ring_fops; |
| 212 | static const struct address_space_operations aio_ctx_aops; |
| 213 | |
| 214 | static struct file *aio_private_file(struct kioctx *ctx, loff_t nr_pages) |
| 215 | { |
| 216 | struct qstr this = QSTR_INIT("[aio]", 5); |
| 217 | struct file *file; |
| 218 | struct path path; |
| 219 | struct inode *inode = alloc_anon_inode(aio_mnt->mnt_sb); |
Dan Carpenter | 7f62656 | 2013-11-13 10:49:40 +0300 | [diff] [blame] | 220 | if (IS_ERR(inode)) |
| 221 | return ERR_CAST(inode); |
Benjamin LaHaise | 71ad749 | 2013-09-17 10:18:25 -0400 | [diff] [blame] | 222 | |
| 223 | inode->i_mapping->a_ops = &aio_ctx_aops; |
| 224 | inode->i_mapping->private_data = ctx; |
| 225 | inode->i_size = PAGE_SIZE * nr_pages; |
| 226 | |
| 227 | path.dentry = d_alloc_pseudo(aio_mnt->mnt_sb, &this); |
| 228 | if (!path.dentry) { |
| 229 | iput(inode); |
| 230 | return ERR_PTR(-ENOMEM); |
| 231 | } |
| 232 | path.mnt = mntget(aio_mnt); |
| 233 | |
| 234 | d_instantiate(path.dentry, inode); |
| 235 | file = alloc_file(&path, FMODE_READ | FMODE_WRITE, &aio_ring_fops); |
| 236 | if (IS_ERR(file)) { |
| 237 | path_put(&path); |
| 238 | return file; |
| 239 | } |
| 240 | |
| 241 | file->f_flags = O_RDWR; |
Benjamin LaHaise | 71ad749 | 2013-09-17 10:18:25 -0400 | [diff] [blame] | 242 | return file; |
| 243 | } |
| 244 | |
| 245 | static struct dentry *aio_mount(struct file_system_type *fs_type, |
| 246 | int flags, const char *dev_name, void *data) |
| 247 | { |
| 248 | static const struct dentry_operations ops = { |
| 249 | .d_dname = simple_dname, |
| 250 | }; |
Jann Horn | 22f6b4d | 2016-09-16 00:31:22 +0200 | [diff] [blame] | 251 | struct dentry *root = mount_pseudo(fs_type, "aio:", NULL, &ops, |
| 252 | AIO_RING_MAGIC); |
| 253 | |
| 254 | if (!IS_ERR(root)) |
| 255 | root->d_sb->s_iflags |= SB_I_NOEXEC; |
| 256 | return root; |
Benjamin LaHaise | 71ad749 | 2013-09-17 10:18:25 -0400 | [diff] [blame] | 257 | } |
| 258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | /* aio_setup |
| 260 | * Creates the slab caches used by the aio routines, panic on |
| 261 | * failure as this is done early during the boot sequence. |
| 262 | */ |
| 263 | static int __init aio_setup(void) |
| 264 | { |
Benjamin LaHaise | 71ad749 | 2013-09-17 10:18:25 -0400 | [diff] [blame] | 265 | static struct file_system_type aio_fs = { |
| 266 | .name = "aio", |
| 267 | .mount = aio_mount, |
| 268 | .kill_sb = kill_anon_super, |
| 269 | }; |
| 270 | aio_mnt = kern_mount(&aio_fs); |
| 271 | if (IS_ERR(aio_mnt)) |
| 272 | panic("Failed to create aio fs mount."); |
| 273 | |
Christoph Hellwig | 04b2fa9 | 2015-02-02 14:49:06 +0100 | [diff] [blame] | 274 | kiocb_cachep = KMEM_CACHE(aio_kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC); |
Christoph Lameter | 0a31bd5 | 2007-05-06 14:49:57 -0700 | [diff] [blame] | 275 | kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | return 0; |
| 277 | } |
H Hartley Sweeten | 385773e | 2009-09-22 16:43:53 -0700 | [diff] [blame] | 278 | __initcall(aio_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
Benjamin LaHaise | 5e9ae2e | 2013-09-26 20:34:51 -0400 | [diff] [blame] | 280 | static void put_aio_ring_file(struct kioctx *ctx) |
| 281 | { |
| 282 | struct file *aio_ring_file = ctx->aio_ring_file; |
Rasmus Villemoes | de04e76 | 2016-09-15 00:25:03 +0200 | [diff] [blame] | 283 | struct address_space *i_mapping; |
| 284 | |
Benjamin LaHaise | 5e9ae2e | 2013-09-26 20:34:51 -0400 | [diff] [blame] | 285 | if (aio_ring_file) { |
Al Viro | 4506309 | 2016-12-04 18:24:56 -0500 | [diff] [blame] | 286 | truncate_setsize(file_inode(aio_ring_file), 0); |
Benjamin LaHaise | 5e9ae2e | 2013-09-26 20:34:51 -0400 | [diff] [blame] | 287 | |
| 288 | /* Prevent further access to the kioctx from migratepages */ |
Al Viro | 4506309 | 2016-12-04 18:24:56 -0500 | [diff] [blame] | 289 | i_mapping = aio_ring_file->f_mapping; |
Rasmus Villemoes | de04e76 | 2016-09-15 00:25:03 +0200 | [diff] [blame] | 290 | spin_lock(&i_mapping->private_lock); |
| 291 | i_mapping->private_data = NULL; |
Benjamin LaHaise | 5e9ae2e | 2013-09-26 20:34:51 -0400 | [diff] [blame] | 292 | ctx->aio_ring_file = NULL; |
Rasmus Villemoes | de04e76 | 2016-09-15 00:25:03 +0200 | [diff] [blame] | 293 | spin_unlock(&i_mapping->private_lock); |
Benjamin LaHaise | 5e9ae2e | 2013-09-26 20:34:51 -0400 | [diff] [blame] | 294 | |
| 295 | fput(aio_ring_file); |
| 296 | } |
| 297 | } |
| 298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | static void aio_free_ring(struct kioctx *ctx) |
| 300 | { |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 301 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Benjamin LaHaise | fa8a53c | 2014-03-28 10:14:45 -0400 | [diff] [blame] | 303 | /* Disconnect the kiotx from the ring file. This prevents future |
| 304 | * accesses to the kioctx from page migration. |
| 305 | */ |
| 306 | put_aio_ring_file(ctx); |
| 307 | |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 308 | for (i = 0; i < ctx->nr_pages; i++) { |
Benjamin LaHaise | 8e321fe | 2013-12-21 17:56:08 -0500 | [diff] [blame] | 309 | struct page *page; |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 310 | pr_debug("pid(%d) [%d] page->count=%d\n", current->pid, i, |
| 311 | page_count(ctx->ring_pages[i])); |
Benjamin LaHaise | 8e321fe | 2013-12-21 17:56:08 -0500 | [diff] [blame] | 312 | page = ctx->ring_pages[i]; |
| 313 | if (!page) |
| 314 | continue; |
| 315 | ctx->ring_pages[i] = NULL; |
| 316 | put_page(page); |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 317 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
Sasha Levin | ddb8c45 | 2013-11-19 17:33:03 -0500 | [diff] [blame] | 319 | if (ctx->ring_pages && ctx->ring_pages != ctx->internal_pages) { |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 320 | kfree(ctx->ring_pages); |
Sasha Levin | ddb8c45 | 2013-11-19 17:33:03 -0500 | [diff] [blame] | 321 | ctx->ring_pages = NULL; |
| 322 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Oleg Nesterov | 5477e70 | 2015-09-04 15:48:04 -0700 | [diff] [blame] | 325 | static int aio_ring_mremap(struct vm_area_struct *vma) |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 326 | { |
Oleg Nesterov | 5477e70 | 2015-09-04 15:48:04 -0700 | [diff] [blame] | 327 | struct file *file = vma->vm_file; |
Pavel Emelyanov | e4a0d3e | 2014-09-18 19:56:17 +0400 | [diff] [blame] | 328 | struct mm_struct *mm = vma->vm_mm; |
| 329 | struct kioctx_table *table; |
Al Viro | b2edffd | 2015-04-06 17:48:54 -0400 | [diff] [blame] | 330 | int i, res = -EINVAL; |
Pavel Emelyanov | e4a0d3e | 2014-09-18 19:56:17 +0400 | [diff] [blame] | 331 | |
| 332 | spin_lock(&mm->ioctx_lock); |
| 333 | rcu_read_lock(); |
| 334 | table = rcu_dereference(mm->ioctx_table); |
| 335 | for (i = 0; i < table->nr; i++) { |
| 336 | struct kioctx *ctx; |
| 337 | |
Tejun Heo | d0264c0 | 2018-03-14 12:10:17 -0700 | [diff] [blame] | 338 | ctx = rcu_dereference(table->table[i]); |
Pavel Emelyanov | e4a0d3e | 2014-09-18 19:56:17 +0400 | [diff] [blame] | 339 | if (ctx && ctx->aio_ring_file == file) { |
Al Viro | b2edffd | 2015-04-06 17:48:54 -0400 | [diff] [blame] | 340 | if (!atomic_read(&ctx->dead)) { |
| 341 | ctx->user_id = ctx->mmap_base = vma->vm_start; |
| 342 | res = 0; |
| 343 | } |
Pavel Emelyanov | e4a0d3e | 2014-09-18 19:56:17 +0400 | [diff] [blame] | 344 | break; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | rcu_read_unlock(); |
| 349 | spin_unlock(&mm->ioctx_lock); |
Al Viro | b2edffd | 2015-04-06 17:48:54 -0400 | [diff] [blame] | 350 | return res; |
Pavel Emelyanov | e4a0d3e | 2014-09-18 19:56:17 +0400 | [diff] [blame] | 351 | } |
| 352 | |
Oleg Nesterov | 5477e70 | 2015-09-04 15:48:04 -0700 | [diff] [blame] | 353 | static const struct vm_operations_struct aio_ring_vm_ops = { |
| 354 | .mremap = aio_ring_mremap, |
| 355 | #if IS_ENABLED(CONFIG_MMU) |
| 356 | .fault = filemap_fault, |
| 357 | .map_pages = filemap_map_pages, |
| 358 | .page_mkwrite = filemap_page_mkwrite, |
| 359 | #endif |
| 360 | }; |
| 361 | |
| 362 | static int aio_ring_mmap(struct file *file, struct vm_area_struct *vma) |
| 363 | { |
| 364 | vma->vm_flags |= VM_DONTEXPAND; |
| 365 | vma->vm_ops = &aio_ring_vm_ops; |
| 366 | return 0; |
| 367 | } |
| 368 | |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 369 | static const struct file_operations aio_ring_fops = { |
| 370 | .mmap = aio_ring_mmap, |
| 371 | }; |
| 372 | |
Benjamin LaHaise | 0c45355 | 2013-07-17 09:34:24 -0400 | [diff] [blame] | 373 | #if IS_ENABLED(CONFIG_MIGRATION) |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 374 | static int aio_migratepage(struct address_space *mapping, struct page *new, |
| 375 | struct page *old, enum migrate_mode mode) |
| 376 | { |
Benjamin LaHaise | 5e9ae2e | 2013-09-26 20:34:51 -0400 | [diff] [blame] | 377 | struct kioctx *ctx; |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 378 | unsigned long flags; |
Benjamin LaHaise | fa8a53c | 2014-03-28 10:14:45 -0400 | [diff] [blame] | 379 | pgoff_t idx; |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 380 | int rc; |
| 381 | |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 382 | /* |
| 383 | * We cannot support the _NO_COPY case here, because copy needs to |
| 384 | * happen under the ctx->completion_lock. That does not work with the |
| 385 | * migration workflow of MIGRATE_SYNC_NO_COPY. |
| 386 | */ |
| 387 | if (mode == MIGRATE_SYNC_NO_COPY) |
| 388 | return -EINVAL; |
| 389 | |
Benjamin LaHaise | 8e321fe | 2013-12-21 17:56:08 -0500 | [diff] [blame] | 390 | rc = 0; |
| 391 | |
Benjamin LaHaise | fa8a53c | 2014-03-28 10:14:45 -0400 | [diff] [blame] | 392 | /* mapping->private_lock here protects against the kioctx teardown. */ |
Benjamin LaHaise | 8e321fe | 2013-12-21 17:56:08 -0500 | [diff] [blame] | 393 | spin_lock(&mapping->private_lock); |
| 394 | ctx = mapping->private_data; |
Benjamin LaHaise | fa8a53c | 2014-03-28 10:14:45 -0400 | [diff] [blame] | 395 | if (!ctx) { |
| 396 | rc = -EINVAL; |
| 397 | goto out; |
| 398 | } |
| 399 | |
| 400 | /* The ring_lock mutex. The prevents aio_read_events() from writing |
| 401 | * to the ring's head, and prevents page migration from mucking in |
| 402 | * a partially initialized kiotx. |
| 403 | */ |
| 404 | if (!mutex_trylock(&ctx->ring_lock)) { |
| 405 | rc = -EAGAIN; |
| 406 | goto out; |
| 407 | } |
| 408 | |
| 409 | idx = old->index; |
| 410 | if (idx < (pgoff_t)ctx->nr_pages) { |
| 411 | /* Make sure the old page hasn't already been changed */ |
| 412 | if (ctx->ring_pages[idx] != old) |
| 413 | rc = -EAGAIN; |
Benjamin LaHaise | 8e321fe | 2013-12-21 17:56:08 -0500 | [diff] [blame] | 414 | } else |
| 415 | rc = -EINVAL; |
Benjamin LaHaise | 8e321fe | 2013-12-21 17:56:08 -0500 | [diff] [blame] | 416 | |
| 417 | if (rc != 0) |
Benjamin LaHaise | fa8a53c | 2014-03-28 10:14:45 -0400 | [diff] [blame] | 418 | goto out_unlock; |
Benjamin LaHaise | 8e321fe | 2013-12-21 17:56:08 -0500 | [diff] [blame] | 419 | |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 420 | /* Writeback must be complete */ |
| 421 | BUG_ON(PageWriteback(old)); |
Benjamin LaHaise | 8e321fe | 2013-12-21 17:56:08 -0500 | [diff] [blame] | 422 | get_page(new); |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 423 | |
Benjamin LaHaise | 8e321fe | 2013-12-21 17:56:08 -0500 | [diff] [blame] | 424 | rc = migrate_page_move_mapping(mapping, new, old, NULL, mode, 1); |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 425 | if (rc != MIGRATEPAGE_SUCCESS) { |
Benjamin LaHaise | 8e321fe | 2013-12-21 17:56:08 -0500 | [diff] [blame] | 426 | put_page(new); |
Benjamin LaHaise | fa8a53c | 2014-03-28 10:14:45 -0400 | [diff] [blame] | 427 | goto out_unlock; |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 428 | } |
| 429 | |
Benjamin LaHaise | fa8a53c | 2014-03-28 10:14:45 -0400 | [diff] [blame] | 430 | /* Take completion_lock to prevent other writes to the ring buffer |
| 431 | * while the old page is copied to the new. This prevents new |
| 432 | * events from being lost. |
Benjamin LaHaise | 5e9ae2e | 2013-09-26 20:34:51 -0400 | [diff] [blame] | 433 | */ |
Benjamin LaHaise | fa8a53c | 2014-03-28 10:14:45 -0400 | [diff] [blame] | 434 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 435 | migrate_page_copy(new, old); |
| 436 | BUG_ON(ctx->ring_pages[idx] != old); |
| 437 | ctx->ring_pages[idx] = new; |
| 438 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 439 | |
| 440 | /* The old page is no longer accessible. */ |
| 441 | put_page(old); |
| 442 | |
| 443 | out_unlock: |
| 444 | mutex_unlock(&ctx->ring_lock); |
| 445 | out: |
Benjamin LaHaise | 5e9ae2e | 2013-09-26 20:34:51 -0400 | [diff] [blame] | 446 | spin_unlock(&mapping->private_lock); |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 447 | return rc; |
| 448 | } |
Benjamin LaHaise | 0c45355 | 2013-07-17 09:34:24 -0400 | [diff] [blame] | 449 | #endif |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 450 | |
| 451 | static const struct address_space_operations aio_ctx_aops = { |
Gu Zheng | 835f252 | 2014-11-06 17:46:21 +0800 | [diff] [blame] | 452 | .set_page_dirty = __set_page_dirty_no_writeback, |
Benjamin LaHaise | 0c45355 | 2013-07-17 09:34:24 -0400 | [diff] [blame] | 453 | #if IS_ENABLED(CONFIG_MIGRATION) |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 454 | .migratepage = aio_migratepage, |
Benjamin LaHaise | 0c45355 | 2013-07-17 09:34:24 -0400 | [diff] [blame] | 455 | #endif |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 456 | }; |
| 457 | |
Mauricio Faria de Oliveira | 2a8a986 | 2017-07-05 10:53:16 -0300 | [diff] [blame] | 458 | static int aio_setup_ring(struct kioctx *ctx, unsigned int nr_events) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | { |
| 460 | struct aio_ring *ring; |
Zach Brown | 41003a7 | 2013-05-07 16:18:25 -0700 | [diff] [blame] | 461 | struct mm_struct *mm = current->mm; |
Linus Torvalds | 3dc9acb | 2013-12-20 05:11:12 +0900 | [diff] [blame] | 462 | unsigned long size, unused; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | int nr_pages; |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 464 | int i; |
| 465 | struct file *file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
| 467 | /* Compensate for the ring buffer's head/tail overlap entry */ |
| 468 | nr_events += 2; /* 1 is required, 2 for good luck */ |
| 469 | |
| 470 | size = sizeof(struct aio_ring); |
| 471 | size += sizeof(struct io_event) * nr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 473 | nr_pages = PFN_UP(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | if (nr_pages < 0) |
| 475 | return -EINVAL; |
| 476 | |
Benjamin LaHaise | 71ad749 | 2013-09-17 10:18:25 -0400 | [diff] [blame] | 477 | file = aio_private_file(ctx, nr_pages); |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 478 | if (IS_ERR(file)) { |
| 479 | ctx->aio_ring_file = NULL; |
Benjamin LaHaise | fa8a53c | 2014-03-28 10:14:45 -0400 | [diff] [blame] | 480 | return -ENOMEM; |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 481 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 483 | ctx->aio_ring_file = file; |
| 484 | nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring)) |
| 485 | / sizeof(struct io_event); |
| 486 | |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 487 | ctx->ring_pages = ctx->internal_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | if (nr_pages > AIO_RING_PAGES) { |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 489 | ctx->ring_pages = kcalloc(nr_pages, sizeof(struct page *), |
| 490 | GFP_KERNEL); |
Gu Zheng | d1b9432 | 2013-12-04 18:19:06 +0800 | [diff] [blame] | 491 | if (!ctx->ring_pages) { |
| 492 | put_aio_ring_file(ctx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | return -ENOMEM; |
Gu Zheng | d1b9432 | 2013-12-04 18:19:06 +0800 | [diff] [blame] | 494 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Linus Torvalds | 3dc9acb | 2013-12-20 05:11:12 +0900 | [diff] [blame] | 497 | for (i = 0; i < nr_pages; i++) { |
| 498 | struct page *page; |
Al Viro | 4506309 | 2016-12-04 18:24:56 -0500 | [diff] [blame] | 499 | page = find_or_create_page(file->f_mapping, |
Linus Torvalds | 3dc9acb | 2013-12-20 05:11:12 +0900 | [diff] [blame] | 500 | i, GFP_HIGHUSER | __GFP_ZERO); |
| 501 | if (!page) |
| 502 | break; |
| 503 | pr_debug("pid(%d) page[%d]->count=%d\n", |
| 504 | current->pid, i, page_count(page)); |
| 505 | SetPageUptodate(page); |
Linus Torvalds | 3dc9acb | 2013-12-20 05:11:12 +0900 | [diff] [blame] | 506 | unlock_page(page); |
| 507 | |
| 508 | ctx->ring_pages[i] = page; |
| 509 | } |
| 510 | ctx->nr_pages = i; |
| 511 | |
| 512 | if (unlikely(i != nr_pages)) { |
| 513 | aio_free_ring(ctx); |
Benjamin LaHaise | fa8a53c | 2014-03-28 10:14:45 -0400 | [diff] [blame] | 514 | return -ENOMEM; |
Linus Torvalds | 3dc9acb | 2013-12-20 05:11:12 +0900 | [diff] [blame] | 515 | } |
| 516 | |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 517 | ctx->mmap_size = nr_pages * PAGE_SIZE; |
| 518 | pr_debug("attempting mmap of %lu bytes\n", ctx->mmap_size); |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 519 | |
Michal Hocko | 013373e | 2016-05-23 16:25:59 -0700 | [diff] [blame] | 520 | if (down_write_killable(&mm->mmap_sem)) { |
| 521 | ctx->mmap_size = 0; |
| 522 | aio_free_ring(ctx); |
| 523 | return -EINTR; |
| 524 | } |
| 525 | |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 526 | ctx->mmap_base = do_mmap_pgoff(ctx->aio_ring_file, 0, ctx->mmap_size, |
| 527 | PROT_READ | PROT_WRITE, |
Mike Rapoport | 897ab3e | 2017-02-24 14:58:22 -0800 | [diff] [blame] | 528 | MAP_SHARED, 0, &unused, NULL); |
Linus Torvalds | 3dc9acb | 2013-12-20 05:11:12 +0900 | [diff] [blame] | 529 | up_write(&mm->mmap_sem); |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 530 | if (IS_ERR((void *)ctx->mmap_base)) { |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 531 | ctx->mmap_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | aio_free_ring(ctx); |
Benjamin LaHaise | fa8a53c | 2014-03-28 10:14:45 -0400 | [diff] [blame] | 533 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 536 | pr_debug("mmap address: 0x%08lx\n", ctx->mmap_base); |
Benjamin LaHaise | d6c355c | 2013-09-09 11:57:59 -0400 | [diff] [blame] | 537 | |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 538 | ctx->user_id = ctx->mmap_base; |
| 539 | ctx->nr_events = nr_events; /* trusted copy */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 541 | ring = kmap_atomic(ctx->ring_pages[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | ring->nr = nr_events; /* user copy */ |
Benjamin LaHaise | db446a0 | 2013-07-30 12:54:40 -0400 | [diff] [blame] | 543 | ring->id = ~0U; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | ring->head = ring->tail = 0; |
| 545 | ring->magic = AIO_RING_MAGIC; |
| 546 | ring->compat_features = AIO_RING_COMPAT_FEATURES; |
| 547 | ring->incompat_features = AIO_RING_INCOMPAT_FEATURES; |
| 548 | ring->header_length = sizeof(struct aio_ring); |
Cong Wang | e8e3c3d | 2011-11-25 23:14:27 +0800 | [diff] [blame] | 549 | kunmap_atomic(ring); |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 550 | flush_dcache_page(ctx->ring_pages[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | #define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event)) |
| 556 | #define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event)) |
| 557 | #define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE) |
| 558 | |
Christoph Hellwig | 04b2fa9 | 2015-02-02 14:49:06 +0100 | [diff] [blame] | 559 | void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel) |
Kent Overstreet | 0460fef | 2013-05-07 16:18:49 -0700 | [diff] [blame] | 560 | { |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 561 | struct aio_kiocb *req = container_of(iocb, struct aio_kiocb, rw); |
Kent Overstreet | 0460fef | 2013-05-07 16:18:49 -0700 | [diff] [blame] | 562 | struct kioctx *ctx = req->ki_ctx; |
| 563 | unsigned long flags; |
| 564 | |
Christoph Hellwig | 75321b5 | 2018-04-09 14:57:56 +0200 | [diff] [blame] | 565 | if (WARN_ON_ONCE(!list_empty(&req->ki_list))) |
| 566 | return; |
| 567 | |
Kent Overstreet | 0460fef | 2013-05-07 16:18:49 -0700 | [diff] [blame] | 568 | spin_lock_irqsave(&ctx->ctx_lock, flags); |
Christoph Hellwig | 75321b5 | 2018-04-09 14:57:56 +0200 | [diff] [blame] | 569 | list_add_tail(&req->ki_list, &ctx->active_reqs); |
Kent Overstreet | 0460fef | 2013-05-07 16:18:49 -0700 | [diff] [blame] | 570 | req->ki_cancel = cancel; |
Kent Overstreet | 0460fef | 2013-05-07 16:18:49 -0700 | [diff] [blame] | 571 | spin_unlock_irqrestore(&ctx->ctx_lock, flags); |
| 572 | } |
| 573 | EXPORT_SYMBOL(kiocb_set_cancel_fn); |
| 574 | |
Christoph Hellwig | 04b2fa9 | 2015-02-02 14:49:06 +0100 | [diff] [blame] | 575 | static int kiocb_cancel(struct aio_kiocb *kiocb) |
Kent Overstreet | 906b973 | 2013-05-07 16:18:31 -0700 | [diff] [blame] | 576 | { |
Kent Overstreet | 0460fef | 2013-05-07 16:18:49 -0700 | [diff] [blame] | 577 | kiocb_cancel_fn *old, *cancel; |
Kent Overstreet | 906b973 | 2013-05-07 16:18:31 -0700 | [diff] [blame] | 578 | |
Kent Overstreet | 0460fef | 2013-05-07 16:18:49 -0700 | [diff] [blame] | 579 | /* |
| 580 | * Don't want to set kiocb->ki_cancel = KIOCB_CANCELLED unless it |
| 581 | * actually has a cancel function, hence the cmpxchg() |
| 582 | */ |
Kent Overstreet | 906b973 | 2013-05-07 16:18:31 -0700 | [diff] [blame] | 583 | |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 584 | cancel = READ_ONCE(kiocb->ki_cancel); |
Kent Overstreet | 0460fef | 2013-05-07 16:18:49 -0700 | [diff] [blame] | 585 | do { |
| 586 | if (!cancel || cancel == KIOCB_CANCELLED) |
Kent Overstreet | 57282d8 | 2013-05-13 13:42:52 -0700 | [diff] [blame] | 587 | return -EINVAL; |
Kent Overstreet | 906b973 | 2013-05-07 16:18:31 -0700 | [diff] [blame] | 588 | |
Kent Overstreet | 0460fef | 2013-05-07 16:18:49 -0700 | [diff] [blame] | 589 | old = cancel; |
| 590 | cancel = cmpxchg(&kiocb->ki_cancel, old, KIOCB_CANCELLED); |
| 591 | } while (cancel != old); |
| 592 | |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 593 | return cancel(&kiocb->rw); |
Kent Overstreet | 906b973 | 2013-05-07 16:18:31 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Tejun Heo | a6d7cff | 2018-03-14 12:10:17 -0700 | [diff] [blame] | 596 | /* |
| 597 | * free_ioctx() should be RCU delayed to synchronize against the RCU |
| 598 | * protected lookup_ioctx() and also needs process context to call |
Tejun Heo | f729863 | 2018-03-14 12:45:15 -0700 | [diff] [blame] | 599 | * aio_free_ring(). Use rcu_work. |
Tejun Heo | a6d7cff | 2018-03-14 12:10:17 -0700 | [diff] [blame] | 600 | */ |
Kent Overstreet | e34ecee | 2013-10-10 19:31:47 -0700 | [diff] [blame] | 601 | static void free_ioctx(struct work_struct *work) |
Kent Overstreet | 36f5588 | 2013-05-07 16:18:41 -0700 | [diff] [blame] | 602 | { |
Tejun Heo | f729863 | 2018-03-14 12:45:15 -0700 | [diff] [blame] | 603 | struct kioctx *ctx = container_of(to_rcu_work(work), struct kioctx, |
| 604 | free_rwork); |
Kent Overstreet | e34ecee | 2013-10-10 19:31:47 -0700 | [diff] [blame] | 605 | pr_debug("freeing %p\n", ctx); |
| 606 | |
| 607 | aio_free_ring(ctx); |
Kent Overstreet | e1bdd5f | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 608 | free_percpu(ctx->cpu); |
Tejun Heo | 9a1049d | 2014-06-28 08:10:14 -0400 | [diff] [blame] | 609 | percpu_ref_exit(&ctx->reqs); |
| 610 | percpu_ref_exit(&ctx->users); |
Kent Overstreet | 36f5588 | 2013-05-07 16:18:41 -0700 | [diff] [blame] | 611 | kmem_cache_free(kioctx_cachep, ctx); |
| 612 | } |
| 613 | |
Kent Overstreet | e34ecee | 2013-10-10 19:31:47 -0700 | [diff] [blame] | 614 | static void free_ioctx_reqs(struct percpu_ref *ref) |
| 615 | { |
| 616 | struct kioctx *ctx = container_of(ref, struct kioctx, reqs); |
| 617 | |
Anatol Pomozov | e02ba72 | 2014-04-15 11:31:33 -0700 | [diff] [blame] | 618 | /* At this point we know that there are no any in-flight requests */ |
Jens Axboe | dc48e56 | 2015-04-15 11:17:23 -0600 | [diff] [blame] | 619 | if (ctx->rq_wait && atomic_dec_and_test(&ctx->rq_wait->count)) |
| 620 | complete(&ctx->rq_wait->comp); |
Anatol Pomozov | e02ba72 | 2014-04-15 11:31:33 -0700 | [diff] [blame] | 621 | |
Tejun Heo | a6d7cff | 2018-03-14 12:10:17 -0700 | [diff] [blame] | 622 | /* Synchronize against RCU protected table->table[] dereferences */ |
Tejun Heo | f729863 | 2018-03-14 12:45:15 -0700 | [diff] [blame] | 623 | INIT_RCU_WORK(&ctx->free_rwork, free_ioctx); |
| 624 | queue_rcu_work(system_wq, &ctx->free_rwork); |
Kent Overstreet | e34ecee | 2013-10-10 19:31:47 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Kent Overstreet | 36f5588 | 2013-05-07 16:18:41 -0700 | [diff] [blame] | 627 | /* |
| 628 | * When this function runs, the kioctx has been removed from the "hash table" |
| 629 | * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted - |
| 630 | * now it's safe to cancel any that need to be. |
| 631 | */ |
Kent Overstreet | e34ecee | 2013-10-10 19:31:47 -0700 | [diff] [blame] | 632 | static void free_ioctx_users(struct percpu_ref *ref) |
Kent Overstreet | 36f5588 | 2013-05-07 16:18:41 -0700 | [diff] [blame] | 633 | { |
Kent Overstreet | e34ecee | 2013-10-10 19:31:47 -0700 | [diff] [blame] | 634 | struct kioctx *ctx = container_of(ref, struct kioctx, users); |
Christoph Hellwig | 04b2fa9 | 2015-02-02 14:49:06 +0100 | [diff] [blame] | 635 | struct aio_kiocb *req; |
Kent Overstreet | 36f5588 | 2013-05-07 16:18:41 -0700 | [diff] [blame] | 636 | |
| 637 | spin_lock_irq(&ctx->ctx_lock); |
| 638 | |
| 639 | while (!list_empty(&ctx->active_reqs)) { |
| 640 | req = list_first_entry(&ctx->active_reqs, |
Christoph Hellwig | 04b2fa9 | 2015-02-02 14:49:06 +0100 | [diff] [blame] | 641 | struct aio_kiocb, ki_list); |
Kent Overstreet | 36f5588 | 2013-05-07 16:18:41 -0700 | [diff] [blame] | 642 | |
| 643 | list_del_init(&req->ki_list); |
Fabian Frederick | d52a8f9 | 2014-04-22 07:26:58 +0200 | [diff] [blame] | 644 | kiocb_cancel(req); |
Kent Overstreet | 36f5588 | 2013-05-07 16:18:41 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | spin_unlock_irq(&ctx->ctx_lock); |
| 648 | |
Kent Overstreet | e34ecee | 2013-10-10 19:31:47 -0700 | [diff] [blame] | 649 | percpu_ref_kill(&ctx->reqs); |
| 650 | percpu_ref_put(&ctx->reqs); |
Kent Overstreet | 36f5588 | 2013-05-07 16:18:41 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Benjamin LaHaise | db446a0 | 2013-07-30 12:54:40 -0400 | [diff] [blame] | 653 | static int ioctx_add_table(struct kioctx *ctx, struct mm_struct *mm) |
| 654 | { |
| 655 | unsigned i, new_nr; |
| 656 | struct kioctx_table *table, *old; |
| 657 | struct aio_ring *ring; |
| 658 | |
| 659 | spin_lock(&mm->ioctx_lock); |
Oleg Nesterov | 855ef0d | 2014-04-30 16:16:36 +0200 | [diff] [blame] | 660 | table = rcu_dereference_raw(mm->ioctx_table); |
Benjamin LaHaise | db446a0 | 2013-07-30 12:54:40 -0400 | [diff] [blame] | 661 | |
| 662 | while (1) { |
| 663 | if (table) |
| 664 | for (i = 0; i < table->nr; i++) |
Tejun Heo | d0264c0 | 2018-03-14 12:10:17 -0700 | [diff] [blame] | 665 | if (!rcu_access_pointer(table->table[i])) { |
Benjamin LaHaise | db446a0 | 2013-07-30 12:54:40 -0400 | [diff] [blame] | 666 | ctx->id = i; |
Tejun Heo | d0264c0 | 2018-03-14 12:10:17 -0700 | [diff] [blame] | 667 | rcu_assign_pointer(table->table[i], ctx); |
Benjamin LaHaise | db446a0 | 2013-07-30 12:54:40 -0400 | [diff] [blame] | 668 | spin_unlock(&mm->ioctx_lock); |
| 669 | |
Benjamin LaHaise | fa8a53c | 2014-03-28 10:14:45 -0400 | [diff] [blame] | 670 | /* While kioctx setup is in progress, |
| 671 | * we are protected from page migration |
| 672 | * changes ring_pages by ->ring_lock. |
| 673 | */ |
Benjamin LaHaise | db446a0 | 2013-07-30 12:54:40 -0400 | [diff] [blame] | 674 | ring = kmap_atomic(ctx->ring_pages[0]); |
| 675 | ring->id = ctx->id; |
| 676 | kunmap_atomic(ring); |
| 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | new_nr = (table ? table->nr : 1) * 4; |
Benjamin LaHaise | db446a0 | 2013-07-30 12:54:40 -0400 | [diff] [blame] | 681 | spin_unlock(&mm->ioctx_lock); |
| 682 | |
| 683 | table = kzalloc(sizeof(*table) + sizeof(struct kioctx *) * |
| 684 | new_nr, GFP_KERNEL); |
| 685 | if (!table) |
| 686 | return -ENOMEM; |
| 687 | |
| 688 | table->nr = new_nr; |
| 689 | |
| 690 | spin_lock(&mm->ioctx_lock); |
Oleg Nesterov | 855ef0d | 2014-04-30 16:16:36 +0200 | [diff] [blame] | 691 | old = rcu_dereference_raw(mm->ioctx_table); |
Benjamin LaHaise | db446a0 | 2013-07-30 12:54:40 -0400 | [diff] [blame] | 692 | |
| 693 | if (!old) { |
| 694 | rcu_assign_pointer(mm->ioctx_table, table); |
| 695 | } else if (table->nr > old->nr) { |
| 696 | memcpy(table->table, old->table, |
| 697 | old->nr * sizeof(struct kioctx *)); |
| 698 | |
| 699 | rcu_assign_pointer(mm->ioctx_table, table); |
| 700 | kfree_rcu(old, rcu); |
| 701 | } else { |
| 702 | kfree(table); |
| 703 | table = old; |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | |
Kent Overstreet | e34ecee | 2013-10-10 19:31:47 -0700 | [diff] [blame] | 708 | static void aio_nr_sub(unsigned nr) |
| 709 | { |
| 710 | spin_lock(&aio_nr_lock); |
| 711 | if (WARN_ON(aio_nr - nr > aio_nr)) |
| 712 | aio_nr = 0; |
| 713 | else |
| 714 | aio_nr -= nr; |
| 715 | spin_unlock(&aio_nr_lock); |
| 716 | } |
| 717 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | /* ioctx_alloc |
| 719 | * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed. |
| 720 | */ |
| 721 | static struct kioctx *ioctx_alloc(unsigned nr_events) |
| 722 | { |
Zach Brown | 41003a7 | 2013-05-07 16:18:25 -0700 | [diff] [blame] | 723 | struct mm_struct *mm = current->mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | struct kioctx *ctx; |
Al Viro | e23754f | 2012-03-06 14:33:22 -0500 | [diff] [blame] | 725 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
Kent Overstreet | e1bdd5f | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 727 | /* |
Mauricio Faria de Oliveira | 2a8a986 | 2017-07-05 10:53:16 -0300 | [diff] [blame] | 728 | * Store the original nr_events -- what userspace passed to io_setup(), |
| 729 | * for counting against the global limit -- before it changes. |
| 730 | */ |
| 731 | unsigned int max_reqs = nr_events; |
| 732 | |
| 733 | /* |
Kent Overstreet | e1bdd5f | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 734 | * We keep track of the number of available ringbuffer slots, to prevent |
| 735 | * overflow (reqs_available), and we also use percpu counters for this. |
| 736 | * |
| 737 | * So since up to half the slots might be on other cpu's percpu counters |
| 738 | * and unavailable, double nr_events so userspace sees what they |
| 739 | * expected: additionally, we move req_batch slots to/from percpu |
| 740 | * counters at a time, so make sure that isn't 0: |
| 741 | */ |
| 742 | nr_events = max(nr_events, num_possible_cpus() * 4); |
| 743 | nr_events *= 2; |
| 744 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | /* Prevent overflows */ |
Al Viro | 08397ac | 2015-03-31 11:43:52 -0400 | [diff] [blame] | 746 | if (nr_events > (0x10000000U / sizeof(struct io_event))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | pr_debug("ENOMEM: nr_events too high\n"); |
| 748 | return ERR_PTR(-EINVAL); |
| 749 | } |
| 750 | |
Mauricio Faria de Oliveira | 2a8a986 | 2017-07-05 10:53:16 -0300 | [diff] [blame] | 751 | if (!nr_events || (unsigned long)max_reqs > aio_max_nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | return ERR_PTR(-EAGAIN); |
| 753 | |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 754 | ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | if (!ctx) |
| 756 | return ERR_PTR(-ENOMEM); |
| 757 | |
Mauricio Faria de Oliveira | 2a8a986 | 2017-07-05 10:53:16 -0300 | [diff] [blame] | 758 | ctx->max_reqs = max_reqs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
Benjamin LaHaise | fa8a53c | 2014-03-28 10:14:45 -0400 | [diff] [blame] | 760 | spin_lock_init(&ctx->ctx_lock); |
| 761 | spin_lock_init(&ctx->completion_lock); |
| 762 | mutex_init(&ctx->ring_lock); |
| 763 | /* Protect against page migration throughout kiotx setup by keeping |
| 764 | * the ring_lock mutex held until setup is complete. */ |
| 765 | mutex_lock(&ctx->ring_lock); |
| 766 | init_waitqueue_head(&ctx->wait); |
| 767 | |
| 768 | INIT_LIST_HEAD(&ctx->active_reqs); |
| 769 | |
Tejun Heo | 2aad2a8 | 2014-09-24 13:31:50 -0400 | [diff] [blame] | 770 | if (percpu_ref_init(&ctx->users, free_ioctx_users, 0, GFP_KERNEL)) |
Kent Overstreet | e34ecee | 2013-10-10 19:31:47 -0700 | [diff] [blame] | 771 | goto err; |
| 772 | |
Tejun Heo | 2aad2a8 | 2014-09-24 13:31:50 -0400 | [diff] [blame] | 773 | if (percpu_ref_init(&ctx->reqs, free_ioctx_reqs, 0, GFP_KERNEL)) |
Kent Overstreet | e34ecee | 2013-10-10 19:31:47 -0700 | [diff] [blame] | 774 | goto err; |
Kent Overstreet | 723be6e | 2013-05-28 15:14:48 -0700 | [diff] [blame] | 775 | |
Kent Overstreet | e1bdd5f | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 776 | ctx->cpu = alloc_percpu(struct kioctx_cpu); |
| 777 | if (!ctx->cpu) |
Kent Overstreet | e34ecee | 2013-10-10 19:31:47 -0700 | [diff] [blame] | 778 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | |
Mauricio Faria de Oliveira | 2a8a986 | 2017-07-05 10:53:16 -0300 | [diff] [blame] | 780 | err = aio_setup_ring(ctx, nr_events); |
Benjamin LaHaise | fa8a53c | 2014-03-28 10:14:45 -0400 | [diff] [blame] | 781 | if (err < 0) |
Kent Overstreet | e34ecee | 2013-10-10 19:31:47 -0700 | [diff] [blame] | 782 | goto err; |
Kent Overstreet | e1bdd5f | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 783 | |
Kent Overstreet | 34e83fc | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 784 | atomic_set(&ctx->reqs_available, ctx->nr_events - 1); |
Kent Overstreet | e1bdd5f | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 785 | ctx->req_batch = (ctx->nr_events - 1) / (num_possible_cpus() * 4); |
Benjamin LaHaise | 6878ea7 | 2013-07-31 10:34:18 -0400 | [diff] [blame] | 786 | if (ctx->req_batch < 1) |
| 787 | ctx->req_batch = 1; |
Kent Overstreet | 34e83fc | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | /* limit the number of system wide aios */ |
Al Viro | 9fa1cb3 | 2012-03-10 23:14:05 -0500 | [diff] [blame] | 790 | spin_lock(&aio_nr_lock); |
Mauricio Faria de Oliveira | 2a8a986 | 2017-07-05 10:53:16 -0300 | [diff] [blame] | 791 | if (aio_nr + ctx->max_reqs > aio_max_nr || |
| 792 | aio_nr + ctx->max_reqs < aio_nr) { |
Al Viro | 9fa1cb3 | 2012-03-10 23:14:05 -0500 | [diff] [blame] | 793 | spin_unlock(&aio_nr_lock); |
Kent Overstreet | e34ecee | 2013-10-10 19:31:47 -0700 | [diff] [blame] | 794 | err = -EAGAIN; |
Gu Zheng | d1b9432 | 2013-12-04 18:19:06 +0800 | [diff] [blame] | 795 | goto err_ctx; |
Al Viro | 2dd542b | 2012-03-10 23:10:35 -0500 | [diff] [blame] | 796 | } |
| 797 | aio_nr += ctx->max_reqs; |
Al Viro | 9fa1cb3 | 2012-03-10 23:14:05 -0500 | [diff] [blame] | 798 | spin_unlock(&aio_nr_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | |
Benjamin LaHaise | 1881686 | 2013-12-21 15:49:28 -0500 | [diff] [blame] | 800 | percpu_ref_get(&ctx->users); /* io_setup() will drop this ref */ |
| 801 | percpu_ref_get(&ctx->reqs); /* free_ioctx_users() will drop this */ |
Kent Overstreet | 723be6e | 2013-05-28 15:14:48 -0700 | [diff] [blame] | 802 | |
Benjamin LaHaise | da90382 | 2013-08-05 13:21:43 -0400 | [diff] [blame] | 803 | err = ioctx_add_table(ctx, mm); |
| 804 | if (err) |
Kent Overstreet | e34ecee | 2013-10-10 19:31:47 -0700 | [diff] [blame] | 805 | goto err_cleanup; |
Benjamin LaHaise | da90382 | 2013-08-05 13:21:43 -0400 | [diff] [blame] | 806 | |
Benjamin LaHaise | fa8a53c | 2014-03-28 10:14:45 -0400 | [diff] [blame] | 807 | /* Release the ring_lock mutex now that all setup is complete. */ |
| 808 | mutex_unlock(&ctx->ring_lock); |
| 809 | |
Kent Overstreet | caf4167 | 2013-05-07 16:18:35 -0700 | [diff] [blame] | 810 | pr_debug("allocated ioctx %p[%ld]: mm=%p mask=0x%x\n", |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 811 | ctx, ctx->user_id, mm, ctx->nr_events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | return ctx; |
| 813 | |
Kent Overstreet | e34ecee | 2013-10-10 19:31:47 -0700 | [diff] [blame] | 814 | err_cleanup: |
| 815 | aio_nr_sub(ctx->max_reqs); |
Gu Zheng | d1b9432 | 2013-12-04 18:19:06 +0800 | [diff] [blame] | 816 | err_ctx: |
Al Viro | deeb852 | 2015-04-06 17:57:44 -0400 | [diff] [blame] | 817 | atomic_set(&ctx->dead, 1); |
| 818 | if (ctx->mmap_size) |
| 819 | vm_munmap(ctx->mmap_base, ctx->mmap_size); |
Gu Zheng | d1b9432 | 2013-12-04 18:19:06 +0800 | [diff] [blame] | 820 | aio_free_ring(ctx); |
Kent Overstreet | e34ecee | 2013-10-10 19:31:47 -0700 | [diff] [blame] | 821 | err: |
Benjamin LaHaise | fa8a53c | 2014-03-28 10:14:45 -0400 | [diff] [blame] | 822 | mutex_unlock(&ctx->ring_lock); |
Kent Overstreet | e1bdd5f | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 823 | free_percpu(ctx->cpu); |
Tejun Heo | 9a1049d | 2014-06-28 08:10:14 -0400 | [diff] [blame] | 824 | percpu_ref_exit(&ctx->reqs); |
| 825 | percpu_ref_exit(&ctx->users); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | kmem_cache_free(kioctx_cachep, ctx); |
Kent Overstreet | caf4167 | 2013-05-07 16:18:35 -0700 | [diff] [blame] | 827 | pr_debug("error allocating ioctx %d\n", err); |
Al Viro | e23754f | 2012-03-06 14:33:22 -0500 | [diff] [blame] | 828 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | } |
| 830 | |
Kent Overstreet | 36f5588 | 2013-05-07 16:18:41 -0700 | [diff] [blame] | 831 | /* kill_ioctx |
| 832 | * Cancels all outstanding aio requests on an aio context. Used |
| 833 | * when the processes owning a context have all exited to encourage |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | * the rapid destruction of the kioctx. |
| 835 | */ |
Benjamin LaHaise | fb2d448 | 2014-04-29 12:45:17 -0400 | [diff] [blame] | 836 | static int kill_ioctx(struct mm_struct *mm, struct kioctx *ctx, |
Jens Axboe | dc48e56 | 2015-04-15 11:17:23 -0600 | [diff] [blame] | 837 | struct ctx_rq_wait *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | { |
Benjamin LaHaise | fa88b6f | 2014-04-29 12:55:48 -0400 | [diff] [blame] | 839 | struct kioctx_table *table; |
Benjamin LaHaise | db446a0 | 2013-07-30 12:54:40 -0400 | [diff] [blame] | 840 | |
Benjamin LaHaise | fa88b6f | 2014-04-29 12:55:48 -0400 | [diff] [blame] | 841 | spin_lock(&mm->ioctx_lock); |
Al Viro | b2edffd | 2015-04-06 17:48:54 -0400 | [diff] [blame] | 842 | if (atomic_xchg(&ctx->dead, 1)) { |
| 843 | spin_unlock(&mm->ioctx_lock); |
| 844 | return -EINVAL; |
| 845 | } |
| 846 | |
Oleg Nesterov | 855ef0d | 2014-04-30 16:16:36 +0200 | [diff] [blame] | 847 | table = rcu_dereference_raw(mm->ioctx_table); |
Tejun Heo | d0264c0 | 2018-03-14 12:10:17 -0700 | [diff] [blame] | 848 | WARN_ON(ctx != rcu_access_pointer(table->table[ctx->id])); |
| 849 | RCU_INIT_POINTER(table->table[ctx->id], NULL); |
Benjamin LaHaise | fa88b6f | 2014-04-29 12:55:48 -0400 | [diff] [blame] | 850 | spin_unlock(&mm->ioctx_lock); |
Kent Overstreet | 4fcc712 | 2013-06-12 14:04:59 -0700 | [diff] [blame] | 851 | |
Tejun Heo | a6d7cff | 2018-03-14 12:10:17 -0700 | [diff] [blame] | 852 | /* free_ioctx_reqs() will do the necessary RCU synchronization */ |
Benjamin LaHaise | fa88b6f | 2014-04-29 12:55:48 -0400 | [diff] [blame] | 853 | wake_up_all(&ctx->wait); |
Kent Overstreet | 4fcc712 | 2013-06-12 14:04:59 -0700 | [diff] [blame] | 854 | |
Benjamin LaHaise | fa88b6f | 2014-04-29 12:55:48 -0400 | [diff] [blame] | 855 | /* |
| 856 | * It'd be more correct to do this in free_ioctx(), after all |
| 857 | * the outstanding kiocbs have finished - but by then io_destroy |
| 858 | * has already returned, so io_setup() could potentially return |
| 859 | * -EAGAIN with no ioctxs actually in use (as far as userspace |
| 860 | * could tell). |
| 861 | */ |
| 862 | aio_nr_sub(ctx->max_reqs); |
Benjamin LaHaise | fb2d448 | 2014-04-29 12:45:17 -0400 | [diff] [blame] | 863 | |
Benjamin LaHaise | fa88b6f | 2014-04-29 12:55:48 -0400 | [diff] [blame] | 864 | if (ctx->mmap_size) |
| 865 | vm_munmap(ctx->mmap_base, ctx->mmap_size); |
| 866 | |
Jens Axboe | dc48e56 | 2015-04-15 11:17:23 -0600 | [diff] [blame] | 867 | ctx->rq_wait = wait; |
Benjamin LaHaise | fa88b6f | 2014-04-29 12:55:48 -0400 | [diff] [blame] | 868 | percpu_ref_kill(&ctx->users); |
| 869 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | } |
| 871 | |
Kent Overstreet | 36f5588 | 2013-05-07 16:18:41 -0700 | [diff] [blame] | 872 | /* |
| 873 | * exit_aio: called when the last user of mm goes away. At this point, there is |
| 874 | * no way for any new requests to be submited or any of the io_* syscalls to be |
| 875 | * called on the context. |
| 876 | * |
| 877 | * There may be outstanding kiocbs, but free_ioctx() will explicitly wait on |
| 878 | * them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | */ |
Harvey Harrison | fc9b52c | 2008-02-08 04:19:52 -0800 | [diff] [blame] | 880 | void exit_aio(struct mm_struct *mm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | { |
Oleg Nesterov | 4b70ac5 | 2014-04-30 19:02:48 +0200 | [diff] [blame] | 882 | struct kioctx_table *table = rcu_dereference_raw(mm->ioctx_table); |
Jens Axboe | dc48e56 | 2015-04-15 11:17:23 -0600 | [diff] [blame] | 883 | struct ctx_rq_wait wait; |
| 884 | int i, skipped; |
Jens Axboe | abf137d | 2008-12-09 08:11:22 +0100 | [diff] [blame] | 885 | |
Oleg Nesterov | 4b70ac5 | 2014-04-30 19:02:48 +0200 | [diff] [blame] | 886 | if (!table) |
| 887 | return; |
Benjamin LaHaise | db446a0 | 2013-07-30 12:54:40 -0400 | [diff] [blame] | 888 | |
Jens Axboe | dc48e56 | 2015-04-15 11:17:23 -0600 | [diff] [blame] | 889 | atomic_set(&wait.count, table->nr); |
| 890 | init_completion(&wait.comp); |
| 891 | |
| 892 | skipped = 0; |
Oleg Nesterov | 4b70ac5 | 2014-04-30 19:02:48 +0200 | [diff] [blame] | 893 | for (i = 0; i < table->nr; ++i) { |
Tejun Heo | d0264c0 | 2018-03-14 12:10:17 -0700 | [diff] [blame] | 894 | struct kioctx *ctx = |
| 895 | rcu_dereference_protected(table->table[i], true); |
Benjamin LaHaise | db446a0 | 2013-07-30 12:54:40 -0400 | [diff] [blame] | 896 | |
Jens Axboe | dc48e56 | 2015-04-15 11:17:23 -0600 | [diff] [blame] | 897 | if (!ctx) { |
| 898 | skipped++; |
Oleg Nesterov | 4b70ac5 | 2014-04-30 19:02:48 +0200 | [diff] [blame] | 899 | continue; |
Jens Axboe | dc48e56 | 2015-04-15 11:17:23 -0600 | [diff] [blame] | 900 | } |
| 901 | |
Al Viro | 936af15 | 2012-04-20 21:49:41 -0400 | [diff] [blame] | 902 | /* |
Oleg Nesterov | 4b70ac5 | 2014-04-30 19:02:48 +0200 | [diff] [blame] | 903 | * We don't need to bother with munmap() here - exit_mmap(mm) |
| 904 | * is coming and it'll unmap everything. And we simply can't, |
| 905 | * this is not necessarily our ->mm. |
| 906 | * Since kill_ioctx() uses non-zero ->mmap_size as indicator |
| 907 | * that it needs to unmap the area, just set it to 0. |
Al Viro | 936af15 | 2012-04-20 21:49:41 -0400 | [diff] [blame] | 908 | */ |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 909 | ctx->mmap_size = 0; |
Jens Axboe | dc48e56 | 2015-04-15 11:17:23 -0600 | [diff] [blame] | 910 | kill_ioctx(mm, ctx, &wait); |
| 911 | } |
Kent Overstreet | 36f5588 | 2013-05-07 16:18:41 -0700 | [diff] [blame] | 912 | |
Jens Axboe | dc48e56 | 2015-04-15 11:17:23 -0600 | [diff] [blame] | 913 | if (!atomic_sub_and_test(skipped, &wait.count)) { |
Gu Zheng | 6098b45 | 2014-09-03 17:45:44 +0800 | [diff] [blame] | 914 | /* Wait until all IO for the context are done. */ |
Jens Axboe | dc48e56 | 2015-04-15 11:17:23 -0600 | [diff] [blame] | 915 | wait_for_completion(&wait.comp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | } |
Oleg Nesterov | 4b70ac5 | 2014-04-30 19:02:48 +0200 | [diff] [blame] | 917 | |
| 918 | RCU_INIT_POINTER(mm->ioctx_table, NULL); |
| 919 | kfree(table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | } |
| 921 | |
Kent Overstreet | e1bdd5f | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 922 | static void put_reqs_available(struct kioctx *ctx, unsigned nr) |
| 923 | { |
| 924 | struct kioctx_cpu *kcpu; |
Benjamin LaHaise | 263782c | 2014-07-14 12:49:26 -0400 | [diff] [blame] | 925 | unsigned long flags; |
Kent Overstreet | e1bdd5f | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 926 | |
Benjamin LaHaise | 263782c | 2014-07-14 12:49:26 -0400 | [diff] [blame] | 927 | local_irq_save(flags); |
Benjamin LaHaise | be6fb45 | 2014-07-22 09:56:56 -0400 | [diff] [blame] | 928 | kcpu = this_cpu_ptr(ctx->cpu); |
Kent Overstreet | e1bdd5f | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 929 | kcpu->reqs_available += nr; |
Benjamin LaHaise | 263782c | 2014-07-14 12:49:26 -0400 | [diff] [blame] | 930 | |
Kent Overstreet | e1bdd5f | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 931 | while (kcpu->reqs_available >= ctx->req_batch * 2) { |
| 932 | kcpu->reqs_available -= ctx->req_batch; |
| 933 | atomic_add(ctx->req_batch, &ctx->reqs_available); |
| 934 | } |
| 935 | |
Benjamin LaHaise | 263782c | 2014-07-14 12:49:26 -0400 | [diff] [blame] | 936 | local_irq_restore(flags); |
Kent Overstreet | e1bdd5f | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | static bool get_reqs_available(struct kioctx *ctx) |
| 940 | { |
| 941 | struct kioctx_cpu *kcpu; |
| 942 | bool ret = false; |
Benjamin LaHaise | 263782c | 2014-07-14 12:49:26 -0400 | [diff] [blame] | 943 | unsigned long flags; |
Kent Overstreet | e1bdd5f | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 944 | |
Benjamin LaHaise | 263782c | 2014-07-14 12:49:26 -0400 | [diff] [blame] | 945 | local_irq_save(flags); |
Benjamin LaHaise | be6fb45 | 2014-07-22 09:56:56 -0400 | [diff] [blame] | 946 | kcpu = this_cpu_ptr(ctx->cpu); |
Kent Overstreet | e1bdd5f | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 947 | if (!kcpu->reqs_available) { |
| 948 | int old, avail = atomic_read(&ctx->reqs_available); |
| 949 | |
| 950 | do { |
| 951 | if (avail < ctx->req_batch) |
| 952 | goto out; |
| 953 | |
| 954 | old = avail; |
| 955 | avail = atomic_cmpxchg(&ctx->reqs_available, |
| 956 | avail, avail - ctx->req_batch); |
| 957 | } while (avail != old); |
| 958 | |
| 959 | kcpu->reqs_available += ctx->req_batch; |
| 960 | } |
| 961 | |
| 962 | ret = true; |
| 963 | kcpu->reqs_available--; |
| 964 | out: |
Benjamin LaHaise | 263782c | 2014-07-14 12:49:26 -0400 | [diff] [blame] | 965 | local_irq_restore(flags); |
Kent Overstreet | e1bdd5f | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 966 | return ret; |
| 967 | } |
| 968 | |
Benjamin LaHaise | d856f32 | 2014-08-24 13:14:05 -0400 | [diff] [blame] | 969 | /* refill_reqs_available |
| 970 | * Updates the reqs_available reference counts used for tracking the |
| 971 | * number of free slots in the completion ring. This can be called |
| 972 | * from aio_complete() (to optimistically update reqs_available) or |
| 973 | * from aio_get_req() (the we're out of events case). It must be |
| 974 | * called holding ctx->completion_lock. |
| 975 | */ |
| 976 | static void refill_reqs_available(struct kioctx *ctx, unsigned head, |
| 977 | unsigned tail) |
| 978 | { |
| 979 | unsigned events_in_ring, completed; |
| 980 | |
| 981 | /* Clamp head since userland can write to it. */ |
| 982 | head %= ctx->nr_events; |
| 983 | if (head <= tail) |
| 984 | events_in_ring = tail - head; |
| 985 | else |
| 986 | events_in_ring = ctx->nr_events - (head - tail); |
| 987 | |
| 988 | completed = ctx->completed_events; |
| 989 | if (events_in_ring < completed) |
| 990 | completed -= events_in_ring; |
| 991 | else |
| 992 | completed = 0; |
| 993 | |
| 994 | if (!completed) |
| 995 | return; |
| 996 | |
| 997 | ctx->completed_events -= completed; |
| 998 | put_reqs_available(ctx, completed); |
| 999 | } |
| 1000 | |
| 1001 | /* user_refill_reqs_available |
| 1002 | * Called to refill reqs_available when aio_get_req() encounters an |
| 1003 | * out of space in the completion ring. |
| 1004 | */ |
| 1005 | static void user_refill_reqs_available(struct kioctx *ctx) |
| 1006 | { |
| 1007 | spin_lock_irq(&ctx->completion_lock); |
| 1008 | if (ctx->completed_events) { |
| 1009 | struct aio_ring *ring; |
| 1010 | unsigned head; |
| 1011 | |
| 1012 | /* Access of ring->head may race with aio_read_events_ring() |
| 1013 | * here, but that's okay since whether we read the old version |
| 1014 | * or the new version, and either will be valid. The important |
| 1015 | * part is that head cannot pass tail since we prevent |
| 1016 | * aio_complete() from updating tail by holding |
| 1017 | * ctx->completion_lock. Even if head is invalid, the check |
| 1018 | * against ctx->completed_events below will make sure we do the |
| 1019 | * safe/right thing. |
| 1020 | */ |
| 1021 | ring = kmap_atomic(ctx->ring_pages[0]); |
| 1022 | head = ring->head; |
| 1023 | kunmap_atomic(ring); |
| 1024 | |
| 1025 | refill_reqs_available(ctx, head, ctx->tail); |
| 1026 | } |
| 1027 | |
| 1028 | spin_unlock_irq(&ctx->completion_lock); |
| 1029 | } |
| 1030 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | /* aio_get_req |
Kent Overstreet | 57282d8 | 2013-05-13 13:42:52 -0700 | [diff] [blame] | 1032 | * Allocate a slot for an aio request. |
| 1033 | * Returns NULL if no requests are free. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | */ |
Christoph Hellwig | 04b2fa9 | 2015-02-02 14:49:06 +0100 | [diff] [blame] | 1035 | static inline struct aio_kiocb *aio_get_req(struct kioctx *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | { |
Christoph Hellwig | 04b2fa9 | 2015-02-02 14:49:06 +0100 | [diff] [blame] | 1037 | struct aio_kiocb *req; |
Kent Overstreet | a1c8eae | 2013-05-07 16:18:53 -0700 | [diff] [blame] | 1038 | |
Benjamin LaHaise | d856f32 | 2014-08-24 13:14:05 -0400 | [diff] [blame] | 1039 | if (!get_reqs_available(ctx)) { |
| 1040 | user_refill_reqs_available(ctx); |
| 1041 | if (!get_reqs_available(ctx)) |
| 1042 | return NULL; |
| 1043 | } |
Kent Overstreet | a1c8eae | 2013-05-07 16:18:53 -0700 | [diff] [blame] | 1044 | |
Kent Overstreet | 0460fef | 2013-05-07 16:18:49 -0700 | [diff] [blame] | 1045 | req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL|__GFP_ZERO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | if (unlikely(!req)) |
Kent Overstreet | a1c8eae | 2013-05-07 16:18:53 -0700 | [diff] [blame] | 1047 | goto out_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | |
Kent Overstreet | e34ecee | 2013-10-10 19:31:47 -0700 | [diff] [blame] | 1049 | percpu_ref_get(&ctx->reqs); |
Christoph Hellwig | 75321b5 | 2018-04-09 14:57:56 +0200 | [diff] [blame] | 1050 | INIT_LIST_HEAD(&req->ki_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | req->ki_ctx = ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | return req; |
Kent Overstreet | a1c8eae | 2013-05-07 16:18:53 -0700 | [diff] [blame] | 1053 | out_put: |
Kent Overstreet | e1bdd5f | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 1054 | put_reqs_available(ctx, 1); |
Kent Overstreet | a1c8eae | 2013-05-07 16:18:53 -0700 | [diff] [blame] | 1055 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
Adrian Bunk | d5470b5 | 2008-04-29 00:58:57 -0700 | [diff] [blame] | 1058 | static struct kioctx *lookup_ioctx(unsigned long ctx_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | { |
Benjamin LaHaise | db446a0 | 2013-07-30 12:54:40 -0400 | [diff] [blame] | 1060 | struct aio_ring __user *ring = (void __user *)ctx_id; |
Jens Axboe | abf137d | 2008-12-09 08:11:22 +0100 | [diff] [blame] | 1061 | struct mm_struct *mm = current->mm; |
Jeff Moyer | 65c2449 | 2009-03-18 17:04:21 -0700 | [diff] [blame] | 1062 | struct kioctx *ctx, *ret = NULL; |
Benjamin LaHaise | db446a0 | 2013-07-30 12:54:40 -0400 | [diff] [blame] | 1063 | struct kioctx_table *table; |
| 1064 | unsigned id; |
| 1065 | |
| 1066 | if (get_user(id, &ring->id)) |
| 1067 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | |
Jens Axboe | abf137d | 2008-12-09 08:11:22 +0100 | [diff] [blame] | 1069 | rcu_read_lock(); |
Benjamin LaHaise | db446a0 | 2013-07-30 12:54:40 -0400 | [diff] [blame] | 1070 | table = rcu_dereference(mm->ioctx_table); |
Jens Axboe | abf137d | 2008-12-09 08:11:22 +0100 | [diff] [blame] | 1071 | |
Benjamin LaHaise | db446a0 | 2013-07-30 12:54:40 -0400 | [diff] [blame] | 1072 | if (!table || id >= table->nr) |
| 1073 | goto out; |
| 1074 | |
Tejun Heo | d0264c0 | 2018-03-14 12:10:17 -0700 | [diff] [blame] | 1075 | ctx = rcu_dereference(table->table[id]); |
Benjamin LaHaise | f30d704 | 2013-08-07 18:23:48 -0400 | [diff] [blame] | 1076 | if (ctx && ctx->user_id == ctx_id) { |
Benjamin LaHaise | db446a0 | 2013-07-30 12:54:40 -0400 | [diff] [blame] | 1077 | percpu_ref_get(&ctx->users); |
| 1078 | ret = ctx; |
Jens Axboe | abf137d | 2008-12-09 08:11:22 +0100 | [diff] [blame] | 1079 | } |
Benjamin LaHaise | db446a0 | 2013-07-30 12:54:40 -0400 | [diff] [blame] | 1080 | out: |
Jens Axboe | abf137d | 2008-12-09 08:11:22 +0100 | [diff] [blame] | 1081 | rcu_read_unlock(); |
Jeff Moyer | 65c2449 | 2009-03-18 17:04:21 -0700 | [diff] [blame] | 1082 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | /* aio_complete |
| 1086 | * Called when the io request on the given iocb is complete. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | */ |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1088 | static void aio_complete(struct aio_kiocb *iocb, long res, long res2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | { |
| 1090 | struct kioctx *ctx = iocb->ki_ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | struct aio_ring *ring; |
Kent Overstreet | 21b4020 | 2013-05-07 16:18:47 -0700 | [diff] [blame] | 1092 | struct io_event *ev_page, *event; |
Benjamin LaHaise | d856f32 | 2014-08-24 13:14:05 -0400 | [diff] [blame] | 1093 | unsigned tail, pos, head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | |
Christoph Hellwig | 75321b5 | 2018-04-09 14:57:56 +0200 | [diff] [blame] | 1096 | if (!list_empty_careful(&iocb->ki_list)) { |
Kent Overstreet | 0460fef | 2013-05-07 16:18:49 -0700 | [diff] [blame] | 1097 | unsigned long flags; |
| 1098 | |
| 1099 | spin_lock_irqsave(&ctx->ctx_lock, flags); |
| 1100 | list_del(&iocb->ki_list); |
| 1101 | spin_unlock_irqrestore(&ctx->ctx_lock, flags); |
| 1102 | } |
Kent Overstreet | 11599eb | 2013-05-07 16:18:39 -0700 | [diff] [blame] | 1103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | /* |
Kent Overstreet | 0460fef | 2013-05-07 16:18:49 -0700 | [diff] [blame] | 1105 | * Add a completion event to the ring buffer. Must be done holding |
Tang Chen | 4b30f07 | 2013-07-03 15:09:16 -0700 | [diff] [blame] | 1106 | * ctx->completion_lock to prevent other code from messing with the tail |
Kent Overstreet | 0460fef | 2013-05-07 16:18:49 -0700 | [diff] [blame] | 1107 | * pointer since we might be called from irq context. |
| 1108 | */ |
| 1109 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1110 | |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 1111 | tail = ctx->tail; |
Kent Overstreet | 21b4020 | 2013-05-07 16:18:47 -0700 | [diff] [blame] | 1112 | pos = tail + AIO_EVENTS_OFFSET; |
| 1113 | |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 1114 | if (++tail >= ctx->nr_events) |
Ken Chen | 4bf69b2 | 2005-05-01 08:59:15 -0700 | [diff] [blame] | 1115 | tail = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 1117 | ev_page = kmap_atomic(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]); |
Kent Overstreet | 21b4020 | 2013-05-07 16:18:47 -0700 | [diff] [blame] | 1118 | event = ev_page + pos % AIO_EVENTS_PER_PAGE; |
| 1119 | |
Christoph Hellwig | 04b2fa9 | 2015-02-02 14:49:06 +0100 | [diff] [blame] | 1120 | event->obj = (u64)(unsigned long)iocb->ki_user_iocb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | event->data = iocb->ki_user_data; |
| 1122 | event->res = res; |
| 1123 | event->res2 = res2; |
| 1124 | |
Kent Overstreet | 21b4020 | 2013-05-07 16:18:47 -0700 | [diff] [blame] | 1125 | kunmap_atomic(ev_page); |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 1126 | flush_dcache_page(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]); |
Kent Overstreet | 21b4020 | 2013-05-07 16:18:47 -0700 | [diff] [blame] | 1127 | |
| 1128 | pr_debug("%p[%u]: %p: %p %Lx %lx %lx\n", |
Christoph Hellwig | 04b2fa9 | 2015-02-02 14:49:06 +0100 | [diff] [blame] | 1129 | ctx, tail, iocb, iocb->ki_user_iocb, iocb->ki_user_data, |
Kent Overstreet | caf4167 | 2013-05-07 16:18:35 -0700 | [diff] [blame] | 1130 | res, res2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | |
| 1132 | /* after flagging the request as done, we |
| 1133 | * must never even look at it again |
| 1134 | */ |
| 1135 | smp_wmb(); /* make event visible before updating tail */ |
| 1136 | |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 1137 | ctx->tail = tail; |
Kent Overstreet | 21b4020 | 2013-05-07 16:18:47 -0700 | [diff] [blame] | 1138 | |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 1139 | ring = kmap_atomic(ctx->ring_pages[0]); |
Benjamin LaHaise | d856f32 | 2014-08-24 13:14:05 -0400 | [diff] [blame] | 1140 | head = ring->head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | ring->tail = tail; |
Cong Wang | e8e3c3d | 2011-11-25 23:14:27 +0800 | [diff] [blame] | 1142 | kunmap_atomic(ring); |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 1143 | flush_dcache_page(ctx->ring_pages[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | |
Benjamin LaHaise | d856f32 | 2014-08-24 13:14:05 -0400 | [diff] [blame] | 1145 | ctx->completed_events++; |
| 1146 | if (ctx->completed_events > 1) |
| 1147 | refill_reqs_available(ctx, head, tail); |
Kent Overstreet | 0460fef | 2013-05-07 16:18:49 -0700 | [diff] [blame] | 1148 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1149 | |
Kent Overstreet | 21b4020 | 2013-05-07 16:18:47 -0700 | [diff] [blame] | 1150 | pr_debug("added to ring %p at [%u]\n", iocb, tail); |
Davide Libenzi | 8d1c98b | 2008-04-10 21:29:19 -0700 | [diff] [blame] | 1151 | |
| 1152 | /* |
| 1153 | * Check if the user asked us to deliver the result through an |
| 1154 | * eventfd. The eventfd_signal() function is safe to be called |
| 1155 | * from IRQ context. |
| 1156 | */ |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1157 | if (iocb->ki_eventfd) { |
Davide Libenzi | 8d1c98b | 2008-04-10 21:29:19 -0700 | [diff] [blame] | 1158 | eventfd_signal(iocb->ki_eventfd, 1); |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1159 | eventfd_ctx_put(iocb->ki_eventfd); |
| 1160 | } |
Davide Libenzi | 8d1c98b | 2008-04-10 21:29:19 -0700 | [diff] [blame] | 1161 | |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1162 | kmem_cache_free(kiocb_cachep, iocb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | |
Quentin Barnes | 6cb2a21 | 2008-03-19 17:00:39 -0700 | [diff] [blame] | 1164 | /* |
| 1165 | * We have to order our ring_info tail store above and test |
| 1166 | * of the wait list below outside the wait lock. This is |
| 1167 | * like in wake_up_bit() where clearing a bit has to be |
| 1168 | * ordered with the unlocked test. |
| 1169 | */ |
| 1170 | smp_mb(); |
| 1171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | if (waitqueue_active(&ctx->wait)) |
| 1173 | wake_up(&ctx->wait); |
| 1174 | |
Kent Overstreet | e34ecee | 2013-10-10 19:31:47 -0700 | [diff] [blame] | 1175 | percpu_ref_put(&ctx->reqs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
Gu Zheng | 2be4e7d | 2014-07-23 18:03:53 +0800 | [diff] [blame] | 1178 | /* aio_read_events_ring |
Kent Overstreet | a31ad38 | 2013-05-07 16:18:45 -0700 | [diff] [blame] | 1179 | * Pull an event off of the ioctx's event ring. Returns the number of |
| 1180 | * events fetched |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | */ |
Kent Overstreet | a31ad38 | 2013-05-07 16:18:45 -0700 | [diff] [blame] | 1182 | static long aio_read_events_ring(struct kioctx *ctx, |
| 1183 | struct io_event __user *event, long nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | struct aio_ring *ring; |
Kent Overstreet | 5ffac12 | 2013-05-09 15:36:07 -0700 | [diff] [blame] | 1186 | unsigned head, tail, pos; |
Kent Overstreet | a31ad38 | 2013-05-07 16:18:45 -0700 | [diff] [blame] | 1187 | long ret = 0; |
| 1188 | int copy_ret; |
| 1189 | |
Dave Chinner | 9c9ce76 | 2015-02-03 19:29:05 -0500 | [diff] [blame] | 1190 | /* |
| 1191 | * The mutex can block and wake us up and that will cause |
| 1192 | * wait_event_interruptible_hrtimeout() to schedule without sleeping |
| 1193 | * and repeat. This should be rare enough that it doesn't cause |
| 1194 | * peformance issues. See the comment in read_events() for more detail. |
| 1195 | */ |
| 1196 | sched_annotate_sleep(); |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 1197 | mutex_lock(&ctx->ring_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | |
Benjamin LaHaise | fa8a53c | 2014-03-28 10:14:45 -0400 | [diff] [blame] | 1199 | /* Access to ->ring_pages here is protected by ctx->ring_lock. */ |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 1200 | ring = kmap_atomic(ctx->ring_pages[0]); |
Kent Overstreet | a31ad38 | 2013-05-07 16:18:45 -0700 | [diff] [blame] | 1201 | head = ring->head; |
Kent Overstreet | 5ffac12 | 2013-05-09 15:36:07 -0700 | [diff] [blame] | 1202 | tail = ring->tail; |
Kent Overstreet | a31ad38 | 2013-05-07 16:18:45 -0700 | [diff] [blame] | 1203 | kunmap_atomic(ring); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | |
Jeff Moyer | 2ff396be | 2014-09-02 13:17:00 -0400 | [diff] [blame] | 1205 | /* |
| 1206 | * Ensure that once we've read the current tail pointer, that |
| 1207 | * we also see the events that were stored up to the tail. |
| 1208 | */ |
| 1209 | smp_rmb(); |
| 1210 | |
Kent Overstreet | 5ffac12 | 2013-05-09 15:36:07 -0700 | [diff] [blame] | 1211 | pr_debug("h%u t%u m%u\n", head, tail, ctx->nr_events); |
Kent Overstreet | a31ad38 | 2013-05-07 16:18:45 -0700 | [diff] [blame] | 1212 | |
Kent Overstreet | 5ffac12 | 2013-05-09 15:36:07 -0700 | [diff] [blame] | 1213 | if (head == tail) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | goto out; |
| 1215 | |
Benjamin LaHaise | edfbbf3 | 2014-06-24 13:32:51 -0400 | [diff] [blame] | 1216 | head %= ctx->nr_events; |
| 1217 | tail %= ctx->nr_events; |
| 1218 | |
Kent Overstreet | a31ad38 | 2013-05-07 16:18:45 -0700 | [diff] [blame] | 1219 | while (ret < nr) { |
| 1220 | long avail; |
| 1221 | struct io_event *ev; |
| 1222 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | |
Kent Overstreet | 5ffac12 | 2013-05-09 15:36:07 -0700 | [diff] [blame] | 1224 | avail = (head <= tail ? tail : ctx->nr_events) - head; |
| 1225 | if (head == tail) |
Kent Overstreet | a31ad38 | 2013-05-07 16:18:45 -0700 | [diff] [blame] | 1226 | break; |
| 1227 | |
| 1228 | avail = min(avail, nr - ret); |
| 1229 | avail = min_t(long, avail, AIO_EVENTS_PER_PAGE - |
| 1230 | ((head + AIO_EVENTS_OFFSET) % AIO_EVENTS_PER_PAGE)); |
| 1231 | |
| 1232 | pos = head + AIO_EVENTS_OFFSET; |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 1233 | page = ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]; |
Kent Overstreet | a31ad38 | 2013-05-07 16:18:45 -0700 | [diff] [blame] | 1234 | pos %= AIO_EVENTS_PER_PAGE; |
| 1235 | |
| 1236 | ev = kmap(page); |
| 1237 | copy_ret = copy_to_user(event + ret, ev + pos, |
| 1238 | sizeof(*ev) * avail); |
| 1239 | kunmap(page); |
| 1240 | |
| 1241 | if (unlikely(copy_ret)) { |
| 1242 | ret = -EFAULT; |
| 1243 | goto out; |
| 1244 | } |
| 1245 | |
| 1246 | ret += avail; |
| 1247 | head += avail; |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 1248 | head %= ctx->nr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 1251 | ring = kmap_atomic(ctx->ring_pages[0]); |
Kent Overstreet | a31ad38 | 2013-05-07 16:18:45 -0700 | [diff] [blame] | 1252 | ring->head = head; |
Zhao Hongjiang | 91d80a8 | 2013-04-26 11:03:53 +0800 | [diff] [blame] | 1253 | kunmap_atomic(ring); |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 1254 | flush_dcache_page(ctx->ring_pages[0]); |
Kent Overstreet | a31ad38 | 2013-05-07 16:18:45 -0700 | [diff] [blame] | 1255 | |
Kent Overstreet | 5ffac12 | 2013-05-09 15:36:07 -0700 | [diff] [blame] | 1256 | pr_debug("%li h%u t%u\n", ret, head, tail); |
Kent Overstreet | a31ad38 | 2013-05-07 16:18:45 -0700 | [diff] [blame] | 1257 | out: |
Kent Overstreet | 58c85dc | 2013-05-07 16:18:55 -0700 | [diff] [blame] | 1258 | mutex_unlock(&ctx->ring_lock); |
Kent Overstreet | a31ad38 | 2013-05-07 16:18:45 -0700 | [diff] [blame] | 1259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | return ret; |
| 1261 | } |
| 1262 | |
Kent Overstreet | a31ad38 | 2013-05-07 16:18:45 -0700 | [diff] [blame] | 1263 | static bool aio_read_events(struct kioctx *ctx, long min_nr, long nr, |
| 1264 | struct io_event __user *event, long *i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | { |
Kent Overstreet | a31ad38 | 2013-05-07 16:18:45 -0700 | [diff] [blame] | 1266 | long ret = aio_read_events_ring(ctx, event + *i, nr - *i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | |
Kent Overstreet | a31ad38 | 2013-05-07 16:18:45 -0700 | [diff] [blame] | 1268 | if (ret > 0) |
| 1269 | *i += ret; |
| 1270 | |
| 1271 | if (unlikely(atomic_read(&ctx->dead))) |
| 1272 | ret = -EINVAL; |
| 1273 | |
| 1274 | if (!*i) |
| 1275 | *i = ret; |
| 1276 | |
| 1277 | return ret < 0 || *i >= min_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
Kent Overstreet | a31ad38 | 2013-05-07 16:18:45 -0700 | [diff] [blame] | 1280 | static long read_events(struct kioctx *ctx, long min_nr, long nr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | struct io_event __user *event, |
Deepa Dinamani | fa2e62a | 2017-08-04 21:12:32 -0700 | [diff] [blame] | 1282 | ktime_t until) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | { |
Kent Overstreet | a31ad38 | 2013-05-07 16:18:45 -0700 | [diff] [blame] | 1284 | long ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | |
Kent Overstreet | a31ad38 | 2013-05-07 16:18:45 -0700 | [diff] [blame] | 1286 | /* |
| 1287 | * Note that aio_read_events() is being called as the conditional - i.e. |
| 1288 | * we're calling it after prepare_to_wait() has set task state to |
| 1289 | * TASK_INTERRUPTIBLE. |
| 1290 | * |
| 1291 | * But aio_read_events() can block, and if it blocks it's going to flip |
| 1292 | * the task state back to TASK_RUNNING. |
| 1293 | * |
| 1294 | * This should be ok, provided it doesn't flip the state back to |
| 1295 | * TASK_RUNNING and return 0 too much - that causes us to spin. That |
| 1296 | * will only happen if the mutex_lock() call blocks, and we then find |
| 1297 | * the ringbuffer empty. So in practice we should be ok, but it's |
| 1298 | * something to be aware of when touching this code. |
| 1299 | */ |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 1300 | if (until == 0) |
Fam Zheng | 5f785de | 2014-11-06 20:44:36 +0800 | [diff] [blame] | 1301 | aio_read_events(ctx, min_nr, nr, event, &ret); |
| 1302 | else |
| 1303 | wait_event_interruptible_hrtimeout(ctx->wait, |
| 1304 | aio_read_events(ctx, min_nr, nr, event, &ret), |
| 1305 | until); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | |
Kent Overstreet | a31ad38 | 2013-05-07 16:18:45 -0700 | [diff] [blame] | 1307 | if (!ret && signal_pending(current)) |
| 1308 | ret = -EINTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | |
Kent Overstreet | a31ad38 | 2013-05-07 16:18:45 -0700 | [diff] [blame] | 1310 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | } |
| 1312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | /* sys_io_setup: |
| 1314 | * Create an aio_context capable of receiving at least nr_events. |
| 1315 | * ctxp must not point to an aio_context that already exists, and |
| 1316 | * must be initialized to 0 prior to the call. On successful |
| 1317 | * creation of the aio_context, *ctxp is filled in with the resulting |
| 1318 | * handle. May fail with -EINVAL if *ctxp is not initialized, |
| 1319 | * if the specified nr_events exceeds internal limits. May fail |
| 1320 | * with -EAGAIN if the specified nr_events exceeds the user's limit |
| 1321 | * of available events. May fail with -ENOMEM if insufficient kernel |
| 1322 | * resources are available. May fail with -EFAULT if an invalid |
| 1323 | * pointer is passed for ctxp. Will fail with -ENOSYS if not |
| 1324 | * implemented. |
| 1325 | */ |
Heiko Carstens | 002c897 | 2009-01-14 14:14:18 +0100 | [diff] [blame] | 1326 | SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | { |
| 1328 | struct kioctx *ioctx = NULL; |
| 1329 | unsigned long ctx; |
| 1330 | long ret; |
| 1331 | |
| 1332 | ret = get_user(ctx, ctxp); |
| 1333 | if (unlikely(ret)) |
| 1334 | goto out; |
| 1335 | |
| 1336 | ret = -EINVAL; |
Zach Brown | d55b5fd | 2005-11-07 00:59:31 -0800 | [diff] [blame] | 1337 | if (unlikely(ctx || nr_events == 0)) { |
Kinglong Mee | acd88d4 | 2015-02-04 21:15:59 +0800 | [diff] [blame] | 1338 | pr_debug("EINVAL: ctx %lu nr_events %u\n", |
Zach Brown | d55b5fd | 2005-11-07 00:59:31 -0800 | [diff] [blame] | 1339 | ctx, nr_events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | goto out; |
| 1341 | } |
| 1342 | |
| 1343 | ioctx = ioctx_alloc(nr_events); |
| 1344 | ret = PTR_ERR(ioctx); |
| 1345 | if (!IS_ERR(ioctx)) { |
| 1346 | ret = put_user(ioctx->user_id, ctxp); |
Al Viro | a2e1859 | 2012-03-20 16:27:57 -0400 | [diff] [blame] | 1347 | if (ret) |
Anatol Pomozov | e02ba72 | 2014-04-15 11:31:33 -0700 | [diff] [blame] | 1348 | kill_ioctx(current->mm, ioctx, NULL); |
Kent Overstreet | 723be6e | 2013-05-28 15:14:48 -0700 | [diff] [blame] | 1349 | percpu_ref_put(&ioctx->users); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | out: |
| 1353 | return ret; |
| 1354 | } |
| 1355 | |
Al Viro | c00d2c7 | 2016-12-20 07:04:57 -0500 | [diff] [blame] | 1356 | #ifdef CONFIG_COMPAT |
| 1357 | COMPAT_SYSCALL_DEFINE2(io_setup, unsigned, nr_events, u32 __user *, ctx32p) |
| 1358 | { |
| 1359 | struct kioctx *ioctx = NULL; |
| 1360 | unsigned long ctx; |
| 1361 | long ret; |
| 1362 | |
| 1363 | ret = get_user(ctx, ctx32p); |
| 1364 | if (unlikely(ret)) |
| 1365 | goto out; |
| 1366 | |
| 1367 | ret = -EINVAL; |
| 1368 | if (unlikely(ctx || nr_events == 0)) { |
| 1369 | pr_debug("EINVAL: ctx %lu nr_events %u\n", |
| 1370 | ctx, nr_events); |
| 1371 | goto out; |
| 1372 | } |
| 1373 | |
| 1374 | ioctx = ioctx_alloc(nr_events); |
| 1375 | ret = PTR_ERR(ioctx); |
| 1376 | if (!IS_ERR(ioctx)) { |
| 1377 | /* truncating is ok because it's a user address */ |
| 1378 | ret = put_user((u32)ioctx->user_id, ctx32p); |
| 1379 | if (ret) |
| 1380 | kill_ioctx(current->mm, ioctx, NULL); |
| 1381 | percpu_ref_put(&ioctx->users); |
| 1382 | } |
| 1383 | |
| 1384 | out: |
| 1385 | return ret; |
| 1386 | } |
| 1387 | #endif |
| 1388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | /* sys_io_destroy: |
| 1390 | * Destroy the aio_context specified. May cancel any outstanding |
| 1391 | * AIOs and block on completion. Will fail with -ENOSYS if not |
Satoru Takeuchi | 642b512 | 2010-08-05 11:23:11 -0700 | [diff] [blame] | 1392 | * implemented. May fail with -EINVAL if the context pointed to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | * is invalid. |
| 1394 | */ |
Heiko Carstens | 002c897 | 2009-01-14 14:14:18 +0100 | [diff] [blame] | 1395 | SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | { |
| 1397 | struct kioctx *ioctx = lookup_ioctx(ctx); |
| 1398 | if (likely(NULL != ioctx)) { |
Jens Axboe | dc48e56 | 2015-04-15 11:17:23 -0600 | [diff] [blame] | 1399 | struct ctx_rq_wait wait; |
Benjamin LaHaise | fb2d448 | 2014-04-29 12:45:17 -0400 | [diff] [blame] | 1400 | int ret; |
Anatol Pomozov | e02ba72 | 2014-04-15 11:31:33 -0700 | [diff] [blame] | 1401 | |
Jens Axboe | dc48e56 | 2015-04-15 11:17:23 -0600 | [diff] [blame] | 1402 | init_completion(&wait.comp); |
| 1403 | atomic_set(&wait.count, 1); |
| 1404 | |
Anatol Pomozov | e02ba72 | 2014-04-15 11:31:33 -0700 | [diff] [blame] | 1405 | /* Pass requests_done to kill_ioctx() where it can be set |
| 1406 | * in a thread-safe way. If we try to set it here then we have |
| 1407 | * a race condition if two io_destroy() called simultaneously. |
| 1408 | */ |
Jens Axboe | dc48e56 | 2015-04-15 11:17:23 -0600 | [diff] [blame] | 1409 | ret = kill_ioctx(current->mm, ioctx, &wait); |
Kent Overstreet | 723be6e | 2013-05-28 15:14:48 -0700 | [diff] [blame] | 1410 | percpu_ref_put(&ioctx->users); |
Anatol Pomozov | e02ba72 | 2014-04-15 11:31:33 -0700 | [diff] [blame] | 1411 | |
| 1412 | /* Wait until all IO for the context are done. Otherwise kernel |
| 1413 | * keep using user-space buffers even if user thinks the context |
| 1414 | * is destroyed. |
| 1415 | */ |
Benjamin LaHaise | fb2d448 | 2014-04-29 12:45:17 -0400 | [diff] [blame] | 1416 | if (!ret) |
Jens Axboe | dc48e56 | 2015-04-15 11:17:23 -0600 | [diff] [blame] | 1417 | wait_for_completion(&wait.comp); |
Anatol Pomozov | e02ba72 | 2014-04-15 11:31:33 -0700 | [diff] [blame] | 1418 | |
Benjamin LaHaise | fb2d448 | 2014-04-29 12:45:17 -0400 | [diff] [blame] | 1419 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | } |
Kinglong Mee | acd88d4 | 2015-02-04 21:15:59 +0800 | [diff] [blame] | 1421 | pr_debug("EINVAL: invalid context id\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | return -EINVAL; |
| 1423 | } |
| 1424 | |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1425 | static void aio_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 1426 | { |
| 1427 | struct aio_kiocb *iocb = container_of(kiocb, struct aio_kiocb, rw); |
| 1428 | |
| 1429 | if (kiocb->ki_flags & IOCB_WRITE) { |
| 1430 | struct inode *inode = file_inode(kiocb->ki_filp); |
| 1431 | |
| 1432 | /* |
| 1433 | * Tell lockdep we inherited freeze protection from submission |
| 1434 | * thread. |
| 1435 | */ |
| 1436 | if (S_ISREG(inode->i_mode)) |
| 1437 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
| 1438 | file_end_write(kiocb->ki_filp); |
| 1439 | } |
| 1440 | |
| 1441 | fput(kiocb->ki_filp); |
| 1442 | aio_complete(iocb, res, res2); |
| 1443 | } |
| 1444 | |
| 1445 | static int aio_prep_rw(struct kiocb *req, struct iocb *iocb) |
| 1446 | { |
| 1447 | int ret; |
| 1448 | |
| 1449 | req->ki_filp = fget(iocb->aio_fildes); |
| 1450 | if (unlikely(!req->ki_filp)) |
| 1451 | return -EBADF; |
| 1452 | req->ki_complete = aio_complete_rw; |
| 1453 | req->ki_pos = iocb->aio_offset; |
| 1454 | req->ki_flags = iocb_flags(req->ki_filp); |
| 1455 | if (iocb->aio_flags & IOCB_FLAG_RESFD) |
| 1456 | req->ki_flags |= IOCB_EVENTFD; |
| 1457 | req->ki_hint = file_write_hint(req->ki_filp); |
| 1458 | ret = kiocb_set_rw_flags(req, iocb->aio_rw_flags); |
| 1459 | if (unlikely(ret)) |
| 1460 | fput(req->ki_filp); |
| 1461 | return ret; |
| 1462 | } |
| 1463 | |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1464 | static int aio_setup_rw(int rw, struct iocb *iocb, struct iovec **iovec, |
| 1465 | bool vectored, bool compat, struct iov_iter *iter) |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 1466 | { |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1467 | void __user *buf = (void __user *)(uintptr_t)iocb->aio_buf; |
| 1468 | size_t len = iocb->aio_nbytes; |
| 1469 | |
| 1470 | if (!vectored) { |
| 1471 | ssize_t ret = import_single_range(rw, buf, len, *iovec, iter); |
| 1472 | *iovec = NULL; |
| 1473 | return ret; |
| 1474 | } |
Jeff Moyer | 9d85cba7 | 2010-05-26 14:44:26 -0700 | [diff] [blame] | 1475 | #ifdef CONFIG_COMPAT |
| 1476 | if (compat) |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1477 | return compat_import_iovec(rw, buf, len, UIO_FASTIOV, iovec, |
| 1478 | iter); |
Jeff Moyer | 9d85cba7 | 2010-05-26 14:44:26 -0700 | [diff] [blame] | 1479 | #endif |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1480 | return import_iovec(rw, buf, len, UIO_FASTIOV, iovec, iter); |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1483 | static inline ssize_t aio_rw_ret(struct kiocb *req, ssize_t ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | { |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1485 | switch (ret) { |
| 1486 | case -EIOCBQUEUED: |
| 1487 | return ret; |
| 1488 | case -ERESTARTSYS: |
| 1489 | case -ERESTARTNOINTR: |
| 1490 | case -ERESTARTNOHAND: |
| 1491 | case -ERESTART_RESTARTBLOCK: |
Kent Overstreet | 41ef4eb | 2013-05-07 16:19:11 -0700 | [diff] [blame] | 1492 | /* |
| 1493 | * There's no easy way to restart the syscall since other AIO's |
| 1494 | * may be already running. Just fail this IO with EINTR. |
| 1495 | */ |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1496 | ret = -EINTR; |
| 1497 | /*FALLTHRU*/ |
| 1498 | default: |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1499 | aio_complete_rw(req, ret, 0); |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1500 | return 0; |
Kent Overstreet | 41ef4eb | 2013-05-07 16:19:11 -0700 | [diff] [blame] | 1501 | } |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1502 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1504 | static ssize_t aio_read(struct kiocb *req, struct iocb *iocb, bool vectored, |
| 1505 | bool compat) |
| 1506 | { |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1507 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
| 1508 | struct iov_iter iter; |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1509 | struct file *file; |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1510 | ssize_t ret; |
| 1511 | |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1512 | ret = aio_prep_rw(req, iocb); |
| 1513 | if (ret) |
| 1514 | return ret; |
| 1515 | file = req->ki_filp; |
| 1516 | |
| 1517 | ret = -EBADF; |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1518 | if (unlikely(!(file->f_mode & FMODE_READ))) |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1519 | goto out_fput; |
| 1520 | ret = -EINVAL; |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1521 | if (unlikely(!file->f_op->read_iter)) |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1522 | goto out_fput; |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1523 | |
| 1524 | ret = aio_setup_rw(READ, iocb, &iovec, vectored, compat, &iter); |
| 1525 | if (ret) |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1526 | goto out_fput; |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1527 | ret = rw_verify_area(READ, file, &req->ki_pos, iov_iter_count(&iter)); |
| 1528 | if (!ret) |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1529 | ret = aio_rw_ret(req, call_read_iter(file, req, &iter)); |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1530 | kfree(iovec); |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1531 | out_fput: |
| 1532 | if (unlikely(ret && ret != -EIOCBQUEUED)) |
| 1533 | fput(file); |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1534 | return ret; |
| 1535 | } |
| 1536 | |
| 1537 | static ssize_t aio_write(struct kiocb *req, struct iocb *iocb, bool vectored, |
| 1538 | bool compat) |
| 1539 | { |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1540 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
| 1541 | struct iov_iter iter; |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1542 | struct file *file; |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1543 | ssize_t ret; |
| 1544 | |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1545 | ret = aio_prep_rw(req, iocb); |
| 1546 | if (ret) |
| 1547 | return ret; |
| 1548 | file = req->ki_filp; |
| 1549 | |
| 1550 | ret = -EBADF; |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1551 | if (unlikely(!(file->f_mode & FMODE_WRITE))) |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1552 | goto out_fput; |
| 1553 | ret = -EINVAL; |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1554 | if (unlikely(!file->f_op->write_iter)) |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1555 | goto out_fput; |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1556 | |
| 1557 | ret = aio_setup_rw(WRITE, iocb, &iovec, vectored, compat, &iter); |
| 1558 | if (ret) |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1559 | goto out_fput; |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1560 | ret = rw_verify_area(WRITE, file, &req->ki_pos, iov_iter_count(&iter)); |
| 1561 | if (!ret) { |
Jan Kara | 70fe2f4 | 2016-10-30 11:42:04 -0500 | [diff] [blame] | 1562 | /* |
Christoph Hellwig | 92ce472 | 2018-04-06 09:28:17 +0200 | [diff] [blame] | 1563 | * Open-code file_start_write here to grab freeze protection, |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1564 | * which will be released by another thread in |
| 1565 | * aio_complete_rw(). Fool lockdep by telling it the lock got |
| 1566 | * released so that it doesn't complain about the held lock when |
| 1567 | * we return to userspace. |
Jan Kara | 70fe2f4 | 2016-10-30 11:42:04 -0500 | [diff] [blame] | 1568 | */ |
Christoph Hellwig | 92ce472 | 2018-04-06 09:28:17 +0200 | [diff] [blame] | 1569 | if (S_ISREG(file_inode(file)->i_mode)) { |
| 1570 | __sb_start_write(file_inode(file)->i_sb, SB_FREEZE_WRITE, true); |
Shaohua Li | a12f1ae | 2016-12-13 12:09:56 -0800 | [diff] [blame] | 1571 | __sb_writers_release(file_inode(file)->i_sb, SB_FREEZE_WRITE); |
Christoph Hellwig | 92ce472 | 2018-04-06 09:28:17 +0200 | [diff] [blame] | 1572 | } |
| 1573 | req->ki_flags |= IOCB_WRITE; |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1574 | ret = aio_rw_ret(req, call_write_iter(file, req, &iter)); |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1575 | } |
| 1576 | kfree(iovec); |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1577 | out_fput: |
| 1578 | if (unlikely(ret && ret != -EIOCBQUEUED)) |
| 1579 | fput(file); |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1580 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | } |
| 1582 | |
Christoph Hellwig | a3c0d43 | 2018-03-27 19:18:57 +0200 | [diff] [blame^] | 1583 | static void aio_fsync_work(struct work_struct *work) |
| 1584 | { |
| 1585 | struct fsync_iocb *req = container_of(work, struct fsync_iocb, work); |
| 1586 | int ret; |
| 1587 | |
| 1588 | ret = vfs_fsync(req->file, req->datasync); |
| 1589 | fput(req->file); |
| 1590 | aio_complete(container_of(req, struct aio_kiocb, fsync), ret, 0); |
| 1591 | } |
| 1592 | |
| 1593 | static int aio_fsync(struct fsync_iocb *req, struct iocb *iocb, bool datasync) |
| 1594 | { |
| 1595 | if (unlikely(iocb->aio_buf || iocb->aio_offset || iocb->aio_nbytes || |
| 1596 | iocb->aio_rw_flags)) |
| 1597 | return -EINVAL; |
| 1598 | |
| 1599 | req->file = fget(iocb->aio_fildes); |
| 1600 | if (unlikely(!req->file)) |
| 1601 | return -EBADF; |
| 1602 | if (unlikely(!req->file->f_op->fsync)) { |
| 1603 | fput(req->file); |
| 1604 | return -EINVAL; |
| 1605 | } |
| 1606 | |
| 1607 | req->datasync = datasync; |
| 1608 | INIT_WORK(&req->work, aio_fsync_work); |
| 1609 | schedule_work(&req->work); |
| 1610 | return -EIOCBQUEUED; |
| 1611 | } |
| 1612 | |
Adrian Bunk | d5470b5 | 2008-04-29 00:58:57 -0700 | [diff] [blame] | 1613 | static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb, |
Kent Overstreet | a1c8eae | 2013-05-07 16:18:53 -0700 | [diff] [blame] | 1614 | struct iocb *iocb, bool compat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | { |
Christoph Hellwig | 04b2fa9 | 2015-02-02 14:49:06 +0100 | [diff] [blame] | 1616 | struct aio_kiocb *req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | ssize_t ret; |
| 1618 | |
| 1619 | /* enforce forwards compatibility on users */ |
Goldwyn Rodrigues | 9830f4b | 2017-06-20 07:05:42 -0500 | [diff] [blame] | 1620 | if (unlikely(iocb->aio_reserved2)) { |
Kent Overstreet | caf4167 | 2013-05-07 16:18:35 -0700 | [diff] [blame] | 1621 | pr_debug("EINVAL: reserve field set\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | return -EINVAL; |
| 1623 | } |
| 1624 | |
| 1625 | /* prevent overflows */ |
| 1626 | if (unlikely( |
| 1627 | (iocb->aio_buf != (unsigned long)iocb->aio_buf) || |
| 1628 | (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) || |
| 1629 | ((ssize_t)iocb->aio_nbytes < 0) |
| 1630 | )) { |
Kinglong Mee | acd88d4 | 2015-02-04 21:15:59 +0800 | [diff] [blame] | 1631 | pr_debug("EINVAL: overflow check\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | return -EINVAL; |
| 1633 | } |
| 1634 | |
Kent Overstreet | 41ef4eb | 2013-05-07 16:19:11 -0700 | [diff] [blame] | 1635 | req = aio_get_req(ctx); |
Kent Overstreet | 1d98ebf | 2013-05-07 16:18:37 -0700 | [diff] [blame] | 1636 | if (unlikely(!req)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | return -EAGAIN; |
Kent Overstreet | 1d98ebf | 2013-05-07 16:18:37 -0700 | [diff] [blame] | 1638 | |
Davide Libenzi | 9c3060b | 2007-05-10 22:23:21 -0700 | [diff] [blame] | 1639 | if (iocb->aio_flags & IOCB_FLAG_RESFD) { |
| 1640 | /* |
| 1641 | * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an |
| 1642 | * instance of the file* now. The file descriptor must be |
| 1643 | * an eventfd() fd, and will be signaled for each completed |
| 1644 | * event using the eventfd_signal() function. |
| 1645 | */ |
Davide Libenzi | 1338901 | 2009-06-30 11:41:11 -0700 | [diff] [blame] | 1646 | req->ki_eventfd = eventfd_ctx_fdget((int) iocb->aio_resfd); |
Hirofumi Nakagawa | 801678c | 2008-04-29 01:03:09 -0700 | [diff] [blame] | 1647 | if (IS_ERR(req->ki_eventfd)) { |
Davide Libenzi | 9c3060b | 2007-05-10 22:23:21 -0700 | [diff] [blame] | 1648 | ret = PTR_ERR(req->ki_eventfd); |
Davide Libenzi | 87c3a86 | 2009-03-18 17:04:19 -0700 | [diff] [blame] | 1649 | req->ki_eventfd = NULL; |
Davide Libenzi | 9c3060b | 2007-05-10 22:23:21 -0700 | [diff] [blame] | 1650 | goto out_put_req; |
| 1651 | } |
Goldwyn Rodrigues | 9830f4b | 2017-06-20 07:05:42 -0500 | [diff] [blame] | 1652 | } |
| 1653 | |
Kent Overstreet | 8a66089 | 2013-05-07 16:19:10 -0700 | [diff] [blame] | 1654 | ret = put_user(KIOCB_KEY, &user_iocb->aio_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | if (unlikely(ret)) { |
Kent Overstreet | caf4167 | 2013-05-07 16:18:35 -0700 | [diff] [blame] | 1656 | pr_debug("EFAULT: aio_key\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | goto out_put_req; |
| 1658 | } |
| 1659 | |
Christoph Hellwig | 04b2fa9 | 2015-02-02 14:49:06 +0100 | [diff] [blame] | 1660 | req->ki_user_iocb = user_iocb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | req->ki_user_data = iocb->aio_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1663 | switch (iocb->aio_lio_opcode) { |
| 1664 | case IOCB_CMD_PREAD: |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1665 | ret = aio_read(&req->rw, iocb, false, compat); |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1666 | break; |
| 1667 | case IOCB_CMD_PWRITE: |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1668 | ret = aio_write(&req->rw, iocb, false, compat); |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1669 | break; |
| 1670 | case IOCB_CMD_PREADV: |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1671 | ret = aio_read(&req->rw, iocb, true, compat); |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1672 | break; |
| 1673 | case IOCB_CMD_PWRITEV: |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1674 | ret = aio_write(&req->rw, iocb, true, compat); |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1675 | break; |
Christoph Hellwig | a3c0d43 | 2018-03-27 19:18:57 +0200 | [diff] [blame^] | 1676 | case IOCB_CMD_FSYNC: |
| 1677 | ret = aio_fsync(&req->fsync, iocb, false); |
| 1678 | break; |
| 1679 | case IOCB_CMD_FDSYNC: |
| 1680 | ret = aio_fsync(&req->fsync, iocb, true); |
| 1681 | break; |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1682 | default: |
| 1683 | pr_debug("invalid aio operation %d\n", iocb->aio_lio_opcode); |
| 1684 | ret = -EINVAL; |
| 1685 | break; |
| 1686 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | |
Christoph Hellwig | 92ce472 | 2018-04-06 09:28:17 +0200 | [diff] [blame] | 1688 | /* |
| 1689 | * If ret is -EIOCBQUEUED, ownership of the file reference acquired |
| 1690 | * above passed to the file system, which at this point might have |
| 1691 | * dropped the reference, so we must be careful to not reference it |
| 1692 | * once we have called into the file system. |
| 1693 | */ |
Christoph Hellwig | 89319d31 | 2016-10-30 11:42:03 -0500 | [diff] [blame] | 1694 | if (ret && ret != -EIOCBQUEUED) |
| 1695 | goto out_put_req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | out_put_req: |
Kent Overstreet | e1bdd5f | 2013-04-26 10:58:39 +1000 | [diff] [blame] | 1698 | put_reqs_available(ctx, 1); |
Kent Overstreet | e34ecee | 2013-10-10 19:31:47 -0700 | [diff] [blame] | 1699 | percpu_ref_put(&ctx->reqs); |
Christoph Hellwig | 54843f8 | 2018-05-02 19:57:21 +0200 | [diff] [blame] | 1700 | if (req->ki_eventfd) |
| 1701 | eventfd_ctx_put(req->ki_eventfd); |
| 1702 | kmem_cache_free(kiocb_cachep, req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | return ret; |
| 1704 | } |
| 1705 | |
Al Viro | c00d2c7 | 2016-12-20 07:04:57 -0500 | [diff] [blame] | 1706 | static long do_io_submit(aio_context_t ctx_id, long nr, |
| 1707 | struct iocb __user *__user *iocbpp, bool compat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | { |
| 1709 | struct kioctx *ctx; |
| 1710 | long ret = 0; |
Jeff Moyer | 080d676 | 2011-11-02 13:40:10 -0700 | [diff] [blame] | 1711 | int i = 0; |
Shaohua Li | 9f5b942 | 2010-07-01 07:55:01 +0200 | [diff] [blame] | 1712 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | |
| 1714 | if (unlikely(nr < 0)) |
| 1715 | return -EINVAL; |
| 1716 | |
Jeff Moyer | 75e1c70 | 2010-09-10 14:16:00 -0700 | [diff] [blame] | 1717 | if (unlikely(nr > LONG_MAX/sizeof(*iocbpp))) |
| 1718 | nr = LONG_MAX/sizeof(*iocbpp); |
| 1719 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp))))) |
| 1721 | return -EFAULT; |
| 1722 | |
| 1723 | ctx = lookup_ioctx(ctx_id); |
| 1724 | if (unlikely(!ctx)) { |
Kent Overstreet | caf4167 | 2013-05-07 16:18:35 -0700 | [diff] [blame] | 1725 | pr_debug("EINVAL: invalid context id\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | return -EINVAL; |
| 1727 | } |
| 1728 | |
Shaohua Li | 9f5b942 | 2010-07-01 07:55:01 +0200 | [diff] [blame] | 1729 | blk_start_plug(&plug); |
| 1730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | /* |
| 1732 | * AKPM: should this return a partial result if some of the IOs were |
| 1733 | * successfully submitted? |
| 1734 | */ |
| 1735 | for (i=0; i<nr; i++) { |
| 1736 | struct iocb __user *user_iocb; |
| 1737 | struct iocb tmp; |
| 1738 | |
| 1739 | if (unlikely(__get_user(user_iocb, iocbpp + i))) { |
| 1740 | ret = -EFAULT; |
| 1741 | break; |
| 1742 | } |
| 1743 | |
| 1744 | if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) { |
| 1745 | ret = -EFAULT; |
| 1746 | break; |
| 1747 | } |
| 1748 | |
Kent Overstreet | a1c8eae | 2013-05-07 16:18:53 -0700 | [diff] [blame] | 1749 | ret = io_submit_one(ctx, user_iocb, &tmp, compat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | if (ret) |
| 1751 | break; |
| 1752 | } |
Shaohua Li | 9f5b942 | 2010-07-01 07:55:01 +0200 | [diff] [blame] | 1753 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | |
Kent Overstreet | 723be6e | 2013-05-28 15:14:48 -0700 | [diff] [blame] | 1755 | percpu_ref_put(&ctx->users); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | return i ? i : ret; |
| 1757 | } |
| 1758 | |
Jeff Moyer | 9d85cba7 | 2010-05-26 14:44:26 -0700 | [diff] [blame] | 1759 | /* sys_io_submit: |
| 1760 | * Queue the nr iocbs pointed to by iocbpp for processing. Returns |
| 1761 | * the number of iocbs queued. May return -EINVAL if the aio_context |
| 1762 | * specified by ctx_id is invalid, if nr is < 0, if the iocb at |
| 1763 | * *iocbpp[0] is not properly initialized, if the operation specified |
| 1764 | * is invalid for the file descriptor in the iocb. May fail with |
| 1765 | * -EFAULT if any of the data structures point to invalid data. May |
| 1766 | * fail with -EBADF if the file descriptor specified in the first |
| 1767 | * iocb is invalid. May fail with -EAGAIN if insufficient resources |
| 1768 | * are available to queue any iocbs. Will return 0 if nr is 0. Will |
| 1769 | * fail with -ENOSYS if not implemented. |
| 1770 | */ |
| 1771 | SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr, |
| 1772 | struct iocb __user * __user *, iocbpp) |
| 1773 | { |
| 1774 | return do_io_submit(ctx_id, nr, iocbpp, 0); |
| 1775 | } |
| 1776 | |
Al Viro | c00d2c7 | 2016-12-20 07:04:57 -0500 | [diff] [blame] | 1777 | #ifdef CONFIG_COMPAT |
| 1778 | static inline long |
| 1779 | copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64) |
| 1780 | { |
| 1781 | compat_uptr_t uptr; |
| 1782 | int i; |
| 1783 | |
| 1784 | for (i = 0; i < nr; ++i) { |
| 1785 | if (get_user(uptr, ptr32 + i)) |
| 1786 | return -EFAULT; |
| 1787 | if (put_user(compat_ptr(uptr), ptr64 + i)) |
| 1788 | return -EFAULT; |
| 1789 | } |
| 1790 | return 0; |
| 1791 | } |
| 1792 | |
| 1793 | #define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *)) |
| 1794 | |
| 1795 | COMPAT_SYSCALL_DEFINE3(io_submit, compat_aio_context_t, ctx_id, |
| 1796 | int, nr, u32 __user *, iocb) |
| 1797 | { |
| 1798 | struct iocb __user * __user *iocb64; |
| 1799 | long ret; |
| 1800 | |
| 1801 | if (unlikely(nr < 0)) |
| 1802 | return -EINVAL; |
| 1803 | |
| 1804 | if (nr > MAX_AIO_SUBMITS) |
| 1805 | nr = MAX_AIO_SUBMITS; |
| 1806 | |
| 1807 | iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64)); |
| 1808 | ret = copy_iocb(nr, iocb, iocb64); |
| 1809 | if (!ret) |
| 1810 | ret = do_io_submit(ctx_id, nr, iocb64, 1); |
| 1811 | return ret; |
| 1812 | } |
| 1813 | #endif |
| 1814 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | /* lookup_kiocb |
| 1816 | * Finds a given iocb for cancellation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | */ |
Christoph Hellwig | 04b2fa9 | 2015-02-02 14:49:06 +0100 | [diff] [blame] | 1818 | static struct aio_kiocb * |
| 1819 | lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb, u32 key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | { |
Christoph Hellwig | 04b2fa9 | 2015-02-02 14:49:06 +0100 | [diff] [blame] | 1821 | struct aio_kiocb *kiocb; |
Zach Brown | d00689a | 2005-11-13 16:07:34 -0800 | [diff] [blame] | 1822 | |
| 1823 | assert_spin_locked(&ctx->ctx_lock); |
| 1824 | |
Kent Overstreet | 8a66089 | 2013-05-07 16:19:10 -0700 | [diff] [blame] | 1825 | if (key != KIOCB_KEY) |
| 1826 | return NULL; |
| 1827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | /* TODO: use a hash or array, this sucks. */ |
Christoph Hellwig | 04b2fa9 | 2015-02-02 14:49:06 +0100 | [diff] [blame] | 1829 | list_for_each_entry(kiocb, &ctx->active_reqs, ki_list) { |
| 1830 | if (kiocb->ki_user_iocb == iocb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | return kiocb; |
| 1832 | } |
| 1833 | return NULL; |
| 1834 | } |
| 1835 | |
| 1836 | /* sys_io_cancel: |
| 1837 | * Attempts to cancel an iocb previously passed to io_submit. If |
| 1838 | * the operation is successfully cancelled, the resulting event is |
| 1839 | * copied into the memory pointed to by result without being placed |
| 1840 | * into the completion queue and 0 is returned. May fail with |
| 1841 | * -EFAULT if any of the data structures pointed to are invalid. |
| 1842 | * May fail with -EINVAL if aio_context specified by ctx_id is |
| 1843 | * invalid. May fail with -EAGAIN if the iocb specified was not |
| 1844 | * cancelled. Will fail with -ENOSYS if not implemented. |
| 1845 | */ |
Heiko Carstens | 002c897 | 2009-01-14 14:14:18 +0100 | [diff] [blame] | 1846 | SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb, |
| 1847 | struct io_event __user *, result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | struct kioctx *ctx; |
Christoph Hellwig | 04b2fa9 | 2015-02-02 14:49:06 +0100 | [diff] [blame] | 1850 | struct aio_kiocb *kiocb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | u32 key; |
| 1852 | int ret; |
| 1853 | |
| 1854 | ret = get_user(key, &iocb->aio_key); |
| 1855 | if (unlikely(ret)) |
| 1856 | return -EFAULT; |
| 1857 | |
| 1858 | ctx = lookup_ioctx(ctx_id); |
| 1859 | if (unlikely(!ctx)) |
| 1860 | return -EINVAL; |
| 1861 | |
| 1862 | spin_lock_irq(&ctx->ctx_lock); |
Kent Overstreet | 906b973 | 2013-05-07 16:18:31 -0700 | [diff] [blame] | 1863 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | kiocb = lookup_kiocb(ctx, iocb, key); |
Kent Overstreet | 906b973 | 2013-05-07 16:18:31 -0700 | [diff] [blame] | 1865 | if (kiocb) |
Fabian Frederick | d52a8f9 | 2014-04-22 07:26:58 +0200 | [diff] [blame] | 1866 | ret = kiocb_cancel(kiocb); |
Kent Overstreet | 906b973 | 2013-05-07 16:18:31 -0700 | [diff] [blame] | 1867 | else |
| 1868 | ret = -EINVAL; |
| 1869 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | spin_unlock_irq(&ctx->ctx_lock); |
| 1871 | |
Kent Overstreet | 906b973 | 2013-05-07 16:18:31 -0700 | [diff] [blame] | 1872 | if (!ret) { |
Kent Overstreet | bec68faa | 2013-05-13 14:45:08 -0700 | [diff] [blame] | 1873 | /* |
| 1874 | * The result argument is no longer used - the io_event is |
| 1875 | * always delivered via the ring buffer. -EINPROGRESS indicates |
| 1876 | * cancellation is progress: |
Kent Overstreet | 906b973 | 2013-05-07 16:18:31 -0700 | [diff] [blame] | 1877 | */ |
Kent Overstreet | bec68faa | 2013-05-13 14:45:08 -0700 | [diff] [blame] | 1878 | ret = -EINPROGRESS; |
Kent Overstreet | 906b973 | 2013-05-07 16:18:31 -0700 | [diff] [blame] | 1879 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | |
Kent Overstreet | 723be6e | 2013-05-28 15:14:48 -0700 | [diff] [blame] | 1881 | percpu_ref_put(&ctx->users); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | |
| 1883 | return ret; |
| 1884 | } |
| 1885 | |
Deepa Dinamani | fa2e62a | 2017-08-04 21:12:32 -0700 | [diff] [blame] | 1886 | static long do_io_getevents(aio_context_t ctx_id, |
| 1887 | long min_nr, |
| 1888 | long nr, |
| 1889 | struct io_event __user *events, |
| 1890 | struct timespec64 *ts) |
| 1891 | { |
| 1892 | ktime_t until = ts ? timespec64_to_ktime(*ts) : KTIME_MAX; |
| 1893 | struct kioctx *ioctx = lookup_ioctx(ctx_id); |
| 1894 | long ret = -EINVAL; |
| 1895 | |
| 1896 | if (likely(ioctx)) { |
| 1897 | if (likely(min_nr <= nr && min_nr >= 0)) |
| 1898 | ret = read_events(ioctx, min_nr, nr, events, until); |
| 1899 | percpu_ref_put(&ioctx->users); |
| 1900 | } |
| 1901 | |
| 1902 | return ret; |
| 1903 | } |
| 1904 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | /* io_getevents: |
| 1906 | * Attempts to read at least min_nr events and up to nr events from |
Satoru Takeuchi | 642b512 | 2010-08-05 11:23:11 -0700 | [diff] [blame] | 1907 | * the completion queue for the aio_context specified by ctx_id. If |
| 1908 | * it succeeds, the number of read events is returned. May fail with |
| 1909 | * -EINVAL if ctx_id is invalid, if min_nr is out of range, if nr is |
| 1910 | * out of range, if timeout is out of range. May fail with -EFAULT |
| 1911 | * if any of the memory specified is invalid. May return 0 or |
| 1912 | * < min_nr if the timeout specified by timeout has elapsed |
| 1913 | * before sufficient events are available, where timeout == NULL |
| 1914 | * specifies an infinite timeout. Note that the timeout pointed to by |
Jeff Moyer | 6900807 | 2013-05-24 15:55:24 -0700 | [diff] [blame] | 1915 | * timeout is relative. Will fail with -ENOSYS if not implemented. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | */ |
Heiko Carstens | 002c897 | 2009-01-14 14:14:18 +0100 | [diff] [blame] | 1917 | SYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id, |
| 1918 | long, min_nr, |
| 1919 | long, nr, |
| 1920 | struct io_event __user *, events, |
| 1921 | struct timespec __user *, timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | { |
Deepa Dinamani | fa2e62a | 2017-08-04 21:12:32 -0700 | [diff] [blame] | 1923 | struct timespec64 ts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | |
Deepa Dinamani | fa2e62a | 2017-08-04 21:12:32 -0700 | [diff] [blame] | 1925 | if (timeout) { |
| 1926 | if (unlikely(get_timespec64(&ts, timeout))) |
| 1927 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | } |
Deepa Dinamani | fa2e62a | 2017-08-04 21:12:32 -0700 | [diff] [blame] | 1929 | |
| 1930 | return do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &ts : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | } |
Al Viro | c00d2c7 | 2016-12-20 07:04:57 -0500 | [diff] [blame] | 1932 | |
| 1933 | #ifdef CONFIG_COMPAT |
| 1934 | COMPAT_SYSCALL_DEFINE5(io_getevents, compat_aio_context_t, ctx_id, |
| 1935 | compat_long_t, min_nr, |
| 1936 | compat_long_t, nr, |
| 1937 | struct io_event __user *, events, |
| 1938 | struct compat_timespec __user *, timeout) |
| 1939 | { |
Deepa Dinamani | fa2e62a | 2017-08-04 21:12:32 -0700 | [diff] [blame] | 1940 | struct timespec64 t; |
Al Viro | c00d2c7 | 2016-12-20 07:04:57 -0500 | [diff] [blame] | 1941 | |
| 1942 | if (timeout) { |
Deepa Dinamani | fa2e62a | 2017-08-04 21:12:32 -0700 | [diff] [blame] | 1943 | if (compat_get_timespec64(&t, timeout)) |
Al Viro | c00d2c7 | 2016-12-20 07:04:57 -0500 | [diff] [blame] | 1944 | return -EFAULT; |
| 1945 | |
Al Viro | c00d2c7 | 2016-12-20 07:04:57 -0500 | [diff] [blame] | 1946 | } |
Deepa Dinamani | fa2e62a | 2017-08-04 21:12:32 -0700 | [diff] [blame] | 1947 | |
| 1948 | return do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &t : NULL); |
Al Viro | c00d2c7 | 2016-12-20 07:04:57 -0500 | [diff] [blame] | 1949 | } |
| 1950 | #endif |