arm64/sme: Set new vector length before reallocating

Message ID 20230726-arm64-fix-sme-fix-v1-1-7752ec58af27@kernel.org
State New
Headers
Series arm64/sme: Set new vector length before reallocating |

Commit Message

Mark Brown July 26, 2023, 12:12 p.m. UTC
  As part of fixing the allocation of the buffer for SVE state when changing
SME vector length we introduced an immediate reallocation of the SVE state,
this is also done when changing the SVE vector length for consistency.
Unfortunately this reallocation is done prior to writing the new vector
length to the task struct, meaning the allocation is done with the old
vector length and can lead to memory corruption due to an undersized buffer
being used.

Move the update of the vector length before the allocation to ensure that
the new vector length is taken into account.

For some reason this isn't triggering any problems when running tests on
the arm64 fixes branch (even after repeated tries) but is triggering
issues very often after merge into mainline.

Fixes: d4d5be94a878 ("arm64/fpsimd: Ensure SME storage is allocated after SVE VL changes")
Signed-off-by: Mark Brown <broonie@kernel.org>
---
 arch/arm64/kernel/fpsimd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)


---
base-commit: d4d5be94a87872421ea2569044092535aff0b886
change-id: 20230726-arm64-fix-sme-fix-d02461350e06

Best regards,
  

Comments

Catalin Marinas July 26, 2023, 5:37 p.m. UTC | #1
On Wed, Jul 26, 2023 at 01:12:26PM +0100, Mark Brown wrote:
> As part of fixing the allocation of the buffer for SVE state when changing
> SME vector length we introduced an immediate reallocation of the SVE state,
> this is also done when changing the SVE vector length for consistency.
> Unfortunately this reallocation is done prior to writing the new vector
> length to the task struct, meaning the allocation is done with the old
> vector length and can lead to memory corruption due to an undersized buffer
> being used.
> 
> Move the update of the vector length before the allocation to ensure that
> the new vector length is taken into account.
> 
> For some reason this isn't triggering any problems when running tests on
> the arm64 fixes branch (even after repeated tries) but is triggering
> issues very often after merge into mainline.
> 
> Fixes: d4d5be94a878 ("arm64/fpsimd: Ensure SME storage is allocated after SVE VL changes")
> Signed-off-by: Mark Brown <broonie@kernel.org>

I'll add a cc stable here since the above commit was fixing code that
went in for 5.19.
  
Catalin Marinas July 26, 2023, 6:45 p.m. UTC | #2
On Wed, 26 Jul 2023 13:12:26 +0100, Mark Brown wrote:
> As part of fixing the allocation of the buffer for SVE state when changing
> SME vector length we introduced an immediate reallocation of the SVE state,
> this is also done when changing the SVE vector length for consistency.
> Unfortunately this reallocation is done prior to writing the new vector
> length to the task struct, meaning the allocation is done with the old
> vector length and can lead to memory corruption due to an undersized buffer
> being used.
> 
> [...]

Applied to arm64 (for-next/fixes), thanks!

[1/1] arm64/sme: Set new vector length before reallocating
      https://git.kernel.org/arm64/c/05d881b85b48
  

Patch

diff --git a/arch/arm64/kernel/fpsimd.c b/arch/arm64/kernel/fpsimd.c
index 89d54a5242d1..c19092642920 100644
--- a/arch/arm64/kernel/fpsimd.c
+++ b/arch/arm64/kernel/fpsimd.c
@@ -917,6 +917,8 @@  int vec_set_vector_length(struct task_struct *task, enum vec_type type,
 	if (task == current)
 		put_cpu_fpsimd_context();
 
+	task_set_vl(task, type, vl);
+
 	/*
 	 * Free the changed states if they are not in use, SME will be
 	 * reallocated to the correct size on next use and we just
@@ -931,8 +933,6 @@  int vec_set_vector_length(struct task_struct *task, enum vec_type type,
 	if (free_sme)
 		sme_free(task);
 
-	task_set_vl(task, type, vl);
-
 out:
 	update_tsk_thread_flag(task, vec_vl_inherit_flag(type),
 			       flags & PR_SVE_VL_INHERIT);