Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Sean Christopherson" <seanjc@google.com>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH v4 13/30] KVM: x86: Expose several TSC helpers via x86.h for use by MSR code
Date: Sat, 13 Jun 2026 00:16:40 +0000	[thread overview]
Message-ID: <20260613001641.040611F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260613000329.732085-14-seanjc@google.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Lockless iteration of `vm_list` in `kvm_arch_enable_virtualization_cpu` during CPU hotplug.
- [High] Lockless cross-CPU modification of `vcpu->arch.tsc_offset_adjustment`.
--

commit ab7eedcd233e127b5aac182b220a36fa043a7767
Author: Sean Christopherson <seanjc@google.com>
Date:   Sat Jun 13 00:03:12 2026 +0000

KVM: x86: Expose several TSC helpers via x86.h for use by MSR code

Begrudgingly move adjust_tsc_offset_{guest,host}() to x86.h as inlines,
and expose several other TSC helpers in anticipation of moving KVM's MSR
code to a dedicated msrs.c. Unfortunately for KVM, several MSRs that KVM
emulates can affect TSC state.

Opportunistically drop a superfluous local "tsc_offset" variable, whose
existence causes checkpatch to complain about lack of a blank line.

No functional change intended.

> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index ece8d53fb7fe6..316ec7a57f7dd 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
[ ... ]
> @@ -2448,22 +2448,6 @@ static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 *user_value)
>  	raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
>  }
>  
> -static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
> -					   s64 adjustment)
> -{
> -	u64 tsc_offset = vcpu->arch.l1_tsc_offset;
> -	kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment);
> -}
> -
> -static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
> -{
> -	if (vcpu->arch.l1_tsc_scaling_ratio != kvm_caps.default_tsc_scaling_ratio)
> -		WARN_ON(adjustment < 0);
> -	adjustment = kvm_scale_tsc((u64) adjustment,
> -				   vcpu->arch.l1_tsc_scaling_ratio);
> -	adjust_tsc_offset_guest(vcpu, adjustment);
> -}
> -
>  #ifdef CONFIG_X86_64
>  
>  static u64 read_tsc(void)

[Severity: High]
This isn't a bug introduced by this patch, but while reviewing these TSC
offset adjustments I noticed a race condition in how
vcpu->arch.tsc_offset_adjustment is updated during CPU hotplug.

In kvm_arch_enable_virtualization_cpu(), there is a lockless cross-CPU
modification of vcpu->arch.tsc_offset_adjustment:

arch/x86/kvm/x86.c:kvm_arch_enable_virtualization_cpu()
	if (backwards_tsc) {
		u64 delta_cyc = max_tsc - local_tsc;
		list_for_each_entry(kvm, &vm_list, vm_list) {
			kvm->arch.backwards_tsc_observed = true;
			kvm_for_each_vcpu(i, vcpu, kvm) {
				vcpu->arch.tsc_offset_adjustment += delta_cyc;

Since CPU hotplug only pauses the CPU being brought online, could the
targeted VCPU be concurrently executing kvm_arch_vcpu_load() on another CPU?

If kvm_arch_vcpu_load() reads and clears this field concurrently without
vcpu->mutex, could the adjustment be lost or applied twice and lead to
incorrect guest TSC values?

[Severity: High]
This is a pre-existing issue, but I also noticed that the iteration over
vm_list in kvm_arch_enable_virtualization_cpu() is lockless:

arch/x86/kvm/x86.c:kvm_arch_enable_virtualization_cpu()
	list_for_each_entry(kvm, &vm_list, vm_list) {
		kvm_for_each_vcpu(i, vcpu, kvm) {

If a user concurrently creates or destroys a VM (which modifies vm_list
under kvm_lock), couldn't this lockless list_for_each_entry() dereference
poisoned pointers from list_del() during destruction, leading to a kernel
panic?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260613000329.732085-1-seanjc@google.com?part=13

  reply	other threads:[~2026-06-13  0:16 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-13  0:02 [PATCH v4 00/30] KVM: x86: x86.{c,h} spring cleaning Sean Christopherson
2026-06-13  0:03 ` [PATCH v4 01/30] KVM: x86: Extract REGS and SREGS runtime sync code to helpers Sean Christopherson
2026-06-15  2:16   ` Huang, Kai
2026-06-15  5:02   ` Binbin Wu
2026-06-13  0:03 ` [PATCH v4 02/30] KVM: x86: Move get_segment_base() to regs.h, as kvm_get_segment_base() Sean Christopherson
2026-06-15  2:43   ` Huang, Kai
2026-06-15  5:03   ` Binbin Wu
2026-06-13  0:03 ` [PATCH v4 03/30] KVM: x86: Rename __{g,s}et_sregs2() => kvm_x86_vcpu_ioctl_{g,s}et_sregs2() Sean Christopherson
2026-06-15  2:46   ` Huang, Kai
2026-06-15  5:13   ` Binbin Wu
2026-06-15 15:58     ` Sean Christopherson
2026-06-13  0:03 ` [PATCH v4 04/30] KVM: x86: Move the bulk of register specific code from x86.c to regs.c Sean Christopherson
2026-06-15  5:25   ` Binbin Wu
2026-06-13  0:03 ` [PATCH v4 05/30] KVM: x86: Move local APIC specific helpers out of asm/kvm_host.h Sean Christopherson
2026-06-15  5:47   ` Binbin Wu
2026-06-15 16:06     ` Sean Christopherson
2026-06-13  0:03 ` [PATCH v4 06/30] KVM: x86: Move kvm_caps and kvm_host_values to asm/kvm_host.h Sean Christopherson
2026-06-13  9:01   ` Xiaoyao Li
2026-06-15  6:49     ` Binbin Wu
2026-06-15 16:24       ` Sean Christopherson
2026-06-13  0:03 ` [PATCH v4 07/30] KVM: x86: Swap the include order between x86.h and mmu.h Sean Christopherson
2026-06-15  7:26   ` Binbin Wu
2026-06-13  0:03 ` [PATCH v4 08/30] KVM: x86: Move tdp_enabled from kvm_host.h to mmu.h Sean Christopherson
2026-06-15  7:33   ` Binbin Wu
2026-06-13  0:03 ` [PATCH v4 09/30] KVM: x86: Move eager_page_split to mmu.{c,h} Sean Christopherson
2026-06-15  7:49   ` Binbin Wu
2026-06-13  0:03 ` [PATCH v4 10/30] KVM: x86/hyperv: Eliminate an unnecessary include of x86.h in hyperv.h Sean Christopherson
2026-06-15  7:52   ` Binbin Wu
2026-06-13  0:03 ` [PATCH v4 11/30] KVM: x86: Move kvm_{load,put}_guest_fpu() to fpu.h Sean Christopherson
2026-06-15  8:13   ` Binbin Wu
2026-06-15 16:31     ` Sean Christopherson
2026-06-13  0:03 ` [PATCH v4 12/30] KVM: x86: Extract get/set MSR (list) ioctl logic to helpers Sean Christopherson
2026-06-15  8:30   ` Binbin Wu
2026-06-13  0:03 ` [PATCH v4 13/30] KVM: x86: Expose several TSC helpers via x86.h for use by MSR code Sean Christopherson
2026-06-13  0:16   ` sashiko-bot [this message]
2026-06-13  0:03 ` [PATCH v4 14/30] KVM: x86: Move the bulk of MSR specific code from x86.c to msrs.{c,h} Sean Christopherson
2026-06-15  9:30   ` Binbin Wu
2026-06-13  0:03 ` [PATCH v4 15/30] KVM: x86: Move register helper declarations from kvm_host.h => regs.h Sean Christopherson
2026-06-13  0:03 ` [PATCH v4 16/30] KVM: x86: Move kvm_{g,s}et_segment() to inline helpers in regs.h Sean Christopherson
2026-06-13  0:03 ` [PATCH v4 17/30] KVM: x86: Move MSR helper declarations from kvm_host.h => msrs.h Sean Christopherson
2026-06-13  0:03 ` [PATCH v4 18/30] KVM: x86: Move "struct kvm_x86_msr_filter" definition to msrs.c Sean Christopherson
2026-06-15  2:47   ` Huang, Kai
2026-06-13  0:03 ` [PATCH v4 19/30] KVM: x86/pmu: Move "struct kvm_x86_pmu_event_filter" definition to pmu.c Sean Christopherson
2026-06-15  2:48   ` Huang, Kai
2026-06-13  0:03 ` [PATCH v4 20/30] KVM: x86: Move MMU helper declarations from kvm_host.h => mmu.h Sean Christopherson
2026-06-13  0:03 ` [PATCH v4 21/30] KVM: x86: Move LLDT assembly wrappers into VMX Sean Christopherson
2026-06-13  0:03 ` [PATCH v4 22/30] KVM: x86: Move misc "VALID MASK" defines from kvm_host.h => x86.c Sean Christopherson
2026-06-13  0:03 ` [PATCH v4 23/30] KVM: x86: Move __kvm_irq_line_state() from kvm_host.h => ioapic.h Sean Christopherson
2026-06-13  0:03 ` [PATCH v4 24/30] KVM: x86: Move IRQ-related helper declarations from kvm_host.h => irq.h Sean Christopherson
2026-06-15 11:55   ` Huang, Kai
2026-06-13  0:03 ` [PATCH v4 25/30] KVM: x86: Move kvm_pv_send_ipi() declaration from kvm_host.h => lapic.h Sean Christopherson
2026-06-13  0:03 ` [PATCH v4 26/30] KVM: x86: Don't treat interrupts as allowed just because a nested run is pending Sean Christopherson
2026-06-15 16:40   ` Yosry Ahmed
2026-06-15 16:43     ` Yosry Ahmed
2026-06-15 17:03       ` Sean Christopherson
2026-06-15 19:37         ` Yosry Ahmed
2026-06-15 17:26     ` Sean Christopherson
2026-06-15 19:48       ` Yosry Ahmed
2026-06-13  0:03 ` [PATCH v4 27/30] KVM: x86: Rework kvm_arch_interrupt_allowed() into kvm_is_interrupt_allowed() Sean Christopherson
2026-06-13  0:03 ` [PATCH v4 28/30] KVM: x86/mmu: Move kvm_arch_async_page_ready() below kvm_tdp_page_fault() Sean Christopherson
2026-06-13  0:03 ` [PATCH v4 29/30] KVM: x86/mmu: Move kvm_mmu_do_page_fault() from mmu_internal.h => mmu.c Sean Christopherson
2026-06-13  0:03 ` [PATCH v4 30/30] KVM: x86: Move a pile of stuff from kvm_host.h => x86.h Sean Christopherson
2026-06-15 13:01   ` Huang, Kai
2026-06-15 14:23     ` Sean Christopherson

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=20260613001641.040611F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=seanjc@google.com \
    /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