Re: [RFC PATCH 2/2] kvm,x86: Report preempt_count to host.

From: Vitaly Kuznetsov
Date: Wed Jul 28 2021 - 04:18:49 EST


Suleiman Souhlal <suleiman@xxxxxxxxxx> writes:

> When KVM_PREEMPT_COUNT_REPORTING is enabled, the host can use
> preempt_count to determine if the guest is in a critical section,
> if it also has CONFIG_KVM_HETEROGENEOUS_RT enabled, in order to
> use heterogeneous RT VCPU configurations.
>
> Signed-off-by: Suleiman Souhlal <suleiman@xxxxxxxxxx>
> ---
> arch/x86/Kconfig | 11 +++++++++++
> arch/x86/kernel/kvm.c | 10 ++++++++++
> 2 files changed, 21 insertions(+)
>
> diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
> index 49270655e827..d8b62789df57 100644
> --- a/arch/x86/Kconfig
> +++ b/arch/x86/Kconfig
> @@ -846,6 +846,17 @@ config PARAVIRT_TIME_ACCOUNTING
> config PARAVIRT_CLOCK
> bool
>
> +config KVM_PREEMPT_COUNT_REPORTING
> + bool "KVM preempt_count reporting to the host"
> + depends on KVM_GUEST && PREEMPT_COUNT
> + default n
> + help
> + Select this option to enable KVM preempt_count reporting to the host,
> + which can be useful in cases where some VCPUs are RT and the rest
> + aren't.
> +
> + If in doubt, say N here.
> +
> config JAILHOUSE_GUEST
> bool "Jailhouse non-root cell support"
> depends on X86_64 && PCI
> diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c
> index a26643dc6bd6..7ec53ea3f979 100644
> --- a/arch/x86/kernel/kvm.c
> +++ b/arch/x86/kernel/kvm.c
> @@ -363,6 +363,16 @@ static void kvm_guest_cpu_init(void)
>
> if (has_steal_clock)
> kvm_register_steal_time();
> +
> +#ifdef CONFIG_KVM_PREEMPT_COUNT_REPORTING
> + if (kvm_para_has_feature(KVM_FEATURE_PREEMPT_COUNT)) {
> + unsigned long pa;
> +
> + pa = slow_virt_to_phys(this_cpu_ptr(&__preempt_count)) |
> + KVM_MSR_ENABLED;
> + wrmsrl(MSR_KVM_PREEMPT_COUNT, pa);
> + }
> +#endif
> }

Please also disable the feature in kvm_guest_cpu_offline() as e.g. upon
kexec the memory address looses its meaning.

>
> static void kvm_pv_disable_apf(void)

--
Vitaly