Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

arm64/sve: Add prctl controls for userspace vector length management

This patch adds two arm64-specific prctls, to permit userspace to
control its vector length:

* PR_SVE_SET_VL: set the thread's SVE vector length and vector
length inheritance mode.

* PR_SVE_GET_VL: get the same information.

Although these prctls resemble instruction set features in the SVE
architecture, they provide additional control: the vector length
inheritance mode is Linux-specific and nothing to do with the
architecture, and the architecture does not permit EL0 to set its
own vector length directly. Both can be used in portable tools
without requiring the use of SVE instructions.

Signed-off-by: Dave Martin <Dave.Martin@arm.com>
Reviewed-by: Catalin Marinas <catalin.marinas@arm.com>
Cc: Alex Bennée <alex.bennee@linaro.org>
[will: Fixed up prctl constants to avoid clash with PDEATHSIG]
Signed-off-by: Will Deacon <will.deacon@arm.com>

authored by

Dave Martin and committed by
Will Deacon
2d2123bc 43d4da2c

+84
+14
arch/arm64/include/asm/fpsimd.h
··· 17 17 #define __ASM_FP_H 18 18 19 19 #include <asm/ptrace.h> 20 + #include <asm/errno.h> 20 21 21 22 #ifndef __ASSEMBLY__ 22 23 ··· 99 98 extern int sve_set_vector_length(struct task_struct *task, 100 99 unsigned long vl, unsigned long flags); 101 100 101 + extern int sve_set_current_vl(unsigned long arg); 102 + extern int sve_get_current_vl(void); 103 + 102 104 /* 103 105 * Probing and setup functions. 104 106 * Calls to these functions must be serialised with one another. ··· 117 113 static inline void fpsimd_release_task(struct task_struct *task) { } 118 114 static inline void sve_sync_to_fpsimd(struct task_struct *task) { } 119 115 static inline void sve_sync_from_fpsimd_zeropad(struct task_struct *task) { } 116 + 117 + static inline int sve_set_current_vl(unsigned long arg) 118 + { 119 + return -EINVAL; 120 + } 121 + 122 + static inline int sve_get_current_vl(void) 123 + { 124 + return -EINVAL; 125 + } 120 126 121 127 static inline void sve_init_vq_map(void) { } 122 128 static inline void sve_update_vq_map(void) { }
+4
arch/arm64/include/asm/processor.h
··· 217 217 int cpu_enable_pan(void *__unused); 218 218 int cpu_enable_cache_maint_trap(void *__unused); 219 219 220 + /* Userspace interface for PR_SVE_{SET,GET}_VL prctl()s: */ 221 + #define SVE_SET_VL(arg) sve_set_current_vl(arg) 222 + #define SVE_GET_VL() sve_get_current_vl() 223 + 220 224 #endif /* __ASSEMBLY__ */ 221 225 #endif /* __ASM_PROCESSOR_H */
+50
arch/arm64/kernel/fpsimd.c
··· 29 29 #include <linux/irqflags.h> 30 30 #include <linux/init.h> 31 31 #include <linux/percpu.h> 32 + #include <linux/prctl.h> 32 33 #include <linux/preempt.h> 33 34 #include <linux/prctl.h> 34 35 #include <linux/ptrace.h> ··· 557 556 clear_tsk_thread_flag(task, TIF_SVE_VL_INHERIT); 558 557 559 558 return 0; 559 + } 560 + 561 + /* 562 + * Encode the current vector length and flags for return. 563 + * This is only required for prctl(): ptrace has separate fields 564 + * 565 + * flags are as for sve_set_vector_length(). 566 + */ 567 + static int sve_prctl_status(unsigned long flags) 568 + { 569 + int ret; 570 + 571 + if (flags & PR_SVE_SET_VL_ONEXEC) 572 + ret = current->thread.sve_vl_onexec; 573 + else 574 + ret = current->thread.sve_vl; 575 + 576 + if (test_thread_flag(TIF_SVE_VL_INHERIT)) 577 + ret |= PR_SVE_VL_INHERIT; 578 + 579 + return ret; 580 + } 581 + 582 + /* PR_SVE_SET_VL */ 583 + int sve_set_current_vl(unsigned long arg) 584 + { 585 + unsigned long vl, flags; 586 + int ret; 587 + 588 + vl = arg & PR_SVE_VL_LEN_MASK; 589 + flags = arg & ~vl; 590 + 591 + if (!system_supports_sve()) 592 + return -EINVAL; 593 + 594 + ret = sve_set_vector_length(current, vl, flags); 595 + if (ret) 596 + return ret; 597 + 598 + return sve_prctl_status(flags); 599 + } 600 + 601 + /* PR_SVE_GET_VL */ 602 + int sve_get_current_vl(void) 603 + { 604 + if (!system_supports_sve()) 605 + return -EINVAL; 606 + 607 + return sve_prctl_status(0); 560 608 } 561 609 562 610 /*
+4
include/uapi/linux/prctl.h
··· 198 198 # define PR_CAP_AMBIENT_CLEAR_ALL 4 199 199 200 200 /* arm64 Scalable Vector Extension controls */ 201 + /* Flag values must be kept in sync with ptrace NT_ARM_SVE interface */ 202 + #define PR_SVE_SET_VL 50 /* set task vector length */ 201 203 # define PR_SVE_SET_VL_ONEXEC (1 << 18) /* defer effect until exec */ 204 + #define PR_SVE_GET_VL 51 /* get task vector length */ 205 + /* Bits common to PR_SVE_SET_VL and PR_SVE_GET_VL */ 202 206 # define PR_SVE_VL_LEN_MASK 0xffff 203 207 # define PR_SVE_VL_INHERIT (1 << 17) /* inherit across exec */ 204 208
+12
kernel/sys.c
··· 110 110 #ifndef SET_FP_MODE 111 111 # define SET_FP_MODE(a,b) (-EINVAL) 112 112 #endif 113 + #ifndef SVE_SET_VL 114 + # define SVE_SET_VL(a) (-EINVAL) 115 + #endif 116 + #ifndef SVE_GET_VL 117 + # define SVE_GET_VL() (-EINVAL) 118 + #endif 113 119 114 120 /* 115 121 * this is where the system-wide overflow UID and GID are defined, for ··· 2390 2384 break; 2391 2385 case PR_GET_FP_MODE: 2392 2386 error = GET_FP_MODE(me); 2387 + break; 2388 + case PR_SVE_SET_VL: 2389 + error = SVE_SET_VL(arg2); 2390 + break; 2391 + case PR_SVE_GET_VL: 2392 + error = SVE_GET_VL(); 2393 2393 break; 2394 2394 default: 2395 2395 error = -EINVAL;