Skip to content

Commit e6ea10d

Browse files
committed
Merge tag 'rust-hrtimer-for-v6.15-v3' of https://github.com/Rust-for-Linux/linux into rust-next
Pull rust-hrtimer updates from Andreas Hindborg: "Introduce Rust support for the 'hrtimer' subsystem: - Add a way to use the 'hrtimer' subsystem from Rust. Rust code can now set up intrusive timers without allocating when starting the timer. - Add support for 'Pin<Box<_>>', 'Arc<_>', 'Pin<&_>' and 'Pin<&mut _>' as pointer types for use with timer callbacks. - Add support for setting clock source and timer mode. 'kernel' crate: - Add 'Arc::as_ptr' for converting an 'Arc' to a raw pointer. This is a dependency for the 'hrtimer' API. - Add 'Box::into_pin' for converting a 'Box<_>' into a 'Pin<Box<_>>' to align with Rust 'alloc'. This is a dependency for the 'hrtimer' API." * tag 'rust-hrtimer-for-v6.15-v3' of https://github.com/Rust-for-Linux/linux: rust: hrtimer: add maintainer entry rust: hrtimer: add clocksource selection through `ClockId` rust: hrtimer: add `HrTimerMode` rust: hrtimer: implement `HrTimerPointer` for `Pin<Box<T>>` rust: alloc: add `Box::into_pin` rust: hrtimer: implement `UnsafeHrTimerPointer` for `Pin<&mut T>` rust: hrtimer: implement `UnsafeHrTimerPointer` for `Pin<&T>` rust: hrtimer: add `hrtimer::ScopedHrTimerPointer` rust: hrtimer: add `UnsafeHrTimerPointer` rust: hrtimer: allow timer restart from timer handler rust: hrtimer: implement `HrTimerPointer` for `Arc` rust: sync: add `Arc::as_ptr` rust: hrtimer: introduce hrtimer support
2 parents 28bb48c + 142d939 commit e6ea10d

File tree

9 files changed

+1052
-2
lines changed

9 files changed

+1052
-2
lines changed

MAINTAINERS

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10370,6 +10370,21 @@ F: kernel/time/timer_list.c
1037010370
F: kernel/time/timer_migration.*
1037110371
F: tools/testing/selftests/timers/
1037210372

10373+
HIGH-RESOLUTION TIMERS [RUST]
10374+
M: Andreas Hindborg <[email protected]>
10375+
R: Boqun Feng <[email protected]>
10376+
R: Frederic Weisbecker <[email protected]>
10377+
R: Lyude Paul <[email protected]>
10378+
R: Thomas Gleixner <[email protected]>
10379+
R: Anna-Maria Behnsen <[email protected]>
10380+
10381+
S: Supported
10382+
W: https://rust-for-linux.com
10383+
B: https://github.com/Rust-for-Linux/linux/issues
10384+
T: git https://github.com/Rust-for-Linux/linux.git hrtimer-next
10385+
F: rust/kernel/time/hrtimer.rs
10386+
F: rust/kernel/time/hrtimer/
10387+
1037310388
HIGH-SPEED SCC DRIVER FOR AX.25
1037410389
1037510390
S: Orphan

rust/kernel/alloc/kbox.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ where
252252
Ok(Self::new(x, flags)?.into())
253253
}
254254

255+
/// Convert a [`Box<T,A>`] to a [`Pin<Box<T,A>>`]. If `T` does not implement
256+
/// [`Unpin`], then `x` will be pinned in memory and can't be moved.
257+
pub fn into_pin(this: Self) -> Pin<Self> {
258+
this.into()
259+
}
260+
255261
/// Forgets the contents (does not run the destructor), but keeps the allocation.
256262
fn forget_contents(this: Self) -> Box<MaybeUninit<T>, A> {
257263
let ptr = Self::into_raw(this);

rust/kernel/sync/arc.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,15 @@ impl<T: ?Sized> Arc<T> {
266266
unsafe { core::ptr::addr_of!((*ptr).data) }
267267
}
268268

269+
/// Return a raw pointer to the data in this arc.
270+
pub fn as_ptr(this: &Self) -> *const T {
271+
let ptr = this.ptr.as_ptr();
272+
273+
// SAFETY: As `ptr` points to a valid allocation of type `ArcInner`,
274+
// field projection to `data`is within bounds of the allocation.
275+
unsafe { core::ptr::addr_of!((*ptr).data) }
276+
}
277+
269278
/// Recreates an [`Arc`] instance previously deconstructed via [`Arc::into_raw`].
270279
///
271280
/// # Safety
@@ -559,11 +568,11 @@ impl<T: ?Sized> ArcBorrow<'_, T> {
559568
}
560569

561570
/// Creates an [`ArcBorrow`] to an [`Arc`] that has previously been deconstructed with
562-
/// [`Arc::into_raw`].
571+
/// [`Arc::into_raw`] or [`Arc::as_ptr`].
563572
///
564573
/// # Safety
565574
///
566-
/// * The provided pointer must originate from a call to [`Arc::into_raw`].
575+
/// * The provided pointer must originate from a call to [`Arc::into_raw`] or [`Arc::as_ptr`].
567576
/// * For the duration of the lifetime annotated on this `ArcBorrow`, the reference count must
568577
/// not hit zero.
569578
/// * For the duration of the lifetime annotated on this `ArcBorrow`, there must not be a

rust/kernel/time.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
//! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h).
99
//! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h).
1010
11+
pub mod hrtimer;
12+
1113
/// The number of nanoseconds per millisecond.
1214
pub const NSEC_PER_MSEC: i64 = bindings::NSEC_PER_MSEC as i64;
1315

@@ -81,3 +83,69 @@ impl core::ops::Sub for Ktime {
8183
}
8284
}
8385
}
86+
87+
/// An identifier for a clock. Used when specifying clock sources.
88+
///
89+
///
90+
/// Selection of the clock depends on the use case. In some cases the usage of a
91+
/// particular clock is mandatory, e.g. in network protocols, filesystems.In other
92+
/// cases the user of the clock has to decide which clock is best suited for the
93+
/// purpose. In most scenarios clock [`ClockId::Monotonic`] is the best choice as it
94+
/// provides a accurate monotonic notion of time (leap second smearing ignored).
95+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
96+
#[repr(u32)]
97+
pub enum ClockId {
98+
/// A settable system-wide clock that measures real (i.e., wall-clock) time.
99+
///
100+
/// Setting this clock requires appropriate privileges. This clock is
101+
/// affected by discontinuous jumps in the system time (e.g., if the system
102+
/// administrator manually changes the clock), and by frequency adjustments
103+
/// performed by NTP and similar applications via adjtime(3), adjtimex(2),
104+
/// clock_adjtime(2), and ntp_adjtime(3). This clock normally counts the
105+
/// number of seconds since 1970-01-01 00:00:00 Coordinated Universal Time
106+
/// (UTC) except that it ignores leap seconds; near a leap second it may be
107+
/// adjusted by leap second smearing to stay roughly in sync with UTC. Leap
108+
/// second smearing applies frequency adjustments to the clock to speed up
109+
/// or slow down the clock to account for the leap second without
110+
/// discontinuities in the clock. If leap second smearing is not applied,
111+
/// the clock will experience discontinuity around leap second adjustment.
112+
RealTime = bindings::CLOCK_REALTIME,
113+
/// A monotonically increasing clock.
114+
///
115+
/// A nonsettable system-wide clock that represents monotonic time since—as
116+
/// described by POSIX—"some unspecified point in the past". On Linux, that
117+
/// point corresponds to the number of seconds that the system has been
118+
/// running since it was booted.
119+
///
120+
/// The CLOCK_MONOTONIC clock is not affected by discontinuous jumps in the
121+
/// CLOCK_REAL (e.g., if the system administrator manually changes the
122+
/// clock), but is affected by frequency adjustments. This clock does not
123+
/// count time that the system is suspended.
124+
Monotonic = bindings::CLOCK_MONOTONIC,
125+
/// A monotonic that ticks while system is suspended.
126+
///
127+
/// A nonsettable system-wide clock that is identical to CLOCK_MONOTONIC,
128+
/// except that it also includes any time that the system is suspended. This
129+
/// allows applications to get a suspend-aware monotonic clock without
130+
/// having to deal with the complications of CLOCK_REALTIME, which may have
131+
/// discontinuities if the time is changed using settimeofday(2) or similar.
132+
BootTime = bindings::CLOCK_BOOTTIME,
133+
/// International Atomic Time.
134+
///
135+
/// A system-wide clock derived from wall-clock time but counting leap seconds.
136+
///
137+
/// This clock is coupled to CLOCK_REALTIME and will be set when CLOCK_REALTIME is
138+
/// set, or when the offset to CLOCK_REALTIME is changed via adjtimex(2). This
139+
/// usually happens during boot and **should** not happen during normal operations.
140+
/// However, if NTP or another application adjusts CLOCK_REALTIME by leap second
141+
/// smearing, this clock will not be precise during leap second smearing.
142+
///
143+
/// The acronym TAI refers to International Atomic Time.
144+
TAI = bindings::CLOCK_TAI,
145+
}
146+
147+
impl ClockId {
148+
fn into_c(self) -> bindings::clockid_t {
149+
self as bindings::clockid_t
150+
}
151+
}

0 commit comments

Comments
 (0)