Skip to content

Commit 888933f

Browse files
author
Christoph Hellwig
committed
aio: simplify cancellation
With the current aio code there is no need for the magic KIOCB_CANCELLED value, as a cancelation just kicks the driver to queue the completion ASAP, with all actual completion handling done in another thread. Given that both the completion path and cancelation take the context lock there is no need for magic cmpxchg loops either. If we remove iocbs from the active list after calling ->ki_cancel (but with ctx_lock still held), we can also rely on the invariant thay anything found on the list has a ->ki_cancel callback and can be cancelled, further simplifing the code. Signed-off-by: Christoph Hellwig <hch@lst.de>
1 parent f3a2752 commit 888933f

1 file changed

Lines changed: 6 additions & 42 deletions

File tree

‎fs/aio.c‎

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,6 @@ struct fsync_iocb {
164164
bool datasync;
165165
};
166166

167-
/*
168-
* We use ki_cancel == KIOCB_CANCELLED to indicate that a kiocb has been either
169-
* cancelled or completed (this makes a certain amount of sense because
170-
* successful cancellation - io_cancel() - does deliver the completion to
171-
* userspace).
172-
*
173-
* And since most things don't implement kiocb cancellation and we'd really like
174-
* kiocb completion to be lockless when possible, we use ki_cancel to
175-
* synchronize cancellation and completion - we only set it to KIOCB_CANCELLED
176-
* with xchg() or cmpxchg(), see batch_complete_aio() and kiocb_cancel().
177-
*/
178-
#define KIOCB_CANCELLED ((void *) (~0ULL))
179-
180167
struct aio_kiocb {
181168
union {
182169
struct kiocb rw;
@@ -574,27 +561,6 @@ void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel)
574561
}
575562
EXPORT_SYMBOL(kiocb_set_cancel_fn);
576563

577-
static int kiocb_cancel(struct aio_kiocb *kiocb)
578-
{
579-
kiocb_cancel_fn *old, *cancel;
580-
581-
/*
582-
* Don't want to set kiocb->ki_cancel = KIOCB_CANCELLED unless it
583-
* actually has a cancel function, hence the cmpxchg()
584-
*/
585-
586-
cancel = READ_ONCE(kiocb->ki_cancel);
587-
do {
588-
if (!cancel || cancel == KIOCB_CANCELLED)
589-
return -EINVAL;
590-
591-
old = cancel;
592-
cancel = cmpxchg(&kiocb->ki_cancel, old, KIOCB_CANCELLED);
593-
} while (cancel != old);
594-
595-
return cancel(&kiocb->rw);
596-
}
597-
598564
/*
599565
* free_ioctx() should be RCU delayed to synchronize against the RCU
600566
* protected lookup_ioctx() and also needs process context to call
@@ -641,7 +607,7 @@ static void free_ioctx_users(struct percpu_ref *ref)
641607
while (!list_empty(&ctx->active_reqs)) {
642608
req = list_first_entry(&ctx->active_reqs,
643609
struct aio_kiocb, ki_list);
644-
kiocb_cancel(req);
610+
req->ki_cancel(&req->rw);
645611
list_del_init(&req->ki_list);
646612
}
647613

@@ -1842,8 +1808,8 @@ SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
18421808
{
18431809
struct kioctx *ctx;
18441810
struct aio_kiocb *kiocb;
1811+
int ret = -EINVAL;
18451812
u32 key;
1846-
int ret;
18471813

18481814
if (unlikely(get_user(key, &iocb->aio_key)))
18491815
return -EFAULT;
@@ -1855,13 +1821,11 @@ SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
18551821
return -EINVAL;
18561822

18571823
spin_lock_irq(&ctx->ctx_lock);
1858-
18591824
kiocb = lookup_kiocb(ctx, iocb);
1860-
if (kiocb)
1861-
ret = kiocb_cancel(kiocb);
1862-
else
1863-
ret = -EINVAL;
1864-
1825+
if (kiocb) {
1826+
ret = kiocb->ki_cancel(&kiocb->rw);
1827+
list_del_init(&kiocb->ki_list);
1828+
}
18651829
spin_unlock_irq(&ctx->ctx_lock);
18661830

18671831
if (!ret) {

0 commit comments

Comments
 (0)