Commit 6fa4918d authored by Tejun Heo's avatar Tejun Heo
Browse files

cgroup: protect cgroup_root->cgroup_idr with a spinlock

Currently, cgroup_root->cgroup_idr is protected by cgroup_mutex, which
ends up requiring cgroup_put() to be invoked under sleepable context.
This is okay for now but is an unusual requirement and we'll soon add
css->id which will have the same problem but won't be able to simply
grab cgroup_mutex as removal will have to happen from css_release()
which can't sleep.

Introduce cgroup_idr_lock and idr_alloc/replace/remove() wrappers
which protects the idr operations with the lock and use them for
cgroup_root->cgroup_idr.  cgroup_put() no longer needs to grab
cgroup_mutex and css_from_id() is updated to always require RCU read
lock instead of either RCU read lock or cgroup_mutex, which doesn't
affect the existing users.
Signed-off-by: default avatarTejun Heo <>
Acked-by: default avatarLi Zefan <>
parent 7d699ddb
......@@ -99,6 +99,12 @@ static DEFINE_MUTEX(cgroup_mutex);
static DECLARE_RWSEM(css_set_rwsem);
* Protects cgroup_idr so that IDs can be released without grabbing
* cgroup_mutex.
static DEFINE_SPINLOCK(cgroup_idr_lock);
* Protects cgroup_subsys->release_agent_path. Modifying it also requires
* cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
......@@ -190,6 +196,37 @@ static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
bool is_add);
static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
/* IDR wrappers which synchronize using cgroup_idr_lock */
static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
gfp_t gfp_mask)
int ret;
ret = idr_alloc(idr, ptr, start, end, gfp_mask);
return ret;
static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
void *ret;
ret = idr_replace(idr, ptr, id);
return ret;
static void cgroup_idr_remove(struct idr *idr, int id)
idr_remove(idr, id);
* cgroup_css - obtain a cgroup's css for the specified subsystem
* @cgrp: the cgroup of interest
......@@ -1058,9 +1095,7 @@ static void cgroup_put(struct cgroup *cgrp)
* per-subsystem and moved to css->id so that lookups are
* successful until the target css is released.
idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
cgrp->id = -1;
call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
......@@ -1531,7 +1566,7 @@ static int cgroup_setup_root(struct cgroup_root *root, unsigned int ss_mask)
ret = idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_NOWAIT);
if (ret < 0)
goto out;
root_cgrp->id = ret;
......@@ -4225,7 +4260,7 @@ static long cgroup_create(struct cgroup *parent, const char *name,
* Temporarily set the pointer to NULL, so idr_find() won't return
* a half-baked cgroup.
cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_NOWAIT);
if (cgrp->id < 0) {
err = -ENOMEM;
goto err_unlock;
......@@ -4268,7 +4303,7 @@ static long cgroup_create(struct cgroup *parent, const char *name,
* @cgrp is now fully operational. If something fails after this
* point, it'll be released via the normal destruction path.
idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
err = cgroup_kn_set_ugid(kn);
if (err)
......@@ -4302,7 +4337,7 @@ static long cgroup_create(struct cgroup *parent, const char *name,
return 0;
idr_remove(&root->cgroup_idr, cgrp->id);
cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
......@@ -5162,7 +5197,7 @@ struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
struct cgroup *cgrp;
cgrp = idr_find(&ss->root->cgroup_idr, id);
if (cgrp)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment