Skip to content

Commit 3e845ce

Browse files
Kent Overstreettorvalds
authored andcommitted
aio: change reqs_active to include unreaped completions
The aio code tries really hard to avoid having to deal with the completion ringbuffer overflowing. To do that, it has to keep track of the number of outstanding kiocbs, and the number of completions currently in the ringbuffer - and it's got to check that every time we allocate a kiocb. Ouch. But - we can improve this quite a bit if we just change reqs_active to mean "number of outstanding requests and unreaped completions" - that means kiocb allocation doesn't have to look at the ringbuffer, which is a fairly significant win. Signed-off-by: Kent Overstreet <koverstreet@google.com> Cc: Zach Brown <zab@redhat.com> Cc: Felipe Balbi <balbi@ti.com> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Mark Fasheh <mfasheh@suse.com> Cc: Joel Becker <jlbec@evilplan.org> Cc: Rusty Russell <rusty@rustcorp.com.au> Cc: Jens Axboe <axboe@kernel.dk> Cc: Asai Thambi S P <asamymuthupa@micron.com> Cc: Selvan Mani <smani@micron.com> Cc: Sam Bradshaw <sbradshaw@micron.com> Cc: Jeff Moyer <jmoyer@redhat.com> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Benjamin LaHaise <bcrl@kvack.org> Signed-off-by: "Theodore Ts'o" <tytso@mit.edu> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 0460fef commit 3e845ce

1 file changed

Lines changed: 32 additions & 15 deletions

File tree

‎fs/aio.c‎

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,6 @@ struct aio_ring_info {
7171
struct page *internal_pages[AIO_RING_PAGES];
7272
};
7373

74-
static inline unsigned aio_ring_avail(struct aio_ring_info *info,
75-
struct aio_ring *ring)
76-
{
77-
return (ring->head + info->nr - 1 - ring->tail) % info->nr;
78-
}
79-
8074
struct kioctx {
8175
atomic_t users;
8276
atomic_t dead;
@@ -92,7 +86,13 @@ struct kioctx {
9286
atomic_t reqs_active;
9387
struct list_head active_reqs; /* used for cancellation */
9488

95-
/* sys_io_setup currently limits this to an unsigned int */
89+
/*
90+
* This is what userspace passed to io_setup(), it's not used for
91+
* anything but counting against the global max_reqs quota.
92+
*
93+
* The real limit is ring->nr - 1, which will be larger (see
94+
* aio_setup_ring())
95+
*/
9696
unsigned max_reqs;
9797

9898
struct aio_ring_info ring_info;
@@ -284,8 +284,11 @@ static void free_ioctx_rcu(struct rcu_head *head)
284284
*/
285285
static void free_ioctx(struct kioctx *ctx)
286286
{
287+
struct aio_ring_info *info = &ctx->ring_info;
288+
struct aio_ring *ring;
287289
struct io_event res;
288290
struct kiocb *req;
291+
unsigned head, avail;
289292

290293
spin_lock_irq(&ctx->ctx_lock);
291294

@@ -299,7 +302,21 @@ static void free_ioctx(struct kioctx *ctx)
299302

300303
spin_unlock_irq(&ctx->ctx_lock);
301304

302-
wait_event(ctx->wait, !atomic_read(&ctx->reqs_active));
305+
ring = kmap_atomic(info->ring_pages[0]);
306+
head = ring->head;
307+
kunmap_atomic(ring);
308+
309+
while (atomic_read(&ctx->reqs_active) > 0) {
310+
wait_event(ctx->wait, head != info->tail);
311+
312+
avail = (head <= info->tail ? info->tail : info->nr) - head;
313+
314+
atomic_sub(avail, &ctx->reqs_active);
315+
head += avail;
316+
head %= info->nr;
317+
}
318+
319+
WARN_ON(atomic_read(&ctx->reqs_active) < 0);
303320

304321
aio_free_ring(ctx);
305322

@@ -548,7 +565,6 @@ static int kiocb_batch_refill(struct kioctx *ctx, struct kiocb_batch *batch)
548565
unsigned short allocated, to_alloc;
549566
long avail;
550567
struct kiocb *req, *n;
551-
struct aio_ring *ring;
552568

553569
to_alloc = min(batch->count, KIOCB_BATCH_SIZE);
554570
for (allocated = 0; allocated < to_alloc; allocated++) {
@@ -563,10 +579,8 @@ static int kiocb_batch_refill(struct kioctx *ctx, struct kiocb_batch *batch)
563579
goto out;
564580

565581
spin_lock_irq(&ctx->ctx_lock);
566-
ring = kmap_atomic(ctx->ring_info.ring_pages[0]);
567582

568-
avail = aio_ring_avail(&ctx->ring_info, ring) -
569-
atomic_read(&ctx->reqs_active);
583+
avail = ctx->ring_info.nr - atomic_read(&ctx->reqs_active) - 1;
570584
BUG_ON(avail < 0);
571585
if (avail < allocated) {
572586
/* Trim back the number of requests. */
@@ -581,7 +595,6 @@ static int kiocb_batch_refill(struct kioctx *ctx, struct kiocb_batch *batch)
581595
batch->count -= allocated;
582596
atomic_add(allocated, &ctx->reqs_active);
583597

584-
kunmap_atomic(ring);
585598
spin_unlock_irq(&ctx->ctx_lock);
586599

587600
out:
@@ -688,8 +701,11 @@ void aio_complete(struct kiocb *iocb, long res, long res2)
688701
* when the event got cancelled.
689702
*/
690703
if (unlikely(xchg(&iocb->ki_cancel,
691-
KIOCB_CANCELLED) == KIOCB_CANCELLED))
704+
KIOCB_CANCELLED) == KIOCB_CANCELLED)) {
705+
atomic_dec(&ctx->reqs_active);
706+
/* Still need the wake_up in case free_ioctx is waiting */
692707
goto put_rq;
708+
}
693709

694710
/*
695711
* Add a completion event to the ring buffer. Must be done holding
@@ -746,7 +762,6 @@ void aio_complete(struct kiocb *iocb, long res, long res2)
746762
put_rq:
747763
/* everything turned out well, dispose of the aiocb. */
748764
aio_put_req(iocb);
749-
atomic_dec(&ctx->reqs_active);
750765

751766
/*
752767
* We have to order our ring_info tail store above and test
@@ -825,6 +840,8 @@ static long aio_read_events_ring(struct kioctx *ctx,
825840
flush_dcache_page(info->ring_pages[0]);
826841

827842
pr_debug("%li h%u t%u\n", ret, head, info->tail);
843+
844+
atomic_sub(ret, &ctx->reqs_active);
828845
out:
829846
mutex_unlock(&info->ring_lock);
830847

0 commit comments

Comments
 (0)