Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1988401pxb; Mon, 23 Aug 2021 09:18:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx0Km+qnkB88r1l7eSeSTOVZs368JRkdh3PK6Tr2YMMoBgXmyEdMkE767rNwr4WC/aiOszc X-Received: by 2002:a02:908a:: with SMTP id x10mr29476452jaf.30.1629735482573; Mon, 23 Aug 2021 09:18:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629735482; cv=none; d=google.com; s=arc-20160816; b=HpGEKNGjG/IwTEzUkS2+PX7rzRJRWD63TVpzUP7x9ScVMahgwY43A8J0+7cM6R3a7s wr51aD/j6YRDV9wIZSnnjCw+wzAEdWiQsXrv/hNbGNQWK/Il+UwaiL1u86rXwC6ea0ud DTcHZtQ68nnmyzu2QCPLbLpow0CytAcPANbRieNijADNv2zJDiuVGdA0tjLp5dtNk8/U IYoMWW+NoE7BLMg/T1dMxBR/hJ5/TQXoGie4K89ivYnhEvoiKzUv1le5MEePEAuA7HlW Aubr9EdynpaaIILYaou74L8irVqA2lSchrIC555VHJIsTJf94lPaKOk0RJHgykngVhN3 zrBQ== 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=ryhD7Cthvbr45hQrpnqITGrpB6V3gOLxyGixZ7GwrfQ=; b=pzQIyeVWIZHmTBE3mPqUhpnx7oLyDEKe/GATTQL0IlMJY5BpkPvVgqGnvnH7rBL2/E cIW9SfuWtm/vkFHc4v8WhY60pZKIvDuuSa+Uz+l2BvkCqQ+ZgoT/F67UddZ8dFr1+Phx EbiWWlrx71ZhXhr8oyEwZAcgwtFZXD8SSsTJEmzFTWBVUqBx21n/s7B0y1nB57q0KcO/ 0//MZnHT3ryr2vwDUdTUbAFpJV6mEE7q73l3WeMG5YS88F1CfhTtkC7pxnSySVnP/K6e hmdgg08+PcCab7RzoOKjNCq+gk3/cLoL0L7zkiXOG+zOA08Y7L7GJQTQ/dH4yVzA7nRT g0Bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="Y/VkN+1g"; 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 i4si15315840ilv.27.2021.08.23.09.17.50; Mon, 23 Aug 2021 09:18:02 -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; dkim=pass header.i=@google.com header.s=20161025 header.b="Y/VkN+1g"; 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 S229905AbhHWQRm (ORCPT + 99 others); Mon, 23 Aug 2021 12:17:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229868AbhHWQRl (ORCPT ); Mon, 23 Aug 2021 12:17:41 -0400 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 420F6C061757 for ; Mon, 23 Aug 2021 09:16:58 -0700 (PDT) Received: by mail-pl1-x62b.google.com with SMTP id c4so10441324plh.7 for ; Mon, 23 Aug 2021 09:16:58 -0700 (PDT) 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=ryhD7Cthvbr45hQrpnqITGrpB6V3gOLxyGixZ7GwrfQ=; b=Y/VkN+1gxsoSe3hsQkEe6PMVj7mm2IHbg36U1kjpignPshE6jeCwDWaJ2eGVOcy7fo g7HYe8UWQTY8A0vodGIdznQPx5HKdAr+AG1tkba7z+zr6NlXwNJ6puA6ZimMj4O5i72w p55fh+Dh7cRtGdX/l8acD/lrMu4AYKWyTqbG1IRPB2uGF4IFb5KA+coz0LB2DMNzGiq4 YQc6WOXOeRmaSygsObdne7yJZmDvXPV7ZZnNUgDcxlvHF4zwoHNUE4bfnQunQQNH3iEM XXBxAwCro3B+wx1+YOs4XTXhOJQQEc4FO1kckqijf+W2SrFQGXSbepi/Zo5z6lM8Ak4I GicQ== 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=ryhD7Cthvbr45hQrpnqITGrpB6V3gOLxyGixZ7GwrfQ=; b=O3F138Nc/Hl1OGVgjP3X/s4YWY/ftedozFzlgdmaRslKUelUCnxhskoj2HN0LmC0PI nk7Zj8DWUF5vr7KGangiWpNTpfMttMuq07vkSRpYYraS7G7Yk2tyXnU1TaZmDIkvN3Hy CIOWoY442HgBKV7/3QXSg7dItarUW571ctlUL8ZJYbpP6JvLT5okH+BasA8CMVEFVK6T lTk70vH77qWHg0WknGBk4h+tOpdWcjUXwpuBHfINHpyTtgtHGrHDDnsRoGoVV84BTefQ LlbGRLmbBz5jwzkGkr6K3gYm5sZdar4q/oYMzbZDFSXMLhWBtfWiZygbnNXE64hEniK5 SMHA== X-Gm-Message-State: AOAM5318n6oo4Jc0p0kM7iioUEsoeXEZCsQztxwY7foNr6Y1Ch+V4Qkw /rzTZJTeXnCT5JW76ovC2sSIVE9SDGWDtQ== X-Received: by 2002:a17:90a:12ca:: with SMTP id b10mr7399333pjg.180.1629735417434; Mon, 23 Aug 2021 09:16:57 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id b14sm16201529pfo.76.2021.08.23.09.16.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Aug 2021 09:16:56 -0700 (PDT) Date: Mon, 23 Aug 2021 16:16:51 +0000 From: Sean Christopherson To: Alexander Shishkin Cc: Paolo Bonzini , Peter Zijlstra , Arnaldo Carvalho de Melo , Ingo Molnar , linux-kernel@vger.kernel.org, Jiri Olsa , kvm@vger.kernel.org, Artem Kashkanov Subject: Re: [PATCH] kvm/x86: Fix PT "host mode" Message-ID: References: <20210823134239.45402-1-alexander.shishkin@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210823134239.45402-1-alexander.shishkin@linux.intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Aug 23, 2021, Alexander Shishkin wrote: > Regardless of the "pt_mode", the kvm driver installs its interrupt handler > for Intel PT, which always overrides the native handler, causing data loss > inside kvm guests, while we're expecting to trace them. > > Fix this by only installing kvm's perf_guest_cbs if pt_mode is set to > guest tracing. Uh, regardless of the correctness of such a change (spoiler alert), making an enormous leap from "one thing is wrong" to "nuke it all!" needs way more justfication/explanation. Or more realistically, such a leap should be a good indication that the proposed change is not correct. > Signed-off-by: Alexander Shishkin > Fixes: ff9d07a0e7ce7 ("KVM: Implement perf callbacks for guest sampling") This should be another clue that the fix isn't correct. That patch is from 2010, Intel PT was announced in 2013 and merged in 2019. > Reported-by: Artem Kashkanov > Tested-by: Artem Kashkanov > --- > arch/x86/include/asm/kvm_host.h | 1 + > arch/x86/kvm/vmx/vmx.c | 6 ++++++ > arch/x86/kvm/x86.c | 10 ++++++++-- > 3 files changed, 15 insertions(+), 2 deletions(-) > ... > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index 9b6bca616929..3ba0001e7388 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -268,6 +268,8 @@ static struct kmem_cache *x86_fpu_cache; > > static struct kmem_cache *x86_emulator_cache; > > +static int __read_mostly intel_pt_enabled; > + > /* > * When called, it means the previous get/set msr reached an invalid msr. > * Return true if we want to ignore/silent this failed msr access. > @@ -8194,7 +8196,10 @@ int kvm_arch_init(void *opaque) > > kvm_timer_init(); > > - perf_register_guest_info_callbacks(&kvm_guest_cbs); > + if (ops->intel_pt_enabled && ops->intel_pt_enabled()) r This is not remotely correct. vmx.c's "pt_mode", which is queried via this path, is modified by hardware_setup(), a.k.a. kvm_x86_ops.hardware_setup(), which runs _after_ this code. And as alluded to above, these are generic perf callbacks, installing them if and only if Intel PT is enabled in a specific mode completely breaks "regular" perf. I'll post a small series, there's a bit of code massage needed to fix this properly. The PMI handler can also be optimized to avoid a retpoline when PT is not exposed to the guest. > + perf_register_guest_info_callbacks(&kvm_guest_cbs); > + intel_pt_enabled = 1; > + } > > if (boot_cpu_has(X86_FEATURE_XSAVE)) { > host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK); > @@ -8229,7 +8234,8 @@ void kvm_arch_exit(void) > clear_hv_tscchange_cb(); > #endif > kvm_lapic_exit(); > - perf_unregister_guest_info_callbacks(&kvm_guest_cbs); > + if (intel_pt_enabled) > + perf_unregister_guest_info_callbacks(&kvm_guest_cbs); > > if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) > cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block, > -- > 2.32.0 >