18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Prevent the compiler from merging or refetching reads or writes. The
48c2ecf20Sopenharmony_ci * compiler is also forbidden from reordering successive instances of
58c2ecf20Sopenharmony_ci * READ_ONCE and WRITE_ONCE, but only when the compiler is aware of some
68c2ecf20Sopenharmony_ci * particular ordering. One way to make the compiler aware of ordering is to
78c2ecf20Sopenharmony_ci * put the two invocations of READ_ONCE or WRITE_ONCE in different C
88c2ecf20Sopenharmony_ci * statements.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * These two macros will also work on aggregate data types like structs or
118c2ecf20Sopenharmony_ci * unions.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * Their two major use cases are: (1) Mediating communication between
148c2ecf20Sopenharmony_ci * process-level code and irq/NMI handlers, all running on the same CPU,
158c2ecf20Sopenharmony_ci * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
168c2ecf20Sopenharmony_ci * mutilate accesses that either do not require ordering or that interact
178c2ecf20Sopenharmony_ci * with an explicit memory barrier or atomic instruction that provides the
188c2ecf20Sopenharmony_ci * required ordering.
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_ci#ifndef __ASM_GENERIC_RWONCE_H
218c2ecf20Sopenharmony_ci#define __ASM_GENERIC_RWONCE_H
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include <linux/compiler_types.h>
268c2ecf20Sopenharmony_ci#include <linux/kasan-checks.h>
278c2ecf20Sopenharmony_ci#include <linux/kcsan-checks.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/*
308c2ecf20Sopenharmony_ci * Yes, this permits 64-bit accesses on 32-bit architectures. These will
318c2ecf20Sopenharmony_ci * actually be atomic in some cases (namely Armv7 + LPAE), but for others we
328c2ecf20Sopenharmony_ci * rely on the access being split into 2x32-bit accesses for a 32-bit quantity
338c2ecf20Sopenharmony_ci * (e.g. a virtual address) and a strong prevailing wind.
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_ci#define compiletime_assert_rwonce_type(t)					\
368c2ecf20Sopenharmony_ci	compiletime_assert(__native_word(t) || sizeof(t) == sizeof(long long),	\
378c2ecf20Sopenharmony_ci		"Unsupported access size for {READ,WRITE}_ONCE().")
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/*
408c2ecf20Sopenharmony_ci * Use __READ_ONCE() instead of READ_ONCE() if you do not require any
418c2ecf20Sopenharmony_ci * atomicity. Note that this may result in tears!
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_ci#ifndef __READ_ONCE
448c2ecf20Sopenharmony_ci#define __READ_ONCE(x)	(*(const volatile __unqual_scalar_typeof(x) *)&(x))
458c2ecf20Sopenharmony_ci#endif
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define READ_ONCE(x)							\
488c2ecf20Sopenharmony_ci({									\
498c2ecf20Sopenharmony_ci	compiletime_assert_rwonce_type(x);				\
508c2ecf20Sopenharmony_ci	__READ_ONCE(x);							\
518c2ecf20Sopenharmony_ci})
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define __WRITE_ONCE(x, val)						\
548c2ecf20Sopenharmony_cido {									\
558c2ecf20Sopenharmony_ci	*(volatile typeof(x) *)&(x) = (val);				\
568c2ecf20Sopenharmony_ci} while (0)
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define WRITE_ONCE(x, val)						\
598c2ecf20Sopenharmony_cido {									\
608c2ecf20Sopenharmony_ci	compiletime_assert_rwonce_type(x);				\
618c2ecf20Sopenharmony_ci	__WRITE_ONCE(x, val);						\
628c2ecf20Sopenharmony_ci} while (0)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic __no_sanitize_or_inline
658c2ecf20Sopenharmony_ciunsigned long __read_once_word_nocheck(const void *addr)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	return __READ_ONCE(*(unsigned long *)addr);
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/*
718c2ecf20Sopenharmony_ci * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need to load a
728c2ecf20Sopenharmony_ci * word from memory atomically but without telling KASAN/KCSAN. This is
738c2ecf20Sopenharmony_ci * usually used by unwinding code when walking the stack of a running process.
748c2ecf20Sopenharmony_ci */
758c2ecf20Sopenharmony_ci#define READ_ONCE_NOCHECK(x)						\
768c2ecf20Sopenharmony_ci({									\
778c2ecf20Sopenharmony_ci	compiletime_assert(sizeof(x) == sizeof(unsigned long),		\
788c2ecf20Sopenharmony_ci		"Unsupported access size for READ_ONCE_NOCHECK().");	\
798c2ecf20Sopenharmony_ci	(typeof(x))__read_once_word_nocheck(&(x));			\
808c2ecf20Sopenharmony_ci})
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic __no_kasan_or_inline
838c2ecf20Sopenharmony_ciunsigned long read_word_at_a_time(const void *addr)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	kasan_check_read(addr, 1);
868c2ecf20Sopenharmony_ci	return *(unsigned long *)addr;
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci#endif /* __ASSEMBLY__ */
908c2ecf20Sopenharmony_ci#endif	/* __ASM_GENERIC_RWONCE_H */
91