162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Prevent the compiler from merging or refetching reads or writes. The
462306a36Sopenharmony_ci * compiler is also forbidden from reordering successive instances of
562306a36Sopenharmony_ci * READ_ONCE and WRITE_ONCE, but only when the compiler is aware of some
662306a36Sopenharmony_ci * particular ordering. One way to make the compiler aware of ordering is to
762306a36Sopenharmony_ci * put the two invocations of READ_ONCE or WRITE_ONCE in different C
862306a36Sopenharmony_ci * statements.
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci * These two macros will also work on aggregate data types like structs or
1162306a36Sopenharmony_ci * unions.
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * Their two major use cases are: (1) Mediating communication between
1462306a36Sopenharmony_ci * process-level code and irq/NMI handlers, all running on the same CPU,
1562306a36Sopenharmony_ci * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
1662306a36Sopenharmony_ci * mutilate accesses that either do not require ordering or that interact
1762306a36Sopenharmony_ci * with an explicit memory barrier or atomic instruction that provides the
1862306a36Sopenharmony_ci * required ordering.
1962306a36Sopenharmony_ci */
2062306a36Sopenharmony_ci#ifndef __ASM_GENERIC_RWONCE_H
2162306a36Sopenharmony_ci#define __ASM_GENERIC_RWONCE_H
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#ifndef __ASSEMBLY__
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#include <linux/compiler_types.h>
2662306a36Sopenharmony_ci#include <linux/kasan-checks.h>
2762306a36Sopenharmony_ci#include <linux/kcsan-checks.h>
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci/*
3062306a36Sopenharmony_ci * Yes, this permits 64-bit accesses on 32-bit architectures. These will
3162306a36Sopenharmony_ci * actually be atomic in some cases (namely Armv7 + LPAE), but for others we
3262306a36Sopenharmony_ci * rely on the access being split into 2x32-bit accesses for a 32-bit quantity
3362306a36Sopenharmony_ci * (e.g. a virtual address) and a strong prevailing wind.
3462306a36Sopenharmony_ci */
3562306a36Sopenharmony_ci#define compiletime_assert_rwonce_type(t)					\
3662306a36Sopenharmony_ci	compiletime_assert(__native_word(t) || sizeof(t) == sizeof(long long),	\
3762306a36Sopenharmony_ci		"Unsupported access size for {READ,WRITE}_ONCE().")
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci/*
4062306a36Sopenharmony_ci * Use __READ_ONCE() instead of READ_ONCE() if you do not require any
4162306a36Sopenharmony_ci * atomicity. Note that this may result in tears!
4262306a36Sopenharmony_ci */
4362306a36Sopenharmony_ci#ifndef __READ_ONCE
4462306a36Sopenharmony_ci#define __READ_ONCE(x)	(*(const volatile __unqual_scalar_typeof(x) *)&(x))
4562306a36Sopenharmony_ci#endif
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci#define READ_ONCE(x)							\
4862306a36Sopenharmony_ci({									\
4962306a36Sopenharmony_ci	compiletime_assert_rwonce_type(x);				\
5062306a36Sopenharmony_ci	__READ_ONCE(x);							\
5162306a36Sopenharmony_ci})
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci#define __WRITE_ONCE(x, val)						\
5462306a36Sopenharmony_cido {									\
5562306a36Sopenharmony_ci	*(volatile typeof(x) *)&(x) = (val);				\
5662306a36Sopenharmony_ci} while (0)
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci#define WRITE_ONCE(x, val)						\
5962306a36Sopenharmony_cido {									\
6062306a36Sopenharmony_ci	compiletime_assert_rwonce_type(x);				\
6162306a36Sopenharmony_ci	__WRITE_ONCE(x, val);						\
6262306a36Sopenharmony_ci} while (0)
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_cistatic __no_sanitize_or_inline
6562306a36Sopenharmony_ciunsigned long __read_once_word_nocheck(const void *addr)
6662306a36Sopenharmony_ci{
6762306a36Sopenharmony_ci	return __READ_ONCE(*(unsigned long *)addr);
6862306a36Sopenharmony_ci}
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci/*
7162306a36Sopenharmony_ci * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need to load a
7262306a36Sopenharmony_ci * word from memory atomically but without telling KASAN/KCSAN. This is
7362306a36Sopenharmony_ci * usually used by unwinding code when walking the stack of a running process.
7462306a36Sopenharmony_ci */
7562306a36Sopenharmony_ci#define READ_ONCE_NOCHECK(x)						\
7662306a36Sopenharmony_ci({									\
7762306a36Sopenharmony_ci	compiletime_assert(sizeof(x) == sizeof(unsigned long),		\
7862306a36Sopenharmony_ci		"Unsupported access size for READ_ONCE_NOCHECK().");	\
7962306a36Sopenharmony_ci	(typeof(x))__read_once_word_nocheck(&(x));			\
8062306a36Sopenharmony_ci})
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_cistatic __no_kasan_or_inline
8362306a36Sopenharmony_ciunsigned long read_word_at_a_time(const void *addr)
8462306a36Sopenharmony_ci{
8562306a36Sopenharmony_ci	kasan_check_read(addr, 1);
8662306a36Sopenharmony_ci	return *(unsigned long *)addr;
8762306a36Sopenharmony_ci}
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci#endif /* __ASSEMBLY__ */
9062306a36Sopenharmony_ci#endif	/* __ASM_GENERIC_RWONCE_H */
91