From: Guo Ren <guoren@kernel.org>
To: guoyaxing@bosc.ac.cn
Cc: alex@ghiti.fr, anup@brainfault.org, aou@eecs.berkeley.edu,
atish.patra@linux.dev, kvm-riscv@lists.infradead.org,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-riscv@lists.infradead.org, majiuyue@bosc.ac.cn,
minachou@andestech.com, palmer@dabbelt.com, pjw@kernel.org,
"Guo Ren (Alibaba DAMO Academy)" <guoren@kernel.org>
Subject: [PATCH] riscv: KVM: Flush VS-stage stale entries
Date: Mon, 3 Aug 2026 13:54:54 +0000 [thread overview]
Message-ID: <20260803135454.2726112-1-guoren@kernel.org> (raw)
In-Reply-To: <20260802050015.104795-1-guoyaxing@bosc.ac.cn>
From: "Guo Ren (Alibaba DAMO Academy)" <guoren@kernel.org>
When a vCPU migrates to a different host CPU, the new CPU may retain
stale VS-stage TLB entries (GVA → GPA) left by a previous run of the
same guest. Issue an HFENCE.VVMA (all ASIDs, all addresses) inside
kvm_riscv_mmu_update_hgatp() whenever the vCPU is loaded on a CPU
different from the one it last exited on.
Signed-off-by: Guo Ren (Alibaba DAMO Academy) <guoren@kernel.org>
---
Does this patch solve your problem? I would prefer to issue a full
HFENCE.VVMA whenever a new VMID is installed on a pCPU.
---
arch/riscv/include/asm/kvm_mmu.h | 2 +-
arch/riscv/kvm/mmu.c | 5 ++++-
arch/riscv/kvm/vcpu.c | 4 ++--
3 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/arch/riscv/include/asm/kvm_mmu.h b/arch/riscv/include/asm/kvm_mmu.h
index 5439e76f0a96..f0c5d2d01052 100644
--- a/arch/riscv/include/asm/kvm_mmu.h
+++ b/arch/riscv/include/asm/kvm_mmu.h
@@ -16,6 +16,6 @@ int kvm_riscv_mmu_map(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,
struct kvm_gstage_mapping *out_map);
int kvm_riscv_mmu_alloc_pgd(struct kvm *kvm);
void kvm_riscv_mmu_free_pgd(struct kvm *kvm);
-void kvm_riscv_mmu_update_hgatp(struct kvm_vcpu *vcpu);
+void kvm_riscv_mmu_update_hgatp(struct kvm_vcpu *vcpu, int cpu);
#endif
diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c
index 082f9b261733..b76a90f5811a 100644
--- a/arch/riscv/kvm/mmu.c
+++ b/arch/riscv/kvm/mmu.c
@@ -705,7 +705,7 @@ void kvm_riscv_mmu_free_pgd(struct kvm *kvm)
free_pages((unsigned long)pgd, get_order(kvm_riscv_gstage_pgd_size));
}
-void kvm_riscv_mmu_update_hgatp(struct kvm_vcpu *vcpu)
+void kvm_riscv_mmu_update_hgatp(struct kvm_vcpu *vcpu, int cpu)
{
struct kvm_arch *ka = &vcpu->kvm->arch;
unsigned long hgatp = kvm_riscv_gstage_mode(ka->pgd_levels)
@@ -718,4 +718,7 @@ void kvm_riscv_mmu_update_hgatp(struct kvm_vcpu *vcpu)
if (!kvm_riscv_gstage_vmid_bits())
kvm_riscv_local_hfence_gvma_all();
+
+ if (vcpu->arch.last_exit_cpu != cpu)
+ asm volatile(HFENCE_VVMA(zero, zero) : : : "memory");
}
diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c
index cf6e231e76e2..9692f5f5a70a 100644
--- a/arch/riscv/kvm/vcpu.c
+++ b/arch/riscv/kvm/vcpu.c
@@ -593,7 +593,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
csr_write(CSR_VSATP, csr->vsatp);
}
- kvm_riscv_mmu_update_hgatp(vcpu);
+ kvm_riscv_mmu_update_hgatp(vcpu, cpu);
kvm_riscv_vcpu_aia_load(vcpu, cpu);
@@ -686,7 +686,7 @@ static int kvm_riscv_check_vcpu_requests(struct kvm_vcpu *vcpu)
kvm_riscv_reset_vcpu(vcpu, true);
if (kvm_check_request(KVM_REQ_UPDATE_HGATP, vcpu))
- kvm_riscv_mmu_update_hgatp(vcpu);
+ kvm_riscv_mmu_update_hgatp(vcpu, vcpu->cpu);
if (kvm_check_request(KVM_REQ_FENCE_I, vcpu))
kvm_riscv_fence_i_process(vcpu);
--
2.43.0
--
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
WARNING: multiple messages have this Message-ID (diff)
From: Guo Ren <guoren@kernel.org>
To: guoyaxing@bosc.ac.cn
Cc: alex@ghiti.fr, anup@brainfault.org, aou@eecs.berkeley.edu,
atish.patra@linux.dev, kvm-riscv@lists.infradead.org,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-riscv@lists.infradead.org, majiuyue@bosc.ac.cn,
minachou@andestech.com, palmer@dabbelt.com, pjw@kernel.org,
"Guo Ren (Alibaba DAMO Academy)" <guoren@kernel.org>
Subject: [PATCH] riscv: KVM: Flush VS-stage stale entries
Date: Mon, 3 Aug 2026 13:54:54 +0000 [thread overview]
Message-ID: <20260803135454.2726112-1-guoren@kernel.org> (raw)
In-Reply-To: <20260802050015.104795-1-guoyaxing@bosc.ac.cn>
From: "Guo Ren (Alibaba DAMO Academy)" <guoren@kernel.org>
When a vCPU migrates to a different host CPU, the new CPU may retain
stale VS-stage TLB entries (GVA → GPA) left by a previous run of the
same guest. Issue an HFENCE.VVMA (all ASIDs, all addresses) inside
kvm_riscv_mmu_update_hgatp() whenever the vCPU is loaded on a CPU
different from the one it last exited on.
Signed-off-by: Guo Ren (Alibaba DAMO Academy) <guoren@kernel.org>
---
Does this patch solve your problem? I would prefer to issue a full
HFENCE.VVMA whenever a new VMID is installed on a pCPU.
---
arch/riscv/include/asm/kvm_mmu.h | 2 +-
arch/riscv/kvm/mmu.c | 5 ++++-
arch/riscv/kvm/vcpu.c | 4 ++--
3 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/arch/riscv/include/asm/kvm_mmu.h b/arch/riscv/include/asm/kvm_mmu.h
index 5439e76f0a96..f0c5d2d01052 100644
--- a/arch/riscv/include/asm/kvm_mmu.h
+++ b/arch/riscv/include/asm/kvm_mmu.h
@@ -16,6 +16,6 @@ int kvm_riscv_mmu_map(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,
struct kvm_gstage_mapping *out_map);
int kvm_riscv_mmu_alloc_pgd(struct kvm *kvm);
void kvm_riscv_mmu_free_pgd(struct kvm *kvm);
-void kvm_riscv_mmu_update_hgatp(struct kvm_vcpu *vcpu);
+void kvm_riscv_mmu_update_hgatp(struct kvm_vcpu *vcpu, int cpu);
#endif
diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c
index 082f9b261733..b76a90f5811a 100644
--- a/arch/riscv/kvm/mmu.c
+++ b/arch/riscv/kvm/mmu.c
@@ -705,7 +705,7 @@ void kvm_riscv_mmu_free_pgd(struct kvm *kvm)
free_pages((unsigned long)pgd, get_order(kvm_riscv_gstage_pgd_size));
}
-void kvm_riscv_mmu_update_hgatp(struct kvm_vcpu *vcpu)
+void kvm_riscv_mmu_update_hgatp(struct kvm_vcpu *vcpu, int cpu)
{
struct kvm_arch *ka = &vcpu->kvm->arch;
unsigned long hgatp = kvm_riscv_gstage_mode(ka->pgd_levels)
@@ -718,4 +718,7 @@ void kvm_riscv_mmu_update_hgatp(struct kvm_vcpu *vcpu)
if (!kvm_riscv_gstage_vmid_bits())
kvm_riscv_local_hfence_gvma_all();
+
+ if (vcpu->arch.last_exit_cpu != cpu)
+ asm volatile(HFENCE_VVMA(zero, zero) : : : "memory");
}
diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c
index cf6e231e76e2..9692f5f5a70a 100644
--- a/arch/riscv/kvm/vcpu.c
+++ b/arch/riscv/kvm/vcpu.c
@@ -593,7 +593,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
csr_write(CSR_VSATP, csr->vsatp);
}
- kvm_riscv_mmu_update_hgatp(vcpu);
+ kvm_riscv_mmu_update_hgatp(vcpu, cpu);
kvm_riscv_vcpu_aia_load(vcpu, cpu);
@@ -686,7 +686,7 @@ static int kvm_riscv_check_vcpu_requests(struct kvm_vcpu *vcpu)
kvm_riscv_reset_vcpu(vcpu, true);
if (kvm_check_request(KVM_REQ_UPDATE_HGATP, vcpu))
- kvm_riscv_mmu_update_hgatp(vcpu);
+ kvm_riscv_mmu_update_hgatp(vcpu, vcpu->cpu);
if (kvm_check_request(KVM_REQ_FENCE_I, vcpu))
kvm_riscv_fence_i_process(vcpu);
--
2.43.0
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
WARNING: multiple messages have this Message-ID (diff)
From: Guo Ren <guoren@kernel.org>
To: guoyaxing@bosc.ac.cn
Cc: alex@ghiti.fr, anup@brainfault.org, aou@eecs.berkeley.edu,
atish.patra@linux.dev, kvm-riscv@lists.infradead.org,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-riscv@lists.infradead.org, majiuyue@bosc.ac.cn,
minachou@andestech.com, palmer@dabbelt.com, pjw@kernel.org,
"Guo Ren (Alibaba DAMO Academy)" <guoren@kernel.org>
Subject: [PATCH] riscv: KVM: Flush VS-stage stale entries
Date: Mon, 3 Aug 2026 13:54:54 +0000 [thread overview]
Message-ID: <20260803135454.2726112-1-guoren@kernel.org> (raw)
In-Reply-To: <20260802050015.104795-1-guoyaxing@bosc.ac.cn>
From: "Guo Ren (Alibaba DAMO Academy)" <guoren@kernel.org>
When a vCPU migrates to a different host CPU, the new CPU may retain
stale VS-stage TLB entries (GVA → GPA) left by a previous run of the
same guest. Issue an HFENCE.VVMA (all ASIDs, all addresses) inside
kvm_riscv_mmu_update_hgatp() whenever the vCPU is loaded on a CPU
different from the one it last exited on.
Signed-off-by: Guo Ren (Alibaba DAMO Academy) <guoren@kernel.org>
---
Does this patch solve your problem? I would prefer to issue a full
HFENCE.VVMA whenever a new VMID is installed on a pCPU.
---
arch/riscv/include/asm/kvm_mmu.h | 2 +-
arch/riscv/kvm/mmu.c | 5 ++++-
arch/riscv/kvm/vcpu.c | 4 ++--
3 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/arch/riscv/include/asm/kvm_mmu.h b/arch/riscv/include/asm/kvm_mmu.h
index 5439e76f0a96..f0c5d2d01052 100644
--- a/arch/riscv/include/asm/kvm_mmu.h
+++ b/arch/riscv/include/asm/kvm_mmu.h
@@ -16,6 +16,6 @@ int kvm_riscv_mmu_map(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,
struct kvm_gstage_mapping *out_map);
int kvm_riscv_mmu_alloc_pgd(struct kvm *kvm);
void kvm_riscv_mmu_free_pgd(struct kvm *kvm);
-void kvm_riscv_mmu_update_hgatp(struct kvm_vcpu *vcpu);
+void kvm_riscv_mmu_update_hgatp(struct kvm_vcpu *vcpu, int cpu);
#endif
diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c
index 082f9b261733..b76a90f5811a 100644
--- a/arch/riscv/kvm/mmu.c
+++ b/arch/riscv/kvm/mmu.c
@@ -705,7 +705,7 @@ void kvm_riscv_mmu_free_pgd(struct kvm *kvm)
free_pages((unsigned long)pgd, get_order(kvm_riscv_gstage_pgd_size));
}
-void kvm_riscv_mmu_update_hgatp(struct kvm_vcpu *vcpu)
+void kvm_riscv_mmu_update_hgatp(struct kvm_vcpu *vcpu, int cpu)
{
struct kvm_arch *ka = &vcpu->kvm->arch;
unsigned long hgatp = kvm_riscv_gstage_mode(ka->pgd_levels)
@@ -718,4 +718,7 @@ void kvm_riscv_mmu_update_hgatp(struct kvm_vcpu *vcpu)
if (!kvm_riscv_gstage_vmid_bits())
kvm_riscv_local_hfence_gvma_all();
+
+ if (vcpu->arch.last_exit_cpu != cpu)
+ asm volatile(HFENCE_VVMA(zero, zero) : : : "memory");
}
diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c
index cf6e231e76e2..9692f5f5a70a 100644
--- a/arch/riscv/kvm/vcpu.c
+++ b/arch/riscv/kvm/vcpu.c
@@ -593,7 +593,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
csr_write(CSR_VSATP, csr->vsatp);
}
- kvm_riscv_mmu_update_hgatp(vcpu);
+ kvm_riscv_mmu_update_hgatp(vcpu, cpu);
kvm_riscv_vcpu_aia_load(vcpu, cpu);
@@ -686,7 +686,7 @@ static int kvm_riscv_check_vcpu_requests(struct kvm_vcpu *vcpu)
kvm_riscv_reset_vcpu(vcpu, true);
if (kvm_check_request(KVM_REQ_UPDATE_HGATP, vcpu))
- kvm_riscv_mmu_update_hgatp(vcpu);
+ kvm_riscv_mmu_update_hgatp(vcpu, vcpu->cpu);
if (kvm_check_request(KVM_REQ_FENCE_I, vcpu))
kvm_riscv_fence_i_process(vcpu);
--
2.43.0
next prev parent reply other threads:[~2026-08-03 13:55 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-02 5:00 [RFC] RISC-V KVM: guest local sfence.vma may miss stale VS-stage TLB after vCPU migration Yaxing Guo
2026-08-02 5:00 ` Yaxing Guo
2026-08-02 5:00 ` Yaxing Guo
2026-08-03 13:54 ` Guo Ren [this message]
2026-08-03 13:54 ` [PATCH] riscv: KVM: Flush VS-stage stale entries Guo Ren
2026-08-03 13:54 ` Guo Ren
2026-08-03 14:14 ` sashiko-bot
2026-08-03 14:22 ` [RFC] RISC-V KVM: guest local sfence.vma may miss stale VS-stage TLB after vCPU migration Anup Patel
2026-08-03 14:22 ` Anup Patel
2026-08-03 14:22 ` Anup Patel
2026-08-03 17:19 ` Guo Ren
2026-08-03 17:19 ` Guo Ren
2026-08-03 17:19 ` Guo Ren
2026-08-03 17:51 ` Guo Ren
2026-08-03 17:51 ` Guo Ren
2026-08-03 17:51 ` Guo Ren
2026-08-04 2:32 ` guoyaxing
2026-08-04 2:32 ` guoyaxing
2026-08-04 2:32 ` guoyaxing
2026-08-04 2:56 ` guoyaxing
2026-08-04 2:56 ` guoyaxing
2026-08-04 2:56 ` guoyaxing
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=20260803135454.2726112-1-guoren@kernel.org \
--to=guoren@kernel.org \
--cc=alex@ghiti.fr \
--cc=anup@brainfault.org \
--cc=aou@eecs.berkeley.edu \
--cc=atish.patra@linux.dev \
--cc=guoyaxing@bosc.ac.cn \
--cc=kvm-riscv@lists.infradead.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=majiuyue@bosc.ac.cn \
--cc=minachou@andestech.com \
--cc=palmer@dabbelt.com \
--cc=pjw@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 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.