mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: Ingo Molnar <mingo@kernel.org>, Peter Zijlstra <peterz@infradead.org>
Cc: "Will Deacon" <will@kernel.org>,
	"Waiman Long" <longman@redhat.com>,
	linux-kernel@vger.kernel.org, "Alice Ryhl" <aliceryhl@google.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Mitchell Levy" <levymitchell0@gmail.com>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Christian Brauner" <brauner@kernel.org>,
	"Martin Rodriguez Reboredo" <yakoyoku@gmail.com>,
	rust-for-linux@vger.kernel.org (open list:RUST)
Subject: [PATCH locking 09/11] rust: sync: condvar: Add wait_interruptible_freezable()
Date: Fri,  7 Mar 2025 15:26:59 -0800	[thread overview]
Message-ID: <20250307232717.1759087-10-boqun.feng@gmail.com> (raw)
In-Reply-To: <20250307232717.1759087-1-boqun.feng@gmail.com>

From: Alice Ryhl <aliceryhl@google.com>

To support waiting for a `CondVar` as a freezable process, add a
wait_interruptible_freezable() function.

Binder needs this function in the appropriate places to freeze a process
where some of its threads are blocked on the Binder driver.

[Boqun: Capitalize the title, reword the commit log and rephrase the
function doc comment with the impersonal style to align with rest of the
file]

Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Link: https://lore.kernel.org/r/20250204-condvar-freeze-v2-1-804483096260@google.com
---
 rust/kernel/sync/condvar.rs | 23 ++++++++++++++++++++++-
 rust/kernel/task.rs         |  2 ++
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/sync/condvar.rs b/rust/kernel/sync/condvar.rs
index 7df565038d7d..5c1e546a26c3 100644
--- a/rust/kernel/sync/condvar.rs
+++ b/rust/kernel/sync/condvar.rs
@@ -11,7 +11,9 @@
     init::PinInit,
     pin_init,
     str::CStr,
-    task::{MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE, TASK_NORMAL, TASK_UNINTERRUPTIBLE},
+    task::{
+        MAX_SCHEDULE_TIMEOUT, TASK_FREEZABLE, TASK_INTERRUPTIBLE, TASK_NORMAL, TASK_UNINTERRUPTIBLE,
+    },
     time::Jiffies,
     types::Opaque,
 };
@@ -159,6 +161,25 @@ pub fn wait_interruptible<T: ?Sized, B: Backend>(&self, guard: &mut Guard<'_, T,
         crate::current!().signal_pending()
     }
 
+    /// Releases the lock and waits for a notification in interruptible and freezable mode.
+    ///
+    /// 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 returns if a signal is pending, so the caller must check the return value"]
+    pub fn wait_interruptible_freezable<T: ?Sized, B: Backend>(
+        &self,
+        guard: &mut Guard<'_, T, B>,
+    ) -> bool {
+        self.wait_internal(
+            TASK_INTERRUPTIBLE | TASK_FREEZABLE,
+            guard,
+            MAX_SCHEDULE_TIMEOUT,
+        );
+        crate::current!().signal_pending()
+    }
+
     /// 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
diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs
index 07bc22a7645c..ea43a3b8d9c5 100644
--- a/rust/kernel/task.rs
+++ b/rust/kernel/task.rs
@@ -23,6 +23,8 @@
 pub const TASK_INTERRUPTIBLE: c_int = bindings::TASK_INTERRUPTIBLE as c_int;
 /// Bitmask for tasks that are sleeping in an uninterruptible state.
 pub const TASK_UNINTERRUPTIBLE: c_int = bindings::TASK_UNINTERRUPTIBLE as c_int;
+/// Bitmask for tasks that are sleeping in a freezable state.
+pub const TASK_FREEZABLE: c_int = bindings::TASK_FREEZABLE as c_int;
 /// Convenience constant for waking up tasks regardless of whether they are in interruptible or
 /// uninterruptible sleep.
 pub const TASK_NORMAL: c_uint = bindings::TASK_NORMAL as c_uint;
-- 
2.47.1


  parent reply	other threads:[~2025-03-07 23:29 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-26  4:12 [GIT PULL] LOCKDEP and Rust locking changes for v6.15 Boqun Feng
2025-02-28 18:57 ` Boqun Feng
2025-03-03 16:52   ` Waiman Long
2025-03-03 18:42 ` Peter Zijlstra
2025-03-07 23:26 ` [PATCH locking 00/11] " Boqun Feng
2025-03-07 23:26   ` [PATCH locking 01/11] locking/rtmutex: Use struct keyword in kernel-doc comment Boqun Feng
2025-03-08  0:08     ` [tip: locking/core] locking/rtmutex: Use the 'struct' " tip-bot2 for Randy Dunlap
2025-03-07 23:26   ` [PATCH locking 02/11] locking/semaphore: Use wake_q to wake up processes outside lock critical section Boqun Feng
2025-03-08  0:08     ` [tip: locking/core] " tip-bot2 for Waiman Long
2025-03-07 23:26   ` [PATCH locking 03/11] locking/lock_events: Add locking events for rtmutex slow paths Boqun Feng
2025-03-08  0:08     ` [tip: locking/core] " tip-bot2 for Waiman Long
2025-03-07 23:26   ` [PATCH locking 04/11] locking/lock_events: Add locking events for lockdep Boqun Feng
2025-03-08  0:08     ` [tip: locking/core] " tip-bot2 for Waiman Long
2025-03-07 23:26   ` [PATCH locking 05/11] locking/lockdep: Disable KASAN instrumentation of lockdep.c Boqun Feng
2025-03-08  0:08     ` [tip: locking/core] " tip-bot2 for Waiman Long
2025-03-07 23:26   ` [PATCH locking 06/11] locking/lockdep: Add kasan_check_byte() check in lock_acquire() Boqun Feng
2025-03-08  0:08     ` [tip: locking/core] " tip-bot2 for Waiman Long
2025-03-07 23:26   ` [PATCH locking 07/11] rust: sync: Add accessor for the lock behind a given guard Boqun Feng
2025-03-08  0:08     ` [tip: locking/core] " tip-bot2 for Alice Ryhl
2025-03-07 23:26   ` [PATCH locking 08/11] rust: sync: lock: Add an example for Guard::lock_ref() Boqun Feng
2025-03-08  0:08     ` [tip: locking/core] rust: sync: lock: Add an example for Guard:: Lock_ref() tip-bot2 for Boqun Feng
2025-03-07 23:26   ` Boqun Feng [this message]
2025-03-08  0:08     ` [tip: locking/core] rust: sync: condvar: Add wait_interruptible_freezable() tip-bot2 for Alice Ryhl
2025-03-07 23:27   ` [PATCH locking 10/11] rust: lockdep: Remove support for dynamically allocated LockClassKeys Boqun Feng
2025-03-08  0:08     ` [tip: locking/core] " tip-bot2 for Mitchell Levy
2025-03-07 23:27   ` [PATCH locking 11/11] rust: lockdep: Use Pin for all LockClassKey usages Boqun Feng
2025-03-08  0:08     ` [tip: locking/core] " tip-bot2 for Mitchell Levy
2025-03-08  0:01   ` [PATCH locking 00/11] LOCKDEP and Rust locking changes for v6.15 Ingo Molnar
2025-03-08  0:04     ` Boqun Feng
2025-03-08  0:11       ` Ingo Molnar
2025-03-08  0:11         ` Boqun Feng

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=20250307232717.1759087-10-boqun.feng@gmail.com \
    --to=boqun.feng@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=brauner@kernel.org \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=levymitchell0@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mingo@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    --cc=will@kernel.org \
    --cc=yakoyoku@gmail.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®