Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Can Qi" <qican5708@163.com>
Cc: "open list:KERNEL VIRTUAL MACHINE FOR RISC-V (KVM/riscv)"
	<kvm@vger.kernel.org>
Subject: Re: [PATCH v6] KVM: riscv: Allow rescheduling during G-stage unmap and teardown
Date: Thu, 03 Dec 2026 08:04:05 +0000	[thread overview]
Message-ID: <sashiko-outbox-247334@kernel.org> (raw)
In-Reply-To: <20261203073501.17161-1-qican5708@163.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] KVM: riscv: PMD yield flushes hit hfence_queue and per-page
  loop
--

commit a9cf70d57632be8dd08b712ee49dab08ded702eb
Author: Can Qi <qican5708@163.com>

KVM: riscv: Allow rescheduling during G-stage unmap and teardown

This commit chunks sleepable G-stage unmaps in PMD-sized spans so mmu_lock
can be dropped between batches, and detaches the G-stage root before
recursively freeing page tables outside mmu_lock.

> diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c
> index e92d564f9c778..29f5de42fa371 100644
> --- a/arch/riscv/kvm/mmu.c
> +++ b/arch/riscv/kvm/mmu.c
> @@ -85,20 +85,56 @@ int kvm_riscv_mmu_ioremap(struct kvm *kvm, gpa_t gpa, phys_addr_t hpa,
>  	return ret;
>  }
>  
> -void kvm_riscv_mmu_iounmap(struct kvm *kvm, gpa_t gpa, unsigned long size)
> +/*
> + * Process one PMD-sized span at a time, so base-page mappings cannot
> + * hold mmu_lock for an entire large memslot when rescheduling is needed.
> + * Keep one batch of invalidations for as long as the write lock is held.
> + */
> +static void mmu_unmap_range(struct kvm *kvm, gpa_t addr, gpa_t end, bool may_block)
>  {
> -	struct kvm_gstage gstage;
> -	bool flush;
> +	gfn_t batch_start = addr >> PAGE_SHIFT;
> +	bool flush = false;
>  
> -	kvm_riscv_gstage_init(&gstage, kvm);
> +	lockdep_assert_held_write(&kvm->mmu_lock);
>  
> -	write_lock(&kvm->mmu_lock);
> -	flush = kvm_riscv_gstage_unmap_range(&gstage, gpa, size, false);
> -	write_unlock(&kvm->mmu_lock);
> +	while (addr < end) {
> +		gpa_t chunk_end = may_block ? min(end, ALIGN(addr + 1, PMD_SIZE)) : end;
> +		struct kvm_gstage gstage;
> +		gfn_t next_gfn;
> +		s64 next;
> +
> +		/* Teardown can detach the root while the lock is dropped. */
> +		kvm_riscv_gstage_init(&gstage, kvm);
> +		if (!gstage.pgd)
> +			break;
> +
> +		next = kvm_riscv_gstage_unmap_range(&gstage, addr, end, chunk_end, &flush);
> +		if (WARN_ON_ONCE(next < 0))
> +			break;
> +
> +		addr = next;
> +		if (!may_block || addr == end ||
> +		    !(need_resched() || rwlock_needbreak(&kvm->mmu_lock)))
> +			continue;
> +
> +		/* A concurrent invalidator must not miss PTEs we have cleared. */
> +		next_gfn = addr >> PAGE_SHIFT;
> +		if (flush)
> +			kvm_flush_remote_tlbs_range(kvm, batch_start, next_gfn - batch_start);
> +		flush = false;
> +		batch_start = next_gfn;
> +		cond_resched_rwlock_write(&kvm->mmu_lock);
> +	}

[Severity: Medium]
Can yielding after a single PMD chunk trigger per-page HFENCE.GVMA loops
and overflow the vCPU hfence_queue?

When kvm_arch_flush_shadow_memslot() removes a large populated memslot
under mmu_lock contention, mmu_unmap_range() can yield after one PMD_SIZE
chunk and call kvm_flush_remote_tlbs_range() for PTRS_PER_PTE pages (512
on RV64, 1024 on RV32) while holding write_lock(&kvm->mmu_lock).

kvm_riscv_local_hfence_gvma_vmid_gpa() only falls back to a full-VMID
flush when the page count exceeds PTRS_PER_PTE:

arch/riscv/kvm/tlb.c:kvm_riscv_local_hfence_gvma_vmid_gpa() {
    ...
	if (PTRS_PER_PTE < (gpsz >> order)) {
		kvm_riscv_local_hfence_gvma_vmid_all(vmid);
		return;
	}
    ...
}

Because a single PMD spans exactly PTRS_PER_PTE pages, each yield runs
512 (RV64) or 1024 (RV32) per-page HFENCE.GVMA instructions per remote
vCPU synchronously under mmu_lock, instead of one full-VMID flush.

Each yield also enqueues an entry in every vCPU's 64-entry hfence_queue.
Even after 64 yields overflow into KVM_REQ_TLB_FLUSH,
kvm_riscv_check_vcpu_requests() still drains all 64 entries after the full
flush, running 32,768 (RV64) or 65,536 (RV32) redundant HFENCE.GVMA calls.

Using kvm_flush_remote_tlbs() before yielding would coalesce into a single
KVM_REQ_TLB_FLUSH and avoid both the per-page loop and hfence_queue growth.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203073501.17161-1-qican5708@163.com?part=1

      reply	other threads:[~2026-12-03  8:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  7:35 [PATCH v6] KVM: riscv: Allow rescheduling during G-stage unmap and teardown Can Qi
2026-12-03  8:04 ` sashiko-bot [this message]

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=sashiko-outbox-247334@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=qican5708@163.com \
    --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