arm64: neon: Do not access kernel_neon_busy with preemption enabled
PATCH 1/1 arm64: neon: Do not access kernel_neon_busy with preemption enabled
From: Yandong Zhao <[email protected]>
Dear, Dave
Thank you very much for reading my email. I have a question and I hope toget your reply.
It is a bug to not close the preemption when executing raw_cpu_read(kernel_neon_busy) in the function may_use_simd()! We encountered a ?sysdump? problem on Kernel 4.14 code because the program entered BUG_ON() in the function kernel_neon_begin(). Our analysis concludes that the process is migrating while executing raw_cpu_read(kernel_neon_busy).
--- arch/arm64/include/asm/simd.h | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-)
arch/arm64/include/asm/simd.h +5 -12
@@ -29,20 +29,13 @@static __must_check inline bool may_use_simd (void ) {/* -* The raw_cpu_read() is racy if called with preemption enabled. -* This is not a bug: kernel_neon_busy is only set when -* preemption is disabled, so we cannot migrate to another CPU -* while it is set, nor can we migrate to a CPU where it is set. -* So, if we find it clear on some CPU then we're guaranteed to -* find it clear on any CPU we could migrate to. -* -* If we are in between kernel_neon_begin()...kernel_neon_end(), -* the flag will be set, but preemption is also disabled, so we -* can't migrate to another CPU and spuriously see it become -* false. +* Operations for contexts where we do not want to do any checks for +* preemptions. Unless strictly necessary, always use this_cpu_*() +* instead. Because of the kernel_neon_busy here we have to make sure +* that it is the current cpu. */ return !in_irq () && !irqs_disabled () && !in_nmi () && - !raw_cpu_read (kernel_neon_busy); + !this_cpu_read (kernel_neon_busy); }#else /* ! CONFIG_KERNEL_MODE_NEON */
Signature · 1 line
1.9.1
PATCH 1/1 arm64: neon: Do not access kernel_neon_busy with preemption enabled
Quoted 11 lines · Yandong.Zhao
From: Yandong Zhao <[email protected]>
Dear, Dave
Thank you very much for reading my email. I have a question and I hope toget your reply.
It is a bug to not close the preemption when executingraw_cpu_read(kernel_neon_busy)in the functionmay_use_simd()! We encountered a ?sysdump? problem on Kernel 4.14 code because the program enteredBUG_ON()in the functionkernel_neon_begin(). Our analysis concludes that the process is migrating while executingraw_cpu_read(kernel_neon_busy).
Can you include a backtrace, please?
Also, do you have any significant patches applies on top of v4.14? Is your configuration different from defconfig?
Quoted 23 lines
Signed-off-by Yandong Zhao <[email protected]>--- arch/arm64/include/asm/simd.h | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-)arch/arm64/include/asm/simd.h +0
-9@@ -29,20 +29,13 @@static __must_check inline bool may_use_simd (void ) {/* -* The raw_cpu_read() is racy if called with preemption enabled. -* This is not a bug: kernel_neon_busy is only set when -* preemption is disabled, so we cannot migrate to another CPU -* while it is set, nor can we migrate to a CPU where it is set. -* So, if we find it clear on some CPU then we're guaranteed to -* find it clear on any CPU we could migrate to. -* -* If we are in between kernel_neon_begin()...kernel_neon_end(), -* the flag will be set, but preemption is also disabled, so we
arch/arm64/include/asm/simd.h line 38
Quoted 10 lines
arch/arm64/include/asm/simd.h +5
-3-* can't migrate to another CPU and spuriously see it become -* false. +* Operations for contexts where we do not want to do any checks for +* preemptions. Unless strictly necessary, always use this_cpu_*() +* instead. Because of the kernel_neon_busy here we have to make sure +* that it is the current cpu. */ return !in_irq () && !irqs_disabled () && !in_nmi () && - !raw_cpu_read (kernel_neon_busy); + !this_cpu_read (kernel_neon_busy);
This doesn't really make sense by itself: this function is required to work in preemptible contexts, so in some situations there is no way to be sure which cpu's kernel_neon_busy flag is read.
The original comment attempts to explain why this doesn't matter, and why raw_cpu_read() is valid. Can you explain why this is wrong?
The BUG_ON() in kernel_neon_begin() is there for the purpose of detecting invalid uses of kernel_neon_begin() -- i.e., one kernel_neon_begin() nested inside another, or kernel_neon_begin() called from hardirq or nmi context.
Are you sure kernel_neon_begin() is not being used wrongly somewhere?
If kernel_neon_begin() is being used wrongly, the following hack may help to identify where the problem is:
Cheers
---Dave
--8<--
arch/arm64/kernel/fpsimd.c +14 -1
@@ -1165,6 +1165,7 @@ void fpsimd_flush_cpu_state(void)DEFINE_PER_CPU (bool, kernel_neon_busy);EXPORT_PER_CPU_SYMBOL (kernel_neon_busy); +static DEFINE_PER_CPU(void *, kernel_neon_caller);/* * Kernel-side NEON support functions
@@ -1188,11 +1189,23 @@ void kernel_neon_begin(void)if (WARN_ON (!system_supports_fpsimd ()))return ; -BUG_ON (!may_use_simd ()); +preempt_disable (); +if (!may_use_simd ()) { +if (in_irq () ||irqs_disabled () ||in_nmi ()) +pr_err ("fpsimd: kernel_neon_begin() from hardirq/nmi context\n" ); + +if (this_cpu_read (kernel_neon_busy)) +pr_err ("fpsimd: kernel_neon_begin() already called by %pF\n" , +this_cpu_read (kernel_neon_caller)); + +BUG (); + } +preempt_enable ();local_bh_disable ();__this_cpu_write (kernel_neon_busy, true); +__this_cpu_write (kernel_neon_caller, (void *)_RET_IP_);/* Save unsaved fpsimd state, if any: */ fpsimd_save ();
A message that is not in the archive (sent off-list or not yet received): CAAMpZHV7wxofLa1AWEKcuGSa5bwzcfCRy=+zyB2-tw5m5NueVQ@mail.gmail.com
PATCH 1/1 arm64: neon: Do not access kernel_neon_busy with preemption enabled
Quoted 15 lines · Yandong Zhao
Hi Dave,
The scenario for this bug is:
The A process is sched out when the CPU0 executes the functionraw_cpu_read(kernel_neon_busy)and just gets the address ofkernel_neon_busywithout reading.
The B process starts runningkernel_neon_begin()on CPU0, and the variablekernel_neon_busyon CPU0 becomes true. At this time, the A process is executed on CPU1 and thekernel_neon_busyvalue is CPU0 (true), soBUG_ON()!!!crash64>
kernel_neon_busy
PER-CPU DATA TYPE:
boolkernel_neon_busy;
PER-CPU ADDRESSES:
[0]:ffffffc07fee30a0
[1]:ffffffc07fef90a0
[2]:ffffffc07ff0f0a0
[3]:ffffffc07ff250a0
Quoted 10 lines
CPU0 CPU1
| |
A task have get addrffffffc07fee30a0|
then sched out |
| |
B taskkernel_neon_begin()[ffffffc07fee30a0]=1 |
| |
| A task sched in and read [ffffffc07fee30a0]==1,soBUG_ON.
| |
B taskkernel_neon_end()[ffffffc07fee30a0]=0 |
Ah, OK. Because raw_cpu_read() read is preemptible, we can read a value for kernel_neon_busy that effectively belongs to another task.
That was a dumb mistake on my part -- thanks for investingating it.
Quoted 36 lines
2018-07-09 18:27 GMT+08:00 Dave Martin <Dave.Martin at arm.com<mailto:[email protected]>>:
Quoted 11 lines · Yandong.Zhao
From: Yandong Zhao <yandong77520 at gmail.com<mailto:[email protected]>>
Dear, Dave
Thank you very much for reading my email. I have a question and I hope toget your reply.
It is a bug to not close the preemption when executingraw_cpu_read(kernel_neon_busy)in the functionmay_use_simd()! We encountered a ?sysdump? problem on Kernel 4.14 code because the program enteredBUG_ON()in the functionkernel_neon_begin(). Our analysis concludes that the process is migrating while executingraw_cpu_read(kernel_neon_busy).Can you include a backtrace, please?
Also, do you have any significant patches applies on top of v4.14? Is your configuration different from defconfig?
Quoted 17 lines
Signed-off-by Yandong Zhao <yandong77520 at gmail.com<mailto:[email protected]>>--- arch/arm64/include/asm/simd.h | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-)arch/arm64/include/asm/simd.h +0
-3@@ -29,20 +29,13 @@static __must_check inline bool may_use_simd (void ) {/* -* The raw_cpu_read() is racy if called with preemption enabled. -* This is not a bug: kernel_neon_busy is only set when -* preemption is disabled, so we cannot migrate to another CPU
Quoted 8 lines
arch/arm64/include/asm/simd.h line 42
Quoted 8 lines
arch/arm64/include/asm/simd.h +0
-8-* while it is set, nor can we migrate to a CPU where it is set. -* So, if we find it clear on some CPU then we're guaranteed to -* find it clear on any CPU we could migrate to. -* -* If we are in between kernel_neon_begin()...kernel_neon_end(), -* the flag will be set, but preemption is also disabled, so we -* can't migrate to another CPU and spuriously see it become -* false.
Please keep the above comment: it remains important to explain why the return value from may_use_simd() remains valid even if the task migrates to another cpu.
The first sentence should be reworded to clarify the nature of the race, something like:
"The this_cpu_read() call is racy if called with preemption enabled, since the task my subsequently migrate to another CPU. This is not a bug: [...]"
Quoted 4 lines
arch/arm64/include/asm/simd.h line 35
Quoted 4 lines
arch/arm64/include/asm/simd.h +4
-0+* Operations for contexts where we do not want to do any checks for +* preemptions. Unless strictly necessary, always use this_cpu_*() +* instead. Because of the kernel_neon_busy here we have to make sure +* that it is the current cpu.
Your comment should be moved to the commit message, since it just explains what was fixed.
Quoted 4 lines
arch/arm64/include/asm/simd.h line 38
Quoted 4 lines
arch/arm64/include/asm/simd.h +1
-1*/ return !in_irq () && !irqs_disabled () && !in_nmi () && - !raw_cpu_read (kernel_neon_busy); + !this_cpu_read (kernel_neon_busy);
This adds overhead, but I don't see a way around it.
Can you post an updated version with an amended commit message and a suitable Fixes: tag?
Cheers
---Dave
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.