[PATCH 1/4] rust: sync: condvar: add wait_interruptible_freezable_timeout()
From: Nicholas Giunta
Date: Thu Dec 03 2026 - 19:33:16 EST
To support waiting on a condition variable with a timeout as a freezable
task, add CondVar::wait_interruptible_freezable_timeout().
Rust Binder requires this functionality to allow idle looper threads to
time out and spin down while still allowing processes to be frozen when
their threads are blocked in the binder driver.
Assisted-by: Gemini
Signed-off-by: Nicholas Giunta <grahalt@xxxxxxxxxx>
---
rust/kernel/sync/condvar.rs | 47 ++++++++++++++++++++++++++++++++++++++-------
1 file changed, 40 insertions(+), 7 deletions(-)
diff --git a/rust/kernel/sync/condvar.rs b/rust/kernel/sync/condvar.rs
index 69d58dfbad7b..e5ddf7427eea 100644
--- a/rust/kernel/sync/condvar.rs
+++ b/rust/kernel/sync/condvar.rs
@@ -177,6 +177,22 @@ pub fn wait_interruptible_freezable<T: ?Sized, B: Backend>(
crate::current!().signal_pending()
}
+ fn wait_interruptible_timeout_internal<T: ?Sized, B: Backend>(
+ &self,
+ wait_state: c_int,
+ guard: &mut Guard<'_, T, B>,
+ jiffies: Jiffies,
+ ) -> CondVarTimeoutResult {
+ let jiffies = jiffies.try_into().unwrap_or(MAX_SCHEDULE_TIMEOUT);
+ let res = self.wait_internal(wait_state, guard, jiffies);
+
+ match (res as Jiffies, crate::current!().signal_pending()) {
+ (jiffies, true) => CondVarTimeoutResult::Signal { jiffies },
+ (0, false) => CondVarTimeoutResult::Timeout,
+ (jiffies, false) => CondVarTimeoutResult::Woken { jiffies },
+ }
+ }
+
/// Releases the lock and waits for a notification in interruptible mode.
///
/// Atomically releases the given lock (whose ownership is proven by the guard) and puts the
@@ -188,14 +204,31 @@ pub fn wait_interruptible_timeout<T: ?Sized, B: Backend>(
guard: &mut Guard<'_, T, B>,
jiffies: Jiffies,
) -> CondVarTimeoutResult {
- let jiffies = jiffies.try_into().unwrap_or(MAX_SCHEDULE_TIMEOUT);
- let res = self.wait_internal(TASK_INTERRUPTIBLE, guard, jiffies);
+ self.wait_interruptible_timeout_internal(TASK_INTERRUPTIBLE, guard, jiffies)
+ }
- match (res as Jiffies, crate::current!().signal_pending()) {
- (jiffies, true) => CondVarTimeoutResult::Signal { jiffies },
- (0, false) => CondVarTimeoutResult::Timeout,
- (jiffies, false) => CondVarTimeoutResult::Woken { jiffies },
- }
+ /// Releases the lock and waits for a notification in interruptible and freezable mode.
+ ///
+ /// Atomically releases the given lock (whose ownership is proven by the guard) and puts the
+ /// thread to sleep. It wakes up when notified by [`CondVar::notify_one`] or
+ /// [`CondVar::notify_all`], or when a timeout occurs, or when the thread receives a signal.
+ ///
+ /// The process is allowed to be frozen during this sleep. No lock should be held when calling
+ /// this function, and there is a lockdep assertion for this. Freezing a task that holds a lock
+ /// can trivially deadlock vs another task that needs that lock to complete before it too can
+ /// hit freezable.
+ #[must_use = "wait_interruptible_freezable_timeout returns if a signal is pending, \
+ so the caller must check the return value"]
+ pub fn wait_interruptible_freezable_timeout<T: ?Sized, B: Backend>(
+ &self,
+ guard: &mut Guard<'_, T, B>,
+ jiffies: Jiffies,
+ ) -> CondVarTimeoutResult {
+ self.wait_interruptible_timeout_internal(
+ TASK_INTERRUPTIBLE | TASK_FREEZABLE,
+ guard,
+ jiffies,
+ )
}
/// Calls the kernel function to notify the appropriate number of threads.
--
2.56.1.915.g69d5ec7298-goog