From: sashiko-bot@kernel.org
To: "Tan Chi" <tanchi25@mails.ucas.ac.cn>
Cc: kvm@vger.kernel.org
Subject: Re: [RFC PATCH 1/3] KVM: riscv: Introduce a recursive G-stage unmap walker
Date: Sun, 29 Nov 2026 14:37:52 +0000 [thread overview]
Message-ID: <sashiko-outbox-239713@kernel.org> (raw)
In-Reply-To: <20261129142252.171879-2-tanchi25@mails.ucas.ac.cn>
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] KVM: riscv: use-after-free of gstage->pgd in
gstage_unmap_range_recursive()
--
commit 5d921baa27d30ba8a457e5d017135853833a0eb5
Author: Tan Chi <tanchi25@mails.ucas.ac.cn>
KVM: riscv: Introduce a recursive G-stage unmap walker
This commit introduces a recursive G-stage page table walker to unmap
address ranges by walking adjacent PTEs within each page table level,
while keeping a fast path for single-page unmaps.
> diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c
> index f4ed659b11e94..dca3cd1c35e41 100644
> --- a/arch/riscv/kvm/gstage.c
> +++ b/arch/riscv/kvm/gstage.c
[ ... ]
> +static noinline bool gstage_unmap_range_recursive(struct kvm_gstage *gstage,
> + gpa_t start, gpa_t size, bool may_block)
> +{
> + int ret;
> + gpa_t addr = start, end = start + size;
> + bool flush = false;
> +
> + if (gstage->flags & KVM_GSTAGE_FLAGS_LOCAL)
> + may_block = false;
> +
> + while (addr < end) {
> + ret = gstage_unmap_walk(gstage, (pte_t *)gstage->pgd,
> + gstage->pgd_levels - 1,
> + &addr, end, may_block, &flush);
> +
> + if (ret != -EAGAIN || addr >= end)
> + break;
> +
> /*
> * If the range is too large, release the kvm->mmu_lock
> * to prevent starvation and lockup detector warnings.
> */
> - if (!(gstage->flags & KVM_GSTAGE_FLAGS_LOCAL) && may_block && addr < end)
> - cond_resched_rwlock_write(&gstage->kvm->mmu_lock);
> + cond_resched_rwlock_write(&gstage->kvm->mmu_lock);
> }
>
> return flush;
> }
[Severity: High]
Can gstage_unmap_range_recursive() access a freed root PGD after
cond_resched_rwlock_write() drops and reacquires kvm->mmu_lock?
kvm_unmap_gfn_range() snapshots kvm->arch.pgd into gstage.pgd once via
kvm_riscv_gstage_init() before calling kvm_riscv_gstage_unmap_range().
When cond_resched_rwlock_write(&gstage->kvm->mmu_lock) releases
mmu_lock and schedules out, a concurrent kvm_mmu_notifier_release() can
tear down and free the root PGD:
CPU0 CPU1
kvm_unmap_gfn_range()
kvm_riscv_gstage_init(&gstage, kvm)
gstage->pgd = kvm->arch.pgd
...
gstage_unmap_range_recursive()
gstage_unmap_walk() -> -EAGAIN
cond_resched_rwlock_write()
write_unlock(&kvm->mmu_lock)
kvm_mmu_notifier_release()
...
kvm_riscv_mmu_free_pgd()
write_lock(&kvm->mmu_lock)
kvm_riscv_gstage_unmap_all()
WRITE_ONCE(kvm->arch.pgd, NULL)
write_unlock(&kvm->mmu_lock)
call_rcu(...,
kvm_riscv_mmu_free_pgd_rcu)
schedule()
/* RCU grace period completes, PGD pages freed */
write_lock(&kvm->mmu_lock)
gstage_unmap_walk(..., gstage->pgd, ...)
ptep_get(ptep) <-- use-after-free
Before this change, kvm_riscv_gstage_get_leaf() read gstage->pgd too,
Wait: actually both old and new read gstage->pgd, or rather after
reacquiring mmu_lock, checking !gstage->kvm->arch.pgd (like
mmu_split_huge_pages() does after cond_resched_rwlock_write()) would
avoid walking the stale gstage->pgd pointer.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261129142252.171879-1-tanchi25@mails.ucas.ac.cn?part=1
next prev parent reply other threads:[~2026-11-29 14:37 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-29 14:22 [RFC PATCH 0/3] KVM: riscv: Reclaim unused G-stage page tables Tan Chi
2026-11-29 14:22 ` Tan Chi
2026-11-29 14:22 ` Tan Chi
2026-11-29 14:22 ` [RFC PATCH 1/3] KVM: riscv: Introduce a recursive G-stage unmap walker Tan Chi
2026-11-29 14:22 ` Tan Chi
2026-11-29 14:22 ` Tan Chi
2026-11-29 14:37 ` sashiko-bot [this message]
2026-11-29 14:22 ` [RFC PATCH 2/3] KVM: riscv: Reclaim G-stage page tables on memslot removal Tan Chi
2026-11-29 14:22 ` Tan Chi
2026-11-29 14:22 ` Tan Chi
2026-11-29 14:22 ` [RFC PATCH 3/3] KVM: riscv: Selectively reclaim G-stage page tables on invalidation Tan Chi
2026-11-29 14:22 ` Tan Chi
2026-11-29 14:22 ` Tan Chi
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-239713@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=kvm@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=tanchi25@mails.ucas.ac.cn \
/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.