Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove most panics from timer-related operations #1749

Merged
merged 3 commits into from
Apr 17, 2024

Conversation

cbiffle
Copy link
Collaborator

@cbiffle cbiffle commented Apr 15, 2024

See the commits for more detail, but in brief, this removes a panic from userlib::hl::sleep_for and then introduces a set_timer_relative operation for doing the common timer set operation in a way that won't panic.

@cbiffle cbiffle force-pushed the cbiffle/no-panic-timestamp-ops branch 4 times, most recently from 93caf7c to ed8ed3b Compare April 15, 2024 19:35
@cbiffle cbiffle requested a review from hawkw April 15, 2024 19:37
@cbiffle cbiffle force-pushed the cbiffle/no-panic-timestamp-ops branch from ed8ed3b to 94fb39b Compare April 15, 2024 19:46
Copy link
Member

@hawkw hawkw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me --- I commented on a couple very minor nits, but they're not important

drv/gimlet-seq-server/src/main.rs Outdated Show resolved Hide resolved
drv/meanwell/src/main.rs Outdated Show resolved Hide resolved
task/jefe/src/main.rs Outdated Show resolved Hide resolved
drv/user-leds/src/main.rs Outdated Show resolved Hide resolved
drv/user-leds/src/main.rs Outdated Show resolved Hide resolved
task/jefe/src/main.rs Outdated Show resolved Hide resolved
task/host-sp-comms/src/main.rs Show resolved Hide resolved
task/monorail-server/src/bsp/sidecar_bcd.rs Show resolved Hide resolved
task/power/src/main.rs Show resolved Hide resolved
@aapoalas aapoalas mentioned this pull request Apr 16, 2024
Previously sleep_for was panicking if the provided timeout plus the
current kernel timestamp overflowed a u64. This inserted a subtle and
difficult-to-remove panic site in many programs.

This changes it to use saturating arithmetic instead. Saturating
arithmetic is still more expensive than wrapping arithmetic (on u64s, at
least, our processors have hardware saturating arithmetic but only up to
u32), but it is far cheaper than a panic.

This means that a program that provides a very, very large timeout will
sleep until "the end of time" (the 64-bit timestamp rollover in 584.5
million years) instead of panicking. Given that the overflow constraint
wasn't previously documented in the API, and given that our timeout
values tend to be small and constant, this seems to me like an
acceptable compromise to reduce text size.
Previously a race condition between the RoT IRQ event and the timer
event could cause this to detect a timeout immediately on the _next_
time it was called, due to a notification being left pending. This
is because the code assumed that the notification for the timer was
sufficient to give up on the remote device; that's not actually the
case.

This changes the code to look at the clock before making decisions about
elapsed time.
@cbiffle cbiffle force-pushed the cbiffle/no-panic-timestamp-ops branch 2 times, most recently from 8fd5ae6 to ea3ead4 Compare April 17, 2024 18:55
This provides a convenient shorthand for the common pattern of reading
the timer, adding a small increment, and writing it back. By limiting
the range of the interval type, I was able to avoid overflows in
realistic scenarios (system uptime < 584 million years), and thus use
non-checked arithmetic to eliminate a common panic site.

I've gone through and fixed all the code where this was obviously the
right thing to use, and left some other code intact if I couldn't
convince myself I wouldn't break it.

In some cases I've just switched the code to use saturating adds, which
are not free (at least for 64-bit integers), but are cheaper than
panicking.
@cbiffle cbiffle force-pushed the cbiffle/no-panic-timestamp-ops branch from ea3ead4 to c849639 Compare April 17, 2024 18:58
@cbiffle cbiffle enabled auto-merge (rebase) April 17, 2024 19:06
@cbiffle cbiffle merged commit 875a07a into master Apr 17, 2024
104 checks passed
@cbiffle cbiffle deleted the cbiffle/no-panic-timestamp-ops branch April 17, 2024 19:06
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.

None yet

3 participants