Skip to content

Commit 5ffac12

Browse files
Kent Overstreetbcrl
authored andcommitted
aio: Don't use ctx->tail unnecessarily
aio_complete() (arguably) needs to keep its own trusted copy of the tail pointer, but io_getevents() doesn't have to use it - it's already using the head pointer from the ring buffer. So convert it to use the tail from the ring buffer so it touches fewer cachelines and doesn't contend with the cacheline aio_complete() needs. 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: Benjamin LaHaise <bcrl@kvack.org>
1 parent bec68fa commit 5ffac12

1 file changed

Lines changed: 23 additions & 19 deletions

File tree

‎fs/aio.c‎

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,8 @@ static void free_ioctx(struct work_struct *work)
406406
struct kioctx *ctx = container_of(work, struct kioctx, free_work);
407407
struct aio_ring *ring;
408408
struct kiocb *req;
409-
unsigned cpu, head, avail;
409+
unsigned cpu, avail;
410+
DEFINE_WAIT(wait);
410411

411412
spin_lock_irq(&ctx->ctx_lock);
412413

@@ -427,22 +428,24 @@ static void free_ioctx(struct work_struct *work)
427428
kcpu->reqs_available = 0;
428429
}
429430

430-
ring = kmap_atomic(ctx->ring_pages[0]);
431-
head = ring->head;
432-
kunmap_atomic(ring);
431+
while (1) {
432+
prepare_to_wait(&ctx->wait, &wait, TASK_UNINTERRUPTIBLE);
433433

434-
while (atomic_read(&ctx->reqs_available) < ctx->nr_events - 1) {
435-
wait_event(ctx->wait,
436-
(head != ctx->tail) ||
437-
(atomic_read(&ctx->reqs_available) >=
438-
ctx->nr_events - 1));
439-
440-
avail = (head <= ctx->tail ? ctx->tail : ctx->nr_events) - head;
434+
ring = kmap_atomic(ctx->ring_pages[0]);
435+
avail = (ring->head <= ring->tail)
436+
? ring->tail - ring->head
437+
: ctx->nr_events - ring->head + ring->tail;
441438

442439
atomic_add(avail, &ctx->reqs_available);
443-
head += avail;
444-
head %= ctx->nr_events;
440+
ring->head = ring->tail;
441+
kunmap_atomic(ring);
442+
443+
if (atomic_read(&ctx->reqs_available) >= ctx->nr_events - 1)
444+
break;
445+
446+
schedule();
445447
}
448+
finish_wait(&ctx->wait, &wait);
446449

447450
WARN_ON(atomic_read(&ctx->reqs_available) > ctx->nr_events - 1);
448451

@@ -869,28 +872,29 @@ static long aio_read_events_ring(struct kioctx *ctx,
869872
struct io_event __user *event, long nr)
870873
{
871874
struct aio_ring *ring;
872-
unsigned head, pos;
875+
unsigned head, tail, pos;
873876
long ret = 0;
874877
int copy_ret;
875878

876879
mutex_lock(&ctx->ring_lock);
877880

878881
ring = kmap_atomic(ctx->ring_pages[0]);
879882
head = ring->head;
883+
tail = ring->tail;
880884
kunmap_atomic(ring);
881885

882-
pr_debug("h%u t%u m%u\n", head, ctx->tail, ctx->nr_events);
886+
pr_debug("h%u t%u m%u\n", head, tail, ctx->nr_events);
883887

884-
if (head == ctx->tail)
888+
if (head == tail)
885889
goto out;
886890

887891
while (ret < nr) {
888892
long avail;
889893
struct io_event *ev;
890894
struct page *page;
891895

892-
avail = (head <= ctx->tail ? ctx->tail : ctx->nr_events) - head;
893-
if (head == ctx->tail)
896+
avail = (head <= tail ? tail : ctx->nr_events) - head;
897+
if (head == tail)
894898
break;
895899

896900
avail = min(avail, nr - ret);
@@ -921,7 +925,7 @@ static long aio_read_events_ring(struct kioctx *ctx,
921925
kunmap_atomic(ring);
922926
flush_dcache_page(ctx->ring_pages[0]);
923927

924-
pr_debug("%li h%u t%u\n", ret, head, ctx->tail);
928+
pr_debug("%li h%u t%u\n", ret, head, tail);
925929

926930
put_reqs_available(ctx, ret);
927931
out:

0 commit comments

Comments
 (0)