From: David Laight <david.laight.linux@gmail.com>
To: Finn Thain <fthain@linux-m68k.org>
Cc: Peter Zijlstra <peterz@infradead.org>,
Will Deacon <will@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
Boqun Feng <boqun.feng@gmail.com>,
Jonathan Corbet <corbet@lwn.net>,
Mark Rutland <mark.rutland@arm.com>,
Arnd Bergmann <arnd@arndb.de>,
linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org,
Geert Uytterhoeven <geert@linux-m68k.org>,
linux-m68k@vger.kernel.org, linux-doc@vger.kernel.org
Subject: Re: [RFC v3 1/5] documentation: Discourage alignment assumptions
Date: Wed, 15 Oct 2025 14:53:32 +0100 [thread overview]
Message-ID: <20251015145332.260eebe6@pumpkin> (raw)
In-Reply-To: <f5f939ae-f966-37ba-369d-be147c0642a3@linux-m68k.org>
On Wed, 15 Oct 2025 18:40:39 +1100 (AEDT)
Finn Thain <fthain@linux-m68k.org> wrote:
> On Tue, 14 Oct 2025, David Laight wrote:
>
> > On Wed, 08 Oct 2025 09:19:20 +1100
> > Finn Thain <fthain@linux-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.
> >
> >
next prev parent reply other threads:[~2025-10-15 13:53 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-07 22:19 [RFC v3 0/5] Align atomic storage Finn Thain
2025-10-07 22:19 ` [RFC v3 1/5] documentation: Discourage alignment assumptions Finn Thain
2025-10-14 10:23 ` David Laight
2025-10-15 7:40 ` Finn Thain
2025-10-15 13:53 ` David Laight [this message]
2025-10-16 6:53 ` Finn Thain
2025-10-16 13:30 ` Arnd Bergmann
2025-10-16 22:14 ` Finn Thain
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251015145332.260eebe6@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=arnd@arndb.de \
--cc=boqun.feng@gmail.com \
--cc=corbet@lwn.net \
--cc=fthain@linux-m68k.org \
--cc=geert@linux-m68k.org \
--cc=linux-arch@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-m68k@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=peterz@infradead.org \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).