Repository navigation
clocksource/drivers/bcm2835_timer: Detect missed compare matches - #7768
Merged
Merged
Conversation
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>
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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