Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2887512rwb; Mon, 15 Aug 2022 13:20:30 -0700 (PDT) X-Google-Smtp-Source: AA6agR5VByT1rFwHYP3u7rtwHJoOxtSKR9zU4PpdgVyQ5hR1xDJq/xA2ptFeaVzYNO0ss6E5fBVj X-Received: by 2002:a17:907:3ea8:b0:730:9756:7c81 with SMTP id hs40-20020a1709073ea800b0073097567c81mr11639548ejc.726.1660594829958; Mon, 15 Aug 2022 13:20:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660594829; cv=none; d=google.com; s=arc-20160816; b=IDOytGAFZt60k786lnu6Q1jkCM1H7HtYMDDmxLcU7ckbSIv5fPqtn0bHr9ULgjP+lG k4zUnJ3hqHVP6G08dxSCzI951H20MM9AEWYmWoE/XrgZxOtWbtHXYWnjkNBpXht6B3jv 0+Icdjc5FXVL2BFhxJfPsR7juBd11dumkMCeKMQBaDqeUW2k9/UdvWwz69iVveBzQhRq ixmJQi5O5UR7u9OmnIT9dCxYLmaMqnmk/sSdjHSiWl6VG0XJWQAFzaX/b85+mU8yPg6k v2vxu14LCQF5gY/1lxZd3VfohHc0fAn7QDe2Qn/Q/bu1XiTroa3vLQy4SBlhJg+gV3pe umLg== 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=DmuEt9tgSMxvO4YDsyGnCUnu1BGoiDFPfWWF+TE8Y+U=; b=ekcJNfn5tAd7zHEJUnpuXdqdyXZyXuuH+2cjx9OkWtCuzTIov8txX3+8OCPt/t/2At 9FTP1PblWua6xKvqHbBoLcGVCDcsPjuStNicvhXdqYGgMxBHAn7jvYJ+cxMNgy3BJ++U FZaSm5WZ66iW9R9cnYOFteEE4DVp8vpT+npGvHEvVdl25d0CKQfFQb65AWKEJE/8QuOX f2QgwStk1SjMATEkSLxOwXqz6PPTLCjWV7MaL2hoIzFRm3dWcFFcgx3k84stmHp5N/Yt 6QyuEAQXSx62fWmDoJ+OETIKrxbjYMP2+EKfuCsCdlxEeataBUy+pHYb0QC6QLFKSOZJ wOxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="mjra04/j"; 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 gi3-20020a1709070c8300b0073319834e99si6174073ejc.455.2022.08.15.13.20.01; Mon, 15 Aug 2022 13:20:29 -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="mjra04/j"; 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 S244040AbiHOUDD (ORCPT + 99 others); Mon, 15 Aug 2022 16:03:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345765AbiHOT7z (ORCPT ); Mon, 15 Aug 2022 15:59:55 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C03B379A5C; Mon, 15 Aug 2022 11:53:13 -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 338836122A; Mon, 15 Aug 2022 18:53:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1F84AC433C1; Mon, 15 Aug 2022 18:53:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660589591; bh=9RVo2cv9gTDUf/SygOdjGWEFi20yyH/rO62XiMle2Yg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mjra04/jiIL0YyyhWSGe/ef/oa3xAV0wLl3IkcxzHbZmtDxGSV6OUbTwMPQ0lgvXh ZlYzzt1rH+k5Jy1vnWOBjXOoiGt+gpAJN6lXLzcT+/X1PVONdo6cTzmC49WNbg9N4R TuPW39ZBkSALzuS3kWBLrOJVOMKUKtUMGjrqVLJo= 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.15 735/779] KVM: VMX: Add helper to check if the guest PMU has PERF_GLOBAL_CTRL Date: Mon, 15 Aug 2022 20:06:19 +0200 Message-Id: <20220815180408.866071215@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180337.130757997@linuxfoundation.org> References: <20220815180337.130757997@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 f9054e7ec89e..e624a39365ec 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -104,7 +104,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); @@ -222,7 +222,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 a8b8150252bb..20f1213a9368 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