arm64/fpsimd: Don't disable softirq when touching FPSIMD/SVE state
v2 cover arm64/fpsimd: Don't disable softirq when touching FPSIMD/SVE state
Hi all,
This patch series keep softirqs enabled while touching FPSIMD/SVE state. For more details on the impact see patch #3.
This patch series has been benchmarked on Linux 5.1-rc4 with defconfig.
On Juno2:
* hackbench 100 process 1000 (10 times)
* .7% quicker
On ThunderX 2:
* hackbench 1000 process 1000 (20 times)
* 3.4% quicker
Note that while the benchmark has been done on 5.1-rc4, the patch series is based on kvm-arm/next as it has few conflicts with the SVE KVM series.
Cheers,
Julien Grall (3):
arm64/fpsimd: Remove the prototype for sve_flush_cpu_state() arch/arm64: fpsimd: Introduce fpsimd_save_and_flush_cpu_state() and
use it
arm64/fpsimd: Don't disable softirq when touching FPSIMD/SVE state
arch/arm64/include/asm/fpsimd.h | 6 +- arch/arm64/include/asm/simd.h | 4 +- arch/arm64/kernel/fpsimd.c | 148 +++++++++++++++++++++++++++++----------- arch/arm64/kvm/fpsimd.c | 3 +- 4 files changed, 112 insertions(+), 49 deletions(-)
Signature · 1 line
2.11.0
PATCH v2 1/3 arm64/fpsimd: Remove the prototype for sve_flush_cpu_state()
The function sve_flush_cpu_state() has been removed in commit 21cdd7fd76e3 "KVM: arm64: Remove eager host SVE state saving".
So remove the associated prototype in asm/fpsimd.h.
---
Changes in v2:
- Patch added
--- arch/arm64/include/asm/fpsimd.h | 1 - 1 file changed, 1 deletion(-)
arch/arm64/include/asm/fpsimd.h +0 -1
@@ -64,7 +64,6 @@ extern void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *state,extern void fpsimd_flush_task_state (struct task_struct *target);extern void fpsimd_flush_cpu_state (void ); -extern void sve_flush_cpu_state (void );/* Maximum VL that SVE VL-agnostic software can transparently support */ #define SVE_VL_ARCH_MAX 0x100
Signature · 1 line
2.11.0
Re: [PATCH v2 1/3] arm64/fpsimd: Remove the prototype for sve_flush_cpu_state()
Quoted 6 lines · Julien Grall
The function
sve_flush_cpu_state()has been removed in commit21cdd7fd76e3"KVM: arm64: Remove eager host SVE state saving".So remove the associated prototype in
asm/fpsimd.h.Signed-off-by Julien Grall <[email protected]>
Good spot, thanks.
Quoted 28 lines
---
Changes in v2:
- Patch added--- arch/arm64/include/asm/fpsimd.h | 1 - 1 file changed, 1 deletion(-)arch/arm64/include/asm/fpsimd.h +0
-1@@ -64,7 +64,6 @@ extern void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *state,extern void fpsimd_flush_task_state (struct task_struct *target);extern void fpsimd_flush_cpu_state (void ); -extern void sve_flush_cpu_state (void );/* Maximum VL that SVE VL-agnostic software can transparently support */ #define SVE_VL_ARCH_MAX 0x100Signature · 7 lines
2.11.0 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v2 2/3 arch/arm64: fpsimd: Introduce fpsimd_save_and_flush_cpu_state() and use it
In reply to Julien GrallThe only external user of fpsimd_save() and fpsimd_flush_cpu_state() is the KVM FPSIMD code.
A follow-up patch will mandate hold of the FPSIMD context while modifying it. Rather than having to export the new helpers to get/put the context, we can just introduce a new function to combine fpsimd_save() and fpsimd_flush_cpu_state().
This has also the advantage to remove any external call of fpsimd_save() and fpsimd_flush_cpu_state(), so they can be turned static.
Lastly, the new function can also be used in the PM notifier.
---
Changes in v2:
- Patch added
--- arch/arm64/include/asm/fpsimd.h | 5 ++--- arch/arm64/kernel/fpsimd.c | 17 +++++++++++++---- arch/arm64/kvm/fpsimd.c | 3 +-- 3 files changed, 16 insertions(+), 9 deletions(-)
arch/arm64/include/asm/fpsimd.h +2 -3
@@ -48,11 +48,11 @@ struct task_struct;extern void fpsimd_save_state (struct user_fpsimd_state *state);extern void fpsimd_load_state (struct user_fpsimd_state *state); -extern void fpsimd_save (void ); -extern void fpsimd_thread_switch (struct task_struct *next);extern void fpsimd_flush_thread (void ); +extern void fpsimd_save_and_flush_cpu_state (void ); +extern void fpsimd_signal_preserve_current_state (void );extern void fpsimd_preserve_current_state (void );extern void fpsimd_restore_current_state (void );
@@ -63,7 +63,6 @@ extern void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *state, void *sve_state, unsigned int sve_vl);extern void fpsimd_flush_task_state (struct task_struct *target); -extern void fpsimd_flush_cpu_state (void );/* Maximum VL that SVE VL-agnostic software can transparently support */ #define SVE_VL_ARCH_MAX 0x100
arch/arm64/kernel/fpsimd.c +13 -4
@@ -241,7 +241,7 @@ static void task_fpsimd_load(void)* * Softirqs (and preemption) must be disabled. */ -void fpsimd_save (void ) +static void fpsimd_save (void ) {struct fpsimd_last_state_struct const *last =this_cpu_ptr (&fpsimd_last_state);
@@ -1117,12 +1117,22 @@ void fpsimd_flush_task_state(struct task_struct *t)* Invalidate any task's FPSIMD state that is present on this cpu. * This function must be called with softirqs disabled. */ -void fpsimd_flush_cpu_state (void ) +static void fpsimd_flush_cpu_state (void ) {__this_cpu_write (fpsimd_last_state.st,NULL );set_thread_flag (TIF_FOREIGN_FPSTATE ); } +/* +* Save the FPSIMD state to memory and invalidate cpu view. +* This function must be called with softirqs (and preemption) disabled. +*/ +void fpsimd_save_and_flush_cpu_state (void ) +{ +fpsimd_save (); +fpsimd_flush_cpu_state (); +} +#ifdef CONFIG_KERNEL_MODE_NEON DEFINE_PER_CPU (bool, kernel_neon_busy);
@@ -1279,8 +1289,7 @@ static int fpsimd_cpu_pm_notifier(struct notifier_block *self, {switch (cmd) {case CPU_PM_ENTER : -fpsimd_save (); -fpsimd_flush_cpu_state (); +fpsimd_save_and_flush_cpu_state ();break ;case CPU_PM_EXIT :break ;
arch/arm64/kvm/fpsimd.c +1 -2
@@ -113,8 +113,7 @@ void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)u64 *guest_zcr = &vcpu->arch.ctxt.sys_regs[ZCR_EL1 ];/* Clean guest FP state to memory and invalidate cpu view */ -fpsimd_save (); -fpsimd_flush_cpu_state (); +fpsimd_save_and_flush_cpu_state ();if (guest_has_sve) *guest_zcr =read_sysreg_s (SYS_ZCR_EL12 );
Signature · 1 line
2.11.0
Re: [PATCH v2 2/3] arch/arm64: fpsimd: Introduce fpsimd_save_and_flush_cpu_state() and use it
Quoted 4 lines · Julien Grall
The only external user of
fpsimd_save()andfpsimd_flush_cpu_state()is the KVM FPSIMD code.A follow-up patch will mandate hold of the FPSIMD context while
This is a bit hard to read without understanding the following patch.
Maybe elaborate a bit with something like
"The following patch will introduce a mechanism to acquire ownership of the FPSIMD/SVE context for performing context management operations. Rather than having to export this [...]"
Quoted 25 lines
modifying it. Rather than having to export the new helpers to get/put the context, we can just introduce a new function to combine
fpsimd_save()andfpsimd_flush_cpu_state().This has also the advantage to remove any external call of
fpsimd_save()andfpsimd_flush_cpu_state(), so they can be turned static.Lastly, the new function can also be used in the PM notifier.
Signed-off-by Julien Grall <[email protected]>---
Changes in v2:
- Patch added--- arch/arm64/include/asm/fpsimd.h | 5 ++--- arch/arm64/kernel/fpsimd.c | 17 +++++++++++++---- arch/arm64/kvm/fpsimd.c | 3 +-- 3 files changed, 16 insertions(+), 9 deletions(-)arch/arm64/include/asm/fpsimd.h +0
-0
arch/arm64/include/asm/fpsimd.h struct task_struct; · line 55
Quoted 10 lines
arch/arm64/include/asm/fpsimd.h +2
-2@@ -48,11 +48,11 @@ struct task_struct;extern void fpsimd_save_state (struct user_fpsimd_state *state);extern void fpsimd_load_state (struct user_fpsimd_state *state); -extern void fpsimd_save (void ); -extern void fpsimd_thread_switch (struct task_struct *next);extern void fpsimd_flush_thread (void ); +extern void fpsimd_save_and_flush_cpu_state (void ); +
Nit, bit I think this makes more sense alongside the other flush functions.
Can you just drop this new declaration in place of the old declaration for sve_flush_cpu_state()?
Quoted 49 lines
arch/arm64/include/asm/fpsimd.h +0
-1extern void fpsimd_signal_preserve_current_state (void );extern void fpsimd_preserve_current_state (void );extern void fpsimd_restore_current_state (void );@@ -63,7 +63,6 @@ extern void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *state, void *sve_state, unsigned int sve_vl);extern void fpsimd_flush_task_state (struct task_struct *target); -extern void fpsimd_flush_cpu_state (void );/* Maximum VL that SVE VL-agnostic software can transparently support */ #define SVE_VL_ARCH_MAX 0x100arch/arm64/kernel/fpsimd.c +12
-2@@ -241,7 +241,7 @@ static void task_fpsimd_load(void)* * Softirqs (and preemption) must be disabled. */ -void fpsimd_save (void ) +static void fpsimd_save (void ) {struct fpsimd_last_state_struct const *last =this_cpu_ptr (&fpsimd_last_state);@@ -1117,12 +1117,22 @@ void fpsimd_flush_task_state(struct task_struct *t)* Invalidate any task's FPSIMD state that is present on this cpu. * This function must be called with softirqs disabled. */ -void fpsimd_flush_cpu_state (void ) +static void fpsimd_flush_cpu_state (void ) {__this_cpu_write (fpsimd_last_state.st,NULL );set_thread_flag (TIF_FOREIGN_FPSTATE ); } +/* +* Save the FPSIMD state to memory and invalidate cpu view. +* This function must be called with softirqs (and preemption) disabled. +*/ +void fpsimd_save_and_flush_cpu_state (void ) +{ +fpsimd_save (); +fpsimd_flush_cpu_state (); +} +#ifdef CONFIG_KERNEL_MODE_NEON DEFINE_PER_CPU (bool, kernel_neon_busy);
arch/arm64/kernel/fpsimd.c static int fpsimd_cpu_pm_notifier(struct notifier_block *self, · line 1292
Quoted 6 lines
arch/arm64/kernel/fpsimd.c +1
-2@@ -1279,8 +1289,7 @@ static int fpsimd_cpu_pm_notifier(struct notifier_block *self, {switch (cmd) {case CPU_PM_ENTER : -fpsimd_save (); -fpsimd_flush_cpu_state (); +fpsimd_save_and_flush_cpu_state ();
Any reason not to do this in kernel_neon_begin() too?
arch/arm64/kvm/fpsimd.c void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu) · line 115
Quoted 11 lines
arch/arm64/kernel/fpsimd.c +0
-0break ;case CPU_PM_EXIT :break ;arch/arm64/kvm/fpsimd.c +0
-0@@ -113,8 +113,7 @@ void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)u64 *guest_zcr = &vcpu->arch.ctxt.sys_regs[ZCR_EL1 ];/* Clean guest FP state to memory and invalidate cpu view */
You can lose the comment here, since the call is now self-explanatory.
arch/arm64/kvm/fpsimd.c void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu) · line 116
Quoted 3 lines
arch/arm64/kvm/fpsimd.c +1
-2-fpsimd_save (); -fpsimd_flush_cpu_state (); +fpsimd_save_and_flush_cpu_state ();
[...]
Otherwise, looks sensible.
Cheers
---Dave
Re: [PATCH v2 2/3] arch/arm64: fpsimd: Introduce fpsimd_save_and_flush_cpu_state() and use it
Hi Dave,
Quoted 11 lines · Dave Martin
Quoted 4 lines · Julien Grall
The only external user of
fpsimd_save()andfpsimd_flush_cpu_state()is the KVM FPSIMD code.A follow-up patch will mandate hold of the FPSIMD context while
This is a bit hard to read without understanding the following patch.
Maybe elaborate a bit with something like
"The following patch will introduce a mechanism to acquire ownership of the FPSIMD/SVE context for performing context management operations. Rather than having to export this [...]"
I will update the commit message with your suggestion.
Quoted 34 lines
Quoted 34 lines
modifying it. Rather than having to export the new helpers to get/put the context, we can just introduce a new function to combine
fpsimd_save()andfpsimd_flush_cpu_state().This has also the advantage to remove any external call of
fpsimd_save()andfpsimd_flush_cpu_state(), so they can be turned static.Lastly, the new function can also be used in the PM notifier.
Signed-off-by Julien Grall <[email protected]>---
Changes in v2:
- Patch added--- arch/arm64/include/asm/fpsimd.h | 5 ++--- arch/arm64/kernel/fpsimd.c | 17 +++++++++++++---- arch/arm64/kvm/fpsimd.c | 3 +-- 3 files changed, 16 insertions(+), 9 deletions(-)arch/arm64/include/asm/fpsimd.h +0
-2@@ -48,11 +48,11 @@ struct task_struct;extern void fpsimd_save_state (struct user_fpsimd_state *state);extern void fpsimd_load_state (struct user_fpsimd_state *state); -extern void fpsimd_save (void ); -extern void fpsimd_thread_switch (struct task_struct *next);extern void fpsimd_flush_thread (void );
Quoted 8 lines
arch/arm64/include/asm/fpsimd.h struct task_struct; · line 55
Quoted 2 lines
arch/arm64/include/asm/fpsimd.h +2
-0+extern void fpsimd_save_and_flush_cpu_state (void ); +Nit, bit I think this makes more sense alongside the other flush functions.
Can you just drop this new declaration in place of the old declaration for
sve_flush_cpu_state()?
Sure, I will do it in the next version.
Quoted 48 lines
Quoted 48 lines
arch/arm64/include/asm/fpsimd.h +0
-1extern void fpsimd_signal_preserve_current_state (void );extern void fpsimd_preserve_current_state (void );extern void fpsimd_restore_current_state (void );@@ -63,7 +63,6 @@ extern void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *state, void *sve_state, unsigned int sve_vl);extern void fpsimd_flush_task_state (struct task_struct *target); -extern void fpsimd_flush_cpu_state (void );/* Maximum VL that SVE VL-agnostic software can transparently support */ #define SVE_VL_ARCH_MAX 0x100arch/arm64/kernel/fpsimd.c +12
-2@@ -241,7 +241,7 @@ static void task_fpsimd_load(void)* * Softirqs (and preemption) must be disabled. */ -void fpsimd_save (void ) +static void fpsimd_save (void ) {struct fpsimd_last_state_struct const *last =this_cpu_ptr (&fpsimd_last_state);@@ -1117,12 +1117,22 @@ void fpsimd_flush_task_state(struct task_struct *t)* Invalidate any task's FPSIMD state that is present on this cpu. * This function must be called with softirqs disabled. */ -void fpsimd_flush_cpu_state (void ) +static void fpsimd_flush_cpu_state (void ) {__this_cpu_write (fpsimd_last_state.st,NULL );set_thread_flag (TIF_FOREIGN_FPSTATE ); } +/* +* Save the FPSIMD state to memory and invalidate cpu view. +* This function must be called with softirqs (and preemption) disabled. +*/ +void fpsimd_save_and_flush_cpu_state (void ) +{ +fpsimd_save (); +fpsimd_flush_cpu_state (); +} +#ifdef CONFIG_KERNEL_MODE_NEON
Quoted 10 lines
arch/arm64/kernel/fpsimd.c static int fpsimd_cpu_pm_notifier(struct notifier_block *self, · line 1292
Quoted 8 lines
arch/arm64/kernel/fpsimd.c +1
-2DEFINE_PER_CPU (bool, kernel_neon_busy);@@ -1279,8 +1289,7 @@ static int fpsimd_cpu_pm_notifier(struct notifier_block *self, {switch (cmd) {case CPU_PM_ENTER : -fpsimd_save (); -fpsimd_flush_cpu_state (); +fpsimd_save_and_flush_cpu_state ();Any reason not to do this in
kernel_neon_begin()too?
In the next patch, fpsimd_save_and_flush_cpu_state() will also call {get, put}_cpu_fpsimd_context(). So we avoid to expose the two new functions outside of the file.
I could introduce a double-underscore version of this function if you think it is worth it.
Quoted 11 lines
Quoted 11 lines
arch/arm64/kernel/fpsimd.c +0
-0break ;case CPU_PM_EXIT :break ;arch/arm64/kvm/fpsimd.c +0
-0@@ -113,8 +113,7 @@ void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)u64 *guest_zcr = &vcpu->arch.ctxt.sys_regs[ZCR_EL1 ];
Quoted 3 lines
arch/arm64/kvm/fpsimd.c void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu) · line 115
Quoted 1 line
arch/arm64/kvm/fpsimd.c +0
-0/* Clean guest FP state to memory and invalidate cpu view */ You can lose the comment here, since the call is now self-explanatory.
Sure.
Quoted 8 lines
arch/arm64/kvm/fpsimd.c void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu) · line 116
Quoted 4 lines
arch/arm64/kvm/fpsimd.c +1
-2-fpsimd_save (); -fpsimd_flush_cpu_state (); +fpsimd_save_and_flush_cpu_state ();[...]
Otherwise, looks sensible.
Thank you for the review!
Cheers,
Signature · 1 line
Julien Grall
PATCH v2 3/3 arm64/fpsimd: Don't disable softirq when touching FPSIMD/SVE state
In reply to Julien GrallWhen the kernel is compiled with CONFIG_KERNEL_MODE_NEON, some part of the kernel may be able to use FPSIMD/SVE. This is for instance the case for crypto code.
Any use of FPSIMD/SVE in the kernel are clearly marked by using the function kernel_neon_{begin, end}. Furthermore, this can only be used when may_use_simd() returns true.
The current implementation of may_use_simd() allows softirq to use FPSIMD/SVE unless it is currently in used (i.e kernel_neon_busy is true). When in used, softirqs usually fallback to a software method.
At the moment, as a softirq may use FPSIMD/SVE, softirqs are disabled when touching the FPSIMD/SVE context. This has the drawback to disable all softirqs even if they are not using FPSIMD/SVE.
As a softirq should not rely on been able to use simd at a given time, there are limited reason to keep softirq disabled when touching the FPSIMD/SVE context. Instead, we can only disable preemption and tell the NEON unit is currently in use.
This patch introduces two new helpers {get, put}_cpu_fpsimd_context to mark the area using FPSIMD/SVE context and use them in replacement of local_bh_{disable, enable}. The functions kernel_neon_{begin, end} are also re-implemented to use the new helpers.
Additionally, this patch introduced a double-underscored version of each helper that can be used when preemption is disabled. This avoid to disable/enable preemption for again and helps documenting places where context can only be used by one instance.
This patch has been benchmarked on Linux 5.1-rc4 with defconfig.
On Juno2:
* hackbench 100 process 1000 (10 times)
* .7% quicker
On ThunderX 2:
* hackbench 1000 process 1000 (20 times)
* 3.4% quicker
---
Changes in v2:
- Remove spurious call to kernel_neon_enable in kernel_neon_begin.
- Rename kernel_neon_{enable, disable} to {get, put}_cpu_fpsimd_context
- Introduce a double-underscore version of the helpers for case where preemption is already disabled
- Introduce have_cpu_fpsimd_context() and use it in WARN_ON(...)
- Surround more places in the code with the new helpers
- Rework the comments
- Update the commit message with the benchmark result
--- arch/arm64/include/asm/simd.h | 4 +- arch/arm64/kernel/fpsimd.c | 133 ++++++++++++++++++++++++++++++------------ 2 files changed, 97 insertions(+), 40 deletions(-)
arch/arm64/include/asm/simd.h +2 -2
@@ -15,10 +15,10 @@#include <linux/preempt.h> #include <linux/types.h> -#ifdef CONFIG_KERNEL_MODE_NEON -DECLARE_PER_CPU (bool, kernel_neon_busy); +#ifdef CONFIG_KERNEL_MODE_NEON +/* * may_use_simd - whether it is allowable at this time to issue SIMD * instructions or access the SIMD register file
arch/arm64/kernel/fpsimd.c +95 -38
306 lines not on this page · raw message
Signature · 1 line
2.11.0
Quoted 10 lines · Julien Grall
When the kernel is compiled with
CONFIG_KERNEL_MODE_NEON, some part of the kernel may be able to use FPSIMD/SVE. This is for instance the case for crypto code.Any use of FPSIMD/SVE in the kernel are clearly marked by using the function kernel_neon_{begin, end}. Furthermore, this can only be used when
may_use_simd()returns true.The current implementation of
may_use_simd()allows softirq to use FPSIMD/SVE unless it is currently in used (i.ekernel_neon_busyis true).
Nit: "in used" -> "in use"
Quoted 1 line
When in used, softirqs usually fallback to a software method.
Likewise.
Nit: "fallback" -> "fall back"
Quoted 6 lines
At the moment, as a softirq may use FPSIMD/SVE, softirqs are disabled when touching the FPSIMD/SVE context. This has the drawback to disable all softirqs even if they are not using FPSIMD/SVE.
As a softirq should not rely on been able to use simd at a given time, there are limited reason to keep softirq disabled when touching the
The implication is not totally clear to me here. Maybe write something like
"Since a softirq is supposed to check may_use_simd() anyway before attempting to use FPSIMD/SVE, there is limited reason to keep softirq disabled when touching the FPSIMD/SVE context [...]"
Quoted 1 line
FPSIMD/SVE context. Instead, we can only disable preemption and tell
I'd put "just" or "simply" instead of "only" here.
Quoted 1 line
the NEON unit is currently in use.
Maybe "mark the FPSIMD/SVE context as in use by setting the CPU's kernel_neon_busy flag".
Quoted 2 lines
This patch introduces two new helpers {get, put}_cpu_fpsimd_context to mark the area using FPSIMD/SVE context and use them in replacement of
uses
Quoted 4 lines
local_bh_{disable, enable}. The functions kernel_neon_{begin, end} are also re-implemented to use the new helpers.
Additionally, this patch introduced a double-underscored version of each
introduces
Quoted 2 lines
helper that can be used when preemption is disabled. This avoid to disable/enable preemption for again and helps documenting places where
The wording seems a bit mangled here? Also, these are not for general use, so maybe say something like
"For use in the fpsimd_thread_switch(), which is a critical path where preemption is already disabled, double-underscored versions of the helpers are provided to avoid disabling preemption again."
(I'm assuming here that we don't need to use these elsewhere -- see other comments.)
Quoted 51 lines
context can only be used by one instance.
This patch has been benchmarked on Linux 5.1-rc4 with defconfig.
On Juno2:
* hackbench 100 process 1000 (10 times)
* .7% quickerOn ThunderX 2:
* hackbench 1000 process 1000 (20 times)
* 3.4% quickerSigned-off-by Julien Grall <[email protected]>---
Changes in v2:
- Remove spurious call tokernel_neon_enableinkernel_neon_begin.
- Rename kernel_neon_{enable, disable} to {get, put}_cpu_fpsimd_context
- Introduce a double-underscore version of the helpers for case where preemption is already disabled
- Introducehave_cpu_fpsimd_context()and use it inWARN_ON(...)
- Surround more places in the code with the new helpers
- Rework the comments
- Update the commit message with the benchmark result--- arch/arm64/include/asm/simd.h | 4 +- arch/arm64/kernel/fpsimd.c | 133 ++++++++++++++++++++++++++++++------------ 2 files changed, 97 insertions(+), 40 deletions(-)arch/arm64/include/asm/simd.h +2
-2@@ -15,10 +15,10 @@#include <linux/preempt.h> #include <linux/types.h> -#ifdef CONFIG_KERNEL_MODE_NEON -DECLARE_PER_CPU (bool, kernel_neon_busy); +#ifdef CONFIG_KERNEL_MODE_NEON +/* * may_use_simd - whether it is allowable at this time to issue SIMD * instructions or access the SIMD register file arch/arm64/kernel/fpsimd.c +0
-0
arch/arm64/kernel/fpsimd.c line 95
Quoted 5 lines
arch/arm64/kernel/fpsimd.c +1
-1@@ -92,7 +92,8 @@* To prevent this from racing with the manipulation of the task's FPSIMD state * from task context and thereby corrupting the state, it is necessary to * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE -* flag with local_bh_disable() unless softirqs are already masked. +* flag with kernel_neon_{disable, enable}. This will still allow softirqs to
These names don't match the code now.
arch/arm64/kernel/fpsimd.c extern void __percpu *efi_sve_state; · line 155
Quoted 10 lines
arch/arm64/kernel/fpsimd.c +3
-0+* run but prevent them to use FPSIMD. * * For a certain task, the sequence may look something like this: * - the task gets scheduled in; if both the task's fpsimd_cpu field @@ -150,6 +151,58 @@ extern void __percpu *efi_sve_state;#endif /* ! CONFIG_ARM64_SVE */ +DEFINE_PER_CPU (bool, kernel_neon_busy); +EXPORT_PER_CPU_SYMBOL (kernel_neon_busy);
This feels mis-named now. Maybe "fpsimd_context_busy" would be a better name?
arch/arm64/kernel/fpsimd.c extern void __percpu *efi_sve_state; · line 160
Quoted 5 lines
arch/arm64/kernel/fpsimd.c +5
-0+ +/* +* Obtain the CPU FPSIMD context for use by the calling context. +* +* The caller may freely modify FPSIMD context until *put_cpu_fpsimd_context()
Nit: Why *? This makes it look a bit like get_cpu_fpsimd_context() returns a pointer and you're saying something about dereferencing that pointer here.
arch/arm64/kernel/fpsimd.c extern void __percpu *efi_sve_state; · line 167
Quoted 7 lines
arch/arm64/kernel/fpsimd.c +7
-0+* is called. +* +* The double-underscore version must only be called if you know the task +* can't be preempted. +* +* __get_cpu_fpsimd_context() *must* be in pair with __put_cpu_fpsimd_context() +* get_cpu_fpsimd_context() *must* be in pair with put_cpu_fpsimd_context()
"in pair" -> "paired with"?
I'd move each of these comments to be next to the function it applies to.
arch/arm64/kernel/fpsimd.c extern void __percpu *efi_sve_state; · line 172
Quoted 5 lines
arch/arm64/kernel/fpsimd.c +5
-0+*/ +static void __get_cpu_fpsimd_context (void ) +{ +bool busy =__this_cpu_xchg (kernel_neon_busy, true); +
I don't mind whether there is a blank line here or not, but please make it consistent with __put_cpu_fpsimd_context().
arch/arm64/kernel/fpsimd.c extern void __percpu *efi_sve_state; · line 185
Quoted 13 lines
arch/arm64/kernel/fpsimd.c +13
-0+WARN_ON (busy); +} + +static void get_cpu_fpsimd_context (void ) +{ +preempt_disable (); +__get_cpu_fpsimd_context (); +} + +/* +* Release the CPU FPSIMD context. +* +* Must be called from a context in which *get_cpu_fpsimd_context() was
Nit: Why *?
Quoted 8 lines
arch/arm64/kernel/fpsimd.c +8
-0+* previously called, with no call to *put_cpu_fpsimd_context() in the +* meantime. +*/ +static void __put_cpu_fpsimd_context (void ) +{ +bool busy =__this_cpu_xchg (kernel_neon_busy, false); +WARN_ON (!busy);/* No matching get_cpu_fpsimd_context()? */ +}
arch/arm64/kernel/fpsimd.c extern void __percpu *efi_sve_state; · line 203
Quoted 10 lines
arch/arm64/kernel/fpsimd.c +10
-0+ +static void put_cpu_fpsimd_context (void ) +{ +__put_cpu_fpsimd_context (); +preempt_enable (); +} + +static bool have_cpu_fpsimd_context (void ) +{ +return (!preemptible () &&__this_cpu_read (kernel_neon_busy));
Nit: Redundant ()
arch/arm64/kernel/fpsimd.c static void sve_free(struct task_struct *task) · line 277
Quoted 11 lines
arch/arm64/kernel/fpsimd.c +3
-1+} +/* * Call __sve_free() directly only if you know task can't be scheduled * or preempted. @@ -221,11 +274,12 @@ static void sve_free(struct task_struct *task)* thread_struct is known to be up to date, when preparing to enter * userspace. * -* Softirqs (and preemption) must be disabled. +* The FPSIMD context must be acquired with get_cpu_fpsimd_context()
or __get_cpu_fpsimd_context()? Since this is effectively documented by the WARN_ON() and this is a local function anyway, maybe it would be simpler just to drop this comment here?
Quoted 10 lines
arch/arm64/kernel/fpsimd.c +2
-1+* before calling this function. */ static void task_fpsimd_load (void ) { -WARN_ON (!in_softirq () && !irqs_disabled ()); +WARN_ON (!have_cpu_fpsimd_context ());if (system_supports_sve () &&test_thread_flag (TIF_SVE )) sve_load_state(sve_pffr(¤t->thread),
arch/arm64/kernel/fpsimd.c static void task_fpsimd_load(void) · line 296
Quoted 5 lines
arch/arm64/kernel/fpsimd.c +1
-1@@ -239,15 +293,22 @@ static void task_fpsimd_load(void)* Ensure FPSIMD/SVE storage in memory for the loaded context is up to * date with respect to the CPU registers. * -* Softirqs (and preemption) must be disabled. +* The FPSIMD context must be acquired with get_cpu_fpsimd_context()
Likewise.
arch/arm64/kernel/fpsimd.c static void task_fpsimd_load(void) · line 306
Quoted 11 lines
arch/arm64/kernel/fpsimd.c +3
-1+* before calling this function. */ static void fpsimd_save (void ) {struct fpsimd_last_state_struct const *last =this_cpu_ptr (&fpsimd_last_state);/* set by fpsimd_bind_task_to_cpu() or fpsimd_bind_state_to_cpu() */ +WARN_ON (!have_cpu_fpsimd_context ()); -WARN_ON (!in_softirq () && !irqs_disabled ()); +if ( !have_cpu_fpsimd_context () )
Nit: Redundant whitespace around expression.
Quoted 170 lines
arch/arm64/kernel/fpsimd.c +29
-16+ { +printk ("preemptible() = %u kernel_neon_busy = %u\n" , +preemptible (),__this_cpu_read (kernel_neon_busy)); +while (1 ); + }if (!test_thread_flag (TIF_FOREIGN_FPSTATE )) {if (system_supports_sve () &&test_thread_flag (TIF_SVE )) {@@ -352,7 +413,8 @@ static int __init sve_sysctl_init(void) { return 0; }* task->thread.sve_state. * * Task can be a non-runnable task, or current. In the latter case, -* softirqs (and preemption) must be disabled. +* the FPSIMD context must be acquired with get_fpu_fpsimd_context() +* before calling this function. * task->thread.sve_state must point to at least sve_state_size(task) * bytes of allocated kernel memory. * task->thread.uw.fpsimd_state must be up to date before calling this @@ -379,7 +441,8 @@ static void fpsimd_to_sve(struct task_struct *task)* task->thread.uw.fpsimd_state. * * Task can be a non-runnable task, or current. In the latter case, -* softirqs (and preemption) must be disabled. +* the FPSIMD context must be acquired with get_fpu_fpsimd_context() +* before calling this function. * task->thread.sve_state must point to at least sve_state_size(task) * bytes of allocated kernel memory. * task->thread.sve_state must be up to date before calling this function. @@ -539,7 +602,7 @@ int sve_set_vector_length(struct task_struct *task,* non-SVE thread. */ if (task == current) { -local_bh_disable (); +get_cpu_fpsimd_context ();fpsimd_save (); }@@ -549,7 +612,7 @@ int sve_set_vector_length(struct task_struct *task,sve_to_fpsimd (task);if (task == current) -local_bh_enable (); +put_cpu_fpsimd_context ();/* * Force reallocation of task SVE state to the correct size @@ -862,7 +925,7 @@ asmlinkage void do_sve_acc(unsigned int esr, struct pt_regs *regs)sve_alloc (current); -local_bh_disable (); +get_cpu_fpsimd_context ();fpsimd_save ();@@ -873,7 +936,7 @@ asmlinkage void do_sve_acc(unsigned int esr, struct pt_regs *regs)if (test_and_set_thread_flag (TIF_SVE ))WARN_ON (1 );/* SVE access shouldn't have trapped */ -local_bh_enable (); +put_cpu_fpsimd_context (); }/* @@ -917,6 +980,8 @@ void fpsimd_thread_switch(struct task_struct *next)if (!system_supports_fpsimd ())return ; +__get_cpu_fpsimd_context (); +/* Save unsaved fpsimd state, if any: */ fpsimd_save ();@@ -931,6 +996,8 @@ void fpsimd_thread_switch(struct task_struct *next)update_tsk_thread_flag (next,TIF_FOREIGN_FPSTATE , wrong_task || wrong_cpu); + +__put_cpu_fpsimd_context (); }void fpsimd_flush_thread (void )@@ -940,7 +1007,7 @@ void fpsimd_flush_thread(void)if (!system_supports_fpsimd ())return ; -local_bh_disable (); +get_cpu_fpsimd_context ();fpsimd_flush_task_state (current); memset(¤t->thread.uw.fpsimd_state, 0,@@ -981,7 +1048,7 @@ void fpsimd_flush_thread(void) current->thread.sve_vl_onexec =0 ; } -local_bh_enable (); +put_cpu_fpsimd_context (); }/* @@ -993,9 +1060,9 @@ void fpsimd_preserve_current_state(void)if (!system_supports_fpsimd ())return ; -local_bh_disable (); +get_cpu_fpsimd_context ();fpsimd_save (); -local_bh_enable (); +put_cpu_fpsimd_context (); }/* @@ -1012,7 +1079,8 @@ void fpsimd_signal_preserve_current_state(void)/* * Associate current's FPSIMD context with this cpu -* Preemption must be disabled when calling this function. +* The FPSIMD context should be acquired with get_cpu_fpsimd_context() +* before calling this function. */ void fpsimd_bind_task_to_cpu (void ) {@@ -1058,14 +1126,14 @@ void fpsimd_restore_current_state(void)if (!system_supports_fpsimd ())return ; -local_bh_disable (); +get_cpu_fpsimd_context ();if (test_and_clear_thread_flag (TIF_FOREIGN_FPSTATE )) {task_fpsimd_load ();fpsimd_bind_task_to_cpu (); } -local_bh_enable (); +put_cpu_fpsimd_context (); }/* @@ -1078,7 +1146,7 @@ void fpsimd_update_current_state(struct user_fpsimd_state const *state)if (!system_supports_fpsimd ())return ; -local_bh_disable (); +get_cpu_fpsimd_context (); current->thread.uw.fpsimd_state = *state;if (system_supports_sve () &&test_thread_flag (TIF_SVE ))@@ -1089,7 +1157,7 @@ void fpsimd_update_current_state(struct user_fpsimd_state const *state)clear_thread_flag (TIF_FOREIGN_FPSTATE ); -local_bh_enable (); +put_cpu_fpsimd_context (); }/* @@ -1115,7 +1183,8 @@ void fpsimd_flush_task_state(struct task_struct *t)/* * Invalidate any task's FPSIMD state that is present on this cpu. -* This function must be called with softirqs disabled. +* The FPSIMD context should be acquired with get_cpu_fpsimd_context() +* before calling this function. */ static void fpsimd_flush_cpu_state (void ) {@@ -1125,19 +1194,18 @@ static void fpsimd_flush_cpu_state(void)/*
arch/arm64/kernel/fpsimd.c static void fpsimd_flush_cpu_state(void) · line 1204
Quoted 10 lines
arch/arm64/kernel/fpsimd.c +3
-1* Save the FPSIMD state to memory and invalidate cpu view. -* This function must be called with softirqs (and preemption) disabled. +* This function must be called with preemption disabled. */ void fpsimd_save_and_flush_cpu_state (void ) { +__get_cpu_fpsimd_context ();fpsimd_save ();fpsimd_flush_cpu_state (); +__put_cpu_fpsimd_context ();
It may be cleaner to avoid the assumption about preemption already being disabled here. fpsimd_thread_switch() is rather a special case, but for this one is this really used on a hot path that justifies the assumption?
If not, we could just move to the regular (non-__) functions here and drop that comment.
[...]
Cheers
---Dave
Hi Dave,
Quoted 27 lines · Dave Martin
Quoted 10 lines · Julien Grall
When the kernel is compiled with
CONFIG_KERNEL_MODE_NEON, some part of the kernel may be able to use FPSIMD/SVE. This is for instance the case for crypto code.Any use of FPSIMD/SVE in the kernel are clearly marked by using the function kernel_neon_{begin, end}. Furthermore, this can only be used when
may_use_simd()returns true.The current implementation of
may_use_simd()allows softirq to use FPSIMD/SVE unless it is currently in used (i.ekernel_neon_busyis true).Nit: "in used" -> "in use"
Quoted 1 line
When in used, softirqs usually fallback to a software method.
Likewise.
Nit: "fallback" -> "fall back"
Quoted 6 lines
At the moment, as a softirq may use FPSIMD/SVE, softirqs are disabled when touching the FPSIMD/SVE context. This has the drawback to disable all softirqs even if they are not using FPSIMD/SVE.
As a softirq should not rely on been able to use simd at a given time, there are limited reason to keep softirq disabled when touching the
Quoted 6 lines
The implication is not totally clear to me here. Maybe write something like
"Since a softirq is supposed to check
may_use_simd()anyway before attempting to use FPSIMD/SVE, there is limited reason to keep softirq disabled when touching the FPSIMD/SVE context [...]"
I will update the commit message.
Quoted 9 lines
Quoted 2 lines
FPSIMD/SVE context. Instead, we can only disable preemption and tell
I'd put "just" or "simply" instead of "only" here.
Quoted 1 line
the NEON unit is currently in use.
Maybe "mark the FPSIMD/SVE context as in use by setting the CPU's
kernel_neon_busyflag".
Sounds better as this flag does not protect only the hardware but some part of context that resides in memory.
Quoted 9 lines
Quoted 3 lines
This patch introduces two new helpers {get, put}_cpu_fpsimd_context to mark the area using FPSIMD/SVE context and use them in replacement of
uses
Quoted 3 lines
local_bh_{disable, enable}. The functions kernel_neon_{begin, end} are also re-implemented to use the new helpers.
Quoted 8 lines
Quoted 1 line
Additionally, this patch introduced a double-underscored version of each
introduces
Quoted 2 lines
helper that can be used when preemption is disabled. This avoid to disable/enable preemption for again and helps documenting places where
The wording seems a bit mangled here?
Oops yes.
Quoted 8 lines
Also, these are not for general use, so maybe say something like
"For use in the
fpsimd_thread_switch(), which is a critical path where preemption is already disabled, double-underscored versions of the helpers are provided to avoid disabling preemption again."(I'm assuming here that we don't need to use these elsewhere -- see other comments.)
I will comment on this below.
Quoted 66 lines
Quoted 52 lines
context can only be used by one instance.
This patch has been benchmarked on Linux 5.1-rc4 with defconfig.
On Juno2:
* hackbench 100 process 1000 (10 times)
* .7% quickerOn ThunderX 2:
* hackbench 1000 process 1000 (20 times)
* 3.4% quickerSigned-off-by Julien Grall <[email protected]>---
Changes in v2:
- Remove spurious call tokernel_neon_enableinkernel_neon_begin.
- Rename kernel_neon_{enable, disable} to {get, put}_cpu_fpsimd_context
- Introduce a double-underscore version of the helpers for case where preemption is already disabled
- Introducehave_cpu_fpsimd_context()and use it inWARN_ON(...)
- Surround more places in the code with the new helpers
- Rework the comments
- Update the commit message with the benchmark result--- arch/arm64/include/asm/simd.h | 4 +- arch/arm64/kernel/fpsimd.c | 133 ++++++++++++++++++++++++++++++------------ 2 files changed, 97 insertions(+), 40 deletions(-)arch/arm64/include/asm/simd.h +2
-2@@ -15,10 +15,10 @@#include <linux/preempt.h> #include <linux/types.h> -#ifdef CONFIG_KERNEL_MODE_NEON -DECLARE_PER_CPU (bool, kernel_neon_busy); +#ifdef CONFIG_KERNEL_MODE_NEON +/* * may_use_simd - whether it is allowable at this time to issue SIMD * instructions or access the SIMD register file arch/arm64/kernel/fpsimd.c +0
-0arch/arm64/kernel/fpsimd.c line 95
Quoted 5 lines
arch/arm64/kernel/fpsimd.c +1
-1@@ -92,7 +92,8 @@* To prevent this from racing with the manipulation of the task's FPSIMD state * from task context and thereby corrupting the state, it is necessary to * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE -* flag with local_bh_disable() unless softirqs are already masked. +* flag with kernel_neon_{disable, enable}. This will still allow softirqs to These names don't match the code now.
Quoted 6 lines
arch/arm64/kernel/fpsimd.c +1
-0+* run but prevent them to use FPSIMD. * * For a certain task, the sequence may look something like this: * - the task gets scheduled in; if both the task's fpsimd_cpu field @@ -150,6 +151,58 @@ extern void __percpu *efi_sve_state;
Quoted 7 lines
arch/arm64/kernel/fpsimd.c extern void __percpu *efi_sve_state; · line 155
Quoted 4 lines
arch/arm64/kernel/fpsimd.c +2
-0#endif /* ! CONFIG_ARM64_SVE */ +DEFINE_PER_CPU (bool, kernel_neon_busy); +EXPORT_PER_CPU_SYMBOL (kernel_neon_busy);This feels mis-named now. Maybe "
fpsimd_context_busy" would be a better name?
Make sense. I will update it.
Quoted 10 lines
arch/arm64/kernel/fpsimd.c extern void __percpu *efi_sve_state; · line 160
Quoted 6 lines
arch/arm64/kernel/fpsimd.c +5
-0+ +/* +* Obtain the CPU FPSIMD context for use by the calling context. +* +* The caller may freely modify FPSIMD context until *put_cpu_fpsimd_context() Nit: Why *? This makes it look a bit like
get_cpu_fpsimd_context()returns a pointer and you're saying something about dereferencing that pointer here.
I tend to use * for wildcard. In this context it used to refers to both the double-underscored version and the one without.
I can use {,__}put_cpu_fpsimd_context() instead.
Quoted 10 lines
arch/arm64/kernel/fpsimd.c extern void __percpu *efi_sve_state; · line 167
Quoted 8 lines
arch/arm64/kernel/fpsimd.c +7
-0+* is called. +* +* The double-underscore version must only be called if you know the task +* can't be preempted. +* +* __get_cpu_fpsimd_context() *must* be in pair with __put_cpu_fpsimd_context() +* get_cpu_fpsimd_context() *must* be in pair with put_cpu_fpsimd_context() "in pair" -> "paired with"?
Sure.
Quoted 3 lines
I'd move each of these comments to be next to the function it applies to.
Do you mean on top of {,__}put_cpu_ or {,__}get_cpu?
Quoted 9 lines
arch/arm64/kernel/fpsimd.c extern void __percpu *efi_sve_state; · line 172
Quoted 6 lines
arch/arm64/kernel/fpsimd.c +5
-0+*/ +static void __get_cpu_fpsimd_context (void ) +{ +bool busy =__this_cpu_xchg (kernel_neon_busy, true); +I don't mind whether there is a blank line here or not, but please make it consistent with
__put_cpu_fpsimd_context().
I will modify __put_cpu_fpsimd_context() to add a newline.
Quoted 16 lines
Quoted 4 lines
arch/arm64/kernel/fpsimd.c +3
-0+WARN_ON (busy); +} +arch/arm64/kernel/fpsimd.c extern void __percpu *efi_sve_state; · line 185
Quoted 10 lines
arch/arm64/kernel/fpsimd.c +10
-0+static void get_cpu_fpsimd_context (void ) +{ +preempt_disable (); +__get_cpu_fpsimd_context (); +} + +/* +* Release the CPU FPSIMD context. +* +* Must be called from a context in which *get_cpu_fpsimd_context() was Nit: Why *?
Same as above, I can update to use {,__} instead of *.
Quoted 22 lines
Quoted 9 lines
arch/arm64/kernel/fpsimd.c +8
-0+* previously called, with no call to *put_cpu_fpsimd_context() in the +* meantime. +*/ +static void __put_cpu_fpsimd_context (void ) +{ +bool busy =__this_cpu_xchg (kernel_neon_busy, false); +WARN_ON (!busy);/* No matching get_cpu_fpsimd_context()? */ +}arch/arm64/kernel/fpsimd.c extern void __percpu *efi_sve_state; · line 203
Quoted 10 lines
arch/arm64/kernel/fpsimd.c +10
-0+ +static void put_cpu_fpsimd_context (void ) +{ +__put_cpu_fpsimd_context (); +preempt_enable (); +} + +static bool have_cpu_fpsimd_context (void ) +{ +return (!preemptible () &&__this_cpu_read (kernel_neon_busy));Nit: Redundant ()
Quoted 15 lines
arch/arm64/kernel/fpsimd.c static void sve_free(struct task_struct *task) · line 277
Quoted 11 lines
arch/arm64/kernel/fpsimd.c +3
-1+} +/* * Call __sve_free() directly only if you know task can't be scheduled * or preempted. @@ -221,11 +274,12 @@ static void sve_free(struct task_struct *task)* thread_struct is known to be up to date, when preparing to enter * userspace. * -* Softirqs (and preemption) must be disabled. +* The FPSIMD context must be acquired with get_cpu_fpsimd_context() or
__get_cpu_fpsimd_context()? Since this is effectively documented by theWARN_ON()and this is a local function anyway, maybe it would be simpler just to drop this comment here?
I am fine with that.
Quoted 8 lines
Quoted 8 lines
arch/arm64/kernel/fpsimd.c +2
-1+* before calling this function. */ static void task_fpsimd_load (void ) { -WARN_ON (!in_softirq () && !irqs_disabled ()); +WARN_ON (!have_cpu_fpsimd_context ());
Quoted 10 lines
arch/arm64/kernel/fpsimd.c static void task_fpsimd_load(void) · line 296
Quoted 8 lines
arch/arm64/kernel/fpsimd.c +1
-1if (system_supports_sve () &&test_thread_flag (TIF_SVE )) sve_load_state(sve_pffr(¤t->thread),@@ -239,15 +293,22 @@ static void task_fpsimd_load(void)* Ensure FPSIMD/SVE storage in memory for the loaded context is up to * date with respect to the CPU registers. * -* Softirqs (and preemption) must be disabled. +* The FPSIMD context must be acquired with get_cpu_fpsimd_context() Likewise.
Ditto.
Quoted 10 lines
Quoted 10 lines
arch/arm64/kernel/fpsimd.c +2
-0+* before calling this function. */ static void fpsimd_save (void ) {struct fpsimd_last_state_struct const *last =this_cpu_ptr (&fpsimd_last_state);/* set by fpsimd_bind_task_to_cpu() or fpsimd_bind_state_to_cpu() */ +WARN_ON (!have_cpu_fpsimd_context ());
Quoted 4 lines
arch/arm64/kernel/fpsimd.c static void task_fpsimd_load(void) · line 306
Quoted 2 lines
arch/arm64/kernel/fpsimd.c +1
-1-WARN_ON (!in_softirq () && !irqs_disabled ()); +if ( !have_cpu_fpsimd_context () )Nit: Redundant whitespace around expression.
This hunk should actually be dropped. I was using for debugging and forgot to remove it before sending the series :/.
Quoted 7 lines
Quoted 7 lines
arch/arm64/kernel/fpsimd.c +5
-0+ { +printk ("preemptible() = %u kernel_neon_busy = %u\n" , +preemptible (),__this_cpu_read (kernel_neon_busy)); +while (1 ); + }
Quoted 9 lines
arch/arm64/kernel/fpsimd.c static int __init sve_sysctl_init(void) { return 0; } · line 417
Quoted 9 lines
arch/arm64/kernel/fpsimd.c +2
-1if (!test_thread_flag (TIF_FOREIGN_FPSTATE )) {if (system_supports_sve () &&test_thread_flag (TIF_SVE )) {@@ -352,7 +413,8 @@ static int __init sve_sysctl_init(void) { return 0; }* task->thread.sve_state. * * Task can be a non-runnable task, or current. In the latter case, -* softirqs (and preemption) must be disabled. +* the FPSIMD context must be acquired with get_fpu_fpsimd_context() +* before calling this function.
I noticed you didn't comment about the usage of get_cpu_fpsimd_context here. Do you want to add a WARN(..) in the function, or just using {,___} here?
Quoted 10 lines
arch/arm64/kernel/fpsimd.c static void fpsimd_to_sve(struct task_struct *task) · line 445
Quoted 10 lines
arch/arm64/kernel/fpsimd.c +2
-1* task->thread.sve_state must point to at least sve_state_size(task) * bytes of allocated kernel memory. * task->thread.uw.fpsimd_state must be up to date before calling this @@ -379,7 +441,8 @@ static void fpsimd_to_sve(struct task_struct *task)* task->thread.uw.fpsimd_state. * * Task can be a non-runnable task, or current. In the latter case, -* softirqs (and preemption) must be disabled. +* the FPSIMD context must be acquired with get_fpu_fpsimd_context() +* before calling this function.
Same question here.
[...]
Quoted 7 lines
arch/arm64/kernel/fpsimd.c void fpsimd_signal_preserve_current_state(void) · line 1083
Quoted 7 lines
arch/arm64/kernel/fpsimd.c +2
-1@@ -1012,7 +1079,8 @@ void fpsimd_signal_preserve_current_state(void)/* * Associate current's FPSIMD context with this cpu -* Preemption must be disabled when calling this function. +* The FPSIMD context should be acquired with get_cpu_fpsimd_context() +* before calling this function.
Same question here.
Quoted 10 lines
Quoted 10 lines
arch/arm64/kernel/fpsimd.c +2
-1/* * Invalidate any task's FPSIMD state that is present on this cpu. -* This function must be called with softirqs disabled. +* The FPSIMD context should be acquired with get_cpu_fpsimd_context() +* before calling this function. */ static void fpsimd_flush_cpu_state (void ) {@@ -1125,19 +1194,18 @@ static void fpsimd_flush_cpu_state(void)
Quoted 15 lines
arch/arm64/kernel/fpsimd.c static void fpsimd_flush_cpu_state(void) · line 1204
Quoted 11 lines
arch/arm64/kernel/fpsimd.c +3
-1/* * Save the FPSIMD state to memory and invalidate cpu view. -* This function must be called with softirqs (and preemption) disabled. +* This function must be called with preemption disabled. */ void fpsimd_save_and_flush_cpu_state (void ) { +__get_cpu_fpsimd_context ();fpsimd_save ();fpsimd_flush_cpu_state (); +__put_cpu_fpsimd_context ();It may be cleaner to avoid the assumption about preemption already being disabled here.
fpsimd_thread_switch()is rather a special case, but for this one is this really used on a hot path that justifies the assumption?
It is currently only called with preemption disabled. So I thought it would be better to avoid disabling preemption again. But I am happy to use the non-__ version if you think it is better.
Cheers,
Signature · 1 line
Julien Grall
Quoted 4 lines · Julien Grall
Hi Dave,
Quoted 1 line · Dave Martin
On Fri, Apr 12, 2019 at 06:14:20PM +0100, Julien Grall wrote:
[...]
Quoted 3 lines
Quoted 3 lines
arch/arm64/kernel/fpsimd.c extern void __percpu *efi_sve_state; · line 158
Quoted 3 lines
arch/arm64/kernel/fpsimd.c +3
-0+ +/* +* Obtain the CPU FPSIMD context for use by the calling context.
If we say something like "Claim ownership of the CPU FPSIMD context for use by the calling context", this may be easier to reference elsewhere (see below).
Quoted 9 lines
Quoted 6 lines
Quoted 2 lines
+ *
+ * The caller may freely modify FPSIMD context until*put_cpu_fpsimd_context()Nit: Why *? This makes it look a bit like
get_cpu_fpsimd_context()returns a pointer and you're saying something about dereferencing that pointer here.I tend to use * for wildcard. In this context it used to refers to both the double-underscored version and the one without.
Ah, right. * makes sense in general, but here it confused me.
Quoted 1 line
I can use {,__}put_cpu_fpsimd_context() instead.
Maybe, but the caller is not supposed to pair get_cpu_fpsimd_context() with __put_cpu_fpsimd_context().
What if we just comment the non-underscore versions. The purpose of the __ versions is then relatively obvious without additional commenting.
Quoted 12 lines
Quoted 11 lines
Quoted 9 lines
+ * is called.
+ *
+ * The double-underscore version must only be called if you know the task
+ * can't be preempted.
+ *
+ *__get_cpu_fpsimd_context()*must* be in pair with__put_cpu_fpsimd_context()
+ *get_cpu_fpsimd_context()*must* be in pair withput_cpu_fpsimd_context()"in pair" -> "paired with"?
Quoted 7 lines
Sure.
Quoted 3 lines
I'd move each of these comments to be next to the function it applies to.
Do you mean on top of {,__}put_cpu_ or {,__}get_cpu?
It doesn't matter. It's the statement about get_cpu_fpsimd_context() next to the definition of __get_cpu_fpsimd_context() that I find a bit weird.
Arguably we could solve this by just having less commenting: the expectation that a "get" should be paired with a subsequent matching "put" is a common pattern (which was the reason for suggesting those names).
Quoted 104 lines
Quoted 8 lines
arch/arm64/kernel/fpsimd.c extern void __percpu *efi_sve_state; · line 172
Quoted 5 lines
+ */
arch/arm64/kernel/fpsimd.c +4
-0+static void __get_cpu_fpsimd_context (void ) +{ +bool busy =__this_cpu_xchg (kernel_neon_busy, true); +I don't mind whether there is a blank line here or not, but please make it consistent with
__put_cpu_fpsimd_context().I will modify
__put_cpu_fpsimd_context()to add a newline.Quoted 16 lines
Quoted 4 lines
arch/arm64/kernel/fpsimd.c +3
-0+WARN_ON (busy); +} +arch/arm64/kernel/fpsimd.c extern void __percpu *efi_sve_state; · line 185
Quoted 10 lines
arch/arm64/kernel/fpsimd.c +10
-0+static void get_cpu_fpsimd_context (void ) +{ +preempt_disable (); +__get_cpu_fpsimd_context (); +} + +/* +* Release the CPU FPSIMD context. +* +* Must be called from a context in which *get_cpu_fpsimd_context() was Nit: Why *?
Same as above, I can update to use {,__} instead of *.
Quoted 22 lines
Quoted 9 lines
+ * previously called, with no call to
*put_cpu_fpsimd_context()in the
+ * meantime.
+ */arch/arm64/kernel/fpsimd.c +5
-0+static void __put_cpu_fpsimd_context (void ) +{ +bool busy =__this_cpu_xchg (kernel_neon_busy, false); +WARN_ON (!busy);/* No matching get_cpu_fpsimd_context()? */ +}arch/arm64/kernel/fpsimd.c extern void __percpu *efi_sve_state; · line 203
Quoted 10 lines
arch/arm64/kernel/fpsimd.c +10
-0+ +static void put_cpu_fpsimd_context (void ) +{ +__put_cpu_fpsimd_context (); +preempt_enable (); +} + +static bool have_cpu_fpsimd_context (void ) +{ +return (!preemptible () &&__this_cpu_read (kernel_neon_busy));Nit: Redundant ()
Quoted 15 lines
arch/arm64/kernel/fpsimd.c static void sve_free(struct task_struct *task) · line 277
Quoted 11 lines
arch/arm64/kernel/fpsimd.c +3
-1+} +/* * Call __sve_free() directly only if you know task can't be scheduled * or preempted. @@ -221,11 +274,12 @@ static void sve_free(struct task_struct *task)* thread_struct is known to be up to date, when preparing to enter * userspace. * -* Softirqs (and preemption) must be disabled. +* The FPSIMD context must be acquired with get_cpu_fpsimd_context() or
__get_cpu_fpsimd_context()? Since this is effectively documented by theWARN_ON()and this is a local function anyway, maybe it would be simpler just to drop this comment here?I am fine with that.
Quoted 17 lines
Quoted 10 lines
arch/arm64/kernel/fpsimd.c +2
-1+* before calling this function. */ static void task_fpsimd_load (void ) { -WARN_ON (!in_softirq () && !irqs_disabled ()); +WARN_ON (!have_cpu_fpsimd_context ());if (system_supports_sve () &&test_thread_flag (TIF_SVE )) sve_load_state(sve_pffr(¤t->thread),arch/arm64/kernel/fpsimd.c static void task_fpsimd_load(void) · line 296
Quoted 5 lines
arch/arm64/kernel/fpsimd.c +1
-1@@ -239,15 +293,22 @@ static void task_fpsimd_load(void)* Ensure FPSIMD/SVE storage in memory for the loaded context is up to * date with respect to the CPU registers. * -* Softirqs (and preemption) must be disabled. +* The FPSIMD context must be acquired with get_cpu_fpsimd_context() Likewise.
Ditto.
Quoted 13 lines
arch/arm64/kernel/fpsimd.c static void task_fpsimd_load(void) · line 306
Quoted 11 lines
arch/arm64/kernel/fpsimd.c +3
-1+* before calling this function. */ static void fpsimd_save (void ) {struct fpsimd_last_state_struct const *last =this_cpu_ptr (&fpsimd_last_state);/* set by fpsimd_bind_task_to_cpu() or fpsimd_bind_state_to_cpu() */ +WARN_ON (!have_cpu_fpsimd_context ()); -WARN_ON (!in_softirq () && !irqs_disabled ()); +if ( !have_cpu_fpsimd_context () )Nit: Redundant whitespace around expression.
Quoted 2 lines
This hunk should actually be dropped. I was using for debugging and forgot to remove it before sending the series :/.
Ah, right. I didn't spot it -- yes, please remove.
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
arch/arm64/kernel/fpsimd.c +5
-0+ { +printk ("preemptible() = %u kernel_neon_busy = %u\n" , +preemptible (),__this_cpu_read (kernel_neon_busy)); +while (1 ); + }if (!test_thread_flag (TIF_FOREIGN_FPSTATE )) {if (system_supports_sve () &&test_thread_flag (TIF_SVE )) {@@ -352,7 +413,8 @@ static int __init sve_sysctl_init(void) { return 0; }* task->thread.sve_state.
Quoted 8 lines
Quoted 5 lines
arch/arm64/kernel/fpsimd.c static int __init sve_sysctl_init(void) { return 0; } · line 417
Quoted 5 lines
arch/arm64/kernel/fpsimd.c +2
-1* * Task can be a non-runnable task, or current. In the latter case, -* softirqs (and preemption) must be disabled. +* the FPSIMD context must be acquired with get_fpu_fpsimd_context() +* before calling this function. I noticed you didn't comment about the usage of
get_cpu_fpsimd_contexthere. Do you want to add a WARN(..) in the function, or just using {,___} here?
Partly because of things getting repetitive.
WARN() is not free, so I suggest we don't add any new ones for now.
From a comment point of view, if we just say something "the caller must have ownership of the cpu FPSIMD context" to refer back to the commenting for get_cpu_fpsimd_context(), that should cover the general case.
Quoted 16 lines
Quoted 11 lines
arch/arm64/kernel/fpsimd.c static void fpsimd_to_sve(struct task_struct *task) · line 445
Quoted 11 lines
arch/arm64/kernel/fpsimd.c +2
-1* task->thread.sve_state must point to at least sve_state_size(task) * bytes of allocated kernel memory. * task->thread.uw.fpsimd_state must be up to date before calling this @@ -379,7 +441,8 @@ static void fpsimd_to_sve(struct task_struct *task)* task->thread.uw.fpsimd_state. * * Task can be a non-runnable task, or current. In the latter case, -* softirqs (and preemption) must be disabled. +* the FPSIMD context must be acquired with get_fpu_fpsimd_context() +* before calling this function. Same question here.
[...]
Quoted 8 lines
Quoted 6 lines
arch/arm64/kernel/fpsimd.c void fpsimd_signal_preserve_current_state(void) · line 1082
Quoted 6 lines
arch/arm64/kernel/fpsimd.c +2
-1@@ -1012,7 +1079,8 @@ void fpsimd_signal_preserve_current_state(void)/* * Associate current's FPSIMD context with this cpu -* Preemption must be disabled when calling this function. +* The FPSIMD context should be acquired with get_cpu_fpsimd_context() +* before calling this function. Same question here.
Same for these.
Quoted 25 lines
Quoted 16 lines
Quoted 16 lines
arch/arm64/kernel/fpsimd.c +3
-2/* * Invalidate any task's FPSIMD state that is present on this cpu. -* This function must be called with softirqs disabled. +* The FPSIMD context should be acquired with get_cpu_fpsimd_context() +* before calling this function. */ static void fpsimd_flush_cpu_state (void ) {@@ -1125,19 +1194,18 @@ static void fpsimd_flush_cpu_state(void)/* * Save the FPSIMD state to memory and invalidate cpu view. -* This function must be called with softirqs (and preemption) disabled. +* This function must be called with preemption disabled. */ void fpsimd_save_and_flush_cpu_state (void ) {Quoted 8 lines
arch/arm64/kernel/fpsimd.c static void fpsimd_flush_cpu_state(void) · line 1204
Quoted 4 lines
arch/arm64/kernel/fpsimd.c +2
-0+__get_cpu_fpsimd_context ();fpsimd_save ();fpsimd_flush_cpu_state (); +__put_cpu_fpsimd_context ();It may be cleaner to avoid the assumption about preemption already being disabled here.
fpsimd_thread_switch()is rather a special case, but for this one is this really used on a hot path that justifies the assumption?
Quoted 3 lines
It is currently only called with preemption disabled. So I thought it would be better to avoid disabling preemption again. But I am happy to use the non-__ version if you think it is better.
Hmmm, this works either way. Since this is not fast-path and has an external caller, it might be worth adding a WARN_ON(preemptible()) here if you want to stick with the __ functions.
Otherwise, I don't have a strong opinion.
Cheers
---Dave
Hi Dave,
Quoted 28 lines · Dave Martin
Quoted 4 lines · Julien Grall
Hi Dave,
Quoted 1 line · Dave Martin
On Fri, Apr 12, 2019 at 06:14:20PM +0100, Julien Grall wrote:
[...]
Quoted 3 lines
Quoted 3 lines
arch/arm64/kernel/fpsimd.c extern void __percpu *efi_sve_state; · line 158
Quoted 3 lines
arch/arm64/kernel/fpsimd.c +3
-0+ +/* +* Obtain the CPU FPSIMD context for use by the calling context. If we say something like "Claim ownership of the CPU FPSIMD context for use by the calling context", this may be easier to reference elsewhere (see below).
Quoted 9 lines
Quoted 6 lines
Quoted 2 lines
+ *
+ * The caller may freely modify FPSIMD context until*put_cpu_fpsimd_context()Nit: Why *? This makes it look a bit like
get_cpu_fpsimd_context()returns a pointer and you're saying something about dereferencing that pointer here.I tend to use * for wildcard. In this context it used to refers to both the double-underscored version and the one without.
Ah, right. * makes sense in general, but here it confused me.
Quoted 7 lines
Quoted 1 line
I can use {,__}put_cpu_fpsimd_context() instead.
Maybe, but the caller is not supposed to pair
get_cpu_fpsimd_context()with__put_cpu_fpsimd_context().What if we just comment the non-underscore versions. The purpose of the __ versions is then relatively obvious without additional commenting.
I am happy with this solution as well.
[...]
Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
arch/arm64/kernel/fpsimd.c +5
-0+ { +printk ("preemptible() = %u kernel_neon_busy = %u\n" , +preemptible (),__this_cpu_read (kernel_neon_busy)); +while (1 ); + }if (!test_thread_flag (TIF_FOREIGN_FPSTATE )) {if (system_supports_sve () &&test_thread_flag (TIF_SVE )) {@@ -352,7 +413,8 @@ static int __init sve_sysctl_init(void) { return 0; }* task->thread.sve_state. Quoted 8 lines
Quoted 5 lines
arch/arm64/kernel/fpsimd.c static int __init sve_sysctl_init(void) { return 0; } · line 417
Quoted 5 lines
arch/arm64/kernel/fpsimd.c +2
-1* * Task can be a non-runnable task, or current. In the latter case, -* softirqs (and preemption) must be disabled. +* the FPSIMD context must be acquired with get_fpu_fpsimd_context() +* before calling this function. I noticed you didn't comment about the usage of
get_cpu_fpsimd_contexthere. Do you want to add a WARN(..) in the function, or just using {,___} here?
Quoted 8 lines
Partly because of things getting repetitive.
WARN()is not free, so I suggest we don't add any new ones for now.From a comment point of view, if we just say something "the caller must
have ownership of the cpu FPSIMD context" to refer back to the commenting forget_cpu_fpsimd_context(), that should cover the general case.
I am fine with this suggestion.
Quoted 52 lines
Quoted 16 lines
Quoted 11 lines
arch/arm64/kernel/fpsimd.c static void fpsimd_to_sve(struct task_struct *task) · line 445
Quoted 11 lines
arch/arm64/kernel/fpsimd.c +2
-1* task->thread.sve_state must point to at least sve_state_size(task) * bytes of allocated kernel memory. * task->thread.uw.fpsimd_state must be up to date before calling this @@ -379,7 +441,8 @@ static void fpsimd_to_sve(struct task_struct *task)* task->thread.uw.fpsimd_state. * * Task can be a non-runnable task, or current. In the latter case, -* softirqs (and preemption) must be disabled. +* the FPSIMD context must be acquired with get_fpu_fpsimd_context() +* before calling this function. Same question here.
[...]
Quoted 8 lines
Quoted 6 lines
arch/arm64/kernel/fpsimd.c void fpsimd_signal_preserve_current_state(void) · line 1082
Quoted 6 lines
arch/arm64/kernel/fpsimd.c +2
-1@@ -1012,7 +1079,8 @@ void fpsimd_signal_preserve_current_state(void)/* * Associate current's FPSIMD context with this cpu -* Preemption must be disabled when calling this function. +* The FPSIMD context should be acquired with get_cpu_fpsimd_context() +* before calling this function. Same question here.
Same for these.
Quoted 25 lines
Quoted 24 lines
Quoted 10 lines
arch/arm64/kernel/fpsimd.c +2
-1/* * Invalidate any task's FPSIMD state that is present on this cpu. -* This function must be called with softirqs disabled. +* The FPSIMD context should be acquired with get_cpu_fpsimd_context() +* before calling this function. */ static void fpsimd_flush_cpu_state (void ) {@@ -1125,19 +1194,18 @@ static void fpsimd_flush_cpu_state(void)/* arch/arm64/kernel/fpsimd.c static void fpsimd_flush_cpu_state(void) · line 1203
Quoted 10 lines
arch/arm64/kernel/fpsimd.c +3
-1* Save the FPSIMD state to memory and invalidate cpu view. -* This function must be called with softirqs (and preemption) disabled. +* This function must be called with preemption disabled. */ void fpsimd_save_and_flush_cpu_state (void ) { +__get_cpu_fpsimd_context ();fpsimd_save ();fpsimd_flush_cpu_state (); +__put_cpu_fpsimd_context ();It may be cleaner to avoid the assumption about preemption already being disabled here.
fpsimd_thread_switch()is rather a special case, but for this one is this really used on a hot path that justifies the assumption?
Quoted 7 lines
Quoted 3 lines
It is currently only called with preemption disabled. So I thought it would be better to avoid disabling preemption again. But I am happy to use the non-__ version if you think it is better.
Hmmm, this works either way. Since this is not fast-path and has an external caller, it might be worth adding a WARN_ON(preemptible()) here if you want to stick with the __ functions.
As it is not a fastpath, I will use the non-underscore version.
Cheers,
Signature · 1 line
Julien Grall
Quoted 8 lines · Julien Grall
Hi Dave,
Quoted 5 lines · Dave Martin
Quoted 4 lines · Julien Grall
Hi Dave,
Quoted 1 line · Dave Martin
On Fri, Apr 12, 2019 at 06:14:20PM +0100, Julien Grall wrote:
[...]
Quoted 21 lines
Quoted 21 lines
Quoted 20 lines
Quoted 8 lines
Quoted 8 lines
arch/arm64/kernel/fpsimd.c +1
-1@@ -1125,19 +1194,18 @@ static void fpsimd_flush_cpu_state(void)/* * Save the FPSIMD state to memory and invalidate cpu view. -* This function must be called with softirqs (and preemption) disabled. +* This function must be called with preemption disabled. */ void fpsimd_save_and_flush_cpu_state (void ) {Quoted 8 lines
arch/arm64/kernel/fpsimd.c static void fpsimd_flush_cpu_state(void) · line 1204
Quoted 4 lines
arch/arm64/kernel/fpsimd.c +2
-0+__get_cpu_fpsimd_context ();fpsimd_save ();fpsimd_flush_cpu_state (); +__put_cpu_fpsimd_context ();It may be cleaner to avoid the assumption about preemption already being disabled here.
fpsimd_thread_switch()is rather a special case, but for this one is this really used on a hot path that justifies the assumption?It is currently only called with preemption disabled. So I thought it would be better to avoid disabling preemption again. But I am happy to use the non-__ version if you think it is better.
Quoted 5 lines
Quoted 3 lines
Hmmm, this works either way. Since this is not fast-path and has an external caller, it might be worth adding a WARN_ON(preemptible()) here if you want to stick with the __ functions.
As it is not a fastpath, I will use the non-underscore version.
OK, either is good for me.
Cheers
---Dave