* [PATCH v2 1/2] vdso: Add VDSO_UNALIGNED_PACKED config option
@ 2026-10-20 16:32 Ian Rogers
2026-10-20 16:32 ` [PATCH v2 2/2] tools headers: Update the vdso/unaligned.h copy with the kernel sources Ian Rogers
2026-10-20 18:07 ` [PATCH v2 1/2] vdso: Add VDSO_UNALIGNED_PACKED config option David Laight
0 siblings, 2 replies; 3+ messages in thread
From: Ian Rogers @ 2026-10-20 16:32 UTC (permalink / raw)
To: Andy Lutomirski, Thomas Gleixner, Vincenzo Frascino,
Arnaldo Carvalho de Melo
Cc: Stefan Kerkmann, Marc Kleine-Budde, Arnd Bergmann,
Christophe Leroy, David Laight, Helge Deller,
James E . J . Bottomley, Jason A . Donenfeld, Alexander Viro,
Eric Biggers, linux-kernel, linux-parisc, Ian Rogers
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@pengutronix.de>
Closes: https://lore.kernel.org/lkml/47f8c30b-e326-4ff0-a2bc-5683209a9e2d@pengutronix.de/
Signed-off-by: Ian Rogers <irogers@google.com>
---
v2: Default VDSO_UNALIGNED_PACKED to !HAVE_EFFICIENT_UNALIGNED_ACCESS
instead of CPU_32v5 to cover all strict-alignment targets (Sashiko,
Arnd Bergmann).
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 9076483c9fbb..726fd15efc82 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 597f5f0f9681..2436c20ee0ef 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.
--
2.56.0.212.g129ede20d9-goog
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH v2 2/2] tools headers: Update the vdso/unaligned.h copy with the kernel sources
2026-10-20 16:32 [PATCH v2 1/2] vdso: Add VDSO_UNALIGNED_PACKED config option Ian Rogers
@ 2026-10-20 16:32 ` Ian Rogers
2026-10-20 18:07 ` [PATCH v2 1/2] vdso: Add VDSO_UNALIGNED_PACKED config option David Laight
1 sibling, 0 replies; 3+ messages in thread
From: Ian Rogers @ 2026-10-20 16:32 UTC (permalink / raw)
To: Andy Lutomirski, Thomas Gleixner, Vincenzo Frascino,
Arnaldo Carvalho de Melo
Cc: Stefan Kerkmann, Marc Kleine-Budde, Arnd Bergmann,
Christophe Leroy, David Laight, Helge Deller,
James E . J . Bottomley, Jason A . Donenfeld, Alexander Viro,
Eric Biggers, linux-kernel, linux-parisc, Ian Rogers
To pick up the changes in:
vdso: Add VDSO_UNALIGNED_PACKED config option
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/include/vdso/unaligned.h | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/tools/include/vdso/unaligned.h b/tools/include/vdso/unaligned.h
index 9076483c9fbb..726fd15efc82 100644
--- a/tools/include/vdso/unaligned.h
+++ b/tools/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 */
--
2.56.0.212.g129ede20d9-goog
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v2 1/2] vdso: Add VDSO_UNALIGNED_PACKED config option
2026-10-20 16:32 [PATCH v2 1/2] vdso: Add VDSO_UNALIGNED_PACKED config option Ian Rogers
2026-10-20 16:32 ` [PATCH v2 2/2] tools headers: Update the vdso/unaligned.h copy with the kernel sources Ian Rogers
@ 2026-10-20 18:07 ` David Laight
1 sibling, 0 replies; 3+ messages in thread
From: David Laight @ 2026-10-20 18:07 UTC (permalink / raw)
To: Ian Rogers
Cc: Andy Lutomirski, Thomas Gleixner, Vincenzo Frascino,
Arnaldo Carvalho de Melo, Stefan Kerkmann, Marc Kleine-Budde,
Arnd Bergmann, Christophe Leroy, Helge Deller,
James E . J . Bottomley, Jason A . Donenfeld, Alexander Viro,
Eric Biggers, linux-kernel, linux-parisc
On Tue, 20 Oct 2026 09:32:09 -0700
Ian Rogers <irogers@google.com> wrote:
> 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@pengutronix.de>
> Closes: https://lore.kernel.org/lkml/47f8c30b-e326-4ff0-a2bc-5683209a9e2d@pengutronix.de/
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
> v2: Default VDSO_UNALIGNED_PACKED to !HAVE_EFFICIENT_UNALIGNED_ACCESS
> instead of CPU_32v5 to cover all strict-alignment targets (Sashiko,
> Arnd Bergmann).
>
> 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 9076483c9fbb..726fd15efc82 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)
I'm confused (easily done).
That code relies on strict alignment being disabled regardless of whether
misaligned accesses fault or not.
So it is either always valid or always wrong.
Additionally I think the compiler can sometimes track the alignment through
casts (although it didn't for a quick check I did earlier).
Since having misaligned non-packed structures really doesn't work, the only
places these functions should be used is on u8[] buffers.
Pointers into those can (probably) be cast to packed structure types
without causing issues if strict aliasing is enabled.
If you write the value as u64 then read as u32 I think they could cross
even though the buffer is u8[].
That might mean you need a barrier() before the read in get() and before
and after the write in put().
(Partial barriers may work - dunno.)
David
> +
> +#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 597f5f0f9681..2436c20ee0ef 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.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-10-20 18:07 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-20 16:32 [PATCH v2 1/2] vdso: Add VDSO_UNALIGNED_PACKED config option Ian Rogers
2026-10-20 16:32 ` [PATCH v2 2/2] tools headers: Update the vdso/unaligned.h copy with the kernel sources Ian Rogers
2026-10-20 18:07 ` [PATCH v2 1/2] vdso: Add VDSO_UNALIGNED_PACKED config option David Laight
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®