Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH v4 1/1] KVM: x86: Introduce has_protected_pmu state for TDX VMs
@ 2026-05-07 21:29 Vishal Annapurve
  2026-05-12 22:47 ` Sean Christopherson
  0 siblings, 1 reply; 3+ messages in thread
From: Vishal Annapurve @ 2026-05-07 21:29 UTC (permalink / raw)
  To: seanjc, pbonzini, dave.hansen
  Cc: rick.p.edgecombe, dapeng1.mi, mizhang, kai.huang, jmattson, kvm,
	linux-kernel, Vishal Annapurve

PMU state for TDX VMs is virtualized by TDX Module [1]. Host has following
toggles to control the PMU functionality exposed to TDX VMs:
1) Configure TD_PARAMS to allow guests to use performance monitoring.
2) Restrict the TD to a subset of the PEBS counters if supported.
3) Limit the TD to setup a certain perfmon events using basic/enhanced
   event filtering.

KVM will need to be enlightened to support these toggles. Introduce
has_protected_pmu state to track the pmu state for such scenarios
and explicitly set the has_protected_pmu flag for TDX VMs.

If pmu state is protected:
1) Disable KVM's PMU virtualization framework as additional
enlightenment is needed within KVM to control/manage the
visibility of PMU state to such VMs.
2) Disallow userspace VMM from enabling PMU virtualization state
using KVM_CAP_PMU_CAPABILITY.

[1] Section 15.2: https://cdrdv2.intel.com/v1/dl/getContent/733575

Suggested-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Vishal Annapurve <vannapurve@google.com>
---
v3 -> v4:
 - Allow userspace to disable pmu virtualization even if
   has_protected_pmu is set.
 - Addressed feedback around enable_pmu initialization in
   kvm_arch_init_vm().

v3: https://lore.kernel.org/kvm/20260507142402.2175933-1-vannapurve@google.com/

 arch/x86/include/asm/kvm_host.h | 1 +
 arch/x86/kvm/vmx/tdx.c          | 6 ++++++
 arch/x86/kvm/x86.c              | 9 ++++++++-
 3 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index c470e40a00aa..8371dcaaed1a 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1422,6 +1422,7 @@ struct kvm_arch {
 	bool has_private_mem;
 	bool has_protected_state;
 	bool has_protected_eoi;
+	bool has_protected_pmu;
 	bool pre_fault_allowed;
 	struct hlist_head *mmu_page_hash;
 	struct list_head active_mmu_pages;
diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index 1e47c194af53..eb4b4518e6f0 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -638,6 +638,12 @@ int tdx_vm_init(struct kvm *kvm)
 	kvm->arch.has_private_mem = true;
 	kvm->arch.disabled_quirks |= KVM_X86_QUIRK_IGNORE_GUEST_PAT;
 
+	/*
+	 * PMU support is provided by the TDX-Module (if enabled for the VM).
+	 * From KVM's perspective, the VM doesn't have a virtual PMU.
+	 */
+	kvm->arch.has_protected_pmu = true;
+
 	/*
 	 * Because guest TD is protected, VMM can't parse the instruction in TD.
 	 * Instead, guest uses MMIO hypercall.  For unmodified device driver,
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 0a1b63c63d1a..c248abb0e2ab 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -6909,6 +6909,13 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 		if (!enable_pmu || (cap->args[0] & ~KVM_CAP_PMU_VALID_MASK))
 			break;
 
+		if (kvm->arch.has_protected_pmu) {
+			WARN_ON_ONCE(kvm->arch.enable_pmu);
+			if (cap->args[0] == KVM_PMU_CAP_DISABLE)
+				r = 0;
+			break;
+		}
+
 		mutex_lock(&kvm->lock);
 		if (!kvm->created_vcpus && !kvm->arch.created_mediated_pmu) {
 			kvm->arch.enable_pmu = !(cap->args[0] & KVM_PMU_CAP_DISABLE);
@@ -13375,7 +13382,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	kvm->arch.default_tsc_khz = max_tsc_khz ? : tsc_khz;
 	kvm->arch.apic_bus_cycle_ns = APIC_BUS_CYCLE_NS_DEFAULT;
 	kvm->arch.guest_can_read_msr_platform_info = true;
-	kvm->arch.enable_pmu = enable_pmu;
+	kvm->arch.enable_pmu = enable_pmu && !kvm->arch.has_protected_pmu;
 
 #if IS_ENABLED(CONFIG_HYPERV)
 	spin_lock_init(&kvm->arch.hv_root_tdp_lock);
-- 
2.54.0.563.g4f69b47b94-goog


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v4 1/1] KVM: x86: Introduce has_protected_pmu state for TDX VMs
  2026-05-07 21:29 [PATCH v4 1/1] KVM: x86: Introduce has_protected_pmu state for TDX VMs Vishal Annapurve
@ 2026-05-12 22:47 ` Sean Christopherson
  2026-05-13  0:15   ` Vishal Annapurve
  0 siblings, 1 reply; 3+ messages in thread
From: Sean Christopherson @ 2026-05-12 22:47 UTC (permalink / raw)
  To: Vishal Annapurve
  Cc: pbonzini, dave.hansen, rick.p.edgecombe, dapeng1.mi, mizhang,
	kai.huang, jmattson, kvm, linux-kernel

On Thu, May 07, 2026, Vishal Annapurve wrote:
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 0a1b63c63d1a..c248abb0e2ab 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -6909,6 +6909,13 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
>  		if (!enable_pmu || (cap->args[0] & ~KVM_CAP_PMU_VALID_MASK))
>  			break;
>  
> +		if (kvm->arch.has_protected_pmu) {
> +			WARN_ON_ONCE(kvm->arch.enable_pmu);
> +			if (cap->args[0] == KVM_PMU_CAP_DISABLE)
> +				r = 0;
> +			break;

Sashiko: 3, Sean: 0

 : Does this code bypass standard lifecycle validation?
 : 
 : For standard VMs, KVM strictly enforces that PMU capabilities can only be
 : configured before any vCPUs are created, returning -EINVAL otherwise. The newly
 : added block for TDX VMs short-circuits this logic, returning 0 unconditionally
 : when disabling the PMU, regardless of whether vCPUs have been created.
 : Could this introduce a UAPI inconsistency where the same userspace sequence
 : could fail on standard VMs but silently succeed on TDX VMs, potentially
 : hiding VMM initialization order bugs?

That potential issue crossed my mind when first trying to figure out how to allow
KVM_PMU_CAP_DISABLE for protected PMUs, but then SQUIRREL!!!

I think my original reaction is the way to go: add extra validation for protected
PMUs (reject everything except CAP_DISABLE), but otherwise let the normal flow do
it's thing.

Completely untested, but I think this would do what we want?

	case KVM_CAP_PMU_CAPABILITY:
		r = -EINVAL;
		if (!enable_pmu || (cap->args[0] & ~KVM_CAP_PMU_VALID_MASK))
			break;

		if (kvm->arch.has_protected_pmu &&
		    cap->args[0] != KVM_PMU_CAP_DISABLE)
			break;

		mutex_lock(&kvm->lock);
		if (!kvm->created_vcpus && !kvm->arch.created_mediated_pmu) {
			kvm->arch.enable_pmu = !(cap->args[0] & KVM_PMU_CAP_DISABLE);
			r = 0;
		}
		mutex_unlock(&kvm->lock);
		break;

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v4 1/1] KVM: x86: Introduce has_protected_pmu state for TDX VMs
  2026-05-12 22:47 ` Sean Christopherson
@ 2026-05-13  0:15   ` Vishal Annapurve
  0 siblings, 0 replies; 3+ messages in thread
From: Vishal Annapurve @ 2026-05-13  0:15 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: pbonzini, dave.hansen, rick.p.edgecombe, dapeng1.mi, mizhang,
	kai.huang, jmattson, kvm, linux-kernel

On Tue, May 12, 2026 at 3:47 PM Sean Christopherson <seanjc@google.com> wrote:
>
> On Thu, May 07, 2026, Vishal Annapurve wrote:
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > index 0a1b63c63d1a..c248abb0e2ab 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> > @@ -6909,6 +6909,13 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
> >               if (!enable_pmu || (cap->args[0] & ~KVM_CAP_PMU_VALID_MASK))
> >                       break;
> >
> > +             if (kvm->arch.has_protected_pmu) {
> > +                     WARN_ON_ONCE(kvm->arch.enable_pmu);
> > +                     if (cap->args[0] == KVM_PMU_CAP_DISABLE)
> > +                             r = 0;
> > +                     break;
>
> Sashiko: 3, Sean: 0
>
>  : Does this code bypass standard lifecycle validation?
>  :
>  : For standard VMs, KVM strictly enforces that PMU capabilities can only be
>  : configured before any vCPUs are created, returning -EINVAL otherwise. The newly
>  : added block for TDX VMs short-circuits this logic, returning 0 unconditionally
>  : when disabling the PMU, regardless of whether vCPUs have been created.
>  : Could this introduce a UAPI inconsistency where the same userspace sequence
>  : could fail on standard VMs but silently succeed on TDX VMs, potentially
>  : hiding VMM initialization order bugs?
>
> That potential issue crossed my mind when first trying to figure out how to allow
> KVM_PMU_CAP_DISABLE for protected PMUs, but then SQUIRREL!!!
>
> I think my original reaction is the way to go: add extra validation for protected
> PMUs (reject everything except CAP_DISABLE), but otherwise let the normal flow do
> it's thing.
>
> Completely untested, but I think this would do what we want?

SGTM. Will include this suggestion in the next version.

>
>         case KVM_CAP_PMU_CAPABILITY:
>                 r = -EINVAL;
>                 if (!enable_pmu || (cap->args[0] & ~KVM_CAP_PMU_VALID_MASK))
>                         break;
>
>                 if (kvm->arch.has_protected_pmu &&
>                     cap->args[0] != KVM_PMU_CAP_DISABLE)
>                         break;
>
>                 mutex_lock(&kvm->lock);
>                 if (!kvm->created_vcpus && !kvm->arch.created_mediated_pmu) {
>                         kvm->arch.enable_pmu = !(cap->args[0] & KVM_PMU_CAP_DISABLE);
>                         r = 0;
>                 }
>                 mutex_unlock(&kvm->lock);
>                 break;

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-05-13  0:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-07 21:29 [PATCH v4 1/1] KVM: x86: Introduce has_protected_pmu state for TDX VMs Vishal Annapurve
2026-05-12 22:47 ` Sean Christopherson
2026-05-13  0:15   ` Vishal Annapurve

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox