Skip to content

Commit 1d7cf25

Browse files
captain5050Thomas Gleixner
authored andcommitted
tools headers: Update the linux/unaligned.h copy with the kernel sources
To pick up the changes in: vdso: Switch get/put_unaligned() from packed struct to memcpy As the code is dependent on __unqual_scalar_typeof, update also the tools version of compiler_types.h to include this. Signed-off-by: Ian Rogers <irogers@google.com> Signed-off-by: Thomas Gleixner <tglx@kernel.org> Link: https://patch.msgid.link/20251016205126.2882625-4-irogers@google.com
1 parent a339671 commit 1d7cf25

2 files changed

Lines changed: 57 additions & 6 deletions

File tree

tools/include/linux/compiler_types.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,26 @@
4040
#define asm_goto_output(x...) asm goto(x)
4141
#endif
4242

43+
/*
44+
* __unqual_scalar_typeof(x) - Declare an unqualified scalar type, leaving
45+
* non-scalar types unchanged.
46+
*/
47+
/*
48+
* Prefer C11 _Generic for better compile-times and simpler code. Note: 'char'
49+
* is not type-compatible with 'signed char', and we define a separate case.
50+
*/
51+
#define __scalar_type_to_expr_cases(type) \
52+
unsigned type: (unsigned type)0, \
53+
signed type: (signed type)0
54+
55+
#define __unqual_scalar_typeof(x) typeof( \
56+
_Generic((x), \
57+
char: (char)0, \
58+
__scalar_type_to_expr_cases(char), \
59+
__scalar_type_to_expr_cases(short), \
60+
__scalar_type_to_expr_cases(int), \
61+
__scalar_type_to_expr_cases(long), \
62+
__scalar_type_to_expr_cases(long long), \
63+
default: (x)))
64+
4365
#endif /* __LINUX_COMPILER_TYPES_H */

tools/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)