[tip: timers/urgent] vdso: Add VDSO_UNALIGNED_PACKED config option

From: tip-bot2 for Ian Rogers

Date: Mon Nov 09 2026 - 10:43:16 EST


The following commit has been merged into the timers/urgent branch of tip:

Commit-ID: 2727aca4439ee4b9085141a3babadc8de580397f
Gitweb: https://git.kernel.org/tip/2727aca4439ee4b9085141a3babadc8de580397f
Author: Ian Rogers <irogers@xxxxxxxxxx>
AuthorDate: Tue, 20 Oct 2026 09:32:09 -07:00
Committer: Thomas Gleixner <tglx@xxxxxxxxxx>
CommitterDate: Mon, 09 Nov 2026 16:34:51 +01:00

vdso: Add VDSO_UNALIGNED_PACKED config option

Commit a339671db64b ("vdso: Switch get/put_unaligned() from packed struct
to memcpy()") switched __get_unaligned_t() and __put_unaligned_t() from
using a __packed struct to __builtin_memcpy() to avoid strict aliasing
undefined behavior.

On targets without efficient unaligned memory accesses (such as ARMv5),
GCC may not inline __builtin_memcpy() into byte shifts and instead emit
stack spills or much slower out-of-line memcpy() calls. Add
CONFIG_VDSO_UNALIGNED_PACKED, defaulting to
!HAVE_EFFICIENT_UNALIGNED_ACCESS, to use the __packed struct
implementation on strict-alignment targets while keeping
__builtin_memcpy() elsewhere and in tools/.

Fixes: a339671db64b ("vdso: Switch get/put_unaligned() from packed struct to memcpy()")
Reported-by: Stefan Kerkmann <s.kerkmann@xxxxxxxxxxxxxx>
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
Link: https://patch.msgid.link/20261020163210.742728-1-irogers@xxxxxxxxxx
Closes: https://lore.kernel.org/lkml/47f8c30b-e326-4ff0-a2bc-5683209a9e2d@xxxxxxxxxxxxxx/
---
include/vdso/unaligned.h | 16 ++++++++++++++++
lib/vdso/Kconfig | 10 ++++++++++
2 files changed, 26 insertions(+)

diff --git a/include/vdso/unaligned.h b/include/vdso/unaligned.h
index 9076483..726fd15 100644
--- a/include/vdso/unaligned.h
+++ b/include/vdso/unaligned.h
@@ -4,6 +4,20 @@

#include <linux/compiler_types.h>

+#ifdef CONFIG_VDSO_UNALIGNED_PACKED
+
+#define __get_unaligned_t(type, ptr) ({ \
+ const struct { type x; } __packed * __get_pptr = (typeof(__get_pptr))(ptr); \
+ __get_pptr->x; \
+})
+
+#define __put_unaligned_t(type, val, ptr) do { \
+ struct { type x; } __packed * __put_pptr = (typeof(__put_pptr))(ptr); \
+ __put_pptr->x = (val); \
+} while (0)
+
+#else /* !CONFIG_VDSO_UNALIGNED_PACKED */
+
/**
* __get_unaligned_t - read an unaligned value from memory.
* @type: the type to load from the pointer.
@@ -41,4 +55,6 @@
sizeof(__put_unaligned_val)); \
} while (0)

+#endif /* CONFIG_VDSO_UNALIGNED_PACKED */
+
#endif /* __VDSO_UNALIGNED_H */
diff --git a/lib/vdso/Kconfig b/lib/vdso/Kconfig
index 597f5f0..2436c20 100644
--- a/lib/vdso/Kconfig
+++ b/lib/vdso/Kconfig
@@ -23,3 +23,13 @@ config VDSO_GETRANDOM
select VDSO_DATASTORE
help
Selected by architectures that support vDSO getrandom().
+
+config VDSO_UNALIGNED_PACKED
+ bool
+ default !HAVE_EFFICIENT_UNALIGNED_ACCESS
+ help
+ Use packed structs rather than memcpy() in include/vdso/unaligned.h
+ for unaligned memory accesses. Enabled by default when the
+ architecture does not have efficient unaligned accesses, as GCC may
+ emit stack spills or out-of-line memcpy() calls instead of inlining
+ byte shifts.