compiler.h: Enforce that READ_ONCE_NOCHECK() access size is sizeof(long)
authorWill Deacon <will@kernel.org>
Fri, 5 Jun 2020 10:19:46 +0000 (11:19 +0100)
committerWill Deacon <will@kernel.org>
Fri, 5 Jun 2020 10:19:46 +0000 (11:19 +0100)
READ_ONCE_NOCHECK() unconditionally performs a sizeof(long)-sized access,
so enforce that the size of the pointed-to object that we are loading
from is the same size as 'long'.

Reported-by: Marco Elver <elver@google.com>
Signed-off-by: Will Deacon <will@kernel.org>
include/linux/compiler.h

index 657e4fd..a0aa56e 100644 (file)
@@ -254,9 +254,12 @@ unsigned long __read_once_word_nocheck(const void *addr)
  */
 #define READ_ONCE_NOCHECK(x)                                           \
 ({                                                                     \
-       unsigned long __x = __read_once_word_nocheck(&(x));             \
+       unsigned long __x;                                              \
+       compiletime_assert(sizeof(x) == sizeof(__x),                    \
+               "Unsupported access size for READ_ONCE_NOCHECK().");    \
+       __x = __read_once_word_nocheck(&(x));                           \
        smp_read_barrier_depends();                                     \
-       __x;                                                            \
+       (typeof(x))__x;                                                 \
 })
 
 static __no_kasan_or_inline