Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Prevent the compiler from merging or refetching reads or writes. The
0004  * compiler is also forbidden from reordering successive instances of
0005  * READ_ONCE and WRITE_ONCE, but only when the compiler is aware of some
0006  * particular ordering. One way to make the compiler aware of ordering is to
0007  * put the two invocations of READ_ONCE or WRITE_ONCE in different C
0008  * statements.
0009  *
0010  * These two macros will also work on aggregate data types like structs or
0011  * unions.
0012  *
0013  * Their two major use cases are: (1) Mediating communication between
0014  * process-level code and irq/NMI handlers, all running on the same CPU,
0015  * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
0016  * mutilate accesses that either do not require ordering or that interact
0017  * with an explicit memory barrier or atomic instruction that provides the
0018  * required ordering.
0019  */
0020 #ifndef __ASM_GENERIC_RWONCE_H
0021 #define __ASM_GENERIC_RWONCE_H
0022 
0023 #ifndef __ASSEMBLY__
0024 
0025 #include <linux/compiler_types.h>
0026 #include <linux/kasan-checks.h>
0027 #include <linux/kcsan-checks.h>
0028 
0029 /*
0030  * Yes, this permits 64-bit accesses on 32-bit architectures. These will
0031  * actually be atomic in some cases (namely Armv7 + LPAE), but for others we
0032  * rely on the access being split into 2x32-bit accesses for a 32-bit quantity
0033  * (e.g. a virtual address) and a strong prevailing wind.
0034  */
0035 #define compiletime_assert_rwonce_type(t)                   \
0036     compiletime_assert(__native_word(t) || sizeof(t) == sizeof(long long),  \
0037         "Unsupported access size for {READ,WRITE}_ONCE().")
0038 
0039 /*
0040  * Use __READ_ONCE() instead of READ_ONCE() if you do not require any
0041  * atomicity. Note that this may result in tears!
0042  */
0043 #ifndef __READ_ONCE
0044 #define __READ_ONCE(x)  (*(const volatile __unqual_scalar_typeof(x) *)&(x))
0045 #endif
0046 
0047 #define READ_ONCE(x)                            \
0048 ({                                  \
0049     compiletime_assert_rwonce_type(x);              \
0050     __READ_ONCE(x);                         \
0051 })
0052 
0053 #define __WRITE_ONCE(x, val)                        \
0054 do {                                    \
0055     *(volatile typeof(x) *)&(x) = (val);                \
0056 } while (0)
0057 
0058 #define WRITE_ONCE(x, val)                      \
0059 do {                                    \
0060     compiletime_assert_rwonce_type(x);              \
0061     __WRITE_ONCE(x, val);                       \
0062 } while (0)
0063 
0064 static __no_sanitize_or_inline
0065 unsigned long __read_once_word_nocheck(const void *addr)
0066 {
0067     return __READ_ONCE(*(unsigned long *)addr);
0068 }
0069 
0070 /*
0071  * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need to load a
0072  * word from memory atomically but without telling KASAN/KCSAN. This is
0073  * usually used by unwinding code when walking the stack of a running process.
0074  */
0075 #define READ_ONCE_NOCHECK(x)                        \
0076 ({                                  \
0077     compiletime_assert(sizeof(x) == sizeof(unsigned long),      \
0078         "Unsupported access size for READ_ONCE_NOCHECK().");    \
0079     (typeof(x))__read_once_word_nocheck(&(x));          \
0080 })
0081 
0082 static __no_kasan_or_inline
0083 unsigned long read_word_at_a_time(const void *addr)
0084 {
0085     kasan_check_read(addr, 1);
0086     return *(unsigned long *)addr;
0087 }
0088 
0089 #endif /* __ASSEMBLY__ */
0090 #endif  /* __ASM_GENERIC_RWONCE_H */