aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/compiler.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/linux/compiler.h')
-rw-r--r--include/linux/compiler.h76
1 files changed, 75 insertions, 1 deletions
diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index 1ef679f4b88e..17f624cdf53c 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -190,6 +190,80 @@ void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
190# define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__) 190# define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__)
191#endif 191#endif
192 192
193#include <uapi/linux/types.h>
194
195static __always_inline void data_access_exceeds_word_size(void)
196#ifdef __compiletime_warning
197__compiletime_warning("data access exceeds word size and won't be atomic")
198#endif
199;
200
201static __always_inline void data_access_exceeds_word_size(void)
202{
203}
204
205static __always_inline void __read_once_size(volatile void *p, void *res, int size)
206{
207 switch (size) {
208 case 1: *(__u8 *)res = *(volatile __u8 *)p; break;
209 case 2: *(__u16 *)res = *(volatile __u16 *)p; break;
210 case 4: *(__u32 *)res = *(volatile __u32 *)p; break;
211#ifdef CONFIG_64BIT
212 case 8: *(__u64 *)res = *(volatile __u64 *)p; break;
213#endif
214 default:
215 barrier();
216 __builtin_memcpy((void *)res, (const void *)p, size);
217 data_access_exceeds_word_size();
218 barrier();
219 }
220}
221
222static __always_inline void __write_once_size(volatile void *p, void *res, int size)
223{
224 switch (size) {
225 case 1: *(volatile __u8 *)p = *(__u8 *)res; break;
226 case 2: *(volatile __u16 *)p = *(__u16 *)res; break;
227 case 4: *(volatile __u32 *)p = *(__u32 *)res; break;
228#ifdef CONFIG_64BIT
229 case 8: *(volatile __u64 *)p = *(__u64 *)res; break;
230#endif
231 default:
232 barrier();
233 __builtin_memcpy((void *)p, (const void *)res, size);
234 data_access_exceeds_word_size();
235 barrier();
236 }
237}
238
239/*
240 * Prevent the compiler from merging or refetching reads or writes. The
241 * compiler is also forbidden from reordering successive instances of
242 * READ_ONCE, WRITE_ONCE and ACCESS_ONCE (see below), but only when the
243 * compiler is aware of some particular ordering. One way to make the
244 * compiler aware of ordering is to put the two invocations of READ_ONCE,
245 * WRITE_ONCE or ACCESS_ONCE() in different C statements.
246 *
247 * In contrast to ACCESS_ONCE these two macros will also work on aggregate
248 * data types like structs or unions. If the size of the accessed data
249 * type exceeds the word size of the machine (e.g., 32 bits or 64 bits)
250 * READ_ONCE() and WRITE_ONCE() will fall back to memcpy and print a
251 * compile-time warning.
252 *
253 * Their two major use cases are: (1) Mediating communication between
254 * process-level code and irq/NMI handlers, all running on the same CPU,
255 * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
256 * mutilate accesses that either do not require ordering or that interact
257 * with an explicit memory barrier or atomic instruction that provides the
258 * required ordering.
259 */
260
261#define READ_ONCE(x) \
262 ({ typeof(x) __val; __read_once_size(&x, &__val, sizeof(__val)); __val; })
263
264#define WRITE_ONCE(x, val) \
265 ({ typeof(x) __val; __val = val; __write_once_size(&x, &__val, sizeof(__val)); __val; })
266
193#endif /* __KERNEL__ */ 267#endif /* __KERNEL__ */
194 268
195#endif /* __ASSEMBLY__ */ 269#endif /* __ASSEMBLY__ */
@@ -315,7 +389,7 @@ void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
315 389
316/* Is this type a native word size -- useful for atomic operations */ 390/* Is this type a native word size -- useful for atomic operations */
317#ifndef __native_word 391#ifndef __native_word
318# define __native_word(t) (sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long)) 392# define __native_word(t) (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
319#endif 393#endif
320 394
321/* Compile time object size, -1 for unknown */ 395/* Compile time object size, -1 for unknown */