mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-05-16 06:41:39 -04:00
rust: time: make ClockSource unsafe trait
Mark the ClockSource trait as unsafe and document its safety requirements. Specifically, implementers must guarantee that their `ktime_get()` implementation returns a value in the inclusive range [0, KTIME_MAX]. Update all existing implementations to use `unsafe impl` with corresponding safety comments. Note that there could be potential users of a customized clock source [1] so we don't seal the trait. Link: https://lore.kernel.org/rust-for-linux/Z9xb1r1x5tOzAIZT@boqun-archlinux/ [1] Suggested-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Link: https://msgid.link/20250630131011.405219-1-fujita.tomonori@gmail.com [ Change range expressions in docs. - Andreas ] Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
This commit is contained in:
committed by
Andreas Hindborg
parent
c51866f65b
commit
67b598db7e
@@ -60,7 +60,13 @@ pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies {
|
||||
/// cases the user of the clock has to decide which clock is best suited for the
|
||||
/// purpose. In most scenarios clock [`Monotonic`] is the best choice as it
|
||||
/// provides a accurate monotonic notion of time (leap second smearing ignored).
|
||||
pub trait ClockSource {
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Implementers must ensure that `ktime_get()` returns a value in the inclusive range
|
||||
/// `0..=KTIME_MAX` (i.e., greater than or equal to 0 and less than or equal to
|
||||
/// `KTIME_MAX`, where `KTIME_MAX` equals `i64::MAX`).
|
||||
pub unsafe trait ClockSource {
|
||||
/// The kernel clock ID associated with this clock source.
|
||||
///
|
||||
/// This constant corresponds to the C side `clockid_t` value.
|
||||
@@ -68,7 +74,7 @@ pub trait ClockSource {
|
||||
|
||||
/// Get the current time from the clock source.
|
||||
///
|
||||
/// The function must return a value in the range from 0 to `KTIME_MAX`.
|
||||
/// The function must return a value in the range `0..=KTIME_MAX`.
|
||||
fn ktime_get() -> bindings::ktime_t;
|
||||
}
|
||||
|
||||
@@ -85,7 +91,9 @@ pub trait ClockSource {
|
||||
/// count time that the system is suspended.
|
||||
pub struct Monotonic;
|
||||
|
||||
impl ClockSource for Monotonic {
|
||||
// SAFETY: The kernel's `ktime_get()` is guaranteed to return a value
|
||||
// in `0..=KTIME_MAX`.
|
||||
unsafe impl ClockSource for Monotonic {
|
||||
const ID: bindings::clockid_t = bindings::CLOCK_MONOTONIC as bindings::clockid_t;
|
||||
|
||||
fn ktime_get() -> bindings::ktime_t {
|
||||
@@ -110,7 +118,9 @@ fn ktime_get() -> bindings::ktime_t {
|
||||
/// the clock will experience discontinuity around leap second adjustment.
|
||||
pub struct RealTime;
|
||||
|
||||
impl ClockSource for RealTime {
|
||||
// SAFETY: The kernel's `ktime_get_real()` is guaranteed to return a value
|
||||
// in `0..=KTIME_MAX`.
|
||||
unsafe impl ClockSource for RealTime {
|
||||
const ID: bindings::clockid_t = bindings::CLOCK_REALTIME as bindings::clockid_t;
|
||||
|
||||
fn ktime_get() -> bindings::ktime_t {
|
||||
@@ -128,7 +138,9 @@ fn ktime_get() -> bindings::ktime_t {
|
||||
/// discontinuities if the time is changed using settimeofday(2) or similar.
|
||||
pub struct BootTime;
|
||||
|
||||
impl ClockSource for BootTime {
|
||||
// SAFETY: The kernel's `ktime_get_boottime()` is guaranteed to return a value
|
||||
// in `0..=KTIME_MAX`.
|
||||
unsafe impl ClockSource for BootTime {
|
||||
const ID: bindings::clockid_t = bindings::CLOCK_BOOTTIME as bindings::clockid_t;
|
||||
|
||||
fn ktime_get() -> bindings::ktime_t {
|
||||
@@ -150,7 +162,9 @@ fn ktime_get() -> bindings::ktime_t {
|
||||
/// The acronym TAI refers to International Atomic Time.
|
||||
pub struct Tai;
|
||||
|
||||
impl ClockSource for Tai {
|
||||
// SAFETY: The kernel's `ktime_get_clocktai()` is guaranteed to return a value
|
||||
// in `0..=KTIME_MAX`.
|
||||
unsafe impl ClockSource for Tai {
|
||||
const ID: bindings::clockid_t = bindings::CLOCK_TAI as bindings::clockid_t;
|
||||
|
||||
fn ktime_get() -> bindings::ktime_t {
|
||||
|
||||
Reference in New Issue
Block a user