All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "David Woodhouse" <dwmw2@infradead.org>
Cc: kvm@vger.kernel.org, linux-rt-devel@lists.linux.dev,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 01/17] KVM: pfncache: Use atomic SRCU for readers instead of a rwlock
Date: Sun, 20 Sep 2026 21:55:41 +0000	[thread overview]
Message-ID: <20260920215542.475D31F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260920211920.928306-2-dwmw2@infradead.org>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] kvm: concurrent MMU notifiers skip SRCU wait causing Use-After-Free
- [High] kvm: mmu_notifier_invalidate_range_start unconditionally sleeps, breaking OOM reaper
- [Low] kvm: compilation failure due to fabricated atomic SRCU API
--

commit 47eed0ef4a708a1117920edb2f9b64f3a7b2a8d4
Author: David Woodhouse <dwmw@amazon.co.uk>
KVM: pfncache: Use atomic SRCU for readers instead of a rwlock

This commit replaces the per-cache rwlock with a dedicated per-VM atomic
SRCU domain (kvm->gpc_srcu) for the read side in the KVM pfncache. Readers
now run under an atomic SRCU read lock, allowing them to execute in hardirq
and sched-out paths without spinning or failing.

Reported-by: syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=208f7f3e5f59c11aeb90
Reported-by: syzbot+919877893c9d28162dc2@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=919877893c9d28162dc2

> diff --git a/include/linux/srcu.h b/include/linux/srcu.h
> --- a/include/linux/srcu.h
> +++ b/include/linux/srcu.h
> @@ -635,6 +635,13 @@ DEFINE_LOCK_GUARD_1(srcu, struct srcu_struct,
>  DECLARE_LOCK_GUARD_1_ATTRS(srcu, __acquires_shared(_T), __releases_shared(*(struct srcu_struct **)_T))
>  #define class_srcu_constructor(_T) WITH_LOCK_GUARD_1_ATTRS(srcu, _T)
>  
> +DEFINE_LOCK_GUARD_1(srcu_atomic, struct srcu_struct,
> +		    _T->idx = srcu_read_lock_atomic(_T->lock),
> +		    srcu_read_unlock_atomic(_T->lock, _T->idx),
> +		    int idx)
> +DECLARE_LOCK_GUARD_1_ATTRS(srcu_atomic, __acquires_shared(_T), __releases_shared(*(struct srcu_struct **)_T))

[Severity: Low]
Does this code rely on a fabricated atomic SRCU API? 

The functions srcu_read_lock_atomic() and srcu_read_unlock_atomic() do not
exist in the Linux kernel tree, which will cause a build failure when
compiling.

> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -1152,6 +1152,8 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
>  		goto out_err_no_srcu;
>  	if (init_srcu_struct(&kvm->irq_srcu))
>  		goto out_err_no_irq_srcu;
> +	if (init_srcu_struct_atomic(&kvm->gpc_srcu))
> +		goto out_err_no_gpc_srcu;

[Severity: Low]
Is init_srcu_struct_atomic() a valid function? 

Using a non-existent API here will also result in a compilation error.

> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -1719,18 +1719,18 @@ static void kvm_setup_guest_pvclock(struct pvclock_vcpu_time_info *ref_hv_clock,
>  {
>  	struct pvclock_vcpu_time_info *guest_hv_clock;
>  	struct pvclock_vcpu_time_info hv_clock;
> -	unsigned long flags;
> +	int idx;
>  
>  	memcpy(&hv_clock, ref_hv_clock, sizeof(hv_clock));
>  
> -	read_lock_irqsave(&gpc->lock, flags);
> +	idx = srcu_read_lock_atomic(&vcpu->kvm->gpc_srcu);

[Severity: Low]
Could this use of srcu_read_lock_atomic() break the build since the function
does not exist?

> diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c
> --- a/virt/kvm/pfncache.c
> +++ b/virt/kvm/pfncache.c
> @@ -26,35 +26,115 @@ void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm, unsigned long start,

[ ... ]

>  		unsigned long gpc_uhva;
>  		int old, new;
>  
> -		read_lock_irq(&gpc->lock);
> +		old = atomic_read_acquire(&gpc->state);
> +		if (!(old & (GPC_VALID | GPC_BECOMING_VALID)))
> +			continue;

[Severity: Critical]
Can this condition cause a use-after-free when concurrent MMU notifiers run?

If Thread A clears GPC_VALID (setting GPC_INVALIDATING), drops gpc_lock, and
blocks in synchronize_srcu_expedited(), a concurrent Thread B could read
gpc->state as GPC_INVALIDATING. 

Thread B would hit this check, skip the cache, and return immediately without
waiting for its own grace period. 

The core MM could then free the physical page while Thread A is still waiting
for KVM readers that are actively dereferencing the freed page.

[ ... ]

> +	 * (The one allocation in the SRCU grace-period machinery itself —
> +	 * the srcu_node tree on the one-time upgrade of a contended
> +	 * srcu_struct — is dealt with separately, by having srcu_gp_end()
> +	 * allocate with GFP_NOWAIT and fall back to a preallocated spare,
> +	 * so the grace period never blocks in reclaim either.)
> +	 */
> +	if (cleared)
> +		synchronize_srcu_expedited(&kvm->gpc_srcu);
> +
> +	/*

[Severity: High]
Does this code unconditionally sleep inside an MMU notifier callback?

The OOM reaper sets range->may_block to false and must not be blocked. Calling
synchronize_srcu_expedited() here violates that constraint and could lead to
deadlocks if the OOM reaper blocks on resources held by the dying process.

[ ... ]

> +	 * GPC_VALID precisely so that this path knows those readers may
> +	 * exist. Our own grace-period wait below necessarily begins after
> +	 * that clear, so waiting here covers them; only then may
> +	 * reader-visible fields (uhva, gpa, memslot, and later pfn/khva) be
> +	 * mutated or the old mapping torn down.
> +	 */
> +	must_drain = atomic_xchg(&gpc->state, 0) &
> +		     (GPC_VALID | GPC_INVALIDATING);
> +	if (must_drain)
> +		synchronize_srcu_expedited(&gpc->kvm->gpc_srcu);
>  
>  	old_pfn = gpc->pfn;

[Severity: High]
Are we using a sleeping synchronization function in the mutator path?

This appears to contradict the commit message's claim that readers are waited
on by a spinning atomic domain.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260920211920.928306-1-dwmw2@infradead.org?part=1

  reply	other threads:[~2026-09-20 21:55 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-20 20:49 [PATCH 00/17] KVM: Use atomic SRCU for gfn-to-pfn cache, reinstate guest mode for x86 nesting David Woodhouse
2026-09-20 20:49 ` [PATCH 01/17] KVM: pfncache: Use atomic SRCU for readers instead of a rwlock David Woodhouse
2026-09-20 21:55   ` sashiko-bot [this message]
2026-09-21  1:42   ` Hillf Danton
2026-09-21  9:54     ` David Woodhouse
2026-09-21 11:21       ` Hillf Danton
2026-09-21 11:29         ` Paolo Bonzini
2026-09-20 20:49 ` [PATCH 02/17] KVM: x86/xen: Extract delivery of event to vCPU into a separate helper David Woodhouse
2026-09-20 20:49 ` [PATCH 03/17] KVM: x86/xen: Explicitly tag "shared info" page as never being dirty tracked David Woodhouse
2026-09-20 20:49 ` [PATCH 04/17] KVM: x86/xen: Don't dirty track "vCPU info" page David Woodhouse
2026-09-20 20:49 ` [PATCH 05/17] KVM: x86: Request the guest TLB flush from record_steal_time() David Woodhouse
2026-09-20 20:49 ` [PATCH 06/17] KVM: x86: Use gfn_to_pfn_cache for steal time / preempted status David Woodhouse
2026-09-20 22:06   ` sashiko-bot
2026-09-20 20:49 ` [PATCH 07/17] KVM: pfncache: Add guest-mode pinning (GUEST_USES_PFN successor) David Woodhouse
2026-09-20 21:53   ` sashiko-bot
2026-09-21 14:17     ` David Woodhouse
2026-09-20 20:49 ` [PATCH 08/17] KVM: pfncache: Return -EAGAIN for a lookup which hits an invalid memslot David Woodhouse
2026-09-20 21:53   ` sashiko-bot
2026-09-20 20:49 ` [PATCH 09/17] KVM: x86: Post KVM_REQ_GET_NESTED_STATE_PAGES on memslot updates David Woodhouse
2026-09-20 20:49 ` [PATCH 10/17] KVM: nVMX: Implement cache for L1 MSR bitmap David Woodhouse
2026-09-20 21:56   ` sashiko-bot
2026-09-20 20:49 ` [PATCH 11/17] KVM: nVMX: Use pinned pfncache for L1 APIC virtualization pages David Woodhouse
2026-09-20 21:57   ` sashiko-bot
2026-09-21 14:31     ` David Woodhouse
2026-09-20 20:49 ` [PATCH 12/17] KVM: selftests: Add nested VMX APIC cache invalidation test David Woodhouse
2026-09-20 21:51   ` sashiko-bot
2026-09-20 20:49 ` [PATCH 13/17] KVM: x86: Move nested GPC lock helpers to x86.h as kvm_gpc_lock_page() David Woodhouse
2026-09-20 20:49 ` [PATCH 14/17] KVM: nSVM: Use a gfn_to_pfn_cache for the vmcb12 page David Woodhouse
2026-09-20 20:49 ` [PATCH 15/17] KVM: nSVM: Cache L1's MSR permissions map pages David Woodhouse
2026-09-20 20:49 ` [PATCH 16/17] KVM: nSVM: Cache L1's IO " David Woodhouse
2026-09-20 20:49 ` [PATCH 17/17] KVM: selftests: Add nested transition benchmark David Woodhouse
2026-09-20 21:52   ` sashiko-bot
2026-09-21 14:10 ` [PATCH 00/17] KVM: Use atomic SRCU for gfn-to-pfn cache, reinstate guest mode for x86 nesting David Woodhouse
2026-09-22  3:16 ` KunWu Chan
2026-09-22 10:37   ` David Woodhouse
2026-09-23  9:54     ` Kunwu Chan
2026-09-23 12:05       ` David Woodhouse
2026-09-23 15:58         ` KunWu Chan
2026-09-23 16:07           ` David Woodhouse
2026-09-23 16:26             ` KunWu Chan
2026-09-23 16:46               ` David Woodhouse
2026-09-24  1:04                 ` KunWu Chan
2026-09-23 21:56               ` David Woodhouse
2026-09-24  0:59                 ` KunWu Chan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260920215542.475D31F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dwmw2@infradead.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.