Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp2900819pxb; Sun, 8 Nov 2020 18:20:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJz9XaODqKBRmOb8IO+tetRcIK/lkSHORKIpg7le0WXSTAmiXzRCPw9AYOkepmGmlkmlc5Cv X-Received: by 2002:a17:906:50f:: with SMTP id j15mr12470869eja.198.1604888416820; Sun, 08 Nov 2020 18:20:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604888416; cv=none; d=google.com; s=arc-20160816; b=fdCGpBxX8orZl8jfO4ATO3c08dwC7Np6MBfLYYhdoRIZKcp0iARzSQe9TFKRk/mlpn 36QRDmOD7flMDXKYHInu/vUQHaO8aWgIp3r94QxMFaVk20Y02km/XHWx+IL1epa8fba0 /z4ZjHrfE/bdwUItsOqN25cDCagIMFREQZEfWu/32pugN5pEh+of3JRGpx5ooKMbVPDJ 7hvSNmqHtHGQdx8ScqK9dwCDBT8pJXKwMpqqG9ra+Dqxcz9B+JO2GyMquDJ81vQ9yDhn G0+QSLptUf9v+XpBiJINiqSAY3BAo0yEbLgFaT9FDaR0HQTN8mUGuS5Em4gq1rcC5N72 /IKA== 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=fiklqkz5zbwl+XXvouHQGx2AxcjX5Ru26VY7SYFFOi8=; b=ROZGZaHJRyS8CroXoLZlMz8F6WX+0xtWNv79J11ovwYOTiVspDxYPF/TwH5phA9WrY gr/w1+5n5n6YzmZ2ve5Txi9WD9fgNjn/U0g7GbAoUP9n95XvaAV9REKELzfIBJUg59a3 W3263kYruTvn/yliI//ZqpO6mahm3fzfm0HvrIxBN0xt9+enuCHnTKzc8DWabzUC1V2y vy0MCSJcuCEiPmUu1JNybKjKjRkAgEXeVMoxLSDDcxpdnnyW0hfKhiKeY51zsHY/LsgV QgzOfS/zfXNX1An/O7Ej9qTjuGdSd9cDBV1y7YeUrx7LGbSqHyqdV9tiYduHEMlzKSjP fw0A== 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 l14si6221582edq.543.2020.11.08.18.19.54; Sun, 08 Nov 2020 18:20:16 -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 S1729460AbgKICRy (ORCPT + 99 others); Sun, 8 Nov 2020 21:17:54 -0500 Received: from mga01.intel.com ([192.55.52.88]:64958 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729445AbgKICRx (ORCPT ); Sun, 8 Nov 2020 21:17:53 -0500 IronPort-SDR: 5uuO/H5jVLH7fDOZziSf65IRpZucRsZgj6Npxu5jJnqijah+S4toVmhLoSXYZNgUcXuUbuHJGm vyMQBZZ2Fvkw== X-IronPort-AV: E=McAfee;i="6000,8403,9799"; a="187684676" X-IronPort-AV: E=Sophos;i="5.77,462,1596524400"; d="scan'208";a="187684676" 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:52 -0800 IronPort-SDR: tACAG5bgdzO4iAd2sttP3a/5Ja2d/QinZb+irtiGB3z/iiuj35A5iLFscsDp/FEXA7A0EXwY8u Te52dqD3iBoA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.77,462,1596524400"; d="scan'208";a="540646284" 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:49 -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 16/17] KVM: x86/pmu: Save guest pebs reset value when a pebs counter is configured Date: Mon, 9 Nov 2020 10:12:53 +0800 Message-Id: <20201109021254.79755-17-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 The guest pebs counter X may be cross mapped to the host counter Y. While the PEBS facility would reload the reset value once a PEBS record is written to guest DS and potentially continue to generate PEBS records before guest read the previous records. KVM will adjust the guest DS pebs reset counter values for exactly mapped host counters but before that, it needs to save the original expected guest reset counter values right after the counter is fully enabled via a trap. We assume that every time the guest PEBS driver enables the counter for large PEBS, it will configure the DS reset counter values as Linux does. Signed-off-by: Like Xu --- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/vmx/pmu_intel.c | 51 +++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index cdc3c6efdd8e..32a677ff1e55 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -418,6 +418,7 @@ struct kvm_pmc { enum pmc_type type; u8 idx; u64 counter; + u64 reset_counter; u64 eventsel; struct perf_event *perf_event; struct kvm_vcpu *vcpu; @@ -458,6 +459,7 @@ struct kvm_pmu { bool counter_cross_mapped; bool need_rewrite_ds_pebs_interrupt_threshold; bool need_rewrite_pebs_records; + bool need_save_reset_counter; /* * The gate to release perf_events not marked in diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index d58d04ee13a5..f5a69addd7a8 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -217,12 +217,14 @@ static void intel_pmu_pebs_setup(struct kvm_pmu *pmu) int bit; pmu->need_rewrite_ds_pebs_interrupt_threshold = false; + pmu->need_save_reset_counter = false; for_each_set_bit(bit, (unsigned long *)&pmu->pebs_enable, X86_PMC_IDX_MAX) { pmc = kvm_x86_ops.pmu_ops->pmc_idx_to_pmc(pmu, bit); if (pmc && pmc_speculative_in_use(pmc)) { pmu->need_rewrite_ds_pebs_interrupt_threshold = true; + pmu->need_save_reset_counter = true; break; } } @@ -619,10 +621,48 @@ static int rewrite_ds_pebs_records(struct kvm_vcpu *vcpu) return ret; } +static int save_ds_pebs_reset_values(struct kvm_vcpu *vcpu) +{ + struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); + struct kvm_pmc *pmc = NULL; + struct debug_store *ds = NULL; + gpa_t gpa; + int srcu_idx, bit, idx, ret; + + ds = kmalloc(sizeof(struct debug_store), GFP_KERNEL); + if (!ds) + return -ENOMEM; + + ret = -EFAULT; + srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); + gpa = kvm_mmu_gva_to_gpa_system(vcpu, pmu->ds_area, NULL); + if (gpa == UNMAPPED_GVA) + goto out; + + if (kvm_read_guest(vcpu->kvm, gpa, ds, sizeof(struct debug_store))) + goto out; + + for_each_set_bit(bit, (unsigned long *)&pmu->pebs_enable, X86_PMC_IDX_MAX) { + pmc = kvm_x86_ops.pmu_ops->pmc_idx_to_pmc(pmu, bit); + + if (pmc) { + idx = (pmc->idx < INTEL_PMC_IDX_FIXED) ? + pmc->idx : (MAX_PEBS_EVENTS + pmc->idx - INTEL_PMC_IDX_FIXED); + pmc->reset_counter = ds->pebs_event_reset[idx]; + } + } + ret = 0; + +out: + srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); + kfree(ds); + return ret; +} + void intel_pmu_handle_event(struct kvm_vcpu *vcpu) { struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); - int ret1, ret2; + int ret1, ret2, ret3; if (pmu->need_rewrite_pebs_records) { pmu->need_rewrite_pebs_records = false; @@ -637,11 +677,16 @@ void intel_pmu_handle_event(struct kvm_vcpu *vcpu) ret2 = rewrite_ds_pebs_interrupt_threshold(vcpu); } + if (pmu->need_save_reset_counter) { + pmu->need_save_reset_counter = false; + ret3 = save_ds_pebs_reset_values(vcpu); + } + out: - if (ret1 == -ENOMEM || ret2 == -ENOMEM) + if (ret1 == -ENOMEM || ret2 == -ENOMEM || ret3 == -ENOMEM) pr_debug_ratelimited("%s: Fail to emulate guest PEBS due to OOM.", __func__); - else if (ret1 == -EFAULT || ret2 == -EFAULT) + else if (ret1 == -EFAULT || ret2 == -EFAULT || ret3 == -EFAULT) pr_debug_ratelimited("%s: Fail to emulate guest PEBS due to GPA fault.", __func__); } -- 2.21.3