Skip to content

Commit 316283f

Browse files
mrutland-armwilldeacon
authored andcommitted
arm64/fpsimd: ptrace: Consistently handle partial writes to NT_ARM_(S)SVE
Partial writes to the NT_ARM_SVE and NT_ARM_SSVE regsets using an payload are handled inconsistently and non-deterministically. A comment within sve_set_common() indicates that we intended that a partial write would preserve any effective FPSIMD/SVE state which was not overwritten, but this has never worked consistently, and during syscalls the FPSIMD vector state may be non-deterministically preserved and may be erroneously migrated between streaming and non-streaming SVE modes. The simplest fix is to handle a partial write by consistently zeroing the remaining state. As detailed below I do not believe this will adversely affect any real usage. Neither GDB nor LLDB attempt partial writes to these regsets, and the documentation (in Documentation/arch/arm64/sve.rst) has always indicated that state preservation was not guaranteed, as is says: | The effect of writing a partial, incomplete payload is unspecified. When the logic was originally introduced in commit: 43d4da2 ("arm64/sve: ptrace and ELF coredump support") ... there were two potential behaviours, depending on TIF_SVE: * When TIF_SVE was clear, all SVE state would be zeroed, excluding the low 128 bits of vectors shared with FPSIMD, FPSR, and FPCR. * When TIF_SVE was set, all SVE state would be zeroed, including the low 128 bits of vectors shared with FPSIMD, but excluding FPSR and FPCR. Note that as writing to NT_ARM_SVE would set TIF_SVE, partial writes to NT_ARM_SVE would not be idempotent, and if a first write preserved the low 128 bits, a subsequent (potentially identical) partial write would discard the low 128 bits. When support for the NT_ARM_SSVE regset was added in commit: e12310a ("arm64/sme: Implement ptrace support for streaming mode SVE registers") ... the above behaviour was retained for writes to the NT_ARM_SVE regset, though writes to the NT_ARM_SSVE would always zero the SVE registers and would not inherit FPSIMD register state. This happened as fpsimd_sync_to_sve() only copied the FPSIMD regs when TIF_SVE was clear and PSTATE.SM==0. Subsequently, when FPSIMD/SVE state tracking was changed across commits: baa8515 ("arm64/fpsimd: Track the saved FPSIMD state type separately to TIF_SVE") a0136be (arm64/fpsimd: Load FP state based on recorded data type") bbc6172 ("arm64/fpsimd: SME no longer requires SVE register state") 8c845e2 ("arm64/sve: Leave SVE enabled on syscall if we don't context switch") ... there was no corresponding update to the ptrace code, nor to fpsimd_sync_to_sve(), which stil considers TIF_SVE and PSTATE.SM rather than the saved fp_type. The saved state can be in the FPSIMD format regardless of whether TIF_SVE is set or clear, and the saved type can change non-deterministically during syscalls. Consequently a subsequent partial write to the NT_ARM_SVE or NT_ARM_SSVE regsets may non-deterministically preserve the FPSIMD state, and may migrate this state between streaming and non-streaming modes. Clean this up by never attempting to preserve ANY state when writing an SVE payload to the NT_ARM_SVE/NT_ARM_SSVE regsets, zeroing all relevant state including FPSR and FPCR. This simplifies the code, makes the behaviour deterministic, and avoids migrating state between streaming and non-streaming modes. As above, I do not believe this should adversely affect existing userspace applications. At the same time, remove fpsimd_sync_to_sve(). It is no longer used, doesn't do what its documentation implies, and gets in the way of other cleanups and fixes. Fixes: 43d4da2 ("arm64/sve: ptrace and ELF coredump support") Signed-off-by: Mark Rutland <mark.rutland@arm.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: David Spickett <david.spickett@arm.com> Cc: Luis Machado <luis.machado@arm.com> Cc: Marc Zyngier <maz@kernel.org> Cc: Mark Brown <broonie@kernel.org> Cc: Will Deacon <will@kernel.org> Link: https://lore.kernel.org/r/20250508132644.1395904-6-mark.rutland@arm.com Signed-off-by: Will Deacon <will@kernel.org>
1 parent be625d8 commit 316283f

3 files changed

Lines changed: 9 additions & 33 deletions

File tree

arch/arm64/include/asm/fpsimd.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ struct vl_info {
198198

199199
extern void sve_alloc(struct task_struct *task, bool flush);
200200
extern void fpsimd_release_task(struct task_struct *task);
201-
extern void fpsimd_sync_to_sve(struct task_struct *task);
202201
extern void sve_sync_to_fpsimd(struct task_struct *task);
203202
extern void sve_sync_from_fpsimd_zeropad(struct task_struct *task);
204203

arch/arm64/kernel/fpsimd.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -759,21 +759,6 @@ void sve_alloc(struct task_struct *task, bool flush)
759759
kzalloc(sve_state_size(task), GFP_KERNEL);
760760
}
761761

762-
/*
763-
* Ensure that task->thread.sve_state is up to date with respect to
764-
* the user task, irrespective of when SVE is in use or not.
765-
*
766-
* This should only be called by ptrace. task must be non-runnable.
767-
* task->thread.sve_state must point to at least sve_state_size(task)
768-
* bytes of allocated kernel memory.
769-
*/
770-
void fpsimd_sync_to_sve(struct task_struct *task)
771-
{
772-
if (!test_tsk_thread_flag(task, TIF_SVE) &&
773-
!thread_sm_enabled(&task->thread))
774-
fpsimd_to_sve(task);
775-
}
776-
777762
/*
778763
* Ensure that task->thread.uw.fpsimd_state is up to date with respect to
779764
* the user task, irrespective of whether SVE is in use or not.

arch/arm64/kernel/ptrace.c

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -910,8 +910,6 @@ static int sve_set_common(struct task_struct *target,
910910

911911
/* Enter/exit streaming mode */
912912
if (system_supports_sme()) {
913-
u64 old_svcr = target->thread.svcr;
914-
915913
switch (type) {
916914
case ARM64_VEC_SVE:
917915
target->thread.svcr &= ~SVCR_SM_MASK;
@@ -931,23 +929,20 @@ static int sve_set_common(struct task_struct *target,
931929
ret = -EINVAL;
932930
goto out;
933931
}
934-
935-
/*
936-
* If we switched then invalidate any existing SVE
937-
* state and ensure there's storage.
938-
*/
939-
if (target->thread.svcr != old_svcr)
940-
sve_alloc(target, true);
941932
}
942933

934+
/* Always zero V regs, FPSR, and FPCR */
935+
memset(&current->thread.uw.fpsimd_state, 0,
936+
sizeof(current->thread.uw.fpsimd_state));
937+
943938
/* Registers: FPSIMD-only case */
944939

945940
BUILD_BUG_ON(SVE_PT_FPSIMD_OFFSET != sizeof(header));
946941
if ((header.flags & SVE_PT_REGS_MASK) == SVE_PT_REGS_FPSIMD) {
947-
ret = __fpr_set(target, regset, pos, count, kbuf, ubuf,
948-
SVE_PT_FPSIMD_OFFSET);
949942
clear_tsk_thread_flag(target, TIF_SVE);
950943
target->thread.fp_type = FP_STATE_FPSIMD;
944+
ret = __fpr_set(target, regset, pos, count, kbuf, ubuf,
945+
SVE_PT_FPSIMD_OFFSET);
951946
goto out;
952947
}
953948

@@ -966,6 +961,7 @@ static int sve_set_common(struct task_struct *target,
966961
goto out;
967962
}
968963

964+
/* Always zero SVE state */
969965
sve_alloc(target, true);
970966
if (!target->thread.sve_state) {
971967
ret = -ENOMEM;
@@ -975,13 +971,9 @@ static int sve_set_common(struct task_struct *target,
975971
}
976972

977973
/*
978-
* Ensure target->thread.sve_state is up to date with target's
979-
* FPSIMD regs, so that a short copyin leaves trailing
980-
* registers unmodified. Only enable SVE if we are
981-
* configuring normal SVE, a system with streaming SVE may not
982-
* have normal SVE.
974+
* Only enable SVE if we are configuring normal SVE, a system with
975+
* streaming SVE may not have normal SVE.
983976
*/
984-
fpsimd_sync_to_sve(target);
985977
if (type == ARM64_VEC_SVE)
986978
set_tsk_thread_flag(target, TIF_SVE);
987979
target->thread.fp_type = FP_STATE_SVE;

0 commit comments

Comments
 (0)