Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp975213pxx; Thu, 29 Oct 2020 20:59:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx+uwg9Q2US1M7h7orjWvOSvnlD3RPTUjL25C8+x8mzsQT9UZ1vmTqRz8vxg70MDwWMYFIY X-Received: by 2002:aa7:ce8d:: with SMTP id y13mr263978edv.65.1604030366484; Thu, 29 Oct 2020 20:59:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1604030366; cv=none; d=google.com; s=arc-20160816; b=oebBlqMMol4gQ6Y9fpu/eu+eQLHxIrSxcoxeHR+VzXeFMLlxGluRVRUYJ+CdxxJze6 NyhMiioU2k2CM355ukTLAJ63iNgK2j09uu3sFFosZd8MapLlWydvpBUNuThL4ZA79w6G e8S8R38GtM7WzNbCcTImC+PLlIfv6yW/9ON131aruZI8k62r6mzgLwStj0LGaGq+zTei LijPgaHOxAVU4z6exR5PKw8o3AGbWGpFGyynFn689dmSgL7IUtT99u8ED6UMaZ4OiRaB zgGNPAf6cVPrLhwtWwuQ3LQx7xZBwEXRKIXq9f0iDDR8BQh/kY/rzOB9InQT/ceVuj5y pMuA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr; bh=uH6+Hh4V+mXftInaW0GdunYVNwXr9tTZXwMVKPXmgkg=; b=Mw3l0fEb32pZ4Pwm61ot21AVhk0OYMla6x/l2sXRAXJHKqePalXCgyQ0dmWzSib2zy 0kR8r+iukFJW+Y2DJU/okdYZs/Qvt7WqMX8yjaLljwlEruIkcFkiJ7IJHh2HQf9EhLzm B4cCUgl1p+F8ovn74wmVN/K+hLr8SWWgq+6eWh7XarsNAelF4rnjRnCZlDobKzllhHV/ wxoiGPtH7er7WF7ZcS+MPZvrsZDlYIvl+8ccyvl5dPoT+mEDGAIdg+azleZMOjh8ZoVj EZcNja36EYg/O2s6CVSaacVtCTuRUE5SpK/GB+4NX+UlZgZnE4dSeyYpLgV6OGKYnqke N+kA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g16si3368103ejk.300.2020.10.29.20.59.04; Thu, 29 Oct 2020 20:59:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726570AbgJ3D5F (ORCPT + 99 others); Thu, 29 Oct 2020 23:57:05 -0400 Received: from mga09.intel.com ([134.134.136.24]:4255 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726564AbgJ3D5D (ORCPT ); Thu, 29 Oct 2020 23:57:03 -0400 IronPort-SDR: gO6yAgym8qJ/lDK3otQ2gwJYWzlzSg8O74/Ztil6pMFPb9Dtig1lwp9rsAiIQRlZT9A4VUr/Pk m5ckPeam+VFw== X-IronPort-AV: E=McAfee;i="6000,8403,9789"; a="168685758" X-IronPort-AV: E=Sophos;i="5.77,432,1596524400"; d="scan'208";a="168685758" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Oct 2020 20:57:03 -0700 IronPort-SDR: KfPz8EPIhu1XwpxivV5XlC1JBF1QiGzbTVf1tTeLXFIkl3pwIh9eXmk2fyQifZklwq/tBp8w35 ZO19Ofvbf+Tw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.77,432,1596524400"; d="scan'208";a="525770468" Received: from sqa-gate.sh.intel.com (HELO clx-ap-likexu.tsp.org) ([10.239.48.212]) by fmsmga006.fm.intel.com with ESMTP; 29 Oct 2020 20:56:59 -0700 From: Like Xu To: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H . Peter Anvin" , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH RESEND v13 09/10] KVM: vmx/pmu: Expose LBR_FMT in the MSR_IA32_PERF_CAPABILITIES Date: Fri, 30 Oct 2020 11:52:19 +0800 Message-Id: <20201030035220.102403-10-like.xu@linux.intel.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20201030035220.102403-1-like.xu@linux.intel.com> References: <20201030035220.102403-1-like.xu@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Userspace could enable guest LBR feature when the exactly supported LBR format value is initialized to the MSR_IA32_PERF_CAPABILITIES and the LBR is also compatible with vPMU version and host cpu model. Signed-off-by: Like Xu --- arch/x86/kvm/vmx/capabilities.h | 9 ++++++++- arch/x86/kvm/vmx/vmx.c | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/vmx/capabilities.h b/arch/x86/kvm/vmx/capabilities.h index 57b940c613ab..a9a7c4d1b634 100644 --- a/arch/x86/kvm/vmx/capabilities.h +++ b/arch/x86/kvm/vmx/capabilities.h @@ -378,7 +378,14 @@ static inline u64 vmx_get_perf_capabilities(void) * Since counters are virtualized, KVM would support full * width counting unconditionally, even if the host lacks it. */ - return PMU_CAP_FW_WRITES; + u64 perf_cap = PMU_CAP_FW_WRITES; + + if (boot_cpu_has(X86_FEATURE_PDCM)) + rdmsrl(MSR_IA32_PERF_CAPABILITIES, perf_cap); + + perf_cap |= perf_cap & PMU_CAP_LBR_FMT; + + return perf_cap; } static inline u64 vmx_supported_debugctl(void) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 2da96ec410a3..4fe1e9605ff4 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2230,6 +2230,13 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) case MSR_IA32_PERF_CAPABILITIES: if (data && !vcpu_to_pmu(vcpu)->version) return 1; + if (data & PMU_CAP_LBR_FMT) { + if ((data & PMU_CAP_LBR_FMT) != + (vmx_get_perf_capabilities() & PMU_CAP_LBR_FMT)) + return 1; + if (!intel_pmu_lbr_is_compatible(vcpu)) + return 1; + } ret = kvm_set_msr_common(vcpu, msr_info); break; -- 2.21.3