From nobody Sun Feb 8 22:05:48 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 63BB2377573 for ; Thu, 29 Jan 2026 23:29:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769729347; cv=none; b=nSVODjcsUn67EYXOyN3mv/038CBsQNDPosLPmmtYDRPRd/yDcDIJAgOFFF3GUCGctfvvPzKt+RzxQiqpotEWGCyn7uzxIb6/hEOSNK9pkpByDmoBgCwtcDKkrU7J7SvCcR2PwLF27yBYAhPhUNp9eWFyt+t9DgR/N6waSwfmBUM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769729347; c=relaxed/simple; bh=FT7tqsXR7z8CLijk8zuS07pg9saPtM/Y6JJH09YC9j8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=odN2NNcboIAr9ujWRz5pomPTUbQTqj+fp7iynWEHqtTzm37jtkjzFufcjedDZGTQJZh2RJ7x93mOnXmbn7SzrT7hv/wYHHMK3FQWxbAaNXwSb0la/Lo+Tzvn9dVx91zMnx7BQ1FKXkzYhw7N8SRYo9pWM1w+hWHfj/DzQ7+Zr8o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jmattson.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=iPcGbahZ; arc=none smtp.client-ip=209.85.216.73 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--jmattson.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="iPcGbahZ" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-34e5a9de94bso3170300a91.0 for ; Thu, 29 Jan 2026 15:29:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769729345; x=1770334145; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Srv37W6IxsjnZSGJ2r5eoka37P7txkKUnZCQigLVbTg=; b=iPcGbahZN2rkYw9FBb32VCwEX+fmwxGAEUTZhSc2rmsa7HL93PFbgog8Z8uvwCFNV3 jbMV9GHG80Fk60ImHlewQEb+thXE5JfG1b7bE4aWkdTyBAar2Ke0Y6Xeb+Cj8sVKugEt 8uYBYmsz8PfTYSMICl+z6kt1yOJgve/GzmEOZQxcxsJM07iz2K6GwsO3IM7UCTjA22Sd QiuA+U0PZBwqXqYbr6zFoJouMj/tj8m882X8Sa8x0NQb2W5xu2qmyfdJ1hDPwwAjxiwR neomTKbV2lkUxb9mA3DxyxCunFJIVp/nK3ZzQvpe7aF61KdruxF03UQbFnmnFfLvB2Cv yKqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769729345; x=1770334145; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Srv37W6IxsjnZSGJ2r5eoka37P7txkKUnZCQigLVbTg=; b=CGjChVy6uYv+jTH+GN5O+IOtGhRAIQpYqZjOpyAXKcBXC5kwzuYe9FazDu0JMWcmkR pU57VdTwGk5+6HL/a3kc1ezXbggXz4hLVKCbRrtmdFoxMz8jOHeKXFjw0ofD1QUyYSeP U/Zb8Hl0PmBZtWFA9aISpbfLteS84cd5ekbrZU1TcXSXvUeMDibu9ZefZ5ZMS21+5YC/ 4T78RBsXcS/kW3Fxwp7fMv6tjL/l6sBuBYc+ZCWHsbz6vKQlzoH+/vYYnDwndR+NEyg2 uTwr88KBmnpn3yfk6MvVfGEEQht4C5B/dvWqODB7ySnxy9ysLqxLUK+22FlbE5/9OjJD zngg== X-Forwarded-Encrypted: i=1; AJvYcCUAb0k9qG/jZ1nGGguiZy1euaHgeyBeGoz6ta/bL4JCcwiMiI3hcHae1BTZ5xbLSkOCeLGRRdPpjwWpPiI=@vger.kernel.org X-Gm-Message-State: AOJu0Yw0mqZaOfRk/HpT6086KRsQxr3ALAJIAIFHgUWF7cfHflMi2AaL Jj2McdTjJkE8QpoD4Imr766IOmb6G/mZRy3FbmKYsz4q9wnP9DpiYOUgdpeBBXwIedo8CucnQvB 4jJnYvrliR3WexQ== X-Received: from pjbga13.prod.google.com ([2002:a17:90b:38d:b0:349:3867:ccc1]) (user=jmattson job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2883:b0:341:8ac7:39b7 with SMTP id 98e67ed59e1d1-3543b3aa986mr976448a91.25.1769729344811; Thu, 29 Jan 2026 15:29:04 -0800 (PST) Date: Thu, 29 Jan 2026 15:28:08 -0800 In-Reply-To: <20260129232835.3710773-1-jmattson@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129232835.3710773-1-jmattson@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129232835.3710773-4-jmattson@google.com> Subject: [PATCH v2 3/5] KVM: x86/pmu: Refresh Host-Only/Guest-Only eventsel at nested transitions From: Jim Mattson To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , Thomas Gleixner , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Sean Christopherson , Paolo Bonzini , Shuah Khan , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: mizhang@google.com, yosryahmed@google.com, sandipan.das@amd.com, Jim Mattson Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add amd_pmu_refresh_host_guest_eventsel_hw() to recalculate eventsel_hw for all PMCs based on the current vCPU state. This is needed because Host-Only and Guest-Only counters must be enabled/disabled at: - SVME changes: When EFER.SVME is modified, counters with Guest-Only bits need their hardware enable state updated. - Nested transitions: When entering or leaving guest mode, Host-Only counters should be disabled/enabled and Guest-Only counters should be enabled/disabled accordingly. Introduce svm_enter_guest_mode() and svm_leave_guest_mode() wrappers that call enter_guest_mode()/leave_guest_mode() followed by the PMU refresh, ensuring the PMU state stays synchronized with guest mode transitions. Signed-off-by: Jim Mattson --- arch/x86/kvm/svm/nested.c | 6 +++--- arch/x86/kvm/svm/pmu.c | 12 ++++++++++++ arch/x86/kvm/svm/svm.c | 2 ++ arch/x86/kvm/svm/svm.h | 17 +++++++++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index de90b104a0dd..a7d1901f256b 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -757,7 +757,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_s= vm *svm, nested_svm_transition_tlb_flush(vcpu); =20 /* Enter Guest-Mode */ - enter_guest_mode(vcpu); + svm_enter_guest_mode(vcpu); =20 /* * Filled at exit: exit_code, exit_info_1, exit_info_2, exit_int_info, @@ -1136,7 +1136,7 @@ int nested_svm_vmexit(struct vcpu_svm *svm) vmcb12 =3D map.hva; =20 /* Exit Guest-Mode */ - leave_guest_mode(vcpu); + svm_leave_guest_mode(vcpu); svm->nested.vmcb12_gpa =3D 0; WARN_ON_ONCE(svm->nested.nested_run_pending); =20 @@ -1402,7 +1402,7 @@ void svm_leave_nested(struct kvm_vcpu *vcpu) svm->nested.nested_run_pending =3D 0; svm->nested.vmcb12_gpa =3D INVALID_GPA; =20 - leave_guest_mode(vcpu); + svm_leave_guest_mode(vcpu); =20 svm_switch_vmcb(svm, &svm->vmcb01); =20 diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c index 8d451110a94d..e2a849fc7daa 100644 --- a/arch/x86/kvm/svm/pmu.c +++ b/arch/x86/kvm/svm/pmu.c @@ -171,6 +171,18 @@ static void amd_pmu_set_eventsel_hw(struct kvm_pmc *pm= c) pmc->eventsel_hw &=3D ~ARCH_PERFMON_EVENTSEL_ENABLE; } =20 +void amd_pmu_refresh_host_guest_eventsel_hw(struct kvm_vcpu *vcpu) +{ + struct kvm_pmu *pmu =3D vcpu_to_pmu(vcpu); + int i; + + if (pmu->reserved_bits & AMD64_EVENTSEL_HOST_GUEST_MASK) + return; + + for (i =3D 0; i < pmu->nr_arch_gp_counters; i++) + amd_pmu_set_eventsel_hw(&pmu->gp_counters[i]); +} + static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_inf= o) { struct kvm_pmu *pmu =3D vcpu_to_pmu(vcpu); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 5f0136dbdde6..498e098a3df0 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -244,6 +244,8 @@ int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer) if (svm_gp_erratum_intercept && !sev_guest(vcpu->kvm)) set_exception_intercept(svm, GP_VECTOR); } + + amd_pmu_refresh_host_guest_eventsel_hw(vcpu); } =20 svm->vmcb->save.efer =3D efer | EFER_SVME; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index ebd7b36b1ceb..86d4d37bfb08 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -864,6 +864,23 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcp= u, u8 vector); void sev_es_prepare_switch_to_guest(struct vcpu_svm *svm, struct sev_es_sa= ve_area *hostsa); void sev_es_unmap_ghcb(struct vcpu_svm *svm); =20 + +/* pmu.c */ +void amd_pmu_refresh_host_guest_eventsel_hw(struct kvm_vcpu *vcpu); + + +static inline void svm_enter_guest_mode(struct kvm_vcpu *vcpu) +{ + enter_guest_mode(vcpu); + amd_pmu_refresh_host_guest_eventsel_hw(vcpu); +} + +static inline void svm_leave_guest_mode(struct kvm_vcpu *vcpu) +{ + leave_guest_mode(vcpu); + amd_pmu_refresh_host_guest_eventsel_hw(vcpu); +} + #ifdef CONFIG_KVM_AMD_SEV int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp); int sev_mem_enc_register_region(struct kvm *kvm, --=20 2.53.0.rc1.225.gd81095ad13-goog