Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

vdso: Switch get/put_unaligned() from packed struct to memcpy()

Type punning is necessary for get/put_unaligned() but the use of a packed
struct violates strict aliasing rules, requiring -fno-strict-aliasing to be
passed to the C compiler.

Switch to using memcpy() so that -fno-strict-aliasing isn't necessary.

Signed-off-by: Ian Rogers <irogers@google.com>
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
Link: https://patch.msgid.link/20251016205126.2882625-3-irogers@google.com

authored by

Ian Rogers and committed by
Thomas Gleixner
a339671d df0f9a66

+35 -6
+35 -6
include/vdso/unaligned.h
··· 2 2 #ifndef __VDSO_UNALIGNED_H 3 3 #define __VDSO_UNALIGNED_H 4 4 5 - #define __get_unaligned_t(type, ptr) ({ \ 6 - const struct { type x; } __packed * __get_pptr = (typeof(__get_pptr))(ptr); \ 7 - __get_pptr->x; \ 5 + #include <linux/compiler_types.h> 6 + 7 + /** 8 + * __get_unaligned_t - read an unaligned value from memory. 9 + * @type: the type to load from the pointer. 10 + * @ptr: the pointer to load from. 11 + * 12 + * Use memcpy to affect an unaligned type sized load avoiding undefined behavior 13 + * from approaches like type punning that require -fno-strict-aliasing in order 14 + * to be correct. As type may be const, use __unqual_scalar_typeof to map to a 15 + * non-const type - you can't memcpy into a const type. The 16 + * __get_unaligned_ctrl_type gives __unqual_scalar_typeof its required 17 + * expression rather than type, a pointer is used to avoid warnings about mixing 18 + * the use of 0 and NULL. The void* cast silences ubsan warnings. 19 + */ 20 + #define __get_unaligned_t(type, ptr) ({ \ 21 + type *__get_unaligned_ctrl_type __always_unused = NULL; \ 22 + __unqual_scalar_typeof(*__get_unaligned_ctrl_type) __get_unaligned_val; \ 23 + __builtin_memcpy(&__get_unaligned_val, (void *)(ptr), \ 24 + sizeof(__get_unaligned_val)); \ 25 + __get_unaligned_val; \ 8 26 }) 9 27 10 - #define __put_unaligned_t(type, val, ptr) do { \ 11 - struct { type x; } __packed * __put_pptr = (typeof(__put_pptr))(ptr); \ 12 - __put_pptr->x = (val); \ 28 + /** 29 + * __put_unaligned_t - write an unaligned value to memory. 30 + * @type: the type of the value to store. 31 + * @val: the value to store. 32 + * @ptr: the pointer to store to. 33 + * 34 + * Use memcpy to affect an unaligned type sized store avoiding undefined 35 + * behavior from approaches like type punning that require -fno-strict-aliasing 36 + * in order to be correct. The void* cast silences ubsan warnings. 37 + */ 38 + #define __put_unaligned_t(type, val, ptr) do { \ 39 + type __put_unaligned_val = (val); \ 40 + __builtin_memcpy((void *)(ptr), &__put_unaligned_val, \ 41 + sizeof(__put_unaligned_val)); \ 13 42 } while (0) 14 43 15 44 #endif /* __VDSO_UNALIGNED_H */