From: Suleiman Souhlal <suleiman@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>,
Sean Christopherson <seanjc@google.com>
Cc: Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
Chao Gao <chao.gao@intel.com>,
David Woodhouse <dwmw2@infradead.org>,
Sergey Senozhatsky <senozhatsky@chromium.org>,
Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>,
Tzung-Bi Shih <tzungbi@kernel.org>,
John Stultz <jstultz@google.com>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
ssouhlal@freebsd.org, Suleiman Souhlal <suleiman@google.com>
Subject: [PATCH v6 1/3] KVM: x86: Advance guest TSC after deep suspend.
Date: Wed, 9 Jul 2025 16:04:48 +0900 [thread overview]
Message-ID: <20250709070450.473297-2-suleiman@google.com> (raw)
In-Reply-To: <20250709070450.473297-1-suleiman@google.com>
Advance guest TSC to current time after suspend when the host
TSCs went backwards.
This makes the behavior consistent between suspends where host TSC
resets and suspends where it doesn't, such as suspend-to-idle, where
in the former case if the host TSC resets, the guests' would
previously be "frozen" due to KVM's backwards TSC prevention, while
in the latter case they would advance.
Suggested-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Suleiman Souhlal <suleiman@google.com>
---
arch/x86/include/asm/kvm_host.h | 1 +
arch/x86/kvm/x86.c | 28 +++++++++++++++++++++++++++-
2 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 639d9bcee8424d..5c465bdd6d088a 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1414,6 +1414,7 @@ struct kvm_arch {
u64 cur_tsc_offset;
u64 cur_tsc_generation;
int nr_vcpus_matched_tsc;
+ bool host_was_suspended;
u32 default_tsc_khz;
bool user_set_tsc;
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index a9d992d5652fa0..e66bab1a1f56e2 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -5035,7 +5035,32 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
/* Apply any externally detected TSC adjustments (due to suspend) */
if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
- adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
+ unsigned long flags;
+ struct kvm *kvm;
+ bool advance;
+ u64 kernel_ns, l1_tsc, offset, tsc_now;
+
+ kvm = vcpu->kvm;
+ advance = kvm_get_time_and_clockread(&kernel_ns, &tsc_now);
+ raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
+ /*
+ * Advance the guest's TSC to current time instead of only
+ * preventing it from going backwards, while making sure
+ * all the vCPUs use the same offset.
+ */
+ if (kvm->arch.host_was_suspended && advance) {
+ l1_tsc = nsec_to_cycles(vcpu,
+ kvm->arch.kvmclock_offset + kernel_ns);
+ offset = kvm_compute_l1_tsc_offset(vcpu, l1_tsc);
+ kvm->arch.cur_tsc_offset = offset;
+ kvm_vcpu_write_tsc_offset(vcpu, offset);
+ } else if (advance)
+ kvm_vcpu_write_tsc_offset(vcpu, kvm->arch.cur_tsc_offset);
+ } else {
+ adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
+ }
+ kvm->arch.host_was_suspended = false;
+ raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
vcpu->arch.tsc_offset_adjustment = 0;
kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
}
@@ -12729,6 +12754,7 @@ int kvm_arch_enable_virtualization_cpu(void)
kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
}
+ kvm->arch.host_was_suspended = true;
/*
* We have to disable TSC offset matching.. if you were
* booting a VM while issuing an S4 host suspend....
--
2.50.0.727.gbf7dc18ff4-goog
next prev parent reply other threads:[~2025-07-09 7:05 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-09 7:04 [PATCH v6 0/3] KVM: x86: Include host suspended time in steal time Suleiman Souhlal
2025-07-09 7:04 ` Suleiman Souhlal [this message]
2025-07-09 21:58 ` [PATCH v6 1/3] KVM: x86: Advance guest TSC after deep suspend kernel test robot
2025-07-10 1:25 ` kernel test robot
2025-07-09 7:04 ` [PATCH v6 2/3] KVM: x86: Include host suspended duration in steal time Suleiman Souhlal
2025-07-09 7:04 ` [PATCH v6 3/3] KVM: x86: Add "suspendsteal" cmdline to request host to add suspend " Suleiman Souhlal
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250709070450.473297-2-suleiman@google.com \
--to=suleiman@google.com \
--cc=bp@alien8.de \
--cc=chao.gao@intel.com \
--cc=dave.hansen@linux.intel.com \
--cc=dwmw2@infradead.org \
--cc=hpa@zytor.com \
--cc=jstultz@google.com \
--cc=konrad.wilk@oracle.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=senozhatsky@chromium.org \
--cc=ssouhlal@freebsd.org \
--cc=tglx@linutronix.de \
--cc=tzungbi@kernel.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).