From nobody Fri Sep 12 11:41:05 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 76B80C05027 for ; Fri, 10 Feb 2023 00:32:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229598AbjBJAcB (ORCPT ); Thu, 9 Feb 2023 19:32:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229625AbjBJAby (ORCPT ); Thu, 9 Feb 2023 19:31:54 -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 75EEF113D0 for ; Thu, 9 Feb 2023 16:31:53 -0800 (PST) Received: by mail-pf1-x449.google.com with SMTP id z7-20020aa79587000000b00593f19705d5so1791650pfj.10 for ; Thu, 09 Feb 2023 16:31:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=+fQX1KYX7m+MKDXvC9TpLUvb23KTkcLLlbaBWIUtq+k=; b=mrSpI7sLKW8Uif58ICcPWzR9/7fsxF/m4jSPvHSMW+ojCKiCmFJBVZRkFSvzeu8Szn yrpBlBlCmcVEKArgHoYpXljxss3+T556owr4o/sEluhUMNoeRN3sHQau0+Nvh1wj7etQ wxxWmrTP5e82mSvMaGdWYEZ2Q2vheEoNYM7cGOvxEeaQKNkwNtWwvK0OlR3QFL8XAD6V 8CDEiuW3eskkKAN+eKZ3ozQMINpDLi/CyI6+8JrkXcFTiLfJRSWB1lK92vTrYbPZMFBr 57uWzVCaIiXIytusjrVV2JqXeaHkDKIuesgc9/t90OdLlURvO7s6pZI4XJIRz3RH7I/i IaMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=+fQX1KYX7m+MKDXvC9TpLUvb23KTkcLLlbaBWIUtq+k=; b=r66/gowG0i8iZn/2vF58W3YEDlAx+bfJ/X6GAjPEvgXmM09sL4gYvnQyZ4R/gSh265 h0xIkQmvFUan8zPxh8KGtocNIUXLR5yIEnitMR0Q0WvqBuCY/ROMMGIpS7HLN5EwVf2u nis2vp5Rq8T8cwQHKuTeiiwRdXhDvmqB/d7ONWZJ0GXmePUAu28j1lVzVrb4+NMD7vKj a6IrxGkCVghuktBGF7dLb8LKPpPXzV+Yw3Cm790auMkZ0MbWKy/eHzn60EmV4SJ7ytjW wV4xrZNxMKvUX2UhbY2L4GrQlyVGKrau9KNHEDRGiiaHh1jLeDmRHLHK3nAZ4x6VTp2k l8EA== X-Gm-Message-State: AO0yUKXMPZUN7rdh5sdYTFibsJUHZ6sIUoIfNbvjSxnhjgkJ3+uo5D0V 4N/dAWDKLt8U6w8KuobCNWP6bGHc9yI= X-Google-Smtp-Source: AK7set/XDIzgJ/wUbZ/JDeptz3169Kgai62OVnoTGZLJBmh5XesLCtPUHinn6BcI6rrusF44ephavhiYAks= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:3d84:0:b0:4fb:48c8:28fb with SMTP id k126-20020a633d84000000b004fb48c828fbmr739276pga.122.1675989112919; Thu, 09 Feb 2023 16:31:52 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:28 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-2-seanjc@google.com> Subject: [PATCH v2 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 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. Signed-off-by: Sean Christopherson Reviewed-by: Xiaoyao Li --- 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.39.1.581.gbfd45094c4-goog From nobody Fri Sep 12 11:41:05 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 A81E8C05027 for ; Fri, 10 Feb 2023 00:32:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230121AbjBJAcE (ORCPT ); Thu, 9 Feb 2023 19:32:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229801AbjBJAb4 (ORCPT ); Thu, 9 Feb 2023 19:31:56 -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 E88CE1817E for ; Thu, 9 Feb 2023 16:31:54 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id b5-20020a170902d50500b00198f3be5233so2112382plg.16 for ; Thu, 09 Feb 2023 16:31:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=GMrgZXh8eRXUUybNQrnQTy6sLOXQO4H5b5tUEnbNKec=; b=oyw0c2cuopZVKbsk7SgDz1IYza0BOCd+ulMvdJ8xEjdGMuT/jqffPe5ewUsajChM75 o8PjURT5214HgqCI7j7vp14jw0Ad6mHw403ZTcTfLxy8th+VCdeljjaad9z/sXp6btAv Ezflm0koCwysFQIjB8wNz7tb/P4486eh4tDgKMwDN+48+ShexTXTmQsc9l0BkWoZGJaM 9qjzhxPj3GcB+C1ycykOtd/bQCV+Qkka+SAbWCb2veOu4EmPKTYrNqTSf7SdhqrskGg+ QvqNpH2Tvh6x8nMwDg8Z11SjW1GK10whZH5VZ3Gnr2IzC/7CYwUqbx8rJn9HHe0l07Fg ezPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=GMrgZXh8eRXUUybNQrnQTy6sLOXQO4H5b5tUEnbNKec=; b=GX5vneono4lkt1JtOdAAKBKqbHNFQNZfxtes9yTwm5IwV34qvGeekzPolqp2N6Wyfm anrMIKcylQ0WLAM99BOA9Z1J79HbMJbRunzPzIEyFoZ+Pe9jy3jMXTaChNK8N4qeOo8r eUo6ojjTaCULnGUi2VZJ04SMUz64i+v3OeOqFHq3WEpUthi9Bhl5SQvV+mUlAYiegnV/ 7pP51gTSgzETK3TvaSASc/PEwHrlTEQcyB96JNWBXwp6GOWmgCcuyJpJT/my5k793yLM LkAhn60sX4/IgMxSCT2UzZLf78myGrsyeR3x+PrMkuaxo4e21ba/99XzZNAghV2sKvIC tVoA== X-Gm-Message-State: AO0yUKXEt40Mk5ZlHlHqyVpYFuc7Ixu1ImJ3Hic+WmfdLsNa4D3a3POE xxNsWpEnRnpK1WD257lfo6QM8+tBHH4= X-Google-Smtp-Source: AK7set+R3ZE3dE85d/8UU+a1nJGJCmgsfDudhKwHpUe86fe8CzvUZFNpjT21c/NDLZS8xbqB0ulFG5tAdro= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:db05:b0:199:4e77:7fae with SMTP id m5-20020a170902db0500b001994e777faemr1708398plx.2.1675989114447; Thu, 09 Feb 2023 16:31:54 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:29 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-3-seanjc@google.com> Subject: [PATCH v2 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 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 Signed-off-by: Sean Christopherson Reviewed-by: Xiaoyao Li --- 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 c91ee2927dd7..b0693195273b 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.39.1.581.gbfd45094c4-goog From nobody Fri Sep 12 11:41:05 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 18606C05027 for ; Fri, 10 Feb 2023 00:32:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230161AbjBJAcJ (ORCPT ); Thu, 9 Feb 2023 19:32:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230115AbjBJAb7 (ORCPT ); Thu, 9 Feb 2023 19:31:59 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5804925B93 for ; Thu, 9 Feb 2023 16:31:57 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-527ac47d1f2so34625707b3.16 for ; Thu, 09 Feb 2023 16:31:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=eucldSXV9a5xphj/g+7fXk4AWtyJg6Cxj5iwvnUWa+8=; b=roWXO+FTDdPfR+Ekt+fGOQKYnHFb/UMtzN+5w1pAUu1FgDsAu/ybD87OWogEBv5CRZ iymrfJBVfHA/n6eI4tDBLQfCNL5rYzZMCL9ouvZLjOn8TIYeNYsLiRLDM01uGgh/GqFZ 5lGLegMFgWy17DEJUtf2xmi/PRnFGdbcP6jdTb6M1Y84L7QKbdMFqJrhNYIpBpKxauJa nfhJmjCIwCxqCNOEuKtr3cjAXFryDFPyliyeudyaf17coxLr9Xk3ZAUtaYrYfpzM0h2V LKYNvpuONpKkXlrYhT/qdgTY34I6un0LWPUopi10YPpvKImcEtW50J3PHyG7+nY7alix F+aQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=eucldSXV9a5xphj/g+7fXk4AWtyJg6Cxj5iwvnUWa+8=; b=DWx/mTVHBvWBitKNl7XJl4uAhrAZeE48J/Ye06W8vdCJLiI1Qa6lavHxV5Y65pMnbI 3QOar7D4AnohA5eXM/qo7rPJ3W+h0mtyAH6ZpPVqDSQxmR8lI0ekKo8mOOtsaNKRnnhp U5Xwg7/M/hQv2B9U61H/WplyFMlIxExcoewGqoa7m31avi/8UJg/ZYsysqfbf1h+8gHt hD5uROBmr+H4LU01Ar9uQsHT0+1IRqJGVoV8PPPAclu90edxM/OEj5vmeNzVvDU4duGh HSQbY+OYjhfs/16SYPGi3yWFYtK+bHp4lXRX47RHMIBPSw68KJYd6l1vWbKOlri1N3ME ypfQ== X-Gm-Message-State: AO0yUKVHGXHqI3Fin03TabMe3HIISu/6/7LHvNys0w4I3kqvhwGfYSVH GUVldZY5hZtp9LcNOqKQVtf0ft4YcJM= X-Google-Smtp-Source: AK7set+xfl4rvD/uDp1yogYEOo3AdGu55fLzRd0Kl71cCiw5/Jp+NwyLM2DReGT+967aSjw+ARhxQt7tWUM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:180c:b0:8bd:9c6c:6498 with SMTP id cf12-20020a056902180c00b008bd9c6c6498mr53ybb.11.1675989116184; Thu, 09 Feb 2023 16:31:56 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:30 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-4-seanjc@google.com> Subject: [PATCH v2 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 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. Signed-off-by: Sean Christopherson Reviewed-by: Xiaoyao Li --- 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 47abd9101e68..ee86db130519 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)) @@ -6960,7 +6960,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.39.1.581.gbfd45094c4-goog From nobody Fri Sep 12 11:41:05 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 4643CC05027 for ; Fri, 10 Feb 2023 00:32:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229918AbjBJAcT (ORCPT ); Thu, 9 Feb 2023 19:32:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230145AbjBJAcA (ORCPT ); Thu, 9 Feb 2023 19:32:00 -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 9FED0305C0 for ; Thu, 9 Feb 2023 16:31:58 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id s24-20020a17090aa11800b00230ffd3f340so1587126pjp.9 for ; Thu, 09 Feb 2023 16:31:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=juCqi+Albnji1zaiiKR3HcTnH91vZKT8inm+htmoAik=; b=gs3d0bZuGZxqvCPJzEIFBUXnJOQBkCgsQ+dAE8fItXubGO+6zcIpRcPIRaTxdkO1t1 0RuquvOafdUZee3vW3OEskw2pHBNQowP01ylnsVqjzEnkJlRMSQAxabRvdHb7s/lIYnU FlEAaSO1WzAZTGyuafkbyXoXBfhBxOL9R+qp0Ggua8jW15QDBCroK3sziWNm6PxVS2hg uXYKEUjUdhRpGEPu/7wpD3t0qU4+zwp2D03LuRqyiScvn1hLVXEh6JXrqdAlteUOI2vk LVQdfIU2uZU0UruQ0vcDvSpklMtLvv/4BSwNCSYfjmfX/om5DxnVbCTtjNPPDZOvr8Wa Fmzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=juCqi+Albnji1zaiiKR3HcTnH91vZKT8inm+htmoAik=; b=iUG+JeC7HDT5bUz51kIzDEmDL6VwHjJ1gFbSwG0EeHacx/n3cTZZZ6TlkS4EdzCbeY 8ImSQZbHqT5qcCI9R58Mz7cUY3bu9pP7HXFTV7xCCfrEo1uK+twCqJ0jPv80FaRlby74 oNb1A48NeSMpDNU5cxprikXccuXELJVj5/o1uWvw/SvB7YKe8GeVpGG22Sfoaam3IzBD m6++6kWAaDB8pqeS86hdEmpAJqLfnDAKS+Ch0XVor+AUU6SN0mwh0pIytaikjppQpdTe oieFRjP3KMr/YT59F/7vWsllNfXn2VJRn+8SSc7XUaFFdGlBiI+Vlrp++EJq+7e66md4 Ub2A== X-Gm-Message-State: AO0yUKXi+hRzRKGvqWWvXtQwyYIoNeNBmneHUY8pHGL4Xk/yNEputT6H avoa/fkhwvjkYHYQVNhwZxR9GhzmjP0= X-Google-Smtp-Source: AK7set+yUu/rsqEsNvliQu/mdB4NbjnoXem0Bw8neo24a82OX/TbaC2E5ce3hyRXQAuuqep3vQVMfvdOMB4= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:b191:b0:194:7c22:1885 with SMTP id s17-20020a170902b19100b001947c221885mr3057336plr.26.1675989118117; Thu, 09 Feb 2023 16:31:58 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:31 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-5-seanjc@google.com> Subject: [PATCH v2 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 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. Signed-off-by: Sean Christopherson Reviewed-by: Xiaoyao Li --- 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.39.1.581.gbfd45094c4-goog From nobody Fri Sep 12 11:41:05 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 9CBE9C61DA4 for ; Fri, 10 Feb 2023 00:32:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230295AbjBJAc0 (ORCPT ); Thu, 9 Feb 2023 19:32:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230174AbjBJAcQ (ORCPT ); Thu, 9 Feb 2023 19:32:16 -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 7A26F4E52D for ; Thu, 9 Feb 2023 16:32:00 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id l22-20020a17090aaa9600b00230aa3c1350so1596225pjq.2 for ; Thu, 09 Feb 2023 16:32:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=00HAzMjPVYVZpNNjib7OPhFskVadeKfZTgeX842t58U=; b=aBtzaPE8mYEmKnAgTm5Kax34YIZeL7JG4RKNgQ8x0KfDYjrvnvQ33kpzNnCl3AZjFx q0YvA0dhIK66QCU2byQQpwLdRR2uvjOsa1tMsA0sdVAGPUxLfrLlfF5LDa3auEb01hKT lZJJqZrfdgikkQzecg4RtA2RbUg3AP2vbP6d5pSW1Oy0oFdX2ZSj7jPuGBOmpAaC72BA H3hPZkLs9AnDnvU7c9BO0Tg+B7NscWiAXdFyL800DZcdLES2IuiEUiScFXp0CEnwejaI nMb/yCangz2qqeh+CQdL6qbm8lQC0stog6QULX5eBG4GEdzcuUFMcJ1B44nbvA24hpHy MY7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=00HAzMjPVYVZpNNjib7OPhFskVadeKfZTgeX842t58U=; b=wdpfv3fYHeENaTnvV76RFN1o6DghiitH1fHZlE1icNCGjcu8PSUj2Byp7MUrO8hc1c 74fQHEH6qVpPyIxvWAhAF88qXGI2RTjBb5Ez3Gk7/WaHCpDvq35BoSbswdNJB0uVeJaD SrGVeYqxC+nTGjU0gXRDQqXXbB8u5AhnPo/QameUmrJxyKtpi7DvbXpwoFina26Pb389 pqsvoU3bt8myqUxcWIULVh8cSqaATBKrgPgq3FnTpt9IL+d/7KgQ4t40BjAoS1vht28T HmIpcju3+SJUOoT+MyFsuIHpEFVMybxkaE5EJr7hy4UMUYSExjmjtbgXKupzvFKdGbMa /7bA== X-Gm-Message-State: AO0yUKWk7f+vFNtf5Q58DLmrpIOpm/iYepDF3bMCS3Cw05Js+TqdcOnu cNpJYsFNQ0ytQVnVSRiHOqJ99DTCmO0= X-Google-Smtp-Source: AK7set/fP6s6kqVz5JdiGeG9ShoDDqitcXHoPray/u3IDKfyLGtoerXLUi/MQHZZGCleoBxxpRC1HTzSXmQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a62:3181:0:b0:58d:8d29:399c with SMTP id x123-20020a623181000000b0058d8d29399cmr2696005pfx.44.1675989119771; Thu, 09 Feb 2023 16:31:59 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:32 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-6-seanjc@google.com> Subject: [PATCH v2 05/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 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 Signed-off-by: Sean Christopherson Reviewed-by: Xiaoyao Li --- arch/x86/kvm/x86.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 7b73a0b45041..186cb6a81643 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,23 @@ 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 (vcpu->arch.last_vmentry_cpu !=3D -1 && + 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.39.1.581.gbfd45094c4-goog From nobody Fri Sep 12 11:41:05 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 93E25C61DA4 for ; Fri, 10 Feb 2023 00:32:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230300AbjBJAc3 (ORCPT ); Thu, 9 Feb 2023 19:32:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230170AbjBJAcQ (ORCPT ); Thu, 9 Feb 2023 19:32:16 -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 7CB495CBF3 for ; Thu, 9 Feb 2023 16:32:02 -0800 (PST) Received: by mail-pg1-x54a.google.com with SMTP id x3-20020a654143000000b004f2f2dafb21so1760547pgp.0 for ; Thu, 09 Feb 2023 16:32:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=qEyz/D/GwawmEFE6mKfSBe68GjnRayQfCebOkuONNBw=; b=keO5G55ZMsL2Z8lWgVdrwBdDX+uSCcf60tBA/h0bLFRFunQ5U8VPjAPDzH6pZORQWk nFQ43s7XYMOcxFPfMOg3Xlj5CCK96SCu9/R6aCyl0Tutl6y55A7r4BGMFVSaBcWI1POs A5azzTnHL/yjeNxWOjKh+hIu+eN4pZ9OrmUhNxyVeT7+aoSp5MdiX3CAIx+MP33eLvqj SKRAXmxKQbst1gty7eyX6ljXkIK+d1fVdqpcHC5Kj9TccjqvRPaN37oj0mxo0d+CAW1b +BJ5S4PwpjQn7P3pHtGLmZKEPafI+rpLBqKOJ732ZFzz/bQQUmeuDwAapEM4arLY9Jgx lUaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=qEyz/D/GwawmEFE6mKfSBe68GjnRayQfCebOkuONNBw=; b=goLZqqRjo13OOAJAO4WRj9ql5eeByQ6onndxW3noqx7H4w3G+m2X6xDEKwXCQf1MzN xcRqfVd8VVn4MWNV8g43wHSoaWl1I9Y+ZsmSajnnv9NW3vW7KfsrzAOLYw1FQlAA1Bwt oDxaMuSIj/QYD9Ran4EPc2P3Ur8x2fhK5EhMXYvrCIJkfrU2OR7WorwISmLu+njjhZ7r Edr3NHSqm0WOCME0Ss0cTvNptLlQGvBhAdHHvGrsi035PXCNnuMqJ8EE3QAYHEQObUtJ jrSy1vZ5YzEfMy4ljXYSpuLYMN96gKZvPwW1A0w+3Szqcfr1f6SmCaPuoncEa6igYzZE MrxQ== X-Gm-Message-State: AO0yUKWDn0nQ8Xq34kXwkzV3t03lKH4XLOLe7ivr8vz7TGkv5o8tXVjr suNfTLIcqwK95a+T0p7WYoDZsEt4Lww= X-Google-Smtp-Source: AK7set+Lg4llgEJ6QaiikJdamHYHEjb0MDTZ0zPakDnAXCfGtwwICr3RneW3uXmfczA4QmZz33KIBVoHPHo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90b:1957:b0:230:9d9a:377d with SMTP id nk23-20020a17090b195700b002309d9a377dmr2128693pjb.102.1675989121567; Thu, 09 Feb 2023 16:32:01 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:33 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-7-seanjc@google.com> Subject: [PATCH v2 06/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 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 186cb6a81643..1b14632a94a3 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3626,9 +3626,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.39.1.581.gbfd45094c4-goog From nobody Fri Sep 12 11:41:05 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 CE0FBC636D3 for ; Fri, 10 Feb 2023 00:32:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230326AbjBJAcg (ORCPT ); Thu, 9 Feb 2023 19:32:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230236AbjBJAcR (ORCPT ); Thu, 9 Feb 2023 19:32:17 -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 C1EFA6E880 for ; Thu, 9 Feb 2023 16:32:03 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id mi18-20020a17090b4b5200b00230e56d5a44so1702774pjb.1 for ; Thu, 09 Feb 2023 16:32:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=lmrHsEd1kvbCEq2I68uT6iSRNvZfXrvkq6sDzsXiSd8=; b=eFeXbUZsLbfNmRG2IC3K5Td95hJyMIBDyhLJbX7KOnO7j9qBQs4ffh7MH+DTeFYR66 GPxjZuzdYJs926CDgjuSJrEU4CZlAnm5YBPHzTt61cE605mDF0frD062wM7KlwwBlT5R oD2TMUDVjJy1Xs9xM0JOmDVSNaPLJj7wphE07o3YIeYrupB8gBOrRAKCEFErIyMXJBux s6tysAlHtj05f2QLU5bIke6dc1SeNxA5u+vZ6zppv5bzec2Cgae0Sk4Ttc3eMgkNWGMy UZt09U2thAEQDY52kwh0mv/0Ie1OwtkxyTfqTMmArhrrn68GXeX0glrtW8tPE1/Yc8fp bYFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=lmrHsEd1kvbCEq2I68uT6iSRNvZfXrvkq6sDzsXiSd8=; b=RqszdTmFyguA3l5Bf0q27G1v+LCW5oUwA1frz05PAxYzrZsa0JldUt/0dSaTkv7rOP za3HVhseWby6N8URrFAf+AJQwupuQ/oYmm0QYLllMG7dwq7bx/Zuyz5uAuX5wVc8SncK W0y0fLjw2SVKM9IO46pH+AVjy5xe9qa7Vnc871CSmi7Ah4Pb9fFDQOwoYJBaRwB0dk3z +Dxy0x+XjaZhQpuL1MhU+0RJ8cdZyXKwyzmtSksVww/1XQKtSvSXxyJdEAX2apaUmNKB /X1eKURL9s/QFxwVGZZBdKLAQLNSzPTrXI7J39Z02r0OzpqYOqdmwPuzyRZbmrgzxj4H sOXw== X-Gm-Message-State: AO0yUKX+WgP0bPeDAvmglp5UVnx1U8N5KB0vsb0g78XvSLN05xw808F/ NnKmkjv1mzM6GcK4agyskcFgEFBJvko= X-Google-Smtp-Source: AK7set+Eno6Q3VlOQChz82W6hjM+oLLJTWmqQOm0SDfFMxGRHgWSwmotAxFKANOUxU33bUR/beHmHd/TSyI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:b346:0:b0:4df:c991:3a74 with SMTP id x6-20020a63b346000000b004dfc9913a74mr2450713pgt.94.1675989122972; Thu, 09 Feb 2023 16:32:02 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:34 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-8-seanjc@google.com> Subject: [PATCH v2 07/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 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.39.1.581.gbfd45094c4-goog From nobody Fri Sep 12 11:41:05 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 58243C61DA4 for ; Fri, 10 Feb 2023 00:32:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230263AbjBJAcl (ORCPT ); Thu, 9 Feb 2023 19:32:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230265AbjBJAcT (ORCPT ); Thu, 9 Feb 2023 19:32:19 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DEA3D5AB0A for ; Thu, 9 Feb 2023 16:32:05 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-52eb75aeecdso26877707b3.13 for ; Thu, 09 Feb 2023 16:32:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=3aMSIJQHTkJvFMLxQlPPsalL8o5N8wDIsCOXy+QNiY0=; b=OHSjSxyPoWtH1UdLZ5y+EA/aw1FemTa8ZC/EoS7IROYb3LEhdj525fOFetgEPeNPU0 mpC5CqyY5mYornbMlS+7TbHD5wdcpHIScipEIbGaF4bYC1yNrUd6RYZTWM4avp2k6obu 6w3H0joiT3dl7FlB2hVL8wXEHCpIJFbC1nuDMXq/nW7C0V/78NE1pW1t9fjYQGM0SIoN gyPdx8xM4v2cBxc0RSCkGN9Wv+EeKrSk7J6nfwNTRdP/oAKZIf5Ws7S/a0IfIa5HZcHH 1URhV/u93ze1HSSSdE0J0h+IWRMIe+NKU6J1eI04jrLNjXmtOzMujNWBoVDO44zD5hKy Avaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=3aMSIJQHTkJvFMLxQlPPsalL8o5N8wDIsCOXy+QNiY0=; b=vgfKW/uurri2KBOhAvD13lW6+51ObCE9gLd4uwU1HWyjkI+bM2zNSEXqvAMn+gwo5o PKPFaYAml5p3lG5onrYw76AbhPD5O1hjkXJafBgT3JUw/QsQ+v5gyAD2QyPI0bCx/8Px JBYUVInl/2bP7XY0Rl6zzy1zB1kPOnEAmlm198mJyO2hHXm0USLlPjRfcQs7mj/xZeuE 3geyL/02EuE4nUKFKb7zbzPVLrX5u+ltBnlkXhbFravxKKVcyeX++Tsq1RE+EtqEsZGm GrrZ8Zfo3SvfW4WXuP0p+tG0b83gQAVsZqmWZZV5I7vn1FtI2losXeJCvivsH0kDsH9E K0YQ== X-Gm-Message-State: AO0yUKViTl4u4DIMtFl76fnMeshfBnL4qer6Tf9yzfjzuOAcaldatxDX kh2SoVLLIOc5Oizfu3RE/d9l4qMIqHQ= X-Google-Smtp-Source: AK7set+aBnKaeDUYBuMdpXcJHAMzYLyR0344kzEnTPzWtTtz5ZAkLBUVQmIKQmhK4GlqYa1qHEHGEvIq+rE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:6983:0:b0:502:349d:a151 with SMTP id e125-20020a816983000000b00502349da151mr1612234ywc.295.1675989124804; Thu, 09 Feb 2023 16:32:04 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:35 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-9-seanjc@google.com> Subject: [PATCH v2 08/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 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. Now that KVM disallows changing PERF_CAPABILITIES after KVM_RUN (same as guest CPUID), 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.39.1.581.gbfd45094c4-goog From nobody Fri Sep 12 11:41:05 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 94E35C61DA4 for ; Fri, 10 Feb 2023 00:32:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230346AbjBJAcy (ORCPT ); Thu, 9 Feb 2023 19:32:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230294AbjBJAcV (ORCPT ); Thu, 9 Feb 2023 19:32:21 -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 AFCFD6E887 for ; Thu, 9 Feb 2023 16:32:06 -0800 (PST) Received: by mail-pf1-x449.google.com with SMTP id x21-20020a056a000bd500b005a84de949aeso1776454pfu.18 for ; Thu, 09 Feb 2023 16:32:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=VYjOjrIZ5J+p9clyJLFd8A3MTNNwgKXkenVfNtTjRZE=; b=UTo9fRkp/Godq8jVF1yiCX5WmacvA07Lr+TfmwdK76MoOGbR/2HIzzxroOmKVrMY0m NhVtAjfAgOpLQZd2qUYFB2dWKyL+M8K21DXxuWuQrsGWfFT3WKvK6tggND4VvAuaRmcO EQXIwBhngs31eZCswvbCHq6HSPHeQosXSJpSWnNXSp3CEnY44C+WbBizGCSl6LiV5z87 Kt3XEFR5A9SLf/mlbTGX8HhkdJmq+whAwsQt61v045kj2CVanZa7w473+IMpwV5zssps Ed7iOFFf9DIjTVsg68O5b/2g1rfHGXqj3TWJ/aEGtQv67gJirjk1l8SGKPZTD7opw3a3 5yJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=VYjOjrIZ5J+p9clyJLFd8A3MTNNwgKXkenVfNtTjRZE=; b=DKTdnPK47tN2//I974BTd6WeYSSSxJ+9P3Vc3xf9PDfSxN1/7+AqgBdRiPiJauEduc 6kZnmje8E//kaG70aXgjDBWBSqem9+B+FDZ+anrT0HWSTwmwPXE5olIUC5cU54uL1+PY KkCXtLYPq+h3aw9ujgChDV520BtU9siof3vq01I7m/ZRSCpoWd38CW5km9qPtIMrrf9s 6NfwpFf2yIstHzruLRTzaBWH2sb1vZOvXA+MUj+R7gnKMwgsSP1QXh0aKX/vMSEDPkun YwVi2SZFjayYdJUwq1sRLhCsDQ0ggSnQCx6djmtN2HVPvdbWwS+qiQoOjl0vbWb2/QDf OHyw== X-Gm-Message-State: AO0yUKV7PHU2sZuOS/Y0xjvFcDhdkt3/7s5LfRlr5nRYiD5GtWU+wkse T5lhv+eegYatAwbby7+mXJQyUc+LXts= X-Google-Smtp-Source: AK7set+FFtLChGWMkz+uej7yxu55dHEMHw5g1Pb/trMPIlJDADBKAdjLRo7mSzBQn11S+deJpMs7J5eYU+c= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:8a90:b0:199:527d:42c2 with SMTP id p16-20020a1709028a9000b00199527d42c2mr1675552plo.18.1675989126261; Thu, 09 Feb 2023 16:32:06 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:36 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-10-seanjc@google.com> Subject: [PATCH v2 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 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.39.1.581.gbfd45094c4-goog From nobody Fri Sep 12 11:41:05 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 83525C61DA4 for ; Fri, 10 Feb 2023 00:33:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230236AbjBJAdG (ORCPT ); Thu, 9 Feb 2023 19:33:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230349AbjBJAch (ORCPT ); Thu, 9 Feb 2023 19:32:37 -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 D35C560B81 for ; Thu, 9 Feb 2023 16:32:08 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id g12-20020a170902868c00b00199148d00f2so2094064plo.17 for ; Thu, 09 Feb 2023 16:32:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=10uKcoMTK28X3E6OqIDW92Whbuau4E38iHBvczmXRls=; b=MZeQazzohpr5oaGhqxIEXxmz0jCJ0cHmRJPQ0SIOXmyBLoDECqha+kpA6s1C1Bhy9p k3O08BXn1tpXk7S5rlcTaAvA9zW2TexG6vazpseFgKRCfnitg5/zcZ/nSJg1WtbZzTkg Yu44+/OUxJbpiq/XAJVns+9eEsVeQCMwh1PAvAJk3bBM987HJUrO4gt0ERZ2hh08Wku3 BxVJP36jWUgAaJEvT3GdUuiadDVtaP8FksFcjrIJuc8J5c97HzZpSCyUy7BnA4GyhnVV WSZbPxnQ8CjoZzKYe2jo/lrngLsZ7NcuE3QG7NXDzn33FACLthp4Oo93JMMqf8WGfJpS 6cDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=10uKcoMTK28X3E6OqIDW92Whbuau4E38iHBvczmXRls=; b=xXyioQ08GVHv2eyug60LRo1l/aZFKd7spY7ilSomwOUDehTOKUAJ1yC3wGwht/YlRi 6WCexr7ab2/HwVh26yRwGr3220sat+FYYyKD8mVCM/+J6kHo265EMNi6NRlje5+aYuQV ykS19naCI+w89Ob44L7iyb7p9gYREDl7B375obshYVMuWPjdLgJpZmyWk0fwyXemUL7V J6t/bwOmqIE8XyFHRzLL86mCGojXeVajlLTpBnVLqVgZREYbCXMJux5jBWMmRGnTcDE5 WnhSK8TlbkwqQgbMzK98CigehTtvPNB2hUrkI9WV+i+3jmd1MwNng8aJfjf2Z3UccFrU q5JQ== X-Gm-Message-State: AO0yUKVX96E/4tzfKR1t/Baz0lAldrjhTol6BzryydS2uur7GxUGjybb QdWcmmqQVJhV/KCgwt9LfttdArkgl+Q= X-Google-Smtp-Source: AK7set8Jw6V2MxGOotIz0pRSdH5xedZQlon3S3K/ISQNTXhngC4VubA3Pf6Mz2P4HC+7/kvZjPqPBJaD274= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a62:53c6:0:b0:58d:a84a:190b with SMTP id h189-20020a6253c6000000b0058da84a190bmr2946268pfb.48.1675989128046; Thu, 09 Feb 2023 16:32:08 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:37 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-11-seanjc@google.com> Subject: [PATCH v2 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 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.39.1.581.gbfd45094c4-goog From nobody Fri Sep 12 11:41:05 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 A9E46C6379F for ; Fri, 10 Feb 2023 00:33:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230006AbjBJAdP (ORCPT ); Thu, 9 Feb 2023 19:33:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230389AbjBJAcn (ORCPT ); Thu, 9 Feb 2023 19:32:43 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B8C136F23E for ; Thu, 9 Feb 2023 16:32:10 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-4bdeb1bbeafso34460347b3.4 for ; Thu, 09 Feb 2023 16:32:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=Vk51ezOTW4zNRYSRQz4ZjoRqnpbLfX1qjajQbLOjz6E=; b=KF2AyvR1TBIJUtf7p+Na1cAbpKslF9Xw+j+Oyin6+e8ZXCwZEGpcDsbJh76rth4Nkk tGhiF5apvHuGkSkNrDPU5XGhjINXuD9J3vCdIVA5aIHAM7mbxc58e8L7oZrEwok4LK0s ZedtPFFfsvDPw7r2j0DjBjf/XuqQ5F789eziWdjiWkPmmW/Tue67s5bZ50vLVB6GvbNh xEsjIbZL1cOuugqo4NkzfThg9PE1yk65PBc8tvt3+Y6GEl09zDluOGaO9f5tsyunVCmM W6XgoSPcN+gRF+xsb0hAvTgeFUqlCt2NjX+TwcJNbeYvFEPLDdQrn9gTJ1/Llzsa/aKS dOQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=Vk51ezOTW4zNRYSRQz4ZjoRqnpbLfX1qjajQbLOjz6E=; b=xgPX1oKUB4GPhxfvOT8G1nctkoP+rfP0qkJ0hdbYHNczW+Bd/jCePU6WpcWr4XbOpz 6Ni3ZUfMazG0tqt0UPIbjuQK4h6/I6ttEGZwRo7iaIomHBQ9uqV+jxnHjbI0pv+R/jqK 1cJxBoLOfA9CydUw7JysqRyXdtpw7coHnZYLXv6dlNXJl3FHtpV0YaI9MESfYlnzrEhj 0jAYCt5PCtgHmqSSzhfG3JkVkWdOhRLK3D2vlk/n44xubgngEDIv7uC+X7c11lJSuHi9 /XjBICHbZh8ZNQbiHeyAjrWmA9uSlrLWtJbw3DWWPjy9GXnYCMuQbU2M+ZPfgrFuuFWk EV2g== X-Gm-Message-State: AO0yUKX5q0//3ACuIGsLbI7wX1UEU0CQyh81X2jR3BuEdYwP2V9/8wl4 kWDgP4zWvHR+q1C8uXilp6aTsGcRN58= X-Google-Smtp-Source: AK7set/Ob/ZegCPdJs7b/shxX7kUESmuPDm+LiV5FRWUWbDjPtQwnN5htwsHujDAHb+RExXCqWRuUCswF10= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:4d:b0:895:2805:1fb3 with SMTP id m13-20020a056902004d00b0089528051fb3mr1249187ybh.275.1675989129752; Thu, 09 Feb 2023 16:32:09 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:38 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-12-seanjc@google.com> Subject: [PATCH v2 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 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.39.1.581.gbfd45094c4-goog From nobody Fri Sep 12 11:41:05 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 7A29FC05027 for ; Fri, 10 Feb 2023 00:33:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230504AbjBJAdW (ORCPT ); Thu, 9 Feb 2023 19:33:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230183AbjBJAcs (ORCPT ); Thu, 9 Feb 2023 19:32:48 -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 86B5D6F8DA for ; Thu, 9 Feb 2023 16:32:12 -0800 (PST) Received: by mail-pg1-x54a.google.com with SMTP id o19-20020a63fb13000000b004fb5e56e652so235283pgh.9 for ; Thu, 09 Feb 2023 16:32:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=ErxDS8Mt98Bh2G42hGdy1a7/D5r7Doi1oWDwj8D/bzs=; b=USF0vWnw8XD6xBLgfR077GlxM3M8cLOgPzJzS6EyFsuX6Tz8IHUkS8VPCkzqBTmHvN Ykf+xRwHERwEpqaivb5knRZXK7+fGnzrSTi1XrZyxqR6Q60BkOI8CM6b8JSh8DB/R0pH 4H+7XX84Ax3fgVPq9kk8MqBvtc17sRz60GvWG7PKDZvy+cW3DHPNk8LGk+rXZ8EdP3MA 1LvirwcUYAWpFeiz8VOvBXd7t1XiX98omtxZerYmngSI8lgMBQDUa3czCiFm3tSiQEYb GrAkqew/EPp8L8AGnDCCHBmFWmectV5S3uIvrtJ0HkEuAQGhWqPNYAlIN4dHYb12rJRX cqIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=ErxDS8Mt98Bh2G42hGdy1a7/D5r7Doi1oWDwj8D/bzs=; b=kZP06VtT65xj/DEjpDceuA7hLa8dXDrYl9nqrOy/22DGWHJo3c24rj16yQsfD6uvoQ ZHbKmafCvyIVYxRZPFWpdJ7kbvfF+sz5PNwQC9LBCsO52fUCPp5ovyGS2QBcSBDD7ZYu PgWnLmaikGdMJ5V3bGT1z6n0pyCPJTv6cd8vnvN5BZtB+2DX/CY+YdbtHRmYhIzlQEMI lrN8e1lsy30X6gD438mGHe49QRzMtVBRjFYxnbYe/gDJ2pfvWimd5lV0ABgYF6Piudu9 ULW+Pji7ZSJEOahXprTkCqH7Pfnpb9UhfPNYtjZHjbBjJ47jq8cuHxzXRHYwZTsiN5hO SfLw== X-Gm-Message-State: AO0yUKXWwU/hCExkFUXyItAda5MNLsO+TCOgk/kTO0oUEALFaJffjOix h1Y/KgHutXZQz3iF/dEHOQlsiS1aElA= X-Google-Smtp-Source: AK7set8h/nB3pdroiVoJRiecXwOsrU2Ut28o2uCxWE9ULqbia1fKZFH/AY7xnX6Y1NyCUWUI+ibxV7GA0Oc= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:6b06:b0:22c:266c:2c45 with SMTP id v6-20020a17090a6b0600b0022c266c2c45mr1763828pjj.49.1675989131403; Thu, 09 Feb 2023 16:32:11 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:39 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-13-seanjc@google.com> Subject: [PATCH v2 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 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.39.1.581.gbfd45094c4-goog From nobody Fri Sep 12 11:41:05 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 B4F57C05027 for ; Fri, 10 Feb 2023 00:33:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230481AbjBJAdg (ORCPT ); Thu, 9 Feb 2023 19:33:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230472AbjBJAcw (ORCPT ); Thu, 9 Feb 2023 19:32:52 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE1C23A84D for ; Thu, 9 Feb 2023 16:32:19 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-52ec7c792b1so11488637b3.5 for ; Thu, 09 Feb 2023 16:32:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=/yC4dBTyHw4BCiIIE+mQoyLNyewJpNpr9hXU3HEhe04=; b=dlFTWIrj5xuEqCgUwiK8eF69WB3PynzH9yUbo/CRFqeCmeAV07IDNmKLTxj9gfHIEl QRajxEu3mwYakZTPaC3A8Ilm9JHeauGNoJ5/n+EKd77uEAO2oU6czqKqSEEkCQC89dRf jYvdefaqRkNNtezzCdDpemGQ7PUWxHdUDLhqeAVUxpK5E7cS5f2j1ytsC3l2e0aYnUQt 4uSrDnSM93RiTBzgzciApnWIjjNdLp0Sk3xWXLxMvJqtZZ062vrjOpR3NNd01Cnn4IfE U1wJoTYNzFVkN+DlYGsS7Bngc9fZm5k/+FOWfI5/Jl+q3L65nbV6Nv3++BK4z3QKVIOz TAjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=/yC4dBTyHw4BCiIIE+mQoyLNyewJpNpr9hXU3HEhe04=; b=naYU92Qgtt0lRSxf1jpYI7x0MquM3bCnpNnHz++6kAzUd8Fd5cx6mbQAcIOAw2vkS+ XJ7ybwEhuJj3BTUE1cQD6j80SsahVjJ0zM82VlJmHunwk5MZ7Q1RQB0vU3lk081kEvaH 7DC2JKZPLofnitiJEZS44fUekg0+wDzvr6mZanJxBT14BXHigYlgfRsiEpykfxGbbFJG k+vWXvkoprc+bCzUiKd4ys4S2onb0NgOFaQ7/AFLYOJL0cbauBcw6rd15gnMyyc6lNa7 EY4wSjuklJ0lJVEvEEX+/6csEGVrnYuhnEO3kicfzSazJltaEwYFauih3RlEtHO4KmX9 Ui8Q== X-Gm-Message-State: AO0yUKXIqdGqt6OU4AxoyeYmmKeiBq/iF9wfx/GhSxvFKHgUmR9rY2p1 NtMswLmMbzyTKqwfYsh5BdQfd72cg9M= X-Google-Smtp-Source: AK7set/a1VPnE1qh75p3Qr32V59FVbB5jrkL/zut0x05iyZEjpKihKDjiuQ52fCyobrlQ49ZnmbrEBPgh/A= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:81c2:0:b0:527:98b2:bce with SMTP id r185-20020a8181c2000000b0052798b20bcemr1538735ywf.222.1675989132992; Thu, 09 Feb 2023 16:32:12 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:40 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-14-seanjc@google.com> Subject: [PATCH v2 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 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.39.1.581.gbfd45094c4-goog From nobody Fri Sep 12 11:41:05 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 CBF3DC636D6 for ; Fri, 10 Feb 2023 00:33:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231136AbjBJAdi (ORCPT ); Thu, 9 Feb 2023 19:33:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53494 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229940AbjBJAcz (ORCPT ); Thu, 9 Feb 2023 19:32:55 -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 01657206AD for ; Thu, 9 Feb 2023 16:32:22 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id jn16-20020a170903051000b00198f5741d23so2098666plb.18 for ; Thu, 09 Feb 2023 16:32:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=l7cJrzLZ8eAvu1dfgk3wdtw3/0KBtZxBV1s4qDZiZW0=; b=UHNNfqkDjSKVJdelnrCH+Ts0wZPROnz5hQmCtIskOZaoDuIeDGReWBlhjSmjHn386d As37kllaRCJpQwmTp+JiTHu0h75RCOEzBhNhkxMvOLLL+JARY52FQ5o7pvUuFzeag/Id DlrpIIEbKR8uW9YDcVqwyueK0O1OA5H9x2EM1Si1M9tLIcuBqHA8COd9SSqsh3CGg9LJ NJB6EZ7I2JfuIK+KVCbRkuWDOanOk7qFrk0l53wpoerKIlhtkwkK4CcNCVntPecs++Og mo4hQxcILy0wuU5tqOxJibz+KkqA4ORjnNlbNZaJrYSX0wJslvYDvSVPNPPpPTUgPyU4 C5sA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=l7cJrzLZ8eAvu1dfgk3wdtw3/0KBtZxBV1s4qDZiZW0=; b=S5d6GvFvTTFE9yjx/tbsemxGlV/tGZk3d4DUc/LPygXjHlx7z3HU4ksU8nW7d+pV6Y PNbPnLkEpUeCawGJUTB6Z06BoZXQutXTQzb5nREVBgiIqJ9ScFRqyQ/j9UgR7M7UG0G5 AgRhOuc/J/Uxi4iHYtFIEov10Fk/TKvtXQDq2v3aiRfb+zsnSie7VZP5uOuT74cLAd07 HTRyEVoi6MNGXQvfahOq1yEJy9qsA/KchbYdQvFJx46q5//6Xxu4IGrqbThm//EIogC8 m1sOk71wV/invLowyL0MTPVRTN+y72eZ8hIkoF1rPVajOlleVXI/77zsjLmFM2FNbJ5l 5rCw== X-Gm-Message-State: AO0yUKVDhWiulG2wV7b7Y7A9AOUpAoz8EZys2LrtjsG3KogqcoUfeFaA TQrnOznz6Zylvw6l6jKyC0/XkcMBJxo= X-Google-Smtp-Source: AK7set+WJwcD0AyAgypiX1zXC1KcJ3PEH8D7Vw/FC/FYsizyfIgwIaP+henyVIjJIHjVlgD4qrw3nDAKu9M= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a62:1a8c:0:b0:592:557f:9f65 with SMTP id a134-20020a621a8c000000b00592557f9f65mr2914611pfa.8.1675989134617; Thu, 09 Feb 2023 16:32:14 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:41 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-15-seanjc@google.com> Subject: [PATCH v2 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 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.39.1.581.gbfd45094c4-goog From nobody Fri Sep 12 11:41:05 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 DF6C4C636D3 for ; Fri, 10 Feb 2023 00:33:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230523AbjBJAdc (ORCPT ); Thu, 9 Feb 2023 19:33:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230465AbjBJAcv (ORCPT ); Thu, 9 Feb 2023 19:32:51 -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 7A1B8234F2 for ; Thu, 9 Feb 2023 16:32:16 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id mi18-20020a17090b4b5200b00230e56d5a44so1703057pjb.1 for ; Thu, 09 Feb 2023 16:32:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=+k4M952d2GDbH8aircBGQJTgd8PxrJu8zZm+/DfU8l4=; b=IA6OgOX8vNndOZJU73bfgl/4tQXWJRnJ0OPt0ATsMtBAaIlTnc/X8ihp+C3Z5DSI4C suqFEyLg7Ruh/4V/G/szO99iX9MFJQosyR0tVPr3w0WQZ/6+TRzGb5ylSg+i2EB6P76p h83f3FdmOSCeQJvaS5UHWuFZSS7WcGJgvg6PZF8ggMV2MBJ4ouRc0c2uPDHt9fGawIcT cU6dBkEQtsuPzILLQnPQG7CCGxTwBFXN4mdRhqrPLDU2M5l5A4oXoGl9QzFmMUB1yaFn DKFtM8UKKAxLS4GBhXVdlJw2U24hdRM+QBifpZKjaMTVuAQfwYBhd8qG/WnWwMd0/Q6A 1ltQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=+k4M952d2GDbH8aircBGQJTgd8PxrJu8zZm+/DfU8l4=; b=VC5erubfCh0qASqV7OEloZ5r7Mvhyzjz3OULHIjRwFr8skUK9XXdknR+w/w1u00JD8 ruFVX97D0Hl+eKyqUUezpHO3+FmJOaGpLKhVxmiuiZX91J/KHG3MJx/WtHYyy/PydXrw NHHng3/68nP6yEOLhwNTL6JCr6DxnLa+azpMfXMeSCSlDpFax149+RDODL6XgADu2QwZ mW2TKw98FGwttjx95PT9jM3kRd2CMKtr8rJn3Tm7N/6atkCKNwGJSxa85NsYYUjd/P4a aFLR4ynyPMOMxHOVeKD2R/LntNBj5mmAv/a/neYKjie+DP+rjbiGOZcMNn3pPkvJT2ek 2nwQ== X-Gm-Message-State: AO0yUKUIHfBELUY2iZ54FrbgxO0yLXTLCuQFHrEhLeQrBAiXegbFO5HO 1rj44HHwUobxj8FTVdFWD+z+QOtqvTk= X-Google-Smtp-Source: AK7set/Gj587GDdBiybnEi5JF56qDN7634spMhaskRTmQFZiTgZrLMy0GNg4ksXHO454W8FrTMWsEBQaauI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:aa7:8ece:0:b0:5a8:5e12:a12e with SMTP id b14-20020aa78ece000000b005a85e12a12emr695322pfr.11.1675989136167; Thu, 09 Feb 2023 16:32:16 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:42 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-16-seanjc@google.com> Subject: [PATCH v2 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 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.39.1.581.gbfd45094c4-goog From nobody Fri Sep 12 11:41:05 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 497CFC636D3 for ; Fri, 10 Feb 2023 00:33:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230283AbjBJAdz (ORCPT ); Thu, 9 Feb 2023 19:33:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230492AbjBJAdP (ORCPT ); Thu, 9 Feb 2023 19:33:15 -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 28C8F6F8FD for ; Thu, 9 Feb 2023 16:32:32 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id l18-20020a17090add9200b00230f60889d6so1595572pjv.3 for ; Thu, 09 Feb 2023 16:32:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=u+uN37eWphC2DXBy/eZAMutSohHuiH84z9BjA6Yonic=; b=a23HHkIyyBmPIc9LXUuzfy6mgHqnSj8/Xj+GczXDjbPEG9C9VhW3YLCnaVrGASU4It dRMLgQ0Ds6KR7AF7VA5wMEdz1pY5REJp1bswbQ13++0xCdoNi9eGewi/wt/JpVA3CpsJ VibX0yz9vMRbTuJ0PzAXnZcuHGub1oyv0ffubrVq38Gfyn49hEAYN+gHDKB1vILwLouw MRymvft4bHIhVvVGp3LdNW2O6gORiCt7rLmDela8Gz7awxkPZN5iNsxMNFbZwUQVXGS9 VZyufZMAD4IfLnuP1VHvx94QEj70p3sUrJX8nsNtu7zY/zfm7bxMBRPUtTGjiQfXK6OS tx9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=u+uN37eWphC2DXBy/eZAMutSohHuiH84z9BjA6Yonic=; b=xQvGpC3H9Kb5S2nWJTPPq+DO9L2PRZCRWIWuaEAmOXTMR5Xsd24i3BKeRNm88zKkOm jFARRi1dqOrfpm/SzEtVALZNmWMQHkA8UPTK0fgg/2crLXj0J0zU8TTR7oYl7HgLM8Ks gANjdb2q/ymN13wtUPzuxWh/n5Bak4011mdu32iDc1GkemxYTcgPZNrkg1I4b/z3vxJy bqMWZR/bxGC6ZM+rbOLOAW4OyULFZTqQYgbosXz/xoZemHsNXzQoOoHxyckdfYDO9nuJ ftBnTYXIIVW1J81vsxxFXXivTf1rghhkLshQMJNag60a67zGjpOjKvHG0suh6/MLUNxf HO3Q== X-Gm-Message-State: AO0yUKXK1T7NoS+dFudj559JZwWstetdI0kWKsHHRwIcfAr9A4+PhUYj 4eMTKHyKR8PH7/0t5PFwyDLO0DHdmz0= X-Google-Smtp-Source: AK7set/pSJFj96UiXm5itrLBsMLS9XaoBFrHLiki+1QbJoSiIpVyZB/8ZeVe5DKNHsvqsmCOnr3K2h1BH1s= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:7c0f:b0:199:49fc:8669 with SMTP id x15-20020a1709027c0f00b0019949fc8669mr2022019pll.6.1675989137722; Thu, 09 Feb 2023 16:32:17 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:43 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-17-seanjc@google.com> Subject: [PATCH v2 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 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.39.1.581.gbfd45094c4-goog From nobody Fri Sep 12 11:41:05 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 EC5B8C05027 for ; Fri, 10 Feb 2023 00:34:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229827AbjBJAeA (ORCPT ); Thu, 9 Feb 2023 19:34:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230183AbjBJAd0 (ORCPT ); Thu, 9 Feb 2023 19:33:26 -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 B9CD46E89B for ; Thu, 9 Feb 2023 16:32:36 -0800 (PST) Received: by mail-pj1-x104a.google.com with SMTP id dt6-20020a17090afa4600b00230183006c3so1590069pjb.7 for ; Thu, 09 Feb 2023 16:32:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=4YaSCxh91s2vz1Wx4zQx2zkw5Mb7VIy4t0nr1FfetkY=; b=Pb9YNrGhDlI/KkrCjvmq6aONc/k8rCIIOTcWwh32p6eVOtc2iLVErkwMmRxXmoJGWr 4ovRCN8XSkgykLLXp0Nya9sUKwW/++zRSESKvzOrWF2AlS7ID5ZSoVelbRpB5nSBmrIy p885ziMNyde2KIuY0kcUsh7pg/ceJus5/PhPYdx275DUpIHm2+6ggVaYO3a0L+S43y3S 090t7+Fw5NupHRJCmZXoF+mMaXk58RXbqONmq0KbDeRkBbfL1GgfWt6TDbfvti+uQruM pLCmoWg1VuLuY0SuX81xSlEyDi0RqmPmiq0CgfOHWiDT1U5snzXS/BwiyRg89eZbZwEc hJUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=4YaSCxh91s2vz1Wx4zQx2zkw5Mb7VIy4t0nr1FfetkY=; b=ucA0eT4BEaQapKFYEwGGfRji7oAuUD/fC47yL8d8Tc1UOKwev9tneDBeZ1BrfcEzhZ o2xK/RFkiFYqmu57QCj17gJj92l9iAttyBJw9blo5WIsm7huio6UrCy/TlA82eofPFyb 6MSkZESBH299UxsvxXbPkxUPv4EbRpF8F8vs2dK455XnbKOvP4hrivzzD79JumA3cBTa b/GDayxGXdqNsoruipy9ocsoKwv8SwGNLbmsBsBb6BZ+1Pv7n1BrERG+tG+p5uWjPunY R+epynaRZyTexXa/FdBeWgGWRRfB+HLb4Wvoc9hLsSfkOZKTT0mQdJDmpCyrOBoNaHZ8 ileQ== X-Gm-Message-State: AO0yUKWkTWgL3tXJde0Ee/oRbRAmwWWnYYrb8AVtawbwCXb9In6v2LT5 heTVu7zM3/2ooVXHv9Vq4jlz3TvpQB8= X-Google-Smtp-Source: AK7set9uyjb5MnG/k7yofyXGpLltrpGM7U4Uhuf+8OT8q+CXc3xORa++jsgQgEDOC3sNPp8HlPpWsq/17dc= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a62:1c08:0:b0:590:762f:58bc with SMTP id c8-20020a621c08000000b00590762f58bcmr2799601pfc.50.1675989139088; Thu, 09 Feb 2023 16:32:19 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:44 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-18-seanjc@google.com> Subject: [PATCH v2 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 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.39.1.581.gbfd45094c4-goog From nobody Fri Sep 12 11:41:05 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 8FBC6C05027 for ; Fri, 10 Feb 2023 00:34:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231181AbjBJAeE (ORCPT ); Thu, 9 Feb 2023 19:34:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53092 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230286AbjBJAd1 (ORCPT ); Thu, 9 Feb 2023 19:33:27 -0500 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 49AE43865D for ; Thu, 9 Feb 2023 16:32:38 -0800 (PST) Received: by mail-pg1-x549.google.com with SMTP id h126-20020a636c84000000b004d31ad79086so1757379pgc.23 for ; Thu, 09 Feb 2023 16:32:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=GV8Qd4jwtvFFFyzJG7jPBupn1ncGCYAxnl51ikPvDjg=; b=RfaxmpJmG9jL4AGZCBmKU4zsa8HltXgf43LDd6T84KlHljb9TGwwAfKaXfK+icJs+c pUvJL17rd027NxDrs4pR5w1EiXGNc6npCDSiyeAhJYe6/ellqC6mmwWc0KgQooBOE/oT xGwtUBZon57cuW9J5Efbvqu5+KlDkNdFI3uf7mlxBuJ00ndVI2VxZgT5SwgGR01NLerQ X0XEwC1GhiSrXG7DjUGVWnrDUE0Ug97iS7igTHVoFu/SilkEljhC3/hFLmCSga9nwY5F 0WUACeMZBk3hONJO9vTn8DBj+1Pd9U20dpofJ3wKywCnk7FuFcmbq1DVgx9LVTOyK3p6 2FgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=GV8Qd4jwtvFFFyzJG7jPBupn1ncGCYAxnl51ikPvDjg=; b=k6DOndbAhN6XubdlL0Lxq8uhad1e+D/Maead1irFF64cZjKDzfx4IRLH1Cxt1nBDWj 1hxXj2WSJUR7KjRpdZEFTeDjMNuuhqGUEAEBPut/fxTpH2BbRZkgZOfFDukRpMXdFuGl rMVQUFCflKebdYw/85AV1EdLl4/mYIq6M20a9sYm5r5e3/NQ5efBjuvTRAc0YM21U5kL 5K700WyZBFoNN0MpCvOOOhW5ZsFB/tvLe8Omp7t33V7pJULeeVBa5TrlJv+eLhZGFwCj 8HyZUkY+AcRFBUwtTX5z5TMFUVcV/5OKhTHoLoObANCOSUE8ZjBQBn58Wn1KMjJ+LxZo WmDQ== X-Gm-Message-State: AO0yUKVseMzxXZCmG2AMnVX6NYp0os5T8nJV/+eTmGtRDqfQy5MWMUJj rzymJ6xNAbRp/+a6/+jl1NBdjSI6LK4= X-Google-Smtp-Source: AK7set/M+sHPDt/EnXMFYsTwY4C0va9HLF/aPs6/hhxt23x5YMe3QsKGfhoONHIBb6R/PiS+LP3u8hwzlL0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:b915:0:b0:4d8:12fd:cd3b with SMTP id z21-20020a63b915000000b004d812fdcd3bmr2616270pge.115.1675989140811; Thu, 09 Feb 2023 16:32:20 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:45 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-19-seanjc@google.com> Subject: [PATCH v2 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 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.39.1.581.gbfd45094c4-goog From nobody Fri Sep 12 11:41:05 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 68165C61DA4 for ; Fri, 10 Feb 2023 00:34:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230462AbjBJAeH (ORCPT ); Thu, 9 Feb 2023 19:34:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230249AbjBJAda (ORCPT ); Thu, 9 Feb 2023 19:33:30 -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 89F7F366BB for ; Thu, 9 Feb 2023 16:32:43 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id o18-20020a17090a5b1200b00230e9fe4ea0so1695115pji.8 for ; Thu, 09 Feb 2023 16:32:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=T/UerwQEfGQ6ukyZTZGTyb45H/ylk3/l1LtZ4//l0d4=; b=rplEd0JY5QtWrqEvlJVu1JobeJWDbvpgObNM1O1mRNIK1hMyw8fLr0S5jWfjz8m4N1 NBLYL1+OCDE/E6PftHc5JRK2NOqyekYjPhonKL2CQdBfD+amuWo5T6MnQSCBHh7elFcV /c67IKrupSWuqUqhOrndgPZBl8NRN1KHb9pOYZfJrqtH57Gn6+ZF/f0h1WmJbdREzQEB 33L3Z8w4OVTVHVzsmFqwLQBuSEr1HL/VHpd04qn1S7DWJs4LqLpAddtAh4ZeBxVf3Akg tIiJLoYv4p8WHaq8+JL5ndl+QFh6eIncRLr01uGRzXV9fBHNklO1Ny8t7DILVUnyVOJ8 ycrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=T/UerwQEfGQ6ukyZTZGTyb45H/ylk3/l1LtZ4//l0d4=; b=OiS7Ng3EeokYmIZOT3G+jFpcjGsyGJ4xChgPFX2pOQvPID/CgU2zmePT0/SyZ8tBIU 0XG2ntX92wY4LZOxZJQSJhj38ShJbANpJ6D86acqtKhesKUp0E+rH5jDCF0Kh9L2I4RE X9TsnXOlGO+Vvmigw8hPqO/Bqv7f4c/omnwXetoFPDmsoFijAfmhansLc6g0/hGqqV/k nsM334xh+FltRjssDhfV+vKuyvXIXn2IPF65vCWG8NM/2QFSohqzfnrykUdZGPQcpv5L 5Eq3vx59b+fMFu8mVplrsHsUE7SbxG4cH1qtuLmRvSp+QC9+fIHwfaTxmAIt+xc5l+Fm 66uA== X-Gm-Message-State: AO0yUKXv/RkSVlrzIN90kOLAHxNXcHJA4L6lI7N45NLtx5M6bPnS3rUX TMSbv3//K23zTnf2jV5E3dUQac+lShk= X-Google-Smtp-Source: AK7set/hIh5XI11TrLn8niHD6cLvpVN7xNs2K6AXFlFsylpTDyIq09h5jMw/u2D4zr57xsRoDgQFWhl8nuM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:d716:b0:230:ee6f:28bc with SMTP id y22-20020a17090ad71600b00230ee6f28bcmr11586pju.1.1675989142716; Thu, 09 Feb 2023 16:32:22 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:46 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-20-seanjc@google.com> Subject: [PATCH v2 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 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.39.1.581.gbfd45094c4-goog From nobody Fri Sep 12 11:41:05 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 D6891C636D3 for ; Fri, 10 Feb 2023 00:34:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230525AbjBJAeT (ORCPT ); Thu, 9 Feb 2023 19:34:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53106 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230140AbjBJAdc (ORCPT ); Thu, 9 Feb 2023 19:33:32 -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 7C2A570960 for ; Thu, 9 Feb 2023 16:32:45 -0800 (PST) Received: by mail-pj1-x104a.google.com with SMTP id l18-20020a17090add9200b00230f60889d6so1595704pjv.3 for ; Thu, 09 Feb 2023 16:32:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=zEtJJXP/d2l8cjo3sRdPdCH/uW1qOGz7yYT36ga6XYA=; b=OIn25dnGFHK3/GsuBf+shRFLsxjVK3A7daMICpgIh6b1/dOcT0jb4kv+ccWY8XrZYW Sh4A5EYfXrqr054hEP0OLCQX1e4XV3tdYGdgi0LcQkkd32Lf9sLdc999ImHUh4Wx9SVy 97ZQ+gMw31UrvhT8Gzbrh2YgeNsxWrp5IWX3SDN+xomuh/7zQp3uUeuVnOzqlB2mftKs UIVT6kpcyNPBKO4dyxWeeQJ20PSw2X2W9oroWk3uKEC2ceVgxgZ3n0/QTS2wZsta3Pf4 LGtUx/enKWsPMrgI5Qlt+FI7kYZmN6+1+E1fniNFKY148RvTdb6HJAI040rD//yjvXiJ 3PAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=zEtJJXP/d2l8cjo3sRdPdCH/uW1qOGz7yYT36ga6XYA=; b=owN4jxAlzx/WaxUE+h9dYAa3dBfKZ/A8pnhaJ1f6ZHZh22QAE6BZp3i59MZTeo0mT3 X7qGcSFPdn2GQuod4H+PIFuV11LqZ9yafblGpWydg6RyJsIibkFAqm3+J8QoKlOr+O+z RsHUGkEOhDnd2WH1DyXXEdyzV9JwptjyyijUlmylk9y7Y5Op6RZTemv4JFCxy50mSGeF wT8G1Cb8WYgK/Auy+wp9OW5xpnlvfDb3lX/sp61LJl6AFquei1imiS3DKLca9Oj76kH0 tLJoJQQjW4k1eAoczoHARqL8O91RofQlWzKW7MD/YcRqzQTqbz4huf6zk1/m4X3hv/wd QZ7w== X-Gm-Message-State: AO0yUKVDPhJeq+DTcWHoPAqufAr4EVrgWh5DQRzLc3x07XSnxG5RsH31 DFGP1TjtdmUPWOkACwD6F0PwSscxoSM= X-Google-Smtp-Source: AK7set8fdxQ/hgqraZET/UcBwzSWB1b4GLp3bl9sSvC5JBGhr4UIh8IzLGN4SiZYjs8ajErEAAl9cD0tOQI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:aa7:9a09:0:b0:593:8deb:820a with SMTP id w9-20020aa79a09000000b005938deb820amr2872910pfj.53.1675989144638; Thu, 09 Feb 2023 16:32:24 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:47 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-21-seanjc@google.com> Subject: [PATCH v2 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 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.39.1.581.gbfd45094c4-goog From nobody Fri Sep 12 11:41:05 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 3FEE6C05027 for ; Fri, 10 Feb 2023 00:34:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231225AbjBJAeY (ORCPT ); Thu, 9 Feb 2023 19:34:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229674AbjBJAdq (ORCPT ); Thu, 9 Feb 2023 19:33:46 -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 6DFB67096A for ; Thu, 9 Feb 2023 16:32:47 -0800 (PST) Received: by mail-pf1-x449.google.com with SMTP id u3-20020a056a00124300b0056d4ab0c7cbso1769129pfi.7 for ; Thu, 09 Feb 2023 16:32:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=J6Gk09VCdivQ/Y/BQoeE1Efc/j/nP2LOhjM52BokYHY=; b=QjfcNWfF+9MosGU5G67r8yr2rtgu0nmHJmhphY7KHFmTTQSKrVcPSVIYRFvfe0RNXf 5ulfvqqBYZ4AyrtK0O08jFiAWUCdJqaFantIPhan7pmEAJHv4B97orVlhsuitHwMvf/R L9WiLGUq4kHCb4/AwzyJm4tArS6NfvH2di3UwYs4LqHeThgzt87PLN9gdkTivZKRwoIw IvaXKhYrUWnv+KNnKkhIwYgxc6P1q8+mKPdM5UE5spmX2CcpJyh+lTMadIS4JIZLLhI/ ZOBkI6q0zgV3XX+lX4v8002KDf5yK3fjEfHzfM/3VEOijq46rrZ1eJvtVu9AXpyV+GRl OS9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=J6Gk09VCdivQ/Y/BQoeE1Efc/j/nP2LOhjM52BokYHY=; b=1PcikNQA6WEtHGH6ipprXYrDQ5Pk11k30j+ql5F4/xDqqTav2dotlvg2ugyLSTiXc6 QTqtUvEmQJmpuPP8wdIGhtOLviP11Zzgrl8j3UotkQeQ9sNA+EoHJVhGt0AFqnURICbF BDuyER/hNOTPWOkGalFs4dnln67BsdLReDaWdfVUR1mWfgccDx5ppWWei7CXco4fYSh1 wp5cUa078Fly7zaqYNvKHeIcU4mMZEGsG9IC/AwWja99NSIvX7oCEqPoPPW2v6WvpZeI pnx11sLuh+XrtjvSYvU2wD0L5huhmiBvnHM+9BcUWg3Hfuu+qBvoWAGAW9sIWaIHXPiN sedw== X-Gm-Message-State: AO0yUKUg7l/hgybX9E9VwZHnhh6CRTB2ViKILVM95ZHjl8EE8eGEWz9d D7d3aohzEySmf7iVeM4MUlF/8MzjyfU= X-Google-Smtp-Source: AK7set8Tf+Mu4344x73YHYpL2zzFxQMThn8EfRNvY0tOHY8icvYWnk+m0wIurpkbQyNmq8saBDnIqVaXXvA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:be48:0:b0:4fb:2c72:9168 with SMTP id g8-20020a63be48000000b004fb2c729168mr1791740pgo.85.1675989146248; Thu, 09 Feb 2023 16:32:26 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:48 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-22-seanjc@google.com> Subject: [PATCH v2 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 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.39.1.581.gbfd45094c4-goog