READ_ONCE, WRITE_ONCE, kcsan: Perform checks in __*_ONCE variants
PATCH 1/1 READ_ONCE, WRITE_ONCE, kcsan: Perform checks in __*_ONCE variants
If left plain, using __READ_ONCE and __WRITE_ONCE will result in many false positives with KCSAN due to being instrumented normally. To fix, we should move the kcsan_check and data_race into __*_ONCE.
---
A proposal to fix the problem with __READ_ONCE/__WRITE_ONCE and KCSAN false positives.
Will, please feel free to take this patch and fiddle with it until it looks like what you want if this is completely off.
Note: Currently __WRITE_ONCE_SCALAR seems to serve no real purpose. Do we still need it?
--- include/linux/compiler.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
include/linux/compiler.h +9 -6
@@ -224,13 +224,16 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,* atomicity or dependency ordering guarantees. Note that this may result * in tears! */ -#define __READ_ONCE (x) (*(const volatile __unqual_scalar_typeof(x) *)&(x)) +#define __READ_ONCE (x) \ +({ \ + kcsan_check_atomic_read(&(x), sizeof(x)); \ + data_race((*(const volatile __unqual_scalar_typeof(x) *)&(x))); \ +})#define __READ_ONCE_SCALAR (x) \ ({ \ typeof(x) *__xp = &(x); \ - __unqual_scalar_typeof(x) __x = data_race(__READ_ONCE(*__xp)); \ - kcsan_check_atomic_read(__xp, sizeof(*__xp)); \ + __unqual_scalar_typeof(x) __x = __READ_ONCE(*__xp); \ smp_read_barrier_depends(); \ (typeof(x))__x; \ })
@@ -243,14 +246,14 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,#define __WRITE_ONCE (x, val) \ do { \ - *(volatile typeof(x) *)&(x) = (val); \ + kcsan_check_atomic_write(&(x), sizeof(x)); \ + data_race(*(volatile typeof(x) *)&(x) = (val)); \ } while (0)#define __WRITE_ONCE_SCALAR (x, val) \ do { \ typeof(x) *__xp = &(x); \ - kcsan_check_atomic_write(__xp, sizeof(*__xp)); \ - data_race(({ __WRITE_ONCE(*__xp, val); 0; })); \ + __WRITE_ONCE(*__xp, val); \ } while (0)#define WRITE_ONCE (x, val) \
Signature · 1 line
2.26.2.645.ge9eca65c58-goog
Peter Zijlstra
NAKQuoted 5 lines · Marco Elver
include/linux/compiler.h +0
-0
include/linux/compiler.h void ftrace_likely_update(struct ftrace_likely_data *f, int val, · line 231
Quoted 9 lines
include/linux/compiler.h +5
-1@@ -224,13 +224,16 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,* atomicity or dependency ordering guarantees. Note that this may result * in tears! */ -#define __READ_ONCE (x) (*(const volatile __unqual_scalar_typeof(x) *)&(x)) +#define __READ_ONCE (x) \ +({ \ + kcsan_check_atomic_read(&(x), sizeof(x)); \ + data_race((*(const volatile __unqual_scalar_typeof(x) *)&(x))); \ +})
NAK
This will actively insert instrumentation into __READ_ONCE() and I need it to not have any.
Quoted 19 lines · Peter Zijlstra
Quoted 5 lines · Marco Elver
include/linux/compiler.h +0
-0include/linux/compiler.h void ftrace_likely_update(struct ftrace_likely_data *f, int val, · line 231
Quoted 9 lines
include/linux/compiler.h +5
-1@@ -224,13 +224,16 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,* atomicity or dependency ordering guarantees. Note that this may result * in tears! */ -#define __READ_ONCE (x) (*(const volatile __unqual_scalar_typeof(x) *)&(x)) +#define __READ_ONCE (x) \ +({ \ + kcsan_check_atomic_read(&(x), sizeof(x)); \ + data_race((*(const volatile __unqual_scalar_typeof(x) *)&(x))); \ +})NAK
Quoted 2 lines
This will actively insert instrumentation into
__READ_ONCE()and I need it to not have any.
Any way to move this forward? Due to linux-next commit 6bcc8f459fe7 (locking/atomics: Flip fallbacks and instrumentation), it triggers a lots of KCSAN warnings due to atomic ops are no longer marked. For example,
[ 197.318288][ T1041] write to 0xffff9302764ccc78 of 8 bytes by task 1048 on cpu 47: [ 197.353119][ T1041] down_read_trylock+0x9e/0x1e0
atomic_long_set(&sem->owner, val); __rwsem_set_reader_owned at kernel/locking/rwsem.c:205 (inlined by) rwsem_set_reader_owned at kernel/locking/rwsem.c:213 (inlined by) __down_read_trylock at kernel/locking/rwsem.c:1373 (inlined by) down_read_trylock at kernel/locking/rwsem.c:1517
[ 197.374641][ T1041] page_lock_anon_vma_read+0x19d/0x3c0 [ 197.398894][ T1041] rmap_walk_anon+0x30e/0x620
[ 197.924695][ T1041] read to 0xffff9302764ccc78 of 8 bytes by task 1041 on cpu 43: [ 197.959501][ T1041] up_read+0xb8/0x41a
arch_atomic64_read at arch/x86/include/asm/atomic64_64.h:22 (inlined by) atomic64_read at include/asm-generic/atomic-instrumented.h:838 (inlined by) atomic_long_read at include/asm-generic/atomic-long.h:29 (inlined by) rwsem_clear_reader_owned at kernel/locking/rwsem.c:242 (inlined by) __up_read at kernel/locking/rwsem.c:1433
(inlined by) up_read at kernel/locking/rwsem.c:1574
[ 197.977728][ T1041] rmap_walk_anon+0x2f2/0x620 [ 197.999055][ T1041] rmap_walk+0xb5/0xe0
Quoted 21 lines · Qian Cai
Quoted 19 lines · Peter Zijlstra
Quoted 5 lines · Marco Elver
include/linux/compiler.h +0
-0include/linux/compiler.h void ftrace_likely_update(struct ftrace_likely_data *f, int val, · line 231
Quoted 9 lines
include/linux/compiler.h +5
-1@@ -224,13 +224,16 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,* atomicity or dependency ordering guarantees. Note that this may result * in tears! */ -#define __READ_ONCE (x) (*(const volatile __unqual_scalar_typeof(x) *)&(x)) +#define __READ_ONCE (x) \ +({ \ + kcsan_check_atomic_read(&(x), sizeof(x)); \ + data_race((*(const volatile __unqual_scalar_typeof(x) *)&(x))); \ +})NAK
Quoted 6 lines
Quoted 2 lines
This will actively insert instrumentation into
__READ_ONCE()and I need it to not have any.Any way to move this forward? Due to linux-next commit
6bcc8f459fe7(locking/atomics: Flip fallbacks and instrumentation), it triggers a lots of KCSAN warnings due to atomic ops are no longer marked.
This is no longer the right solution we believe due to the various requirements that Peter also mentioned. See the discussion here:
https://lkml.kernel.org/r/CANpmjNOGFqhtDa9wWpXs2kztQsSozbwsuMO5BqqW0c0g0zGfSA@mail.gmail.com
The new solution is here:
https://lkml.kernel.org/r/[email protected]
While it's a little inconvenient that we'll require Clang 11 (currently available by building yourself from LLVM repo), but until we get GCC fixed (my patch there still pending :-/), this is probably
the right solution going forward. If possible, please do test!
Thanks,
-- Marco
Quoted 24 lines
For
example,[ 197.318288][ T1041] write to 0xffff9302764ccc78 of 8 bytes by task 1048 on cpu 47: [ 197.353119][ T1041] down_read_trylock+0x9e/0x1e0
atomic_long_set(&sem->owner, val);__rwsem_set_reader_ownedatkernel/locking/rwsem.c:205(inlined by)rwsem_set_reader_ownedatkernel/locking/rwsem.c:213(inlined by)__down_read_trylockatkernel/locking/rwsem.c:1373(inlined by)down_read_trylockatkernel/locking/rwsem.c:1517[ 197.374641][ T1041] page_lock_anon_vma_read+0x19d/0x3c0 [ 197.398894][ T1041] rmap_walk_anon+0x30e/0x620[ 197.924695][ T1041] read to 0xffff9302764ccc78 of 8 bytes by task 1041 on cpu 43: [ 197.959501][ T1041] up_read+0xb8/0x41a
arch_atomic64_readatarch/x86/include/asm/atomic64_64.h:22(inlined by)atomic64_readatinclude/asm-generic/atomic-instrumented.h:838(inlined by)atomic_long_readatinclude/asm-generic/atomic-long.h:29(inlined by)rwsem_clear_reader_ownedatkernel/locking/rwsem.c:242(inlined by)__up_readatkernel/locking/rwsem.c:1433
(inlined by)up_readatkernel/locking/rwsem.c:1574[ 197.977728][ T1041] rmap_walk_anon+0x2f2/0x620 [ 197.999055][ T1041] rmap_walk+0xb5/0xe0
Quoted 34 lines · Marco Elver
Quoted 21 lines · Qian Cai
Quoted 19 lines · Peter Zijlstra
Quoted 5 lines · Marco Elver
include/linux/compiler.h +0
-0include/linux/compiler.h void ftrace_likely_update(struct ftrace_likely_data *f, int val, · line 231
Quoted 9 lines
include/linux/compiler.h +5
-1@@ -224,13 +224,16 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,* atomicity or dependency ordering guarantees. Note that this may result * in tears! */ -#define __READ_ONCE (x) (*(const volatile __unqual_scalar_typeof(x) *)&(x)) +#define __READ_ONCE (x) \ +({ \ + kcsan_check_atomic_read(&(x), sizeof(x)); \ + data_race((*(const volatile __unqual_scalar_typeof(x) *)&(x))); \ +})NAK
Quoted 6 lines
Quoted 2 lines
This will actively insert instrumentation into
__READ_ONCE()and I need it to not have any.Any way to move this forward? Due to linux-next commit
6bcc8f459fe7(locking/atomics: Flip fallbacks and instrumentation), it triggers a lots of KCSAN warnings due to atomic ops are no longer marked.This is no longer the right solution we believe due to the various requirements that Peter also mentioned. See the discussion here:
https://lkml.kernel.org/r/CANpmjNOGFqhtDa9wWpXs2kztQsSozbwsuMO5BqqW0c0g0zGfSA@mail.gmail.com
Quoted 6 lines
The new solution is here:
https://lkml.kernel.org/r/[email protected]While it's a little inconvenient that we'll require Clang 11 (currently available by building yourself from LLVM repo), but until we get GCC fixed (my patch there still pending :-/), this is probably
the right solution going forward. If possible, please do test!
That would be quite unfortunate. The version here is still gcc-8.3.1 and clang-9.0.1 on RHEL 8.2 here. It will probably need many years to be able to get the fixed compilers having versions that high. Sigh... Also, I want to avoid compiling compilers on my own.
Quoted 8 lines · Qian Cai
Quoted 6 lines · Marco Elver
The new solution is here:
https://lkml.kernel.org/r/[email protected]While it's a little inconvenient that we'll require Clang 11 (currently available by building yourself from LLVM repo), but until we get GCC fixed (my patch there still pending :-/), this is probably
the right solution going forward. If possible, please do test!
Quoted 4 lines
That would be quite unfortunate. The version here is still gcc-8.3.1 and clang-9.0.1 on RHEL 8.2 here. It will probably need many years to be able to get the fixed compilers having versions that high. Sigh... Also, I want to avoid compiling compilers on my own.
Yes, it's unfortunate, but we have to stop making major concessions just because tools are not up to the task.
We've done that way too much in the past and this particular problem clearly demonstrates that there are limits.
Making brand new technology depend on sane tools is not asked too much. And yes, it's inconvenient, but all of us have to build tools every now and then to get our job done. It's not the end of the world.
Building clang is trivial enough and pointing the make to the right compiler is not rocket science either.
Thanks,
tglx
Quoted 10 lines · Thomas Gleixner
Yes, it's unfortunate, but we have to stop making major concessions just because tools are not up to the task.
We've done that way too much in the past and this particular problem clearly demonstrates that there are limits.
Quoted 6 lines
Making brand new technology depend on sane tools is not asked too much. And yes, it's inconvenient, but all of us have to build tools every now and then to get our job done. It's not the end of the world.
Building clang is trivial enough and pointing the make to the right compiler is not rocket science either.
Yes, it all make sense from that angle. On the other hand, I want to be focus on kernel rather than compilers by using a stable and rocket-solid version. Not mentioned the time lost by compiling and properly manage my own toolchain in an automated environment, using such new version of compilers means that I have to inevitably deal with compiler bugs occasionally. Anyway, it is just some other more bugs I have to deal with, and I don’t have a better solution to offer right now.
Quoted 10 lines · Qian Cai
Quoted 10 lines · Thomas Gleixner
Yes, it's unfortunate, but we have to stop making major concessions just because tools are not up to the task.
We've done that way too much in the past and this particular problem clearly demonstrates that there are limits.
Quoted 8 lines
Quoted 6 lines
Making brand new technology depend on sane tools is not asked too much. And yes, it's inconvenient, but all of us have to build tools every now and then to get our job done. It's not the end of the world.
Building clang is trivial enough and pointing the make to the right compiler is not rocket science either.
Yes, it all make sense from that angle. On the other hand, I want to be focus on kernel rather than compilers by using a stable and rocket-solid version. Not mentioned the time lost by compiling and properly manage my own toolchain in an automated environment, using such new version of compilers means that I have to inevitably deal with compiler bugs occasionally. Anyway, it is just some other more bugs I have to deal with, and I don’t have a better solution to offer right now.
Hi Qian,
Shameless plug but I have made a Python script to efficiently configure then build clang specifically for building the kernel (turn off a lot of different things that the kernel does not need).
https://github.com/ClangBuiltLinux/tc-build
I added an option '--use-good-revision', which uses an older master version (basically somewhere between clang-10 and current master) that has been qualified against the kernel. I currently update it every Linux release but I am probably going to start doing it every month as I have written a pretty decent framework to ensure that nothing is breaking on either the LLVM or kernel side.
$ ./build-llvm.py --use-good-revision
should be all you need to get off the ground and running if you wanted to give it a shot. The script is completely self contained by default so it won't mess with the rest of your system. Additionally, leaving off '--use-good-revision' will just use the master branch, which can definitely be broken but not as often as you would think (although I totally understand wanting to focus on kernel regressions only).
Cheers,
Nathan
Quoted 36 lines · Nathan Chancellor
Quoted 10 lines · Qian Cai
Quoted 10 lines · Thomas Gleixner
Yes, it's unfortunate, but we have to stop making major concessions just because tools are not up to the task.
We've done that way too much in the past and this particular problem clearly demonstrates that there are limits.
Quoted 8 lines
Quoted 6 lines
Making brand new technology depend on sane tools is not asked too much. And yes, it's inconvenient, but all of us have to build tools every now and then to get our job done. It's not the end of the world.
Building clang is trivial enough and pointing the make to the right compiler is not rocket science either.
Yes, it all make sense from that angle. On the other hand, I want to be focus on kernel rather than compilers by using a stable and rocket-solid version. Not mentioned the time lost by compiling and properly manage my own toolchain in an automated environment, using such new version of compilers means that I have to inevitably deal with compiler bugs occasionally. Anyway, it is just some other more bugs I have to deal with, and I don’t have a better solution to offer right now.
Hi Qian,
Shameless plug but I have made a Python script to efficiently configure then build clang specifically for building the kernel (turn off a lot of different things that the kernel does not need).
https://github.com/ClangBuiltLinux/tc-build
I added an option '--use-good-revision', which uses an older master version (basically somewhere between clang-10 and current master) that has been qualified against the kernel. I currently update it every Linux release but I am probably going to start doing it every month as I have written a pretty decent framework to ensure that nothing is breaking on either the LLVM or kernel side.
Quoted 8 lines
$ ./build-llvm.py --use-good-revisionshould be all you need to get off the ground and running if you wanted to give it a shot. The script is completely self contained by default so it won't mess with the rest of your system. Additionally, leaving off '--use-good-revision' will just use the master branch, which can definitely be broken but not as often as you would think (although I totally understand wanting to focus on kernel regressions only).
Great, thanks. I'll try it in a bit.
Quoted 40 lines · Qian Cai
Quoted 38 lines · Nathan Chancellor
Quoted 10 lines · Qian Cai
Quoted 10 lines · Thomas Gleixner
Yes, it's unfortunate, but we have to stop making major concessions just because tools are not up to the task.
We've done that way too much in the past and this particular problem clearly demonstrates that there are limits.
Quoted 8 lines
Quoted 6 lines
Making brand new technology depend on sane tools is not asked too much. And yes, it's inconvenient, but all of us have to build tools every now and then to get our job done. It's not the end of the world.
Building clang is trivial enough and pointing the make to the right compiler is not rocket science either.
Yes, it all make sense from that angle. On the other hand, I want to be focus on kernel rather than compilers by using a stable and rocket-solid version. Not mentioned the time lost by compiling and properly manage my own toolchain in an automated environment, using such new version of compilers means that I have to inevitably deal with compiler bugs occasionally. Anyway, it is just some other more bugs I have to deal with, and I don’t have a better solution to offer right now.
Hi Qian,
Shameless plug but I have made a Python script to efficiently configure then build clang specifically for building the kernel (turn off a lot of different things that the kernel does not need).
https://github.com/ClangBuiltLinux/tc-build
I added an option '--use-good-revision', which uses an older master version (basically somewhere between clang-10 and current master) that has been qualified against the kernel. I currently update it every Linux release but I am probably going to start doing it every month as I have written a pretty decent framework to ensure that nothing is breaking on either the LLVM or kernel side.
$ ./build-llvm.py --use-good-revision
Quoted 8 lines
Quoted 6 lines
should be all you need to get off the ground and running if you wanted to give it a shot. The script is completely self contained by default so it won't mess with the rest of your system. Additionally, leaving off '--use-good-revision' will just use the master branch, which can definitely be broken but not as often as you would think (although I totally understand wanting to focus on kernel regressions only).
Great, thanks. I'll try it in a bit.
Please let me know if there are any issues!
Do note that in order to get support for Marco's series, you will need to have a version of LLVM that includes [1], which the current --use-good-revision does not. You can checkout that revision exactly through the '-b' ('--branch') parameter:
$ ./build-llvm.py -b 5a2c31116f412c3b6888be361137efd705e05814
I also see another patch in LLVM that concerns KCSAN [2] but that does not appear used in Marco's series. Still might be worth having available in your version of clang.
I'll try to bump the hash that '--use-good-revision' uses soon. I might wait until 5.7 final so that I can do both at the same time like I usually do but we'll see how much time I have.
[1]: https://github.com/llvm/llvm-project/commit/5a2c31116f412c3b6888be361137efd705e05814
[2]: https://github.com/llvm/llvm-project/commit/151ed6aa38a3ec6c01973b35f684586b6e1c0f7e
Cheers,
Nathan
Quoted 64 lines · Nathan Chancellor
Quoted 40 lines · Qian Cai
Quoted 38 lines · Nathan Chancellor
Quoted 10 lines · Qian Cai
Quoted 10 lines · Thomas Gleixner
Yes, it's unfortunate, but we have to stop making major concessions just because tools are not up to the task.
We've done that way too much in the past and this particular problem clearly demonstrates that there are limits.
Quoted 8 lines
Quoted 6 lines
Making brand new technology depend on sane tools is not asked too much. And yes, it's inconvenient, but all of us have to build tools every now and then to get our job done. It's not the end of the world.
Building clang is trivial enough and pointing the make to the right compiler is not rocket science either.
Yes, it all make sense from that angle. On the other hand, I want to be focus on kernel rather than compilers by using a stable and rocket-solid version. Not mentioned the time lost by compiling and properly manage my own toolchain in an automated environment, using such new version of compilers means that I have to inevitably deal with compiler bugs occasionally. Anyway, it is just some other more bugs I have to deal with, and I don’t have a better solution to offer right now.
Hi Qian,
Shameless plug but I have made a Python script to efficiently configure then build clang specifically for building the kernel (turn off a lot of different things that the kernel does not need).
https://github.com/ClangBuiltLinux/tc-build
I added an option '--use-good-revision', which uses an older master version (basically somewhere between clang-10 and current master) that has been qualified against the kernel. I currently update it every Linux release but I am probably going to start doing it every month as I have written a pretty decent framework to ensure that nothing is breaking on either the LLVM or kernel side.
$ ./build-llvm.py --use-good-revisionQuoted 8 lines
Quoted 6 lines
should be all you need to get off the ground and running if you wanted to give it a shot. The script is completely self contained by default so it won't mess with the rest of your system. Additionally, leaving off '--use-good-revision' will just use the master branch, which can definitely be broken but not as often as you would think (although I totally understand wanting to focus on kernel regressions only).
Great, thanks. I'll try it in a bit.
Please let me know if there are any issues!
Do note that in order to get support for Marco's series, you will need to have a version of LLVM that includes [1], which the current --use-good-revision does not. You can checkout that revision exactly through the '-b' ('--branch') parameter:
$ ./build-llvm.py -b 5a2c31116f412c3b6888be361137efd705e05814I also see another patch in LLVM that concerns KCSAN [2] but that does not appear used in Marco's series. Still might be worth having available in your version of clang.
Quoted 6 lines
I'll try to bump the hash that '--use-good-revision' uses soon. I might wait until 5.7 final so that I can do both at the same time like I usually do but we'll see how much time I have.
[1]: https://github.com/llvm/llvm-project/commit/5a2c31116f412c3b6888be361137efd705e05814
[2]: https://github.com/llvm/llvm-project/commit/151ed6aa38a3ec6c01973b35f684586b6e1c0f7e
Thanks for sharing the script, this is very useful!
Note that [2] above is used, but optional:
https://lore.kernel.org/lkml/[email protected]/
It's not required for KCSAN to function correctly, but if it's available it'll help find more data races with the default config.
Thanks,
-- Marco