Skip to contentSkip to conversation

KVM: arm64: Allocate for maximum vector length for pKVM host sve state

PATCH Superseded Fuad Tabba 2 participants 6 messages Jun 5, 2024 – Jun 6, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Fuad Tabba

author
#0 lore raw

PATCH 1/1 KVM: arm64: Allocate for maximum vector length for pKVM host sve state

In a heterogeneous system, a cpu could have an sve/sme vector length that is larger than the rest. Store the value of the largest possible vector length in the system. When allocating memory for the host sve state in pKVM, allocate for the largest possible sve vector length.

Fixes 66d5b53e20a6 ("KVM: arm64: Allocate memory mapped at hyp for host sve state in pKVM")

---
Based on kvmarm/fixes (afb91f5f8ad7)

Mark Brown submitted a patch series that fixes this [1]. I'm sending this patch since it might be simpler.

[1] https://lore.kernel.org/all/[email protected]/

---
 arch/arm64/include/asm/fpsimd.h | 13 +++++++++++++
 arch/arm64/kernel/fpsimd.c      |  7 ++++++-
 arch/arm64/kvm/reset.c          |  2 +-
 3 files changed, 20 insertions(+), 2 deletions(-)
arch/arm64/include/asm/fpsimd.h +13 -0
index bc69ac368d73..a72dda932291 100644
@@ -184,6 +184,9 @@ struct vl_info {
 	int max_vl;
 	int max_virtualisable_vl;
 
+	/* The maximum vl encountered for any cpu in the system. */
+	int max_system_vl;
+
 	/*
 	 * Set of available vector lengths,
 	 * where length vq encoded as bit __vq_to_bit(vq):
@@ -274,6 +277,11 @@ static inline int vec_max_virtualisable_vl(enum vec_type type)
 	return vl_info[type].max_virtualisable_vl;
 }
 
+static inline int vec_max_system_vl(enum vec_type type)
+{
+	return vl_info[type].max_system_vl;
+}
+
 static inline int sve_max_vl(void)
 {
 	return vec_max_vl(ARM64_VEC_SVE);
@@ -284,6 +292,11 @@ static inline int sve_max_virtualisable_vl(void)
 	return vec_max_virtualisable_vl(ARM64_VEC_SVE);
 }
 
+static inline int sve_max_system_vl(void)
+{
+	return vec_max_system_vl(ARM64_VEC_SVE);
+}
+
 /* Ensure vq >= SVE_VQ_MIN && vq <= SVE_VQ_MAX before calling this function */
 static inline bool vq_available(enum vec_type type, unsigned int vq)
 {
arch/arm64/kernel/fpsimd.c +6 -1
index 82e8a6017382..e0af4c3c9a40 100644
@@ -1006,7 +1006,7 @@ int sme_get_current_vl(void)
 static void vec_probe_vqs(struct vl_info *info,
 			  DECLARE_BITMAP(map, SVE_VQ_MAX))
 {
-	unsigned int vq, vl;
+	unsigned int vq, vl, max_vl = 0;
 
 	bitmap_zero(map, SVE_VQ_MAX);
 
@@ -1031,7 +1031,12 @@ static void vec_probe_vqs(struct vl_info *info,
 
 		vq = sve_vq_from_vl(vl); /* skip intervening lengths */
 		set_bit(__vq_to_bit(vq), map);
+
+		if (!max_vl)
+			max_vl = vl;
 	}
+
+	info->max_system_vl = max((int) max_vl, info->max_system_vl);
 }
 
 /*
arch/arm64/kvm/reset.c +1 -1
index 3fc8ca164dbe..6236f9d0a0ec 100644
@@ -52,7 +52,7 @@ int __init kvm_arm_init_sve(void)
 {
 	if (system_supports_sve()) {
 		kvm_sve_max_vl = sve_max_virtualisable_vl();
-		kvm_host_sve_max_vl = sve_max_vl();
+		kvm_host_sve_max_vl = sve_max_system_vl();
 		kvm_nvhe_sym(kvm_host_sve_max_vl) = kvm_host_sve_max_vl;
 
 		/*

base-commit: afb91f5f8ad7af172d993a34fde1947892408f53

Signature · 1 line
2.45.2.505.gda0bf45e8d-goog
#1 lore raw

arch/arm64/include/asm/fpsimd.h struct vl_info { · line 189

Quoted 3 lines · Fuad Tabba
arch/arm64/include/asm/fpsimd.h +3 -0
+	/* The maximum vl encountered for any cpu in the system. */
+	int max_system_vl;
+

As previously mentioned I'd really prefer not to call this _system_ due to the potential confusion with the maximum VL we're using. Naming is hard.

arch/arm64/kernel/fpsimd.c static void vec_probe_vqs(struct vl_info *info, · line 1039

Quoted 10 lines
arch/arm64/kernel/fpsimd.c +5 -0
@@ -1031,7 +1031,12 @@ static void vec_probe_vqs(struct vl_info *info,
 
 		vq = sve_vq_from_vl(vl); /* skip intervening lengths */
 		set_bit(__vq_to_bit(vq), map);
+
+		if (!max_vl)
+			max_vl = vl;
 	}
+
+	info->max_system_vl = max((int) max_vl, info->max_system_vl);

It's not clear to me why we stage this in a local variable prior to updating the global value, and like I say it does feel cleaner and safer to keep the current split between enumeration of the PE and integration of the results. This should *work* but it doesn't feel consistent with the rest of the code.

arch/arm64/kvm/reset.c int __init kvm_arm_init_sve(void) · line 55

Quoted 2 lines
arch/arm64/kvm/reset.c +1 -1
-               kvm_host_sve_max_vl = sve_max_vl();
+               kvm_host_sve_max_vl = sve_max_system_vl();

I appreciate that we're adding a host_ on the front of it but it would feel safer to keep the name of the hypervisor copy of the value consistent with where we copied it from, sve_max_vl is a different thing in the host kernel.

author
#2 lore raw

Hi Mark,

Quoted 10 lines · Mark Brown

arch/arm64/include/asm/fpsimd.h struct vl_info { · line 189

Quoted 3 lines · Fuad Tabba
arch/arm64/include/asm/fpsimd.h +3 -0
+     /* The maximum vl encountered for any cpu in the system. */
+     int max_system_vl;
+

As previously mentioned I'd really prefer not to call this _system_ due to the potential confusion with the maximum VL we're using. Naming is hard.

I agree :) I have no strong preference really. It's just that for me max_cpu_vl sounds like it's per cpu. But now I'm just bikeshedding...

Quoted 16 lines

arch/arm64/kernel/fpsimd.c static void vec_probe_vqs(struct vl_info *info, · line 1039

Quoted 10 lines
arch/arm64/kernel/fpsimd.c +5 -0
@@ -1031,7 +1031,12 @@ static void vec_probe_vqs(struct vl_info *info,
 
              vq = sve_vq_from_vl(vl); /* skip intervening lengths */
              set_bit(__vq_to_bit(vq), map);
+
+             if (!max_vl)
+                     max_vl = vl;
      }
+
+     info->max_system_vl = max((int) max_vl, info->max_system_vl);

It's not clear to me why we stage this in a local variable prior to updating the global value, and like I say it does feel cleaner and safer to keep the current split between enumeration of the PE and integration of the results. This should *work* but it doesn't feel consistent with the rest of the code.

To me it felt more clear like this, but I'm fine with not doing the staging first.

Quoted 8 lines

arch/arm64/kvm/reset.c int __init kvm_arm_init_sve(void) · line 55

Quoted 3 lines
arch/arm64/kvm/reset.c +1 -1
-               kvm_host_sve_max_vl = sve_max_vl();
+               kvm_host_sve_max_vl = sve_max_system_vl();

I appreciate that we're adding a host_ on the front of it but it would feel safer to keep the name of the hypervisor copy of the value consistent with where we copied it from, sve_max_vl is a different thing in the host kernel.

I agree, this should be renamed.

Thanks!
/fuad

#3 lore raw
Quoted 7 lines · Fuad Tabba
Quoted 5 lines · Mark Brown
Quoted 3 lines · Fuad Tabba
arch/arm64/include/asm/fpsimd.h +3 -0
+     /* The maximum vl encountered for any cpu in the system. */
+     int max_system_vl;
+
Quoted 6 lines
Quoted 3 lines

As previously mentioned I'd really prefer not to call this _system_ due to the potential confusion with the maximum VL we're using. Naming is hard.

I agree :) I have no strong preference really. It's just that for me max_cpu_vl sounds like it's per cpu. But now I'm just bikeshedding...

seen?

Quoted 17 lines
Quoted 16 lines

arch/arm64/kernel/fpsimd.c static void vec_probe_vqs(struct vl_info *info, · line 1039

Quoted 10 lines
arch/arm64/kernel/fpsimd.c +5 -0
@@ -1031,7 +1031,12 @@ static void vec_probe_vqs(struct vl_info *info,
 
              vq = sve_vq_from_vl(vl); /* skip intervening lengths */
              set_bit(__vq_to_bit(vq), map);
+
+             if (!max_vl)
+                     max_vl = vl;
      }
+
+     info->max_system_vl = max((int) max_vl, info->max_system_vl);

It's not clear to me why we stage this in a local variable prior to updating the global value, and like I say it does feel cleaner and safer to keep the current split between enumeration of the PE and integration of the results. This should *work* but it doesn't feel consistent with the rest of the code.

Quoted 2 lines

To me it felt more clear like this, but I'm fine with not doing the staging first.

That was the start of the thought path that lead me to implement based on splitting it out of vec_probe_vqs() like the rest of the code does, the logic doesn't really fit in here for multiple reasons.

Quoted 14 lines
Quoted 9 lines

arch/arm64/kvm/reset.c int __init kvm_arm_init_sve(void) · line 55

Quoted 4 lines
arch/arm64/kvm/reset.c +1 -1
-               kvm_host_sve_max_vl = sve_max_vl();
+               kvm_host_sve_max_vl = sve_max_system_vl();

I appreciate that we're adding a host_ on the front of it but it would feel safer to keep the name of the hypervisor copy of the value consistent with where we copied it from, sve_max_vl is a different thing in the host kernel.

I agree, this should be renamed.

Thanks!
/fuad

author
#4 lore raw

Hi Mark,

Quoted 35 lines · Mark Brown
Quoted 7 lines · Fuad Tabba
Quoted 5 lines · Mark Brown
Quoted 3 lines · Fuad Tabba
arch/arm64/include/asm/fpsimd.h +3 -0
+     /* The maximum vl encountered for any cpu in the system. */
+     int max_system_vl;
+
Quoted 6 lines
Quoted 3 lines

As previously mentioned I'd really prefer not to call this _system_ due to the potential confusion with the maximum VL we're using. Naming is hard.

I agree :) I have no strong preference really. It's just that for me max_cpu_vl sounds like it's per cpu. But now I'm just bikeshedding...

seen?

Quoted 16 lines
Quoted 16 lines

arch/arm64/kernel/fpsimd.c static void vec_probe_vqs(struct vl_info *info, · line 1039

Quoted 10 lines
arch/arm64/kernel/fpsimd.c +5 -0
@@ -1031,7 +1031,12 @@ static void vec_probe_vqs(struct vl_info *info,
 
              vq = sve_vq_from_vl(vl); /* skip intervening lengths */
              set_bit(__vq_to_bit(vq), map);
+
+             if (!max_vl)
+                     max_vl = vl;
      }
+
+     info->max_system_vl = max((int) max_vl, info->max_system_vl);

It's not clear to me why we stage this in a local variable prior to updating the global value, and like I say it does feel cleaner and safer to keep the current split between enumeration of the PE and integration of the results. This should *work* but it doesn't feel consistent with the rest of the code.

Quoted 6 lines
Quoted 2 lines

To me it felt more clear like this, but I'm fine with not doing the staging first.

That was the start of the thought path that lead me to implement based on splitting it out of vec_probe_vqs() like the rest of the code does, the logic doesn't really fit in here for multiple reasons.

The alternative entails duplicating code in vec_init_vq_map() and vec_update_vq_map().

To me at least, it's straightforward to understand how max_vl is computed if we set it as soon as we find it. This as opposed to first having the vq bits set in a map, then in two different functions find the first set bit in the map, which really corresponds to the vq, to then convert it back to a vl.

Thanks,
/fuad

Quoted 14 lines
Quoted 14 lines
Quoted 9 lines

arch/arm64/kvm/reset.c int __init kvm_arm_init_sve(void) · line 55

Quoted 4 lines
arch/arm64/kvm/reset.c +1 -1
-               kvm_host_sve_max_vl = sve_max_vl();
+               kvm_host_sve_max_vl = sve_max_system_vl();

I appreciate that we're adding a host_ on the front of it but it would feel safer to keep the name of the hypervisor copy of the value consistent with where we copied it from, sve_max_vl is a different thing in the host kernel.

I agree, this should be renamed.

Thanks!
/fuad

#5 lore raw
Quoted 7 lines · Fuad Tabba
Quoted 5 lines · Mark Brown

On Wed, Jun 05, 2024 at 05:14:23PM +0100, Fuad Tabba wrote:

That was the start of the thought path that lead me to implement based on splitting it out of vec_probe_vqs() like the rest of the code does, the logic doesn't really fit in here for multiple reasons.

Quoted 8 lines

The alternative entails duplicating code in vec_init_vq_map() and vec_update_vq_map().

To me at least, it's straightforward to understand how max_vl is computed if we set it as soon as we find it. This as opposed to first having the vq bits set in a map, then in two different functions find the first set bit in the map, which really corresponds to the vq, to then convert it back to a vl.

We need handling in multiple places in part because we do actually want to treat late CPUs differently to CPUs that come up during normal boot, even if we can get away with it for now with pKVM due to it rejecting late CPUs entirely. The bouncing between VQ and VL is I agree a little annoying, if you're looking at this code you're familiar with it though. TBH not having it is a bit of a "did we miss something?" flag.