Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp2901390pxb; Sun, 8 Nov 2020 18:21:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJyyzPzlMSaqkfD0ekUbB25imvvcVSgTfyaeM7EhRaJWae/9sGQdF4FeYueuS3WD0hIE1k8T X-Received: by 2002:a17:906:dbf4:: with SMTP id yd20mr12862257ejb.53.1604888505009; Sun, 08 Nov 2020 18:21:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604888505; cv=none; d=google.com; s=arc-20160816; b=UTMIWDq4SP0tmJzEJlA0tdEKRNb7hSiRneIgqCpcyCJlRF1xrLiO7t9izf4yjECPQA uMroSwgmkme0PVsATXNdI6wENF+e4twSYwgnkjuPgKhXzpLgWH3TYzPa3JcATEhXjpwv o0odjoKj53ABHaPNLTi0Rzzeows0fZNrAjCOsCf4P6G50064wdtmpkVBOaEYEkQ94K1t PHnhvWkz4WpiEQpP2fzAiQA5jt/skLhMjeQCapZditUVuhjx0pKjVky7iCw2Vp7xUS8y yIroSQ5FNHW2EvWDS5vbU6ttG75UevPLIbLdaFgMKMzoAg88i8RwXWokZdE+LBsn2K25 opfA== 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=YCD35Sr3tasVqCgp8kzpTvXrqJGA3zIwHuSPRhTu2oo=; b=lFUYwrUDuKeA9XFE7QgIvY4sQksHA5KbmWPn5ZgLxkyYVw2x5q22MSScRIhlRBelIG g9v/ZL/zhF57rzn/3Z4/MZZlk93+zL+oKYUKyPOZxMoNzXk5o88HUv/SrCvG6vUS7rF4 W3ptReYsjiZxFdQEt4olhIVtNWFMRGNrHqqhfh2jpOY4ItLTwnTlCLuAwwoRxo5wia66 goH5aeHuBvAN4lbsdEsyduWFxF4lO+gQ8BAJ06so1rrEF4RQJsA02t3mY3+e15ifMfX6 CHnVYOYcizSn3id/vARNxYvtCqNGPb4GZJzBEYBTfIT6iFkulQbdFKngOE1ZqlQ9f6RW zfTQ== 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 k12si6106669eds.191.2020.11.08.18.21.22; Sun, 08 Nov 2020 18:21:44 -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 S1729430AbgKICRs (ORCPT + 99 others); Sun, 8 Nov 2020 21:17:48 -0500 Received: from mga01.intel.com ([192.55.52.88]:64940 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729406AbgKICRl (ORCPT ); Sun, 8 Nov 2020 21:17:41 -0500 IronPort-SDR: QTcoxYNBSx9DY28Upe4qlUMOa1PWli1vED5iIEPmwiHfehGUATzyhq5HMtL8GcC0PLovHRDo8Y NsHyIzqiK53w== X-IronPort-AV: E=McAfee;i="6000,8403,9799"; a="187684647" X-IronPort-AV: E=Sophos;i="5.77,462,1596524400"; d="scan'208";a="187684647" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2020 18:17:41 -0800 IronPort-SDR: 9sRnK9CDdGsfGnWmzEA/DNbJMWbubFNiwCiZGcQDFxpncvlUiFyakvm5hygfRlJ4+LIgrxkfxS nZuRkFTt5obw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.77,462,1596524400"; d="scan'208";a="540646259" Received: from e5-2699-v4-likexu.sh.intel.com ([10.239.48.39]) by orsmga005.jf.intel.com with ESMTP; 08 Nov 2020 18:17:37 -0800 From: Like Xu To: Peter Zijlstra , Paolo Bonzini , kvm@vger.kernel.org Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Kan Liang , luwei.kang@intel.com, Thomas Gleixner , wei.w.wang@intel.com, Tony Luck , Stephane Eranian , Mark Gross , Srinivas Pandruvada , linux-kernel@vger.kernel.org Subject: [PATCH RFC v2 13/17] KVM: x86/pmu: Add hook to emulate pebs for cross-mapped counters Date: Mon, 9 Nov 2020 10:12:50 +0800 Message-Id: <20201109021254.79755-14-like.xu@linux.intel.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20201109021254.79755-1-like.xu@linux.intel.com> References: <20201109021254.79755-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 To emulate PEBS facility, KVM may needs setup context such as guest DS PEBS fields correctly before vm-entry and this part will be implemented in the vmx handle_event() hook. When the cross-map happens to any enabled PEBS counter, it will make PMU request and exit to kvm_pmu_handle_event() for some rewrite stuff and then back to cross-map check again and finally to vm-entry. In this hook, KVM would rewrite the state for the guest and it won't move events, hence races with the NMI PMI are not a problem. Signed-off-by: Like Xu --- arch/x86/kvm/pmu.c | 3 +++ arch/x86/kvm/pmu.h | 1 + arch/x86/kvm/vmx/pmu_intel.c | 9 +++++++++ arch/x86/kvm/vmx/vmx.c | 3 --- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index a6c5951a5728..f87be3c2140e 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -339,6 +339,9 @@ void kvm_pmu_handle_event(struct kvm_vcpu *vcpu) */ if (unlikely(pmu->need_cleanup)) kvm_pmu_cleanup(vcpu); + + if (kvm_x86_ops.pmu_ops->handle_event) + kvm_x86_ops.pmu_ops->handle_event(vcpu); } /* check if idx is a valid index to access PMU */ diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h index f5ec94e9a1dc..b1e52e33f08c 100644 --- a/arch/x86/kvm/pmu.h +++ b/arch/x86/kvm/pmu.h @@ -45,6 +45,7 @@ struct kvm_pmu_ops { void (*refresh)(struct kvm_vcpu *vcpu); void (*init)(struct kvm_vcpu *vcpu); void (*reset)(struct kvm_vcpu *vcpu); + void (*handle_event)(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 99d9453e0176..2917105e584e 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -491,6 +491,14 @@ static void intel_pmu_reset(struct kvm_vcpu *vcpu) pmu->global_ovf_ctrl = 0; } +void intel_pmu_handle_event(struct kvm_vcpu *vcpu) +{ + struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); + + if (!(pmu->global_ctrl & pmu->pebs_enable)) + return; +} + struct kvm_pmu_ops intel_pmu_ops = { .find_arch_event = intel_find_arch_event, .find_fixed_event = intel_find_fixed_event, @@ -505,4 +513,5 @@ struct kvm_pmu_ops intel_pmu_ops = { .refresh = intel_pmu_refresh, .init = intel_pmu_init, .reset = intel_pmu_reset, + .handle_event = intel_pmu_handle_event, }; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 302808ec9699..3b62907c8959 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -6556,9 +6556,6 @@ static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) if (!msrs) return; - if (pmu->counter_cross_mapped) - msrs[1].guest = 0; - if (nr_msrs > 2 && msrs[1].guest) { msrs[2].guest = pmu->ds_area; if (nr_msrs > 3) -- 2.21.3