Skip to content

Commit 04b2fa9

Browse files
Christoph HellwigAl Viro
authored andcommitted
fs: split generic and aio kiocb
Most callers in the kernel want to perform synchronous file I/O, but still have to bloat the stack with a full struct kiocb. Split out the parts needed in filesystem code from those in the aio code, and only allocate those needed to pass down argument on the stack. The aio code embedds the generic iocb in the one it allocates and can easily get back to it by using container_of. Also add a ->ki_complete method to struct kiocb, this is used to call into the aio code and thus removes the dependency on aio for filesystems impementing asynchronous operations. It will also allow other callers to substitute their own completion callback. We also add a new ->ki_flags field to work around the nasty layering violation recently introduced in commit 5e33f6 ("usb: gadget: ffs: add eventfd notification about ffs events"). Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 599bd19 commit 04b2fa9

7 files changed

Lines changed: 81 additions & 77 deletions

File tree

‎drivers/usb/gadget/function/f_fs.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,9 +655,10 @@ static void ffs_user_copy_worker(struct work_struct *work)
655655
unuse_mm(io_data->mm);
656656
}
657657

658-
aio_complete(io_data->kiocb, ret, ret);
658+
io_data->kiocb->ki_complete(io_data->kiocb, ret, ret);
659659

660-
if (io_data->ffs->ffs_eventfd && !io_data->kiocb->ki_eventfd)
660+
if (io_data->ffs->ffs_eventfd &&
661+
!(io_data->kiocb->ki_flags & IOCB_EVENTFD))
661662
eventfd_signal(io_data->ffs->ffs_eventfd, 1);
662663

663664
usb_ep_free_request(io_data->ep, io_data->req);

‎drivers/usb/gadget/legacy/inode.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ static void ep_user_copy_worker(struct work_struct *work)
469469
ret = -EFAULT;
470470

471471
/* completing the iocb can drop the ctx and mm, don't touch mm after */
472-
aio_complete(iocb, ret, ret);
472+
iocb->ki_complete(iocb, ret, ret);
473473

474474
kfree(priv->buf);
475475
kfree(priv->to_free);
@@ -497,7 +497,8 @@ static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
497497
kfree(priv);
498498
iocb->private = NULL;
499499
/* aio_complete() reports bytes-transferred _and_ faults */
500-
aio_complete(iocb, req->actual ? req->actual : req->status,
500+
501+
iocb->ki_complete(iocb, req->actual ? req->actual : req->status,
501502
req->status);
502503
} else {
503504
/* ep_copy_to_user() won't report both; we hide some faults */

‎fs/aio.c‎

Lines changed: 65 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,38 @@ struct kioctx {
151151
unsigned id;
152152
};
153153

154+
/*
155+
* We use ki_cancel == KIOCB_CANCELLED to indicate that a kiocb has been either
156+
* cancelled or completed (this makes a certain amount of sense because
157+
* successful cancellation - io_cancel() - does deliver the completion to
158+
* userspace).
159+
*
160+
* And since most things don't implement kiocb cancellation and we'd really like
161+
* kiocb completion to be lockless when possible, we use ki_cancel to
162+
* synchronize cancellation and completion - we only set it to KIOCB_CANCELLED
163+
* with xchg() or cmpxchg(), see batch_complete_aio() and kiocb_cancel().
164+
*/
165+
#define KIOCB_CANCELLED ((void *) (~0ULL))
166+
167+
struct aio_kiocb {
168+
struct kiocb common;
169+
170+
struct kioctx *ki_ctx;
171+
kiocb_cancel_fn *ki_cancel;
172+
173+
struct iocb __user *ki_user_iocb; /* user's aiocb */
174+
__u64 ki_user_data; /* user's data for completion */
175+
176+
struct list_head ki_list; /* the aio core uses this
177+
* for cancellation */
178+
179+
/*
180+
* If the aio_resfd field of the userspace iocb is not zero,
181+
* this is the underlying eventfd context to deliver events to.
182+
*/
183+
struct eventfd_ctx *ki_eventfd;
184+
};
185+
154186
/*------ sysctl variables----*/
155187
static DEFINE_SPINLOCK(aio_nr_lock);
156188
unsigned long aio_nr; /* current system wide number of aio requests */
@@ -220,7 +252,7 @@ static int __init aio_setup(void)
220252
if (IS_ERR(aio_mnt))
221253
panic("Failed to create aio fs mount.");
222254

223-
kiocb_cachep = KMEM_CACHE(kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
255+
kiocb_cachep = KMEM_CACHE(aio_kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
224256
kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
225257

226258
pr_debug("sizeof(struct page) = %zu\n", sizeof(struct page));
@@ -480,8 +512,9 @@ static int aio_setup_ring(struct kioctx *ctx)
480512
#define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
481513
#define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
482514

483-
void kiocb_set_cancel_fn(struct kiocb *req, kiocb_cancel_fn *cancel)
515+
void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel)
484516
{
517+
struct aio_kiocb *req = container_of(iocb, struct aio_kiocb, common);
485518
struct kioctx *ctx = req->ki_ctx;
486519
unsigned long flags;
487520

@@ -496,7 +529,7 @@ void kiocb_set_cancel_fn(struct kiocb *req, kiocb_cancel_fn *cancel)
496529
}
497530
EXPORT_SYMBOL(kiocb_set_cancel_fn);
498531

499-
static int kiocb_cancel(struct kiocb *kiocb)
532+
static int kiocb_cancel(struct aio_kiocb *kiocb)
500533
{
501534
kiocb_cancel_fn *old, *cancel;
502535

@@ -514,7 +547,7 @@ static int kiocb_cancel(struct kiocb *kiocb)
514547
cancel = cmpxchg(&kiocb->ki_cancel, old, KIOCB_CANCELLED);
515548
} while (cancel != old);
516549

517-
return cancel(kiocb);
550+
return cancel(&kiocb->common);
518551
}
519552

520553
static void free_ioctx(struct work_struct *work)
@@ -550,13 +583,13 @@ static void free_ioctx_reqs(struct percpu_ref *ref)
550583
static void free_ioctx_users(struct percpu_ref *ref)
551584
{
552585
struct kioctx *ctx = container_of(ref, struct kioctx, users);
553-
struct kiocb *req;
586+
struct aio_kiocb *req;
554587

555588
spin_lock_irq(&ctx->ctx_lock);
556589

557590
while (!list_empty(&ctx->active_reqs)) {
558591
req = list_first_entry(&ctx->active_reqs,
559-
struct kiocb, ki_list);
592+
struct aio_kiocb, ki_list);
560593

561594
list_del_init(&req->ki_list);
562595
kiocb_cancel(req);
@@ -932,9 +965,9 @@ static void user_refill_reqs_available(struct kioctx *ctx)
932965
* Allocate a slot for an aio request.
933966
* Returns NULL if no requests are free.
934967
*/
935-
static inline struct kiocb *aio_get_req(struct kioctx *ctx)
968+
static inline struct aio_kiocb *aio_get_req(struct kioctx *ctx)
936969
{
937-
struct kiocb *req;
970+
struct aio_kiocb *req;
938971

939972
if (!get_reqs_available(ctx)) {
940973
user_refill_reqs_available(ctx);
@@ -955,10 +988,10 @@ static inline struct kiocb *aio_get_req(struct kioctx *ctx)
955988
return NULL;
956989
}
957990

958-
static void kiocb_free(struct kiocb *req)
991+
static void kiocb_free(struct aio_kiocb *req)
959992
{
960-
if (req->ki_filp)
961-
fput(req->ki_filp);
993+
if (req->common.ki_filp)
994+
fput(req->common.ki_filp);
962995
if (req->ki_eventfd != NULL)
963996
eventfd_ctx_put(req->ki_eventfd);
964997
kmem_cache_free(kiocb_cachep, req);
@@ -994,8 +1027,9 @@ static struct kioctx *lookup_ioctx(unsigned long ctx_id)
9941027
/* aio_complete
9951028
* Called when the io request on the given iocb is complete.
9961029
*/
997-
void aio_complete(struct kiocb *iocb, long res, long res2)
1030+
static void aio_complete(struct kiocb *kiocb, long res, long res2)
9981031
{
1032+
struct aio_kiocb *iocb = container_of(kiocb, struct aio_kiocb, common);
9991033
struct kioctx *ctx = iocb->ki_ctx;
10001034
struct aio_ring *ring;
10011035
struct io_event *ev_page, *event;
@@ -1009,7 +1043,7 @@ void aio_complete(struct kiocb *iocb, long res, long res2)
10091043
* ref, no other paths have a way to get another ref
10101044
* - the sync task helpfully left a reference to itself in the iocb
10111045
*/
1012-
BUG_ON(is_sync_kiocb(iocb));
1046+
BUG_ON(is_sync_kiocb(kiocb));
10131047

10141048
if (iocb->ki_list.next) {
10151049
unsigned long flags;
@@ -1035,7 +1069,7 @@ void aio_complete(struct kiocb *iocb, long res, long res2)
10351069
ev_page = kmap_atomic(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
10361070
event = ev_page + pos % AIO_EVENTS_PER_PAGE;
10371071

1038-
event->obj = (u64)(unsigned long)iocb->ki_obj.user;
1072+
event->obj = (u64)(unsigned long)iocb->ki_user_iocb;
10391073
event->data = iocb->ki_user_data;
10401074
event->res = res;
10411075
event->res2 = res2;
@@ -1044,7 +1078,7 @@ void aio_complete(struct kiocb *iocb, long res, long res2)
10441078
flush_dcache_page(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
10451079

10461080
pr_debug("%p[%u]: %p: %p %Lx %lx %lx\n",
1047-
ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
1081+
ctx, tail, iocb, iocb->ki_user_iocb, iocb->ki_user_data,
10481082
res, res2);
10491083

10501084
/* after flagging the request as done, we
@@ -1091,7 +1125,6 @@ void aio_complete(struct kiocb *iocb, long res, long res2)
10911125

10921126
percpu_ref_put(&ctx->reqs);
10931127
}
1094-
EXPORT_SYMBOL(aio_complete);
10951128

10961129
/* aio_read_events_ring
10971130
* Pull an event off of the ioctx's event ring. Returns the number of
@@ -1480,7 +1513,7 @@ static ssize_t aio_run_iocb(struct kiocb *req, unsigned opcode,
14801513
static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
14811514
struct iocb *iocb, bool compat)
14821515
{
1483-
struct kiocb *req;
1516+
struct aio_kiocb *req;
14841517
ssize_t ret;
14851518

14861519
/* enforce forwards compatibility on users */
@@ -1503,11 +1536,14 @@ static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
15031536
if (unlikely(!req))
15041537
return -EAGAIN;
15051538

1506-
req->ki_filp = fget(iocb->aio_fildes);
1507-
if (unlikely(!req->ki_filp)) {
1539+
req->common.ki_filp = fget(iocb->aio_fildes);
1540+
if (unlikely(!req->common.ki_filp)) {
15081541
ret = -EBADF;
15091542
goto out_put_req;
15101543
}
1544+
req->common.ki_pos = iocb->aio_offset;
1545+
req->common.ki_complete = aio_complete;
1546+
req->common.ki_flags = 0;
15111547

15121548
if (iocb->aio_flags & IOCB_FLAG_RESFD) {
15131549
/*
@@ -1522,6 +1558,8 @@ static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
15221558
req->ki_eventfd = NULL;
15231559
goto out_put_req;
15241560
}
1561+
1562+
req->common.ki_flags |= IOCB_EVENTFD;
15251563
}
15261564

15271565
ret = put_user(KIOCB_KEY, &user_iocb->aio_key);
@@ -1530,11 +1568,10 @@ static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
15301568
goto out_put_req;
15311569
}
15321570

1533-
req->ki_obj.user = user_iocb;
1571+
req->ki_user_iocb = user_iocb;
15341572
req->ki_user_data = iocb->aio_data;
1535-
req->ki_pos = iocb->aio_offset;
15361573

1537-
ret = aio_run_iocb(req, iocb->aio_lio_opcode,
1574+
ret = aio_run_iocb(&req->common, iocb->aio_lio_opcode,
15381575
(char __user *)(unsigned long)iocb->aio_buf,
15391576
iocb->aio_nbytes,
15401577
compat);
@@ -1623,20 +1660,19 @@ SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
16231660
/* lookup_kiocb
16241661
* Finds a given iocb for cancellation.
16251662
*/
1626-
static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
1627-
u32 key)
1663+
static struct aio_kiocb *
1664+
lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb, u32 key)
16281665
{
1629-
struct list_head *pos;
1666+
struct aio_kiocb *kiocb;
16301667

16311668
assert_spin_locked(&ctx->ctx_lock);
16321669

16331670
if (key != KIOCB_KEY)
16341671
return NULL;
16351672

16361673
/* TODO: use a hash or array, this sucks. */
1637-
list_for_each(pos, &ctx->active_reqs) {
1638-
struct kiocb *kiocb = list_kiocb(pos);
1639-
if (kiocb->ki_obj.user == iocb)
1674+
list_for_each_entry(kiocb, &ctx->active_reqs, ki_list) {
1675+
if (kiocb->ki_user_iocb == iocb)
16401676
return kiocb;
16411677
}
16421678
return NULL;
@@ -1656,7 +1692,7 @@ SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
16561692
struct io_event __user *, result)
16571693
{
16581694
struct kioctx *ctx;
1659-
struct kiocb *kiocb;
1695+
struct aio_kiocb *kiocb;
16601696
u32 key;
16611697
int ret;
16621698

‎fs/direct-io.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ static ssize_t dio_complete(struct dio *dio, loff_t offset, ssize_t ret,
265265
ret = err;
266266
}
267267

268-
aio_complete(dio->iocb, ret, 0);
268+
dio->iocb->ki_complete(dio->iocb, ret, 0);
269269
}
270270

271271
kmem_cache_free(dio_cache, dio);
@@ -1056,7 +1056,7 @@ static inline int drop_refcount(struct dio *dio)
10561056
* operation. AIO can if it was a broken operation described above or
10571057
* in fact if all the bios race to complete before we get here. In
10581058
* that case dio_complete() translates the EIOCBQUEUED into the proper
1059-
* return code that the caller will hand to aio_complete().
1059+
* return code that the caller will hand to ->complete().
10601060
*
10611061
* This is managed by the bio_lock instead of being an atomic_t so that
10621062
* completion paths can drop their ref and use the remaining count to

‎fs/fuse/file.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
584584
spin_unlock(&fc->lock);
585585
}
586586

587-
aio_complete(io->iocb, res, 0);
587+
io->iocb->ki_complete(io->iocb, res, 0);
588588
kfree(io);
589589
}
590590
}

‎fs/nfs/direct.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ static void nfs_direct_complete(struct nfs_direct_req *dreq, bool write)
393393
long res = (long) dreq->error;
394394
if (!res)
395395
res = (long) dreq->count;
396-
aio_complete(dreq->iocb, res, 0);
396+
dreq->iocb->ki_complete(dreq->iocb, res, 0);
397397
}
398398

399399
complete_all(&dreq->completion);

0 commit comments

Comments
 (0)