Received: by 2002:a05:7412:3290:b0:fa:6e18:a558 with SMTP id ev16csp641474rdb; Fri, 26 Jan 2024 06:35:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IGx4OkPqcbXwhKqjw/Zd2yad3jCtL6Hm/XKfgpJzCq4KkBZHw3imnu/J54NfgqhwmDsgSCI X-Received: by 2002:a4a:d6c8:0:b0:599:f3da:4715 with SMTP id j8-20020a4ad6c8000000b00599f3da4715mr1195713oot.0.1706279745784; Fri, 26 Jan 2024 06:35:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706279745; cv=pass; d=google.com; s=arc-20160816; b=fs9exVXD0FhdRQvyvUw1Z2TeUULHkRH4+3uGhmIeckDoRgt/rRkn1o1oBLmHwo7+z9 RTrehU3v+n/1iWzWDB1AKNzmw6EnXYtmjESF+gpMEyOEKOoIChhWeK+mlj+5N10uX36I m4akwujqS5T6C1y/ehlWiffqGEfxecBZmuDkwU4Ir2PLrKsYEYga1p5ijIuwLZv+Mef/ Vr44hYj2qK7C4RsaNvdcxinqbHGxUO95Y4Zhl6e6JFJ9S+IB8/AayBAT7NTxfA+tDPEm xOPV61cu7fRbURqujs7BOBR0wF58J1qsdAEZk37/giLQmNCLof+jIwpwZMIciFqxnnly PM4A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=CMRK6M/hpDw6mQtzXxRz9JMBOVLO6UVLdW6CfYBb5cA=; fh=ILHfJ0uLc1EruXziwgJ5p79BI7cXVVRZxn5LqOC7zKA=; b=qlrB2wOpPXXzEG2HM5HRWFPVEwWN+sT9UwbgmA9AzjEsmY7AViDIH+TcZU1+IJKrNZ RtDrFNiOoWnFzWuf6Zsh/l6k/bTJaoCERNSNJn0iM9ez/FxMwmBr5Sd/fsIeTFdsUj4m 8hHjDHJNEjyH+AFguev+1fgNNUJ3ci6RoIOnow687QBBIzd+KogqXSpebcqIkFmvm9aW OHk1XN2/uSlox+QMTFh/jWU4ubjgmysRSWFTRHxxV1Mt2U+SFGN+OFtlFFG9cmCk1Q5h yhXxUIMiDPF32Mb2Z51N9uyUqqjlP92SFWiHC5FdbqAyMPDdXnft7sEK6ud5WNiiZJm2 jIAg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="LPcO/LdJ"; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-39823-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-39823-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id kd18-20020a056214401200b0068193f922e8si1384278qvb.325.2024.01.26.06.35.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 06:35:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-39823-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="LPcO/LdJ"; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-39823-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-39823-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 018611C244BC for ; Fri, 26 Jan 2024 09:35:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0E9E913E221; Fri, 26 Jan 2024 08:56:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="LPcO/LdJ" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 99DB525633; Fri, 26 Jan 2024 08:56:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706259414; cv=none; b=lDIVPjWnaHxAWSuBC8zqgACZwPLdIeGkDzbg4mLgc/ofpcAEjs1crOVKC5wSi+2YtaCaKg7RFMjjIR/gM/A4aX8GOqXq+VRZubhXBGDHNpqflGLkeyHryVQUmcxrl2KacbxRjrTzDgWWgz4ARZsJN9T1xo1xELbTjaDOyPibZlU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706259414; c=relaxed/simple; bh=YPU3LPyNSnF5QA2pVbK/JlE2xn3RFRw6LyKXbEinvVg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=L0BFcdQuqPPtrsSoZIQ02ySbN3jP2TslOPXKOT9vEqKm3JmZUSFdA9q1tXFx9dLs61NCNzbIJpb7zCzafG2tLEE9sH8rlPN9hQYd1BdMOqaohDtTfpXcECCcLbNie67I1n/3RCgwGboTd9ZxeBSgGaStpzXp06gu2f6vkWiCoeg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=LPcO/LdJ; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706259413; x=1737795413; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=YPU3LPyNSnF5QA2pVbK/JlE2xn3RFRw6LyKXbEinvVg=; b=LPcO/LdJGpThcdAzxByCcXsZ62krkvuG/YssfgoDSwSuOg2Q4Lk6ZSjf GQ53LgsLGYrQ9fLbVSTBpVWaJUwlZlexLDqNLwLUZArpcsIGgtvAy7DHq sVSczI8JPVDm78kZxKJqhuXDuUEeurlOusfct5sAfr6t3QhBu/SyKU88U lCh6AzFH1WB5c6Xj6y2IeZZVBAwAHwY7hUAfD4o3zAz2aji7+f0Zpw5jo KtNunFJEKUDz52OTRct14OnqOLRs5HVPSzkUMHshKZKvWXRSm0T6WRy8q bczNMp5WYGAG8sbjXAjBDtSwN2Bl+WaBuGw+50SzGqZJCQrZkivIAeL8q Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10964"; a="9792346" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="9792346" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Jan 2024 00:56:52 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10964"; a="930310057" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="930310057" Received: from yanli3-mobl.ccr.corp.intel.com (HELO xiongzha-desk1.ccr.corp.intel.com) ([10.254.213.178]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Jan 2024 00:56:47 -0800 From: Xiong Zhang To: seanjc@google.com, pbonzini@redhat.com, peterz@infradead.org, mizhang@google.com, kan.liang@intel.com, zhenyuw@linux.intel.com, dapeng1.mi@linux.intel.com, jmattson@google.com Cc: kvm@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, zhiyuan.lv@intel.com, eranian@google.com, irogers@google.com, samantha.alt@intel.com, like.xu.linux@gmail.com, chao.gao@intel.com, xiong.y.zhang@linux.intel.com, Xiong Zhang Subject: [RFC PATCH 15/41] KVM: x86/pmu: Manage MSR interception for IA32_PERF_GLOBAL_CTRL Date: Fri, 26 Jan 2024 16:54:18 +0800 Message-Id: <20240126085444.324918-16-xiong.y.zhang@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240126085444.324918-1-xiong.y.zhang@linux.intel.com> References: <20240126085444.324918-1-xiong.y.zhang@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Xiong Zhang In PMU passthrough mode, there are three requirements to manage IA32_PERF_GLOBAL_CTRL: - guest IA32_PERF_GLOBAL_CTRL MSR must be saved at vm exit. - IA32_PERF_GLOBAL_CTRL MSR must be cleared at vm exit to avoid any counter of running within KVM runloop. - guest IA32_PERF_GLOBAL_CTRL MSR must be restored at vm entry. Introduce vmx_set_perf_global_ctrl() function to auto switching IA32_PERF_GLOBAL_CTR and invoke it after the VMM finishes setting up the CPUID bits. Signed-off-by: Xiong Zhang Signed-off-by: Mingwei Zhang --- arch/x86/include/asm/vmx.h | 1 + arch/x86/kvm/vmx/vmx.c | 89 ++++++++++++++++++++++++++++++++------ arch/x86/kvm/vmx/vmx.h | 3 +- 3 files changed, 78 insertions(+), 15 deletions(-) diff --git a/arch/x86/include/asm/vmx.h b/arch/x86/include/asm/vmx.h index 0e73616b82f3..f574e7b429a3 100644 --- a/arch/x86/include/asm/vmx.h +++ b/arch/x86/include/asm/vmx.h @@ -104,6 +104,7 @@ #define VM_EXIT_CLEAR_BNDCFGS 0x00800000 #define VM_EXIT_PT_CONCEAL_PIP 0x01000000 #define VM_EXIT_CLEAR_IA32_RTIT_CTL 0x02000000 +#define VM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRL 0x40000000 #define VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR 0x00036dff diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 33cb69ff0804..8ab266e1e2a7 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4387,6 +4387,74 @@ static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) return pin_based_exec_ctrl; } +static void vmx_set_perf_global_ctrl(struct vcpu_vmx *vmx) +{ + u32 vmentry_ctrl = vm_entry_controls_get(vmx); + u32 vmexit_ctrl = vm_exit_controls_get(vmx); + int i; + + /* + * PERF_GLOBAL_CTRL is toggled dynamically in emulated vPMU. + */ + if (cpu_has_perf_global_ctrl_bug() || + !is_passthrough_pmu_enabled(&vmx->vcpu)) { + vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; + vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; + vmexit_ctrl &= ~VM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRL; + } + + if (is_passthrough_pmu_enabled(&vmx->vcpu)) { + /* + * Setup auto restore guest PERF_GLOBAL_CTRL MSR at vm entry. + */ + if (vmentry_ctrl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) + vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, 0); + else { + i = vmx_find_loadstore_msr_slot(&vmx->msr_autoload.guest, + MSR_CORE_PERF_GLOBAL_CTRL); + if (i < 0) { + i = vmx->msr_autoload.guest.nr++; + vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, + vmx->msr_autoload.guest.nr); + } + vmx->msr_autoload.guest.val[i].index = MSR_CORE_PERF_GLOBAL_CTRL; + vmx->msr_autoload.guest.val[i].value = 0; + } + /* + * Setup auto clear host PERF_GLOBAL_CTRL msr at vm exit. + */ + if (vmexit_ctrl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) + vmcs_write64(HOST_IA32_PERF_GLOBAL_CTRL, 0); + else { + i = vmx_find_loadstore_msr_slot(&vmx->msr_autoload.host, + MSR_CORE_PERF_GLOBAL_CTRL); + if (i < 0) { + i = vmx->msr_autoload.host.nr++; + vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, + vmx->msr_autoload.host.nr); + } + vmx->msr_autoload.host.val[i].index = MSR_CORE_PERF_GLOBAL_CTRL; + vmx->msr_autoload.host.val[i].value = 0; + } + /* + * Setup auto save guest PERF_GLOBAL_CTRL msr at vm exit + */ + if (!(vmexit_ctrl & VM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRL)) { + i = vmx_find_loadstore_msr_slot(&vmx->msr_autostore.guest, + MSR_CORE_PERF_GLOBAL_CTRL); + if (i < 0) { + i = vmx->msr_autostore.guest.nr++; + vmcs_write32(VM_EXIT_MSR_STORE_COUNT, + vmx->msr_autostore.guest.nr); + } + vmx->msr_autostore.guest.val[i].index = MSR_CORE_PERF_GLOBAL_CTRL; + } + } + + vm_entry_controls_set(vmx, vmentry_ctrl); + vm_exit_controls_set(vmx, vmexit_ctrl); +} + static u32 vmx_vmentry_ctrl(void) { u32 vmentry_ctrl = vmcs_config.vmentry_ctrl; @@ -4394,15 +4462,9 @@ static u32 vmx_vmentry_ctrl(void) if (vmx_pt_mode_is_system()) vmentry_ctrl &= ~(VM_ENTRY_PT_CONCEAL_PIP | VM_ENTRY_LOAD_IA32_RTIT_CTL); - /* - * IA32e mode, and loading of EFER and PERF_GLOBAL_CTRL are toggled dynamically. - */ - vmentry_ctrl &= ~(VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL | - VM_ENTRY_LOAD_IA32_EFER | - VM_ENTRY_IA32E_MODE); - if (cpu_has_perf_global_ctrl_bug()) - vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; + /* IA32e mode, and loading of EFER is toggled dynamically. */ + vmentry_ctrl &= ~(VM_ENTRY_LOAD_IA32_EFER | VM_ENTRY_IA32E_MODE); return vmentry_ctrl; } @@ -4422,12 +4484,8 @@ static u32 vmx_vmexit_ctrl(void) vmexit_ctrl &= ~(VM_EXIT_PT_CONCEAL_PIP | VM_EXIT_CLEAR_IA32_RTIT_CTL); - if (cpu_has_perf_global_ctrl_bug()) - vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; - - /* Loading of EFER and PERF_GLOBAL_CTRL are toggled dynamically */ - return vmexit_ctrl & - ~(VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL | VM_EXIT_LOAD_IA32_EFER); + /* Loading of EFER is toggled dynamically */ + return vmexit_ctrl & ~VM_EXIT_LOAD_IA32_EFER; } static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) @@ -4765,6 +4823,7 @@ static void init_vmcs(struct vcpu_vmx *vmx) vmcs_write64(VM_FUNCTION_CONTROL, 0); vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); + vmcs_write64(VM_EXIT_MSR_STORE_ADDR, __pa(vmx->msr_autostore.guest.val)); vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); @@ -7822,6 +7881,8 @@ static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) if (is_passthrough_pmu_enabled(&vmx->vcpu)) exec_controls_clearbit(vmx, CPU_BASED_RDPMC_EXITING); + vmx_set_perf_global_ctrl(vmx); + /* Refresh #PF interception to account for MAXPHYADDR changes. */ vmx_update_exception_bitmap(vcpu); } diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index c2130d2c8e24..c89db35e1de8 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -502,7 +502,8 @@ static inline u8 vmx_get_rvi(void) VM_EXIT_LOAD_IA32_EFER | \ VM_EXIT_CLEAR_BNDCFGS | \ VM_EXIT_PT_CONCEAL_PIP | \ - VM_EXIT_CLEAR_IA32_RTIT_CTL) + VM_EXIT_CLEAR_IA32_RTIT_CTL | \ + VM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRL) #define KVM_REQUIRED_VMX_PIN_BASED_VM_EXEC_CONTROL \ (PIN_BASED_EXT_INTR_MASK | \ -- 2.34.1