Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp758741pxt; Fri, 6 Aug 2021 13:03:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxfZdNjQf0A83jeRJlypmWE8eZ5wdexr5Izcyk9B9ihBpgqNmNaAmQhO6lEj9wgE34I8JWe X-Received: by 2002:a02:90cb:: with SMTP id c11mr11534085jag.53.1628280223221; Fri, 06 Aug 2021 13:03:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628280223; cv=none; d=google.com; s=arc-20160816; b=tcNNp9GCWj2bTFXQQ3CPjyBL7F6e1GESEXAHuS1LRwLYqGRiNaipyLgAYc7y4Gtqij 9b3Gk7LRKFOn19l6ja3djPfMBfvI556b8vRLL7OJvZJCQFiLfPu+NOBQG+BUW4x33n0l CYOHd1JZh+s1KGPyCq6NdM53tMPCJ1vcfJLBDHGv/4Dx9pF0ZQ2eT19LEiy/QuxZVxC6 o7Q4WJg1UGrVfk/cVxoSNMR5rBHAq5CTW+yNbZPZ6kBxsFtg3vc+bj6mgqiC7gAExLbS R7Oiuckr37kthm1tPtqHoVY3wxcKjLy73gVkv9PVVSUYhmdCLSDOYHywDxOOX1M/8JkD XUgQ== 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; bh=4s4BusE+Vt/wJ+G5iLSba+ichTOV7QcWhEjHmBFR2tU=; b=YtVmFIP9HoYkkxDmV096ON0M0cf2x+8IeVi5DzlkErZCalUxJjh1olFTsAbIkwUEqp e6BQvBQ61Yr4CyEGfeA4Sh+gAx4AmoLBKzjUV1rzfuztBBWxUa+MwBWB0kkADw70pkfR vzPziLoOL+2O8RY4VAAAzHYMKP04SUY6xGCn0YpPvxpw8VRHpTw9ewhNPZMqYwGGzJMP E4Gtp+6eDTd5oAIeTC1oxJLyoWVuPfe43DIHrrDCDMAaoOyXwmxkepnzDMIAJztiuTJ0 E1XVkMoY8SYJ1dFfogXYifG5ebGrzMfYhatZC8DI0v8rgWm3/g1APH8MaYYESSjmIi0i mOww== 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 n8si8985353ili.154.2021.08.06.13.03.30; Fri, 06 Aug 2021 13:03:43 -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 S1344214AbhHFNjA (ORCPT + 99 others); Fri, 6 Aug 2021 09:39:00 -0400 Received: from mga14.intel.com ([192.55.52.115]:13922 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344173AbhHFNiu (ORCPT ); Fri, 6 Aug 2021 09:38:50 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10068"; a="214101448" X-IronPort-AV: E=Sophos;i="5.84,300,1620716400"; d="scan'208";a="214101448" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Aug 2021 06:38:34 -0700 X-IronPort-AV: E=Sophos;i="5.84,300,1620716400"; d="scan'208";a="523463363" Received: from vmm_a4_icx.sh.intel.com (HELO localhost.localdomain) ([10.239.53.245]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Aug 2021 06:38:30 -0700 From: Zhu Lingshan To: peterz@infradead.org, pbonzini@redhat.com Cc: bp@alien8.de, seanjc@google.com, vkuznets@redhat.com, wanpengli@tencent.com, jmattson@google.com, joro@8bytes.org, kan.liang@linux.intel.com, ak@linux.intel.com, wei.w.wang@intel.com, eranian@google.com, liuxiangdong5@huawei.com, linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, like.xu.linux@gmail.com, boris.ostrvsky@oracle.com, Like Xu , Zhu Lingshan Subject: [PATCH V10 03/18] perf/x86/intel: Handle guest PEBS overflow PMI for KVM guest Date: Fri, 6 Aug 2021 21:37:47 +0800 Message-Id: <20210806133802.3528-4-lingshan.zhu@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210806133802.3528-1-lingshan.zhu@intel.com> References: <20210806133802.3528-1-lingshan.zhu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Like Xu With PEBS virtualization, the guest PEBS records get delivered to the guest DS, and the host pmi handler uses perf_guest_cbs->is_in_guest() to distinguish whether the PMI comes from the guest code like Intel PT. No matter how many guest PEBS counters are overflowed, only triggering one fake event is enough. The fake event causes the KVM PMI callback to be called, thereby injecting the PEBS overflow PMI into the guest. KVM may inject the PMI with BUFFER_OVF set, even if the guest DS is empty. That should really be harmless. Thus guest PEBS handler would retrieve the correct information from its own PEBS records buffer. Originally-by: Andi Kleen Co-developed-by: Kan Liang Signed-off-by: Kan Liang Signed-off-by: Like Xu Signed-off-by: Zhu Lingshan Acked-by: Peter Zijlstra (Intel) --- arch/x86/events/intel/core.c | 45 ++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index da835f5a37e2..2770eba232c7 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -2783,6 +2783,50 @@ static void intel_pmu_reset(void) } DECLARE_STATIC_CALL(x86_guest_handle_intel_pt_intr, *(perf_guest_cbs->handle_intel_pt_intr)); +DECLARE_STATIC_CALL(x86_guest_state, *(perf_guest_cbs->state)); + +/* + * We may be running with guest PEBS events created by KVM, and the + * PEBS records are logged into the guest's DS and invisible to host. + * + * In the case of guest PEBS overflow, we only trigger a fake event + * to emulate the PEBS overflow PMI for guest PBES counters in KVM. + * The guest will then vm-entry and check the guest DS area to read + * the guest PEBS records. + * + * The contents and other behavior of the guest event do not matter. + */ +static void x86_pmu_handle_guest_pebs(struct pt_regs *regs, + struct perf_sample_data *data) +{ + struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); + u64 guest_pebs_idxs = cpuc->pebs_enabled & ~cpuc->intel_ctrl_host_mask; + struct perf_event *event = NULL; + unsigned int guest = 0; + int bit; + + guest = static_call(x86_guest_state)(); + if (!(guest & PERF_GUEST_ACTIVE)) + return; + + if (!x86_pmu.pebs_vmx || !x86_pmu.pebs_active || + !guest_pebs_idxs) + return; + + for_each_set_bit(bit, (unsigned long *)&guest_pebs_idxs, + INTEL_PMC_IDX_FIXED + x86_pmu.num_counters_fixed) { + event = cpuc->events[bit]; + if (!event->attr.precise_ip) + continue; + + perf_sample_data_init(data, 0, event->hw.last_period); + if (perf_event_overflow(event, data, regs)) + x86_pmu_stop(event, 0); + + /* Inject one fake event is enough. */ + break; + } +} static int handle_pmi_common(struct pt_regs *regs, u64 status) { @@ -2835,6 +2879,7 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status) u64 pebs_enabled = cpuc->pebs_enabled; handled++; + x86_pmu_handle_guest_pebs(regs, &data); x86_pmu.drain_pebs(regs, &data); status &= intel_ctrl | GLOBAL_STATUS_TRACE_TOPAPMI; -- 2.27.0