From: Philipp Stanner <phasta@kernel.org>
To: "Peter Zijlstra" <peterz@infradead.org>,
"Ingo Molnar" <mingo@redhat.com>, "Will Deacon" <will@kernel.org>,
"Boqun Feng" <boqun@kernel.org>,
"Waiman Long" <longman@redhat.com>, "Gary Guo" <gary@garyguo.net>,
"Alice Ryhl" <aliceryhl@google.com>,
"Lyude Paul" <lyude@redhat.com>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Onur Özkan" <work@onurozkan.dev>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
Philipp Stanner <phasta@kernel.org>
Subject: [PATCH v2] rust: sync: Accept errors for Lock payload data
Date: Wed, 30 Sep 2026 17:41:02 +0200 [thread overview]
Message-ID: <20260930154101.1885705-2-phasta@kernel.org> (raw)
The `Lock` baseclass currently does not allow for fallible user-data.
This can cause conflicts when used with other primitives, for example
when a lock shall be pin-initialized with payload data that is fallible.
Add support for the lock base class so that it works with
try_pin_init!(). Adjust spinlock and mutex accordingly.
Signed-off-by: Philipp Stanner <phasta@kernel.org>
---
Changes in v2:
- Don't import prelude in doctests. (Gary)
- Keep using pin_init! over try_pin_init! in Lock::new(). (Gary)
---
rust/kernel/sync/lock.rs | 19 +++++++++++++------
rust/kernel/sync/lock/mutex.rs | 6 +++---
rust/kernel/sync/lock/spinlock.rs | 17 +++++++----------
3 files changed, 23 insertions(+), 19 deletions(-)
diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
index 10b6b5e9b024..5467f1d1af55 100644
--- a/rust/kernel/sync/lock.rs
+++ b/rust/kernel/sync/lock.rs
@@ -7,11 +7,13 @@
use super::LockClassKey;
use crate::{
- str::{CStr, CStrExt as _},
+ str::CStr,
types::{NotThreadSafe, Opaque, ScopeGuard},
};
use core::{cell::UnsafeCell, marker::PhantomPinned, pin::Pin};
-use pin_init::{pin_data, pin_init, PinInit, Wrapper};
+use pin_init::{pin_data, PinInit, Wrapper};
+
+use kernel::prelude::*;
pub mod mutex;
pub mod spinlock;
@@ -126,13 +128,18 @@ unsafe impl<T: ?Sized + Send, B: Backend> Send for Lock<T, B> {}
// data it protects is `Send`.
unsafe impl<T: ?Sized + Send, B: Backend> Sync for Lock<T, B> {}
+use core::convert::Infallible;
+
impl<T, B: Backend> Lock<T, B> {
/// Constructs a new lock initialiser.
- pub fn new(
- t: impl PinInit<T>,
+ pub fn new<E>(
+ t: impl PinInit<T, E>,
name: &'static CStr,
key: Pin<&'static LockClassKey>,
- ) -> impl PinInit<Self> {
+ ) -> impl PinInit<Self, E>
+ where
+ E: From<Infallible>,
+ {
pin_init!(Self {
data <- UnsafeCell::pin_init(t),
_pin: PhantomPinned,
@@ -141,7 +148,7 @@ pub fn new(
state <- Opaque::ffi_init(|slot| unsafe {
B::init(slot, name.as_char_ptr(), key.as_ptr())
}),
- })
+ }? E)
}
}
diff --git a/rust/kernel/sync/lock/mutex.rs b/rust/kernel/sync/lock/mutex.rs
index cda0203efefb..38d8fb9dad5d 100644
--- a/rust/kernel/sync/lock/mutex.rs
+++ b/rust/kernel/sync/lock/mutex.rs
@@ -49,8 +49,8 @@ macro_rules! new_mutex {
/// }
///
/// impl Example {
-/// fn new() -> impl PinInit<Self> {
-/// pin_init!(Self {
+/// fn new() -> impl PinInit<Self, Error> {
+/// try_pin_init!(Self {
/// c: 10,
/// d <- new_mutex!(Inner { a: 20, b: 30 }),
/// })
@@ -58,7 +58,7 @@ macro_rules! new_mutex {
/// }
///
/// // Allocate a boxed `Example`.
-/// let e = KBox::pin_init(Example::new(), GFP_KERNEL)?;
+/// let e = KBox::try_pin_init(Example::new(), GFP_KERNEL)?;
/// assert_eq!(e.c, 10);
/// assert_eq!(e.d.lock().a, 20);
/// assert_eq!(e.d.lock().b, 30);
diff --git a/rust/kernel/sync/lock/spinlock.rs b/rust/kernel/sync/lock/spinlock.rs
index aafc80125f59..222443fa1c6c 100644
--- a/rust/kernel/sync/lock/spinlock.rs
+++ b/rust/kernel/sync/lock/spinlock.rs
@@ -4,10 +4,7 @@
//!
//! This module allows Rust code to use the kernel's `spinlock_t`.
use super::*;
-use crate::{
- interrupt::LocalInterruptDisabled,
- prelude::*, //
-};
+use crate::interrupt::LocalInterruptDisabled;
/// Creates a [`SpinLock`] initialiser with the given name and a newly-created lock class.
///
@@ -52,8 +49,8 @@ macro_rules! new_spinlock {
/// }
///
/// impl Example {
-/// fn new() -> impl PinInit<Self> {
-/// pin_init!(Self {
+/// fn new() -> impl PinInit<Self, Error> {
+/// try_pin_init!(Self {
/// c: 10,
/// d <- new_spinlock!(Inner { a: 20, b: 30 }),
/// })
@@ -199,8 +196,8 @@ macro_rules! new_spinlock_irq {
/// }
///
/// impl Example {
-/// fn new() -> impl PinInit<Self> {
-/// pin_init!(Self {
+/// fn new() -> impl PinInit<Self, Error> {
+/// try_pin_init!(Self {
/// c <- new_spinlock_irq!(Inner { a: 0, b: 10 }),
/// d <- new_spinlock_irq!(Inner { a: 20, b: 30 }),
/// })
@@ -244,8 +241,8 @@ macro_rules! new_spinlock_irq {
/// }
///
/// impl Example {
-/// fn new() -> impl PinInit<Self> {
-/// pin_init!(Self {
+/// fn new() -> impl PinInit<Self, Error> {
+/// try_pin_init!(Self {
/// inner <- new_spinlock_irq!(Inner { a: 20 }),
/// })
/// }
base-commit: f9a6960c7b27c03becd50a2a7620273d80e4040d
--
2.55.0
reply other threads:[~2026-09-30 15:41 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20260930154101.1885705-2-phasta@kernel.org \
--to=phasta@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=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=longman@redhat.com \
--cc=lossin@kernel.org \
--cc=lyude@redhat.com \
--cc=mingo@redhat.com \
--cc=ojeda@kernel.org \
--cc=peterz@infradead.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=will@kernel.org \
--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 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®