blob: 61d2e69429519e6a5ee78910595fcede80503da8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * An async IO implementation for Linux
3 * Written by Benjamin LaHaise <bcrl@kvack.org>
4 *
5 * Implements an efficient asynchronous io interface.
6 *
7 * Copyright 2000, 2001, 2002 Red Hat, Inc. All Rights Reserved.
8 *
9 * See ../COPYING for licensing terms.
10 */
Kent Overstreetcaf41672013-05-07 16:18:35 -070011#define pr_fmt(fmt) "%s: " fmt, __func__
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/errno.h>
16#include <linux/time.h>
17#include <linux/aio_abi.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050018#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/syscalls.h>
Jens Axboeb9d128f2009-10-29 13:59:26 +010020#include <linux/backing-dev.h>
Badari Pulavarty027445c2006-09-30 23:28:46 -070021#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Ingo Molnar174cd4b2017-02-02 19:15:33 +010023#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/fs.h>
25#include <linux/file.h>
26#include <linux/mm.h>
27#include <linux/mman.h>
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070028#include <linux/mmu_context.h>
Kent Overstreete1bdd5f2013-04-26 10:58:39 +100029#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/slab.h>
31#include <linux/timer.h>
32#include <linux/aio.h>
33#include <linux/highmem.h>
34#include <linux/workqueue.h>
35#include <linux/security.h>
Davide Libenzi9c3060b2007-05-10 22:23:21 -070036#include <linux/eventfd.h>
Jeff Moyercfb1e332009-10-02 18:57:36 -040037#include <linux/blkdev.h>
Jeff Moyer9d85cba72010-05-26 14:44:26 -070038#include <linux/compat.h>
Gu Zheng36bc08c2013-07-16 17:56:16 +080039#include <linux/migrate.h>
40#include <linux/ramfs.h>
Kent Overstreet723be6e2013-05-28 15:14:48 -070041#include <linux/percpu-refcount.h>
Benjamin LaHaise71ad7492013-09-17 10:18:25 -040042#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <asm/kmap_types.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080045#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Al Viro68d70d02013-06-19 15:26:04 +040047#include "internal.h"
48
Kent Overstreet4e179bc2013-05-07 16:18:33 -070049#define AIO_RING_MAGIC 0xa10a10a1
50#define AIO_RING_COMPAT_FEATURES 1
51#define AIO_RING_INCOMPAT_FEATURES 0
52struct aio_ring {
53 unsigned id; /* kernel internal index number */
54 unsigned nr; /* number of io_events */
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -040055 unsigned head; /* Written to by userland or under ring_lock
56 * mutex by aio_read_events_ring(). */
Kent Overstreet4e179bc2013-05-07 16:18:33 -070057 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 Overstreet4e179bc2013-05-07 16:18:33 -070069
Benjamin LaHaisedb446a02013-07-30 12:54:40 -040070struct kioctx_table {
Tejun Heod0264c02018-03-14 12:10:17 -070071 struct rcu_head rcu;
72 unsigned nr;
73 struct kioctx __rcu *table[];
Benjamin LaHaisedb446a02013-07-30 12:54:40 -040074};
75
Kent Overstreete1bdd5f2013-04-26 10:58:39 +100076struct kioctx_cpu {
77 unsigned reqs_available;
78};
79
Jens Axboedc48e562015-04-15 11:17:23 -060080struct ctx_rq_wait {
81 struct completion comp;
82 atomic_t count;
83};
84
Kent Overstreet4e179bc2013-05-07 16:18:33 -070085struct kioctx {
Kent Overstreet723be6e2013-05-28 15:14:48 -070086 struct percpu_ref users;
Kent Overstreet36f55882013-05-07 16:18:41 -070087 atomic_t dead;
Kent Overstreet4e179bc2013-05-07 16:18:33 -070088
Kent Overstreete34ecee2013-10-10 19:31:47 -070089 struct percpu_ref reqs;
90
Kent Overstreet4e179bc2013-05-07 16:18:33 -070091 unsigned long user_id;
Kent Overstreet4e179bc2013-05-07 16:18:33 -070092
Kent Overstreete1bdd5f2013-04-26 10:58:39 +100093 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 Overstreet3e845ce2013-05-07 16:18:51 -0700100 /*
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 Overstreet58c85dc2013-05-07 16:18:55 -0700104 * The real limit is nr_events - 1, which will be larger (see
Kent Overstreet3e845ce2013-05-07 16:18:51 -0700105 * aio_setup_ring())
106 */
Kent Overstreet4e179bc2013-05-07 16:18:33 -0700107 unsigned max_reqs;
108
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700109 /* Size of ringbuffer, in units of struct io_event */
110 unsigned nr_events;
Kent Overstreet4e179bc2013-05-07 16:18:33 -0700111
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700112 unsigned long mmap_base;
113 unsigned long mmap_size;
114
115 struct page **ring_pages;
116 long nr_pages;
117
Tejun Heof7298632018-03-14 12:45:15 -0700118 struct rcu_work free_rwork; /* see free_ioctx() */
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700119
Anatol Pomozove02ba722014-04-15 11:31:33 -0700120 /*
121 * signals when all in-flight requests are done
122 */
Jens Axboedc48e562015-04-15 11:17:23 -0600123 struct ctx_rq_wait *rq_wait;
Anatol Pomozove02ba722014-04-15 11:31:33 -0700124
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700125 struct {
Kent Overstreet34e83fc2013-04-26 10:58:39 +1000126 /*
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 Overstreete1bdd5f2013-04-26 10:58:39 +1000131 *
132 * We batch accesses to it with a percpu version.
Kent Overstreet34e83fc2013-04-26 10:58:39 +1000133 */
134 atomic_t reqs_available;
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700135 } ____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 Overstreet58c85dc2013-05-07 16:18:55 -0700142 struct {
143 struct mutex ring_lock;
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700144 wait_queue_head_t wait;
145 } ____cacheline_aligned_in_smp;
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700146
147 struct {
148 unsigned tail;
Benjamin LaHaised856f322014-08-24 13:14:05 -0400149 unsigned completed_events;
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700150 spinlock_t completion_lock;
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700151 } ____cacheline_aligned_in_smp;
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700152
153 struct page *internal_pages[AIO_RING_PAGES];
Gu Zheng36bc08c2013-07-16 17:56:16 +0800154 struct file *aio_ring_file;
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400155
156 unsigned id;
Kent Overstreet4e179bc2013-05-07 16:18:33 -0700157};
158
Christoph Hellwiga3c0d432018-03-27 19:18:57 +0200159struct fsync_iocb {
160 struct work_struct work;
161 struct file *file;
162 bool datasync;
163};
164
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100165/*
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
178struct aio_kiocb {
Christoph Hellwig54843f82018-05-02 19:57:21 +0200179 union {
180 struct kiocb rw;
Christoph Hellwiga3c0d432018-03-27 19:18:57 +0200181 struct fsync_iocb fsync;
Christoph Hellwig54843f82018-05-02 19:57:21 +0200182 };
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100183
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 Torvalds1da177e2005-04-16 15:20:36 -0700200/*------ sysctl variables----*/
Zach Brownd55b5fd2005-11-07 00:59:31 -0800201static DEFINE_SPINLOCK(aio_nr_lock);
202unsigned long aio_nr; /* current system wide number of aio requests */
203unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/*----end sysctl variables---*/
205
Christoph Lametere18b8902006-12-06 20:33:20 -0800206static struct kmem_cache *kiocb_cachep;
207static struct kmem_cache *kioctx_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400209static struct vfsmount *aio_mnt;
210
211static const struct file_operations aio_ring_fops;
212static const struct address_space_operations aio_ctx_aops;
213
214static 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 Carpenter7f626562013-11-13 10:49:40 +0300220 if (IS_ERR(inode))
221 return ERR_CAST(inode);
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400222
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 LaHaise71ad7492013-09-17 10:18:25 -0400242 return file;
243}
244
245static 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 Horn22f6b4d2016-09-16 00:31:22 +0200251 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 LaHaise71ad7492013-09-17 10:18:25 -0400257}
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259/* 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 */
263static int __init aio_setup(void)
264{
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400265 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 Hellwig04b2fa92015-02-02 14:49:06 +0100274 kiocb_cachep = KMEM_CACHE(aio_kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
Christoph Lameter0a31bd52007-05-06 14:49:57 -0700275 kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return 0;
277}
H Hartley Sweeten385773e2009-09-22 16:43:53 -0700278__initcall(aio_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400280static void put_aio_ring_file(struct kioctx *ctx)
281{
282 struct file *aio_ring_file = ctx->aio_ring_file;
Rasmus Villemoesde04e762016-09-15 00:25:03 +0200283 struct address_space *i_mapping;
284
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400285 if (aio_ring_file) {
Al Viro45063092016-12-04 18:24:56 -0500286 truncate_setsize(file_inode(aio_ring_file), 0);
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400287
288 /* Prevent further access to the kioctx from migratepages */
Al Viro45063092016-12-04 18:24:56 -0500289 i_mapping = aio_ring_file->f_mapping;
Rasmus Villemoesde04e762016-09-15 00:25:03 +0200290 spin_lock(&i_mapping->private_lock);
291 i_mapping->private_data = NULL;
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400292 ctx->aio_ring_file = NULL;
Rasmus Villemoesde04e762016-09-15 00:25:03 +0200293 spin_unlock(&i_mapping->private_lock);
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400294
295 fput(aio_ring_file);
296 }
297}
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299static void aio_free_ring(struct kioctx *ctx)
300{
Gu Zheng36bc08c2013-07-16 17:56:16 +0800301 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400303 /* 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 Zheng36bc08c2013-07-16 17:56:16 +0800308 for (i = 0; i < ctx->nr_pages; i++) {
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500309 struct page *page;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800310 pr_debug("pid(%d) [%d] page->count=%d\n", current->pid, i,
311 page_count(ctx->ring_pages[i]));
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500312 page = ctx->ring_pages[i];
313 if (!page)
314 continue;
315 ctx->ring_pages[i] = NULL;
316 put_page(page);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Sasha Levinddb8c452013-11-19 17:33:03 -0500319 if (ctx->ring_pages && ctx->ring_pages != ctx->internal_pages) {
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700320 kfree(ctx->ring_pages);
Sasha Levinddb8c452013-11-19 17:33:03 -0500321 ctx->ring_pages = NULL;
322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Oleg Nesterov5477e702015-09-04 15:48:04 -0700325static int aio_ring_mremap(struct vm_area_struct *vma)
Gu Zheng36bc08c2013-07-16 17:56:16 +0800326{
Oleg Nesterov5477e702015-09-04 15:48:04 -0700327 struct file *file = vma->vm_file;
Pavel Emelyanove4a0d3e2014-09-18 19:56:17 +0400328 struct mm_struct *mm = vma->vm_mm;
329 struct kioctx_table *table;
Al Virob2edffd2015-04-06 17:48:54 -0400330 int i, res = -EINVAL;
Pavel Emelyanove4a0d3e2014-09-18 19:56:17 +0400331
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 Heod0264c02018-03-14 12:10:17 -0700338 ctx = rcu_dereference(table->table[i]);
Pavel Emelyanove4a0d3e2014-09-18 19:56:17 +0400339 if (ctx && ctx->aio_ring_file == file) {
Al Virob2edffd2015-04-06 17:48:54 -0400340 if (!atomic_read(&ctx->dead)) {
341 ctx->user_id = ctx->mmap_base = vma->vm_start;
342 res = 0;
343 }
Pavel Emelyanove4a0d3e2014-09-18 19:56:17 +0400344 break;
345 }
346 }
347
348 rcu_read_unlock();
349 spin_unlock(&mm->ioctx_lock);
Al Virob2edffd2015-04-06 17:48:54 -0400350 return res;
Pavel Emelyanove4a0d3e2014-09-18 19:56:17 +0400351}
352
Oleg Nesterov5477e702015-09-04 15:48:04 -0700353static 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
362static 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 Zheng36bc08c2013-07-16 17:56:16 +0800369static const struct file_operations aio_ring_fops = {
370 .mmap = aio_ring_mmap,
371};
372
Benjamin LaHaise0c453552013-07-17 09:34:24 -0400373#if IS_ENABLED(CONFIG_MIGRATION)
Gu Zheng36bc08c2013-07-16 17:56:16 +0800374static int aio_migratepage(struct address_space *mapping, struct page *new,
375 struct page *old, enum migrate_mode mode)
376{
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400377 struct kioctx *ctx;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800378 unsigned long flags;
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400379 pgoff_t idx;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800380 int rc;
381
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700382 /*
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 LaHaise8e321fe2013-12-21 17:56:08 -0500390 rc = 0;
391
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400392 /* mapping->private_lock here protects against the kioctx teardown. */
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500393 spin_lock(&mapping->private_lock);
394 ctx = mapping->private_data;
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400395 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 LaHaise8e321fe2013-12-21 17:56:08 -0500414 } else
415 rc = -EINVAL;
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500416
417 if (rc != 0)
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400418 goto out_unlock;
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500419
Gu Zheng36bc08c2013-07-16 17:56:16 +0800420 /* Writeback must be complete */
421 BUG_ON(PageWriteback(old));
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500422 get_page(new);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800423
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500424 rc = migrate_page_move_mapping(mapping, new, old, NULL, mode, 1);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800425 if (rc != MIGRATEPAGE_SUCCESS) {
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500426 put_page(new);
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400427 goto out_unlock;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800428 }
429
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400430 /* 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 LaHaise5e9ae2e2013-09-26 20:34:51 -0400433 */
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400434 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
443out_unlock:
444 mutex_unlock(&ctx->ring_lock);
445out:
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400446 spin_unlock(&mapping->private_lock);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800447 return rc;
448}
Benjamin LaHaise0c453552013-07-17 09:34:24 -0400449#endif
Gu Zheng36bc08c2013-07-16 17:56:16 +0800450
451static const struct address_space_operations aio_ctx_aops = {
Gu Zheng835f2522014-11-06 17:46:21 +0800452 .set_page_dirty = __set_page_dirty_no_writeback,
Benjamin LaHaise0c453552013-07-17 09:34:24 -0400453#if IS_ENABLED(CONFIG_MIGRATION)
Gu Zheng36bc08c2013-07-16 17:56:16 +0800454 .migratepage = aio_migratepage,
Benjamin LaHaise0c453552013-07-17 09:34:24 -0400455#endif
Gu Zheng36bc08c2013-07-16 17:56:16 +0800456};
457
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300458static int aio_setup_ring(struct kioctx *ctx, unsigned int nr_events)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 struct aio_ring *ring;
Zach Brown41003a72013-05-07 16:18:25 -0700461 struct mm_struct *mm = current->mm;
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900462 unsigned long size, unused;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 int nr_pages;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800464 int i;
465 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
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 Torvalds1da177e2005-04-16 15:20:36 -0700472
Gu Zheng36bc08c2013-07-16 17:56:16 +0800473 nr_pages = PFN_UP(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (nr_pages < 0)
475 return -EINVAL;
476
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400477 file = aio_private_file(ctx, nr_pages);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800478 if (IS_ERR(file)) {
479 ctx->aio_ring_file = NULL;
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400480 return -ENOMEM;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Gu Zheng36bc08c2013-07-16 17:56:16 +0800483 ctx->aio_ring_file = file;
484 nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring))
485 / sizeof(struct io_event);
486
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700487 ctx->ring_pages = ctx->internal_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (nr_pages > AIO_RING_PAGES) {
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700489 ctx->ring_pages = kcalloc(nr_pages, sizeof(struct page *),
490 GFP_KERNEL);
Gu Zhengd1b94322013-12-04 18:19:06 +0800491 if (!ctx->ring_pages) {
492 put_aio_ring_file(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return -ENOMEM;
Gu Zhengd1b94322013-12-04 18:19:06 +0800494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900497 for (i = 0; i < nr_pages; i++) {
498 struct page *page;
Al Viro45063092016-12-04 18:24:56 -0500499 page = find_or_create_page(file->f_mapping,
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900500 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 Torvalds3dc9acb2013-12-20 05:11:12 +0900506 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 LaHaisefa8a53c2014-03-28 10:14:45 -0400514 return -ENOMEM;
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900515 }
516
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700517 ctx->mmap_size = nr_pages * PAGE_SIZE;
518 pr_debug("attempting mmap of %lu bytes\n", ctx->mmap_size);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800519
Michal Hocko013373e2016-05-23 16:25:59 -0700520 if (down_write_killable(&mm->mmap_sem)) {
521 ctx->mmap_size = 0;
522 aio_free_ring(ctx);
523 return -EINTR;
524 }
525
Gu Zheng36bc08c2013-07-16 17:56:16 +0800526 ctx->mmap_base = do_mmap_pgoff(ctx->aio_ring_file, 0, ctx->mmap_size,
527 PROT_READ | PROT_WRITE,
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800528 MAP_SHARED, 0, &unused, NULL);
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900529 up_write(&mm->mmap_sem);
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700530 if (IS_ERR((void *)ctx->mmap_base)) {
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700531 ctx->mmap_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 aio_free_ring(ctx);
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400533 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700536 pr_debug("mmap address: 0x%08lx\n", ctx->mmap_base);
Benjamin LaHaised6c355c2013-09-09 11:57:59 -0400537
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700538 ctx->user_id = ctx->mmap_base;
539 ctx->nr_events = nr_events; /* trusted copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700541 ring = kmap_atomic(ctx->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 ring->nr = nr_events; /* user copy */
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400543 ring->id = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 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 Wange8e3c3d2011-11-25 23:14:27 +0800549 kunmap_atomic(ring);
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700550 flush_dcache_page(ctx->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 return 0;
553}
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555#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 Hellwig04b2fa92015-02-02 14:49:06 +0100559void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel)
Kent Overstreet0460fef2013-05-07 16:18:49 -0700560{
Christoph Hellwig54843f82018-05-02 19:57:21 +0200561 struct aio_kiocb *req = container_of(iocb, struct aio_kiocb, rw);
Kent Overstreet0460fef2013-05-07 16:18:49 -0700562 struct kioctx *ctx = req->ki_ctx;
563 unsigned long flags;
564
Christoph Hellwig75321b52018-04-09 14:57:56 +0200565 if (WARN_ON_ONCE(!list_empty(&req->ki_list)))
566 return;
567
Kent Overstreet0460fef2013-05-07 16:18:49 -0700568 spin_lock_irqsave(&ctx->ctx_lock, flags);
Christoph Hellwig75321b52018-04-09 14:57:56 +0200569 list_add_tail(&req->ki_list, &ctx->active_reqs);
Kent Overstreet0460fef2013-05-07 16:18:49 -0700570 req->ki_cancel = cancel;
Kent Overstreet0460fef2013-05-07 16:18:49 -0700571 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
572}
573EXPORT_SYMBOL(kiocb_set_cancel_fn);
574
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100575static int kiocb_cancel(struct aio_kiocb *kiocb)
Kent Overstreet906b9732013-05-07 16:18:31 -0700576{
Kent Overstreet0460fef2013-05-07 16:18:49 -0700577 kiocb_cancel_fn *old, *cancel;
Kent Overstreet906b9732013-05-07 16:18:31 -0700578
Kent Overstreet0460fef2013-05-07 16:18:49 -0700579 /*
580 * Don't want to set kiocb->ki_cancel = KIOCB_CANCELLED unless it
581 * actually has a cancel function, hence the cmpxchg()
582 */
Kent Overstreet906b9732013-05-07 16:18:31 -0700583
Mark Rutland6aa7de02017-10-23 14:07:29 -0700584 cancel = READ_ONCE(kiocb->ki_cancel);
Kent Overstreet0460fef2013-05-07 16:18:49 -0700585 do {
586 if (!cancel || cancel == KIOCB_CANCELLED)
Kent Overstreet57282d82013-05-13 13:42:52 -0700587 return -EINVAL;
Kent Overstreet906b9732013-05-07 16:18:31 -0700588
Kent Overstreet0460fef2013-05-07 16:18:49 -0700589 old = cancel;
590 cancel = cmpxchg(&kiocb->ki_cancel, old, KIOCB_CANCELLED);
591 } while (cancel != old);
592
Christoph Hellwig54843f82018-05-02 19:57:21 +0200593 return cancel(&kiocb->rw);
Kent Overstreet906b9732013-05-07 16:18:31 -0700594}
595
Tejun Heoa6d7cff2018-03-14 12:10:17 -0700596/*
597 * free_ioctx() should be RCU delayed to synchronize against the RCU
598 * protected lookup_ioctx() and also needs process context to call
Tejun Heof7298632018-03-14 12:45:15 -0700599 * aio_free_ring(). Use rcu_work.
Tejun Heoa6d7cff2018-03-14 12:10:17 -0700600 */
Kent Overstreete34ecee2013-10-10 19:31:47 -0700601static void free_ioctx(struct work_struct *work)
Kent Overstreet36f55882013-05-07 16:18:41 -0700602{
Tejun Heof7298632018-03-14 12:45:15 -0700603 struct kioctx *ctx = container_of(to_rcu_work(work), struct kioctx,
604 free_rwork);
Kent Overstreete34ecee2013-10-10 19:31:47 -0700605 pr_debug("freeing %p\n", ctx);
606
607 aio_free_ring(ctx);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000608 free_percpu(ctx->cpu);
Tejun Heo9a1049d2014-06-28 08:10:14 -0400609 percpu_ref_exit(&ctx->reqs);
610 percpu_ref_exit(&ctx->users);
Kent Overstreet36f55882013-05-07 16:18:41 -0700611 kmem_cache_free(kioctx_cachep, ctx);
612}
613
Kent Overstreete34ecee2013-10-10 19:31:47 -0700614static void free_ioctx_reqs(struct percpu_ref *ref)
615{
616 struct kioctx *ctx = container_of(ref, struct kioctx, reqs);
617
Anatol Pomozove02ba722014-04-15 11:31:33 -0700618 /* At this point we know that there are no any in-flight requests */
Jens Axboedc48e562015-04-15 11:17:23 -0600619 if (ctx->rq_wait && atomic_dec_and_test(&ctx->rq_wait->count))
620 complete(&ctx->rq_wait->comp);
Anatol Pomozove02ba722014-04-15 11:31:33 -0700621
Tejun Heoa6d7cff2018-03-14 12:10:17 -0700622 /* Synchronize against RCU protected table->table[] dereferences */
Tejun Heof7298632018-03-14 12:45:15 -0700623 INIT_RCU_WORK(&ctx->free_rwork, free_ioctx);
624 queue_rcu_work(system_wq, &ctx->free_rwork);
Kent Overstreete34ecee2013-10-10 19:31:47 -0700625}
626
Kent Overstreet36f55882013-05-07 16:18:41 -0700627/*
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 Overstreete34ecee2013-10-10 19:31:47 -0700632static void free_ioctx_users(struct percpu_ref *ref)
Kent Overstreet36f55882013-05-07 16:18:41 -0700633{
Kent Overstreete34ecee2013-10-10 19:31:47 -0700634 struct kioctx *ctx = container_of(ref, struct kioctx, users);
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100635 struct aio_kiocb *req;
Kent Overstreet36f55882013-05-07 16:18:41 -0700636
637 spin_lock_irq(&ctx->ctx_lock);
638
639 while (!list_empty(&ctx->active_reqs)) {
640 req = list_first_entry(&ctx->active_reqs,
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100641 struct aio_kiocb, ki_list);
Kent Overstreet36f55882013-05-07 16:18:41 -0700642
643 list_del_init(&req->ki_list);
Fabian Frederickd52a8f92014-04-22 07:26:58 +0200644 kiocb_cancel(req);
Kent Overstreet36f55882013-05-07 16:18:41 -0700645 }
646
647 spin_unlock_irq(&ctx->ctx_lock);
648
Kent Overstreete34ecee2013-10-10 19:31:47 -0700649 percpu_ref_kill(&ctx->reqs);
650 percpu_ref_put(&ctx->reqs);
Kent Overstreet36f55882013-05-07 16:18:41 -0700651}
652
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400653static 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 Nesterov855ef0d2014-04-30 16:16:36 +0200660 table = rcu_dereference_raw(mm->ioctx_table);
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400661
662 while (1) {
663 if (table)
664 for (i = 0; i < table->nr; i++)
Tejun Heod0264c02018-03-14 12:10:17 -0700665 if (!rcu_access_pointer(table->table[i])) {
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400666 ctx->id = i;
Tejun Heod0264c02018-03-14 12:10:17 -0700667 rcu_assign_pointer(table->table[i], ctx);
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400668 spin_unlock(&mm->ioctx_lock);
669
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400670 /* While kioctx setup is in progress,
671 * we are protected from page migration
672 * changes ring_pages by ->ring_lock.
673 */
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400674 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 LaHaisedb446a02013-07-30 12:54:40 -0400681 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 Nesterov855ef0d2014-04-30 16:16:36 +0200691 old = rcu_dereference_raw(mm->ioctx_table);
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400692
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 Overstreete34ecee2013-10-10 19:31:47 -0700708static 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 Torvalds1da177e2005-04-16 15:20:36 -0700718/* ioctx_alloc
719 * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
720 */
721static struct kioctx *ioctx_alloc(unsigned nr_events)
722{
Zach Brown41003a72013-05-07 16:18:25 -0700723 struct mm_struct *mm = current->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 struct kioctx *ctx;
Al Viroe23754f2012-03-06 14:33:22 -0500725 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000727 /*
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300728 * 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 Overstreete1bdd5f2013-04-26 10:58:39 +1000734 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700745 /* Prevent overflows */
Al Viro08397ac2015-03-31 11:43:52 -0400746 if (nr_events > (0x10000000U / sizeof(struct io_event))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 pr_debug("ENOMEM: nr_events too high\n");
748 return ERR_PTR(-EINVAL);
749 }
750
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300751 if (!nr_events || (unsigned long)max_reqs > aio_max_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return ERR_PTR(-EAGAIN);
753
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800754 ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (!ctx)
756 return ERR_PTR(-ENOMEM);
757
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300758 ctx->max_reqs = max_reqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400760 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 Heo2aad2a82014-09-24 13:31:50 -0400770 if (percpu_ref_init(&ctx->users, free_ioctx_users, 0, GFP_KERNEL))
Kent Overstreete34ecee2013-10-10 19:31:47 -0700771 goto err;
772
Tejun Heo2aad2a82014-09-24 13:31:50 -0400773 if (percpu_ref_init(&ctx->reqs, free_ioctx_reqs, 0, GFP_KERNEL))
Kent Overstreete34ecee2013-10-10 19:31:47 -0700774 goto err;
Kent Overstreet723be6e2013-05-28 15:14:48 -0700775
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000776 ctx->cpu = alloc_percpu(struct kioctx_cpu);
777 if (!ctx->cpu)
Kent Overstreete34ecee2013-10-10 19:31:47 -0700778 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300780 err = aio_setup_ring(ctx, nr_events);
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400781 if (err < 0)
Kent Overstreete34ecee2013-10-10 19:31:47 -0700782 goto err;
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000783
Kent Overstreet34e83fc2013-04-26 10:58:39 +1000784 atomic_set(&ctx->reqs_available, ctx->nr_events - 1);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000785 ctx->req_batch = (ctx->nr_events - 1) / (num_possible_cpus() * 4);
Benjamin LaHaise6878ea72013-07-31 10:34:18 -0400786 if (ctx->req_batch < 1)
787 ctx->req_batch = 1;
Kent Overstreet34e83fc2013-04-26 10:58:39 +1000788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 /* limit the number of system wide aios */
Al Viro9fa1cb32012-03-10 23:14:05 -0500790 spin_lock(&aio_nr_lock);
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300791 if (aio_nr + ctx->max_reqs > aio_max_nr ||
792 aio_nr + ctx->max_reqs < aio_nr) {
Al Viro9fa1cb32012-03-10 23:14:05 -0500793 spin_unlock(&aio_nr_lock);
Kent Overstreete34ecee2013-10-10 19:31:47 -0700794 err = -EAGAIN;
Gu Zhengd1b94322013-12-04 18:19:06 +0800795 goto err_ctx;
Al Viro2dd542b2012-03-10 23:10:35 -0500796 }
797 aio_nr += ctx->max_reqs;
Al Viro9fa1cb32012-03-10 23:14:05 -0500798 spin_unlock(&aio_nr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Benjamin LaHaise18816862013-12-21 15:49:28 -0500800 percpu_ref_get(&ctx->users); /* io_setup() will drop this ref */
801 percpu_ref_get(&ctx->reqs); /* free_ioctx_users() will drop this */
Kent Overstreet723be6e2013-05-28 15:14:48 -0700802
Benjamin LaHaiseda903822013-08-05 13:21:43 -0400803 err = ioctx_add_table(ctx, mm);
804 if (err)
Kent Overstreete34ecee2013-10-10 19:31:47 -0700805 goto err_cleanup;
Benjamin LaHaiseda903822013-08-05 13:21:43 -0400806
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400807 /* Release the ring_lock mutex now that all setup is complete. */
808 mutex_unlock(&ctx->ring_lock);
809
Kent Overstreetcaf41672013-05-07 16:18:35 -0700810 pr_debug("allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700811 ctx, ctx->user_id, mm, ctx->nr_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return ctx;
813
Kent Overstreete34ecee2013-10-10 19:31:47 -0700814err_cleanup:
815 aio_nr_sub(ctx->max_reqs);
Gu Zhengd1b94322013-12-04 18:19:06 +0800816err_ctx:
Al Virodeeb8522015-04-06 17:57:44 -0400817 atomic_set(&ctx->dead, 1);
818 if (ctx->mmap_size)
819 vm_munmap(ctx->mmap_base, ctx->mmap_size);
Gu Zhengd1b94322013-12-04 18:19:06 +0800820 aio_free_ring(ctx);
Kent Overstreete34ecee2013-10-10 19:31:47 -0700821err:
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400822 mutex_unlock(&ctx->ring_lock);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000823 free_percpu(ctx->cpu);
Tejun Heo9a1049d2014-06-28 08:10:14 -0400824 percpu_ref_exit(&ctx->reqs);
825 percpu_ref_exit(&ctx->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 kmem_cache_free(kioctx_cachep, ctx);
Kent Overstreetcaf41672013-05-07 16:18:35 -0700827 pr_debug("error allocating ioctx %d\n", err);
Al Viroe23754f2012-03-06 14:33:22 -0500828 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Kent Overstreet36f55882013-05-07 16:18:41 -0700831/* 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 Torvalds1da177e2005-04-16 15:20:36 -0700834 * the rapid destruction of the kioctx.
835 */
Benjamin LaHaisefb2d4482014-04-29 12:45:17 -0400836static int kill_ioctx(struct mm_struct *mm, struct kioctx *ctx,
Jens Axboedc48e562015-04-15 11:17:23 -0600837 struct ctx_rq_wait *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400839 struct kioctx_table *table;
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400840
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400841 spin_lock(&mm->ioctx_lock);
Al Virob2edffd2015-04-06 17:48:54 -0400842 if (atomic_xchg(&ctx->dead, 1)) {
843 spin_unlock(&mm->ioctx_lock);
844 return -EINVAL;
845 }
846
Oleg Nesterov855ef0d2014-04-30 16:16:36 +0200847 table = rcu_dereference_raw(mm->ioctx_table);
Tejun Heod0264c02018-03-14 12:10:17 -0700848 WARN_ON(ctx != rcu_access_pointer(table->table[ctx->id]));
849 RCU_INIT_POINTER(table->table[ctx->id], NULL);
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400850 spin_unlock(&mm->ioctx_lock);
Kent Overstreet4fcc7122013-06-12 14:04:59 -0700851
Tejun Heoa6d7cff2018-03-14 12:10:17 -0700852 /* free_ioctx_reqs() will do the necessary RCU synchronization */
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400853 wake_up_all(&ctx->wait);
Kent Overstreet4fcc7122013-06-12 14:04:59 -0700854
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400855 /*
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 LaHaisefb2d4482014-04-29 12:45:17 -0400863
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400864 if (ctx->mmap_size)
865 vm_munmap(ctx->mmap_base, ctx->mmap_size);
866
Jens Axboedc48e562015-04-15 11:17:23 -0600867 ctx->rq_wait = wait;
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400868 percpu_ref_kill(&ctx->users);
869 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
Kent Overstreet36f55882013-05-07 16:18:41 -0700872/*
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 Torvalds1da177e2005-04-16 15:20:36 -0700879 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800880void exit_aio(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200882 struct kioctx_table *table = rcu_dereference_raw(mm->ioctx_table);
Jens Axboedc48e562015-04-15 11:17:23 -0600883 struct ctx_rq_wait wait;
884 int i, skipped;
Jens Axboeabf137d2008-12-09 08:11:22 +0100885
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200886 if (!table)
887 return;
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400888
Jens Axboedc48e562015-04-15 11:17:23 -0600889 atomic_set(&wait.count, table->nr);
890 init_completion(&wait.comp);
891
892 skipped = 0;
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200893 for (i = 0; i < table->nr; ++i) {
Tejun Heod0264c02018-03-14 12:10:17 -0700894 struct kioctx *ctx =
895 rcu_dereference_protected(table->table[i], true);
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400896
Jens Axboedc48e562015-04-15 11:17:23 -0600897 if (!ctx) {
898 skipped++;
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200899 continue;
Jens Axboedc48e562015-04-15 11:17:23 -0600900 }
901
Al Viro936af152012-04-20 21:49:41 -0400902 /*
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200903 * 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 Viro936af152012-04-20 21:49:41 -0400908 */
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700909 ctx->mmap_size = 0;
Jens Axboedc48e562015-04-15 11:17:23 -0600910 kill_ioctx(mm, ctx, &wait);
911 }
Kent Overstreet36f55882013-05-07 16:18:41 -0700912
Jens Axboedc48e562015-04-15 11:17:23 -0600913 if (!atomic_sub_and_test(skipped, &wait.count)) {
Gu Zheng6098b452014-09-03 17:45:44 +0800914 /* Wait until all IO for the context are done. */
Jens Axboedc48e562015-04-15 11:17:23 -0600915 wait_for_completion(&wait.comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200917
918 RCU_INIT_POINTER(mm->ioctx_table, NULL);
919 kfree(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000922static void put_reqs_available(struct kioctx *ctx, unsigned nr)
923{
924 struct kioctx_cpu *kcpu;
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400925 unsigned long flags;
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000926
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400927 local_irq_save(flags);
Benjamin LaHaisebe6fb452014-07-22 09:56:56 -0400928 kcpu = this_cpu_ptr(ctx->cpu);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000929 kcpu->reqs_available += nr;
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400930
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000931 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 LaHaise263782c2014-07-14 12:49:26 -0400936 local_irq_restore(flags);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000937}
938
939static bool get_reqs_available(struct kioctx *ctx)
940{
941 struct kioctx_cpu *kcpu;
942 bool ret = false;
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400943 unsigned long flags;
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000944
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400945 local_irq_save(flags);
Benjamin LaHaisebe6fb452014-07-22 09:56:56 -0400946 kcpu = this_cpu_ptr(ctx->cpu);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000947 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--;
964out:
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400965 local_irq_restore(flags);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000966 return ret;
967}
968
Benjamin LaHaised856f322014-08-24 13:14:05 -0400969/* 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 */
976static 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 */
1005static 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 Torvalds1da177e2005-04-16 15:20:36 -07001031/* aio_get_req
Kent Overstreet57282d82013-05-13 13:42:52 -07001032 * Allocate a slot for an aio request.
1033 * Returns NULL if no requests are free.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 */
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001035static inline struct aio_kiocb *aio_get_req(struct kioctx *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001037 struct aio_kiocb *req;
Kent Overstreeta1c8eae2013-05-07 16:18:53 -07001038
Benjamin LaHaised856f322014-08-24 13:14:05 -04001039 if (!get_reqs_available(ctx)) {
1040 user_refill_reqs_available(ctx);
1041 if (!get_reqs_available(ctx))
1042 return NULL;
1043 }
Kent Overstreeta1c8eae2013-05-07 16:18:53 -07001044
Kent Overstreet0460fef2013-05-07 16:18:49 -07001045 req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL|__GFP_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (unlikely(!req))
Kent Overstreeta1c8eae2013-05-07 16:18:53 -07001047 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Kent Overstreete34ecee2013-10-10 19:31:47 -07001049 percpu_ref_get(&ctx->reqs);
Christoph Hellwig75321b52018-04-09 14:57:56 +02001050 INIT_LIST_HEAD(&req->ki_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 req->ki_ctx = ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return req;
Kent Overstreeta1c8eae2013-05-07 16:18:53 -07001053out_put:
Kent Overstreete1bdd5f2013-04-26 10:58:39 +10001054 put_reqs_available(ctx, 1);
Kent Overstreeta1c8eae2013-05-07 16:18:53 -07001055 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
Adrian Bunkd5470b52008-04-29 00:58:57 -07001058static struct kioctx *lookup_ioctx(unsigned long ctx_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Benjamin LaHaisedb446a02013-07-30 12:54:40 -04001060 struct aio_ring __user *ring = (void __user *)ctx_id;
Jens Axboeabf137d2008-12-09 08:11:22 +01001061 struct mm_struct *mm = current->mm;
Jeff Moyer65c24492009-03-18 17:04:21 -07001062 struct kioctx *ctx, *ret = NULL;
Benjamin LaHaisedb446a02013-07-30 12:54:40 -04001063 struct kioctx_table *table;
1064 unsigned id;
1065
1066 if (get_user(id, &ring->id))
1067 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Jens Axboeabf137d2008-12-09 08:11:22 +01001069 rcu_read_lock();
Benjamin LaHaisedb446a02013-07-30 12:54:40 -04001070 table = rcu_dereference(mm->ioctx_table);
Jens Axboeabf137d2008-12-09 08:11:22 +01001071
Benjamin LaHaisedb446a02013-07-30 12:54:40 -04001072 if (!table || id >= table->nr)
1073 goto out;
1074
Tejun Heod0264c02018-03-14 12:10:17 -07001075 ctx = rcu_dereference(table->table[id]);
Benjamin LaHaisef30d7042013-08-07 18:23:48 -04001076 if (ctx && ctx->user_id == ctx_id) {
Benjamin LaHaisedb446a02013-07-30 12:54:40 -04001077 percpu_ref_get(&ctx->users);
1078 ret = ctx;
Jens Axboeabf137d2008-12-09 08:11:22 +01001079 }
Benjamin LaHaisedb446a02013-07-30 12:54:40 -04001080out:
Jens Axboeabf137d2008-12-09 08:11:22 +01001081 rcu_read_unlock();
Jeff Moyer65c24492009-03-18 17:04:21 -07001082 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085/* aio_complete
1086 * Called when the io request on the given iocb is complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 */
Christoph Hellwig54843f82018-05-02 19:57:21 +02001088static void aio_complete(struct aio_kiocb *iocb, long res, long res2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
1090 struct kioctx *ctx = iocb->ki_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 struct aio_ring *ring;
Kent Overstreet21b40202013-05-07 16:18:47 -07001092 struct io_event *ev_page, *event;
Benjamin LaHaised856f322014-08-24 13:14:05 -04001093 unsigned tail, pos, head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Christoph Hellwig75321b52018-04-09 14:57:56 +02001096 if (!list_empty_careful(&iocb->ki_list)) {
Kent Overstreet0460fef2013-05-07 16:18:49 -07001097 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 Overstreet11599eb2013-05-07 16:18:39 -07001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 /*
Kent Overstreet0460fef2013-05-07 16:18:49 -07001105 * Add a completion event to the ring buffer. Must be done holding
Tang Chen4b30f072013-07-03 15:09:16 -07001106 * ctx->completion_lock to prevent other code from messing with the tail
Kent Overstreet0460fef2013-05-07 16:18:49 -07001107 * pointer since we might be called from irq context.
1108 */
1109 spin_lock_irqsave(&ctx->completion_lock, flags);
1110
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001111 tail = ctx->tail;
Kent Overstreet21b40202013-05-07 16:18:47 -07001112 pos = tail + AIO_EVENTS_OFFSET;
1113
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001114 if (++tail >= ctx->nr_events)
Ken Chen4bf69b22005-05-01 08:59:15 -07001115 tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001117 ev_page = kmap_atomic(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
Kent Overstreet21b40202013-05-07 16:18:47 -07001118 event = ev_page + pos % AIO_EVENTS_PER_PAGE;
1119
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001120 event->obj = (u64)(unsigned long)iocb->ki_user_iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 event->data = iocb->ki_user_data;
1122 event->res = res;
1123 event->res2 = res2;
1124
Kent Overstreet21b40202013-05-07 16:18:47 -07001125 kunmap_atomic(ev_page);
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001126 flush_dcache_page(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
Kent Overstreet21b40202013-05-07 16:18:47 -07001127
1128 pr_debug("%p[%u]: %p: %p %Lx %lx %lx\n",
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001129 ctx, tail, iocb, iocb->ki_user_iocb, iocb->ki_user_data,
Kent Overstreetcaf41672013-05-07 16:18:35 -07001130 res, res2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
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 Overstreet58c85dc2013-05-07 16:18:55 -07001137 ctx->tail = tail;
Kent Overstreet21b40202013-05-07 16:18:47 -07001138
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001139 ring = kmap_atomic(ctx->ring_pages[0]);
Benjamin LaHaised856f322014-08-24 13:14:05 -04001140 head = ring->head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 ring->tail = tail;
Cong Wange8e3c3d2011-11-25 23:14:27 +08001142 kunmap_atomic(ring);
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001143 flush_dcache_page(ctx->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Benjamin LaHaised856f322014-08-24 13:14:05 -04001145 ctx->completed_events++;
1146 if (ctx->completed_events > 1)
1147 refill_reqs_available(ctx, head, tail);
Kent Overstreet0460fef2013-05-07 16:18:49 -07001148 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1149
Kent Overstreet21b40202013-05-07 16:18:47 -07001150 pr_debug("added to ring %p at [%u]\n", iocb, tail);
Davide Libenzi8d1c98b2008-04-10 21:29:19 -07001151
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 Hellwig54843f82018-05-02 19:57:21 +02001157 if (iocb->ki_eventfd) {
Davide Libenzi8d1c98b2008-04-10 21:29:19 -07001158 eventfd_signal(iocb->ki_eventfd, 1);
Christoph Hellwig54843f82018-05-02 19:57:21 +02001159 eventfd_ctx_put(iocb->ki_eventfd);
1160 }
Davide Libenzi8d1c98b2008-04-10 21:29:19 -07001161
Christoph Hellwig54843f82018-05-02 19:57:21 +02001162 kmem_cache_free(kiocb_cachep, iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Quentin Barnes6cb2a212008-03-19 17:00:39 -07001164 /*
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 Torvalds1da177e2005-04-16 15:20:36 -07001172 if (waitqueue_active(&ctx->wait))
1173 wake_up(&ctx->wait);
1174
Kent Overstreete34ecee2013-10-10 19:31:47 -07001175 percpu_ref_put(&ctx->reqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
Gu Zheng2be4e7d2014-07-23 18:03:53 +08001178/* aio_read_events_ring
Kent Overstreeta31ad382013-05-07 16:18:45 -07001179 * Pull an event off of the ioctx's event ring. Returns the number of
1180 * events fetched
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 */
Kent Overstreeta31ad382013-05-07 16:18:45 -07001182static long aio_read_events_ring(struct kioctx *ctx,
1183 struct io_event __user *event, long nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 struct aio_ring *ring;
Kent Overstreet5ffac122013-05-09 15:36:07 -07001186 unsigned head, tail, pos;
Kent Overstreeta31ad382013-05-07 16:18:45 -07001187 long ret = 0;
1188 int copy_ret;
1189
Dave Chinner9c9ce762015-02-03 19:29:05 -05001190 /*
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 Overstreet58c85dc2013-05-07 16:18:55 -07001197 mutex_lock(&ctx->ring_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -04001199 /* Access to ->ring_pages here is protected by ctx->ring_lock. */
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001200 ring = kmap_atomic(ctx->ring_pages[0]);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001201 head = ring->head;
Kent Overstreet5ffac122013-05-09 15:36:07 -07001202 tail = ring->tail;
Kent Overstreeta31ad382013-05-07 16:18:45 -07001203 kunmap_atomic(ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Jeff Moyer2ff396be2014-09-02 13:17:00 -04001205 /*
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 Overstreet5ffac122013-05-09 15:36:07 -07001211 pr_debug("h%u t%u m%u\n", head, tail, ctx->nr_events);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001212
Kent Overstreet5ffac122013-05-09 15:36:07 -07001213 if (head == tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 goto out;
1215
Benjamin LaHaiseedfbbf32014-06-24 13:32:51 -04001216 head %= ctx->nr_events;
1217 tail %= ctx->nr_events;
1218
Kent Overstreeta31ad382013-05-07 16:18:45 -07001219 while (ret < nr) {
1220 long avail;
1221 struct io_event *ev;
1222 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Kent Overstreet5ffac122013-05-09 15:36:07 -07001224 avail = (head <= tail ? tail : ctx->nr_events) - head;
1225 if (head == tail)
Kent Overstreeta31ad382013-05-07 16:18:45 -07001226 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 Overstreet58c85dc2013-05-07 16:18:55 -07001233 page = ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE];
Kent Overstreeta31ad382013-05-07 16:18:45 -07001234 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 Overstreet58c85dc2013-05-07 16:18:55 -07001248 head %= ctx->nr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001251 ring = kmap_atomic(ctx->ring_pages[0]);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001252 ring->head = head;
Zhao Hongjiang91d80a82013-04-26 11:03:53 +08001253 kunmap_atomic(ring);
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001254 flush_dcache_page(ctx->ring_pages[0]);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001255
Kent Overstreet5ffac122013-05-09 15:36:07 -07001256 pr_debug("%li h%u t%u\n", ret, head, tail);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001257out:
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001258 mutex_unlock(&ctx->ring_lock);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return ret;
1261}
1262
Kent Overstreeta31ad382013-05-07 16:18:45 -07001263static bool aio_read_events(struct kioctx *ctx, long min_nr, long nr,
1264 struct io_event __user *event, long *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Kent Overstreeta31ad382013-05-07 16:18:45 -07001266 long ret = aio_read_events_ring(ctx, event + *i, nr - *i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Kent Overstreeta31ad382013-05-07 16:18:45 -07001268 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 Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Kent Overstreeta31ad382013-05-07 16:18:45 -07001280static long read_events(struct kioctx *ctx, long min_nr, long nr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 struct io_event __user *event,
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07001282 ktime_t until)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Kent Overstreeta31ad382013-05-07 16:18:45 -07001284 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Kent Overstreeta31ad382013-05-07 16:18:45 -07001286 /*
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 Gleixner2456e852016-12-25 11:38:40 +01001300 if (until == 0)
Fam Zheng5f785de2014-11-06 20:44:36 +08001301 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 Torvalds1da177e2005-04-16 15:20:36 -07001306
Kent Overstreeta31ad382013-05-07 16:18:45 -07001307 if (!ret && signal_pending(current))
1308 ret = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Kent Overstreeta31ad382013-05-07 16:18:45 -07001310 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313/* 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 Carstens002c8972009-01-14 14:14:18 +01001326SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
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 Brownd55b5fd2005-11-07 00:59:31 -08001337 if (unlikely(ctx || nr_events == 0)) {
Kinglong Meeacd88d42015-02-04 21:15:59 +08001338 pr_debug("EINVAL: ctx %lu nr_events %u\n",
Zach Brownd55b5fd2005-11-07 00:59:31 -08001339 ctx, nr_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 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 Viroa2e18592012-03-20 16:27:57 -04001347 if (ret)
Anatol Pomozove02ba722014-04-15 11:31:33 -07001348 kill_ioctx(current->mm, ioctx, NULL);
Kent Overstreet723be6e2013-05-28 15:14:48 -07001349 percpu_ref_put(&ioctx->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
1351
1352out:
1353 return ret;
1354}
1355
Al Viroc00d2c72016-12-20 07:04:57 -05001356#ifdef CONFIG_COMPAT
1357COMPAT_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
1384out:
1385 return ret;
1386}
1387#endif
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389/* 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 Takeuchi642b5122010-08-05 11:23:11 -07001392 * implemented. May fail with -EINVAL if the context pointed to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 * is invalid.
1394 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001395SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
1397 struct kioctx *ioctx = lookup_ioctx(ctx);
1398 if (likely(NULL != ioctx)) {
Jens Axboedc48e562015-04-15 11:17:23 -06001399 struct ctx_rq_wait wait;
Benjamin LaHaisefb2d4482014-04-29 12:45:17 -04001400 int ret;
Anatol Pomozove02ba722014-04-15 11:31:33 -07001401
Jens Axboedc48e562015-04-15 11:17:23 -06001402 init_completion(&wait.comp);
1403 atomic_set(&wait.count, 1);
1404
Anatol Pomozove02ba722014-04-15 11:31:33 -07001405 /* 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 Axboedc48e562015-04-15 11:17:23 -06001409 ret = kill_ioctx(current->mm, ioctx, &wait);
Kent Overstreet723be6e2013-05-28 15:14:48 -07001410 percpu_ref_put(&ioctx->users);
Anatol Pomozove02ba722014-04-15 11:31:33 -07001411
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 LaHaisefb2d4482014-04-29 12:45:17 -04001416 if (!ret)
Jens Axboedc48e562015-04-15 11:17:23 -06001417 wait_for_completion(&wait.comp);
Anatol Pomozove02ba722014-04-15 11:31:33 -07001418
Benjamin LaHaisefb2d4482014-04-29 12:45:17 -04001419 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
Kinglong Meeacd88d42015-02-04 21:15:59 +08001421 pr_debug("EINVAL: invalid context id\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return -EINVAL;
1423}
1424
Christoph Hellwig54843f82018-05-02 19:57:21 +02001425static 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
1445static 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 Hellwig89319d312016-10-30 11:42:03 -05001464static int aio_setup_rw(int rw, struct iocb *iocb, struct iovec **iovec,
1465 bool vectored, bool compat, struct iov_iter *iter)
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001466{
Christoph Hellwig89319d312016-10-30 11:42:03 -05001467 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 Moyer9d85cba72010-05-26 14:44:26 -07001475#ifdef CONFIG_COMPAT
1476 if (compat)
Christoph Hellwig89319d312016-10-30 11:42:03 -05001477 return compat_import_iovec(rw, buf, len, UIO_FASTIOV, iovec,
1478 iter);
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001479#endif
Christoph Hellwig89319d312016-10-30 11:42:03 -05001480 return import_iovec(rw, buf, len, UIO_FASTIOV, iovec, iter);
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001481}
1482
Christoph Hellwig54843f82018-05-02 19:57:21 +02001483static inline ssize_t aio_rw_ret(struct kiocb *req, ssize_t ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Christoph Hellwig89319d312016-10-30 11:42:03 -05001485 switch (ret) {
1486 case -EIOCBQUEUED:
1487 return ret;
1488 case -ERESTARTSYS:
1489 case -ERESTARTNOINTR:
1490 case -ERESTARTNOHAND:
1491 case -ERESTART_RESTARTBLOCK:
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001492 /*
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 Hellwig89319d312016-10-30 11:42:03 -05001496 ret = -EINTR;
1497 /*FALLTHRU*/
1498 default:
Christoph Hellwig54843f82018-05-02 19:57:21 +02001499 aio_complete_rw(req, ret, 0);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001500 return 0;
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001501 }
Christoph Hellwig89319d312016-10-30 11:42:03 -05001502}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Christoph Hellwig89319d312016-10-30 11:42:03 -05001504static ssize_t aio_read(struct kiocb *req, struct iocb *iocb, bool vectored,
1505 bool compat)
1506{
Christoph Hellwig89319d312016-10-30 11:42:03 -05001507 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1508 struct iov_iter iter;
Christoph Hellwig54843f82018-05-02 19:57:21 +02001509 struct file *file;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001510 ssize_t ret;
1511
Christoph Hellwig54843f82018-05-02 19:57:21 +02001512 ret = aio_prep_rw(req, iocb);
1513 if (ret)
1514 return ret;
1515 file = req->ki_filp;
1516
1517 ret = -EBADF;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001518 if (unlikely(!(file->f_mode & FMODE_READ)))
Christoph Hellwig54843f82018-05-02 19:57:21 +02001519 goto out_fput;
1520 ret = -EINVAL;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001521 if (unlikely(!file->f_op->read_iter))
Christoph Hellwig54843f82018-05-02 19:57:21 +02001522 goto out_fput;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001523
1524 ret = aio_setup_rw(READ, iocb, &iovec, vectored, compat, &iter);
1525 if (ret)
Christoph Hellwig54843f82018-05-02 19:57:21 +02001526 goto out_fput;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001527 ret = rw_verify_area(READ, file, &req->ki_pos, iov_iter_count(&iter));
1528 if (!ret)
Christoph Hellwig54843f82018-05-02 19:57:21 +02001529 ret = aio_rw_ret(req, call_read_iter(file, req, &iter));
Christoph Hellwig89319d312016-10-30 11:42:03 -05001530 kfree(iovec);
Christoph Hellwig54843f82018-05-02 19:57:21 +02001531out_fput:
1532 if (unlikely(ret && ret != -EIOCBQUEUED))
1533 fput(file);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001534 return ret;
1535}
1536
1537static ssize_t aio_write(struct kiocb *req, struct iocb *iocb, bool vectored,
1538 bool compat)
1539{
Christoph Hellwig89319d312016-10-30 11:42:03 -05001540 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1541 struct iov_iter iter;
Christoph Hellwig54843f82018-05-02 19:57:21 +02001542 struct file *file;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001543 ssize_t ret;
1544
Christoph Hellwig54843f82018-05-02 19:57:21 +02001545 ret = aio_prep_rw(req, iocb);
1546 if (ret)
1547 return ret;
1548 file = req->ki_filp;
1549
1550 ret = -EBADF;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001551 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Christoph Hellwig54843f82018-05-02 19:57:21 +02001552 goto out_fput;
1553 ret = -EINVAL;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001554 if (unlikely(!file->f_op->write_iter))
Christoph Hellwig54843f82018-05-02 19:57:21 +02001555 goto out_fput;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001556
1557 ret = aio_setup_rw(WRITE, iocb, &iovec, vectored, compat, &iter);
1558 if (ret)
Christoph Hellwig54843f82018-05-02 19:57:21 +02001559 goto out_fput;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001560 ret = rw_verify_area(WRITE, file, &req->ki_pos, iov_iter_count(&iter));
1561 if (!ret) {
Jan Kara70fe2f42016-10-30 11:42:04 -05001562 /*
Christoph Hellwig92ce4722018-04-06 09:28:17 +02001563 * Open-code file_start_write here to grab freeze protection,
Christoph Hellwig54843f82018-05-02 19:57:21 +02001564 * 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 Kara70fe2f42016-10-30 11:42:04 -05001568 */
Christoph Hellwig92ce4722018-04-06 09:28:17 +02001569 if (S_ISREG(file_inode(file)->i_mode)) {
1570 __sb_start_write(file_inode(file)->i_sb, SB_FREEZE_WRITE, true);
Shaohua Lia12f1ae2016-12-13 12:09:56 -08001571 __sb_writers_release(file_inode(file)->i_sb, SB_FREEZE_WRITE);
Christoph Hellwig92ce4722018-04-06 09:28:17 +02001572 }
1573 req->ki_flags |= IOCB_WRITE;
Christoph Hellwig54843f82018-05-02 19:57:21 +02001574 ret = aio_rw_ret(req, call_write_iter(file, req, &iter));
Christoph Hellwig89319d312016-10-30 11:42:03 -05001575 }
1576 kfree(iovec);
Christoph Hellwig54843f82018-05-02 19:57:21 +02001577out_fput:
1578 if (unlikely(ret && ret != -EIOCBQUEUED))
1579 fput(file);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001580 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
Christoph Hellwiga3c0d432018-03-27 19:18:57 +02001583static 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
1593static 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 Bunkd5470b52008-04-29 00:58:57 -07001613static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
Kent Overstreeta1c8eae2013-05-07 16:18:53 -07001614 struct iocb *iocb, bool compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001616 struct aio_kiocb *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 ssize_t ret;
1618
1619 /* enforce forwards compatibility on users */
Goldwyn Rodrigues9830f4b2017-06-20 07:05:42 -05001620 if (unlikely(iocb->aio_reserved2)) {
Kent Overstreetcaf41672013-05-07 16:18:35 -07001621 pr_debug("EINVAL: reserve field set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 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 Meeacd88d42015-02-04 21:15:59 +08001631 pr_debug("EINVAL: overflow check\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return -EINVAL;
1633 }
1634
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001635 req = aio_get_req(ctx);
Kent Overstreet1d98ebf2013-05-07 16:18:37 -07001636 if (unlikely(!req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return -EAGAIN;
Kent Overstreet1d98ebf2013-05-07 16:18:37 -07001638
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001639 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 Libenzi13389012009-06-30 11:41:11 -07001646 req->ki_eventfd = eventfd_ctx_fdget((int) iocb->aio_resfd);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07001647 if (IS_ERR(req->ki_eventfd)) {
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001648 ret = PTR_ERR(req->ki_eventfd);
Davide Libenzi87c3a862009-03-18 17:04:19 -07001649 req->ki_eventfd = NULL;
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001650 goto out_put_req;
1651 }
Goldwyn Rodrigues9830f4b2017-06-20 07:05:42 -05001652 }
1653
Kent Overstreet8a660892013-05-07 16:19:10 -07001654 ret = put_user(KIOCB_KEY, &user_iocb->aio_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (unlikely(ret)) {
Kent Overstreetcaf41672013-05-07 16:18:35 -07001656 pr_debug("EFAULT: aio_key\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 goto out_put_req;
1658 }
1659
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001660 req->ki_user_iocb = user_iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 req->ki_user_data = iocb->aio_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Christoph Hellwig89319d312016-10-30 11:42:03 -05001663 switch (iocb->aio_lio_opcode) {
1664 case IOCB_CMD_PREAD:
Christoph Hellwig54843f82018-05-02 19:57:21 +02001665 ret = aio_read(&req->rw, iocb, false, compat);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001666 break;
1667 case IOCB_CMD_PWRITE:
Christoph Hellwig54843f82018-05-02 19:57:21 +02001668 ret = aio_write(&req->rw, iocb, false, compat);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001669 break;
1670 case IOCB_CMD_PREADV:
Christoph Hellwig54843f82018-05-02 19:57:21 +02001671 ret = aio_read(&req->rw, iocb, true, compat);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001672 break;
1673 case IOCB_CMD_PWRITEV:
Christoph Hellwig54843f82018-05-02 19:57:21 +02001674 ret = aio_write(&req->rw, iocb, true, compat);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001675 break;
Christoph Hellwiga3c0d432018-03-27 19:18:57 +02001676 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 Hellwig89319d312016-10-30 11:42:03 -05001682 default:
1683 pr_debug("invalid aio operation %d\n", iocb->aio_lio_opcode);
1684 ret = -EINVAL;
1685 break;
1686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Christoph Hellwig92ce4722018-04-06 09:28:17 +02001688 /*
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 Hellwig89319d312016-10-30 11:42:03 -05001694 if (ret && ret != -EIOCBQUEUED)
1695 goto out_put_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697out_put_req:
Kent Overstreete1bdd5f2013-04-26 10:58:39 +10001698 put_reqs_available(ctx, 1);
Kent Overstreete34ecee2013-10-10 19:31:47 -07001699 percpu_ref_put(&ctx->reqs);
Christoph Hellwig54843f82018-05-02 19:57:21 +02001700 if (req->ki_eventfd)
1701 eventfd_ctx_put(req->ki_eventfd);
1702 kmem_cache_free(kiocb_cachep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return ret;
1704}
1705
Al Viroc00d2c72016-12-20 07:04:57 -05001706static long do_io_submit(aio_context_t ctx_id, long nr,
1707 struct iocb __user *__user *iocbpp, bool compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
1709 struct kioctx *ctx;
1710 long ret = 0;
Jeff Moyer080d6762011-11-02 13:40:10 -07001711 int i = 0;
Shaohua Li9f5b9422010-07-01 07:55:01 +02001712 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 if (unlikely(nr < 0))
1715 return -EINVAL;
1716
Jeff Moyer75e1c702010-09-10 14:16:00 -07001717 if (unlikely(nr > LONG_MAX/sizeof(*iocbpp)))
1718 nr = LONG_MAX/sizeof(*iocbpp);
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 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 Overstreetcaf41672013-05-07 16:18:35 -07001725 pr_debug("EINVAL: invalid context id\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return -EINVAL;
1727 }
1728
Shaohua Li9f5b9422010-07-01 07:55:01 +02001729 blk_start_plug(&plug);
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 /*
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 Overstreeta1c8eae2013-05-07 16:18:53 -07001749 ret = io_submit_one(ctx, user_iocb, &tmp, compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (ret)
1751 break;
1752 }
Shaohua Li9f5b9422010-07-01 07:55:01 +02001753 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Kent Overstreet723be6e2013-05-28 15:14:48 -07001755 percpu_ref_put(&ctx->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return i ? i : ret;
1757}
1758
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001759/* 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 */
1771SYSCALL_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 Viroc00d2c72016-12-20 07:04:57 -05001777#ifdef CONFIG_COMPAT
1778static inline long
1779copy_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
1795COMPAT_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 Torvalds1da177e2005-04-16 15:20:36 -07001815/* lookup_kiocb
1816 * Finds a given iocb for cancellation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 */
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001818static struct aio_kiocb *
1819lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001821 struct aio_kiocb *kiocb;
Zach Brownd00689a2005-11-13 16:07:34 -08001822
1823 assert_spin_locked(&ctx->ctx_lock);
1824
Kent Overstreet8a660892013-05-07 16:19:10 -07001825 if (key != KIOCB_KEY)
1826 return NULL;
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /* TODO: use a hash or array, this sucks. */
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001829 list_for_each_entry(kiocb, &ctx->active_reqs, ki_list) {
1830 if (kiocb->ki_user_iocb == iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 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 Carstens002c8972009-01-14 14:14:18 +01001846SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
1847 struct io_event __user *, result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 struct kioctx *ctx;
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001850 struct aio_kiocb *kiocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 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 Overstreet906b9732013-05-07 16:18:31 -07001863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 kiocb = lookup_kiocb(ctx, iocb, key);
Kent Overstreet906b9732013-05-07 16:18:31 -07001865 if (kiocb)
Fabian Frederickd52a8f92014-04-22 07:26:58 +02001866 ret = kiocb_cancel(kiocb);
Kent Overstreet906b9732013-05-07 16:18:31 -07001867 else
1868 ret = -EINVAL;
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 spin_unlock_irq(&ctx->ctx_lock);
1871
Kent Overstreet906b9732013-05-07 16:18:31 -07001872 if (!ret) {
Kent Overstreetbec68faa2013-05-13 14:45:08 -07001873 /*
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 Overstreet906b9732013-05-07 16:18:31 -07001877 */
Kent Overstreetbec68faa2013-05-13 14:45:08 -07001878 ret = -EINPROGRESS;
Kent Overstreet906b9732013-05-07 16:18:31 -07001879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Kent Overstreet723be6e2013-05-28 15:14:48 -07001881 percpu_ref_put(&ctx->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 return ret;
1884}
1885
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07001886static 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 Torvalds1da177e2005-04-16 15:20:36 -07001905/* io_getevents:
1906 * Attempts to read at least min_nr events and up to nr events from
Satoru Takeuchi642b5122010-08-05 11:23:11 -07001907 * 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 Moyer69008072013-05-24 15:55:24 -07001915 * timeout is relative. Will fail with -ENOSYS if not implemented.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001917SYSCALL_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 Torvalds1da177e2005-04-16 15:20:36 -07001922{
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07001923 struct timespec64 ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07001925 if (timeout) {
1926 if (unlikely(get_timespec64(&ts, timeout)))
1927 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 }
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07001929
1930 return do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &ts : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
Al Viroc00d2c72016-12-20 07:04:57 -05001932
1933#ifdef CONFIG_COMPAT
1934COMPAT_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 Dinamanifa2e62a2017-08-04 21:12:32 -07001940 struct timespec64 t;
Al Viroc00d2c72016-12-20 07:04:57 -05001941
1942 if (timeout) {
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07001943 if (compat_get_timespec64(&t, timeout))
Al Viroc00d2c72016-12-20 07:04:57 -05001944 return -EFAULT;
1945
Al Viroc00d2c72016-12-20 07:04:57 -05001946 }
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07001947
1948 return do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &t : NULL);
Al Viroc00d2c72016-12-20 07:04:57 -05001949}
1950#endif