From patchwork Thu Nov 21 18:52:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882254 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 417A31D79A6 for ; Thu, 21 Nov 2024 18:53:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215201; cv=none; b=N6hm5Pc1A54/ogKreDkoFu/9YOx2nlQ7i3Scq0cJ0/E5lJy8lcxfWuNn6DsL2fdeW8fCQXBg9D/BgH84kxbV7zI9B7NA0Wbl3ymKFFyPU7acQkAbq+eennWa/x5ZVJXFj7cwMIuAILSCoMP9Huf3ovZQmW9gMEpCqGx11savwpQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215201; c=relaxed/simple; bh=eGhYnE5emXo94Pyg5quiuPFdiSkETv0T4xLbIt+gbbE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tmiwxqi00o7uU8sf8hH2WY+Qr/wHWbqKej68ARHc/7+7IAEeeCODXRX9f6ok0tjslicFCxYpLy1TdC//n2bmQJ3ZqqmA67MtJPaSpmtuN+xz1eEMyO8Fg44rIA866pTfkn7RBseqCazW7uJicuCgX9Wt9+EhmHcF1ZVeImasNNA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=epBHLhlg; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="epBHLhlg" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6eeb2b305efso30331267b3.0 for ; Thu, 21 Nov 2024 10:53:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215199; x=1732819999; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=T+9Madf5AUgYPaX6aUUL9JHycbWIRkK+aZqhIzgnbrs=; b=epBHLhlgmfnklftRQidJ2GmzQ+NWXPZW4UK+9IvFWJMBEz545B6BzS5+fs/HgOlrnx doncHJ6bH3mEZomG9hhOSpCDC+ZE+C3Mc/+aN+F+YxDoy56mOWkBuePVzTJzJM5Ze4H5 +6Qlc8xxQq9s4SlVnaaZZvNG1g3T5ZP7k8taUUxiWkBMEC/JdmAeMU6qyir8OpILwAQ9 TsYABene16OKHGimxZX0pZsTEPOdJScw+vldYYnQJ08ZAp3Lf0TvloFI6D7bEPIQLpbH EvppUyxtL6gq8rgZWbESl63tfumL8ia9OBvjtEVJCcM37Pqnj8w0BbokVSYVXEdXneHP 2y7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215199; x=1732819999; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=T+9Madf5AUgYPaX6aUUL9JHycbWIRkK+aZqhIzgnbrs=; b=IYQB4rQ36/Zgv6s6yLvowBT9+d/4PATnw6od7LHLitSTF/lSxlGn+1X4W1ak0JxvMz UtTwcMa9WdDMl2qYEirMjZAYhjFlc9IvuQ08fD0S3eiGutHZWS+CyJoDPfvHDx0/Lmsj gg72BdsPZKgQCvPiXTCDuvwBEUiFyIuCwQuGycFW5Z529sAEWaNqj4SwV4m6t/QPsKdg 6D3CgUMsLEXLvLcR75oTGludNeWnXykxr/VJx0YxCT3F46rUhQRIwAWdJHeY48e89m4E v6uf+eC50Hfn6ZXZD8A817c7OAmrNg1zrGmcrL/6NegnaS/8sCMt3oT/TV4e/CqK1vgT n92Q== X-Forwarded-Encrypted: i=1; AJvYcCWUVWd1qExFsk0C8nns584t4UTPPDLhwgchAfipoF64qYaPQa+q7fCu2Ugmbw1EuHaMG+w=@vger.kernel.org X-Gm-Message-State: AOJu0YwB4VEzEPUg4G2TCQqudkS3gwjGz5CZAlRMF08OppqR1X0QXe5x bor67uXyYHhAeZd9ed7noFq49KtuS4KiyNyyrIndCss143VsR//zDhOrpUrXgM4xKGlsOWwW0M3 5x181oA== X-Google-Smtp-Source: AGHT+IFW1c+yo5NB9i6gITZMQ/WYNQr1/fXSlza8bauLrwlg/McJYGaXVTMf7a6D63EaXjw6XMTTYqlKnd3N X-Received: from mizhang-super.c.googlers.com ([34.105.13.176]) (user=mizhang job=sendgmr) by 2002:a25:ab92:0:b0:e30:d5cf:775a with SMTP id 3f1490d57ef6-e38f74af44emr892276.6.1732215199276; Thu, 21 Nov 2024 10:53:19 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:52:53 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-2-mizhang@google.com> Subject: [RFC PATCH 01/22] x86/aperfmperf: Introduce get_host_[am]perf() From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang From: Jim Mattson In preparation for KVM pass-through of IA32_APERF and IA32_MPERF, introduce wrappers that read these MSRs. Going forward, all kernel code that needs host APERF/MPERF values should use these wrappers instead of rdmsrl(). While these functions currently just read the MSRs directly, future patches will enhance them to handle cases where the MSRs contain guest values. Moving all host APERF/MPERF reads to use these functions now will make it easier to add this functionality later. No functional change intended. Signed-off-by: Jim Mattson Reviewed-by: Mingwei Zhang Signed-off-by: Mingwei Zhang --- arch/x86/include/asm/topology.h | 3 +++ arch/x86/kernel/cpu/aperfmperf.c | 22 ++++++++++++++++++---- drivers/cpufreq/amd-pstate.c | 4 ++-- drivers/cpufreq/intel_pstate.c | 5 +++-- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/topology.h b/arch/x86/include/asm/topology.h index 92f3664dd933b..2ef9903cf85d7 100644 --- a/arch/x86/include/asm/topology.h +++ b/arch/x86/include/asm/topology.h @@ -302,6 +302,9 @@ static inline void arch_set_max_freq_ratio(bool turbo_disabled) { } static inline void freq_invariance_set_perf_ratio(u64 ratio, bool turbo_disabled) { } #endif +extern u64 get_host_aperf(void); +extern u64 get_host_mperf(void); + extern void arch_scale_freq_tick(void); #define arch_scale_freq_tick arch_scale_freq_tick diff --git a/arch/x86/kernel/cpu/aperfmperf.c b/arch/x86/kernel/cpu/aperfmperf.c index f642de2ebdac8..3be5070ba3361 100644 --- a/arch/x86/kernel/cpu/aperfmperf.c +++ b/arch/x86/kernel/cpu/aperfmperf.c @@ -40,8 +40,8 @@ static void init_counter_refs(void) { u64 aperf, mperf; - rdmsrl(MSR_IA32_APERF, aperf); - rdmsrl(MSR_IA32_MPERF, mperf); + aperf = get_host_aperf(); + mperf = get_host_mperf(); this_cpu_write(cpu_samples.aperf, aperf); this_cpu_write(cpu_samples.mperf, mperf); @@ -94,6 +94,20 @@ void arch_set_max_freq_ratio(bool turbo_disabled) } EXPORT_SYMBOL_GPL(arch_set_max_freq_ratio); +u64 get_host_aperf(void) +{ + WARN_ON_ONCE(!irqs_disabled()); + return native_read_msr(MSR_IA32_APERF); +} +EXPORT_SYMBOL_GPL(get_host_aperf); + +u64 get_host_mperf(void) +{ + WARN_ON_ONCE(!irqs_disabled()); + return native_read_msr(MSR_IA32_MPERF); +} +EXPORT_SYMBOL_GPL(get_host_mperf); + static bool __init turbo_disabled(void) { u64 misc_en; @@ -474,8 +488,8 @@ void arch_scale_freq_tick(void) if (!cpu_feature_enabled(X86_FEATURE_APERFMPERF)) return; - rdmsrl(MSR_IA32_APERF, aperf); - rdmsrl(MSR_IA32_MPERF, mperf); + aperf = get_host_aperf(); + mperf = get_host_mperf(); acnt = aperf - s->aperf; mcnt = mperf - s->mperf; diff --git a/drivers/cpufreq/amd-pstate.c b/drivers/cpufreq/amd-pstate.c index b63863f77c677..c26092284be56 100644 --- a/drivers/cpufreq/amd-pstate.c +++ b/drivers/cpufreq/amd-pstate.c @@ -446,8 +446,8 @@ static inline bool amd_pstate_sample(struct amd_cpudata *cpudata) unsigned long flags; local_irq_save(flags); - rdmsrl(MSR_IA32_APERF, aperf); - rdmsrl(MSR_IA32_MPERF, mperf); + aperf = get_host_aperf(); + mperf = get_host_mperf(); tsc = rdtsc(); if (cpudata->prev.mperf == mperf || cpudata->prev.tsc == tsc) { diff --git a/drivers/cpufreq/intel_pstate.c b/drivers/cpufreq/intel_pstate.c index 400337f3b572d..993a66095547f 100644 --- a/drivers/cpufreq/intel_pstate.c +++ b/drivers/cpufreq/intel_pstate.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -2423,8 +2424,8 @@ static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time) u64 tsc; local_irq_save(flags); - rdmsrl(MSR_IA32_APERF, aperf); - rdmsrl(MSR_IA32_MPERF, mperf); + aperf = get_host_aperf(); + mperf = get_host_mperf(); tsc = rdtsc(); if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) { local_irq_restore(flags); From patchwork Thu Nov 21 18:52:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882256 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC8091DE3D8 for ; Thu, 21 Nov 2024 18:53:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215204; cv=none; b=q/DV6E1Btex8E7cy5/BIk72zWUcuOuL8E5gDZiPWY33KzuXCze7bxvxVUNAeIlUQFpBKzyADmTNsWw/TcfqqPgJqadWq4nd+HUiPi+JFddpNtn7rldjRtwgdRKE1dOBjnNgHjkxo42s6FS8bv9N8/5LPxUJy+Ngikfq0rHUZRoc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215204; c=relaxed/simple; bh=1MOR7DCPmoVrL2EupOMAwcUroCAGOgOeQudXxZyVN2g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Uh4rRIo468IqaMcU8SSvJuD/y6dQY7o9SPdYwU4aJcv/gmCvy1RzWdptrGa22XZH6aOv0GvN83GOc4s7xikrgUqM8NSJQ0q2RmFAo62mKWsDZ6GyKKRESr0HfJMaqFwt3q936DNTyXwTSewgeRQOyXa34DhBOwEtx+VBGXIULZ8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ktVOWZII; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ktVOWZII" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6eae6aba72fso24218117b3.2 for ; Thu, 21 Nov 2024 10:53:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215201; x=1732820001; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=yX+GRl14cPObJa/kACzBQrLOUmiZNTxc+R8PcnYIGO8=; b=ktVOWZIIM9e6kSmCyKrD20l+GKNugXeKhziRNR5qitJmewhveDSbmql9bIyJ9T+jNp vrZjxMwMO3dZW0op2r6VOPwFb9uRu2SDNUCCl8lCgYKjco+U26fepAXc79EnhWfZ3a6A xQ+8xBNM9sdW6Fikelon+mBeFOHrF0bnr64KIWbR56Hsw8SwB1WEIte7oFiYsHwVQ+Hj unX1vsq2LqutNwCtnA253MTVKR17RUJbPLdni7X9MU8LU4uAg4fKK6CNCI3CObeWccBw 6yb3flmEGYNFIlTLGWp3fU6mCSrga81+eJKUA3fuY7HqjGYBVkvDRZ8TMvWfNtRz+AFt JEvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215201; x=1732820001; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=yX+GRl14cPObJa/kACzBQrLOUmiZNTxc+R8PcnYIGO8=; b=igRo8LLtSCyMWhyk61cO87nQA/CXpl4Ba9ac0xISFFUYchweLuxed0X33MLLQSX2K2 9gTbNIYDHC4flkJPRpY5Tsbi9tTqCmHQ69IYlJcpNLAyfFeL6vbYnKSo+gq+j7U5riKb hpUIy6m9osgMxomPpTAqw99FTsLDVB9iWKbpYsIae7JQeKTkQr2WbJsSBVgYw2Si8LbE Srgklk8hdCGEhnPiIGvYajl0KxYyLIW/GGsxKaMqmY9Pr2mJYl7C8XxUKdL0/yW4QBjt kQ1ilIuMM+XRwDwXo0hRS8NKtO3BGLEnsp/MTfnLGXRnVpKo3eLDQ8zLyimPjlrcOJKy r+vQ== X-Forwarded-Encrypted: i=1; AJvYcCXUuxddOUpsC/OwEJzP9eCuJxlht6bp+2nsg19NtbE+QIMd9nA5ips6In1yCIdO1eLA4jI=@vger.kernel.org X-Gm-Message-State: AOJu0Ywi1BGeVQ6l+vEsN6Ydmbece7Tj3geM0S6G9zPUjk9hedadQvs5 JXjoRbGXx+U4dwqB5KP3WThfUl7OyvV0QvQdNSUy9G7Si67iE6eGoFcIRBIbhp8PojNjAYF2NfU zVTPFhA== X-Google-Smtp-Source: AGHT+IEaIdHhehjQ/cZzYbs1B5PHMTwy0M/hKHYmKCVWAh5XULvcYUBoSgtrQsU3NFy0g50qZrAVQfI3lmYy X-Received: from mizhang-super.c.googlers.com ([34.105.13.176]) (user=mizhang job=sendgmr) by 2002:a05:690c:67c1:b0:6ea:1f5b:1f54 with SMTP id 00721157ae682-6eee07bb768mr3687b3.0.1732215200906; Thu, 21 Nov 2024 10:53:20 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:52:54 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-3-mizhang@google.com> Subject: [RFC PATCH 02/22] x86/aperfmperf: Introduce set_guest_[am]perf() From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang From: Jim Mattson KVM guests need access to IA32_APERF and IA32_MPERF to observe their effective CPU frequency, but intercepting reads of these MSRs is too expensive since Linux guests read them every scheduler tick (250 Hz by default). Allow the guest to read these MSRs without interception by loading guest values into the hardware MSRs. When loading a guest value into IA32_APERF or IA32_MPERF: 1. Query the current host value 2. Record the offset between guest and host values in a per-CPU variable 3. Load the guest value into the MSR Modify get_host_[am]perf() to add the per-CPU offset to the raw MSR value, so that host kernel code can still obtain correct host values even when the MSRs contain guest values. Signed-off-by: Jim Mattson Reviewed-by: Mingwei Zhang Signed-off-by: Mingwei Zhang --- arch/x86/include/asm/topology.h | 5 +++++ arch/x86/kernel/cpu/aperfmperf.c | 31 +++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/topology.h b/arch/x86/include/asm/topology.h index 2ef9903cf85d7..fef5846c01976 100644 --- a/arch/x86/include/asm/topology.h +++ b/arch/x86/include/asm/topology.h @@ -302,8 +302,13 @@ static inline void arch_set_max_freq_ratio(bool turbo_disabled) { } static inline void freq_invariance_set_perf_ratio(u64 ratio, bool turbo_disabled) { } #endif +DECLARE_PER_CPU(u64, host_aperf_offset); +DECLARE_PER_CPU(u64, host_mperf_offset); + extern u64 get_host_aperf(void); extern u64 get_host_mperf(void); +extern void set_guest_aperf(u64 aperf); +extern void set_guest_mperf(u64 mperf); extern void arch_scale_freq_tick(void); #define arch_scale_freq_tick arch_scale_freq_tick diff --git a/arch/x86/kernel/cpu/aperfmperf.c b/arch/x86/kernel/cpu/aperfmperf.c index 3be5070ba3361..8b66872aa98c1 100644 --- a/arch/x86/kernel/cpu/aperfmperf.c +++ b/arch/x86/kernel/cpu/aperfmperf.c @@ -94,20 +94,47 @@ void arch_set_max_freq_ratio(bool turbo_disabled) } EXPORT_SYMBOL_GPL(arch_set_max_freq_ratio); +DEFINE_PER_CPU(u64, host_aperf_offset); +DEFINE_PER_CPU(u64, host_mperf_offset); + u64 get_host_aperf(void) { WARN_ON_ONCE(!irqs_disabled()); - return native_read_msr(MSR_IA32_APERF); + return native_read_msr(MSR_IA32_APERF) + + this_cpu_read(host_aperf_offset); } EXPORT_SYMBOL_GPL(get_host_aperf); u64 get_host_mperf(void) { WARN_ON_ONCE(!irqs_disabled()); - return native_read_msr(MSR_IA32_MPERF); + return native_read_msr(MSR_IA32_MPERF) + + this_cpu_read(host_mperf_offset); } EXPORT_SYMBOL_GPL(get_host_mperf); +void set_guest_aperf(u64 guest_aperf) +{ + u64 host_aperf; + + WARN_ON_ONCE(!irqs_disabled()); + host_aperf = get_host_aperf(); + wrmsrl(MSR_IA32_APERF, guest_aperf); + this_cpu_write(host_aperf_offset, host_aperf - guest_aperf); +} +EXPORT_SYMBOL_GPL(set_guest_aperf); + +void set_guest_mperf(u64 guest_mperf) +{ + u64 host_mperf; + + WARN_ON_ONCE(!irqs_disabled()); + host_mperf = get_host_mperf(); + wrmsrl(MSR_IA32_MPERF, guest_mperf); + this_cpu_write(host_mperf_offset, host_mperf - guest_mperf); +} +EXPORT_SYMBOL_GPL(set_guest_mperf); + static bool __init turbo_disabled(void) { u64 misc_en; From patchwork Thu Nov 21 18:52:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882257 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A76F61DE4FD for ; Thu, 21 Nov 2024 18:53:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215205; cv=none; b=RVoA2pLxMPzpPZwFPW5hCI67vfn5IrbhBEG2zZkyIS1hPXKKhNFM/4ncq1xVFCdOauTtA1vpnz2I+5DHf6W5M2WnsqB4XByWs7x4ABKl0s7uY6a8RcEJrE0igV+XNHORtehCk8RJtEcquFWfCP6DuUP+Fp8viVGTA75q5kzIZiQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215205; c=relaxed/simple; bh=/MlTsvEZhBm6lsxe7EmxCdECCTQKt4d+z1t/zvxQERw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FVNzkBys9MgV0bvGtYKWmUL2kBapCZ4UJrLlFWrL0Dbtdx33CmXKTdcl7elrcSVc6ENrqPmhMrzLljh9Y7s66Sy8pvSghIex+Ityd4iEJ7Bwh3cFk2REtJpmxMsxKfYcUMvJq4XwhfPsKgRxUrxu7Z5B1r6O0RhySa9qwJwISqg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=hFstuxpQ; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="hFstuxpQ" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e387e8e8426so3251367276.1 for ; Thu, 21 Nov 2024 10:53:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215202; x=1732820002; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=rsHo0Py3TPU7pbh7g3h3HouheNFLNCt6iVoC0xc8Ffg=; b=hFstuxpQUnAlVZ3bZY0abEGefUG6T5jzWCjPKELKL2luMCA0BgWlYBnf/t8KI/Fcag gpjsF21oDoUURQfgS4Wtb3BXl+i//gNyaPJOEEjy94US8YihoTUdtW4SitxRCesVEUea vxHJdK4GDFgE30iC1NVwzwk3rhAHsEz/WzYnSvpT6QoXZn6VNSgF1bkyRmSRRfz8V28D mqOkCcoeTfkTgrENC3dl7X04Z54uYxMrQJFtuhQdY1xPkHd158oMT3CfiyF0wWNtQcRE VBs35tgJczBa26nosQ4/hsw0AJLMnOMQpyqBse7zc2u04zwX6FF31jQidRu6qcUJI78z LHsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215202; x=1732820002; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=rsHo0Py3TPU7pbh7g3h3HouheNFLNCt6iVoC0xc8Ffg=; b=wRGM+/gff5VUx8JZevXj9MqXeWg5dh2hF22ih8TxHjFTdmfeLAeBfbKy56hBSAhXIy rhG7EQGRxsTEjCwAuyCYAV17SUc3MfOa7xEvoV1etapOVAQdJhibID8ZKJgmGbSLmTdu gNlRKWIl4JUkyTYMNL8iinXaUCsO2QpjWkVejl6ZLg42ebC/poMqmZSURToy8ycRve1Q pWRkEe8VwfsujzxBRXYGyA75WWUE3A5xCcqSFRUpvbNaApq3oRckwzwCfW7NXonYkLAZ 8trI3KQfEzj/Rkhzw9hxfnh20kMsP3uuQNfKRkcx1Skj3l5JdOLXK/I2JI8D0vzgxXzK tqQw== X-Forwarded-Encrypted: i=1; AJvYcCWgTNrpMEOsptzGgQOEDsZq39mibGaJV1HAqUdK0vPw1B5uVc2QZIsI4NV1EZ+GPpfQ69E=@vger.kernel.org X-Gm-Message-State: AOJu0Yx2WAsjTRI7uICd1B6Q3QYvAkFGcCDDaKhA3og3fbbpQJ2tzrD8 tS0rSfhRX8XiJGU4vS9Xhzs8OSj9zw3NO1z4kTdmIW7d560Sk/5QzsC5aE9zlljXIByj9330fmn 13Ko6Ig== X-Google-Smtp-Source: AGHT+IEa89oTDs6AEs0b6qM9YtZe6l8s01zJVqD4TK4bwVkMk4+VBxvLRHFEIhtWa5nFhIw1lpDFQnaYmZLg X-Received: from mizhang-super.c.googlers.com ([35.247.89.60]) (user=mizhang job=sendgmr) by 2002:a25:e90b:0:b0:e2e:317a:d599 with SMTP id 3f1490d57ef6-e38f700d9c3mr998276.2.1732215202627; Thu, 21 Nov 2024 10:53:22 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:52:55 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-4-mizhang@google.com> Subject: [RFC PATCH 03/22] x86/aperfmperf: Introduce restore_host_[am]perf() From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang From: Jim Mattson Round out the {host,guest}[am]perf APIs by adding functions to restore host values to the hardware MSRs. These functions: 1. Write the current host value (obtained via get_host_[am]perf()) to the corresponding MSR 2. Clear the per-CPU offset used to track the difference between guest and host values Signed-off-by: Jim Mattson --- arch/x86/include/asm/topology.h | 2 ++ arch/x86/kernel/cpu/aperfmperf.c | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/arch/x86/include/asm/topology.h b/arch/x86/include/asm/topology.h index fef5846c01976..8d4d4cd41bd84 100644 --- a/arch/x86/include/asm/topology.h +++ b/arch/x86/include/asm/topology.h @@ -309,6 +309,8 @@ extern u64 get_host_aperf(void); extern u64 get_host_mperf(void); extern void set_guest_aperf(u64 aperf); extern void set_guest_mperf(u64 mperf); +extern void restore_host_aperf(void); +extern void restore_host_mperf(void); extern void arch_scale_freq_tick(void); #define arch_scale_freq_tick arch_scale_freq_tick diff --git a/arch/x86/kernel/cpu/aperfmperf.c b/arch/x86/kernel/cpu/aperfmperf.c index 8b66872aa98c1..4d6c0b8b39452 100644 --- a/arch/x86/kernel/cpu/aperfmperf.c +++ b/arch/x86/kernel/cpu/aperfmperf.c @@ -135,6 +135,22 @@ void set_guest_mperf(u64 guest_mperf) } EXPORT_SYMBOL_GPL(set_guest_mperf); +void restore_host_aperf(void) +{ + WARN_ON_ONCE(!irqs_disabled()); + wrmsrl(MSR_IA32_APERF, get_host_aperf()); + this_cpu_write(host_aperf_offset, 0); +} +EXPORT_SYMBOL_GPL(restore_host_aperf); + +void restore_host_mperf(void) +{ + WARN_ON_ONCE(!irqs_disabled()); + wrmsrl(MSR_IA32_MPERF, get_host_mperf()); + this_cpu_write(host_mperf_offset, 0); +} +EXPORT_SYMBOL_GPL(restore_host_mperf); + static bool __init turbo_disabled(void) { u64 misc_en; From patchwork Thu Nov 21 18:52:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882258 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 72C021DE8AA for ; Thu, 21 Nov 2024 18:53:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215207; cv=none; b=E6o6SNK3Vhi4qdRLksOky/9TKjULeb1+qBmR4RF2XOaNG3QykqRi5HEQ2ZxSs/F9A/fiJbcjYyLzV2WPzqzkTe9jnHLqVYjvnT+VOSRdn2NKeB5bfB9XUTHIZ2kR8Bx6U/bUn375SpkniHgPoGkJgZ5JN4+EEvJ7rILmg5euVTQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215207; c=relaxed/simple; bh=zQeHQQYxmX3ZzHTtjiFSFfyrCaL0bw/h5dOSf2uFEmc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FDnZRqE6Ru6YP4DgI0XvflEFxWYMy4XJdogpCcSmvW7f5OXJI8w2XVRwvZTGLEFWiz5HYRnkzMO9NwtPqpQVHITLNCYwepU6qKPrbZv2e32IKUOtEwhu41xFI0xpmtUF6ocqCBARuf2PNpXusJePFeQaEej6Hhi7Xjs2HYHBXiI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=pTkCW21K; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="pTkCW21K" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e38797ab481so2357549276.0 for ; Thu, 21 Nov 2024 10:53:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215204; x=1732820004; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=wBqsFsDnuHLstZ7Qk2AL8tKHRSfdoFWPpSV+NKxD0lI=; b=pTkCW21KMEF2eNcLmI8jpilUsPe92gbjskvw4jUohMNmebEwTMwNdH4BERGVqMh8h5 xy3ZSu5oy3TYxB1rJ2BuDyC6VtISayiRfobC803jLPxzRX85kbmEqSA4XBgYT1uDV1AX BdIphr/jnwCb2lilObf/R4ZK6TVohe+7d5m9N7qzJq4rkOfvkrTd8snZmVof0GuTC3cO E/fS5UKY0tYTwZpJtgjjOyGFIUNLZB6GvobHG/t33ImuoGfPaVw0RmNK6bKk4oPV7nJr EKuTtGRY4JDTG1l0q2EsgN0ZvHazTc29Y0QeX0Vpz2UNUav/vTdTMf783pEPRx3mHI+S dB+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215204; x=1732820004; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=wBqsFsDnuHLstZ7Qk2AL8tKHRSfdoFWPpSV+NKxD0lI=; b=VgD1zG0YP6vamIs4iunpAas3qh3w0iw+iMeFIJGynwmHR5uAtQl5OE0bI7EriASxW5 /1hko9WuhXNbKm7gnWn/flThgYiusLUPbAj38G+KYUZles6RKnOapzxsP0Lf2o0BsIa6 JxvxNRPQrcEorGFcJNzXtcj9WQXvuuBM6VNlNPJ2/B7FbtFeqXNjSb0WcGRc8Y0K8hz/ kJIikmc91yGjKm0El8FB3TK0WdRBX29SzTz3d72XY6kMwpsrwZa88KVY8p1v5dAp/RqI 63eC3DbZkYr/dfqp1GL9zWJzhE+yEK4etRy4GjLfvoxWyycqU6s0oV5Avsd49204ZGHn YNLA== X-Forwarded-Encrypted: i=1; AJvYcCUCliXzoX7wkl/wmrbxkSGDNHuXHbp1yNFTDJ1IBmgjclvG5QsHQKfIuTSHioXpBrX4aIE=@vger.kernel.org X-Gm-Message-State: AOJu0YysIcQmhlt4DC2Oq7vIUmUG5FH6wX5JkLnGWMfm6v41sFS5UbJq wH95OD9NfVihrY3MdkJIxArwX8sJiDeQw4x2oB421w0/oZah6u/g05o9Vfcerzn4YkrASVQLbrZ D7AWsMQ== X-Google-Smtp-Source: AGHT+IFR7b00AVpq3w7NzmU3QLKaMUDgzjZ3a4b8x79R2STg6s1+R4EedNLCWPd00E0OkIf55MhuejUtLDkw X-Received: from mizhang-super.c.googlers.com ([35.247.89.60]) (user=mizhang job=sendgmr) by 2002:a25:8250:0:b0:e33:104c:fac8 with SMTP id 3f1490d57ef6-e38cb748645mr3108276.7.1732215204400; Thu, 21 Nov 2024 10:53:24 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:52:56 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-5-mizhang@google.com> Subject: [RFC PATCH 04/22] x86/msr: Adjust remote reads of IA32_[AM]PERF by the per-cpu host offset From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang From: Jim Mattson When reading IA32_APERF or IA32_MPERF remotely via /dev/cpu/*/msr, account for any offset between the hardware MSR value and the true host value. This ensures tools like turbostat get correct host values even when the hardware MSRs contain guest values. Signed-off-by: Jim Mattson Reviewed-by: Mingwei Zhang Signed-off-by: Mingwei Zhang --- arch/x86/lib/msr-smp.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/arch/x86/lib/msr-smp.c b/arch/x86/lib/msr-smp.c index acd463d887e1c..43c5d21e840fb 100644 --- a/arch/x86/lib/msr-smp.c +++ b/arch/x86/lib/msr-smp.c @@ -4,6 +4,15 @@ #include #include #include +#include + +static void adjust_host_aperfmperf(u32 msr_no, struct msr *reg) +{ + if (msr_no == MSR_IA32_APERF) + reg->q += this_cpu_read(host_aperf_offset); + else if (msr_no == MSR_IA32_MPERF) + reg->q += this_cpu_read(host_mperf_offset); +} static void __rdmsr_on_cpu(void *info) { @@ -16,6 +25,7 @@ static void __rdmsr_on_cpu(void *info) reg = &rv->reg; rdmsr(rv->msr_no, reg->l, reg->h); + adjust_host_aperfmperf(rv->msr_no, reg); } static void __wrmsr_on_cpu(void *info) @@ -154,6 +164,7 @@ static void __rdmsr_safe_on_cpu(void *info) struct msr_info_completion *rv = info; rv->msr.err = rdmsr_safe(rv->msr.msr_no, &rv->msr.reg.l, &rv->msr.reg.h); + adjust_host_aperfmperf(rv->msr.msr_no, &rv->msr.reg); complete(&rv->done); } From patchwork Thu Nov 21 18:52:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882259 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 291211DEFC6 for ; Thu, 21 Nov 2024 18:53:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215209; cv=none; b=HXZ0MRWNCtZd56rsd7YJy8XzLzLModbbcFZerVCwXTEcDRXTpwkFV2Ebt2kws05Bjc7J9QH5anxGH1ONUJTrInvGKi3vM6X2UlyphulB9bbOh7UhcXxDnjhjkJ9A6tJJKccFiQ4qRJi9rZLmh6zSWOr5qQL64sgQQZdX3ICgEVM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215209; c=relaxed/simple; bh=3LJNuc0FG7aWLKpUxqt+8FC4Sp0enBCrekTBe7yW4mc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=QZzbOkQjt2sCRRUfjzo/u/0jHFM29WEv1bmJQBnizbULk5ZsycM2oTU3XJwMKurbbemks/0ejdi8hWht7eqikoEBL92++g7mAosY0rluKHfBDgPjTPJbNdsOekvFDrzJqSLtMBmzyX/Uqq6vdYRS3vvWJzRa6UIngLqMfaQKNYM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=l64VzQCS; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="l64VzQCS" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6ee6122ea3fso20933377b3.1 for ; Thu, 21 Nov 2024 10:53:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215206; x=1732820006; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=ldqdeENXrKnhWpzxrOZizNypt8qWrYQwaqWYe/ZI/p0=; b=l64VzQCSX2OGlNkHidG4Pt0qBFrGyhfmGmqFscUPYgRZrBHVwS2wJBbjwAqfSJHbv4 7FMXsJQe7DBkkWaISjT4x+emou5wWjmt0KbUpdc+75o11v82JsUY2Neng/yY+aK2/ocK Oiv41CxvMpdKI2YmThz9ONdy0ssCsYSlKW8+yu17WxX87ptRT8ELRSG2lTEiGoKW+VRF uAfHfNsS7yPNWf41QnXtJUxHbZv6CWTWMmjmZoRcEdZrNE/K5alA+WvtZS/RXhxlhbDk uSBj58D8fMVYim0FigXRUgoQ8SQAOz4C2ci/Q8vUOlfBrXWO+7nTgLQGHV2OEvSVwUH4 ngyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215206; x=1732820006; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ldqdeENXrKnhWpzxrOZizNypt8qWrYQwaqWYe/ZI/p0=; b=PbgghVT9la/K78+/TavIN8xkH3A5EOo2F7fzm/pBMbB5ID32rnaRJgDMjVNgDt6rw1 1ryLGQJoypZbhpDLRz32wtYNsL6NKqMCT9L+BB8XXIYGVMr0IHsT43osaNPr9qo1zlyg XhArAOH44W5qFrTLRmy7oHPAVtPmTGGsXF8VsVAa8XJLJ33oDmTFFiikcY92bCP5Eo9e DbpuDhuw2dkILTaDWnYSDYecQEw9P2/M7a1Hl1uR70jg5g/LJYBLQlEgJytdXff4vPC1 lKXjXrE7egudnwt4BIAO1/E9YN3efKyWXcJIDYt5ZJ5BlPhy3SgIDFQ0Sn5zypRYGoH+ h05g== X-Forwarded-Encrypted: i=1; AJvYcCVH9NaUc5pgUREtYJevnYXhUOt3TKMNiBAj6Zp6Tgk9/fSeXuC3VJCttyXPvA+ZxmLRPXE=@vger.kernel.org X-Gm-Message-State: AOJu0YzrHY1bxAoHUVHOEeOLTGTbV7R3ARzwaMnvp9sVB3PX3vD3dhcQ ZJR1lDh2cl8tdVUDhida/kvECsTZdxQu88+qCXjIYm1KwnMKjeMu2lb4+GkjzY0ndCm3ip/pcbs I2eiU8A== X-Google-Smtp-Source: AGHT+IEvgJFcwcEy1VprydexSjluVe7GGGLSwDWQd6+6WjkEkzi4dzVs6HiLRuow03gjfWhDHnT4jtN61JUj X-Received: from mizhang-super.c.googlers.com ([35.247.89.60]) (user=mizhang job=sendgmr) by 2002:a5b:9d2:0:b0:e28:f231:1aa8 with SMTP id 3f1490d57ef6-e38cb546a8cmr7249276.2.1732215206128; Thu, 21 Nov 2024 10:53:26 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:52:57 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-6-mizhang@google.com> Subject: [RFC PATCH 05/22] KVM: x86: Introduce kvm_vcpu_make_runnable() From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang From: Jim Mattson Factor out common code that handles the transition from HALTED/AP_RESET_HOLD to RUNNABLE state. In addition to changing mp_state, this transition has side effects (clearing pv_unhalted, apf.halted) which must be handled consistently across all code paths. As future patches add more side effects to this state transition, this helper ensures they will be applied uniformly at all transition points. No functional change intended. Signed-off-by: Jim Mattson Reviewed-by: Mingwei Zhang Signed-off-by: Mingwei Zhang --- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/x86.c | 16 +++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 6d9f763a7bb9d..04ef56d10cbb1 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2283,6 +2283,8 @@ static inline bool kvm_is_supported_user_return_msr(u32 msr) return kvm_find_user_return_msr(msr) >= 0; } +void kvm_vcpu_make_runnable(struct kvm_vcpu *vcpu); + u64 kvm_scale_tsc(u64 tsc, u64 ratio); u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc); u64 kvm_calc_nested_tsc_offset(u64 l1_offset, u64 l2_offset, u64 l2_multiplier); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 83fe0a78146fc..3c6b0ca91e5f5 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11167,6 +11167,16 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu); } +void kvm_vcpu_make_runnable(struct kvm_vcpu *vcpu) +{ + if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED || + vcpu->arch.mp_state == KVM_MP_STATE_AP_RESET_HOLD) + vcpu->arch.pv.pv_unhalted = false; + vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; + vcpu->arch.apf.halted = false; +} +EXPORT_SYMBOL_GPL(kvm_vcpu_make_runnable); + /* Called within kvm->srcu read side. */ static inline int vcpu_block(struct kvm_vcpu *vcpu) { @@ -11222,12 +11232,8 @@ static inline int vcpu_block(struct kvm_vcpu *vcpu) switch(vcpu->arch.mp_state) { case KVM_MP_STATE_HALTED: case KVM_MP_STATE_AP_RESET_HOLD: - vcpu->arch.pv.pv_unhalted = false; - vcpu->arch.mp_state = - KVM_MP_STATE_RUNNABLE; - fallthrough; case KVM_MP_STATE_RUNNABLE: - vcpu->arch.apf.halted = false; + kvm_vcpu_make_runnable(vcpu); break; case KVM_MP_STATE_INIT_RECEIVED: break; From patchwork Thu Nov 21 18:52:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882260 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1C4B91DED59 for ; Thu, 21 Nov 2024 18:53:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215210; cv=none; b=gJBzDQ8/wqlFD4/04sgzP1Poz6HrFmCGcw8xLUlolLUrJ1UQUqGDxIZqGaYe0Y//SNzQy1wvx47AP5RP8sHhwOVcbtRX8QTIRbImqQVw0sMDzLu7Rb1Oa9OaS1qj13nDvRNKIzO2EhWqDZxjX9HcoKexo5VrkAlAHFjhuykbqic= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215210; c=relaxed/simple; bh=SxrvpTqqakO+SE1JSfeW2dZ+qSHPbHK7pKymKVP/ZXE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DkS6idNHtyNU17mEtfB5bjYXMKDx3YCRC3e7i+5r2xcbxdkDAwsK3UgDgvKJV5OMVCbDKwXcY2XVxYJZ3ldydwNtwJe2kCbpSmB/Hlu6ppQ9U1eeLw/vMcnVw2ewgAdzndNJvHvAxBabJ8ikIge7VHrEY9hwLva2zu18QLjP+f8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=3Hirfohn; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="3Hirfohn" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6eeb8aa2280so20398707b3.1 for ; Thu, 21 Nov 2024 10:53:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215208; x=1732820008; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=+b6W97BMpWzJsRkR70E8bmCkFexWmOp62HeJxz267S8=; b=3Hirfohnw5OR/d6y1tUuuQruToXiIzqE+NYWUoNDJQrLP2VQqsbreE68+fd9kLwfXL 0+zqCw8gktyAgpONx00PVqaJMf5boSxD5ffnGZVDiA4/q9z6vd9vIgoEl1XItwAMIxiA /ZAfoQADeaal9BKGF5eqCIVJJ0FTop+qjX/LX4lqlK05SgM+YNak8KkZlSR3XYzzuKSb jYJ7+zU2ZVxiyMCeJiMW8z15HQceERfGfz3yWAfsAYJniFZ5J5mM5dErHDkxfPaNJ64X wY4tm+Cwb8kRHNKPBqX+evyLiQICl8b8eZxii43Y3ZKQ2sgNZwqxFEYxKSYMtSiRGuNK pb/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215208; x=1732820008; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=+b6W97BMpWzJsRkR70E8bmCkFexWmOp62HeJxz267S8=; b=MoeWh0yJKtZ7bw7ncUrMnsqW8tUdI5OMnZwL8CYyDzaJZQeYwWiWcbTsn0WnGUqyPu IGoQOzcWTKyPQ7PvXPkUNk+sPWfP4ZqQOv8c3GD3CTt3JUPaOdvvb9ZcKyKGIdZId+fM OytGiU6SFID3mA47Mqssv/NM2lTz/e4UHg5lcjbb1gzR33c1GHX4WhTi7G4dgUBGE74A n0/Stu6vl02GPdmzn0e1JOeOOCVEpYDaHCojylh4cD64mCsdJ55eUd8CMNYdw9qFiuRF Q3qigssWwJWCmDO/bouv3QWD+3CjmgbgQdJAhiUzEjiNnzfqy9rP6mzimwAqk6y4yDvP 8Zqw== X-Forwarded-Encrypted: i=1; AJvYcCUa8mMU5U7FuiD5OgefqEGl8KN/9rxKn1tFLdGMmLj99m0WjeQuh8ENPUIGtZbXJ4uMNiI=@vger.kernel.org X-Gm-Message-State: AOJu0YxwPPEjVjpv9+1X0F95BfSvGWRS5tKLpLkVsH5Nee2TezNKC0/b xrmpd/14+4xP5rZQOV5LV/hA+O8im/ohihjZXUkFO8VbAa9rIZ1bRp7brJLoa8LAdMAg0eDI1yU ud9k7hg== X-Google-Smtp-Source: AGHT+IHA/LlqfVC4cB578a8wvHBObnhD0lyB5qPv6+RAzcNl/mZXGlIBsUax3fhUb5PzQtIk0zmfwsAkpA1H X-Received: from mizhang-super.c.googlers.com ([35.247.89.60]) (user=mizhang job=sendgmr) by 2002:a05:690c:6709:b0:6e2:1713:bdb5 with SMTP id 00721157ae682-6eee0a39166mr21687b3.5.1732215208089; Thu, 21 Nov 2024 10:53:28 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:52:58 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-7-mizhang@google.com> Subject: [RFC PATCH 06/22] KVM: x86: INIT may transition from HALTED to RUNNABLE From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang From: Jim Mattson When a halted vCPU is awakened by an INIT signal, it might have been the target of a previous KVM_HC_KICK_CPU hypercall, in which case pv_unhalted would be set. This flag should be cleared before the next HLT instruction, as kvm_vcpu_has_events() would otherwise return true and prevent the vCPU from entering the halt state. Use kvm_vcpu_make_runnable() to ensure consistent handling of the HALTED to RUNNABLE state transition. Fixes: 6aef266c6e17 ("kvm hypervisor : Add a hypercall to KVM hypervisor to support pv-ticketlocks") Signed-off-by: Jim Mattson --- arch/x86/kvm/lapic.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 95c6beb8ce279..97aa634505306 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -3372,9 +3372,8 @@ int kvm_apic_accept_events(struct kvm_vcpu *vcpu) if (test_and_clear_bit(KVM_APIC_INIT, &apic->pending_events)) { kvm_vcpu_reset(vcpu, true); - if (kvm_vcpu_is_bsp(apic->vcpu)) - vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; - else + kvm_vcpu_make_runnable(vcpu); + if (!kvm_vcpu_is_bsp(apic->vcpu)) vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; } if (test_and_clear_bit(KVM_APIC_SIPI, &apic->pending_events)) { From patchwork Thu Nov 21 18:52:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882261 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8E9AF1DF745 for ; Thu, 21 Nov 2024 18:53:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215212; cv=none; b=d91Xfb86cSI6BbnxWLnll/2WZIW190hMdnES1rfO2aVoyaRVZvdoalY7lnGKvfjYeRH78oNYPdWqX3D8p24Ju+5MalfLkHQxqu8yEtzXDmBO5a3MR7bGCXTa33z+Du6XUmKPXE62oVO/1VHluPfa0aqkXrFc0ucBHcBUnuIdkRM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215212; c=relaxed/simple; bh=HIwgyq0n9Pc6IQ1JW0mNNbc4Q1w8gUiM8GLpxz3MNio=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uqauNA4MCwbNdLmVqrtCgaKxeeeR7MykYsrIUoitJe+1Ke79IvlkZhjl79S6tEQ1mWlV3DTkP3GCj8IVE/k86dLTNRZLCdWCbI+B90poRI8mVzewmvOw/obvA5/ysQKDoP2SCEzkAIhKLJW8X9blT2vihZYiUwVItWMXLJhXnBo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ZmRG4+Du; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ZmRG4+Du" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6ea258fe4b6so27708837b3.1 for ; Thu, 21 Nov 2024 10:53:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215209; x=1732820009; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=179m8kOvQfRqP37wmtAhsMQnswgm0gukoWaPbObarIY=; b=ZmRG4+DuMWNCDnDdrGbuILOaPLCIfuysnoc2coDwQ9tvZBu971oWt8QGQk/v0ZG8SB lh5yW9bVqyx41EYwpRhDt36BkHYVPtwoBFU9mOdt9BI4okKYZTl0Y60nUhZiUkXKkGnc 93ejV7dIWXIb3QafDV6dDwcIWoxGsGmwlBmk9vUh3e2EZ6aRHGpO823QSDtTPjf1VWTp 4K7z+Gd2pl7/RRaZ/yK1kZVyAxuPbfBXYG55UrptpxAvdQNMc1mU7XR3ZDQL1lPPiRtY hYJwGYmF0PTApfT5w1xPuVXyEkO1p2x06sKtUegp7cX4vLjMdy0J7luuiyXrV9yV8mXw aBtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215209; x=1732820009; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=179m8kOvQfRqP37wmtAhsMQnswgm0gukoWaPbObarIY=; b=VSbRC1f3iXG3+OLykBZTrEzQRR2b4RYziutKweptU91/uhu1VpOasZ0PlABL6It8t0 U4QRmv/bOjNgrRIY28mWf2cydzd57trkB7G/UJ9hgDdUmAHAvdmUEgzQDoYCRurye8Vh wC2Ac4zU7BOKgDp2jx2huLZ2WtpLEvw7awYBlWbOmorTGwryIOQfpzEMDKl4Yl0A0Y35 gz6wW9dAOYLMoEhGfbJCQENVfTh13isc4R7uIpwt7b/um1oxv+CtfKLOixSAv1SXvWO7 mbPUFpnIztz9+UlUUHlq3deJwHlMVgC13/h6rpOm79fGMzZJHh/nOgvNDerBYQMm3ZXG Aq4A== X-Forwarded-Encrypted: i=1; AJvYcCWV3fsXSrD7AwzQwlv61KtFO/2m4ITt/yRV/p3K/6ZSaz4eQ14nti6vWYwTsMMqpeU52YQ=@vger.kernel.org X-Gm-Message-State: AOJu0YwHSIjlVlnXN7xSV1Wxaor1fLoj2wl3ZXP/zboGJld9eZMZX6EL 7Xz00UX0IK0gEcn1rDfcM8ORfHUJNBuJJO+i3G6dwB0F28fJoDa4oAXxZ1TzHs7PiV9Si3wnBb2 M3VJmBA== X-Google-Smtp-Source: AGHT+IHDdAZrVVVtQkbNY+ODhRentmxWbP6Rfzn5FkbIRQBeRGy86k9mMoUCty77jPdE55ldMhNkDkbK1cVV X-Received: from mizhang-super.c.googlers.com ([35.247.89.60]) (user=mizhang job=sendgmr) by 2002:a05:690c:4b07:b0:663:ddc1:eab8 with SMTP id 00721157ae682-6eee0a26d5emr29857b3.4.1732215209587; Thu, 21 Nov 2024 10:53:29 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:52:59 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-8-mizhang@google.com> Subject: [RFC PATCH 07/22] KVM: nSVM: Nested #VMEXIT may transition from HALTED to RUNNABLE From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang From: Jim Mattson When a halted vCPU is awakened by a nested event, it might have been the target of a previous KVM_HC_KICK_CPU hypercall, in which case pv_unhalted would be set. This flag should be cleared before the next HLT instruction, as kvm_vcpu_has_events() would otherwise return true and prevent the vCPU from entering the halt state. Use kvm_vcpu_make_runnable() to ensure consistent handling of the HALTED to RUNNABLE state transition. Fixes: 38c0b192bd6d ("KVM: SVM: leave halted state on vmexit") Signed-off-by: Jim Mattson --- arch/x86/kvm/svm/nested.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index cf84103ce38b9..49e6cdfeac4da 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -994,7 +994,7 @@ int nested_svm_vmexit(struct vcpu_svm *svm) kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); /* in case we halted in L2 */ - svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE; + kvm_vcpu_make_runnable(vcpu); /* Give the current vmcb to the guest */ From patchwork Thu Nov 21 18:53:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882262 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4774B1D95B3 for ; Thu, 21 Nov 2024 18:53:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215214; cv=none; b=ZtRSygUu7rkWVGo5NH23wfKIiqOuI8cM16ySUKIt21e37J6eVy9gSseEiWo3gRythna0gvQbswuAZhnmw+TWpg+HW8PyyPAZLIMRorknwP8gqfONtOzqpoZEfnMSJNRhqXa6N6i2+AyZop4rDAmWBj8ZQi3xpH/otT1zapjDqdU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215214; c=relaxed/simple; bh=AdrfMvymD5zuRqTBBGGoWNJ0DY8WVGxpVB1lhy6aBew=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Rj9dWUkcHKpEdyghyXGjR36eEQHyH1i3kzcsu8Cugfwau9J3eR4yyfyPmzcf2dURGBJKymdakoPMtDyO5XyF6gmOJ/ebutK7QVTfCEMEEibf39nsjn6W/XVnRtVCVzlj7MPI+XdXM7lRMkKK5u9kT7phuvs1pAciM1ZYd4qn1dQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=c6Wb3y92; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="c6Wb3y92" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6eea70c89cbso14721937b3.1 for ; Thu, 21 Nov 2024 10:53:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215211; x=1732820011; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=w7D09IdaThZ8r32cYXtPxjnC2nPk7QEMmJJMfReh37c=; b=c6Wb3y92jN9o1wPhx1H+KZtjUhOaBjvDEfYVw+FCK3DHZdtS6/wyXVHkn1aWAbJoQ2 R1pkXJaSF5eibHaUkPoqxUKHZG/cqM13vXaDgvodrGHxiywJKY/NSVA0YNQ6a+cOh1kX RgHGXskBmN0IqFYn0xFDxZVFplviQmfREwjmAweHplMDxCDWjnGsIpRubdCLnEeAeKZ+ ttEeYWv+dA0CRLP7DrOKbfN7TCvDdI0H7iEbGFMTO4yxqqyqcemDkD8P9Kzg3/9DNCwA T7J8MX5oaA2r5QoZjSE3Fy9nJsZ6oaCUtQP7EbQGheA8e1DCPGfnWzqsCdlQyMaCsoPj qgmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215211; x=1732820011; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=w7D09IdaThZ8r32cYXtPxjnC2nPk7QEMmJJMfReh37c=; b=uy3+lv4AKa/e+8x7F9Pu4c2vmYr+nCing39SkbG8pLXW38zr97Y9qr1Q265jEtX8vk szvKScGSQrzh8kAk/sXNIRNkKJe0wdM50ri2mDvqxdme7UK9llMgY77MHOp0aFHpNn4I rByBXDlkAfCE1wt861QmpRg9Kk61ANKGJfkr96CrAZkEMhgkADQHs4NF+c39oTAo5nuw h/a2hUjjpn886UbTDauLuRwXNE6ZWIByDIessdjUKJNhq3iyIHYXpHrCN3SpCh0iKTvR mgv/nny4OPfVL8JCaMb7Fn3jvzi9hwG1pG7nAAitFiL9UlyDJ09qNcRIJM/W9lA9gzPG w3Cg== X-Forwarded-Encrypted: i=1; AJvYcCW2mj1V1oeqhyf+1qdjn5WF0W8Esjc7/fDoVAVJgCd+6+kWmYrODqI9bCvoVX3fI8ProDw=@vger.kernel.org X-Gm-Message-State: AOJu0YzxqSRe84BVs6lupF0p89B3b5qyTcrskhGmarVVPFiC+FqV5FLS ZG7wZXgBn8sh3e5d0QmcPF62GGfhRD8QiLeSunj/ugzrHwI0e5aBLLgMf7VSio7oa5LiG2U/twc 0pc/RjQ== X-Google-Smtp-Source: AGHT+IFbxnZIDPmwz4a6QDx5WTc3XPCOUOGBb+6SCxkWJJgfTYLxgm8qVWZQaaUmH8BKA4bQqcQX6mWx6Kh8 X-Received: from mizhang-super.c.googlers.com ([34.105.13.176]) (user=mizhang job=sendgmr) by 2002:a81:a742:0:b0:6ee:a2b0:a803 with SMTP id 00721157ae682-6eecd2b215emr527187b3.1.1732215211264; Thu, 21 Nov 2024 10:53:31 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:53:00 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-9-mizhang@google.com> Subject: [RFC PATCH 08/22] KVM: nVMX: Nested VM-exit may transition from HALTED to RUNNABLE From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang From: Jim Mattson When a halted vCPU is awakened by a nested event, it might have been the target of a previous KVM_HC_KICK_CPU hypercall, in which case pv_unhalted would be set. This flag should be cleared before the next HLT instruction, as kvm_vcpu_has_events() would otherwise return true and prevent the vCPU from entering the halt state. Use kvm_vcpu_make_runnable() to ensure consistent handling of the HALTED to RUNNABLE state transition. Fixes: b6b8a1451fc4 ("KVM: nVMX: Rework interception of IRQs and NMIs") Signed-off-by: Jim Mattson --- arch/x86/kvm/vmx/nested.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 931a7361c30f2..202eacfd87036 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -5048,7 +5048,7 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason, vmx->nested.need_vmcs12_to_shadow_sync = true; /* in case we halted in L2 */ - vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; + kvm_vcpu_make_runnable(vcpu); if (likely(!vmx->fail)) { if (vm_exit_reason != -1) From patchwork Thu Nov 21 18:53:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882263 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BAE651DFDB9 for ; Thu, 21 Nov 2024 18:53:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215216; cv=none; b=KsWfIk5A2ZN6g6q+YBShZLGZMBTmVZS+AePXmjPNqqORe1zhG7xAiq4Kid8b5DQE0IQ+PXQWu/kFe3CIQrP6LM2/tEDNlSHYAzPWqgQso3aWYNwVT7PjkaGqGV4fCWxDSUE8orl75CWVvCEOTiAPrWNeE1y31V5iYZqoPgLW6QI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215216; c=relaxed/simple; bh=L6KbIt0/KvcQN/tGAQPm9b6wAl1wm8uAx55r4uF0rZ0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Dd0wMBXQu999xBhX/iweyZ4U3V48tYr2heIW9E0S95e8bcF1N0TdL5LkgWWvPQ8MbzUVgsEzg7Jo3+oL9M/dzVWtbmDMlBKppvawu/50zkCayhS//TT9sR1qEe6Gf16knelrSpsKrKby9Eo3rJOGY/sIQzb05+0bWWLjDezWKKM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=r3K4c32M; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="r3K4c32M" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-71e4fa3eaafso1329098b3a.1 for ; Thu, 21 Nov 2024 10:53:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215213; x=1732820013; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=75cN6vpBl6q0bt4xLEE0VfpR783T6++jzGxWUcLySKo=; b=r3K4c32MGrz6TbefEF5gp/p19YI32WDiHN1Fqrmkx1eBLI3kReN8jzdn3L3J6n0E4v JQtBE3AgCLXKU9/+WX8PXrGZWzPs8B49YSPLoKUT8HtYMCJQfshHpN/GMRbkECq+cosu zN8KAPW6Z7oDH7g5FhebYtJ51n0UkxlhwAXhEomExpprX0etKR1cGjHgVAwhgPYZZxiA 2ZNCkIE6fnDqnxbZhWR9Dz2UilFu4kZnJSlOvIHCbJOPH5Dhpj07sGLfMLTQVGh7KvLR OXTPfC+zjPsG9LjMkyFd5aQalcpRE/yhQSidxGFzmLOgfKEqqc17a+5bO21O2FvtdiXW oBwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215213; x=1732820013; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=75cN6vpBl6q0bt4xLEE0VfpR783T6++jzGxWUcLySKo=; b=bb0/jQbvJtnqT/7tcNULO15oS6bMHMJKMZpPgsrcJRaWZ0v2G+Mj2fX6m4r1GD7K48 A2IoriJEjNKGLcbshlvSf/zAxBADMXOYtaVHsYprhdr7hVZauviTBOYWnpjWT+sdazL9 d7QtqArfj+Fsp+AXSkOAwTrvUNLmWfgLY1GEEcpHG3eGDTOcHGCpAhKw/gwAsDExJYPb FvytQg2dyZrGpQyb6SRsaTZXcUtNDsc84Z6/mZ8aboaCakrlTub6TtrRVG1MIq8fZNKl exlLwPg14DpQSMmq8z9jGMSKdy1K4nZdwMgONQV/4FF7J8WANne/ge48Yu1fRbsc1KOI fGnQ== X-Forwarded-Encrypted: i=1; AJvYcCWnp0Xp+PLdhc1NJ7bfcEA3EmT1x3I1RpLIDMtQRT+1pS4Gb96gRa8SVHNAfLHk9U3iETU=@vger.kernel.org X-Gm-Message-State: AOJu0Yw/dDga+4F9djbNmq58z+0O9ctEdQs7kQXqHWqcAeOxnrM2BN1o o8DV27+gwULhfYNuPONtDJrkxNmX6wDZOZk4SjWHSLUk9X+ryrzBN5GrAB1uGnWuSJjIJaN7Jt/ x7h9fNg== X-Google-Smtp-Source: AGHT+IHP6COYTNf6/JtdvUV7mQ9ms/6tBN3oxJUtYpsMH+h3z77LegfOxWfKvjQWoDLBqbCVlZHxViw6Xo6D X-Received: from mizhang-super.c.googlers.com ([35.247.89.60]) (user=mizhang job=sendgmr) by 2002:a62:e811:0:b0:724:daf0:e060 with SMTP id d2e1a72fcca58-724df3c0f18mr40b3a.1.1732215212841; Thu, 21 Nov 2024 10:53:32 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:53:01 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-10-mizhang@google.com> Subject: [RFC PATCH 09/22] KVM: x86: Introduce KVM_X86_FEATURE_APERFMPERF From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang The APERFMPERF feature bit appears in CPUID.6H:ECX[0], but is exposed to Linux via X86_FEATURE_APERFMPERF in a Linux-defined feature word. To enable KVM's reverse CPUID functionality for this feature, define a KVM feature word matching the hardware CPUID leaf that contains the APERFMPERF bit. This patch only adds the feature definition - enabling and advertising the feature to guests will be handled in later patches. Signed-off-by: Mingwei Zhang Co-developed-by: Jim Mattson Signed-off-by: Jim Mattson --- arch/x86/kvm/reverse_cpuid.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/arch/x86/kvm/reverse_cpuid.h b/arch/x86/kvm/reverse_cpuid.h index 0d17d6b706396..d12e5d9ab2251 100644 --- a/arch/x86/kvm/reverse_cpuid.h +++ b/arch/x86/kvm/reverse_cpuid.h @@ -18,6 +18,7 @@ enum kvm_only_cpuid_leafs { CPUID_8000_0022_EAX, CPUID_7_2_EDX, CPUID_24_0_EBX, + CPUID_6_ECX, NR_KVM_CPU_CAPS, NKVMCAPINTS = NR_KVM_CPU_CAPS - NCAPINTS, @@ -42,6 +43,9 @@ enum kvm_only_cpuid_leafs { #define KVM_X86_FEATURE_SGX2 KVM_X86_FEATURE(CPUID_12_EAX, 1) #define KVM_X86_FEATURE_SGX_EDECCSSA KVM_X86_FEATURE(CPUID_12_EAX, 11) +/* Intel-defined sub-features, CPUID level 0x00000006 (ECX) */ +#define KVM_X86_FEATURE_APERFMPERF KVM_X86_FEATURE(CPUID_6_ECX, 0) + /* Intel-defined sub-features, CPUID level 0x00000007:1 (EDX) */ #define X86_FEATURE_AVX_VNNI_INT8 KVM_X86_FEATURE(CPUID_7_1_EDX, 4) #define X86_FEATURE_AVX_NE_CONVERT KVM_X86_FEATURE(CPUID_7_1_EDX, 5) @@ -98,6 +102,7 @@ static const struct cpuid_reg reverse_cpuid[] = { [CPUID_8000_0022_EAX] = {0x80000022, 0, CPUID_EAX}, [CPUID_7_2_EDX] = { 7, 2, CPUID_EDX}, [CPUID_24_0_EBX] = { 0x24, 0, CPUID_EBX}, + [CPUID_6_ECX] = { 6, 0, CPUID_ECX}, }; /* @@ -135,6 +140,7 @@ static __always_inline u32 __feature_translate(int x86_feature) KVM_X86_TRANSLATE_FEATURE(SGX_EDECCSSA); KVM_X86_TRANSLATE_FEATURE(CONSTANT_TSC); KVM_X86_TRANSLATE_FEATURE(PERFMON_V2); + KVM_X86_TRANSLATE_FEATURE(APERFMPERF); KVM_X86_TRANSLATE_FEATURE(RRSBA_CTRL); KVM_X86_TRANSLATE_FEATURE(BHI_CTRL); default: From patchwork Thu Nov 21 18:53:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882264 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9B7691E0080 for ; Thu, 21 Nov 2024 18:53:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215217; cv=none; b=oiPvmhmtVsF5EhCUGBI8lEHSaLybQehGJmrQG8UhFfZQ0qQvF0FHte4bYN+oKAJuw54Yj3FypTIqCcaHk0vlqbwaz2tisTMJTZzx9QHmCN3yC83Mf9wP6/5dRYR4qiYSZwJeFyBU73LldP+c8uPbi1bPGp68i6/SBH0DIbZJSoA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215217; c=relaxed/simple; bh=+gdgcWuh2O/6zKqknikKz3/xyPeXXjH9mYNuZaRDZ6U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SIahFH9KsAoHSnWGpF27cbgoOwT0n09U+3ooFXIqqVxVq6ovTOEBFC+z7kqHcePa/fmzJIKZ0v9DIqn8240nkVn5y1sfu0c84jO7K+pZuK4u1ui/5bZQtEQdWOYPmo20/h+B/TfS1LnnQZXiL/K2NaKTumB3XkURuvZHc+9KGTU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=eszvWBeW; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="eszvWBeW" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6ee6c2b65b9so21204627b3.0 for ; Thu, 21 Nov 2024 10:53:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215215; x=1732820015; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=QkbV9zrukS41fr5Vait1KqXFkmbxs1DpuaY1AHu1I4U=; b=eszvWBeW9u5rRdTypyScDpDEZTsZzFJGx68Js6hEBmg/WkgglnL5/STwBMJ3OjLTRo Eoj43QIbw2eMoU0VuDtMJ81dyqxj5QxSSo+CJesbqH/kUsMskNsn41z4LDQxoN0T53Lc apHyyk61gey1jhE1gpfDSfCm5VfBN5sjA8cizefrhRHAkpZz7jX8BNJpmcDNbN1mbzsi WsD+ILXhlG4IHXJQK2ogUA0rOrjgyaZDLaIhHEVZAUjnP5BbSU3CVCav03UZxDeCG34R 119x/Wd9ZQ4duW2C34jxb3sWdPHDC1CqxtCRrPeby2Pua+igQsI+GnaiPZoeFn2UJ6mI U5EQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215215; x=1732820015; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=QkbV9zrukS41fr5Vait1KqXFkmbxs1DpuaY1AHu1I4U=; b=jRklWNbxNZEAF4FrzIkC6pXaizy/jwHTKi9fppSYM5SMj8B3bDbO7cXpghp+EUxeJF m26QOoLrBQTvl59eDMGvoypLsgHQS1D58JfhP6KTIAlH/6CoCX2Q2n+V3lmS42mqZLg3 yC/uhYb53hSdJmDS2sEkOxlM61drTWJvG1/MFvrnSw4O5XTDrFQ0wBTkzcwqcZ/N3Ler titnrgexN9Q3U1HTeUeR7l8FkG5j9rvlurcjv6kkDluRmts8uCj6c5mBSjnwEKrxG5Bp uWAy5AX+K7612xFWg4uuS2+js+HljPlkS4jQCWKEaRap/XEzQ+Ks8Dae+m7BMErrzQKE CDwg== X-Forwarded-Encrypted: i=1; AJvYcCWgOBj710jo4YPpS+Z8bPE/3zz2sQPwg7JAMp95PWMk1Eqk0s8IS8jNNEl32HQ7nKCm9Tg=@vger.kernel.org X-Gm-Message-State: AOJu0YzJyTPg++QnCV2K4UdcYOCLDhwVSWweOJ6HudKW2Gbs/mBOVcgk BgaPrrWpBLP0Pih8h6h8ByADtc3ujIO46yozfueengOazb8+DkBoMWsh4PdEra+5QbYjB0J1t/x r2o+zrA== X-Google-Smtp-Source: AGHT+IGIRJlHME06OCdzxklPerNVnkfO0fBICJYLbM7Zc7eo9Fha71pf7btmtjGjjQ8kbPxByDae0pN7Tpyh X-Received: from mizhang-super.c.googlers.com ([35.247.89.60]) (user=mizhang job=sendgmr) by 2002:a05:690c:fce:b0:6ee:3b47:59a3 with SMTP id 00721157ae682-6eee08a86fdmr2087b3.2.1732215214711; Thu, 21 Nov 2024 10:53:34 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:53:02 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-11-mizhang@google.com> Subject: [RFC PATCH 10/22] KVM: x86: Make APERFMPERF a governed feature From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang From: Jim Mattson KVM must verify both host support and guest CPUID enumeration before enabling guest access to APERFMPERF. This verification is deferred until the implementation of guest APERFMPERF is complete. This declaration enables "guest_can_use(vcpu, X86_FEATURE_APERFMPERF)". Otherwise, that expression results in a compile-time error. Signed-off-by: Jim Mattson Reviewed-by: Mingwei Zhang Signed-off-by: Mingwei Zhang --- arch/x86/kvm/governed_features.h | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/x86/kvm/governed_features.h b/arch/x86/kvm/governed_features.h index ad463b1ed4e4a..fa77d655d2355 100644 --- a/arch/x86/kvm/governed_features.h +++ b/arch/x86/kvm/governed_features.h @@ -17,6 +17,7 @@ KVM_GOVERNED_X86_FEATURE(PFTHRESHOLD) KVM_GOVERNED_X86_FEATURE(VGIF) KVM_GOVERNED_X86_FEATURE(VNMI) KVM_GOVERNED_X86_FEATURE(LAM) +KVM_GOVERNED_X86_FEATURE(APERFMPERF) #undef KVM_GOVERNED_X86_FEATURE #undef KVM_GOVERNED_FEATURE From patchwork Thu Nov 21 18:53:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882265 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 847771E04A9 for ; Thu, 21 Nov 2024 18:53:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215219; cv=none; b=WbobbjfHs9Q2gVShe4CVjEgMp4QKNiEYmmOanfRkIeQD2WWEVnG8WY0ZpkGR+DLUySKkDJeZFQ09EHDeGNPWnDOEzaJxc3EzTHS3slzXzFRQZfXbvzjTxk8kUabf9K23/Z0aAQBMiXjcGnpT7vPY3GofSqKrwYcvzHoXQp0k9lg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215219; c=relaxed/simple; bh=VnUEvIgOY10j/aE/ufecNYMq79Fqq+mS5k1WDTSCKFE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SybPHkQEGsAbZn4UZycrctXlOE0090mDkYrt0aj3fdSnY2Nsn7+PxvRaXr/sz3wHy1dADhzLGlMLy9SHFLE/ahhUnxmbz2VZar0g7dWEU+B95TT6wb9bCcxIjoKnkNeH40ngWrX5+tgiZZX2sxP/IHyFWz//HvHelY9+11DfZkY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ACTzjQHK; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ACTzjQHK" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6ee7ccc0283so20404907b3.2 for ; Thu, 21 Nov 2024 10:53:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215216; x=1732820016; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=AFRU4BWr8SpweEeBBix0zZoy9QkEncA+tQMcSsG2aPs=; b=ACTzjQHKDGL++AUqRLNBtmYDz9y5TfwonJSnUUFsHXdKIpFi9gQZcxXoLNXn9XArv/ W0YjSc5Nvnt+47E2nFUv4m6Cqe6b/F1HxgU92TBSpI9ZhUx1bO4P1SR2Hdm2QQOj177i svIn41ihyk/eKtx8n9Ue51nU/5Ic18OiFu5XeWom2G1RJ/8FruQVB4RezAa6G6Wp25Xe cb8fJDfUJNP2nmojZdA61CbQ0mfn6z3VispEUXTw826BcO13KTQRANR4jlR4V7qJZWJ2 mwj8C+mlZKRxCqG07sc/PhuaHKXyPSPTeP/ocs8kzrjZLt6a0P5Q4T0g1qEphwvxrTM1 b5UA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215216; x=1732820016; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=AFRU4BWr8SpweEeBBix0zZoy9QkEncA+tQMcSsG2aPs=; b=LQPiN5dxjJMgLR/FDZHYHHkkVZExVRjpUidj+Zqi6KJyHXOg3ow0Q5eIcNSS7+RMEN Nt+usnOV8Lt6IVFVpxTjX1v41w6v27Eo9AOJMGjZ/I2pI9UaBWNgY2FNNkh43iKvs7YR TPaeteVr4fAD/oZRD8FW3nJMqGCxMx3T6PkEmSoa7yk5iXhKxrrc8AMroYX7k7Cj9g0o cqP1sqeYE0rjG26hP/qDFvsJ8gyydX4n8foV/LANvnAtlXgokjp4dsGCWE8WvmRVtahw DeHRXOboSWb1mRST8KbgUf24YZDoAZ9o/SUypFwq7KL48ZLrPdqOR+YK8BPpN3f9axG+ ZNjg== X-Forwarded-Encrypted: i=1; AJvYcCVVBB6Mg9xRvYZu9n7f+KacJsv89F83a7o4+LTUNeEn0+32C4FPz/ApWLALv+1X7VxIQJk=@vger.kernel.org X-Gm-Message-State: AOJu0Ywn+g1CKPknxHcZcc+BHvWv+g70m1DElQksax3owY0kK2Rl/1ZZ pAqYucHSilKpDL5A+5+nPqwHuRv+tYpacpIQQKlzNYPqdWr239e2PRNQfHekMAL+QSLJb8pnemu Y/8Be+A== X-Google-Smtp-Source: AGHT+IG5NTyhfTOvOMtguS2XrmwrQUHPkXMhoswbbltJbsHj7nG8Hf9HsNhFgPZx0QThUafRc4jHGvqlK4Je X-Received: from mizhang-super.c.googlers.com ([34.105.13.176]) (user=mizhang job=sendgmr) by 2002:a5b:f51:0:b0:e28:e97f:538d with SMTP id 3f1490d57ef6-e38cb5d9aa0mr3198276.6.1732215216510; Thu, 21 Nov 2024 10:53:36 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:53:03 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-12-mizhang@google.com> Subject: [RFC PATCH 11/22] KVM: x86: Initialize guest [am]perf at vcpu power-on From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang From: Jim Mattson The guest's IA32_APERF and IA32_MPERF MSRs start at zero. However, IA32_MPERF should be incremented whenever the vCPU is in C0, just as the host's IA32_MPERF MSR is incremented by hardware. Record the host TSC at vcpu_reset() to start tracking time spent in C0. Later patches will add the host TSC delta to the guest's stored IA32_MPERF value at appropriate points. Signed-off-by: Jim Mattson --- arch/x86/include/asm/kvm_host.h | 9 +++++++++ arch/x86/kvm/x86.c | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 04ef56d10cbb1..067e6ec7f7e9c 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -738,6 +738,13 @@ struct kvm_queued_exception { bool has_payload; }; +struct kvm_vcpu_aperfmperf { + u64 guest_aperf; + u64 guest_mperf; + u64 host_tsc; + bool loaded_while_running; +}; + struct kvm_vcpu_arch { /* * rip and regs accesses must go through @@ -1040,6 +1047,8 @@ struct kvm_vcpu_arch { #if IS_ENABLED(CONFIG_HYPERV) hpa_t hv_root_tdp; #endif + + struct kvm_vcpu_aperfmperf aperfmperf; }; struct kvm_lpage_info { diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 3c6b0ca91e5f5..d66cccff13347 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12476,6 +12476,13 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) __kvm_set_xcr(vcpu, 0, XFEATURE_MASK_FP); __kvm_set_msr(vcpu, MSR_IA32_XSS, 0, true); + + /* + * IA32_MPERF should start running now. Record the host TSC + * so that we can add the host TSC delta the next time that + * we load the guest [am]perf values into the hardware MSRs. + */ + vcpu->arch.aperfmperf.host_tsc = rdtsc(); } /* All GPRs except RDX (handled below) are zeroed on RESET/INIT. */ From patchwork Thu Nov 21 18:53:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882266 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B00DA1E0B9C for ; Thu, 21 Nov 2024 18:53:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215221; cv=none; b=qMAArbT1uVcWNW1/tDy5sPrLZM3w3ADV3aYcvo6+b22x6Uxt4p3ItEztr1LjvR2pOgVxHiHRGiizeHMlDTKSQPMC+DgGqiKtyPmzqOcjVG3QGkXLXh2Tx+qDQB+3ycKRFUi0V6ZR9TaHSHCcl3yHUu+O5tGaMhQHPbeUOiNOcoI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215221; c=relaxed/simple; bh=J443xY11+FlsZ1O8RFSn4sEVsfffWxAMll4hKwD4QB4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ftlCS6cZZHH/hwSfnH1BDhpD04Brur8jDaMEIgauPxxplHwsn9LZl1ChJVgMYyBs9mkFG3OcQmKV5Y2JjV+w3xTqDq0lCJ/FhX5GpslbsQ+/pwYvdT55kwo1ccadtxVg4815fZt5oFApJHRqR4Hbyk9/yQoFJyYyvVibKZX1KP8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=iulUX1QA; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="iulUX1QA" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-720397dcc7cso1370779b3a.0 for ; Thu, 21 Nov 2024 10:53:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215219; x=1732820019; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=Zf8fZX+l+VoLEVD4pNXnS0cuADuy19dfjl6m0H+bXqc=; b=iulUX1QAMitX/+jSG7NTRLVmmstFGc9S3316onneVgEP3o2ky+RLaubbFAwI5YuF99 pzzIuOpS4a7CU8YORBBJk5K8V33wOgsvhmDz7Oi0yapJl40bXYVWAhop83ZKm/2/wnQs SbQSmekbTyBNge6FRpJuuFFY0LrTmjjSfgX2khX7Cyu07Hdurr6kCps+SgUU0t+0XCTs lAVET9aMiYtuGVb/pDbhV8Igxi46+FmA1LXu2mQeUlXwDD/CO4hQc6wiP31QZLPo2jNR bceTLjHcBLaenZEOhhh44hDyF5k8hZopkLy2Q3+odumVLfY8YKP8G0TB7U4XrsxoiIgK LKOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215219; x=1732820019; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Zf8fZX+l+VoLEVD4pNXnS0cuADuy19dfjl6m0H+bXqc=; b=DJ7NskZnGIPgxkqvzvoh2AkR8z3b2sMNxW8LKy2gR5OMmcZhLwoWDAiEUfeqAn8hqV wXI56g4igRVhIl0QrbL0dytCR0RsXyHSl6hS2FyZtxvUZ5jiwxaR0+/CDnwm563VyD8Q wO+hhQzXo+mOJ6A6L/WCA8kLHCU9gFLzXIxvTh9wLopPd4U9MbJVoasx63aKGBZd5fAO TTd33uKLZvoConvYzrXGADT3TfJ2a+pOdC0RkMeTXFnNwiC7gKcJ+QbBAT2RDJR5Z29p 5ib61IZNdlcBc4biSfcaVqW+tdmF09cezXg9DB0k8QHOLHFwLeOjeEsB95ZQRJ2by6LI raYg== X-Forwarded-Encrypted: i=1; AJvYcCVxopJu8CsjOxRMeWWTTEehoZvFeHOdUbes+Ov1a1vkg3EMEiapSPqDqVqU/HmM7zRo9uw=@vger.kernel.org X-Gm-Message-State: AOJu0Yx3Af7X7VJsebJqyNIFeGtf30TBX/t4znh/8J3x43rJG3LrUDTg Lx2ZUbmmgNynD1xBStkNbH/9Szeajwqk6HWC1HOB0XScOtjFJdVGhU+WCUZwn8Jjrn5ySqdwjod K+P3amA== X-Google-Smtp-Source: AGHT+IGYnMzeEjpt0TlemIbCUyvIDYTH0IyT6CjbExrl/UtCJsQ1yD4xmFeiL/4fw9BWaodK87t0RCsIAwvu X-Received: from mizhang-super.c.googlers.com ([35.247.89.60]) (user=mizhang job=sendgmr) by 2002:a17:90b:3694:b0:2ea:8548:3818 with SMTP id 98e67ed59e1d1-2eaca7fa74fmr3986a91.7.1732215218148; Thu, 21 Nov 2024 10:53:38 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:53:04 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-13-mizhang@google.com> Subject: [RFC PATCH 12/22] KVM: x86: Load guest [am]perf into hardware MSRs at vcpu_load() From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang For vCPUs with APERFMPERF and in KVM_RUN, load the guest IA32_APERF and IA32_MPERF values into the hardware MSRs when loading the vCPU, but only if the vCPU is not halted. For running vCPUs, first add in any "background" C0 cycles accumulated since the last checkpoint. Note that for host TSC measurements of background C0 cycles, we assume IA32_MPERF increments at the same frequency as TSC. While this is true for all known processors with these MSRs, it is not architecturally guaranteed. Signed-off-by: Mingwei Zhang Co-developed-by: Jim Mattson Signed-off-by: Jim Mattson --- arch/x86/kvm/x86.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index d66cccff13347..b914578718d9c 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1918,6 +1918,22 @@ static int kvm_set_msr_ignored_check(struct kvm_vcpu *vcpu, _kvm_set_msr); } +/* + * Add elapsed TSC ticks to guest IA32_MPERF while vCPU is in C0 but + * not running. Uses TSC instead of host MPERF to include time when + * physical CPU is in lower C-states, as guest MPERF should count + * whenever vCPU is in C0. Assumes TSC and MPERF frequencies match. + */ +static void kvm_accumulate_background_guest_mperf(struct kvm_vcpu *vcpu) +{ + u64 now = rdtsc(); + s64 tsc_delta = now - vcpu->arch.aperfmperf.host_tsc; + + if (tsc_delta > 0) + vcpu->arch.aperfmperf.guest_mperf += tsc_delta; + vcpu->arch.aperfmperf.host_tsc = now; +} + /* * Read the MSR specified by @index into @data. Select MSR specific fault * checks are bypassed if @host_initiated is %true. @@ -4980,6 +4996,19 @@ static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu) return kvm_arch_has_noncoherent_dma(vcpu->kvm); } +static void kvm_load_guest_aperfmperf(struct kvm_vcpu *vcpu, bool update_mperf) +{ + unsigned long flags; + + local_irq_save(flags); + if (update_mperf) + kvm_accumulate_background_guest_mperf(vcpu); + set_guest_aperf(vcpu->arch.aperfmperf.guest_aperf); + set_guest_mperf(vcpu->arch.aperfmperf.guest_mperf); + vcpu->arch.aperfmperf.loaded_while_running = true; + local_irq_restore(flags); +} + void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) { struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); @@ -5039,6 +5068,12 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) vcpu->cpu = cpu; } + if (vcpu->wants_to_run && + guest_can_use(vcpu, X86_FEATURE_APERFMPERF) && + (vcpu->scheduled_out ? vcpu->arch.aperfmperf.loaded_while_running : + (vcpu->arch.mp_state != KVM_MP_STATE_HALTED))) + kvm_load_guest_aperfmperf(vcpu, true); + kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu); } From patchwork Thu Nov 21 18:53:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882267 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6C40C1E0DD4 for ; Thu, 21 Nov 2024 18:53:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215223; cv=none; b=I1ezD/u7mnjqR0uAsDm78qELqmVisf99d97z5QOcs3oQ+r1UnN7RvgfMog2lSU9BrL+8Qlg5bwRK8IE7a8RQvvrWewu9waDXbDG4GGom5phCm11++dtCTVa93KwtFeGCCNvLvZ+t5Op2xuk7PG7nv+6tgFKNV+VwtTvlTkHYMfc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215223; c=relaxed/simple; bh=Ia47x5eqWq7m7XvirgYoICFMCzP4YkrxFKv1nGHdVnI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Sbg/Yvc8PGfBROYb0Y2jeb5QVxbJX7L0JU/RVNKoEdy88sOR9wNsBzRYfHiAQHnF+LucnH8mjuo6xcx1f0kRR2uPYxdDiMZVfoNJ9/JtfPhD/FAGfKbfyEICXH2BqLxTfOEtikc7Rz9MsFN1fO+Amdv52GnzwQc39ys3HzgnpHY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=jxqjqYcC; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="jxqjqYcC" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-720738f8040so999404b3a.2 for ; Thu, 21 Nov 2024 10:53:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215221; x=1732820021; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=czlM8Zx0msme5nVYY+cCt41KVQO5hp+W87Vv3EaE5IQ=; b=jxqjqYcCi+rUa+jljXZFhrQdsgiFX7otgcaea5xbORgCs6gWKLMtNhnNcr6HQlkAM/ noAegI58v52BYubQxXYqmu9Ck9AfQltrx6zLO0GXQCpYZWAiUnLiVD+I0tLC9+Ggo8+/ dJ4iPIkhC2WnsWgs+WEAiCwYJnjLF8hQ6HYViXEfpD67xp/elLpmW4spu3Td+aCy9UCH 9I8ZKExSwpxtbDwtECU9rJmwnTrwvpsYO8ROvSclTlToS5ugWUoVlkdT4QTi4LGoe01u YfOUILvHKa+CsSQF7yAOhEOTzg882/yDZyF3GCw5y82qpSgs/G7ZrDWgAkZGoZEGgNai KqHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215221; x=1732820021; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=czlM8Zx0msme5nVYY+cCt41KVQO5hp+W87Vv3EaE5IQ=; b=HlaQhS1sQ+8+1Su2aaNkIfoiechzDDQOksZ9u6ia+bz0ID0AJ/5+jFAH9l4BSNt5NZ 3Vl9Y7z0Lrq/VmNwWvY7wyJo9rOhE7Jueqw4xXiK09WqA7WphJSPS0Bns7cBtry2wX+6 4zoNIGPXyit34Y+3y1OWo2mKER3KgtwlU9Z509wF/ekvdXQKCapguyfIa7+nsPZEEXqh TEcOK2vO0sKob7vgHlMiA6PfRWvwxa8GiZuIqrPkJGV20fF+EKmWhRxA3Qo01eGEAyPm 3wuhpTSYRtsrNz0eWPeK/tBv2ys+ophk6CaU2VraOMyibnoCqgLLviiYY9wtE0Nlecvf /g9A== X-Forwarded-Encrypted: i=1; AJvYcCU6bQlJJrCnVdovSWkFa4Rt4v7YkHhSjHWE5VT20xot6tF6fzetcD1f+cajGGkBYxu4mEc=@vger.kernel.org X-Gm-Message-State: AOJu0YxwoFZtXnsqiAzv9vi9/TmgUKPI7aHTveRpCyPSTasWOFlslD9c dIIJtt8ayKm4ncJ6K5q6sqORR7jyBr7TaxppQM84G7zVFBZt961bYT6rbxxsAt78Mn276IAAGE8 L+mrwOg== X-Google-Smtp-Source: AGHT+IHuvV0WxRsObLmAMkZqtAMSt8mnH4jrgbXi7VVFR03pnbqw7dTGWT6Ax0Ujfs7YRaBjuYhmpaT6B+nG X-Received: from mizhang-super.c.googlers.com ([35.247.89.60]) (user=mizhang job=sendgmr) by 2002:a62:e40a:0:b0:720:2e1a:de with SMTP id d2e1a72fcca58-724df5d10f9mr87b3a.1.1732215220806; Thu, 21 Nov 2024 10:53:40 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:53:05 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-14-mizhang@google.com> Subject: [RFC PATCH 13/22] KVM: x86: Load guest [am]perf when leaving halt state From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang From: Jim Mattson When a vCPU transitions from HALTED to RUNNABLE, it resumes time keeping: its virtual IA32_MPERF MSR should start accumulating C0 cycles again. Load the guest values into the hardware MSRs for direct guest access. Background cycle accumulation is unnecessary at this point since the vCPU has been in C1, so the guest's IA32_MPERF has been stopped. Signed-off-by: Jim Mattson --- arch/x86/kvm/x86.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b914578718d9c..acfa9ecc5bc36 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11204,9 +11204,16 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) void kvm_vcpu_make_runnable(struct kvm_vcpu *vcpu) { - if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED || - vcpu->arch.mp_state == KVM_MP_STATE_AP_RESET_HOLD) + switch (vcpu->arch.mp_state) { + case KVM_MP_STATE_HALTED: + if (guest_can_use(vcpu, X86_FEATURE_APERFMPERF) && + vcpu->wants_to_run) + kvm_load_guest_aperfmperf(vcpu, false); + fallthrough; + case KVM_MP_STATE_AP_RESET_HOLD: vcpu->arch.pv.pv_unhalted = false; + break; + } vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; vcpu->arch.apf.halted = false; } From patchwork Thu Nov 21 18:53:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882268 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6616D1E1325 for ; Thu, 21 Nov 2024 18:53:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215225; cv=none; b=JFl/0JZ5nl6LF8LA54y9uYl2+lP4M6ViDq7fCq5SyAIImc6M3DIq9HDrxaV9QOy2vamRcJttazfrkd85RcPHnMSCG5IQtr54lCoWQ0CZamsjCaQ9xYvBmJy22alA/TDfrx+mu0OzaIo8w94xJA+iyu0AKKXNs3kEz/M01TCpmKQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215225; c=relaxed/simple; bh=dpvmC6fUg1oYhPj0KX7fGNscwICe9zRsrr6N3ofRupc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rRf3mN81Cwru6y3WAAxAHqMlYxStP7KhpEvLrX5pweoRB8ws7pwwe1aRSCNtyVil4K8uYwgD1eV+vamXOSQV+N4iX8xDM+nF5rWEbHqTjwtZ/dPkG8cAz1zG4bUKqaUP++9Og73MvsfvVVjctBRkEQA70ifQ8Zdb27MUi/MBmLM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=U81UXU5U; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="U81UXU5U" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6ee7ccc0286so20553407b3.2 for ; Thu, 21 Nov 2024 10:53:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215222; x=1732820022; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=PTZwTdWNiJi5zJc4U346f9dQ01Nf48/ag1/NR2uEtTY=; b=U81UXU5U93yVsVK8sO+tVnlyAYBKwLw0nHgeFyomsvzKhRJWKqXPsJhwJDXZZRM0ir ZlcQiXYSEBqYwAqrMRgCvghTfTafXu8zLr99pOzjhzaCpdP2pVzRLzTh0qXuHXK7G+lg +7sIiIR/rS69XQ78TQQpjzztRwR6dWLlKO6mDFs2z5wPtGR1m+WMsneLu3tUeUCdJTKv U2AmY5IAeASvhaPYNpz3r+Sj7c0e8EE/54Gg+VGqxAWloBYoCmtFMG48zJM8w/iY9fWq arDxZEUTs1KNiI9J5ixPO+Y2UTbj8R9ZwIxzUBxZ02eHRC56cMqdYSn/7du4+VSPFvBd R2FQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215222; x=1732820022; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=PTZwTdWNiJi5zJc4U346f9dQ01Nf48/ag1/NR2uEtTY=; b=MTXW/1wu6/bGodHnMxYALUE3ApuFh69Y8kF7SFuVY2A1o6w7ID7KTyI8/XFqRlgxNv bpeIM8vruhx/rVfv2YsgDFsHHu7jpKrPMlLvENhTicM+nx2uydUYDM207KdA6EVGK5Mi pnT3H2aUBuxxykPGza8oyvgildhOy5RAehHEDYb7bW7tlwK/aQrrJzrdiY80Fhp2734z fFOAOQandInB4GZprjmPTtFKEjF8/LfdoxSAuwcbXVVkCWRuHVusgfIQpNtMZAzyDHh1 FLP5qJe6yWxqYMVchSlsUT6tzHGeZkrp9g3BIa57Xk40VTBbmc25zS9nReOZYXR9Mvvr Jntw== X-Forwarded-Encrypted: i=1; AJvYcCUZ+AzuvRJWukCPvT6jaZSAhsNMDmkLTfiFbVlqY8+QTKx2MCoiKbJjZKq302R8kV5EwJ0=@vger.kernel.org X-Gm-Message-State: AOJu0YyD30OjqWR4pzkXre9m6isOH0QvOvlAXAz+Y6xJ6KR3f2xj741p gd270PbkXse0GBK3C+OYMx9PHgY+76whrIARQ0gk6EufLwOtU0A0FOWCPau5o7UsEWPb4piC4l7 xOnByhw== X-Google-Smtp-Source: AGHT+IHARvlONItudnx3LP0qpCsMs2eWD55KOxdOXi+6vEQansb9DlnkhO2mSfcmALCI/jGewWhmjCqnweLA X-Received: from mizhang-super.c.googlers.com ([35.247.89.60]) (user=mizhang job=sendgmr) by 2002:a0d:dc04:0:b0:6ee:8453:f427 with SMTP id 00721157ae682-6eee0a5e9dbmr1207b3.7.1732215222397; Thu, 21 Nov 2024 10:53:42 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:53:06 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-15-mizhang@google.com> Subject: [RFC PATCH 14/22] KVM: x86: Introduce kvm_user_return_notifier_register() From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang From: Jim Mattson Factor out user return notifier registration logic from kvm_set_user_return_msr() into a separate helper function to prepare for the registration of KVM's user return notifier from kvm_load_guest_aperfmperf(). No functional change intended. Signed-off-by: Jim Mattson --- arch/x86/kvm/x86.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index acfa9ecc5bc36..6df8f21b83eb1 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -637,6 +637,15 @@ static void kvm_user_return_msr_cpu_online(void) } } +static void kvm_user_return_notifier_register(struct kvm_user_return_msrs *msrs) +{ + if (!msrs->registered) { + msrs->urn.on_user_return = kvm_on_user_return; + user_return_notifier_register(&msrs->urn); + msrs->registered = true; + } +} + int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) { struct kvm_user_return_msrs *msrs = this_cpu_ptr(user_return_msrs); @@ -650,11 +659,7 @@ int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) return 1; msrs->values[slot].curr = value; - if (!msrs->registered) { - msrs->urn.on_user_return = kvm_on_user_return; - user_return_notifier_register(&msrs->urn); - msrs->registered = true; - } + kvm_user_return_notifier_register(msrs); return 0; } EXPORT_SYMBOL_GPL(kvm_set_user_return_msr); From patchwork Thu Nov 21 18:53:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882269 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B2A481E0E0D for ; Thu, 21 Nov 2024 18:53:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215226; cv=none; b=aH/erC2brYVVvOZ355KjhjmxWyA2iT/yasEh5dzJ1fERmu8sReW4VxGr/OLd2g/8sJjbXpFdnn5yqMWnqDFKuKzkZkh3+BSEVOcEjKTYUzBYHWdMVCbqe1vINn4m82JSxl9TzkPXrNSy33tPwagqMUKiD9zsZrM3wAImRExcyF0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215226; c=relaxed/simple; bh=E9JI3Pd2om4q8RKGN7/yj9E3VkNXyoDn/zNLNHrykuQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TuUc1YAha589eBipo+wSOvxYGfFX1MUNgkUNe1rn9k32Z5tIl6p5BjfHeuwEFIGKfXKyOmrW3aUdJcPICk7wAzttgT/MIUZ2IvyZqZrfxYjS9ETwaMGshsfT73CEnuIfbFHcOhMrl0PJRVHYJ+l+IAmHlR5v7QhCrgDuNrbQ4PM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=oCmsRzS5; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="oCmsRzS5" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-72459f94af7so1370982b3a.2 for ; Thu, 21 Nov 2024 10:53:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215224; x=1732820024; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=SnSGQmraT80AADBF0uMReh/30q4TRUP6V5ySJUvAAIk=; b=oCmsRzS5dt2UE7gkJk1GB7BUosxs5Hyb/g1ZGI4fCoWSqXPokVndRZVLWWERDKa9ru +l2DR0IllkhPdrvH/xS08qHJZjIE68yE0jpT/j1zt2araBGF3O4mpteRVMkUWJBE68yU /aWkmvhUykH0AdcOjCnGJYLSyYU2vWAAtazmqVqr6NI4r8gCZev7LkmHupXuSZsWSH0P EopJtRgVuha2OgmFLGYwOfEfU0QWygQ7S02evEL+iMkRvLqFnwLrRORCRa1ZaYkZNlp1 pMzCWbcLq5d3LWGwu1sZDt0SfsQJQS9TqQFnUiMzZyw/taj3AOkqGxkhyN1bXADHqmF/ N8iQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215224; x=1732820024; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=SnSGQmraT80AADBF0uMReh/30q4TRUP6V5ySJUvAAIk=; b=aCVlZtsPlcAvvGuOWZonCACpCGEHwd4pBNMrnZNYaoLLQ4Jfvm5rz8NdSkpF2jdbJh /jCTbMZs+4awtcvI5y841y0MseV6+meOD3KaZBkXRSVQBdqBkbup0K0V2Jzyva1Na8he FFLtjAlCaj06z/8OVJkABkoFrb8MusM+k3SbE8wnGAcrtosIOL62X7s7FK7VYkmBCWS3 +2UX+JO+R8DcOfB5O+hF/NKAN5f18DeyO91z62NYYWa1pOvcgp8kwVPSgaLl1sxc1QKH xjXa33+CFPmz39Ix++Q31JuM1FsUfBtWqp7jeNqjUmxo5KEmYwotHG6xtQK7a+axaTnK AMbg== X-Forwarded-Encrypted: i=1; AJvYcCUKyyaA4xmdg6tWEQwLzeo83bstuslbXVlJqCjPmfLVmt6R4QbvJ/VW+aHiY6DaTs/ekrI=@vger.kernel.org X-Gm-Message-State: AOJu0YyM6bkJey/bePkftW/mNlMSwETEPrz52qXZCUJWPHJUDsE+tpfh DdNC2jUHYPSK3DA8hLOICainXcflmJ3fG4Y9fsJr4SnVQI6U961f+zvACzAHVURGo8qavd1EpPE dJk3wJA== X-Google-Smtp-Source: AGHT+IGyVXzczMNxhGjYz9B0YSQ0wD+pGCEGi8sb3RmGy5Whzw9pkoB0H3zOHOP/5JzV1UzbqMu+KUIB6p/E X-Received: from mizhang-super.c.googlers.com ([34.105.13.176]) (user=mizhang job=sendgmr) by 2002:a17:902:d4cf:b0:212:6061:251c with SMTP id d9443c01a7336-2129f2717c3mr535ad.7.1732215224004; Thu, 21 Nov 2024 10:53:44 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:53:07 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-16-mizhang@google.com> Subject: [RFC PATCH 15/22] KVM: x86: Restore host IA32_[AM]PERF on userspace return From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang From: Jim Mattson Add support for restoring host IA32_APERF and IA32_MPERF values when returning to userspace. While not strictly necessary since reads of /dev/cpu/*/msr now reconstruct host values, restoring the host values maintains cleaner system state. Leverage KVM's existing user return notifier infrastructure but add a separate flag since these MSRs require dynamic value restoration rather than static value restoration. Restoration is only performed when guest values have been loaded into the hardware MSRs. Signed-off-by: Jim Mattson --- arch/x86/kvm/x86.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 6df8f21b83eb1..ad5351673362c 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -207,6 +207,7 @@ module_param(mitigate_smt_rsb, bool, 0444); struct kvm_user_return_msrs { struct user_return_notifier urn; bool registered; + bool restore_aperfmperf; struct kvm_user_return_msr_values { u64 host; u64 curr; @@ -571,6 +572,11 @@ static void kvm_on_user_return(struct user_return_notifier *urn) * interrupted and executed through kvm_arch_disable_virtualization_cpu() */ local_irq_save(flags); + if (msrs->restore_aperfmperf) { + restore_host_aperf(); + restore_host_mperf(); + msrs->restore_aperfmperf = false; + } if (msrs->registered) { msrs->registered = false; user_return_notifier_unregister(urn); @@ -5003,6 +5009,7 @@ static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu) static void kvm_load_guest_aperfmperf(struct kvm_vcpu *vcpu, bool update_mperf) { + struct kvm_user_return_msrs *msrs; unsigned long flags; local_irq_save(flags); @@ -5011,6 +5018,9 @@ static void kvm_load_guest_aperfmperf(struct kvm_vcpu *vcpu, bool update_mperf) set_guest_aperf(vcpu->arch.aperfmperf.guest_aperf); set_guest_mperf(vcpu->arch.aperfmperf.guest_mperf); vcpu->arch.aperfmperf.loaded_while_running = true; + msrs = this_cpu_ptr(user_return_msrs); + kvm_user_return_notifier_register(msrs); + msrs->restore_aperfmperf = true; local_irq_restore(flags); } From patchwork Thu Nov 21 18:53:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882270 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 486311E1C3E for ; Thu, 21 Nov 2024 18:53:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215227; cv=none; b=iA8euQ8o6MOIJDBU4yma5Suu2rKtq9o7KPSaiyWpZvdlSgdofXoe+J3dY+AqmZp66uWDbF43uUSHI6m3O4Ovypv5ou91GRrBXHGlxJTxl8156lm7hVt/l5WSXo54TSeVyUzsY7RgYsBB4q61pP8ALqWtWUsBugensNkIo2rqMYA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215227; c=relaxed/simple; bh=kH3D5R4M6NKtNCZGtvdwg+QTSyMzUsIWYvW/jUpioyA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kBOOVQLoROUZ938TZbR4kYkSLq4notqnROlkWeFkODB3mJ/tv+qu6uMZFgIagJYkiHSCc6pbnSZQTMlPlm1brWndEsJSBjbdn0XQlQm2hGaGvDXFSF7QDNLuXKmpSMGcQ6qKG3zmHjj0uQceXSPW+WYhTW12cFqNq0E92GPEzB0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=yWzJ++4E; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="yWzJ++4E" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-71e55c9d23cso1017304b3a.0 for ; Thu, 21 Nov 2024 10:53:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215226; x=1732820026; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=/vtFvw8p5uLNhWI0pwE3zjisLWQE5QnAFBSwBwyPeu0=; b=yWzJ++4E5julIyUqjtSvVkEqy23uSVKQLRijgA2PaTuBdR10Y9ksAspY0DS63UmAi8 8F+G7iVPuwtzgxE7c3X91yIYXbYvgiTrbE4OQMbUQ8Y9VwrNkLnkLzk8EEjjuVUNF3j+ TB6MHuchXvqADl8aFGn/BpHOmVG3mumcHzML9OIgRWy7IaHvyuHogLpA+9yZl/koq3b6 mZ7B3/oTW28F/sQCofBh8FqrufOGVufTIb++TU/1F1HwpWzysz4UvFV/aYoPFUqt/3wF k01Udc7JsOKumRWflbJ30s3XoLgwS0A7aA6TBCusGw+3zYU5aG5hDRdx4zFQxusr7uEh FCgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215226; x=1732820026; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=/vtFvw8p5uLNhWI0pwE3zjisLWQE5QnAFBSwBwyPeu0=; b=t/JkVBY7ddSB0Y8kJizvBITkK2LdRJBJ0+ebvYacAnKyL1oape30WMlivsoOLg/CT4 xrnm92xmb8VleOWKFR8+WzbU63q/eSeTUXb8tuWEntXGiWMeoPHdFfmuY/o0et7bTGEk kKwgDUOxb867uPqZO86A8fbvppLn/hw4C0XbQuKvhNpjo+q7Uy0y9kFvmGWu721tqujo zktSTpKGofRTaTEWPzADr6eWU+ovfG7o0/sq0MGBvtSveUp48kjinMPvYkoKc/0TUXFX vuv6h4/Dk2371w3QS8VcTz9gLouRWIObUtGO7GlTgQU9MrO7WN+SnbRrbxzqmK/KR52o d9qA== X-Forwarded-Encrypted: i=1; AJvYcCUm6L2NkKRuxRI1tPqLP3ZBTouENUBlV7HwVk95Lm9iBz4Nvmp8eKWTRybggXo1JMGqlAY=@vger.kernel.org X-Gm-Message-State: AOJu0YwekPppicbkHWPy3Usaw4gcliY3kQaF19MEPEwwbYHP1GO2PqfZ ph2f7GFLRNz3vYDSbx28ucCF42uAWir4ykhTadjPT3Cr0njHEyVgNt27VDIsHOj2EGaugltxDfB 6KNJk1w== X-Google-Smtp-Source: AGHT+IFbWsmMQq438PKz0JqAFBdWxkozDwaN7GD30aXSE1hcn3GxW73iad3n1P+et4T/231tXqeFsMKVoHWU X-Received: from mizhang-super.c.googlers.com ([34.105.13.176]) (user=mizhang job=sendgmr) by 2002:a62:e20d:0:b0:724:d915:5836 with SMTP id d2e1a72fcca58-724d915588fmr1625b3a.0.1732215225677; Thu, 21 Nov 2024 10:53:45 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:53:08 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-17-mizhang@google.com> Subject: [RFC PATCH 16/22] KVM: x86: Save guest [am]perf checkpoint on HLT From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang When the guest executes HLT, the vCPU transitions from virtual C0 to C1 state. Its virtual IA32_APERF and IA32_MPERF MSRs should stop counting at this point, just as the host's MSRs stop when it enters C1. Save a checkpoint of the current hardware MSR values and host TSC. Later, if/when the vCPU becomes runnable again, we will start accumulating C0 cycles from this checkpoint. To avoid complications, also restore host MSR values at this time, Signed-off-by: Mingwei Zhang Co-developed-by: Jim Mattson Signed-off-by: Jim Mattson --- arch/x86/kvm/x86.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ad5351673362c..793f5d2afeb2b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -5139,6 +5139,21 @@ static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu) mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); } +static void kvm_put_guest_aperfmperf(struct kvm_vcpu *vcpu) +{ + unsigned long flags; + + local_irq_save(flags); + if (vcpu->arch.aperfmperf.loaded_while_running) { + rdmsrl(MSR_IA32_APERF, vcpu->arch.aperfmperf.guest_aperf); + rdmsrl(MSR_IA32_MPERF, vcpu->arch.aperfmperf.guest_mperf); + vcpu->arch.aperfmperf.host_tsc = rdtsc(); + if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) + vcpu->arch.aperfmperf.loaded_while_running = false; + } + local_irq_restore(flags); +} + void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) { int idx; @@ -11363,10 +11378,13 @@ static int __kvm_emulate_halt(struct kvm_vcpu *vcpu, int state, int reason) */ ++vcpu->stat.halt_exits; if (lapic_in_kernel(vcpu)) { - if (kvm_vcpu_has_events(vcpu)) + if (kvm_vcpu_has_events(vcpu)) { vcpu->arch.pv.pv_unhalted = false; - else + } else { vcpu->arch.mp_state = state; + if (guest_can_use(vcpu, X86_FEATURE_APERFMPERF)) + kvm_put_guest_aperfmperf(vcpu); + } return 1; } else { vcpu->run->exit_reason = reason; From patchwork Thu Nov 21 18:53:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882271 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 483D61E231F for ; Thu, 21 Nov 2024 18:53:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215230; cv=none; b=CQVr1IsJ6MFxHF/+Hbck36fOHmQgkG/tFG02eOzm/t8n3qhYZ4GaAji+eU0XcP8p1gJZX471AqvK0Y1tOJaa07AAewDV9NoM0zay3Lv0hmRqcCVizZmE8dEBBgp638GvY3Eo2EehiWNuol6XYtBRjl0VOf/stEVa//B8DwXm8j4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215230; c=relaxed/simple; bh=eHXZPCixs0Kp9lb7wTpd1eAC5lTE/wHduYYezTPcltI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FHQ5tWkNw32KA8yOQASNOjAMmI067o6dXTpuTMy7tyKodAYlhgZ7hDCeaWRDoBQpWBASXgoK9uTS2rAOy7gpkN4G6TOcOMzoJVWIPAZir48FtqRtJn4bKY3X3xBbdUf1KuHoz/8FRjQ5InstAjjaZOn+RElcUVvDSy4ZvslraKc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=2Q3vNv8L; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="2Q3vNv8L" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6eebd4e7cc5so14918377b3.0 for ; Thu, 21 Nov 2024 10:53:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215227; x=1732820027; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=S3ejUU/7aVBo5O4Q0RNsvCKZVgKY1XQah2zaS1IN8Y8=; b=2Q3vNv8LrR2WteR3PRdx5a0g0bxBUBMmsz/1ptNtGsXDSdQHBv8If8DdkSLHMCr8mK Ryy8n1eERwWnj6OGFjS7EuhJNu4Lu0eflomgCC62OJ4pNP9W9mmGSTrCG8qJ21UQ0m/Y XtT55v1s0frhm3EIGGYIDTD78wCmIWP4ukINQpSHy3okwpTGqBIaB0H1H2/fRL4EEj+Y XOE1UEIUFcaVlo7oZe+dF1sx+pXzgf3u5j/YTxG5LJ2r9upJTt6YmU/fXmMZAwWZmB7h OGvbPHgdyMQCFxnvi4BTPas6DrTmZ7rHKPG2Ag/wbrsscOjyBu1GDIaWbNPYyWFeml/p B7Hg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215227; x=1732820027; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=S3ejUU/7aVBo5O4Q0RNsvCKZVgKY1XQah2zaS1IN8Y8=; b=dcDFFZlc8rU8E/NSXsybrz7DWCSV8+QC49LRjeu4r3X0UJ10hJQR28e71we7nj85/k aFhwr2hm7x5NYZAnANG02jFyg9EoBiXKUJFj8EUTOKfHeIQOHn1Q0qmNw4W1jMNszn9M NeOtkQeKA1yVvkbhyEkFbyaCuzhapiQ0FuAUD6NHaXUK6phfBKH+rc6tzfinFdAjODDa OGyJKarSuXb7nZqF0kRN5Dhewn00ZbLZQo1DiXqRlT1gljqC38mFuI4WhNLXEZ0/RIbJ 56uGyJMyMFNdIstPa6s05cfWjqTBcqaTUrQEmnfegh+9FkHilsBo0ZAsxUJXXXkgipnr ifKA== X-Forwarded-Encrypted: i=1; AJvYcCV0NFWHWa5xqnPcDrobx+u3klf4CdDuv6tfwNnikN994FvG4//6/DD3TgZRhwiHZr5Qx28=@vger.kernel.org X-Gm-Message-State: AOJu0YwNa2XemoysVNu55wrFnjquA2oV9lsrp/lqqTpsh5moLhwVzho8 2araUOpDHKgTBQSdskpyAiHmVKXGS6fdURu58Zl05FhDUAIvn2sY5wCkF423BuHXKRXPyp92eav teg7AIA== X-Google-Smtp-Source: AGHT+IFnZhLdaAm9Iv4V2heWNMD+AOPfEaQHbFcWJ8d4iuBWsqxwVlNaJU/yAnomaTfOoVWSEqQuOYrN36SC X-Received: from mizhang-super.c.googlers.com ([35.247.89.60]) (user=mizhang job=sendgmr) by 2002:a81:a742:0:b0:6ee:a2b0:a803 with SMTP id 00721157ae682-6eecd2b215emr527207b3.1.1732215227332; Thu, 21 Nov 2024 10:53:47 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:53:09 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-18-mizhang@google.com> Subject: [RFC PATCH 17/22] KVM: x86: Save guest [am]perf checkpoint on vcpu_put() From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang For vCPUs with APERFMPERF that are in KVM_RUN and not halted, checkpoint the current hardware MSR values along with the host TSC when unloading the vCPU. While still in virtual C0 state, the vCPU will no longer run on this physical CPU, requiring different handling for each counter: - IA32_APERF should stop accumulating since no actual CPU cycles are being spent on behalf of the guest - IA32_MPERF should continue accumulating cycles since the guest is still in C0 state Later when the vCPU is reloaded, we'll use this checkpoint and the host TSC delta to properly account for any "background" cycles that should be reflected in the guest's IA32_MPERF value. Signed-off-by: Mingwei Zhang Co-developed-by: Jim Mattson Signed-off-by: Jim Mattson --- arch/x86/kvm/x86.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 793f5d2afeb2b..7c22bda3b1f7b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -5173,6 +5173,11 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) srcu_read_unlock(&vcpu->kvm->srcu, idx); } + if (vcpu->wants_to_run && + guest_can_use(vcpu, X86_FEATURE_APERFMPERF) && + vcpu->arch.aperfmperf.loaded_while_running) + kvm_put_guest_aperfmperf(vcpu); + kvm_x86_call(vcpu_put)(vcpu); vcpu->arch.last_host_tsc = rdtsc(); } From patchwork Thu Nov 21 18:53:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882272 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 954DA1E2607 for ; Thu, 21 Nov 2024 18:53:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215231; cv=none; b=FEc+LJHkQjoxX4DklfdGk+113fk4oTKLIUzLxRfe78rdolRkORCC1+APfAQ+a6wBy2SuRTGuT9XAXnXmYFFnd10/eWWmhAjEp8khf+ZHemRGw8oS7KD4D6ESRCrX5qAPLCsGZLN6QqD+3BCmmeQEalHrU+Tkx8ojwu4ufqcvCoI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215231; c=relaxed/simple; bh=qemrFZ+DR3sCl7VG4GKYwVD8L8FwjlVxsp3WJhJHwXU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=njUy9Mu9Gtven9p4pyYFKSXyh8GdxMg3jcfq96oVeZRtBtHwn88LtB+PJ8lvoGBxCg0z2GttihvlZ5sRjQEXGBixSE1gwAL5YmIVVUbT2E9DqoBpPtIF19RX6uovnElurmSMSGRUIccXRM/E/1R8l+HPvCvK9lafvNj2Jer2VfU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=uC6XDDIa; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="uC6XDDIa" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6eeab4be015so21619667b3.0 for ; Thu, 21 Nov 2024 10:53:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215229; x=1732820029; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=Sh4pMBblHn1VD2N9Y2lp4abB6oxAsUcAPAYEWiOVGrA=; b=uC6XDDIamI6lGnwNA6gWzfVX+QGACb4lytpRAYWk3pyND8yI3nGZ0+RV/UsoOxUdrT 1aP2aR3KJD4xok2SZLsolRHCoGqR0d0Ne8NavIDLJ0Q0GRsSkYeI/eL6OhT1BMWXBcW5 v7xi7ulDRvFQmZKC4qC7KQ5vx0aZaYDTG7qZApDlgJtOVrWkxRppfTmFQ3WEcnrp71Yv lyH77bY2Qt/006ClS1xB7SV/w51EmhXaMSv6+e3z2/oDFVd44d5uM6cIFQrWvJjcqJ9u YTzfHN/AQE6G/wLrmaolXadstknKakMTLYvZtQedyF+/EW+P8sGqF1rhUHap1QgeMpBw AqdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215229; x=1732820029; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Sh4pMBblHn1VD2N9Y2lp4abB6oxAsUcAPAYEWiOVGrA=; b=f5THv4/EeFHKwuBBy25gyVpvdlPOp3pm3kvP6/bsz0/HhB13r2OLbwKjut/Re+D1rw FI4l85rS7Gk6s1KMTJTh1fBjxlg2mReLkiZV/hN8QMUqv/s/mIY2DZOaJv9aIdQPiFjz XZaDSTTWxv6tRj2urromQOXEq+DA6A88/sL/iFsIB9BBIb3CN0WFEWekv3XfQI6Ggh6z ZEJIKWu0nRb+dWMbETOyTEsaeMUxc1E+/EqwnAdaay+aJiU7BjvEC9JniIOMgGJ75EJ0 9AXmIYXR11A0liRmR0J/FGcdqTHAftckXCVQLFMHqV/WwuhNbVsJfOoEa2LjsARg4imL G/Aw== X-Forwarded-Encrypted: i=1; AJvYcCWchxZEfJtfJyk+sAGazimho72vsQvrg+37G1mzNFmAxdjhQOTxmX6DNs21bcs6P5NHoKQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxuPFOPPjNuaKfDFPSQIAee2cnbBLdQVq5bbxop1m3PljCRiotL fT/mwnJBVPW73pVkrYu0KtjQvPsUuLWvOS81s+ubHaixG5DboCHFdZymwwwqR08oTef4RWmOJkm bZY1w1Q== X-Google-Smtp-Source: AGHT+IEJaYc97wodeOZjWOswXnwDACandNWSXkgUJpIhOpYXATvfyTkw2S5hqwBp9Sei4x4azZY6rZTmf+9J X-Received: from mizhang-super.c.googlers.com ([35.247.89.60]) (user=mizhang job=sendgmr) by 2002:a25:c581:0:b0:e30:d61e:b110 with SMTP id 3f1490d57ef6-e38cb5fbd2fmr9499276.5.1732215228789; Thu, 21 Nov 2024 10:53:48 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:53:10 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-19-mizhang@google.com> Subject: [RFC PATCH 18/22] KVM: x86: Update aperfmperf on host-initiated MP_STATE transitions From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang From: Jim Mattson When the host modifies a vCPU's MP_STATE after the vCPU has started running, maintain the accuracy of guest aperfmperf tracking: 1. For transitions from !HALTED to HALTED, add any accumulated "background" TSC ticks to the guest_mperf checkpoint before stopping the counter. 2. For transitions from HALTED to !HALTED, record the current TSC in host_tsc to begin accumulating background cycles in guest_mperf. This ensures the guest MPERF counter properly reflects time spent in C0 vs C1 states, even when state transitions are initiated by the host rather than the guest. Signed-off-by: Jim Mattson Reviewed-by: Mingwei Zhang Signed-off-by: Mingwei Zhang --- arch/x86/kvm/x86.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 7c22bda3b1f7b..cd1f1ae86f83f 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11904,6 +11904,18 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED)) goto out; + if (kvm_vcpu_has_run(vcpu) && + guest_can_use(vcpu, X86_FEATURE_APERFMPERF)) { + if (mp_state->mp_state == KVM_MP_STATE_HALTED && + vcpu->arch.mp_state != KVM_MP_STATE_HALTED) { + kvm_accumulate_background_guest_mperf(vcpu); + vcpu->arch.aperfmperf.loaded_while_running = false; + } else if (mp_state->mp_state != KVM_MP_STATE_HALTED && + vcpu->arch.mp_state == KVM_MP_STATE_HALTED) { + vcpu->arch.aperfmperf.host_tsc = rdtsc(); + } + } + if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) { vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events); From patchwork Thu Nov 21 18:53:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882273 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 841A11E2853 for ; Thu, 21 Nov 2024 18:53:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215233; cv=none; b=daSdsBcUM8kOEBurqIvq64sCrqy1MW00h5Bce7BsESQCS+B4dbVBT9J9uoiWRtM3ngBHjbxpxAWzAuGwoGaU2s7j32rp/JJKywn8FH6b/i1tsUFZHJBQYaL2Ijy753R31jpyO6i4T586WYw8OX1lGzP9Pn1ene6CD0OoUK4+k4M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215233; c=relaxed/simple; bh=cg2iyp4+CvEy683WYX+ECsqwzXyjhokH3kRioPhRAhA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bjVoQXn1m+w3C9h6yD+9+dHX3cAt1hD2CyJWzyiodM+wvJmz500P9sAasAu2iTxO7407skfqQa8PK0DTnAQ2cdZ53kvjtNd4GMip/4y33J01N1g8AsRFwVj8+OA4GN1wDNdWqpwt7EKnb574aKa83LaJdjwrISd9EberWNBes54= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=j9JoyGno; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="j9JoyGno" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e35e9d36da3so2130655276.1 for ; Thu, 21 Nov 2024 10:53:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215230; x=1732820030; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=umjqRaPxMOMBPCY2bbDeorL7OH9qU4LVOhrbwYAX8Gs=; b=j9JoyGnokUtEHz3lfzkhbspaGKCa+33sm0MVSbCDRr0sBQBSR+grVb6mYQDOGsT93f SOx9ViDC28yPoPUAQiNWP/NHAPKgOWxO9kCQ61n7uyeltW42CWGpdbTB+2bOK6r0O/54 O7d6PUgJflGfGf7l2FSbL5+nZ2rG2vHNOxgQCpy/HEKvs5KjDCvVpAouEf6pNEk36pdy NEkeyXWCmQ1WdZmV/YhWg674h4UEqJEFPEhAYFaZL9C67LLyFmAE3eqcjdbWvHDymwZ7 soYq20UQisXLuW4e2dHtz/MIf8wTdYNCV1pzB9enM8ecQOXxF0bxvXKUsI9iz0d2Hx+b iLbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215230; x=1732820030; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=umjqRaPxMOMBPCY2bbDeorL7OH9qU4LVOhrbwYAX8Gs=; b=B1D9BO6WMxeoICaYjkn7vKEUqd5cCEf5WckSYYkacLpGbq5r6INki72mOq3CAXuhzh JeAr/wGrJmoqCr9KTqvgSCIQkxduQxaIqohw8hoDO3gfYp8ecioi/PVx4H5qhrSCqvbs uSUk+jwZgF1++BkWoHT5xIrxbyyaSavtKZLs2QuUTNwe7vTdktYf4KeTcj/xKbjmpebd 0HtefDUqY+aELvdhR53fD1GDarhj9U2aJXaNxL2Xn+WzKDMI+9Wfu+G/8plaNSLl9h6Q xqGkMD5Ubc1ia827Pn2SJUVbMtewzDhh2l5PC2ljx05Bu+NzKTty8ygTZQaaHScjuyWX FyCQ== X-Forwarded-Encrypted: i=1; AJvYcCWqPPtk30jX5xsvDvo2mxd8r0U13IeGScvQ8jszaXJmK4rfdAf8pQp8ouycZzKAfN5SV7I=@vger.kernel.org X-Gm-Message-State: AOJu0YzH+5EyK/xI3VM3SEtaip154/W0UQWqZJOVGoqKot+oYLQiPWm3 mujVjT5xDAC/rxHTNxMmV38TFRZmR/Ymky4dgjwUGrSJayaScP+N2ZW2siajCiqW+VEbq2LyDSr YLDGsPQ== X-Google-Smtp-Source: AGHT+IG4i7fOAFePg97UFWHvr+E2Z6FO0kC5FD9Us3UIujj+wD5TBePM+4bKH0fL2aTTdEKp0RfkKSC8TCCi X-Received: from mizhang-super.c.googlers.com ([34.105.13.176]) (user=mizhang job=sendgmr) by 2002:a25:ba4c:0:b0:e38:10a7:808e with SMTP id 3f1490d57ef6-e38f889f2ddmr1276.0.1732215230445; Thu, 21 Nov 2024 10:53:50 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:53:11 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-20-mizhang@google.com> Subject: [RFC PATCH 19/22] KVM: x86: Allow host and guest access to IA32_[AM]PERF From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang Implement MSR read/write handlers for IA32_APERF and IA32_MPERF to support both host and guest access: - Host userspace access via KVM_[GS]ET_MSRS only reads/writes the snapshot values in vcpu->arch.aperfmperf - Guest writes update both the hardware MSRs (via set_guest_[am]perf) and the snapshots - For host-initiated writes of IA32_MPERF, record the current TSC to establish a new baseline for background cycle accumulation - Guest reads don't reach these handlers as they access the MSRs directly Add both MSRs to msrs_to_save_base[] to ensure they are properly serialized during vCPU state save/restore operations. Signed-off-by: Mingwei Zhang Co-developed-by: Jim Mattson Signed-off-by: Jim Mattson --- arch/x86/kvm/x86.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index cd1f1ae86f83f..4394ecb291401 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -334,6 +334,7 @@ static const u32 msrs_to_save_base[] = { MSR_IA32_UMWAIT_CONTROL, MSR_IA32_XFD, MSR_IA32_XFD_ERR, + MSR_IA32_APERF, MSR_IA32_MPERF, }; static const u32 msrs_to_save_pmu[] = { @@ -4151,6 +4152,26 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) return 1; vcpu->arch.msr_misc_features_enables = data; break; + case MSR_IA32_APERF: + if ((data || !msr_info->host_initiated) && + !guest_can_use(vcpu, X86_FEATURE_APERFMPERF)) + return 1; + + vcpu->arch.aperfmperf.guest_aperf = data; + if (unlikely(!msr_info->host_initiated)) + set_guest_aperf(data); + break; + case MSR_IA32_MPERF: + if ((data || !msr_info->host_initiated) && + !guest_can_use(vcpu, X86_FEATURE_APERFMPERF)) + return 1; + + vcpu->arch.aperfmperf.guest_mperf = data; + if (likely(msr_info->host_initiated)) + vcpu->arch.aperfmperf.host_tsc = rdtsc(); + else + set_guest_mperf(data); + break; #ifdef CONFIG_X86_64 case MSR_IA32_XFD: if (!msr_info->host_initiated && @@ -4524,6 +4545,22 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) msr_info->data = vcpu->arch.guest_fpu.xfd_err; break; #endif + case MSR_IA32_APERF: + /* Guest read access should never reach here. */ + if (!msr_info->host_initiated) + return 1; + + msr_info->data = vcpu->arch.aperfmperf.guest_aperf; + break; + case MSR_IA32_MPERF: + /* Guest read access should never reach here. */ + if (!msr_info->host_initiated) + return 1; + + if (vcpu->arch.mp_state != KVM_MP_STATE_HALTED) + kvm_accumulate_background_guest_mperf(vcpu); + msr_info->data = vcpu->arch.aperfmperf.guest_mperf; + break; default: if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) return kvm_pmu_get_msr(vcpu, msr_info); @@ -7535,6 +7572,11 @@ static void kvm_probe_msr_to_save(u32 msr_index) if (!(kvm_get_arch_capabilities() & ARCH_CAP_TSX_CTRL_MSR)) return; break; + case MSR_IA32_APERF: + case MSR_IA32_MPERF: + if (!kvm_cpu_cap_has(KVM_X86_FEATURE_APERFMPERF)) + return; + break; default: break; } From patchwork Thu Nov 21 18:53:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882274 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DA2111DBB31 for ; Thu, 21 Nov 2024 18:53:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215235; cv=none; b=qeVrq8cinb8PBr85HSRhDYxaBgDuWl8hDRdGt2XchbfqRDwKz0UYRdHVUsBk5rIqpk+umQkzNQeRC+dOcwNr7Bv9o+Tk4ZnbBfJNVuo5f5+Y2sQAFvvSUyrtFfJPKINrT6/VjTKgjTvbqUKujPwYFtn8BO+JQayeKZAc0HgVRaM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215235; c=relaxed/simple; bh=Hy08ItaN1vh35+QFnvoRmFPDzzUHBtLBpFRgo8LJ4H0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TQswYIiX9pUY6i5xrRjIZM+HpEuwxuw4QTLSNslt46fKRjs+ezFk5QyminCkEIdYLFgDRZekCg+Bdt8A6Sbu9wneMc+hagslC4Oiy/hJSj2x/4siF8buc9HrbEeUp49P/yUB20VN7nTWAtps1Yz4bW4ED2aaT4sTtf8xDeYjtN8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=biGQBj2G; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="biGQBj2G" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-7ea8c2b257bso1129822a12.1 for ; Thu, 21 Nov 2024 10:53:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215232; x=1732820032; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=sPf5bDD3hksLR3rzX7SusPWKEW/ra9S+dTiXjvmnjtI=; b=biGQBj2G4TPm5qDQgcgKgHzbPZFjunZThdC7upjLFC1MrxViYNlWzKnnDDBNW9YOYq pIoQumN8lk3OmohmHnpkjjDfVdmX16xfGhnmAbFG/eutckAbTgyLC0jCJ1s8Far2cTMY DBri7lmvm0pp99vG5Flnl69Bc2i1GRkChhsGQeKi7PVDLFD3KrnB4qJBkmK9oKV/JNGf cOgPsVMPxQuzA7CiJr/b6bhFnBlzJuKKs9zJ2NCv/TZ1uzR+uYHC5mP/4Zxj6J3Q+nKy BnZ7H4Xa23iFJDpOoZym6cn5s7F5Pkq2DrIw1jxAh1Kug3RfO7go5wBN2XoPxjlf6SnE ++Cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215232; x=1732820032; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=sPf5bDD3hksLR3rzX7SusPWKEW/ra9S+dTiXjvmnjtI=; b=g20JU+iBeMCYdbZxWwDRoCL5T80OPd5n7TZaZbaaIGBP7To2E+QPNdr7GGvBQ01i8S M0gws94q1lJ/vJzRDEhp8prZDFGQhFvtOLLWcckaxGzUo0dqxyjDBGLk7xnqqyiDTKJS hIJBsQOF30JmTNv8BWGFZ+Jk3K1Rzgcj5LEZQ1b1EIiH2IG99uqU0rxVTu2sWYa/zkQf XnQXUV8hfDh15iEl48aTVX16/VHr6EUvFZ36GPh5XY2L3immNES7k/mhsS+s+idVqJXt cjhNeE1hiwWRb6jEJakmtqmGa2pRelKaJos97SJXJ8Euyfey3SzCLD9yDEKzqdHuna+3 anbg== X-Forwarded-Encrypted: i=1; AJvYcCVF4kUNDzxDaBwQZ/Qw+SdimOWKcoFQKtSGuPV5gcamGBzIxKO2y9Wdh7vyL+YosAlQ/uY=@vger.kernel.org X-Gm-Message-State: AOJu0Yx7FXcJMXTOTFqrYr5oeOxn2he95Q4CshsLt50Fl5xcvp2S47Je wcwjIsYYLJknDAGtsdCwl7DY9yEqwlp43GImi/4Hv8+90t2bYOf0lOEH3oKnTfoXbS8PG4zw5oh xFY0N5Q== X-Google-Smtp-Source: AGHT+IGPeTewj+iJvEbKTL9QyGdyQogrjTSOIxx2PfdMSr0ggvJqvr9e70xwTnl5Z/jK702TslGml2RZu/Gc X-Received: from mizhang-super.c.googlers.com ([34.105.13.176]) (user=mizhang job=sendgmr) by 2002:a63:6307:0:b0:7f7:1648:276b with SMTP id 41be03b00d2f7-7fbccaa9bd1mr115a12.4.1732215232189; Thu, 21 Nov 2024 10:53:52 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:53:12 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-21-mizhang@google.com> Subject: [RFC PATCH 20/22] KVM: VMX: Pass through guest reads of IA32_[AM]PERF From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang Allow direct guest rdmsr access to IA32_APERF and IA32_MPERF while continuing to intercept guest wrmsr. Direct read access reduces overhead for guests that poll these MSRs frequently (e.g. every scheduler tick), while write interception remains necessary to maintain proper host offset tracking. Signed-off-by: Mingwei Zhang Co-developed-by: Jim Mattson Signed-off-by: Jim Mattson --- arch/x86/kvm/vmx/vmx.c | 7 +++++++ arch/x86/kvm/vmx/vmx.h | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index d28618e9277ed..07f013912370e 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -186,6 +186,8 @@ static u32 vmx_possible_passthrough_msrs[MAX_POSSIBLE_PASSTHROUGH_MSRS] = { MSR_CORE_C3_RESIDENCY, MSR_CORE_C6_RESIDENCY, MSR_CORE_C7_RESIDENCY, + MSR_IA32_APERF, + MSR_IA32_MPERF, }; /* @@ -7871,6 +7873,11 @@ void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) vmx_set_intercept_for_msr(vcpu, MSR_IA32_FLUSH_CMD, MSR_TYPE_W, !guest_cpuid_has(vcpu, X86_FEATURE_FLUSH_L1D)); + vmx_set_intercept_for_msr(vcpu, MSR_IA32_APERF, MSR_TYPE_R, + !guest_can_use(vcpu, X86_FEATURE_APERFMPERF)); + vmx_set_intercept_for_msr(vcpu, MSR_IA32_MPERF, MSR_TYPE_R, + !guest_can_use(vcpu, X86_FEATURE_APERFMPERF)); + set_cr4_guest_host_mask(vmx); vmx_write_encls_bitmap(vcpu, NULL); diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index 2325f773a20be..929f153cdcbae 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -356,7 +356,7 @@ struct vcpu_vmx { struct lbr_desc lbr_desc; /* Save desired MSR intercept (read: pass-through) state */ -#define MAX_POSSIBLE_PASSTHROUGH_MSRS 16 +#define MAX_POSSIBLE_PASSTHROUGH_MSRS 18 struct { DECLARE_BITMAP(read, MAX_POSSIBLE_PASSTHROUGH_MSRS); DECLARE_BITMAP(write, MAX_POSSIBLE_PASSTHROUGH_MSRS); From patchwork Thu Nov 21 18:53:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882275 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 793BA1E376C for ; Thu, 21 Nov 2024 18:53:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215236; cv=none; b=QIWWCIxsvT44WjjF4l02LOA2zxhXITHOPIEh1bAdCphJSCQFY7WOdH9+9yjMKVKLtd2k7Bz4fGZIwqkyq40C3rJJZ0mKEWgfQWqe+ZcJ9omVSBV/2k8cGwmigkiDQ/dziSlhsFWnUAlrsvvNH8Ww+w8C406+Qpj2d19w6sMiRVo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215236; c=relaxed/simple; bh=80AJRXxgHUR06s0FkZz93urmoI/O6TPv0yGQJeLF4Ws=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fuJMFNxF6n9aa3Bbwrw1jmJJD8Ws8Fv5aC7zjVNVVFBxSRrRN+k5bg+t0yGqowYnWjac4EIdcfqDaqy9QQCjUKrsCoDkIH93excQlnm4p/eHvWWgH5uncPbLfACzpxSviJZugP9NtCMLb4MxBIcGPjen9DakKTUuPiNhrMa0jjA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=pqMKSLaW; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="pqMKSLaW" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-71e648910e9so1638145b3a.2 for ; Thu, 21 Nov 2024 10:53:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215234; x=1732820034; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=u7lzN3ovy8rt3qRKTNWPH3t6jAPHyPUfK7VhgkoNSLI=; b=pqMKSLaWv0Tsh7Nk1K4KFSSgLnBMVq5RMs38Tx+Wdr3KCVykc0Pfj1PCwvTAP7dtR/ SZnPufL+Oa1SjX3IhI6iqJp2KQry44tsmllHW3AzhOfXkrGgSuAtvbmK/eXqlxBpU+7+ PDHjDPfAAMvMyVWJMR2cxcfD5n/0tSOHpeod6O/kRWIdBIVpjl06Ij/XOuX5fojrcUu0 r8dSZqFWO7mmQ1o+FMWdh7fVJ4XRJMwWV4WA3MD6wCwCyi1rMmqhFy+/CSUJFqW3PueQ LJCDPH5wksS80BBCWO1/KhSKmfLS6/WFGwWv9YDXjfso26mI8EqTX6/sYzhNMLrMkYbW UG1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215234; x=1732820034; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=u7lzN3ovy8rt3qRKTNWPH3t6jAPHyPUfK7VhgkoNSLI=; b=JysCyshcDpDFkQwJY2JoCRd8E+nwy4JCSfe166zQPmb6fhohEPW3gwAJ6V1m5ybSOz +NPwA8OFo8ltQjKGJNxcRO6uZ7/7vy4erO/rQmvAu681nSia1MWr60mN9VqYhugOFDLW K3ZjyzQpgdUyOZuFOR00J62VrBktPeZA16C4XOjk/xBMExBcGOn7DscInfXFVH5Zx98X jEIQZmMQMk81sxB9I/hls9poYsMzpeNYJGwdYf+K8l7AeC4OcN6QYnx+ibBhfDJGjvhx G7oWqMEEV0ur+E1w3tAqsQ97bUzZtylDcA5cC0ZUWyUF+069hf7h5iIDngEDgdqX2FPw PgoQ== X-Forwarded-Encrypted: i=1; AJvYcCVYse768sw+Xtlf0sMt3Ib4RFFJ01Jt7UNhCaepUe5j6xLW4VM4M2UyZ0XniLTXAuce2QQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxBOeW7SAZJi4V6zmsRfkKVseI6q7hp3VIpmE3AwlTnJXgiN1ny LKnQtqQSz0TXFTLLNQtibiuVcmfW8RjtrUd3mQpP8XlLxzoa1yP/ehlFMa7Gd/3Z1Pvon1y4jdM /2v+V6Q== X-Google-Smtp-Source: AGHT+IE0CLSo7knZFzWKfqBspTMKtJgzSjy6POJ7md/JMhCXlD1vM+E4fOblN3WVaGm9ltr3SlK9z7I68mrO X-Received: from mizhang-super.c.googlers.com ([34.105.13.176]) (user=mizhang job=sendgmr) by 2002:a62:e811:0:b0:724:daf0:e060 with SMTP id d2e1a72fcca58-724df3c0f18mr43b3a.1.1732215233908; Thu, 21 Nov 2024 10:53:53 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:53:13 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-22-mizhang@google.com> Subject: [RFC PATCH 21/22] KVM: SVM: Pass through guest reads of IA32_[AM]PERF From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang Allow direct guest rdmsr access to IA32_APERF and IA32_MPERF while continuing to intercept guest wrmsr. Direct read access reduces overhead for guests that poll these MSRs frequently (e.g. every scheduler tick), while write interception remains necessary to maintain proper host offset tracking. Signed-off-by: Mingwei Zhang Co-developed-by: Jim Mattson Signed-off-by: Jim Mattson --- arch/x86/kvm/svm/svm.c | 7 +++++++ arch/x86/kvm/svm/svm.h | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 9df3e1e5ae81a..332947e0e9670 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -142,6 +142,8 @@ static const struct svm_direct_access_msrs { { .index = X2APIC_MSR(APIC_TMICT), .always = false }, { .index = X2APIC_MSR(APIC_TMCCT), .always = false }, { .index = X2APIC_MSR(APIC_TDCR), .always = false }, + { .index = MSR_IA32_APERF, .always = false }, + { .index = MSR_IA32_MPERF, .always = false }, { .index = MSR_INVALID, .always = false }, }; @@ -1231,6 +1233,11 @@ static inline void init_vmcb_after_set_cpuid(struct kvm_vcpu *vcpu) set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 1, 1); set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 1, 1); } + + set_msr_interception(vcpu, svm->msrpm, MSR_IA32_APERF, + guest_can_use(vcpu, X86_FEATURE_APERFMPERF), 0); + set_msr_interception(vcpu, svm->msrpm, MSR_IA32_MPERF, + guest_can_use(vcpu, X86_FEATURE_APERFMPERF), 0); } static void init_vmcb(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 43fa6a16eb191..5ae5a13b9771a 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -44,7 +44,7 @@ static inline struct page *__sme_pa_to_page(unsigned long pa) #define IOPM_SIZE PAGE_SIZE * 3 #define MSRPM_SIZE PAGE_SIZE * 2 -#define MAX_DIRECT_ACCESS_MSRS 48 +#define MAX_DIRECT_ACCESS_MSRS 50 #define MSRPM_OFFSETS 32 extern u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly; extern bool npt_enabled; From patchwork Thu Nov 21 18:53:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 13882276 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F2E8A1E3DFD for ; Thu, 21 Nov 2024 18:53:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215239; cv=none; b=SSWgWqtkJ8VN8u/SYREpvOWj/32NtYP7nzF9vaLmkwHWLUB4Grw6XiMWIt85dXil7TKpvi4yfae6412LY2dp/D9mxTjFXWG+Nt4mTTXYLlsHw5N5JuBnE+7G3yd8RIFmOJTbVPwgRvOX1bikpxU2vYNgckqLwY5X/jqd5y91yJI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215239; c=relaxed/simple; bh=+IEAVeNUG4Wwg4+URCtcn5c0HktTlRe4I+YksLsQipM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZYz9lW8xKiNzWaRl4u/ea82mjSyigEZYDMDuyPLBBrgZYvJHYG5Fc71Wcp1bnFAR9AIEHlAH+FWXQD/P3GxClizvjJ/KZNUQDjHosdDyaXPnrOOhGapn3oIqzja7Z3MRcjt9AurHp1LnmtqwzmKdHSDWIuMjZBUh4cFnCSi63Qg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=fBvZSRdW; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mizhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="fBvZSRdW" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e389bce28b5so1983475276.1 for ; Thu, 21 Nov 2024 10:53:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732215236; x=1732820036; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=gotUN9en8Y0fM6dWMvs2gnDnz6g25DbjxDMa+m4kWZA=; b=fBvZSRdWkJzqhgJFySJ/5j+Ob+y+qFT6F8PvOequJBzv91rnJKEBBSM1G2qcasTX+S 2ZAZPX57xzIs1m6ZDgrzMCxzWBrcOZG20biKtecqY5bY6lCVNaZi6KpupnwOWJgvN7xC 3N7qWsOfDgPkaqHg2Ele6txJvt0v+xxvbnSrPLiTYMr6AfRd6FrAg8bJQWgHgDGNnvhE nPuANWArzUQrM9sblqCRccDObnUy9TurbQSFyixwjFApMFddtW3v24gvapHwz56LBArP LudZdNXfCqZ2jbHNSzIxqE0zJW/haaE/5JbQnvdBSnMU9QHy+q3x5kOoD6RqiBz6Geok OOdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732215236; x=1732820036; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=gotUN9en8Y0fM6dWMvs2gnDnz6g25DbjxDMa+m4kWZA=; b=v48uHLMd77XOg+xsWuRl9IXAe0LjybIWSr8AS7NNIolOKv8Yg9mbiT7kW7wlXEf9Tn EBfe358qDpm2u94OC49sxV/xNW1RZQxzVtpCOEBX1W0WeyP8HVBXtcYMdpAdUQpo8IYZ dNM2XhiT079hkoFkbCuVgztLtbEiNxu1mfUjXXt1tZHRciU3yM+daRzf18sackOT3jbw Y+IdHMLd6KCTyajIryktLDH4tKqiW5jlihNPnuu7+x2XDrES3cMS0BUCGfvahQQtoBkS U1bkzHBTtaPgHE/kCZONwt/Qstj9hzYo2mxNS9UP+rcC+4bV2yADJ+Gz3o/Gv/HkJZr1 MbCA== X-Forwarded-Encrypted: i=1; AJvYcCVd1k6BKgRJRYYP8aVKvDQp3VJrM6XcIwRGE4eqa/P9uqIfysURhFsODfqaYdE04UDB9fo=@vger.kernel.org X-Gm-Message-State: AOJu0Yx8rMB59gpyTwd+or6tphFtmK6/pJ/rzsrGTfiuhS7pLgGDPE+P AevXbLGbVU2DkeTWcUGhghBzt+ndEpllDg9XvYUvwk3VfnxHh5UYSeto4vdvE+SRGG1NAv6eA6h spYoqaw== X-Google-Smtp-Source: AGHT+IEW6YOy45p6Yk1pt6xc1BV0LA1vh8Rz0MnfiWsd57OUw2v6XIc4MXun1tG0SfU3T/pcelgNrQXa7DSZ X-Received: from mizhang-super.c.googlers.com ([35.247.89.60]) (user=mizhang job=sendgmr) by 2002:a25:d890:0:b0:e38:b443:99e6 with SMTP id 3f1490d57ef6-e38cb72ee91mr9764276.9.1732215235622; Thu, 21 Nov 2024 10:53:55 -0800 (PST) Reply-To: Mingwei Zhang Date: Thu, 21 Nov 2024 18:53:14 +0000 In-Reply-To: <20241121185315.3416855-1-mizhang@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121185315.3416855-1-mizhang@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121185315.3416855-23-mizhang@google.com> Subject: [RFC PATCH 22/22] KVM: x86: Enable guest usage of X86_FEATURE_APERFMPERF From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Huang Rui , "Gautham R. Shenoy" , Mario Limonciello , "Rafael J. Wysocki" , Viresh Kumar , Srinivas Pandruvada , Len Brown Cc: "H. Peter Anvin" , Perry Yuan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Jim Mattson , Mingwei Zhang Enable support for IA32_APERF/IA32_MPERF performance monitoring in KVM guests. These MSRs allow guests to measure their effective CPU frequency by comparing actual CPU cycles (APERF) against reference cycles (MPERF). Only expose X86_FEATURE_APERFMPERF to guests when the host has both CONSTANT_TSC and NONSTOP_TSC. These features ensure the TSC frequency remains stable across C-states and P-states, which is necessary for "background" MPERF accounting. Guest TSC scaling via KVM_SET_TSC_KHZ is not supported: - On Intel, IA32_MPERF ticks at host rate regardless of guest TSC scaling, making passthrough impossible without intercepting reads - On AMD, guest TSC scaling does affect IA32_MPERF reads, but handling it would significantly complicate cycle accounting Record host support in kvm_cpu_caps[], advertise the feature to userspace via CPUID.06H:ECX, and enable the governed feature when supported by both host and guest CPUID. Signed-off-by: Mingwei Zhang Co-developed-by: Jim Mattson Signed-off-by: Jim Mattson --- arch/x86/kvm/cpuid.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 41786b834b163..309fa7fef6b7b 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -399,6 +399,10 @@ static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) kvm_hv_set_cpuid(vcpu, kvm_cpuid_has_hyperv(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent)); + if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) && + boot_cpu_has(X86_FEATURE_NONSTOP_TSC)) + kvm_governed_feature_check_and_set(vcpu, X86_FEATURE_APERFMPERF); + /* Invoke the vendor callback only after the above state is updated. */ kvm_x86_call(vcpu_after_set_cpuid)(vcpu); @@ -697,6 +701,12 @@ void kvm_set_cpu_caps(void) if (boot_cpu_has(X86_FEATURE_AMD_SSBD)) kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL_SSBD); + if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) && + boot_cpu_has(X86_FEATURE_NONSTOP_TSC)) + kvm_cpu_cap_init_kvm_defined(CPUID_6_ECX, F(APERFMPERF)); + else + kvm_cpu_cap_init_kvm_defined(CPUID_6_ECX, 0); + kvm_cpu_cap_mask(CPUID_7_1_EAX, F(AVX_VNNI) | F(AVX512_BF16) | F(CMPCCXADD) | F(FZRM) | F(FSRS) | F(FSRC) | @@ -993,7 +1003,7 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) case 6: /* Thermal management */ entry->eax = 0x4; /* allow ARAT */ entry->ebx = 0; - entry->ecx = 0; + cpuid_entry_override(entry, CPUID_6_ECX); entry->edx = 0; break; /* function 7 has additional index. */