diff mbox series

[RFC,v4,02/42] stackdepot: build with -fno-builtin

Message ID 20191220184955.223741-3-glider@google.com (mailing list archive)
State New, archived
Headers show
Series Add KernelMemorySanitizer infrastructure | expand

Commit Message

Alexander Potapenko Dec. 20, 2019, 6:49 p.m. UTC
Clang may replace stackdepot_memcmp() with a call to instrumented bcmp(),
which is exactly what we wanted to avoid creating stackdepot_memcmp().
Building the file with -fno-builtin prevents such optimizations.

Signed-off-by: Alexander Potapenko <glider@google.com>
To: Alexander Potapenko <glider@google.com>
Cc: Vegard Nossum <vegard.nossum@oracle.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Marco Elver <elver@google.com>
Cc: Andrey Konovalov <andreyknvl@google.com>
Cc: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: linux-mm@kvack.org
---
This patch was previously called "stackdepot: prevent Clang from optimizing
away stackdepot_memcmp()".

v3:
 - use -fno-builtin instead of a barrier

Change-Id: I4495b617b15c0ab003a61c1f0d54d0026fa8b144
---
 lib/Makefile     | 4 ++++
 lib/stackdepot.c | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)

Comments

Steven Rostedt Jan. 3, 2020, 5:37 p.m. UTC | #1
On Fri, 20 Dec 2019 19:49:15 +0100
glider@google.com wrote:

> Clang may replace stackdepot_memcmp() with a call to instrumented bcmp(),
> which is exactly what we wanted to avoid creating stackdepot_memcmp().
> Building the file with -fno-builtin prevents such optimizations.
> 
> Signed-off-by: Alexander Potapenko <glider@google.com>
> To: Alexander Potapenko <glider@google.com>
> Cc: Vegard Nossum <vegard.nossum@oracle.com>
> Cc: Dmitry Vyukov <dvyukov@google.com>
> Cc: Marco Elver <elver@google.com>
> Cc: Andrey Konovalov <andreyknvl@google.com>
> Cc: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
> Cc: Arnd Bergmann <arnd@arndb.de>
> Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
> Cc: linux-mm@kvack.org
> ---
> This patch was previously called "stackdepot: prevent Clang from optimizing
> away stackdepot_memcmp()".
> 
> v3:
>  - use -fno-builtin instead of a barrier
> 
> Change-Id: I4495b617b15c0ab003a61c1f0d54d0026fa8b144
> ---
>  lib/Makefile     | 4 ++++
>  lib/stackdepot.c | 2 +-
>  2 files changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/Makefile b/lib/Makefile
> index 93217d44237f..066a6649e929 100644
> --- a/lib/Makefile
> +++ b/lib/Makefile
> @@ -218,6 +218,10 @@ obj-$(CONFIG_MEMREGION) += memregion.o
>  obj-$(CONFIG_STMP_DEVICE) += stmp_device.o
>  obj-$(CONFIG_IRQ_POLL) += irq_poll.o
>  
> +# stackdepot.c should not be instrumented or call instrumented functions.
> +# Prevent the compiler from calling builtins like memcmp() or bcmp() from this
> +# file.
> +CFLAGS_stackdepot.o += -fno-builtin
>  obj-$(CONFIG_STACKDEPOT) += stackdepot.o
>  KASAN_SANITIZE_stackdepot.o := n
>  KCOV_INSTRUMENT_stackdepot.o := n
> diff --git a/lib/stackdepot.c b/lib/stackdepot.c
> index 1d3aab244897..9d33059d874c 100644
> --- a/lib/stackdepot.c
> +++ b/lib/stackdepot.c
> @@ -163,7 +163,7 @@ int stackdepot_memcmp(const unsigned long *u1, const unsigned long *u2,
>  			unsigned int n)
>  {
>  	for ( ; n-- ; u1++, u2++) {
> -		if (*u1 != *u2)
> +		if ((*u1) != (*u2))

This change isn't mentioned in the change log.

-- Steve

>  			return 1;
>  	}
>  	return 0;
diff mbox series

Patch

diff --git a/lib/Makefile b/lib/Makefile
index 93217d44237f..066a6649e929 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -218,6 +218,10 @@  obj-$(CONFIG_MEMREGION) += memregion.o
 obj-$(CONFIG_STMP_DEVICE) += stmp_device.o
 obj-$(CONFIG_IRQ_POLL) += irq_poll.o
 
+# stackdepot.c should not be instrumented or call instrumented functions.
+# Prevent the compiler from calling builtins like memcmp() or bcmp() from this
+# file.
+CFLAGS_stackdepot.o += -fno-builtin
 obj-$(CONFIG_STACKDEPOT) += stackdepot.o
 KASAN_SANITIZE_stackdepot.o := n
 KCOV_INSTRUMENT_stackdepot.o := n
diff --git a/lib/stackdepot.c b/lib/stackdepot.c
index 1d3aab244897..9d33059d874c 100644
--- a/lib/stackdepot.c
+++ b/lib/stackdepot.c
@@ -163,7 +163,7 @@  int stackdepot_memcmp(const unsigned long *u1, const unsigned long *u2,
 			unsigned int n)
 {
 	for ( ; n-- ; u1++, u2++) {
-		if (*u1 != *u2)
+		if ((*u1) != (*u2))
 			return 1;
 	}
 	return 0;