x86, fpu: cleanups, introduce non-lazy FPU restore for xsave
Subsystems (4)
cover x86, fpu: cleanups, introduce non-lazy FPU restore for xsave
These patches are against tip/x86/fpu. Few cleanups and more improtantly this series introduces the non-lazy FPU restore mechanism for processors supporting xsave feature. More details in the individual patch changelogs.
Thanks.
Suresh Siddha (6):
x86, fpu: drop_fpu() before restoring new state from sigframe x86, fpu: remove unnecessary user_fpu_end() in save_xstate_sig() x86, kvm: use kernel_fpu_begin/end() in kvm_load/put_guest_fpu() x86, fpu: always use kernel_fpu_begin/end() for in-kernel FPU usage lguest, x86: handle guest TS bit for lazy/non-lazy fpu host models x86, fpu: use non-lazy fpu restore for processors supporting xsave
arch/x86/include/asm/fpu-internal.h | 118 +++++++++++++++++++--------------- arch/x86/include/asm/i387.h | 1 + arch/x86/include/asm/xor_32.h | 56 +++-------------- arch/x86/include/asm/xor_64.h | 61 +++--------------- arch/x86/include/asm/xor_avx.h | 54 ++++------------ arch/x86/include/asm/xsave.h | 1 + arch/x86/kernel/i387.c | 20 +++++- arch/x86/kernel/process.c | 12 +++- arch/x86/kernel/process_32.c | 4 - arch/x86/kernel/process_64.c | 4 - arch/x86/kernel/traps.c | 5 +- arch/x86/kernel/xsave.c | 58 +++++++++++++---- arch/x86/kvm/x86.c | 3 +- drivers/lguest/x86/core.c | 10 ++- 14 files changed, 180 insertions(+), 227 deletions(-)
Signature · 1 line
1.7.6.5
PATCH 1/6 x86, fpu: drop_fpu() before restoring new state from sigframe
No need to save the state with unlazy_fpu(), that is about to get overwritten by the state from the signal frame. Instead use drop_fpu() and continue to restore the new state.
Also fold the stop_fpu_preload() into drop_fpu().
--- arch/x86/include/asm/fpu-internal.h | 7 +------ arch/x86/kernel/xsave.c | 8 +++----- 2 files changed, 4 insertions(+), 11 deletions(-)
arch/x86/include/asm/fpu-internal.h +1 -6
@@ -448,17 +448,12 @@ static inline void save_init_fpu(struct task_struct *tsk)preempt_enable (); } -static inline void stop_fpu_preload (struct task_struct *tsk) -{ - tsk->fpu_counter =0 ; -} -static inline void drop_fpu (struct task_struct *tsk) {/* * Forget coprocessor state.. */ -stop_fpu_preload (tsk); + tsk->fpu_counter =0 ;preempt_disable ();__drop_fpu (tsk);preempt_enable ();
arch/x86/kernel/xsave.c +3 -5
@@ -381,16 +381,14 @@ int __restore_xstate_sig(void __user *buf, void __user *buf_fx, int size)struct xsave_struct *xsave = &tsk->thread.fpu.state->xsave;struct user_i387_ia32_struct env; -stop_fpu_preload (tsk); -unlazy_fpu (tsk); +drop_fpu (tsk);if (__copy_from_user (xsave, buf_fx, state_size) || -__copy_from_user (&env, buf,sizeof (env))) { -drop_fpu (tsk); +__copy_from_user (&env, buf,sizeof (env)))return -1 ; - }sanitize_restored_xstate (tsk, &env, xstate_bv, fx_only); +set_used_math (); }else {/* * For 64-bit frames and 32-bit fsave frames, restore the user
Signature · 1 line
1.7.6.5
[tip:x86/fpu] x86, fpu: drop_fpu() before restoring new state from sigframe
Commit-ID: 739390035c5fba2132fa424309786ff7bdd2cc1e Gitweb: http://git.kernel.org/tip/739390035c5fba2132fa424309786ff7bdd2cc1e Author: Suresh Siddha <[email protected]> AuthorDate: Fri, 24 Aug 2012 14:12:57 -0700 Committer: H. Peter Anvin <[email protected]> CommitDate: Fri, 24 Aug 2012 14:26:47 -0700
x86, fpu: drop_fpu() before restoring new state from sigframe
No need to save the state with unlazy_fpu(), that is about to get overwritten by the state from the signal frame. Instead use drop_fpu() and continue to restore the new state.
Also fold the stop_fpu_preload() into drop_fpu().
--- arch/x86/include/asm/fpu-internal.h | 7 +------ arch/x86/kernel/xsave.c | 8 +++----- 2 files changed, 4 insertions(+), 11 deletions(-)
arch/x86/include/asm/fpu-internal.h +1 -6
@@ -448,17 +448,12 @@ static inline void save_init_fpu(struct task_struct *tsk)preempt_enable (); } -static inline void stop_fpu_preload (struct task_struct *tsk) -{ - tsk->fpu_counter =0 ; -} -static inline void drop_fpu (struct task_struct *tsk) {/* * Forget coprocessor state.. */ -stop_fpu_preload (tsk); + tsk->fpu_counter =0 ;preempt_disable ();__drop_fpu (tsk);preempt_enable ();
arch/x86/kernel/xsave.c +3 -5
@@ -381,16 +381,14 @@ int __restore_xstate_sig(void __user *buf, void __user *buf_fx, int size)struct xsave_struct *xsave = &tsk->thread.fpu.state->xsave;struct user_i387_ia32_struct env; -stop_fpu_preload (tsk); -unlazy_fpu (tsk); +drop_fpu (tsk);if (__copy_from_user (xsave, buf_fx, state_size) || -__copy_from_user (&env, buf,sizeof (env))) { -drop_fpu (tsk); +__copy_from_user (&env, buf,sizeof (env)))return -1 ; - }sanitize_restored_xstate (tsk, &env, xstate_bv, fx_only); +set_used_math (); }else {/* * For 64-bit frames and 32-bit fsave frames, restore the user
[tip:x86/fpu] x86, fpu: drop_fpu() before restoring new state from sigframe
In reply to Suresh SiddhaCommit-ID: e962591749dfd4df9fea2c530ed7a3cfed50e5aa Gitweb: http://git.kernel.org/tip/e962591749dfd4df9fea2c530ed7a3cfed50e5aa Author: Suresh Siddha <[email protected]> AuthorDate: Fri, 24 Aug 2012 14:12:57 -0700 Committer: H. Peter Anvin <[email protected]> CommitDate: Tue, 18 Sep 2012 15:52:05 -0700
x86, fpu: drop_fpu() before restoring new state from sigframe
No need to save the state with unlazy_fpu(), that is about to get overwritten by the state from the signal frame. Instead use drop_fpu() and continue to restore the new state.
Also fold the stop_fpu_preload() into drop_fpu().
--- arch/x86/include/asm/fpu-internal.h | 7 +------ arch/x86/kernel/xsave.c | 8 +++----- 2 files changed, 4 insertions(+), 11 deletions(-)
arch/x86/include/asm/fpu-internal.h +1 -6
@@ -448,17 +448,12 @@ static inline void save_init_fpu(struct task_struct *tsk)preempt_enable (); } -static inline void stop_fpu_preload (struct task_struct *tsk) -{ - tsk->fpu_counter =0 ; -} -static inline void drop_fpu (struct task_struct *tsk) {/* * Forget coprocessor state.. */ -stop_fpu_preload (tsk); + tsk->fpu_counter =0 ;preempt_disable ();__drop_fpu (tsk);preempt_enable ();
arch/x86/kernel/xsave.c +3 -5
@@ -382,16 +382,14 @@ int __restore_xstate_sig(void __user *buf, void __user *buf_fx, int size)struct xsave_struct *xsave = &tsk->thread.fpu.state->xsave;struct user_i387_ia32_struct env; -stop_fpu_preload (tsk); -unlazy_fpu (tsk); +drop_fpu (tsk);if (__copy_from_user (xsave, buf_fx, state_size) || -__copy_from_user (&env, buf,sizeof (env))) { -drop_fpu (tsk); +__copy_from_user (&env, buf,sizeof (env)))return -1 ; - }sanitize_restored_xstate (tsk, &env, xstate_bv, fx_only); +set_used_math (); }else {/* * For 64-bit frames and 32-bit fsave frames, restore the user
PATCH 2/6 x86, fpu: remove unnecessary user_fpu_end() in save_xstate_sig()
In reply to Suresh SiddhaFew lines below we do drop_fpu() which is more safer. Remove the unnecessary user_fpu_end() in save_xstate_sig(), which allows the drop_fpu() to ignore any pending exceptions from the user-space and drop the current fpu.
--- arch/x86/include/asm/fpu-internal.h | 17 +++-------------- arch/x86/kernel/xsave.c | 1 - 2 files changed, 3 insertions(+), 15 deletions(-)
arch/x86/include/asm/fpu-internal.h +3 -14
@@ -412,22 +412,11 @@ static inline void __drop_fpu(struct task_struct *tsk) }/* -* The actual user_fpu_begin/end() functions -* need to be preemption-safe. +* Need to be preemption-safe. * -* NOTE! user_fpu_end() must be used only after you -* have saved the FP state, and user_fpu_begin() must -* be used only immediately before restoring it. -* These functions do not do any save/restore on -* their own. +* NOTE! user_fpu_begin() must be used only immediately before restoring +* it. This function does not do any save/restore on their own. */ -static inline void user_fpu_end (void ) -{ -preempt_disable (); -__thread_fpu_end (current); -preempt_enable (); -} -static inline void user_fpu_begin (void ) {preempt_disable ();
arch/x86/kernel/xsave.c +0 -1
@@ -254,7 +254,6 @@ int save_xstate_sig(void __user *buf, void __user *buf_fx, int size)/* Update the thread's fxstate to save the fsave header. */ if (ia32_fxstate)fpu_fxsave (&tsk->thread.fpu); -user_fpu_end (); }else {sanitize_i387_state (tsk);if (__copy_to_user (buf_fx, xsave, xstate_size))
Signature · 1 line
1.7.6.5
[tip:x86/fpu] x86, fpu: remove unnecessary user_fpu_end() in save_xstate_sig()
Commit-ID: cc50fae05beb2db9f4587bbb1a0d6aba2af5b407 Gitweb: http://git.kernel.org/tip/cc50fae05beb2db9f4587bbb1a0d6aba2af5b407 Author: Suresh Siddha <[email protected]> AuthorDate: Fri, 24 Aug 2012 14:12:58 -0700 Committer: H. Peter Anvin <[email protected]> CommitDate: Fri, 24 Aug 2012 14:26:48 -0700
x86, fpu: remove unnecessary user_fpu_end() in save_xstate_sig()
Few lines below we do drop_fpu() which is more safer. Remove the unnecessary user_fpu_end() in save_xstate_sig(), which allows the drop_fpu() to ignore any pending exceptions from the user-space and drop the current fpu.
--- arch/x86/include/asm/fpu-internal.h | 17 +++-------------- arch/x86/kernel/xsave.c | 1 - 2 files changed, 3 insertions(+), 15 deletions(-)
arch/x86/include/asm/fpu-internal.h +3 -14
@@ -412,22 +412,11 @@ static inline void __drop_fpu(struct task_struct *tsk) }/* -* The actual user_fpu_begin/end() functions -* need to be preemption-safe. +* Need to be preemption-safe. * -* NOTE! user_fpu_end() must be used only after you -* have saved the FP state, and user_fpu_begin() must -* be used only immediately before restoring it. -* These functions do not do any save/restore on -* their own. +* NOTE! user_fpu_begin() must be used only immediately before restoring +* it. This function does not do any save/restore on their own. */ -static inline void user_fpu_end (void ) -{ -preempt_disable (); -__thread_fpu_end (current); -preempt_enable (); -} -static inline void user_fpu_begin (void ) {preempt_disable ();
arch/x86/kernel/xsave.c +0 -1
@@ -254,7 +254,6 @@ int save_xstate_sig(void __user *buf, void __user *buf_fx, int size)/* Update the thread's fxstate to save the fsave header. */ if (ia32_fxstate)fpu_fxsave (&tsk->thread.fpu); -user_fpu_end (); }else {sanitize_i387_state (tsk);if (__copy_to_user (buf_fx, xsave, xstate_size))
Re: [PATCH 2/6] x86, fpu: remove unnecessary user_fpu_end() in save_xstate_sig()
In reply to Suresh SiddhaQuoted 27 lines · Suresh Siddha
Few lines below we do
drop_fpu()which is more safer. Remove the unnecessaryuser_fpu_end()insave_xstate_sig(), which allows thedrop_fpu()to ignore any pending exceptions from the user-space and drop the current fpu.Signed-off-by Suresh Siddha <[email protected]>--- arch/x86/include/asm/fpu-internal.h | 17 +++-------------- arch/x86/kernel/xsave.c | 1 - 2 files changed, 3 insertions(+), 15 deletions(-)arch/x86/include/asm/fpu-internal.h +1
-6@@ -412,22 +412,11 @@ static inline void __drop_fpu(struct task_struct *tsk) }/* -* The actual user_fpu_begin/end() functions -* need to be preemption-safe. +* Need to be preemption-safe. * -* NOTE! user_fpu_end() must be used only after you -* have saved the FP state, and user_fpu_begin() must -* be used only immediately before restoring it. -* These functions do not do any save/restore on
arch/x86/include/asm/fpu-internal.h static inline void __drop_fpu(struct task_struct *tsk) · line 429
Quoted 10 lines
arch/x86/include/asm/fpu-internal.h +2
-7-* their own. +* NOTE! user_fpu_begin() must be used only immediately before restoring +* it. This function does not do any save/restore on their own. */ -static inline void user_fpu_end (void ) -{ -preempt_disable (); -__thread_fpu_end (current); -preempt_enable (); -}
Now that user_fpu_begin has lost its counterpart user_fpu_end, maybe rename it to user_fpu_init() or something similar which doesn't suggest there's a _begin and an _end thingy?
Thanks.
Signature · 8 lines
Regards/Gruss, Boris. Advanced Micro Devices GmbH Einsteinring 24, 85609 Dornach GM: Alberto Bozzo Reg: Dornach, Landkreis Muenchen HRB Nr. 43632 WEEE Registernr: 129 19551
[tip:x86/fpu] x86, fpu: remove unnecessary user_fpu_end() in save_xstate_sig()
In reply to Suresh SiddhaCommit-ID: 377ffbcc536a5a6666dc077395163ab149c02610 Gitweb: http://git.kernel.org/tip/377ffbcc536a5a6666dc077395163ab149c02610 Author: Suresh Siddha <[email protected]> AuthorDate: Fri, 24 Aug 2012 14:12:58 -0700 Committer: H. Peter Anvin <[email protected]> CommitDate: Tue, 18 Sep 2012 15:52:06 -0700
x86, fpu: remove unnecessary user_fpu_end() in save_xstate_sig()
Few lines below we do drop_fpu() which is more safer. Remove the unnecessary user_fpu_end() in save_xstate_sig(), which allows the drop_fpu() to ignore any pending exceptions from the user-space and drop the current fpu.
--- arch/x86/include/asm/fpu-internal.h | 17 +++-------------- arch/x86/kernel/xsave.c | 1 - 2 files changed, 3 insertions(+), 15 deletions(-)
arch/x86/include/asm/fpu-internal.h +3 -14
@@ -412,22 +412,11 @@ static inline void __drop_fpu(struct task_struct *tsk) }/* -* The actual user_fpu_begin/end() functions -* need to be preemption-safe. +* Need to be preemption-safe. * -* NOTE! user_fpu_end() must be used only after you -* have saved the FP state, and user_fpu_begin() must -* be used only immediately before restoring it. -* These functions do not do any save/restore on -* their own. +* NOTE! user_fpu_begin() must be used only immediately before restoring +* it. This function does not do any save/restore on their own. */ -static inline void user_fpu_end (void ) -{ -preempt_disable (); -__thread_fpu_end (current); -preempt_enable (); -} -static inline void user_fpu_begin (void ) {preempt_disable ();
arch/x86/kernel/xsave.c +0 -1
@@ -255,7 +255,6 @@ int save_xstate_sig(void __user *buf, void __user *buf_fx, int size)/* Update the thread's fxstate to save the fsave header. */ if (ia32_fxstate)fpu_fxsave (&tsk->thread.fpu); -user_fpu_end (); }else {sanitize_i387_state (tsk);if (__copy_to_user (buf_fx, xsave, xstate_size))
PATCH 3/6 x86, kvm: use kernel_fpu_begin/end() in kvm_load/put_guest_fpu()
In reply to Suresh Siddhakvm's guest fpu save/restore should be wrapped around kernel_fpu_begin/end(). This will avoid for example taking a DNA in kvm_load_guest_fpu() when it tries to load the fpu immediately after doing unlazy_fpu() on the host side.
More importantly this will prevent the host process fpu from being corrupted.
--- arch/x86/kvm/x86.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
arch/x86/kvm/x86.c +2 -1
@@ -5969,7 +5969,7 @@ void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)*/ kvm_put_guest_xcr0 (vcpu); vcpu->guest_fpu_loaded =1 ; -unlazy_fpu (current); +kernel_fpu_begin ();fpu_restore_checking (&vcpu->arch.guest_fpu);trace_kvm_fpu (1 ); }
@@ -5983,6 +5983,7 @@ void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) vcpu->guest_fpu_loaded =0 ;fpu_save_init (&vcpu->arch.guest_fpu); +kernel_fpu_end (); ++vcpu->stat.fpu_reload;kvm_make_request (KVM_REQ_DEACTIVATE_FPU , vcpu);trace_kvm_fpu (0 );
Signature · 1 line
1.7.6.5
[tip:x86/fpu] x86, kvm: use kernel_fpu_begin/end() in kvm_load/ put_guest_fpu()
Commit-ID: 98700fa647b3572f7fa55485570ab9fc53b91d23 Gitweb: http://git.kernel.org/tip/98700fa647b3572f7fa55485570ab9fc53b91d23 Author: Suresh Siddha <[email protected]> AuthorDate: Fri, 24 Aug 2012 14:12:59 -0700 Committer: H. Peter Anvin <[email protected]> CommitDate: Fri, 24 Aug 2012 14:26:49 -0700
x86, kvm: use kernel_fpu_begin/end() in kvm_load/put_guest_fpu()
kvm's guest fpu save/restore should be wrapped around kernel_fpu_begin/end(). This will avoid for example taking a DNA in kvm_load_guest_fpu() when it tries to load the fpu immediately after doing unlazy_fpu() on the host side.
More importantly this will prevent the host process fpu from being corrupted.
--- arch/x86/kvm/x86.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
arch/x86/kvm/x86.c +2 -1
@@ -5954,7 +5954,7 @@ void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)*/ kvm_put_guest_xcr0 (vcpu); vcpu->guest_fpu_loaded =1 ; -unlazy_fpu (current); +kernel_fpu_begin ();fpu_restore_checking (&vcpu->arch.guest_fpu);trace_kvm_fpu (1 ); }
@@ -5968,6 +5968,7 @@ void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) vcpu->guest_fpu_loaded =0 ;fpu_save_init (&vcpu->arch.guest_fpu); +kernel_fpu_end (); ++vcpu->stat.fpu_reload;kvm_make_request (KVM_REQ_DEACTIVATE_FPU , vcpu);trace_kvm_fpu (0 );
[tip:x86/fpu] x86, kvm: use kernel_fpu_begin/end() in kvm_load/ put_guest_fpu()
In reply to Suresh SiddhaCommit-ID: 9c1c3fac53378c9782c18f80107965578d7b7167 Gitweb: http://git.kernel.org/tip/9c1c3fac53378c9782c18f80107965578d7b7167 Author: Suresh Siddha <[email protected]> AuthorDate: Fri, 24 Aug 2012 14:12:59 -0700 Committer: H. Peter Anvin <[email protected]> CommitDate: Tue, 18 Sep 2012 15:52:07 -0700
x86, kvm: use kernel_fpu_begin/end() in kvm_load/put_guest_fpu()
kvm's guest fpu save/restore should be wrapped around kernel_fpu_begin/end(). This will avoid for example taking a DNA in kvm_load_guest_fpu() when it tries to load the fpu immediately after doing unlazy_fpu() on the host side.
More importantly this will prevent the host process fpu from being corrupted.
--- arch/x86/kvm/x86.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
arch/x86/kvm/x86.c +2 -1
@@ -5972,7 +5972,7 @@ void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)*/ kvm_put_guest_xcr0 (vcpu); vcpu->guest_fpu_loaded =1 ; -unlazy_fpu (current); +kernel_fpu_begin ();fpu_restore_checking (&vcpu->arch.guest_fpu);trace_kvm_fpu (1 ); }
@@ -5986,6 +5986,7 @@ void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) vcpu->guest_fpu_loaded =0 ;fpu_save_init (&vcpu->arch.guest_fpu); +kernel_fpu_end (); ++vcpu->stat.fpu_reload;kvm_make_request (KVM_REQ_DEACTIVATE_FPU , vcpu);trace_kvm_fpu (0 );
Re: [PATCH 3/6] x86, kvm: use kernel_fpu_begin/end() in kvm_load/put_guest_fpu()
In reply to Suresh SiddhaQuoted 19 lines · Suresh Siddha
kvm's guest fpu save/restore should be wrapped around kernel_fpu_begin/end(). This will avoid for example taking a DNA in
kvm_load_guest_fpu()when it tries to load the fpu immediately after doingunlazy_fpu()on the host side.More importantly this will prevent the host process fpu from being corrupted.
Signed-off-by Suresh Siddha <[email protected]>Cc Avi Kivity <[email protected]>--- arch/x86/kvm/x86.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) · line 5974
Quoted 7 lines
arch/x86/kvm/x86.c +1
-1@@ -5969,7 +5969,7 @@ void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)*/ kvm_put_guest_xcr0 (vcpu); vcpu->guest_fpu_loaded =1 ; -unlazy_fpu (current); +kernel_fpu_begin ();fpu_restore_checking (&vcpu->arch.guest_fpu);trace_kvm_fpu (1 );
This breaks kvm, since it disables preemption. What we want here is to save the user fpu state if it was loaded, and do nothing if wasn't. Don't know what's the new API for that.
Quoted 10 lines
arch/x86/kvm/x86.c +1
-0}@@ -5983,6 +5983,7 @@ void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) vcpu->guest_fpu_loaded =0 ;fpu_save_init (&vcpu->arch.guest_fpu); +kernel_fpu_end (); ++vcpu->stat.fpu_reload;kvm_make_request (KVM_REQ_DEACTIVATE_FPU , vcpu);trace_kvm_fpu (0 );
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 3/6] x86, kvm: use kernel_fpu_begin/end() in kvm_load/put_guest_fpu()
Quoted 15 lines · Avi Kivity
Quoted 14 lines · Suresh Siddha
kvm's guest fpu save/restore should be wrapped around kernel_fpu_begin/end(). This will avoid for example taking a DNA in
kvm_load_guest_fpu()when it tries to load the fpu immediately after doingunlazy_fpu()on the host side.More importantly this will prevent the host process fpu from being corrupted.
Signed-off-by Suresh Siddha <[email protected]>Cc Avi Kivity <[email protected]>--- arch/x86/kvm/x86.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
Quoted 16 lines
arch/x86/kvm/x86.c void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) · line 5974
Quoted 12 lines
arch/x86/kvm/x86.c +1
-1@@ -5969,7 +5969,7 @@ void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)*/ kvm_put_guest_xcr0 (vcpu); vcpu->guest_fpu_loaded =1 ; -unlazy_fpu (current); +kernel_fpu_begin ();fpu_restore_checking (&vcpu->arch.guest_fpu);trace_kvm_fpu (1 );This breaks kvm, since it disables preemption. What we want here is to save the user fpu state if it was loaded, and do nothing if wasn't. Don't know what's the new API for that.
These routines (kvm_load/put_guest_fpu()) are already called with preemption disabled but as you mentioned, we don't want the preemption to be disabled completely between the kvm_load_guest_fpu() and kvm_put_guest_fpu().
Also KVM already has the preempt notifier which is doing the kvm_put_guest_fpu(), so something like the appended should address this. I will test this shortly.
--- arch/x86/include/asm/i387.h | 17 +++++++++++++++-- arch/x86/kernel/i387.c | 13 +++++-------- arch/x86/kvm/x86.c | 4 ++-- 3 files changed, 22 insertions(+), 12 deletions(-)
arch/x86/include/asm/i387.h +15 -2
@@ -24,8 +24,21 @@ extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);extern void math_state_restore (void );extern bool irq_fpu_usable (void ); -extern void kernel_fpu_begin (void ); -extern void kernel_fpu_end (void ); +extern void __kernel_fpu_begin (void ); +extern void __kernel_fpu_end (void ); + +static inline void kernel_fpu_begin (void ) +{ +WARN_ON_ONCE (!irq_fpu_usable ()); +preempt_disable (); +__kernel_fpu_begin (); +} + +static inline void kernel_fpu_end (void ) +{ +__kernel_fpu_end (); +preempt_enable (); +}/* * Some instructions like VIA's padlock instructions generate a spurious
arch/x86/kernel/i387.c +5 -8
@@ -73,32 +73,29 @@ bool irq_fpu_usable(void) }EXPORT_SYMBOL (irq_fpu_usable); -void kernel_fpu_begin (void ) +void __kernel_fpu_begin (void ) {struct task_struct *me = current; -WARN_ON_ONCE (!irq_fpu_usable ()); -preempt_disable ();if (__thread_has_fpu (me)) {__save_init_fpu (me);__thread_clear_has_fpu (me); -/* We do 'stts()' in kernel_fpu_end() */ +/* We do 'stts()' in __kernel_fpu_end() */ }else if (!use_eager_fpu ()) {this_cpu_write (fpu_owner_task,NULL );clts (); } } -EXPORT_SYMBOL (kernel_fpu_begin); +EXPORT_SYMBOL (__kernel_fpu_begin); -void kernel_fpu_end (void ) +void __kernel_fpu_end (void ) {if (use_eager_fpu ())math_state_restore ();else stts (); -preempt_enable (); } -EXPORT_SYMBOL (kernel_fpu_end); +EXPORT_SYMBOL (__kernel_fpu_end);void unlazy_fpu (struct task_struct *tsk) {
arch/x86/kvm/x86.c +2 -2
@@ -5979,7 +5979,7 @@ void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)*/ kvm_put_guest_xcr0 (vcpu); vcpu->guest_fpu_loaded =1 ; -kernel_fpu_begin (); +__kernel_fpu_begin ();fpu_restore_checking (&vcpu->arch.guest_fpu);trace_kvm_fpu (1 ); }
@@ -5993,7 +5993,7 @@ void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) vcpu->guest_fpu_loaded =0 ;fpu_save_init (&vcpu->arch.guest_fpu); -kernel_fpu_end (); +__kernel_fpu_end (); ++vcpu->stat.fpu_reload;kvm_make_request (KVM_REQ_DEACTIVATE_FPU , vcpu);trace_kvm_fpu (0 );
Re: [PATCH 3/6] x86, kvm: use kernel_fpu_begin/end() in kvm_load/put_guest_fpu()
Quoted 9 lines · Suresh Siddha
These routines (kvm_load/put_guest_fpu()) are already called with preemption disabled but as you mentioned, we don't want the preemption to be disabled completely between the
kvm_load_guest_fpu()andkvm_put_guest_fpu().Also KVM already has the preempt notifier which is doing the
kvm_put_guest_fpu(), so something like the appended should address this. I will test this shortly.
Note, we could also go in a different direction and make kernel_fpu_begin() use preempt notifiers and thus make its users preemptible. But that's for a separate patchset.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 3/6] x86, kvm: use kernel_fpu_begin/end() in kvm_load/put_guest_fpu()
Quoted 7 lines · Avi Kivity
Quoted 5 lines · Suresh Siddha
These routines (kvm_load/put_guest_fpu()) are already called with preemption disabled but as you mentioned, we don't want the preemption to be disabled completely between the
kvm_load_guest_fpu()andkvm_put_guest_fpu().
Quoted 8 lines
Quoted 4 lines
Also KVM already has the preempt notifier which is doing the
kvm_put_guest_fpu(), so something like the appended should address this. I will test this shortly.Note, we could also go in a different direction and make
kernel_fpu_begin()use preempt notifiers and thus make its users preemptible. But that's for a separate patchset.
yep, but we need the fpu buffer to save/restore the kernel fpu state.
KVM already has those buffers allocated in the guest cpu state and hence it all works out ok. But yes, we can revisit this in future.
thanks,
suresh
Re: [PATCH 3/6] x86, kvm: use kernel_fpu_begin/end() in kvm_load/put_guest_fpu()
Quoted 13 lines · Suresh Siddha
Quoted 12 lines · Avi Kivity
Quoted 9 lines · Suresh Siddha
These routines (kvm_load/put_guest_fpu()) are already called with preemption disabled but as you mentioned, we don't want the preemption to be disabled completely between the
kvm_load_guest_fpu()andkvm_put_guest_fpu().Also KVM already has the preempt notifier which is doing the
kvm_put_guest_fpu(), so something like the appended should address this. I will test this shortly.
Quoted 8 lines
Quoted 3 lines
Note, we could also go in a different direction and make
kernel_fpu_begin()use preempt notifiers and thus make its users preemptible. But that's for a separate patchset.yep, but we need the fpu buffer to save/restore the kernel fpu state.
KVM already has those buffers allocated in the guest cpu state and hence it all works out ok. But yes, we can revisit this in future.
kernel_fpu_begin() can allocate it. It means changing the APIs, but changing the behaviour to be preemptible is a bigger change anyway.
Signature · 1 line
error compiling committee.c: too many arguments to function
H. Peter Anvin
reviewerRe: [PATCH 3/6] x86, kvm: use kernel_fpu_begin/end() in kvm_load/put_guest_fpu()
In reply to Avi KivityQuoted 5 lines · Avi Kivity
Note, we could also go in a different direction and make
kernel_fpu_begin()use preempt notifiers and thus make its users preemptible. But that's for a separate patchset.
Where would you put the state if you were preempted? You want to allocate a full extra buffer for the kernel xstate for each thread just in case? ("Yes" is a valid answer to that question, but it is a fair chunk of memory.)
-hpa
Re: [PATCH 3/6] x86, kvm: use kernel_fpu_begin/end() in kvm_load/put_guest_fpu()
Quoted 11 lines · H. Peter Anvin
Quoted 5 lines · Avi Kivity
Note, we could also go in a different direction and make
kernel_fpu_begin()use preempt notifiers and thus make its users preemptible. But that's for a separate patchset.Where would you put the state if you were preempted? You want to allocate a full extra buffer for the kernel xstate for each thread just in case? ("Yes" is a valid answer to that question, but it is a fair chunk of memory.)
kernel_fpu_begin() could receive a pointer to a struct fpu, with fpu->state either preallocated by the caller, or allocated by kernel_fpu_begin() itself.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 3/6] x86, kvm: use kernel_fpu_begin/end() in kvm_load/put_guest_fpu()
In reply to Avi KivityQuoted 8 lines · Suresh Siddha
These routines (kvm_load/put_guest_fpu()) are already called with preemption disabled but as you mentioned, we don't want the preemption to be disabled completely between the
kvm_load_guest_fpu()andkvm_put_guest_fpu().Also KVM already has the preempt notifier which is doing the
kvm_put_guest_fpu(), so something like the appended should address this. I will test this shortly.
Appended the tested fix (one more VMX based change needed as it fiddles with cr0.TS host bit).
Thanks.
--8<--
From: Suresh Siddha <[email protected]>
Subject: x86, kvm: fix kvm's usage of kernel_fpu_begin/end()
Preemption is disabled between kernel_fpu_begin/end() and as such it is not a good idea to use these routines in kvm_load/put_guest_fpu() which can be very far apart.
kvm_load/put_guest_fpu() routines are already called with preemption disabled and KVM already uses the preempt notifier to save the guest fpu state using kvm_put_guest_fpu().
So introduce __kernel_fpu_begin/end() routines which don't touch preemption and use them instead of kernel_fpu_begin/end() for KVM's use model of saving/restoring guest FPU state.
Also with this change (and with eagerFPU model), fix the host cr0.TS vm-exit state in the case of VMX. For eagerFPU case, host cr0.TS is always clear. So no need to worry about it. For the traditional lazyFPU restore case, cr0.TS bit is always set during vm-exit and depending on the guest FPU state and the host task's FPU state, cr0.TS bit is cleared when needed.
--- arch/x86/include/asm/fpu-internal.h | 5 ----- arch/x86/include/asm/i387.h | 28 ++++++++++++++++++++++++++-- arch/x86/include/asm/processor.h | 5 +++++ arch/x86/kernel/i387.c | 13 +++++-------- arch/x86/kvm/vmx.c | 11 +++++++++-- arch/x86/kvm/x86.c | 4 ++-- 6 files changed, 47 insertions(+), 19 deletions(-)
arch/x86/include/asm/fpu-internal.h +0 -5
@@ -85,11 +85,6 @@ static inline int is_x32_frame(void)#define X87_FSW_ES (1 << 7)/* Exception Summary */ -static __always_inline __pure bool use_eager_fpu(void) -{ -return static_cpu_has (X86_FEATURE_EAGER_FPU ); -} - static __always_inline __pure bool use_xsaveopt(void) {return static_cpu_has (X86_FEATURE_XSAVEOPT );
arch/x86/include/asm/i387.h +26 -2
@@ -24,8 +24,32 @@ extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);extern void math_state_restore (void );extern bool irq_fpu_usable (void ); -extern void kernel_fpu_begin (void ); -extern void kernel_fpu_end (void ); + +/* +* 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. +*/ +extern void __kernel_fpu_begin (void ); +extern void __kernel_fpu_end (void ); + +static inline void kernel_fpu_begin (void ) +{ +WARN_ON_ONCE (!irq_fpu_usable ()); +preempt_disable (); +__kernel_fpu_begin (); +} + +static inline void kernel_fpu_end (void ) +{ +__kernel_fpu_end (); +preempt_enable (); +}/* * Some instructions like VIA's padlock instructions generate a spurious
arch/x86/include/asm/processor.h +5 -0
@@ -402,6 +402,11 @@ struct fpu {union thread_xstate *state; }; +static __always_inline __pure bool use_eager_fpu(void) +{ +return static_cpu_has (X86_FEATURE_EAGER_FPU ); +} +#ifdef CONFIG_X86_64 DECLARE_PER_CPU(struct orig_ist, orig_ist);
arch/x86/kernel/i387.c +5 -8
@@ -73,32 +73,29 @@ bool irq_fpu_usable(void) }EXPORT_SYMBOL (irq_fpu_usable); -void kernel_fpu_begin (void ) +void __kernel_fpu_begin (void ) {struct task_struct *me = current; -WARN_ON_ONCE (!irq_fpu_usable ()); -preempt_disable ();if (__thread_has_fpu (me)) {__save_init_fpu (me);__thread_clear_has_fpu (me); -/* We do 'stts()' in kernel_fpu_end() */ +/* We do 'stts()' in __kernel_fpu_end() */ }else if (!use_eager_fpu ()) {this_cpu_write (fpu_owner_task,NULL );clts (); } } -EXPORT_SYMBOL (kernel_fpu_begin); +EXPORT_SYMBOL (__kernel_fpu_begin); -void kernel_fpu_end (void ) +void __kernel_fpu_end (void ) {if (use_eager_fpu ())math_state_restore ();else stts (); -preempt_enable (); } -EXPORT_SYMBOL (kernel_fpu_end); +EXPORT_SYMBOL (__kernel_fpu_end);void unlazy_fpu (struct task_struct *tsk) {
arch/x86/kvm/vmx.c +9 -2
@@ -1493,7 +1493,8 @@ static void __vmx_load_host_state(struct vcpu_vmx *vmx)#ifdef CONFIG_X86_64 wrmsrl (MSR_KERNEL_GS_BASE , vmx->msr_host_kernel_gs_base);#endif -if (user_has_fpu ()) +/* Did the host task or the guest vcpu has FPU restored lazily? */ +if (!use_eager_fpu () && (user_has_fpu () || vmx->vcpu.guest_fpu_loaded))clts ();load_gdt (&__get_cpu_var (host_gdt)); }
@@ -3743,7 +3744,13 @@ static void vmx_set_constant_host_state(void)unsigned long tmpl;struct desc_ptr dt; -vmcs_writel (HOST_CR0 ,read_cr0 () |X86_CR0_TS );/* 22.2.3 */ +/* +* Eager FPU always has the cr0.TS bit clear. +*/ +if (use_eager_fpu ()) +vmcs_writel (HOST_CR0 ,read_cr0 ());/* 22.2.3 */ +else +vmcs_writel (HOST_CR0 ,read_cr0 () |X86_CR0_TS );/* 22.2.3 */ vmcs_writel (HOST_CR4 ,read_cr4 ());/* 22.2.3, 22.2.5 */ vmcs_writel (HOST_CR3 ,read_cr3 ());/* 22.2.3 FIXME: shadow tables */
arch/x86/kvm/x86.c +2 -2
@@ -5979,7 +5979,7 @@ void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)*/ kvm_put_guest_xcr0 (vcpu); vcpu->guest_fpu_loaded =1 ; -kernel_fpu_begin (); +__kernel_fpu_begin ();fpu_restore_checking (&vcpu->arch.guest_fpu);trace_kvm_fpu (1 ); }
@@ -5993,7 +5993,7 @@ void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) vcpu->guest_fpu_loaded =0 ;fpu_save_init (&vcpu->arch.guest_fpu); -kernel_fpu_end (); +__kernel_fpu_end (); ++vcpu->stat.fpu_reload;kvm_make_request (KVM_REQ_DEACTIVATE_FPU , vcpu);trace_kvm_fpu (0 );
Re: [PATCH 3/6] x86, kvm: use kernel_fpu_begin/end() in kvm_load/put_guest_fpu()
arch/x86/kvm/vmx.c static void __vmx_load_host_state(struct vcpu_vmx *vmx) · line 1498
Quoted 12 lines · Suresh Siddha
arch/x86/kvm/vmx.c +2
-1@@ -1493,7 +1493,8 @@ static void __vmx_load_host_state(struct vcpu_vmx *vmx)#ifdef CONFIG_X86_64 wrmsrl (MSR_KERNEL_GS_BASE , vmx->msr_host_kernel_gs_base);#endif -if (user_has_fpu ()) +/* Did the host task or the guest vcpu has FPU restored lazily? */ +if (!use_eager_fpu () && (user_has_fpu () || vmx->vcpu.guest_fpu_loaded))clts ();
Why do the clts() if guest_fpu_loaded()?
An interrupt might arrive after this, look at TS (interrupted_kernel_fpu_idle()), and stomp on the the guest's fpu.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 3/6] x86, kvm: use kernel_fpu_begin/end() in kvm_load/put_guest_fpu()
Quoted 16 lines · Avi Kivity
arch/x86/kvm/vmx.c static void __vmx_load_host_state(struct vcpu_vmx *vmx) · line 1498
Quoted 12 lines · Suresh Siddha
arch/x86/kvm/vmx.c +2
-1@@ -1493,7 +1493,8 @@ static void __vmx_load_host_state(struct vcpu_vmx *vmx)#ifdef CONFIG_X86_64 wrmsrl (MSR_KERNEL_GS_BASE , vmx->msr_host_kernel_gs_base);#endif -if (user_has_fpu ()) +/* Did the host task or the guest vcpu has FPU restored lazily? */ +if (!use_eager_fpu () && (user_has_fpu () || vmx->vcpu.guest_fpu_loaded))clts ();Why do the
clts()ifguest_fpu_loaded()?
Quoted 2 lines
An interrupt might arrive after this, look at TS (
interrupted_kernel_fpu_idle()), and stomp on the the guest's fpu.
Actually clts() is harmless, as this condition, (read_cr0() & X86_CR0_TS)
in interrupted_kernel_fpu_idle() will return false.
But you raise a good point, any interrupt between the vmexit and the __vmx_load_host_state() can stomp on the guest FPU as the vmexit was unconditionally setting host's cr0.TS bit and with the kvm using kernel_fpu_begin/end(), !__thread_has_fpu(current) in the interrupted_kernel_fpu_idle() will be always true.
So the right thing to do here is to always have the cr0.TS bit clear during vmexit and set that bit back in __vmx_load_host_state() if the FPU state is not active.
Appended the modified patch.
thanks,
suresh
--8<--
From: Suresh Siddha <[email protected]>
Subject: x86, kvm: fix kvm's usage of kernel_fpu_begin/end()
Preemption is disabled between kernel_fpu_begin/end() and as such it is not a good idea to use these routines in kvm_load/put_guest_fpu() which can be very far apart.
kvm_load/put_guest_fpu() routines are already called with preemption disabled and KVM already uses the preempt notifier to save the guest fpu state using kvm_put_guest_fpu().
So introduce __kernel_fpu_begin/end() routines which don't touch preemption and use them instead of kernel_fpu_begin/end() for KVM's use model of saving/restoring guest FPU state.
Also with this change (and with eagerFPU model), fix the host cr0.TS vm-exit state in the case of VMX. For eagerFPU case, host cr0.TS is always clear. So no need to worry about it. For the traditional lazyFPU restore case, change the cr0.TS bit for the host state during vm-exit to be always clear and cr0.TS bit is set in the __vmx_load_host_state() when the FPU (guest FPU or the host task's FPU) state is not active. This ensures that the host/guest FPU state is properly saved, restored
during context-switch and with interrupts (using irq_fpu_usable()) not stomping on the active FPU state.
--- arch/x86/include/asm/i387.h | 28 ++++++++++++++++++++++++++-- arch/x86/kernel/i387.c | 13 +++++-------- arch/x86/kvm/vmx.c | 10 +++++++--- arch/x86/kvm/x86.c | 4 ++-- 4 files changed, 40 insertions(+), 15 deletions(-)
arch/x86/include/asm/i387.h +26 -2
@@ -24,8 +24,32 @@ extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);extern void math_state_restore (void );extern bool irq_fpu_usable (void ); -extern void kernel_fpu_begin (void ); -extern void kernel_fpu_end (void ); + +/* +* 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. +*/ +extern void __kernel_fpu_begin (void ); +extern void __kernel_fpu_end (void ); + +static inline void kernel_fpu_begin (void ) +{ +WARN_ON_ONCE (!irq_fpu_usable ()); +preempt_disable (); +__kernel_fpu_begin (); +} + +static inline void kernel_fpu_end (void ) +{ +__kernel_fpu_end (); +preempt_enable (); +}/* * Some instructions like VIA's padlock instructions generate a spurious
arch/x86/kernel/i387.c +5 -8
@@ -73,32 +73,29 @@ bool irq_fpu_usable(void) }EXPORT_SYMBOL (irq_fpu_usable); -void kernel_fpu_begin (void ) +void __kernel_fpu_begin (void ) {struct task_struct *me = current; -WARN_ON_ONCE (!irq_fpu_usable ()); -preempt_disable ();if (__thread_has_fpu (me)) {__save_init_fpu (me);__thread_clear_has_fpu (me); -/* We do 'stts()' in kernel_fpu_end() */ +/* We do 'stts()' in __kernel_fpu_end() */ }else if (!use_eager_fpu ()) {this_cpu_write (fpu_owner_task,NULL );clts (); } } -EXPORT_SYMBOL (kernel_fpu_begin); +EXPORT_SYMBOL (__kernel_fpu_begin); -void kernel_fpu_end (void ) +void __kernel_fpu_end (void ) {if (use_eager_fpu ())math_state_restore ();else stts (); -preempt_enable (); } -EXPORT_SYMBOL (kernel_fpu_end); +EXPORT_SYMBOL (__kernel_fpu_end);void unlazy_fpu (struct task_struct *tsk) {
arch/x86/kvm/vmx.c +7 -3
@@ -1493,8 +1493,12 @@ static void __vmx_load_host_state(struct vcpu_vmx *vmx)#ifdef CONFIG_X86_64 wrmsrl (MSR_KERNEL_GS_BASE , vmx->msr_host_kernel_gs_base);#endif -if (user_has_fpu ()) -clts (); +/* +* If the FPU is not active (through the host task or +* the guest vcpu), then restore the cr0.TS bit. +*/ +if (!user_has_fpu () && !vmx->vcpu.guest_fpu_loaded) +stts ();load_gdt (&__get_cpu_var (host_gdt)); }
@@ -3743,7 +3747,7 @@ static void vmx_set_constant_host_state(void)unsigned long tmpl;struct desc_ptr dt; -vmcs_writel (HOST_CR0 ,read_cr0 () |X86_CR0_TS );/* 22.2.3 */ +vmcs_writel (HOST_CR0 ,read_cr0 () & ~X86_CR0_TS );/* 22.2.3 */ vmcs_writel (HOST_CR4 ,read_cr4 ());/* 22.2.3, 22.2.5 */ vmcs_writel (HOST_CR3 ,read_cr3 ());/* 22.2.3 FIXME: shadow tables */
arch/x86/kvm/x86.c +2 -2
@@ -5979,7 +5979,7 @@ void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)*/ kvm_put_guest_xcr0 (vcpu); vcpu->guest_fpu_loaded =1 ; -kernel_fpu_begin (); +__kernel_fpu_begin ();fpu_restore_checking (&vcpu->arch.guest_fpu);trace_kvm_fpu (1 ); }
@@ -5993,7 +5993,7 @@ void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) vcpu->guest_fpu_loaded =0 ;fpu_save_init (&vcpu->arch.guest_fpu); -kernel_fpu_end (); +__kernel_fpu_end (); ++vcpu->stat.fpu_reload;kvm_make_request (KVM_REQ_DEACTIVATE_FPU , vcpu);trace_kvm_fpu (0 );
[tip:x86/fpu] x86, kvm: fix kvm's usage of kernel_fpu_begin/end()
Commit-ID: b1a74bf8212367be2b1d6685c11a84e056eaaaf1 Gitweb: http://git.kernel.org/tip/b1a74bf8212367be2b1d6685c11a84e056eaaaf1 Author: Suresh Siddha <[email protected]> AuthorDate: Thu, 20 Sep 2012 11:01:49 -0700 Committer: H. Peter Anvin <[email protected]> CommitDate: Fri, 21 Sep 2012 16:59:04 -0700
x86, kvm: fix kvm's usage of kernel_fpu_begin/end()
Preemption is disabled between kernel_fpu_begin/end() and as such it is not a good idea to use these routines in kvm_load/put_guest_fpu() which can be very far apart.
kvm_load/put_guest_fpu() routines are already called with preemption disabled and KVM already uses the preempt notifier to save the guest fpu state using kvm_put_guest_fpu().
So introduce __kernel_fpu_begin/end() routines which don't touch preemption and use them instead of kernel_fpu_begin/end() for KVM's use model of saving/restoring guest FPU state.
Also with this change (and with eagerFPU model), fix the host cr0.TS vm-exit state in the case of VMX. For eagerFPU case, host cr0.TS is always clear. So no need to worry about it. For the traditional lazyFPU restore case, change the cr0.TS bit for the host state during vm-exit to be always clear and cr0.TS bit is set in the __vmx_load_host_state() when the FPU (guest FPU or the host task's FPU) state is not active. This ensures that the host/guest FPU state is properly saved, restored
during context-switch and with interrupts (using irq_fpu_usable()) not stomping on the active FPU state.
--- arch/x86/include/asm/i387.h | 28 ++++++++++++++++++++++++++-- arch/x86/kernel/i387.c | 13 +++++-------- arch/x86/kvm/vmx.c | 10 +++++++--- arch/x86/kvm/x86.c | 4 ++-- 4 files changed, 40 insertions(+), 15 deletions(-)
arch/x86/include/asm/i387.h +26 -2
@@ -24,8 +24,32 @@ extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);extern void math_state_restore (void );extern bool irq_fpu_usable (void ); -extern void kernel_fpu_begin (void ); -extern void kernel_fpu_end (void ); + +/* +* 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. +*/ +extern void __kernel_fpu_begin (void ); +extern void __kernel_fpu_end (void ); + +static inline void kernel_fpu_begin (void ) +{ +WARN_ON_ONCE (!irq_fpu_usable ()); +preempt_disable (); +__kernel_fpu_begin (); +} + +static inline void kernel_fpu_end (void ) +{ +__kernel_fpu_end (); +preempt_enable (); +}/* * Some instructions like VIA's padlock instructions generate a spurious
arch/x86/kernel/i387.c +5 -8
@@ -73,32 +73,29 @@ bool irq_fpu_usable(void) }EXPORT_SYMBOL (irq_fpu_usable); -void kernel_fpu_begin (void ) +void __kernel_fpu_begin (void ) {struct task_struct *me = current; -WARN_ON_ONCE (!irq_fpu_usable ()); -preempt_disable ();if (__thread_has_fpu (me)) {__save_init_fpu (me);__thread_clear_has_fpu (me); -/* We do 'stts()' in kernel_fpu_end() */ +/* We do 'stts()' in __kernel_fpu_end() */ }else if (!use_eager_fpu ()) {this_cpu_write (fpu_owner_task,NULL );clts (); } } -EXPORT_SYMBOL (kernel_fpu_begin); +EXPORT_SYMBOL (__kernel_fpu_begin); -void kernel_fpu_end (void ) +void __kernel_fpu_end (void ) {if (use_eager_fpu ())math_state_restore ();else stts (); -preempt_enable (); } -EXPORT_SYMBOL (kernel_fpu_end); +EXPORT_SYMBOL (__kernel_fpu_end);void unlazy_fpu (struct task_struct *tsk) {
arch/x86/kvm/vmx.c +7 -3
@@ -1493,8 +1493,12 @@ static void __vmx_load_host_state(struct vcpu_vmx *vmx)#ifdef CONFIG_X86_64 wrmsrl (MSR_KERNEL_GS_BASE , vmx->msr_host_kernel_gs_base);#endif -if (user_has_fpu ()) -clts (); +/* +* If the FPU is not active (through the host task or +* the guest vcpu), then restore the cr0.TS bit. +*/ +if (!user_has_fpu () && !vmx->vcpu.guest_fpu_loaded) +stts ();load_gdt (&__get_cpu_var (host_gdt)); }
@@ -3730,7 +3734,7 @@ static void vmx_set_constant_host_state(void)unsigned long tmpl;struct desc_ptr dt; -vmcs_writel (HOST_CR0 ,read_cr0 () |X86_CR0_TS );/* 22.2.3 */ +vmcs_writel (HOST_CR0 ,read_cr0 () & ~X86_CR0_TS );/* 22.2.3 */ vmcs_writel (HOST_CR4 ,read_cr4 ());/* 22.2.3, 22.2.5 */ vmcs_writel (HOST_CR3 ,read_cr3 ());/* 22.2.3 FIXME: shadow tables */
arch/x86/kvm/x86.c +2 -2
@@ -5972,7 +5972,7 @@ void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)*/ kvm_put_guest_xcr0 (vcpu); vcpu->guest_fpu_loaded =1 ; -kernel_fpu_begin (); +__kernel_fpu_begin ();fpu_restore_checking (&vcpu->arch.guest_fpu);trace_kvm_fpu (1 ); }
@@ -5986,7 +5986,7 @@ void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) vcpu->guest_fpu_loaded =0 ;fpu_save_init (&vcpu->arch.guest_fpu); -kernel_fpu_end (); +__kernel_fpu_end (); ++vcpu->stat.fpu_reload;kvm_make_request (KVM_REQ_DEACTIVATE_FPU , vcpu);trace_kvm_fpu (0 );
PATCH 4/6 x86, fpu: always use kernel_fpu_begin/end() for in-kernel FPU usage
In reply to Suresh Siddhause kernel_fpu_begin/end() instead of unconditionally accessing cr0 and saving/restoring just the few used xmm/ymm registers.
This has some advantages like:
* If the task's FPU state is already active, then kernel_fpu_begin() will just save the user-state and avoiding the read/write of cr0. In general, cr0 accesses are much slower.
* Manual save/restore of xmm/ymm registers will affect the 'modified' and the 'init' optimizations brought in the by xsaveopt/xrstor infrastructure.
* Foward compatibility with future vector register extensions will be a problem if the xmm/ymm registers are manually saved and restored (corrupting the extended state of those vector registers).
With this patch, there was no significant difference in the xor throughput using AVX, measured during boot.
--- arch/x86/include/asm/xor_32.h | 56 +++++------------------------------- arch/x86/include/asm/xor_64.h | 61 ++++++---------------------------------- arch/x86/include/asm/xor_avx.h | 54 ++++++++--------------------------- 3 files changed, 29 insertions(+), 142 deletions(-)
arch/x86/include/asm/xor_32.h +8 -48
@@ -534,38 +534,6 @@ static struct xor_block_template xor_block_p5_mmx = {* Copyright (C) 1999 Zach Brown (with obvious credit due Ingo) */ -#define XMMS_SAVE \ -do { \ - preempt_disable(); \ - cr0 = read_cr0(); \ - clts(); \ - asm volatile( \ - "movups %%xmm0,(%0) ;\n\t" \ - "movups %%xmm1,0x10(%0) ;\n\t" \ - "movups %%xmm2,0x20(%0) ;\n\t" \ - "movups %%xmm3,0x30(%0) ;\n\t" \ - : \ - : "r" (xmm_save) \ - : "memory"); \ -} while (0) - -#define XMMS_RESTORE \ -do { \ - asm volatile( \ - "sfence ;\n\t" \ - "movups (%0),%%xmm0 ;\n\t" \ - "movups 0x10(%0),%%xmm1 ;\n\t" \ - "movups 0x20(%0),%%xmm2 ;\n\t" \ - "movups 0x30(%0),%%xmm3 ;\n\t" \ - : \ - : "r" (xmm_save) \ - : "memory"); \ - write_cr0(cr0); \ - preempt_enable(); \ -} while (0) - -#define ALIGN16 __attribute__((aligned(16))) -#define OFFS (x) "16*("#x")"#define PF_OFFS (x) "256+16*("#x")"#define PF0 (x) " prefetchnta "PF_OFFS(x)"(%1) ;\n"
@@ -587,10 +555,8 @@ static void xor_sse_2(unsigned long bytes, unsigned long *p1, unsigned long *p2) { unsigned long lines = bytes >> 8; - char xmm_save[16*4] ALIGN16; -int cr0; -XMMS_SAVE ; +kernel_fpu_begin (); asm volatile(#undef BLOCK
@@ -633,7 +599,7 @@ xor_sse_2(unsigned long bytes, unsigned long *p1, unsigned long *p2) : : "memory"); -XMMS_RESTORE ; +kernel_fpu_end (); } static void
@@ -641,10 +607,8 @@ xor_sse_3(unsigned long bytes, unsigned long *p1, unsigned long *p2, unsigned long *p3) {unsigned long lines = bytes >>8 ; - char xmm_save[16*4] ALIGN16; -int cr0; -XMMS_SAVE ; +kernel_fpu_begin (); asm volatile(#undef BLOCK
@@ -694,7 +658,7 @@ xor_sse_3(unsigned long bytes, unsigned long *p1, unsigned long *p2, : : "memory" ); -XMMS_RESTORE ; +kernel_fpu_end (); } static void
@@ -702,10 +666,8 @@ xor_sse_4(unsigned long bytes, unsigned long *p1, unsigned long *p2, unsigned long *p3, unsigned long *p4) { unsigned long lines = bytes >> 8; - char xmm_save[16*4] ALIGN16; -int cr0; -XMMS_SAVE ; +kernel_fpu_begin (); asm volatile(#undef BLOCK
@@ -762,7 +724,7 @@ xor_sse_4(unsigned long bytes, unsigned long *p1, unsigned long *p2, : : "memory" ); -XMMS_RESTORE ; +kernel_fpu_end (); } static void
@@ -770,10 +732,8 @@ xor_sse_5(unsigned long bytes, unsigned long *p1, unsigned long *p2, unsigned long *p3, unsigned long *p4, unsigned long *p5) { unsigned long lines = bytes >> 8; - char xmm_save[16*4] ALIGN16; -int cr0; -XMMS_SAVE ; +kernel_fpu_begin ();/* Make sure GCC forgets anything it knows about p4 or p5, such that it won't pass to the asm volatile below a
@@ -850,7 +810,7 @@ xor_sse_5(unsigned long bytes, unsigned long *p1, unsigned long *p2,like assuming they have some legal value. */ asm("" :"=r" (p4),"=r" (p5)); -XMMS_RESTORE ; +kernel_fpu_end (); }static struct xor_block_template xor_block_pIII_sse = {
arch/x86/include/asm/xor_64.h +9 -52
119 lines not on this page · raw message
arch/x86/include/asm/xor_avx.h +12 -42
109 lines not on this page · raw message
Signature · 1 line
1.7.6.5
[tip:x86/fpu] x86, fpu: always use kernel_fpu_begin/end() for in-kernel FPU usage
Commit-ID: 964735018df03c94dd12665385d59e3b2c7c08b8 Gitweb: http://git.kernel.org/tip/964735018df03c94dd12665385d59e3b2c7c08b8 Author: Suresh Siddha <[email protected]> AuthorDate: Fri, 24 Aug 2012 14:13:00 -0700 Committer: H. Peter Anvin <[email protected]> CommitDate: Fri, 24 Aug 2012 14:26:50 -0700
x86, fpu: always use kernel_fpu_begin/end() for in-kernel FPU usage
use kernel_fpu_begin/end() instead of unconditionally accessing cr0 and saving/restoring just the few used xmm/ymm registers.
This has some advantages like:
* If the task's FPU state is already active, then kernel_fpu_begin() will just save the user-state and avoiding the read/write of cr0. In general, cr0 accesses are much slower.
* Manual save/restore of xmm/ymm registers will affect the 'modified' and the 'init' optimizations brought in the by xsaveopt/xrstor infrastructure.
* Foward compatibility with future vector register extensions will be a problem if the xmm/ymm registers are manually saved and restored (corrupting the extended state of those vector registers).
With this patch, there was no significant difference in the xor throughput using AVX, measured during boot.
--- arch/x86/include/asm/xor_32.h | 56 +++++------------------------------- arch/x86/include/asm/xor_64.h | 61 ++++++---------------------------------- arch/x86/include/asm/xor_avx.h | 54 ++++++++--------------------------- 3 files changed, 29 insertions(+), 142 deletions(-)
arch/x86/include/asm/xor_32.h +8 -48
@@ -534,38 +534,6 @@ static struct xor_block_template xor_block_p5_mmx = {* Copyright (C) 1999 Zach Brown (with obvious credit due Ingo) */ -#define XMMS_SAVE \ -do { \ - preempt_disable(); \ - cr0 = read_cr0(); \ - clts(); \ - asm volatile( \ - "movups %%xmm0,(%0) ;\n\t" \ - "movups %%xmm1,0x10(%0) ;\n\t" \ - "movups %%xmm2,0x20(%0) ;\n\t" \ - "movups %%xmm3,0x30(%0) ;\n\t" \ - : \ - : "r" (xmm_save) \ - : "memory"); \ -} while (0) - -#define XMMS_RESTORE \ -do { \ - asm volatile( \ - "sfence ;\n\t" \ - "movups (%0),%%xmm0 ;\n\t" \ - "movups 0x10(%0),%%xmm1 ;\n\t" \ - "movups 0x20(%0),%%xmm2 ;\n\t" \ - "movups 0x30(%0),%%xmm3 ;\n\t" \ - : \ - : "r" (xmm_save) \ - : "memory"); \ - write_cr0(cr0); \ - preempt_enable(); \ -} while (0) - -#define ALIGN16 __attribute__((aligned(16))) -#define OFFS (x) "16*("#x")"#define PF_OFFS (x) "256+16*("#x")"#define PF0 (x) " prefetchnta "PF_OFFS(x)"(%1) ;\n"
@@ -587,10 +555,8 @@ static void xor_sse_2(unsigned long bytes, unsigned long *p1, unsigned long *p2) { unsigned long lines = bytes >> 8; - char xmm_save[16*4] ALIGN16; -int cr0; -XMMS_SAVE ; +kernel_fpu_begin (); asm volatile(#undef BLOCK
@@ -633,7 +599,7 @@ xor_sse_2(unsigned long bytes, unsigned long *p1, unsigned long *p2) : : "memory"); -XMMS_RESTORE ; +kernel_fpu_end (); } static void
@@ -641,10 +607,8 @@ xor_sse_3(unsigned long bytes, unsigned long *p1, unsigned long *p2, unsigned long *p3) {unsigned long lines = bytes >>8 ; - char xmm_save[16*4] ALIGN16; -int cr0; -XMMS_SAVE ; +kernel_fpu_begin (); asm volatile(#undef BLOCK
@@ -694,7 +658,7 @@ xor_sse_3(unsigned long bytes, unsigned long *p1, unsigned long *p2, : : "memory" ); -XMMS_RESTORE ; +kernel_fpu_end (); } static void
@@ -702,10 +666,8 @@ xor_sse_4(unsigned long bytes, unsigned long *p1, unsigned long *p2, unsigned long *p3, unsigned long *p4) { unsigned long lines = bytes >> 8; - char xmm_save[16*4] ALIGN16; -int cr0; -XMMS_SAVE ; +kernel_fpu_begin (); asm volatile(#undef BLOCK
@@ -762,7 +724,7 @@ xor_sse_4(unsigned long bytes, unsigned long *p1, unsigned long *p2, : : "memory" ); -XMMS_RESTORE ; +kernel_fpu_end (); } static void
@@ -770,10 +732,8 @@ xor_sse_5(unsigned long bytes, unsigned long *p1, unsigned long *p2, unsigned long *p3, unsigned long *p4, unsigned long *p5) { unsigned long lines = bytes >> 8; - char xmm_save[16*4] ALIGN16; -int cr0; -XMMS_SAVE ; +kernel_fpu_begin ();/* Make sure GCC forgets anything it knows about p4 or p5, such that it won't pass to the asm volatile below a
@@ -850,7 +810,7 @@ xor_sse_5(unsigned long bytes, unsigned long *p1, unsigned long *p2,like assuming they have some legal value. */ asm("" :"=r" (p4),"=r" (p5)); -XMMS_RESTORE ; +kernel_fpu_end (); }static struct xor_block_template xor_block_pIII_sse = {
arch/x86/include/asm/xor_64.h +9 -52
119 lines not on this page · raw message
arch/x86/include/asm/xor_avx.h +12 -42
109 lines not on this page · raw message
[tip:x86/fpu] x86, fpu: always use kernel_fpu_begin/end() for in-kernel FPU usage
In reply to Suresh SiddhaCommit-ID: 841e3604d35aa70d399146abdc526d8c89a2c2f5 Gitweb: http://git.kernel.org/tip/841e3604d35aa70d399146abdc526d8c89a2c2f5 Author: Suresh Siddha <[email protected]> AuthorDate: Fri, 24 Aug 2012 14:13:00 -0700 Committer: H. Peter Anvin <[email protected]> CommitDate: Tue, 18 Sep 2012 15:52:08 -0700
x86, fpu: always use kernel_fpu_begin/end() for in-kernel FPU usage
use kernel_fpu_begin/end() instead of unconditionally accessing cr0 and saving/restoring just the few used xmm/ymm registers.
This has some advantages like:
* If the task's FPU state is already active, then kernel_fpu_begin() will just save the user-state and avoiding the read/write of cr0. In general, cr0 accesses are much slower.
* Manual save/restore of xmm/ymm registers will affect the 'modified' and the 'init' optimizations brought in the by xsaveopt/xrstor infrastructure.
* Foward compatibility with future vector register extensions will be a problem if the xmm/ymm registers are manually saved and restored (corrupting the extended state of those vector registers).
With this patch, there was no significant difference in the xor throughput using AVX, measured during boot.
--- arch/x86/include/asm/xor_32.h | 56 +++++------------------------------- arch/x86/include/asm/xor_64.h | 61 ++++++---------------------------------- arch/x86/include/asm/xor_avx.h | 54 ++++++++--------------------------- 3 files changed, 29 insertions(+), 142 deletions(-)
arch/x86/include/asm/xor_32.h +8 -48
@@ -534,38 +534,6 @@ static struct xor_block_template xor_block_p5_mmx = {* Copyright (C) 1999 Zach Brown (with obvious credit due Ingo) */ -#define XMMS_SAVE \ -do { \ - preempt_disable(); \ - cr0 = read_cr0(); \ - clts(); \ - asm volatile( \ - "movups %%xmm0,(%0) ;\n\t" \ - "movups %%xmm1,0x10(%0) ;\n\t" \ - "movups %%xmm2,0x20(%0) ;\n\t" \ - "movups %%xmm3,0x30(%0) ;\n\t" \ - : \ - : "r" (xmm_save) \ - : "memory"); \ -} while (0) - -#define XMMS_RESTORE \ -do { \ - asm volatile( \ - "sfence ;\n\t" \ - "movups (%0),%%xmm0 ;\n\t" \ - "movups 0x10(%0),%%xmm1 ;\n\t" \ - "movups 0x20(%0),%%xmm2 ;\n\t" \ - "movups 0x30(%0),%%xmm3 ;\n\t" \ - : \ - : "r" (xmm_save) \ - : "memory"); \ - write_cr0(cr0); \ - preempt_enable(); \ -} while (0) - -#define ALIGN16 __attribute__((aligned(16))) -#define OFFS (x) "16*("#x")"#define PF_OFFS (x) "256+16*("#x")"#define PF0 (x) " prefetchnta "PF_OFFS(x)"(%1) ;\n"
@@ -587,10 +555,8 @@ static void xor_sse_2(unsigned long bytes, unsigned long *p1, unsigned long *p2) { unsigned long lines = bytes >> 8; - char xmm_save[16*4] ALIGN16; -int cr0; -XMMS_SAVE ; +kernel_fpu_begin (); asm volatile(#undef BLOCK
@@ -633,7 +599,7 @@ xor_sse_2(unsigned long bytes, unsigned long *p1, unsigned long *p2) : : "memory"); -XMMS_RESTORE ; +kernel_fpu_end (); } static void
@@ -641,10 +607,8 @@ xor_sse_3(unsigned long bytes, unsigned long *p1, unsigned long *p2, unsigned long *p3) {unsigned long lines = bytes >>8 ; - char xmm_save[16*4] ALIGN16; -int cr0; -XMMS_SAVE ; +kernel_fpu_begin (); asm volatile(#undef BLOCK
@@ -694,7 +658,7 @@ xor_sse_3(unsigned long bytes, unsigned long *p1, unsigned long *p2, : : "memory" ); -XMMS_RESTORE ; +kernel_fpu_end (); } static void
@@ -702,10 +666,8 @@ xor_sse_4(unsigned long bytes, unsigned long *p1, unsigned long *p2, unsigned long *p3, unsigned long *p4) { unsigned long lines = bytes >> 8; - char xmm_save[16*4] ALIGN16; -int cr0; -XMMS_SAVE ; +kernel_fpu_begin (); asm volatile(#undef BLOCK
@@ -762,7 +724,7 @@ xor_sse_4(unsigned long bytes, unsigned long *p1, unsigned long *p2, : : "memory" ); -XMMS_RESTORE ; +kernel_fpu_end (); } static void
@@ -770,10 +732,8 @@ xor_sse_5(unsigned long bytes, unsigned long *p1, unsigned long *p2, unsigned long *p3, unsigned long *p4, unsigned long *p5) { unsigned long lines = bytes >> 8; - char xmm_save[16*4] ALIGN16; -int cr0; -XMMS_SAVE ; +kernel_fpu_begin ();/* Make sure GCC forgets anything it knows about p4 or p5, such that it won't pass to the asm volatile below a
@@ -850,7 +810,7 @@ xor_sse_5(unsigned long bytes, unsigned long *p1, unsigned long *p2,like assuming they have some legal value. */ asm("" :"=r" (p4),"=r" (p5)); -XMMS_RESTORE ; +kernel_fpu_end (); }static struct xor_block_template xor_block_pIII_sse = {
arch/x86/include/asm/xor_64.h +9 -52
119 lines not on this page · raw message
arch/x86/include/asm/xor_avx.h +12 -42
109 lines not on this page · raw message
PATCH 5/6 lguest, x86: handle guest TS bit for lazy/non-lazy fpu host models
In reply to Suresh SiddhaInstead of using unlazy_fpu() check if user_has_fpu() and set/clear the host TS bits so that the lguest works fine with both the lazy/non-lazy FPU host models with minimal changes.
--- drivers/lguest/x86/core.c | 10 +++++++--- 1 files changed, 7 insertions(+), 3 deletions(-)
drivers/lguest/x86/core.c +7 -3
@@ -203,8 +203,8 @@ void lguest_arch_run_guest(struct lg_cpu *cpu)* we set it now, so we can trap and pass that trap to the Guest if it * uses the FPU. */ -if (cpu->ts) -unlazy_fpu (current); +if (cpu->ts &&user_has_fpu ()) +stts ();/* * SYSENTER is an optimized way of doing system calls. We can't allow
@@ -234,6 +234,10 @@ void lguest_arch_run_guest(struct lg_cpu *cpu)if (boot_cpu_has (X86_FEATURE_SEP ))wrmsr (MSR_IA32_SYSENTER_CS , __KERNEL_CS,0 ); +/* Clear the host TS bit if it was set above. */ +if (cpu->ts &&user_has_fpu ()) +clts (); +/* * If the Guest page faulted, then the cr2 register will tell us the * bad virtual address. We have to grab this now, because once we
@@ -249,7 +253,7 @@ void lguest_arch_run_guest(struct lg_cpu *cpu)* a different CPU. So all the critical stuff should be done * before this. */ - else if (cpu->regs->trapnum == 7) + else if (cpu->regs->trapnum == 7 && !user_has_fpu()) math_state_restore(); }
Signature · 1 line
1.7.6.5
[tip:x86/fpu] lguest, x86: handle guest TS bit for lazy/ non-lazy fpu host models
Commit-ID: 1ce83ffda9aea53e6e4b6b6a82c028a019526010 Gitweb: http://git.kernel.org/tip/1ce83ffda9aea53e6e4b6b6a82c028a019526010 Author: Suresh Siddha <[email protected]> AuthorDate: Fri, 24 Aug 2012 14:13:01 -0700 Committer: H. Peter Anvin <[email protected]> CommitDate: Fri, 24 Aug 2012 14:26:52 -0700
lguest, x86: handle guest TS bit for lazy/non-lazy fpu host models
Instead of using unlazy_fpu() check if user_has_fpu() and set/clear the host TS bits so that the lguest works fine with both the lazy/non-lazy FPU host models with minimal changes.
--- drivers/lguest/x86/core.c | 10 +++++++--- 1 files changed, 7 insertions(+), 3 deletions(-)
drivers/lguest/x86/core.c +7 -3
@@ -203,8 +203,8 @@ void lguest_arch_run_guest(struct lg_cpu *cpu)* we set it now, so we can trap and pass that trap to the Guest if it * uses the FPU. */ -if (cpu->ts) -unlazy_fpu (current); +if (cpu->ts &&user_has_fpu ()) +stts ();/* * SYSENTER is an optimized way of doing system calls. We can't allow
@@ -234,6 +234,10 @@ void lguest_arch_run_guest(struct lg_cpu *cpu)if (boot_cpu_has (X86_FEATURE_SEP ))wrmsr (MSR_IA32_SYSENTER_CS , __KERNEL_CS,0 ); +/* Clear the host TS bit if it was set above. */ +if (cpu->ts &&user_has_fpu ()) +clts (); +/* * If the Guest page faulted, then the cr2 register will tell us the * bad virtual address. We have to grab this now, because once we
@@ -249,7 +253,7 @@ void lguest_arch_run_guest(struct lg_cpu *cpu)* a different CPU. So all the critical stuff should be done * before this. */ - else if (cpu->regs->trapnum == 7) + else if (cpu->regs->trapnum == 7 && !user_has_fpu()) math_state_restore(); }
[tip:x86/fpu] lguest, x86: handle guest TS bit for lazy/ non-lazy fpu host models
In reply to Suresh SiddhaCommit-ID: 9c6ff8bbb69a4e7b47ac40bfa44509296e89c5c0 Gitweb: http://git.kernel.org/tip/9c6ff8bbb69a4e7b47ac40bfa44509296e89c5c0 Author: Suresh Siddha <[email protected]> AuthorDate: Fri, 24 Aug 2012 14:13:01 -0700 Committer: H. Peter Anvin <[email protected]> CommitDate: Tue, 18 Sep 2012 15:52:09 -0700
lguest, x86: handle guest TS bit for lazy/non-lazy fpu host models
Instead of using unlazy_fpu() check if user_has_fpu() and set/clear the host TS bits so that the lguest works fine with both the lazy/non-lazy FPU host models with minimal changes.
--- drivers/lguest/x86/core.c | 10 +++++++--- 1 files changed, 7 insertions(+), 3 deletions(-)
drivers/lguest/x86/core.c +7 -3
@@ -203,8 +203,8 @@ void lguest_arch_run_guest(struct lg_cpu *cpu)* we set it now, so we can trap and pass that trap to the Guest if it * uses the FPU. */ -if (cpu->ts) -unlazy_fpu (current); +if (cpu->ts &&user_has_fpu ()) +stts ();/* * SYSENTER is an optimized way of doing system calls. We can't allow
@@ -234,6 +234,10 @@ void lguest_arch_run_guest(struct lg_cpu *cpu)if (boot_cpu_has (X86_FEATURE_SEP ))wrmsr (MSR_IA32_SYSENTER_CS , __KERNEL_CS,0 ); +/* Clear the host TS bit if it was set above. */ +if (cpu->ts &&user_has_fpu ()) +clts (); +/* * If the Guest page faulted, then the cr2 register will tell us the * bad virtual address. We have to grab this now, because once we
@@ -249,7 +253,7 @@ void lguest_arch_run_guest(struct lg_cpu *cpu)* a different CPU. So all the critical stuff should be done * before this. */ - else if (cpu->regs->trapnum == 7) + else if (cpu->regs->trapnum == 7 && !user_has_fpu()) math_state_restore(); }
PATCH 6/6 x86, fpu: use non-lazy fpu restore for processors supporting xsave
In reply to Suresh SiddhaFundamental model of the current Linux kernel is to lazily init and restore FPU instead of restoring the task state during context switch. This changes that fundamental lazy model to the non-lazy model for the processors supporting xsave feature.
Reasons driving this model change are:
i. Newer processors support optimized state save/restore using xsaveopt and xrstor by tracking the INIT state and MODIFIED state during context-switch. This is faster than modifying the cr0.TS bit which has serializing semantics.
ii. Newer glibc versions use SSE for some of the optimized copy/clear routines. With certain workloads (like boot, kernel-compilation etc), application completes its work with in the first 5 task switches, thus taking upto 5 #DNA traps with the kernel not getting a chance to apply the above mentioned pre-load heuristic.
iii. Some xstate features (like AMD's LWP feature) don't honor the cr0.TS bit and thus will not work correctly in the presence of lazy restore. Non-lazy state restore is needed for enabling such features.
Some data on a two socket SNB system:
* Saved 20K DNA exceptions during boot on a two socket SNB system.
* Saved 50K DNA exceptions during kernel-compilation workload.
* Improved throughput of the AVX based checksumming function inside the kernel by ~15% as xsave/xrstor is faster than the serializing clts/stts pair.
--- arch/x86/include/asm/fpu-internal.h | 96 +++++++++++++++++++++++------------ arch/x86/include/asm/i387.h | 1 + arch/x86/include/asm/xsave.h | 1 + arch/x86/kernel/i387.c | 20 ++++++- arch/x86/kernel/process.c | 12 +++-- arch/x86/kernel/process_32.c | 4 -- arch/x86/kernel/process_64.c | 4 -- arch/x86/kernel/traps.c | 5 ++- arch/x86/kernel/xsave.c | 57 +++++++++++++++++---- 9 files changed, 140 insertions(+), 60 deletions(-)
arch/x86/include/asm/fpu-internal.h +63 -33
@@ -291,15 +291,48 @@ static inline void __thread_set_has_fpu(struct task_struct *tsk)static inline void __thread_fpu_end (struct task_struct *tsk) {__thread_clear_has_fpu (tsk); -stts (); +if (!use_xsave ()) +stts (); }static inline void __thread_fpu_begin (struct task_struct *tsk) { -clts (); +if (!use_xsave ()) +clts ();__thread_set_has_fpu (tsk); } +static inline void __drop_fpu (struct task_struct *tsk) +{ +if (__thread_has_fpu (tsk)) { +/* Ignore delayed exceptions from user space */ + asmvolatile ("1: fwait\n" +"2:\n" + _ASM_EXTABLE(1b, 2b)); +__thread_fpu_end (tsk); + } +} + +static inline void drop_fpu (struct task_struct *tsk) +{ +/* +* Forget coprocessor state.. +*/ +preempt_disable (); + tsk->fpu_counter =0 ; +__drop_fpu (tsk); +clear_used_math (); +preempt_enable (); +} + +static inline void drop_init_fpu (struct task_struct *tsk) +{ +if (!use_xsave ()) +drop_fpu (tsk); +else +xrstor_state (init_xstate_buf,-1 ); +} +/* * FPU state switching for scheduling. *
@@ -333,7 +366,12 @@ static inline fpu_switch_t switch_fpu_prepare(struct task_struct *old, struct ta {fpu_switch_t fpu; - fpu.preload =tsk_used_math (new) && new->fpu_counter >5 ; +/* +* If the task has used the math, pre-load the FPU on xsave processors +* or if the past 5 consecutive context-switches used math. +*/ + fpu.preload =tsk_used_math (new) && (use_xsave () || + new->fpu_counter >5 );if (__thread_has_fpu (old)) {if (!__save_init_fpu (old)) cpu = ~0 ;
@@ -345,14 +383,14 @@ static inline fpu_switch_t switch_fpu_prepare(struct task_struct *old, struct ta new->fpu_counter++;__thread_set_has_fpu (new);prefetch (new->thread.fpu.state); - }else + }else if (!use_xsave ())stts (); }else { old->fpu_counter =0 ; old->thread.fpu.last_cpu = ~0 ;if (fpu.preload) { new->fpu_counter++; -if (fpu_lazy_restore (new, cpu)) +if (!use_xsave () &&fpu_lazy_restore (new, cpu)) fpu.preload =0 ;else prefetch (new->thread.fpu.state);
@@ -372,7 +410,7 @@ static inline void switch_fpu_finish(struct task_struct *new, fpu_switch_t fpu) {if (fpu.preload) {if (unlikely (restore_fpu_checking (new))) -__thread_fpu_end (new); +drop_init_fpu (new); } }
@@ -400,17 +438,6 @@ static inline int restore_xstate_sig(void __user *buf, int ia32_frame)return __restore_xstate_sig (buf, buf_fx, size); } -static inline void __drop_fpu (struct task_struct *tsk) -{ -if (__thread_has_fpu (tsk)) { -/* Ignore delayed exceptions from user space */ - asmvolatile ("1: fwait\n" -"2:\n" - _ASM_EXTABLE(1b, 2b)); -__thread_fpu_end (tsk); - } -} -/* * Need to be preemption-safe. *
@@ -431,24 +458,18 @@ static inline void user_fpu_begin(void)static inline void save_init_fpu (struct task_struct *tsk) {WARN_ON_ONCE (!__thread_has_fpu (tsk)); + +if (use_xsave ()) { +xsave_state (&tsk->thread.fpu.state->xsave,-1 ); +return ; + } +preempt_disable ();__save_init_fpu (tsk);__thread_fpu_end (tsk);preempt_enable (); } -static inline void drop_fpu (struct task_struct *tsk) -{ -/* -* Forget coprocessor state.. -*/ - tsk->fpu_counter =0 ; -preempt_disable (); -__drop_fpu (tsk); -preempt_enable (); -clear_used_math (); -} -/* * i387 state interaction */
@@ -503,12 +524,21 @@ static inline void fpu_free(struct fpu *fpu) } } -static inline void fpu_copy (struct fpu *dst,struct fpu *src) +static inline void fpu_copy (struct task_struct *dst,struct task_struct *src) { -memcpy (dst->state, src->state, xstate_size); -} +if (use_xsave ()) { +struct xsave_struct *xsave = &dst->thread.fpu.state->xsave; -extern void fpu_finit (struct fpu *fpu); +memset (&xsave->xsave_hdr,0 ,sizeof (struct xsave_hdr_struct )); +xsave_state (xsave,-1 ); + }else { +struct fpu *dfpu = &dst->thread.fpu; +struct fpu *sfpu = &src->thread.fpu; + +unlazy_fpu (src); +memcpy (dfpu->state, sfpu->state, xstate_size); + } +} static inline unsigned long alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx,
arch/x86/include/asm/i387.h +1 -0
@@ -19,6 +19,7 @@ struct pt_regs;struct user_i387_struct ;extern int init_fpu (struct task_struct *child); +extern void fpu_finit (struct fpu *fpu);extern int dump_fpu (struct pt_regs *,struct user_i387_struct *);extern void math_state_restore (void );
arch/x86/include/asm/xsave.h +1 -0
@@ -34,6 +34,7 @@extern unsigned int xstate_size;extern u64 pcntxt_mask;extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS ]; +extern struct xsave_struct *init_xstate_buf;extern void xsave_init (void );extern void update_regset_xstate_info (unsigned int size,u64 xstate_mask);
arch/x86/kernel/i387.c +17 -3
@@ -22,7 +22,15 @@/* * Were we in an interrupt that interrupted kernel mode? * -* We can do a kernel_fpu_begin/end() pair *ONLY* if that +* For now, on xsave platforms we will return interrupted +* kernel FPU as not-idle. TBD: As we use non-lazy FPU restore +* for xsave platforms, ideally we can change the return value +* to something like __thread_has_fpu(current). But we need to +* be careful of doing __thread_clear_has_fpu() before saving +* the FPU etc for supporting nested uses etc. For now, take +* the simple route! +* +* On others, we can do a kernel_fpu_begin/end() pair *ONLY* if that * pair does nothing at all: the thread must not have fpu (so * that we don't try to save the FPU state), and TS must * be set (so that the clts/stts pair does nothing that is
@@ -30,6 +38,9 @@*/ static inline bool interrupted_kernel_fpu_idle (void ) { +if (use_xsave ()) +return 0 ; +return !__thread_has_fpu (current) && (read_cr0 () &X86_CR0_TS ); }
@@ -73,7 +84,7 @@ void kernel_fpu_begin(void)__save_init_fpu (me);__thread_clear_has_fpu (me);/* We do 'stts()' in kernel_fpu_end() */ - }else { + }else if (!use_xsave ()) {this_cpu_write (fpu_owner_task,NULL );clts (); }
@@ -82,7 +93,10 @@ EXPORT_SYMBOL(kernel_fpu_begin);void kernel_fpu_end (void ) { -stts (); +if (use_xsave ()) +math_state_restore (); +else +stts ();preempt_enable (); }EXPORT_SYMBOL (kernel_fpu_end);
arch/x86/kernel/process.c +8 -4
@@ -66,15 +66,13 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) {int ret; -unlazy_fpu (src); - *dst = *src;if (fpu_allocated (&src->thread.fpu)) {memset (&dst->thread.fpu,0 ,sizeof (dst->thread.fpu)); ret =fpu_alloc (&dst->thread.fpu);if (ret)return ret; -fpu_copy (&dst->thread.fpu, &src->thread.fpu); +fpu_copy (dst, src); }return 0 ; }
@@ -153,7 +151,13 @@ void flush_thread(void)flush_ptrace_hw_breakpoint (tsk);memset (tsk->thread.tls_array,0 ,sizeof (tsk->thread.tls_array)); -drop_fpu (tsk); +drop_init_fpu (tsk); +/* +* Free the FPU state for non xsave platforms. They get reallocated +* lazily at the first use. +*/ +if (!use_xsave ()) +free_thread_xstate (tsk); }static void hard_disable_TSC (void )
arch/x86/kernel/process_32.c +0 -4
@@ -190,10 +190,6 @@ start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp) regs->cs = __USER_CS; regs->ip = new_ip; regs->sp = new_sp; -/* -* Free the old FP and other extended state -*/ -free_thread_xstate (current); }EXPORT_SYMBOL_GPL (start_thread);
arch/x86/kernel/process_64.c +0 -4
@@ -232,10 +232,6 @@ start_thread_common(struct pt_regs *regs, unsigned long new_ip, regs->cs = _cs; regs->ss = _ss; regs->flags =X86_EFLAGS_IF ; -/* -* Free the old FP and other extended state -*/ -free_thread_xstate (current); }void
arch/x86/kernel/traps.c +4 -1
@@ -613,11 +613,12 @@ void math_state_restore(void) }__thread_fpu_begin (tsk); +/* * Paranoid restore. send a SIGSEGV if we fail to restore the state. */ if (unlikely (restore_fpu_checking (tsk))) { -__thread_fpu_end (tsk); +drop_init_fpu (tsk);force_sig (SIGSEGV , tsk);return ; }
@@ -629,6 +630,8 @@ EXPORT_SYMBOL_GPL(math_state_restore); dotraplinkage void __kprobesdo_device_not_available (struct pt_regs *regs,long error_code) { +BUG_ON (use_xsave ()); +#ifdef CONFIG_MATH_EMULATION if (read_cr0 () &X86_CR0_EM ) {struct math_emu_info info = { };
arch/x86/kernel/xsave.c +46 -11
126 lines not on this page · raw message
Signature · 1 line
1.7.6.5
[tip:x86/fpu] x86, fpu: use non-lazy fpu restore for processors supporting xsave
Commit-ID: 127f5403bfbc5f52cf0fbbadfa5e624a32a137ff Gitweb: http://git.kernel.org/tip/127f5403bfbc5f52cf0fbbadfa5e624a32a137ff Author: Suresh Siddha <[email protected]> AuthorDate: Fri, 24 Aug 2012 14:13:02 -0700 Committer: H. Peter Anvin <[email protected]> CommitDate: Fri, 24 Aug 2012 14:26:54 -0700
x86, fpu: use non-lazy fpu restore for processors supporting xsave
Fundamental model of the current Linux kernel is to lazily init and restore FPU instead of restoring the task state during context switch. This changes that fundamental lazy model to the non-lazy model for the processors supporting xsave feature.
Reasons driving this model change are:
i. Newer processors support optimized state save/restore using xsaveopt and xrstor by tracking the INIT state and MODIFIED state during context-switch. This is faster than modifying the cr0.TS bit which has serializing semantics.
ii. Newer glibc versions use SSE for some of the optimized copy/clear routines. With certain workloads (like boot, kernel-compilation etc), application completes its work with in the first 5 task switches, thus taking upto 5 #DNA traps with the kernel not getting a chance to apply the above mentioned pre-load heuristic.
iii. Some xstate features (like AMD's LWP feature) don't honor the cr0.TS bit and thus will not work correctly in the presence of lazy restore. Non-lazy state restore is needed for enabling such features.
Some data on a two socket SNB system:
* Saved 20K DNA exceptions during boot on a two socket SNB system.
* Saved 50K DNA exceptions during kernel-compilation workload.
* Improved throughput of the AVX based checksumming function inside the kernel by ~15% as xsave/xrstor is faster than the serializing clts/stts pair.
--- arch/x86/include/asm/fpu-internal.h | 96 +++++++++++++++++++++++------------ arch/x86/include/asm/i387.h | 1 + arch/x86/include/asm/xsave.h | 1 + arch/x86/kernel/i387.c | 20 ++++++- arch/x86/kernel/process.c | 12 +++-- arch/x86/kernel/process_32.c | 4 -- arch/x86/kernel/process_64.c | 4 -- arch/x86/kernel/traps.c | 5 ++- arch/x86/kernel/xsave.c | 57 +++++++++++++++++---- 9 files changed, 140 insertions(+), 60 deletions(-)
arch/x86/include/asm/fpu-internal.h +63 -33
@@ -291,15 +291,48 @@ static inline void __thread_set_has_fpu(struct task_struct *tsk)static inline void __thread_fpu_end (struct task_struct *tsk) {__thread_clear_has_fpu (tsk); -stts (); +if (!use_xsave ()) +stts (); }static inline void __thread_fpu_begin (struct task_struct *tsk) { -clts (); +if (!use_xsave ()) +clts ();__thread_set_has_fpu (tsk); } +static inline void __drop_fpu (struct task_struct *tsk) +{ +if (__thread_has_fpu (tsk)) { +/* Ignore delayed exceptions from user space */ + asmvolatile ("1: fwait\n" +"2:\n" + _ASM_EXTABLE(1b, 2b)); +__thread_fpu_end (tsk); + } +} + +static inline void drop_fpu (struct task_struct *tsk) +{ +/* +* Forget coprocessor state.. +*/ +preempt_disable (); + tsk->fpu_counter =0 ; +__drop_fpu (tsk); +clear_used_math (); +preempt_enable (); +} + +static inline void drop_init_fpu (struct task_struct *tsk) +{ +if (!use_xsave ()) +drop_fpu (tsk); +else +xrstor_state (init_xstate_buf,-1 ); +} +/* * FPU state switching for scheduling. *
@@ -333,7 +366,12 @@ static inline fpu_switch_t switch_fpu_prepare(struct task_struct *old, struct ta {fpu_switch_t fpu; - fpu.preload =tsk_used_math (new) && new->fpu_counter >5 ; +/* +* If the task has used the math, pre-load the FPU on xsave processors +* or if the past 5 consecutive context-switches used math. +*/ + fpu.preload =tsk_used_math (new) && (use_xsave () || + new->fpu_counter >5 );if (__thread_has_fpu (old)) {if (!__save_init_fpu (old)) cpu = ~0 ;
@@ -345,14 +383,14 @@ static inline fpu_switch_t switch_fpu_prepare(struct task_struct *old, struct ta new->fpu_counter++;__thread_set_has_fpu (new);prefetch (new->thread.fpu.state); - }else + }else if (!use_xsave ())stts (); }else { old->fpu_counter =0 ; old->thread.fpu.last_cpu = ~0 ;if (fpu.preload) { new->fpu_counter++; -if (fpu_lazy_restore (new, cpu)) +if (!use_xsave () &&fpu_lazy_restore (new, cpu)) fpu.preload =0 ;else prefetch (new->thread.fpu.state);
@@ -372,7 +410,7 @@ static inline void switch_fpu_finish(struct task_struct *new, fpu_switch_t fpu) {if (fpu.preload) {if (unlikely (restore_fpu_checking (new))) -__thread_fpu_end (new); +drop_init_fpu (new); } }
@@ -400,17 +438,6 @@ static inline int restore_xstate_sig(void __user *buf, int ia32_frame)return __restore_xstate_sig (buf, buf_fx, size); } -static inline void __drop_fpu (struct task_struct *tsk) -{ -if (__thread_has_fpu (tsk)) { -/* Ignore delayed exceptions from user space */ - asmvolatile ("1: fwait\n" -"2:\n" - _ASM_EXTABLE(1b, 2b)); -__thread_fpu_end (tsk); - } -} -/* * Need to be preemption-safe. *
@@ -431,24 +458,18 @@ static inline void user_fpu_begin(void)static inline void save_init_fpu (struct task_struct *tsk) {WARN_ON_ONCE (!__thread_has_fpu (tsk)); + +if (use_xsave ()) { +xsave_state (&tsk->thread.fpu.state->xsave,-1 ); +return ; + } +preempt_disable ();__save_init_fpu (tsk);__thread_fpu_end (tsk);preempt_enable (); } -static inline void drop_fpu (struct task_struct *tsk) -{ -/* -* Forget coprocessor state.. -*/ - tsk->fpu_counter =0 ; -preempt_disable (); -__drop_fpu (tsk); -preempt_enable (); -clear_used_math (); -} -/* * i387 state interaction */
@@ -503,12 +524,21 @@ static inline void fpu_free(struct fpu *fpu) } } -static inline void fpu_copy (struct fpu *dst,struct fpu *src) +static inline void fpu_copy (struct task_struct *dst,struct task_struct *src) { -memcpy (dst->state, src->state, xstate_size); -} +if (use_xsave ()) { +struct xsave_struct *xsave = &dst->thread.fpu.state->xsave; -extern void fpu_finit (struct fpu *fpu); +memset (&xsave->xsave_hdr,0 ,sizeof (struct xsave_hdr_struct )); +xsave_state (xsave,-1 ); + }else { +struct fpu *dfpu = &dst->thread.fpu; +struct fpu *sfpu = &src->thread.fpu; + +unlazy_fpu (src); +memcpy (dfpu->state, sfpu->state, xstate_size); + } +} static inline unsigned long alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx,
arch/x86/include/asm/i387.h +1 -0
@@ -19,6 +19,7 @@ struct pt_regs;struct user_i387_struct ;extern int init_fpu (struct task_struct *child); +extern void fpu_finit (struct fpu *fpu);extern int dump_fpu (struct pt_regs *,struct user_i387_struct *);extern void math_state_restore (void );
arch/x86/include/asm/xsave.h +1 -0
@@ -34,6 +34,7 @@extern unsigned int xstate_size;extern u64 pcntxt_mask;extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS ]; +extern struct xsave_struct *init_xstate_buf;extern void xsave_init (void );extern void update_regset_xstate_info (unsigned int size,u64 xstate_mask);
arch/x86/kernel/i387.c +17 -3
@@ -22,7 +22,15 @@/* * Were we in an interrupt that interrupted kernel mode? * -* We can do a kernel_fpu_begin/end() pair *ONLY* if that +* For now, on xsave platforms we will return interrupted +* kernel FPU as not-idle. TBD: As we use non-lazy FPU restore +* for xsave platforms, ideally we can change the return value +* to something like __thread_has_fpu(current). But we need to +* be careful of doing __thread_clear_has_fpu() before saving +* the FPU etc for supporting nested uses etc. For now, take +* the simple route! +* +* On others, we can do a kernel_fpu_begin/end() pair *ONLY* if that * pair does nothing at all: the thread must not have fpu (so * that we don't try to save the FPU state), and TS must * be set (so that the clts/stts pair does nothing that is
@@ -30,6 +38,9 @@*/ static inline bool interrupted_kernel_fpu_idle (void ) { +if (use_xsave ()) +return 0 ; +return !__thread_has_fpu (current) && (read_cr0 () &X86_CR0_TS ); }
@@ -73,7 +84,7 @@ void kernel_fpu_begin(void)__save_init_fpu (me);__thread_clear_has_fpu (me);/* We do 'stts()' in kernel_fpu_end() */ - }else { + }else if (!use_xsave ()) {this_cpu_write (fpu_owner_task,NULL );clts (); }
@@ -82,7 +93,10 @@ EXPORT_SYMBOL(kernel_fpu_begin);void kernel_fpu_end (void ) { -stts (); +if (use_xsave ()) +math_state_restore (); +else +stts ();preempt_enable (); }EXPORT_SYMBOL (kernel_fpu_end);
arch/x86/kernel/process.c +8 -4
@@ -66,15 +66,13 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) {int ret; -unlazy_fpu (src); - *dst = *src;if (fpu_allocated (&src->thread.fpu)) {memset (&dst->thread.fpu,0 ,sizeof (dst->thread.fpu)); ret =fpu_alloc (&dst->thread.fpu);if (ret)return ret; -fpu_copy (&dst->thread.fpu, &src->thread.fpu); +fpu_copy (dst, src); }return 0 ; }
@@ -153,7 +151,13 @@ void flush_thread(void)flush_ptrace_hw_breakpoint (tsk);memset (tsk->thread.tls_array,0 ,sizeof (tsk->thread.tls_array)); -drop_fpu (tsk); +drop_init_fpu (tsk); +/* +* Free the FPU state for non xsave platforms. They get reallocated +* lazily at the first use. +*/ +if (!use_xsave ()) +free_thread_xstate (tsk); }static void hard_disable_TSC (void )
arch/x86/kernel/process_32.c +0 -4
@@ -190,10 +190,6 @@ start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp) regs->cs = __USER_CS; regs->ip = new_ip; regs->sp = new_sp; -/* -* Free the old FP and other extended state -*/ -free_thread_xstate (current); }EXPORT_SYMBOL_GPL (start_thread);
arch/x86/kernel/process_64.c +0 -4
@@ -232,10 +232,6 @@ start_thread_common(struct pt_regs *regs, unsigned long new_ip, regs->cs = _cs; regs->ss = _ss; regs->flags =X86_EFLAGS_IF ; -/* -* Free the old FP and other extended state -*/ -free_thread_xstate (current); }void
arch/x86/kernel/traps.c +4 -1
@@ -613,11 +613,12 @@ void math_state_restore(void) }__thread_fpu_begin (tsk); +/* * Paranoid restore. send a SIGSEGV if we fail to restore the state. */ if (unlikely (restore_fpu_checking (tsk))) { -__thread_fpu_end (tsk); +drop_init_fpu (tsk);force_sig (SIGSEGV , tsk);return ; }
@@ -629,6 +630,8 @@ EXPORT_SYMBOL_GPL(math_state_restore); dotraplinkage void __kprobesdo_device_not_available (struct pt_regs *regs,long error_code) { +BUG_ON (use_xsave ()); +#ifdef CONFIG_MATH_EMULATION if (read_cr0 () &X86_CR0_EM ) {struct math_emu_info info = { };
arch/x86/kernel/xsave.c +46 -11
126 lines not on this page · raw message
H. Peter Anvin
reviewerI have applied this to tip:x86/fpu, but I have also asked Suresh to prepare a followon patch to decouple eager save from the existence of the XSAVE instruction. It seems pretty clear that eager save is a net benefit in the presence of the XSAVEOPT, but it isn't as clear for only having XSAVE, as far as I can tell. Either way it would seem to be a policy decision that is somewhat separate from the exact instruction.
-hpa
Quoted 6 lines · H. Peter Anvin
I have applied this to tip:x86/fpu, but I have also asked Suresh to prepare a followon patch to decouple eager save from the existence of the XSAVE instruction. It seems pretty clear that eager save is a net benefit in the presence of the XSAVEOPT, but it isn't as clear for only having XSAVE, as far as I can tell. Either way it would seem to be a policy decision that is somewhat separate from the exact instruction.
Agreed. It would also be good for testing bugs and/or performance for both xsave and non-xsave cases.
Linus
v2 cover eagerfpu patches for tip/x86/fpu
v2 version of the previous patchset https://lkml.org/lkml/2012/9/7/548
that decouples eagerfpu into a synthetic cpuid feature.
changes from v1:
* Made "eagerfpu=" boot parameter tri-state
* removed the cpu_has_xmm check in the fx_finit (not related to eagerfpu per-say, but this patchset touches that area of code).
Suresh Siddha (5):
x86, fpu: decouple non-lazy/eager fpu restore from xsave x86, fpu: enable eagerfpu by default for xsaveopt x86, fpu: move check_fpu() after alternative_instructions() x86, fpu: make eagerfpu= boot param tri-state x86, fpu: remove cpu_has_xmm check in the fx_finit()
Documentation/kernel-parameters.txt | 6 ++ arch/x86/include/asm/cpufeature.h | 3 + arch/x86/include/asm/fpu-internal.h | 53 ++++++++++++++------ arch/x86/kernel/cpu/bugs.c | 7 ++- arch/x86/kernel/cpu/common.c | 2 - arch/x86/kernel/i387.c | 25 +++------ arch/x86/kernel/process.c | 2 +- arch/x86/kernel/traps.c | 2 +- arch/x86/kernel/xsave.c | 95 ++++++++++++++++++++++++----------- 9 files changed, 129 insertions(+), 66 deletions(-)
Signature · 1 line
1.7.6.5
PATCH v2 1/5 x86, fpu: decouple non-lazy/eager fpu restore from xsave
Decouple non-lazy/eager fpu restore policy from the existence of the xsave feature. Introduce a synthetic CPUID flag to represent the eagerfpu policy. "eagerfpu=on" boot paramter will enable the policy.
--- Documentation/kernel-parameters.txt | 4 ++ arch/x86/include/asm/cpufeature.h | 2 + arch/x86/include/asm/fpu-internal.h | 54 ++++++++++++++++------ arch/x86/kernel/cpu/common.c | 2 - arch/x86/kernel/i387.c | 25 +++------- arch/x86/kernel/process.c | 2 +- arch/x86/kernel/traps.c | 2 +- arch/x86/kernel/xsave.c | 87 +++++++++++++++++++++++------------ 8 files changed, 112 insertions(+), 66 deletions(-)
Documentation/kernel-parameters.txt +4 -0
@@ -1834,6 +1834,10 @@ bytes respectively. Such letter suffixes can also be entirely omitted. and restore using xsave. The kernel will fallback to enabling legacy floating-point and sse state. + eagerfpu= [X86] + on enable eager fpu restore + off disable eager fpu restore + nohlt [BUGS=ARM,SH] Tells the kernel that the sleep(SH) or wfi(ARM) instruction doesn't work correctly and not to use it. This is also useful when using JTAG debugger.
arch/x86/include/asm/cpufeature.h +2 -0
@@ -97,6 +97,7 @@#define X86_FEATURE_EXTD_APICID (3*32+26)/* has extended APICID (8 bits) */ #define X86_FEATURE_AMD_DCM (3*32+27)/* multi-node processor */ #define X86_FEATURE_APERFMPERF (3*32+28)/* APERFMPERF */ +#define X86_FEATURE_EAGER_FPU (3*32+29)/* "eagerfpu" Non lazy FPU restore */ /* Intel-defined CPU features, CPUID level 0x00000001 (ecx), word 4 */ #define X86_FEATURE_XMM3 (4*32+ 0)/* "pni" SSE-3 */
@@ -303,6 +304,7 @@ extern const char * const x86_power_flags[32];#define cpu_has_perfctr_core boot_cpu_has(X86_FEATURE_PERFCTR_CORE)#define cpu_has_cx8 boot_cpu_has(X86_FEATURE_CX8)#define cpu_has_cx16 boot_cpu_has(X86_FEATURE_CX16) +#define cpu_has_eager_fpu boot_cpu_has(X86_FEATURE_EAGER_FPU)#if defined(CONFIG_X86_INVLPG ) || defined(CONFIG_X86_64 )# define cpu_has_invlpg 1
arch/x86/include/asm/fpu-internal.h +39 -15
126 lines not on this page · raw message
arch/x86/kernel/cpu/common.c +0 -2
@@ -1264,7 +1264,6 @@ void __cpuinit cpu_init(void)dbg_restore_debug_regs ();fpu_init (); -xsave_init ();raw_local_save_flags (kernel_eflags);
@@ -1319,6 +1318,5 @@ void __cpuinit cpu_init(void)dbg_restore_debug_regs (); fpu_init(); - xsave_init(); }#endif
arch/x86/kernel/i387.c +8 -17
@@ -22,9 +22,8 @@/* * Were we in an interrupt that interrupted kernel mode? * -* For now, on xsave platforms we will return interrupted -* kernel FPU as not-idle. TBD: As we use non-lazy FPU restore -* for xsave platforms, ideally we can change the return value +* For now, with eagerfpu we will return interrupted kernel FPU +* state as not-idle. TBD: Ideally we can change the return value * to something like __thread_has_fpu(current). But we need to * be careful of doing __thread_clear_has_fpu() before saving * the FPU etc for supporting nested uses etc. For now, take
@@ -38,7 +37,7 @@*/ static inline bool interrupted_kernel_fpu_idle (void ) { -if (use_xsave ()) +if (use_eager_fpu ())return 0 ; return !__thread_has_fpu(current) &&
@@ -84,7 +83,7 @@ void kernel_fpu_begin(void)__save_init_fpu (me);__thread_clear_has_fpu (me);/* We do 'stts()' in kernel_fpu_end() */ - }else if (!use_xsave ()) { + }else if (!use_eager_fpu ()) {this_cpu_write (fpu_owner_task,NULL );clts (); }
@@ -93,7 +92,7 @@ EXPORT_SYMBOL(kernel_fpu_begin);void kernel_fpu_end (void ) { -if (use_xsave ()) +if (use_eager_fpu ())math_state_restore ();else stts ();
@@ -122,7 +121,6 @@ static void __cpuinit mxcsr_feature_mask_init(void) {unsigned long mask =0 ; -clts ();if (cpu_has_fxsr) {memset (&fx_scratch,0 ,sizeof (struct i387_fxsave_struct )); asmvolatile ("fxsave %0" : :"m" (fx_scratch));
@@ -131,7 +129,6 @@ static void __cpuinit mxcsr_feature_mask_init(void) mask =0x0000ffbf ; } mxcsr_feature_mask &= mask; -stts (); }static void __cpuinit init_thread_xstate (void )
@@ -185,9 +182,8 @@ void __cpuinit fpu_init(void)init_thread_xstate ();mxcsr_feature_mask_init (); -/* clean state in init */ -current_thread_info ()->status =0 ; -clear_used_math (); +xsave_init (); +eager_fpu_init (); }void fpu_finit (struct fpu *fpu)
@@ -198,12 +194,7 @@ void fpu_finit(struct fpu *fpu) }if (cpu_has_fxsr) { -struct i387_fxsave_struct *fx = &fpu->state->fxsave; - -memset (fx,0 , xstate_size); - fx->cwd =0x37f ; -if (cpu_has_xmm) - fx->mxcsr =MXCSR_DEFAULT ; +fx_finit (&fpu->state->fxsave); }else {struct i387_fsave_struct *fp = &fpu->state->fsave;memset (fp,0 , xstate_size);
arch/x86/kernel/process.c +1 -1
@@ -156,7 +156,7 @@ void flush_thread(void)* Free the FPU state for non xsave platforms. They get reallocated * lazily at the first use. */ -if (!use_xsave ()) +if (!use_eager_fpu ())free_thread_xstate (tsk); }
arch/x86/kernel/traps.c +1 -1
@@ -630,7 +630,7 @@ EXPORT_SYMBOL_GPL(math_state_restore); dotraplinkage void __kprobesdo_device_not_available (struct pt_regs *regs,long error_code) { -BUG_ON (use_xsave ()); +BUG_ON (use_eager_fpu ());#ifdef CONFIG_MATH_EMULATION if (read_cr0 () &X86_CR0_EM ) {
arch/x86/kernel/xsave.c +57 -30
137 lines not on this page · raw message
Signature · 1 line
1.7.6.5
Re: [PATCH v2 1/5] x86, fpu: decouple non-lazy/eager fpu restore from xsave
Hi!
irely omitted.
Documentation/kernel-parameters.txt bytes respectively. Such letter suffixes can also be entirely omitted. · line 1840
Quoted 7 lines
Documentation/kernel-parameters.txt +4
-0and restore using xsave. The kernel will fallback to enabling legacy floating-point and sse state. + eagerfpu= [X86] + on enable eager fpu restore + off disable eager fpu restore +
Hmm. Only useful piece of information here is that this is about FPU _restore_. Otherwise it tells the user exactly nothing.
Why would user want to enable it? Disable it? What is the default?
Pavel
[tip:x86/fpu] x86, fpu: decouple non-lazy/ eager fpu restore from xsave
In reply to Suresh SiddhaCommit-ID: 5d2bd7009f306c82afddd1ca4d9763ad8473c216 Gitweb: http://git.kernel.org/tip/5d2bd7009f306c82afddd1ca4d9763ad8473c216 Author: Suresh Siddha <[email protected]> AuthorDate: Thu, 6 Sep 2012 14:58:52 -0700 Committer: H. Peter Anvin <[email protected]> CommitDate: Tue, 18 Sep 2012 15:52:22 -0700
x86, fpu: decouple non-lazy/eager fpu restore from xsave
Decouple non-lazy/eager fpu restore policy from the existence of the xsave feature. Introduce a synthetic CPUID flag to represent the eagerfpu policy. "eagerfpu=on" boot paramter will enable the policy.
--- Documentation/kernel-parameters.txt | 4 ++ arch/x86/include/asm/cpufeature.h | 2 + arch/x86/include/asm/fpu-internal.h | 54 ++++++++++++++++------ arch/x86/kernel/cpu/common.c | 2 - arch/x86/kernel/i387.c | 25 +++------- arch/x86/kernel/process.c | 2 +- arch/x86/kernel/traps.c | 2 +- arch/x86/kernel/xsave.c | 87 +++++++++++++++++++++++------------ 8 files changed, 112 insertions(+), 66 deletions(-)
Documentation/kernel-parameters.txt +4 -0
@@ -1833,6 +1833,10 @@ bytes respectively. Such letter suffixes can also be entirely omitted. and restore using xsave. The kernel will fallback to enabling legacy floating-point and sse state. + eagerfpu= [X86] + on enable eager fpu restore + off disable eager fpu restore + nohlt [BUGS=ARM,SH] Tells the kernel that the sleep(SH) or wfi(ARM) instruction doesn't work correctly and not to use it. This is also useful when using JTAG debugger.
arch/x86/include/asm/cpufeature.h +2 -0
@@ -97,6 +97,7 @@#define X86_FEATURE_EXTD_APICID (3*32+26)/* has extended APICID (8 bits) */ #define X86_FEATURE_AMD_DCM (3*32+27)/* multi-node processor */ #define X86_FEATURE_APERFMPERF (3*32+28)/* APERFMPERF */ +#define X86_FEATURE_EAGER_FPU (3*32+29)/* "eagerfpu" Non lazy FPU restore */ /* Intel-defined CPU features, CPUID level 0x00000001 (ecx), word 4 */ #define X86_FEATURE_XMM3 (4*32+ 0)/* "pni" SSE-3 */
@@ -305,6 +306,7 @@ extern const char * const x86_power_flags[32];#define cpu_has_perfctr_core boot_cpu_has(X86_FEATURE_PERFCTR_CORE)#define cpu_has_cx8 boot_cpu_has(X86_FEATURE_CX8)#define cpu_has_cx16 boot_cpu_has(X86_FEATURE_CX16) +#define cpu_has_eager_fpu boot_cpu_has(X86_FEATURE_EAGER_FPU)#if defined(CONFIG_X86_INVLPG ) || defined(CONFIG_X86_64 )# define cpu_has_invlpg 1
arch/x86/include/asm/fpu-internal.h +39 -15
126 lines not on this page · raw message
arch/x86/kernel/cpu/common.c +0 -2
@@ -1297,7 +1297,6 @@ void __cpuinit cpu_init(void)dbg_restore_debug_regs ();fpu_init (); -xsave_init ();raw_local_save_flags (kernel_eflags);
@@ -1352,6 +1351,5 @@ void __cpuinit cpu_init(void)dbg_restore_debug_regs (); fpu_init(); - xsave_init(); }#endif
arch/x86/kernel/i387.c +8 -17
@@ -22,9 +22,8 @@/* * Were we in an interrupt that interrupted kernel mode? * -* For now, on xsave platforms we will return interrupted -* kernel FPU as not-idle. TBD: As we use non-lazy FPU restore -* for xsave platforms, ideally we can change the return value +* For now, with eagerfpu we will return interrupted kernel FPU +* state as not-idle. TBD: Ideally we can change the return value * to something like __thread_has_fpu(current). But we need to * be careful of doing __thread_clear_has_fpu() before saving * the FPU etc for supporting nested uses etc. For now, take
@@ -38,7 +37,7 @@*/ static inline bool interrupted_kernel_fpu_idle (void ) { -if (use_xsave ()) +if (use_eager_fpu ())return 0 ; return !__thread_has_fpu(current) &&
@@ -84,7 +83,7 @@ void kernel_fpu_begin(void)__save_init_fpu (me);__thread_clear_has_fpu (me);/* We do 'stts()' in kernel_fpu_end() */ - }else if (!use_xsave ()) { + }else if (!use_eager_fpu ()) {this_cpu_write (fpu_owner_task,NULL );clts (); }
@@ -93,7 +92,7 @@ EXPORT_SYMBOL(kernel_fpu_begin);void kernel_fpu_end (void ) { -if (use_xsave ()) +if (use_eager_fpu ())math_state_restore ();else stts ();
@@ -122,7 +121,6 @@ static void __cpuinit mxcsr_feature_mask_init(void) {unsigned long mask =0 ; -clts ();if (cpu_has_fxsr) {memset (&fx_scratch,0 ,sizeof (struct i387_fxsave_struct )); asmvolatile ("fxsave %0" : :"m" (fx_scratch));
@@ -131,7 +129,6 @@ static void __cpuinit mxcsr_feature_mask_init(void) mask =0x0000ffbf ; } mxcsr_feature_mask &= mask; -stts (); }static void __cpuinit init_thread_xstate (void )
@@ -185,9 +182,8 @@ void __cpuinit fpu_init(void)init_thread_xstate ();mxcsr_feature_mask_init (); -/* clean state in init */ -current_thread_info ()->status =0 ; -clear_used_math (); +xsave_init (); +eager_fpu_init (); }void fpu_finit (struct fpu *fpu)
@@ -198,12 +194,7 @@ void fpu_finit(struct fpu *fpu) }if (cpu_has_fxsr) { -struct i387_fxsave_struct *fx = &fpu->state->fxsave; - -memset (fx,0 , xstate_size); - fx->cwd =0x37f ; -if (cpu_has_xmm) - fx->mxcsr =MXCSR_DEFAULT ; +fx_finit (&fpu->state->fxsave); }else {struct i387_fsave_struct *fp = &fpu->state->fsave;memset (fp,0 , xstate_size);
arch/x86/kernel/process.c +1 -1
@@ -156,7 +156,7 @@ void flush_thread(void)* Free the FPU state for non xsave platforms. They get reallocated * lazily at the first use. */ -if (!use_xsave ()) +if (!use_eager_fpu ())free_thread_xstate (tsk); }
arch/x86/kernel/traps.c +1 -1
@@ -630,7 +630,7 @@ EXPORT_SYMBOL_GPL(math_state_restore); dotraplinkage void __kprobesdo_device_not_available (struct pt_regs *regs,long error_code) { -BUG_ON (use_xsave ()); +BUG_ON (use_eager_fpu ());#ifdef CONFIG_MATH_EMULATION if (read_cr0 () &X86_CR0_EM ) {
arch/x86/kernel/xsave.c +57 -30
137 lines not on this page · raw message
PATCH v2 2/5 x86, fpu: enable eagerfpu by default for xsaveopt
In reply to Suresh Siddhaxsaveopt/xrstor support optimized state save/restore by tracking the INIT state and MODIFIED state during context-switch.
Enable eagerfpu by default for processors supporting xsaveopt. Can be disabled by passing "eagerfpu=off" boot parameter.
--- Documentation/kernel-parameters.txt | 2 +- arch/x86/include/asm/cpufeature.h | 1 + arch/x86/kernel/xsave.c | 3 +++ 3 files changed, 5 insertions(+), 1 deletions(-)
Documentation/kernel-parameters.txt +1 -1
@@ -1835,7 +1835,7 @@ bytes respectively. Such letter suffixes can also be entirely omitted. enabling legacy floating-point and sse state. eagerfpu= [X86] - on enable eager fpu restore + on enable eager fpu restore (default for xsaveopt) off disable eager fpu restore nohlt [BUGS=ARM,SH] Tells the kernel that the sleep(SH) or
arch/x86/include/asm/cpufeature.h +1 -0
@@ -298,6 +298,7 @@ extern const char * const x86_power_flags[32];#define cpu_has_xmm4_2 boot_cpu_has(X86_FEATURE_XMM4_2)#define cpu_has_x2apic boot_cpu_has(X86_FEATURE_X2APIC)#define cpu_has_xsave boot_cpu_has(X86_FEATURE_XSAVE) +#define cpu_has_xsaveopt boot_cpu_has(X86_FEATURE_XSAVEOPT)#define cpu_has_osxsave boot_cpu_has(X86_FEATURE_OSXSAVE)#define cpu_has_hypervisor boot_cpu_has(X86_FEATURE_HYPERVISOR)#define cpu_has_pclmulqdq boot_cpu_has(X86_FEATURE_PCLMULQDQ)
arch/x86/kernel/xsave.c +3 -0
@@ -556,6 +556,9 @@ static void __init xstate_enable_boot_cpu(void)prepare_fx_sw_frame ();setup_init_fpu_buf (); +if (cpu_has_xsaveopt && !disable_eagerfpu) +setup_force_cpu_cap (X86_FEATURE_EAGER_FPU ); +pr_info ("enabled xstate_bv 0x%llx, cntxt size 0x%x\n" , pcntxt_mask, xstate_size); }
Signature · 1 line
1.7.6.5
[tip:x86/fpu] x86, fpu: enable eagerfpu by default for xsaveopt
Commit-ID: 212b02125f3725127148475059a70031bd031bdb Gitweb: http://git.kernel.org/tip/212b02125f3725127148475059a70031bd031bdb Author: Suresh Siddha <[email protected]> AuthorDate: Thu, 6 Sep 2012 15:05:18 -0700 Committer: H. Peter Anvin <[email protected]> CommitDate: Tue, 18 Sep 2012 15:52:23 -0700
x86, fpu: enable eagerfpu by default for xsaveopt
xsaveopt/xrstor support optimized state save/restore by tracking the INIT state and MODIFIED state during context-switch.
Enable eagerfpu by default for processors supporting xsaveopt. Can be disabled by passing "eagerfpu=off" boot parameter.
--- Documentation/kernel-parameters.txt | 2 +- arch/x86/include/asm/cpufeature.h | 1 + arch/x86/kernel/xsave.c | 3 +++ 3 files changed, 5 insertions(+), 1 deletions(-)
Documentation/kernel-parameters.txt +1 -1
@@ -1834,7 +1834,7 @@ bytes respectively. Such letter suffixes can also be entirely omitted. enabling legacy floating-point and sse state. eagerfpu= [X86] - on enable eager fpu restore + on enable eager fpu restore (default for xsaveopt) off disable eager fpu restore nohlt [BUGS=ARM,SH] Tells the kernel that the sleep(SH) or
arch/x86/include/asm/cpufeature.h +1 -0
@@ -300,6 +300,7 @@ extern const char * const x86_power_flags[32];#define cpu_has_xmm4_2 boot_cpu_has(X86_FEATURE_XMM4_2)#define cpu_has_x2apic boot_cpu_has(X86_FEATURE_X2APIC)#define cpu_has_xsave boot_cpu_has(X86_FEATURE_XSAVE) +#define cpu_has_xsaveopt boot_cpu_has(X86_FEATURE_XSAVEOPT)#define cpu_has_osxsave boot_cpu_has(X86_FEATURE_OSXSAVE)#define cpu_has_hypervisor boot_cpu_has(X86_FEATURE_HYPERVISOR)#define cpu_has_pclmulqdq boot_cpu_has(X86_FEATURE_PCLMULQDQ)
arch/x86/kernel/xsave.c +3 -0
@@ -557,6 +557,9 @@ static void __init xstate_enable_boot_cpu(void)prepare_fx_sw_frame ();setup_init_fpu_buf (); +if (cpu_has_xsaveopt && !disable_eagerfpu) +setup_force_cpu_cap (X86_FEATURE_EAGER_FPU ); +pr_info ("enabled xstate_bv 0x%llx, cntxt size 0x%x\n" , pcntxt_mask, xstate_size); }
PATCH v2 3/5 x86, fpu: move check_fpu() after alternative_instructions()
In reply to Suresh Siddhause_eager_fpu() in kernel_fpu_begin/end() relies on the patched alternative instructions. So move check_fpu() which uses the kernel_fpu_begin/end() after alternative_instructions().
--- arch/x86/kernel/cpu/bugs.c | 7 ++++++- 1 files changed, 6 insertions(+), 1 deletions(-)
arch/x86/kernel/cpu/bugs.c +6 -1
@@ -165,10 +165,15 @@ void __init check_bugs(void)print_cpu_info (&boot_cpu_data);#endif check_config (); -check_fpu ();check_hlt ();check_popad ();init_utsname ()->machine[1 ] ='0' + (boot_cpu_data.x86 >6 ?6 : boot_cpu_data.x86);alternative_instructions (); + +/* +* kernel_fpu_begin/end() in check_fpu() relies on the patched +* alternative instructions. +*/ +check_fpu (); }
Signature · 1 line
1.7.6.5
[tip:x86/fpu] x86, fpu: use non-lazy fpu restore for processors supporting xsave
Commit-ID: 304bceda6a18ae0b0240b8aac9a6bdf8ce2d2469 Gitweb: http://git.kernel.org/tip/304bceda6a18ae0b0240b8aac9a6bdf8ce2d2469 Author: Suresh Siddha <[email protected]> AuthorDate: Fri, 24 Aug 2012 14:13:02 -0700 Committer: H. Peter Anvin <[email protected]> CommitDate: Tue, 18 Sep 2012 15:52:11 -0700
x86, fpu: use non-lazy fpu restore for processors supporting xsave
Fundamental model of the current Linux kernel is to lazily init and restore FPU instead of restoring the task state during context switch. This changes that fundamental lazy model to the non-lazy model for the processors supporting xsave feature.
Reasons driving this model change are:
i. Newer processors support optimized state save/restore using xsaveopt and xrstor by tracking the INIT state and MODIFIED state during context-switch. This is faster than modifying the cr0.TS bit which has serializing semantics.
ii. Newer glibc versions use SSE for some of the optimized copy/clear routines. With certain workloads (like boot, kernel-compilation etc), application completes its work with in the first 5 task switches, thus taking upto 5 #DNA traps with the kernel not getting a chance to apply the above mentioned pre-load heuristic.
iii. Some xstate features (like AMD's LWP feature) don't honor the cr0.TS bit and thus will not work correctly in the presence of lazy restore. Non-lazy state restore is needed for enabling such features.
Some data on a two socket SNB system:
* Saved 20K DNA exceptions during boot on a two socket SNB system.
* Saved 50K DNA exceptions during kernel-compilation workload.
* Improved throughput of the AVX based checksumming function inside the kernel by ~15% as xsave/xrstor is faster than the serializing clts/stts pair.
Also now kernel_fpu_begin/end() relies on the patched alternative instructions. So move check_fpu() which uses the kernel_fpu_begin/end() after alternative_instructions().
Merge 32-bit boot fix from,
--- arch/x86/include/asm/fpu-internal.h | 96 +++++++++++++++++++++++------------ arch/x86/include/asm/i387.h | 1 + arch/x86/include/asm/xsave.h | 1 + arch/x86/kernel/cpu/bugs.c | 7 ++- arch/x86/kernel/i387.c | 20 ++++++- arch/x86/kernel/process.c | 12 +++-- arch/x86/kernel/process_32.c | 4 -- arch/x86/kernel/process_64.c | 4 -- arch/x86/kernel/traps.c | 5 ++- arch/x86/kernel/xsave.c | 57 +++++++++++++++++---- 10 files changed, 146 insertions(+), 61 deletions(-)
arch/x86/include/asm/fpu-internal.h +63 -33
@@ -291,15 +291,48 @@ static inline void __thread_set_has_fpu(struct task_struct *tsk)static inline void __thread_fpu_end (struct task_struct *tsk) {__thread_clear_has_fpu (tsk); -stts (); +if (!use_xsave ()) +stts (); }static inline void __thread_fpu_begin (struct task_struct *tsk) { -clts (); +if (!use_xsave ()) +clts ();__thread_set_has_fpu (tsk); } +static inline void __drop_fpu (struct task_struct *tsk) +{ +if (__thread_has_fpu (tsk)) { +/* Ignore delayed exceptions from user space */ + asmvolatile ("1: fwait\n" +"2:\n" + _ASM_EXTABLE(1b, 2b)); +__thread_fpu_end (tsk); + } +} + +static inline void drop_fpu (struct task_struct *tsk) +{ +/* +* Forget coprocessor state.. +*/ +preempt_disable (); + tsk->fpu_counter =0 ; +__drop_fpu (tsk); +clear_used_math (); +preempt_enable (); +} + +static inline void drop_init_fpu (struct task_struct *tsk) +{ +if (!use_xsave ()) +drop_fpu (tsk); +else +xrstor_state (init_xstate_buf,-1 ); +} +/* * FPU state switching for scheduling. *
@@ -333,7 +366,12 @@ static inline fpu_switch_t switch_fpu_prepare(struct task_struct *old, struct ta {fpu_switch_t fpu; - fpu.preload =tsk_used_math (new) && new->fpu_counter >5 ; +/* +* If the task has used the math, pre-load the FPU on xsave processors +* or if the past 5 consecutive context-switches used math. +*/ + fpu.preload =tsk_used_math (new) && (use_xsave () || + new->fpu_counter >5 );if (__thread_has_fpu (old)) {if (!__save_init_fpu (old)) cpu = ~0 ;
@@ -345,14 +383,14 @@ static inline fpu_switch_t switch_fpu_prepare(struct task_struct *old, struct ta new->fpu_counter++;__thread_set_has_fpu (new);prefetch (new->thread.fpu.state); - }else + }else if (!use_xsave ())stts (); }else { old->fpu_counter =0 ; old->thread.fpu.last_cpu = ~0 ;if (fpu.preload) { new->fpu_counter++; -if (fpu_lazy_restore (new, cpu)) +if (!use_xsave () &&fpu_lazy_restore (new, cpu)) fpu.preload =0 ;else prefetch (new->thread.fpu.state);
@@ -372,7 +410,7 @@ static inline void switch_fpu_finish(struct task_struct *new, fpu_switch_t fpu) {if (fpu.preload) {if (unlikely (restore_fpu_checking (new))) -__thread_fpu_end (new); +drop_init_fpu (new); } }
@@ -400,17 +438,6 @@ static inline int restore_xstate_sig(void __user *buf, int ia32_frame)return __restore_xstate_sig (buf, buf_fx, size); } -static inline void __drop_fpu (struct task_struct *tsk) -{ -if (__thread_has_fpu (tsk)) { -/* Ignore delayed exceptions from user space */ - asmvolatile ("1: fwait\n" -"2:\n" - _ASM_EXTABLE(1b, 2b)); -__thread_fpu_end (tsk); - } -} -/* * Need to be preemption-safe. *
@@ -431,24 +458,18 @@ static inline void user_fpu_begin(void)static inline void save_init_fpu (struct task_struct *tsk) {WARN_ON_ONCE (!__thread_has_fpu (tsk)); + +if (use_xsave ()) { +xsave_state (&tsk->thread.fpu.state->xsave,-1 ); +return ; + } +preempt_disable ();__save_init_fpu (tsk);__thread_fpu_end (tsk);preempt_enable (); } -static inline void drop_fpu (struct task_struct *tsk) -{ -/* -* Forget coprocessor state.. -*/ - tsk->fpu_counter =0 ; -preempt_disable (); -__drop_fpu (tsk); -preempt_enable (); -clear_used_math (); -} -/* * i387 state interaction */
@@ -503,12 +524,21 @@ static inline void fpu_free(struct fpu *fpu) } } -static inline void fpu_copy (struct fpu *dst,struct fpu *src) +static inline void fpu_copy (struct task_struct *dst,struct task_struct *src) { -memcpy (dst->state, src->state, xstate_size); -} +if (use_xsave ()) { +struct xsave_struct *xsave = &dst->thread.fpu.state->xsave; -extern void fpu_finit (struct fpu *fpu); +memset (&xsave->xsave_hdr,0 ,sizeof (struct xsave_hdr_struct )); +xsave_state (xsave,-1 ); + }else { +struct fpu *dfpu = &dst->thread.fpu; +struct fpu *sfpu = &src->thread.fpu; + +unlazy_fpu (src); +memcpy (dfpu->state, sfpu->state, xstate_size); + } +} static inline unsigned long alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx,
arch/x86/include/asm/i387.h +1 -0
@@ -19,6 +19,7 @@ struct pt_regs;struct user_i387_struct ;extern int init_fpu (struct task_struct *child); +extern void fpu_finit (struct fpu *fpu);extern int dump_fpu (struct pt_regs *,struct user_i387_struct *);extern void math_state_restore (void );
arch/x86/include/asm/xsave.h +1 -0
@@ -34,6 +34,7 @@extern unsigned int xstate_size;extern u64 pcntxt_mask;extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS ]; +extern struct xsave_struct *init_xstate_buf;extern void xsave_init (void );extern void update_regset_xstate_info (unsigned int size,u64 xstate_mask);
arch/x86/kernel/cpu/bugs.c +6 -1
@@ -165,10 +165,15 @@ void __init check_bugs(void)print_cpu_info (&boot_cpu_data);#endif check_config (); -check_fpu ();check_hlt ();check_popad ();init_utsname ()->machine[1 ] ='0' + (boot_cpu_data.x86 >6 ?6 : boot_cpu_data.x86);alternative_instructions (); + +/* +* kernel_fpu_begin/end() in check_fpu() relies on the patched +* alternative instructions. +*/ +check_fpu (); }
arch/x86/kernel/i387.c +17 -3
@@ -22,7 +22,15 @@/* * Were we in an interrupt that interrupted kernel mode? * -* We can do a kernel_fpu_begin/end() pair *ONLY* if that +* For now, on xsave platforms we will return interrupted +* kernel FPU as not-idle. TBD: As we use non-lazy FPU restore +* for xsave platforms, ideally we can change the return value +* to something like __thread_has_fpu(current). But we need to +* be careful of doing __thread_clear_has_fpu() before saving +* the FPU etc for supporting nested uses etc. For now, take +* the simple route! +* +* On others, we can do a kernel_fpu_begin/end() pair *ONLY* if that * pair does nothing at all: the thread must not have fpu (so * that we don't try to save the FPU state), and TS must * be set (so that the clts/stts pair does nothing that is
@@ -30,6 +38,9 @@*/ static inline bool interrupted_kernel_fpu_idle (void ) { +if (use_xsave ()) +return 0 ; +return !__thread_has_fpu (current) && (read_cr0 () &X86_CR0_TS ); }
@@ -73,7 +84,7 @@ void kernel_fpu_begin(void)__save_init_fpu (me);__thread_clear_has_fpu (me);/* We do 'stts()' in kernel_fpu_end() */ - }else { + }else if (!use_xsave ()) {this_cpu_write (fpu_owner_task,NULL );clts (); }
@@ -82,7 +93,10 @@ EXPORT_SYMBOL(kernel_fpu_begin);void kernel_fpu_end (void ) { -stts (); +if (use_xsave ()) +math_state_restore (); +else +stts ();preempt_enable (); }EXPORT_SYMBOL (kernel_fpu_end);
arch/x86/kernel/process.c +8 -4
@@ -66,15 +66,13 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) {int ret; -unlazy_fpu (src); - *dst = *src;if (fpu_allocated (&src->thread.fpu)) {memset (&dst->thread.fpu,0 ,sizeof (dst->thread.fpu)); ret =fpu_alloc (&dst->thread.fpu);if (ret)return ret; -fpu_copy (&dst->thread.fpu, &src->thread.fpu); +fpu_copy (dst, src); }return 0 ; }
@@ -153,7 +151,13 @@ void flush_thread(void)flush_ptrace_hw_breakpoint (tsk);memset (tsk->thread.tls_array,0 ,sizeof (tsk->thread.tls_array)); -drop_fpu (tsk); +drop_init_fpu (tsk); +/* +* Free the FPU state for non xsave platforms. They get reallocated +* lazily at the first use. +*/ +if (!use_xsave ()) +free_thread_xstate (tsk); }static void hard_disable_TSC (void )
arch/x86/kernel/process_32.c +0 -4
@@ -190,10 +190,6 @@ start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp) regs->cs = __USER_CS; regs->ip = new_ip; regs->sp = new_sp; -/* -* Free the old FP and other extended state -*/ -free_thread_xstate (current); }EXPORT_SYMBOL_GPL (start_thread);
arch/x86/kernel/process_64.c +0 -4
@@ -232,10 +232,6 @@ start_thread_common(struct pt_regs *regs, unsigned long new_ip, regs->cs = _cs; regs->ss = _ss; regs->flags =X86_EFLAGS_IF ; -/* -* Free the old FP and other extended state -*/ -free_thread_xstate (current); }void
arch/x86/kernel/traps.c +4 -1
@@ -613,11 +613,12 @@ void math_state_restore(void) }__thread_fpu_begin (tsk); +/* * Paranoid restore. send a SIGSEGV if we fail to restore the state. */ if (unlikely (restore_fpu_checking (tsk))) { -__thread_fpu_end (tsk); +drop_init_fpu (tsk);force_sig (SIGSEGV , tsk);return ; }
@@ -629,6 +630,8 @@ EXPORT_SYMBOL_GPL(math_state_restore); dotraplinkage void __kprobesdo_device_not_available (struct pt_regs *regs,long error_code) { +BUG_ON (use_xsave ()); +#ifdef CONFIG_MATH_EMULATION if (read_cr0 () &X86_CR0_EM ) {struct math_emu_info info = { };
arch/x86/kernel/xsave.c +46 -11
126 lines not on this page · raw message
PATCH v2 4/5 x86, fpu: make eagerfpu= boot param tri-state
In reply to Suresh SiddhaAdd the "eagerfpu=auto" (that selects the default scheme in enabling eagerfpu) which can override compiled-in boot parameters like "eagerfpu=on/off" (that force enable/disable eagerfpu).
--- Documentation/kernel-parameters.txt | 4 +++- arch/x86/kernel/xsave.c | 17 ++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-)
Documentation/kernel-parameters.txt +3 -1
@@ -1835,8 +1835,10 @@ bytes respectively. Such letter suffixes can also be entirely omitted. enabling legacy floating-point and sse state. eagerfpu= [X86] - on enable eager fpu restore (default for xsaveopt) + on enable eager fpu restore off disable eager fpu restore + auto selects the default scheme, which automatically + enables eagerfpu restore for xsaveopt. nohlt [BUGS=ARM,SH] Tells the kernel that the sleep(SH) or wfi(ARM) instruction doesn't work correctly and not to
arch/x86/kernel/xsave.c +12 -5
@@ -507,13 +507,15 @@ static void __init setup_init_fpu_buf(void)xsave_state (init_xstate_buf,-1 ); } -static int disable_eagerfpu; +static enum {AUTO ,ENABLE ,DISABLE } eagerfpu =AUTO ;static int __init eager_fpu_setup (char *s) {if (!strcmp (s,"on" )) -setup_force_cpu_cap (X86_FEATURE_EAGER_FPU ); + eagerfpu =ENABLE ;else if (!strcmp (s,"off" )) - disable_eagerfpu =1 ; + eagerfpu =DISABLE ; +else if (!strcmp (s,"auto" )) + eagerfpu =AUTO ;return 1 ; }__setup ("eagerfpu=" , eager_fpu_setup);
@@ -556,8 +558,9 @@ static void __init xstate_enable_boot_cpu(void)prepare_fx_sw_frame ();setup_init_fpu_buf (); -if (cpu_has_xsaveopt && !disable_eagerfpu) -setup_force_cpu_cap (X86_FEATURE_EAGER_FPU ); +/* Auto enable eagerfpu for xsaveopt */ +if (cpu_has_xsaveopt && eagerfpu !=DISABLE ) + eagerfpu =ENABLE ;pr_info ("enabled xstate_bv 0x%llx, cntxt size 0x%x\n" , pcntxt_mask, xstate_size);
@@ -597,6 +600,10 @@ void __cpuinit eager_fpu_init(void)clear_used_math ();current_thread_info ()->status =0 ; + +if (eagerfpu ==ENABLE ) +setup_force_cpu_cap (X86_FEATURE_EAGER_FPU ); +if (!cpu_has_eager_fpu) {stts ();return ;
Signature · 1 line
1.7.6.5
[tip:x86/fpu] x86, fpu: make eagerfpu= boot param tri-state
Commit-ID: e00229819f306b1f86134095347e9187dc346bd1 Gitweb: http://git.kernel.org/tip/e00229819f306b1f86134095347e9187dc346bd1 Author: Suresh Siddha <[email protected]> AuthorDate: Mon, 10 Sep 2012 10:32:32 -0700 Committer: H. Peter Anvin <[email protected]> CommitDate: Tue, 18 Sep 2012 15:52:24 -0700
x86, fpu: make eagerfpu= boot param tri-state
Add the "eagerfpu=auto" (that selects the default scheme in enabling eagerfpu) which can override compiled-in boot parameters like "eagerfpu=on/off" (that force enable/disable eagerfpu).
--- Documentation/kernel-parameters.txt | 4 +++- arch/x86/kernel/xsave.c | 17 ++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-)
Documentation/kernel-parameters.txt +3 -1
@@ -1834,8 +1834,10 @@ bytes respectively. Such letter suffixes can also be entirely omitted. enabling legacy floating-point and sse state. eagerfpu= [X86] - on enable eager fpu restore (default for xsaveopt) + on enable eager fpu restore off disable eager fpu restore + auto selects the default scheme, which automatically + enables eagerfpu restore for xsaveopt. nohlt [BUGS=ARM,SH] Tells the kernel that the sleep(SH) or wfi(ARM) instruction doesn't work correctly and not to
arch/x86/kernel/xsave.c +12 -5
@@ -508,13 +508,15 @@ static void __init setup_init_fpu_buf(void)xsave_state (init_xstate_buf,-1 ); } -static int disable_eagerfpu; +static enum {AUTO ,ENABLE ,DISABLE } eagerfpu =AUTO ;static int __init eager_fpu_setup (char *s) {if (!strcmp (s,"on" )) -setup_force_cpu_cap (X86_FEATURE_EAGER_FPU ); + eagerfpu =ENABLE ;else if (!strcmp (s,"off" )) - disable_eagerfpu =1 ; + eagerfpu =DISABLE ; +else if (!strcmp (s,"auto" )) + eagerfpu =AUTO ;return 1 ; }__setup ("eagerfpu=" , eager_fpu_setup);
@@ -557,8 +559,9 @@ static void __init xstate_enable_boot_cpu(void)prepare_fx_sw_frame ();setup_init_fpu_buf (); -if (cpu_has_xsaveopt && !disable_eagerfpu) -setup_force_cpu_cap (X86_FEATURE_EAGER_FPU ); +/* Auto enable eagerfpu for xsaveopt */ +if (cpu_has_xsaveopt && eagerfpu !=DISABLE ) + eagerfpu =ENABLE ;pr_info ("enabled xstate_bv 0x%llx, cntxt size 0x%x\n" , pcntxt_mask, xstate_size);
@@ -598,6 +601,10 @@ void __cpuinit eager_fpu_init(void)clear_used_math ();current_thread_info ()->status =0 ; + +if (eagerfpu ==ENABLE ) +setup_force_cpu_cap (X86_FEATURE_EAGER_FPU ); +if (!cpu_has_eager_fpu) {stts ();return ;
PATCH v2 5/5 x86, fpu: remove cpu_has_xmm check in the fx_finit()
In reply to Suresh SiddhaCPUs with FXSAVE but no XMM/MXCSR (Pentium II from Intel, Crusoe/TM-3xxx/5xxx from Transmeta, and presumably some of the K6 generation from AMD) ever looked at the mxcsr field during fxrstor/fxsave. So remove the cpu_has_xmm check in the fx_finit()
--- arch/x86/include/asm/fpu-internal.h | 3 +-- 1 files changed, 1 insertions(+), 2 deletions(-)
arch/x86/include/asm/fpu-internal.h +1 -2
@@ -109,8 +109,7 @@ static inline void fx_finit(struct i387_fxsave_struct *fx) {memset (fx,0 , xstate_size); fx->cwd =0x37f ; -if (cpu_has_xmm) - fx->mxcsr =MXCSR_DEFAULT ; + fx->mxcsr =MXCSR_DEFAULT ; }extern void __sanitize_i387_state (struct task_struct *);
Signature · 1 line
1.7.6.5
[tip:x86/fpu] x86, fpu: remove cpu_has_xmm check in the fx_finit()
Commit-ID: a8615af4bc3621cb01096541dafa6f68352ec2d9 Gitweb: http://git.kernel.org/tip/a8615af4bc3621cb01096541dafa6f68352ec2d9 Author: Suresh Siddha <[email protected]> AuthorDate: Mon, 10 Sep 2012 10:40:08 -0700 Committer: H. Peter Anvin <[email protected]> CommitDate: Tue, 18 Sep 2012 15:52:24 -0700
x86, fpu: remove cpu_has_xmm check in the fx_finit()
CPUs with FXSAVE but no XMM/MXCSR (Pentium II from Intel, Crusoe/TM-3xxx/5xxx from Transmeta, and presumably some of the K6 generation from AMD) ever looked at the mxcsr field during fxrstor/fxsave. So remove the cpu_has_xmm check in the fx_finit()
--- arch/x86/include/asm/fpu-internal.h | 3 +-- 1 files changed, 1 insertions(+), 2 deletions(-)
arch/x86/include/asm/fpu-internal.h +1 -2
@@ -109,8 +109,7 @@ static inline void fx_finit(struct i387_fxsave_struct *fx) {memset (fx,0 , xstate_size); fx->cwd =0x37f ; -if (cpu_has_xmm) - fx->mxcsr =MXCSR_DEFAULT ; + fx->mxcsr =MXCSR_DEFAULT ; }extern void __sanitize_i387_state (struct task_struct *);
Re: [PATCH v2 0/5] eagerfpu patches for tip/x86/fpu
In reply to Suresh SiddhaOn Mon, Sep 10, 2012 at 11:11:00AM -0700, Suresh Siddha wrote:
+ Andre.
Quoted 30 lines
v2 version of the previous patchset https://lkml.org/lkml/2012/9/7/548
that decouples eagerfpu into a synthetic cpuid feature.changes from v1:
* Made "eagerfpu=" boot parameter tri-state
* removed thecpu_has_xmmcheck in thefx_finit(not related to eagerfpu per-say, but this patchset touches that area of code).Suresh Siddha (5):
x86, fpu: decouple non-lazy/eager fpu restore from xsave x86, fpu: enable eagerfpu by default for xsaveopt x86, fpu: movecheck_fpu()afteralternative_instructions()x86, fpu: make eagerfpu= boot param tri-state x86, fpu: removecpu_has_xmmcheck in thefx_finit()Documentation/kernel-parameters.txt | 6 ++ arch/x86/include/asm/cpufeature.h | 3 + arch/x86/include/asm/fpu-internal.h | 53 ++++++++++++++------ arch/x86/kernel/cpu/bugs.c | 7 ++- arch/x86/kernel/cpu/common.c | 2 - arch/x86/kernel/i387.c | 25 +++------ arch/x86/kernel/process.c | 2 +- arch/x86/kernel/traps.c | 2 +- arch/x86/kernel/xsave.c | 95 ++++++++++++++++++++++++----------- 9 files changed, 129 insertions(+), 66 deletions(-)Signature · 1 line
1.7.6.5
Signature · 8 lines
Regards/Gruss, Boris. Advanced Micro Devices GmbH Einsteinring 24, 85609 Dornach GM: Alberto Bozzo Reg: Dornach, Landkreis Muenchen HRB Nr. 43632 WEEE Registernr: 129 19551