Re: [PATCH] m68k: delay: Fix rounding of short udelay() and ndelay()
From: Graeme Harker
Date: Fri Nov 20 2026 - 11:43:32 EST
Hi Finn,
Thanks for running it again, and with both interrupt settings.
> test_ndelay(40) 191E-09 -> 17.0E-09
I think this one has wrapped the timer as well. 400000 calls only fit
in T2's 83.7 ms if each takes less than 209 ns, and 17 ns is less than
one clock on the LC040. With one wrap it would be 226 ns, which is
longer than before, as ndelay(400) now is. 40000 calls should show it.
> test_ndelay(40000) 38.7E-06 -> 46.5E-06
> test_ndelay(400000) 366E-06 -> 456E-06
These show a real problem with the patch: long ndelay()s now run about
15% long. The 5 I took from x86 is 2^32 / 10^9 rounded up, but
2^32 / 10^9 is 4.295, so every ndelay() asks for 16.4% more loops than
it should. Combined with your udelay(1000) of 979 us (2% short), that
predicts 453 us for ndelay(400000), and you measured 456 us. The old
code was short instead, from rounding loops_per_jiffy down to a
multiple of 2048.
I can't explain the old ndelay(4000) of 6.03 us without knowing
loops_per_jiffy. What does your machine calibrate to?
I can see three ways to go, and I'd like to know which you and Geert
would prefer:
1. Keep 5, as x86 does. Delays are never short, but up to 16% long.
2. Use a closer constant, e.g. __xdelay(nsec * 4 + ((nsec * 1209) >> 12)),
which is 4.29517, within 0.005% of 2^32 / 10^9 and still never
short. It costs another multiply and a shift on every call, and the
second term overflows for delays above 3.5 ms, which ndelay() should
not be asked for anyway.
3. Leave ndelay() as it is and fix only udelay().
Option 2 seems the most accurate, but ndelay() is mostly used for short
delays, and for those the call overhead you measured matters more than
the constant.
Graeme