Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3035853rwb; Mon, 15 Aug 2022 16:35:59 -0700 (PDT) X-Google-Smtp-Source: AA6agR7Oxv5uGfXnseorhEVraMphc2cD1zR3zmOba2sh0/rPgONnV62fUsPLgFmk12Cawkg1MibZ X-Received: by 2002:a05:6402:611:b0:43c:cb2d:76c0 with SMTP id n17-20020a056402061100b0043ccb2d76c0mr16512294edv.425.1660606558899; Mon, 15 Aug 2022 16:35:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660606558; cv=none; d=google.com; s=arc-20160816; b=e4v1ub1jvaiait1tLDCfPWD5zQOoVIJDUB0lv2J59yoPgu7e7839yPbm997JjSQnIw UDnY0xTWwqRkAae+y3sPj9Nun/+TrUhSSTze/gArIyWwYD/xzdwGSe0vNzRjE4XktEJb EhyraqwhwrFx8sP8LltwAD4fdA/+1HrOSWXAue01LM6/0YJOsNJvxDCqdPJQhexzFcKu j040LEn/8ZWkGdd9ZPRcpx9ay3SQNMkuyyuDwjznuozeXmmigUQ+PwEGHKOT/Lz7oFnp zCKHpiwTWQx9XvXIIDZcLa7cYXv4NgJBYf0g8J/8qvFQhi+T1At1SoHhV08Fu93Mu9NT iEwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=HboXwcwuHe5z18fbGnBgutES4Nj6oHnLu/2UdfX7Fj0=; b=JdByB6fKIpKqPKThmsTuRQ4ViPPhKgDnIqYjd25a6bXB7Db7ze/DPYEVUTj+nO7Puq OQVs3Iv1CueshrH9ttWBuj+4R6A+nLd3xi9ULGxZ7EqcfHNwwiyH6Hx6fia8rCCz1GJD loPO9LngtRMqbFGnkaCFyUNvw2sjCDZvE+KtRJPHTQ00tLkMv4uk4h9NwgQj6HKcDFlF jyxlHEu5xN6GmYTjo96l8OoNbQ9PPTxpVuD6+KOymDrhY3aVMh4lQj2u3AFKgJ5vKxpt 4MdojHkfIloRlnIWGaooIZtxWvBRjQrIFYRdA5gXtTygIVXslnnceIk4YA7NhYe2G3jm Ln1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=i5g1rADz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id fj3-20020a1709069c8300b00730c3a98892si10583515ejc.978.2022.08.15.16.35.23; Mon, 15 Aug 2022 16:35:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=i5g1rADz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244240AbiHOXce (ORCPT + 99 others); Mon, 15 Aug 2022 19:32:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43398 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344156AbiHOX0K (ORCPT ); Mon, 15 Aug 2022 19:26:10 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 820828048A; Mon, 15 Aug 2022 13:06:40 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 168396069F; Mon, 15 Aug 2022 20:06:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0BCB8C433C1; Mon, 15 Aug 2022 20:06:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660593999; bh=M5ZHIbu1TKuEQtFj2vxw4lKQ1U5GhVRC3YEpgB+BG6k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=i5g1rADzvzs6jtykUn/TNBuMGb6jfLrQHxI9DJLnNEfOCfW81EL2rLBs7HEK04uIY r9Pg8FbXLSlvWgK7xuGauWwimGoaDpqafSR6WQHXw7009hElOsL7unAlXVqvgS69Do 8hAkSTTwYJsyQZKpSx7gBPFjt/RAXEqp/OlvRbYY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sean Christopherson , Paolo Bonzini , Sasha Levin Subject: [PATCH 5.18 1040/1095] KVM: VMX: Add helper to check if the guest PMU has PERF_GLOBAL_CTRL Date: Mon, 15 Aug 2022 20:07:19 +0200 Message-Id: <20220815180512.120876879@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180429.240518113@linuxfoundation.org> References: <20220815180429.240518113@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Christopherson [ Upstream commit b663f0b5f3d665c261256d1f76e98f077c6e56af ] Add a helper to check of the guest PMU has PERF_GLOBAL_CTRL, which is unintuitive _and_ diverges from Intel's architecturally defined behavior. Even worse, KVM currently implements the check using two different (but equivalent) checks, _and_ there has been at least one attempt to add a _third_ flavor. Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson Message-Id: <20220722224409.1336532-4-seanjc@google.com> Signed-off-by: Paolo Bonzini Signed-off-by: Sasha Levin --- arch/x86/kvm/vmx/pmu_intel.c | 4 ++-- arch/x86/kvm/vmx/vmx.h | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index 2cbd5f183ab5..8bd154f8c966 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -98,7 +98,7 @@ static bool intel_pmc_is_enabled(struct kvm_pmc *pmc) { struct kvm_pmu *pmu = pmc_to_pmu(pmc); - if (pmu->version < 2) + if (!intel_pmu_has_perf_global_ctrl(pmu)) return true; return test_bit(pmc->idx, (unsigned long *)&pmu->global_ctrl); @@ -215,7 +215,7 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr) case MSR_CORE_PERF_GLOBAL_STATUS: case MSR_CORE_PERF_GLOBAL_CTRL: case MSR_CORE_PERF_GLOBAL_OVF_CTRL: - ret = pmu->version > 1; + return intel_pmu_has_perf_global_ctrl(pmu); break; default: ret = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0) || diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index 1e7f9453894b..93aa1f3ea01e 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -92,6 +92,18 @@ union vmx_exit_reason { u32 full; }; +static inline bool intel_pmu_has_perf_global_ctrl(struct kvm_pmu *pmu) +{ + /* + * Architecturally, Intel's SDM states that IA32_PERF_GLOBAL_CTRL is + * supported if "CPUID.0AH: EAX[7:0] > 0", i.e. if the PMU version is + * greater than zero. However, KVM only exposes and emulates the MSR + * to/for the guest if the guest PMU supports at least "Architectural + * Performance Monitoring Version 2". + */ + return pmu->version > 1; +} + #define vcpu_to_lbr_desc(vcpu) (&to_vmx(vcpu)->lbr_desc) #define vcpu_to_lbr_records(vcpu) (&to_vmx(vcpu)->lbr_desc.records) -- 2.35.1