-
Notifications
You must be signed in to change notification settings - Fork 1.5k
sched/hrtimer: Fix functional correctness issues. #18131
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
base: master
Are you sure you want to change the base?
Conversation
This commit fixed the functional correctness issues in tick mode and non-tickless alarm mode. - In tick mode, we do not need reprogram the timer. - In non-tickless alarm mode, the up_timer_start receive the relative time as the parameter. Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
9f8b1ef to
7059bf8
Compare
Since the hrtimer->expired is not monotonic, it can not be used as the version. This commit added the ownership encoding to ensure the invariant that the cancelled the timer can not modify the hrtimer again. Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
On 32-bit platform with 64-bit pointer, read/write to the `g_hrtimer_running` is not atomic, we should protect it with the seqcount to ensure the value we read is correct. Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit added hrtimer_wait to simplify the wait. Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit move the hrtimer_gettime to clock_systime_nsec to improve the code reusability. Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit added hrtimer_gettime API to support the query of the rest of the delay time of the hrtimer in nanoseconds. Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit simplified the hrtimer_cancel. Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit fixed the functional correctness issue in hrtimer_start by adding HRTIMER_MAX_DELAY fr the HRTIMER_MODE_REL. Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
7059bf8 to
533e2e6
Compare
acassis
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Fix-Point thanks for these modifications, but I think you need to update the Documentation https://nuttx.apache.org/docs/latest/reference/os/time_clock.html#high-resolution-timer-interfaces to reflect the this you are doing here. Otherwise users will end up with an incorrect Documentation
|
@wangchdo Please help to review this PR |
Summary
This PR is part of the #17675. It introduces a series of fixes and improvements to the high-resolution timer (hrtimer) subsystem, primarily focusing on functional correctness, synchronization in SMP environments, and API enhancements.
The changes were necessary to address several issues:
g_hrtimer_runningarray were not atomic, potentially leading to data races. This is fixed by replacing the spinlock with a sequence lock (seqcount) to protect concurrent access.hrtimer->expiredas a version number was flawed as it's not monotonic. The new implementation introduces an ownership encoding scheme (using the LSB of the pointer ing_hrtimer_running) to enforce the invariant that a cancelled timer cannot be modified again, ensuring correct behavior during concurrent cancellation and callback execution.hrtimer_gettime()API is added to allow querying the remaining delay of a timer in nanoseconds. For code reusability, the internalhrtimer_gettime()helper is moved to the public interface asclock_systime_nsec().hrtimer_startare capped atHRTIMER_MAX_DELAYto prevent overflow, and simplifies the cancellation logic.hrtimer_cancelandhrtimer_cancel_syncfunctions are refactored to have clearer semantics (asynchronous vs. synchronous cancellation) and a helper functionhrtimer_waitis introduced to consolidate the waiting logic.The main modifications include:
clock_systime_nsec()inline function toclock.h.HRTIMER_MAX_DELAYand updating documentation inhrtimer.h.hrtimer_gettime()API prototype.g_hrtimer_lock) in the internal header and implementation files.hrtimer_gettime.c.hrtimer_start,hrtimer_cancel,hrtimer_process, and related functions to use the new synchronization and state management mechanisms.Impact
hrtimer_cancelandhrtimer_cancel_syncare now more explicitly documented.hrtimer_cancelprovides limited ownership (allows restarting but not freeing), whilehrtimer_cancel_syncprovides full ownership (allows safe deallocation). Users must choose the appropriate function based on their needs to avoid concurrency errors.hrtimer_gettime(FAR hrtimer_t *timer)is available to query the time until a timer's next expiration in nanoseconds.clock_systime_nsec()is available for other parts of the system to get the current system time in nanoseconds.CMakeLists.txt,Make.defs) are updated to include the newhrtimer_gettime.csource file.hrtimer_cancel,hrtimer_cancel_sync,hrtimer_start) has been updated to reflect the new behavior, assumptions, and return values. This provides clearer guidance for developers.hrtimer_cancelnow has an additional meaning: a value > 0 indicates the timer callback is running. Code that only checked for negative values (errors) might need adjustment if it intends to handle the "running" case. The internal locking mechanism change is transparent to the API but ensures correct concurrent operation.Test
Tested on
rv-virt:smp,ostestpassed.