From: Lance Yang <lance.yang@linux.dev>
To: Finn Thain <fthain@linux-m68k.org>
Cc: akpm@linux-foundation.org, geert@linux-m68k.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: Mon, 25 Aug 2025 19:19:19 +0800 [thread overview]
Message-ID: <d95592ec-f51e-4d80-b633-7440b4e69944@linux.dev> (raw)
In-Reply-To: <d07778f8-8990-226b-5171-4a36e6e18f32@linux-m68k.org>
Thanks for digging deeper!
On 2025/8/25 18:49, Finn Thain wrote:
>
> [Belated Cc linux-m68k...]
>
> On Mon, 25 Aug 2025, Lance Yang wrote:
>
>>
>> On 2025/8/25 14:17, Finn Thain wrote:
>>>
>>> On Mon, 25 Aug 2025, Lance Yang wrote:
>>>
>>>>
>>>> What if we squash the runtime check fix into your patch?
>>>
>>> Did my patch not solve the problem?
>>
>> Hmm... it should solve the problem for natural alignment, which is a
>> critical fix.
>>
>> But it cannot solve the problem of forced misalignment from drivers
>> using #pragma pack(1). The runtime warning will still trigger in those
>> cases.
>>
>> I built a simple test module on a kernel with your patch applied:
>>
>> ```
>> #include <linux/module.h>
>> #include <linux/init.h>
>>
>> struct __attribute__((packed)) test_container {
>> char padding[49];
>> struct mutex io_lock;
>> };
>>
>> static int __init alignment_init(void)
>> {
>> struct test_container cont;
>> 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 15:44:50 2025] io_lock address offset mod 4: 1
>>
>
> Thanks for sending code to illustrate your point. Unfortunately, I was not
> able to reproduce your results. Tested on x86, your test module shows no
> misalignment:
>
> [131840.349042] io_lock address offset mod 4: 0
>
> Tested on m68k I also get 0, given the patch at the top of this thread:
>
> [ 0.400000] io_lock address offset mod 4: 0
>
>>
>> As we can see, a packed struct can still force the entire mutex object
>> to an unaligned address. With an address like this, the WARN_ON_ONCE can
>> still be triggered.
>
> I don't think so. But there is something unexpected going on here -- the
> output from pahole appears to say io_lock is at offset 49, which seems to
> contradict the printk() output above.
Interesting! That contradiction is the key. It seems we're seeing different
compiler behaviors.
I'm on GCC 14.2.0 (Debian 14.2.0-19), and it appears to be strictly
respecting
the packed attribute.
So let's print something more:
```
#include <linux/module.h>
#include <linux/init.h>
struct __attribute__((packed)) test_container {
char padding[49];
struct mutex io_lock;
};
static int __init alignment_init(void)
{
struct test_container cont;
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:15:33 2025] Container base address : ff1100063570f840
[Mon Aug 25 19:15:33 2025] io_lock member address : ff1100063570f871
[Mon Aug 25 19:15:33 2025] io_lock address offset mod 4: 1
```
io_lock is exactly base + 49, resulting in misalignment.
Seems like your compiler is cleverly re-aligning the whole struct on the
stack, but we can't rely on that behavior, as it's not guaranteed across
all compilers or versions. wdyt?
next prev parent reply other threads:[~2025-08-25 11:19 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-25 2:03 [PATCH] atomic: Specify natural alignment for atomic_t Finn Thain
[not found] ` <20250825032743.80641-1-ioworker0@gmail.com>
[not found] ` <c8851682-25f1-f594-e30f-5b62e019d37b@linux-m68k.org>
[not found] ` <96ae7afc-c882-4c3d-9dea-3e2ae2789caf@linux.dev>
[not found] ` <5a44c60b-650a-1f8a-d5cb-abf9f0716817@linux-m68k.org>
[not found] ` <4e7e7292-338d-4a57-84ec-ae7427f6ad7c@linux.dev>
2025-08-25 10:49 ` Finn Thain
2025-08-25 11:19 ` Lance Yang [this message]
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
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
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=d95592ec-f51e-4d80-b633-7440b4e69944@linux.dev \
--to=lance.yang@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=fthain@linux-m68k.org \
--cc=geert@linux-m68k.org \
--cc=ioworker0@gmail.com \
--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).