diff --git a/arch/x86/xen/smp.c b/arch/x86/xen/smp.c index 05f92c812ac882..537aaacc397248 100644 --- a/arch/x86/xen/smp.c +++ b/arch/x86/xen/smp.c @@ -4,8 +4,14 @@ #include #include #include +#include + +#include +#include +#include #include +#include #include #include "xen-ops.h" @@ -266,3 +272,80 @@ static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id) return IRQ_HANDLED; } + +/* How long a CPU waiting in stop_machine blocks before it looks again. */ +#define XEN_STOP_MACHINE_POLL_NS NSEC_PER_MSEC + +/* + * Xen system time from this vCPU's time info only. pvclock_clocksource_read() + * would also move the clamp shared by all CPUs, which is wrong while a resume + * has not yet re-registered the time info of the CPUs waiting here. + */ +static notrace u64 xen_stop_machine_now(const struct vcpu_info *vcpu) +{ + const struct pvclock_vcpu_time_info *src = &vcpu->time; + unsigned int version; + u64 now; + + do { + version = pvclock_read_begin(src); + now = __pvclock_read_cycles(src, rdtsc_ordered()); + } while (pvclock_read_retry(src, version)); + + return now; +} + +/* + * A CPU waiting in stop_machine spins until the other CPUs taking part have + * caught up. While one of them is not running, the spinning only uses the + * physical CPU time, and under a credit2 cap the domain budget, that the + * missing CPU needs to catch up. Block in Xen instead, until the timeout or + * a new event for this domain. + * + * The CPUs taking part are those online and those in @cpumask: a CPU going + * offline leaves cpu_online_mask before it acknowledges the step that takes + * it down, and a CPU coming online is not in it yet. stop_two_cpus() has + * only two CPUs taking part, so an idle CPU elsewhere also makes it block. + * + * A CPU blocked here reads as not running, so once one waiter blocks, the + * others block too, and the rest of the stop_machine advances on the next + * event for the domain, or at the latest at the timeout. The waiters then use + * a capped budget only for one wake per timeout each, which a very small cap + * still feels, and a stop_machine on an uncapped domain slows down by up to a + * few timeouts. + * + * Until a resume re-registers a CPU's runstate area and vcpu_info, both hold + * what the old host last wrote: every CPU reads as not running and the + * timeout is in the old host's time, so the waiters can block until the next + * event for the domain. + * + * A vCPU that Xen deschedules in user mode (PV, or PVH and HVM with page + * table isolation) can read as running until Xen next updates its runstate + * from kernel mode; waiting for such a CPU still spins. + * + * This relies on Xen's SCHEDOP_poll: an absolute timeout, and a wake on any + * new event for the domain. KVM's Xen emulation reads the timeout as + * relative and wakes a poller with interrupts off on few events, so there a + * waiter can sleep for a very long time and stop_machine can hang once the + * host preempts a vCPU. Such guests are not supported. + */ +notrace void stop_machine_yield(const struct cpumask *cpumask) +{ + struct sched_poll poll = {}; + struct vcpu_info *vcpu; + unsigned int cpu; + + /* Resume clears xen_vcpu for a moment on vCPUs from MAX_VIRT_CPUS up. */ + vcpu = xen_domain() ? __this_cpu_read(xen_vcpu) : NULL; + if (vcpu) { + for_each_cpu_or(cpu, cpu_online_mask, cpumask) { + if (xen_vcpu_running(cpu)) + continue; + poll.timeout = xen_stop_machine_now(vcpu) + + XEN_STOP_MACHINE_POLL_NS; + HYPERVISOR_sched_op(SCHEDOP_poll, &poll); + return; + } + } + cpu_relax(); +} diff --git a/drivers/xen/time.c b/drivers/xen/time.c index 5683383d230529..f53aa06716efc7 100644 --- a/drivers/xen/time.c +++ b/drivers/xen/time.c @@ -142,6 +142,17 @@ bool xen_vcpu_stolen(int vcpu) return per_cpu(xen_runstate, vcpu).state == RUNSTATE_runnable; } +/* + * return true unless xen last recorded the vcpu as not running; a vcpu + * descheduled in user mode can read as running until xen next updates its + * runstate from kernel mode, and a vcpu whose runstate area was never + * registered always reads as running + */ +notrace bool xen_vcpu_running(int vcpu) +{ + return per_cpu(xen_runstate, vcpu).state == RUNSTATE_running; +} + u64 xen_steal_clock(int cpu) { struct vcpu_runstate_info state; diff --git a/include/xen/xen-ops.h b/include/xen/xen-ops.h index 496e6013c689f9..3e0dfb54e7a13c 100644 --- a/include/xen/xen-ops.h +++ b/include/xen/xen-ops.h @@ -33,6 +33,7 @@ void xen_reboot(int reason); void xen_resume_notifier_register(struct notifier_block *nb); bool xen_vcpu_stolen(int vcpu); +bool xen_vcpu_running(int vcpu); void xen_setup_runstate_info(int cpu); void xen_time_setup_guest(void); void xen_manage_runstate_time(int action);