Skip to content

Commit 9a1049d

Browse files
committed
percpu-refcount: require percpu_ref to be exited explicitly
Currently, a percpu_ref undoes percpu_ref_init() automatically by freeing the allocated percpu area when the percpu_ref is killed. While seemingly convenient, this has the following niggles. * It's impossible to re-init a released reference counter without going through re-allocation. * In the similar vein, it's impossible to initialize a percpu_ref count with static percpu variables. * We need and have an explicit destructor anyway for failure paths - percpu_ref_cancel_init(). This patch removes the automatic percpu counter freeing in percpu_ref_kill_rcu() and repurposes percpu_ref_cancel_init() into a generic destructor now named percpu_ref_exit(). percpu_ref_destroy() is considered but it gets confusing with percpu_ref_kill() while "exit" clearly indicates that it's the counterpart of percpu_ref_init(). All percpu_ref_cancel_init() users are updated to invoke percpu_ref_exit() instead and explicit percpu_ref_exit() calls are added to the destruction path of all percpu_ref users. Signed-off-by: Tejun Heo <tj@kernel.org> Acked-by: Benjamin LaHaise <bcrl@kvack.org> Cc: Kent Overstreet <kmo@daterainc.com> Cc: Christoph Lameter <cl@linux-foundation.org> Cc: Benjamin LaHaise <bcrl@kvack.org> Cc: Nicholas A. Bellinger <nab@linux-iscsi.org> Cc: Li Zefan <lizefan@huawei.com>
1 parent 7d74207 commit 9a1049d

5 files changed

Lines changed: 24 additions & 33 deletions

File tree

‎drivers/target/target_core_tpg.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ int core_tpg_add_lun(
825825

826826
ret = core_dev_export(dev, tpg, lun);
827827
if (ret < 0) {
828-
percpu_ref_cancel_init(&lun->lun_ref);
828+
percpu_ref_exit(&lun->lun_ref);
829829
return ret;
830830
}
831831

@@ -880,5 +880,7 @@ int core_tpg_post_dellun(
880880
lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
881881
spin_unlock(&tpg->tpg_lun_lock);
882882

883+
percpu_ref_exit(&lun->lun_ref);
884+
883885
return 0;
884886
}

‎fs/aio.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@ static void free_ioctx(struct work_struct *work)
506506

507507
aio_free_ring(ctx);
508508
free_percpu(ctx->cpu);
509+
percpu_ref_exit(&ctx->reqs);
510+
percpu_ref_exit(&ctx->users);
509511
kmem_cache_free(kioctx_cachep, ctx);
510512
}
511513

@@ -715,8 +717,8 @@ static struct kioctx *ioctx_alloc(unsigned nr_events)
715717
err:
716718
mutex_unlock(&ctx->ring_lock);
717719
free_percpu(ctx->cpu);
718-
percpu_ref_cancel_init(&ctx->reqs);
719-
percpu_ref_cancel_init(&ctx->users);
720+
percpu_ref_exit(&ctx->reqs);
721+
percpu_ref_exit(&ctx->users);
720722
kmem_cache_free(kioctx_cachep, ctx);
721723
pr_debug("error allocating ioctx %d\n", err);
722724
return ERR_PTR(err);

‎include/linux/percpu-refcount.h‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ struct percpu_ref {
5757
atomic_t count;
5858
/*
5959
* The low bit of the pointer indicates whether the ref is in percpu
60-
* mode; if set, then get/put will manipulate the atomic_t (this is a
61-
* hack because we need to keep the pointer around for
62-
* percpu_ref_kill_rcu())
60+
* mode; if set, then get/put will manipulate the atomic_t.
6361
*/
6462
unsigned long pcpu_count_ptr;
6563
percpu_ref_func_t *release;
@@ -69,7 +67,7 @@ struct percpu_ref {
6967

7068
int __must_check percpu_ref_init(struct percpu_ref *ref,
7169
percpu_ref_func_t *release);
72-
void percpu_ref_cancel_init(struct percpu_ref *ref);
70+
void percpu_ref_exit(struct percpu_ref *ref);
7371
void percpu_ref_kill_and_confirm(struct percpu_ref *ref,
7472
percpu_ref_func_t *confirm_kill);
7573

‎kernel/cgroup.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,7 +1638,7 @@ static int cgroup_setup_root(struct cgroup_root *root, unsigned int ss_mask)
16381638
exit_root_id:
16391639
cgroup_exit_root_id(root);
16401640
cancel_ref:
1641-
percpu_ref_cancel_init(&root_cgrp->self.refcnt);
1641+
percpu_ref_exit(&root_cgrp->self.refcnt);
16421642
out:
16431643
free_cgrp_cset_links(&tmp_links);
16441644
return ret;
@@ -4133,6 +4133,8 @@ static void css_free_work_fn(struct work_struct *work)
41334133
container_of(work, struct cgroup_subsys_state, destroy_work);
41344134
struct cgroup *cgrp = css->cgroup;
41354135

4136+
percpu_ref_exit(&css->refcnt);
4137+
41364138
if (css->ss) {
41374139
/* css free path */
41384140
if (css->parent)
@@ -4330,7 +4332,7 @@ static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
43304332
err_free_id:
43314333
cgroup_idr_remove(&ss->css_idr, css->id);
43324334
err_free_percpu_ref:
4333-
percpu_ref_cancel_init(&css->refcnt);
4335+
percpu_ref_exit(&css->refcnt);
43344336
err_free_css:
43354337
call_rcu(&css->rcu_head, css_free_rcu_fn);
43364338
return err;
@@ -4441,7 +4443,7 @@ static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
44414443
out_free_id:
44424444
cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
44434445
out_cancel_ref:
4444-
percpu_ref_cancel_init(&cgrp->self.refcnt);
4446+
percpu_ref_exit(&cgrp->self.refcnt);
44454447
out_free_cgrp:
44464448
kfree(cgrp);
44474449
out_unlock:

‎lib/percpu-refcount.c‎

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,36 +61,25 @@ int percpu_ref_init(struct percpu_ref *ref, percpu_ref_func_t *release)
6161
EXPORT_SYMBOL_GPL(percpu_ref_init);
6262

6363
/**
64-
* percpu_ref_cancel_init - cancel percpu_ref_init()
65-
* @ref: percpu_ref to cancel init for
64+
* percpu_ref_exit - undo percpu_ref_init()
65+
* @ref: percpu_ref to exit
6666
*
67-
* Once a percpu_ref is initialized, its destruction is initiated by
68-
* percpu_ref_kill() and completes asynchronously, which can be painful to
69-
* do when destroying a half-constructed object in init failure path.
70-
*
71-
* This function destroys @ref without invoking @ref->release and the
72-
* memory area containing it can be freed immediately on return. To
73-
* prevent accidental misuse, it's required that @ref has finished
74-
* percpu_ref_init(), whether successful or not, but never used.
75-
*
76-
* The weird name and usage restriction are to prevent people from using
77-
* this function by mistake for normal shutdown instead of
78-
* percpu_ref_kill().
67+
* This function exits @ref. The caller is responsible for ensuring that
68+
* @ref is no longer in active use. The usual places to invoke this
69+
* function from are the @ref->release() callback or in init failure path
70+
* where percpu_ref_init() succeeded but other parts of the initialization
71+
* of the embedding object failed.
7972
*/
80-
void percpu_ref_cancel_init(struct percpu_ref *ref)
73+
void percpu_ref_exit(struct percpu_ref *ref)
8174
{
8275
unsigned __percpu *pcpu_count = pcpu_count_ptr(ref);
83-
int cpu;
84-
85-
WARN_ON_ONCE(atomic_read(&ref->count) != 1 + PCPU_COUNT_BIAS);
8676

8777
if (pcpu_count) {
88-
for_each_possible_cpu(cpu)
89-
WARN_ON_ONCE(*per_cpu_ptr(pcpu_count, cpu));
9078
free_percpu(pcpu_count);
79+
ref->pcpu_count_ptr = PCPU_REF_DEAD;
9180
}
9281
}
93-
EXPORT_SYMBOL_GPL(percpu_ref_cancel_init);
82+
EXPORT_SYMBOL_GPL(percpu_ref_exit);
9483

9584
static void percpu_ref_kill_rcu(struct rcu_head *rcu)
9685
{
@@ -102,8 +91,6 @@ static void percpu_ref_kill_rcu(struct rcu_head *rcu)
10291
for_each_possible_cpu(cpu)
10392
count += *per_cpu_ptr(pcpu_count, cpu);
10493

105-
free_percpu(pcpu_count);
106-
10794
pr_debug("global %i pcpu %i", atomic_read(&ref->count), (int) count);
10895

10996
/*

0 commit comments

Comments
 (0)