x86: load FPU registers on return to userland
Subsystems (7)
PATCH v5 1/1 x86: load FPU registers on return to userland
This is a refurbished series originally started by by Rik van Riel. The goal is load the FPU registers on return to userland and not on every context switch. By this optimisation we can:
- avoid loading the registers if the task stays in kernel and does not return to userland
- make kernel_fpu_begin() cheaper: it only saves the registers on the first invocation. The second invocation does not need save them again.
To access the FPU registers in kernel we need:
- disable preemption to avoid that the scheduler switches tasks. By doing so it would set TIF_NEED_FPU_LOAD and the FPU registers would be not valid.
- disable BH because the softirq might use kernel_fpu_begin() and then set TIF_NEED_FPU_LOAD instead loading the FPU registers on completion.
v4…v5:
Rebased on top of a fix, noticed a problem with XSAVES and then redid the restore on sig return (patch #26 to #28).
I don't like very much the sig save+restore thing that we are doing. It has been always like that. I *think* that this is just because we have nowhere to stash the FPU state while we are handling the signal. We could add another fpu->state for the signal handler and avoid the thing. Debian code-search revealed that criu' is using it (and I didn't figure out why). Nothing else (that is packaged in Debian). Maybe we could get rid of this and if criu' would then use a dedicated interface for its needs rather the signal interface that happen to do what it wants :)
v3…v4:
It has been suggested to remove the `initialized' member of the struct fpu because it should not required be needed with lazy-FPU-restore and would make the review easier. This is the first part of the series, the second is basically the rebase of the v3 queue. As a result, the diffstat became negative (which wasn't the case in previous version) :) I tried to incorporate all the review comments that came up, some of them were "outdated" after the removal of the `initialized' member. I'm sorry should I missed any.
v1…v3:
v2 was never posted. I followed the idea to completely decouple PKRU from xstate. This didn't quite work and made a few things complicated. One obvious required fixup is copy_fpstate_to_sigframe() where the PKRU state needs to be fiddled into xstate. This required another xfeatures_mask so that the sanity checks were performed and xstate_offsets would be computed. Additionally ptrace also reads/sets xstate in order to get/set the register and PKRU is one of them. So this would need some fiddle, too.
In v3 I dropped that decouple idea. I also learned that the wrpkru instruction is not privileged and so caching it in kernel does not work. Instead I keep PKRU in xstate area and load it at context switch time while the remaining registers are deferred (until return to userland). The offset of PKRU within xstate is enumerated at boot time so why not use it.
Rik van Riel (5):
x86/fpu: Add (__)make_fpregs_active helpers
x86/fpu: Eager switch PKRU state
x86/fpu: Always store the registers in copy_fpstate_to_sigframe() x86/fpu: Prepare copy_fpstate_to_sigframe() for TIF_NEED_FPU_LOAD x86/fpu: Defer FPU state load until return to userspace
Sebastian Andrzej Siewior (24):
x86/fpu: Use ULL for shift in xfeature_uncompacted_offset() x86/entry/32: Remove asm/math_emu.h include
x86/entry: Remove _TIF_ALLWORK_MASK
x86/pkeys: Make init_pkru_value static
x86/fpu: add might_fault() to user_insn()
x86/fpu: Update comment for __raw_xsave_addr()
x86/fpu: don't export __kernel_fpu_{begin|end}()
x86/fpu: Remove fpu->initialized usage in __fpu__restore_sig() x86/fpu: Remove fpu__restore()
x86/fpu: Remove preempt_disable() in fpu__clear() x86/fpu: Always init the `state' in fpu__clear()
x86/fpu: Remove fpu->initialized usage in copy_fpstate_to_sigframe() x86/fpu: Don't save fxregs for ia32 frames in
copy_fpstate_to_sigframe()
x86/fpu: Remove fpu->initialized
x86/fpu: Remove user_fpu_begin()
x86/fpu: Make __raw_xsave_addr() use feature number instead of mask x86/fpu: Make get_xsave_field_ptr() and get_xsave_addr() use feature
number instead of mask
x86/fpu: Only write PKRU if it is different from current x86/pkeys: Don't check if PKRU is zero before writting it x86/entry: Add TIF_NEED_FPU_LOAD
x86/fpu: Update xstate's PKRU value on write_pkru() x86/fpu: Inline copy_user_to_fpregs_zeroing()
x86/fpu: Let __fpu__restore_sig() restore the !32bit+fxsr frame from
kernel memory
x86/fpu: Merge the two code paths in __fpu__restore_sig()
Documentation/preempt-locking.txt | 1 - arch/x86/entry/common.c | 8 ++ arch/x86/ia32/ia32_signal.c | 17 +-- arch/x86/include/asm/efi.h | 6 +- arch/x86/include/asm/fpu/api.h | 47 ++++-- arch/x86/include/asm/fpu/internal.h | 152 ++++++++------------ arch/x86/include/asm/fpu/signal.h | 2 +- arch/x86/include/asm/fpu/types.h | 9 -- arch/x86/include/asm/fpu/xstate.h | 5 +- arch/x86/include/asm/pgtable.h | 20 ++- arch/x86/include/asm/special_insns.h | 13 +- arch/x86/include/asm/thread_info.h | 10 +- arch/x86/include/asm/trace/fpu.h | 8 +- arch/x86/kernel/fpu/core.c | 199 ++++++++++++-------------- arch/x86/kernel/fpu/init.c | 2 - arch/x86/kernel/fpu/regset.c | 24 +--- arch/x86/kernel/fpu/signal.c | 206 ++++++++++++--------------- arch/x86/kernel/fpu/xstate.c | 47 +++--- arch/x86/kernel/process.c | 2 +- arch/x86/kernel/process_32.c | 14 +- arch/x86/kernel/process_64.c | 11 +- arch/x86/kernel/signal.c | 17 +-- arch/x86/kernel/traps.c | 2 +- arch/x86/kvm/x86.c | 47 +++--- arch/x86/math-emu/fpu_entry.c | 3 - arch/x86/mm/mpx.c | 6 +- arch/x86/mm/pkeys.c | 15 +- 27 files changed, 424 insertions(+), 469 deletions(-)
git://git.kernel.org/pub/scm/linux/kernel/git/bigeasy/staging.git x86_fpu_rtu_v5
Sebastian
PATCH 1/29 x86/fpu: Use ULL for shift in xfeature_uncompacted_offset()
The xfeature mask is 64bit so a shift from a number to its mask should have LL prefix or else nr > 31 will be lost. This is not a problem now but should XFEATURE_MASK_SUPERVISOR gain a bit >31 then this check won't catch it.
Use BIT_ULL() to compute a mask from a number.
--- arch/x86/kernel/fpu/xstate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/fpu/xstate.c +1 -1
@@ -444,7 +444,7 @@ static int xfeature_uncompacted_offset(int xfeature_nr)* format. Checking a supervisor state's uncompacted offset is * an error. */ -if (XFEATURE_MASK_SUPERVISOR & (1 << xfeature_nr)) { +if (XFEATURE_MASK_SUPERVISOR &BIT_ULL (xfeature_nr)) {WARN_ONCE (1 ,"No fixed offset for xstate %d\n" , xfeature_nr);return -1 ; }
Signature · 1 line
2.20.0.rc1
Re: [PATCH 01/29] x86/fpu: Use ULL for shift in xfeature_uncompacted_offset()
Quoted 11 lines · Sebastian Andrzej Siewior
The xfeature mask is 64bit so a shift from a number to its mask should
have LL prefix or else nr > 31 will be lost. This is not a problem now
but shouldXFEATURE_MASK_SUPERVISORgain a bit >31 then this check won't
catch it.Use
BIT_ULL()to compute a mask from a number.Signed-off-by Sebastian Andrzej Siewior <[email protected]>
Signature · 1 line
All Rights Reversed.
tip-bot for Sebastian Andrzej Siewior
merge bot[tip:x86/fpu] x86/fpu: Use unsigned long long shift in xfeature_uncompacted_offset()
In reply to Sebastian Andrzej Siewiorx86/fpu: Use unsigned long long shift in xfeature_uncompacted_offset()
Commit-ID: d08452390179710dc7989242605e3c1faa62b64f Gitweb: https://git.kernel.org/tip/d08452390179710dc7989242605e3c1faa62b64f Author: Sebastian Andrzej Siewior <[email protected]> AuthorDate: Wed, 28 Nov 2018 23:20:07 +0100 Committer: Borislav Petkov <[email protected]> CommitDate: Mon, 3 Dec 2018 18:40:28 +0100
x86/fpu: Use unsigned long long shift in xfeature_uncompacted_offset()
The xfeature mask is 64-bit so a shift from a number to its mask should have ULL suffix or else bits above position 31 will be lost. This is not a problem now but should XFEATURE_MASK_SUPERVISOR gain a bit >31 then this check won't catch it.
Use BIT_ULL() to compute a mask from a number.
--- arch/x86/kernel/fpu/xstate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/fpu/xstate.c +1 -1
@@ -444,7 +444,7 @@ static int xfeature_uncompacted_offset(int xfeature_nr)* format. Checking a supervisor state's uncompacted offset is * an error. */ -if (XFEATURE_MASK_SUPERVISOR & (1 << xfeature_nr)) { +if (XFEATURE_MASK_SUPERVISOR &BIT_ULL (xfeature_nr)) {WARN_ONCE (1 ,"No fixed offset for xstate %d\n" , xfeature_nr);return -1 ; }
PATCH 2/29 x86/entry/32: Remove asm/math_emu.h include
In reply to Sebastian Andrzej SiewiorThe math_emu.h header files contains the definition of struct math_emu_info. It is not used in this file.
Remove asm/math_emu.h include.
--- arch/x86/kernel/process_32.c | 3 --- 1 file changed, 3 deletions(-)
arch/x86/kernel/process_32.c +0 -3
@@ -44,9 +44,6 @@#include <asm/processor.h> #include <asm/fpu/internal.h> #include <asm/desc.h> -#ifdef CONFIG_MATH_EMULATION -#include <asm/math_emu.h> -#endif #include <linux/err.h>
Signature · 1 line
2.20.0.rc1
Re: [PATCH 02/29] x86/entry/32: Remove asm/math_emu.h include
Quoted 7 lines · Sebastian Andrzej Siewior
The
math_emu.hheader files contains the definition of structmath_emu_info. It is not used in this file.Remove
asm/math_emu.hinclude.Reviewed-by Andy Lutomirski <[email protected]>Signed-off-by Sebastian Andrzej Siewior <[email protected]>
Signature · 1 line
All Rights Reversed.
tip-bot for Sebastian Andrzej Siewior
merge bot[tip:x86/fpu] x86/process/32: Remove asm/math_emu.h include
In reply to Sebastian Andrzej Siewiorx86/process/32: Remove asm/math_emu.h include
Commit-ID: e08e32119b944f519664451bfb342a25b4045ead Gitweb: https://git.kernel.org/tip/e08e32119b944f519664451bfb342a25b4045ead Author: Sebastian Andrzej Siewior <[email protected]> AuthorDate: Wed, 28 Nov 2018 23:20:08 +0100 Committer: Borislav Petkov <[email protected]> CommitDate: Mon, 3 Dec 2018 18:51:16 +0100
x86/process/32: Remove asm/math_emu.h include
The math_emu.h header files contains the definition of struct math_emu_info which is not used in this file.
Remove the asm/math_emu.h include.
--- arch/x86/kernel/process_32.c | 3 --- 1 file changed, 3 deletions(-)
arch/x86/kernel/process_32.c +0 -3
@@ -44,9 +44,6 @@#include <asm/processor.h> #include <asm/fpu/internal.h> #include <asm/desc.h> -#ifdef CONFIG_MATH_EMULATION -#include <asm/math_emu.h> -#endif #include <linux/err.h>
PATCH 3/29 x86/entry: Remove _TIF_ALLWORK_MASK
In reply to Sebastian Andrzej SiewiorThere is no user of _TIF_ALLWORK_MASK since commit 21d375b6b34ff ("x86/entry/64: Remove the SYSCALL64 fast path"). Remove unused define _TIF_ALLWORK_MASK.
--- arch/x86/include/asm/thread_info.h | 8 -------- 1 file changed, 8 deletions(-)
arch/x86/include/asm/thread_info.h +0 -8
@@ -136,14 +136,6 @@ struct thread_info { _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT | \ _TIF_NOHZ) -/* work to do on any return to user space */ -#define _TIF_ALLWORK_MASK \ - (_TIF_SYSCALL_TRACE | _TIF_NOTIFY_RESUME | _TIF_SIGPENDING | \ - _TIF_NEED_RESCHED | _TIF_SINGLESTEP | _TIF_SYSCALL_EMU | \ - _TIF_SYSCALL_AUDIT | _TIF_USER_RETURN_NOTIFY | _TIF_UPROBE | \ - _TIF_PATCH_PENDING | _TIF_NOHZ | _TIF_SYSCALL_TRACEPOINT | \ - _TIF_FSCHECK) -/* flags to check in __switch_to() */ #define _TIF_WORK_CTXSW \ (_TIF_IO_BITMAP|_TIF_NOCPUID|_TIF_NOTSC|_TIF_BLOCKSTEP|_TIF_SSBD)
Signature · 1 line
2.20.0.rc1
Re: [PATCH 03/29] x86/entry: Remove _TIF_ALLWORK_MASK
Quoted 6 lines · Sebastian Andrzej Siewior
There is no user of
_TIF_ALLWORK_MASKsince commit21d375b6b34ff("x86/entry/64: Remove the SYSCALL64 fast path"). Remove unused define_TIF_ALLWORK_MASK.Reviewed-by Borislav Petkov <[email protected]>Signed-off-by Sebastian Andrzej Siewior <[email protected]>
Signature · 1 line
All Rights Reversed.
tip-bot for Sebastian Andrzej Siewior
merge bot[tip:x86/fpu] x86/thread_info: Remove _TIF_ALLWORK_MASK
In reply to Sebastian Andrzej Siewiorx86/thread_info: Remove _TIF_ALLWORK_MASK
Commit-ID: d23650e06299e9b2d04e090ab8c625cdf24465bf Gitweb: https://git.kernel.org/tip/d23650e06299e9b2d04e090ab8c625cdf24465bf Author: Sebastian Andrzej Siewior <[email protected]> AuthorDate: Wed, 28 Nov 2018 23:20:09 +0100 Committer: Borislav Petkov <[email protected]> CommitDate: Mon, 3 Dec 2018 19:00:28 +0100
x86/thread_info: Remove _TIF_ALLWORK_MASK
There is no user of _TIF_ALLWORK_MASK since commit
21d375b6b34ff ("x86/entry/64: Remove the SYSCALL64 fast path").
Remove the unused define _TIF_ALLWORK_MASK.
--- arch/x86/include/asm/thread_info.h | 8 -------- 1 file changed, 8 deletions(-)
arch/x86/include/asm/thread_info.h +0 -8
@@ -140,14 +140,6 @@ struct thread_info { _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT | \ _TIF_NOHZ) -/* work to do on any return to user space */ -#define _TIF_ALLWORK_MASK \ - (_TIF_SYSCALL_TRACE | _TIF_NOTIFY_RESUME | _TIF_SIGPENDING | \ - _TIF_NEED_RESCHED | _TIF_SINGLESTEP | _TIF_SYSCALL_EMU | \ - _TIF_SYSCALL_AUDIT | _TIF_USER_RETURN_NOTIFY | _TIF_UPROBE | \ - _TIF_PATCH_PENDING | _TIF_NOHZ | _TIF_SYSCALL_TRACEPOINT | \ - _TIF_FSCHECK) -/* flags to check in __switch_to() */ #define _TIF_WORK_CTXSW_BASE \ (_TIF_IO_BITMAP|_TIF_NOCPUID|_TIF_NOTSC|_TIF_BLOCKSTEP| \
PATCH 4/29 x86/pkeys: Make init_pkru_value static
In reply to Sebastian Andrzej SiewiorThe variable init_pkru_value isn't used outside of this file. Make init_pkru_value static.
--- arch/x86/mm/pkeys.c | 1 + 1 file changed, 1 insertion(+)
arch/x86/mm/pkeys.c +1 -0
@@ -131,6 +131,7 @@ int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot, int pkey* in the process's lifetime will not accidentally get access * to data which is pkey-protected later on. */ +static u32 init_pkru_value =PKRU_AD_KEY (1 ) |PKRU_AD_KEY (2 ) |PKRU_AD_KEY (3 ) |PKRU_AD_KEY (4 ) |PKRU_AD_KEY (5 ) |PKRU_AD_KEY (6 ) | PKRU_AD_KEY( 7) | PKRU_AD_KEY( 8) | PKRU_AD_KEY( 9) |
Signature · 1 line
2.20.0.rc1
Re: [PATCH 04/29] x86/pkeys: Make init_pkru_value static
Quoted 5 lines · Sebastian Andrzej Siewior
The variable
init_pkru_valueisn't used outside of this file. Makeinit_pkru_valuestatic.Acked-by Dave Hansen <[email protected]>Signed-off-by Sebastian Andrzej Siewior <[email protected]>
Signature · 1 line
All Rights Reversed.
tip-bot for Sebastian Andrzej Siewior
merge bot[tip:x86/fpu] x86/pkeys: Make init_pkru_value static
In reply to Sebastian Andrzej Siewiorx86/pkeys: Make init_pkru_value static
Commit-ID: 1a4226efaf808fe9079ea3b29919883008918297 Gitweb: https://git.kernel.org/tip/1a4226efaf808fe9079ea3b29919883008918297 Author: Sebastian Andrzej Siewior <[email protected]> AuthorDate: Wed, 28 Nov 2018 23:20:10 +0100 Committer: Borislav Petkov <[email protected]> CommitDate: Mon, 3 Dec 2018 19:10:18 +0100
x86/pkeys: Make init_pkru_value static
The variable init_pkru_value isn't used outside of this file. Make it static.
--- arch/x86/mm/pkeys.c | 1 + 1 file changed, 1 insertion(+)
arch/x86/mm/pkeys.c +1 -0
@@ -131,6 +131,7 @@ int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot, int pkey* in the process's lifetime will not accidentally get access * to data which is pkey-protected later on. */ +static u32 init_pkru_value =PKRU_AD_KEY (1 ) |PKRU_AD_KEY (2 ) |PKRU_AD_KEY (3 ) |PKRU_AD_KEY (4 ) |PKRU_AD_KEY (5 ) |PKRU_AD_KEY (6 ) | PKRU_AD_KEY( 7) | PKRU_AD_KEY( 8) | PKRU_AD_KEY( 9) |
PATCH 5/29 x86/fpu: add might_fault() to user_insn()
In reply to Sebastian Andrzej SiewiorEvery user of user_insn() passes an user memory pointer to this macro.
Add might_fault() to user_insn() so we can spot users which are using this macro in sections where page faulting is not allowed.
--- arch/x86/include/asm/fpu/internal.h | 1 + 1 file changed, 1 insertion(+)
arch/x86/include/asm/fpu/internal.h +1 -0
@@ -106,6 +106,7 @@ extern void fpstate_sanitize_xstate(struct fpu *fpu); #define user_insn(insn, output, input...) \ ({ \ int err; \ + might_fault(); \ asm volatile(ASM_STAC "\n" \ "1:" #insn "\n\t" \ "2: " ASM_CLAC "\n" \
Signature · 1 line
2.20.0.rc1
Re: [PATCH 05/29] x86/fpu: add might_fault() to user_insn()
Quoted 5 lines · Sebastian Andrzej Siewior
Every user of
user_insn()passes an user memory pointer to this macro.Add
might_fault()touser_insn()so we can spot users which are using this macro in sections where page faulting is not allowed.
Nice catch.
Quoted 1 line
Signed-off-by Sebastian Andrzej Siewior <[email protected]>
Signature · 1 line
All Rights Reversed.
tip-bot for Sebastian Andrzej Siewior
merge bot[tip:x86/fpu] x86/fpu: Add might_fault() to user_insn()
In reply to Sebastian Andrzej Siewiorx86/fpu: Add might_fault() to user_insn()
Commit-ID: 6637401c35b2f327a35d27f44bda05e327f2f017 Gitweb: https://git.kernel.org/tip/6637401c35b2f327a35d27f44bda05e327f2f017 Author: Sebastian Andrzej Siewior <[email protected]> AuthorDate: Wed, 28 Nov 2018 23:20:11 +0100 Committer: Borislav Petkov <[email protected]> CommitDate: Mon, 3 Dec 2018 19:15:32 +0100
x86/fpu: Add might_fault() to user_insn()
Every user of user_insn() passes an user memory pointer to this macro.
Add might_fault() to user_insn() so we can spot users which are using this macro in sections where page faulting is not allowed.
[ bp: Space it out to make it more visible. ]
--- arch/x86/include/asm/fpu/internal.h | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/include/asm/fpu/internal.h +3 -0
@@ -106,6 +106,9 @@ extern void fpstate_sanitize_xstate(struct fpu *fpu); #define user_insn(insn, output, input...) \ ({ \ int err; \ + \ + might_fault(); \ + \ asm volatile(ASM_STAC "\n" \ "1:" #insn "\n\t" \ "2: " ASM_CLAC "\n" \
PATCH 6/29 x86/fpu: Update comment for __raw_xsave_addr()
In reply to Sebastian Andrzej SiewiorThe comment above __raw_xsave_addr() claims that the function does not work for compacted buffers and was introduced in commit
b8b9b6ba9dec3("x86/fpu: Allow setting of XSAVE state")
In this commit, the function was factored out of get_xsave_addr() and this function claims that it works with "standard format or compacted format of xsave area". It accesses the "xstate_comp_offsets" variable for the actual offset and it was introduced in commit
7496d6458fe32 ("Define kernel API to get address of each state in xsave area")
Based on the code (back then and now):
- xstate_offsets holds the standard offset.
- if compacted mode is not supported then xstate_comp_offsets gets the xstate_offsets copied.
- if compacted mode is supported then xstate_comp_offsets will hold the offset for the compacted buffer.
Based on that the function works for compacted buffers as long as the CPU supports it and this what we care about.
Remove the "Note:" which is not accurate.
--- arch/x86/kernel/fpu/xstate.c | 2 -- 1 file changed, 2 deletions(-)
arch/x86/kernel/fpu/xstate.c +0 -2
@@ -808,8 +808,6 @@ void fpu__resume_cpu(void)* Given an xstate feature mask, calculate where in the xsave * buffer the state is. Callers should ensure that the buffer * is valid. -* -* Note: does not work for compacted buffers. */ void *__raw_xsave_addr (struct xregs_state *xsave,int xstate_feature_mask) {
Signature · 1 line
2.20.0.rc1
Re: [PATCH 06/29] x86/fpu: Update comment for __raw_xsave_addr()
Quoted 4 lines · Sebastian Andrzej Siewior
The comment above
__raw_xsave_addr()claims that the function does not
work for compacted buffers and was introduced in commit
b8b9b6ba9dec3("x86/fpu: Allow setting of XSAVE state")
...
Quoted 7 lines
Based on that the function works for compacted buffers as long as the CPU supports it and this what we care about.
Remove the "Note:" which is not accurate.
Suggested-by Paolo Bonzini <[email protected]>Signed-off-by Sebastian Andrzej Siewior <[email protected]>
Signature · 1 line
All Rights Reversed.
tip-bot for Sebastian Andrzej Siewior
merge bot[tip:x86/fpu] x86/fpu: Update comment for __raw_xsave_addr()
In reply to Sebastian Andrzej Siewiorx86/fpu: Update comment for __raw_xsave_addr()
Commit-ID: 2f2fcc40a961ed04f0e130803fbaa868c2899310 Gitweb: https://git.kernel.org/tip/2f2fcc40a961ed04f0e130803fbaa868c2899310 Author: Sebastian Andrzej Siewior <[email protected]> AuthorDate: Wed, 28 Nov 2018 23:20:12 +0100 Committer: Borislav Petkov <[email protected]> CommitDate: Mon, 3 Dec 2018 19:27:14 +0100
x86/fpu: Update comment for __raw_xsave_addr()
The comment above __raw_xsave_addr() claims that the function does not work for compacted buffers and was introduced in:
b8b9b6ba9dec3 ("x86/fpu: Allow setting of XSAVE state")
In this commit, the function was factored out of get_xsave_addr() and this function claims that it works with "standard format or compacted format of xsave area". It accesses the "xstate_comp_offsets" variable for the actual offset and it was introduced in commit
7496d6458fe32 ("Define kernel API to get address of each state in xsave area")
Based on the code (back then and now):
- xstate_offsets holds the standard offset.
- if compacted mode is not supported then xstate_comp_offsets gets the xstate_offsets copied.
- if compacted mode is supported then xstate_comp_offsets will hold the offset for the compacted buffer.
Based on that the function works for compacted buffers as long as the CPU supports it and this what we care about.
Remove the "Note:" which is not accurate.
--- arch/x86/kernel/fpu/xstate.c | 2 -- 1 file changed, 2 deletions(-)
arch/x86/kernel/fpu/xstate.c +0 -2
@@ -808,8 +808,6 @@ void fpu__resume_cpu(void)* Given an xstate feature mask, calculate where in the xsave * buffer the state is. Callers should ensure that the buffer * is valid. -* -* Note: does not work for compacted buffers. */ void *__raw_xsave_addr (struct xregs_state *xsave,int xstate_feature_mask) {
PATCH 7/29 x86/fpu: don't export __kernel_fpu_{begin|end}()
In reply to Sebastian Andrzej SiewiorThere is one user using __kernel_fpu_begin() and before invoking it, it invokes preempt_disable(). So it could invoke kernel_fpu_begin() right away. The 32bit version of arch_efi_call_virt_setup() and arch_efi_call_virt_teardown() does this already.
The comment above *kernel_fpu*() claims that before invoking __kernel_fpu_begin() preemption should be disabled and that KVM is a good example of doing it. Well, KVM doesn't do that since commit
f775b13eedee2 ("x86,kvm: move qemu/guest FPU switching out to vcpu_run")
so it is not an example anymore.
With EFI gone as the last user of __kernel_fpu_{begin|end}(), it can be made static and not exported anymore.
--- arch/x86/include/asm/efi.h | 6 ++---- arch/x86/include/asm/fpu/api.h | 16 ++++++---------- arch/x86/kernel/fpu/core.c | 6 ++---- 3 files changed, 10 insertions(+), 18 deletions(-)
arch/x86/include/asm/efi.h +2 -4
@@ -82,8 +82,7 @@ struct efi_scratch {#define arch_efi_call_virt_setup () \ ({ \ efi_sync_low_kernel_mappings(); \ - preempt_disable(); \ - __kernel_fpu_begin(); \ + kernel_fpu_begin(); \ firmware_restrict_branch_speculation_start(); \ \ if (!efi_enabled(EFI_OLD_MEMMAP)) \
@@ -99,8 +98,7 @@ struct efi_scratch {efi_switch_mm (efi_scratch.prev_mm); \ \firmware_restrict_branch_speculation_end (); \ -__kernel_fpu_end (); \ -preempt_enable (); \ +kernel_fpu_end (); \ }) extern void __iomem *__init efi_ioremap(unsigned long addr, unsigned long size,
arch/x86/include/asm/fpu/api.h +6 -10
@@ -12,17 +12,13 @@#define _ASM_X86_FPU_API_H/* -* Careful: __kernel_fpu_begin/end() must be called with preempt disabled -* and they don't touch the preempt state on their own. -* If you enable preemption after __kernel_fpu_begin(), preempt notifier -* should call the __kernel_fpu_end() to prevent the kernel/user FPU -* state from getting corrupted. KVM for example uses this model. -* -* All other cases use kernel_fpu_begin/end() which disable preemption -* during kernel FPU usage. +* Use kernel_fpu_begin/end() if you intend to use FPU in kernel context. It +* disables preemption so be carefull if you intend to use it for long periods +* of time. +* If you intend to use the FPU in softirq you need to check first with +* irq_fpu_usable() if it is possible. +* Using the FPU in hardirq is not allowed. */ -extern void __kernel_fpu_begin (void ); -extern void __kernel_fpu_end (void );extern void kernel_fpu_begin (void );extern void kernel_fpu_end (void );extern bool irq_fpu_usable (void );
arch/x86/kernel/fpu/core.c +2 -4
@@ -93,7 +93,7 @@ bool irq_fpu_usable(void) }EXPORT_SYMBOL (irq_fpu_usable); -void __kernel_fpu_begin (void ) +static void __kernel_fpu_begin (void ) {struct fpu *fpu = ¤t->thread.fpu;
@@ -111,9 +111,8 @@ void __kernel_fpu_begin(void)__cpu_invalidate_fpregs_state (); } } -EXPORT_SYMBOL (__kernel_fpu_begin); -void __kernel_fpu_end (void ) +static void __kernel_fpu_end (void ) {struct fpu *fpu = ¤t->thread.fpu;
@@ -122,7 +121,6 @@ void __kernel_fpu_end(void)kernel_fpu_enable (); } -EXPORT_SYMBOL (__kernel_fpu_end);void kernel_fpu_begin (void ) {
Signature · 1 line
2.20.0.rc1
Re: [PATCH 07/29] x86/fpu: don't export __kernel_fpu_{begin|end}()
Quoted 5 lines · Sebastian Andrzej Siewior
+ * Use kernel_fpu_begin/end() if you intend to use FPU in kernel context. It
+ * disables preemption so be carefull if you intend to use it for long periods
Just how careful do you want to be?
One l seems sufficient :)
Feel free to add my Reviewed-by: once that spelling error is fixed :)
Signature · 1 line
All Rights Reversed.
PATCH v2 7/29 x86/fpu: don't export __kernel_fpu_{begin|end}()
There is one user using __kernel_fpu_begin() and before invoking it, it invokes preempt_disable(). So it could invoke kernel_fpu_begin() right away. The 32bit version of arch_efi_call_virt_setup() and arch_efi_call_virt_teardown() does this already.
The comment above *kernel_fpu*() claims that before invoking __kernel_fpu_begin() preemption should be disabled and that KVM is a good example of doing it. Well, KVM doesn't do that since commit
f775b13eedee2 ("x86,kvm: move qemu/guest FPU switching out to vcpu_run")
so it is not an example anymore.
With EFI gone as the last user of __kernel_fpu_{begin|end}(), it can be made static and not exported anymore.
---
v1…v2: carefull' -> careful'
arch/x86/include/asm/efi.h | 6 ++---- arch/x86/include/asm/fpu/api.h | 16 ++++++---------- arch/x86/kernel/fpu/core.c | 6 ++---- 3 files changed, 10 insertions(+), 18 deletions(-)
arch/x86/include/asm/efi.h +2 -4
@@ -82,8 +82,7 @@ struct efi_scratch {#define arch_efi_call_virt_setup () \ ({ \ efi_sync_low_kernel_mappings(); \ - preempt_disable(); \ - __kernel_fpu_begin(); \ + kernel_fpu_begin(); \ firmware_restrict_branch_speculation_start(); \ \ if (!efi_enabled(EFI_OLD_MEMMAP)) \
@@ -99,8 +98,7 @@ struct efi_scratch {efi_switch_mm (efi_scratch.prev_mm); \ \firmware_restrict_branch_speculation_end (); \ -__kernel_fpu_end (); \ -preempt_enable (); \ +kernel_fpu_end (); \ }) extern void __iomem *__init efi_ioremap(unsigned long addr, unsigned long size,
arch/x86/include/asm/fpu/api.h +6 -10
@@ -12,17 +12,13 @@#define _ASM_X86_FPU_API_H/* -* Careful: __kernel_fpu_begin/end() must be called with preempt disabled -* and they don't touch the preempt state on their own. -* If you enable preemption after __kernel_fpu_begin(), preempt notifier -* should call the __kernel_fpu_end() to prevent the kernel/user FPU -* state from getting corrupted. KVM for example uses this model. -* -* All other cases use kernel_fpu_begin/end() which disable preemption -* during kernel FPU usage. +* Use kernel_fpu_begin/end() if you intend to use FPU in kernel context. It +* disables preemption so be careful if you intend to use it for long periods +* of time. +* If you intend to use the FPU in softirq you need to check first with +* irq_fpu_usable() if it is possible. +* Using the FPU in hardirq is not allowed. */ -extern void __kernel_fpu_begin (void ); -extern void __kernel_fpu_end (void );extern void kernel_fpu_begin (void );extern void kernel_fpu_end (void );extern bool irq_fpu_usable (void );
arch/x86/kernel/fpu/core.c +2 -4
@@ -93,7 +93,7 @@ bool irq_fpu_usable(void) }EXPORT_SYMBOL (irq_fpu_usable); -void __kernel_fpu_begin (void ) +static void __kernel_fpu_begin (void ) {struct fpu *fpu = ¤t->thread.fpu;
@@ -111,9 +111,8 @@ void __kernel_fpu_begin(void)__cpu_invalidate_fpregs_state (); } } -EXPORT_SYMBOL (__kernel_fpu_begin); -void __kernel_fpu_end (void ) +static void __kernel_fpu_end (void ) {struct fpu *fpu = ¤t->thread.fpu;
@@ -122,7 +121,6 @@ void __kernel_fpu_end(void)kernel_fpu_enable (); } -EXPORT_SYMBOL (__kernel_fpu_end);void kernel_fpu_begin (void ) {
Signature · 1 line
2.20.0.rc1
tip-bot for Sebastian Andrzej Siewior
merge bot[tip:x86/fpu] x86/fpu: Don't export __kernel_fpu_{begin,end}()
x86/fpu: Don't export __kernel_fpu_{begin,end}()
Commit-ID: 7d79adb87fa79e4a4c59424fd5b5a922861fc5f6 Gitweb: https://git.kernel.org/tip/7d79adb87fa79e4a4c59424fd5b5a922861fc5f6 Author: Sebastian Andrzej Siewior <[email protected]> AuthorDate: Thu, 29 Nov 2018 16:02:10 +0100 Committer: Borislav Petkov <[email protected]> CommitDate: Mon, 3 Dec 2018 19:37:27 +0100
x86/fpu: Don't export __kernel_fpu_{begin,end}()
There is one user of __kernel_fpu_begin() and before invoking it, it invokes preempt_disable(). So it could invoke kernel_fpu_begin() right away. The 32bit version of arch_efi_call_virt_setup() and arch_efi_call_virt_teardown() does this already.
The comment above *kernel_fpu*() claims that before invoking __kernel_fpu_begin() preemption should be disabled and that KVM is a good example of doing it. Well, KVM doesn't do that since commit
f775b13eedee2 ("x86,kvm: move qemu/guest FPU switching out to vcpu_run")
so it is not an example anymore.
With EFI gone as the last user of __kernel_fpu_{begin|end}(), both can be made static and not exported anymore.
--- arch/x86/include/asm/efi.h | 6 ++---- arch/x86/include/asm/fpu/api.h | 16 ++++++---------- arch/x86/kernel/fpu/core.c | 6 ++---- 3 files changed, 10 insertions(+), 18 deletions(-)
arch/x86/include/asm/efi.h +2 -4
@@ -82,8 +82,7 @@ struct efi_scratch {#define arch_efi_call_virt_setup () \ ({ \ efi_sync_low_kernel_mappings(); \ - preempt_disable(); \ - __kernel_fpu_begin(); \ + kernel_fpu_begin(); \ firmware_restrict_branch_speculation_start(); \ \ if (!efi_enabled(EFI_OLD_MEMMAP)) \
@@ -99,8 +98,7 @@ struct efi_scratch {efi_switch_mm (efi_scratch.prev_mm); \ \firmware_restrict_branch_speculation_end (); \ -__kernel_fpu_end (); \ -preempt_enable (); \ +kernel_fpu_end (); \ }) extern void __iomem *__init efi_ioremap(unsigned long addr, unsigned long size,
arch/x86/include/asm/fpu/api.h +6 -10
@@ -12,17 +12,13 @@#define _ASM_X86_FPU_API_H/* -* Careful: __kernel_fpu_begin/end() must be called with preempt disabled -* and they don't touch the preempt state on their own. -* If you enable preemption after __kernel_fpu_begin(), preempt notifier -* should call the __kernel_fpu_end() to prevent the kernel/user FPU -* state from getting corrupted. KVM for example uses this model. -* -* All other cases use kernel_fpu_begin/end() which disable preemption -* during kernel FPU usage. +* Use kernel_fpu_begin/end() if you intend to use FPU in kernel context. It +* disables preemption so be careful if you intend to use it for long periods +* of time. +* If you intend to use the FPU in softirq you need to check first with +* irq_fpu_usable() if it is possible. +* Using the FPU in hardirq is not allowed. */ -extern void __kernel_fpu_begin (void ); -extern void __kernel_fpu_end (void );extern void kernel_fpu_begin (void );extern void kernel_fpu_end (void );extern bool irq_fpu_usable (void );
arch/x86/kernel/fpu/core.c +2 -4
@@ -93,7 +93,7 @@ bool irq_fpu_usable(void) }EXPORT_SYMBOL (irq_fpu_usable); -void __kernel_fpu_begin (void ) +static void __kernel_fpu_begin (void ) {struct fpu *fpu = ¤t->thread.fpu;
@@ -111,9 +111,8 @@ void __kernel_fpu_begin(void)__cpu_invalidate_fpregs_state (); } } -EXPORT_SYMBOL (__kernel_fpu_begin); -void __kernel_fpu_end (void ) +static void __kernel_fpu_end (void ) {struct fpu *fpu = ¤t->thread.fpu;
@@ -122,7 +121,6 @@ void __kernel_fpu_end(void)kernel_fpu_enable (); } -EXPORT_SYMBOL (__kernel_fpu_end);void kernel_fpu_begin (void ) {
Re: [tip:x86/fpu] x86/fpu: Don't export __kernel_fpu_{begin,end}()
Quoted 85 lines · tip-bot for Sebastian Andrzej Siewior
Commit-ID: 7d79adb87fa79e4a4c59424fd5b5a922861fc5f6 Gitweb: https://git.kernel.org/tip/7d79adb87fa79e4a4c59424fd5b5a922861fc5f6 Author: Sebastian Andrzej Siewior <[email protected]> AuthorDate: Thu, 29 Nov 2018 16:02:10 +0100 Committer: Borislav Petkov <[email protected]> CommitDate: Mon, 3 Dec 2018 19:37:27 +0100x86/fpu: Don't export __kernel_fpu_{begin,end}()
There is one user of
__kernel_fpu_begin()and before invoking it, it invokespreempt_disable(). So it could invokekernel_fpu_begin()right away. The 32bit version ofarch_efi_call_virt_setup()andarch_efi_call_virt_teardown()does this already.The comment above *kernel_fpu*() claims that before invoking
__kernel_fpu_begin()preemption should be disabled and that KVM is a good example of doing it. Well, KVM doesn't do that since commit
f775b13eedee2("x86,kvm: move qemu/guest FPU switching out tovcpu_run")so it is not an example anymore.
With EFI gone as the last user of __kernel_fpu_{begin|end}(), both can be made static and not exported anymore.
Signed-off-by Sebastian Andrzej Siewior <[email protected]>Signed-off-by Borislav Petkov <[email protected]>Reviewed-by Rik van Riel <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc "Jason A. Donenfeld" <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Ard Biesheuvel <[email protected]>Cc Dave Hansen <[email protected]>Cc Ingo Molnar <[email protected]>Cc Nicolai Stange <[email protected]>Cc Paolo Bonzini <[email protected]>Cc Radim Krčmář <[email protected]>Cc Thomas Gleixner <[email protected]>Cc kvm ML <[email protected]>Cc linux-efi <[email protected]>Cc x86-ml <[email protected]>--- arch/x86/include/asm/efi.h | 6 ++---- arch/x86/include/asm/fpu/api.h | 16 ++++++---------- arch/x86/kernel/fpu/core.c | 6 ++---- 3 files changed, 10 insertions(+), 18 deletions(-)arch/x86/include/asm/efi.h +2
-4@@ -82,8 +82,7 @@ struct efi_scratch {#define arch_efi_call_virt_setup () \ ({ \ efi_sync_low_kernel_mappings(); \ - preempt_disable(); \ - __kernel_fpu_begin(); \ + kernel_fpu_begin(); \ firmware_restrict_branch_speculation_start(); \ \ if (!efi_enabled(EFI_OLD_MEMMAP)) \@@ -99,8 +98,7 @@ struct efi_scratch {efi_switch_mm (efi_scratch.prev_mm); \ \firmware_restrict_branch_speculation_end (); \ -__kernel_fpu_end (); \ -preempt_enable (); \ +kernel_fpu_end (); \ }) extern void __iomem *__init efi_ioremap(unsigned long addr, unsigned long size,arch/x86/include/asm/fpu/api.h +0
-4@@ -12,17 +12,13 @@#define _ASM_X86_FPU_API_H/* -* Careful: __kernel_fpu_begin/end() must be called with preempt disabled -* and they don't touch the preempt state on their own. -* If you enable preemption after __kernel_fpu_begin(), preempt notifier -* should call the __kernel_fpu_end() to prevent the kernel/user FPU
arch/x86/include/asm/fpu/api.h line 20
Quoted 10 lines
arch/x86/include/asm/fpu/api.h +6
-4-* state from getting corrupted. KVM for example uses this model. -* -* All other cases use kernel_fpu_begin/end() which disable preemption -* during kernel FPU usage. +* Use kernel_fpu_begin/end() if you intend to use FPU in kernel context. It +* disables preemption so be careful if you intend to use it for long periods +* of time. +* If you intend to use the FPU in softirq you need to check first with +* irq_fpu_usable() if it is possible. +* Using the FPU in hardirq is not allowed.
According to the documentation in x86/kernel/fpu/core.c, this is not true. So which one is accurate?
Quoted 38 lines
arch/x86/include/asm/fpu/api.h +0
-2*/ -extern void __kernel_fpu_begin (void ); -extern void __kernel_fpu_end (void );extern void kernel_fpu_begin (void );extern void kernel_fpu_end (void );extern bool irq_fpu_usable (void );arch/x86/kernel/fpu/core.c +2
-4@@ -93,7 +93,7 @@ bool irq_fpu_usable(void) }EXPORT_SYMBOL (irq_fpu_usable); -void __kernel_fpu_begin (void ) +static void __kernel_fpu_begin (void ) {struct fpu *fpu = ¤t->thread.fpu;@@ -111,9 +111,8 @@ void __kernel_fpu_begin(void)__cpu_invalidate_fpregs_state (); } } -EXPORT_SYMBOL (__kernel_fpu_begin); -void __kernel_fpu_end (void ) +static void __kernel_fpu_end (void ) {struct fpu *fpu = ¤t->thread.fpu;@@ -122,7 +121,6 @@ void __kernel_fpu_end(void)kernel_fpu_enable (); } -EXPORT_SYMBOL (__kernel_fpu_end);void kernel_fpu_begin (void ) {
Borislav Petkov
maintainerRe: [tip:x86/fpu] x86/fpu: Don't export __kernel_fpu_{begin,end}()
Quoted 4 lines · Ard Biesheuvel
Quoted 1 line
+ * Using the FPU in hardirq is not allowed.
According to the documentation in
x86/kernel/fpu/core.c, this is not true. So which one is accurate?
I think you mean the irq from user mode... Yap, we do allow that.
Sebastian?
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Borislav Petkov
maintainerRe: [tip:x86/fpu] x86/fpu: Don't export __kernel_fpu_{begin,end}()
Quoted 9 lines · Borislav Petkov
Quoted 4 lines · Ard Biesheuvel
Quoted 1 line
+ * Using the FPU in hardirq is not allowed.
According to the documentation in
x86/kernel/fpu/core.c, this is not true. So which one is accurate?I think you mean the irq from user mode... Yap, we do allow that.
Sebastian?
Ok, I've zapped that sentence from the comment until we clarify what Sebastian meant.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [tip:x86/fpu] x86/fpu: Don't export __kernel_fpu_{begin,end}()
In reply to Borislav PetkovQuoted 9 lines · Borislav Petkov
Quoted 4 lines · Ard Biesheuvel
Quoted 1 line
+ * Using the FPU in hardirq is not allowed.
According to the documentation in
x86/kernel/fpu/core.c, this is not true. So which one is accurate?I think you mean the irq from user mode... Yap, we do allow that.
Sebastian?
Do you refer to
| * - by IRQ context code to potentially use the FPU
| * if it's unused.
? It is possible to use the FPU in IRQ context.
The FPU could be used in user-context surrounded by kernel_fpu_begin(). This only disables preemption so an IRQ could interrupt it. This IRQ could then use the FPU or raise a SoftIRQ which would use it. Therefore on x86 it is required to check with irq_fpu_usable() if the FPU can be used. If the FPU can not be used, you have to implement fallback code.
With the "restore FPU on return to userland" series we need to modify the FPU in a few places. The softirq and preemption is disabled. I didn't find any in-IRQ users.
Going forward I would like to remove the in-IRQ part and irq_fpu_usable() and disable softirq as part of kernel_fpu_begin().
Quoted 1 line
Thx.
Sebastian
Borislav Petkov
maintainerRe: [tip:x86/fpu] x86/fpu: Don't export __kernel_fpu_{begin,end}()
Quoted 5 lines · Sebastian Andrzej Siewior
Do you refer to
| * - by IRQ context code to potentially use the FPU
| * if it's unused.? It is possible to use the FPU in IRQ context.
I mean interrupted_user_mode() where we apparently can use the FPU when handling an IRQ from user mode.
Quoted 1 line
The FPU could be used in user-context surrounded by
kernel_fpu_begin().
Right, that.
Quoted 3 lines
This only disables preemption so an IRQ could interrupt it. This IRQ could then use the FPU or raise a SoftIRQ which would use it. Therefore on x86 it is required to check with
irq_fpu_usable()if the
Yes, and the check that thing does is:
return !in_interrupt() ||
interrupted_user_mode() || ...so you're either *not* in interrupt, or you've gotten the IRQ while in user mode.
Quoted 8 lines
FPU can be used. If the FPU can not be used, you have to implement fallback code.
With the "restore FPU on return to userland" series we need to modify the FPU in a few places. The softirq and preemption is disabled. I didn't find any in-IRQ users.
Going forward I would like to remove the in-IRQ part andirq_fpu_usable()and disable softirq as part ofkernel_fpu_begin().
Right, and we should document all those new conditions prominently so that people are aware.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.tip-bot for Sebastian Andrzej Siewior
merge bot[tip:x86/fpu] x86/fpu: Don't export __kernel_fpu_{begin,end}()
In reply to Sebastian Andrzej Siewiorx86/fpu: Don't export __kernel_fpu_{begin,end}()
Commit-ID: 12209993e98c5fa1855c467f22a24e3d5b8be205 Gitweb: https://git.kernel.org/tip/12209993e98c5fa1855c467f22a24e3d5b8be205 Author: Sebastian Andrzej Siewior <[email protected]> AuthorDate: Thu, 29 Nov 2018 16:02:10 +0100 Committer: Borislav Petkov <[email protected]> CommitDate: Tue, 4 Dec 2018 12:37:28 +0100
x86/fpu: Don't export __kernel_fpu_{begin,end}()
There is one user of __kernel_fpu_begin() and before invoking it, it invokes preempt_disable(). So it could invoke kernel_fpu_begin() right away. The 32bit version of arch_efi_call_virt_setup() and arch_efi_call_virt_teardown() does this already.
The comment above *kernel_fpu*() claims that before invoking __kernel_fpu_begin() preemption should be disabled and that KVM is a good example of doing it. Well, KVM doesn't do that since commit
f775b13eedee2 ("x86,kvm: move qemu/guest FPU switching out to vcpu_run")
so it is not an example anymore.
With EFI gone as the last user of __kernel_fpu_{begin|end}(), both can be made static and not exported anymore.
--- arch/x86/include/asm/efi.h | 6 ++---- arch/x86/include/asm/fpu/api.h | 15 +++++---------- arch/x86/kernel/fpu/core.c | 6 ++---- 3 files changed, 9 insertions(+), 18 deletions(-)
arch/x86/include/asm/efi.h +2 -4
@@ -82,8 +82,7 @@ struct efi_scratch {#define arch_efi_call_virt_setup () \ ({ \ efi_sync_low_kernel_mappings(); \ - preempt_disable(); \ - __kernel_fpu_begin(); \ + kernel_fpu_begin(); \ firmware_restrict_branch_speculation_start(); \ \ if (!efi_enabled(EFI_OLD_MEMMAP)) \
@@ -99,8 +98,7 @@ struct efi_scratch {efi_switch_mm (efi_scratch.prev_mm); \ \firmware_restrict_branch_speculation_end (); \ -__kernel_fpu_end (); \ -preempt_enable (); \ +kernel_fpu_end (); \ }) extern void __iomem *__init efi_ioremap(unsigned long addr, unsigned long size,
arch/x86/include/asm/fpu/api.h +5 -10
@@ -12,17 +12,12 @@#define _ASM_X86_FPU_API_H/* -* Careful: __kernel_fpu_begin/end() must be called with preempt disabled -* and they don't touch the preempt state on their own. -* If you enable preemption after __kernel_fpu_begin(), preempt notifier -* should call the __kernel_fpu_end() to prevent the kernel/user FPU -* state from getting corrupted. KVM for example uses this model. -* -* All other cases use kernel_fpu_begin/end() which disable preemption -* during kernel FPU usage. +* Use kernel_fpu_begin/end() if you intend to use FPU in kernel context. It +* disables preemption so be careful if you intend to use it for long periods +* of time. +* If you intend to use the FPU in softirq you need to check first with +* irq_fpu_usable() if it is possible. */ -extern void __kernel_fpu_begin (void ); -extern void __kernel_fpu_end (void );extern void kernel_fpu_begin (void );extern void kernel_fpu_end (void );extern bool irq_fpu_usable (void );
arch/x86/kernel/fpu/core.c +2 -4
@@ -93,7 +93,7 @@ bool irq_fpu_usable(void) }EXPORT_SYMBOL (irq_fpu_usable); -void __kernel_fpu_begin (void ) +static void __kernel_fpu_begin (void ) {struct fpu *fpu = ¤t->thread.fpu;
@@ -111,9 +111,8 @@ void __kernel_fpu_begin(void)__cpu_invalidate_fpregs_state (); } } -EXPORT_SYMBOL (__kernel_fpu_begin); -void __kernel_fpu_end (void ) +static void __kernel_fpu_end (void ) {struct fpu *fpu = ¤t->thread.fpu;
@@ -122,7 +121,6 @@ void __kernel_fpu_end(void)kernel_fpu_enable (); } -EXPORT_SYMBOL (__kernel_fpu_end);void kernel_fpu_begin (void ) {
PATCH 8/29 x86/fpu: Remove fpu->initialized usage in __fpu__restore_sig()
In reply to Sebastian Andrzej SiewiorThis is a preparation for the removal of the ->initialized member in the fpu struct.__fpu__restore_sig() is deactivating the FPU via fpu__drop() and then setting manually ->initialized followed by fpu__restore(). The result is that it is possible to manipulate fpu->state and the state of registers won't be saved/restored on a context switch which would overwrite fpu->state.
Don't access the fpu->state while the content is read from user space and examined / sanitized. Use a temporary kmalloc() buffer for the preparation of the FPU registers and once the state is considered okay, load it. Should something go wrong, return with an error and without altering the original FPU registers.
The removal of "fpu__initialize()" is a nop because fpu->initialized is already set for the user task.
--- arch/x86/include/asm/fpu/signal.h | 2 +- arch/x86/kernel/fpu/regset.c | 5 ++-- arch/x86/kernel/fpu/signal.c | 41 ++++++++++++------------------- 3 files changed, 19 insertions(+), 29 deletions(-)
arch/x86/include/asm/fpu/signal.h +1 -1
@@ -22,7 +22,7 @@ int ia32_setup_frame(int sig, struct ksignal *ksig,extern void convert_from_fxsr (struct user_i387_ia32_struct *env,struct task_struct *tsk); -extern void convert_to_fxsr (struct task_struct *tsk, +extern void convert_to_fxsr (struct fxregs_state *fxsave,const struct user_i387_ia32_struct *env);unsigned long
arch/x86/kernel/fpu/regset.c +2 -3
@@ -269,11 +269,10 @@ convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)memcpy (&to[i], &from[i],sizeof (to[0 ])); } -void convert_to_fxsr (struct task_struct *tsk, +void convert_to_fxsr (struct fxregs_state *fxsave,const struct user_i387_ia32_struct *env) { -struct fxregs_state *fxsave = &tsk->thread.fpu.state.fxsave;struct _fpreg *from = (struct _fpreg *) &env->st_space[0 ];struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0 ];int i;
@@ -350,7 +349,7 @@ int fpregs_set(struct task_struct *target, const struct user_regset *regset, ret =user_regset_copyin (&pos, &count, &kbuf, &ubuf, &env,0 ,-1 );if (!ret) -convert_to_fxsr (target, &env); +convert_to_fxsr (&target->thread.fpu.state.fxsave, &env);/* * update the header bit in the xsave header, indicating the
arch/x86/kernel/fpu/signal.c +16 -25
83 lines not on this page · raw message
Signature · 1 line
2.20.0.rc1
Borislav Petkov
maintainerRe: [PATCH 08/29] x86/fpu: Remove fpu->initialized usage in __fpu__restore_sig()
Quoted 23 lines · Sebastian Andrzej Siewior
This is a preparation for the removal of the ->initialized member in the fpu struct.
__fpu__restore_sig()is deactivating the FPU viafpu__drop()and then setting manually ->initialized followed byfpu__restore(). The result is that it is possible to manipulatefpu->stateand the state of registers won't be saved/restored on a context switch which would overwritefpu->state.Don't access the
fpu->statewhile the content is read from user space and examined / sanitized. Use a temporarykmalloc()buffer for the preparation of the FPU registers and once the state is considered okay, load it. Should something go wrong, return with an error and without altering the original FPU registers.The removal of "
fpu__initialize()" is a nop becausefpu->initializedis already set for the user task.Signed-off-by Sebastian Andrzej Siewior <[email protected]>--- arch/x86/include/asm/fpu/signal.h | 2 +- arch/x86/kernel/fpu/regset.c | 5 ++-- arch/x86/kernel/fpu/signal.c | 41 ++++++++++++------------------- 3 files changed, 19 insertions(+), 29 deletions(-)
...
Quoted 37 lines
arch/x86/kernel/fpu/signal.c +3
-5@@ -207,11 +207,11 @@ int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size) }static inline void -sanitize_restored_xstate (struct task_struct *tsk, +sanitize_restored_xstate (union fpregs_state *state,struct user_i387_ia32_struct *ia32_env,u64 xfeatures,int fx_only) { -struct xregs_state *xsave = &tsk->thread.fpu.state.xsave; +struct xregs_state *xsave = &state->xsave;struct xstate_header *header = &xsave->header;if (use_xsave ()) {@@ -238,7 +238,7 @@ sanitize_restored_xstate(struct task_struct *tsk,*/ xsave->i387.mxcsr &= mxcsr_feature_mask; -convert_to_fxsr (tsk, ia32_env); +convert_to_fxsr (&state->fxsave, ia32_env); } }@@ -284,8 +284,6 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)if (!access_ok (VERIFY_READ , buf, size))return -EACCES ; -fpu__initialize (fpu); -if (!static_cpu_has (X86_FEATURE_FPU )) return fpregs_soft_set(current, NULL, 0, sizeof(struct user_i387_ia32_struct),
arch/x86/kernel/fpu/signal.c static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size) · line 318
Quoted 7 lines
arch/x86/kernel/fpu/signal.c +2
-0@@ -314,41 +312,34 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)* thread's fpu state, reconstruct fxstate from the fsave * header. Validate and sanitize the copied state. */ +union fpregs_state *state; +void *tmp;struct user_i387_ia32_struct env;int err =0 ;
Sort those in reverse xmas order pls.
Otherwise looks ok.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [PATCH 08/29] x86/fpu: Remove fpu->initialized usage in __fpu__restore_sig()
Quoted 11 lines · Borislav Petkov
arch/x86/kernel/fpu/signal.c static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size) · line 318
Quoted 8 lines
arch/x86/kernel/fpu/signal.c +2
-0@@ -314,41 +312,34 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)* thread's fpu state, reconstruct fxstate from the fsave * header. Validate and sanitize the copied state. */ +union fpregs_state *state; +void *tmp;struct user_i387_ia32_struct env;int err =0 ;Sort those in reverse xmas order pls.
Quoted 1 line
Otherwise looks ok.
You mean:
struct user_i387_ia32_struct env;
+ union fpregs_state *state;
+ void *tmp;
int err = 0;
?
Sebastian
Borislav Petkov
maintainerRe: [PATCH 08/29] x86/fpu: Remove fpu->initialized usage in __fpu__restore_sig()
Quoted 6 lines · Sebastian Andrzej Siewior
You mean:
structuser_i387_ia32_structenv;
+ unionfpregs_state*state;
+ void *tmp;
int err = 0;
?
Yap.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.PATCH 9/29 x86/fpu: Remove fpu__restore()
In reply to Sebastian Andrzej SiewiorThere are no users of fpu__restore() so it is time to remove it. The comment regarding fpu__restore() and TS bit is stale since commit
b3b0870ef3ffe ("i387: do not preload FPU state at task switch time")
and has no meaning since.
--- Documentation/preempt-locking.txt | 1 - arch/x86/include/asm/fpu/internal.h | 1 - arch/x86/kernel/fpu/core.c | 24 ------------------------ arch/x86/kernel/process_32.c | 4 +--- arch/x86/kernel/process_64.c | 4 +--- 5 files changed, 2 insertions(+), 32 deletions(-)
Documentation/preempt-locking.txt +0 -1
@@ -52,7 +52,6 @@ preemption must be disabled around such regions. Note, some FPU functions are already explicitly preempt safe. For example, kernel_fpu_begin and kernel_fpu_end will disable and enable preemption. -However, fpu__restore() must be called with preemption disabled. RULE #3: Lock acquire and release must be performed by same task
arch/x86/include/asm/fpu/internal.h +0 -1
@@ -28,7 +28,6 @@ extern void fpu__initialize(struct fpu *fpu);extern void fpu__prepare_read (struct fpu *fpu);extern void fpu__prepare_write (struct fpu *fpu);extern void fpu__save (struct fpu *fpu); -extern void fpu__restore (struct fpu *fpu);extern int fpu__restore_sig (void __user *buf,int ia32_frame);extern void fpu__drop (struct fpu *fpu);extern int fpu__copy (struct fpu *dst_fpu,struct fpu *src_fpu);
arch/x86/kernel/fpu/core.c +0 -24
@@ -303,30 +303,6 @@ void fpu__prepare_write(struct fpu *fpu) } } -/* -* 'fpu__restore()' is called to copy FPU registers from -* the FPU fpstate to the live hw registers and to activate -* access to the hardware registers, so that FPU instructions -* can be used afterwards. -* -* Must be called with kernel preemption disabled (for example -* with local interrupts disabled, as it is in the case of -* do_device_not_available()). -*/ -void fpu__restore (struct fpu *fpu) -{ -fpu__initialize (fpu); - -/* Avoid __kernel_fpu_begin() right after fpregs_activate() */ -kernel_fpu_disable (); -trace_x86_fpu_before_restore (fpu); -fpregs_activate (fpu); -copy_kernel_to_fpregs (&fpu->state); -trace_x86_fpu_after_restore (fpu); -kernel_fpu_enable (); -} -EXPORT_SYMBOL_GPL (fpu__restore); -/* * Drops current FPU state: deactivates the fpregs and * the fpstate. NOTE: it still leaves previous contents
arch/x86/kernel/process_32.c +1 -3
@@ -271,9 +271,7 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)/* * Leave lazy mode, flushing any hypercalls made here. * This must be done before restoring TLS segments so -* the GDT and LDT are properly updated, and must be -* done before fpu__restore(), so the TS bit is up -* to date. +* the GDT and LDT are properly updated. */ arch_end_context_switch (next_p);
arch/x86/kernel/process_64.c +1 -3
@@ -576,9 +576,7 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)/* * Leave lazy mode, flushing any hypercalls made here. This * must be done after loading TLS entries in the GDT but before -* loading segments that might reference them, and and it must -* be done before fpu__restore(), so the TS bit is up to -* date. +* loading segments that might reference them. */ arch_end_context_switch (next_p);
Signature · 1 line
2.20.0.rc1
PATCH 10/29 x86/fpu: Remove preempt_disable() in fpu__clear()
In reply to Sebastian Andrzej SiewiorThe preempt_disable() section was introduced in commit
a10b6a16cdad8 ("x86/fpu: Make the fpu state change in fpu__clear() scheduler-atomic")
and it was said to be temporary.
fpu__initialize() initializes the FPU struct to its "init" value and then sets ->initialized to 1. The last part is the important one. The content of the `state' does not matter because it gets set via copy_init_fpstate_to_fpregs().
A preemption here has little meaning because the register will always be set to the same content after copy_init_fpstate_to_fpregs(). A softirq with a kernel_fpu_begin() could also force to save FPU's register after fpu__initialize() without changing the outcome here.
Remove the preempt_disable() section in fpu__clear(), preemption here does not hurt.
--- arch/x86/kernel/fpu/core.c | 2 -- 1 file changed, 2 deletions(-)
arch/x86/kernel/fpu/core.c +0 -2
@@ -366,11 +366,9 @@ void fpu__clear(struct fpu *fpu)* Make sure fpstate is cleared and initialized. */ if (static_cpu_has (X86_FEATURE_FPU )) { -preempt_disable ();fpu__initialize (fpu);user_fpu_begin ();copy_init_fpstate_to_fpregs (); -preempt_enable (); } }
Signature · 1 line
2.20.0.rc1
PATCH 11/29 x86/fpu: Always init the `state' in fpu__clear()
In reply to Sebastian Andrzej Siewiorfpu__clear() only initializes the `state' if the FPU is present. This initialisation is also required for the FPU-less system and takes place math_emulate(). Since fpu__initialize() only performs the initialization if ->initialized is zero it does not matter that it is invoked each time an opcode is emulated. It makes the removal of ->initialized easier if the struct is also initialized in FPU-less case at the same time.
Move fpu__initialize() before the FPU check so it is also performed in FPU-less case.
--- arch/x86/include/asm/fpu/internal.h | 1 - arch/x86/kernel/fpu/core.c | 5 ++--- arch/x86/math-emu/fpu_entry.c | 3 --- 3 files changed, 2 insertions(+), 7 deletions(-)
arch/x86/include/asm/fpu/internal.h +0 -1
@@ -24,7 +24,6 @@/* * High level FPU state handling functions: */ -extern void fpu__initialize (struct fpu *fpu);extern void fpu__prepare_read (struct fpu *fpu);extern void fpu__prepare_write (struct fpu *fpu);extern void fpu__save (struct fpu *fpu);
arch/x86/kernel/fpu/core.c +2 -3
@@ -223,7 +223,7 @@ int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu)* Activate the current task's in-memory FPU context, * if it has not been used before: */ -void fpu__initialize (struct fpu *fpu) +static void fpu__initialize (struct fpu *fpu) {WARN_ON_FPU (fpu != ¤t->thread.fpu);
@@ -236,7 +236,6 @@ void fpu__initialize(struct fpu *fpu) fpu->initialized =1 ; } } -EXPORT_SYMBOL_GPL (fpu__initialize);/* * This function must be called before we read a task's fpstate.
@@ -365,8 +364,8 @@ void fpu__clear(struct fpu *fpu)/* * Make sure fpstate is cleared and initialized. */ +fpu__initialize (fpu);if (static_cpu_has (X86_FEATURE_FPU )) { -fpu__initialize (fpu);user_fpu_begin ();copy_init_fpstate_to_fpregs (); }
arch/x86/math-emu/fpu_entry.c +0 -3
@@ -113,9 +113,6 @@ void math_emulate(struct math_emu_info *info)unsigned long code_base =0 ;unsigned long code_limit =0 ;/* Initialized to stop compiler warnings */ struct desc_struct code_descriptor; -struct fpu *fpu = ¤t->thread.fpu; - -fpu__initialize (fpu);#ifdef RE_ENTRANT_CHECKING if (emulating) {
Signature · 1 line
2.20.0.rc1
Borislav Petkov
maintainerRe: [PATCH 11/29] x86/fpu: Always init the `state' in fpu__clear()
Quoted 16 lines · Sebastian Andrzej Siewior
fpu__clear()only initializes the `state' if the FPU is present. This initialisation is also required for the FPU-less system and takes placemath_emulate(). Sincefpu__initialize()only performs the initialization if ->initialized is zero it does not matter that it is invoked each time an opcode is emulated. It makes the removal of ->initialized easier if the struct is also initialized in FPU-less case at the same time.Move
fpu__initialize()before the FPU check so it is also performed in FPU-less case.Signed-off-by Sebastian Andrzej Siewior <[email protected]>--- arch/x86/include/asm/fpu/internal.h | 1 - arch/x86/kernel/fpu/core.c | 5 ++--- arch/x86/math-emu/fpu_entry.c | 3 --- 3 files changed, 2 insertions(+), 7 deletions(-)
...
arch/x86/math-emu/fpu_entry.c void math_emulate(struct math_emu_info *info) · line 118
Quoted 11 lines
arch/x86/math-emu/fpu_entry.c +0
-3@@ -113,9 +113,6 @@ void math_emulate(struct math_emu_info *info)unsigned long code_base =0 ;unsigned long code_limit =0 ;/* Initialized to stop compiler warnings */ struct desc_struct code_descriptor; -struct fpu *fpu = ¤t->thread.fpu; - -fpu__initialize (fpu);
Ok, you're removing it here but where is the FPU going to be initialized now in the FPU-less case?
IOW, it is not clear to me where fpu__clear() get called for the FPU emulation case now...
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [PATCH 11/29] x86/fpu: Always init the `state' in fpu__clear()
Quoted 15 lines · Borislav Petkov
arch/x86/math-emu/fpu_entry.c void math_emulate(struct math_emu_info *info) · line 118
Quoted 11 lines
arch/x86/math-emu/fpu_entry.c +0
-3@@ -113,9 +113,6 @@ void math_emulate(struct math_emu_info *info)unsigned long code_base =0 ;unsigned long code_limit =0 ;/* Initialized to stop compiler warnings */ struct desc_struct code_descriptor; -struct fpu *fpu = ¤t->thread.fpu; - -fpu__initialize (fpu);Ok, you're removing it here but where is the FPU going to be initialized now in the FPU-less case?
Quoted 2 lines
IOW, it is not clear to me where
fpu__clear()get called for the FPU emulation case now...
During fork() fpu__initialize() is called for both (with and without FPU) systems. The difference is made in fpu__clear() where the init is avoided in the FPU-less case. With this hunk:
arch/x86/kernel/fpu/core.c +1 -1
@@ -365,8 +364,8 @@ void fpu__clear(struct fpu *fpu)/* * Make sure fpstate is cleared and initialized. */ +fpu__initialize (fpu);if (static_cpu_has (X86_FEATURE_FPU )) { -fpu__initialize (fpu);user_fpu_begin ();copy_init_fpstate_to_fpregs (); }
we have the initialization in both cases.
The only "negative" thing might be that we now have a memset(, 0, 684) (fpstate_init_soft()) during fork which would be avoided in case the FPU-less system has a userland which is compiled with a soft-FPU toolchain.
I would worry about it once someone complains (and I doubt someone complains even if we remove soft FPU support).
Sebastian
PATCH 12/29 x86/fpu: Remove fpu->initialized usage in copy_fpstate_to_sigframe()
In reply to Sebastian Andrzej SiewiorSince ->initialized is always true for user tasks and kernel threads don't get this far, we always save the registers directly to userspace.
Remove check for ->initialized because it is always true and remove the false condition.
--- arch/x86/kernel/fpu/signal.c | 30 ++++++------------------------ 1 file changed, 6 insertions(+), 24 deletions(-)
arch/x86/kernel/fpu/signal.c +6 -24
@@ -157,7 +157,6 @@ static inline int copy_fpregs_to_sigframe(struct xregs_state __user *buf)int copy_fpstate_to_sigframe (void __user *buf,void __user *buf_fx,int size) {struct fpu *fpu = ¤t->thread.fpu; -struct xregs_state *xsave = &fpu->state.xsave;struct task_struct *tsk = current;int ia32_fxstate = (buf != buf_fx);
@@ -172,29 +171,12 @@ int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)sizeof (struct user_i387_ia32_struct ),NULL , (struct _fpstate_32 __user *) buf) ? -1 : 1; -if (fpu->initialized ||using_compacted_format ()) { -/* Save the live register state to the user directly. */ -if (copy_fpregs_to_sigframe (buf_fx)) -return -1 ; -/* Update the thread's fxstate to save the fsave header. */ -if (ia32_fxstate) -copy_fxregs_to_kernel (fpu); - }else { -/* -* It is a *bug* if kernel uses compacted-format for xsave -* area and we copy it out directly to a signal frame. It -* should have been handled above by saving the registers -* directly. -*/ -if (boot_cpu_has (X86_FEATURE_XSAVES )) { -WARN_ONCE (1 ,"x86/fpu: saving compacted-format xsave area to a signal frame!\n" ); -return -1 ; - } - -fpstate_sanitize_xstate (fpu); -if (__copy_to_user (buf_fx, xsave, fpu_user_xstate_size)) -return -1 ; - } +/* Save the live register state to the user directly. */ +if (copy_fpregs_to_sigframe (buf_fx)) +return -1 ; +/* Update the thread's fxstate to save the fsave header. */ +if (ia32_fxstate) +copy_fxregs_to_kernel (fpu);/* Save the fsave header for the 32-bit frames. */ if ((ia32_fxstate || !use_fxsr ()) &&save_fsave_header (tsk, buf))
Signature · 1 line
2.20.0.rc1
PATCH 13/29 x86/fpu: Don't save fxregs for ia32 frames in copy_fpstate_to_sigframe()
In reply to Sebastian Andrzej SiewiorWhy does copy_fpstate_to_sigframe() do copy_fxregs_to_kernel() in the ia32_fxstate case? I don't know. It just does.
Maybe it was required at some point, maybe it was added by accident and nobody noticed it because it makes no difference.
In copy_fpstate_to_sigframe() we stash the FPU state into the task's stackframe. Then the CPU's FPU registers (and its fpu->state) are cleared (handle_signal() does fpu__clear()). So it makes *no* difference what happens to fpu->state after copy_fpregs_to_sigframe().
Remove copy_fxregs_to_kernel() since it does not matter what it does and save a few cycles.
--- arch/x86/kernel/fpu/signal.c | 3 --- 1 file changed, 3 deletions(-)
arch/x86/kernel/fpu/signal.c +0 -3
@@ -174,9 +174,6 @@ int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)/* Save the live register state to the user directly. */ if (copy_fpregs_to_sigframe (buf_fx))return -1 ; -/* Update the thread's fxstate to save the fsave header. */ -if (ia32_fxstate) -copy_fxregs_to_kernel (fpu);/* Save the fsave header for the 32-bit frames. */ if ((ia32_fxstate || !use_fxsr ()) &&save_fsave_header (tsk, buf))
Signature · 1 line
2.20.0.rc1
PATCH 14/29 x86/fpu: Remove fpu->initialized
In reply to Sebastian Andrzej SiewiorThe `initialized' member of the fpu struct is always set to one user tasks and zero for kernel tasks. This avoids saving/restoring the FPU registers for kernel threads.
I expect that fpu->initialized is always 1 and the 0 case has been removed or is not important. For instance fpu__drop() sets the value to zero and its caller call either fpu__initialize() (which would set it back to one) or don't return to userland.
The context switch code (switch_fpu_prepare() + switch_fpu_finish()) can't unconditionally save/restore registers for kernel threads. I have no idea what will happen if we restore a zero FPU context for the kernel thread (since it never was initialized). Also it has been agreed that for PKRU we don't want a random state (inherited from the previous task) but a deterministic one.
For kernel_fpu_begin() (+end) the situation is similar: The kernel test bot told me, that EFI with runtime services uses this before alternatives_patched is true. Which means that this function is used too early and it wasn't the case before.
For those two cases current->mm is used to determine between user & kernel thread. For kernel_fpu_begin() we skip save/restore of the FPU registers.
During the context switch into a kernel thread we don't do anything. There is no reason to save the FPU state of a kernel thread.
--- arch/x86/ia32/ia32_signal.c | 17 +++----- arch/x86/include/asm/fpu/internal.h | 15 +++---- arch/x86/include/asm/fpu/types.h | 9 ---- arch/x86/include/asm/trace/fpu.h | 5 +-- arch/x86/kernel/fpu/core.c | 68 ++++++++--------------------- arch/x86/kernel/fpu/init.c | 2 - arch/x86/kernel/fpu/regset.c | 19 ++------ arch/x86/kernel/fpu/xstate.c | 2 - arch/x86/kernel/process_32.c | 4 +- arch/x86/kernel/process_64.c | 4 +- arch/x86/kernel/signal.c | 17 +++----- arch/x86/mm/pkeys.c | 7 +-- 12 files changed, 49 insertions(+), 120 deletions(-)
arch/x86/ia32/ia32_signal.c +6 -11
@@ -216,8 +216,7 @@ static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,size_t frame_size, void __user **fpstate) { - struct fpu *fpu = ¤t->thread.fpu; -unsigned long sp; + unsigned long sp, fx_aligned, math_size;/* Default to using normal stack */ sp = regs->sp;
@@ -231,15 +230,11 @@ static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs, ksig->ka.sa.sa_restorer) sp = (unsigned long ) ksig->ka.sa.sa_restorer; -if (fpu->initialized) { -unsigned long fx_aligned, math_size; - - sp =fpu__alloc_mathframe (sp,1 , &fx_aligned, &math_size); - *fpstate = (struct _fpstate_32 __user *) sp; -if (copy_fpstate_to_sigframe (*fpstate, (void __user *)fx_aligned, - math_size) <0 ) -return (void __user *)-1L ; - } + sp =fpu__alloc_mathframe (sp,1 , &fx_aligned, &math_size); + *fpstate = (struct _fpstate_32 __user *) sp; +if (copy_fpstate_to_sigframe (*fpstate, (void __user *)fx_aligned, + math_size) <0 ) +return (void __user *)-1L ; sp -= frame_size;/* Align the stack pointer according to the i386 ABI,
arch/x86/include/asm/fpu/internal.h +7 -8
@@ -527,7 +527,7 @@ static inline void fpregs_activate(struct fpu *fpu)static inline void switch_fpu_prepare (struct fpu *old_fpu,int cpu) { -if (static_cpu_has (X86_FEATURE_FPU ) && old_fpu->initialized) { +if (static_cpu_has (X86_FEATURE_FPU ) && current->mm) {if (!copy_fpregs_to_fpstate (old_fpu)) old_fpu->last_cpu =-1 ;else
@@ -535,8 +535,7 @@ switch_fpu_prepare(struct fpu *old_fpu, int cpu)/* But leave fpu_fpregs_owner_ctx! */ trace_x86_fpu_regs_deactivated (old_fpu); - }else - old_fpu->last_cpu =-1 ; + } }/*
@@ -549,12 +548,12 @@ switch_fpu_prepare(struct fpu *old_fpu, int cpu)*/ static inline void switch_fpu_finish (struct fpu *new_fpu,int cpu) { -bool preload =static_cpu_has (X86_FEATURE_FPU ) && - new_fpu->initialized; +if (static_cpu_has (X86_FEATURE_FPU )) { +if (!fpregs_state_valid (new_fpu, cpu)) { +if (current->mm) +copy_kernel_to_fpregs (&new_fpu->state); + } -if (preload) { -if (!fpregs_state_valid (new_fpu, cpu)) -copy_kernel_to_fpregs (&new_fpu->state);fpregs_activate (new_fpu); } }
arch/x86/include/asm/fpu/types.h +0 -9
@@ -293,15 +293,6 @@ struct fpu {*/ unsigned int last_cpu; -/* -* @initialized: -* -* This flag indicates whether this context is initialized: if the task -* is not running then we can restore from this context, if the task -* is running then we should save into this context. -*/ -unsigned char initialized; -/* * @state: *
arch/x86/include/asm/trace/fpu.h +1 -4
@@ -13,22 +13,19 @@ DECLARE_EVENT_CLASS(x86_fpu,TP_STRUCT__entry (__field (struct fpu *,fpu ) -__field (bool, initialized)__field (u64, xfeatures)__field (u64, xcomp_bv) ),TP_fast_assign ( __entry->fpu = fpu; - __entry->initialized = fpu->initialized;if (boot_cpu_has (X86_FEATURE_OSXSAVE )) { __entry->xfeatures = fpu->state.xsave.header.xfeatures; __entry->xcomp_bv = fpu->state.xsave.header.xcomp_bv; } ), -TP_printk ("x86/fpu: %p initialized: %d xfeatures: %llx xcomp_bv: %llx" , +TP_printk ("x86/fpu: %p xfeatures: %llx xcomp_bv: %llx" , __entry->fpu, - __entry->initialized, __entry->xfeatures, __entry->xcomp_bv )
arch/x86/kernel/fpu/core.c +19 -49
130 lines not on this page · raw message
arch/x86/kernel/fpu/init.c +0 -2
@@ -239,8 +239,6 @@ static void __init fpu__init_system_ctx_switch(void)WARN_ON_FPU (!on_boot_cpu); on_boot_cpu =0 ; - -WARN_ON_FPU (current->thread.fpu.initialized); }/*
arch/x86/kernel/fpu/regset.c +4 -15
@@ -15,16 +15,12 @@*/ int regset_fpregs_active (struct task_struct *target,const struct user_regset *regset) { -struct fpu *target_fpu = &target->thread.fpu; - -return target_fpu->initialized ? regset->n :0 ; +return regset->n; }int regset_xregset_fpregs_active (struct task_struct *target,const struct user_regset *regset) { -struct fpu *target_fpu = &target->thread.fpu; - -if (boot_cpu_has (X86_FEATURE_FXSR ) && target_fpu->initialized) +if (boot_cpu_has (X86_FEATURE_FXSR ))return regset->n;else return 0 ;
@@ -370,16 +366,9 @@ int fpregs_set(struct task_struct *target, const struct user_regset *regset,int dump_fpu (struct pt_regs *regs,struct user_i387_struct *ufpu) {struct task_struct *tsk = current; -struct fpu *fpu = &tsk->thread.fpu; -int fpvalid; - fpvalid = fpu->initialized; -if (fpvalid) - fpvalid = !fpregs_get (tsk,NULL , -0 ,sizeof (struct user_i387_ia32_struct ), - ufpu,NULL ); - -return fpvalid; +return !fpregs_get (tsk,NULL ,0 ,sizeof (struct user_i387_ia32_struct ), + ufpu,NULL ); }EXPORT_SYMBOL (dump_fpu);
arch/x86/kernel/fpu/xstate.c +0 -2
@@ -892,8 +892,6 @@ const void *get_xsave_field_ptr(int xsave_state) {struct fpu *fpu = ¤t->thread.fpu; -if (!fpu->initialized) -return NULL ;/* * fpu__save() takes the CPU's xstate registers * and saves them off to the 'fpu memory buffer.
arch/x86/kernel/process_32.c +2 -2
@@ -292,10 +292,10 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)if (prev->gs | next->gs)lazy_load_gs (next->gs); -switch_fpu_finish (next_fpu, cpu); -this_cpu_write (current_task, next_p); +switch_fpu_finish (next_fpu, cpu); +/* Load the Intel cache allocation PQR MSR. */ intel_rdt_sched_in ();
arch/x86/kernel/process_64.c +2 -2
@@ -604,14 +604,14 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)x86_fsgsbase_load (prev, next); -switch_fpu_finish (next_fpu, cpu); -/* * Switch the PDA and FPU contexts. */ this_cpu_write (current_task, next_p);this_cpu_write (cpu_current_top_of_stack,task_top_of_stack (next_p)); +switch_fpu_finish (next_fpu, cpu); +/* Reload sp0. */ update_task_stack (next_p);
arch/x86/kernel/signal.c +7 -10
@@ -246,7 +246,7 @@ get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,unsigned long sp = regs->sp;unsigned long buf_fx =0 ;int onsigstack =on_sig_stack (sp); -struct fpu *fpu = ¤t->thread.fpu; +int ret;/* redzone */ if (IS_ENABLED (CONFIG_X86_64 ))
@@ -265,11 +265,9 @@ get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size, sp = (unsigned long ) ka->sa.sa_restorer; } -if (fpu->initialized) { - sp =fpu__alloc_mathframe (sp,IS_ENABLED (CONFIG_X86_32 ), - &buf_fx, &math_size); - *fpstate = (void __user *)sp; - } + sp =fpu__alloc_mathframe (sp,IS_ENABLED (CONFIG_X86_32 ), + &buf_fx, &math_size); + *fpstate = (void __user *)sp; sp =align_sigframe (sp - frame_size);
@@ -281,8 +279,8 @@ get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,return (void __user *)-1L ;/* save i387 and extended state */ -if (fpu->initialized && -copy_fpstate_to_sigframe (*fpstate, (void __user *)buf_fx, math_size) <0 ) + ret =copy_fpstate_to_sigframe (*fpstate, (void __user *)buf_fx, math_size); +if (ret <0 )return (void __user *)-1L ;return (void __user *)sp;
@@ -763,8 +761,7 @@ handle_signal(struct ksignal *ksig, struct pt_regs *regs)/* * Ensure the signal handler starts with the new fpu state. */ -if (fpu->initialized) -fpu__clear (fpu); +fpu__clear (fpu); }signal_setup_done (failed, ksig, stepping); }
arch/x86/mm/pkeys.c +1 -6
@@ -39,17 +39,12 @@ int __execute_only_pkey(struct mm_struct *mm)* dance to set PKRU if we do not need to. Check it * first and assume that if the execute-only pkey is * write-disabled that we do not have to set it -* ourselves. We need preempt off so that nobody -* can make fpregs inactive. +* ourselves. */ -preempt_disable ();if (!need_to_set_mm_pkey && - current->thread.fpu.initialized && !__pkru_allows_read (read_pkru (), execute_only_pkey)) { -preempt_enable ();return execute_only_pkey; } -preempt_enable ();/* * Set up PKRU so that it denies access for everything
Signature · 1 line
2.20.0.rc1
PATCH 15/29 x86/fpu: Remove user_fpu_begin()
In reply to Sebastian Andrzej Siewioruser_fpu_begin() sets fpu_fpregs_owner_ctx to task's fpu struct. This is always the case since there is no lazy FPU anymore.
fpu_fpregs_owner_ctx is used during context switch to decide if it needs to load the saved registers or if the currently loaded registers are valid. It could be skipped during
taskA -> kernel thread -> taskA
because the switch to kernel thread would not alter the CPU's FPU state.
Since this field is always updated during context switch and never invalidated, setting it manually (in user context) makes no difference. A kernel thread with kernel_fpu_begin() block could set fpu_fpregs_owner_ctx to NULL but a kernel thread does not use user_fpu_begin().
This is a leftover from the lazy-FPU time.
Remove user_fpu_begin(), it does not change fpu_fpregs_owner_ctx's content.
--- arch/x86/include/asm/fpu/internal.h | 17 ----------------- arch/x86/kernel/fpu/core.c | 4 +--- arch/x86/kernel/fpu/signal.c | 1 - 3 files changed, 1 insertion(+), 21 deletions(-)
arch/x86/include/asm/fpu/internal.h +0 -17
@@ -558,23 +558,6 @@ static inline void switch_fpu_finish(struct fpu *new_fpu, int cpu) } } -/* -* Needs to be preemption-safe. -* -* NOTE! user_fpu_begin() must be used only immediately before restoring -* the save state. It does not do any saving/restoring on its own. In -* lazy FPU mode, it is just an optimization to avoid a #NM exception, -* the task can lose the FPU right after preempt_enable(). -*/ -static inline void user_fpu_begin (void ) -{ -struct fpu *fpu = ¤t->thread.fpu; - -preempt_disable (); -fpregs_activate (fpu); -preempt_enable (); -} -/* * MXCSR and XCR definitions: */
arch/x86/kernel/fpu/core.c +1 -3
@@ -335,10 +335,8 @@ void fpu__clear(struct fpu *fpu)* Make sure fpstate is cleared and initialized. */ fpu__initialize (fpu); -if (static_cpu_has (X86_FEATURE_FPU )) { -user_fpu_begin (); +if (static_cpu_has (X86_FEATURE_FPU ))copy_init_fpstate_to_fpregs (); - } }/*
arch/x86/kernel/fpu/signal.c +0 -1
@@ -325,7 +325,6 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)* For 64-bit frames and 32-bit fsave frames, restore the user * state to the registers directly (with exceptions handled). */ -user_fpu_begin ();if (copy_user_to_fpregs_zeroing (buf_fx, xfeatures, fx_only)) {fpu__clear (fpu);return -1 ;
Signature · 1 line
2.20.0.rc1
PATCH 16/29 x86/fpu: Add (__)make_fpregs_active helpers
In reply to Sebastian Andrzej SiewiorFrom: Rik van Riel <[email protected]>
Add helper function that ensures the floating point registers for the current task are active. Use with preemption disabled.
--- arch/x86/include/asm/fpu/api.h | 11 +++++++++++ arch/x86/include/asm/fpu/internal.h | 19 +++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-)
arch/x86/include/asm/fpu/api.h +11 -0
@@ -10,6 +10,7 @@#ifndef _ASM_X86_FPU_API_H#define _ASM_X86_FPU_API_H +#include <linux/preempt.h> /* * Use kernel_fpu_begin/end() if you intend to use FPU in kernel context. It
@@ -23,6 +24,16 @@ extern void kernel_fpu_begin(void);extern void kernel_fpu_end (void );extern bool irq_fpu_usable (void ); +static inline void __fpregs_changes_begin (void ) +{ +preempt_disable (); +} + +static inline void __fpregs_changes_end (void ) +{ +preempt_enable (); +} +/* * Query the presence of one or more xfeatures. Works on any legacy CPU as well. *
arch/x86/include/asm/fpu/internal.h +11 -8
@@ -513,6 +513,15 @@ static inline void fpregs_activate(struct fpu *fpu)trace_x86_fpu_regs_activated (fpu); } +static inline void __fpregs_load_activate (struct fpu *fpu,int cpu) +{ +if (!fpregs_state_valid (fpu, cpu)) { +if (current->mm) +copy_kernel_to_fpregs (&fpu->state); +fpregs_activate (fpu); + } +} +/* * FPU state switching for scheduling. *
@@ -548,14 +557,8 @@ switch_fpu_prepare(struct fpu *old_fpu, int cpu)*/ static inline void switch_fpu_finish (struct fpu *new_fpu,int cpu) { -if (static_cpu_has (X86_FEATURE_FPU )) { -if (!fpregs_state_valid (new_fpu, cpu)) { -if (current->mm) -copy_kernel_to_fpregs (&new_fpu->state); - } - -fpregs_activate (new_fpu); - } +if (static_cpu_has (X86_FEATURE_FPU )) +__fpregs_load_activate (new_fpu, cpu); }/*
Signature · 1 line
2.20.0.rc1
PATCH 17/29 x86/fpu: Make __raw_xsave_addr() use feature number instead of mask
In reply to Sebastian Andrzej SiewiorMost users of __raw_xsave_addr() use a feature number, shift it to a mask and then __raw_xsave_addr() shifts it back to the feature number.
Make __raw_xsave_addr() use the feature number as an argument.
--- arch/x86/kernel/fpu/xstate.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
arch/x86/kernel/fpu/xstate.c +11 -11
@@ -805,20 +805,18 @@ void fpu__resume_cpu(void) }/* -* Given an xstate feature mask, calculate where in the xsave +* Given an xstate feature nr, calculate where in the xsave * buffer the state is. Callers should ensure that the buffer * is valid. */ -void *__raw_xsave_addr (struct xregs_state *xsave,int xstate_feature_mask) +static void *__raw_xsave_addr (struct xregs_state *xsave,int xfeature_nr) { -int feature_nr =fls64 (xstate_feature_mask) -1 ; - -if (!xfeature_enabled (feature_nr)) { +if (!xfeature_enabled (xfeature_nr)) {WARN_ON_FPU (1 );return NULL ; } -return (void *)xsave + xstate_comp_offsets[feature_nr]; +return (void *)xsave + xstate_comp_offsets[xfeature_nr]; }/* * Given the xsave area and a state inside, this function returns the
@@ -840,6 +838,7 @@ void *__raw_xsave_addr(struct xregs_state *xsave, int xstate_feature_mask)*/ void *get_xsave_addr (struct xregs_state *xsave,int xstate_feature) { +int xfeature_nr;/* * Do we even *have* xsave state? */
@@ -867,7 +866,8 @@ void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature)if (!(xsave->header.xfeatures & xstate_feature))return NULL ; -return __raw_xsave_addr (xsave, xstate_feature); + xfeature_nr =fls64 (xstate_feature) -1 ; +return __raw_xsave_addr (xsave, xfeature_nr); }EXPORT_SYMBOL_GPL (get_xsave_addr);
@@ -1014,7 +1014,7 @@ int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int of* Copy only in-use xstates: */ if ((header.xfeatures >> i) &1 ) { -void *src =__raw_xsave_addr (xsave,1 << i); +void *src =__raw_xsave_addr (xsave, i); offset = xstate_offsets[i]; size = xstate_sizes[i];
@@ -1100,7 +1100,7 @@ int copy_xstate_to_user(void __user *ubuf, struct xregs_state *xsave, unsigned i* Copy only in-use xstates: */ if ((header.xfeatures >> i) &1 ) { -void *src =__raw_xsave_addr (xsave,1 << i); +void *src =__raw_xsave_addr (xsave, i); offset = xstate_offsets[i]; size = xstate_sizes[i];
@@ -1157,7 +1157,7 @@ int copy_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf)u64 mask = ((u64 )1 << i);if (hdr.xfeatures & mask) { -void *dst =__raw_xsave_addr (xsave,1 << i); +void *dst =__raw_xsave_addr (xsave, i); offset = xstate_offsets[i]; size = xstate_sizes[i];
@@ -1211,7 +1211,7 @@ int copy_user_to_xstate(struct xregs_state *xsave, const void __user *ubuf)u64 mask = ((u64 )1 << i);if (hdr.xfeatures & mask) { -void *dst =__raw_xsave_addr (xsave,1 << i); +void *dst =__raw_xsave_addr (xsave, i); offset = xstate_offsets[i]; size = xstate_sizes[i];
Signature · 1 line
2.20.0.rc1
PATCH 18/29 x86/fpu: Make get_xsave_field_ptr() and get_xsave_addr() use feature number instead of mask
In reply to Sebastian Andrzej SiewiorAfter changing the argument of __raw_xsave_addr() from a mask to number Dave suggested to check if it makes sense to do the same for get_xsave_addr(). As it turns out it does. Only get_xsave_addr() needs the mask to check if the requested feature is part of what is support/saved and then uses the number again. The shift operation is cheaper compared to "find last bit set". Also, the feature number uses less opcode space compared to the mask :)
Make get_xsave_addr() argument a xfeature number instead of mask and fix up its callers.
As part of this use xfeature_nr and xfeature_mask consistently. This results in changes to the kvm code as:
feature -> xfeature_mask index -> xfeature_nr
--- arch/x86/include/asm/fpu/xstate.h | 4 ++-- arch/x86/kernel/fpu/xstate.c | 23 +++++++++++------------ arch/x86/kernel/traps.c | 2 +- arch/x86/kvm/x86.c | 28 ++++++++++++++-------------- arch/x86/mm/mpx.c | 6 +++--- 5 files changed, 31 insertions(+), 32 deletions(-)
arch/x86/include/asm/fpu/xstate.h +2 -2
@@ -46,8 +46,8 @@ extern void __init update_regset_xstate_info(unsigned int size, u64 xstate_mask);void fpu__xstate_clear_all_cpu_caps (void ); -void *get_xsave_addr (struct xregs_state *xsave,int xstate); -const void *get_xsave_field_ptr (int xstate_field); +void *get_xsave_addr (struct xregs_state *xsave,int xfeature_nr); +const void *get_xsave_field_ptr (int xfeature_nr);int using_compacted_format (void );int copy_xstate_to_kernel (void *kbuf,struct xregs_state *xsave,unsigned int offset,unsigned int size);int copy_xstate_to_user (void __user *ubuf,struct xregs_state *xsave,unsigned int offset,unsigned int size);
arch/x86/kernel/fpu/xstate.c +11 -12
@@ -830,15 +830,15 @@ static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr)* * Inputs: * xstate: the thread's storage area for all FPU data -* xstate_feature: state which is defined in xsave.h (e.g. -* XFEATURE_MASK_FP, XFEATURE_MASK_SSE, etc...) +* xfeature_nr: state which is defined in xsave.h (e.g. XFEATURE_FP, +* XFEATURE_SSE, etc...) * Output: * address of the state in the xsave area, or NULL if the * field is not present in the xsave buffer. */ -void *get_xsave_addr (struct xregs_state *xsave,int xstate_feature) +void *get_xsave_addr (struct xregs_state *xsave,int xfeature_nr) { -int xfeature_nr; +u64 xfeature_mask =1ULL << xfeature_nr;/* * Do we even *have* xsave state? */
@@ -850,11 +850,11 @@ void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature)* have not enabled. Remember that pcntxt_mask is * what we write to the XCR0 register. */ -WARN_ONCE (!(xfeatures_mask & xstate_feature), +WARN_ONCE (!(xfeatures_mask & xfeature_mask),"get of unsupported state" );/* * This assumes the last 'xsave*' instruction to -* have requested that 'xstate_feature' be saved. +* have requested that 'xfeature_mask' be saved. * If it did not, we might be seeing and old value * of the field in the buffer. *
@@ -863,10 +863,9 @@ void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature)* or because the "init optimization" caused it * to not be saved. */ -if (!(xsave->header.xfeatures & xstate_feature)) +if (!(xsave->header.xfeatures & xfeature_mask))return NULL ; - xfeature_nr =fls64 (xstate_feature) -1 ;return __raw_xsave_addr (xsave, xfeature_nr); }EXPORT_SYMBOL_GPL (get_xsave_addr);
@@ -882,13 +881,13 @@ EXPORT_SYMBOL_GPL(get_xsave_addr);* Note that this only works on the current task. * * Inputs: -* @xsave_state: state which is defined in xsave.h (e.g. XFEATURE_MASK_FP, -* XFEATURE_MASK_SSE, etc...) +* @xfeature_nr: state which is defined in xsave.h (e.g. XFEATURE_FP, +* XFEATURE_SSE, etc...) * Output: * address of the state in the xsave area or NULL if the state * is not present or is in its 'init state'. */ -const void *get_xsave_field_ptr (int xsave_state) +const void *get_xsave_field_ptr (int xfeature_nr) {struct fpu *fpu = ¤t->thread.fpu;
@@ -898,7 +897,7 @@ const void *get_xsave_field_ptr(int xsave_state)*/ fpu__save (fpu); -return get_xsave_addr (&fpu->state.xsave, xsave_state); +return get_xsave_addr (&fpu->state.xsave, xfeature_nr); }#ifdef CONFIG_ARCH_HAS_PKEYS
arch/x86/kernel/traps.c +1 -1
@@ -455,7 +455,7 @@ dotraplinkage void do_bounds(struct pt_regs *regs, long error_code)* which is all zeros which indicates MPX was not * responsible for the exception. */ - bndcsr =get_xsave_field_ptr (XFEATURE_MASK_BNDCSR ); + bndcsr =get_xsave_field_ptr (XFEATURE_BNDCSR );if (!bndcsr) goto exit_trap;
arch/x86/kvm/x86.c +14 -14
@@ -3650,15 +3650,15 @@ static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)*/ valid = xstate_bv & ~XFEATURE_MASK_FPSSE ;while (valid) { -u64 feature = valid & -valid; -int index =fls64 (feature) -1 ; -void *src =get_xsave_addr (xsave, feature); +u64 xfeature_mask = valid & -valid; +int xfeature_nr =fls64 (xfeature_mask) -1 ; +void *src =get_xsave_addr (xsave, xfeature_nr);if (src) {u32 size, offset, ecx, edx; -cpuid_count (XSTATE_CPUID , index, +cpuid_count (XSTATE_CPUID , xfeature_nr, &size, &offset, &ecx, &edx); -if (feature ==XFEATURE_MASK_PKRU ) +if (xfeature_nr ==XFEATURE_PKRU )memcpy (dest + offset, &vcpu->arch.pkru,sizeof (vcpu->arch.pkru));else
@@ -3666,7 +3666,7 @@ static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu) } - valid -= feature; + valid -= xfeature_mask; } }
@@ -3693,22 +3693,22 @@ static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)*/ valid = xstate_bv & ~XFEATURE_MASK_FPSSE ;while (valid) { -u64 feature = valid & -valid; -int index =fls64 (feature) -1 ; -void *dest =get_xsave_addr (xsave, feature); +u64 xfeature_mask = valid & -valid; +int xfeature_nr =fls64 (xfeature_mask) -1 ; +void *dest =get_xsave_addr (xsave, xfeature_nr);if (dest) {u32 size, offset, ecx, edx; -cpuid_count (XSTATE_CPUID , index, +cpuid_count (XSTATE_CPUID , xfeature_nr, &size, &offset, &ecx, &edx); -if (feature ==XFEATURE_MASK_PKRU ) +if (xfeature_nr ==XFEATURE_PKRU )memcpy (&vcpu->arch.pkru, src + offset,sizeof (vcpu->arch.pkru));else memcpy (dest, src + offset, size); } - valid -= feature; + valid -= xfeature_mask; } }
@@ -8704,11 +8704,11 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)if (init_event)kvm_put_guest_fpu (vcpu); mpx_state_buffer =get_xsave_addr (&vcpu->arch.guest_fpu.state.xsave, -XFEATURE_MASK_BNDREGS ); +XFEATURE_BNDREGS );if (mpx_state_buffer)memset (mpx_state_buffer,0 ,sizeof (struct mpx_bndreg_state )); mpx_state_buffer =get_xsave_addr (&vcpu->arch.guest_fpu.state.xsave, -XFEATURE_MASK_BNDCSR ); +XFEATURE_BNDCSR );if (mpx_state_buffer)memset (mpx_state_buffer,0 ,sizeof (struct mpx_bndcsr ));if (init_event)
arch/x86/mm/mpx.c +3 -3
@@ -142,7 +142,7 @@ int mpx_fault_info(struct mpx_fault_info *info, struct pt_regs *regs) goto err_out; }/* get bndregs field from current task's xsave area */ - bndregs =get_xsave_field_ptr (XFEATURE_MASK_BNDREGS ); + bndregs =get_xsave_field_ptr (XFEATURE_BNDREGS );if (!bndregs) { err = -EINVAL ; goto err_out;
@@ -190,7 +190,7 @@ static __user void *mpx_get_bounds_dir(void)* The bounds directory pointer is stored in a register * only accessible if we first do an xsave. */ - bndcsr =get_xsave_field_ptr (XFEATURE_MASK_BNDCSR ); + bndcsr =get_xsave_field_ptr (XFEATURE_BNDCSR );if (!bndcsr)return MPX_INVALID_BOUNDS_DIR ;
@@ -376,7 +376,7 @@ static int do_mpx_bt_fault(void)const struct mpx_bndcsr *bndcsr;struct mm_struct *mm = current->mm; - bndcsr =get_xsave_field_ptr (XFEATURE_MASK_BNDCSR ); + bndcsr =get_xsave_field_ptr (XFEATURE_BNDCSR );if (!bndcsr)return -EINVAL ;/*
Signature · 1 line
2.20.0.rc1
PATCH 19/29 x86/fpu: Only write PKRU if it is different from current
In reply to Sebastian Andrzej SiewiorDave Hansen says that the wrpkru' is more expensive than rdpkru'. It has a higher cycle cost and it's also practically a (light) speculation barrier.
As an optimisation read the current PKRU value and only write the new one if it is different.
--- arch/x86/include/asm/special_insns.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
arch/x86/include/asm/special_insns.h +12 -1
@@ -107,7 +107,7 @@ static inline u32 __read_pkru(void)return pkru; } -static inline void __write_pkru (u32 pkru) +static inline void __write_pkru_insn (u32 pkru) {u32 ecx =0 , edx =0 ;
@@ -118,6 +118,17 @@ static inline void __write_pkru(u32 pkru) asmvolatile (".byte 0x0f,0x01,0xef\n\t" : : "a" (pkru), "c"(ecx), "d"(edx)); } + +static inline void __write_pkru (u32 pkru) +{ +/* +* Writting PKRU is expensive. Only write the PKRU value if it is +* different from the current one. +*/ +if (pkru ==__read_pkru ()) +return ; +__write_pkru_insn (pkru); +}#else static inline u32 __read_pkru (void ) {
Signature · 1 line
2.20.0.rc1
PATCH 20/29 x86/pkeys: Don't check if PKRU is zero before writting it
In reply to Sebastian Andrzej Siewiorwrite_pkru() checks if the current value is the same as the expected value. So instead just checking if the current and new value is zero (and skip the write in such a case) we can benefit from that.
Remove the zero check of PKRU, write_pkru() provides a similar check.
--- arch/x86/mm/pkeys.c | 7 ------- 1 file changed, 7 deletions(-)
arch/x86/mm/pkeys.c +0 -7
@@ -142,13 +142,6 @@ u32 init_pkru_value = PKRU_AD_KEY( 1) | PKRU_AD_KEY( 2) | PKRU_AD_KEY( 3) |void copy_init_pkru_to_fpregs (void ) {u32 init_pkru_value_snapshot =READ_ONCE (init_pkru_value); -/* -* Any write to PKRU takes it out of the XSAVE 'init -* state' which increases context switch cost. Avoid -* writing 0 when PKRU was already 0. -*/ -if (!init_pkru_value_snapshot && !read_pkru ()) -return ;/* * Override the PKRU state that came from 'init_fpstate' * with the baseline from the process.
Signature · 1 line
2.20.0.rc1
PATCH 21/29 x86/fpu: Eager switch PKRU state
In reply to Sebastian Andrzej SiewiorFrom: Rik van Riel <[email protected]>
While most of a task's FPU state is only needed in user space, the protection keys need to be in place immediately after a context switch.
The reason is that any access to userspace memory while running in kernel mode also need to abide by the memory permissions specified in the protection keys.
The "eager switch" is a preparation for loading the FPU state on return to userland. Instead of decoupling PKRU state from xstate I update PKRU within xstate on write operations by the kernel.
The read/write_pkru() is moved to another header file so it can easily accessed from pgtable.h and fpu/internal.h.
For user tasks we should always get the PKRU from the xsave area and it should not change anything because the PKRU value was loaded as part of FPU restore.
For kernel kernel threads we now will have the default "allow everything" written. Before this commit the kernel thread would end up with a random value which it inherited from the previous user task.
--- arch/x86/include/asm/fpu/internal.h | 20 ++++++++++++++++++-- arch/x86/include/asm/fpu/xstate.h | 1 + 2 files changed, 19 insertions(+), 2 deletions(-)
arch/x86/include/asm/fpu/internal.h +18 -2
@@ -557,8 +557,24 @@ switch_fpu_prepare(struct fpu *old_fpu, int cpu)*/ static inline void switch_fpu_finish (struct fpu *new_fpu,int cpu) { -if (static_cpu_has (X86_FEATURE_FPU )) -__fpregs_load_activate (new_fpu, cpu); +struct pkru_state *pk; +u32 pkru_val =0 ; + +if (!static_cpu_has (X86_FEATURE_FPU )) +return ; + +__fpregs_load_activate (new_fpu, cpu); + +if (!cpu_feature_enabled (X86_FEATURE_OSPKE )) +return ; + +if (current->mm) { + pk =get_xsave_addr (&new_fpu->state.xsave,XFEATURE_PKRU ); +WARN_ON_ONCE (!pk); +if (pk) + pkru_val = pk->pkru; + } +__write_pkru (pkru_val); }/*
arch/x86/include/asm/fpu/xstate.h +1 -0
@@ -5,6 +5,7 @@#include <linux/types.h> #include <asm/processor.h> #include <linux/uaccess.h> +#include <asm/user.h> /* Bit 63 of XCR0 is reserved for future expansion */ #define XFEATURE_MASK_EXTEND (~(XFEATURE_MASK_FPSSE | (1ULL << 63)))
Signature · 1 line
2.20.0.rc1
PATCH 22/29 x86/entry: Add TIF_NEED_FPU_LOAD
In reply to Sebastian Andrzej SiewiorAdd TIF_NEED_FPU_LOAD. This is reserved for loading the FPU registers before returning to userland. This flag must not be set for systems without a FPU.
If this flag is cleared, the CPU's FPU register hold the current content of current()'s FPU register. The in-memory copy (union fpregs_state) is not valid.
If this flag is set, then all of CPU's FPU register may hold a random value (except for PKRU) and it is required to load the content of the FPU register on return to userland.
It is introduced now, so we can add code handling it now before adding the main feature.
--- arch/x86/include/asm/thread_info.h | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/include/asm/thread_info.h +2 -0
@@ -83,6 +83,7 @@ struct thread_info {#define TIF_SYSCALL_EMU 6/* syscall emulation active */ #define TIF_SYSCALL_AUDIT 7/* syscall auditing active */ #define TIF_SECCOMP 8/* secure computing */ +#define TIF_NEED_FPU_LOAD 10/* load FPU on return to userspace */ #define TIF_USER_RETURN_NOTIFY 11/* notify kernel of userspace return */ #define TIF_UPROBE 12/* breakpointed or singlestepping */ #define TIF_PATCH_PENDING 13/* pending live patching update */
@@ -110,6 +111,7 @@ struct thread_info {#define _TIF_SYSCALL_EMU (1 << TIF_SYSCALL_EMU)#define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)#define _TIF_SECCOMP (1 << TIF_SECCOMP) +#define _TIF_NEED_FPU_LOAD (1 << TIF_NEED_FPU_LOAD)#define _TIF_USER_RETURN_NOTIFY (1 << TIF_USER_RETURN_NOTIFY)#define _TIF_UPROBE (1 << TIF_UPROBE)#define _TIF_PATCH_PENDING (1 << TIF_PATCH_PENDING)
Signature · 1 line
2.20.0.rc1
PATCH 23/29 x86/fpu: Always store the registers in copy_fpstate_to_sigframe()
In reply to Sebastian Andrzej SiewiorFrom: Rik van Riel <[email protected]>
copy_fpstate_to_sigframe() stores the registers directly to user space. This is okay because the FPU register are valid and saving it directly avoids saving it into kernel memory and making a copy. However… We can't keep doing this if we are going to restore the FPU registers on the return to userland. It is possible that the FPU registers will be invalidated in the middle of the save operation and this should be done with disabled preemption / BH.
Save the FPU registers to task's FPU struct and copy them to the user memory later on.
This code is extracted from an earlier version of the patchset while there still was lazy-FPU on x86.
--- arch/x86/include/asm/fpu/internal.h | 45 ----------------------------- arch/x86/kernel/fpu/signal.c | 29 +++++++------------ 2 files changed, 10 insertions(+), 64 deletions(-)
arch/x86/include/asm/fpu/internal.h +0 -45
@@ -124,22 +124,6 @@ extern void fpstate_sanitize_xstate(struct fpu *fpu); _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_fprestore) \ : output : input) -static inline int copy_fregs_to_user(struct fregs_state __user *fx) -{ - return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx)); -} - -static inline int copy_fxregs_to_user(struct fxregs_state __user *fx) -{ -if (IS_ENABLED (CONFIG_X86_32 )) - return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx)); -else if (IS_ENABLED (CONFIG_AS_FXSAVEQ )) - return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx)); - -/* See comment in copy_fxregs_to_kernel() below. */ - return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx)); -} - static inline void copy_kernel_to_fxregs(struct fxregs_state *fx) { if (IS_ENABLED(CONFIG_X86_32)) {
@@ -350,35 +334,6 @@ static inline void copy_kernel_to_xregs(struct xregs_state *xstate, u64 mask)XSTATE_XRESTORE (xstate, lmask, hmask); } -/* -* Save xstate to user space xsave area. -* -* We don't use modified optimization because xrstor/xrstors might track -* a different application. -* -* We don't use compacted format xsave area for -* backward compatibility for old applications which don't understand -* compacted format of xsave area. -*/ -static inline int copy_xregs_to_user (struct xregs_state __user *buf) -{ -int err; - -/* -* Clear the xsave header first, so that reserved fields are -* initialized to zero. -*/ - err =__clear_user (&buf->header,sizeof (buf->header)); -if (unlikely (err)) -return -EFAULT ; - -stac (); -XSTATE_OP (XSAVE , buf,-1 ,-1 , err); -clac (); - -return err; -} -/* * Restore xstate from user space xsave area. */
arch/x86/kernel/fpu/signal.c +10 -19
@@ -118,22 +118,6 @@ static inline int save_xstate_epilog(void __user *buf, int ia32_frame)return err; } -static inline int copy_fpregs_to_sigframe (struct xregs_state __user *buf) -{ -int err; - -if (use_xsave ()) - err =copy_xregs_to_user (buf); -else if (use_fxsr ()) - err =copy_fxregs_to_user ((struct fxregs_state __user *) buf); -else - err =copy_fregs_to_user ((struct fregs_state __user *) buf); - -if (unlikely (err) &&__clear_user (buf, fpu_user_xstate_size)) - err = -EFAULT ; -return err; -} -/* * Save the fpu, extended register state to the user signal frame. *
@@ -157,6 +141,7 @@ static inline int copy_fpregs_to_sigframe(struct xregs_state __user *buf)int copy_fpstate_to_sigframe (void __user *buf,void __user *buf_fx,int size) {struct fpu *fpu = ¤t->thread.fpu; +struct xregs_state *xsave = &fpu->state.xsave;struct task_struct *tsk = current;int ia32_fxstate = (buf != buf_fx);
@@ -171,9 +156,15 @@ int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)sizeof (struct user_i387_ia32_struct ),NULL , (struct _fpstate_32 __user *) buf) ? -1 : 1; -/* Save the live register state to the user directly. */ -if (copy_fpregs_to_sigframe (buf_fx)) -return -1 ; +copy_fpregs_to_fpstate (fpu); + +if (using_compacted_format ()) { +copy_xstate_to_user (buf_fx, xsave,0 , size); + }else { +fpstate_sanitize_xstate (fpu); +if (__copy_to_user (buf_fx, xsave, fpu_user_xstate_size)) +return -1 ; + }/* Save the fsave header for the 32-bit frames. */ if ((ia32_fxstate || !use_fxsr ()) &&save_fsave_header (tsk, buf))
Signature · 1 line
2.20.0.rc1
PATCH 24/29 x86/fpu: Prepare copy_fpstate_to_sigframe() for TIF_NEED_FPU_LOAD
In reply to Sebastian Andrzej SiewiorFrom: Rik van Riel <[email protected]>
The FPU registers need only to be saved if TIF_NEED_FPU_LOAD is not set. Otherwise this has been already done and can be skipped.
--- arch/x86/kernel/fpu/signal.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
arch/x86/kernel/fpu/signal.c +10 -1
@@ -156,7 +156,16 @@ int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)sizeof (struct user_i387_ia32_struct ),NULL , (struct _fpstate_32 __user *) buf) ? -1 : 1; -copy_fpregs_to_fpstate (fpu); +__fpregs_changes_begin (); +/* +* If we do not need to load the FPU registers at return to userspace +* then the CPU has the current state and we need to save it. Otherwise +* it is already done and we can skip it. +*/ +if (!test_thread_flag (TIF_NEED_FPU_LOAD )) +copy_fpregs_to_fpstate (fpu); + +__fpregs_changes_end ();if (using_compacted_format ()) {copy_xstate_to_user (buf_fx, xsave,0 , size);
Signature · 1 line
2.20.0.rc1
PATCH 25/29 x86/fpu: Update xstate's PKRU value on write_pkru()
In reply to Sebastian Andrzej SiewiorDuring the context switch the xstate is loaded which also includes the PKRU value.
If xstate is restored on return to userland it is required that the PKRU value in xstate is the same as the one in the CPU.
Save the PKRU in xstate during modification.
--- arch/x86/include/asm/pgtable.h | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
arch/x86/include/asm/pgtable.h +18 -2
@@ -23,6 +23,8 @@#ifndef __ASSEMBLY__#include <asm/x86_init.h> +#include <asm/fpu/xstate.h> +#include <asm/fpu/api.h> extern pgd_t early_top_pgt[PTRS_PER_PGD ];int __init __early_make_pgtable (unsigned long address,pmdval_t pmd);
@@ -133,8 +135,22 @@ static inline u32 read_pkru(void)static inline void write_pkru (u32 pkru) { -if (boot_cpu_has (X86_FEATURE_OSPKE )) -__write_pkru (pkru); +struct pkru_state *pk; + +if (!boot_cpu_has (X86_FEATURE_OSPKE )) +return ; + + pk =get_xsave_addr (¤t->thread.fpu.state.xsave,XFEATURE_PKRU ); +/* +* The PKRU value in xstate needs to be in sync with the value that is +* written to the CPU. The FPU restore on return to userland would +* otherwise load the previous value again. +*/ +__fpregs_changes_begin (); +if (pk) + pk->pkru = pkru; +__write_pkru (pkru); +__fpregs_changes_end (); }static inline int pte_young (pte_t pte)
Signature · 1 line
2.20.0.rc1
PATCH 26/29 x86/fpu: Inline copy_user_to_fpregs_zeroing()
In reply to Sebastian Andrzej SiewiorStart refactoring __fpu__restore_sig() by inlining copy_user_to_fpregs_zeroing().
--- arch/x86/kernel/fpu/signal.c | 42 ++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 23 deletions(-)
arch/x86/kernel/fpu/signal.c +19 -23
@@ -221,28 +221,6 @@ sanitize_restored_xstate(union fpregs_state *state, } } -/* -* Restore the extended state if present. Otherwise, restore the FP/SSE state. -*/ -static inline int copy_user_to_fpregs_zeroing (void __user *buf,u64 xbv,int fx_only) -{ -if (use_xsave ()) { -if ((unsigned long )buf %64 || fx_only) { -u64 init_bv = xfeatures_mask & ~XFEATURE_MASK_FPSSE ; -copy_kernel_to_xregs (&init_fpstate.xsave, init_bv); -return copy_user_to_fxregs (buf); - }else { -u64 init_bv = xfeatures_mask & ~xbv; -if (unlikely (init_bv)) -copy_kernel_to_xregs (&init_fpstate.xsave, init_bv); -return copy_user_to_xregs (buf, xbv); - } - }else if (use_fxsr ()) { -return copy_user_to_fxregs (buf); - }else -return copy_user_to_fregs (buf); -} -static int __fpu__restore_sig (void __user *buf,void __user *buf_fx,int size) {int ia32_fxstate = (buf != buf_fx);
@@ -321,11 +299,29 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)kfree (tmp);return err; }else { +int ret; +/* * For 64-bit frames and 32-bit fsave frames, restore the user * state to the registers directly (with exceptions handled). */ -if (copy_user_to_fpregs_zeroing (buf_fx, xfeatures, fx_only)) { +if (use_xsave ()) { +if ((unsigned long )buf_fx %64 || fx_only) { +u64 init_bv = xfeatures_mask & ~XFEATURE_MASK_FPSSE ; +copy_kernel_to_xregs (&init_fpstate.xsave, init_bv); + ret =copy_user_to_fxregs (buf_fx); + }else { +u64 init_bv = xfeatures_mask & ~xfeatures; +if (unlikely (init_bv)) +copy_kernel_to_xregs (&init_fpstate.xsave, init_bv); + ret =copy_user_to_xregs (buf_fx, xfeatures); + } + }else if (use_fxsr ()) { + ret =copy_user_to_fxregs (buf_fx); + }else + ret =copy_user_to_fregs (buf_fx); + +if (ret) {fpu__clear (fpu);return -1 ; }
Signature · 1 line
2.20.0.rc1
Re: [PATCH 26/29] x86/fpu: Inline copy_user_to_fpregs_zeroing()
Quoted 4 lines · Sebastian Andrzej Siewior
Start refactoring
__fpu__restore_sig()by inliningcopy_user_to_fpregs_zeroing().Signed-off-by Sebastian Andrzej Siewior <[email protected]>
Signature · 2 lines
Cheers, Joey Pabalinas
PATCH 27/29 x86/fpu: Let __fpu__restore_sig() restore the !32bit+fxsr frame from kernel memory
In reply to Sebastian Andrzej SiewiorThe !32bit+fxsr case loads the new state from user memory. In case we restore the FPU state on return to userland we can't do this. It would be required to disable preemption in order to avoid a context switch which would set TIF_NEED_FPU_LOAD. If this happens before the "restore" operation then the loaded registers would become volatile.
Disabling preemption while accessing user memory requires to disable the pagefault handler. An error during XRSTOR would then mean that either a page fault occured (and we have to retry with enabled page fault handler) or a #GP occured because the xstate is bogus (after all the sig-handler can modify it).
In order to avoid that mess, copy the FPU state from userland, validate it and then load it. The copy_users_…() helper are basically the old helper except that they operate on kernel memory and the fault handler just sets the error value and the caller handles it.
--- arch/x86/include/asm/fpu/internal.h | 32 ++++++++++----- arch/x86/kernel/fpu/signal.c | 62 +++++++++++++++++++++++------ 2 files changed, 71 insertions(+), 23 deletions(-)
arch/x86/include/asm/fpu/internal.h +22 -10
@@ -118,6 +118,21 @@ extern void fpstate_sanitize_xstate(struct fpu *fpu); err; \ }) +#define kernel_insn_norestore(insn, output, input...) \ +({ \ + int err; \ + asm volatile("1:" #insn "\n\t" \ + "2:\n" \ + ".section .fixup,\"ax\"\n" \ + "3: movl $-1,%[err]\n" \ + " jmp 2b\n" \ + ".previous\n" \ + _ASM_EXTABLE(1b, 3b) \ + : [err] "=r" (err), output \ + : "0"(0), input); \ + err; \ +}) + #define kernel_insn(insn, output, input...) \ asm volatile("1:" #insn "\n\t" \ "2:\n" \
@@ -138,15 +153,15 @@ static inline void copy_kernel_to_fxregs(struct fxregs_state *fx) } } -static inline int copy_user_to_fxregs (struct fxregs_state __user *fx) +static inline int copy_users_to_fxregs (struct fxregs_state *fx) {if (IS_ENABLED (CONFIG_X86_32 )) - return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx)); + return kernel_insn_norestore(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));else if (IS_ENABLED (CONFIG_AS_FXSAVEQ )) - return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx)); + return kernel_insn_norestore(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));/* See comment in copy_fxregs_to_kernel() below. */ - return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx), + return kernel_insn_norestore(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx), "m" (*fx)); }
@@ -155,9 +170,9 @@ static inline void copy_kernel_to_fregs(struct fregs_state *fx) kernel_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx)); } -static inline int copy_user_to_fregs (struct fregs_state __user *fx) +static inline int copy_users_to_fregs (struct fregs_state *fx) { - return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx)); + return kernel_insn_norestore(frstor %[fx], "=m" (*fx), [fx] "m" (*fx)); }static inline void copy_fxregs_to_kernel (struct fpu *fpu)
@@ -337,16 +352,13 @@ static inline void copy_kernel_to_xregs(struct xregs_state *xstate, u64 mask)/* * Restore xstate from user space xsave area. */ -static inline int copy_user_to_xregs (struct xregs_state __user *buf,u64 mask) +static inline int copy_users_to_xregs (struct xregs_state *xstate,u64 mask) { -struct xregs_state *xstate = ((__force struct xregs_state *)buf);u32 lmask = mask;u32 hmask = mask >>32 ;int err; -stac ();XSTATE_OP (XRSTOR , xstate, lmask, hmask, err); -clac ();return err; }
arch/x86/kernel/fpu/signal.c +49 -13
84 lines not on this page · raw message
Signature · 1 line
2.20.0.rc1
PATCH 28/29 x86/fpu: Merge the two code paths in __fpu__restore_sig()
In reply to Sebastian Andrzej SiewiorThe ia32_fxstate case (32bit with fxsr) and the other (64bit, 32bit without fxsr) restore both from kernel memory and sanitize the content. The !ia32_fxstate version restores missing xstates from "init state" while the ia32_fxstate doesn't and skips it.
Merge the two code paths and keep the !ia32_fxstate version. Copy only the user_i387_ia32_struct data structure in the ia32_fxstate.
--- arch/x86/kernel/fpu/signal.c | 162 ++++++++++++++--------------------- 1 file changed, 65 insertions(+), 97 deletions(-)
arch/x86/kernel/fpu/signal.c +65 -97
@@ -228,6 +228,11 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)struct task_struct *tsk = current;struct fpu *fpu = &tsk->thread.fpu;int state_size = fpu_kernel_xstate_size; +union fpregs_state *state; +void *tmp; +struct user_i387_ia32_struct env; +struct user_i387_ia32_struct *envp =NULL ; +int ret =0 ;u64 xfeatures =0 ;int fx_only =0 ;
@@ -264,106 +269,69 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size) } } + tmp =kzalloc (sizeof (*state) + fpu_kernel_xstate_size +64 ,GFP_KERNEL ); +if (!tmp) +return -ENOMEM ; + state =PTR_ALIGN (tmp,64 ); + +if ((unsigned long )buf_fx %64 ) + fx_only =1 ; + +/* +* For 32-bit frames with fxstate, copy the fxstate so it can be +* reconstructed later. +*/ if (ia32_fxstate) { -/* -* For 32-bit frames with fxstate, copy the user state to the -* thread's fpu state, reconstruct fxstate from the fsave -* header. Validate and sanitize the copied state. -*/ -union fpregs_state *state; -void *tmp; -struct user_i387_ia32_struct env; -int err =0 ; - - tmp =kzalloc (sizeof (*state) + fpu_kernel_xstate_size +64 ,GFP_KERNEL ); -if (!tmp) -return -ENOMEM ; - state =PTR_ALIGN (tmp,64 ); - -if (using_compacted_format ()) { - err =copy_user_to_xstate (&state->xsave, buf_fx); - }else { - err =__copy_from_user (&state->xsave, buf_fx, state_size); - -if (!err && state_size > offsetof(struct xregs_state , header)) - err =validate_xstate_header (&state->xsave.header); - } - -if (err ||__copy_from_user (&env, buf,sizeof (env))) { - err =-1 ; - }else { -sanitize_restored_xstate (state, &env, - xfeatures, fx_only); -copy_kernel_to_fpregs (state); - } - -kfree (tmp); -return err; - }else { -union fpregs_state *state; -void *tmp; -int ret; - - tmp =kzalloc (sizeof (*state) + fpu_kernel_xstate_size +64 ,GFP_KERNEL ); -if (!tmp) -return -ENOMEM ; - state =PTR_ALIGN (tmp,64 ); - -/* -* For 64-bit frames and 32-bit fsave frames, restore the user -* state to the registers directly (with exceptions handled). -*/ -if ((unsigned long )buf_fx %64 ) - fx_only =1 ; - -if (use_xsave () && !fx_only) { -u64 init_bv = xfeatures_mask & ~xfeatures; - -if (using_compacted_format ()) { - ret =copy_user_to_xstate (&state->xsave, buf_fx); - }else { - ret =__copy_from_user (&state->xsave, buf_fx, state_size); - -if (!ret && state_size > offsetof(struct xregs_state , header)) - ret =validate_xstate_header (&state->xsave.header); - } -if (ret) - goto err_out; -sanitize_restored_xstate (state,NULL , xfeatures, - fx_only); - -if (unlikely (init_bv)) -copy_kernel_to_xregs (&init_fpstate.xsave, init_bv); - ret =copy_users_to_xregs (&state->xsave, xfeatures); - - }else if (use_fxsr ()) { - ret =__copy_from_user (&state->fxsave, buf_fx, state_size); -if (ret) - goto err_out; - -if (use_xsave ()) { -u64 init_bv = xfeatures_mask & ~XFEATURE_MASK_FPSSE ; -copy_kernel_to_xregs (&init_fpstate.xsave, init_bv); - } - state->fxsave.mxcsr &= mxcsr_feature_mask; - - ret =copy_users_to_fxregs (&state->fxsave); - }else { - ret =__copy_from_user (&state->fsave, buf_fx, state_size); -if (ret) - goto err_out; - ret =copy_users_to_fregs (buf_fx); - } - -err_out: -kfree (tmp); -if (ret) { -fpu__clear (fpu); -return -1 ; - } + ret =__copy_from_user (&env, buf,sizeof (env)); +if (ret) + goto err_out; + envp = &env; } -return 0 ; +if (use_xsave () && !fx_only) { +u64 init_bv = xfeatures_mask & ~xfeatures; + +if (using_compacted_format ()) { + ret =copy_user_to_xstate (&state->xsave, buf_fx); + }else { + ret =__copy_from_user (&state->xsave, buf_fx, state_size); + +if (!ret && state_size > offsetof(struct xregs_state , header)) + ret =validate_xstate_header (&state->xsave.header); + } +if (ret) + goto err_out; + +sanitize_restored_xstate (state, envp, xfeatures, fx_only); + +if (unlikely (init_bv)) +copy_kernel_to_xregs (&init_fpstate.xsave, init_bv); + ret =copy_users_to_xregs (&state->xsave, xfeatures); + + }else if (use_fxsr ()) { + ret =__copy_from_user (&state->fxsave, buf_fx, state_size); +if (ret) + goto err_out; + +sanitize_restored_xstate (state, envp, xfeatures, fx_only); +if (use_xsave ()) { +u64 init_bv = xfeatures_mask & ~XFEATURE_MASK_FPSSE ; +copy_kernel_to_xregs (&init_fpstate.xsave, init_bv); + } + + ret =copy_users_to_fxregs (&state->fxsave); + }else { + ret =__copy_from_user (&state->fsave, buf_fx, state_size); +if (ret) + goto err_out; + ret =copy_users_to_fregs (buf_fx); + } + +err_out: +kfree (tmp); +if (ret) +fpu__clear (fpu); +return ret; }static inline int xstate_sigframe_size (void )
Signature · 1 line
2.20.0.rc1
PATCH 29/29 x86/fpu: Defer FPU state load until return to userspace
In reply to Sebastian Andrzej SiewiorFrom: Rik van Riel <[email protected]>
Defer loading of FPU state until return to userspace. This gives the kernel the potential to skip loading FPU state for tasks that stay in kernel mode, or for tasks that end up with repeated invocations of kernel_fpu_begin() & kernel_fpu_end().
The __fpregs_changes_{begin|end}() section ensures that the register remain unchanged. Otherwise a context switch or a BH could save the registers to its FPU context and processor's FPU register would became random if beeing modified at the same time.
KVM swaps the host/guest register on entry/exit path. I kept the flow as is. First it ensures that the registers are loaded and then saves the current (host) state before it loads the guest's register. The swap is done at the very end with disabled interrupts so it should not change anymore before theg guest is entered. The read/save version seems to be cheaper compared to memcpy() in a micro benchmark.
Each thread gets TIF_NEED_FPU_LOAD set as part of fork() / fpu__copy(). For kernel threads, this flag gets never cleared which avoids saving / restoring the FPU state for kernel threads and during in-kernel usage of the FPU register.
--- arch/x86/entry/common.c | 8 +++ arch/x86/include/asm/fpu/api.h | 22 +++++- arch/x86/include/asm/fpu/internal.h | 27 +++++--- arch/x86/include/asm/trace/fpu.h | 5 +- arch/x86/kernel/fpu/core.c | 104 +++++++++++++++++++++------- arch/x86/kernel/fpu/signal.c | 47 ++++++++----- arch/x86/kernel/process.c | 2 +- arch/x86/kernel/process_32.c | 5 +- arch/x86/kernel/process_64.c | 5 +- arch/x86/kvm/x86.c | 19 +++-- 10 files changed, 179 insertions(+), 65 deletions(-)
arch/x86/entry/common.c +8 -0
@@ -31,6 +31,7 @@#include <asm/vdso.h> #include <linux/uaccess.h> #include <asm/cpufeature.h> +#include <asm/fpu/api.h> #define CREATE_TRACE_POINTS #include <trace/events/syscalls.h>
@@ -196,6 +197,13 @@ __visible inline void prepare_exit_to_usermode(struct pt_regs *regs)if (unlikely (cached_flags &EXIT_TO_USERMODE_LOOP_FLAGS ))exit_to_usermode_loop (regs, cached_flags); +/* Reload ti->flags; we may have rescheduled above. */ + cached_flags =READ_ONCE (ti->flags); + +fpregs_assert_state_consistent (); +if (unlikely (cached_flags & _TIF_NEED_FPU_LOAD)) +switch_fpu_return (); +#ifdef CONFIG_COMPAT /* * Compat syscalls set TS_COMPAT. Make sure we clear it before
arch/x86/include/asm/fpu/api.h +21 -1
@@ -10,7 +10,7 @@#ifndef _ASM_X86_FPU_API_H#define _ASM_X86_FPU_API_H -#include <linux/preempt.h> +#include <linux/bottom_half.h> /* * Use kernel_fpu_begin/end() if you intend to use FPU in kernel context. It
@@ -23,17 +23,37 @@extern void kernel_fpu_begin (void );extern void kernel_fpu_end (void );extern bool irq_fpu_usable (void ); +extern void fpregs_mark_activate (void ); +/* +* Use __fpregs_changes_begin() while editing CPU's FPU registers or fpu->state. +* A context switch will (and softirq might) save CPU's FPU register to +* fpu->state and set TIF_NEED_FPU_LOAD leaving CPU's FPU registers in a random +* state. +*/ static inline void __fpregs_changes_begin (void ) {preempt_disable (); +local_bh_disable (); }static inline void __fpregs_changes_end (void ) { +local_bh_enable ();preempt_enable (); } +#ifdef CONFIG_X86_DEBUG_FPU +extern void fpregs_assert_state_consistent (void ); +#else +static inline void fpregs_assert_state_consistent (void ) { } +#endif + +/* +* Load the task FPU state before returning to userspace. +*/ +extern void switch_fpu_return (void ); +/* * Query the presence of one or more xfeatures. Works on any legacy CPU as well. *
arch/x86/include/asm/fpu/internal.h +17 -10
@@ -29,7 +29,7 @@ extern void fpu__prepare_write(struct fpu *fpu);extern void fpu__save (struct fpu *fpu);extern int fpu__restore_sig (void __user *buf,int ia32_frame);extern void fpu__drop (struct fpu *fpu); -extern int fpu__copy (struct fpu *dst_fpu,struct fpu *src_fpu); +extern int fpu__copy (struct task_struct *dst,struct task_struct *src);extern void fpu__clear (struct fpu *fpu);extern int fpu__exception_code (struct fpu *fpu,int trap_nr);extern int dump_fpu (struct pt_regs *ptregs,struct user_i387_struct *fpstate);
@@ -480,13 +480,20 @@ static inline void fpregs_activate(struct fpu *fpu)trace_x86_fpu_regs_activated (fpu); } -static inline void __fpregs_load_activate (struct fpu *fpu,int cpu) +static inline void __fpregs_load_activate (void ) { +struct fpu *fpu = ¤t->thread.fpu; +int cpu =smp_processor_id (); + +if (WARN_ON_ONCE (current->mm ==NULL )) +return ; +if (!fpregs_state_valid (fpu, cpu)) { -if (current->mm) -copy_kernel_to_fpregs (&fpu->state); +copy_kernel_to_fpregs (&fpu->state);fpregs_activate (fpu); + fpu->last_cpu = cpu; } +clear_thread_flag (TIF_NEED_FPU_LOAD ); }/*
@@ -497,8 +504,8 @@ static inline void __fpregs_load_activate(struct fpu *fpu, int cpu)* - switch_fpu_prepare() saves the old state. * This is done within the context of the old process. * -* - switch_fpu_finish() restores the new state as -* necessary. +* - switch_fpu_finish() sets TIF_NEED_FPU_LOAD; the floating point state +* will get loaded on return to userspace, or when the kernel needs it. */ static inline void switch_fpu_prepare (struct fpu *old_fpu,int cpu)
@@ -519,10 +526,10 @@ switch_fpu_prepare(struct fpu *old_fpu, int cpu)*/ /* -* Set up the userspace FPU context for the new task, if the task -* has used the FPU. +* Load PKRU from the FPU context if available. Delay loading the loading of the +* complete FPU state until the return to userland. */ -static inline void switch_fpu_finish (struct fpu *new_fpu,int cpu) +static inline void switch_fpu_finish (struct fpu *new_fpu) {struct pkru_state *pk;u32 pkru_val =0 ;
@@ -530,7 +537,7 @@ static inline void switch_fpu_finish(struct fpu *new_fpu, int cpu)if (!static_cpu_has (X86_FEATURE_FPU ))return ; -__fpregs_load_activate (new_fpu, cpu); +set_thread_flag (TIF_NEED_FPU_LOAD );if (!cpu_feature_enabled (X86_FEATURE_OSPKE ))return ;
arch/x86/include/asm/trace/fpu.h +4 -1
@@ -13,19 +13,22 @@ DECLARE_EVENT_CLASS(x86_fpu,TP_STRUCT__entry (__field (struct fpu *,fpu ) +__field (bool, load_fpu)__field (u64, xfeatures)__field (u64, xcomp_bv) ),TP_fast_assign ( __entry->fpu = fpu; + __entry->load_fpu =test_thread_flag (TIF_NEED_FPU_LOAD );if (boot_cpu_has (X86_FEATURE_OSXSAVE )) { __entry->xfeatures = fpu->state.xsave.header.xfeatures; __entry->xcomp_bv = fpu->state.xsave.header.xcomp_bv; } ), -TP_printk ("x86/fpu: %p xfeatures: %llx xcomp_bv: %llx" , +TP_printk ("x86/fpu: %p load: %d xfeatures: %llx xcomp_bv: %llx" , __entry->fpu, + __entry->load_fpu, __entry->xfeatures, __entry->xcomp_bv )
arch/x86/kernel/fpu/core.c +78 -26
167 lines not on this page · raw message
arch/x86/kernel/fpu/signal.c +29 -18
99 lines not on this page · raw message
arch/x86/kernel/process.c +1 -1
@@ -96,7 +96,7 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) dst->thread.vm86 =NULL ;#endif -return fpu__copy (&dst->thread.fpu, &src->thread.fpu); +return fpu__copy (dst, src); }/*
arch/x86/kernel/process_32.c +3 -2
@@ -233,7 +233,8 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)/* never put a printk in __switch_to... printk() calls wake_up*() indirectly */ -switch_fpu_prepare (prev_fpu, cpu); +if (!test_thread_flag (TIF_NEED_FPU_LOAD )) +switch_fpu_prepare (prev_fpu, cpu);/* * Save away %gs. No need to save %fs, as it was saved on the
@@ -294,7 +295,7 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)this_cpu_write (current_task, next_p); -switch_fpu_finish (next_fpu, cpu); +switch_fpu_finish (next_fpu);/* Load the Intel cache allocation PQR MSR. */ intel_rdt_sched_in ();
arch/x86/kernel/process_64.c +3 -2
@@ -558,7 +558,8 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)WARN_ON_ONCE (IS_ENABLED (CONFIG_DEBUG_ENTRY ) &&this_cpu_read (irq_count) !=-1 ); -switch_fpu_prepare (prev_fpu, cpu); +if (!test_thread_flag (TIF_NEED_FPU_LOAD )) +switch_fpu_prepare (prev_fpu, cpu);/* We must save %fs and %gs before load_TLS() because * %fs and %gs may be cleared by load_TLS().
@@ -610,7 +611,7 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)this_cpu_write (current_task, next_p);this_cpu_write (cpu_current_top_of_stack,task_top_of_stack (next_p)); -switch_fpu_finish (next_fpu, cpu); +switch_fpu_finish (next_fpu);/* Reload sp0. */ update_task_stack (next_p);
arch/x86/kvm/x86.c +15 -4
@@ -7734,6 +7734,10 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)wait_lapic_expire (vcpu);guest_enter_irqoff (); +fpregs_assert_state_consistent (); +if (test_thread_flag (TIF_NEED_FPU_LOAD )) +switch_fpu_return (); +if (unlikely (vcpu->arch.switch_db_regs)) {set_debugreg (0 ,7 );set_debugreg (vcpu->arch.eff_db[0 ],0 );
@@ -7993,22 +7997,29 @@ static int complete_emulated_mmio(struct kvm_vcpu *vcpu)/* Swap (qemu) user FPU context for the guest FPU context. */ static void kvm_load_guest_fpu (struct kvm_vcpu *vcpu) { -preempt_disable (); +__fpregs_changes_begin (); +copy_fpregs_to_fpstate (&vcpu->arch.user_fpu); +/* PKRU is separately restored in kvm_x86_ops->run. */ __copy_kernel_to_fpregs (&vcpu->arch.guest_fpu.state, ~XFEATURE_MASK_PKRU ); -preempt_enable (); + +fpregs_mark_activate (); +__fpregs_changes_end ();trace_kvm_fpu (1 ); }/* When vcpu_run ends, restore user space FPU context. */ static void kvm_put_guest_fpu (struct kvm_vcpu *vcpu) { -preempt_disable (); +__fpregs_changes_begin (); +copy_fpregs_to_fpstate (&vcpu->arch.guest_fpu);copy_kernel_to_fpregs (&vcpu->arch.user_fpu.state); -preempt_enable (); + +fpregs_mark_activate (); +__fpregs_changes_end (); ++vcpu->stat.fpu_reload;trace_kvm_fpu (0 ); }
Signature · 1 line
2.20.0.rc1
Re: [PATCH 29/29] x86/fpu: Defer FPU state load until return to userspace
Quoted 31 lines · On 2018-11-28 23:20:35 [+0100], To [email protected] wrote:
arch/x86/kernel/fpu/signal.c +6
-5@@ -292,43 +295,51 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)u64 init_bv = xfeatures_mask & ~xfeatures;if (using_compacted_format ()) { - ret =copy_user_to_xstate (&state->xsave, buf_fx); + ret =copy_user_to_xstate (&fpu->state.xsave, buf_fx); }else { - ret =__copy_from_user (&state->xsave, buf_fx, state_size); + ret =__copy_from_user (&fpu->state.xsave, buf_fx, state_size);if (!ret && state_size > offsetof(struct xregs_state , header)) - ret =validate_xstate_header (&state->xsave.header); + ret =validate_xstate_header (&fpu->state.xsave.header); }if (ret) goto err_out; -sanitize_restored_xstate (state, envp, xfeatures, fx_only); +sanitize_restored_xstate (&fpu->state, envp, xfeatures, fx_only); +__fpregs_changes_begin ();if (unlikely (init_bv))copy_kernel_to_xregs (&init_fpstate.xsave, init_bv); - ret =copy_users_to_xregs (&state->xsave, xfeatures); + ret =copy_users_to_xregs (&fpu->state.xsave, xfeatures); }else if (use_fxsr ()) {
arch/x86/kernel/fpu/signal.c static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size) · line 322
Quoted 10 lines
arch/x86/kernel/fpu/signal.c +5
-3- ret =__copy_from_user (&state->fxsave, buf_fx, state_size); -if (ret) + ret =__copy_from_user (&fpu->state.fxsave, buf_fx, state_size); +if (ret) { + err = -EFAULT ; goto err_out; + } -sanitize_restored_xstate (state, envp, xfeatures, fx_only); +sanitize_restored_xstate (&fpu->state, &env, xfeatures, fx_only);
This should have remained envp' and not become &env'.
Sebastian
Re: [PATCH 29/29] x86/fpu: Defer FPU state load until return to userspace
In reply to Sebastian Andrzej SiewiorQuoted 15 lines · On 2018-11-28 23:20:35 [+0100], To [email protected] wrote:
arch/x86/kernel/fpu/signal.c +1
-2@@ -228,13 +228,12 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)struct task_struct *tsk = current;struct fpu *fpu = &tsk->thread.fpu;int state_size = fpu_kernel_xstate_size; -union fpregs_state *state; -void *tmp;struct user_i387_ia32_struct env;struct user_i387_ia32_struct *envp =NULL ;int ret =0 ;u64 xfeatures =0 ;int fx_only =0 ; +int err;
…
arch/x86/kernel/fpu/signal.c static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size) · line 320
Quoted 8 lines
arch/x86/kernel/fpu/signal.c +4
-2}else if (use_fxsr ()) { - ret =__copy_from_user (&state->fxsave, buf_fx, state_size); -if (ret) + ret =__copy_from_user (&fpu->state.fxsave, buf_fx, state_size); +if (ret) { + err = -EFAULT ; goto err_out; + }
After reworking the series after addressing the comments for 08/29 I noticed that err' should have been ret'.
Sebastian
Quoted 1 line · Sebastian Andrzej Siewior
This is a refurbished series originally started by by Rik van Riel. The
Could someone please apply patch 1 - 7?
Sebastian