mirror of
git://git.yoctoproject.org/linux-yocto.git
synced 2025-10-22 23:13:01 +02:00
Rust timekeeping changes for v6.16
- 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`. -----BEGIN PGP SIGNATURE----- iQJKBAABCAA0FiEEEsH5R1a/fCoV1sAS4bgaPnkoY3cFAmgZ1+8WHGEuaGluZGJv cmdAa2VybmVsLm9yZwAKCRDhuBo+eShjd8MTD/9di4KXLhRpWaqVcNDbmNIPDaCL e241wbiOkganrrel2ZtNg5ZVnxvHv7sNXcq1QrARNQE+/hb0LuXK/NHXINaovN2Y bL29UyZGeL8rVW7sw4EKR/q8WE4dxUqCGBL6cF0/8aRQHGPteUuWm3Le5T4p4fej tM0++NT9JzbNe0RFDYK5S5stl1S8VJiFeVLniTB85WhtpBdSvBGJ4P4d0GMUOwnZ 4Nbis56CXFtR32G2B6N+ztiBpMHidqCJ3LkavnXkff/tD6ja5bScsbgeZerwLOrR Rkxz6n1IDFkx+tYLS7U54XWFnWGM0TeGtyGsxdHwPVn3g8O3sak5UN6mT+imeLXY 3KAsMqXEMMYoONnYtxS3k2Th9KtWs79SCi5lMou6YekqMZBDQxvQmBP0M7vnNGeL wtGQ1ZUz3OsNwepjtH0Pjg13MmAK5Do+e+SEWGDwXiftDhnOmvRFFChqDO7kE/Jp sqq6y0IZBBoZEiNsfvF22NunGXvx0MePUq8bppEtf3v8j8KCK3pW9jXMa/9eECTO IBbIqG+uPvIGiTR6FnlEMBn8TUc1RBGSKuQ6qOKX/4YQ0uayTTQjO0ugNiMpODvB 2w9HiPymq7Mk5iZ3Dmeeo1Je5AJEN1VsWiOAs5URABXAYeSopOdrsaBLG1vgK+yW 0+xkDsf6VNiGcWdhbg== =M1o/ -----END PGP SIGNATURE----- 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
This commit is contained in:
commit
373827fce2
11
MAINTAINERS
11
MAINTAINERS
|
@ -10602,20 +10602,23 @@ F: kernel/time/timer_list.c
|
|||
F: kernel/time/timer_migration.*
|
||||
F: tools/testing/selftests/timers/
|
||||
|
||||
HIGH-RESOLUTION TIMERS [RUST]
|
||||
DELAY, SLEEP, TIMEKEEPING, TIMERS [RUST]
|
||||
M: Andreas Hindborg <a.hindborg@kernel.org>
|
||||
R: Boqun Feng <boqun.feng@gmail.com>
|
||||
R: FUJITA Tomonori <fujita.tomonori@gmail.com>
|
||||
R: Frederic Weisbecker <frederic@kernel.org>
|
||||
R: Lyude Paul <lyude@redhat.com>
|
||||
R: Thomas Gleixner <tglx@linutronix.de>
|
||||
R: Anna-Maria Behnsen <anna-maria@linutronix.de>
|
||||
R: John Stultz <jstultz@google.com>
|
||||
R: Stephen Boyd <sboyd@kernel.org>
|
||||
L: rust-for-linux@vger.kernel.org
|
||||
S: Supported
|
||||
W: https://rust-for-linux.com
|
||||
B: https://github.com/Rust-for-Linux/linux/issues
|
||||
T: git https://github.com/Rust-for-Linux/linux.git hrtimer-next
|
||||
F: rust/kernel/time/hrtimer.rs
|
||||
F: rust/kernel/time/hrtimer/
|
||||
T: git https://github.com/Rust-for-Linux/linux.git timekeeping-next
|
||||
F: rust/kernel/time.rs
|
||||
F: rust/kernel/time/
|
||||
|
||||
HIGH-SPEED SCC DRIVER FOR AX.25
|
||||
L: linux-hams@vger.kernel.org
|
||||
|
|
|
@ -5,14 +5,36 @@
|
|||
//! This module contains the kernel APIs related to time and timers that
|
||||
//! have been ported or wrapped for usage by Rust code in the kernel.
|
||||
//!
|
||||
//! There are two types in this module:
|
||||
//!
|
||||
//! - The [`Instant`] type represents a specific point in time.
|
||||
//! - The [`Delta`] type represents a span of time.
|
||||
//!
|
||||
//! Note that the C side uses `ktime_t` type to represent both. However, timestamp
|
||||
//! and timedelta are different. To avoid confusion, we use two different types.
|
||||
//!
|
||||
//! A [`Instant`] object can be created by calling the [`Instant::now()`] function.
|
||||
//! It represents a point in time at which the object was created.
|
||||
//! By calling the [`Instant::elapsed()`] method, a [`Delta`] object representing
|
||||
//! the elapsed time can be created. The [`Delta`] object can also be created
|
||||
//! by subtracting two [`Instant`] objects.
|
||||
//!
|
||||
//! A [`Delta`] type supports methods to retrieve the duration in various units.
|
||||
//!
|
||||
//! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h).
|
||||
//! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h).
|
||||
|
||||
pub mod hrtimer;
|
||||
|
||||
/// The number of nanoseconds per microsecond.
|
||||
pub const NSEC_PER_USEC: i64 = bindings::NSEC_PER_USEC as i64;
|
||||
|
||||
/// The number of nanoseconds per millisecond.
|
||||
pub const NSEC_PER_MSEC: i64 = bindings::NSEC_PER_MSEC as i64;
|
||||
|
||||
/// The number of nanoseconds per second.
|
||||
pub const NSEC_PER_SEC: i64 = bindings::NSEC_PER_SEC as i64;
|
||||
|
||||
/// The time unit of Linux kernel. One jiffy equals (1/HZ) second.
|
||||
pub type Jiffies = crate::ffi::c_ulong;
|
||||
|
||||
|
@ -27,59 +49,44 @@ pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies {
|
|||
unsafe { bindings::__msecs_to_jiffies(msecs) }
|
||||
}
|
||||
|
||||
/// A Rust wrapper around a `ktime_t`.
|
||||
/// A specific point in time.
|
||||
///
|
||||
/// # Invariants
|
||||
///
|
||||
/// The `inner` value is in the range from 0 to `KTIME_MAX`.
|
||||
#[repr(transparent)]
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Ktime {
|
||||
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord)]
|
||||
pub struct Instant {
|
||||
inner: bindings::ktime_t,
|
||||
}
|
||||
|
||||
impl Ktime {
|
||||
/// Create a `Ktime` from a raw `ktime_t`.
|
||||
#[inline]
|
||||
pub fn from_raw(inner: bindings::ktime_t) -> Self {
|
||||
Self { inner }
|
||||
}
|
||||
|
||||
impl Instant {
|
||||
/// Get the current time using `CLOCK_MONOTONIC`.
|
||||
#[inline]
|
||||
pub fn ktime_get() -> Self {
|
||||
// SAFETY: It is always safe to call `ktime_get` outside of NMI context.
|
||||
Self::from_raw(unsafe { bindings::ktime_get() })
|
||||
}
|
||||
|
||||
/// Divide the number of nanoseconds by a compile-time constant.
|
||||
#[inline]
|
||||
fn divns_constant<const DIV: i64>(self) -> i64 {
|
||||
self.to_ns() / DIV
|
||||
}
|
||||
|
||||
/// Returns the number of nanoseconds.
|
||||
#[inline]
|
||||
pub fn to_ns(self) -> i64 {
|
||||
self.inner
|
||||
}
|
||||
|
||||
/// Returns the number of milliseconds.
|
||||
#[inline]
|
||||
pub fn to_ms(self) -> i64 {
|
||||
self.divns_constant::<NSEC_PER_MSEC>()
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the number of milliseconds between two ktimes.
|
||||
#[inline]
|
||||
pub fn ktime_ms_delta(later: Ktime, earlier: Ktime) -> i64 {
|
||||
(later - earlier).to_ms()
|
||||
}
|
||||
|
||||
impl core::ops::Sub for Ktime {
|
||||
type Output = Ktime;
|
||||
|
||||
#[inline]
|
||||
fn sub(self, other: Ktime) -> Ktime {
|
||||
pub fn now() -> Self {
|
||||
// INVARIANT: The `ktime_get()` function returns a value in the range
|
||||
// from 0 to `KTIME_MAX`.
|
||||
Self {
|
||||
inner: self.inner - other.inner,
|
||||
// SAFETY: It is always safe to call `ktime_get()` outside of NMI context.
|
||||
inner: unsafe { bindings::ktime_get() },
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the amount of time elapsed since the [`Instant`].
|
||||
#[inline]
|
||||
pub fn elapsed(&self) -> Delta {
|
||||
Self::now() - *self
|
||||
}
|
||||
}
|
||||
|
||||
impl core::ops::Sub for Instant {
|
||||
type Output = Delta;
|
||||
|
||||
// By the type invariant, it never overflows.
|
||||
#[inline]
|
||||
fn sub(self, other: Instant) -> Delta {
|
||||
Delta {
|
||||
nanos: self.inner - other.inner,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -149,3 +156,85 @@ impl ClockId {
|
|||
self as bindings::clockid_t
|
||||
}
|
||||
}
|
||||
|
||||
/// A span of time.
|
||||
///
|
||||
/// This struct represents a span of time, with its value stored as nanoseconds.
|
||||
/// The value can represent any valid i64 value, including negative, zero, and
|
||||
/// positive numbers.
|
||||
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Debug)]
|
||||
pub struct Delta {
|
||||
nanos: i64,
|
||||
}
|
||||
|
||||
impl Delta {
|
||||
/// A span of time equal to zero.
|
||||
pub const ZERO: Self = Self { nanos: 0 };
|
||||
|
||||
/// Create a new [`Delta`] from a number of microseconds.
|
||||
///
|
||||
/// The `micros` can range from -9_223_372_036_854_775 to 9_223_372_036_854_775.
|
||||
/// If `micros` is outside this range, `i64::MIN` is used for negative values,
|
||||
/// and `i64::MAX` is used for positive values due to saturation.
|
||||
#[inline]
|
||||
pub const fn from_micros(micros: i64) -> Self {
|
||||
Self {
|
||||
nanos: micros.saturating_mul(NSEC_PER_USEC),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new [`Delta`] from a number of milliseconds.
|
||||
///
|
||||
/// The `millis` can range from -9_223_372_036_854 to 9_223_372_036_854.
|
||||
/// If `millis` is outside this range, `i64::MIN` is used for negative values,
|
||||
/// and `i64::MAX` is used for positive values due to saturation.
|
||||
#[inline]
|
||||
pub const fn from_millis(millis: i64) -> Self {
|
||||
Self {
|
||||
nanos: millis.saturating_mul(NSEC_PER_MSEC),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new [`Delta`] from a number of seconds.
|
||||
///
|
||||
/// The `secs` can range from -9_223_372_036 to 9_223_372_036.
|
||||
/// If `secs` is outside this range, `i64::MIN` is used for negative values,
|
||||
/// and `i64::MAX` is used for positive values due to saturation.
|
||||
#[inline]
|
||||
pub const fn from_secs(secs: i64) -> Self {
|
||||
Self {
|
||||
nanos: secs.saturating_mul(NSEC_PER_SEC),
|
||||
}
|
||||
}
|
||||
|
||||
/// Return `true` if the [`Delta`] spans no time.
|
||||
#[inline]
|
||||
pub fn is_zero(self) -> bool {
|
||||
self.as_nanos() == 0
|
||||
}
|
||||
|
||||
/// Return `true` if the [`Delta`] spans a negative amount of time.
|
||||
#[inline]
|
||||
pub fn is_negative(self) -> bool {
|
||||
self.as_nanos() < 0
|
||||
}
|
||||
|
||||
/// Return the number of nanoseconds in the [`Delta`].
|
||||
#[inline]
|
||||
pub const fn as_nanos(self) -> i64 {
|
||||
self.nanos
|
||||
}
|
||||
|
||||
/// Return the smallest number of microseconds greater than or equal
|
||||
/// to the value in the [`Delta`].
|
||||
#[inline]
|
||||
pub const fn as_micros_ceil(self) -> i64 {
|
||||
self.as_nanos().saturating_add(NSEC_PER_USEC - 1) / NSEC_PER_USEC
|
||||
}
|
||||
|
||||
/// Return the number of milliseconds in the [`Delta`].
|
||||
#[inline]
|
||||
pub const fn as_millis(self) -> i64 {
|
||||
self.as_nanos() / NSEC_PER_MSEC
|
||||
}
|
||||
}
|
||||
|
|
|
@ -68,10 +68,26 @@
|
|||
//! `start` operation.
|
||||
|
||||
use super::ClockId;
|
||||
use crate::{prelude::*, time::Ktime, types::Opaque};
|
||||
use crate::{prelude::*, types::Opaque};
|
||||
use core::marker::PhantomData;
|
||||
use pin_init::PinInit;
|
||||
|
||||
/// A Rust wrapper around a `ktime_t`.
|
||||
// NOTE: Ktime is going to be removed when hrtimer is converted to Instant/Delta.
|
||||
#[repr(transparent)]
|
||||
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord)]
|
||||
pub struct Ktime {
|
||||
inner: bindings::ktime_t,
|
||||
}
|
||||
|
||||
impl Ktime {
|
||||
/// Returns the number of nanoseconds.
|
||||
#[inline]
|
||||
pub fn to_ns(self) -> i64 {
|
||||
self.inner
|
||||
}
|
||||
}
|
||||
|
||||
/// A timer backed by a C `struct hrtimer`.
|
||||
///
|
||||
/// # Invariants
|
||||
|
|
|
@ -5,10 +5,10 @@ use super::HrTimer;
|
|||
use super::HrTimerCallback;
|
||||
use super::HrTimerHandle;
|
||||
use super::HrTimerPointer;
|
||||
use super::Ktime;
|
||||
use super::RawHrTimerCallback;
|
||||
use crate::sync::Arc;
|
||||
use crate::sync::ArcBorrow;
|
||||
use crate::time::Ktime;
|
||||
|
||||
/// A handle for an `Arc<HasHrTimer<T>>` returned by a call to
|
||||
/// [`HrTimerPointer::start`].
|
||||
|
|
|
@ -4,9 +4,9 @@ use super::HasHrTimer;
|
|||
use super::HrTimer;
|
||||
use super::HrTimerCallback;
|
||||
use super::HrTimerHandle;
|
||||
use super::Ktime;
|
||||
use super::RawHrTimerCallback;
|
||||
use super::UnsafeHrTimerPointer;
|
||||
use crate::time::Ktime;
|
||||
use core::pin::Pin;
|
||||
|
||||
/// A handle for a `Pin<&HasHrTimer>`. When the handle exists, the timer might be
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
|
||||
use super::{
|
||||
HasHrTimer, HrTimer, HrTimerCallback, HrTimerHandle, RawHrTimerCallback, UnsafeHrTimerPointer,
|
||||
HasHrTimer, HrTimer, HrTimerCallback, HrTimerHandle, Ktime, RawHrTimerCallback,
|
||||
UnsafeHrTimerPointer,
|
||||
};
|
||||
use crate::time::Ktime;
|
||||
use core::{marker::PhantomData, pin::Pin, ptr::NonNull};
|
||||
|
||||
/// A handle for a `Pin<&mut HasHrTimer>`. When the handle exists, the timer might
|
||||
|
|
|
@ -5,9 +5,9 @@ use super::HrTimer;
|
|||
use super::HrTimerCallback;
|
||||
use super::HrTimerHandle;
|
||||
use super::HrTimerPointer;
|
||||
use super::Ktime;
|
||||
use super::RawHrTimerCallback;
|
||||
use crate::prelude::*;
|
||||
use crate::time::Ktime;
|
||||
use core::ptr::NonNull;
|
||||
|
||||
/// A handle for a [`Box<HasHrTimer<T>>`] returned by a call to
|
||||
|
|
Loading…
Reference in New Issue
Block a user