Skip to content

Commit d976441

Browse files
aryabininIngo Molnar
authored andcommitted
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(). Signed-off-by: Andrey Ryabinin <[email protected]> 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]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent a75ca54 commit d976441

File tree

2 files changed

+66
-13
lines changed

2 files changed

+66
-13
lines changed

include/linux/compiler-gcc.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,25 @@
237237
#define KASAN_ABI_VERSION 3
238238
#endif
239239

240+
#if GCC_VERSION >= 40902
241+
/*
242+
* Tell the compiler that address safety instrumentation (KASAN)
243+
* should not be applied to that function.
244+
* Conflicts with inlining: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
245+
*/
246+
#define __no_sanitize_address __attribute__((no_sanitize_address))
247+
#endif
248+
240249
#endif /* gcc version >= 40000 specific checks */
241250

242251
#if !defined(__noclone)
243252
#define __noclone /* not needed */
244253
#endif
245254

255+
#if !defined(__no_sanitize_address)
256+
#define __no_sanitize_address
257+
#endif
258+
246259
/*
247260
* A trick to suppress uninitialized variable warning without generating any
248261
* code

include/linux/compiler.h

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,45 @@ void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
198198

199199
#include <uapi/linux/types.h>
200200

201-
static __always_inline void __read_once_size(const volatile void *p, void *res, int size)
201+
#define __READ_ONCE_SIZE \
202+
({ \
203+
switch (size) { \
204+
case 1: *(__u8 *)res = *(volatile __u8 *)p; break; \
205+
case 2: *(__u16 *)res = *(volatile __u16 *)p; break; \
206+
case 4: *(__u32 *)res = *(volatile __u32 *)p; break; \
207+
case 8: *(__u64 *)res = *(volatile __u64 *)p; break; \
208+
default: \
209+
barrier(); \
210+
__builtin_memcpy((void *)res, (const void *)p, size); \
211+
barrier(); \
212+
} \
213+
})
214+
215+
static __always_inline
216+
void __read_once_size(const volatile void *p, void *res, int size)
202217
{
203-
switch (size) {
204-
case 1: *(__u8 *)res = *(volatile __u8 *)p; break;
205-
case 2: *(__u16 *)res = *(volatile __u16 *)p; break;
206-
case 4: *(__u32 *)res = *(volatile __u32 *)p; break;
207-
case 8: *(__u64 *)res = *(volatile __u64 *)p; break;
208-
default:
209-
barrier();
210-
__builtin_memcpy((void *)res, (const void *)p, size);
211-
barrier();
212-
}
218+
__READ_ONCE_SIZE;
219+
}
220+
221+
#ifdef CONFIG_KASAN
222+
/*
223+
* This function is not 'inline' because __no_sanitize_address confilcts
224+
* with inlining. Attempt to inline it may cause a build failure.
225+
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
226+
* '__maybe_unused' allows us to avoid defined-but-not-used warnings.
227+
*/
228+
static __no_sanitize_address __maybe_unused
229+
void __read_once_size_nocheck(const volatile void *p, void *res, int size)
230+
{
231+
__READ_ONCE_SIZE;
232+
}
233+
#else
234+
static __always_inline
235+
void __read_once_size_nocheck(const volatile void *p, void *res, int size)
236+
{
237+
__READ_ONCE_SIZE;
213238
}
239+
#endif
214240

215241
static __always_inline void __write_once_size(volatile void *p, void *res, int size)
216242
{
@@ -248,8 +274,22 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s
248274
* required ordering.
249275
*/
250276

251-
#define READ_ONCE(x) \
252-
({ union { typeof(x) __val; char __c[1]; } __u; __read_once_size(&(x), __u.__c, sizeof(x)); __u.__val; })
277+
#define __READ_ONCE(x, check) \
278+
({ \
279+
union { typeof(x) __val; char __c[1]; } __u; \
280+
if (check) \
281+
__read_once_size(&(x), __u.__c, sizeof(x)); \
282+
else \
283+
__read_once_size_nocheck(&(x), __u.__c, sizeof(x)); \
284+
__u.__val; \
285+
})
286+
#define READ_ONCE(x) __READ_ONCE(x, 1)
287+
288+
/*
289+
* Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need
290+
* to hide memory access from KASAN.
291+
*/
292+
#define READ_ONCE_NOCHECK(x) __READ_ONCE(x, 0)
253293

254294
#define WRITE_ONCE(x, val) \
255295
({ \

0 commit comments

Comments
 (0)