* [PATCH v2 1/5] devres: move struct devres_node into base.h
2026-02-13 22:07 [PATCH v2 0/5] Use struct devres_node in Devres<T> Danilo Krummrich
@ 2026-02-13 22:07 ` Danilo Krummrich
2026-02-13 22:07 ` [PATCH v2 2/5] devres: export devres_node_init() and devres_node_add() Danilo Krummrich
` (5 subsequent siblings)
6 siblings, 0 replies; 9+ messages in thread
From: Danilo Krummrich @ 2026-02-13 22:07 UTC (permalink / raw)
To: gregkh, rafael, ojeda, boqun.feng, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross
Cc: driver-core, rust-for-linux, linux-kernel, Danilo Krummrich
Move struct devres_node into base.h, such that we can access it from the
Rust devres code.
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
drivers/base/base.h | 12 ++++++++++++
drivers/base/devres.c | 12 ------------
2 files changed, 12 insertions(+), 12 deletions(-)
diff --git a/drivers/base/base.h b/drivers/base/base.h
index 8c2175820da9..5e0e7eefa405 100644
--- a/drivers/base/base.h
+++ b/drivers/base/base.h
@@ -222,6 +222,18 @@ static inline void device_set_driver(struct device *dev, const struct device_dri
WRITE_ONCE(dev->driver, (struct device_driver *)drv);
}
+struct devres_node;
+typedef void (*dr_node_release_t)(struct device *dev, struct devres_node *node);
+typedef void (*dr_node_free_t)(struct devres_node *node);
+
+struct devres_node {
+ struct list_head entry;
+ dr_node_release_t release;
+ dr_node_free_t free_node;
+ const char *name;
+ size_t size;
+};
+
void devres_for_each_res(struct device *dev, dr_release_t release,
dr_match_t match, void *match_data,
void (*fn)(struct device *, void *, void *),
diff --git a/drivers/base/devres.c b/drivers/base/devres.c
index 0fca73b56168..7c9ef6fc6827 100644
--- a/drivers/base/devres.c
+++ b/drivers/base/devres.c
@@ -16,18 +16,6 @@
#include "base.h"
#include "trace.h"
-struct devres_node;
-typedef void (*dr_node_release_t)(struct device *dev, struct devres_node *node);
-typedef void (*dr_node_free_t)(struct devres_node *node);
-
-struct devres_node {
- struct list_head entry;
- dr_node_release_t release;
- dr_node_free_t free_node;
- const char *name;
- size_t size;
-};
-
struct devres {
struct devres_node node;
dr_release_t release;
--
2.53.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH v2 2/5] devres: export devres_node_init() and devres_node_add()
2026-02-13 22:07 [PATCH v2 0/5] Use struct devres_node in Devres<T> Danilo Krummrich
2026-02-13 22:07 ` [PATCH v2 1/5] devres: move struct devres_node into base.h Danilo Krummrich
@ 2026-02-13 22:07 ` Danilo Krummrich
2026-02-13 22:07 ` [PATCH v2 3/5] devres: add devres_node_remove() Danilo Krummrich
` (4 subsequent siblings)
6 siblings, 0 replies; 9+ messages in thread
From: Danilo Krummrich @ 2026-02-13 22:07 UTC (permalink / raw)
To: gregkh, rafael, ojeda, boqun.feng, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross
Cc: driver-core, rust-for-linux, linux-kernel, Danilo Krummrich
Export devres_node_init() and devres_node_add() through base.h, such
that we can access is from the Rust devres code.
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
drivers/base/base.h | 3 +++
drivers/base/devres.c | 8 ++++----
2 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/drivers/base/base.h b/drivers/base/base.h
index 5e0e7eefa405..0ad52a847bd0 100644
--- a/drivers/base/base.h
+++ b/drivers/base/base.h
@@ -234,6 +234,9 @@ struct devres_node {
size_t size;
};
+void devres_node_init(struct devres_node *node, dr_node_release_t release,
+ dr_node_free_t free_node);
+void devres_node_add(struct device *dev, struct devres_node *node);
void devres_for_each_res(struct device *dev, dr_release_t release,
dr_match_t match, void *match_data,
void (*fn)(struct device *, void *, void *),
diff --git a/drivers/base/devres.c b/drivers/base/devres.c
index 7c9ef6fc6827..aa39e87082a1 100644
--- a/drivers/base/devres.c
+++ b/drivers/base/devres.c
@@ -36,9 +36,9 @@ struct devres_group {
/* -- 8 pointers */
};
-static void devres_node_init(struct devres_node *node,
- dr_node_release_t release,
- dr_node_free_t free_node)
+void devres_node_init(struct devres_node *node,
+ dr_node_release_t release,
+ dr_node_free_t free_node)
{
INIT_LIST_HEAD(&node->entry);
node->release = release;
@@ -256,7 +256,7 @@ void devres_free(void *res)
}
EXPORT_SYMBOL_GPL(devres_free);
-static void devres_node_add(struct device *dev, struct devres_node *node)
+void devres_node_add(struct device *dev, struct devres_node *node)
{
guard(spinlock_irqsave)(&dev->devres_lock);
--
2.53.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH v2 3/5] devres: add devres_node_remove()
2026-02-13 22:07 [PATCH v2 0/5] Use struct devres_node in Devres<T> Danilo Krummrich
2026-02-13 22:07 ` [PATCH v2 1/5] devres: move struct devres_node into base.h Danilo Krummrich
2026-02-13 22:07 ` [PATCH v2 2/5] devres: export devres_node_init() and devres_node_add() Danilo Krummrich
@ 2026-02-13 22:07 ` Danilo Krummrich
2026-02-13 22:07 ` [PATCH v2 4/5] devres: rename and export set_node_dbginfo() Danilo Krummrich
` (3 subsequent siblings)
6 siblings, 0 replies; 9+ messages in thread
From: Danilo Krummrich @ 2026-02-13 22:07 UTC (permalink / raw)
To: gregkh, rafael, ojeda, boqun.feng, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross
Cc: driver-core, rust-for-linux, linux-kernel, Danilo Krummrich
When the Rust Devres<T> container type is dropped we need a way to
remove the embedded struct devres_node from the device's node list.
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
drivers/base/base.h | 1 +
drivers/base/devres.c | 16 ++++++++++++++++
2 files changed, 17 insertions(+)
diff --git a/drivers/base/base.h b/drivers/base/base.h
index 0ad52a847bd0..93b2020baa99 100644
--- a/drivers/base/base.h
+++ b/drivers/base/base.h
@@ -237,6 +237,7 @@ struct devres_node {
void devres_node_init(struct devres_node *node, dr_node_release_t release,
dr_node_free_t free_node);
void devres_node_add(struct device *dev, struct devres_node *node);
+bool devres_node_remove(struct device *dev, struct devres_node *node);
void devres_for_each_res(struct device *dev, dr_release_t release,
dr_match_t match, void *match_data,
void (*fn)(struct device *, void *, void *),
diff --git a/drivers/base/devres.c b/drivers/base/devres.c
index aa39e87082a1..bf51404e2bf4 100644
--- a/drivers/base/devres.c
+++ b/drivers/base/devres.c
@@ -361,6 +361,22 @@ void *devres_get(struct device *dev, void *new_res,
}
EXPORT_SYMBOL_GPL(devres_get);
+bool devres_node_remove(struct device *dev, struct devres_node *node)
+{
+ struct devres_node *__node;
+
+ guard(spinlock_irqsave)(&dev->devres_lock);
+ list_for_each_entry_reverse(__node, &dev->devres_head, entry) {
+ if (__node == node) {
+ list_del_init(&node->entry);
+ devres_log(dev, node, "REM");
+ return true;
+ }
+ }
+
+ return false;
+}
+
/**
* devres_remove - Find a device resource and remove it
* @dev: Device to find resource from
--
2.53.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH v2 4/5] devres: rename and export set_node_dbginfo()
2026-02-13 22:07 [PATCH v2 0/5] Use struct devres_node in Devres<T> Danilo Krummrich
` (2 preceding siblings ...)
2026-02-13 22:07 ` [PATCH v2 3/5] devres: add devres_node_remove() Danilo Krummrich
@ 2026-02-13 22:07 ` Danilo Krummrich
2026-02-13 22:07 ` [PATCH v2 5/5] rust: devres: embed struct devres_node directly Danilo Krummrich
` (2 subsequent siblings)
6 siblings, 0 replies; 9+ messages in thread
From: Danilo Krummrich @ 2026-02-13 22:07 UTC (permalink / raw)
To: gregkh, rafael, ojeda, boqun.feng, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross
Cc: driver-core, rust-for-linux, linux-kernel, Danilo Krummrich
Rename set_node_dbginfo() to devres_set_node_dbginfo() and export it
through base.h, such that we can access is from the Rust devres code.
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
drivers/base/base.h | 2 ++
drivers/base/devres.c | 14 +++++++-------
2 files changed, 9 insertions(+), 7 deletions(-)
diff --git a/drivers/base/base.h b/drivers/base/base.h
index 93b2020baa99..9aed7e2c78ef 100644
--- a/drivers/base/base.h
+++ b/drivers/base/base.h
@@ -238,6 +238,8 @@ void devres_node_init(struct devres_node *node, dr_node_release_t release,
dr_node_free_t free_node);
void devres_node_add(struct device *dev, struct devres_node *node);
bool devres_node_remove(struct device *dev, struct devres_node *node);
+void devres_set_node_dbginfo(struct devres_node *node, const char *name,
+ size_t size);
void devres_for_each_res(struct device *dev, dr_release_t release,
dr_match_t match, void *match_data,
void (*fn)(struct device *, void *, void *),
diff --git a/drivers/base/devres.c b/drivers/base/devres.c
index bf51404e2bf4..e36d2cc7723d 100644
--- a/drivers/base/devres.c
+++ b/drivers/base/devres.c
@@ -50,7 +50,7 @@ static inline void free_node(struct devres_node *node)
node->free_node(node);
}
-static void set_node_dbginfo(struct devres_node *node, const char *name,
+void devres_set_node_dbginfo(struct devres_node *node, const char *name,
size_t size)
{
node->name = name;
@@ -189,7 +189,7 @@ void *__devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid,
dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid);
if (unlikely(!dr))
return NULL;
- set_node_dbginfo(&dr->node, name, size);
+ devres_set_node_dbginfo(&dr->node, name, size);
return dr->data;
}
EXPORT_SYMBOL_GPL(__devres_alloc_node);
@@ -603,8 +603,8 @@ void *devres_open_group(struct device *dev, void *id, gfp_t gfp)
devres_node_init(&grp->node[0], &group_open_release, devres_group_free);
devres_node_init(&grp->node[1], &group_close_release, NULL);
- set_node_dbginfo(&grp->node[0], "grp<", 0);
- set_node_dbginfo(&grp->node[1], "grp>", 0);
+ devres_set_node_dbginfo(&grp->node[0], "grp<", 0);
+ devres_set_node_dbginfo(&grp->node[1], "grp>", 0);
grp->id = grp;
if (id)
grp->id = id;
@@ -792,7 +792,7 @@ int __devm_add_action(struct device *dev, void (*action)(void *), void *data, co
return -ENOMEM;
devres_node_init(&devres->node, devm_action_release, devm_action_free);
- set_node_dbginfo(&devres->node, name, sizeof(*devres));
+ devres_set_node_dbginfo(&devres->node, name, sizeof(*devres));
devres->action.data = data;
devres->action.action = action;
@@ -953,7 +953,7 @@ void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)
* This is named devm_kzalloc_release for historical reasons
* The initial implementation did not support kmalloc, only kzalloc
*/
- set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
+ devres_set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
devres_add(dev, dr->data);
return dr->data;
}
@@ -1024,7 +1024,7 @@ void *devm_krealloc(struct device *dev, void *ptr, size_t new_size, gfp_t gfp)
if (!new_dr)
return NULL;
- set_node_dbginfo(&new_dr->node, "devm_krealloc_release", new_size);
+ devres_set_node_dbginfo(&new_dr->node, "devm_krealloc_release", new_size);
/*
* The spinlock protects the linked list against concurrent
--
2.53.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH v2 5/5] rust: devres: embed struct devres_node directly
2026-02-13 22:07 [PATCH v2 0/5] Use struct devres_node in Devres<T> Danilo Krummrich
` (3 preceding siblings ...)
2026-02-13 22:07 ` [PATCH v2 4/5] devres: rename and export set_node_dbginfo() Danilo Krummrich
@ 2026-02-13 22:07 ` Danilo Krummrich
2026-02-16 8:29 ` Alice Ryhl
2026-03-12 15:12 ` [PATCH v2 0/5] Use struct devres_node in Devres<T> Greg KH
2026-03-18 0:12 ` Danilo Krummrich
6 siblings, 1 reply; 9+ messages in thread
From: Danilo Krummrich @ 2026-02-13 22:07 UTC (permalink / raw)
To: gregkh, rafael, ojeda, boqun.feng, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross
Cc: driver-core, rust-for-linux, linux-kernel, Danilo Krummrich
Currently, the Devres<T> container uses devm_add_action() to register a
devres callback.
devm_add_action() allocates a struct action_devres, which on top of
struct devres_node, just keeps a data pointer and release function
pointer.
This is an unnecessary indirection, given that analogous to struct
devres, the Devres<T> container can just embed a struct devres_node
directly without an additional allocation.
In contrast to struct devres, we don't need to force an alignment of
ARCH_DMA_MINALIGN (as struct devres does to account for the worst case)
since we have generics in Rust. I.e. the compiler already ensures
correct alignment of the embedded T in Devres<T>.
Thus, get rid of devm_add_action() and instead embed a struct
devres_node directly.
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
rust/kernel/devres.rs | 182 +++++++++++++++++++++++++++++++-----------
1 file changed, 135 insertions(+), 47 deletions(-)
diff --git a/rust/kernel/devres.rs b/rust/kernel/devres.rs
index 6afe196be42c..4c1efad758c7 100644
--- a/rust/kernel/devres.rs
+++ b/rust/kernel/devres.rs
@@ -23,9 +23,22 @@
rcu,
Arc, //
},
- types::ForeignOwnable,
+ types::{
+ ForeignOwnable,
+ Opaque, //
+ },
};
+/// Inner type that embeds a `struct devres_node` and the `Revocable<T>`.
+#[repr(C)]
+#[pin_data]
+struct Inner<T> {
+ #[pin]
+ node: Opaque<bindings::devres_node>,
+ #[pin]
+ data: Revocable<T>,
+}
+
/// This abstraction is meant to be used by subsystems to containerize [`Device`] bound resources to
/// manage their lifetime.
///
@@ -111,12 +124,63 @@
/// ```
pub struct Devres<T: Send> {
dev: ARef<Device>,
- /// Pointer to [`Self::devres_callback`].
- ///
- /// Has to be stored, since Rust does not guarantee to always return the same address for a
- /// function. However, the C API uses the address as a key.
- callback: unsafe extern "C" fn(*mut c_void),
- data: Arc<Revocable<T>>,
+ inner: Arc<Inner<T>>,
+}
+
+// Calling the FFI functions from the `base` module directly from the `Devres<T>` impl may result in
+// them being called directly from driver modules. This happens since the Rust compiler will use
+// monomorphisation, so it might happen that functions are instantiated within the calling driver
+// module. For now, work around this with `#[inline(never)]` helpers.
+//
+// TODO: Remove once a more generic solution has been implemented. For instance, we may be able to
+// leverage `bindgen` to take care of this depending on whether a symbol is (already) exported.
+mod base {
+ use kernel::{
+ bindings,
+ prelude::*, //
+ };
+
+ #[inline(never)]
+ #[allow(clippy::missing_safety_doc)]
+ pub(super) unsafe fn devres_node_init(
+ node: *mut bindings::devres_node,
+ release: bindings::dr_node_release_t,
+ free: bindings::dr_node_free_t,
+ ) {
+ // SAFETY: Safety requirements are the same as `bindings::devres_node_init`.
+ unsafe { bindings::devres_node_init(node, release, free) }
+ }
+
+ #[inline(never)]
+ #[allow(clippy::missing_safety_doc)]
+ pub(super) unsafe fn devres_set_node_dbginfo(
+ node: *mut bindings::devres_node,
+ name: *const c_char,
+ size: usize,
+ ) {
+ // SAFETY: Safety requirements are the same as `bindings::devres_set_node_dbginfo`.
+ unsafe { bindings::devres_set_node_dbginfo(node, name, size) }
+ }
+
+ #[inline(never)]
+ #[allow(clippy::missing_safety_doc)]
+ pub(super) unsafe fn devres_node_add(
+ dev: *mut bindings::device,
+ node: *mut bindings::devres_node,
+ ) {
+ // SAFETY: Safety requirements are the same as `bindings::devres_node_add`.
+ unsafe { bindings::devres_node_add(dev, node) }
+ }
+
+ #[inline(never)]
+ #[allow(clippy::missing_safety_doc)]
+ pub(super) unsafe fn devres_node_remove(
+ dev: *mut bindings::device,
+ node: *mut bindings::devres_node,
+ ) -> bool {
+ // SAFETY: Safety requirements are the same as `bindings::devres_node_remove`.
+ unsafe { bindings::devres_node_remove(dev, node) }
+ }
}
impl<T: Send> Devres<T> {
@@ -128,58 +192,82 @@ pub fn new<E>(dev: &Device<Bound>, data: impl PinInit<T, E>) -> Result<Self>
where
Error: From<E>,
{
- let callback = Self::devres_callback;
- let data = Arc::pin_init(Revocable::new(data), GFP_KERNEL)?;
- let devres_data = data.clone();
+ let inner = Arc::pin_init::<Error>(
+ try_pin_init!(Inner {
+ node <- Opaque::ffi_init(|node: *mut bindings::devres_node| {
+ // SAFETY: `node` is a valid pointer to an uninitialized `struct devres_node`.
+ unsafe {
+ base::devres_node_init(
+ node,
+ Some(Self::devres_node_release),
+ Some(Self::devres_node_free_node),
+ )
+ };
+
+ // SAFETY: `node` is a valid pointer to an uninitialized `struct devres_node`.
+ unsafe {
+ base::devres_set_node_dbginfo(
+ node,
+ // TODO: Use `core::any::type_name::<T>()` once we are able to convert
+ // the `&str` to a NULL terminated `&CStr` without additional
+ // allocation.
+ c"Devres<T>".as_char_ptr(),
+ core::mem::size_of::<Revocable<T>>(),
+ )
+ };
+ }),
+ data <- Revocable::new(data),
+ }),
+ GFP_KERNEL,
+ )?;
// SAFETY:
- // - `dev.as_raw()` is a pointer to a valid bound device.
- // - `data` is guaranteed to be a valid for the duration of the lifetime of `Self`.
- // - `devm_add_action()` is guaranteed not to call `callback` for the entire lifetime of
- // `dev`.
- to_result(unsafe {
- bindings::devm_add_action(
- dev.as_raw(),
- Some(callback),
- Arc::as_ptr(&data).cast_mut().cast(),
- )
- })?;
-
- // `devm_add_action()` was successful and has consumed the reference count.
- core::mem::forget(devres_data);
+ // - `dev` is a valid pointer to a bound `struct device`.
+ // - `node` is a valid pointer to a `struct devres_node`.
+ // - `devres_node_add()` is guaranteed not to call `devres_node_release()` for the entire
+ // lifetime of `dev`.
+ unsafe { base::devres_node_add(dev.as_raw(), inner.node.get()) };
+
+ // Take additional reference count for `devres_node_add()`.
+ core::mem::forget(inner.clone());
Ok(Self {
dev: dev.into(),
- callback,
- data,
+ inner,
})
}
fn data(&self) -> &Revocable<T> {
- &self.data
+ &self.inner.data
}
#[allow(clippy::missing_safety_doc)]
- unsafe extern "C" fn devres_callback(ptr: *mut kernel::ffi::c_void) {
- // SAFETY: In `Self::new` we've passed a valid pointer of `Revocable<T>` to
- // `devm_add_action()`, hence `ptr` must be a valid pointer to `Revocable<T>`.
- let data = unsafe { Arc::from_raw(ptr.cast::<Revocable<T>>()) };
+ unsafe extern "C" fn devres_node_release(
+ _dev: *mut bindings::device,
+ node: *mut bindings::devres_node,
+ ) {
+ let node = Opaque::cast_from(node);
+
+ // SAFETY: `node` is in the same allocation as its container.
+ let inner = unsafe { kernel::container_of!(node, Inner<T>, node) };
+
+ // SAFETY: `inner` is a valid `Inner<T>` pointer.
+ let inner = unsafe { &*inner };
- data.revoke();
+ inner.data.revoke();
+ }
+
+ #[allow(clippy::missing_safety_doc)]
+ unsafe extern "C" fn devres_node_free_node(node: *mut bindings::devres_node) {
+ // SAFETY: `node` points to the entire `Inner<T>` allocation.
+ drop(unsafe { Arc::from_raw(node.cast::<Inner<T>>()) });
}
- fn remove_action(&self) -> bool {
+ fn remove_node(&self) -> bool {
// SAFETY:
- // - `self.dev` is a valid `Device`,
- // - the `action` and `data` pointers are the exact same ones as given to
- // `devm_add_action()` previously,
- (unsafe {
- bindings::devm_remove_action_nowarn(
- self.dev.as_raw(),
- Some(self.callback),
- core::ptr::from_ref(self.data()).cast_mut().cast(),
- )
- } == 0)
+ // - `self.device().as_raw()` is a valid pointer to a bound `struct device`.
+ // - `self.inner.node.get()` is a valid pointer to a `struct devres_node`.
+ unsafe { base::devres_node_remove(self.device().as_raw(), self.inner.node.get()) }
}
/// Return a reference of the [`Device`] this [`Devres`] instance has been created with.
@@ -261,12 +349,12 @@ fn drop(&mut self) {
// SAFETY: When `drop` runs, it is guaranteed that nobody is accessing the revocable data
// anymore, hence it is safe not to wait for the grace period to finish.
if unsafe { self.data().revoke_nosync() } {
- // We revoked `self.data` before the devres action did, hence try to remove it.
- if self.remove_action() {
+ // We revoked `self.data` before devres did, hence try to remove it.
+ if self.remove_node() {
// SAFETY: In `Self::new` we have taken an additional reference count of `self.data`
- // for `devm_add_action()`. Since `remove_action()` was successful, we have to drop
+ // for `devres_node_add()`. Since `remove_node()` was successful, we have to drop
// this additional reference count.
- drop(unsafe { Arc::from_raw(Arc::as_ptr(&self.data)) });
+ drop(unsafe { Arc::from_raw(Arc::as_ptr(&self.inner)) });
}
}
}
--
2.53.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH v2 5/5] rust: devres: embed struct devres_node directly
2026-02-13 22:07 ` [PATCH v2 5/5] rust: devres: embed struct devres_node directly Danilo Krummrich
@ 2026-02-16 8:29 ` Alice Ryhl
0 siblings, 0 replies; 9+ messages in thread
From: Alice Ryhl @ 2026-02-16 8:29 UTC (permalink / raw)
To: Danilo Krummrich
Cc: gregkh, rafael, ojeda, boqun.feng, gary, bjorn3_gh, lossin,
a.hindborg, tmgross, driver-core, rust-for-linux, linux-kernel
On Fri, Feb 13, 2026 at 11:07:15PM +0100, Danilo Krummrich wrote:
> Currently, the Devres<T> container uses devm_add_action() to register a
> devres callback.
>
> devm_add_action() allocates a struct action_devres, which on top of
> struct devres_node, just keeps a data pointer and release function
> pointer.
>
> This is an unnecessary indirection, given that analogous to struct
> devres, the Devres<T> container can just embed a struct devres_node
> directly without an additional allocation.
>
> In contrast to struct devres, we don't need to force an alignment of
> ARCH_DMA_MINALIGN (as struct devres does to account for the worst case)
> since we have generics in Rust. I.e. the compiler already ensures
> correct alignment of the embedded T in Devres<T>.
>
> Thus, get rid of devm_add_action() and instead embed a struct
> devres_node directly.
>
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> /// This abstraction is meant to be used by subsystems to containerize [`Device`] bound resources to
> /// manage their lifetime.
> ///
> @@ -111,12 +124,63 @@
> /// ```
> pub struct Devres<T: Send> {
> dev: ARef<Device>,
Wouldn't it be nicer to move this into the Arc? Most of the time, it
would probably fit in the kmalloc bucket without enlarging it, so it
seems like that would be better for most scenarios.
> +// Calling the FFI functions from the `base` module directly from the `Devres<T>` impl may result in
> +// them being called directly from driver modules. This happens since the Rust compiler will use
> +// monomorphisation, so it might happen that functions are instantiated within the calling driver
> +// module. For now, work around this with `#[inline(never)]` helpers.
> +//
> +// TODO: Remove once a more generic solution has been implemented. For instance, we may be able to
> +// leverage `bindgen` to take care of this depending on whether a symbol is (already) exported.
I'm not sure what a generic solution would look like. There is an
assumption that if A can call B and B can call C, then A can call C.
Other than just exporting the C methods, possibly for Rust only?
What happens in C when you turn on LTO and core kernel methods get
inlined into modules?
> + #[inline(never)]
> + #[allow(clippy::missing_safety_doc)]
> + pub(super) unsafe fn devres_node_remove(
> + dev: *mut bindings::device,
> + node: *mut bindings::devres_node,
> + ) -> bool {
Since you're taking the opportunity to wrap these, I'd consider adding
#[must_use] here.
> + // SAFETY: `node` is a valid pointer to an uninitialized `struct devres_node`.
> + unsafe {
> + base::devres_set_node_dbginfo(
> + node,
> + // TODO: Use `core::any::type_name::<T>()` once we are able to convert
> + // the `&str` to a NULL terminated `&CStr` without additional
> + // allocation.
> + c"Devres<T>".as_char_ptr(),
The problem is that type_name() is not const. We can convert a const
&str to &CStr with no problems already.
Alice
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v2 0/5] Use struct devres_node in Devres<T>
2026-02-13 22:07 [PATCH v2 0/5] Use struct devres_node in Devres<T> Danilo Krummrich
` (4 preceding siblings ...)
2026-02-13 22:07 ` [PATCH v2 5/5] rust: devres: embed struct devres_node directly Danilo Krummrich
@ 2026-03-12 15:12 ` Greg KH
2026-03-18 0:12 ` Danilo Krummrich
6 siblings, 0 replies; 9+ messages in thread
From: Greg KH @ 2026-03-12 15:12 UTC (permalink / raw)
To: Danilo Krummrich
Cc: rafael, ojeda, boqun.feng, gary, bjorn3_gh, lossin, a.hindborg,
aliceryhl, tmgross, driver-core, rust-for-linux, linux-kernel
On Fri, Feb 13, 2026 at 11:07:10PM +0100, Danilo Krummrich wrote:
> Currently, the Devres<T> container uses devm_add_action() to register a
> devres callback.
>
> devm_add_action() allocates a struct action_devres, which on top of
> struct devres_node, just keeps a data pointer and release function
> pointer.
>
> This is an unnecessary indirection, given that analogous to struct
> devres, the Devres<T> container can just embed a struct devres_node
> directly without an additional allocation.
>
> In contrast to struct devres, we don't need to force an alignment of
> ARCH_DMA_MINALIGN (as struct devres does to account for the worst case)
> since we have generics in Rust. I.e. the compiler already ensures
> correct alignment of the embedded T in Devres<T>.
>
> Thus, get rid of devm_add_action() and instead embed a struct
> devres_node directly.
>
> This patch series is based on [1].
>
> [1] https://lore.kernel.org/lkml/20260202235210.55176-1-dakr@kernel.org/
>
> Changes in v2:
> - Prevent driver-core internal C APIs from potentially being called directly
> from driver modules due to monomorphisation, to prevent unnecessary symbol
> exports.
>
> Danilo Krummrich (5):
> devres: move struct devres_node into base.h
> devres: export devres_node_init() and devres_node_add()
> devres: add devres_node_remove()
> devres: rename and export set_node_dbginfo()
> rust: devres: embed struct devres_node directly
>
> drivers/base/base.h | 18 +++++
> drivers/base/devres.c | 50 ++++++------
> rust/kernel/devres.rs | 182 +++++++++++++++++++++++++++++++-----------
> 3 files changed, 180 insertions(+), 70 deletions(-)
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH v2 0/5] Use struct devres_node in Devres<T>
2026-02-13 22:07 [PATCH v2 0/5] Use struct devres_node in Devres<T> Danilo Krummrich
` (5 preceding siblings ...)
2026-03-12 15:12 ` [PATCH v2 0/5] Use struct devres_node in Devres<T> Greg KH
@ 2026-03-18 0:12 ` Danilo Krummrich
6 siblings, 0 replies; 9+ messages in thread
From: Danilo Krummrich @ 2026-03-18 0:12 UTC (permalink / raw)
To: gregkh, rafael, ojeda, boqun.feng, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross
Cc: driver-core, rust-for-linux, linux-kernel
On Fri Feb 13, 2026 at 11:07 PM CET, Danilo Krummrich wrote:
Applied to driver-core-testing, thanks!
> Danilo Krummrich (5):
> devres: move struct devres_node into base.h
> devres: export devres_node_init() and devres_node_add()
> devres: add devres_node_remove()
> devres: rename and export set_node_dbginfo()
> rust: devres: embed struct devres_node directly
[ * Improve comment about core::any::type_name(),
* add #[must_use] to devres_node_remove(),
* use container_of!() in devres_node_free_node().
- Danilo ]
^ permalink raw reply [flat|nested] 9+ messages in thread