From: Mark Brown <broonie@kernel.org>
To: Marc Zyngier <maz@kernel.org>,
Oliver Upton <oliver.upton@linux.dev>,
Joey Gouly <joey.gouly@arm.com>,
Catalin Marinas <catalin.marinas@arm.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Will Deacon <will@kernel.org>,
Paolo Bonzini <pbonzini@redhat.com>,
Jonathan Corbet <corbet@lwn.net>, Shuah Khan <shuah@kernel.org>
Cc: Dave Martin <Dave.Martin@arm.com>, Fuad Tabba <tabba@google.com>,
Mark Rutland <mark.rutland@arm.com>,
linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev,
linux-kernel@vger.kernel.org, kvm@vger.kernel.org,
linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org,
Mark Brown <broonie@kernel.org>
Subject: [PATCH v4 25/27] KVM: arm64: Provide interface for configuring and enabling SME for guests
Date: Fri, 14 Feb 2025 01:58:08 +0000 [thread overview]
Message-ID: <20250214-kvm-arm64-sme-v4-25-d64a681adcc2@kernel.org> (raw)
In-Reply-To: <20250214-kvm-arm64-sme-v4-0-d64a681adcc2@kernel.org>
Since SME requires configuration of a vector length in order to know the
size of both the streaming mode SVE state and ZA array we implement a
capability for it and require that it be enabled and finalized before
the SME specific state can be accessed, similarly to SVE.
Due to the overlap with sizing the SVE state we finalise both SVE and
SME with a single finalization, preventing any further changes to the
SVE and SME configuration once KVM_ARM_VCPU_VEC (an alias for _VCPU_SVE)
has been finalised. This is not a thing of great elegance but it ensures
that we never have a state where one of SVE or SME is finalised and the
other not, avoiding complexity.
Signed-off-by: Mark Brown <broonie@kernel.org>
---
arch/arm64/include/asm/kvm_host.h | 5 +-
arch/arm64/include/uapi/asm/kvm.h | 1 +
arch/arm64/kvm/arm.c | 10 ++++
arch/arm64/kvm/reset.c | 114 ++++++++++++++++++++++++++++++++------
include/uapi/linux/kvm.h | 1 +
5 files changed, 111 insertions(+), 20 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index bbab22688a465dc0b5a4463cfaf061612b550fbd..ab7f0cddfcd2b57b742fd5193ebe44cbb8ca8677 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -79,6 +79,7 @@ extern unsigned int __ro_after_init kvm_host_max_vl[ARM64_VEC_MAX];
DECLARE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
int __init kvm_arm_init_sve(void);
+int __init kvm_arm_init_sme(void);
u32 __attribute_const__ kvm_target_cpu(void);
void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
@@ -1013,10 +1014,10 @@ struct kvm_vcpu_arch {
size_t __size_ret; \
unsigned int __vcpu_vq; \
\
- if (WARN_ON(!sve_vl_valid((vcpu)->arch.max_vl[ARM64_VEC_SVE]))) { \
+ if (WARN_ON(!sve_vl_valid(vcpu_max_vl(vcpu)))) { \
__size_ret = 0; \
} else { \
- __vcpu_vq = vcpu_sve_max_vq(vcpu); \
+ __vcpu_vq = sve_vl_from_vq(vcpu_max_vl(vcpu)); \
__size_ret = SVE_SIG_REGS_SIZE(__vcpu_vq); \
} \
\
diff --git a/arch/arm64/include/uapi/asm/kvm.h b/arch/arm64/include/uapi/asm/kvm.h
index 1f75c9938f313e70a82d9792cb3c89caec2076a1..80a7d3119ec8421ca5c7050ed996fc9ba09ad7cf 100644
--- a/arch/arm64/include/uapi/asm/kvm.h
+++ b/arch/arm64/include/uapi/asm/kvm.h
@@ -105,6 +105,7 @@ struct kvm_regs {
#define KVM_ARM_VCPU_PTRAUTH_ADDRESS 5 /* VCPU uses address authentication */
#define KVM_ARM_VCPU_PTRAUTH_GENERIC 6 /* VCPU uses generic authentication */
#define KVM_ARM_VCPU_HAS_EL2 7 /* Support nested virtualization */
+#define KVM_ARM_VCPU_SME 8 /* enable SME for this CPU */
/*
* An alias for _SVE since we finalize VL configuration for both SVE and SME
diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index b8e55a441282f57cb2d4bc55a43b41d5b774dfdd..5f44fe80af67369764d979e3cacd5bedacc5f7cb 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -377,6 +377,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
case KVM_CAP_ARM_SVE:
r = system_supports_sve();
break;
+ case KVM_CAP_ARM_SME:
+ r = system_supports_sme();
+ break;
case KVM_CAP_ARM_PTRAUTH_ADDRESS:
case KVM_CAP_ARM_PTRAUTH_GENERIC:
r = kvm_has_full_ptr_auth();
@@ -1394,6 +1397,9 @@ static unsigned long system_supported_vcpu_features(void)
if (!system_supports_sve())
clear_bit(KVM_ARM_VCPU_SVE, &features);
+ if (!system_supports_sme())
+ clear_bit(KVM_ARM_VCPU_SME, &features);
+
if (!kvm_has_full_ptr_auth()) {
clear_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, &features);
clear_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features);
@@ -2784,6 +2790,10 @@ static __init int kvm_arm_init(void)
if (err)
return err;
+ err = kvm_arm_init_sme();
+ if (err)
+ return err;
+
err = kvm_arm_vmid_alloc_init();
if (err) {
kvm_err("Failed to initialize VMID allocator.\n");
diff --git a/arch/arm64/kvm/reset.c b/arch/arm64/kvm/reset.c
index 3cb91dc6dc3dc5cc484900dbd9f4cdfedb3e2b4a..78b5aa7976dbbfec35d83f5e373ef87636c2e3e4 100644
--- a/arch/arm64/kvm/reset.c
+++ b/arch/arm64/kvm/reset.c
@@ -76,6 +76,34 @@ int __init kvm_arm_init_sve(void)
return 0;
}
+int __init kvm_arm_init_sme(void)
+{
+ if (system_supports_sme()) {
+ kvm_max_vl[ARM64_VEC_SME] = sme_max_virtualisable_vl();
+ kvm_host_max_vl[ARM64_VEC_SME] = sme_max_vl();
+ kvm_nvhe_sym(kvm_host_max_vl[ARM64_VEC_SME]) = kvm_host_max_vl[ARM64_VEC_SME];
+
+ /*
+ * The get_sve_reg()/set_sve_reg() ioctl interface will need
+ * to be extended with multiple register slice support in
+ * order to support vector lengths greater than
+ * VL_ARCH_MAX:
+ */
+ if (WARN_ON(kvm_max_vl[ARM64_VEC_SME] > VL_ARCH_MAX))
+ kvm_max_vl[ARM64_VEC_SME] = VL_ARCH_MAX;
+
+ /*
+ * Don't even try to make use of vector lengths that
+ * aren't available on all CPUs, for now:
+ */
+ if (kvm_max_vl[ARM64_VEC_SME] < sme_max_vl())
+ pr_warn("KVM: SME vector length for guests limited to %u bytes\n",
+ kvm_max_vl[ARM64_VEC_SME]);
+ }
+
+ return 0;
+}
+
static void kvm_vcpu_enable_sve(struct kvm_vcpu *vcpu)
{
vcpu->arch.max_vl[ARM64_VEC_SVE] = kvm_max_vl[ARM64_VEC_SVE];
@@ -88,42 +116,84 @@ static void kvm_vcpu_enable_sve(struct kvm_vcpu *vcpu)
set_bit(KVM_ARCH_FLAG_GUEST_HAS_SVE, &vcpu->kvm->arch.flags);
}
+static void kvm_vcpu_enable_sme(struct kvm_vcpu *vcpu)
+{
+ vcpu->arch.max_vl[ARM64_VEC_SME] = kvm_max_vl[ARM64_VEC_SME];
+
+ /*
+ * Userspace can still customize the vector lengths by writing
+ * KVM_REG_ARM64_SME_VLS. Allocation is deferred until
+ * kvm_arm_vcpu_finalize(), which freezes the configuration.
+ */
+ set_bit(KVM_ARCH_FLAG_GUEST_HAS_SME, &vcpu->kvm->arch.flags);
+}
+
/*
- * Finalize vcpu's maximum SVE vector length, allocating
- * vcpu->arch.sve_state as necessary.
+ * Finalize vcpu's maximum vector lengths, allocating
+ * vcpu->arch.sve_state and vcpu->arch.sme_state as necessary.
*/
static int kvm_vcpu_finalize_vec(struct kvm_vcpu *vcpu)
{
- void *buf;
+ void *sve_state, *sme_state;
unsigned int vl;
- size_t reg_sz;
int ret;
- vl = vcpu->arch.max_vl[ARM64_VEC_SVE];
-
/*
* Responsibility for these properties is shared between
* kvm_arm_init_sve(), kvm_vcpu_enable_sve() and
* set_sve_vls(). Double-check here just to be sure:
*/
- if (WARN_ON(!sve_vl_valid(vl) || vl > sve_max_virtualisable_vl() ||
- vl > VL_ARCH_MAX))
- return -EIO;
+ if (vcpu_has_sve(vcpu)) {
+ vl = vcpu->arch.max_vl[ARM64_VEC_SVE];
+ if (WARN_ON(!sve_vl_valid(vl) ||
+ vl > sve_max_virtualisable_vl() ||
+ vl > VL_ARCH_MAX))
+ return -EIO;
+ }
- reg_sz = vcpu_sve_state_size(vcpu);
- buf = kzalloc(reg_sz, GFP_KERNEL_ACCOUNT);
- if (!buf)
+ /* Similarly for SME */
+ if (vcpu_has_sme(vcpu)) {
+ vl = vcpu->arch.max_vl[ARM64_VEC_SME];
+ if (WARN_ON(!sve_vl_valid(vl) ||
+ vl > sme_max_virtualisable_vl() ||
+ vl > VL_ARCH_MAX))
+ return -EIO;
+ }
+
+ sve_state = kzalloc(vcpu_sve_state_size(vcpu), GFP_KERNEL_ACCOUNT);
+ if (!sve_state)
return -ENOMEM;
- ret = kvm_share_hyp(buf, buf + reg_sz);
- if (ret) {
- kfree(buf);
- return ret;
+ ret = kvm_share_hyp(sve_state, sve_state + vcpu_sve_state_size(vcpu));
+ if (ret)
+ goto err_sve_alloc;
+
+ if (vcpu_has_sme(vcpu)) {
+ sme_state = kzalloc(vcpu_sme_state_size(vcpu),
+ GFP_KERNEL_ACCOUNT);
+ if (!sme_state) {
+ ret = -ENOMEM;
+ goto err_sve_map;
+ }
+
+ ret = kvm_share_hyp(sme_state,
+ sme_state + vcpu_sme_state_size(vcpu));
+ if (ret)
+ goto err_sve_map;
+ } else {
+ sme_state = NULL;
}
-
- vcpu->arch.sve_state = buf;
+
+ vcpu->arch.sve_state = sve_state;
+ vcpu->arch.sme_state = sme_state;
vcpu_set_flag(vcpu, VCPU_VEC_FINALIZED);
return 0;
+
+err_sve_map:
+ kvm_unshare_hyp(sve_state, sve_state + vcpu_sve_state_size(vcpu));
+err_sve_alloc:
+ kfree(sve_state);
+ return ret;
}
int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature)
@@ -153,11 +223,15 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu)
void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
{
void *sve_state = vcpu->arch.sve_state;
+ void *sme_state = vcpu->arch.sme_state;
kvm_unshare_hyp(vcpu, vcpu + 1);
if (sve_state)
kvm_unshare_hyp(sve_state, sve_state + vcpu_sve_state_size(vcpu));
kfree(sve_state);
+ if (sme_state)
+ kvm_unshare_hyp(sme_state, sme_state + vcpu_sme_state_size(vcpu));
+ kfree(sme_state);
kfree(vcpu->arch.ccsidr);
}
@@ -165,6 +239,8 @@ static void kvm_vcpu_reset_vec(struct kvm_vcpu *vcpu)
{
if (vcpu_has_sve(vcpu))
memset(vcpu->arch.sve_state, 0, vcpu_sve_state_size(vcpu));
+ if (vcpu_has_sme(vcpu))
+ memset(vcpu->arch.sme_state, 0, vcpu_sme_state_size(vcpu));
}
/**
@@ -207,6 +283,8 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
if (!kvm_arm_vcpu_vec_finalized(vcpu)) {
if (vcpu_has_feature(vcpu, KVM_ARM_VCPU_SVE))
kvm_vcpu_enable_sve(vcpu);
+ if (vcpu_has_feature(vcpu, KVM_ARM_VCPU_SME))
+ kvm_vcpu_enable_sme(vcpu);
} else {
kvm_vcpu_reset_vec(vcpu);
}
diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
index 45e6d8fca9b9969622dc15a6af924221782ac2e0..b1d9bd08e0c4643e5000dda5d2c5540e528fce7b 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -929,6 +929,7 @@ struct kvm_enable_cap {
#define KVM_CAP_PRE_FAULT_MEMORY 236
#define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237
#define KVM_CAP_X86_GUEST_MODE 238
+#define KVM_CAP_ARM_SME 239
struct kvm_irq_routing_irqchip {
__u32 irqchip;
--
2.39.5
next prev parent reply other threads:[~2025-02-14 2:02 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-14 1:57 [PATCH v4 00/27] KVM: arm64: Implement support for SME in non-protected guests Mark Brown
2025-02-14 1:57 ` [PATCH v4 01/27] arm64/fpsimd: Update FA64 and ZT0 enables when loading SME state Mark Brown
2025-02-14 1:57 ` [PATCH v4 02/27] arm64/fpsimd: Decide to save ZT0 and streaming mode FFR at bind time Mark Brown
2025-02-14 1:57 ` [PATCH v4 03/27] arm64/fpsimd: Check enable bit for FA64 when saving EFI state Mark Brown
2025-02-14 1:57 ` [PATCH v4 04/27] arm64/fpsimd: Determine maximum virtualisable SME vector length Mark Brown
2025-02-14 1:57 ` [PATCH v4 05/27] KVM: arm64: Introduce non-UNDEF FGT control Mark Brown
2025-02-14 1:57 ` [PATCH v4 06/27] KVM: arm64: Pay attention to FFR parameter in SVE save and load Mark Brown
2025-02-14 1:57 ` [PATCH v4 07/27] KVM: arm64: Pull ctxt_has_ helpers to start of sysreg-sr.h Mark Brown
2025-02-14 1:57 ` [PATCH v4 08/27] KVM: arm64: Move SVE state access macros after feature test macros Mark Brown
2025-02-14 1:57 ` [PATCH v4 09/27] KVM: arm64: Rename SVE finalization constants to be more general Mark Brown
2025-02-14 1:57 ` [PATCH v4 10/27] KVM: arm64: Document the KVM ABI for SME Mark Brown
2025-02-14 1:57 ` [PATCH v4 11/27] KVM: arm64: Define internal features " Mark Brown
2025-02-14 1:57 ` [PATCH v4 12/27] KVM: arm64: Rename sve_state_reg_region Mark Brown
2025-02-14 1:57 ` [PATCH v4 13/27] KVM: arm64: Store vector lengths in an array Mark Brown
2025-02-14 1:57 ` [PATCH v4 14/27] KVM: arm64: Implement SME vector length configuration Mark Brown
2025-02-14 1:57 ` [PATCH v4 15/27] KVM: arm64: Support SME control registers Mark Brown
2025-02-14 1:57 ` [PATCH v4 16/27] KVM: arm64: Support TPIDR2_EL0 Mark Brown
2025-02-14 1:58 ` [PATCH v4 17/27] KVM: arm64: Support SME identification registers for guests Mark Brown
2025-02-14 1:58 ` [PATCH v4 18/27] KVM: arm64: Support SME priority registers Mark Brown
2025-02-14 1:58 ` [PATCH v4 19/27] KVM: arm64: Provide assembly for SME state restore Mark Brown
2025-02-14 1:58 ` [PATCH v4 20/27] KVM: arm64: Support userspace access to streaming mode Z and P registers Mark Brown
2025-02-14 1:58 ` [PATCH v4 21/27] KVM: arm64: Expose SME specific state to userspace Mark Brown
2025-02-14 1:58 ` [PATCH v4 22/27] KVM: arm64: Context switch SME state for normal guests Mark Brown
2025-02-14 1:58 ` [PATCH v4 23/27] KVM: arm64: Handle SME exceptions Mark Brown
2025-02-14 1:58 ` [PATCH v4 24/27] KVM: arm64: Expose SME to nested guests Mark Brown
2025-02-14 1:58 ` Mark Brown [this message]
2025-02-14 1:58 ` [PATCH v4 26/27] KVM: arm64: selftests: Add SME system registers to get-reg-list Mark Brown
2025-02-14 1:58 ` [PATCH v4 27/27] KVM: arm64: selftests: Add SME to set_id_regs test Mark Brown
2025-02-14 9:24 ` [PATCH v4 00/27] KVM: arm64: Implement support for SME in non-protected guests Marc Zyngier
2025-02-14 15:13 ` Mark Brown
2025-02-17 9:37 ` Marc Zyngier
2025-02-18 22:54 ` Mark Brown
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=20250214-kvm-arm64-sme-v4-25-d64a681adcc2@kernel.org \
--to=broonie@kernel.org \
--cc=Dave.Martin@arm.com \
--cc=catalin.marinas@arm.com \
--cc=corbet@lwn.net \
--cc=joey.gouly@arm.com \
--cc=kvm@vger.kernel.org \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=maz@kernel.org \
--cc=oliver.upton@linux.dev \
--cc=pbonzini@redhat.com \
--cc=shuah@kernel.org \
--cc=suzuki.poulose@arm.com \
--cc=tabba@google.com \
--cc=will@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®