Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 780cd8b3 authored by Tejun Heo's avatar Tejun Heo
Browse files

cgroup: make for_each_subsys() useable under cgroup_root_mutex



We want to use for_each_subsys() in cgroupfs_root handling where only
cgroup_root_mutex is held.  The only way cgroup_subsys[] can change is
through module load/unload, make cgroup_[un]load_subsys() grab
cgroup_root_mutex too and update the lockdep annotation in
for_each_subsys() to allow either cgroup_mutex or cgroup_root_mutex.

* Lockdep annotation is moved from inner 'if' condition to outer 'for'
  init caluse.  There's no reason to execute the assertion every loop.

* Loop index @i is renamed to @ssid.  Indices iterating through subsys
  will be [re]named to @ssid gradually.

v2: cgroup_assert_mutex_or_root_locked() caused build failure if
    !CONFIG_LOCKEDP.  Conditionalize its definition.  The build failure
    was reported by kbuild test bot.

Signed-off-by: default avatarTejun Heo <tj@kernel.org>
Acked-by: default avatarLi Zefan <lizefan@huawei.com>
Cc: kbuild test robot <fengguang.wu@intel.com>
parent 87fb54f1
Loading
Loading
Loading
Loading
+20 −6
Original line number Original line Diff line number Diff line
@@ -98,6 +98,14 @@ static DEFINE_MUTEX(cgroup_root_mutex);
			   lockdep_is_held(&cgroup_mutex),		\
			   lockdep_is_held(&cgroup_mutex),		\
			   "cgroup_mutex or RCU read lock required");
			   "cgroup_mutex or RCU read lock required");


#ifdef CONFIG_LOCKDEP
#define cgroup_assert_mutex_or_root_locked()				\
	WARN_ON_ONCE(debug_locks && (!lockdep_is_held(&cgroup_mutex) &&	\
				     !lockdep_is_held(&cgroup_root_mutex)))
#else
#define cgroup_assert_mutex_or_root_locked()	do { } while (0)
#endif

/*
/*
 * cgroup destruction makes heavy use of work items and there can be a lot
 * cgroup destruction makes heavy use of work items and there can be a lot
 * of concurrent destructions.  Use a separate workqueue so that cgroup
 * of concurrent destructions.  Use a separate workqueue so that cgroup
@@ -237,14 +245,15 @@ static int notify_on_release(const struct cgroup *cgrp)
/**
/**
 * for_each_subsys - iterate all loaded cgroup subsystems
 * for_each_subsys - iterate all loaded cgroup subsystems
 * @ss: the iteration cursor
 * @ss: the iteration cursor
 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
 *
 *
 * Should be called under cgroup_mutex.
 * Iterates through all loaded subsystems.  Should be called under
 * cgroup_mutex or cgroup_root_mutex.
 */
 */
#define for_each_subsys(ss, i)						\
#define for_each_subsys(ss, ssid)					\
	for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++)			\
	for (({ cgroup_assert_mutex_or_root_locked(); (ssid) = 0; });	\
		if (({ lockdep_assert_held(&cgroup_mutex);		\
	     (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++)			\
		       !((ss) = cgroup_subsys[i]); })) { }		\
		if (!((ss) = cgroup_subsys[(ssid)])) { }		\
		else
		else


/**
/**
@@ -4592,6 +4601,7 @@ int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
	cgroup_init_cftsets(ss);
	cgroup_init_cftsets(ss);


	mutex_lock(&cgroup_mutex);
	mutex_lock(&cgroup_mutex);
	mutex_lock(&cgroup_root_mutex);
	cgroup_subsys[ss->subsys_id] = ss;
	cgroup_subsys[ss->subsys_id] = ss;


	/*
	/*
@@ -4641,10 +4651,12 @@ int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
		goto err_unload;
		goto err_unload;


	/* success! */
	/* success! */
	mutex_unlock(&cgroup_root_mutex);
	mutex_unlock(&cgroup_mutex);
	mutex_unlock(&cgroup_mutex);
	return 0;
	return 0;


err_unload:
err_unload:
	mutex_unlock(&cgroup_root_mutex);
	mutex_unlock(&cgroup_mutex);
	mutex_unlock(&cgroup_mutex);
	/* @ss can't be mounted here as try_module_get() would fail */
	/* @ss can't be mounted here as try_module_get() would fail */
	cgroup_unload_subsys(ss);
	cgroup_unload_subsys(ss);
@@ -4674,6 +4686,7 @@ void cgroup_unload_subsys(struct cgroup_subsys *ss)
	BUG_ON(ss->root != &cgroup_dummy_root);
	BUG_ON(ss->root != &cgroup_dummy_root);


	mutex_lock(&cgroup_mutex);
	mutex_lock(&cgroup_mutex);
	mutex_lock(&cgroup_root_mutex);


	offline_css(cgroup_css(cgroup_dummy_top, ss));
	offline_css(cgroup_css(cgroup_dummy_top, ss));


@@ -4708,6 +4721,7 @@ void cgroup_unload_subsys(struct cgroup_subsys *ss)
	ss->css_free(cgroup_css(cgroup_dummy_top, ss));
	ss->css_free(cgroup_css(cgroup_dummy_top, ss));
	RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);
	RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);


	mutex_unlock(&cgroup_root_mutex);
	mutex_unlock(&cgroup_mutex);
	mutex_unlock(&cgroup_mutex);
}
}
EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
EXPORT_SYMBOL_GPL(cgroup_unload_subsys);