Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Jun Miao <jun.miao@intel.com>
To: seanjc@google.com, pbonzini@redhat.com, tglx@kernel.org,
	mingo@redhat.com, dave.hansen@linux.intel.com
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, jun.miao@intel.com
Subject: [PATCH] KVM: x86: Block TSC multiplier writes for protected guest TSC
Date: Tue, 12 May 2026 07:18:30 -0400	[thread overview]
Message-ID: <20260512111830.1295437-1-jun.miao@intel.com> (raw)

Commit d5a4e408e69b ("KVM: x86: Add infrastructure for secure TSC")
added the guest_tsc_protected flag to prevent KVM from changing the
TSC offset/multiplier of vCPUs whose TSC is managed by a confidential
computing module (e.g. TDX, SEV-SNP Secure TSC).  However only the TSC
offset write path was guarded; kvm_vcpu_write_tsc_multiplier() was
left unprotected.

As a result, userspace can still change the TSC scaling ratio of a
TDX vCPU via the KVM_SET_TSC_KHZ ioctl path:

  KVM_SET_TSC_KHZ
    -> kvm_arch_vcpu_ioctl()
    -> kvm_set_tsc_khz()
    -> set_tsc_khz()
    -> kvm_vcpu_write_tsc_multiplier()   <-- not guarded

and similarly during kvm_arch_vcpu_create() -> kvm_set_tsc_khz()
which can reset the multiplier to default_tsc_scaling_ratio.

Make kvm_vcpu_write_tsc_multiplier() symmetric with
kvm_vcpu_write_tsc_offset() by skipping the update when
guest_tsc_protected is set.  This single chokepoint covers all
existing callers (set_tsc_khz() in both ioctl and vCPU create paths).

Fixes: d5a4e408e69b ("KVM: x86: Add infrastructure for secure TSC")
Signed-off-by: Jun Miao <jun.miao@intel.com>
---
 arch/x86/kvm/x86.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 0a1b63c63d1a..e935fe33d9c2 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -2736,6 +2736,9 @@ static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 l1_offset)
 
 static void kvm_vcpu_write_tsc_multiplier(struct kvm_vcpu *vcpu, u64 l1_multiplier)
 {
+	if (vcpu->arch.guest_tsc_protected)
+		return;
+
 	vcpu->arch.l1_tsc_scaling_ratio = l1_multiplier;
 
 	/* Userspace is changing the multiplier while L2 is active */
-- 
2.43.0


             reply	other threads:[~2026-05-11 13:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-12 11:18 Jun Miao [this message]
2026-05-12  1:12 ` [PATCH] KVM: x86: Block TSC multiplier writes for protected guest TSC Huang, Kai
2026-05-12  1:29   ` Miao, Jun

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=20260512111830.1295437-1-jun.miao@intel.com \
    --to=jun.miao@intel.com \
    --cc=dave.hansen@linux.intel.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=tglx@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