[Devel] [PATCH RH9 1/6] cgroup: remove excess rcu_read_lock in cgroup marking

Pavel Tikhomirov ptikhomirov at virtuozzo.com
Tue Oct 12 19:22:04 MSK 2021


We do not need rcu_read_lock here because we are already protected by
ve->op_sem from ve->ve_ns modification, so let's simplify code.

Also remove excess warnings.

https://jira.sw.ru/browse/PSBM-134002
Fixes: 907c1e6cd67f ("cgroup: Mark cgroup CGRP_VE_ROOT")
Signed-off-by: Pavel Tikhomirov <ptikhomirov at virtuozzo.com>
---
 kernel/cgroup/cgroup.c | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index 05fe9436a9a3..a425f06eceea 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -1969,19 +1969,20 @@ void cgroup_mark_ve_root(struct ve_struct *ve)
 
 	spin_lock_irq(&css_set_lock);
 
-	rcu_read_lock();
-	cset = rcu_dereference(ve->ve_ns)->cgroup_ns->root_cset;
-	if (WARN_ON(!cset))
-		goto unlock;
+	/*
+	 * We can safely use ve->ve_ns without rcu_read_lock here, as we are
+	 * always called _after_ ve_grab_context under ve->op_sem, so we've
+	 * just set ve_ns and nobody else can modify it under us.
+	 */
+	cset = rcu_dereference_protected(ve->ve_ns,
+			lockdep_is_held(&ve->op_sem))->cgroup_ns->root_cset;
 
 	list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
 		cgrp = link->cgrp;
 		set_bit(CGRP_VE_ROOT, &cgrp->flags);
 	}
-	link_ve_root_cpu_cgroup(cset->subsys[cpu_cgrp_id]);
-unlock:
-	rcu_read_unlock();
 
+	link_ve_root_cpu_cgroup(cset->subsys[cpu_cgrp_id]);
 	spin_unlock_irq(&css_set_lock);
 }
 
@@ -1993,17 +1994,18 @@ void cgroup_unmark_ve_roots(struct ve_struct *ve)
 
 	spin_lock_irq(&css_set_lock);
 
-	rcu_read_lock();
-	cset = rcu_dereference(ve->ve_ns)->cgroup_ns->root_cset;
-	if (WARN_ON(!cset))
-		goto unlock;
+	/*
+	 * We can safely use ve->ve_ns without rcu_read_lock here, as we are
+	 * always called _before_ ve_drop_context under ve->op_sem, so we
+	 * did not change ve_ns yet and nobody else can modify it under us.
+	 */
+	cset = rcu_dereference_protected(ve->ve_ns,
+			lockdep_is_held(&ve->op_sem))->cgroup_ns->root_cset;
 
 	list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
 		cgrp = link->cgrp;
 		clear_bit(CGRP_VE_ROOT, &cgrp->flags);
 	}
-unlock:
-	rcu_read_unlock();
 
 	spin_unlock_irq(&css_set_lock);
 }
-- 
2.31.1



More information about the Devel mailing list