Use struct devres_node in Devres<T>
Subsystems (2)
Danilo Krummrich
maintainerv2 cover Use struct devres_node in Devres<T>
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/[email protected]/
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(-)
base-commit: 1680377f05a809e4489731ad0e5a5fd5471ec40e
Signature · 1 line
2.53.0
Danilo Krummrich
maintainerPATCH v2 1/5 devres: move struct devres_node into base.h
Move struct devres_node into base.h, such that we can access it from the Rust devres code.
--- drivers/base/base.h | 12 ++++++++++++ drivers/base/devres.c | 12 ------------ 2 files changed, 12 insertions(+), 12 deletions(-)
drivers/base/base.h +12 -0
@@ -222,6 +222,18 @@ static inline void device_set_driver(struct device *dev, const struct device_driWRITE_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 *),
drivers/base/devres.c +0 -12
@@ -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;
Signature · 1 line
2.53.0
Danilo Krummrich
maintainerPATCH v2 2/5 devres: export devres_node_init() and devres_node_add()
In reply to Danilo KrummrichExport devres_node_init() and devres_node_add() through base.h, such that we can access is from the Rust devres code.
--- drivers/base/base.h | 3 +++ drivers/base/devres.c | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-)
drivers/base/base.h +3 -0
@@ -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 *),
drivers/base/devres.c +4 -4
@@ -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);
Signature · 1 line
2.53.0
Danilo Krummrich
maintainerPATCH v2 3/5 devres: add devres_node_remove()
In reply to Danilo KrummrichWhen 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.
--- drivers/base/base.h | 1 + drivers/base/devres.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+)
drivers/base/base.h +1 -0
@@ -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 *),
drivers/base/devres.c +16 -0
@@ -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
Signature · 1 line
2.53.0
Danilo Krummrich
maintainerPATCH v2 4/5 devres: rename and export set_node_dbginfo()
In reply to Danilo KrummrichRename 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.
--- drivers/base/base.h | 2 ++ drivers/base/devres.c | 14 +++++++------- 2 files changed, 9 insertions(+), 7 deletions(-)
drivers/base/base.h +2 -0
@@ -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 *),
drivers/base/devres.c +7 -7
@@ -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, coreturn -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
Signature · 1 line
2.53.0
Danilo Krummrich
maintainerPATCH v2 5/5 rust: devres: embed struct devres_node directly
In reply to Danilo KrummrichCurrently, 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.
--- rust/kernel/devres.rs | 182 +++++++++++++++++++++++++++++++----------- 1 file changed, 135 insertions(+), 47 deletions(-)
rust/kernel/devres.rs +135 -47
@@ -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)) }); } } }
Signature · 1 line
2.53.0
Re: [PATCH v2 5/5] rust: devres: embed struct devres_node directly
Quoted 16 lines · Danilo Krummrich
Currently, the Devres<T> container uses
devm_add_action()to register a devres callback.
devm_add_action()allocates a structaction_devres, which on top of structdevres_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_nodedirectly 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>.
Quoted 4 lines
Thus, get rid of
devm_add_action()and instead embed a structdevres_nodedirectly.Signed-off-by Danilo Krummrich <[email protected]>
rust/kernel/devres.rs line 126
Quoted 7 lines
/// This abstraction is meant to be used by subsystems to containerize [
Device] bound resources to /// manage their lifetime.
///rust/kernel/devres.rs +0
-0@@ -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.
rust/kernel/devres.rs line 136
Quoted 7 lines
rust/kernel/devres.rs +7
-0+// 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?
rust/kernel/devres.rs line 180
Quoted 6 lines
rust/kernel/devres.rs +6
-0+#[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.
rust/kernel/devres.rs pub fn new<E>(dev: &Device<Bound>, data: impl PinInit<T, E>) -> Result<Self> · line 214
Quoted 8 lines
rust/kernel/devres.rs +8
-0+// 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
Quoted 35 lines · Danilo Krummrich
Currently, the Devres<T> container uses
devm_add_action()to register a devres callback.
devm_add_action()allocates a structaction_devres, which on top of structdevres_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_nodedirectly 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 structdevres_nodedirectly.This patch series is based on [1].
[1] https://lore.kernel.org/lkml/[email protected]/
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 structdevres_nodeintobase.h
devres: exportdevres_node_init()anddevres_node_add()
devres: adddevres_node_remove()
devres: rename and exportset_node_dbginfo()
rust: devres: embed structdevres_nodedirectly
Quoted 4 lines
drivers/base/base.h | 18 +++++ drivers/base/devres.c | 50 ++++++------ rust/kernel/devres.rs | 182 +++++++++++++++++++++++++++++++----------- 3 files changed, 180 insertions(+), 70 deletions(-)
Danilo Krummrich
maintainerOn Fri Feb 13, 2026 at 11:07 PM CET, Danilo Krummrich wrote:
Applied to driver-core-testing, thanks!
Quoted 6 lines
Danilo Krummrich (5):
devres: move structdevres_nodeintobase.h
devres: exportdevres_node_init()anddevres_node_add()
devres: adddevres_node_remove()
devres: rename and exportset_node_dbginfo()
rust: devres: embed structdevres_nodedirectly
[ * Improve comment about core::any::type_name(),
* add #[must_use] to devres_node_remove(),
* use container_of!() in devres_node_free_node().
- Danilo ]