[PATCH v3 1/5] rust: task: add safe schedule_timeout() wrapper
From: Danilo Krummrich
Date: Mon Nov 16 2026 - 19:15:27 EST
Add a safe wrapper around schedule_timeout() that takes and returns
Delta<Jiffy>, treating negative durations as zero.
Acked-by: Miguel Ojeda <ojeda@xxxxxxxxxx>
Signed-off-by: Danilo Krummrich <dakr@xxxxxxxxxx>
---
rust/kernel/task.rs | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs
index 3336df493dec..2fc0607ba9b7 100644
--- a/rust/kernel/task.rs
+++ b/rust/kernel/task.rs
@@ -10,6 +10,10 @@
pid_namespace::PidNamespace,
prelude::*,
sync::aref::ARef,
+ time::{
+ Delta,
+ Jiffy, //
+ },
types::{NotThreadSafe, Opaque},
};
use core::{
@@ -20,6 +24,25 @@
/// A sentinel value used for infinite timeouts.
pub const MAX_SCHEDULE_TIMEOUT: c_long = c_long::MAX;
+/// Sleeps for the given timeout, or until woken.
+///
+/// Returns the remaining time, or zero if the timeout expired. The task state should be set before
+/// calling this.
+///
+/// A negative timeout is treated as zero.
+///
+/// A timeout of [`MAX_SCHEDULE_TIMEOUT`] jiffies means an indefinite wait. In that case, the
+/// returned duration is also [`MAX_SCHEDULE_TIMEOUT`] jiffies.
+#[inline]
+pub fn schedule_timeout(timeout: Delta<Jiffy>) -> Delta<Jiffy> {
+ let timeout = timeout.as_jiffies().max(0);
+
+ // SAFETY: `schedule_timeout()` is always safe to call.
+ let remaining = unsafe { bindings::schedule_timeout(timeout) };
+
+ Delta::from_jiffies(remaining)
+}
+
/// Bitmask for tasks that are sleeping in an interruptible state.
pub const TASK_INTERRUPTIBLE: c_int = bindings::TASK_INTERRUPTIBLE as c_int;
/// Bitmask for tasks that are sleeping in an uninterruptible state.
--
2.56.1