Skip to contentSkip to conversation

RFC: ptp: add comment about register access race

PATCH In review Arnd Bergmann 4 participants 6 messages Feb 27, 2025 – Mar 4, 2025

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 RFC: ptp: add comment about register access race

From: Arnd Bergmann <[email protected]>

While reviewing a patch to the ioread64_hi_lo() helpers, I noticed that there are several PTP drivers that use multiple register reads to access a 64-bit hardware register in a racy way.

There are usually safe ways of doing this, but at least these four drivers do that. A third register read obviously makes the hardware access 50% slower. If the low word counds nanoseconds and a single register read takes on the order of 1µs, the resulting value is wrong in one of 4 million cases, which is pretty rare but common enough that it would be observed in practice.

---
Sorry I hadn't sent this out as a proper patch so far. Any ideas what we should do here?

---
 drivers/net/ethernet/xscale/ptp_ixp46x.c | 8 ++++++++
 drivers/ptp/ptp_dfl_tod.c                | 4 ++++
 drivers/ptp/ptp_ocp.c                    | 4 ++++
 drivers/ptp/ptp_pch.c                    | 8 ++++++++
 4 files changed, 24 insertions(+)
drivers/net/ethernet/xscale/ptp_ixp46x.c +8 -0
index 94203eb46e6b..e9c8589fdef0 100644
@@ -43,6 +43,10 @@ static u64 ixp_systime_read(struct ixp46x_ts_regs *regs)
 	u64 ns;
 	u32 lo, hi;
 
+	/*
+	 * Caution: Split access lo/hi, which has a small race
+	 * when the low half overflows while we read it.
+	 */
 	lo = __raw_readl(&regs->systime_lo);
 	hi = __raw_readl(&regs->systime_hi);
 
@@ -61,6 +65,10 @@ static void ixp_systime_write(struct ixp46x_ts_regs *regs, u64 ns)
 	hi = ns >> 32;
 	lo = ns & 0xffffffff;
 
+	/*
+	 * This can equally overflow when the low half of the new
+	 * nanosecond value is close to 0xffffffff.
+	 */
 	__raw_writel(lo, &regs->systime_lo);
 	__raw_writel(hi, &regs->systime_hi);
 }
drivers/ptp/ptp_dfl_tod.c +4 -0
index f699d541b360..1eed76c3a256 100644
@@ -104,6 +104,10 @@ static int coarse_adjust_tod_clock(struct dfl_tod *dt, s64 delta)
 	if (delta == 0)
 		return 0;
 
+	/*
+	 * Caution: Split access lo/hi, which has a small race
+	 * when the low half overflows while we read it.
+	 */
 	nanosec = readl(base + TOD_NANOSEC);
 	seconds_lsb = readl(base + TOD_SECONDSL);
 	seconds_msb = readl(base + TOD_SECONDSH);
drivers/ptp/ptp_ocp.c +4 -0
index b651087f426f..cdf2ebe7c9bc 100644
@@ -1229,6 +1229,10 @@ __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts,
 		sts->post_ts = ns_to_timespec64(ns - bp->ts_window_adjust);
 	}
 
+	/*
+	 * Caution: Split access to nanoseconds/seconds, which has a small
+	 * race when the low half overflows while we read it.
+	 */
 	time_ns = ioread32(&bp->reg->time_ns);
 	time_sec = ioread32(&bp->reg->time_sec);
 
drivers/ptp/ptp_pch.c +8 -0
index b8a9a54a176c..a90c206e320b 100644
@@ -147,6 +147,10 @@ static u64 pch_systime_read(struct pch_ts_regs __iomem *regs)
 {
 	u64 ns;
 
+	/*
+	 * Caution: Split access lo/hi, which has a small race
+	 * when the low half overflows while we read it.
+	 */
 	ns = ioread64_lo_hi(&regs->systime_lo);
 
 	return ns << TICKS_NS_SHIFT;
@@ -154,6 +158,10 @@ static u64 pch_systime_read(struct pch_ts_regs __iomem *regs)
 
 static void pch_systime_write(struct pch_ts_regs __iomem *regs, u64 ns)
 {
+	/*
+	 * This can equally overflow when the low half of the new
+	 * nanosecond value is close to 0xffffffff.
+	 */
 	iowrite64_lo_hi(ns >> TICKS_NS_SHIFT, &regs->systime_lo);
 }
 
Signature · 1 line
2.39.5
#1 lore raw
Quoted 12 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

While reviewing a patch to the ioread64_hi_lo() helpers, I noticed that there are several PTP drivers that use multiple register reads to access a 64-bit hardware register in a racy way.

There are usually safe ways of doing this, but at least these four drivers do that. A third register read obviously makes the hardware access 50% slower. If the low word counds nanoseconds and a single register read takes on the order of 1µs, the resulting value is wrong in one of 4 million cases, which is pretty rare but common enough that it would be observed in practice.

...

Quoted 2 lines

Sorry I hadn't sent this out as a proper patch so far. Any ideas what we should do here?

Actually this reminds me one of the discussion where it was some interesting HW design that latches the value on the first read of _low_ part (IIRC), but I might be mistaken with the details.

That said, it's from HW to HW, it might be race-less in some cases.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#2 lore raw
Quoted 7 lines · Andy Shevchenko
Quoted 6 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

While reviewing a patch to the ioread64_hi_lo() helpers, I noticed that there are several PTP drivers that use multiple register reads to access a 64-bit hardware register in a racy way.

Quoted 6 lines
Quoted 6 lines

There are usually safe ways of doing this, but at least these four drivers do that. A third register read obviously makes the hardware access 50% slower. If the low word counds nanoseconds and a single register read takes on the order of 1µs, the resulting value is wrong in one of 4 million cases, which is pretty rare but common enough that it would be observed in practice.

If the hardware does NOT latch the registers together, then the driver must do:

1. hi1 = read hi
2. low = read lo
3. hi2 = read h1
4. if (hi2 == hi1 return (hi1 << 32) | low;
5. goto step 1.

This for correctness, and correctness > performance.

Quoted 2 lines
Quoted 2 lines

Sorry I hadn't sent this out as a proper patch so far. Any ideas what we should do here?

Need to have driver authors check the data sheet because ...

Quoted 5 lines

Actually this reminds me one of the discussion where it was some interesting HW design that latches the value on the first read of _low_ part (IIRC), but I might be mistaken with the details.

That said, it's from HW to HW, it might be race-less in some cases.

... of this.

Thanks,
Richard

#3 lore raw
Quoted 17 lines · Richard Cochran
Quoted 7 lines · Andy Shevchenko
Quoted 6 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

While reviewing a patch to the ioread64_hi_lo() helpers, I noticed that there are several PTP drivers that use multiple register reads to access a 64-bit hardware register in a racy way.

Quoted 6 lines
Quoted 6 lines

There are usually safe ways of doing this, but at least these four drivers do that. A third register read obviously makes the hardware access 50% slower. If the low word counds nanoseconds and a single register read takes on the order of 1µs, the resulting value is wrong in one of 4 million cases, which is pretty rare but common enough that it would be observed in practice.

If the hardware does NOT latch the registers together, then the driver must do:

Quoted 7 lines

1. hi1 = read hi
2. low = read lo
3. hi2 = read h1
4. if (hi2 == hi1 return (hi1 << 32) | low;
5. goto step 1.

This for correctness, and correctness > performance.

Right.

Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

Sorry I hadn't sent this out as a proper patch so far. Any ideas what we should do here?

Need to have driver authors check the data sheet because ...

Quoted 7 lines
Quoted 5 lines

Actually this reminds me one of the discussion where it was some interesting HW design that latches the value on the first read of _low_ part (IIRC), but I might be mistaken with the details.

That said, it's from HW to HW, it might be race-less in some cases.

... of this.

Perhaps it's still good to have a comment, but rephrase it that the code is questionable depending on the HW behaviour that needs to be checked.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#4 lore raw
Quoted 2 lines · Andy Shevchenko

Perhaps it's still good to have a comment, but rephrase it that the code is questionable depending on the HW behaviour that needs to be checked.

IIRC both ixp4xx and the PCH are the same design and latch high reg on read of low.

Thanks,
Richard

#5 lore raw
Quoted 6 lines · Richard Cochran
Quoted 2 lines · Andy Shevchenko

Perhaps it's still good to have a comment, but rephrase it that the code is questionable depending on the HW behaviour that needs to be checked.

IIRC both ixp4xx and the PCH are the same design and latch high reg on read of low.

Ok, if that's a common thing, let's assume that the drivers are all correct for the respective hardware and discard my patch.

With the patch I have queued up in the asm-generic tree, the behavior changes so ioread64_lo_hi() no longer gets turned into a single 64-bit access, and that is then more likely to be correct for both 32-bit and 64-bit architectures in case the device doesn't actually implement 64-bit transactions but does correctly latch the contents.

Arnd