From 6158754e71382041e7978c1d906b374c5a68d061 Mon Sep 17 00:00:00 2001 From: Daniel Rossier Date: Tue, 10 Nov 2026 08:58:18 +0100 Subject: [PATCH 1/5] avz: a scheduler's lock is held by one CPU, and the others wait for it The paths of a domain's scheduler (its wake, its sleep, its unparking, a virq set pending, the scheduling decision itself) took the scheduler's lock unless it was held, taking "held" for "held by this CPU": a path entered with the lock held from an outer one on the same CPU must not take it again. But the lock is also taken by another CPU: the agency pauses a living capsule for an intermediate Delta snapshot from whichever of its CPUs the read runs on, and wakes it after, while the capsule CPU runs its scheduler every 30 ms as long as two capsules are runnable. When the two met, each went on without the lock, and whichever released it first released the other's: the capsule CPU could pick the capsule being paused after the pause had taken it off the runnable set, and keep it running with no scheduling timer, since one domain was left runnable, and no further event, since the agency CPU that would send one waited, with its interrupts off, for the capsule to leave the CPU. That CPU is the one the agency's interrupts go to: the agency fell silent whole, and the capsules with it. Each CPU now records the scheduler lock it holds: sched_lock() takes the lock unless this CPU holds it, and says so for sched_unlock(), which releases nothing the CPU does not hold; the context switch releases what the scheduling decision took or inherited. A CPU that finds the lock held by another waits for it, and counts the wait (sched_lock_contended()). The wait for a domain to leave its CPU is unchanged, and reports once, after 2 s, what each CPU is at, so that a stall of this kind, if any remains, says where it is. Seen once in 776 Delta snapshots over the campaigns: the mission's host in s4-delta-s8 (reports/eval-s4) fell silent 520 ms after the staging's Full was acknowledged, when the first Delta was read, with no snapshot line on the console and no exception; the earlier s2a event, a data abort in the scan under a concurrent reset, was another defect, closed by 5af2c78774. --- so3/so3/avz/include/avz/sched-if.h | 10 +++ so3/so3/avz/kernel/domain.c | 2 +- so3/so3/avz/kernel/event_channel.c | 10 +-- so3/so3/avz/kernel/schedule.c | 121 ++++++++++++++++++++++------- 4 files changed, 105 insertions(+), 38 deletions(-) diff --git a/so3/so3/avz/include/avz/sched-if.h b/so3/so3/avz/include/avz/sched-if.h index af2439759..62add6d39 100644 --- a/so3/so3/avz/include/avz/sched-if.h +++ b/so3/so3/avz/include/avz/sched-if.h @@ -47,4 +47,14 @@ struct scheduler { extern struct scheduler sched_flip; extern struct scheduler sched_agency; +/* + * The lock of a scheduler is taken by the CPU it schedules and by the agency + * CPUs that pause, wake or signal a capsule: sched_lock() takes it unless this + * CPU holds it already, and says whether it did, for sched_unlock(). + */ + +bool sched_lock(struct schedule_data *sd); +void sched_unlock(struct schedule_data *sd, bool taken); +unsigned long sched_lock_contended(void); + #endif /* __SCHED_IF_H__ */ diff --git a/so3/so3/avz/kernel/domain.c b/so3/so3/avz/kernel/domain.c index 37137e9cc..1e01cafc0 100644 --- a/so3/so3/avz/kernel/domain.c +++ b/so3/so3/avz/kernel/domain.c @@ -237,7 +237,7 @@ void context_switch(struct domain *prev, struct domain *next) /* Memory barrier /after/ clearing the running flag. */ smp_mb(); - spin_unlock(&prev->sched->sched_data.schedule_lock); + sched_unlock(&prev->sched->sched_data, true); __switch_domain_to(prev, next); } diff --git a/so3/so3/avz/kernel/event_channel.c b/so3/so3/avz/kernel/event_channel.c index c88b4e79b..819681cf4 100644 --- a/so3/so3/avz/kernel/event_channel.c +++ b/so3/so3/avz/kernel/event_channel.c @@ -377,7 +377,7 @@ void send_guest_virq(struct domain *d, int virq) { unsigned long flags; int evtchn; - bool __already_locked = false; + bool taken; flags = spin_lock_irqsave(&d->virq_lock); evtchn = d->virq_to_evtchn[virq]; @@ -385,10 +385,7 @@ void send_guest_virq(struct domain *d, int virq) if (unlikely(evtchn == 0)) goto out; - if (spin_is_locked(&d->sched->sched_data.schedule_lock)) - __already_locked = true; - else - spin_lock(&d->sched->sched_data.schedule_lock); + taken = sched_lock(&d->sched->sched_data); spin_lock(&d->event_lock); @@ -396,8 +393,7 @@ void send_guest_virq(struct domain *d, int virq) spin_unlock(&d->event_lock); - if (!__already_locked) - spin_unlock(&d->sched->sched_data.schedule_lock); + sched_unlock(&d->sched->sched_data, taken); out: spin_unlock_irqrestore(&d->virq_lock, flags); diff --git a/so3/so3/avz/kernel/schedule.c b/so3/so3/avz/kernel/schedule.c index 60b89f299..3fe0611d4 100644 --- a/so3/so3/avz/kernel/schedule.c +++ b/so3/so3/avz/kernel/schedule.c @@ -25,6 +25,8 @@ #include +#include + #include #include #include @@ -34,6 +36,64 @@ DEFINE_PER_CPU(struct domain *, current_domain); static spinlock_t schedule_lock; +/* + * The scheduler lock this CPU holds, if any. A CPU enters a scheduler's paths + * holding its lock from an outer one, and must then neither take it again nor + * release it under that outer path. Whether the lock is held says nothing + * about who holds it: an agency CPU pausing a capsule for a snapshot meets the + * capsule CPU inside its scheduler, and each must wait for the other. + */ + +static DEFINE_PER_CPU(spinlock_t *, sched_lock_held); +static DEFINE_PER_CPU(unsigned long, sched_lock_waited); + +/** + * Take a scheduler's lock, unless this CPU holds it already. + * + * @return true when it was taken here, for sched_unlock() + */ +bool sched_lock(struct schedule_data *sd) +{ + if (this_cpu(sched_lock_held) == &sd->schedule_lock) + return false; + + if (spin_is_locked(&sd->schedule_lock)) + this_cpu(sched_lock_waited)++; + + spin_lock(&sd->schedule_lock); + this_cpu(sched_lock_held) = &sd->schedule_lock; + + return true; +} + +/** + * Release a scheduler's lock taken by sched_lock() on this CPU; nothing when + * it was held from an outer path, or released by a context switch since. + */ +void sched_unlock(struct schedule_data *sd, bool taken) +{ + if (!taken || (this_cpu(sched_lock_held) != &sd->schedule_lock)) + return; + + this_cpu(sched_lock_held) = NULL; + spin_unlock(&sd->schedule_lock); +} + +/** + * How many times a CPU found a scheduler's lock held by another CPU and + * waited for it, over all CPUs. + */ +unsigned long sched_lock_contended(void) +{ + unsigned long n = 0; + int i; + + for (i = 0; i < CONFIG_NR_CPUS; i++) + n += per_cpu(sched_lock_waited, i); + + return n; +} + /* Various timer handlers. */ inline void domain_runstate_change(struct domain *d, int new_state) @@ -61,12 +121,7 @@ int sched_init_domain(struct domain *d, unsigned int processor) void vcpu_wake(struct domain *d) { - bool __already_locked = false; - - if (spin_is_locked(&d->sched->sched_data.schedule_lock)) - __already_locked = true; - else - spin_lock(&d->sched->sched_data.schedule_lock); + bool taken = sched_lock(&d->sched->sched_data); if (d->runstate >= RUNSTATE_blocked) { domain_runstate_change(d, RUNSTATE_runnable); @@ -76,8 +131,7 @@ void vcpu_wake(struct domain *d) clear_bit(_VPF_blocked, &d->pause_flags); } - if (!__already_locked) - spin_unlock(&d->sched->sched_data.schedule_lock); + sched_unlock(&d->sched->sched_data, taken); } /** @@ -120,21 +174,17 @@ bool vcpu_park(struct domain *d) */ void vcpu_unpark(struct domain *d) { - bool __already_locked = false; + bool taken; if (atomic_cmpxchg(&d->parked, 1, 0) != 1) return; - if (spin_is_locked(&d->sched->sched_data.schedule_lock)) - __already_locked = true; - else - spin_lock(&d->sched->sched_data.schedule_lock); + taken = sched_lock(&d->sched->sched_data); if (!atomic_read(&d->pause_count)) d->sched->wake(d); - if (!__already_locked) - spin_unlock(&d->sched->sched_data.schedule_lock); + sched_unlock(&d->sched->sched_data, taken); } /** @@ -181,12 +231,7 @@ void sched_destroy_domain(struct domain *d) void vcpu_sleep_nosync(struct domain *d) { - bool __already_locked = false; - - if (spin_is_locked(&d->sched->sched_data.schedule_lock)) - __already_locked = true; - else - spin_lock(&d->sched->sched_data.schedule_lock); + bool taken = sched_lock(&d->sched->sched_data); /* * Stop associated timers. @@ -210,19 +255,34 @@ void vcpu_sleep_nosync(struct domain *d) d->sched->sleep(d); - if (!__already_locked) - spin_unlock(&d->sched->sched_data.schedule_lock); + sched_unlock(&d->sched->sched_data, taken); } /* - * Set a domain sleeping. The domain state is set to blocked. + * Set a domain sleeping. The domain state is set to blocked, and the caller + * waits for the domain to leave its CPU, which the sleep asked that CPU to + * schedule for. A wait beyond any scheduling tick is a stall: it is told + * once, with what each CPU is at, and waited on all the same. */ void vcpu_sleep_sync(struct domain *d) { + u64 since = arch_counter_get_cntvct(); + bool told = false; + vcpu_sleep_nosync(d); - while (d->is_running) + while (d->is_running) { cpu_relax(); + + if (!told && (arch_counter_get_cntvct() - since > 2 * (u64) arch_timer_get_cntfrq())) { + told = true; + printk("%s: CPU%d has waited 2 s for domain %d to leave CPU%d: runstate %d, pause count %d, CPU%d runs domain %d, scheduler lock %s, lock waits %lu\n", + __func__, smp_processor_id(), d->avz_shared->domID, d->processor, d->runstate, + atomic_read(&d->pause_count), d->processor, + per_cpu(current_domain, d->processor) ? per_cpu(current_domain, d->processor)->avz_shared->domID : -1, + spin_is_locked(&d->sched->sched_data.schedule_lock) ? "held" : "free", sched_lock_contended()); + } + } } /* @@ -267,10 +327,11 @@ static void domain_schedule(void) cpu_do_idle(); } - /* To avoid that another CPU manipulates scheduler data structures */ - /* Maybe the lock is already acquired from do_sleep() for example */ - if (!spin_is_locked(¤t_domain->sched->sched_data.schedule_lock)) - spin_lock(¤t_domain->sched->sched_data.schedule_lock); + /* Against the other CPUs, and not again when an outer path on this CPU + * holds it: the context switch, or the no-op path below, releases it. + */ + + sched_lock(¤t_domain->sched->sched_data); sd = ¤t_domain->sched->sched_data; @@ -288,7 +349,7 @@ static void domain_schedule(void) #endif /* CONFIG_SOO */ if (unlikely(prev == next)) { - spin_unlock(&prev->sched->sched_data.schedule_lock); + sched_unlock(&prev->sched->sched_data, true); ASSERT(prev->runstate == RUNSTATE_running); return; From 2874cdf06cf6cb478aedbc8e98574c1cd7d296df Mon Sep 17 00:00:00 2001 From: Daniel Rossier Date: Tue, 10 Nov 2026 10:58:46 +0100 Subject: [PATCH 2/5] avz: a virq is sent under the event lock alone, after which the scheduler's lock comes Sending a virq took the scheduler's lock, then the event lock; sending an event over a channel takes the event lock, then, as the pending bit is set and the domain unparked, the scheduler's lock. Two orders of the same two locks: the capsule CPU sending a capsule its timer tick met an agency CPU notifying that capsule's ring, each holding the lock the other waited for. Before 1df511970b the second taker of the scheduler's lock went on without it, so the inversion never blocked; from 1df511970b on it waits, and the agency CPU hung in the hypercall with its interrupts off, so that the node's Linux reported the CPU stalled and the agency fell silent. The virq is now sent under the event lock alone: setting the event pending unparks the domain under the scheduler's lock itself, so the one order is the event lock first, the scheduler's lock after, on every path; the scheduler's lock covers only timer and softirq locks beneath it. Seen in every run of the first validation gate of 1df511970b (h1-*): the agency CPUs of most drones stalled within the first minutes, in Full runs as in Delta ones. --- so3/so3/avz/kernel/event_channel.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/so3/so3/avz/kernel/event_channel.c b/so3/so3/avz/kernel/event_channel.c index 819681cf4..e45560852 100644 --- a/so3/so3/avz/kernel/event_channel.c +++ b/so3/so3/avz/kernel/event_channel.c @@ -377,7 +377,6 @@ void send_guest_virq(struct domain *d, int virq) { unsigned long flags; int evtchn; - bool taken; flags = spin_lock_irqsave(&d->virq_lock); evtchn = d->virq_to_evtchn[virq]; @@ -385,7 +384,10 @@ void send_guest_virq(struct domain *d, int virq) if (unlikely(evtchn == 0)) goto out; - taken = sched_lock(&d->sched->sched_data); + /* Under the event lock alone, as evtchn_send(): the unparking that + * setting the event pending ends with takes the scheduler's lock, so + * that lock comes after the event lock, never before it. + */ spin_lock(&d->event_lock); @@ -393,8 +395,6 @@ void send_guest_virq(struct domain *d, int virq) spin_unlock(&d->event_lock); - sched_unlock(&d->sched->sched_data, taken); - out: spin_unlock_irqrestore(&d->virq_lock, flags); } From 4d2fc666c9186c3d612162499691a94966aee4f8 Mon Sep 17 00:00:00 2001 From: Daniel Rossier Date: Fri, 13 Nov 2026 23:27:29 +0100 Subject: [PATCH 3/5] avz: take and release a scheduler lock with its holder mark, interrupts off sched_lock() took the lock and then wrote the per-CPU mark of its holder; sched_unlock() cleared the mark and then released the lock. An interrupt between the two steps ran the scheduling softirq, which read the mark, concluded that the lock was not its own and waited for it on the CPU that held it: the CPU spun forever with interrupts masked. Seen at boot: the agency never printed after "unpausing the agency domain". A stopped guest showed CPU0 in spin_lock() on the agency scheduler's lock, called from domain_schedule() <- do_softirq() <- irq_handle(), the interrupt taken at the first instruction of spin_unlock() in sched_unlock() called by vcpu_wake() <- domain_unpause(); the lock word was 1 and CPU0's mark NULL. The other CPUs were idle. Frequency: about 1 guest boot in 4200 over the campaigns, once in 132 boots in a loop of bare boots. Both steps are now done with the local interrupts masked; waiting for the lock still happens with interrupts as the caller had them. Check: 400 loops of 11 bare boots (4400 guest boots) with the fix, no freeze. Limits: the same window existed for any caller with interrupts enabled, not only at boot; no such freeze was identified in a run. The check bounds the frequency and cannot prove absence: at the campaigns' rate of 1 in 4200, 4400 clean boots would occur by chance about one time in three; at the loop's own rate before the fix (one freeze in 132 boots, a single sample) they would not. --- so3/so3/avz/kernel/schedule.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/so3/so3/avz/kernel/schedule.c b/so3/so3/avz/kernel/schedule.c index 3fe0611d4..acee31c89 100644 --- a/so3/so3/avz/kernel/schedule.c +++ b/so3/so3/avz/kernel/schedule.c @@ -54,14 +54,23 @@ static DEFINE_PER_CPU(unsigned long, sched_lock_waited); */ bool sched_lock(struct schedule_data *sd) { + unsigned long flags; + if (this_cpu(sched_lock_held) == &sd->schedule_lock) return false; if (spin_is_locked(&sd->schedule_lock)) this_cpu(sched_lock_waited)++; - spin_lock(&sd->schedule_lock); + /* + * The lock and the mark of its holder change together as seen from + * this CPU's interrupts: the scheduling softirq reads the mark to know + * whether the lock is its own. + */ + + flags = spin_lock_irqsave(&sd->schedule_lock); this_cpu(sched_lock_held) = &sd->schedule_lock; + local_irq_restore(flags); return true; } @@ -72,11 +81,15 @@ bool sched_lock(struct schedule_data *sd) */ void sched_unlock(struct schedule_data *sd, bool taken) { + unsigned long flags; + if (!taken || (this_cpu(sched_lock_held) != &sd->schedule_lock)) return; + flags = local_irq_save(); this_cpu(sched_lock_held) = NULL; spin_unlock(&sd->schedule_lock); + local_irq_restore(flags); } /** From 15d0236db350f463df174328f42bbf52772f2344 Mon Sep 17 00:00:00 2001 From: Daniel Rossier Date: Sat, 14 Nov 2026 09:59:14 +0100 Subject: [PATCH 4/5] CHANGELOG: the scheduler lock fixes in the v6.4.0 entry --- CHANGELOG | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 965e4aed9..035892328 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -18,7 +18,10 @@ POSIX networking calls, and the build system aligned on Infrabase. packet); the physical EOImode is no longer mirrored into the GICv2 VMCR; 128 grant pages per domain; the stage-2 TLBs are flushed when the screen changes capsule; an idle capsule leaves the capsule CPU to the others and - a paused one stays off the scheduler when an event arrives. A Raspberry + a paused one stays off the scheduler when an event arrives; a scheduler's + lock has one holder which the other CPUs wait for, taken and released + with its holder mark under masked interrupts (an interrupt in between + froze the CPU at boot, about one boot in 4200). A Raspberry Pi 5 (BCM2712) platform skeleton is added; it builds but has not been validated on a board yet. From 786d141477e58c67eaba0662c82a1c9ee9193a96 Mon Sep 17 00:00:00 2001 From: Daniel Rossier Date: Sat, 14 Nov 2026 10:38:19 +0100 Subject: [PATCH 5/5] avz: clang-format 19 on the scheduler's wait report --- so3/so3/avz/kernel/schedule.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/so3/so3/avz/kernel/schedule.c b/so3/so3/avz/kernel/schedule.c index acee31c89..bea149f58 100644 --- a/so3/so3/avz/kernel/schedule.c +++ b/so3/so3/avz/kernel/schedule.c @@ -292,7 +292,9 @@ void vcpu_sleep_sync(struct domain *d) printk("%s: CPU%d has waited 2 s for domain %d to leave CPU%d: runstate %d, pause count %d, CPU%d runs domain %d, scheduler lock %s, lock waits %lu\n", __func__, smp_processor_id(), d->avz_shared->domID, d->processor, d->runstate, atomic_read(&d->pause_count), d->processor, - per_cpu(current_domain, d->processor) ? per_cpu(current_domain, d->processor)->avz_shared->domID : -1, + per_cpu(current_domain, d->processor) ? + per_cpu(current_domain, d->processor)->avz_shared->domID : + -1, spin_is_locked(&d->sched->sched_data.schedule_lock) ? "held" : "free", sched_lock_contended()); } }