From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 763ED46AF3F for ; Fri, 22 May 2026 15:15:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779462950; cv=none; b=k9R5vatZOgLg/R2cYal9hlUZeA7fy6PRFf0YxZwgUGWG/oXhrnxaF3nB9BYNhMDP9SWKgHSy/tkIuFF9N4rxf1fvN3qOGhc2gpTIvG4yqkidEwpKMfVzj9t54AQqxbpGOzgHrMV1AxtcFezt0CJDsstEJ8j08i8azQknsRCIyvw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779462950; c=relaxed/simple; bh=9g8EAfGvDG1p08Pmtu91DH9geQTr1Eg1+earq8LfPG4=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=lkiRPYx/B3e++VIvd+0KRsDB2olBJSvRfD5UmtlBgeTcNwpCjCHr1UZGZblDU5PWPxOunJkwI9TXpayQekw35PRX5oR2kVNjnaovWudiBbMjwBgw6C2sW6FpHLdpmoWsZ1Xt5sqjeBKoIggwReFq6MgyR3fpV0dLvVXAha+iOvs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vannapurve.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vfd+qkAP; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vannapurve.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vfd+qkAP" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2b99eb06178so175152625ad.2 for ; Fri, 22 May 2026 08:15:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1779462937; x=1780067737; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=J8KQAP9mWp0ZRebZLa5bEvFOR8JqQ907ANUwCS1BhIk=; b=vfd+qkAPjFOttcXas/Ji9M35M4Rw8hcV5fTyPE3i/qz6Urtu59oiZWjVnliJDV8b8X nXc9ju8hf7FIeD4KxoV5671zCCTo2KNxg28Cmp4AowLYke7YszNsR6TOu0LU29I05LHu NSjtge7CzHic4JZstW7NiIi5zCq0R9rSyguhEBY1sHF3mhEiONnewRhq/IygjhEMo45+ UqI62LjC+u9pJ3cidfHtOwJCng5tWfNVW3jIAdm2IrU+XE1LeLKh+9GOP76Pkzbyv+tT /XN9Xrdhft0Grt9ieG4VlRCvjwukUwPKDX1O1aJ7qZg6qvWELuu5Nlts4Y0ezfW/P1jP usZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779462937; x=1780067737; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=J8KQAP9mWp0ZRebZLa5bEvFOR8JqQ907ANUwCS1BhIk=; b=FzP+J9VJ5JOU1lz+deRP67ncfmM5vbm22qqcU3qvY63AKTeHQYTsPkYbpXOIIIt0pZ RsjFJ3Xh1NifRXp6pjdjn4M9i7SxFvCUZAOA5oPYsKIxySL1xelzQNfdkR3IuTfHYFmJ /ADC+9SVP6/UGXiO/ApG78NR2C59RkNN2a49QtBPIgU29T3X+j4xzaZh8QKx0ImZTR0N 4EZ5cHlE1ob+lhVAQU2bBq/OaWC3GXfzb4N1m72Ggvx8IlSPDzx8PmAcXRgkra2L+aV6 h7Jl42/BEIrcTB+9UsZFb/URyQkQbjAnqwn+ZlmwZS3pQXMCmpD8oHtqJsBFcom+6xSs /VWw== X-Forwarded-Encrypted: i=1; AFNElJ+M1PgzE5t/5oNjm8fu15ik7YRRIinvVw+p3Ecx8GTIq8T27J/XxOH06+mfhUKTz66J+I3fENXCfWLQpNA=@vger.kernel.org X-Gm-Message-State: AOJu0YzxaBusl1dJ41VZSnRyUbKJWCKiQXr/xOo2uUnq1ilHcYUAuAwy vUiQXcwTO7Zb/H/xbMQi3KFgr3lLD1mXeWLDLbir33a17LK+ewZAXCmK3wLIJ372AlYwaG8OLIh SwPAKPXqc15QOhqQUgW7deQ== X-Received: from pleg14.prod.google.com ([2002:a17:902:e38e:b0:2b4:58ed:55d2]) (user=vannapurve job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:f70e:b0:2b2:5503:1b8d with SMTP id d9443c01a7336-2beb0347058mr43525335ad.6.1779462936728; Fri, 22 May 2026 08:15:36 -0700 (PDT) Date: Fri, 22 May 2026 15:15:34 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.54.0.746.g67dd491aae-goog Message-ID: <20260522151534.652522-1-vannapurve@google.com> Subject: [PATCH v5 1/1] KVM: x86: Introduce has_protected_pmu state for TDX VMs From: Vishal Annapurve To: seanjc@google.com, pbonzini@redhat.com, dave.hansen@linux.intel.com Cc: rick.p.edgecombe@intel.com, dapeng1.mi@linux.intel.com, mizhang@google.com, kai.huang@intel.com, jmattson@google.com, michael.roth@amd.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vishal Annapurve Content-Type: text/plain; charset="UTF-8" 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 Signed-off-by: Vishal Annapurve --- v4 -> v5: - Allow userspace to disable PMU virtualization before creating any vcpus inline with existing ABI contract for KVM_CAP_PMU_CAPABILITY. v4: https://lore.kernel.org/kvm/20260507212957.2629561-1-vannapurve@google.com/ arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/vmx/tdx.c | 6 ++++++ arch/x86/kvm/x86.c | 6 +++++- 3 files changed, 12 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..733674ea0d2a 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -6909,6 +6909,10 @@ 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 && + 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); @@ -13375,7 +13379,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.746.g67dd491aae-goog