Hi Geert,
On 2025/9/1 16:45, Geert Uytterhoeven 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
Right. That's definitely not how packed structs should be used ;)
MMIO register layouts, and must not contain control data for critical sections.
Unfortunately, this patten was found in an in-tree driver, as reported[1] by kernel test robot, and there might be other undiscovered instances ...
[1] https://lore.kernel.org/oe-kbuild-all/202508240539.ARmC1Umu-lkp@intel.com
Cheers, Lance