18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci *	An async IO implementation for Linux
38c2ecf20Sopenharmony_ci *	Written by Benjamin LaHaise <bcrl@kvack.org>
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *	Implements an efficient asynchronous io interface.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *	Copyright 2000, 2001, 2002 Red Hat, Inc.  All Rights Reserved.
88c2ecf20Sopenharmony_ci *	Copyright 2018 Christoph Hellwig.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *	See ../COPYING for licensing terms.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "%s: " fmt, __func__
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/kernel.h>
158c2ecf20Sopenharmony_ci#include <linux/init.h>
168c2ecf20Sopenharmony_ci#include <linux/errno.h>
178c2ecf20Sopenharmony_ci#include <linux/time.h>
188c2ecf20Sopenharmony_ci#include <linux/aio_abi.h>
198c2ecf20Sopenharmony_ci#include <linux/export.h>
208c2ecf20Sopenharmony_ci#include <linux/syscalls.h>
218c2ecf20Sopenharmony_ci#include <linux/backing-dev.h>
228c2ecf20Sopenharmony_ci#include <linux/refcount.h>
238c2ecf20Sopenharmony_ci#include <linux/uio.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include <linux/sched/signal.h>
268c2ecf20Sopenharmony_ci#include <linux/fs.h>
278c2ecf20Sopenharmony_ci#include <linux/file.h>
288c2ecf20Sopenharmony_ci#include <linux/mm.h>
298c2ecf20Sopenharmony_ci#include <linux/mman.h>
308c2ecf20Sopenharmony_ci#include <linux/percpu.h>
318c2ecf20Sopenharmony_ci#include <linux/slab.h>
328c2ecf20Sopenharmony_ci#include <linux/timer.h>
338c2ecf20Sopenharmony_ci#include <linux/aio.h>
348c2ecf20Sopenharmony_ci#include <linux/highmem.h>
358c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
368c2ecf20Sopenharmony_ci#include <linux/security.h>
378c2ecf20Sopenharmony_ci#include <linux/eventfd.h>
388c2ecf20Sopenharmony_ci#include <linux/blkdev.h>
398c2ecf20Sopenharmony_ci#include <linux/compat.h>
408c2ecf20Sopenharmony_ci#include <linux/migrate.h>
418c2ecf20Sopenharmony_ci#include <linux/ramfs.h>
428c2ecf20Sopenharmony_ci#include <linux/percpu-refcount.h>
438c2ecf20Sopenharmony_ci#include <linux/mount.h>
448c2ecf20Sopenharmony_ci#include <linux/pseudo_fs.h>
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#include <asm/kmap_types.h>
478c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
488c2ecf20Sopenharmony_ci#include <linux/nospec.h>
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#include "internal.h"
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define KIOCB_KEY		0
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define AIO_RING_MAGIC			0xa10a10a1
558c2ecf20Sopenharmony_ci#define AIO_RING_COMPAT_FEATURES	1
568c2ecf20Sopenharmony_ci#define AIO_RING_INCOMPAT_FEATURES	0
578c2ecf20Sopenharmony_cistruct aio_ring {
588c2ecf20Sopenharmony_ci	unsigned	id;	/* kernel internal index number */
598c2ecf20Sopenharmony_ci	unsigned	nr;	/* number of io_events */
608c2ecf20Sopenharmony_ci	unsigned	head;	/* Written to by userland or under ring_lock
618c2ecf20Sopenharmony_ci				 * mutex by aio_read_events_ring(). */
628c2ecf20Sopenharmony_ci	unsigned	tail;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	unsigned	magic;
658c2ecf20Sopenharmony_ci	unsigned	compat_features;
668c2ecf20Sopenharmony_ci	unsigned	incompat_features;
678c2ecf20Sopenharmony_ci	unsigned	header_length;	/* size of aio_ring */
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	struct io_event		io_events[];
718c2ecf20Sopenharmony_ci}; /* 128 bytes + ring size */
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci/*
748c2ecf20Sopenharmony_ci * Plugging is meant to work with larger batches of IOs. If we don't
758c2ecf20Sopenharmony_ci * have more than the below, then don't bother setting up a plug.
768c2ecf20Sopenharmony_ci */
778c2ecf20Sopenharmony_ci#define AIO_PLUG_THRESHOLD	2
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#define AIO_RING_PAGES	8
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistruct kioctx_table {
828c2ecf20Sopenharmony_ci	struct rcu_head		rcu;
838c2ecf20Sopenharmony_ci	unsigned		nr;
848c2ecf20Sopenharmony_ci	struct kioctx __rcu	*table[];
858c2ecf20Sopenharmony_ci};
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistruct kioctx_cpu {
888c2ecf20Sopenharmony_ci	unsigned		reqs_available;
898c2ecf20Sopenharmony_ci};
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistruct ctx_rq_wait {
928c2ecf20Sopenharmony_ci	struct completion comp;
938c2ecf20Sopenharmony_ci	atomic_t count;
948c2ecf20Sopenharmony_ci};
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistruct kioctx {
978c2ecf20Sopenharmony_ci	struct percpu_ref	users;
988c2ecf20Sopenharmony_ci	atomic_t		dead;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	struct percpu_ref	reqs;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	unsigned long		user_id;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	struct __percpu kioctx_cpu *cpu;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	/*
1078c2ecf20Sopenharmony_ci	 * For percpu reqs_available, number of slots we move to/from global
1088c2ecf20Sopenharmony_ci	 * counter at a time:
1098c2ecf20Sopenharmony_ci	 */
1108c2ecf20Sopenharmony_ci	unsigned		req_batch;
1118c2ecf20Sopenharmony_ci	/*
1128c2ecf20Sopenharmony_ci	 * This is what userspace passed to io_setup(), it's not used for
1138c2ecf20Sopenharmony_ci	 * anything but counting against the global max_reqs quota.
1148c2ecf20Sopenharmony_ci	 *
1158c2ecf20Sopenharmony_ci	 * The real limit is nr_events - 1, which will be larger (see
1168c2ecf20Sopenharmony_ci	 * aio_setup_ring())
1178c2ecf20Sopenharmony_ci	 */
1188c2ecf20Sopenharmony_ci	unsigned		max_reqs;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	/* Size of ringbuffer, in units of struct io_event */
1218c2ecf20Sopenharmony_ci	unsigned		nr_events;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	unsigned long		mmap_base;
1248c2ecf20Sopenharmony_ci	unsigned long		mmap_size;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	struct page		**ring_pages;
1278c2ecf20Sopenharmony_ci	long			nr_pages;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	struct rcu_work		free_rwork;	/* see free_ioctx() */
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	/*
1328c2ecf20Sopenharmony_ci	 * signals when all in-flight requests are done
1338c2ecf20Sopenharmony_ci	 */
1348c2ecf20Sopenharmony_ci	struct ctx_rq_wait	*rq_wait;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	struct {
1378c2ecf20Sopenharmony_ci		/*
1388c2ecf20Sopenharmony_ci		 * This counts the number of available slots in the ringbuffer,
1398c2ecf20Sopenharmony_ci		 * so we avoid overflowing it: it's decremented (if positive)
1408c2ecf20Sopenharmony_ci		 * when allocating a kiocb and incremented when the resulting
1418c2ecf20Sopenharmony_ci		 * io_event is pulled off the ringbuffer.
1428c2ecf20Sopenharmony_ci		 *
1438c2ecf20Sopenharmony_ci		 * We batch accesses to it with a percpu version.
1448c2ecf20Sopenharmony_ci		 */
1458c2ecf20Sopenharmony_ci		atomic_t	reqs_available;
1468c2ecf20Sopenharmony_ci	} ____cacheline_aligned_in_smp;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	struct {
1498c2ecf20Sopenharmony_ci		spinlock_t	ctx_lock;
1508c2ecf20Sopenharmony_ci		struct list_head active_reqs;	/* used for cancellation */
1518c2ecf20Sopenharmony_ci	} ____cacheline_aligned_in_smp;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	struct {
1548c2ecf20Sopenharmony_ci		struct mutex	ring_lock;
1558c2ecf20Sopenharmony_ci		wait_queue_head_t wait;
1568c2ecf20Sopenharmony_ci	} ____cacheline_aligned_in_smp;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	struct {
1598c2ecf20Sopenharmony_ci		unsigned	tail;
1608c2ecf20Sopenharmony_ci		unsigned	completed_events;
1618c2ecf20Sopenharmony_ci		spinlock_t	completion_lock;
1628c2ecf20Sopenharmony_ci	} ____cacheline_aligned_in_smp;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	struct page		*internal_pages[AIO_RING_PAGES];
1658c2ecf20Sopenharmony_ci	struct file		*aio_ring_file;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	unsigned		id;
1688c2ecf20Sopenharmony_ci};
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci/*
1718c2ecf20Sopenharmony_ci * First field must be the file pointer in all the
1728c2ecf20Sopenharmony_ci * iocb unions! See also 'struct kiocb' in <linux/fs.h>
1738c2ecf20Sopenharmony_ci */
1748c2ecf20Sopenharmony_cistruct fsync_iocb {
1758c2ecf20Sopenharmony_ci	struct file		*file;
1768c2ecf20Sopenharmony_ci	struct work_struct	work;
1778c2ecf20Sopenharmony_ci	bool			datasync;
1788c2ecf20Sopenharmony_ci	struct cred		*creds;
1798c2ecf20Sopenharmony_ci};
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistruct poll_iocb {
1828c2ecf20Sopenharmony_ci	struct file		*file;
1838c2ecf20Sopenharmony_ci	struct wait_queue_head	*head;
1848c2ecf20Sopenharmony_ci	__poll_t		events;
1858c2ecf20Sopenharmony_ci	bool			cancelled;
1868c2ecf20Sopenharmony_ci	bool			work_scheduled;
1878c2ecf20Sopenharmony_ci	bool			work_need_resched;
1888c2ecf20Sopenharmony_ci	struct wait_queue_entry	wait;
1898c2ecf20Sopenharmony_ci	struct work_struct	work;
1908c2ecf20Sopenharmony_ci};
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci/*
1938c2ecf20Sopenharmony_ci * NOTE! Each of the iocb union members has the file pointer
1948c2ecf20Sopenharmony_ci * as the first entry in their struct definition. So you can
1958c2ecf20Sopenharmony_ci * access the file pointer through any of the sub-structs,
1968c2ecf20Sopenharmony_ci * or directly as just 'ki_filp' in this struct.
1978c2ecf20Sopenharmony_ci */
1988c2ecf20Sopenharmony_cistruct aio_kiocb {
1998c2ecf20Sopenharmony_ci	union {
2008c2ecf20Sopenharmony_ci		struct file		*ki_filp;
2018c2ecf20Sopenharmony_ci		struct kiocb		rw;
2028c2ecf20Sopenharmony_ci		struct fsync_iocb	fsync;
2038c2ecf20Sopenharmony_ci		struct poll_iocb	poll;
2048c2ecf20Sopenharmony_ci	};
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	struct kioctx		*ki_ctx;
2078c2ecf20Sopenharmony_ci	kiocb_cancel_fn		*ki_cancel;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	struct io_event		ki_res;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	struct list_head	ki_list;	/* the aio core uses this
2128c2ecf20Sopenharmony_ci						 * for cancellation */
2138c2ecf20Sopenharmony_ci	refcount_t		ki_refcnt;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	/*
2168c2ecf20Sopenharmony_ci	 * If the aio_resfd field of the userspace iocb is not zero,
2178c2ecf20Sopenharmony_ci	 * this is the underlying eventfd context to deliver events to.
2188c2ecf20Sopenharmony_ci	 */
2198c2ecf20Sopenharmony_ci	struct eventfd_ctx	*ki_eventfd;
2208c2ecf20Sopenharmony_ci};
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci/*------ sysctl variables----*/
2238c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(aio_nr_lock);
2248c2ecf20Sopenharmony_ciunsigned long aio_nr;		/* current system wide number of aio requests */
2258c2ecf20Sopenharmony_ciunsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
2268c2ecf20Sopenharmony_ci/*----end sysctl variables---*/
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistatic struct kmem_cache	*kiocb_cachep;
2298c2ecf20Sopenharmony_cistatic struct kmem_cache	*kioctx_cachep;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_cistatic struct vfsmount *aio_mnt;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_cistatic const struct file_operations aio_ring_fops;
2348c2ecf20Sopenharmony_cistatic const struct address_space_operations aio_ctx_aops;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic struct file *aio_private_file(struct kioctx *ctx, loff_t nr_pages)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	struct file *file;
2398c2ecf20Sopenharmony_ci	struct inode *inode = alloc_anon_inode(aio_mnt->mnt_sb);
2408c2ecf20Sopenharmony_ci	if (IS_ERR(inode))
2418c2ecf20Sopenharmony_ci		return ERR_CAST(inode);
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	inode->i_mapping->a_ops = &aio_ctx_aops;
2448c2ecf20Sopenharmony_ci	inode->i_mapping->private_data = ctx;
2458c2ecf20Sopenharmony_ci	inode->i_size = PAGE_SIZE * nr_pages;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	file = alloc_file_pseudo(inode, aio_mnt, "[aio]",
2488c2ecf20Sopenharmony_ci				O_RDWR, &aio_ring_fops);
2498c2ecf20Sopenharmony_ci	if (IS_ERR(file))
2508c2ecf20Sopenharmony_ci		iput(inode);
2518c2ecf20Sopenharmony_ci	return file;
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_cistatic int aio_init_fs_context(struct fs_context *fc)
2558c2ecf20Sopenharmony_ci{
2568c2ecf20Sopenharmony_ci	if (!init_pseudo(fc, AIO_RING_MAGIC))
2578c2ecf20Sopenharmony_ci		return -ENOMEM;
2588c2ecf20Sopenharmony_ci	fc->s_iflags |= SB_I_NOEXEC;
2598c2ecf20Sopenharmony_ci	return 0;
2608c2ecf20Sopenharmony_ci}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci/* aio_setup
2638c2ecf20Sopenharmony_ci *	Creates the slab caches used by the aio routines, panic on
2648c2ecf20Sopenharmony_ci *	failure as this is done early during the boot sequence.
2658c2ecf20Sopenharmony_ci */
2668c2ecf20Sopenharmony_cistatic int __init aio_setup(void)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	static struct file_system_type aio_fs = {
2698c2ecf20Sopenharmony_ci		.name		= "aio",
2708c2ecf20Sopenharmony_ci		.init_fs_context = aio_init_fs_context,
2718c2ecf20Sopenharmony_ci		.kill_sb	= kill_anon_super,
2728c2ecf20Sopenharmony_ci	};
2738c2ecf20Sopenharmony_ci	aio_mnt = kern_mount(&aio_fs);
2748c2ecf20Sopenharmony_ci	if (IS_ERR(aio_mnt))
2758c2ecf20Sopenharmony_ci		panic("Failed to create aio fs mount.");
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	kiocb_cachep = KMEM_CACHE(aio_kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
2788c2ecf20Sopenharmony_ci	kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
2798c2ecf20Sopenharmony_ci	return 0;
2808c2ecf20Sopenharmony_ci}
2818c2ecf20Sopenharmony_ci__initcall(aio_setup);
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_cistatic void put_aio_ring_file(struct kioctx *ctx)
2848c2ecf20Sopenharmony_ci{
2858c2ecf20Sopenharmony_ci	struct file *aio_ring_file = ctx->aio_ring_file;
2868c2ecf20Sopenharmony_ci	struct address_space *i_mapping;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	if (aio_ring_file) {
2898c2ecf20Sopenharmony_ci		truncate_setsize(file_inode(aio_ring_file), 0);
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci		/* Prevent further access to the kioctx from migratepages */
2928c2ecf20Sopenharmony_ci		i_mapping = aio_ring_file->f_mapping;
2938c2ecf20Sopenharmony_ci		spin_lock(&i_mapping->private_lock);
2948c2ecf20Sopenharmony_ci		i_mapping->private_data = NULL;
2958c2ecf20Sopenharmony_ci		ctx->aio_ring_file = NULL;
2968c2ecf20Sopenharmony_ci		spin_unlock(&i_mapping->private_lock);
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci		fput(aio_ring_file);
2998c2ecf20Sopenharmony_ci	}
3008c2ecf20Sopenharmony_ci}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_cistatic void aio_free_ring(struct kioctx *ctx)
3038c2ecf20Sopenharmony_ci{
3048c2ecf20Sopenharmony_ci	int i;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	/* Disconnect the kiotx from the ring file.  This prevents future
3078c2ecf20Sopenharmony_ci	 * accesses to the kioctx from page migration.
3088c2ecf20Sopenharmony_ci	 */
3098c2ecf20Sopenharmony_ci	put_aio_ring_file(ctx);
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	for (i = 0; i < ctx->nr_pages; i++) {
3128c2ecf20Sopenharmony_ci		struct page *page;
3138c2ecf20Sopenharmony_ci		pr_debug("pid(%d) [%d] page->count=%d\n", current->pid, i,
3148c2ecf20Sopenharmony_ci				page_count(ctx->ring_pages[i]));
3158c2ecf20Sopenharmony_ci		page = ctx->ring_pages[i];
3168c2ecf20Sopenharmony_ci		if (!page)
3178c2ecf20Sopenharmony_ci			continue;
3188c2ecf20Sopenharmony_ci		ctx->ring_pages[i] = NULL;
3198c2ecf20Sopenharmony_ci		put_page(page);
3208c2ecf20Sopenharmony_ci	}
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	if (ctx->ring_pages && ctx->ring_pages != ctx->internal_pages) {
3238c2ecf20Sopenharmony_ci		kfree(ctx->ring_pages);
3248c2ecf20Sopenharmony_ci		ctx->ring_pages = NULL;
3258c2ecf20Sopenharmony_ci	}
3268c2ecf20Sopenharmony_ci}
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_cistatic int aio_ring_mremap(struct vm_area_struct *vma)
3298c2ecf20Sopenharmony_ci{
3308c2ecf20Sopenharmony_ci	struct file *file = vma->vm_file;
3318c2ecf20Sopenharmony_ci	struct mm_struct *mm = vma->vm_mm;
3328c2ecf20Sopenharmony_ci	struct kioctx_table *table;
3338c2ecf20Sopenharmony_ci	int i, res = -EINVAL;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	spin_lock(&mm->ioctx_lock);
3368c2ecf20Sopenharmony_ci	rcu_read_lock();
3378c2ecf20Sopenharmony_ci	table = rcu_dereference(mm->ioctx_table);
3388c2ecf20Sopenharmony_ci	if (!table)
3398c2ecf20Sopenharmony_ci		goto out_unlock;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	for (i = 0; i < table->nr; i++) {
3428c2ecf20Sopenharmony_ci		struct kioctx *ctx;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci		ctx = rcu_dereference(table->table[i]);
3458c2ecf20Sopenharmony_ci		if (ctx && ctx->aio_ring_file == file) {
3468c2ecf20Sopenharmony_ci			if (!atomic_read(&ctx->dead)) {
3478c2ecf20Sopenharmony_ci				ctx->user_id = ctx->mmap_base = vma->vm_start;
3488c2ecf20Sopenharmony_ci				res = 0;
3498c2ecf20Sopenharmony_ci			}
3508c2ecf20Sopenharmony_ci			break;
3518c2ecf20Sopenharmony_ci		}
3528c2ecf20Sopenharmony_ci	}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ciout_unlock:
3558c2ecf20Sopenharmony_ci	rcu_read_unlock();
3568c2ecf20Sopenharmony_ci	spin_unlock(&mm->ioctx_lock);
3578c2ecf20Sopenharmony_ci	return res;
3588c2ecf20Sopenharmony_ci}
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_cistatic const struct vm_operations_struct aio_ring_vm_ops = {
3618c2ecf20Sopenharmony_ci	.mremap		= aio_ring_mremap,
3628c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_MMU)
3638c2ecf20Sopenharmony_ci	.fault		= filemap_fault,
3648c2ecf20Sopenharmony_ci	.map_pages	= filemap_map_pages,
3658c2ecf20Sopenharmony_ci	.page_mkwrite	= filemap_page_mkwrite,
3668c2ecf20Sopenharmony_ci#endif
3678c2ecf20Sopenharmony_ci};
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_cistatic int aio_ring_mmap(struct file *file, struct vm_area_struct *vma)
3708c2ecf20Sopenharmony_ci{
3718c2ecf20Sopenharmony_ci	vma->vm_flags |= VM_DONTEXPAND;
3728c2ecf20Sopenharmony_ci	vma->vm_ops = &aio_ring_vm_ops;
3738c2ecf20Sopenharmony_ci	return 0;
3748c2ecf20Sopenharmony_ci}
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_cistatic const struct file_operations aio_ring_fops = {
3778c2ecf20Sopenharmony_ci	.mmap = aio_ring_mmap,
3788c2ecf20Sopenharmony_ci};
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_MIGRATION)
3818c2ecf20Sopenharmony_cistatic int aio_migratepage(struct address_space *mapping, struct page *new,
3828c2ecf20Sopenharmony_ci			struct page *old, enum migrate_mode mode)
3838c2ecf20Sopenharmony_ci{
3848c2ecf20Sopenharmony_ci	struct kioctx *ctx;
3858c2ecf20Sopenharmony_ci	unsigned long flags;
3868c2ecf20Sopenharmony_ci	pgoff_t idx;
3878c2ecf20Sopenharmony_ci	int rc;
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	/*
3908c2ecf20Sopenharmony_ci	 * We cannot support the _NO_COPY case here, because copy needs to
3918c2ecf20Sopenharmony_ci	 * happen under the ctx->completion_lock. That does not work with the
3928c2ecf20Sopenharmony_ci	 * migration workflow of MIGRATE_SYNC_NO_COPY.
3938c2ecf20Sopenharmony_ci	 */
3948c2ecf20Sopenharmony_ci	if (mode == MIGRATE_SYNC_NO_COPY)
3958c2ecf20Sopenharmony_ci		return -EINVAL;
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	rc = 0;
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	/* mapping->private_lock here protects against the kioctx teardown.  */
4008c2ecf20Sopenharmony_ci	spin_lock(&mapping->private_lock);
4018c2ecf20Sopenharmony_ci	ctx = mapping->private_data;
4028c2ecf20Sopenharmony_ci	if (!ctx) {
4038c2ecf20Sopenharmony_ci		rc = -EINVAL;
4048c2ecf20Sopenharmony_ci		goto out;
4058c2ecf20Sopenharmony_ci	}
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	/* The ring_lock mutex.  The prevents aio_read_events() from writing
4088c2ecf20Sopenharmony_ci	 * to the ring's head, and prevents page migration from mucking in
4098c2ecf20Sopenharmony_ci	 * a partially initialized kiotx.
4108c2ecf20Sopenharmony_ci	 */
4118c2ecf20Sopenharmony_ci	if (!mutex_trylock(&ctx->ring_lock)) {
4128c2ecf20Sopenharmony_ci		rc = -EAGAIN;
4138c2ecf20Sopenharmony_ci		goto out;
4148c2ecf20Sopenharmony_ci	}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	idx = old->index;
4178c2ecf20Sopenharmony_ci	if (idx < (pgoff_t)ctx->nr_pages) {
4188c2ecf20Sopenharmony_ci		/* Make sure the old page hasn't already been changed */
4198c2ecf20Sopenharmony_ci		if (ctx->ring_pages[idx] != old)
4208c2ecf20Sopenharmony_ci			rc = -EAGAIN;
4218c2ecf20Sopenharmony_ci	} else
4228c2ecf20Sopenharmony_ci		rc = -EINVAL;
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	if (rc != 0)
4258c2ecf20Sopenharmony_ci		goto out_unlock;
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	/* Writeback must be complete */
4288c2ecf20Sopenharmony_ci	BUG_ON(PageWriteback(old));
4298c2ecf20Sopenharmony_ci	get_page(new);
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	rc = migrate_page_move_mapping(mapping, new, old, 1);
4328c2ecf20Sopenharmony_ci	if (rc != MIGRATEPAGE_SUCCESS) {
4338c2ecf20Sopenharmony_ci		put_page(new);
4348c2ecf20Sopenharmony_ci		goto out_unlock;
4358c2ecf20Sopenharmony_ci	}
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	/* Take completion_lock to prevent other writes to the ring buffer
4388c2ecf20Sopenharmony_ci	 * while the old page is copied to the new.  This prevents new
4398c2ecf20Sopenharmony_ci	 * events from being lost.
4408c2ecf20Sopenharmony_ci	 */
4418c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ctx->completion_lock, flags);
4428c2ecf20Sopenharmony_ci	migrate_page_copy(new, old);
4438c2ecf20Sopenharmony_ci	BUG_ON(ctx->ring_pages[idx] != old);
4448c2ecf20Sopenharmony_ci	ctx->ring_pages[idx] = new;
4458c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ctx->completion_lock, flags);
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	/* The old page is no longer accessible. */
4488c2ecf20Sopenharmony_ci	put_page(old);
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ciout_unlock:
4518c2ecf20Sopenharmony_ci	mutex_unlock(&ctx->ring_lock);
4528c2ecf20Sopenharmony_ciout:
4538c2ecf20Sopenharmony_ci	spin_unlock(&mapping->private_lock);
4548c2ecf20Sopenharmony_ci	return rc;
4558c2ecf20Sopenharmony_ci}
4568c2ecf20Sopenharmony_ci#endif
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_cistatic const struct address_space_operations aio_ctx_aops = {
4598c2ecf20Sopenharmony_ci	.set_page_dirty = __set_page_dirty_no_writeback,
4608c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_MIGRATION)
4618c2ecf20Sopenharmony_ci	.migratepage	= aio_migratepage,
4628c2ecf20Sopenharmony_ci#endif
4638c2ecf20Sopenharmony_ci};
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_cistatic int aio_setup_ring(struct kioctx *ctx, unsigned int nr_events)
4668c2ecf20Sopenharmony_ci{
4678c2ecf20Sopenharmony_ci	struct aio_ring *ring;
4688c2ecf20Sopenharmony_ci	struct mm_struct *mm = current->mm;
4698c2ecf20Sopenharmony_ci	unsigned long size, unused;
4708c2ecf20Sopenharmony_ci	int nr_pages;
4718c2ecf20Sopenharmony_ci	int i;
4728c2ecf20Sopenharmony_ci	struct file *file;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	/* Compensate for the ring buffer's head/tail overlap entry */
4758c2ecf20Sopenharmony_ci	nr_events += 2;	/* 1 is required, 2 for good luck */
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	size = sizeof(struct aio_ring);
4788c2ecf20Sopenharmony_ci	size += sizeof(struct io_event) * nr_events;
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	nr_pages = PFN_UP(size);
4818c2ecf20Sopenharmony_ci	if (nr_pages < 0)
4828c2ecf20Sopenharmony_ci		return -EINVAL;
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	file = aio_private_file(ctx, nr_pages);
4858c2ecf20Sopenharmony_ci	if (IS_ERR(file)) {
4868c2ecf20Sopenharmony_ci		ctx->aio_ring_file = NULL;
4878c2ecf20Sopenharmony_ci		return -ENOMEM;
4888c2ecf20Sopenharmony_ci	}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	ctx->aio_ring_file = file;
4918c2ecf20Sopenharmony_ci	nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring))
4928c2ecf20Sopenharmony_ci			/ sizeof(struct io_event);
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	ctx->ring_pages = ctx->internal_pages;
4958c2ecf20Sopenharmony_ci	if (nr_pages > AIO_RING_PAGES) {
4968c2ecf20Sopenharmony_ci		ctx->ring_pages = kcalloc(nr_pages, sizeof(struct page *),
4978c2ecf20Sopenharmony_ci					  GFP_KERNEL);
4988c2ecf20Sopenharmony_ci		if (!ctx->ring_pages) {
4998c2ecf20Sopenharmony_ci			put_aio_ring_file(ctx);
5008c2ecf20Sopenharmony_ci			return -ENOMEM;
5018c2ecf20Sopenharmony_ci		}
5028c2ecf20Sopenharmony_ci	}
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	for (i = 0; i < nr_pages; i++) {
5058c2ecf20Sopenharmony_ci		struct page *page;
5068c2ecf20Sopenharmony_ci		page = find_or_create_page(file->f_mapping,
5078c2ecf20Sopenharmony_ci					   i, GFP_HIGHUSER | __GFP_ZERO);
5088c2ecf20Sopenharmony_ci		if (!page)
5098c2ecf20Sopenharmony_ci			break;
5108c2ecf20Sopenharmony_ci		pr_debug("pid(%d) page[%d]->count=%d\n",
5118c2ecf20Sopenharmony_ci			 current->pid, i, page_count(page));
5128c2ecf20Sopenharmony_ci		SetPageUptodate(page);
5138c2ecf20Sopenharmony_ci		unlock_page(page);
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci		ctx->ring_pages[i] = page;
5168c2ecf20Sopenharmony_ci	}
5178c2ecf20Sopenharmony_ci	ctx->nr_pages = i;
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	if (unlikely(i != nr_pages)) {
5208c2ecf20Sopenharmony_ci		aio_free_ring(ctx);
5218c2ecf20Sopenharmony_ci		return -ENOMEM;
5228c2ecf20Sopenharmony_ci	}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	ctx->mmap_size = nr_pages * PAGE_SIZE;
5258c2ecf20Sopenharmony_ci	pr_debug("attempting mmap of %lu bytes\n", ctx->mmap_size);
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	if (mmap_write_lock_killable(mm)) {
5288c2ecf20Sopenharmony_ci		ctx->mmap_size = 0;
5298c2ecf20Sopenharmony_ci		aio_free_ring(ctx);
5308c2ecf20Sopenharmony_ci		return -EINTR;
5318c2ecf20Sopenharmony_ci	}
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	ctx->mmap_base = do_mmap(ctx->aio_ring_file, 0, ctx->mmap_size,
5348c2ecf20Sopenharmony_ci				 PROT_READ | PROT_WRITE,
5358c2ecf20Sopenharmony_ci				 MAP_SHARED, 0, &unused, NULL);
5368c2ecf20Sopenharmony_ci	mmap_write_unlock(mm);
5378c2ecf20Sopenharmony_ci	if (IS_ERR((void *)ctx->mmap_base)) {
5388c2ecf20Sopenharmony_ci		ctx->mmap_size = 0;
5398c2ecf20Sopenharmony_ci		aio_free_ring(ctx);
5408c2ecf20Sopenharmony_ci		return -ENOMEM;
5418c2ecf20Sopenharmony_ci	}
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	pr_debug("mmap address: 0x%08lx\n", ctx->mmap_base);
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	ctx->user_id = ctx->mmap_base;
5468c2ecf20Sopenharmony_ci	ctx->nr_events = nr_events; /* trusted copy */
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	ring = kmap_atomic(ctx->ring_pages[0]);
5498c2ecf20Sopenharmony_ci	ring->nr = nr_events;	/* user copy */
5508c2ecf20Sopenharmony_ci	ring->id = ~0U;
5518c2ecf20Sopenharmony_ci	ring->head = ring->tail = 0;
5528c2ecf20Sopenharmony_ci	ring->magic = AIO_RING_MAGIC;
5538c2ecf20Sopenharmony_ci	ring->compat_features = AIO_RING_COMPAT_FEATURES;
5548c2ecf20Sopenharmony_ci	ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
5558c2ecf20Sopenharmony_ci	ring->header_length = sizeof(struct aio_ring);
5568c2ecf20Sopenharmony_ci	kunmap_atomic(ring);
5578c2ecf20Sopenharmony_ci	flush_dcache_page(ctx->ring_pages[0]);
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	return 0;
5608c2ecf20Sopenharmony_ci}
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci#define AIO_EVENTS_PER_PAGE	(PAGE_SIZE / sizeof(struct io_event))
5638c2ecf20Sopenharmony_ci#define AIO_EVENTS_FIRST_PAGE	((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
5648c2ecf20Sopenharmony_ci#define AIO_EVENTS_OFFSET	(AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_civoid kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel)
5678c2ecf20Sopenharmony_ci{
5688c2ecf20Sopenharmony_ci	struct aio_kiocb *req;
5698c2ecf20Sopenharmony_ci	struct kioctx *ctx;
5708c2ecf20Sopenharmony_ci	unsigned long flags;
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	/*
5738c2ecf20Sopenharmony_ci	 * kiocb didn't come from aio or is neither a read nor a write, hence
5748c2ecf20Sopenharmony_ci	 * ignore it.
5758c2ecf20Sopenharmony_ci	 */
5768c2ecf20Sopenharmony_ci	if (!(iocb->ki_flags & IOCB_AIO_RW))
5778c2ecf20Sopenharmony_ci		return;
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	req = container_of(iocb, struct aio_kiocb, rw);
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(!list_empty(&req->ki_list)))
5828c2ecf20Sopenharmony_ci		return;
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	ctx = req->ki_ctx;
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ctx->ctx_lock, flags);
5878c2ecf20Sopenharmony_ci	list_add_tail(&req->ki_list, &ctx->active_reqs);
5888c2ecf20Sopenharmony_ci	req->ki_cancel = cancel;
5898c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ctx->ctx_lock, flags);
5908c2ecf20Sopenharmony_ci}
5918c2ecf20Sopenharmony_ciEXPORT_SYMBOL(kiocb_set_cancel_fn);
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci/*
5948c2ecf20Sopenharmony_ci * free_ioctx() should be RCU delayed to synchronize against the RCU
5958c2ecf20Sopenharmony_ci * protected lookup_ioctx() and also needs process context to call
5968c2ecf20Sopenharmony_ci * aio_free_ring().  Use rcu_work.
5978c2ecf20Sopenharmony_ci */
5988c2ecf20Sopenharmony_cistatic void free_ioctx(struct work_struct *work)
5998c2ecf20Sopenharmony_ci{
6008c2ecf20Sopenharmony_ci	struct kioctx *ctx = container_of(to_rcu_work(work), struct kioctx,
6018c2ecf20Sopenharmony_ci					  free_rwork);
6028c2ecf20Sopenharmony_ci	pr_debug("freeing %p\n", ctx);
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	aio_free_ring(ctx);
6058c2ecf20Sopenharmony_ci	free_percpu(ctx->cpu);
6068c2ecf20Sopenharmony_ci	percpu_ref_exit(&ctx->reqs);
6078c2ecf20Sopenharmony_ci	percpu_ref_exit(&ctx->users);
6088c2ecf20Sopenharmony_ci	kmem_cache_free(kioctx_cachep, ctx);
6098c2ecf20Sopenharmony_ci}
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_cistatic void free_ioctx_reqs(struct percpu_ref *ref)
6128c2ecf20Sopenharmony_ci{
6138c2ecf20Sopenharmony_ci	struct kioctx *ctx = container_of(ref, struct kioctx, reqs);
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	/* At this point we know that there are no any in-flight requests */
6168c2ecf20Sopenharmony_ci	if (ctx->rq_wait && atomic_dec_and_test(&ctx->rq_wait->count))
6178c2ecf20Sopenharmony_ci		complete(&ctx->rq_wait->comp);
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci	/* Synchronize against RCU protected table->table[] dereferences */
6208c2ecf20Sopenharmony_ci	INIT_RCU_WORK(&ctx->free_rwork, free_ioctx);
6218c2ecf20Sopenharmony_ci	queue_rcu_work(system_wq, &ctx->free_rwork);
6228c2ecf20Sopenharmony_ci}
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci/*
6258c2ecf20Sopenharmony_ci * When this function runs, the kioctx has been removed from the "hash table"
6268c2ecf20Sopenharmony_ci * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
6278c2ecf20Sopenharmony_ci * now it's safe to cancel any that need to be.
6288c2ecf20Sopenharmony_ci */
6298c2ecf20Sopenharmony_cistatic void free_ioctx_users(struct percpu_ref *ref)
6308c2ecf20Sopenharmony_ci{
6318c2ecf20Sopenharmony_ci	struct kioctx *ctx = container_of(ref, struct kioctx, users);
6328c2ecf20Sopenharmony_ci	struct aio_kiocb *req;
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	spin_lock_irq(&ctx->ctx_lock);
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	while (!list_empty(&ctx->active_reqs)) {
6378c2ecf20Sopenharmony_ci		req = list_first_entry(&ctx->active_reqs,
6388c2ecf20Sopenharmony_ci				       struct aio_kiocb, ki_list);
6398c2ecf20Sopenharmony_ci		req->ki_cancel(&req->rw);
6408c2ecf20Sopenharmony_ci		list_del_init(&req->ki_list);
6418c2ecf20Sopenharmony_ci	}
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	spin_unlock_irq(&ctx->ctx_lock);
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	percpu_ref_kill(&ctx->reqs);
6468c2ecf20Sopenharmony_ci	percpu_ref_put(&ctx->reqs);
6478c2ecf20Sopenharmony_ci}
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_cistatic int ioctx_add_table(struct kioctx *ctx, struct mm_struct *mm)
6508c2ecf20Sopenharmony_ci{
6518c2ecf20Sopenharmony_ci	unsigned i, new_nr;
6528c2ecf20Sopenharmony_ci	struct kioctx_table *table, *old;
6538c2ecf20Sopenharmony_ci	struct aio_ring *ring;
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	spin_lock(&mm->ioctx_lock);
6568c2ecf20Sopenharmony_ci	table = rcu_dereference_raw(mm->ioctx_table);
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	while (1) {
6598c2ecf20Sopenharmony_ci		if (table)
6608c2ecf20Sopenharmony_ci			for (i = 0; i < table->nr; i++)
6618c2ecf20Sopenharmony_ci				if (!rcu_access_pointer(table->table[i])) {
6628c2ecf20Sopenharmony_ci					ctx->id = i;
6638c2ecf20Sopenharmony_ci					rcu_assign_pointer(table->table[i], ctx);
6648c2ecf20Sopenharmony_ci					spin_unlock(&mm->ioctx_lock);
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci					/* While kioctx setup is in progress,
6678c2ecf20Sopenharmony_ci					 * we are protected from page migration
6688c2ecf20Sopenharmony_ci					 * changes ring_pages by ->ring_lock.
6698c2ecf20Sopenharmony_ci					 */
6708c2ecf20Sopenharmony_ci					ring = kmap_atomic(ctx->ring_pages[0]);
6718c2ecf20Sopenharmony_ci					ring->id = ctx->id;
6728c2ecf20Sopenharmony_ci					kunmap_atomic(ring);
6738c2ecf20Sopenharmony_ci					return 0;
6748c2ecf20Sopenharmony_ci				}
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci		new_nr = (table ? table->nr : 1) * 4;
6778c2ecf20Sopenharmony_ci		spin_unlock(&mm->ioctx_lock);
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci		table = kzalloc(sizeof(*table) + sizeof(struct kioctx *) *
6808c2ecf20Sopenharmony_ci				new_nr, GFP_KERNEL);
6818c2ecf20Sopenharmony_ci		if (!table)
6828c2ecf20Sopenharmony_ci			return -ENOMEM;
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci		table->nr = new_nr;
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci		spin_lock(&mm->ioctx_lock);
6878c2ecf20Sopenharmony_ci		old = rcu_dereference_raw(mm->ioctx_table);
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci		if (!old) {
6908c2ecf20Sopenharmony_ci			rcu_assign_pointer(mm->ioctx_table, table);
6918c2ecf20Sopenharmony_ci		} else if (table->nr > old->nr) {
6928c2ecf20Sopenharmony_ci			memcpy(table->table, old->table,
6938c2ecf20Sopenharmony_ci			       old->nr * sizeof(struct kioctx *));
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci			rcu_assign_pointer(mm->ioctx_table, table);
6968c2ecf20Sopenharmony_ci			kfree_rcu(old, rcu);
6978c2ecf20Sopenharmony_ci		} else {
6988c2ecf20Sopenharmony_ci			kfree(table);
6998c2ecf20Sopenharmony_ci			table = old;
7008c2ecf20Sopenharmony_ci		}
7018c2ecf20Sopenharmony_ci	}
7028c2ecf20Sopenharmony_ci}
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_cistatic void aio_nr_sub(unsigned nr)
7058c2ecf20Sopenharmony_ci{
7068c2ecf20Sopenharmony_ci	spin_lock(&aio_nr_lock);
7078c2ecf20Sopenharmony_ci	if (WARN_ON(aio_nr - nr > aio_nr))
7088c2ecf20Sopenharmony_ci		aio_nr = 0;
7098c2ecf20Sopenharmony_ci	else
7108c2ecf20Sopenharmony_ci		aio_nr -= nr;
7118c2ecf20Sopenharmony_ci	spin_unlock(&aio_nr_lock);
7128c2ecf20Sopenharmony_ci}
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci/* ioctx_alloc
7158c2ecf20Sopenharmony_ci *	Allocates and initializes an ioctx.  Returns an ERR_PTR if it failed.
7168c2ecf20Sopenharmony_ci */
7178c2ecf20Sopenharmony_cistatic struct kioctx *ioctx_alloc(unsigned nr_events)
7188c2ecf20Sopenharmony_ci{
7198c2ecf20Sopenharmony_ci	struct mm_struct *mm = current->mm;
7208c2ecf20Sopenharmony_ci	struct kioctx *ctx;
7218c2ecf20Sopenharmony_ci	int err = -ENOMEM;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	/*
7248c2ecf20Sopenharmony_ci	 * Store the original nr_events -- what userspace passed to io_setup(),
7258c2ecf20Sopenharmony_ci	 * for counting against the global limit -- before it changes.
7268c2ecf20Sopenharmony_ci	 */
7278c2ecf20Sopenharmony_ci	unsigned int max_reqs = nr_events;
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	/*
7308c2ecf20Sopenharmony_ci	 * We keep track of the number of available ringbuffer slots, to prevent
7318c2ecf20Sopenharmony_ci	 * overflow (reqs_available), and we also use percpu counters for this.
7328c2ecf20Sopenharmony_ci	 *
7338c2ecf20Sopenharmony_ci	 * So since up to half the slots might be on other cpu's percpu counters
7348c2ecf20Sopenharmony_ci	 * and unavailable, double nr_events so userspace sees what they
7358c2ecf20Sopenharmony_ci	 * expected: additionally, we move req_batch slots to/from percpu
7368c2ecf20Sopenharmony_ci	 * counters at a time, so make sure that isn't 0:
7378c2ecf20Sopenharmony_ci	 */
7388c2ecf20Sopenharmony_ci	nr_events = max(nr_events, num_possible_cpus() * 4);
7398c2ecf20Sopenharmony_ci	nr_events *= 2;
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci	/* Prevent overflows */
7428c2ecf20Sopenharmony_ci	if (nr_events > (0x10000000U / sizeof(struct io_event))) {
7438c2ecf20Sopenharmony_ci		pr_debug("ENOMEM: nr_events too high\n");
7448c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
7458c2ecf20Sopenharmony_ci	}
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	if (!nr_events || (unsigned long)max_reqs > aio_max_nr)
7488c2ecf20Sopenharmony_ci		return ERR_PTR(-EAGAIN);
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
7518c2ecf20Sopenharmony_ci	if (!ctx)
7528c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	ctx->max_reqs = max_reqs;
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	spin_lock_init(&ctx->ctx_lock);
7578c2ecf20Sopenharmony_ci	spin_lock_init(&ctx->completion_lock);
7588c2ecf20Sopenharmony_ci	mutex_init(&ctx->ring_lock);
7598c2ecf20Sopenharmony_ci	/* Protect against page migration throughout kiotx setup by keeping
7608c2ecf20Sopenharmony_ci	 * the ring_lock mutex held until setup is complete. */
7618c2ecf20Sopenharmony_ci	mutex_lock(&ctx->ring_lock);
7628c2ecf20Sopenharmony_ci	init_waitqueue_head(&ctx->wait);
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&ctx->active_reqs);
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	if (percpu_ref_init(&ctx->users, free_ioctx_users, 0, GFP_KERNEL))
7678c2ecf20Sopenharmony_ci		goto err;
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	if (percpu_ref_init(&ctx->reqs, free_ioctx_reqs, 0, GFP_KERNEL))
7708c2ecf20Sopenharmony_ci		goto err;
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci	ctx->cpu = alloc_percpu(struct kioctx_cpu);
7738c2ecf20Sopenharmony_ci	if (!ctx->cpu)
7748c2ecf20Sopenharmony_ci		goto err;
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	err = aio_setup_ring(ctx, nr_events);
7778c2ecf20Sopenharmony_ci	if (err < 0)
7788c2ecf20Sopenharmony_ci		goto err;
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	atomic_set(&ctx->reqs_available, ctx->nr_events - 1);
7818c2ecf20Sopenharmony_ci	ctx->req_batch = (ctx->nr_events - 1) / (num_possible_cpus() * 4);
7828c2ecf20Sopenharmony_ci	if (ctx->req_batch < 1)
7838c2ecf20Sopenharmony_ci		ctx->req_batch = 1;
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	/* limit the number of system wide aios */
7868c2ecf20Sopenharmony_ci	spin_lock(&aio_nr_lock);
7878c2ecf20Sopenharmony_ci	if (aio_nr + ctx->max_reqs > aio_max_nr ||
7888c2ecf20Sopenharmony_ci	    aio_nr + ctx->max_reqs < aio_nr) {
7898c2ecf20Sopenharmony_ci		spin_unlock(&aio_nr_lock);
7908c2ecf20Sopenharmony_ci		err = -EAGAIN;
7918c2ecf20Sopenharmony_ci		goto err_ctx;
7928c2ecf20Sopenharmony_ci	}
7938c2ecf20Sopenharmony_ci	aio_nr += ctx->max_reqs;
7948c2ecf20Sopenharmony_ci	spin_unlock(&aio_nr_lock);
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	percpu_ref_get(&ctx->users);	/* io_setup() will drop this ref */
7978c2ecf20Sopenharmony_ci	percpu_ref_get(&ctx->reqs);	/* free_ioctx_users() will drop this */
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci	err = ioctx_add_table(ctx, mm);
8008c2ecf20Sopenharmony_ci	if (err)
8018c2ecf20Sopenharmony_ci		goto err_cleanup;
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	/* Release the ring_lock mutex now that all setup is complete. */
8048c2ecf20Sopenharmony_ci	mutex_unlock(&ctx->ring_lock);
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	pr_debug("allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
8078c2ecf20Sopenharmony_ci		 ctx, ctx->user_id, mm, ctx->nr_events);
8088c2ecf20Sopenharmony_ci	return ctx;
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_cierr_cleanup:
8118c2ecf20Sopenharmony_ci	aio_nr_sub(ctx->max_reqs);
8128c2ecf20Sopenharmony_cierr_ctx:
8138c2ecf20Sopenharmony_ci	atomic_set(&ctx->dead, 1);
8148c2ecf20Sopenharmony_ci	if (ctx->mmap_size)
8158c2ecf20Sopenharmony_ci		vm_munmap(ctx->mmap_base, ctx->mmap_size);
8168c2ecf20Sopenharmony_ci	aio_free_ring(ctx);
8178c2ecf20Sopenharmony_cierr:
8188c2ecf20Sopenharmony_ci	mutex_unlock(&ctx->ring_lock);
8198c2ecf20Sopenharmony_ci	free_percpu(ctx->cpu);
8208c2ecf20Sopenharmony_ci	percpu_ref_exit(&ctx->reqs);
8218c2ecf20Sopenharmony_ci	percpu_ref_exit(&ctx->users);
8228c2ecf20Sopenharmony_ci	kmem_cache_free(kioctx_cachep, ctx);
8238c2ecf20Sopenharmony_ci	pr_debug("error allocating ioctx %d\n", err);
8248c2ecf20Sopenharmony_ci	return ERR_PTR(err);
8258c2ecf20Sopenharmony_ci}
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci/* kill_ioctx
8288c2ecf20Sopenharmony_ci *	Cancels all outstanding aio requests on an aio context.  Used
8298c2ecf20Sopenharmony_ci *	when the processes owning a context have all exited to encourage
8308c2ecf20Sopenharmony_ci *	the rapid destruction of the kioctx.
8318c2ecf20Sopenharmony_ci */
8328c2ecf20Sopenharmony_cistatic int kill_ioctx(struct mm_struct *mm, struct kioctx *ctx,
8338c2ecf20Sopenharmony_ci		      struct ctx_rq_wait *wait)
8348c2ecf20Sopenharmony_ci{
8358c2ecf20Sopenharmony_ci	struct kioctx_table *table;
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	spin_lock(&mm->ioctx_lock);
8388c2ecf20Sopenharmony_ci	if (atomic_xchg(&ctx->dead, 1)) {
8398c2ecf20Sopenharmony_ci		spin_unlock(&mm->ioctx_lock);
8408c2ecf20Sopenharmony_ci		return -EINVAL;
8418c2ecf20Sopenharmony_ci	}
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci	table = rcu_dereference_raw(mm->ioctx_table);
8448c2ecf20Sopenharmony_ci	WARN_ON(ctx != rcu_access_pointer(table->table[ctx->id]));
8458c2ecf20Sopenharmony_ci	RCU_INIT_POINTER(table->table[ctx->id], NULL);
8468c2ecf20Sopenharmony_ci	spin_unlock(&mm->ioctx_lock);
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	/* free_ioctx_reqs() will do the necessary RCU synchronization */
8498c2ecf20Sopenharmony_ci	wake_up_all(&ctx->wait);
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci	/*
8528c2ecf20Sopenharmony_ci	 * It'd be more correct to do this in free_ioctx(), after all
8538c2ecf20Sopenharmony_ci	 * the outstanding kiocbs have finished - but by then io_destroy
8548c2ecf20Sopenharmony_ci	 * has already returned, so io_setup() could potentially return
8558c2ecf20Sopenharmony_ci	 * -EAGAIN with no ioctxs actually in use (as far as userspace
8568c2ecf20Sopenharmony_ci	 *  could tell).
8578c2ecf20Sopenharmony_ci	 */
8588c2ecf20Sopenharmony_ci	aio_nr_sub(ctx->max_reqs);
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci	if (ctx->mmap_size)
8618c2ecf20Sopenharmony_ci		vm_munmap(ctx->mmap_base, ctx->mmap_size);
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	ctx->rq_wait = wait;
8648c2ecf20Sopenharmony_ci	percpu_ref_kill(&ctx->users);
8658c2ecf20Sopenharmony_ci	return 0;
8668c2ecf20Sopenharmony_ci}
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci/*
8698c2ecf20Sopenharmony_ci * exit_aio: called when the last user of mm goes away.  At this point, there is
8708c2ecf20Sopenharmony_ci * no way for any new requests to be submited or any of the io_* syscalls to be
8718c2ecf20Sopenharmony_ci * called on the context.
8728c2ecf20Sopenharmony_ci *
8738c2ecf20Sopenharmony_ci * There may be outstanding kiocbs, but free_ioctx() will explicitly wait on
8748c2ecf20Sopenharmony_ci * them.
8758c2ecf20Sopenharmony_ci */
8768c2ecf20Sopenharmony_civoid exit_aio(struct mm_struct *mm)
8778c2ecf20Sopenharmony_ci{
8788c2ecf20Sopenharmony_ci	struct kioctx_table *table = rcu_dereference_raw(mm->ioctx_table);
8798c2ecf20Sopenharmony_ci	struct ctx_rq_wait wait;
8808c2ecf20Sopenharmony_ci	int i, skipped;
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	if (!table)
8838c2ecf20Sopenharmony_ci		return;
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	atomic_set(&wait.count, table->nr);
8868c2ecf20Sopenharmony_ci	init_completion(&wait.comp);
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci	skipped = 0;
8898c2ecf20Sopenharmony_ci	for (i = 0; i < table->nr; ++i) {
8908c2ecf20Sopenharmony_ci		struct kioctx *ctx =
8918c2ecf20Sopenharmony_ci			rcu_dereference_protected(table->table[i], true);
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_ci		if (!ctx) {
8948c2ecf20Sopenharmony_ci			skipped++;
8958c2ecf20Sopenharmony_ci			continue;
8968c2ecf20Sopenharmony_ci		}
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci		/*
8998c2ecf20Sopenharmony_ci		 * We don't need to bother with munmap() here - exit_mmap(mm)
9008c2ecf20Sopenharmony_ci		 * is coming and it'll unmap everything. And we simply can't,
9018c2ecf20Sopenharmony_ci		 * this is not necessarily our ->mm.
9028c2ecf20Sopenharmony_ci		 * Since kill_ioctx() uses non-zero ->mmap_size as indicator
9038c2ecf20Sopenharmony_ci		 * that it needs to unmap the area, just set it to 0.
9048c2ecf20Sopenharmony_ci		 */
9058c2ecf20Sopenharmony_ci		ctx->mmap_size = 0;
9068c2ecf20Sopenharmony_ci		kill_ioctx(mm, ctx, &wait);
9078c2ecf20Sopenharmony_ci	}
9088c2ecf20Sopenharmony_ci
9098c2ecf20Sopenharmony_ci	if (!atomic_sub_and_test(skipped, &wait.count)) {
9108c2ecf20Sopenharmony_ci		/* Wait until all IO for the context are done. */
9118c2ecf20Sopenharmony_ci		wait_for_completion(&wait.comp);
9128c2ecf20Sopenharmony_ci	}
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci	RCU_INIT_POINTER(mm->ioctx_table, NULL);
9158c2ecf20Sopenharmony_ci	kfree(table);
9168c2ecf20Sopenharmony_ci}
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_cistatic void put_reqs_available(struct kioctx *ctx, unsigned nr)
9198c2ecf20Sopenharmony_ci{
9208c2ecf20Sopenharmony_ci	struct kioctx_cpu *kcpu;
9218c2ecf20Sopenharmony_ci	unsigned long flags;
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	local_irq_save(flags);
9248c2ecf20Sopenharmony_ci	kcpu = this_cpu_ptr(ctx->cpu);
9258c2ecf20Sopenharmony_ci	kcpu->reqs_available += nr;
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_ci	while (kcpu->reqs_available >= ctx->req_batch * 2) {
9288c2ecf20Sopenharmony_ci		kcpu->reqs_available -= ctx->req_batch;
9298c2ecf20Sopenharmony_ci		atomic_add(ctx->req_batch, &ctx->reqs_available);
9308c2ecf20Sopenharmony_ci	}
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	local_irq_restore(flags);
9338c2ecf20Sopenharmony_ci}
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_cistatic bool __get_reqs_available(struct kioctx *ctx)
9368c2ecf20Sopenharmony_ci{
9378c2ecf20Sopenharmony_ci	struct kioctx_cpu *kcpu;
9388c2ecf20Sopenharmony_ci	bool ret = false;
9398c2ecf20Sopenharmony_ci	unsigned long flags;
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci	local_irq_save(flags);
9428c2ecf20Sopenharmony_ci	kcpu = this_cpu_ptr(ctx->cpu);
9438c2ecf20Sopenharmony_ci	if (!kcpu->reqs_available) {
9448c2ecf20Sopenharmony_ci		int old, avail = atomic_read(&ctx->reqs_available);
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci		do {
9478c2ecf20Sopenharmony_ci			if (avail < ctx->req_batch)
9488c2ecf20Sopenharmony_ci				goto out;
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci			old = avail;
9518c2ecf20Sopenharmony_ci			avail = atomic_cmpxchg(&ctx->reqs_available,
9528c2ecf20Sopenharmony_ci					       avail, avail - ctx->req_batch);
9538c2ecf20Sopenharmony_ci		} while (avail != old);
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ci		kcpu->reqs_available += ctx->req_batch;
9568c2ecf20Sopenharmony_ci	}
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci	ret = true;
9598c2ecf20Sopenharmony_ci	kcpu->reqs_available--;
9608c2ecf20Sopenharmony_ciout:
9618c2ecf20Sopenharmony_ci	local_irq_restore(flags);
9628c2ecf20Sopenharmony_ci	return ret;
9638c2ecf20Sopenharmony_ci}
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci/* refill_reqs_available
9668c2ecf20Sopenharmony_ci *	Updates the reqs_available reference counts used for tracking the
9678c2ecf20Sopenharmony_ci *	number of free slots in the completion ring.  This can be called
9688c2ecf20Sopenharmony_ci *	from aio_complete() (to optimistically update reqs_available) or
9698c2ecf20Sopenharmony_ci *	from aio_get_req() (the we're out of events case).  It must be
9708c2ecf20Sopenharmony_ci *	called holding ctx->completion_lock.
9718c2ecf20Sopenharmony_ci */
9728c2ecf20Sopenharmony_cistatic void refill_reqs_available(struct kioctx *ctx, unsigned head,
9738c2ecf20Sopenharmony_ci                                  unsigned tail)
9748c2ecf20Sopenharmony_ci{
9758c2ecf20Sopenharmony_ci	unsigned events_in_ring, completed;
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_ci	/* Clamp head since userland can write to it. */
9788c2ecf20Sopenharmony_ci	head %= ctx->nr_events;
9798c2ecf20Sopenharmony_ci	if (head <= tail)
9808c2ecf20Sopenharmony_ci		events_in_ring = tail - head;
9818c2ecf20Sopenharmony_ci	else
9828c2ecf20Sopenharmony_ci		events_in_ring = ctx->nr_events - (head - tail);
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_ci	completed = ctx->completed_events;
9858c2ecf20Sopenharmony_ci	if (events_in_ring < completed)
9868c2ecf20Sopenharmony_ci		completed -= events_in_ring;
9878c2ecf20Sopenharmony_ci	else
9888c2ecf20Sopenharmony_ci		completed = 0;
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci	if (!completed)
9918c2ecf20Sopenharmony_ci		return;
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_ci	ctx->completed_events -= completed;
9948c2ecf20Sopenharmony_ci	put_reqs_available(ctx, completed);
9958c2ecf20Sopenharmony_ci}
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_ci/* user_refill_reqs_available
9988c2ecf20Sopenharmony_ci *	Called to refill reqs_available when aio_get_req() encounters an
9998c2ecf20Sopenharmony_ci *	out of space in the completion ring.
10008c2ecf20Sopenharmony_ci */
10018c2ecf20Sopenharmony_cistatic void user_refill_reqs_available(struct kioctx *ctx)
10028c2ecf20Sopenharmony_ci{
10038c2ecf20Sopenharmony_ci	spin_lock_irq(&ctx->completion_lock);
10048c2ecf20Sopenharmony_ci	if (ctx->completed_events) {
10058c2ecf20Sopenharmony_ci		struct aio_ring *ring;
10068c2ecf20Sopenharmony_ci		unsigned head;
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci		/* Access of ring->head may race with aio_read_events_ring()
10098c2ecf20Sopenharmony_ci		 * here, but that's okay since whether we read the old version
10108c2ecf20Sopenharmony_ci		 * or the new version, and either will be valid.  The important
10118c2ecf20Sopenharmony_ci		 * part is that head cannot pass tail since we prevent
10128c2ecf20Sopenharmony_ci		 * aio_complete() from updating tail by holding
10138c2ecf20Sopenharmony_ci		 * ctx->completion_lock.  Even if head is invalid, the check
10148c2ecf20Sopenharmony_ci		 * against ctx->completed_events below will make sure we do the
10158c2ecf20Sopenharmony_ci		 * safe/right thing.
10168c2ecf20Sopenharmony_ci		 */
10178c2ecf20Sopenharmony_ci		ring = kmap_atomic(ctx->ring_pages[0]);
10188c2ecf20Sopenharmony_ci		head = ring->head;
10198c2ecf20Sopenharmony_ci		kunmap_atomic(ring);
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci		refill_reqs_available(ctx, head, ctx->tail);
10228c2ecf20Sopenharmony_ci	}
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci	spin_unlock_irq(&ctx->completion_lock);
10258c2ecf20Sopenharmony_ci}
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_cistatic bool get_reqs_available(struct kioctx *ctx)
10288c2ecf20Sopenharmony_ci{
10298c2ecf20Sopenharmony_ci	if (__get_reqs_available(ctx))
10308c2ecf20Sopenharmony_ci		return true;
10318c2ecf20Sopenharmony_ci	user_refill_reqs_available(ctx);
10328c2ecf20Sopenharmony_ci	return __get_reqs_available(ctx);
10338c2ecf20Sopenharmony_ci}
10348c2ecf20Sopenharmony_ci
10358c2ecf20Sopenharmony_ci/* aio_get_req
10368c2ecf20Sopenharmony_ci *	Allocate a slot for an aio request.
10378c2ecf20Sopenharmony_ci * Returns NULL if no requests are free.
10388c2ecf20Sopenharmony_ci *
10398c2ecf20Sopenharmony_ci * The refcount is initialized to 2 - one for the async op completion,
10408c2ecf20Sopenharmony_ci * one for the synchronous code that does this.
10418c2ecf20Sopenharmony_ci */
10428c2ecf20Sopenharmony_cistatic inline struct aio_kiocb *aio_get_req(struct kioctx *ctx)
10438c2ecf20Sopenharmony_ci{
10448c2ecf20Sopenharmony_ci	struct aio_kiocb *req;
10458c2ecf20Sopenharmony_ci
10468c2ecf20Sopenharmony_ci	req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL);
10478c2ecf20Sopenharmony_ci	if (unlikely(!req))
10488c2ecf20Sopenharmony_ci		return NULL;
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci	if (unlikely(!get_reqs_available(ctx))) {
10518c2ecf20Sopenharmony_ci		kmem_cache_free(kiocb_cachep, req);
10528c2ecf20Sopenharmony_ci		return NULL;
10538c2ecf20Sopenharmony_ci	}
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_ci	percpu_ref_get(&ctx->reqs);
10568c2ecf20Sopenharmony_ci	req->ki_ctx = ctx;
10578c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&req->ki_list);
10588c2ecf20Sopenharmony_ci	refcount_set(&req->ki_refcnt, 2);
10598c2ecf20Sopenharmony_ci	req->ki_eventfd = NULL;
10608c2ecf20Sopenharmony_ci	return req;
10618c2ecf20Sopenharmony_ci}
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_cistatic struct kioctx *lookup_ioctx(unsigned long ctx_id)
10648c2ecf20Sopenharmony_ci{
10658c2ecf20Sopenharmony_ci	struct aio_ring __user *ring  = (void __user *)ctx_id;
10668c2ecf20Sopenharmony_ci	struct mm_struct *mm = current->mm;
10678c2ecf20Sopenharmony_ci	struct kioctx *ctx, *ret = NULL;
10688c2ecf20Sopenharmony_ci	struct kioctx_table *table;
10698c2ecf20Sopenharmony_ci	unsigned id;
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ci	if (get_user(id, &ring->id))
10728c2ecf20Sopenharmony_ci		return NULL;
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_ci	rcu_read_lock();
10758c2ecf20Sopenharmony_ci	table = rcu_dereference(mm->ioctx_table);
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_ci	if (!table || id >= table->nr)
10788c2ecf20Sopenharmony_ci		goto out;
10798c2ecf20Sopenharmony_ci
10808c2ecf20Sopenharmony_ci	id = array_index_nospec(id, table->nr);
10818c2ecf20Sopenharmony_ci	ctx = rcu_dereference(table->table[id]);
10828c2ecf20Sopenharmony_ci	if (ctx && ctx->user_id == ctx_id) {
10838c2ecf20Sopenharmony_ci		if (percpu_ref_tryget_live(&ctx->users))
10848c2ecf20Sopenharmony_ci			ret = ctx;
10858c2ecf20Sopenharmony_ci	}
10868c2ecf20Sopenharmony_ciout:
10878c2ecf20Sopenharmony_ci	rcu_read_unlock();
10888c2ecf20Sopenharmony_ci	return ret;
10898c2ecf20Sopenharmony_ci}
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_cistatic inline void iocb_destroy(struct aio_kiocb *iocb)
10928c2ecf20Sopenharmony_ci{
10938c2ecf20Sopenharmony_ci	if (iocb->ki_eventfd)
10948c2ecf20Sopenharmony_ci		eventfd_ctx_put(iocb->ki_eventfd);
10958c2ecf20Sopenharmony_ci	if (iocb->ki_filp)
10968c2ecf20Sopenharmony_ci		fput(iocb->ki_filp);
10978c2ecf20Sopenharmony_ci	percpu_ref_put(&iocb->ki_ctx->reqs);
10988c2ecf20Sopenharmony_ci	kmem_cache_free(kiocb_cachep, iocb);
10998c2ecf20Sopenharmony_ci}
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci/* aio_complete
11028c2ecf20Sopenharmony_ci *	Called when the io request on the given iocb is complete.
11038c2ecf20Sopenharmony_ci */
11048c2ecf20Sopenharmony_cistatic void aio_complete(struct aio_kiocb *iocb)
11058c2ecf20Sopenharmony_ci{
11068c2ecf20Sopenharmony_ci	struct kioctx	*ctx = iocb->ki_ctx;
11078c2ecf20Sopenharmony_ci	struct aio_ring	*ring;
11088c2ecf20Sopenharmony_ci	struct io_event	*ev_page, *event;
11098c2ecf20Sopenharmony_ci	unsigned tail, pos, head;
11108c2ecf20Sopenharmony_ci	unsigned long	flags;
11118c2ecf20Sopenharmony_ci
11128c2ecf20Sopenharmony_ci	/*
11138c2ecf20Sopenharmony_ci	 * Add a completion event to the ring buffer. Must be done holding
11148c2ecf20Sopenharmony_ci	 * ctx->completion_lock to prevent other code from messing with the tail
11158c2ecf20Sopenharmony_ci	 * pointer since we might be called from irq context.
11168c2ecf20Sopenharmony_ci	 */
11178c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ctx->completion_lock, flags);
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_ci	tail = ctx->tail;
11208c2ecf20Sopenharmony_ci	pos = tail + AIO_EVENTS_OFFSET;
11218c2ecf20Sopenharmony_ci
11228c2ecf20Sopenharmony_ci	if (++tail >= ctx->nr_events)
11238c2ecf20Sopenharmony_ci		tail = 0;
11248c2ecf20Sopenharmony_ci
11258c2ecf20Sopenharmony_ci	ev_page = kmap_atomic(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
11268c2ecf20Sopenharmony_ci	event = ev_page + pos % AIO_EVENTS_PER_PAGE;
11278c2ecf20Sopenharmony_ci
11288c2ecf20Sopenharmony_ci	*event = iocb->ki_res;
11298c2ecf20Sopenharmony_ci
11308c2ecf20Sopenharmony_ci	kunmap_atomic(ev_page);
11318c2ecf20Sopenharmony_ci	flush_dcache_page(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci	pr_debug("%p[%u]: %p: %p %Lx %Lx %Lx\n", ctx, tail, iocb,
11348c2ecf20Sopenharmony_ci		 (void __user *)(unsigned long)iocb->ki_res.obj,
11358c2ecf20Sopenharmony_ci		 iocb->ki_res.data, iocb->ki_res.res, iocb->ki_res.res2);
11368c2ecf20Sopenharmony_ci
11378c2ecf20Sopenharmony_ci	/* after flagging the request as done, we
11388c2ecf20Sopenharmony_ci	 * must never even look at it again
11398c2ecf20Sopenharmony_ci	 */
11408c2ecf20Sopenharmony_ci	smp_wmb();	/* make event visible before updating tail */
11418c2ecf20Sopenharmony_ci
11428c2ecf20Sopenharmony_ci	ctx->tail = tail;
11438c2ecf20Sopenharmony_ci
11448c2ecf20Sopenharmony_ci	ring = kmap_atomic(ctx->ring_pages[0]);
11458c2ecf20Sopenharmony_ci	head = ring->head;
11468c2ecf20Sopenharmony_ci	ring->tail = tail;
11478c2ecf20Sopenharmony_ci	kunmap_atomic(ring);
11488c2ecf20Sopenharmony_ci	flush_dcache_page(ctx->ring_pages[0]);
11498c2ecf20Sopenharmony_ci
11508c2ecf20Sopenharmony_ci	ctx->completed_events++;
11518c2ecf20Sopenharmony_ci	if (ctx->completed_events > 1)
11528c2ecf20Sopenharmony_ci		refill_reqs_available(ctx, head, tail);
11538c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ctx->completion_lock, flags);
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_ci	pr_debug("added to ring %p at [%u]\n", iocb, tail);
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_ci	/*
11588c2ecf20Sopenharmony_ci	 * Check if the user asked us to deliver the result through an
11598c2ecf20Sopenharmony_ci	 * eventfd. The eventfd_signal() function is safe to be called
11608c2ecf20Sopenharmony_ci	 * from IRQ context.
11618c2ecf20Sopenharmony_ci	 */
11628c2ecf20Sopenharmony_ci	if (iocb->ki_eventfd)
11638c2ecf20Sopenharmony_ci		eventfd_signal(iocb->ki_eventfd, 1);
11648c2ecf20Sopenharmony_ci
11658c2ecf20Sopenharmony_ci	/*
11668c2ecf20Sopenharmony_ci	 * We have to order our ring_info tail store above and test
11678c2ecf20Sopenharmony_ci	 * of the wait list below outside the wait lock.  This is
11688c2ecf20Sopenharmony_ci	 * like in wake_up_bit() where clearing a bit has to be
11698c2ecf20Sopenharmony_ci	 * ordered with the unlocked test.
11708c2ecf20Sopenharmony_ci	 */
11718c2ecf20Sopenharmony_ci	smp_mb();
11728c2ecf20Sopenharmony_ci
11738c2ecf20Sopenharmony_ci	if (waitqueue_active(&ctx->wait))
11748c2ecf20Sopenharmony_ci		wake_up(&ctx->wait);
11758c2ecf20Sopenharmony_ci}
11768c2ecf20Sopenharmony_ci
11778c2ecf20Sopenharmony_cistatic inline void iocb_put(struct aio_kiocb *iocb)
11788c2ecf20Sopenharmony_ci{
11798c2ecf20Sopenharmony_ci	if (refcount_dec_and_test(&iocb->ki_refcnt)) {
11808c2ecf20Sopenharmony_ci		aio_complete(iocb);
11818c2ecf20Sopenharmony_ci		iocb_destroy(iocb);
11828c2ecf20Sopenharmony_ci	}
11838c2ecf20Sopenharmony_ci}
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ci/* aio_read_events_ring
11868c2ecf20Sopenharmony_ci *	Pull an event off of the ioctx's event ring.  Returns the number of
11878c2ecf20Sopenharmony_ci *	events fetched
11888c2ecf20Sopenharmony_ci */
11898c2ecf20Sopenharmony_cistatic long aio_read_events_ring(struct kioctx *ctx,
11908c2ecf20Sopenharmony_ci				 struct io_event __user *event, long nr)
11918c2ecf20Sopenharmony_ci{
11928c2ecf20Sopenharmony_ci	struct aio_ring *ring;
11938c2ecf20Sopenharmony_ci	unsigned head, tail, pos;
11948c2ecf20Sopenharmony_ci	long ret = 0;
11958c2ecf20Sopenharmony_ci	int copy_ret;
11968c2ecf20Sopenharmony_ci
11978c2ecf20Sopenharmony_ci	/*
11988c2ecf20Sopenharmony_ci	 * The mutex can block and wake us up and that will cause
11998c2ecf20Sopenharmony_ci	 * wait_event_interruptible_hrtimeout() to schedule without sleeping
12008c2ecf20Sopenharmony_ci	 * and repeat. This should be rare enough that it doesn't cause
12018c2ecf20Sopenharmony_ci	 * peformance issues. See the comment in read_events() for more detail.
12028c2ecf20Sopenharmony_ci	 */
12038c2ecf20Sopenharmony_ci	sched_annotate_sleep();
12048c2ecf20Sopenharmony_ci	mutex_lock(&ctx->ring_lock);
12058c2ecf20Sopenharmony_ci
12068c2ecf20Sopenharmony_ci	/* Access to ->ring_pages here is protected by ctx->ring_lock. */
12078c2ecf20Sopenharmony_ci	ring = kmap_atomic(ctx->ring_pages[0]);
12088c2ecf20Sopenharmony_ci	head = ring->head;
12098c2ecf20Sopenharmony_ci	tail = ring->tail;
12108c2ecf20Sopenharmony_ci	kunmap_atomic(ring);
12118c2ecf20Sopenharmony_ci
12128c2ecf20Sopenharmony_ci	/*
12138c2ecf20Sopenharmony_ci	 * Ensure that once we've read the current tail pointer, that
12148c2ecf20Sopenharmony_ci	 * we also see the events that were stored up to the tail.
12158c2ecf20Sopenharmony_ci	 */
12168c2ecf20Sopenharmony_ci	smp_rmb();
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ci	pr_debug("h%u t%u m%u\n", head, tail, ctx->nr_events);
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_ci	if (head == tail)
12218c2ecf20Sopenharmony_ci		goto out;
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci	head %= ctx->nr_events;
12248c2ecf20Sopenharmony_ci	tail %= ctx->nr_events;
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci	while (ret < nr) {
12278c2ecf20Sopenharmony_ci		long avail;
12288c2ecf20Sopenharmony_ci		struct io_event *ev;
12298c2ecf20Sopenharmony_ci		struct page *page;
12308c2ecf20Sopenharmony_ci
12318c2ecf20Sopenharmony_ci		avail = (head <= tail ?  tail : ctx->nr_events) - head;
12328c2ecf20Sopenharmony_ci		if (head == tail)
12338c2ecf20Sopenharmony_ci			break;
12348c2ecf20Sopenharmony_ci
12358c2ecf20Sopenharmony_ci		pos = head + AIO_EVENTS_OFFSET;
12368c2ecf20Sopenharmony_ci		page = ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE];
12378c2ecf20Sopenharmony_ci		pos %= AIO_EVENTS_PER_PAGE;
12388c2ecf20Sopenharmony_ci
12398c2ecf20Sopenharmony_ci		avail = min(avail, nr - ret);
12408c2ecf20Sopenharmony_ci		avail = min_t(long, avail, AIO_EVENTS_PER_PAGE - pos);
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci		ev = kmap(page);
12438c2ecf20Sopenharmony_ci		copy_ret = copy_to_user(event + ret, ev + pos,
12448c2ecf20Sopenharmony_ci					sizeof(*ev) * avail);
12458c2ecf20Sopenharmony_ci		kunmap(page);
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_ci		if (unlikely(copy_ret)) {
12488c2ecf20Sopenharmony_ci			ret = -EFAULT;
12498c2ecf20Sopenharmony_ci			goto out;
12508c2ecf20Sopenharmony_ci		}
12518c2ecf20Sopenharmony_ci
12528c2ecf20Sopenharmony_ci		ret += avail;
12538c2ecf20Sopenharmony_ci		head += avail;
12548c2ecf20Sopenharmony_ci		head %= ctx->nr_events;
12558c2ecf20Sopenharmony_ci	}
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_ci	ring = kmap_atomic(ctx->ring_pages[0]);
12588c2ecf20Sopenharmony_ci	ring->head = head;
12598c2ecf20Sopenharmony_ci	kunmap_atomic(ring);
12608c2ecf20Sopenharmony_ci	flush_dcache_page(ctx->ring_pages[0]);
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_ci	pr_debug("%li  h%u t%u\n", ret, head, tail);
12638c2ecf20Sopenharmony_ciout:
12648c2ecf20Sopenharmony_ci	mutex_unlock(&ctx->ring_lock);
12658c2ecf20Sopenharmony_ci
12668c2ecf20Sopenharmony_ci	return ret;
12678c2ecf20Sopenharmony_ci}
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_cistatic bool aio_read_events(struct kioctx *ctx, long min_nr, long nr,
12708c2ecf20Sopenharmony_ci			    struct io_event __user *event, long *i)
12718c2ecf20Sopenharmony_ci{
12728c2ecf20Sopenharmony_ci	long ret = aio_read_events_ring(ctx, event + *i, nr - *i);
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_ci	if (ret > 0)
12758c2ecf20Sopenharmony_ci		*i += ret;
12768c2ecf20Sopenharmony_ci
12778c2ecf20Sopenharmony_ci	if (unlikely(atomic_read(&ctx->dead)))
12788c2ecf20Sopenharmony_ci		ret = -EINVAL;
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_ci	if (!*i)
12818c2ecf20Sopenharmony_ci		*i = ret;
12828c2ecf20Sopenharmony_ci
12838c2ecf20Sopenharmony_ci	return ret < 0 || *i >= min_nr;
12848c2ecf20Sopenharmony_ci}
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_cistatic long read_events(struct kioctx *ctx, long min_nr, long nr,
12878c2ecf20Sopenharmony_ci			struct io_event __user *event,
12888c2ecf20Sopenharmony_ci			ktime_t until)
12898c2ecf20Sopenharmony_ci{
12908c2ecf20Sopenharmony_ci	long ret = 0;
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci	/*
12938c2ecf20Sopenharmony_ci	 * Note that aio_read_events() is being called as the conditional - i.e.
12948c2ecf20Sopenharmony_ci	 * we're calling it after prepare_to_wait() has set task state to
12958c2ecf20Sopenharmony_ci	 * TASK_INTERRUPTIBLE.
12968c2ecf20Sopenharmony_ci	 *
12978c2ecf20Sopenharmony_ci	 * But aio_read_events() can block, and if it blocks it's going to flip
12988c2ecf20Sopenharmony_ci	 * the task state back to TASK_RUNNING.
12998c2ecf20Sopenharmony_ci	 *
13008c2ecf20Sopenharmony_ci	 * This should be ok, provided it doesn't flip the state back to
13018c2ecf20Sopenharmony_ci	 * TASK_RUNNING and return 0 too much - that causes us to spin. That
13028c2ecf20Sopenharmony_ci	 * will only happen if the mutex_lock() call blocks, and we then find
13038c2ecf20Sopenharmony_ci	 * the ringbuffer empty. So in practice we should be ok, but it's
13048c2ecf20Sopenharmony_ci	 * something to be aware of when touching this code.
13058c2ecf20Sopenharmony_ci	 */
13068c2ecf20Sopenharmony_ci	if (until == 0)
13078c2ecf20Sopenharmony_ci		aio_read_events(ctx, min_nr, nr, event, &ret);
13088c2ecf20Sopenharmony_ci	else
13098c2ecf20Sopenharmony_ci		wait_event_interruptible_hrtimeout(ctx->wait,
13108c2ecf20Sopenharmony_ci				aio_read_events(ctx, min_nr, nr, event, &ret),
13118c2ecf20Sopenharmony_ci				until);
13128c2ecf20Sopenharmony_ci	return ret;
13138c2ecf20Sopenharmony_ci}
13148c2ecf20Sopenharmony_ci
13158c2ecf20Sopenharmony_ci/* sys_io_setup:
13168c2ecf20Sopenharmony_ci *	Create an aio_context capable of receiving at least nr_events.
13178c2ecf20Sopenharmony_ci *	ctxp must not point to an aio_context that already exists, and
13188c2ecf20Sopenharmony_ci *	must be initialized to 0 prior to the call.  On successful
13198c2ecf20Sopenharmony_ci *	creation of the aio_context, *ctxp is filled in with the resulting
13208c2ecf20Sopenharmony_ci *	handle.  May fail with -EINVAL if *ctxp is not initialized,
13218c2ecf20Sopenharmony_ci *	if the specified nr_events exceeds internal limits.  May fail
13228c2ecf20Sopenharmony_ci *	with -EAGAIN if the specified nr_events exceeds the user's limit
13238c2ecf20Sopenharmony_ci *	of available events.  May fail with -ENOMEM if insufficient kernel
13248c2ecf20Sopenharmony_ci *	resources are available.  May fail with -EFAULT if an invalid
13258c2ecf20Sopenharmony_ci *	pointer is passed for ctxp.  Will fail with -ENOSYS if not
13268c2ecf20Sopenharmony_ci *	implemented.
13278c2ecf20Sopenharmony_ci */
13288c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
13298c2ecf20Sopenharmony_ci{
13308c2ecf20Sopenharmony_ci	struct kioctx *ioctx = NULL;
13318c2ecf20Sopenharmony_ci	unsigned long ctx;
13328c2ecf20Sopenharmony_ci	long ret;
13338c2ecf20Sopenharmony_ci
13348c2ecf20Sopenharmony_ci	ret = get_user(ctx, ctxp);
13358c2ecf20Sopenharmony_ci	if (unlikely(ret))
13368c2ecf20Sopenharmony_ci		goto out;
13378c2ecf20Sopenharmony_ci
13388c2ecf20Sopenharmony_ci	ret = -EINVAL;
13398c2ecf20Sopenharmony_ci	if (unlikely(ctx || nr_events == 0)) {
13408c2ecf20Sopenharmony_ci		pr_debug("EINVAL: ctx %lu nr_events %u\n",
13418c2ecf20Sopenharmony_ci		         ctx, nr_events);
13428c2ecf20Sopenharmony_ci		goto out;
13438c2ecf20Sopenharmony_ci	}
13448c2ecf20Sopenharmony_ci
13458c2ecf20Sopenharmony_ci	ioctx = ioctx_alloc(nr_events);
13468c2ecf20Sopenharmony_ci	ret = PTR_ERR(ioctx);
13478c2ecf20Sopenharmony_ci	if (!IS_ERR(ioctx)) {
13488c2ecf20Sopenharmony_ci		ret = put_user(ioctx->user_id, ctxp);
13498c2ecf20Sopenharmony_ci		if (ret)
13508c2ecf20Sopenharmony_ci			kill_ioctx(current->mm, ioctx, NULL);
13518c2ecf20Sopenharmony_ci		percpu_ref_put(&ioctx->users);
13528c2ecf20Sopenharmony_ci	}
13538c2ecf20Sopenharmony_ci
13548c2ecf20Sopenharmony_ciout:
13558c2ecf20Sopenharmony_ci	return ret;
13568c2ecf20Sopenharmony_ci}
13578c2ecf20Sopenharmony_ci
13588c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT
13598c2ecf20Sopenharmony_ciCOMPAT_SYSCALL_DEFINE2(io_setup, unsigned, nr_events, u32 __user *, ctx32p)
13608c2ecf20Sopenharmony_ci{
13618c2ecf20Sopenharmony_ci	struct kioctx *ioctx = NULL;
13628c2ecf20Sopenharmony_ci	unsigned long ctx;
13638c2ecf20Sopenharmony_ci	long ret;
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_ci	ret = get_user(ctx, ctx32p);
13668c2ecf20Sopenharmony_ci	if (unlikely(ret))
13678c2ecf20Sopenharmony_ci		goto out;
13688c2ecf20Sopenharmony_ci
13698c2ecf20Sopenharmony_ci	ret = -EINVAL;
13708c2ecf20Sopenharmony_ci	if (unlikely(ctx || nr_events == 0)) {
13718c2ecf20Sopenharmony_ci		pr_debug("EINVAL: ctx %lu nr_events %u\n",
13728c2ecf20Sopenharmony_ci		         ctx, nr_events);
13738c2ecf20Sopenharmony_ci		goto out;
13748c2ecf20Sopenharmony_ci	}
13758c2ecf20Sopenharmony_ci
13768c2ecf20Sopenharmony_ci	ioctx = ioctx_alloc(nr_events);
13778c2ecf20Sopenharmony_ci	ret = PTR_ERR(ioctx);
13788c2ecf20Sopenharmony_ci	if (!IS_ERR(ioctx)) {
13798c2ecf20Sopenharmony_ci		/* truncating is ok because it's a user address */
13808c2ecf20Sopenharmony_ci		ret = put_user((u32)ioctx->user_id, ctx32p);
13818c2ecf20Sopenharmony_ci		if (ret)
13828c2ecf20Sopenharmony_ci			kill_ioctx(current->mm, ioctx, NULL);
13838c2ecf20Sopenharmony_ci		percpu_ref_put(&ioctx->users);
13848c2ecf20Sopenharmony_ci	}
13858c2ecf20Sopenharmony_ci
13868c2ecf20Sopenharmony_ciout:
13878c2ecf20Sopenharmony_ci	return ret;
13888c2ecf20Sopenharmony_ci}
13898c2ecf20Sopenharmony_ci#endif
13908c2ecf20Sopenharmony_ci
13918c2ecf20Sopenharmony_ci/* sys_io_destroy:
13928c2ecf20Sopenharmony_ci *	Destroy the aio_context specified.  May cancel any outstanding
13938c2ecf20Sopenharmony_ci *	AIOs and block on completion.  Will fail with -ENOSYS if not
13948c2ecf20Sopenharmony_ci *	implemented.  May fail with -EINVAL if the context pointed to
13958c2ecf20Sopenharmony_ci *	is invalid.
13968c2ecf20Sopenharmony_ci */
13978c2ecf20Sopenharmony_ciSYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
13988c2ecf20Sopenharmony_ci{
13998c2ecf20Sopenharmony_ci	struct kioctx *ioctx = lookup_ioctx(ctx);
14008c2ecf20Sopenharmony_ci	if (likely(NULL != ioctx)) {
14018c2ecf20Sopenharmony_ci		struct ctx_rq_wait wait;
14028c2ecf20Sopenharmony_ci		int ret;
14038c2ecf20Sopenharmony_ci
14048c2ecf20Sopenharmony_ci		init_completion(&wait.comp);
14058c2ecf20Sopenharmony_ci		atomic_set(&wait.count, 1);
14068c2ecf20Sopenharmony_ci
14078c2ecf20Sopenharmony_ci		/* Pass requests_done to kill_ioctx() where it can be set
14088c2ecf20Sopenharmony_ci		 * in a thread-safe way. If we try to set it here then we have
14098c2ecf20Sopenharmony_ci		 * a race condition if two io_destroy() called simultaneously.
14108c2ecf20Sopenharmony_ci		 */
14118c2ecf20Sopenharmony_ci		ret = kill_ioctx(current->mm, ioctx, &wait);
14128c2ecf20Sopenharmony_ci		percpu_ref_put(&ioctx->users);
14138c2ecf20Sopenharmony_ci
14148c2ecf20Sopenharmony_ci		/* Wait until all IO for the context are done. Otherwise kernel
14158c2ecf20Sopenharmony_ci		 * keep using user-space buffers even if user thinks the context
14168c2ecf20Sopenharmony_ci		 * is destroyed.
14178c2ecf20Sopenharmony_ci		 */
14188c2ecf20Sopenharmony_ci		if (!ret)
14198c2ecf20Sopenharmony_ci			wait_for_completion(&wait.comp);
14208c2ecf20Sopenharmony_ci
14218c2ecf20Sopenharmony_ci		return ret;
14228c2ecf20Sopenharmony_ci	}
14238c2ecf20Sopenharmony_ci	pr_debug("EINVAL: invalid context id\n");
14248c2ecf20Sopenharmony_ci	return -EINVAL;
14258c2ecf20Sopenharmony_ci}
14268c2ecf20Sopenharmony_ci
14278c2ecf20Sopenharmony_cistatic void aio_remove_iocb(struct aio_kiocb *iocb)
14288c2ecf20Sopenharmony_ci{
14298c2ecf20Sopenharmony_ci	struct kioctx *ctx = iocb->ki_ctx;
14308c2ecf20Sopenharmony_ci	unsigned long flags;
14318c2ecf20Sopenharmony_ci
14328c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ctx->ctx_lock, flags);
14338c2ecf20Sopenharmony_ci	list_del(&iocb->ki_list);
14348c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ctx->ctx_lock, flags);
14358c2ecf20Sopenharmony_ci}
14368c2ecf20Sopenharmony_ci
14378c2ecf20Sopenharmony_cistatic void aio_complete_rw(struct kiocb *kiocb, long res, long res2)
14388c2ecf20Sopenharmony_ci{
14398c2ecf20Sopenharmony_ci	struct aio_kiocb *iocb = container_of(kiocb, struct aio_kiocb, rw);
14408c2ecf20Sopenharmony_ci
14418c2ecf20Sopenharmony_ci	if (!list_empty_careful(&iocb->ki_list))
14428c2ecf20Sopenharmony_ci		aio_remove_iocb(iocb);
14438c2ecf20Sopenharmony_ci
14448c2ecf20Sopenharmony_ci	if (kiocb->ki_flags & IOCB_WRITE) {
14458c2ecf20Sopenharmony_ci		struct inode *inode = file_inode(kiocb->ki_filp);
14468c2ecf20Sopenharmony_ci
14478c2ecf20Sopenharmony_ci		/*
14488c2ecf20Sopenharmony_ci		 * Tell lockdep we inherited freeze protection from submission
14498c2ecf20Sopenharmony_ci		 * thread.
14508c2ecf20Sopenharmony_ci		 */
14518c2ecf20Sopenharmony_ci		if (S_ISREG(inode->i_mode))
14528c2ecf20Sopenharmony_ci			__sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
14538c2ecf20Sopenharmony_ci		file_end_write(kiocb->ki_filp);
14548c2ecf20Sopenharmony_ci	}
14558c2ecf20Sopenharmony_ci
14568c2ecf20Sopenharmony_ci	iocb->ki_res.res = res;
14578c2ecf20Sopenharmony_ci	iocb->ki_res.res2 = res2;
14588c2ecf20Sopenharmony_ci	iocb_put(iocb);
14598c2ecf20Sopenharmony_ci}
14608c2ecf20Sopenharmony_ci
14618c2ecf20Sopenharmony_cistatic int aio_prep_rw(struct kiocb *req, const struct iocb *iocb)
14628c2ecf20Sopenharmony_ci{
14638c2ecf20Sopenharmony_ci	int ret;
14648c2ecf20Sopenharmony_ci
14658c2ecf20Sopenharmony_ci	req->ki_complete = aio_complete_rw;
14668c2ecf20Sopenharmony_ci	req->private = NULL;
14678c2ecf20Sopenharmony_ci	req->ki_pos = iocb->aio_offset;
14688c2ecf20Sopenharmony_ci	req->ki_flags = iocb_flags(req->ki_filp) | IOCB_AIO_RW;
14698c2ecf20Sopenharmony_ci	if (iocb->aio_flags & IOCB_FLAG_RESFD)
14708c2ecf20Sopenharmony_ci		req->ki_flags |= IOCB_EVENTFD;
14718c2ecf20Sopenharmony_ci	req->ki_hint = ki_hint_validate(file_write_hint(req->ki_filp));
14728c2ecf20Sopenharmony_ci	if (iocb->aio_flags & IOCB_FLAG_IOPRIO) {
14738c2ecf20Sopenharmony_ci		/*
14748c2ecf20Sopenharmony_ci		 * If the IOCB_FLAG_IOPRIO flag of aio_flags is set, then
14758c2ecf20Sopenharmony_ci		 * aio_reqprio is interpreted as an I/O scheduling
14768c2ecf20Sopenharmony_ci		 * class and priority.
14778c2ecf20Sopenharmony_ci		 */
14788c2ecf20Sopenharmony_ci		ret = ioprio_check_cap(iocb->aio_reqprio);
14798c2ecf20Sopenharmony_ci		if (ret) {
14808c2ecf20Sopenharmony_ci			pr_debug("aio ioprio check cap error: %d\n", ret);
14818c2ecf20Sopenharmony_ci			return ret;
14828c2ecf20Sopenharmony_ci		}
14838c2ecf20Sopenharmony_ci
14848c2ecf20Sopenharmony_ci		req->ki_ioprio = iocb->aio_reqprio;
14858c2ecf20Sopenharmony_ci	} else
14868c2ecf20Sopenharmony_ci		req->ki_ioprio = get_current_ioprio();
14878c2ecf20Sopenharmony_ci
14888c2ecf20Sopenharmony_ci	ret = kiocb_set_rw_flags(req, iocb->aio_rw_flags);
14898c2ecf20Sopenharmony_ci	if (unlikely(ret))
14908c2ecf20Sopenharmony_ci		return ret;
14918c2ecf20Sopenharmony_ci
14928c2ecf20Sopenharmony_ci	req->ki_flags &= ~IOCB_HIPRI; /* no one is going to poll for this I/O */
14938c2ecf20Sopenharmony_ci	return 0;
14948c2ecf20Sopenharmony_ci}
14958c2ecf20Sopenharmony_ci
14968c2ecf20Sopenharmony_cistatic ssize_t aio_setup_rw(int rw, const struct iocb *iocb,
14978c2ecf20Sopenharmony_ci		struct iovec **iovec, bool vectored, bool compat,
14988c2ecf20Sopenharmony_ci		struct iov_iter *iter)
14998c2ecf20Sopenharmony_ci{
15008c2ecf20Sopenharmony_ci	void __user *buf = (void __user *)(uintptr_t)iocb->aio_buf;
15018c2ecf20Sopenharmony_ci	size_t len = iocb->aio_nbytes;
15028c2ecf20Sopenharmony_ci
15038c2ecf20Sopenharmony_ci	if (!vectored) {
15048c2ecf20Sopenharmony_ci		ssize_t ret = import_single_range(rw, buf, len, *iovec, iter);
15058c2ecf20Sopenharmony_ci		*iovec = NULL;
15068c2ecf20Sopenharmony_ci		return ret;
15078c2ecf20Sopenharmony_ci	}
15088c2ecf20Sopenharmony_ci
15098c2ecf20Sopenharmony_ci	return __import_iovec(rw, buf, len, UIO_FASTIOV, iovec, iter, compat);
15108c2ecf20Sopenharmony_ci}
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_cistatic inline void aio_rw_done(struct kiocb *req, ssize_t ret)
15138c2ecf20Sopenharmony_ci{
15148c2ecf20Sopenharmony_ci	switch (ret) {
15158c2ecf20Sopenharmony_ci	case -EIOCBQUEUED:
15168c2ecf20Sopenharmony_ci		break;
15178c2ecf20Sopenharmony_ci	case -ERESTARTSYS:
15188c2ecf20Sopenharmony_ci	case -ERESTARTNOINTR:
15198c2ecf20Sopenharmony_ci	case -ERESTARTNOHAND:
15208c2ecf20Sopenharmony_ci	case -ERESTART_RESTARTBLOCK:
15218c2ecf20Sopenharmony_ci		/*
15228c2ecf20Sopenharmony_ci		 * There's no easy way to restart the syscall since other AIO's
15238c2ecf20Sopenharmony_ci		 * may be already running. Just fail this IO with EINTR.
15248c2ecf20Sopenharmony_ci		 */
15258c2ecf20Sopenharmony_ci		ret = -EINTR;
15268c2ecf20Sopenharmony_ci		fallthrough;
15278c2ecf20Sopenharmony_ci	default:
15288c2ecf20Sopenharmony_ci		req->ki_complete(req, ret, 0);
15298c2ecf20Sopenharmony_ci	}
15308c2ecf20Sopenharmony_ci}
15318c2ecf20Sopenharmony_ci
15328c2ecf20Sopenharmony_cistatic int aio_read(struct kiocb *req, const struct iocb *iocb,
15338c2ecf20Sopenharmony_ci			bool vectored, bool compat)
15348c2ecf20Sopenharmony_ci{
15358c2ecf20Sopenharmony_ci	struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
15368c2ecf20Sopenharmony_ci	struct iov_iter iter;
15378c2ecf20Sopenharmony_ci	struct file *file;
15388c2ecf20Sopenharmony_ci	int ret;
15398c2ecf20Sopenharmony_ci
15408c2ecf20Sopenharmony_ci	ret = aio_prep_rw(req, iocb);
15418c2ecf20Sopenharmony_ci	if (ret)
15428c2ecf20Sopenharmony_ci		return ret;
15438c2ecf20Sopenharmony_ci	file = req->ki_filp;
15448c2ecf20Sopenharmony_ci	if (unlikely(!(file->f_mode & FMODE_READ)))
15458c2ecf20Sopenharmony_ci		return -EBADF;
15468c2ecf20Sopenharmony_ci	ret = -EINVAL;
15478c2ecf20Sopenharmony_ci	if (unlikely(!file->f_op->read_iter))
15488c2ecf20Sopenharmony_ci		return -EINVAL;
15498c2ecf20Sopenharmony_ci
15508c2ecf20Sopenharmony_ci	ret = aio_setup_rw(READ, iocb, &iovec, vectored, compat, &iter);
15518c2ecf20Sopenharmony_ci	if (ret < 0)
15528c2ecf20Sopenharmony_ci		return ret;
15538c2ecf20Sopenharmony_ci	ret = rw_verify_area(READ, file, &req->ki_pos, iov_iter_count(&iter));
15548c2ecf20Sopenharmony_ci	if (!ret)
15558c2ecf20Sopenharmony_ci		aio_rw_done(req, call_read_iter(file, req, &iter));
15568c2ecf20Sopenharmony_ci	kfree(iovec);
15578c2ecf20Sopenharmony_ci	return ret;
15588c2ecf20Sopenharmony_ci}
15598c2ecf20Sopenharmony_ci
15608c2ecf20Sopenharmony_cistatic int aio_write(struct kiocb *req, const struct iocb *iocb,
15618c2ecf20Sopenharmony_ci			 bool vectored, bool compat)
15628c2ecf20Sopenharmony_ci{
15638c2ecf20Sopenharmony_ci	struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
15648c2ecf20Sopenharmony_ci	struct iov_iter iter;
15658c2ecf20Sopenharmony_ci	struct file *file;
15668c2ecf20Sopenharmony_ci	int ret;
15678c2ecf20Sopenharmony_ci
15688c2ecf20Sopenharmony_ci	ret = aio_prep_rw(req, iocb);
15698c2ecf20Sopenharmony_ci	if (ret)
15708c2ecf20Sopenharmony_ci		return ret;
15718c2ecf20Sopenharmony_ci	file = req->ki_filp;
15728c2ecf20Sopenharmony_ci
15738c2ecf20Sopenharmony_ci	if (unlikely(!(file->f_mode & FMODE_WRITE)))
15748c2ecf20Sopenharmony_ci		return -EBADF;
15758c2ecf20Sopenharmony_ci	if (unlikely(!file->f_op->write_iter))
15768c2ecf20Sopenharmony_ci		return -EINVAL;
15778c2ecf20Sopenharmony_ci
15788c2ecf20Sopenharmony_ci	ret = aio_setup_rw(WRITE, iocb, &iovec, vectored, compat, &iter);
15798c2ecf20Sopenharmony_ci	if (ret < 0)
15808c2ecf20Sopenharmony_ci		return ret;
15818c2ecf20Sopenharmony_ci	ret = rw_verify_area(WRITE, file, &req->ki_pos, iov_iter_count(&iter));
15828c2ecf20Sopenharmony_ci	if (!ret) {
15838c2ecf20Sopenharmony_ci		/*
15848c2ecf20Sopenharmony_ci		 * Open-code file_start_write here to grab freeze protection,
15858c2ecf20Sopenharmony_ci		 * which will be released by another thread in
15868c2ecf20Sopenharmony_ci		 * aio_complete_rw().  Fool lockdep by telling it the lock got
15878c2ecf20Sopenharmony_ci		 * released so that it doesn't complain about the held lock when
15888c2ecf20Sopenharmony_ci		 * we return to userspace.
15898c2ecf20Sopenharmony_ci		 */
15908c2ecf20Sopenharmony_ci		if (S_ISREG(file_inode(file)->i_mode)) {
15918c2ecf20Sopenharmony_ci			sb_start_write(file_inode(file)->i_sb);
15928c2ecf20Sopenharmony_ci			__sb_writers_release(file_inode(file)->i_sb, SB_FREEZE_WRITE);
15938c2ecf20Sopenharmony_ci		}
15948c2ecf20Sopenharmony_ci		req->ki_flags |= IOCB_WRITE;
15958c2ecf20Sopenharmony_ci		aio_rw_done(req, call_write_iter(file, req, &iter));
15968c2ecf20Sopenharmony_ci	}
15978c2ecf20Sopenharmony_ci	kfree(iovec);
15988c2ecf20Sopenharmony_ci	return ret;
15998c2ecf20Sopenharmony_ci}
16008c2ecf20Sopenharmony_ci
16018c2ecf20Sopenharmony_cistatic void aio_fsync_work(struct work_struct *work)
16028c2ecf20Sopenharmony_ci{
16038c2ecf20Sopenharmony_ci	struct aio_kiocb *iocb = container_of(work, struct aio_kiocb, fsync.work);
16048c2ecf20Sopenharmony_ci	const struct cred *old_cred = override_creds(iocb->fsync.creds);
16058c2ecf20Sopenharmony_ci
16068c2ecf20Sopenharmony_ci	iocb->ki_res.res = vfs_fsync(iocb->fsync.file, iocb->fsync.datasync);
16078c2ecf20Sopenharmony_ci	revert_creds(old_cred);
16088c2ecf20Sopenharmony_ci	put_cred(iocb->fsync.creds);
16098c2ecf20Sopenharmony_ci	iocb_put(iocb);
16108c2ecf20Sopenharmony_ci}
16118c2ecf20Sopenharmony_ci
16128c2ecf20Sopenharmony_cistatic int aio_fsync(struct fsync_iocb *req, const struct iocb *iocb,
16138c2ecf20Sopenharmony_ci		     bool datasync)
16148c2ecf20Sopenharmony_ci{
16158c2ecf20Sopenharmony_ci	if (unlikely(iocb->aio_buf || iocb->aio_offset || iocb->aio_nbytes ||
16168c2ecf20Sopenharmony_ci			iocb->aio_rw_flags))
16178c2ecf20Sopenharmony_ci		return -EINVAL;
16188c2ecf20Sopenharmony_ci
16198c2ecf20Sopenharmony_ci	if (unlikely(!req->file->f_op->fsync))
16208c2ecf20Sopenharmony_ci		return -EINVAL;
16218c2ecf20Sopenharmony_ci
16228c2ecf20Sopenharmony_ci	req->creds = prepare_creds();
16238c2ecf20Sopenharmony_ci	if (!req->creds)
16248c2ecf20Sopenharmony_ci		return -ENOMEM;
16258c2ecf20Sopenharmony_ci
16268c2ecf20Sopenharmony_ci	req->datasync = datasync;
16278c2ecf20Sopenharmony_ci	INIT_WORK(&req->work, aio_fsync_work);
16288c2ecf20Sopenharmony_ci	schedule_work(&req->work);
16298c2ecf20Sopenharmony_ci	return 0;
16308c2ecf20Sopenharmony_ci}
16318c2ecf20Sopenharmony_ci
16328c2ecf20Sopenharmony_cistatic void aio_poll_put_work(struct work_struct *work)
16338c2ecf20Sopenharmony_ci{
16348c2ecf20Sopenharmony_ci	struct poll_iocb *req = container_of(work, struct poll_iocb, work);
16358c2ecf20Sopenharmony_ci	struct aio_kiocb *iocb = container_of(req, struct aio_kiocb, poll);
16368c2ecf20Sopenharmony_ci
16378c2ecf20Sopenharmony_ci	iocb_put(iocb);
16388c2ecf20Sopenharmony_ci}
16398c2ecf20Sopenharmony_ci
16408c2ecf20Sopenharmony_ci/*
16418c2ecf20Sopenharmony_ci * Safely lock the waitqueue which the request is on, synchronizing with the
16428c2ecf20Sopenharmony_ci * case where the ->poll() provider decides to free its waitqueue early.
16438c2ecf20Sopenharmony_ci *
16448c2ecf20Sopenharmony_ci * Returns true on success, meaning that req->head->lock was locked, req->wait
16458c2ecf20Sopenharmony_ci * is on req->head, and an RCU read lock was taken.  Returns false if the
16468c2ecf20Sopenharmony_ci * request was already removed from its waitqueue (which might no longer exist).
16478c2ecf20Sopenharmony_ci */
16488c2ecf20Sopenharmony_cistatic bool poll_iocb_lock_wq(struct poll_iocb *req)
16498c2ecf20Sopenharmony_ci{
16508c2ecf20Sopenharmony_ci	wait_queue_head_t *head;
16518c2ecf20Sopenharmony_ci
16528c2ecf20Sopenharmony_ci	/*
16538c2ecf20Sopenharmony_ci	 * While we hold the waitqueue lock and the waitqueue is nonempty,
16548c2ecf20Sopenharmony_ci	 * wake_up_pollfree() will wait for us.  However, taking the waitqueue
16558c2ecf20Sopenharmony_ci	 * lock in the first place can race with the waitqueue being freed.
16568c2ecf20Sopenharmony_ci	 *
16578c2ecf20Sopenharmony_ci	 * We solve this as eventpoll does: by taking advantage of the fact that
16588c2ecf20Sopenharmony_ci	 * all users of wake_up_pollfree() will RCU-delay the actual free.  If
16598c2ecf20Sopenharmony_ci	 * we enter rcu_read_lock() and see that the pointer to the queue is
16608c2ecf20Sopenharmony_ci	 * non-NULL, we can then lock it without the memory being freed out from
16618c2ecf20Sopenharmony_ci	 * under us, then check whether the request is still on the queue.
16628c2ecf20Sopenharmony_ci	 *
16638c2ecf20Sopenharmony_ci	 * Keep holding rcu_read_lock() as long as we hold the queue lock, in
16648c2ecf20Sopenharmony_ci	 * case the caller deletes the entry from the queue, leaving it empty.
16658c2ecf20Sopenharmony_ci	 * In that case, only RCU prevents the queue memory from being freed.
16668c2ecf20Sopenharmony_ci	 */
16678c2ecf20Sopenharmony_ci	rcu_read_lock();
16688c2ecf20Sopenharmony_ci	head = smp_load_acquire(&req->head);
16698c2ecf20Sopenharmony_ci	if (head) {
16708c2ecf20Sopenharmony_ci		spin_lock(&head->lock);
16718c2ecf20Sopenharmony_ci		if (!list_empty(&req->wait.entry))
16728c2ecf20Sopenharmony_ci			return true;
16738c2ecf20Sopenharmony_ci		spin_unlock(&head->lock);
16748c2ecf20Sopenharmony_ci	}
16758c2ecf20Sopenharmony_ci	rcu_read_unlock();
16768c2ecf20Sopenharmony_ci	return false;
16778c2ecf20Sopenharmony_ci}
16788c2ecf20Sopenharmony_ci
16798c2ecf20Sopenharmony_cistatic void poll_iocb_unlock_wq(struct poll_iocb *req)
16808c2ecf20Sopenharmony_ci{
16818c2ecf20Sopenharmony_ci	spin_unlock(&req->head->lock);
16828c2ecf20Sopenharmony_ci	rcu_read_unlock();
16838c2ecf20Sopenharmony_ci}
16848c2ecf20Sopenharmony_ci
16858c2ecf20Sopenharmony_cistatic void aio_poll_complete_work(struct work_struct *work)
16868c2ecf20Sopenharmony_ci{
16878c2ecf20Sopenharmony_ci	struct poll_iocb *req = container_of(work, struct poll_iocb, work);
16888c2ecf20Sopenharmony_ci	struct aio_kiocb *iocb = container_of(req, struct aio_kiocb, poll);
16898c2ecf20Sopenharmony_ci	struct poll_table_struct pt = { ._key = req->events };
16908c2ecf20Sopenharmony_ci	struct kioctx *ctx = iocb->ki_ctx;
16918c2ecf20Sopenharmony_ci	__poll_t mask = 0;
16928c2ecf20Sopenharmony_ci
16938c2ecf20Sopenharmony_ci	if (!READ_ONCE(req->cancelled))
16948c2ecf20Sopenharmony_ci		mask = vfs_poll(req->file, &pt) & req->events;
16958c2ecf20Sopenharmony_ci
16968c2ecf20Sopenharmony_ci	/*
16978c2ecf20Sopenharmony_ci	 * Note that ->ki_cancel callers also delete iocb from active_reqs after
16988c2ecf20Sopenharmony_ci	 * calling ->ki_cancel.  We need the ctx_lock roundtrip here to
16998c2ecf20Sopenharmony_ci	 * synchronize with them.  In the cancellation case the list_del_init
17008c2ecf20Sopenharmony_ci	 * itself is not actually needed, but harmless so we keep it in to
17018c2ecf20Sopenharmony_ci	 * avoid further branches in the fast path.
17028c2ecf20Sopenharmony_ci	 */
17038c2ecf20Sopenharmony_ci	spin_lock_irq(&ctx->ctx_lock);
17048c2ecf20Sopenharmony_ci	if (poll_iocb_lock_wq(req)) {
17058c2ecf20Sopenharmony_ci		if (!mask && !READ_ONCE(req->cancelled)) {
17068c2ecf20Sopenharmony_ci			/*
17078c2ecf20Sopenharmony_ci			 * The request isn't actually ready to be completed yet.
17088c2ecf20Sopenharmony_ci			 * Reschedule completion if another wakeup came in.
17098c2ecf20Sopenharmony_ci			 */
17108c2ecf20Sopenharmony_ci			if (req->work_need_resched) {
17118c2ecf20Sopenharmony_ci				schedule_work(&req->work);
17128c2ecf20Sopenharmony_ci				req->work_need_resched = false;
17138c2ecf20Sopenharmony_ci			} else {
17148c2ecf20Sopenharmony_ci				req->work_scheduled = false;
17158c2ecf20Sopenharmony_ci			}
17168c2ecf20Sopenharmony_ci			poll_iocb_unlock_wq(req);
17178c2ecf20Sopenharmony_ci			spin_unlock_irq(&ctx->ctx_lock);
17188c2ecf20Sopenharmony_ci			return;
17198c2ecf20Sopenharmony_ci		}
17208c2ecf20Sopenharmony_ci		list_del_init(&req->wait.entry);
17218c2ecf20Sopenharmony_ci		poll_iocb_unlock_wq(req);
17228c2ecf20Sopenharmony_ci	} /* else, POLLFREE has freed the waitqueue, so we must complete */
17238c2ecf20Sopenharmony_ci	list_del_init(&iocb->ki_list);
17248c2ecf20Sopenharmony_ci	iocb->ki_res.res = mangle_poll(mask);
17258c2ecf20Sopenharmony_ci	spin_unlock_irq(&ctx->ctx_lock);
17268c2ecf20Sopenharmony_ci
17278c2ecf20Sopenharmony_ci	iocb_put(iocb);
17288c2ecf20Sopenharmony_ci}
17298c2ecf20Sopenharmony_ci
17308c2ecf20Sopenharmony_ci/* assumes we are called with irqs disabled */
17318c2ecf20Sopenharmony_cistatic int aio_poll_cancel(struct kiocb *iocb)
17328c2ecf20Sopenharmony_ci{
17338c2ecf20Sopenharmony_ci	struct aio_kiocb *aiocb = container_of(iocb, struct aio_kiocb, rw);
17348c2ecf20Sopenharmony_ci	struct poll_iocb *req = &aiocb->poll;
17358c2ecf20Sopenharmony_ci
17368c2ecf20Sopenharmony_ci	if (poll_iocb_lock_wq(req)) {
17378c2ecf20Sopenharmony_ci		WRITE_ONCE(req->cancelled, true);
17388c2ecf20Sopenharmony_ci		if (!req->work_scheduled) {
17398c2ecf20Sopenharmony_ci			schedule_work(&aiocb->poll.work);
17408c2ecf20Sopenharmony_ci			req->work_scheduled = true;
17418c2ecf20Sopenharmony_ci		}
17428c2ecf20Sopenharmony_ci		poll_iocb_unlock_wq(req);
17438c2ecf20Sopenharmony_ci	} /* else, the request was force-cancelled by POLLFREE already */
17448c2ecf20Sopenharmony_ci
17458c2ecf20Sopenharmony_ci	return 0;
17468c2ecf20Sopenharmony_ci}
17478c2ecf20Sopenharmony_ci
17488c2ecf20Sopenharmony_cistatic int aio_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
17498c2ecf20Sopenharmony_ci		void *key)
17508c2ecf20Sopenharmony_ci{
17518c2ecf20Sopenharmony_ci	struct poll_iocb *req = container_of(wait, struct poll_iocb, wait);
17528c2ecf20Sopenharmony_ci	struct aio_kiocb *iocb = container_of(req, struct aio_kiocb, poll);
17538c2ecf20Sopenharmony_ci	__poll_t mask = key_to_poll(key);
17548c2ecf20Sopenharmony_ci	unsigned long flags;
17558c2ecf20Sopenharmony_ci
17568c2ecf20Sopenharmony_ci	/* for instances that support it check for an event match first: */
17578c2ecf20Sopenharmony_ci	if (mask && !(mask & req->events))
17588c2ecf20Sopenharmony_ci		return 0;
17598c2ecf20Sopenharmony_ci
17608c2ecf20Sopenharmony_ci	/*
17618c2ecf20Sopenharmony_ci	 * Complete the request inline if possible.  This requires that three
17628c2ecf20Sopenharmony_ci	 * conditions be met:
17638c2ecf20Sopenharmony_ci	 *   1. An event mask must have been passed.  If a plain wakeup was done
17648c2ecf20Sopenharmony_ci	 *	instead, then mask == 0 and we have to call vfs_poll() to get
17658c2ecf20Sopenharmony_ci	 *	the events, so inline completion isn't possible.
17668c2ecf20Sopenharmony_ci	 *   2. The completion work must not have already been scheduled.
17678c2ecf20Sopenharmony_ci	 *   3. ctx_lock must not be busy.  We have to use trylock because we
17688c2ecf20Sopenharmony_ci	 *	already hold the waitqueue lock, so this inverts the normal
17698c2ecf20Sopenharmony_ci	 *	locking order.  Use irqsave/irqrestore because not all
17708c2ecf20Sopenharmony_ci	 *	filesystems (e.g. fuse) call this function with IRQs disabled,
17718c2ecf20Sopenharmony_ci	 *	yet IRQs have to be disabled before ctx_lock is obtained.
17728c2ecf20Sopenharmony_ci	 */
17738c2ecf20Sopenharmony_ci	if (mask && !req->work_scheduled &&
17748c2ecf20Sopenharmony_ci	    spin_trylock_irqsave(&iocb->ki_ctx->ctx_lock, flags)) {
17758c2ecf20Sopenharmony_ci		struct kioctx *ctx = iocb->ki_ctx;
17768c2ecf20Sopenharmony_ci
17778c2ecf20Sopenharmony_ci		list_del_init(&req->wait.entry);
17788c2ecf20Sopenharmony_ci		list_del(&iocb->ki_list);
17798c2ecf20Sopenharmony_ci		iocb->ki_res.res = mangle_poll(mask);
17808c2ecf20Sopenharmony_ci		if (iocb->ki_eventfd && eventfd_signal_count()) {
17818c2ecf20Sopenharmony_ci			iocb = NULL;
17828c2ecf20Sopenharmony_ci			INIT_WORK(&req->work, aio_poll_put_work);
17838c2ecf20Sopenharmony_ci			schedule_work(&req->work);
17848c2ecf20Sopenharmony_ci		}
17858c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ctx->ctx_lock, flags);
17868c2ecf20Sopenharmony_ci		if (iocb)
17878c2ecf20Sopenharmony_ci			iocb_put(iocb);
17888c2ecf20Sopenharmony_ci	} else {
17898c2ecf20Sopenharmony_ci		/*
17908c2ecf20Sopenharmony_ci		 * Schedule the completion work if needed.  If it was already
17918c2ecf20Sopenharmony_ci		 * scheduled, record that another wakeup came in.
17928c2ecf20Sopenharmony_ci		 *
17938c2ecf20Sopenharmony_ci		 * Don't remove the request from the waitqueue here, as it might
17948c2ecf20Sopenharmony_ci		 * not actually be complete yet (we won't know until vfs_poll()
17958c2ecf20Sopenharmony_ci		 * is called), and we must not miss any wakeups.  POLLFREE is an
17968c2ecf20Sopenharmony_ci		 * exception to this; see below.
17978c2ecf20Sopenharmony_ci		 */
17988c2ecf20Sopenharmony_ci		if (req->work_scheduled) {
17998c2ecf20Sopenharmony_ci			req->work_need_resched = true;
18008c2ecf20Sopenharmony_ci		} else {
18018c2ecf20Sopenharmony_ci			schedule_work(&req->work);
18028c2ecf20Sopenharmony_ci			req->work_scheduled = true;
18038c2ecf20Sopenharmony_ci		}
18048c2ecf20Sopenharmony_ci
18058c2ecf20Sopenharmony_ci		/*
18068c2ecf20Sopenharmony_ci		 * If the waitqueue is being freed early but we can't complete
18078c2ecf20Sopenharmony_ci		 * the request inline, we have to tear down the request as best
18088c2ecf20Sopenharmony_ci		 * we can.  That means immediately removing the request from its
18098c2ecf20Sopenharmony_ci		 * waitqueue and preventing all further accesses to the
18108c2ecf20Sopenharmony_ci		 * waitqueue via the request.  We also need to schedule the
18118c2ecf20Sopenharmony_ci		 * completion work (done above).  Also mark the request as
18128c2ecf20Sopenharmony_ci		 * cancelled, to potentially skip an unneeded call to ->poll().
18138c2ecf20Sopenharmony_ci		 */
18148c2ecf20Sopenharmony_ci		if (mask & POLLFREE) {
18158c2ecf20Sopenharmony_ci			WRITE_ONCE(req->cancelled, true);
18168c2ecf20Sopenharmony_ci			list_del_init(&req->wait.entry);
18178c2ecf20Sopenharmony_ci
18188c2ecf20Sopenharmony_ci			/*
18198c2ecf20Sopenharmony_ci			 * Careful: this *must* be the last step, since as soon
18208c2ecf20Sopenharmony_ci			 * as req->head is NULL'ed out, the request can be
18218c2ecf20Sopenharmony_ci			 * completed and freed, since aio_poll_complete_work()
18228c2ecf20Sopenharmony_ci			 * will no longer need to take the waitqueue lock.
18238c2ecf20Sopenharmony_ci			 */
18248c2ecf20Sopenharmony_ci			smp_store_release(&req->head, NULL);
18258c2ecf20Sopenharmony_ci		}
18268c2ecf20Sopenharmony_ci	}
18278c2ecf20Sopenharmony_ci	return 1;
18288c2ecf20Sopenharmony_ci}
18298c2ecf20Sopenharmony_ci
18308c2ecf20Sopenharmony_cistruct aio_poll_table {
18318c2ecf20Sopenharmony_ci	struct poll_table_struct	pt;
18328c2ecf20Sopenharmony_ci	struct aio_kiocb		*iocb;
18338c2ecf20Sopenharmony_ci	bool				queued;
18348c2ecf20Sopenharmony_ci	int				error;
18358c2ecf20Sopenharmony_ci};
18368c2ecf20Sopenharmony_ci
18378c2ecf20Sopenharmony_cistatic void
18388c2ecf20Sopenharmony_ciaio_poll_queue_proc(struct file *file, struct wait_queue_head *head,
18398c2ecf20Sopenharmony_ci		struct poll_table_struct *p)
18408c2ecf20Sopenharmony_ci{
18418c2ecf20Sopenharmony_ci	struct aio_poll_table *pt = container_of(p, struct aio_poll_table, pt);
18428c2ecf20Sopenharmony_ci
18438c2ecf20Sopenharmony_ci	/* multiple wait queues per file are not supported */
18448c2ecf20Sopenharmony_ci	if (unlikely(pt->queued)) {
18458c2ecf20Sopenharmony_ci		pt->error = -EINVAL;
18468c2ecf20Sopenharmony_ci		return;
18478c2ecf20Sopenharmony_ci	}
18488c2ecf20Sopenharmony_ci
18498c2ecf20Sopenharmony_ci	pt->queued = true;
18508c2ecf20Sopenharmony_ci	pt->error = 0;
18518c2ecf20Sopenharmony_ci	pt->iocb->poll.head = head;
18528c2ecf20Sopenharmony_ci	add_wait_queue(head, &pt->iocb->poll.wait);
18538c2ecf20Sopenharmony_ci}
18548c2ecf20Sopenharmony_ci
18558c2ecf20Sopenharmony_cistatic int aio_poll(struct aio_kiocb *aiocb, const struct iocb *iocb)
18568c2ecf20Sopenharmony_ci{
18578c2ecf20Sopenharmony_ci	struct kioctx *ctx = aiocb->ki_ctx;
18588c2ecf20Sopenharmony_ci	struct poll_iocb *req = &aiocb->poll;
18598c2ecf20Sopenharmony_ci	struct aio_poll_table apt;
18608c2ecf20Sopenharmony_ci	bool cancel = false;
18618c2ecf20Sopenharmony_ci	__poll_t mask;
18628c2ecf20Sopenharmony_ci
18638c2ecf20Sopenharmony_ci	/* reject any unknown events outside the normal event mask. */
18648c2ecf20Sopenharmony_ci	if ((u16)iocb->aio_buf != iocb->aio_buf)
18658c2ecf20Sopenharmony_ci		return -EINVAL;
18668c2ecf20Sopenharmony_ci	/* reject fields that are not defined for poll */
18678c2ecf20Sopenharmony_ci	if (iocb->aio_offset || iocb->aio_nbytes || iocb->aio_rw_flags)
18688c2ecf20Sopenharmony_ci		return -EINVAL;
18698c2ecf20Sopenharmony_ci
18708c2ecf20Sopenharmony_ci	INIT_WORK(&req->work, aio_poll_complete_work);
18718c2ecf20Sopenharmony_ci	req->events = demangle_poll(iocb->aio_buf) | EPOLLERR | EPOLLHUP;
18728c2ecf20Sopenharmony_ci
18738c2ecf20Sopenharmony_ci	req->head = NULL;
18748c2ecf20Sopenharmony_ci	req->cancelled = false;
18758c2ecf20Sopenharmony_ci	req->work_scheduled = false;
18768c2ecf20Sopenharmony_ci	req->work_need_resched = false;
18778c2ecf20Sopenharmony_ci
18788c2ecf20Sopenharmony_ci	apt.pt._qproc = aio_poll_queue_proc;
18798c2ecf20Sopenharmony_ci	apt.pt._key = req->events;
18808c2ecf20Sopenharmony_ci	apt.iocb = aiocb;
18818c2ecf20Sopenharmony_ci	apt.queued = false;
18828c2ecf20Sopenharmony_ci	apt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
18838c2ecf20Sopenharmony_ci
18848c2ecf20Sopenharmony_ci	/* initialized the list so that we can do list_empty checks */
18858c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&req->wait.entry);
18868c2ecf20Sopenharmony_ci	init_waitqueue_func_entry(&req->wait, aio_poll_wake);
18878c2ecf20Sopenharmony_ci
18888c2ecf20Sopenharmony_ci	mask = vfs_poll(req->file, &apt.pt) & req->events;
18898c2ecf20Sopenharmony_ci	spin_lock_irq(&ctx->ctx_lock);
18908c2ecf20Sopenharmony_ci	if (likely(apt.queued)) {
18918c2ecf20Sopenharmony_ci		bool on_queue = poll_iocb_lock_wq(req);
18928c2ecf20Sopenharmony_ci
18938c2ecf20Sopenharmony_ci		if (!on_queue || req->work_scheduled) {
18948c2ecf20Sopenharmony_ci			/*
18958c2ecf20Sopenharmony_ci			 * aio_poll_wake() already either scheduled the async
18968c2ecf20Sopenharmony_ci			 * completion work, or completed the request inline.
18978c2ecf20Sopenharmony_ci			 */
18988c2ecf20Sopenharmony_ci			if (apt.error) /* unsupported case: multiple queues */
18998c2ecf20Sopenharmony_ci				cancel = true;
19008c2ecf20Sopenharmony_ci			apt.error = 0;
19018c2ecf20Sopenharmony_ci			mask = 0;
19028c2ecf20Sopenharmony_ci		}
19038c2ecf20Sopenharmony_ci		if (mask || apt.error) {
19048c2ecf20Sopenharmony_ci			/* Steal to complete synchronously. */
19058c2ecf20Sopenharmony_ci			list_del_init(&req->wait.entry);
19068c2ecf20Sopenharmony_ci		} else if (cancel) {
19078c2ecf20Sopenharmony_ci			/* Cancel if possible (may be too late though). */
19088c2ecf20Sopenharmony_ci			WRITE_ONCE(req->cancelled, true);
19098c2ecf20Sopenharmony_ci		} else if (on_queue) {
19108c2ecf20Sopenharmony_ci			/*
19118c2ecf20Sopenharmony_ci			 * Actually waiting for an event, so add the request to
19128c2ecf20Sopenharmony_ci			 * active_reqs so that it can be cancelled if needed.
19138c2ecf20Sopenharmony_ci			 */
19148c2ecf20Sopenharmony_ci			list_add_tail(&aiocb->ki_list, &ctx->active_reqs);
19158c2ecf20Sopenharmony_ci			aiocb->ki_cancel = aio_poll_cancel;
19168c2ecf20Sopenharmony_ci		}
19178c2ecf20Sopenharmony_ci		if (on_queue)
19188c2ecf20Sopenharmony_ci			poll_iocb_unlock_wq(req);
19198c2ecf20Sopenharmony_ci	}
19208c2ecf20Sopenharmony_ci	if (mask) { /* no async, we'd stolen it */
19218c2ecf20Sopenharmony_ci		aiocb->ki_res.res = mangle_poll(mask);
19228c2ecf20Sopenharmony_ci		apt.error = 0;
19238c2ecf20Sopenharmony_ci	}
19248c2ecf20Sopenharmony_ci	spin_unlock_irq(&ctx->ctx_lock);
19258c2ecf20Sopenharmony_ci	if (mask)
19268c2ecf20Sopenharmony_ci		iocb_put(aiocb);
19278c2ecf20Sopenharmony_ci	return apt.error;
19288c2ecf20Sopenharmony_ci}
19298c2ecf20Sopenharmony_ci
19308c2ecf20Sopenharmony_cistatic int __io_submit_one(struct kioctx *ctx, const struct iocb *iocb,
19318c2ecf20Sopenharmony_ci			   struct iocb __user *user_iocb, struct aio_kiocb *req,
19328c2ecf20Sopenharmony_ci			   bool compat)
19338c2ecf20Sopenharmony_ci{
19348c2ecf20Sopenharmony_ci	req->ki_filp = fget(iocb->aio_fildes);
19358c2ecf20Sopenharmony_ci	if (unlikely(!req->ki_filp))
19368c2ecf20Sopenharmony_ci		return -EBADF;
19378c2ecf20Sopenharmony_ci
19388c2ecf20Sopenharmony_ci	if (iocb->aio_flags & IOCB_FLAG_RESFD) {
19398c2ecf20Sopenharmony_ci		struct eventfd_ctx *eventfd;
19408c2ecf20Sopenharmony_ci		/*
19418c2ecf20Sopenharmony_ci		 * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
19428c2ecf20Sopenharmony_ci		 * instance of the file* now. The file descriptor must be
19438c2ecf20Sopenharmony_ci		 * an eventfd() fd, and will be signaled for each completed
19448c2ecf20Sopenharmony_ci		 * event using the eventfd_signal() function.
19458c2ecf20Sopenharmony_ci		 */
19468c2ecf20Sopenharmony_ci		eventfd = eventfd_ctx_fdget(iocb->aio_resfd);
19478c2ecf20Sopenharmony_ci		if (IS_ERR(eventfd))
19488c2ecf20Sopenharmony_ci			return PTR_ERR(eventfd);
19498c2ecf20Sopenharmony_ci
19508c2ecf20Sopenharmony_ci		req->ki_eventfd = eventfd;
19518c2ecf20Sopenharmony_ci	}
19528c2ecf20Sopenharmony_ci
19538c2ecf20Sopenharmony_ci	if (unlikely(put_user(KIOCB_KEY, &user_iocb->aio_key))) {
19548c2ecf20Sopenharmony_ci		pr_debug("EFAULT: aio_key\n");
19558c2ecf20Sopenharmony_ci		return -EFAULT;
19568c2ecf20Sopenharmony_ci	}
19578c2ecf20Sopenharmony_ci
19588c2ecf20Sopenharmony_ci	req->ki_res.obj = (u64)(unsigned long)user_iocb;
19598c2ecf20Sopenharmony_ci	req->ki_res.data = iocb->aio_data;
19608c2ecf20Sopenharmony_ci	req->ki_res.res = 0;
19618c2ecf20Sopenharmony_ci	req->ki_res.res2 = 0;
19628c2ecf20Sopenharmony_ci
19638c2ecf20Sopenharmony_ci	switch (iocb->aio_lio_opcode) {
19648c2ecf20Sopenharmony_ci	case IOCB_CMD_PREAD:
19658c2ecf20Sopenharmony_ci		return aio_read(&req->rw, iocb, false, compat);
19668c2ecf20Sopenharmony_ci	case IOCB_CMD_PWRITE:
19678c2ecf20Sopenharmony_ci		return aio_write(&req->rw, iocb, false, compat);
19688c2ecf20Sopenharmony_ci	case IOCB_CMD_PREADV:
19698c2ecf20Sopenharmony_ci		return aio_read(&req->rw, iocb, true, compat);
19708c2ecf20Sopenharmony_ci	case IOCB_CMD_PWRITEV:
19718c2ecf20Sopenharmony_ci		return aio_write(&req->rw, iocb, true, compat);
19728c2ecf20Sopenharmony_ci	case IOCB_CMD_FSYNC:
19738c2ecf20Sopenharmony_ci		return aio_fsync(&req->fsync, iocb, false);
19748c2ecf20Sopenharmony_ci	case IOCB_CMD_FDSYNC:
19758c2ecf20Sopenharmony_ci		return aio_fsync(&req->fsync, iocb, true);
19768c2ecf20Sopenharmony_ci	case IOCB_CMD_POLL:
19778c2ecf20Sopenharmony_ci		return aio_poll(req, iocb);
19788c2ecf20Sopenharmony_ci	default:
19798c2ecf20Sopenharmony_ci		pr_debug("invalid aio operation %d\n", iocb->aio_lio_opcode);
19808c2ecf20Sopenharmony_ci		return -EINVAL;
19818c2ecf20Sopenharmony_ci	}
19828c2ecf20Sopenharmony_ci}
19838c2ecf20Sopenharmony_ci
19848c2ecf20Sopenharmony_cistatic int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
19858c2ecf20Sopenharmony_ci			 bool compat)
19868c2ecf20Sopenharmony_ci{
19878c2ecf20Sopenharmony_ci	struct aio_kiocb *req;
19888c2ecf20Sopenharmony_ci	struct iocb iocb;
19898c2ecf20Sopenharmony_ci	int err;
19908c2ecf20Sopenharmony_ci
19918c2ecf20Sopenharmony_ci	if (unlikely(copy_from_user(&iocb, user_iocb, sizeof(iocb))))
19928c2ecf20Sopenharmony_ci		return -EFAULT;
19938c2ecf20Sopenharmony_ci
19948c2ecf20Sopenharmony_ci	/* enforce forwards compatibility on users */
19958c2ecf20Sopenharmony_ci	if (unlikely(iocb.aio_reserved2)) {
19968c2ecf20Sopenharmony_ci		pr_debug("EINVAL: reserve field set\n");
19978c2ecf20Sopenharmony_ci		return -EINVAL;
19988c2ecf20Sopenharmony_ci	}
19998c2ecf20Sopenharmony_ci
20008c2ecf20Sopenharmony_ci	/* prevent overflows */
20018c2ecf20Sopenharmony_ci	if (unlikely(
20028c2ecf20Sopenharmony_ci	    (iocb.aio_buf != (unsigned long)iocb.aio_buf) ||
20038c2ecf20Sopenharmony_ci	    (iocb.aio_nbytes != (size_t)iocb.aio_nbytes) ||
20048c2ecf20Sopenharmony_ci	    ((ssize_t)iocb.aio_nbytes < 0)
20058c2ecf20Sopenharmony_ci	   )) {
20068c2ecf20Sopenharmony_ci		pr_debug("EINVAL: overflow check\n");
20078c2ecf20Sopenharmony_ci		return -EINVAL;
20088c2ecf20Sopenharmony_ci	}
20098c2ecf20Sopenharmony_ci
20108c2ecf20Sopenharmony_ci	req = aio_get_req(ctx);
20118c2ecf20Sopenharmony_ci	if (unlikely(!req))
20128c2ecf20Sopenharmony_ci		return -EAGAIN;
20138c2ecf20Sopenharmony_ci
20148c2ecf20Sopenharmony_ci	err = __io_submit_one(ctx, &iocb, user_iocb, req, compat);
20158c2ecf20Sopenharmony_ci
20168c2ecf20Sopenharmony_ci	/* Done with the synchronous reference */
20178c2ecf20Sopenharmony_ci	iocb_put(req);
20188c2ecf20Sopenharmony_ci
20198c2ecf20Sopenharmony_ci	/*
20208c2ecf20Sopenharmony_ci	 * If err is 0, we'd either done aio_complete() ourselves or have
20218c2ecf20Sopenharmony_ci	 * arranged for that to be done asynchronously.  Anything non-zero
20228c2ecf20Sopenharmony_ci	 * means that we need to destroy req ourselves.
20238c2ecf20Sopenharmony_ci	 */
20248c2ecf20Sopenharmony_ci	if (unlikely(err)) {
20258c2ecf20Sopenharmony_ci		iocb_destroy(req);
20268c2ecf20Sopenharmony_ci		put_reqs_available(ctx, 1);
20278c2ecf20Sopenharmony_ci	}
20288c2ecf20Sopenharmony_ci	return err;
20298c2ecf20Sopenharmony_ci}
20308c2ecf20Sopenharmony_ci
20318c2ecf20Sopenharmony_ci/* sys_io_submit:
20328c2ecf20Sopenharmony_ci *	Queue the nr iocbs pointed to by iocbpp for processing.  Returns
20338c2ecf20Sopenharmony_ci *	the number of iocbs queued.  May return -EINVAL if the aio_context
20348c2ecf20Sopenharmony_ci *	specified by ctx_id is invalid, if nr is < 0, if the iocb at
20358c2ecf20Sopenharmony_ci *	*iocbpp[0] is not properly initialized, if the operation specified
20368c2ecf20Sopenharmony_ci *	is invalid for the file descriptor in the iocb.  May fail with
20378c2ecf20Sopenharmony_ci *	-EFAULT if any of the data structures point to invalid data.  May
20388c2ecf20Sopenharmony_ci *	fail with -EBADF if the file descriptor specified in the first
20398c2ecf20Sopenharmony_ci *	iocb is invalid.  May fail with -EAGAIN if insufficient resources
20408c2ecf20Sopenharmony_ci *	are available to queue any iocbs.  Will return 0 if nr is 0.  Will
20418c2ecf20Sopenharmony_ci *	fail with -ENOSYS if not implemented.
20428c2ecf20Sopenharmony_ci */
20438c2ecf20Sopenharmony_ciSYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
20448c2ecf20Sopenharmony_ci		struct iocb __user * __user *, iocbpp)
20458c2ecf20Sopenharmony_ci{
20468c2ecf20Sopenharmony_ci	struct kioctx *ctx;
20478c2ecf20Sopenharmony_ci	long ret = 0;
20488c2ecf20Sopenharmony_ci	int i = 0;
20498c2ecf20Sopenharmony_ci	struct blk_plug plug;
20508c2ecf20Sopenharmony_ci
20518c2ecf20Sopenharmony_ci	if (unlikely(nr < 0))
20528c2ecf20Sopenharmony_ci		return -EINVAL;
20538c2ecf20Sopenharmony_ci
20548c2ecf20Sopenharmony_ci	ctx = lookup_ioctx(ctx_id);
20558c2ecf20Sopenharmony_ci	if (unlikely(!ctx)) {
20568c2ecf20Sopenharmony_ci		pr_debug("EINVAL: invalid context id\n");
20578c2ecf20Sopenharmony_ci		return -EINVAL;
20588c2ecf20Sopenharmony_ci	}
20598c2ecf20Sopenharmony_ci
20608c2ecf20Sopenharmony_ci	if (nr > ctx->nr_events)
20618c2ecf20Sopenharmony_ci		nr = ctx->nr_events;
20628c2ecf20Sopenharmony_ci
20638c2ecf20Sopenharmony_ci	if (nr > AIO_PLUG_THRESHOLD)
20648c2ecf20Sopenharmony_ci		blk_start_plug(&plug);
20658c2ecf20Sopenharmony_ci	for (i = 0; i < nr; i++) {
20668c2ecf20Sopenharmony_ci		struct iocb __user *user_iocb;
20678c2ecf20Sopenharmony_ci
20688c2ecf20Sopenharmony_ci		if (unlikely(get_user(user_iocb, iocbpp + i))) {
20698c2ecf20Sopenharmony_ci			ret = -EFAULT;
20708c2ecf20Sopenharmony_ci			break;
20718c2ecf20Sopenharmony_ci		}
20728c2ecf20Sopenharmony_ci
20738c2ecf20Sopenharmony_ci		ret = io_submit_one(ctx, user_iocb, false);
20748c2ecf20Sopenharmony_ci		if (ret)
20758c2ecf20Sopenharmony_ci			break;
20768c2ecf20Sopenharmony_ci	}
20778c2ecf20Sopenharmony_ci	if (nr > AIO_PLUG_THRESHOLD)
20788c2ecf20Sopenharmony_ci		blk_finish_plug(&plug);
20798c2ecf20Sopenharmony_ci
20808c2ecf20Sopenharmony_ci	percpu_ref_put(&ctx->users);
20818c2ecf20Sopenharmony_ci	return i ? i : ret;
20828c2ecf20Sopenharmony_ci}
20838c2ecf20Sopenharmony_ci
20848c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT
20858c2ecf20Sopenharmony_ciCOMPAT_SYSCALL_DEFINE3(io_submit, compat_aio_context_t, ctx_id,
20868c2ecf20Sopenharmony_ci		       int, nr, compat_uptr_t __user *, iocbpp)
20878c2ecf20Sopenharmony_ci{
20888c2ecf20Sopenharmony_ci	struct kioctx *ctx;
20898c2ecf20Sopenharmony_ci	long ret = 0;
20908c2ecf20Sopenharmony_ci	int i = 0;
20918c2ecf20Sopenharmony_ci	struct blk_plug plug;
20928c2ecf20Sopenharmony_ci
20938c2ecf20Sopenharmony_ci	if (unlikely(nr < 0))
20948c2ecf20Sopenharmony_ci		return -EINVAL;
20958c2ecf20Sopenharmony_ci
20968c2ecf20Sopenharmony_ci	ctx = lookup_ioctx(ctx_id);
20978c2ecf20Sopenharmony_ci	if (unlikely(!ctx)) {
20988c2ecf20Sopenharmony_ci		pr_debug("EINVAL: invalid context id\n");
20998c2ecf20Sopenharmony_ci		return -EINVAL;
21008c2ecf20Sopenharmony_ci	}
21018c2ecf20Sopenharmony_ci
21028c2ecf20Sopenharmony_ci	if (nr > ctx->nr_events)
21038c2ecf20Sopenharmony_ci		nr = ctx->nr_events;
21048c2ecf20Sopenharmony_ci
21058c2ecf20Sopenharmony_ci	if (nr > AIO_PLUG_THRESHOLD)
21068c2ecf20Sopenharmony_ci		blk_start_plug(&plug);
21078c2ecf20Sopenharmony_ci	for (i = 0; i < nr; i++) {
21088c2ecf20Sopenharmony_ci		compat_uptr_t user_iocb;
21098c2ecf20Sopenharmony_ci
21108c2ecf20Sopenharmony_ci		if (unlikely(get_user(user_iocb, iocbpp + i))) {
21118c2ecf20Sopenharmony_ci			ret = -EFAULT;
21128c2ecf20Sopenharmony_ci			break;
21138c2ecf20Sopenharmony_ci		}
21148c2ecf20Sopenharmony_ci
21158c2ecf20Sopenharmony_ci		ret = io_submit_one(ctx, compat_ptr(user_iocb), true);
21168c2ecf20Sopenharmony_ci		if (ret)
21178c2ecf20Sopenharmony_ci			break;
21188c2ecf20Sopenharmony_ci	}
21198c2ecf20Sopenharmony_ci	if (nr > AIO_PLUG_THRESHOLD)
21208c2ecf20Sopenharmony_ci		blk_finish_plug(&plug);
21218c2ecf20Sopenharmony_ci
21228c2ecf20Sopenharmony_ci	percpu_ref_put(&ctx->users);
21238c2ecf20Sopenharmony_ci	return i ? i : ret;
21248c2ecf20Sopenharmony_ci}
21258c2ecf20Sopenharmony_ci#endif
21268c2ecf20Sopenharmony_ci
21278c2ecf20Sopenharmony_ci/* sys_io_cancel:
21288c2ecf20Sopenharmony_ci *	Attempts to cancel an iocb previously passed to io_submit.  If
21298c2ecf20Sopenharmony_ci *	the operation is successfully cancelled, the resulting event is
21308c2ecf20Sopenharmony_ci *	copied into the memory pointed to by result without being placed
21318c2ecf20Sopenharmony_ci *	into the completion queue and 0 is returned.  May fail with
21328c2ecf20Sopenharmony_ci *	-EFAULT if any of the data structures pointed to are invalid.
21338c2ecf20Sopenharmony_ci *	May fail with -EINVAL if aio_context specified by ctx_id is
21348c2ecf20Sopenharmony_ci *	invalid.  May fail with -EAGAIN if the iocb specified was not
21358c2ecf20Sopenharmony_ci *	cancelled.  Will fail with -ENOSYS if not implemented.
21368c2ecf20Sopenharmony_ci */
21378c2ecf20Sopenharmony_ciSYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
21388c2ecf20Sopenharmony_ci		struct io_event __user *, result)
21398c2ecf20Sopenharmony_ci{
21408c2ecf20Sopenharmony_ci	struct kioctx *ctx;
21418c2ecf20Sopenharmony_ci	struct aio_kiocb *kiocb;
21428c2ecf20Sopenharmony_ci	int ret = -EINVAL;
21438c2ecf20Sopenharmony_ci	u32 key;
21448c2ecf20Sopenharmony_ci	u64 obj = (u64)(unsigned long)iocb;
21458c2ecf20Sopenharmony_ci
21468c2ecf20Sopenharmony_ci	if (unlikely(get_user(key, &iocb->aio_key)))
21478c2ecf20Sopenharmony_ci		return -EFAULT;
21488c2ecf20Sopenharmony_ci	if (unlikely(key != KIOCB_KEY))
21498c2ecf20Sopenharmony_ci		return -EINVAL;
21508c2ecf20Sopenharmony_ci
21518c2ecf20Sopenharmony_ci	ctx = lookup_ioctx(ctx_id);
21528c2ecf20Sopenharmony_ci	if (unlikely(!ctx))
21538c2ecf20Sopenharmony_ci		return -EINVAL;
21548c2ecf20Sopenharmony_ci
21558c2ecf20Sopenharmony_ci	spin_lock_irq(&ctx->ctx_lock);
21568c2ecf20Sopenharmony_ci	/* TODO: use a hash or array, this sucks. */
21578c2ecf20Sopenharmony_ci	list_for_each_entry(kiocb, &ctx->active_reqs, ki_list) {
21588c2ecf20Sopenharmony_ci		if (kiocb->ki_res.obj == obj) {
21598c2ecf20Sopenharmony_ci			ret = kiocb->ki_cancel(&kiocb->rw);
21608c2ecf20Sopenharmony_ci			list_del_init(&kiocb->ki_list);
21618c2ecf20Sopenharmony_ci			break;
21628c2ecf20Sopenharmony_ci		}
21638c2ecf20Sopenharmony_ci	}
21648c2ecf20Sopenharmony_ci	spin_unlock_irq(&ctx->ctx_lock);
21658c2ecf20Sopenharmony_ci
21668c2ecf20Sopenharmony_ci	if (!ret) {
21678c2ecf20Sopenharmony_ci		/*
21688c2ecf20Sopenharmony_ci		 * The result argument is no longer used - the io_event is
21698c2ecf20Sopenharmony_ci		 * always delivered via the ring buffer. -EINPROGRESS indicates
21708c2ecf20Sopenharmony_ci		 * cancellation is progress:
21718c2ecf20Sopenharmony_ci		 */
21728c2ecf20Sopenharmony_ci		ret = -EINPROGRESS;
21738c2ecf20Sopenharmony_ci	}
21748c2ecf20Sopenharmony_ci
21758c2ecf20Sopenharmony_ci	percpu_ref_put(&ctx->users);
21768c2ecf20Sopenharmony_ci
21778c2ecf20Sopenharmony_ci	return ret;
21788c2ecf20Sopenharmony_ci}
21798c2ecf20Sopenharmony_ci
21808c2ecf20Sopenharmony_cistatic long do_io_getevents(aio_context_t ctx_id,
21818c2ecf20Sopenharmony_ci		long min_nr,
21828c2ecf20Sopenharmony_ci		long nr,
21838c2ecf20Sopenharmony_ci		struct io_event __user *events,
21848c2ecf20Sopenharmony_ci		struct timespec64 *ts)
21858c2ecf20Sopenharmony_ci{
21868c2ecf20Sopenharmony_ci	ktime_t until = KTIME_MAX;
21878c2ecf20Sopenharmony_ci	struct kioctx *ioctx = NULL;
21888c2ecf20Sopenharmony_ci	long ret = -EINVAL;
21898c2ecf20Sopenharmony_ci
21908c2ecf20Sopenharmony_ci	if (ts) {
21918c2ecf20Sopenharmony_ci		if (!timespec64_valid(ts))
21928c2ecf20Sopenharmony_ci			return ret;
21938c2ecf20Sopenharmony_ci		until = timespec64_to_ktime(*ts);
21948c2ecf20Sopenharmony_ci	}
21958c2ecf20Sopenharmony_ci
21968c2ecf20Sopenharmony_ci	ioctx = lookup_ioctx(ctx_id);
21978c2ecf20Sopenharmony_ci	if (likely(ioctx)) {
21988c2ecf20Sopenharmony_ci		if (likely(min_nr <= nr && min_nr >= 0))
21998c2ecf20Sopenharmony_ci			ret = read_events(ioctx, min_nr, nr, events, until);
22008c2ecf20Sopenharmony_ci		percpu_ref_put(&ioctx->users);
22018c2ecf20Sopenharmony_ci	}
22028c2ecf20Sopenharmony_ci
22038c2ecf20Sopenharmony_ci	return ret;
22048c2ecf20Sopenharmony_ci}
22058c2ecf20Sopenharmony_ci
22068c2ecf20Sopenharmony_ci/* io_getevents:
22078c2ecf20Sopenharmony_ci *	Attempts to read at least min_nr events and up to nr events from
22088c2ecf20Sopenharmony_ci *	the completion queue for the aio_context specified by ctx_id. If
22098c2ecf20Sopenharmony_ci *	it succeeds, the number of read events is returned. May fail with
22108c2ecf20Sopenharmony_ci *	-EINVAL if ctx_id is invalid, if min_nr is out of range, if nr is
22118c2ecf20Sopenharmony_ci *	out of range, if timeout is out of range.  May fail with -EFAULT
22128c2ecf20Sopenharmony_ci *	if any of the memory specified is invalid.  May return 0 or
22138c2ecf20Sopenharmony_ci *	< min_nr if the timeout specified by timeout has elapsed
22148c2ecf20Sopenharmony_ci *	before sufficient events are available, where timeout == NULL
22158c2ecf20Sopenharmony_ci *	specifies an infinite timeout. Note that the timeout pointed to by
22168c2ecf20Sopenharmony_ci *	timeout is relative.  Will fail with -ENOSYS if not implemented.
22178c2ecf20Sopenharmony_ci */
22188c2ecf20Sopenharmony_ci#ifdef CONFIG_64BIT
22198c2ecf20Sopenharmony_ci
22208c2ecf20Sopenharmony_ciSYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id,
22218c2ecf20Sopenharmony_ci		long, min_nr,
22228c2ecf20Sopenharmony_ci		long, nr,
22238c2ecf20Sopenharmony_ci		struct io_event __user *, events,
22248c2ecf20Sopenharmony_ci		struct __kernel_timespec __user *, timeout)
22258c2ecf20Sopenharmony_ci{
22268c2ecf20Sopenharmony_ci	struct timespec64	ts;
22278c2ecf20Sopenharmony_ci	int			ret;
22288c2ecf20Sopenharmony_ci
22298c2ecf20Sopenharmony_ci	if (timeout && unlikely(get_timespec64(&ts, timeout)))
22308c2ecf20Sopenharmony_ci		return -EFAULT;
22318c2ecf20Sopenharmony_ci
22328c2ecf20Sopenharmony_ci	ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &ts : NULL);
22338c2ecf20Sopenharmony_ci	if (!ret && signal_pending(current))
22348c2ecf20Sopenharmony_ci		ret = -EINTR;
22358c2ecf20Sopenharmony_ci	return ret;
22368c2ecf20Sopenharmony_ci}
22378c2ecf20Sopenharmony_ci
22388c2ecf20Sopenharmony_ci#endif
22398c2ecf20Sopenharmony_ci
22408c2ecf20Sopenharmony_cistruct __aio_sigset {
22418c2ecf20Sopenharmony_ci	const sigset_t __user	*sigmask;
22428c2ecf20Sopenharmony_ci	size_t		sigsetsize;
22438c2ecf20Sopenharmony_ci};
22448c2ecf20Sopenharmony_ci
22458c2ecf20Sopenharmony_ciSYSCALL_DEFINE6(io_pgetevents,
22468c2ecf20Sopenharmony_ci		aio_context_t, ctx_id,
22478c2ecf20Sopenharmony_ci		long, min_nr,
22488c2ecf20Sopenharmony_ci		long, nr,
22498c2ecf20Sopenharmony_ci		struct io_event __user *, events,
22508c2ecf20Sopenharmony_ci		struct __kernel_timespec __user *, timeout,
22518c2ecf20Sopenharmony_ci		const struct __aio_sigset __user *, usig)
22528c2ecf20Sopenharmony_ci{
22538c2ecf20Sopenharmony_ci	struct __aio_sigset	ksig = { NULL, };
22548c2ecf20Sopenharmony_ci	struct timespec64	ts;
22558c2ecf20Sopenharmony_ci	bool interrupted;
22568c2ecf20Sopenharmony_ci	int ret;
22578c2ecf20Sopenharmony_ci
22588c2ecf20Sopenharmony_ci	if (timeout && unlikely(get_timespec64(&ts, timeout)))
22598c2ecf20Sopenharmony_ci		return -EFAULT;
22608c2ecf20Sopenharmony_ci
22618c2ecf20Sopenharmony_ci	if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
22628c2ecf20Sopenharmony_ci		return -EFAULT;
22638c2ecf20Sopenharmony_ci
22648c2ecf20Sopenharmony_ci	ret = set_user_sigmask(ksig.sigmask, ksig.sigsetsize);
22658c2ecf20Sopenharmony_ci	if (ret)
22668c2ecf20Sopenharmony_ci		return ret;
22678c2ecf20Sopenharmony_ci
22688c2ecf20Sopenharmony_ci	ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &ts : NULL);
22698c2ecf20Sopenharmony_ci
22708c2ecf20Sopenharmony_ci	interrupted = signal_pending(current);
22718c2ecf20Sopenharmony_ci	restore_saved_sigmask_unless(interrupted);
22728c2ecf20Sopenharmony_ci	if (interrupted && !ret)
22738c2ecf20Sopenharmony_ci		ret = -ERESTARTNOHAND;
22748c2ecf20Sopenharmony_ci
22758c2ecf20Sopenharmony_ci	return ret;
22768c2ecf20Sopenharmony_ci}
22778c2ecf20Sopenharmony_ci
22788c2ecf20Sopenharmony_ci#if defined(CONFIG_COMPAT_32BIT_TIME) && !defined(CONFIG_64BIT)
22798c2ecf20Sopenharmony_ci
22808c2ecf20Sopenharmony_ciSYSCALL_DEFINE6(io_pgetevents_time32,
22818c2ecf20Sopenharmony_ci		aio_context_t, ctx_id,
22828c2ecf20Sopenharmony_ci		long, min_nr,
22838c2ecf20Sopenharmony_ci		long, nr,
22848c2ecf20Sopenharmony_ci		struct io_event __user *, events,
22858c2ecf20Sopenharmony_ci		struct old_timespec32 __user *, timeout,
22868c2ecf20Sopenharmony_ci		const struct __aio_sigset __user *, usig)
22878c2ecf20Sopenharmony_ci{
22888c2ecf20Sopenharmony_ci	struct __aio_sigset	ksig = { NULL, };
22898c2ecf20Sopenharmony_ci	struct timespec64	ts;
22908c2ecf20Sopenharmony_ci	bool interrupted;
22918c2ecf20Sopenharmony_ci	int ret;
22928c2ecf20Sopenharmony_ci
22938c2ecf20Sopenharmony_ci	if (timeout && unlikely(get_old_timespec32(&ts, timeout)))
22948c2ecf20Sopenharmony_ci		return -EFAULT;
22958c2ecf20Sopenharmony_ci
22968c2ecf20Sopenharmony_ci	if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
22978c2ecf20Sopenharmony_ci		return -EFAULT;
22988c2ecf20Sopenharmony_ci
22998c2ecf20Sopenharmony_ci
23008c2ecf20Sopenharmony_ci	ret = set_user_sigmask(ksig.sigmask, ksig.sigsetsize);
23018c2ecf20Sopenharmony_ci	if (ret)
23028c2ecf20Sopenharmony_ci		return ret;
23038c2ecf20Sopenharmony_ci
23048c2ecf20Sopenharmony_ci	ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &ts : NULL);
23058c2ecf20Sopenharmony_ci
23068c2ecf20Sopenharmony_ci	interrupted = signal_pending(current);
23078c2ecf20Sopenharmony_ci	restore_saved_sigmask_unless(interrupted);
23088c2ecf20Sopenharmony_ci	if (interrupted && !ret)
23098c2ecf20Sopenharmony_ci		ret = -ERESTARTNOHAND;
23108c2ecf20Sopenharmony_ci
23118c2ecf20Sopenharmony_ci	return ret;
23128c2ecf20Sopenharmony_ci}
23138c2ecf20Sopenharmony_ci
23148c2ecf20Sopenharmony_ci#endif
23158c2ecf20Sopenharmony_ci
23168c2ecf20Sopenharmony_ci#if defined(CONFIG_COMPAT_32BIT_TIME)
23178c2ecf20Sopenharmony_ci
23188c2ecf20Sopenharmony_ciSYSCALL_DEFINE5(io_getevents_time32, __u32, ctx_id,
23198c2ecf20Sopenharmony_ci		__s32, min_nr,
23208c2ecf20Sopenharmony_ci		__s32, nr,
23218c2ecf20Sopenharmony_ci		struct io_event __user *, events,
23228c2ecf20Sopenharmony_ci		struct old_timespec32 __user *, timeout)
23238c2ecf20Sopenharmony_ci{
23248c2ecf20Sopenharmony_ci	struct timespec64 t;
23258c2ecf20Sopenharmony_ci	int ret;
23268c2ecf20Sopenharmony_ci
23278c2ecf20Sopenharmony_ci	if (timeout && get_old_timespec32(&t, timeout))
23288c2ecf20Sopenharmony_ci		return -EFAULT;
23298c2ecf20Sopenharmony_ci
23308c2ecf20Sopenharmony_ci	ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &t : NULL);
23318c2ecf20Sopenharmony_ci	if (!ret && signal_pending(current))
23328c2ecf20Sopenharmony_ci		ret = -EINTR;
23338c2ecf20Sopenharmony_ci	return ret;
23348c2ecf20Sopenharmony_ci}
23358c2ecf20Sopenharmony_ci
23368c2ecf20Sopenharmony_ci#endif
23378c2ecf20Sopenharmony_ci
23388c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT
23398c2ecf20Sopenharmony_ci
23408c2ecf20Sopenharmony_cistruct __compat_aio_sigset {
23418c2ecf20Sopenharmony_ci	compat_uptr_t		sigmask;
23428c2ecf20Sopenharmony_ci	compat_size_t		sigsetsize;
23438c2ecf20Sopenharmony_ci};
23448c2ecf20Sopenharmony_ci
23458c2ecf20Sopenharmony_ci#if defined(CONFIG_COMPAT_32BIT_TIME)
23468c2ecf20Sopenharmony_ci
23478c2ecf20Sopenharmony_ciCOMPAT_SYSCALL_DEFINE6(io_pgetevents,
23488c2ecf20Sopenharmony_ci		compat_aio_context_t, ctx_id,
23498c2ecf20Sopenharmony_ci		compat_long_t, min_nr,
23508c2ecf20Sopenharmony_ci		compat_long_t, nr,
23518c2ecf20Sopenharmony_ci		struct io_event __user *, events,
23528c2ecf20Sopenharmony_ci		struct old_timespec32 __user *, timeout,
23538c2ecf20Sopenharmony_ci		const struct __compat_aio_sigset __user *, usig)
23548c2ecf20Sopenharmony_ci{
23558c2ecf20Sopenharmony_ci	struct __compat_aio_sigset ksig = { 0, };
23568c2ecf20Sopenharmony_ci	struct timespec64 t;
23578c2ecf20Sopenharmony_ci	bool interrupted;
23588c2ecf20Sopenharmony_ci	int ret;
23598c2ecf20Sopenharmony_ci
23608c2ecf20Sopenharmony_ci	if (timeout && get_old_timespec32(&t, timeout))
23618c2ecf20Sopenharmony_ci		return -EFAULT;
23628c2ecf20Sopenharmony_ci
23638c2ecf20Sopenharmony_ci	if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
23648c2ecf20Sopenharmony_ci		return -EFAULT;
23658c2ecf20Sopenharmony_ci
23668c2ecf20Sopenharmony_ci	ret = set_compat_user_sigmask(compat_ptr(ksig.sigmask), ksig.sigsetsize);
23678c2ecf20Sopenharmony_ci	if (ret)
23688c2ecf20Sopenharmony_ci		return ret;
23698c2ecf20Sopenharmony_ci
23708c2ecf20Sopenharmony_ci	ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &t : NULL);
23718c2ecf20Sopenharmony_ci
23728c2ecf20Sopenharmony_ci	interrupted = signal_pending(current);
23738c2ecf20Sopenharmony_ci	restore_saved_sigmask_unless(interrupted);
23748c2ecf20Sopenharmony_ci	if (interrupted && !ret)
23758c2ecf20Sopenharmony_ci		ret = -ERESTARTNOHAND;
23768c2ecf20Sopenharmony_ci
23778c2ecf20Sopenharmony_ci	return ret;
23788c2ecf20Sopenharmony_ci}
23798c2ecf20Sopenharmony_ci
23808c2ecf20Sopenharmony_ci#endif
23818c2ecf20Sopenharmony_ci
23828c2ecf20Sopenharmony_ciCOMPAT_SYSCALL_DEFINE6(io_pgetevents_time64,
23838c2ecf20Sopenharmony_ci		compat_aio_context_t, ctx_id,
23848c2ecf20Sopenharmony_ci		compat_long_t, min_nr,
23858c2ecf20Sopenharmony_ci		compat_long_t, nr,
23868c2ecf20Sopenharmony_ci		struct io_event __user *, events,
23878c2ecf20Sopenharmony_ci		struct __kernel_timespec __user *, timeout,
23888c2ecf20Sopenharmony_ci		const struct __compat_aio_sigset __user *, usig)
23898c2ecf20Sopenharmony_ci{
23908c2ecf20Sopenharmony_ci	struct __compat_aio_sigset ksig = { 0, };
23918c2ecf20Sopenharmony_ci	struct timespec64 t;
23928c2ecf20Sopenharmony_ci	bool interrupted;
23938c2ecf20Sopenharmony_ci	int ret;
23948c2ecf20Sopenharmony_ci
23958c2ecf20Sopenharmony_ci	if (timeout && get_timespec64(&t, timeout))
23968c2ecf20Sopenharmony_ci		return -EFAULT;
23978c2ecf20Sopenharmony_ci
23988c2ecf20Sopenharmony_ci	if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
23998c2ecf20Sopenharmony_ci		return -EFAULT;
24008c2ecf20Sopenharmony_ci
24018c2ecf20Sopenharmony_ci	ret = set_compat_user_sigmask(compat_ptr(ksig.sigmask), ksig.sigsetsize);
24028c2ecf20Sopenharmony_ci	if (ret)
24038c2ecf20Sopenharmony_ci		return ret;
24048c2ecf20Sopenharmony_ci
24058c2ecf20Sopenharmony_ci	ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &t : NULL);
24068c2ecf20Sopenharmony_ci
24078c2ecf20Sopenharmony_ci	interrupted = signal_pending(current);
24088c2ecf20Sopenharmony_ci	restore_saved_sigmask_unless(interrupted);
24098c2ecf20Sopenharmony_ci	if (interrupted && !ret)
24108c2ecf20Sopenharmony_ci		ret = -ERESTARTNOHAND;
24118c2ecf20Sopenharmony_ci
24128c2ecf20Sopenharmony_ci	return ret;
24138c2ecf20Sopenharmony_ci}
24148c2ecf20Sopenharmony_ci#endif
2415