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: 13882278 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 4E2AE1D79BE for ; Thu, 21 Nov 2024 18:53:20 +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=1732215202; cv=none; b=sIxGjFJGeaVTO5BJ3BJYTBDEKgholxiL9y8qfggPMNrv1TMZmIBOTz5Rjr12F61NCgWGzYM48WaITlIra+xP+RHaposWPylE9LzOdtyZ5a/N+hEug0ESKaCgwtouCdYZru1MZvS0f2KiooHxmtU3a8E+QNw8c75Sf52RmzGuBCo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215202; c=relaxed/simple; bh=eGhYnE5emXo94Pyg5quiuPFdiSkETv0T4xLbIt+gbbE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HFZRuvxaxwbe7cvS6S5xEDAsXh7MObZqq6bHoDnr10uUiqDrmKbdknAy5gdxRck03XU5I8ketvi9v8U7iB7fj4SDcSKuXd9sf1qFvZpjnEBRURZ/S2/VUBHeoRaidXJ87nxEYDLZX5dJm8ZOMRZ8GIPSrNtGBVnOegLI501Ezxg= 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.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="epBHLhlg" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6eec3cb4a81so23295087b3.1 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=sGzjYuW83Eb+5rZJnctTNYJ8uozVXVXks7vsMdFlkTOr84vErYzMuJ/MEytBHxrxWw BL4juMjMlAfXaz+jfsLGuPDtOhQCuAxgHCg5VSeMPX4Q8vR/KSgSSc+Q8pNFTMflwDRl VVrykmjtIYqTg7vNaeXKy5WJc0RZDebZNJAum4PzHk6Q0FYpDvBVQ8i7rqHT7ezGDa+P ll93fzEtAThpbss53mvJmso24NYCAZd6LDLaSLqfycRJEl41a6P9dYmInZOCsk746655 4D2BB/naqgFYgvh3DltTxaEDlHHNxvu2t7Jy8KC7BgcGG/evefHBy/gtUXmSIs9qbZx3 x1aQ== X-Forwarded-Encrypted: i=1; AJvYcCXcYWha+ciY/D1X02HO3MogGVgf7myoNvakbxi2EjYEvq8BLguOL1QIK1QtnM4nlOAPh9xTwnnX4Q==@vger.kernel.org X-Gm-Message-State: AOJu0YxNr1RnNu9fP8oraAURXaYAvEltpoOA8w8d3TcZGdCZUFqxLdpo kvIqTikpP1mjVHkJgAtHh/UqgZBjHX+nM+4k1GkE31tL6E4WA4hk0V2L2fyrvGlXaidCdc+XImT bKlXpbw== 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: linux-pm@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: 13882279 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 B73141DE3D6 for ; Thu, 21 Nov 2024 18:53:21 +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=1732215203; cv=none; b=O8AdSqAjMvrWqBJ1Kg1PB4Yfk24PSTaHJS2U59vU00BaRgepsoFZv4Isn8zNC/kMEK9ksG+zVmvbeGfHlqQlRdWSsRtslOkdnev6KBExzrWO4I9DAURzPS2KvUSUhK4c7mQcbj0BG7WSNCCj/2e88LER4GboGgyISpu+vK9b8qs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215203; c=relaxed/simple; bh=1MOR7DCPmoVrL2EupOMAwcUroCAGOgOeQudXxZyVN2g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MgYpCsZmtQBuEHTohPvhvS0gQAN+QnpcyE6Ud5NGhSF2NVShKZZTsXhInYpHJ4QjcKaN9qBxU6FxKPxS6JVVCrgSIqeDoTsoGuB+QukvN8e6xnTa77LNyv/AzbqtzBfAy/1bfnecfk6DwrI6gQ2zDDyK9/4RPsJpQagocoyMqMI= 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.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="ktVOWZII" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6eeb9152b2cso23164567b3.1 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=TyquSS5UhIrrQI72NK+olDFN85KOZFcbw1Q2gf48GPsJc/eIVlcDy6iAr+J998VfHH PqSUK/VHE275V1nI/7mMhuRf7fZJKoHO6QejtgeTRMdTKqZzFhOa9KnEPR7hKD1sKTML VgkgqMbqU/vKNA58cmkoIokNrBnVetPgoaP/S38bsgrwLo7/p5a8rZZVdrh1AJLawfqQ tkYiR8GeqGG3Lwtx+p+m8pavCDVeb3K61JUOzlU56vC+X7KhLNcAHo/NpLeJ4uK/z3iI EP2NTQKRBdSxTu+GxUUJ+Mzgh/UEBA0uHyI+2Yc379+3LcGnzpk01X9btfkxZ1XEDvLd kxnw== X-Forwarded-Encrypted: i=1; AJvYcCWMYfxGqzXzV96fcBz/ieVa/kWA7nzsW5hKEBmd8bRuT1YC1kSe4wclP0SDnBAs0F8IVVglwdKRAQ==@vger.kernel.org X-Gm-Message-State: AOJu0YyvFu8t8+MDcQMZ9Ek1IAwJBgY45pfeUer5aS5mpftoeaqmv3lP wYW1hN1hxFGQiy79efWIINXWlMj60WpqLf2HlG0ZHLrIOrszM7zJdPdi+mxOS/eCKzva8tkhixo Kp/ZREQ== 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: linux-pm@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: 13882280 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 AE0491DE4FF for ; Thu, 21 Nov 2024 18:53:23 +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=1732215205; cv=none; b=r0gMnL6zIoHvVi+WgODVs7c2SjagFeRFOcUnhZWyiX1PXaHOk5rhlX3/z2uyKdGLrGdGsmyB+GZGxomECKtk02QRvSdgh/BfQQZeUjnFSoHulinzVN+98H74J/kCGN4Suva5gIMFGim8MW3i8DymDJzWOYjsQBf+ltdMBGMQb9A= 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.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="hFstuxpQ" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6eec3cb4a81so23295617b3.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=qOzks02LJIe2HhE8ZSW5YGNANcELMqbJQFAEKecaXuelYhDEDnYQTKJjt8QJqss1kR MpPgvT+tn60oOet0IbSaUVnrcOOloMfZnMF7VwX6AM/osl7XWi64FlbSFe/ts2G2VkKp 6yHGIFH56/48vArPcJVFIOS8fu+xVeb/6+upXlPYk+fAslTb35YxrDCGUci8Bnm0vChK 03aoAbZQkAliqLu2uAhQliSGkBVvKLdbgjjANb7YKfxJdHMJWQv5hnUyYjWaDkZe7NE4 VhIb76HIo6nytCHoBk9F+8agB31g0vGMSPZvLod95rYGFe6lQbQHUOXzE1hdaEN1yrWb 6Dew== X-Forwarded-Encrypted: i=1; AJvYcCXw9chuaGVBuNKBHOZPlQ/xrSNXxGZxwr0DuFpIXhVLQm4ysSbJ47NeRSEgwOk8axDPnA3F+Rg7WQ==@vger.kernel.org X-Gm-Message-State: AOJu0YzvXSKADZ9XLADOOWjThLrcda4ugpWDYJKspGH+me5+TvpTOWnR STzu83Eqrs975P3Dpq90CJDExxXMSRRIWD4PgY40xqf33k+4Uv0PbiCvAq85QCLcWQ3AsD7bf13 xyAsltA== 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: linux-pm@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: 13882281 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 66F9B1DE8A7 for ; Thu, 21 Nov 2024 18:53:25 +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=1732215207; cv=none; b=kAsKyETHXWj2exHm2uJajabTySkh4zy8bF3TAeE5ptyqSyfQBMeEciJKTY3wWjkTzUDgiFqjROASZfQ+OQmHxYPXAJSsmwa35YAGcVLP7u/mDPpJzdRNRJv3TZFWmD+c3TxrfVtKSfLDRgLZrM4ePBjJoyzRFDHOJX9dKRwqP0I= 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.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="pTkCW21K" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e293150c2c6so2280791276.1 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=HsmXsfl/kyYKuEO+OYAdmwJFOVqmxyPpzBf305LPjvCuKvtmpoxxslv2d44NXdSwcd pGOMptszkmuLiY9sqy1Zffb1LegZY2gUUhZL45AVmAm6K8m499U0GfYAi9CGMafiavIb FVON6UpoiHfE8k3vR3CVJDf5c2kpFeuygck4tThq6jO8y+bItOU0w82amY3sh709iCpb s/SqRvJ3FNWxMx8b09qHVMGwxYsUDGQwIvlPUNt8zO5KgSV5pIRbMXmlE7cET/i9zn/f QEoWWdrFDQ7PiHrJoOUt5leMRVtiulnRajU5N4l9s7m0zR1iP6hrTNy09nnKklPGxvJ7 iY2A== X-Forwarded-Encrypted: i=1; AJvYcCXZe11R3A43UQqNd1wAAe8TMpQPzOMXtadEavLUUcHQqJFQbfjyRcWcSgx0lfxFmW+uKLgsrvIb0Q==@vger.kernel.org X-Gm-Message-State: AOJu0YyjIYWgzMn5iYS//n+vh5e/UyZOlMb/ZResiPcQyL6/hIMcbgRX VayreDJZjVSGva75Uxu+/wfoRdTuCMvx1sWgmmestmtux41pFLdAH0Nfsjsy7CzrdgodnfW5fQQ nzByujg== 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: linux-pm@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: 13882282 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 1D9C81DE4FD for ; Thu, 21 Nov 2024 18:53:26 +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=1732215208; cv=none; b=OhYtYrQpME5Ld5TTO6h7Vy/nDzZrcxgi0otwNSg7TVndYgFlSW40vEA6CJYYgWFQvMjTJOqkfHhqeT0WtRHW7mgSkWo0dJ6oYBYQYtmrrZdmvWZ3cwIMZJe6WWhZ/DNnRiyxquSmOc3Dogh4hGngyt+c6EDrOd+YjXrQ4qAp3o8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215208; c=relaxed/simple; bh=3LJNuc0FG7aWLKpUxqt+8FC4Sp0enBCrekTBe7yW4mc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cb0y6eG+CGwCmEjbJ/o0PBsjLTDAQ9CE/aN8STHFAYFtP+khlE79yheRdR7UQEdzrssBuRd7TulkoMGR/K1B55mvBcNTtt9ypZuGKx3CQZaoUef1LA0Oa1oWwCitVJKxM/mUdQJHDauO00g0mX6cewT8KXDqLw3bvRpVa16+j/k= 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.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="l64VzQCS" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e3891f31330so2151908276.3 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=ERksTbexFbBqmoFJ6MuwhyEC1YwaIO9rvIAKP1xFUM1aMFsRUFCy+8jxjef3DOTk27 6PzPtnZOpVZu+EfEzeHIMTpEefk6dwyWgLLMXaDc7fBqiRgHAdlgUOOHopcXZTA9p7h6 VLFe+/0v7pbxP0x/EUTi85U7owbHoGW9WisHer2ECZo2saEvr/lNxspGZFW/3fblrCZC jYLwTaZBfMSSAdV26uG105JM+5HcDXZDSKvIh+AwPurX24/Sy0NIJx2G42Slm2Nsc68v yRoP2umlTFKsTax0i1MPJoa8EEdYV0ZLDUDiD2jHuMqPKYIgwCWafKCnkGi5CzJzBl0i qo9g== X-Forwarded-Encrypted: i=1; AJvYcCWrpmYMKpDHxWNY9Wg1DjvsjSBUMbbOmBkSmW/wCE4OdxSPAoVcDr7fFcZRo6TS4pWKFU9kCfg6+Q==@vger.kernel.org X-Gm-Message-State: AOJu0Yx9QwDbjyZVBP+2J1Mw1YzhwwPH46PXzZ8oXAyXSpaq7Kcu9pJk SjSHgs0c6KRJvycWOdMFdi4ae8FwEK5zriN9fUgJMszD3eYLWWRpVXeTm+cscG8JR6YdqYIUf6y N5idDnw== 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: linux-pm@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: 13882283 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 10C1F1DF26D for ; Thu, 21 Nov 2024 18:53:28 +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=1732215211; cv=none; b=RVkkRFKgMpCAfTbEQ1rGo7SrISlIwNX854NSFA9IFoiAkzpwvcZEctlWW7bPXNFiWOqXybLjiTwovSYCdywn5vrEMkMmtiIdrfm2RrSy0zHSTvTmycaZL/Vpu1FiqxPR+NQyrBkPzJGL2IKtgfb4aieSUccOj+IOmX2dh7ABz+g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215211; 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=iy0hMTaaYexr0uq+ngHxB7cb9d8ARMPbPL8jePOB/0N1gFo1Dr3ywkJOoEjJRITTZwOrGXGoQM2KSO8YFUGLWdDIqvcijyAcLxQlfroEhl3KHsOnGKYYCGPylUlbyynZUutCK2/Bb5jtTRWbBcNm7hhfLJN1+pqlOEyBXkQnhSs= 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.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="3Hirfohn" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6eeb8aa2280so20398697b3.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=Au3E7rLovgpbzb1rDq4SsgEWBKNszuQ/IZaqqbT4QHSQrKdaftnNCXOtgYU9F9kVAT 45j+iLoyJVk1OnPIacgqV2XZe1y9EGe4HItljYdUMQv7GIg5pDS0LCsyMc/cMjxQe4W6 PXAjn3YfKyLHM8No2zgdG2oldrRpXA8cUS341xjfCj/ofByG658e+wW8IoZ5QCxxNFJE NaOUKLkrXDe/CD0gVVuVpMZ7SoNDfbLAPz49ewrwcRie4tMublajg2SfwDTR7h6eYKqG NHJbKdL2QP433qH4ldyrr5DZEd/YKpqFGyf3M+13hi5zeDTEFfjb+hRh0VpcTlpsXSlO BrIQ== X-Forwarded-Encrypted: i=1; AJvYcCXb7Yug3kxDaEnrbHMMeV42t8c09im5cyavhvlV8I8HdWHvQx5We00r8x1S7U5L6El/P657sX3ZmA==@vger.kernel.org X-Gm-Message-State: AOJu0YyWPA87MD7jrPNA4DwZLR+kBCGdwBUGMzmfv6/LKQkDdYKCB/rZ HCuxzAPQef0+j5Pq02WYXRgBDPiCHg47D7rEBxcA0oQClLhoZPnxrnnf46ah6LZbfhKkyCid5zC Di0tCkA== 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: linux-pm@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: 13882284 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 7255B1DF731 for ; Thu, 21 Nov 2024 18:53:30 +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=1732215212; cv=none; b=VugVkMIZesad2Zr/arCU6Nxu5UhsUuxTgN7qCH2fCs6Y5KTLIspnhwhpTywcZ0A0djbBBBPrXRj9y4M14+nlmJfZIBUhrCphWzkKiy8BbM1iZ/Js+TuLTBhUU424WTM53RD2YJFNU/INrQJ2BHQASjFic4Fkps1tyZwIhi20by0= 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.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="ZmRG4+Du" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6eeab4be015so21612567b3.0 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=MTaP6YpJUksp7PJ5KKfTsVOuLqGuMWN0GD70hFuAppYBFMZ3YDmesWimJV6MBFFFll xVuTgfgIXeqGjPDk16mC3zzGs3x5YCcpxnsnmQmHBJQidTW+N/C1rCP38MVO7XG4uBE4 4N2rhh38BohbrDs1tZLuvV/8iaTQYMrk8i3Fz+9glw11GQDVJHJ/2LmTQuKOkgD9UJns XbsudI3jMpw5H9sNpb/AcHKpW+XeEyQlrnKwRjl77uXbQVy6HY7KcGuylpYg51eTVHUY lLy55+mr8sQbdsHOrw9u/+qYA3YCMKS0q1lktXDEpOD8J2NOJubo7BD6t30HM2S71vLG 4hqg== X-Forwarded-Encrypted: i=1; AJvYcCWBAZYKTQ7XTleDgk81APPjz7goQ3lVIsdiNYLre5xfswVKGBQ0ESHIHaLRgLnEC1sTBHmdriQ6+g==@vger.kernel.org X-Gm-Message-State: AOJu0YzfpJ8T7GMQLtUQX0QbJw4+Uv+q1SkzACBIOTWBCCru1mCQToZ9 zMyO7ntd79lypXUecwAWOOzHHhG2VlNdFERGLrqDLehgiGlqDjr1EUDS505rY2MH4sObkdspR5t zM2DyhA== 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: linux-pm@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: 13882285 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 33A1E1DF98A 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=1732215213; cv=none; b=jnkvJ80vNfkfCVIV4cheIrkktPCJpPuwm/VWBF5l/KGObe74TuNomdbUZGuKazOZAJ1bcvKNIJ291hT4+ZvCX31QYtZ4+fC/QCm8yRoGGT+VN+0fhU4TWkRoF/uPUQukEY25awlikqR54M3KCZ+AK1iRumoxMmQaFe5HmCIyl2w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215213; c=relaxed/simple; bh=AdrfMvymD5zuRqTBBGGoWNJ0DY8WVGxpVB1lhy6aBew=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bfOnDEVisQSr/icVg75+i8Eqb9L0NFpfrvtOyp3r4HaNA3Ihq4wqUKEimgB2MCFiT8JoPUMF7ap9DEvbRxQO+hdRSyAvFIOxXFvRP2+N31QMKt2m7XyWdkE8QELDP4guN8sh92RDQTKLLKZJWXEXMcFF3CkQkMNObVDJYAlynnk= 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-6eebd4e7cc5so14915787b3.0 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=mW/TsqVJmM+cH18R8hL+kISQhaWGCTb5bcVMcdq7PaprbPeoqMvIW10whkb5TRG+bG r0ixK0r0FXhS0BnnHDMZzTOv2JK0Zr7XbFH9r9DrPooVk5D5EHNVhxrbqlJ+TeYek2dN rbjbxAHrJG1ksQPL4nO5r9t1RPRjwZJ2zeEff7XS2N+1xPXtK2IlC0X78elrtuSC1F3n a4ZHUh/h8vcM7Vr1Prx1mR13jzxgov1SFbUMkWkB1/pPI3um9yqAt6ovWTM4rHbznOvg 0e1VsGO+L53XOohQFk5MIukiXPZfGC6e5FM9Pajkn0y+CY6dzszd8E0Xd6cuvY1Stt9Q h4+g== X-Forwarded-Encrypted: i=1; AJvYcCXM2SL8Tcdkno5uBX2I8IVX6IFybM+0uK0y9JRUwuKAMjAA+HVBqMlScMbx8w1D6TmCfNrdU2+Cvw==@vger.kernel.org X-Gm-Message-State: AOJu0YwYpuMWlGQigK28C4h4BdbU+N3SdSsVUBzySbSbKiv5IJPS1LzM bbVDoGHa2FlsxN6Itftk8igv4kIE6NevdT2GGHn2gVEURhIk8J7vC8HcjnOGqOCeLz9dw1i9ido gN+fx0A== 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: linux-pm@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: 13882286 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 8FB591DFDB4 for ; Thu, 21 Nov 2024 18:53:33 +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=1732215216; cv=none; b=SIjOyjCGtZ1k366Q52t9AdrBw/faygYrhAKeGXkDIu9V0Dii6+tRDhsarp1W+e3QpdE1vuK27XtL6KU+sF+zb8cwUogtfvJ83OUxes/fiOYi/w4vdKTYy4k0KNDpuzZCiX3pL8XPhy1O3YLEm79D+kwQx21BORQgb0sm/A/c8pc= 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.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="r3K4c32M" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-722531f7806so1680810b3a.0 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=bN6gjs+JniLuld8yfEhfY3u5wGKSDIzWvpu55pF39SfT4XewWgcbJnb6VY6E4jboPY qOlUlmmJlI1+HYtaz85kZo8H8xRAXwycFp78hbWEebRLt8eRb5XwFDf4LxQ6lYHAJ2Pa fzToRIgtpxCyIkXzA2lMfeDTMPoMzEzHTUknh3BdzpqAXXIkCC5/jK8yV3W6jlF3Qruo mM9i8A5aYoEgppFL0Dg9yT4cFQxH6h8HBaSEM9qHcVqVnU8DQVjWwRU/VLTHjh5chlr6 jTpFzD38y7LGJr3BpoQWe+3jKjVJD8H2YFlRs/Do2tLB0Oq9E5fjXVsPjb/R970vFqTj 6Vwg== X-Forwarded-Encrypted: i=1; AJvYcCV/uJKijtZ2C5LoBGDJSbMIuhMUGwMzlVNNQHiKOnqso8FrauFX4ntdqpXqy8tMm1A3CiWrxhqMdg==@vger.kernel.org X-Gm-Message-State: AOJu0Yzx8Dvf55eoWe1M3MTMGT9wC67RxLTKOEN1BNJvbktjmI6uOvJ+ 7s4GdCrR4KQUUZAfS1ENqDS5un/qC6I/Od+XQoGYbDRNS8f9IKPqDEqLF8YcEg056KPURRflnkH h91077w== 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: linux-pm@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: 13882287 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 9B7031DFE3F 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-6ee6c2b65b9so21204647b3.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=R/XW0bQ35zfZXjgSRfeu+Zsb2gJQyw2bYB7wZXsQUIjtQBZVc8yfh544VB4xs6DHWK BnVxuL11Q87Ons4KdEFd5JUEnI0PUy1V32gDNxiAMbJY6x53jRLCsqVd5eL7XgrvLTsa B85yAsyNcjdBjB5mnoscWvbrsx9ULxWO31E3LEkPRDWv1yor9IWKIKodHed4uiyGUdrc NEsUuTFq2kQ42Le8kZ3MzY5969islw2WNBz7BqfP0ymAmez4OgbSc3m9/S28e5kwSZVf a3ycdQsb3oaNrS0ctdZQ7TyPbAtoSaAgmOSeDdg6eYPxDgZ0Jx2PnlYXsPpcwqRFZXbE Xxtg== X-Forwarded-Encrypted: i=1; AJvYcCWlLA722pTRuocF1AgOQ3fltzfzoRa67lSJUraVSNzM7y3kqL/Ubq0jglvbxytZ0Ujf3ufP8Dbd4w==@vger.kernel.org X-Gm-Message-State: AOJu0YxFI1Y2b/Jh+WDLrYNBxeDMfQyFxEM6ns3Xo4LokIvMBEI2fX6Y tu6e1Mxv+nxr2gcmicwsvb+ld0Ie+13eRJFFanCsLGP4tAtkKmyzH+3hORzs+8UcMF3B9eRulFf aimWhNw== 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: linux-pm@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: 13882288 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 96D331E04B1 for ; Thu, 21 Nov 2024 18:53:37 +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=1732215219; cv=none; b=khK9VOPi1mhUERslDieV+vJoM/xAMu7soLKkvnxHPaTi5i/b8I2EU24oq2xCffaqwAO9kq+TtcEWDVkXzYUGjHZXuRepTRLA7rHuo4Zny4G31wmXz0BM7p8Am+ap9vhhBbqAOV5GmTyi7GgB1LQmHHjwub+ZpGng1ga5qwX0MhQ= 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.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="ACTzjQHK" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6ee813065afso21136377b3.0 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=ZhwjgR3E/5D2MQiSeOjSd4/P5MpoCX6jEizqdL8VHSZNSkkwdHgTmbauTadsOv61rh XynhOJER+XpsomIjrWNQyuHOFOKVbguSAyyRwxjynyK7k6Q6WixlETR8E4/B8l5+tkoz SFumTRWJqF1v+Qm/+6O21GjYaXgnMrioTd3R6Qn9UCI2Z7y5BEnIHtpELV/Avd9Pp6Gr FTOVakX32qtORkoJCz/jIynck17pST0ly3cEH576lz6sSHwUCvIEbjkDhva6pmevmwvU DBMkecqLl491nFAJpToFOEiHfrJH9eLR8v/bssmWjpl+YRcXptBWXOntNJYmIQAr6sn5 ZyRA== X-Forwarded-Encrypted: i=1; AJvYcCUzTxi+MA1EmPsOOupwgePZzBxPtDK3ly24P8ntlIXGw3rjaIZuPAwil4LTbamu2n77LrMs9oD2JQ==@vger.kernel.org X-Gm-Message-State: AOJu0YyclvNhLipMlIv/oO/4Niixc0JSlTD9+3st5E1nivyOGxTDYL+5 BVVwFDbmM5deoeq7slx6Dm8XmoLdGFQ4pgOQJFBbjDkz8K0G4jtJrxyAXs7rsGkbPm5rDInakTL 6UkwCiQ== 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: linux-pm@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: 13882289 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 8FBD91E0B93 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-71e5a7bd897so1251039b3a.1 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=T8kFRB9Ws5RArJNpXjIQefiRc3EzLfRrWnvKT/VRUtlU/2UrfKkDw6eH52fRNf6YK7 7r20bVkEymLOPn+5LuP9sg8c2bSTsYDaJvIS0bBd58aIqBEch6bMi65j/L71Burv7GCG IwnVQa4zgzyIYPFYyrdSivQP1M9Nu6eQty/xmWCN3sixZtMxqZfZiNB2gdkRwJLak8S5 pSpaKrtsgtXMuDvgFEXkb3GvY7+0I/wApQJvSEB9m3GfEINBR1KiRsC40UsVFY8fPRLX XMwazD/LnOO8llZpz/dPBHVrzwSj/4JeZ0+qCcIzj9vPn3/Y5U0Ee3+dAfMxuuimEa9b JfAg== X-Forwarded-Encrypted: i=1; AJvYcCUAj0pvrhNJE8ntB1FbyWaKGfQ66JKvodTU3+iHnWAc/aMlMLQBybCap/IsGdZBx3JrLtkJqY7a0Q==@vger.kernel.org X-Gm-Message-State: AOJu0Yz6zlp0KJb/SS2j0VxUxRP8N1uajq4SQnrWi+kqFUFgv+Fgw2kY 0Fhvbk05iiCXtP7wXLgsGS5Rab4EAFi7lAHdAumE7er9eMk9wdM9HjWmf8isDSp+QM3NNNimGop JrtLDEA== 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: linux-pm@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: 13882290 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 5387D1E0DD0 for ; Thu, 21 Nov 2024 18:53:41 +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=1732215222; cv=none; b=Lvllg99m20H3lG9rXorna6H4ym1W7K/A3NrYpERAd/CfWaxoocSsQebaBG6Iz4Iyj6n+pYEit1AE2WDSpFxwvxQvQnVMV2FWMeD2WwfLAgOviI+nXOIRxUji/PXFn6al6N+g/3DETTqI/INIu4RjnungQdsJ6oWJvlUpe8opVq4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215222; c=relaxed/simple; bh=Ia47x5eqWq7m7XvirgYoICFMCzP4YkrxFKv1nGHdVnI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IrJ1Jn5CsKedITjWzIfGP5xUXiSva05+5tma2v598o2FGXmv4BcJhjOVXtUbVSsMk6pLXv23QXei5GjqpNbS6/kG/uAheQL+RiLvs1C8MSZq8p0ZqGwIVUaRwYCk8EJyPIDVu/eobvhy7q+t+SCgre5sv3QytmuU3bI632D4Ok4= 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.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="jxqjqYcC" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-71e578061ffso1522080b3a.1 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=FcRBQ+EuYiS8Gf2gejhpR3fUGHvqigTEcez1vil8Eqmq+iRZ23DVIthF1nc58ZIUXs QMy0Hk0ZvQW/aK/+ywlGY0M45q3eBcgyACdDWmPAGuEfCpIOKDJN5m1a7z11a3OYuusH PCeCFAy+jzc8axMoLcn0p4lONx6LaKcsVY99QbipjdXb/464VHOw2tgGrTGJnCTZNv53 M2POutbK/hE5uZCybvh/ywSnLLVQTjHImRvSH9hPqKH4d0jYLV63EN3zuMMLLivzCSNT 5IippUJlmE+SaI8oW4D9u6ZwsW9YRhYkb0g5kLm/ylREGJUppoXY2BhJ9qL2FrLxpfNz yImA== X-Forwarded-Encrypted: i=1; AJvYcCVUNV8MBbXc8h+pYNWMIT8xhamX0vOkj+CFLZYCQIcyMRG8QEO3h2WmyujU16Z6IJzEgNgbZ3g15A==@vger.kernel.org X-Gm-Message-State: AOJu0Yz3uza14Glc3XNmfoSNPNRhD5tDGVkc/oHemaz+I98J5Cjdv8L6 KtZH+sIC39vlWvYX90mu1g2suPEPXk4Qs7QgtXgftW69HaXdTwMi2Lw56qJu48oJzvJfUN+gL2/ BX/KlYg== 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: linux-pm@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: 13882291 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 660A11E1324 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-6eedb7b9455so8061757b3.0 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=eYMewP18MnBxpQTZMGbnByxs993t4hhJRj7Mr9MHvvzt6kBKBe6SrZIN/MHZSMsFjI EE9oJWpp3E2ATmncWXPs6IsIjn1tj4yjVJkyCTPq1QFx8hyLIgvkkklBFjVAxzNZC4Ep Rp8yxvm1E49pGlt5SCaIv95ty4s5hLV3T7WW5rvVkx3wiwuNeKTEeLaKu01KsGf6jRY1 PQi4ke06sXREENm2zrhGD7rCLxwf9OqmSrtIKxo2/fNZ/emU4l1Tu7z+m1Y8jb9z9KZu WbkJ5CVuQ5p98GhDqr9Pzk4nR858KuNySvjIwpJ8nQj68iDyMoOnIVOduusTwRcVbwkt uEJA== X-Forwarded-Encrypted: i=1; AJvYcCWcW1abEJcaimCDWcD0hnhMsKpZK4w//7hQU0vD6Dt7GDrl186yPNCbfEtJuq9EWWy5VisBoQIb7Q==@vger.kernel.org X-Gm-Message-State: AOJu0YzDrNIaFGj2dL+az2nD3wGCQ9iX/6rUjRPOnx/FItUAWbuh8YcR W/DcJAEX8Hfq8iGlK80qhp2mnKZTESvPFMDZA37YfR8PrhceO3iZeqhau1MXcEQFNvVZFX7cbu+ oiOw6zg== 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: linux-pm@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: 13882292 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 AFFC61E0DD4 for ; Thu, 21 Nov 2024 18:53:44 +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=1732215226; cv=none; b=CVD4xtMdjioDgHe92DxRkhb0vJmo6wDKKt834e3uGIQFZeABBOWclj3PG8Ei02tqTcKtK/JuG/saNqFUpx2X6tO3bOA7J6q7yU1Q5vFD4qFfWTfLGt0Q1Sl0wMfyRc+aBaGou4O/k/i36ZYlS733ISDdqnDTr+pUXhOf8Ej2804= 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.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="oCmsRzS5" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-724d6a812b6so621927b3a.3 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=Hr8BBdEDTE1590DkvyU6e9+AYh0+mJpte/xlI/dGwcZgTdgms1Of0I0M0t0dy57vaz 2/h9pPUcX8I8tNDoQS1+6waY4d6EEVaK+0PwsetC1FRdNGLOnZTpXlPYSZ9NKjeFzI0Z LjAqRtLJPuSILPEU48kmsb/q/wq3dH/YgFms2BSOb5EYlZeuMDbYYHO5QOUrGwGMGfWE XeWjvylZNkpBXinY3CA9kyzIpvejQGkSFPvzPv43K8Mqdo/twCYiDXlkhI72zewwrKX6 d3jRcPP/pAnBqZFenbGok17dw7xS/RJoKprBy/9OsUnHrixFIM3R5EZOW9mSivTwO+Ok buiQ== X-Forwarded-Encrypted: i=1; AJvYcCU+fAE2vaDSBsZMTK0yJ/RjLcsE79waLTNqyanWcyppjy8SWS+BpmaueKprsl1tfaWCafkkn6vlPA==@vger.kernel.org X-Gm-Message-State: AOJu0Yz1fr3TZ9iEJ5Z4QkBBdv2QRcUliUL2vI6ks+M5gtqVTo+Agkus ki9sJNV7S5Kj2AjFMwAzGd/MJaA1N7z0PUPBcpcU5Iz4yjMQxRXnQwS1yY4bSulDZXwUZ3Y+tZp AOblOZw== 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: linux-pm@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: 13882293 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 62E4B1E200E for ; Thu, 21 Nov 2024 18:53:46 +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=1732215228; cv=none; b=LInkrW4jnDjo4SDBjwHgYC2Vc7P1CwymvGWMAO++gbMiuQ9d5oVo27MpXyvWRsaUghaT14VvyfVBjsR1CsL3QUf5iF+PUj93CP3UlkxbigQrxZQPSYoJ7Qj24ZHAw44Dwfp8ZukIv6zWtVGn6KCllkJp28I6RrrEs4xdum1BT/4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215228; c=relaxed/simple; bh=kH3D5R4M6NKtNCZGtvdwg+QTSyMzUsIWYvW/jUpioyA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DBUsVz2dGNhH3MpAvajqvjHuT5gczURaHMB2QS0+na0Pfp7p34KIRNrbGfNNkbUzX6JcrRN9+9MxOtEqgs35u3R2WdsR8ZAfk4mkgGW3lGqqOveVC3LRw9fh07GmAtR90vwN79woq28LK+hQUR/PkVoLEC0sKAUuACgkeY6va6s= 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.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="yWzJ++4E" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-71e55c9d23cso1017303b3a.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=NxycTbtH8bsAjRcifC8Di0mo57C/ngmr/LYMJ3J+JPqHnjcXYOnP/d7Y8qt7lphpPm 42bvcpQMiAJMeVQrppdjw3TypQ/Fh1TZS50bFKvvxrOgrOukPK9C3Bu1I7bleBErYmd4 +DxWqIT/0UrWhzFIYHY1HPFyrrs9yAZUV5kBPiFtaUUte438qou1Kl/uYYvKu/XAffMv 2/WFWudHrzeUqMnhiR1cQBx4EQKWKHSCteVsp8yJ96nviqREPcIQp/3JqueAzLIyWcED ogDlcuziCH6sEysJHo6KFzqWKc6nMXWdTxbJY1xE9Ch/7cps4qs10dfxsZHpnkJQVLzC tCCw== X-Forwarded-Encrypted: i=1; AJvYcCW+M3IO7bu/3ovTANCidcfaicpxpDVotkf6MQ6rBxHcBVb7XxyKEAfToW37bX+tV5ZlYECnd0xCGA==@vger.kernel.org X-Gm-Message-State: AOJu0YyondsHVpkCDJ8zt5ELMmMvpD3usH4UjTXzSbW3V+U2z9XfV7H0 pET+lsGkG+UY6kdTrxxUFGUebsabGGClZkGhz+Hfr9HwxNx/076o/tPaxIC45Zh4IDIG8DXgbla mtKamMw== 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: linux-pm@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: 13882294 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 2FA5C1E2319 for ; Thu, 21 Nov 2024 18:53:48 +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=1732215230; cv=none; b=fSUj9NytRHRN+XbLRJahrZtzHsoDGbD7DRhdDh8v4R4eApcxrHZRC1jZzJZwKLBUFqAt1BjkE1ct6NDxW1tDDFGi6xqW4lZk0vZOU0Q/DQ/bg669lSmBEPVERKSAz+Mg/FNtWe90YkzLAaX1n17Ewy0b74ges+lX9MacjzTalJ8= 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.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="2Q3vNv8L" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6eebd4e7cc5so14918387b3.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=H+928U6QyfGGxv83jN1VQsVLX6E/SiGTg85mCcLbeGajy/WH9wGost1vjqTsb2+PoP 58h+aQbVcbE2YAV4x9HI3qijkjnETFFyvodXVDDi3yfKGkKZLOzqBOsqTc3tAmx3UUkz 2yISWrtN0YqiH+niARLD2pE9tcMgwc2SK1EfVANpu0zJYtiTsBP+T880wyMwuj1BqPmQ NJr55kXSQ8KXK2PJAgqFYVNefSQ672OJq2H/bP3PuHrEq92cib45eGtcIQ7eZduHtkCC WzJJUdmgEKilarjC/tqCnfe565yaLRd6Izd1f6UuOYnQ//FNjMItNoXCi8K2sZCABDBm m4gg== X-Forwarded-Encrypted: i=1; AJvYcCUtUvYmb4jGcDL2UNzj6F1E9e3hvcQn3oaLYGOHDHcC9YUbxeMGyBtCCCz/hPJtvuqLjUQzdX/lKQ==@vger.kernel.org X-Gm-Message-State: AOJu0YwQhdZWzhhJY1+QXLDwL2KkXm94o7fRJoss7+EIgniXXsBrED51 slQxToEh6R2W9vx5W+qCl6Ie3E/8wuNGfU4bVmHBWdR/M0aDy3cIWdyb+Dpf7gTTrsSvsl5CS7n 5m+gKYw== 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: linux-pm@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: 13882295 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 DA97E1DB943 for ; Thu, 21 Nov 2024 18:53:49 +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=1732215231; cv=none; b=WzCdWetLcBLmfsAnuq9/E99LYBUxiI+iu0qjFyLOtjhzESJ3TsDaVUUJN+rPEv0BXsSncXccS01HCsGRc3FngeEjoXATqiugN95Q1QF7X87VOeIxcuBW5uOKkhiSMBw445+ciKAmB/75+7SGxBMHXCZeJ88ocgFd4fdt6lzewdU= 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.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="uC6XDDIa" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6ea258fe4b6so27717667b3.1 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=GQflEdidMWDIE2nuG+Bt0OAMiIzKA6Tr5Km451pQkdPGjAgIWIgbj4R0M02TcvGD6K yF6587iRiU8tCkt7oe3zoy7vfYI9aY5xXlMefAIg0dwtmHi3A/P9rHWXKKy+RCNusDrB MIDiCABnIyD1bhe8gNtdz+n9L5prP6aN/NPWzLwGR/tPIFVbrbafC6qhfXF2HlawYT7I aYfeYCKW0XTk0T4k//92IO7kIkvqvu9SxpOTcgBcJWw08m4N8BC1jaBfuBL+MZ7xRVHk VR12PP9KPXMeyl5RFIfkgUOCBN6sPQSa0NnwCtoOb4sewV5I/OVzpnzwP4iTXoTjlhj5 ovUA== X-Forwarded-Encrypted: i=1; AJvYcCXjuAFGNjk+7+2tZ662AJUMJJG5njHQjVbAYU28ATvk7HdjaNm3tv5usMo3TNzNlEQY/AuZWQjlIA==@vger.kernel.org X-Gm-Message-State: AOJu0YxsktCmyav5pj5FesCHUYjvpeAu3CDosP8GCh3560BveqZ2r+eB qw/2jLkU4biw1a/2P7BNxDgT5rL1n2EHh/OlEX6LuqBU8piN92MBx4oDO+9Zq1PUVE0UR66Icyl MeTnEfQ== 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: linux-pm@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: 13882296 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 840FC1E284D 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-e38902a3200so2030786276.2 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=eHBZ+0Yt94LOqR+pMgft0Y9rtV2lKoMCG6CCFGm61CRT3CaI4xMDOttCD7k/hYkgbD Wl4xjYSn0sqO12frSAw9okwGdcVz5y/7ebNyh6o/oiJzeSW/biZpTNWPT/PcuXRVm91H PtcTIY1jaKun0qN6mqc3S9sdZo3c9eXWUCcUYX3c1mQq70rpyQgsbFg8ZKA8uR+VAN20 JfMFHTizB0QQcKpvFvoS88oyOUX5BkMse2hihiuPFEc1rDLIRxml5MdRz5AfvbRUGhM1 AL4FSk/hA259MIhpiIqlJLFwBAhlBFxX3B2RjgFuUMygD/RcSGLW3R3JMFG/evoocmoY EpVQ== X-Forwarded-Encrypted: i=1; AJvYcCWIspUKnDP4VsT8tigzmfBv2ndDK4GDw6kCS9sLLS3G8neoT0WLVk6NPhB3b+OzS/YKyKw3kx0eRQ==@vger.kernel.org X-Gm-Message-State: AOJu0Yz2twt/+zNtUSJM1V7ORglu2nFxoXQ4DLLRB5odDDWnDw7RVPws heQMKncwOvfhmr+36sCAv8yLBcaCk+/uu7TO0VKb2w+TrF0kLVjjG+4g9/yzdhPCB5c3bMkI/Lo uBp0ecg== 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: linux-pm@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: 13882297 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 DA2AF1E2823 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-7ea69eeb659so1185923a12.0 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=pPk5iS1a3fNnncYvd6ZN+Btoi0TNzLEbt1hkoNRTEparKoCkXYICCjMwVIEKSTlL3M gE+N1btL/O7ZELOKmEhdk+JT2om67XT5aza3KVBOKBf9isjn3F5S24SL6zdvjpvdf+bx VM7HoBsjF6W+JdauTD4gAVLSNngSyv139jtSYmbUcpwWJVCgYJeZROHdVKg3d0DOowfT WjYh3h1zdQgoVwgoKOnnWhd6+KabUvK3WuuDzVAwsvoLe3pCyVcXsGRzQVYZnVPC6MC2 09OzzqxCwOIDpzdhAxtuk7JuUk0IM1pp3RgauMLijdOOM0SN/O3FnDAUOh9K8H+cj9qG N77A== X-Forwarded-Encrypted: i=1; AJvYcCUtAIr9MV8d8u3pnlylyw79Abx/0uhbGFO9cLrBXRuepWl95z9wInfXybMJ5y0Wed+Edp2Delhx4w==@vger.kernel.org X-Gm-Message-State: AOJu0Yz1Cl1x0Zp8xvWS5lYl9sVValbhLyH4ws4ujn/oY00UYe+M/IsX m1VpzO94Jpsh0upRxyYycIDY7Ua/3fz2kym9kmB/EmAuaPUAnlctAaVbFHbwyDoEX+T/EyayAyC jtBHMpA== 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: linux-pm@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: 13882298 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 73F841E3769 for ; Thu, 21 Nov 2024 18:53:54 +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=1732215238; cv=none; b=EznAYELWdIlAvifY19jAdGBAUL2PN3VCiIXAywXjjaLhDJohYgKemrwFOIyMhz+HEk0RQRVp/m+9IXasiW0XaDjRBAf8OfO5HBC61DSPNCQ9DEYA+cmW7CVeto3SNt5Q0gUeNtEAFpQNz84xNcjjKLCuFfbjl+gFbJK+fIeQUPI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732215238; c=relaxed/simple; bh=80AJRXxgHUR06s0FkZz93urmoI/O6TPv0yGQJeLF4Ws=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Cm0oC/Lalk1LkKvZ7DeLQRzpV2gdTKNyGOdRuNCaS89FIBNzRtGRdUIMl5Xgl4NXzse2e0YI+MfNEuLpfWEzReSW1gQ6Jf8o9gw2Vu7NAI7KaoDm1CXmRxgWvZi73z60jtoxWJQgB88XKNwQ116DHFtzyyM90PFb/OqUrq/2fV0= 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.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="pqMKSLaW" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-71e455defb9so1630812b3a.3 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=SpUiNqENanZ/segLM7BGNHXBxBhSfalqm1/nxpyeqUuarprKaRDub+Jw9U146ImMxQ c8MWkH6/3umXEx8SJRsPPgeEotdMOhgE3yUON8fXKzB+O72LL+nr3iivzUUpkGkdKEl5 LrwmJ3iJMiMRIIKneUuM9f/oFvwHGNJ+Z7jEiAbhcEjnp+WvbAnGLL3itgN0PebimcjL Lv2S0IXdNmiqEWjTVZRSZ28E7xZj9wGUlCfWuWxWuqDE8F5o5IVSVK2mcMPQV43lYvQc uSJvpyspXnHIZn1lCqAKwwX69NIQFsFf39Uun8H0grUqyu4PZQib5eivVBVTrAEel6nr yd3Q== X-Forwarded-Encrypted: i=1; AJvYcCWzXFLri83bt8QgBm95dvrepZP4dKeoHAJtaP847v08fgPPZCY2HAGeqT6liO1TjIZd/6qSiQiz5g==@vger.kernel.org X-Gm-Message-State: AOJu0YybX8F7JLI0nlvPWI186BL9e+m383YtiFcsKBRcCPS2esOVdhIb qs9/0Irc8HZjDr9TMpzh1CBYLkAe+A+cphDYxnrTa6olALBPsTsGgv8Ye5yRRl2e3l4jjvbgByo EyRpScw== 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: linux-pm@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: 13882299 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 17A461E4101 for ; Thu, 21 Nov 2024 18:53:56 +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=1732215239; cv=none; b=MlAhfVRJe8sKBiR+m97u42dyKyRg7tRwUvjSKpE8TulDXWgv10qBKkcWUQKlu3ST/MUCxZxJzYfTMp/sWzww9vz1tZuUD7gjDIlxlCrIqieMvn4KHNCx+QUReQNFxRY8QycVS2AXKonrAjWZSyAy+sphFVQOwap63SYQxxr+bTc= 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.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="fBvZSRdW" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e3884e5e828so2179582276.3 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=CphkirLlCYhOCpMEmBhcBCl0o1YEGeUOc3Ez55E7bQOiCk2ENuFHqasKvVtCHT+NxE AshhZvBI0wlBWBD2xHK0sCBK8A4SpYVluO3RxEXVzf/cfD5wmhxx4j4t4PKNWeRllTFs bZvhQEsMj/rBu23wVp2oAgg4925Fh/gIyBQkjCWhB1nbYN4Pa6tDADIep11KAb76QYKj wh++GI4RNtPr471IuJn1XwNviojhsPFjwdu127BsK5T6w+RYMLVUFA2YVfVUt42DwTzm o2Oy6/xOZ61s7bXHYyGEGrloQgp5d9EK9v9sldkP8wxTFtWQtmUyeyp/ZL8+hpl8hGfI YluQ== X-Forwarded-Encrypted: i=1; AJvYcCXlkzAwW4eCCA/13GeZYa66ZUGNhawnM2v9ohUw+4/IkJlt013txllJmEck548nm5sgIi798D9qPg==@vger.kernel.org X-Gm-Message-State: AOJu0Yzbz67XR7XcODNA+Xx8wCfboG/cC6WDEw/YQTAag1TXqSRkPGq5 0SQvYZSAN2lkBRJjcQfHUY3Yu/7PRbf3jXWNyVf7PCN9PRmL9eamNnDCDKRV4SXQqpKlyOUCIcS pHxN/CQ== 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: linux-pm@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. */