[Devel] [PATCH RHEL9 COMMIT] cgroup: remove excess rcu_read_lock in cgroup marking

Konstantin Khorenko khorenko at virtuozzo.com
Wed Oct 13 14:13:29 MSK 2021


The commit is pushed to "branch-rh9-5.14.vz9.1.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh9-5.14.0-4.vz9.10.1
------>
commit 90c3b81039abfa45f1037f86739a183374086912
Author: Pavel Tikhomirov <ptikhomirov at virtuozzo.com>
Date:   Wed Oct 13 14:13:18 2021 +0300

    cgroup: remove excess rcu_read_lock in cgroup marking
    
    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);
 }


More information about the Devel mailing list