Skip to content

Commit 723be6e

Browse files
Kent Overstreetbcrl
authored andcommitted
aio: percpu ioctx refcount
This just converts the ioctx refcount to the new generic dynamic percpu refcount code. 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: Benjamin LaHaise <bcrl@kvack.org>
1 parent e1bdd5f commit 723be6e

1 file changed

Lines changed: 27 additions & 39 deletions

File tree

‎fs/aio.c‎

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <linux/anon_inodes.h>
4040
#include <linux/migrate.h>
4141
#include <linux/ramfs.h>
42+
#include <linux/percpu-refcount.h>
4243

4344
#include <asm/kmap_types.h>
4445
#include <asm/uaccess.h>
@@ -70,7 +71,7 @@ struct kioctx_cpu {
7071
};
7172

7273
struct kioctx {
73-
atomic_t users;
74+
struct percpu_ref users;
7475
atomic_t dead;
7576

7677
/* This needs improving */
@@ -103,7 +104,7 @@ struct kioctx {
103104
long nr_pages;
104105

105106
struct rcu_head rcu_head;
106-
struct work_struct rcu_work;
107+
struct work_struct free_work;
107108

108109
struct {
109110
/*
@@ -403,8 +404,9 @@ static void free_ioctx_rcu(struct rcu_head *head)
403404
* and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
404405
* now it's safe to cancel any that need to be.
405406
*/
406-
static void free_ioctx(struct kioctx *ctx)
407+
static void free_ioctx(struct work_struct *work)
407408
{
409+
struct kioctx *ctx = container_of(work, struct kioctx, free_work);
408410
struct aio_ring *ring;
409411
struct io_event res;
410412
struct kiocb *req;
@@ -462,10 +464,12 @@ static void free_ioctx(struct kioctx *ctx)
462464
call_rcu(&ctx->rcu_head, free_ioctx_rcu);
463465
}
464466

465-
static void put_ioctx(struct kioctx *ctx)
467+
static void free_ioctx_ref(struct percpu_ref *ref)
466468
{
467-
if (unlikely(atomic_dec_and_test(&ctx->users)))
468-
free_ioctx(ctx);
469+
struct kioctx *ctx = container_of(ref, struct kioctx, users);
470+
471+
INIT_WORK(&ctx->free_work, free_ioctx);
472+
schedule_work(&ctx->free_work);
469473
}
470474

471475
/* ioctx_alloc
@@ -505,8 +509,9 @@ static struct kioctx *ioctx_alloc(unsigned nr_events)
505509

506510
ctx->max_reqs = nr_events;
507511

508-
atomic_set(&ctx->users, 2);
509-
atomic_set(&ctx->dead, 0);
512+
if (percpu_ref_init(&ctx->users, free_ioctx_ref))
513+
goto out_freectx;
514+
510515
spin_lock_init(&ctx->ctx_lock);
511516
spin_lock_init(&ctx->completion_lock);
512517
mutex_init(&ctx->ring_lock);
@@ -516,7 +521,7 @@ static struct kioctx *ioctx_alloc(unsigned nr_events)
516521

517522
ctx->cpu = alloc_percpu(struct kioctx_cpu);
518523
if (!ctx->cpu)
519-
goto out_freectx;
524+
goto out_freeref;
520525

521526
if (aio_setup_ring(ctx) < 0)
522527
goto out_freepcpu;
@@ -535,6 +540,8 @@ static struct kioctx *ioctx_alloc(unsigned nr_events)
535540
aio_nr += ctx->max_reqs;
536541
spin_unlock(&aio_nr_lock);
537542

543+
percpu_ref_get(&ctx->users); /* io_setup() will drop this ref */
544+
538545
/* now link into global list. */
539546
spin_lock(&mm->ioctx_lock);
540547
hlist_add_head_rcu(&ctx->list, &mm->ioctx_list);
@@ -549,6 +556,8 @@ static struct kioctx *ioctx_alloc(unsigned nr_events)
549556
aio_free_ring(ctx);
550557
out_freepcpu:
551558
free_percpu(ctx->cpu);
559+
out_freeref:
560+
free_percpu(ctx->users.pcpu_count);
552561
out_freectx:
553562
if (ctx->aio_ring_file)
554563
fput(ctx->aio_ring_file);
@@ -557,22 +566,6 @@ static struct kioctx *ioctx_alloc(unsigned nr_events)
557566
return ERR_PTR(err);
558567
}
559568

560-
static void kill_ioctx_work(struct work_struct *work)
561-
{
562-
struct kioctx *ctx = container_of(work, struct kioctx, rcu_work);
563-
564-
wake_up_all(&ctx->wait);
565-
put_ioctx(ctx);
566-
}
567-
568-
static void kill_ioctx_rcu(struct rcu_head *head)
569-
{
570-
struct kioctx *ctx = container_of(head, struct kioctx, rcu_head);
571-
572-
INIT_WORK(&ctx->rcu_work, kill_ioctx_work);
573-
schedule_work(&ctx->rcu_work);
574-
}
575-
576569
/* kill_ioctx
577570
* Cancels all outstanding aio requests on an aio context. Used
578571
* when the processes owning a context have all exited to encourage
@@ -582,6 +575,8 @@ static void kill_ioctx(struct kioctx *ctx)
582575
{
583576
if (!atomic_xchg(&ctx->dead, 1)) {
584577
hlist_del_rcu(&ctx->list);
578+
/* percpu_ref_kill() will do the necessary call_rcu() */
579+
wake_up_all(&ctx->wait);
585580

586581
/*
587582
* It'd be more correct to do this in free_ioctx(), after all
@@ -598,8 +593,7 @@ static void kill_ioctx(struct kioctx *ctx)
598593
if (ctx->mmap_size)
599594
vm_munmap(ctx->mmap_base, ctx->mmap_size);
600595

601-
/* Between hlist_del_rcu() and dropping the initial ref */
602-
call_rcu(&ctx->rcu_head, kill_ioctx_rcu);
596+
percpu_ref_kill(&ctx->users);
603597
}
604598
}
605599

@@ -633,12 +627,6 @@ void exit_aio(struct mm_struct *mm)
633627
struct hlist_node *n;
634628

635629
hlist_for_each_entry_safe(ctx, n, &mm->ioctx_list, list) {
636-
if (1 != atomic_read(&ctx->users))
637-
printk(KERN_DEBUG
638-
"exit_aio:ioctx still alive: %d %d %d\n",
639-
atomic_read(&ctx->users),
640-
atomic_read(&ctx->dead),
641-
atomic_read(&ctx->reqs_available));
642630
/*
643631
* We don't need to bother with munmap() here -
644632
* exit_mmap(mm) is coming and it'll unmap everything.
@@ -757,7 +745,7 @@ static struct kioctx *lookup_ioctx(unsigned long ctx_id)
757745

758746
hlist_for_each_entry_rcu(ctx, &mm->ioctx_list, list) {
759747
if (ctx->user_id == ctx_id) {
760-
atomic_inc(&ctx->users);
748+
percpu_ref_get(&ctx->users);
761749
ret = ctx;
762750
break;
763751
}
@@ -1054,7 +1042,7 @@ SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
10541042
ret = put_user(ioctx->user_id, ctxp);
10551043
if (ret)
10561044
kill_ioctx(ioctx);
1057-
put_ioctx(ioctx);
1045+
percpu_ref_put(&ioctx->users);
10581046
}
10591047

10601048
out:
@@ -1072,7 +1060,7 @@ SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
10721060
struct kioctx *ioctx = lookup_ioctx(ctx);
10731061
if (likely(NULL != ioctx)) {
10741062
kill_ioctx(ioctx);
1075-
put_ioctx(ioctx);
1063+
percpu_ref_put(&ioctx->users);
10761064
return 0;
10771065
}
10781066
pr_debug("EINVAL: io_destroy: invalid context id\n");
@@ -1394,7 +1382,7 @@ long do_io_submit(aio_context_t ctx_id, long nr,
13941382
}
13951383
blk_finish_plug(&plug);
13961384

1397-
put_ioctx(ctx);
1385+
percpu_ref_put(&ctx->users);
13981386
return i ? i : ret;
13991387
}
14001388

@@ -1483,7 +1471,7 @@ SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
14831471
ret = -EFAULT;
14841472
}
14851473

1486-
put_ioctx(ctx);
1474+
percpu_ref_put(&ctx->users);
14871475

14881476
return ret;
14891477
}
@@ -1512,7 +1500,7 @@ SYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id,
15121500
if (likely(ioctx)) {
15131501
if (likely(min_nr <= nr && min_nr >= 0))
15141502
ret = read_events(ioctx, min_nr, nr, events, timeout);
1515-
put_ioctx(ioctx);
1503+
percpu_ref_put(&ioctx->users);
15161504
}
15171505
return ret;
15181506
}

0 commit comments

Comments
 (0)