Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1874345pxb; Mon, 23 Aug 2021 06:45:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy1WUxvQp09hpoXQyqoif0YfX1CU7WJ8+PELbFthnK2UUpZYtaYJCAfRSpcrFB7kYTjbiWD X-Received: by 2002:a05:6402:4406:: with SMTP id y6mr36464725eda.242.1629726305807; Mon, 23 Aug 2021 06:45:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629726305; cv=none; d=google.com; s=arc-20160816; b=VoMeKGR9obeclm+ihdRMPnPLwRx99a6E82GGGhablJ/o3J2JFdUT5EUUTKviZQgJtI 63jZG9xkCEA40GV41JyUnOKOQVn+N7Ab8e5iDu+jgQ/drsKgpVTBBVofhKsj7POrwWVs GwKqVGh9R0neqEDLcAWsO2caPIguV/Fb5od9kAOr+5GV9BGwzSs4Pq0PkLUO27nyfLIJ P2zHdtl/aLu2K0GpUixThMDk2ZylqHovceEDg3PG7pXx4PAVHPdikzL2DCOTYUm0Pzlm Dj79PwmuA9zvjQ3cGZo7RiYQvs1FpEifgFSoDmAJArDri6u//g53SoW9N9dCXWgtLiCr o1Jw== 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 :message-id:date:subject:cc:to:from; bh=QMr/1s5rrmsetMAidPiyJvdzaU4pFueQ2rFy5/DZuQI=; b=OdqyUHdKFQeQhguNpsr0yz7J3yhQNRf/veafhP7A/UWB4nYS1ktHv25ueoLnkmaKvz L7vd+70W9+BmUtE0zgHX/K2bI9ehwfsc+pwcR34BfaXwGL4swwJAG7YJLhThA+t7AT23 QGJOg9GilqrCcPH5S8+aLIAZd2eQsVIOR8L0ptSAs4RM3hCI/kQo7bIJJ+mKMNrQhril PwNza+KlGc01394bktzWDCg2RPSyijfQOIq/V64ww86frif7Zer3zaSSb1lmybB3x5jf 3FW0FYLMoSkRyOqO6nI6Q1Hsr18+zeL37RBAgoSsJ9ER1V9/5WMkM78iINDNoMoo1PE0 gqOg== 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 fl2si13389477ejc.34.2021.08.23.06.44.40; Mon, 23 Aug 2021 06:45:05 -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 S229678AbhHWNne (ORCPT + 99 others); Mon, 23 Aug 2021 09:43:34 -0400 Received: from mga17.intel.com ([192.55.52.151]:53980 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229477AbhHWNne (ORCPT ); Mon, 23 Aug 2021 09:43:34 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10084"; a="197353299" X-IronPort-AV: E=Sophos;i="5.84,344,1620716400"; d="scan'208";a="197353299" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Aug 2021 06:42:48 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.84,344,1620716400"; d="scan'208";a="492703467" Received: from black.fi.intel.com (HELO black.fi.intel.com.) ([10.237.72.28]) by fmsmga008.fm.intel.com with ESMTP; 23 Aug 2021 06:42:45 -0700 From: Alexander Shishkin To: Paolo Bonzini Cc: Peter Zijlstra , Arnaldo Carvalho de Melo , Ingo Molnar , linux-kernel@vger.kernel.org, Jiri Olsa , kvm@vger.kernel.org, Alexander Shishkin , Artem Kashkanov Subject: [PATCH] kvm/x86: Fix PT "host mode" Date: Mon, 23 Aug 2021 16:42:39 +0300 Message-Id: <20210823134239.45402-1-alexander.shishkin@linux.intel.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Signed-off-by: Alexander Shishkin Fixes: ff9d07a0e7ce7 ("KVM: Implement perf callbacks for guest sampling") 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/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 55efbacfc244..84a1ed067f35 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1408,6 +1408,7 @@ struct kvm_x86_init_ops { int (*disabled_by_bios)(void); int (*check_processor_compatibility)(void); int (*hardware_setup)(void); + int (*intel_pt_enabled)(void); struct kvm_x86_ops *runtime_ops; }; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 4bceb5ca3a89..0c239aa3532a 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7943,11 +7943,17 @@ static __init int hardware_setup(void) return r; } +static int vmx_intel_pt_enabled(void) +{ + return vmx_pt_mode_is_host_guest(); +} + static struct kvm_x86_init_ops vmx_init_ops __initdata = { .cpu_has_kvm_support = cpu_has_kvm_support, .disabled_by_bios = vmx_disabled_by_bios, .check_processor_compatibility = vmx_check_processor_compat, .hardware_setup = hardware_setup, + .intel_pt_enabled = vmx_intel_pt_enabled, .runtime_ops = &vmx_x86_ops, }; 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()) { + 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