diff options
Diffstat (limited to 'include/linux/compiler.h')
| -rw-r--r-- | include/linux/compiler.h | 66 |
1 files changed, 53 insertions, 13 deletions
diff --git a/include/linux/compiler.h b/include/linux/compiler.h index c836eb2dc44d..3d7810341b57 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h | |||
| @@ -198,19 +198,45 @@ void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect); | |||
| 198 | 198 | ||
| 199 | #include <uapi/linux/types.h> | 199 | #include <uapi/linux/types.h> |
| 200 | 200 | ||
| 201 | static __always_inline void __read_once_size(const volatile void *p, void *res, int size) | 201 | #define __READ_ONCE_SIZE \ |
| 202 | ({ \ | ||
| 203 | switch (size) { \ | ||
| 204 | case 1: *(__u8 *)res = *(volatile __u8 *)p; break; \ | ||
| 205 | case 2: *(__u16 *)res = *(volatile __u16 *)p; break; \ | ||
| 206 | case 4: *(__u32 *)res = *(volatile __u32 *)p; break; \ | ||
| 207 | case 8: *(__u64 *)res = *(volatile __u64 *)p; break; \ | ||
| 208 | default: \ | ||
| 209 | barrier(); \ | ||
| 210 | __builtin_memcpy((void *)res, (const void *)p, size); \ | ||
| 211 | barrier(); \ | ||
| 212 | } \ | ||
| 213 | }) | ||
| 214 | |||
| 215 | static __always_inline | ||
| 216 | void __read_once_size(const volatile void *p, void *res, int size) | ||
| 202 | { | 217 | { |
| 203 | switch (size) { | 218 | __READ_ONCE_SIZE; |
| 204 | case 1: *(__u8 *)res = *(volatile __u8 *)p; break; | 219 | } |
| 205 | case 2: *(__u16 *)res = *(volatile __u16 *)p; break; | 220 | |
| 206 | case 4: *(__u32 *)res = *(volatile __u32 *)p; break; | 221 | #ifdef CONFIG_KASAN |
| 207 | case 8: *(__u64 *)res = *(volatile __u64 *)p; break; | 222 | /* |
| 208 | default: | 223 | * This function is not 'inline' because __no_sanitize_address confilcts |
| 209 | barrier(); | 224 | * with inlining. Attempt to inline it may cause a build failure. |
| 210 | __builtin_memcpy((void *)res, (const void *)p, size); | 225 | * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368 |
| 211 | barrier(); | 226 | * '__maybe_unused' allows us to avoid defined-but-not-used warnings. |
| 212 | } | 227 | */ |
| 228 | static __no_sanitize_address __maybe_unused | ||
| 229 | void __read_once_size_nocheck(const volatile void *p, void *res, int size) | ||
| 230 | { | ||
| 231 | __READ_ONCE_SIZE; | ||
| 232 | } | ||
| 233 | #else | ||
| 234 | static __always_inline | ||
| 235 | void __read_once_size_nocheck(const volatile void *p, void *res, int size) | ||
| 236 | { | ||
| 237 | __READ_ONCE_SIZE; | ||
| 213 | } | 238 | } |
| 239 | #endif | ||
| 214 | 240 | ||
| 215 | static __always_inline void __write_once_size(volatile void *p, void *res, int size) | 241 | static __always_inline void __write_once_size(volatile void *p, void *res, int size) |
| 216 | { | 242 | { |
| @@ -248,8 +274,22 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s | |||
| 248 | * required ordering. | 274 | * required ordering. |
| 249 | */ | 275 | */ |
| 250 | 276 | ||
| 251 | #define READ_ONCE(x) \ | 277 | #define __READ_ONCE(x, check) \ |
| 252 | ({ union { typeof(x) __val; char __c[1]; } __u; __read_once_size(&(x), __u.__c, sizeof(x)); __u.__val; }) | 278 | ({ \ |
| 279 | union { typeof(x) __val; char __c[1]; } __u; \ | ||
| 280 | if (check) \ | ||
| 281 | __read_once_size(&(x), __u.__c, sizeof(x)); \ | ||
| 282 | else \ | ||
| 283 | __read_once_size_nocheck(&(x), __u.__c, sizeof(x)); \ | ||
| 284 | __u.__val; \ | ||
| 285 | }) | ||
| 286 | #define READ_ONCE(x) __READ_ONCE(x, 1) | ||
| 287 | |||
| 288 | /* | ||
| 289 | * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need | ||
| 290 | * to hide memory access from KASAN. | ||
| 291 | */ | ||
| 292 | #define READ_ONCE_NOCHECK(x) __READ_ONCE(x, 0) | ||
| 253 | 293 | ||
| 254 | #define WRITE_ONCE(x, val) \ | 294 | #define WRITE_ONCE(x, val) \ |
| 255 | ({ \ | 295 | ({ \ |
