From: Danilo Krummrich <dakr@kernel.org>
To: boqun@kernel.org, gary@garyguo.net, aliceryhl@google.com,
lyude@redhat.com, daniel.almeida@collabora.com,
work@onurozkan.dev, ojeda@kernel.org, bjorn3_gh@protonmail.com,
lossin@kernel.org, a.hindborg@kernel.org, tmgross@umich.edu,
dakr@kernel.org, tamird@kernel.org, acourbot@nvidia.com,
viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
linux-fsdevel@vger.kernel.org
Subject: [PATCH v3 4/5] rust: sync: condvar: use task::schedule_timeout()
Date: Tue, 17 Nov 2026 01:14:45 +0100 [thread overview]
Message-ID: <20261117001507.1611984-5-dakr@kernel.org> (raw)
In-Reply-To: <20261117001507.1611984-1-dakr@kernel.org>
Convert CondVar::wait_internal() from calling
bindings::schedule_timeout() directly to using the safe
task::schedule_timeout() wrapper. This removes the unsafe call from
CondVar.
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
rust/kernel/sync/condvar.rs | 42 ++++++++++++++++++-------------------
1 file changed, 20 insertions(+), 22 deletions(-)
diff --git a/rust/kernel/sync/condvar.rs b/rust/kernel/sync/condvar.rs
index 2deeaf2038e4..dae07dc140c5 100644
--- a/rust/kernel/sync/condvar.rs
+++ b/rust/kernel/sync/condvar.rs
@@ -16,7 +16,7 @@
prelude::*,
str::CStr,
task::{
- MAX_SCHEDULE_TIMEOUT,
+ self,
TASK_FREEZABLE,
TASK_INTERRUPTIBLE,
TASK_UNINTERRUPTIBLE, //
@@ -107,11 +107,10 @@ fn wait_internal<T: ?Sized, B: Backend>(
&self,
wait_state: c_int,
guard: &mut Guard<'_, T, B>,
- timeout_in_jiffies: c_long,
- ) -> c_long {
+ timeout: Delta<Jiffy>,
+ ) -> Delta<Jiffy> {
self.wq.wait_once_exclusive(wait_state, || {
- // SAFETY: Switches to another thread. The timeout can be any number.
- guard.do_unlocked(|| unsafe { bindings::schedule_timeout(timeout_in_jiffies) })
+ guard.do_unlocked(|| task::schedule_timeout(timeout))
})
}
@@ -122,7 +121,11 @@ fn wait_internal<T: ?Sized, B: Backend>(
/// [`CondVar::notify_one`] or [`CondVar::notify_all`]. Note that it may also wake up
/// spuriously.
pub fn wait<T: ?Sized, B: Backend>(&self, guard: &mut Guard<'_, T, B>) {
- self.wait_internal(TASK_UNINTERRUPTIBLE, guard, MAX_SCHEDULE_TIMEOUT);
+ self.wait_internal(
+ TASK_UNINTERRUPTIBLE,
+ guard,
+ Delta::from_jiffies(task::MAX_SCHEDULE_TIMEOUT),
+ );
}
/// Releases the lock and waits for a notification in interruptible mode.
@@ -133,7 +136,11 @@ pub fn wait<T: ?Sized, B: Backend>(&self, guard: &mut Guard<'_, T, B>) {
/// Returns whether there is a signal pending.
#[must_use = "wait_interruptible returns if a signal is pending, so the caller must check the return value"]
pub fn wait_interruptible<T: ?Sized, B: Backend>(&self, guard: &mut Guard<'_, T, B>) -> bool {
- self.wait_internal(TASK_INTERRUPTIBLE, guard, MAX_SCHEDULE_TIMEOUT);
+ self.wait_internal(
+ TASK_INTERRUPTIBLE,
+ guard,
+ Delta::from_jiffies(task::MAX_SCHEDULE_TIMEOUT),
+ );
crate::current!().signal_pending()
}
@@ -151,7 +158,7 @@ pub fn wait_interruptible_freezable<T: ?Sized, B: Backend>(
self.wait_internal(
TASK_INTERRUPTIBLE | TASK_FREEZABLE,
guard,
- MAX_SCHEDULE_TIMEOUT,
+ Delta::from_jiffies(task::MAX_SCHEDULE_TIMEOUT),
);
crate::current!().signal_pending()
}
@@ -169,21 +176,12 @@ pub fn wait_interruptible_timeout<T: ?Sized, B: Backend>(
guard: &mut Guard<'_, T, B>,
timeout: Delta<Jiffy>,
) -> CondVarTimeoutResult {
- let jiffies = timeout.as_jiffies();
- let res = self.wait_internal(
- TASK_INTERRUPTIBLE,
- guard,
- jiffies.clamp(0, MAX_SCHEDULE_TIMEOUT),
- );
+ let remaining = self.wait_internal(TASK_INTERRUPTIBLE, guard, timeout);
- match (res, crate::current!().signal_pending()) {
- (remainder, true) => CondVarTimeoutResult::Signal {
- remainder: Delta::from_jiffies(remainder),
- },
- (0, false) => CondVarTimeoutResult::Timeout,
- (remainder, false) => CondVarTimeoutResult::Woken {
- remainder: Delta::from_jiffies(remainder),
- },
+ match (remaining, current!().signal_pending()) {
+ (remainder, true) => CondVarTimeoutResult::Signal { remainder },
+ (Delta::ZERO, false) => CondVarTimeoutResult::Timeout,
+ (remainder, false) => CondVarTimeoutResult::Woken { remainder },
}
}
--
2.56.1
next prev parent reply other threads:[~2026-11-17 0:15 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 0:14 [PATCH v3 0/5] rust: sync: add WaitQueue infrastructure Danilo Krummrich
2026-11-17 0:14 ` [PATCH v3 1/5] rust: task: add safe schedule_timeout() wrapper Danilo Krummrich
2026-11-17 0:45 ` FUJITA Tomonori
2026-11-17 11:40 ` Gary Guo
2026-11-17 11:56 ` Danilo Krummrich
2026-11-17 12:02 ` Gary Guo
2026-11-17 13:14 ` Danilo Krummrich
2026-11-17 14:16 ` Alice Ryhl
2026-11-17 0:14 ` [PATCH v3 2/5] rust: sync: add WaitQueue infrastructure Danilo Krummrich
2026-11-17 11:59 ` Gary Guo
2026-11-17 14:16 ` Alice Ryhl
2026-11-17 0:14 ` [PATCH v3 3/5] rust: sync: convert CondVar and PollCondVar to use WaitQueue Danilo Krummrich
2026-11-17 14:16 ` Alice Ryhl
2026-11-17 0:14 ` Danilo Krummrich [this message]
2026-11-17 14:16 ` [PATCH v3 4/5] rust: sync: condvar: use task::schedule_timeout() Alice Ryhl
2026-11-17 22:06 ` FUJITA Tomonori
2026-11-17 0:14 ` [PATCH v3 5/5] rust: sync: condvar: add non-exclusive freezable waiting Danilo Krummrich
2026-11-17 14:16 ` Alice Ryhl
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261117001507.1611984-5-dakr@kernel.org \
--to=dakr@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=brauner@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=lyude@redhat.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=viro@zeniv.linux.org.uk \
--cc=work@onurozkan.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.