[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20251015145332.260eebe6@pumpkin>
Date: Wed, 15 Oct 2025 14:53:32 +0100
From: David Laight <david.laight.linux@...il.com>
To: Finn Thain <fthain@...ux-m68k.org>
Cc: Peter Zijlstra <peterz@...radead.org>, Will Deacon <will@...nel.org>,
Andrew Morton <akpm@...ux-foundation.org>, Boqun Feng
<boqun.feng@...il.com>, Jonathan Corbet <corbet@....net>, Mark Rutland
<mark.rutland@....com>, Arnd Bergmann <arnd@...db.de>,
linux-kernel@...r.kernel.org, linux-arch@...r.kernel.org, Geert
Uytterhoeven <geert@...ux-m68k.org>, linux-m68k@...r.kernel.org,
linux-doc@...r.kernel.org
Subject: Re: [RFC v3 1/5] documentation: Discourage alignment assumptions
On Wed, 15 Oct 2025 18:40:39 +1100 (AEDT)
Finn Thain <fthain@...ux-m68k.org> wrote:
> On Tue, 14 Oct 2025, David Laight wrote:
>
> > On Wed, 08 Oct 2025 09:19:20 +1100
> > Finn Thain <fthain@...ux-m68k.org> wrote:
> >
> > > Discourage assumptions that simply don't hold for all Linux ABIs.
> > > Exceptions to the natural alignment rule for scalar types include
> > > long long on i386 and sh.
> > > ---
> > > Documentation/core-api/unaligned-memory-access.rst | 7 -------
> > > 1 file changed, 7 deletions(-)
> > >
> > > diff --git a/Documentation/core-api/unaligned-memory-access.rst b/Documentation/core-api/unaligned-memory-access.rst
> > > index 5ceeb80eb539..1390ce2b7291 100644
> > > --- a/Documentation/core-api/unaligned-memory-access.rst
> > > +++ b/Documentation/core-api/unaligned-memory-access.rst
> > > @@ -40,9 +40,6 @@ The rule mentioned above forms what we refer to as natural alignment:
> > > When accessing N bytes of memory, the base memory address must be evenly
> > > divisible by N, i.e. addr % N == 0.
> > >
> > > -When writing code, assume the target architecture has natural alignment
> > > -requirements.
> >
> > I think I'd be more explicit, perhaps:
> > Note that not all architectures align 64bit items on 8 byte boundaries or
> > even 32bit items on 4 byte boundaries.
> >
>
> That's what the next para is alluding to...
>
> > > In reality, only a few architectures require natural alignment on all sizes
> > > of memory access. However, we must consider ALL supported architectures;
> > > writing code that satisfies natural alignment requirements is the easiest way
> > > to achieve full portability.
>
> How about this?
>
> "In reality, only a few architectures require natural alignment for all
> sizes of memory access. That is, not all architectures need 64-bit values
> to be aligned on 8-byte boundaries and 32-bit values on 4-byte boundaries.
> However, when writing code intended to achieve full portability, we must
> consider all supported architectures."
There are several separate alignments:
- The alignment the cpu needs, for most x86 instructions this is 1 byte [1].
Many RISC cpu require 'word' alignment (for some definition of 'word').
A problematic case is data that crosses page boundaries.
- The alignment the compiler uses for structure members; returned by _Alignof().
m68k only 16bit aligns 32bit values.
- The 'preferred' alignment returned by __alignof__().
32bit x86 returns 8 for 64bit types even though the ABI only 4-byte aligns them.
- The 'natural' alignment based on the size of the item.
I'd guess that 'complex double' (if supported) may only be 8 byte aligned.
What normally matters is the ABI alignment for structure members.
If you mark anything 'packed' the compiler will generate shifts and masks (etc)
to get working code.
Taking the address of an item in a packed structure generates a warning
for very good reason.
[1] I've fallen foul of gcc deciding to 'vectorise' a loop and then having
it crash because the buffer address was misaligned.
Nasty because the code worked in initial testing and I expected the loop
(32bit adds of a buffer) to work fine even when misaligned.
David
>
> > > @@ -103,10 +100,6 @@ Therefore, for standard structure types you can always rely on the compiler
> > > to pad structures so that accesses to fields are suitably aligned (assuming
> > > you do not cast the field to a type of different length).
> > >
> > > -Similarly, you can also rely on the compiler to align variables and function
> > > -parameters to a naturally aligned scheme, based on the size of the type of
> > > -the variable.
> > > -
> > > At this point, it should be clear that accessing a single byte (u8 or char)
> > > will never cause an unaligned access, because all memory addresses are evenly
> > > divisible by one.
> >
> >
Powered by blists - more mailing lists