[PATCH v6 4/9] rust: device: Enable printing fwnode name and path

Remo Senekowitsch posted 9 patches 6 months, 4 weeks ago
There is a newer version of this series
[PATCH v6 4/9] rust: device: Enable printing fwnode name and path
Posted by Remo Senekowitsch 6 months, 4 weeks ago
Add two new public methods `display_name` and `display_path` to
`FwNode`. They can be used by driver authors for logging purposes. In
addition, they will be used by core property abstractions for automatic
logging, for example when a driver attempts to read a required but
missing property.

Signed-off-by: Remo Senekowitsch <remo@buenzli.dev>
---
 rust/kernel/device/property.rs | 75 ++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)

diff --git a/rust/kernel/device/property.rs b/rust/kernel/device/property.rs
index 71a0605b8ecad..09c38775b2354 100644
--- a/rust/kernel/device/property.rs
+++ b/rust/kernel/device/property.rs
@@ -32,6 +32,81 @@ pub(crate) fn as_raw(&self) -> *mut bindings::fwnode_handle {
         self.0.get()
     }
 
+    /// Returns an object that implements [`Display`](core::fmt::Display) for
+    /// printing the name of a node.
+    pub fn display_name(&self) -> impl core::fmt::Display + '_ {
+        struct FwNodeDisplayName<'a>(&'a FwNode);
+
+        impl core::fmt::Display for FwNodeDisplayName<'_> {
+            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+                // SAFETY: `self` is valid by its type invariant.
+                let name = unsafe { bindings::fwnode_get_name(self.0.as_raw()) };
+                if name.is_null() {
+                    return Ok(());
+                }
+                // SAFETY:
+                // - `fwnode_get_name` returns null or a valid C string.
+                // - `name` was checked to be non-null.
+                let name = unsafe { CStr::from_char_ptr(name) };
+                write!(f, "{name}")
+            }
+        }
+
+        FwNodeDisplayName(self)
+    }
+
+    /// Returns an object that implements [`Display`](core::fmt::Display) for
+    /// printing the full path of a node.
+    pub fn display_path(&self) -> impl core::fmt::Display + '_ {
+        struct FwNodeDisplayPath<'a>(&'a FwNode);
+
+        impl core::fmt::Display for FwNodeDisplayPath<'_> {
+            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+                // The logic here is the same as the one in lib/vsprintf.c
+                // (fwnode_full_name_string).
+
+                // SAFETY: `self.0.as_raw()` is valid by its type invariant.
+                let num_parents = unsafe { bindings::fwnode_count_parents(self.0.as_raw()) };
+
+                for depth in (0..=num_parents).rev() {
+                    let fwnode = if depth == 0 {
+                        self.0.as_raw()
+                    } else {
+                        // SAFETY: `self.0.as_raw()` is valid.
+                        unsafe { bindings::fwnode_get_nth_parent(self.0.as_raw(), depth) }
+                    };
+
+                    // SAFETY: `fwnode` is valid, it is either `self.0.as_raw()`
+                    // or the return value of `bindings::fwnode_get_nth_parent`
+                    // which returns a valid pointer to a `fwnode_handle` if the
+                    // provided depth is within the valid range, which we know
+                    // to be true.
+                    let prefix = unsafe { bindings::fwnode_get_name_prefix(fwnode) };
+                    if !prefix.is_null() {
+                        // SAFETY: `fwnode_get_name_prefix` returns null or a
+                        // valid C string.
+                        let prefix = unsafe { CStr::from_char_ptr(prefix) };
+                        write!(f, "{prefix}")?;
+                    }
+                    write!(f, "{}", self.0.display_name())?;
+
+                    if depth != 0 {
+                        // SAFETY:
+                        // - `fwnode` is valid, because `depth` is
+                        //   a valid depth of a parent of `self.0.as_raw()`.
+                        // - `fwnode_get_nth_parent` increments the refcount and
+                        //   we are responsible to decrement it.
+                        unsafe { bindings::fwnode_handle_put(fwnode) }
+                    }
+                }
+
+                Ok(())
+            }
+        }
+
+        FwNodeDisplayPath(self)
+    }
+
     /// Checks if property is present or not.
     pub fn property_present(&self, name: &CStr) -> bool {
         // SAFETY: By the invariant of `CStr`, `name` is null-terminated.
-- 
2.49.0
Re: [PATCH v6 4/9] rust: device: Enable printing fwnode name and path
Posted by Danilo Krummrich 6 months, 4 weeks ago
On Sat, May 24, 2025 at 09:22:27PM +0200, Remo Senekowitsch wrote:
> +    /// Returns an object that implements [`Display`](core::fmt::Display) for
> +    /// printing the full path of a node.
> +    pub fn display_path(&self) -> impl core::fmt::Display + '_ {

While testing the code I found the following buggy print statement:

	"test-device@2/test-device@2/test-device@2/test-device@2: property 'test,u32-required-prop' is missing"

and I think the following changes fix this:

> +        struct FwNodeDisplayPath<'a>(&'a FwNode);
> +
> +        impl core::fmt::Display for FwNodeDisplayPath<'_> {
> +            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
> +                // The logic here is the same as the one in lib/vsprintf.c
> +                // (fwnode_full_name_string).
> +
> +                // SAFETY: `self.0.as_raw()` is valid by its type invariant.
> +                let num_parents = unsafe { bindings::fwnode_count_parents(self.0.as_raw()) };
> +
> +                for depth in (0..=num_parents).rev() {
> +                    let fwnode = if depth == 0 {
> +                        self.0.as_raw()

	self.0

> +                    } else {
> +                        // SAFETY: `self.0.as_raw()` is valid.
> +                        unsafe { bindings::fwnode_get_nth_parent(self.0.as_raw(), depth) }

	let ptr = unsafe { bindings::fwnode_get_nth_parent(self.0.as_raw(), depth) };
	FwNode::as_ref(ptr)

where FwNode::as_ref() is

	unsafe fn as_ref<'a>(ptr: *mut bindings::fwnode_handle) -> &'a Self

> +                    };
> +
> +                    // SAFETY: `fwnode` is valid, it is either `self.0.as_raw()`
> +                    // or the return value of `bindings::fwnode_get_nth_parent`
> +                    // which returns a valid pointer to a `fwnode_handle` if the
> +                    // provided depth is within the valid range, which we know
> +                    // to be true.
> +                    let prefix = unsafe { bindings::fwnode_get_name_prefix(fwnode) };
> +                    if !prefix.is_null() {
> +                        // SAFETY: `fwnode_get_name_prefix` returns null or a
> +                        // valid C string.
> +                        let prefix = unsafe { CStr::from_char_ptr(prefix) };
> +                        write!(f, "{prefix}")?;
> +                    }
> +                    write!(f, "{}", self.0.display_name())?;

	write!(f, "{}", fwnode.0.display_name())?;

Otherwise we always write the name of self, no matter the actual parent depth.

> +
> +                    if depth != 0 {
> +                        // SAFETY:
> +                        // - `fwnode` is valid, because `depth` is
> +                        //   a valid depth of a parent of `self.0.as_raw()`.
> +                        // - `fwnode_get_nth_parent` increments the refcount and
> +                        //   we are responsible to decrement it.
> +                        unsafe { bindings::fwnode_handle_put(fwnode) }
> +                    }
> +                }
> +
> +                Ok(())
> +            }
> +        }
> +
> +        FwNodeDisplayPath(self)
> +    }