diff mbox series

[kvmtool] arm64: Allow the user to select the max SVE vector length

Message ID 20240620165702.1134918-2-oliver.upton@linux.dev (mailing list archive)
State New, archived
Headers show
Series [kvmtool] arm64: Allow the user to select the max SVE vector length | expand

Commit Message

Oliver Upton June 20, 2024, 4:57 p.m. UTC
Add a new flag, --sve-max-vl, which allows the user to specify an SVE
vector length for the VM. Just zero out unsupported VLs from what KVM
supports rather than cooking up the bitmap from scratch.

Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
---
 arm/aarch64/include/kvm/kvm-config-arch.h |  6 ++-
 arm/aarch64/kvm-cpu.c                     | 65 ++++++++++++++++++++---
 arm/include/arm-common/kvm-config-arch.h  |  3 +-
 3 files changed, 65 insertions(+), 9 deletions(-)


base-commit: da4cfc3e540341b84c4bbad705b5a15865bc1f80

Comments

Will Deacon July 5, 2024, 10:54 a.m. UTC | #1
On Thu, 20 Jun 2024 16:57:03 +0000, Oliver Upton wrote:
> Add a new flag, --sve-max-vl, which allows the user to specify an SVE
> vector length for the VM. Just zero out unsupported VLs from what KVM
> supports rather than cooking up the bitmap from scratch.
> 
> 

Applied to kvmtool (master), thanks!

[1/1] arm64: Allow the user to select the max SVE vector length
      https://git.kernel.org/will/kvmtool/c/ca31abf5d9c3

Cheers,
diff mbox series

Patch

diff --git a/arm/aarch64/include/kvm/kvm-config-arch.h b/arm/aarch64/include/kvm/kvm-config-arch.h
index eae8080d3fd9..642fe672d833 100644
--- a/arm/aarch64/include/kvm/kvm-config-arch.h
+++ b/arm/aarch64/include/kvm/kvm-config-arch.h
@@ -2,6 +2,7 @@ 
 #define KVM__KVM_CONFIG_ARCH_H
 
 int vcpu_affinity_parser(const struct option *opt, const char *arg, int unset);
+int sve_vl_parser(const struct option *opt, const char *arg, int unset);
 
 #define ARM_OPT_ARCH_RUN(cfg)						\
 	OPT_BOOLEAN('\0', "aarch32", &(cfg)->aarch32_guest,		\
@@ -19,7 +20,10 @@  int vcpu_affinity_parser(const struct option *opt, const char *arg, int unset);
 			"Specify random seed for Kernel Address Space "	\
 			"Layout Randomization (KASLR)"),		\
 	OPT_BOOLEAN('\0', "no-pvtime", &(cfg)->no_pvtime, "Disable"	\
-			" stolen time"),
+			" stolen time"),				\
+	OPT_CALLBACK('\0', "sve-max-vl", NULL, "vector length",		\
+		     "Specify the max SVE vector length (in bits) for "	\
+		     "all vCPUs", sve_vl_parser, kvm),
 #include "arm-common/kvm-config-arch.h"
 
 #endif /* KVM__KVM_CONFIG_ARCH_H */
diff --git a/arm/aarch64/kvm-cpu.c b/arm/aarch64/kvm-cpu.c
index c8be10b3ca94..7b6061af00e6 100644
--- a/arm/aarch64/kvm-cpu.c
+++ b/arm/aarch64/kvm-cpu.c
@@ -3,6 +3,7 @@ 
 #include "kvm/virtio.h"
 
 #include <asm/ptrace.h>
+#include <linux/bitops.h>
 
 #define COMPAT_PSR_F_BIT	0x00000040
 #define COMPAT_PSR_I_BIT	0x00000080
@@ -154,17 +155,67 @@  void kvm_cpu__select_features(struct kvm *kvm, struct kvm_vcpu_init *init)
 		init->features[0] |= 1UL << KVM_ARM_VCPU_SVE;
 }
 
-int kvm_cpu__configure_features(struct kvm_cpu *vcpu)
+int sve_vl_parser(const struct option *opt, const char *arg, int unset)
 {
-	if (kvm__supports_extension(vcpu->kvm, KVM_CAP_ARM_SVE)) {
-		int feature = KVM_ARM_VCPU_SVE;
+	struct kvm *kvm = opt->ptr;
+	unsigned long val;
+	unsigned int vq;
+
+	errno = 0;
+	val = strtoull(arg, NULL, 10);
+	if (errno == ERANGE)
+		die("SVE vector length too large: %s", arg);
+
+	if (!val || (val & (val - 1)))
+		die("SVE vector length isn't power of 2: %s", arg);
+
+	vq = val / 128;
+	if (vq > KVM_ARM64_SVE_VQ_MAX || vq < KVM_ARM64_SVE_VQ_MIN)
+		die("SVE vector length out of range: %s", arg);
+
+	kvm->cfg.arch.sve_max_vq = vq;
+	return 0;
+}
+
+static int vcpu_configure_sve(struct kvm_cpu *vcpu)
+{
+	unsigned int max_vq = vcpu->kvm->cfg.arch.sve_max_vq;
+	int feature = KVM_ARM_VCPU_SVE;
+
+	if (max_vq) {
+		unsigned long vls[KVM_ARM64_SVE_VLS_WORDS];
+		struct kvm_one_reg reg = {
+			.id	= KVM_REG_ARM64_SVE_VLS,
+			.addr	= (u64)&vls,
+		};
+		unsigned int vq;
+
+		if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg))
+			die_perror("KVM_GET_ONE_REG failed (KVM_ARM64_SVE_VLS)");
 
-		if (ioctl(vcpu->vcpu_fd, KVM_ARM_VCPU_FINALIZE, &feature)) {
-			pr_err("KVM_ARM_VCPU_FINALIZE: %s", strerror(errno));
-			return -1;
-		}
+		if (!test_bit(max_vq - KVM_ARM64_SVE_VQ_MIN, vls))
+			die("SVE vector length (%u) not supported", max_vq * 128);
+
+		for (vq = KVM_ARM64_SVE_VQ_MAX; vq > max_vq; vq--)
+			clear_bit(vq - KVM_ARM64_SVE_VQ_MIN, vls);
+
+		if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg))
+			die_perror("KVM_SET_ONE_REG failed (KVM_ARM64_SVE_VLS)");
 	}
 
+	if (ioctl(vcpu->vcpu_fd, KVM_ARM_VCPU_FINALIZE, &feature)) {
+		pr_err("KVM_ARM_VCPU_FINALIZE: %s", strerror(errno));
+		return -1;
+	}
+
+	return 0;
+}
+
+int kvm_cpu__configure_features(struct kvm_cpu *vcpu)
+{
+	if (kvm__supports_extension(vcpu->kvm, KVM_CAP_ARM_SVE))
+		return vcpu_configure_sve(vcpu);
+
 	return 0;
 }
 
diff --git a/arm/include/arm-common/kvm-config-arch.h b/arm/include/arm-common/kvm-config-arch.h
index 23a74867a474..4722d8f8acf7 100644
--- a/arm/include/arm-common/kvm-config-arch.h
+++ b/arm/include/arm-common/kvm-config-arch.h
@@ -13,7 +13,8 @@  struct kvm_config_arch {
 	u64		kaslr_seed;
 	enum irqchip_type irqchip;
 	u64		fw_addr;
-	bool no_pvtime;
+	unsigned int	sve_max_vq;
+	bool		no_pvtime;
 };
 
 int irqchip_parser(const struct option *opt, const char *arg, int unset);