Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp17109pxb; Thu, 7 Jan 2021 17:50:59 -0800 (PST) X-Google-Smtp-Source: ABdhPJzwDQ1abpCIx2D6UcbRF0voodVb0rNRzSGf7Y68EZXyNB+D0xpdiet1HRkKfwLTBAJMyp9i X-Received: by 2002:a17:906:3fc4:: with SMTP id k4mr1074437ejj.137.1610070659250; Thu, 07 Jan 2021 17:50:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610070659; cv=none; d=google.com; s=arc-20160816; b=l1Sg9iqtTEmVbuRpYaBvL5L4x1AilrmnVbxOoIj8WIQJgtXka9rfy3n6yHmSR1ux1I Ytaujo6uWX2KTRbdI5kY7BtL6J8W3k+3DkKlmt7b/b2klmHvOR3zS4NHpxBgI05H2K/C 3YZrjssQC0sokkaE/swPnz06O2Zy5WPrAv+iAWyWwHMKxn/1XFGYEoH0GrF8HEw20M0O DoGynIF2KFa+LQ7xONMkx/WqriDJ9mJpShL4q6LAM9Rkklc9AwNKyM2lkjSyr26gxhP9 DKPcYtPbS1jLcxBTdvziGuy9xZLP1t15RNhVJePGK69QA2VT4YjohM2V0Tv2No4wxoN7 GH8g== 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=GdIW2C3lG6siMomlJQSuUgICYfWaDZYtLLBySzov/ZY=; b=PsFyJSI6CNF7IVnSyJD5QRbr4JB5Oi9MNRwpX+ffwPty/Ja3z4xF3yKWaauekXLsCa 6nJ4+plNABPuUpTD6EY4Agk0pWSMWYuWsVZ9zkZ/qg/FEs06CIRUaxGoXn5R0kPpVQ0C KuJvS3v25avCbkY1vK0zEnCauCbwLD7+wDW1CSVhZIVxmYrF8B5uhDnqbOKvBqRJhSx+ LefR6rh/CcES2rlWPLbJ+jCp0V623RgvMT8A/Tc+8Uk6hDkSafU825Dui92kGfwjh7gc a3Leez61CdTrIjHY96/lnJOktgqHAOWT4PPmT5SBr7Lp4gYSa4JhteQ/ZiD2m5gR6xjw a0XA== 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 a89si3024582ede.326.2021.01.07.17.50.35; Thu, 07 Jan 2021 17:50:59 -0800 (PST) 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 S1730008AbhAHBsz (ORCPT + 99 others); Thu, 7 Jan 2021 20:48:55 -0500 Received: from mga09.intel.com ([134.134.136.24]:27995 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729922AbhAHBsy (ORCPT ); Thu, 7 Jan 2021 20:48:54 -0500 IronPort-SDR: 9iiN9hjdI2XP1Ybd+XHPweuAU4RgZiiXUcjOC6OtcdKr98/nNi6+p7v6IbUjGPiWa11a5qxIb+ p6LEFADOj9HQ== X-IronPort-AV: E=McAfee;i="6000,8403,9857"; a="177672424" X-IronPort-AV: E=Sophos;i="5.79,330,1602572400"; d="scan'208";a="177672424" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Jan 2021 17:43:40 -0800 IronPort-SDR: LmWcOyDft4qsOBX5+mNuAS80gt1r8VU1nrAdVFa1o1EQqs3w5SAFF/t0pvL1WUk2CgI/+8Hh6e E6xx5m2rt0gg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.79,330,1602572400"; d="scan'208";a="379938134" Received: from clx-ap-likexu.sh.intel.com ([10.239.48.108]) by orsmga008.jf.intel.com with ESMTP; 07 Jan 2021 17:43:36 -0800 From: Like Xu To: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel Cc: Ingo Molnar , Thomas Gleixner , Borislav Petkov , "H . Peter Anvin" , ak@linux.intel.com, wei.w.wang@intel.com, kan.liang@intel.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RESEND v13 10/10] KVM: vmx/pmu: Release guest LBR event via lazy release mechanism Date: Fri, 8 Jan 2021 09:37:04 +0800 Message-Id: <20210108013704.134985-11-like.xu@linux.intel.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210108013704.134985-1-like.xu@linux.intel.com> References: <20210108013704.134985-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 The vPMU uses GUEST_LBR_IN_USE_IDX (bit 58) in 'pmu->pmc_in_use' to indicate whether a guest LBR event is still needed by the vcpu. If the vcpu no longer accesses LBR related registers within a scheduling time slice, and the enable bit of LBR has been unset, vPMU will treat the guest LBR event as a bland event of a vPMC counter and release it as usual. Also, the pass-through state of LBR records msrs is cancelled. Signed-off-by: Like Xu Reviewed-by: Andi Kleen --- arch/x86/kvm/pmu.c | 7 +++++++ arch/x86/kvm/pmu.h | 4 ++++ arch/x86/kvm/vmx/pmu_intel.c | 17 ++++++++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index 405890c723a1..e7c72eea07d4 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -463,6 +463,7 @@ void kvm_pmu_cleanup(struct kvm_vcpu *vcpu) struct kvm_pmc *pmc = NULL; DECLARE_BITMAP(bitmask, X86_PMC_IDX_MAX); int i; + bool extra_cleanup = false; pmu->need_cleanup = false; @@ -474,8 +475,14 @@ void kvm_pmu_cleanup(struct kvm_vcpu *vcpu) if (pmc && pmc->perf_event && !pmc_speculative_in_use(pmc)) pmc_stop_counter(pmc); + + if (i == INTEL_GUEST_LBR_INUSE) + extra_cleanup = true; } + if (extra_cleanup && kvm_x86_ops.pmu_ops->cleanup) + kvm_x86_ops.pmu_ops->cleanup(vcpu); + bitmap_zero(pmu->pmc_in_use, X86_PMC_IDX_MAX); } diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h index 742a4e98df8c..c8b650866f56 100644 --- a/arch/x86/kvm/pmu.h +++ b/arch/x86/kvm/pmu.h @@ -15,6 +15,9 @@ #define VMWARE_BACKDOOR_PMC_REAL_TIME 0x10001 #define VMWARE_BACKDOOR_PMC_APPARENT_TIME 0x10002 +/* Indicates whether Intel LBR msrs were accessed during the last time slice. */ +#define INTEL_GUEST_LBR_INUSE INTEL_PMC_IDX_FIXED_VLBR + #define MAX_FIXED_COUNTERS 3 struct kvm_event_hw_type_mapping { @@ -40,6 +43,7 @@ struct kvm_pmu_ops { void (*init)(struct kvm_vcpu *vcpu); void (*reset)(struct kvm_vcpu *vcpu); void (*deliver_pmi)(struct kvm_vcpu *vcpu); + void (*cleanup)(struct kvm_vcpu *vcpu); }; static inline u64 pmc_bitmask(struct kvm_pmc *pmc) diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index 8120685c43d4..4d10f564607d 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -310,6 +310,7 @@ int intel_pmu_create_guest_lbr_event(struct kvm_vcpu *vcpu) } lbr_desc->event = event; vcpu_to_pmu(vcpu)->event_count++; + __set_bit(INTEL_GUEST_LBR_INUSE, vcpu_to_pmu(vcpu)->pmc_in_use); return 0; } @@ -342,10 +343,12 @@ static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu, rdmsrl(index, msr_info->data); else wrmsrl(index, msr_info->data); + __set_bit(INTEL_GUEST_LBR_INUSE, vcpu_to_pmu(vcpu)->pmc_in_use); local_irq_enable(); return true; } local_irq_enable(); + clear_bit(INTEL_GUEST_LBR_INUSE, vcpu_to_pmu(vcpu)->pmc_in_use); dummy: if (read) @@ -496,7 +499,8 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu) if (!intel_pmu_lbr_is_enabled(vcpu)) { vcpu->arch.perf_capabilities &= ~PMU_CAP_LBR_FMT; lbr_desc->records.nr = 0; - } + } else + bitmap_set(pmu->all_valid_pmc_idx, INTEL_GUEST_LBR_INUSE, 1); pmu->nr_arch_gp_counters = min_t(int, eax.split.num_counters, x86_pmu.num_counters_gp); @@ -669,17 +673,21 @@ static inline void vmx_enable_lbr_msrs_passthrough(struct kvm_vcpu *vcpu) */ void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu) { + struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu); if (!lbr_desc->event) { vmx_disable_lbr_msrs_passthrough(vcpu); if (vmcs_read64(GUEST_IA32_DEBUGCTL) & DEBUGCTLMSR_LBR) goto warn; + if (test_bit(INTEL_GUEST_LBR_INUSE, pmu->pmc_in_use)) + goto warn; return; } if (lbr_desc->event->state < PERF_EVENT_STATE_ACTIVE) { vmx_disable_lbr_msrs_passthrough(vcpu); + __clear_bit(INTEL_GUEST_LBR_INUSE, pmu->pmc_in_use); goto warn; } else vmx_enable_lbr_msrs_passthrough(vcpu); @@ -691,6 +699,12 @@ void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu) vcpu->vcpu_id); } +static void intel_pmu_cleanup(struct kvm_vcpu *vcpu) +{ + if (!(vmcs_read64(GUEST_IA32_DEBUGCTL) & DEBUGCTLMSR_LBR)) + intel_pmu_release_guest_lbr_event(vcpu); +} + struct kvm_pmu_ops intel_pmu_ops = { .find_arch_event = intel_find_arch_event, .find_fixed_event = intel_find_fixed_event, @@ -706,4 +720,5 @@ struct kvm_pmu_ops intel_pmu_ops = { .init = intel_pmu_init, .reset = intel_pmu_reset, .deliver_pmi = intel_pmu_deliver_pmi, + .cleanup = intel_pmu_cleanup, }; -- 2.29.2