Re: [RFC PATCH 08/11] rust: add devres abstraction

From: Dirk Behme
Date: Mon Jun 03 2024 - 03:21:12 EST


On 20.05.2024 19:25, Danilo Krummrich wrote:
Add a Rust abstraction for the kernel's devres (device resource
management) implementation.

The Devres type acts as a container to manage the lifetime and
accessibility of device bound resources. Therefore it registers a
devres callback and revokes access to the resource on invocation.

Users of the Devres abstraction can simply free the corresponding
resources in their Drop implementation, which is invoked when either the
Devres instance goes out of scope or the devres callback leads to the
resource being revoked, which implies a call to drop_in_place().

Co-developed-by: Philipp Stanner <pstanner@xxxxxxxxxx>
Signed-off-by: Philipp Stanner <pstanner@xxxxxxxxxx>
Signed-off-by: Danilo Krummrich <dakr@xxxxxxxxxx>

...

+impl<T> Devres<T> {
+ /// Creates a new [`Devres`] instance of the give data.

Typo? give -> given

+ pub fn new(dev: ARef<Device>, data: T, flags: Flags) -> Result<Self> {
+ let callback = devres_callback::<T>;
+
+ let inner = Box::pin_init(
+ pin_init!( DevresInner {
+ dev: dev,
+ data <- Revocable::new(data),
+ }),
+ flags,
+ )?;
+
+ let ret = unsafe {
+ bindings::devm_add_action(inner.dev.as_raw(), Some(callback), inner.as_cptr())
+ };
+
+ if ret != 0 {
+ return Err(Error::from_errno(ret));
+ }
+
+ // We have to store the exact callback function pointer used with
+ // `bindings::devm_add_action` for `bindings::devm_remove_action`. There compiler might put

Missing 'the'? "There *the* compiler might put ..."

Best regards

Dirk