2020-02-14 19:05:28

by Marco Elver

[permalink] [raw]
Subject: [PATCH] kcsan, trace: Make KCSAN compatible with tracing

Previously the system would lock up if ftrace was enabled together with
KCSAN. This is due to recursion on reporting if the tracer code is
instrumented with KCSAN.

To avoid this for all types of tracing, disable KCSAN instrumentation
for all of kernel/trace.

Signed-off-by: Marco Elver <[email protected]>
Reported-by: Qian Cai <[email protected]>
Cc: Paul E. McKenney <[email protected]>
Cc: Steven Rostedt <[email protected]>
---
kernel/kcsan/Makefile | 2 ++
kernel/trace/Makefile | 3 +++
2 files changed, 5 insertions(+)

diff --git a/kernel/kcsan/Makefile b/kernel/kcsan/Makefile
index df6b7799e4927..d4999b38d1be5 100644
--- a/kernel/kcsan/Makefile
+++ b/kernel/kcsan/Makefile
@@ -4,6 +4,8 @@ KCOV_INSTRUMENT := n
UBSAN_SANITIZE := n

CFLAGS_REMOVE_core.o = $(CC_FLAGS_FTRACE)
+CFLAGS_REMOVE_debugfs.o = $(CC_FLAGS_FTRACE)
+CFLAGS_REMOVE_report.o = $(CC_FLAGS_FTRACE)

CFLAGS_core.o := $(call cc-option,-fno-conserve-stack,) \
$(call cc-option,-fno-stack-protector,)
diff --git a/kernel/trace/Makefile b/kernel/trace/Makefile
index f9dcd19165fa2..6b601d88bf71e 100644
--- a/kernel/trace/Makefile
+++ b/kernel/trace/Makefile
@@ -6,6 +6,9 @@ ifdef CONFIG_FUNCTION_TRACER
ORIG_CFLAGS := $(KBUILD_CFLAGS)
KBUILD_CFLAGS = $(subst $(CC_FLAGS_FTRACE),,$(ORIG_CFLAGS))

+# Avoid recursion due to instrumentation.
+KCSAN_SANITIZE := n
+
ifdef CONFIG_FTRACE_SELFTEST
# selftest needs instrumentation
CFLAGS_trace_selftest_dynamic.o = $(CC_FLAGS_FTRACE)
--
2.25.0.265.gbab2e86ba0-goog


2020-02-14 19:36:37

by Qian Cai

[permalink] [raw]
Subject: Re: [PATCH] kcsan, trace: Make KCSAN compatible with tracing

On Fri, 2020-02-14 at 20:05 +0100, Marco Elver wrote:
> Previously the system would lock up if ftrace was enabled together with
> KCSAN. This is due to recursion on reporting if the tracer code is
> instrumented with KCSAN.
>
> To avoid this for all types of tracing, disable KCSAN instrumentation
> for all of kernel/trace.

I remembered that KCSAN + ftrace was working last week, but I probably had a bad
memory. Anyway, this patch works fine. Feel free to add,

Tested-by: Qian Cai <[email protected]>

>
> Signed-off-by: Marco Elver <[email protected]>
> Reported-by: Qian Cai <[email protected]>
> Cc: Paul E. McKenney <[email protected]>
> Cc: Steven Rostedt <[email protected]>
> ---
> kernel/kcsan/Makefile | 2 ++
> kernel/trace/Makefile | 3 +++
> 2 files changed, 5 insertions(+)
>
> diff --git a/kernel/kcsan/Makefile b/kernel/kcsan/Makefile
> index df6b7799e4927..d4999b38d1be5 100644
> --- a/kernel/kcsan/Makefile
> +++ b/kernel/kcsan/Makefile
> @@ -4,6 +4,8 @@ KCOV_INSTRUMENT := n
> UBSAN_SANITIZE := n
>
> CFLAGS_REMOVE_core.o = $(CC_FLAGS_FTRACE)
> +CFLAGS_REMOVE_debugfs.o = $(CC_FLAGS_FTRACE)
> +CFLAGS_REMOVE_report.o = $(CC_FLAGS_FTRACE)
>
> CFLAGS_core.o := $(call cc-option,-fno-conserve-stack,) \
> $(call cc-option,-fno-stack-protector,)
> diff --git a/kernel/trace/Makefile b/kernel/trace/Makefile
> index f9dcd19165fa2..6b601d88bf71e 100644
> --- a/kernel/trace/Makefile
> +++ b/kernel/trace/Makefile
> @@ -6,6 +6,9 @@ ifdef CONFIG_FUNCTION_TRACER
> ORIG_CFLAGS := $(KBUILD_CFLAGS)
> KBUILD_CFLAGS = $(subst $(CC_FLAGS_FTRACE),,$(ORIG_CFLAGS))
>
> +# Avoid recursion due to instrumentation.
> +KCSAN_SANITIZE := n
> +
> ifdef CONFIG_FTRACE_SELFTEST
> # selftest needs instrumentation
> CFLAGS_trace_selftest_dynamic.o = $(CC_FLAGS_FTRACE)

2020-02-14 22:00:13

by Marco Elver

[permalink] [raw]
Subject: Re: [PATCH] kcsan, trace: Make KCSAN compatible with tracing

On Fri, 14 Feb 2020 at 20:35, Qian Cai <[email protected]> wrote:
>
> On Fri, 2020-02-14 at 20:05 +0100, Marco Elver wrote:
> > Previously the system would lock up if ftrace was enabled together with
> > KCSAN. This is due to recursion on reporting if the tracer code is
> > instrumented with KCSAN.
> >
> > To avoid this for all types of tracing, disable KCSAN instrumentation
> > for all of kernel/trace.
>
> I remembered that KCSAN + ftrace was working last week, but I probably had a bad
> memory. Anyway, this patch works fine. Feel free to add,
>
> Tested-by: Qian Cai <[email protected]>

Based your further feedback I've sent v2:
http://lkml.kernel.org/r/[email protected]

Thanks,
-- Marco

> >
> > Signed-off-by: Marco Elver <[email protected]>
> > Reported-by: Qian Cai <[email protected]>
> > Cc: Paul E. McKenney <[email protected]>
> > Cc: Steven Rostedt <[email protected]>
> > ---
> > kernel/kcsan/Makefile | 2 ++
> > kernel/trace/Makefile | 3 +++
> > 2 files changed, 5 insertions(+)
> >
> > diff --git a/kernel/kcsan/Makefile b/kernel/kcsan/Makefile
> > index df6b7799e4927..d4999b38d1be5 100644
> > --- a/kernel/kcsan/Makefile
> > +++ b/kernel/kcsan/Makefile
> > @@ -4,6 +4,8 @@ KCOV_INSTRUMENT := n
> > UBSAN_SANITIZE := n
> >
> > CFLAGS_REMOVE_core.o = $(CC_FLAGS_FTRACE)
> > +CFLAGS_REMOVE_debugfs.o = $(CC_FLAGS_FTRACE)
> > +CFLAGS_REMOVE_report.o = $(CC_FLAGS_FTRACE)
> >
> > CFLAGS_core.o := $(call cc-option,-fno-conserve-stack,) \
> > $(call cc-option,-fno-stack-protector,)
> > diff --git a/kernel/trace/Makefile b/kernel/trace/Makefile
> > index f9dcd19165fa2..6b601d88bf71e 100644
> > --- a/kernel/trace/Makefile
> > +++ b/kernel/trace/Makefile
> > @@ -6,6 +6,9 @@ ifdef CONFIG_FUNCTION_TRACER
> > ORIG_CFLAGS := $(KBUILD_CFLAGS)
> > KBUILD_CFLAGS = $(subst $(CC_FLAGS_FTRACE),,$(ORIG_CFLAGS))
> >
> > +# Avoid recursion due to instrumentation.
> > +KCSAN_SANITIZE := n
> > +
> > ifdef CONFIG_FTRACE_SELFTEST
> > # selftest needs instrumentation
> > CFLAGS_trace_selftest_dynamic.o = $(CC_FLAGS_FTRACE)