mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Anup Patel <anup.patel@oss.qualcomm.com>
To: Paolo Bonzini <pbonzini@redhat.com>, Atish Patra <atish.patra@linux.dev>
Cc: Palmer Dabbelt <palmer@dabbelt.com>,
	Paul Walmsley <pjw@kernel.org>, Alexandre Ghiti <alex@ghiti.fr>,
	Shuah Khan <shuah@kernel.org>, Anup Patel <anup@brainfault.org>,
	Andrew Jones <andrew.jones@oss.qualcomm.com>,
	kvm-riscv@lists.infradead.org, kvm@vger.kernel.org,
	linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org,
	Anup Patel <anup.patel@oss.qualcomm.com>
Subject: [PATCH 11/27] RISC-V: KVM: Use half VMID space for nested guest
Date: Tue, 20 Jan 2026 13:29:57 +0530	[thread overview]
Message-ID: <20260120080013.2153519-12-anup.patel@oss.qualcomm.com> (raw)
In-Reply-To: <20260120080013.2153519-1-anup.patel@oss.qualcomm.com>

A single guest with nested virtualization needs two VMIDs: one for
the guest hypervisor (L1) and another for the nested guest (L2).

To support this, divide the VMID space into two equal parts when
nested virtualization is enabled.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
---
 arch/riscv/include/asm/kvm_vmid.h |  1 +
 arch/riscv/kvm/main.c             |  4 ++--
 arch/riscv/kvm/tlb.c              | 11 +++++++++--
 arch/riscv/kvm/vmid.c             | 33 ++++++++++++++++++++++++++++---
 4 files changed, 42 insertions(+), 7 deletions(-)

diff --git a/arch/riscv/include/asm/kvm_vmid.h b/arch/riscv/include/asm/kvm_vmid.h
index db61b0525a8d..3048e12a639c 100644
--- a/arch/riscv/include/asm/kvm_vmid.h
+++ b/arch/riscv/include/asm/kvm_vmid.h
@@ -19,6 +19,7 @@ struct kvm_vmid {
 
 void __init kvm_riscv_gstage_vmid_detect(void);
 unsigned long kvm_riscv_gstage_vmid_bits(void);
+unsigned long kvm_riscv_gstage_nested_vmid(unsigned long vmid);
 int kvm_riscv_gstage_vmid_init(struct kvm *kvm);
 bool kvm_riscv_gstage_vmid_ver_changed(struct kvm_vmid *vmid);
 void kvm_riscv_gstage_vmid_update(struct kvm_vcpu *vcpu);
diff --git a/arch/riscv/kvm/main.c b/arch/riscv/kvm/main.c
index 5b4bf972d242..28044eefda47 100644
--- a/arch/riscv/kvm/main.c
+++ b/arch/riscv/kvm/main.c
@@ -123,8 +123,6 @@ static int __init riscv_kvm_init(void)
 		return -ENODEV;
 	}
 
-	kvm_riscv_gstage_vmid_detect();
-
 	rc = kvm_riscv_aia_init();
 	if (rc && rc != -ENODEV) {
 		kvm_riscv_nacl_exit();
@@ -133,6 +131,8 @@ static int __init riscv_kvm_init(void)
 
 	kvm_riscv_nested_init();
 
+	kvm_riscv_gstage_vmid_detect();
+
 	kvm_info("hypervisor extension available\n");
 
 	if (kvm_riscv_nacl_available()) {
diff --git a/arch/riscv/kvm/tlb.c b/arch/riscv/kvm/tlb.c
index ff1aeac4eb8e..a95aa5336560 100644
--- a/arch/riscv/kvm/tlb.c
+++ b/arch/riscv/kvm/tlb.c
@@ -160,7 +160,7 @@ void kvm_riscv_local_hfence_vvma_all(unsigned long vmid)
 
 void kvm_riscv_local_tlb_sanitize(struct kvm_vcpu *vcpu)
 {
-	unsigned long vmid;
+	unsigned long vmid, nvmid;
 
 	if (!kvm_riscv_gstage_vmid_bits() ||
 	    vcpu->arch.last_exit_cpu == vcpu->cpu)
@@ -180,12 +180,19 @@ void kvm_riscv_local_tlb_sanitize(struct kvm_vcpu *vcpu)
 	vmid = READ_ONCE(vcpu->kvm->arch.vmid.vmid);
 	kvm_riscv_local_hfence_gvma_vmid_all(vmid);
 
+	nvmid = kvm_riscv_gstage_nested_vmid(vmid);
+	if (vmid != nvmid)
+		kvm_riscv_local_hfence_gvma_vmid_all(nvmid);
+
 	/*
 	 * Flush VS-stage TLB entries for implementation where VS-stage
 	 * TLB does not cahce guest physical address and VMID.
 	 */
-	if (static_branch_unlikely(&kvm_riscv_vsstage_tlb_no_gpa))
+	if (static_branch_unlikely(&kvm_riscv_vsstage_tlb_no_gpa)) {
 		kvm_riscv_local_hfence_vvma_all(vmid);
+		if (vmid != nvmid)
+			kvm_riscv_local_hfence_vvma_all(nvmid);
+	}
 }
 
 void kvm_riscv_fence_i_process(struct kvm_vcpu *vcpu)
diff --git a/arch/riscv/kvm/vmid.c b/arch/riscv/kvm/vmid.c
index cf34d448289d..2ddd95fe2d9c 100644
--- a/arch/riscv/kvm/vmid.c
+++ b/arch/riscv/kvm/vmid.c
@@ -25,6 +25,8 @@ static DEFINE_SPINLOCK(vmid_lock);
 
 void __init kvm_riscv_gstage_vmid_detect(void)
 {
+	unsigned long min_vmids;
+
 	/* Figure-out number of VMID bits in HW */
 	csr_write(CSR_HGATP, (kvm_riscv_gstage_mode << HGATP_MODE_SHIFT) | HGATP_VMID);
 	vmid_bits = csr_read(CSR_HGATP);
@@ -35,8 +37,23 @@ void __init kvm_riscv_gstage_vmid_detect(void)
 	/* We polluted local TLB so flush all guest TLB */
 	kvm_riscv_local_hfence_gvma_all();
 
-	/* We don't use VMID bits if they are not sufficient */
-	if ((1UL << vmid_bits) < num_possible_cpus())
+	/*
+	 * A single guest with nested virtualization needs two
+	 * VMIDs: one for the guest hypervisor (L1) and another
+	 * for the nested guest (L2).
+	 *
+	 * Potentially, we can have a separate guest running on
+	 * each host CPU so the number of VMIDs should not be:
+	 *
+	 * 1. less than the number of host CPUs for
+	 *    nested virtualization disabled
+	 * 2. less than twice the number of host CPUs for
+	 *    nested virtualization enabled
+	 */
+	min_vmids = num_possible_cpus();
+	if (kvm_riscv_nested_available())
+		min_vmids = min_vmids * 2;
+	if (BIT(vmid_bits) < min_vmids)
 		vmid_bits = 0;
 }
 
@@ -45,6 +62,13 @@ unsigned long kvm_riscv_gstage_vmid_bits(void)
 	return vmid_bits;
 }
 
+unsigned long kvm_riscv_gstage_nested_vmid(unsigned long vmid)
+{
+	if (kvm_riscv_nested_available())
+		return vmid | BIT(vmid_bits - 1);
+	return vmid;
+}
+
 int kvm_riscv_gstage_vmid_init(struct kvm *kvm)
 {
 	/* Mark the initial VMID and VMID version invalid */
@@ -112,7 +136,10 @@ void kvm_riscv_gstage_vmid_update(struct kvm_vcpu *vcpu)
 
 	vmid->vmid = vmid_next;
 	vmid_next++;
-	vmid_next &= (1 << vmid_bits) - 1;
+	if (kvm_riscv_nested_available())
+		vmid_next &= BIT(vmid_bits - 1) - 1;
+	else
+		vmid_next &= BIT(vmid_bits) - 1;
 
 	WRITE_ONCE(vmid->vmid_version, READ_ONCE(vmid_version));
 
-- 
2.43.0


  parent reply	other threads:[~2026-01-20  8:01 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-20  7:59 [PATCH 00/27] Nested virtualization for KVM RISC-V Anup Patel
2026-01-20  7:59 ` [PATCH 01/27] RISC-V: KVM: Fix error code returned for Smstateen ONE_REG Anup Patel
2026-03-06  7:04   ` Anup Patel
2026-01-20  7:59 ` [PATCH 02/27] RISC-V: KVM: Fix error code returned for Ssaia ONE_REG Anup Patel
2026-03-06  7:04   ` Anup Patel
2026-01-20  7:59 ` [PATCH 03/27] RISC-V: KVM: Check host Ssaia extension when creating AIA irqchip Anup Patel
2026-03-06  7:04   ` Anup Patel
2026-01-20  7:59 ` [PATCH 04/27] RISC-V: KVM: Introduce common kvm_riscv_isa_check_host() Anup Patel
2026-03-13 14:22   ` Radim Krčmář
2026-01-20  7:59 ` [PATCH 05/27] RISC-V: KVM: Factor-out ISA checks into separate sources Anup Patel
2026-03-13 14:14   ` Radim Krčmář
2026-04-03 12:34     ` Anup Patel
2026-01-20  7:59 ` [PATCH 06/27] RISC-V: KVM: Move timer state defines closer to struct in UAPI header Anup Patel
2026-01-20  7:59 ` [PATCH 07/27] RISC-V: KVM: Add hideleg to struct kvm_vcpu_config Anup Patel
2026-03-13 13:49   ` Radim Krčmář
2026-01-20  7:59 ` [PATCH 08/27] RISC-V: KVM: Factor-out VCPU config into separate sources Anup Patel
2026-03-13 13:46   ` Radim Krčmář
2026-01-20  7:59 ` [PATCH 09/27] RISC-V: KVM: Don't check hstateen0 when updating sstateen0 CSR Anup Patel
2026-03-13 13:27   ` Radim Krčmář
2026-01-20  7:59 ` [PATCH 10/27] RISC-V: KVM: Initial skeletal nested virtualization support Anup Patel
2026-01-20  7:59 ` Anup Patel [this message]
2026-01-20  7:59 ` [PATCH 12/27] RISC-V: KVM: Extend kvm_riscv_mmu_update_hgatp() for nested virtualization Anup Patel
2026-01-20  7:59 ` [PATCH 13/27] RISC-V: KVM: Extend kvm_riscv_vcpu_config_load() " Anup Patel
2026-01-20  8:00 ` [PATCH 14/27] RISC-V: KVM: Extend kvm_riscv_vcpu_update_timedelta() for nested virt Anup Patel
2026-01-20  8:00 ` [PATCH 15/27] RISC-V: KVM: Extend trap redirection for nested virtualization Anup Patel
2026-01-20  8:00 ` [PATCH 16/27] RISC-V: KVM: Check and inject nested virtual interrupts Anup Patel
2026-01-20  8:00 ` [PATCH 17/27] RISC-V: KVM: Extend kvm_riscv_isa_check_host() for nested virt Anup Patel
2026-01-20  8:00 ` [PATCH 18/27] RISC-V: KVM: Trap-n-emulate SRET for Guest HS-mode Anup Patel
2026-01-20  8:00 ` [PATCH 19/27] RISC-V: KVM: Redirect nested supervisor ecall and breakpoint traps Anup Patel
2026-01-20  8:00 ` [PATCH 20/27] RISC-V: KVM: Redirect nested WFI and WRS traps Anup Patel
2026-01-20  8:00 ` [PATCH 21/27] RISC-V: KVM: Implement remote HFENCE SBI calls for guest Anup Patel
2026-01-20  8:00 ` [PATCH 22/27] RISC-V: KVM: Add CSR emulation for nested virtualization Anup Patel
2026-01-20  8:00 ` [PATCH 23/27] RISC-V: KVM: Add HFENCE " Anup Patel
2026-01-20  8:00 ` [PATCH 24/27] RISC-V: KVM: Add ONE_REG interface for nested virtualization state Anup Patel
2026-01-20  8:00 ` [PATCH 25/27] RISC-V: KVM: selftests: Add nested virt state to get-reg-list test Anup Patel
2026-01-20  8:00 ` [PATCH 26/27] RISC-V: KVM: Add ONE_REG interface for nested virtualization CSRs Anup Patel
2026-01-20  8:00 ` [PATCH 27/27] RISC-V: KVM: selftests: Add nested virt CSRs to get-reg-list test Anup Patel
2026-04-03 12:36 ` [PATCH 00/27] Nested virtualization for KVM RISC-V 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=20260120080013.2153519-12-anup.patel@oss.qualcomm.com \
    --to=anup.patel@oss.qualcomm.com \
    --cc=alex@ghiti.fr \
    --cc=andrew.jones@oss.qualcomm.com \
    --cc=anup@brainfault.org \
    --cc=atish.patra@linux.dev \
    --cc=kvm-riscv@lists.infradead.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=pbonzini@redhat.com \
    --cc=pjw@kernel.org \
    --cc=shuah@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®