Linux Documentation
 help / color / mirror / Atom feed
From: Ahmed Abdelraoof <araoof@amazon.de>
To: Ingo Molnar <mingo@redhat.com>, Peter Zijlstra <peterz@infradead.org>
Cc: Juri Lelli <juri.lelli@redhat.com>,
	Vincent Guittot <vincent.guittot@linaro.org>,
	Dietmar Eggemann <dietmar.eggemann@arm.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ben Segall <bsegall@google.com>, "Mel Gorman" <mgorman@suse.de>,
	Valentin Schneider <vschneid@redhat.com>,
	"K Prateek Nayak" <kprateek.nayak@amd.com>,
	Jonathan Corbet <corbet@lwn.net>,
	Shuah Khan <skhan@linuxfoundation.org>,
	Randy Dunlap <rdunlap@infradead.org>, <linux-doc@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>,
	Ahmed Abdelraoof <araoof@amazon.com>,
	Anselm Busse <abusse@amazon.com>
Subject: [PATCH 2/4] sched/core: Nest the isolation unit rq locks under one lock for lockdep
Date: Thu, 3 Dec 2026 22:13:05 +0000	[thread overview]
Message-ID: <20261203221307.86078-3-araoof@amazon.de> (raw)
In-Reply-To: <20261203221307.86078-1-araoof@amazon.de>

From: Ahmed Abdelraoof <araoof@amazon.com>

Lockdep does not properly handle nested locking of more than 8 locks of
the same class using subclasses. This showed up when testing
L2-cache-based core scheduling policies in QEMU with custom topologies.

To work around this limitation, take the rq lock of the unit's first CPU
as a subclass of its own and nest the other rq locks under it.

The idea of using a nest lock to work around the 8-subclass limit is
inspired by commit e4a454ced74c ("KVM: add kvm_lock_all_vcpus and
kvm_trylock_all_vcpus"), where they did the trick using an already
existing lock.

The idea of splitting the first lock into a subclass of its own is
inspired by commit 566c09c53455 ("raid5: relieve lock contention in
get_active_stripe()").

Co-developed-by: Anselm Busse <abusse@amazon.com>
Signed-off-by: Anselm Busse <abusse@amazon.com>
Signed-off-by: Ahmed Abdelraoof <araoof@amazon.com>
---
 kernel/sched/core.c | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index cdd944bb274d..b62716094445 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -429,16 +429,26 @@ static DEFINE_MUTEX(sched_core_mutex);
 static atomic_t sched_core_count;
 static struct cpumask sched_core_mask;
 
+/*
+ * The rq locks of a unit share one lockdep class, and a subclass per CPU
+ * would overflow MAX_LOCKDEP_SUBCLASSES on units wider than eight CPUs. So
+ * nest them under the first CPU's rq lock, taken as a subclass of its own.
+ */
+#define SCHED_CORE_NEST_SUBCLASS	(SINGLE_DEPTH_NESTING + 1)
+
 static void sched_core_lock(int cpu, unsigned long *flags)
 	__context_unsafe(/* acquires multiple */)
 	__acquires(&runqueues.__lock) /* overapproximation */
 {
 	const struct cpumask *isolation_mask = sched_core_isolation_mask(cpu);
-	int t, i = 0;
+	struct rq *first = cpu_rq(cpumask_first(isolation_mask));
+	int t;
 
-	local_irq_save(*flags);
-	for_each_cpu(t, isolation_mask)
-		raw_spin_lock_nested(&cpu_rq(t)->__lock, i++);
+	raw_spin_lock_irqsave_nested(&first->__lock, *flags, SCHED_CORE_NEST_SUBCLASS);
+	for_each_cpu(t, isolation_mask) {
+		if (cpu_rq(t) != first)
+			raw_spin_lock_nest_lock(&cpu_rq(t)->__lock, &first->__lock);
+	}
 }
 
 static void sched_core_unlock(int cpu, unsigned long *flags)
@@ -446,11 +456,14 @@ static void sched_core_unlock(int cpu, unsigned long *flags)
 	__releases(&runqueues.__lock) /* overapproximation */
 {
 	const struct cpumask *isolation_mask = sched_core_isolation_mask(cpu);
+	struct rq *first = cpu_rq(cpumask_first(isolation_mask));
 	int t;
 
-	for_each_cpu(t, isolation_mask)
-		raw_spin_unlock(&cpu_rq(t)->__lock);
-	local_irq_restore(*flags);
+	for_each_cpu(t, isolation_mask) {
+		if (cpu_rq(t) != first)
+			raw_spin_unlock(&cpu_rq(t)->__lock);
+	}
+	raw_spin_unlock_irqrestore(&first->__lock, *flags);
 }
 
 static void __sched_core_flip(bool enabled)
-- 
2.47.3


  parent reply	other threads:[~2026-12-03 22:14 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 22:13 [PATCH 0/4] sched/core: Core scheduling beyond SMT siblings Ahmed Abdelraoof
2026-12-03 22:13 ` [PATCH 1/4] sched/core: Decouple the core scheduling unit from cpu_smt_mask Ahmed Abdelraoof
2026-12-03 22:13 ` Ahmed Abdelraoof [this message]
2026-12-03 22:13 ` [PATCH 3/4] sched/core: Gate core scheduling on the isolation unit Ahmed Abdelraoof
2026-12-03 22:13 ` [PATCH 4/4] sched/core: Support L2 cache isolation Ahmed Abdelraoof

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=20261203221307.86078-3-araoof@amazon.de \
    --to=araoof@amazon.de \
    --cc=abusse@amazon.com \
    --cc=araoof@amazon.com \
    --cc=bsegall@google.com \
    --cc=corbet@lwn.net \
    --cc=dietmar.eggemann@arm.com \
    --cc=juri.lelli@redhat.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rdunlap@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=skhan@linuxfoundation.org \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    /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