Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities
@ 2026-12-04 18:03 Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 01/24] ptrace: Convert ptrace_attach() to use lock guards Paolo Bonzini
                   ` (24 more replies)
  0 siblings, 25 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:03 UTC (permalink / raw)
  To: linux-kernel, kvm

Loongarch:
- Flush TLBs *before* dropping mmu_lock

RISC-V:
- Flush TLBs *before* dropping mmu_lock

x86:
- Flush TLB *before* dropping mmu_lock, and flush them entirely
- Flush TLBs if child SPTE is zapped when splitting a huge SPTE
- Pin kvm->mm prior to borrowing mm when setting event channel
- Disable hypervisor (VMX Preemption) timer if TSC deadline wraps
- Reset local APIC timer metadata on RESET, INIT, and KVM_SET_LAPIC
- Ensure guest FPU is resident in hardware when updating actual XFD MSR
- Ensure pCPU is valid before sending IPI to deliver posted IRQ

Generic:
- Invalidate all GFN=>PFN caches when VM and/or its process is dying

Paolo

Hou Wenlong (3):
  KVM: x86/mmu: Move round_gfn_for_level() helper into mmu_internal.h
  KVM: x86/mmu: Fix wrong gfn range of tlb flushing in
    kvm_set_pte_rmapp()
  KVM: x86/mmu: Fix wrong start gfn of tlb flushing with range

Jun Miao (1):
  KVM: Fix comments that refer to the non-existent
    install_new_memslots()

Kyle Zeng (1):
  KVM: x86/mmu: Flush all TLB entries, not a precise range, when
    yielding

Peter Zijlstra (1):
  ptrace: Convert ptrace_attach() to use lock guards

Sean Christopherson (18):
  KVM: x86: Grab lapic_timer in a local variable to cleanup periodic
    code
  KVM: x86/mmu: Flush TLBs if child SPTE is zapped when splitting a huge
    SPTE
  KVM: x86/mmu: Propagate the return from kvm_zap_all_rmap_sptes() out
    of __rmap_add()
  KVM: x86: Disable hypervisor (VMX Preemption) timer if TSC deadline
    wraps
  KVM: x86: Reset local APIC timer metadata on RESET, INIT, and
    KVM_SET_LAPIC
  KVM: VMX: Retry APIC-access page reload if invalidation is in-progress
  KVM: x86: Use standard mmu_notifier invalidate hooks for APIC access
    page
  KVM: x86/mmu: Flush TLBs in kvm_unmap_gfn_range() if the unmap may
    block/yield
  KVM: x86/xen: Pin kvm->mm prior to borrowing mm when setting event
    channel
  KVM: Fold kvm_flush_shadow_all() into kvm_mmu_notifier_release()
  KVM: WARN if there are dangling MMU invalidations at VM destruction
  KVM: WARN if there are in-flight memslot updates when a VM's process
    exits
  KVM: Set mn_active_invalidate_count when the VM's mm is released
  KVM: Invalidate all GFN=>PFN caches when VM and/or its process is
    dying
  x86/fpu: Bail early from guest XFD update if XFD isn't supported
  x86/fpu: Clear XSTATE_BV[i] in guest XSAVE state if guest FPU isn't
    in-use
  x86/fpu: Ensure guest FPU is resident in hardware when updating actual
    XFD MSR
  KVM: VMX: Ensure pCPU is valid before sending IPI to deliver posted
    IRQ

 Documentation/virt/kvm/locking.rst |   2 +-
 arch/x86/kernel/fpu/core.c         |  37 ++++---
 arch/x86/kvm/lapic.c               |  79 ++++++++++-----
 arch/x86/kvm/mmu/mmu.c             |  75 ++++++++++----
 arch/x86/kvm/mmu/mmu_internal.h    |  14 +++
 arch/x86/kvm/mmu/paging_tmpl.h     |   5 +-
 arch/x86/kvm/mmu/tdp_iter.c        |  11 +--
 arch/x86/kvm/vmx/vmx.c             |  53 ++++++++--
 arch/x86/kvm/x86.c                 |  16 +--
 arch/x86/kvm/xen.c                 |   7 +-
 include/linux/kvm_host.h           |   7 +-
 include/linux/sched/task.h         |   2 +
 include/linux/spinlock.h           |  26 +++++
 kernel/ptrace.c                    | 154 ++++++++++++++---------------
 virt/kvm/kvm_main.c                |  78 +++++++++------
 virt/kvm/kvm_mm.h                  |   2 +
 virt/kvm/pfncache.c                |  12 +++
 17 files changed, 374 insertions(+), 206 deletions(-)

-- 
2.56.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 01/24] ptrace: Convert ptrace_attach() to use lock guards
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 02/24] KVM: x86: Grab lapic_timer in a local variable to cleanup periodic code Paolo Bonzini
                   ` (23 subsequent siblings)
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: Peter Zijlstra, Oleg Nesterov, Sasha Levin

From: Peter Zijlstra <peterz@infradead.org>

[ Upstream commit 5431fdd2c181dd2eac218e45b44deb2925fa48f0 ]

Created as testing for the conditional guard infrastructure.
Specifically this makes use of the following form:

  scoped_cond_guard (mutex_intr, return -ERESTARTNOINTR,
		     &task->signal->cred_guard_mutex) {
    ...
  }
  ...
  return 0;

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Oleg Nesterov <oleg@redhat.com>
Link: https://lkml.kernel.org/r/20231102110706.568467727%40infradead.org
Stable-dep-of: 60a1969fae62 ("ALSA: seq: Serialize UMP output teardown with event_input")
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 include/linux/sched/task.h |   2 +
 include/linux/spinlock.h   |  26 +++++++
 kernel/ptrace.c            | 154 ++++++++++++++++++-------------------
 3 files changed, 102 insertions(+), 80 deletions(-)

diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h
index 00135a8d5773..490a954f96b2 100644
--- a/include/linux/sched/task.h
+++ b/include/linux/sched/task.h
@@ -211,4 +211,6 @@ static inline void task_unlock(struct task_struct *p)
 	spin_unlock(&p->alloc_lock);
 }
 
+DEFINE_GUARD(task_lock, struct task_struct *, task_lock(_T), task_unlock(_T))
+
 #endif /* _LINUX_SCHED_TASK_H */
diff --git a/include/linux/spinlock.h b/include/linux/spinlock.h
index 6a4c7cee005c..8ee78d57e294 100644
--- a/include/linux/spinlock.h
+++ b/include/linux/spinlock.h
@@ -539,5 +539,31 @@ DEFINE_LOCK_GUARD_1(spinlock_irqsave, spinlock_t,
 DEFINE_LOCK_GUARD_1_COND(spinlock_irqsave, _try,
 			 spin_trylock_irqsave(_T->lock, _T->flags))
 
+DEFINE_LOCK_GUARD_1(read_lock, rwlock_t,
+		    read_lock(_T->lock),
+		    read_unlock(_T->lock))
+
+DEFINE_LOCK_GUARD_1(read_lock_irq, rwlock_t,
+		    read_lock_irq(_T->lock),
+		    read_unlock_irq(_T->lock))
+
+DEFINE_LOCK_GUARD_1(read_lock_irqsave, rwlock_t,
+		    read_lock_irqsave(_T->lock, _T->flags),
+		    read_unlock_irqrestore(_T->lock, _T->flags),
+		    unsigned long flags)
+
+DEFINE_LOCK_GUARD_1(write_lock, rwlock_t,
+		    write_lock(_T->lock),
+		    write_unlock(_T->lock))
+
+DEFINE_LOCK_GUARD_1(write_lock_irq, rwlock_t,
+		    write_lock_irq(_T->lock),
+		    write_unlock_irq(_T->lock))
+
+DEFINE_LOCK_GUARD_1(write_lock_irqsave, rwlock_t,
+		    write_lock_irqsave(_T->lock, _T->flags),
+		    write_unlock_irqrestore(_T->lock, _T->flags),
+		    unsigned long flags)
+
 #undef __LINUX_INSIDE_SPINLOCK_H
 #endif /* __LINUX_SPINLOCK_H */
diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index 435848809ebc..2dac1cb42833 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -395,71 +395,9 @@ static int check_ptrace_options(unsigned long data)
 	return 0;
 }
 
-static int ptrace_attach(struct task_struct *task, long request,
-			 unsigned long addr,
-			 unsigned long flags)
+static inline void ptrace_set_stopped(struct task_struct *task)
 {
-	bool seize = (request == PTRACE_SEIZE);
-	int retval;
-
-	retval = -EIO;
-	if (seize) {
-		if (addr != 0)
-			goto out;
-		/*
-		 * This duplicates the check in check_ptrace_options() because
-		 * ptrace_attach() and ptrace_setoptions() have historically
-		 * used different error codes for unknown ptrace options.
-		 */
-		if (flags & ~(unsigned long)PTRACE_O_MASK)
-			goto out;
-		retval = check_ptrace_options(flags);
-		if (retval)
-			return retval;
-		flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT);
-	} else {
-		flags = PT_PTRACED;
-	}
-
-	audit_ptrace(task);
-
-	retval = -EPERM;
-	if (unlikely(task->flags & PF_KTHREAD))
-		goto out;
-	if (same_thread_group(task, current))
-		goto out;
-
-	/*
-	 * Protect exec's credential calculations against our interference;
-	 * SUID, SGID and LSM creds get determined differently
-	 * under ptrace.
-	 */
-	retval = -ERESTARTNOINTR;
-	if (mutex_lock_interruptible(&task->signal->cred_guard_mutex))
-		goto out;
-
-	task_lock(task);
-	retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS);
-	task_unlock(task);
-	if (retval)
-		goto unlock_creds;
-
-	write_lock_irq(&tasklist_lock);
-	retval = -EPERM;
-	if (unlikely(task->exit_state))
-		goto unlock_tasklist;
-	if (task->ptrace)
-		goto unlock_tasklist;
-
-	task->ptrace = flags;
-
-	ptrace_link(task, current);
-
-	/* SEIZE doesn't trap tracee on attach */
-	if (!seize)
-		send_sig_info(SIGSTOP, SEND_SIG_PRIV, task);
-
-	spin_lock(&task->sighand->siglock);
+	guard(spinlock)(&task->sighand->siglock);
 
 	/*
 	 * If the task is already STOPPED, set JOBCTL_TRAP_STOP and
@@ -483,28 +421,84 @@ static int ptrace_attach(struct task_struct *task, long request,
 		task->jobctl &= ~JOBCTL_STOPPED;
 		signal_wake_up_state(task, __TASK_STOPPED);
 	}
+}
 
-	spin_unlock(&task->sighand->siglock);
+static int ptrace_attach(struct task_struct *task, long request,
+			 unsigned long addr,
+			 unsigned long flags)
+{
+	bool seize = (request == PTRACE_SEIZE);
+	int retval;
 
-	retval = 0;
-unlock_tasklist:
-	write_unlock_irq(&tasklist_lock);
-unlock_creds:
-	mutex_unlock(&task->signal->cred_guard_mutex);
-out:
-	if (!retval) {
+	if (seize) {
+		if (addr != 0)
+			return -EIO;
 		/*
-		 * We do not bother to change retval or clear JOBCTL_TRAPPING
-		 * if wait_on_bit() was interrupted by SIGKILL. The tracer will
-		 * not return to user-mode, it will exit and clear this bit in
-		 * __ptrace_unlink() if it wasn't already cleared by the tracee;
-		 * and until then nobody can ptrace this task.
+		 * This duplicates the check in check_ptrace_options() because
+		 * ptrace_attach() and ptrace_setoptions() have historically
+		 * used different error codes for unknown ptrace options.
 		 */
-		wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT, TASK_KILLABLE);
-		proc_ptrace_connector(task, PTRACE_ATTACH);
+		if (flags & ~(unsigned long)PTRACE_O_MASK)
+			return -EIO;
+
+		retval = check_ptrace_options(flags);
+		if (retval)
+			return retval;
+		flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT);
+	} else {
+		flags = PT_PTRACED;
 	}
 
-	return retval;
+	audit_ptrace(task);
+
+	if (unlikely(task->flags & PF_KTHREAD))
+		return -EPERM;
+	if (same_thread_group(task, current))
+		return -EPERM;
+
+	/*
+	 * Protect exec's credential calculations against our interference;
+	 * SUID, SGID and LSM creds get determined differently
+	 * under ptrace.
+	 */
+	scoped_cond_guard (mutex_intr, return -ERESTARTNOINTR,
+			   &task->signal->cred_guard_mutex) {
+
+		scoped_guard (task_lock, task) {
+			retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS);
+			if (retval)
+				return retval;
+		}
+
+		scoped_guard (write_lock_irq, &tasklist_lock) {
+			if (unlikely(task->exit_state))
+				return -EPERM;
+			if (task->ptrace)
+				return -EPERM;
+
+			task->ptrace = flags;
+
+			ptrace_link(task, current);
+
+			/* SEIZE doesn't trap tracee on attach */
+			if (!seize)
+				send_sig_info(SIGSTOP, SEND_SIG_PRIV, task);
+
+			ptrace_set_stopped(task);
+		}
+	}
+
+	/*
+	 * We do not bother to change retval or clear JOBCTL_TRAPPING
+	 * if wait_on_bit() was interrupted by SIGKILL. The tracer will
+	 * not return to user-mode, it will exit and clear this bit in
+	 * __ptrace_unlink() if it wasn't already cleared by the tracee;
+	 * and until then nobody can ptrace this task.
+	 */
+	wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT, TASK_KILLABLE);
+	proc_ptrace_connector(task, PTRACE_ATTACH);
+
+	return 0;
 }
 
 /**
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 02/24] KVM: x86: Grab lapic_timer in a local variable to cleanup periodic code
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 01/24] ptrace: Convert ptrace_attach() to use lock guards Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 03/24] KVM: x86/mmu: Move round_gfn_for_level() helper into mmu_internal.h Paolo Bonzini
                   ` (22 subsequent siblings)
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: Sean Christopherson

From: Sean Christopherson <seanjc@google.com>

[ Upstream commit a091fe60c2d3943b058132a64682a509d55bd325 ]

Stash apic->lapic_timer in a local "ktimer" variable in
advance_periodic_target_expiration() to eliminate a few unaligned wraps,
and to make the code easier to read overall.

No functional change intended.

Link: https://patch.msgid.link/20251113205114.1647493-5-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/lapic.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index bfac2a1ea066..ad98e4f725e6 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -1886,6 +1886,7 @@ static bool set_target_expiration(struct kvm_lapic *apic, u32 count_reg)
 
 static void advance_periodic_target_expiration(struct kvm_lapic *apic)
 {
+	struct kvm_timer *ktimer = &apic->lapic_timer;
 	ktime_t now = ktime_get();
 	u64 tscl = rdtsc();
 	ktime_t delta;
@@ -1897,9 +1898,8 @@ static void advance_periodic_target_expiration(struct kvm_lapic *apic)
 	 * over time as differences in the periods accumulate, e.g. due to
 	 * differences in the underlying clocks or numerical approximation errors.
 	 */
-	apic->lapic_timer.target_expiration =
-		ktime_add_ns(apic->lapic_timer.target_expiration,
-				apic->lapic_timer.period);
+	ktimer->target_expiration = ktime_add_ns(ktimer->target_expiration,
+						 ktimer->period);
 
 	/*
 	 * If the new expiration is in the past, e.g. because userspace stopped
@@ -1910,17 +1910,17 @@ static void advance_periodic_target_expiration(struct kvm_lapic *apic)
 	 * past will do nothing more than waste host cycles, and can even lead
 	 * to a hard lockup in extreme cases.
 	 */
-	if (ktime_before(apic->lapic_timer.target_expiration, now))
-		apic->lapic_timer.target_expiration = now;
+	if (ktime_before(ktimer->target_expiration, now))
+		ktimer->target_expiration = now;
 
 	/*
 	 * Note, ensuring the expiration isn't in the past also prevents delta
 	 * from going negative, which could cause the TSC deadline to become
 	 * excessively large due to it an unsigned value.
 	 */
-	delta = ktime_sub(apic->lapic_timer.target_expiration, now);
-	apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
-		nsec_to_cycles(apic->vcpu, delta);
+	delta = ktime_sub(ktimer->target_expiration, now);
+	ktimer->tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
+			      nsec_to_cycles(apic->vcpu, delta);
 }
 
 static void start_sw_period(struct kvm_lapic *apic)
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 03/24] KVM: x86/mmu: Move round_gfn_for_level() helper into mmu_internal.h
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 01/24] ptrace: Convert ptrace_attach() to use lock guards Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 02/24] KVM: x86: Grab lapic_timer in a local variable to cleanup periodic code Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:22   ` sashiko-bot
  2026-12-04 18:04 ` [PATCH 6.1.y 04/24] KVM: x86/mmu: Fix wrong gfn range of tlb flushing in kvm_set_pte_rmapp() Paolo Bonzini
                   ` (21 subsequent siblings)
  24 siblings, 1 reply; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: Hou Wenlong, Sean Christopherson

From: Hou Wenlong <houwenlong.hwl@antgroup.com>

[ Upstream commit c667a3baeddcc982bf512c126aec8d0f04adecfe ]

Rounding down the GFN to a huge page size is a common pattern throughout
KVM, so move round_gfn_for_level() helper in tdp_iter.c to
mmu_internal.h for common usage. Also rename it as gfn_round_for_level()
to use gfn_* prefix and clean up the other call sites.

Signed-off-by: Hou Wenlong <houwenlong.hwl@antgroup.com>
Link: https://lore.kernel.org/r/415c64782f27444898db650e21cf28eeb6441dfa.1665214747.git.houwenlong.hwl@antgroup.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/mmu/mmu.c          |  5 +++--
 arch/x86/kvm/mmu/mmu_internal.h |  5 +++++
 arch/x86/kvm/mmu/paging_tmpl.h  |  2 +-
 arch/x86/kvm/mmu/tdp_iter.c     | 11 +++--------
 4 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 0a1318eee1ef..72a61480890e 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -3148,7 +3148,7 @@ static int direct_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
 		if (fault->nx_huge_page_workaround_enabled)
 			disallowed_hugepage_adjust(fault, *it.sptep, it.level);
 
-		base_gfn = fault->gfn & ~(KVM_PAGES_PER_HPAGE(it.level) - 1);
+		base_gfn = gfn_round_for_level(fault->gfn, it.level);
 		if (it.level == fault->goal_level)
 			break;
 
@@ -4375,7 +4375,8 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
 	if (shadow_memtype_mask && kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
 		for ( ; fault->max_level > PG_LEVEL_4K; --fault->max_level) {
 			int page_num = KVM_PAGES_PER_HPAGE(fault->max_level);
-			gfn_t base = (fault->addr >> PAGE_SHIFT) & ~(page_num - 1);
+			gfn_t base = gfn_round_for_level(fault->gfn,
+							 fault->max_level);
 
 			if (kvm_mtrr_check_gfn_range_consistency(vcpu, base, page_num))
 				break;
diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h
index 009024d42183..6d631af0b383 100644
--- a/arch/x86/kvm/mmu/mmu_internal.h
+++ b/arch/x86/kvm/mmu/mmu_internal.h
@@ -154,6 +154,11 @@ static inline bool kvm_mmu_page_ad_need_write_protect(struct kvm_mmu_page *sp)
 	return kvm_x86_ops.cpu_dirty_log_size && sp->role.guest_mode;
 }
 
+static inline gfn_t gfn_round_for_level(gfn_t gfn, int level)
+{
+	return gfn & -KVM_PAGES_PER_HPAGE(level);
+}
+
 int mmu_try_to_unsync_pages(struct kvm *kvm, const struct kvm_memory_slot *slot,
 			    gfn_t gfn, bool can_unsync, bool prefetch);
 
diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h
index 4ff63e273687..f79333aed51e 100644
--- a/arch/x86/kvm/mmu/paging_tmpl.h
+++ b/arch/x86/kvm/mmu/paging_tmpl.h
@@ -721,7 +721,7 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,
 		if (fault->nx_huge_page_workaround_enabled)
 			disallowed_hugepage_adjust(fault, *it.sptep, it.level);
 
-		base_gfn = fault->gfn & ~(KVM_PAGES_PER_HPAGE(it.level) - 1);
+		base_gfn = gfn_round_for_level(fault->gfn, it.level);
 		if (it.level == fault->goal_level)
 			break;
 
diff --git a/arch/x86/kvm/mmu/tdp_iter.c b/arch/x86/kvm/mmu/tdp_iter.c
index 39b48e7d7d1a..c4bb4abed6d0 100644
--- a/arch/x86/kvm/mmu/tdp_iter.c
+++ b/arch/x86/kvm/mmu/tdp_iter.c
@@ -15,11 +15,6 @@ static void tdp_iter_refresh_sptep(struct tdp_iter *iter)
 	iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep);
 }
 
-static gfn_t round_gfn_for_level(gfn_t gfn, int level)
-{
-	return gfn & -KVM_PAGES_PER_HPAGE(level);
-}
-
 /*
  * Return the TDP iterator to the root PT and allow it to continue its
  * traversal over the paging structure from there.
@@ -30,7 +25,7 @@ void tdp_iter_restart(struct tdp_iter *iter)
 	iter->yielded_gfn = iter->next_last_level_gfn;
 	iter->level = iter->root_level;
 
-	iter->gfn = round_gfn_for_level(iter->next_last_level_gfn, iter->level);
+	iter->gfn = gfn_round_for_level(iter->next_last_level_gfn, iter->level);
 	tdp_iter_refresh_sptep(iter);
 
 	iter->valid = true;
@@ -97,7 +92,7 @@ static bool try_step_down(struct tdp_iter *iter)
 
 	iter->level--;
 	iter->pt_path[iter->level - 1] = child_pt;
-	iter->gfn = round_gfn_for_level(iter->next_last_level_gfn, iter->level);
+	iter->gfn = gfn_round_for_level(iter->next_last_level_gfn, iter->level);
 	tdp_iter_refresh_sptep(iter);
 
 	return true;
@@ -139,7 +134,7 @@ static bool try_step_up(struct tdp_iter *iter)
 		return false;
 
 	iter->level++;
-	iter->gfn = round_gfn_for_level(iter->gfn, iter->level);
+	iter->gfn = gfn_round_for_level(iter->gfn, iter->level);
 	tdp_iter_refresh_sptep(iter);
 
 	return true;
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 04/24] KVM: x86/mmu: Fix wrong gfn range of tlb flushing in kvm_set_pte_rmapp()
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (2 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 03/24] KVM: x86/mmu: Move round_gfn_for_level() helper into mmu_internal.h Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 05/24] KVM: x86/mmu: Fix wrong start gfn of tlb flushing with range Paolo Bonzini
                   ` (20 subsequent siblings)
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: Hou Wenlong, Sean Christopherson

From: Hou Wenlong <houwenlong.hwl@antgroup.com>

[ Upstream commit 9ffe9265375cbaf6c01647e31ae9fee8595b698c ]

When the spte of hupe page is dropped in kvm_set_pte_rmapp(), the whole
gfn range covered by the spte should be flushed. However,
rmap_walk_init_level() doesn't align down the gfn for new level like tdp
iterator does, then the gfn used in kvm_set_pte_rmapp() is not the base
gfn of huge page. And the size of gfn range is wrong too for huge page.
Use the base gfn of huge page and the size of huge page for flushing
tlbs for huge page. Also introduce a helper function to flush the given
page (huge or not) of guest memory, which would help prevent future
buggy use of kvm_flush_remote_tlbs_with_address() in such case.

Fixes: c3134ce240eed ("KVM: Replace old tlb flush function with new one to flush a specified range.")
Signed-off-by: Hou Wenlong <houwenlong.hwl@antgroup.com>
Link: https://lore.kernel.org/r/0ce24d7078fa5f1f8d64b0c59826c50f32f8065e.1665214747.git.houwenlong.hwl@antgroup.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/mmu/mmu.c          | 2 +-
 arch/x86/kvm/mmu/mmu_internal.h | 9 +++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 72a61480890e..ba21f01f1d4b 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -1436,7 +1436,7 @@ static bool kvm_set_pte_rmap(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
 	}
 
 	if (need_flush && kvm_available_flush_tlb_with_range()) {
-		kvm_flush_remote_tlbs_with_address(kvm, gfn, 1);
+		kvm_flush_remote_tlbs_gfn(kvm, gfn, level);
 		return false;
 	}
 
diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h
index 6d631af0b383..1165b5e431a5 100644
--- a/arch/x86/kvm/mmu/mmu_internal.h
+++ b/arch/x86/kvm/mmu/mmu_internal.h
@@ -167,8 +167,17 @@ void kvm_mmu_gfn_allow_lpage(const struct kvm_memory_slot *slot, gfn_t gfn);
 bool kvm_mmu_slot_gfn_write_protect(struct kvm *kvm,
 				    struct kvm_memory_slot *slot, u64 gfn,
 				    int min_level);
+
 void kvm_flush_remote_tlbs_with_address(struct kvm *kvm,
 					u64 start_gfn, u64 pages);
+
+/* Flush the given page (huge or not) of guest memory. */
+static inline void kvm_flush_remote_tlbs_gfn(struct kvm *kvm, gfn_t gfn, int level)
+{
+	kvm_flush_remote_tlbs_with_address(kvm, gfn_round_for_level(gfn, level),
+					   KVM_PAGES_PER_HPAGE(level));
+}
+
 unsigned int pte_list_count(struct kvm_rmap_head *rmap_head);
 
 extern int nx_huge_pages;
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 05/24] KVM: x86/mmu: Fix wrong start gfn of tlb flushing with range
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (3 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 04/24] KVM: x86/mmu: Fix wrong gfn range of tlb flushing in kvm_set_pte_rmapp() Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 06/24] KVM: x86/mmu: Flush TLBs if child SPTE is zapped when splitting a huge SPTE Paolo Bonzini
                   ` (19 subsequent siblings)
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: Hou Wenlong, David Matlack, Sean Christopherson

From: Hou Wenlong <houwenlong.hwl@antgroup.com>

[ Upstream commit 1b2dc7360463932d2e7cac9461de16ce6fa11cb7 ]

When a spte is dropped, the start gfn of tlb flushing should be the gfn
of spte not the base gfn of SP which contains the spte. Also introduce a
helper function to do range-based flushing when a spte is dropped, which
would help prevent future buggy use of
kvm_flush_remote_tlbs_with_address() in such case.

Fixes: c3134ce240eed ("KVM: Replace old tlb flush function with new one to flush a specified range.")
Suggested-by: David Matlack <dmatlack@google.com>
Signed-off-by: Hou Wenlong <houwenlong.hwl@antgroup.com>
Link: https://lore.kernel.org/r/72ac2169a261976f00c1703e88cda676dfb960f5.1665214747.git.houwenlong.hwl@antgroup.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/mmu/mmu.c         | 17 +++++++++++++----
 arch/x86/kvm/mmu/paging_tmpl.h |  3 +--
 2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index ba21f01f1d4b..a240e014a50e 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -280,6 +280,17 @@ void kvm_flush_remote_tlbs_with_address(struct kvm *kvm,
 	kvm_flush_remote_tlbs_with_range(kvm, &range);
 }
 
+static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index);
+
+/* Flush the range of guest memory mapped by the given SPTE. */
+static void kvm_flush_remote_tlbs_sptep(struct kvm *kvm, u64 *sptep)
+{
+	struct kvm_mmu_page *sp = sptep_to_sp(sptep);
+	gfn_t gfn = kvm_mmu_page_get_gfn(sp, spte_index(sptep));
+
+	kvm_flush_remote_tlbs_gfn(kvm, gfn, sp->role.level);
+}
+
 static void mark_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, u64 gfn,
 			   unsigned int access)
 {
@@ -1606,8 +1617,7 @@ static void __rmap_add(struct kvm *kvm,
 		kvm->stat.max_mmu_rmap_size = rmap_count;
 	if (rmap_count > RMAP_RECYCLE_THRESHOLD) {
 		kvm_zap_all_rmap_sptes(kvm, rmap_head);
-		kvm_flush_remote_tlbs_with_address(
-				kvm, sp->gfn, KVM_PAGES_PER_HPAGE(sp->role.level));
+		kvm_flush_remote_tlbs_gfn(kvm, gfn, sp->role.level);
 	}
 }
 
@@ -6454,8 +6464,7 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm,
 			kvm_zap_one_rmap_spte(kvm, rmap_head, sptep);
 
 			if (kvm_available_flush_tlb_with_range())
-				kvm_flush_remote_tlbs_with_address(kvm, sp->gfn,
-					KVM_PAGES_PER_HPAGE(sp->role.level));
+				kvm_flush_remote_tlbs_sptep(kvm, sptep);
 			else
 				need_tlb_flush = 1;
 
diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h
index f79333aed51e..cf8574eeffab 100644
--- a/arch/x86/kvm/mmu/paging_tmpl.h
+++ b/arch/x86/kvm/mmu/paging_tmpl.h
@@ -956,8 +956,7 @@ static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva, hpa_t root_hpa)
 
 			mmu_page_zap_pte(vcpu->kvm, sp, sptep, NULL);
 			if (is_shadow_present_pte(old_spte))
-				kvm_flush_remote_tlbs_with_address(vcpu->kvm,
-					sp->gfn, KVM_PAGES_PER_HPAGE(sp->role.level));
+				kvm_flush_remote_tlbs_sptep(vcpu->kvm, sptep);
 
 			if (!rmap_can_add(vcpu))
 				break;
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 06/24] KVM: x86/mmu: Flush TLBs if child SPTE is zapped when splitting a huge SPTE
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (4 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 05/24] KVM: x86/mmu: Fix wrong start gfn of tlb flushing with range Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 07/24] KVM: x86/mmu: Propagate the return from kvm_zap_all_rmap_sptes() out of __rmap_add() Paolo Bonzini
                   ` (18 subsequent siblings)
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Sean Christopherson, stable, Chris Mason, Stefan Teodorescu

From: Sean Christopherson <seanjc@google.com>

[ Upstream commit d31322f7c0003c4b3cba9d44c85fd73aa877c9f9 ]

Queue a TLB flush when eagerly splitting a huge SPTE and adding a child
SPTE to the rmap fails due to hitting the recycle threshold.  If adding to
the rmap fails, __rmap_add() zaps the entire list of rmaps, including the
recently-added child SPTE.  Eliding the flush in that case is unsafe as the
huge SPTE is still PRESENT, after the TLB flush  performed by __rmap_add(),
i.e. can be (re)cached by running vCPUs.

Push the TLB flush out from __rmap_add() to its two callers by returning a
bool to indicate that a TLB flush is needed.  Return "TLB flush needed"
instead of success/failuire so that a feature cleanup can simply return the
result of kvm_zap_all_rmap_sptes() (which *should* always be true; if there
isn't at least one rmap entry, then things have really gone sideways).

Fixes: 0378739401cf ("KVM: x86/mmu: Avoid unnecessary flush on eager page split")
Cc: stable@vger.kernel.org
Reported-by: Chris Mason <mason@kernel.org>
Reported-by: Stefan Teodorescu <fane@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20261113155010.442816-2-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/mmu/mmu.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index a240e014a50e..1db7e30fca88 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -1597,7 +1597,7 @@ static bool kvm_test_age_rmap(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
 
 #define RMAP_RECYCLE_THRESHOLD 1000
 
-static void __rmap_add(struct kvm *kvm,
+static bool __rmap_add(struct kvm *kvm,
 		       struct kvm_mmu_memory_cache *cache,
 		       const struct kvm_memory_slot *slot,
 		       u64 *spte, gfn_t gfn, unsigned int access)
@@ -1617,8 +1617,9 @@ static void __rmap_add(struct kvm *kvm,
 		kvm->stat.max_mmu_rmap_size = rmap_count;
 	if (rmap_count > RMAP_RECYCLE_THRESHOLD) {
 		kvm_zap_all_rmap_sptes(kvm, rmap_head);
-		kvm_flush_remote_tlbs_gfn(kvm, gfn, sp->role.level);
+		return true;
 	}
+	return false;
 }
 
 static void rmap_add(struct kvm_vcpu *vcpu, const struct kvm_memory_slot *slot,
@@ -1626,7 +1627,9 @@ static void rmap_add(struct kvm_vcpu *vcpu, const struct kvm_memory_slot *slot,
 {
 	struct kvm_mmu_memory_cache *cache = &vcpu->arch.mmu_pte_list_desc_cache;
 
-	__rmap_add(vcpu->kvm, cache, slot, spte, gfn, access);
+	if (__rmap_add(vcpu->kvm, cache, slot, spte, gfn, access))
+		kvm_flush_remote_tlbs_gfn(vcpu->kvm, gfn,
+					  sptep_to_sp(spte)->role.level);
 }
 
 bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
@@ -6259,7 +6262,7 @@ static void shadow_mmu_split_huge_page(struct kvm *kvm,
 		 *
 		 * However, if a given SPTE points to a lower level page table,
 		 * that lower level page table may only be partially populated.
-		 * Installing such SPTEs would effectively unmap a potion of the
+		 * Installing such SPTEs would effectively unmap a portion of the
 		 * huge page. Unmapping guest memory always requires a TLB flush
 		 * since a subsequent operation on the unmapped regions would
 		 * fail to detect the need to flush.
@@ -6271,7 +6274,15 @@ static void shadow_mmu_split_huge_page(struct kvm *kvm,
 
 		spte = make_huge_page_split_spte(kvm, huge_spte, sp->role, index);
 		mmu_spte_set(sptep, spte);
-		__rmap_add(kvm, cache, slot, sptep, gfn, sp->role.access);
+
+		/*
+		 * Queue a TLB flush if adding the SPTE to the rmap zapped the
+		 * child SPTE that was just added.  In that case, replacing the
+		 * huge SPTE with a shadow page will effectively unmap guest
+		 * memory (see above), as the huge SPTE is still PRESENT and
+		 * can be (re)cached in the TLB before it is replaced.
+		 */
+		flush |= __rmap_add(kvm, cache, slot, sptep, gfn, sp->role.access);
 	}
 
 	__link_shadow_page(kvm, cache, huge_sptep, sp, flush);
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 07/24] KVM: x86/mmu: Propagate the return from kvm_zap_all_rmap_sptes() out of __rmap_add()
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (5 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 06/24] KVM: x86/mmu: Flush TLBs if child SPTE is zapped when splitting a huge SPTE Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 08/24] KVM: x86: Disable hypervisor (VMX Preemption) timer if TSC deadline wraps Paolo Bonzini
                   ` (17 subsequent siblings)
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: Sean Christopherson

From: Sean Christopherson <seanjc@google.com>

[ Upstream commit 4e29a0c758a9ef3668f7e52305a6727796ff516b ]

When recycling all rmaps, propagate the result of kvm_zap_all_rmap_sptes()
out of __rmap_add() instead of unconditionally returning "true", to make it
more obvious why a TLB flush is required.  Barring a KVM bug elsewhere that
corrupts the rmaps (mmu_lock is held for write), it's impossible for
kvm_zap_all_rmap_sptes() to return "false".  And if rmaps are corrupted,
KVM has far bigger problems then potentially leaving behind stale TLB
entries.

Assuming there are no egregious, fatals bug elsewhere in KVM, no functional
change intended.

Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20261113155010.442816-3-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/mmu/mmu.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 1db7e30fca88..38704c8c1414 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -1615,10 +1615,9 @@ static bool __rmap_add(struct kvm *kvm,
 
 	if (rmap_count > kvm->stat.max_mmu_rmap_size)
 		kvm->stat.max_mmu_rmap_size = rmap_count;
-	if (rmap_count > RMAP_RECYCLE_THRESHOLD) {
-		kvm_zap_all_rmap_sptes(kvm, rmap_head);
-		return true;
-	}
+	if (rmap_count > RMAP_RECYCLE_THRESHOLD)
+		return kvm_zap_all_rmap_sptes(kvm, rmap_head);
+
 	return false;
 }
 
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 08/24] KVM: x86: Disable hypervisor (VMX Preemption) timer if TSC deadline wraps
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (6 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 07/24] KVM: x86/mmu: Propagate the return from kvm_zap_all_rmap_sptes() out of __rmap_add() Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 09/24] KVM: x86: Reset local APIC timer metadata on RESET, INIT, and KVM_SET_LAPIC Paolo Bonzini
                   ` (16 subsequent siblings)
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Sean Christopherson, Stefan Teodorescu, David Woodhouse, stable

From: Sean Christopherson <seanjc@google.com>

[ Upstream commit 0b135caf3230bc4030c32bb7da5b8d4af64c379c ]

Disable the VMX Preemption Timer, by configuring the deadline to be '0',
if converting from a one-shot/periodic expiry to a deadline would wrap the
TSC, i.e. would result in a deadline that's far in the past, even though
the expiry is firmly in the future.  Architecturally, delivering the APIC
timer IRQ way too early is obviously wrong, but the bigger problem is what
can happen if the APIC timer is in periodic mode.

If the guest sets its TSC to a large value, but not _too_ large, puts it
APIC timer into periodic mode, and then programs the maximal period, then
KVM will compute a TSC deadline that is far "earlier" than the current TSC,
but with plenty of headroom for guest TSC to continue counting without the
TSC itself wrapping.  Even though the VMX side of things detects that the
timer has already expired, KVM leaves the hypervisor timer armed so that it
can compute the new deadline/expiry from the VM-Exit handler.  On the VMX
side of things, KVM handles the "deadline in the past" by forcing the VMX
Preemption Timer to '0', which generates an immediate VM-Exit.

All of that would be "fine", except that KVM handles VMX Preemption Timer
exits in its fastpath, will recompute the same in-the-past deadline, and
constantly re-enter the guest with a timer value of '0'.  Architecturally,
the SDM states that such behavior can trigger a VM-Exit *without* fetching
any instructions in the guest.

  It is possible for the VMX-preemption timer to expire during VM entry
  (e.g., if the value in the VMX-preemption timer-value field is zero). If
  this happens (and if the VM entry was not to the wait-for-SIPI state), a
  VM exit occurs with its normal priority after any event injection and
  before execution of any instruction following VM entry.

Empirically, on bare metal, if the timer expires "during" VM-Entry, the CPU
may or may not recognize IRQs (it's not clear exactly what conditions lead
to IRQs being recognized or not).  In the worst case scenario, the host can
experience hard lockups if the tight entry<=>exit loop never opens a window
for host IRQs to be recognized.

The problem is especially noticeable when running as a KVM guest, as KVM
*never* opens a window for host (L1) IRQs, even with non-zero (but small)
VMX Preemption Timer values, as KVM-as-L0 starts the timer fairly early in
the nested VM-Enter flow and so the timer always expires before the nested
VM-Enter gets to the point where it checks for L1 IRQs.

Opportunistically read TSC and ktime in close succession when computing the
target expiration and the TSC deadline, to minimize the divergence between
the two (though in practice, reading the values even a few hundred cycles
apart is a complete non-issue.

Fixes: 26efe2fd92e5 ("KVM: VMX: Handle preemption timer fastpath")
Fixes: 8003c9ae204e ("KVM: LAPIC: add APIC Timer periodic/oneshot mode VMX preemption timer support")
Reported-by: Stefan Teodorescu <fane@google.com>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: stable@vger.kernel.org
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20261028183358.1798566-2-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/lapic.c | 37 ++++++++++++++++++++++++-------------
 1 file changed, 24 insertions(+), 13 deletions(-)

diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index ad98e4f725e6..5bd19eb86dfb 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -1810,6 +1810,26 @@ static inline u64 tmict_to_ns(struct kvm_lapic *apic, u32 tmict)
 	return (u64)tmict * APIC_BUS_CYCLE_NS * (u64)apic->divide_count;
 }
 
+static u64 ns_to_tsc_deadline(struct kvm_lapic *apic, s64 delta)
+{
+	u64 tsc_now = rdtsc();
+	u64 deadline;
+
+	/*
+	 * Disable the hypervisor timer, which is deadline-based, if converting
+	 * the one-shot/periodic expiry to a TSC deadline wraps the TSC, i.e.
+	 * results in a deadline far in the past, even though the expiry itself
+	 * is in the future.  Alternatively, KVM could track the deadline as a
+	 * 128 bit value, but in practice the deadline will only wrap if the
+	 * guest is doing weird things with TSC (which is writable via MSR).
+	 */
+	if (check_add_overflow(kvm_read_l1_tsc(apic->vcpu, tsc_now),
+			       nsec_to_cycles(apic->vcpu, delta), &deadline))
+		return 0;
+
+	return deadline;
+}
+
 static void update_target_expiration(struct kvm_lapic *apic, uint32_t old_divisor)
 {
 	ktime_t now, remaining;
@@ -1828,19 +1848,14 @@ static void update_target_expiration(struct kvm_lapic *apic, uint32_t old_diviso
 	ns_remaining_new = mul_u64_u32_div(ns_remaining_old,
 	                                   apic->divide_count, old_divisor);
 
-	apic->lapic_timer.tscdeadline +=
-		nsec_to_cycles(apic->vcpu, ns_remaining_new) -
-		nsec_to_cycles(apic->vcpu, ns_remaining_old);
 	apic->lapic_timer.target_expiration = ktime_add_ns(now, ns_remaining_new);
+	apic->lapic_timer.tscdeadline = ns_to_tsc_deadline(apic, ns_remaining_new);
 }
 
 static bool set_target_expiration(struct kvm_lapic *apic, u32 count_reg)
 {
-	ktime_t now;
-	u64 tscl = rdtsc();
 	s64 deadline;
 
-	now = ktime_get();
 	apic->lapic_timer.period =
 			tmict_to_ns(apic, kvm_lapic_get_reg(apic, APIC_TMICT));
 
@@ -1877,10 +1892,8 @@ static bool set_target_expiration(struct kvm_lapic *apic, u32 count_reg)
 		}
 	}
 
-	apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
-		nsec_to_cycles(apic->vcpu, deadline);
-	apic->lapic_timer.target_expiration = ktime_add_ns(now, deadline);
-
+	apic->lapic_timer.target_expiration = ktime_add_ns(ktime_get(), deadline);
+	apic->lapic_timer.tscdeadline = ns_to_tsc_deadline(apic, deadline);
 	return true;
 }
 
@@ -1888,7 +1901,6 @@ static void advance_periodic_target_expiration(struct kvm_lapic *apic)
 {
 	struct kvm_timer *ktimer = &apic->lapic_timer;
 	ktime_t now = ktime_get();
-	u64 tscl = rdtsc();
 	ktime_t delta;
 
 	/*
@@ -1919,8 +1931,7 @@ static void advance_periodic_target_expiration(struct kvm_lapic *apic)
 	 * excessively large due to it an unsigned value.
 	 */
 	delta = ktime_sub(ktimer->target_expiration, now);
-	ktimer->tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
-			      nsec_to_cycles(apic->vcpu, delta);
+	ktimer->tscdeadline = ns_to_tsc_deadline(apic, ktime_to_ns(delta));
 }
 
 static void start_sw_period(struct kvm_lapic *apic)
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 09/24] KVM: x86: Reset local APIC timer metadata on RESET, INIT, and KVM_SET_LAPIC
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (7 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 08/24] KVM: x86: Disable hypervisor (VMX Preemption) timer if TSC deadline wraps Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 10/24] KVM: x86/mmu: Flush all TLB entries, not a precise range, when yielding Paolo Bonzini
                   ` (15 subsequent siblings)
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Sean Christopherson, Stefan Teodorescu, David Woodhouse, stable

From: Sean Christopherson <seanjc@google.com>

[ Upstream commit c876175f4bb87e3e19c6ecdf9b67beb895fe8bda ]

Reset all of the local APIC timer's metadata that's tied to the current
state of the timer on INIT (and RESET) and KVM_SET_LAPIC, *except* for the
TSC deadline when doing KVM_SET_LAPIC, as kvm_timer.tscdeadline subtly
holds/tracks the value of MSR_IA32_TSC_DEADLINE and isn't computed by
__start_apic_timer().  In particular, leaving the target expiration as-is
allows a misbehaving guest to induce a divide error by overflowing a u64.

After INIT (or RESET), the divide count register is '0', which translates
to an actual divisor of '2'.  The other divisors are 1, 4, 8, 16, 32, 64,
and 128, so the guest can scale the target_expiration from 2x-64x after
every INIT (or cut it in half, but that's uninteresting).  After enough
multiplications, things go kablooie.  E.g. setting the target_expiration
to -1u (TMICT is a 32-bit value) and then scaling by 64x via a divisor
of 128 generates a #DE after 6 rounds of INIT:

  Oops: divide error: 0000 [#1] SMP
  CPU: 0 UID: 1000 PID: 604 Comm: div_error Not tainted 7.3.0-rc7-e86da2bfcc430-sink-all #7307 PREEMPTLAZY
  Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015
  RIP: 0010:kvm_lapic_reg_write+0x364/0x7d0 [kvm]
   <TASK>
   kvm_set_msr_common+0x649/0x1540 [kvm]
   vmx_set_msr+0xdb5/0x13a0 [kvm_intel]
   __kvm_set_msr+0xc4/0x2a0 [kvm]
   kvm_set_msr_ignored_check+0x1d/0x140 [kvm]
   __kvm_emulate_wrmsr+0x36/0x170 [kvm]
   vt_handle_exit+0x1e/0x50 [kvm_intel]
   kvm_arch_vcpu_ioctl_run+0x972/0x21c0 [kvm]
   kvm_vcpu_ioctl+0x2c7/0x960 [kvm]
   __x64_sys_ioctl+0x90/0xd0
   do_syscall_64+0x9c/0x4d0
   entry_SYSCALL_64_after_hwframe+0x4b/0x53
  RIP: 0033:0x421d1b
   </TASK>
  Modules linked in: kvm_intel kvm irqbypass

Opportunistically purge the target_expiration and expired_tscdeadline when
the timer mode changes to/from deadline mode, even though that path should
be fine (it doesn't interact with the divide count).

Fixes: c301b909e4b9 ("KVM: LAPIC: Apply change to TDCR right away to the timer")
Reported-by: Stefan Teodorescu <fane@google.com>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: stable@vger.kernel.org
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20261028183358.1798566-3-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/lapic.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index 5bd19eb86dfb..14dd011bb53d 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -1596,6 +1596,23 @@ static void cancel_apic_timer(struct kvm_lapic *apic)
 	atomic_set(&apic->lapic_timer.pending, 0);
 }
 
+static void __kvm_lapic_reset_timer(struct kvm_lapic *apic)
+{
+	struct kvm_timer *ktimer = &apic->lapic_timer;
+
+	cancel_apic_timer(apic);
+
+	ktimer->period = 0;
+	ktimer->target_expiration = 0;
+	ktimer->expired_tscdeadline = 0;
+}
+
+static void kvm_lapic_reset_timer(struct kvm_lapic *apic)
+{
+	__kvm_lapic_reset_timer(apic);
+	apic->lapic_timer.tscdeadline = 0;
+}
+
 static void apic_update_lvtt(struct kvm_lapic *apic)
 {
 	u32 timer_mode = kvm_lapic_get_reg(apic, APIC_LVTT) &
@@ -1604,10 +1621,8 @@ static void apic_update_lvtt(struct kvm_lapic *apic)
 	if (apic->lapic_timer.timer_mode != timer_mode) {
 		if (apic_lvtt_tscdeadline(apic) != (timer_mode ==
 				APIC_LVT_TIMER_TSCDEADLINE)) {
-			cancel_apic_timer(apic);
+			kvm_lapic_reset_timer(apic);
 			kvm_lapic_set_reg(apic, APIC_TMICT, 0);
-			apic->lapic_timer.period = 0;
-			apic->lapic_timer.tscdeadline = 0;
 		}
 		apic->lapic_timer.timer_mode = timer_mode;
 		limit_periodic_timer_frequency(apic);
@@ -2551,8 +2566,7 @@ void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event)
 	if (!apic)
 		return;
 
-	/* Stop the timer in case it's a reset to an active apic */
-	hrtimer_cancel(&apic->lapic_timer.timer);
+	kvm_lapic_reset_timer(apic);
 
 	/* The xAPIC ID is set at RESET even if the APIC was already enabled. */
 	if (!init_event)
@@ -2589,7 +2603,6 @@ void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event)
 	}
 	kvm_apic_update_apicv(vcpu);
 	update_divide_count(apic);
-	atomic_set(&apic->lapic_timer.pending, 0);
 
 	vcpu->arch.pv_eoi.msr_val = 0;
 	apic_update_ppr(apic);
@@ -2868,6 +2881,9 @@ int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
 		kvm_recalculate_apic_map(vcpu->kvm);
 		return r;
 	}
+
+	__kvm_lapic_reset_timer(apic);
+
 	memcpy(vcpu->arch.apic->regs, s->regs, sizeof(*s));
 
 	if (!apic_x2apic_mode(apic))
@@ -2878,8 +2894,6 @@ int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
 	kvm_apic_set_version(vcpu);
 
 	apic_update_ppr(apic);
-	cancel_apic_timer(apic);
-	apic->lapic_timer.expired_tscdeadline = 0;
 	apic_update_lvtt(apic);
 	apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
 	update_divide_count(apic);
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 10/24] KVM: x86/mmu: Flush all TLB entries, not a precise range, when yielding
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (8 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 09/24] KVM: x86: Reset local APIC timer metadata on RESET, INIT, and KVM_SET_LAPIC Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 11/24] KVM: VMX: Retry APIC-access page reload if invalidation is in-progress Paolo Bonzini
                   ` (14 subsequent siblings)
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Kyle Zeng, stable, Kevin Liu, Stefan Teodorescu, Ulfar Erlingsson,
	Sean Christopherson

From: Kyle Zeng <kylebot@openai.com>

[ Upstream commit dc3709f44c9617c9fa6d5d0e71dc5b82f47a57b7 ]

Flush *all* TLB entries, not just the currently visible range of GFNs, when
yielding and dropping mmu_lock the shadow MMU, as a pending flush may cover
a bigger range than what is currently in scope, e.g. from a different
memslot or from a different page-table level.  Flushing only the range of
GFNs that are directly relevant to the current slot+level result in KVM
failing to TLB flush entries for previously-zapped SPTEs when running on
Hyper-V (the only time the range-based flushing is used).

Fixes: f285c633cb6d ("kvm: mmu: Used range based flushing in slot_handle_level_range")
Cc: stable@vger.kernel.org
Reported-by: Kevin Liu <kevin.liu@openai.com>
Reported-by: Stefan Teodorescu <fane@google.com>
Reported-by: Ulfar Erlingsson <ulfar@google.com>
Signed-off-by: Kyle Zeng <kylebot@openai.com>
[sean: split to separate patch, write changelog, rewrite comment, add more reporters]
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20261119172647.64295-4-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/mmu/mmu.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 38704c8c1414..58fef57e0837 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -5775,9 +5775,12 @@ slot_handle_level_range(struct kvm *kvm, const struct kvm_memory_slot *memslot,
 
 		if (need_resched() || rwlock_needbreak(&kvm->mmu_lock)) {
 			if (flush && flush_on_yield) {
-				kvm_flush_remote_tlbs_with_address(kvm,
-						start_gfn,
-						iterator.gfn - start_gfn + 1);
+				/*
+				 * DO NOT do a range-based flush, as the caller
+				 * may be zapping a range greater than what is
+				 * visible here!
+				 */
+				kvm_flush_remote_tlbs(kvm);
 				flush = false;
 			}
 			cond_resched_rwlock_write(&kvm->mmu_lock);
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 11/24] KVM: VMX: Retry APIC-access page reload if invalidation is in-progress
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (9 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 10/24] KVM: x86/mmu: Flush all TLB entries, not a precise range, when yielding Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 12/24] KVM: x86: Use standard mmu_notifier invalidate hooks for APIC access page Paolo Bonzini
                   ` (13 subsequent siblings)
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Sean Christopherson, Jason Gunthorpe, Alistair Popple,
	Robin Murphy

From: Sean Christopherson <seanjc@google.com>

[ Upstream commit 878940b33d7678e39a526ffe264ee025977dc67e ]

Re-request an APIC-access page reload if there is a relevant mmu_notifier
invalidation in-progress when KVM retrieves the backing pfn, i.e. stall
vCPUs until the backing pfn for the APIC-access page is "officially"
stable.  Relying on the primary MMU to not make changes after invoking
->invalidate_range() works, e.g. any additional changes to a PRESENT PTE
would also trigger an ->invalidate_range(), but using ->invalidate_range()
to fudge around KVM not honoring past and in-progress invalidations is a
bit hacky.

Honoring invalidations will allow using KVM's standard mmu_notifier hooks
to detect APIC-access page reloads, which will in turn allow removing
KVM's implementation of ->invalidate_range() (the APIC-access page case is
a true one-off).

Opportunistically add a comment to explain why doing nothing if a memslot
isn't found is functionally correct.

Suggested-by: Jason Gunthorpe <jgg@nvidia.com>
Cc: Alistair Popple <apopple@nvidia.com>
Cc: Robin Murphy <robin.murphy@arm.com>
Reviewed-by: Alistair Popple <apopple@nvidia.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Link: https://lore.kernel.org/r/20230602011518.787006-2-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/vmx/vmx.c | 50 +++++++++++++++++++++++++++++++++++++-----
 1 file changed, 45 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 9fa2eb33d54d..28e0198c2024 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -6690,7 +6690,12 @@ void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
 
 static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu)
 {
-	struct page *page;
+	const gfn_t gfn = APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT;
+	struct kvm *kvm = vcpu->kvm;
+	struct kvm_memslots *slots = kvm_memslots(kvm);
+	struct kvm_memory_slot *slot;
+	unsigned long mmu_seq;
+	kvm_pfn_t pfn;
 
 	/* Defer reload until vmcs01 is the current VMCS. */
 	if (is_guest_mode(vcpu)) {
@@ -6702,18 +6707,53 @@ static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu)
 	    SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
 		return;
 
-	page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
-	if (is_error_page(page))
+	/*
+	 * Grab the memslot so that the hva lookup for the mmu_notifier retry
+	 * is guaranteed to use the same memslot as the pfn lookup, i.e. rely
+	 * on the pfn lookup's validation of the memslot to ensure a valid hva
+	 * is used for the retry check.
+	 */
+	slot = id_to_memslot(slots, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT);
+	if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
 		return;
 
-	vmcs_write64(APIC_ACCESS_ADDR, page_to_phys(page));
+	/*
+	 * Ensure that the mmu_notifier sequence count is read before KVM
+	 * retrieves the pfn from the primary MMU.  Note, the memslot is
+	 * protected by SRCU, not the mmu_notifier.  Pairs with the smp_wmb()
+	 * in kvm_mmu_invalidate_end().
+	 */
+	mmu_seq = kvm->mmu_invalidate_seq;
+	smp_rmb();
+
+	/*
+	 * No need to retry if the memslot does not exist or is invalid.  KVM
+	 * controls the APIC-access page memslot, and only deletes the memslot
+	 * if APICv is permanently inhibited, i.e. the memslot won't reappear.
+	 */
+	pfn = gfn_to_pfn_memslot(slot, gfn);
+	if (is_error_noslot_pfn(pfn))
+		return;
+
+	read_lock(&vcpu->kvm->mmu_lock);
+	if (mmu_invalidate_retry_hva(kvm, mmu_seq,
+				     gfn_to_hva_memslot(slot, gfn))) {
+		kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
+		read_unlock(&vcpu->kvm->mmu_lock);
+		goto out;
+	}
+
+	vmcs_write64(APIC_ACCESS_ADDR, pfn_to_hpa(pfn));
+	read_unlock(&vcpu->kvm->mmu_lock);
+
 	vmx_flush_tlb_current(vcpu);
 
+out:
 	/*
 	 * Do not pin apic access page in memory, the MMU notifier
 	 * will call us again if it is migrated or swapped out.
 	 */
-	put_page(page);
+	kvm_release_pfn_clean(pfn);
 }
 
 static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 12/24] KVM: x86: Use standard mmu_notifier invalidate hooks for APIC access page
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (10 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 11/24] KVM: VMX: Retry APIC-access page reload if invalidation is in-progress Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 13/24] KVM: x86/mmu: Flush TLBs in kvm_unmap_gfn_range() if the unmap may block/yield Paolo Bonzini
                   ` (12 subsequent siblings)
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Sean Christopherson, Jason Gunthorpe, Alistair Popple,
	Robin Murphy

From: Sean Christopherson <seanjc@google.com>

[ Upstream commit 0a8a5f2c8c266e9d94fb45f76a26cff135d0051c ]

Now that KVM honors past and in-progress mmu_notifier invalidations when
reloading the APIC-access page, use KVM's "standard" invalidation hooks
to trigger a reload and delete the one-off usage of invalidate_range().

Aside from eliminating one-off code in KVM, dropping KVM's use of
invalidate_range() will allow common mmu_notifier to redefine the API to
be more strictly focused on invalidating secondary TLBs that share the
primary MMU's page tables.

Suggested-by: Jason Gunthorpe <jgg@nvidia.com>
Cc: Alistair Popple <apopple@nvidia.com>
Cc: Robin Murphy <robin.murphy@arm.com>
Reviewed-by: Alistair Popple <apopple@nvidia.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Link: https://lore.kernel.org/r/20230602011518.787006-3-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/mmu/mmu.c   |  3 +++
 arch/x86/kvm/x86.c       | 14 --------------
 include/linux/kvm_host.h |  3 ---
 virt/kvm/kvm_main.c      | 18 ------------------
 4 files changed, 3 insertions(+), 35 deletions(-)

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 58fef57e0837..d6c83b7611d6 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -1552,6 +1552,9 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 	if (is_tdp_mmu_enabled(kvm))
 		flush = kvm_tdp_mmu_unmap_gfn_range(kvm, range, flush);
 
+	if (range->slot->id == APIC_ACCESS_PAGE_PRIVATE_MEMSLOT)
+		kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
+
 	return flush;
 }
 
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index f24de94fb9a3..972d0ed30d1d 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -10607,20 +10607,6 @@ static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
 		vcpu, (u64 *)vcpu->arch.ioapic_handled_vectors);
 }
 
-void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
-					    unsigned long start, unsigned long end)
-{
-	unsigned long apic_address;
-
-	/*
-	 * The physical address of apic access page is stored in the VMCS.
-	 * Update it when it becomes invalid.
-	 */
-	apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
-	if (start <= apic_address && apic_address < end)
-		kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
-}
-
 void kvm_arch_guest_memory_reclaimed(struct kvm *kvm)
 {
 	static_call_cond(kvm_x86_guest_memory_reclaimed)(kvm);
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index e1bea71ca928..b5a3a48379cf 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -2247,9 +2247,6 @@ static inline long kvm_arch_vcpu_async_ioctl(struct file *filp,
 }
 #endif /* CONFIG_HAVE_KVM_VCPU_ASYNC_IOCTL */
 
-void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
-					    unsigned long start, unsigned long end);
-
 void kvm_arch_guest_memory_reclaimed(struct kvm *kvm);
 
 #ifdef CONFIG_HAVE_KVM_VCPU_RUN_PID_CHANGE
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 9221a006970c..9a4e2130a301 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -159,11 +159,6 @@ static unsigned long long kvm_active_vms;
 
 static DEFINE_PER_CPU(cpumask_var_t, cpu_kick_mask);
 
-__weak void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
-						   unsigned long start, unsigned long end)
-{
-}
-
 __weak void kvm_arch_guest_memory_reclaimed(struct kvm *kvm)
 {
 }
@@ -526,18 +521,6 @@ static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
 	return container_of(mn, struct kvm, mmu_notifier);
 }
 
-static void kvm_mmu_notifier_invalidate_range(struct mmu_notifier *mn,
-					      struct mm_struct *mm,
-					      unsigned long start, unsigned long end)
-{
-	struct kvm *kvm = mmu_notifier_to_kvm(mn);
-	int idx;
-
-	idx = srcu_read_lock(&kvm->srcu);
-	kvm_arch_mmu_notifier_invalidate_range(kvm, start, end);
-	srcu_read_unlock(&kvm->srcu, idx);
-}
-
 typedef bool (*hva_handler_t)(struct kvm *kvm, struct kvm_gfn_range *range);
 
 typedef void (*on_lock_fn_t)(struct kvm *kvm, unsigned long start,
@@ -915,7 +898,6 @@ static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
 }
 
 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
-	.invalidate_range	= kvm_mmu_notifier_invalidate_range,
 	.invalidate_range_start	= kvm_mmu_notifier_invalidate_range_start,
 	.invalidate_range_end	= kvm_mmu_notifier_invalidate_range_end,
 	.clear_flush_young	= kvm_mmu_notifier_clear_flush_young,
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 13/24] KVM: x86/mmu: Flush TLBs in kvm_unmap_gfn_range() if the unmap may block/yield
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (11 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 12/24] KVM: x86: Use standard mmu_notifier invalidate hooks for APIC access page Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 14/24] KVM: x86/xen: Pin kvm->mm prior to borrowing mm when setting event channel Paolo Bonzini
                   ` (11 subsequent siblings)
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Sean Christopherson, stable, Kevin Liu, Chris Mason, Kyle Zeng

From: Sean Christopherson <seanjc@google.com>

[ Upstream commit 27e2443f180a769a6e58a8f3821cacd01325bfc4 ]

Immediately flush TLBs if a flush is pending after unmapping a GFN range,
*before* returning to the caller, if the overall unmap operation may block,
i.e. if a *future* call to kvm_unmap_gfn_range() may yield and drop
mmu_lock.  Because kvm_unmap_gfn_range() starts with "flush = false", KVM
incorrectly loses track of the pending flush if an overall unmap operation
straddles multiple memslots.  KVM will still service the TLB flush before
the *caller* completes, but KVM relies on its mappings being completely
unreachable prior to dropping mmu_lock.

E.g. if a MMU notifier invalidation event runs after mmu_lock is dropped,
but before TLBs are flushed, then the MMU notifier task can observe the
missing PTE and return without issuing its own TLB flush, resulting in a
use-after-free (if the to-be-invalidated page was freed or moved).

Hack a fix into x86, but leave a TODO behind to undo the hack, or at least
make it less awful.  By dumb luck, x86 is the only arch that conditionally
yields *and* utilizes the common TLB flushing mechanism.

Fixes: e1eed5847b09 ("KVM: x86/mmu: Allow yielding during MMU notifier unmap/zap, if possible")
Cc: stable@vger.kernel.org
Reported-by: Kevin Liu <kevin.liu@openai.com>
Reported-by: Chris Mason <mason@kernel.org>
Cc: Kyle Zeng <kylebot@openai.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20261119172647.64295-5-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/mmu/mmu.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index d6c83b7611d6..0ece6fe15fd3 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -1555,6 +1555,21 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 	if (range->slot->id == APIC_ACCESS_PAGE_PRIVATE_MEMSLOT)
 		kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
 
+	/*
+	 * Immediately flush TLBs if a *future* invocation of the current unmap
+	 * operation may block, in which case the pending flush will be dropped
+	 * due to starting with "flush = false" above.
+	 *
+	 * TODO: Plumb @flush around common KVM so that pending flushes can be
+	 *       processed if and only if KVM actually yields, and so that this
+	 *       code doesn't rely on may_block being constant for the entirety
+	 *       of an unmap operation.
+	 */
+	if (flush && range->may_block) {
+		kvm_flush_remote_tlbs(kvm);
+		return false;
+	}
+
 	return flush;
 }
 
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 14/24] KVM: x86/xen: Pin kvm->mm prior to borrowing mm when setting event channel
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (12 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 13/24] KVM: x86/mmu: Flush TLBs in kvm_unmap_gfn_range() if the unmap may block/yield Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 15/24] KVM: Fold kvm_flush_shadow_all() into kvm_mmu_notifier_release() Paolo Bonzini
                   ` (10 subsequent siblings)
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: Sean Christopherson, stable, Sashiko Bot

From: Sean Christopherson <seanjc@google.com>

[ Upstream commit d6d94b952ac457d3c34aad172cacf00ef01eef8c ]

When setting a Xen event channel from a kthread, get a reference to the
VM's mm prior to borrowing the mm, and bail with -EFAULT if the mm is dead
or dying.  The VM's irqfds are deactivated only when the VM itself is dying,
i.e. the worker can trigger event channel writes if the VM's process exits
before the VM itself is destroyed.

Note, there's no need to use mmput_async(), as kvm_xen_set_evtchn() borrows
an mm if and only if it's already running in a workqueue.

Fixes: 14243b387137 ("KVM: x86/xen: Add KVM_IRQ_ROUTING_XEN_EVTCHN and event channel delivery")
Cc: stable@vger.kernel.org
Reported-by: Sashiko Bot <sashiko-bot@kernel.org>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20261120195742.55768-2-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/xen.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c
index baa0c6d18096..11ab3bf55852 100644
--- a/arch/x86/kvm/xen.c
+++ b/arch/x86/kvm/xen.c
@@ -1476,6 +1476,9 @@ static int kvm_xen_set_evtchn(struct kvm_xen_evtchn *xe, struct kvm *kvm)
 		if (WARN_ON_ONCE(current->mm))
 			return -EINVAL;
 
+		if (!mmget_not_zero(kvm->mm))
+			return -EFAULT;
+
 		kthread_use_mm(kvm->mm);
 		mm_borrowed = true;
 	}
@@ -1519,8 +1522,10 @@ static int kvm_xen_set_evtchn(struct kvm_xen_evtchn *xe, struct kvm *kvm)
 
 	mutex_unlock(&kvm->lock);
 
-	if (mm_borrowed)
+	if (mm_borrowed) {
 		kthread_unuse_mm(kvm->mm);
+		mmput(kvm->mm);
+	}
 
 	return rc;
 }
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 15/24] KVM: Fold kvm_flush_shadow_all() into kvm_mmu_notifier_release()
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (13 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 14/24] KVM: x86/xen: Pin kvm->mm prior to borrowing mm when setting event channel Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 16/24] KVM: WARN if there are dangling MMU invalidations at VM destruction Paolo Bonzini
                   ` (9 subsequent siblings)
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: Sean Christopherson, stable

From: Sean Christopherson <seanjc@google.com>

[ Upstream commit c9c6a91ce0371a7f951c3d2f2bad7bcead6a4840 ]

Fold kvm_flush_shadow_all() into kvm_mmu_notifier_release() now that all
all architectures use the generic mmu_notifier, i.e. all flow through
.release() when the VM and/or process is dying.  This will allow setting
mn_active_invalidate_count and _leaving_ it set when all SPTEs are zapped,
without having to worry about hypothetical scenarios where future KVM could
invoke kvm_flush_shadow_all() prior to the VM/process dying.

No functional change intended.

Cc: stable@vger.kernel.org
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20261120195742.55768-3-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 virt/kvm/kvm_main.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 9a4e2130a301..9760348f60ea 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -373,11 +373,13 @@ void kvm_flush_remote_tlbs(struct kvm *kvm)
 EXPORT_SYMBOL_GPL(kvm_flush_remote_tlbs);
 #endif
 
+#if !defined CONFIG_MMU_NOTIFIER || !defined KVM_ARCH_WANT_MMU_NOTIFIER
 static void kvm_flush_shadow_all(struct kvm *kvm)
 {
 	kvm_arch_flush_shadow_all(kvm);
 	kvm_arch_guest_memory_reclaimed(kvm);
 }
+#endif
 
 #ifdef KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE
 static inline void *mmu_memory_cache_alloc_obj(struct kvm_mmu_memory_cache *mc,
@@ -893,7 +895,8 @@ static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
 	int idx;
 
 	idx = srcu_read_lock(&kvm->srcu);
-	kvm_flush_shadow_all(kvm);
+	kvm_arch_flush_shadow_all(kvm);
+	kvm_arch_guest_memory_reclaimed(kvm);
 	srcu_read_unlock(&kvm->srcu, idx);
 }
 
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 16/24] KVM: WARN if there are dangling MMU invalidations at VM destruction
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (14 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 15/24] KVM: Fold kvm_flush_shadow_all() into kvm_mmu_notifier_release() Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 17/24] KVM: WARN if there are in-flight memslot updates when a VM's process exits Paolo Bonzini
                   ` (8 subsequent siblings)
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: Sean Christopherson, Fuad Tabba

From: Sean Christopherson <seanjc@google.com>

[ Upstream commit d497a0fab8b8457214fcc9b1a39530920ea7e95e ]

Add an assertion that there are no in-progress MMU invalidations when a
VM is being destroyed, with the exception of the scenario where KVM
unregisters its MMU notifier between an .invalidate_range_start() call and
the corresponding .invalidate_range_end().

KVM can't detect unpaired calls from the mmu_notifier due to the above
exception waiver, but the assertion can detect KVM bugs, e.g. such as the
bug that *almost* escaped initial guest_memfd development.

Link: https://lore.kernel.org/all/e397d30c-c6af-e68f-d18e-b4e3739c5389@linux.intel.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Fuad Tabba <tabba@google.com>
Tested-by: Fuad Tabba <tabba@google.com>
Link: https://patch.msgid.link/20231027182217.3615211-5-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 virt/kvm/kvm_main.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 9760348f60ea..7ffe1900ffaf 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -1312,9 +1312,16 @@ static void kvm_destroy_vm(struct kvm *kvm)
 	 * No threads can be waiting in install_new_memslots as the
 	 * last reference on KVM has been dropped, but freeing
 	 * memslots would deadlock without this manual intervention.
+	 *
+	 * If the count isn't unbalanced, i.e. KVM did NOT unregister its MMU
+	 * notifier between a start() and end(), then there shouldn't be any
+	 * in-progress invalidations.
 	 */
 	WARN_ON(rcuwait_active(&kvm->mn_memslots_update_rcuwait));
-	kvm->mn_active_invalidate_count = 0;
+	if (kvm->mn_active_invalidate_count)
+		kvm->mn_active_invalidate_count = 0;
+	else
+		WARN_ON(kvm->mmu_invalidate_in_progress);
 #else
 	kvm_flush_shadow_all(kvm);
 #endif
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 17/24] KVM: WARN if there are in-flight memslot updates when a VM's process exits
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (15 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 16/24] KVM: WARN if there are dangling MMU invalidations at VM destruction Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 18/24] KVM: Fix comments that refer to the non-existent install_new_memslots() Paolo Bonzini
                   ` (7 subsequent siblings)
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: Sean Christopherson, stable

From: Sean Christopherson <seanjc@google.com>

[ Upstream commit fd00a80aa395b803832ebaf1c494d721e9483dbb ]

Move the sanity check on there being no outstanding memslot updates from VM
destruction to kvm_mmu_notifier_release(), i.e. WARN if there are
outstanding memslot updates when the VM is dying *or* the owning process is
exiting.  KVM subtly already guarantees (and relies on!) memslot updates to
be mutually exclusive with the mm dying.

If KVM unregistered its mmu_notifier between .invalidate_range_start() and
.invalidate_range_end(), then the invalidate count would be left elevated
until the VM is destroyed, but if there were an outstanding memslot update,
it would be impossible for the VM to be destroyed.

Opportunistically change the WARNs to WARN_ON_ONCE() so that a KVM bug
doesn't unintentionally DoS the host due to spamming dmesg (hanging a task
is bad enough).

Cc: stable@vger.kernel.org
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20261120195742.55768-4-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 virt/kvm/kvm_main.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 7ffe1900ffaf..9845e6a734f3 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -894,6 +894,14 @@ static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
 	struct kvm *kvm = mmu_notifier_to_kvm(mn);
 	int idx;
 
+	/*
+	 * No threads can be waiting in kvm_swap_active_memslots() as KVM only
+	 * allows KVM_SET_USER_MEMORY_REGION{,2} in process context, and only
+	 * from the process that created the VM, i.e. to @mm.  If @mm is dying,
+	 * then there can't possibly be outstanding memslot updates.
+	 */
+	WARN_ON_ONCE(rcuwait_active(&kvm->mn_memslots_update_rcuwait));
+
 	idx = srcu_read_lock(&kvm->srcu);
 	kvm_arch_flush_shadow_all(kvm);
 	kvm_arch_guest_memory_reclaimed(kvm);
@@ -1317,7 +1325,6 @@ static void kvm_destroy_vm(struct kvm *kvm)
 	 * notifier between a start() and end(), then there shouldn't be any
 	 * in-progress invalidations.
 	 */
-	WARN_ON(rcuwait_active(&kvm->mn_memslots_update_rcuwait));
 	if (kvm->mn_active_invalidate_count)
 		kvm->mn_active_invalidate_count = 0;
 	else
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 18/24] KVM: Fix comments that refer to the non-existent install_new_memslots()
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (16 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 17/24] KVM: WARN if there are in-flight memslot updates when a VM's process exits Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 19/24] KVM: Set mn_active_invalidate_count when the VM's mm is released Paolo Bonzini
                   ` (6 subsequent siblings)
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: Jun Miao, Sean Christopherson

From: Jun Miao <jun.miao@intel.com>

[ Upstream commit b0d237087c674c43df76c1a0bc2737592f3038f4 ]

Fix stale comments that were left behind when install_new_memslots() was
replaced by kvm_swap_active_memslots() as part of the scalable memslots
rework.

Fixes: a54d806688fe ("KVM: Keep memslots in tree-based structures instead of array-based ones")
Signed-off-by: Jun Miao <jun.miao@intel.com>
Link: https://lore.kernel.org/r/20230223052851.1054799-1-jun.miao@intel.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 Documentation/virt/kvm/locking.rst |  2 +-
 include/linux/kvm_host.h           |  4 ++--
 virt/kvm/kvm_main.c                | 14 +++++++-------
 3 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/Documentation/virt/kvm/locking.rst b/Documentation/virt/kvm/locking.rst
index 845a561629f1..c93fd24bb726 100644
--- a/Documentation/virt/kvm/locking.rst
+++ b/Documentation/virt/kvm/locking.rst
@@ -24,7 +24,7 @@ The acquisition orders for mutexes are as follows:
 - kvm->mn_active_invalidate_count ensures that pairs of
   invalidate_range_start() and invalidate_range_end() callbacks
   use the same memslots array.  kvm->slots_lock and kvm->slots_arch_lock
-  are taken on the waiting side in install_new_memslots, so MMU notifiers
+  are taken on the waiting side when modifying memslots, so MMU notifiers
   must not take either kvm->slots_lock or kvm->slots_arch_lock.
 
 On x86:
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index b5a3a48379cf..2c602e741a08 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -58,7 +58,7 @@
 
 /*
  * Bit 63 of the memslot generation number is an "update in-progress flag",
- * e.g. is temporarily set for the duration of install_new_memslots().
+ * e.g. is temporarily set for the duration of kvm_swap_active_memslots().
  * This flag effectively creates a unique generation number that is used to
  * mark cached memslot data, e.g. MMIO accesses, as potentially being stale,
  * i.e. may (or may not) have come from the previous memslots generation.
@@ -702,7 +702,7 @@ struct kvm {
 	 * use by the VM. To be used under the slots_lock (above) or in a
 	 * kvm->srcu critical section where acquiring the slots_lock would
 	 * lead to deadlock with the synchronize_srcu in
-	 * install_new_memslots.
+	 * kvm_swap_active_memslots().
 	 */
 	struct mutex slots_arch_lock;
 	struct mm_struct *mm; /* userspace tied to this vm */
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 9845e6a734f3..add614b3b40d 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -1317,7 +1317,7 @@ static void kvm_destroy_vm(struct kvm *kvm)
 	 * At this point, pending calls to invalidate_range_start()
 	 * have completed but no more MMU notifiers will run, so
 	 * mn_active_invalidate_count may remain unbalanced.
-	 * No threads can be waiting in install_new_memslots as the
+	 * No threads can be waiting in kvm_swap_active_memslots() as the
 	 * last reference on KVM has been dropped, but freeing
 	 * memslots would deadlock without this manual intervention.
 	 *
@@ -1759,13 +1759,13 @@ static void kvm_invalidate_memslot(struct kvm *kvm,
 	kvm_arch_flush_shadow_memslot(kvm, old);
 	kvm_arch_guest_memory_reclaimed(kvm);
 
-	/* Was released by kvm_swap_active_memslots, reacquire. */
+	/* Was released by kvm_swap_active_memslots(), reacquire. */
 	mutex_lock(&kvm->slots_arch_lock);
 
 	/*
 	 * Copy the arch-specific field of the newly-installed slot back to the
 	 * old slot as the arch data could have changed between releasing
-	 * slots_arch_lock in install_new_memslots() and re-acquiring the lock
+	 * slots_arch_lock in kvm_swap_active_memslots() and re-acquiring the lock
 	 * above.  Writers are required to retrieve memslots *after* acquiring
 	 * slots_arch_lock, thus the active slot's data is guaranteed to be fresh.
 	 */
@@ -1827,11 +1827,11 @@ static int kvm_set_memslot(struct kvm *kvm,
 	int r;
 
 	/*
-	 * Released in kvm_swap_active_memslots.
+	 * Released in kvm_swap_active_memslots().
 	 *
-	 * Must be held from before the current memslots are copied until
-	 * after the new memslots are installed with rcu_assign_pointer,
-	 * then released before the synchronize srcu in kvm_swap_active_memslots.
+	 * Must be held from before the current memslots are copied until after
+	 * the new memslots are installed with rcu_assign_pointer, then
+	 * released before the synchronize srcu in kvm_swap_active_memslots().
 	 *
 	 * When modifying memslots outside of the slots_lock, must be held
 	 * before reading the pointer to the current memslots until after all
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 19/24] KVM: Set mn_active_invalidate_count when the VM's mm is released
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (17 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 18/24] KVM: Fix comments that refer to the non-existent install_new_memslots() Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 20/24] KVM: Invalidate all GFN=>PFN caches when VM and/or its process is dying Paolo Bonzini
                   ` (5 subsequent siblings)
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: Sean Christopherson, stable

From: Sean Christopherson <seanjc@google.com>

[ Upstream commit fca5e4804655f39e40cf302ca37f6bd35445881b ]

Set mn_active_invalidate_count permanently (until the VM is destroyed) when
the VM's mmu_notifier release() hook is called, i.e. when the VM and/or its
process is dying.  This will allow using gfn_to_pfn_cache_invalidate_start()
more or less verbatim to invalidate all GPCs on .release(), without having
to play games with the sequence count.  As pointed out by Sashiko, a racing
hva_to_pfn_retry() could set its GPC valid after re-acquiring gpc->lock if
it's not forced to retry.

Isolate the mn_active_invalidate_count change on the off chance it causes
problems.  Elevating mn_active_invalidate_count should be a-ok because it's
already possible for kvm_mmu_notifier_release() to run while the count is
elevated *and* cause it to remain elevated, as .release() can be called
between .invalidate_range_start() and .invalidate_range_end().  If that
happens, the matching end() will never occur, but kvm_destroy_vm() will
squash the count before freeing memslots.  Note, there can't be any
in-flight memslot updates when .release() runs, as KVM only does memslot
updates from process context (except for the kvm_destroy_vm() case).

Adjust the sanity check on in-progress invalidations to account for an
invalidate count of '1' now being the effective baseline when the VM is
being destroyed.

Opportunistically tweak the comment to call out that unregistering the MMU
notifier between start() and end() doesn't *guarantee* KVM should observe
an in-progress invalidation, because KVM tracks those invalidations if and
only if there is a relevant memslot.

Opportunistically add a TODO to call out that KVM can stop squashing the
invalidate count once x86 stops abusing internal memslot updates to unmap
VMAs for KVM-internal memslots[*].

Link: https://lore.kernel.org/all/20261030212112.2598640-7-seanjc@google.com [*]
Cc: stable@vger.kernel.org
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20261120195742.55768-5-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 virt/kvm/kvm_main.c | 42 ++++++++++++++++++++++++++++--------------
 1 file changed, 28 insertions(+), 14 deletions(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index add614b3b40d..bb46945cc0e4 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -902,6 +902,16 @@ static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
 	 */
 	WARN_ON_ONCE(rcuwait_active(&kvm->mn_memslots_update_rcuwait));
 
+	/*
+	 * Elevate the invalidate count to prevent concurrent walks of the user
+	 * page tables from fully succeeding if they race with teardown of the
+	 * page tables.  The elevated count will force lookups to always retry,
+	 * until eventually the lookup fails outright after teardown completes.
+	 */
+	spin_lock(&kvm->mn_invalidate_lock);
+	kvm->mn_active_invalidate_count++;
+	spin_unlock(&kvm->mn_invalidate_lock);
+
 	idx = srcu_read_lock(&kvm->srcu);
 	kvm_arch_flush_shadow_all(kvm);
 	kvm_arch_guest_memory_reclaimed(kvm);
@@ -1313,22 +1323,26 @@ static void kvm_destroy_vm(struct kvm *kvm)
 	kvm_coalesced_mmio_free(kvm);
 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
 	mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
+
 	/*
-	 * At this point, pending calls to invalidate_range_start()
-	 * have completed but no more MMU notifiers will run, so
-	 * mn_active_invalidate_count may remain unbalanced.
-	 * No threads can be waiting in kvm_swap_active_memslots() as the
-	 * last reference on KVM has been dropped, but freeing
-	 * memslots would deadlock without this manual intervention.
-	 *
-	 * If the count isn't unbalanced, i.e. KVM did NOT unregister its MMU
-	 * notifier between a start() and end(), then there shouldn't be any
-	 * in-progress invalidations.
+	 * Sanity check KVM's tracking and internal use of start()+end(), e.g.
+	 * to detect a unpaired start() or end().  KVM increments the count in
+	 * kvm_mmu_notifier_release(), so the count must be at least 1; and
+	 * if kvm->mmu_invalidate_in_progress is set, then KVM unregistered
+	 * its MMU notifier between kvm_mmu_invalidate_range_start() and
+	 * kvm_mmu_invalidate_range_end(), so kvm->mn_active_invalidate_count must
+	 * have been elevated further by kvm_mmu_notifier_invalidate_range_start().
 	 */
-	if (kvm->mn_active_invalidate_count)
-		kvm->mn_active_invalidate_count = 0;
-	else
-		WARN_ON(kvm->mmu_invalidate_in_progress);
+	WARN_ON_ONCE(kvm->mn_active_invalidate_count < 1 + !!kvm->mmu_invalidate_in_progress);
+
+	/*
+	 * Squash the invalidate count so that arch code can update memslots
+	 * as part of VM destruction.
+	 *
+	 * TODO: Drop this once KVM x86 no longer relies on setting internal
+	 *       memslots to munmap() the corresponding VMAs.
+	 */
+	kvm->mn_active_invalidate_count = 0;
 #else
 	kvm_flush_shadow_all(kvm);
 #endif
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 20/24] KVM: Invalidate all GFN=>PFN caches when VM and/or its process is dying
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (18 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 19/24] KVM: Set mn_active_invalidate_count when the VM's mm is released Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 21/24] x86/fpu: Bail early from guest XFD update if XFD isn't supported Paolo Bonzini
                   ` (4 subsequent siblings)
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Sean Christopherson, stable, Amirmohammad Eftekhar, Chris Mason

From: Sean Christopherson <seanjc@google.com>

[ Upstream commit c4424cbaaf04f3d3113d838ef836529a533b390a ]

Invalidate all GPCs in kvm_flush_shadow_all(), i.e. when a VM or its owning
process is dying, to ensure a GPC can't be used after the associated mm has
exited, e.g. via a timer callback.  Take care to invalidate the caches
before notifying arch code that memory has been reclaimed, as all in-flight
accesses must complete before KVM SEV performs cache maintenance on the
affected memory.

  BUG: KASAN: use-after-free in kvm_xen_set_evtchn_fast+0x3fe/0xfd0 [kvm]
  Write of size 8 at addr ffff88852702c800 by task swapper/4/0
  CPU: 4 UID: 0 PID: 0 Comm: swapper/4 Not tainted 7.3.0-rc3-master-g587858367581-dirty-kasan #1 PREEMPT(lazy)
  Hardware name: LENOVO 21HMCTO1WW/21HMCTO1WW, BIOS N3XET48W (1.23 ) 11/01/2023
  Call Trace:
   <IRQ>
   dump_stack_lvl+0x91/0xf0
   print_report+0xd1/0x630
   kasan_report+0xf3/0x130
   kasan_check_range+0x11c/0x200
   __kasan_check_write+0x14/0x30
   kvm_xen_set_evtchn_fast+0x3fe/0xfd0 [kvm]
   xen_timer_callback+0x1eb/0x2b0 [kvm]
   __hrtimer_run_queues+0x43e/0x8b0
   hrtimer_interrupt+0x3aa/0xa00
   __sysvec_apic_timer_interrupt+0xc1/0x3a0
   sysvec_apic_timer_interrupt+0x8d/0xd0
   </IRQ>
   <TASK>
   asm_sysvec_apic_timer_interrupt+0x1b/0x20
  The buggy address belongs to the physical page:
  page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x7965608db pfn:0x52702c
  flags: 0x17ffffc0000000(node=0|zone=2|lastcpupid=0x1fffff)
  raw: 0017ffffc0000000 ffffea0014864108 ffffea001c118e48 0000000000000000
  raw: 00000007965608db 0000000000000000 00000000ffffffff 0000000000000000
  page dumped because: kasan: bad access detected

Alternatively, invalidate_all() could reuse invalidate_start(), but that'd
require tweaking invalidate_start() to special case "all", as simply
in 0=>ULONG_MAX is subtly misleading/wrong.  Because invalidate_start()
takes an exclusive @end, using ULONG_MAX wouldn't strictly guarantee that
all GPCs are invalidated.  That's somewhat of a moot point since GPCs only
ever deal with user addresses and CONFIG_HAVE_KVM_PFNCACHE is only selected
by x86, which guarantees ULONG_MAX is a kernel address.  But getting the
logic "right" the first time is easy enough.

Reusing invalidate_start() would also be suboptimal, as it requires taking
the per-GPC lock twice.  That's extremely unlikely to matter in practice,
but again, open coding invalidate_all() isn't complex, and avoids making
invalidate_start() itself (slightly) more complex.

Fixes: 982ed0de4753 ("KVM: Reinstate gfn_to_pfn_cache with invalidation support")
Cc: stable@vger.kernel.org
Reported-by: Amirmohammad Eftekhar <amirmohammad.eftekhar@cispa.de>
Closes: https://lore.kernel.org/all/20261017193911.40182-1-amirmohammad.eftekhar@cispa.de
Suggested-by: Amirmohammad Eftekhar <amirmohammad.eftekhar@cispa.de>
Reported-by: Chris Mason <mason@kernel.org>
Suggested-by: Chris Mason <mason@kernel.org>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20261120195742.55768-6-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 virt/kvm/kvm_main.c |  1 +
 virt/kvm/kvm_mm.h   |  2 ++
 virt/kvm/pfncache.c | 12 ++++++++++++
 3 files changed, 15 insertions(+)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index bb46945cc0e4..8c9163ab16ee 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -914,6 +914,7 @@ static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
 
 	idx = srcu_read_lock(&kvm->srcu);
 	kvm_arch_flush_shadow_all(kvm);
+	gfn_to_pfn_cache_invalidate_all(kvm);
 	kvm_arch_guest_memory_reclaimed(kvm);
 	srcu_read_unlock(&kvm->srcu, idx);
 }
diff --git a/virt/kvm/kvm_mm.h b/virt/kvm/kvm_mm.h
index 41da467d99c9..3b21b82c1323 100644
--- a/virt/kvm/kvm_mm.h
+++ b/virt/kvm/kvm_mm.h
@@ -32,6 +32,7 @@ void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm,
 				       unsigned long start,
 				       unsigned long end,
 				       bool may_block);
+void gfn_to_pfn_cache_invalidate_all(struct kvm *kvm);
 #else
 static inline void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm,
 						     unsigned long start,
@@ -39,6 +40,7 @@ static inline void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm,
 						     bool may_block)
 {
 }
+static inline void gfn_to_pfn_cache_invalidate_all(struct kvm *kvm) { }
 #endif /* HAVE_KVM_PFNCACHE */
 
 #endif /* __KVM_MM_H__ */
diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c
index 7c248193ca26..679138404b0f 100644
--- a/virt/kvm/pfncache.c
+++ b/virt/kvm/pfncache.c
@@ -19,6 +19,18 @@
 
 #include "kvm_mm.h"
 
+void gfn_to_pfn_cache_invalidate_all(struct kvm *kvm)
+{
+	struct gfn_to_pfn_cache *gpc;
+
+	guard(spinlock)(&kvm->gpc_lock);
+
+	list_for_each_entry(gpc, &kvm->gpc_list, list) {
+		guard(write_lock_irq)(&gpc->lock);
+		gpc->valid = false;
+	}
+}
+
 /*
  * MMU notifier 'invalidate_range_start' hook.
  */
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 21/24] x86/fpu: Bail early from guest XFD update if XFD isn't supported
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (19 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 20/24] KVM: Invalidate all GFN=>PFN caches when VM and/or its process is dying Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 22/24] x86/fpu: Clear XSTATE_BV[i] in guest XSAVE state if guest FPU isn't in-use Paolo Bonzini
                   ` (3 subsequent siblings)
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: Sean Christopherson, stable, Jim Mattson

From: Sean Christopherson <seanjc@google.com>

[ Upstream commit 389bafa29e91827ee95127c40893e64295e4499f ]

As is, KVM allows userspace to set guest XFD to 0 even if XFD isn't
supported; KVM unarguably shouldn't try to update guest XFD when the
kernel doesn't support XFD/XSAVE, but fixing that flaw could potentially
impact KVM's uABI.  Thus, ignore KVM-requested guest XFD updates if the
kernel doesn't support XFD.

The current code is subtly fine and does not corrupt guest_fpu->fpstate
by interpreting it as an xstate structure; xsave.header.xfeatures is
modified if and only if xfd != 0, which KVM does reject.  Still, it's
more defensive to return early and WARN if XFD is not set to 0.

Cc: stable@vger.kernel.org
Reported-by: Jim Mattson <jmattson@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20261119001506.64568-2-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kernel/fpu/core.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c
index 2805ce8f0259..17cb1f5e7c3c 100644
--- a/arch/x86/kernel/fpu/core.c
+++ b/arch/x86/kernel/fpu/core.c
@@ -296,6 +296,11 @@ void fpu_update_guest_xfd(struct fpu_guest *guest_fpu, u64 xfd)
 {
 	struct fpstate *fpstate = guest_fpu->fpstate;
 
+	if (!cpu_feature_enabled(X86_FEATURE_XFD)) {
+		WARN_ON_ONCE(xfd);
+		return;
+	}
+
 	fpregs_lock();
 
 	/*
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 22/24] x86/fpu: Clear XSTATE_BV[i] in guest XSAVE state if guest FPU isn't in-use
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (20 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 21/24] x86/fpu: Bail early from guest XFD update if XFD isn't supported Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 18:04 ` [PATCH 6.1.y 23/24] x86/fpu: Ensure guest FPU is resident in hardware when updating actual XFD MSR Paolo Bonzini
                   ` (2 subsequent siblings)
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Sean Christopherson, Stefan Teodorescu, Chris Mason, Josef Bacik,
	David Woodhouse, Jim Mattson, Dave Hansen

From: Sean Christopherson <seanjc@google.com>

[ Upstream commit 28743b52a883547367ca1a8de86ecbc30fd9a660 ]

When updating a guest FPU's XFD, manually clear affected XSTATE_BV[i] state
if the FPU isn't in-use, otherwise a subsequent load of the guest's FPU
may trigger an #NM due to XSTATE_BV[i] being stale with respect to the XFD
value loaded in hardware.

  Oops: unexpected #NM exception: 0000 [#1] SMP
  CPU: 63 UID: 1000 PID: 1139 Comm: guest_xfd_nm Tainted: G        W
  Tainted: [W]=WARN
  Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015
  RIP: 0010:restore_fpregs_from_fpstate+0x36/0xa0
  Call Trace:
   <TASK>
   fpu_swap_kvm_fpstate+0x64/0x130
   kvm_arch_vcpu_ioctl_run+0x334/0x21e0 [kvm]
   kvm_vcpu_ioctl+0x2c7/0x960 [kvm]
   __x64_sys_ioctl+0x90/0xd0
   do_syscall_64+0x9c/0x4d0
   entry_SYSCALL_64_after_hwframe+0x4b/0x53
   </TASK>

Arguably, failure to ensure the FPU is loaded is a KVM bug, but addressing
that issue is non-trivial due to quirks in KVM's uABI.  KVM saves/restores
INIT and SIPI events via MP_STATE (where valid states are RUNNING, HALTED,
SHUTDOWN, WFS, etc.) instead of saving/restoring the raw INIT/SIPI events.
To avoid losing pending INIT/SIPI events, KVM processes pending INIT/SIPIs
when userspace is getting MP_STATE, i.e. outside of the KVM_RUN flow and
thus outside of kvm_arch_vcpu_ioctl_run()'s kvm_{load,put}_guest_fpu() pair.

Combined with KVM's emulation of VMX's VM-Exit MSR load/store lists (when
the vCPU is running nested), KVM will inadvertently trigger an emulated XFD
write when emulating an INIT VM-Exit outside of KVM_RUN:

  kvm_arch_vcpu_ioctl_get_mpstate()
    -> kvm_apic_accept_events()
      -> vmx_check_nested_events()
        -> nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0)
          -> load_vmcs12_host_state()
            -> nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, vmcs12->vm_exit_msr_load_count)
              -> kvm_set_msr(vcpu, MSR_IA32_XFD, 0x40000)
                -> kvm_set_msr_common()
                  -> fpu_update_guest_xfd(&vcpu->arch.guest_fpu, 0x40000)

Unconditionally doing kvm_{load,put}_guest_fpu() around INIT emulation when
handling KVM_GET_MP_STATE would fix this specific variant, but the nVMX MSR
load/store lists are problematic in general and can be reached by even more
subtle methods outside of KVM_RUN.  E.g. if userspace disables VMX entirely
by stuffing MSR_IA32_FEAT_CTL via KVM_SET_MSRS or KVM_SET_ONE_REG while L2
is active, then KVM will force the vCPU out of L2 via a (non-architectural)
nested VM-Exit.  For now, fix the immediate, urgent issue, and temporarily
defer a more complete fix for the KVM mess.

Deliberately handle the !in_use case separately in anticipation of fixing
another, similar bug for the in_use=true case.  If the guest FPU isn't in
use, i.e. isn't associated with the current task (or *any* task), then
locking the FPU regs and checking TIF_NEED_FPU_LOAD is meaningless and
unnecessary, and trying to juggle TIF_NEED_FPU_LOAD vs. in_use in the same
flow makes the entire mess inscrutable.

Similarly, duplicate the fpstate->xfd update as well (although this one is
unfortunately more likely to be permanent).  If the FPU is in-use, then XFD
needs to be updated *after* locking the FPU, otherwise trying to load FPU
state saved with the old XFD using new XFD will #NM if the old state
contains valid data for any newly-disabled features.

Fixes: b45f72177594 ("x86/fpu: Clear XSTATE_BV[i] in guest XSAVE state whenever XFD[i]=1")
Reported-by: Stefan Teodorescu <fane@google.com>
Suggested-by: Stefan Teodorescu <fane@google.com>
Reported-by: Chris Mason <mason@kernel.org>
Suggested-by: Chris Mason <mason@kernel.org>
Cc: Josef Bacik <josef@toxicpanda.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Jim Mattson <jmattson@google.com>
Acked-by: Dave Hansen <dave.hansen@linux.intel.com>
Reviewed-by: Jim Mattson <jmattson@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20261119001506.64568-3-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kernel/fpu/core.c | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c
index 17cb1f5e7c3c..99e910ebedc4 100644
--- a/arch/x86/kernel/fpu/core.c
+++ b/arch/x86/kernel/fpu/core.c
@@ -301,13 +301,24 @@ void fpu_update_guest_xfd(struct fpu_guest *guest_fpu, u64 xfd)
 		return;
 	}
 
-	fpregs_lock();
-
 	/*
 	 * KVM's guest ABI is that setting XFD[i]=1 *can* immediately revert the
 	 * save state to its initial configuration.  Likewise, KVM_GET_XSAVE does
 	 * the same as XSAVE and returns XSTATE_BV[i]=0 whenever XFD[i]=1.
 	 *
+	 * If the guest's FPU state isn't in-use, i.e. if the host FPU state is
+	 * active, then the guest FPU isn't associated with the current task and
+	 * *can't* be resident in hardware.  Update the in-memory guest fpstate
+	 * and clear all disabled components to prevent an #NM on XRSTOR if/when
+	 * the guest FPU is next loaded into hardware.
+	 */
+	if (!fpstate->in_use) {
+		fpstate->xfd = xfd;
+		fpstate->regs.xsave.header.xfeatures &= ~xfd;
+		return;
+	}
+
+	/*
 	 * If the guest's FPU state is in hardware, just update XFD: the XSAVE
 	 * in fpu_swap_kvm_fpstate will clear XSTATE_BV[i] whenever XFD[i]=1.
 	 *
@@ -315,13 +326,13 @@ void fpu_update_guest_xfd(struct fpu_guest *guest_fpu, u64 xfd)
 	 * disabled components in XSTATE_BV now, or a subsequent XRSTOR will
 	 * attempt to load disabled components and generate #NM _in the host_.
 	 */
+	fpregs_lock();
+	fpstate->xfd = xfd;
+
 	if (xfd && test_thread_flag(TIF_NEED_FPU_LOAD))
 		fpstate->regs.xsave.header.xfeatures &= ~xfd;
 
-	fpstate->xfd = xfd;
-	if (fpstate->in_use)
-		xfd_update_state(fpstate);
-
+	xfd_update_state(fpstate);
 	fpregs_unlock();
 }
 EXPORT_SYMBOL_GPL(fpu_update_guest_xfd);
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 23/24] x86/fpu: Ensure guest FPU is resident in hardware when updating actual XFD MSR
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (21 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 22/24] x86/fpu: Clear XSTATE_BV[i] in guest XSAVE state if guest FPU isn't in-use Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 19:15   ` sashiko-bot
  2026-12-04 18:04 ` [PATCH 6.1.y 24/24] KVM: VMX: Ensure pCPU is valid before sending IPI to deliver posted IRQ Paolo Bonzini
  2026-12-04 20:29 ` [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
  24 siblings, 1 reply; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Sean Christopherson, stable, Stefan Teodorescu, Chris Mason,
	Josef Bacik, David Woodhouse, Jim Mattson, Dave Hansen

From: Sean Christopherson <seanjc@google.com>

[ Upstream commit 98d7c3da6a83babc4b209656411adde144ba9ece ]

When updating a guest FPU's XFD, ensure the guest FPU state is resident in
hardware.  Failure to do so means updating XFD may effectively clobber XFD,
and in the worst case scenario can lead to an #NM due to failing to update
XFD.  Specifically, if the guest FPU is in-use (associated with the current
task) and the current task has TIF_NEED_FPU_LOAD, then lock and load the
task's FPU regs.

The underlying issue is that TIF_NEED_FPU_LOAD doesn't actually mean the
FPU isn't resident in hardware, it only says the FPU *might* not be resident
in hardware and *may* need a reload.  E.g. if the current task was tagged
with TIF_NEED_FPU_LOAD by switch_fpu(), but never evicted from hardware,
then KVM's call to switch_fpu_return() will elide the xfd_update_state()
call (and the XRSTOR) due to the FPU being valid, and run the vCPU with a
stale XFD.

E.g. on a single pCPU 'A', timeshared by two vCPUs:

  vcpu0 / fpu0                          vcpu1 / fpu1
                                        TIF_NEED_FPU_LOAD = 1
                                        <schedules out>
  dirty TMM0 (XINUSE[18] == 1)
  wrmsr(MSR_IA32_XFD, 0x40000)
  fpu_sync_guest_vmexit_xfd_state()
  <schedules out>
  TIF_NEED_FPU_LOAD = 1 (switch_fpu())
  fpu_fpregs_owner_ctx = fpu0
  fpu0->last_cpu = A
                                        <schedules in>
                                        fpu_update_guest_xfd(fpu1, 0)
                                        wrmsrq(MSR_IA32_XFD, 0)
  fpregs_state_valid() == true
  |
  -> switch_fpu_return() skips
     restore_fpregs_from_fpstate() and
     xfd_update_state()

  fpu_swap_kvm_fpstate() (exit to user)
  |
  -> fpu0->regs.xsave.header.xfeatures
     is saved with XSTATE_BV[18] = 1

  fpu_swap_kvm_fpstate() (entry from user)
  |
  -> restore_fpregs_from_fpstate() => #NM

Alternatively, fpu_update_guest_xfd() could skip xfd_update_state() *and*
force an invalidation of the current FPU (set last_cpu = -1) to ensure the
correct XFD is loaded when doing switch_fpu_return().  However, that gets
all kinds of convoluted, and emulating an XFD write with TIF_NEED_FPU_LOAD=1
is very uncommon, and in all likelihood the guest FPU is going to be loaded
into hardware in the near future anyways, i.e. avoiding (maybe) loading the
guest FPU buys nothing but complexity.

Fixes: b45f72177594 ("x86/fpu: Clear XSTATE_BV[i] in guest XSAVE state whenever XFD[i]=1")
Cc: stable@vger.kernel.org
Reported-by: Stefan Teodorescu <fane@google.com>
Suggested-by: Stefan Teodorescu <fane@google.com>
Reported-by: Chris Mason <mason@kernel.org>
Suggested-by: Chris Mason <mason@kernel.org>
Cc: Josef Bacik <josef@toxicpanda.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Jim Mattson <jmattson@google.com>
Acked-by: Dave Hansen <dave.hansen@linux.intel.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20261119001506.64568-4-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kernel/fpu/core.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c
index 99e910ebedc4..f21e42de9c25 100644
--- a/arch/x86/kernel/fpu/core.c
+++ b/arch/x86/kernel/fpu/core.c
@@ -319,19 +319,18 @@ void fpu_update_guest_xfd(struct fpu_guest *guest_fpu, u64 xfd)
 	}
 
 	/*
-	 * If the guest's FPU state is in hardware, just update XFD: the XSAVE
-	 * in fpu_swap_kvm_fpstate will clear XSTATE_BV[i] whenever XFD[i]=1.
-	 *
-	 * If however the guest's FPU state is NOT resident in hardware, clear
-	 * disabled components in XSTATE_BV now, or a subsequent XRSTOR will
-	 * attempt to load disabled components and generate #NM _in the host_.
+	 * If the guest's FPU state is in-use, then ensure the state is resident
+	 * in hardware and simply update XFD.  The guest is responsible for
+	 * clearing disabled components when doing its own XRSTOR operations,
+	 * and the XSAVE in fpu_swap_kvm_fpstate() when putting the guest FPU
+	 * will clear XSTATE_BV[i] whenever XFD[i]=1.
+	 */
+	fpregs_lock_and_load();
+	/*
+	 * Update XFD *after* loading FPU state, otherwise XRSTOR would try to
+	 * load XSTATE saved with the old XFD, but using the new XFD.
 	 */
-	fpregs_lock();
 	fpstate->xfd = xfd;
-
-	if (xfd && test_thread_flag(TIF_NEED_FPU_LOAD))
-		fpstate->regs.xsave.header.xfeatures &= ~xfd;
-
 	xfd_update_state(fpstate);
 	fpregs_unlock();
 }
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 6.1.y 24/24] KVM: VMX: Ensure pCPU is valid before sending IPI to deliver posted IRQ
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (22 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 23/24] x86/fpu: Ensure guest FPU is resident in hardware when updating actual XFD MSR Paolo Bonzini
@ 2026-12-04 18:04 ` Paolo Bonzini
  2026-12-04 19:18   ` sashiko-bot
  2026-12-04 20:29 ` [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
  24 siblings, 1 reply; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 18:04 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Sean Christopherson, stable, Stefan Teodorescu, Sagi Shahar

From: Sean Christopherson <seanjc@google.com>

[ Upstream commit 55abfb5917d39e9511ca89d37f86cdc3a1d1797b ]

Verify the target pCPU is valid before sending an IPI to trigger CPU=>CPU
posted IRQ processing, as TDX invalidates vcpu->cpu when disassociating a
pCPU from a vCPU.  Ensure the relevant reads and writes are performed once,
to prevent vcpu->cpu from being re-read after it's been validated.

If a TDX vCPU's task is migrated immediately after a VM-Exit, and the CPU
sending the IPI is sufficiently delayed, KVM will attempt to send an IPI to
pCPU '-1', generate an out-of-bounds read, and ultimately crash.

  BUG: unable to handle page fault for address: ffffffff88021be8
  #PF: supervisor read access in kernel mode
  #PF: error_code(0x0000) - not-present page
  PGD 10a0815067 P4D 10a0815067 PUD 0
  Oops: 0000 [#1] SMP NOPTI
  CPU: 0 PID: 173535 Comm: repro Tainted: G           O       6.6.103-smp-1301.106.448.13 #1
  Hardware name: Google Izumi/Izumi, BIOS 0.20260728.0-16 07/28/2026
  RIP: 0010:memcpy+0xc/0x20
  Call Trace:
   <TASK>
   ? __x2apic_send_IPI_mask+0x71/0x160
   tdx_deliver_interrupt+0x75/0xe0 [kvm_intel]
   __apic_accept_irq+0xdf/0x240 [kvm]
   kvm_irq_delivery_to_apic+0x1df/0x2e0 [kvm]
   kvm_apic_send_ipi+0xa3/0x100 [kvm]
   kvm_x2apic_icr_write+0x47/0xc0 [kvm]
   __kvm_set_msr+0xa0/0x1c0 [kvm]
   kvm_set_msr_ignored_check+0x18/0xf0 [kvm]
   kvm_emulate_wrmsr+0x74/0x180 [kvm]
   tdx_handle_exit+0x238/0x13a0 [kvm_intel]

Note, using the "mask" variant to send the IPI is stupid and inefficient,
and actively contributed to making the bug worse.  Because TDX requires
x2APIC, if KVM had used the more obvious send_IPI(), then attempting to
send an IPI to '-1' would have morphed into a broadcast the IRQ to all CPUs
instead of an out-of-bounds read.  That wart will be fixed shortly.

Note #2, the flaw has existed since commit 705699a13994 ("KVM: nVMX: Enable
nested posted interrupt processing"), but was unreachable until TDX came
along and invalidated the CPU of a live vCPU.

Note #3, __kvm_vcpu_kick() and kvm_make_vcpu_request() already guard
against an invalid pCPU.

Note #4, tdx_vcpu_free() checks vcpu->cpu without using READ_ONCE(), but
that's a-ok since the vCPU is unreachable, i.e. vcpu->cpu can't change.

Note #5, invalidating vcpu->cpu probably means TDX is triggering a VM-wide
blast of IPIs to do KVM_REQ_GLOBAL_CLOCK_UPDATE => KVM_REQ_CLOCK_UPDATE,
because kvm_arch_vcpu_load() thinks the vCPU has never been loaded on any
CPU.  That too will be fixed in the near future.

Note #6, related to note #5, invoking kvm_x86_call(vcpu_load)(vcpu, cpu) in
the middle of kvm_arch_vcpu_load() changes vcpu->cpu to -1 for TDX vCPUs,
i.e. fixing that would fix #5 as well (although it's simpler to just make
the request during vCPU creation).

Note #7, the only reason reading vcpu->cpu multiple times when loading a
vCPU isn't completely broken is because loading a vCPU is mutually
exclusive with disassociating a vCPU from a pCPU, outside of the
aforementioned self-induced pain with .vcpu_load(), thanks to commit
15945e9ec195 ("KVM: TDX: Guard VM state transitions with "all" the locks").

Fixes: d789fa6efac9 ("KVM: TDX: Handle vCPU dissociation")
Cc: stable@vger.kernel.org
Reported-by: Stefan Teodorescu <fane@google.com>
Cc: Sagi Shahar <sagis@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20261119173451.67434-1-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/vmx/vmx.c | 3 ++-
 arch/x86/kvm/x86.c     | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 28e0198c2024..516c4e083c50 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -4139,9 +4139,10 @@ static inline void kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
 		 * not wait for any requested interrupts in PIR, and sending a
 		 * notification event also results in a benign, spurious event.
 		 */
+		int cpu = READ_ONCE(vcpu->cpu);
 
 		if (vcpu != kvm_get_running_vcpu())
-			apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
+			apic->send_IPI_mask(get_cpu_mask(cpu), pi_vec);
 		return;
 	}
 #endif
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 972d0ed30d1d..b667af4fa2a7 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -4783,7 +4783,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 			kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
 		if (vcpu->cpu != cpu)
 			kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
-		vcpu->cpu = cpu;
+		WRITE_ONCE(vcpu->cpu, cpu);
 	}
 
 	kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 29+ messages in thread

* Re: [PATCH 6.1.y 03/24] KVM: x86/mmu: Move round_gfn_for_level() helper into mmu_internal.h
  2026-12-04 18:04 ` [PATCH 6.1.y 03/24] KVM: x86/mmu: Move round_gfn_for_level() helper into mmu_internal.h Paolo Bonzini
@ 2026-12-04 18:22   ` sashiko-bot
  0 siblings, 0 replies; 29+ messages in thread
From: sashiko-bot @ 2026-12-04 18:22 UTC (permalink / raw)
  To: Paolo Bonzini, kvm

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] kvm/x86: kvm_tdp_page_fault() reads zero-initialized fault->gfn
- [High] kvm: kvm_tdp_page_fault() reads zeroed fault->gfn before it is
  set
--

commit 73752e956b3b2e05f08dc7c1a2dd6aeb316925fe
Author: Hou Wenlong <houwenlong.hwl@antgroup.com>

KVM: x86/mmu: Move round_gfn_for_level() helper into mmu_internal.h

This commit moves round_gfn_for_level() from tdp_iter.c to mmu_internal.h
as gfn_round_for_level() and converts other call sites to use it.

Link: https://lore.kernel.org/r/415c64782f27444898db650e21cf28eeb6441dfa.1665214747.git.houwenlong.hwl@antgroup.com

> diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
> index 0a1318eee1ef8..72a61480890e6 100644
> --- a/arch/x86/kvm/mmu/mmu.c
> +++ b/arch/x86/kvm/mmu/mmu.c

[ ... ]

> @@ -4375,7 +4375,8 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
>  	if (shadow_memtype_mask && kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
>  		for ( ; fault->max_level > PG_LEVEL_4K; --fault->max_level) {
>  			int page_num = KVM_PAGES_PER_HPAGE(fault->max_level);
> -			gfn_t base = (fault->addr >> PAGE_SHIFT) & ~(page_num - 1);
> +			gfn_t base = gfn_round_for_level(fault->gfn,
> +							 fault->max_level);
>  
>  			if (kvm_mtrr_check_gfn_range_consistency(vcpu, base, page_num))
>  				break;

[Severity: High]
Is fault->gfn initialized yet when kvm_tdp_page_fault() runs?

In this tree, kvm_mmu_do_page_fault() leaves fault.gfn zero-initialized
and direct_page_fault() only sets it later from fault->addr.  As a result,
base is always 0 and kvm_mtrr_check_gfn_range_consistency() always checks
guest MTRRs at GFN 0 instead of the faulting GPA.

On Intel EPT VMs with non-coherent DMA, if guest low memory has uniform
MTRRs while a faulting hugepage spans mixed memory types, KVM fails to
restrict fault->max_level and maps the range as a single hugepage, risking
DMA cache-coherency corruption.

Conversely, when legacy fixed MTRRs in the first 1 MiB are mixed, every
TDP fault across the guest is demoted to 4 KiB pages.

Passing fault->addr >> PAGE_SHIFT to gfn_round_for_level() here (or
backporting the commit that initializes fault.gfn in
kvm_mmu_do_page_fault()) would avoid this regression.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204180423.2168641-1-pbonzini@redhat.com?part=3

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH 6.1.y 23/24] x86/fpu: Ensure guest FPU is resident in hardware when updating actual XFD MSR
  2026-12-04 18:04 ` [PATCH 6.1.y 23/24] x86/fpu: Ensure guest FPU is resident in hardware when updating actual XFD MSR Paolo Bonzini
@ 2026-12-04 19:15   ` sashiko-bot
  0 siblings, 0 replies; 29+ messages in thread
From: sashiko-bot @ 2026-12-04 19:15 UTC (permalink / raw)
  To: Paolo Bonzini, kvm

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] x86/fpu: undefined fpregs_lock_and_load() call in
  fpu_update_guest_xfd()
--

commit 1699a8ac95cf60d653358ccad0388430af2ea364
Author: Sean Christopherson <seanjc@google.com>

x86/fpu: Ensure guest FPU is resident in hardware when updating actual XFD MSR

This commit updates fpu_update_guest_xfd() to ensure the guest FPU state is
resident in hardware before updating the XFD MSR, replacing fpregs_lock() and
conditional XSTATE_BV clearing with fpregs_lock_and_load().

Link: https://patch.msgid.link/20261119001506.64568-4-seanjc@google.com

> diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c
> index 99e910ebedc46..f21e42de9c252 100644
> --- a/arch/x86/kernel/fpu/core.c
> +++ b/arch/x86/kernel/fpu/core.c
> @@ -319,19 +319,18 @@ void fpu_update_guest_xfd(struct fpu_guest *guest_fpu, u64 xfd)
>  	}
>  
>  	/*
> -	 * If the guest's FPU state is in hardware, just update XFD: the XSAVE
> -	 * in fpu_swap_kvm_fpstate will clear XSTATE_BV[i] whenever XFD[i]=1.
> -	 *
> -	 * If however the guest's FPU state is NOT resident in hardware, clear
> -	 * disabled components in XSTATE_BV now, or a subsequent XRSTOR will
> -	 * attempt to load disabled components and generate #NM _in the host_.
> +	 * If the guest's FPU state is in-use, then ensure the state is resident
> +	 * in hardware and simply update XFD.  The guest is responsible for
> +	 * clearing disabled components when doing its own XRSTOR operations,
> +	 * and the XSAVE in fpu_swap_kvm_fpstate() when putting the guest FPU
> +	 * will clear XSTATE_BV[i] whenever XFD[i]=1.
> +	 */
> +	fpregs_lock_and_load();

[Severity: High]
Does this break builds with CONFIG_X86_64 and CONFIG_KVM enabled?

fpregs_lock_and_load() is neither declared nor defined in this 6.1.y tree,
resulting in an implicit declaration and link failure.  Existing callers such
as fpu_clone() open-code fpregs_lock() followed by a TIF_NEED_FPU_LOAD check
and fpregs_restore_userregs() instead.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204180423.2168641-1-pbonzini@redhat.com?part=23

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH 6.1.y 24/24] KVM: VMX: Ensure pCPU is valid before sending IPI to deliver posted IRQ
  2026-12-04 18:04 ` [PATCH 6.1.y 24/24] KVM: VMX: Ensure pCPU is valid before sending IPI to deliver posted IRQ Paolo Bonzini
@ 2026-12-04 19:18   ` sashiko-bot
  0 siblings, 0 replies; 29+ messages in thread
From: sashiko-bot @ 2026-12-04 19:18 UTC (permalink / raw)
  To: Paolo Bonzini, kvm

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] KVM: VMX: missing pCPU validity check in
  kvm_vcpu_trigger_posted_interrupt()
--

commit 3b3c27514b94a3a527d60347b3acb2bdda3a5b8b
Author: Sean Christopherson <seanjc@google.com>

KVM: VMX: Ensure pCPU is valid before sending IPI to deliver posted IRQ

This backport wraps the vcpu->cpu read in kvm_vcpu_trigger_posted_interrupt()
with READ_ONCE() and the vcpu->cpu write in kvm_arch_vcpu_load() with
WRITE_ONCE().

Link: https://patch.msgid.link/20261119173451.67434-1-seanjc@google.com

> diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
> index 28e0198c20246..516c4e083c50c 100644
> --- a/arch/x86/kvm/vmx/vmx.c
> +++ b/arch/x86/kvm/vmx/vmx.c
> @@ -4139,9 +4139,10 @@ static inline void kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
>  		 * not wait for any requested interrupts in PIR, and sending a
>  		 * notification event also results in a benign, spurious event.
>  		 */
> +		int cpu = READ_ONCE(vcpu->cpu);
>  
>  		if (vcpu != kvm_get_running_vcpu())
> -			apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
> +			apic->send_IPI_mask(get_cpu_mask(cpu), pi_vec);
>  		return;
>  	}
>  #endif

[Severity: High]
Did this backport drop the target pCPU validity check on cpu?

kvm_vcpu_trigger_posted_interrupt() snapshots vcpu->cpu with READ_ONCE(),
but passes cpu straight to get_cpu_mask() without checking that it is
valid (such as cpu >= 0 or cpu != -1).

When vcpu->cpu is -1 on trees with vCPU dissociation such as TDX,
get_cpu_mask() converts -1 to an unsigned int and returns an
out-of-bounds cpumask pointer, causing an out-of-bounds read and page
fault in apic->send_IPI_mask().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204180423.2168641-1-pbonzini@redhat.com?part=24

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities
  2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
                   ` (23 preceding siblings ...)
  2026-12-04 18:04 ` [PATCH 6.1.y 24/24] KVM: VMX: Ensure pCPU is valid before sending IPI to deliver posted IRQ Paolo Bonzini
@ 2026-12-04 20:29 ` Paolo Bonzini
  24 siblings, 0 replies; 29+ messages in thread
From: Paolo Bonzini @ 2026-12-04 20:29 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: linux-kernel, kvm

I didn't mean to send this, as it was prepared but (clearly) not tested.
Too many branches to send out...

I do intend to test and re-post it, but it will take a few days as there
is a public holiday early next week.

On Fri, 04 Dec 2026 19:03:59 +0100, Paolo Bonzini wrote:
> Loongarch:
> - Flush TLBs *before* dropping mmu_lock
>
> RISC-V:
> - Flush TLBs *before* dropping mmu_lock
>
> x86:
> - Flush TLB *before* dropping mmu_lock, and flush them entirely
> - Flush TLBs if child SPTE is zapped when splitting a huge SPTE
> - Pin kvm->mm prior to borrowing mm when setting event channel
> - Disable hypervisor (VMX Preemption) timer if TSC deadline wraps
> - Reset local APIC timer metadata on RESET, INIT, and KVM_SET_LAPIC
> - Ensure guest FPU is resident in hardware when updating actual XFD MSR
> - Ensure pCPU is valid before sending IPI to deliver posted IRQ
>
> Generic:
> - Invalidate all GFN=>PFN caches when VM and/or its process is dying
>
> Paolo
>
> Hou Wenlong (3):
>   KVM: x86/mmu: Move round_gfn_for_level() helper into mmu_internal.h
>   KVM: x86/mmu: Fix wrong gfn range of tlb flushing in
>     kvm_set_pte_rmapp()
>   KVM: x86/mmu: Fix wrong start gfn of tlb flushing with range
>
> Jun Miao (1):
>   KVM: Fix comments that refer to the non-existent
>     install_new_memslots()
>
> Kyle Zeng (1):
>   KVM: x86/mmu: Flush all TLB entries, not a precise range, when
>     yielding
>
> Peter Zijlstra (1):
>   ptrace: Convert ptrace_attach() to use lock guards
>
> Sean Christopherson (18):
>   KVM: x86: Grab lapic_timer in a local variable to cleanup periodic
>     code
>   KVM: x86/mmu: Flush TLBs if child SPTE is zapped when splitting a huge
>     SPTE
>   KVM: x86/mmu: Propagate the return from kvm_zap_all_rmap_sptes() out
>     of __rmap_add()
>   KVM: x86: Disable hypervisor (VMX Preemption) timer if TSC deadline
>     wraps
>   KVM: x86: Reset local APIC timer metadata on RESET, INIT, and
>     KVM_SET_LAPIC
>   KVM: VMX: Retry APIC-access page reload if invalidation is in-progress
>   KVM: x86: Use standard mmu_notifier invalidate hooks for APIC access
>     page
>   KVM: x86/mmu: Flush TLBs in kvm_unmap_gfn_range() if the unmap may
>     block/yield
>   KVM: x86/xen: Pin kvm->mm prior to borrowing mm when setting event
>     channel
>   KVM: Fold kvm_flush_shadow_all() into kvm_mmu_notifier_release()
>   KVM: WARN if there are dangling MMU invalidations at VM destruction
>   KVM: WARN if there are in-flight memslot updates when a VM's process
>     exits
>   KVM: Set mn_active_invalidate_count when the VM's mm is released
>   KVM: Invalidate all GFN=>PFN caches when VM and/or its process is
>     dying
>   x86/fpu: Bail early from guest XFD update if XFD isn't supported
>   x86/fpu: Clear XSTATE_BV[i] in guest XSAVE state if guest FPU isn't
>     in-use
>   x86/fpu: Ensure guest FPU is resident in hardware when updating actual
>     XFD MSR
>   KVM: VMX: Ensure pCPU is valid before sending IPI to deliver posted
>     IRQ
>
>  Documentation/virt/kvm/locking.rst |   2 +-
>  arch/x86/kernel/fpu/core.c         |  37 ++++---
>  arch/x86/kvm/lapic.c               |  79 ++++++++++-----
>  arch/x86/kvm/mmu/mmu.c             |  75 ++++++++++----
>  arch/x86/kvm/mmu/mmu_internal.h    |  14 +++
>  arch/x86/kvm/mmu/paging_tmpl.h     |   5 +-
>  arch/x86/kvm/mmu/tdp_iter.c        |  11 +--
>  arch/x86/kvm/vmx/vmx.c             |  53 ++++++++--
>  arch/x86/kvm/x86.c                 |  16 +--
>  arch/x86/kvm/xen.c                 |   7 +-
>  include/linux/kvm_host.h           |   7 +-
>  include/linux/sched/task.h         |   2 +
>  include/linux/spinlock.h           |  26 +++++
>  kernel/ptrace.c                    | 154 ++++++++++++++---------------
>  virt/kvm/kvm_main.c                |  78 +++++++++------
>  virt/kvm/kvm_mm.h                  |   2 +
>  virt/kvm/pfncache.c                |  12 +++
>  17 files changed, 374 insertions(+), 206 deletions(-)
>
> -- 
> 2.56.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

end of thread, other threads:[~2026-12-04 20:29 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 18:03 [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 01/24] ptrace: Convert ptrace_attach() to use lock guards Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 02/24] KVM: x86: Grab lapic_timer in a local variable to cleanup periodic code Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 03/24] KVM: x86/mmu: Move round_gfn_for_level() helper into mmu_internal.h Paolo Bonzini
2026-12-04 18:22   ` sashiko-bot
2026-12-04 18:04 ` [PATCH 6.1.y 04/24] KVM: x86/mmu: Fix wrong gfn range of tlb flushing in kvm_set_pte_rmapp() Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 05/24] KVM: x86/mmu: Fix wrong start gfn of tlb flushing with range Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 06/24] KVM: x86/mmu: Flush TLBs if child SPTE is zapped when splitting a huge SPTE Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 07/24] KVM: x86/mmu: Propagate the return from kvm_zap_all_rmap_sptes() out of __rmap_add() Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 08/24] KVM: x86: Disable hypervisor (VMX Preemption) timer if TSC deadline wraps Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 09/24] KVM: x86: Reset local APIC timer metadata on RESET, INIT, and KVM_SET_LAPIC Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 10/24] KVM: x86/mmu: Flush all TLB entries, not a precise range, when yielding Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 11/24] KVM: VMX: Retry APIC-access page reload if invalidation is in-progress Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 12/24] KVM: x86: Use standard mmu_notifier invalidate hooks for APIC access page Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 13/24] KVM: x86/mmu: Flush TLBs in kvm_unmap_gfn_range() if the unmap may block/yield Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 14/24] KVM: x86/xen: Pin kvm->mm prior to borrowing mm when setting event channel Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 15/24] KVM: Fold kvm_flush_shadow_all() into kvm_mmu_notifier_release() Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 16/24] KVM: WARN if there are dangling MMU invalidations at VM destruction Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 17/24] KVM: WARN if there are in-flight memslot updates when a VM's process exits Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 18/24] KVM: Fix comments that refer to the non-existent install_new_memslots() Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 19/24] KVM: Set mn_active_invalidate_count when the VM's mm is released Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 20/24] KVM: Invalidate all GFN=>PFN caches when VM and/or its process is dying Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 21/24] x86/fpu: Bail early from guest XFD update if XFD isn't supported Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 22/24] x86/fpu: Clear XSTATE_BV[i] in guest XSAVE state if guest FPU isn't in-use Paolo Bonzini
2026-12-04 18:04 ` [PATCH 6.1.y 23/24] x86/fpu: Ensure guest FPU is resident in hardware when updating actual XFD MSR Paolo Bonzini
2026-12-04 19:15   ` sashiko-bot
2026-12-04 18:04 ` [PATCH 6.1.y 24/24] KVM: VMX: Ensure pCPU is valid before sending IPI to deliver posted IRQ Paolo Bonzini
2026-12-04 19:18   ` sashiko-bot
2026-12-04 20:29 ` [PATCH 6.1.y 00/24] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox