From: Anup Patel <anup.patel@oss.qualcomm.com>
To: Palmer Dabbelt <palmer@dabbelt.com>,
Paul Walmsley <pjw@kernel.org>,
Atish Patra <atish.patra@linux.dev>,
Andrew Jones <andrew.jones@linux.dev>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Anup Patel <anup@brainfault.org>,
kvm@vger.kernel.org, kvm-riscv@lists.infradead.org,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
Kyle Zeng <kylebot@openai.com>,
stable@vger.kernel.org, Sean Christopherson <seanjc@google.com>,
Anup Patel <anup.patel@oss.qualcomm.com>
Subject: [PATCH 1/2] RISC-V: KVM: Flush TLBs before dropping mmu_lock when yielding
Date: Sun, 6 Dec 2026 18:56:16 +0530 [thread overview]
Message-ID: <20261206132617.1644596-1-anup.patel@oss.qualcomm.com> (raw)
From: Kyle Zeng <kylebot@openai.com>
When unmapping a GPA range, flush TLBs for the processed GPA range *before*
dropping mmu_lock. The ENTIRE point of holding mmu_lock is to ensure other
MMU operations will either see the present stage-2 PTEs, or can rely on any
references to the old PTEs being fully zapped.
E.g. if a MMU notifier invalidation event runs while the zapping task
has dropped mmu_lock and yielded, then the MMU notifier task observe
the missing PTE and return without issuing its own TLB flush, resulting
in a use-after-free (if the to-be-invalidated page was freed or moved).
Fixes: 60aa6734f542 ("RISC-V: KVM: Batch stage-2 TLB flushes")
Cc: stable@vger.kernel.org
Signed-off-by: Kyle Zeng <kylebot@openai.com>
[sean: rewrite changelog and drop the comment]
Signed-off-by: Sean Christopherson <seanjc@google.com>
[anup: updated kvm_riscv_mmu_iounmap()]
Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
---
arch/riscv/kvm/gstage.c | 9 ++++++++-
arch/riscv/kvm/mmu.c | 5 ++---
2 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c
index 241f4c19ca18..8e011fe74977 100644
--- a/arch/riscv/kvm/gstage.c
+++ b/arch/riscv/kvm/gstage.c
@@ -480,8 +480,15 @@ 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.
*/
- 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 (flush) {
+ kvm_flush_remote_tlbs_range(gstage->kvm, start >> PAGE_SHIFT,
+ (addr - start) >> PAGE_SHIFT);
+ flush = false;
+ }
cond_resched_rwlock_write(&gstage->kvm->mmu_lock);
+ }
}
return flush;
diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c
index e92d564f9c77..aa42876ce487 100644
--- a/arch/riscv/kvm/mmu.c
+++ b/arch/riscv/kvm/mmu.c
@@ -94,11 +94,10 @@ void kvm_riscv_mmu_iounmap(struct kvm *kvm, gpa_t gpa, unsigned long size)
write_lock(&kvm->mmu_lock);
flush = kvm_riscv_gstage_unmap_range(&gstage, gpa, size, false);
- write_unlock(&kvm->mmu_lock);
-
if (flush)
kvm_flush_remote_tlbs_range(kvm, gpa >> PAGE_SHIFT,
size >> PAGE_SHIFT);
+ write_unlock(&kvm->mmu_lock);
}
static bool need_topup_split_caches_or_resched(struct kvm *kvm, int count)
@@ -211,10 +210,10 @@ void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
write_lock(&kvm->mmu_lock);
flush = kvm_riscv_gstage_unmap_range(&gstage, gpa, size, false);
- write_unlock(&kvm->mmu_lock);
if (flush)
kvm_flush_remote_tlbs_range(kvm, gpa >> PAGE_SHIFT,
size >> PAGE_SHIFT);
+ write_unlock(&kvm->mmu_lock);
}
static void mmu_split_memory_region(struct kvm *kvm, int slot)
--
2.43.0
next reply other threads:[~2026-12-06 13:27 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-06 13:26 Anup Patel [this message]
2026-12-06 13:26 ` [PATCH 2/2] RISC-V: KVM: Use cond_resched_rwlock_write() in kvm_riscv_gstage_unmap_all() Anup Patel
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=20261206132617.1644596-1-anup.patel@oss.qualcomm.com \
--to=anup.patel@oss.qualcomm.com \
--cc=andrew.jones@linux.dev \
--cc=anup@brainfault.org \
--cc=atish.patra@linux.dev \
--cc=kvm-riscv@lists.infradead.org \
--cc=kvm@vger.kernel.org \
--cc=kylebot@openai.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=palmer@dabbelt.com \
--cc=pbonzini@redhat.com \
--cc=pjw@kernel.org \
--cc=seanjc@google.com \
--cc=stable@vger.kernel.org \
/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
all inboxes | Powered by JetHome®