[PATCH 2/4] sched/core: Nest the isolation unit rq locks under one lock for lockdep
From: Ahmed Abdelraoof
Date: Thu Dec 03 2026 - 17:14:17 EST
From: Ahmed Abdelraoof <araoof@xxxxxxxxxx>
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@xxxxxxxxxx>
Signed-off-by: Anselm Busse <abusse@xxxxxxxxxx>
Signed-off-by: Ahmed Abdelraoof <araoof@xxxxxxxxxx>
---
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