Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp13818772pxu; Mon, 4 Jan 2021 05:27:31 -0800 (PST) X-Google-Smtp-Source: ABdhPJwyCaupqWOhmAsRahHg/pEFwbB9gD42CaQUc74T5Y5MgTS8ijEppl490MdluDkfr4Iov4rc X-Received: by 2002:a50:9f4e:: with SMTP id b72mr71340892edf.200.1609766851783; Mon, 04 Jan 2021 05:27:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609766851; cv=none; d=google.com; s=arc-20160816; b=xkHKXis/rlzNmtaRO5e7kmr2wo6+6ZZzJDosA2vMsZ0qpGCV5NncOum8Kivfmm4+XX Gbn2tK5O412zUdiGZnPz7o0iUp0+StBVqDkQHeL972KArxIqdaP0WoV5eTxiNectHYNn yh/yFp/DT31BEc1ewiyxfyNWZapKkYx6HgIqXT5/HNM2gUit5IDmWRQ/Zv05ooAIF+i5 yfMPL1NsMzUWhxIaLULaULvcMnZ//Q8Y+dUj2uQq6Y629fk6O2JqfaPTOpYl2HkyjJWK /+6Qra9tmHMSc6QflDwVx14iz0TQyLo5drOBshYSYXMG5tjP4QgRvsPHRpa4sYa2TFh3 F6UQ== 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=GqB2xEsSWr5G4wK4VBQ2+NhrTR2YRilxxtGZ1bh7izo=; b=Qov/+Rdb9leeJz+nXg+amIzPutHX1oeXlBfCWZm0gTPbT17iV3rOqlY6WBrcCA4iLt mEsuV6e7tbBMIDGdQIG//bAgKyMF4ErgGqSy75xGrCI6vCvolMQrqj3rrrG00kdtYxiN 1zgolVaE+0c5iEVBoKf+rzawoOuBmb0MJdIRW9Li3e94oiXGSIIqYBoXNF8SIolqv9SY 5RR0/nOaOpV0t8Kax7PqRrGKjdCc6yGmYUmf8nf05KiZ2CNL88BIG6tKlBI8CYmP5N9R 3vsNC7ugbPYJXFpjMM3U3NPwdw2dmBKcvAI0r4vY1l/t6SXCPubnGfjuJ8qS33fxBBHv q8/w== 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 f7si27976421ejx.294.2021.01.04.05.27.08; Mon, 04 Jan 2021 05:27:31 -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 S1727204AbhADNZM (ORCPT + 99 others); Mon, 4 Jan 2021 08:25:12 -0500 Received: from mga07.intel.com ([134.134.136.100]:23250 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726930AbhADNZM (ORCPT ); Mon, 4 Jan 2021 08:25:12 -0500 IronPort-SDR: l2PhqGUoTsEdM2h5oiem1oxX9CgVfyYtfwLCJkfcTjPi1FbCU2RXYFoq205exb97+cog96kVza crfikpR9o8eA== X-IronPort-AV: E=McAfee;i="6000,8403,9853"; a="241034326" X-IronPort-AV: E=Sophos;i="5.78,474,1599548400"; d="scan'208";a="241034326" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Jan 2021 05:22:15 -0800 IronPort-SDR: MY/GFEr/CAORwT7Z16np5UK0CQjaAlb2tVw3/J0YZ6ueNnbh4kFlnwWQeRHjZPeoJbvkHePRkW LalRcSPsdKag== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.78,474,1599548400"; d="scan'208";a="461944563" Received: from clx-ap-likexu.sh.intel.com ([10.239.48.108]) by fmsmga001.fm.intel.com with ESMTP; 04 Jan 2021 05:22:12 -0800 From: Like Xu To: Peter Zijlstra , Paolo Bonzini , eranian@google.com, kvm@vger.kernel.org Cc: Ingo Molnar , Sean Christopherson , Thomas Gleixner , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Andi Kleen , Kan Liang , wei.w.wang@intel.com, luwei.kang@intel.com, linux-kernel@vger.kernel.org Subject: [PATCH v3 05/17] KVM: x86/pmu: Reprogram guest PEBS event to emulate guest PEBS counter Date: Mon, 4 Jan 2021 21:15:30 +0800 Message-Id: <20210104131542.495413-6-like.xu@linux.intel.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210104131542.495413-1-like.xu@linux.intel.com> References: <20210104131542.495413-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 When a guest counter is configured as a PEBS counter through IA32_PEBS_ENABLE, a guest PEBS event will be reprogrammed by configuring a non-zero precision level in the perf_event_attr. The guest PEBS overflow PMI bit would be set in the guest GLOBAL_STATUS MSR when PEBS facility generates a PEBS overflow PMI based on guest IA32_DS_AREA MSR. The attr.precise_ip would be adjusted to a special precision level when the new PEBS-PDIR feature is supported later which would affect the host counters scheduling. The guest PEBS event would not be reused for non-PEBS guest event even with the same guest counter index. Originally-by: Andi Kleen Co-developed-by: Kan Liang Signed-off-by: Kan Liang Signed-off-by: Like Xu --- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/pmu.c | 29 +++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 94c8bfee4a82..09dacda33fb8 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -449,6 +449,8 @@ struct kvm_pmu { DECLARE_BITMAP(all_valid_pmc_idx, X86_PMC_IDX_MAX); DECLARE_BITMAP(pmc_in_use, X86_PMC_IDX_MAX); + u64 pebs_enable; + /* * The gate to release perf_events not marked in * pmc_in_use only once in a vcpu time slice. diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index 67741d2a0308..2e81c50323e2 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -76,7 +76,12 @@ static void kvm_perf_overflow_intr(struct perf_event *perf_event, struct kvm_pmu *pmu = pmc_to_pmu(pmc); if (!test_and_set_bit(pmc->idx, pmu->reprogram_pmi)) { - __set_bit(pmc->idx, (unsigned long *)&pmu->global_status); + if (perf_event->attr.precise_ip) { + /* Indicate PEBS overflow PMI to guest. */ + __set_bit(GLOBAL_STATUS_BUFFER_OVF_BIT, + (unsigned long *)&pmu->global_status); + } else + __set_bit(pmc->idx, (unsigned long *)&pmu->global_status); kvm_make_request(KVM_REQ_PMU, pmc->vcpu); /* @@ -99,6 +104,7 @@ static void pmc_reprogram_counter(struct kvm_pmc *pmc, u32 type, bool exclude_kernel, bool intr, bool in_tx, bool in_tx_cp) { + struct kvm_pmu *pmu = vcpu_to_pmu(pmc->vcpu); struct perf_event *event; struct perf_event_attr attr = { .type = type, @@ -110,6 +116,7 @@ static void pmc_reprogram_counter(struct kvm_pmc *pmc, u32 type, .exclude_kernel = exclude_kernel, .config = config, }; + bool pebs = test_bit(pmc->idx, (unsigned long *)&pmu->pebs_enable); attr.sample_period = get_sample_period(pmc, pmc->counter); @@ -124,9 +131,23 @@ static void pmc_reprogram_counter(struct kvm_pmc *pmc, u32 type, attr.sample_period = 0; attr.config |= HSW_IN_TX_CHECKPOINTED; } + if (pebs) { + /* + * The non-zero precision level of guest event makes the ordinary + * guest event becomes a guest PEBS event and triggers the host + * PEBS PMI handler to determine whether the PEBS overflow PMI + * comes from the host counters or the guest. + * + * For most PEBS hardware events, the difference in the software + * precision levels of guest and host PEBS events will not affect + * the accuracy of the PEBS profiling result, because the "event IP" + * in the PEBS record is calibrated on the guest side. + */ + attr.precise_ip = 1; + } event = perf_event_create_kernel_counter(&attr, -1, current, - intr ? kvm_perf_overflow_intr : + (intr || pebs) ? kvm_perf_overflow_intr : kvm_perf_overflow, pmc); if (IS_ERR(event)) { pr_debug_ratelimited("kvm_pmu: event creation failed %ld for pmc->idx = %d\n", @@ -161,6 +182,10 @@ static bool pmc_resume_counter(struct kvm_pmc *pmc) get_sample_period(pmc, pmc->counter))) return false; + if (!test_bit(pmc->idx, (unsigned long *)&pmc_to_pmu(pmc)->pebs_enable) && + pmc->perf_event->attr.precise_ip) + return false; + /* reuse perf_event to serve as pmc_reprogram_counter() does*/ perf_event_enable(pmc->perf_event); -- 2.29.2