Re: [PATCH 7/8] vdso/align: Preserve object size through PTR_ALIGN()
From: Jack Wallner
Date: Mon Nov 30 2026 - 14:40:39 EST
Hi David,
On Mon, 30 Nov 2026, David Laight wrote:
> In a quick check (x - (x & 3)) seems to get compiled as (x & ~3).
> That might be enough to keep builtin_dynamic_object_size still
> returning a value while stopping the bloat.
I built it, as
p + (a) - 1 - (((unsigned long)p + (a) - 1) & ((a) - 1))
with the pointer arithmetic done left to right. In a userspace test it
keeps the object size. On its own it compiles to as many instructions
as the old macro with Clang (3 on x86-64, arm, arm64, riscv and
powerpc, 4 on i386), but not with GCC (5 instead of 3 on x86-64, 6
instead of 5 on i386). In the kernel build it is larger than the
current form on every target, though. I also tried the mask form
p + (-(unsigned long)p & ((a) - 1)).
First, a correction to my last mail: its Clang numbers were wrong.
allmodconfig enables RANDSTRUCT_FULL with Clang, and my before and
after build directories each generated their own seed, so the two sides
had different struct layouts. That changed code in hundreds of
functions on arm and riscv (834 and 351 grew; with the same seed, 28
each). On arm, for example, the stack canary's offset in task_struct
went from 0xc84 to 0x1f7c, which no longer fits in one load.
Below, every build is the series with Alex's changes, and the Clang
builds reuse the base build's seed. The GCC builds have no randstruct.
Function text growth over the objects that use PTR_ALIGN():
current your form -p & mask
x86-64 GCC +1394 +2330 +1941
i386 GCC +980 +1802 +1761
x86-64 Clang +695 +1686 +2485
arm64 Clang +760 +2036 +1700
powerpc Clang +1476 +1688 +1456
arm Clang +1140 +1468 +1832
riscv Clang +704 +1800 +1788
That is at most 0.07% of the text in those objects for the current
form, 0.11% for yours and 0.12% for the mask form. Both alternatives
also lose some of the new checks: 6 of the 18 on powerpc (5 of them in
p10_aes_gcm_crypt()) and one each on x86-64 GCC, arm and arm64. The
current form compiles to the same code as the old macro on its own, on
all eight compiler and target pairs I tried. So I'll keep it.
On x86, arm64 and powerpc most of the current form's growth is in
functions that gained a check; on arm a bit under half (I could not
count the checks reliably on riscv). The two x86-64 GCC functions I
could not explain last time: ismt_access() gains three FORTIFY
"field-spanning write" WARNs from its memcpy() calls (my first count
only looked for __fortify_panic()), and the copies fit, as the
changelog says. ath12k_dp_reoq_lut_setup() is no longer inlined into
ath12k_dp_setup(), which shrinks by 335 bytes, so the function text of
dp.o grows by 86 in total. On Clang the largest growth in a function
with no new check is gf128mul_lle() on arm, at 176 bytes: Clang
addresses the table as array + padding instead of through the aligned
pointer, and the stack frame grows by 80 bytes.
> Although I'd have thought that the compiler could only track the
> size when it already knows the pointer in aligned.
__builtin_dynamic_object_size() does not need to know. Its result is
the size left at p minus the padding, computed at run time. The new
KUnit test hides the starting offset with OPTIMIZER_HIDE_VAR() and
checks the object size exactly, for a heap object and for a struct on
the stack (the member size there is only bounded, since GCC and Clang
differ on it).
> > I'd rather keep that out of this series, but I can send it as a
> > separate patch if you think it is worth doing.
>
> It is definitely unrelated.
Agreed, I'll leave it out.
> Still long, maybe just:
> /* Add padding so the the compiler still knows the the size of @p */
Taken, as:
/* Add padding so the compiler still knows the size of @p */
All of this will be in v2, with the changelog's size numbers and their
explanation corrected.
Thanks,
Jack