linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] kcsan: Add __kcsan_{enable,disable}_current() variants
@ 2020-04-24 15:47 Marco Elver
  2020-04-24 15:47 ` [PATCH 2/2] objtool, kcsan: Add kcsan_disable_current() and kcsan_enable_current_nowarn() Marco Elver
  2020-04-24 15:57 ` [PATCH 1/2] kcsan: Add __kcsan_{enable,disable}_current() variants Marco Elver
  0 siblings, 2 replies; 4+ messages in thread
From: Marco Elver @ 2020-04-24 15:47 UTC (permalink / raw)
  To: elver
  Cc: paulmck, dvyukov, glider, andreyknvl, will, kasan-dev,
	linux-kernel, jpoimboe, peterz

The __kcsan_{enable,disable}_current() variants only call into KCSAN if
KCSAN is enabled for the current compilation unit. Note: This is
typically not what we want, as we usually want to ensure that even calls
into other functions still have KCSAN disabled.

These variants may safely be used in header files that are shared
between regular kernel code and code that does not link the KCSAN
runtime.

Signed-off-by: Marco Elver <elver@google.com>
---
This is to help with the new READ_ONCE()/WRITE_ONCE():
https://lkml.kernel.org/r/20200424134238.GE21141@willie-the-truck

These should be using __kcsan_disable_current() and
__kcsan_enable_current(), instead of the non-'__' variants.
---
 include/linux/kcsan-checks.h | 17 ++++++++++++++---
 kernel/kcsan/core.c          |  7 +++++++
 2 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/include/linux/kcsan-checks.h b/include/linux/kcsan-checks.h
index ef95ddc49182..7b0b9c44f5f3 100644
--- a/include/linux/kcsan-checks.h
+++ b/include/linux/kcsan-checks.h
@@ -49,6 +49,7 @@ void kcsan_disable_current(void);
  * Supports nesting.
  */
 void kcsan_enable_current(void);
+void kcsan_enable_current_nowarn(void); /* Safe in uaccess regions. */
 
 /**
  * kcsan_nestable_atomic_begin - begin nestable atomic region
@@ -149,6 +150,7 @@ static inline void __kcsan_check_access(const volatile void *ptr, size_t size,
 
 static inline void kcsan_disable_current(void)		{ }
 static inline void kcsan_enable_current(void)		{ }
+static inline void kcsan_enable_current_nowarn(void)	{ }
 static inline void kcsan_nestable_atomic_begin(void)	{ }
 static inline void kcsan_nestable_atomic_end(void)	{ }
 static inline void kcsan_flat_atomic_begin(void)	{ }
@@ -165,15 +167,24 @@ static inline void kcsan_end_scoped_access(struct kcsan_scoped_access *sa) { }
 
 #endif /* CONFIG_KCSAN */
 
+#ifdef __SANITIZE_THREAD__
 /*
- * kcsan_*: Only calls into the runtime when the particular compilation unit has
- * KCSAN instrumentation enabled. May be used in header files.
+ * Only calls into the runtime when the particular compilation unit has KCSAN
+ * instrumentation enabled. May be used in header files.
  */
-#ifdef __SANITIZE_THREAD__
 #define kcsan_check_access __kcsan_check_access
+
+/*
+ * Only use these to disable KCSAN for accesses in the current compilation unit;
+ * calls into libraries may still perform KCSAN checks.
+ */
+#define __kcsan_disable_current kcsan_disable_current
+#define __kcsan_enable_current kcsan_enable_current_nowarn
 #else
 static inline void kcsan_check_access(const volatile void *ptr, size_t size,
 				      int type) { }
+static inline void __kcsan_enable_current(void)  { }
+static inline void __kcsan_disable_current(void) { }
 #endif
 
 /**
diff --git a/kernel/kcsan/core.c b/kernel/kcsan/core.c
index 40919943617b..0a0f018cb154 100644
--- a/kernel/kcsan/core.c
+++ b/kernel/kcsan/core.c
@@ -625,6 +625,13 @@ void kcsan_enable_current(void)
 }
 EXPORT_SYMBOL(kcsan_enable_current);
 
+void kcsan_enable_current_nowarn(void)
+{
+	if (get_ctx()->disable_count-- == 0)
+		kcsan_disable_current();
+}
+EXPORT_SYMBOL(kcsan_enable_current_nowarn);
+
 void kcsan_nestable_atomic_begin(void)
 {
 	/*
-- 
2.26.2.303.gf8c07b1a785-goog


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

* [PATCH 2/2] objtool, kcsan: Add kcsan_disable_current() and kcsan_enable_current_nowarn()
  2020-04-24 15:47 [PATCH 1/2] kcsan: Add __kcsan_{enable,disable}_current() variants Marco Elver
@ 2020-04-24 15:47 ` Marco Elver
  2020-04-24 15:57 ` [PATCH 1/2] kcsan: Add __kcsan_{enable,disable}_current() variants Marco Elver
  1 sibling, 0 replies; 4+ messages in thread
From: Marco Elver @ 2020-04-24 15:47 UTC (permalink / raw)
  To: elver
  Cc: paulmck, dvyukov, glider, andreyknvl, will, kasan-dev,
	linux-kernel, jpoimboe, peterz

Both are safe to be called from uaccess contexts.

Signed-off-by: Marco Elver <elver@google.com>
---
 tools/objtool/check.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 70e721002743..a22272c819f3 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -482,6 +482,8 @@ static const char *uaccess_safe_builtin[] = {
 	"kcsan_found_watchpoint",
 	"kcsan_setup_watchpoint",
 	"kcsan_check_scoped_accesses",
+	"kcsan_disable_current",
+	"kcsan_enable_current_nowarn",
 	/* KCSAN/TSAN */
 	"__tsan_func_entry",
 	"__tsan_func_exit",
-- 
2.26.2.303.gf8c07b1a785-goog


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

* Re: [PATCH 1/2] kcsan: Add __kcsan_{enable,disable}_current() variants
  2020-04-24 15:47 [PATCH 1/2] kcsan: Add __kcsan_{enable,disable}_current() variants Marco Elver
  2020-04-24 15:47 ` [PATCH 2/2] objtool, kcsan: Add kcsan_disable_current() and kcsan_enable_current_nowarn() Marco Elver
@ 2020-04-24 15:57 ` Marco Elver
  2020-04-25  0:17   ` Paul E. McKenney
  1 sibling, 1 reply; 4+ messages in thread
From: Marco Elver @ 2020-04-24 15:57 UTC (permalink / raw)
  To: Marco Elver
  Cc: Paul E. McKenney, Dmitry Vyukov, Alexander Potapenko,
	Andrey Konovalov, Will Deacon, kasan-dev, LKML, Josh Poimboeuf,
	Peter Zijlstra

On Fri, 24 Apr 2020 at 17:47, Marco Elver <elver@google.com> wrote:
>
> The __kcsan_{enable,disable}_current() variants only call into KCSAN if
> KCSAN is enabled for the current compilation unit. Note: This is
> typically not what we want, as we usually want to ensure that even calls
> into other functions still have KCSAN disabled.
>
> These variants may safely be used in header files that are shared
> between regular kernel code and code that does not link the KCSAN
> runtime.
>
> Signed-off-by: Marco Elver <elver@google.com>
> ---
> This is to help with the new READ_ONCE()/WRITE_ONCE():
> https://lkml.kernel.org/r/20200424134238.GE21141@willie-the-truck
>
> These should be using __kcsan_disable_current() and
> __kcsan_enable_current(), instead of the non-'__' variants.
> ---

Paul: These 2 patches may want to be in the set for 5.8, depending on
what Will wants to do.

An alternative would be that Will takes my 2 patches and carries them,
avoiding some complex patch-dependency. That is assuming his set of
patches will go in -tip on top of KCSAN.

Thanks,
-- Marco

>  include/linux/kcsan-checks.h | 17 ++++++++++++++---
>  kernel/kcsan/core.c          |  7 +++++++
>  2 files changed, 21 insertions(+), 3 deletions(-)
>
> diff --git a/include/linux/kcsan-checks.h b/include/linux/kcsan-checks.h
> index ef95ddc49182..7b0b9c44f5f3 100644
> --- a/include/linux/kcsan-checks.h
> +++ b/include/linux/kcsan-checks.h
> @@ -49,6 +49,7 @@ void kcsan_disable_current(void);
>   * Supports nesting.
>   */
>  void kcsan_enable_current(void);
> +void kcsan_enable_current_nowarn(void); /* Safe in uaccess regions. */
>
>  /**
>   * kcsan_nestable_atomic_begin - begin nestable atomic region
> @@ -149,6 +150,7 @@ static inline void __kcsan_check_access(const volatile void *ptr, size_t size,
>
>  static inline void kcsan_disable_current(void)         { }
>  static inline void kcsan_enable_current(void)          { }
> +static inline void kcsan_enable_current_nowarn(void)   { }
>  static inline void kcsan_nestable_atomic_begin(void)   { }
>  static inline void kcsan_nestable_atomic_end(void)     { }
>  static inline void kcsan_flat_atomic_begin(void)       { }
> @@ -165,15 +167,24 @@ static inline void kcsan_end_scoped_access(struct kcsan_scoped_access *sa) { }
>
>  #endif /* CONFIG_KCSAN */
>
> +#ifdef __SANITIZE_THREAD__
>  /*
> - * kcsan_*: Only calls into the runtime when the particular compilation unit has
> - * KCSAN instrumentation enabled. May be used in header files.
> + * Only calls into the runtime when the particular compilation unit has KCSAN
> + * instrumentation enabled. May be used in header files.
>   */
> -#ifdef __SANITIZE_THREAD__
>  #define kcsan_check_access __kcsan_check_access
> +
> +/*
> + * Only use these to disable KCSAN for accesses in the current compilation unit;
> + * calls into libraries may still perform KCSAN checks.
> + */
> +#define __kcsan_disable_current kcsan_disable_current
> +#define __kcsan_enable_current kcsan_enable_current_nowarn
>  #else
>  static inline void kcsan_check_access(const volatile void *ptr, size_t size,
>                                       int type) { }
> +static inline void __kcsan_enable_current(void)  { }
> +static inline void __kcsan_disable_current(void) { }
>  #endif
>
>  /**
> diff --git a/kernel/kcsan/core.c b/kernel/kcsan/core.c
> index 40919943617b..0a0f018cb154 100644
> --- a/kernel/kcsan/core.c
> +++ b/kernel/kcsan/core.c
> @@ -625,6 +625,13 @@ void kcsan_enable_current(void)
>  }
>  EXPORT_SYMBOL(kcsan_enable_current);
>
> +void kcsan_enable_current_nowarn(void)
> +{
> +       if (get_ctx()->disable_count-- == 0)
> +               kcsan_disable_current();
> +}
> +EXPORT_SYMBOL(kcsan_enable_current_nowarn);
> +
>  void kcsan_nestable_atomic_begin(void)
>  {
>         /*
> --
> 2.26.2.303.gf8c07b1a785-goog
>

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

* Re: [PATCH 1/2] kcsan: Add __kcsan_{enable,disable}_current() variants
  2020-04-24 15:57 ` [PATCH 1/2] kcsan: Add __kcsan_{enable,disable}_current() variants Marco Elver
@ 2020-04-25  0:17   ` Paul E. McKenney
  0 siblings, 0 replies; 4+ messages in thread
From: Paul E. McKenney @ 2020-04-25  0:17 UTC (permalink / raw)
  To: Marco Elver
  Cc: Dmitry Vyukov, Alexander Potapenko, Andrey Konovalov,
	Will Deacon, kasan-dev, LKML, Josh Poimboeuf, Peter Zijlstra

On Fri, Apr 24, 2020 at 05:57:04PM +0200, Marco Elver wrote:
> On Fri, 24 Apr 2020 at 17:47, Marco Elver <elver@google.com> wrote:
> >
> > The __kcsan_{enable,disable}_current() variants only call into KCSAN if
> > KCSAN is enabled for the current compilation unit. Note: This is
> > typically not what we want, as we usually want to ensure that even calls
> > into other functions still have KCSAN disabled.
> >
> > These variants may safely be used in header files that are shared
> > between regular kernel code and code that does not link the KCSAN
> > runtime.
> >
> > Signed-off-by: Marco Elver <elver@google.com>
> > ---
> > This is to help with the new READ_ONCE()/WRITE_ONCE():
> > https://lkml.kernel.org/r/20200424134238.GE21141@willie-the-truck
> >
> > These should be using __kcsan_disable_current() and
> > __kcsan_enable_current(), instead of the non-'__' variants.
> > ---
> 
> Paul: These 2 patches may want to be in the set for 5.8, depending on
> what Will wants to do.
> 
> An alternative would be that Will takes my 2 patches and carries them,
> avoiding some complex patch-dependency. That is assuming his set of
> patches will go in -tip on top of KCSAN.

For the moment I have pulled them into -rcu and am testing them,
thank you!  I will leave them in the v5.9 bucket for the moment,
but please let me know how things proceed with Will.

							Thanx, Paul

> Thanks,
> -- Marco
> 
> >  include/linux/kcsan-checks.h | 17 ++++++++++++++---
> >  kernel/kcsan/core.c          |  7 +++++++
> >  2 files changed, 21 insertions(+), 3 deletions(-)
> >
> > diff --git a/include/linux/kcsan-checks.h b/include/linux/kcsan-checks.h
> > index ef95ddc49182..7b0b9c44f5f3 100644
> > --- a/include/linux/kcsan-checks.h
> > +++ b/include/linux/kcsan-checks.h
> > @@ -49,6 +49,7 @@ void kcsan_disable_current(void);
> >   * Supports nesting.
> >   */
> >  void kcsan_enable_current(void);
> > +void kcsan_enable_current_nowarn(void); /* Safe in uaccess regions. */
> >
> >  /**
> >   * kcsan_nestable_atomic_begin - begin nestable atomic region
> > @@ -149,6 +150,7 @@ static inline void __kcsan_check_access(const volatile void *ptr, size_t size,
> >
> >  static inline void kcsan_disable_current(void)         { }
> >  static inline void kcsan_enable_current(void)          { }
> > +static inline void kcsan_enable_current_nowarn(void)   { }
> >  static inline void kcsan_nestable_atomic_begin(void)   { }
> >  static inline void kcsan_nestable_atomic_end(void)     { }
> >  static inline void kcsan_flat_atomic_begin(void)       { }
> > @@ -165,15 +167,24 @@ static inline void kcsan_end_scoped_access(struct kcsan_scoped_access *sa) { }
> >
> >  #endif /* CONFIG_KCSAN */
> >
> > +#ifdef __SANITIZE_THREAD__
> >  /*
> > - * kcsan_*: Only calls into the runtime when the particular compilation unit has
> > - * KCSAN instrumentation enabled. May be used in header files.
> > + * Only calls into the runtime when the particular compilation unit has KCSAN
> > + * instrumentation enabled. May be used in header files.
> >   */
> > -#ifdef __SANITIZE_THREAD__
> >  #define kcsan_check_access __kcsan_check_access
> > +
> > +/*
> > + * Only use these to disable KCSAN for accesses in the current compilation unit;
> > + * calls into libraries may still perform KCSAN checks.
> > + */
> > +#define __kcsan_disable_current kcsan_disable_current
> > +#define __kcsan_enable_current kcsan_enable_current_nowarn
> >  #else
> >  static inline void kcsan_check_access(const volatile void *ptr, size_t size,
> >                                       int type) { }
> > +static inline void __kcsan_enable_current(void)  { }
> > +static inline void __kcsan_disable_current(void) { }
> >  #endif
> >
> >  /**
> > diff --git a/kernel/kcsan/core.c b/kernel/kcsan/core.c
> > index 40919943617b..0a0f018cb154 100644
> > --- a/kernel/kcsan/core.c
> > +++ b/kernel/kcsan/core.c
> > @@ -625,6 +625,13 @@ void kcsan_enable_current(void)
> >  }
> >  EXPORT_SYMBOL(kcsan_enable_current);
> >
> > +void kcsan_enable_current_nowarn(void)
> > +{
> > +       if (get_ctx()->disable_count-- == 0)
> > +               kcsan_disable_current();
> > +}
> > +EXPORT_SYMBOL(kcsan_enable_current_nowarn);
> > +
> >  void kcsan_nestable_atomic_begin(void)
> >  {
> >         /*
> > --
> > 2.26.2.303.gf8c07b1a785-goog
> >

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

end of thread, other threads:[~2020-04-25  0:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-24 15:47 [PATCH 1/2] kcsan: Add __kcsan_{enable,disable}_current() variants Marco Elver
2020-04-24 15:47 ` [PATCH 2/2] objtool, kcsan: Add kcsan_disable_current() and kcsan_enable_current_nowarn() Marco Elver
2020-04-24 15:57 ` [PATCH 1/2] kcsan: Add __kcsan_{enable,disable}_current() variants Marco Elver
2020-04-25  0:17   ` Paul E. McKenney

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).