[PATCH v4 1/4] vdso/math64: Use OPTIMIZER_HIDE_VAR() in __iter_div_u64_rem()

From: Zhan Xusheng

Date: Tue Sep 01 2026 - 23:39:15 EST


The loop subtracts because the quotient is tiny at every caller, and the
asm() keeps the compiler from turning it into a division instead. It
offers a memory alternative for the value:

asm("" : "+rm"(dividend));

clang takes that alternative and spills the value inside the loop, on
64-bit as well as 32-bit. OPTIMIZER_HIDE_VAR() is the register-only form
of the same barrier and is what the rest of the kernel uses for this.

The helper sits on the clock_gettime() path through vdso_set_timespec(),
so this is measurable: x86 vDSO built with clang 18 loses 64 bytes of
vdso64 text and 112 bytes of vdso32. gcc 13 emits the same code either
way. Neither compiler turns the loop into a division with the new form,
checked at both widths.

Signed-off-by: Zhan Xusheng <zhanxusheng@xxxxxxxxxx>
---
include/vdso/math64.h | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/include/vdso/math64.h b/include/vdso/math64.h
index 22ae212f8b28..83ebac2e5c1b 100644
--- a/include/vdso/math64.h
+++ b/include/vdso/math64.h
@@ -8,9 +8,11 @@ __iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder)
u32 ret = 0;

while (dividend >= divisor) {
- /* The following asm() prevents the compiler from
- optimising this loop into a modulo operation. */
- asm("" : "+rm"(dividend));
+ /*
+ * Prevent the compiler from optimising this loop into a
+ * modulo operation.
+ */
+ OPTIMIZER_HIDE_VAR(dividend);

dividend -= divisor;
ret++;
--
2.43.0