Re: [PATCH] m68k: delay: Fix rounding of short udelay() and ndelay()
From: Finn Thain
Date: Sat Nov 21 2026 - 04:56:58 EST
On Fri, 20 Nov 2026, Graeme Harker wrote:
> > 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.
>
Well, the problem with 40 ns x 40000 calls (or 1.6 ms) is that such a
short interval will usually encounter no tick interrupt, and the
measurement would be erratic (if interrupts were enabled).
The measurements below were made with interrupts disabled so add 4% if you
want to account for the tick interrupt.
test_udelay(10) 10.8E-06 -> 11.8E-06
test_udelay(20) 19.7E-06 -> 21.5E-06
test_udelay(40) 37.8E-06 -> 41.1E-06
test_udelay(100) 92.3E-06 -> 99.7E-06
test_udelay(200) 192E-06 -> 198E-06
test_udelay(400) 392E-06 -> 393E-06
test_udelay(1000) 973E-06 -> 980E-06
test_ndelay(20) 401E-09 -> 612E-09
test_ndelay(40) 401E-09 -> 1.06E-06
test_ndelay(100) 2.49E-06 -> 1.17E-06
test_ndelay(200) 2.55E-06 -> 1.22E-06
test_ndelay(400) 2.75E-06 -> 1.50E-06
test_ndelay(1000) 3.30E-06 -> 2.14E-06
test_ndelay(2000) 4.24E-06 -> 3.33E-06
test_ndelay(4000) 6.07E-06 -> 5.63E-06
test_ndelay(10000) 11.6E-06 -> 12.5E-06
test_ndelay(20000) 20.8E-06 -> 23.9E-06
test_ndelay(40000) 39.2E-06 -> 46.9E-06
> > 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?
>
It logs "Calibrating delay loop... 21.55 BogoMIPS lpj=107776)".
> 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.
>
I note that the API documentation warns callers that "ndelay(), udelay()
and mdelay() may return early for several reasons". So I think I'll defer
to the arch maintainers on this one.
---
diff --git a/arch/m68k/mac/config.c b/arch/m68k/mac/config.c
index b745cf20a71f..9e3547698619 100644
--- a/arch/m68k/mac/config.c
+++ b/arch/m68k/mac/config.c
@@ -65,6 +65,90 @@ static void __init mac_sched_init(void)
via_init_clock();
}
+#define GET_VIA1_TIMER2() \
+({ \
+ unsigned long flags; \
+ local_irq_save(flags); \
+ u8 high = via1[vT2CH]; \
+ u8 low = via1[vT2CL]; \
+ torn = via1[vT2CH] != high; \
+ local_irq_restore(flags); \
+ 1 + low + (high << 8); \
+})
+
+#define DELAY_LOOP(n, arg) \
+do { \
+ int d, i, I = n / 40; \
+ u16 t1, t2; \
+ bool torn; \
+ do { t1 = GET_VIA1_TIMER2(); } while (torn); \
+ for (i = 0; i < I; ++i) { \
+ arg; arg; arg; arg; arg; arg; arg; arg; arg; arg; \
+ arg; arg; arg; arg; arg; arg; arg; arg; arg; arg; \
+ arg; arg; arg; arg; arg; arg; arg; arg; arg; arg; \
+ arg; arg; arg; arg; arg; arg; arg; arg; arg; arg; \
+ } \
+ t2 = GET_VIA1_TIMER2(); \
+ d = t1 - t2; \
+ if (d < 0) d += 0x10000; \
+ if (torn) pr_info("torn\n"); \
+ else pr_info("%10d, %20s, %10d\n", I * 40 , #arg, d); \
+} while (0)
+
+static inline void test_udelay(unsigned long t)
+{
+ volatile unsigned long vt = t;
+
+ udelay(vt);
+}
+
+static inline void test_ndelay(unsigned long t)
+{
+ volatile unsigned long vt = t;
+
+ ndelay(vt);
+}
+
+static void measure_delays(void)
+{
+ // Given ECLK is 783360 Hz, the 16 bit counter can measure up to 80 ms.
+ // Loops of 40ms of udelay() calls:
+ DELAY_LOOP(4000, test_udelay(10));
+ DELAY_LOOP(2000, test_udelay(20));
+ DELAY_LOOP(1000, test_udelay(40));
+ DELAY_LOOP( 400, test_udelay(100));
+ DELAY_LOOP( 200, test_udelay(200));
+ DELAY_LOOP( 100, test_udelay(400));
+ DELAY_LOOP( 40, test_udelay(1000));
+ // Loops of 1.6ms of ndelay() calls:
+ DELAY_LOOP(80000, test_ndelay(20));
+ DELAY_LOOP(40000, test_ndelay(40));
+ DELAY_LOOP(16000, test_ndelay(100));
+ DELAY_LOOP( 8000, test_ndelay(200));
+ DELAY_LOOP( 4000, test_ndelay(400));
+ DELAY_LOOP( 1600, test_ndelay(1000));
+ DELAY_LOOP( 800, test_ndelay(2000));
+ DELAY_LOOP( 400, test_ndelay(4000));
+ DELAY_LOOP( 160, test_ndelay(10000));
+ DELAY_LOOP( 80, test_ndelay(20000));
+ DELAY_LOOP( 40, test_ndelay(40000));
+}
+
+static int __init measure_delays_init(void)
+{
+ unsigned long flags;
+
+ via1[vACR] &= ~0x20; // free running T2 with no port B output
+
+ local_irq_save(flags);
+ measure_delays();
+ local_irq_restore(flags);
+
+ return 0;
+}
+
+subsys_initcall(measure_delays_init);
+
/*
* Parse a Macintosh-specific record in the bootinfo
*/