Skip to contentSkip to conversation

Silence KASAN warnings in get_wchan()

PATCH Superseded Andrey Ryabinin 10 participants 59 messages Oct 13, 2015 – Oct 20, 2015

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover Silence KASAN warnings in get_wchan()

Originally I suggested to implement READ_ONCE_NOCHECK(), like this:
#define READ_ONCE_NOCHECK(x) \

{(	typeof(x) __val; \
	kasan_disable_local(); \
	__val = READ_ONCE(x); \
	kasan_enable_local(); \
	__val;\

)}

But then I realised that we can't put this into linux/compiler.h since it requires to add some includes (we need linux/kasan.h which also requires linux/sched.h). It's not even that simple to put this into kernel.h

So I've come to another approach, see the first patch for details. Pros:
- It generates more efficient code rather than variant above
Cons:
- REAS_ONCE() becomes rather messy.

Andrey Ryabinin (2):
Provide READ_ONCE_NOCHECK()
x86/process: Silence KASAN warnings in get_wchan()

 arch/x86/kernel/process.c    |  6 +++---
 include/linux/compiler-gcc.h | 13 ++++++++++++
 include/linux/compiler.h     | 49 ++++++++++++++++++++++++++++++++------------
 3 files changed, 52 insertions(+), 16 deletions(-)
Signature · 1 line
2.4.9
#1 lore raw

PATCH v2 1/2 Provide READ_ONCE_NOCHECK()

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().

This patch creates __read_once_size_nocheck() a clone of __read_once_size_check() (renamed __read_once_size()).
The only difference between them is 'no_sanitized_address' attribute appended to '*_nocheck' function. This attribute tells compiler to not 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().

---
 include/linux/compiler-gcc.h | 13 ++++++++++++
 include/linux/compiler.h     | 49 ++++++++++++++++++++++++++++++++------------
 2 files changed, 49 insertions(+), 13 deletions(-)
include/linux/compiler-gcc.h +13 -0
index dfaa7b3..7ab09de 100644
@@ -237,12 +237,25 @@
 #define KASAN_ABI_VERSION 3
 #endif
 
+#if GCC_VERSION >= 40902
+/*
+ * Tell the compiler that address safety instrumentation (e.g. KASAN)
+ * should not be applied to that function.
+ * Confilcts 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
include/linux/compiler.h +36 -13

64 lines not on this page · raw message

Signature · 1 line
2.4.9
#2 lore raw

Re: [PATCH v2 1/2] Provide READ_ONCE_NOCHECK()

Quoted 29 lines · Andrey Ryabinin
include/linux/compiler.h +26 -0
+#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_check(const volatile void *p, void *res, int size)
 {
+	__READ_ONCE_SIZE;
 }
 
+#ifdef CONFIG_KASAN
+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 __alias(__read_once_size_check)
+void __read_once_size_nocheck(const volatile void *p, void *res, int size);

include/linux/compiler.h void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);

Quoted 10 lines
include/linux/compiler.h +9 -0
+#endif
 
+#define __READ_ONCE(x, check)				\
+({								\
+	union { typeof(x) __val; char __c[1]; } __u;		\
+	__read_once_size##check(&(x), __u.__c, sizeof(x));	\
+	__u.__val;						\
+})
+#define READ_ONCE(x) __READ_ONCE(x, _check)
+#define READ_ONCE_NOCHECK(x) __READ_ONCE(x, _nocheck)

So all this code is quite convoluted, and as the changelog explains it's done to work around a GCC inlining + no-kasan bug.

But please explain this in the code as well, in a comment, so future generations are not kept wondering why these relatively simple wrappers are coded in such an ugly and roundabout fashion.

Thanks,

Ingo

#3 lore raw

Re: [PATCH v2 1/2] Provide READ_ONCE_NOCHECK()

In reply to Andrey Ryabinin
reproduce: wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # save the attached .config to linux build tree…

Hi Andrey,

[auto build test WARNING on tip/auto-latest -- if it's inappropriate base, please suggest rules for selecting the more suitable base]

url:    https://github.com/0day-ci/linux/commits/Andrey-Ryabinin/Provide-READ_ONCE_NOCHECK/20151013-204127
config: mn10300-allyesconfig (attached as .config)

reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=mn10300

All warnings (new ones prefixed by >>):

fs/nfs/filelayout/filelayout.c: In function 'filelayout_read_pagelist':
fs/nfs/filelayout/filelayout.c:476:2: warning: format '%Zu' expects argument of type 'size_t', but argument 5 has type '__u32' [-Wformat=]

     dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
     ^
   In file included from include/linux/nfs_fs.h:28:0,
                    from fs/nfs/filelayout/filelayout.c:32:
   fs/nfs/filelayout/filelayout.c: In function 'filelayout_write_pagelist':
   include/linux/compiler.h:270:8: warning: format '%Zu' expects argument of type 'size_t', but argument 5 has type '__u32' [-Wformat=]
     union { typeof(x) __val; char __c[1]; } __u;  \
           ^
   include/linux/sunrpc/debug.h:33:24: note: in definition of macro 'dfprintk'
       printk(KERN_DEFAULT args); \
                           ^
>> include/linux/compiler.h:274:22: note: in expansion of macro '__READ_ONCE'
    #define READ_ONCE(x) __READ_ONCE(x, _check)
                         ^
   include/asm-generic/atomic.h:130:24: note: in expansion of macro 'READ_ONCE'
    #define atomic_read(v) READ_ONCE((v)->counter)
                           ^
   include/linux/sunrpc/debug.h:23:48: note: in expansion of macro 'atomic_read'
    #define dprintk(args...) dfprintk(FACILITY, ## args)
                                                   ^

fs/nfs/filelayout/filelayout.c:534:2: note: in expansion of macro 'dprintk'
dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d\n", ^

vim +/__READ_ONCE +274 include/linux/compiler.h

264 * with an explicit memory barrier or atomic instruction that provides the
265 * required ordering.
266 */
267
268 #define __READ_ONCE(x, check) \
269 ({ \

 > 270		union { typeof(x) __val; char __c[1]; } __u;		\
   271		__read_once_size##check(&(x), __u.__c, sizeof(x));	\
   272		__u.__val;						\
   273	})
 > 274	#define READ_ONCE(x) __READ_ONCE(x, _check)

275 #define READ_ONCE_NOCHECK(x) __READ_ONCE(x, _nocheck)
276
277 #define WRITE_ONCE(x, val) \

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#4 lore raw

Re: [PATCH v2 1/2] Provide READ_ONCE_NOCHECK()

Quoted 12 lines · kbuild test robot

Hi Andrey,

[auto build test WARNING on tip/auto-latest -- if it's inappropriate base, please suggest rules for selecting the more suitable base]

url:    https://github.com/0day-ci/linux/commits/Andrey-Ryabinin/Provide-READ_ONCE_NOCHECK/20151013-204127
config: mn10300-allyesconfig (attached as .config)

reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=mn10300

Quoted 6 lines

All warnings (new ones prefixed by >>):

fs/nfs/filelayout/filelayout.c: In function 'filelayout_read_pagelist':

   fs/nfs/filelayout/filelayout.c:476:2: warning: format '%Zu' expects argument of type 'size_t', but argument 5 has type '__u32' [-Wformat=]
     dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
     ^

So the actual warning is here. And it's not new.

Quoted 9 lines

In file included from include/linux/nfs_fs.h:28:0,
from fs/nfs/filelayout/filelayout.c:32:

   fs/nfs/filelayout/filelayout.c: In function 'filelayout_write_pagelist':
   include/linux/compiler.h:270:8: warning: format '%Zu' expects argument of type 'size_t', but argument 5 has type '__u32' [-Wformat=]
     union { typeof(x) __val; char __c[1]; } __u;  \
           ^
   include/linux/sunrpc/debug.h:33:24: note: in definition of macro 'dfprintk'
       printk(KERN_DEFAULT args); \
                           ^
Quoted 3 lines
Quoted 1 line
Quoted 1 line

include/linux/compiler.h:274:22: note: in expansion of macro '__READ_ONCE'

    #define READ_ONCE(x) __READ_ONCE(x, _check)
                         ^

And this 'note: in expansion of macro' belongs to the warning above. So it's a false-positive.

Quoted 31 lines
   include/asm-generic/atomic.h:130:24: note: in expansion of macro 'READ_ONCE'
    #define atomic_read(v) READ_ONCE((v)->counter)
                           ^
   include/linux/sunrpc/debug.h:23:48: note: in expansion of macro 'atomic_read'
    #define dprintk(args...) dfprintk(FACILITY, ## args)
                                                   ^
   fs/nfs/filelayout/filelayout.c:534:2: note: in expansion of macro 'dprintk'
     dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d\n",
     ^

vim +/__READ_ONCE +274 include/linux/compiler.h

   264	 * with an explicit memory barrier or atomic instruction that provides the
   265	 * required ordering.
   266	 */
   267	
   268	#define __READ_ONCE(x, check)					\
   269	({								\
Quoted 1 line

270 union { typeof(x) __val; char __c[1]; } __u; \

   271		__read_once_size##check(&(x), __u.__c, sizeof(x));	\
   272		__u.__val;						\
   273	})
Quoted 1 line

274 #define READ_ONCE(x) __READ_ONCE(x, _check)

275 #define READ_ONCE_NOCHECK(x) __READ_ONCE(x, _nocheck)
276
277 #define WRITE_ONCE(x, val) \

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#5 lore raw

Re: [PATCH v2 1/2] Provide READ_ONCE_NOCHECK()

Quoted 33 lines · Andrey Ryabinin
Quoted 12 lines · kbuild test robot

Hi Andrey,

[auto build test WARNING on tip/auto-latest -- if it's inappropriate base, please suggest rules for selecting the more suitable base]

url:    https://github.com/0day-ci/linux/commits/Andrey-Ryabinin/Provide-READ_ONCE_NOCHECK/20151013-204127
config: mn10300-allyesconfig (attached as .config)

reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=mn10300

Quoted 6 lines

All warnings (new ones prefixed by >>):

fs/nfs/filelayout/filelayout.c: In function 'filelayout_read_pagelist':

   fs/nfs/filelayout/filelayout.c:476:2: warning: format '%Zu' expects argument of type 'size_t', but argument 5 has type '__u32' [-Wformat=]
     dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
     ^

So the actual warning is here. And it's not new.

Quoted 9 lines

In file included from include/linux/nfs_fs.h:28:0,
from fs/nfs/filelayout/filelayout.c:32:

   fs/nfs/filelayout/filelayout.c: In function 'filelayout_write_pagelist':
   include/linux/compiler.h:270:8: warning: format '%Zu' expects argument of type 'size_t', but argument 5 has type '__u32' [-Wformat=]
     union { typeof(x) __val; char __c[1]; } __u;  \
           ^
   include/linux/sunrpc/debug.h:33:24: note: in definition of macro 'dfprintk'
       printk(KERN_DEFAULT args); \
                           ^
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

include/linux/compiler.h:274:22: note: in expansion of macro '__READ_ONCE'

    #define READ_ONCE(x) __READ_ONCE(x, _check)
                         ^

And this 'note: in expansion of macro' belongs to the warning above. So it's a false-positive.

So if this is a new warning introduced by these patches then the warning needs to be addresses - regardless of whether it's a false positive or not.

Thanks,

Ingo

#6 lore raw

Re: [PATCH v2 1/2] Provide READ_ONCE_NOCHECK()

Quoted 37 lines · Ingo Molnar
Quoted 34 lines · Andrey Ryabinin
Quoted 12 lines · kbuild test robot

Hi Andrey,

[auto build test WARNING on tip/auto-latest -- if it's inappropriate base, please suggest rules for selecting the more suitable base]

url:    https://github.com/0day-ci/linux/commits/Andrey-Ryabinin/Provide-READ_ONCE_NOCHECK/20151013-204127
config: mn10300-allyesconfig (attached as .config)

reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=mn10300

Quoted 6 lines

All warnings (new ones prefixed by >>):

fs/nfs/filelayout/filelayout.c: In function 'filelayout_read_pagelist':

   fs/nfs/filelayout/filelayout.c:476:2: warning: format '%Zu' expects argument of type 'size_t', but argument 5 has type '__u32' [-Wformat=]
     dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
     ^

So the actual warning is here. And it's not new.

Quoted 10 lines

In file included from include/linux/nfs_fs.h:28:0,
from fs/nfs/filelayout/filelayout.c:32:

   fs/nfs/filelayout/filelayout.c: In function 'filelayout_write_pagelist':
   include/linux/compiler.h:270:8: warning: format '%Zu' expects argument of type 'size_t', but argument 5 has type '__u32' [-Wformat=]
     union { typeof(x) __val; char __c[1]; } __u;  \
           ^
   include/linux/sunrpc/debug.h:33:24: note: in definition of macro 'dfprintk'
       printk(KERN_DEFAULT args); \
                           ^
Quoted 1 line
Quoted 1 line

include/linux/compiler.h:274:22: note: in expansion of macro '__READ_ONCE'

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
    #define READ_ONCE(x) __READ_ONCE(x, _check)
                         ^

And this 'note: in expansion of macro' belongs to the warning above. So it's a false-positive.

So if this is a new warning introduced by these patches then the warning needs to be addresses

It's not a new warning. It's a new 'note: in expansion of macro' message which is belongs to the old warning from above.
I changed the macros, so obviously it caused change in macro expansion backtrace. The actual warning existed before this patch.

Quoted 1 line

- regardless of whether it's a false positive or not.

I wasn't very clear. By false positive I meant that it is kbuild robot's false positive. The warning itself is not a false positive, but it's an old warning, it's not introduced by this patch.

It seems that kbuild robot treats 'note: in expansion of macro' message as a new warning, but such messages are not warnings by itself. I think, that kbuild robot should just ignore new 'note: in expansion of macro' messages unless these messages preceded by a new 'warning:' message.

Quoted 4 lines

Thanks,

Ingo

#7 lore raw

PATCH v2 2/2 x86/process: Silence KASAN warnings in get_wchan()

In reply to Andrey Ryabinin

get_wchan() is racy by design, it may access volatile stack of running task, thus it may access redzone in a stack frame and cause KASAN to warn about this.

Use READ_ONCE_NOCHECK() to silence these warnings.

---
 arch/x86/kernel/process.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/process.c +3 -3
index 39e585a..e28db18 100644
@@ -550,14 +550,14 @@ unsigned long get_wchan(struct task_struct *p)
 	if (sp < bottom || sp > top)
 		return 0;
 
-	fp = READ_ONCE(*(unsigned long *)sp);
+	fp = READ_ONCE_NOCHECK(*(unsigned long *)sp);
 	do {
 		if (fp < bottom || fp > top)
 			return 0;
-		ip = READ_ONCE(*(unsigned long *)(fp + sizeof(unsigned long)));
+		ip = READ_ONCE_NOCHECK(*(unsigned long *)(fp + sizeof(unsigned long)));
 		if (!in_sched_functions(ip))
 			return ip;
-		fp = READ_ONCE(*(unsigned long *)fp);
+		fp = READ_ONCE_NOCHECK(*(unsigned long *)fp);
 	} while (count++ < 16 && p->state != TASK_RUNNING);
 	return 0;
 }
Signature · 1 line
2.4.9
#8 lore raw

Re: [PATCH v2 2/2] x86/process: Silence KASAN warnings in get_wchan()

Quoted 24 lines · Andrey Ryabinin

get_wchan() is racy by design, it may access volatile stack of running task, thus it may access redzone in a stack frame and cause KASAN to warn about this.

Use READ_ONCE_NOCHECK() to silence these warnings.

---
 arch/x86/kernel/process.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/process.c +1 -1
index 39e585a..e28db18 100644
@@ -550,14 +550,14 @@ unsigned long get_wchan(struct task_struct *p)
 	if (sp < bottom || sp > top)
 		return 0;
 
-	fp = READ_ONCE(*(unsigned long *)sp);
+	fp = READ_ONCE_NOCHECK(*(unsigned long *)sp);
 	do {
 		if (fp < bottom || fp > top)

arch/x86/kernel/process.c unsigned long get_wchan(struct task_struct *p) · line 563

Quoted 10 lines
arch/x86/kernel/process.c +2 -2
 			return 0;
-		ip = READ_ONCE(*(unsigned long *)(fp + sizeof(unsigned long)));
+		ip = READ_ONCE_NOCHECK(*(unsigned long *)(fp + sizeof(unsigned long)));
 		if (!in_sched_functions(ip))
 			return ip;
-		fp = READ_ONCE(*(unsigned long *)fp);
+		fp = READ_ONCE_NOCHECK(*(unsigned long *)fp);
 	} while (count++ < 16 && p->state != TASK_RUNNING);
 	return 0;
 }

Hm, exactly how is the 'red zone' defined? Is this about the current task mostly, or when doing get_wchan() on other tasks?

Thanks,

Ingo

#9 lore raw

Re: [PATCH v2 2/2] x86/process: Silence KASAN warnings in get_wchan()

Quoted 32 lines · Ingo Molnar
Quoted 29 lines · Andrey Ryabinin

get_wchan() is racy by design, it may access volatile stack of running task, thus it may access redzone in a stack frame and cause KASAN to warn about this.

Use READ_ONCE_NOCHECK() to silence these warnings.

---
 arch/x86/kernel/process.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/process.c +2 -2
index 39e585a..e28db18 100644
@@ -550,14 +550,14 @@ unsigned long get_wchan(struct task_struct *p)
 	if (sp < bottom || sp > top)
 		return 0;
 
-	fp = READ_ONCE(*(unsigned long *)sp);
+	fp = READ_ONCE_NOCHECK(*(unsigned long *)sp);
 	do {
 		if (fp < bottom || fp > top)
 			return 0;
-		ip = READ_ONCE(*(unsigned long *)(fp + sizeof(unsigned long)));
+		ip = READ_ONCE_NOCHECK(*(unsigned long *)(fp + sizeof(unsigned long)));
 		if (!in_sched_functions(ip))
 			return ip;
Quoted 8 lines

arch/x86/kernel/process.c unsigned long get_wchan(struct task_struct *p) · line 563

Quoted 5 lines
arch/x86/kernel/process.c +1 -1
-		fp = READ_ONCE(*(unsigned long *)fp);
+		fp = READ_ONCE_NOCHECK(*(unsigned long *)fp);
 	} while (count++ < 16 && p->state != TASK_RUNNING);
 	return 0;
 }

Hm, exactly how is the 'red zone' defined? Is this about the current task mostly, or when doing get_wchan() on other tasks?

We doing get_whcan() *only* on other tasks:

520: if (!p || p == current || p->state == TASK_RUNNING)
521: return 0;

Current wouldn't be a problem for KASAN.

Quoted 4 lines

Thanks,

Ingo

#10 lore raw

Re: [PATCH v2 2/2] x86/process: Silence KASAN warnings in get_wchan()

In reply to Ingo Molnar
Quoted 32 lines · Ingo Molnar
Quoted 29 lines · Andrey Ryabinin

get_wchan() is racy by design, it may access volatile stack of running task, thus it may access redzone in a stack frame and cause KASAN to warn about this.

Use READ_ONCE_NOCHECK() to silence these warnings.

---
 arch/x86/kernel/process.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/process.c +2 -2
index 39e585a..e28db18 100644
@@ -550,14 +550,14 @@ unsigned long get_wchan(struct task_struct *p)
      if (sp < bottom || sp > top)
              return 0;
 
-     fp = READ_ONCE(*(unsigned long *)sp);
+     fp = READ_ONCE_NOCHECK(*(unsigned long *)sp);
      do {
              if (fp < bottom || fp > top)
                      return 0;
-             ip = READ_ONCE(*(unsigned long *)(fp + sizeof(unsigned long)));
+             ip = READ_ONCE_NOCHECK(*(unsigned long *)(fp + sizeof(unsigned long)));
              if (!in_sched_functions(ip))
                      return ip;
Quoted 8 lines

arch/x86/kernel/process.c unsigned long get_wchan(struct task_struct *p) · line 563

Quoted 5 lines
arch/x86/kernel/process.c +1 -1
-             fp = READ_ONCE(*(unsigned long *)fp);
+             fp = READ_ONCE_NOCHECK(*(unsigned long *)fp);
      } while (count++ < 16 && p->state != TASK_RUNNING);
      return 0;
 }

Hm, exactly how is the 'red zone' defined? Is this about the current task mostly, or when doing get_wchan() on other tasks?

When code is compiled with AddressSanitizer, most variables on stack have redzones around them, on entry function "poisons" these redzones (any accesses to them will be flagged), on exit function "unpoisons" these redzones.

#11 lore raw

Re: [PATCH v2 2/2] x86/process: Silence KASAN warnings in get_wchan()

Quoted 43 lines · Dmitry Vyukov
Quoted 32 lines · Ingo Molnar
Quoted 29 lines · Andrey Ryabinin

get_wchan() is racy by design, it may access volatile stack of running task, thus it may access redzone in a stack frame and cause KASAN to warn about this.

Use READ_ONCE_NOCHECK() to silence these warnings.

---
 arch/x86/kernel/process.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/process.c +2 -2
index 39e585a..e28db18 100644
@@ -550,14 +550,14 @@ unsigned long get_wchan(struct task_struct *p)
      if (sp < bottom || sp > top)
              return 0;
 
-     fp = READ_ONCE(*(unsigned long *)sp);
+     fp = READ_ONCE_NOCHECK(*(unsigned long *)sp);
      do {
              if (fp < bottom || fp > top)
                      return 0;
-             ip = READ_ONCE(*(unsigned long *)(fp + sizeof(unsigned long)));
+             ip = READ_ONCE_NOCHECK(*(unsigned long *)(fp + sizeof(unsigned long)));
              if (!in_sched_functions(ip))
                      return ip;
Quoted 8 lines

arch/x86/kernel/process.c unsigned long get_wchan(struct task_struct *p) · line 563

Quoted 5 lines
arch/x86/kernel/process.c +1 -1
-             fp = READ_ONCE(*(unsigned long *)fp);
+             fp = READ_ONCE_NOCHECK(*(unsigned long *)fp);
      } while (count++ < 16 && p->state != TASK_RUNNING);
      return 0;
 }

Hm, exactly how is the 'red zone' defined? Is this about the current task mostly, or when doing get_wchan() on other tasks?

Quoted 5 lines

When code is compiled with AddressSanitizer, most variables on stack have redzones around them, on entry function "poisons" these redzones (any accesses to them will be flagged), on exit function "unpoisons" these redzones.

An example bellow (stolen from slides - http://events.linuxfoundation.org/sites/events/files/slides/LinuxCon%20North%20America%202015%20KernelAddressSanitizer.pdf)

The following function:

void foo(void) {
	char a[328];
	...
	a[i] = 0;
}

will be transform by GCC to something like this:

void foo(void) {
       char redzone1[32];
       char a[328];
       char redzone2[24];
       char redzone3[32];

       int *shadow = (&redzone1 >> 3) + shadow_offset;
       shadow[0] = 0xf1f1f1f1; // poison redzone1
       shadow[11] = 0xf4f4f400; // poison redzone2
       shadow[12] = 0xf3f3f3f3; // poison redzone3

       ...
       __asan_store1(&a[i]); //check access to a[i]
       a[i] = 0;

       shadow[0] = shadow[11] = shadow[12] = 0; //unpoison redzones.
}
#12 lore raw

Re: [PATCH v2 2/2] x86/process: Silence KASAN warnings in get_wchan()

In reply to Dmitry Vyukov
Quoted 6 lines · Dmitry Vyukov
Quoted 2 lines

Hm, exactly how is the 'red zone' defined? Is this about the current task mostly, or when doing get_wchan() on other tasks?

When code is compiled with AddressSanitizer, most variables on stack have redzones around them, on entry function "poisons" these redzones (any accesses to them will be flagged), on exit function "unpoisons" these redzones.

I see, fair enough!

This series looks good to me, modulo the small documentation nit I had about the first patch.

Thanks,

Ingo

#13 lore raw

v3 cover Silence KASAN warnings in get_wchan()

In reply to Andrey Ryabinin

Changes since v2:
- Added some code comments in the first patch.

Andrey Ryabinin (2):
Provide READ_ONCE_NOCHECK()
x86/process: Silence KASAN warnings in get_wchan()

 arch/x86/kernel/process.c    |  6 ++---
 include/linux/compiler-gcc.h | 13 ++++++++++
 include/linux/compiler.h     | 60 ++++++++++++++++++++++++++++++++++----------
 3 files changed, 63 insertions(+), 16 deletions(-)
Signature · 1 line
2.4.9
#14 lore raw

PATCH v3 1/2 Provide READ_ONCE_NOCHECK()

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().

This patch creates __read_once_size_nocheck() a clone of __read_once_size_check() (renamed __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().

---
 include/linux/compiler-gcc.h | 13 ++++++++++
 include/linux/compiler.h     | 60 ++++++++++++++++++++++++++++++++++----------
 2 files changed, 60 insertions(+), 13 deletions(-)
include/linux/compiler-gcc.h +13 -0
index dfaa7b3..f2a9aec 100644
@@ -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.
+ * Confilcts 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
include/linux/compiler.h +47 -13

74 lines not on this page · raw message

Signature · 1 line
2.4.9
#15 lore raw

[tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

compiler, atomics: Provide READ_ONCE_NOCHECK()
Commit-ID:  4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Gitweb:     http://git.kernel.org/tip/4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Author:     Andrey Ryabinin <[email protected]>
AuthorDate: Tue, 13 Oct 2015 18:28:07 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 14 Oct 2015 16:44:06 +0200

compiler, atomics: Provide READ_ONCE_NOCHECK()

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().

This patch creates __read_once_size_nocheck() a clone of __read_once_size_check() (renamed __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().

Cc Alexander Potapenko <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Kostya Serebryany <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Wolfram Gloger <[email protected]>
Cc kasan-dev <[email protected]>
---
 include/linux/compiler-gcc.h | 13 ++++++++++
 include/linux/compiler.h     | 60 ++++++++++++++++++++++++++++++++++----------
 2 files changed, 60 insertions(+), 13 deletions(-)
include/linux/compiler-gcc.h +13 -0
index dfaa7b3..f2a9aec 100644
@@ -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.
+ * Confilcts 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
include/linux/compiler.h +47 -13

74 lines not on this page · raw message

#16 lore raw

Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

Quoted 25 lines · tip-bot for Andrey Ryabinin
Commit-ID:  4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Gitweb:     http://git.kernel.org/tip/4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Author:     Andrey Ryabinin <[email protected]>
AuthorDate: Tue, 13 Oct 2015 18:28:07 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 14 Oct 2015 16:44:06 +0200

compiler, atomics: Provide READ_ONCE_NOCHECK()

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().

This patch creates __read_once_size_nocheck() a clone of __read_once_size_check() (renamed __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

Quoted 1 line

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

So I add READ_ONCE_NOCHECK() for accesses for which the compiler cannot prove safe address for KASAN's benefit, but READ_ONCE() suffices for the data-race-detection logic in KTSAN, correct?

Thanx, Paul

Quoted 134 lines
Cc Alexander Potapenko <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Kostya Serebryany <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Wolfram Gloger <[email protected]>
Cc kasan-dev <[email protected]>
---
 include/linux/compiler-gcc.h | 13 ++++++++++
 include/linux/compiler.h     | 60 ++++++++++++++++++++++++++++++++++----------
 2 files changed, 60 insertions(+), 13 deletions(-)
include/linux/compiler-gcc.h +13 -0
index dfaa7b3..f2a9aec 100644
@@ -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.
+ * Confilcts 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
include/linux/compiler.h +47 -13
index c836eb2..aa2ae4c 100644
@@ -198,19 +198,42 @@ 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_check(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 __alias(__read_once_size_check)
+void __read_once_size_nocheck(const volatile void *p, void *res, int size);
+#endif
 
 static __always_inline void __write_once_size(volatile void *p, void *res, int size)
 {
@@ -248,8 +271,19 @@ 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;		\
+	__read_once_size##check(&(x), __u.__c, sizeof(x));	\
+	__u.__val;						\
+})
+#define READ_ONCE(x) __READ_ONCE(x, _check)
+
+/*
+ * 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, _nocheck)
 
 #define WRITE_ONCE(x, val) \
 ({							\
#17 lore raw

Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

Quoted 26 lines · Paul E. McKenney
Quoted 25 lines · tip-bot for Andrey Ryabinin
Commit-ID:  4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Gitweb:     http://git.kernel.org/tip/4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Author:     Andrey Ryabinin <[email protected]>
AuthorDate: Tue, 13 Oct 2015 18:28:07 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 14 Oct 2015 16:44:06 +0200

compiler, atomics: Provide READ_ONCE_NOCHECK()

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().

This patch creates __read_once_size_nocheck() a clone of __read_once_size_check() (renamed __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

Quoted 5 lines
Quoted 1 line

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

So I add READ_ONCE_NOCHECK() for accesses for which the compiler cannot prove safe address for KASAN's benefit, but READ_ONCE() suffices for the data-race-detection logic in KTSAN, correct?

KTSAN also needs READ_ONCE_NOCHECK() here. KTSAN will flag races between get_wchan() and the thread accesses to own stack even more aggressively than KASAN, because KTSAN won't like get_wchan() accesses even to non-poisoned areas of other thread stack.

Quoted 143 lines

Thanx, Paul

Quoted 124 lines
Cc Alexander Potapenko <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Kostya Serebryany <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Wolfram Gloger <[email protected]>
Cc kasan-dev <[email protected]>
---
 include/linux/compiler-gcc.h | 13 ++++++++++
 include/linux/compiler.h     | 60 ++++++++++++++++++++++++++++++++++----------
 2 files changed, 60 insertions(+), 13 deletions(-)
include/linux/compiler-gcc.h +13 -0
index dfaa7b3..f2a9aec 100644
@@ -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.
+ * Confilcts 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
include/linux/compiler.h +40 -13
index c836eb2..aa2ae4c 100644
@@ -198,19 +198,42 @@ 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_check(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 __alias(__read_once_size_check)
+void __read_once_size_nocheck(const volatile void *p, void *res, int size);
+#endif
 
 static __always_inline void __write_once_size(volatile void *p, void *res, int size)
 {
@@ -248,8 +271,19 @@ 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;            \
+     __read_once_size##check(&(x), __u.__c, sizeof(x));      \
+     __u.__val;                                              \
+})

include/linux/compiler.h static __always_inline void __write_once_size(volatile void *p, void *res, int s · line 289

Quoted 10 lines
include/linux/compiler.h +7 -0
+#define READ_ONCE(x) __READ_ONCE(x, _check)
+
+/*
+ * 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, _nocheck)
 
 #define WRITE_ONCE(x, val) \
 ({                                                   \

--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20151014154532.GV3910%40linux.vnet.ibm.com.
For more options, visit https://groups.google.com/d/optout.

#18 lore raw

Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

Quoted 30 lines · Dmitry Vyukov
Quoted 28 lines · Paul E. McKenney
Quoted 26 lines · tip-bot for Andrey Ryabinin
Commit-ID:  4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Gitweb:     http://git.kernel.org/tip/4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Author:     Andrey Ryabinin <[email protected]>
AuthorDate: Tue, 13 Oct 2015 18:28:07 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 14 Oct 2015 16:44:06 +0200

compiler, atomics: Provide READ_ONCE_NOCHECK()

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().

This patch creates __read_once_size_nocheck() a clone of __read_once_size_check() (renamed __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().

Quoted 8 lines
Quoted 3 lines

So I add READ_ONCE_NOCHECK() for accesses for which the compiler cannot prove safe address for KASAN's benefit, but READ_ONCE() suffices for the data-race-detection logic in KTSAN, correct?

KTSAN also needs READ_ONCE_NOCHECK() here. KTSAN will flag races between get_wchan() and the thread accesses to own stack even more aggressively than KASAN, because KTSAN won't like get_wchan() accesses even to non-poisoned areas of other thread stack.

So to keep KTSAN happy, any read from some other thread's stack requires READ_ONCE_NOCHECK()? What if the access is via a locking primitive or read-modify-write atomic operation?

This is of some interest in RCU, which implements synchronous grace periods using completions that are allocated on the calling task's stack and manipulated by RCU callbacks that are likely executing elsewhere.

Thanx, Paul

Quoted 142 lines
Quoted 141 lines
Quoted 124 lines
Cc Alexander Potapenko <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Kostya Serebryany <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Wolfram Gloger <[email protected]>
Cc kasan-dev <[email protected]>
---
 include/linux/compiler-gcc.h | 13 ++++++++++
 include/linux/compiler.h     | 60 ++++++++++++++++++++++++++++++++++----------
 2 files changed, 60 insertions(+), 13 deletions(-)
include/linux/compiler-gcc.h +13 -0
index dfaa7b3..f2a9aec 100644
@@ -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.
+ * Confilcts 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
include/linux/compiler.h +40 -13
index c836eb2..aa2ae4c 100644
@@ -198,19 +198,42 @@ 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_check(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 __alias(__read_once_size_check)
+void __read_once_size_nocheck(const volatile void *p, void *res, int size);
+#endif
 
 static __always_inline void __write_once_size(volatile void *p, void *res, int size)
 {
@@ -248,8 +271,19 @@ 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;            \
+     __read_once_size##check(&(x), __u.__c, sizeof(x));      \
+     __u.__val;                                              \
+})

include/linux/compiler.h static __always_inline void __write_once_size(volatile void *p, void *res, int s · line 289

Quoted 10 lines
include/linux/compiler.h +7 -0
+#define READ_ONCE(x) __READ_ONCE(x, _check)
+
+/*
+ * 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, _nocheck)
 
 #define WRITE_ONCE(x, val) \
 ({                                                   \

--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20151014154532.GV3910%40linux.vnet.ibm.com.
For more options, visit https://groups.google.com/d/optout.

#19 lore raw

Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

Quoted 40 lines · Paul E. McKenney
Quoted 30 lines · Dmitry Vyukov
Quoted 28 lines · Paul E. McKenney
Quoted 26 lines · tip-bot for Andrey Ryabinin
Commit-ID:  4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Gitweb:     http://git.kernel.org/tip/4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Author:     Andrey Ryabinin <[email protected]>
AuthorDate: Tue, 13 Oct 2015 18:28:07 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 14 Oct 2015 16:44:06 +0200

compiler, atomics: Provide READ_ONCE_NOCHECK()

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().

This patch creates __read_once_size_nocheck() a clone of __read_once_size_check() (renamed __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().

Quoted 8 lines
Quoted 3 lines

So I add READ_ONCE_NOCHECK() for accesses for which the compiler cannot prove safe address for KASAN's benefit, but READ_ONCE() suffices for the data-race-detection logic in KTSAN, correct?

KTSAN also needs READ_ONCE_NOCHECK() here. KTSAN will flag races between get_wchan() and the thread accesses to own stack even more aggressively than KASAN, because KTSAN won't like get_wchan() accesses even to non-poisoned areas of other thread stack.

Quoted 7 lines

So to keep KTSAN happy, any read from some other thread's stack requires READ_ONCE_NOCHECK()? What if the access is via a locking primitive or read-modify-write atomic operation?

This is of some interest in RCU, which implements synchronous grace periods using completions that are allocated on the calling task's stack and manipulated by RCU callbacks that are likely executing elsewhere.

KTSAN does not have any special logic for stacks. It just generally flags pairs of accesses when (1) at least one access is not atomic,
(2) at least one access is a write and (3) these accesses are not synchronized by means of other synchronization.
There is a bunch of cases when kernel code allocates objects on stack and then passes them to other threads, but as far as there is proper synchronization it is OK.

For the record, KTSAN is this:
https://github.com/google/ktsan
https://github.com/google/ktsan/wiki/Found-Bugs

#20 lore raw

Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

Quoted 18 lines · Dmitry Vyukov
Quoted 9 lines
Quoted 8 lines
Quoted 3 lines

So I add READ_ONCE_NOCHECK() for accesses for which the compiler cannot prove safe address for KASAN's benefit, but READ_ONCE() suffices for the data-race-detection logic in KTSAN, correct?

KTSAN also needs READ_ONCE_NOCHECK() here. KTSAN will flag races between get_wchan() and the thread accesses to own stack even more aggressively than KASAN, because KTSAN won't like get_wchan() accesses even to non-poisoned areas of other thread stack.

Quoted 7 lines

So to keep KTSAN happy, any read from some other thread's stack requires READ_ONCE_NOCHECK()? What if the access is via a locking primitive or read-modify-write atomic operation?

This is of some interest in RCU, which implements synchronous grace periods using completions that are allocated on the calling task's stack and manipulated by RCU callbacks that are likely executing elsewhere.

Quoted 4 lines

KTSAN does not have any special logic for stacks. It just generally flags pairs of accesses when (1) at least one access is not atomic,
(2) at least one access is a write and (3) these accesses are not synchronized by means of other synchronization.

But but but.. WRITE_ONCE/READ_ONCE _are_ atomic when on naturally aligned machine word sized thingies. We very much rely on that.

And the wchan thing is very much that, its not some weird large object, its a single word, read with an explicit 'volatile' cast.

This is good, and should not require more magic annotations.

#21 lore raw

Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

Quoted 24 lines · Peter Zijlstra
Quoted 18 lines · Dmitry Vyukov
Quoted 9 lines
Quoted 8 lines
Quoted 3 lines

So I add READ_ONCE_NOCHECK() for accesses for which the compiler cannot prove safe address for KASAN's benefit, but READ_ONCE() suffices for the data-race-detection logic in KTSAN, correct?

KTSAN also needs READ_ONCE_NOCHECK() here. KTSAN will flag races between get_wchan() and the thread accesses to own stack even more aggressively than KASAN, because KTSAN won't like get_wchan() accesses even to non-poisoned areas of other thread stack.

Quoted 7 lines

So to keep KTSAN happy, any read from some other thread's stack requires READ_ONCE_NOCHECK()? What if the access is via a locking primitive or read-modify-write atomic operation?

This is of some interest in RCU, which implements synchronous grace periods using completions that are allocated on the calling task's stack and manipulated by RCU callbacks that are likely executing elsewhere.

Quoted 4 lines

KTSAN does not have any special logic for stacks. It just generally flags pairs of accesses when (1) at least one access is not atomic,
(2) at least one access is a write and (3) these accesses are not synchronized by means of other synchronization.

Quoted 7 lines

But but but.. WRITE_ONCE/READ_ONCE _are_ atomic when on naturally aligned machine word sized thingies. We very much rely on that.

And the wchan thing is very much that, its not some weird large object, its a single word, read with an explicit 'volatile' cast.

This is good, and should not require more magic annotations.

Well, if another thread writes it byte-by-byte, it pretty much does not matter how you read it.
Note that I said "at least one access is not atomic". If both are atomic, then this is, of course, legal. And KTSAN considers READ/WRITE_ONCE as atomic operations.

#22 lore raw

Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

Quoted 6 lines · Dmitry Vyukov

Well, if another thread writes it byte-by-byte, it pretty much does not matter how you read it.
Note that I said "at least one access is not atomic". If both are atomic, then this is, of course, legal. And KTSAN considers READ/WRITE_ONCE as atomic operations.

OK, then I'm confused on what exactly the annotation does, but less worried.

#23 lore raw

Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

Quoted 10 lines · Peter Zijlstra
Quoted 6 lines · Dmitry Vyukov

Well, if another thread writes it byte-by-byte, it pretty much does not matter how you read it.
Note that I said "at least one access is not atomic". If both are atomic, then this is, of course, legal. And KTSAN considers READ/WRITE_ONCE as atomic operations.

OK, then I'm confused on what exactly the annotation does, but less worried.

The annotation says "hey, KASAN (etc), don't worry if you think that the memory being accessed is out of bounds". Presumably KTSAN is okay with the operation because it's atomic, but KASAN dislikes it because it's accessing memory that is out of bounds from the perspective of a C program.

I'd still rather find a way to just delete get_wchan, but whatever.

--Andy

#24 lore raw

Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

Quoted 9 lines · Andy Lutomirski
Quoted 8 lines · Peter Zijlstra
Quoted 6 lines · Dmitry Vyukov

Well, if another thread writes it byte-by-byte, it pretty much does not matter how you read it.
Note that I said "at least one access is not atomic". If both are atomic, then this is, of course, legal. And KTSAN considers READ/WRITE_ONCE as atomic operations.

Quoted 8 lines
Quoted 2 lines

OK, then I'm confused on what exactly the annotation does, but less worried.

The annotation says "hey, KASAN (etc), don't worry if you think that the memory being accessed is out of bounds". Presumably KTSAN is okay with the operation because it's atomic, but KASAN dislikes it because it's accessing memory that is out of bounds from the perspective of a C program.

There's going to be more of that..

Quoted 1 line

I'd still rather find a way to just delete get_wchan, but whatever.

:-)

#25 lore raw

Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

Quoted 3 lines · Peter Zijlstra
Quoted 1 line

I'd still rather find a way to just delete get_wchan, but whatever.

:-)

AFAICS can only do that at the price of slowing down various scheduler functions by saving the caller address.

Thanks,

Ingo

#26 lore raw

Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

Quoted 10 lines · Ingo Molnar
Quoted 3 lines · Peter Zijlstra
Quoted 1 line

I'd still rather find a way to just delete get_wchan, but whatever.

:-)

AFAICS can only do that at the price of slowing down various scheduler functions by saving the caller address.

A quick check on my machine:

# for i in /proc/*/wchan; do cat $i && echo ''; done |sort |uniq

doesn't have very much of interest to say:

0
devtmpfsd
dmcrypt_write
do_sigtimedwait
do_wait
ep_poll
fsnotify_mark_destroy
futex_wait_queue_me
hrtimer_nanosleep
irq_thread
kauditd_thread
khugepaged
kjournald2
ksm_scan_thread
kswapd
kthreadd
pipe_wait
poll_schedule_timeout
rcu_gp_kthread
rcu_nocb_kthread
rescuer_thread
scsi_error_handler
sk_wait_data
smpboot_thread_fn
unix_stream_recvmsg
wait_woken
worker_thread
xfsaild

A bunch of those look like they're specific to kernel threads, for which this whole mechanism is probably pointless -- just reading /proc/PID/stack (with suitable privilege) is probably better.

For the rest, a few are useful, but I find myself wondering whether this mechanism is really useful enough to be worth keeping. We could just return "asleep" in /proc/PID/wchan.

A more interesting thing to do might be to try to decode regs->orig_ax to give a guess as to which syscall is asleep. But this seems like a lot of fiddling and a lot of worry about security issues for a mechanism of dubious value. Also:

$ cat /proc/1/wchan
ep_poll

Why do we allow unprivileged queries like that at all?

--Andy

#27 lore raw

Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

In reply to Peter Zijlstra
Quoted 10 lines · Peter Zijlstra
Quoted 6 lines · Dmitry Vyukov

Well, if another thread writes it byte-by-byte, it pretty much does not matter how you read it.
Note that I said "at least one access is not atomic". If both are atomic, then this is, of course, legal. And KTSAN considers READ/WRITE_ONCE as atomic operations.

OK, then I'm confused on what exactly the annotation does, but less worried.

The plan is to make READ_ONCE_NOCHECK ignored by KTSAN, just it is ignored by KASAN. So that it never leads to a report ("not checked").

#28 lore raw

Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

Quoted 9 lines · Dmitry Vyukov
Quoted 8 lines · Peter Zijlstra
Quoted 6 lines · Dmitry Vyukov

Well, if another thread writes it byte-by-byte, it pretty much does not matter how you read it.
Note that I said "at least one access is not atomic". If both are atomic, then this is, of course, legal. And KTSAN considers READ/WRITE_ONCE as atomic operations.

Quoted 5 lines
Quoted 2 lines

OK, then I'm confused on what exactly the annotation does, but less worried.

The plan is to make READ_ONCE_NOCHECK ignored by KTSAN, just it is ignored by KASAN. So that it never leads to a report ("not checked").

Would a _NOKSAN suffix not be more appropriate? NOCHECK seems somewhat generic.

#29 lore raw

Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

In reply to Dmitry Vyukov
Quoted 50 lines · Dmitry Vyukov
Quoted 40 lines · Paul E. McKenney
Quoted 30 lines · Dmitry Vyukov
Quoted 28 lines · Paul E. McKenney
Quoted 26 lines · tip-bot for Andrey Ryabinin
Commit-ID:  4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Gitweb:     http://git.kernel.org/tip/4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Author:     Andrey Ryabinin <[email protected]>
AuthorDate: Tue, 13 Oct 2015 18:28:07 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 14 Oct 2015 16:44:06 +0200

compiler, atomics: Provide READ_ONCE_NOCHECK()

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().

This patch creates __read_once_size_nocheck() a clone of __read_once_size_check() (renamed __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().

Quoted 8 lines
Quoted 3 lines

So I add READ_ONCE_NOCHECK() for accesses for which the compiler cannot prove safe address for KASAN's benefit, but READ_ONCE() suffices for the data-race-detection logic in KTSAN, correct?

KTSAN also needs READ_ONCE_NOCHECK() here. KTSAN will flag races between get_wchan() and the thread accesses to own stack even more aggressively than KASAN, because KTSAN won't like get_wchan() accesses even to non-poisoned areas of other thread stack.

Quoted 7 lines

So to keep KTSAN happy, any read from some other thread's stack requires READ_ONCE_NOCHECK()? What if the access is via a locking primitive or read-modify-write atomic operation?

This is of some interest in RCU, which implements synchronous grace periods using completions that are allocated on the calling task's stack and manipulated by RCU callbacks that are likely executing elsewhere.

Quoted 7 lines

KTSAN does not have any special logic for stacks. It just generally flags pairs of accesses when (1) at least one access is not atomic,
(2) at least one access is a write and (3) these accesses are not synchronized by means of other synchronization.
There is a bunch of cases when kernel code allocates objects on stack and then passes them to other threads, but as far as there is proper synchronization it is OK.

OK, so let me see if I understand this. ;-)

KASAN requires READ_ONCE_NOCHECK() for get_wchan(). KTSAN would be just as happy with READ_ONCE(), but READ_ONCE_NOCHECK() works for both.

Did I get it right?

Thanx, Paul

Quoted 4 lines

For the record, KTSAN is this:
https://github.com/google/ktsan
https://github.com/google/ktsan/wiki/Found-Bugs

#30 lore raw

Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

Quoted 59 lines · Paul E. McKenney
Quoted 50 lines · Dmitry Vyukov
Quoted 40 lines · Paul E. McKenney
Quoted 30 lines · Dmitry Vyukov
Quoted 28 lines · Paul E. McKenney
Quoted 26 lines · tip-bot for Andrey Ryabinin
Commit-ID:  4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Gitweb:     http://git.kernel.org/tip/4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Author:     Andrey Ryabinin <[email protected]>
AuthorDate: Tue, 13 Oct 2015 18:28:07 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 14 Oct 2015 16:44:06 +0200

compiler, atomics: Provide READ_ONCE_NOCHECK()

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().

This patch creates __read_once_size_nocheck() a clone of __read_once_size_check() (renamed __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().

Quoted 8 lines
Quoted 3 lines

So I add READ_ONCE_NOCHECK() for accesses for which the compiler cannot prove safe address for KASAN's benefit, but READ_ONCE() suffices for the data-race-detection logic in KTSAN, correct?

KTSAN also needs READ_ONCE_NOCHECK() here. KTSAN will flag races between get_wchan() and the thread accesses to own stack even more aggressively than KASAN, because KTSAN won't like get_wchan() accesses even to non-poisoned areas of other thread stack.

Quoted 7 lines

So to keep KTSAN happy, any read from some other thread's stack requires READ_ONCE_NOCHECK()? What if the access is via a locking primitive or read-modify-write atomic operation?

This is of some interest in RCU, which implements synchronous grace periods using completions that are allocated on the calling task's stack and manipulated by RCU callbacks that are likely executing elsewhere.

Quoted 7 lines

KTSAN does not have any special logic for stacks. It just generally flags pairs of accesses when (1) at least one access is not atomic,
(2) at least one access is a write and (3) these accesses are not synchronized by means of other synchronization.
There is a bunch of cases when kernel code allocates objects on stack and then passes them to other threads, but as far as there is proper synchronization it is OK.

Quoted 7 lines

OK, so let me see if I understand this. ;-)

KASAN requires READ_ONCE_NOCHECK() for get_wchan(). KTSAN would be just as happy with READ_ONCE(), but READ_ONCE_NOCHECK() works for both.

Did I get it right?

No, KTSAN also needs READ_ONCE_NOCHECK. READ_ONCE in get_wchan can lead to a data race report. Consider:

// the other thead
some_stack_var = ...;

// get_wchan
bp = READ_ONCE(p); // where p happens to point to some_stack_var in the other thread

This is generally not atomic and not safe. And this is a data race by all possible definitions.
Only READ_ONCE on reading side is not enough to ensure atomicity, also all concurrent writes must be done with atomic operations.

#31 lore raw

Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

Quoted 77 lines · Dmitry Vyukov
Quoted 59 lines · Paul E. McKenney
Quoted 50 lines · Dmitry Vyukov
Quoted 40 lines · Paul E. McKenney
Quoted 30 lines · Dmitry Vyukov
Quoted 28 lines · Paul E. McKenney
Quoted 26 lines · tip-bot for Andrey Ryabinin
Commit-ID:  4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Gitweb:     http://git.kernel.org/tip/4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Author:     Andrey Ryabinin <[email protected]>
AuthorDate: Tue, 13 Oct 2015 18:28:07 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 14 Oct 2015 16:44:06 +0200

compiler, atomics: Provide READ_ONCE_NOCHECK()

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().

This patch creates __read_once_size_nocheck() a clone of __read_once_size_check() (renamed __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().

Quoted 8 lines
Quoted 3 lines

So I add READ_ONCE_NOCHECK() for accesses for which the compiler cannot prove safe address for KASAN's benefit, but READ_ONCE() suffices for the data-race-detection logic in KTSAN, correct?

KTSAN also needs READ_ONCE_NOCHECK() here. KTSAN will flag races between get_wchan() and the thread accesses to own stack even more aggressively than KASAN, because KTSAN won't like get_wchan() accesses even to non-poisoned areas of other thread stack.

Quoted 7 lines

So to keep KTSAN happy, any read from some other thread's stack requires READ_ONCE_NOCHECK()? What if the access is via a locking primitive or read-modify-write atomic operation?

This is of some interest in RCU, which implements synchronous grace periods using completions that are allocated on the calling task's stack and manipulated by RCU callbacks that are likely executing elsewhere.

Quoted 7 lines

KTSAN does not have any special logic for stacks. It just generally flags pairs of accesses when (1) at least one access is not atomic,
(2) at least one access is a write and (3) these accesses are not synchronized by means of other synchronization.
There is a bunch of cases when kernel code allocates objects on stack and then passes them to other threads, but as far as there is proper synchronization it is OK.

Quoted 7 lines

OK, so let me see if I understand this. ;-)

KASAN requires READ_ONCE_NOCHECK() for get_wchan(). KTSAN would be just as happy with READ_ONCE(), but READ_ONCE_NOCHECK() works for both.

Did I get it right?

No, KTSAN also needs READ_ONCE_NOCHECK. READ_ONCE in get_wchan can lead to a data race report. Consider:

// the other thead
some_stack_var = ...;

Quoted 8 lines

// get_wchan
bp = READ_ONCE(p); // where p happens to point to some_stack_var in the other thread

This is generally not atomic and not safe. And this is a data race by all possible definitions.
Only READ_ONCE on reading side is not enough to ensure atomicity, also all concurrent writes must be done with atomic operations.

OK. However, this is specific to get_wchan()'s out-of-bounds stack, right? If I have multiple tasks accessing some other task's on-stack variable, then as long as all other potentially concurrent accesses use atomic operations, READ_ONCE() suffices. Or am I still missing something here?

Thanx, Paul

#32 lore raw

Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

Quoted 73 lines · Paul E. McKenney
Quoted 73 lines
Quoted 53 lines
Quoted 45 lines
Quoted 37 lines
Quoted 28 lines
Quoted 28 lines · tip-bot for Andrey Ryabinin
Quoted 26 lines
Commit-ID:  4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Gitweb:     http://git.kernel.org/tip/4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Author:     Andrey Ryabinin <[email protected]>
AuthorDate: Tue, 13 Oct 2015 18:28:07 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 14 Oct 2015 16:44:06 +0200

compiler, atomics: Provide READ_ONCE_NOCHECK()

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().

This patch creates __read_once_size_nocheck() a clone of __read_once_size_check() (renamed __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().

Quoted 8 lines
Quoted 3 lines

So I add READ_ONCE_NOCHECK() for accesses for which the compiler cannot prove safe address for KASAN's benefit, but READ_ONCE() suffices for the data-race-detection logic in KTSAN, correct?

KTSAN also needs READ_ONCE_NOCHECK() here. KTSAN will flag races between get_wchan() and the thread accesses to own stack even more aggressively than KASAN, because KTSAN won't like get_wchan() accesses even to non-poisoned areas of other thread stack.

Quoted 7 lines

So to keep KTSAN happy, any read from some other thread's stack requires READ_ONCE_NOCHECK()? What if the access is via a locking primitive or read-modify-write atomic operation?

This is of some interest in RCU, which implements synchronous grace periods using completions that are allocated on the calling task's stack and manipulated by RCU callbacks that are likely executing elsewhere.

Quoted 7 lines

KTSAN does not have any special logic for stacks. It just generally flags pairs of accesses when (1) at least one access is not atomic,
(2) at least one access is a write and (3) these accesses are not synchronized by means of other synchronization.
There is a bunch of cases when kernel code allocates objects on stack and then passes them to other threads, but as far as there is proper synchronization it is OK.

Quoted 7 lines

OK, so let me see if I understand this. ;-)

KASAN requires READ_ONCE_NOCHECK() for get_wchan(). KTSAN would be just as happy with READ_ONCE(), but READ_ONCE_NOCHECK() works for both.

Did I get it right?

No, KTSAN also needs READ_ONCE_NOCHECK. READ_ONCE in get_wchan can lead to a data race report. Consider:

// the other thead
some_stack_var = ...;

// get_wchan
bp = READ_ONCE(p); // where p happens to point to some_stack_var in the other thread

Quoted 6 lines
Quoted 4 lines

This is generally not atomic and not safe. And this is a data race by all possible definitions.
Only READ_ONCE on reading side is not enough to ensure atomicity, also all concurrent writes must be done with atomic operations.

OK. However, this is specific to get_wchan()'s out-of-bounds stack, right?

Yes... and no.
This is specific to racy accesses, not necessary to out-of-bounds stack. If you have a data race in-bounds, it is also not OK :)

Quoted 3 lines

If I have multiple tasks accessing some other task's on-stack variable, then as long as all other potentially concurrent accesses use atomic operations, READ_ONCE() suffices. Or am I still missing something here?

This is correct.
Generally, the idea is that KTSAN flags what you think is a bug, and does not flag what you think is not a bug. If you have all proper synchronization in place, then KTSAN should not flag that; whether the object is on stack or not is irrelevant.

#33 lore raw

Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

Quoted 80 lines · Dmitry Vyukov
Quoted 78 lines · Paul E. McKenney
Quoted 77 lines
Quoted 53 lines
Quoted 45 lines
Quoted 37 lines
Quoted 28 lines
Quoted 28 lines · tip-bot for Andrey Ryabinin
Quoted 26 lines
Commit-ID:  4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Gitweb:     http://git.kernel.org/tip/4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Author:     Andrey Ryabinin <[email protected]>
AuthorDate: Tue, 13 Oct 2015 18:28:07 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 14 Oct 2015 16:44:06 +0200

compiler, atomics: Provide READ_ONCE_NOCHECK()

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().

This patch creates __read_once_size_nocheck() a clone of __read_once_size_check() (renamed __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().

Quoted 8 lines
Quoted 3 lines

So I add READ_ONCE_NOCHECK() for accesses for which the compiler cannot prove safe address for KASAN's benefit, but READ_ONCE() suffices for the data-race-detection logic in KTSAN, correct?

KTSAN also needs READ_ONCE_NOCHECK() here. KTSAN will flag races between get_wchan() and the thread accesses to own stack even more aggressively than KASAN, because KTSAN won't like get_wchan() accesses even to non-poisoned areas of other thread stack.

Quoted 7 lines

So to keep KTSAN happy, any read from some other thread's stack requires READ_ONCE_NOCHECK()? What if the access is via a locking primitive or read-modify-write atomic operation?

This is of some interest in RCU, which implements synchronous grace periods using completions that are allocated on the calling task's stack and manipulated by RCU callbacks that are likely executing elsewhere.

Quoted 7 lines

KTSAN does not have any special logic for stacks. It just generally flags pairs of accesses when (1) at least one access is not atomic,
(2) at least one access is a write and (3) these accesses are not synchronized by means of other synchronization.
There is a bunch of cases when kernel code allocates objects on stack and then passes them to other threads, but as far as there is proper synchronization it is OK.

Quoted 7 lines

OK, so let me see if I understand this. ;-)

KASAN requires READ_ONCE_NOCHECK() for get_wchan(). KTSAN would be just as happy with READ_ONCE(), but READ_ONCE_NOCHECK() works for both.

Did I get it right?

No, KTSAN also needs READ_ONCE_NOCHECK. READ_ONCE in get_wchan can lead to a data race report. Consider:

// the other thead
some_stack_var = ...;

// get_wchan
bp = READ_ONCE(p); // where p happens to point to some_stack_var in the other thread

This is generally not atomic and not safe. And this is a data race by all possible definitions.
Only READ_ONCE on reading side is not enough to ensure atomicity, also all concurrent writes must be done with atomic operations.

Quoted 5 lines
Quoted 1 line

OK. However, this is specific to get_wchan()'s out-of-bounds stack, right?

Yes... and no.
This is specific to racy accesses, not necessary to out-of-bounds stack. If you have a data race in-bounds, it is also not OK :)

But a non-data-race out of bounds would presumably trigger KASAN but not KTSAN.

Quoted 9 lines
Quoted 3 lines

If I have multiple tasks accessing some other task's on-stack variable, then as long as all other potentially concurrent accesses use atomic operations, READ_ONCE() suffices. Or am I still missing something here?

This is correct.
Generally, the idea is that KTSAN flags what you think is a bug, and does not flag what you think is not a bug. If you have all proper synchronization in place, then KTSAN should not flag that; whether the object is on stack or not is irrelevant.

Good!

Thanx, Paul

#34 lore raw

Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

In reply to Dmitry Vyukov
Quoted 28 lines · Dmitry Vyukov
Quoted 26 lines · Paul E. McKenney
Quoted 25 lines · tip-bot for Andrey Ryabinin
Commit-ID:  4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Gitweb:     http://git.kernel.org/tip/4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Author:     Andrey Ryabinin <[email protected]>
AuthorDate: Tue, 13 Oct 2015 18:28:07 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 14 Oct 2015 16:44:06 +0200

compiler, atomics: Provide READ_ONCE_NOCHECK()

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().

This patch creates __read_once_size_nocheck() a clone of __read_once_size_check() (renamed __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

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

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

So I add READ_ONCE_NOCHECK() for accesses for which the compiler cannot prove safe address for KASAN's benefit, but READ_ONCE() suffices for the data-race-detection logic in KTSAN, correct?

KTSAN also needs READ_ONCE_NOCHECK() here.

Does it? What's the difference between READ_ONCE_NOCHECK() and READ_ONCE() with KTSAN=y? AFAIK READ_ONCE() is sufficient to hide race from KTSAN. It doesn't *require* READ_ONCE_NOCHECK(), right?

Quoted 5 lines

KTSAN will flag races
between get_wchan() and the thread accesses to own stack even more aggressively than KASAN, because KTSAN won't like get_wchan() accesses even to non-poisoned areas of other thread stack.

#35 lore raw

Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

Quoted 33 lines · Andrey Ryabinin
Quoted 30 lines · Dmitry Vyukov
Quoted 28 lines · Paul E. McKenney
Quoted 26 lines · tip-bot for Andrey Ryabinin
Commit-ID:  4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Gitweb:     http://git.kernel.org/tip/4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Author:     Andrey Ryabinin <[email protected]>
AuthorDate: Tue, 13 Oct 2015 18:28:07 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 14 Oct 2015 16:44:06 +0200

compiler, atomics: Provide READ_ONCE_NOCHECK()

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().

This patch creates __read_once_size_nocheck() a clone of __read_once_size_check() (renamed __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().

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

So I add READ_ONCE_NOCHECK() for accesses for which the compiler cannot prove safe address for KASAN's benefit, but READ_ONCE() suffices for the data-race-detection logic in KTSAN, correct?

KTSAN also needs READ_ONCE_NOCHECK() here.

Does it? What's the difference between READ_ONCE_NOCHECK() and READ_ONCE() with KTSAN=y? AFAIK READ_ONCE() is sufficient to hide race from KTSAN. It doesn't *require* READ_ONCE_NOCHECK(), right?

For not there is no difference, because you just added READ_ONCE_NOCHECK and we have not yet supported it.
But my plan is to completely ignore accessed from READ_ONCE_NOCHECK in KTSAN so that they never lead to race reports.

READ_ONCE in get_wchan still can lead to a data race report, because it is READ_ONCE in get_wchan versus a normal write to stack in the other thread. That is not atomic and not generally safe.

#36 lore raw

Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK ()

Quoted 45 lines · Dmitry Vyukov
Quoted 33 lines · Andrey Ryabinin
Quoted 30 lines · Dmitry Vyukov
Quoted 28 lines · Paul E. McKenney
Quoted 26 lines · tip-bot for Andrey Ryabinin
Commit-ID:  4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Gitweb:     http://git.kernel.org/tip/4115ffdf4d6f8986a7abe1dd522c163f599bc0e6
Author:     Andrey Ryabinin <[email protected]>
AuthorDate: Tue, 13 Oct 2015 18:28:07 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 14 Oct 2015 16:44:06 +0200

compiler, atomics: Provide READ_ONCE_NOCHECK()

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().

This patch creates __read_once_size_nocheck() a clone of __read_once_size_check() (renamed __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().

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

So I add READ_ONCE_NOCHECK() for accesses for which the compiler cannot prove safe address for KASAN's benefit, but READ_ONCE() suffices for the data-race-detection logic in KTSAN, correct?

KTSAN also needs READ_ONCE_NOCHECK() here.

Does it? What's the difference between READ_ONCE_NOCHECK() and READ_ONCE() with KTSAN=y? AFAIK READ_ONCE() is sufficient to hide race from KTSAN. It doesn't *require* READ_ONCE_NOCHECK(), right?

Quoted 8 lines

For not there is no difference, because you just added READ_ONCE_NOCHECK and we have not yet supported it.
But my plan is to completely ignore accessed from READ_ONCE_NOCHECK in KTSAN so that they never lead to race reports.

READ_ONCE in get_wchan still can lead to a data race report, because it is READ_ONCE in get_wchan versus a normal write to stack in the other thread. That is not atomic and not generally safe.

Where possible, it would be better to make the normal write instead be WRITE_ONCE(). That might well not be possible here, but let's not be too aggressive about silencing KTSAN.

Thanx, Paul

#37 lore raw

linux-next: build problems (Was: [PATCH v3 1/2] Provide READ_ONCE_NOCHECK())

In reply to Andrey Ryabinin

Hi Andrey,

Quoted 15 lines · On Tue, 13 Oct 2015 18:28:07 +0300 Andrey Ryabinin <[email protected]>…

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().

This patch creates __read_once_size_nocheck() a clone of __read_once_size_check() (renamed __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

Quoted 7 lines

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

---
 include/linux/compiler-gcc.h | 13 ++++++++++
 include/linux/compiler.h     | 60 ++++++++++++++++++++++++++++++++++----------
 2 files changed, 60 insertions(+), 13 deletions(-)

I am pretty sure that this patch is causing quite a bit of compile breakage in linux-next today. During the day I compile with gcc 4.9.0 and did not see any problems with c86_64 allmodconfig, or i386 defconfig etc, but overnight we compile with older compilers (gcc 4.6.3 in particular) and are getting quite a few errors:

Quoted 1 line

From an i386 allnoconfig build:

arch/x86/entry/vdso/vdso32.so.dbg: undefined symbols found /home/kisskb/slave/src/arch/x86/entry/vdso/Makefile:154: recipe for target 'arch/x86/entry/vdso/vdso32.so.dbg' failed

Quoted 1 line

From an x86_64 allnoconfig build:

arch/x86/entry/vdso/vclock_gettime.o: In function `__read_once_size_check':
vclock_gettime.c:(.text+0x5f): undefined reference to memcpy' arch/x86/entry/vdso/vgetcpu.o: In function __read_once_size_check':
vgetcpu.c:(.text+0x2f): undefined reference to `memcpy'

and several others ...

Signature · 2 lines
Cheers,
Stephen Rothwell                    [email protected]
#38 lore raw

Re: linux-next: build problems (Was: [PATCH v3 1/2] Provide READ_ONCE_NOCHECK())

Quoted 26 lines · Stephen Rothwell

Hi Andrey,

Quoted 15 lines · On Tue, 13 Oct 2015 18:28:07 +0300 Andrey Ryabinin <[email protected]>…

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().

This patch creates __read_once_size_nocheck() a clone of __read_once_size_check() (renamed __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

Quoted 7 lines

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

---
 include/linux/compiler-gcc.h | 13 ++++++++++
 include/linux/compiler.h     | 60 ++++++++++++++++++++++++++++++++++----------
 2 files changed, 60 insertions(+), 13 deletions(-)
Quoted 6 lines

I am pretty sure that this patch is causing quite a bit of compile breakage in linux-next today. During the day I compile with gcc 4.9.0 and did not see any problems with c86_64 allmodconfig, or i386 defconfig etc, but overnight we compile with older compilers (gcc 4.6.3 in particular) and are getting quite a few errors:

Looks like that older GCC doesn't like __alias (or combination of static __always_inline __alias). It creates outline and unused copy of __read_once_size_check() function in the object file. Should be easy to work around this.

Quoted 14 lines

From an i386 allnoconfig build:

arch/x86/entry/vdso/vdso32.so.dbg: undefined symbols found /home/kisskb/slave/src/arch/x86/entry/vdso/Makefile:154: recipe for target 'arch/x86/entry/vdso/vdso32.so.dbg' failed

From an x86_64 allnoconfig build:

arch/x86/entry/vdso/vclock_gettime.o: In function `__read_once_size_check':
vclock_gettime.c:(.text+0x5f): undefined reference to memcpy' arch/x86/entry/vdso/vgetcpu.o: In function __read_once_size_check':
vgetcpu.c:(.text+0x2f): undefined reference to `memcpy'

and several others ...

#39 lore raw

PATCH 1/1 compiler, READ_ONCE: Fix build failure with some older GCC

In reply to Stephen Rothwell

Some old versions of GCC doesn't handle __alias (or maybe a combination of 'static __always_inline __alias') right.

GCC creates outline and unused copy of __read_once_size_check() function in the object file which references memcpy and causes the build failure:

	arch/x86/entry/vdso/vclock_gettime.o: In function `__read_once_size_check':
	vclock_gettime.c:(.text+0x5f): undefined reference to `memcpy'
	arch/x86/entry/vdso/vgetcpu.o: In function `__read_once_size_check':
	vgetcpu.c:(.text+0x2f): undefined reference to `memcpy'

We could avoid using alias to work around this problem.

---
 include/linux/compiler.h | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
include/linux/compiler.h +5 -2
index aa2ae4c..3436a4c 100644
@@ -231,8 +231,11 @@ void __read_once_size_nocheck(const volatile void *p, void *res, int size)
 	__READ_ONCE_SIZE;
 }
 #else
-static __always_inline __alias(__read_once_size_check)
-void __read_once_size_nocheck(const volatile void *p, void *res, int size);
+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)
Signature · 1 line
2.4.9
#40 lore raw

Re: [PATCH] compiler, READ_ONCE: Fix build failure with some older GCC

Quoted 25 lines · Andrey Ryabinin

Some old versions of GCC doesn't handle __alias (or maybe a combination of 'static __always_inline __alias') right.

GCC creates outline and unused copy of __read_once_size_check() function in the object file which references memcpy and causes the build failure:

	arch/x86/entry/vdso/vclock_gettime.o: In function `__read_once_size_check':
	vclock_gettime.c:(.text+0x5f): undefined reference to `memcpy'
	arch/x86/entry/vdso/vgetcpu.o: In function `__read_once_size_check':
	vgetcpu.c:(.text+0x2f): undefined reference to `memcpy'

We could avoid using alias to work around this problem.

---
 include/linux/compiler.h | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
include/linux/compiler.h +0 -0
index aa2ae4c..3436a4c 100644
@@ -231,8 +231,11 @@ void __read_once_size_nocheck(const volatile void *p, void *res, int size)
 	__READ_ONCE_SIZE;

include/linux/compiler.h void __read_once_size_nocheck(const volatile void *p, void *res, int size) · line 239

Quoted 10 lines
include/linux/compiler.h +5 -2
 }
 #else
-static __always_inline __alias(__read_once_size_check)
-void __read_once_size_nocheck(const volatile void *p, void *res, int size);
+static __always_inline
+void __read_once_size_nocheck(const volatile void *p, void *res, int size)
+{
+	__READ_ONCE_SIZE;
+}
 #endif

Yeah, so could you please re-send the original two patches, with all the changes (typo fixes, renaming, this build fix, etc.) propagated that were found in testing and that were suggested in the review thread?

I'll remove the broken commits from linux-next meanwhile.

Thanks,

Ingo

#41 lore raw

PATCH v3 2/2 x86/process: Silence KASAN warnings in get_wchan()

In reply to Andrey Ryabinin

get_wchan() is racy by design, it may access volatile stack of running task, thus it may access redzone in a stack frame and cause KASAN to warn about this.

Use READ_ONCE_NOCHECK() to silence these warnings.

---
 arch/x86/kernel/process.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/process.c +3 -3
index 39e585a..e28db18 100644
@@ -550,14 +550,14 @@ unsigned long get_wchan(struct task_struct *p)
 	if (sp < bottom || sp > top)
 		return 0;
 
-	fp = READ_ONCE(*(unsigned long *)sp);
+	fp = READ_ONCE_NOCHECK(*(unsigned long *)sp);
 	do {
 		if (fp < bottom || fp > top)
 			return 0;
-		ip = READ_ONCE(*(unsigned long *)(fp + sizeof(unsigned long)));
+		ip = READ_ONCE_NOCHECK(*(unsigned long *)(fp + sizeof(unsigned long)));
 		if (!in_sched_functions(ip))
 			return ip;
-		fp = READ_ONCE(*(unsigned long *)fp);
+		fp = READ_ONCE_NOCHECK(*(unsigned long *)fp);
 	} while (count++ < 16 && p->state != TASK_RUNNING);
 	return 0;
 }
Signature · 1 line
2.4.9
#42 lore raw

[tip:locking/urgent] x86/mm: Silence KASAN warnings in get_wchan( )

x86/mm: Silence KASAN warnings in get_wchan()
Commit-ID:  1e6f85cb6771d54c9346270d7b0667500d9f3eab
Gitweb:     http://git.kernel.org/tip/1e6f85cb6771d54c9346270d7b0667500d9f3eab
Author:     Andrey Ryabinin <[email protected]>
AuthorDate: Tue, 13 Oct 2015 18:28:08 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 14 Oct 2015 16:44:06 +0200

x86/mm: Silence KASAN warnings in get_wchan()

get_wchan() is racy by design, it may access volatile stack of running task, thus it may access redzone in a stack frame and cause KASAN to warn about this.

Use READ_ONCE_NOCHECK() to silence these warnings.

Cc Alexander Potapenko <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Kostya Serebryany <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Wolfram Gloger <[email protected]>
Cc kasan-dev <[email protected]>
---
 arch/x86/kernel/process.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/process.c +3 -3
index 39e585a..e28db18 100644
@@ -550,14 +550,14 @@ unsigned long get_wchan(struct task_struct *p)
 	if (sp < bottom || sp > top)
 		return 0;
 
-	fp = READ_ONCE(*(unsigned long *)sp);
+	fp = READ_ONCE_NOCHECK(*(unsigned long *)sp);
 	do {
 		if (fp < bottom || fp > top)
 			return 0;
-		ip = READ_ONCE(*(unsigned long *)(fp + sizeof(unsigned long)));
+		ip = READ_ONCE_NOCHECK(*(unsigned long *)(fp + sizeof(unsigned long)));
 		if (!in_sched_functions(ip))
 			return ip;
-		fp = READ_ONCE(*(unsigned long *)fp);
+		fp = READ_ONCE_NOCHECK(*(unsigned long *)fp);
 	} while (count++ < 16 && p->state != TASK_RUNNING);
 	return 0;
 }
#43 lore raw

v4 cover [PATCH v4 0/2]

In reply to Andrey Ryabinin

Changes since v3:
- Fixed build failure.
- Rename: s/READ_ONCE_NOCHECK/READ_ONCE_NOKSAN
- Spelling fix.

Changes since v2:
- Added some code comments in the first patch.

Andrey Ryabinin (2):
compiler, atomics: Provide READ_ONCE_NOKSAN()
x86/mm: Silence KASAN warnings in get_wchan()

 arch/x86/kernel/process.c    |  6 ++--
 include/linux/compiler-gcc.h | 13 +++++++++
 include/linux/compiler.h     | 66 +++++++++++++++++++++++++++++++++++---------
 3 files changed, 69 insertions(+), 16 deletions(-)
Signature · 1 line
2.4.9
#44 lore raw

PATCH v4 1/2 compiler, atomics: Provide READ_ONCE_NOKSAN()

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_NOKSAN() macro. KASAN will not check the memory accessed by READ_ONCE_NOKSAN(). The KernelThreadSanitizer (KTSAN) is going to ignore it as well.

This patch creates __read_once_size_noksan() a clone of __read_once_size(). The only difference between them is 'no_sanitized_address' attribute appended to '*_nokasan' 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_NOKSAN() is just a clone of READ_ONCE().

---
 include/linux/compiler-gcc.h | 13 +++++++++
 include/linux/compiler.h     | 66 +++++++++++++++++++++++++++++++++++---------
 2 files changed, 66 insertions(+), 13 deletions(-)
include/linux/compiler-gcc.h +13 -0
index dfaa7b3..8efb40e 100644
@@ -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
include/linux/compiler.h +53 -13

80 lines not on this page · raw message

Signature · 1 line
2.4.9
#45 lore raw

Re: [PATCH v4 1/2] compiler, atomics: Provide READ_ONCE_NOKSAN()

Quoted 16 lines · Andrey Ryabinin

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_NOKSAN() macro. KASAN will not check the memory accessed by READ_ONCE_NOKSAN(). The KernelThreadSanitizer (KTSAN) is going to ignore it as well.

This patch creates __read_once_size_noksan() a clone of __read_once_size(). The only difference between them is 'no_sanitized_address' attribute appended to '*_nokasan' 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

Quoted 1 line

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

Would we need a similar annotation for things like mutex_spin_on_owner()'s dereference of owner, or is that considered safe by KASAN?

(its not actually safe; as I remember we have a problem with using rcu_read_lock for tasks like that)

#46 lore raw

Re: [PATCH v4 1/2] compiler, atomics: Provide READ_ONCE_NOKSAN()

Quoted 17 lines · Peter Zijlstra
Quoted 16 lines · Andrey Ryabinin

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_NOKSAN() macro. KASAN will not check the memory accessed by READ_ONCE_NOKSAN(). The KernelThreadSanitizer (KTSAN) is going to ignore it as well.

This patch creates __read_once_size_noksan() a clone of __read_once_size(). The only difference between them is 'no_sanitized_address' attribute appended to '*_nokasan' 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

Quoted 9 lines
Quoted 1 line

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

Would we need a similar annotation for things like mutex_spin_on_owner()'s dereference of owner, or is that considered safe by KASAN?

(its not actually safe; as I remember we have a problem with using rcu_read_lock for tasks like that)

How exactly it's not safe? If we could dereference freed owner, I'd say we need to fix this, but not hide.
I've seen use-after-free in mutex_spin_on_owner() once, but it was caused
by GPF in kernel which killed some task while it was holding mutex. So the next time we tried to grab that mutex, lock->owner was already dead.
But normally we should release all locks before we able to kill task, so this won't happen.

#47 lore raw

Re: [PATCH v4 1/2] compiler, atomics: Provide READ_ONCE_NOKSAN()

Quoted 24 lines · Andrey Ryabinin
Quoted 23 lines · Peter Zijlstra
Quoted 16 lines · Andrey Ryabinin

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_NOKSAN() macro. KASAN will not check the memory accessed by READ_ONCE_NOKSAN(). The KernelThreadSanitizer (KTSAN) is going to ignore it as well.

This patch creates __read_once_size_noksan() a clone of __read_once_size(). The only difference between them is 'no_sanitized_address' attribute appended to '*_nokasan' 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

Quoted 1 line

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

Would we need a similar annotation for things like mutex_spin_on_owner()'s dereference of owner, or is that considered safe by KASAN?

Quoted 5 lines
Quoted 3 lines

(its not actually safe; as I remember we have a problem with using rcu_read_lock for tasks like that)

How exactly it's not safe?

I was worried perhaps KASAN would trip over the speculative nature of the owner pointer, but we do verify it, so I suppose its allright.

Quoted 2 lines

If we could dereference freed owner, I'd say we need to fix this, but not hide.

We should, just not 'trivial' and we seem to get distracted while thinking of possible fixes :/

maintainer
#48 lore raw

Re: [PATCH v4 1/2] compiler, atomics: Provide READ_ONCE_NOKSAN()

In reply to Andrey Ryabinin
Quoted 7 lines · Andrey Ryabinin

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_NOKSAN() macro. KASAN will not check the memory accessed by READ_ONCE_NOKSAN(). The KernelThreadSanitizer (KTSAN) is going to ignore it as well.

Frankly, the "NOKSAN" suffix is too specific. I know, I know, I'm bikeshedding but what happens if yet another tool wants to be disabled from checking there and that tool is not *SAN? We rename again?

So the "NOCHECK" suffix made much more sense, even if it was generic. IMNSVHO.

Thanks.

Signature · 4 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
#49 lore raw

Re: [PATCH v4 1/2] compiler, atomics: Provide READ_ONCE_NOKSAN()

Quoted 9 lines · Borislav Petkov
Quoted 7 lines · Andrey Ryabinin

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_NOKSAN() macro. KASAN will not check the memory accessed by READ_ONCE_NOKSAN(). The KernelThreadSanitizer (KTSAN) is going to ignore it as well.

Quoted 7 lines

Frankly, the "NOKSAN" suffix is too specific. I know, I know, I'm bikeshedding but what happens if yet another tool wants to be disabled from checking there and that tool is not *SAN? We rename again?

So the "NOCHECK" suffix made much more sense, even if it was generic. IMNSVHO.

Sounds reasonable.
Ingo, what do you think?

Quoted 2 lines

Thanks.

#50 lore raw

Re: [PATCH v4 1/2] compiler, atomics: Provide READ_ONCE_NOKSAN()

Quoted 14 lines · Andrey Ryabinin
Quoted 13 lines · Borislav Petkov
Quoted 7 lines · Andrey Ryabinin

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_NOKSAN() macro. KASAN will not check the memory accessed by READ_ONCE_NOKSAN(). The KernelThreadSanitizer (KTSAN) is going to ignore it as well.

Frankly, the "NOKSAN" suffix is too specific. I know, I know, I'm bikeshedding but what happens if yet another tool wants to be disabled from checking there and that tool is not *SAN? We rename again?

Quoted 6 lines
Quoted 3 lines

So the "NOCHECK" suffix made much more sense, even if it was generic. IMNSVHO.

Sounds reasonable.
Ingo, what do you think?

Fine with me too.

Thanks,

Ingo

#51 lore raw

Re: [PATCH v4 1/2] compiler, atomics: Provide READ_ONCE_NOKSAN()

In reply to Borislav Petkov
Quoted 4 lines · Borislav Petkov
Quoted 3 lines · Andrey Ryabinin

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

Quoted 8 lines
Quoted 4 lines

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

Frankly, the "NOKSAN" suffix is too specific. I know, I know, I'm bikeshedding but what happens if yet another tool wants to be disabled from checking there and that tool is not *SAN? We rename again?

Plaid, I say!!! The color of the bikeshed must be plaid!!! ;-)

Thanx, Paul

Quoted 11 lines

So the "NOCHECK" suffix made much more sense, even if it was generic. IMNSVHO.

Thanks.

Signature · 4 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
#52 lore raw

PATCH v4 2/2 x86/mm: Silence KASAN warnings in get_wchan()

In reply to Andrey Ryabinin

get_wchan() is racy by design, it may access volatile stack of running task, thus it may access redzone in a stack frame and cause KASAN to warn about this.

Use READ_ONCE_NOKSAN() to silence these warnings.

---
 arch/x86/kernel/process.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/process.c +3 -3
index 39e585a..971cf5f 100644
@@ -550,14 +550,14 @@ unsigned long get_wchan(struct task_struct *p)
 	if (sp < bottom || sp > top)
 		return 0;
 
-	fp = READ_ONCE(*(unsigned long *)sp);
+	fp = READ_ONCE_NOKSAN(*(unsigned long *)sp);
 	do {
 		if (fp < bottom || fp > top)
 			return 0;
-		ip = READ_ONCE(*(unsigned long *)(fp + sizeof(unsigned long)));
+		ip = READ_ONCE_NOKSAN(*(unsigned long *)(fp + sizeof(unsigned long)));
 		if (!in_sched_functions(ip))
 			return ip;
-		fp = READ_ONCE(*(unsigned long *)fp);
+		fp = READ_ONCE_NOKSAN(*(unsigned long *)fp);
 	} while (count++ < 16 && p->state != TASK_RUNNING);
 	return 0;
 }
Signature · 1 line
2.4.9
#53 lore raw

v4 cover Silence KASAN warnings in get_wchan()

In reply to Andrey Ryabinin
Quoted 17 lines · Andrey Ryabinin

Changes since v3:
- Fixed build failure.
- Rename: s/READ_ONCE_NOCHECK/READ_ONCE_NOKSAN
- Spelling fix.

Changes since v2:
- Added some code comments in the first patch.

Andrey Ryabinin (2):
compiler, atomics: Provide READ_ONCE_NOKSAN()
x86/mm: Silence KASAN warnings in get_wchan()

 arch/x86/kernel/process.c    |  6 ++--
 include/linux/compiler-gcc.h | 13 +++++++++
 include/linux/compiler.h     | 66 +++++++++++++++++++++++++++++++++++---------
 3 files changed, 69 insertions(+), 16 deletions(-)
#54 lore raw

v5 cover Silence KASAN warnings in get_wchan()

In reply to Andrey Ryabinin

Changes since v4:
- Rename back to READ_ONCE_NOCHECK().

Changes since v3:
- Fixed build failure.
- Rename: s/READ_ONCE_NOCHECK/READ_ONCE_NOKSAN
- Spelling fix.

Changes since v2:
- Added some code comments in the first patch.

Andrey Ryabinin (2):
compiler, atomics: Provide READ_ONCE_NOCHECK()
x86/mm: Silence KASAN warnings in get_wchan()

 arch/x86/kernel/process.c    |  6 ++--
 include/linux/compiler-gcc.h | 13 +++++++++
 include/linux/compiler.h     | 66 +++++++++++++++++++++++++++++++++++---------
 3 files changed, 69 insertions(+), 16 deletions(-)
Signature · 1 line
2.4.9
#55 lore raw

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

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().

---
 include/linux/compiler-gcc.h | 13 +++++++++
 include/linux/compiler.h     | 66 +++++++++++++++++++++++++++++++++++---------
 2 files changed, 66 insertions(+), 13 deletions(-)
include/linux/compiler-gcc.h +13 -0
index dfaa7b3..8efb40e 100644
@@ -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
include/linux/compiler.h +53 -13

80 lines not on this page · raw message

Signature · 1 line
2.4.9
#56 lore raw

[tip:x86/urgent] compiler, atomics, kasan: Provide READ_ONCE_NOCHECK()

compiler, atomics, kasan: Provide READ_ONCE_NOCHECK()
Commit-ID:  d976441f44bc5d48635d081d277aa76556ffbf8b
Gitweb:     http://git.kernel.org/tip/d976441f44bc5d48635d081d277aa76556ffbf8b
Author:     Andrey Ryabinin <[email protected]>
AuthorDate: Mon, 19 Oct 2015 11:37:17 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 20 Oct 2015 11:04:19 +0200

compiler, atomics, kasan: Provide READ_ONCE_NOCHECK()

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().

Cc Alexander Potapenko <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Kostya Serebryany <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Wolfram Gloger <[email protected]>
Cc kasan-dev <[email protected]>
---
 include/linux/compiler-gcc.h | 13 +++++++++
 include/linux/compiler.h     | 66 +++++++++++++++++++++++++++++++++++---------
 2 files changed, 66 insertions(+), 13 deletions(-)
include/linux/compiler-gcc.h +13 -0
index dfaa7b3..8efb40e 100644
@@ -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
include/linux/compiler.h +53 -13

80 lines not on this page · raw message

#57 lore raw

PATCH v5 2/2 x86/mm: Silence KASAN warnings in get_wchan()

In reply to Andrey Ryabinin

get_wchan() is racy by design, it may access volatile stack of running task, thus it may access redzone in a stack frame and cause KASAN to warn about this.

Use READ_ONCE_NOCHECK() to silence these warnings.

---
 arch/x86/kernel/process.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/process.c +3 -3
index 39e585a..e28db18 100644
@@ -550,14 +550,14 @@ unsigned long get_wchan(struct task_struct *p)
 	if (sp < bottom || sp > top)
 		return 0;
 
-	fp = READ_ONCE(*(unsigned long *)sp);
+	fp = READ_ONCE_NOCHECK(*(unsigned long *)sp);
 	do {
 		if (fp < bottom || fp > top)
 			return 0;
-		ip = READ_ONCE(*(unsigned long *)(fp + sizeof(unsigned long)));
+		ip = READ_ONCE_NOCHECK(*(unsigned long *)(fp + sizeof(unsigned long)));
 		if (!in_sched_functions(ip))
 			return ip;
-		fp = READ_ONCE(*(unsigned long *)fp);
+		fp = READ_ONCE_NOCHECK(*(unsigned long *)fp);
 	} while (count++ < 16 && p->state != TASK_RUNNING);
 	return 0;
 }
Signature · 1 line
2.4.9
#58 lore raw

[tip:x86/urgent] x86/mm, kasan: Silence KASAN warnings in get_wchan()

x86/mm, kasan: Silence KASAN warnings in get_wchan()
Commit-ID:  f7d27c35ddff7c100d7a98db499ac0040149ac05
Gitweb:     http://git.kernel.org/tip/f7d27c35ddff7c100d7a98db499ac0040149ac05
Author:     Andrey Ryabinin <[email protected]>
AuthorDate: Mon, 19 Oct 2015 11:37:18 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 20 Oct 2015 11:04:19 +0200

x86/mm, kasan: Silence KASAN warnings in get_wchan()

get_wchan() is racy by design, it may access volatile stack of running task, thus it may access redzone in a stack frame and cause KASAN to warn about this.

Use READ_ONCE_NOCHECK() to silence these warnings.

Cc Alexander Potapenko <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Kostya Serebryany <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Wolfram Gloger <[email protected]>
Cc kasan-dev <[email protected]>
---
 arch/x86/kernel/process.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/process.c +3 -3
index 39e585a..e28db18 100644
@@ -550,14 +550,14 @@ unsigned long get_wchan(struct task_struct *p)
 	if (sp < bottom || sp > top)
 		return 0;
 
-	fp = READ_ONCE(*(unsigned long *)sp);
+	fp = READ_ONCE_NOCHECK(*(unsigned long *)sp);
 	do {
 		if (fp < bottom || fp > top)
 			return 0;
-		ip = READ_ONCE(*(unsigned long *)(fp + sizeof(unsigned long)));
+		ip = READ_ONCE_NOCHECK(*(unsigned long *)(fp + sizeof(unsigned long)));
 		if (!in_sched_functions(ip))
 			return ip;
-		fp = READ_ONCE(*(unsigned long *)fp);
+		fp = READ_ONCE_NOCHECK(*(unsigned long *)fp);
 	} while (count++ < 16 && p->state != TASK_RUNNING);
 	return 0;
 }