Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1250455

[PATCH v5 1/2] compiler, atomics: Provide READ_ONCE_NOCHECK()

From Andrey Ryabinin <aryabinin@virtuozzo.com>
Newsgroups linux.kernel
Subject [PATCH v5 1/2] compiler, atomics: Provide READ_ONCE_NOCHECK()
Date 2015-10-19 10:40 +0200
Message-ID <qle94-7rs-19@gated-at.bofh.it> (permalink)
References <qj721-6Fq-17@gated-at.bofh.it> <qle93-7rs-13@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


Some code may perform racy by design memory reads. This could be
harmless, yet such code may produce KASAN warnings.

To hide such accesses from KASAN this patch introduces
READ_ONCE_NOCHECK() macro. KASAN will not check the memory
accessed by READ_ONCE_NOCHECK(). The KernelThreadSanitizer (KTSAN)
is going to ignore it as well.

This patch creates __read_once_size_nocheck() a clone of
__read_once_size(). The only difference between them is
'no_sanitized_address' attribute appended to '*_nocheck' function.
This attribute tells the compiler that instrumentation of memory
accesses should not be applied to that function. We declare it as
static '__maybe_unsed' because GCC is not capable to inline such
function: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368

With KASAN=n READ_ONCE_NOCHECK() is just a clone of READ_ONCE().

Signed-off-by: Andrey Ryabinin <aryabinin@virtuozzo.com>
---
 include/linux/compiler-gcc.h | 13 +++++++++
 include/linux/compiler.h     | 66 +++++++++++++++++++++++++++++++++++---------
 2 files changed, 66 insertions(+), 13 deletions(-)

diff --git a/include/linux/compiler-gcc.h b/include/linux/compiler-gcc.h
index dfaa7b3..8efb40e 100644
--- a/include/linux/compiler-gcc.h
+++ b/include/linux/compiler-gcc.h
@@ -237,12 +237,25 @@
 #define KASAN_ABI_VERSION 3
 #endif
 
+#if GCC_VERSION >= 40902
+/*
+ * Tell the compiler that address safety instrumentation (KASAN)
+ * should not be applied to that function.
+ * Conflicts with inlining: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
+ */
+#define __no_sanitize_address __attribute__((no_sanitize_address))
+#endif
+
 #endif	/* gcc version >= 40000 specific checks */
 
 #if !defined(__noclone)
 #define __noclone	/* not needed */
 #endif
 
+#if !defined(__no_sanitize_address)
+#define __no_sanitize_address
+#endif
+
 /*
  * A trick to suppress uninitialized variable warning without generating any
  * code
diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index c836eb2..3d78103 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -198,19 +198,45 @@ void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
 
 #include <uapi/linux/types.h>
 
-static __always_inline void __read_once_size(const volatile void *p, void *res, int size)
+#define __READ_ONCE_SIZE						\
+({									\
+	switch (size) {							\
+	case 1: *(__u8 *)res = *(volatile __u8 *)p; break;		\
+	case 2: *(__u16 *)res = *(volatile __u16 *)p; break;		\
+	case 4: *(__u32 *)res = *(volatile __u32 *)p; break;		\
+	case 8: *(__u64 *)res = *(volatile __u64 *)p; break;		\
+	default:							\
+		barrier();						\
+		__builtin_memcpy((void *)res, (const void *)p, size);	\
+		barrier();						\
+	}								\
+})
+
+static __always_inline
+void __read_once_size(const volatile void *p, void *res, int size)
 {
-	switch (size) {
-	case 1: *(__u8 *)res = *(volatile __u8 *)p; break;
-	case 2: *(__u16 *)res = *(volatile __u16 *)p; break;
-	case 4: *(__u32 *)res = *(volatile __u32 *)p; break;
-	case 8: *(__u64 *)res = *(volatile __u64 *)p; break;
-	default:
-		barrier();
-		__builtin_memcpy((void *)res, (const void *)p, size);
-		barrier();
-	}
+	__READ_ONCE_SIZE;
+}
+
+#ifdef CONFIG_KASAN
+/*
+ * This function is not 'inline' because __no_sanitize_address confilcts
+ * with inlining. Attempt to inline it may cause a build failure.
+ * 	https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
+ * '__maybe_unused' allows us to avoid defined-but-not-used warnings.
+ */
+static __no_sanitize_address __maybe_unused
+void __read_once_size_nocheck(const volatile void *p, void *res, int size)
+{
+	__READ_ONCE_SIZE;
+}
+#else
+static __always_inline
+void __read_once_size_nocheck(const volatile void *p, void *res, int size)
+{
+	__READ_ONCE_SIZE;
 }
+#endif
 
 static __always_inline void __write_once_size(volatile void *p, void *res, int size)
 {
@@ -248,8 +274,22 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s
  * required ordering.
  */
 
-#define READ_ONCE(x) \
-	({ union { typeof(x) __val; char __c[1]; } __u; __read_once_size(&(x), __u.__c, sizeof(x)); __u.__val; })
+#define __READ_ONCE(x, check)						\
+({									\
+	union { typeof(x) __val; char __c[1]; } __u;			\
+	if (check)							\
+		__read_once_size(&(x), __u.__c, sizeof(x));		\
+	else								\
+		__read_once_size_nocheck(&(x), __u.__c, sizeof(x));	\
+	__u.__val;							\
+})
+#define READ_ONCE(x) __READ_ONCE(x, 1)
+
+/*
+ * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need
+ * to hide memory access from KASAN.
+ */
+#define READ_ONCE_NOCHECK(x) __READ_ONCE(x, 0)
 
 #define WRITE_ONCE(x, val) \
 ({							\
-- 
2.4.9

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH v2 0/2] Silence KASAN warnings in get_wchan() Andrey Ryabinin <aryabinin@virtuozzo.com> - 2015-10-13 14:50 +0200
  [PATCH v3 0/2] Silence KASAN warnings in get_wchan() Andrey Ryabinin <aryabinin@virtuozzo.com> - 2015-10-13 17:30 +0200
    [PATCH v3 1/2] Provide READ_ONCE_NOCHECK() Andrey Ryabinin <aryabinin@virtuozzo.com> - 2015-10-13 17:40 +0200
      [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK  () tip-bot for Andrey Ryabinin <tipbot@zytor.com> - 2015-10-14 17:40 +0200
        Re: [tip:locking/urgent] compiler, atomics: Provide  READ_ONCE_NOCHECK () "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> - 2015-10-14 17:50 +0200
          Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK () Dmitry Vyukov <dvyukov@google.com> - 2015-10-14 18:00 +0200
            Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK () Dmitry Vyukov <dvyukov@google.com> - 2015-10-14 18:10 +0200
              Re: [tip:locking/urgent] compiler, atomics: Provide  READ_ONCE_NOCHECK () Peter Zijlstra <peterz@infradead.org> - 2015-10-14 18:20 +0200
                Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK () Dmitry Vyukov <dvyukov@google.com> - 2015-10-14 18:20 +0200
                Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK () Andy Lutomirski <luto@amacapital.net> - 2015-10-14 18:30 +0200
                Re: [tip:locking/urgent] compiler, atomics: Provide  READ_ONCE_NOCHECK () Peter Zijlstra <peterz@infradead.org> - 2015-10-14 18:40 +0200
                Re: [tip:locking/urgent] compiler, atomics: Provide  READ_ONCE_NOCHECK () Ingo Molnar <mingo@kernel.org> - 2015-10-14 19:50 +0200
                Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK () Andy Lutomirski <luto@amacapital.net> - 2015-10-14 20:00 +0200
                Re: [tip:locking/urgent] compiler, atomics: Provide  READ_ONCE_NOCHECK () Peter Zijlstra <peterz@infradead.org> - 2015-10-14 18:30 +0200
                Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK () Dmitry Vyukov <dvyukov@google.com> - 2015-10-14 18:40 +0200
                Re: [tip:locking/urgent] compiler, atomics: Provide  READ_ONCE_NOCHECK () Peter Zijlstra <peterz@infradead.org> - 2015-10-14 19:00 +0200
              Re: [tip:locking/urgent] compiler, atomics: Provide  READ_ONCE_NOCHECK () "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> - 2015-10-14 18:30 +0200
                Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK () Dmitry Vyukov <dvyukov@google.com> - 2015-10-14 18:40 +0200
                Re: [tip:locking/urgent] compiler, atomics: Provide  READ_ONCE_NOCHECK () "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> - 2015-10-14 19:10 +0200
                Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK () Dmitry Vyukov <dvyukov@google.com> - 2015-10-14 19:30 +0200
                Re: [tip:locking/urgent] compiler, atomics: Provide  READ_ONCE_NOCHECK () "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> - 2015-10-14 19:40 +0200
            Re: [tip:locking/urgent] compiler, atomics: Provide  READ_ONCE_NOCHECK () "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> - 2015-10-14 18:10 +0200
            Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK  () Andrey Ryabinin <aryabinin@virtuozzo.com> - 2015-10-14 18:20 +0200
              Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK () Dmitry Vyukov <dvyukov@google.com> - 2015-10-14 18:40 +0200
                Re: [tip:locking/urgent] compiler, atomics: Provide  READ_ONCE_NOCHECK () "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> - 2015-10-14 19:10 +0200
      linux-next: build problems (Was: [PATCH v3 1/2] Provide  READ_ONCE_NOCHECK()) Stephen Rothwell <sfr@canb.auug.org.au> - 2015-10-15 11:20 +0200
        Re: linux-next: build problems (Was: [PATCH v3 1/2] Provide  READ_ONCE_NOCHECK()) Andrey Ryabinin <aryabinin@virtuozzo.com> - 2015-10-15 12:10 +0200
        [PATCH] compiler, READ_ONCE: Fix build failure with some older GCC Andrey Ryabinin <aryabinin@virtuozzo.com> - 2015-10-15 12:30 +0200
          Re: [PATCH] compiler, READ_ONCE: Fix build failure with some older  GCC Ingo Molnar <mingo@kernel.org> - 2015-10-15 13:40 +0200
    [PATCH v3 2/2] x86/process: Silence KASAN warnings in get_wchan() Andrey Ryabinin <aryabinin@virtuozzo.com> - 2015-10-13 17:40 +0200
      [tip:locking/urgent] x86/mm: Silence KASAN warnings in get_wchan(  ) tip-bot for Andrey Ryabinin <tipbot@zytor.com> - 2015-10-14 17:40 +0200
  [PATCH v4 2/2] x86/mm: Silence KASAN warnings in get_wchan() Andrey Ryabinin <aryabinin@virtuozzo.com> - 2015-10-16 11:50 +0200
  [PATCH v4 0/2] Andrey Ryabinin <aryabinin@virtuozzo.com> - 2015-10-16 11:50 +0200
    [PATCH v4 1/2] compiler, atomics: Provide READ_ONCE_NOKSAN() Andrey Ryabinin <aryabinin@virtuozzo.com> - 2015-10-16 11:50 +0200
      Re: [PATCH v4 1/2] compiler, atomics: Provide READ_ONCE_NOKSAN() Peter Zijlstra <peterz@infradead.org> - 2015-10-16 12:10 +0200
        Re: [PATCH v4 1/2] compiler, atomics: Provide READ_ONCE_NOKSAN() Andrey Ryabinin <aryabinin@virtuozzo.com> - 2015-10-16 13:00 +0200
          Re: [PATCH v4 1/2] compiler, atomics: Provide READ_ONCE_NOKSAN() Peter Zijlstra <peterz@infradead.org> - 2015-10-16 13:10 +0200
      Re: [PATCH v4 1/2] compiler, atomics: Provide READ_ONCE_NOKSAN() Borislav Petkov <bp@alien8.de> - 2015-10-16 12:40 +0200
        Re: [PATCH v4 1/2] compiler, atomics: Provide READ_ONCE_NOKSAN() Andrey Ryabinin <aryabinin@virtuozzo.com> - 2015-10-16 14:00 +0200
          Re: [PATCH v4 1/2] compiler, atomics: Provide READ_ONCE_NOKSAN() Ingo Molnar <mingo@kernel.org> - 2015-10-18 09:30 +0200
        Re: [PATCH v4 1/2] compiler, atomics: Provide READ_ONCE_NOKSAN() "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> - 2015-10-16 18:10 +0200
    [PATCH v4 0/2] Silence KASAN warnings in get_wchan() Andrey Ryabinin <aryabinin@virtuozzo.com> - 2015-10-16 11:50 +0200
  [PATCH v5 2/2] x86/mm: Silence KASAN warnings in get_wchan() Andrey Ryabinin <aryabinin@virtuozzo.com> - 2015-10-19 10:40 +0200
    [tip:x86/urgent] x86/mm, kasan:   Silence KASAN warnings in get_wchan() tip-bot for Andrey Ryabinin <tipbot@zytor.com> - 2015-10-20 11:50 +0200
  [PATCH v5 0/2] Silence KASAN warnings in get_wchan() Andrey Ryabinin <aryabinin@virtuozzo.com> - 2015-10-19 10:40 +0200
    [PATCH v5 1/2] compiler, atomics: Provide READ_ONCE_NOCHECK() Andrey Ryabinin <aryabinin@virtuozzo.com> - 2015-10-19 10:40 +0200
      [tip:x86/urgent] compiler, atomics, kasan:   Provide READ_ONCE_NOCHECK() tip-bot for Andrey Ryabinin <tipbot@zytor.com> - 2015-10-20 11:50 +0200

csiph-web