Skip to contentSkip to conversation

rust: sync: Accept errors for Lock payload data

PATCH Applied Philipp Stanner 5 participants 5 messages Oct 22 – Nov 5

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 rust: sync: Accept errors for Lock payload data

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.

---
Changes in v4:
- Do imports at appropriate places. (Gary)
- Remove unnecessary changes from pin_init to try_pin_init. (Gary)
- re-run rustfmt.

Changes in v3:
- Vertical formatting for imports

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          | 28 +++++++++++++++++++++-------
 rust/kernel/sync/lock/spinlock.rs |  5 +----
 2 files changed, 22 insertions(+), 11 deletions(-)
rust/kernel/sync/lock.rs +21 -7
index 10b6b5e9b024..05a123fff0ae 100644
@@ -7,11 +7,22 @@
 
 use super::LockClassKey;
 use crate::{
-    str::{CStr, CStrExt as _},
+    prelude::*,
+    str::CStr,
     types::{NotThreadSafe, Opaque, ScopeGuard},
 };
-use core::{cell::UnsafeCell, marker::PhantomPinned, pin::Pin};
-use pin_init::{pin_data, pin_init, PinInit, Wrapper};
+
+use core::{
+    cell::UnsafeCell,
+    convert::Infallible,
+    marker::PhantomPinned,
+    pin::Pin, //
+};
+use pin_init::{
+    pin_data,
+    PinInit,
+    Wrapper, //
+};
 
 pub mod mutex;
 pub mod spinlock;
@@ -128,11 +139,14 @@ unsafe impl<T: ?Sized + Send, B: Backend> Sync for Lock<T, B> {}
 
 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 +155,7 @@ pub fn new(
             state <- Opaque::ffi_init(|slot| unsafe {
                 B::init(slot, name.as_char_ptr(), key.as_ptr())
             }),
-        })
+        }? E)
     }
 }
 
rust/kernel/sync/lock/spinlock.rs +1 -4
index 6a71bb3a5c45..b8b7492a99e5 100644
@@ -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.
 ///

base-commit: 7be4bc52fd9e0e55dc9a2e0232b89427d2c4b614

Signature · 1 line
2.56.0
maintainer Reviewed-by on v4 1/1
#1 lore raw
Quoted 8 lines · Philipp Stanner

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.

Boqun, are you ok with merging this through drm-rust-next for JobQueue, once we get Gary's reviewed-by?

maintainer
#2 lore raw
Quoted 5 lines · Alice Ryhl
Quoted 4 lines · Philipp Stanner

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.

Quoted 8 lines
Quoted 4 lines

Add support for the lock base class so that it works with try_pin_init!(). Adjust spinlock and mutex accordingly.

Boqun, are you ok with merging this through drm-rust-next for JobQueue, once we get Gary's reviewed-by?

Yes ;-)

Regards,
Boqun

Quoted 1 line
maintainer Reviewed-by on v4 1/1
#3 lore raw
In reply to Philipp Stanner
Quoted 8 lines · Philipp Stanner

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.

Quoted 16 lines

---
Changes in v4:
- Do imports at appropriate places. (Gary)
- Remove unnecessary changes from pin_init to try_pin_init. (Gary)
- re-run rustfmt.

Changes in v3:
- Vertical formatting for imports

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          | 28 +++++++++++++++++++++-------
 rust/kernel/sync/lock/spinlock.rs |  5 +----
 2 files changed, 22 insertions(+), 11 deletions(-)
reviewer Applied
#4 lore raw
In reply to Philipp Stanner
Quoted 1 line · Philipp Stanner

[PATCH v4] rust: sync: Accept errors for Lock payload data

Applied, thanks!

Branch: drm-rust-next
Tree: https://gitlab.freedesktop.org/drm/rust/kernel.git

[1/1] rust: sync: Accept errors for Lock payload data

      commit: 7a6bc132f1b5

The patch will appear in the next linux-next integration (typically within 24 hours on weekdays).

The patch is queued up for the upcoming merge window for the next major kernel release.