x86: Support Intel Advanced Matrix Extensions (part 4)
Subsystems (2)
cover x86: Support Intel Advanced Matrix Extensions (part 4)
This is the last part of the effort to support AMX. This series follows the KVM part:
https://lore.kernel.org/lkml/[email protected]/
With AMX the FPU register state buffer which is part of task_struct::thread::fpu is not going to be extended unconditionally for all tasks on an AMX enabled system as that would waste minimum 8K per task.
AMX provides a mechanism to trap on first use. That trap will be utilized to allocate a larger register state buffer when the task (process) has permissions to use it. The default buffer task_struct will only carry states up to AVX512.
The cost of XFD switching only matters for an AMX-enabled system. With the cleanup of the KVM FPU handling, host-side XFD/AMX is completely independent of guest-side XFD/AMX.
The per-task feature and size information helps to support dynamic features organically compared to the old versions.
Each task has a unique sigframe length with dynamic features. sigaltstack() has a new size checker to support a per-task sigframe size.
This version also fixes the syscall implementation and the XFD state switching on hot paths.
Here is a summary of patches:
- At first, add arch-specific sigaltstack size check
- Establish a set of new syscalls to control dynamic XSTATE components
- Update infrastructure to prepare dynamic features
- Support XFD state and switching, and add buffer reallocation helpers
- Finally, enable AMX with XFD #NM handling
This series is based on:
git://git.kernel.org/pub/scm/linux/kernel/git/tglx/devel.git x86/fpu-3-kvm
and a revision of the version in the tglx tree -- some bug fixes, polish, and addressing Dave Hansen's feedback (noted on each patch):
git://git.kernel.org/pub/scm/linux/kernel/git/tglx/devel.git x86/fpu
This comes with a large selftest suite which is going to be posted separately. A preview along with the full series is available here:
git://github.com/intel/amx-linux.git amx
Thanks,
Chang
Chang S. Bae (15):
x86/fpu/xstate: Provide xstate_calculate_size() x86/arch_prctl: Add controls for dynamic XSTATE components x86/fpu/signal: Prepare for variable sigframe length x86/fpu: Reset permission and fpstate on exec() x86/cpufeatures: Add eXtended Feature Disabling (XFD) feature bit x86/msr-index: Add MSRs for XFD
x86/fpu: Add XFD state to fpstate
x86/fpu: Update XFD state where required x86/fpu/xstate: Add XFD #NM handler
x86/fpu/xstate: Add fpstate_realloc()/free() x86/fpu/xstate: Prepare XSAVE feature table for gaps in state
component numbers
x86/fpu/amx: Define AMX state components and have it used for
boot-time checks
x86/fpu: Calculate the default sizes independently x86/fpu: Add XFD handling for dynamic states x86/fpu/amx: Enable the AMX feature in 64-bit mode
Thomas Gleixner (8):
signal: Add an optional check for altstack size x86/signal: Implement sigaltstack size validation x86/fpu: Add members to struct fpu to cache permission information x86/fpu: Add fpu_state_config::legacy_features
x86/fpu: Add basic helpers for dynamically enabled features x86/signal: Use fpu::__state_user_size for sigalt stack validation x86/fpu: Prepare fpu_clone() for dynamically enabled features x86/fpu: Add sanity checks for XFD
.../admin-guide/kernel-parameters.txt | 9 + arch/Kconfig | 3 + arch/x86/Kconfig | 17 + arch/x86/include/asm/cpufeatures.h | 2 + arch/x86/include/asm/fpu/api.h | 11 + arch/x86/include/asm/fpu/sched.h | 2 +- arch/x86/include/asm/fpu/types.h | 88 +++ arch/x86/include/asm/fpu/xstate.h | 21 +- arch/x86/include/asm/msr-index.h | 2 + arch/x86/include/asm/proto.h | 2 +- arch/x86/include/uapi/asm/prctl.h | 4 + arch/x86/kernel/cpu/cpuid-deps.c | 2 + arch/x86/kernel/fpu/context.h | 2 + arch/x86/kernel/fpu/core.c | 93 ++- arch/x86/kernel/fpu/init.c | 9 +- arch/x86/kernel/fpu/internal.h | 3 - arch/x86/kernel/fpu/signal.c | 70 +-- arch/x86/kernel/fpu/xstate.c | 566 ++++++++++++++++-- arch/x86/kernel/fpu/xstate.h | 61 +- arch/x86/kernel/process.c | 21 +- arch/x86/kernel/signal.c | 64 +- arch/x86/kernel/traps.c | 38 ++ include/linux/signal.h | 6 + kernel/signal.c | 35 +- 24 files changed, 1008 insertions(+), 123 deletions(-)
base-commit: 672f086122b67b299aa89c6b5f647c20ef84157f --
2.17.1
PATCH 1/23 signal: Add an optional check for altstack size
From: Thomas Gleixner <[email protected]>
The upcoming support for dynamically enabled FPU features on x86 requires an architecture specific sanity check and serialization of the store to task::sas_ss_size. The check is required to ensure that:
- Enabling of a dynamic feature, which changes the sigframe size fits into an enabled sigaltstack
- Installing a too small sigaltstack after a dynamic feature has been added is not possible.
It needs serialization to prevent race conditions of all sorts in the feature enable code as that has to walk the thread list of the process.
Add the infrastructure in form of a config option and provide empty stubs for architectures which do not need that.
--- arch/Kconfig | 3 +++ include/linux/signal.h | 6 ++++++ kernel/signal.c | 35 +++++++++++++++++++++++++++++------ 3 files changed, 38 insertions(+), 6 deletions(-)
arch/Kconfig +3 -0
@@ -1288,6 +1288,9 @@ config ARCH_HAS_ELFCORE_COMPAT config ARCH_HAS_PARANOID_L1D_FLUSH bool +config DYNAMIC_SIGFRAME + bool + source "kernel/gcov/Kconfig" source "scripts/gcc-plugins/Kconfig"
include/linux/signal.h +6 -0
@@ -464,6 +464,12 @@ int __save_altstack(stack_t __user *, unsigned long);unsafe_put_user (t->sas_ss_size, &__uss->ss_size, label); \ }while (0 ); +#ifdef CONFIG_DYNAMIC_SIGFRAME +bool sigaltstack_size_valid (size_t ss_size); +#else +static inline bool sigaltstack_size_valid (size_t size) {return true; } +#endif /* !CONFIG_DYNAMIC_SIGFRAME */ +#ifdef CONFIG_PROC_FS struct seq_file ;extern void render_sigset_t (struct seq_file *,const char *,sigset_t *);
kernel/signal.c +29 -6
@@ -4151,11 +4151,29 @@ int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)return 0 ; } +#ifdef CONFIG_DYNAMIC_SIGFRAME +static inline void sigaltstack_lock (void ) + __acquires(¤t->sighand->siglock) +{ +spin_lock_irq (¤t->sighand->siglock); +} + +static inline void sigaltstack_unlock (void ) + __releases(¤t->sighand->siglock) +{ +spin_unlock_irq (¤t->sighand->siglock); +} +#else +static inline void sigaltstack_lock (void ) { } +static inline void sigaltstack_unlock (void ) { } +#endif +static int do_sigaltstack (const stack_t *ss,stack_t *oss,unsigned long sp,size_t min_ss_size) {struct task_struct *t = current; +int ret =0 ;if (oss) {memset (oss,0 ,sizeof (stack_t));
@@ -4179,19 +4197,24 @@ do_sigaltstack (const stack_t *ss, stack_t *oss, unsigned long sp, ss_mode != 0))return -EINVAL ; +sigaltstack_lock ();if (ss_mode ==SS_DISABLE ) { ss_size =0 ; ss_sp =NULL ; }else {if (unlikely (ss_size < min_ss_size)) -return -ENOMEM ; + ret = -ENOMEM ; +if (!sigaltstack_size_valid (ss_size)) + ret = -ENOMEM ; } - - t->sas_ss_sp = (unsigned long ) ss_sp; - t->sas_ss_size = ss_size; - t->sas_ss_flags = ss_flags; +if (!ret) { + t->sas_ss_sp = (unsigned long ) ss_sp; + t->sas_ss_size = ss_size; + t->sas_ss_flags = ss_flags; + } +sigaltstack_unlock (); } -return 0 ; +return ret; } SYSCALL_DEFINE2(sigaltstack,const stack_t __user *,uss, stack_t __user *,uoss)
Signature · 1 line
2.17.1
Re: [PATCH 01/23] signal: Add an optional check for altstack size
Quoted 2 lines · Chang Seok
Cc Oleg Nesterov <[email protected]>
Hi Oleg,
Apologies on my typo -- s/ole/oleg/
The original message can be found:
https://lore.kernel.org/lkml/[email protected]/
Thanks,
Chang
Re: [PATCH 01/23] signal: Add an optional check for altstack size
In reply to Chang S. BaeQuoted 9 lines · Chang S. Bae
From: Thomas Gleixner <[email protected]>
The upcoming support for dynamically enabled FPU features on x86 requires an architecture specific sanity check and serialization of the store to
task::sas_ss_size. The check is required to ensure that:- Enabling of a dynamic feature, which changes the sigframe size fits into an enabled sigaltstack
Quoted 8 lines
- Installing a too small sigaltstack after a dynamic feature has been added is not possible.
It needs serialization to prevent race conditions of all sorts in the feature enable code as that has to walk the thread list of the process.
Add the infrastructure in form of a config option and provide empty stubs for architectures which do not need that.
Last I looked Al Viro was doing a lot of work on sigframes, adding him to the cc.
That said description in the patch is very sorely lacking.
First the reason for the new locking is not really explained, it talks about serialization but it does not talk about what is protected. Especially given that the signal delivery code already has to check if the signal frame on the stack when pushing a new signal I don't understand what the code is trying to prevent.
Second the reason that 2K is not enough mentioned. The current value of MINSIGSTKSZ on x86 is 2K.
Third the issues with modifying the userspace ABI are not discussed. Frankly that is a pretty big consideration. MINSIGSTKSZ is exported to userspace and userspace fundamentally needs to allocate the alternate signal frame.
Forth the sigframe size on x86 is already dynamic and is already computed by get_sigframe_size.
So can we please please please have a better description of what is going on and the trade offs that are being made.
Thank you,
Eric
Quoted 106 lines
Signed-off-by Thomas Gleixner <[email protected]>Signed-off-by Chang S. Bae <[email protected]>Cc Oleg Nesterov <[email protected]>Cc Eric W. Biederman <[email protected]>--- arch/Kconfig | 3 +++ include/linux/signal.h | 6 ++++++ kernel/signal.c | 35 +++++++++++++++++++++++++++++------ 3 files changed, 38 insertions(+), 6 deletions(-)arch/Kconfig +3
-0@@ -1288,6 +1288,9 @@ config ARCH_HAS_ELFCORE_COMPAT config ARCH_HAS_PARANOID_L1D_FLUSH bool +config DYNAMIC_SIGFRAME + bool + source "kernel/gcov/Kconfig" source "scripts/gcc-plugins/Kconfig"include/linux/signal.h +6
-0@@ -464,6 +464,12 @@ int __save_altstack(stack_t __user *, unsigned long);unsafe_put_user (t->sas_ss_size, &__uss->ss_size, label); \ }while (0 ); +#ifdef CONFIG_DYNAMIC_SIGFRAME +bool sigaltstack_size_valid (size_t ss_size); +#else +static inline bool sigaltstack_size_valid (size_t size) {return true; } +#endif /* !CONFIG_DYNAMIC_SIGFRAME */ +#ifdef CONFIG_PROC_FS struct seq_file ;extern void render_sigset_t (struct seq_file *,const char *,sigset_t *);kernel/signal.c +29
-6@@ -4151,11 +4151,29 @@ int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)return 0 ; } +#ifdef CONFIG_DYNAMIC_SIGFRAME +static inline void sigaltstack_lock (void ) + __acquires(¤t->sighand->siglock) +{ +spin_lock_irq (¤t->sighand->siglock); +} + +static inline void sigaltstack_unlock (void ) + __releases(¤t->sighand->siglock) +{ +spin_unlock_irq (¤t->sighand->siglock); +} +#else +static inline void sigaltstack_lock (void ) { } +static inline void sigaltstack_unlock (void ) { } +#endif +static int do_sigaltstack (const stack_t *ss,stack_t *oss,unsigned long sp,size_t min_ss_size) {struct task_struct *t = current; +int ret =0 ;if (oss) {memset (oss,0 ,sizeof (stack_t));@@ -4179,19 +4197,24 @@ do_sigaltstack (const stack_t *ss, stack_t *oss, unsigned long sp, ss_mode != 0))return -EINVAL ; +sigaltstack_lock ();if (ss_mode ==SS_DISABLE ) { ss_size =0 ; ss_sp =NULL ; }else {if (unlikely (ss_size < min_ss_size)) -return -ENOMEM ; + ret = -ENOMEM ; +if (!sigaltstack_size_valid (ss_size)) + ret = -ENOMEM ; } - - t->sas_ss_sp = (unsigned long ) ss_sp; - t->sas_ss_size = ss_size; - t->sas_ss_flags = ss_flags; +if (!ret) { + t->sas_ss_sp = (unsigned long ) ss_sp; + t->sas_ss_size = ss_size; + t->sas_ss_flags = ss_flags; + } +sigaltstack_unlock (); } -return 0 ; +return ret; } SYSCALL_DEFINE2(sigaltstack,const stack_t __user *,uss, stack_t __user *,uoss)
Re: [PATCH 01/23] signal: Add an optional check for altstack size
Hi Eric,
Quoted 18 lines · Eric W. Biederman
Quoted 15 lines · Chang S. Bae
From: Thomas Gleixner <[email protected]>
The upcoming support for dynamically enabled FPU features on x86 requires an architecture specific sanity check and serialization of the store to
task::sas_ss_size. The check is required to ensure that:- Enabling of a dynamic feature, which changes the sigframe size fits into an enabled sigaltstack
- Installing a too small sigaltstack after a dynamic feature has been added is not possible.
It needs serialization to prevent race conditions of all sorts in the feature enable code as that has to walk the thread list of the process.
Quoted 8 lines
Quoted 2 lines
Add the infrastructure in form of a config option and provide empty stubs for architectures which do not need that.
Last I looked Al Viro was doing a lot of work on sigframes, adding him to the cc.
That said description in the patch is very sorely lacking.
Will update the changelog though, let me clarify what you pointed out here at first.
Quoted 5 lines
First the reason for the new locking is not really explained, it talks about serialization but it does not talk about what is protected. Especially given that the signal delivery code already has to check if the signal frame on the stack when pushing a new signal I don't understand what the code is trying to prevent.
Later in this series, a new x86-specific prctl() is introduced so that an application may ask permission to use dynamic states. It means the sigframe size is also dynamic. Besides that, in the implemented mechanism [1]:
"Task asks for permission for a facility and kernel checks whether that's
supported. If supported it does:
...
3) Validate that no task has a sigaltstack installed
which is smaller than the resulting sigframe size
...
"
The new sigframe size out of new permission is validated via each thread’s altstack size. Accessing each task::sas_ss_size can be racy with sigaltstack(). So, it is protected by sighand lock. (3) looks like this in a nutshell:
spin_lock_irq(¤t->sighand->siglock);
...
int validate_sigaltstack(unsigned int usize) {
struct task_struct *thread, *leader = current->group_leader;
unsigned long framesize = get_sigframe_size();
...
for_each_thread(leader, thread) {
if (thread->sas_ss_size && thread->sas_ss_size < framesize)
return -ENOSPC;
}
...
}
...
spin_unlock_irq(¤t->sighand->siglock);Quoted 2 lines
Second the reason that 2K is not enough mentioned. The current value of MINSIGSTKSZ on x86 is 2K.
The MINSIGSTKSZ constant is 2K but this is already too small in x86 with AVX512. Increasing this might break binaries that were already compiled with the old value. They used to work because their sigalstack was never used.
Quoted 4 lines
Third the issues with modifying the userspace ABI are not discussed. Frankly that is a pretty big consideration. MINSIGSTKSZ is exported to userspace and userspace fundamentally needs to allocate the alternate signal frame.
Now, there is an effort to redefine MINSIGSTKSZ as a dynamic value. The latest glibc v2.34 has this [2]:
"Add _SC_MINSIGSTKSZ and _SC_SIGSTKSZ. When _DYNAMIC_STACK_SIZE_SOURCE
or _GNU_SOURCE are defined, MINSIGSTKSZ and SIGSTKSZ are no longer constant on Linux. MINSIGSTKSZ is redefined to sysconf(_SC_MINSIGSTKSZ) and SIGSTKSZ is redefined to sysconf (_SC_SIGSTKSZ). This supports dynamic sized register sets for modern architectural features like Arm SVE."
Quoted 2 lines
Forth the sigframe size on x86 is already dynamic and is already computed by
get_sigframe_size.
Also, the x86 kernel supports exporting it via the AT_MINSIGSTKSZ aux vector
[4] since v5.14. I had developed the code with H.J. who authored the glibc code [3].
Quoted 2 lines
So can we please please please have a better description of what is going on and the trade offs that are being made.
Okay, but I think the dynamic MINSIGSTKSZ is not what this patch does, no? Maybe the task::sas_ss_size part needs more clarity though.
Thanks,
Chang
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://sourceware.org/pipermail/libc-alpha/2021-August/129718.html
[3] https://sourceware.org/git/?p=glibc.git;a=commit;h=6c57d320484988e87e446e2e60ce42816bf51d53
[4] https://lore.kernel.org/lkml/[email protected]/
Re: [PATCH 01/23] signal: Add an optional check for altstack size
In reply to Eric W. BiedermanHi Eric,
Quoted 5 lines
First the reason for the new locking is not really explained, it talks about serialization but it does not talk about what is protected. Especially given that the signal delivery code already has to check if the signal frame on the stack when pushing a new signal I don't understand what the code is trying to prevent.
Yeah, the basic idea is to ensure that there are no races between do_sigaltstack() and enabling the new "dynamic features".
Quoted 4 lines
Third the issues with modifying the userspace ABI are not discussed. Frankly that is a pretty big consideration. MINSIGSTKSZ is exported to userspace and userspace fundamentally needs to allocate the alternate signal frame.
Agreed. This is what we settled on to both respect old programs that have MINSIGSTKSZ=2k compiled in *and* support new ones that need bigger stacks.
Quoted 2 lines
Forth the sigframe size on x86 is already dynamic and is already computed by
get_sigframe_size.
Right. This is much more about making the altstack size checks dynamic than the sigframe size.
Quoted 2 lines
So can we please please please have a better description of what is going on and the trade offs that are being made.
I've got a suggested replacement changelog below. Please let me know if it clarifies things, or leaves anything out.
How would this be a better subject?
signal: Add optional dynamic altstack size checks
And this for a changelog?
--
New x86 FPU features will be very large, requiring ~10k of stack in signal handlers. These new features require a new approach called "dynamic features".
The kernel currently tries to ensure that altstacks are reasonably sized. Right now, on x86, sys_sigaltstack() requires a size of >=2k. However, that 2k is a constant. Simply raising that 2k requirement to
Quoted 1 line
10k for the new features would break existing apps which have a
compiled-in size of 2k.
Instead of universally enforcing a larger stack, prohibit a process from using dynamic features without properly-sized altstacks. This must be enforced in two places:
* A dynamic feature can not be enabled without an large-enough altstack for each process thread.
* Once a dynamic feature is enabled, any request to install a too-small altstack will be rejected
The dynamic feature enabling code must examine each thread in a process to ensure that the altstacks are large enough. Add a new lock (sigaltstack_lock()) to ensure that threads can not race and change their altstack after being examined.
Add the infrastructure in form of a config option and provide empty stubs for architectures which do not need dynamic altstack size checks.
This implementation will be fleshed out for x86 in:
x86/arch_prctl: Add controls for dynamic XSTATE components
[tip: x86/fpu] signal: Add an optional check for altstack size
In reply to Chang S. BaeThe following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 1bdda24c4af64cd2d65dec5192ab624c5fee7ca0 Gitweb: https://git.kernel.org/tip/1bdda24c4af64cd2d65dec5192ab624c5fee7ca0 Author: Thomas Gleixner <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:05 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:15:12 +02:00
signal: Add an optional check for altstack size
New x86 FPU features will be very large, requiring ~10k of stack in signal handlers. These new features require a new approach called "dynamic features".
The kernel currently tries to ensure that altstacks are reasonably sized. Right now, on x86, sys_sigaltstack() requires a size of >=2k. However, that 2k is a constant. Simply raising that 2k requirement to >10k for the new features would break existing apps which have a compiled-in size of 2k.
Instead of universally enforcing a larger stack, prohibit a process from using dynamic features without properly-sized altstacks. This must be enforced in two places:
* A dynamic feature can not be enabled without an large-enough altstack for each process thread.
* Once a dynamic feature is enabled, any request to install a too-small altstack will be rejected
The dynamic feature enabling code must examine each thread in a process to ensure that the altstacks are large enough. Add a new lock (sigaltstack_lock()) to ensure that threads can not race and change their altstack after being examined.
Add the infrastructure in form of a config option and provide empty stubs for architectures which do not need dynamic altstack size checks.
This implementation will be fleshed out for x86 in a future patch called
x86/arch_prctl: Add controls for dynamic XSTATE components
[dhansen: commit message. ]
--- arch/Kconfig | 3 +++ include/linux/signal.h | 6 ++++++ kernel/signal.c | 35 +++++++++++++++++++++++++++++------ 3 files changed, 38 insertions(+), 6 deletions(-)
arch/Kconfig +3 -0
@@ -1288,6 +1288,9 @@ config ARCH_HAS_ELFCORE_COMPAT config ARCH_HAS_PARANOID_L1D_FLUSH bool +config DYNAMIC_SIGFRAME + bool + source "kernel/gcov/Kconfig" source "scripts/gcc-plugins/Kconfig"
include/linux/signal.h +6 -0
@@ -464,6 +464,12 @@ int __save_altstack(stack_t __user *, unsigned long);unsafe_put_user (t->sas_ss_size, &__uss->ss_size, label); \ }while (0 ); +#ifdef CONFIG_DYNAMIC_SIGFRAME +bool sigaltstack_size_valid (size_t ss_size); +#else +static inline bool sigaltstack_size_valid (size_t size) {return true; } +#endif /* !CONFIG_DYNAMIC_SIGFRAME */ +#ifdef CONFIG_PROC_FS struct seq_file ;extern void render_sigset_t (struct seq_file *,const char *,sigset_t *);
kernel/signal.c +29 -6
@@ -4151,11 +4151,29 @@ int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)return 0 ; } +#ifdef CONFIG_DYNAMIC_SIGFRAME +static inline void sigaltstack_lock (void ) + __acquires(¤t->sighand->siglock) +{ +spin_lock_irq (¤t->sighand->siglock); +} + +static inline void sigaltstack_unlock (void ) + __releases(¤t->sighand->siglock) +{ +spin_unlock_irq (¤t->sighand->siglock); +} +#else +static inline void sigaltstack_lock (void ) { } +static inline void sigaltstack_unlock (void ) { } +#endif +static int do_sigaltstack (const stack_t *ss,stack_t *oss,unsigned long sp,size_t min_ss_size) {struct task_struct *t = current; +int ret =0 ;if (oss) {memset (oss,0 ,sizeof (stack_t));
@@ -4179,19 +4197,24 @@ do_sigaltstack (const stack_t *ss, stack_t *oss, unsigned long sp, ss_mode != 0))return -EINVAL ; +sigaltstack_lock ();if (ss_mode ==SS_DISABLE ) { ss_size =0 ; ss_sp =NULL ; }else {if (unlikely (ss_size < min_ss_size)) -return -ENOMEM ; + ret = -ENOMEM ; +if (!sigaltstack_size_valid (ss_size)) + ret = -ENOMEM ; } - - t->sas_ss_sp = (unsigned long ) ss_sp; - t->sas_ss_size = ss_size; - t->sas_ss_flags = ss_flags; +if (!ret) { + t->sas_ss_sp = (unsigned long ) ss_sp; + t->sas_ss_size = ss_size; + t->sas_ss_flags = ss_flags; + } +sigaltstack_unlock (); } -return 0 ; +return ret; } SYSCALL_DEFINE2(sigaltstack,const stack_t __user *,uss, stack_t __user *,uoss)
PATCH 2/23 x86/signal: Implement sigaltstack size validation
In reply to Chang S. BaeFrom: Thomas Gleixner <[email protected]>
For historical reasons MINSIGSTKSZ is a constant which became already too small with AVX512 support.
Add a mechanism to enforce strict checking of the sigaltstack size against the real size of the FPU frame.
The strict check can be enabled via a config option and can also be controlled via the kernel command line option 'strict_sas_size' independent of the config switch.
Enabling it might break existing applications which allocate a too small sigaltstack but 'work' because they never get a signal delivered. Though it can be handy to filter out binaries which are not yet aware of AT_MINSIGSTKSZ.
Also the upcoming support for dynamically enabled FPU features requires a strict sanity check to ensure that:
- Enabling of a dynamic feature, which changes the sigframe size fits into an enabled sigaltstack
- Installing a too small sigaltstack after a dynamic feature has been added is not possible.
Implement the base check which is controlled by config and command line options.
--- .../admin-guide/kernel-parameters.txt | 9 +++++ arch/x86/Kconfig | 17 +++++++++ arch/x86/kernel/signal.c | 35 +++++++++++++++++++ 3 files changed, 61 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +9 -0
@@ -5497,6 +5497,15 @@ stifb= [HW] Format: bpp:<bpp1>[:<bpp2>[:<bpp3>...]] + strict_sas_size= + [X86] + Format: <bool> + Enable or disable strict sigaltstack size checks + against the required signal frame size which + depends on the supported FPU features. This can + be used to filter out binaries which have + not yet been made aware of AT_MINSIGSTKSZ. + sunrpc.min_resvport= sunrpc.max_resvport= [NFS,SUNRPC]
arch/x86/Kconfig +17 -0
@@ -125,6 +125,7 @@ config X86 select CLOCKSOURCE_VALIDATE_LAST_CYCLE select CLOCKSOURCE_WATCHDOG select DCACHE_WORD_ACCESS + select DYNAMIC_SIGFRAME select EDAC_ATOMIC_SCRUB select EDAC_SUPPORT select GENERIC_CLOCKEVENTS_BROADCAST if X86_64 || (X86_32 && X86_LOCAL_APIC)
@@ -2389,6 +2390,22 @@ config MODIFY_LDT_SYSCALL Saying 'N' here may make sense for embedded or server kernels. +config STRICT_SIGALTSTACK_SIZE + bool "Enforce strict size checking for sigaltstack" + depends on DYNAMIC_SIGFRAME + help + For historical reasons MINSIGSTKSZ is a constant which became + already too small with AVX512 support. Add a mechanism to + enforce strict checking of the sigaltstack size against the + real size of the FPU frame. This option enables the check + by default. It can also be controlled via the kernel command + line option 'strict_sas_size' independent of this config + switch. Enabling it might break existing applications which + allocate a too small sigaltstack but 'work' because they + never get a signal delivered. + + Say 'N' unless you want to really enforce this check. + source "kernel/livepatch/Kconfig" endmenu
arch/x86/kernel/signal.c +35 -0
@@ -15,6 +15,7 @@#include <linux/mm.h> #include <linux/smp.h> #include <linux/kernel.h> +#include <linux/kstrtox.h> #include <linux/errno.h> #include <linux/wait.h> #include <linux/tracehook.h>
@@ -40,6 +41,7 @@#include <linux/compat.h> #include <asm/proto.h> #include <asm/ia32_unistd.h> +#include <asm/fpu/xstate.h> #endif /* CONFIG_X86_64 */ #include <asm/syscall.h>
@@ -907,6 +909,39 @@ void signal_fault(struct pt_regs *regs, void __user *frame, char *where)force_sig (SIGSEGV ); } +#ifdef CONFIG_DYNAMIC_SIGFRAME +#ifdef CONFIG_STRICT_SIGALTSTACK_SIZE +static bool strict_sigaltstack_size__ro_after_init = true; +#else +static bool strict_sigaltstack_size__ro_after_init = false; +#endif + +static int __init strict_sas_size (char *arg) +{ +return kstrtobool (arg, &strict_sigaltstack_size); +} +__setup ("strict_sas_size" , strict_sas_size); + +/* +* MINSIGSTKSZ is 2048 and can't be changed despite the fact that AVX512 +* exceeds that size already. As such programs might never use the +* sigaltstack they just continued to work. While always checking against +* the real size would be correct, this might be considered a regression. +* +* Therefore avoid the sanity check, unless enforced by kernel config or +* command line option. +*/ +bool sigaltstack_size_valid (size_t ss_size) +{ +lockdep_assert_held (¤t->sighand->siglock); + +if (strict_sigaltstack_size) +return ss_size >get_sigframe_size (); + +return true; +} +#endif /* CONFIG_DYNAMIC_SIGFRAME */ +#ifdef CONFIG_X86_X32_ABI COMPAT_SYSCALL_DEFINE0 (x32_rt_sigreturn) {
Signature · 1 line
2.17.1
[tip: x86/fpu] x86/signal: Implement sigaltstack size validation
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 3aac3ebea08f2d342364f827c8979ab0e1dd591e Gitweb: https://git.kernel.org/tip/3aac3ebea08f2d342364f827c8979ab0e1dd591e Author: Thomas Gleixner <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:06 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:18:09 +02:00
x86/signal: Implement sigaltstack size validation
For historical reasons MINSIGSTKSZ is a constant which became already too small with AVX512 support.
Add a mechanism to enforce strict checking of the sigaltstack size against the real size of the FPU frame.
The strict check can be enabled via a config option and can also be controlled via the kernel command line option 'strict_sas_size' independent of the config switch.
Enabling it might break existing applications which allocate a too small sigaltstack but 'work' because they never get a signal delivered. Though it can be handy to filter out binaries which are not yet aware of AT_MINSIGSTKSZ.
Also the upcoming support for dynamically enabled FPU features requires a strict sanity check to ensure that:
- Enabling of a dynamic feature, which changes the sigframe size fits into an enabled sigaltstack
- Installing a too small sigaltstack after a dynamic feature has been added is not possible.
Implement the base check which is controlled by config and command line options.
--- Documentation/admin-guide/kernel-parameters.txt | 9 ++++- arch/x86/Kconfig | 17 ++++++++- arch/x86/kernel/signal.c | 35 ++++++++++++++++- 3 files changed, 61 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +9 -0
@@ -5497,6 +5497,15 @@ stifb= [HW] Format: bpp:<bpp1>[:<bpp2>[:<bpp3>...]] + strict_sas_size= + [X86] + Format: <bool> + Enable or disable strict sigaltstack size checks + against the required signal frame size which + depends on the supported FPU features. This can + be used to filter out binaries which have + not yet been made aware of AT_MINSIGSTKSZ. + sunrpc.min_resvport= sunrpc.max_resvport= [NFS,SUNRPC]
arch/x86/Kconfig +17 -0
@@ -125,6 +125,7 @@ config X86 select CLOCKSOURCE_VALIDATE_LAST_CYCLE select CLOCKSOURCE_WATCHDOG select DCACHE_WORD_ACCESS + select DYNAMIC_SIGFRAME select EDAC_ATOMIC_SCRUB select EDAC_SUPPORT select GENERIC_CLOCKEVENTS_BROADCAST if X86_64 || (X86_32 && X86_LOCAL_APIC)
@@ -2388,6 +2389,22 @@ config MODIFY_LDT_SYSCALL Saying 'N' here may make sense for embedded or server kernels. +config STRICT_SIGALTSTACK_SIZE + bool "Enforce strict size checking for sigaltstack" + depends on DYNAMIC_SIGFRAME + help + For historical reasons MINSIGSTKSZ is a constant which became + already too small with AVX512 support. Add a mechanism to + enforce strict checking of the sigaltstack size against the + real size of the FPU frame. This option enables the check + by default. It can also be controlled via the kernel command + line option 'strict_sas_size' independent of this config + switch. Enabling it might break existing applications which + allocate a too small sigaltstack but 'work' because they + never get a signal delivered. + + Say 'N' unless you want to really enforce this check. + source "kernel/livepatch/Kconfig" endmenu
arch/x86/kernel/signal.c +35 -0
@@ -15,6 +15,7 @@#include <linux/mm.h> #include <linux/smp.h> #include <linux/kernel.h> +#include <linux/kstrtox.h> #include <linux/errno.h> #include <linux/wait.h> #include <linux/tracehook.h>
@@ -40,6 +41,7 @@#include <linux/compat.h> #include <asm/proto.h> #include <asm/ia32_unistd.h> +#include <asm/fpu/xstate.h> #endif /* CONFIG_X86_64 */ #include <asm/syscall.h>
@@ -907,6 +909,39 @@ void signal_fault(struct pt_regs *regs, void __user *frame, char *where)force_sig (SIGSEGV ); } +#ifdef CONFIG_DYNAMIC_SIGFRAME +#ifdef CONFIG_STRICT_SIGALTSTACK_SIZE +static bool strict_sigaltstack_size__ro_after_init = true; +#else +static bool strict_sigaltstack_size__ro_after_init = false; +#endif + +static int __init strict_sas_size (char *arg) +{ +return kstrtobool (arg, &strict_sigaltstack_size); +} +__setup ("strict_sas_size" , strict_sas_size); + +/* +* MINSIGSTKSZ is 2048 and can't be changed despite the fact that AVX512 +* exceeds that size already. As such programs might never use the +* sigaltstack they just continued to work. While always checking against +* the real size would be correct, this might be considered a regression. +* +* Therefore avoid the sanity check, unless enforced by kernel config or +* command line option. +*/ +bool sigaltstack_size_valid (size_t ss_size) +{ +lockdep_assert_held (¤t->sighand->siglock); + +if (strict_sigaltstack_size) +return ss_size >get_sigframe_size (); + +return true; +} +#endif /* CONFIG_DYNAMIC_SIGFRAME */ +#ifdef CONFIG_X86_X32_ABI COMPAT_SYSCALL_DEFINE0 (x32_rt_sigreturn) {
PATCH 3/23 x86/fpu/xstate: Provide xstate_calculate_size()
In reply to Chang S. BaeSplit out the size calculation from the paranoia check, so it can be used for recalculating buffer sizes when dynamically enabled features becomes supported.
--- arch/x86/kernel/fpu/xstate.c | 46 ++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 18 deletions(-)
arch/x86/kernel/fpu/xstate.c +28 -18
@@ -549,6 +549,33 @@ static bool __init check_xstate_against_struct(int nr)return true; } +static unsigned int xstate_calculate_size (u64 xfeatures,bool compacted) +{ +unsigned int size =FXSAVE_SIZE +XSAVE_HDR_SIZE ; +int i; + +for_each_extended_xfeature (i, xfeatures) { +/* Align from the end of the previous feature */ +if (xfeature_is_aligned (i)) + size =ALIGN (size,64 ); +/* +* In compacted format the enabled features are packed, +* i.e. disabled features do not occupy space. +* +* In non-compacted format the offsets are fixed and +* disabled states still occupy space in the memory buffer. +*/ +if (!compacted) + size =xfeature_uncompacted_offset (i); +/* +* Add the feature size even for non-compacted format +* to make the end result correct +*/ + size +=xfeature_size (i); + } +return size; +} +/* * This essentially double-checks what the cpu told us about * how large the XSAVE buffer needs to be. We are recalculating
@@ -575,25 +602,8 @@ static bool __init paranoid_xstate_size_valid(unsigned int kernel_size)XSTATE_WARN_ON (1 );return false; } - -/* Align from the end of the previous feature */ -if (xfeature_is_aligned (i)) - size =ALIGN (size,64 ); -/* -* In compacted format the enabled features are packed, -* i.e. disabled features do not occupy space. -* -* In non-compacted format the offsets are fixed and -* disabled states still occupy space in the memory buffer. -*/ -if (!compacted) - size =xfeature_uncompacted_offset (i); -/* -* Add the feature size even for non-compacted format -* to make the end result correct -*/ - size +=xfeature_size (i); } + size =xstate_calculate_size (fpu_kernel_cfg.max_features, compacted);XSTATE_WARN_ON (size != kernel_size);return size == kernel_size; }
Signature · 1 line
2.17.1
[tip: x86/fpu] x86/fpu/xstate: Provide xstate_calculate_size()
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 84e4dccc8fce20b497388d756e12de5c9006eb48 Gitweb: https://git.kernel.org/tip/84e4dccc8fce20b497388d756e12de5c9006eb48 Author: Chang S. Bae <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:07 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:18:09 +02:00
x86/fpu/xstate: Provide xstate_calculate_size()
Split out the size calculation from the paranoia check so it can be used for recalculating buffer sizes when dynamically enabled features are supported.
--- arch/x86/kernel/fpu/xstate.c | 46 +++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 18 deletions(-)
arch/x86/kernel/fpu/xstate.c +28 -18
@@ -549,6 +549,33 @@ static bool __init check_xstate_against_struct(int nr)return true; } +static unsigned int xstate_calculate_size (u64 xfeatures,bool compacted) +{ +unsigned int size =FXSAVE_SIZE +XSAVE_HDR_SIZE ; +int i; + +for_each_extended_xfeature (i, xfeatures) { +/* Align from the end of the previous feature */ +if (xfeature_is_aligned (i)) + size =ALIGN (size,64 ); +/* +* In compacted format the enabled features are packed, +* i.e. disabled features do not occupy space. +* +* In non-compacted format the offsets are fixed and +* disabled states still occupy space in the memory buffer. +*/ +if (!compacted) + size =xfeature_uncompacted_offset (i); +/* +* Add the feature size even for non-compacted format +* to make the end result correct +*/ + size +=xfeature_size (i); + } +return size; +} +/* * This essentially double-checks what the cpu told us about * how large the XSAVE buffer needs to be. We are recalculating
@@ -575,25 +602,8 @@ static bool __init paranoid_xstate_size_valid(unsigned int kernel_size)XSTATE_WARN_ON (1 );return false; } - -/* Align from the end of the previous feature */ -if (xfeature_is_aligned (i)) - size =ALIGN (size,64 ); -/* -* In compacted format the enabled features are packed, -* i.e. disabled features do not occupy space. -* -* In non-compacted format the offsets are fixed and -* disabled states still occupy space in the memory buffer. -*/ -if (!compacted) - size =xfeature_uncompacted_offset (i); -/* -* Add the feature size even for non-compacted format -* to make the end result correct -*/ - size +=xfeature_size (i); } + size =xstate_calculate_size (fpu_kernel_cfg.max_features, compacted);XSTATE_WARN_ON (size != kernel_size);return size == kernel_size; }
PATCH 4/23 x86/fpu: Add members to struct fpu to cache permission information
In reply to Chang S. BaeFrom: Thomas Gleixner <[email protected]>
Dynamically enabled features can be requested by any thread of a running process at any time. The request does neither enable the feature nor allocate larger buffers. It just stores the permission to use the feature by adding the features to the permission bitmap and by calculating the required sizes for kernel and user space.
The reallocation of the kernel buffer happens when the feature is used for the first time which is caught by an exception. The permission bitmap is then checked and if the feature is permitted, then it becomes fully enabled. If not, the task dies similar to a task which uses an undefined instruction.
The size information is precomputed to allow proper sigaltstack size checks once the feature is permitted, but not yet in use because otherwise this would open race windows where too small stacks could be installed causing a later fail on signal delivery.
Initialize them to the default feature set and sizes.
--- arch/x86/include/asm/fpu/types.h | 46 ++++++++++++++++++++++++++++++++ arch/x86/kernel/fpu/core.c | 5 ++++ 2 files changed, 51 insertions(+)
arch/x86/include/asm/fpu/types.h +46 -0
@@ -352,6 +352,45 @@ struct fpstate {/* @regs is dynamically sized! Don't add anything after @regs! */ } __attribute__ ((aligned (64))); +struct fpu_state_perm { +/* +* @__state_perm: +* +* This bitmap indicates the permission for state components, which +* are available to a thread group. The permission prctl() sets the +* enabled state bits in thread_group_leader()->thread.fpu. +* +* All run time operations use the per thread information in the +* currently active fpu.fpstate which contains the xfeature masks +* and sizes for kernel and user space. +* +* This master permission field is only to be used when +* task.fpu.fpstate based checks fail to validate whether the task +* is allowed to expand it's xfeatures set which requires to +* allocate a larger sized fpstate buffer. +* +* Do not access this field directly. Use the provided helper +* function. Unlocked access is possible for quick checks. +*/ +u64 __state_perm; + +/* +* @__state_size: +* +* The size required for @__state_perm. Only valid to access +* with sighand locked. +*/ +unsigned int __state_size; + +/* +* @__user_state_size: +* +* The size required for @__state_perm user part. Only valid to +* access with sighand locked. +*/ +unsigned int __user_state_size; +}; +/* * Highest level per task FPU state data structure that * contains the FPU register state plus various FPU
@@ -395,6 +434,13 @@ struct fpu {*/ struct fpstate *__task_fpstate; +/* +* @perm: +* +* Permission related information +*/ +struct fpu_state_perm perm; +/* * @__fpstate: *
arch/x86/kernel/fpu/core.c +5 -0
@@ -415,6 +415,11 @@ void fpstate_reset(struct fpu *fpu)/* Set the fpstate pointer to the default fpstate */ fpu->fpstate = &fpu->__fpstate;__fpstate_reset (fpu->fpstate); + +/* Initialize the permission related info in fpu */ + fpu->perm.__state_perm = fpu_kernel_cfg.default_features; + fpu->perm.__state_size = fpu_kernel_cfg.default_size; + fpu->perm.__user_state_size = fpu_user_cfg.default_size; }/* Clone current's FPU state on fork */
Signature · 1 line
2.17.1
[tip: x86/fpu] x86/fpu: Add members to struct fpu to cache permission information
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 6f6a7c09c4065a5b140194dfcfe4cf7104fec4d2 Gitweb: https://git.kernel.org/tip/6f6a7c09c4065a5b140194dfcfe4cf7104fec4d2 Author: Thomas Gleixner <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:08 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:18:09 +02:00
x86/fpu: Add members to struct fpu to cache permission information
Dynamically enabled features can be requested by any thread of a running process at any time. The request does neither enable the feature nor allocate larger buffers. It just stores the permission to use the feature by adding the features to the permission bitmap and by calculating the required sizes for kernel and user space.
The reallocation of the kernel buffer happens when the feature is used for the first time which is caught by an exception. The permission bitmap is then checked and if the feature is permitted, then it becomes fully enabled. If not, the task dies similarly to a task which uses an undefined instruction.
The size information is precomputed to allow proper sigaltstack size checks once the feature is permitted, but not yet in use because otherwise this would open race windows where too small stacks could be installed causing a later fail on signal delivery.
Initialize them to the default feature set and sizes.
--- arch/x86/include/asm/fpu/types.h | 46 +++++++++++++++++++++++++++++++- arch/x86/kernel/fpu/core.c | 5 +++- 2 files changed, 51 insertions(+)
arch/x86/include/asm/fpu/types.h +46 -0
@@ -352,6 +352,45 @@ struct fpstate {/* @regs is dynamically sized! Don't add anything after @regs! */ }__aligned(64) ; +struct fpu_state_perm { +/* +* @__state_perm: +* +* This bitmap indicates the permission for state components, which +* are available to a thread group. The permission prctl() sets the +* enabled state bits in thread_group_leader()->thread.fpu. +* +* All run time operations use the per thread information in the +* currently active fpu.fpstate which contains the xfeature masks +* and sizes for kernel and user space. +* +* This master permission field is only to be used when +* task.fpu.fpstate based checks fail to validate whether the task +* is allowed to expand it's xfeatures set which requires to +* allocate a larger sized fpstate buffer. +* +* Do not access this field directly. Use the provided helper +* function. Unlocked access is possible for quick checks. +*/ +u64 __state_perm; + +/* +* @__state_size: +* +* The size required for @__state_perm. Only valid to access +* with sighand locked. +*/ +unsigned int __state_size; + +/* +* @__user_state_size: +* +* The size required for @__state_perm user part. Only valid to +* access with sighand locked. +*/ +unsigned int __user_state_size; +}; +/* * Highest level per task FPU state data structure that * contains the FPU register state plus various FPU
@@ -396,6 +435,13 @@ struct fpu {struct fpstate *__task_fpstate;/* +* @perm: +* +* Permission related information +*/ +struct fpu_state_perm perm; + +/* * @__fpstate: * * Initial in-memory storage for FPU registers which are saved in
arch/x86/kernel/fpu/core.c +5 -0
@@ -412,6 +412,11 @@ void fpstate_reset(struct fpu *fpu)/* Set the fpstate pointer to the default fpstate */ fpu->fpstate = &fpu->__fpstate;__fpstate_reset (fpu->fpstate); + +/* Initialize the permission related info in fpu */ + fpu->perm.__state_perm = fpu_kernel_cfg.default_features; + fpu->perm.__state_size = fpu_kernel_cfg.default_size; + fpu->perm.__user_state_size = fpu_user_cfg.default_size; }/* Clone current's FPU state on fork */
PATCH 5/23 x86/fpu: Add fpu_state_config::legacy_features
In reply to Chang S. BaeFrom: Thomas Gleixner <[email protected]>
The upcoming prctl() which is required to request the permission for a dynamically enabled feature will also provide an option to retrieve the supported features. If the CPU does not support xsave the supported features would be 0 even when the CPU supports FP and SSE.
Provide separate storage for the legacy feature set to avoid that and fill in the bits in the legacy init function.
--- arch/x86/include/asm/fpu/types.h | 7 +++++++ arch/x86/kernel/fpu/init.c | 9 ++++++--- 2 files changed, 13 insertions(+), 3 deletions(-)
arch/x86/include/asm/fpu/types.h +7 -0
@@ -500,6 +500,13 @@ struct fpu_state_config {* be requested by user space before usage. */ u64 default_features; +/* +* @legacy_features: +* +* Features which can be reported back to user space +* even without XSAVE support, i.e. legacy features FP + SSE +*/ +u64 legacy_features; };/* FPU state configuration information */
arch/x86/kernel/fpu/init.c +6 -3
@@ -193,12 +193,15 @@ static void __init fpu__init_system_xstate_size_legacy(void)* Note that the size configuration might be overwritten later * during fpu__init_system_xstate(). */ -if (!cpu_feature_enabled (X86_FEATURE_FPU )) +if (!cpu_feature_enabled (X86_FEATURE_FPU )) { size =sizeof (struct swregs_state ); -else if (cpu_feature_enabled (X86_FEATURE_FXSR )) + }else if (cpu_feature_enabled (X86_FEATURE_FXSR )) { size =sizeof (struct fxregs_state ); -else + fpu_user_cfg.legacy_features =XFEATURE_MASK_FPSSE ; + }else { size =sizeof (struct fregs_state ); + fpu_user_cfg.legacy_features =XFEATURE_MASK_FP ; + } fpu_kernel_cfg.max_size = size; fpu_kernel_cfg.default_size = size;
Signature · 1 line
2.17.1
[tip: x86/fpu] x86/fpu: Add fpu_state_config::legacy_features
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: c33f0a81a2cf3920465309ce683534751bb86485 Gitweb: https://git.kernel.org/tip/c33f0a81a2cf3920465309ce683534751bb86485 Author: Thomas Gleixner <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:09 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:18:09 +02:00
x86/fpu: Add fpu_state_config::legacy_features
The upcoming prctl() which is required to request the permission for a dynamically enabled feature will also provide an option to retrieve the supported features. If the CPU does not support XSAVE, the supported features would be 0 even when the CPU supports FP and SSE.
Provide separate storage for the legacy feature set to avoid that and fill in the bits in the legacy init function.
--- arch/x86/include/asm/fpu/types.h | 7 +++++++ arch/x86/kernel/fpu/init.c | 9 ++++++--- 2 files changed, 13 insertions(+), 3 deletions(-)
arch/x86/include/asm/fpu/types.h +7 -0
@@ -503,6 +503,13 @@ struct fpu_state_config {* be requested by user space before usage. */ u64 default_features; +/* +* @legacy_features: +* +* Features which can be reported back to user space +* even without XSAVE support, i.e. legacy features FP + SSE +*/ +u64 legacy_features; };/* FPU state configuration information */
arch/x86/kernel/fpu/init.c +6 -3
@@ -193,12 +193,15 @@ static void __init fpu__init_system_xstate_size_legacy(void)* Note that the size configuration might be overwritten later * during fpu__init_system_xstate(). */ -if (!cpu_feature_enabled (X86_FEATURE_FPU )) +if (!cpu_feature_enabled (X86_FEATURE_FPU )) { size =sizeof (struct swregs_state ); -else if (cpu_feature_enabled (X86_FEATURE_FXSR )) + }else if (cpu_feature_enabled (X86_FEATURE_FXSR )) { size =sizeof (struct fxregs_state ); -else + fpu_user_cfg.legacy_features =XFEATURE_MASK_FPSSE ; + }else { size =sizeof (struct fregs_state ); + fpu_user_cfg.legacy_features =XFEATURE_MASK_FP ; + } fpu_kernel_cfg.max_size = size; fpu_kernel_cfg.default_size = size;
PATCH 6/23 x86/arch_prctl: Add controls for dynamic XSTATE components
In reply to Chang S. BaeDynamically enabled XSTATE features are by default disabled for all processes. A process has to request permission to use such a feature.
To support this implement a architecture specific prctl() with the options:
- ARCH_GET_XCOMP_SUPP
Copies the supported feature bitmap into the user space provided u64 storage. The pointer is handed in via arg2
- ARCH_GET_XCOMP_PERM
Copies the process wide permitted feature bitmap into the user space provided u64 storage. The pointer is handed in via arg2
- ARCH_REQ_XCOMP_PERM
Request permission for a feature set. A feature set can be mapped to a facility, e.g. AMX, and can require one or more XSTATE components to be enabled.
The feature argument is the number of the highest XSTATE component which is required for a facility to work.
The request argument is not a user supplied bitmap because that makes filtering harder (think seccomp) and even impossible because to support 32bit tasks the argument would have to be a pointer.
The permission mechanism works this way:
Task asks for permission for a facility and kernel checks whether that's supported. If supported it does:
1) Check whether permission has already been granted
2) Compute the size of the required kernel and user space buffer (sigframe) size.
3) Validate that no task has a sigaltstack installed which is smaller than the resulting sigframe size
4) Add the requested feature bit(s) to the permission bitmap of current->group_leader->fpu and store the sizes in the group leaders fpu struct as well.
If that is successful then the feature is still not enabled for any of the tasks. The first usage of a related instruction will result in a #NM trap. The trap handler validates the permission bit of the tasks group leader and if permitted it installs a larger kernel buffer and transfers the permission and size info to the new fpstate container which makes all the FPU functions which require per task information aware of the extended feature set.
[ tglx: Adopted to new base code, added missing serialization,
massaged namings, comments and changelog ]
---
Changes from the tglx tree:
* Remove the dynamic feature example in the changelog for clarity. The bit-17 is not dynamic feature. (Dave Hansen)
--- arch/x86/include/asm/fpu/api.h | 4 + arch/x86/include/asm/proto.h | 2 +- arch/x86/include/uapi/asm/prctl.h | 4 + arch/x86/kernel/fpu/xstate.c | 156 ++++++++++++++++++++++++++++++ arch/x86/kernel/fpu/xstate.h | 6 ++ arch/x86/kernel/process.c | 9 +- 6 files changed, 178 insertions(+), 3 deletions(-)
arch/x86/include/asm/fpu/api.h +4 -0
@@ -151,4 +151,8 @@ static inline bool fpstate_is_confidential(struct fpu_guest *gfpu) return gfpu->fpstate->is_confidential; } +/* prctl */ +struct task_struct ; +extern long fpu_xstate_prctl (struct task_struct *tsk,int option,unsigned long arg2); +#endif /* _ASM_X86_FPU_API_H */
arch/x86/include/asm/proto.h +1 -1
@@ -40,6 +40,6 @@ void x86_report_nx(void);extern int reboot_force;long do_arch_prctl_common (struct task_struct *task,int option, -unsigned long cpuid_enabled); +unsigned long arg2);#endif /* _ASM_X86_PROTO_H */
arch/x86/include/uapi/asm/prctl.h +4 -0
@@ -10,6 +10,10 @@#define ARCH_GET_CPUID 0x1011#define ARCH_SET_CPUID 0x1012 +#define ARCH_GET_XCOMP_SUPP 0x1021 +#define ARCH_GET_XCOMP_PERM 0x1022 +#define ARCH_REQ_XCOMP_PERM 0x1023 +#define ARCH_MAP_VDSO_X32 0x2001#define ARCH_MAP_VDSO_32 0x2002#define ARCH_MAP_VDSO_64 0x2003
arch/x86/kernel/fpu/xstate.c +156 -0
174 lines not on this page · raw message
arch/x86/kernel/fpu/xstate.h +6 -0
@@ -15,6 +15,12 @@ static inline void xstate_init_xcomp_bv(struct xregs_state *xsave, u64 mask) xsave->header.xcomp_bv = mask |XCOMP_BV_COMPACTED_FORMAT ; } +static inline u64 xstate_get_host_group_perm (void ) +{ +/* Pairs with WRITE_ONCE() in xstate_request_perm() */ +return READ_ONCE (current->group_leader->thread.fpu.perm.__state_perm); +} +enum xstate_copy_mode {XSTATE_COPY_FP ,XSTATE_COPY_FX ,
arch/x86/kernel/process.c +7 -2
@@ -30,6 +30,7 @@#include <asm/apic.h> #include <linux/uaccess.h> #include <asm/mwait.h> +#include <asm/fpu/api.h> #include <asm/fpu/sched.h> #include <asm/debugreg.h> #include <asm/nmi.h>
@@ -1003,13 +1004,17 @@ unsigned long get_wchan(struct task_struct *p) }long do_arch_prctl_common (struct task_struct *task,int option, -unsigned long cpuid_enabled) +unsigned long arg2) {switch (option) {case ARCH_GET_CPUID :return get_cpuid_mode ();case ARCH_SET_CPUID : -return set_cpuid_mode (task, cpuid_enabled); +return set_cpuid_mode (task, arg2); +case ARCH_GET_XCOMP_SUPP : +case ARCH_GET_XCOMP_PERM : +case ARCH_REQ_XCOMP_PERM : +return fpu_xstate_prctl (task, option, arg2); }return -EINVAL ;
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH 06/23] x86/arch_prctl: Add controls for dynamic XSTATE components
Quoted 10 lines · Chang S. Bae
+/**
+ *fpu_xstate_prctl- xstate permission operations
+ * @tsk: Redundant pointer to current
+ * @option: A subfunction ofarch_prctl()
+ * @arg2: option argument
+ * Return: 0 if successful; otherwise, an error code
+ *
+ * Option arguments:
+ *
+ *ARCH_GET_XCOMP_SUPP: Pointer to user space u64 to store the info
Quoted 8 lines
+ *
ARCH_GET_XCOMP_PERM: Pointer to user space u64 to store the info
+ *ARCH_REQ_XCOMP_PERM: Facility number requested
+ *
+ * For facilities which require more than one XSTATE component, the request
+ * must be the highest state component number related to that facility,
+ * e.g. for AMX which requiresXFEATURE_XTILE_CFG(17)and
+ *XFEATURE_XTILE_DATA(18)this would beXFEATURE_XTILE_DATA(18).
+ */
Can I pls get this new set of prctl()s documented properly ontop of this patchset?
I guess Documentation/x86/xstate.rst or so.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH 1/1 Documentation/x86: Add documentation for using dynamic XSTATE features
Explain how dynamic XSTATE features can be enabled via the architecture-specific prctl() along with dynamic sigframe size and first use trap handling.
--- Documentation/x86/index.rst | 1 + Documentation/x86/xstate.rst | 66 ++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 Documentation/x86/xstate.rst
Documentation/x86/index.rst +1 -0
@@ -37,3 +37,4 @@ x86-specific Documentation sgx features elf_auxvec + xstate
Documentation/x86/xstate.rst new file +66 -0
66 lines not on this page · raw message
Signature · 1 line
2.17.1
[tip: x86/fpu] Documentation/x86: Add documentation for using dynamic XSTATE features
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 93175ec299f8418b415da8aabd9cc97506d49ab7 Gitweb: https://git.kernel.org/tip/93175ec299f8418b415da8aabd9cc97506d49ab7 Author: Chang S. Bae <[email protected]> AuthorDate: Tue, 26 Oct 2021 02:11:57 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 11:31:56 +02:00
Documentation/x86: Add documentation for using dynamic XSTATE features
Explain how dynamic XSTATE features can be enabled via the architecture-specific prctl() along with dynamic sigframe size and first use trap handling.
--- Documentation/x86/index.rst | 1 +- Documentation/x86/xstate.rst | 65 +++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+) create mode 100644 Documentation/x86/xstate.rst
Documentation/x86/index.rst +1 -0
@@ -37,3 +37,4 @@ x86-specific Documentation sgx features elf_auxvec + xstate
Documentation/x86/xstate.rst new file +65 -0
65 lines not on this page · raw message
[tip: x86/fpu] Documentation/x86: Add documentation for using dynamic XSTATE features
In reply to Chang S. BaeThe following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: d7a9590f608dbedd917eb0857a074accdf0d3919 Gitweb: https://git.kernel.org/tip/d7a9590f608dbedd917eb0857a074accdf0d3919 Author: Chang S. Bae <[email protected]> AuthorDate: Tue, 26 Oct 2021 02:11:57 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Thu, 28 Oct 2021 14:54:58 +02:00
Documentation/x86: Add documentation for using dynamic XSTATE features
Explain how dynamic XSTATE features can be enabled via the architecture-specific prctl() along with dynamic sigframe size and first use trap handling.
Fix:
Documentation/x86/xstate.rst:15: WARNING: Title underline too short.
as reported by Stephen Rothwell <[email protected]>
--- Documentation/x86/index.rst | 1 +- Documentation/x86/xstate.rst | 65 +++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+) create mode 100644 Documentation/x86/xstate.rst
Documentation/x86/index.rst +1 -0
@@ -37,3 +37,4 @@ x86-specific Documentation sgx features elf_auxvec + xstate
Documentation/x86/xstate.rst new file +65 -0
65 lines not on this page · raw message
[tip: x86/fpu] x86/arch_prctl: Add controls for dynamic XSTATE components
In reply to Chang S. BaeThe following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: db8268df0983adc2bb1fb48c9e5f7bfbb5f617f3 Gitweb: https://git.kernel.org/tip/db8268df0983adc2bb1fb48c9e5f7bfbb5f617f3 Author: Chang S. Bae <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:10 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:18:09 +02:00
x86/arch_prctl: Add controls for dynamic XSTATE components
Dynamically enabled XSTATE features are by default disabled for all processes. A process has to request permission to use such a feature.
To support this implement a architecture specific prctl() with the options:
- ARCH_GET_XCOMP_SUPP
Copies the supported feature bitmap into the user space provided u64 storage. The pointer is handed in via arg2
- ARCH_GET_XCOMP_PERM
Copies the process wide permitted feature bitmap into the user space provided u64 storage. The pointer is handed in via arg2
- ARCH_REQ_XCOMP_PERM
Request permission for a feature set. A feature set can be mapped to a facility, e.g. AMX, and can require one or more XSTATE components to be enabled.
The feature argument is the number of the highest XSTATE component which is required for a facility to work.
The request argument is not a user supplied bitmap because that makes filtering harder (think seccomp) and even impossible because to support 32bit tasks the argument would have to be a pointer.
The permission mechanism works this way:
Task asks for permission for a facility and kernel checks whether that's supported. If supported it does:
1) Check whether permission has already been granted
2) Compute the size of the required kernel and user space buffer (sigframe) size.
3) Validate that no task has a sigaltstack installed which is smaller than the resulting sigframe size
4) Add the requested feature bit(s) to the permission bitmap of current->group_leader->fpu and store the sizes in the group leaders fpu struct as well.
If that is successful then the feature is still not enabled for any of the tasks. The first usage of a related instruction will result in a #NM trap. The trap handler validates the permission bit of the tasks group leader and if permitted it installs a larger kernel buffer and transfers the permission and size info to the new fpstate container which makes all the FPU functions which require per task information aware of the extended feature set.
[ tglx: Adopted to new base code, added missing serialization,
massaged namings, comments and changelog ]
--- arch/x86/include/asm/fpu/api.h | 4 +- arch/x86/include/asm/proto.h | 2 +- arch/x86/include/uapi/asm/prctl.h | 4 +- arch/x86/kernel/fpu/xstate.c | 156 +++++++++++++++++++++++++++++- arch/x86/kernel/fpu/xstate.h | 6 +- arch/x86/kernel/process.c | 9 +- 6 files changed, 178 insertions(+), 3 deletions(-)
arch/x86/include/asm/fpu/api.h +4 -0
@@ -151,4 +151,8 @@ static inline bool fpstate_is_confidential(struct fpu_guest *gfpu) return gfpu->fpstate->is_confidential; } +/* prctl */ +struct task_struct ; +extern long fpu_xstate_prctl (struct task_struct *tsk,int option,unsigned long arg2); +#endif /* _ASM_X86_FPU_API_H */
arch/x86/include/asm/proto.h +1 -1
@@ -40,6 +40,6 @@ void x86_report_nx(void);extern int reboot_force;long do_arch_prctl_common (struct task_struct *task,int option, -unsigned long cpuid_enabled); +unsigned long arg2);#endif /* _ASM_X86_PROTO_H */
arch/x86/include/uapi/asm/prctl.h +4 -0
@@ -10,6 +10,10 @@#define ARCH_GET_CPUID 0x1011#define ARCH_SET_CPUID 0x1012 +#define ARCH_GET_XCOMP_SUPP 0x1021 +#define ARCH_GET_XCOMP_PERM 0x1022 +#define ARCH_REQ_XCOMP_PERM 0x1023 +#define ARCH_MAP_VDSO_X32 0x2001#define ARCH_MAP_VDSO_32 0x2002#define ARCH_MAP_VDSO_64 0x2003
arch/x86/kernel/fpu/xstate.c +156 -0
174 lines not on this page · raw message
arch/x86/kernel/fpu/xstate.h +6 -0
@@ -15,6 +15,12 @@ static inline void xstate_init_xcomp_bv(struct xregs_state *xsave, u64 mask) xsave->header.xcomp_bv = mask |XCOMP_BV_COMPACTED_FORMAT ; } +static inline u64 xstate_get_host_group_perm (void ) +{ +/* Pairs with WRITE_ONCE() in xstate_request_perm() */ +return READ_ONCE (current->group_leader->thread.fpu.perm.__state_perm); +} +enum xstate_copy_mode {XSTATE_COPY_FP ,XSTATE_COPY_FX ,
arch/x86/kernel/process.c +7 -2
@@ -30,6 +30,7 @@#include <asm/apic.h> #include <linux/uaccess.h> #include <asm/mwait.h> +#include <asm/fpu/api.h> #include <asm/fpu/sched.h> #include <asm/debugreg.h> #include <asm/nmi.h>
@@ -1003,13 +1004,17 @@ out: }long do_arch_prctl_common (struct task_struct *task,int option, -unsigned long cpuid_enabled) +unsigned long arg2) {switch (option) {case ARCH_GET_CPUID :return get_cpuid_mode ();case ARCH_SET_CPUID : -return set_cpuid_mode (task, cpuid_enabled); +return set_cpuid_mode (task, arg2); +case ARCH_GET_XCOMP_SUPP : +case ARCH_GET_XCOMP_PERM : +case ARCH_REQ_XCOMP_PERM : +return fpu_xstate_prctl (task, option, arg2); }return -EINVAL ;
PATCH 7/23 x86/fpu: Add basic helpers for dynamically enabled features
In reply to Chang S. BaeFrom: Thomas Gleixner <[email protected]>
To allow building up the infrastructure required to support dynamically enabled FPU features add:
- XFEATURES_MASK_DYNAMIC
This constant will hold xfeatures which can be dynamically enabled.
- fpu_state_size_dynamic()
A static branch for 64-bit and a simple 'return false' for 32-bit.
This helper allows to add dynamic feature specific changes to common code which is shared between 32-bit and 64-bit without #ifdeffery.
---
Changes from the tglx tree:
* Add the missing #ifdeffery. (Dave Hansen)
--- arch/x86/include/asm/fpu/xstate.h | 14 ++++++++++++++ arch/x86/kernel/fpu/core.c | 4 ++++ 2 files changed, 18 insertions(+)
arch/x86/include/asm/fpu/xstate.h +14 -0
@@ -43,6 +43,9 @@#define XFEATURE_MASK_USER_RESTORE \ (XFEATURE_MASK_USER_SUPPORTED & ~XFEATURE_MASK_PKRU) +/* Features which are dynamically enabled for a process on request */ +#define XFEATURE_MASK_USER_DYNAMIC 0ULL +/* All currently supported supervisor features */ #define XFEATURE_MASK_SUPERVISOR_SUPPORTED (XFEATURE_MASK_PASID)
@@ -96,4 +99,15 @@ int xfeature_size(int xfeature_nr);void xsaves (struct xregs_state *xsave,u64 mask);void xrstors (struct xregs_state *xsave,u64 mask); +#ifdef CONFIG_X86_64 +DECLARE_STATIC_KEY_FALSE (__fpu_state_size_dynamic); +#endif + +static __always_inline __pure bool fpu_state_size_dynamic(void) +{ +if (IS_ENABLED (CONFIG_X86_64 )) +return static_branch_unlikely (&__fpu_state_size_dynamic); +return false; +} +#endif
arch/x86/kernel/fpu/core.c +4 -0
@@ -25,6 +25,10 @@#define CREATE_TRACE_POINTS #include <asm/trace/fpu.h> +#ifdef CONFIG_X86_64 +DEFINE_STATIC_KEY_FALSE (__fpu_state_size_dynamic); +#endif +/* The FPU state configuration data for kernel and user space */ struct fpu_state_config fpu_kernel_cfg__ro_after_init ;struct fpu_state_config fpu_user_cfg__ro_after_init ;
Signature · 1 line
2.17.1
[tip: x86/fpu] x86/fpu: Add basic helpers for dynamically enabled features
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 23686ef25d4ae81bc12fe3994d1905191fcf71f8 Gitweb: https://git.kernel.org/tip/23686ef25d4ae81bc12fe3994d1905191fcf71f8 Author: Thomas Gleixner <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:11 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:18:09 +02:00
x86/fpu: Add basic helpers for dynamically enabled features
To allow building up the infrastructure required to support dynamically enabled FPU features, add:
- XFEATURES_MASK_DYNAMIC
This constant will hold xfeatures which can be dynamically enabled.
- fpu_state_size_dynamic()
A static branch for 64-bit and a simple 'return false' for 32-bit.
This helper allows to add dynamic-feature-specific changes to common code which is shared between 32-bit and 64-bit without #ifdeffery.
--- arch/x86/include/asm/fpu/xstate.h | 21 +++++++++++++++++++++ arch/x86/kernel/fpu/core.c | 4 ++++ 2 files changed, 25 insertions(+)
arch/x86/include/asm/fpu/xstate.h +21 -0
@@ -43,6 +43,9 @@#define XFEATURE_MASK_USER_RESTORE \ (XFEATURE_MASK_USER_SUPPORTED & ~XFEATURE_MASK_PKRU) +/* Features which are dynamically enabled for a process on request */ +#define XFEATURE_MASK_USER_DYNAMIC 0ULL +/* All currently supported supervisor features */ #define XFEATURE_MASK_SUPERVISOR_SUPPORTED (XFEATURE_MASK_PASID)
@@ -96,4 +99,22 @@ int xfeature_size(int xfeature_nr);void xsaves (struct xregs_state *xsave,u64 mask);void xrstors (struct xregs_state *xsave,u64 mask); +#ifdef CONFIG_X86_64 +DECLARE_STATIC_KEY_FALSE (__fpu_state_size_dynamic); +#endif + +#ifdef CONFIG_X86_64 +DECLARE_STATIC_KEY_FALSE (__fpu_state_size_dynamic); + +static __always_inline __pure bool fpu_state_size_dynamic(void) +{ +return static_branch_unlikely (&__fpu_state_size_dynamic); +} +#else +static __always_inline __pure bool fpu_state_size_dynamic(void) +{ +return false; +} +#endif +#endif
arch/x86/kernel/fpu/core.c +4 -0
@@ -25,6 +25,10 @@#define CREATE_TRACE_POINTS #include <asm/trace/fpu.h> +#ifdef CONFIG_X86_64 +DEFINE_STATIC_KEY_FALSE (__fpu_state_size_dynamic); +#endif +/* The FPU state configuration data for kernel and user space */ struct fpu_state_config fpu_kernel_cfg__ro_after_init ;struct fpu_state_config fpu_user_cfg__ro_after_init ;
PATCH 8/23 x86/signal: Use fpu::__state_user_size for sigalt stack validation
In reply to Chang S. BaeFrom: Thomas Gleixner <[email protected]>
Use the current->group_leader->fpu to check for pending permissions to use extended features and validate against the resulting user space size which is stored in the group leaders fpu struct as well.
This prevents a task to install a too small sized sigaltstack after permissions to use dynamically enabled features has been granted, but the task has not (yet) used a related instruction.
--- arch/x86/kernel/signal.c | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-)
arch/x86/kernel/signal.c +31 -4
@@ -32,6 +32,7 @@#include <asm/processor.h> #include <asm/ucontext.h> #include <asm/fpu/signal.h> +#include <asm/fpu/xstate.h> #include <asm/vdso.h> #include <asm/mce.h> #include <asm/sighandling.h>
@@ -720,12 +721,15 @@ SYSCALL_DEFINE0(rt_sigreturn)/* max_frame_size tells userspace the worst case signal stack size. */ static unsigned long __ro_after_init max_frame_size; +static unsigned int __ro_after_init fpu_default_state_size;void __init init_sigframe_size (void ) { + fpu_default_state_size =fpu__get_fpstate_size (); + max_frame_size =MAX_FRAME_SIGINFO_UCTXT_SIZE +MAX_FRAME_PADDING ; - max_frame_size +=fpu__get_fpstate_size () +MAX_XSAVE_PADDING ; + max_frame_size += fpu_default_state_size +MAX_XSAVE_PADDING ;/* Userspace expects an aligned size. */ max_frame_size =round_up (max_frame_size,FRAME_ALIGNMENT );
@@ -928,15 +932,38 @@ __setup("strict_sas_size", strict_sas_size);* sigaltstack they just continued to work. While always checking against * the real size would be correct, this might be considered a regression. * -* Therefore avoid the sanity check, unless enforced by kernel config or -* command line option. +* Therefore avoid the sanity check, unless enforced by kernel +* configuration or command line option. +* +* When dynamic FPU features are supported, the check is also enforced when +* the task has permissions to use dynamic features. Tasks which have no +* permission are checked against the size of the non-dynamic feature set +* if strict checking is enabled. This avoids forcing all tasks on the +* system to allocate large sigaltstacks even if they are never going +* to use a dynamic feature. As this is serialized via sighand::siglock +* any permission request for a dynamic feature either happened already +* or will see the newly install sigaltstack size in the permission checks. */ bool sigaltstack_size_valid (size_t ss_size) { +unsigned long fsize = max_frame_size - fpu_default_state_size; +u64 mask; +lockdep_assert_held (¤t->sighand->siglock); +if (!fpu_state_size_dynamic () && !strict_sigaltstack_size) +return true; + + fsize += current->group_leader->thread.fpu.perm.__user_state_size; +if (likely (ss_size > fsize)) +return true; +if (strict_sigaltstack_size) -return ss_size >get_sigframe_size (); +return ss_size > fsize; + + mask = current->group_leader->thread.fpu.perm.__state_perm; +if (mask &XFEATURE_MASK_USER_DYNAMIC ) +return ss_size > fsize;return true; }
Signature · 1 line
2.17.1
[tip: x86/fpu] x86/signal: Use fpu::__state_user_size for sigalt stack validation
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 4b7ca609a33dd8696bcbd2f1ad949e26a591592f Gitweb: https://git.kernel.org/tip/4b7ca609a33dd8696bcbd2f1ad949e26a591592f Author: Thomas Gleixner <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:12 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:18:09 +02:00
x86/signal: Use fpu::__state_user_size for sigalt stack validation
Use the current->group_leader->fpu to check for pending permissions to use extended features and validate against the resulting user space size which is stored in the group leaders fpu struct as well.
This prevents a task from installing a too small sized sigaltstack after permissions to use dynamically enabled features have been granted, but the task has not (yet) used a related instruction.
--- arch/x86/kernel/signal.c | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-)
arch/x86/kernel/signal.c +31 -4
@@ -32,6 +32,7 @@#include <asm/processor.h> #include <asm/ucontext.h> #include <asm/fpu/signal.h> +#include <asm/fpu/xstate.h> #include <asm/vdso.h> #include <asm/mce.h> #include <asm/sighandling.h>
@@ -720,12 +721,15 @@ badframe:/* max_frame_size tells userspace the worst case signal stack size. */ static unsigned long __ro_after_init max_frame_size; +static unsigned int __ro_after_init fpu_default_state_size;void __init init_sigframe_size (void ) { + fpu_default_state_size =fpu__get_fpstate_size (); + max_frame_size =MAX_FRAME_SIGINFO_UCTXT_SIZE +MAX_FRAME_PADDING ; - max_frame_size +=fpu__get_fpstate_size () +MAX_XSAVE_PADDING ; + max_frame_size += fpu_default_state_size +MAX_XSAVE_PADDING ;/* Userspace expects an aligned size. */ max_frame_size =round_up (max_frame_size,FRAME_ALIGNMENT );
@@ -928,15 +932,38 @@ __setup("strict_sas_size", strict_sas_size);* sigaltstack they just continued to work. While always checking against * the real size would be correct, this might be considered a regression. * -* Therefore avoid the sanity check, unless enforced by kernel config or -* command line option. +* Therefore avoid the sanity check, unless enforced by kernel +* configuration or command line option. +* +* When dynamic FPU features are supported, the check is also enforced when +* the task has permissions to use dynamic features. Tasks which have no +* permission are checked against the size of the non-dynamic feature set +* if strict checking is enabled. This avoids forcing all tasks on the +* system to allocate large sigaltstacks even if they are never going +* to use a dynamic feature. As this is serialized via sighand::siglock +* any permission request for a dynamic feature either happened already +* or will see the newly install sigaltstack size in the permission checks. */ bool sigaltstack_size_valid (size_t ss_size) { +unsigned long fsize = max_frame_size - fpu_default_state_size; +u64 mask; +lockdep_assert_held (¤t->sighand->siglock); +if (!fpu_state_size_dynamic () && !strict_sigaltstack_size) +return true; + + fsize += current->group_leader->thread.fpu.perm.__user_state_size; +if (likely (ss_size > fsize)) +return true; +if (strict_sigaltstack_size) -return ss_size >get_sigframe_size (); +return ss_size > fsize; + + mask = current->group_leader->thread.fpu.perm.__state_perm; +if (mask &XFEATURE_MASK_USER_DYNAMIC ) +return ss_size > fsize;return true; }
PATCH 9/23 x86/fpu/signal: Prepare for variable sigframe length
In reply to Chang S. BaeThe software reserved portion of the fxsave frame in the signal frame is copied from structures which have been set up at boot time. With dynamically enabled features the content of these structures is not longer correct because the xfeatures and size can be different per task.
Calculate the software reserved portion at runtime and fill in the xfeatures and size values from the tasks active fpstate.
--- arch/x86/kernel/fpu/internal.h | 3 -- arch/x86/kernel/fpu/signal.c | 62 ++++++++++++++-------------------- arch/x86/kernel/fpu/xstate.c | 1 - 3 files changed, 26 insertions(+), 40 deletions(-)
arch/x86/kernel/fpu/internal.h +0 -3
@@ -21,9 +21,6 @@ static __always_inline __pure bool use_fxsr(void)# define WARN_ON_FPU (x) ({ (void)(x); 0; })#endif -/* Init functions */ -extern void fpu__init_prepare_fx_sw_frame (void ); -/* Used in init.c */ extern void fpstate_init_user (struct fpstate *fpstate);extern void fpstate_reset (struct fpu *fpu);
arch/x86/kernel/fpu/signal.c +26 -36
101 lines not on this page · raw message
arch/x86/kernel/fpu/xstate.c +0 -1
@@ -830,7 +830,6 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)update_regset_xstate_info (fpu_user_cfg.max_size, fpu_user_cfg.max_features); -fpu__init_prepare_fx_sw_frame ();setup_init_fpu_buf ();setup_xstate_comp_offsets ();setup_supervisor_only_offsets ();
Signature · 1 line
2.17.1
[tip: x86/fpu] x86/fpu/signal: Prepare for variable sigframe length
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 53599b4d54b9b8dda1d537a558946869d2acbddc Gitweb: https://git.kernel.org/tip/53599b4d54b9b8dda1d537a558946869d2acbddc Author: Chang S. Bae <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:13 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:18:09 +02:00
x86/fpu/signal: Prepare for variable sigframe length
The software reserved portion of the fxsave frame in the signal frame is copied from structures which have been set up at boot time. With dynamically enabled features the content of these structures is no longer correct because the xfeatures and size can be different per task.
Calculate the software reserved portion at runtime and fill in the xfeatures and size values from the tasks active fpstate.
--- arch/x86/kernel/fpu/internal.h | 3 +-- arch/x86/kernel/fpu/signal.c | 62 +++++++++++++-------------------- arch/x86/kernel/fpu/xstate.c | 1 +- 3 files changed, 26 insertions(+), 40 deletions(-)
arch/x86/kernel/fpu/internal.h +0 -3
@@ -21,9 +21,6 @@ static __always_inline __pure bool use_fxsr(void)# define WARN_ON_FPU (x) ({ (void)(x); 0; })#endif -/* Init functions */ -extern void fpu__init_prepare_fx_sw_frame (void ); -/* Used in init.c */ extern void fpstate_init_user (struct fpstate *fpstate);extern void fpstate_reset (struct fpu *fpu);
arch/x86/kernel/fpu/signal.c +26 -36
101 lines not on this page · raw message
arch/x86/kernel/fpu/xstate.c +0 -1
@@ -830,7 +830,6 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)update_regset_xstate_info (fpu_user_cfg.max_size, fpu_user_cfg.max_features); -fpu__init_prepare_fx_sw_frame ();setup_init_fpu_buf ();setup_xstate_comp_offsets ();setup_supervisor_only_offsets ();
PATCH 10/23 x86/fpu: Prepare fpu_clone() for dynamically enabled features
In reply to Chang S. BaeFrom: Thomas Gleixner <[email protected]>
The default portion of the parent's FPU state is saved in a child task. With dynamic features enabled, the non-default portion is not saved in a child's fpstate because these register states are defined to be caller-saved. The new task's fpstate is therefore the default buffer.
Fork inherits the permission of the parent.
Also, do not use memcpy() when TIF_NEED_FPU_LOAD is set because it is invalid when the parent has dynamic features.
---
Changes from the tglx tree:
* Add the changelog.
* Update comments. (Dave Hansen)
--- arch/x86/include/asm/fpu/sched.h | 2 +- arch/x86/kernel/fpu/core.c | 35 +++++++++++++++++++++++--------- arch/x86/kernel/process.c | 2 +- 3 files changed, 27 insertions(+), 12 deletions(-)
arch/x86/include/asm/fpu/sched.h +1 -1
@@ -11,7 +11,7 @@extern void save_fpregs_to_fpstate (struct fpu *fpu);extern void fpu__drop (struct fpu *fpu); -extern int fpu_clone (struct task_struct *dst); +extern int fpu_clone (struct task_struct *dst,unsigned long clone_flags);extern void fpu_flush_thread (void );/*
arch/x86/kernel/fpu/core.c +25 -10
@@ -426,8 +426,20 @@ void fpstate_reset(struct fpu *fpu) fpu->perm.__user_state_size = fpu_user_cfg.default_size; } +static inline void fpu_inherit_perms (struct fpu *dst_fpu) +{ +if (fpu_state_size_dynamic ()) { +struct fpu *src_fpu = ¤t->group_leader->thread.fpu; + +spin_lock_irq (¤t->sighand->siglock); +/* Fork also inherits the permissions of the parent */ + dst_fpu->perm = src_fpu->perm; +spin_unlock_irq (¤t->sighand->siglock); + } +} +/* Clone current's FPU state on fork */ -int fpu_clone (struct task_struct *dst) +int fpu_clone (struct task_struct *dst,unsigned long clone_flags) {struct fpu *src_fpu = ¤t->thread.fpu;struct fpu *dst_fpu = &dst->thread.fpu;
@@ -458,17 +470,20 @@ int fpu_clone(struct task_struct *dst) }/* -* If the FPU registers are not owned by current just memcpy() the -* state. Otherwise save the FPU registers directly into the -* child's FPU context, without any memory-to-memory copying. +* Save the default portion of the current FPU state into the +* clone. Assume all dynamic features to be defined as caller- +* saved, which enables skipping both the expansion of fpstate +* and the copying of any dynamic state. +* +* Do not use memcpy() when TIF_NEED_FPU_LOAD is set because +* copying is not valid when current uses non-default states. */ fpregs_lock (); -if (test_thread_flag (TIF_NEED_FPU_LOAD )) { -memcpy (&dst_fpu->fpstate->regs, &src_fpu->fpstate->regs, - dst_fpu->fpstate->size); - }else { -save_fpregs_to_fpstate (dst_fpu); - } +if (test_thread_flag (TIF_NEED_FPU_LOAD )) +fpregs_restore_userregs (); +save_fpregs_to_fpstate (dst_fpu); +if (!(clone_flags &CLONE_THREAD )) +fpu_inherit_perms (dst_fpu);fpregs_unlock ();trace_x86_fpu_copy_src (src_fpu);
arch/x86/kernel/process.c +1 -1
@@ -157,7 +157,7 @@ int copy_thread(unsigned long clone_flags, unsigned long sp, unsigned long arg, frame->flags =X86_EFLAGS_FIXED ;#endif -fpu_clone (p); +fpu_clone (p, clone_flags);/* Kernel thread ? */ if (unlikely (p->flags &PF_KTHREAD )) {
Signature · 1 line
2.17.1
[tip: x86/fpu] x86/fpu: Prepare fpu_clone() for dynamically enabled features
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 9e798e9aa14c45fb94e47b30bf6347b369ce9df7 Gitweb: https://git.kernel.org/tip/9e798e9aa14c45fb94e47b30bf6347b369ce9df7 Author: Thomas Gleixner <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:14 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:18:09 +02:00
x86/fpu: Prepare fpu_clone() for dynamically enabled features
The default portion of the parent's FPU state is saved in a child task. With dynamic features enabled, the non-default portion is not saved in a child's fpstate because these register states are defined to be caller-saved. The new task's fpstate is therefore the default buffer.
Fork inherits the permission of the parent.
Also, do not use memcpy() when TIF_NEED_FPU_LOAD is set because it is invalid when the parent has dynamic features.
--- arch/x86/include/asm/fpu/sched.h | 2 +- arch/x86/kernel/fpu/core.c | 35 ++++++++++++++++++++++--------- arch/x86/kernel/process.c | 2 +- 3 files changed, 27 insertions(+), 12 deletions(-)
arch/x86/include/asm/fpu/sched.h +1 -1
@@ -11,7 +11,7 @@extern void save_fpregs_to_fpstate (struct fpu *fpu);extern void fpu__drop (struct fpu *fpu); -extern int fpu_clone (struct task_struct *dst); +extern int fpu_clone (struct task_struct *dst,unsigned long clone_flags);extern void fpu_flush_thread (void );/*
arch/x86/kernel/fpu/core.c +25 -10
@@ -423,8 +423,20 @@ void fpstate_reset(struct fpu *fpu) fpu->perm.__user_state_size = fpu_user_cfg.default_size; } +static inline void fpu_inherit_perms (struct fpu *dst_fpu) +{ +if (fpu_state_size_dynamic ()) { +struct fpu *src_fpu = ¤t->group_leader->thread.fpu; + +spin_lock_irq (¤t->sighand->siglock); +/* Fork also inherits the permissions of the parent */ + dst_fpu->perm = src_fpu->perm; +spin_unlock_irq (¤t->sighand->siglock); + } +} +/* Clone current's FPU state on fork */ -int fpu_clone (struct task_struct *dst) +int fpu_clone (struct task_struct *dst,unsigned long clone_flags) {struct fpu *src_fpu = ¤t->thread.fpu;struct fpu *dst_fpu = &dst->thread.fpu;
@@ -455,17 +467,20 @@ int fpu_clone(struct task_struct *dst) }/* -* If the FPU registers are not owned by current just memcpy() the -* state. Otherwise save the FPU registers directly into the -* child's FPU context, without any memory-to-memory copying. +* Save the default portion of the current FPU state into the +* clone. Assume all dynamic features to be defined as caller- +* saved, which enables skipping both the expansion of fpstate +* and the copying of any dynamic state. +* +* Do not use memcpy() when TIF_NEED_FPU_LOAD is set because +* copying is not valid when current uses non-default states. */ fpregs_lock (); -if (test_thread_flag (TIF_NEED_FPU_LOAD )) { -memcpy (&dst_fpu->fpstate->regs, &src_fpu->fpstate->regs, - dst_fpu->fpstate->size); - }else { -save_fpregs_to_fpstate (dst_fpu); - } +if (test_thread_flag (TIF_NEED_FPU_LOAD )) +fpregs_restore_userregs (); +save_fpregs_to_fpstate (dst_fpu); +if (!(clone_flags &CLONE_THREAD )) +fpu_inherit_perms (dst_fpu);fpregs_unlock ();trace_x86_fpu_copy_src (src_fpu);
arch/x86/kernel/process.c +1 -1
@@ -157,7 +157,7 @@ int copy_thread(unsigned long clone_flags, unsigned long sp, unsigned long arg, frame->flags =X86_EFLAGS_FIXED ;#endif -fpu_clone (p); +fpu_clone (p, clone_flags);/* Kernel thread ? */ if (unlikely (p->flags &PF_KTHREAD )) {
PATCH 11/23 x86/fpu: Reset permission and fpstate on exec()
In reply to Chang S. BaeOn exec(), extended register states saved in the buffer is cleared. With dynamic features, each task carries variables besides the register states. The struct fpu has permission information and struct fpstate contains buffer size and feature masks. They are all dynamically updated with dynamic features.
Reset the current task's entire FPU data before an exec() so that the new task starts with default permission and fpstate.
Rename the register state reset function because the old naming confuses as it does not reset struct fpstate.
---
Changes from the tglx tree:
* Add as a new patch.
--- arch/x86/kernel/fpu/core.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/kernel/fpu/core.c +4 -3
@@ -547,7 +547,7 @@ static inline void restore_fpregs_from_init_fpstate(u64 features_mask)/* * Reset current->fpu memory state to the init values. */ -static void fpu_reset_fpstate (void ) +static void fpu_reset_fpregs (void ) {struct fpu *fpu = ¤t->thread.fpu;
@@ -582,7 +582,7 @@ void fpu__clear_user_states(struct fpu *fpu)fpregs_lock ();if (!cpu_feature_enabled (X86_FEATURE_FPU )) { -fpu_reset_fpstate (); +fpu_reset_fpregs ();fpregs_unlock ();return ; }
@@ -612,7 +612,8 @@ void fpu__clear_user_states(struct fpu *fpu)void fpu_flush_thread (void ) { -fpu_reset_fpstate (); +fpstate_reset (¤t->thread.fpu); +fpu_reset_fpregs (); }/* * Load FPU context before returning to userspace.
Signature · 1 line
2.17.1
[tip: x86/fpu] x86/fpu: Reset permission and fpstate on exec()
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: e61d6310a0f80cb986fd2076d432760b3619fb6d Gitweb: https://git.kernel.org/tip/e61d6310a0f80cb986fd2076d432760b3619fb6d Author: Chang S. Bae <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:15 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:18:09 +02:00
x86/fpu: Reset permission and fpstate on exec()
On exec(), extended register states saved in the buffer is cleared. With dynamic features, each task carries variables besides the register states. The struct fpu has permission information and struct fpstate contains buffer size and feature masks. They are all dynamically updated with dynamic features.
Reset the current task's entire FPU data before an exec() so that the new task starts with default permission and fpstate.
Rename the register state reset function because the old naming confuses as it does not reset struct fpstate.
--- arch/x86/kernel/fpu/core.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/kernel/fpu/core.c +4 -3
@@ -544,7 +544,7 @@ static inline void restore_fpregs_from_init_fpstate(u64 features_mask)/* * Reset current->fpu memory state to the init values. */ -static void fpu_reset_fpstate (void ) +static void fpu_reset_fpregs (void ) {struct fpu *fpu = ¤t->thread.fpu;
@@ -579,7 +579,7 @@ void fpu__clear_user_states(struct fpu *fpu)fpregs_lock ();if (!cpu_feature_enabled (X86_FEATURE_FPU )) { -fpu_reset_fpstate (); +fpu_reset_fpregs ();fpregs_unlock ();return ; }
@@ -609,7 +609,8 @@ void fpu__clear_user_states(struct fpu *fpu)void fpu_flush_thread (void ) { -fpu_reset_fpstate (); +fpstate_reset (¤t->thread.fpu); +fpu_reset_fpregs (); }/* * Load FPU context before returning to userspace.
PATCH 12/23 x86/cpufeatures: Add eXtended Feature Disabling (XFD) feature bit
In reply to Chang S. BaeIntel's eXtended Feature Disable (XFD) feature is an extension of the XSAVE architecture. XFD allows the kernel to enable a feature state in XCR0 and to receive a #NM trap when a task uses instructions accessing that state.
This is going to be used to postpone the allocation of a larger XSTATE buffer for a task to the point where it is actually using a related instruction after the permission to use that facility has been granted.
XFD is not used by the kernel, but only applied to userspace. This is a matter of policy as the kernel knows how a fpstate is reallocated and the XFD state.
The compacted XSAVE format is adjustable for dynamic features. Make XFD depend on XSAVES.
---
Changes from the tglx tree:
* Call out the policy to use XFD for userspace. (Dave Hansen)
* Make XFD depend on XSAVES. (Dave Hansen)
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/kernel/cpu/cpuid-deps.c | 1 + 2 files changed, 2 insertions(+)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -277,6 +277,7 @@#define X86_FEATURE_XSAVEC (10*32+ 1)/* XSAVEC instruction */ #define X86_FEATURE_XGETBV1 (10*32+ 2)/* XGETBV with ECX = 1 instruction */ #define X86_FEATURE_XSAVES (10*32+ 3)/* XSAVES/XRSTORS instructions */ +#define X86_FEATURE_XFD (10*32+ 4)/* "" eXtended Feature Disabling */ /* * Extended auxiliary flags: Linux defined - for features scattered in various
arch/x86/kernel/cpu/cpuid-deps.c +1 -0
@@ -75,6 +75,7 @@ static const struct cpuid_dep cpuid_deps[] = { { X86_FEATURE_SGX_LC, X86_FEATURE_SGX }, { X86_FEATURE_SGX1, X86_FEATURE_SGX }, { X86_FEATURE_SGX2, X86_FEATURE_SGX1 }, + { X86_FEATURE_XFD, X86_FEATURE_XSAVES }, {} };
Signature · 1 line
2.17.1
[tip: x86/fpu] x86/cpufeatures: Add eXtended Feature Disabling (XFD) feature bit
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: c351101678ce54492b6e09810ec02efc0df036a9 Gitweb: https://git.kernel.org/tip/c351101678ce54492b6e09810ec02efc0df036a9 Author: Chang S. Bae <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:16 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:18:09 +02:00
x86/cpufeatures: Add eXtended Feature Disabling (XFD) feature bit
Intel's eXtended Feature Disable (XFD) feature is an extension of the XSAVE architecture. XFD allows the kernel to enable a feature state in XCR0 and to receive a #NM trap when a task uses instructions accessing that state.
This is going to be used to postpone the allocation of a larger XSTATE buffer for a task to the point where it is actually using a related instruction after the permission to use that facility has been granted.
XFD is not used by the kernel, but only applied to userspace. This is a matter of policy as the kernel knows how a fpstate is reallocated and the XFD state.
The compacted XSAVE format is adjustable for dynamic features. Make XFD depend on XSAVES.
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/kernel/cpu/cpuid-deps.c | 1 + 2 files changed, 2 insertions(+)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -277,6 +277,7 @@#define X86_FEATURE_XSAVEC (10*32+ 1)/* XSAVEC instruction */ #define X86_FEATURE_XGETBV1 (10*32+ 2)/* XGETBV with ECX = 1 instruction */ #define X86_FEATURE_XSAVES (10*32+ 3)/* XSAVES/XRSTORS instructions */ +#define X86_FEATURE_XFD (10*32+ 4)/* "" eXtended Feature Disabling */ /* * Extended auxiliary flags: Linux defined - for features scattered in various
arch/x86/kernel/cpu/cpuid-deps.c +1 -0
@@ -75,6 +75,7 @@ static const struct cpuid_dep cpuid_deps[] = { { X86_FEATURE_SGX_LC, X86_FEATURE_SGX }, { X86_FEATURE_SGX1, X86_FEATURE_SGX }, { X86_FEATURE_SGX2, X86_FEATURE_SGX1 }, + { X86_FEATURE_XFD, X86_FEATURE_XSAVES }, {} };
PATCH 13/23 x86/msr-index: Add MSRs for XFD
In reply to Chang S. BaeXFD introduces two MSRs:
- IA32_XFD to enable/disable a feature controlled by XFD
- IA32_XFD_ERR to expose the #NM trap handler which feature was tried to be used for the first time.
Both use the same xstate-component bitmap format, used by XCR0.
--- arch/x86/include/asm/msr-index.h | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/include/asm/msr-index.h +2 -0
@@ -625,6 +625,8 @@#define MSR_IA32_BNDCFGS_RSVD 0x00000ffc +#define MSR_IA32_XFD 0x000001c4 +#define MSR_IA32_XFD_ERR 0x000001c5#define MSR_IA32_XSS 0x00000da0#define MSR_IA32_APICBASE 0x0000001b
Signature · 1 line
2.17.1
[tip: x86/fpu] x86/msr-index: Add MSRs for XFD
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: dae1bd58389615d401a84aedc38fa075ef8f7de6 Gitweb: https://git.kernel.org/tip/dae1bd58389615d401a84aedc38fa075ef8f7de6 Author: Chang S. Bae <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:17 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:18:09 +02:00
x86/msr-index: Add MSRs for XFD
XFD introduces two MSRs:
- IA32_XFD to enable/disable a feature controlled by XFD
- IA32_XFD_ERR to expose to the #NM trap handler which feature was tried to be used for the first time.
Both use the same xstate-component bitmap format, used by XCR0.
--- arch/x86/include/asm/msr-index.h | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/include/asm/msr-index.h +2 -0
@@ -625,6 +625,8 @@#define MSR_IA32_BNDCFGS_RSVD 0x00000ffc +#define MSR_IA32_XFD 0x000001c4 +#define MSR_IA32_XFD_ERR 0x000001c5#define MSR_IA32_XSS 0x00000da0#define MSR_IA32_APICBASE 0x0000001b
PATCH 14/23 x86/fpu: Add XFD state to fpstate
In reply to Chang S. BaeAdd storage for XFD register state to struct fpstate. This will be used to store the XFD MSR state. This will be used for switching the XFD MSR when FPU content is restored.
Add a per-CPU variable to cache the current MSR value so the MSR has only to be written when the values are different.
---
Changes from the tglx tree:
* Add a missing #ifdeffery.
--- arch/x86/include/asm/fpu/types.h | 3 +++ arch/x86/kernel/fpu/core.c | 2 ++ arch/x86/kernel/fpu/xstate.h | 4 ++++ 3 files changed, 9 insertions(+)
arch/x86/include/asm/fpu/types.h +3 -0
@@ -322,6 +322,9 @@ struct fpstate {/* @user_xfeatures: xfeatures valid in UABI buffers */ u64 user_xfeatures; +/* @xfd: xfeatures disabled to trap userspace use. */ +u64 xfd; +/* @is_valloc: Indicator for dynamically allocated state */ unsigned int is_valloc : 1;
arch/x86/kernel/fpu/core.c +2 -0
@@ -27,6 +27,7 @@#ifdef CONFIG_X86_64 DEFINE_STATIC_KEY_FALSE (__fpu_state_size_dynamic); +DEFINE_PER_CPU (u64, xfd_state);#endif /* The FPU state configuration data for kernel and user space */
@@ -412,6 +413,7 @@ static void __fpstate_reset(struct fpstate *fpstate) fpstate->user_size = fpu_user_cfg.default_size; fpstate->xfeatures = fpu_kernel_cfg.default_features; fpstate->user_xfeatures = fpu_user_cfg.default_features; + fpstate->xfd = init_fpstate.xfd; }void fpstate_reset (struct fpu *fpu)
arch/x86/kernel/fpu/xstate.h +4 -0
@@ -5,6 +5,10 @@#include <asm/cpufeature.h> #include <asm/fpu/xstate.h> +#ifdef CONFIG_X86_64 +DECLARE_PER_CPU (u64, xfd_state); +#endif +static inline void xstate_init_xcomp_bv (struct xregs_state *xsave,u64 mask) {/*
Signature · 1 line
2.17.1
[tip: x86/fpu] x86/fpu: Add XFD state to fpstate
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 8bf26758ca9659866b844dd51037314b4c0fa6bd Gitweb: https://git.kernel.org/tip/8bf26758ca9659866b844dd51037314b4c0fa6bd Author: Chang S. Bae <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:18 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:18:09 +02:00
x86/fpu: Add XFD state to fpstate
Add storage for XFD register state to struct fpstate. This will be used to store the XFD MSR state. This will be used for switching the XFD MSR when FPU content is restored.
Add a per-CPU variable to cache the current MSR value so the MSR has only to be written when the values are different.
--- arch/x86/include/asm/fpu/types.h | 3 +++ arch/x86/kernel/fpu/core.c | 2 ++ arch/x86/kernel/fpu/xstate.h | 4 ++++ 3 files changed, 9 insertions(+)
arch/x86/include/asm/fpu/types.h +3 -0
@@ -322,6 +322,9 @@ struct fpstate {/* @user_xfeatures: xfeatures valid in UABI buffers */ u64 user_xfeatures; +/* @xfd: xfeatures disabled to trap userspace use. */ +u64 xfd; +/* @is_valloc: Indicator for dynamically allocated state */ unsigned int is_valloc : 1;
arch/x86/kernel/fpu/core.c +2 -0
@@ -27,6 +27,7 @@#ifdef CONFIG_X86_64 DEFINE_STATIC_KEY_FALSE (__fpu_state_size_dynamic); +DEFINE_PER_CPU (u64, xfd_state);#endif /* The FPU state configuration data for kernel and user space */
@@ -409,6 +410,7 @@ static void __fpstate_reset(struct fpstate *fpstate) fpstate->user_size = fpu_user_cfg.default_size; fpstate->xfeatures = fpu_kernel_cfg.default_features; fpstate->user_xfeatures = fpu_user_cfg.default_features; + fpstate->xfd = init_fpstate.xfd; }void fpstate_reset (struct fpu *fpu)
arch/x86/kernel/fpu/xstate.h +4 -0
@@ -5,6 +5,10 @@#include <asm/cpufeature.h> #include <asm/fpu/xstate.h> +#ifdef CONFIG_X86_64 +DECLARE_PER_CPU (u64, xfd_state); +#endif +static inline void xstate_init_xcomp_bv (struct xregs_state *xsave,u64 mask) {/*
PATCH 15/23 x86/fpu: Add sanity checks for XFD
In reply to Chang S. BaeFrom: Thomas Gleixner <[email protected]>
Add debug functionality to ensure that the XFD MSR is up to date for XSAVE* and XRSTOR* operations.
---
Changes from the tglx tree:
* Rename the xstate ops validation helper. (Dave Hansen)
* Update code comments. (Dave Hansen)
--- arch/x86/kernel/fpu/core.c | 9 +++--- arch/x86/kernel/fpu/signal.c | 6 ++-- arch/x86/kernel/fpu/xstate.c | 55 ++++++++++++++++++++++++++++++++++++ arch/x86/kernel/fpu/xstate.h | 34 +++++++++++++++++++--- 4 files changed, 92 insertions(+), 12 deletions(-)
arch/x86/kernel/fpu/core.c +4 -5
@@ -166,7 +166,7 @@ void restore_fpregs_from_fpstate(struct fpstate *fpstate, u64 mask)*/ mask = fpu_kernel_cfg.max_features & mask; -os_xrstor (&fpstate->regs.xsave, mask); +os_xrstor (fpstate, mask); }else {if (use_fxsr ())fxrstor (&fpstate->regs.fxsave);
@@ -537,7 +537,7 @@ void fpu__drop(struct fpu *fpu)static inline void restore_fpregs_from_init_fpstate (u64 features_mask) {if (use_xsave ()) -os_xrstor (&init_fpstate.regs.xsave, features_mask); +os_xrstor (&init_fpstate, features_mask);else if (use_fxsr ())fxrstor (&init_fpstate.regs.fxsave);else
@@ -594,9 +594,8 @@ void fpu__clear_user_states(struct fpu *fpu)* corresponding registers. */ if (xfeatures_mask_supervisor () && - !fpregs_state_valid (fpu,smp_processor_id ())) { -os_xrstor (&fpu->fpstate->regs.xsave,xfeatures_mask_supervisor ()); - } + !fpregs_state_valid (fpu,smp_processor_id ())) +os_xrstor_supervisor (fpu->fpstate);/* Reset user states in registers. */ restore_fpregs_from_init_fpstate (XFEATURE_MASK_USER_RESTORE );
arch/x86/kernel/fpu/signal.c +3 -3
@@ -261,7 +261,7 @@ static int __restore_fpregs_from_user(void __user *buf, u64 ufeatures, ret =fxrstor_from_user_sigframe (buf);if (!ret &&unlikely (init_bv)) -os_xrstor (&init_fpstate.regs.xsave, init_bv); +os_xrstor (&init_fpstate, init_bv);return ret; }else if (use_fxsr ()) {return fxrstor_from_user_sigframe (buf);
@@ -322,7 +322,7 @@ static bool restore_fpregs_from_user(void __user *buf, u64 xrestore,* been restored from a user buffer directly. */ if (test_thread_flag (TIF_NEED_FPU_LOAD ) &&xfeatures_mask_supervisor ()) -os_xrstor (&fpu->fpstate->regs.xsave,xfeatures_mask_supervisor ()); +os_xrstor_supervisor (fpu->fpstate);fpregs_mark_activate ();fpregs_unlock ();
@@ -432,7 +432,7 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,u64 mask = user_xfeatures |xfeatures_mask_supervisor (); fpregs->xsave.header.xfeatures &= mask; - success = !os_xrstor_safe (&fpregs->xsave, + success = !os_xrstor_safe (fpu->fpstate, fpu_kernel_cfg.max_features); }else { success = !fxrstor_safe (&fpregs->fxsave);
arch/x86/kernel/fpu/xstate.c +55 -0
61 lines not on this page · raw message
arch/x86/kernel/fpu/xstate.h +30 -4
@@ -130,6 +130,12 @@ static inline u64 xfeatures_mask_independent(void) : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \ : "memory") +#if defined(CONFIG_X86_64 ) && defined(CONFIG_X86_DEBUG_FPU ) +extern void xfd_validate_state (struct fpstate *fpstate,u64 mask,bool rstor); +#else +static inline void xfd_validate_state (struct fpstate *fpstate,u64 mask,bool rstor) { } +#endif +/* * Save processor xstate to xsave area. *
@@ -144,6 +150,7 @@ static inline void os_xsave(struct fpstate *fpstate)int err;WARN_ON_FPU (!alternatives_patched); +xfd_validate_state (fpstate, mask, false);XSTATE_XSAVE (&fpstate->regs.xsave, lmask, hmask, err);
@@ -156,12 +163,23 @@ static inline void os_xsave(struct fpstate *fpstate)* * Uses XRSTORS when XSAVES is used, XRSTOR otherwise. */ -static inline void os_xrstor (struct xregs_state *xstate,u64 mask) +static inline void os_xrstor (struct fpstate *fpstate,u64 mask) +{ +u32 lmask = mask; +u32 hmask = mask >>32 ; + +xfd_validate_state (fpstate, mask, true); +XSTATE_XRESTORE (&fpstate->regs.xsave, lmask, hmask); +} + +/* Restore of supervisor state. Does not require XFD */ +static inline void os_xrstor_supervisor (struct fpstate *fpstate) { +u64 mask =xfeatures_mask_supervisor ();u32 lmask = mask;u32 hmask = mask >>32 ; -XSTATE_XRESTORE (xstate, lmask, hmask); +XSTATE_XRESTORE (&fpstate->regs.xsave, lmask, hmask); }/*
@@ -184,11 +202,14 @@ static inline int xsave_to_user_sigframe(struct xregs_state __user *buf)* internally, e.g. PKRU. That's user space ABI and also required * to allow the signal handler to modify PKRU. */ -u64 mask = current->thread.fpu.fpstate->user_xfeatures; +struct fpstate *fpstate = current->thread.fpu.fpstate; +u64 mask = fpstate->user_xfeatures;u32 lmask = mask;u32 hmask = mask >>32 ;int err; +xfd_validate_state (fpstate, mask, false); +stac ();XSTATE_OP (XSAVE , buf, lmask, hmask, err);clac ();
@@ -206,6 +227,8 @@ static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64u32 hmask = mask >>32 ;int err; +xfd_validate_state (current->thread.fpu.fpstate, mask, true); +stac ();XSTATE_OP (XRSTOR , xstate, lmask, hmask, err);clac ();
@@ -217,12 +240,15 @@ static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64* Restore xstate from kernel space xsave area, return an error code instead of * an exception. */ -static inline int os_xrstor_safe (struct xregs_state *xstate,u64 mask) +static inline int os_xrstor_safe (struct fpstate *fpstate,u64 mask) { +struct xregs_state *xstate = &fpstate->regs.xsave;u32 lmask = mask;u32 hmask = mask >>32 ;int err; +/* Must enforce XFD update here */ +if (cpu_feature_enabled (X86_FEATURE_XSAVES ))XSTATE_OP (XRSTORS , xstate, lmask, hmask, err);else
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH 15/23] x86/fpu: Add sanity checks for XFD
Quoted 4 lines · Chang S. Bae
arch/x86/kernel/fpu/xstate.h +0
-0@@ -217,12 +240,15 @@ static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64* Restore xstate from kernel space xsave area, return an error code instead of * an exception. */
arch/x86/kernel/fpu/xstate.h static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64 · line 251
Quoted 10 lines
arch/x86/kernel/fpu/xstate.h +4
-1-static inline int os_xrstor_safe (struct xregs_state *xstate,u64 mask) +static inline int os_xrstor_safe (struct fpstate *fpstate,u64 mask) { +struct xregs_state *xstate = &fpstate->regs.xsave;u32 lmask = mask;u32 hmask = mask >>32 ;int err; +/* Must enforce XFD update here */ +
<--- something's missing here?
Quoted 6 lines
arch/x86/kernel/fpu/xstate.h +0
-0if (cpu_feature_enabled (X86_FEATURE_XSAVES ))XSTATE_OP (XRSTORS , xstate, lmask, hmask, err);else Signature · 1 line
2.17.1
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH 15/23] x86/fpu: Add sanity checks for XFD
Quoted 13 lines · Borislav Petkov
Quoted 12 lines · Chang S. Bae
arch/x86/kernel/fpu/xstate.h +2
-1@@ -217,12 +240,15 @@ static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64* Restore xstate from kernel space xsave area, return an error code instead of * an exception. */ -static inline int os_xrstor_safe (struct xregs_state *xstate,u64 mask) +static inline int os_xrstor_safe (struct fpstate *fpstate,u64 mask) { +struct xregs_state *xstate = &fpstate->regs.xsave;u32 lmask = mask;u32 hmask = mask >>32 ;int err;
Quoted 4 lines
arch/x86/kernel/fpu/xstate.h static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64 · line 251
Quoted 2 lines
arch/x86/kernel/fpu/xstate.h +2
-0+/* Must enforce XFD update here */ +<--- something's missing here?
That comment is replaced in the next patch which adds the actual XFD update. I added it as a reminder. Could probably go away.
Thanks,
tglx
Re: [PATCH 15/23] x86/fpu: Add sanity checks for XFD
In reply to Chang S. BaeQuoted 98 lines · Chang S. Bae
From: Thomas Gleixner <[email protected]>
Add debug functionality to ensure that the XFD MSR is up to date for XSAVE* and XRSTOR* operations.
Signed-off-by Thomas Gleixner <[email protected]>Signed-off-by Chang S. Bae <[email protected]>---
Changes from the tglx tree:
* Rename the xstate ops validation helper. (Dave Hansen)
* Update code comments. (Dave Hansen)--- arch/x86/kernel/fpu/core.c | 9 +++--- arch/x86/kernel/fpu/signal.c | 6 ++-- arch/x86/kernel/fpu/xstate.c | 55 ++++++++++++++++++++++++++++++++++++ arch/x86/kernel/fpu/xstate.h | 34 +++++++++++++++++++--- 4 files changed, 92 insertions(+), 12 deletions(-)arch/x86/kernel/fpu/core.c +4
-5@@ -166,7 +166,7 @@ void restore_fpregs_from_fpstate(struct fpstate *fpstate, u64 mask)*/ mask = fpu_kernel_cfg.max_features & mask; -os_xrstor (&fpstate->regs.xsave, mask); +os_xrstor (fpstate, mask); }else {if (use_fxsr ())fxrstor (&fpstate->regs.fxsave);@@ -537,7 +537,7 @@ void fpu__drop(struct fpu *fpu)static inline void restore_fpregs_from_init_fpstate (u64 features_mask) {if (use_xsave ()) -os_xrstor (&init_fpstate.regs.xsave, features_mask); +os_xrstor (&init_fpstate, features_mask);else if (use_fxsr ())fxrstor (&init_fpstate.regs.fxsave);else @@ -594,9 +594,8 @@ void fpu__clear_user_states(struct fpu *fpu)* corresponding registers. */ if (xfeatures_mask_supervisor () && - !fpregs_state_valid (fpu,smp_processor_id ())) { -os_xrstor (&fpu->fpstate->regs.xsave,xfeatures_mask_supervisor ()); - } + !fpregs_state_valid (fpu,smp_processor_id ())) +os_xrstor_supervisor (fpu->fpstate);/* Reset user states in registers. */ restore_fpregs_from_init_fpstate (XFEATURE_MASK_USER_RESTORE );arch/x86/kernel/fpu/signal.c +3
-3@@ -261,7 +261,7 @@ static int __restore_fpregs_from_user(void __user *buf, u64 ufeatures, ret =fxrstor_from_user_sigframe (buf);if (!ret &&unlikely (init_bv)) -os_xrstor (&init_fpstate.regs.xsave, init_bv); +os_xrstor (&init_fpstate, init_bv);return ret; }else if (use_fxsr ()) {return fxrstor_from_user_sigframe (buf);@@ -322,7 +322,7 @@ static bool restore_fpregs_from_user(void __user *buf, u64 xrestore,* been restored from a user buffer directly. */ if (test_thread_flag (TIF_NEED_FPU_LOAD ) &&xfeatures_mask_supervisor ()) -os_xrstor (&fpu->fpstate->regs.xsave,xfeatures_mask_supervisor ()); +os_xrstor_supervisor (fpu->fpstate);fpregs_mark_activate ();fpregs_unlock ();@@ -432,7 +432,7 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,u64 mask = user_xfeatures |xfeatures_mask_supervisor (); fpregs->xsave.header.xfeatures &= mask; - success = !os_xrstor_safe (&fpregs->xsave, + success = !os_xrstor_safe (fpu->fpstate, fpu_kernel_cfg.max_features); }else { success = !fxrstor_safe (&fpregs->fxsave);arch/x86/kernel/fpu/xstate.c +7
-0@@ -1301,6 +1301,61 @@ EXPORT_SYMBOL_GPL(fpstate_clear_xstate_component);#endif #ifdef CONFIG_X86_64 + +#ifdef CONFIG_X86_DEBUG_FPU +/* +* Ensure that a subsequent XSAVE* or XRSTOR* instruction with RFBM=@mask +* can safely operate on the @fpstate buffer. +*/ +static bool xstate_op_valid (struct fpstate *fpstate,u64 mask,bool rstor)
arch/x86/kernel/fpu/xstate.c EXPORT_SYMBOL_GPL(fpstate_clear_xstate_component); · line 1320
Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +10
-0+{ +u64 xfd =__this_cpu_read (xfd_state); + +if (fpstate->xfd == xfd) +return true; + +/* For current's fpstate the XFD state must be correct. */ +if (fpstate->xfd == current->thread.fpu.fpstate->xfd) +return false; +
Should this return true or is the comment confusing?
Quoted 134 lines
arch/x86/kernel/fpu/xstate.c +38
-0+/* +* XRSTOR(S) from init_fpstate are always correct as it will just +* bring all components into init state and not read from the +* buffer. XSAVE(S) raises #PF after init. +*/ +if (fpstate == &init_fpstate) +return rstor; + +/* +* XSAVE(S): clone(), fpu_swap_kvm_fpu() +* XRSTORS(S): fpu_swap_kvm_fpu() +*/ + +/* +* No XSAVE/XRSTOR instructions (except XSAVE itself) touch +* the buffer area for XFD-disabled state components. +*/ + mask &= ~xfd; + +/* +* Remove features which are valid in fpstate. They +* have space allocated in fpstate. +*/ + mask &= ~fpstate->xfeatures; + +/* +* Any remaining state components in 'mask' might be written +* by XSAVE/XRSTOR. Fail validation it found. +*/ + return !mask; +} + +void xfd_validate_state (struct fpstate *fpstate,u64 mask,bool rstor) +{ +WARN_ON_ONCE (!xstate_op_valid (fpstate, mask, rstor)); +} +#endif /* CONFIG_X86_DEBUG_FPU */ +static int validate_sigaltstack (unsigned int usize) {struct task_struct *thread, *leader = current->group_leader;arch/x86/kernel/fpu/xstate.h +30
-4@@ -130,6 +130,12 @@ static inline u64 xfeatures_mask_independent(void) : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \ : "memory") +#if defined(CONFIG_X86_64 ) && defined(CONFIG_X86_DEBUG_FPU ) +extern void xfd_validate_state (struct fpstate *fpstate,u64 mask,bool rstor); +#else +static inline void xfd_validate_state (struct fpstate *fpstate,u64 mask,bool rstor) { } +#endif +/* * Save processor xstate to xsave area. * @@ -144,6 +150,7 @@ static inline void os_xsave(struct fpstate *fpstate)int err;WARN_ON_FPU (!alternatives_patched); +xfd_validate_state (fpstate, mask, false);XSTATE_XSAVE (&fpstate->regs.xsave, lmask, hmask, err);@@ -156,12 +163,23 @@ static inline void os_xsave(struct fpstate *fpstate)* * Uses XRSTORS when XSAVES is used, XRSTOR otherwise. */ -static inline void os_xrstor (struct xregs_state *xstate,u64 mask) +static inline void os_xrstor (struct fpstate *fpstate,u64 mask) +{ +u32 lmask = mask; +u32 hmask = mask >>32 ; + +xfd_validate_state (fpstate, mask, true); +XSTATE_XRESTORE (&fpstate->regs.xsave, lmask, hmask); +} + +/* Restore of supervisor state. Does not require XFD */ +static inline void os_xrstor_supervisor (struct fpstate *fpstate) { +u64 mask =xfeatures_mask_supervisor ();u32 lmask = mask;u32 hmask = mask >>32 ; -XSTATE_XRESTORE (xstate, lmask, hmask); +XSTATE_XRESTORE (&fpstate->regs.xsave, lmask, hmask); }/* @@ -184,11 +202,14 @@ static inline int xsave_to_user_sigframe(struct xregs_state __user *buf)* internally, e.g. PKRU. That's user space ABI and also required * to allow the signal handler to modify PKRU. */ -u64 mask = current->thread.fpu.fpstate->user_xfeatures; +struct fpstate *fpstate = current->thread.fpu.fpstate; +u64 mask = fpstate->user_xfeatures;u32 lmask = mask;u32 hmask = mask >>32 ;int err; +xfd_validate_state (fpstate, mask, false); +stac ();XSTATE_OP (XSAVE , buf, lmask, hmask, err);clac ();@@ -206,6 +227,8 @@ static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64u32 hmask = mask >>32 ;int err; +xfd_validate_state (current->thread.fpu.fpstate, mask, true); +stac ();XSTATE_OP (XRSTOR , xstate, lmask, hmask, err);clac ();@@ -217,12 +240,15 @@ static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64* Restore xstate from kernel space xsave area, return an error code instead of * an exception. */ -static inline int os_xrstor_safe (struct xregs_state *xstate,u64 mask) +static inline int os_xrstor_safe (struct fpstate *fpstate,u64 mask) { +struct xregs_state *xstate = &fpstate->regs.xsave;u32 lmask = mask;u32 hmask = mask >>32 ;int err; +/* Must enforce XFD update here */ +if (cpu_feature_enabled (X86_FEATURE_XSAVES ))XSTATE_OP (XRSTORS , xstate, lmask, hmask, err);else
Re: [PATCH 15/23] x86/fpu: Add sanity checks for XFD
Quoted 10 lines · Mika Penttilä
Quoted 9 lines · Chang S. Bae
arch/x86/kernel/fpu/xstate.c +9
-0+#ifdef CONFIG_X86_DEBUG_FPU +/* +* Ensure that a subsequent XSAVE* or XRSTOR* instruction with RFBM=@mask +* can safely operate on the @fpstate buffer. +*/ +static bool xstate_op_valid (struct fpstate *fpstate,u64 mask,bool rstor) +{ +u64 xfd =__this_cpu_read (xfd_state); +
Quoted 8 lines
arch/x86/kernel/fpu/xstate.c EXPORT_SYMBOL_GPL(fpstate_clear_xstate_component); · line 1320
Quoted 7 lines
arch/x86/kernel/fpu/xstate.c +7
-0+if (fpstate->xfd == xfd) +return true; + +/* For current's fpstate the XFD state must be correct. */ +if (fpstate->xfd == current->thread.fpu.fpstate->xfd) +return false; +Should this return true or is the comment confusing?
Comment might be confusing. The logic here is:
If fpstate->xfd equal xfd then it's valid.
So the next check is whether fpstate is the same as current's fpstate. If that's the case then the result is invalid because for current's fpstate the first condition should be true. But if it is not true then the state is not valid.
Thanks,
tglx
Re: [PATCH 15/23] x86/fpu: Add sanity checks for XFD
Quoted 19 lines · Thomas Gleixner
Quoted 10 lines · Mika Penttilä
Quoted 9 lines · Chang S. Bae
arch/x86/kernel/fpu/xstate.c +9
-0+#ifdef CONFIG_X86_DEBUG_FPU +/* +* Ensure that a subsequent XSAVE* or XRSTOR* instruction with RFBM=@mask +* can safely operate on the @fpstate buffer. +*/ +static bool xstate_op_valid (struct fpstate *fpstate,u64 mask,bool rstor) +{ +u64 xfd =__this_cpu_read (xfd_state); +Quoted 8 lines
arch/x86/kernel/fpu/xstate.c EXPORT_SYMBOL_GPL(fpstate_clear_xstate_component); · line 1320
Quoted 7 lines
arch/x86/kernel/fpu/xstate.c +7
-0+if (fpstate->xfd == xfd) +return true; + +/* For current's fpstate the XFD state must be correct. */ +if (fpstate->xfd == current->thread.fpu.fpstate->xfd) +return false; +Should this return true or is the comment confusing?
Quoted 8 lines
Comment might be confusing. The logic here is:
If
fpstate->xfdequal xfd then it's valid.So the next check is whether fpstate is the same as current's fpstate. If that's the case then the result is invalid because for current's fpstate the first condition should be true. But if it is not true then the state is not valid.
Maybe I'm just a dummy today, but I spent way too much time looking at that line of code. Would a slightly longer comment help. Perhaps:
/* * The XFD MSR doesn't match the passed-in fpstate. * Make sure it at least matches current's fpstate. * If not, XFD was set up wrong and is invalid. */
Below is an even more exhaustive look at it. Feel free to ignore.
--
In most cases, we are doing a normal old context switch. XFD is already set up by this point and the @fpstate is pointed to by current->thread.fpu. fpstate->xfd==xfd, life is good and we skip the rest of the checks.
If they don't match, we have some more checks to do. Here's a bit-by-bit walk through it. Just like the hardware MSR at this point, assume that only single bit can be set in any of the xfd's.
MSR | fpstate | cur->fpstate | valid
-------------------------------------
0 | 0 | x | 1 // MSR matches @fpstate 0 | 1 | 0 | 1 // MSR matches cur->fpstate 0 | 1 | 1 | 0 <- *** MSR matches nothing! 1 | 0 | 0 | 0 <- *** MSR matches nothing! 1 | 0 | 1 | 1 // MSR matches cur->fpstate 1 | 1 | x | 1 // MSR matches @fpstate
(BTW, "valid" here means either return true from earlier, or
falling through to the later xstate_op_valid() checks)
If a bit is *CLEAR* in the MSR, then it better be *CLEAR* in either fpstate->xfd or current->...fpstate->xfd.
If a bit is *SET* in the MSR, then it better be *SET* in eitherfpstate->xfd or current->...fpstate->xfd.
It's bad if the MSR doesn't match *either* fpstate->xfd or current->...fpstate->xfd. Because, if it does not match, how did we get here? What random fpstate was the MSR set up to work with?
[tip: x86/fpu] x86/fpu: Add sanity checks for XFD
In reply to Chang S. BaeThe following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 5529acf47ec31ece0815f69d43f5e6a1e485a0f3 Gitweb: https://git.kernel.org/tip/5529acf47ec31ece0815f69d43f5e6a1e485a0f3 Author: Thomas Gleixner <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:19 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:52:35 +02:00
x86/fpu: Add sanity checks for XFD
Add debug functionality to ensure that the XFD MSR is up to date for XSAVE* and XRSTOR* operations.
[ tglx: Improve comment. ]
--- arch/x86/kernel/fpu/core.c | 9 ++--- arch/x86/kernel/fpu/signal.c | 6 ++-- arch/x86/kernel/fpu/xstate.c | 58 +++++++++++++++++++++++++++++++++++- arch/x86/kernel/fpu/xstate.h | 34 ++++++++++++++++++--- 4 files changed, 95 insertions(+), 12 deletions(-)
arch/x86/kernel/fpu/core.c +4 -5
@@ -166,7 +166,7 @@ void restore_fpregs_from_fpstate(struct fpstate *fpstate, u64 mask)*/ mask = fpu_kernel_cfg.max_features & mask; -os_xrstor (&fpstate->regs.xsave, mask); +os_xrstor (fpstate, mask); }else {if (use_fxsr ())fxrstor (&fpstate->regs.fxsave);
@@ -534,7 +534,7 @@ void fpu__drop(struct fpu *fpu)static inline void restore_fpregs_from_init_fpstate (u64 features_mask) {if (use_xsave ()) -os_xrstor (&init_fpstate.regs.xsave, features_mask); +os_xrstor (&init_fpstate, features_mask);else if (use_fxsr ())fxrstor (&init_fpstate.regs.fxsave);else
@@ -591,9 +591,8 @@ void fpu__clear_user_states(struct fpu *fpu)* corresponding registers. */ if (xfeatures_mask_supervisor () && - !fpregs_state_valid (fpu,smp_processor_id ())) { -os_xrstor (&fpu->fpstate->regs.xsave,xfeatures_mask_supervisor ()); - } + !fpregs_state_valid (fpu,smp_processor_id ())) +os_xrstor_supervisor (fpu->fpstate);/* Reset user states in registers. */ restore_fpregs_from_init_fpstate (XFEATURE_MASK_USER_RESTORE );
arch/x86/kernel/fpu/signal.c +3 -3
@@ -261,7 +261,7 @@ static int __restore_fpregs_from_user(void __user *buf, u64 ufeatures, ret =fxrstor_from_user_sigframe (buf);if (!ret &&unlikely (init_bv)) -os_xrstor (&init_fpstate.regs.xsave, init_bv); +os_xrstor (&init_fpstate, init_bv);return ret; }else if (use_fxsr ()) {return fxrstor_from_user_sigframe (buf);
@@ -322,7 +322,7 @@ retry:* been restored from a user buffer directly. */ if (test_thread_flag (TIF_NEED_FPU_LOAD ) &&xfeatures_mask_supervisor ()) -os_xrstor (&fpu->fpstate->regs.xsave,xfeatures_mask_supervisor ()); +os_xrstor_supervisor (fpu->fpstate);fpregs_mark_activate ();fpregs_unlock ();
@@ -432,7 +432,7 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,u64 mask = user_xfeatures |xfeatures_mask_supervisor (); fpregs->xsave.header.xfeatures &= mask; - success = !os_xrstor_safe (&fpregs->xsave, + success = !os_xrstor_safe (fpu->fpstate, fpu_kernel_cfg.max_features); }else { success = !fxrstor_safe (&fpregs->fxsave);
arch/x86/kernel/fpu/xstate.c +58 -0
64 lines not on this page · raw message
arch/x86/kernel/fpu/xstate.h +30 -4
@@ -130,6 +130,12 @@ static inline u64 xfeatures_mask_independent(void) : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \ : "memory") +#if defined(CONFIG_X86_64 ) && defined(CONFIG_X86_DEBUG_FPU ) +extern void xfd_validate_state (struct fpstate *fpstate,u64 mask,bool rstor); +#else +static inline void xfd_validate_state (struct fpstate *fpstate,u64 mask,bool rstor) { } +#endif +/* * Save processor xstate to xsave area. *
@@ -144,6 +150,7 @@ static inline void os_xsave(struct fpstate *fpstate)int err;WARN_ON_FPU (!alternatives_patched); +xfd_validate_state (fpstate, mask, false);XSTATE_XSAVE (&fpstate->regs.xsave, lmask, hmask, err);
@@ -156,12 +163,23 @@ static inline void os_xsave(struct fpstate *fpstate)* * Uses XRSTORS when XSAVES is used, XRSTOR otherwise. */ -static inline void os_xrstor (struct xregs_state *xstate,u64 mask) +static inline void os_xrstor (struct fpstate *fpstate,u64 mask) +{ +u32 lmask = mask; +u32 hmask = mask >>32 ; + +xfd_validate_state (fpstate, mask, true); +XSTATE_XRESTORE (&fpstate->regs.xsave, lmask, hmask); +} + +/* Restore of supervisor state. Does not require XFD */ +static inline void os_xrstor_supervisor (struct fpstate *fpstate) { +u64 mask =xfeatures_mask_supervisor ();u32 lmask = mask;u32 hmask = mask >>32 ; -XSTATE_XRESTORE (xstate, lmask, hmask); +XSTATE_XRESTORE (&fpstate->regs.xsave, lmask, hmask); }/*
@@ -184,11 +202,14 @@ static inline int xsave_to_user_sigframe(struct xregs_state __user *buf)* internally, e.g. PKRU. That's user space ABI and also required * to allow the signal handler to modify PKRU. */ -u64 mask = current->thread.fpu.fpstate->user_xfeatures; +struct fpstate *fpstate = current->thread.fpu.fpstate; +u64 mask = fpstate->user_xfeatures;u32 lmask = mask;u32 hmask = mask >>32 ;int err; +xfd_validate_state (fpstate, mask, false); +stac ();XSTATE_OP (XSAVE , buf, lmask, hmask, err);clac ();
@@ -206,6 +227,8 @@ static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64u32 hmask = mask >>32 ;int err; +xfd_validate_state (current->thread.fpu.fpstate, mask, true); +stac ();XSTATE_OP (XRSTOR , xstate, lmask, hmask, err);clac ();
@@ -217,12 +240,15 @@ static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64* Restore xstate from kernel space xsave area, return an error code instead of * an exception. */ -static inline int os_xrstor_safe (struct xregs_state *xstate,u64 mask) +static inline int os_xrstor_safe (struct fpstate *fpstate,u64 mask) { +struct xregs_state *xstate = &fpstate->regs.xsave;u32 lmask = mask;u32 hmask = mask >>32 ;int err; +/* Must enforce XFD update here */ +if (cpu_feature_enabled (X86_FEATURE_XSAVES ))XSTATE_OP (XRSTORS , xstate, lmask, hmask, err);else
PATCH 16/23 x86/fpu: Update XFD state where required
In reply to Chang S. BaeThe IA32_XFD_MSR allows to arm #NM traps for XSTATE components which are enabled in XCR0. The register has to be restored before the tasks XSTATE is restored. The life time rules are the same as for FPU state.
XFD is updated on return to userspace only when the FPU state of the task is not up to date in the registers. It's updated before the XRSTORS so that eventually enabled dynamic features are restored as well and not brought into init state.
Also in signal handling for restoring FPU state from user space the correctness of the XFD state has to be ensured.
Add it to CPU initialization and resume as well.
--- arch/x86/kernel/fpu/context.h | 2 ++ arch/x86/kernel/fpu/core.c | 25 +++++++++++++++++++++++++ arch/x86/kernel/fpu/signal.c | 2 ++ arch/x86/kernel/fpu/xstate.c | 12 ++++++++++++ arch/x86/kernel/fpu/xstate.h | 19 ++++++++++++++++++- 5 files changed, 59 insertions(+), 1 deletion(-)
arch/x86/kernel/fpu/context.h +2 -0
@@ -69,6 +69,8 @@ static inline void fpregs_restore_userregs(void)* correct because it was either set in switch_to() or in * flush_thread(). So it is excluded because it might be * not up to date in current->thread.fpu.xsave state. +* +* XFD state is handled in restore_fpregs_from_fpstate(). */ restore_fpregs_from_fpstate (fpu->fpstate,XFEATURE_MASK_FPSTATE );
arch/x86/kernel/fpu/core.c +25 -0
@@ -155,6 +155,23 @@ void restore_fpregs_from_fpstate(struct fpstate *fpstate, u64 mask) }if (use_xsave ()) { +/* +* Dynamically enabled features are enabled in XCR0, but +* usage requires also that the corresponding bits in XFD +* are cleared. If the bits are set then using a related +* instruction will raise #NM. This allows to do the +* allocation of the larger FPU buffer lazy from #NM or if +* the task has no permission to kill it which would happen +* via #UD if the feature is disabled in XCR0. +* +* XFD state is following the same life time rules as +* XSTATE and to restore state correctly XFD has to be +* updated before XRSTORS otherwise the component would +* stay in or go into init state even if the bits are set +* in fpstate::regs::xsave::xfeatures. +*/ +xfd_update_state (fpstate); +/* * Restoring state always needs to modify all features * which are in @mask even if the current task cannot use
@@ -244,7 +261,15 @@ int fpu_swap_kvm_fpstate(struct fpu_guest *guest_fpu, bool enter_guest,if (!cur_fps->is_confidential) { restore_mask &=XFEATURE_MASK_FPSTATE ; +/* Includes XFD update */ restore_fpregs_from_fpstate (cur_fps, restore_mask); + }else { +/* +* XSTATE is restored by firmware from encrypted +* memory. Make sure XFD state is correct while +* running with guest fpstate +*/ +xfd_update_state (cur_fps); }fpregs_mark_activate ();
arch/x86/kernel/fpu/signal.c +2 -0
@@ -282,6 +282,8 @@ static bool restore_fpregs_from_user(void __user *buf, u64 xrestore, retry:fpregs_lock (); +/* Ensure that XFD is up to date */ +xfd_update_state (fpu->fpstate);pagefault_disable (); ret =__restore_fpregs_from_user (buf, fpu->fpstate->user_xfeatures, xrestore, fx_only);
arch/x86/kernel/fpu/xstate.c +12 -0
@@ -136,6 +136,15 @@ void fpu__init_cpu_xstate(void)cr4_set_bits (X86_CR4_OSXSAVE ); +/* +* Must happen after CR4 setup and before xsetbv() to allow KVM +* lazy passthrough. Write independent of the dynamic state static +* key as that does not work on the boot CPU. This also ensures +* that any stale state is wiped out from XFD. +*/ +if (cpu_feature_enabled (X86_FEATURE_XFD )) +wrmsrl (MSR_IA32_XFD , init_fpstate.xfd); +/* * XCR_XFEATURE_ENABLED_MASK (aka. XCR0) sets user features * managed by XSAVE{C, OPT, S} and XRSTOR{S}. Only XSAVE user
@@ -875,6 +884,9 @@ void fpu__resume_cpu(void)wrmsrl (MSR_IA32_XSS ,xfeatures_mask_supervisor () |xfeatures_mask_independent ()); } + +if (fpu_state_size_dynamic ()) +wrmsrl (MSR_IA32_XFD , current->thread.fpu.fpstate->xfd); }/*
arch/x86/kernel/fpu/xstate.h +18 -1
@@ -136,6 +136,22 @@ extern void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor);static inline void xfd_validate_state (struct fpstate *fpstate,u64 mask,bool rstor) { }#endif +#ifdef CONFIG_X86_64 +static inline void xfd_update_state (struct fpstate *fpstate) +{ +if (fpu_state_size_dynamic ()) { +u64 xfd = fpstate->xfd; + +if (__this_cpu_read (xfd_state) != xfd) { +wrmsrl (MSR_IA32_XFD , xfd); +__this_cpu_write (xfd_state, xfd); + } + } +} +#else +static inline void xfd_update_state (struct fpstate *fpstate) { } +#endif +/* * Save processor xstate to xsave area. *
@@ -247,7 +263,8 @@ static inline int os_xrstor_safe(struct fpstate *fpstate, u64 mask)u32 hmask = mask >>32 ;int err; -/* Must enforce XFD update here */ +/* Ensure that XFD is up to date */ +xfd_update_state (fpstate);if (cpu_feature_enabled (X86_FEATURE_XSAVES ))XSTATE_OP (XRSTORS , xstate, lmask, hmask, err);
Signature · 1 line
2.17.1
[tip: x86/fpu] x86/fpu: Update XFD state where required
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 672365477ae8afca5a1cca98c1deb733235e4525 Gitweb: https://git.kernel.org/tip/672365477ae8afca5a1cca98c1deb733235e4525 Author: Chang S. Bae <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:20 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:53:02 +02:00
x86/fpu: Update XFD state where required
The IA32_XFD_MSR allows to arm #NM traps for XSTATE components which are enabled in XCR0. The register has to be restored before the tasks XSTATE is restored. The life time rules are the same as for FPU state.
XFD is updated on return to userspace only when the FPU state of the task is not up to date in the registers. It's updated before the XRSTORS so that eventually enabled dynamic features are restored as well and not brought into init state.
Also in signal handling for restoring FPU state from user space the correctness of the XFD state has to be ensured.
Add it to CPU initialization and resume as well.
--- arch/x86/kernel/fpu/context.h | 2 ++ arch/x86/kernel/fpu/core.c | 28 +++++++++++++++++++++++++++- arch/x86/kernel/fpu/signal.c | 2 ++ arch/x86/kernel/fpu/xstate.c | 12 ++++++++++++ arch/x86/kernel/fpu/xstate.h | 19 ++++++++++++++++++- 5 files changed, 61 insertions(+), 2 deletions(-)
arch/x86/kernel/fpu/context.h +2 -0
@@ -69,6 +69,8 @@ static inline void fpregs_restore_userregs(void)* correct because it was either set in switch_to() or in * flush_thread(). So it is excluded because it might be * not up to date in current->thread.fpu.xsave state. +* +* XFD state is handled in restore_fpregs_from_fpstate(). */ restore_fpregs_from_fpstate (fpu->fpstate,XFEATURE_MASK_FPSTATE );
arch/x86/kernel/fpu/core.c +27 -1
@@ -156,6 +156,23 @@ void restore_fpregs_from_fpstate(struct fpstate *fpstate, u64 mask)if (use_xsave ()) {/* +* Dynamically enabled features are enabled in XCR0, but +* usage requires also that the corresponding bits in XFD +* are cleared. If the bits are set then using a related +* instruction will raise #NM. This allows to do the +* allocation of the larger FPU buffer lazy from #NM or if +* the task has no permission to kill it which would happen +* via #UD if the feature is disabled in XCR0. +* +* XFD state is following the same life time rules as +* XSTATE and to restore state correctly XFD has to be +* updated before XRSTORS otherwise the component would +* stay in or go into init state even if the bits are set +* in fpstate::regs::xsave::xfeatures. +*/ +xfd_update_state (fpstate); + +/* * Restoring state always needs to modify all features * which are in @mask even if the current task cannot use * extended features.
@@ -241,8 +258,17 @@ int fpu_swap_kvm_fpstate(struct fpu_guest *guest_fpu, bool enter_guest) cur_fps = fpu->fpstate; -if (!cur_fps->is_confidential) +if (!cur_fps->is_confidential) { +/* Includes XFD update */ restore_fpregs_from_fpstate (cur_fps,XFEATURE_MASK_FPSTATE ); + }else { +/* +* XSTATE is restored by firmware from encrypted +* memory. Make sure XFD state is correct while +* running with guest fpstate +*/ +xfd_update_state (cur_fps); + }fpregs_mark_activate ();fpregs_unlock ();
arch/x86/kernel/fpu/signal.c +2 -0
@@ -282,6 +282,8 @@ static bool restore_fpregs_from_user(void __user *buf, u64 xrestore, retry:fpregs_lock (); +/* Ensure that XFD is up to date */ +xfd_update_state (fpu->fpstate);pagefault_disable (); ret =__restore_fpregs_from_user (buf, fpu->fpstate->user_xfeatures, xrestore, fx_only);
arch/x86/kernel/fpu/xstate.c +12 -0
@@ -137,6 +137,15 @@ void fpu__init_cpu_xstate(void)cr4_set_bits (X86_CR4_OSXSAVE );/* +* Must happen after CR4 setup and before xsetbv() to allow KVM +* lazy passthrough. Write independent of the dynamic state static +* key as that does not work on the boot CPU. This also ensures +* that any stale state is wiped out from XFD. +*/ +if (cpu_feature_enabled (X86_FEATURE_XFD )) +wrmsrl (MSR_IA32_XFD , init_fpstate.xfd); + +/* * XCR_XFEATURE_ENABLED_MASK (aka. XCR0) sets user features * managed by XSAVE{C, OPT, S} and XRSTOR{S}. Only XSAVE user * states can be set here.
@@ -875,6 +884,9 @@ void fpu__resume_cpu(void)wrmsrl (MSR_IA32_XSS ,xfeatures_mask_supervisor () |xfeatures_mask_independent ()); } + +if (fpu_state_size_dynamic ()) +wrmsrl (MSR_IA32_XFD , current->thread.fpu.fpstate->xfd); }/*
arch/x86/kernel/fpu/xstate.h +18 -1
@@ -136,6 +136,22 @@ extern void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor);static inline void xfd_validate_state (struct fpstate *fpstate,u64 mask,bool rstor) { }#endif +#ifdef CONFIG_X86_64 +static inline void xfd_update_state (struct fpstate *fpstate) +{ +if (fpu_state_size_dynamic ()) { +u64 xfd = fpstate->xfd; + +if (__this_cpu_read (xfd_state) != xfd) { +wrmsrl (MSR_IA32_XFD , xfd); +__this_cpu_write (xfd_state, xfd); + } + } +} +#else +static inline void xfd_update_state (struct fpstate *fpstate) { } +#endif +/* * Save processor xstate to xsave area. *
@@ -247,7 +263,8 @@ static inline int os_xrstor_safe(struct fpstate *fpstate, u64 mask)u32 hmask = mask >>32 ;int err; -/* Must enforce XFD update here */ +/* Ensure that XFD is up to date */ +xfd_update_state (fpstate);if (cpu_feature_enabled (X86_FEATURE_XSAVES ))XSTATE_OP (XRSTORS , xstate, lmask, hmask, err);
PATCH 17/23 x86/fpu/xstate: Add XFD #NM handler
In reply to Chang S. BaeIf the XFD MSR has feature bits set then #NM will be raised when user space attempts to use an instruction related to one of these features.
When the task has no permissions to use that feature, raise SIGILL, which is the same behavior as #UD.
If the task has permissions, calculate the new buffer size for the extended feature set and allocate a larger fpstate. In the unlikely case that vzalloc() fails, SIGSEGV is raised.
The allocation function will be added in the next step. Provide a stub which fails for now.
--- arch/x86/include/asm/fpu/xstate.h | 2 ++ arch/x86/kernel/fpu/xstate.c | 47 +++++++++++++++++++++++++++++++ arch/x86/kernel/traps.c | 38 +++++++++++++++++++++++++ 3 files changed, 87 insertions(+)
arch/x86/include/asm/fpu/xstate.h +2 -0
@@ -99,6 +99,8 @@ int xfeature_size(int xfeature_nr);void xsaves (struct xregs_state *xsave,u64 mask);void xrstors (struct xregs_state *xsave,u64 mask); +int xfd_enable_feature (u64 xfd_err); +#ifdef CONFIG_X86_64 DECLARE_STATIC_KEY_FALSE (__fpu_state_size_dynamic);#endif
arch/x86/kernel/fpu/xstate.c +47 -0
53 lines not on this page · raw message
arch/x86/kernel/traps.c +38 -0
@@ -1108,10 +1108,48 @@ DEFINE_IDTENTRY(exc_spurious_interrupt_bug)*/ } +static bool handle_xfd_event (struct pt_regs *regs) +{ +u64 xfd_err; +int err; + +if (!IS_ENABLED (CONFIG_X86_64 ) || !cpu_feature_enabled (X86_FEATURE_XFD )) +return false; + +rdmsrl (MSR_IA32_XFD_ERR , xfd_err); +if (!xfd_err) +return false; + +wrmsrl (MSR_IA32_XFD_ERR ,0 ); + +/* Die if that happens in kernel space */ +if (WARN_ON (!user_mode (regs))) +return false; + +local_irq_enable (); + + err =xfd_enable_feature (xfd_err); + +switch (err) { +case -EPERM : +force_sig_fault (SIGILL ,ILL_ILLOPC ,error_get_trap_addr (regs)); +break ; +case -EFAULT : +force_sig (SIGSEGV ); +break ; + } + +local_irq_disable (); +return true; +} +DEFINE_IDTENTRY (exc_device_not_available) {unsigned long cr0 =read_cr0 (); +if (handle_xfd_event (regs)) +return ; +#ifdef CONFIG_MATH_EMULATION if (!boot_cpu_has (X86_FEATURE_FPU ) && (cr0 &X86_CR0_EM )) {struct math_emu_info info = { };
Signature · 1 line
2.17.1
[tip: x86/fpu] x86/fpu/xstate: Add XFD #NM handler
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 783e87b404956f8958657aed8a6a72aa98d5b7e1 Gitweb: https://git.kernel.org/tip/783e87b404956f8958657aed8a6a72aa98d5b7e1 Author: Chang S. Bae <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:21 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:53:02 +02:00
x86/fpu/xstate: Add XFD #NM handler
If the XFD MSR has feature bits set then #NM will be raised when user space attempts to use an instruction related to one of these features.
When the task has no permissions to use that feature, raise SIGILL, which is the same behavior as #UD.
If the task has permissions, calculate the new buffer size for the extended feature set and allocate a larger fpstate. In the unlikely case that vzalloc() fails, SIGSEGV is raised.
The allocation function will be added in the next step. Provide a stub which fails for now.
[ tglx: Updated serialization ]
--- arch/x86/include/asm/fpu/xstate.h | 2 +- arch/x86/kernel/fpu/xstate.c | 47 ++++++++++++++++++++++++++++++- arch/x86/kernel/traps.c | 38 ++++++++++++++++++++++++- 3 files changed, 87 insertions(+)
arch/x86/include/asm/fpu/xstate.h +2 -0
@@ -99,6 +99,8 @@ int xfeature_size(int xfeature_nr);void xsaves (struct xregs_state *xsave,u64 mask);void xrstors (struct xregs_state *xsave,u64 mask); +int xfd_enable_feature (u64 xfd_err); +#ifdef CONFIG_X86_64 DECLARE_STATIC_KEY_FALSE (__fpu_state_size_dynamic);#endif
arch/x86/kernel/fpu/xstate.c +47 -0
53 lines not on this page · raw message
arch/x86/kernel/traps.c +38 -0
@@ -1108,10 +1108,48 @@ DEFINE_IDTENTRY(exc_spurious_interrupt_bug)*/ } +static bool handle_xfd_event (struct pt_regs *regs) +{ +u64 xfd_err; +int err; + +if (!IS_ENABLED (CONFIG_X86_64 ) || !cpu_feature_enabled (X86_FEATURE_XFD )) +return false; + +rdmsrl (MSR_IA32_XFD_ERR , xfd_err); +if (!xfd_err) +return false; + +wrmsrl (MSR_IA32_XFD_ERR ,0 ); + +/* Die if that happens in kernel space */ +if (WARN_ON (!user_mode (regs))) +return false; + +local_irq_enable (); + + err =xfd_enable_feature (xfd_err); + +switch (err) { +case -EPERM : +force_sig_fault (SIGILL ,ILL_ILLOPC ,error_get_trap_addr (regs)); +break ; +case -EFAULT : +force_sig (SIGSEGV ); +break ; + } + +local_irq_disable (); +return true; +} +DEFINE_IDTENTRY (exc_device_not_available) {unsigned long cr0 =read_cr0 (); +if (handle_xfd_event (regs)) +return ; +#ifdef CONFIG_MATH_EMULATION if (!boot_cpu_has (X86_FEATURE_FPU ) && (cr0 &X86_CR0_EM )) {struct math_emu_info info = { };
PATCH 18/23 x86/fpu/xstate: Add fpstate_realloc()/free()
In reply to Chang S. BaeThe fpstate embedded in struct fpu is the default state for storing the FPU registers. It's sized so that the default supported features can be stored. For dynamically enabled features the register buffer is too small.
The #NM handler detects first use of a feature which is disabled in the XFD MSR. After handling permission checks it recalculates the size for kernel space and user space state and invokes fpstate_realloc() which tries to reallocate fpstate and install it.
Provide the allocator function which checks whether the current buffer size is sufficient and if not allocates one. If allocation is successful the new fpstate is initialized with the new features and sizes and the now enabled features is removed from the task's XFD mask.
realloc_fpstate() uses vzalloc(). If use of this mechanism grows to re-allocate buffers larger than 64KB, a more sophisticated allocation scheme that includes purpose-built reclaim capability might be justified.
--- arch/x86/include/asm/fpu/api.h | 7 +++ arch/x86/kernel/fpu/xstate.c | 97 +++++++++++++++++++++++++++++++--- arch/x86/kernel/process.c | 10 ++++ 3 files changed, 106 insertions(+), 8 deletions(-)
arch/x86/include/asm/fpu/api.h +7 -0
@@ -130,6 +130,13 @@ static inline void fpstate_init_soft(struct swregs_state *soft) {}/* State tracking */ DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx); +/* Process cleanup */ +#ifdef CONFIG_X86_64 +extern void fpstate_free (struct fpu *fpu); +#else +static inline void fpstate_free (struct fpu *fpu) { } +#endif +/* fpstate-related functions which are exported to KVM */ extern void fpstate_clear_xstate_component (struct fpstate *fps,unsigned int xfeature);
arch/x86/kernel/fpu/xstate.c +89 -8
127 lines not on this page · raw message
arch/x86/kernel/process.c +10 -0
@@ -32,6 +32,7 @@#include <asm/mwait.h> #include <asm/fpu/api.h> #include <asm/fpu/sched.h> +#include <asm/fpu/xstate.h> #include <asm/debugreg.h> #include <asm/nmi.h> #include <asm/tlbflush.h>
@@ -90,9 +91,18 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)#endif /* Drop the copied pointer to current's fpstate */ dst->thread.fpu.fpstate =NULL ; +return 0 ; } +#ifdef CONFIG_X86_64 +void arch_release_task_struct (struct task_struct *tsk) +{ +if (fpu_state_size_dynamic ()) +fpstate_free (&tsk->thread.fpu); +} +#endif +/* * Free thread data structures etc.. */
Signature · 1 line
2.17.1
[tip: x86/fpu] x86/fpu/xstate: Add fpstate_realloc()/free()
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 500afbf645a040a39e1af0dba2fdf6ebf224bd47 Gitweb: https://git.kernel.org/tip/500afbf645a040a39e1af0dba2fdf6ebf224bd47 Author: Chang S. Bae <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:22 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:53:02 +02:00
x86/fpu/xstate: Add fpstate_realloc()/free()
The fpstate embedded in struct fpu is the default state for storing the FPU registers. It's sized so that the default supported features can be stored. For dynamically enabled features the register buffer is too small.
The #NM handler detects first use of a feature which is disabled in the XFD MSR. After handling permission checks it recalculates the size for kernel space and user space state and invokes fpstate_realloc() which tries to reallocate fpstate and install it.
Provide the allocator function which checks whether the current buffer size is sufficient and if not allocates one. If allocation is successful the new fpstate is initialized with the new features and sizes and the now enabled features is removed from the task's XFD mask.
realloc_fpstate() uses vzalloc(). If use of this mechanism grows to re-allocate buffers larger than 64KB, a more sophisticated allocation scheme that includes purpose-built reclaim capability might be justified.
--- arch/x86/include/asm/fpu/api.h | 7 ++- arch/x86/kernel/fpu/xstate.c | 97 ++++++++++++++++++++++++++++++--- arch/x86/kernel/process.c | 10 +++- 3 files changed, 106 insertions(+), 8 deletions(-)
arch/x86/include/asm/fpu/api.h +7 -0
@@ -130,6 +130,13 @@ static inline void fpstate_init_soft(struct swregs_state *soft) {}/* State tracking */ DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx); +/* Process cleanup */ +#ifdef CONFIG_X86_64 +extern void fpstate_free (struct fpu *fpu); +#else +static inline void fpstate_free (struct fpu *fpu) { } +#endif +/* fpstate-related functions which are exported to KVM */ extern void fpstate_clear_xstate_component (struct fpstate *fps,unsigned int xfeature);
arch/x86/kernel/fpu/xstate.c +89 -8
127 lines not on this page · raw message
arch/x86/kernel/process.c +10 -0
@@ -32,6 +32,7 @@#include <asm/mwait.h> #include <asm/fpu/api.h> #include <asm/fpu/sched.h> +#include <asm/fpu/xstate.h> #include <asm/debugreg.h> #include <asm/nmi.h> #include <asm/tlbflush.h>
@@ -90,9 +91,18 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)#endif /* Drop the copied pointer to current's fpstate */ dst->thread.fpu.fpstate =NULL ; +return 0 ; } +#ifdef CONFIG_X86_64 +void arch_release_task_struct (struct task_struct *tsk) +{ +if (fpu_state_size_dynamic ()) +fpstate_free (&tsk->thread.fpu); +} +#endif +/* * Free thread data structures etc.. */
PATCH 19/23 x86/fpu/xstate: Prepare XSAVE feature table for gaps in state component numbers
In reply to Chang S. BaeThe kernel checks at boot time which features are available by walking a XSAVE feature table which contains the CPUID feature bit numbers which need to be checked whether a feature is available on a CPU or not. So far the feature numbers have been linear, but AMX will create a gap which the current code cannot handle.
Make the table entries explicitly indexed and adjust the loop code accordingly to prepare for that.
No functional change.
--- arch/x86/kernel/fpu/xstate.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-)
arch/x86/kernel/fpu/xstate.c +16 -13
@@ -53,18 +53,18 @@ static const char *xfeature_names[] = "unknown xstate feature" , }; -static short xsave_cpuid_features[]__initdata = { -X86_FEATURE_FPU , -X86_FEATURE_XMM , -X86_FEATURE_AVX , -X86_FEATURE_MPX , -X86_FEATURE_MPX , -X86_FEATURE_AVX512F , -X86_FEATURE_AVX512F , -X86_FEATURE_AVX512F , -X86_FEATURE_INTEL_PT , -X86_FEATURE_PKU , -X86_FEATURE_ENQCMD , +static unsigned short xsave_cpuid_features[]__initdata = { + [XFEATURE_FP ] =X86_FEATURE_FPU , + [XFEATURE_SSE ] =X86_FEATURE_XMM , + [XFEATURE_YMM ] =X86_FEATURE_AVX , + [XFEATURE_BNDREGS ] =X86_FEATURE_MPX , + [XFEATURE_BNDCSR ] =X86_FEATURE_MPX , + [XFEATURE_OPMASK ] =X86_FEATURE_AVX512F , + [XFEATURE_ZMM_Hi256] =X86_FEATURE_AVX512F , + [XFEATURE_Hi16_ZMM] =X86_FEATURE_AVX512F , + [XFEATURE_PT_UNIMPLEMENTED_SO_FAR ] =X86_FEATURE_INTEL_PT , + [XFEATURE_PKRU ] =X86_FEATURE_PKU , + [XFEATURE_PASID ] =X86_FEATURE_ENQCMD , }; static unsigned int xstate_offsets[XFEATURE_MAX] __ro_after_init =
@@ -809,7 +809,10 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)* Clear XSAVE features that are disabled in the normal CPUID. */ for (i =0 ; i <ARRAY_SIZE (xsave_cpuid_features); i++) { -if (!boot_cpu_has (xsave_cpuid_features[i])) +unsigned short cid = xsave_cpuid_features[i]; + +/* Careful: X86_FEATURE_FPU is 0! */ +if ((i !=XFEATURE_FP && !cid) || !boot_cpu_has (cid)) fpu_kernel_cfg.max_features &= ~BIT_ULL (i); }
Signature · 1 line
2.17.1
[tip: x86/fpu] x86/fpu/xstate: Prepare XSAVE feature table for gaps in state component numbers
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 70c3f1671b0cbc386b387f1de33b7837e276a195 Gitweb: https://git.kernel.org/tip/70c3f1671b0cbc386b387f1de33b7837e276a195 Author: Chang S. Bae <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:23 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:53:02 +02:00
x86/fpu/xstate: Prepare XSAVE feature table for gaps in state component numbers
The kernel checks at boot time which features are available by walking a XSAVE feature table which contains the CPUID feature bit numbers which need to be checked whether a feature is available on a CPU or not. So far the feature numbers have been linear, but AMX will create a gap which the current code cannot handle.
Make the table entries explicitly indexed and adjust the loop code accordingly to prepare for that.
No functional change.
--- arch/x86/kernel/fpu/xstate.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-)
arch/x86/kernel/fpu/xstate.c +16 -13
@@ -53,18 +53,18 @@ static const char *xfeature_names[] = "unknown xstate feature" , }; -static short xsave_cpuid_features[]__initdata = { -X86_FEATURE_FPU , -X86_FEATURE_XMM , -X86_FEATURE_AVX , -X86_FEATURE_MPX , -X86_FEATURE_MPX , -X86_FEATURE_AVX512F , -X86_FEATURE_AVX512F , -X86_FEATURE_AVX512F , -X86_FEATURE_INTEL_PT , -X86_FEATURE_PKU , -X86_FEATURE_ENQCMD , +static unsigned short xsave_cpuid_features[]__initdata = { + [XFEATURE_FP ] =X86_FEATURE_FPU , + [XFEATURE_SSE ] =X86_FEATURE_XMM , + [XFEATURE_YMM ] =X86_FEATURE_AVX , + [XFEATURE_BNDREGS ] =X86_FEATURE_MPX , + [XFEATURE_BNDCSR ] =X86_FEATURE_MPX , + [XFEATURE_OPMASK ] =X86_FEATURE_AVX512F , + [XFEATURE_ZMM_Hi256] =X86_FEATURE_AVX512F , + [XFEATURE_Hi16_ZMM] =X86_FEATURE_AVX512F , + [XFEATURE_PT_UNIMPLEMENTED_SO_FAR ] =X86_FEATURE_INTEL_PT , + [XFEATURE_PKRU ] =X86_FEATURE_PKU , + [XFEATURE_PASID ] =X86_FEATURE_ENQCMD , }; static unsigned int xstate_offsets[XFEATURE_MAX] __ro_after_init =
@@ -809,7 +809,10 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)* Clear XSAVE features that are disabled in the normal CPUID. */ for (i =0 ; i <ARRAY_SIZE (xsave_cpuid_features); i++) { -if (!boot_cpu_has (xsave_cpuid_features[i])) +unsigned short cid = xsave_cpuid_features[i]; + +/* Careful: X86_FEATURE_FPU is 0! */ +if ((i !=XFEATURE_FP && !cid) || !boot_cpu_has (cid)) fpu_kernel_cfg.max_features &= ~BIT_ULL (i); }
PATCH 20/23 x86/fpu/amx: Define AMX state components and have it used for boot-time checks
In reply to Chang S. BaeThe XSTATE initialization uses check_xstate_against_struct() to sanity check the size of XSTATE-enabled features. AMX is a XSAVE-enabled feature, and its size is not hard-coded but discoverable at run-time via CPUID.
The AMX state is composed of state components 17 and 18, which are all user state components. The first component is the XTILECFG state of a 64-byte tile-related control register. The state component 18, called XTILEDATA, contains the actual tile data, and the state size varies on implementations. The architectural maximum, as defined in the CPUID(0x1d, 1): EAX[15:0], is a byte less than 64KB. The first implementation supports 8KB.
Check the XTILEDATA state size dynamically. The feature introduces the new tile register, TMM. Define one register struct only and read the number of registers from CPUID. Cross-check the overall size with CPUID again.
---
Changes from the tglx tree:
* Make AMX_TILE depend on XFD.
* Update the changelog.
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/fpu/types.h | 32 ++++++++++++ arch/x86/include/asm/fpu/xstate.h | 2 + arch/x86/kernel/fpu/xstate.c | 80 +++++++++++++++++++++++++++++- 4 files changed, 114 insertions(+), 1 deletion(-)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -299,6 +299,7 @@/* Intel-defined CPU features, CPUID level 0x00000007:1 (EAX), word 12 */ #define X86_FEATURE_AVX_VNNI (12*32+ 4)/* AVX VNNI instructions */ #define X86_FEATURE_AVX512_BF16 (12*32+ 5)/* AVX512 BFLOAT16 instructions */ +#define X86_FEATURE_AMX_TILE (18*32+24)/* AMX tile Support */ /* AMD-defined CPU features, CPUID level 0x80000008 (EBX), word 13 */ #define X86_FEATURE_CLZERO (13*32+ 0)/* CLZERO instruction */
arch/x86/include/asm/fpu/types.h +32 -0
@@ -120,6 +120,9 @@ enum xfeature {XFEATURE_RSRVD_COMP_13 ,XFEATURE_RSRVD_COMP_14 ,XFEATURE_LBR , +XFEATURE_RSRVD_COMP_16 , +XFEATURE_XTILE_CFG , +XFEATURE_XTILE_DATA , XFEATURE_MAX, };
@@ -136,12 +139,21 @@ enum xfeature {#define XFEATURE_MASK_PKRU (1 << XFEATURE_PKRU)#define XFEATURE_MASK_PASID (1 << XFEATURE_PASID)#define XFEATURE_MASK_LBR (1 << XFEATURE_LBR) +#define XFEATURE_MASK_XTILE_CFG (1 << XFEATURE_XTILE_CFG) +#define XFEATURE_MASK_XTILE_DATA (1 << XFEATURE_XTILE_DATA)#define XFEATURE_MASK_FPSSE (XFEATURE_MASK_FP | XFEATURE_MASK_SSE)#define XFEATURE_MASK_AVX512 (XFEATURE_MASK_OPMASK \ | XFEATURE_MASK_ZMM_Hi256 \ | XFEATURE_MASK_Hi16_ZMM) +#ifdef CONFIG_X86_64 +# define XFEATURE_MASK_XTILE (XFEATURE_MASK_XTILE_DATA \ + | XFEATURE_MASK_XTILE_CFG) +#else +# define XFEATURE_MASK_XTILE (0) +#endif +#define FIRST_EXTENDED_XFEATURE XFEATURE_YMMstruct reg_128_bit {
@@ -153,6 +165,9 @@ struct reg_256_bit {struct reg_512_bit {u8 regbytes[512 /8 ]; }; +struct reg_1024_byte { +u8 regbytes[1024 ]; +};/* * State component 2:
@@ -255,6 +270,23 @@ struct arch_lbr_state {u64 ler_to;u64 ler_info;struct lbr_entry entries[]; +}; + +/* +* State component 17: 64-byte tile configuration register. +*/ +struct xtile_cfg { +u64 tcfg[8 ]; +} __packed; + +/* +* State component 18: 1KB tile data register. +* Each register represents 16 64-byte rows of the matrix +* data. But the number of registers depends on the actual +* implementation. +*/ +struct xtile_data { +struct reg_1024_byte tmm; } __packed;/*
arch/x86/include/asm/fpu/xstate.h +2 -0
@@ -14,6 +14,8 @@#define XSTATE_CPUID 0x0000000d +#define TILE_CPUID 0x0000001d +#define FXSAVE_SIZE 512#define XSAVE_HDR_SIZE 64
arch/x86/kernel/fpu/xstate.c +79 -1
116 lines not on this page · raw message
Signature · 1 line
2.17.1
[tip: x86/fpu] x86/fpu/amx: Define AMX state components and have it used for boot-time checks
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: eec2113eabd92b7bfbaf1033fa82dc8eb4951203 Gitweb: https://git.kernel.org/tip/eec2113eabd92b7bfbaf1033fa82dc8eb4951203 Author: Chang S. Bae <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:24 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:53:02 +02:00
x86/fpu/amx: Define AMX state components and have it used for boot-time checks
The XSTATE initialization uses check_xstate_against_struct() to sanity check the size of XSTATE-enabled features. AMX is a XSAVE-enabled feature, and its size is not hard-coded but discoverable at run-time via CPUID.
The AMX state is composed of state components 17 and 18, which are all user state components. The first component is the XTILECFG state of a 64-byte tile-related control register. The state component 18, called XTILEDATA, contains the actual tile data, and the state size varies on implementations. The architectural maximum, as defined in the CPUID(0x1d, 1): EAX[15:0], is a byte less than 64KB. The first implementation supports 8KB.
Check the XTILEDATA state size dynamically. The feature introduces the new tile register, TMM. Define one register struct only and read the number of registers from CPUID. Cross-check the overall size with CPUID again.
--- arch/x86/include/asm/cpufeatures.h | 1 +- arch/x86/include/asm/fpu/types.h | 32 ++++++++++++- arch/x86/include/asm/fpu/xstate.h | 2 +- arch/x86/kernel/fpu/xstate.c | 80 ++++++++++++++++++++++++++++- 4 files changed, 114 insertions(+), 1 deletion(-)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -299,6 +299,7 @@/* Intel-defined CPU features, CPUID level 0x00000007:1 (EAX), word 12 */ #define X86_FEATURE_AVX_VNNI (12*32+ 4)/* AVX VNNI instructions */ #define X86_FEATURE_AVX512_BF16 (12*32+ 5)/* AVX512 BFLOAT16 instructions */ +#define X86_FEATURE_AMX_TILE (18*32+24)/* AMX tile Support */ /* AMD-defined CPU features, CPUID level 0x80000008 (EBX), word 13 */ #define X86_FEATURE_CLZERO (13*32+ 0)/* CLZERO instruction */
arch/x86/include/asm/fpu/types.h +32 -0
@@ -120,6 +120,9 @@ enum xfeature {XFEATURE_RSRVD_COMP_13 ,XFEATURE_RSRVD_COMP_14 ,XFEATURE_LBR , +XFEATURE_RSRVD_COMP_16 , +XFEATURE_XTILE_CFG , +XFEATURE_XTILE_DATA , XFEATURE_MAX, };
@@ -136,12 +139,21 @@ enum xfeature {#define XFEATURE_MASK_PKRU (1 << XFEATURE_PKRU)#define XFEATURE_MASK_PASID (1 << XFEATURE_PASID)#define XFEATURE_MASK_LBR (1 << XFEATURE_LBR) +#define XFEATURE_MASK_XTILE_CFG (1 << XFEATURE_XTILE_CFG) +#define XFEATURE_MASK_XTILE_DATA (1 << XFEATURE_XTILE_DATA)#define XFEATURE_MASK_FPSSE (XFEATURE_MASK_FP | XFEATURE_MASK_SSE)#define XFEATURE_MASK_AVX512 (XFEATURE_MASK_OPMASK \ | XFEATURE_MASK_ZMM_Hi256 \ | XFEATURE_MASK_Hi16_ZMM) +#ifdef CONFIG_X86_64 +# define XFEATURE_MASK_XTILE (XFEATURE_MASK_XTILE_DATA \ + | XFEATURE_MASK_XTILE_CFG) +#else +# define XFEATURE_MASK_XTILE (0) +#endif +#define FIRST_EXTENDED_XFEATURE XFEATURE_YMMstruct reg_128_bit {
@@ -153,6 +165,9 @@ struct reg_256_bit {struct reg_512_bit {u8 regbytes[512 /8 ]; }; +struct reg_1024_byte { +u8 regbytes[1024 ]; +};/* * State component 2:
@@ -255,6 +270,23 @@ struct arch_lbr_state {u64 ler_to;u64 ler_info;struct lbr_entry entries[]; +}; + +/* +* State component 17: 64-byte tile configuration register. +*/ +struct xtile_cfg { +u64 tcfg[8 ]; +} __packed; + +/* +* State component 18: 1KB tile data register. +* Each register represents 16 64-byte rows of the matrix +* data. But the number of registers depends on the actual +* implementation. +*/ +struct xtile_data { +struct reg_1024_byte tmm; } __packed;/*
arch/x86/include/asm/fpu/xstate.h +2 -0
@@ -14,6 +14,8 @@#define XSTATE_CPUID 0x0000000d +#define TILE_CPUID 0x0000001d +#define FXSAVE_SIZE 512#define XSAVE_HDR_SIZE 64
arch/x86/kernel/fpu/xstate.c +79 -1
116 lines not on this page · raw message
PATCH 21/23 x86/fpu: Calculate the default sizes independently
In reply to Chang S. BaeWhen dynamically enabled states are supported the maximum and default sizes for the kernel buffers and user space interfaces are not longer identical.
Put the necessary calculations in place which only take the default enabled features into account.
---
Changes from the tglx tree:
* Fix to work when XSAVES is not available.
--- arch/x86/kernel/fpu/xstate.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-)
arch/x86/kernel/fpu/xstate.c +21 -9
@@ -781,35 +781,40 @@ static bool __init is_supported_xstate_size(unsigned int test_xstate_size)static int __init init_xstate_size (void ) {/* Recompute the context size for enabled features: */ -unsigned int user_size, kernel_size; +unsigned int user_size, kernel_size, kernel_default_size; +bool compacted =cpu_feature_enabled (X86_FEATURE_XSAVES );/* Uncompacted user space size */ user_size =get_xsave_size_user ();/* * XSAVES kernel size includes supervisor states and -* uses compacted format. +* uses compacted format when available. * * XSAVE does not support supervisor states so * kernel and user size is identical. */ -if (cpu_feature_enabled (X86_FEATURE_XSAVES )) +if (compacted) kernel_size =get_xsaves_size_no_independent ();else kernel_size = user_size; -/* Ensure we have the space to store all enabled features. */ -if (!is_supported_xstate_size (kernel_size)) + kernel_default_size = +xstate_calculate_size (fpu_kernel_cfg.default_features, compacted); + +/* Ensure we have the space to store all default enabled features. */ +if (!is_supported_xstate_size (kernel_default_size))return -EINVAL ;if (!paranoid_xstate_size_valid (kernel_size))return -EINVAL ; -/* Keep it the same for now */ fpu_kernel_cfg.max_size = kernel_size; - fpu_kernel_cfg.default_size = kernel_size; fpu_user_cfg.max_size = user_size; - fpu_user_cfg.default_size = user_size; + + fpu_kernel_cfg.default_size = kernel_default_size; + fpu_user_cfg.default_size = +xstate_calculate_size (fpu_user_cfg.default_features, false);return 0 ; }
@@ -894,15 +899,21 @@ void __init fpu__init_system_xstate(unsigned int legacy_size) fpu_kernel_cfg.max_features &= ~BIT_ULL (i); } +if (!cpu_feature_enabled (X86_FEATURE_XFD )) + fpu_kernel_cfg.max_features &= ~XFEATURE_MASK_USER_DYNAMIC ; + fpu_kernel_cfg.max_features &=XFEATURE_MASK_USER_SUPPORTED |XFEATURE_MASK_SUPERVISOR_SUPPORTED ; fpu_user_cfg.max_features = fpu_kernel_cfg.max_features; fpu_user_cfg.max_features &=XFEATURE_MASK_USER_SUPPORTED ; -/* Identical for now */ +/* Clean out dynamic features from default */ fpu_kernel_cfg.default_features = fpu_kernel_cfg.max_features; + fpu_kernel_cfg.default_features &= ~XFEATURE_MASK_USER_DYNAMIC ; + fpu_user_cfg.default_features = fpu_user_cfg.max_features; + fpu_user_cfg.default_features &= ~XFEATURE_MASK_USER_DYNAMIC ;/* Store it for paranoia check at the end */ xfeatures = fpu_kernel_cfg.max_features;
@@ -913,6 +924,7 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)if (err) goto out_disable; +/* Reset the state for the current task */ fpstate_reset (¤t->thread.fpu);/*
Signature · 1 line
2.17.1
[tip: x86/fpu] x86/fpu: Calculate the default sizes independently
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 2ae996e0c1a38ca57a52438ab9deec6761dcba62 Gitweb: https://git.kernel.org/tip/2ae996e0c1a38ca57a52438ab9deec6761dcba62 Author: Chang S. Bae <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:25 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:53:02 +02:00
x86/fpu: Calculate the default sizes independently
When dynamically enabled states are supported the maximum and default sizes for the kernel buffers and user space interfaces are not longer identical.
Put the necessary calculations in place which only take the default enabled features into account.
--- arch/x86/kernel/fpu/xstate.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-)
arch/x86/kernel/fpu/xstate.c +21 -9
@@ -781,35 +781,40 @@ static bool __init is_supported_xstate_size(unsigned int test_xstate_size)static int __init init_xstate_size (void ) {/* Recompute the context size for enabled features: */ -unsigned int user_size, kernel_size; +unsigned int user_size, kernel_size, kernel_default_size; +bool compacted =cpu_feature_enabled (X86_FEATURE_XSAVES );/* Uncompacted user space size */ user_size =get_xsave_size_user ();/* * XSAVES kernel size includes supervisor states and -* uses compacted format. +* uses compacted format when available. * * XSAVE does not support supervisor states so * kernel and user size is identical. */ -if (cpu_feature_enabled (X86_FEATURE_XSAVES )) +if (compacted) kernel_size =get_xsaves_size_no_independent ();else kernel_size = user_size; -/* Ensure we have the space to store all enabled features. */ -if (!is_supported_xstate_size (kernel_size)) + kernel_default_size = +xstate_calculate_size (fpu_kernel_cfg.default_features, compacted); + +/* Ensure we have the space to store all default enabled features. */ +if (!is_supported_xstate_size (kernel_default_size))return -EINVAL ;if (!paranoid_xstate_size_valid (kernel_size))return -EINVAL ; -/* Keep it the same for now */ fpu_kernel_cfg.max_size = kernel_size; - fpu_kernel_cfg.default_size = kernel_size; fpu_user_cfg.max_size = user_size; - fpu_user_cfg.default_size = user_size; + + fpu_kernel_cfg.default_size = kernel_default_size; + fpu_user_cfg.default_size = +xstate_calculate_size (fpu_user_cfg.default_features, false);return 0 ; }
@@ -894,15 +899,21 @@ void __init fpu__init_system_xstate(unsigned int legacy_size) fpu_kernel_cfg.max_features &= ~BIT_ULL (i); } +if (!cpu_feature_enabled (X86_FEATURE_XFD )) + fpu_kernel_cfg.max_features &= ~XFEATURE_MASK_USER_DYNAMIC ; + fpu_kernel_cfg.max_features &=XFEATURE_MASK_USER_SUPPORTED |XFEATURE_MASK_SUPERVISOR_SUPPORTED ; fpu_user_cfg.max_features = fpu_kernel_cfg.max_features; fpu_user_cfg.max_features &=XFEATURE_MASK_USER_SUPPORTED ; -/* Identical for now */ +/* Clean out dynamic features from default */ fpu_kernel_cfg.default_features = fpu_kernel_cfg.max_features; + fpu_kernel_cfg.default_features &= ~XFEATURE_MASK_USER_DYNAMIC ; + fpu_user_cfg.default_features = fpu_user_cfg.max_features; + fpu_user_cfg.default_features &= ~XFEATURE_MASK_USER_DYNAMIC ;/* Store it for paranoia check at the end */ xfeatures = fpu_kernel_cfg.max_features;
@@ -913,6 +924,7 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)if (err) goto out_disable; +/* Reset the state for the current task */ fpstate_reset (¤t->thread.fpu);/*
PATCH 22/23 x86/fpu: Add XFD handling for dynamic states
In reply to Chang S. BaeTo handle the dynamic sizing of buffers on first use the XFD MSR has to be armed. Store the delta between the maximum available and the default feature bits in init_fpstate where it can be retrieved for task creation.
If the delta is non zero then dynamic features are enabled. This needs also to enable the static key which guards the XFD updates. This is delayed to an initcall because the FPU setup runs before jump labels are initialized.
--- arch/x86/kernel/fpu/xstate.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-)
arch/x86/kernel/fpu/xstate.c +27 -1
@@ -835,6 +835,12 @@ static void __init fpu__init_disable_system_xstate(unsigned int legacy_size) fpu_user_cfg.max_size = legacy_size; fpu_user_cfg.default_size = legacy_size; +/* +* Prevent enabling the static branch which enables writes to the +* XFD MSR. +*/ + init_fpstate.xfd =0 ; +fpstate_reset (¤t->thread.fpu); }
@@ -918,6 +924,14 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)/* Store it for paranoia check at the end */ xfeatures = fpu_kernel_cfg.max_features; +/* +* Initialize the default XFD state in initfp_state and enable the +* dynamic sizing mechanism if dynamic states are available. The +* static key cannot be enabled here because this runs before +* jump_label_init(). This is delayed to an initcall. +*/ + init_fpstate.xfd = fpu_user_cfg.max_features &XFEATURE_MASK_USER_DYNAMIC ; +/* Enable xstate instructions to be able to continue with initialization: */ fpu__init_cpu_xstate (); err =init_xstate_size ();
@@ -1463,9 +1477,21 @@ void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor) }#endif /* CONFIG_X86_DEBUG_FPU */ +static int __init xfd_update_static_branch (void ) +{ +/* +* If init_fpstate.xfd has bits set then dynamic features are +* available and the dynamic sizing must be enabled. +*/ +if (init_fpstate.xfd) +static_branch_enable (&__fpu_state_size_dynamic); +return 0 ; +} +arch_initcall (xfd_update_static_branch) + void fpstate_free(struct fpu *fpu) { -if (fpu->fpstate || fpu->fpstate != &fpu->__fpstate) +if (fpu->fpstate && fpu->fpstate != &fpu->__fpstate)vfree (fpu->fpstate); }
Signature · 1 line
2.17.1
[tip: x86/fpu] x86/fpu: Add XFD handling for dynamic states
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: db3e7321b4b84b1cb39598ff79b90d1252481378 Gitweb: https://git.kernel.org/tip/db3e7321b4b84b1cb39598ff79b90d1252481378 Author: Chang S. Bae <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:26 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:53:03 +02:00
x86/fpu: Add XFD handling for dynamic states
To handle the dynamic sizing of buffers on first use the XFD MSR has to be armed. Store the delta between the maximum available and the default feature bits in init_fpstate where it can be retrieved for task creation.
If the delta is non zero then dynamic features are enabled. This needs also to enable the static key which guards the XFD updates. This is delayed to an initcall because the FPU setup runs before jump labels are initialized.
--- arch/x86/kernel/fpu/xstate.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-)
arch/x86/kernel/fpu/xstate.c +27 -1
@@ -835,6 +835,12 @@ static void __init fpu__init_disable_system_xstate(unsigned int legacy_size) fpu_user_cfg.max_size = legacy_size; fpu_user_cfg.default_size = legacy_size; +/* +* Prevent enabling the static branch which enables writes to the +* XFD MSR. +*/ + init_fpstate.xfd =0 ; +fpstate_reset (¤t->thread.fpu); }
@@ -918,6 +924,14 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)/* Store it for paranoia check at the end */ xfeatures = fpu_kernel_cfg.max_features; +/* +* Initialize the default XFD state in initfp_state and enable the +* dynamic sizing mechanism if dynamic states are available. The +* static key cannot be enabled here because this runs before +* jump_label_init(). This is delayed to an initcall. +*/ + init_fpstate.xfd = fpu_user_cfg.max_features &XFEATURE_MASK_USER_DYNAMIC ; +/* Enable xstate instructions to be able to continue with initialization: */ fpu__init_cpu_xstate (); err =init_xstate_size ();
@@ -1466,9 +1480,21 @@ void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor) }#endif /* CONFIG_X86_DEBUG_FPU */ +static int __init xfd_update_static_branch (void ) +{ +/* +* If init_fpstate.xfd has bits set then dynamic features are +* available and the dynamic sizing must be enabled. +*/ +if (init_fpstate.xfd) +static_branch_enable (&__fpu_state_size_dynamic); +return 0 ; +} +arch_initcall (xfd_update_static_branch) + void fpstate_free(struct fpu *fpu) { -if (fpu->fpstate || fpu->fpstate != &fpu->__fpstate) +if (fpu->fpstate && fpu->fpstate != &fpu->__fpstate)vfree (fpu->fpstate); }
PATCH 23/23 x86/fpu/amx: Enable the AMX feature in 64-bit mode
In reply to Chang S. BaeAdd the AMX state components in XFEATURE_MASK_USER_SUPPORTED and the TILE_DATA component to the dynamic states and update the permission check table accordingly.
This is only effective on 64 bit kernels as for 32bit kernels XFEATURE_MASK_TILE is defined as 0.
TILE_DATA is caller-saved state and the only dynamic state. Add build time sanity check to ensure the assumption that every dynamic feature is caller- saved.
Make AMX state depend on XFD as it is dynamic feature.
---
Changes from the tglx tree:
* Add compile time sanity check. (Dave Hansen)
* Make AMX depend on XFD.
--- arch/x86/include/asm/fpu/xstate.h | 5 +++-- arch/x86/kernel/cpu/cpuid-deps.c | 1 + arch/x86/kernel/fpu/core.c | 6 ++++++ arch/x86/kernel/fpu/xstate.c | 5 +++-- 4 files changed, 13 insertions(+), 4 deletions(-)
arch/x86/include/asm/fpu/xstate.h +3 -2
@@ -35,7 +35,8 @@ XFEATURE_MASK_Hi16_ZMM | \XFEATURE_MASK_PKRU | \XFEATURE_MASK_BNDREGS | \ - XFEATURE_MASK_BNDCSR) +XFEATURE_MASK_BNDCSR | \ + XFEATURE_MASK_XTILE)/* * Features which are restored when returning to user space.
@@ -46,7 +47,7 @@ (XFEATURE_MASK_USER_SUPPORTED & ~XFEATURE_MASK_PKRU ) /* Features which are dynamically enabled for a process on request */ -#define XFEATURE_MASK_USER_DYNAMIC 0ULL +#define XFEATURE_MASK_USER_DYNAMIC XFEATURE_MASK_XTILE_DATA/* All currently supported supervisor features */ #define XFEATURE_MASK_SUPERVISOR_SUPPORTED (XFEATURE_MASK_PASID)
arch/x86/kernel/cpu/cpuid-deps.c +1 -0
@@ -76,6 +76,7 @@ static const struct cpuid_dep cpuid_deps[] = { { X86_FEATURE_SGX1, X86_FEATURE_SGX }, { X86_FEATURE_SGX2, X86_FEATURE_SGX1 }, { X86_FEATURE_XFD, X86_FEATURE_XSAVES }, + { X86_FEATURE_AMX_TILE, X86_FEATURE_XFD }, {} };
arch/x86/kernel/fpu/core.c +6 -0
@@ -496,6 +496,12 @@ int fpu_clone(struct task_struct *dst, unsigned long clone_flags)return 0 ; } +/* +* If a new feature is added, ensure all dynamic features are +* caller-saved from here! +*/ +BUILD_BUG_ON (XFEATURE_MASK_USER_DYNAMIC !=XFEATURE_MASK_XTILE_DATA ); +/* * Save the default portion of the current FPU state into the * clone. Assume all dynamic features to be defined as caller-
arch/x86/kernel/fpu/xstate.c +3 -2
@@ -404,7 +404,8 @@ static __init void os_xrstor_booting(struct xregs_state *xstate)XFEATURE_MASK_PKRU | \XFEATURE_MASK_BNDREGS | \XFEATURE_MASK_BNDCSR | \ - XFEATURE_MASK_PASID) +XFEATURE_MASK_PASID | \ + XFEATURE_MASK_XTILE)/* * setup the xstate image representing the init state
@@ -1633,7 +1634,7 @@ static int __xstate_request_perm(u64 permitted, u64 requested)* Permissions array to map facilities with more than one component */ static const u64 xstate_prctl_req[XFEATURE_MAX ] = { -/* [XFEATURE_XTILE_DATA] = XFEATURE_MASK_XTILE, */ + [XFEATURE_XTILE_DATA ] =XFEATURE_MASK_XTILE_DATA , };static int xstate_request_perm (unsigned long idx)
Signature · 1 line
2.17.1
[tip: x86/fpu] x86/fpu/amx: Enable the AMX feature in 64-bit mode
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 2308ee57d93d896618dd65c996429c9d3e469fe0 Gitweb: https://git.kernel.org/tip/2308ee57d93d896618dd65c996429c9d3e469fe0 Author: Chang S. Bae <[email protected]> AuthorDate: Thu, 21 Oct 2021 15:55:27 -07:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 26 Oct 2021 10:53:03 +02:00
x86/fpu/amx: Enable the AMX feature in 64-bit mode
Add the AMX state components in XFEATURE_MASK_USER_SUPPORTED and the TILE_DATA component to the dynamic states and update the permission check table accordingly.
This is only effective on 64 bit kernels as for 32bit kernels XFEATURE_MASK_TILE is defined as 0.
TILE_DATA is caller-saved state and the only dynamic state. Add build time sanity check to ensure the assumption that every dynamic feature is caller- saved.
Make AMX state depend on XFD as it is dynamic feature.
--- arch/x86/include/asm/fpu/xstate.h | 5 +++-- arch/x86/kernel/cpu/cpuid-deps.c | 1 + arch/x86/kernel/fpu/core.c | 6 ++++++ arch/x86/kernel/fpu/xstate.c | 5 +++-- 4 files changed, 13 insertions(+), 4 deletions(-)
arch/x86/include/asm/fpu/xstate.h +3 -2
@@ -35,7 +35,8 @@ XFEATURE_MASK_Hi16_ZMM | \XFEATURE_MASK_PKRU | \XFEATURE_MASK_BNDREGS | \ - XFEATURE_MASK_BNDCSR) +XFEATURE_MASK_BNDCSR | \ + XFEATURE_MASK_XTILE)/* * Features which are restored when returning to user space.
@@ -46,7 +47,7 @@ (XFEATURE_MASK_USER_SUPPORTED & ~XFEATURE_MASK_PKRU ) /* Features which are dynamically enabled for a process on request */ -#define XFEATURE_MASK_USER_DYNAMIC 0ULL +#define XFEATURE_MASK_USER_DYNAMIC XFEATURE_MASK_XTILE_DATA/* All currently supported supervisor features */ #define XFEATURE_MASK_SUPERVISOR_SUPPORTED (XFEATURE_MASK_PASID)
arch/x86/kernel/cpu/cpuid-deps.c +1 -0
@@ -76,6 +76,7 @@ static const struct cpuid_dep cpuid_deps[] = { { X86_FEATURE_SGX1, X86_FEATURE_SGX }, { X86_FEATURE_SGX2, X86_FEATURE_SGX1 }, { X86_FEATURE_XFD, X86_FEATURE_XSAVES }, + { X86_FEATURE_AMX_TILE, X86_FEATURE_XFD }, {} };
arch/x86/kernel/fpu/core.c +6 -0
@@ -495,6 +495,12 @@ int fpu_clone(struct task_struct *dst, unsigned long clone_flags) }/* +* If a new feature is added, ensure all dynamic features are +* caller-saved from here! +*/ +BUILD_BUG_ON (XFEATURE_MASK_USER_DYNAMIC !=XFEATURE_MASK_XTILE_DATA ); + +/* * Save the default portion of the current FPU state into the * clone. Assume all dynamic features to be defined as caller- * saved, which enables skipping both the expansion of fpstate
arch/x86/kernel/fpu/xstate.c +3 -2
@@ -404,7 +404,8 @@ static __init void os_xrstor_booting(struct xregs_state *xstate)XFEATURE_MASK_PKRU | \XFEATURE_MASK_BNDREGS | \XFEATURE_MASK_BNDCSR | \ - XFEATURE_MASK_PASID) +XFEATURE_MASK_PASID | \ + XFEATURE_MASK_XTILE)/* * setup the xstate image representing the init state
@@ -1636,7 +1637,7 @@ static int __xstate_request_perm(u64 permitted, u64 requested)* Permissions array to map facilities with more than one component */ static const u64 xstate_prctl_req[XFEATURE_MAX ] = { -/* [XFEATURE_XTILE_DATA] = XFEATURE_MASK_XTILE, */ + [XFEATURE_XTILE_DATA ] =XFEATURE_MASK_XTILE_DATA , };static int xstate_request_perm (unsigned long idx)