Skip to content

clocksource/drivers/bcm2835_timer: Detect missed compare matches - #7768

Merged
pelwell merged 1 commit into
raspberrypi:rpi-6.12.yfrom
pelwell:is7764_612
Nov 18, 2026
Merged

pelwell merged 1 commit into
raspberrypi:rpi-6.12.yfrom
pelwell:is7764_612

Conversation

@pelwell

@pelwell pelwell commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

The BCM2835 system timer compare registers use an equality match, so the interrupt only fires when the low 32 bits of the free-running 1MHz counter equal the programmed value. bcm2835_time_set_next_event() reads the counter, adds the delta and writes the result to the compare register, always returning success. If the counter has already passed the target by the time the write lands - easily done with a min_delta of 15us, slow peripheral accesses and an unmaskable FIQ such as the dwc_otg one - the event is not delivered until the counter wraps, 2^32us (~4295s) later.

On an otherwise quiet system nothing updates the timekeeper during that time, and once the clocksource delta wraps a whole counter period is lost, causing the clock to fall ~4295s behind.

Read the counter back after programming the compare and return -ETIME if the target has already been reached, so that the clockevents core retries (and increases min_delta_ns if necessary).

The check treats the difference as signed, so reduce max_delta to 0x7fffffff; otherwise a delta above 2^31 (e.g. a distant hrtimer clamped to max_delta_ns) would be reported as a false miss.

Assisted-by: Claude

The BCM2835 system timer compare registers use an equality match, so
the interrupt only fires when the low 32 bits of the free-running 1MHz
counter equal the programmed value. bcm2835_time_set_next_event() reads
the counter, adds the delta and writes the result to the compare
register, always returning success. If the counter has already passed
the target by the time the write lands - easily done with a min_delta
of 15us, slow peripheral accesses and an unmaskable FIQ such as the
dwc_otg one - the event is not delivered until the counter wraps,
2^32us (~4295s) later.

On an otherwise quiet system nothing updates the timekeeper during that
time, and once the clocksource delta wraps a whole counter period is
lost, causing the clock to fall ~4295s behind.

Read the counter back after programming the compare and return -ETIME
if the target has already been reached, so that the clockevents core
retries (and increases min_delta_ns if necessary).

The check treats the difference as signed, so reduce max_delta to
0x7fffffff; otherwise a delta above 2^31 (e.g. a distant hrtimer
clamped to max_delta_ns) would be reported as a false miss.

Assisted-by: Claude
Signed-off-by: Phil Elwell <phil@raspberrypi.com>
@paul-1

paul-1 commented Nov 18, 2026

Copy link
Copy Markdown

Just to add a use case to this.

I spent a weekend troubleshooting this error with Claude. My original trigger of the timer wrap was using wiringPi to monitor 4 GPIO's (4 high priority scheduled threads SCHED_RR/55). The same change here was recommended, but in trying to validate the change really fixed the problem, some debug code was added to the code to see if the wrap was being detected. But the extra code changed the timing just enough I could never get the wrap to occur.

One other thing that seemed to impact this was the cpu governor. If ondemand was used, the fault would happen (There was a very high rate of CPU frequency changes). If set to performance, even the unpatched kernel would not hang.

@pelwell
pelwell merged commit dcc3a57 into raspberrypi:rpi-6.12.y Nov 18, 2026
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants