Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp500677pxb; Thu, 14 Jan 2021 10:58:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJxsUaz9AhDxzmviT1aOo8QQl/eRxdljKd0xOfZ2DOzpwt90MMNkTlG6Wfv7eF0ch7wmln2I X-Received: by 2002:a50:b5c5:: with SMTP id a63mr6836850ede.227.1610650726014; Thu, 14 Jan 2021 10:58:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610650726; cv=none; d=google.com; s=arc-20160816; b=ho+g0nDznOeoCnaVYJrYWBpFW59Evo4uQ15ZJA2ZyYkGC8fmoaXmqtrj7UiGbP1Vw4 FnN0hd74vvRTu9yIGN12T0moDajiwbxb343soGBOGZFwPh/n2n3RzMLQa4rlgdf4jAff ppWd4FM7o94/DmgDI/YRtQs938QNj7IL1zZGRQ+h4hmVUe5ZX4mCQS91+7Vv2wwno/lO DWpjlKTdo9d/sJMoP5Ik+VfBMUWv9Yc8bpGdkBi5kmlS09mv9R/wMEo4+AJPkxQzXgzW 3tWSzBktWoO7uBFnmDDkWHQSINMIT2P1MSFGGuiVP5UdwSQa0Jhlscxp/+dl2rLmizbc EW6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=BZd5vz4hy8Xv+MkxlJJ7ylWIWD/7dL9BLPaHzk7fwBc=; b=yjniGld4+TSt+JXxu90ra4M37cERcMMJwX6erYIko+vpAgr+9U0KMKZpseRoTRVhIG n65WlvnT3kkzE3cLDogEi3zW60rnm5+fsSKJ6jt8Ya7vcQ2ib807Vj7vEkMr4KO7ekk9 IX54oneUisCNYB/W/xDohuDgutLV0TnDIRrwsBrB7TYHIIHwSO8C0flH7w/FPOWmT9tF qYTnsHb9I/D4q4YcxDc/zHphYS9F4Q80asAm5xgsJPdMXVWVx+VliEG7Pe6UkxbhJdwy 55FN5T6LhXSNS5A7+U4qhdqLRib/+KGgPPnznyYz3H8ZP+xAIWrFebpr/JhXTVemDm5k Q7gA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=C2sF1sIA; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ia15si2205798ejc.443.2021.01.14.10.58.22; Thu, 14 Jan 2021 10:58:46 -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; dkim=pass header.i=@google.com header.s=20161025 header.b=C2sF1sIA; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729370AbhANS4Q (ORCPT + 99 others); Thu, 14 Jan 2021 13:56:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37952 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727134AbhANS4O (ORCPT ); Thu, 14 Jan 2021 13:56:14 -0500 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B57E5C061575 for ; Thu, 14 Jan 2021 10:55:34 -0800 (PST) Received: by mail-pj1-x102b.google.com with SMTP id p15so3515517pjv.3 for ; Thu, 14 Jan 2021 10:55:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=BZd5vz4hy8Xv+MkxlJJ7ylWIWD/7dL9BLPaHzk7fwBc=; b=C2sF1sIAUAJVx9PwWWwp02Cw3l3B0u/TrqSSltHVQ9OjN7DXJYO7ZtFBcEH1AFHB1j v/waeY6VTZlJJ9p/7QbkMFLAfNuHb/mwxy66ZX4QhI+QVjc5Jc/aMwQ1MfVYDw8kTuMz 8I3M8NnbcqDq27BkDm08ayDSChTXb4hBC9Wf6Yz5GqQj34UNP+ygcPvEbtwJO8Ok3kRX Rc+aInAhPqOblsd4T+yD3tuM/lecpBfOM8kU921D8IDxh94HzIl87AioKSrP8Go/iFpe CjdyFFzBGot82Nj5F98G76i46WLJsLBp1cxe/aACXumSfygwbsEmkOx+RE38m//2iAVP wDfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=BZd5vz4hy8Xv+MkxlJJ7ylWIWD/7dL9BLPaHzk7fwBc=; b=Jnw/rER1h7FWsEkFDWHBXDbwGejjYNogLJua8mN/0mU6WeATiEffILRuRTY7PyKb3t oF+UPvmh36nytApVh2yGXrr0NqevpU1dlvIPmqWSZct0JqTSoSL/PVj4FfYmaH71M6wb xOXstFfyLzt+TP2j1FerZcRCr+5cS6Fcdg+EkrveprBgqblqkFVRXRuwQCnXmm5w769i +qw3PMgpSGVmFqKvnwWOuWnBfKVn1E6x6fpmcmA4Vejm6xFCyccXsPIrAlbqlpjfyq7b pQYIfibETov7zwp/ETdKPpAPnmQwVoeTX5MLsiXyKzoDrc7LxC2aee8Cc/h4Ygh3M0xN uyKw== X-Gm-Message-State: AOAM531hs8ZLPiq3HOVmIlUp7n6NjWnu97aGLGDyhD3fPQxpCj0P8Jkb JBaA6btqCqtG+VLn37iEX9MMdA== X-Received: by 2002:a17:902:bf06:b029:dc:1f:ac61 with SMTP id bi6-20020a170902bf06b02900dc001fac61mr8924128plb.16.1610650534057; Thu, 14 Jan 2021 10:55:34 -0800 (PST) Received: from google.com ([2620:15c:f:10:1ea0:b8ff:fe73:50f5]) by smtp.gmail.com with ESMTPSA id p17sm5782386pfn.52.2021.01.14.10.55.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Jan 2021 10:55:33 -0800 (PST) Date: Thu, 14 Jan 2021 10:55:26 -0800 From: Sean Christopherson To: Like Xu Cc: Peter Zijlstra , Paolo Bonzini , eranian@google.com, kvm@vger.kernel.org, Ingo Molnar , 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: Re: [PATCH v3 04/17] perf: x86/ds: Handle guest PEBS overflow PMI and inject it to guest Message-ID: References: <20210104131542.495413-1-like.xu@linux.intel.com> <20210104131542.495413-5-like.xu@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210104131542.495413-5-like.xu@linux.intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jan 04, 2021, Like Xu wrote: > --- > arch/x86/events/intel/ds.c | 62 ++++++++++++++++++++++++++++++++++++++ > 1 file changed, 62 insertions(+) > > diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c > index b47cc4226934..c499bdb58373 100644 > --- a/arch/x86/events/intel/ds.c > +++ b/arch/x86/events/intel/ds.c > @@ -1721,6 +1721,65 @@ intel_pmu_save_and_restart_reload(struct perf_event *event, int count) > return 0; > } > > +/* > + * 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 guest PEBS overflow PMI may be dropped when both the guest and > + * the host use PEBS. Therefore, KVM will not enable guest PEBS once > + * the host PEBS is enabled since it may bring a confused unknown NMI. > + * > + * The contents and other behavior of the guest event do not matter. > + */ > +static int intel_pmu_handle_guest_pebs(struct cpu_hw_events *cpuc, > + struct pt_regs *iregs, > + struct debug_store *ds) > +{ > + struct perf_sample_data data; > + struct perf_event *event = NULL; > + u64 guest_pebs_idxs = cpuc->pebs_enabled & ~cpuc->intel_ctrl_host_mask; > + int bit; > + > + /* > + * Ideally, we should check guest DS to understand if it's > + * a guest PEBS overflow PMI from guest PEBS counters. > + * However, it brings high overhead to retrieve guest DS in host. > + * So we check host DS instead for performance. > + * > + * If PEBS interrupt threshold on host is not exceeded in a NMI, there > + * must be a PEBS overflow PMI generated from the guest PEBS counters. > + * There is no ambiguity since the reported event in the PMI is guest > + * only. It gets handled correctly on a case by case base for each event. > + * > + * Note: KVM disables the co-existence of guest PEBS and host PEBS. By "KVM", do you mean KVM's loading of the MSRs provided by intel_guest_get_msrs()? Because the PMU should really be the entity that controls guest vs. host. KVM should just be a dumb pipe that handles the mechanics of how values are context switch. For example, commit 7099e2e1f4d9 ("KVM: VMX: disable PEBS before a guest entry"), where KVM does an explicit WRMSR(PEBS_ENABLE) to (attempt to) force PEBS quiescence, is flawed in that the PMU can re-enable PEBS after the WRMSR if a PMI arrives between the WRMSR and VM-Enter (because VMX can't block NMIs). The PMU really needs to be involved in the WRMSR workaround. > + */ > + if (!guest_pebs_idxs || !in_nmi() || Are PEBS updates guaranteed to be isolated in both directions on relevant hardware? By that I mean, will host updates be fully processed before VM-Enter compeletes, and guest updates before VM-Exit completes? If that's the case, then this path could be optimized to change the KVM invocation of the NMI handler so that the "is this a guest PEBS PMI" check is done if and only if the PMI originated from with the guest. > + ds->pebs_index >= ds->pebs_interrupt_threshold) > + return 0; > + > + 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, iregs)) > + x86_pmu_stop(event, 0); > + > + /* Inject one fake event is enough. */ > + return 1; > + } > + > + return 0; > +}