VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   32 Branches   54 Tags
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700    1) /*
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700    2)  *	An async IO implementation for Linux
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700    3)  *	Written by Benjamin LaHaise <bcrl@kvack.org>
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700    4)  *
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700    5)  *	Implements an efficient asynchronous io interface.
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700    6)  *
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700    7)  *	Copyright 2000, 2001, 2002 Red Hat, Inc.  All Rights Reserved.
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200    8)  *	Copyright 2018 Christoph Hellwig.
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700    9)  *
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700   10)  *	See ../COPYING for licensing terms.
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700   11)  */
caf4167aa73be (Kent Overstreet            2013-05-07 16:18:35 -0700   12) #define pr_fmt(fmt) "%s: " fmt, __func__
caf4167aa73be (Kent Overstreet            2013-05-07 16:18:35 -0700   13) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700   14) #include <linux/kernel.h>
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700   15) #include <linux/init.h>
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700   16) #include <linux/errno.h>
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700   17) #include <linux/time.h>
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700   18) #include <linux/aio_abi.h>
630d9c47274aa (Paul Gortmaker             2011-11-16 23:57:37 -0500   19) #include <linux/export.h>
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700   20) #include <linux/syscalls.h>
b9d128f1088ea (Jens Axboe                 2009-10-29 13:59:26 +0100   21) #include <linux/backing-dev.h>
9018ccc453af0 (Christoph Hellwig          2018-07-24 11:36:37 +0200   22) #include <linux/refcount.h>
027445c37282b (Badari Pulavarty           2006-09-30 23:28:46 -0700   23) #include <linux/uio.h>
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700   24) 
174cd4b1e5fbd (Ingo Molnar                2017-02-02 19:15:33 +0100   25) #include <linux/sched/signal.h>
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700   26) #include <linux/fs.h>
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700   27) #include <linux/file.h>
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700   28) #include <linux/mm.h>
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700   29) #include <linux/mman.h>
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000   30) #include <linux/percpu.h>
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700   31) #include <linux/slab.h>
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700   32) #include <linux/timer.h>
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700   33) #include <linux/aio.h>
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700   34) #include <linux/highmem.h>
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700   35) #include <linux/workqueue.h>
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700   36) #include <linux/security.h>
9c3060bedd841 (Davide Libenzi             2007-05-10 22:23:21 -0700   37) #include <linux/eventfd.h>
cfb1e33eed481 (Jeff Moyer                 2009-10-02 18:57:36 -0400   38) #include <linux/blkdev.h>
9d85cba718efe (Jeff Moyer                 2010-05-26 14:44:26 -0700   39) #include <linux/compat.h>
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800   40) #include <linux/migrate.h>
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800   41) #include <linux/ramfs.h>
723be6e39d142 (Kent Overstreet            2013-05-28 15:14:48 -0700   42) #include <linux/percpu-refcount.h>
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400   43) #include <linux/mount.h>
52db59df17c0c (David Howells              2019-03-25 16:38:23 +0000   44) #include <linux/pseudo_fs.h>
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700   45) 
7c0f6ba682b9c (Linus Torvalds             2016-12-24 11:46:01 -0800   46) #include <linux/uaccess.h>
a538e3ff9dabc (Jeff Moyer                 2018-12-11 12:37:49 -0500   47) #include <linux/nospec.h>
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700   48) 
68d70d03f8f5b (Al Viro                    2013-06-19 15:26:04 +0400   49) #include "internal.h"
68d70d03f8f5b (Al Viro                    2013-06-19 15:26:04 +0400   50) 
f3a2752a43de1 (Christoph Hellwig          2018-03-30 11:19:25 +0200   51) #define KIOCB_KEY		0
f3a2752a43de1 (Christoph Hellwig          2018-03-30 11:19:25 +0200   52) 
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700   53) #define AIO_RING_MAGIC			0xa10a10a1
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700   54) #define AIO_RING_COMPAT_FEATURES	1
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700   55) #define AIO_RING_INCOMPAT_FEATURES	0
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700   56) struct aio_ring {
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700   57) 	unsigned	id;	/* kernel internal index number */
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700   58) 	unsigned	nr;	/* number of io_events */
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400   59) 	unsigned	head;	/* Written to by userland or under ring_lock
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400   60) 				 * mutex by aio_read_events_ring(). */
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700   61) 	unsigned	tail;
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700   62) 
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700   63) 	unsigned	magic;
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700   64) 	unsigned	compat_features;
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700   65) 	unsigned	incompat_features;
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700   66) 	unsigned	header_length;	/* size of aio_ring */
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700   67) 
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700   68) 
241cb28e38c32 (Gustavo A. R. Silva        2020-05-28 09:35:11 -0500   69) 	struct io_event		io_events[];
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700   70) }; /* 128 bytes + ring size */
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700   71) 
a79d40e9b0f30 (Jens Axboe                 2018-12-04 09:45:32 -0700   72) /*
a79d40e9b0f30 (Jens Axboe                 2018-12-04 09:45:32 -0700   73)  * Plugging is meant to work with larger batches of IOs. If we don't
a79d40e9b0f30 (Jens Axboe                 2018-12-04 09:45:32 -0700   74)  * have more than the below, then don't bother setting up a plug.
a79d40e9b0f30 (Jens Axboe                 2018-12-04 09:45:32 -0700   75)  */
a79d40e9b0f30 (Jens Axboe                 2018-12-04 09:45:32 -0700   76) #define AIO_PLUG_THRESHOLD	2
a79d40e9b0f30 (Jens Axboe                 2018-12-04 09:45:32 -0700   77) 
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700   78) #define AIO_RING_PAGES	8
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700   79) 
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400   80) struct kioctx_table {
d0264c01e7587 (Tejun Heo                  2018-03-14 12:10:17 -0700   81) 	struct rcu_head		rcu;
d0264c01e7587 (Tejun Heo                  2018-03-14 12:10:17 -0700   82) 	unsigned		nr;
d0264c01e7587 (Tejun Heo                  2018-03-14 12:10:17 -0700   83) 	struct kioctx __rcu	*table[];
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400   84) };
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400   85) 
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000   86) struct kioctx_cpu {
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000   87) 	unsigned		reqs_available;
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000   88) };
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000   89) 
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600   90) struct ctx_rq_wait {
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600   91) 	struct completion comp;
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600   92) 	atomic_t count;
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600   93) };
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600   94) 
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700   95) struct kioctx {
723be6e39d142 (Kent Overstreet            2013-05-28 15:14:48 -0700   96) 	struct percpu_ref	users;
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700   97) 	atomic_t		dead;
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700   98) 
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700   99) 	struct percpu_ref	reqs;
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  100) 
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700  101) 	unsigned long		user_id;
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700  102) 
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  103) 	struct __percpu kioctx_cpu *cpu;
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  104) 
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  105) 	/*
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  106) 	 * For percpu reqs_available, number of slots we move to/from global
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  107) 	 * counter at a time:
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  108) 	 */
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  109) 	unsigned		req_batch;
3e845ce01a391 (Kent Overstreet            2013-05-07 16:18:51 -0700  110) 	/*
3e845ce01a391 (Kent Overstreet            2013-05-07 16:18:51 -0700  111) 	 * This is what userspace passed to io_setup(), it's not used for
3e845ce01a391 (Kent Overstreet            2013-05-07 16:18:51 -0700  112) 	 * anything but counting against the global max_reqs quota.
3e845ce01a391 (Kent Overstreet            2013-05-07 16:18:51 -0700  113) 	 *
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  114) 	 * The real limit is nr_events - 1, which will be larger (see
3e845ce01a391 (Kent Overstreet            2013-05-07 16:18:51 -0700  115) 	 * aio_setup_ring())
3e845ce01a391 (Kent Overstreet            2013-05-07 16:18:51 -0700  116) 	 */
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700  117) 	unsigned		max_reqs;
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700  118) 
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  119) 	/* Size of ringbuffer, in units of struct io_event */
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  120) 	unsigned		nr_events;
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700  121) 
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  122) 	unsigned long		mmap_base;
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  123) 	unsigned long		mmap_size;
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  124) 
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  125) 	struct page		**ring_pages;
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  126) 	long			nr_pages;
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  127) 
f729863a8c969 (Tejun Heo                  2018-03-14 12:45:15 -0700  128) 	struct rcu_work		free_rwork;	/* see free_ioctx() */
4e23bcaeb9e8d (Kent Overstreet            2013-05-07 16:18:56 -0700  129) 
e02ba72aabfad (Anatol Pomozov             2014-04-15 11:31:33 -0700  130) 	/*
e02ba72aabfad (Anatol Pomozov             2014-04-15 11:31:33 -0700  131) 	 * signals when all in-flight requests are done
e02ba72aabfad (Anatol Pomozov             2014-04-15 11:31:33 -0700  132) 	 */
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600  133) 	struct ctx_rq_wait	*rq_wait;
e02ba72aabfad (Anatol Pomozov             2014-04-15 11:31:33 -0700  134) 
4e23bcaeb9e8d (Kent Overstreet            2013-05-07 16:18:56 -0700  135) 	struct {
34e83fc618085 (Kent Overstreet            2013-04-26 10:58:39 +1000  136) 		/*
34e83fc618085 (Kent Overstreet            2013-04-26 10:58:39 +1000  137) 		 * This counts the number of available slots in the ringbuffer,
34e83fc618085 (Kent Overstreet            2013-04-26 10:58:39 +1000  138) 		 * so we avoid overflowing it: it's decremented (if positive)
34e83fc618085 (Kent Overstreet            2013-04-26 10:58:39 +1000  139) 		 * when allocating a kiocb and incremented when the resulting
34e83fc618085 (Kent Overstreet            2013-04-26 10:58:39 +1000  140) 		 * io_event is pulled off the ringbuffer.
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  141) 		 *
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  142) 		 * We batch accesses to it with a percpu version.
34e83fc618085 (Kent Overstreet            2013-04-26 10:58:39 +1000  143) 		 */
34e83fc618085 (Kent Overstreet            2013-04-26 10:58:39 +1000  144) 		atomic_t	reqs_available;
4e23bcaeb9e8d (Kent Overstreet            2013-05-07 16:18:56 -0700  145) 	} ____cacheline_aligned_in_smp;
4e23bcaeb9e8d (Kent Overstreet            2013-05-07 16:18:56 -0700  146) 
4e23bcaeb9e8d (Kent Overstreet            2013-05-07 16:18:56 -0700  147) 	struct {
4e23bcaeb9e8d (Kent Overstreet            2013-05-07 16:18:56 -0700  148) 		spinlock_t	ctx_lock;
4e23bcaeb9e8d (Kent Overstreet            2013-05-07 16:18:56 -0700  149) 		struct list_head active_reqs;	/* used for cancellation */
4e23bcaeb9e8d (Kent Overstreet            2013-05-07 16:18:56 -0700  150) 	} ____cacheline_aligned_in_smp;
4e23bcaeb9e8d (Kent Overstreet            2013-05-07 16:18:56 -0700  151) 
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  152) 	struct {
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  153) 		struct mutex	ring_lock;
4e23bcaeb9e8d (Kent Overstreet            2013-05-07 16:18:56 -0700  154) 		wait_queue_head_t wait;
4e23bcaeb9e8d (Kent Overstreet            2013-05-07 16:18:56 -0700  155) 	} ____cacheline_aligned_in_smp;
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  156) 
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  157) 	struct {
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  158) 		unsigned	tail;
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  159) 		unsigned	completed_events;
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  160) 		spinlock_t	completion_lock;
4e23bcaeb9e8d (Kent Overstreet            2013-05-07 16:18:56 -0700  161) 	} ____cacheline_aligned_in_smp;
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  162) 
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  163) 	struct page		*internal_pages[AIO_RING_PAGES];
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  164) 	struct file		*aio_ring_file;
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  165) 
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  166) 	unsigned		id;
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700  167) };
4e179bca67186 (Kent Overstreet            2013-05-07 16:18:33 -0700  168) 
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800  169) /*
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800  170)  * First field must be the file pointer in all the
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800  171)  * iocb unions! See also 'struct kiocb' in <linux/fs.h>
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800  172)  */
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200  173) struct fsync_iocb {
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200  174) 	struct file		*file;
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800  175) 	struct work_struct	work;
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200  176) 	bool			datasync;
530f32fc370fd (Miklos Szeredi             2020-05-14 16:44:24 +0200  177) 	struct cred		*creds;
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200  178) };
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200  179) 
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200  180) struct poll_iocb {
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200  181) 	struct file		*file;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200  182) 	struct wait_queue_head	*head;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200  183) 	__poll_t		events;
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500  184) 	bool			done;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200  185) 	bool			cancelled;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200  186) 	struct wait_queue_entry	wait;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200  187) 	struct work_struct	work;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200  188) };
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200  189) 
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800  190) /*
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800  191)  * NOTE! Each of the iocb union members has the file pointer
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800  192)  * as the first entry in their struct definition. So you can
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800  193)  * access the file pointer through any of the sub-structs,
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800  194)  * or directly as just 'ki_filp' in this struct.
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800  195)  */
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100  196) struct aio_kiocb {
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200  197) 	union {
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800  198) 		struct file		*ki_filp;
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200  199) 		struct kiocb		rw;
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200  200) 		struct fsync_iocb	fsync;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200  201) 		struct poll_iocb	poll;
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200  202) 	};
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100  203) 
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100  204) 	struct kioctx		*ki_ctx;
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100  205) 	kiocb_cancel_fn		*ki_cancel;
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100  206) 
a9339b7855094 (Al Viro                    2019-03-07 19:43:45 -0500  207) 	struct io_event		ki_res;
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100  208) 
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100  209) 	struct list_head	ki_list;	/* the aio core uses this
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100  210) 						 * for cancellation */
9018ccc453af0 (Christoph Hellwig          2018-07-24 11:36:37 +0200  211) 	refcount_t		ki_refcnt;
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100  212) 
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100  213) 	/*
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100  214) 	 * If the aio_resfd field of the userspace iocb is not zero,
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100  215) 	 * this is the underlying eventfd context to deliver events to.
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100  216) 	 */
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100  217) 	struct eventfd_ctx	*ki_eventfd;
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100  218) };
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100  219) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  220) /*------ sysctl variables----*/
d55b5fdaf4084 (Zach Brown                 2005-11-07 00:59:31 -0800  221) static DEFINE_SPINLOCK(aio_nr_lock);
d55b5fdaf4084 (Zach Brown                 2005-11-07 00:59:31 -0800  222) unsigned long aio_nr;		/* current system wide number of aio requests */
d55b5fdaf4084 (Zach Brown                 2005-11-07 00:59:31 -0800  223) unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  224) /*----end sysctl variables---*/
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  225) 
e18b890bb0881 (Christoph Lameter          2006-12-06 20:33:20 -0800  226) static struct kmem_cache	*kiocb_cachep;
e18b890bb0881 (Christoph Lameter          2006-12-06 20:33:20 -0800  227) static struct kmem_cache	*kioctx_cachep;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  228) 
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  229) static struct vfsmount *aio_mnt;
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  230) 
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  231) static const struct file_operations aio_ring_fops;
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  232) static const struct address_space_operations aio_ctx_aops;
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  233) 
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  234) static struct file *aio_private_file(struct kioctx *ctx, loff_t nr_pages)
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  235) {
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  236) 	struct file *file;
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  237) 	struct inode *inode = alloc_anon_inode(aio_mnt->mnt_sb);
7f62656be8a8e (Dan Carpenter              2013-11-13 10:49:40 +0300  238) 	if (IS_ERR(inode))
7f62656be8a8e (Dan Carpenter              2013-11-13 10:49:40 +0300  239) 		return ERR_CAST(inode);
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  240) 
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  241) 	inode->i_mapping->a_ops = &aio_ctx_aops;
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  242) 	inode->i_mapping->private_data = ctx;
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  243) 	inode->i_size = PAGE_SIZE * nr_pages;
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  244) 
d93aa9d82aea8 (Al Viro                    2018-06-09 09:40:05 -0400  245) 	file = alloc_file_pseudo(inode, aio_mnt, "[aio]",
d93aa9d82aea8 (Al Viro                    2018-06-09 09:40:05 -0400  246) 				O_RDWR, &aio_ring_fops);
c9c554f21490b (Al Viro                    2018-07-11 14:19:04 -0400  247) 	if (IS_ERR(file))
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  248) 		iput(inode);
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  249) 	return file;
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  250) }
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  251) 
52db59df17c0c (David Howells              2019-03-25 16:38:23 +0000  252) static int aio_init_fs_context(struct fs_context *fc)
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  253) {
52db59df17c0c (David Howells              2019-03-25 16:38:23 +0000  254) 	if (!init_pseudo(fc, AIO_RING_MAGIC))
52db59df17c0c (David Howells              2019-03-25 16:38:23 +0000  255) 		return -ENOMEM;
52db59df17c0c (David Howells              2019-03-25 16:38:23 +0000  256) 	fc->s_iflags |= SB_I_NOEXEC;
52db59df17c0c (David Howells              2019-03-25 16:38:23 +0000  257) 	return 0;
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  258) }
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  259) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  260) /* aio_setup
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  261)  *	Creates the slab caches used by the aio routines, panic on
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  262)  *	failure as this is done early during the boot sequence.
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  263)  */
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  264) static int __init aio_setup(void)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  265) {
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  266) 	static struct file_system_type aio_fs = {
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  267) 		.name		= "aio",
52db59df17c0c (David Howells              2019-03-25 16:38:23 +0000  268) 		.init_fs_context = aio_init_fs_context,
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  269) 		.kill_sb	= kill_anon_super,
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  270) 	};
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  271) 	aio_mnt = kern_mount(&aio_fs);
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  272) 	if (IS_ERR(aio_mnt))
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  273) 		panic("Failed to create aio fs mount.");
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  274) 
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100  275) 	kiocb_cachep = KMEM_CACHE(aio_kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
0a31bd5f2bbb6 (Christoph Lameter          2007-05-06 14:49:57 -0700  276) 	kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  277) 	return 0;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  278) }
385773e04806e (H Hartley Sweeten          2009-09-22 16:43:53 -0700  279) __initcall(aio_setup);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  280) 
5e9ae2e5da0be (Benjamin LaHaise           2013-09-26 20:34:51 -0400  281) static void put_aio_ring_file(struct kioctx *ctx)
5e9ae2e5da0be (Benjamin LaHaise           2013-09-26 20:34:51 -0400  282) {
5e9ae2e5da0be (Benjamin LaHaise           2013-09-26 20:34:51 -0400  283) 	struct file *aio_ring_file = ctx->aio_ring_file;
de04e76935ad5 (Rasmus Villemoes           2016-09-15 00:25:03 +0200  284) 	struct address_space *i_mapping;
de04e76935ad5 (Rasmus Villemoes           2016-09-15 00:25:03 +0200  285) 
5e9ae2e5da0be (Benjamin LaHaise           2013-09-26 20:34:51 -0400  286) 	if (aio_ring_file) {
450630975da9e (Al Viro                    2016-12-04 18:24:56 -0500  287) 		truncate_setsize(file_inode(aio_ring_file), 0);
5e9ae2e5da0be (Benjamin LaHaise           2013-09-26 20:34:51 -0400  288) 
5e9ae2e5da0be (Benjamin LaHaise           2013-09-26 20:34:51 -0400  289) 		/* Prevent further access to the kioctx from migratepages */
450630975da9e (Al Viro                    2016-12-04 18:24:56 -0500  290) 		i_mapping = aio_ring_file->f_mapping;
de04e76935ad5 (Rasmus Villemoes           2016-09-15 00:25:03 +0200  291) 		spin_lock(&i_mapping->private_lock);
de04e76935ad5 (Rasmus Villemoes           2016-09-15 00:25:03 +0200  292) 		i_mapping->private_data = NULL;
5e9ae2e5da0be (Benjamin LaHaise           2013-09-26 20:34:51 -0400  293) 		ctx->aio_ring_file = NULL;
de04e76935ad5 (Rasmus Villemoes           2016-09-15 00:25:03 +0200  294) 		spin_unlock(&i_mapping->private_lock);
5e9ae2e5da0be (Benjamin LaHaise           2013-09-26 20:34:51 -0400  295) 
5e9ae2e5da0be (Benjamin LaHaise           2013-09-26 20:34:51 -0400  296) 		fput(aio_ring_file);
5e9ae2e5da0be (Benjamin LaHaise           2013-09-26 20:34:51 -0400  297) 	}
5e9ae2e5da0be (Benjamin LaHaise           2013-09-26 20:34:51 -0400  298) }
5e9ae2e5da0be (Benjamin LaHaise           2013-09-26 20:34:51 -0400  299) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  300) static void aio_free_ring(struct kioctx *ctx)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  301) {
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  302) 	int i;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  303) 
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  304) 	/* Disconnect the kiotx from the ring file.  This prevents future
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  305) 	 * accesses to the kioctx from page migration.
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  306) 	 */
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  307) 	put_aio_ring_file(ctx);
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  308) 
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  309) 	for (i = 0; i < ctx->nr_pages; i++) {
8e321fefb0e60 (Benjamin LaHaise           2013-12-21 17:56:08 -0500  310) 		struct page *page;
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  311) 		pr_debug("pid(%d) [%d] page->count=%d\n", current->pid, i,
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  312) 				page_count(ctx->ring_pages[i]));
8e321fefb0e60 (Benjamin LaHaise           2013-12-21 17:56:08 -0500  313) 		page = ctx->ring_pages[i];
8e321fefb0e60 (Benjamin LaHaise           2013-12-21 17:56:08 -0500  314) 		if (!page)
8e321fefb0e60 (Benjamin LaHaise           2013-12-21 17:56:08 -0500  315) 			continue;
8e321fefb0e60 (Benjamin LaHaise           2013-12-21 17:56:08 -0500  316) 		ctx->ring_pages[i] = NULL;
8e321fefb0e60 (Benjamin LaHaise           2013-12-21 17:56:08 -0500  317) 		put_page(page);
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  318) 	}
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  319) 
ddb8c45ba1514 (Sasha Levin                2013-11-19 17:33:03 -0500  320) 	if (ctx->ring_pages && ctx->ring_pages != ctx->internal_pages) {
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  321) 		kfree(ctx->ring_pages);
ddb8c45ba1514 (Sasha Levin                2013-11-19 17:33:03 -0500  322) 		ctx->ring_pages = NULL;
ddb8c45ba1514 (Sasha Levin                2013-11-19 17:33:03 -0500  323) 	}
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  324) }
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  325) 
14d071134c740 (Brian Geffon               2021-04-29 22:57:48 -0700  326) static int aio_ring_mremap(struct vm_area_struct *vma)
e4a0d3e720e7e (Pavel Emelyanov            2014-09-18 19:56:17 +0400  327) {
5477e70a6420a (Oleg Nesterov              2015-09-04 15:48:04 -0700  328) 	struct file *file = vma->vm_file;
e4a0d3e720e7e (Pavel Emelyanov            2014-09-18 19:56:17 +0400  329) 	struct mm_struct *mm = vma->vm_mm;
e4a0d3e720e7e (Pavel Emelyanov            2014-09-18 19:56:17 +0400  330) 	struct kioctx_table *table;
b2edffdd912b4 (Al Viro                    2015-04-06 17:48:54 -0400  331) 	int i, res = -EINVAL;
e4a0d3e720e7e (Pavel Emelyanov            2014-09-18 19:56:17 +0400  332) 
e4a0d3e720e7e (Pavel Emelyanov            2014-09-18 19:56:17 +0400  333) 	spin_lock(&mm->ioctx_lock);
e4a0d3e720e7e (Pavel Emelyanov            2014-09-18 19:56:17 +0400  334) 	rcu_read_lock();
e4a0d3e720e7e (Pavel Emelyanov            2014-09-18 19:56:17 +0400  335) 	table = rcu_dereference(mm->ioctx_table);
e4a0d3e720e7e (Pavel Emelyanov            2014-09-18 19:56:17 +0400  336) 	for (i = 0; i < table->nr; i++) {
e4a0d3e720e7e (Pavel Emelyanov            2014-09-18 19:56:17 +0400  337) 		struct kioctx *ctx;
e4a0d3e720e7e (Pavel Emelyanov            2014-09-18 19:56:17 +0400  338) 
d0264c01e7587 (Tejun Heo                  2018-03-14 12:10:17 -0700  339) 		ctx = rcu_dereference(table->table[i]);
e4a0d3e720e7e (Pavel Emelyanov            2014-09-18 19:56:17 +0400  340) 		if (ctx && ctx->aio_ring_file == file) {
b2edffdd912b4 (Al Viro                    2015-04-06 17:48:54 -0400  341) 			if (!atomic_read(&ctx->dead)) {
b2edffdd912b4 (Al Viro                    2015-04-06 17:48:54 -0400  342) 				ctx->user_id = ctx->mmap_base = vma->vm_start;
b2edffdd912b4 (Al Viro                    2015-04-06 17:48:54 -0400  343) 				res = 0;
b2edffdd912b4 (Al Viro                    2015-04-06 17:48:54 -0400  344) 			}
e4a0d3e720e7e (Pavel Emelyanov            2014-09-18 19:56:17 +0400  345) 			break;
e4a0d3e720e7e (Pavel Emelyanov            2014-09-18 19:56:17 +0400  346) 		}
e4a0d3e720e7e (Pavel Emelyanov            2014-09-18 19:56:17 +0400  347) 	}
e4a0d3e720e7e (Pavel Emelyanov            2014-09-18 19:56:17 +0400  348) 
e4a0d3e720e7e (Pavel Emelyanov            2014-09-18 19:56:17 +0400  349) 	rcu_read_unlock();
e4a0d3e720e7e (Pavel Emelyanov            2014-09-18 19:56:17 +0400  350) 	spin_unlock(&mm->ioctx_lock);
b2edffdd912b4 (Al Viro                    2015-04-06 17:48:54 -0400  351) 	return res;
e4a0d3e720e7e (Pavel Emelyanov            2014-09-18 19:56:17 +0400  352) }
e4a0d3e720e7e (Pavel Emelyanov            2014-09-18 19:56:17 +0400  353) 
5477e70a6420a (Oleg Nesterov              2015-09-04 15:48:04 -0700  354) static const struct vm_operations_struct aio_ring_vm_ops = {
5477e70a6420a (Oleg Nesterov              2015-09-04 15:48:04 -0700  355) 	.mremap		= aio_ring_mremap,
5477e70a6420a (Oleg Nesterov              2015-09-04 15:48:04 -0700  356) #if IS_ENABLED(CONFIG_MMU)
5477e70a6420a (Oleg Nesterov              2015-09-04 15:48:04 -0700  357) 	.fault		= filemap_fault,
5477e70a6420a (Oleg Nesterov              2015-09-04 15:48:04 -0700  358) 	.map_pages	= filemap_map_pages,
5477e70a6420a (Oleg Nesterov              2015-09-04 15:48:04 -0700  359) 	.page_mkwrite	= filemap_page_mkwrite,
5477e70a6420a (Oleg Nesterov              2015-09-04 15:48:04 -0700  360) #endif
5477e70a6420a (Oleg Nesterov              2015-09-04 15:48:04 -0700  361) };
5477e70a6420a (Oleg Nesterov              2015-09-04 15:48:04 -0700  362) 
5477e70a6420a (Oleg Nesterov              2015-09-04 15:48:04 -0700  363) static int aio_ring_mmap(struct file *file, struct vm_area_struct *vma)
5477e70a6420a (Oleg Nesterov              2015-09-04 15:48:04 -0700  364) {
5477e70a6420a (Oleg Nesterov              2015-09-04 15:48:04 -0700  365) 	vma->vm_flags |= VM_DONTEXPAND;
5477e70a6420a (Oleg Nesterov              2015-09-04 15:48:04 -0700  366) 	vma->vm_ops = &aio_ring_vm_ops;
5477e70a6420a (Oleg Nesterov              2015-09-04 15:48:04 -0700  367) 	return 0;
5477e70a6420a (Oleg Nesterov              2015-09-04 15:48:04 -0700  368) }
5477e70a6420a (Oleg Nesterov              2015-09-04 15:48:04 -0700  369) 
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  370) static const struct file_operations aio_ring_fops = {
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  371) 	.mmap = aio_ring_mmap,
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  372) };
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  373) 
0c45355fc7c48 (Benjamin LaHaise           2013-07-17 09:34:24 -0400  374) #if IS_ENABLED(CONFIG_MIGRATION)
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  375) static int aio_migratepage(struct address_space *mapping, struct page *new,
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  376) 			struct page *old, enum migrate_mode mode)
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  377) {
5e9ae2e5da0be (Benjamin LaHaise           2013-09-26 20:34:51 -0400  378) 	struct kioctx *ctx;
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  379) 	unsigned long flags;
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  380) 	pgoff_t idx;
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  381) 	int rc;
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  382) 
2916ecc0f9d43 (Jérôme Glisse              2017-09-08 16:12:06 -0700  383) 	/*
2916ecc0f9d43 (Jérôme Glisse              2017-09-08 16:12:06 -0700  384) 	 * We cannot support the _NO_COPY case here, because copy needs to
2916ecc0f9d43 (Jérôme Glisse              2017-09-08 16:12:06 -0700  385) 	 * happen under the ctx->completion_lock. That does not work with the
2916ecc0f9d43 (Jérôme Glisse              2017-09-08 16:12:06 -0700  386) 	 * migration workflow of MIGRATE_SYNC_NO_COPY.
2916ecc0f9d43 (Jérôme Glisse              2017-09-08 16:12:06 -0700  387) 	 */
2916ecc0f9d43 (Jérôme Glisse              2017-09-08 16:12:06 -0700  388) 	if (mode == MIGRATE_SYNC_NO_COPY)
2916ecc0f9d43 (Jérôme Glisse              2017-09-08 16:12:06 -0700  389) 		return -EINVAL;
2916ecc0f9d43 (Jérôme Glisse              2017-09-08 16:12:06 -0700  390) 
8e321fefb0e60 (Benjamin LaHaise           2013-12-21 17:56:08 -0500  391) 	rc = 0;
8e321fefb0e60 (Benjamin LaHaise           2013-12-21 17:56:08 -0500  392) 
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  393) 	/* mapping->private_lock here protects against the kioctx teardown.  */
8e321fefb0e60 (Benjamin LaHaise           2013-12-21 17:56:08 -0500  394) 	spin_lock(&mapping->private_lock);
8e321fefb0e60 (Benjamin LaHaise           2013-12-21 17:56:08 -0500  395) 	ctx = mapping->private_data;
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  396) 	if (!ctx) {
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  397) 		rc = -EINVAL;
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  398) 		goto out;
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  399) 	}
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  400) 
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  401) 	/* The ring_lock mutex.  The prevents aio_read_events() from writing
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  402) 	 * to the ring's head, and prevents page migration from mucking in
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  403) 	 * a partially initialized kiotx.
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  404) 	 */
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  405) 	if (!mutex_trylock(&ctx->ring_lock)) {
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  406) 		rc = -EAGAIN;
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  407) 		goto out;
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  408) 	}
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  409) 
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  410) 	idx = old->index;
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  411) 	if (idx < (pgoff_t)ctx->nr_pages) {
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  412) 		/* Make sure the old page hasn't already been changed */
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  413) 		if (ctx->ring_pages[idx] != old)
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  414) 			rc = -EAGAIN;
8e321fefb0e60 (Benjamin LaHaise           2013-12-21 17:56:08 -0500  415) 	} else
8e321fefb0e60 (Benjamin LaHaise           2013-12-21 17:56:08 -0500  416) 		rc = -EINVAL;
8e321fefb0e60 (Benjamin LaHaise           2013-12-21 17:56:08 -0500  417) 
8e321fefb0e60 (Benjamin LaHaise           2013-12-21 17:56:08 -0500  418) 	if (rc != 0)
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  419) 		goto out_unlock;
8e321fefb0e60 (Benjamin LaHaise           2013-12-21 17:56:08 -0500  420) 
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  421) 	/* Writeback must be complete */
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  422) 	BUG_ON(PageWriteback(old));
8e321fefb0e60 (Benjamin LaHaise           2013-12-21 17:56:08 -0500  423) 	get_page(new);
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  424) 
371096949f0ad (Keith Busch                2019-07-18 15:58:46 -0700  425) 	rc = migrate_page_move_mapping(mapping, new, old, 1);
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  426) 	if (rc != MIGRATEPAGE_SUCCESS) {
8e321fefb0e60 (Benjamin LaHaise           2013-12-21 17:56:08 -0500  427) 		put_page(new);
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  428) 		goto out_unlock;
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  429) 	}
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  430) 
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  431) 	/* Take completion_lock to prevent other writes to the ring buffer
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  432) 	 * while the old page is copied to the new.  This prevents new
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  433) 	 * events from being lost.
5e9ae2e5da0be (Benjamin LaHaise           2013-09-26 20:34:51 -0400  434) 	 */
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  435) 	spin_lock_irqsave(&ctx->completion_lock, flags);
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  436) 	migrate_page_copy(new, old);
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  437) 	BUG_ON(ctx->ring_pages[idx] != old);
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  438) 	ctx->ring_pages[idx] = new;
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  439) 	spin_unlock_irqrestore(&ctx->completion_lock, flags);
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  440) 
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  441) 	/* The old page is no longer accessible. */
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  442) 	put_page(old);
8e321fefb0e60 (Benjamin LaHaise           2013-12-21 17:56:08 -0500  443) 
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  444) out_unlock:
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  445) 	mutex_unlock(&ctx->ring_lock);
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  446) out:
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  447) 	spin_unlock(&mapping->private_lock);
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  448) 	return rc;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  449) }
0c45355fc7c48 (Benjamin LaHaise           2013-07-17 09:34:24 -0400  450) #endif
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  451) 
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  452) static const struct address_space_operations aio_ctx_aops = {
835f252c6debd (Gu Zheng                   2014-11-06 17:46:21 +0800  453) 	.set_page_dirty = __set_page_dirty_no_writeback,
0c45355fc7c48 (Benjamin LaHaise           2013-07-17 09:34:24 -0400  454) #if IS_ENABLED(CONFIG_MIGRATION)
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  455) 	.migratepage	= aio_migratepage,
0c45355fc7c48 (Benjamin LaHaise           2013-07-17 09:34:24 -0400  456) #endif
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  457) };
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  458) 
2a8a98673c13c (Mauricio Faria de Oliveira 2017-07-05 10:53:16 -0300  459) static int aio_setup_ring(struct kioctx *ctx, unsigned int nr_events)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  460) {
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  461) 	struct aio_ring *ring;
41003a7bcfed1 (Zach Brown                 2013-05-07 16:18:25 -0700  462) 	struct mm_struct *mm = current->mm;
3dc9acb676003 (Linus Torvalds             2013-12-20 05:11:12 +0900  463) 	unsigned long size, unused;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  464) 	int nr_pages;
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  465) 	int i;
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  466) 	struct file *file;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  467) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  468) 	/* Compensate for the ring buffer's head/tail overlap entry */
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  469) 	nr_events += 2;	/* 1 is required, 2 for good luck */
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  470) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  471) 	size = sizeof(struct aio_ring);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  472) 	size += sizeof(struct io_event) * nr_events;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  473) 
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  474) 	nr_pages = PFN_UP(size);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  475) 	if (nr_pages < 0)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  476) 		return -EINVAL;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  477) 
71ad7490c1f32 (Benjamin LaHaise           2013-09-17 10:18:25 -0400  478) 	file = aio_private_file(ctx, nr_pages);
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  479) 	if (IS_ERR(file)) {
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  480) 		ctx->aio_ring_file = NULL;
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  481) 		return -ENOMEM;
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  482) 	}
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  483) 
3dc9acb676003 (Linus Torvalds             2013-12-20 05:11:12 +0900  484) 	ctx->aio_ring_file = file;
3dc9acb676003 (Linus Torvalds             2013-12-20 05:11:12 +0900  485) 	nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring))
3dc9acb676003 (Linus Torvalds             2013-12-20 05:11:12 +0900  486) 			/ sizeof(struct io_event);
3dc9acb676003 (Linus Torvalds             2013-12-20 05:11:12 +0900  487) 
3dc9acb676003 (Linus Torvalds             2013-12-20 05:11:12 +0900  488) 	ctx->ring_pages = ctx->internal_pages;
3dc9acb676003 (Linus Torvalds             2013-12-20 05:11:12 +0900  489) 	if (nr_pages > AIO_RING_PAGES) {
3dc9acb676003 (Linus Torvalds             2013-12-20 05:11:12 +0900  490) 		ctx->ring_pages = kcalloc(nr_pages, sizeof(struct page *),
3dc9acb676003 (Linus Torvalds             2013-12-20 05:11:12 +0900  491) 					  GFP_KERNEL);
3dc9acb676003 (Linus Torvalds             2013-12-20 05:11:12 +0900  492) 		if (!ctx->ring_pages) {
3dc9acb676003 (Linus Torvalds             2013-12-20 05:11:12 +0900  493) 			put_aio_ring_file(ctx);
3dc9acb676003 (Linus Torvalds             2013-12-20 05:11:12 +0900  494) 			return -ENOMEM;
3dc9acb676003 (Linus Torvalds             2013-12-20 05:11:12 +0900  495) 		}
3dc9acb676003 (Linus Torvalds             2013-12-20 05:11:12 +0900  496) 	}
3dc9acb676003 (Linus Torvalds             2013-12-20 05:11:12 +0900  497) 
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  498) 	for (i = 0; i < nr_pages; i++) {
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  499) 		struct page *page;
450630975da9e (Al Viro                    2016-12-04 18:24:56 -0500  500) 		page = find_or_create_page(file->f_mapping,
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  501) 					   i, GFP_HIGHUSER | __GFP_ZERO);
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  502) 		if (!page)
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  503) 			break;
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  504) 		pr_debug("pid(%d) page[%d]->count=%d\n",
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  505) 			 current->pid, i, page_count(page));
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  506) 		SetPageUptodate(page);
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  507) 		unlock_page(page);
3dc9acb676003 (Linus Torvalds             2013-12-20 05:11:12 +0900  508) 
3dc9acb676003 (Linus Torvalds             2013-12-20 05:11:12 +0900  509) 		ctx->ring_pages[i] = page;
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  510) 	}
3dc9acb676003 (Linus Torvalds             2013-12-20 05:11:12 +0900  511) 	ctx->nr_pages = i;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  512) 
3dc9acb676003 (Linus Torvalds             2013-12-20 05:11:12 +0900  513) 	if (unlikely(i != nr_pages)) {
3dc9acb676003 (Linus Torvalds             2013-12-20 05:11:12 +0900  514) 		aio_free_ring(ctx);
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  515) 		return -ENOMEM;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  516) 	}
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  517) 
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  518) 	ctx->mmap_size = nr_pages * PAGE_SIZE;
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  519) 	pr_debug("attempting mmap of %lu bytes\n", ctx->mmap_size);
36bc08cc01709 (Gu Zheng                   2013-07-16 17:56:16 +0800  520) 
d8ed45c5dcd45 (Michel Lespinasse          2020-06-08 21:33:25 -0700  521) 	if (mmap_write_lock_killable(mm)) {
013373e8b8673 (Michal Hocko               2016-05-23 16:25:59 -0700  522) 		ctx->mmap_size = 0;
013373e8b8673 (Michal Hocko               2016-05-23 16:25:59 -0700  523) 		aio_free_ring(ctx);
013373e8b8673 (Michal Hocko               2016-05-23 16:25:59 -0700  524) 		return -EINTR;
013373e8b8673 (Michal Hocko               2016-05-23 16:25:59 -0700  525) 	}
013373e8b8673 (Michal Hocko               2016-05-23 16:25:59 -0700  526) 
45e55300f1149 (Peter Collingbourne        2020-08-06 23:23:37 -0700  527) 	ctx->mmap_base = do_mmap(ctx->aio_ring_file, 0, ctx->mmap_size,
45e55300f1149 (Peter Collingbourne        2020-08-06 23:23:37 -0700  528) 				 PROT_READ | PROT_WRITE,
45e55300f1149 (Peter Collingbourne        2020-08-06 23:23:37 -0700  529) 				 MAP_SHARED, 0, &unused, NULL);
d8ed45c5dcd45 (Michel Lespinasse          2020-06-08 21:33:25 -0700  530) 	mmap_write_unlock(mm);
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  531) 	if (IS_ERR((void *)ctx->mmap_base)) {
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  532) 		ctx->mmap_size = 0;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  533) 		aio_free_ring(ctx);
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  534) 		return -ENOMEM;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  535) 	}
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  536) 
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  537) 	pr_debug("mmap address: 0x%08lx\n", ctx->mmap_base);
d6c355c7dabcd (Benjamin LaHaise           2013-09-09 11:57:59 -0400  538) 
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  539) 	ctx->user_id = ctx->mmap_base;
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  540) 	ctx->nr_events = nr_events; /* trusted copy */
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  541) 
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  542) 	ring = kmap_atomic(ctx->ring_pages[0]);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  543) 	ring->nr = nr_events;	/* user copy */
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  544) 	ring->id = ~0U;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  545) 	ring->head = ring->tail = 0;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  546) 	ring->magic = AIO_RING_MAGIC;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  547) 	ring->compat_features = AIO_RING_COMPAT_FEATURES;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  548) 	ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  549) 	ring->header_length = sizeof(struct aio_ring);
e8e3c3d66fd9d (Cong Wang                  2011-11-25 23:14:27 +0800  550) 	kunmap_atomic(ring);
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  551) 	flush_dcache_page(ctx->ring_pages[0]);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  552) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  553) 	return 0;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  554) }
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  555) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  556) #define AIO_EVENTS_PER_PAGE	(PAGE_SIZE / sizeof(struct io_event))
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  557) #define AIO_EVENTS_FIRST_PAGE	((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  558) #define AIO_EVENTS_OFFSET	(AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  559) 
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100  560) void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel)
0460fef2a9215 (Kent Overstreet            2013-05-07 16:18:49 -0700  561) {
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200  562) 	struct aio_kiocb *req = container_of(iocb, struct aio_kiocb, rw);
0460fef2a9215 (Kent Overstreet            2013-05-07 16:18:49 -0700  563) 	struct kioctx *ctx = req->ki_ctx;
0460fef2a9215 (Kent Overstreet            2013-05-07 16:18:49 -0700  564) 	unsigned long flags;
0460fef2a9215 (Kent Overstreet            2013-05-07 16:18:49 -0700  565) 
75321b50a37a5 (Christoph Hellwig          2018-04-09 14:57:56 +0200  566) 	if (WARN_ON_ONCE(!list_empty(&req->ki_list)))
75321b50a37a5 (Christoph Hellwig          2018-04-09 14:57:56 +0200  567) 		return;
0460fef2a9215 (Kent Overstreet            2013-05-07 16:18:49 -0700  568) 
75321b50a37a5 (Christoph Hellwig          2018-04-09 14:57:56 +0200  569) 	spin_lock_irqsave(&ctx->ctx_lock, flags);
75321b50a37a5 (Christoph Hellwig          2018-04-09 14:57:56 +0200  570) 	list_add_tail(&req->ki_list, &ctx->active_reqs);
0460fef2a9215 (Kent Overstreet            2013-05-07 16:18:49 -0700  571) 	req->ki_cancel = cancel;
0460fef2a9215 (Kent Overstreet            2013-05-07 16:18:49 -0700  572) 	spin_unlock_irqrestore(&ctx->ctx_lock, flags);
0460fef2a9215 (Kent Overstreet            2013-05-07 16:18:49 -0700  573) }
0460fef2a9215 (Kent Overstreet            2013-05-07 16:18:49 -0700  574) EXPORT_SYMBOL(kiocb_set_cancel_fn);
0460fef2a9215 (Kent Overstreet            2013-05-07 16:18:49 -0700  575) 
a6d7cff472eea (Tejun Heo                  2018-03-14 12:10:17 -0700  576) /*
a6d7cff472eea (Tejun Heo                  2018-03-14 12:10:17 -0700  577)  * free_ioctx() should be RCU delayed to synchronize against the RCU
a6d7cff472eea (Tejun Heo                  2018-03-14 12:10:17 -0700  578)  * protected lookup_ioctx() and also needs process context to call
f729863a8c969 (Tejun Heo                  2018-03-14 12:45:15 -0700  579)  * aio_free_ring().  Use rcu_work.
a6d7cff472eea (Tejun Heo                  2018-03-14 12:10:17 -0700  580)  */
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  581) static void free_ioctx(struct work_struct *work)
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  582) {
f729863a8c969 (Tejun Heo                  2018-03-14 12:45:15 -0700  583) 	struct kioctx *ctx = container_of(to_rcu_work(work), struct kioctx,
f729863a8c969 (Tejun Heo                  2018-03-14 12:45:15 -0700  584) 					  free_rwork);
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  585) 	pr_debug("freeing %p\n", ctx);
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  586) 
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  587) 	aio_free_ring(ctx);
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  588) 	free_percpu(ctx->cpu);
9a1049da9bd2c (Tejun Heo                  2014-06-28 08:10:14 -0400  589) 	percpu_ref_exit(&ctx->reqs);
9a1049da9bd2c (Tejun Heo                  2014-06-28 08:10:14 -0400  590) 	percpu_ref_exit(&ctx->users);
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  591) 	kmem_cache_free(kioctx_cachep, ctx);
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  592) }
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  593) 
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  594) static void free_ioctx_reqs(struct percpu_ref *ref)
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  595) {
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  596) 	struct kioctx *ctx = container_of(ref, struct kioctx, reqs);
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  597) 
e02ba72aabfad (Anatol Pomozov             2014-04-15 11:31:33 -0700  598) 	/* At this point we know that there are no any in-flight requests */
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600  599) 	if (ctx->rq_wait && atomic_dec_and_test(&ctx->rq_wait->count))
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600  600) 		complete(&ctx->rq_wait->comp);
e02ba72aabfad (Anatol Pomozov             2014-04-15 11:31:33 -0700  601) 
a6d7cff472eea (Tejun Heo                  2018-03-14 12:10:17 -0700  602) 	/* Synchronize against RCU protected table->table[] dereferences */
f729863a8c969 (Tejun Heo                  2018-03-14 12:45:15 -0700  603) 	INIT_RCU_WORK(&ctx->free_rwork, free_ioctx);
f729863a8c969 (Tejun Heo                  2018-03-14 12:45:15 -0700  604) 	queue_rcu_work(system_wq, &ctx->free_rwork);
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  605) }
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  606) 
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  607) /*
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  608)  * When this function runs, the kioctx has been removed from the "hash table"
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  609)  * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  610)  * now it's safe to cancel any that need to be.
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  611)  */
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  612) static void free_ioctx_users(struct percpu_ref *ref)
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  613) {
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  614) 	struct kioctx *ctx = container_of(ref, struct kioctx, users);
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100  615) 	struct aio_kiocb *req;
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  616) 
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  617) 	spin_lock_irq(&ctx->ctx_lock);
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  618) 
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  619) 	while (!list_empty(&ctx->active_reqs)) {
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  620) 		req = list_first_entry(&ctx->active_reqs,
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100  621) 				       struct aio_kiocb, ki_list);
888933f8fdf06 (Christoph Hellwig          2018-05-23 14:11:02 +0200  622) 		req->ki_cancel(&req->rw);
4faa99965e027 (Al Viro                    2018-05-23 22:53:22 -0400  623) 		list_del_init(&req->ki_list);
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  624) 	}
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  625) 
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  626) 	spin_unlock_irq(&ctx->ctx_lock);
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  627) 
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  628) 	percpu_ref_kill(&ctx->reqs);
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  629) 	percpu_ref_put(&ctx->reqs);
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  630) }
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  631) 
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  632) static int ioctx_add_table(struct kioctx *ctx, struct mm_struct *mm)
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  633) {
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  634) 	unsigned i, new_nr;
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  635) 	struct kioctx_table *table, *old;
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  636) 	struct aio_ring *ring;
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  637) 
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  638) 	spin_lock(&mm->ioctx_lock);
855ef0dec7271 (Oleg Nesterov              2014-04-30 16:16:36 +0200  639) 	table = rcu_dereference_raw(mm->ioctx_table);
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  640) 
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  641) 	while (1) {
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  642) 		if (table)
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  643) 			for (i = 0; i < table->nr; i++)
d0264c01e7587 (Tejun Heo                  2018-03-14 12:10:17 -0700  644) 				if (!rcu_access_pointer(table->table[i])) {
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  645) 					ctx->id = i;
d0264c01e7587 (Tejun Heo                  2018-03-14 12:10:17 -0700  646) 					rcu_assign_pointer(table->table[i], ctx);
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  647) 					spin_unlock(&mm->ioctx_lock);
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  648) 
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  649) 					/* While kioctx setup is in progress,
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  650) 					 * we are protected from page migration
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  651) 					 * changes ring_pages by ->ring_lock.
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  652) 					 */
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  653) 					ring = kmap_atomic(ctx->ring_pages[0]);
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  654) 					ring->id = ctx->id;
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  655) 					kunmap_atomic(ring);
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  656) 					return 0;
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  657) 				}
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  658) 
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  659) 		new_nr = (table ? table->nr : 1) * 4;
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  660) 		spin_unlock(&mm->ioctx_lock);
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  661) 
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  662) 		table = kzalloc(sizeof(*table) + sizeof(struct kioctx *) *
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  663) 				new_nr, GFP_KERNEL);
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  664) 		if (!table)
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  665) 			return -ENOMEM;
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  666) 
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  667) 		table->nr = new_nr;
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  668) 
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  669) 		spin_lock(&mm->ioctx_lock);
855ef0dec7271 (Oleg Nesterov              2014-04-30 16:16:36 +0200  670) 		old = rcu_dereference_raw(mm->ioctx_table);
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  671) 
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  672) 		if (!old) {
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  673) 			rcu_assign_pointer(mm->ioctx_table, table);
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  674) 		} else if (table->nr > old->nr) {
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  675) 			memcpy(table->table, old->table,
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  676) 			       old->nr * sizeof(struct kioctx *));
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  677) 
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  678) 			rcu_assign_pointer(mm->ioctx_table, table);
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  679) 			kfree_rcu(old, rcu);
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  680) 		} else {
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  681) 			kfree(table);
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  682) 			table = old;
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  683) 		}
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  684) 	}
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  685) }
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  686) 
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  687) static void aio_nr_sub(unsigned nr)
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  688) {
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  689) 	spin_lock(&aio_nr_lock);
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  690) 	if (WARN_ON(aio_nr - nr > aio_nr))
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  691) 		aio_nr = 0;
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  692) 	else
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  693) 		aio_nr -= nr;
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  694) 	spin_unlock(&aio_nr_lock);
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  695) }
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  696) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  697) /* ioctx_alloc
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  698)  *	Allocates and initializes an ioctx.  Returns an ERR_PTR if it failed.
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  699)  */
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  700) static struct kioctx *ioctx_alloc(unsigned nr_events)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  701) {
41003a7bcfed1 (Zach Brown                 2013-05-07 16:18:25 -0700  702) 	struct mm_struct *mm = current->mm;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  703) 	struct kioctx *ctx;
e23754f880f10 (Al Viro                    2012-03-06 14:33:22 -0500  704) 	int err = -ENOMEM;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  705) 
2a8a98673c13c (Mauricio Faria de Oliveira 2017-07-05 10:53:16 -0300  706) 	/*
2a8a98673c13c (Mauricio Faria de Oliveira 2017-07-05 10:53:16 -0300  707) 	 * Store the original nr_events -- what userspace passed to io_setup(),
2a8a98673c13c (Mauricio Faria de Oliveira 2017-07-05 10:53:16 -0300  708) 	 * for counting against the global limit -- before it changes.
2a8a98673c13c (Mauricio Faria de Oliveira 2017-07-05 10:53:16 -0300  709) 	 */
2a8a98673c13c (Mauricio Faria de Oliveira 2017-07-05 10:53:16 -0300  710) 	unsigned int max_reqs = nr_events;
2a8a98673c13c (Mauricio Faria de Oliveira 2017-07-05 10:53:16 -0300  711) 
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  712) 	/*
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  713) 	 * We keep track of the number of available ringbuffer slots, to prevent
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  714) 	 * overflow (reqs_available), and we also use percpu counters for this.
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  715) 	 *
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  716) 	 * So since up to half the slots might be on other cpu's percpu counters
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  717) 	 * and unavailable, double nr_events so userspace sees what they
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  718) 	 * expected: additionally, we move req_batch slots to/from percpu
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  719) 	 * counters at a time, so make sure that isn't 0:
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  720) 	 */
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  721) 	nr_events = max(nr_events, num_possible_cpus() * 4);
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  722) 	nr_events *= 2;
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  723) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  724) 	/* Prevent overflows */
08397acdd0f02 (Al Viro                    2015-03-31 11:43:52 -0400  725) 	if (nr_events > (0x10000000U / sizeof(struct io_event))) {
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  726) 		pr_debug("ENOMEM: nr_events too high\n");
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  727) 		return ERR_PTR(-EINVAL);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  728) 	}
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  729) 
2a8a98673c13c (Mauricio Faria de Oliveira 2017-07-05 10:53:16 -0300  730) 	if (!nr_events || (unsigned long)max_reqs > aio_max_nr)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  731) 		return ERR_PTR(-EAGAIN);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  732) 
c376222960ae9 (Robert P. J. Day           2007-02-10 01:45:03 -0800  733) 	ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  734) 	if (!ctx)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  735) 		return ERR_PTR(-ENOMEM);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  736) 
2a8a98673c13c (Mauricio Faria de Oliveira 2017-07-05 10:53:16 -0300  737) 	ctx->max_reqs = max_reqs;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  738) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  739) 	spin_lock_init(&ctx->ctx_lock);
0460fef2a9215 (Kent Overstreet            2013-05-07 16:18:49 -0700  740) 	spin_lock_init(&ctx->completion_lock);
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  741) 	mutex_init(&ctx->ring_lock);
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  742) 	/* Protect against page migration throughout kiotx setup by keeping
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  743) 	 * the ring_lock mutex held until setup is complete. */
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  744) 	mutex_lock(&ctx->ring_lock);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  745) 	init_waitqueue_head(&ctx->wait);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  746) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  747) 	INIT_LIST_HEAD(&ctx->active_reqs);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  748) 
2aad2a86f6685 (Tejun Heo                  2014-09-24 13:31:50 -0400  749) 	if (percpu_ref_init(&ctx->users, free_ioctx_users, 0, GFP_KERNEL))
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  750) 		goto err;
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  751) 
2aad2a86f6685 (Tejun Heo                  2014-09-24 13:31:50 -0400  752) 	if (percpu_ref_init(&ctx->reqs, free_ioctx_reqs, 0, GFP_KERNEL))
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  753) 		goto err;
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  754) 
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  755) 	ctx->cpu = alloc_percpu(struct kioctx_cpu);
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  756) 	if (!ctx->cpu)
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  757) 		goto err;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  758) 
2a8a98673c13c (Mauricio Faria de Oliveira 2017-07-05 10:53:16 -0300  759) 	err = aio_setup_ring(ctx, nr_events);
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  760) 	if (err < 0)
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  761) 		goto err;
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  762) 
34e83fc618085 (Kent Overstreet            2013-04-26 10:58:39 +1000  763) 	atomic_set(&ctx->reqs_available, ctx->nr_events - 1);
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  764) 	ctx->req_batch = (ctx->nr_events - 1) / (num_possible_cpus() * 4);
6878ea72a5d1a (Benjamin LaHaise           2013-07-31 10:34:18 -0400  765) 	if (ctx->req_batch < 1)
6878ea72a5d1a (Benjamin LaHaise           2013-07-31 10:34:18 -0400  766) 		ctx->req_batch = 1;
34e83fc618085 (Kent Overstreet            2013-04-26 10:58:39 +1000  767) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  768) 	/* limit the number of system wide aios */
9fa1cb397fa05 (Al Viro                    2012-03-10 23:14:05 -0500  769) 	spin_lock(&aio_nr_lock);
2a8a98673c13c (Mauricio Faria de Oliveira 2017-07-05 10:53:16 -0300  770) 	if (aio_nr + ctx->max_reqs > aio_max_nr ||
2a8a98673c13c (Mauricio Faria de Oliveira 2017-07-05 10:53:16 -0300  771) 	    aio_nr + ctx->max_reqs < aio_nr) {
9fa1cb397fa05 (Al Viro                    2012-03-10 23:14:05 -0500  772) 		spin_unlock(&aio_nr_lock);
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  773) 		err = -EAGAIN;
d1b9432712a25 (Gu Zheng                   2013-12-04 18:19:06 +0800  774) 		goto err_ctx;
2dd542b7aeb1c (Al Viro                    2012-03-10 23:10:35 -0500  775) 	}
2dd542b7aeb1c (Al Viro                    2012-03-10 23:10:35 -0500  776) 	aio_nr += ctx->max_reqs;
9fa1cb397fa05 (Al Viro                    2012-03-10 23:14:05 -0500  777) 	spin_unlock(&aio_nr_lock);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  778) 
1881686f84206 (Benjamin LaHaise           2013-12-21 15:49:28 -0500  779) 	percpu_ref_get(&ctx->users);	/* io_setup() will drop this ref */
1881686f84206 (Benjamin LaHaise           2013-12-21 15:49:28 -0500  780) 	percpu_ref_get(&ctx->reqs);	/* free_ioctx_users() will drop this */
723be6e39d142 (Kent Overstreet            2013-05-28 15:14:48 -0700  781) 
da90382c2ec36 (Benjamin LaHaise           2013-08-05 13:21:43 -0400  782) 	err = ioctx_add_table(ctx, mm);
da90382c2ec36 (Benjamin LaHaise           2013-08-05 13:21:43 -0400  783) 	if (err)
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  784) 		goto err_cleanup;
da90382c2ec36 (Benjamin LaHaise           2013-08-05 13:21:43 -0400  785) 
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  786) 	/* Release the ring_lock mutex now that all setup is complete. */
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  787) 	mutex_unlock(&ctx->ring_lock);
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  788) 
caf4167aa73be (Kent Overstreet            2013-05-07 16:18:35 -0700  789) 	pr_debug("allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  790) 		 ctx, ctx->user_id, mm, ctx->nr_events);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  791) 	return ctx;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  792) 
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  793) err_cleanup:
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  794) 	aio_nr_sub(ctx->max_reqs);
d1b9432712a25 (Gu Zheng                   2013-12-04 18:19:06 +0800  795) err_ctx:
deeb8525f9bce (Al Viro                    2015-04-06 17:57:44 -0400  796) 	atomic_set(&ctx->dead, 1);
deeb8525f9bce (Al Viro                    2015-04-06 17:57:44 -0400  797) 	if (ctx->mmap_size)
deeb8525f9bce (Al Viro                    2015-04-06 17:57:44 -0400  798) 		vm_munmap(ctx->mmap_base, ctx->mmap_size);
d1b9432712a25 (Gu Zheng                   2013-12-04 18:19:06 +0800  799) 	aio_free_ring(ctx);
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700  800) err:
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400  801) 	mutex_unlock(&ctx->ring_lock);
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  802) 	free_percpu(ctx->cpu);
9a1049da9bd2c (Tejun Heo                  2014-06-28 08:10:14 -0400  803) 	percpu_ref_exit(&ctx->reqs);
9a1049da9bd2c (Tejun Heo                  2014-06-28 08:10:14 -0400  804) 	percpu_ref_exit(&ctx->users);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  805) 	kmem_cache_free(kioctx_cachep, ctx);
caf4167aa73be (Kent Overstreet            2013-05-07 16:18:35 -0700  806) 	pr_debug("error allocating ioctx %d\n", err);
e23754f880f10 (Al Viro                    2012-03-06 14:33:22 -0500  807) 	return ERR_PTR(err);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  808) }
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  809) 
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  810) /* kill_ioctx
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  811)  *	Cancels all outstanding aio requests on an aio context.  Used
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  812)  *	when the processes owning a context have all exited to encourage
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  813)  *	the rapid destruction of the kioctx.
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  814)  */
fb2d44838320b (Benjamin LaHaise           2014-04-29 12:45:17 -0400  815) static int kill_ioctx(struct mm_struct *mm, struct kioctx *ctx,
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600  816) 		      struct ctx_rq_wait *wait)
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  817) {
fa88b6f8803c8 (Benjamin LaHaise           2014-04-29 12:55:48 -0400  818) 	struct kioctx_table *table;
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  819) 
b2edffdd912b4 (Al Viro                    2015-04-06 17:48:54 -0400  820) 	spin_lock(&mm->ioctx_lock);
b2edffdd912b4 (Al Viro                    2015-04-06 17:48:54 -0400  821) 	if (atomic_xchg(&ctx->dead, 1)) {
b2edffdd912b4 (Al Viro                    2015-04-06 17:48:54 -0400  822) 		spin_unlock(&mm->ioctx_lock);
fa88b6f8803c8 (Benjamin LaHaise           2014-04-29 12:55:48 -0400  823) 		return -EINVAL;
b2edffdd912b4 (Al Viro                    2015-04-06 17:48:54 -0400  824) 	}
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  825) 
855ef0dec7271 (Oleg Nesterov              2014-04-30 16:16:36 +0200  826) 	table = rcu_dereference_raw(mm->ioctx_table);
d0264c01e7587 (Tejun Heo                  2018-03-14 12:10:17 -0700  827) 	WARN_ON(ctx != rcu_access_pointer(table->table[ctx->id]));
d0264c01e7587 (Tejun Heo                  2018-03-14 12:10:17 -0700  828) 	RCU_INIT_POINTER(table->table[ctx->id], NULL);
fa88b6f8803c8 (Benjamin LaHaise           2014-04-29 12:55:48 -0400  829) 	spin_unlock(&mm->ioctx_lock);
4fcc712f5c48b (Kent Overstreet            2013-06-12 14:04:59 -0700  830) 
a6d7cff472eea (Tejun Heo                  2018-03-14 12:10:17 -0700  831) 	/* free_ioctx_reqs() will do the necessary RCU synchronization */
fa88b6f8803c8 (Benjamin LaHaise           2014-04-29 12:55:48 -0400  832) 	wake_up_all(&ctx->wait);
4fcc712f5c48b (Kent Overstreet            2013-06-12 14:04:59 -0700  833) 
fa88b6f8803c8 (Benjamin LaHaise           2014-04-29 12:55:48 -0400  834) 	/*
fa88b6f8803c8 (Benjamin LaHaise           2014-04-29 12:55:48 -0400  835) 	 * It'd be more correct to do this in free_ioctx(), after all
fa88b6f8803c8 (Benjamin LaHaise           2014-04-29 12:55:48 -0400  836) 	 * the outstanding kiocbs have finished - but by then io_destroy
fa88b6f8803c8 (Benjamin LaHaise           2014-04-29 12:55:48 -0400  837) 	 * has already returned, so io_setup() could potentially return
fa88b6f8803c8 (Benjamin LaHaise           2014-04-29 12:55:48 -0400  838) 	 * -EAGAIN with no ioctxs actually in use (as far as userspace
fa88b6f8803c8 (Benjamin LaHaise           2014-04-29 12:55:48 -0400  839) 	 *  could tell).
fa88b6f8803c8 (Benjamin LaHaise           2014-04-29 12:55:48 -0400  840) 	 */
fa88b6f8803c8 (Benjamin LaHaise           2014-04-29 12:55:48 -0400  841) 	aio_nr_sub(ctx->max_reqs);
4fcc712f5c48b (Kent Overstreet            2013-06-12 14:04:59 -0700  842) 
fa88b6f8803c8 (Benjamin LaHaise           2014-04-29 12:55:48 -0400  843) 	if (ctx->mmap_size)
fa88b6f8803c8 (Benjamin LaHaise           2014-04-29 12:55:48 -0400  844) 		vm_munmap(ctx->mmap_base, ctx->mmap_size);
fb2d44838320b (Benjamin LaHaise           2014-04-29 12:45:17 -0400  845) 
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600  846) 	ctx->rq_wait = wait;
fa88b6f8803c8 (Benjamin LaHaise           2014-04-29 12:55:48 -0400  847) 	percpu_ref_kill(&ctx->users);
fa88b6f8803c8 (Benjamin LaHaise           2014-04-29 12:55:48 -0400  848) 	return 0;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  849) }
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  850) 
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  851) /*
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  852)  * exit_aio: called when the last user of mm goes away.  At this point, there is
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  853)  * no way for any new requests to be submited or any of the io_* syscalls to be
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  854)  * called on the context.
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  855)  *
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  856)  * There may be outstanding kiocbs, but free_ioctx() will explicitly wait on
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  857)  * them.
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  858)  */
fc9b52cd8f5f4 (Harvey Harrison            2008-02-08 04:19:52 -0800  859) void exit_aio(struct mm_struct *mm)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  860) {
4b70ac5fd9b58 (Oleg Nesterov              2014-04-30 19:02:48 +0200  861) 	struct kioctx_table *table = rcu_dereference_raw(mm->ioctx_table);
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600  862) 	struct ctx_rq_wait wait;
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600  863) 	int i, skipped;
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  864) 
4b70ac5fd9b58 (Oleg Nesterov              2014-04-30 19:02:48 +0200  865) 	if (!table)
4b70ac5fd9b58 (Oleg Nesterov              2014-04-30 19:02:48 +0200  866) 		return;
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400  867) 
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600  868) 	atomic_set(&wait.count, table->nr);
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600  869) 	init_completion(&wait.comp);
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600  870) 
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600  871) 	skipped = 0;
4b70ac5fd9b58 (Oleg Nesterov              2014-04-30 19:02:48 +0200  872) 	for (i = 0; i < table->nr; ++i) {
d0264c01e7587 (Tejun Heo                  2018-03-14 12:10:17 -0700  873) 		struct kioctx *ctx =
d0264c01e7587 (Tejun Heo                  2018-03-14 12:10:17 -0700  874) 			rcu_dereference_protected(table->table[i], true);
abf137dd77121 (Jens Axboe                 2008-12-09 08:11:22 +0100  875) 
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600  876) 		if (!ctx) {
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600  877) 			skipped++;
4b70ac5fd9b58 (Oleg Nesterov              2014-04-30 19:02:48 +0200  878) 			continue;
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600  879) 		}
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600  880) 
936af1576e4c2 (Al Viro                    2012-04-20 21:49:41 -0400  881) 		/*
4b70ac5fd9b58 (Oleg Nesterov              2014-04-30 19:02:48 +0200  882) 		 * We don't need to bother with munmap() here - exit_mmap(mm)
4b70ac5fd9b58 (Oleg Nesterov              2014-04-30 19:02:48 +0200  883) 		 * is coming and it'll unmap everything. And we simply can't,
4b70ac5fd9b58 (Oleg Nesterov              2014-04-30 19:02:48 +0200  884) 		 * this is not necessarily our ->mm.
4b70ac5fd9b58 (Oleg Nesterov              2014-04-30 19:02:48 +0200  885) 		 * Since kill_ioctx() uses non-zero ->mmap_size as indicator
4b70ac5fd9b58 (Oleg Nesterov              2014-04-30 19:02:48 +0200  886) 		 * that it needs to unmap the area, just set it to 0.
936af1576e4c2 (Al Viro                    2012-04-20 21:49:41 -0400  887) 		 */
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700  888) 		ctx->mmap_size = 0;
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600  889) 		kill_ioctx(mm, ctx, &wait);
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600  890) 	}
36f5588905c10 (Kent Overstreet            2013-05-07 16:18:41 -0700  891) 
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600  892) 	if (!atomic_sub_and_test(skipped, &wait.count)) {
6098b45b32e6b (Gu Zheng                   2014-09-03 17:45:44 +0800  893) 		/* Wait until all IO for the context are done. */
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600  894) 		wait_for_completion(&wait.comp);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  895) 	}
4b70ac5fd9b58 (Oleg Nesterov              2014-04-30 19:02:48 +0200  896) 
4b70ac5fd9b58 (Oleg Nesterov              2014-04-30 19:02:48 +0200  897) 	RCU_INIT_POINTER(mm->ioctx_table, NULL);
4b70ac5fd9b58 (Oleg Nesterov              2014-04-30 19:02:48 +0200  898) 	kfree(table);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  899) }
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700  900) 
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  901) static void put_reqs_available(struct kioctx *ctx, unsigned nr)
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  902) {
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  903) 	struct kioctx_cpu *kcpu;
263782c1c95bb (Benjamin LaHaise           2014-07-14 12:49:26 -0400  904) 	unsigned long flags;
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  905) 
263782c1c95bb (Benjamin LaHaise           2014-07-14 12:49:26 -0400  906) 	local_irq_save(flags);
be6fb451a2458 (Benjamin LaHaise           2014-07-22 09:56:56 -0400  907) 	kcpu = this_cpu_ptr(ctx->cpu);
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  908) 	kcpu->reqs_available += nr;
263782c1c95bb (Benjamin LaHaise           2014-07-14 12:49:26 -0400  909) 
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  910) 	while (kcpu->reqs_available >= ctx->req_batch * 2) {
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  911) 		kcpu->reqs_available -= ctx->req_batch;
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  912) 		atomic_add(ctx->req_batch, &ctx->reqs_available);
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  913) 	}
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  914) 
263782c1c95bb (Benjamin LaHaise           2014-07-14 12:49:26 -0400  915) 	local_irq_restore(flags);
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  916) }
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  917) 
432c79978c33e (Christoph Hellwig          2018-11-19 15:57:42 -0700  918) static bool __get_reqs_available(struct kioctx *ctx)
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  919) {
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  920) 	struct kioctx_cpu *kcpu;
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  921) 	bool ret = false;
263782c1c95bb (Benjamin LaHaise           2014-07-14 12:49:26 -0400  922) 	unsigned long flags;
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  923) 
263782c1c95bb (Benjamin LaHaise           2014-07-14 12:49:26 -0400  924) 	local_irq_save(flags);
be6fb451a2458 (Benjamin LaHaise           2014-07-22 09:56:56 -0400  925) 	kcpu = this_cpu_ptr(ctx->cpu);
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  926) 	if (!kcpu->reqs_available) {
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  927) 		int old, avail = atomic_read(&ctx->reqs_available);
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  928) 
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  929) 		do {
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  930) 			if (avail < ctx->req_batch)
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  931) 				goto out;
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  932) 
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  933) 			old = avail;
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  934) 			avail = atomic_cmpxchg(&ctx->reqs_available,
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  935) 					       avail, avail - ctx->req_batch);
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  936) 		} while (avail != old);
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  937) 
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  938) 		kcpu->reqs_available += ctx->req_batch;
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  939) 	}
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  940) 
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  941) 	ret = true;
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  942) 	kcpu->reqs_available--;
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  943) out:
263782c1c95bb (Benjamin LaHaise           2014-07-14 12:49:26 -0400  944) 	local_irq_restore(flags);
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  945) 	return ret;
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  946) }
e1bdd5f27a5b1 (Kent Overstreet            2013-04-26 10:58:39 +1000  947) 
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  948) /* refill_reqs_available
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  949)  *	Updates the reqs_available reference counts used for tracking the
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  950)  *	number of free slots in the completion ring.  This can be called
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  951)  *	from aio_complete() (to optimistically update reqs_available) or
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  952)  *	from aio_get_req() (the we're out of events case).  It must be
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  953)  *	called holding ctx->completion_lock.
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  954)  */
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  955) static void refill_reqs_available(struct kioctx *ctx, unsigned head,
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  956)                                   unsigned tail)
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  957) {
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  958) 	unsigned events_in_ring, completed;
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  959) 
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  960) 	/* Clamp head since userland can write to it. */
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  961) 	head %= ctx->nr_events;
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  962) 	if (head <= tail)
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  963) 		events_in_ring = tail - head;
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  964) 	else
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  965) 		events_in_ring = ctx->nr_events - (head - tail);
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  966) 
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  967) 	completed = ctx->completed_events;
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  968) 	if (events_in_ring < completed)
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  969) 		completed -= events_in_ring;
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  970) 	else
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  971) 		completed = 0;
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  972) 
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  973) 	if (!completed)
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  974) 		return;
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  975) 
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  976) 	ctx->completed_events -= completed;
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  977) 	put_reqs_available(ctx, completed);
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  978) }
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  979) 
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  980) /* user_refill_reqs_available
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  981)  *	Called to refill reqs_available when aio_get_req() encounters an
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  982)  *	out of space in the completion ring.
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  983)  */
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  984) static void user_refill_reqs_available(struct kioctx *ctx)
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  985) {
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  986) 	spin_lock_irq(&ctx->completion_lock);
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  987) 	if (ctx->completed_events) {
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  988) 		struct aio_ring *ring;
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  989) 		unsigned head;
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  990) 
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  991) 		/* Access of ring->head may race with aio_read_events_ring()
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  992) 		 * here, but that's okay since whether we read the old version
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  993) 		 * or the new version, and either will be valid.  The important
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  994) 		 * part is that head cannot pass tail since we prevent
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  995) 		 * aio_complete() from updating tail by holding
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  996) 		 * ctx->completion_lock.  Even if head is invalid, the check
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  997) 		 * against ctx->completed_events below will make sure we do the
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  998) 		 * safe/right thing.
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400  999) 		 */
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400 1000) 		ring = kmap_atomic(ctx->ring_pages[0]);
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400 1001) 		head = ring->head;
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400 1002) 		kunmap_atomic(ring);
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400 1003) 
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400 1004) 		refill_reqs_available(ctx, head, ctx->tail);
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400 1005) 	}
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400 1006) 
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400 1007) 	spin_unlock_irq(&ctx->completion_lock);
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400 1008) }
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400 1009) 
432c79978c33e (Christoph Hellwig          2018-11-19 15:57:42 -0700 1010) static bool get_reqs_available(struct kioctx *ctx)
432c79978c33e (Christoph Hellwig          2018-11-19 15:57:42 -0700 1011) {
432c79978c33e (Christoph Hellwig          2018-11-19 15:57:42 -0700 1012) 	if (__get_reqs_available(ctx))
432c79978c33e (Christoph Hellwig          2018-11-19 15:57:42 -0700 1013) 		return true;
432c79978c33e (Christoph Hellwig          2018-11-19 15:57:42 -0700 1014) 	user_refill_reqs_available(ctx);
432c79978c33e (Christoph Hellwig          2018-11-19 15:57:42 -0700 1015) 	return __get_reqs_available(ctx);
432c79978c33e (Christoph Hellwig          2018-11-19 15:57:42 -0700 1016) }
432c79978c33e (Christoph Hellwig          2018-11-19 15:57:42 -0700 1017) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1018) /* aio_get_req
57282d8fd7440 (Kent Overstreet            2013-05-13 13:42:52 -0700 1019)  *	Allocate a slot for an aio request.
57282d8fd7440 (Kent Overstreet            2013-05-13 13:42:52 -0700 1020)  * Returns NULL if no requests are free.
b53119f13a048 (Linus Torvalds             2019-03-06 20:22:54 -0500 1021)  *
b53119f13a048 (Linus Torvalds             2019-03-06 20:22:54 -0500 1022)  * The refcount is initialized to 2 - one for the async op completion,
b53119f13a048 (Linus Torvalds             2019-03-06 20:22:54 -0500 1023)  * one for the synchronous code that does this.
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1024)  */
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100 1025) static inline struct aio_kiocb *aio_get_req(struct kioctx *ctx)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1026) {
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100 1027) 	struct aio_kiocb *req;
a1c8eae75ea3b (Kent Overstreet            2013-05-07 16:18:53 -0700 1028) 
2bc4ca9bb600c (Jens Axboe                 2018-12-04 09:44:49 -0700 1029) 	req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1030) 	if (unlikely(!req))
432c79978c33e (Christoph Hellwig          2018-11-19 15:57:42 -0700 1031) 		return NULL;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1032) 
fa0ca2aee3bec (Al Viro                    2019-03-06 18:21:08 -0500 1033) 	if (unlikely(!get_reqs_available(ctx))) {
6af1c849dfb1f (Wei Yongjun                2019-04-04 08:44:05 +0000 1034) 		kmem_cache_free(kiocb_cachep, req);
fa0ca2aee3bec (Al Viro                    2019-03-06 18:21:08 -0500 1035) 		return NULL;
fa0ca2aee3bec (Al Viro                    2019-03-06 18:21:08 -0500 1036) 	}
fa0ca2aee3bec (Al Viro                    2019-03-06 18:21:08 -0500 1037) 
e34ecee2ae791 (Kent Overstreet            2013-10-10 19:31:47 -0700 1038) 	percpu_ref_get(&ctx->reqs);
2bc4ca9bb600c (Jens Axboe                 2018-12-04 09:44:49 -0700 1039) 	req->ki_ctx = ctx;
75321b50a37a5 (Christoph Hellwig          2018-04-09 14:57:56 +0200 1040) 	INIT_LIST_HEAD(&req->ki_list);
b53119f13a048 (Linus Torvalds             2019-03-06 20:22:54 -0500 1041) 	refcount_set(&req->ki_refcnt, 2);
2bc4ca9bb600c (Jens Axboe                 2018-12-04 09:44:49 -0700 1042) 	req->ki_eventfd = NULL;
080d676de095a (Jeff Moyer                 2011-11-02 13:40:10 -0700 1043) 	return req;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1044) }
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1045) 
d5470b596abdd (Adrian Bunk                2008-04-29 00:58:57 -0700 1046) static struct kioctx *lookup_ioctx(unsigned long ctx_id)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1047) {
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400 1048) 	struct aio_ring __user *ring  = (void __user *)ctx_id;
abf137dd77121 (Jens Axboe                 2008-12-09 08:11:22 +0100 1049) 	struct mm_struct *mm = current->mm;
65c24491b4fef (Jeff Moyer                 2009-03-18 17:04:21 -0700 1050) 	struct kioctx *ctx, *ret = NULL;
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400 1051) 	struct kioctx_table *table;
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400 1052) 	unsigned id;
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400 1053) 
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400 1054) 	if (get_user(id, &ring->id))
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400 1055) 		return NULL;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1056) 
abf137dd77121 (Jens Axboe                 2008-12-09 08:11:22 +0100 1057) 	rcu_read_lock();
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400 1058) 	table = rcu_dereference(mm->ioctx_table);
abf137dd77121 (Jens Axboe                 2008-12-09 08:11:22 +0100 1059) 
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400 1060) 	if (!table || id >= table->nr)
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400 1061) 		goto out;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1062) 
a538e3ff9dabc (Jeff Moyer                 2018-12-11 12:37:49 -0500 1063) 	id = array_index_nospec(id, table->nr);
d0264c01e7587 (Tejun Heo                  2018-03-14 12:10:17 -0700 1064) 	ctx = rcu_dereference(table->table[id]);
f30d704fe1244 (Benjamin LaHaise           2013-08-07 18:23:48 -0400 1065) 	if (ctx && ctx->user_id == ctx_id) {
baf10564fbb66 (Al Viro                    2018-05-20 16:46:23 -0400 1066) 		if (percpu_ref_tryget_live(&ctx->users))
baf10564fbb66 (Al Viro                    2018-05-20 16:46:23 -0400 1067) 			ret = ctx;
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400 1068) 	}
db446a08c23d5 (Benjamin LaHaise           2013-07-30 12:54:40 -0400 1069) out:
abf137dd77121 (Jens Axboe                 2008-12-09 08:11:22 +0100 1070) 	rcu_read_unlock();
65c24491b4fef (Jeff Moyer                 2009-03-18 17:04:21 -0700 1071) 	return ret;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1072) }
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1073) 
b53119f13a048 (Linus Torvalds             2019-03-06 20:22:54 -0500 1074) static inline void iocb_destroy(struct aio_kiocb *iocb)
b53119f13a048 (Linus Torvalds             2019-03-06 20:22:54 -0500 1075) {
7425970347a21 (Al Viro                    2019-03-06 18:18:31 -0500 1076) 	if (iocb->ki_eventfd)
7425970347a21 (Al Viro                    2019-03-06 18:18:31 -0500 1077) 		eventfd_ctx_put(iocb->ki_eventfd);
b53119f13a048 (Linus Torvalds             2019-03-06 20:22:54 -0500 1078) 	if (iocb->ki_filp)
b53119f13a048 (Linus Torvalds             2019-03-06 20:22:54 -0500 1079) 		fput(iocb->ki_filp);
b53119f13a048 (Linus Torvalds             2019-03-06 20:22:54 -0500 1080) 	percpu_ref_put(&iocb->ki_ctx->reqs);
b53119f13a048 (Linus Torvalds             2019-03-06 20:22:54 -0500 1081) 	kmem_cache_free(kiocb_cachep, iocb);
b53119f13a048 (Linus Torvalds             2019-03-06 20:22:54 -0500 1082) }
b53119f13a048 (Linus Torvalds             2019-03-06 20:22:54 -0500 1083) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1084) /* aio_complete
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1085)  *	Called when the io request on the given iocb is complete.
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1086)  */
2bb874c0d873d (Al Viro                    2019-03-07 19:49:55 -0500 1087) static void aio_complete(struct aio_kiocb *iocb)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1088) {
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1089) 	struct kioctx	*ctx = iocb->ki_ctx;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1090) 	struct aio_ring	*ring;
21b40200cfe96 (Kent Overstreet            2013-05-07 16:18:47 -0700 1091) 	struct io_event	*ev_page, *event;
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400 1092) 	unsigned tail, pos, head;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1093) 	unsigned long	flags;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1094) 
0460fef2a9215 (Kent Overstreet            2013-05-07 16:18:49 -0700 1095) 	/*
0460fef2a9215 (Kent Overstreet            2013-05-07 16:18:49 -0700 1096) 	 * Add a completion event to the ring buffer. Must be done holding
4b30f07e74d2d (Tang Chen                  2013-07-03 15:09:16 -0700 1097) 	 * ctx->completion_lock to prevent other code from messing with the tail
0460fef2a9215 (Kent Overstreet            2013-05-07 16:18:49 -0700 1098) 	 * pointer since we might be called from irq context.
0460fef2a9215 (Kent Overstreet            2013-05-07 16:18:49 -0700 1099) 	 */
0460fef2a9215 (Kent Overstreet            2013-05-07 16:18:49 -0700 1100) 	spin_lock_irqsave(&ctx->completion_lock, flags);
0460fef2a9215 (Kent Overstreet            2013-05-07 16:18:49 -0700 1101) 
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700 1102) 	tail = ctx->tail;
21b40200cfe96 (Kent Overstreet            2013-05-07 16:18:47 -0700 1103) 	pos = tail + AIO_EVENTS_OFFSET;
21b40200cfe96 (Kent Overstreet            2013-05-07 16:18:47 -0700 1104) 
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700 1105) 	if (++tail >= ctx->nr_events)
4bf69b2a06090 (Kenneth W Chen             2005-05-01 08:59:15 -0700 1106) 		tail = 0;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1107) 
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700 1108) 	ev_page = kmap_atomic(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
21b40200cfe96 (Kent Overstreet            2013-05-07 16:18:47 -0700 1109) 	event = ev_page + pos % AIO_EVENTS_PER_PAGE;
21b40200cfe96 (Kent Overstreet            2013-05-07 16:18:47 -0700 1110) 
a9339b7855094 (Al Viro                    2019-03-07 19:43:45 -0500 1111) 	*event = iocb->ki_res;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1112) 
21b40200cfe96 (Kent Overstreet            2013-05-07 16:18:47 -0700 1113) 	kunmap_atomic(ev_page);
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700 1114) 	flush_dcache_page(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
21b40200cfe96 (Kent Overstreet            2013-05-07 16:18:47 -0700 1115) 
a9339b7855094 (Al Viro                    2019-03-07 19:43:45 -0500 1116) 	pr_debug("%p[%u]: %p: %p %Lx %Lx %Lx\n", ctx, tail, iocb,
a9339b7855094 (Al Viro                    2019-03-07 19:43:45 -0500 1117) 		 (void __user *)(unsigned long)iocb->ki_res.obj,
a9339b7855094 (Al Viro                    2019-03-07 19:43:45 -0500 1118) 		 iocb->ki_res.data, iocb->ki_res.res, iocb->ki_res.res2);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1119) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1120) 	/* after flagging the request as done, we
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1121) 	 * must never even look at it again
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1122) 	 */
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1123) 	smp_wmb();	/* make event visible before updating tail */
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1124) 
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700 1125) 	ctx->tail = tail;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1126) 
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700 1127) 	ring = kmap_atomic(ctx->ring_pages[0]);
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400 1128) 	head = ring->head;
21b40200cfe96 (Kent Overstreet            2013-05-07 16:18:47 -0700 1129) 	ring->tail = tail;
e8e3c3d66fd9d (Cong Wang                  2011-11-25 23:14:27 +0800 1130) 	kunmap_atomic(ring);
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700 1131) 	flush_dcache_page(ctx->ring_pages[0]);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1132) 
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400 1133) 	ctx->completed_events++;
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400 1134) 	if (ctx->completed_events > 1)
d856f32a86b2b (Benjamin LaHaise           2014-08-24 13:14:05 -0400 1135) 		refill_reqs_available(ctx, head, tail);
0460fef2a9215 (Kent Overstreet            2013-05-07 16:18:49 -0700 1136) 	spin_unlock_irqrestore(&ctx->completion_lock, flags);
0460fef2a9215 (Kent Overstreet            2013-05-07 16:18:49 -0700 1137) 
21b40200cfe96 (Kent Overstreet            2013-05-07 16:18:47 -0700 1138) 	pr_debug("added to ring %p at [%u]\n", iocb, tail);
8d1c98b0b5c01 (Davide Libenzi             2008-04-10 21:29:19 -0700 1139) 
8d1c98b0b5c01 (Davide Libenzi             2008-04-10 21:29:19 -0700 1140) 	/*
8d1c98b0b5c01 (Davide Libenzi             2008-04-10 21:29:19 -0700 1141) 	 * Check if the user asked us to deliver the result through an
8d1c98b0b5c01 (Davide Libenzi             2008-04-10 21:29:19 -0700 1142) 	 * eventfd. The eventfd_signal() function is safe to be called
8d1c98b0b5c01 (Davide Libenzi             2008-04-10 21:29:19 -0700 1143) 	 * from IRQ context.
8d1c98b0b5c01 (Davide Libenzi             2008-04-10 21:29:19 -0700 1144) 	 */
7425970347a21 (Al Viro                    2019-03-06 18:18:31 -0500 1145) 	if (iocb->ki_eventfd)
8d1c98b0b5c01 (Davide Libenzi             2008-04-10 21:29:19 -0700 1146) 		eventfd_signal(iocb->ki_eventfd, 1);
8d1c98b0b5c01 (Davide Libenzi             2008-04-10 21:29:19 -0700 1147) 
6cb2a21049b89 (Quentin Barnes             2008-03-19 17:00:39 -0700 1148) 	/*
6cb2a21049b89 (Quentin Barnes             2008-03-19 17:00:39 -0700 1149) 	 * We have to order our ring_info tail store above and test
6cb2a21049b89 (Quentin Barnes             2008-03-19 17:00:39 -0700 1150) 	 * of the wait list below outside the wait lock.  This is
6cb2a21049b89 (Quentin Barnes             2008-03-19 17:00:39 -0700 1151) 	 * like in wake_up_bit() where clearing a bit has to be
6cb2a21049b89 (Quentin Barnes             2008-03-19 17:00:39 -0700 1152) 	 * ordered with the unlocked test.
6cb2a21049b89 (Quentin Barnes             2008-03-19 17:00:39 -0700 1153) 	 */
6cb2a21049b89 (Quentin Barnes             2008-03-19 17:00:39 -0700 1154) 	smp_mb();
6cb2a21049b89 (Quentin Barnes             2008-03-19 17:00:39 -0700 1155) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1156) 	if (waitqueue_active(&ctx->wait))
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1157) 		wake_up(&ctx->wait);
2bb874c0d873d (Al Viro                    2019-03-07 19:49:55 -0500 1158) }
2bb874c0d873d (Al Viro                    2019-03-07 19:49:55 -0500 1159) 
2bb874c0d873d (Al Viro                    2019-03-07 19:49:55 -0500 1160) static inline void iocb_put(struct aio_kiocb *iocb)
2bb874c0d873d (Al Viro                    2019-03-07 19:49:55 -0500 1161) {
2bb874c0d873d (Al Viro                    2019-03-07 19:49:55 -0500 1162) 	if (refcount_dec_and_test(&iocb->ki_refcnt)) {
2bb874c0d873d (Al Viro                    2019-03-07 19:49:55 -0500 1163) 		aio_complete(iocb);
2bb874c0d873d (Al Viro                    2019-03-07 19:49:55 -0500 1164) 		iocb_destroy(iocb);
2bb874c0d873d (Al Viro                    2019-03-07 19:49:55 -0500 1165) 	}
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1166) }
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1167) 
2be4e7deec2d4 (Gu Zheng                   2014-07-23 18:03:53 +0800 1168) /* aio_read_events_ring
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1169)  *	Pull an event off of the ioctx's event ring.  Returns the number of
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1170)  *	events fetched
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1171)  */
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1172) static long aio_read_events_ring(struct kioctx *ctx,
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1173) 				 struct io_event __user *event, long nr)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1174) {
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1175) 	struct aio_ring *ring;
5ffac122dbda8 (Kent Overstreet            2013-05-09 15:36:07 -0700 1176) 	unsigned head, tail, pos;
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1177) 	long ret = 0;
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1178) 	int copy_ret;
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1179) 
9c9ce763b114e (Dave Chinner               2015-02-03 19:29:05 -0500 1180) 	/*
9c9ce763b114e (Dave Chinner               2015-02-03 19:29:05 -0500 1181) 	 * The mutex can block and wake us up and that will cause
9c9ce763b114e (Dave Chinner               2015-02-03 19:29:05 -0500 1182) 	 * wait_event_interruptible_hrtimeout() to schedule without sleeping
9c9ce763b114e (Dave Chinner               2015-02-03 19:29:05 -0500 1183) 	 * and repeat. This should be rare enough that it doesn't cause
9c9ce763b114e (Dave Chinner               2015-02-03 19:29:05 -0500 1184) 	 * peformance issues. See the comment in read_events() for more detail.
9c9ce763b114e (Dave Chinner               2015-02-03 19:29:05 -0500 1185) 	 */
9c9ce763b114e (Dave Chinner               2015-02-03 19:29:05 -0500 1186) 	sched_annotate_sleep();
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700 1187) 	mutex_lock(&ctx->ring_lock);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1188) 
fa8a53c39f3fd (Benjamin LaHaise           2014-03-28 10:14:45 -0400 1189) 	/* Access to ->ring_pages here is protected by ctx->ring_lock. */
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700 1190) 	ring = kmap_atomic(ctx->ring_pages[0]);
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1191) 	head = ring->head;
5ffac122dbda8 (Kent Overstreet            2013-05-09 15:36:07 -0700 1192) 	tail = ring->tail;
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1193) 	kunmap_atomic(ring);
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1194) 
2ff396be602f1 (Jeff Moyer                 2014-09-02 13:17:00 -0400 1195) 	/*
2ff396be602f1 (Jeff Moyer                 2014-09-02 13:17:00 -0400 1196) 	 * Ensure that once we've read the current tail pointer, that
2ff396be602f1 (Jeff Moyer                 2014-09-02 13:17:00 -0400 1197) 	 * we also see the events that were stored up to the tail.
2ff396be602f1 (Jeff Moyer                 2014-09-02 13:17:00 -0400 1198) 	 */
2ff396be602f1 (Jeff Moyer                 2014-09-02 13:17:00 -0400 1199) 	smp_rmb();
2ff396be602f1 (Jeff Moyer                 2014-09-02 13:17:00 -0400 1200) 
5ffac122dbda8 (Kent Overstreet            2013-05-09 15:36:07 -0700 1201) 	pr_debug("h%u t%u m%u\n", head, tail, ctx->nr_events);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1202) 
5ffac122dbda8 (Kent Overstreet            2013-05-09 15:36:07 -0700 1203) 	if (head == tail)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1204) 		goto out;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1205) 
edfbbf388f293 (Benjamin LaHaise           2014-06-24 13:32:51 -0400 1206) 	head %= ctx->nr_events;
edfbbf388f293 (Benjamin LaHaise           2014-06-24 13:32:51 -0400 1207) 	tail %= ctx->nr_events;
edfbbf388f293 (Benjamin LaHaise           2014-06-24 13:32:51 -0400 1208) 
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1209) 	while (ret < nr) {
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1210) 		long avail;
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1211) 		struct io_event *ev;
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1212) 		struct page *page;
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1213) 
5ffac122dbda8 (Kent Overstreet            2013-05-09 15:36:07 -0700 1214) 		avail = (head <= tail ?  tail : ctx->nr_events) - head;
5ffac122dbda8 (Kent Overstreet            2013-05-09 15:36:07 -0700 1215) 		if (head == tail)
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1216) 			break;
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1217) 
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1218) 		pos = head + AIO_EVENTS_OFFSET;
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700 1219) 		page = ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE];
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1220) 		pos %= AIO_EVENTS_PER_PAGE;
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1221) 
d2988bd412fac (Al Viro                    2018-05-26 19:13:10 -0400 1222) 		avail = min(avail, nr - ret);
d2988bd412fac (Al Viro                    2018-05-26 19:13:10 -0400 1223) 		avail = min_t(long, avail, AIO_EVENTS_PER_PAGE - pos);
d2988bd412fac (Al Viro                    2018-05-26 19:13:10 -0400 1224) 
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1225) 		ev = kmap(page);
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1226) 		copy_ret = copy_to_user(event + ret, ev + pos,
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1227) 					sizeof(*ev) * avail);
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1228) 		kunmap(page);
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1229) 
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1230) 		if (unlikely(copy_ret)) {
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1231) 			ret = -EFAULT;
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1232) 			goto out;
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1233) 		}
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1234) 
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1235) 		ret += avail;
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1236) 		head += avail;
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700 1237) 		head %= ctx->nr_events;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1238) 	}
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1239) 
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700 1240) 	ring = kmap_atomic(ctx->ring_pages[0]);
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1241) 	ring->head = head;
91d80a84bbc8f (Zhao Hongjiang             2013-04-26 11:03:53 +0800 1242) 	kunmap_atomic(ring);
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700 1243) 	flush_dcache_page(ctx->ring_pages[0]);
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1244) 
5ffac122dbda8 (Kent Overstreet            2013-05-09 15:36:07 -0700 1245) 	pr_debug("%li  h%u t%u\n", ret, head, tail);
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1246) out:
58c85dc20a2c5 (Kent Overstreet            2013-05-07 16:18:55 -0700 1247) 	mutex_unlock(&ctx->ring_lock);
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1248) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1249) 	return ret;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1250) }
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1251) 
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1252) static bool aio_read_events(struct kioctx *ctx, long min_nr, long nr,
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1253) 			    struct io_event __user *event, long *i)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1254) {
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1255) 	long ret = aio_read_events_ring(ctx, event + *i, nr - *i);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1256) 
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1257) 	if (ret > 0)
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1258) 		*i += ret;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1259) 
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1260) 	if (unlikely(atomic_read(&ctx->dead)))
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1261) 		ret = -EINVAL;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1262) 
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1263) 	if (!*i)
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1264) 		*i = ret;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1265) 
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1266) 	return ret < 0 || *i >= min_nr;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1267) }
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1268) 
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1269) static long read_events(struct kioctx *ctx, long min_nr, long nr,
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1270) 			struct io_event __user *event,
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 1271) 			ktime_t until)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1272) {
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1273) 	long ret = 0;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1274) 
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1275) 	/*
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1276) 	 * Note that aio_read_events() is being called as the conditional - i.e.
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1277) 	 * we're calling it after prepare_to_wait() has set task state to
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1278) 	 * TASK_INTERRUPTIBLE.
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1279) 	 *
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1280) 	 * But aio_read_events() can block, and if it blocks it's going to flip
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1281) 	 * the task state back to TASK_RUNNING.
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1282) 	 *
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1283) 	 * This should be ok, provided it doesn't flip the state back to
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1284) 	 * TASK_RUNNING and return 0 too much - that causes us to spin. That
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1285) 	 * will only happen if the mutex_lock() call blocks, and we then find
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1286) 	 * the ringbuffer empty. So in practice we should be ok, but it's
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1287) 	 * something to be aware of when touching this code.
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1288) 	 */
2456e85535441 (Thomas Gleixner            2016-12-25 11:38:40 +0100 1289) 	if (until == 0)
5f785de588735 (Fam Zheng                  2014-11-06 20:44:36 +0800 1290) 		aio_read_events(ctx, min_nr, nr, event, &ret);
5f785de588735 (Fam Zheng                  2014-11-06 20:44:36 +0800 1291) 	else
5f785de588735 (Fam Zheng                  2014-11-06 20:44:36 +0800 1292) 		wait_event_interruptible_hrtimeout(ctx->wait,
5f785de588735 (Fam Zheng                  2014-11-06 20:44:36 +0800 1293) 				aio_read_events(ctx, min_nr, nr, event, &ret),
5f785de588735 (Fam Zheng                  2014-11-06 20:44:36 +0800 1294) 				until);
a31ad380bed81 (Kent Overstreet            2013-05-07 16:18:45 -0700 1295) 	return ret;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1296) }
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1297) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1298) /* sys_io_setup:
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1299)  *	Create an aio_context capable of receiving at least nr_events.
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1300)  *	ctxp must not point to an aio_context that already exists, and
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1301)  *	must be initialized to 0 prior to the call.  On successful
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1302)  *	creation of the aio_context, *ctxp is filled in with the resulting 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1303)  *	handle.  May fail with -EINVAL if *ctxp is not initialized,
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1304)  *	if the specified nr_events exceeds internal limits.  May fail 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1305)  *	with -EAGAIN if the specified nr_events exceeds the user's limit 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1306)  *	of available events.  May fail with -ENOMEM if insufficient kernel
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1307)  *	resources are available.  May fail with -EFAULT if an invalid
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1308)  *	pointer is passed for ctxp.  Will fail with -ENOSYS if not
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1309)  *	implemented.
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1310)  */
002c8976ee537 (Heiko Carstens             2009-01-14 14:14:18 +0100 1311) SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1312) {
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1313) 	struct kioctx *ioctx = NULL;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1314) 	unsigned long ctx;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1315) 	long ret;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1316) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1317) 	ret = get_user(ctx, ctxp);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1318) 	if (unlikely(ret))
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1319) 		goto out;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1320) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1321) 	ret = -EINVAL;
d55b5fdaf4084 (Zach Brown                 2005-11-07 00:59:31 -0800 1322) 	if (unlikely(ctx || nr_events == 0)) {
acd88d4e1af3c (Kinglong Mee               2015-02-04 21:15:59 +0800 1323) 		pr_debug("EINVAL: ctx %lu nr_events %u\n",
d55b5fdaf4084 (Zach Brown                 2005-11-07 00:59:31 -0800 1324) 		         ctx, nr_events);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1325) 		goto out;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1326) 	}
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1327) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1328) 	ioctx = ioctx_alloc(nr_events);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1329) 	ret = PTR_ERR(ioctx);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1330) 	if (!IS_ERR(ioctx)) {
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1331) 		ret = put_user(ioctx->user_id, ctxp);
a2e1859adb3b0 (Al Viro                    2012-03-20 16:27:57 -0400 1332) 		if (ret)
e02ba72aabfad (Anatol Pomozov             2014-04-15 11:31:33 -0700 1333) 			kill_ioctx(current->mm, ioctx, NULL);
723be6e39d142 (Kent Overstreet            2013-05-28 15:14:48 -0700 1334) 		percpu_ref_put(&ioctx->users);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1335) 	}
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1336) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1337) out:
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1338) 	return ret;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1339) }
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1340) 
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1341) #ifdef CONFIG_COMPAT
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1342) COMPAT_SYSCALL_DEFINE2(io_setup, unsigned, nr_events, u32 __user *, ctx32p)
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1343) {
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1344) 	struct kioctx *ioctx = NULL;
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1345) 	unsigned long ctx;
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1346) 	long ret;
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1347) 
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1348) 	ret = get_user(ctx, ctx32p);
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1349) 	if (unlikely(ret))
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1350) 		goto out;
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1351) 
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1352) 	ret = -EINVAL;
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1353) 	if (unlikely(ctx || nr_events == 0)) {
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1354) 		pr_debug("EINVAL: ctx %lu nr_events %u\n",
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1355) 		         ctx, nr_events);
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1356) 		goto out;
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1357) 	}
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1358) 
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1359) 	ioctx = ioctx_alloc(nr_events);
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1360) 	ret = PTR_ERR(ioctx);
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1361) 	if (!IS_ERR(ioctx)) {
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1362) 		/* truncating is ok because it's a user address */
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1363) 		ret = put_user((u32)ioctx->user_id, ctx32p);
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1364) 		if (ret)
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1365) 			kill_ioctx(current->mm, ioctx, NULL);
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1366) 		percpu_ref_put(&ioctx->users);
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1367) 	}
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1368) 
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1369) out:
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1370) 	return ret;
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1371) }
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1372) #endif
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1373) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1374) /* sys_io_destroy:
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1375)  *	Destroy the aio_context specified.  May cancel any outstanding 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1376)  *	AIOs and block on completion.  Will fail with -ENOSYS if not
642b5123ac5ec (Satoru Takeuchi            2010-08-05 11:23:11 -0700 1377)  *	implemented.  May fail with -EINVAL if the context pointed to
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1378)  *	is invalid.
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1379)  */
002c8976ee537 (Heiko Carstens             2009-01-14 14:14:18 +0100 1380) SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1381) {
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1382) 	struct kioctx *ioctx = lookup_ioctx(ctx);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1383) 	if (likely(NULL != ioctx)) {
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600 1384) 		struct ctx_rq_wait wait;
fb2d44838320b (Benjamin LaHaise           2014-04-29 12:45:17 -0400 1385) 		int ret;
e02ba72aabfad (Anatol Pomozov             2014-04-15 11:31:33 -0700 1386) 
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600 1387) 		init_completion(&wait.comp);
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600 1388) 		atomic_set(&wait.count, 1);
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600 1389) 
e02ba72aabfad (Anatol Pomozov             2014-04-15 11:31:33 -0700 1390) 		/* Pass requests_done to kill_ioctx() where it can be set
e02ba72aabfad (Anatol Pomozov             2014-04-15 11:31:33 -0700 1391) 		 * in a thread-safe way. If we try to set it here then we have
e02ba72aabfad (Anatol Pomozov             2014-04-15 11:31:33 -0700 1392) 		 * a race condition if two io_destroy() called simultaneously.
e02ba72aabfad (Anatol Pomozov             2014-04-15 11:31:33 -0700 1393) 		 */
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600 1394) 		ret = kill_ioctx(current->mm, ioctx, &wait);
723be6e39d142 (Kent Overstreet            2013-05-28 15:14:48 -0700 1395) 		percpu_ref_put(&ioctx->users);
e02ba72aabfad (Anatol Pomozov             2014-04-15 11:31:33 -0700 1396) 
e02ba72aabfad (Anatol Pomozov             2014-04-15 11:31:33 -0700 1397) 		/* Wait until all IO for the context are done. Otherwise kernel
e02ba72aabfad (Anatol Pomozov             2014-04-15 11:31:33 -0700 1398) 		 * keep using user-space buffers even if user thinks the context
e02ba72aabfad (Anatol Pomozov             2014-04-15 11:31:33 -0700 1399) 		 * is destroyed.
e02ba72aabfad (Anatol Pomozov             2014-04-15 11:31:33 -0700 1400) 		 */
fb2d44838320b (Benjamin LaHaise           2014-04-29 12:45:17 -0400 1401) 		if (!ret)
dc48e56d76161 (Jens Axboe                 2015-04-15 11:17:23 -0600 1402) 			wait_for_completion(&wait.comp);
e02ba72aabfad (Anatol Pomozov             2014-04-15 11:31:33 -0700 1403) 
fb2d44838320b (Benjamin LaHaise           2014-04-29 12:45:17 -0400 1404) 		return ret;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1405) 	}
acd88d4e1af3c (Kinglong Mee               2015-02-04 21:15:59 +0800 1406) 	pr_debug("EINVAL: invalid context id\n");
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1407) 	return -EINVAL;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1408) }
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1409) 
3c96c7f4caeb0 (Al Viro                    2018-05-28 13:37:43 -0400 1410) static void aio_remove_iocb(struct aio_kiocb *iocb)
3c96c7f4caeb0 (Al Viro                    2018-05-28 13:37:43 -0400 1411) {
3c96c7f4caeb0 (Al Viro                    2018-05-28 13:37:43 -0400 1412) 	struct kioctx *ctx = iocb->ki_ctx;
3c96c7f4caeb0 (Al Viro                    2018-05-28 13:37:43 -0400 1413) 	unsigned long flags;
3c96c7f4caeb0 (Al Viro                    2018-05-28 13:37:43 -0400 1414) 
3c96c7f4caeb0 (Al Viro                    2018-05-28 13:37:43 -0400 1415) 	spin_lock_irqsave(&ctx->ctx_lock, flags);
3c96c7f4caeb0 (Al Viro                    2018-05-28 13:37:43 -0400 1416) 	list_del(&iocb->ki_list);
3c96c7f4caeb0 (Al Viro                    2018-05-28 13:37:43 -0400 1417) 	spin_unlock_irqrestore(&ctx->ctx_lock, flags);
3c96c7f4caeb0 (Al Viro                    2018-05-28 13:37:43 -0400 1418) }
3c96c7f4caeb0 (Al Viro                    2018-05-28 13:37:43 -0400 1419) 
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1420) static void aio_complete_rw(struct kiocb *kiocb, long res, long res2)
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1421) {
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1422) 	struct aio_kiocb *iocb = container_of(kiocb, struct aio_kiocb, rw);
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1423) 
3c96c7f4caeb0 (Al Viro                    2018-05-28 13:37:43 -0400 1424) 	if (!list_empty_careful(&iocb->ki_list))
3c96c7f4caeb0 (Al Viro                    2018-05-28 13:37:43 -0400 1425) 		aio_remove_iocb(iocb);
3c96c7f4caeb0 (Al Viro                    2018-05-28 13:37:43 -0400 1426) 
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1427) 	if (kiocb->ki_flags & IOCB_WRITE) {
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1428) 		struct inode *inode = file_inode(kiocb->ki_filp);
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1429) 
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1430) 		/*
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1431) 		 * Tell lockdep we inherited freeze protection from submission
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1432) 		 * thread.
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1433) 		 */
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1434) 		if (S_ISREG(inode->i_mode))
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1435) 			__sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1436) 		file_end_write(kiocb->ki_filp);
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1437) 	}
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1438) 
2bb874c0d873d (Al Viro                    2019-03-07 19:49:55 -0500 1439) 	iocb->ki_res.res = res;
2bb874c0d873d (Al Viro                    2019-03-07 19:49:55 -0500 1440) 	iocb->ki_res.res2 = res2;
2bb874c0d873d (Al Viro                    2019-03-07 19:49:55 -0500 1441) 	iocb_put(iocb);
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1442) }
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1443) 
88a6f18b950e2 (Jens Axboe                 2018-11-24 14:46:14 -0700 1444) static int aio_prep_rw(struct kiocb *req, const struct iocb *iocb)
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1445) {
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1446) 	int ret;
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1447) 
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1448) 	req->ki_complete = aio_complete_rw;
ec51f8ee1e634 (Mike Marshall              2019-02-05 14:13:35 -0500 1449) 	req->private = NULL;
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1450) 	req->ki_pos = iocb->aio_offset;
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1451) 	req->ki_flags = iocb_flags(req->ki_filp);
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1452) 	if (iocb->aio_flags & IOCB_FLAG_RESFD)
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1453) 		req->ki_flags |= IOCB_EVENTFD;
fc28724d67c90 (Adam Manzanares            2018-05-22 10:52:18 -0700 1454) 	req->ki_hint = ki_hint_validate(file_write_hint(req->ki_filp));
d9a08a9e616be (Adam Manzanares            2018-05-22 10:52:19 -0700 1455) 	if (iocb->aio_flags & IOCB_FLAG_IOPRIO) {
d9a08a9e616be (Adam Manzanares            2018-05-22 10:52:19 -0700 1456) 		/*
d9a08a9e616be (Adam Manzanares            2018-05-22 10:52:19 -0700 1457) 		 * If the IOCB_FLAG_IOPRIO flag of aio_flags is set, then
d9a08a9e616be (Adam Manzanares            2018-05-22 10:52:19 -0700 1458) 		 * aio_reqprio is interpreted as an I/O scheduling
d9a08a9e616be (Adam Manzanares            2018-05-22 10:52:19 -0700 1459) 		 * class and priority.
d9a08a9e616be (Adam Manzanares            2018-05-22 10:52:19 -0700 1460) 		 */
d9a08a9e616be (Adam Manzanares            2018-05-22 10:52:19 -0700 1461) 		ret = ioprio_check_cap(iocb->aio_reqprio);
d9a08a9e616be (Adam Manzanares            2018-05-22 10:52:19 -0700 1462) 		if (ret) {
9a6d9a62e0fd4 (Adam Manzanares            2018-06-04 10:59:57 -0700 1463) 			pr_debug("aio ioprio check cap error: %d\n", ret);
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800 1464) 			return ret;
d9a08a9e616be (Adam Manzanares            2018-05-22 10:52:19 -0700 1465) 		}
d9a08a9e616be (Adam Manzanares            2018-05-22 10:52:19 -0700 1466) 
d9a08a9e616be (Adam Manzanares            2018-05-22 10:52:19 -0700 1467) 		req->ki_ioprio = iocb->aio_reqprio;
d9a08a9e616be (Adam Manzanares            2018-05-22 10:52:19 -0700 1468) 	} else
76dc891395dc6 (Damien Le Moal             2018-11-20 10:52:36 +0900 1469) 		req->ki_ioprio = get_current_ioprio();
d9a08a9e616be (Adam Manzanares            2018-05-22 10:52:19 -0700 1470) 
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1471) 	ret = kiocb_set_rw_flags(req, iocb->aio_rw_flags);
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1472) 	if (unlikely(ret))
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800 1473) 		return ret;
154989e45fd8d (Christoph Hellwig          2018-11-22 16:44:07 +0100 1474) 
154989e45fd8d (Christoph Hellwig          2018-11-22 16:44:07 +0100 1475) 	req->ki_flags &= ~IOCB_HIPRI; /* no one is going to poll for this I/O */
154989e45fd8d (Christoph Hellwig          2018-11-22 16:44:07 +0100 1476) 	return 0;
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1477) }
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1478) 
87e5e6dab6c2a (Jens Axboe                 2019-05-14 16:02:22 -0600 1479) static ssize_t aio_setup_rw(int rw, const struct iocb *iocb,
87e5e6dab6c2a (Jens Axboe                 2019-05-14 16:02:22 -0600 1480) 		struct iovec **iovec, bool vectored, bool compat,
87e5e6dab6c2a (Jens Axboe                 2019-05-14 16:02:22 -0600 1481) 		struct iov_iter *iter)
eed4e51fb60c3 (Badari Pulavarty           2006-09-30 23:28:49 -0700 1482) {
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1483) 	void __user *buf = (void __user *)(uintptr_t)iocb->aio_buf;
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1484) 	size_t len = iocb->aio_nbytes;
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1485) 
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1486) 	if (!vectored) {
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1487) 		ssize_t ret = import_single_range(rw, buf, len, *iovec, iter);
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1488) 		*iovec = NULL;
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1489) 		return ret;
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1490) 	}
89cd35c58bc2e (Christoph Hellwig          2020-09-25 06:51:41 +0200 1491) 
89cd35c58bc2e (Christoph Hellwig          2020-09-25 06:51:41 +0200 1492) 	return __import_iovec(rw, buf, len, UIO_FASTIOV, iovec, iter, compat);
eed4e51fb60c3 (Badari Pulavarty           2006-09-30 23:28:49 -0700 1493) }
eed4e51fb60c3 (Badari Pulavarty           2006-09-30 23:28:49 -0700 1494) 
9061d14a8a970 (Al Viro                    2018-05-26 19:11:40 -0400 1495) static inline void aio_rw_done(struct kiocb *req, ssize_t ret)
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1496) {
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1497) 	switch (ret) {
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1498) 	case -EIOCBQUEUED:
9061d14a8a970 (Al Viro                    2018-05-26 19:11:40 -0400 1499) 		break;
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1500) 	case -ERESTARTSYS:
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1501) 	case -ERESTARTNOINTR:
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1502) 	case -ERESTARTNOHAND:
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1503) 	case -ERESTART_RESTARTBLOCK:
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1504) 		/*
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1505) 		 * There's no easy way to restart the syscall since other AIO's
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1506) 		 * may be already running. Just fail this IO with EINTR.
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1507) 		 */
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1508) 		ret = -EINTR;
df561f6688fef (Gustavo A. R. Silva        2020-08-23 17:36:59 -0500 1509) 		fallthrough;
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1510) 	default:
bc9bff61624ac (Jens Axboe                 2018-11-06 14:27:13 -0700 1511) 		req->ki_complete(req, ret, 0);
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1512) 	}
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1513) }
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1514) 
958c13ce141cd (Al Viro                    2019-03-06 18:13:00 -0500 1515) static int aio_read(struct kiocb *req, const struct iocb *iocb,
88a6f18b950e2 (Jens Axboe                 2018-11-24 14:46:14 -0700 1516) 			bool vectored, bool compat)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1517) {
00fefb9cf2b54 (Gu Zheng                   2014-07-23 18:03:54 +0800 1518) 	struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
293bc9822fa9b (Al Viro                    2014-02-11 18:37:41 -0500 1519) 	struct iov_iter iter;
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1520) 	struct file *file;
958c13ce141cd (Al Viro                    2019-03-06 18:13:00 -0500 1521) 	int ret;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1522) 
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1523) 	ret = aio_prep_rw(req, iocb);
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1524) 	if (ret)
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1525) 		return ret;
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1526) 	file = req->ki_filp;
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1527) 	if (unlikely(!(file->f_mode & FMODE_READ)))
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800 1528) 		return -EBADF;
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1529) 	ret = -EINVAL;
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1530) 	if (unlikely(!file->f_op->read_iter))
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800 1531) 		return -EINVAL;
73a7075e3f6ec (Kent Overstreet            2013-05-09 15:03:42 -0700 1532) 
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1533) 	ret = aio_setup_rw(READ, iocb, &iovec, vectored, compat, &iter);
87e5e6dab6c2a (Jens Axboe                 2019-05-14 16:02:22 -0600 1534) 	if (ret < 0)
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800 1535) 		return ret;
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1536) 	ret = rw_verify_area(READ, file, &req->ki_pos, iov_iter_count(&iter));
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1537) 	if (!ret)
9061d14a8a970 (Al Viro                    2018-05-26 19:11:40 -0400 1538) 		aio_rw_done(req, call_read_iter(file, req, &iter));
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1539) 	kfree(iovec);
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1540) 	return ret;
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1541) }
73a7075e3f6ec (Kent Overstreet            2013-05-09 15:03:42 -0700 1542) 
958c13ce141cd (Al Viro                    2019-03-06 18:13:00 -0500 1543) static int aio_write(struct kiocb *req, const struct iocb *iocb,
88a6f18b950e2 (Jens Axboe                 2018-11-24 14:46:14 -0700 1544) 			 bool vectored, bool compat)
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1545) {
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1546) 	struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1547) 	struct iov_iter iter;
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1548) 	struct file *file;
958c13ce141cd (Al Viro                    2019-03-06 18:13:00 -0500 1549) 	int ret;
41ef4eb8eef8d (Kent Overstreet            2013-05-07 16:19:11 -0700 1550) 
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1551) 	ret = aio_prep_rw(req, iocb);
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1552) 	if (ret)
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1553) 		return ret;
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1554) 	file = req->ki_filp;
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1555) 
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1556) 	if (unlikely(!(file->f_mode & FMODE_WRITE)))
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800 1557) 		return -EBADF;
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1558) 	if (unlikely(!file->f_op->write_iter))
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800 1559) 		return -EINVAL;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1560) 
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1561) 	ret = aio_setup_rw(WRITE, iocb, &iovec, vectored, compat, &iter);
87e5e6dab6c2a (Jens Axboe                 2019-05-14 16:02:22 -0600 1562) 	if (ret < 0)
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800 1563) 		return ret;
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1564) 	ret = rw_verify_area(WRITE, file, &req->ki_pos, iov_iter_count(&iter));
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1565) 	if (!ret) {
70fe2f48152e6 (Jan Kara                   2016-10-30 11:42:04 -0500 1566) 		/*
92ce4728563ad (Christoph Hellwig          2018-04-06 09:28:17 +0200 1567) 		 * Open-code file_start_write here to grab freeze protection,
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1568) 		 * which will be released by another thread in
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1569) 		 * aio_complete_rw().  Fool lockdep by telling it the lock got
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1570) 		 * released so that it doesn't complain about the held lock when
54843f875f7a9 (Christoph Hellwig          2018-05-02 19:57:21 +0200 1571) 		 * we return to userspace.
70fe2f48152e6 (Jan Kara                   2016-10-30 11:42:04 -0500 1572) 		 */
92ce4728563ad (Christoph Hellwig          2018-04-06 09:28:17 +0200 1573) 		if (S_ISREG(file_inode(file)->i_mode)) {
8a3c84b649b03 (Darrick J. Wong            2020-11-10 16:50:21 -0800 1574) 			sb_start_write(file_inode(file)->i_sb);
a12f1ae61c489 (Shaohua Li                 2016-12-13 12:09:56 -0800 1575) 			__sb_writers_release(file_inode(file)->i_sb, SB_FREEZE_WRITE);
92ce4728563ad (Christoph Hellwig          2018-04-06 09:28:17 +0200 1576) 		}
92ce4728563ad (Christoph Hellwig          2018-04-06 09:28:17 +0200 1577) 		req->ki_flags |= IOCB_WRITE;
9061d14a8a970 (Al Viro                    2018-05-26 19:11:40 -0400 1578) 		aio_rw_done(req, call_write_iter(file, req, &iter));
41ef4eb8eef8d (Kent Overstreet            2013-05-07 16:19:11 -0700 1579) 	}
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1580) 	kfree(iovec);
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1581) 	return ret;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1582) }
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1583) 
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200 1584) static void aio_fsync_work(struct work_struct *work)
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200 1585) {
2bb874c0d873d (Al Viro                    2019-03-07 19:49:55 -0500 1586) 	struct aio_kiocb *iocb = container_of(work, struct aio_kiocb, fsync.work);
530f32fc370fd (Miklos Szeredi             2020-05-14 16:44:24 +0200 1587) 	const struct cred *old_cred = override_creds(iocb->fsync.creds);
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200 1588) 
2bb874c0d873d (Al Viro                    2019-03-07 19:49:55 -0500 1589) 	iocb->ki_res.res = vfs_fsync(iocb->fsync.file, iocb->fsync.datasync);
530f32fc370fd (Miklos Szeredi             2020-05-14 16:44:24 +0200 1590) 	revert_creds(old_cred);
530f32fc370fd (Miklos Szeredi             2020-05-14 16:44:24 +0200 1591) 	put_cred(iocb->fsync.creds);
2bb874c0d873d (Al Viro                    2019-03-07 19:49:55 -0500 1592) 	iocb_put(iocb);
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200 1593) }
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200 1594) 
88a6f18b950e2 (Jens Axboe                 2018-11-24 14:46:14 -0700 1595) static int aio_fsync(struct fsync_iocb *req, const struct iocb *iocb,
88a6f18b950e2 (Jens Axboe                 2018-11-24 14:46:14 -0700 1596) 		     bool datasync)
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200 1597) {
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200 1598) 	if (unlikely(iocb->aio_buf || iocb->aio_offset || iocb->aio_nbytes ||
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200 1599) 			iocb->aio_rw_flags))
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200 1600) 		return -EINVAL;
a11e1d432b51f (Linus Torvalds             2018-06-28 09:43:44 -0700 1601) 
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800 1602) 	if (unlikely(!req->file->f_op->fsync))
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200 1603) 		return -EINVAL;
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200 1604) 
530f32fc370fd (Miklos Szeredi             2020-05-14 16:44:24 +0200 1605) 	req->creds = prepare_creds();
530f32fc370fd (Miklos Szeredi             2020-05-14 16:44:24 +0200 1606) 	if (!req->creds)
530f32fc370fd (Miklos Szeredi             2020-05-14 16:44:24 +0200 1607) 		return -ENOMEM;
530f32fc370fd (Miklos Szeredi             2020-05-14 16:44:24 +0200 1608) 
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200 1609) 	req->datasync = datasync;
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200 1610) 	INIT_WORK(&req->work, aio_fsync_work);
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200 1611) 	schedule_work(&req->work);
9061d14a8a970 (Al Viro                    2018-05-26 19:11:40 -0400 1612) 	return 0;
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200 1613) }
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200 1614) 
01d7a356872ee (Jens Axboe                 2020-02-03 10:33:42 -0700 1615) static void aio_poll_put_work(struct work_struct *work)
01d7a356872ee (Jens Axboe                 2020-02-03 10:33:42 -0700 1616) {
01d7a356872ee (Jens Axboe                 2020-02-03 10:33:42 -0700 1617) 	struct poll_iocb *req = container_of(work, struct poll_iocb, work);
01d7a356872ee (Jens Axboe                 2020-02-03 10:33:42 -0700 1618) 	struct aio_kiocb *iocb = container_of(req, struct aio_kiocb, poll);
01d7a356872ee (Jens Axboe                 2020-02-03 10:33:42 -0700 1619) 
01d7a356872ee (Jens Axboe                 2020-02-03 10:33:42 -0700 1620) 	iocb_put(iocb);
01d7a356872ee (Jens Axboe                 2020-02-03 10:33:42 -0700 1621) }
01d7a356872ee (Jens Axboe                 2020-02-03 10:33:42 -0700 1622) 
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1623) static void aio_poll_complete_work(struct work_struct *work)
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1624) {
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1625) 	struct poll_iocb *req = container_of(work, struct poll_iocb, work);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1626) 	struct aio_kiocb *iocb = container_of(req, struct aio_kiocb, poll);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1627) 	struct poll_table_struct pt = { ._key = req->events };
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1628) 	struct kioctx *ctx = iocb->ki_ctx;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1629) 	__poll_t mask = 0;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1630) 
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1631) 	if (!READ_ONCE(req->cancelled))
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1632) 		mask = vfs_poll(req->file, &pt) & req->events;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1633) 
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1634) 	/*
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1635) 	 * Note that ->ki_cancel callers also delete iocb from active_reqs after
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1636) 	 * calling ->ki_cancel.  We need the ctx_lock roundtrip here to
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1637) 	 * synchronize with them.  In the cancellation case the list_del_init
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1638) 	 * itself is not actually needed, but harmless so we keep it in to
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1639) 	 * avoid further branches in the fast path.
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1640) 	 */
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1641) 	spin_lock_irq(&ctx->ctx_lock);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1642) 	if (!mask && !READ_ONCE(req->cancelled)) {
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1643) 		add_wait_queue(req->head, &req->wait);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1644) 		spin_unlock_irq(&ctx->ctx_lock);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1645) 		return;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1646) 	}
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1647) 	list_del_init(&iocb->ki_list);
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1648) 	iocb->ki_res.res = mangle_poll(mask);
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1649) 	req->done = true;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1650) 	spin_unlock_irq(&ctx->ctx_lock);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1651) 
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1652) 	iocb_put(iocb);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1653) }
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1654) 
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1655) /* assumes we are called with irqs disabled */
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1656) static int aio_poll_cancel(struct kiocb *iocb)
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1657) {
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1658) 	struct aio_kiocb *aiocb = container_of(iocb, struct aio_kiocb, rw);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1659) 	struct poll_iocb *req = &aiocb->poll;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1660) 
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1661) 	spin_lock(&req->head->lock);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1662) 	WRITE_ONCE(req->cancelled, true);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1663) 	if (!list_empty(&req->wait.entry)) {
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1664) 		list_del_init(&req->wait.entry);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1665) 		schedule_work(&aiocb->poll.work);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1666) 	}
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1667) 	spin_unlock(&req->head->lock);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1668) 
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1669) 	return 0;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1670) }
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1671) 
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1672) static int aio_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1673) 		void *key)
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1674) {
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1675) 	struct poll_iocb *req = container_of(wait, struct poll_iocb, wait);
e8693bcfa0b4a (Christoph Hellwig          2018-07-16 12:25:17 +0200 1676) 	struct aio_kiocb *iocb = container_of(req, struct aio_kiocb, poll);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1677) 	__poll_t mask = key_to_poll(key);
d3d6a18d7d351 (Bart Van Assche            2019-02-08 16:59:49 -0800 1678) 	unsigned long flags;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1679) 
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1680) 	/* for instances that support it check for an event match first: */
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1681) 	if (mask && !(mask & req->events))
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1682) 		return 0;
e8693bcfa0b4a (Christoph Hellwig          2018-07-16 12:25:17 +0200 1683) 
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1684) 	list_del_init(&req->wait.entry);
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1685) 
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1686) 	if (mask && spin_trylock_irqsave(&iocb->ki_ctx->ctx_lock, flags)) {
01d7a356872ee (Jens Axboe                 2020-02-03 10:33:42 -0700 1687) 		struct kioctx *ctx = iocb->ki_ctx;
01d7a356872ee (Jens Axboe                 2020-02-03 10:33:42 -0700 1688) 
d3d6a18d7d351 (Bart Van Assche            2019-02-08 16:59:49 -0800 1689) 		/*
d3d6a18d7d351 (Bart Van Assche            2019-02-08 16:59:49 -0800 1690) 		 * Try to complete the iocb inline if we can. Use
d3d6a18d7d351 (Bart Van Assche            2019-02-08 16:59:49 -0800 1691) 		 * irqsave/irqrestore because not all filesystems (e.g. fuse)
d3d6a18d7d351 (Bart Van Assche            2019-02-08 16:59:49 -0800 1692) 		 * call this function with IRQs disabled and because IRQs
d3d6a18d7d351 (Bart Van Assche            2019-02-08 16:59:49 -0800 1693) 		 * have to be disabled before ctx_lock is obtained.
d3d6a18d7d351 (Bart Van Assche            2019-02-08 16:59:49 -0800 1694) 		 */
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1695) 		list_del(&iocb->ki_list);
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1696) 		iocb->ki_res.res = mangle_poll(mask);
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1697) 		req->done = true;
01d7a356872ee (Jens Axboe                 2020-02-03 10:33:42 -0700 1698) 		if (iocb->ki_eventfd && eventfd_signal_count()) {
01d7a356872ee (Jens Axboe                 2020-02-03 10:33:42 -0700 1699) 			iocb = NULL;
01d7a356872ee (Jens Axboe                 2020-02-03 10:33:42 -0700 1700) 			INIT_WORK(&req->work, aio_poll_put_work);
01d7a356872ee (Jens Axboe                 2020-02-03 10:33:42 -0700 1701) 			schedule_work(&req->work);
01d7a356872ee (Jens Axboe                 2020-02-03 10:33:42 -0700 1702) 		}
01d7a356872ee (Jens Axboe                 2020-02-03 10:33:42 -0700 1703) 		spin_unlock_irqrestore(&ctx->ctx_lock, flags);
01d7a356872ee (Jens Axboe                 2020-02-03 10:33:42 -0700 1704) 		if (iocb)
01d7a356872ee (Jens Axboe                 2020-02-03 10:33:42 -0700 1705) 			iocb_put(iocb);
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1706) 	} else {
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1707) 		schedule_work(&req->work);
e8693bcfa0b4a (Christoph Hellwig          2018-07-16 12:25:17 +0200 1708) 	}
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1709) 	return 1;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1710) }
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1711) 
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1712) struct aio_poll_table {
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1713) 	struct poll_table_struct	pt;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1714) 	struct aio_kiocb		*iocb;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1715) 	int				error;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1716) };
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1717) 
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1718) static void
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1719) aio_poll_queue_proc(struct file *file, struct wait_queue_head *head,
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1720) 		struct poll_table_struct *p)
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1721) {
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1722) 	struct aio_poll_table *pt = container_of(p, struct aio_poll_table, pt);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1723) 
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1724) 	/* multiple wait queues per file are not supported */
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1725) 	if (unlikely(pt->iocb->poll.head)) {
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1726) 		pt->error = -EINVAL;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1727) 		return;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1728) 	}
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1729) 
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1730) 	pt->error = 0;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1731) 	pt->iocb->poll.head = head;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1732) 	add_wait_queue(head, &pt->iocb->poll.wait);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1733) }
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1734) 
958c13ce141cd (Al Viro                    2019-03-06 18:13:00 -0500 1735) static int aio_poll(struct aio_kiocb *aiocb, const struct iocb *iocb)
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1736) {
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1737) 	struct kioctx *ctx = aiocb->ki_ctx;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1738) 	struct poll_iocb *req = &aiocb->poll;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1739) 	struct aio_poll_table apt;
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1740) 	bool cancel = false;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1741) 	__poll_t mask;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1742) 
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1743) 	/* reject any unknown events outside the normal event mask. */
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1744) 	if ((u16)iocb->aio_buf != iocb->aio_buf)
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1745) 		return -EINVAL;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1746) 	/* reject fields that are not defined for poll */
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1747) 	if (iocb->aio_offset || iocb->aio_nbytes || iocb->aio_rw_flags)
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1748) 		return -EINVAL;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1749) 
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1750) 	INIT_WORK(&req->work, aio_poll_complete_work);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1751) 	req->events = demangle_poll(iocb->aio_buf) | EPOLLERR | EPOLLHUP;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1752) 
2bc4ca9bb600c (Jens Axboe                 2018-12-04 09:44:49 -0700 1753) 	req->head = NULL;
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1754) 	req->done = false;
2bc4ca9bb600c (Jens Axboe                 2018-12-04 09:44:49 -0700 1755) 	req->cancelled = false;
2bc4ca9bb600c (Jens Axboe                 2018-12-04 09:44:49 -0700 1756) 
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1757) 	apt.pt._qproc = aio_poll_queue_proc;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1758) 	apt.pt._key = req->events;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1759) 	apt.iocb = aiocb;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1760) 	apt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1761) 
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1762) 	/* initialized the list so that we can do list_empty checks */
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1763) 	INIT_LIST_HEAD(&req->wait.entry);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1764) 	init_waitqueue_func_entry(&req->wait, aio_poll_wake);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1765) 
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1766) 	mask = vfs_poll(req->file, &apt.pt) & req->events;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1767) 	spin_lock_irq(&ctx->ctx_lock);
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1768) 	if (likely(req->head)) {
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1769) 		spin_lock(&req->head->lock);
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1770) 		if (unlikely(list_empty(&req->wait.entry))) {
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1771) 			if (apt.error)
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1772) 				cancel = true;
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1773) 			apt.error = 0;
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1774) 			mask = 0;
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1775) 		}
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1776) 		if (mask || apt.error) {
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1777) 			list_del_init(&req->wait.entry);
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1778) 		} else if (cancel) {
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1779) 			WRITE_ONCE(req->cancelled, true);
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1780) 		} else if (!req->done) { /* actually waiting for an event */
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1781) 			list_add_tail(&aiocb->ki_list, &ctx->active_reqs);
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1782) 			aiocb->ki_cancel = aio_poll_cancel;
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1783) 		}
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1784) 		spin_unlock(&req->head->lock);
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1785) 	}
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1786) 	if (mask) { /* no async, we'd stolen it */
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1787) 		aiocb->ki_res.res = mangle_poll(mask);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1788) 		apt.error = 0;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1789) 	}
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1790) 	spin_unlock_irq(&ctx->ctx_lock);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1791) 	if (mask)
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1792) 		iocb_put(aiocb);
af5c72b1fc7a0 (Al Viro                    2019-03-07 21:45:41 -0500 1793) 	return apt.error;
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1794) }
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1795) 
88a6f18b950e2 (Jens Axboe                 2018-11-24 14:46:14 -0700 1796) static int __io_submit_one(struct kioctx *ctx, const struct iocb *iocb,
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1797) 			   struct iocb __user *user_iocb, struct aio_kiocb *req,
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1798) 			   bool compat)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1799) {
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800 1800) 	req->ki_filp = fget(iocb->aio_fildes);
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800 1801) 	if (unlikely(!req->ki_filp))
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1802) 		return -EBADF;
84c4e1f89fefe (Linus Torvalds             2019-03-03 14:23:33 -0800 1803) 
88a6f18b950e2 (Jens Axboe                 2018-11-24 14:46:14 -0700 1804) 	if (iocb->aio_flags & IOCB_FLAG_RESFD) {
7425970347a21 (Al Viro                    2019-03-06 18:18:31 -0500 1805) 		struct eventfd_ctx *eventfd;
9c3060bedd841 (Davide Libenzi             2007-05-10 22:23:21 -0700 1806) 		/*
9c3060bedd841 (Davide Libenzi             2007-05-10 22:23:21 -0700 1807) 		 * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
9c3060bedd841 (Davide Libenzi             2007-05-10 22:23:21 -0700 1808) 		 * instance of the file* now. The file descriptor must be
9c3060bedd841 (Davide Libenzi             2007-05-10 22:23:21 -0700 1809) 		 * an eventfd() fd, and will be signaled for each completed
9c3060bedd841 (Davide Libenzi             2007-05-10 22:23:21 -0700 1810) 		 * event using the eventfd_signal() function.
9c3060bedd841 (Davide Libenzi             2007-05-10 22:23:21 -0700 1811) 		 */
7425970347a21 (Al Viro                    2019-03-06 18:18:31 -0500 1812) 		eventfd = eventfd_ctx_fdget(iocb->aio_resfd);
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1813) 		if (IS_ERR(eventfd))
18bfb9c6a8a5f (Dan Carpenter              2019-04-03 09:22:35 +0300 1814) 			return PTR_ERR(eventfd);
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1815) 
7425970347a21 (Al Viro                    2019-03-06 18:18:31 -0500 1816) 		req->ki_eventfd = eventfd;
9830f4be159b2 (Goldwyn Rodrigues          2017-06-20 07:05:42 -0500 1817) 	}
9830f4be159b2 (Goldwyn Rodrigues          2017-06-20 07:05:42 -0500 1818) 
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1819) 	if (unlikely(put_user(KIOCB_KEY, &user_iocb->aio_key))) {
caf4167aa73be (Kent Overstreet            2013-05-07 16:18:35 -0700 1820) 		pr_debug("EFAULT: aio_key\n");
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1821) 		return -EFAULT;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1822) 	}
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1823) 
a9339b7855094 (Al Viro                    2019-03-07 19:43:45 -0500 1824) 	req->ki_res.obj = (u64)(unsigned long)user_iocb;
a9339b7855094 (Al Viro                    2019-03-07 19:43:45 -0500 1825) 	req->ki_res.data = iocb->aio_data;
a9339b7855094 (Al Viro                    2019-03-07 19:43:45 -0500 1826) 	req->ki_res.res = 0;
a9339b7855094 (Al Viro                    2019-03-07 19:43:45 -0500 1827) 	req->ki_res.res2 = 0;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1828) 
88a6f18b950e2 (Jens Axboe                 2018-11-24 14:46:14 -0700 1829) 	switch (iocb->aio_lio_opcode) {
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1830) 	case IOCB_CMD_PREAD:
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1831) 		return aio_read(&req->rw, iocb, false, compat);
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1832) 	case IOCB_CMD_PWRITE:
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1833) 		return aio_write(&req->rw, iocb, false, compat);
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1834) 	case IOCB_CMD_PREADV:
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1835) 		return aio_read(&req->rw, iocb, true, compat);
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1836) 	case IOCB_CMD_PWRITEV:
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1837) 		return aio_write(&req->rw, iocb, true, compat);
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200 1838) 	case IOCB_CMD_FSYNC:
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1839) 		return aio_fsync(&req->fsync, iocb, false);
a3c0d439e4d92 (Christoph Hellwig          2018-03-27 19:18:57 +0200 1840) 	case IOCB_CMD_FDSYNC:
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1841) 		return aio_fsync(&req->fsync, iocb, true);
bfe4037e722ec (Christoph Hellwig          2018-07-16 09:08:20 +0200 1842) 	case IOCB_CMD_POLL:
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1843) 		return aio_poll(req, iocb);
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1844) 	default:
88a6f18b950e2 (Jens Axboe                 2018-11-24 14:46:14 -0700 1845) 		pr_debug("invalid aio operation %d\n", iocb->aio_lio_opcode);
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1846) 		return -EINVAL;
89319d31d2d09 (Christoph Hellwig          2016-10-30 11:42:03 -0500 1847) 	}
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1848) }
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1849) 
88a6f18b950e2 (Jens Axboe                 2018-11-24 14:46:14 -0700 1850) static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
88a6f18b950e2 (Jens Axboe                 2018-11-24 14:46:14 -0700 1851) 			 bool compat)
88a6f18b950e2 (Jens Axboe                 2018-11-24 14:46:14 -0700 1852) {
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1853) 	struct aio_kiocb *req;
88a6f18b950e2 (Jens Axboe                 2018-11-24 14:46:14 -0700 1854) 	struct iocb iocb;
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1855) 	int err;
88a6f18b950e2 (Jens Axboe                 2018-11-24 14:46:14 -0700 1856) 
88a6f18b950e2 (Jens Axboe                 2018-11-24 14:46:14 -0700 1857) 	if (unlikely(copy_from_user(&iocb, user_iocb, sizeof(iocb))))
88a6f18b950e2 (Jens Axboe                 2018-11-24 14:46:14 -0700 1858) 		return -EFAULT;
88a6f18b950e2 (Jens Axboe                 2018-11-24 14:46:14 -0700 1859) 
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1860) 	/* enforce forwards compatibility on users */
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1861) 	if (unlikely(iocb.aio_reserved2)) {
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1862) 		pr_debug("EINVAL: reserve field set\n");
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1863) 		return -EINVAL;
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1864) 	}
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1865) 
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1866) 	/* prevent overflows */
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1867) 	if (unlikely(
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1868) 	    (iocb.aio_buf != (unsigned long)iocb.aio_buf) ||
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1869) 	    (iocb.aio_nbytes != (size_t)iocb.aio_nbytes) ||
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1870) 	    ((ssize_t)iocb.aio_nbytes < 0)
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1871) 	   )) {
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1872) 		pr_debug("EINVAL: overflow check\n");
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1873) 		return -EINVAL;
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1874) 	}
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1875) 
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1876) 	req = aio_get_req(ctx);
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1877) 	if (unlikely(!req))
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1878) 		return -EAGAIN;
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1879) 
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1880) 	err = __io_submit_one(ctx, &iocb, user_iocb, req, compat);
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1881) 
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1882) 	/* Done with the synchronous reference */
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1883) 	iocb_put(req);
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1884) 
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1885) 	/*
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1886) 	 * If err is 0, we'd either done aio_complete() ourselves or have
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1887) 	 * arranged for that to be done asynchronously.  Anything non-zero
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1888) 	 * means that we need to destroy req ourselves.
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1889) 	 */
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1890) 	if (unlikely(err)) {
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1891) 		iocb_destroy(req);
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1892) 		put_reqs_available(ctx, 1);
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1893) 	}
7316b49c2a117 (Al Viro                    2019-03-06 18:24:51 -0500 1894) 	return err;
88a6f18b950e2 (Jens Axboe                 2018-11-24 14:46:14 -0700 1895) }
88a6f18b950e2 (Jens Axboe                 2018-11-24 14:46:14 -0700 1896) 
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1897) /* sys_io_submit:
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1898)  *	Queue the nr iocbs pointed to by iocbpp for processing.  Returns
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1899)  *	the number of iocbs queued.  May return -EINVAL if the aio_context
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1900)  *	specified by ctx_id is invalid, if nr is < 0, if the iocb at
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1901)  *	*iocbpp[0] is not properly initialized, if the operation specified
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1902)  *	is invalid for the file descriptor in the iocb.  May fail with
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1903)  *	-EFAULT if any of the data structures point to invalid data.  May
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1904)  *	fail with -EBADF if the file descriptor specified in the first
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1905)  *	iocb is invalid.  May fail with -EAGAIN if insufficient resources
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1906)  *	are available to queue any iocbs.  Will return 0 if nr is 0.  Will
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1907)  *	fail with -ENOSYS if not implemented.
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1908)  */
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1909) SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1910) 		struct iocb __user * __user *, iocbpp)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1911) {
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1912) 	struct kioctx *ctx;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1913) 	long ret = 0;
080d676de095a (Jeff Moyer                 2011-11-02 13:40:10 -0700 1914) 	int i = 0;
9f5b9425468c8 (Shaohua Li                 2010-07-01 07:55:01 +0200 1915) 	struct blk_plug plug;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1916) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1917) 	if (unlikely(nr < 0))
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1918) 		return -EINVAL;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1919) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1920) 	ctx = lookup_ioctx(ctx_id);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1921) 	if (unlikely(!ctx)) {
caf4167aa73be (Kent Overstreet            2013-05-07 16:18:35 -0700 1922) 		pr_debug("EINVAL: invalid context id\n");
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1923) 		return -EINVAL;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1924) 	}
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1925) 
1da92779e2e8f (Al Viro                    2018-05-26 20:10:07 -0400 1926) 	if (nr > ctx->nr_events)
1da92779e2e8f (Al Viro                    2018-05-26 20:10:07 -0400 1927) 		nr = ctx->nr_events;
1da92779e2e8f (Al Viro                    2018-05-26 20:10:07 -0400 1928) 
a79d40e9b0f30 (Jens Axboe                 2018-12-04 09:45:32 -0700 1929) 	if (nr > AIO_PLUG_THRESHOLD)
a79d40e9b0f30 (Jens Axboe                 2018-12-04 09:45:32 -0700 1930) 		blk_start_plug(&plug);
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1931) 	for (i = 0; i < nr; i++) {
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1932) 		struct iocb __user *user_iocb;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1933) 
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1934) 		if (unlikely(get_user(user_iocb, iocbpp + i))) {
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1935) 			ret = -EFAULT;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1936) 			break;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1937) 		}
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1938) 
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1939) 		ret = io_submit_one(ctx, user_iocb, false);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1940) 		if (ret)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1941) 			break;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1942) 	}
a79d40e9b0f30 (Jens Axboe                 2018-12-04 09:45:32 -0700 1943) 	if (nr > AIO_PLUG_THRESHOLD)
a79d40e9b0f30 (Jens Axboe                 2018-12-04 09:45:32 -0700 1944) 		blk_finish_plug(&plug);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1945) 
723be6e39d142 (Kent Overstreet            2013-05-28 15:14:48 -0700 1946) 	percpu_ref_put(&ctx->users);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1947) 	return i ? i : ret;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1948) }
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1949) 
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1950) #ifdef CONFIG_COMPAT
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1951) COMPAT_SYSCALL_DEFINE3(io_submit, compat_aio_context_t, ctx_id,
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1952) 		       int, nr, compat_uptr_t __user *, iocbpp)
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1953) {
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1954) 	struct kioctx *ctx;
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1955) 	long ret = 0;
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1956) 	int i = 0;
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1957) 	struct blk_plug plug;
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1958) 
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1959) 	if (unlikely(nr < 0))
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1960) 		return -EINVAL;
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1961) 
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1962) 	ctx = lookup_ioctx(ctx_id);
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1963) 	if (unlikely(!ctx)) {
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1964) 		pr_debug("EINVAL: invalid context id\n");
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1965) 		return -EINVAL;
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1966) 	}
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1967) 
1da92779e2e8f (Al Viro                    2018-05-26 20:10:07 -0400 1968) 	if (nr > ctx->nr_events)
1da92779e2e8f (Al Viro                    2018-05-26 20:10:07 -0400 1969) 		nr = ctx->nr_events;
1da92779e2e8f (Al Viro                    2018-05-26 20:10:07 -0400 1970) 
a79d40e9b0f30 (Jens Axboe                 2018-12-04 09:45:32 -0700 1971) 	if (nr > AIO_PLUG_THRESHOLD)
a79d40e9b0f30 (Jens Axboe                 2018-12-04 09:45:32 -0700 1972) 		blk_start_plug(&plug);
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1973) 	for (i = 0; i < nr; i++) {
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1974) 		compat_uptr_t user_iocb;
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1975) 
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1976) 		if (unlikely(get_user(user_iocb, iocbpp + i))) {
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1977) 			ret = -EFAULT;
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1978) 			break;
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1979) 		}
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1980) 
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1981) 		ret = io_submit_one(ctx, compat_ptr(user_iocb), true);
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1982) 		if (ret)
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1983) 			break;
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1984) 	}
a79d40e9b0f30 (Jens Axboe                 2018-12-04 09:45:32 -0700 1985) 	if (nr > AIO_PLUG_THRESHOLD)
a79d40e9b0f30 (Jens Axboe                 2018-12-04 09:45:32 -0700 1986) 		blk_finish_plug(&plug);
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1987) 
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1988) 	percpu_ref_put(&ctx->users);
67ba049f94ebe (Al Viro                    2018-05-26 20:10:07 -0400 1989) 	return i ? i : ret;
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1990) }
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1991) #endif
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 1992) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1993) /* sys_io_cancel:
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1994)  *	Attempts to cancel an iocb previously passed to io_submit.  If
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1995)  *	the operation is successfully cancelled, the resulting event is
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1996)  *	copied into the memory pointed to by result without being placed
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1997)  *	into the completion queue and 0 is returned.  May fail with
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1998)  *	-EFAULT if any of the data structures pointed to are invalid.
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 1999)  *	May fail with -EINVAL if aio_context specified by ctx_id is
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2000)  *	invalid.  May fail with -EAGAIN if the iocb specified was not
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2001)  *	cancelled.  Will fail with -ENOSYS if not implemented.
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2002)  */
002c8976ee537 (Heiko Carstens             2009-01-14 14:14:18 +0100 2003) SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
002c8976ee537 (Heiko Carstens             2009-01-14 14:14:18 +0100 2004) 		struct io_event __user *, result)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2005) {
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2006) 	struct kioctx *ctx;
04b2fa9f8f36e (Christoph Hellwig          2015-02-02 14:49:06 +0100 2007) 	struct aio_kiocb *kiocb;
888933f8fdf06 (Christoph Hellwig          2018-05-23 14:11:02 +0200 2008) 	int ret = -EINVAL;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2009) 	u32 key;
a9339b7855094 (Al Viro                    2019-03-07 19:43:45 -0500 2010) 	u64 obj = (u64)(unsigned long)iocb;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2011) 
f3a2752a43de1 (Christoph Hellwig          2018-03-30 11:19:25 +0200 2012) 	if (unlikely(get_user(key, &iocb->aio_key)))
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2013) 		return -EFAULT;
f3a2752a43de1 (Christoph Hellwig          2018-03-30 11:19:25 +0200 2014) 	if (unlikely(key != KIOCB_KEY))
f3a2752a43de1 (Christoph Hellwig          2018-03-30 11:19:25 +0200 2015) 		return -EINVAL;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2016) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2017) 	ctx = lookup_ioctx(ctx_id);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2018) 	if (unlikely(!ctx))
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2019) 		return -EINVAL;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2020) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2021) 	spin_lock_irq(&ctx->ctx_lock);
833f4154ed560 (Al Viro                    2019-03-11 19:00:36 -0400 2022) 	/* TODO: use a hash or array, this sucks. */
833f4154ed560 (Al Viro                    2019-03-11 19:00:36 -0400 2023) 	list_for_each_entry(kiocb, &ctx->active_reqs, ki_list) {
a9339b7855094 (Al Viro                    2019-03-07 19:43:45 -0500 2024) 		if (kiocb->ki_res.obj == obj) {
833f4154ed560 (Al Viro                    2019-03-11 19:00:36 -0400 2025) 			ret = kiocb->ki_cancel(&kiocb->rw);
833f4154ed560 (Al Viro                    2019-03-11 19:00:36 -0400 2026) 			list_del_init(&kiocb->ki_list);
833f4154ed560 (Al Viro                    2019-03-11 19:00:36 -0400 2027) 			break;
833f4154ed560 (Al Viro                    2019-03-11 19:00:36 -0400 2028) 		}
888933f8fdf06 (Christoph Hellwig          2018-05-23 14:11:02 +0200 2029) 	}
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2030) 	spin_unlock_irq(&ctx->ctx_lock);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2031) 
906b973cf0ae8 (Kent Overstreet            2013-05-07 16:18:31 -0700 2032) 	if (!ret) {
bec68faaf3ba7 (Kent Overstreet            2013-05-13 14:45:08 -0700 2033) 		/*
bec68faaf3ba7 (Kent Overstreet            2013-05-13 14:45:08 -0700 2034) 		 * The result argument is no longer used - the io_event is
bec68faaf3ba7 (Kent Overstreet            2013-05-13 14:45:08 -0700 2035) 		 * always delivered via the ring buffer. -EINPROGRESS indicates
bec68faaf3ba7 (Kent Overstreet            2013-05-13 14:45:08 -0700 2036) 		 * cancellation is progress:
906b973cf0ae8 (Kent Overstreet            2013-05-07 16:18:31 -0700 2037) 		 */
bec68faaf3ba7 (Kent Overstreet            2013-05-13 14:45:08 -0700 2038) 		ret = -EINPROGRESS;
906b973cf0ae8 (Kent Overstreet            2013-05-07 16:18:31 -0700 2039) 	}
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2040) 
723be6e39d142 (Kent Overstreet            2013-05-28 15:14:48 -0700 2041) 	percpu_ref_put(&ctx->users);
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2042) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2043) 	return ret;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2044) }
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2045) 
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2046) static long do_io_getevents(aio_context_t ctx_id,
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2047) 		long min_nr,
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2048) 		long nr,
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2049) 		struct io_event __user *events,
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2050) 		struct timespec64 *ts)
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2051) {
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2052) 	ktime_t until = ts ? timespec64_to_ktime(*ts) : KTIME_MAX;
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2053) 	struct kioctx *ioctx = lookup_ioctx(ctx_id);
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2054) 	long ret = -EINVAL;
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2055) 
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2056) 	if (likely(ioctx)) {
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2057) 		if (likely(min_nr <= nr && min_nr >= 0))
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2058) 			ret = read_events(ioctx, min_nr, nr, events, until);
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2059) 		percpu_ref_put(&ioctx->users);
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2060) 	}
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2061) 
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2062) 	return ret;
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2063) }
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2064) 
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2065) /* io_getevents:
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2066)  *	Attempts to read at least min_nr events and up to nr events from
642b5123ac5ec (Satoru Takeuchi            2010-08-05 11:23:11 -0700 2067)  *	the completion queue for the aio_context specified by ctx_id. If
642b5123ac5ec (Satoru Takeuchi            2010-08-05 11:23:11 -0700 2068)  *	it succeeds, the number of read events is returned. May fail with
642b5123ac5ec (Satoru Takeuchi            2010-08-05 11:23:11 -0700 2069)  *	-EINVAL if ctx_id is invalid, if min_nr is out of range, if nr is
642b5123ac5ec (Satoru Takeuchi            2010-08-05 11:23:11 -0700 2070)  *	out of range, if timeout is out of range.  May fail with -EFAULT
642b5123ac5ec (Satoru Takeuchi            2010-08-05 11:23:11 -0700 2071)  *	if any of the memory specified is invalid.  May return 0 or
642b5123ac5ec (Satoru Takeuchi            2010-08-05 11:23:11 -0700 2072)  *	< min_nr if the timeout specified by timeout has elapsed
642b5123ac5ec (Satoru Takeuchi            2010-08-05 11:23:11 -0700 2073)  *	before sufficient events are available, where timeout == NULL
642b5123ac5ec (Satoru Takeuchi            2010-08-05 11:23:11 -0700 2074)  *	specifies an infinite timeout. Note that the timeout pointed to by
6900807c6b95d (Jeff Moyer                 2013-05-24 15:55:24 -0700 2075)  *	timeout is relative.  Will fail with -ENOSYS if not implemented.
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2076)  */
3ca47e958a64b (Arnd Bergmann              2019-04-23 17:43:50 +0200 2077) #ifdef CONFIG_64BIT
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2078) 
002c8976ee537 (Heiko Carstens             2009-01-14 14:14:18 +0100 2079) SYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id,
002c8976ee537 (Heiko Carstens             2009-01-14 14:14:18 +0100 2080) 		long, min_nr,
002c8976ee537 (Heiko Carstens             2009-01-14 14:14:18 +0100 2081) 		long, nr,
002c8976ee537 (Heiko Carstens             2009-01-14 14:14:18 +0100 2082) 		struct io_event __user *, events,
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2083) 		struct __kernel_timespec __user *, timeout)
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2084) {
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2085) 	struct timespec64	ts;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2086) 	int			ret;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2087) 
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2088) 	if (timeout && unlikely(get_timespec64(&ts, timeout)))
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2089) 		return -EFAULT;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2090) 
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2091) 	ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &ts : NULL);
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2092) 	if (!ret && signal_pending(current))
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2093) 		ret = -EINTR;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2094) 	return ret;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2095) }
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2096) 
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2097) #endif
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2098) 
9ba546c01976a (Christoph Hellwig          2018-07-11 15:48:46 +0200 2099) struct __aio_sigset {
9ba546c01976a (Christoph Hellwig          2018-07-11 15:48:46 +0200 2100) 	const sigset_t __user	*sigmask;
9ba546c01976a (Christoph Hellwig          2018-07-11 15:48:46 +0200 2101) 	size_t		sigsetsize;
9ba546c01976a (Christoph Hellwig          2018-07-11 15:48:46 +0200 2102) };
9ba546c01976a (Christoph Hellwig          2018-07-11 15:48:46 +0200 2103) 
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2104) SYSCALL_DEFINE6(io_pgetevents,
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2105) 		aio_context_t, ctx_id,
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2106) 		long, min_nr,
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2107) 		long, nr,
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2108) 		struct io_event __user *, events,
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2109) 		struct __kernel_timespec __user *, timeout,
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2110) 		const struct __aio_sigset __user *, usig)
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2111) {
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2112) 	struct __aio_sigset	ksig = { NULL, };
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2113) 	struct timespec64	ts;
97abc889ee296 (Oleg Nesterov              2019-06-28 12:06:50 -0700 2114) 	bool interrupted;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2115) 	int ret;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2116) 
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2117) 	if (timeout && unlikely(get_timespec64(&ts, timeout)))
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2118) 		return -EFAULT;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2119) 
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2120) 	if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2121) 		return -EFAULT;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2122) 
b772434be0891 (Oleg Nesterov              2019-07-16 16:29:53 -0700 2123) 	ret = set_user_sigmask(ksig.sigmask, ksig.sigsetsize);
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2124) 	if (ret)
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2125) 		return ret;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2126) 
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2127) 	ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &ts : NULL);
97abc889ee296 (Oleg Nesterov              2019-06-28 12:06:50 -0700 2128) 
97abc889ee296 (Oleg Nesterov              2019-06-28 12:06:50 -0700 2129) 	interrupted = signal_pending(current);
b772434be0891 (Oleg Nesterov              2019-07-16 16:29:53 -0700 2130) 	restore_saved_sigmask_unless(interrupted);
97abc889ee296 (Oleg Nesterov              2019-06-28 12:06:50 -0700 2131) 	if (interrupted && !ret)
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2132) 		ret = -ERESTARTNOHAND;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2133) 
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2134) 	return ret;
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2135) }
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2136) 
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2137) #if defined(CONFIG_COMPAT_32BIT_TIME) && !defined(CONFIG_64BIT)
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2138) 
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2139) SYSCALL_DEFINE6(io_pgetevents_time32,
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2140) 		aio_context_t, ctx_id,
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2141) 		long, min_nr,
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2142) 		long, nr,
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2143) 		struct io_event __user *, events,
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2144) 		struct old_timespec32 __user *, timeout,
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2145) 		const struct __aio_sigset __user *, usig)
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2146) {
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2147) 	struct __aio_sigset	ksig = { NULL, };
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2148) 	struct timespec64	ts;
97abc889ee296 (Oleg Nesterov              2019-06-28 12:06:50 -0700 2149) 	bool interrupted;
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2150) 	int ret;
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2151) 
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2152) 	if (timeout && unlikely(get_old_timespec32(&ts, timeout)))
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2153) 		return -EFAULT;
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2154) 
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2155) 	if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2156) 		return -EFAULT;
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2157) 
ded653ccbec03 (Deepa Dinamani             2018-09-19 21:41:04 -0700 2158) 
b772434be0891 (Oleg Nesterov              2019-07-16 16:29:53 -0700 2159) 	ret = set_user_sigmask(ksig.sigmask, ksig.sigsetsize);
ded653ccbec03 (Deepa Dinamani             2018-09-19 21:41:04 -0700 2160) 	if (ret)
ded653ccbec03 (Deepa Dinamani             2018-09-19 21:41:04 -0700 2161) 		return ret;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2162) 
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2163) 	ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &ts : NULL);
97abc889ee296 (Oleg Nesterov              2019-06-28 12:06:50 -0700 2164) 
97abc889ee296 (Oleg Nesterov              2019-06-28 12:06:50 -0700 2165) 	interrupted = signal_pending(current);
b772434be0891 (Oleg Nesterov              2019-07-16 16:29:53 -0700 2166) 	restore_saved_sigmask_unless(interrupted);
97abc889ee296 (Oleg Nesterov              2019-06-28 12:06:50 -0700 2167) 	if (interrupted && !ret)
854a6ed56839a (Deepa Dinamani             2018-09-19 21:41:05 -0700 2168) 		ret = -ERESTARTNOHAND;
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2169) 
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2170) 	return ret;
^1da177e4c3f4 (Linus Torvalds             2005-04-16 15:20:36 -0700 2171) }
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 2172) 
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2173) #endif
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2174) 
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2175) #if defined(CONFIG_COMPAT_32BIT_TIME)
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2176) 
8dabe7245bbc1 (Arnd Bergmann              2019-01-07 00:33:08 +0100 2177) SYSCALL_DEFINE5(io_getevents_time32, __u32, ctx_id,
8dabe7245bbc1 (Arnd Bergmann              2019-01-07 00:33:08 +0100 2178) 		__s32, min_nr,
8dabe7245bbc1 (Arnd Bergmann              2019-01-07 00:33:08 +0100 2179) 		__s32, nr,
8dabe7245bbc1 (Arnd Bergmann              2019-01-07 00:33:08 +0100 2180) 		struct io_event __user *, events,
8dabe7245bbc1 (Arnd Bergmann              2019-01-07 00:33:08 +0100 2181) 		struct old_timespec32 __user *, timeout)
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 2182) {
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2183) 	struct timespec64 t;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2184) 	int ret;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2185) 
9afc5eee65ca7 (Arnd Bergmann              2018-07-13 12:52:28 +0200 2186) 	if (timeout && get_old_timespec32(&t, timeout))
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2187) 		return -EFAULT;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2188) 
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2189) 	ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &t : NULL);
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2190) 	if (!ret && signal_pending(current))
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2191) 		ret = -EINTR;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2192) 	return ret;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2193) }
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2194) 
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2195) #endif
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2196) 
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2197) #ifdef CONFIG_COMPAT
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 2198) 
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2199) struct __compat_aio_sigset {
97eba80fcca75 (Guillem Jover              2019-08-21 05:38:20 +0200 2200) 	compat_uptr_t		sigmask;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2201) 	compat_size_t		sigsetsize;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2202) };
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2203) 
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2204) #if defined(CONFIG_COMPAT_32BIT_TIME)
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2205) 
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2206) COMPAT_SYSCALL_DEFINE6(io_pgetevents,
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2207) 		compat_aio_context_t, ctx_id,
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2208) 		compat_long_t, min_nr,
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2209) 		compat_long_t, nr,
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2210) 		struct io_event __user *, events,
9afc5eee65ca7 (Arnd Bergmann              2018-07-13 12:52:28 +0200 2211) 		struct old_timespec32 __user *, timeout,
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2212) 		const struct __compat_aio_sigset __user *, usig)
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2213) {
97eba80fcca75 (Guillem Jover              2019-08-21 05:38:20 +0200 2214) 	struct __compat_aio_sigset ksig = { 0, };
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2215) 	struct timespec64 t;
97abc889ee296 (Oleg Nesterov              2019-06-28 12:06:50 -0700 2216) 	bool interrupted;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2217) 	int ret;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2218) 
9afc5eee65ca7 (Arnd Bergmann              2018-07-13 12:52:28 +0200 2219) 	if (timeout && get_old_timespec32(&t, timeout))
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2220) 		return -EFAULT;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2221) 
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2222) 	if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2223) 		return -EFAULT;
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2224) 
97eba80fcca75 (Guillem Jover              2019-08-21 05:38:20 +0200 2225) 	ret = set_compat_user_sigmask(compat_ptr(ksig.sigmask), ksig.sigsetsize);
ded653ccbec03 (Deepa Dinamani             2018-09-19 21:41:04 -0700 2226) 	if (ret)
ded653ccbec03 (Deepa Dinamani             2018-09-19 21:41:04 -0700 2227) 		return ret;
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 2228) 
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2229) 	ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &t : NULL);
97abc889ee296 (Oleg Nesterov              2019-06-28 12:06:50 -0700 2230) 
97abc889ee296 (Oleg Nesterov              2019-06-28 12:06:50 -0700 2231) 	interrupted = signal_pending(current);
b772434be0891 (Oleg Nesterov              2019-07-16 16:29:53 -0700 2232) 	restore_saved_sigmask_unless(interrupted);
97abc889ee296 (Oleg Nesterov              2019-06-28 12:06:50 -0700 2233) 	if (interrupted && !ret)
854a6ed56839a (Deepa Dinamani             2018-09-19 21:41:05 -0700 2234) 		ret = -ERESTARTNOHAND;
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2235) 
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2236) 	return ret;
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 2237) }
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2238) 
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2239) #endif
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2240) 
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2241) COMPAT_SYSCALL_DEFINE6(io_pgetevents_time64,
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2242) 		compat_aio_context_t, ctx_id,
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2243) 		compat_long_t, min_nr,
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2244) 		compat_long_t, nr,
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2245) 		struct io_event __user *, events,
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2246) 		struct __kernel_timespec __user *, timeout,
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2247) 		const struct __compat_aio_sigset __user *, usig)
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2248) {
97eba80fcca75 (Guillem Jover              2019-08-21 05:38:20 +0200 2249) 	struct __compat_aio_sigset ksig = { 0, };
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2250) 	struct timespec64 t;
97abc889ee296 (Oleg Nesterov              2019-06-28 12:06:50 -0700 2251) 	bool interrupted;
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2252) 	int ret;
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2253) 
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2254) 	if (timeout && get_timespec64(&t, timeout))
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2255) 		return -EFAULT;
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2256) 
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2257) 	if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2258) 		return -EFAULT;
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2259) 
97eba80fcca75 (Guillem Jover              2019-08-21 05:38:20 +0200 2260) 	ret = set_compat_user_sigmask(compat_ptr(ksig.sigmask), ksig.sigsetsize);
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2261) 	if (ret)
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2262) 		return ret;
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2263) 
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2264) 	ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &t : NULL);
97abc889ee296 (Oleg Nesterov              2019-06-28 12:06:50 -0700 2265) 
97abc889ee296 (Oleg Nesterov              2019-06-28 12:06:50 -0700 2266) 	interrupted = signal_pending(current);
b772434be0891 (Oleg Nesterov              2019-07-16 16:29:53 -0700 2267) 	restore_saved_sigmask_unless(interrupted);
97abc889ee296 (Oleg Nesterov              2019-06-28 12:06:50 -0700 2268) 	if (interrupted && !ret)
7a35397f8c06b (Deepa Dinamani             2018-09-19 21:41:08 -0700 2269) 		ret = -ERESTARTNOHAND;
fa2e62a540034 (Deepa Dinamani             2017-08-04 21:12:32 -0700 2270) 
7a074e96dee62 (Christoph Hellwig          2018-05-02 19:51:00 +0200 2271) 	return ret;
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 2272) }
c00d2c7e89880 (Al Viro                    2016-12-20 07:04:57 -0500 2273) #endif