Skip to content

Commit 373827f

Browse files
committed
Merge tag 'rust-timekeeping-for-v6.16-v2' of https://github.com/Rust-for-Linux/linux into rust-next
Pull timekeeping updates from Andreas Hindborg: - Morph the Rust hrtimer subsystem into the Rust timekeeping subsystem, covering delay, sleep, timekeeping, timers. This new subsystem has all the relevant timekeeping C maintainers listed in the entry. - Replace 'Ktime' with 'Delta' and 'Instant' types to represent a duration of time and a point in time. - Temporarily add 'Ktime' to 'hrtimer' module to allow 'hrtimer' to delay converting to 'Instant' and 'Delta'. * tag 'rust-timekeeping-for-v6.16-v2' of https://github.com/Rust-for-Linux/linux: MAINTAINERS: rust: Add a new section for all of the time stuff rust: time: Introduce Instant type rust: time: Introduce Delta type rust: time: Add PartialEq/Eq/PartialOrd/Ord trait to Ktime rust: hrtimer: Add Ktime temporarily
2 parents edc5e6e + 6791859 commit 373827f

File tree

7 files changed

+157
-49
lines changed

7 files changed

+157
-49
lines changed

MAINTAINERS

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10602,20 +10602,23 @@ F: kernel/time/timer_list.c
1060210602
F: kernel/time/timer_migration.*
1060310603
F: tools/testing/selftests/timers/
1060410604

10605-
HIGH-RESOLUTION TIMERS [RUST]
10605+
DELAY, SLEEP, TIMEKEEPING, TIMERS [RUST]
1060610606
M: Andreas Hindborg <a.hindborg@kernel.org>
1060710607
R: Boqun Feng <boqun.feng@gmail.com>
10608+
R: FUJITA Tomonori <fujita.tomonori@gmail.com>
1060810609
R: Frederic Weisbecker <frederic@kernel.org>
1060910610
R: Lyude Paul <lyude@redhat.com>
1061010611
R: Thomas Gleixner <tglx@linutronix.de>
1061110612
R: Anna-Maria Behnsen <anna-maria@linutronix.de>
10613+
R: John Stultz <jstultz@google.com>
10614+
R: Stephen Boyd <sboyd@kernel.org>
1061210615
L: rust-for-linux@vger.kernel.org
1061310616
S: Supported
1061410617
W: https://rust-for-linux.com
1061510618
B: https://github.com/Rust-for-Linux/linux/issues
10616-
T: git https://github.com/Rust-for-Linux/linux.git hrtimer-next
10617-
F: rust/kernel/time/hrtimer.rs
10618-
F: rust/kernel/time/hrtimer/
10619+
T: git https://github.com/Rust-for-Linux/linux.git timekeeping-next
10620+
F: rust/kernel/time.rs
10621+
F: rust/kernel/time/
1061910622

1062010623
HIGH-SPEED SCC DRIVER FOR AX.25
1062110624
L: linux-hams@vger.kernel.org

rust/kernel/time.rs

Lines changed: 128 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,36 @@
55
//! This module contains the kernel APIs related to time and timers that
66
//! have been ported or wrapped for usage by Rust code in the kernel.
77
//!
8+
//! There are two types in this module:
9+
//!
10+
//! - The [`Instant`] type represents a specific point in time.
11+
//! - The [`Delta`] type represents a span of time.
12+
//!
13+
//! Note that the C side uses `ktime_t` type to represent both. However, timestamp
14+
//! and timedelta are different. To avoid confusion, we use two different types.
15+
//!
16+
//! A [`Instant`] object can be created by calling the [`Instant::now()`] function.
17+
//! It represents a point in time at which the object was created.
18+
//! By calling the [`Instant::elapsed()`] method, a [`Delta`] object representing
19+
//! the elapsed time can be created. The [`Delta`] object can also be created
20+
//! by subtracting two [`Instant`] objects.
21+
//!
22+
//! A [`Delta`] type supports methods to retrieve the duration in various units.
23+
//!
824
//! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h).
925
//! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h).
1026
1127
pub mod hrtimer;
1228

29+
/// The number of nanoseconds per microsecond.
30+
pub const NSEC_PER_USEC: i64 = bindings::NSEC_PER_USEC as i64;
31+
1332
/// The number of nanoseconds per millisecond.
1433
pub const NSEC_PER_MSEC: i64 = bindings::NSEC_PER_MSEC as i64;
1534

35+
/// The number of nanoseconds per second.
36+
pub const NSEC_PER_SEC: i64 = bindings::NSEC_PER_SEC as i64;
37+
1638
/// The time unit of Linux kernel. One jiffy equals (1/HZ) second.
1739
pub type Jiffies = crate::ffi::c_ulong;
1840

@@ -27,59 +49,44 @@ pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies {
2749
unsafe { bindings::__msecs_to_jiffies(msecs) }
2850
}
2951

30-
/// A Rust wrapper around a `ktime_t`.
52+
/// A specific point in time.
53+
///
54+
/// # Invariants
55+
///
56+
/// The `inner` value is in the range from 0 to `KTIME_MAX`.
3157
#[repr(transparent)]
32-
#[derive(Copy, Clone)]
33-
pub struct Ktime {
58+
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord)]
59+
pub struct Instant {
3460
inner: bindings::ktime_t,
3561
}
3662

37-
impl Ktime {
38-
/// Create a `Ktime` from a raw `ktime_t`.
39-
#[inline]
40-
pub fn from_raw(inner: bindings::ktime_t) -> Self {
41-
Self { inner }
42-
}
43-
63+
impl Instant {
4464
/// Get the current time using `CLOCK_MONOTONIC`.
4565
#[inline]
46-
pub fn ktime_get() -> Self {
47-
// SAFETY: It is always safe to call `ktime_get` outside of NMI context.
48-
Self::from_raw(unsafe { bindings::ktime_get() })
49-
}
50-
51-
/// Divide the number of nanoseconds by a compile-time constant.
52-
#[inline]
53-
fn divns_constant<const DIV: i64>(self) -> i64 {
54-
self.to_ns() / DIV
55-
}
56-
57-
/// Returns the number of nanoseconds.
58-
#[inline]
59-
pub fn to_ns(self) -> i64 {
60-
self.inner
66+
pub fn now() -> Self {
67+
// INVARIANT: The `ktime_get()` function returns a value in the range
68+
// from 0 to `KTIME_MAX`.
69+
Self {
70+
// SAFETY: It is always safe to call `ktime_get()` outside of NMI context.
71+
inner: unsafe { bindings::ktime_get() },
72+
}
6173
}
6274

63-
/// Returns the number of milliseconds.
75+
/// Return the amount of time elapsed since the [`Instant`].
6476
#[inline]
65-
pub fn to_ms(self) -> i64 {
66-
self.divns_constant::<NSEC_PER_MSEC>()
77+
pub fn elapsed(&self) -> Delta {
78+
Self::now() - *self
6779
}
6880
}
6981

70-
/// Returns the number of milliseconds between two ktimes.
71-
#[inline]
72-
pub fn ktime_ms_delta(later: Ktime, earlier: Ktime) -> i64 {
73-
(later - earlier).to_ms()
74-
}
75-
76-
impl core::ops::Sub for Ktime {
77-
type Output = Ktime;
82+
impl core::ops::Sub for Instant {
83+
type Output = Delta;
7884

85+
// By the type invariant, it never overflows.
7986
#[inline]
80-
fn sub(self, other: Ktime) -> Ktime {
81-
Self {
82-
inner: self.inner - other.inner,
87+
fn sub(self, other: Instant) -> Delta {
88+
Delta {
89+
nanos: self.inner - other.inner,
8390
}
8491
}
8592
}
@@ -149,3 +156,85 @@ impl ClockId {
149156
self as bindings::clockid_t
150157
}
151158
}
159+
160+
/// A span of time.
161+
///
162+
/// This struct represents a span of time, with its value stored as nanoseconds.
163+
/// The value can represent any valid i64 value, including negative, zero, and
164+
/// positive numbers.
165+
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Debug)]
166+
pub struct Delta {
167+
nanos: i64,
168+
}
169+
170+
impl Delta {
171+
/// A span of time equal to zero.
172+
pub const ZERO: Self = Self { nanos: 0 };
173+
174+
/// Create a new [`Delta`] from a number of microseconds.
175+
///
176+
/// The `micros` can range from -9_223_372_036_854_775 to 9_223_372_036_854_775.
177+
/// If `micros` is outside this range, `i64::MIN` is used for negative values,
178+
/// and `i64::MAX` is used for positive values due to saturation.
179+
#[inline]
180+
pub const fn from_micros(micros: i64) -> Self {
181+
Self {
182+
nanos: micros.saturating_mul(NSEC_PER_USEC),
183+
}
184+
}
185+
186+
/// Create a new [`Delta`] from a number of milliseconds.
187+
///
188+
/// The `millis` can range from -9_223_372_036_854 to 9_223_372_036_854.
189+
/// If `millis` is outside this range, `i64::MIN` is used for negative values,
190+
/// and `i64::MAX` is used for positive values due to saturation.
191+
#[inline]
192+
pub const fn from_millis(millis: i64) -> Self {
193+
Self {
194+
nanos: millis.saturating_mul(NSEC_PER_MSEC),
195+
}
196+
}
197+
198+
/// Create a new [`Delta`] from a number of seconds.
199+
///
200+
/// The `secs` can range from -9_223_372_036 to 9_223_372_036.
201+
/// If `secs` is outside this range, `i64::MIN` is used for negative values,
202+
/// and `i64::MAX` is used for positive values due to saturation.
203+
#[inline]
204+
pub const fn from_secs(secs: i64) -> Self {
205+
Self {
206+
nanos: secs.saturating_mul(NSEC_PER_SEC),
207+
}
208+
}
209+
210+
/// Return `true` if the [`Delta`] spans no time.
211+
#[inline]
212+
pub fn is_zero(self) -> bool {
213+
self.as_nanos() == 0
214+
}
215+
216+
/// Return `true` if the [`Delta`] spans a negative amount of time.
217+
#[inline]
218+
pub fn is_negative(self) -> bool {
219+
self.as_nanos() < 0
220+
}
221+
222+
/// Return the number of nanoseconds in the [`Delta`].
223+
#[inline]
224+
pub const fn as_nanos(self) -> i64 {
225+
self.nanos
226+
}
227+
228+
/// Return the smallest number of microseconds greater than or equal
229+
/// to the value in the [`Delta`].
230+
#[inline]
231+
pub const fn as_micros_ceil(self) -> i64 {
232+
self.as_nanos().saturating_add(NSEC_PER_USEC - 1) / NSEC_PER_USEC
233+
}
234+
235+
/// Return the number of milliseconds in the [`Delta`].
236+
#[inline]
237+
pub const fn as_millis(self) -> i64 {
238+
self.as_nanos() / NSEC_PER_MSEC
239+
}
240+
}

rust/kernel/time/hrtimer.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,26 @@
6868
//! `start` operation.
6969
7070
use super::ClockId;
71-
use crate::{prelude::*, time::Ktime, types::Opaque};
71+
use crate::{prelude::*, types::Opaque};
7272
use core::marker::PhantomData;
7373
use pin_init::PinInit;
7474

75+
/// A Rust wrapper around a `ktime_t`.
76+
// NOTE: Ktime is going to be removed when hrtimer is converted to Instant/Delta.
77+
#[repr(transparent)]
78+
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord)]
79+
pub struct Ktime {
80+
inner: bindings::ktime_t,
81+
}
82+
83+
impl Ktime {
84+
/// Returns the number of nanoseconds.
85+
#[inline]
86+
pub fn to_ns(self) -> i64 {
87+
self.inner
88+
}
89+
}
90+
7591
/// A timer backed by a C `struct hrtimer`.
7692
///
7793
/// # Invariants

rust/kernel/time/hrtimer/arc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use super::HrTimer;
55
use super::HrTimerCallback;
66
use super::HrTimerHandle;
77
use super::HrTimerPointer;
8+
use super::Ktime;
89
use super::RawHrTimerCallback;
910
use crate::sync::Arc;
1011
use crate::sync::ArcBorrow;
11-
use crate::time::Ktime;
1212

1313
/// A handle for an `Arc<HasHrTimer<T>>` returned by a call to
1414
/// [`HrTimerPointer::start`].

rust/kernel/time/hrtimer/pin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use super::HasHrTimer;
44
use super::HrTimer;
55
use super::HrTimerCallback;
66
use super::HrTimerHandle;
7+
use super::Ktime;
78
use super::RawHrTimerCallback;
89
use super::UnsafeHrTimerPointer;
9-
use crate::time::Ktime;
1010
use core::pin::Pin;
1111

1212
/// A handle for a `Pin<&HasHrTimer>`. When the handle exists, the timer might be

rust/kernel/time/hrtimer/pin_mut.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// SPDX-License-Identifier: GPL-2.0
22

33
use super::{
4-
HasHrTimer, HrTimer, HrTimerCallback, HrTimerHandle, RawHrTimerCallback, UnsafeHrTimerPointer,
4+
HasHrTimer, HrTimer, HrTimerCallback, HrTimerHandle, Ktime, RawHrTimerCallback,
5+
UnsafeHrTimerPointer,
56
};
6-
use crate::time::Ktime;
77
use core::{marker::PhantomData, pin::Pin, ptr::NonNull};
88

99
/// A handle for a `Pin<&mut HasHrTimer>`. When the handle exists, the timer might

rust/kernel/time/hrtimer/tbox.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use super::HrTimer;
55
use super::HrTimerCallback;
66
use super::HrTimerHandle;
77
use super::HrTimerPointer;
8+
use super::Ktime;
89
use super::RawHrTimerCallback;
910
use crate::prelude::*;
10-
use crate::time::Ktime;
1111
use core::ptr::NonNull;
1212

1313
/// A handle for a [`Box<HasHrTimer<T>>`] returned by a call to

0 commit comments

Comments
 (0)