From: sashiko-bot@kernel.org
To: "Can Qi" <qican5708@163.com>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH] KVM: riscv: Allow rescheduling during G-stage unmap
Date: Mon, 23 Nov 2026 06:24:53 +0000 [thread overview]
Message-ID: <sashiko-outbox-228763@kernel.org> (raw)
In-Reply-To: <20261123055036.1554937-1-qican5708@163.com>
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] KVM: riscv: redundant TLB flush after
kvm_riscv_gstage_unmap_range() yield
--
commit 08865a8ca16cac26d3da38f3cc5232bb2fb890ba
Author: Can Qi <qican5708@163.com>
KVM: riscv: Allow rescheduling during G-stage unmap
This commit allows G-stage unmap walks to flush pending TLB invalidations
and yield mmu_lock when rescheduling is needed, and moves freeing of the
VM-owned split-page cache to kvm_arch_destroy_vm().
> diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c
> index be70607a33ea0..ea7a156ec4f3f 100644
> --- a/arch/riscv/kvm/gstage.c
> +++ b/arch/riscv/kvm/gstage.c
> @@ -423,9 +423,20 @@ bool kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage,
> bool found_leaf;
> unsigned long page_size;
> gpa_t addr = start, end = start + size;
> + gfn_t last_flush_gfn = start >> PAGE_SHIFT;
> bool flush = false;
> + bool pending_flush = false;
> +
> + if (!(gstage->flags & KVM_GSTAGE_FLAGS_LOCAL))
> + lockdep_assert_held_write(&gstage->kvm->mmu_lock);
>
> while (addr < end) {
> + /* A concurrent teardown may have freed the root while unlocked. */
> + if (!(gstage->flags & KVM_GSTAGE_FLAGS_LOCAL) &&
> + (!gstage->kvm->arch.pgd ||
> + gstage->pgd != gstage->kvm->arch.pgd))
> + break;
> +
> found_leaf = kvm_riscv_gstage_get_leaf(gstage, addr, &ptep, &ptep_level);
> ret = gstage_level_to_page_size(gstage, ptep_level, &page_size);
> if (ret)
> @@ -434,10 +445,11 @@ bool kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage,
> if (!found_leaf) {
> addr = ALIGN(addr + 1, page_size);
> } else {
> - if (!(addr & (page_size - 1)) && ((end - addr) >= page_size))
> - flush |= kvm_riscv_gstage_op_pte(gstage, addr, ptep,
> + if (!(addr & (page_size - 1)) && ((end - addr) >= page_size)) {
> + pending_flush |= kvm_riscv_gstage_op_pte(gstage, addr, ptep,
> ptep_level, GSTAGE_OP_CLEAR);
> - else {
> + flush |= pending_flush;
> + } else {
> WARN_ONCE(1, "Skip unmap range addr: %#llx, end: %#llx, page_size: %#lx\n",
> addr, end, page_size);
> }
> @@ -448,9 +460,18 @@ bool kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage,
> /*
> * If the range is too large, release the kvm->mmu_lock
> * to prevent starvation and lockup detector warnings.
> + * Flush first so another invalidator cannot miss cleared PTEs.
> */
> - if (!(gstage->flags & KVM_GSTAGE_FLAGS_LOCAL) && may_block && addr < end)
> + if (!(gstage->flags & KVM_GSTAGE_FLAGS_LOCAL) && may_block && addr < end &&
> + (need_resched() || rwlock_needbreak(&gstage->kvm->mmu_lock))) {
> + if (pending_flush) {
> + kvm_flush_remote_tlbs_range(gstage->kvm, last_flush_gfn,
> + (addr >> PAGE_SHIFT) - last_flush_gfn);
> + pending_flush = false;
> + }
> + last_flush_gfn = addr >> PAGE_SHIFT;
> cond_resched_rwlock_write(&gstage->kvm->mmu_lock);
> + }
> }
>
> return flush;
[Severity: Medium]
Should kvm_riscv_gstage_unmap_range() return pending_flush instead of
flush?
When the walk yields mmu_lock, it flushes the cleared PTEs in
[last_flush_gfn, addr >> PAGE_SHIFT) and resets pending_flush to false,
but flush stays latched to true. Callers such as
kvm_arch_flush_shadow_memslot() and kvm_unmap_gfn_range() check the
return value to decide whether a post-walk TLB flush is needed, so they
will issue a redundant remote TLB shootdown across the entire range even
when all cleared PTEs were already flushed before yielding.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123055036.1554937-1-qican5708@163.com?part=1
prev parent reply other threads:[~2026-11-23 6:24 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 5:50 [PATCH] KVM: riscv: Allow rescheduling during G-stage unmap Can Qi
2026-11-23 6:24 ` 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-228763@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