From mboxrd@z Thu Jan 1 00:00:00 1970 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 337D2C4332F for ; Thu, 6 Oct 2022 00:03:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229808AbiJFADl (ORCPT ); Wed, 5 Oct 2022 20:03:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44492 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229754AbiJFADa (ORCPT ); Wed, 5 Oct 2022 20:03:30 -0400 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 0956884E6D for ; Wed, 5 Oct 2022 17:03:25 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id h2-20020a656382000000b0044b0f6c572fso132778pgv.20 for ; Wed, 05 Oct 2022 17:03:25 -0700 (PDT) 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=HjxVpgNXisyN9sBypn/hCSm/iZsV2/LVKlhCgT8b+e8=; b=ruN0AC6W5Ztqa0QvB1F8i1r7EWXxBLZ5OoV5I/k6Y4Ic+syMQaA/93QLmKYLpFi4YM A5IcyUYjjZOIlMTOhbgHarjZuWu3KLJPADE5VWXaY8jQclkz+aNPytfwX9GQqnfsi6Kj pSvIiq7iGC/g9neZ5cti6kDzITiSrDadAiEO2CGnm8Of/QQ+ZurndAsZJXF1SsoGcMrd fP39yW2WqsXq5bjVRB0sBKG5rLInvN6GiNveBfx8s334+K7+yhBDKNL5dXaBfrgZ/ki4 MvI9NEJGxzhBUeIfHiZ24Z12/ghgrmB3wedHWEGGR6gKT/YF6is1Zs6p9XdQWaZeasoG CT1g== 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=HjxVpgNXisyN9sBypn/hCSm/iZsV2/LVKlhCgT8b+e8=; b=JUaVmOohd9dMXvNSmsZXHvdaXg0epeXnAJI5EznaRQia5YbM0OW3W/Oe+Cy2DblYI+ mUyTVjGD87tTp6XWex3A1UvRcMPq28kcmw3X4wS8N/kPcEUlkIup8pNqGh5HCB3QgvJJ 21ZulFnG8rfswjSGcgZ3PkU+ZOOyj2wb14c3/leyg/RcMZhH+z7p8xfo7JH4uj9WDJZg 7bDiMGGAymvuoTPzMF3VZpfJM7jQPjd/9EkvUSKoM6/gKEMxP8Kk7hdpWOx4cFBOrsGr mZCdQKfvs7AKT5Kzc4w+tqJixo4TYBONaZOKPhApYbqUS7uqXIbyFU6CMwgZNBAXwAw6 UgaQ== X-Gm-Message-State: ACrzQf0IWGOFDa5BTcBWy/BAOCuoX3ZsOx6ysBgVBD20j2mfa91vr2yT eGNr8MTL/kxe1KqNx2RALFjYqj2PfVw= X-Google-Smtp-Source: AMsMyM4N/Zl8uSic3XOrOHu86WdR+m/a+W1ikd/K0K7LrmvDvn4vJN9uYIRkwmvFZF0FGbswaXQnl8x2FlI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:e545:b0:177:e335:9757 with SMTP id n5-20020a170902e54500b00177e3359757mr2036698plf.152.1665014604559; Wed, 05 Oct 2022 17:03:24 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 6 Oct 2022 00:03:10 +0000 In-Reply-To: <20221006000314.73240-1-seanjc@google.com> Mime-Version: 1.0 References: <20221006000314.73240-1-seanjc@google.com> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog Message-ID: <20221006000314.73240-5-seanjc@google.com> Subject: [PATCH v5 4/8] KVM: VMX: Ignore guest CPUID for host userspace writes to DEBUGCTL From: Sean Christopherson To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Sean Christopherson , Paolo Bonzini Cc: Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Like Xu Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-perf-users@vger.kernel.org Ignore guest CPUID for host userspace writes to the DEBUGCTL MSR, KVM's ABI is that setting CPUID vs. state can be done in any order, i.e. KVM allows userspace to stuff MSRs prior to setting the guest's CPUID that makes the new MSR "legal". Keep the vmx_get_perf_capabilities() check for guest writes, even though it's technically unnecessary since the vCPU's PERF_CAPABILITIES is consulted when refreshing LBR support. A future patch will clean up vmx_get_perf_capabilities() to avoid the RDMSR on every call, at which point the paranoia will incur no meaningful overhead. Note, prior to vmx_get_perf_capabilities() checking that the host fully supports LBRs via x86_perf_get_lbr(), KVM effectively relied on intel_pmu_lbr_is_enabled() to guard against host userspace enabling LBRs on platforms without full support. Fixes: c646236344e9 ("KVM: vmx/pmu: Add PMU_CAP_LBR_FMT check when guest LBR is enabled") Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 97fc873c37fa..e70ac91cd2fb 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2021,16 +2021,16 @@ static u64 nested_vmx_truncate_sysenter_addr(struct kvm_vcpu *vcpu, return (unsigned long)data; } -static u64 vcpu_supported_debugctl(struct kvm_vcpu *vcpu) +static u64 vmx_get_supported_debugctl(struct kvm_vcpu *vcpu, bool host_initiated) { u64 debugctl = 0; if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT) && - guest_cpuid_has(vcpu, X86_FEATURE_BUS_LOCK_DETECT)) + (host_initiated || guest_cpuid_has(vcpu, X86_FEATURE_BUS_LOCK_DETECT))) debugctl |= DEBUGCTLMSR_BUS_LOCK_DETECT; if ((vmx_get_perf_capabilities() & PMU_CAP_LBR_FMT) && - intel_pmu_lbr_is_enabled(vcpu)) + (host_initiated || intel_pmu_lbr_is_enabled(vcpu))) debugctl |= DEBUGCTLMSR_LBR | DEBUGCTLMSR_FREEZE_LBRS_ON_PMI; return debugctl; @@ -2105,7 +2105,9 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) vmcs_writel(GUEST_SYSENTER_ESP, data); break; case MSR_IA32_DEBUGCTLMSR: { - u64 invalid = data & ~vcpu_supported_debugctl(vcpu); + u64 invalid; + + invalid = data & ~vmx_get_supported_debugctl(vcpu, msr_info->host_initiated); if (invalid & (DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR)) { if (report_ignored_msrs) vcpu_unimpl(vcpu, "%s: BTF|LBR in IA32_DEBUGCTLMSR 0x%llx, nop\n", -- 2.38.0.rc1.362.ged0d419d3c-goog