Skip to content

Commit a1c8eae

Browse files
Kent Overstreettorvalds
authored andcommitted
aio: kill batch allocation
Previously, allocating a kiocb required touching quite a few global (well, per kioctx) cachelines... so batching up allocation to amortize those was worthwhile. But we've gotten rid of some of those, and in another couple of patches kiocb allocation won't require writing to any shared cachelines, so that means we can just rip this code out. 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> Reviewed-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 3e845ce commit a1c8eae

2 files changed

Lines changed: 15 additions & 102 deletions

File tree

‎fs/aio.c‎

Lines changed: 15 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -510,108 +510,27 @@ void exit_aio(struct mm_struct *mm)
510510
* This prevents races between the aio code path referencing the
511511
* req (after submitting it) and aio_complete() freeing the req.
512512
*/
513-
static struct kiocb *__aio_get_req(struct kioctx *ctx)
513+
static inline struct kiocb *aio_get_req(struct kioctx *ctx)
514514
{
515-
struct kiocb *req = NULL;
515+
struct kiocb *req;
516+
517+
if (atomic_read(&ctx->reqs_active) >= ctx->ring_info.nr)
518+
return NULL;
519+
520+
if (atomic_inc_return(&ctx->reqs_active) > ctx->ring_info.nr - 1)
521+
goto out_put;
516522

517523
req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL|__GFP_ZERO);
518524
if (unlikely(!req))
519-
return NULL;
525+
goto out_put;
520526

521527
atomic_set(&req->ki_users, 2);
522528
req->ki_ctx = ctx;
523529

524530
return req;
525-
}
526-
527-
/*
528-
* struct kiocb's are allocated in batches to reduce the number of
529-
* times the ctx lock is acquired and released.
530-
*/
531-
#define KIOCB_BATCH_SIZE 32L
532-
struct kiocb_batch {
533-
struct list_head head;
534-
long count; /* number of requests left to allocate */
535-
};
536-
537-
static void kiocb_batch_init(struct kiocb_batch *batch, long total)
538-
{
539-
INIT_LIST_HEAD(&batch->head);
540-
batch->count = total;
541-
}
542-
543-
static void kiocb_batch_free(struct kioctx *ctx, struct kiocb_batch *batch)
544-
{
545-
struct kiocb *req, *n;
546-
547-
if (list_empty(&batch->head))
548-
return;
549-
550-
spin_lock_irq(&ctx->ctx_lock);
551-
list_for_each_entry_safe(req, n, &batch->head, ki_batch) {
552-
list_del(&req->ki_batch);
553-
kmem_cache_free(kiocb_cachep, req);
554-
atomic_dec(&ctx->reqs_active);
555-
}
556-
spin_unlock_irq(&ctx->ctx_lock);
557-
}
558-
559-
/*
560-
* Allocate a batch of kiocbs. This avoids taking and dropping the
561-
* context lock a lot during setup.
562-
*/
563-
static int kiocb_batch_refill(struct kioctx *ctx, struct kiocb_batch *batch)
564-
{
565-
unsigned short allocated, to_alloc;
566-
long avail;
567-
struct kiocb *req, *n;
568-
569-
to_alloc = min(batch->count, KIOCB_BATCH_SIZE);
570-
for (allocated = 0; allocated < to_alloc; allocated++) {
571-
req = __aio_get_req(ctx);
572-
if (!req)
573-
/* allocation failed, go with what we've got */
574-
break;
575-
list_add(&req->ki_batch, &batch->head);
576-
}
577-
578-
if (allocated == 0)
579-
goto out;
580-
581-
spin_lock_irq(&ctx->ctx_lock);
582-
583-
avail = ctx->ring_info.nr - atomic_read(&ctx->reqs_active) - 1;
584-
BUG_ON(avail < 0);
585-
if (avail < allocated) {
586-
/* Trim back the number of requests. */
587-
list_for_each_entry_safe(req, n, &batch->head, ki_batch) {
588-
list_del(&req->ki_batch);
589-
kmem_cache_free(kiocb_cachep, req);
590-
if (--allocated <= avail)
591-
break;
592-
}
593-
}
594-
595-
batch->count -= allocated;
596-
atomic_add(allocated, &ctx->reqs_active);
597-
598-
spin_unlock_irq(&ctx->ctx_lock);
599-
600-
out:
601-
return allocated;
602-
}
603-
604-
static inline struct kiocb *aio_get_req(struct kioctx *ctx,
605-
struct kiocb_batch *batch)
606-
{
607-
struct kiocb *req;
608-
609-
if (list_empty(&batch->head))
610-
if (kiocb_batch_refill(ctx, batch) == 0)
611-
return NULL;
612-
req = list_first_entry(&batch->head, struct kiocb, ki_batch);
613-
list_del(&req->ki_batch);
614-
return req;
531+
out_put:
532+
atomic_dec(&ctx->reqs_active);
533+
return NULL;
615534
}
616535

617536
static void kiocb_free(struct kiocb *req)
@@ -1198,8 +1117,7 @@ static ssize_t aio_setup_iocb(struct kiocb *kiocb, bool compat)
11981117
}
11991118

12001119
static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
1201-
struct iocb *iocb, struct kiocb_batch *batch,
1202-
bool compat)
1120+
struct iocb *iocb, bool compat)
12031121
{
12041122
struct kiocb *req;
12051123
ssize_t ret;
@@ -1220,7 +1138,7 @@ static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
12201138
return -EINVAL;
12211139
}
12221140

1223-
req = aio_get_req(ctx, batch); /* returns with 2 references to req */
1141+
req = aio_get_req(ctx); /* returns with 2 references to req */
12241142
if (unlikely(!req))
12251143
return -EAGAIN;
12261144

@@ -1293,7 +1211,6 @@ long do_io_submit(aio_context_t ctx_id, long nr,
12931211
long ret = 0;
12941212
int i = 0;
12951213
struct blk_plug plug;
1296-
struct kiocb_batch batch;
12971214

12981215
if (unlikely(nr < 0))
12991216
return -EINVAL;
@@ -1310,8 +1227,6 @@ long do_io_submit(aio_context_t ctx_id, long nr,
13101227
return -EINVAL;
13111228
}
13121229

1313-
kiocb_batch_init(&batch, nr);
1314-
13151230
blk_start_plug(&plug);
13161231

13171232
/*
@@ -1332,13 +1247,12 @@ long do_io_submit(aio_context_t ctx_id, long nr,
13321247
break;
13331248
}
13341249

1335-
ret = io_submit_one(ctx, user_iocb, &tmp, &batch, compat);
1250+
ret = io_submit_one(ctx, user_iocb, &tmp, compat);
13361251
if (ret)
13371252
break;
13381253
}
13391254
blk_finish_plug(&plug);
13401255

1341-
kiocb_batch_free(ctx, &batch);
13421256
put_ioctx(ctx);
13431257
return i ? i : ret;
13441258
}

‎include/linux/aio.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ struct kiocb {
8585

8686
struct list_head ki_list; /* the aio core uses this
8787
* for cancellation */
88-
struct list_head ki_batch; /* batch allocation */
8988

9089
/*
9190
* If the aio_resfd field of the userspace iocb is not zero,

0 commit comments

Comments
 (0)