From: David Laight <david.laight.linux@gmail.com>
To: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Lance Yang <lance.yang@linux.dev>,
Finn Thain <fthain@linux-m68k.org>,
akpm@linux-foundation.org, linux-kernel@vger.kernel.org,
mhiramat@kernel.org, oak@helsinkinet.fi, peterz@infradead.org,
stable@vger.kernel.org, will@kernel.org,
Lance Yang <ioworker0@gmail.com>,
linux-m68k@lists.linux-m68k.org
Subject: Re: [PATCH] atomic: Specify natural alignment for atomic_t
Date: Sat, 6 Sep 2025 12:43:04 +0100 [thread overview]
Message-ID: <20250906124304.6dc17f1f@pumpkin> (raw)
In-Reply-To: <CAMuHMdV-AtPm-W-QUC1HixJ8Koy_HdESwCCOhRs3Q26=wjWwog@mail.gmail.com>
On Mon, 1 Sep 2025 10:45:46 +0200
Geert Uytterhoeven <geert@linux-m68k.org> wrote:
> Hi Lance,
>
> On Thu, 28 Aug 2025 at 04:05, Lance Yang <lance.yang@linux.dev> wrote:
> > On 2025/8/28 07:43, Finn Thain wrote:
> > > On Mon, 25 Aug 2025, Lance Yang wrote:
> > >> Same here, using a global static variable instead of a local one. The
> > >> result is consistently misaligned.
> > >>
> > >> ```
> > >> #include <linux/module.h>
> > >> #include <linux/init.h>
> > >>
> > >> static struct __attribute__((packed)) test_container {
> > >> char padding[49];
> > >> struct mutex io_lock;
> > >> } cont;
> > >>
> > >> static int __init alignment_init(void)
> > >> {
> > >> pr_info("Container base address : %px\n", &cont);
> > >> pr_info("io_lock member address : %px\n", &cont.io_lock);
> > >> pr_info("io_lock address offset mod 4: %lu\n", (unsigned long)&cont.io_lock % 4);
> > >> return 0;
> > >> }
> > >>
> > >> static void __exit alignment_exit(void)
> > >> {
> > >> pr_info("Module unloaded\n");
> > >> }
> > >>
> > >> module_init(alignment_init);
> > >> module_exit(alignment_exit);
> > >> MODULE_LICENSE("GPL");
> > >> MODULE_AUTHOR("x");
> > >> MODULE_DESCRIPTION("x");
> > >> ```
> > >>
> > >> Result from dmesg:
> > >>
> > >> ```
> > >> [Mon Aug 25 19:33:28 2025] Container base address : ffffffffc28f0940
> > >> [Mon Aug 25 19:33:28 2025] io_lock member address : ffffffffc28f0971
> > >> [Mon Aug 25 19:33:28 2025] io_lock address offset mod 4: 1
> > >> ```
> > >>
> > >
> > > FTR, I was able to reproduce that result (i.e. static storage):
> > >
> > > [ 0.320000] Container base address : 0055d9d0
> > > [ 0.320000] io_lock member address : 0055da01
> > > [ 0.320000] io_lock address offset mod 4: 1
> > >
> > > I think the experiments you sent previously would have demonstrated the
> > > same result, except for the unpredictable base address that you sensibly
> > > logged in this version.
> >
> > Thanks for taking the time to reproduce it!
> >
> > This proves the problem can happen in practice (e.g., with packed structs),
> > so we need to ignore the unaligned pointers on the architectures that don't
> > trap for now.
>
> Putting locks inside a packed struct is definitely a Very Bad Idea
> and a No Go. Packed structs are meant to describe memory data and
> MMIO register layouts, and must not contain control data for critical
> sections.
Even for MMIO register layouts you don't (usually) want 'packed'.
You may need to add explicit padding, and an 'error if padded' attribute
you be useful.
Sometimes you have (eg) a 64bit item on a 32bit boundary, marking the
member 'packed' will remove the gap before it - usually what is wanted.
In reality pretty much nothing should be 'packed'.
David.
>
> Gr{oetje,eeting}s,
>
> Geert
>
next prev parent reply other threads:[~2025-09-06 11:43 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-25 2:03 [PATCH] atomic: Specify natural alignment for atomic_t Finn Thain
2025-08-25 3:27 ` Lance Yang
2025-08-25 3:59 ` Finn Thain
2025-08-25 4:22 ` Lance Yang
2025-08-25 4:07 ` Finn Thain
2025-08-25 5:00 ` Lance Yang
2025-08-25 6:17 ` Finn Thain
2025-08-25 7:46 ` Lance Yang
2025-08-25 10:49 ` Finn Thain
2025-08-25 11:19 ` Lance Yang
2025-08-25 11:36 ` Lance Yang
2025-08-27 23:43 ` Finn Thain
2025-08-28 2:05 ` Lance Yang
2025-09-01 8:45 ` Geert Uytterhoeven
2025-09-02 13:30 ` Lance Yang
2025-09-02 14:14 ` Geert Uytterhoeven
2025-09-06 11:43 ` David Laight [this message]
2025-08-25 12:07 ` David Laight
2025-08-25 12:33 ` Lance Yang
2025-08-27 8:00 ` Finn Thain
2025-08-27 9:34 ` Lance Yang
2025-09-01 8:48 ` Geert Uytterhoeven
2025-08-25 7:12 ` Peter Zijlstra
2025-08-25 8:03 ` Finn Thain
2025-08-25 11:41 ` Peter Zijlstra
2025-08-27 7:17 ` Finn Thain
2025-08-27 11:54 ` Peter Zijlstra
2025-08-28 9:53 ` Finn Thain
2025-09-01 9:36 ` Peter Zijlstra
2025-09-01 9:40 ` Peter Zijlstra
2025-08-26 15:22 ` Eero Tamminen
2025-08-26 17:33 ` Lance Yang
2025-09-01 8:51 ` Geert Uytterhoeven
2025-09-01 15:12 ` Eero Tamminen
2025-09-06 11:50 ` David Laight
2025-08-27 2:45 ` Masami Hiramatsu
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=20250906124304.6dc17f1f@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=fthain@linux-m68k.org \
--cc=geert@linux-m68k.org \
--cc=ioworker0@gmail.com \
--cc=lance.yang@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-m68k@lists.linux-m68k.org \
--cc=mhiramat@kernel.org \
--cc=oak@helsinkinet.fi \
--cc=peterz@infradead.org \
--cc=stable@vger.kernel.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).