Skip to content

Commit a339671

Browse files
captain5050Thomas Gleixner
authored andcommitted
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
1 parent df0f9a6 commit a339671

1 file changed

Lines changed: 35 additions & 6 deletions

File tree

include/vdso/unaligned.h

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,43 @@
22
#ifndef __VDSO_UNALIGNED_H
33
#define __VDSO_UNALIGNED_H
44

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; \
826
})
927

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)); \
1342
} while (0)
1443

1544
#endif /* __VDSO_UNALIGNED_H */

0 commit comments

Comments
 (0)