Skip to contentSkip to conversation

rust: time: rename ClockSource trait to ClockId

PATCH Applied FUJITA Tomonori 3 participants 7 messages Jul 22 – Sep 29

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: time: rename ClockSource trait to ClockId

From: FUJITA Tomonori <[email protected]>

The ClockSource trait has nothing to do with the C struct clocksource in include/linux/clocksource.h, which abstracts the hardware counter used as a source of time for the majority of the clockids. The trait instead carries a clockid_t ID, i.e. one of the IDs of "the various system clocks (for POSIX.1b interval timers)" as described in include/uapi/linux/time.h (CLOCK_MONOTONIC, CLOCK_REALTIME, ...). It thus plays the role of a clockid_t, and the ClockSource name overlaps confusingly with the C clocksource concept when reading across C and Rust code.

Rename the trait to ClockId to reflect that it represents a clockid_t. This is a pure rename; there is no functional change.

---
 rust/kernel/time.rs         | 34 +++++++--------
 rust/kernel/time/hrtimer.rs | 82 ++++++++++++++++++-------------------
 2 files changed, 58 insertions(+), 58 deletions(-)
rust/kernel/time.rs +17 -17
index 363e93cbb139..4fecab6692b6 100644
@@ -53,9 +53,9 @@ pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies {
     unsafe { bindings::__msecs_to_jiffies(msecs) }
 }
 
-/// Trait for clock sources.
+/// Trait for kernel clock identifiers.
 ///
-/// Selection of the clock source depends on the use case. In some cases the usage of a
+/// Selection of the clock depends on the use case. In some cases the usage of a
 /// particular clock is mandatory, e.g. in network protocols, filesystems. In other
 /// 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
@@ -66,13 +66,13 @@ pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies {
 /// 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.
+pub unsafe trait ClockId {
+    /// The kernel clock ID associated with this clock.
     ///
     /// This constant corresponds to the C side `clockid_t` value.
     const ID: bindings::clockid_t;
 
-    /// Get the current time from the clock source.
+    /// Get the current time from the clock.
     ///
     /// The function must return a value in the range `0..=KTIME_MAX`.
     fn ktime_get() -> bindings::ktime_t;
@@ -93,7 +93,7 @@ pub unsafe trait ClockSource {
 
 // SAFETY: The kernel's `ktime_get()` is guaranteed to return a value
 // in `0..=KTIME_MAX`.
-unsafe impl ClockSource for Monotonic {
+unsafe impl ClockId for Monotonic {
     const ID: bindings::clockid_t = bindings::CLOCK_MONOTONIC as bindings::clockid_t;
 
     fn ktime_get() -> bindings::ktime_t {
@@ -120,7 +120,7 @@ fn ktime_get() -> bindings::ktime_t {
 
 // SAFETY: The kernel's `ktime_get_real()` is guaranteed to return a value
 // in `0..=KTIME_MAX`.
-unsafe impl ClockSource for RealTime {
+unsafe impl ClockId for RealTime {
     const ID: bindings::clockid_t = bindings::CLOCK_REALTIME as bindings::clockid_t;
 
     fn ktime_get() -> bindings::ktime_t {
@@ -140,7 +140,7 @@ fn ktime_get() -> bindings::ktime_t {
 
 // SAFETY: The kernel's `ktime_get_boottime()` is guaranteed to return a value
 // in `0..=KTIME_MAX`.
-unsafe impl ClockSource for BootTime {
+unsafe impl ClockId for BootTime {
     const ID: bindings::clockid_t = bindings::CLOCK_BOOTTIME as bindings::clockid_t;
 
     fn ktime_get() -> bindings::ktime_t {
@@ -164,7 +164,7 @@ fn ktime_get() -> bindings::ktime_t {
 
 // SAFETY: The kernel's `ktime_get_clocktai()` is guaranteed to return a value
 // in `0..=KTIME_MAX`.
-unsafe impl ClockSource for Tai {
+unsafe impl ClockId for Tai {
     const ID: bindings::clockid_t = bindings::CLOCK_TAI as bindings::clockid_t;
 
     fn ktime_get() -> bindings::ktime_t {
@@ -180,24 +180,24 @@ fn ktime_get() -> bindings::ktime_t {
 /// The `inner` value is in the range from 0 to `KTIME_MAX`.
 #[repr(transparent)]
 #[derive(PartialEq, PartialOrd, Eq, Ord)]
-pub struct Instant<C: ClockSource> {
+pub struct Instant<C: ClockId> {
     inner: bindings::ktime_t,
     _c: PhantomData<C>,
 }
 
-impl<C: ClockSource> Clone for Instant<C> {
+impl<C: ClockId> Clone for Instant<C> {
     fn clone(&self) -> Self {
         *self
     }
 }
 
-impl<C: ClockSource> Copy for Instant<C> {}
+impl<C: ClockId> Copy for Instant<C> {}
 
-impl<C: ClockSource> Instant<C> {
+impl<C: ClockId> Instant<C> {
     /// Get the current time from the clock source.
     #[inline]
     pub fn now() -> Self {
-        // INVARIANT: The `ClockSource::ktime_get()` function returns a value in the range
+        // INVARIANT: The `ClockId::ktime_get()` function returns a value in the range
         // from 0 to `KTIME_MAX`.
         Self {
             inner: C::ktime_get(),
@@ -239,7 +239,7 @@ pub(crate) unsafe fn from_ktime(ktime: bindings::ktime_t) -> Self {
     }
 }
 
-impl<C: ClockSource> ops::Sub for Instant<C> {
+impl<C: ClockId> ops::Sub for Instant<C> {
     type Output = Delta;
 
     // By the type invariant, it never overflows.
@@ -251,7 +251,7 @@ fn sub(self, other: Instant<C>) -> Delta {
     }
 }
 
-impl<T: ClockSource> ops::Add<Delta> for Instant<T> {
+impl<T: ClockId> ops::Add<Delta> for Instant<T> {
     type Output = Self;
 
     #[inline]
@@ -271,7 +271,7 @@ fn add(self, rhs: Delta) -> Self::Output {
     }
 }
 
-impl<T: ClockSource> ops::Sub<Delta> for Instant<T> {
+impl<T: ClockId> ops::Sub<Delta> for Instant<T> {
     type Output = Self;
 
     #[inline]
rust/kernel/time/hrtimer.rs +41 -41

189 lines not on this page · raw message

base-commit: 7059bdf4f04a3e14f4fafb3ac35fdca913e3e21a

Signature · 1 line
2.43.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

The ClockSource trait has nothing to do with the C struct clocksource in include/linux/clocksource.h, which abstracts the hardware counter used as a source of time for the majority of the clockids. The trait instead carries a clockid_t ID, i.e. one of the IDs of "the various system clocks (for POSIX.1b interval timers)" as described in include/uapi/linux/time.h (CLOCK_MONOTONIC, CLOCK_REALTIME, ...). It thus plays the role of a clockid_t, and the ClockSource name overlaps confusingly with the C clocksource concept when reading across C and Rust code.

Quoted 6 lines

Rename the trait to ClockId to reflect that it represents a clockid_t. This is a pure rename; there is no functional change.

Makes sense to me.

Best regards,
Andreas Hindborg

#2 lore raw
Quoted 17 lines · On Wed, 05 Aug 2026 13:01:17 +0200 Andreas Hindborg <[email protected]>…
Quoted 15 lines · FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

The ClockSource trait has nothing to do with the C struct clocksource in include/linux/clocksource.h, which abstracts the hardware counter used as a source of time for the majority of the clockids. The trait instead carries a clockid_t ID, i.e. one of the IDs of "the various system clocks (for POSIX.1b interval timers)" as described in include/uapi/linux/time.h (CLOCK_MONOTONIC, CLOCK_REALTIME, ...). It thus plays the role of a clockid_t, and the ClockSource name overlaps confusingly with the C clocksource concept when reading across C and Rust code.

Rename the trait to ClockId to reflect that it represents a clockid_t. This is a pure rename; there is no functional change.

Quoted 7 lines
Quoted 3 lines

Makes sense to me.

Thanks!

Miguel, Andreas, can we merge this via rust-next?

#3 lore raw
Quoted 4 lines · FUJITA Tomonori

Thanks!

Miguel, Andreas, can we merge this via rust-next?

If Andreas is OK with it, we can try, yeah -- he was mentioning to me earlier today about a few others.

Cheers,
Miguel

#4 lore raw
Quoted 8 lines · On Mon, 10 Aug 2026 15:32:44 +0200 Miguel Ojeda…
Quoted 4 lines · FUJITA Tomonori

Thanks!

Miguel, Andreas, can we merge this via rust-next?

If Andreas is OK with it, we can try, yeah -- he was mentioning to me earlier today about a few others.

Andreas, can we get it into the next merge window via rust-next?

maintainer
#5 lore raw
Quoted 5 lines · FUJITA Tomonori
Quoted 2 lines · On Mon, 10 Aug 2026 15:32:44 +0200 Miguel Ojeda…
Quoted 1 line · FUJITA Tomonori
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Thanks!

Miguel, Andreas, can we merge this via rust-next?

If Andreas is OK with it, we can try, yeah -- he was mentioning to me earlier today about a few others.

Andreas, can we get it into the next merge window via rust-next?

Yes, I plan to stage it for the next merge window. It goes to (rust-)timekeeping-next and linux-next picks it up from there. From there it goes to rust-next via PR when we get to ~rc5.

Would be nice with more review tags though.

Best regards,
Andreas Hindborg

maintainer Applied
#6 lore raw
In reply to FUJITA Tomonori
Quoted 11 lines · FUJITA Tomonori

The ClockSource trait has nothing to do with the C struct clocksource in include/linux/clocksource.h, which abstracts the hardware counter used as a source of time for the majority of the clockids. The trait instead carries a clockid_t ID, i.e. one of the IDs of "the various system clocks (for POSIX.1b interval timers)" as described in include/uapi/linux/time.h (CLOCK_MONOTONIC, CLOCK_REALTIME, ...). It thus plays the role of a clockid_t, and the ClockSource name overlaps confusingly with the C clocksource concept when reading across C and Rust code.

[...]

Applied, thanks!

[1/1] rust: time: rename ClockSource trait to ClockId

      commit: 6a8e2c63f2fece3828ac09e4e434666de842acfe

Best regards,

Signature · 1 line
Andreas Hindborg <[email protected]>