Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Amit Machhiwal <amachhiw@linux.ibm.com>
To: sashiko-reviews@lists.linux.dev
Cc: Amit Machhiwal <amachhiw@linux.ibm.com>, kvm@vger.kernel.org
Subject: Re: [PATCH v2 2/3] KVM: PPC: Book3S HV: Add preempt_disable() around virtual-mode HPTE bit-lock users
Date: Mon, 5 Oct 2026 23:27:08 +0530	[thread overview]
Message-ID: <20261005232317.1da37e05-5f-amachhiw@linux.ibm.com> (raw)
In-Reply-To: <20260930175243.54FF71F00898@smtp.kernel.org>

On 2026/09/30 05:52 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
> - [High] kvm: ppc: deadlock in lock_rmap() due to preemption guard in HPT hcalls
> - [Medium] kvm: ppc: resize_hpt_rehash_hpte() spins with preemption disabled
> --
> 
> commit 68aaf6c48f58de2d582ba27ebef746d63f655779
> Author: Amit Machhiwal <amachhiw@linux.ibm.com>
> 
> KVM: PPC: Book3S HV: Add preempt_disable() around virtual-mode HPTE bit-lock users
> 
> This patch adds preemption disable guards around virtual-mode HPTE bit-lock
> users in KVM PPC. This prevents situations where a lock-holding thread
> gets preempted, starving other threads spinning on the same lock.
> 
> > diff --git a/arch/powerpc/kvm/book3s_64_mmu_hv.c b/arch/powerpc/kvm/book3s_64_mmu_hv.c
> > index 2ccb3d138f46c..59da958e09cb4 100644
> > --- a/arch/powerpc/kvm/book3s_64_mmu_hv.c
> > +++ b/arch/powerpc/kvm/book3s_64_mmu_hv.c
> 
> [ ... ]
> 
> > @@ -1219,6 +1229,7 @@ static unsigned long resize_hpt_rehash_hpte(struct kvm_resize_hpt *resize,
> >  	if (!(vpte & HPTE_V_VALID) && !(vpte & HPTE_V_ABSENT))
> >  		return 0; /* nothing to do */
> >  
> > +	preempt_disable();
> >  	while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
> >  		cpu_relax();
> >  
> 
> [Severity: Medium]
> The commit message states: "On failed lock acquisition the guard is released
> before the cpu_relax() spin so the lock owner can be scheduled."
> 
> Does this unconditional spin with preemption disabled violate that
> description?

Acknowledged.  The commit message description of the preempt_disable() guard is
imprecise for resize_hpt_rehash_hpte().  I'll look into clarifying it.

> 
> [ ... ]
> 
> > diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c
> > index aa51968e206a7..0b7743bb89d9a 100644
> > --- a/arch/powerpc/kvm/book3s_hv.c
> > +++ b/arch/powerpc/kvm/book3s_hv.c
> 
> [ ... ]
> 
> > @@ -1212,9 +1216,11 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
> >  	case H_CLEAR_REF:
> >  	case H_PROTECT:
> >  	case H_BULK_REMOVE:
> > +		preempt_disable();
> >  		idx = srcu_read_lock(&kvm->srcu);
> >  		ret = kvmppc_pseries_do_hpt_hcall(vcpu, req);
> >  		srcu_read_unlock(&kvm->srcu, idx);
> > +		preempt_enable();
> >  		if (ret == H_TOO_HARD)
> >  			return RESUME_HOST;
> >  		break;
> 
> [Severity: High]
> Does this preemption guard create a permanent deadlock with lock_rmap()?
> 
> For example, a host thread executing kvm_test_clear_dirty_npages() or
> kvm_unmap_rmapp() acquires lock_rmap() with preemption enabled:
> 
> arch/powerpc/kvm/book3s_64_mmu_hv.c:kvm_test_clear_dirty_npages() {
>     ...
>     retry:
>         lock_rmap(rmapp);
>     ...
> }
> 
> If the host thread is preempted while holding the bit-lock, and a VCPU
> thread for the same guest is scheduled on the same CPU, it can execute an
> H_ENTER hypercall.
> 
> When the VCPU thread reaches kvmppc_do_h_enter(), it attempts to acquire
> the same rmap lock:
> 
> arch/powerpc/kvm/book3s_hv_rm_mmu.c:kvmppc_do_h_enter() {
>     ...
>     if (realmode)
>         rmap = real_vmalloc_addr(rmap);
>     lock_rmap(rmap);
>     ...
> }
> 
> Since preemption is now disabled across the entire hcall by this patch,
> the VCPU thread could spin indefinitely on contention. It would never yield
> the CPU, meaning the preempted host thread could never resume to release
> the lock.

This is correct.  The v2 fix placed preempt_disable() just before try_lock_hpte(),
but lock_rmap() is acquired earlier in kvm_unmap_rmapp() and kvm_age_rmapp()
with preemption still enabled — leaving a residual window where a host thread
can be preempted while holding lock_rmap() alone.  I'll look into fixing this.

Thanks,
Amit

  reply	other threads:[~2026-10-05 17:49 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-30 17:37 [PATCH v2 0/3] KVM: PPC: Fixes for Book3S HV HPT locking and paired-single decoding Amit Machhiwal
2026-09-30 17:37 ` [PATCH v2 1/3] KVM: PPC: Book3S HV: Add SRCU protection for virtual-mode HPT hcalls Amit Machhiwal
2026-09-30 17:37 ` [PATCH v2 2/3] KVM: PPC: Book3S HV: Add preempt_disable() around virtual-mode HPTE bit-lock users Amit Machhiwal
2026-09-30 17:52   ` sashiko-bot
2026-10-05 17:57     ` Amit Machhiwal [this message]
2026-10-05 14:28   ` Shrikanth Hegde
2026-10-05 18:27     ` Amit Machhiwal
2026-10-06  3:01       ` Shrikanth Hegde
2026-09-30 17:37 ` [PATCH v2 3/3] KVM: PPC: Fix get_d_signext() 12-bit displacement for paired-single D-form Amit Machhiwal
2026-10-06  3:27   ` Shrikanth Hegde

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=20261005232317.1da37e05-5f-amachhiw@linux.ibm.com \
    --to=amachhiw@linux.ibm.com \
    --cc=kvm@vger.kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox