From: Jinyu Tang <tjytimi@163.com>
To: Anup Patel <apatel@ventanamicro.com>,
Anup Patel <anup@brainfault.org>,
Paolo Bonzini <pbonzini@redhat.com>,
Sean Christopherson <seanjc@google.com>
Cc: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
"Atish Patra" <atish.patra@linux.dev>,
"Paul Walmsley" <pjw@kernel.org>,
"Paul Walmsley" <paul.walmsley@sifive.com>,
"Palmer Dabbelt" <palmer@dabbelt.com>,
"Albert Ou" <aou@eecs.berkeley.edu>,
"Alexandre Ghiti" <alex@ghiti.fr>,
"Radim Krčmář" <radim.krcmar@oss.qualcomm.com>,
"Andrew Jones" <andrew.jones@oss.qualcomm.com>,
"Conor Dooley" <conor.dooley@microchip.com>,
"Yong-Xuan Wang" <yongxuan.wang@sifive.com>,
"Nutty Liu" <nutty.liu@hotmail.com>,
"Jinyu Tang" <tjytimi@163.com>
Subject: [PATCH 3/5] KVM: riscv: Add a G-stage PTE cmpxchg helper
Date: Sun, 17 May 2026 23:34:25 +0800 [thread overview]
Message-ID: <20260517153427.94889-4-tjytimi@163.com> (raw)
In-Reply-To: <20260517153427.94889-1-tjytimi@163.com>
Permission-only G-stage PTE updates can run in parallel once they are
moved to the read side of mmu_lock. Plain set_pte() is not enough for
that case because another CPU may update the same PTE first.
x86 handles the same class of SPTE races with cmpxchg-based updates in
its fast page fault and TDP MMU paths. Add a small RISC-V helper for
atomic G-stage PTE updates. The helper reports contention to the caller
and flushes the target range only when the PTE value actually changes.
Signed-off-by: Jinyu Tang <tjytimi@163.com>
---
arch/riscv/include/asm/kvm_gstage.h | 4 ++++
arch/riscv/kvm/gstage.c | 14 ++++++++++++++
2 files changed, 18 insertions(+)
diff --git a/arch/riscv/include/asm/kvm_gstage.h b/arch/riscv/include/asm/kvm_gstage.h
index 9c908432b..afe80e4bf 100644
--- a/arch/riscv/include/asm/kvm_gstage.h
+++ b/arch/riscv/include/asm/kvm_gstage.h
@@ -54,6 +54,10 @@ int kvm_riscv_gstage_set_pte(struct kvm_gstage *gstage,
struct kvm_mmu_memory_cache *pcache,
const struct kvm_gstage_mapping *map);
+bool kvm_riscv_gstage_try_update_pte(struct kvm_gstage *gstage, u32 level,
+ gpa_t addr, pte_t *ptep,
+ pte_t old_pte, pte_t new_pte);
+
int kvm_riscv_gstage_map_page(struct kvm_gstage *gstage,
struct kvm_mmu_memory_cache *pcache,
gpa_t gpa, phys_addr_t hpa, unsigned long page_size,
diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c
index 6f934cb4a..d70584b9e 100644
--- a/arch/riscv/kvm/gstage.c
+++ b/arch/riscv/kvm/gstage.c
@@ -123,6 +123,20 @@ static void gstage_tlb_flush(struct kvm_gstage *gstage, u32 level, gpa_t addr)
gstage->vmid);
}
+bool kvm_riscv_gstage_try_update_pte(struct kvm_gstage *gstage, u32 level,
+ gpa_t addr, pte_t *ptep,
+ pte_t old_pte, pte_t new_pte)
+{
+ if (cmpxchg(&ptep->pte, pte_val(old_pte), pte_val(new_pte)) !=
+ pte_val(old_pte))
+ return false;
+
+ if (pte_val(old_pte) != pte_val(new_pte))
+ gstage_tlb_flush(gstage, level, addr);
+
+ return true;
+}
+
int kvm_riscv_gstage_set_pte(struct kvm_gstage *gstage,
struct kvm_mmu_memory_cache *pcache,
const struct kvm_gstage_mapping *map)
--
2.43.0
next prev parent reply other threads:[~2026-05-17 15:37 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-17 15:34 [PATCH 0/5] KVM: riscv: Speed up dirty logging write faults Jinyu Tang
2026-05-17 15:34 ` [PATCH 1/5] KVM: riscv: Rely on common MMU notifier locking Jinyu Tang
2026-05-17 15:34 ` [PATCH 2/5] KVM: riscv: Use an rwlock for mmu_lock Jinyu Tang
2026-05-17 15:34 ` Jinyu Tang [this message]
2026-05-17 15:34 ` [PATCH 4/5] KVM: riscv: Update G-stage PTE permissions atomically Jinyu Tang
2026-05-17 15:34 ` [PATCH 5/5] KVM: riscv: Fast-path dirty logging write faults Jinyu Tang
2026-05-28 22:35 ` Inochi Amaoto
2026-06-03 13:12 ` [PATCH 0/5] KVM: riscv: Speed up " Anup Patel
2026-06-26 8:21 ` patchwork-bot+linux-riscv
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=20260517153427.94889-4-tjytimi@163.com \
--to=tjytimi@163.com \
--cc=alex@ghiti.fr \
--cc=andrew.jones@oss.qualcomm.com \
--cc=anup@brainfault.org \
--cc=aou@eecs.berkeley.edu \
--cc=apatel@ventanamicro.com \
--cc=atish.patra@linux.dev \
--cc=conor.dooley@microchip.com \
--cc=kvm-riscv@lists.infradead.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=nutty.liu@hotmail.com \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.com \
--cc=pbonzini@redhat.com \
--cc=pjw@kernel.org \
--cc=radim.krcmar@oss.qualcomm.com \
--cc=seanjc@google.com \
--cc=yongxuan.wang@sifive.com \
/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®