Skip to contentSkip to conversation

kcsan: xtensa: Add atomic builtin stubs for 32-bit systems

PATCH In review Rohan McLure 4 participants 9 messages Feb 16, 2023 – Feb 20, 2023

Subsystems (3)
lore
Share & tools
lore
#1 lore raw

PATCH 2/2 powerpc/{32,book3e}: kcsan: Extend KCSAN Support

In reply to Rohan McLure

Enable HAVE_ARCH_KCSAN on all powerpc platforms, permitting use of the kernel concurrency sanitiser through the CONFIG_KCSAN_* kconfig options.

Boots and passes selftests on 32-bit and 64-bit platforms. See documentation in Documentation/dev-tools/kcsan.rst for more information.

---
New patch

---
 arch/powerpc/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/Kconfig +1 -1
index 2c9cdf1d8761..45771448d47a 100644
@@ -197,7 +197,7 @@ config PPC
 	select HAVE_ARCH_KASAN			if PPC_RADIX_MMU
 	select HAVE_ARCH_KASAN			if PPC_BOOK3E_64
 	select HAVE_ARCH_KASAN_VMALLOC		if HAVE_ARCH_KASAN
-	select HAVE_ARCH_KCSAN            	if PPC_BOOK3S_64
+	select HAVE_ARCH_KCSAN
 	select HAVE_ARCH_KFENCE			if ARCH_SUPPORTS_DEBUG_PAGEALLOC
 	select HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET
 	select HAVE_ARCH_KGDB
Signature · 1 line
2.37.2
#0 lore raw

PATCH 1/2 kcsan: xtensa: Add atomic builtin stubs for 32-bit systems

KCSAN instruments calls to atomic builtins, and will in turn call these builtins itself. As such, architectures supporting KCSAN must have compiler support for these atomic primitives.

Since 32-bit systems are unlikely to have 64-bit compiler builtins, provide a stub for each missing builtin, and use BUG() to assert unreachability.

In commit 725aea873261 ("xtensa: enable KCSAN"), xtensa implements these locally. Move these definitions to be accessible to all 32-bit architectures that do not provide the necessary builtins, with opt in for PowerPC and xtensa.

---
Previously issued as a part of a patch series adding KCSAN support to 64-bit.

v1: Remove __has_builtin check, as gcc is not obligated to inline builtins detected using this check, but instead is permitted to supply them in libatomic:

Instead, opt-in PPC32 and xtensa.

---
 arch/xtensa/lib/Makefile                              | 1 -
 kernel/kcsan/Makefile                                 | 2 ++
 arch/xtensa/lib/kcsan-stubs.c => kernel/kcsan/stubs.c | 0
 3 files changed, 2 insertions(+), 1 deletion(-)
 rename arch/xtensa/lib/kcsan-stubs.c => kernel/kcsan/stubs.c (100%)
arch/xtensa/lib/Makefile +0 -1
index 7ecef0519a27..d69356dc97df 100644
@@ -8,5 +8,4 @@ lib-y	+= memcopy.o memset.o checksum.o \
 	   divsi3.o udivsi3.o modsi3.o umodsi3.o mulsi3.o umulsidi3.o \
 	   usercopy.o strncpy_user.o strnlen_user.o
 lib-$(CONFIG_PCI) += pci-auto.o
-lib-$(CONFIG_KCSAN) += kcsan-stubs.o
 KCSAN_SANITIZE_kcsan-stubs.o := n
kernel/kcsan/Makefile +2 -0
index 8cf70f068d92..86dd713d8855 100644
@@ -12,6 +12,8 @@ CFLAGS_core.o := $(call cc-option,-fno-conserve-stack) \
 	-fno-stack-protector -DDISABLE_BRANCH_PROFILING
 
 obj-y := core.o debugfs.o report.o
+obj-$(CONFIG_PPC32) += stubs.o
+obj-$(CONFIG_XTENSA) += stubs.o
 
 KCSAN_INSTRUMENT_BARRIERS_selftest.o := y
 obj-$(CONFIG_KCSAN_SELFTEST) += selftest.o
kernel/kcsan/stubs.c renamed from arch/xtensa/lib/kcsan-stubs.c +0 -0
similarity index 100%
rename from arch/xtensa/lib/kcsan-stubs.c
rename to kernel/kcsan/stubs.c
Signature · 1 line
2.37.2
#6 lore raw

Le 16/02/2023 à 06:09, Rohan McLure a écrit :

Quoted 8 lines

KCSAN instruments calls to atomic builtins, and will in turn call these builtins itself. As such, architectures supporting KCSAN must have compiler support for these atomic primitives.

Since 32-bit systems are unlikely to have 64-bit compiler builtins, provide a stub for each missing builtin, and use BUG() to assert unreachability.

Quoted 7 lines

In commit 725aea873261 ("xtensa: enable KCSAN"), xtensa implements these locally. Move these definitions to be accessible to all 32-bit architectures that do not provide the necessary builtins, with opt in for PowerPC and xtensa.

This series should also be addressed to KCSAN Maintainers, shouldn't it ?

KCSAN
M: Marco Elver <[email protected]>
R: Dmitry Vyukov <[email protected]>
L: [email protected]
S: Maintained
F: Documentation/dev-tools/kcsan.rst
F: include/linux/kcsan*.h
F: kernel/kcsan/
F: lib/Kconfig.kcsan
F: scripts/Makefile.kcsan

Quoted 31 lines

---
Previously issued as a part of a patch series adding KCSAN support to 64-bit.

v1: Remove __has_builtin check, as gcc is not obligated to inline builtins detected using this check, but instead is permitted to supply them in libatomic:

Instead, opt-in PPC32 and xtensa.

---
  arch/xtensa/lib/Makefile                              | 1 -
  kernel/kcsan/Makefile                                 | 2 ++
  arch/xtensa/lib/kcsan-stubs.c => kernel/kcsan/stubs.c | 0
  3 files changed, 2 insertions(+), 1 deletion(-)

rename arch/xtensa/lib/kcsan-stubs.c => kernel/kcsan/stubs.c (100%)

arch/xtensa/lib/Makefile +0 -1
index 7ecef0519a27..d69356dc97df 100644
@@ -8,5 +8,4 @@ lib-y	+= memcopy.o memset.o checksum.o \
  	   divsi3.o udivsi3.o modsi3.o umodsi3.o mulsi3.o umulsidi3.o \
  	   usercopy.o strncpy_user.o strnlen_user.o
  lib-$(CONFIG_PCI) += pci-auto.o
-lib-$(CONFIG_KCSAN) += kcsan-stubs.o
  KCSAN_SANITIZE_kcsan-stubs.o := n
kernel/kcsan/Makefile +0 -0
index 8cf70f068d92..86dd713d8855 100644

kernel/kcsan/Makefile CFLAGS_core.o := $(call cc-option,-fno-conserve-stack) \ · line 16

Quoted 5 lines
kernel/kcsan/Makefile +2 -0
@@ -12,6 +12,8 @@ CFLAGS_core.o := $(call cc-option,-fno-conserve-stack) \
  	-fno-stack-protector -DDISABLE_BRANCH_PROFILING
 
  obj-y := core.o debugfs.o report.o
+obj-$(CONFIG_PPC32) += stubs.o
+obj-$(CONFIG_XTENSA) += stubs.o

Not sure it is acceptable to do it that way.

There should likely be something like a CONFIG_ARCH_WANTS_KCSAN_STUBS in KCSAN's Kconfig then PPC32 and XTENSA should select it.

Quoted 7 lines
kernel/kcsan/Makefile +0 -0
  KCSAN_INSTRUMENT_BARRIERS_selftest.o := y
  obj-$(CONFIG_KCSAN_SELFTEST) += selftest.o
kernel/kcsan/stubs.c renamed from arch/xtensa/lib/kcsan-stubs.c +0 -0
similarity index 100%
rename from arch/xtensa/lib/kcsan-stubs.c
rename to kernel/kcsan/stubs.c
#2 lore raw

Re: [PATCH 2/2] powerpc/{32,book3e}: kcsan: Extend KCSAN Support

In reply to Rohan McLure

Le 16/02/2023 à 06:09, Rohan McLure a écrit :

Quoted 18 lines

Enable HAVE_ARCH_KCSAN on all powerpc platforms, permitting use of the kernel concurrency sanitiser through the CONFIG_KCSAN_* kconfig options.

Boots and passes selftests on 32-bit and 64-bit platforms. See documentation in Documentation/dev-tools/kcsan.rst for more information.

---
New patch

---
  arch/powerpc/Kconfig | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/Kconfig +0 -0
index 2c9cdf1d8761..45771448d47a 100644

arch/powerpc/Kconfig config PPC · line 200

Quoted 5 lines
arch/powerpc/Kconfig +1 -1
@@ -197,7 +197,7 @@ config PPC
  	select HAVE_ARCH_KASAN			if PPC_RADIX_MMU
  	select HAVE_ARCH_KASAN			if PPC_BOOK3E_64
  	select HAVE_ARCH_KASAN_VMALLOC		if HAVE_ARCH_KASAN
-	select HAVE_ARCH_KCSAN            	if PPC_BOOK3S_64
+	select HAVE_ARCH_KCSAN

So that's a followup of a not yet posted version v5 of the other series ? Why not just add patch 1 in that series and have KCSAN for all powerpc at once ?

Quoted 3 lines
arch/powerpc/Kconfig +0 -0
  	select HAVE_ARCH_KFENCE			if ARCH_SUPPORTS_DEBUG_PAGEALLOC
  	select HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET
  	select HAVE_ARCH_KGDB
maintainer
#7 lore raw
In reply to Christophe Leroy
Quoted 66 lines · Christophe Leroy

Le 16/02/2023 à 06:09, Rohan McLure a écrit :

Quoted 8 lines

KCSAN instruments calls to atomic builtins, and will in turn call these builtins itself. As such, architectures supporting KCSAN must have compiler support for these atomic primitives.

Since 32-bit systems are unlikely to have 64-bit compiler builtins, provide a stub for each missing builtin, and use BUG() to assert unreachability.

Quoted 7 lines

In commit 725aea873261 ("xtensa: enable KCSAN"), xtensa implements these locally. Move these definitions to be accessible to all 32-bit architectures that do not provide the necessary builtins, with opt in for PowerPC and xtensa.

This series should also be addressed to KCSAN Maintainers, shouldn't it ?

KCSAN
M: Marco Elver <[email protected]>
R: Dmitry Vyukov <[email protected]>
L: [email protected]
S: Maintained
F: Documentation/dev-tools/kcsan.rst
F: include/linux/kcsan*.h
F: kernel/kcsan/
F: lib/Kconfig.kcsan
F: scripts/Makefile.kcsan

Quoted 33 lines

---
Previously issued as a part of a patch series adding KCSAN support to 64-bit.

v1: Remove __has_builtin check, as gcc is not obligated to inline builtins detected using this check, but instead is permitted to supply them in libatomic:

Instead, opt-in PPC32 and xtensa.

---
  arch/xtensa/lib/Makefile                              | 1 -
  kernel/kcsan/Makefile                                 | 2 ++
  arch/xtensa/lib/kcsan-stubs.c => kernel/kcsan/stubs.c | 0
  3 files changed, 2 insertions(+), 1 deletion(-)

rename arch/xtensa/lib/kcsan-stubs.c => kernel/kcsan/stubs.c (100%)

arch/xtensa/lib/Makefile +0 -1
index 7ecef0519a27..d69356dc97df 100644
@@ -8,5 +8,4 @@ lib-y	+= memcopy.o memset.o checksum.o \
  	   divsi3.o udivsi3.o modsi3.o umodsi3.o mulsi3.o umulsidi3.o \
  	   usercopy.o strncpy_user.o strnlen_user.o
  lib-$(CONFIG_PCI) += pci-auto.o
-lib-$(CONFIG_KCSAN) += kcsan-stubs.o
  KCSAN_SANITIZE_kcsan-stubs.o := n
kernel/kcsan/Makefile +0 -0
index 8cf70f068d92..86dd713d8855 100644
@@ -12,6 +12,8 @@ CFLAGS_core.o := $(call cc-option,-fno-conserve-stack) \
  	-fno-stack-protector -DDISABLE_BRANCH_PROFILING
 
Quoted 8 lines

kernel/kcsan/Makefile CFLAGS_core.o := $(call cc-option,-fno-conserve-stack) \ · line 16

Quoted 3 lines
kernel/kcsan/Makefile +2 -0
  obj-y := core.o debugfs.o report.o
+obj-$(CONFIG_PPC32) += stubs.o
+obj-$(CONFIG_XTENSA) += stubs.o

Not sure it is acceptable to do it that way.

There should likely be something like a CONFIG_ARCH_WANTS_KCSAN_STUBS in KCSAN's Kconfig then PPC32 and XTENSA should select it.

The longer I think about it, since these stubs all BUG() anyway, perhaps we ought to just avoid them altogether. If you delete all the stubs from ppc and xtensa, but do this:

 | diff --git a/kernel/kcsan/core.c b/kernel/kcsan/core.c
 | index 54d077e1a2dc..8169d6dadd0e 100644
 | --- a/kernel/kcsan/core.c
 | +++ b/kernel/kcsan/core.c
 | @@ -1261,7 +1261,9 @@ static __always_inline void kcsan_atomic_builtin_memorder(int memorder)
 |  DEFINE_TSAN_ATOMIC_OPS(8);
 |  DEFINE_TSAN_ATOMIC_OPS(16);
 |  DEFINE_TSAN_ATOMIC_OPS(32);
 | +#ifdef CONFIG_64BIT
 |  DEFINE_TSAN_ATOMIC_OPS(64);
 | +#endif
 |  
 |  void __tsan_atomic_thread_fence(int memorder);
 |  void __tsan_atomic_thread_fence(int memorder)

Does that work?

#3 lore raw

Re: [PATCH 2/2] powerpc/{32,book3e}: kcsan: Extend KCSAN Support

In reply to Christophe Leroy
Quoted 18 lines · Christophe Leroy
Quoted 13 lines · Rohan McLure

Enable HAVE_ARCH_KCSAN on all powerpc platforms, permitting use of the kernel concurrency sanitiser through the CONFIG_KCSAN_* kconfig options.

Boots and passes selftests on 32-bit and 64-bit platforms. See documentation in Documentation/dev-tools/kcsan.rst for more information.

---
New patch

---
 arch/powerpc/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

arch/powerpc/Kconfig config PPC · line 200

Quoted 10 lines
arch/powerpc/Kconfig +1 -1
index 2c9cdf1d8761..45771448d47a 100644
@@ -197,7 +197,7 @@ config PPC
  select HAVE_ARCH_KASAN if PPC_RADIX_MMU
  select HAVE_ARCH_KASAN if PPC_BOOK3E_64
  select HAVE_ARCH_KASAN_VMALLOC if HAVE_ARCH_KASAN
- select HAVE_ARCH_KCSAN             if PPC_BOOK3S_64
+ select HAVE_ARCH_KCSAN

So that's a followup of a not yet posted version v5 of the other series ? Why not just add patch 1 in that series and have KCSAN for all powerpc at once ?

So the v3 was accepted upstream, likely to appear in 6.3. This patch series is just to extend support to other platforms, once kcsan supports us.

Quoted 4 lines
Quoted 4 lines
arch/powerpc/Kconfig +0 -0
  select HAVE_ARCH_KFENCE if ARCH_SUPPORTS_DEBUG_PAGEALLOC
  select HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET
  select HAVE_ARCH_KGDB
#8 lore raw
In reply to Marco Elver
Quoted 97 lines · Marco Elver
Quoted 66 lines · Christophe Leroy
Quoted 8 lines · Rohan McLure

KCSAN instruments calls to atomic builtins, and will in turn call these builtins itself. As such, architectures supporting KCSAN must have compiler support for these atomic primitives.

Since 32-bit systems are unlikely to have 64-bit compiler builtins, provide a stub for each missing builtin, and use BUG() to assert unreachability.

Quoted 7 lines

In commit 725aea873261 ("xtensa: enable KCSAN"), xtensa implements these locally. Move these definitions to be accessible to all 32-bit architectures that do not provide the necessary builtins, with opt in for PowerPC and xtensa.

This series should also be addressed to KCSAN Maintainers, shouldn't it ?

KCSAN
M: Marco Elver <[email protected]>
R: Dmitry Vyukov <[email protected]>
L: [email protected]
S: Maintained
F: Documentation/dev-tools/kcsan.rst
F: include/linux/kcsan*.h
F: kernel/kcsan/
F: lib/Kconfig.kcsan
F: scripts/Makefile.kcsan

Quoted 33 lines

---
Previously issued as a part of a patch series adding KCSAN support to 64-bit.

v1: Remove __has_builtin check, as gcc is not obligated to inline builtins detected using this check, but instead is permitted to supply them in libatomic:

Instead, opt-in PPC32 and xtensa.

---
 arch/xtensa/lib/Makefile                              | 1 -
 kernel/kcsan/Makefile                                 | 2 ++
 arch/xtensa/lib/kcsan-stubs.c => kernel/kcsan/stubs.c | 0
 3 files changed, 2 insertions(+), 1 deletion(-)
 rename arch/xtensa/lib/kcsan-stubs.c => kernel/kcsan/stubs.c (100%)
arch/xtensa/lib/Makefile +0 -1
index 7ecef0519a27..d69356dc97df 100644
@@ -8,5 +8,4 @@ lib-y += memcopy.o memset.o checksum.o \
     divsi3.o udivsi3.o modsi3.o umodsi3.o mulsi3.o umulsidi3.o \
     usercopy.o strncpy_user.o strnlen_user.o
 lib-$(CONFIG_PCI) += pci-auto.o
-lib-$(CONFIG_KCSAN) += kcsan-stubs.o
 KCSAN_SANITIZE_kcsan-stubs.o := n
kernel/kcsan/Makefile +0 -0
index 8cf70f068d92..86dd713d8855 100644
@@ -12,6 +12,8 @@ CFLAGS_core.o := $(call cc-option,-fno-conserve-stack) \
  -fno-stack-protector -DDISABLE_BRANCH_PROFILING
 
Quoted 8 lines

kernel/kcsan/Makefile CFLAGS_core.o := $(call cc-option,-fno-conserve-stack) \ · line 16

Quoted 3 lines
kernel/kcsan/Makefile +2 -0
 obj-y := core.o debugfs.o report.o
+obj-$(CONFIG_PPC32) += stubs.o
+obj-$(CONFIG_XTENSA) += stubs.o

Not sure it is acceptable to do it that way.

There should likely be something like a CONFIG_ARCH_WANTS_KCSAN_STUBS in KCSAN's Kconfig then PPC32 and XTENSA should select it.

The longer I think about it, since these stubs all BUG() anyway, perhaps we ought to just avoid them altogether. If you delete all the stubs from ppc and xtensa, but do this:

| diff --git a/kernel/kcsan/core.c b/kernel/kcsan/core.c
| index 54d077e1a2dc..8169d6dadd0e 100644
| --- a/kernel/kcsan/core.c
| +++ b/kernel/kcsan/core.c
| @@ -1261,7 +1261,9 @@ static __always_inline void kcsan_atomic_builtin_memorder(int memorder)
|  DEFINE_TSAN_ATOMIC_OPS(8);
|  DEFINE_TSAN_ATOMIC_OPS(16);
|  DEFINE_TSAN_ATOMIC_OPS(32);
| +#ifdef CONFIG_64BIT
|  DEFINE_TSAN_ATOMIC_OPS(64);
| +#endif
|  
|  void __tsan_atomic_thread_fence(int memorder);
|  void __tsan_atomic_thread_fence(int memorder)
Quoted 1 line

Does that work?

This makes much more sense. Rather than assume that kcsan is the only consumer of __atomic_*_8, and stubbing accordingly, we should just remove its mention from relevant sub-archs.

#4 lore raw

Re: [PATCH 2/2] powerpc/{32,book3e}: kcsan: Extend KCSAN Support

In reply to Rohan McLure

Le 17/02/2023 à 00:23, Rohan McLure a écrit :

Quoted 33 lines
Quoted 18 lines · Christophe Leroy
Quoted 13 lines · Rohan McLure

Enable HAVE_ARCH_KCSAN on all powerpc platforms, permitting use of the kernel concurrency sanitiser through the CONFIG_KCSAN_* kconfig options.

Boots and passes selftests on 32-bit and 64-bit platforms. See documentation in Documentation/dev-tools/kcsan.rst for more information.

---
New patch

---
  arch/powerpc/Kconfig | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

arch/powerpc/Kconfig config PPC · line 200

Quoted 10 lines
arch/powerpc/Kconfig +1 -1
index 2c9cdf1d8761..45771448d47a 100644
@@ -197,7 +197,7 @@ config PPC
   select HAVE_ARCH_KASAN if PPC_RADIX_MMU
   select HAVE_ARCH_KASAN if PPC_BOOK3E_64
   select HAVE_ARCH_KASAN_VMALLOC if HAVE_ARCH_KASAN
- select HAVE_ARCH_KCSAN             if PPC_BOOK3S_64
+ select HAVE_ARCH_KCSAN

So that's a followup of a not yet posted version v5 of the other series ? Why not just add patch 1 in that series and have KCSAN for all powerpc at once ?

Quoted 2 lines

So the v3 was accepted upstream, likely to appear in 6.3. This patch series is just to extend support to other platforms, once kcsan supports us.

Hum ... Ok.

I checked in checkpatch before writting that mail and saw that v4 was flagged "changes requested", so I didn't notice v3 was accepted.

Quoted 9 lines
Quoted 4 lines
Quoted 4 lines
arch/powerpc/Kconfig +0 -0
   select HAVE_ARCH_KFENCE if ARCH_SUPPORTS_DEBUG_PAGEALLOC
   select HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET
   select HAVE_ARCH_KGDB
#5 lore raw

Re: [PATCH 2/2] powerpc/{32,book3e}: kcsan: Extend KCSAN Support

Quoted 31 lines · Christophe Leroy

Le 17/02/2023 à 00:23, Rohan McLure a écrit :

Quoted 30 lines
Quoted 29 lines · Christophe Leroy
Quoted 18 lines · Rohan McLure

Enable HAVE_ARCH_KCSAN on all powerpc platforms, permitting use of the kernel concurrency sanitiser through the CONFIG_KCSAN_* kconfig options.

Boots and passes selftests on 32-bit and 64-bit platforms. See documentation in Documentation/dev-tools/kcsan.rst for more information.

---
New patch

---
  arch/powerpc/Kconfig | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/Kconfig +0 -0
index 2c9cdf1d8761..45771448d47a 100644

arch/powerpc/Kconfig config PPC · line 200

Quoted 5 lines
arch/powerpc/Kconfig +1 -1
@@ -197,7 +197,7 @@ config PPC
   select HAVE_ARCH_KASAN if PPC_RADIX_MMU
   select HAVE_ARCH_KASAN if PPC_BOOK3E_64
   select HAVE_ARCH_KASAN_VMALLOC if HAVE_ARCH_KASAN
- select HAVE_ARCH_KCSAN             if PPC_BOOK3S_64
+ select HAVE_ARCH_KCSAN

So that's a followup of a not yet posted version v5 of the other series ? Why not just add patch 1 in that series and have KCSAN for all powerpc at once ?

Quoted 7 lines
Quoted 2 lines

So the v3 was accepted upstream, likely to appear in 6.3. This patch series is just to extend support to other platforms, once kcsan supports us.

Hum ... Ok.

I checked in checkpatch before writting that mail and saw that v4 was flagged "changes requested", so I didn't notice v3 was accepted.

Sorry that's my fault. I talked to Rohan on chat and decided that taking v3 was the best way to get something in before the merge window closed.

cheers