[PATCH v3 02/14] rust: sync: completion: add wait_for_completion_timeout()
From: John Hubbard <jhubbard@nvidia.com>
Date: 2026-09-03 03:15:28
Also in:
lkml
Subsystem:
rust, rust [sync], the rest · Maintainers:
Miguel Ojeda, Boqun Feng, Gary Guo, Alice Ryhl, Linus Torvalds
From: Joel Fernandes <joelagnelf@nvidia.com> A driver that runs an interrupt self-test during probe waits for the handler to fire. wait_for_completion() has no timeout, so a broken interrupt path stalls probe indefinitely. Add a timeout variant of wait_for_completion(). Reviewed-by: Alexandre Courbot <acourbot@nvidia.com> Signed-off-by: Joel Fernandes <joelagnelf@nvidia.com> [jhubbard: return the remaining jiffies] Signed-off-by: John Hubbard <jhubbard@nvidia.com> --- rust/kernel/sync/completion.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/sync/completion.rs b/rust/kernel/sync/completion.rs
index 35ff049ff078..7e8b3c1c880e 100644
--- a/rust/kernel/sync/completion.rs
+++ b/rust/kernel/sync/completion.rs@@ -6,7 +6,12 @@ //! //! C header: [`include/linux/completion.h`](srctree/include/linux/completion.h) -use crate::{bindings, prelude::*, types::Opaque}; +use crate::{ + bindings, + prelude::*, + time::Jiffies, + types::Opaque, // +}; /// Synchronization primitive to signal when a certain task has been completed. ///
@@ -111,4 +116,20 @@ pub fn wait_for_completion(&self) { // SAFETY: `self.as_raw()` is a pointer to a valid `struct completion`. unsafe { bindings::wait_for_completion(self.as_raw()) }; } + + /// Wait for completion of a task, with a timeout. + /// + /// This method waits for the completion of a task, or until `timeout` elapses. It is not + /// interruptible. Returns the number of jiffies left when the task completed, or [`None`] if + /// `timeout` elapsed first. + /// + /// See also [`Completion::complete_all`]. + #[inline] + pub fn wait_for_completion_timeout(&self, timeout: Jiffies) -> Option<Jiffies> { + // SAFETY: `self.as_raw()` is a pointer to a valid `struct completion`. + match unsafe { bindings::wait_for_completion_timeout(self.as_raw(), timeout) } { + 0 => None, + remaining => Some(remaining), + } + } }
--
2.55.0