LKML Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH v2] kcsan, trace: Make KCSAN compatible with tracing
@ 2020-02-14 21:10 Marco Elver
  2020-02-14 23:40 ` Paul E. McKenney
  0 siblings, 1 reply; 4+ messages in thread
From: Marco Elver @ 2020-02-14 21:10 UTC (permalink / raw)
  To: elver
  Cc: paulmck, andreyknvl, glider, dvyukov, kasan-dev, linux-kernel,
	rostedt, mingo, x86, Qian Cai

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.

Furthermore, since KCSAN relies on udelay() to introduce delay, we have
to disable ftrace for udelay() (currently done for x86) in case KCSAN is
used together with lockdep and ftrace. The reason is that it may corrupt
lockdep IRQ flags tracing state due to a peculiar case of recursion
(details in Makefile comment).

Signed-off-by: Marco Elver <elver@google.com>
Reported-by: Qian Cai <cai@lca.pw>
Cc: Paul E. McKenney <paulmck@kernel.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
---
v2:
*  Fix KCSAN+lockdep+ftrace compatibility.
---
 arch/x86/lib/Makefile | 5 +++++
 kernel/kcsan/Makefile | 2 ++
 kernel/trace/Makefile | 3 +++
 3 files changed, 10 insertions(+)

diff --git a/arch/x86/lib/Makefile b/arch/x86/lib/Makefile
index 432a077056775..6110bce7237bd 100644
--- a/arch/x86/lib/Makefile
+++ b/arch/x86/lib/Makefile
@@ -8,6 +8,11 @@ KCOV_INSTRUMENT_delay.o	:= n
 
 # KCSAN uses udelay for introducing watchpoint delay; avoid recursion.
 KCSAN_SANITIZE_delay.o := n
+ifdef CONFIG_KCSAN
+# In case KCSAN+lockdep+ftrace are enabled, disable ftrace for delay.o to avoid
+# lockdep -> [other libs] -> KCSAN -> udelay -> ftrace -> lockdep recursion.
+CFLAGS_REMOVE_delay.o = $(CC_FLAGS_FTRACE)
+endif
 
 # Early boot use of cmdline; don't instrument it
 ifdef CONFIG_AMD_MEM_ENCRYPT
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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] kcsan, trace: Make KCSAN compatible with tracing
  2020-02-14 21:10 [PATCH v2] kcsan, trace: Make KCSAN compatible with tracing Marco Elver
@ 2020-02-14 23:40 ` Paul E. McKenney
  2020-02-17 17:06   ` Qian Cai
  0 siblings, 1 reply; 4+ messages in thread
From: Paul E. McKenney @ 2020-02-14 23:40 UTC (permalink / raw)
  To: Marco Elver
  Cc: andreyknvl, glider, dvyukov, kasan-dev, linux-kernel, rostedt,
	mingo, x86, Qian Cai

On Fri, Feb 14, 2020 at 10:10:35PM +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.
> 
> Furthermore, since KCSAN relies on udelay() to introduce delay, we have
> to disable ftrace for udelay() (currently done for x86) in case KCSAN is
> used together with lockdep and ftrace. The reason is that it may corrupt
> lockdep IRQ flags tracing state due to a peculiar case of recursion
> (details in Makefile comment).
> 
> Signed-off-by: Marco Elver <elver@google.com>
> Reported-by: Qian Cai <cai@lca.pw>
> Cc: Paul E. McKenney <paulmck@kernel.org>
> Cc: Steven Rostedt <rostedt@goodmis.org>

Queued for review and further testing, thank you!

Qian, does this also fix things for you?

							Thanx, Paul

> ---
> v2:
> *  Fix KCSAN+lockdep+ftrace compatibility.
> ---
>  arch/x86/lib/Makefile | 5 +++++
>  kernel/kcsan/Makefile | 2 ++
>  kernel/trace/Makefile | 3 +++
>  3 files changed, 10 insertions(+)
> 
> diff --git a/arch/x86/lib/Makefile b/arch/x86/lib/Makefile
> index 432a077056775..6110bce7237bd 100644
> --- a/arch/x86/lib/Makefile
> +++ b/arch/x86/lib/Makefile
> @@ -8,6 +8,11 @@ KCOV_INSTRUMENT_delay.o	:= n
>  
>  # KCSAN uses udelay for introducing watchpoint delay; avoid recursion.
>  KCSAN_SANITIZE_delay.o := n
> +ifdef CONFIG_KCSAN
> +# In case KCSAN+lockdep+ftrace are enabled, disable ftrace for delay.o to avoid
> +# lockdep -> [other libs] -> KCSAN -> udelay -> ftrace -> lockdep recursion.
> +CFLAGS_REMOVE_delay.o = $(CC_FLAGS_FTRACE)
> +endif
>  
>  # Early boot use of cmdline; don't instrument it
>  ifdef CONFIG_AMD_MEM_ENCRYPT
> 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
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] kcsan, trace: Make KCSAN compatible with tracing
  2020-02-14 23:40 ` Paul E. McKenney
@ 2020-02-17 17:06   ` Qian Cai
  2020-02-17 23:14     ` Paul E. McKenney
  0 siblings, 1 reply; 4+ messages in thread
From: Qian Cai @ 2020-02-17 17:06 UTC (permalink / raw)
  To: paulmck, Marco Elver
  Cc: andreyknvl, glider, dvyukov, kasan-dev, linux-kernel, rostedt,
	mingo, x86

On Fri, 2020-02-14 at 15:40 -0800, Paul E. McKenney wrote:
> On Fri, Feb 14, 2020 at 10:10:35PM +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.
> > 
> > Furthermore, since KCSAN relies on udelay() to introduce delay, we have
> > to disable ftrace for udelay() (currently done for x86) in case KCSAN is
> > used together with lockdep and ftrace. The reason is that it may corrupt
> > lockdep IRQ flags tracing state due to a peculiar case of recursion
> > (details in Makefile comment).
> > 
> > Signed-off-by: Marco Elver <elver@google.com>
> > Reported-by: Qian Cai <cai@lca.pw>
> > Cc: Paul E. McKenney <paulmck@kernel.org>
> > Cc: Steven Rostedt <rostedt@goodmis.org>
> 
> Queued for review and further testing, thank you!
> 
> Qian, does this also fix things for you?

It works fine. Feel free to use,

Tested-by: Qian Cai <cai@lca.pw>

> 
> 							Thanx, Paul
> 
> > ---
> > v2:
> > *  Fix KCSAN+lockdep+ftrace compatibility.
> > ---
> >  arch/x86/lib/Makefile | 5 +++++
> >  kernel/kcsan/Makefile | 2 ++
> >  kernel/trace/Makefile | 3 +++
> >  3 files changed, 10 insertions(+)
> > 
> > diff --git a/arch/x86/lib/Makefile b/arch/x86/lib/Makefile
> > index 432a077056775..6110bce7237bd 100644
> > --- a/arch/x86/lib/Makefile
> > +++ b/arch/x86/lib/Makefile
> > @@ -8,6 +8,11 @@ KCOV_INSTRUMENT_delay.o	:= n
> >  
> >  # KCSAN uses udelay for introducing watchpoint delay; avoid recursion.
> >  KCSAN_SANITIZE_delay.o := n
> > +ifdef CONFIG_KCSAN
> > +# In case KCSAN+lockdep+ftrace are enabled, disable ftrace for delay.o to avoid
> > +# lockdep -> [other libs] -> KCSAN -> udelay -> ftrace -> lockdep recursion.
> > +CFLAGS_REMOVE_delay.o = $(CC_FLAGS_FTRACE)
> > +endif
> >  
> >  # Early boot use of cmdline; don't instrument it
> >  ifdef CONFIG_AMD_MEM_ENCRYPT
> > 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
> > 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] kcsan, trace: Make KCSAN compatible with tracing
  2020-02-17 17:06   ` Qian Cai
@ 2020-02-17 23:14     ` Paul E. McKenney
  0 siblings, 0 replies; 4+ messages in thread
From: Paul E. McKenney @ 2020-02-17 23:14 UTC (permalink / raw)
  To: Qian Cai
  Cc: Marco Elver, andreyknvl, glider, dvyukov, kasan-dev,
	linux-kernel, rostedt, mingo, x86

On Mon, Feb 17, 2020 at 12:06:14PM -0500, Qian Cai wrote:
> On Fri, 2020-02-14 at 15:40 -0800, Paul E. McKenney wrote:
> > On Fri, Feb 14, 2020 at 10:10:35PM +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.
> > > 
> > > Furthermore, since KCSAN relies on udelay() to introduce delay, we have
> > > to disable ftrace for udelay() (currently done for x86) in case KCSAN is
> > > used together with lockdep and ftrace. The reason is that it may corrupt
> > > lockdep IRQ flags tracing state due to a peculiar case of recursion
> > > (details in Makefile comment).
> > > 
> > > Signed-off-by: Marco Elver <elver@google.com>
> > > Reported-by: Qian Cai <cai@lca.pw>
> > > Cc: Paul E. McKenney <paulmck@kernel.org>
> > > Cc: Steven Rostedt <rostedt@goodmis.org>
> > 
> > Queued for review and further testing, thank you!
> > 
> > Qian, does this also fix things for you?
> 
> It works fine. Feel free to use,
> 
> Tested-by: Qian Cai <cai@lca.pw>

Applied, thank you!

							Thanx, Paul

> > > ---
> > > v2:
> > > *  Fix KCSAN+lockdep+ftrace compatibility.
> > > ---
> > >  arch/x86/lib/Makefile | 5 +++++
> > >  kernel/kcsan/Makefile | 2 ++
> > >  kernel/trace/Makefile | 3 +++
> > >  3 files changed, 10 insertions(+)
> > > 
> > > diff --git a/arch/x86/lib/Makefile b/arch/x86/lib/Makefile
> > > index 432a077056775..6110bce7237bd 100644
> > > --- a/arch/x86/lib/Makefile
> > > +++ b/arch/x86/lib/Makefile
> > > @@ -8,6 +8,11 @@ KCOV_INSTRUMENT_delay.o	:= n
> > >  
> > >  # KCSAN uses udelay for introducing watchpoint delay; avoid recursion.
> > >  KCSAN_SANITIZE_delay.o := n
> > > +ifdef CONFIG_KCSAN
> > > +# In case KCSAN+lockdep+ftrace are enabled, disable ftrace for delay.o to avoid
> > > +# lockdep -> [other libs] -> KCSAN -> udelay -> ftrace -> lockdep recursion.
> > > +CFLAGS_REMOVE_delay.o = $(CC_FLAGS_FTRACE)
> > > +endif
> > >  
> > >  # Early boot use of cmdline; don't instrument it
> > >  ifdef CONFIG_AMD_MEM_ENCRYPT
> > > 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
> > > 

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-14 21:10 [PATCH v2] kcsan, trace: Make KCSAN compatible with tracing Marco Elver
2020-02-14 23:40 ` Paul E. McKenney
2020-02-17 17:06   ` Qian Cai
2020-02-17 23:14     ` Paul E. McKenney

LKML Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/lkml/0 lkml/git/0.git
	git clone --mirror https://lore.kernel.org/lkml/1 lkml/git/1.git
	git clone --mirror https://lore.kernel.org/lkml/2 lkml/git/2.git
	git clone --mirror https://lore.kernel.org/lkml/3 lkml/git/3.git
	git clone --mirror https://lore.kernel.org/lkml/4 lkml/git/4.git
	git clone --mirror https://lore.kernel.org/lkml/5 lkml/git/5.git
	git clone --mirror https://lore.kernel.org/lkml/6 lkml/git/6.git
	git clone --mirror https://lore.kernel.org/lkml/7 lkml/git/7.git
	git clone --mirror https://lore.kernel.org/lkml/8 lkml/git/8.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 lkml lkml/ https://lore.kernel.org/lkml \
		linux-kernel@vger.kernel.org
	public-inbox-index lkml

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-kernel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git