blob: aaaaf4d12c7394fe1bb9836516b7b7fc1eb69105 [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.
Christoph Hellwigbfe40372018-07-16 09:08:20 +02008 * Copyright 2018 Christoph Hellwig.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * See ../COPYING for licensing terms.
11 */
Kent Overstreetcaf41672013-05-07 16:18:35 -070012#define pr_fmt(fmt) "%s: " fmt, __func__
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/errno.h>
17#include <linux/time.h>
18#include <linux/aio_abi.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050019#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/syscalls.h>
Jens Axboeb9d128f2009-10-29 13:59:26 +010021#include <linux/backing-dev.h>
Christoph Hellwig9018ccc2018-07-24 11:36:37 +020022#include <linux/refcount.h>
Badari Pulavarty027445c2006-09-30 23:28:46 -070023#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Ingo Molnar174cd4b2017-02-02 19:15:33 +010025#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/fs.h>
27#include <linux/file.h>
28#include <linux/mm.h>
29#include <linux/mman.h>
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070030#include <linux/mmu_context.h>
Kent Overstreete1bdd5f2013-04-26 10:58:39 +100031#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/slab.h>
33#include <linux/timer.h>
34#include <linux/aio.h>
35#include <linux/highmem.h>
36#include <linux/workqueue.h>
37#include <linux/security.h>
Davide Libenzi9c3060b2007-05-10 22:23:21 -070038#include <linux/eventfd.h>
Jeff Moyercfb1e332009-10-02 18:57:36 -040039#include <linux/blkdev.h>
Jeff Moyer9d85cba72010-05-26 14:44:26 -070040#include <linux/compat.h>
Gu Zheng36bc08c2013-07-16 17:56:16 +080041#include <linux/migrate.h>
42#include <linux/ramfs.h>
Kent Overstreet723be6e2013-05-28 15:14:48 -070043#include <linux/percpu-refcount.h>
Benjamin LaHaise71ad7492013-09-17 10:18:25 -040044#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <asm/kmap_types.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080047#include <linux/uaccess.h>
Jeff Moyera538e3f2018-12-11 12:37:49 -050048#include <linux/nospec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Al Viro68d70d02013-06-19 15:26:04 +040050#include "internal.h"
51
Christoph Hellwigf3a27522018-03-30 11:19:25 +020052#define KIOCB_KEY 0
53
Kent Overstreet4e179bc2013-05-07 16:18:33 -070054#define AIO_RING_MAGIC 0xa10a10a1
55#define AIO_RING_COMPAT_FEATURES 1
56#define AIO_RING_INCOMPAT_FEATURES 0
57struct aio_ring {
58 unsigned id; /* kernel internal index number */
59 unsigned nr; /* number of io_events */
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -040060 unsigned head; /* Written to by userland or under ring_lock
61 * mutex by aio_read_events_ring(). */
Kent Overstreet4e179bc2013-05-07 16:18:33 -070062 unsigned tail;
63
64 unsigned magic;
65 unsigned compat_features;
66 unsigned incompat_features;
67 unsigned header_length; /* size of aio_ring */
68
69
70 struct io_event io_events[0];
71}; /* 128 bytes + ring size */
72
Jens Axboea79d40e2018-12-04 09:45:32 -070073/*
74 * Plugging is meant to work with larger batches of IOs. If we don't
75 * have more than the below, then don't bother setting up a plug.
76 */
77#define AIO_PLUG_THRESHOLD 2
78
Kent Overstreet4e179bc2013-05-07 16:18:33 -070079#define AIO_RING_PAGES 8
Kent Overstreet4e179bc2013-05-07 16:18:33 -070080
Benjamin LaHaisedb446a02013-07-30 12:54:40 -040081struct kioctx_table {
Tejun Heod0264c02018-03-14 12:10:17 -070082 struct rcu_head rcu;
83 unsigned nr;
84 struct kioctx __rcu *table[];
Benjamin LaHaisedb446a02013-07-30 12:54:40 -040085};
86
Kent Overstreete1bdd5f2013-04-26 10:58:39 +100087struct kioctx_cpu {
88 unsigned reqs_available;
89};
90
Jens Axboedc48e562015-04-15 11:17:23 -060091struct ctx_rq_wait {
92 struct completion comp;
93 atomic_t count;
94};
95
Kent Overstreet4e179bc2013-05-07 16:18:33 -070096struct kioctx {
Kent Overstreet723be6e2013-05-28 15:14:48 -070097 struct percpu_ref users;
Kent Overstreet36f55882013-05-07 16:18:41 -070098 atomic_t dead;
Kent Overstreet4e179bc2013-05-07 16:18:33 -070099
Kent Overstreete34ecee2013-10-10 19:31:47 -0700100 struct percpu_ref reqs;
101
Kent Overstreet4e179bc2013-05-07 16:18:33 -0700102 unsigned long user_id;
Kent Overstreet4e179bc2013-05-07 16:18:33 -0700103
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000104 struct __percpu kioctx_cpu *cpu;
105
106 /*
107 * For percpu reqs_available, number of slots we move to/from global
108 * counter at a time:
109 */
110 unsigned req_batch;
Kent Overstreet3e845ce2013-05-07 16:18:51 -0700111 /*
112 * This is what userspace passed to io_setup(), it's not used for
113 * anything but counting against the global max_reqs quota.
114 *
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700115 * The real limit is nr_events - 1, which will be larger (see
Kent Overstreet3e845ce2013-05-07 16:18:51 -0700116 * aio_setup_ring())
117 */
Kent Overstreet4e179bc2013-05-07 16:18:33 -0700118 unsigned max_reqs;
119
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700120 /* Size of ringbuffer, in units of struct io_event */
121 unsigned nr_events;
Kent Overstreet4e179bc2013-05-07 16:18:33 -0700122
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700123 unsigned long mmap_base;
124 unsigned long mmap_size;
125
126 struct page **ring_pages;
127 long nr_pages;
128
Tejun Heof7298632018-03-14 12:45:15 -0700129 struct rcu_work free_rwork; /* see free_ioctx() */
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700130
Anatol Pomozove02ba722014-04-15 11:31:33 -0700131 /*
132 * signals when all in-flight requests are done
133 */
Jens Axboedc48e562015-04-15 11:17:23 -0600134 struct ctx_rq_wait *rq_wait;
Anatol Pomozove02ba722014-04-15 11:31:33 -0700135
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700136 struct {
Kent Overstreet34e83fc2013-04-26 10:58:39 +1000137 /*
138 * This counts the number of available slots in the ringbuffer,
139 * so we avoid overflowing it: it's decremented (if positive)
140 * when allocating a kiocb and incremented when the resulting
141 * io_event is pulled off the ringbuffer.
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000142 *
143 * We batch accesses to it with a percpu version.
Kent Overstreet34e83fc2013-04-26 10:58:39 +1000144 */
145 atomic_t reqs_available;
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700146 } ____cacheline_aligned_in_smp;
147
148 struct {
149 spinlock_t ctx_lock;
150 struct list_head active_reqs; /* used for cancellation */
151 } ____cacheline_aligned_in_smp;
152
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700153 struct {
154 struct mutex ring_lock;
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700155 wait_queue_head_t wait;
156 } ____cacheline_aligned_in_smp;
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700157
158 struct {
159 unsigned tail;
Benjamin LaHaised856f322014-08-24 13:14:05 -0400160 unsigned completed_events;
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700161 spinlock_t completion_lock;
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700162 } ____cacheline_aligned_in_smp;
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700163
164 struct page *internal_pages[AIO_RING_PAGES];
Gu Zheng36bc08c2013-07-16 17:56:16 +0800165 struct file *aio_ring_file;
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400166
167 unsigned id;
Kent Overstreet4e179bc2013-05-07 16:18:33 -0700168};
169
Christoph Hellwiga3c0d432018-03-27 19:18:57 +0200170struct fsync_iocb {
171 struct work_struct work;
172 struct file *file;
173 bool datasync;
174};
175
Christoph Hellwigbfe40372018-07-16 09:08:20 +0200176struct poll_iocb {
177 struct file *file;
178 struct wait_queue_head *head;
179 __poll_t events;
180 bool woken;
181 bool cancelled;
182 struct wait_queue_entry wait;
183 struct work_struct work;
184};
185
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100186struct aio_kiocb {
Christoph Hellwig54843f82018-05-02 19:57:21 +0200187 union {
188 struct kiocb rw;
Christoph Hellwiga3c0d432018-03-27 19:18:57 +0200189 struct fsync_iocb fsync;
Christoph Hellwigbfe40372018-07-16 09:08:20 +0200190 struct poll_iocb poll;
Christoph Hellwig54843f82018-05-02 19:57:21 +0200191 };
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100192
193 struct kioctx *ki_ctx;
194 kiocb_cancel_fn *ki_cancel;
195
196 struct iocb __user *ki_user_iocb; /* user's aiocb */
197 __u64 ki_user_data; /* user's data for completion */
198
199 struct list_head ki_list; /* the aio core uses this
200 * for cancellation */
Christoph Hellwig9018ccc2018-07-24 11:36:37 +0200201 refcount_t ki_refcnt;
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100202
203 /*
204 * If the aio_resfd field of the userspace iocb is not zero,
205 * this is the underlying eventfd context to deliver events to.
206 */
207 struct eventfd_ctx *ki_eventfd;
208};
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*------ sysctl variables----*/
Zach Brownd55b5fd2005-11-07 00:59:31 -0800211static DEFINE_SPINLOCK(aio_nr_lock);
212unsigned long aio_nr; /* current system wide number of aio requests */
213unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/*----end sysctl variables---*/
215
Christoph Lametere18b8902006-12-06 20:33:20 -0800216static struct kmem_cache *kiocb_cachep;
217static struct kmem_cache *kioctx_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400219static struct vfsmount *aio_mnt;
220
221static const struct file_operations aio_ring_fops;
222static const struct address_space_operations aio_ctx_aops;
223
224static struct file *aio_private_file(struct kioctx *ctx, loff_t nr_pages)
225{
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400226 struct file *file;
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400227 struct inode *inode = alloc_anon_inode(aio_mnt->mnt_sb);
Dan Carpenter7f626562013-11-13 10:49:40 +0300228 if (IS_ERR(inode))
229 return ERR_CAST(inode);
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400230
231 inode->i_mapping->a_ops = &aio_ctx_aops;
232 inode->i_mapping->private_data = ctx;
233 inode->i_size = PAGE_SIZE * nr_pages;
234
Al Virod93aa9d2018-06-09 09:40:05 -0400235 file = alloc_file_pseudo(inode, aio_mnt, "[aio]",
236 O_RDWR, &aio_ring_fops);
Al Viroc9c554f2018-07-11 14:19:04 -0400237 if (IS_ERR(file))
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400238 iput(inode);
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400239 return file;
240}
241
242static struct dentry *aio_mount(struct file_system_type *fs_type,
243 int flags, const char *dev_name, void *data)
244{
Al Virod93aa9d2018-06-09 09:40:05 -0400245 struct dentry *root = mount_pseudo(fs_type, "aio:", NULL, NULL,
Jann Horn22f6b4d2016-09-16 00:31:22 +0200246 AIO_RING_MAGIC);
247
248 if (!IS_ERR(root))
249 root->d_sb->s_iflags |= SB_I_NOEXEC;
250 return root;
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400251}
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253/* aio_setup
254 * Creates the slab caches used by the aio routines, panic on
255 * failure as this is done early during the boot sequence.
256 */
257static int __init aio_setup(void)
258{
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400259 static struct file_system_type aio_fs = {
260 .name = "aio",
261 .mount = aio_mount,
262 .kill_sb = kill_anon_super,
263 };
264 aio_mnt = kern_mount(&aio_fs);
265 if (IS_ERR(aio_mnt))
266 panic("Failed to create aio fs mount.");
267
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100268 kiocb_cachep = KMEM_CACHE(aio_kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
Christoph Lameter0a31bd52007-05-06 14:49:57 -0700269 kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return 0;
271}
H Hartley Sweeten385773e2009-09-22 16:43:53 -0700272__initcall(aio_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400274static void put_aio_ring_file(struct kioctx *ctx)
275{
276 struct file *aio_ring_file = ctx->aio_ring_file;
Rasmus Villemoesde04e762016-09-15 00:25:03 +0200277 struct address_space *i_mapping;
278
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400279 if (aio_ring_file) {
Al Viro45063092016-12-04 18:24:56 -0500280 truncate_setsize(file_inode(aio_ring_file), 0);
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400281
282 /* Prevent further access to the kioctx from migratepages */
Al Viro45063092016-12-04 18:24:56 -0500283 i_mapping = aio_ring_file->f_mapping;
Rasmus Villemoesde04e762016-09-15 00:25:03 +0200284 spin_lock(&i_mapping->private_lock);
285 i_mapping->private_data = NULL;
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400286 ctx->aio_ring_file = NULL;
Rasmus Villemoesde04e762016-09-15 00:25:03 +0200287 spin_unlock(&i_mapping->private_lock);
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400288
289 fput(aio_ring_file);
290 }
291}
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293static void aio_free_ring(struct kioctx *ctx)
294{
Gu Zheng36bc08c2013-07-16 17:56:16 +0800295 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400297 /* Disconnect the kiotx from the ring file. This prevents future
298 * accesses to the kioctx from page migration.
299 */
300 put_aio_ring_file(ctx);
301
Gu Zheng36bc08c2013-07-16 17:56:16 +0800302 for (i = 0; i < ctx->nr_pages; i++) {
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500303 struct page *page;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800304 pr_debug("pid(%d) [%d] page->count=%d\n", current->pid, i,
305 page_count(ctx->ring_pages[i]));
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500306 page = ctx->ring_pages[i];
307 if (!page)
308 continue;
309 ctx->ring_pages[i] = NULL;
310 put_page(page);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Sasha Levinddb8c452013-11-19 17:33:03 -0500313 if (ctx->ring_pages && ctx->ring_pages != ctx->internal_pages) {
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700314 kfree(ctx->ring_pages);
Sasha Levinddb8c452013-11-19 17:33:03 -0500315 ctx->ring_pages = NULL;
316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Oleg Nesterov5477e702015-09-04 15:48:04 -0700319static int aio_ring_mremap(struct vm_area_struct *vma)
Gu Zheng36bc08c2013-07-16 17:56:16 +0800320{
Oleg Nesterov5477e702015-09-04 15:48:04 -0700321 struct file *file = vma->vm_file;
Pavel Emelyanove4a0d3e2014-09-18 19:56:17 +0400322 struct mm_struct *mm = vma->vm_mm;
323 struct kioctx_table *table;
Al Virob2edffd2015-04-06 17:48:54 -0400324 int i, res = -EINVAL;
Pavel Emelyanove4a0d3e2014-09-18 19:56:17 +0400325
326 spin_lock(&mm->ioctx_lock);
327 rcu_read_lock();
328 table = rcu_dereference(mm->ioctx_table);
329 for (i = 0; i < table->nr; i++) {
330 struct kioctx *ctx;
331
Tejun Heod0264c02018-03-14 12:10:17 -0700332 ctx = rcu_dereference(table->table[i]);
Pavel Emelyanove4a0d3e2014-09-18 19:56:17 +0400333 if (ctx && ctx->aio_ring_file == file) {
Al Virob2edffd2015-04-06 17:48:54 -0400334 if (!atomic_read(&ctx->dead)) {
335 ctx->user_id = ctx->mmap_base = vma->vm_start;
336 res = 0;
337 }
Pavel Emelyanove4a0d3e2014-09-18 19:56:17 +0400338 break;
339 }
340 }
341
342 rcu_read_unlock();
343 spin_unlock(&mm->ioctx_lock);
Al Virob2edffd2015-04-06 17:48:54 -0400344 return res;
Pavel Emelyanove4a0d3e2014-09-18 19:56:17 +0400345}
346
Oleg Nesterov5477e702015-09-04 15:48:04 -0700347static const struct vm_operations_struct aio_ring_vm_ops = {
348 .mremap = aio_ring_mremap,
349#if IS_ENABLED(CONFIG_MMU)
350 .fault = filemap_fault,
351 .map_pages = filemap_map_pages,
352 .page_mkwrite = filemap_page_mkwrite,
353#endif
354};
355
356static int aio_ring_mmap(struct file *file, struct vm_area_struct *vma)
357{
358 vma->vm_flags |= VM_DONTEXPAND;
359 vma->vm_ops = &aio_ring_vm_ops;
360 return 0;
361}
362
Gu Zheng36bc08c2013-07-16 17:56:16 +0800363static const struct file_operations aio_ring_fops = {
364 .mmap = aio_ring_mmap,
365};
366
Benjamin LaHaise0c453552013-07-17 09:34:24 -0400367#if IS_ENABLED(CONFIG_MIGRATION)
Gu Zheng36bc08c2013-07-16 17:56:16 +0800368static int aio_migratepage(struct address_space *mapping, struct page *new,
369 struct page *old, enum migrate_mode mode)
370{
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400371 struct kioctx *ctx;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800372 unsigned long flags;
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400373 pgoff_t idx;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800374 int rc;
375
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700376 /*
377 * We cannot support the _NO_COPY case here, because copy needs to
378 * happen under the ctx->completion_lock. That does not work with the
379 * migration workflow of MIGRATE_SYNC_NO_COPY.
380 */
381 if (mode == MIGRATE_SYNC_NO_COPY)
382 return -EINVAL;
383
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500384 rc = 0;
385
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400386 /* mapping->private_lock here protects against the kioctx teardown. */
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500387 spin_lock(&mapping->private_lock);
388 ctx = mapping->private_data;
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400389 if (!ctx) {
390 rc = -EINVAL;
391 goto out;
392 }
393
394 /* The ring_lock mutex. The prevents aio_read_events() from writing
395 * to the ring's head, and prevents page migration from mucking in
396 * a partially initialized kiotx.
397 */
398 if (!mutex_trylock(&ctx->ring_lock)) {
399 rc = -EAGAIN;
400 goto out;
401 }
402
403 idx = old->index;
404 if (idx < (pgoff_t)ctx->nr_pages) {
405 /* Make sure the old page hasn't already been changed */
406 if (ctx->ring_pages[idx] != old)
407 rc = -EAGAIN;
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500408 } else
409 rc = -EINVAL;
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500410
411 if (rc != 0)
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400412 goto out_unlock;
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500413
Gu Zheng36bc08c2013-07-16 17:56:16 +0800414 /* Writeback must be complete */
415 BUG_ON(PageWriteback(old));
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500416 get_page(new);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800417
Jan Karaab41ee62018-12-28 00:39:20 -0800418 rc = migrate_page_move_mapping(mapping, new, old, mode, 1);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800419 if (rc != MIGRATEPAGE_SUCCESS) {
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500420 put_page(new);
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400421 goto out_unlock;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800422 }
423
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400424 /* Take completion_lock to prevent other writes to the ring buffer
425 * while the old page is copied to the new. This prevents new
426 * events from being lost.
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400427 */
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400428 spin_lock_irqsave(&ctx->completion_lock, flags);
429 migrate_page_copy(new, old);
430 BUG_ON(ctx->ring_pages[idx] != old);
431 ctx->ring_pages[idx] = new;
432 spin_unlock_irqrestore(&ctx->completion_lock, flags);
433
434 /* The old page is no longer accessible. */
435 put_page(old);
436
437out_unlock:
438 mutex_unlock(&ctx->ring_lock);
439out:
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400440 spin_unlock(&mapping->private_lock);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800441 return rc;
442}
Benjamin LaHaise0c453552013-07-17 09:34:24 -0400443#endif
Gu Zheng36bc08c2013-07-16 17:56:16 +0800444
445static const struct address_space_operations aio_ctx_aops = {
Gu Zheng835f2522014-11-06 17:46:21 +0800446 .set_page_dirty = __set_page_dirty_no_writeback,
Benjamin LaHaise0c453552013-07-17 09:34:24 -0400447#if IS_ENABLED(CONFIG_MIGRATION)
Gu Zheng36bc08c2013-07-16 17:56:16 +0800448 .migratepage = aio_migratepage,
Benjamin LaHaise0c453552013-07-17 09:34:24 -0400449#endif
Gu Zheng36bc08c2013-07-16 17:56:16 +0800450};
451
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300452static int aio_setup_ring(struct kioctx *ctx, unsigned int nr_events)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 struct aio_ring *ring;
Zach Brown41003a72013-05-07 16:18:25 -0700455 struct mm_struct *mm = current->mm;
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900456 unsigned long size, unused;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 int nr_pages;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800458 int i;
459 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 /* Compensate for the ring buffer's head/tail overlap entry */
462 nr_events += 2; /* 1 is required, 2 for good luck */
463
464 size = sizeof(struct aio_ring);
465 size += sizeof(struct io_event) * nr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Gu Zheng36bc08c2013-07-16 17:56:16 +0800467 nr_pages = PFN_UP(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (nr_pages < 0)
469 return -EINVAL;
470
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400471 file = aio_private_file(ctx, nr_pages);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800472 if (IS_ERR(file)) {
473 ctx->aio_ring_file = NULL;
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400474 return -ENOMEM;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Gu Zheng36bc08c2013-07-16 17:56:16 +0800477 ctx->aio_ring_file = file;
478 nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring))
479 / sizeof(struct io_event);
480
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700481 ctx->ring_pages = ctx->internal_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (nr_pages > AIO_RING_PAGES) {
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700483 ctx->ring_pages = kcalloc(nr_pages, sizeof(struct page *),
484 GFP_KERNEL);
Gu Zhengd1b94322013-12-04 18:19:06 +0800485 if (!ctx->ring_pages) {
486 put_aio_ring_file(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return -ENOMEM;
Gu Zhengd1b94322013-12-04 18:19:06 +0800488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900491 for (i = 0; i < nr_pages; i++) {
492 struct page *page;
Al Viro45063092016-12-04 18:24:56 -0500493 page = find_or_create_page(file->f_mapping,
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900494 i, GFP_HIGHUSER | __GFP_ZERO);
495 if (!page)
496 break;
497 pr_debug("pid(%d) page[%d]->count=%d\n",
498 current->pid, i, page_count(page));
499 SetPageUptodate(page);
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900500 unlock_page(page);
501
502 ctx->ring_pages[i] = page;
503 }
504 ctx->nr_pages = i;
505
506 if (unlikely(i != nr_pages)) {
507 aio_free_ring(ctx);
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400508 return -ENOMEM;
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900509 }
510
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700511 ctx->mmap_size = nr_pages * PAGE_SIZE;
512 pr_debug("attempting mmap of %lu bytes\n", ctx->mmap_size);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800513
Michal Hocko013373e2016-05-23 16:25:59 -0700514 if (down_write_killable(&mm->mmap_sem)) {
515 ctx->mmap_size = 0;
516 aio_free_ring(ctx);
517 return -EINTR;
518 }
519
Gu Zheng36bc08c2013-07-16 17:56:16 +0800520 ctx->mmap_base = do_mmap_pgoff(ctx->aio_ring_file, 0, ctx->mmap_size,
521 PROT_READ | PROT_WRITE,
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800522 MAP_SHARED, 0, &unused, NULL);
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900523 up_write(&mm->mmap_sem);
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700524 if (IS_ERR((void *)ctx->mmap_base)) {
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700525 ctx->mmap_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 aio_free_ring(ctx);
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400527 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700530 pr_debug("mmap address: 0x%08lx\n", ctx->mmap_base);
Benjamin LaHaised6c355c2013-09-09 11:57:59 -0400531
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700532 ctx->user_id = ctx->mmap_base;
533 ctx->nr_events = nr_events; /* trusted copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700535 ring = kmap_atomic(ctx->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 ring->nr = nr_events; /* user copy */
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400537 ring->id = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 ring->head = ring->tail = 0;
539 ring->magic = AIO_RING_MAGIC;
540 ring->compat_features = AIO_RING_COMPAT_FEATURES;
541 ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
542 ring->header_length = sizeof(struct aio_ring);
Cong Wange8e3c3d2011-11-25 23:14:27 +0800543 kunmap_atomic(ring);
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700544 flush_dcache_page(ctx->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 return 0;
547}
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549#define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
550#define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
551#define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
552
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100553void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel)
Kent Overstreet0460fef2013-05-07 16:18:49 -0700554{
Christoph Hellwig54843f82018-05-02 19:57:21 +0200555 struct aio_kiocb *req = container_of(iocb, struct aio_kiocb, rw);
Kent Overstreet0460fef2013-05-07 16:18:49 -0700556 struct kioctx *ctx = req->ki_ctx;
557 unsigned long flags;
558
Christoph Hellwig75321b52018-04-09 14:57:56 +0200559 if (WARN_ON_ONCE(!list_empty(&req->ki_list)))
560 return;
561
Kent Overstreet0460fef2013-05-07 16:18:49 -0700562 spin_lock_irqsave(&ctx->ctx_lock, flags);
Christoph Hellwig75321b52018-04-09 14:57:56 +0200563 list_add_tail(&req->ki_list, &ctx->active_reqs);
Kent Overstreet0460fef2013-05-07 16:18:49 -0700564 req->ki_cancel = cancel;
Kent Overstreet0460fef2013-05-07 16:18:49 -0700565 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
566}
567EXPORT_SYMBOL(kiocb_set_cancel_fn);
568
Tejun Heoa6d7cff2018-03-14 12:10:17 -0700569/*
570 * free_ioctx() should be RCU delayed to synchronize against the RCU
571 * protected lookup_ioctx() and also needs process context to call
Tejun Heof7298632018-03-14 12:45:15 -0700572 * aio_free_ring(). Use rcu_work.
Tejun Heoa6d7cff2018-03-14 12:10:17 -0700573 */
Kent Overstreete34ecee2013-10-10 19:31:47 -0700574static void free_ioctx(struct work_struct *work)
Kent Overstreet36f55882013-05-07 16:18:41 -0700575{
Tejun Heof7298632018-03-14 12:45:15 -0700576 struct kioctx *ctx = container_of(to_rcu_work(work), struct kioctx,
577 free_rwork);
Kent Overstreete34ecee2013-10-10 19:31:47 -0700578 pr_debug("freeing %p\n", ctx);
579
580 aio_free_ring(ctx);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000581 free_percpu(ctx->cpu);
Tejun Heo9a1049d2014-06-28 08:10:14 -0400582 percpu_ref_exit(&ctx->reqs);
583 percpu_ref_exit(&ctx->users);
Kent Overstreet36f55882013-05-07 16:18:41 -0700584 kmem_cache_free(kioctx_cachep, ctx);
585}
586
Kent Overstreete34ecee2013-10-10 19:31:47 -0700587static void free_ioctx_reqs(struct percpu_ref *ref)
588{
589 struct kioctx *ctx = container_of(ref, struct kioctx, reqs);
590
Anatol Pomozove02ba722014-04-15 11:31:33 -0700591 /* At this point we know that there are no any in-flight requests */
Jens Axboedc48e562015-04-15 11:17:23 -0600592 if (ctx->rq_wait && atomic_dec_and_test(&ctx->rq_wait->count))
593 complete(&ctx->rq_wait->comp);
Anatol Pomozove02ba722014-04-15 11:31:33 -0700594
Tejun Heoa6d7cff2018-03-14 12:10:17 -0700595 /* Synchronize against RCU protected table->table[] dereferences */
Tejun Heof7298632018-03-14 12:45:15 -0700596 INIT_RCU_WORK(&ctx->free_rwork, free_ioctx);
597 queue_rcu_work(system_wq, &ctx->free_rwork);
Kent Overstreete34ecee2013-10-10 19:31:47 -0700598}
599
Kent Overstreet36f55882013-05-07 16:18:41 -0700600/*
601 * When this function runs, the kioctx has been removed from the "hash table"
602 * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
603 * now it's safe to cancel any that need to be.
604 */
Kent Overstreete34ecee2013-10-10 19:31:47 -0700605static void free_ioctx_users(struct percpu_ref *ref)
Kent Overstreet36f55882013-05-07 16:18:41 -0700606{
Kent Overstreete34ecee2013-10-10 19:31:47 -0700607 struct kioctx *ctx = container_of(ref, struct kioctx, users);
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100608 struct aio_kiocb *req;
Kent Overstreet36f55882013-05-07 16:18:41 -0700609
610 spin_lock_irq(&ctx->ctx_lock);
611
612 while (!list_empty(&ctx->active_reqs)) {
613 req = list_first_entry(&ctx->active_reqs,
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100614 struct aio_kiocb, ki_list);
Christoph Hellwig888933f2018-05-23 14:11:02 +0200615 req->ki_cancel(&req->rw);
Al Viro4faa99962018-05-23 22:53:22 -0400616 list_del_init(&req->ki_list);
Kent Overstreet36f55882013-05-07 16:18:41 -0700617 }
618
619 spin_unlock_irq(&ctx->ctx_lock);
620
Kent Overstreete34ecee2013-10-10 19:31:47 -0700621 percpu_ref_kill(&ctx->reqs);
622 percpu_ref_put(&ctx->reqs);
Kent Overstreet36f55882013-05-07 16:18:41 -0700623}
624
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400625static int ioctx_add_table(struct kioctx *ctx, struct mm_struct *mm)
626{
627 unsigned i, new_nr;
628 struct kioctx_table *table, *old;
629 struct aio_ring *ring;
630
631 spin_lock(&mm->ioctx_lock);
Oleg Nesterov855ef0d2014-04-30 16:16:36 +0200632 table = rcu_dereference_raw(mm->ioctx_table);
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400633
634 while (1) {
635 if (table)
636 for (i = 0; i < table->nr; i++)
Tejun Heod0264c02018-03-14 12:10:17 -0700637 if (!rcu_access_pointer(table->table[i])) {
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400638 ctx->id = i;
Tejun Heod0264c02018-03-14 12:10:17 -0700639 rcu_assign_pointer(table->table[i], ctx);
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400640 spin_unlock(&mm->ioctx_lock);
641
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400642 /* While kioctx setup is in progress,
643 * we are protected from page migration
644 * changes ring_pages by ->ring_lock.
645 */
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400646 ring = kmap_atomic(ctx->ring_pages[0]);
647 ring->id = ctx->id;
648 kunmap_atomic(ring);
649 return 0;
650 }
651
652 new_nr = (table ? table->nr : 1) * 4;
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400653 spin_unlock(&mm->ioctx_lock);
654
655 table = kzalloc(sizeof(*table) + sizeof(struct kioctx *) *
656 new_nr, GFP_KERNEL);
657 if (!table)
658 return -ENOMEM;
659
660 table->nr = new_nr;
661
662 spin_lock(&mm->ioctx_lock);
Oleg Nesterov855ef0d2014-04-30 16:16:36 +0200663 old = rcu_dereference_raw(mm->ioctx_table);
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400664
665 if (!old) {
666 rcu_assign_pointer(mm->ioctx_table, table);
667 } else if (table->nr > old->nr) {
668 memcpy(table->table, old->table,
669 old->nr * sizeof(struct kioctx *));
670
671 rcu_assign_pointer(mm->ioctx_table, table);
672 kfree_rcu(old, rcu);
673 } else {
674 kfree(table);
675 table = old;
676 }
677 }
678}
679
Kent Overstreete34ecee2013-10-10 19:31:47 -0700680static void aio_nr_sub(unsigned nr)
681{
682 spin_lock(&aio_nr_lock);
683 if (WARN_ON(aio_nr - nr > aio_nr))
684 aio_nr = 0;
685 else
686 aio_nr -= nr;
687 spin_unlock(&aio_nr_lock);
688}
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690/* ioctx_alloc
691 * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
692 */
693static struct kioctx *ioctx_alloc(unsigned nr_events)
694{
Zach Brown41003a72013-05-07 16:18:25 -0700695 struct mm_struct *mm = current->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 struct kioctx *ctx;
Al Viroe23754f2012-03-06 14:33:22 -0500697 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000699 /*
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300700 * Store the original nr_events -- what userspace passed to io_setup(),
701 * for counting against the global limit -- before it changes.
702 */
703 unsigned int max_reqs = nr_events;
704
705 /*
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000706 * We keep track of the number of available ringbuffer slots, to prevent
707 * overflow (reqs_available), and we also use percpu counters for this.
708 *
709 * So since up to half the slots might be on other cpu's percpu counters
710 * and unavailable, double nr_events so userspace sees what they
711 * expected: additionally, we move req_batch slots to/from percpu
712 * counters at a time, so make sure that isn't 0:
713 */
714 nr_events = max(nr_events, num_possible_cpus() * 4);
715 nr_events *= 2;
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /* Prevent overflows */
Al Viro08397ac2015-03-31 11:43:52 -0400718 if (nr_events > (0x10000000U / sizeof(struct io_event))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 pr_debug("ENOMEM: nr_events too high\n");
720 return ERR_PTR(-EINVAL);
721 }
722
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300723 if (!nr_events || (unsigned long)max_reqs > aio_max_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return ERR_PTR(-EAGAIN);
725
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800726 ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (!ctx)
728 return ERR_PTR(-ENOMEM);
729
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300730 ctx->max_reqs = max_reqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400732 spin_lock_init(&ctx->ctx_lock);
733 spin_lock_init(&ctx->completion_lock);
734 mutex_init(&ctx->ring_lock);
735 /* Protect against page migration throughout kiotx setup by keeping
736 * the ring_lock mutex held until setup is complete. */
737 mutex_lock(&ctx->ring_lock);
738 init_waitqueue_head(&ctx->wait);
739
740 INIT_LIST_HEAD(&ctx->active_reqs);
741
Tejun Heo2aad2a82014-09-24 13:31:50 -0400742 if (percpu_ref_init(&ctx->users, free_ioctx_users, 0, GFP_KERNEL))
Kent Overstreete34ecee2013-10-10 19:31:47 -0700743 goto err;
744
Tejun Heo2aad2a82014-09-24 13:31:50 -0400745 if (percpu_ref_init(&ctx->reqs, free_ioctx_reqs, 0, GFP_KERNEL))
Kent Overstreete34ecee2013-10-10 19:31:47 -0700746 goto err;
Kent Overstreet723be6e2013-05-28 15:14:48 -0700747
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000748 ctx->cpu = alloc_percpu(struct kioctx_cpu);
749 if (!ctx->cpu)
Kent Overstreete34ecee2013-10-10 19:31:47 -0700750 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300752 err = aio_setup_ring(ctx, nr_events);
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400753 if (err < 0)
Kent Overstreete34ecee2013-10-10 19:31:47 -0700754 goto err;
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000755
Kent Overstreet34e83fc2013-04-26 10:58:39 +1000756 atomic_set(&ctx->reqs_available, ctx->nr_events - 1);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000757 ctx->req_batch = (ctx->nr_events - 1) / (num_possible_cpus() * 4);
Benjamin LaHaise6878ea72013-07-31 10:34:18 -0400758 if (ctx->req_batch < 1)
759 ctx->req_batch = 1;
Kent Overstreet34e83fc2013-04-26 10:58:39 +1000760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /* limit the number of system wide aios */
Al Viro9fa1cb32012-03-10 23:14:05 -0500762 spin_lock(&aio_nr_lock);
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300763 if (aio_nr + ctx->max_reqs > aio_max_nr ||
764 aio_nr + ctx->max_reqs < aio_nr) {
Al Viro9fa1cb32012-03-10 23:14:05 -0500765 spin_unlock(&aio_nr_lock);
Kent Overstreete34ecee2013-10-10 19:31:47 -0700766 err = -EAGAIN;
Gu Zhengd1b94322013-12-04 18:19:06 +0800767 goto err_ctx;
Al Viro2dd542b2012-03-10 23:10:35 -0500768 }
769 aio_nr += ctx->max_reqs;
Al Viro9fa1cb32012-03-10 23:14:05 -0500770 spin_unlock(&aio_nr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Benjamin LaHaise18816862013-12-21 15:49:28 -0500772 percpu_ref_get(&ctx->users); /* io_setup() will drop this ref */
773 percpu_ref_get(&ctx->reqs); /* free_ioctx_users() will drop this */
Kent Overstreet723be6e2013-05-28 15:14:48 -0700774
Benjamin LaHaiseda903822013-08-05 13:21:43 -0400775 err = ioctx_add_table(ctx, mm);
776 if (err)
Kent Overstreete34ecee2013-10-10 19:31:47 -0700777 goto err_cleanup;
Benjamin LaHaiseda903822013-08-05 13:21:43 -0400778
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400779 /* Release the ring_lock mutex now that all setup is complete. */
780 mutex_unlock(&ctx->ring_lock);
781
Kent Overstreetcaf41672013-05-07 16:18:35 -0700782 pr_debug("allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700783 ctx, ctx->user_id, mm, ctx->nr_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return ctx;
785
Kent Overstreete34ecee2013-10-10 19:31:47 -0700786err_cleanup:
787 aio_nr_sub(ctx->max_reqs);
Gu Zhengd1b94322013-12-04 18:19:06 +0800788err_ctx:
Al Virodeeb8522015-04-06 17:57:44 -0400789 atomic_set(&ctx->dead, 1);
790 if (ctx->mmap_size)
791 vm_munmap(ctx->mmap_base, ctx->mmap_size);
Gu Zhengd1b94322013-12-04 18:19:06 +0800792 aio_free_ring(ctx);
Kent Overstreete34ecee2013-10-10 19:31:47 -0700793err:
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400794 mutex_unlock(&ctx->ring_lock);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000795 free_percpu(ctx->cpu);
Tejun Heo9a1049d2014-06-28 08:10:14 -0400796 percpu_ref_exit(&ctx->reqs);
797 percpu_ref_exit(&ctx->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 kmem_cache_free(kioctx_cachep, ctx);
Kent Overstreetcaf41672013-05-07 16:18:35 -0700799 pr_debug("error allocating ioctx %d\n", err);
Al Viroe23754f2012-03-06 14:33:22 -0500800 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
Kent Overstreet36f55882013-05-07 16:18:41 -0700803/* kill_ioctx
804 * Cancels all outstanding aio requests on an aio context. Used
805 * when the processes owning a context have all exited to encourage
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 * the rapid destruction of the kioctx.
807 */
Benjamin LaHaisefb2d4482014-04-29 12:45:17 -0400808static int kill_ioctx(struct mm_struct *mm, struct kioctx *ctx,
Jens Axboedc48e562015-04-15 11:17:23 -0600809 struct ctx_rq_wait *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400811 struct kioctx_table *table;
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400812
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400813 spin_lock(&mm->ioctx_lock);
Al Virob2edffd2015-04-06 17:48:54 -0400814 if (atomic_xchg(&ctx->dead, 1)) {
815 spin_unlock(&mm->ioctx_lock);
816 return -EINVAL;
817 }
818
Oleg Nesterov855ef0d2014-04-30 16:16:36 +0200819 table = rcu_dereference_raw(mm->ioctx_table);
Tejun Heod0264c02018-03-14 12:10:17 -0700820 WARN_ON(ctx != rcu_access_pointer(table->table[ctx->id]));
821 RCU_INIT_POINTER(table->table[ctx->id], NULL);
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400822 spin_unlock(&mm->ioctx_lock);
Kent Overstreet4fcc7122013-06-12 14:04:59 -0700823
Tejun Heoa6d7cff2018-03-14 12:10:17 -0700824 /* free_ioctx_reqs() will do the necessary RCU synchronization */
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400825 wake_up_all(&ctx->wait);
Kent Overstreet4fcc7122013-06-12 14:04:59 -0700826
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400827 /*
828 * It'd be more correct to do this in free_ioctx(), after all
829 * the outstanding kiocbs have finished - but by then io_destroy
830 * has already returned, so io_setup() could potentially return
831 * -EAGAIN with no ioctxs actually in use (as far as userspace
832 * could tell).
833 */
834 aio_nr_sub(ctx->max_reqs);
Benjamin LaHaisefb2d4482014-04-29 12:45:17 -0400835
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400836 if (ctx->mmap_size)
837 vm_munmap(ctx->mmap_base, ctx->mmap_size);
838
Jens Axboedc48e562015-04-15 11:17:23 -0600839 ctx->rq_wait = wait;
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400840 percpu_ref_kill(&ctx->users);
841 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Kent Overstreet36f55882013-05-07 16:18:41 -0700844/*
845 * exit_aio: called when the last user of mm goes away. At this point, there is
846 * no way for any new requests to be submited or any of the io_* syscalls to be
847 * called on the context.
848 *
849 * There may be outstanding kiocbs, but free_ioctx() will explicitly wait on
850 * them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800852void exit_aio(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200854 struct kioctx_table *table = rcu_dereference_raw(mm->ioctx_table);
Jens Axboedc48e562015-04-15 11:17:23 -0600855 struct ctx_rq_wait wait;
856 int i, skipped;
Jens Axboeabf137d2008-12-09 08:11:22 +0100857
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200858 if (!table)
859 return;
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400860
Jens Axboedc48e562015-04-15 11:17:23 -0600861 atomic_set(&wait.count, table->nr);
862 init_completion(&wait.comp);
863
864 skipped = 0;
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200865 for (i = 0; i < table->nr; ++i) {
Tejun Heod0264c02018-03-14 12:10:17 -0700866 struct kioctx *ctx =
867 rcu_dereference_protected(table->table[i], true);
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400868
Jens Axboedc48e562015-04-15 11:17:23 -0600869 if (!ctx) {
870 skipped++;
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200871 continue;
Jens Axboedc48e562015-04-15 11:17:23 -0600872 }
873
Al Viro936af152012-04-20 21:49:41 -0400874 /*
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200875 * We don't need to bother with munmap() here - exit_mmap(mm)
876 * is coming and it'll unmap everything. And we simply can't,
877 * this is not necessarily our ->mm.
878 * Since kill_ioctx() uses non-zero ->mmap_size as indicator
879 * that it needs to unmap the area, just set it to 0.
Al Viro936af152012-04-20 21:49:41 -0400880 */
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700881 ctx->mmap_size = 0;
Jens Axboedc48e562015-04-15 11:17:23 -0600882 kill_ioctx(mm, ctx, &wait);
883 }
Kent Overstreet36f55882013-05-07 16:18:41 -0700884
Jens Axboedc48e562015-04-15 11:17:23 -0600885 if (!atomic_sub_and_test(skipped, &wait.count)) {
Gu Zheng6098b452014-09-03 17:45:44 +0800886 /* Wait until all IO for the context are done. */
Jens Axboedc48e562015-04-15 11:17:23 -0600887 wait_for_completion(&wait.comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200889
890 RCU_INIT_POINTER(mm->ioctx_table, NULL);
891 kfree(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000894static void put_reqs_available(struct kioctx *ctx, unsigned nr)
895{
896 struct kioctx_cpu *kcpu;
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400897 unsigned long flags;
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000898
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400899 local_irq_save(flags);
Benjamin LaHaisebe6fb452014-07-22 09:56:56 -0400900 kcpu = this_cpu_ptr(ctx->cpu);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000901 kcpu->reqs_available += nr;
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400902
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000903 while (kcpu->reqs_available >= ctx->req_batch * 2) {
904 kcpu->reqs_available -= ctx->req_batch;
905 atomic_add(ctx->req_batch, &ctx->reqs_available);
906 }
907
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400908 local_irq_restore(flags);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000909}
910
Christoph Hellwig432c7992018-11-19 15:57:42 -0700911static bool __get_reqs_available(struct kioctx *ctx)
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000912{
913 struct kioctx_cpu *kcpu;
914 bool ret = false;
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400915 unsigned long flags;
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000916
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400917 local_irq_save(flags);
Benjamin LaHaisebe6fb452014-07-22 09:56:56 -0400918 kcpu = this_cpu_ptr(ctx->cpu);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000919 if (!kcpu->reqs_available) {
920 int old, avail = atomic_read(&ctx->reqs_available);
921
922 do {
923 if (avail < ctx->req_batch)
924 goto out;
925
926 old = avail;
927 avail = atomic_cmpxchg(&ctx->reqs_available,
928 avail, avail - ctx->req_batch);
929 } while (avail != old);
930
931 kcpu->reqs_available += ctx->req_batch;
932 }
933
934 ret = true;
935 kcpu->reqs_available--;
936out:
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400937 local_irq_restore(flags);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000938 return ret;
939}
940
Benjamin LaHaised856f322014-08-24 13:14:05 -0400941/* refill_reqs_available
942 * Updates the reqs_available reference counts used for tracking the
943 * number of free slots in the completion ring. This can be called
944 * from aio_complete() (to optimistically update reqs_available) or
945 * from aio_get_req() (the we're out of events case). It must be
946 * called holding ctx->completion_lock.
947 */
948static void refill_reqs_available(struct kioctx *ctx, unsigned head,
949 unsigned tail)
950{
951 unsigned events_in_ring, completed;
952
953 /* Clamp head since userland can write to it. */
954 head %= ctx->nr_events;
955 if (head <= tail)
956 events_in_ring = tail - head;
957 else
958 events_in_ring = ctx->nr_events - (head - tail);
959
960 completed = ctx->completed_events;
961 if (events_in_ring < completed)
962 completed -= events_in_ring;
963 else
964 completed = 0;
965
966 if (!completed)
967 return;
968
969 ctx->completed_events -= completed;
970 put_reqs_available(ctx, completed);
971}
972
973/* user_refill_reqs_available
974 * Called to refill reqs_available when aio_get_req() encounters an
975 * out of space in the completion ring.
976 */
977static void user_refill_reqs_available(struct kioctx *ctx)
978{
979 spin_lock_irq(&ctx->completion_lock);
980 if (ctx->completed_events) {
981 struct aio_ring *ring;
982 unsigned head;
983
984 /* Access of ring->head may race with aio_read_events_ring()
985 * here, but that's okay since whether we read the old version
986 * or the new version, and either will be valid. The important
987 * part is that head cannot pass tail since we prevent
988 * aio_complete() from updating tail by holding
989 * ctx->completion_lock. Even if head is invalid, the check
990 * against ctx->completed_events below will make sure we do the
991 * safe/right thing.
992 */
993 ring = kmap_atomic(ctx->ring_pages[0]);
994 head = ring->head;
995 kunmap_atomic(ring);
996
997 refill_reqs_available(ctx, head, ctx->tail);
998 }
999
1000 spin_unlock_irq(&ctx->completion_lock);
1001}
1002
Christoph Hellwig432c7992018-11-19 15:57:42 -07001003static bool get_reqs_available(struct kioctx *ctx)
1004{
1005 if (__get_reqs_available(ctx))
1006 return true;
1007 user_refill_reqs_available(ctx);
1008 return __get_reqs_available(ctx);
1009}
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011/* aio_get_req
Kent Overstreet57282d82013-05-13 13:42:52 -07001012 * Allocate a slot for an aio request.
1013 * Returns NULL if no requests are free.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 */
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001015static inline struct aio_kiocb *aio_get_req(struct kioctx *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001017 struct aio_kiocb *req;
Kent Overstreeta1c8eae2013-05-07 16:18:53 -07001018
Jens Axboe2bc4ca92018-12-04 09:44:49 -07001019 req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (unlikely(!req))
Christoph Hellwig432c7992018-11-19 15:57:42 -07001021 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Kent Overstreete34ecee2013-10-10 19:31:47 -07001023 percpu_ref_get(&ctx->reqs);
Jens Axboe2bc4ca92018-12-04 09:44:49 -07001024 req->ki_ctx = ctx;
Christoph Hellwig75321b52018-04-09 14:57:56 +02001025 INIT_LIST_HEAD(&req->ki_list);
Christoph Hellwig9018ccc2018-07-24 11:36:37 +02001026 refcount_set(&req->ki_refcnt, 0);
Jens Axboe2bc4ca92018-12-04 09:44:49 -07001027 req->ki_eventfd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
Adrian Bunkd5470b52008-04-29 00:58:57 -07001031static struct kioctx *lookup_ioctx(unsigned long ctx_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Benjamin LaHaisedb446a02013-07-30 12:54:40 -04001033 struct aio_ring __user *ring = (void __user *)ctx_id;
Jens Axboeabf137d2008-12-09 08:11:22 +01001034 struct mm_struct *mm = current->mm;
Jeff Moyer65c24492009-03-18 17:04:21 -07001035 struct kioctx *ctx, *ret = NULL;
Benjamin LaHaisedb446a02013-07-30 12:54:40 -04001036 struct kioctx_table *table;
1037 unsigned id;
1038
1039 if (get_user(id, &ring->id))
1040 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Jens Axboeabf137d2008-12-09 08:11:22 +01001042 rcu_read_lock();
Benjamin LaHaisedb446a02013-07-30 12:54:40 -04001043 table = rcu_dereference(mm->ioctx_table);
Jens Axboeabf137d2008-12-09 08:11:22 +01001044
Benjamin LaHaisedb446a02013-07-30 12:54:40 -04001045 if (!table || id >= table->nr)
1046 goto out;
1047
Jeff Moyera538e3f2018-12-11 12:37:49 -05001048 id = array_index_nospec(id, table->nr);
Tejun Heod0264c02018-03-14 12:10:17 -07001049 ctx = rcu_dereference(table->table[id]);
Benjamin LaHaisef30d7042013-08-07 18:23:48 -04001050 if (ctx && ctx->user_id == ctx_id) {
Al Virobaf10562018-05-20 16:46:23 -04001051 if (percpu_ref_tryget_live(&ctx->users))
1052 ret = ctx;
Jens Axboeabf137d2008-12-09 08:11:22 +01001053 }
Benjamin LaHaisedb446a02013-07-30 12:54:40 -04001054out:
Jens Axboeabf137d2008-12-09 08:11:22 +01001055 rcu_read_unlock();
Jeff Moyer65c24492009-03-18 17:04:21 -07001056 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
Christoph Hellwig9018ccc2018-07-24 11:36:37 +02001059static inline void iocb_put(struct aio_kiocb *iocb)
1060{
1061 if (refcount_read(&iocb->ki_refcnt) == 0 ||
1062 refcount_dec_and_test(&iocb->ki_refcnt)) {
1063 percpu_ref_put(&iocb->ki_ctx->reqs);
1064 kmem_cache_free(kiocb_cachep, iocb);
1065 }
1066}
1067
Jens Axboe875736b2018-11-20 20:06:23 -07001068static void aio_fill_event(struct io_event *ev, struct aio_kiocb *iocb,
1069 long res, long res2)
1070{
1071 ev->obj = (u64)(unsigned long)iocb->ki_user_iocb;
1072 ev->data = iocb->ki_user_data;
1073 ev->res = res;
1074 ev->res2 = res2;
1075}
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077/* aio_complete
1078 * Called when the io request on the given iocb is complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 */
Christoph Hellwig54843f82018-05-02 19:57:21 +02001080static void aio_complete(struct aio_kiocb *iocb, long res, long res2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
1082 struct kioctx *ctx = iocb->ki_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 struct aio_ring *ring;
Kent Overstreet21b40202013-05-07 16:18:47 -07001084 struct io_event *ev_page, *event;
Benjamin LaHaised856f322014-08-24 13:14:05 -04001085 unsigned tail, pos, head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 /*
Kent Overstreet0460fef2013-05-07 16:18:49 -07001089 * Add a completion event to the ring buffer. Must be done holding
Tang Chen4b30f072013-07-03 15:09:16 -07001090 * ctx->completion_lock to prevent other code from messing with the tail
Kent Overstreet0460fef2013-05-07 16:18:49 -07001091 * pointer since we might be called from irq context.
1092 */
1093 spin_lock_irqsave(&ctx->completion_lock, flags);
1094
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001095 tail = ctx->tail;
Kent Overstreet21b40202013-05-07 16:18:47 -07001096 pos = tail + AIO_EVENTS_OFFSET;
1097
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001098 if (++tail >= ctx->nr_events)
Ken Chen4bf69b22005-05-01 08:59:15 -07001099 tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001101 ev_page = kmap_atomic(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
Kent Overstreet21b40202013-05-07 16:18:47 -07001102 event = ev_page + pos % AIO_EVENTS_PER_PAGE;
1103
Jens Axboe875736b2018-11-20 20:06:23 -07001104 aio_fill_event(event, iocb, res, res2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Kent Overstreet21b40202013-05-07 16:18:47 -07001106 kunmap_atomic(ev_page);
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001107 flush_dcache_page(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
Kent Overstreet21b40202013-05-07 16:18:47 -07001108
1109 pr_debug("%p[%u]: %p: %p %Lx %lx %lx\n",
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001110 ctx, tail, iocb, iocb->ki_user_iocb, iocb->ki_user_data,
Kent Overstreetcaf41672013-05-07 16:18:35 -07001111 res, res2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 /* after flagging the request as done, we
1114 * must never even look at it again
1115 */
1116 smp_wmb(); /* make event visible before updating tail */
1117
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001118 ctx->tail = tail;
Kent Overstreet21b40202013-05-07 16:18:47 -07001119
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001120 ring = kmap_atomic(ctx->ring_pages[0]);
Benjamin LaHaised856f322014-08-24 13:14:05 -04001121 head = ring->head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 ring->tail = tail;
Cong Wange8e3c3d2011-11-25 23:14:27 +08001123 kunmap_atomic(ring);
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001124 flush_dcache_page(ctx->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Benjamin LaHaised856f322014-08-24 13:14:05 -04001126 ctx->completed_events++;
1127 if (ctx->completed_events > 1)
1128 refill_reqs_available(ctx, head, tail);
Kent Overstreet0460fef2013-05-07 16:18:49 -07001129 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1130
Kent Overstreet21b40202013-05-07 16:18:47 -07001131 pr_debug("added to ring %p at [%u]\n", iocb, tail);
Davide Libenzi8d1c98b2008-04-10 21:29:19 -07001132
1133 /*
1134 * Check if the user asked us to deliver the result through an
1135 * eventfd. The eventfd_signal() function is safe to be called
1136 * from IRQ context.
1137 */
Christoph Hellwig54843f82018-05-02 19:57:21 +02001138 if (iocb->ki_eventfd) {
Davide Libenzi8d1c98b2008-04-10 21:29:19 -07001139 eventfd_signal(iocb->ki_eventfd, 1);
Christoph Hellwig54843f82018-05-02 19:57:21 +02001140 eventfd_ctx_put(iocb->ki_eventfd);
1141 }
Davide Libenzi8d1c98b2008-04-10 21:29:19 -07001142
Quentin Barnes6cb2a212008-03-19 17:00:39 -07001143 /*
1144 * We have to order our ring_info tail store above and test
1145 * of the wait list below outside the wait lock. This is
1146 * like in wake_up_bit() where clearing a bit has to be
1147 * ordered with the unlocked test.
1148 */
1149 smp_mb();
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (waitqueue_active(&ctx->wait))
1152 wake_up(&ctx->wait);
Christoph Hellwig9018ccc2018-07-24 11:36:37 +02001153 iocb_put(iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
Gu Zheng2be4e7d2014-07-23 18:03:53 +08001156/* aio_read_events_ring
Kent Overstreeta31ad382013-05-07 16:18:45 -07001157 * Pull an event off of the ioctx's event ring. Returns the number of
1158 * events fetched
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 */
Kent Overstreeta31ad382013-05-07 16:18:45 -07001160static long aio_read_events_ring(struct kioctx *ctx,
1161 struct io_event __user *event, long nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 struct aio_ring *ring;
Kent Overstreet5ffac122013-05-09 15:36:07 -07001164 unsigned head, tail, pos;
Kent Overstreeta31ad382013-05-07 16:18:45 -07001165 long ret = 0;
1166 int copy_ret;
1167
Dave Chinner9c9ce762015-02-03 19:29:05 -05001168 /*
1169 * The mutex can block and wake us up and that will cause
1170 * wait_event_interruptible_hrtimeout() to schedule without sleeping
1171 * and repeat. This should be rare enough that it doesn't cause
1172 * peformance issues. See the comment in read_events() for more detail.
1173 */
1174 sched_annotate_sleep();
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001175 mutex_lock(&ctx->ring_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -04001177 /* Access to ->ring_pages here is protected by ctx->ring_lock. */
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001178 ring = kmap_atomic(ctx->ring_pages[0]);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001179 head = ring->head;
Kent Overstreet5ffac122013-05-09 15:36:07 -07001180 tail = ring->tail;
Kent Overstreeta31ad382013-05-07 16:18:45 -07001181 kunmap_atomic(ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Jeff Moyer2ff396be2014-09-02 13:17:00 -04001183 /*
1184 * Ensure that once we've read the current tail pointer, that
1185 * we also see the events that were stored up to the tail.
1186 */
1187 smp_rmb();
1188
Kent Overstreet5ffac122013-05-09 15:36:07 -07001189 pr_debug("h%u t%u m%u\n", head, tail, ctx->nr_events);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001190
Kent Overstreet5ffac122013-05-09 15:36:07 -07001191 if (head == tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 goto out;
1193
Benjamin LaHaiseedfbbf32014-06-24 13:32:51 -04001194 head %= ctx->nr_events;
1195 tail %= ctx->nr_events;
1196
Kent Overstreeta31ad382013-05-07 16:18:45 -07001197 while (ret < nr) {
1198 long avail;
1199 struct io_event *ev;
1200 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Kent Overstreet5ffac122013-05-09 15:36:07 -07001202 avail = (head <= tail ? tail : ctx->nr_events) - head;
1203 if (head == tail)
Kent Overstreeta31ad382013-05-07 16:18:45 -07001204 break;
1205
Kent Overstreeta31ad382013-05-07 16:18:45 -07001206 pos = head + AIO_EVENTS_OFFSET;
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001207 page = ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE];
Kent Overstreeta31ad382013-05-07 16:18:45 -07001208 pos %= AIO_EVENTS_PER_PAGE;
1209
Al Virod2988bd42018-05-26 19:13:10 -04001210 avail = min(avail, nr - ret);
1211 avail = min_t(long, avail, AIO_EVENTS_PER_PAGE - pos);
1212
Kent Overstreeta31ad382013-05-07 16:18:45 -07001213 ev = kmap(page);
1214 copy_ret = copy_to_user(event + ret, ev + pos,
1215 sizeof(*ev) * avail);
1216 kunmap(page);
1217
1218 if (unlikely(copy_ret)) {
1219 ret = -EFAULT;
1220 goto out;
1221 }
1222
1223 ret += avail;
1224 head += avail;
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001225 head %= ctx->nr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001228 ring = kmap_atomic(ctx->ring_pages[0]);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001229 ring->head = head;
Zhao Hongjiang91d80a82013-04-26 11:03:53 +08001230 kunmap_atomic(ring);
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001231 flush_dcache_page(ctx->ring_pages[0]);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001232
Kent Overstreet5ffac122013-05-09 15:36:07 -07001233 pr_debug("%li h%u t%u\n", ret, head, tail);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001234out:
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001235 mutex_unlock(&ctx->ring_lock);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return ret;
1238}
1239
Kent Overstreeta31ad382013-05-07 16:18:45 -07001240static bool aio_read_events(struct kioctx *ctx, long min_nr, long nr,
1241 struct io_event __user *event, long *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Kent Overstreeta31ad382013-05-07 16:18:45 -07001243 long ret = aio_read_events_ring(ctx, event + *i, nr - *i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Kent Overstreeta31ad382013-05-07 16:18:45 -07001245 if (ret > 0)
1246 *i += ret;
1247
1248 if (unlikely(atomic_read(&ctx->dead)))
1249 ret = -EINVAL;
1250
1251 if (!*i)
1252 *i = ret;
1253
1254 return ret < 0 || *i >= min_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
Kent Overstreeta31ad382013-05-07 16:18:45 -07001257static long read_events(struct kioctx *ctx, long min_nr, long nr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 struct io_event __user *event,
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07001259 ktime_t until)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Kent Overstreeta31ad382013-05-07 16:18:45 -07001261 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Kent Overstreeta31ad382013-05-07 16:18:45 -07001263 /*
1264 * Note that aio_read_events() is being called as the conditional - i.e.
1265 * we're calling it after prepare_to_wait() has set task state to
1266 * TASK_INTERRUPTIBLE.
1267 *
1268 * But aio_read_events() can block, and if it blocks it's going to flip
1269 * the task state back to TASK_RUNNING.
1270 *
1271 * This should be ok, provided it doesn't flip the state back to
1272 * TASK_RUNNING and return 0 too much - that causes us to spin. That
1273 * will only happen if the mutex_lock() call blocks, and we then find
1274 * the ringbuffer empty. So in practice we should be ok, but it's
1275 * something to be aware of when touching this code.
1276 */
Thomas Gleixner2456e852016-12-25 11:38:40 +01001277 if (until == 0)
Fam Zheng5f785de2014-11-06 20:44:36 +08001278 aio_read_events(ctx, min_nr, nr, event, &ret);
1279 else
1280 wait_event_interruptible_hrtimeout(ctx->wait,
1281 aio_read_events(ctx, min_nr, nr, event, &ret),
1282 until);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001283 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286/* sys_io_setup:
1287 * Create an aio_context capable of receiving at least nr_events.
1288 * ctxp must not point to an aio_context that already exists, and
1289 * must be initialized to 0 prior to the call. On successful
1290 * creation of the aio_context, *ctxp is filled in with the resulting
1291 * handle. May fail with -EINVAL if *ctxp is not initialized,
1292 * if the specified nr_events exceeds internal limits. May fail
1293 * with -EAGAIN if the specified nr_events exceeds the user's limit
1294 * of available events. May fail with -ENOMEM if insufficient kernel
1295 * resources are available. May fail with -EFAULT if an invalid
1296 * pointer is passed for ctxp. Will fail with -ENOSYS if not
1297 * implemented.
1298 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001299SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 struct kioctx *ioctx = NULL;
1302 unsigned long ctx;
1303 long ret;
1304
1305 ret = get_user(ctx, ctxp);
1306 if (unlikely(ret))
1307 goto out;
1308
1309 ret = -EINVAL;
Zach Brownd55b5fd2005-11-07 00:59:31 -08001310 if (unlikely(ctx || nr_events == 0)) {
Kinglong Meeacd88d42015-02-04 21:15:59 +08001311 pr_debug("EINVAL: ctx %lu nr_events %u\n",
Zach Brownd55b5fd2005-11-07 00:59:31 -08001312 ctx, nr_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 goto out;
1314 }
1315
1316 ioctx = ioctx_alloc(nr_events);
1317 ret = PTR_ERR(ioctx);
1318 if (!IS_ERR(ioctx)) {
1319 ret = put_user(ioctx->user_id, ctxp);
Al Viroa2e18592012-03-20 16:27:57 -04001320 if (ret)
Anatol Pomozove02ba722014-04-15 11:31:33 -07001321 kill_ioctx(current->mm, ioctx, NULL);
Kent Overstreet723be6e2013-05-28 15:14:48 -07001322 percpu_ref_put(&ioctx->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
1324
1325out:
1326 return ret;
1327}
1328
Al Viroc00d2c72016-12-20 07:04:57 -05001329#ifdef CONFIG_COMPAT
1330COMPAT_SYSCALL_DEFINE2(io_setup, unsigned, nr_events, u32 __user *, ctx32p)
1331{
1332 struct kioctx *ioctx = NULL;
1333 unsigned long ctx;
1334 long ret;
1335
1336 ret = get_user(ctx, ctx32p);
1337 if (unlikely(ret))
1338 goto out;
1339
1340 ret = -EINVAL;
1341 if (unlikely(ctx || nr_events == 0)) {
1342 pr_debug("EINVAL: ctx %lu nr_events %u\n",
1343 ctx, nr_events);
1344 goto out;
1345 }
1346
1347 ioctx = ioctx_alloc(nr_events);
1348 ret = PTR_ERR(ioctx);
1349 if (!IS_ERR(ioctx)) {
1350 /* truncating is ok because it's a user address */
1351 ret = put_user((u32)ioctx->user_id, ctx32p);
1352 if (ret)
1353 kill_ioctx(current->mm, ioctx, NULL);
1354 percpu_ref_put(&ioctx->users);
1355 }
1356
1357out:
1358 return ret;
1359}
1360#endif
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362/* sys_io_destroy:
1363 * Destroy the aio_context specified. May cancel any outstanding
1364 * AIOs and block on completion. Will fail with -ENOSYS if not
Satoru Takeuchi642b5122010-08-05 11:23:11 -07001365 * implemented. May fail with -EINVAL if the context pointed to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 * is invalid.
1367 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001368SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
1370 struct kioctx *ioctx = lookup_ioctx(ctx);
1371 if (likely(NULL != ioctx)) {
Jens Axboedc48e562015-04-15 11:17:23 -06001372 struct ctx_rq_wait wait;
Benjamin LaHaisefb2d4482014-04-29 12:45:17 -04001373 int ret;
Anatol Pomozove02ba722014-04-15 11:31:33 -07001374
Jens Axboedc48e562015-04-15 11:17:23 -06001375 init_completion(&wait.comp);
1376 atomic_set(&wait.count, 1);
1377
Anatol Pomozove02ba722014-04-15 11:31:33 -07001378 /* Pass requests_done to kill_ioctx() where it can be set
1379 * in a thread-safe way. If we try to set it here then we have
1380 * a race condition if two io_destroy() called simultaneously.
1381 */
Jens Axboedc48e562015-04-15 11:17:23 -06001382 ret = kill_ioctx(current->mm, ioctx, &wait);
Kent Overstreet723be6e2013-05-28 15:14:48 -07001383 percpu_ref_put(&ioctx->users);
Anatol Pomozove02ba722014-04-15 11:31:33 -07001384
1385 /* Wait until all IO for the context are done. Otherwise kernel
1386 * keep using user-space buffers even if user thinks the context
1387 * is destroyed.
1388 */
Benjamin LaHaisefb2d4482014-04-29 12:45:17 -04001389 if (!ret)
Jens Axboedc48e562015-04-15 11:17:23 -06001390 wait_for_completion(&wait.comp);
Anatol Pomozove02ba722014-04-15 11:31:33 -07001391
Benjamin LaHaisefb2d4482014-04-29 12:45:17 -04001392 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
Kinglong Meeacd88d42015-02-04 21:15:59 +08001394 pr_debug("EINVAL: invalid context id\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return -EINVAL;
1396}
1397
Al Viro3c96c7f2018-05-28 13:37:43 -04001398static void aio_remove_iocb(struct aio_kiocb *iocb)
1399{
1400 struct kioctx *ctx = iocb->ki_ctx;
1401 unsigned long flags;
1402
1403 spin_lock_irqsave(&ctx->ctx_lock, flags);
1404 list_del(&iocb->ki_list);
1405 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
1406}
1407
Christoph Hellwig54843f82018-05-02 19:57:21 +02001408static void aio_complete_rw(struct kiocb *kiocb, long res, long res2)
1409{
1410 struct aio_kiocb *iocb = container_of(kiocb, struct aio_kiocb, rw);
1411
Al Viro3c96c7f2018-05-28 13:37:43 -04001412 if (!list_empty_careful(&iocb->ki_list))
1413 aio_remove_iocb(iocb);
1414
Christoph Hellwig54843f82018-05-02 19:57:21 +02001415 if (kiocb->ki_flags & IOCB_WRITE) {
1416 struct inode *inode = file_inode(kiocb->ki_filp);
1417
1418 /*
1419 * Tell lockdep we inherited freeze protection from submission
1420 * thread.
1421 */
1422 if (S_ISREG(inode->i_mode))
1423 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
1424 file_end_write(kiocb->ki_filp);
1425 }
1426
1427 fput(kiocb->ki_filp);
1428 aio_complete(iocb, res, res2);
1429}
1430
Jens Axboe88a6f182018-11-24 14:46:14 -07001431static int aio_prep_rw(struct kiocb *req, const struct iocb *iocb)
Christoph Hellwig54843f82018-05-02 19:57:21 +02001432{
1433 int ret;
1434
1435 req->ki_filp = fget(iocb->aio_fildes);
1436 if (unlikely(!req->ki_filp))
1437 return -EBADF;
1438 req->ki_complete = aio_complete_rw;
Mike Marshallec51f8e2019-02-05 14:13:35 -05001439 req->private = NULL;
Christoph Hellwig54843f82018-05-02 19:57:21 +02001440 req->ki_pos = iocb->aio_offset;
1441 req->ki_flags = iocb_flags(req->ki_filp);
1442 if (iocb->aio_flags & IOCB_FLAG_RESFD)
1443 req->ki_flags |= IOCB_EVENTFD;
Adam Manzanaresfc287242018-05-22 10:52:18 -07001444 req->ki_hint = ki_hint_validate(file_write_hint(req->ki_filp));
Adam Manzanaresd9a08a92018-05-22 10:52:19 -07001445 if (iocb->aio_flags & IOCB_FLAG_IOPRIO) {
1446 /*
1447 * If the IOCB_FLAG_IOPRIO flag of aio_flags is set, then
1448 * aio_reqprio is interpreted as an I/O scheduling
1449 * class and priority.
1450 */
1451 ret = ioprio_check_cap(iocb->aio_reqprio);
1452 if (ret) {
Adam Manzanares9a6d9a62018-06-04 10:59:57 -07001453 pr_debug("aio ioprio check cap error: %d\n", ret);
Christoph Hellwig154989e2018-11-22 16:44:07 +01001454 goto out_fput;
Adam Manzanaresd9a08a92018-05-22 10:52:19 -07001455 }
1456
1457 req->ki_ioprio = iocb->aio_reqprio;
1458 } else
Damien Le Moal76dc8912018-11-20 10:52:36 +09001459 req->ki_ioprio = get_current_ioprio();
Adam Manzanaresd9a08a92018-05-22 10:52:19 -07001460
Christoph Hellwig54843f82018-05-02 19:57:21 +02001461 ret = kiocb_set_rw_flags(req, iocb->aio_rw_flags);
1462 if (unlikely(ret))
Christoph Hellwig154989e2018-11-22 16:44:07 +01001463 goto out_fput;
1464
1465 req->ki_flags &= ~IOCB_HIPRI; /* no one is going to poll for this I/O */
1466 return 0;
1467
1468out_fput:
1469 fput(req->ki_filp);
Christoph Hellwig54843f82018-05-02 19:57:21 +02001470 return ret;
1471}
1472
Jens Axboe88a6f182018-11-24 14:46:14 -07001473static int aio_setup_rw(int rw, const struct iocb *iocb, struct iovec **iovec,
Christoph Hellwig89319d312016-10-30 11:42:03 -05001474 bool vectored, bool compat, struct iov_iter *iter)
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001475{
Christoph Hellwig89319d312016-10-30 11:42:03 -05001476 void __user *buf = (void __user *)(uintptr_t)iocb->aio_buf;
1477 size_t len = iocb->aio_nbytes;
1478
1479 if (!vectored) {
1480 ssize_t ret = import_single_range(rw, buf, len, *iovec, iter);
1481 *iovec = NULL;
1482 return ret;
1483 }
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001484#ifdef CONFIG_COMPAT
1485 if (compat)
Christoph Hellwig89319d312016-10-30 11:42:03 -05001486 return compat_import_iovec(rw, buf, len, UIO_FASTIOV, iovec,
1487 iter);
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001488#endif
Christoph Hellwig89319d312016-10-30 11:42:03 -05001489 return import_iovec(rw, buf, len, UIO_FASTIOV, iovec, iter);
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001490}
1491
Al Viro9061d142018-05-26 19:11:40 -04001492static inline void aio_rw_done(struct kiocb *req, ssize_t ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Christoph Hellwig89319d312016-10-30 11:42:03 -05001494 switch (ret) {
1495 case -EIOCBQUEUED:
Al Viro9061d142018-05-26 19:11:40 -04001496 break;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001497 case -ERESTARTSYS:
1498 case -ERESTARTNOINTR:
1499 case -ERESTARTNOHAND:
1500 case -ERESTART_RESTARTBLOCK:
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001501 /*
1502 * There's no easy way to restart the syscall since other AIO's
1503 * may be already running. Just fail this IO with EINTR.
1504 */
Christoph Hellwig89319d312016-10-30 11:42:03 -05001505 ret = -EINTR;
1506 /*FALLTHRU*/
1507 default:
Jens Axboebc9bff62018-11-06 14:27:13 -07001508 req->ki_complete(req, ret, 0);
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001509 }
Christoph Hellwig89319d312016-10-30 11:42:03 -05001510}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Jens Axboe88a6f182018-11-24 14:46:14 -07001512static ssize_t aio_read(struct kiocb *req, const struct iocb *iocb,
1513 bool vectored, bool compat)
Christoph Hellwig89319d312016-10-30 11:42:03 -05001514{
Christoph Hellwig89319d312016-10-30 11:42:03 -05001515 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1516 struct iov_iter iter;
Christoph Hellwig54843f82018-05-02 19:57:21 +02001517 struct file *file;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001518 ssize_t ret;
1519
Christoph Hellwig54843f82018-05-02 19:57:21 +02001520 ret = aio_prep_rw(req, iocb);
1521 if (ret)
1522 return ret;
1523 file = req->ki_filp;
1524
1525 ret = -EBADF;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001526 if (unlikely(!(file->f_mode & FMODE_READ)))
Christoph Hellwig54843f82018-05-02 19:57:21 +02001527 goto out_fput;
1528 ret = -EINVAL;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001529 if (unlikely(!file->f_op->read_iter))
Christoph Hellwig54843f82018-05-02 19:57:21 +02001530 goto out_fput;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001531
1532 ret = aio_setup_rw(READ, iocb, &iovec, vectored, compat, &iter);
1533 if (ret)
Christoph Hellwig54843f82018-05-02 19:57:21 +02001534 goto out_fput;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001535 ret = rw_verify_area(READ, file, &req->ki_pos, iov_iter_count(&iter));
1536 if (!ret)
Al Viro9061d142018-05-26 19:11:40 -04001537 aio_rw_done(req, call_read_iter(file, req, &iter));
Christoph Hellwig89319d312016-10-30 11:42:03 -05001538 kfree(iovec);
Christoph Hellwig54843f82018-05-02 19:57:21 +02001539out_fput:
Al Viro9061d142018-05-26 19:11:40 -04001540 if (unlikely(ret))
Christoph Hellwig54843f82018-05-02 19:57:21 +02001541 fput(file);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001542 return ret;
1543}
1544
Jens Axboe88a6f182018-11-24 14:46:14 -07001545static ssize_t aio_write(struct kiocb *req, const struct iocb *iocb,
1546 bool vectored, bool compat)
Christoph Hellwig89319d312016-10-30 11:42:03 -05001547{
Christoph Hellwig89319d312016-10-30 11:42:03 -05001548 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1549 struct iov_iter iter;
Christoph Hellwig54843f82018-05-02 19:57:21 +02001550 struct file *file;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001551 ssize_t ret;
1552
Christoph Hellwig54843f82018-05-02 19:57:21 +02001553 ret = aio_prep_rw(req, iocb);
1554 if (ret)
1555 return ret;
1556 file = req->ki_filp;
1557
1558 ret = -EBADF;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001559 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Christoph Hellwig54843f82018-05-02 19:57:21 +02001560 goto out_fput;
1561 ret = -EINVAL;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001562 if (unlikely(!file->f_op->write_iter))
Christoph Hellwig54843f82018-05-02 19:57:21 +02001563 goto out_fput;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001564
1565 ret = aio_setup_rw(WRITE, iocb, &iovec, vectored, compat, &iter);
1566 if (ret)
Christoph Hellwig54843f82018-05-02 19:57:21 +02001567 goto out_fput;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001568 ret = rw_verify_area(WRITE, file, &req->ki_pos, iov_iter_count(&iter));
1569 if (!ret) {
Jan Kara70fe2f42016-10-30 11:42:04 -05001570 /*
Christoph Hellwig92ce4722018-04-06 09:28:17 +02001571 * Open-code file_start_write here to grab freeze protection,
Christoph Hellwig54843f82018-05-02 19:57:21 +02001572 * which will be released by another thread in
1573 * aio_complete_rw(). Fool lockdep by telling it the lock got
1574 * released so that it doesn't complain about the held lock when
1575 * we return to userspace.
Jan Kara70fe2f42016-10-30 11:42:04 -05001576 */
Christoph Hellwig92ce4722018-04-06 09:28:17 +02001577 if (S_ISREG(file_inode(file)->i_mode)) {
1578 __sb_start_write(file_inode(file)->i_sb, SB_FREEZE_WRITE, true);
Shaohua Lia12f1ae2016-12-13 12:09:56 -08001579 __sb_writers_release(file_inode(file)->i_sb, SB_FREEZE_WRITE);
Christoph Hellwig92ce4722018-04-06 09:28:17 +02001580 }
1581 req->ki_flags |= IOCB_WRITE;
Al Viro9061d142018-05-26 19:11:40 -04001582 aio_rw_done(req, call_write_iter(file, req, &iter));
Christoph Hellwig89319d312016-10-30 11:42:03 -05001583 }
1584 kfree(iovec);
Christoph Hellwig54843f82018-05-02 19:57:21 +02001585out_fput:
Al Viro9061d142018-05-26 19:11:40 -04001586 if (unlikely(ret))
Christoph Hellwig54843f82018-05-02 19:57:21 +02001587 fput(file);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001588 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
1590
Christoph Hellwiga3c0d432018-03-27 19:18:57 +02001591static void aio_fsync_work(struct work_struct *work)
1592{
1593 struct fsync_iocb *req = container_of(work, struct fsync_iocb, work);
1594 int ret;
1595
1596 ret = vfs_fsync(req->file, req->datasync);
1597 fput(req->file);
1598 aio_complete(container_of(req, struct aio_kiocb, fsync), ret, 0);
1599}
1600
Jens Axboe88a6f182018-11-24 14:46:14 -07001601static int aio_fsync(struct fsync_iocb *req, const struct iocb *iocb,
1602 bool datasync)
Christoph Hellwiga3c0d432018-03-27 19:18:57 +02001603{
1604 if (unlikely(iocb->aio_buf || iocb->aio_offset || iocb->aio_nbytes ||
1605 iocb->aio_rw_flags))
1606 return -EINVAL;
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001607
Christoph Hellwiga3c0d432018-03-27 19:18:57 +02001608 req->file = fget(iocb->aio_fildes);
1609 if (unlikely(!req->file))
1610 return -EBADF;
1611 if (unlikely(!req->file->f_op->fsync)) {
1612 fput(req->file);
1613 return -EINVAL;
1614 }
1615
1616 req->datasync = datasync;
1617 INIT_WORK(&req->work, aio_fsync_work);
1618 schedule_work(&req->work);
Al Viro9061d142018-05-26 19:11:40 -04001619 return 0;
Christoph Hellwiga3c0d432018-03-27 19:18:57 +02001620}
1621
Christoph Hellwigbfe40372018-07-16 09:08:20 +02001622static inline void aio_poll_complete(struct aio_kiocb *iocb, __poll_t mask)
1623{
1624 struct file *file = iocb->poll.file;
1625
1626 aio_complete(iocb, mangle_poll(mask), 0);
1627 fput(file);
1628}
1629
1630static void aio_poll_complete_work(struct work_struct *work)
1631{
1632 struct poll_iocb *req = container_of(work, struct poll_iocb, work);
1633 struct aio_kiocb *iocb = container_of(req, struct aio_kiocb, poll);
1634 struct poll_table_struct pt = { ._key = req->events };
1635 struct kioctx *ctx = iocb->ki_ctx;
1636 __poll_t mask = 0;
1637
1638 if (!READ_ONCE(req->cancelled))
1639 mask = vfs_poll(req->file, &pt) & req->events;
1640
1641 /*
1642 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1643 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1644 * synchronize with them. In the cancellation case the list_del_init
1645 * itself is not actually needed, but harmless so we keep it in to
1646 * avoid further branches in the fast path.
1647 */
1648 spin_lock_irq(&ctx->ctx_lock);
1649 if (!mask && !READ_ONCE(req->cancelled)) {
1650 add_wait_queue(req->head, &req->wait);
1651 spin_unlock_irq(&ctx->ctx_lock);
1652 return;
1653 }
1654 list_del_init(&iocb->ki_list);
1655 spin_unlock_irq(&ctx->ctx_lock);
1656
1657 aio_poll_complete(iocb, mask);
1658}
1659
1660/* assumes we are called with irqs disabled */
1661static int aio_poll_cancel(struct kiocb *iocb)
1662{
1663 struct aio_kiocb *aiocb = container_of(iocb, struct aio_kiocb, rw);
1664 struct poll_iocb *req = &aiocb->poll;
1665
1666 spin_lock(&req->head->lock);
1667 WRITE_ONCE(req->cancelled, true);
1668 if (!list_empty(&req->wait.entry)) {
1669 list_del_init(&req->wait.entry);
1670 schedule_work(&aiocb->poll.work);
1671 }
1672 spin_unlock(&req->head->lock);
1673
1674 return 0;
1675}
1676
1677static int aio_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1678 void *key)
1679{
1680 struct poll_iocb *req = container_of(wait, struct poll_iocb, wait);
Christoph Hellwige8693bc2018-07-16 12:25:17 +02001681 struct aio_kiocb *iocb = container_of(req, struct aio_kiocb, poll);
Christoph Hellwigbfe40372018-07-16 09:08:20 +02001682 __poll_t mask = key_to_poll(key);
1683
1684 req->woken = true;
1685
1686 /* for instances that support it check for an event match first: */
Christoph Hellwige8693bc2018-07-16 12:25:17 +02001687 if (mask) {
1688 if (!(mask & req->events))
1689 return 0;
1690
1691 /* try to complete the iocb inline if we can: */
1692 if (spin_trylock(&iocb->ki_ctx->ctx_lock)) {
1693 list_del(&iocb->ki_list);
1694 spin_unlock(&iocb->ki_ctx->ctx_lock);
1695
1696 list_del_init(&req->wait.entry);
1697 aio_poll_complete(iocb, mask);
1698 return 1;
1699 }
1700 }
Christoph Hellwigbfe40372018-07-16 09:08:20 +02001701
1702 list_del_init(&req->wait.entry);
1703 schedule_work(&req->work);
1704 return 1;
1705}
1706
1707struct aio_poll_table {
1708 struct poll_table_struct pt;
1709 struct aio_kiocb *iocb;
1710 int error;
1711};
1712
1713static void
1714aio_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1715 struct poll_table_struct *p)
1716{
1717 struct aio_poll_table *pt = container_of(p, struct aio_poll_table, pt);
1718
1719 /* multiple wait queues per file are not supported */
1720 if (unlikely(pt->iocb->poll.head)) {
1721 pt->error = -EINVAL;
1722 return;
1723 }
1724
1725 pt->error = 0;
1726 pt->iocb->poll.head = head;
1727 add_wait_queue(head, &pt->iocb->poll.wait);
1728}
1729
Jens Axboe88a6f182018-11-24 14:46:14 -07001730static ssize_t aio_poll(struct aio_kiocb *aiocb, const struct iocb *iocb)
Christoph Hellwigbfe40372018-07-16 09:08:20 +02001731{
1732 struct kioctx *ctx = aiocb->ki_ctx;
1733 struct poll_iocb *req = &aiocb->poll;
1734 struct aio_poll_table apt;
1735 __poll_t mask;
1736
1737 /* reject any unknown events outside the normal event mask. */
1738 if ((u16)iocb->aio_buf != iocb->aio_buf)
1739 return -EINVAL;
1740 /* reject fields that are not defined for poll */
1741 if (iocb->aio_offset || iocb->aio_nbytes || iocb->aio_rw_flags)
1742 return -EINVAL;
1743
1744 INIT_WORK(&req->work, aio_poll_complete_work);
1745 req->events = demangle_poll(iocb->aio_buf) | EPOLLERR | EPOLLHUP;
1746 req->file = fget(iocb->aio_fildes);
1747 if (unlikely(!req->file))
1748 return -EBADF;
1749
Jens Axboe2bc4ca92018-12-04 09:44:49 -07001750 req->head = NULL;
1751 req->woken = false;
1752 req->cancelled = false;
1753
Christoph Hellwigbfe40372018-07-16 09:08:20 +02001754 apt.pt._qproc = aio_poll_queue_proc;
1755 apt.pt._key = req->events;
1756 apt.iocb = aiocb;
1757 apt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1758
1759 /* initialized the list so that we can do list_empty checks */
1760 INIT_LIST_HEAD(&req->wait.entry);
1761 init_waitqueue_func_entry(&req->wait, aio_poll_wake);
1762
1763 /* one for removal from waitqueue, one for this function */
1764 refcount_set(&aiocb->ki_refcnt, 2);
1765
1766 mask = vfs_poll(req->file, &apt.pt) & req->events;
1767 if (unlikely(!req->head)) {
1768 /* we did not manage to set up a waitqueue, done */
1769 goto out;
1770 }
1771
1772 spin_lock_irq(&ctx->ctx_lock);
1773 spin_lock(&req->head->lock);
1774 if (req->woken) {
1775 /* wake_up context handles the rest */
1776 mask = 0;
1777 apt.error = 0;
1778 } else if (mask || apt.error) {
1779 /* if we get an error or a mask we are done */
1780 WARN_ON_ONCE(list_empty(&req->wait.entry));
1781 list_del_init(&req->wait.entry);
1782 } else {
1783 /* actually waiting for an event */
1784 list_add_tail(&aiocb->ki_list, &ctx->active_reqs);
1785 aiocb->ki_cancel = aio_poll_cancel;
1786 }
1787 spin_unlock(&req->head->lock);
1788 spin_unlock_irq(&ctx->ctx_lock);
1789
1790out:
1791 if (unlikely(apt.error)) {
1792 fput(req->file);
1793 return apt.error;
1794 }
1795
1796 if (mask)
1797 aio_poll_complete(aiocb, mask);
1798 iocb_put(aiocb);
1799 return 0;
1800}
1801
Jens Axboe88a6f182018-11-24 14:46:14 -07001802static int __io_submit_one(struct kioctx *ctx, const struct iocb *iocb,
1803 struct iocb __user *user_iocb, bool compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001805 struct aio_kiocb *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 ssize_t ret;
1807
1808 /* enforce forwards compatibility on users */
Jens Axboe88a6f182018-11-24 14:46:14 -07001809 if (unlikely(iocb->aio_reserved2)) {
Kent Overstreetcaf41672013-05-07 16:18:35 -07001810 pr_debug("EINVAL: reserve field set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 return -EINVAL;
1812 }
1813
1814 /* prevent overflows */
1815 if (unlikely(
Jens Axboe88a6f182018-11-24 14:46:14 -07001816 (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
1817 (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
1818 ((ssize_t)iocb->aio_nbytes < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 )) {
Kinglong Meeacd88d42015-02-04 21:15:59 +08001820 pr_debug("EINVAL: overflow check\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return -EINVAL;
1822 }
1823
Christoph Hellwig432c7992018-11-19 15:57:42 -07001824 if (!get_reqs_available(ctx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 return -EAGAIN;
Kent Overstreet1d98ebf2013-05-07 16:18:37 -07001826
Christoph Hellwig432c7992018-11-19 15:57:42 -07001827 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 req = aio_get_req(ctx);
1829 if (unlikely(!req))
Christoph Hellwig432c7992018-11-19 15:57:42 -07001830 goto out_put_reqs_available;
Kent Overstreet1d98ebf2013-05-07 16:18:37 -07001831
Jens Axboe88a6f182018-11-24 14:46:14 -07001832 if (iocb->aio_flags & IOCB_FLAG_RESFD) {
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001833 /*
1834 * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
1835 * instance of the file* now. The file descriptor must be
1836 * an eventfd() fd, and will be signaled for each completed
1837 * event using the eventfd_signal() function.
1838 */
Jens Axboe88a6f182018-11-24 14:46:14 -07001839 req->ki_eventfd = eventfd_ctx_fdget((int) iocb->aio_resfd);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07001840 if (IS_ERR(req->ki_eventfd)) {
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001841 ret = PTR_ERR(req->ki_eventfd);
Davide Libenzi87c3a862009-03-18 17:04:19 -07001842 req->ki_eventfd = NULL;
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001843 goto out_put_req;
1844 }
Goldwyn Rodrigues9830f4b2017-06-20 07:05:42 -05001845 }
1846
Kent Overstreet8a660892013-05-07 16:19:10 -07001847 ret = put_user(KIOCB_KEY, &user_iocb->aio_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (unlikely(ret)) {
Kent Overstreetcaf41672013-05-07 16:18:35 -07001849 pr_debug("EFAULT: aio_key\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 goto out_put_req;
1851 }
1852
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001853 req->ki_user_iocb = user_iocb;
Jens Axboe88a6f182018-11-24 14:46:14 -07001854 req->ki_user_data = iocb->aio_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Jens Axboe88a6f182018-11-24 14:46:14 -07001856 switch (iocb->aio_lio_opcode) {
Christoph Hellwig89319d312016-10-30 11:42:03 -05001857 case IOCB_CMD_PREAD:
Jens Axboe88a6f182018-11-24 14:46:14 -07001858 ret = aio_read(&req->rw, iocb, false, compat);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001859 break;
1860 case IOCB_CMD_PWRITE:
Jens Axboe88a6f182018-11-24 14:46:14 -07001861 ret = aio_write(&req->rw, iocb, false, compat);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001862 break;
1863 case IOCB_CMD_PREADV:
Jens Axboe88a6f182018-11-24 14:46:14 -07001864 ret = aio_read(&req->rw, iocb, true, compat);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001865 break;
1866 case IOCB_CMD_PWRITEV:
Jens Axboe88a6f182018-11-24 14:46:14 -07001867 ret = aio_write(&req->rw, iocb, true, compat);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001868 break;
Christoph Hellwiga3c0d432018-03-27 19:18:57 +02001869 case IOCB_CMD_FSYNC:
Jens Axboe88a6f182018-11-24 14:46:14 -07001870 ret = aio_fsync(&req->fsync, iocb, false);
Christoph Hellwiga3c0d432018-03-27 19:18:57 +02001871 break;
1872 case IOCB_CMD_FDSYNC:
Jens Axboe88a6f182018-11-24 14:46:14 -07001873 ret = aio_fsync(&req->fsync, iocb, true);
Christoph Hellwigac060cb2018-05-28 07:19:49 +02001874 break;
Christoph Hellwigbfe40372018-07-16 09:08:20 +02001875 case IOCB_CMD_POLL:
Jens Axboe88a6f182018-11-24 14:46:14 -07001876 ret = aio_poll(req, iocb);
Christoph Hellwigbfe40372018-07-16 09:08:20 +02001877 break;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001878 default:
Jens Axboe88a6f182018-11-24 14:46:14 -07001879 pr_debug("invalid aio operation %d\n", iocb->aio_lio_opcode);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001880 ret = -EINVAL;
1881 break;
1882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Christoph Hellwig92ce4722018-04-06 09:28:17 +02001884 /*
Al Viro9061d142018-05-26 19:11:40 -04001885 * If ret is 0, we'd either done aio_complete() ourselves or have
1886 * arranged for that to be done asynchronously. Anything non-zero
1887 * means that we need to destroy req ourselves.
Christoph Hellwig92ce4722018-04-06 09:28:17 +02001888 */
Al Viro9061d142018-05-26 19:11:40 -04001889 if (ret)
Christoph Hellwig89319d312016-10-30 11:42:03 -05001890 goto out_put_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892out_put_req:
Christoph Hellwig54843f82018-05-02 19:57:21 +02001893 if (req->ki_eventfd)
1894 eventfd_ctx_put(req->ki_eventfd);
Jens Axboe71ebc6f2018-11-24 21:33:09 -07001895 iocb_put(req);
Christoph Hellwig432c7992018-11-19 15:57:42 -07001896out_put_reqs_available:
1897 put_reqs_available(ctx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 return ret;
1899}
1900
Jens Axboe88a6f182018-11-24 14:46:14 -07001901static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
1902 bool compat)
1903{
1904 struct iocb iocb;
1905
1906 if (unlikely(copy_from_user(&iocb, user_iocb, sizeof(iocb))))
1907 return -EFAULT;
1908
1909 return __io_submit_one(ctx, &iocb, user_iocb, compat);
1910}
1911
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001912/* sys_io_submit:
1913 * Queue the nr iocbs pointed to by iocbpp for processing. Returns
1914 * the number of iocbs queued. May return -EINVAL if the aio_context
1915 * specified by ctx_id is invalid, if nr is < 0, if the iocb at
1916 * *iocbpp[0] is not properly initialized, if the operation specified
1917 * is invalid for the file descriptor in the iocb. May fail with
1918 * -EFAULT if any of the data structures point to invalid data. May
1919 * fail with -EBADF if the file descriptor specified in the first
1920 * iocb is invalid. May fail with -EAGAIN if insufficient resources
1921 * are available to queue any iocbs. Will return 0 if nr is 0. Will
1922 * fail with -ENOSYS if not implemented.
1923 */
1924SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
1925 struct iocb __user * __user *, iocbpp)
1926{
Al Viro67ba0492018-05-26 20:10:07 -04001927 struct kioctx *ctx;
1928 long ret = 0;
1929 int i = 0;
1930 struct blk_plug plug;
1931
1932 if (unlikely(nr < 0))
1933 return -EINVAL;
1934
Al Viro67ba0492018-05-26 20:10:07 -04001935 ctx = lookup_ioctx(ctx_id);
1936 if (unlikely(!ctx)) {
1937 pr_debug("EINVAL: invalid context id\n");
1938 return -EINVAL;
1939 }
1940
Al Viro1da92772018-05-26 20:10:07 -04001941 if (nr > ctx->nr_events)
1942 nr = ctx->nr_events;
1943
Jens Axboea79d40e2018-12-04 09:45:32 -07001944 if (nr > AIO_PLUG_THRESHOLD)
1945 blk_start_plug(&plug);
Al Viro67ba0492018-05-26 20:10:07 -04001946 for (i = 0; i < nr; i++) {
1947 struct iocb __user *user_iocb;
1948
1949 if (unlikely(get_user(user_iocb, iocbpp + i))) {
1950 ret = -EFAULT;
1951 break;
1952 }
1953
1954 ret = io_submit_one(ctx, user_iocb, false);
1955 if (ret)
1956 break;
1957 }
Jens Axboea79d40e2018-12-04 09:45:32 -07001958 if (nr > AIO_PLUG_THRESHOLD)
1959 blk_finish_plug(&plug);
Al Viro67ba0492018-05-26 20:10:07 -04001960
1961 percpu_ref_put(&ctx->users);
1962 return i ? i : ret;
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001963}
1964
Al Viroc00d2c72016-12-20 07:04:57 -05001965#ifdef CONFIG_COMPAT
Al Viroc00d2c72016-12-20 07:04:57 -05001966COMPAT_SYSCALL_DEFINE3(io_submit, compat_aio_context_t, ctx_id,
Al Viro67ba0492018-05-26 20:10:07 -04001967 int, nr, compat_uptr_t __user *, iocbpp)
Al Viroc00d2c72016-12-20 07:04:57 -05001968{
Al Viro67ba0492018-05-26 20:10:07 -04001969 struct kioctx *ctx;
1970 long ret = 0;
1971 int i = 0;
1972 struct blk_plug plug;
Al Viroc00d2c72016-12-20 07:04:57 -05001973
1974 if (unlikely(nr < 0))
1975 return -EINVAL;
1976
Al Viro67ba0492018-05-26 20:10:07 -04001977 ctx = lookup_ioctx(ctx_id);
1978 if (unlikely(!ctx)) {
1979 pr_debug("EINVAL: invalid context id\n");
1980 return -EINVAL;
1981 }
1982
Al Viro1da92772018-05-26 20:10:07 -04001983 if (nr > ctx->nr_events)
1984 nr = ctx->nr_events;
1985
Jens Axboea79d40e2018-12-04 09:45:32 -07001986 if (nr > AIO_PLUG_THRESHOLD)
1987 blk_start_plug(&plug);
Al Viro67ba0492018-05-26 20:10:07 -04001988 for (i = 0; i < nr; i++) {
1989 compat_uptr_t user_iocb;
1990
1991 if (unlikely(get_user(user_iocb, iocbpp + i))) {
1992 ret = -EFAULT;
1993 break;
1994 }
1995
1996 ret = io_submit_one(ctx, compat_ptr(user_iocb), true);
1997 if (ret)
1998 break;
1999 }
Jens Axboea79d40e2018-12-04 09:45:32 -07002000 if (nr > AIO_PLUG_THRESHOLD)
2001 blk_finish_plug(&plug);
Al Viro67ba0492018-05-26 20:10:07 -04002002
2003 percpu_ref_put(&ctx->users);
2004 return i ? i : ret;
Al Viroc00d2c72016-12-20 07:04:57 -05002005}
2006#endif
2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008/* lookup_kiocb
2009 * Finds a given iocb for cancellation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 */
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01002011static struct aio_kiocb *
Christoph Hellwigf3a27522018-03-30 11:19:25 +02002012lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01002014 struct aio_kiocb *kiocb;
Zach Brownd00689a2005-11-13 16:07:34 -08002015
2016 assert_spin_locked(&ctx->ctx_lock);
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 /* TODO: use a hash or array, this sucks. */
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01002019 list_for_each_entry(kiocb, &ctx->active_reqs, ki_list) {
2020 if (kiocb->ki_user_iocb == iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return kiocb;
2022 }
2023 return NULL;
2024}
2025
2026/* sys_io_cancel:
2027 * Attempts to cancel an iocb previously passed to io_submit. If
2028 * the operation is successfully cancelled, the resulting event is
2029 * copied into the memory pointed to by result without being placed
2030 * into the completion queue and 0 is returned. May fail with
2031 * -EFAULT if any of the data structures pointed to are invalid.
2032 * May fail with -EINVAL if aio_context specified by ctx_id is
2033 * invalid. May fail with -EAGAIN if the iocb specified was not
2034 * cancelled. Will fail with -ENOSYS if not implemented.
2035 */
Heiko Carstens002c8972009-01-14 14:14:18 +01002036SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
2037 struct io_event __user *, result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 struct kioctx *ctx;
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01002040 struct aio_kiocb *kiocb;
Christoph Hellwig888933f2018-05-23 14:11:02 +02002041 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 u32 key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Christoph Hellwigf3a27522018-03-30 11:19:25 +02002044 if (unlikely(get_user(key, &iocb->aio_key)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 return -EFAULT;
Christoph Hellwigf3a27522018-03-30 11:19:25 +02002046 if (unlikely(key != KIOCB_KEY))
2047 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 ctx = lookup_ioctx(ctx_id);
2050 if (unlikely(!ctx))
2051 return -EINVAL;
2052
2053 spin_lock_irq(&ctx->ctx_lock);
Christoph Hellwigf3a27522018-03-30 11:19:25 +02002054 kiocb = lookup_kiocb(ctx, iocb);
Christoph Hellwig888933f2018-05-23 14:11:02 +02002055 if (kiocb) {
2056 ret = kiocb->ki_cancel(&kiocb->rw);
2057 list_del_init(&kiocb->ki_list);
2058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 spin_unlock_irq(&ctx->ctx_lock);
2060
Kent Overstreet906b9732013-05-07 16:18:31 -07002061 if (!ret) {
Kent Overstreetbec68faa2013-05-13 14:45:08 -07002062 /*
2063 * The result argument is no longer used - the io_event is
2064 * always delivered via the ring buffer. -EINPROGRESS indicates
2065 * cancellation is progress:
Kent Overstreet906b9732013-05-07 16:18:31 -07002066 */
Kent Overstreetbec68faa2013-05-13 14:45:08 -07002067 ret = -EINPROGRESS;
Kent Overstreet906b9732013-05-07 16:18:31 -07002068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Kent Overstreet723be6e2013-05-28 15:14:48 -07002070 percpu_ref_put(&ctx->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 return ret;
2073}
2074
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07002075static long do_io_getevents(aio_context_t ctx_id,
2076 long min_nr,
2077 long nr,
2078 struct io_event __user *events,
2079 struct timespec64 *ts)
2080{
2081 ktime_t until = ts ? timespec64_to_ktime(*ts) : KTIME_MAX;
2082 struct kioctx *ioctx = lookup_ioctx(ctx_id);
2083 long ret = -EINVAL;
2084
2085 if (likely(ioctx)) {
2086 if (likely(min_nr <= nr && min_nr >= 0))
2087 ret = read_events(ioctx, min_nr, nr, events, until);
2088 percpu_ref_put(&ioctx->users);
2089 }
2090
2091 return ret;
2092}
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094/* io_getevents:
2095 * Attempts to read at least min_nr events and up to nr events from
Satoru Takeuchi642b5122010-08-05 11:23:11 -07002096 * the completion queue for the aio_context specified by ctx_id. If
2097 * it succeeds, the number of read events is returned. May fail with
2098 * -EINVAL if ctx_id is invalid, if min_nr is out of range, if nr is
2099 * out of range, if timeout is out of range. May fail with -EFAULT
2100 * if any of the memory specified is invalid. May return 0 or
2101 * < min_nr if the timeout specified by timeout has elapsed
2102 * before sufficient events are available, where timeout == NULL
2103 * specifies an infinite timeout. Note that the timeout pointed to by
Jeff Moyer69008072013-05-24 15:55:24 -07002104 * timeout is relative. Will fail with -ENOSYS if not implemented.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 */
Deepa Dinamani7a353972018-09-19 21:41:08 -07002106#if !defined(CONFIG_64BIT_TIME) || defined(CONFIG_64BIT)
2107
Heiko Carstens002c8972009-01-14 14:14:18 +01002108SYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id,
2109 long, min_nr,
2110 long, nr,
2111 struct io_event __user *, events,
Deepa Dinamani7a353972018-09-19 21:41:08 -07002112 struct __kernel_timespec __user *, timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07002114 struct timespec64 ts;
Christoph Hellwig7a074e92018-05-02 19:51:00 +02002115 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Christoph Hellwig7a074e92018-05-02 19:51:00 +02002117 if (timeout && unlikely(get_timespec64(&ts, timeout)))
2118 return -EFAULT;
2119
2120 ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &ts : NULL);
2121 if (!ret && signal_pending(current))
2122 ret = -EINTR;
2123 return ret;
2124}
2125
Deepa Dinamani7a353972018-09-19 21:41:08 -07002126#endif
2127
Christoph Hellwig9ba546c2018-07-11 15:48:46 +02002128struct __aio_sigset {
2129 const sigset_t __user *sigmask;
2130 size_t sigsetsize;
2131};
2132
Christoph Hellwig7a074e92018-05-02 19:51:00 +02002133SYSCALL_DEFINE6(io_pgetevents,
2134 aio_context_t, ctx_id,
2135 long, min_nr,
2136 long, nr,
2137 struct io_event __user *, events,
Deepa Dinamani7a353972018-09-19 21:41:08 -07002138 struct __kernel_timespec __user *, timeout,
Christoph Hellwig7a074e92018-05-02 19:51:00 +02002139 const struct __aio_sigset __user *, usig)
2140{
2141 struct __aio_sigset ksig = { NULL, };
2142 sigset_t ksigmask, sigsaved;
2143 struct timespec64 ts;
2144 int ret;
2145
2146 if (timeout && unlikely(get_timespec64(&ts, timeout)))
2147 return -EFAULT;
2148
2149 if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
2150 return -EFAULT;
2151
Deepa Dinamani7a353972018-09-19 21:41:08 -07002152 ret = set_user_sigmask(ksig.sigmask, &ksigmask, &sigsaved, ksig.sigsetsize);
2153 if (ret)
2154 return ret;
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07002155
Christoph Hellwig7a074e92018-05-02 19:51:00 +02002156 ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &ts : NULL);
Deepa Dinamani7a353972018-09-19 21:41:08 -07002157 restore_user_sigmask(ksig.sigmask, &sigsaved);
2158 if (signal_pending(current) && !ret)
2159 ret = -ERESTARTNOHAND;
Christoph Hellwig7a074e92018-05-02 19:51:00 +02002160
2161 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
Al Viroc00d2c72016-12-20 07:04:57 -05002163
Deepa Dinamani7a353972018-09-19 21:41:08 -07002164#if defined(CONFIG_COMPAT_32BIT_TIME) && !defined(CONFIG_64BIT)
2165
2166SYSCALL_DEFINE6(io_pgetevents_time32,
2167 aio_context_t, ctx_id,
2168 long, min_nr,
2169 long, nr,
2170 struct io_event __user *, events,
2171 struct old_timespec32 __user *, timeout,
2172 const struct __aio_sigset __user *, usig)
2173{
2174 struct __aio_sigset ksig = { NULL, };
2175 sigset_t ksigmask, sigsaved;
2176 struct timespec64 ts;
2177 int ret;
2178
2179 if (timeout && unlikely(get_old_timespec32(&ts, timeout)))
2180 return -EFAULT;
2181
2182 if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
2183 return -EFAULT;
2184
Deepa Dinamanided653c2018-09-19 21:41:04 -07002185
2186 ret = set_user_sigmask(ksig.sigmask, &ksigmask, &sigsaved, ksig.sigsetsize);
2187 if (ret)
2188 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &ts : NULL);
Deepa Dinamani854a6ed2018-09-19 21:41:05 -07002191 restore_user_sigmask(ksig.sigmask, &sigsaved);
2192 if (signal_pending(current) && !ret)
2193 ret = -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 return ret;
2196}
Al Viroc00d2c72016-12-20 07:04:57 -05002197
Deepa Dinamani7a353972018-09-19 21:41:08 -07002198#endif
2199
2200#if defined(CONFIG_COMPAT_32BIT_TIME)
2201
Al Viroc00d2c72016-12-20 07:04:57 -05002202COMPAT_SYSCALL_DEFINE5(io_getevents, compat_aio_context_t, ctx_id,
2203 compat_long_t, min_nr,
2204 compat_long_t, nr,
2205 struct io_event __user *, events,
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +02002206 struct old_timespec32 __user *, timeout)
Al Viroc00d2c72016-12-20 07:04:57 -05002207{
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07002208 struct timespec64 t;
Christoph Hellwig7a074e92018-05-02 19:51:00 +02002209 int ret;
Al Viroc00d2c72016-12-20 07:04:57 -05002210
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +02002211 if (timeout && get_old_timespec32(&t, timeout))
Christoph Hellwig7a074e92018-05-02 19:51:00 +02002212 return -EFAULT;
2213
2214 ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &t : NULL);
2215 if (!ret && signal_pending(current))
2216 ret = -EINTR;
2217 return ret;
2218}
2219
Deepa Dinamani7a353972018-09-19 21:41:08 -07002220#endif
2221
2222#ifdef CONFIG_COMPAT
Christoph Hellwig7a074e92018-05-02 19:51:00 +02002223
2224struct __compat_aio_sigset {
2225 compat_sigset_t __user *sigmask;
2226 compat_size_t sigsetsize;
2227};
2228
Deepa Dinamani7a353972018-09-19 21:41:08 -07002229#if defined(CONFIG_COMPAT_32BIT_TIME)
2230
Christoph Hellwig7a074e92018-05-02 19:51:00 +02002231COMPAT_SYSCALL_DEFINE6(io_pgetevents,
2232 compat_aio_context_t, ctx_id,
2233 compat_long_t, min_nr,
2234 compat_long_t, nr,
2235 struct io_event __user *, events,
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +02002236 struct old_timespec32 __user *, timeout,
Christoph Hellwig7a074e92018-05-02 19:51:00 +02002237 const struct __compat_aio_sigset __user *, usig)
2238{
2239 struct __compat_aio_sigset ksig = { NULL, };
2240 sigset_t ksigmask, sigsaved;
2241 struct timespec64 t;
2242 int ret;
2243
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +02002244 if (timeout && get_old_timespec32(&t, timeout))
Christoph Hellwig7a074e92018-05-02 19:51:00 +02002245 return -EFAULT;
2246
2247 if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
2248 return -EFAULT;
2249
Deepa Dinamanided653c2018-09-19 21:41:04 -07002250 ret = set_compat_user_sigmask(ksig.sigmask, &ksigmask, &sigsaved, ksig.sigsetsize);
2251 if (ret)
2252 return ret;
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07002253
Christoph Hellwig7a074e92018-05-02 19:51:00 +02002254 ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &t : NULL);
Deepa Dinamani854a6ed2018-09-19 21:41:05 -07002255 restore_user_sigmask(ksig.sigmask, &sigsaved);
2256 if (signal_pending(current) && !ret)
2257 ret = -ERESTARTNOHAND;
Christoph Hellwig7a074e92018-05-02 19:51:00 +02002258
2259 return ret;
Al Viroc00d2c72016-12-20 07:04:57 -05002260}
Deepa Dinamani7a353972018-09-19 21:41:08 -07002261
2262#endif
2263
2264COMPAT_SYSCALL_DEFINE6(io_pgetevents_time64,
2265 compat_aio_context_t, ctx_id,
2266 compat_long_t, min_nr,
2267 compat_long_t, nr,
2268 struct io_event __user *, events,
2269 struct __kernel_timespec __user *, timeout,
2270 const struct __compat_aio_sigset __user *, usig)
2271{
2272 struct __compat_aio_sigset ksig = { NULL, };
2273 sigset_t ksigmask, sigsaved;
2274 struct timespec64 t;
2275 int ret;
2276
2277 if (timeout && get_timespec64(&t, timeout))
2278 return -EFAULT;
2279
2280 if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
2281 return -EFAULT;
2282
2283 ret = set_compat_user_sigmask(ksig.sigmask, &ksigmask, &sigsaved, ksig.sigsetsize);
2284 if (ret)
2285 return ret;
2286
2287 ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &t : NULL);
2288 restore_user_sigmask(ksig.sigmask, &sigsaved);
2289 if (signal_pending(current) && !ret)
2290 ret = -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 return ret;
2293}
2294#endif