From nobody Fri Sep 12 11:36:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 04518C6FD1F for ; Sat, 11 Mar 2023 00:46:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229956AbjCKAqa (ORCPT ); Fri, 10 Mar 2023 19:46:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229814AbjCKAq0 (ORCPT ); Fri, 10 Mar 2023 19:46:26 -0500 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9973810CEAF for ; Fri, 10 Mar 2023 16:46:24 -0800 (PST) Received: by mail-pf1-x449.google.com with SMTP id a10-20020a056a000c8a00b005fc6b117942so3620991pfv.2 for ; Fri, 10 Mar 2023 16:46:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495584; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=e9RojlZ2eL/0VXIqGaaoxFK7jlplkq7U0XdDBwSj9b0=; b=JB4PASMRRmBkJfWFVik95c10a0nFWHxJMxeSDIyWskBg1vNNXKiJJcqABpo2y60OQ7 h5i/BrzTISduqrtNislgZQ7HQQSIR469ahJc6DGzNpo+0ZhXvhr45jUuM5LX8aKmUhAh TZbZfNH5h3H4+9SQMivU5BwRnoAy9/6zmbeBSQe97fjqCa338jJwLiJ3Bsbi08QncREq 8/M0iDvvjtHsrEEL7no0qzAj+oJIUPnN+0G6zxtiY1UrejZd0uUcwwve63W+QXvcAvF4 PWbAqzv8LygI2Rh4p2ngvIM1UFJTAeQZXh5aILnCnAQdJwzPiqPET1zI5jaUZ3iMnHAJ qIcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495584; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=e9RojlZ2eL/0VXIqGaaoxFK7jlplkq7U0XdDBwSj9b0=; b=SK61BOeTQS6YANR+SdM7luZOFImGMI26Sv04dV41BlGfl4VTXtu7+d0AiPh/N7br0H m5lezq36fpYh0gn2EPKkLxpDjqonrULnawKRhWLSdj+yeDJHNUAAnRQZiEb1e9f64bjB 5pjckpn+lIsLNEJGfwV6BPN8ZHoFjJIItMKpmu98RULzaC84AaWurSPs5xYvydjuSObA nLFTw/6K9ozWc4TvkvrRPvdrOqDZCQa7N33WYi4QhOTq/7T0LuflDEDqpZxW3kY+8rHU cbo35egcwcyTwH7gLBp0AJ2JsIq3ppGmnrz2MM6/RB5nLdOvLp8EFOFJbJCpaDviklqo ahcw== X-Gm-Message-State: AO0yUKUHne7nK6q2zjyUKW2uqfo9F+3rYXnLC0rSgh7XpM2ykT8TyL07 jLiafx3UddmtjIfuoph11pvGLOD2ipI= X-Google-Smtp-Source: AK7set9k5FJ5x2cGUuCVUtFIe6AKIb2HNsSgwAS+Cp/2j2PpVT1yAsMCYwhOE5/lzwV4r1vmLT0pjSezYAw= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:9a45:b0:19a:b98f:46a0 with SMTP id x5-20020a1709029a4500b0019ab98f46a0mr1605288plv.0.1678495584165; Fri, 10 Mar 2023 16:46:24 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:45:58 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-2-seanjc@google.com> Subject: [PATCH v3 01/21] KVM: x86: Rename kvm_init_msr_list() to clarify it inits multiple lists From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rename kvm_init_msr_list() to kvm_init_msr_lists() to clarify that it initializes multiple lists: MSRs to save, emulated MSRs, and feature MSRs. No functional change intended. Reviewed-by: Xiaoyao Li Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index f706621c35b8..7b91f73a837d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7071,7 +7071,7 @@ static void kvm_probe_msr_to_save(u32 msr_index) msrs_to_save[num_msrs_to_save++] =3D msr_index; } =20 -static void kvm_init_msr_list(void) +static void kvm_init_msr_lists(void) { unsigned i; =20 @@ -9450,7 +9450,7 @@ static int __kvm_x86_vendor_init(struct kvm_x86_init_= ops *ops) kvm_caps.max_guest_tsc_khz =3D max; } kvm_caps.default_tsc_scaling_ratio =3D 1ULL << kvm_caps.tsc_scaling_ratio= _frac_bits; - kvm_init_msr_list(); + kvm_init_msr_lists(); return 0; =20 out_unwind_ops: --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Fri Sep 12 11:36:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 736F6C6FD1F for ; Sat, 11 Mar 2023 00:46:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230013AbjCKAqe (ORCPT ); Fri, 10 Mar 2023 19:46:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229885AbjCKAq1 (ORCPT ); Fri, 10 Mar 2023 19:46:27 -0500 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6668C139D10 for ; Fri, 10 Mar 2023 16:46:26 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id iy17-20020a170903131100b0019ce046d8f3so3681183plb.23 for ; Fri, 10 Mar 2023 16:46:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495586; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=UCsKlBygaljo69pUMHBC7qjxT0Bam71YbVnLW7zzuTY=; b=bopgSxhYe35UHp/rSiq09olTYF29A1imC6FArKNKFxhlP0K0N0lZ6PLoQ965s5g5Rt NVCHCqPnHGytA8ygpHPGELSoVIp9ofpPVlaUF8GAUXZTHX1yB92x3JO++ljLO35kbtVz rDQ+LNBkzoyarhZecu2C5j5b9GblnHruxlaZU45g+YyY1q63393HbLRSsKxv/bdhth8D i20oGCke767wvQVjjkPHusCU10BkLI2SISNDtkhVDhbsVaA/Oiy6DHfvoE3qcAEdY24M BTGeCxij4o+iNPUCnrCJCqjPtr7fWle3vrLWZiVDTgmi56LSrvUElX2/IvgwFu5nnaUO CtAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495586; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=UCsKlBygaljo69pUMHBC7qjxT0Bam71YbVnLW7zzuTY=; b=LtmlgoCYVFoZyuC2UpSoM01QMNamQ7RkntYKgoOPUjHng5toSsV5PgxeLJ41DKf4Vj xuySgU/1FaGVXWnPkWuzb1jECxEoD2obC/LSRicxewVQoz16CeFUZ6ryYKqkSpGHTyBW HHVI0AXHCTL5uGJRtaDB2XafcfKKBYmKZlHVz6cu40fGYgx6B0puUGHMiYDQM0lXcx8r cUp3qxz9WNYXrE8A/EjOBIByg1CwoJ0TuOWokwVHfaXGnTnlSztU/5zTbDOmPQg/me56 CCaQ90L3TdT5y9hHJbwvyzZOLoMIjZWtspFSiMdn2T5Q7w5Ji8tjJpGu/fbrDtBTVWVj PToA== X-Gm-Message-State: AO0yUKUV5+sdg4eOtYwlr8XYZHJpGkT86zHVFcX+/KvtFb34CUODNRbA +KeQDYrCUMZlp6KhBt3kif7oFKM4F68= X-Google-Smtp-Source: AK7set99ZSXAcHXf00oP45I5uaEIdA6QUlelD/iSNJsNSlIOwPxUisVvKHaY+mfZM1joKZnzP/vR49Q3MdA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:dd86:b0:233:be3d:8a42 with SMTP id l6-20020a17090add8600b00233be3d8a42mr10052365pjv.0.1678495585934; Fri, 10 Mar 2023 16:46:25 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:45:59 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-3-seanjc@google.com> Subject: [PATCH v3 02/21] KVM: x86: Add a helper to query whether or not a vCPU has ever run From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a helper to query if a vCPU has run so that KVM doesn't have to open code the check on last_vmentry_cpu being set to a magic value. No functional change intended. Suggested-by: Xiaoyao Li Cc: Like Xu Reviewed-by: Xiaoyao Li Signed-off-by: Sean Christopherson --- arch/x86/kvm/cpuid.c | 2 +- arch/x86/kvm/mmu/mmu.c | 2 +- arch/x86/kvm/x86.h | 5 +++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 8f8edeaf8177..448d627ce891 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -420,7 +420,7 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct = kvm_cpuid_entry2 *e2, * KVM_SET_CPUID{,2} again. To support this legacy behavior, check * whether the supplied CPUID data is equal to what's already set. */ - if (vcpu->arch.last_vmentry_cpu !=3D -1) { + if (kvm_vcpu_has_run(vcpu)) { r =3D kvm_cpuid_check_equal(vcpu, e2, nent); if (r) return r; diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index c8ebe542c565..e5a6d785d77a 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -5393,7 +5393,7 @@ void kvm_mmu_after_set_cpuid(struct kvm_vcpu *vcpu) * Changing guest CPUID after KVM_RUN is forbidden, see the comment in * kvm_arch_vcpu_ioctl(). */ - KVM_BUG_ON(vcpu->arch.last_vmentry_cpu !=3D -1, vcpu->kvm); + KVM_BUG_ON(kvm_vcpu_has_run(vcpu), vcpu->kvm); } =20 void kvm_mmu_reset_context(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index a8167b47b8c8..754190af1791 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -83,6 +83,11 @@ static inline unsigned int __shrink_ple_window(unsigned = int val, void kvm_service_local_tlb_flush_requests(struct kvm_vcpu *vcpu); int kvm_check_nested_events(struct kvm_vcpu *vcpu); =20 +static inline bool kvm_vcpu_has_run(struct kvm_vcpu *vcpu) +{ + return vcpu->arch.last_vmentry_cpu !=3D -1; +} + static inline bool kvm_is_exception_pending(struct kvm_vcpu *vcpu) { return vcpu->arch.exception.pending || --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Fri Sep 12 11:36:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 639D1C6FD19 for ; Sat, 11 Mar 2023 00:46:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230155AbjCKAqi (ORCPT ); Fri, 10 Mar 2023 19:46:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229994AbjCKAq3 (ORCPT ); Fri, 10 Mar 2023 19:46:29 -0500 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 451E913B95F for ; Fri, 10 Mar 2023 16:46:28 -0800 (PST) Received: by mail-pf1-x44a.google.com with SMTP id p36-20020a056a000a2400b005f72df7d97bso3628745pfh.19 for ; Fri, 10 Mar 2023 16:46:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495588; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=037pUqXeQr7lbfms7Jj3Q4cOJvKKygZOU+H5t+p+MOc=; b=Owkx/G9kjCUuXtpKm84c1IzthMXdx92l6GjzKwTOY0Jh/hetT0yNl2D5JAKzaNCoQj m/0E2Rah7jDMcc3WGS1FqAGQpOBQJmma+gZC6JDfJO1ubESN7hU77dx8hajnvGoPts1+ 4j1ke8oKdBUh7RGTnqKuAaLX4x78Z9eYYvrTiEcHB/Y2DZBuQ9RT9RzfM8dm0gCG5Zpd RwKJvCo7hg0KKhHb3mKyFsf/Ete3PNUbm0WAZhWE+hhHLBo0EfMi+JAIAjUG7QW3xUFA 83D2qsMO89xv/dw3qlpQF+m1c75e2oWk08FKeqrSvxYkW/Vfe2vHiRK0kyRKDai9SOCH C6Qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495588; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=037pUqXeQr7lbfms7Jj3Q4cOJvKKygZOU+H5t+p+MOc=; b=lZw+dQTdH+Fy1m0C+NPrF9W27WjMJn69s2qIKWjPTVViUVp9t1uzXXQl91Y8shwO/R 0SocMHQIEbj/NgEniQmlwEqTNEX3YlixZ+2UZkzfsPnAww7akVfH91PBkZAW8EIhyj7t 87fQXHbCCq3+vWp/7YWf/C0zyx4pKMdlHd5Iu4QAu6ZIOqTQXTvY5WxUEQPAXYyD8ThY dsFqb+ETI9P5yDq6YR1NJZf7/5jc3Z3V6vlWPesVDaL7dJpAZ6R+0V7atp8MwohAOagD ZRQKtFkj7bGzgzQjEqAv8BSjeglGxQ3G6B8jLDTDj6V58aqcphrb7tjp22J6UwMhgYVj H1kw== X-Gm-Message-State: AO0yUKVcoOS+wustcls1+uLUvx1WJb0YMfr8nTPHnbW2r6XsLO2mLifp 6LuGn88UUmQPRm1xHmj8fVYQ19nRgsI= X-Google-Smtp-Source: AK7set/91VC9PMmHF4FV8Qbg8XLWgHyiljZ2d2gMBIvhfCThKgxYTJlcjU7RdiRpNZzXr0V4H3DqW3QTw6A= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a62:79c4:0:b0:622:65eb:f6bd with SMTP id u187-20020a6279c4000000b0062265ebf6bdmr17868pfc.2.1678495587846; Fri, 10 Mar 2023 16:46:27 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:46:00 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-4-seanjc@google.com> Subject: [PATCH v3 03/21] KVM: x86: Add macros to track first...last VMX feature MSRs From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add macros to track the range of VMX feature MSRs that are emulated by KVM to reduce the maintenance cost of extending the set of emulated MSRs. Note, KVM doesn't necessarily emulate all known/consumed VMX MSRs, e.g. PROCBASED_CTLS3 is consumed by KVM to enable IPI virtualization, but is not emulated as KVM doesn't emulate/virtualize IPI virtualization for nested guests. No functional change intended. Reviewed-by: Xiaoyao Li Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/vmx.c | 8 ++++---- arch/x86/kvm/x86.h | 8 ++++++++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index b43775490074..a5b9ebd6f2c5 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4107,7 +4107,7 @@ static bool svm_has_emulated_msr(struct kvm *kvm, u32= index) { switch (index) { case MSR_IA32_MCG_EXT_CTL: - case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: + case KVM_FIRST_EMULATED_VMX_MSR ... KVM_LAST_EMULATED_VMX_MSR: return false; case MSR_IA32_SMBASE: if (!IS_ENABLED(CONFIG_KVM_SMM)) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index bcac3efcde41..56607996f8f4 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -1945,7 +1945,7 @@ static inline bool is_vmx_feature_control_msr_valid(s= truct vcpu_vmx *vmx, static int vmx_get_msr_feature(struct kvm_msr_entry *msr) { switch (msr->index) { - case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: + case KVM_FIRST_EMULATED_VMX_MSR ... KVM_LAST_EMULATED_VMX_MSR: if (!nested) return 1; return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data); @@ -2030,7 +2030,7 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct = msr_data *msr_info) msr_info->data =3D to_vmx(vcpu)->msr_ia32_sgxlepubkeyhash [msr_info->index - MSR_IA32_SGXLEPUBKEYHASH0]; break; - case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: + case KVM_FIRST_EMULATED_VMX_MSR ... KVM_LAST_EMULATED_VMX_MSR: if (!nested_vmx_allowed(vcpu)) return 1; if (vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index, @@ -2366,7 +2366,7 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct = msr_data *msr_info) vmx->msr_ia32_sgxlepubkeyhash [msr_index - MSR_IA32_SGXLEPUBKEYHASH0] =3D data; break; - case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: + case KVM_FIRST_EMULATED_VMX_MSR ... KVM_LAST_EMULATED_VMX_MSR: if (!msr_info->host_initiated) return 1; /* they are read-only */ if (!nested_vmx_allowed(vcpu)) @@ -6957,7 +6957,7 @@ static bool vmx_has_emulated_msr(struct kvm *kvm, u32= index) * real mode. */ return enable_unrestricted_guest || emulate_invalid_guest_state; - case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: + case KVM_FIRST_EMULATED_VMX_MSR ... KVM_LAST_EMULATED_VMX_MSR: return nested; case MSR_AMD64_VIRT_SPEC_CTRL: case MSR_AMD64_TSC_RATIO: diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index 754190af1791..4bc483d082ee 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -40,6 +40,14 @@ void kvm_spurious_fault(void); failed; \ }) =20 +/* + * The first...last VMX feature MSRs that are emulated by KVM. This may o= r may + * not cover all known VMX MSRs, as KVM doesn't emulate an MSR until there= 's an + * associated feature that KVM supports for nested virtualization. + */ +#define KVM_FIRST_EMULATED_VMX_MSR MSR_IA32_VMX_BASIC +#define KVM_LAST_EMULATED_VMX_MSR MSR_IA32_VMX_VMFUNC + #define KVM_DEFAULT_PLE_GAP 128 #define KVM_VMX_DEFAULT_PLE_WINDOW 4096 #define KVM_DEFAULT_PLE_WINDOW_GROW 2 --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Fri Sep 12 11:36:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 20BB7C6FD1F for ; Sat, 11 Mar 2023 00:46:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230263AbjCKAqr (ORCPT ); Fri, 10 Mar 2023 19:46:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36650 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230037AbjCKAqd (ORCPT ); Fri, 10 Mar 2023 19:46:33 -0500 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4DA9613D1FF for ; Fri, 10 Mar 2023 16:46:30 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id s15-20020a170902ea0f00b0019d0c7a83dfso3652217plg.14 for ; Fri, 10 Mar 2023 16:46:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495589; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=bjAmlz3HqB+H+MWnUu8RY7jRVQBDjdxPFlGV9NdkjQ0=; b=rEdsF7x730kYSTxUbNW+tTzBPVv37BkIJ4qZ6LT+f1AEjjskZKSd6bbHZ22kmA5Ie2 zzLz+FWczoChypB6ChImJDDEmhyfJYNW9O9VKb1dWcymfeXQFhhvs7RZBlti6Oy2arZc Bjw1Xh1rvkPuQg5Ry1UV3G033HXiyU5+MDRKvuutgDAHEQhoKEBOhwZVBm4qGJSlhxel kV8wfoBNtyOk4tTR+8/0L74dWhsq+WwCMNyHfFO1w5lvBGZXmXasatDUbZHhqejDuWjT CKKykn0Vgv8CZ+A0OUZI+PDYlUeG/2CT2cw2Uo03g04S3zGiuj+PdtLcfMsL8v3GDLTQ sFbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495589; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=bjAmlz3HqB+H+MWnUu8RY7jRVQBDjdxPFlGV9NdkjQ0=; b=5sZb3aAhrRumRsPQy7N+zZaeikspf/YfMwvovjnJr1T4Z60CB5XUwtu4ng6p82Sdt8 WSM7SjKoXABnyKbAERX9THWsV0tFn9boo2e9T0NvNsbcGfGXjop8SJhppqX7s65k3hOK jiiZLKFwWu0S/Uf+CoEVO8hBcpDaVJqbIHB1fPzAETg8VKY8FrEIb36z9XXbWtn/VWWK 40DqHlWbDPZpX2L4l9c3yDalVAEoEXyyHsB4ydv/eaNiSiVanqujKHYI1xpW+N5tyOqw be4h61ZwrgR+wRaxeYsqvZSqpSirTHYWGu28A6KiCx68iHPPKZOsVQrzlsrmgtspmj9d 2c6g== X-Gm-Message-State: AO0yUKX58B7+UftcL+Ed880V99xHgHGhkCXOx6mgo1j0sTto2g0BvQ3G A1OUespFLW0DU3O9jgx1DhXTWXMd8ls= X-Google-Smtp-Source: AK7set99+kCypvapzRp6HXB60/UBcb3nXr34N4jI+Zgyl8MDz4bjb8ECHz+5Pl0Vo8jOvp/Eu1/vwwz+pxo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:7fcd:b0:19a:7bd4:5b0d with SMTP id t13-20020a1709027fcd00b0019a7bd45b0dmr10328292plb.8.1678495589646; Fri, 10 Mar 2023 16:46:29 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:46:01 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-5-seanjc@google.com> Subject: [PATCH v3 04/21] KVM: x86: Generate set of VMX feature MSRs using first/last definitions From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add VMX MSRs to the runtime list of feature MSRs by iterating over the range of emulated MSRs instead of manually defining each MSR in the "all" list. Using the range definition reduces the cost of emulating a new VMX MSR, e.g. prevents forgetting to add an MSR to the list. Extracting the VMX MSRs from the "all" list, which is a compile-time constant, also shrinks the list to the point where the compiler can heavily optimize code that iterates over the list. No functional change intended. Reviewed-by: Xiaoyao Li Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 53 +++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 7b91f73a837d..7b73a0b45041 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1539,36 +1539,19 @@ static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_a= ll)]; static unsigned num_emulated_msrs; =20 /* - * List of msr numbers which are used to expose MSR-based features that - * can be used by a hypervisor to validate requested CPU features. + * List of MSRs that control the existence of MSR-based features, i.e. MSRs + * that are effectively CPUID leafs. VMX MSRs are also included in the se= t of + * feature MSRs, but are handled separately to allow expedited lookups. */ -static const u32 msr_based_features_all[] =3D { - MSR_IA32_VMX_BASIC, - MSR_IA32_VMX_TRUE_PINBASED_CTLS, - MSR_IA32_VMX_PINBASED_CTLS, - MSR_IA32_VMX_TRUE_PROCBASED_CTLS, - MSR_IA32_VMX_PROCBASED_CTLS, - MSR_IA32_VMX_TRUE_EXIT_CTLS, - MSR_IA32_VMX_EXIT_CTLS, - MSR_IA32_VMX_TRUE_ENTRY_CTLS, - MSR_IA32_VMX_ENTRY_CTLS, - MSR_IA32_VMX_MISC, - MSR_IA32_VMX_CR0_FIXED0, - MSR_IA32_VMX_CR0_FIXED1, - MSR_IA32_VMX_CR4_FIXED0, - MSR_IA32_VMX_CR4_FIXED1, - MSR_IA32_VMX_VMCS_ENUM, - MSR_IA32_VMX_PROCBASED_CTLS2, - MSR_IA32_VMX_EPT_VPID_CAP, - MSR_IA32_VMX_VMFUNC, - +static const u32 msr_based_features_all_except_vmx[] =3D { MSR_AMD64_DE_CFG, MSR_IA32_UCODE_REV, MSR_IA32_ARCH_CAPABILITIES, MSR_IA32_PERF_CAPABILITIES, }; =20 -static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)]; +static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all_except_vmx= ) + + (KVM_LAST_EMULATED_VMX_MSR - KVM_FIRST_EMULATED_VMX_MSR + 1)]; static unsigned int num_msr_based_features; =20 /* @@ -6996,6 +6979,18 @@ long kvm_arch_vm_ioctl(struct file *filp, return r; } =20 +static void kvm_probe_feature_msr(u32 msr_index) +{ + struct kvm_msr_entry msr =3D { + .index =3D msr_index, + }; + + if (kvm_get_msr_feature(&msr)) + return; + + msr_based_features[num_msr_based_features++] =3D msr_index; +} + static void kvm_probe_msr_to_save(u32 msr_index) { u32 dummy[2]; @@ -7097,15 +7092,11 @@ static void kvm_init_msr_lists(void) emulated_msrs[num_emulated_msrs++] =3D emulated_msrs_all[i]; } =20 - for (i =3D 0; i < ARRAY_SIZE(msr_based_features_all); i++) { - struct kvm_msr_entry msr; + for (i =3D KVM_FIRST_EMULATED_VMX_MSR; i <=3D KVM_LAST_EMULATED_VMX_MSR; = i++) + kvm_probe_feature_msr(i); =20 - msr.index =3D msr_based_features_all[i]; - if (kvm_get_msr_feature(&msr)) - continue; - - msr_based_features[num_msr_based_features++] =3D msr_based_features_all[= i]; - } + for (i =3D 0; i < ARRAY_SIZE(msr_based_features_all_except_vmx); i++) + kvm_probe_feature_msr(msr_based_features_all_except_vmx[i]); } =20 static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len, --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Fri Sep 12 11:36:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 985D5C6FA99 for ; Sat, 11 Mar 2023 00:46:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230401AbjCKAq4 (ORCPT ); Fri, 10 Mar 2023 19:46:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230032AbjCKAqn (ORCPT ); Fri, 10 Mar 2023 19:46:43 -0500 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F59D13E0B4 for ; Fri, 10 Mar 2023 16:46:32 -0800 (PST) Received: by mail-pg1-x54a.google.com with SMTP id z4-20020a63e104000000b005033bc79287so1671334pgh.22 for ; Fri, 10 Mar 2023 16:46:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495591; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=/K3SNYnoXPbtGJnYKgYAyon1VCKMrYBBJfg3kvO2j1w=; b=HAXKMJmlAXL3VlLkD8B15Nc2mewXMfn/fVqBuTwrhEKRliaSZuq2QGcmlrcyOVTo1Z 4GtTMYDZZ4mW3vD23LMpqxsiLZONLboQddJeoDmBK++P6Zp8X9s6BXjKMsPDqimIKsGL rx80XqlPJgSkdLg7arjnfsW3aYS2qbmw2prNTjObh+IyBnMtUlW3xPqckd6aaXDHjINz m5NAza8F6+ZVgzqf5NMdMbZm846UYwgltZltA42Zl5Nh63gwfe0QN8cUHlnOobdCP9sX iXRKejCmzlbFQHIHjcV5RnEa4yLF5E7e/2XtUTRip6yTlgJkF3Fs2sKYTJ9F0SyBEgGm qKcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495591; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=/K3SNYnoXPbtGJnYKgYAyon1VCKMrYBBJfg3kvO2j1w=; b=eEwV2oIc7M34M334Vs7ahrKz0ni6ft6825/trVm2dQLZ1z8WqkJX/QeEOfRX3yaggl 8DonIvwwdE2drPCXNu8VmttephSE/10DpTXN0Oojx2JWVt29h8aOdbM5NyfoKMgDJAl4 LEJ2xtSkxZt9a2PsWKVOjj2ToEOJRNdlJVrm90WoqNVsGonTcCCVHt9jz0tql+mGHyP0 0EyJ/ARwK5aYnG/LHJheiuvmFCoCRmwXn23TOz5t1i68EvW/2siEf02pwILBugKfxAkO tng8p696F2xXvj7Nj91jM7XeUCGVT9AOJd+mcyp2qEnw3Rr8qnyxiir2BNvpMqWAK0r9 8ulg== X-Gm-Message-State: AO0yUKUnFwzAZoYIV1cJifzqAiASYu6fl4n7EVqxYQsU/BDbgA/XbLOQ T8ygipBrmkc1qsXaY88Hj2OgAbSIzww= X-Google-Smtp-Source: AK7set/6fcyxyyaEUDeDMuzHefFQJJJd/EwplJORjyavhZJxxlgYMcEUkZ5th6QY8PWLVy4HTWtKql2tl30= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:d18a:b0:234:c035:7749 with SMTP id fu10-20020a17090ad18a00b00234c0357749mr9644225pjb.0.1678495591577; Fri, 10 Mar 2023 16:46:31 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:46:02 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-6-seanjc@google.com> Subject: [PATCH v3 05/21] KVM: selftests: Split PMU caps sub-tests to avoid writing MSR after KVM_RUN From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Split the PERF_CAPABILITIES subtests into two parts so that the LBR format testcases don't execute after KVM_RUN. Similar to the guest CPUID model, KVM will soon disallow changing PERF_CAPABILITIES after KVM_RUN, at which point attempting to set the MSR after KVM_RUN will yield false positives and/or false negatives depending on what the test is trying to do. Land the LBR format test in a more generic "immutable features" test in anticipation of expanding its scope to other immutable features. Signed-off-by: Sean Christopherson --- .../selftests/kvm/x86_64/vmx_pmu_caps_test.c | 51 +++++++++++-------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c b/tools= /testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c index c280ba1e6572..ac08c0fdd84d 100644 --- a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +++ b/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c @@ -41,24 +41,10 @@ static void guest_code(void) wrmsr(MSR_IA32_PERF_CAPABILITIES, PMU_CAP_LBR_FMT); } =20 -int main(int argc, char *argv[]) +static void test_fungible_perf_capabilities(union perf_capabilities host_c= ap) { - struct kvm_vm *vm; struct kvm_vcpu *vcpu; - int ret; - union perf_capabilities host_cap; - uint64_t val; - - host_cap.capabilities =3D kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITIES); - host_cap.capabilities &=3D (PMU_CAP_FW_WRITES | PMU_CAP_LBR_FMT); - - /* Create VM */ - vm =3D vm_create_with_one_vcpu(&vcpu, guest_code); - - TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_PDCM)); - - TEST_REQUIRE(kvm_cpu_has_p(X86_PROPERTY_PMU_VERSION)); - TEST_REQUIRE(kvm_cpu_property(X86_PROPERTY_PMU_VERSION) > 0); + struct kvm_vm *vm =3D vm_create_with_one_vcpu(&vcpu, guest_code); =20 /* testcase 1, set capabilities when we have PDCM bit */ vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, PMU_CAP_FW_WRITES); @@ -70,7 +56,16 @@ int main(int argc, char *argv[]) vcpu_run(vcpu); ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), PMU_CAP_FW_WRIT= ES); =20 - /* testcase 2, check valid LBR formats are accepted */ + kvm_vm_free(vm); +} + +static void test_immutable_perf_capabilities(union perf_capabilities host_= cap) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm =3D vm_create_with_one_vcpu(&vcpu, NULL); + uint64_t val; + int ret; + vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, 0); ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), 0); =20 @@ -78,8 +73,8 @@ int main(int argc, char *argv[]) ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), (u64)host_cap.l= br_format); =20 /* - * Testcase 3, check that an "invalid" LBR format is rejected. Only an - * exact match of the host's format (and 0/disabled) is allowed. + * KVM only supports the host's native LBR format, as well as '0' (to + * disable LBR support). Verify KVM rejects all other LBR formats. */ for (val =3D 1; val <=3D PMU_CAP_LBR_FMT; val++) { if (val =3D=3D (host_cap.capabilities & PMU_CAP_LBR_FMT)) @@ -88,7 +83,23 @@ int main(int argc, char *argv[]) ret =3D _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, val); TEST_ASSERT(!ret, "Bad LBR FMT =3D 0x%lx didn't fail", val); } + kvm_vm_free(vm); +} + +int main(int argc, char *argv[]) +{ + union perf_capabilities host_cap; + + TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_PDCM)); + + TEST_REQUIRE(kvm_cpu_has_p(X86_PROPERTY_PMU_VERSION)); + TEST_REQUIRE(kvm_cpu_property(X86_PROPERTY_PMU_VERSION) > 0); + + host_cap.capabilities =3D kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITIES); + host_cap.capabilities &=3D (PMU_CAP_FW_WRITES | PMU_CAP_LBR_FMT); + + test_fungible_perf_capabilities(host_cap); + test_immutable_perf_capabilities(host_cap); =20 printf("Completed perf capability tests.\n"); - kvm_vm_free(vm); } --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Fri Sep 12 11:36:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 70B6FC6FD19 for ; Sat, 11 Mar 2023 00:47:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229994AbjCKArC (ORCPT ); Fri, 10 Mar 2023 19:47:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37638 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230246AbjCKAqp (ORCPT ); Fri, 10 Mar 2023 19:46:45 -0500 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 66D3113F190 for ; Fri, 10 Mar 2023 16:46:34 -0800 (PST) Received: by mail-pf1-x44a.google.com with SMTP id y35-20020a056a00182300b005e8e2c6afe2so3654179pfa.12 for ; Fri, 10 Mar 2023 16:46:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495593; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=zCOvRr1PWIR8jxDT0Nx3YLXl9PnvY8jog+vx0dMgysc=; b=nzmhl4R/Q+oaG1Se5KEs4EOP7zWfYacH54F4ladIGdZLCJkwfVINgW3AqC7RwWHz07 EULJktOyDxN8NrCJAH5ekl12s1h8TLPfFit5PvBqARXmN0eISDSSKUpX1DL8K3OtkkD3 Xio8a/4AtQSYUKdLPBeZz+jdOY4N7y1l8ckj+/sARBtlw1C4g6J+bGdne7F5QdRM588p VxlEnAMWaUx/CUNkeoOkk6r0zN3bScGmPTwnrMPpPTCiaYjVsTDpnaBlFe/knPdCTaVE aryFm5TwGdQiyjszeKkVWy5bdhFmWoCvrYJPIFXklhu94eNlMQbDuGYfCNBZza8/AnAV ywXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495593; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=zCOvRr1PWIR8jxDT0Nx3YLXl9PnvY8jog+vx0dMgysc=; b=fJeeT+5pJ43Y8jtMBg+wkQtJ/V5WZkDPkhTfklcxTXVCOmoEF5uvYZ/VC3qHYj2m9r WOfPh3wvRZhQSEbFrq3u5ccIhRQx67tImsyfKbCYkxRPKBgR6/30Qhk6fkDi/cfZoh0+ GNbN7ddqnbBohOcHPhKd1xDrOm7MOZnbYiTds9AsseLWl2HW5LZkmNWGlz1sfdnve8k9 nEMwOfNyDralDWgl38awmksS54ahJYjp60Chubd+WjENHixXYoyENuiagpn4tX5a5DYx gj7fUFQRAdPKyTCw9edQpKo62ag9pg0dafYY0oOFIYlSEAgm1uCnR1uX2QckdF03+ab7 FQqg== X-Gm-Message-State: AO0yUKVCF1DORqnfOJvWsfBW9UVHiYzuzs1xFdl396BtWA2Nu97bfCyH sRLQv1xkh/1NaNnQPEjsrgq5k/FCsQs= X-Google-Smtp-Source: AK7set/KLC6zsFqjCD7RlVaP1yI9mtRtIsb2tqNO8iAFNZm4m7sxOPkLBS2H8RWwvreh8ie/lKJnt8T0oCg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a62:db45:0:b0:606:653c:f19b with SMTP id f66-20020a62db45000000b00606653cf19bmr11011987pfg.5.1678495593538; Fri, 10 Mar 2023 16:46:33 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:46:03 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-7-seanjc@google.com> Subject: [PATCH v3 06/21] KVM: x86: Disallow writes to immutable feature MSRs after KVM_RUN From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Disallow writes to feature MSRs after KVM_RUN to prevent userspace from changing the vCPU model after running the vCPU. Similar to guest CPUID, KVM uses feature MSRs to configure intercepts, determine what operations are/aren't allowed, etc. Changing the capabilities while the vCPU is active will at best yield unpredictable guest behavior, and at worst could be dangerous to KVM. Allow writing the current value, e.g. so that userspace can blindly set all MSRs when emulating RESET, and unconditionally allow writes to MSR_IA32_UCODE_REV so that userspace can emulate patch loads. Special case the VMX MSRs to keep the generic list small, i.e. so that KVM can do a linear walk of the generic list without incurring meaningful overhead. Cc: Like Xu Cc: Yu Zhang Reviewed-by: Xiaoyao Li Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 7b73a0b45041..219492cd887e 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1554,6 +1554,25 @@ static u32 msr_based_features[ARRAY_SIZE(msr_based_f= eatures_all_except_vmx) + (KVM_LAST_EMULATED_VMX_MSR - KVM_FIRST_EMULATED_VMX_MSR + 1)]; static unsigned int num_msr_based_features; =20 +/* + * All feature MSRs except uCode revID, which tracks the currently loaded = uCode + * patch, are immutable once the vCPU model is defined. + */ +static bool kvm_is_immutable_feature_msr(u32 msr) +{ + int i; + + if (msr >=3D KVM_FIRST_EMULATED_VMX_MSR && msr <=3D KVM_LAST_EMULATED_VMX= _MSR) + return true; + + for (i =3D 0; i < ARRAY_SIZE(msr_based_features_all_except_vmx); i++) { + if (msr =3D=3D msr_based_features_all_except_vmx[i]) + return msr !=3D MSR_IA32_UCODE_REV; + } + + return false; +} + /* * Some IA32_ARCH_CAPABILITIES bits have dependencies on MSRs that KVM * does not yet virtualize. These include: @@ -2168,6 +2187,22 @@ static int do_get_msr(struct kvm_vcpu *vcpu, unsigne= d index, u64 *data) =20 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) { + u64 val; + + /* + * Disallow writes to immutable feature MSRs after KVM_RUN. KVM does + * not support modifying the guest vCPU model on the fly, e.g. changing + * the nVMX capabilities while L2 is running is nonsensical. Ignore + * writes of the same value, e.g. to allow userspace to blindly stuff + * all MSRs when emulating RESET. + */ + if (kvm_vcpu_has_run(vcpu) && kvm_is_immutable_feature_msr(index)) { + if (do_get_msr(vcpu, index, &val) || *data !=3D val) + return -EINVAL; + + return 0; + } + return kvm_set_msr_ignored_check(vcpu, index, *data, true); } =20 --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Fri Sep 12 11:36:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 06421C6FA99 for ; Sat, 11 Mar 2023 00:47:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230378AbjCKArO (ORCPT ); Fri, 10 Mar 2023 19:47:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37576 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229967AbjCKAqz (ORCPT ); Fri, 10 Mar 2023 19:46:55 -0500 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3807713D1F0 for ; Fri, 10 Mar 2023 16:46:36 -0800 (PST) Received: by mail-pf1-x44a.google.com with SMTP id x137-20020a62868f000000b0060017d68643so3624784pfd.18 for ; Fri, 10 Mar 2023 16:46:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495595; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=fG/gctdmsHG4KFmH0rDrimK5SXVIN8L7Kh/kR2GvG5A=; b=BCXSRihXjCF5gcpGEqZASI3X2u2m382eLGuNLXdp9s9FXaM2NnxlLaov/07yvpjiZI d5dc57wxslbohOAxK7jf/NKVwHIt6oyCqizaYjDuIEe518RQDeiCwgkRz6SEh1xibxxX ySlMYp/aC6gazQyrjwOT5yeD6v4xmZdy3az3oJlb3y4CK8bpbPOhydZ7ceuCu9o9Huz+ WtTW8lcLsuNoak4HCLCQKkZsigrLQu+oQDdqSWBVFk+7C+frapYtH+PegbRXJIXk+q4c D7YI+dZaAvhESpu2jak5vyM8eTXpPHNXsdv1vtNYC6FHKPnU6WPGLLsvDasDULfj7KX9 wtlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495595; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=fG/gctdmsHG4KFmH0rDrimK5SXVIN8L7Kh/kR2GvG5A=; b=ypxt8vDbAbbbnPNH7LGcw4aZGxU3k+bam/vLaZ/u84UoKKdOd1JtHYohqYjS20+QiK a+y8LVDNc1YWXq1DJ5eJztYHT2JQKSigfJl/pyJvOgWy20CoTw5CVxlRfPG3Uh6Wmpps OhYlngdE9czCss9F2nGHLGuYE0mNzCv4NCj1kKYAFJTIrA/m0UENccs/JE/GC31Hwybi UkYvuu4AABg4gPJuE7nzEV586paDRAsgrQ1zmtoGYfuV1acAcLS/TtO37i4Rx5StSf6h I9jw7/duWm2n1KWCR8MM4W7TK1pXcRSRmTH/m+o4kUKmXEmDo9JJFxmrfeJeAgeMb6MT DCAg== X-Gm-Message-State: AO0yUKUnhVnV7DlIacxxDUR6rgh04CY1IG5v7bZE3gkbWt+qqXE2AHos d9r24MxhUo+Uu6BQubfpF3GtRp1u1Qo= X-Google-Smtp-Source: AK7set/8k4ZuMdVVQdkcEV/wb/yLxhP6jK2ME2RuLadTBBF5FTif/SapvdPl8BOgE6/fCrXKesQjcqO3wpo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a62:8247:0:b0:5a8:bdd2:f99c with SMTP id w68-20020a628247000000b005a8bdd2f99cmr11215844pfd.1.1678495595379; Fri, 10 Mar 2023 16:46:35 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:46:04 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-8-seanjc@google.com> Subject: [PATCH v3 07/21] KVM: x86/pmu: WARN and bug the VM if PMU is refreshed after vCPU has run From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that KVM disallows changing feature MSRs, i.e. PERF_CAPABILITIES, after running a vCPU, WARN and bug the VM if the PMU is refreshed after the vCPU has run. Note, KVM has disallowed CPUID updates after running a vCPU since commit feb627e8d6f6 ("KVM: x86: Forbid KVM_SET_CPUID{,2} after KVM_RUN"), i.e. PERF_CAPABILITIES was the only remaining way to trigger a PMU refresh after KVM_RUN. Cc: Like Xu Signed-off-by: Sean Christopherson --- arch/x86/kvm/pmu.c | 3 +++ arch/x86/kvm/x86.c | 10 +++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index 612e6c70ce2e..7e974c4e61b0 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -589,6 +589,9 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_d= ata *msr_info) */ void kvm_pmu_refresh(struct kvm_vcpu *vcpu) { + if (KVM_BUG_ON(kvm_vcpu_has_run(vcpu), vcpu->kvm)) + return; + static_call(kvm_x86_pmu_refresh)(vcpu); } =20 diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 219492cd887e..93a7302e4fc3 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3625,9 +3625,17 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct= msr_data *msr_info) if (data & ~kvm_caps.supported_perf_cap) return 1; =20 + /* + * Note, this is not just a performance optimization! KVM + * disallows changing feature MSRs after the vCPU has run; PMU + * refresh will bug the VM if called after the vCPU has run. + */ + if (vcpu->arch.perf_capabilities =3D=3D data) + break; + vcpu->arch.perf_capabilities =3D data; kvm_pmu_refresh(vcpu); - return 0; + break; case MSR_EFER: return set_efer(vcpu, msr_info); case MSR_K7_HWCR: --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Fri Sep 12 11:36:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1627DC6FD19 for ; Sat, 11 Mar 2023 00:47:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230410AbjCKArT (ORCPT ); Fri, 10 Mar 2023 19:47:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230398AbjCKAq4 (ORCPT ); Fri, 10 Mar 2023 19:46:56 -0500 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D3231140884 for ; Fri, 10 Mar 2023 16:46:37 -0800 (PST) Received: by mail-pf1-x449.google.com with SMTP id i7-20020a626d07000000b005d29737db06so3654568pfc.15 for ; Fri, 10 Mar 2023 16:46:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495597; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=GrjkB3GbLNCU488mD1o6+a8J9bvUDXMzY1ZmunPEt30=; b=kGctua3b9Ol+gxvKXIEOfldGSTZQksGh7WFY8sbi7ayH9VX5eY0lmQFH/cEwfN8Fda lHQN/eC6wt2hSnbn+QrTdhrGprDYcUeWWUPut1SJMDWpSX2jVzmkZT6Tom5pP9+44jeG D/b2yVuttB3e5qUYJ03jT68wS3LcR47uXDzQrCmfUUsqASAP30otOQGxk3k0sZOgYyd4 k1RRpKTpSWpFtaY4X2O9MmVNsmUbDAhkE5hATUcas6+mU9WOQSj3wwDtiYJUMR1FZEdK s650N+K9ZpWOAZPU34XcCYog6+6pNi9DOzL38NLvYnuMIZ7SD0tLeIEf7lV9Xl4atlVx Jy8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495597; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=GrjkB3GbLNCU488mD1o6+a8J9bvUDXMzY1ZmunPEt30=; b=kAsx1llQ3V+UuAaU0fWmo7YtozhHRIaLytIG+UDp1XqICBmYeSVDBhi1fb38BoeXI3 X70XKVSgBeloB9EtkGWIP/CjPCbegatNmdAdQS3o7N4lgfOob5UyvGubFcrKAI3I3Uji yVwF5R8v0HEIob8qa9I1kJABcg7V7aIcSM1zZpoOlFyvan2AxCrIMWirKZOQ5IQkIbfT vDEFp9D0+qndOVb6nOLjhlrBpFyScL4TKJbEV0zXN0Ovzw7fO5JWuYwIbw0qCKW/ckZ2 O7GSAj1hwR0TPOBl/p+cctaH37V6xeBy9R6nUxVh1+zaWvNUnJ8PmmVDTLJTW1XyCQMe Y9rw== X-Gm-Message-State: AO0yUKXiFKrjUw/3Z+uv+yhtAEXIkPmmTsCHbfLq5oYIqjCJomO4jkTy gds+duiHdu+t7r2UdLyIJd+Sqq3u/ak= X-Google-Smtp-Source: AK7set8bTZF8ot+69fmxqaQOfk/QNAD3tjdOKkLqGRUSqklDwJaPm+iPoiA3bgdpSiwskoWPIPPDcpGdywU= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a65:6953:0:b0:501:f894:ae62 with SMTP id w19-20020a656953000000b00501f894ae62mr1277678pgq.4.1678495597406; Fri, 10 Mar 2023 16:46:37 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:46:05 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-9-seanjc@google.com> Subject: [PATCH v3 08/21] KVM: x86/pmu: Zero out LBR capabilities during PMU refresh From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Zero out the LBR capabilities during PMU refresh to avoid exposing LBRs to the guest against userspace's wishes. If userspace modifies the guest's CPUID model or invokes KVM_CAP_PMU_CAPABILITY to disable vPMU after an initial KVM_SET_CPUID2, but before the first KVM_RUN, KVM will retain the previous LBR info due to bailing before refreshing the LBR descriptor. Note, this is a very theoretical bug, there is no known use case where a VMM would deliberately enable the vPMU via KVM_SET_CPUID2, and then later disable the vPMU. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/pmu_intel.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index e8a3be0b9df9..d889bb2a1de5 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -531,6 +531,16 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu) pmu->pebs_enable_mask =3D ~0ull; pmu->pebs_data_cfg_mask =3D ~0ull; =20 + memset(&lbr_desc->records, 0, sizeof(lbr_desc->records)); + + /* + * Setting passthrough of LBR MSRs is done only in the VM-Entry loop, + * and PMU refresh is disallowed after the vCPU has run, i.e. this code + * should never be reached while KVM is passing through MSRs. + */ + if (KVM_BUG_ON(lbr_desc->msr_passthrough, vcpu->kvm)) + return; + entry =3D kvm_find_cpuid_entry(vcpu, 0xa); if (!entry || !vcpu->kvm->arch.enable_pmu) return; --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Fri Sep 12 11:36:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C0438C6FD19 for ; Sat, 11 Mar 2023 00:47:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230469AbjCKArl (ORCPT ); Fri, 10 Mar 2023 19:47:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37730 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230268AbjCKAq7 (ORCPT ); Fri, 10 Mar 2023 19:46:59 -0500 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0FC7B13F68F for ; Fri, 10 Mar 2023 16:46:39 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id iw4-20020a170903044400b0019ccafc1fbeso3625784plb.3 for ; Fri, 10 Mar 2023 16:46:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495599; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=UwMxWX9lXc3VYeDgFb3qxT6J0GWhUZ4fTBpHKmaPUo4=; b=hLQNM/3Ck9a+9imtZyP3GGuRTeJaPSfHJ3XwUnAuDM6l7vf+ikPghSZhCLyFewxoTt 718eWMvalMU42w8rWkdJJUZkYM2/S2CxvLq3CW8IHb/ZZTrMYsJys6tQHDuLVfegXA6M 3e3RDgLF8L7LDGWlIiLgvGG/7GYRPQYdnqTqWJVtR/Hhwk4IhRQiIKAyv/7J42kNq6g6 XzfJZIzEDu7cNBDO8+zQAibWaYjGYP2MDPxuYR2rpR/7KWp4X7/GmqvpstJp4cB6eTiD Gc2tU8JPK9q3ttX1zc+p525ikfyD+dpPWif0Dtg7kB2APcnlLrPxDZHEa/BRVNYR8j1x Yaiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495599; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=UwMxWX9lXc3VYeDgFb3qxT6J0GWhUZ4fTBpHKmaPUo4=; b=gJBEZwGvo0bk3n7OK76IJgo9hM5DJhf9ni3Rh+pEtgqQz5A+tLHcjWJMiUbswm14oc 0x3OO8jFthudtJso00Ksjenjmpn0830i1J6R4U6yZCcHela6OUoUH5Pj21QqyT/SVZa4 nIe0i4NG8UjPYK1W2iNWTR64QWotyIa1/GZGMKFNXzucqUEZb8GKr+wIsB1zsn357c9q D7Prwt2lBAawbvrrwZ3X1PBHwrSm9ZwhcELCxc8oAgiUu4K3Tdg4p5btDkPUffJJ2V4W BvKIRWmrlMnLZzZsbrRmvHjCkwLTgCk/FxihFajqbY8Acc2PHnLmcJxHV1hGUFcpXC/5 t39A== X-Gm-Message-State: AO0yUKV1jxZMnWU2lTN/Z17CpSUVJz5mMYRAkIWKkV7jbuRLTMzHR+5r /oXUMEGPzICeUHgNbQkMseAzzONGWLg= X-Google-Smtp-Source: AK7set/4vPR76Kbgdz9p8EAthmWAAnq7WtUuTEjcW1T+e90PxuXEbMs1G0zbXFkGUgiWE0nh5GotPVsTEXQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90b:894:b0:23b:318d:dda7 with SMTP id bj20-20020a17090b089400b0023b318ddda7mr224259pjb.4.1678495599064; Fri, 10 Mar 2023 16:46:39 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:46:06 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-10-seanjc@google.com> Subject: [PATCH v3 09/21] KVM: selftests: Move 0/initial value PERF_CAPS checks to dedicated sub-test From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use a separate sub-test to verify userspace can clear PERF_CAPABILITIES and restore it to the KVM-supported value, as the testcase isn't unique to the LBR format. Signed-off-by: Sean Christopherson --- .../selftests/kvm/x86_64/vmx_pmu_caps_test.c | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c b/tools= /testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c index ac08c0fdd84d..c3b0738e361b 100644 --- a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +++ b/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c @@ -41,6 +41,24 @@ static void guest_code(void) wrmsr(MSR_IA32_PERF_CAPABILITIES, PMU_CAP_LBR_FMT); } =20 +/* + * Verify KVM allows writing PERF_CAPABILITIES with all KVM-supported feat= ures + * enabled, as well as '0' (to disable all features). + */ +static void test_basic_perf_capabilities(union perf_capabilities host_cap) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm =3D vm_create_with_one_vcpu(&vcpu, NULL); + + vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, 0); + ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), 0); + + vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities); + ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), host_cap.capabi= lities); + + kvm_vm_free(vm); +} + static void test_fungible_perf_capabilities(union perf_capabilities host_c= ap) { struct kvm_vcpu *vcpu; @@ -66,12 +84,6 @@ static void test_immutable_perf_capabilities(union perf_= capabilities host_cap) uint64_t val; int ret; =20 - vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, 0); - ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), 0); - - vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.lbr_format); - ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), (u64)host_cap.l= br_format); - /* * KVM only supports the host's native LBR format, as well as '0' (to * disable LBR support). Verify KVM rejects all other LBR formats. @@ -98,6 +110,7 @@ int main(int argc, char *argv[]) host_cap.capabilities =3D kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITIES); host_cap.capabilities &=3D (PMU_CAP_FW_WRITES | PMU_CAP_LBR_FMT); =20 + test_basic_perf_capabilities(host_cap); test_fungible_perf_capabilities(host_cap); test_immutable_perf_capabilities(host_cap); =20 --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Fri Sep 12 11:36:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4CC76C6FD1F for ; Sat, 11 Mar 2023 00:47:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231211AbjCKArs (ORCPT ); Fri, 10 Mar 2023 19:47:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230474AbjCKArI (ORCPT ); Fri, 10 Mar 2023 19:47:08 -0500 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ADB3713EB9E for ; Fri, 10 Mar 2023 16:46:41 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id iy17-20020a170903131100b0019ce046d8f3so3681386plb.23 for ; Fri, 10 Mar 2023 16:46:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495601; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=yxirUIJ6f39Vff0ZETvGTfQiZcgxHh0oGFGaX9tsDZI=; b=XRPIim0wWTGV2xd60uJ0P6WuxE3MZzQxEFfMCa70jMiRJwRNB3XbYFH9QKNEBCIY1Z BnhJ1KDO89oWDnsd0TgJG8YFNllKSs5pHdzellm6llcJsDQUXYLXQhJXIrbdf+6hJF2w rQlUX74nH01pIXP1o7EmssYmiotFtk8bK+oHRcV78jgCmvHdIVyQjp6SzGiaR4kBzoMZ GNo6/2uqcUZgNWKPY13oV5XmU6cvsmBydi/zMtfPM+2m1ZO7yOzJcDbH8REarYrpoW9a y9BMdra+O4x92LPA2seTeXSE0c/YeYP5RvRVk6jn5IyEs2x2awfWu1J6q+20GpLJmUKm jnoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495601; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=yxirUIJ6f39Vff0ZETvGTfQiZcgxHh0oGFGaX9tsDZI=; b=kkDWvEipYWYLulX9p9t4Rp7+HM++0bOVJ91VSUoZQi2Wjs3hcLvbggGrfcNDRIMRal NTZbQ0m1QSmVCIhRQLvXze5E3mpGq8iI//6iDd5LH82A6mweDTcxfWxf5X0urlV2oS8V sbdWYrea4g0lPLOPSp/Emq3l1Z6qEx2Eh6HrA1+HZK6nUS/gffAtdrd1DMEij9XtZ0r+ IS1JBElZ+AVYjVxiickha2KtDMTqVVNmXaTwtTvsY1SIw8nhytBGZLSkKtA4VmpnkC8/ q0+LksxXGE3+NvtCEsHuB95Vo5NpUUrA17fia2y24CFYfqsb3XxEd4Y7ttaN77Llg4HB sDDA== X-Gm-Message-State: AO0yUKX1EthwsS7vUgWKEyrqfe6GTS51HaGcPUm0OkuL5yQSh4hdu9Ql m5+KRV+0mdU+KOEQSVp8pWJ+VgZDSl8= X-Google-Smtp-Source: AK7set/X7Pw1rLmFh09E2mfLb65niXUw4qlCbyojA13IEkKsCIhgL6KkAGgfDb99/5PvYaqWXXTLLpaLtyU= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:9319:0:b0:503:7bdf:df4b with SMTP id b25-20020a639319000000b005037bdfdf4bmr9772662pge.3.1678495600955; Fri, 10 Mar 2023 16:46:40 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:46:07 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-11-seanjc@google.com> Subject: [PATCH v3 10/21] KVM: selftests: Assert that full-width PMC writes are supported if PDCM=1 From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" KVM emulates full-width PMC writes in software, assert that KVM reports full-width writes as supported if PERF_CAPABILITIES is supported. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c b/tools= /testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c index c3b0738e361b..035470b38400 100644 --- a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +++ b/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c @@ -110,6 +110,9 @@ int main(int argc, char *argv[]) host_cap.capabilities =3D kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITIES); host_cap.capabilities &=3D (PMU_CAP_FW_WRITES | PMU_CAP_LBR_FMT); =20 + TEST_ASSERT(host_cap.full_width_write, + "Full-width writes should always be supported"); + test_basic_perf_capabilities(host_cap); test_fungible_perf_capabilities(host_cap); test_immutable_perf_capabilities(host_cap); --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Fri Sep 12 11:36:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4B54DC6FD19 for ; Sat, 11 Mar 2023 00:47:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230515AbjCKAr6 (ORCPT ); Fri, 10 Mar 2023 19:47:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229967AbjCKArP (ORCPT ); Fri, 10 Mar 2023 19:47:15 -0500 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF7AA13E082 for ; Fri, 10 Mar 2023 16:46:44 -0800 (PST) Received: by mail-pg1-x54a.google.com with SMTP id s3-20020a632c03000000b0050300a8089aso1635096pgs.0 for ; Fri, 10 Mar 2023 16:46:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495602; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=UlahK4EgI3F4LmN6mIFBVS1s8jM35sN7dKW8KqGf28I=; b=XYfJ0XzSMcF3E1kvv8pyJPBUY1oOTCxtwnj2AeqySuQR1ZfvAKc1SW9iPHhnHJe3Gi XcutjwG0mgZaRNsN46psXVVdRMFDBq81EReYNPudybB7KQX35AkogMA4TCUr/VbuZvJx /Hku8ULfZNkx8JFRFzRKqCm+FH1VdoYEXygr0lWSRV08+fWnuTiTlMkxKw1BJvqob1GB 3avYN4po4ipfmpO5vp3/rDjXCawszeDN6tP4Hn6j/nGQ5Mn2rLnkV2CSZ7GHd9sNhr7/ o+PPXOXQsCoX2fQ9HR12St5LPalBRdIIsvavz0RQtcWzQFysV3ImhfIBkvx24fD+16hu zjWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495602; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=UlahK4EgI3F4LmN6mIFBVS1s8jM35sN7dKW8KqGf28I=; b=lbfUSBd2XU1yoW/xbjpd2TlA91LBYNBxR3yV0wfJ5RMC6uswoVRtQWm0mHwHaLTklX r0T9n2Co3kNUUcz2l5M0adB886aElhHGLiJzqJmHwOWP2WLPHom4DpT+pZofhugVX6xI K+58q5lBQ71UryUZLZcPC5nEm+5IlqZnKsyULCSyu5yBg5W3IC/NNCaru2V/LqPp7x2u dx88fZsZ9AcDA/ZVZRMmDrGQYWzRPl1UuZlPxER8JWF5KKf/HYLo1OzR1cG3I+KZA97x UsoWxUTWIS2yzNZ1GJsU0epPBsCqi8oXNXR3Vrym59zUKSAM10Uy9+tG5y6tMHTvwEnE 07Hw== X-Gm-Message-State: AO0yUKUoJdMhj4Xx9bYaHsyQ3l8aK1sIf3nJRtYN6y/XiBC0uZkvdIuU TIfp/gJykK1e8qgQQfWIPIWpjA0USAE= X-Google-Smtp-Source: AK7set8HwFPz9V2UekB+yM7oEJ6o6fajmdcDwtPEGxNDv9yOe0tK/NfCJPiB1RiF4p0C3VRjGYZd8q16rTg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a62:a512:0:b0:593:fcfb:208b with SMTP id v18-20020a62a512000000b00593fcfb208bmr11074429pfm.3.1678495602725; Fri, 10 Mar 2023 16:46:42 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:46:08 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-12-seanjc@google.com> Subject: [PATCH v3 11/21] KVM: selftests: Print out failing MSR and value in vcpu_set_msr() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Reimplement vcpu_set_msr() as a macro and pretty print the failing MSR (when possible) and the value if KVM_SET_MSRS fails instead of using the using the standard KVM_IOCTL_ERROR(). KVM_SET_MSRS is somewhat odd in that it returns the index of the last successful write, i.e. will be '0' on failure barring an entirely different KVM bug. And for writing MSRs, the MSR being written and the value being written are almost always relevant to the failure, i.e. just saying "failed!" doesn't help debug. Place the string goo in a separate macro in anticipation of using it to further expand MSR testing. Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/x86_64/processor.h | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools= /testing/selftests/kvm/include/x86_64/processor.h index 53ffa43c90db..26c8e202a956 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -928,14 +928,30 @@ static inline void vcpu_clear_cpuid_feature(struct kv= m_vcpu *vcpu, uint64_t vcpu_get_msr(struct kvm_vcpu *vcpu, uint64_t msr_index); int _vcpu_set_msr(struct kvm_vcpu *vcpu, uint64_t msr_index, uint64_t msr_= value); =20 -static inline void vcpu_set_msr(struct kvm_vcpu *vcpu, uint64_t msr_index, - uint64_t msr_value) -{ - int r =3D _vcpu_set_msr(vcpu, msr_index, msr_value); - - TEST_ASSERT(r =3D=3D 1, KVM_IOCTL_ERROR(KVM_SET_MSRS, r)); -} +/* + * Assert on an MSR access(es) and pretty print the MSR name when possible. + * Note, the caller provides the stringified name so that the name of macr= o is + * printed, not the value the macro resolves to (due to macro expansion). + */ +#define TEST_ASSERT_MSR(cond, fmt, msr, str, args...) \ +do { \ + if (__builtin_constant_p(msr)) { \ + TEST_ASSERT(cond, fmt, str, args); \ + } else if (!(cond)) { \ + char buf[16]; \ + \ + snprintf(buf, sizeof(buf), "MSR 0x%x", msr); \ + TEST_ASSERT(cond, fmt, buf, args); \ + } \ +} while (0) =20 +#define vcpu_set_msr(vcpu, msr, val) \ +do { \ + uint64_t v =3D val; \ + \ + TEST_ASSERT_MSR(_vcpu_set_msr(vcpu, msr, v) =3D=3D 1, \ + "KVM_SET_MSRS failed on %s, value =3D 0x%lx", msr, #msr, v); \ +} while (0) =20 void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bit= s); bool vm_is_unrestricted_guest(struct kvm_vm *vm); --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Fri Sep 12 11:36:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D07A5C74A4B for ; Sat, 11 Mar 2023 00:48:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230432AbjCKAsJ (ORCPT ); Fri, 10 Mar 2023 19:48:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38496 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231140AbjCKArW (ORCPT ); Fri, 10 Mar 2023 19:47:22 -0500 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D5D4141609 for ; Fri, 10 Mar 2023 16:46:50 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id l10-20020a17090270ca00b0019caa6e6bd1so3629934plt.2 for ; Fri, 10 Mar 2023 16:46:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495604; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=k4NjhMvOz8i60KQi141LzevW2pwEgTqlrWyWCj3rgJ8=; b=q5q2rp0XdgbYt+cOilERZrXszKOvS2r8jQDmCDCMfL70G4frpG5F55N+ggVBRg0IMz g0bJG22iYxgecggNlGgCI+tsoYEdA+BBqB/koX3AsNH0e48OitM+Fm2RCOknRkMk0GhS UTQ174IH0V+jzvsk+pkE0BW7461LDpIuzqzw5Ae6aJRzhabml5Xx60zne5mq089L6JRd nhicKGGuOhPUODOCbYnLtWouPP6YpoPvr0gxC8BYM/YeLUjTf5gHkBcOD2NkFgBxI1ek W7FGXfMTDas+NtLIjnN02R0mDqFC1C2TxasL/D8CHVcUvQnfRXBP/2rlN4ZHZXCP/BKp ruQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495604; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=k4NjhMvOz8i60KQi141LzevW2pwEgTqlrWyWCj3rgJ8=; b=zK86fg19AflIL4vw/c9RA3lnbozT31835B0jg0a6/l+yTvTSK2w4odUvv/pDUEpqXt ik1uArQW0xpX609l4mVVdXDHa0/rLwXe6ZJzw58t8W7M8mCsKAKT15BxVjMrtwbJQpCw PWFun7bNuTvIikn9hNbfgrKI+C5k9P3fFTAG+ZtE22M9GSXGB/V+YOnRl7In9QdoN/b5 Caahw7TbX0lhX4dnobyHWAVPApJV3IcSxJvB3dj3fevafrEXWv2p9JHfgrgH6X30ke1J 6fmKWDDXYjhBdxdvFbNaHsPqY+YWW4SWfEM0d1Co/G9LLkSVGFzeyXdXhBIXYxcbAFuG wxYg== X-Gm-Message-State: AO0yUKUgUULxpLVMYfwzxqUr9/hzT44ZE4vKMIxnrSgv11X6wMRSjzE5 yCJEYxwhu2r4Qm2ewBT4AC4Svj97ly4= X-Google-Smtp-Source: AK7set/S9AhDw3GSh/Df5C1jvHWEN75uHrpzD2xoHdS6qef32PvUE3aUMhP9PUs2c56XZDxqYmPk/72jR3g= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:744a:0:b0:4fc:27c2:840d with SMTP id e10-20020a63744a000000b004fc27c2840dmr8922435pgn.12.1678495604462; Fri, 10 Mar 2023 16:46:44 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:46:09 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-13-seanjc@google.com> Subject: [PATCH v3 12/21] KVM: selftests: Verify KVM preserves userspace writes to "durable" MSRs From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Assert that KVM provides "read what you wrote" semantics for all "durable" MSRs (for lack of a better name). The extra coverage is cheap from a runtime performance perspective, and verifying the behavior in the common helper avoids gratuitous copy+paste in individual tests. Note, this affects all tests that set MSRs from userspace! Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/x86_64/processor.h | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools= /testing/selftests/kvm/include/x86_64/processor.h index 26c8e202a956..52260f6c2465 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -945,12 +945,27 @@ do { \ } \ } while (0) =20 +/* + * Returns true if KVM should return the last written value when reading a= n MSR + * from userspace, e.g. the MSR isn't a command MSR, doesn't emulate state= that + * is changing, etc. This is NOT an exhaustive list! The intent is to fi= lter + * out MSRs that are not durable _and_ that a selftest wants to write. + */ +static inline bool is_durable_msr(uint32_t msr) +{ + return msr !=3D MSR_IA32_TSC; +} + #define vcpu_set_msr(vcpu, msr, val) \ do { \ - uint64_t v =3D val; \ + uint64_t r, v =3D val; \ \ TEST_ASSERT_MSR(_vcpu_set_msr(vcpu, msr, v) =3D=3D 1, \ "KVM_SET_MSRS failed on %s, value =3D 0x%lx", msr, #msr, v); \ + if (!is_durable_msr(msr)) \ + break; \ + r =3D vcpu_get_msr(vcpu, msr); \ + TEST_ASSERT_MSR(r =3D=3D v, "Set %s to '0x%lx', got back '0x%lx'", msr, #= msr, v, r);\ } while (0) =20 void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bit= s); --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Fri Sep 12 11:36:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3914DC6FD1F for ; Sat, 11 Mar 2023 00:48:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231305AbjCKAsd (ORCPT ); Fri, 10 Mar 2023 19:48:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230479AbjCKArl (ORCPT ); Fri, 10 Mar 2023 19:47:41 -0500 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE5AC13E09F for ; Fri, 10 Mar 2023 16:47:01 -0800 (PST) Received: by mail-pj1-x104a.google.com with SMTP id q9-20020a17090a9f4900b00237d026fc55so5074561pjv.3 for ; Fri, 10 Mar 2023 16:47:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495606; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=BZgH5Eee9L8XSMp14xmQcQyAmW1Ng+mM4Frav4M8BP8=; b=oDfm8RGtJfI416/t8hlaquI6QMoR4wxUzcfw6CKhAB2RBWVCL1SmwemdG8vpnF0CvJ fTTbQ0GLnEmQu0zQit5uGQpAoptSfwIFDikvaKmcaL6Va2UHNeAaiHC0Z4BcJmZOZfTO jhzLwFGgVX2F+/EsIT9Eq7SRgoWWEjvqKQaBuJxOT9dygHJr2lA8S02HV4MZyCISAYCf ur2XGdzNAYMdzrdlbqm+iIL4ORLD3I3vgYWnRSXVlYByRIOVpHdALznIuIol5S+BOmoJ Xc/oZ9OVUT9VFxlbC065IBwR1FtzkcBcdr99jtBgmkEU10VeljeEb/9awajBkPtOCoIZ u5eQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495606; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=BZgH5Eee9L8XSMp14xmQcQyAmW1Ng+mM4Frav4M8BP8=; b=FFAUozViXRLlfiF+VvMHzLGaSFrfQF6xxqZRZ9a7QSSF1lmqt4zJrOzMZrzFxkZQRo XBOfUvqkT+s5MOW1iejwpAwTnXwqtu2sbBFvprzSejDfHkLgIdFt9gsT1tqd0BLyTAsi 5vZoIh5AZrCVWSAAV1xJVzjTwfqIyU71M45eiIUnmydsWdRgXJ+wLGMJ76TDB1FreNcd bE7BEwtJ5hgQlo5K21JGeQ/OPOAAi2OEzIZgCH+MN3NwNNwpsyS9M6qyA5GazKVDdDoh sUcWDlvYoS8hcgd0GZ5sP3xMMp8MpCSMuTY+/QnztLjtZzb60BfpHkctBvWK97gsv+k0 YbPA== X-Gm-Message-State: AO0yUKXcOnjsP/PJkxGpoKR51/3gVINXRmNF5X/N7l5c7NdBNxsEQj6O 1XNa4wa4pYDd6iSLyqSP0oDExBHe0dw= X-Google-Smtp-Source: AK7set867AijtX5RzA06gfubmn9RP+tWssyGJSUx8tZ1dOGPMWCKp7lIg0JZVqgZpd5zDJQme4rN9dz4xQ4= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:a80c:0:b0:4fc:2369:811 with SMTP id o12-20020a63a80c000000b004fc23690811mr8981769pgf.6.1678495606218; Fri, 10 Mar 2023 16:46:46 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:46:10 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-14-seanjc@google.com> Subject: [PATCH v3 13/21] KVM: selftests: Drop now-redundant checks on PERF_CAPABILITIES writes From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that vcpu_set_msr() verifies the expected "read what was wrote" semantics of all durable MSRs, including PERF_CAPABILITIES, drop the now-redundant manual checks in the VMX PMU caps test. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c b/tools= /testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c index 035470b38400..f7a27b5c949b 100644 --- a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +++ b/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c @@ -51,10 +51,7 @@ static void test_basic_perf_capabilities(union perf_capa= bilities host_cap) struct kvm_vm *vm =3D vm_create_with_one_vcpu(&vcpu, NULL); =20 vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, 0); - ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), 0); - vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities); - ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), host_cap.capabi= lities); =20 kvm_vm_free(vm); } @@ -67,9 +64,6 @@ static void test_fungible_perf_capabilities(union perf_ca= pabilities host_cap) /* testcase 1, set capabilities when we have PDCM bit */ vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, PMU_CAP_FW_WRITES); =20 - /* check capabilities can be retrieved with KVM_GET_MSR */ - ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), PMU_CAP_FW_WRIT= ES); - /* check whatever we write with KVM_SET_MSR is _not_ modified */ vcpu_run(vcpu); ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), PMU_CAP_FW_WRIT= ES); --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Fri Sep 12 11:36:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DFA94C6FA99 for ; Sat, 11 Mar 2023 00:48:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231313AbjCKAsj (ORCPT ); Fri, 10 Mar 2023 19:48:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231236AbjCKAr5 (ORCPT ); Fri, 10 Mar 2023 19:47:57 -0500 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 111A213D1F9 for ; Fri, 10 Mar 2023 16:47:09 -0800 (PST) Received: by mail-pf1-x449.google.com with SMTP id bw25-20020a056a00409900b005a9d0e66a7aso3619713pfb.5 for ; Fri, 10 Mar 2023 16:47:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495608; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=9vJeMLqZfyrbvMuuFaCmaxsMrbWr7H337rmJ5xRTXIs=; b=Kah5uerSLv53eTxqQbYkTMm/VSfhMH/uE576qqE1uZPXCNPv3bIF5b9zRIQVfxRcqz dPOzQLBIvzKMXY2ToV2LkU9soOLNbMqV3qJgrhtVChM1+o0YwcQCR6NTOUOuSu20UFRr uPm8SleluvchU6ezjxOKruAnPyBN7+RIAjGeej0r/7a4LfGYyEiq8gNZjNQaCZr1ofdZ bcoVfCnrVdZJplhht6rpTfI8NCAu4i8BA9/9QuX7VzKcMNKygv9WKsqDK7GDW7JGOhb8 6og4apy8lwRR4ibTLicAg00A6qnQWea+U1znibqiiiyxqkpo5D/NwcEy1gJqlMR3RLZF nDtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495608; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=9vJeMLqZfyrbvMuuFaCmaxsMrbWr7H337rmJ5xRTXIs=; b=o1ANn5wrE3rZd0XJNKiVDVKEllaNzqP1+x8SWibZIrwgp6YfM8DDmTJ4eMibesJ1Iv HqQEDmTMrfe4OJ8pYMael1Qw9ZhEsh7EqKUkJEt9/UIMNW+FFyIbD+Pegc4HndSniNrZ gWBgTKcMkJu3ZNkcAcgOm2x1KMepFGP9t9391GDXkWb1tgXK1YPKaN7ZVOVhw1JFlKpT wrT+eAsYHELy0EfuIYh+/EzUzf9v7ZLjEKgTRwzQ7hW9nG0f/EYp/YhRgKOMkSZ3tw+S pkefM0kRSSN+aL2dolg02R2UJBk+mtuOOnH/DRnK4W7S14FRCREM47ygpl3euiejvYZE blKQ== X-Gm-Message-State: AO0yUKU79vCCSz1phLbyptJKuYCtT2Pg4ermnZ83TAUuvB5cbWWbomoy JZ5394RxhYY8hAicpvUR3wFXloPjs0A= X-Google-Smtp-Source: AK7set/oBFTvvesOnOYo5lCSpAdPJKwAaw6kT0Z4sBlj7pCMt7OhvriPscelw4Lvol/Ta+n0BXNT2teFBKs= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:dc0d:b0:235:1fb7:393c with SMTP id i13-20020a17090adc0d00b002351fb7393cmr1625337pjv.1.1678495608015; Fri, 10 Mar 2023 16:46:48 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:46:11 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-15-seanjc@google.com> Subject: [PATCH v3 14/21] KVM: selftests: Test all fungible features in PERF_CAPABILITIES From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Verify that userspace can set all fungible features in PERF_CAPABILITIES. Drop the now unused #define of the "full-width writes" flag. Signed-off-by: Sean Christopherson --- .../selftests/kvm/x86_64/vmx_pmu_caps_test.c | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c b/tools= /testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c index f7a27b5c949b..2647282ff380 100644 --- a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +++ b/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c @@ -14,10 +14,11 @@ #define _GNU_SOURCE /* for program_invocation_short_name */ #include =20 +#include + #include "kvm_util.h" #include "vmx.h" =20 -#define PMU_CAP_FW_WRITES (1ULL << 13) #define PMU_CAP_LBR_FMT 0x3f =20 union perf_capabilities { @@ -36,6 +37,18 @@ union perf_capabilities { u64 capabilities; }; =20 +/* + * The LBR format and most PEBS features are immutable, all other features= are + * fungible (if supported by the host and KVM). + */ +static const union perf_capabilities immutable_caps =3D { + .lbr_format =3D -1, + .pebs_trap =3D 1, + .pebs_arch_reg =3D 1, + .pebs_format =3D -1, + .pebs_baseline =3D 1, +}; + static void guest_code(void) { wrmsr(MSR_IA32_PERF_CAPABILITIES, PMU_CAP_LBR_FMT); @@ -58,15 +71,22 @@ static void test_basic_perf_capabilities(union perf_cap= abilities host_cap) =20 static void test_fungible_perf_capabilities(union perf_capabilities host_c= ap) { + const uint64_t fungible_caps =3D host_cap.capabilities & ~immutable_caps.= capabilities; + struct kvm_vcpu *vcpu; struct kvm_vm *vm =3D vm_create_with_one_vcpu(&vcpu, guest_code); + int bit; =20 - /* testcase 1, set capabilities when we have PDCM bit */ - vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, PMU_CAP_FW_WRITES); + for_each_set_bit(bit, &fungible_caps, 64) { + vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, BIT_ULL(bit)); + vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, + host_cap.capabilities & ~BIT_ULL(bit)); + } + vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities); =20 /* check whatever we write with KVM_SET_MSR is _not_ modified */ vcpu_run(vcpu); - ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), PMU_CAP_FW_WRIT= ES); + ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), host_cap.capabi= lities); =20 kvm_vm_free(vm); } @@ -102,7 +122,6 @@ int main(int argc, char *argv[]) TEST_REQUIRE(kvm_cpu_property(X86_PROPERTY_PMU_VERSION) > 0); =20 host_cap.capabilities =3D kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITIES); - host_cap.capabilities &=3D (PMU_CAP_FW_WRITES | PMU_CAP_LBR_FMT); =20 TEST_ASSERT(host_cap.full_width_write, "Full-width writes should always be supported"); --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Fri Sep 12 11:36:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9B220C6FD1F for ; Sat, 11 Mar 2023 00:48:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231341AbjCKAsx (ORCPT ); Fri, 10 Mar 2023 19:48:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231240AbjCKAr5 (ORCPT ); Fri, 10 Mar 2023 19:47:57 -0500 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5CEF813D1F0 for ; Fri, 10 Mar 2023 16:47:10 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id bh9-20020a170902a98900b0019e506b80d0so3672475plb.13 for ; Fri, 10 Mar 2023 16:47:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495610; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=8shLf2BD8j5DUhNgoX3o6+mHtMqyjRCN50sfLrkOnl0=; b=DzIJin/sBdxZML/E6j9IbyfB58NFBLF8eEspI/Uquf+oD2qtETdUnzoBF8auHpny27 eq4mIh7mZVBqBL4IbIGn3vzTT89YOInoNUdQ818qbYezkIF6meu5kKIHs5NNGfjoArOR 5RY8ukjOIZuBheNV8jAjVASnihu20OUr6XnsgyLMS8YijC6PWVblx7nCnVCQYx2BkMsP FSnbbBNIRJf4AVS61IuFyKhfISIENSJngu4fMeFZQ/v1sKDc1Qi//ZGM03eOl5DNQQOz AcJSuM6stNsMShoHvhJGiXzM47AEALKEVeeyZPA3GxgrQiX2QvMvbXfSD/n+snhXjafJ gSrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495610; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=8shLf2BD8j5DUhNgoX3o6+mHtMqyjRCN50sfLrkOnl0=; b=khLyabsDWQlApbeX7os8rFs3CVZxf8pHQ347HlIA81MKeRZB7eYArMMph2gaP7CPyN NAEr92kkcrb8ppiNII5YTij75aLYfYzhzv4F+z3g3vN4QOrnak4kx0cXNhgldUIrN3+s lxpd0y4NrT6kDfPHYR7FZThywxAS4ePgdFJZYDCaXLpGUk6zn+cFChp7FOn/LprotqDQ WGS71bB6xsZ6WpwuAvJqbVd5bbD4aVyZzr93tR0D5Xbno5R7S0ToeEks5FcRFYl63Tby xJIj/TbzN4oRJFsQlMW1waZOQKMWSaPVzK0ciQ+zAGJkQVTN7VZWQpNrI687BjewXOU9 ln4Q== X-Gm-Message-State: AO0yUKW3+Z4ctN3SpM6jjYkkIMD04bT3ZZFaWftQrOrZ3//VTA098FEx CJYzxBfnDgKepCcOSzGPPClYj5K/HUs= X-Google-Smtp-Source: AK7set8ji35GdrABX70264YHSZ0clvQ2YZjx7+nYdKp881dNrsJ0rPofQeE7MR1tL9gsOH2keLIq4C2Ktd8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a62:a512:0:b0:593:fcfb:208b with SMTP id v18-20020a62a512000000b00593fcfb208bmr11074485pfm.3.1678495609926; Fri, 10 Mar 2023 16:46:49 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:46:12 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-16-seanjc@google.com> Subject: [PATCH v3 15/21] KVM: selftests: Test all immutable non-format bits in PERF_CAPABILITIES From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add negative testing of all immutable bits in PERF_CAPABILITIES, i.e. single bits that are reserved-0 or are effectively reserved-1 by KVM. Omit LBR and PEBS format bits from the test as it's easier to test them manually than it is to add safeguards to the comment path, e.g. toggling a single bit can yield a format of '0', which is legal as a "disable" value. Signed-off-by: Sean Christopherson --- .../selftests/kvm/x86_64/vmx_pmu_caps_test.c | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c b/tools= /testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c index 2647282ff380..d91bf44a2e39 100644 --- a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +++ b/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c @@ -49,6 +49,11 @@ static const union perf_capabilities immutable_caps =3D { .pebs_baseline =3D 1, }; =20 +static const union perf_capabilities format_caps =3D { + .lbr_format =3D -1, + .pebs_format =3D -1, +}; + static void guest_code(void) { wrmsr(MSR_IA32_PERF_CAPABILITIES, PMU_CAP_LBR_FMT); @@ -91,12 +96,30 @@ static void test_fungible_perf_capabilities(union perf_= capabilities host_cap) kvm_vm_free(vm); } =20 +/* + * Verify KVM rejects attempts to set unsupported and/or immutable feature= s in + * PERF_CAPABILITIES. Note, LBR format and PEBS format need to be validat= ed + * separately as they are multi-bit values, e.g. toggling or setting a sin= gle + * bit can generate a false positive without dedicated safeguards. + */ static void test_immutable_perf_capabilities(union perf_capabilities host_= cap) { + const uint64_t reserved_caps =3D (~host_cap.capabilities | + immutable_caps.capabilities) & + ~format_caps.capabilities; + struct kvm_vcpu *vcpu; struct kvm_vm *vm =3D vm_create_with_one_vcpu(&vcpu, NULL); uint64_t val; - int ret; + int r, bit; + + for_each_set_bit(bit, &reserved_caps, 64) { + r =3D _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, + host_cap.capabilities ^ BIT_ULL(bit)); + TEST_ASSERT(!r, "%s immutable feature 0x%llx (bit %d) didn't fail", + host_cap.capabilities & BIT_ULL(bit) ? "Setting" : "Clearing", + BIT_ULL(bit), bit); + } =20 /* * KVM only supports the host's native LBR format, as well as '0' (to @@ -106,9 +129,10 @@ static void test_immutable_perf_capabilities(union per= f_capabilities host_cap) if (val =3D=3D (host_cap.capabilities & PMU_CAP_LBR_FMT)) continue; =20 - ret =3D _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, val); - TEST_ASSERT(!ret, "Bad LBR FMT =3D 0x%lx didn't fail", val); + r =3D _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, val); + TEST_ASSERT(!r, "Bad LBR FMT =3D 0x%lx didn't fail", val); } + kvm_vm_free(vm); } =20 --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Fri Sep 12 11:36:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 78771C6FA99 for ; Sat, 11 Mar 2023 00:49:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231140AbjCKAtG (ORCPT ); Fri, 10 Mar 2023 19:49:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38530 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230415AbjCKAsH (ORCPT ); Fri, 10 Mar 2023 19:48:07 -0500 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF79913EB92 for ; Fri, 10 Mar 2023 16:47:16 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id a9-20020a170902b58900b0019e2eafafddso3625078pls.7 for ; Fri, 10 Mar 2023 16:47:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495611; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=IhTBia6bsmV3yZc9hMtuCJ7+iIO3gStCyZPlx0VMvQQ=; b=Gvu/bH0wLcYdNxbtY2pIVUnwdv5diml4fm36b2o7zImi6GqKbDgWBNLkjS6TOYB0rW Kd6UijLo1FAp0KYUbGLQn3/3nXbBu5K1L+Uh75ozfpJCSu3tw3PS4q7BHxcpOQ3pEj/7 1ju2iac7nsa5DhCdFGjlIFAo/Hndm/HTw/1VN667M51a2tGSZYktQp87TnLCzy3YnUcZ JeqVy7LmSj/zOM79hXzJjqsIjoIoHg7F7ltFynS7AA0/mD+HSLBGQk/D+34VWb42TNJr aMR8mhVhyUsUK32gscNSeEGxG6faDkskat7GMJ9ZbqtQuz9tDnJdByQ3u0wQ+BMfRMSB zkGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495611; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=IhTBia6bsmV3yZc9hMtuCJ7+iIO3gStCyZPlx0VMvQQ=; b=662JgemML6FWflwpxMMKw7ZAEsKDVcInxQS7tBjlim1Lum/oKTxRiKTIhBFvvJUysW WgFLsDPhLqExxMh+Oms+xYL3txtoVIYKf/HHmJHPnBuls+T0cgaTRHc5ZZPccipUVm9H QnXgRCJmwxMurFBq3x2qe+AElriClW5K+qxPlxFiOFchEChoksJG5aVz6tZWXc29bJtL mjMeP8z5u5Cfw4RUMd2NAPPxbWbDqk+7ZQ1kwAkXz9yBHw5lAqacm6bS07rXjG4Da9FF 58wWB/N2YBMPI6nXXzn0SSy7HIl7mjOw5n4tXihr9wNLbdXlampdpVSGCTpTmsw2mNrt CHlA== X-Gm-Message-State: AO0yUKUMS+97t+WaFD3tXV50MM11g+rab0n9zO/5mDo52TFoKHDUiSlG xBGDTuA5QguSA4FDTVQegj9Ax5V4T7w= X-Google-Smtp-Source: AK7set9LcE4ENH9TQQNFoUglKcHLo4MbBBqYi2LpFazCMV3msaC/jCEvQ4EJN1xOYa/P2cAIO5AIfagl5T0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:f705:0:b0:503:7cc9:3f8d with SMTP id x5-20020a63f705000000b005037cc93f8dmr9669146pgh.9.1678495611683; Fri, 10 Mar 2023 16:46:51 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:46:13 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-17-seanjc@google.com> Subject: [PATCH v3 16/21] KVM: selftests: Expand negative testing of guest writes to PERF_CAPABILITIES From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Test that the guest can't write 0 to PERF_CAPABILITIES, can't write the current value, and can't toggle _any_ bits. There is no reason to special case the LBR format. Signed-off-by: Sean Christopherson --- .../selftests/kvm/x86_64/vmx_pmu_caps_test.c | 61 ++++++++++++++++--- 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c b/tools= /testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c index d91bf44a2e39..44fc6101a547 100644 --- a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +++ b/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c @@ -54,9 +54,59 @@ static const union perf_capabilities format_caps =3D { .pebs_format =3D -1, }; =20 -static void guest_code(void) +static void guest_code(uint64_t current_val) { - wrmsr(MSR_IA32_PERF_CAPABILITIES, PMU_CAP_LBR_FMT); + uint8_t vector; + int i; + + vector =3D wrmsr_safe(MSR_IA32_PERF_CAPABILITIES, current_val); + GUEST_ASSERT_2(vector =3D=3D GP_VECTOR, current_val, vector); + + vector =3D wrmsr_safe(MSR_IA32_PERF_CAPABILITIES, 0); + GUEST_ASSERT_2(vector =3D=3D GP_VECTOR, 0, vector); + + for (i =3D 0; i < 64; i++) { + vector =3D wrmsr_safe(MSR_IA32_PERF_CAPABILITIES, + current_val ^ BIT_ULL(i)); + GUEST_ASSERT_2(vector =3D=3D GP_VECTOR, + current_val ^ BIT_ULL(i), vector); + } + + GUEST_DONE(); +} + +/* + * Verify that guest WRMSRs to PERF_CAPABILITIES #GP regardless of the val= ue + * written, that the guest always sees the userspace controlled value, and= that + * PERF_CAPABILITIES is immutable after KVM_RUN. + */ +static void test_guest_wrmsr_perf_capabilities(union perf_capabilities hos= t_cap) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm =3D vm_create_with_one_vcpu(&vcpu, guest_code); + struct ucall uc; + + vm_init_descriptor_tables(vm); + vcpu_init_descriptor_tables(vcpu); + + vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities); + + vcpu_args_set(vcpu, 1, host_cap.capabilities); + vcpu_run(vcpu); + + switch (get_ucall(vcpu, &uc)) { + case UCALL_ABORT: + REPORT_GUEST_ASSERT_2(uc, "val =3D 0x%lx, vector =3D %lu"); + break; + case UCALL_DONE: + break; + default: + TEST_FAIL("Unexpected ucall: %lu", uc.cmd); + } + + ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), host_cap.capabi= lities); + + kvm_vm_free(vm); } =20 /* @@ -79,7 +129,7 @@ static void test_fungible_perf_capabilities(union perf_c= apabilities host_cap) const uint64_t fungible_caps =3D host_cap.capabilities & ~immutable_caps.= capabilities; =20 struct kvm_vcpu *vcpu; - struct kvm_vm *vm =3D vm_create_with_one_vcpu(&vcpu, guest_code); + struct kvm_vm *vm =3D vm_create_with_one_vcpu(&vcpu, NULL); int bit; =20 for_each_set_bit(bit, &fungible_caps, 64) { @@ -89,10 +139,6 @@ static void test_fungible_perf_capabilities(union perf_= capabilities host_cap) } vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities); =20 - /* check whatever we write with KVM_SET_MSR is _not_ modified */ - vcpu_run(vcpu); - ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), host_cap.capabi= lities); - kvm_vm_free(vm); } =20 @@ -153,6 +199,7 @@ int main(int argc, char *argv[]) test_basic_perf_capabilities(host_cap); test_fungible_perf_capabilities(host_cap); test_immutable_perf_capabilities(host_cap); + test_guest_wrmsr_perf_capabilities(host_cap); =20 printf("Completed perf capability tests.\n"); } --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Fri Sep 12 11:36:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DC93DC6FA99 for ; Sat, 11 Mar 2023 00:49:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231268AbjCKAtJ (ORCPT ); Fri, 10 Mar 2023 19:49:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231154AbjCKAsI (ORCPT ); Fri, 10 Mar 2023 19:48:08 -0500 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D32913F1BD for ; Fri, 10 Mar 2023 16:47:18 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id z9-20020a170902708900b0019f271f33aeso94160plk.9 for ; Fri, 10 Mar 2023 16:47:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495613; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=q7VAgdz7LFZdy0TwRuw+10uNxgOpC5z+66aVJ61MQCU=; b=iXJ2XNnkOZuwv5o644EpKaTumVCWjQTJc7L+xJgsS5JAvFsSb4kCqGNAqqu3qHbc55 540pfekOKMEyD6rzjkGYjtZaXTADInrV/emexCgODzuGh7jgR63UV/dMz+uKu7+qYoFX 1+clgRHynd+Y/VpEhsuWVZP1uJMIWyyiewhU4u7N37fuu9HIQOUfdyT6qqYRlTdY4EOA 2Hp98G2dca3cBJfSW/3Z0hm9qKO8YyLQWLl+Ri4fpxJ6c1x853GMTIzy0HVkjx4Mg4Hw 66E+WqHe6DtOL53eyMaFOwcJZRb9ylIvt1V/NTyc/gFUofeP39kjtzZIyWtoGCN7kNxO GKNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495613; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=q7VAgdz7LFZdy0TwRuw+10uNxgOpC5z+66aVJ61MQCU=; b=L4QLqN7WUCKPs60Z3G6GkMUB05LtqPvnZChY5MTEJ/JeQDemPZEsKHKze9q7shovyS JdmOqbZQ/q94QWNgsdj2Mp6OUnfZZE3HAUsXwqOJMSDnSyLViVK7G+pO502E9fDH9MtW xli/k+pNRxpZsiinPwHN2IG1yDwdoQTY9+jlS5ebVxCO7GRx1o+hP/8acIKBqrj70Sgk 8EJc0rW6N8lwnQAfre/YlPNrltrT7BZLOfod/b9qOEXIwe54yKjeUwYDFZOnN+rU+MC3 CYyxm5GzDz1ndtIThlo/kkYhAqFjYmZ2i7oistijlvrWzaIgTdVXEI7hXE3L4hDqfWSj FbyQ== X-Gm-Message-State: AO0yUKVoYDTXJfyKkwiIdfXMFcekUQ5wwBQRYFJwZyUzybUQeDtNNA8T 1hZkDkTgUKTFmWE2TzGBk07htYgkNhY= X-Google-Smtp-Source: AK7set8RBIub1ZSKgPGlxkz2aELAhnydTonTa6HLjz83OBALURYLH6cWulDiZXgIoBW8jhvrtg+r1J532qo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a62:c1c3:0:b0:5d6:4f73:9ad with SMTP id i186-20020a62c1c3000000b005d64f7309admr11096443pfg.2.1678495613533; Fri, 10 Mar 2023 16:46:53 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:46:14 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-18-seanjc@google.com> Subject: [PATCH v3 17/21] KVM: selftests: Test post-KVM_RUN writes to PERF_CAPABILITIES From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that KVM disallows changing PERF_CAPABILITIES after KVM_RUN, expand the host side checks to verify KVM rejects any attempts to change bits from userspace. Signed-off-by: Sean Christopherson --- .../selftests/kvm/x86_64/vmx_pmu_caps_test.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c b/tools= /testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c index 44fc6101a547..6fc86f5eba0b 100644 --- a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +++ b/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c @@ -85,6 +85,7 @@ static void test_guest_wrmsr_perf_capabilities(union perf= _capabilities host_cap) struct kvm_vcpu *vcpu; struct kvm_vm *vm =3D vm_create_with_one_vcpu(&vcpu, guest_code); struct ucall uc; + int r, i; =20 vm_init_descriptor_tables(vm); vcpu_init_descriptor_tables(vcpu); @@ -106,6 +107,18 @@ static void test_guest_wrmsr_perf_capabilities(union p= erf_capabilities host_cap) =20 ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), host_cap.capabi= lities); =20 + vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities); + + r =3D _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, 0); + TEST_ASSERT(!r, "Post-KVM_RUN write '0' didn't fail"); + + for (i =3D 0; i < 64; i++) { + r =3D _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, + host_cap.capabilities ^ BIT_ULL(i)); + TEST_ASSERT(!r, "Post-KVM_RUN write '0x%llx'didn't fail", + host_cap.capabilities ^ BIT_ULL(i)); + } + kvm_vm_free(vm); } =20 --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Fri Sep 12 11:36:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5D2D7C6FA99 for ; Sat, 11 Mar 2023 00:48:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231175AbjCKAsP (ORCPT ); Fri, 10 Mar 2023 19:48:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230093AbjCKAr1 (ORCPT ); Fri, 10 Mar 2023 19:47:27 -0500 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC9A313E0B5 for ; Fri, 10 Mar 2023 16:46:55 -0800 (PST) Received: by mail-pf1-x44a.google.com with SMTP id x137-20020a62868f000000b0060017d68643so3625070pfd.18 for ; Fri, 10 Mar 2023 16:46:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495615; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=XQpHQnTblZcYiw70uXGfxV6izqCE3iLyNZEfMn1FeJc=; b=F73jJN26RfNAt0Q4PPpnKkx/ZnL8G2GZ0Wcbjb0JAbkHSSC6mLvfHoFU8cX9axXq6p 2tOVrsjfrCSeOHC+FyDNMdVhFYFQ20L3L3ltPU9pnj9OZYvc8/UTDi7apGxan/ASgKDp BJNtysKgFKFHTkdFSktdJqPiPZp/UfE8TwoI5b0j8Exe3ilYq2/4LVHb30PgHfWQLBP8 IYWon6Qhn9n1XNHl2f4jhzthYyl7xtbB7W+UUiA73jPnj4VuK+VPKqCxN2Xvbr18EH0O 51NcqaKmDb5zj9Aw6HlbJ6ZDJJdiqpJsKs2dUBqL5iOSDpCvAEJL/eTYonN/DSEDF6Rd 2HJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495615; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=XQpHQnTblZcYiw70uXGfxV6izqCE3iLyNZEfMn1FeJc=; b=0W0yLBhLtlPwSfuKdC8rXTsbyGQonqDktJ1f/k+0pRGk5U4psDoKh9TGppt8x8ihpp gBv19FyDEKYzrj9xy1tmdXLeK2XolK3knfQt1JwpJ5nAZqtdotahI/KAuVIPPPaC8Y1C bEcyJ+eloICfIJID6CYOxUXEBPigdeUrq95byjQqNBmvNnotU3FPXllauJN7Uip4DGoI 2w3bMl6PEHDgpveHNPIGVYIhDgfkHDX7XKmwIN7iQFW2FiRjysFAuRNrZl0pB5BgzcHS 60/EtIhU1v8PBTrBAvXKiTbvZCd4t8Jkbv0s3z3QsHvSuQHYG9Pc7cdTtB4rS95YFYRZ 4B2Q== X-Gm-Message-State: AO0yUKUSP6GS33jMQ4w4MZJA2dfL/ZdQNEHqVl8vRvL3tXdzWSpu7n6L OozAZXrNhqCT1NCdg77MH0mZyl6X9yE= X-Google-Smtp-Source: AK7set97PJajfdHDsySczsXjilCUcygpoXZXoRuz+znVS6Sy8iBdOZWHu2JoCxrNpduPnoEz66UkQC+qO6o= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:744a:0:b0:503:919f:b942 with SMTP id e10-20020a63744a000000b00503919fb942mr9549279pgn.11.1678495615416; Fri, 10 Mar 2023 16:46:55 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:46:15 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-19-seanjc@google.com> Subject: [PATCH v3 18/21] KVM: selftests: Drop "all done!" printf() from PERF_CAPABILITIES test From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Drop the arbitrary "done" message from the VMX PMU caps test, it's pretty obvious the test is done when the process exits. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c b/tools= /testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c index 6fc86f5eba0b..6733d879a00b 100644 --- a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +++ b/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c @@ -213,6 +213,4 @@ int main(int argc, char *argv[]) test_fungible_perf_capabilities(host_cap); test_immutable_perf_capabilities(host_cap); test_guest_wrmsr_perf_capabilities(host_cap); - - printf("Completed perf capability tests.\n"); } --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Fri Sep 12 11:36:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 62C29C6FD19 for ; Sat, 11 Mar 2023 00:49:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229914AbjCKAtO (ORCPT ); Fri, 10 Mar 2023 19:49:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230459AbjCKAsN (ORCPT ); Fri, 10 Mar 2023 19:48:13 -0500 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18C9113F55E for ; Fri, 10 Mar 2023 16:47:25 -0800 (PST) Received: by mail-pf1-x449.google.com with SMTP id i15-20020aa78d8f000000b005edc307b103so3627151pfr.17 for ; Fri, 10 Mar 2023 16:47:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495617; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=PL46ZVFFY4W6THy18E6ByGyTQEOReUquwUIR0GdzpTw=; b=UqLaWbM1URFaqajdFYpP3qWI1GLaFwLH3UIQad3gxXdNnR1HssacIG0e3wji4Ozdtd IEBfvROKjN8oIX1O4Ul4D2qwqpHTLH9/lqh4xWW2SSMSrxIZeWH0RCMdX7n4O02NM/3p 4qVVv6rBds34QA6rHcUxGX7oOPvXMqfIG6ImBaYN/SclYX6ddXVFvHePPAY/J2nlqBNI yk27LLdv2THmLKQdIC1pviVhi2NqGZsw+1Vjr/uwqIqAJqF89zvGLLkN9sLoYKJLAmmu EEKx7yRv7Q65JJnBwtpoe+0V5j7yssZ6d3zuNkSllf8hv/AkmuD//pZLv2tauiB5tpTj QRoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495617; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=PL46ZVFFY4W6THy18E6ByGyTQEOReUquwUIR0GdzpTw=; b=U+QUcQyh/z68zDfz3gURkTmZqotaSHOO6POOD69HeR90wfh6hgOMIeYgQONyQQN2Hs dhGIRK+QEjmoXIp3OZ0ozWpLtCc/hIif39eaSwBxy6/niKw+KdGQarJhqerPeNXhhvgQ kgr/fGvrpgZo3eDlX4zGRV0AUaMx05ouoxkwOeFjSxnGQTlgWKNQaUwZwl8mkGGiFahs 9WCmx/JsSrg6q+wR68bnZrCiBWrjFJ4G2jm/D9/k8SjU+CIkS3UQBRZA41JlzIZsn9UU I4xC7MTeuO5fhpCiwPDACUU1pgfreFYCfAs3D5jqVdwMGLZm/sL/m25aYbSSUKrq5iBu sh4A== X-Gm-Message-State: AO0yUKX1q16eBqkOFbiUd3I3Lo+ucoqQnSPSEgdSKYH2K86c0ywZAX2F 6CGLlngVxslzShtFKH5te/7NpjLOikU= X-Google-Smtp-Source: AK7set9Mo+BuWR6CB5HxeWiftETZGTR15RP7d7hcjx3C76bs5pjrOGHQqlEKK3ZHcM9GayWy71Fb6QbcBNw= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a62:ce43:0:b0:593:dc61:2161 with SMTP id y64-20020a62ce43000000b00593dc612161mr11028772pfg.2.1678495617175; Fri, 10 Mar 2023 16:46:57 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:46:16 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-20-seanjc@google.com> Subject: [PATCH v3 19/21] KVM: selftests: Refactor LBR_FMT test to avoid use of separate macro From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rework the LBR format test to use the bitfield instead of a separate mask macro, mainly so that adding a nearly-identical PEBS format test doesn't have to copy-paste-tweak the macro too. No functional change intended. Signed-off-by: Sean Christopherson --- .../selftests/kvm/x86_64/vmx_pmu_caps_test.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c b/tools= /testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c index 6733d879a00b..38aec88d733b 100644 --- a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +++ b/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c @@ -19,8 +19,6 @@ #include "kvm_util.h" #include "vmx.h" =20 -#define PMU_CAP_LBR_FMT 0x3f - union perf_capabilities { struct { u64 lbr_format:6; @@ -169,7 +167,7 @@ static void test_immutable_perf_capabilities(union perf= _capabilities host_cap) =20 struct kvm_vcpu *vcpu; struct kvm_vm *vm =3D vm_create_with_one_vcpu(&vcpu, NULL); - uint64_t val; + union perf_capabilities val =3D host_cap; int r, bit; =20 for_each_set_bit(bit, &reserved_caps, 64) { @@ -184,12 +182,13 @@ static void test_immutable_perf_capabilities(union pe= rf_capabilities host_cap) * KVM only supports the host's native LBR format, as well as '0' (to * disable LBR support). Verify KVM rejects all other LBR formats. */ - for (val =3D 1; val <=3D PMU_CAP_LBR_FMT; val++) { - if (val =3D=3D (host_cap.capabilities & PMU_CAP_LBR_FMT)) + for (val.lbr_format =3D 1; val.lbr_format; val.lbr_format++) { + if (val.lbr_format =3D=3D host_cap.lbr_format) continue; =20 - r =3D _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, val); - TEST_ASSERT(!r, "Bad LBR FMT =3D 0x%lx didn't fail", val); + r =3D _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, val.capabilities); + TEST_ASSERT(!r, "Bad LBR FMT =3D 0x%x didn't fail, host =3D 0x%x", + val.lbr_format, host_cap.lbr_format); } =20 kvm_vm_free(vm); --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Fri Sep 12 11:36:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 70374C6FD19 for ; Sat, 11 Mar 2023 00:49:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231185AbjCKAtS (ORCPT ); Fri, 10 Mar 2023 19:49:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230021AbjCKAs2 (ORCPT ); Fri, 10 Mar 2023 19:48:28 -0500 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08EDF13E0A1 for ; Fri, 10 Mar 2023 16:47:28 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id mm13-20020a17090b358d00b002377ec65e7aso5061268pjb.7 for ; Fri, 10 Mar 2023 16:47:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495619; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=TM0EcdyR+iQodypxNgMstja1NjDzbXCLluws31y0Y4g=; b=ZBbk159wt5l6UBv4QH5eTyNTwkgV+nu/eqBcq3NcB0T7RFO2GZtpHZUTjmYUjMH5NW tje2qxq7wXS5nMEPgdA1LytSLK9RAg8l3IKFaf47KMKlQfMJoRAnIMn6I+pI3euB4ZJH a1URbCQyyajHpNEYUBYxqpHZEUKP80ZOcWVxo9EvF459FPRMX2NN22gVpGDUxwNigRif w/Ph0rKREQscznfR3Dw3a/xF2Xly6rwwYYzvkZNUsRaAdCNOKsNYgdkGrI+RWDS3jNsT gla4sR8Zlmh9REIVmirpUBpFrNOrEEwo7qqKx5Y57G54xHqzmkRpSQHPmTpPyt3Q6VKE Qc2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495619; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=TM0EcdyR+iQodypxNgMstja1NjDzbXCLluws31y0Y4g=; b=aKy6yAhW5zqm2kWKx5nB8+rnJxHI6AgwAbUzyOH75hr5ZqZC4GRw7/PHHbW8ghxGZm 07cDTrdt4bg99rrk2++09HDpjov4cbXPpFGiZjSfOXu9BdEjQ41NjB6Y5MfU1NLqvKZd vayIBTshk0UmEBTYRIDNPbR5TbyJ9CIYJpNjnbOtsXZFip/SegSy1uA7hQKfkk9mJEBC CcTRbOX/ddtE+IaI+m+c8ZfUosaerwaNodvN5UYvebeQStdSL//d9kqgAF0KM40Jg7yO f9dqMUPp0IBwHp+/nXCTFCbavQiMBhJYvkzqZUv20vX6ZzrhdPcKHiylDFArrqIy4n66 dAtw== X-Gm-Message-State: AO0yUKUvQkQFltAWN09WSInjHrsi5FM1VDTIOTFaOmw3XzKOB8uFXyz6 OQu63T0lwE2jHbztVk4MDLrX2cCAb+k= X-Google-Smtp-Source: AK7set9LXxplb54jbkGT2IEAHEj02At0I5VKSYv6GzL1BF9jwSJ68N7YOkkOuNPUw6tYdY73po0vJfaLDTU= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:69c4:b0:233:bc72:1c69 with SMTP id s62-20020a17090a69c400b00233bc721c69mr9581314pjj.9.1678495619325; Fri, 10 Mar 2023 16:46:59 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:46:17 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-21-seanjc@google.com> Subject: [PATCH v3 20/21] KVM: selftests: Add negative testcase for PEBS format in PERF_CAPABILITIES From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Expand the immutable features sub-test for PERF_CAPABILITIES to verify KVM rejects any attempt to use a PEBS format other than the host's. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c b/tools= /testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c index 38aec88d733b..29aaa0419294 100644 --- a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +++ b/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c @@ -191,6 +191,16 @@ static void test_immutable_perf_capabilities(union per= f_capabilities host_cap) val.lbr_format, host_cap.lbr_format); } =20 + /* Ditto for the PEBS format. */ + for (val.pebs_format =3D 1; val.pebs_format; val.pebs_format++) { + if (val.pebs_format =3D=3D host_cap.pebs_format) + continue; + + r =3D _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, val.capabilities); + TEST_ASSERT(!r, "Bad PEBS FMT =3D 0x%x didn't fail, host =3D 0x%x", + val.pebs_format, host_cap.pebs_format); + } + kvm_vm_free(vm); } =20 --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Fri Sep 12 11:36:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 49EA8C6FA99 for ; Sat, 11 Mar 2023 00:49:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231436AbjCKAtV (ORCPT ); Fri, 10 Mar 2023 19:49:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38236 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231279AbjCKAsb (ORCPT ); Fri, 10 Mar 2023 19:48:31 -0500 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD6B213B97D for ; Fri, 10 Mar 2023 16:47:29 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id lm13-20020a170903298d00b0019a8c8a13dfso3652997plb.16 for ; Fri, 10 Mar 2023 16:47:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495621; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=qD1jIziSrgpE9j3MUNLoJWWdusyfTlvDCPPTQgu6pW4=; b=cRu0v4x3nzZkysQwV7Hjv2HLzgmm+NtSGFmgLD80UubovRtPmuUIa4optzL3Fwvd/o hPF3a9cuL6DQlU5TImx1c07CESv3RjmbBkKkBKwtimUD4sqiH1YyurCPmVHrrZkVflcj nGsvH9MuFpe/HCIL+tf4oVjoBTSKR+/Q3+z+cBlb+tXlcJyuh51L5MY/qWNjb+yIHiRf FblBDwn2s8TEJ39B3Sgft0DISWOkaqz87xxdRGULPVaLzS+me/AOug2po6jpmZX7YQGo M/K/v8E+CuVP5nRs/sHyU1BI5i6e1AQNE36d+NV89sgJW/RiDrJx33tUZC4+2ML/yfPt X8AA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495621; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=qD1jIziSrgpE9j3MUNLoJWWdusyfTlvDCPPTQgu6pW4=; b=qelhvm0fUl/yv2tRLXGmxkV4bIWa+4nv5+z0J5beESc+xZLz1McxBj9w3JzZYvV7mH xzeKbsxzlEgOM78N9x00CV249A1FWPfPhaPB/n1SaqMFmIHqclnBZeCL4JRnbwaZK9I6 w6ai3ph/kr6Cvs1SSPXrzY13QqrZ+8L40fGfOBa1XuPqnzAcWSZqafaTIL31S6PgdTmL x0zI2v0iSmWKtXuDVvp5XyVfBtkMl1c1nO1UmL+72hhUZxtBz1ui34HVUipQNWxQ05QQ vtxwavMpKY4E34ZUFNMsLHrGYFbhYrhkJon8Kd+itaqqjnwlhVLIuA2JGsWihNCrv5Im 07nA== X-Gm-Message-State: AO0yUKXQIc01XnKHOGfPY1Qpn8hOh/D9x8VAV1FVOo4u3A8OCNkE55ot OzlGSNt5aAQaQq7gZdNCevpoQITAT4k= X-Google-Smtp-Source: AK7set+vF/Jqc70yV1/s3GYU99WeqUkc/YNhzWWZfOcpqVXElTJs48RHXD9kntZT1B35JY8QlrNsOAVwchA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:2959:0:b0:503:7bcd:89e9 with SMTP id bu25-20020a632959000000b005037bcd89e9mr9679853pgb.1.1678495621202; Fri, 10 Mar 2023 16:47:01 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:46:18 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-22-seanjc@google.com> Subject: [PATCH v3 21/21] KVM: selftests: Verify LBRs are disabled if vPMU is disabled From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Verify that disabling the guest's vPMU via CPUID also disables LBRs. KVM has had at least one bug where LBRs would remain enabled even though the intent was to disable everything PMU related. Signed-off-by: Sean Christopherson --- .../selftests/kvm/x86_64/vmx_pmu_caps_test.c | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c b/tools= /testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c index 29aaa0419294..3009b3e5254d 100644 --- a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +++ b/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c @@ -204,6 +204,34 @@ static void test_immutable_perf_capabilities(union per= f_capabilities host_cap) kvm_vm_free(vm); } =20 +/* + * Test that LBR MSRs are writable when LBRs are enabled, and then verify = that + * disabling the vPMU via CPUID also disables LBR support. Set bits 2:0 of + * LBR_TOS as those bits are writable across all uarch implementations (ar= ch + * LBRs will need to poke a different MSR). + */ +static void test_lbr_perf_capabilities(union perf_capabilities host_cap) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + int r; + + if (!host_cap.lbr_format) + return; + + vm =3D vm_create_with_one_vcpu(&vcpu, NULL); + + vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities); + vcpu_set_msr(vcpu, MSR_LBR_TOS, 7); + + vcpu_clear_cpuid_entry(vcpu, X86_PROPERTY_PMU_VERSION.function); + + r =3D _vcpu_set_msr(vcpu, MSR_LBR_TOS, 7); + TEST_ASSERT(!r, "Writing LBR_TOS should fail after disabling vPMU"); + + kvm_vm_free(vm); +} + int main(int argc, char *argv[]) { union perf_capabilities host_cap; @@ -222,4 +250,5 @@ int main(int argc, char *argv[]) test_fungible_perf_capabilities(host_cap); test_immutable_perf_capabilities(host_cap); test_guest_wrmsr_perf_capabilities(host_cap); + test_lbr_perf_capabilities(host_cap); } --=20 2.40.0.rc1.284.g88254d51c5-goog