Re: [RFC v2 2/3] atomic: Specify alignment for atomic_t and atomic64_t
From: Finn Thain
Date: Tue Sep 30 2025 - 21:46:37 EST
On Tue, 30 Sep 2025, Geert Uytterhoeven wrote:
> > To silence the misalignment WARN from CONFIG_DEBUG_ATOMIC, for 64-bit
> > atomic operations, for my small m68k .config, it was also necesary to
> > increase ARCH_SLAB_MINALIGN to 8. However, I'm not advocating a
>
> Probably ARCH_SLAB_MINALIGN should be 4 on m68k. Somehow I thought that
> was already the case, but it is __alignof__(unsigned long long) = 2.
>
I agree -- setting ARCH_SLAB_MINALIGN to 4 would be better style, and may
avoid surprises in future. Right now that won't have any effect because
that value gets increased to sizeof(void *) by calculate_alignment() and
gets increased to ARCH_KMALLOC_MINALIGN or ARCH_DMA_MINALIGN by
__kmalloc_minalign().
> > ARCH_SLAB_MINALIGN increase, as that wastes memory. I think it might be
> > more useful to limit the alignment test for CONFIG_DEBUG_ATOMIC, as
> > follows.
>
> Did you check what would be the actual impact of increasing it to 4 or 8?
>
> > --- a/include/linux/instrumented.h
> > +++ b/include/linux/instrumented.h
> > @@ -68,7 +68,7 @@ static __always_inline void instrument_atomic_read(const volatile void *v, size_
> > {
> > kasan_check_read(v, size);
> > kcsan_check_atomic_read(v, size);
> > - WARN_ON_ONCE(IS_ENABLED(CONFIG_DEBUG_ATOMIC) && ((unsigned long)v & (size - 1)));
> > + WARN_ON_ONCE(IS_ENABLED(CONFIG_DEBUG_ATOMIC) && ((unsigned long)v & (size - 1) & 3));
>
> I'd make that an arch-overridable define instead of hardcoded 3.
>
How about (sizeof(atomic_long_t) - 1)?
Can you comment on this, Peter?