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. 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..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 __already_locked = false; flags = spin_lock_irqsave(&d->virq_lock); evtchn = d->virq_to_evtchn[virq]; @@ -385,10 +384,10 @@ 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); + /* 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); @@ -396,9 +395,6 @@ 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); - 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..bea149f58 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,77 @@ 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) +{ + 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)++; + + /* + * 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; +} + +/** + * 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) +{ + 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); +} + +/** + * 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 +134,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 +144,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 +187,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 +244,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 +268,36 @@ 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 +342,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 +364,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;