From: fangyu.yu@linux.alibaba.com
To: pbonzini@redhat.com, corbet@lwn.net, anup@brainfault.org,
atish.patra@linux.dev, pjw@kernel.org, palmer@dabbelt.com,
aou@eecs.berkeley.edu, alex@ghiti.fr, skhan@linuxfoundation.org
Cc: guoren@kernel.org, radim.krcmar@oss.qualcomm.com,
andrew.jones@oss.qualcomm.com, linux-doc@vger.kernel.org,
kvm@vger.kernel.org, kvm-riscv@lists.infradead.org,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
Fangyu Yu <fangyu.yu@linux.alibaba.com>
Subject: [PATCH v8 3/3] RISC-V: KVM: Reuse KVM_CAP_VM_GPA_BITS to select HGATP.MODE
Date: Fri, 3 Apr 2026 23:30:18 +0800 [thread overview]
Message-ID: <20260403153019.9916-4-fangyu.yu@linux.alibaba.com> (raw)
In-Reply-To: <20260403153019.9916-1-fangyu.yu@linux.alibaba.com>
From: Fangyu Yu <fangyu.yu@linux.alibaba.com>
Reuse KVM_CAP_VM_GPA_BITS to advertise and select the effective
G-stage GPA width for a VM.
KVM_CHECK_EXTENSION(KVM_CAP_VM_GPA_BITS) returns the effective GPA
bits for a VM, KVM_ENABLE_CAP(KVM_CAP_VM_GPA_BITS) allows userspace
to downsize the effective GPA width by selecting a smaller G-stage
page table format:
- gpa_bits <= 41 selects Sv39x4 (pgd_levels=3)
- gpa_bits <= 50 selects Sv48x4 (pgd_levels=4)
- gpa_bits <= 59 selects Sv57x4 (pgd_levels=5)
Reject the request with -EINVAL for unsupported values and with -EBUSY
if vCPUs have been created or any memslot is populated.
Signed-off-by: Fangyu Yu <fangyu.yu@linux.alibaba.com>
Reviewed-by: Andrew Jones <andrew.jones@oss.qualcomm.com>
Reviewed-by: Guo Ren <guoren@kernel.org>
---
arch/riscv/kvm/vm.c | 44 ++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 42 insertions(+), 2 deletions(-)
diff --git a/arch/riscv/kvm/vm.c b/arch/riscv/kvm/vm.c
index fb7c4e07961f..a9f083feeb76 100644
--- a/arch/riscv/kvm/vm.c
+++ b/arch/riscv/kvm/vm.c
@@ -214,12 +214,52 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
{
+ if (cap->flags)
+ return -EINVAL;
+
switch (cap->cap) {
case KVM_CAP_RISCV_MP_STATE_RESET:
- if (cap->flags)
- return -EINVAL;
kvm->arch.mp_state_reset = true;
return 0;
+ case KVM_CAP_VM_GPA_BITS: {
+ unsigned long gpa_bits = cap->args[0];
+ unsigned long new_levels;
+ int r = 0;
+
+ /* Decide target pgd levels from requested gpa_bits */
+#ifdef CONFIG_64BIT
+ if (gpa_bits <= 41)
+ new_levels = 3; /* Sv39x4 */
+ else if (gpa_bits <= 50)
+ new_levels = 4; /* Sv48x4 */
+ else if (gpa_bits <= 59)
+ new_levels = 5; /* Sv57x4 */
+ else
+ return -EINVAL;
+#else
+ /* 32-bit: only Sv32x4*/
+ if (gpa_bits <= 34)
+ new_levels = 2;
+ else
+ return -EINVAL;
+#endif
+ if (new_levels > kvm_riscv_gstage_max_pgd_levels)
+ return -EINVAL;
+
+ /* Follow KVM's lock ordering: kvm->lock -> kvm->slots_lock. */
+ mutex_lock(&kvm->lock);
+ mutex_lock(&kvm->slots_lock);
+
+ if (kvm->created_vcpus || !kvm_are_all_memslots_empty(kvm))
+ r = -EBUSY;
+ else
+ kvm->arch.pgd_levels = new_levels;
+
+ mutex_unlock(&kvm->slots_lock);
+ mutex_unlock(&kvm->lock);
+
+ return r;
+ }
default:
return -EINVAL;
}
--
2.50.1
next prev parent reply other threads:[~2026-04-03 15:30 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-03 15:30 [PATCH v8 0/3] Support runtime configuration for per-VM's HGATP mode fangyu.yu
2026-04-03 15:30 ` [PATCH v8 1/3] RISC-V: KVM: " fangyu.yu
2026-04-04 3:27 ` Nutty.Liu
2026-04-03 15:30 ` [PATCH v8 2/3] RISC-V: KVM: Cache gstage pgd_levels in struct kvm_gstage fangyu.yu
2026-04-04 3:25 ` Nutty.Liu
2026-04-03 15:30 ` fangyu.yu [this message]
2026-04-03 16:42 ` [PATCH v8 3/3] RISC-V: KVM: Reuse KVM_CAP_VM_GPA_BITS to select HGATP.MODE Anup Patel
2026-04-04 3:25 ` Nutty.Liu
2026-04-03 16:43 ` [PATCH v8 0/3] Support runtime configuration for per-VM's HGATP mode Anup Patel
2026-04-04 11:02 ` 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=20260403153019.9916-4-fangyu.yu@linux.alibaba.com \
--to=fangyu.yu@linux.alibaba.com \
--cc=alex@ghiti.fr \
--cc=andrew.jones@oss.qualcomm.com \
--cc=anup@brainfault.org \
--cc=aou@eecs.berkeley.edu \
--cc=atish.patra@linux.dev \
--cc=corbet@lwn.net \
--cc=guoren@kernel.org \
--cc=kvm-riscv@lists.infradead.org \
--cc=kvm@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--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=radim.krcmar@oss.qualcomm.com \
--cc=skhan@linuxfoundation.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