Skip to content

Commit 85d6822

Browse files
Peter ZijlstraFrederic Weisbecker
authored andcommitted
rcu: Break rcu_node_0 --> &rq->__lock order
Commit 851a723 ("sched: Always clear user_cpus_ptr in do_set_cpus_allowed()") added a kfree() call to free any user provided affinity mask, if present. It was changed later to use kfree_rcu() in commit 9a5418b ("sched/core: Use kfree_rcu() in do_set_cpus_allowed()") to avoid a circular locking dependency problem. It turns out that even kfree_rcu() isn't safe for avoiding circular locking problem. As reported by kernel test robot, the following circular locking dependency now exists: &rdp->nocb_lock --> rcu_node_0 --> &rq->__lock Solve this by breaking the rcu_node_0 --> &rq->__lock chain by moving the resched_cpu() out from under rcu_node lock. [peterz: heavily borrowed from Waiman's Changelog] [paulmck: applied Z qiang feedback] Fixes: 851a723 ("sched: Always clear user_cpus_ptr in do_set_cpus_allowed()") Reported-by: kernel test robot <oliver.sang@intel.com> Acked-by: Waiman Long <longman@redhat.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lore.kernel.org/oe-lkp/202310302207.a25f1a30-oliver.sang@intel.com Signed-off-by: Paul E. McKenney <paulmck@kernel.org> Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
1 parent 2656821 commit 85d6822

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

kernel/rcu/tree.c

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -755,14 +755,19 @@ static int dyntick_save_progress_counter(struct rcu_data *rdp)
755755
}
756756

757757
/*
758-
* Return true if the specified CPU has passed through a quiescent
759-
* state by virtue of being in or having passed through an dynticks
760-
* idle state since the last call to dyntick_save_progress_counter()
761-
* for this same CPU, or by virtue of having been offline.
758+
* Returns positive if the specified CPU has passed through a quiescent state
759+
* by virtue of being in or having passed through an dynticks idle state since
760+
* the last call to dyntick_save_progress_counter() for this same CPU, or by
761+
* virtue of having been offline.
762+
*
763+
* Returns negative if the specified CPU needs a force resched.
764+
*
765+
* Returns zero otherwise.
762766
*/
763767
static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
764768
{
765769
unsigned long jtsq;
770+
int ret = 0;
766771
struct rcu_node *rnp = rdp->mynode;
767772

768773
/*
@@ -848,8 +853,8 @@ static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
848853
(time_after(jiffies, READ_ONCE(rdp->last_fqs_resched) + jtsq * 3) ||
849854
rcu_state.cbovld)) {
850855
WRITE_ONCE(rdp->rcu_urgent_qs, true);
851-
resched_cpu(rdp->cpu);
852856
WRITE_ONCE(rdp->last_fqs_resched, jiffies);
857+
ret = -1;
853858
}
854859

855860
/*
@@ -862,8 +867,8 @@ static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
862867
if (time_after(jiffies, rcu_state.jiffies_resched)) {
863868
if (time_after(jiffies,
864869
READ_ONCE(rdp->last_fqs_resched) + jtsq)) {
865-
resched_cpu(rdp->cpu);
866870
WRITE_ONCE(rdp->last_fqs_resched, jiffies);
871+
ret = -1;
867872
}
868873
if (IS_ENABLED(CONFIG_IRQ_WORK) &&
869874
!rdp->rcu_iw_pending && rdp->rcu_iw_gp_seq != rnp->gp_seq &&
@@ -892,7 +897,7 @@ static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
892897
}
893898
}
894899

895-
return 0;
900+
return ret;
896901
}
897902

898903
/* Trace-event wrapper function for trace_rcu_future_grace_period. */
@@ -2271,15 +2276,15 @@ static void force_qs_rnp(int (*f)(struct rcu_data *rdp))
22712276
{
22722277
int cpu;
22732278
unsigned long flags;
2274-
unsigned long mask;
2275-
struct rcu_data *rdp;
22762279
struct rcu_node *rnp;
22772280

22782281
rcu_state.cbovld = rcu_state.cbovldnext;
22792282
rcu_state.cbovldnext = false;
22802283
rcu_for_each_leaf_node(rnp) {
2284+
unsigned long mask = 0;
2285+
unsigned long rsmask = 0;
2286+
22812287
cond_resched_tasks_rcu_qs();
2282-
mask = 0;
22832288
raw_spin_lock_irqsave_rcu_node(rnp, flags);
22842289
rcu_state.cbovldnext |= !!rnp->cbovldmask;
22852290
if (rnp->qsmask == 0) {
@@ -2297,11 +2302,17 @@ static void force_qs_rnp(int (*f)(struct rcu_data *rdp))
22972302
continue;
22982303
}
22992304
for_each_leaf_node_cpu_mask(rnp, cpu, rnp->qsmask) {
2305+
struct rcu_data *rdp;
2306+
int ret;
2307+
23002308
rdp = per_cpu_ptr(&rcu_data, cpu);
2301-
if (f(rdp)) {
2309+
ret = f(rdp);
2310+
if (ret > 0) {
23022311
mask |= rdp->grpmask;
23032312
rcu_disable_urgency_upon_qs(rdp);
23042313
}
2314+
if (ret < 0)
2315+
rsmask |= rdp->grpmask;
23052316
}
23062317
if (mask != 0) {
23072318
/* Idle/offline CPUs, report (releases rnp->lock). */
@@ -2310,6 +2321,9 @@ static void force_qs_rnp(int (*f)(struct rcu_data *rdp))
23102321
/* Nothing to do here, so just drop the lock. */
23112322
raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
23122323
}
2324+
2325+
for_each_leaf_node_cpu_mask(rnp, cpu, rsmask)
2326+
resched_cpu(cpu);
23132327
}
23142328
}
23152329

0 commit comments

Comments
 (0)