[PATCH v4 0/3] Switch get/put unaligned to use memcpy

From: Ian Rogers
Date: Tue Jul 22 2025 - 17:58:50 EST


The existing type punning approach with packed structs requires
-fno-strict-aliasing to be passed to the compiler for
correctness. This is true in the kernel tree but not in the tools
directory resulting in this suggested patch from Eric Biggers
<ebiggers@xxxxxxxxxx>:
https://lore.kernel.org/lkml/20250625202311.23244-2-ebiggers@xxxxxxxxxx/

Requiring -fno-strict-aliasing seems unfortunate and so this patch
makes the unaligned code work via memcpy for type punning rather than
the packed attribute.

v4: switch the type/expression variable __get_unaligned_ctrl_type that
is used by _Generic to be a pointer to avoid 0 vs NULL usage
warnings - always use NULL and dereference the type. This should
also hopefully address analysis bots complaints.

v3: switch to __unqual_scalar_typeof, reducing the code, and use an
uninitialized variable rather than a cast of 0 to try to avoid a
sparse warning about not using NULL. The code is trying to
navigate a minefield of uninitialized and casting warnings,
hopefully the best balance has been struck, but the code will fail
for cases like:
const void *val = get_unaligned((const void * const *)ptr);
due to __unqual_scalar_typeof leaving the 2nd const of the cast in
place. Thankfully no code does this - tested with an
allyesconfig. Support would be achievable by using void* as a
default case in __unqual_scalar_typeof, it just doesn't seem worth
it for a fairly unusual const case.

v2: switch memcpy to __builtin_memcpy to avoid potential/disallowed
memcpy calls in vdso caused by -fno-builtin. Reported by
Christophe Leroy <christophe.leroy@xxxxxxxxxx>:
https://lore.kernel.org/lkml/c57de5bf-d55c-48c5-9dfa-e2fb844dafe9@xxxxxxxxxx/

Ian Rogers (3):
vdso: Switch get/put unaligned from packed struct to memcpy
tools headers: Update the linux/unaligned.h copy with the kernel
sources
tools headers: Remove unneeded ignoring of warnings in unaligned.h

include/vdso/unaligned.h | 41 ++++++++++++++++++++++++----
tools/include/linux/compiler_types.h | 22 +++++++++++++++
tools/include/linux/unaligned.h | 4 ---
tools/include/vdso/unaligned.h | 41 ++++++++++++++++++++++++----
4 files changed, 92 insertions(+), 16 deletions(-)

--
2.50.0.727.gbf7dc18ff4-goog