Skip to contentSkip to conversation

vdso: Add VDSO_UNALIGNED_PACKED config option

PATCH Applied Ian Rogers 4 participants 9 messages Oct 20 – Nov 9

Subsystems (3)
lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

PATCH v2 1/2 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()")

---
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(+)
include/vdso/unaligned.h +16 -0
index 9076483c9fbb..726fd15efc82 100644
@@ -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 */
lib/vdso/Kconfig +10 -0
index 597f5f0f9681..2436c20ee0ef 100644
@@ -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.
Signature · 1 line
2.56.0.212.g129ede20d9-goog
author
#1 lore raw

PATCH v2 2/2 tools headers: Update the vdso/unaligned.h copy with the kernel sources

To pick up the changes in:

vdso: Add VDSO_UNALIGNED_PACKED config option

---
 tools/include/vdso/unaligned.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
tools/include/vdso/unaligned.h +16 -0
index 9076483c9fbb..726fd15efc82 100644
@@ -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 */
Signature · 1 line
2.56.0.212.g129ede20d9-goog
merge bot Applied
#2 lore raw

[tip: timers/urgent] tools headers: Update the vdso/unaligned.h copy with the kernel sources

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

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

Commit-ID:     5e85b36768b7bba53a55b1c9a1b1cdcab3684634
Gitweb:        https://git.kernel.org/tip/5e85b36768b7bba53a55b1c9a1b1cdcab3684634
Author:        Ian Rogers <[email protected]>
AuthorDate:    Tue, 20 Oct 2026 09:32:10 -07:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Mon, 09 Nov 2026 16:34:51 +01:00

tools headers: Update the vdso/unaligned.h copy with the kernel sources

To pick up the changes in:

vdso: Add VDSO_UNALIGNED_PACKED config option

---
 tools/include/vdso/unaligned.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
tools/include/vdso/unaligned.h +16 -0
index 9076483..726fd15 100644
@@ -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 */
#3 lore raw
In reply to Ian Rogers
Quoted 35 lines · On Tue, 20 Oct 2026 09:32:09 -0700 Ian Rogers <[email protected]> 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()")

---
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(+)
include/vdso/unaligned.h +1 -0
index 9076483c9fbb..726fd15efc82 100644
@@ -4,6 +4,20 @@
 
 #include <linux/compiler_types.h>
 
+#ifdef CONFIG_VDSO_UNALIGNED_PACKED

include/vdso/unaligned.h line 17

Quoted 10 lines
include/vdso/unaligned.h +10 -0
+
+#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

Quoted 31 lines
include/vdso/unaligned.h +5 -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 */
lib/vdso/Kconfig +10 -0
index 597f5f0f9681..2436c20ee0ef 100644
@@ -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.
#4 lore raw
Quoted 21 lines · David Laight
Quoted 4 lines · On Tue, 20 Oct 2026 09:32:09 -0700 Ian Rogers <[email protected]> wrote:

#include <linux/compiler_types.h>

include/vdso/unaligned.h +1 -0
+#ifdef CONFIG_VDSO_UNALIGNED_PACKED

include/vdso/unaligned.h line 17

Quoted 10 lines
include/vdso/unaligned.h +10 -0
+
+#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.

Quoted 4 lines

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).

This is the traditional way we have been doing the unaligned access in the kernel before the change. By annotating the structure as __packed, we tell the compiler that it cannot rely on alignment here, so it uses whatever method it knows to access unaligned data depending on the architecture and flags.

The reason we need the struct here is that you cannot use __packed as a qualifier on a variable, only on a type or struct member.

Quoted 4 lines

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.

strict aliasing is thankfully never enabled in the kernel.

The cast is from a higher alignment to byte alignment, and that is always allowed unlike the reverse. Technically the only reason we ever need the get_unaligned() helpers is to work around pointers to objects that should exist because the ABI requires them to be aligned but the pointer instead came out of some byte stream or from taking a (non-__packed) pointer of a __packed struct member.

Arnd

#5 lore raw
Quoted 24 lines · On Fri, 23 Oct 2026 23:49:50 +0200 "Arnd Bergmann" <[email protected]> wrote:
Quoted 23 lines · David Laight
Quoted 4 lines · On Tue, 20 Oct 2026 09:32:09 -0700 Ian Rogers <[email protected]> wrote:

#include <linux/compiler_types.h>

include/vdso/unaligned.h +1 -0
+#ifdef CONFIG_VDSO_UNALIGNED_PACKED

include/vdso/unaligned.h line 17

Quoted 10 lines
include/vdso/unaligned.h +10 -0
+
+#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.

Quoted 8 lines
Quoted 2 lines

Additionally I think the compiler can sometimes track the alignment through casts (although it didn't for a quick check I did earlier).

This is the traditional way we have been doing the unaligned access in the kernel before the change. By annotating the structure as __packed, we tell the compiler that it cannot rely on alignment here, so it uses whatever method it knows to access unaligned data depending on the architecture and flags.

That relies on the compiler not tracking the alignment through the casts. The only way the C language lets you get a 'foo *' pointer is taking the address of a 'foo' data item so it must be aligned.
So even if you cast it to another type that alignment can be assumed. I've seen crashes from memcpy() using word accesses when trying to copy from a misaligned pointer into an aligned copy of the structure. Now the current code must be avoiding that - but I think the compiler can do it.

Quoted 2 lines

The reason we need the struct here is that you cannot use __packed as a qualifier on a variable, only on a type or struct member.

I know all about that.
Somewhere it also says that you can only increase the alignment (I think of a type), but you can definitely have a type for 'u64 with 32bit alignment' and use it in a structure.

Quoted 8 lines
Quoted 5 lines

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.

strict aliasing is thankfully never enabled in the kernel.

Quoted 7 lines

The cast is from a higher alignment to byte alignment, and that is always allowed unlike the reverse. Technically the only reason we ever need the get_unaligned() helpers is to work around pointers to objects that should exist because the ABI requires them to be aligned but the pointer instead came out of some byte stream or from taking a (non-__packed) pointer of a __packed struct member.

I think those ought to be found and marked packed.
I doubt any of them get used other than part of the byte stream. That leaves the explicit get_unaligned_le32() used to pull 32bit values from a byte stream (that isn't defined by a structure).

But we're probably arguing in basic agreement.

Quoted 2 lines

Arnd

#6 lore raw
Quoted 10 lines · David Laight
Quoted 8 lines · On Fri, 23 Oct 2026 23:49:50 +0200 "Arnd Bergmann" <[email protected]> wrote:
Quoted 1 line · David Laight

On Tue, 20 Oct 2026 09:32:09 -0700 Ian Rogers <[email protected]> wrote:

This is the traditional way we have been doing the unaligned access in the kernel before the change. By annotating the structure as __packed, we tell the compiler that it cannot rely on alignment here, so it uses whatever method it knows to access unaligned data depending on the architecture and flags.

Quoted 3 lines

That relies on the compiler not tracking the alignment through the casts. The only way the C language lets you get a 'foo *' pointer is taking the address of a 'foo' data item so it must be aligned.

I have definitely seen the reverse, where a u32 pointer dereference in __raw_readl() gets turned into individual byte accesses because the pointer originally came from a packed structure. This is also bad because it breaks MMIO register access.

We also know that the packed struct version works, either because the standard requires a punned pointer to respect the minimum alignment, or because the implementation is smart enough to only ever lower the alignment for punned pointers.

Quoted 5 lines

So even if you cast it to another type that alignment can be assumed. I've seen crashes from memcpy() using word accesses when trying to copy from a misaligned pointer into an aligned copy of the structure. Now the current code must be avoiding that - but I think the compiler can do it.

Right, especially the memcpy() without type punning seems dangerous. We have had several instances of code breaking on armv7 where CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS is set but the stp/ldp instructions for 64-bit objects still require alignment.

I don't see any instances of the exact issue you describe, but this one is similar and definitely broken on armv7 (adding the dw_mmc maintainers to Cc for information):

#ifndef readq

#define __raw_writeq(__value, __reg) \
        (*(volatile u64 __force *)(__reg) = (__value))

#define __raw_readq(__reg) (*(volatile u64 __force *)(__reg)) #endif
#define mci_fifo_writeq(__value, __reg) __raw_writeq(__reg, __value) static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt) {
...

#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
        if (unlikely((unsigned long)buf & 0x7)) {
                while (cnt >= 8) {
                        u64 aligned_buf[16];
                        int len = min(cnt & -8, (int)sizeof(aligned_buf));
                        int items = len >> 3;
                        int i;
                        /* memcpy from input buffer into aligned buffer */
                        memcpy(aligned_buf, buf, len);
                        buf += len;
                        cnt -= len;
                        /* push data from aligned buffer into fifo */
                        for (i = 0; i < items; ++i)
                                mci_fifo_writeq(host->fifo_reg, aligned_buf[i]);
                }
        } else
#endif
        {
                u64 *pdata = buf;

                for (; cnt >= 8; cnt -= 8)
                        mci_fifo_writeq(host->fifo_reg, *pdata++);
                buf = pdata;
        }

The 'u64 *pdata = buf' cast is of course broken here since that is not allowed for unaligned pointers on supported target regardless of CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS. The else{...} portion has to be written using get_unaligned_u64() here, as the #ifndef branch should only optimize for performance and not be required for correctness.
If '*buf' was a u64 pointer instead of void, I could see the compiler messing this up in the way you describe, by optimizing out the temporary array entirely.

An unrelated problem is that the mci_fifo_writeq() definition is not portable because the volatile u64 pointer dereference does not guarantee that the two 32-bit halves are stored in the order that the hardware register requires. Presumably that was what led to DW_MMC_QUIRK_FIFO64_32.

Quoted 7 lines
Quoted 2 lines

The reason we need the struct here is that you cannot use __packed as a qualifier on a variable, only on a type or struct member.

I know all about that.
Somewhere it also says that you can only increase the alignment (I think of a type), but you can definitely have a type for 'u64 with 32bit alignment' and use it in a structure.

Right, we do rely on that in e.g. x86

typedef s64 __attribute__((aligned(4))) compat_s64;

We cannot use this in a macro though because there is no way to define a local typedef inside of a function or to change the alignment of pointer without the struct.

Arnd

#7 lore raw
Quoted 11 lines · On Sat, 24 Oct 2026 13:05:19 +0200 "Arnd Bergmann" <[email protected]> wrote:
Quoted 10 lines · David Laight
Quoted 8 lines · On Fri, 23 Oct 2026 23:49:50 +0200 "Arnd Bergmann" <[email protected]> wrote:
Quoted 1 line · David Laight

On Tue, 20 Oct 2026 09:32:09 -0700 Ian Rogers <[email protected]> wrote:

This is the traditional way we have been doing the unaligned access in the kernel before the change. By annotating the structure as __packed, we tell the compiler that it cannot rely on alignment here, so it uses whatever method it knows to access unaligned data depending on the architecture and flags.

Quoted 8 lines
Quoted 3 lines

That relies on the compiler not tracking the alignment through the casts. The only way the C language lets you get a 'foo *' pointer is taking the address of a 'foo' data item so it must be aligned.

I have definitely seen the reverse, where a u32 pointer dereference in __raw_readl() gets turned into individual byte accesses because the pointer originally came from a packed structure. This is also bad because it breaks MMIO register access.

The code rather relies on the compiler not splitting a volatile access. But it is allowed to do that (eg 64bit accesses on 32bit) so it may be allowed all the time and the kernel just hopes it never does.

The whole thing is a bear pit :-)

Quoted 5 lines

We also know that the packed struct version works, either because the standard requires a punned pointer to respect the minimum alignment, or because the implementation is smart enough to only ever lower the alignment for punned pointers.

Or the implementation currently chooses to honour the lower alignment for punned pointers.

The standard doesn't allow many casts at all. IIRC you can cast a pointer to 'void *' and then back to its original type, and you might be able to cast a 'pointer to struct' to a pointer to its first element. Remember it has to allow for word addressable systems where the offset of a char in the word is held in (say) the top 2 bits.
(I doubt any such systems still exist...)

Quoted 11 lines
Quoted 6 lines

So even if you cast it to another type that alignment can be assumed. I've seen crashes from memcpy() using word accesses when trying to copy from a misaligned pointer into an aligned copy of the structure. Now the current code must be avoiding that - but I think the compiler can do it.

Right, especially the memcpy() without type punning seems dangerous. We have had several instances of code breaking on armv7 where CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS is set but the stp/ldp instructions for 64-bit objects still require alignment.

I had the compiler unroll loops on x86 and had a 'sum += *buf++;' checksum crash when the buffer (mmap() of a file) was misaligned.

Quoted 39 lines

I don't see any instances of the exact issue you describe, but this one is similar and definitely broken on armv7 (adding the dw_mmc maintainers to Cc for information):

#ifndef readq

#define __raw_writeq(__value, __reg) \
        (*(volatile u64 __force *)(__reg) = (__value))

#define __raw_readq(__reg) (*(volatile u64 __force *)(__reg)) #endif
#define mci_fifo_writeq(__value, __reg) __raw_writeq(__reg, __value) static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt) {
...

#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
        if (unlikely((unsigned long)buf & 0x7)) {
                while (cnt >= 8) {
                        u64 aligned_buf[16];
                        int len = min(cnt & -8, (int)sizeof(aligned_buf));
                        int items = len >> 3;
                        int i;
                        /* memcpy from input buffer into aligned buffer */
                        memcpy(aligned_buf, buf, len);
                        buf += len;
                        cnt -= len;
                        /* push data from aligned buffer into fifo */
                        for (i = 0; i < items; ++i)
                                mci_fifo_writeq(host->fifo_reg, aligned_buf[i]);
                }
        } else
#endif
        {
                u64 *pdata = buf;

                for (; cnt >= 8; cnt -= 8)
                        mci_fifo_writeq(host->fifo_reg, *pdata++);
                buf = pdata;
        }
Quoted 9 lines

The 'u64 *pdata = buf' cast is of course broken here since that is not allowed for unaligned pointers on supported target regardless of CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS. The else{...} portion has to be written using get_unaligned_u64() here, as the #ifndef branch should only optimize for performance and not be required for correctness.
If '*buf' was a u64 pointer instead of void, I could see the compiler messing this up in the way you describe, by optimizing out the temporary array entirely.

Jeepers that is horrid.
The simple:

	for (off = 0; off < cnt; off += 8)
		mci_fifo_writeq(host->fifo_reg, get_unaligned_u64(buf + off));

will generate better code.
The compiler will usually optimise out 'off', decrement 'cnt' and increment 'buf'. It does that even when you try to use negative offsets from the end of the buffer - which generates the best code on x86. (And you might want to copy host->fifo_reg into a local.)

It might be worth optimising for CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS being unset and the buffer being aligned (to sizeof long, not 8). But that isn't the conditional there.

Quoted 14 lines

An unrelated problem is that the mci_fifo_writeq() definition is not portable because the volatile u64 pointer dereference does not guarantee that the two 32-bit halves are stored in the order that the hardware register requires. Presumably that was what led to DW_MMC_QUIRK_FIFO64_32.

Quoted 7 lines
Quoted 2 lines

The reason we need the struct here is that you cannot use __packed as a qualifier on a variable, only on a type or struct member.

I know all about that.
Somewhere it also says that you can only increase the alignment (I think of a type), but you can definitely have a type for 'u64 with 32bit alignment' and use it in a structure.

Quoted 7 lines

Right, we do rely on that in e.g. x86

typedef s64 __attribute__((aligned(4))) compat_s64;

We cannot use this in a macro though because there is no way to define a local typedef inside of a function or to change the alignment of pointer without the struct.

I think it could be inside the cast - the same as the struct definition. But that is not really any different to the packed struct.

David

Quoted 2 lines

Arnd

merge bot Applied
#8 lore raw
In reply to Ian Rogers
The following commit has been merged into the timers/urgent branch of tip:

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 <[email protected]>
AuthorDate:    Tue, 20 Oct 2026 09:32:09 -07:00
Committer:     Thomas Gleixner <[email protected]>
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/.

---
 include/vdso/unaligned.h | 16 ++++++++++++++++
 lib/vdso/Kconfig         | 10 ++++++++++
 2 files changed, 26 insertions(+)
include/vdso/unaligned.h +16 -0
index 9076483..726fd15 100644
@@ -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 */
lib/vdso/Kconfig +10 -0
index 597f5f0..2436c20 100644
@@ -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.