Skip to content

Commit 82e1708

Browse files
Darksonnojeda
authored andcommitted
rust: time: add msecs to jiffies conversion
Defines type aliases and conversions for msecs and jiffies. This is used by Rust Binder for process freezing. There, we want to sleep until the freeze operation completes, but we want to be able to abort the process freezing if it doesn't complete within some timeout. The freeze timeout is supplied in msecs. Note that we need to convert to jiffies in Binder. It is not enough to introduce a variant of `CondVar::wait_timeout` that takes the timeout in msecs because we need to be able to restart the sleep with the remaining sleep duration if it is interrupted, and if the API takes msecs rather than jiffies, then that would require a conversion roundtrip jiffies-> msecs->jiffies that is best avoided. Suggested-by: Boqun Feng <boqun.feng@gmail.com> Reviewed-by: Boqun Feng <boqun.feng@gmail.com> Reviewed-by: Benno Lossin <benno.lossin@proton.me> Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com> Reviewed-by: Tiago Lam <tiagolam@gmail.com> Signed-off-by: Alice Ryhl <aliceryhl@google.com> Link: https://lore.kernel.org/r/20240108-rb-new-condvar-methods-v4-2-88e0c871cc05@google.com Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 3e64541 commit 82e1708

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

rust/bindings/bindings_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <kunit/test.h>
1010
#include <linux/errname.h>
1111
#include <linux/ethtool.h>
12+
#include <linux/jiffies.h>
1213
#include <linux/mdio.h>
1314
#include <linux/phy.h>
1415
#include <linux/slab.h>

rust/kernel/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub mod std_vendor;
4848
pub mod str;
4949
pub mod sync;
5050
pub mod task;
51+
pub mod time;
5152
pub mod types;
5253
pub mod workqueue;
5354

rust/kernel/time.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Time related primitives.
4+
//!
5+
//! This module contains the kernel APIs related to time and timers that
6+
//! have been ported or wrapped for usage by Rust code in the kernel.
7+
8+
/// The time unit of Linux kernel. One jiffy equals (1/HZ) second.
9+
pub type Jiffies = core::ffi::c_ulong;
10+
11+
/// The millisecond time unit.
12+
pub type Msecs = core::ffi::c_uint;
13+
14+
/// Converts milliseconds to jiffies.
15+
#[inline]
16+
pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies {
17+
// SAFETY: The `__msecs_to_jiffies` function is always safe to call no
18+
// matter what the argument is.
19+
unsafe { bindings::__msecs_to_jiffies(msecs) }
20+
}

0 commit comments

Comments
 (0)