lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <CAAhV-H4AkF3pYN31ADCnMLVirDcEhMjyr+FekBz1pQ+aggh9FQ@mail.gmail.com>
Date: Sat, 22 Nov 2025 10:39:20 +0800
From: Huacai Chen <chenhuacai@...nel.org>
To: Bibo Mao <maobibo@...ngson.cn>
Cc: WANG Xuerui <kernel@...0n.name>, kvm@...r.kernel.org, loongarch@...ts.linux.dev, 
	linux-kernel@...r.kernel.org
Subject: Re: [PATCH] LoongArch: KVM: Get VM PMU capability from HW GCFG register

Applied with some small changes, thanks.

Huacai

On Tue, Sep 30, 2025 at 5:37 PM Bibo Mao <maobibo@...ngson.cn> wrote:
>
> Now VM PMU capability comes from host PMU capability directly, instead
> bit 23 of HW GCFG CSR register also show PMU capability for VM. It
> will be better if it comes from HW GCFG CSR register rather than host
> PMU capability, especially when LVZ function is emulated in TCG mode,
> however without PMU capability.
>
> Signed-off-by: Bibo Mao <maobibo@...ngson.cn>
> ---
>  arch/loongarch/include/asm/kvm_host.h  |  8 +++++++
>  arch/loongarch/include/asm/loongarch.h |  2 ++
>  arch/loongarch/kvm/vm.c                | 30 +++++++++++++++++---------
>  3 files changed, 30 insertions(+), 10 deletions(-)
>
> diff --git a/arch/loongarch/include/asm/kvm_host.h b/arch/loongarch/include/asm/kvm_host.h
> index 0cecbd038bb3..392480c9b958 100644
> --- a/arch/loongarch/include/asm/kvm_host.h
> +++ b/arch/loongarch/include/asm/kvm_host.h
> @@ -126,6 +126,8 @@ struct kvm_arch {
>         struct kvm_phyid_map  *phyid_map;
>         /* Enabled PV features */
>         unsigned long pv_features;
> +       /* Supported features from KVM */
> +       unsigned long support_features;
>
>         s64 time_offset;
>         struct kvm_context __percpu *vmcs;
> @@ -293,6 +295,12 @@ static inline int kvm_get_pmu_num(struct kvm_vcpu_arch *arch)
>         return (arch->cpucfg[6] & CPUCFG6_PMNUM) >> CPUCFG6_PMNUM_SHIFT;
>  }
>
> +/* Check whether KVM support this feature, however VMM may disable it */
> +static inline bool kvm_vm_support(struct kvm_arch *arch, int feature)
> +{
> +       return !!(arch->support_features & BIT_ULL(feature));
> +}
> +
>  bool kvm_arch_pmi_in_guest(struct kvm_vcpu *vcpu);
>
>  /* Debug: dump vcpu state */
> diff --git a/arch/loongarch/include/asm/loongarch.h b/arch/loongarch/include/asm/loongarch.h
> index 09dfd7eb406e..b640f8f6d7bd 100644
> --- a/arch/loongarch/include/asm/loongarch.h
> +++ b/arch/loongarch/include/asm/loongarch.h
> @@ -510,6 +510,8 @@
>  #define  CSR_GCFG_GPERF_SHIFT          24
>  #define  CSR_GCFG_GPERF_WIDTH          3
>  #define  CSR_GCFG_GPERF                        (_ULCAST_(0x7) << CSR_GCFG_GPERF_SHIFT)
> +#define  CSR_GCFG_GPMP_SHIFT           23
> +#define  CSR_GCFG_GPMP                 (_ULCAST_(0x1) << CSR_GCFG_GPMP_SHIFT)
>  #define  CSR_GCFG_GCI_SHIFT            20
>  #define  CSR_GCFG_GCI_WIDTH            2
>  #define  CSR_GCFG_GCI                  (_ULCAST_(0x3) << CSR_GCFG_GCI_SHIFT)
> diff --git a/arch/loongarch/kvm/vm.c b/arch/loongarch/kvm/vm.c
> index edccfc8c9cd8..735ad20d9ea9 100644
> --- a/arch/loongarch/kvm/vm.c
> +++ b/arch/loongarch/kvm/vm.c
> @@ -6,6 +6,7 @@
>  #include <linux/kvm_host.h>
>  #include <asm/kvm_mmu.h>
>  #include <asm/kvm_vcpu.h>
> +#include <asm/kvm_csr.h>
>  #include <asm/kvm_eiointc.h>
>  #include <asm/kvm_pch_pic.h>
>
> @@ -24,6 +25,23 @@ const struct kvm_stats_header kvm_vm_stats_header = {
>                                         sizeof(kvm_vm_stats_desc),
>  };
>
> +static void kvm_vm_init_features(struct kvm *kvm)
> +{
> +       unsigned long val;
> +
> +       /* Enable all PV features by default */
> +       kvm->arch.pv_features = BIT(KVM_FEATURE_IPI);
> +       kvm->arch.support_features = BIT(KVM_LOONGARCH_VM_FEAT_PV_IPI);
> +       if (kvm_pvtime_supported()) {
> +               kvm->arch.pv_features |= BIT(KVM_FEATURE_STEAL_TIME);
> +               kvm->arch.support_features |= BIT(KVM_LOONGARCH_VM_FEAT_PV_STEALTIME);
> +       }
> +
> +       val = read_csr_gcfg();
> +       if (val & CSR_GCFG_GPMP)
> +               kvm->arch.support_features |= BIT(KVM_LOONGARCH_VM_FEAT_PMU);
> +}
> +
>  int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
>  {
>         int i;
> @@ -42,11 +60,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
>         spin_lock_init(&kvm->arch.phyid_map_lock);
>
>         kvm_init_vmcs(kvm);
> -
> -       /* Enable all PV features by default */
> -       kvm->arch.pv_features = BIT(KVM_FEATURE_IPI);
> -       if (kvm_pvtime_supported())
> -               kvm->arch.pv_features |= BIT(KVM_FEATURE_STEAL_TIME);
> +       kvm_vm_init_features(kvm);
>
>         /*
>          * cpu_vabits means user address space only (a half of total).
> @@ -137,13 +151,9 @@ static int kvm_vm_feature_has_attr(struct kvm *kvm, struct kvm_device_attr *attr
>                         return 0;
>                 return -ENXIO;
>         case KVM_LOONGARCH_VM_FEAT_PMU:
> -               if (cpu_has_pmp)
> -                       return 0;
> -               return -ENXIO;
>         case KVM_LOONGARCH_VM_FEAT_PV_IPI:
> -               return 0;
>         case KVM_LOONGARCH_VM_FEAT_PV_STEALTIME:
> -               if (kvm_pvtime_supported())
> +               if (kvm_vm_support(&kvm->arch, attr->attr))
>                         return 0;
>                 return -ENXIO;
>         default:
>
> base-commit: e5f0a698b34ed76002dc5cff3804a61c80233a7a
> --
> 2.39.3
>
>

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ