blob: 27454594e37a1e1ca6a7ad25524090954d50a1de [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * An async IO implementation for Linux
3 * Written by Benjamin LaHaise <bcrl@kvack.org>
4 *
5 * Implements an efficient asynchronous io interface.
6 *
7 * Copyright 2000, 2001, 2002 Red Hat, Inc. All Rights Reserved.
8 *
9 * See ../COPYING for licensing terms.
10 */
Kent Overstreetcaf41672013-05-07 16:18:35 -070011#define pr_fmt(fmt) "%s: " fmt, __func__
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/errno.h>
16#include <linux/time.h>
17#include <linux/aio_abi.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050018#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/syscalls.h>
Jens Axboeb9d128f2009-10-29 13:59:26 +010020#include <linux/backing-dev.h>
Badari Pulavarty027445c2006-09-30 23:28:46 -070021#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Ingo Molnar174cd4b2017-02-02 19:15:33 +010023#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/fs.h>
25#include <linux/file.h>
26#include <linux/mm.h>
27#include <linux/mman.h>
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070028#include <linux/mmu_context.h>
Kent Overstreete1bdd5f2013-04-26 10:58:39 +100029#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/slab.h>
31#include <linux/timer.h>
32#include <linux/aio.h>
33#include <linux/highmem.h>
34#include <linux/workqueue.h>
35#include <linux/security.h>
Davide Libenzi9c3060b2007-05-10 22:23:21 -070036#include <linux/eventfd.h>
Jeff Moyercfb1e332009-10-02 18:57:36 -040037#include <linux/blkdev.h>
Jeff Moyer9d85cba72010-05-26 14:44:26 -070038#include <linux/compat.h>
Gu Zheng36bc08c2013-07-16 17:56:16 +080039#include <linux/migrate.h>
40#include <linux/ramfs.h>
Kent Overstreet723be6e2013-05-28 15:14:48 -070041#include <linux/percpu-refcount.h>
Benjamin LaHaise71ad7492013-09-17 10:18:25 -040042#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <asm/kmap_types.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080045#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Al Viro68d70d02013-06-19 15:26:04 +040047#include "internal.h"
48
Christoph Hellwigf3a27522018-03-30 11:19:25 +020049#define KIOCB_KEY 0
50
Kent Overstreet4e179bc2013-05-07 16:18:33 -070051#define AIO_RING_MAGIC 0xa10a10a1
52#define AIO_RING_COMPAT_FEATURES 1
53#define AIO_RING_INCOMPAT_FEATURES 0
54struct aio_ring {
55 unsigned id; /* kernel internal index number */
56 unsigned nr; /* number of io_events */
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -040057 unsigned head; /* Written to by userland or under ring_lock
58 * mutex by aio_read_events_ring(). */
Kent Overstreet4e179bc2013-05-07 16:18:33 -070059 unsigned tail;
60
61 unsigned magic;
62 unsigned compat_features;
63 unsigned incompat_features;
64 unsigned header_length; /* size of aio_ring */
65
66
67 struct io_event io_events[0];
68}; /* 128 bytes + ring size */
69
70#define AIO_RING_PAGES 8
Kent Overstreet4e179bc2013-05-07 16:18:33 -070071
Benjamin LaHaisedb446a02013-07-30 12:54:40 -040072struct kioctx_table {
Tejun Heod0264c02018-03-14 12:10:17 -070073 struct rcu_head rcu;
74 unsigned nr;
75 struct kioctx __rcu *table[];
Benjamin LaHaisedb446a02013-07-30 12:54:40 -040076};
77
Kent Overstreete1bdd5f2013-04-26 10:58:39 +100078struct kioctx_cpu {
79 unsigned reqs_available;
80};
81
Jens Axboedc48e562015-04-15 11:17:23 -060082struct ctx_rq_wait {
83 struct completion comp;
84 atomic_t count;
85};
86
Kent Overstreet4e179bc2013-05-07 16:18:33 -070087struct kioctx {
Kent Overstreet723be6e2013-05-28 15:14:48 -070088 struct percpu_ref users;
Kent Overstreet36f55882013-05-07 16:18:41 -070089 atomic_t dead;
Kent Overstreet4e179bc2013-05-07 16:18:33 -070090
Kent Overstreete34ecee2013-10-10 19:31:47 -070091 struct percpu_ref reqs;
92
Kent Overstreet4e179bc2013-05-07 16:18:33 -070093 unsigned long user_id;
Kent Overstreet4e179bc2013-05-07 16:18:33 -070094
Kent Overstreete1bdd5f2013-04-26 10:58:39 +100095 struct __percpu kioctx_cpu *cpu;
96
97 /*
98 * For percpu reqs_available, number of slots we move to/from global
99 * counter at a time:
100 */
101 unsigned req_batch;
Kent Overstreet3e845ce2013-05-07 16:18:51 -0700102 /*
103 * This is what userspace passed to io_setup(), it's not used for
104 * anything but counting against the global max_reqs quota.
105 *
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700106 * The real limit is nr_events - 1, which will be larger (see
Kent Overstreet3e845ce2013-05-07 16:18:51 -0700107 * aio_setup_ring())
108 */
Kent Overstreet4e179bc2013-05-07 16:18:33 -0700109 unsigned max_reqs;
110
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700111 /* Size of ringbuffer, in units of struct io_event */
112 unsigned nr_events;
Kent Overstreet4e179bc2013-05-07 16:18:33 -0700113
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700114 unsigned long mmap_base;
115 unsigned long mmap_size;
116
117 struct page **ring_pages;
118 long nr_pages;
119
Tejun Heof7298632018-03-14 12:45:15 -0700120 struct rcu_work free_rwork; /* see free_ioctx() */
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700121
Anatol Pomozove02ba722014-04-15 11:31:33 -0700122 /*
123 * signals when all in-flight requests are done
124 */
Jens Axboedc48e562015-04-15 11:17:23 -0600125 struct ctx_rq_wait *rq_wait;
Anatol Pomozove02ba722014-04-15 11:31:33 -0700126
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700127 struct {
Kent Overstreet34e83fc2013-04-26 10:58:39 +1000128 /*
129 * This counts the number of available slots in the ringbuffer,
130 * so we avoid overflowing it: it's decremented (if positive)
131 * when allocating a kiocb and incremented when the resulting
132 * io_event is pulled off the ringbuffer.
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000133 *
134 * We batch accesses to it with a percpu version.
Kent Overstreet34e83fc2013-04-26 10:58:39 +1000135 */
136 atomic_t reqs_available;
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700137 } ____cacheline_aligned_in_smp;
138
139 struct {
140 spinlock_t ctx_lock;
141 struct list_head active_reqs; /* used for cancellation */
142 } ____cacheline_aligned_in_smp;
143
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700144 struct {
145 struct mutex ring_lock;
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700146 wait_queue_head_t wait;
147 } ____cacheline_aligned_in_smp;
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700148
149 struct {
150 unsigned tail;
Benjamin LaHaised856f322014-08-24 13:14:05 -0400151 unsigned completed_events;
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700152 spinlock_t completion_lock;
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700153 } ____cacheline_aligned_in_smp;
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700154
155 struct page *internal_pages[AIO_RING_PAGES];
Gu Zheng36bc08c2013-07-16 17:56:16 +0800156 struct file *aio_ring_file;
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400157
158 unsigned id;
Kent Overstreet4e179bc2013-05-07 16:18:33 -0700159};
160
Christoph Hellwiga3c0d432018-03-27 19:18:57 +0200161struct fsync_iocb {
162 struct work_struct work;
163 struct file *file;
164 bool datasync;
165};
166
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100167struct aio_kiocb {
Christoph Hellwig54843f82018-05-02 19:57:21 +0200168 union {
169 struct kiocb rw;
Christoph Hellwiga3c0d432018-03-27 19:18:57 +0200170 struct fsync_iocb fsync;
Christoph Hellwig54843f82018-05-02 19:57:21 +0200171 };
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100172
173 struct kioctx *ki_ctx;
174 kiocb_cancel_fn *ki_cancel;
175
176 struct iocb __user *ki_user_iocb; /* user's aiocb */
177 __u64 ki_user_data; /* user's data for completion */
178
179 struct list_head ki_list; /* the aio core uses this
180 * for cancellation */
181
182 /*
183 * If the aio_resfd field of the userspace iocb is not zero,
184 * this is the underlying eventfd context to deliver events to.
185 */
186 struct eventfd_ctx *ki_eventfd;
187};
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*------ sysctl variables----*/
Zach Brownd55b5fd2005-11-07 00:59:31 -0800190static DEFINE_SPINLOCK(aio_nr_lock);
191unsigned long aio_nr; /* current system wide number of aio requests */
192unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/*----end sysctl variables---*/
194
Christoph Lametere18b8902006-12-06 20:33:20 -0800195static struct kmem_cache *kiocb_cachep;
196static struct kmem_cache *kioctx_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400198static struct vfsmount *aio_mnt;
199
200static const struct file_operations aio_ring_fops;
201static const struct address_space_operations aio_ctx_aops;
202
203static struct file *aio_private_file(struct kioctx *ctx, loff_t nr_pages)
204{
205 struct qstr this = QSTR_INIT("[aio]", 5);
206 struct file *file;
207 struct path path;
208 struct inode *inode = alloc_anon_inode(aio_mnt->mnt_sb);
Dan Carpenter7f626562013-11-13 10:49:40 +0300209 if (IS_ERR(inode))
210 return ERR_CAST(inode);
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400211
212 inode->i_mapping->a_ops = &aio_ctx_aops;
213 inode->i_mapping->private_data = ctx;
214 inode->i_size = PAGE_SIZE * nr_pages;
215
216 path.dentry = d_alloc_pseudo(aio_mnt->mnt_sb, &this);
217 if (!path.dentry) {
218 iput(inode);
219 return ERR_PTR(-ENOMEM);
220 }
221 path.mnt = mntget(aio_mnt);
222
223 d_instantiate(path.dentry, inode);
224 file = alloc_file(&path, FMODE_READ | FMODE_WRITE, &aio_ring_fops);
225 if (IS_ERR(file)) {
226 path_put(&path);
227 return file;
228 }
229
230 file->f_flags = O_RDWR;
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400231 return file;
232}
233
234static struct dentry *aio_mount(struct file_system_type *fs_type,
235 int flags, const char *dev_name, void *data)
236{
237 static const struct dentry_operations ops = {
238 .d_dname = simple_dname,
239 };
Jann Horn22f6b4d2016-09-16 00:31:22 +0200240 struct dentry *root = mount_pseudo(fs_type, "aio:", NULL, &ops,
241 AIO_RING_MAGIC);
242
243 if (!IS_ERR(root))
244 root->d_sb->s_iflags |= SB_I_NOEXEC;
245 return root;
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/* aio_setup
249 * Creates the slab caches used by the aio routines, panic on
250 * failure as this is done early during the boot sequence.
251 */
252static int __init aio_setup(void)
253{
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400254 static struct file_system_type aio_fs = {
255 .name = "aio",
256 .mount = aio_mount,
257 .kill_sb = kill_anon_super,
258 };
259 aio_mnt = kern_mount(&aio_fs);
260 if (IS_ERR(aio_mnt))
261 panic("Failed to create aio fs mount.");
262
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100263 kiocb_cachep = KMEM_CACHE(aio_kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
Christoph Lameter0a31bd52007-05-06 14:49:57 -0700264 kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return 0;
266}
H Hartley Sweeten385773e2009-09-22 16:43:53 -0700267__initcall(aio_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400269static void put_aio_ring_file(struct kioctx *ctx)
270{
271 struct file *aio_ring_file = ctx->aio_ring_file;
Rasmus Villemoesde04e762016-09-15 00:25:03 +0200272 struct address_space *i_mapping;
273
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400274 if (aio_ring_file) {
Al Viro45063092016-12-04 18:24:56 -0500275 truncate_setsize(file_inode(aio_ring_file), 0);
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400276
277 /* Prevent further access to the kioctx from migratepages */
Al Viro45063092016-12-04 18:24:56 -0500278 i_mapping = aio_ring_file->f_mapping;
Rasmus Villemoesde04e762016-09-15 00:25:03 +0200279 spin_lock(&i_mapping->private_lock);
280 i_mapping->private_data = NULL;
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400281 ctx->aio_ring_file = NULL;
Rasmus Villemoesde04e762016-09-15 00:25:03 +0200282 spin_unlock(&i_mapping->private_lock);
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400283
284 fput(aio_ring_file);
285 }
286}
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288static void aio_free_ring(struct kioctx *ctx)
289{
Gu Zheng36bc08c2013-07-16 17:56:16 +0800290 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400292 /* Disconnect the kiotx from the ring file. This prevents future
293 * accesses to the kioctx from page migration.
294 */
295 put_aio_ring_file(ctx);
296
Gu Zheng36bc08c2013-07-16 17:56:16 +0800297 for (i = 0; i < ctx->nr_pages; i++) {
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500298 struct page *page;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800299 pr_debug("pid(%d) [%d] page->count=%d\n", current->pid, i,
300 page_count(ctx->ring_pages[i]));
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500301 page = ctx->ring_pages[i];
302 if (!page)
303 continue;
304 ctx->ring_pages[i] = NULL;
305 put_page(page);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Sasha Levinddb8c452013-11-19 17:33:03 -0500308 if (ctx->ring_pages && ctx->ring_pages != ctx->internal_pages) {
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700309 kfree(ctx->ring_pages);
Sasha Levinddb8c452013-11-19 17:33:03 -0500310 ctx->ring_pages = NULL;
311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
Oleg Nesterov5477e702015-09-04 15:48:04 -0700314static int aio_ring_mremap(struct vm_area_struct *vma)
Gu Zheng36bc08c2013-07-16 17:56:16 +0800315{
Oleg Nesterov5477e702015-09-04 15:48:04 -0700316 struct file *file = vma->vm_file;
Pavel Emelyanove4a0d3e2014-09-18 19:56:17 +0400317 struct mm_struct *mm = vma->vm_mm;
318 struct kioctx_table *table;
Al Virob2edffd2015-04-06 17:48:54 -0400319 int i, res = -EINVAL;
Pavel Emelyanove4a0d3e2014-09-18 19:56:17 +0400320
321 spin_lock(&mm->ioctx_lock);
322 rcu_read_lock();
323 table = rcu_dereference(mm->ioctx_table);
324 for (i = 0; i < table->nr; i++) {
325 struct kioctx *ctx;
326
Tejun Heod0264c02018-03-14 12:10:17 -0700327 ctx = rcu_dereference(table->table[i]);
Pavel Emelyanove4a0d3e2014-09-18 19:56:17 +0400328 if (ctx && ctx->aio_ring_file == file) {
Al Virob2edffd2015-04-06 17:48:54 -0400329 if (!atomic_read(&ctx->dead)) {
330 ctx->user_id = ctx->mmap_base = vma->vm_start;
331 res = 0;
332 }
Pavel Emelyanove4a0d3e2014-09-18 19:56:17 +0400333 break;
334 }
335 }
336
337 rcu_read_unlock();
338 spin_unlock(&mm->ioctx_lock);
Al Virob2edffd2015-04-06 17:48:54 -0400339 return res;
Pavel Emelyanove4a0d3e2014-09-18 19:56:17 +0400340}
341
Oleg Nesterov5477e702015-09-04 15:48:04 -0700342static const struct vm_operations_struct aio_ring_vm_ops = {
343 .mremap = aio_ring_mremap,
344#if IS_ENABLED(CONFIG_MMU)
345 .fault = filemap_fault,
346 .map_pages = filemap_map_pages,
347 .page_mkwrite = filemap_page_mkwrite,
348#endif
349};
350
351static int aio_ring_mmap(struct file *file, struct vm_area_struct *vma)
352{
353 vma->vm_flags |= VM_DONTEXPAND;
354 vma->vm_ops = &aio_ring_vm_ops;
355 return 0;
356}
357
Gu Zheng36bc08c2013-07-16 17:56:16 +0800358static const struct file_operations aio_ring_fops = {
359 .mmap = aio_ring_mmap,
360};
361
Benjamin LaHaise0c453552013-07-17 09:34:24 -0400362#if IS_ENABLED(CONFIG_MIGRATION)
Gu Zheng36bc08c2013-07-16 17:56:16 +0800363static int aio_migratepage(struct address_space *mapping, struct page *new,
364 struct page *old, enum migrate_mode mode)
365{
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400366 struct kioctx *ctx;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800367 unsigned long flags;
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400368 pgoff_t idx;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800369 int rc;
370
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700371 /*
372 * We cannot support the _NO_COPY case here, because copy needs to
373 * happen under the ctx->completion_lock. That does not work with the
374 * migration workflow of MIGRATE_SYNC_NO_COPY.
375 */
376 if (mode == MIGRATE_SYNC_NO_COPY)
377 return -EINVAL;
378
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500379 rc = 0;
380
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400381 /* mapping->private_lock here protects against the kioctx teardown. */
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500382 spin_lock(&mapping->private_lock);
383 ctx = mapping->private_data;
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400384 if (!ctx) {
385 rc = -EINVAL;
386 goto out;
387 }
388
389 /* The ring_lock mutex. The prevents aio_read_events() from writing
390 * to the ring's head, and prevents page migration from mucking in
391 * a partially initialized kiotx.
392 */
393 if (!mutex_trylock(&ctx->ring_lock)) {
394 rc = -EAGAIN;
395 goto out;
396 }
397
398 idx = old->index;
399 if (idx < (pgoff_t)ctx->nr_pages) {
400 /* Make sure the old page hasn't already been changed */
401 if (ctx->ring_pages[idx] != old)
402 rc = -EAGAIN;
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500403 } else
404 rc = -EINVAL;
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500405
406 if (rc != 0)
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400407 goto out_unlock;
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500408
Gu Zheng36bc08c2013-07-16 17:56:16 +0800409 /* Writeback must be complete */
410 BUG_ON(PageWriteback(old));
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500411 get_page(new);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800412
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500413 rc = migrate_page_move_mapping(mapping, new, old, NULL, mode, 1);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800414 if (rc != MIGRATEPAGE_SUCCESS) {
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500415 put_page(new);
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400416 goto out_unlock;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800417 }
418
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400419 /* Take completion_lock to prevent other writes to the ring buffer
420 * while the old page is copied to the new. This prevents new
421 * events from being lost.
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400422 */
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400423 spin_lock_irqsave(&ctx->completion_lock, flags);
424 migrate_page_copy(new, old);
425 BUG_ON(ctx->ring_pages[idx] != old);
426 ctx->ring_pages[idx] = new;
427 spin_unlock_irqrestore(&ctx->completion_lock, flags);
428
429 /* The old page is no longer accessible. */
430 put_page(old);
431
432out_unlock:
433 mutex_unlock(&ctx->ring_lock);
434out:
Benjamin LaHaise5e9ae2e2013-09-26 20:34:51 -0400435 spin_unlock(&mapping->private_lock);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800436 return rc;
437}
Benjamin LaHaise0c453552013-07-17 09:34:24 -0400438#endif
Gu Zheng36bc08c2013-07-16 17:56:16 +0800439
440static const struct address_space_operations aio_ctx_aops = {
Gu Zheng835f2522014-11-06 17:46:21 +0800441 .set_page_dirty = __set_page_dirty_no_writeback,
Benjamin LaHaise0c453552013-07-17 09:34:24 -0400442#if IS_ENABLED(CONFIG_MIGRATION)
Gu Zheng36bc08c2013-07-16 17:56:16 +0800443 .migratepage = aio_migratepage,
Benjamin LaHaise0c453552013-07-17 09:34:24 -0400444#endif
Gu Zheng36bc08c2013-07-16 17:56:16 +0800445};
446
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300447static int aio_setup_ring(struct kioctx *ctx, unsigned int nr_events)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 struct aio_ring *ring;
Zach Brown41003a72013-05-07 16:18:25 -0700450 struct mm_struct *mm = current->mm;
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900451 unsigned long size, unused;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 int nr_pages;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800453 int i;
454 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 /* Compensate for the ring buffer's head/tail overlap entry */
457 nr_events += 2; /* 1 is required, 2 for good luck */
458
459 size = sizeof(struct aio_ring);
460 size += sizeof(struct io_event) * nr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Gu Zheng36bc08c2013-07-16 17:56:16 +0800462 nr_pages = PFN_UP(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (nr_pages < 0)
464 return -EINVAL;
465
Benjamin LaHaise71ad7492013-09-17 10:18:25 -0400466 file = aio_private_file(ctx, nr_pages);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800467 if (IS_ERR(file)) {
468 ctx->aio_ring_file = NULL;
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400469 return -ENOMEM;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Gu Zheng36bc08c2013-07-16 17:56:16 +0800472 ctx->aio_ring_file = file;
473 nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring))
474 / sizeof(struct io_event);
475
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700476 ctx->ring_pages = ctx->internal_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (nr_pages > AIO_RING_PAGES) {
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700478 ctx->ring_pages = kcalloc(nr_pages, sizeof(struct page *),
479 GFP_KERNEL);
Gu Zhengd1b94322013-12-04 18:19:06 +0800480 if (!ctx->ring_pages) {
481 put_aio_ring_file(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return -ENOMEM;
Gu Zhengd1b94322013-12-04 18:19:06 +0800483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900486 for (i = 0; i < nr_pages; i++) {
487 struct page *page;
Al Viro45063092016-12-04 18:24:56 -0500488 page = find_or_create_page(file->f_mapping,
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900489 i, GFP_HIGHUSER | __GFP_ZERO);
490 if (!page)
491 break;
492 pr_debug("pid(%d) page[%d]->count=%d\n",
493 current->pid, i, page_count(page));
494 SetPageUptodate(page);
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900495 unlock_page(page);
496
497 ctx->ring_pages[i] = page;
498 }
499 ctx->nr_pages = i;
500
501 if (unlikely(i != nr_pages)) {
502 aio_free_ring(ctx);
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400503 return -ENOMEM;
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900504 }
505
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700506 ctx->mmap_size = nr_pages * PAGE_SIZE;
507 pr_debug("attempting mmap of %lu bytes\n", ctx->mmap_size);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800508
Michal Hocko013373e2016-05-23 16:25:59 -0700509 if (down_write_killable(&mm->mmap_sem)) {
510 ctx->mmap_size = 0;
511 aio_free_ring(ctx);
512 return -EINTR;
513 }
514
Gu Zheng36bc08c2013-07-16 17:56:16 +0800515 ctx->mmap_base = do_mmap_pgoff(ctx->aio_ring_file, 0, ctx->mmap_size,
516 PROT_READ | PROT_WRITE,
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800517 MAP_SHARED, 0, &unused, NULL);
Linus Torvalds3dc9acb2013-12-20 05:11:12 +0900518 up_write(&mm->mmap_sem);
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700519 if (IS_ERR((void *)ctx->mmap_base)) {
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700520 ctx->mmap_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 aio_free_ring(ctx);
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400522 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700525 pr_debug("mmap address: 0x%08lx\n", ctx->mmap_base);
Benjamin LaHaised6c355c2013-09-09 11:57:59 -0400526
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700527 ctx->user_id = ctx->mmap_base;
528 ctx->nr_events = nr_events; /* trusted copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700530 ring = kmap_atomic(ctx->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 ring->nr = nr_events; /* user copy */
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400532 ring->id = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 ring->head = ring->tail = 0;
534 ring->magic = AIO_RING_MAGIC;
535 ring->compat_features = AIO_RING_COMPAT_FEATURES;
536 ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
537 ring->header_length = sizeof(struct aio_ring);
Cong Wange8e3c3d2011-11-25 23:14:27 +0800538 kunmap_atomic(ring);
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700539 flush_dcache_page(ctx->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 return 0;
542}
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544#define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
545#define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
546#define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
547
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100548void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel)
Kent Overstreet0460fef2013-05-07 16:18:49 -0700549{
Christoph Hellwig54843f82018-05-02 19:57:21 +0200550 struct aio_kiocb *req = container_of(iocb, struct aio_kiocb, rw);
Kent Overstreet0460fef2013-05-07 16:18:49 -0700551 struct kioctx *ctx = req->ki_ctx;
552 unsigned long flags;
553
Christoph Hellwig75321b52018-04-09 14:57:56 +0200554 if (WARN_ON_ONCE(!list_empty(&req->ki_list)))
555 return;
556
Kent Overstreet0460fef2013-05-07 16:18:49 -0700557 spin_lock_irqsave(&ctx->ctx_lock, flags);
Christoph Hellwig75321b52018-04-09 14:57:56 +0200558 list_add_tail(&req->ki_list, &ctx->active_reqs);
Kent Overstreet0460fef2013-05-07 16:18:49 -0700559 req->ki_cancel = cancel;
Kent Overstreet0460fef2013-05-07 16:18:49 -0700560 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
561}
562EXPORT_SYMBOL(kiocb_set_cancel_fn);
563
Tejun Heoa6d7cff2018-03-14 12:10:17 -0700564/*
565 * free_ioctx() should be RCU delayed to synchronize against the RCU
566 * protected lookup_ioctx() and also needs process context to call
Tejun Heof7298632018-03-14 12:45:15 -0700567 * aio_free_ring(). Use rcu_work.
Tejun Heoa6d7cff2018-03-14 12:10:17 -0700568 */
Kent Overstreete34ecee2013-10-10 19:31:47 -0700569static void free_ioctx(struct work_struct *work)
Kent Overstreet36f55882013-05-07 16:18:41 -0700570{
Tejun Heof7298632018-03-14 12:45:15 -0700571 struct kioctx *ctx = container_of(to_rcu_work(work), struct kioctx,
572 free_rwork);
Kent Overstreete34ecee2013-10-10 19:31:47 -0700573 pr_debug("freeing %p\n", ctx);
574
575 aio_free_ring(ctx);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000576 free_percpu(ctx->cpu);
Tejun Heo9a1049d2014-06-28 08:10:14 -0400577 percpu_ref_exit(&ctx->reqs);
578 percpu_ref_exit(&ctx->users);
Kent Overstreet36f55882013-05-07 16:18:41 -0700579 kmem_cache_free(kioctx_cachep, ctx);
580}
581
Kent Overstreete34ecee2013-10-10 19:31:47 -0700582static void free_ioctx_reqs(struct percpu_ref *ref)
583{
584 struct kioctx *ctx = container_of(ref, struct kioctx, reqs);
585
Anatol Pomozove02ba722014-04-15 11:31:33 -0700586 /* At this point we know that there are no any in-flight requests */
Jens Axboedc48e562015-04-15 11:17:23 -0600587 if (ctx->rq_wait && atomic_dec_and_test(&ctx->rq_wait->count))
588 complete(&ctx->rq_wait->comp);
Anatol Pomozove02ba722014-04-15 11:31:33 -0700589
Tejun Heoa6d7cff2018-03-14 12:10:17 -0700590 /* Synchronize against RCU protected table->table[] dereferences */
Tejun Heof7298632018-03-14 12:45:15 -0700591 INIT_RCU_WORK(&ctx->free_rwork, free_ioctx);
592 queue_rcu_work(system_wq, &ctx->free_rwork);
Kent Overstreete34ecee2013-10-10 19:31:47 -0700593}
594
Kent Overstreet36f55882013-05-07 16:18:41 -0700595/*
596 * When this function runs, the kioctx has been removed from the "hash table"
597 * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
598 * now it's safe to cancel any that need to be.
599 */
Kent Overstreete34ecee2013-10-10 19:31:47 -0700600static void free_ioctx_users(struct percpu_ref *ref)
Kent Overstreet36f55882013-05-07 16:18:41 -0700601{
Kent Overstreete34ecee2013-10-10 19:31:47 -0700602 struct kioctx *ctx = container_of(ref, struct kioctx, users);
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100603 struct aio_kiocb *req;
Kent Overstreet36f55882013-05-07 16:18:41 -0700604
605 spin_lock_irq(&ctx->ctx_lock);
606
607 while (!list_empty(&ctx->active_reqs)) {
608 req = list_first_entry(&ctx->active_reqs,
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100609 struct aio_kiocb, ki_list);
Christoph Hellwig888933f2018-05-23 14:11:02 +0200610 req->ki_cancel(&req->rw);
Al Viro4faa99962018-05-23 22:53:22 -0400611 list_del_init(&req->ki_list);
Kent Overstreet36f55882013-05-07 16:18:41 -0700612 }
613
614 spin_unlock_irq(&ctx->ctx_lock);
615
Kent Overstreete34ecee2013-10-10 19:31:47 -0700616 percpu_ref_kill(&ctx->reqs);
617 percpu_ref_put(&ctx->reqs);
Kent Overstreet36f55882013-05-07 16:18:41 -0700618}
619
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400620static int ioctx_add_table(struct kioctx *ctx, struct mm_struct *mm)
621{
622 unsigned i, new_nr;
623 struct kioctx_table *table, *old;
624 struct aio_ring *ring;
625
626 spin_lock(&mm->ioctx_lock);
Oleg Nesterov855ef0d2014-04-30 16:16:36 +0200627 table = rcu_dereference_raw(mm->ioctx_table);
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400628
629 while (1) {
630 if (table)
631 for (i = 0; i < table->nr; i++)
Tejun Heod0264c02018-03-14 12:10:17 -0700632 if (!rcu_access_pointer(table->table[i])) {
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400633 ctx->id = i;
Tejun Heod0264c02018-03-14 12:10:17 -0700634 rcu_assign_pointer(table->table[i], ctx);
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400635 spin_unlock(&mm->ioctx_lock);
636
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400637 /* While kioctx setup is in progress,
638 * we are protected from page migration
639 * changes ring_pages by ->ring_lock.
640 */
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400641 ring = kmap_atomic(ctx->ring_pages[0]);
642 ring->id = ctx->id;
643 kunmap_atomic(ring);
644 return 0;
645 }
646
647 new_nr = (table ? table->nr : 1) * 4;
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400648 spin_unlock(&mm->ioctx_lock);
649
650 table = kzalloc(sizeof(*table) + sizeof(struct kioctx *) *
651 new_nr, GFP_KERNEL);
652 if (!table)
653 return -ENOMEM;
654
655 table->nr = new_nr;
656
657 spin_lock(&mm->ioctx_lock);
Oleg Nesterov855ef0d2014-04-30 16:16:36 +0200658 old = rcu_dereference_raw(mm->ioctx_table);
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400659
660 if (!old) {
661 rcu_assign_pointer(mm->ioctx_table, table);
662 } else if (table->nr > old->nr) {
663 memcpy(table->table, old->table,
664 old->nr * sizeof(struct kioctx *));
665
666 rcu_assign_pointer(mm->ioctx_table, table);
667 kfree_rcu(old, rcu);
668 } else {
669 kfree(table);
670 table = old;
671 }
672 }
673}
674
Kent Overstreete34ecee2013-10-10 19:31:47 -0700675static void aio_nr_sub(unsigned nr)
676{
677 spin_lock(&aio_nr_lock);
678 if (WARN_ON(aio_nr - nr > aio_nr))
679 aio_nr = 0;
680 else
681 aio_nr -= nr;
682 spin_unlock(&aio_nr_lock);
683}
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685/* ioctx_alloc
686 * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
687 */
688static struct kioctx *ioctx_alloc(unsigned nr_events)
689{
Zach Brown41003a72013-05-07 16:18:25 -0700690 struct mm_struct *mm = current->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 struct kioctx *ctx;
Al Viroe23754f2012-03-06 14:33:22 -0500692 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000694 /*
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300695 * Store the original nr_events -- what userspace passed to io_setup(),
696 * for counting against the global limit -- before it changes.
697 */
698 unsigned int max_reqs = nr_events;
699
700 /*
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000701 * We keep track of the number of available ringbuffer slots, to prevent
702 * overflow (reqs_available), and we also use percpu counters for this.
703 *
704 * So since up to half the slots might be on other cpu's percpu counters
705 * and unavailable, double nr_events so userspace sees what they
706 * expected: additionally, we move req_batch slots to/from percpu
707 * counters at a time, so make sure that isn't 0:
708 */
709 nr_events = max(nr_events, num_possible_cpus() * 4);
710 nr_events *= 2;
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* Prevent overflows */
Al Viro08397ac2015-03-31 11:43:52 -0400713 if (nr_events > (0x10000000U / sizeof(struct io_event))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 pr_debug("ENOMEM: nr_events too high\n");
715 return ERR_PTR(-EINVAL);
716 }
717
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300718 if (!nr_events || (unsigned long)max_reqs > aio_max_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return ERR_PTR(-EAGAIN);
720
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800721 ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (!ctx)
723 return ERR_PTR(-ENOMEM);
724
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300725 ctx->max_reqs = max_reqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400727 spin_lock_init(&ctx->ctx_lock);
728 spin_lock_init(&ctx->completion_lock);
729 mutex_init(&ctx->ring_lock);
730 /* Protect against page migration throughout kiotx setup by keeping
731 * the ring_lock mutex held until setup is complete. */
732 mutex_lock(&ctx->ring_lock);
733 init_waitqueue_head(&ctx->wait);
734
735 INIT_LIST_HEAD(&ctx->active_reqs);
736
Tejun Heo2aad2a82014-09-24 13:31:50 -0400737 if (percpu_ref_init(&ctx->users, free_ioctx_users, 0, GFP_KERNEL))
Kent Overstreete34ecee2013-10-10 19:31:47 -0700738 goto err;
739
Tejun Heo2aad2a82014-09-24 13:31:50 -0400740 if (percpu_ref_init(&ctx->reqs, free_ioctx_reqs, 0, GFP_KERNEL))
Kent Overstreete34ecee2013-10-10 19:31:47 -0700741 goto err;
Kent Overstreet723be6e2013-05-28 15:14:48 -0700742
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000743 ctx->cpu = alloc_percpu(struct kioctx_cpu);
744 if (!ctx->cpu)
Kent Overstreete34ecee2013-10-10 19:31:47 -0700745 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300747 err = aio_setup_ring(ctx, nr_events);
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400748 if (err < 0)
Kent Overstreete34ecee2013-10-10 19:31:47 -0700749 goto err;
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000750
Kent Overstreet34e83fc2013-04-26 10:58:39 +1000751 atomic_set(&ctx->reqs_available, ctx->nr_events - 1);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000752 ctx->req_batch = (ctx->nr_events - 1) / (num_possible_cpus() * 4);
Benjamin LaHaise6878ea72013-07-31 10:34:18 -0400753 if (ctx->req_batch < 1)
754 ctx->req_batch = 1;
Kent Overstreet34e83fc2013-04-26 10:58:39 +1000755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /* limit the number of system wide aios */
Al Viro9fa1cb32012-03-10 23:14:05 -0500757 spin_lock(&aio_nr_lock);
Mauricio Faria de Oliveira2a8a9862017-07-05 10:53:16 -0300758 if (aio_nr + ctx->max_reqs > aio_max_nr ||
759 aio_nr + ctx->max_reqs < aio_nr) {
Al Viro9fa1cb32012-03-10 23:14:05 -0500760 spin_unlock(&aio_nr_lock);
Kent Overstreete34ecee2013-10-10 19:31:47 -0700761 err = -EAGAIN;
Gu Zhengd1b94322013-12-04 18:19:06 +0800762 goto err_ctx;
Al Viro2dd542b2012-03-10 23:10:35 -0500763 }
764 aio_nr += ctx->max_reqs;
Al Viro9fa1cb32012-03-10 23:14:05 -0500765 spin_unlock(&aio_nr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Benjamin LaHaise18816862013-12-21 15:49:28 -0500767 percpu_ref_get(&ctx->users); /* io_setup() will drop this ref */
768 percpu_ref_get(&ctx->reqs); /* free_ioctx_users() will drop this */
Kent Overstreet723be6e2013-05-28 15:14:48 -0700769
Benjamin LaHaiseda903822013-08-05 13:21:43 -0400770 err = ioctx_add_table(ctx, mm);
771 if (err)
Kent Overstreete34ecee2013-10-10 19:31:47 -0700772 goto err_cleanup;
Benjamin LaHaiseda903822013-08-05 13:21:43 -0400773
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400774 /* Release the ring_lock mutex now that all setup is complete. */
775 mutex_unlock(&ctx->ring_lock);
776
Kent Overstreetcaf41672013-05-07 16:18:35 -0700777 pr_debug("allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700778 ctx, ctx->user_id, mm, ctx->nr_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return ctx;
780
Kent Overstreete34ecee2013-10-10 19:31:47 -0700781err_cleanup:
782 aio_nr_sub(ctx->max_reqs);
Gu Zhengd1b94322013-12-04 18:19:06 +0800783err_ctx:
Al Virodeeb8522015-04-06 17:57:44 -0400784 atomic_set(&ctx->dead, 1);
785 if (ctx->mmap_size)
786 vm_munmap(ctx->mmap_base, ctx->mmap_size);
Gu Zhengd1b94322013-12-04 18:19:06 +0800787 aio_free_ring(ctx);
Kent Overstreete34ecee2013-10-10 19:31:47 -0700788err:
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -0400789 mutex_unlock(&ctx->ring_lock);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000790 free_percpu(ctx->cpu);
Tejun Heo9a1049d2014-06-28 08:10:14 -0400791 percpu_ref_exit(&ctx->reqs);
792 percpu_ref_exit(&ctx->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 kmem_cache_free(kioctx_cachep, ctx);
Kent Overstreetcaf41672013-05-07 16:18:35 -0700794 pr_debug("error allocating ioctx %d\n", err);
Al Viroe23754f2012-03-06 14:33:22 -0500795 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
Kent Overstreet36f55882013-05-07 16:18:41 -0700798/* kill_ioctx
799 * Cancels all outstanding aio requests on an aio context. Used
800 * when the processes owning a context have all exited to encourage
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 * the rapid destruction of the kioctx.
802 */
Benjamin LaHaisefb2d4482014-04-29 12:45:17 -0400803static int kill_ioctx(struct mm_struct *mm, struct kioctx *ctx,
Jens Axboedc48e562015-04-15 11:17:23 -0600804 struct ctx_rq_wait *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400806 struct kioctx_table *table;
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400807
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400808 spin_lock(&mm->ioctx_lock);
Al Virob2edffd2015-04-06 17:48:54 -0400809 if (atomic_xchg(&ctx->dead, 1)) {
810 spin_unlock(&mm->ioctx_lock);
811 return -EINVAL;
812 }
813
Oleg Nesterov855ef0d2014-04-30 16:16:36 +0200814 table = rcu_dereference_raw(mm->ioctx_table);
Tejun Heod0264c02018-03-14 12:10:17 -0700815 WARN_ON(ctx != rcu_access_pointer(table->table[ctx->id]));
816 RCU_INIT_POINTER(table->table[ctx->id], NULL);
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400817 spin_unlock(&mm->ioctx_lock);
Kent Overstreet4fcc7122013-06-12 14:04:59 -0700818
Tejun Heoa6d7cff2018-03-14 12:10:17 -0700819 /* free_ioctx_reqs() will do the necessary RCU synchronization */
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400820 wake_up_all(&ctx->wait);
Kent Overstreet4fcc7122013-06-12 14:04:59 -0700821
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400822 /*
823 * It'd be more correct to do this in free_ioctx(), after all
824 * the outstanding kiocbs have finished - but by then io_destroy
825 * has already returned, so io_setup() could potentially return
826 * -EAGAIN with no ioctxs actually in use (as far as userspace
827 * could tell).
828 */
829 aio_nr_sub(ctx->max_reqs);
Benjamin LaHaisefb2d4482014-04-29 12:45:17 -0400830
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400831 if (ctx->mmap_size)
832 vm_munmap(ctx->mmap_base, ctx->mmap_size);
833
Jens Axboedc48e562015-04-15 11:17:23 -0600834 ctx->rq_wait = wait;
Benjamin LaHaisefa88b6f2014-04-29 12:55:48 -0400835 percpu_ref_kill(&ctx->users);
836 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Kent Overstreet36f55882013-05-07 16:18:41 -0700839/*
840 * exit_aio: called when the last user of mm goes away. At this point, there is
841 * no way for any new requests to be submited or any of the io_* syscalls to be
842 * called on the context.
843 *
844 * There may be outstanding kiocbs, but free_ioctx() will explicitly wait on
845 * them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800847void exit_aio(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200849 struct kioctx_table *table = rcu_dereference_raw(mm->ioctx_table);
Jens Axboedc48e562015-04-15 11:17:23 -0600850 struct ctx_rq_wait wait;
851 int i, skipped;
Jens Axboeabf137d2008-12-09 08:11:22 +0100852
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200853 if (!table)
854 return;
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400855
Jens Axboedc48e562015-04-15 11:17:23 -0600856 atomic_set(&wait.count, table->nr);
857 init_completion(&wait.comp);
858
859 skipped = 0;
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200860 for (i = 0; i < table->nr; ++i) {
Tejun Heod0264c02018-03-14 12:10:17 -0700861 struct kioctx *ctx =
862 rcu_dereference_protected(table->table[i], true);
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400863
Jens Axboedc48e562015-04-15 11:17:23 -0600864 if (!ctx) {
865 skipped++;
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200866 continue;
Jens Axboedc48e562015-04-15 11:17:23 -0600867 }
868
Al Viro936af152012-04-20 21:49:41 -0400869 /*
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200870 * We don't need to bother with munmap() here - exit_mmap(mm)
871 * is coming and it'll unmap everything. And we simply can't,
872 * this is not necessarily our ->mm.
873 * Since kill_ioctx() uses non-zero ->mmap_size as indicator
874 * that it needs to unmap the area, just set it to 0.
Al Viro936af152012-04-20 21:49:41 -0400875 */
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700876 ctx->mmap_size = 0;
Jens Axboedc48e562015-04-15 11:17:23 -0600877 kill_ioctx(mm, ctx, &wait);
878 }
Kent Overstreet36f55882013-05-07 16:18:41 -0700879
Jens Axboedc48e562015-04-15 11:17:23 -0600880 if (!atomic_sub_and_test(skipped, &wait.count)) {
Gu Zheng6098b452014-09-03 17:45:44 +0800881 /* Wait until all IO for the context are done. */
Jens Axboedc48e562015-04-15 11:17:23 -0600882 wait_for_completion(&wait.comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
Oleg Nesterov4b70ac52014-04-30 19:02:48 +0200884
885 RCU_INIT_POINTER(mm->ioctx_table, NULL);
886 kfree(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000889static void put_reqs_available(struct kioctx *ctx, unsigned nr)
890{
891 struct kioctx_cpu *kcpu;
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400892 unsigned long flags;
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000893
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400894 local_irq_save(flags);
Benjamin LaHaisebe6fb452014-07-22 09:56:56 -0400895 kcpu = this_cpu_ptr(ctx->cpu);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000896 kcpu->reqs_available += nr;
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400897
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000898 while (kcpu->reqs_available >= ctx->req_batch * 2) {
899 kcpu->reqs_available -= ctx->req_batch;
900 atomic_add(ctx->req_batch, &ctx->reqs_available);
901 }
902
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400903 local_irq_restore(flags);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000904}
905
906static bool get_reqs_available(struct kioctx *ctx)
907{
908 struct kioctx_cpu *kcpu;
909 bool ret = false;
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400910 unsigned long flags;
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000911
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400912 local_irq_save(flags);
Benjamin LaHaisebe6fb452014-07-22 09:56:56 -0400913 kcpu = this_cpu_ptr(ctx->cpu);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000914 if (!kcpu->reqs_available) {
915 int old, avail = atomic_read(&ctx->reqs_available);
916
917 do {
918 if (avail < ctx->req_batch)
919 goto out;
920
921 old = avail;
922 avail = atomic_cmpxchg(&ctx->reqs_available,
923 avail, avail - ctx->req_batch);
924 } while (avail != old);
925
926 kcpu->reqs_available += ctx->req_batch;
927 }
928
929 ret = true;
930 kcpu->reqs_available--;
931out:
Benjamin LaHaise263782c2014-07-14 12:49:26 -0400932 local_irq_restore(flags);
Kent Overstreete1bdd5f2013-04-26 10:58:39 +1000933 return ret;
934}
935
Benjamin LaHaised856f322014-08-24 13:14:05 -0400936/* refill_reqs_available
937 * Updates the reqs_available reference counts used for tracking the
938 * number of free slots in the completion ring. This can be called
939 * from aio_complete() (to optimistically update reqs_available) or
940 * from aio_get_req() (the we're out of events case). It must be
941 * called holding ctx->completion_lock.
942 */
943static void refill_reqs_available(struct kioctx *ctx, unsigned head,
944 unsigned tail)
945{
946 unsigned events_in_ring, completed;
947
948 /* Clamp head since userland can write to it. */
949 head %= ctx->nr_events;
950 if (head <= tail)
951 events_in_ring = tail - head;
952 else
953 events_in_ring = ctx->nr_events - (head - tail);
954
955 completed = ctx->completed_events;
956 if (events_in_ring < completed)
957 completed -= events_in_ring;
958 else
959 completed = 0;
960
961 if (!completed)
962 return;
963
964 ctx->completed_events -= completed;
965 put_reqs_available(ctx, completed);
966}
967
968/* user_refill_reqs_available
969 * Called to refill reqs_available when aio_get_req() encounters an
970 * out of space in the completion ring.
971 */
972static void user_refill_reqs_available(struct kioctx *ctx)
973{
974 spin_lock_irq(&ctx->completion_lock);
975 if (ctx->completed_events) {
976 struct aio_ring *ring;
977 unsigned head;
978
979 /* Access of ring->head may race with aio_read_events_ring()
980 * here, but that's okay since whether we read the old version
981 * or the new version, and either will be valid. The important
982 * part is that head cannot pass tail since we prevent
983 * aio_complete() from updating tail by holding
984 * ctx->completion_lock. Even if head is invalid, the check
985 * against ctx->completed_events below will make sure we do the
986 * safe/right thing.
987 */
988 ring = kmap_atomic(ctx->ring_pages[0]);
989 head = ring->head;
990 kunmap_atomic(ring);
991
992 refill_reqs_available(ctx, head, ctx->tail);
993 }
994
995 spin_unlock_irq(&ctx->completion_lock);
996}
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998/* aio_get_req
Kent Overstreet57282d82013-05-13 13:42:52 -0700999 * Allocate a slot for an aio request.
1000 * Returns NULL if no requests are free.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 */
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001002static inline struct aio_kiocb *aio_get_req(struct kioctx *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001004 struct aio_kiocb *req;
Kent Overstreeta1c8eae2013-05-07 16:18:53 -07001005
Benjamin LaHaised856f322014-08-24 13:14:05 -04001006 if (!get_reqs_available(ctx)) {
1007 user_refill_reqs_available(ctx);
1008 if (!get_reqs_available(ctx))
1009 return NULL;
1010 }
Kent Overstreeta1c8eae2013-05-07 16:18:53 -07001011
Kent Overstreet0460fef2013-05-07 16:18:49 -07001012 req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL|__GFP_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (unlikely(!req))
Kent Overstreeta1c8eae2013-05-07 16:18:53 -07001014 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Kent Overstreete34ecee2013-10-10 19:31:47 -07001016 percpu_ref_get(&ctx->reqs);
Christoph Hellwig75321b52018-04-09 14:57:56 +02001017 INIT_LIST_HEAD(&req->ki_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 req->ki_ctx = ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return req;
Kent Overstreeta1c8eae2013-05-07 16:18:53 -07001020out_put:
Kent Overstreete1bdd5f2013-04-26 10:58:39 +10001021 put_reqs_available(ctx, 1);
Kent Overstreeta1c8eae2013-05-07 16:18:53 -07001022 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
Adrian Bunkd5470b52008-04-29 00:58:57 -07001025static struct kioctx *lookup_ioctx(unsigned long ctx_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Benjamin LaHaisedb446a02013-07-30 12:54:40 -04001027 struct aio_ring __user *ring = (void __user *)ctx_id;
Jens Axboeabf137d2008-12-09 08:11:22 +01001028 struct mm_struct *mm = current->mm;
Jeff Moyer65c24492009-03-18 17:04:21 -07001029 struct kioctx *ctx, *ret = NULL;
Benjamin LaHaisedb446a02013-07-30 12:54:40 -04001030 struct kioctx_table *table;
1031 unsigned id;
1032
1033 if (get_user(id, &ring->id))
1034 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Jens Axboeabf137d2008-12-09 08:11:22 +01001036 rcu_read_lock();
Benjamin LaHaisedb446a02013-07-30 12:54:40 -04001037 table = rcu_dereference(mm->ioctx_table);
Jens Axboeabf137d2008-12-09 08:11:22 +01001038
Benjamin LaHaisedb446a02013-07-30 12:54:40 -04001039 if (!table || id >= table->nr)
1040 goto out;
1041
Tejun Heod0264c02018-03-14 12:10:17 -07001042 ctx = rcu_dereference(table->table[id]);
Benjamin LaHaisef30d7042013-08-07 18:23:48 -04001043 if (ctx && ctx->user_id == ctx_id) {
Al Virobaf10562018-05-20 16:46:23 -04001044 if (percpu_ref_tryget_live(&ctx->users))
1045 ret = ctx;
Jens Axboeabf137d2008-12-09 08:11:22 +01001046 }
Benjamin LaHaisedb446a02013-07-30 12:54:40 -04001047out:
Jens Axboeabf137d2008-12-09 08:11:22 +01001048 rcu_read_unlock();
Jeff Moyer65c24492009-03-18 17:04:21 -07001049 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052/* aio_complete
1053 * Called when the io request on the given iocb is complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 */
Christoph Hellwig54843f82018-05-02 19:57:21 +02001055static void aio_complete(struct aio_kiocb *iocb, long res, long res2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 struct kioctx *ctx = iocb->ki_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 struct aio_ring *ring;
Kent Overstreet21b40202013-05-07 16:18:47 -07001059 struct io_event *ev_page, *event;
Benjamin LaHaised856f322014-08-24 13:14:05 -04001060 unsigned tail, pos, head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 /*
Kent Overstreet0460fef2013-05-07 16:18:49 -07001064 * Add a completion event to the ring buffer. Must be done holding
Tang Chen4b30f072013-07-03 15:09:16 -07001065 * ctx->completion_lock to prevent other code from messing with the tail
Kent Overstreet0460fef2013-05-07 16:18:49 -07001066 * pointer since we might be called from irq context.
1067 */
1068 spin_lock_irqsave(&ctx->completion_lock, flags);
1069
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001070 tail = ctx->tail;
Kent Overstreet21b40202013-05-07 16:18:47 -07001071 pos = tail + AIO_EVENTS_OFFSET;
1072
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001073 if (++tail >= ctx->nr_events)
Ken Chen4bf69b22005-05-01 08:59:15 -07001074 tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001076 ev_page = kmap_atomic(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
Kent Overstreet21b40202013-05-07 16:18:47 -07001077 event = ev_page + pos % AIO_EVENTS_PER_PAGE;
1078
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001079 event->obj = (u64)(unsigned long)iocb->ki_user_iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 event->data = iocb->ki_user_data;
1081 event->res = res;
1082 event->res2 = res2;
1083
Kent Overstreet21b40202013-05-07 16:18:47 -07001084 kunmap_atomic(ev_page);
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001085 flush_dcache_page(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
Kent Overstreet21b40202013-05-07 16:18:47 -07001086
1087 pr_debug("%p[%u]: %p: %p %Lx %lx %lx\n",
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001088 ctx, tail, iocb, iocb->ki_user_iocb, iocb->ki_user_data,
Kent Overstreetcaf41672013-05-07 16:18:35 -07001089 res, res2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 /* after flagging the request as done, we
1092 * must never even look at it again
1093 */
1094 smp_wmb(); /* make event visible before updating tail */
1095
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001096 ctx->tail = tail;
Kent Overstreet21b40202013-05-07 16:18:47 -07001097
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001098 ring = kmap_atomic(ctx->ring_pages[0]);
Benjamin LaHaised856f322014-08-24 13:14:05 -04001099 head = ring->head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 ring->tail = tail;
Cong Wange8e3c3d2011-11-25 23:14:27 +08001101 kunmap_atomic(ring);
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001102 flush_dcache_page(ctx->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Benjamin LaHaised856f322014-08-24 13:14:05 -04001104 ctx->completed_events++;
1105 if (ctx->completed_events > 1)
1106 refill_reqs_available(ctx, head, tail);
Kent Overstreet0460fef2013-05-07 16:18:49 -07001107 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1108
Kent Overstreet21b40202013-05-07 16:18:47 -07001109 pr_debug("added to ring %p at [%u]\n", iocb, tail);
Davide Libenzi8d1c98b2008-04-10 21:29:19 -07001110
1111 /*
1112 * Check if the user asked us to deliver the result through an
1113 * eventfd. The eventfd_signal() function is safe to be called
1114 * from IRQ context.
1115 */
Christoph Hellwig54843f82018-05-02 19:57:21 +02001116 if (iocb->ki_eventfd) {
Davide Libenzi8d1c98b2008-04-10 21:29:19 -07001117 eventfd_signal(iocb->ki_eventfd, 1);
Christoph Hellwig54843f82018-05-02 19:57:21 +02001118 eventfd_ctx_put(iocb->ki_eventfd);
1119 }
Davide Libenzi8d1c98b2008-04-10 21:29:19 -07001120
Christoph Hellwig54843f82018-05-02 19:57:21 +02001121 kmem_cache_free(kiocb_cachep, iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Quentin Barnes6cb2a212008-03-19 17:00:39 -07001123 /*
1124 * We have to order our ring_info tail store above and test
1125 * of the wait list below outside the wait lock. This is
1126 * like in wake_up_bit() where clearing a bit has to be
1127 * ordered with the unlocked test.
1128 */
1129 smp_mb();
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (waitqueue_active(&ctx->wait))
1132 wake_up(&ctx->wait);
1133
Kent Overstreete34ecee2013-10-10 19:31:47 -07001134 percpu_ref_put(&ctx->reqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
Gu Zheng2be4e7d2014-07-23 18:03:53 +08001137/* aio_read_events_ring
Kent Overstreeta31ad382013-05-07 16:18:45 -07001138 * Pull an event off of the ioctx's event ring. Returns the number of
1139 * events fetched
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 */
Kent Overstreeta31ad382013-05-07 16:18:45 -07001141static long aio_read_events_ring(struct kioctx *ctx,
1142 struct io_event __user *event, long nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 struct aio_ring *ring;
Kent Overstreet5ffac122013-05-09 15:36:07 -07001145 unsigned head, tail, pos;
Kent Overstreeta31ad382013-05-07 16:18:45 -07001146 long ret = 0;
1147 int copy_ret;
1148
Dave Chinner9c9ce762015-02-03 19:29:05 -05001149 /*
1150 * The mutex can block and wake us up and that will cause
1151 * wait_event_interruptible_hrtimeout() to schedule without sleeping
1152 * and repeat. This should be rare enough that it doesn't cause
1153 * peformance issues. See the comment in read_events() for more detail.
1154 */
1155 sched_annotate_sleep();
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001156 mutex_lock(&ctx->ring_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Benjamin LaHaisefa8a53c2014-03-28 10:14:45 -04001158 /* Access to ->ring_pages here is protected by ctx->ring_lock. */
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001159 ring = kmap_atomic(ctx->ring_pages[0]);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001160 head = ring->head;
Kent Overstreet5ffac122013-05-09 15:36:07 -07001161 tail = ring->tail;
Kent Overstreeta31ad382013-05-07 16:18:45 -07001162 kunmap_atomic(ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Jeff Moyer2ff396be2014-09-02 13:17:00 -04001164 /*
1165 * Ensure that once we've read the current tail pointer, that
1166 * we also see the events that were stored up to the tail.
1167 */
1168 smp_rmb();
1169
Kent Overstreet5ffac122013-05-09 15:36:07 -07001170 pr_debug("h%u t%u m%u\n", head, tail, ctx->nr_events);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001171
Kent Overstreet5ffac122013-05-09 15:36:07 -07001172 if (head == tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 goto out;
1174
Benjamin LaHaiseedfbbf32014-06-24 13:32:51 -04001175 head %= ctx->nr_events;
1176 tail %= ctx->nr_events;
1177
Kent Overstreeta31ad382013-05-07 16:18:45 -07001178 while (ret < nr) {
1179 long avail;
1180 struct io_event *ev;
1181 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Kent Overstreet5ffac122013-05-09 15:36:07 -07001183 avail = (head <= tail ? tail : ctx->nr_events) - head;
1184 if (head == tail)
Kent Overstreeta31ad382013-05-07 16:18:45 -07001185 break;
1186
Kent Overstreeta31ad382013-05-07 16:18:45 -07001187 pos = head + AIO_EVENTS_OFFSET;
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001188 page = ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE];
Kent Overstreeta31ad382013-05-07 16:18:45 -07001189 pos %= AIO_EVENTS_PER_PAGE;
1190
Al Virod2988bd42018-05-26 19:13:10 -04001191 avail = min(avail, nr - ret);
1192 avail = min_t(long, avail, AIO_EVENTS_PER_PAGE - pos);
1193
Kent Overstreeta31ad382013-05-07 16:18:45 -07001194 ev = kmap(page);
1195 copy_ret = copy_to_user(event + ret, ev + pos,
1196 sizeof(*ev) * avail);
1197 kunmap(page);
1198
1199 if (unlikely(copy_ret)) {
1200 ret = -EFAULT;
1201 goto out;
1202 }
1203
1204 ret += avail;
1205 head += avail;
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001206 head %= ctx->nr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001209 ring = kmap_atomic(ctx->ring_pages[0]);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001210 ring->head = head;
Zhao Hongjiang91d80a82013-04-26 11:03:53 +08001211 kunmap_atomic(ring);
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001212 flush_dcache_page(ctx->ring_pages[0]);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001213
Kent Overstreet5ffac122013-05-09 15:36:07 -07001214 pr_debug("%li h%u t%u\n", ret, head, tail);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001215out:
Kent Overstreet58c85dc2013-05-07 16:18:55 -07001216 mutex_unlock(&ctx->ring_lock);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return ret;
1219}
1220
Kent Overstreeta31ad382013-05-07 16:18:45 -07001221static bool aio_read_events(struct kioctx *ctx, long min_nr, long nr,
1222 struct io_event __user *event, long *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Kent Overstreeta31ad382013-05-07 16:18:45 -07001224 long ret = aio_read_events_ring(ctx, event + *i, nr - *i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Kent Overstreeta31ad382013-05-07 16:18:45 -07001226 if (ret > 0)
1227 *i += ret;
1228
1229 if (unlikely(atomic_read(&ctx->dead)))
1230 ret = -EINVAL;
1231
1232 if (!*i)
1233 *i = ret;
1234
1235 return ret < 0 || *i >= min_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Kent Overstreeta31ad382013-05-07 16:18:45 -07001238static long read_events(struct kioctx *ctx, long min_nr, long nr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 struct io_event __user *event,
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07001240 ktime_t until)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Kent Overstreeta31ad382013-05-07 16:18:45 -07001242 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Kent Overstreeta31ad382013-05-07 16:18:45 -07001244 /*
1245 * Note that aio_read_events() is being called as the conditional - i.e.
1246 * we're calling it after prepare_to_wait() has set task state to
1247 * TASK_INTERRUPTIBLE.
1248 *
1249 * But aio_read_events() can block, and if it blocks it's going to flip
1250 * the task state back to TASK_RUNNING.
1251 *
1252 * This should be ok, provided it doesn't flip the state back to
1253 * TASK_RUNNING and return 0 too much - that causes us to spin. That
1254 * will only happen if the mutex_lock() call blocks, and we then find
1255 * the ringbuffer empty. So in practice we should be ok, but it's
1256 * something to be aware of when touching this code.
1257 */
Thomas Gleixner2456e852016-12-25 11:38:40 +01001258 if (until == 0)
Fam Zheng5f785de2014-11-06 20:44:36 +08001259 aio_read_events(ctx, min_nr, nr, event, &ret);
1260 else
1261 wait_event_interruptible_hrtimeout(ctx->wait,
1262 aio_read_events(ctx, min_nr, nr, event, &ret),
1263 until);
Kent Overstreeta31ad382013-05-07 16:18:45 -07001264 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267/* sys_io_setup:
1268 * Create an aio_context capable of receiving at least nr_events.
1269 * ctxp must not point to an aio_context that already exists, and
1270 * must be initialized to 0 prior to the call. On successful
1271 * creation of the aio_context, *ctxp is filled in with the resulting
1272 * handle. May fail with -EINVAL if *ctxp is not initialized,
1273 * if the specified nr_events exceeds internal limits. May fail
1274 * with -EAGAIN if the specified nr_events exceeds the user's limit
1275 * of available events. May fail with -ENOMEM if insufficient kernel
1276 * resources are available. May fail with -EFAULT if an invalid
1277 * pointer is passed for ctxp. Will fail with -ENOSYS if not
1278 * implemented.
1279 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001280SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 struct kioctx *ioctx = NULL;
1283 unsigned long ctx;
1284 long ret;
1285
1286 ret = get_user(ctx, ctxp);
1287 if (unlikely(ret))
1288 goto out;
1289
1290 ret = -EINVAL;
Zach Brownd55b5fd2005-11-07 00:59:31 -08001291 if (unlikely(ctx || nr_events == 0)) {
Kinglong Meeacd88d42015-02-04 21:15:59 +08001292 pr_debug("EINVAL: ctx %lu nr_events %u\n",
Zach Brownd55b5fd2005-11-07 00:59:31 -08001293 ctx, nr_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 goto out;
1295 }
1296
1297 ioctx = ioctx_alloc(nr_events);
1298 ret = PTR_ERR(ioctx);
1299 if (!IS_ERR(ioctx)) {
1300 ret = put_user(ioctx->user_id, ctxp);
Al Viroa2e18592012-03-20 16:27:57 -04001301 if (ret)
Anatol Pomozove02ba722014-04-15 11:31:33 -07001302 kill_ioctx(current->mm, ioctx, NULL);
Kent Overstreet723be6e2013-05-28 15:14:48 -07001303 percpu_ref_put(&ioctx->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305
1306out:
1307 return ret;
1308}
1309
Al Viroc00d2c72016-12-20 07:04:57 -05001310#ifdef CONFIG_COMPAT
1311COMPAT_SYSCALL_DEFINE2(io_setup, unsigned, nr_events, u32 __user *, ctx32p)
1312{
1313 struct kioctx *ioctx = NULL;
1314 unsigned long ctx;
1315 long ret;
1316
1317 ret = get_user(ctx, ctx32p);
1318 if (unlikely(ret))
1319 goto out;
1320
1321 ret = -EINVAL;
1322 if (unlikely(ctx || nr_events == 0)) {
1323 pr_debug("EINVAL: ctx %lu nr_events %u\n",
1324 ctx, nr_events);
1325 goto out;
1326 }
1327
1328 ioctx = ioctx_alloc(nr_events);
1329 ret = PTR_ERR(ioctx);
1330 if (!IS_ERR(ioctx)) {
1331 /* truncating is ok because it's a user address */
1332 ret = put_user((u32)ioctx->user_id, ctx32p);
1333 if (ret)
1334 kill_ioctx(current->mm, ioctx, NULL);
1335 percpu_ref_put(&ioctx->users);
1336 }
1337
1338out:
1339 return ret;
1340}
1341#endif
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343/* sys_io_destroy:
1344 * Destroy the aio_context specified. May cancel any outstanding
1345 * AIOs and block on completion. Will fail with -ENOSYS if not
Satoru Takeuchi642b5122010-08-05 11:23:11 -07001346 * implemented. May fail with -EINVAL if the context pointed to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 * is invalid.
1348 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001349SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 struct kioctx *ioctx = lookup_ioctx(ctx);
1352 if (likely(NULL != ioctx)) {
Jens Axboedc48e562015-04-15 11:17:23 -06001353 struct ctx_rq_wait wait;
Benjamin LaHaisefb2d4482014-04-29 12:45:17 -04001354 int ret;
Anatol Pomozove02ba722014-04-15 11:31:33 -07001355
Jens Axboedc48e562015-04-15 11:17:23 -06001356 init_completion(&wait.comp);
1357 atomic_set(&wait.count, 1);
1358
Anatol Pomozove02ba722014-04-15 11:31:33 -07001359 /* Pass requests_done to kill_ioctx() where it can be set
1360 * in a thread-safe way. If we try to set it here then we have
1361 * a race condition if two io_destroy() called simultaneously.
1362 */
Jens Axboedc48e562015-04-15 11:17:23 -06001363 ret = kill_ioctx(current->mm, ioctx, &wait);
Kent Overstreet723be6e2013-05-28 15:14:48 -07001364 percpu_ref_put(&ioctx->users);
Anatol Pomozove02ba722014-04-15 11:31:33 -07001365
1366 /* Wait until all IO for the context are done. Otherwise kernel
1367 * keep using user-space buffers even if user thinks the context
1368 * is destroyed.
1369 */
Benjamin LaHaisefb2d4482014-04-29 12:45:17 -04001370 if (!ret)
Jens Axboedc48e562015-04-15 11:17:23 -06001371 wait_for_completion(&wait.comp);
Anatol Pomozove02ba722014-04-15 11:31:33 -07001372
Benjamin LaHaisefb2d4482014-04-29 12:45:17 -04001373 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
Kinglong Meeacd88d42015-02-04 21:15:59 +08001375 pr_debug("EINVAL: invalid context id\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return -EINVAL;
1377}
1378
Al Viro3c96c7f2018-05-28 13:37:43 -04001379static void aio_remove_iocb(struct aio_kiocb *iocb)
1380{
1381 struct kioctx *ctx = iocb->ki_ctx;
1382 unsigned long flags;
1383
1384 spin_lock_irqsave(&ctx->ctx_lock, flags);
1385 list_del(&iocb->ki_list);
1386 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
1387}
1388
Christoph Hellwig54843f82018-05-02 19:57:21 +02001389static void aio_complete_rw(struct kiocb *kiocb, long res, long res2)
1390{
1391 struct aio_kiocb *iocb = container_of(kiocb, struct aio_kiocb, rw);
1392
Al Viro3c96c7f2018-05-28 13:37:43 -04001393 if (!list_empty_careful(&iocb->ki_list))
1394 aio_remove_iocb(iocb);
1395
Christoph Hellwig54843f82018-05-02 19:57:21 +02001396 if (kiocb->ki_flags & IOCB_WRITE) {
1397 struct inode *inode = file_inode(kiocb->ki_filp);
1398
1399 /*
1400 * Tell lockdep we inherited freeze protection from submission
1401 * thread.
1402 */
1403 if (S_ISREG(inode->i_mode))
1404 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
1405 file_end_write(kiocb->ki_filp);
1406 }
1407
1408 fput(kiocb->ki_filp);
1409 aio_complete(iocb, res, res2);
1410}
1411
1412static int aio_prep_rw(struct kiocb *req, struct iocb *iocb)
1413{
1414 int ret;
1415
1416 req->ki_filp = fget(iocb->aio_fildes);
1417 if (unlikely(!req->ki_filp))
1418 return -EBADF;
1419 req->ki_complete = aio_complete_rw;
1420 req->ki_pos = iocb->aio_offset;
1421 req->ki_flags = iocb_flags(req->ki_filp);
1422 if (iocb->aio_flags & IOCB_FLAG_RESFD)
1423 req->ki_flags |= IOCB_EVENTFD;
Adam Manzanaresfc287242018-05-22 10:52:18 -07001424 req->ki_hint = ki_hint_validate(file_write_hint(req->ki_filp));
Adam Manzanaresd9a08a92018-05-22 10:52:19 -07001425 if (iocb->aio_flags & IOCB_FLAG_IOPRIO) {
1426 /*
1427 * If the IOCB_FLAG_IOPRIO flag of aio_flags is set, then
1428 * aio_reqprio is interpreted as an I/O scheduling
1429 * class and priority.
1430 */
1431 ret = ioprio_check_cap(iocb->aio_reqprio);
1432 if (ret) {
Adam Manzanares9a6d9a62018-06-04 10:59:57 -07001433 pr_debug("aio ioprio check cap error: %d\n", ret);
1434 return ret;
Adam Manzanaresd9a08a92018-05-22 10:52:19 -07001435 }
1436
1437 req->ki_ioprio = iocb->aio_reqprio;
1438 } else
1439 req->ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);
1440
Christoph Hellwig54843f82018-05-02 19:57:21 +02001441 ret = kiocb_set_rw_flags(req, iocb->aio_rw_flags);
1442 if (unlikely(ret))
1443 fput(req->ki_filp);
1444 return ret;
1445}
1446
Christoph Hellwig89319d312016-10-30 11:42:03 -05001447static int aio_setup_rw(int rw, struct iocb *iocb, struct iovec **iovec,
1448 bool vectored, bool compat, struct iov_iter *iter)
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001449{
Christoph Hellwig89319d312016-10-30 11:42:03 -05001450 void __user *buf = (void __user *)(uintptr_t)iocb->aio_buf;
1451 size_t len = iocb->aio_nbytes;
1452
1453 if (!vectored) {
1454 ssize_t ret = import_single_range(rw, buf, len, *iovec, iter);
1455 *iovec = NULL;
1456 return ret;
1457 }
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001458#ifdef CONFIG_COMPAT
1459 if (compat)
Christoph Hellwig89319d312016-10-30 11:42:03 -05001460 return compat_import_iovec(rw, buf, len, UIO_FASTIOV, iovec,
1461 iter);
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001462#endif
Christoph Hellwig89319d312016-10-30 11:42:03 -05001463 return import_iovec(rw, buf, len, UIO_FASTIOV, iovec, iter);
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001464}
1465
Al Viro9061d142018-05-26 19:11:40 -04001466static inline void aio_rw_done(struct kiocb *req, ssize_t ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Christoph Hellwig89319d312016-10-30 11:42:03 -05001468 switch (ret) {
1469 case -EIOCBQUEUED:
Al Viro9061d142018-05-26 19:11:40 -04001470 break;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001471 case -ERESTARTSYS:
1472 case -ERESTARTNOINTR:
1473 case -ERESTARTNOHAND:
1474 case -ERESTART_RESTARTBLOCK:
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001475 /*
1476 * There's no easy way to restart the syscall since other AIO's
1477 * may be already running. Just fail this IO with EINTR.
1478 */
Christoph Hellwig89319d312016-10-30 11:42:03 -05001479 ret = -EINTR;
1480 /*FALLTHRU*/
1481 default:
Christoph Hellwig54843f82018-05-02 19:57:21 +02001482 aio_complete_rw(req, ret, 0);
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001483 }
Christoph Hellwig89319d312016-10-30 11:42:03 -05001484}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Christoph Hellwig89319d312016-10-30 11:42:03 -05001486static ssize_t aio_read(struct kiocb *req, struct iocb *iocb, bool vectored,
1487 bool compat)
1488{
Christoph Hellwig89319d312016-10-30 11:42:03 -05001489 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1490 struct iov_iter iter;
Christoph Hellwig54843f82018-05-02 19:57:21 +02001491 struct file *file;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001492 ssize_t ret;
1493
Christoph Hellwig54843f82018-05-02 19:57:21 +02001494 ret = aio_prep_rw(req, iocb);
1495 if (ret)
1496 return ret;
1497 file = req->ki_filp;
1498
1499 ret = -EBADF;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001500 if (unlikely(!(file->f_mode & FMODE_READ)))
Christoph Hellwig54843f82018-05-02 19:57:21 +02001501 goto out_fput;
1502 ret = -EINVAL;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001503 if (unlikely(!file->f_op->read_iter))
Christoph Hellwig54843f82018-05-02 19:57:21 +02001504 goto out_fput;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001505
1506 ret = aio_setup_rw(READ, iocb, &iovec, vectored, compat, &iter);
1507 if (ret)
Christoph Hellwig54843f82018-05-02 19:57:21 +02001508 goto out_fput;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001509 ret = rw_verify_area(READ, file, &req->ki_pos, iov_iter_count(&iter));
1510 if (!ret)
Al Viro9061d142018-05-26 19:11:40 -04001511 aio_rw_done(req, call_read_iter(file, req, &iter));
Christoph Hellwig89319d312016-10-30 11:42:03 -05001512 kfree(iovec);
Christoph Hellwig54843f82018-05-02 19:57:21 +02001513out_fput:
Al Viro9061d142018-05-26 19:11:40 -04001514 if (unlikely(ret))
Christoph Hellwig54843f82018-05-02 19:57:21 +02001515 fput(file);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001516 return ret;
1517}
1518
1519static ssize_t aio_write(struct kiocb *req, struct iocb *iocb, bool vectored,
1520 bool compat)
1521{
Christoph Hellwig89319d312016-10-30 11:42:03 -05001522 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1523 struct iov_iter iter;
Christoph Hellwig54843f82018-05-02 19:57:21 +02001524 struct file *file;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001525 ssize_t ret;
1526
Christoph Hellwig54843f82018-05-02 19:57:21 +02001527 ret = aio_prep_rw(req, iocb);
1528 if (ret)
1529 return ret;
1530 file = req->ki_filp;
1531
1532 ret = -EBADF;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001533 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Christoph Hellwig54843f82018-05-02 19:57:21 +02001534 goto out_fput;
1535 ret = -EINVAL;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001536 if (unlikely(!file->f_op->write_iter))
Christoph Hellwig54843f82018-05-02 19:57:21 +02001537 goto out_fput;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001538
1539 ret = aio_setup_rw(WRITE, iocb, &iovec, vectored, compat, &iter);
1540 if (ret)
Christoph Hellwig54843f82018-05-02 19:57:21 +02001541 goto out_fput;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001542 ret = rw_verify_area(WRITE, file, &req->ki_pos, iov_iter_count(&iter));
1543 if (!ret) {
Jan Kara70fe2f42016-10-30 11:42:04 -05001544 /*
Christoph Hellwig92ce4722018-04-06 09:28:17 +02001545 * Open-code file_start_write here to grab freeze protection,
Christoph Hellwig54843f82018-05-02 19:57:21 +02001546 * which will be released by another thread in
1547 * aio_complete_rw(). Fool lockdep by telling it the lock got
1548 * released so that it doesn't complain about the held lock when
1549 * we return to userspace.
Jan Kara70fe2f42016-10-30 11:42:04 -05001550 */
Christoph Hellwig92ce4722018-04-06 09:28:17 +02001551 if (S_ISREG(file_inode(file)->i_mode)) {
1552 __sb_start_write(file_inode(file)->i_sb, SB_FREEZE_WRITE, true);
Shaohua Lia12f1ae2016-12-13 12:09:56 -08001553 __sb_writers_release(file_inode(file)->i_sb, SB_FREEZE_WRITE);
Christoph Hellwig92ce4722018-04-06 09:28:17 +02001554 }
1555 req->ki_flags |= IOCB_WRITE;
Al Viro9061d142018-05-26 19:11:40 -04001556 aio_rw_done(req, call_write_iter(file, req, &iter));
Christoph Hellwig89319d312016-10-30 11:42:03 -05001557 }
1558 kfree(iovec);
Christoph Hellwig54843f82018-05-02 19:57:21 +02001559out_fput:
Al Viro9061d142018-05-26 19:11:40 -04001560 if (unlikely(ret))
Christoph Hellwig54843f82018-05-02 19:57:21 +02001561 fput(file);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001562 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
Christoph Hellwiga3c0d432018-03-27 19:18:57 +02001565static void aio_fsync_work(struct work_struct *work)
1566{
1567 struct fsync_iocb *req = container_of(work, struct fsync_iocb, work);
1568 int ret;
1569
1570 ret = vfs_fsync(req->file, req->datasync);
1571 fput(req->file);
1572 aio_complete(container_of(req, struct aio_kiocb, fsync), ret, 0);
1573}
1574
1575static int aio_fsync(struct fsync_iocb *req, struct iocb *iocb, bool datasync)
1576{
1577 if (unlikely(iocb->aio_buf || iocb->aio_offset || iocb->aio_nbytes ||
1578 iocb->aio_rw_flags))
1579 return -EINVAL;
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001580
Christoph Hellwiga3c0d432018-03-27 19:18:57 +02001581 req->file = fget(iocb->aio_fildes);
1582 if (unlikely(!req->file))
1583 return -EBADF;
1584 if (unlikely(!req->file->f_op->fsync)) {
1585 fput(req->file);
1586 return -EINVAL;
1587 }
1588
1589 req->datasync = datasync;
1590 INIT_WORK(&req->work, aio_fsync_work);
1591 schedule_work(&req->work);
Al Viro9061d142018-05-26 19:11:40 -04001592 return 0;
Christoph Hellwiga3c0d432018-03-27 19:18:57 +02001593}
1594
Adrian Bunkd5470b52008-04-29 00:58:57 -07001595static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
Al Viro95af8492018-05-26 19:43:16 -04001596 bool compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001598 struct aio_kiocb *req;
Al Viro95af8492018-05-26 19:43:16 -04001599 struct iocb iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 ssize_t ret;
1601
Al Viro95af8492018-05-26 19:43:16 -04001602 if (unlikely(copy_from_user(&iocb, user_iocb, sizeof(iocb))))
1603 return -EFAULT;
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 /* enforce forwards compatibility on users */
Al Viro95af8492018-05-26 19:43:16 -04001606 if (unlikely(iocb.aio_reserved2)) {
Kent Overstreetcaf41672013-05-07 16:18:35 -07001607 pr_debug("EINVAL: reserve field set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return -EINVAL;
1609 }
1610
1611 /* prevent overflows */
1612 if (unlikely(
Al Viro95af8492018-05-26 19:43:16 -04001613 (iocb.aio_buf != (unsigned long)iocb.aio_buf) ||
1614 (iocb.aio_nbytes != (size_t)iocb.aio_nbytes) ||
1615 ((ssize_t)iocb.aio_nbytes < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 )) {
Kinglong Meeacd88d42015-02-04 21:15:59 +08001617 pr_debug("EINVAL: overflow check\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return -EINVAL;
1619 }
1620
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001621 req = aio_get_req(ctx);
Kent Overstreet1d98ebf2013-05-07 16:18:37 -07001622 if (unlikely(!req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return -EAGAIN;
Kent Overstreet1d98ebf2013-05-07 16:18:37 -07001624
Al Viro95af8492018-05-26 19:43:16 -04001625 if (iocb.aio_flags & IOCB_FLAG_RESFD) {
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001626 /*
1627 * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
1628 * instance of the file* now. The file descriptor must be
1629 * an eventfd() fd, and will be signaled for each completed
1630 * event using the eventfd_signal() function.
1631 */
Al Viro95af8492018-05-26 19:43:16 -04001632 req->ki_eventfd = eventfd_ctx_fdget((int) iocb.aio_resfd);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07001633 if (IS_ERR(req->ki_eventfd)) {
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001634 ret = PTR_ERR(req->ki_eventfd);
Davide Libenzi87c3a862009-03-18 17:04:19 -07001635 req->ki_eventfd = NULL;
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001636 goto out_put_req;
1637 }
Goldwyn Rodrigues9830f4b2017-06-20 07:05:42 -05001638 }
1639
Kent Overstreet8a660892013-05-07 16:19:10 -07001640 ret = put_user(KIOCB_KEY, &user_iocb->aio_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (unlikely(ret)) {
Kent Overstreetcaf41672013-05-07 16:18:35 -07001642 pr_debug("EFAULT: aio_key\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 goto out_put_req;
1644 }
1645
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001646 req->ki_user_iocb = user_iocb;
Al Viro95af8492018-05-26 19:43:16 -04001647 req->ki_user_data = iocb.aio_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Al Viro95af8492018-05-26 19:43:16 -04001649 switch (iocb.aio_lio_opcode) {
Christoph Hellwig89319d312016-10-30 11:42:03 -05001650 case IOCB_CMD_PREAD:
Al Viro95af8492018-05-26 19:43:16 -04001651 ret = aio_read(&req->rw, &iocb, false, compat);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001652 break;
1653 case IOCB_CMD_PWRITE:
Al Viro95af8492018-05-26 19:43:16 -04001654 ret = aio_write(&req->rw, &iocb, false, compat);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001655 break;
1656 case IOCB_CMD_PREADV:
Al Viro95af8492018-05-26 19:43:16 -04001657 ret = aio_read(&req->rw, &iocb, true, compat);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001658 break;
1659 case IOCB_CMD_PWRITEV:
Al Viro95af8492018-05-26 19:43:16 -04001660 ret = aio_write(&req->rw, &iocb, true, compat);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001661 break;
Christoph Hellwiga3c0d432018-03-27 19:18:57 +02001662 case IOCB_CMD_FSYNC:
Al Viro95af8492018-05-26 19:43:16 -04001663 ret = aio_fsync(&req->fsync, &iocb, false);
Christoph Hellwiga3c0d432018-03-27 19:18:57 +02001664 break;
1665 case IOCB_CMD_FDSYNC:
Al Viro95af8492018-05-26 19:43:16 -04001666 ret = aio_fsync(&req->fsync, &iocb, true);
Christoph Hellwigac060cb2018-05-28 07:19:49 +02001667 break;
Christoph Hellwig89319d312016-10-30 11:42:03 -05001668 default:
Al Viro95af8492018-05-26 19:43:16 -04001669 pr_debug("invalid aio operation %d\n", iocb.aio_lio_opcode);
Christoph Hellwig89319d312016-10-30 11:42:03 -05001670 ret = -EINVAL;
1671 break;
1672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Christoph Hellwig92ce4722018-04-06 09:28:17 +02001674 /*
Al Viro9061d142018-05-26 19:11:40 -04001675 * If ret is 0, we'd either done aio_complete() ourselves or have
1676 * arranged for that to be done asynchronously. Anything non-zero
1677 * means that we need to destroy req ourselves.
Christoph Hellwig92ce4722018-04-06 09:28:17 +02001678 */
Al Viro9061d142018-05-26 19:11:40 -04001679 if (ret)
Christoph Hellwig89319d312016-10-30 11:42:03 -05001680 goto out_put_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682out_put_req:
Kent Overstreete1bdd5f2013-04-26 10:58:39 +10001683 put_reqs_available(ctx, 1);
Kent Overstreete34ecee2013-10-10 19:31:47 -07001684 percpu_ref_put(&ctx->reqs);
Christoph Hellwig54843f82018-05-02 19:57:21 +02001685 if (req->ki_eventfd)
1686 eventfd_ctx_put(req->ki_eventfd);
1687 kmem_cache_free(kiocb_cachep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 return ret;
1689}
1690
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001691/* sys_io_submit:
1692 * Queue the nr iocbs pointed to by iocbpp for processing. Returns
1693 * the number of iocbs queued. May return -EINVAL if the aio_context
1694 * specified by ctx_id is invalid, if nr is < 0, if the iocb at
1695 * *iocbpp[0] is not properly initialized, if the operation specified
1696 * is invalid for the file descriptor in the iocb. May fail with
1697 * -EFAULT if any of the data structures point to invalid data. May
1698 * fail with -EBADF if the file descriptor specified in the first
1699 * iocb is invalid. May fail with -EAGAIN if insufficient resources
1700 * are available to queue any iocbs. Will return 0 if nr is 0. Will
1701 * fail with -ENOSYS if not implemented.
1702 */
1703SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
1704 struct iocb __user * __user *, iocbpp)
1705{
Al Viro67ba0492018-05-26 20:10:07 -04001706 struct kioctx *ctx;
1707 long ret = 0;
1708 int i = 0;
1709 struct blk_plug plug;
1710
1711 if (unlikely(nr < 0))
1712 return -EINVAL;
1713
Al Viro67ba0492018-05-26 20:10:07 -04001714 ctx = lookup_ioctx(ctx_id);
1715 if (unlikely(!ctx)) {
1716 pr_debug("EINVAL: invalid context id\n");
1717 return -EINVAL;
1718 }
1719
Al Viro1da92772018-05-26 20:10:07 -04001720 if (nr > ctx->nr_events)
1721 nr = ctx->nr_events;
1722
Al Viro67ba0492018-05-26 20:10:07 -04001723 blk_start_plug(&plug);
1724 for (i = 0; i < nr; i++) {
1725 struct iocb __user *user_iocb;
1726
1727 if (unlikely(get_user(user_iocb, iocbpp + i))) {
1728 ret = -EFAULT;
1729 break;
1730 }
1731
1732 ret = io_submit_one(ctx, user_iocb, false);
1733 if (ret)
1734 break;
1735 }
1736 blk_finish_plug(&plug);
1737
1738 percpu_ref_put(&ctx->users);
1739 return i ? i : ret;
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001740}
1741
Al Viroc00d2c72016-12-20 07:04:57 -05001742#ifdef CONFIG_COMPAT
Al Viroc00d2c72016-12-20 07:04:57 -05001743COMPAT_SYSCALL_DEFINE3(io_submit, compat_aio_context_t, ctx_id,
Al Viro67ba0492018-05-26 20:10:07 -04001744 int, nr, compat_uptr_t __user *, iocbpp)
Al Viroc00d2c72016-12-20 07:04:57 -05001745{
Al Viro67ba0492018-05-26 20:10:07 -04001746 struct kioctx *ctx;
1747 long ret = 0;
1748 int i = 0;
1749 struct blk_plug plug;
Al Viroc00d2c72016-12-20 07:04:57 -05001750
1751 if (unlikely(nr < 0))
1752 return -EINVAL;
1753
Al Viro67ba0492018-05-26 20:10:07 -04001754 ctx = lookup_ioctx(ctx_id);
1755 if (unlikely(!ctx)) {
1756 pr_debug("EINVAL: invalid context id\n");
1757 return -EINVAL;
1758 }
1759
Al Viro1da92772018-05-26 20:10:07 -04001760 if (nr > ctx->nr_events)
1761 nr = ctx->nr_events;
1762
Al Viro67ba0492018-05-26 20:10:07 -04001763 blk_start_plug(&plug);
1764 for (i = 0; i < nr; i++) {
1765 compat_uptr_t user_iocb;
1766
1767 if (unlikely(get_user(user_iocb, iocbpp + i))) {
1768 ret = -EFAULT;
1769 break;
1770 }
1771
1772 ret = io_submit_one(ctx, compat_ptr(user_iocb), true);
1773 if (ret)
1774 break;
1775 }
1776 blk_finish_plug(&plug);
1777
1778 percpu_ref_put(&ctx->users);
1779 return i ? i : ret;
Al Viroc00d2c72016-12-20 07:04:57 -05001780}
1781#endif
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783/* lookup_kiocb
1784 * Finds a given iocb for cancellation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 */
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001786static struct aio_kiocb *
Christoph Hellwigf3a27522018-03-30 11:19:25 +02001787lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001789 struct aio_kiocb *kiocb;
Zach Brownd00689a2005-11-13 16:07:34 -08001790
1791 assert_spin_locked(&ctx->ctx_lock);
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 /* TODO: use a hash or array, this sucks. */
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001794 list_for_each_entry(kiocb, &ctx->active_reqs, ki_list) {
1795 if (kiocb->ki_user_iocb == iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return kiocb;
1797 }
1798 return NULL;
1799}
1800
1801/* sys_io_cancel:
1802 * Attempts to cancel an iocb previously passed to io_submit. If
1803 * the operation is successfully cancelled, the resulting event is
1804 * copied into the memory pointed to by result without being placed
1805 * into the completion queue and 0 is returned. May fail with
1806 * -EFAULT if any of the data structures pointed to are invalid.
1807 * May fail with -EINVAL if aio_context specified by ctx_id is
1808 * invalid. May fail with -EAGAIN if the iocb specified was not
1809 * cancelled. Will fail with -ENOSYS if not implemented.
1810 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001811SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
1812 struct io_event __user *, result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 struct kioctx *ctx;
Christoph Hellwig04b2fa92015-02-02 14:49:06 +01001815 struct aio_kiocb *kiocb;
Christoph Hellwig888933f2018-05-23 14:11:02 +02001816 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 u32 key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Christoph Hellwigf3a27522018-03-30 11:19:25 +02001819 if (unlikely(get_user(key, &iocb->aio_key)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return -EFAULT;
Christoph Hellwigf3a27522018-03-30 11:19:25 +02001821 if (unlikely(key != KIOCB_KEY))
1822 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 ctx = lookup_ioctx(ctx_id);
1825 if (unlikely(!ctx))
1826 return -EINVAL;
1827
1828 spin_lock_irq(&ctx->ctx_lock);
Christoph Hellwigf3a27522018-03-30 11:19:25 +02001829 kiocb = lookup_kiocb(ctx, iocb);
Christoph Hellwig888933f2018-05-23 14:11:02 +02001830 if (kiocb) {
1831 ret = kiocb->ki_cancel(&kiocb->rw);
1832 list_del_init(&kiocb->ki_list);
1833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 spin_unlock_irq(&ctx->ctx_lock);
1835
Kent Overstreet906b9732013-05-07 16:18:31 -07001836 if (!ret) {
Kent Overstreetbec68faa2013-05-13 14:45:08 -07001837 /*
1838 * The result argument is no longer used - the io_event is
1839 * always delivered via the ring buffer. -EINPROGRESS indicates
1840 * cancellation is progress:
Kent Overstreet906b9732013-05-07 16:18:31 -07001841 */
Kent Overstreetbec68faa2013-05-13 14:45:08 -07001842 ret = -EINPROGRESS;
Kent Overstreet906b9732013-05-07 16:18:31 -07001843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Kent Overstreet723be6e2013-05-28 15:14:48 -07001845 percpu_ref_put(&ctx->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 return ret;
1848}
1849
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07001850static long do_io_getevents(aio_context_t ctx_id,
1851 long min_nr,
1852 long nr,
1853 struct io_event __user *events,
1854 struct timespec64 *ts)
1855{
1856 ktime_t until = ts ? timespec64_to_ktime(*ts) : KTIME_MAX;
1857 struct kioctx *ioctx = lookup_ioctx(ctx_id);
1858 long ret = -EINVAL;
1859
1860 if (likely(ioctx)) {
1861 if (likely(min_nr <= nr && min_nr >= 0))
1862 ret = read_events(ioctx, min_nr, nr, events, until);
1863 percpu_ref_put(&ioctx->users);
1864 }
1865
1866 return ret;
1867}
1868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869/* io_getevents:
1870 * Attempts to read at least min_nr events and up to nr events from
Satoru Takeuchi642b5122010-08-05 11:23:11 -07001871 * the completion queue for the aio_context specified by ctx_id. If
1872 * it succeeds, the number of read events is returned. May fail with
1873 * -EINVAL if ctx_id is invalid, if min_nr is out of range, if nr is
1874 * out of range, if timeout is out of range. May fail with -EFAULT
1875 * if any of the memory specified is invalid. May return 0 or
1876 * < min_nr if the timeout specified by timeout has elapsed
1877 * before sufficient events are available, where timeout == NULL
1878 * specifies an infinite timeout. Note that the timeout pointed to by
Jeff Moyer69008072013-05-24 15:55:24 -07001879 * timeout is relative. Will fail with -ENOSYS if not implemented.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001881SYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id,
1882 long, min_nr,
1883 long, nr,
1884 struct io_event __user *, events,
1885 struct timespec __user *, timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07001887 struct timespec64 ts;
Christoph Hellwig7a074e92018-05-02 19:51:00 +02001888 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Christoph Hellwig7a074e92018-05-02 19:51:00 +02001890 if (timeout && unlikely(get_timespec64(&ts, timeout)))
1891 return -EFAULT;
1892
1893 ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &ts : NULL);
1894 if (!ret && signal_pending(current))
1895 ret = -EINTR;
1896 return ret;
1897}
1898
Christoph Hellwig9ba546c2018-07-11 15:48:46 +02001899struct __aio_sigset {
1900 const sigset_t __user *sigmask;
1901 size_t sigsetsize;
1902};
1903
Christoph Hellwig7a074e92018-05-02 19:51:00 +02001904SYSCALL_DEFINE6(io_pgetevents,
1905 aio_context_t, ctx_id,
1906 long, min_nr,
1907 long, nr,
1908 struct io_event __user *, events,
1909 struct timespec __user *, timeout,
1910 const struct __aio_sigset __user *, usig)
1911{
1912 struct __aio_sigset ksig = { NULL, };
1913 sigset_t ksigmask, sigsaved;
1914 struct timespec64 ts;
1915 int ret;
1916
1917 if (timeout && unlikely(get_timespec64(&ts, timeout)))
1918 return -EFAULT;
1919
1920 if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
1921 return -EFAULT;
1922
1923 if (ksig.sigmask) {
1924 if (ksig.sigsetsize != sizeof(sigset_t))
1925 return -EINVAL;
1926 if (copy_from_user(&ksigmask, ksig.sigmask, sizeof(ksigmask)))
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07001927 return -EFAULT;
Christoph Hellwig7a074e92018-05-02 19:51:00 +02001928 sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
1929 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 }
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07001931
Christoph Hellwig7a074e92018-05-02 19:51:00 +02001932 ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &ts : NULL);
1933 if (signal_pending(current)) {
1934 if (ksig.sigmask) {
1935 current->saved_sigmask = sigsaved;
1936 set_restore_sigmask();
1937 }
1938
1939 if (!ret)
1940 ret = -ERESTARTNOHAND;
1941 } else {
1942 if (ksig.sigmask)
1943 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1944 }
1945
1946 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947}
Al Viroc00d2c72016-12-20 07:04:57 -05001948
1949#ifdef CONFIG_COMPAT
1950COMPAT_SYSCALL_DEFINE5(io_getevents, compat_aio_context_t, ctx_id,
1951 compat_long_t, min_nr,
1952 compat_long_t, nr,
1953 struct io_event __user *, events,
1954 struct compat_timespec __user *, timeout)
1955{
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07001956 struct timespec64 t;
Christoph Hellwig7a074e92018-05-02 19:51:00 +02001957 int ret;
Al Viroc00d2c72016-12-20 07:04:57 -05001958
Christoph Hellwig7a074e92018-05-02 19:51:00 +02001959 if (timeout && compat_get_timespec64(&t, timeout))
1960 return -EFAULT;
1961
1962 ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &t : NULL);
1963 if (!ret && signal_pending(current))
1964 ret = -EINTR;
1965 return ret;
1966}
1967
1968
1969struct __compat_aio_sigset {
1970 compat_sigset_t __user *sigmask;
1971 compat_size_t sigsetsize;
1972};
1973
1974COMPAT_SYSCALL_DEFINE6(io_pgetevents,
1975 compat_aio_context_t, ctx_id,
1976 compat_long_t, min_nr,
1977 compat_long_t, nr,
1978 struct io_event __user *, events,
1979 struct compat_timespec __user *, timeout,
1980 const struct __compat_aio_sigset __user *, usig)
1981{
1982 struct __compat_aio_sigset ksig = { NULL, };
1983 sigset_t ksigmask, sigsaved;
1984 struct timespec64 t;
1985 int ret;
1986
1987 if (timeout && compat_get_timespec64(&t, timeout))
1988 return -EFAULT;
1989
1990 if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
1991 return -EFAULT;
1992
1993 if (ksig.sigmask) {
1994 if (ksig.sigsetsize != sizeof(compat_sigset_t))
1995 return -EINVAL;
1996 if (get_compat_sigset(&ksigmask, ksig.sigmask))
Al Viroc00d2c72016-12-20 07:04:57 -05001997 return -EFAULT;
Christoph Hellwig7a074e92018-05-02 19:51:00 +02001998 sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
1999 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
Al Viroc00d2c72016-12-20 07:04:57 -05002000 }
Deepa Dinamanifa2e62a2017-08-04 21:12:32 -07002001
Christoph Hellwig7a074e92018-05-02 19:51:00 +02002002 ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &t : NULL);
2003 if (signal_pending(current)) {
2004 if (ksig.sigmask) {
2005 current->saved_sigmask = sigsaved;
2006 set_restore_sigmask();
2007 }
2008 if (!ret)
2009 ret = -ERESTARTNOHAND;
2010 } else {
2011 if (ksig.sigmask)
2012 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2013 }
2014
2015 return ret;
Al Viroc00d2c72016-12-20 07:04:57 -05002016}
2017#endif