Support for polled and buffered aio (and more)
Subsystems (11)
Jens Axboe
maintainerPATCH v9 1/1 Support for polled and buffered aio (and more)
A few fixes and cleanups for the core part of this, and then a few experimental patches at the end:
1) Add support for polling for the SQTHREAD. If enabled, this will poll the SQ ring for submissions, and (if polling is enabled), it will poll for completions and place them in the CQ ring. This means an application will never have to enter the kernel do do IO, it simply updates the SQ ring with what it needs. Currently the thread goes to sleep after an idle second, to avoid spinning all the time. But if IO is continually flowing, it will keep running. Some room for improvement here, with some sort of backoff in terms of spinning and sleeping, but the concept seems sound. When updating the SQ ring with new entries, the application needs to do:
barrier();
if (sq_ring->kflags & IORING_SQ_NEED_WAKEUP)
io_ring_enter(ctx, to_submit, 0, IORING_SUBMIT);
in case the thread has gone to sleep. If IORING_SQ_NEED_WAKEUP isn't set, then no call needs to be made.
This works both with polled IO, in which case the SQ thread will poll for completions too (as mentioned), and with buffered/O_DIRECT async aio. For the latter, completions end up in the CQ ring when the IRQ triggers.
2) Utilization of struct io_event ->res2. This is unused in the kernel. One idea is to populate this with meta data about the IO operation. For buffered aio, we can tell the application if the IO hit media or was served out of the page cache. These kinds of metrics may be useful for an application.
For the latter, the sample application I wrote:
http://git.kernel.dk/cgit/fio/plain/t/aio-ring.c
will give you by-the-second cache hit rates when run with buffered=1, and fio with the aio-ring engine will do the same.
As before, find these patches in my aio-poll branch:
http://git.kernel.dk/cgit/linux-block/log/?h=aio-poll
or clone it here:
git://git.kernel.dk/linux-block aio-poll
and the code is based on my for-4.21/aio branch, which holds the aio patches queued up for 4.21 already.
Changes since v8:
- Get rid of kmalloc/kfree for inline cached buffered aio
- Add IOCTX_FLAG_SQPOLL
- Correct syscall prototype in <linux/syscalls.h> to match actual syscall
- Add "IO was cached" hint to res2
- Fixed an issue with indexing fixed buffer mappings
- Check for fixed buffer location, EFAULT if app changed location
- Unify bio_iov_bvec_add_pages() and bio_iov_iter_get_pages()
- Split out prep patches
- Style fixups
Documentation/filesystems/vfs.txt | 3 + Documentation/sysctl/fs.txt | 8 +- arch/x86/entry/syscalls/syscall_64.tbl | 2 + block/bio.c | 56 +- fs/aio.c | 1938 ++++++++++++++++++++++-- fs/block_dev.c | 23 +- fs/file.c | 15 +- fs/file_table.c | 9 +- fs/gfs2/file.c | 2 + fs/iomap.c | 52 +- fs/xfs/xfs_file.c | 1 + include/linux/blk_types.h | 2 + include/linux/file.h | 2 + include/linux/fs.h | 6 +- include/linux/iomap.h | 1 + include/linux/syscalls.h | 4 + include/uapi/asm-generic/unistd.h | 4 +- include/uapi/linux/aio_abi.h | 47 + kernel/sys_ni.c | 2 + 19 files changed, 1993 insertions(+), 184 deletions(-)
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerPATCH 1/22 fs: add an iopoll method to struct file_operations
From: Christoph Hellwig <[email protected]>
This new methods is used to explicitly poll for I/O completion for an iocb. It must be called for any iocb submitted asynchronously (that is with a non-null ki_complete) which has the IOCB_HIPRI flag set.
The method is assisted by a new ki_cookie field in struct iocb to store the polling cookie.
TODO: we can probably union ki_cookie with the existing hint and I/O priority fields to avoid struct kiocb growth.
--- Documentation/filesystems/vfs.txt | 3 +++ include/linux/fs.h | 2 ++ 2 files changed, 5 insertions(+)
Documentation/filesystems/vfs.txt +3 -0
@@ -857,6 +857,7 @@ struct file_operations { ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *); ssize_t (*read_iter) (struct kiocb *, struct iov_iter *); ssize_t (*write_iter) (struct kiocb *, struct iov_iter *); + int (*iopoll)(struct kiocb *kiocb, bool spin); int (*iterate) (struct file *, struct dir_context *); int (*iterate_shared) (struct file *, struct dir_context *); __poll_t (*poll) (struct file *, struct poll_table_struct *);
@@ -902,6 +903,8 @@ otherwise noted. write_iter: possibly asynchronous write with iov_iter as source + iopoll: called when aio wants to poll for completions on HIPRI iocbs + iterate: called when the VFS needs to read the directory contents iterate_shared: called when the VFS needs to read the directory contents
include/linux/fs.h +2 -0
@@ -310,6 +310,7 @@ struct kiocb {int ki_flags;u16 ki_hint;u16 ki_ioprio;/* See linux/ioprio.h */ +unsigned int ki_cookie;/* for ->iopoll */ } __randomize_layout;static inline bool is_sync_kiocb (struct kiocb *kiocb)
@@ -1781,6 +1782,7 @@ struct file_operations {ssize_t (*write) (struct file *,const char __user *,size_t ,loff_t *);ssize_t (*read_iter) (struct kiocb *,struct iov_iter *);ssize_t (*write_iter) (struct kiocb *,struct iov_iter *); +int (*iopoll)(struct kiocb *kiocb,bool spin);int (*iterate) (struct file *,struct dir_context *);int (*iterate_shared) (struct file *,struct dir_context *);__poll_t (*poll) (struct file *,struct poll_table_struct *);
Signature · 1 line
2.17.1
Jens Axboe
maintainerPATCH 2/22 block: add REQ_HIPRI_ASYNC
In reply to Jens AxboeFor the upcoming async polled IO, we can't sleep allocating requests. If we do, then we introduce a deadlock where the submitter already has async polled IO in-flight, but can't wait for them to complete since polled requests must be active found and reaped.
--- include/linux/blk_types.h | 1 + 1 file changed, 1 insertion(+)
include/linux/blk_types.h +1 -0
@@ -348,6 +348,7 @@ enum req_flag_bits {#define REQ_NOWAIT (1ULL << __REQ_NOWAIT)#define REQ_NOUNMAP (1ULL << __REQ_NOUNMAP)#define REQ_HIPRI (1ULL << __REQ_HIPRI) +#define REQ_HIPRI_ASYNC (REQ_HIPRI | REQ_NOWAIT)#define REQ_DRV (1ULL << __REQ_DRV)#define REQ_SWAP (1ULL << __REQ_SWAP)
Signature · 1 line
2.17.1
Re: [PATCH 02/22] block: add REQ_HIPRI_ASYNC
Quoted 4 lines · Jens Axboe
For the upcoming async polled IO, we can't sleep allocating requests. If we do, then we introduce a deadlock where the submitter already has async polled IO in-flight, but can't wait for them to complete since polled requests must be active found and reaped.
So two comments here. First I think after the next -rc1 we really need to rename REQ_HIPRI to REQ_POLLED. Unlike the iocb hipri flag which is an advice to treat this I/O as urgent REQ_POLLED is a hard charaterisation and having the hipri name is badly misnamed for that.
Second I retract my suggestion to add this _ASYNC variant, as it seems to cause more confusion than it helps. I think instead we want a helper like this:
static void bio_set_polled(struct bio *bio, struct kiocb *kiocb)
{
bio->bi_opf |= REQ_POLLED;
if (!is_sync_kiocb(kiocb)
bio->bi_opf |= REQ_NOWAIT;
}maybe plus comments to better indicate the intentional and consolidate the is_sync check.
Jens Axboe
maintainerRe: [PATCH 02/22] block: add REQ_HIPRI_ASYNC
Quoted 10 lines · Christoph Hellwig
Quoted 4 lines · Jens Axboe
For the upcoming async polled IO, we can't sleep allocating requests. If we do, then we introduce a deadlock where the submitter already has async polled IO in-flight, but can't wait for them to complete since polled requests must be active found and reaped.
So two comments here. First I think after the next -rc1 we really need to rename
REQ_HIPRItoREQ_POLLED. Unlike the iocb hipri flag which is an advice to treat this I/O as urgentREQ_POLLEDis a hard charaterisation and having the hipri name is badly misnamed for that.
Agree, it doesn't make a lot of sense to not call it _POLLED.
Quoted 5 lines
Second I retract my suggestion to add this
_ASYNCvariant, as it seems to cause more confusion than it helps. I think instead we want a helper like this:static void
bio_set_polled(struct bio *bio, struct kiocb *kiocb)
Quoted 8 lines
{ bio->bi_opf |= REQ_POLLED; if (!is_sync_kiocb(kiocb) bio->bi_opf |= REQ_NOWAIT; }maybe plus comments to better indicate the intentional and consolidate the
is_synccheck.
I'm fine with that, I'll do the cleanup.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerPATCH 3/22 block: wire up block device iopoll method
In reply to Jens AxboeFrom: Christoph Hellwig <[email protected]>
Just call blk_poll on the iocb cookie, we can derive the block device from the inode trivially.
--- fs/block_dev.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
fs/block_dev.c +10 -0
@@ -281,6 +281,14 @@ struct blkdev_dio {static struct bio_set blkdev_dio_pool; +static int blkdev_iopoll (struct kiocb *kiocb,bool wait) +{ +struct block_device *bdev =I_BDEV (kiocb->ki_filp->f_mapping->host); +struct request_queue *q =bdev_get_queue (bdev); + +return blk_poll (q,READ_ONCE (kiocb->ki_cookie), wait); +} +static void blkdev_bio_end_io (struct bio *bio) {struct blkdev_dio *dio = bio->bi_private;
@@ -398,6 +406,7 @@ __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, int nr_pages) bio->bi_opf |=REQ_HIPRI ; qc =submit_bio (bio); +WRITE_ONCE (iocb->ki_cookie, qc);break ; }
@@ -2070,6 +2079,7 @@ const struct file_operations def_blk_fops = { .llseek = block_llseek, .read_iter = blkdev_read_iter, .write_iter = blkdev_write_iter, + .iopoll = blkdev_iopoll, .mmap = generic_file_mmap, .fsync = blkdev_fsync, .unlocked_ioctl = block_ioctl,
Signature · 1 line
2.17.1
Jens Axboe
maintainerPATCH 4/22 block: use REQ_HIPRI_ASYNC for non-sync polled IO
In reply to Jens AxboeTell the block layer if it's a sync or async polled request, so it can do the right thing.
--- fs/block_dev.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
fs/block_dev.c +6 -2
@@ -402,8 +402,12 @@ __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, int nr_pages) nr_pages =iov_iter_npages (iter,BIO_MAX_PAGES );if (!nr_pages) { -if (iocb->ki_flags &IOCB_HIPRI ) - bio->bi_opf |=REQ_HIPRI ; +if (iocb->ki_flags &IOCB_HIPRI ) { +if (!is_sync) + bio->bi_opf |=REQ_HIPRI_ASYNC ; +else + bio->bi_opf |=REQ_HIPRI ; + } qc =submit_bio (bio);WRITE_ONCE (iocb->ki_cookie, qc);
Signature · 1 line
2.17.1
Jens Axboe
maintainerPATCH 5/22 iomap: wire up the iopoll method
In reply to Jens AxboeFrom: Christoph Hellwig <[email protected]>
Store the request queue the last bio was submitted to in the iocb private data in addition to the cookie so that we find the right block device. Also refactor the common direct I/O bio submission code into a nice little helper.
Modified to use REQ_HIPRI_ASYNC for async polled IO.
--- fs/gfs2/file.c | 2 ++ fs/iomap.c | 47 +++++++++++++++++++++++++++++-------------- fs/xfs/xfs_file.c | 1 + include/linux/iomap.h | 1 + 4 files changed, 36 insertions(+), 15 deletions(-)
fs/gfs2/file.c +2 -0
@@ -1280,6 +1280,7 @@ const struct file_operations gfs2_file_fops = { .llseek = gfs2_llseek, .read_iter = gfs2_file_read_iter, .write_iter = gfs2_file_write_iter, + .iopoll = iomap_dio_iopoll, .unlocked_ioctl = gfs2_ioctl, .mmap = gfs2_mmap, .open = gfs2_open,
@@ -1310,6 +1311,7 @@ const struct file_operations gfs2_file_fops_nolock = { .llseek = gfs2_llseek, .read_iter = gfs2_file_read_iter, .write_iter = gfs2_file_write_iter, + .iopoll = iomap_dio_iopoll, .unlocked_ioctl = gfs2_ioctl, .mmap = gfs2_mmap, .open = gfs2_open,
fs/iomap.c +32 -15
86 lines not on this page · raw message
fs/xfs/xfs_file.c +1 -0
@@ -1203,6 +1203,7 @@ const struct file_operations xfs_file_operations = { .write_iter = xfs_file_write_iter, .splice_read = generic_file_splice_read, .splice_write = iter_file_splice_write, + .iopoll = iomap_dio_iopoll, .unlocked_ioctl = xfs_file_ioctl, #ifdef CONFIG_COMPAT .compat_ioctl = xfs_file_compat_ioctl,
include/linux/iomap.h +1 -0
@@ -162,6 +162,7 @@ typedef int (iomap_dio_end_io_t)(struct kiocb *iocb, ssize_t ret, unsigned flags);ssize_t iomap_dio_rw (struct kiocb *iocb,struct iov_iter *iter,const struct iomap_ops *ops,iomap_dio_end_io_t end_io); +int iomap_dio_iopoll (struct kiocb *kiocb,bool spin);#ifdef CONFIG_SWAP struct file ;
Signature · 1 line
2.17.1
Jens Axboe
maintainerPATCH 6/22 aio: add io_setup2() system call
In reply to Jens AxboeThis is just like io_setup(), except add a flags argument to let the caller control/define some of the io_context behavior.
Outside of the flags, we add an iocb array and two user pointers for future use.
--- Documentation/sysctl/fs.txt | 8 +-- arch/x86/entry/syscalls/syscall_64.tbl | 1 + fs/aio.c | 76 +++++++++++++++++--------- include/linux/syscalls.h | 2 + include/uapi/asm-generic/unistd.h | 4 +- kernel/sys_ni.c | 1 + 6 files changed, 62 insertions(+), 30 deletions(-)
Documentation/sysctl/fs.txt +4 -4
@@ -47,10 +47,10 @@ Currently, these files are in /proc/sys/fs: aio-nr & aio-max-nr: aio-nr is the running total of the number of events specified on the -io_setup system call for all currently active aio contexts. If aio-nr -reaches aio-max-nr then io_setup will fail with EAGAIN. Note that -raising aio-max-nr does not result in the pre-allocation or re-sizing -of any kernel data structures. +io_setup/io_setup2 system call for all currently active aio contexts. +If aio-nr reaches aio-max-nr then io_setup will fail with EAGAIN. +Note that raising aio-max-nr does not result in the pre-allocation or +re-sizing of any kernel data structures. ==============================================================
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
@@ -343,6 +343,7 @@ 332 common statx __x64_sys_statx 333 common io_pgetevents __x64_sys_io_pgetevents 334 common rseq __x64_sys_rseq +335 common io_setup2 __x64_sys_io_setup2 # # x32-specific system call numbers start at 512 to avoid cache impact
fs/aio.c +51 -25
136 lines not on this page · raw message
include/linux/syscalls.h +2 -0
@@ -287,6 +287,8 @@ static inline void addr_limit_user_check(void)*/ #ifndef CONFIG_ARCH_HAS_SYSCALL_WRAPPER asmlinkage long sys_io_setup (unsigned nr_reqs,aio_context_t __user *ctx); +asmlinkage long sys_io_setup2 (unsigned ,unsigned ,void __user *,void __user *, +aio_context_t __user *);asmlinkage long sys_io_destroy (aio_context_t ctx);asmlinkage long sys_io_submit (aio_context_t ,long ,struct iocb __user *__user *);
include/uapi/asm-generic/unistd.h +3 -1
@@ -738,9 +738,11 @@ __SYSCALL(__NR_statx, sys_statx) __SC_COMP(__NR_io_pgetevents, sys_io_pgetevents, compat_sys_io_pgetevents)#define __NR_rseq 293 __SYSCALL(__NR_rseq, sys_rseq) +#define __NR_io_setup2 294 +__SYSCALL(__NR_io_setup2, sys_io_setup2)#undef __NR_syscalls -#define __NR_syscalls 294 +#define __NR_syscalls 295/* * 32 bit systems traditionally used different
kernel/sys_ni.c +1 -0
@@ -37,6 +37,7 @@ asmlinkage long sys_ni_syscall(void)*/ COND_SYSCALL (io_setup); +COND_SYSCALL (io_setup2);COND_SYSCALL_COMPAT (io_setup);COND_SYSCALL (io_destroy);COND_SYSCALL (io_submit);
Signature · 1 line
2.17.1
Re: [PATCH 06/22] aio: add io_setup2() system call
Quoted 7 lines · Jens Axboe
This is just like
io_setup(), except add a flags argument to let the caller control/define some of theio_contextbehavior.Outside of the flags, we add an iocb array and two user pointers for future use.
Signed-off-by Jens Axboe <[email protected]>
Looks good,
But this really needs a Cc to linux-abi and a manpage addition.
Jens Axboe
maintainerPATCH 7/22 aio: support for IO polling
In reply to Jens AxboeAdd polled variants of PREAD/PREADV and PWRITE/PWRITEV. These act like their non-polled counterparts, except we expect to poll for completion of them. The polling happens at io_getevent() time, and works just like non-polled IO.
To setup an io_context for polled IO, the application must call io_setup2() with IOCTX_FLAG_IOPOLL as one of the flags. It is illegal to mix and match polled and non-polled IO on an io_context.
Polled IO doesn't support the user mapped completion ring. Events must be reaped through the io_getevents() system call. For non-irq driven poll devices, there's no way to support completion reaping from userspace by just looking at the ring. The application itself is the one that pulls completion entries.
--- fs/aio.c | 419 +++++++++++++++++++++++++++++++---- include/uapi/linux/aio_abi.h | 4 + 2 files changed, 384 insertions(+), 39 deletions(-)
fs/aio.c +380 -39
@@ -147,6 +147,18 @@ struct kioctx {atomic_t reqs_available; }____cacheline_aligned_in_smp ; +/* iopoll submission state */ +struct { +spinlock_t poll_lock; +struct list_head poll_submitted; + }____cacheline_aligned_in_smp ; + +/* iopoll completion state */ +struct { +struct list_head poll_completing; +struct mutex getevents_lock; + }____cacheline_aligned_in_smp ; +struct {spinlock_t ctx_lock;struct list_head active_reqs;/* used for cancellation */
@@ -199,14 +211,27 @@ struct aio_kiocb {__u64 ki_user_data;/* user's data for completion */ struct list_head ki_list;/* the aio core uses this -* for cancellation */ +* for cancellation, or for +* polled IO */ + +unsigned long ki_flags; +#define KIOCB_F_POLL_COMPLETED 0/* polled IO has completed */ +#define KIOCB_F_POLL_EAGAIN 1/* polled submission got EAGAIN */ +refcount_t ki_refcnt; -/* -* If the aio_resfd field of the userspace iocb is not zero, -* this is the underlying eventfd context to deliver events to. -*/ -struct eventfd_ctx *ki_eventfd; +union { +/* +* If the aio_resfd field of the userspace iocb is not zero, +* this is the underlying eventfd context to deliver events to. +*/ +struct eventfd_ctx *ki_eventfd; + +/* +* For polled IO, stash completion info here +*/ +struct io_event ki_ev; + }; };/*------ sysctl variables----*/
@@ -223,6 +248,8 @@ static struct vfsmount *aio_mnt;static const struct file_operations aio_ring_fops;static const struct address_space_operations aio_ctx_aops; +static void aio_iopoll_reap_events (struct kioctx *); +static struct file *aio_private_file (struct kioctx *ctx,loff_t nr_pages) {struct file *file;
@@ -460,11 +487,15 @@ static int aio_setup_ring(struct kioctx *ctx, unsigned int nr_events)int i;struct file *file; -/* Compensate for the ring buffer's head/tail overlap entry */ - nr_events +=2 ;/* 1 is required, 2 for good luck */ - +/* +* Compensate for the ring buffer's head/tail overlap entry. +* IO polling doesn't require any io event entries +*/ size =sizeof (struct aio_ring ); - size +=sizeof (struct io_event ) * nr_events; +if (!(ctx->flags &IOCTX_FLAG_IOPOLL )) { + nr_events +=2 ;/* 1 is required, 2 for good luck */ + size +=sizeof (struct io_event ) * nr_events; + } nr_pages =PFN_UP (size);if (nr_pages <0 )
@@ -548,6 +579,14 @@ static int aio_setup_ring(struct kioctx *ctx, unsigned int nr_events)return 0 ; } +/* +* Don't support cancel on anything that isn't old aio +*/ +static bool aio_ctx_supports_cancel (struct kioctx *ctx) +{ +return (ctx->flags &IOCTX_FLAG_IOPOLL ) ==0 ; +} +#define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))#define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))#define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
@@ -558,6 +597,8 @@ void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel)struct kioctx *ctx = req->ki_ctx;unsigned long flags; +if (WARN_ON_ONCE (!aio_ctx_supports_cancel (ctx))) +return ;if (WARN_ON_ONCE (!list_empty (&req->ki_list)))return ;
@@ -746,6 +787,11 @@ static struct kioctx *io_setup_flags(unsigned long ctxid,INIT_LIST_HEAD (&ctx->active_reqs); +spin_lock_init (&ctx->poll_lock); +INIT_LIST_HEAD (&ctx->poll_submitted); +INIT_LIST_HEAD (&ctx->poll_completing); +mutex_init (&ctx->getevents_lock); +if (percpu_ref_init (&ctx->users, free_ioctx_users,0 ,GFP_KERNEL )) goto err;
@@ -817,11 +863,15 @@ static int kill_ioctx(struct mm_struct *mm, struct kioctx *ctx, {struct kioctx_table *table; +mutex_lock (&ctx->getevents_lock);spin_lock (&mm->ioctx_lock);if (atomic_xchg (&ctx->dead,1 )) {spin_unlock (&mm->ioctx_lock); +mutex_unlock (&ctx->getevents_lock);return -EINVAL ; } +aio_iopoll_reap_events (ctx); +mutex_unlock (&ctx->getevents_lock); table =rcu_dereference_raw (mm->ioctx_table);WARN_ON (ctx !=rcu_access_pointer (table->table[ctx->id]));
@@ -1030,6 +1080,7 @@ static inline struct aio_kiocb *aio_get_req(struct kioctx *ctx)percpu_ref_get (&ctx->reqs); req->ki_ctx = ctx;INIT_LIST_HEAD (&req->ki_list); + req->ki_flags =0 ;refcount_set (&req->ki_refcnt,0 ); req->ki_eventfd =NULL ;return req;
@@ -1072,6 +1123,15 @@ static inline void iocb_put(struct aio_kiocb *iocb) } } +static void iocb_put_many (struct kioctx *ctx,void **iocbs,int *nr) +{ +if (*nr) { +percpu_ref_put_many (&ctx->reqs, *nr); +kmem_cache_free_bulk (kiocb_cachep, *nr, iocbs); + *nr =0 ; + } +} +static void aio_fill_event (struct io_event *ev,struct aio_kiocb *iocb,long res,long res2) {
@@ -1261,6 +1321,185 @@ static bool aio_read_events(struct kioctx *ctx, long min_nr, long nr,return ret <0 || *i >= min_nr; } +#define AIO_IOPOLL_BATCH 8 + +/* +* Process completed iocb iopoll entries, copying the result to userspace. +*/ +static long aio_iopoll_reap (struct kioctx *ctx,struct io_event __user *evs, +unsigned int *nr_events,long max) +{ +void *iocbs[AIO_IOPOLL_BATCH ]; +struct aio_kiocb *iocb, *n; +int to_free =0 , ret =0 ; + +/* Shouldn't happen... */ +if (*nr_events >= max) +return 0 ; + +list_for_each_entry_safe (iocb, n, &ctx->poll_completing, ki_list) { +if (*nr_events == max) +break ; +if (!test_bit (KIOCB_F_POLL_COMPLETED , &iocb->ki_flags)) +continue ; +if (to_free ==AIO_IOPOLL_BATCH ) +iocb_put_many (ctx, iocbs, &to_free); + +list_del (&iocb->ki_list); + iocbs[to_free++] = iocb; + +fput (iocb->rw.ki_filp); + +if (evs &©_to_user (evs + *nr_events, &iocb->ki_ev, +sizeof (iocb->ki_ev))) { + ret = -EFAULT ; +break ; + } + (*nr_events)++; + } + +if (to_free) +iocb_put_many (ctx, iocbs, &to_free); + +return ret; +} + +/* +* Poll for a mininum of 'min' events, and a maximum of 'max'. Note that if +* min == 0 we consider that a non-spinning poll check - we'll still enter +* the driver poll loop, but only as a non-spinning completion check. +*/ +static int aio_iopoll_getevents (struct kioctx *ctx, +struct io_event __user *event, +unsigned int *nr_events,long min,long max) +{ +struct aio_kiocb *iocb; +int to_poll, polled, ret; + +/* +* Check if we already have done events that satisfy what we need +*/ +if (!list_empty (&ctx->poll_completing)) { + ret =aio_iopoll_reap (ctx, event, nr_events, max); +if (ret <0 ) +return ret; +if ((min && *nr_events >= min) || *nr_events >= max) +return 0 ; + } + +/* +* Take in a new working set from the submitted list, if possible. +*/ +if (!list_empty_careful (&ctx->poll_submitted)) { +spin_lock (&ctx->poll_lock); +list_splice_init (&ctx->poll_submitted, &ctx->poll_completing); +spin_unlock (&ctx->poll_lock); + } + +if (list_empty (&ctx->poll_completing)) +return 0 ; + +/* +* Check again now that we have a new batch. +*/ + ret =aio_iopoll_reap (ctx, event, nr_events, max); +if (ret <0 ) +return ret; +if ((min && *nr_events >= min) || *nr_events >= max) +return 0 ; + +/* +* Find up to 'max' worth of events to poll for, including the +* events we already successfully polled +*/ + polled = to_poll =0 ; +list_for_each_entry (iocb, &ctx->poll_completing, ki_list) { +/* +* Poll for needed events with spin == true, anything after +* that we just check if we have more, up to max. +*/ +bool spin = !polled || *nr_events < min; +struct kiocb *kiocb = &iocb->rw; + +if (test_bit (KIOCB_F_POLL_COMPLETED , &iocb->ki_flags)) +break ; +if (++to_poll + *nr_events > max) +break ; + + ret = kiocb->ki_filp->f_op->iopoll (kiocb, spin); +if (ret <0 ) +return ret; + + polled += ret; +if (polled + *nr_events >= max) +break ; + } + + ret =aio_iopoll_reap (ctx, event, nr_events, max); +if (ret <0 ) +return ret; +if (*nr_events >= min) +return 0 ; +return to_poll; +} + +/* +* We can't just wait for polled events to come to us, we have to actively +* find and complete them. +*/ +static void aio_iopoll_reap_events (struct kioctx *ctx) +{ +if (!(ctx->flags &IOCTX_FLAG_IOPOLL )) +return ; + +while (!list_empty_careful (&ctx->poll_submitted) || + !list_empty (&ctx->poll_completing)) { +unsigned int nr_events =0 ; + +aio_iopoll_getevents (ctx,NULL , &nr_events,1 ,UINT_MAX ); + } +} + +static int __aio_iopoll_check (struct kioctx *ctx,struct io_event __user *event, +unsigned int *nr_events,long min_nr,long max_nr) +{ +int ret =0 ; + +while (!*nr_events || !need_resched ()) { +int tmin =0 ; + +if (*nr_events < min_nr) + tmin = min_nr - *nr_events; + + ret =aio_iopoll_getevents (ctx, event, nr_events, tmin, max_nr); +if (ret <=0 ) +break ; + ret =0 ; + } + +return ret; +} + +static int aio_iopoll_check (struct kioctx *ctx,long min_nr,long nr, +struct io_event __user *event) +{ +unsigned int nr_events =0 ; +int ret; + +/* Only allow one thread polling at a time */ +if (!mutex_trylock (&ctx->getevents_lock)) +return -EBUSY ; +if (unlikely (atomic_read (&ctx->dead))) { + ret = -EINVAL ; + goto err; + } + + ret =__aio_iopoll_check (ctx, event, &nr_events, min_nr, nr); +err: +mutex_unlock (&ctx->getevents_lock); +return nr_events ? nr_events : ret; +} +static long read_events (struct kioctx *ctx,long min_nr,long nr,struct io_event __user *event,ktime_t until)
@@ -1305,7 +1544,9 @@ SYSCALL_DEFINE5(io_setup2, u32, nr_events, u32, flags, void __user *, user1,unsigned long ctx;long ret; -if (flags || user1 || user2) +if (user1 || user2) +return -EINVAL ; +if (flags & ~IOCTX_FLAG_IOPOLL )return -EINVAL ; ret =get_user (ctx, ctxp);
@@ -1431,13 +1672,8 @@ static void aio_remove_iocb(struct aio_kiocb *iocb)spin_unlock_irqrestore (&ctx->ctx_lock, flags); } -static void aio_complete_rw (struct kiocb *kiocb,long res,long res2) +static void kiocb_end_write (struct kiocb *kiocb) { - struct aio_kiocb *iocb = container_of(kiocb, struct aio_kiocb, rw); - -if (!list_empty_careful (&iocb->ki_list)) -aio_remove_iocb (iocb); -if (kiocb->ki_flags &IOCB_WRITE ) {struct inode *inode =file_inode (kiocb->ki_filp);
@@ -1449,19 +1685,48 @@ static void aio_complete_rw(struct kiocb *kiocb, long res, long res2)__sb_writers_acquired (inode->i_sb,SB_FREEZE_WRITE );file_end_write (kiocb->ki_filp); } +} + +static void aio_complete_rw (struct kiocb *kiocb,long res,long res2) +{ + struct aio_kiocb *iocb = container_of(kiocb, struct aio_kiocb, rw); + +if (!list_empty_careful (&iocb->ki_list)) +aio_remove_iocb (iocb); + +kiocb_end_write (kiocb);fput (kiocb->ki_filp);aio_complete (iocb, res, res2); } -static int aio_prep_rw (struct kiocb *req,const struct iocb *iocb) +static void aio_complete_rw_poll (struct kiocb *kiocb,long res,long res2) { + struct aio_kiocb *iocb = container_of(kiocb, struct aio_kiocb, rw); + +kiocb_end_write (kiocb); + +/* +* Handle EAGAIN from resource limits with polled IO inline, don't +* pass the event back to userspace. +*/ +if (unlikely (res == -EAGAIN )) +set_bit (KIOCB_F_POLL_EAGAIN , &iocb->ki_flags); +else { +aio_fill_event (&iocb->ki_ev, iocb, res, res2); +set_bit (KIOCB_F_POLL_COMPLETED , &iocb->ki_flags); + } +} + +static int aio_prep_rw (struct aio_kiocb *kiocb,const struct iocb *iocb) +{ +struct kioctx *ctx = kiocb->ki_ctx; +struct kiocb *req = &kiocb->rw;int ret; req->ki_filp =fget (iocb->aio_fildes);if (unlikely (!req->ki_filp))return -EBADF ; - req->ki_complete = aio_complete_rw; req->ki_pos = iocb->aio_offset; req->ki_flags =iocb_flags (req->ki_filp);if (iocb->aio_flags &IOCB_FLAG_RESFD )
@@ -1487,9 +1752,35 @@ static int aio_prep_rw(struct kiocb *req, const struct iocb *iocb)if (unlikely (ret)) goto out_fput; - req->ki_flags &= ~IOCB_HIPRI ;/* no one is going to poll for this I/O */ -return 0 ; +if (iocb->aio_flags &IOCB_FLAG_HIPRI ) { +/* shares space in the union, and is rather pointless.. */ + ret = -EINVAL ; +if (iocb->aio_flags &IOCB_FLAG_RESFD ) + goto out_fput; + +/* can't submit polled IO to a non-polled ctx */ +if (!(ctx->flags &IOCTX_FLAG_IOPOLL )) + goto out_fput; + + ret = -EOPNOTSUPP ; +if (!(req->ki_flags &IOCB_DIRECT ) || + !req->ki_filp->f_op->iopoll) + goto out_fput; + + req->ki_flags |=IOCB_HIPRI ; + req->ki_complete = aio_complete_rw_poll; + }else { +/* can't submit non-polled IO to a polled ctx */ + ret = -EINVAL ; +if (ctx->flags &IOCTX_FLAG_IOPOLL ) + goto out_fput; +/* no one is going to poll for this I/O */ + req->ki_flags &= ~IOCB_HIPRI ; + req->ki_complete = aio_complete_rw; + } + +return 0 ; out_fput:fput (req->ki_filp);return ret;
@@ -1534,15 +1825,40 @@ static inline void aio_rw_done(struct kiocb *req, ssize_t ret) } } -static ssize_t aio_read (struct kiocb *req,const struct iocb *iocb, +/* +* After the iocb has been issued, it's safe to be found on the poll list. +* Adding the kiocb to the list AFTER submission ensures that we don't +* find it from a io_getevents() thread before the issuer is done accessing +* the kiocb cookie. +*/ +static void aio_iopoll_iocb_issued (struct aio_kiocb *kiocb) +{ +/* +* For fast devices, IO may have already completed. If it has, add +* it to the front so we find it first. We can't add to the poll_done +* list as that's unlocked from the completion side. +*/ +const int front =test_bit (KIOCB_F_POLL_COMPLETED , &kiocb->ki_flags); +struct kioctx *ctx = kiocb->ki_ctx; + +spin_lock (&ctx->poll_lock); +if (front) +list_add (&kiocb->ki_list, &ctx->poll_submitted); +else +list_add_tail (&kiocb->ki_list, &ctx->poll_submitted); +spin_unlock (&ctx->poll_lock); +} + +static ssize_t aio_read (struct aio_kiocb *kiocb,const struct iocb *iocb,bool vectored,bool compat) {struct iovec inline_vecs[UIO_FASTIOV ], *iovec = inline_vecs; +struct kiocb *req = &kiocb->rw;struct iov_iter iter;struct file *file;ssize_t ret; - ret =aio_prep_rw (req, iocb); + ret =aio_prep_rw (kiocb, iocb);if (ret)return ret; file = req->ki_filp;
@@ -1567,15 +1883,16 @@ static ssize_t aio_read(struct kiocb *req, const struct iocb *iocb,return ret; } -static ssize_t aio_write (struct kiocb *req,const struct iocb *iocb, +static ssize_t aio_write (struct aio_kiocb *kiocb,const struct iocb *iocb,bool vectored,bool compat) {struct iovec inline_vecs[UIO_FASTIOV ], *iovec = inline_vecs; +struct kiocb *req = &kiocb->rw;struct iov_iter iter;struct file *file;ssize_t ret; - ret =aio_prep_rw (req, iocb); + ret =aio_prep_rw (kiocb, iocb);if (ret)return ret; file = req->ki_filp;
@@ -1846,7 +2163,8 @@ static int __io_submit_one(struct kioctx *ctx, const struct iocb *iocb,return -EINVAL ; } -if (!get_reqs_available (ctx)) +/* Poll IO doesn't need ring reservations */ +if (!(ctx->flags &IOCTX_FLAG_IOPOLL ) && !get_reqs_available (ctx))return -EAGAIN ; ret = -EAGAIN ;
@@ -1869,35 +2187,44 @@ static int __io_submit_one(struct kioctx *ctx, const struct iocb *iocb, } } - ret =put_user (KIOCB_KEY , &user_iocb->aio_key); -if (unlikely (ret)) { -pr_debug ("EFAULT: aio_key\n" ); - goto out_put_req; +if (aio_ctx_supports_cancel (ctx)) { + ret =put_user (KIOCB_KEY , &user_iocb->aio_key); +if (unlikely (ret)) { +pr_debug ("EFAULT: aio_key\n" ); + goto out_put_req; + } } req->ki_user_iocb = user_iocb; req->ki_user_data = iocb->aio_data; + ret = -EINVAL ;switch (iocb->aio_lio_opcode) {case IOCB_CMD_PREAD : - ret =aio_read (&req->rw, iocb, false, compat); + ret =aio_read (req, iocb, false, compat);break ;case IOCB_CMD_PWRITE : - ret =aio_write (&req->rw, iocb, false, compat); + ret =aio_write (req, iocb, false, compat);break ;case IOCB_CMD_PREADV : - ret =aio_read (&req->rw, iocb, true, compat); + ret =aio_read (req, iocb, true, compat);break ;case IOCB_CMD_PWRITEV : - ret =aio_write (&req->rw, iocb, true, compat); + ret =aio_write (req, iocb, true, compat);break ;case IOCB_CMD_FSYNC : +if (ctx->flags &IOCTX_FLAG_IOPOLL ) +break ; ret =aio_fsync (&req->fsync, iocb, false);break ;case IOCB_CMD_FDSYNC : +if (ctx->flags &IOCTX_FLAG_IOPOLL ) +break ; ret =aio_fsync (&req->fsync, iocb, true);break ;case IOCB_CMD_POLL : +if (ctx->flags &IOCTX_FLAG_IOPOLL ) +break ; ret =aio_poll (req, iocb);break ;default :
@@ -1913,13 +2240,21 @@ static int __io_submit_one(struct kioctx *ctx, const struct iocb *iocb,*/ if (ret) goto out_put_req; +if (ctx->flags &IOCTX_FLAG_IOPOLL ) { +if (test_bit (KIOCB_F_POLL_EAGAIN , &req->ki_flags)) { + ret = -EAGAIN ; + goto out_put_req; + } +aio_iopoll_iocb_issued (req); + }return 0 ; out_put_req:if (req->ki_eventfd)eventfd_ctx_put (req->ki_eventfd);iocb_put (req); out_put_reqs_available: -put_reqs_available (ctx,1 ); +if (!(ctx->flags &IOCTX_FLAG_IOPOLL )) +put_reqs_available (ctx,1 );return ret; }
@@ -2075,6 +2410,9 @@ SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,if (unlikely (!ctx))return -EINVAL ; +if (!aio_ctx_supports_cancel (ctx)) + goto err; +spin_lock_irq (&ctx->ctx_lock); kiocb =lookup_kiocb (ctx, iocb);if (kiocb) {
@@ -2091,9 +2429,8 @@ SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,*/ ret = -EINPROGRESS ; } - +err:percpu_ref_put (&ctx->users); -return ret; }
@@ -2108,8 +2445,12 @@ static long do_io_getevents(aio_context_t ctx_id,long ret = -EINVAL ;if (likely (ioctx)) { -if (likely (min_nr <= nr && min_nr >=0 )) - ret =read_events (ioctx, min_nr, nr, events, until); +if (likely (min_nr <= nr && min_nr >=0 )) { +if (ioctx->flags &IOCTX_FLAG_IOPOLL ) + ret =aio_iopoll_check (ioctx, min_nr, nr, events); +else + ret =read_events (ioctx, min_nr, nr, events, until); + }percpu_ref_put (&ioctx->users); }
include/uapi/linux/aio_abi.h +4 -0
@@ -52,9 +52,11 @@ enum {* is valid. * IOCB_FLAG_IOPRIO - Set if the "aio_reqprio" member of the "struct iocb" * is valid. +* IOCB_FLAG_HIPRI - Use IO completion polling */ #define IOCB_FLAG_RESFD (1 << 0)#define IOCB_FLAG_IOPRIO (1 << 1) +#define IOCB_FLAG_HIPRI (1 << 2)/* read() from /dev/aio returns these structures. */ struct io_event {
@@ -106,6 +108,8 @@ struct iocb {__u32 aio_resfd; };/* 64 bytes */ +#define IOCTX_FLAG_IOPOLL (1 << 0)/* io_context is polled */ +#undef IFBIG#undef IFLITTLE
Signature · 1 line
2.17.1
Re: [PATCH 07/22] aio: support for IO polling
Quoted 4 lines · Jens Axboe
Add polled variants of PREAD/PREADV and PWRITE/PWRITEV. These act like their non-polled counterparts, except we expect to poll for completion of them. The polling happens at
io_getevent()time, and works just like non-polled IO.
This description is highly misleading as it doesn't match the code.
Also needs a Cc to linux-api and a man page addition.
Quoted 1 line
+#define
AIO_IOPOLL_BATCH8
We later rename this to AIO_POLL batch and move it up the file, it would be great if we could that fold in here.
fs/aio.c static void aio_complete_rw(struct kiocb *kiocb, long res, long res2) · line 1712
Quoted 4 lines
fs/aio.c +4
-0+/* +* Handle EAGAIN from resource limits with polled IO inline, don't +* pass the event back to userspace. +*/
This comment looks confusing. I think this is the RWF_NOWAIT failure? I'd say either explain it way better or not at all.
fs/aio.c static void aio_complete_rw(struct kiocb *kiocb, long res, long res2) · line 1718
Quoted 6 lines
fs/aio.c +6
-0+if (unlikely (res == -EAGAIN )) +set_bit (KIOCB_F_POLL_EAGAIN , &iocb->ki_flags); +else { +aio_fill_event (&iocb->ki_ev, iocb, res, res2); +set_bit (KIOCB_F_POLL_COMPLETED , &iocb->ki_flags); + }
Please always use braces on both sides of the else.
fs/aio.c static int aio_prep_rw(struct kiocb *req, const struct iocb *iocb) · line 1763
Quoted 9 lines
fs/aio.c +9
-0+if (iocb->aio_flags &IOCB_FLAG_HIPRI ) { +/* shares space in the union, and is rather pointless.. */ + ret = -EINVAL ; +if (iocb->aio_flags &IOCB_FLAG_RESFD ) + goto out_fput; + +/* can't submit polled IO to a non-polled ctx */ +if (!(ctx->flags &IOCTX_FLAG_IOPOLL )) + goto out_fput;
Given that we can't mix and match polled and non-polled I/O we should drop IOCB_FLAG_HIPRI entirely.
Jens Axboe
maintainerRe: [PATCH 07/22] aio: support for IO polling
Quoted 6 lines · Christoph Hellwig
Quoted 4 lines · Jens Axboe
Add polled variants of PREAD/PREADV and PWRITE/PWRITEV. These act like their non-polled counterparts, except we expect to poll for completion of them. The polling happens at
io_getevent()time, and works just like non-polled IO.
Quoted 8 lines
This description is highly misleading as it doesn't match the code.
Also needs a Cc to linux-api and a man page addition.
Quoted 1 line
+#define
AIO_IOPOLL_BATCH8We later rename this to
AIO_POLLbatch and move it up the file, it would be great if we could that fold in here.
It is moved, but it's never renamed. I'll get rid of the move.
Quoted 7 lines
fs/aio.c static void aio_complete_rw(struct kiocb *kiocb, long res, long res2) · line 1712
Quoted 4 lines
fs/aio.c +4
-0+/* +* Handle EAGAIN from resource limits with polled IO inline, don't +* pass the event back to userspace. +*/ This comment looks confusing. I think this is the
RWF_NOWAITfailure? I'd say either explain it way better or not at all.
Right, it's for RQF_NOWAIT. The point is we return that in the submission path, not through an io_event. I'll update the comment.
Quoted 8 lines
fs/aio.c static void aio_complete_rw(struct kiocb *kiocb, long res, long res2) · line 1718
Quoted 6 lines
fs/aio.c +6
-0+if (unlikely (res == -EAGAIN )) +set_bit (KIOCB_F_POLL_EAGAIN , &iocb->ki_flags); +else { +aio_fill_event (&iocb->ki_ev, iocb, res, res2); +set_bit (KIOCB_F_POLL_COMPLETED , &iocb->ki_flags); + }Please always use braces on both sides of the else.
Fixed
Quoted 4 lines
Quoted 4 lines
fs/aio.c +4
-0+if (iocb->aio_flags &IOCB_FLAG_HIPRI ) { +/* shares space in the union, and is rather pointless.. */ + ret = -EINVAL ; +if (iocb->aio_flags &IOCB_FLAG_RESFD )
Quoted 8 lines
fs/aio.c static int aio_prep_rw(struct kiocb *req, const struct iocb *iocb) · line 1763
Quoted 5 lines
fs/aio.c +5
-0+ goto out_fput; + +/* can't submit polled IO to a non-polled ctx */ +if (!(ctx->flags &IOCTX_FLAG_IOPOLL )) + goto out_fput;Given that we can't mix and match polled and non-polled I/O we should drop
IOCB_FLAG_HIPRIentirely.
I kind of like how it's explicit like that, even if it isn't strictly needed. It makes it easier to verify apps are doing the right thing too. Don't feel super strongly about it, though.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerPATCH 8/22 aio: add submission side request cache
In reply to Jens AxboeWe have to add each submitted polled request to the io_context poll_submitted list, which means we have to grab the poll_lock. We already use the block plug to batch submissions if we're doing a batch of IO submissions, extend that to cover the poll requests internally as well.
--- fs/aio.c | 136 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 113 insertions(+), 23 deletions(-)
fs/aio.c +113 -23
@@ -234,6 +234,21 @@ struct aio_kiocb { }; }; +struct aio_submit_state { +struct kioctx *ctx; + +struct blk_plug plug; +#ifdef CONFIG_BLOCK +struct blk_plug_cb plug_cb; +#endif + +/* +* Polled iocbs that have been submitted, but not added to the ctx yet +*/ +struct list_head req_list; +unsigned int req_count; +}; +/*------ sysctl variables----*/ static DEFINE_SPINLOCK (aio_nr_lock);unsigned long aio_nr;/* current system wide number of aio requests */
@@ -248,6 +263,15 @@ static struct vfsmount *aio_mnt;static const struct file_operations aio_ring_fops;static const struct address_space_operations aio_ctx_aops; +/* +* We rely on block level unplugs to flush pending requests, if we schedule +*/ +#ifdef CONFIG_BLOCK +static const bool aio_use_state_req_list = true; +#else +static const bool aio_use_state_req_list = false; +#endif +static void aio_iopoll_reap_events (struct kioctx *);static struct file *aio_private_file (struct kioctx *ctx,loff_t nr_pages)
@@ -1825,13 +1849,28 @@ static inline void aio_rw_done(struct kiocb *req, ssize_t ret) } } +/* +* Called either at the end of IO submission, or through a plug callback +* because we're going to schedule. Moves out local batch of requests to +* the ctx poll list, so they can be found for polling + reaping. +*/ +static void aio_flush_state_reqs (struct kioctx *ctx, +struct aio_submit_state *state) +{ +spin_lock (&ctx->poll_lock); +list_splice_tail_init (&state->req_list, &ctx->poll_submitted); +spin_unlock (&ctx->poll_lock); + state->req_count =0 ; +} +/* * After the iocb has been issued, it's safe to be found on the poll list. * Adding the kiocb to the list AFTER submission ensures that we don't * find it from a io_getevents() thread before the issuer is done accessing * the kiocb cookie. */ -static void aio_iopoll_iocb_issued (struct aio_kiocb *kiocb) +static void aio_iopoll_iocb_issued (struct aio_submit_state *state, +struct aio_kiocb *kiocb) {/* * For fast devices, IO may have already completed. If it has, add
@@ -1841,12 +1880,21 @@ static void aio_iopoll_iocb_issued(struct aio_kiocb *kiocb)const int front =test_bit (KIOCB_F_POLL_COMPLETED , &kiocb->ki_flags);struct kioctx *ctx = kiocb->ki_ctx; -spin_lock (&ctx->poll_lock); -if (front) -list_add (&kiocb->ki_list, &ctx->poll_submitted); -else -list_add_tail (&kiocb->ki_list, &ctx->poll_submitted); -spin_unlock (&ctx->poll_lock); +if (!state || !aio_use_state_req_list) { +spin_lock (&ctx->poll_lock); +if (front) +list_add (&kiocb->ki_list, &ctx->poll_submitted); +else +list_add_tail (&kiocb->ki_list, &ctx->poll_submitted); +spin_unlock (&ctx->poll_lock); + }else { +if (front) +list_add (&kiocb->ki_list, &state->req_list); +else +list_add_tail (&kiocb->ki_list, &state->req_list); +if (++state->req_count >=AIO_IOPOLL_BATCH ) +aio_flush_state_reqs (ctx, state); + } } static ssize_t aio_read(struct aio_kiocb *kiocb, const struct iocb *iocb,
@@ -2142,7 +2190,8 @@ static ssize_t aio_poll(struct aio_kiocb *aiocb, const struct iocb *iocb) }static int __io_submit_one (struct kioctx *ctx,const struct iocb *iocb, -struct iocb __user *user_iocb,bool compat) +struct iocb __user *user_iocb, +struct aio_submit_state *state,bool compat) {struct aio_kiocb *req;ssize_t ret;
@@ -2245,7 +2294,7 @@ static int __io_submit_one(struct kioctx *ctx, const struct iocb *iocb, ret = -EAGAIN ; goto out_put_req; } -aio_iopoll_iocb_issued (req); +aio_iopoll_iocb_issued (state, req); }return 0 ; out_put_req:
@@ -2259,14 +2308,51 @@ static int __io_submit_one(struct kioctx *ctx, const struct iocb *iocb, }static int io_submit_one (struct kioctx *ctx,struct iocb __user *user_iocb, -bool compat) +struct aio_submit_state *state,bool compat) {struct iocb iocb;if (unlikely (copy_from_user (&iocb, user_iocb,sizeof (iocb))))return -EFAULT ; -return __io_submit_one (ctx, &iocb, user_iocb, compat); +return __io_submit_one (ctx, &iocb, user_iocb, state, compat); +} + +#ifdef CONFIG_BLOCK +static void aio_state_unplug (struct blk_plug_cb *cb,bool from_schedule) +{ +struct aio_submit_state *state; + + state = container_of(cb, struct aio_submit_state, plug_cb); +if (!list_empty (&state->req_list)) +aio_flush_state_reqs (state->ctx, state); +} +#endif + +/* +* Batched submission is done, ensure local IO is flushed out. +*/ +static void aio_submit_state_end (struct aio_submit_state *state) +{ +blk_finish_plug (&state->plug); +if (!list_empty (&state->req_list)) +aio_flush_state_reqs (state->ctx, state); +} + +/* +* Start submission side cache. +*/ +static void aio_submit_state_start (struct aio_submit_state *state, +struct kioctx *ctx) +{ + state->ctx = ctx; +INIT_LIST_HEAD (&state->req_list); + state->req_count =0 ; +#ifdef CONFIG_BLOCK + state->plug_cb.callback = aio_state_unplug; +blk_start_plug (&state->plug); +list_add (&state->plug_cb.list, &state->plug.cb_list); +#endif }/* sys_io_submit:
@@ -2284,10 +2370,10 @@ static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,SYSCALL_DEFINE3 (io_submit, aio_context_t, ctx_id, long, nr, struct iocb __user * __user *, iocbpp) { +struct aio_submit_state state, *statep =NULL ;struct kioctx *ctx;long ret =0 ;int i =0 ; -struct blk_plug plug;if (unlikely (nr <0 ))return -EINVAL ;
@@ -2301,8 +2387,10 @@ SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,if (nr > ctx->nr_events) nr = ctx->nr_events; -if (nr >AIO_PLUG_THRESHOLD ) -blk_start_plug (&plug); +if (nr >AIO_PLUG_THRESHOLD ) { +aio_submit_state_start (&state, ctx); + statep = &state; + }for (i =0 ; i < nr; i++) {struct iocb __user *user_iocb;
@@ -2311,12 +2399,12 @@ SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,break ; } - ret =io_submit_one (ctx, user_iocb, false); + ret =io_submit_one (ctx, user_iocb, statep, false);if (ret)break ; } -if (nr >AIO_PLUG_THRESHOLD ) -blk_finish_plug (&plug); +if (statep) +aio_submit_state_end (statep);percpu_ref_put (&ctx->users);return i ? i : ret;
@@ -2326,10 +2414,10 @@ SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,COMPAT_SYSCALL_DEFINE3 (io_submit, compat_aio_context_t, ctx_id, int, nr, compat_uptr_t __user *, iocbpp) { +struct aio_submit_state state, *statep =NULL ;struct kioctx *ctx;long ret =0 ;int i =0 ; -struct blk_plug plug;if (unlikely (nr <0 ))return -EINVAL ;
@@ -2343,8 +2431,10 @@ COMPAT_SYSCALL_DEFINE3(io_submit, compat_aio_context_t, ctx_id,if (nr > ctx->nr_events) nr = ctx->nr_events; -if (nr >AIO_PLUG_THRESHOLD ) -blk_start_plug (&plug); +if (nr >AIO_PLUG_THRESHOLD ) { +aio_submit_state_start (&state, ctx); + statep = &state; + }for (i =0 ; i < nr; i++) {compat_uptr_t user_iocb;
@@ -2353,12 +2443,12 @@ COMPAT_SYSCALL_DEFINE3(io_submit, compat_aio_context_t, ctx_id,break ; } - ret =io_submit_one (ctx,compat_ptr (user_iocb), true); + ret =io_submit_one (ctx,compat_ptr (user_iocb), statep, true);if (ret)break ; } -if (nr >AIO_PLUG_THRESHOLD ) -blk_finish_plug (&plug); +if (statep) +aio_submit_state_end (statep);percpu_ref_put (&ctx->users);return i ? i : ret;
Signature · 1 line
2.17.1
Re: [PATCH 08/22] aio: add submission side request cache
Quoted 5 lines · Jens Axboe
We have to add each submitted polled request to the
io_contextpoll_submittedlist, which means we have to grab thepoll_lock. We already use the block plug to batch submissions if we're doing a batch of IO submissions, extend that to cover the poll requests internally as well.
The way we make this conditional looks rather odd. What about something like the untested patch below to clean this up a bit? I also think that we don't need the explicit aio_flush_state_reqs call in aio_submit_state_end given that blk_finish_plug already calls the supplied callback.
index d22913b7de74..ebf5d787d03b 100644
fs/aio.c +53 -54
@@ -235,18 +235,18 @@ struct aio_kiocb { };struct aio_submit_state { +#ifdef CONFIG_BLOCK struct kioctx *ctx;struct blk_plug plug; -#ifdef CONFIG_BLOCK struct blk_plug_cb plug_cb; -#endif /* * Polled iocbs that have been submitted, but not added to the ctx yet */ struct list_head req_list;unsigned int req_count; +#endif };/*------ sysctl variables----*/
@@ -263,15 +263,6 @@ static struct vfsmount *aio_mnt;static const struct file_operations aio_ring_fops;static const struct address_space_operations aio_ctx_aops; -/* -* We rely on block level unplugs to flush pending requests, if we schedule -*/ -#ifdef CONFIG_BLOCK -static const bool aio_use_state_req_list = true; -#else -static const bool aio_use_state_req_list = false; -#endif -static void aio_iopoll_reap_events (struct kioctx *);static struct file *aio_private_file (struct kioctx *ctx,loff_t nr_pages)
@@ -1849,52 +1840,55 @@ static inline void aio_rw_done(struct kiocb *req, ssize_t ret) } } +#ifdef CONFIG_BLOCK /* -* Called either at the end of IO submission, or through a plug callback -* because we're going to schedule. Moves out local batch of requests to -* the ctx poll list, so they can be found for polling + reaping. +* Called either at the end of IO submission, or through a plug callback because +* we're going to schedule. Moves out local batch of requests to the ctx poll +* list, so they can be found for polling + reaping. */ -static void aio_flush_state_reqs (struct kioctx *ctx, -struct aio_submit_state *state) +static void aio_flush_state_reqs (struct aio_submit_state *state) { -spin_lock (&ctx->poll_lock); -list_splice_tail_init (&state->req_list, &ctx->poll_submitted); -spin_unlock (&ctx->poll_lock); +spin_lock (&state->ctx->poll_lock); +list_splice_tail_init (&state->req_list, &state->ctx->poll_submitted); +spin_unlock (&state->ctx->poll_lock); state->req_count =0 ; } +static void aio_iopoll_add_iocb_to_state (struct aio_submit_state *state, +struct aio_kiocb *kiocb) +{ +/* +* For fast devices, IO may have already completed. If it has, add it to +* the front so we find it first. We can't add to the poll_done list as +* that is used without locking from the completion side. +*/ +if (test_bit (KIOCB_F_POLL_COMPLETED , &kiocb->ki_flags)) +list_add (&kiocb->ki_list, &state->req_list); +else +list_add_tail (&kiocb->ki_list, &state->req_list); +if (++state->req_count >=AIO_IOPOLL_BATCH ) +aio_flush_state_reqs (state); +} +#else +#define aio_iopoll_add_iocb_to_state (state, kiocb) do { } while (0) +#endif +/* * After the iocb has been issued, it's safe to be found on the poll list. * Adding the kiocb to the list AFTER submission ensures that we don't * find it from a io_getevents() thread before the issuer is done accessing * the kiocb cookie. */ -static void aio_iopoll_iocb_issued (struct aio_submit_state *state, -struct aio_kiocb *kiocb) +static void aio_iopoll_add_iocb_to_list (struct aio_kiocb *kiocb) { -/* -* For fast devices, IO may have already completed. If it has, add -* it to the front so we find it first. We can't add to the poll_done -* list as that's unlocked from the completion side. -*/ -const int front =test_bit (KIOCB_F_POLL_COMPLETED , &kiocb->ki_flags);struct kioctx *ctx = kiocb->ki_ctx; -if (!state || !aio_use_state_req_list) { -spin_lock (&ctx->poll_lock); -if (front) -list_add (&kiocb->ki_list, &ctx->poll_submitted); -else -list_add_tail (&kiocb->ki_list, &ctx->poll_submitted); -spin_unlock (&ctx->poll_lock); - }else { -if (front) -list_add (&kiocb->ki_list, &state->req_list); -else -list_add_tail (&kiocb->ki_list, &state->req_list); -if (++state->req_count >=AIO_IOPOLL_BATCH ) -aio_flush_state_reqs (ctx, state); - } +spin_lock (&ctx->poll_lock); +if (test_bit (KIOCB_F_POLL_COMPLETED , &kiocb->ki_flags)) +list_add (&kiocb->ki_list, &ctx->poll_submitted); +else +list_add_tail (&kiocb->ki_list, &ctx->poll_submitted); +spin_unlock (&ctx->poll_lock); } static ssize_t aio_read(struct aio_kiocb *kiocb, const struct iocb *iocb,
@@ -2294,7 +2288,11 @@ static int __io_submit_one(struct kioctx *ctx, const struct iocb *iocb, ret = -EAGAIN ; goto out_put_req; } -aio_iopoll_iocb_issued (state, req); + +if (state) +aio_iopoll_add_iocb_to_state (state, req); +else +aio_iopoll_add_iocb_to_list (req); }return 0 ; out_put_req:
@@ -2325,9 +2323,8 @@ static void aio_state_unplug(struct blk_plug_cb *cb, bool from_schedule) state = container_of(cb, struct aio_submit_state, plug_cb);if (!list_empty (&state->req_list)) - aio_flush_state_reqs(state->ctx, state); +aio_flush_state_reqs (state); } -#endif /* * Batched submission is done, ensure local IO is flushed out.
@@ -2336,24 +2333,27 @@ static void aio_submit_state_end(struct aio_submit_state *state) {blk_finish_plug (&state->plug);if (!list_empty (&state->req_list)) -aio_flush_state_reqs (state->ctx, state); +aio_flush_state_reqs (state); }/* * Start submission side cache. */ -static void aio_submit_state_start (struct aio_submit_state *state, -struct kioctx *ctx) +static struct aio_submit_state * +aio_submit_state_start (struct aio_submit_state *state,struct kioctx *ctx) { state->ctx = ctx;INIT_LIST_HEAD (&state->req_list); state->req_count =0 ; -#ifdef CONFIG_BLOCK state->plug_cb.callback = aio_state_unplug;blk_start_plug (&state->plug);list_add (&state->plug_cb.list, &state->plug.cb_list); -#endif +return state; } +#else +#define aio_submit_state_end (state) do { } while (0) +#define aio_submit_state_start (state, ctx) (state) +#endif /* CONFIG_BLOCK */ /* sys_io_submit: * Queue the nr iocbs pointed to by iocbpp for processing. Returns
@@ -2387,10 +2387,9 @@ SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,if (nr > ctx->nr_events) nr = ctx->nr_events; -if (nr >AIO_PLUG_THRESHOLD ) { -aio_submit_state_start (&state, ctx); - statep = &state; - } +if (IS_ENABLED (CONFIG_BLOCK ) && nr >AIO_PLUG_THRESHOLD ) + statep =aio_submit_state_start (&state, ctx); +for (i =0 ; i < nr; i++) {struct iocb __user *user_iocb;
Jens Axboe
maintainerRe: [PATCH 08/22] aio: add submission side request cache
Quoted 11 lines · Christoph Hellwig
Quoted 5 lines · Jens Axboe
We have to add each submitted polled request to the
io_contextpoll_submittedlist, which means we have to grab thepoll_lock. We already use the block plug to batch submissions if we're doing a batch of IO submissions, extend that to cover the poll requests internally as well.The way we make this conditional looks rather odd. What about something like the untested patch below to clean this up a bit? I also think that we don't need the explicit
aio_flush_state_reqscall inaio_submit_state_endgiven thatblk_finish_plugalready calls the supplied callback.
I folded in most of this and fixed a few missed cases, and made it work with !CONFIG_BLOCK as well.
I do think the flush is needed, if we did block and flushed the plug earier.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerRe: [PATCH 08/22] aio: add submission side request cache
Quoted 8 lines · Jens Axboe
Quoted 7 lines · Christoph Hellwig
Quoted 5 lines · Jens Axboe
We have to add each submitted polled request to the
io_contextpoll_submittedlist, which means we have to grab thepoll_lock. We already use the block plug to batch submissions if we're doing a batch of IO submissions, extend that to cover the poll requests internally as well.
Quoted 7 lines
Quoted 4 lines
The way we make this conditional looks rather odd. What about something like the untested patch below to clean this up a bit? I also think that we don't need the explicit
aio_flush_state_reqscall inaio_submit_state_endgiven thatblk_finish_plugalready calls the supplied callback.I folded in most of this and fixed a few missed cases, and made it work with !CONFIG_BLOCK as well.
Actually, looking closer, it needs to be redone. It doesn't take into account that we'll be using the submission state for file caching too, for instance. So we can't just hide it behind !CONFIG_BLOCK.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerPATCH 9/22 fs: add fget_many() and fput_many()
In reply to Jens AxboeSome uses cases repeatedly get and put references to the same file, but the only exposed interface is doing these one at the time. As each of these entail an atomic inc or dec on a shared structure, that cost can add up.
Add fget_many(), which works just like fget(), except it takes an argument for how many references to get on the file. Ditto fput_many(), which can drop an arbitrary number of references to a file.
--- fs/file.c | 15 ++++++++++----- fs/file_table.c | 9 +++++++-- include/linux/file.h | 2 ++ include/linux/fs.h | 4 +++- 4 files changed, 22 insertions(+), 8 deletions(-)
fs/file.c +10 -5
@@ -676,7 +676,7 @@ void do_close_on_exec(struct files_struct *files)spin_unlock (&files->file_lock); } -static struct file *__fget (unsigned int fd,fmode_t mask) +static struct file *__fget (unsigned int fd,fmode_t mask,unsigned int refs) {struct files_struct *files = current->files;struct file *file;
@@ -691,7 +691,7 @@ static struct file *__fget(unsigned int fd, fmode_t mask)*/ if (file->f_mode & mask) file =NULL ; -else if (!get_file_rcu (file)) +else if (!get_file_rcu_many (file, refs)) goto loop; }rcu_read_unlock ();
@@ -699,15 +699,20 @@ static struct file *__fget(unsigned int fd, fmode_t mask)return file; } +struct file *fget_many (unsigned int fd,unsigned int refs) +{ +return __fget (fd,FMODE_PATH , refs); +} +struct file *fget (unsigned int fd) { -return __fget (fd,FMODE_PATH ); +return fget_many (fd,1 ); }EXPORT_SYMBOL (fget);struct file *fget_raw (unsigned int fd) { -return __fget (fd,0 ); +return __fget (fd,0 ,1 ); }EXPORT_SYMBOL (fget_raw);
@@ -738,7 +743,7 @@ static unsigned long __fget_light(unsigned int fd, fmode_t mask)return 0 ;return (unsigned long )file; }else { - file =__fget (fd, mask); + file =__fget (fd, mask,1 );if (!file)return 0 ;return FDPUT_FPUT | (unsigned long )file;
fs/file_table.c +7 -2
@@ -326,9 +326,9 @@ void flush_delayed_fput(void) static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput); -void fput (struct file *file) +void fput_many (struct file *file,unsigned int refs) { -if (atomic_long_dec_and_test (&file->f_count)) { +if (atomic_long_sub_and_test (refs, &file->f_count)) {struct task_struct *task = current;if (likely (!in_interrupt () && !(task->flags &PF_KTHREAD ))) {
@@ -347,6 +347,11 @@ void fput(struct file *file) } } +void fput (struct file *file) +{ +fput_many (file,1 ); +} +/* * synchronous analog of fput(); for kernel threads that might be needed * in some umount() (and thus can't use flush_delayed_fput() without
include/linux/file.h +2 -0
@@ -13,6 +13,7 @@struct file ;extern void fput (struct file *); +extern void fput_many (struct file *,unsigned int );struct file_operations ;struct vfsmount ;
@@ -44,6 +45,7 @@ static inline void fdput(struct fd fd) }extern struct file *fget (unsigned int fd); +extern struct file *fget_many (unsigned int fd,unsigned int refs);extern struct file *fget_raw (unsigned int fd);extern unsigned long __fdget (unsigned int fd);extern unsigned long __fdget_raw (unsigned int fd);
include/linux/fs.h +3 -1
@@ -952,7 +952,9 @@ static inline struct file *get_file(struct file *f)atomic_long_inc (&f->f_count);return f; } -#define get_file_rcu (x) atomic_long_inc_not_zero(&(x)->f_count) +#define get_file_rcu_many (x, cnt) \ + atomic_long_add_unless(&(x)->f_count, (cnt), 0) +#define get_file_rcu (x) get_file_rcu_many((x), 1)#define fput_atomic (x) atomic_long_add_unless(&(x)->f_count, -1, 1)#define file_count (x) atomic_long_read(&(x)->f_count)
Signature · 1 line
2.17.1
Jens Axboe
maintainerPATCH 10/22 aio: use fget/fput_many() for file references
In reply to Jens AxboeOn the submission side, add file reference batching to the aio_submit_state. We get as many references as the number of iocbs we are submitting, and drop unused ones if we end up switching files. The assumption here is that we're usually only dealing with one fd, and if there are multiple, hopefuly they are at least somewhat ordered. Could trivially be extended to cover multiple fds, if needed.
On the completion side we do the same thing, except this is trivially done just locally in aio_iopoll_reap().
--- fs/aio.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 94 insertions(+), 16 deletions(-)
fs/aio.c +94 -16
@@ -247,6 +247,15 @@ struct aio_submit_state {*/ struct list_head req_list;unsigned int req_count; + +/* +* File reference cache +*/ +struct file *file; +unsigned int fd; +unsigned int has_refs; +unsigned int used_refs; +unsigned int ios_left; };/*------ sysctl variables----*/
@@ -1355,7 +1364,8 @@ static long aio_iopoll_reap(struct kioctx *ctx, struct io_event __user *evs, {void *iocbs[AIO_IOPOLL_BATCH ];struct aio_kiocb *iocb, *n; -int to_free =0 , ret =0 ; +int file_count, to_free =0 , ret =0 ; +struct file *file =NULL ;/* Shouldn't happen... */ if (*nr_events >= max)
@@ -1372,7 +1382,20 @@ static long aio_iopoll_reap(struct kioctx *ctx, struct io_event __user *evs,list_del (&iocb->ki_list); iocbs[to_free++] = iocb; -fput (iocb->rw.ki_filp); +/* +* Batched puts of the same file, to avoid dirtying the +* file usage count multiple times, if avoidable. +*/ +if (!file) { + file = iocb->rw.ki_filp; + file_count =1 ; + }else if (file == iocb->rw.ki_filp) { + file_count++; + }else { +fput_many (file, file_count); + file = iocb->rw.ki_filp; + file_count =1 ; + }if (evs &©_to_user (evs + *nr_events, &iocb->ki_ev,sizeof (iocb->ki_ev))) {
@@ -1382,6 +1405,9 @@ static long aio_iopoll_reap(struct kioctx *ctx, struct io_event __user *evs, (*nr_events)++; } +if (file) +fput_many (file, file_count); +if (to_free)iocb_put_many (ctx, iocbs, &to_free);
@@ -1742,13 +1768,60 @@ static void aio_complete_rw_poll(struct kiocb *kiocb, long res, long res2) } } -static int aio_prep_rw (struct aio_kiocb *kiocb,const struct iocb *iocb) +static void aio_file_put (struct aio_submit_state *state,struct file *file) +{ +if (!state) { +fput (file); + }else if (state->file) { +int diff = state->has_refs - state->used_refs; + +if (diff) +fput_many (state->file, diff); + state->file =NULL ; + } +} + +/* +* Get as many references to a file as we have IOs left in this submission, +* assuming most submissions are for one file, or at least that each file +* has more than one submission. +*/ +static struct file *aio_file_get (struct aio_submit_state *state,int fd) +{ +if (!state) +return fget (fd); + +if (!state->file) { +get_file: + state->file =fget_many (fd, state->ios_left); +if (!state->file) +return NULL ; + + state->fd = fd; + state->has_refs = state->ios_left; + state->used_refs =1 ; + state->ios_left--; +return state->file; + } + +if (state->fd == fd) { + state->used_refs++; + state->ios_left--; +return state->file; + } + +aio_file_put (state,NULL ); + goto get_file; +} + +static int aio_prep_rw (struct aio_kiocb *kiocb,const struct iocb *iocb, +struct aio_submit_state *state) {struct kioctx *ctx = kiocb->ki_ctx;struct kiocb *req = &kiocb->rw;int ret; - req->ki_filp =fget (iocb->aio_fildes); + req->ki_filp =aio_file_get (state, iocb->aio_fildes);if (unlikely (!req->ki_filp))return -EBADF ; req->ki_pos = iocb->aio_offset;
@@ -1806,7 +1879,7 @@ static int aio_prep_rw(struct aio_kiocb *kiocb, const struct iocb *iocb)return 0 ; out_fput: -fput (req->ki_filp); +aio_file_put (state, req->ki_filp);return ret; }
@@ -1898,7 +1971,8 @@ static void aio_iopoll_iocb_issued(struct aio_submit_state *state, }static ssize_t aio_read (struct aio_kiocb *kiocb,const struct iocb *iocb, -bool vectored,bool compat) +struct aio_submit_state *state,bool vectored, +bool compat) {struct iovec inline_vecs[UIO_FASTIOV ], *iovec = inline_vecs;struct kiocb *req = &kiocb->rw;
@@ -1906,7 +1980,7 @@ static ssize_t aio_read(struct aio_kiocb *kiocb, const struct iocb *iocb,struct file *file;ssize_t ret; - ret =aio_prep_rw (kiocb, iocb); + ret =aio_prep_rw (kiocb, iocb, state);if (ret)return ret; file = req->ki_filp;
@@ -1932,7 +2006,8 @@ static ssize_t aio_read(struct aio_kiocb *kiocb, const struct iocb *iocb, }static ssize_t aio_write (struct aio_kiocb *kiocb,const struct iocb *iocb, -bool vectored,bool compat) +struct aio_submit_state *state,bool vectored, +bool compat) {struct iovec inline_vecs[UIO_FASTIOV ], *iovec = inline_vecs;struct kiocb *req = &kiocb->rw;
@@ -1940,7 +2015,7 @@ static ssize_t aio_write(struct aio_kiocb *kiocb, const struct iocb *iocb,struct file *file;ssize_t ret; - ret =aio_prep_rw (kiocb, iocb); + ret =aio_prep_rw (kiocb, iocb, state);if (ret)return ret; file = req->ki_filp;
@@ -2250,16 +2325,16 @@ static int __io_submit_one(struct kioctx *ctx, const struct iocb *iocb, ret = -EINVAL ;switch (iocb->aio_lio_opcode) {case IOCB_CMD_PREAD : - ret =aio_read (req, iocb, false, compat); + ret =aio_read (req, iocb, state, false, compat);break ;case IOCB_CMD_PWRITE : - ret =aio_write (req, iocb, false, compat); + ret =aio_write (req, iocb, state, false, compat);break ;case IOCB_CMD_PREADV : - ret =aio_read (req, iocb, true, compat); + ret =aio_read (req, iocb, state, true, compat);break ;case IOCB_CMD_PWRITEV : - ret =aio_write (req, iocb, true, compat); + ret =aio_write (req, iocb, state, true, compat);break ;case IOCB_CMD_FSYNC :if (ctx->flags &IOCTX_FLAG_IOPOLL )
@@ -2337,17 +2412,20 @@ static void aio_submit_state_end(struct aio_submit_state *state)blk_finish_plug (&state->plug);if (!list_empty (&state->req_list))aio_flush_state_reqs (state->ctx, state); +aio_file_put (state,NULL ); }/* * Start submission side cache. */ static void aio_submit_state_start (struct aio_submit_state *state, -struct kioctx *ctx) +struct kioctx *ctx,int max_ios) { state->ctx = ctx;INIT_LIST_HEAD (&state->req_list); state->req_count =0 ; + state->file =NULL ; + state->ios_left = max_ios;#ifdef CONFIG_BLOCK state->plug_cb.callback = aio_state_unplug;blk_start_plug (&state->plug);
@@ -2388,7 +2466,7 @@ SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr, nr = ctx->nr_events;if (nr >AIO_PLUG_THRESHOLD ) { -aio_submit_state_start (&state, ctx); +aio_submit_state_start (&state, ctx, nr); statep = &state; }for (i =0 ; i < nr; i++) {
@@ -2432,7 +2510,7 @@ COMPAT_SYSCALL_DEFINE3(io_submit, compat_aio_context_t, ctx_id, nr = ctx->nr_events;if (nr >AIO_PLUG_THRESHOLD ) { -aio_submit_state_start (&state, ctx); +aio_submit_state_start (&state, ctx, nr); statep = &state; }for (i =0 ; i < nr; i++) {
Signature · 1 line
2.17.1
Jens Axboe
maintainerPATCH 11/22 aio: split iocb init from allocation
In reply to Jens AxboeIn preparation from having pre-allocated requests, that we then just need to initialize before use.
--- fs/aio.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)
fs/aio.c +11 -6
@@ -1098,6 +1098,16 @@ static bool get_reqs_available(struct kioctx *ctx)return __get_reqs_available (ctx); } +static void aio_iocb_init (struct kioctx *ctx,struct aio_kiocb *req) +{ +percpu_ref_get (&ctx->reqs); + req->ki_ctx = ctx; +INIT_LIST_HEAD (&req->ki_list); + req->ki_flags =0 ; +refcount_set (&req->ki_refcnt,0 ); + req->ki_eventfd =NULL ; +} +/* aio_get_req * Allocate a slot for an aio request. * Returns NULL if no requests are free.
@@ -1110,12 +1120,7 @@ static inline struct aio_kiocb *aio_get_req(struct kioctx *ctx)if (unlikely (!req))return NULL ; -percpu_ref_get (&ctx->reqs); - req->ki_ctx = ctx; -INIT_LIST_HEAD (&req->ki_list); - req->ki_flags =0 ; -refcount_set (&req->ki_refcnt,0 ); - req->ki_eventfd =NULL ; +aio_iocb_init (ctx, req);return req; }
Signature · 1 line
2.17.1
Jens Axboe
maintainerPATCH 12/22 aio: batch aio_kiocb allocation
In reply to Jens AxboeSimilarly to how we use the state->ios_left to know how many references to get to a file, we can use it to allocate the aio_kiocb's we need in bulk.
--- fs/aio.c | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-)
fs/aio.c +38 -8
@@ -234,6 +234,8 @@ struct aio_kiocb { }; }; +#define AIO_IOPOLL_BATCH 8 +struct aio_submit_state {struct kioctx *ctx;
@@ -248,6 +250,13 @@ struct aio_submit_state {struct list_head req_list;unsigned int req_count; +/* +* aio_kiocb alloc cache +*/ +void *iocbs[AIO_IOPOLL_BATCH ]; +unsigned int free_iocbs; +unsigned int cur_iocb; +/* * File reference cache */
@@ -1112,15 +1121,34 @@ static void aio_iocb_init(struct kioctx *ctx, struct aio_kiocb *req)* Allocate a slot for an aio request. * Returns NULL if no requests are free. */ -static inline struct aio_kiocb *aio_get_req (struct kioctx *ctx) +static struct aio_kiocb *aio_get_req (struct kioctx *ctx, +struct aio_submit_state *state) {struct aio_kiocb *req; - req =kmem_cache_alloc (kiocb_cachep,GFP_KERNEL ); -if (unlikely (!req)) -return NULL ; +if (!state) + req =kmem_cache_alloc (kiocb_cachep,GFP_KERNEL ); +else if (!state->free_iocbs) { +size_t size; +int ret; + + size =min_t (size_t, state->ios_left,ARRAY_SIZE (state->iocbs)); + ret =kmem_cache_alloc_bulk (kiocb_cachep,GFP_KERNEL , size, + state->iocbs); +if (ret <=0 ) +return ERR_PTR (-ENOMEM ); + state->free_iocbs = ret -1 ; + state->cur_iocb =1 ; + req = state->iocbs[0 ]; + }else { + req = state->iocbs[state->cur_iocb]; + state->free_iocbs--; + state->cur_iocb++; + } + +if (req) +aio_iocb_init (ctx, req); -aio_iocb_init (ctx, req);return req; }
@@ -1359,8 +1387,6 @@ static bool aio_read_events(struct kioctx *ctx, long min_nr, long nr,return ret <0 || *i >= min_nr; } -#define AIO_IOPOLL_BATCH 8 -/* * Process completed iocb iopoll entries, copying the result to userspace. */
@@ -2297,7 +2323,7 @@ static int __io_submit_one(struct kioctx *ctx, const struct iocb *iocb,return -EAGAIN ; ret = -EAGAIN ; - req =aio_get_req (ctx); + req =aio_get_req (ctx, state);if (unlikely (!req)) goto out_put_reqs_available;
@@ -2418,6 +2444,9 @@ static void aio_submit_state_end(struct aio_submit_state *state)if (!list_empty (&state->req_list))aio_flush_state_reqs (state->ctx, state);aio_file_put (state,NULL ); +if (state->free_iocbs) +kmem_cache_free_bulk (kiocb_cachep, state->free_iocbs, + &state->iocbs[state->cur_iocb]); }/*
@@ -2429,6 +2458,7 @@ static void aio_submit_state_start(struct aio_submit_state *state, state->ctx = ctx;INIT_LIST_HEAD (&state->req_list); state->req_count =0 ; + state->free_iocbs =0 ; state->file =NULL ; state->ios_left = max_ios;#ifdef CONFIG_BLOCK
Signature · 1 line
2.17.1
Jens Axboe
maintainerPATCH 13/22 aio: split old ring complete out from aio_complete()
In reply to Jens Axboe--- fs/aio.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
fs/aio.c +12 -5
@@ -1207,12 +1207,9 @@ static void aio_fill_event(struct io_event *ev, struct aio_kiocb *iocb, ev->res2 = res2; } -/* aio_complete -* Called when the io request on the given iocb is complete. -*/ -static void aio_complete (struct aio_kiocb *iocb,long res,long res2) +static void aio_ring_complete (struct kioctx *ctx,struct aio_kiocb *iocb, +long res,long res2) { -struct kioctx *ctx = iocb->ki_ctx;struct aio_ring *ring;struct io_event *ev_page, *event;unsigned tail, pos, head;
@@ -1262,6 +1259,16 @@ static void aio_complete(struct aio_kiocb *iocb, long res, long res2)spin_unlock_irqrestore (&ctx->completion_lock, flags);pr_debug ("added to ring %p at [%u]\n" , iocb, tail); +} + +/* aio_complete +* Called when the io request on the given iocb is complete. +*/ +static void aio_complete (struct aio_kiocb *iocb,long res,long res2) +{ +struct kioctx *ctx = iocb->ki_ctx; + +aio_ring_complete (ctx, iocb, res, res2);/* * Check if the user asked us to deliver the result through an
Signature · 1 line
2.17.1
Jens Axboe
maintainerPATCH 14/22 aio: pass in user index to __io_submit_one()
In reply to Jens AxboeThis is used for the user iocb pointer right now, but in preparation for having iocbs not reside in userspace, unionize with a ki_index and pass that in instead.
--- fs/aio.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
fs/aio.c +14 -6
@@ -207,7 +207,11 @@ struct aio_kiocb {struct kioctx *ki_ctx;kiocb_cancel_fn *ki_cancel; -struct iocb __user *ki_user_iocb;/* user's aiocb */ +union { +struct iocb __user *ki_user_iocb;/* user's aiocb */ +unsigned long ki_index; + }; +__u64 ki_user_data;/* user's data for completion */ struct list_head ki_list;/* the aio core uses this
@@ -1201,7 +1205,7 @@ static void iocb_put_many(struct kioctx *ctx, void **iocbs, int *nr)static void aio_fill_event (struct io_event *ev,struct aio_kiocb *iocb,long res,long res2) { - ev->obj = (u64 )(unsigned long )iocb->ki_user_iocb; + ev->obj = iocb->ki_index; ev->data = iocb->ki_user_data; ev->res = res; ev->res2 = res2;
@@ -2303,7 +2307,7 @@ static ssize_t aio_poll(struct aio_kiocb *aiocb, const struct iocb *iocb) }static int __io_submit_one (struct kioctx *ctx,const struct iocb *iocb, -struct iocb __user *user_iocb, +unsigned long ki_index,struct aio_submit_state *state,bool compat) {struct aio_kiocb *req;
@@ -2350,14 +2354,17 @@ static int __io_submit_one(struct kioctx *ctx, const struct iocb *iocb, }if (aio_ctx_supports_cancel (ctx)) { +struct iocb __user *user_iocb = (struct iocb __user *) ki_index; + ret =put_user (KIOCB_KEY , &user_iocb->aio_key);if (unlikely (ret)) {pr_debug ("EFAULT: aio_key\n" ); goto out_put_req; } - } + req->ki_user_iocb = user_iocb; + }else + req->ki_index = ki_index; - req->ki_user_iocb = user_iocb; req->ki_user_data = iocb->aio_data; ret = -EINVAL ;
@@ -2423,12 +2430,13 @@ static int __io_submit_one(struct kioctx *ctx, const struct iocb *iocb,static int io_submit_one (struct kioctx *ctx,struct iocb __user *user_iocb,struct aio_submit_state *state,bool compat) { +unsigned long ki_index = (unsigned long ) user_iocb;struct iocb iocb;if (unlikely (copy_from_user (&iocb, user_iocb,sizeof (iocb))))return -EFAULT ; -return __io_submit_one (ctx, &iocb, user_iocb, state, compat); +return __io_submit_one (ctx, &iocb, ki_index, state, compat); }#ifdef CONFIG_BLOCK
Signature · 1 line
2.17.1
Jens Axboe
maintainerPATCH 15/22 aio: add support for submission/completion rings
In reply to Jens AxboeExperimental support for submitting and completing IO through rings shared between the application and kernel.
The submission rings are struct iocb, like we would submit through io_submit(), and the completion rings are struct io_event, like we would pass in (and copy back) from io_getevents().
A new system call is added for this, io_ring_enter(). This system call submits IO that is queued in the SQ ring, and/or completes IO and stores the results in the CQ ring.
This could be augmented with a kernel thread that does the submission and polling, then the application would never have to enter the kernel to do IO.
Sample application: http://git.kernel.dk/cgit/fio/plain/t/aio-ring.c
--- arch/x86/entry/syscalls/syscall_64.tbl | 1 + fs/aio.c | 485 +++++++++++++++++++++++-- include/linux/syscalls.h | 4 +- include/uapi/linux/aio_abi.h | 29 ++ kernel/sys_ni.c | 1 + 5 files changed, 494 insertions(+), 26 deletions(-)
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
@@ -344,6 +344,7 @@ 333 common io_pgetevents __x64_sys_io_pgetevents 334 common rseq __x64_sys_rseq 335 common io_setup2 __x64_sys_io_setup2 +336 common io_ring_enter __x64_sys_io_ring_enter # # x32-specific system call numbers start at 512 to avoid cache impact
fs/aio.c +460 -25
624 lines not on this page · raw message
include/linux/syscalls.h +3 -1
@@ -287,8 +287,10 @@ static inline void addr_limit_user_check(void)*/ #ifndef CONFIG_ARCH_HAS_SYSCALL_WRAPPER asmlinkage long sys_io_setup (unsigned nr_reqs,aio_context_t __user *ctx); -asmlinkage long sys_io_setup2 (unsigned ,unsigned ,void __user *,void __user *, +asmlinkage long sys_io_setup2 (unsigned ,unsigned ,struct aio_sq_ring __user *, +struct aio_cq_ring __user *,aio_context_t __user *); +asmlinkage long sys_io_ring_enter (aio_context_t ,unsigned ,unsigned ,unsigned );asmlinkage long sys_io_destroy (aio_context_t ctx);asmlinkage long sys_io_submit (aio_context_t ,long ,struct iocb __user *__user *);
include/uapi/linux/aio_abi.h +29 -0
@@ -109,6 +109,35 @@ struct iocb { };/* 64 bytes */ #define IOCTX_FLAG_IOPOLL (1 << 0)/* io_context is polled */ +#define IOCTX_FLAG_SCQRING (1 << 1)/* Use SQ/CQ rings */ + +struct aio_sq_ring { +union { +struct { +u32 head;/* kernel consumer head */ +u32 tail;/* app producer tail */ +u32 nr_events;/* max events in ring */ +u64 iocbs;/* setup pointer to app iocbs */ + }; +u32 pad[16 ]; + }; +u32 array[0 ];/* actual ring, index to iocbs */ +}; + +struct aio_cq_ring { +union { +struct { +u32 head;/* app consumer head */ +u32 tail;/* kernel producer tail */ +u32 nr_events;/* max events in ring */ + }; +struct io_event pad; + }; +struct io_event events[0 ];/* ring, array of io_events */ +}; + +#define IORING_FLAG_SUBMIT (1 << 0) +#define IORING_FLAG_GETEVENTS (1 << 1)#undef IFBIG#undef IFLITTLE
kernel/sys_ni.c +1 -0
@@ -38,6 +38,7 @@ asmlinkage long sys_ni_syscall(void)COND_SYSCALL (io_setup);COND_SYSCALL (io_setup2); +COND_SYSCALL (io_ring_enter);COND_SYSCALL_COMPAT (io_setup);COND_SYSCALL (io_destroy);COND_SYSCALL (io_submit);
Signature · 1 line
2.17.1
Jens Axboe
maintainerPATCH 16/22 block: add BIO_HOLD_PAGES flag
In reply to Jens AxboeFor user mapped IO, we do get_user_pages() upfront, and then do a put_page() on each page at end_io time to release the page reference. In preparation for having permanently mapped pages, add a BIO_HOLD_PAGES flag that tells us not to release the pages, the caller will do that.
--- block/bio.c | 6 ++++-- include/linux/blk_types.h | 1 + 2 files changed, 5 insertions(+), 2 deletions(-)
block/bio.c +4 -2
@@ -1634,7 +1634,8 @@ static void bio_dirty_fn(struct work_struct *work) next = bio->bi_private;bio_set_pages_dirty (bio); -bio_release_pages (bio); +if (!bio_flagged (bio,BIO_HOLD_PAGES )) +bio_release_pages (bio);bio_put (bio); } }
@@ -1650,7 +1651,8 @@ void bio_check_pages_dirty(struct bio *bio) goto defer; } -bio_release_pages (bio); +if (!bio_flagged (bio,BIO_HOLD_PAGES )) +bio_release_pages (bio);bio_put (bio);return ; defer:
include/linux/blk_types.h +1 -0
@@ -215,6 +215,7 @@ struct bio {/* * bio flags */ +#define BIO_HOLD_PAGES 0/* don't put O_DIRECT pages */ #define BIO_SEG_VALID 1/* bi_phys_segments valid */ #define BIO_CLONED 2/* doesn't own data */ #define BIO_BOUNCED 3/* bio is a bounce bio */
Signature · 1 line
2.17.1
Re: [PATCH 16/22] block: add BIO_HOLD_PAGES flag
Quoted 6 lines · Jens Axboe
For user mapped IO, we do
get_user_pages()upfront, and then do aput_page()on each page atend_iotime to release the page reference. In preparation for having permanently mapped pages, add aBIO_HOLD_PAGESflag that tells us not to release the pages, the caller will do that.Signed-off-by Jens Axboe <[email protected]>
This looks sensible, but I'm not sure it makes sense on its own. The changes are a lot more obvious if merged into the next patch.
Jens Axboe
maintainerRe: [PATCH 16/22] block: add BIO_HOLD_PAGES flag
Quoted 10 lines · Christoph Hellwig
Quoted 6 lines · Jens Axboe
For user mapped IO, we do
get_user_pages()upfront, and then do aput_page()on each page atend_iotime to release the page reference. In preparation for having permanently mapped pages, add aBIO_HOLD_PAGESflag that tells us not to release the pages, the caller will do that.Signed-off-by Jens Axboe <[email protected]>This looks sensible, but I'm not sure it makes sense on its own. The changes are a lot more obvious if merged into the next patch.
I'll fold with the next one.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerPATCH 17/22 block: implement bio helper to add iter bvec pages to bio
In reply to Jens AxboeFor an ITER_BVEC, we can just iterate the iov and add the pages to the bio directly.
The current two callers of bio_iov_iter_get_pages() are updated to check if they need to release pages on completion. This makes them work with bvecs that contain kernel mapped pages already.
--- block/bio.c | 50 ++++++++++++++++++++++++++++++++++++++++++-------- fs/block_dev.c | 5 +++-- fs/iomap.c | 5 +++-- 3 files changed, 48 insertions(+), 12 deletions(-)
block/bio.c +42 -8
@@ -828,6 +828,20 @@ int bio_add_page(struct bio *bio, struct page *page, }EXPORT_SYMBOL (bio_add_page); +static int __bio_iov_bvec_add_pages (struct bio *bio,struct iov_iter *iter) +{ +const struct bio_vec *bv = iter->bvec + iter->iov_offset; +size_t size; + + size =bio_add_page (bio, bv->bv_page, bv->bv_len, bv->bv_offset); +if (size == bv->bv_len) { +iov_iter_advance (iter, size); +return 0 ; + } + +return -EINVAL ; +} +#define PAGE_PTRS_PER_BVEC (sizeof(struct bio_vec) / sizeof(struct page *))/**
@@ -876,23 +890,43 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter) }/** -* bio_iov_iter_get_pages - pin user or kernel pages and add them to a bio +* bio_iov_iter_get_pages - add user or kernel pages to a bio * @bio: bio to add pages to -* @iter: iov iterator describing the region to be mapped +* @iter: iov iterator describing the region to be added +* +* This takes either an iterator pointing to user memory, or one pointing to +* kernel pages (BVEC iterator). If we're adding user pages, we pin them and +* map them into the kernel. On IO completion, the caller should put those +* pages. If we're adding kernel pages, we just have to add the pages to the +* bio directly. We don't grab an extra reference to those pages (the user +* should already have that), and we don't put the page on IO completion. +* The caller needs to check if the bio is flagged BIO_HOLD_PAGES on IO +* completion. If it isn't, then pages should be released. * -* Pins pages from *iter and appends them to @bio's bvec array. The -* pages will have to be released using put_page() when done. * The function tries, but does not guarantee, to pin as many pages as -* fit into the bio, or are requested in *iter, whatever is smaller. -* If MM encounters an error pinning the requested pages, it stops. -* Error is returned only if 0 pages could be pinned. +* fit into the bio, or are requested in *iter, whatever is smaller. If +* MM encounters an error pinning the requested pages, it stops. Error +* is returned only if 0 pages could be pinned. */ int bio_iov_iter_get_pages (struct bio *bio,struct iov_iter *iter) { +const bool is_bvec =iov_iter_is_bvec (iter);unsigned short orig_vcnt = bio->bi_vcnt; +/* +* If this is a BVEC iter, then the pages are kernel pages. Don't +* release them on IO completion. +*/ +if (is_bvec) +bio_set_flag (bio,BIO_HOLD_PAGES ); + do { - int ret = __bio_iov_iter_get_pages(bio, iter); + int ret; + + if (is_bvec) + ret = __bio_iov_bvec_add_pages(bio, iter); + else + ret = __bio_iov_iter_get_pages(bio, iter); if (unlikely(ret)) return bio->bi_vcnt > orig_vcnt ? 0 : ret;
fs/block_dev.c +3 -2
@@ -326,8 +326,9 @@ static void blkdev_bio_end_io(struct bio *bio)struct bio_vec *bvec;int i; -bio_for_each_segment_all (bvec, bio, i) -put_page (bvec->bv_page); +if (!bio_flagged (bio,BIO_HOLD_PAGES )) +bio_for_each_segment_all (bvec, bio, i) +put_page (bvec->bv_page);bio_put (bio); } }
fs/iomap.c +3 -2
@@ -1580,8 +1580,9 @@ static void iomap_dio_bio_end_io(struct bio *bio)struct bio_vec *bvec;int i; -bio_for_each_segment_all (bvec, bio, i) -put_page (bvec->bv_page); +if (!bio_flagged (bio,BIO_HOLD_PAGES )) +bio_for_each_segment_all (bvec, bio, i) +put_page (bvec->bv_page);bio_put (bio); } }
Signature · 1 line
2.17.1
Re: [PATCH 17/22] block: implement bio helper to add iter bvec pages to bio
I don't think we need the loop for the ITER_BVEC case as bio_Add_page will only fail when the bio is full. Something like this would simplify the implementation:
block/bio.c +14 -25
@@ -831,18 +831,18 @@ EXPORT_SYMBOL(bio_add_page);static int __bio_iov_bvec_add_pages (struct bio *bio,struct iov_iter *iter) {const struct bio_vec *bv = iter->bvec; -unsigned int len; -size_t size; - - len =min_t (size_t, bv->bv_len, iter->count); - size =bio_add_page (bio, bv->bv_page, len, - bv->bv_offset + iter->iov_offset); -if (size == len) { -iov_iter_advance (iter, size); -return 0 ; - } +unsigned int len =min_t (size_t, bv->bv_len, iter->count); +unsigned int offset = bv->bv_offset + iter->iov_offset; -return -EINVAL ; +/* +* If this is a BVEC iter, then the pages are kernel pages. Don't +* release them on IO completion. +*/ +bio_set_flag (bio,BIO_HOLD_PAGES ); +if (bio_add_page (bio, bv->bv_page, len, offset) != len) +return -EINVAL ; +iov_iter_advance (iter, len); +return 0 ; }#define PAGE_PTRS_PER_BVEC (sizeof(struct bio_vec) / sizeof(struct page *))
@@ -913,24 +913,13 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)*/ int bio_iov_iter_get_pages (struct bio *bio,struct iov_iter *iter) { -const bool is_bvec =iov_iter_is_bvec (iter);unsigned short orig_vcnt = bio->bi_vcnt; -/* -* If this is a BVEC iter, then the pages are kernel pages. Don't -* release them on IO completion. -*/ -if (is_bvec) -bio_set_flag (bio,BIO_HOLD_PAGES ); +if (iov_iter_is_bvec (iter)) +return __bio_iov_bvec_add_pages (bio, iter); do { - int ret; - - if (is_bvec) - ret = __bio_iov_bvec_add_pages(bio, iter); - else - ret = __bio_iov_iter_get_pages(bio, iter); - + int ret = __bio_iov_iter_get_pages(bio, iter); if (unlikely(ret)) return bio->bi_vcnt > orig_vcnt ? 0 : ret;
Jens Axboe
maintainerRe: [PATCH 17/22] block: implement bio helper to add iter bvec pages to bio
Quoted 3 lines · Christoph Hellwig
I don't think we need the loop for the
ITER_BVECcase asbio_Add_pagewill only fail when the bio is full. Something like this would simplify the implementation:
Then how would multi bvec iters work? With this change, we'd do one page at the time. The bvec iter can be much bigger than that.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerPATCH 18/22 aio: add support for pre-mapped user IO buffers
In reply to Jens AxboeIf we have fixed user buffers, we can map them into the kernel when we setup the io_context. That avoids the need to do get_user_pages() for each and every IO.
To utilize this feature, the application must set both IOCTX_FLAG_USERIOCB, to provide iocb's in userspace, and then IOCTX_FLAG_FIXEDBUFS. The latter tells aio that the iocbs that are mapped already contain valid destination and sizes. These buffers can then be mapped into the kernel for the life time of the io_context, as opposed to just the duration of the each single IO.
Only works with non-vectored read/write commands for now, not with PREADV/PWRITEV.
A limit of 4M is imposed as the largest buffer we currently support. There's nothing preventing us from going larger, but we need some cap, and 4M seemed like it would definitely be big enough. RLIMIT_MEMLOCK is used to cap the total amount of memory pinned.
--- fs/aio.c | 203 +++++++++++++++++++++++++++++++---- include/uapi/linux/aio_abi.h | 1 + 2 files changed, 186 insertions(+), 18 deletions(-)
fs/aio.c +185 -18
@@ -42,6 +42,8 @@#include <linux/ramfs.h> #include <linux/percpu-refcount.h> #include <linux/mount.h> +#include <linux/sizes.h> +#include <linux/nospec.h> #include <asm/kmap_types.h> #include <linux/uaccess.h>
@@ -105,6 +107,12 @@ struct aio_iocb_ring {struct aio_mapped_range iocb_range;/* maps user iocbs */ }; +struct aio_mapped_ubuf { +u64 ubuf; +struct bio_vec *bvec; +unsigned int nr_bvecs; +}; +struct kioctx {struct percpu_ref users;atomic_t dead;
@@ -140,6 +148,9 @@ struct kioctx {struct page **ring_pages;long nr_pages; +/* if used, fixed mapped user buffers */ +struct aio_mapped_ubuf *user_bufs; +/* if used, completion and submission rings */ struct aio_iocb_ring sq_ring;struct aio_mapped_range cq_ring;
@@ -318,8 +329,10 @@ static const bool aio_use_state_req_list = true;static const bool aio_use_state_req_list = false;#endif +static void aio_iocb_buffer_unmap (struct kioctx *);static void aio_scqring_unmap (struct kioctx *);static void aio_iopoll_reap_events (struct kioctx *); +static const struct iocb *aio_iocb_from_index (struct kioctx *ctx,unsigned idx);static struct file *aio_private_file (struct kioctx *ctx,loff_t nr_pages) {
@@ -698,6 +711,7 @@ static void free_ioctx(struct work_struct *work)pr_debug ("freeing %p\n" , ctx);aio_scqring_unmap (ctx); +aio_iocb_buffer_unmap (ctx);aio_free_ring (ctx);free_percpu (ctx->cpu);percpu_ref_exit (&ctx->reqs);
@@ -1836,6 +1850,123 @@ static int aio_scqring_map(struct kioctx *ctx,return ret; } +static void aio_iocb_buffer_unmap (struct kioctx *ctx) +{ +int i, j; + +if (!ctx->user_bufs) +return ; + +for (i =0 ; i < ctx->max_reqs; i++) { +struct aio_mapped_ubuf *amu = &ctx->user_bufs[i]; + +for (j =0 ; j < amu->nr_bvecs; j++) +put_page (amu->bvec[j].bv_page); + +kfree (amu->bvec); + amu->nr_bvecs =0 ; + } + +kfree (ctx->user_bufs); + ctx->user_bufs =NULL ; +} + +static int aio_iocb_buffer_map (struct kioctx *ctx) +{ +unsigned long total_pages, page_limit; +struct page **pages =NULL ; +int i, j, got_pages =0 ; +const struct iocb *iocb; +int ret = -EINVAL ; + + ctx->user_bufs =kzalloc (ctx->max_reqs *sizeof (struct aio_mapped_ubuf ), +GFP_KERNEL ); +if (!ctx->user_bufs) +return -ENOMEM ; + +/* Don't allow more pages than we can safely lock */ + total_pages =0 ; + page_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; + +for (i =0 ; i < ctx->max_reqs; i++) { +struct aio_mapped_ubuf *amu = &ctx->user_bufs[i]; +unsigned long off, start, end, ubuf; +int pret, nr_pages; +size_t size; + + iocb =aio_iocb_from_index (ctx, i); + +/* +* Don't impose further limits on the size and buffer +* constraints here, we'll -EINVAL later when IO is +* submitted if they are wrong. +*/ + ret = -EFAULT ; +if (!iocb->aio_buf) + goto err; + +/* arbitrary limit, but we need something */ +if (iocb->aio_nbytes >SZ_4M ) + goto err; + + ubuf = iocb->aio_buf; + end = (ubuf + iocb->aio_nbytes +PAGE_SIZE -1 ) >>PAGE_SHIFT ; + start = ubuf >>PAGE_SHIFT ; + nr_pages = end - start; + + ret = -ENOMEM ; +if (total_pages + nr_pages > page_limit) + goto err; + +if (!pages || nr_pages > got_pages) { +kfree (pages); + pages =kmalloc (nr_pages *sizeof (struct page *), +GFP_KERNEL ); +if (!pages) + goto err; + got_pages = nr_pages; + } + + amu->bvec =kmalloc (nr_pages *sizeof (struct bio_vec ), +GFP_KERNEL ); +if (!amu->bvec) + goto err; + +down_write (¤t->mm->mmap_sem); + pret =get_user_pages (ubuf, nr_pages,1 , pages,NULL ); +up_write (¤t->mm->mmap_sem); + +if (pret < nr_pages) { +if (pret <0 ) + ret = pret; + goto err; + } + + off = ubuf & ~PAGE_MASK ; + size = iocb->aio_nbytes; +for (j =0 ; j < nr_pages; j++) { +size_t vec_len; + + vec_len =min_t (size_t, size,PAGE_SIZE - off); + amu->bvec[j].bv_page = pages[j]; + amu->bvec[j].bv_len = vec_len; + amu->bvec[j].bv_offset = off; + off =0 ; + size -= vec_len; + } +/* store original address for later verification */ + amu->ubuf = ubuf; + amu->nr_bvecs = nr_pages; + total_pages += nr_pages; + } +kfree (pages); +return 0 ; +err: +kfree (pages); +aio_iocb_buffer_unmap (ctx); +return ret; +} +/* sys_io_setup2: * Like sys_io_setup(), except that it takes a set of flags * (IOCTX_FLAG_*), and some pointers to user structures:
@@ -1853,7 +1984,8 @@ SYSCALL_DEFINE5(io_setup2, u32, nr_events, u32, flags,unsigned long ctx;long ret; -if (flags & ~(IOCTX_FLAG_IOPOLL |IOCTX_FLAG_SCQRING )) +if (flags & ~(IOCTX_FLAG_IOPOLL |IOCTX_FLAG_SCQRING | +IOCTX_FLAG_FIXEDBUFS ))return -EINVAL ; ret =get_user (ctx, ctxp);
@@ -1869,6 +2001,15 @@ SYSCALL_DEFINE5(io_setup2, u32, nr_events, u32, flags, ret =aio_scqring_map (ioctx, sq_ring, cq_ring);if (ret) goto err; +if (flags &IOCTX_FLAG_FIXEDBUFS ) { + ret =aio_iocb_buffer_map (ioctx); +if (ret) + goto err; + } + }else if (flags &IOCTX_FLAG_FIXEDBUFS ) { +/* can only support fixed bufs with SQ/CQ ring */ + ret = -EINVAL ; + goto err; } ret =put_user (ioctx->user_id, ctxp);
@@ -2148,23 +2289,44 @@ static int aio_prep_rw(struct aio_kiocb *kiocb, const struct iocb *iocb,return ret; } -static int aio_setup_rw (int rw,const struct iocb *iocb,struct iovec **iovec, -bool vectored,bool compat,struct iov_iter *iter) +static int aio_setup_rw (int rw,struct aio_kiocb *kiocb, +const struct iocb *iocb,struct iovec **iovec,bool vectored, +bool compat,bool kaddr,struct iov_iter *iter) { -void __user *buf = (void __user *)(uintptr_t )iocb->aio_buf; +void __user *ubuf = (void __user *)(uintptr_t )iocb->aio_buf;size_t len = iocb->aio_nbytes;if (!vectored) { -ssize_t ret =import_single_range (rw, buf, len, *iovec, iter); +ssize_t ret; + +if (!kaddr) { + ret =import_single_range (rw, ubuf, len, *iovec, iter); + }else { +struct kioctx *ctx = kiocb->ki_ctx; +struct aio_mapped_ubuf *amu; +int index; + +/* __io_submit_one() already validated the index */ + index =array_index_nospec (kiocb->ki_index, + ctx->max_reqs); + amu = &ctx->user_bufs[index]; +if (unlikely (amu->ubuf != iocb->aio_buf)) +return -EFAULT ; +iov_iter_bvec (iter, rw, amu->bvec, amu->nr_bvecs, len); + ret =0 ; + + } *iovec =NULL ;return ret; } +if (kaddr) +return -EINVAL ;#ifdef CONFIG_COMPAT if (compat) -return compat_import_iovec (rw, buf, len,UIO_FASTIOV , iovec, +return compat_import_iovec (rw, ubuf, len,UIO_FASTIOV , iovec, iter);#endif -return import_iovec (rw, buf, len,UIO_FASTIOV , iovec, iter); +return import_iovec (rw, ubuf, len,UIO_FASTIOV , iovec, iter); }static inline void aio_rw_done (struct kiocb *req,ssize_t ret)
@@ -2237,7 +2399,7 @@ static void aio_iopoll_iocb_issued(struct aio_submit_state *state,static ssize_t aio_read (struct aio_kiocb *kiocb,const struct iocb *iocb,struct aio_submit_state *state,bool vectored, -bool compat) +bool compat,bool kaddr) {struct iovec inline_vecs[UIO_FASTIOV ], *iovec = inline_vecs;struct kiocb *req = &kiocb->rw;
@@ -2257,9 +2419,11 @@ static ssize_t aio_read(struct aio_kiocb *kiocb, const struct iocb *iocb,if (unlikely (!file->f_op->read_iter)) goto out_fput; - ret =aio_setup_rw (READ , iocb, &iovec, vectored, compat, &iter); + ret =aio_setup_rw (READ , kiocb, iocb, &iovec, vectored, compat, kaddr, + &iter);if (ret) goto out_fput; + ret =rw_verify_area (READ , file, &req->ki_pos,iov_iter_count (&iter));if (!ret)aio_rw_done (req,call_read_iter (file, req, &iter));
@@ -2272,7 +2436,7 @@ static ssize_t aio_read(struct aio_kiocb *kiocb, const struct iocb *iocb,static ssize_t aio_write (struct aio_kiocb *kiocb,const struct iocb *iocb,struct aio_submit_state *state,bool vectored, -bool compat) +bool compat,bool kaddr) {struct iovec inline_vecs[UIO_FASTIOV ], *iovec = inline_vecs;struct kiocb *req = &kiocb->rw;
@@ -2292,7 +2456,8 @@ static ssize_t aio_write(struct aio_kiocb *kiocb, const struct iocb *iocb,if (unlikely (!file->f_op->write_iter)) goto out_fput; - ret =aio_setup_rw (WRITE , iocb, &iovec, vectored, compat, &iter); + ret =aio_setup_rw (WRITE , kiocb, iocb, &iovec, vectored, compat, kaddr, + &iter);if (ret) goto out_fput; ret =rw_verify_area (WRITE , file, &req->ki_pos,iov_iter_count (&iter));
@@ -2531,7 +2696,8 @@ static ssize_t aio_poll(struct aio_kiocb *aiocb, const struct iocb *iocb)static int __io_submit_one (struct kioctx *ctx,const struct iocb *iocb,unsigned long ki_index, -struct aio_submit_state *state,bool compat) +struct aio_submit_state *state,bool compat, +bool kaddr) {struct aio_kiocb *req;ssize_t ret;
@@ -2592,16 +2758,16 @@ static int __io_submit_one(struct kioctx *ctx, const struct iocb *iocb, ret = -EINVAL ;switch (iocb->aio_lio_opcode) {case IOCB_CMD_PREAD : - ret =aio_read (req, iocb, state, false, compat); + ret =aio_read (req, iocb, state, false, compat, kaddr);break ;case IOCB_CMD_PWRITE : - ret =aio_write (req, iocb, state, false, compat); + ret =aio_write (req, iocb, state, false, compat, kaddr);break ;case IOCB_CMD_PREADV : - ret =aio_read (req, iocb, state, true, compat); + ret =aio_read (req, iocb, state, true, compat, kaddr);break ;case IOCB_CMD_PWRITEV : - ret =aio_write (req, iocb, state, true, compat); + ret =aio_write (req, iocb, state, true, compat, kaddr);break ;case IOCB_CMD_FSYNC :if (ctx->flags &IOCTX_FLAG_IOPOLL )
@@ -2658,7 +2824,7 @@ static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,if (unlikely (copy_from_user (&iocb, user_iocb,sizeof (iocb))))return -EFAULT ; -return __io_submit_one (ctx, &iocb, ki_index, state, compat); +return __io_submit_one (ctx, &iocb, ki_index, state, compat, false); }#ifdef CONFIG_BLOCK
@@ -2761,6 +2927,7 @@ static const struct iocb *aio_peek_sqring(struct kioctx *ctx,static int aio_ring_submit (struct kioctx *ctx,unsigned int to_submit) { +bool kaddr = (ctx->flags &IOCTX_FLAG_FIXEDBUFS ) !=0 ;struct aio_submit_state state, *statep =NULL ;int i, ret =0 , submit =0 ;
@@ -2777,7 +2944,7 @@ static int aio_ring_submit(struct kioctx *ctx, unsigned int to_submit)if (!iocb)break ; - ret =__io_submit_one (ctx, iocb, iocb_index, statep, false); + ret =__io_submit_one (ctx, iocb, iocb_index, statep, false, kaddr);if (ret)break ;
include/uapi/linux/aio_abi.h +1 -0
@@ -110,6 +110,7 @@ struct iocb {#define IOCTX_FLAG_IOPOLL (1 << 0)/* io_context is polled */ #define IOCTX_FLAG_SCQRING (1 << 1)/* Use SQ/CQ rings */ +#define IOCTX_FLAG_FIXEDBUFS (1 << 2)/* IO buffers are fixed */ struct aio_sq_ring { union {
Signature · 1 line
2.17.1
Jens Axboe
maintainerPATCH 19/22 aio: support kernel side submission for aio with SCQRING
In reply to Jens AxboeAdd support for backing the io_context with either a thread, or a workqueue and letting those handle the submission for us. This can be used to reduce overhead for submission, or to always make submission async. The latter is particularly useful for buffered aio, which is now fully async with this feature.
For polled IO, we could have the kernel side thread hammer on the SQ ring and submit when it finds IO. This would mean that an application would NEVER have to enter the kernel to do IO! Didn't add this yet, but it would be trivial to add.
If an application sets IOCTX_FLAG_SCQTHREAD, the io_context gets a single thread backing. If used with buffered IO, this will limit the device queue depth to 1, but it will be async, IOs will simply be serialized.
Or an application can set IOCTX_FLAG_SQWQ, in which case the io_context gets a work queue backing. The concurrency level is the mininum of twice the available CPUs, or the queue depth specific for the context. For this mode, we attempt to do buffered reads inline, in case they are cached. So we should only punt to a workqueue, if we would have to block to get our data.
Tested with polling, no polling, fixedbufs, no fixedbufs, buffered, O_DIRECT.
See this sample application for how to use it:
http://git.kernel.dk/cgit/fio/plain/t/aio-ring.c
--- fs/aio.c | 438 ++++++++++++++++++++++++++++++++--- include/uapi/linux/aio_abi.h | 3 + 2 files changed, 414 insertions(+), 27 deletions(-)
fs/aio.c +411 -27
@@ -25,6 +25,7 @@#include <linux/sched/signal.h> #include <linux/fs.h> #include <linux/file.h> +#include <linux/fdtable.h> #include <linux/mm.h> #include <linux/mman.h> #include <linux/mmu_context.h>
@@ -44,6 +45,7 @@#include <linux/mount.h> #include <linux/sizes.h> #include <linux/nospec.h> +#include <linux/sched/mm.h> #include <asm/kmap_types.h> #include <linux/uaccess.h>
@@ -113,6 +115,14 @@ struct aio_mapped_ubuf {unsigned int nr_bvecs; }; +struct aio_sq_offload { +struct task_struct *thread;/* if using a thread */ +struct workqueue_struct *wq;/* wq offload */ +struct mm_struct *mm; +struct files_struct *files; +wait_queue_head_t wait; +}; +struct kioctx {struct percpu_ref users;atomic_t dead;
@@ -155,6 +165,10 @@ struct kioctx {struct aio_iocb_ring sq_ring;struct aio_mapped_range cq_ring;int cq_ring_overflow; +int submit_eagain; + +/* sq ring submitter thread, if used */ +struct aio_sq_offload sq_offload;struct rcu_work free_rwork;/* see free_ioctx() */
@@ -249,6 +263,7 @@ struct aio_kiocb {unsigned long ki_flags;#define KIOCB_F_POLL_COMPLETED 0/* polled IO has completed */ #define KIOCB_F_POLL_EAGAIN 1/* polled submission got EAGAIN */ +#define KIOCB_F_FORCE_NONBLOCK 2/* inline submission attempt */ refcount_t ki_refcnt;
@@ -1357,19 +1372,31 @@ static void aio_complete(struct aio_kiocb *iocb, long res, long res2)unsigned int tail;/* -* If we can't get a cq entry, userspace overflowed the -* submission (by quite a lot). Flag it as an overflow -* condition, and next io_ring_enter(2) call will return -* -EOVERFLOW. +* Catch EAGAIN early if we've forced a nonblock attempt, as +* we don't want to pass that back down to userspace through +* the CQ ring. Just mark the ctx as such, so the caller will +* see it and punt to workqueue. This is just for buffered +* aio reads. */ -spin_lock_irqsave (&ctx->completion_lock, flags); - ev =aio_peek_cqring (ctx, &tail); -if (ev) { -aio_fill_event (ev, iocb, res, res2); -aio_commit_cqring (ctx, tail); - }else - ctx->cq_ring_overflow =1 ; -spin_unlock_irqrestore (&ctx->completion_lock, flags); +if (res == -EAGAIN && +test_bit (KIOCB_F_FORCE_NONBLOCK , &iocb->ki_flags)) { + ctx->submit_eagain =1 ; + }else { +/* +* If we can't get a cq entry, userspace overflowed the +* submission (by quite a lot). Flag it as an overflow +* condition, and next io_ring_enter(2) call will return +* -EOVERFLOW. +*/ +spin_lock_irqsave (&ctx->completion_lock, flags); + ev =aio_peek_cqring (ctx, &tail); +if (ev) { +aio_fill_event (ev, iocb, res, res2); +aio_commit_cqring (ctx, tail); + }else + ctx->cq_ring_overflow =1 ; +spin_unlock_irqrestore (&ctx->completion_lock, flags); + } }else {aio_ring_complete (ctx, iocb, res, res2);
@@ -1735,6 +1762,63 @@ static long read_events(struct kioctx *ctx, long min_nr, long nr,return ret; } +static int aio_sq_thread (void *); + +static int aio_sq_thread_start (struct kioctx *ctx) +{ +struct aio_sq_ring *ring = ctx->sq_ring.ring; +struct aio_sq_offload *aso = &ctx->sq_offload; +int ret; + +memset (aso,0 ,sizeof (*aso)); +init_waitqueue_head (&aso->wait); + +if (!(ctx->flags &IOCTX_FLAG_FIXEDBUFS )) + aso->mm = current->mm; + + ret = -EBADF ; + aso->files =get_files_struct (current); +if (!aso->files) + goto err; + +if (ctx->flags &IOCTX_FLAG_SQTHREAD ) { +char name[32 ]; + +snprintf (name,sizeof (name),"aio-sq-%lu/%d" , ctx->user_id, + ring->sq_thread_cpu); + aso->thread =kthread_create_on_cpu (aio_sq_thread, ctx, + ring->sq_thread_cpu, name); +if (IS_ERR (aso->thread)) { + ret =PTR_ERR (aso->thread); + aso->thread =NULL ; + goto err; + } +wake_up_process (aso->thread); + }else if (ctx->flags &IOCTX_FLAG_SQWQ ) { +int concurrency; + +/* Do QD, or 2 * CPUS, whatever is smallest */ + concurrency =min (ring->nr_events -1 ,2 *num_online_cpus ()); + aso->wq =alloc_workqueue ("aio-sq-%lu" , +WQ_UNBOUND |WQ_FREEZABLE , + concurrency, ctx->user_id); +if (!aso->wq) { + ret = -ENOMEM ; + goto err; + } + } + +return 0 ; +err: +if (aso->files) { +put_files_struct (aso->files); + aso->files =NULL ; + } +if (aso->mm) + aso->mm =NULL ; +return ret; +} +static void aio_unmap_range (struct aio_mapped_range *range) {int i;
@@ -1780,6 +1864,20 @@ static int aio_map_range(struct aio_mapped_range *range, void __user *uaddr,static void aio_scqring_unmap (struct kioctx *ctx) { +struct aio_sq_offload *aso = &ctx->sq_offload; + +if (aso->thread) { +kthread_park (aso->thread); +kthread_stop (aso->thread); + aso->thread =NULL ; + }else if (aso->wq) { +destroy_workqueue (aso->wq); + aso->wq =NULL ; + } +if (aso->files) { +put_files_struct (aso->files); + aso->files =NULL ; + }aio_unmap_range (&ctx->sq_ring.ring_range);aio_unmap_range (&ctx->sq_ring.iocb_range);aio_unmap_range (&ctx->cq_ring);
@@ -1841,6 +1939,9 @@ static int aio_scqring_map(struct kioctx *ctx, kcq_ring->nr_events = cq_ring_size; kcq_ring->head = kcq_ring->tail =0 ; +if (ctx->flags & (IOCTX_FLAG_SQTHREAD |IOCTX_FLAG_SQWQ )) + ret =aio_sq_thread_start (ctx); + err:if (ret) {aio_unmap_range (&ctx->sq_ring.ring_range);
@@ -1985,7 +2086,8 @@ SYSCALL_DEFINE5(io_setup2, u32, nr_events, u32, flags,long ret;if (flags & ~(IOCTX_FLAG_IOPOLL |IOCTX_FLAG_SCQRING | -IOCTX_FLAG_FIXEDBUFS )) +IOCTX_FLAG_FIXEDBUFS |IOCTX_FLAG_SQTHREAD | +IOCTX_FLAG_SQWQ ))return -EINVAL ; ret =get_user (ctx, ctxp);
@@ -2006,8 +2108,9 @@ SYSCALL_DEFINE5(io_setup2, u32, nr_events, u32, flags,if (ret) goto err; } - }else if (flags &IOCTX_FLAG_FIXEDBUFS ) { -/* can only support fixed bufs with SQ/CQ ring */ + }else if (flags & (IOCTX_FLAG_FIXEDBUFS |IOCTX_FLAG_SQTHREAD | +IOCTX_FLAG_SQWQ )) { +/* These features only supported with SCQRING */ ret = -EINVAL ; goto err; }
@@ -2221,7 +2324,7 @@ static struct file *aio_file_get(struct aio_submit_state *state, int fd) }static int aio_prep_rw (struct aio_kiocb *kiocb,const struct iocb *iocb, -struct aio_submit_state *state) +struct aio_submit_state *state,bool force_nonblock) {struct kioctx *ctx = kiocb->ki_ctx;struct kiocb *req = &kiocb->rw;
@@ -2254,6 +2357,10 @@ static int aio_prep_rw(struct aio_kiocb *kiocb, const struct iocb *iocb, ret =kiocb_set_rw_flags (req, iocb->aio_rw_flags);if (unlikely (ret)) goto out_fput; +if (force_nonblock) { + req->ki_flags |=IOCB_NOWAIT ; +set_bit (KIOCB_F_FORCE_NONBLOCK , &kiocb->ki_flags); + }if (iocb->aio_flags &IOCB_FLAG_HIPRI ) {/* shares space in the union, and is rather pointless.. */
@@ -2399,7 +2506,7 @@ static void aio_iopoll_iocb_issued(struct aio_submit_state *state,static ssize_t aio_read (struct aio_kiocb *kiocb,const struct iocb *iocb,struct aio_submit_state *state,bool vectored, -bool compat,bool kaddr) +bool compat,bool kaddr,bool force_nonblock) {struct iovec inline_vecs[UIO_FASTIOV ], *iovec = inline_vecs;struct kiocb *req = &kiocb->rw;
@@ -2407,7 +2514,7 @@ static ssize_t aio_read(struct aio_kiocb *kiocb, const struct iocb *iocb,struct file *file;ssize_t ret; - ret =aio_prep_rw (kiocb, iocb, state); + ret =aio_prep_rw (kiocb, iocb, state, force_nonblock);if (ret)return ret; file = req->ki_filp;
@@ -2444,7 +2551,7 @@ static ssize_t aio_write(struct aio_kiocb *kiocb, const struct iocb *iocb,struct file *file;ssize_t ret; - ret =aio_prep_rw (kiocb, iocb, state); + ret =aio_prep_rw (kiocb, iocb, state, false);if (ret)return ret; file = req->ki_filp;
@@ -2697,7 +2804,7 @@ static ssize_t aio_poll(struct aio_kiocb *aiocb, const struct iocb *iocb)static int __io_submit_one (struct kioctx *ctx,const struct iocb *iocb,unsigned long ki_index,struct aio_submit_state *state,bool compat, -bool kaddr) +bool kaddr,bool force_nonblock) {struct aio_kiocb *req;ssize_t ret;
@@ -2758,13 +2865,15 @@ static int __io_submit_one(struct kioctx *ctx, const struct iocb *iocb, ret = -EINVAL ;switch (iocb->aio_lio_opcode) {case IOCB_CMD_PREAD : - ret =aio_read (req, iocb, state, false, compat, kaddr); + ret =aio_read (req, iocb, state, false, compat, kaddr, + force_nonblock);break ;case IOCB_CMD_PWRITE : ret =aio_write (req, iocb, state, false, compat, kaddr);break ;case IOCB_CMD_PREADV : - ret =aio_read (req, iocb, state, true, compat, kaddr); + ret =aio_read (req, iocb, state, true, compat, kaddr, + force_nonblock);break ;case IOCB_CMD_PWRITEV : ret =aio_write (req, iocb, state, true, compat, kaddr);
@@ -2824,7 +2933,8 @@ static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,if (unlikely (copy_from_user (&iocb, user_iocb,sizeof (iocb))))return -EFAULT ; -return __io_submit_one (ctx, &iocb, ki_index, state, compat, false); +return __io_submit_one (ctx, &iocb, ki_index, state, compat, false, + false); }#ifdef CONFIG_BLOCK
@@ -2944,7 +3054,8 @@ static int aio_ring_submit(struct kioctx *ctx, unsigned int to_submit)if (!iocb)break ; - ret =__io_submit_one (ctx, iocb, iocb_index, statep, false, kaddr); + ret =__io_submit_one (ctx, iocb, iocb_index, statep, false, kaddr, + false);if (ret)break ;
@@ -2996,15 +3107,288 @@ static int aio_cqring_wait(struct kioctx *ctx, int min_events)return ret; } +static void aio_fill_cq_error (struct kioctx *ctx,const struct iocb *iocb, +long ret) +{ +struct io_event *ev; +unsigned tail; + +/* +* Only really need the lock for non-polled IO, but this is an error +* so not worth checking. Just lock it so we know kernel access to +* the CQ ring is serialized. +*/ +spin_lock_irq (&ctx->completion_lock); + ev =aio_peek_cqring (ctx, &tail); + ev->obj = iocb->aio_data; + ev->data =0 ; + ev->res = ret; + ev->res2 =0 ; +aio_commit_cqring (ctx, tail); +spin_unlock_irq (&ctx->completion_lock); + +/* +* for thread offload, app could already be sleeping in io_ring_enter() +* before we get to flag the error. wake them up, if needed. +*/ +if (ctx->flags & (IOCTX_FLAG_SQTHREAD |IOCTX_FLAG_SQWQ )) +if (waitqueue_active (&ctx->wait)) +wake_up (&ctx->wait); +} + +struct iocb_submit { +const struct iocb *iocb; +unsigned int index; +}; + +static int aio_submit_iocbs (struct kioctx *ctx,struct iocb_submit *iocbs, +unsigned int nr,struct mm_struct *cur_mm, +bool mm_fault) +{ +struct aio_submit_state state, *statep =NULL ; +int ret, i, submitted =0 ; + +if (nr >AIO_PLUG_THRESHOLD ) { +aio_submit_state_start (&state, ctx, nr); + statep = &state; + } + +for (i =0 ; i < nr; i++) { +if (unlikely (mm_fault)) + ret = -EFAULT ; +else + ret =__io_submit_one (ctx, iocbs[i].iocb, + iocbs[i].index, statep, false, + !cur_mm, false); +if (!ret) { + submitted++; +continue ; + } + +aio_fill_cq_error (ctx, iocbs[i].iocb, ret); + } + +if (statep) +aio_submit_state_end (&state); + +return submitted; +} + +/* +* sq thread only supports O_DIRECT or FIXEDBUFS IO +*/ +static int aio_sq_thread (void *data) +{ +struct iocb_submit iocbs[AIO_IOPOLL_BATCH ]; +struct kioctx *ctx = data; +struct aio_sq_offload *aso = &ctx->sq_offload; +struct mm_struct *cur_mm =NULL ; +struct files_struct *old_files; +mm_segment_t old_fs; +DEFINE_WAIT (wait); + + old_files = current->files; + current->files = aso->files; + + old_fs =get_fs (); +set_fs (USER_DS ); + +while (!kthread_should_stop ()) { +const struct iocb *iocb; +bool mm_fault = false; +unsigned nhead, index; +int i; + + iocb =aio_peek_sqring (ctx, &index, &nhead); +if (!iocb) { +prepare_to_wait (&aso->wait, &wait,TASK_INTERRUPTIBLE ); + iocb =aio_peek_sqring (ctx, &index, &nhead); +if (!iocb) { +/* +* Drop cur_mm before scheduler. We can't hold +* it for long periods, and it would also +* introduce a deadlock with kill_ioctx(). +*/ +if (cur_mm) { +unuse_mm (cur_mm); +mmput (cur_mm); + cur_mm =NULL ; + } +if (kthread_should_park ()) +kthread_parkme (); +if (kthread_should_stop ()) { +finish_wait (&aso->wait, &wait); +break ; + } +if (signal_pending (current)) +flush_signals (current); +schedule (); + } +finish_wait (&aso->wait, &wait); +if (!iocb) +continue ; + } + +/* If ->mm is set, we're not doing FIXEDBUFS */ +if (aso->mm && !cur_mm) { + mm_fault = !mmget_not_zero (aso->mm); +if (!mm_fault) { +use_mm (aso->mm); + cur_mm = aso->mm; + } + } + + i =0 ; +do { +if (i ==ARRAY_SIZE (iocbs)) +break ; + iocbs[i].iocb = iocb; + iocbs[i].index = index; + ++i; +aio_commit_sqring (ctx, nhead); + }while ((iocb =aio_peek_sqring (ctx, &index, &nhead)) !=NULL ); + +aio_submit_iocbs (ctx, iocbs, i, cur_mm, mm_fault); + } + current->files = old_files; +set_fs (old_fs); +if (cur_mm) { +unuse_mm (cur_mm); +mmput (cur_mm); + } +return 0 ; +} + +struct aio_io_work { +struct work_struct work; +struct kioctx *ctx; +struct iocb iocb; +unsigned iocb_index; +}; + +static void aio_sq_wq_submit_work (struct work_struct *work) +{ + struct aio_io_work *aiw = container_of(work, struct aio_io_work, work); +struct kioctx *ctx = aiw->ctx; +struct aio_sq_offload *aso = &ctx->sq_offload; +mm_segment_t old_fs =get_fs (); +struct files_struct *old_files; +int ret; + + old_files = current->files; + current->files = aso->files; + +if (aso->mm) { +if (!mmget_not_zero (aso->mm)) { + ret = -EFAULT ; + goto err; + } +use_mm (aso->mm); + } + +set_fs (USER_DS ); + + ret =__io_submit_one (ctx, &aiw->iocb, aiw->iocb_index,NULL , false, + !aso->mm, false); + +set_fs (old_fs); +if (aso->mm) { +unuse_mm (aso->mm); +mmput (aso->mm); + } + +err: +if (ret) +aio_fill_cq_error (ctx, &aiw->iocb, ret); + current->files = old_files; +kfree (aiw); +} + +/* +* If this is a read, try a cached inline read first. If the IO is in the +* page cache, we can satisfy it without blocking and without having to +* punt to a threaded execution. This is much faster, particularly for +* lower queue depth IO, and it's always a lot more efficient. +*/ +static bool aio_sq_try_inline (struct kioctx *ctx,const struct iocb *iocb, +unsigned index) +{ +struct aio_sq_offload *aso = &ctx->sq_offload; +int ret; + +if (iocb->aio_lio_opcode !=IOCB_CMD_PREAD && + iocb->aio_lio_opcode !=IOCB_CMD_PREADV ) +return false; + + ret =__io_submit_one (ctx, iocb, index,NULL , false, !aso->mm, true); +if (ret == -EAGAIN || ctx->submit_eagain) { + ctx->submit_eagain =0 ; +return false; + } + +/* +* We're done - even if this was an error, return 0. The error will +* be in the CQ ring for the application. +*/ +return true; +} + +static int aio_sq_wq_submit (struct kioctx *ctx,unsigned int to_submit) +{ +struct aio_io_work *work; +const struct iocb *iocb; +unsigned nhead, index; +int ret, queued; + + ret = queued =0 ; +while ((iocb =aio_peek_sqring (ctx, &index, &nhead)) !=NULL ) { + ret =aio_sq_try_inline (ctx, iocb, index); +if (!ret) { + work =kmalloc (sizeof (*work),GFP_KERNEL ); +if (!work) { + ret = -ENOMEM ; +break ; + } +memcpy (&work->iocb, iocb,sizeof (*iocb)); +aio_commit_sqring (ctx, nhead); + work->iocb_index = index; +INIT_WORK (&work->work, aio_sq_wq_submit_work); + work->ctx = ctx; +queue_work (ctx->sq_offload.wq, &work->work); + } + queued++; +if (queued == to_submit) +break ; + } + +return queued ? queued : ret; +} +static int __io_ring_enter (struct kioctx *ctx,unsigned int to_submit,unsigned int min_complete,unsigned int flags) {int ret =0 ;if (flags &IORING_FLAG_SUBMIT ) { - ret =aio_ring_submit (ctx, to_submit); -if (ret <0 ) -return ret; +if (!to_submit) +return 0 ; + +/* +* Three options here: +* 1) We have an sq thread, just wake it up to do submissions +* 2) We have an sq wq, queue a work item for each iocb +* 3) Submit directly +*/ +if (ctx->flags &IOCTX_FLAG_SQTHREAD ) { +wake_up (&ctx->sq_offload.wait); + ret = to_submit; + }else if (ctx->flags &IOCTX_FLAG_SQWQ ) { + ret =aio_sq_wq_submit (ctx, to_submit); + }else { + ret =aio_ring_submit (ctx, to_submit); +if (ret <0 ) +return ret; + } }if (flags &IORING_FLAG_GETEVENTS ) {unsigned int nr_events =0 ;
include/uapi/linux/aio_abi.h +3 -0
@@ -111,6 +111,8 @@ struct iocb {#define IOCTX_FLAG_IOPOLL (1 << 0)/* io_context is polled */ #define IOCTX_FLAG_SCQRING (1 << 1)/* Use SQ/CQ rings */ #define IOCTX_FLAG_FIXEDBUFS (1 << 2)/* IO buffers are fixed */ +#define IOCTX_FLAG_SQTHREAD (1 << 3)/* Use SQ thread */ +#define IOCTX_FLAG_SQWQ (1 << 4)/* Use SQ workqueue */ struct aio_sq_ring { union {
@@ -118,6 +120,7 @@ struct aio_sq_ring {u32 head;/* kernel consumer head */ u32 tail;/* app producer tail */ u32 nr_events;/* max events in ring */ +u16 sq_thread_cpu;u64 iocbs;/* setup pointer to app iocbs */ };u32 pad[16 ];
Signature · 1 line
2.17.1
Jens Axboe
maintainerPATCH 20/22 aio: enable polling for IOCTX_FLAG_SQTHREAD
In reply to Jens AxboeThis enables an application to do IO, without ever entering the kernel. By using the SQ ring to fill in new events and watching for completions on the CQ ring, we can submit and reap IOs without doing a single system call. The kernel side thread will poll for new submissions, and in case of HIPRI/polled IO, it'll also poll for completions.
For O_DIRECT, we can do this with just SQTHREAD being enabled. For buffered aio, we need the workqueue as well. If we can satisfy the buffered inline from the SQTHREAD, we do that. If not, we punt to the workqueue. This is just like buffered aio off the io_ring_enter(2) system call.
Proof of concept. If the thread has been idle for 1 second, it will set sq_ring->kflags |= IORING_SQ_NEED_WAKEUP. The application will have to call io_ring_enter() to start things back up again. If IO is kept busy, that will never be needed. Basically an application that has this feature enabled will guard it's io_ring_enter(2) call with:
barrier();
if (ring->kflags & IORING_SQ_NEED_WAKEUP)
io_ring_enter(ctx, to_submit, 0, IORING_SUBMIT);
instead of calling it unconditionally.
Improvements:
1) Maybe have smarter backoff. Busy loop for X time, then go to monitor/mwait, finally the schedule we have now after an idle second. Might not be worth the complexity.
2) Probably want the application to pass in the appropriate grace period, not hard code it at 1 second.
--- fs/aio.c | 141 ++++++++++++++++++++++++++++------- include/uapi/linux/aio_abi.h | 4 + 2 files changed, 116 insertions(+), 29 deletions(-)
fs/aio.c +112 -29
@@ -117,6 +117,7 @@ struct aio_mapped_ubuf {struct aio_sq_offload {struct task_struct *thread;/* if using a thread */ +bool thread_poll;struct workqueue_struct *wq;/* wq offload */ struct mm_struct *mm;struct files_struct *files;
@@ -348,6 +349,7 @@ static void aio_iocb_buffer_unmap(struct kioctx *);static void aio_scqring_unmap (struct kioctx *);static void aio_iopoll_reap_events (struct kioctx *);static const struct iocb *aio_iocb_from_index (struct kioctx *ctx,unsigned idx); +static void aio_sq_wq_submit_work (struct work_struct *work);static struct file *aio_private_file (struct kioctx *ctx,loff_t nr_pages) {
@@ -1781,6 +1783,9 @@ static int aio_sq_thread_start(struct kioctx *ctx)if (!aso->files) goto err; +if (ctx->flags &IOCTX_FLAG_SQPOLL ) + aso->thread_poll = true; +if (ctx->flags &IOCTX_FLAG_SQTHREAD ) {char name[32 ];
@@ -1794,7 +1799,8 @@ static int aio_sq_thread_start(struct kioctx *ctx) goto err; }wake_up_process (aso->thread); - }else if (ctx->flags &IOCTX_FLAG_SQWQ ) { + } +if (ctx->flags &IOCTX_FLAG_SQWQ ) {int concurrency;/* Do QD, or 2 * CPUS, whatever is smallest */
@@ -1870,7 +1876,8 @@ static void aio_scqring_unmap(struct kioctx *ctx)kthread_park (aso->thread);kthread_stop (aso->thread); aso->thread =NULL ; - }else if (aso->wq) { + } +if (aso->wq) {destroy_workqueue (aso->wq); aso->wq =NULL ; }
@@ -2087,7 +2094,7 @@ SYSCALL_DEFINE5(io_setup2, u32, nr_events, u32, flags,if (flags & ~(IOCTX_FLAG_IOPOLL |IOCTX_FLAG_SCQRING |IOCTX_FLAG_FIXEDBUFS |IOCTX_FLAG_SQTHREAD | -IOCTX_FLAG_SQWQ )) +IOCTX_FLAG_SQWQ |IOCTX_FLAG_SQPOLL ))return -EINVAL ; ret =get_user (ctx, ctxp);
@@ -3141,28 +3148,69 @@ struct iocb_submit {unsigned int index; }; +struct aio_io_work { +struct work_struct work; +struct kioctx *ctx; +struct iocb iocb; +unsigned iocb_index; +}; + +static int aio_queue_async_work (struct kioctx *ctx,struct iocb_submit *is) +{ +struct aio_io_work *work; + + work =kmalloc (sizeof (*work),GFP_KERNEL ); +if (work) { +memcpy (&work->iocb, is->iocb,sizeof (*is->iocb)); + work->iocb_index = is->index; +INIT_WORK (&work->work, aio_sq_wq_submit_work); + work->ctx = ctx; +queue_work (ctx->sq_offload.wq, &work->work); +return 0 ; + } + +return -ENOMEM ; +} +static int aio_submit_iocbs (struct kioctx *ctx,struct iocb_submit *iocbs,unsigned int nr,struct mm_struct *cur_mm,bool mm_fault) {struct aio_submit_state state, *statep =NULL ;int ret, i, submitted =0 ; +bool force_nonblock;if (nr >AIO_PLUG_THRESHOLD ) {aio_submit_state_start (&state, ctx, nr); statep = &state; } +/* +* Having both a thread and a workqueue only makes sense for buffered +* IO, where we can't submit in an async fashion. Use the NOWAIT +* trick from the SQ thread, and punt to the workqueue if we can't +* satisfy this iocb without blocking. This is only necessary +* for buffered IO with sqthread polled submission. +*/ + force_nonblock = (ctx->flags &IOCTX_FLAG_SQWQ ) !=0 ; +for (i =0 ; i < nr; i++) { -if (unlikely (mm_fault)) +if (unlikely (mm_fault)) { ret = -EFAULT ; -else + }else { ret =__io_submit_one (ctx, iocbs[i].iocb, iocbs[i].index, statep, false, - !cur_mm, false); -if (!ret) { - submitted++; -continue ; + !cur_mm, force_nonblock); +/* nogo, submit to workqueue */ +if (force_nonblock && + (ret == -EAGAIN || ctx->submit_eagain)) { + ctx->submit_eagain =0 ; + ret =aio_queue_async_work (ctx, &iocbs[i]); + } +if (!ret) { + submitted++; +continue ; + } }aio_fill_cq_error (ctx, iocbs[i].iocb, ret);
@@ -3175,17 +3223,23 @@ static int aio_submit_iocbs(struct kioctx *ctx, struct iocb_submit *iocbs, }/* -* sq thread only supports O_DIRECT or FIXEDBUFS IO +* SQ thread is woken if the app asked for offloaded submission. This can +* be either O_DIRECT, in which case we do submissions directly, or it can +* be buffered IO, in which case we do them inline if we can do so without +* blocking. If we can't, then we punt to a workqueue. */ static int aio_sq_thread (void *data) {struct iocb_submit iocbs[AIO_IOPOLL_BATCH ];struct kioctx *ctx = data; +struct aio_sq_ring *ring = ctx->sq_ring.ring;struct aio_sq_offload *aso = &ctx->sq_offload;struct mm_struct *cur_mm =NULL ;struct files_struct *old_files;mm_segment_t old_fs;DEFINE_WAIT (wait); +unsigned inflight; +unsigned long timeout; old_files = current->files; current->files = aso->files;
@@ -3193,15 +3247,50 @@ static int aio_sq_thread(void *data) old_fs =get_fs ();set_fs (USER_DS ); + timeout = inflight =0 ;while (!kthread_should_stop ()) {const struct iocb *iocb;bool mm_fault = false;unsigned nhead, index;int i; +if (aso->thread_poll && inflight) { +unsigned int nr_events =0 ; + +/* +* Buffered IO, just pretend everything completed. +* We don't have to poll completions for that. +*/ +if (ctx->flags &IOCTX_FLAG_IOPOLL ) +__aio_iopoll_check (ctx,NULL , &nr_events,0 ,-1U ); +else + nr_events = inflight; + + inflight -= nr_events; +if (!inflight) + timeout = jiffies +HZ ; + } + iocb =aio_peek_sqring (ctx, &index, &nhead);if (!iocb) { +/* +* If we're polling, let us spin for a second without +* work before going to sleep. +*/ +if (aso->thread_poll) { +if (inflight || !time_after (jiffies, timeout)) { +cpu_relax (); +continue ; + } + }prepare_to_wait (&aso->wait, &wait,TASK_INTERRUPTIBLE ); + +/* Tell userspace we may need a wakeup call */ +if (aso->thread_poll) { + ring->kflags |=IORING_SQ_NEED_WAKEUP ; +smp_wmb (); + } + iocb =aio_peek_sqring (ctx, &index, &nhead);if (!iocb) {/*
@@ -3223,6 +3312,9 @@ static int aio_sq_thread(void *data)if (signal_pending (current))flush_signals (current);schedule (); + +if (aso->thread_poll) + ring->kflags &= ~IORING_SQ_NEED_WAKEUP ; }finish_wait (&aso->wait, &wait);if (!iocb)
@@ -3248,7 +3340,7 @@ static int aio_sq_thread(void *data)aio_commit_sqring (ctx, nhead); }while ((iocb =aio_peek_sqring (ctx, &index, &nhead)) !=NULL ); -aio_submit_iocbs (ctx, iocbs, i, cur_mm, mm_fault); + inflight +=aio_submit_iocbs (ctx, iocbs, i, cur_mm, mm_fault); } current->files = old_files;set_fs (old_fs);
@@ -3259,13 +3351,6 @@ static int aio_sq_thread(void *data)return 0 ; } -struct aio_io_work { -struct work_struct work; -struct kioctx *ctx; -struct iocb iocb; -unsigned iocb_index; -}; -static void aio_sq_wq_submit_work (struct work_struct *work) { struct aio_io_work *aiw = container_of(work, struct aio_io_work, work);
@@ -3335,7 +3420,6 @@ static bool aio_sq_try_inline(struct kioctx *ctx, const struct iocb *iocb,static int aio_sq_wq_submit (struct kioctx *ctx,unsigned int to_submit) { -struct aio_io_work *work;const struct iocb *iocb;unsigned nhead, index;int ret, queued;
@@ -3344,18 +3428,17 @@ static int aio_sq_wq_submit(struct kioctx *ctx, unsigned int to_submit)while ((iocb =aio_peek_sqring (ctx, &index, &nhead)) !=NULL ) { ret =aio_sq_try_inline (ctx, iocb, index);if (!ret) { - work =kmalloc (sizeof (*work),GFP_KERNEL ); -if (!work) { - ret = -ENOMEM ; +struct iocb_submit is = { + .iocb = iocb, + .index = index + }; + + ret =aio_queue_async_work (ctx, &is); +if (ret)break ; - } -memcpy (&work->iocb, iocb,sizeof (*iocb)); -aio_commit_sqring (ctx, nhead); - work->iocb_index = index; -INIT_WORK (&work->work, aio_sq_wq_submit_work); - work->ctx = ctx; -queue_work (ctx->sq_offload.wq, &work->work); } + +aio_commit_sqring (ctx, nhead); queued++;if (queued == to_submit)break ;
include/uapi/linux/aio_abi.h +4 -0
@@ -113,6 +113,9 @@ struct iocb {#define IOCTX_FLAG_FIXEDBUFS (1 << 2)/* IO buffers are fixed */ #define IOCTX_FLAG_SQTHREAD (1 << 3)/* Use SQ thread */ #define IOCTX_FLAG_SQWQ (1 << 4)/* Use SQ workqueue */ +#define IOCTX_FLAG_SQPOLL (1 << 5)/* SQ thread polls */ + +#define IORING_SQ_NEED_WAKEUP (1 << 0)/* needs io_ring_enter wakeup */ struct aio_sq_ring { union {
@@ -121,6 +124,7 @@ struct aio_sq_ring {u32 tail;/* app producer tail */ u32 nr_events;/* max events in ring */ u16 sq_thread_cpu; +u16 kflags;/* kernel info to app */ u64 iocbs;/* setup pointer to app iocbs */ };u32 pad[16 ];
Signature · 1 line
2.17.1
Jens Axboe
maintainerPATCH 21/22 aio: utilize io_event->res2 for CQ ring
In reply to Jens AxboeWe don't use this at all, grab it to provide hints about the data. The first hint we add is IORING_RES2_CACHEHIT, whether a read was served out of the page cache, or if it hit media. This is useful for buffered aio, O_DIRECT reads would never have this set (for obvious reasons).
--- fs/aio.c | 7 +++++-- include/uapi/linux/aio_abi.h | 6 ++++++ 2 files changed, 11 insertions(+), 2 deletions(-)
fs/aio.c +5 -2
@@ -1369,6 +1369,7 @@ static void aio_complete(struct aio_kiocb *iocb, long res, long res2)struct kioctx *ctx = iocb->ki_ctx;if (ctx->flags &IOCTX_FLAG_SCQRING ) { +int nowait =test_bit (KIOCB_F_FORCE_NONBLOCK , &iocb->ki_flags);unsigned long flags;struct io_event *ev;unsigned int tail;
@@ -1380,10 +1381,12 @@ static void aio_complete(struct aio_kiocb *iocb, long res, long res2)* see it and punt to workqueue. This is just for buffered * aio reads. */ -if (res == -EAGAIN && -test_bit (KIOCB_F_FORCE_NONBLOCK , &iocb->ki_flags)) { +if (res == -EAGAIN && nowait) { ctx->submit_eagain =1 ; }else { +if (nowait) + res2 =IOEV_RES2_CACHEHIT ; +/* * If we can't get a cq entry, userspace overflowed the * submission (by quite a lot). Flag it as an overflow
include/uapi/linux/aio_abi.h +6 -0
@@ -66,6 +66,12 @@ struct io_event {__s64 res2;/* secondary result */ }; +/* +* aio CQ ring commandeers the otherwise unused ev->res2 to provide +* metadata about the IO. +*/ +#define IOEV_RES2_CACHEHIT (1 << 0)/* IO did not hit media */ +/* * we always use a 64bit off_t when communicating * with userland. its up to libraries to do the
Signature · 1 line
2.17.1
Jens Axboe
maintainerPATCH 22/22 aio: add my copyright
In reply to Jens Axboe--- fs/aio.c | 1 + 1 file changed, 1 insertion(+)
fs/aio.c +1 -0
@@ -6,6 +6,7 @@* * Copyright 2000, 2001, 2002 Red Hat, Inc. All Rights Reserved. * Copyright 2018 Christoph Hellwig. +* Copyright 2018 Jens Axboe * * See ../COPYING for licensing terms. */
Signature · 1 line
2.17.1