public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Paul E. McKenney" <paulmck@kernel.org>
To: rcu@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, kernel-team@fb.com,
	rostedt@goodmis.org, David Woodhouse <dwmw@amazon.co.uk>,
	"Paul E . McKenney" <paulmck@kernel.org>
Subject: [PATCH rcu 3/9] rcu: Add mutex for rcu boost kthread spawning and affinity setting
Date: Fri,  4 Feb 2022 15:07:59 -0800	[thread overview]
Message-ID: <20220204230805.4193767-3-paulmck@kernel.org> (raw)
In-Reply-To: <20220204230751.GA4193671@paulmck-ThinkPad-P17-Gen-1>

From: David Woodhouse <dwmw@amazon.co.uk>

As we handle parallel CPU bringup, we will need to take care to avoid
spawning multiple boost threads, or race conditions when setting their
affinity. Spotted by Paul McKenney.

Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
---
 kernel/rcu/tree.c        |  1 +
 kernel/rcu/tree.h        |  3 +++
 kernel/rcu/tree_plugin.h | 10 ++++++++--
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c
index a4c25a6283b0b..d1d1a8c51223b 100644
--- a/kernel/rcu/tree.c
+++ b/kernel/rcu/tree.c
@@ -4570,6 +4570,7 @@ static void __init rcu_init_one(void)
 			init_waitqueue_head(&rnp->exp_wq[2]);
 			init_waitqueue_head(&rnp->exp_wq[3]);
 			spin_lock_init(&rnp->exp_lock);
+			mutex_init(&rnp->boost_kthread_mutex);
 		}
 	}
 
diff --git a/kernel/rcu/tree.h b/kernel/rcu/tree.h
index 486fc901bd085..3b8b60de07c38 100644
--- a/kernel/rcu/tree.h
+++ b/kernel/rcu/tree.h
@@ -110,6 +110,9 @@ struct rcu_node {
 				/*  side effect, not as a lock. */
 	unsigned long boost_time;
 				/* When to start boosting (jiffies). */
+	struct mutex boost_kthread_mutex;
+				/* Exclusion for thread spawning and affinity */
+				/*  manipulation. */
 	struct task_struct *boost_kthread_task;
 				/* kthread that takes care of priority */
 				/*  boosting for this rcu_node structure. */
diff --git a/kernel/rcu/tree_plugin.h b/kernel/rcu/tree_plugin.h
index c5b45c2f68a15..07845dcd33c5e 100644
--- a/kernel/rcu/tree_plugin.h
+++ b/kernel/rcu/tree_plugin.h
@@ -1172,15 +1172,16 @@ static void rcu_spawn_one_boost_kthread(struct rcu_node *rnp)
 	struct sched_param sp;
 	struct task_struct *t;
 
+	mutex_lock(&rnp->boost_kthread_mutex);
 	if (rnp->boost_kthread_task || !rcu_scheduler_fully_active)
-		return;
+		goto out;
 
 	rcu_state.boost = 1;
 
 	t = kthread_create(rcu_boost_kthread, (void *)rnp,
 			   "rcub/%d", rnp_index);
 	if (WARN_ON_ONCE(IS_ERR(t)))
-		return;
+		goto out;
 
 	raw_spin_lock_irqsave_rcu_node(rnp, flags);
 	rnp->boost_kthread_task = t;
@@ -1188,6 +1189,9 @@ static void rcu_spawn_one_boost_kthread(struct rcu_node *rnp)
 	sp.sched_priority = kthread_prio;
 	sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
 	wake_up_process(t); /* get to TASK_INTERRUPTIBLE quickly. */
+
+ out:
+	mutex_unlock(&rnp->boost_kthread_mutex);
 }
 
 /*
@@ -1210,6 +1214,7 @@ static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
 		return;
 	if (!zalloc_cpumask_var(&cm, GFP_KERNEL))
 		return;
+	mutex_lock(&rnp->boost_kthread_mutex);
 	for_each_leaf_node_possible_cpu(rnp, cpu)
 		if ((mask & leaf_node_cpu_bit(rnp, cpu)) &&
 		    cpu != outgoingcpu)
@@ -1218,6 +1223,7 @@ static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
 	if (cpumask_weight(cm) == 0)
 		cpumask_copy(cm, housekeeping_cpumask(HK_FLAG_RCU));
 	set_cpus_allowed_ptr(t, cm);
+	mutex_unlock(&rnp->boost_kthread_mutex);
 	free_cpumask_var(cm);
 }
 
-- 
2.31.1.189.g2e36527f23


  parent reply	other threads:[~2022-02-04 23:08 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-04 23:07 [PATCH rcu 0/9] Miscellaneous fixes for v5.18 Paul E. McKenney
2022-02-04 23:07 ` [PATCH rcu 1/9] MAINTAINERS: Add Frederic and Neeraj to their RCU files Paul E. McKenney
2022-02-11 15:15   ` Frederic Weisbecker
2022-02-11 16:40     ` Paul E. McKenney
2022-02-04 23:07 ` [PATCH rcu 2/9] rcu: Fix description of kvfree_rcu() Paul E. McKenney
2022-02-04 23:07 ` Paul E. McKenney [this message]
2022-02-11 14:57   ` [PATCH rcu 3/9] rcu: Add mutex for rcu boost kthread spawning and affinity setting Frederic Weisbecker
2022-02-11 15:11     ` Frederic Weisbecker
2022-02-11 15:42       ` Paul E. McKenney
2022-02-11 15:43         ` Frederic Weisbecker
2022-02-04 23:08 ` [PATCH rcu 4/9] rcu: Inline __call_rcu() into call_rcu() Paul E. McKenney
2022-02-04 23:08 ` [PATCH rcu 5/9] kasan: Record work creation stack trace with interrupts enabled Paul E. McKenney
2022-02-04 23:08 ` [PATCH rcu 6/9] rcu: Mark writes to the rcu_segcblist structure's ->flags field Paul E. McKenney
2022-02-04 23:08 ` [PATCH rcu 7/9] rcu: Uninline multi-use function: finish_rcuwait() Paul E. McKenney
2022-02-04 23:08 ` [PATCH rcu 8/9] rcu: Remove __read_mostly annotations from rcu_scheduler_active externs Paul E. McKenney
2022-02-04 23:08 ` [PATCH rcu 9/9] rcu: Replace cpumask_weight with cpumask_empty where appropriate Paul E. McKenney
2022-02-11 15:17   ` Frederic Weisbecker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220204230805.4193767-3-paulmck@kernel.org \
    --to=paulmck@kernel.org \
    --cc=dwmw@amazon.co.uk \
    --cc=kernel-team@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rcu@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox