Skip to contentSkip to conversation

More Rust bindings for device property reads

PATCH Superseded Remo Senekowitsch 8 participants 83 messages Mar 26, 2025 – Apr 24, 2025

Subsystems (5)
ACPI Driver core Rust Device tree +1 more subsystems

v1 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

cover More Rust bindings for device property reads

More Rust bindings for device property reads

This adds more Rust bindings for reading device properties, based on Rob Herring's work. I'm working on a driver[1] that uses these, but the driver has more dependencies than this.

Rob Herring and Dirk Behme did several review iterations over on Zulip already[1], I'm thankful for their great input.

This is my first time posting to the mailing list, please let me know if I did anything wrong.

Remo

[1] https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/topic/DS90UB954.20driver.20done.2C.20ready.20to.20upstream.3F/with/507874342

#1 lore raw

PATCH 1/10 rust: Move property_present to property.rs

Not all property-related APIs can be exposed directly on a device. For example, iterating over child nodes of a device will yield fwnode_handle. Thus, in order to access properties on these child nodes, the APIs has to be duplicated on a fwnode as they are in C.

A related discussion can be found on the R4L Zulip[1].

[1] https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/topic/DS90UB954.20driver.20done.2C.20ready.20to.20upstream.3F/near/505415697

---
 rust/helpers/helpers.c  |  1 +
 rust/helpers/property.c | 13 ++++++++
 rust/kernel/device.rs   |  7 ----
 rust/kernel/lib.rs      |  1 +
 rust/kernel/property.rs | 73 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 88 insertions(+), 7 deletions(-)
 create mode 100644 rust/helpers/property.c
 create mode 100644 rust/kernel/property.rs
rust/helpers/helpers.c +1 -0
index 0640b7e11..b4eec5bf2 100644
@@ -23,6 +23,7 @@
 #include "platform.c"
 #include "pci.c"
 #include "pid_namespace.c"
+#include "property.c"
 #include "rbtree.c"
 #include "rcu.c"
 #include "refcount.c"
rust/helpers/property.c new file +13 -0
new file mode 100644
index 000000000..c37c74488
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/property.h>
+
+struct fwnode_handle *rust_helper_dev_fwnode(struct device *dev)
+{
+	return dev_fwnode(dev);
+}
+
+void rust_helper_fwnode_handle_put(struct fwnode_handle *fwnode)
+{
+	fwnode_handle_put(fwnode);
+}
rust/kernel/device.rs +0 -7
index db2d9658b..d5e6a19ff 100644
@@ -6,7 +6,6 @@
 
 use crate::{
     bindings,
-    str::CStr,
     types::{ARef, Opaque},
 };
 use core::{fmt, ptr};
@@ -181,12 +180,6 @@ unsafe fn printk(&self, klevel: &[u8], msg: fmt::Arguments<'_>) {
             )
         };
     }
-
-    /// 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.
-        unsafe { bindings::device_property_present(self.as_raw().cast_const(), name.as_char_ptr()) }
-    }
 }
 
 // SAFETY: Instances of `Device` are always reference-counted.
rust/kernel/lib.rs +1 -0
index 496ed32b0..ca233fd20 100644
@@ -67,6 +67,7 @@
 pub mod platform;
 pub mod prelude;
 pub mod print;
+pub mod property;
 pub mod rbtree;
 pub mod revocable;
 pub mod security;
rust/kernel/property.rs new file +73 -0

73 lines not on this page · raw message

Signature · 1 line
2.49.0
maintainer
#2 lore raw

Re: [PATCH 01/10] rust: Move property_present to property.rs

Quoted 4 lines · Remo Senekowitsch

Not all property-related APIs can be exposed directly on a device. For example, iterating over child nodes of a device will yield fwnode_handle. Thus, in order to access properties on these child nodes, the APIs has to be duplicated on a fwnode as they are in C.

s/has/have/

Quoted 4 lines

A related discussion can be found on the R4L Zulip[1].

[1] https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/topic/DS90UB954.20driver.20done.2C.20ready.20to.20upstream.3F/near/505415697

Useful below the '---', but I don't think we want to keep this link forever. And who knows how long it will be valid? The commit msg needs to stand on its own, and I think it does.

Quoted 93 lines
---
 rust/helpers/helpers.c  |  1 +
 rust/helpers/property.c | 13 ++++++++
 rust/kernel/device.rs   |  7 ----
 rust/kernel/lib.rs      |  1 +
 rust/kernel/property.rs | 73 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 88 insertions(+), 7 deletions(-)
 create mode 100644 rust/helpers/property.c
 create mode 100644 rust/kernel/property.rs
rust/helpers/helpers.c +1 -0
index 0640b7e11..b4eec5bf2 100644
@@ -23,6 +23,7 @@
 #include "platform.c"
 #include "pci.c"
 #include "pid_namespace.c"
+#include "property.c"
 #include "rbtree.c"
 #include "rcu.c"
 #include "refcount.c"
rust/helpers/property.c new file +13 -0
new file mode 100644
index 000000000..c37c74488
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/property.h>
+
+struct fwnode_handle *rust_helper_dev_fwnode(struct device *dev)
+{
+	return dev_fwnode(dev);
+}
+
+void rust_helper_fwnode_handle_put(struct fwnode_handle *fwnode)
+{
+	fwnode_handle_put(fwnode);
+}
rust/kernel/device.rs +0 -7
index db2d9658b..d5e6a19ff 100644
@@ -6,7 +6,6 @@
 
 use crate::{
     bindings,
-    str::CStr,
     types::{ARef, Opaque},
 };
 use core::{fmt, ptr};
@@ -181,12 +180,6 @@ unsafe fn printk(&self, klevel: &[u8], msg: fmt::Arguments<'_>) {
             )
         };
     }
-
-    /// 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.
-        unsafe { bindings::device_property_present(self.as_raw().cast_const(), name.as_char_ptr()) }
-    }
 }
 
 // SAFETY: Instances of `Device` are always reference-counted.
rust/kernel/lib.rs +1 -0
index 496ed32b0..ca233fd20 100644
@@ -67,6 +67,7 @@
 pub mod platform;
 pub mod prelude;
 pub mod print;
+pub mod property;
 pub mod rbtree;
 pub mod revocable;
 pub mod security;
rust/kernel/property.rs new file +7 -0
new file mode 100644
index 000000000..b0a4bb63a
@@ -0,0 +1,73 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Unified device property interface.
+//!
+//! C header: [`include/linux/property.h`](srctree/include/linux/property.h)
+
+use core::ptr;

rust/kernel/property.rs line 17

Quoted 10 lines
rust/kernel/property.rs new file +10 -0
+
+use crate::{bindings, device::Device, str::CStr, types::Opaque};
+
+impl Device {
+    /// Obtain the fwnode corresponding to the device.
+    fn fwnode(&self) -> &FwNode {
+        // SAFETY: `self` is valid.
+        let fwnode_handle = unsafe { bindings::dev_fwnode(self.as_raw()) };
+        if fwnode_handle.is_null() {
+            panic!("fwnode_handle cannot be null");

It's usually not a good idea to panic the kernel especially with something a driver calls as that's probably recoverable.

Users/drivers testing fwnode_handle/of_node for NULL is pretty common. Though often that's a legacy code path, so maybe not allowing NULL is fine for now.

rust/kernel/property.rs line 29

Quoted 12 lines
rust/kernel/property.rs new file +12 -0
+        }
+        // SAFETY: `fwnode_handle` is valid. Its lifetime is tied to `&self`. We
+        // return a reference instead of an `ARef<FwNode>` because `dev_fwnode()`
+        // doesn't increment the refcount.
+        unsafe { &*fwnode_handle.cast() }
+    }
+
+    /// Checks if property is present or not.
+    pub fn property_present(&self, name: &CStr) -> bool {
+        self.fwnode().property_present(name)
+    }
+}

The C developer in me wants to put this after the FwNode stuff since this uses it.

Quoted 47 lines
rust/kernel/property.rs new file +44 -0
+
+/// A reference-counted fwnode_handle.
+///
+/// This structure represents the Rust abstraction for a
+/// C `struct fwnode_handle`. This implementation abstracts the usage of an
+/// already existing C `struct fwnode_handle` within Rust code that we get
+/// passed from the C side.
+///
+/// # Invariants
+///
+/// A `FwNode` instance represents a valid `struct fwnode_handle` created by the
+/// C portion of the kernel.
+///
+/// Instances of this type are always reference-counted, that is, a call to
+/// `fwnode_handle_get` ensures that the allocation remains valid at least until
+/// the matching call to `fwnode_handle_put`.
+#[repr(transparent)]
+pub struct FwNode(Opaque<bindings::fwnode_handle>);
+
+impl FwNode {
+    /// Obtain the raw `struct fwnode_handle *`.
+    pub(crate) fn as_raw(&self) -> *mut bindings::fwnode_handle {
+        self.0.get()
+    }
+
+    /// 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.
+        unsafe { bindings::fwnode_property_present(self.as_raw().cast_const(), name.as_char_ptr()) }
+    }
+}
+
+// SAFETY: Instances of `FwNode` are always reference-counted.
+unsafe impl crate::types::AlwaysRefCounted for FwNode {
+    fn inc_ref(&self) {
+        // SAFETY: The existence of a shared reference guarantees that the refcount is non-zero.
+        unsafe { bindings::fwnode_handle_get(self.as_raw()) };
+    }
+
+    unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
+        // SAFETY: The safety requirements guarantee that the refcount is non-zero.
+        unsafe { bindings::fwnode_handle_put(obj.cast().as_ptr()) }
+    }
+}
Signature · 1 line
2.49.0
maintainer
#3 lore raw

Re: [PATCH 01/10] rust: Move property_present to property.rs

Quoted 9 lines · Rob Herring
Quoted 4 lines · Remo Senekowitsch

Not all property-related APIs can be exposed directly on a device. For example, iterating over child nodes of a device will yield fwnode_handle. Thus, in order to access properties on these child nodes, the APIs has to be duplicated on a fwnode as they are in C.

s/has/have/

Quoted 1 line
Quoted 19 lines
Quoted 3 lines

A related discussion can be found on the R4L Zulip[1].

[1] https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/topic/DS90UB954.20driver.20done.2C.20ready.20to.20upstream.3F/near/505415697

Useful below the '---', but I don't think we want to keep this link forever. And who knows how long it will be valid? The commit msg needs to stand on its own, and I think it does.

Quoted 11 lines
---
 rust/helpers/helpers.c  |  1 +
 rust/helpers/property.c | 13 ++++++++
 rust/kernel/device.rs   |  7 ----
 rust/kernel/lib.rs      |  1 +
 rust/kernel/property.rs | 73 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 88 insertions(+), 7 deletions(-)
 create mode 100644 rust/helpers/property.c
 create mode 100644 rust/kernel/property.rs

Also, property.rs needs to be added to MAINTAINERS. I guess it goes under driver core with Greg.

Rob

#4 lore raw

Re: [PATCH 01/10] rust: Move property_present to property.rs

In reply to Rob Herring
Quoted 10 lines · Rob Herring

rust/kernel/property.rs line 17

Quoted 8 lines · Remo Senekowitsch
rust/kernel/property.rs new file +7 -0
+impl Device {
+    /// Obtain the fwnode corresponding to the device.
+    fn fwnode(&self) -> &FwNode {
+        // SAFETY: `self` is valid.
+        let fwnode_handle = unsafe { bindings::dev_fwnode(self.as_raw()) };
+        if fwnode_handle.is_null() {
+            panic!("fwnode_handle cannot be null");
Quoted 6 lines

It's usually not a good idea to panic the kernel especially with something a driver calls as that's probably recoverable.

Users/drivers testing fwnode_handle/of_node for NULL is pretty common. Though often that's a legacy code path, so maybe not allowing NULL is fine for now.

Just to be clear on this, should I keep this as is, or return a result? In the latter case, all the duplicated methods on Device that just call self.fwnode().same_method() would have a result in their function signatur as well. That includes property_present, read_property and children.

Quoted 7 lines
Quoted 7 lines
rust/kernel/property.rs new file +7 -0
+        }
+        // SAFETY: `fwnode_handle` is valid. Its lifetime is tied to `&self`. We
+        // return a reference instead of an `ARef<FwNode>` because `dev_fwnode()`
+        // doesn't increment the refcount.
+        unsafe { &*fwnode_handle.cast() }
+    }
+
Quoted 8 lines

rust/kernel/property.rs line 29

Quoted 5 lines
rust/kernel/property.rs new file +5 -0
+    /// Checks if property is present or not.
+    pub fn property_present(&self, name: &CStr) -> bool {
+        self.fwnode().property_present(name)
+    }
+}

The C developer in me wants to put this after the FwNode stuff since this uses it.

Is that just a comment or a call to action? :-)

Remo

#5 lore raw

Re: [PATCH 01/10] rust: Move property_present to property.rs

In reply to Remo Senekowitsch

On Wed, Mar 26, 2025 at 12:13 PM Remo Senekowitsch Wrote:

rust/kernel/property.rs line 22

Quoted 4 lines
rust/kernel/property.rs new file +4 -0
+        // SAFETY: `fwnode_handle` is valid. Its lifetime is tied to `&self`. We
+        // return a reference instead of an `ARef<FwNode>` because `dev_fwnode()`
+        // doesn't increment the refcount.
+        unsafe { &*fwnode_handle.cast() }

this safety comment should say why it is safe to cast from a struct fwnode_handle* to a *const FwNode

Andrew

#6 lore raw

Re: [PATCH 01/10] rust: Move property_present to property.rs

In reply to Remo Senekowitsch
Quoted 8 lines · Remo Senekowitsch

Not all property-related APIs can be exposed directly on a device. For example, iterating over child nodes of a device will yield fwnode_handle. Thus, in order to access properties on these child nodes, the APIs has to be duplicated on a fwnode as they are in C.

A related discussion can be found on the R4L Zulip[1].

[1] https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/topic/DS90UB954.20driver.20done.2C.20ready.20to.20upstream.3F/near/505415697

You can make the above to be a Link tag like

Link ... [1]
Quoted 1 line

...

rust/helpers/property.c line 8

Quoted 4 lines
rust/helpers/property.c new file +4 -0
+struct fwnode_handle *rust_helper_dev_fwnode(struct device *dev)
+{
+	return dev_fwnode(dev);
+}

Why not const? For most of the property retrieval APIs the parameter is const.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#7 lore raw

Re: [PATCH 01/10] rust: Move property_present to property.rs

Quoted 17 lines · Andy Shevchenko
Quoted 8 lines · Remo Senekowitsch

Not all property-related APIs can be exposed directly on a device. For example, iterating over child nodes of a device will yield fwnode_handle. Thus, in order to access properties on these child nodes, the APIs has to be duplicated on a fwnode as they are in C.

A related discussion can be found on the R4L Zulip[1].

[1] https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/topic/DS90UB954.20driver.20done.2C.20ready.20to.20upstream.3F/near/505415697

You can make the above to be a Link tag like

Link ... [1]
Quoted 1 line
Quoted 8 lines

...

rust/helpers/property.c line 8

Quoted 4 lines
rust/helpers/property.c new file +4 -0
+struct fwnode_handle *rust_helper_dev_fwnode(struct device *dev)
+{
+     return dev_fwnode(dev);
+}

Why not const? For most of the property retrieval APIs the parameter is const.

Because you might need to modify the refcount.

Though the rust code should probably use __dev_fwnode() and/or __dev_fwnode_const() directly and avoid the need for the helper here.

Rob

#8 lore raw

Re: [PATCH 01/10] rust: Move property_present to property.rs

Quoted 3 lines · Rob Herring
Quoted 1 line · Andy Shevchenko

On Wed, Mar 26, 2025 at 06:13:40PM +0100, Remo Senekowitsch wrote:

...

Quoted 11 lines
Quoted 6 lines

rust/helpers/property.c line 8

Quoted 4 lines
rust/helpers/property.c new file +4 -0
+struct fwnode_handle *rust_helper_dev_fwnode(struct device *dev)
+{
+     return dev_fwnode(dev);
+}

Why not const? For most of the property retrieval APIs the parameter is const.

Because you might need to modify the refcount.

Though the rust code should probably use __dev_fwnode() and/or __dev_fwnode_const() directly and avoid the need for the helper here.

Indeed, that would help at least to understand the intention.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw

PATCH 2/10 rust: Add an Integer trait

In reply to Remo Senekowitsch

From: "Rob Herring (Arm)" <[email protected]>

Add an "Integer" trait similar to crate::num::Integer. This is useful for implementing generic methods which operate on different sizes of integers. One example is reading DT/ACPI firmware properties.

This was originally proposed by Alice Ryhl[1].

[1] https://lore.kernel.org/rust-for-linux/CAH5fLgiXPZqKpWSSNdx-Ww-E9h2tOLcF3_8Y4C_JQ0eU8EMwFw@mail.gmail.com/

---
 rust/kernel/types.rs | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
rust/kernel/types.rs +21 -1
index 2bbaab83b..21647b7ba 100644
@@ -3,10 +3,11 @@
 //! Kernel types.
 
 use crate::init::{self, PinInit};
+use crate::transmute::{AsBytes, FromBytes};
 use core::{
     cell::UnsafeCell,
     marker::{PhantomData, PhantomPinned},
-    mem::{ManuallyDrop, MaybeUninit},
+    mem::{size_of, ManuallyDrop, MaybeUninit},
     ops::{Deref, DerefMut},
     ptr::NonNull,
 };
@@ -553,6 +554,25 @@ pub enum Either<L, R> {
     Right(R),
 }
 
+/// Trait defined for all integer types similar to `crate::num::Integer`
+pub trait Integer: FromBytes + AsBytes + Copy {
+    /// Size of the integer in bytes
+    const SIZE: usize;
+}
+
+macro_rules! impl_int {
+    ($($typ:ty),* $(,)?) => {$(
+        impl Integer for $typ {
+            const SIZE: usize = size_of::<Self>();
+        }
+    )*};
+}
+
+impl_int! {
+    u8, u16, u32, u64, usize,
+    i8, i16, i32, i64, isize,
+}
+
 /// Zero-sized type to mark types not [`Send`].
 ///
 /// Add this type as a field to your struct if your type should not be sent to a different task.
Signature · 1 line
2.49.0
maintainer
#10 lore raw

Re: [PATCH 02/10] rust: Add an Integer trait

Quoted 6 lines · Remo Senekowitsch

From: "Rob Herring (Arm)" <[email protected]>

Add an "Integer" trait similar to crate::num::Integer. This is useful for implementing generic methods which operate on different sizes of integers. One example is reading DT/ACPI firmware properties.

Quoted 6 lines

This was originally proposed by Alice Ryhl[1].

[1] https://lore.kernel.org/rust-for-linux/CAH5fLgiXPZqKpWSSNdx-Ww-E9h2tOLcF3_8Y4C_JQ0eU8EMwFw@mail.gmail.com/

This needs your Signed-off-by too because you are sending it.

Quoted 50 lines
---
 rust/kernel/types.rs | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
rust/kernel/types.rs +21 -1
index 2bbaab83b..21647b7ba 100644
@@ -3,10 +3,11 @@
 //! Kernel types.
 
 use crate::init::{self, PinInit};
+use crate::transmute::{AsBytes, FromBytes};
 use core::{
     cell::UnsafeCell,
     marker::{PhantomData, PhantomPinned},
-    mem::{ManuallyDrop, MaybeUninit},
+    mem::{size_of, ManuallyDrop, MaybeUninit},
     ops::{Deref, DerefMut},
     ptr::NonNull,
 };
@@ -553,6 +554,25 @@ pub enum Either<L, R> {
     Right(R),
 }
 
+/// Trait defined for all integer types similar to `crate::num::Integer`
+pub trait Integer: FromBytes + AsBytes + Copy {
+    /// Size of the integer in bytes
+    const SIZE: usize;
+}
+
+macro_rules! impl_int {
+    ($($typ:ty),* $(,)?) => {$(
+        impl Integer for $typ {
+            const SIZE: usize = size_of::<Self>();
+        }
+    )*};
+}
+
+impl_int! {
+    u8, u16, u32, u64, usize,
+    i8, i16, i32, i64, isize,
+}
+
 /// Zero-sized type to mark types not [`Send`].
 ///
 /// Add this type as a field to your struct if your type should not be sent to a different task.
Signature · 1 line
2.49.0
#11 lore raw

PATCH 3/10 device property: Add fwnode_property_read_int_array()

In reply to Remo Senekowitsch

The rust bindings for reading device properties has a single implementation supporting differing sizes of integers. The fwnode C API already has a similar interface, but it is not exposed with the fwnode_property_ API. Add the fwnode_property_read_int_array() wrapper.

---
 drivers/base/property.c  | 9 +++++----
 include/linux/property.h | 2 ++
 2 files changed, 7 insertions(+), 4 deletions(-)
drivers/base/property.c +5 -4
index c1392743d..64d5b7055 100644
@@ -292,10 +292,10 @@ int device_property_match_string(const struct device *dev, const char *propname,
 }
 EXPORT_SYMBOL_GPL(device_property_match_string);
 
-static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
-					  const char *propname,
-					  unsigned int elem_size, void *val,
-					  size_t nval)
+int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
+				   const char *propname,
+				   unsigned int elem_size, void *val,
+				   size_t nval)
 {
 	int ret;
 
@@ -310,6 +310,7 @@ static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
 	return fwnode_call_int_op(fwnode->secondary, property_read_int_array, propname,
 				  elem_size, val, nval);
 }
+EXPORT_SYMBOL_GPL(fwnode_property_read_int_array);
 
 /**
  * fwnode_property_read_u8_array - return a u8 array property of firmware node
include/linux/property.h +2 -0
index e214ecd24..441a1ad76 100644
@@ -57,6 +57,8 @@ bool fwnode_property_present(const struct fwnode_handle *fwnode,
 			     const char *propname);
 bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
 			     const char *propname);
+int fwnode_property_read_int_array(const struct fwnode_handle *fwnode, const char *propname,
+				   unsigned int elem_size, void *val, size_t nval);
 int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
 				  const char *propname, u8 *val,
 				  size_t nval);
Signature · 1 line
2.49.0
#12 lore raw

Re: [PATCH 03/10] device property: Add fwnode_property_read_int_array()

Quoted 4 lines · Remo Senekowitsch

The rust bindings for reading device properties has a single implementation supporting differing sizes of integers. The fwnode C API already has a similar interface, but it is not exposed with the fwnode_property_ API. Add the fwnode_property_read_int_array() wrapper.

...

Quoted 1 line

+EXPORT_SYMBOL_GPL(fwnode_property_read_int_array);

I'm not sure about this. We have a lot of assumptions in the code that the arrays beneath are only represented by the selected number of integer types. This opens a Pandora's box, e.g., reading in u24, which is not supported by the upper layers..

Quoted 2 lines
include/linux/property.h +2 -0
+int fwnode_property_read_int_array(const struct fwnode_handle *fwnode, const char *propname,
+				   unsigned int elem_size, void *val, size_t nval);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#13 lore raw

Re: [PATCH 03/10] device property: Add fwnode_property_read_int_array()

Quoted 10 lines · Andy Shevchenko
Quoted 4 lines · Remo Senekowitsch

The rust bindings for reading device properties has a single implementation supporting differing sizes of integers. The fwnode C API already has a similar interface, but it is not exposed with the fwnode_property_ API. Add the fwnode_property_read_int_array() wrapper.

...

Quoted 1 line

+EXPORT_SYMBOL_GPL(fwnode_property_read_int_array);

Quoted 7 lines

I'm not sure about this. We have a lot of assumptions in the code that the arrays beneath are only represented by the selected number of integer types. This opens a Pandora's box, e.g., reading in u24, which is not supported by the upper layers..

include/linux/property.h bool fwnode_property_present(const struct fwnode_handle *fwnode, · line 61

Quoted 2 lines
include/linux/property.h +2 -0
+int fwnode_property_read_int_array(const struct fwnode_handle *fwnode, const char *propname,
+				   unsigned int elem_size, void *val, size_t nval);

Here's an alternative approach using a macro to map each integer type explicitly to its corresponding read function. There are some additional changes that will be necessary to make the rest work, but this is the gist of it.

+macro_rules! impl_property_for_int {
+ ($($int:ty: $f:ident),* $(,)?) => {
+ $(
+ impl<const N: usize> Property for [$int; N] {
+ fn read(fwnode: &FwNode, name: &CStr) -> Result<Self> {
+ let mut val: [MaybeUninit<$int>; N] = [const { MaybeUninit::uninit() }; N];
+

+                    // SAFETY: `name` is non-null and null-terminated. `fwnode.as_raw` is valid
+                    // because `fwnode` is valid. `val.as_ptr` is valid because `val` is valid.
+                    let ret = unsafe {
+                        bindings::$f(
+                            fwnode.as_raw(),
+                            name.as_char_ptr(),
+                            val.as_mut_ptr().cast(),
+                            val.len(),

+ )
+ };
+ to_result(ret)?;
+

+                    // SAFETY: `val` is always initialized when
+                    // fwnode_property_read_$t_array is successful.
+                    Ok(val.map(|v| unsafe { v.assume_init() }))

+ }
+ }
+ impl Property for $int {
+ fn read(fwnode: &FwNode, name: &CStr) -> Result<Self> {
+ let val: [_; 1] = <[$int; 1] as Property>::read(fwnode, name)?;
+ Ok(val[0])
+ }
+ }
+ )*
+ };
+}
+impl_property_for_int! {

+    u8: fwnode_property_read_u8_array,
+    u16: fwnode_property_read_u16_array,
+    u32: fwnode_property_read_u32_array,
+    u64: fwnode_property_read_u64_array,
+    i8: fwnode_property_read_u8_array,
+    i16: fwnode_property_read_u16_array,
+    i32: fwnode_property_read_u32_array,
+    i64: fwnode_property_read_u64_array,

+}

#14 lore raw

Re: [PATCH 03/10] device property: Add fwnode_property_read_int_array()

Quoted 6 lines · Remo Senekowitsch
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Remo Senekowitsch

The rust bindings for reading device properties has a single implementation supporting differing sizes of integers. The fwnode C API already has a similar interface, but it is not exposed with the fwnode_property_ API. Add the fwnode_property_read_int_array() wrapper.

...

Quoted 7 lines
Quoted 7 lines
Quoted 1 line

+EXPORT_SYMBOL_GPL(fwnode_property_read_int_array);

I'm not sure about this. We have a lot of assumptions in the code that the arrays beneath are only represented by the selected number of integer types. This opens a Pandora's box, e.g., reading in u24, which is not supported by the upper layers..

Quoted 6 lines
Quoted 2 lines

include/linux/property.h bool fwnode_property_present(const struct fwnode_handle *fwnode, · line 61

Quoted 2 lines
include/linux/property.h +2 -0
+int fwnode_property_read_int_array(const struct fwnode_handle *fwnode, const char *propname,
+				   unsigned int elem_size, void *val, size_t nval);

Here's an alternative approach using a macro to map each integer type explicitly to its corresponding read function. There are some additional changes that will be necessary to make the rest work, but this is the gist of it.

I don;'t know Rust to tell anything about this, but at least it feels much better approach.

Quoted 33 lines
(quoted diff) +33 -0
+macro_rules! impl_property_for_int {
+    ($($int:ty: $f:ident),* $(,)?) => {
+        $(
+            impl<const N: usize> Property for [$int; N] {
+                fn read(fwnode: &FwNode, name: &CStr) -> Result<Self> {
+                    let mut val: [MaybeUninit<$int>; N] = [const { MaybeUninit::uninit() }; N];
+
+                    // SAFETY: `name` is non-null and null-terminated. `fwnode.as_raw` is valid
+                    // because `fwnode` is valid. `val.as_ptr` is valid because `val` is valid.
+                    let ret = unsafe {
+                        bindings::$f(
+                            fwnode.as_raw(),
+                            name.as_char_ptr(),
+                            val.as_mut_ptr().cast(),
+                            val.len(),
+                        )
+                    };
+                    to_result(ret)?;
+
+                    // SAFETY: `val` is always initialized when
+                    // fwnode_property_read_$t_array is successful.
+                    Ok(val.map(|v| unsafe { v.assume_init() }))
+                }
+            }
+            impl Property for $int {
+                fn read(fwnode: &FwNode, name: &CStr) -> Result<Self> {
+                    let val: [_; 1] = <[$int; 1] as Property>::read(fwnode, name)?;
+                    Ok(val[0])
+                }
+            }
+        )*
+    };
+}

Review

Quoted 10 lines
(quoted diff) +9 -0
+impl_property_for_int! {
+    u8: fwnode_property_read_u8_array,
+    u16: fwnode_property_read_u16_array,
+    u32: fwnode_property_read_u32_array,
+    u64: fwnode_property_read_u64_array,
 
+    i8: fwnode_property_read_u8_array,
+    i16: fwnode_property_read_u16_array,
+    i32: fwnode_property_read_u32_array,
+    i64: fwnode_property_read_u64_array,

Dunno what these mean. Are they signed integers? Do you really need this kind of types to be supported? Properties usually unsigned and I never saw (but it doesn't mean they are not exist) the negative values there. Does DT schema even allow this? Does DT validator complain or not on the negative values supplied in the DTS?

Quoted 1 line

+}

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#15 lore raw

Re: [PATCH 03/10] device property: Add fwnode_property_read_int_array()

Quoted 21 lines · Andy Shevchenko
Quoted 6 lines · Remo Senekowitsch
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Remo Senekowitsch

The rust bindings for reading device properties has a single implementation supporting differing sizes of integers. The fwnode C API already has a similar interface, but it is not exposed with the fwnode_property_ API. Add the fwnode_property_read_int_array() wrapper.

...

Quoted 10 lines
Quoted 9 lines
Quoted 1 line

+EXPORT_SYMBOL_GPL(fwnode_property_read_int_array);

I'm not sure about this. We have a lot of assumptions in the code that the arrays beneath are only represented by the selected number of integer types. This opens a Pandora's box, e.g., reading in u24, which is not supported by the upper layers..

Quoted 2 lines
include/linux/property.h +2 -0
+int fwnode_property_read_int_array(const struct fwnode_handle *fwnode, const char *propname,
+                             unsigned int elem_size, void *val, size_t nval);
Quoted 6 lines
Quoted 3 lines

Here's an alternative approach using a macro to map each integer type explicitly to its corresponding read function. There are some additional changes that will be necessary to make the rest work, but this is the gist of it.

I don;'t know Rust to tell anything about this, but at least it feels much better approach.

I know a little Rust and it is much worse. It is implementing the same code 8 times instead of 1 time just to work-around the C API.

Rob

#16 lore raw

Re: [PATCH 03/10] device property: Add fwnode_property_read_int_array()

Quoted 27 lines · Rob Herring
Quoted 25 lines · Andy Shevchenko
Quoted 6 lines · Remo Senekowitsch
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Remo Senekowitsch

The rust bindings for reading device properties has a single implementation supporting differing sizes of integers. The fwnode C API already has a similar interface, but it is not exposed with the fwnode_property_ API. Add the fwnode_property_read_int_array() wrapper.

...

Quoted 13 lines
Quoted 9 lines
Quoted 1 line

+EXPORT_SYMBOL_GPL(fwnode_property_read_int_array);

I'm not sure about this. We have a lot of assumptions in the code that the arrays beneath are only represented by the selected number of integer types. This opens a Pandora's box, e.g., reading in u24, which is not supported by the upper layers..

Quoted 2 lines
include/linux/property.h +2 -0
+int fwnode_property_read_int_array(const struct fwnode_handle *fwnode, const char *propname,
+                             unsigned int elem_size, void *val, size_t nval);

Here's an alternative approach using a macro to map each integer type explicitly to its corresponding read function. There are some additional changes that will be necessary to make the rest work, but this is the gist of it.

Quoted 5 lines
Quoted 2 lines

I don;'t know Rust to tell anything about this, but at least it feels much better approach.

I know a little Rust and it is much worse. It is implementing the same code 8 times instead of 1 time just to work-around the C API.

You mean it's worse because it will generate too much code during compile time?

Remo

#17 lore raw

Re: [PATCH 03/10] device property: Add fwnode_property_read_int_array()

In reply to Rob Herring
Quoted 27 lines · Rob Herring
Quoted 25 lines · Andy Shevchenko
Quoted 6 lines · Remo Senekowitsch
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Remo Senekowitsch

The rust bindings for reading device properties has a single implementation supporting differing sizes of integers. The fwnode C API already has a similar interface, but it is not exposed with the fwnode_property_ API. Add the fwnode_property_read_int_array() wrapper.

...

Quoted 13 lines
Quoted 9 lines
Quoted 1 line

+EXPORT_SYMBOL_GPL(fwnode_property_read_int_array);

I'm not sure about this. We have a lot of assumptions in the code that the arrays beneath are only represented by the selected number of integer types. This opens a Pandora's box, e.g., reading in u24, which is not supported by the upper layers..

Quoted 2 lines
(quoted diff) +2 -0
+int fwnode_property_read_int_array(const struct fwnode_handle *fwnode, const char *propname,
+                             unsigned int elem_size, void *val, size_t nval);

Here's an alternative approach using a macro to map each integer type explicitly to its corresponding read function. There are some additional changes that will be necessary to make the rest work, but this is the gist of it.

Quoted 5 lines
Quoted 2 lines

I don;'t know Rust to tell anything about this, but at least it feels much better approach.

I know a little Rust and it is much worse. It is implementing the same code 8 times instead of 1 time just to work-around the C API.

I prepared a functioning version of the macro-based approach. I'll post the patch for reference and discussion. We don't have to go with it.

Remo

---
 rust/kernel/property.rs | 124 +++++++++++++++++++++++++---------------
 1 file changed, 77 insertions(+), 47 deletions(-)
rust/kernel/property.rs +77 -47
index ceedd1a82..78dcc189e 100644
@@ -4,7 +4,7 @@
 //!
 //! C header: [`include/linux/property.h`](srctree/include/linux/property.h)
 
-use core::{ffi::c_void, mem::MaybeUninit, ptr};
+use core::{mem::MaybeUninit, ptr};
 
 use crate::{
     alloc::KVec,
@@ -13,7 +13,7 @@
     error::{to_result, Result},
     prelude::*,
     str::{CStr, CString},
-    types::{ARef, Integer, Opaque},
+    types::{ARef, Opaque},
 };
 
 impl Device {
@@ -43,7 +43,7 @@ pub fn property_match_string(&self, name: &CStr, match_str: &CStr) -> Result<usi
     }
 
     /// Returns firmware property `name` integer array values in a KVec
-    pub fn property_read_array_vec<'fwnode, 'name, T: Integer>(
+    pub fn property_read_array_vec<'fwnode, 'name, T: PropertyInt>(
         &'fwnode self,
         name: &'name CStr,
         len: usize,
@@ -52,7 +52,7 @@ pub fn property_read_array_vec<'fwnode, 'name, T: Integer>(
     }
 
     /// Returns integer array length for firmware property `name`
-    pub fn property_count_elem<T: Integer>(&self, name: &CStr) -> Result<usize> {
+    pub fn property_count_elem<T: PropertyInt>(&self, name: &CStr) -> Result<usize> {
         self.fwnode().property_count_elem::<T>(name)
     }
 
@@ -148,24 +148,17 @@ pub fn property_match_string(&self, name: &CStr, match_str: &CStr) -> Result<usi
     }
 
     /// Returns firmware property `name` integer array values in a KVec
-    pub fn property_read_array_vec<'fwnode, 'name, T: Integer>(
+    pub fn property_read_array_vec<'fwnode, 'name, T: PropertyInt>(
         &'fwnode self,
         name: &'name CStr,
         len: usize,
     ) -> Result<PropertyGuard<'fwnode, 'name, KVec<T>>> {
         let mut val: KVec<T> = KVec::with_capacity(len, GFP_KERNEL)?;
 
-        // SAFETY: `name` is non-null and null-terminated. `self.as_raw` is valid
-        // because `self` is valid. `val.as_ptr` is valid because `val` is valid.
-        let err = unsafe {
-            bindings::fwnode_property_read_int_array(
-                self.as_raw(),
-                name.as_char_ptr(),
-                T::SIZE as u32,
-                val.as_ptr() as *mut c_void,
-                len,
-            )
-        };
+        // SAFETY: `val.as_mut_ptr()` is valid because `KVec::with_capacity`
+        // didn't return an error and it has at least space for `len` number
+        // of elements.
+        let err = unsafe { T::read_array_out_param(self, name, val.as_mut_ptr(), len) };
         let res = if err < 0 {
             Err(Error::from_errno(err))
         } else {
@@ -181,19 +174,11 @@ pub fn property_read_array_vec<'fwnode, 'name, T: Integer>(
     }
 
     /// Returns integer array length for firmware property `name`
-    pub fn property_count_elem<T: Integer>(&self, name: &CStr) -> Result<usize> {
+    pub fn property_count_elem<T: PropertyInt>(&self, name: &CStr) -> Result<usize> {
         // SAFETY: `name` is non-null and null-terminated. `self.as_raw` is valid
         // because `self` is valid. Passing null pointer buffer is valid to obtain
         // the number of elements in the property array.
-        let ret = unsafe {
-            bindings::fwnode_property_read_int_array(
-                self.as_raw(),
-                name.as_char_ptr(),
-                T::SIZE as u32,
-                ptr::null_mut(),
-                0,
-            )
-        };
+        let ret = unsafe { T::read_array_out_param(self, name, ptr::null_mut(), 0) };
         to_result(ret)?;
         Ok(ret as usize)
     }
@@ -201,8 +186,8 @@ pub fn property_count_elem<T: Integer>(&self, name: &CStr) -> Result<usize> {
     /// Returns the value of firmware property `name`.
     ///
     /// This method is generic over the type of value to read. Informally,
-    /// the types that can be read are booleans, strings, integers and arrays
-    /// of integers.
+    /// the types that can be read are booleans, strings, unsigned integers and
+    /// arrays of unsigned integers.
     ///
     /// Reading a `KVec` of integers is done with the
     /// separate method [Self::property_read_array_vec], because it takes an
@@ -300,6 +285,9 @@ pub fn property_get_reference_args(
             NArgs::N(nargs) => (ptr::null(), nargs),
         };
 
+        // SAFETY: `self.0.get()` is valid. `prop.as_char_ptr()` is valid and
+        // zero-terminated. `nargs_prop` is valid and zero-terminated if `nargs`
+        // is zero, otherwise it is allowed to be a null-pointer.
         let ret = unsafe {
             bindings::fwnode_property_get_reference_args(
                 self.0.get(),
@@ -388,34 +376,76 @@ fn read(fwnode: &FwNode, name: &CStr) -> Result<Self> {
         Ok(str.try_into()?)
     }
 }
-impl<T: Integer, const N: usize> Property for [T; N] {
+/// Implemented for all integers that can be read as properties.
+///
+/// This helper trait is needed to associate the integer types of various sizes
+/// with their corresponding `fwnode_property_read_*_array` functions.
+pub trait PropertyInt: Property {
+    /// # Safety
+    ///
+    /// Callers must ensure that if `len` is non-zero, `out_param` must be
+    /// valid and point to memory that has enough space to hold at least `len`
+    /// number of elements.
+    unsafe fn read_array_out_param(
+        fwnode: &FwNode,
+        name: &CStr,
+        out_param: *mut Self,
+        len: usize,
+    ) -> ffi::c_int;
+}
+// This macro generates implementations of the traits `Property` and
+// `PropertyInt` for integers of various sizes. Its input is a list
+// of pairs separated by commas. The first element of the pair is the
+// type of the integer, the second one is the name of its corresponding
+// `fwnode_property_read_*_array` function.
+macro_rules! impl_property_for_int {
+    ($($int:ty: $f:ident),* $(,)?) => { $(
+        impl Property for $int {
+            fn read(fwnode: &FwNode, name: &CStr) -> Result<Self> {
+                let val: [_; 1] = <[$int; 1] as Property>::read(fwnode, name)?;
+                Ok(val[0])
+            }
+        }
+        impl PropertyInt for $int {
+            unsafe fn read_array_out_param(
+                fwnode: &FwNode,
+                name: &CStr,
+                out_param: *mut Self,
+                len: usize,
+            ) -> ffi::c_int {
+                // SAFETY: `name` is non-null and null-terminated.
+                // `fwnode.as_raw` is valid because `fwnode` is valid.
+                // `out_param` is valid and has enough space for at least
+                // `len` number of elements as per the safety requirement.
+                unsafe {
+                    bindings::$f(fwnode.as_raw(), name.as_char_ptr(), out_param, len)
+                }
+            }
+        }
+    )* };
+}
+impl_property_for_int! {
+    u8: fwnode_property_read_u8_array,
+    u16: fwnode_property_read_u16_array,
+    u32: fwnode_property_read_u32_array,
+    u64: fwnode_property_read_u64_array,
+}
+impl<T: PropertyInt, const N: usize> Property for [T; N] {
     fn read(fwnode: &FwNode, name: &CStr) -> Result<Self> {
         let mut val: [MaybeUninit<T>; N] = [const { MaybeUninit::uninit() }; N];
 
-        // SAFETY: `name` is non-null and null-terminated. `fwnode.as_raw` is valid
-        // because `fwnode` is valid. `val.as_ptr` is valid because `val` is valid.
-        let ret = unsafe {
-            bindings::fwnode_property_read_int_array(
-                fwnode.as_raw(),
-                name.as_char_ptr(),
-                T::SIZE as u32,
-                val.as_mut_ptr().cast(),
-                val.len(),
-            )
-        };
+        // SAFETY: `name` is non-null and null-terminated. `fwnode.as_raw` is
+        // valid because `fwnode` is valid. `val.as_ptr` is valid because `val`
+        // is valid. Casting from `*mut MaybeUninit<T>` to `*mut T` is safe
+        // because `MaybeUninit<T>` has the same memory layout as `T`.
+        let ret = unsafe { T::read_array_out_param(fwnode, name, val.as_mut_ptr().cast(), N) };
         to_result(ret)?;
 
         // SAFETY: `val` is always initialized when
-        // fwnode_property_read_int_array is successful.
+        // fwnode_property_read_$t_array is successful.
         Ok(val.map(|v| unsafe { v.assume_init() }))
     }
 }
-impl<T: Integer> Property for T {
-    fn read(fwnode: &FwNode, name: &CStr) -> Result<T> {
-        let val: [_; 1] = <[T; 1] as Property>::read(fwnode, name)?;
-        Ok(val[0])
-    }
-}
 
 /// The number of arguments of a reference.
 pub enum NArgs<'a> {
Signature · 1 line
2.49.0
maintainer
#18 lore raw

Re: [PATCH 03/10] device property: Add fwnode_property_read_int_array()

Quoted 29 lines · Remo Senekowitsch
Quoted 27 lines · Rob Herring
Quoted 25 lines · Andy Shevchenko
Quoted 6 lines · Remo Senekowitsch
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Remo Senekowitsch

The rust bindings for reading device properties has a single implementation supporting differing sizes of integers. The fwnode C API already has a similar interface, but it is not exposed with the fwnode_property_ API. Add the fwnode_property_read_int_array() wrapper.

...

Quoted 13 lines
Quoted 9 lines
Quoted 1 line

+EXPORT_SYMBOL_GPL(fwnode_property_read_int_array);

I'm not sure about this. We have a lot of assumptions in the code that the arrays beneath are only represented by the selected number of integer types. This opens a Pandora's box, e.g., reading in u24, which is not supported by the upper layers..

Quoted 2 lines
(quoted diff) +2 -0
+int fwnode_property_read_int_array(const struct fwnode_handle *fwnode, const char *propname,
+                             unsigned int elem_size, void *val, size_t nval);

Here's an alternative approach using a macro to map each integer type explicitly to its corresponding read function. There are some additional changes that will be necessary to make the rest work, but this is the gist of it.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I don;'t know Rust to tell anything about this, but at least it feels much better approach.

I know a little Rust and it is much worse. It is implementing the same code 8 times instead of 1 time just to work-around the C API.

I prepared a functioning version of the macro-based approach. I'll post the patch for reference and discussion. We don't have to go with it.

Please wait for the discussion to finish.

Rob

#19 lore raw

Re: [PATCH 03/10] device property: Add fwnode_property_read_int_array()

In reply to Rob Herring
Quoted 27 lines · Rob Herring
Quoted 25 lines · Andy Shevchenko
Quoted 6 lines · Remo Senekowitsch
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Remo Senekowitsch

The rust bindings for reading device properties has a single implementation supporting differing sizes of integers. The fwnode C API already has a similar interface, but it is not exposed with the fwnode_property_ API. Add the fwnode_property_read_int_array() wrapper.

...

Quoted 13 lines
Quoted 9 lines
Quoted 1 line

+EXPORT_SYMBOL_GPL(fwnode_property_read_int_array);

I'm not sure about this. We have a lot of assumptions in the code that the arrays beneath are only represented by the selected number of integer types. This opens a Pandora's box, e.g., reading in u24, which is not supported by the upper layers..

Quoted 2 lines
include/linux/property.h +2 -0
+int fwnode_property_read_int_array(const struct fwnode_handle *fwnode, const char *propname,
+                             unsigned int elem_size, void *val, size_t nval);

Here's an alternative approach using a macro to map each integer type explicitly to its corresponding read function. There are some additional changes that will be necessary to make the rest work, but this is the gist of it.

Quoted 5 lines
Quoted 2 lines

I don;'t know Rust to tell anything about this, but at least it feels much better approach.

I know a little Rust and it is much worse. It is implementing the same code 8 times instead of 1 time just to work-around the C API.

Can you please elaborate on this concern?

The previous approach uses generics, which means the function is also "copy-pasted" for each concrete type it is used with at compile time, just like with a macro. So using a macro wouldn't be worse than generics if binary size is the concern.

If the concern is about readability, I have managed to move all logic outside the macro body, all that remains is the association between type and *_read_array function.

Remo

maintainer
#20 lore raw

Re: [PATCH 03/10] device property: Add fwnode_property_read_int_array()

In reply to Andy Shevchenko
Quoted 7 lines · Andy Shevchenko
Quoted 4 lines · Remo Senekowitsch

The rust bindings for reading device properties has a single implementation supporting differing sizes of integers. The fwnode C API already has a similar interface, but it is not exposed with the fwnode_property_ API. Add the fwnode_property_read_int_array() wrapper.

Quoted 8 lines

...

Quoted 1 line

+EXPORT_SYMBOL_GPL(fwnode_property_read_int_array);

I'm not sure about this. We have a lot of assumptions in the code that the arrays beneath are only represented by the selected number of integer types. This opens a Pandora's box, e.g., reading in u24, which is not supported by the upper layers..

We can probably drop the export if it is just that which you object to.

Without this function, the implementation is just multiple levels of switch statements to work-around what's needed for the nicest rust API.

Rob

#21 lore raw

Re: [PATCH 03/10] device property: Add fwnode_property_read_int_array()

Quoted 8 lines · Rob Herring
Quoted 6 lines · Andy Shevchenko
Quoted 4 lines · Remo Senekowitsch

The rust bindings for reading device properties has a single implementation supporting differing sizes of integers. The fwnode C API already has a similar interface, but it is not exposed with the fwnode_property_ API. Add the fwnode_property_read_int_array() wrapper.

...

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

+EXPORT_SYMBOL_GPL(fwnode_property_read_int_array);

I'm not sure about this. We have a lot of assumptions in the code that the arrays beneath are only represented by the selected number of integer types. This opens a Pandora's box, e.g., reading in u24, which is not supported by the upper layers..

We can probably drop the export if it is just that which you object to.

Yes, this is main point, but dropping it does not prevent from still using in the complied-in code. Is it possible to hide it better?

Quoted 3 lines

Without this function, the implementation is just multiple levels of switch statements to work-around what's needed for the nicest rust API.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#22 lore raw

Re: [PATCH 03/10] device property: Add fwnode_property_read_int_array()

Quoted 20 lines · Andy Shevchenko
Quoted 8 lines · Rob Herring
Quoted 6 lines · Andy Shevchenko
Quoted 4 lines · Remo Senekowitsch

The rust bindings for reading device properties has a single implementation supporting differing sizes of integers. The fwnode C API already has a similar interface, but it is not exposed with the fwnode_property_ API. Add the fwnode_property_read_int_array() wrapper.

...

Quoted 7 lines
Quoted 6 lines
Quoted 1 line

+EXPORT_SYMBOL_GPL(fwnode_property_read_int_array);

I'm not sure about this. We have a lot of assumptions in the code that the arrays beneath are only represented by the selected number of integer types. This opens a Pandora's box, e.g., reading in u24, which is not supported by the upper layers..

Quoted 4 lines
Quoted 1 line

We can probably drop the export if it is just that which you object to.

Yes, this is main point, but dropping it does not prevent from still using in the complied-in code. Is it possible to hide it better?

Don't put any declaration in the header and declare it in the rust code? But lack of declaration generates warnings.

Also, all the backends will reject an arbitrary size. So your worry about u24 or other odd sizes isn't really valid. But if you want to be doubly paranoid for when we add a new firmware backend (shoot me now), you could move this from the swnode implementation to the fwnode implementation:

        if (!is_power_of_2(elem_size) || elem_size > sizeof(u64))
                return -ENXIO;

Rob

#23 lore raw

Re: [PATCH 03/10] device property: Add fwnode_property_read_int_array()

Quoted 10 lines · Rob Herring
Quoted 8 lines · Andy Shevchenko
Quoted 7 lines · Rob Herring
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Remo Senekowitsch

The rust bindings for reading device properties has a single implementation supporting differing sizes of integers. The fwnode C API already has a similar interface, but it is not exposed with the fwnode_property_ API. Add the fwnode_property_read_int_array() wrapper.

...

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 1 line

+EXPORT_SYMBOL_GPL(fwnode_property_read_int_array);

I'm not sure about this. We have a lot of assumptions in the code that the arrays beneath are only represented by the selected number of integer types. This opens a Pandora's box, e.g., reading in u24, which is not supported by the upper layers..

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

We can probably drop the export if it is just that which you object to.

Yes, this is main point, but dropping it does not prevent from still using in the complied-in code. Is it possible to hide it better?

Don't put any declaration in the header and declare it in the rust code? But lack of declaration generates warnings.

Exactly. And I believe we have the typed versions of int_array for a reason. Otherwise what's the point in having them to begin with?
(The first what comes to my mind is a compile time type checking, so we don't
try to load u8 with u32 data or any other dirty tricks.)

Maybe it deserves a header that can be included explicitly in the rust stuff and being checked at compile time to avoid people using that? Can we achieve something like C preprocessor

#ifndef FOO
#error This header must not be included directly
#endif

Quoted 8 lines

Also, all the backends will reject an arbitrary size. So your worry about u24 or other odd sizes isn't really valid. But if you want to be doubly paranoid for when we add a new firmware backend (shoot me now), you could move this from the swnode implementation to the fwnode implementation:

        if (!is_power_of_2(elem_size) || elem_size > sizeof(u64))
                return -ENXIO;

That might work. But still an interface of int_array seems lower by level than typed ones.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#24 lore raw

Re: [PATCH 03/10] device property: Add fwnode_property_read_int_array()

Quoted 35 lines · Andy Shevchenko
Quoted 10 lines · Rob Herring
Quoted 8 lines · Andy Shevchenko
Quoted 7 lines · Rob Herring
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Remo Senekowitsch

The rust bindings for reading device properties has a single implementation supporting differing sizes of integers. The fwnode C API already has a similar interface, but it is not exposed with the fwnode_property_ API. Add the fwnode_property_read_int_array() wrapper.

...

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 1 line

+EXPORT_SYMBOL_GPL(fwnode_property_read_int_array);

I'm not sure about this. We have a lot of assumptions in the code that the arrays beneath are only represented by the selected number of integer types. This opens a Pandora's box, e.g., reading in u24, which is not supported by the upper layers..

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

We can probably drop the export if it is just that which you object to.

Yes, this is main point, but dropping it does not prevent from still using in the complied-in code. Is it possible to hide it better?

Don't put any declaration in the header and declare it in the rust code? But lack of declaration generates warnings.

Exactly. And I believe we have the typed versions of int_array for a reason. Otherwise what's the point in having them to begin with?
(The first what comes to my mind is a compile time type checking, so we don't
try to load u8 with u32 data or any other dirty tricks.)

Quoted 7 lines

Maybe it deserves a header that can be included explicitly in the rust stuff and being checked at compile time to avoid people using that? Can we achieve something like C preprocessor

#ifndef FOO
#error This header must not be included directly
#endif

Looks there are RUST_BINDGEN or __BINDGEN__ define which could be used here. Don't need a header, just wrap the declaration. No idea which one would be preferred as there's exactly 1 use of each. Miguel?

Rob

#25 lore raw

Re: [PATCH 03/10] device property: Add fwnode_property_read_int_array()

Quoted 4 lines · Rob Herring

Looks there are RUST_BINDGEN or __BINDGEN__ define which could be used here. Don't need a header, just wrap the declaration. No idea which one would be preferred as there's exactly 1 use of each. Miguel?

If you mean bcachefs' RUST_BINDGEN, then I think that one comes from bcachefs-tools, i.e. we don't define it.

Cheers,
Miguel

#26 lore raw

Re: [PATCH 03/10] device property: Add fwnode_property_read_int_array()

Quoted 8 lines · Miguel Ojeda
Quoted 4 lines · Rob Herring

Looks there are RUST_BINDGEN or __BINDGEN__ define which could be used here. Don't need a header, just wrap the declaration. No idea which one would be preferred as there's exactly 1 use of each. Miguel?

If you mean bcachefs' RUST_BINDGEN, then I think that one comes from bcachefs-tools, i.e. we don't define it.

But is there a way to have a function that is not really exported to be used in Rust only code?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#27 lore raw

Re: [PATCH 03/10] device property: Add fwnode_property_read_int_array()

Quoted 8 lines · Andy Shevchenko
Quoted 6 lines · Miguel Ojeda
Quoted 4 lines · Rob Herring

Looks there are RUST_BINDGEN or __BINDGEN__ define which could be used here. Don't need a header, just wrap the declaration. No idea which one would be preferred as there's exactly 1 use of each. Miguel?

Quoted 5 lines
Quoted 2 lines

If you mean bcachefs' RUST_BINDGEN, then I think that one comes from bcachefs-tools, i.e. we don't define it.

But is there a way to have a function that is not really exported to be used in Rust only code?

#ifdef __BINDGEN__
int fwnode_property_read_int_array(const struct fwnode_handle *fwnode, const char *propname,

                                unsigned int elem_size, void *val, size_t nval);

#endif

Rob

#28 lore raw

Re: [PATCH 03/10] device property: Add fwnode_property_read_int_array()

Quoted 12 lines · Rob Herring
Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Miguel Ojeda
Quoted 4 lines · Rob Herring

Looks there are RUST_BINDGEN or __BINDGEN__ define which could be used here. Don't need a header, just wrap the declaration. No idea which one would be preferred as there's exactly 1 use of each. Miguel?

If you mean bcachefs' RUST_BINDGEN, then I think that one comes from bcachefs-tools, i.e. we don't define it.

Quoted 8 lines
Quoted 2 lines

But is there a way to have a function that is not really exported to be used in Rust only code?

#ifdef __BINDGEN__
int fwnode_property_read_int_array(const struct fwnode_handle *fwnode, const char *propname,

                                unsigned int elem_size, void *val, size_t nval);

#endif

Okay. This can be working compromise. Meanwhile I leave the discussion go on how to proceed the best on the Rust code side. It seems to me if the macro approach is indeed looks nice (let's say comparatively nice as this piece in C header) I would prefer that we do not export it at all.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#29 lore raw

PATCH 4/10 rust: Add bindings for reading device properties

In reply to Remo Senekowitsch

The device property API is a firmware agnostic API for reading properties from firmware (DT/ACPI) devices nodes and swnodes.

While the C API takes a pointer to a caller allocated variable/buffer, the rust API is designed to return a value and can be used in struct initialization. Rust generics are also utilized to support different sizes of properties (e.g. u8, u16, u32).

---
 rust/kernel/property.rs | 153 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 151 insertions(+), 2 deletions(-)
rust/kernel/property.rs +151 -2
index b0a4bb63a..4756ea766 100644
@@ -4,9 +4,17 @@
 //!
 //! C header: [`include/linux/property.h`](srctree/include/linux/property.h)
 
-use core::ptr;
+use core::{ffi::c_void, mem::MaybeUninit, ptr};
 
-use crate::{bindings, device::Device, str::CStr, types::Opaque};
+use crate::{
+    alloc::KVec,
+    bindings,
+    device::Device,
+    error::{to_result, Result},
+    prelude::*,
+    str::{CStr, CString},
+    types::{Integer, Opaque},
+};
 
 impl Device {
     /// Obtain the fwnode corresponding to the device.
@@ -26,6 +34,41 @@ fn fwnode(&self) -> &FwNode {
     pub fn property_present(&self, name: &CStr) -> bool {
         self.fwnode().property_present(name)
     }
+
+    /// Returns if a firmware property `name` is true or false
+    pub fn property_read_bool(&self, name: &CStr) -> bool {
+        self.fwnode().property_read_bool(name)
+    }
+
+    /// Returns the index of matching string `match_str` for firmware string property `name`
+    pub fn property_read_string(&self, name: &CStr) -> Result<CString> {
+        self.fwnode().property_read_string(name)
+    }
+
+    /// Returns the index of matching string `match_str` for firmware string property `name`
+    pub fn property_match_string(&self, name: &CStr, match_str: &CStr) -> Result<usize> {
+        self.fwnode().property_match_string(name, match_str)
+    }
+
+    /// Returns firmware property `name` integer scalar value
+    pub fn property_read<T: Integer>(&self, name: &CStr) -> Result<T> {
+        self.fwnode().property_read(name)
+    }
+
+    /// Returns firmware property `name` integer array values
+    pub fn property_read_array<T: Integer, const N: usize>(&self, name: &CStr) -> Result<[T; N]> {
+        self.fwnode().property_read_array(name)
+    }
+
+    /// Returns firmware property `name` integer array values in a KVec
+    pub fn property_read_array_vec<T: Integer>(&self, name: &CStr, len: usize) -> Result<KVec<T>> {
+        self.fwnode().property_read_array_vec(name, len)
+    }
+
+    /// Returns integer array length for firmware property `name`
+    pub fn property_count_elem<T: Integer>(&self, name: &CStr) -> Result<usize> {
+        self.fwnode().property_count_elem::<T>(name)
+    }
 }
 
 /// A reference-counted fwnode_handle.
@@ -57,6 +100,112 @@ pub fn property_present(&self, name: &CStr) -> bool {
         // SAFETY: By the invariant of `CStr`, `name` is null-terminated.
         unsafe { bindings::fwnode_property_present(self.as_raw().cast_const(), name.as_char_ptr()) }
     }
+
+    /// Returns if a firmware property `name` is true or false
+    pub fn property_read_bool(&self, name: &CStr) -> bool {
+        // SAFETY: `name` is non-null and null-terminated. `self.as_raw` is valid
+        // because `self` is valid.
+        unsafe { bindings::fwnode_property_read_bool(self.as_raw(), name.as_char_ptr()) }
+    }
+
+    /// Returns the index of matching string `match_str` for firmware string property `name`
+    pub fn property_read_string(&self, name: &CStr) -> Result<CString> {
+        let mut str = core::ptr::null_mut();
+        let pstr: *mut _ = &mut str;
+
+        // SAFETY: `name` is non-null and null-terminated. `self.as_raw` is
+        // valid because `self` is valid.
+        let ret = unsafe {
+            bindings::fwnode_property_read_string(self.as_raw(), name.as_char_ptr(), pstr as _)
+        };
+        to_result(ret)?;
+
+        // SAFETY: `pstr` contains a non-null ptr on success
+        let str = unsafe { CStr::from_char_ptr(*pstr) };
+        Ok(str.try_into()?)
+    }
+
+    /// Returns the index of matching string `match_str` for firmware string property `name`
+    pub fn property_match_string(&self, name: &CStr, match_str: &CStr) -> Result<usize> {
+        // SAFETY: `name` and `match_str` are non-null and null-terminated. `self.as_raw` is
+        // valid because `self` is valid.
+        let ret = unsafe {
+            bindings::fwnode_property_match_string(
+                self.as_raw(),
+                name.as_char_ptr(),
+                match_str.as_char_ptr(),
+            )
+        };
+        to_result(ret)?;
+        Ok(ret as usize)
+    }
+
+    /// Returns firmware property `name` integer scalar value
+    pub fn property_read<T: Integer>(&self, name: &CStr) -> Result<T> {
+        let val: [_; 1] = Self::property_read_array(self, name)?;
+        Ok(val[0])
+    }
+
+    /// Returns firmware property `name` integer array values
+    pub fn property_read_array<T: Integer, const N: usize>(&self, name: &CStr) -> Result<[T; N]> {
+        let val: [MaybeUninit<T>; N] = [const { MaybeUninit::uninit() }; N];
+
+        // SAFETY: `name` is non-null and null-terminated. `self.as_raw` is valid
+        // because `self` is valid. `val.as_ptr` is valid because `val` is valid.
+        let ret = unsafe {
+            bindings::fwnode_property_read_int_array(
+                self.as_raw(),
+                name.as_char_ptr(),
+                T::SIZE as u32,
+                val.as_ptr() as *mut c_void,
+                val.len(),
+            )
+        };
+        to_result(ret)?;
+
+        // SAFETY: `val` is always initialized when
+        // fwnode_property_read_int_array is successful.
+        Ok(val.map(|v| unsafe { v.assume_init() }))
+    }
+
+    /// Returns firmware property `name` integer array values in a KVec
+    pub fn property_read_array_vec<T: Integer>(&self, name: &CStr, len: usize) -> Result<KVec<T>> {
+        let mut val: KVec<T> = KVec::with_capacity(len, GFP_KERNEL)?;
+
+        // SAFETY: `name` is non-null and null-terminated. `self.as_raw` is valid
+        // because `self` is valid. `val.as_ptr` is valid because `val` is valid.
+        to_result(unsafe {
+            bindings::fwnode_property_read_int_array(
+                self.as_raw(),
+                name.as_char_ptr(),
+                T::SIZE as u32,
+                val.as_ptr() as *mut c_void,
+                len,
+            )
+        })?;
+
+        // SAFETY: fwnode_property_read_int_array() writes exactly `len` entries on success
+        unsafe { val.set_len(len) }
+        Ok(val)
+    }
+
+    /// Returns integer array length for firmware property `name`
+    pub fn property_count_elem<T: Integer>(&self, name: &CStr) -> Result<usize> {
+        // SAFETY: `name` is non-null and null-terminated. `self.as_raw` is valid
+        // because `self` is valid. Passing null pointer buffer is valid to obtain
+        // the number of elements in the property array.
+        let ret = unsafe {
+            bindings::fwnode_property_read_int_array(
+                self.as_raw(),
+                name.as_char_ptr(),
+                T::SIZE as u32,
+                ptr::null_mut(),
+                0,
+            )
+        };
+        to_result(ret)?;
+        Ok(ret as usize)
+    }
 }
 
 // SAFETY: Instances of `FwNode` are always reference-counted.
Signature · 1 line
2.49.0
maintainer
#30 lore raw

Re: [PATCH 04/10] rust: Add bindings for reading device properties

Quoted 40 lines · Remo Senekowitsch

The device property API is a firmware agnostic API for reading properties from firmware (DT/ACPI) devices nodes and swnodes.

While the C API takes a pointer to a caller allocated variable/buffer, the rust API is designed to return a value and can be used in struct initialization. Rust generics are also utilized to support different sizes of properties (e.g. u8, u16, u32).

---
 rust/kernel/property.rs | 153 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 151 insertions(+), 2 deletions(-)
rust/kernel/property.rs +10 -2
index b0a4bb63a..4756ea766 100644
@@ -4,9 +4,17 @@
 //!
 //! C header: [`include/linux/property.h`](srctree/include/linux/property.h)
 
-use core::ptr;
+use core::{ffi::c_void, mem::MaybeUninit, ptr};
 
-use crate::{bindings, device::Device, str::CStr, types::Opaque};
+use crate::{
+    alloc::KVec,
+    bindings,
+    device::Device,
+    error::{to_result, Result},
+    prelude::*,
+    str::{CStr, CString},
+    types::{Integer, Opaque},
+};
 
 impl Device {
     /// Obtain the fwnode corresponding to the device.

rust/kernel/property.rs fn fwnode(&self) -> &FwNode { · line 43

Quoted 10 lines
rust/kernel/property.rs +7 -0
@@ -26,6 +34,41 @@ fn fwnode(&self) -> &FwNode {
     pub fn property_present(&self, name: &CStr) -> bool {
         self.fwnode().property_present(name)
     }
+
+    /// Returns if a firmware property `name` is true or false
+    pub fn property_read_bool(&self, name: &CStr) -> bool {
+        self.fwnode().property_read_bool(name)
+    }
+
+    /// Returns the index of matching string `match_str` for firmware string property `name`

Comment doesn't match the function.

Quoted 34 lines
rust/kernel/property.rs +28 -0
+    pub fn property_read_string(&self, name: &CStr) -> Result<CString> {
+        self.fwnode().property_read_string(name)
+    }
+
+    /// Returns the index of matching string `match_str` for firmware string property `name`
+    pub fn property_match_string(&self, name: &CStr, match_str: &CStr) -> Result<usize> {
+        self.fwnode().property_match_string(name, match_str)
+    }
+
+    /// Returns firmware property `name` integer scalar value
+    pub fn property_read<T: Integer>(&self, name: &CStr) -> Result<T> {
+        self.fwnode().property_read(name)
+    }
+
+    /// Returns firmware property `name` integer array values
+    pub fn property_read_array<T: Integer, const N: usize>(&self, name: &CStr) -> Result<[T; N]> {
+        self.fwnode().property_read_array(name)
+    }
+
+    /// Returns firmware property `name` integer array values in a KVec
+    pub fn property_read_array_vec<T: Integer>(&self, name: &CStr, len: usize) -> Result<KVec<T>> {
+        self.fwnode().property_read_array_vec(name, len)
+    }
+
+    /// Returns integer array length for firmware property `name`
+    pub fn property_count_elem<T: Integer>(&self, name: &CStr) -> Result<usize> {
+        self.fwnode().property_count_elem::<T>(name)
+    }
 }
 
 /// A reference-counted fwnode_handle.
@@ -57,6 +100,112 @@ pub fn property_present(&self, name: &CStr) -> bool {
         // SAFETY: By the invariant of `CStr`, `name` is null-terminated.
         unsafe { bindings::fwnode_property_present(self.as_raw().cast_const(), name.as_char_ptr()) }

rust/kernel/property.rs pub fn property_present(&self, name: &CStr) -> bool { · line 111

Quoted 10 lines
rust/kernel/property.rs +9 -0
     }
+
+    /// Returns if a firmware property `name` is true or false
+    pub fn property_read_bool(&self, name: &CStr) -> bool {
+        // SAFETY: `name` is non-null and null-terminated. `self.as_raw` is valid
+        // because `self` is valid.
+        unsafe { bindings::fwnode_property_read_bool(self.as_raw(), name.as_char_ptr()) }
+    }
+
+    /// Returns the index of matching string `match_str` for firmware string property `name`

Same comment copy-n-paste mismatch.

Quoted 5 lines
rust/kernel/property.rs +5 -0
+    pub fn property_read_string(&self, name: &CStr) -> Result<CString> {
+        let mut str = core::ptr::null_mut();
+        let pstr: *mut _ = &mut str;
+
+        // SAFETY: `name` is non-null and null-terminated. `self.as_raw` is

rust/kernel/property.rs pub fn property_present(&self, name: &CStr) -> bool { · line 126

Quoted 10 lines
rust/kernel/property.rs +10 -0
+        // valid because `self` is valid.
+        let ret = unsafe {
+            bindings::fwnode_property_read_string(self.as_raw(), name.as_char_ptr(), pstr as _)
+        };
+        to_result(ret)?;
+
+        // SAFETY: `pstr` contains a non-null ptr on success
+        let str = unsafe { CStr::from_char_ptr(*pstr) };
+        Ok(str.try_into()?)
+    }

There's a problem with the C version of this function that I'd like to not repeat in Rust especially since ownership is clear.

The issue is that we never know when the returned string is no longer needed. For DT overlays, we need to be able free the string when/if an overlay is removed. Though overlays are somewhat orthogonal to this. It's really just when the property's node refcount goes to 0 that the property value could be freed.

So this function should probably return a copy of the string that the caller owns.

Rob

#31 lore raw

Re: [PATCH 04/10] rust: Add bindings for reading device properties

Quoted 20 lines · Rob Herring
Quoted 5 lines · Remo Senekowitsch
rust/kernel/property.rs +5 -0
+    pub fn property_read_string(&self, name: &CStr) -> Result<CString> {
+        let mut str = core::ptr::null_mut();
+        let pstr: *mut _ = &mut str;
+
+        // SAFETY: `name` is non-null and null-terminated. `self.as_raw` is

rust/kernel/property.rs pub fn property_present(&self, name: &CStr) -> bool { · line 126

Quoted 10 lines
rust/kernel/property.rs +10 -0
+        // valid because `self` is valid.
+        let ret = unsafe {
+            bindings::fwnode_property_read_string(self.as_raw(), name.as_char_ptr(), pstr as _)
+        };
+        to_result(ret)?;
+
+        // SAFETY: `pstr` contains a non-null ptr on success
+        let str = unsafe { CStr::from_char_ptr(*pstr) };
+        Ok(str.try_into()?)
+    }

There's a problem with the C version of this function that I'd like to not repeat in Rust especially since ownership is clear.

Quoted 8 lines

The issue is that we never know when the returned string is no longer needed. For DT overlays, we need to be able free the string when/if an overlay is removed. Though overlays are somewhat orthogonal to this. It's really just when the property's node refcount goes to 0 that the property value could be freed.

So this function should probably return a copy of the string that the caller owns.

I think the function already does that. The variable str is of type &CStr, so the expression str.try_into()? on the last line calls the implementation of impl TryFrom<&CStr> for CString, which is documented here:
https://rust.docs.kernel.org/kernel/str/struct.CString.html#impl-TryFrom%3C%26CStr%3E-for-CString

And looking at the source, it shows that the CString is created with a new owned memory allocation:
https://rust.docs.kernel.org/src/kernel/str.rs.html#878-890

Remo

#32 lore raw

PATCH 5/10 rust: Read properties via single generic method

In reply to Remo Senekowitsch

This can probably be squashed into its parent. I haven't done that yet in case there is something wrong with the generic approach and the previous one is preferred.

---
 rust/kernel/property.rs | 171 +++++++++++++++++++++++-----------------
 1 file changed, 99 insertions(+), 72 deletions(-)
rust/kernel/property.rs +99 -72
index 4756ea766..4a03008ce 100644
@@ -35,31 +35,11 @@ pub fn property_present(&self, name: &CStr) -> bool {
         self.fwnode().property_present(name)
     }
 
-    /// Returns if a firmware property `name` is true or false
-    pub fn property_read_bool(&self, name: &CStr) -> bool {
-        self.fwnode().property_read_bool(name)
-    }
-
-    /// Returns the index of matching string `match_str` for firmware string property `name`
-    pub fn property_read_string(&self, name: &CStr) -> Result<CString> {
-        self.fwnode().property_read_string(name)
-    }
-
     /// Returns the index of matching string `match_str` for firmware string property `name`
     pub fn property_match_string(&self, name: &CStr, match_str: &CStr) -> Result<usize> {
         self.fwnode().property_match_string(name, match_str)
     }
 
-    /// Returns firmware property `name` integer scalar value
-    pub fn property_read<T: Integer>(&self, name: &CStr) -> Result<T> {
-        self.fwnode().property_read(name)
-    }
-
-    /// Returns firmware property `name` integer array values
-    pub fn property_read_array<T: Integer, const N: usize>(&self, name: &CStr) -> Result<[T; N]> {
-        self.fwnode().property_read_array(name)
-    }
-
     /// Returns firmware property `name` integer array values in a KVec
     pub fn property_read_array_vec<T: Integer>(&self, name: &CStr, len: usize) -> Result<KVec<T>> {
         self.fwnode().property_read_array_vec(name, len)
@@ -69,6 +49,11 @@ pub fn property_read_array_vec<T: Integer>(&self, name: &CStr, len: usize) -> Re
     pub fn property_count_elem<T: Integer>(&self, name: &CStr) -> Result<usize> {
         self.fwnode().property_count_elem::<T>(name)
     }
+
+    /// Returns firmware property `name` integer scalar value
+    pub fn property_read<T: Property>(&self, name: &CStr) -> Result<T> {
+        self.fwnode().property_read(name)
+    }
 }
 
 /// A reference-counted fwnode_handle.
@@ -101,30 +86,6 @@ pub fn property_present(&self, name: &CStr) -> bool {
         unsafe { bindings::fwnode_property_present(self.as_raw().cast_const(), name.as_char_ptr()) }
     }
 
-    /// Returns if a firmware property `name` is true or false
-    pub fn property_read_bool(&self, name: &CStr) -> bool {
-        // SAFETY: `name` is non-null and null-terminated. `self.as_raw` is valid
-        // because `self` is valid.
-        unsafe { bindings::fwnode_property_read_bool(self.as_raw(), name.as_char_ptr()) }
-    }
-
-    /// Returns the index of matching string `match_str` for firmware string property `name`
-    pub fn property_read_string(&self, name: &CStr) -> Result<CString> {
-        let mut str = core::ptr::null_mut();
-        let pstr: *mut _ = &mut str;
-
-        // SAFETY: `name` is non-null and null-terminated. `self.as_raw` is
-        // valid because `self` is valid.
-        let ret = unsafe {
-            bindings::fwnode_property_read_string(self.as_raw(), name.as_char_ptr(), pstr as _)
-        };
-        to_result(ret)?;
-
-        // SAFETY: `pstr` contains a non-null ptr on success
-        let str = unsafe { CStr::from_char_ptr(*pstr) };
-        Ok(str.try_into()?)
-    }
-
     /// Returns the index of matching string `match_str` for firmware string property `name`
     pub fn property_match_string(&self, name: &CStr, match_str: &CStr) -> Result<usize> {
         // SAFETY: `name` and `match_str` are non-null and null-terminated. `self.as_raw` is
@@ -140,34 +101,6 @@ pub fn property_match_string(&self, name: &CStr, match_str: &CStr) -> Result<usi
         Ok(ret as usize)
     }
 
-    /// Returns firmware property `name` integer scalar value
-    pub fn property_read<T: Integer>(&self, name: &CStr) -> Result<T> {
-        let val: [_; 1] = Self::property_read_array(self, name)?;
-        Ok(val[0])
-    }
-
-    /// Returns firmware property `name` integer array values
-    pub fn property_read_array<T: Integer, const N: usize>(&self, name: &CStr) -> Result<[T; N]> {
-        let val: [MaybeUninit<T>; N] = [const { MaybeUninit::uninit() }; N];
-
-        // SAFETY: `name` is non-null and null-terminated. `self.as_raw` is valid
-        // because `self` is valid. `val.as_ptr` is valid because `val` is valid.
-        let ret = unsafe {
-            bindings::fwnode_property_read_int_array(
-                self.as_raw(),
-                name.as_char_ptr(),
-                T::SIZE as u32,
-                val.as_ptr() as *mut c_void,
-                val.len(),
-            )
-        };
-        to_result(ret)?;
-
-        // SAFETY: `val` is always initialized when
-        // fwnode_property_read_int_array is successful.
-        Ok(val.map(|v| unsafe { v.assume_init() }))
-    }
-
     /// Returns firmware property `name` integer array values in a KVec
     pub fn property_read_array_vec<T: Integer>(&self, name: &CStr, len: usize) -> Result<KVec<T>> {
         let mut val: KVec<T> = KVec::with_capacity(len, GFP_KERNEL)?;
@@ -206,6 +139,38 @@ pub fn property_count_elem<T: Integer>(&self, name: &CStr) -> Result<usize> {
         to_result(ret)?;
         Ok(ret as usize)
     }
+
+    /// Returns the value of firmware property `name`.
+    ///
+    /// This method is generic over the type of value to read. Informally,
+    /// the types that can be read are booleans, strings, integers and arrays
+    /// of integers.
+    ///
+    /// Reading a `KVec` of integers is done with the
+    /// separate method [Self::property_read_array_vec], because it takes an
+    /// additional `len` argument.
+    ///
+    /// When reading a boolean, this method never fails. A missing property
+    /// is interpreted as `false`, whereas a present property is interpreted
+    /// as `true`.
+    ///
+    /// For more precise documentation about what types can be read, see
+    /// the [implementors of Property][Property#implementors] and [its
+    /// implementations on foreign types][Property#foreign-impls].
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// # use crate::{device::Device, types::CString};
+    /// fn examples(dev: &Device) -> Result {
+    ///     let fwnode = dev.fwnode();
+    ///     let b: bool = fwnode.property_read("some-bool")?;
+    ///     let s: CString = fwnode.property_read("some-str")?;
+    /// }
+    /// ```
+    pub fn property_read<T: Property>(&self, name: &CStr) -> Result<T> {
+        T::read(&self, name)
+    }
 }
 
 // SAFETY: Instances of `FwNode` are always reference-counted.
@@ -220,3 +185,65 @@ unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
         unsafe { bindings::fwnode_handle_put(obj.cast().as_ptr()) }
     }
 }
+
+/// Implemented for all types that can be read as properties.
+///
+/// This is used to make [FwNode::property_read] generic over the type of
+/// property being read.
+pub trait Property: Sized {
+    /// Used to make [FwNode::property_read] generic.
+    fn read(fwnode: &FwNode, name: &CStr) -> Result<Self>;
+}
+
+impl Property for bool {
+    fn read(fwnode: &FwNode, name: &CStr) -> Result<Self> {
+        // SAFETY: `name` is non-null and null-terminated. `fwnode.as_raw` is valid
+        // because `fwnode` is valid.
+        Ok(unsafe { bindings::fwnode_property_read_bool(fwnode.as_raw(), name.as_char_ptr()) })
+    }
+}
+impl Property for CString {
+    fn read(fwnode: &FwNode, name: &CStr) -> Result<Self> {
+        let mut str: *mut u8 = ptr::null_mut();
+        let pstr: *mut _ = &mut str;
+
+        // SAFETY: `name` is non-null and null-terminated. `fwnode.as_raw` is
+        // valid because `fwnode` is valid.
+        let ret = unsafe {
+            bindings::fwnode_property_read_string(fwnode.as_raw(), name.as_char_ptr(), pstr.cast())
+        };
+        to_result(ret)?;
+
+        // SAFETY: `pstr` contains a non-null ptr on success
+        let str = unsafe { CStr::from_char_ptr(*pstr) };
+        Ok(str.try_into()?)
+    }
+}
+impl<T: Integer, const N: usize> Property for [T; N] {
+    fn read(fwnode: &FwNode, name: &CStr) -> Result<Self> {
+        let mut val: [MaybeUninit<T>; N] = [const { MaybeUninit::uninit() }; N];
+
+        // SAFETY: `name` is non-null and null-terminated. `fwnode.as_raw` is valid
+        // because `fwnode` is valid. `val.as_ptr` is valid because `val` is valid.
+        let ret = unsafe {
+            bindings::fwnode_property_read_int_array(
+                fwnode.as_raw(),
+                name.as_char_ptr(),
+                T::SIZE as u32,
+                val.as_mut_ptr().cast(),
+                val.len(),
+            )
+        };
+        to_result(ret)?;
+
+        // SAFETY: `val` is always initialized when
+        // fwnode_property_read_int_array is successful.
+        Ok(val.map(|v| unsafe { v.assume_init() }))
+    }
+}
+impl<T: Integer> Property for T {
+    fn read(fwnode: &FwNode, name: &CStr) -> Result<T> {
+        let val: [_; 1] = <[T; 1] as Property>::read(fwnode, name)?;
+        Ok(val[0])
+    }
+}
Signature · 1 line
2.49.0
maintainer
#33 lore raw

Re: [PATCH 05/10] rust: Read properties via single generic method

Quoted 3 lines · Remo Senekowitsch

This can probably be squashed into its parent. I haven't done that yet in case there is something wrong with the generic approach and the previous one is preferred.

Looks good to me other than the returning of strings issue. And yes it should be squashed. (Really, it should have been in this series so reviewers aren't reviewing code you delete in the next patch).

Quoted 5 lines
---
 rust/kernel/property.rs | 171 +++++++++++++++++++++++-----------------
 1 file changed, 99 insertions(+), 72 deletions(-)
#34 lore raw

PATCH 6/10 rust: property: Add child accessor and iterator

In reply to Remo Senekowitsch
---
 rust/kernel/property.rs | 57 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 56 insertions(+), 1 deletion(-)
rust/kernel/property.rs +56 -1
index 4a03008ce..dc927ad93 100644
@@ -13,7 +13,7 @@
     error::{to_result, Result},
     prelude::*,
     str::{CStr, CString},
-    types::{Integer, Opaque},
+    types::{ARef, Integer, Opaque},
 };
 
 impl Device {
@@ -54,6 +54,16 @@ pub fn property_count_elem<T: Integer>(&self, name: &CStr) -> Result<usize> {
     pub fn property_read<T: Property>(&self, name: &CStr) -> Result<T> {
         self.fwnode().property_read(name)
     }
+
+    /// Returns first matching named child node handle.
+    pub fn get_child_by_name(&self, name: &CStr) -> Option<ARef<FwNode>> {
+        self.fwnode().get_child_by_name(name)
+    }
+
+    /// Returns an iterator over a node's children.
+    pub fn children<'a>(&'a self) -> impl Iterator<Item = ARef<FwNode>> + 'a {
+        self.fwnode().children()
+    }
 }
 
 /// A reference-counted fwnode_handle.
@@ -75,6 +85,11 @@ pub fn property_read<T: Property>(&self, name: &CStr) -> Result<T> {
 pub struct FwNode(Opaque<bindings::fwnode_handle>);
 
 impl FwNode {
+    // SAFETY: `raw` must have its refcount incremented.
+    unsafe fn from_raw(raw: *mut bindings::fwnode_handle) -> ARef<Self> {
+        unsafe { ARef::from_raw(ptr::NonNull::new_unchecked(raw.cast())) }
+    }
+
     /// Obtain the raw `struct fwnode_handle *`.
     pub(crate) fn as_raw(&self) -> *mut bindings::fwnode_handle {
         self.0.get()
@@ -171,6 +186,46 @@ pub fn property_count_elem<T: Integer>(&self, name: &CStr) -> Result<usize> {
     pub fn property_read<T: Property>(&self, name: &CStr) -> Result<T> {
         T::read(&self, name)
     }
+
+    /// Returns first matching named child node handle.
+    pub fn get_child_by_name(&self, name: &CStr) -> Option<ARef<Self>> {
+        // SAFETY: `self` and `name` are valid.
+        let child =
+            unsafe { bindings::fwnode_get_named_child_node(self.as_raw(), name.as_char_ptr()) };
+        if child.is_null() {
+            return None;
+        }
+        // SAFETY: `fwnode_get_named_child_node` returns a pointer with refcount incremented.
+        Some(unsafe { Self::from_raw(child) })
+    }
+
+    /// Returns an iterator over a node's children.
+    pub fn children<'a>(&'a self) -> impl Iterator<Item = ARef<FwNode>> + 'a {
+        let mut prev: Option<ARef<FwNode>> = None;
+
+        core::iter::from_fn(move || {
+            let prev_ptr = match prev.take() {
+                None => ptr::null_mut(),
+                Some(prev) => {
+                    // We will pass `prev` to `fwnode_get_next_child_node`,
+                    // which decrements its refcount, so we use
+                    // `ARef::into_raw` to avoid decrementing the refcount
+                    // twice.
+                    let prev = ARef::into_raw(prev);
+                    prev.as_ptr().cast()
+                }
+            };
+            let next = unsafe { bindings::fwnode_get_next_child_node(self.as_raw(), prev_ptr) };
+            if next.is_null() {
+                return None;
+            }
+            // SAFETY: `fwnode_get_next_child_node` returns a pointer with
+            // refcount incremented.
+            let next = unsafe { FwNode::from_raw(next) };
+            prev = Some(next.clone());
+            Some(next)
+        })
+    }
 }
 
 // SAFETY: Instances of `FwNode` are always reference-counted.
Signature · 1 line
2.49.0
#35 lore raw

Re: [PATCH 06/10] rust: property: Add child accessor and iterator

On Wed, Mar 26, 2025 at 12:13 PM Remo Senekowitsch Wrote:

rust/kernel/property.rs pub fn property_read<T: Property>(&self, name: &CStr) -> Result<T> { · line 91

Quoted 5 lines

impl FwNode {

rust/kernel/property.rs +4 -0
+    // SAFETY: `raw` must have its refcount incremented.
+    unsafe fn from_raw(raw: *mut bindings::fwnode_handle) -> ARef<Self> {
+        unsafe { ARef::from_raw(ptr::NonNull::new_unchecked(raw.cast())) }
+    }

this safety comment should say why it is ok to cast from a struct fwnode_handle* to a *const FwNode

rust/kernel/property.rs pub fn property_count_elem<T: Integer>(&self, name: &CStr) -> Result<usize> { · line 218

Quoted 9 lines
rust/kernel/property.rs +9 -0
+                    // We will pass `prev` to `fwnode_get_next_child_node`,
+                    // which decrements its refcount, so we use
+                    // `ARef::into_raw` to avoid decrementing the refcount
+                    // twice.
+                    let prev = ARef::into_raw(prev);
+                    prev.as_ptr().cast()
+                }
+            };
+            let next = unsafe { bindings::fwnode_get_next_child_node(self.as_raw(), prev_ptr) };

this is missing a safety comment

rust/kernel/property.rs pub fn property_count_elem<T: Integer>(&self, name: &CStr) -> Result<usize> { · line 221

Quoted 3 lines
rust/kernel/property.rs +3 -0
+            if next.is_null() {
+                return None;
+            }

Andrew

#37 lore raw

PATCH 7/10 rust: Add arrayvec

In reply to Remo Senekowitsch

This patch is basically a proof of concept intendend to gather feedback about how to do this properly. Normally I would want to use the crate from crates.io[1], but that's not an option in the kernel. We could also vendor the entire source code of arrayvec. I'm not sure if people will be happy with that.

[1] https://crates.io/crates/arrayvec

---
 rust/kernel/arrayvec.rs | 81 +++++++++++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs      |  1 +
 2 files changed, 82 insertions(+)
 create mode 100644 rust/kernel/arrayvec.rs
rust/kernel/arrayvec.rs new file +81 -0
new file mode 100644
index 000000000..041e7dcce
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Provides [ArrayVec], a stack-allocated vector with static capacity.
+
+use core::mem::MaybeUninit;
+
+/// A stack-allocated vector with statically fixed capacity.
+///
+/// This can be useful to avoid heap allocation and still ensure safety where a
+/// small but dynamic number of elements is needed.
+///
+/// For example, consider a function that returns a variable number of values,
+/// but no more than 8. In C, one might achieve this by passing a pointer to
+/// a stack-allocated array as an out-parameter and making the function return
+/// the actual number of elements. This is not safe, because nothing prevents
+/// the caller from reading elements from the array that weren't actually
+/// initialized by the function. `ArrayVec` solves this problem, users are
+/// prevented from accessing uninitialized elements.
+///
+/// This basically exists already (in a much more mature form) on crates.io:
+/// <https://crates.io/crates/arrayvec>
+#[derive(Debug)]
+pub struct ArrayVec<const N: usize, T> {
+    array: [core::mem::MaybeUninit<T>; N],
+    len: usize,
+}
+
+impl<const N: usize, T> ArrayVec<N, T> {
+    /// Adds a new element to the end of the vector.
+    ///
+    /// # Panics
+    ///
+    /// Panics if the vector is already full.
+    pub fn push(&mut self, elem: T) {
+        if self.len == N {
+            panic!("OOM")
+        }
+        self.array[self.len] = MaybeUninit::new(elem);
+        self.len += 1;
+    }
+
+    /// Returns the length of the vector.
+    pub fn len(&self) -> usize {
+        self.len
+    }
+}
+
+impl<const N: usize, T> Default for ArrayVec<N, T> {
+    fn default() -> Self {
+        Self {
+            array: [const { MaybeUninit::uninit() }; N],
+            len: 0,
+        }
+    }
+}
+
+impl<const N: usize, T> AsRef<[T]> for ArrayVec<N, T> {
+    fn as_ref(&self) -> &[T] {
+        // SAFETY: As per the type invariant, all elements at index < self.len
+        // are initialized.
+        unsafe { core::mem::transmute(&self.array[..self.len]) }
+    }
+}
+
+impl<const N: usize, T> AsMut<[T]> for ArrayVec<N, T> {
+    fn as_mut(&mut self) -> &mut [T] {
+        // SAFETY: As per the type invariant, all elements at index < self.len
+        // are initialized.
+        unsafe { core::mem::transmute(&mut self.array[..self.len]) }
+    }
+}
+
+impl<const N: usize, T> Drop for ArrayVec<N, T> {
+    fn drop(&mut self) {
+        unsafe {
+            let slice: &mut [T] =
+                core::slice::from_raw_parts_mut(self.array.as_mut_ptr().cast(), self.len);
+            core::ptr::drop_in_place(slice);
+        }
+    }
+}
rust/kernel/lib.rs +1 -0
index ca233fd20..0777f7a42 100644
@@ -36,6 +36,7 @@
 pub use ffi;
 
 pub mod alloc;
+pub mod arrayvec;
 #[cfg(CONFIG_BLOCK)]
 pub mod block;
 #[doc(hidden)]
Signature · 1 line
2.49.0
#38 lore raw

Re: [PATCH 07/10] rust: Add arrayvec

On Wed, Mar 26, 2025 at 12:13 PM Remo Senekowitsch Wrote:

rust/kernel/arrayvec.rs line 26

Quoted 4 lines
rust/kernel/arrayvec.rs new file +4 -0
+pub struct ArrayVec<const N: usize, T> {
+    array: [core::mem::MaybeUninit<T>; N],
+    len: usize,
+}

I would reverse the order of the generic arguments so T is first so that it matches the declaration of an array: [T; N]

rust/kernel/arrayvec.rs line 40

Quoted 12 lines
rust/kernel/arrayvec.rs new file +12 -0
+    /// Adds a new element to the end of the vector.
+    ///
+    /// # Panics
+    ///
+    /// Panics if the vector is already full.
+    pub fn push(&mut self, elem: T) {
+        if self.len == N {
+            panic!("OOM")
+        }
+        self.array[self.len] = MaybeUninit::new(elem);
+        self.len += 1;
+    }

This function should not panic. I would rewrite it so that its signature is pub fn push(&mut self, elem: T) -> Result<(), T>

Quoted 5 lines
rust/kernel/arrayvec.rs new file +5 -0
+impl<const N: usize, T> AsRef<[T]> for ArrayVec<N, T> {
+    fn as_ref(&self) -> &[T] {
+        // SAFETY: As per the type invariant, all elements at index < self.len
+        // are initialized.
+        unsafe { core::mem::transmute(&self.array[..self.len]) }

rust/kernel/arrayvec.rs line 71

Quoted 10 lines
rust/kernel/arrayvec.rs new file +10 -0
+    }
+}
+
+impl<const N: usize, T> AsMut<[T]> for ArrayVec<N, T> {
+    fn as_mut(&mut self) -> &mut [T] {
+        // SAFETY: As per the type invariant, all elements at index < self.len
+        // are initialized.
+        unsafe { core::mem::transmute(&mut self.array[..self.len]) }
+    }
+}

I would replace both uses of transmute here with slice::from_raw_parts like you did in drop.

also it's not needed but you could also impl Deref and DerefMut for ArrayVec because you have already impl'ed AsRef and AsMut so it should be simple

rust/kernel/arrayvec.rs line 75

Quoted 3 lines
rust/kernel/arrayvec.rs new file +3 -0
+impl<const N: usize, T> Drop for ArrayVec<N, T> {
+    fn drop(&mut self) {
+        unsafe {

this is missing a safety comment

rust/kernel/arrayvec.rs line 81

Quoted 6 lines
rust/kernel/arrayvec.rs new file +6 -0
+            let slice: &mut [T] =
+                core::slice::from_raw_parts_mut(self.array.as_mut_ptr().cast(), self.len);
+            core::ptr::drop_in_place(slice);
+        }
+    }
+}

Andrew

maintainer
#39 lore raw

Re: [PATCH 07/10] rust: Add arrayvec

In reply to Remo Senekowitsch
Quoted 5 lines · Remo Senekowitsch

This patch is basically a proof of concept intendend to gather feedback about how to do this properly. Normally I would want to use the crate from crates.io[1], but that's not an option in the kernel. We could also vendor the entire source code of arrayvec. I'm not sure if people will be happy with that.

Do we really need this? The only user in this series I could spot was property_get_reference_args(). And I think that with a proper abstraction of struct fwnode_reference_args we could avoid to copy memory at all.

If it turns out we actually need something like this, I'd prefer to move it to rust/kernel/alloc/ and see if it's worth to derive a common trait that maybe can share a few things between ArrayVec and Vec.

Quoted 42 lines

[1] https://crates.io/crates/arrayvec

---
 rust/kernel/arrayvec.rs | 81 +++++++++++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs      |  1 +
 2 files changed, 82 insertions(+)
 create mode 100644 rust/kernel/arrayvec.rs
rust/kernel/arrayvec.rs new file +26 -0
new file mode 100644
index 000000000..041e7dcce
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Provides [ArrayVec], a stack-allocated vector with static capacity.
+
+use core::mem::MaybeUninit;
+
+/// A stack-allocated vector with statically fixed capacity.
+///
+/// This can be useful to avoid heap allocation and still ensure safety where a
+/// small but dynamic number of elements is needed.
+///
+/// For example, consider a function that returns a variable number of values,
+/// but no more than 8. In C, one might achieve this by passing a pointer to
+/// a stack-allocated array as an out-parameter and making the function return
+/// the actual number of elements. This is not safe, because nothing prevents
+/// the caller from reading elements from the array that weren't actually
+/// initialized by the function. `ArrayVec` solves this problem, users are
+/// prevented from accessing uninitialized elements.
+///
+/// This basically exists already (in a much more mature form) on crates.io:
+/// <https://crates.io/crates/arrayvec>
+#[derive(Debug)]
+pub struct ArrayVec<const N: usize, T> {
+    array: [core::mem::MaybeUninit<T>; N],
+    len: usize,
+}

rust/kernel/arrayvec.rs line 36

Quoted 10 lines
rust/kernel/arrayvec.rs new file +10 -0
+
+impl<const N: usize, T> ArrayVec<N, T> {
+    /// Adds a new element to the end of the vector.
+    ///
+    /// # Panics
+    ///
+    /// Panics if the vector is already full.
+    pub fn push(&mut self, elem: T) {
+        if self.len == N {
+            panic!("OOM")

Please do not panic, this should return a Result instead.

#40 lore raw

PATCH 8/10 rust: property: Add property_get_reference_args

In reply to Remo Senekowitsch
---
 rust/kernel/property.rs | 63 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
rust/kernel/property.rs +63 -0
index dc927ad93..f1d0a33ba 100644
@@ -8,6 +8,7 @@
 
 use crate::{
     alloc::KVec,
+    arrayvec::ArrayVec,
     bindings,
     device::Device,
     error::{to_result, Result},
@@ -64,6 +65,20 @@ pub fn get_child_by_name(&self, name: &CStr) -> Option<ARef<FwNode>> {
     pub fn children<'a>(&'a self) -> impl Iterator<Item = ARef<FwNode>> + 'a {
         self.fwnode().children()
     }
+
+    /// Finds a reference with arguments.
+    pub fn property_get_reference_args(
+        &self,
+        prop: &CStr,
+        nargs: NArgs<'_>,
+        index: u32,
+    ) -> Result<(
+        ARef<FwNode>,
+        ArrayVec<{ bindings::NR_FWNODE_REFERENCE_ARGS as usize }, u64>,
+    )> {
+        self.fwnode()
+            .property_get_reference_args(prop, nargs, index)
+    }
 }
 
 /// A reference-counted fwnode_handle.
@@ -226,6 +241,45 @@ pub fn children<'a>(&'a self) -> impl Iterator<Item = ARef<FwNode>> + 'a {
             Some(next)
         })
     }
+
+    /// Finds a reference with arguments.
+    pub fn property_get_reference_args(
+        &self,
+        prop: &CStr,
+        nargs: NArgs<'_>,
+        index: u32,
+    ) -> Result<(
+        ARef<Self>,
+        ArrayVec<{ bindings::NR_FWNODE_REFERENCE_ARGS as usize }, u64>,
+    )> {
+        let mut out_args = bindings::fwnode_reference_args::default();
+
+        let (nargs_prop, nargs) = match nargs {
+            NArgs::Prop(nargs_prop) => (nargs_prop.as_char_ptr(), 0),
+            NArgs::N(nargs) => (ptr::null(), nargs),
+        };
+
+        let ret = unsafe {
+            bindings::fwnode_property_get_reference_args(
+                self.0.get(),
+                prop.as_char_ptr(),
+                nargs_prop,
+                nargs,
+                index,
+                &mut out_args,
+            )
+        };
+        to_result(ret)?;
+
+        let node = unsafe { FwNode::from_raw(out_args.fwnode) };
+        let mut args = ArrayVec::default();
+
+        for i in 0..out_args.nargs {
+            args.push(out_args.args[i as usize]);
+        }
+
+        Ok((node, args))
+    }
 }
 
 // SAFETY: Instances of `FwNode` are always reference-counted.
@@ -302,3 +356,12 @@ fn read(fwnode: &FwNode, name: &CStr) -> Result<T> {
         Ok(val[0])
     }
 }
+
+/// The number of arguments of a reference.
+pub enum NArgs<'a> {
+    /// The name of the property of the reference indicating the number of
+    /// arguments.
+    Prop(&'a CStr),
+    /// The known number of arguments.
+    N(u32),
+}
Signature · 1 line
2.49.0
#41 lore raw

Re: [PATCH 08/10] rust: property: Add property_get_reference_args

On Wed, Mar 26, 2025 at 12:13 PM Remo Senekowitsch Wrote:

Quoted 27 lines
rust/kernel/property.rs +27 -0
+    pub fn property_get_reference_args(
+        &self,
+        prop: &CStr,
+        nargs: NArgs<'_>,
+        index: u32,
+    ) -> Result<(
+        ARef<Self>,
+        ArrayVec<{ bindings::NR_FWNODE_REFERENCE_ARGS as usize }, u64>,
+    )> {
+        let mut out_args = bindings::fwnode_reference_args::default();
+
+        let (nargs_prop, nargs) = match nargs {
+            NArgs::Prop(nargs_prop) => (nargs_prop.as_char_ptr(), 0),
+            NArgs::N(nargs) => (ptr::null(), nargs),
+        };
+
+        let ret = unsafe {
+            bindings::fwnode_property_get_reference_args(
+                self.0.get(),
+                prop.as_char_ptr(),
+                nargs_prop,
+                nargs,
+                index,
+                &mut out_args,
+            )
+        };
+        to_result(ret)?;

rust/kernel/property.rs pub fn children<'a>(&'a self) -> impl Iterator<Item = ARef<FwNode>> + 'a { · line 282

Quoted 10 lines
rust/kernel/property.rs +10 -0
+
+        let node = unsafe { FwNode::from_raw(out_args.fwnode) };
+        let mut args = ArrayVec::default();
+
+        for i in 0..out_args.nargs {
+            args.push(out_args.args[i as usize]);
+        }
+
+        Ok((node, args))
+    }

This function is missing safety comments.

Andrew

#42 lore raw

Re: [PATCH 08/10] rust: property: Add property_get_reference_args

Quoted 2 lines · Andrew Ballance

This function is missing safety comments.

Yeah -- that should be spotted by Clippy.

Remo: did you build with CLIPPY=1?

Thanks!

Cheers,
Miguel

#43 lore raw

Re: [PATCH 08/10] rust: property: Add property_get_reference_args

Quoted 2 lines · Miguel Ojeda

Remo: did you build with CLIPPY=1?

I didn't! Usually I have rust-analyzer configured with clippy, but that doesn't seem to work here. I added it to my build scripts and can see the warnings now, future patches should be free of clippy warnings.

Thanks for the tip!

Remo

maintainer
#44 lore raw

Re: [PATCH 08/10] rust: property: Add property_get_reference_args

In reply to Remo Senekowitsch
Quoted 68 lines · Remo Senekowitsch
---
 rust/kernel/property.rs | 63 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
rust/kernel/property.rs +41 -0
index dc927ad93..f1d0a33ba 100644
@@ -8,6 +8,7 @@
 
 use crate::{
     alloc::KVec,
+    arrayvec::ArrayVec,
     bindings,
     device::Device,
     error::{to_result, Result},
@@ -64,6 +65,20 @@ pub fn get_child_by_name(&self, name: &CStr) -> Option<ARef<FwNode>> {
     pub fn children<'a>(&'a self) -> impl Iterator<Item = ARef<FwNode>> + 'a {
         self.fwnode().children()
     }
+
+    /// Finds a reference with arguments.
+    pub fn property_get_reference_args(
+        &self,
+        prop: &CStr,
+        nargs: NArgs<'_>,
+        index: u32,
+    ) -> Result<(
+        ARef<FwNode>,
+        ArrayVec<{ bindings::NR_FWNODE_REFERENCE_ARGS as usize }, u64>,
+    )> {
+        self.fwnode()
+            .property_get_reference_args(prop, nargs, index)
+    }
 }
 
 /// A reference-counted fwnode_handle.
@@ -226,6 +241,45 @@ pub fn children<'a>(&'a self) -> impl Iterator<Item = ARef<FwNode>> + 'a {
             Some(next)
         })
     }
+
+    /// Finds a reference with arguments.
+    pub fn property_get_reference_args(
+        &self,
+        prop: &CStr,
+        nargs: NArgs<'_>,
+        index: u32,
+    ) -> Result<(
+        ARef<Self>,
+        ArrayVec<{ bindings::NR_FWNODE_REFERENCE_ARGS as usize }, u64>,
+    )> {
+        let mut out_args = bindings::fwnode_reference_args::default();
+
+        let (nargs_prop, nargs) = match nargs {
+            NArgs::Prop(nargs_prop) => (nargs_prop.as_char_ptr(), 0),
+            NArgs::N(nargs) => (ptr::null(), nargs),
+        };
+
+        let ret = unsafe {
+            bindings::fwnode_property_get_reference_args(
+                self.0.get(),
+                prop.as_char_ptr(),
+                nargs_prop,
+                nargs,
+                index,
+                &mut out_args,

rust/kernel/property.rs pub fn children<'a>(&'a self) -> impl Iterator<Item = ARef<FwNode>> + 'a { · line 279

Quoted 10 lines
rust/kernel/property.rs +10 -0
+            )
+        };
+        to_result(ret)?;
+
+        let node = unsafe { FwNode::from_raw(out_args.fwnode) };
+        let mut args = ArrayVec::default();
+
+        for i in 0..out_args.nargs {
+            args.push(out_args.args[i as usize]);
+        }

Why the copy? Can't you just write an abstraction for struct fwnode_reference_args and just return an instance of that?

#45 lore raw

PATCH 9/10 rust: property: Add PropertyGuard

In reply to Remo Senekowitsch

Forcing users to specify whether a property is supposed to be required or not allows us to move error logging of missing required properties into core, preventing a lot of boilerplate in drivers.

---
 rust/kernel/property.rs | 146 ++++++++++++++++++++++++++++++++++------
 1 file changed, 127 insertions(+), 19 deletions(-)
rust/kernel/property.rs +127 -19
index f1d0a33ba..e4f0e5f97 100644
@@ -42,7 +42,11 @@ pub fn property_match_string(&self, name: &CStr, match_str: &CStr) -> Result<usi
     }
 
     /// Returns firmware property `name` integer array values in a KVec
-    pub fn property_read_array_vec<T: Integer>(&self, name: &CStr, len: usize) -> Result<KVec<T>> {
+    pub fn property_read_array_vec<'fwnode, 'name, T: Integer>(
+        &'fwnode self,
+        name: &'name CStr,
+        len: usize,
+    ) -> Result<PropertyGuard<'fwnode, 'name, KVec<T>>> {
         self.fwnode().property_read_array_vec(name, len)
     }
 
@@ -52,12 +56,18 @@ pub fn property_count_elem<T: Integer>(&self, name: &CStr) -> Result<usize> {
     }
 
     /// Returns firmware property `name` integer scalar value
-    pub fn property_read<T: Property>(&self, name: &CStr) -> Result<T> {
+    pub fn property_read<'fwnode, 'name, T: Property>(
+        &'fwnode self,
+        name: &'name CStr,
+    ) -> PropertyGuard<'fwnode, 'name, T> {
         self.fwnode().property_read(name)
     }
 
     /// Returns first matching named child node handle.
-    pub fn get_child_by_name(&self, name: &CStr) -> Option<ARef<FwNode>> {
+    pub fn get_child_by_name<'fwnode, 'name>(
+        &'fwnode self,
+        name: &'name CStr,
+    ) -> PropertyGuard<'fwnode, 'name, ARef<FwNode>> {
         self.fwnode().get_child_by_name(name)
     }
 
@@ -132,12 +142,16 @@ pub fn property_match_string(&self, name: &CStr, match_str: &CStr) -> Result<usi
     }
 
     /// Returns firmware property `name` integer array values in a KVec
-    pub fn property_read_array_vec<T: Integer>(&self, name: &CStr, len: usize) -> Result<KVec<T>> {
+    pub fn property_read_array_vec<'fwnode, 'name, T: Integer>(
+        &'fwnode self,
+        name: &'name CStr,
+        len: usize,
+    ) -> Result<PropertyGuard<'fwnode, 'name, KVec<T>>> {
         let mut val: KVec<T> = KVec::with_capacity(len, GFP_KERNEL)?;
 
         // SAFETY: `name` is non-null and null-terminated. `self.as_raw` is valid
         // because `self` is valid. `val.as_ptr` is valid because `val` is valid.
-        to_result(unsafe {
+        let err = unsafe {
             bindings::fwnode_property_read_int_array(
                 self.as_raw(),
                 name.as_char_ptr(),
@@ -145,11 +159,19 @@ pub fn property_read_array_vec<T: Integer>(&self, name: &CStr, len: usize) -> Re
                 val.as_ptr() as *mut c_void,
                 len,
             )
-        })?;
-
-        // SAFETY: fwnode_property_read_int_array() writes exactly `len` entries on success
-        unsafe { val.set_len(len) }
-        Ok(val)
+        };
+        let res = if err < 0 {
+            Err(Error::from_errno(err))
+        } else {
+            // SAFETY: fwnode_property_read_int_array() writes exactly `len` entries on success
+            unsafe { val.set_len(len) }
+            Ok(val)
+        };
+        Ok(PropertyGuard {
+            inner: res,
+            fwnode: self,
+            name,
+        })
     }
 
     /// Returns integer array length for firmware property `name`
@@ -194,24 +216,42 @@ pub fn property_count_elem<T: Integer>(&self, name: &CStr) -> Result<usize> {
     /// # use crate::{device::Device, types::CString};
     /// fn examples(dev: &Device) -> Result {
     ///     let fwnode = dev.fwnode();
-    ///     let b: bool = fwnode.property_read("some-bool")?;
-    ///     let s: CString = fwnode.property_read("some-str")?;
+    ///     let b: bool = fwnode.property_read("some-bool").required()?;
+    ///     if let Some(s) = fwnode.property_read::<CString>("some-str").optional() {
+    ///         // ...
+    ///     }
     /// }
     /// ```
-    pub fn property_read<T: Property>(&self, name: &CStr) -> Result<T> {
-        T::read(&self, name)
+    pub fn property_read<'fwnode, 'name, T: Property>(
+        &'fwnode self,
+        name: &'name CStr,
+    ) -> PropertyGuard<'fwnode, 'name, T> {
+        PropertyGuard {
+            inner: T::read(&self, name),
+            fwnode: self,
+            name,
+        }
     }
 
     /// Returns first matching named child node handle.
-    pub fn get_child_by_name(&self, name: &CStr) -> Option<ARef<Self>> {
+    pub fn get_child_by_name<'fwnode, 'name>(
+        &'fwnode self,
+        name: &'name CStr,
+    ) -> PropertyGuard<'fwnode, 'name, ARef<Self>> {
         // SAFETY: `self` and `name` are valid.
         let child =
             unsafe { bindings::fwnode_get_named_child_node(self.as_raw(), name.as_char_ptr()) };
-        if child.is_null() {
-            return None;
+        let res = if child.is_null() {
+            Err(ENOENT)
+        } else {
+            // SAFETY: `fwnode_get_named_child_node` returns a pointer with refcount incremented.
+            Ok(unsafe { Self::from_raw(child) })
+        };
+        PropertyGuard {
+            inner: res,
+            fwnode: self,
+            name,
         }
-        // SAFETY: `fwnode_get_named_child_node` returns a pointer with refcount incremented.
-        Some(unsafe { Self::from_raw(child) })
     }
 
     /// Returns an iterator over a node's children.
@@ -365,3 +405,71 @@ pub enum NArgs<'a> {
     /// The known number of arguments.
     N(u32),
 }
+
+/// A helper for reading device properties.
+///
+/// Use [Self::required] if a missing property is considered a bug and
+/// [Self::optional] otherwise.
+///
+/// For convenience, [Self::or] and [Self::or_default] are provided.
+pub struct PropertyGuard<'fwnode, 'name, T> {
+    /// The result of reading the property.
+    inner: Result<T>,
+    /// The fwnode of the property, used for logging in the "required" case.
+    fwnode: &'fwnode FwNode,
+    /// The name of the property, used for logging in the "required" case.
+    name: &'name CStr,
+}
+
+impl<T> PropertyGuard<'_, '_, T> {
+    /// Access the property, indicating it is required.
+    ///
+    /// If the property is not present, the error is automatically logged. If a
+    /// missing property is not an error, use [Self::optional] instead.
+    pub fn required(self) -> Result<T> {
+        if self.inner.is_err() {
+            // Get the device associated with the fwnode for device-associated
+            // logging.
+            // TODO: Are we allowed to do this? The field `fwnode_handle.dev`
+            // has a somewhat vague comment, which could mean we're not
+            // supposed to access it:
+            // https://elixir.bootlin.com/linux/v6.13.6/source/include/linux/fwnode.h#L51
+            // SAFETY: According to the invariant of FwNode, it is valid.
+            let dev = unsafe { (*self.fwnode.as_raw()).dev };
+
+            if dev.is_null() {
+                pr_err!("property '{}' is missing\n", self.name);
+            } else {
+                // SAFETY: If dev is not null, it points to a valid device.
+                let dev: &Device = unsafe { &*dev.cast() };
+                dev_err!(dev, "property '{}' is missing\n", self.name);
+            };
+        }
+        self.inner
+    }
+
+    /// Access the property, indicating it is optional.
+    ///
+    /// In contrast to [Self::required], no error message is logged if the
+    /// property is not present.
+    pub fn optional(self) -> Option<T> {
+        self.inner.ok()
+    }
+
+    /// Access the property or the specified default value.
+    ///
+    /// Do not pass a sentinel value as default to detect a missing property.
+    /// Use [Self::required] or [Self::optional] instead.
+    pub fn or(self, default: T) -> T {
+        self.inner.unwrap_or(default)
+    }
+}
+
+impl<T: Default> PropertyGuard<'_, '_, T> {
+    /// Access the property or a default value.
+    ///
+    /// Use [Self::or] to specify a custom default value.
+    pub fn or_default(self) -> T {
+        self.inner.unwrap_or_default()
+    }
+}
Signature · 1 line
2.49.0
#46 lore raw

Re: [PATCH 09/10] rust: property: Add PropertyGuard

On Wed, Mar 26, 2025 at 12:13 PM Remo Senekowitsch Wrote:

rust/kernel/property.rs pub enum NArgs<'a> { · line 415

Quoted 7 lines
rust/kernel/property.rs +7 -0
+/// A helper for reading device properties.
+///
+/// Use [Self::required] if a missing property is considered a bug and
+/// [Self::optional] otherwise.
+///
+/// For convenience, [Self::or] and [Self::or_default] are provided.
+pub struct PropertyGuard<'fwnode, 'name, T> {

nit: when linking items in rustdoc comments you should suround them like [this]

Andrew

maintainer
#47 lore raw

Re: [PATCH 09/10] rust: property: Add PropertyGuard

In reply to Remo Senekowitsch
Quoted 3 lines · Remo Senekowitsch

Forcing users to specify whether a property is supposed to be required or not allows us to move error logging of missing required properties into core, preventing a lot of boilerplate in drivers.

We'll want to squash this one too.

Quoted 33 lines
---
 rust/kernel/property.rs | 146 ++++++++++++++++++++++++++++++++++------
 1 file changed, 127 insertions(+), 19 deletions(-)
rust/kernel/property.rs +9 -2
index f1d0a33ba..e4f0e5f97 100644
@@ -42,7 +42,11 @@ pub fn property_match_string(&self, name: &CStr, match_str: &CStr) -> Result<usi
     }
 
     /// Returns firmware property `name` integer array values in a KVec
-    pub fn property_read_array_vec<T: Integer>(&self, name: &CStr, len: usize) -> Result<KVec<T>> {
+    pub fn property_read_array_vec<'fwnode, 'name, T: Integer>(
+        &'fwnode self,
+        name: &'name CStr,
+        len: usize,
+    ) -> Result<PropertyGuard<'fwnode, 'name, KVec<T>>> {
         self.fwnode().property_read_array_vec(name, len)
     }
 
@@ -52,12 +56,18 @@ pub fn property_count_elem<T: Integer>(&self, name: &CStr) -> Result<usize> {
     }
 
     /// Returns firmware property `name` integer scalar value
-    pub fn property_read<T: Property>(&self, name: &CStr) -> Result<T> {
+    pub fn property_read<'fwnode, 'name, T: Property>(
+        &'fwnode self,
+        name: &'name CStr,
+    ) -> PropertyGuard<'fwnode, 'name, T> {
         self.fwnode().property_read(name)

rust/kernel/property.rs pub fn property_count_elem<T: Integer>(&self, name: &CStr) -> Result<usize> { · line 72

Quoted 10 lines
rust/kernel/property.rs +4 -1
     }
 
     /// Returns first matching named child node handle.
-    pub fn get_child_by_name(&self, name: &CStr) -> Option<ARef<FwNode>> {
+    pub fn get_child_by_name<'fwnode, 'name>(
+        &'fwnode self,
+        name: &'name CStr,
+    ) -> PropertyGuard<'fwnode, 'name, ARef<FwNode>> {
         self.fwnode().get_child_by_name(name)
     }

While child nodes can be required or optional, it's a bit less common to be optional. There error message on missing required child nodes is going to be a bit misleading saying 'property'. So maybe don't do the PropertyGuard on child node handling?

Quoted 125 lines
rust/kernel/property.rs +70 -16
@@ -132,12 +142,16 @@ pub fn property_match_string(&self, name: &CStr, match_str: &CStr) -> Result<usi
     }
 
     /// Returns firmware property `name` integer array values in a KVec
-    pub fn property_read_array_vec<T: Integer>(&self, name: &CStr, len: usize) -> Result<KVec<T>> {
+    pub fn property_read_array_vec<'fwnode, 'name, T: Integer>(
+        &'fwnode self,
+        name: &'name CStr,
+        len: usize,
+    ) -> Result<PropertyGuard<'fwnode, 'name, KVec<T>>> {
         let mut val: KVec<T> = KVec::with_capacity(len, GFP_KERNEL)?;
 
         // SAFETY: `name` is non-null and null-terminated. `self.as_raw` is valid
         // because `self` is valid. `val.as_ptr` is valid because `val` is valid.
-        to_result(unsafe {
+        let err = unsafe {
             bindings::fwnode_property_read_int_array(
                 self.as_raw(),
                 name.as_char_ptr(),
@@ -145,11 +159,19 @@ pub fn property_read_array_vec<T: Integer>(&self, name: &CStr, len: usize) -> Re
                 val.as_ptr() as *mut c_void,
                 len,
             )
-        })?;
-
-        // SAFETY: fwnode_property_read_int_array() writes exactly `len` entries on success
-        unsafe { val.set_len(len) }
-        Ok(val)
+        };
+        let res = if err < 0 {
+            Err(Error::from_errno(err))
+        } else {
+            // SAFETY: fwnode_property_read_int_array() writes exactly `len` entries on success
+            unsafe { val.set_len(len) }
+            Ok(val)
+        };
+        Ok(PropertyGuard {
+            inner: res,
+            fwnode: self,
+            name,
+        })
     }
 
     /// Returns integer array length for firmware property `name`
@@ -194,24 +216,42 @@ pub fn property_count_elem<T: Integer>(&self, name: &CStr) -> Result<usize> {
     /// # use crate::{device::Device, types::CString};
     /// fn examples(dev: &Device) -> Result {
     ///     let fwnode = dev.fwnode();
-    ///     let b: bool = fwnode.property_read("some-bool")?;
-    ///     let s: CString = fwnode.property_read("some-str")?;
+    ///     let b: bool = fwnode.property_read("some-bool").required()?;
+    ///     if let Some(s) = fwnode.property_read::<CString>("some-str").optional() {
+    ///         // ...
+    ///     }
     /// }
     /// ```
-    pub fn property_read<T: Property>(&self, name: &CStr) -> Result<T> {
-        T::read(&self, name)
+    pub fn property_read<'fwnode, 'name, T: Property>(
+        &'fwnode self,
+        name: &'name CStr,
+    ) -> PropertyGuard<'fwnode, 'name, T> {
+        PropertyGuard {
+            inner: T::read(&self, name),
+            fwnode: self,
+            name,
+        }
     }
 
     /// Returns first matching named child node handle.
-    pub fn get_child_by_name(&self, name: &CStr) -> Option<ARef<Self>> {
+    pub fn get_child_by_name<'fwnode, 'name>(
+        &'fwnode self,
+        name: &'name CStr,
+    ) -> PropertyGuard<'fwnode, 'name, ARef<Self>> {
         // SAFETY: `self` and `name` are valid.
         let child =
             unsafe { bindings::fwnode_get_named_child_node(self.as_raw(), name.as_char_ptr()) };
-        if child.is_null() {
-            return None;
+        let res = if child.is_null() {
+            Err(ENOENT)
+        } else {
+            // SAFETY: `fwnode_get_named_child_node` returns a pointer with refcount incremented.
+            Ok(unsafe { Self::from_raw(child) })
+        };
+        PropertyGuard {
+            inner: res,
+            fwnode: self,
+            name,
         }
-        // SAFETY: `fwnode_get_named_child_node` returns a pointer with refcount incremented.
-        Some(unsafe { Self::from_raw(child) })
     }
 
     /// Returns an iterator over a node's children.
@@ -365,3 +405,71 @@ pub enum NArgs<'a> {
     /// The known number of arguments.
     N(u32),
 }
+
+/// A helper for reading device properties.
+///
+/// Use [Self::required] if a missing property is considered a bug and
+/// [Self::optional] otherwise.
+///
+/// For convenience, [Self::or] and [Self::or_default] are provided.
+pub struct PropertyGuard<'fwnode, 'name, T> {
+    /// The result of reading the property.
+    inner: Result<T>,
+    /// The fwnode of the property, used for logging in the "required" case.
+    fwnode: &'fwnode FwNode,
+    /// The name of the property, used for logging in the "required" case.
+    name: &'name CStr,
+}
+
+impl<T> PropertyGuard<'_, '_, T> {
+    /// Access the property, indicating it is required.
+    ///
+    /// If the property is not present, the error is automatically logged. If a
+    /// missing property is not an error, use [Self::optional] instead.
+    pub fn required(self) -> Result<T> {
+        if self.inner.is_err() {
+            // Get the device associated with the fwnode for device-associated

rust/kernel/property.rs pub enum NArgs<'a> { · line 441

Quoted 10 lines
rust/kernel/property.rs +10 -0
+            // logging.
+            // TODO: Are we allowed to do this? The field `fwnode_handle.dev`
+            // has a somewhat vague comment, which could mean we're not
+            // supposed to access it:
+            // https://elixir.bootlin.com/linux/v6.13.6/source/include/linux/fwnode.h#L51
+            // SAFETY: According to the invariant of FwNode, it is valid.
+            let dev = unsafe { (*self.fwnode.as_raw()).dev };
+
+            if dev.is_null() {
+                pr_err!("property '{}' is missing\n", self.name);

We should print the node path in this case (and perhaps both cases).

We have "%pfw" printf format specifier for C. We're going to need the same for rust.

Quoted 37 lines
rust/kernel/property.rs +34 -0
+            } else {
+                // SAFETY: If dev is not null, it points to a valid device.
+                let dev: &Device = unsafe { &*dev.cast() };
+                dev_err!(dev, "property '{}' is missing\n", self.name);
+            };
+        }
+        self.inner
+    }
+
+    /// Access the property, indicating it is optional.
+    ///
+    /// In contrast to [Self::required], no error message is logged if the
+    /// property is not present.
+    pub fn optional(self) -> Option<T> {
+        self.inner.ok()
+    }
+
+    /// Access the property or the specified default value.
+    ///
+    /// Do not pass a sentinel value as default to detect a missing property.
+    /// Use [Self::required] or [Self::optional] instead.
+    pub fn or(self, default: T) -> T {
+        self.inner.unwrap_or(default)
+    }
+}
+
+impl<T: Default> PropertyGuard<'_, '_, T> {
+    /// Access the property or a default value.
+    ///
+    /// Use [Self::or] to specify a custom default value.
+    pub fn or_default(self) -> T {
+        self.inner.unwrap_or_default()
+    }
+}
Signature · 1 line
2.49.0
#48 lore raw

PATCH 10/10 samples: rust: platform: Add property read examples

In reply to Remo Senekowitsch

Add some example usage of the device property read methods for DT/ACPI/swnode properties.

---
 drivers/of/unittest-data/tests-platform.dtsi |  3 ++
 samples/rust/rust_driver_platform.rs         | 56 +++++++++++++++++++-
 2 files changed, 58 insertions(+), 1 deletion(-)
drivers/of/unittest-data/tests-platform.dtsi +3 -0
index 4171f43cf..50a51f38a 100644
@@ -37,6 +37,9 @@ dev@100 {
 			test-device@2 {
 				compatible = "test,rust-device";
 				reg = <0x2>;
+
+				test,u32-prop = <0xdeadbeef>;
+				test,i16-array = /bits/ 16 <1 2 (-3) (-4)>;
 			};
 		};
 
samples/rust/rust_driver_platform.rs +55 -1

68 lines not on this page · raw message

Signature · 1 line
2.49.0
maintainer
#49 lore raw

Re: [PATCH 10/10] samples: rust: platform: Add property read examples

Quoted 39 lines · Remo Senekowitsch

Add some example usage of the device property read methods for DT/ACPI/swnode properties.

---
 drivers/of/unittest-data/tests-platform.dtsi |  3 ++
 samples/rust/rust_driver_platform.rs         | 56 +++++++++++++++++++-
 2 files changed, 58 insertions(+), 1 deletion(-)
drivers/of/unittest-data/tests-platform.dtsi +3 -0
index 4171f43cf..50a51f38a 100644
@@ -37,6 +37,9 @@ dev@100 {
 			test-device@2 {
 				compatible = "test,rust-device";
 				reg = <0x2>;
+
+				test,u32-prop = <0xdeadbeef>;
+				test,i16-array = /bits/ 16 <1 2 (-3) (-4)>;
 			};
 		};
 
samples/rust/rust_driver_platform.rs +1 -1
index 8120609e2..ed25a3781 100644
@@ -2,7 +2,7 @@
 
 //! Rust Platform driver sample.
 
-use kernel::{c_str, of, platform, prelude::*};
+use kernel::{c_str, of, platform, prelude::*, str::CString};
 
 struct SampleDriver {
     pdev: platform::Device,

samples/rust/rust_driver_platform.rs fn probe(pdev: &mut platform::Device, info: Option<&Self::IdInfo>) -> Result<Pin · line 31

Quoted 4 lines
samples/rust/rust_driver_platform.rs +1 -0
@@ -28,6 +28,60 @@ fn probe(pdev: &mut platform::Device, info: Option<&Self::IdInfo>) -> Result<Pin
             dev_info!(pdev.as_ref(), "Probed with info: '{}'.\n", info.0);
         }
 
+        let dev = pdev.as_ref();

We should move this to the top and replace all the 'pdev.as_ref()' with 'dev'.

samples/rust/rust_driver_platform.rs fn probe(pdev: &mut platform::Device, info: Option<&Self::IdInfo>) -> Result<Pin · line 34

Quoted 3 lines
samples/rust/rust_driver_platform.rs +3 -0
+        if let Ok(idx) = dev.property_match_string(c_str!("compatible"), c_str!("test,rust-device"))
+        {
+            dev_info!(pdev.as_ref(), "matched compatible string idx = {}\n", idx);

Like here. (Looks like this is my fault.)

samples/rust/rust_driver_platform.rs fn probe(pdev: &mut platform::Device, info: Option<&Self::IdInfo>) -> Result<Pin · line 46

Quoted 12 lines
samples/rust/rust_driver_platform.rs +12 -0
+        }
+
+        if let Ok(str) = dev
+            .property_read::<CString>(c_str!("compatible"))
+            .required()
+        {
+            dev_info!(pdev.as_ref(), "compatible string = {:?}\n", str);
+        }
+
+        let prop = dev
+            .property_read::<bool>(c_str!("test,bool-prop"))
+            .required()?;

The 'required' is kind of odd for boolean properties. They are never required as not present is the only way to to get false.

Quoted 44 lines
samples/rust/rust_driver_platform.rs +38 -0
+        dev_info!(dev, "bool prop is {}\n", prop);
+
+        if dev.property_present(c_str!("test,u32-prop")) {
+            dev_info!(dev, "'test,u32-prop' is present\n");
+        }
+
+        let prop = dev
+            .property_read::<u32>(c_str!("test,u32-optional-prop"))
+            .or(0x12);
+        dev_info!(
+            dev,
+            "'test,u32-optional-prop' is {:#x} (default = {:#x})\n",
+            prop,
+            0x12
+        );
+
+        // Missing property without a default will print an error
+        let _ = dev
+            .property_read::<u32>(c_str!("test,u32-required-prop"))
+            .required()?;
+
+        let prop: u32 = dev.property_read(c_str!("test,u32-prop")).required()?;
+        dev_info!(dev, "'test,u32-prop' is {:#x}\n", prop);
+
+        let prop: [i16; 4] = dev.property_read(c_str!("test,i16-array")).required()?;
+        dev_info!(dev, "'test,i16-array' is {:?}\n", prop);
+        dev_info!(
+            dev,
+            "'test,i16-array' length is {}\n",
+            dev.property_count_elem::<u16>(c_str!("test,i16-array"))
+                .unwrap()
+        );
+
+        let prop: KVec<i16> = dev
+            .property_read_array_vec(c_str!("test,i16-array"), 4)?
+            .required()?;
+        dev_info!(dev, "'test,i16-array' is KVec {:?}\n", prop);
+
         let drvdata = KBox::new(Self { pdev: pdev.clone() }, GFP_KERNEL)?;
 
         Ok(drvdata.into())
Signature · 1 line
2.49.0
#50 lore raw

Re: [PATCH 10/10] samples: rust: platform: Add property read examples

Quoted 7 lines · Rob Herring

samples/rust/rust_driver_platform.rs fn probe(pdev: &mut platform::Device, info: Option<&Self::IdInfo>) -> Result<Pin · line 46

Quoted 4 lines
samples/rust/rust_driver_platform.rs +3 -0
+        let prop = dev
+            .property_read::<bool>(c_str!("test,bool-prop"))
+            .required()?;

The 'required' is kind of odd for boolean properties. They are never required as not present is the only way to to get false.

Agreed. I can think of a few alternatives:

* Make the trait Property more flexible to allow each implementor to specify what its output type for the read function is, via an associated type. I really don't like this idea, because overly generic APIs can mess with type inference and become less ergonomic because of it.

* Use propert_present instead. That doesn't perfectly express the intention, because it doesn't warn if the property is present but has a type other than bool.

* Add an additional inherent method property_read_bool, which returns a plain bool instead of PropertyGuard<bool>. Then there will be three slightly different ways to read a bool: property_present, property_read_bool and property_read::<bool>. Maybe that's confusing.

* Add property_read_bool and remove impl Property for bool. That would avoid confusion between property_read_bool and property_read::<bool>, only the former would work.

maintainer
#51 lore raw

Re: [PATCH 10/10] samples: rust: platform: Add property read examples

Quoted 12 lines · Remo Senekowitsch
Quoted 7 lines · Rob Herring

samples/rust/rust_driver_platform.rs fn probe(pdev: &mut platform::Device, info: Option<&Self::IdInfo>) -> Result<Pin · line 46

Quoted 4 lines
samples/rust/rust_driver_platform.rs +3 -0
+        let prop = dev
+            .property_read::<bool>(c_str!("test,bool-prop"))
+            .required()?;

The 'required' is kind of odd for boolean properties. They are never required as not present is the only way to to get false.

Agreed. I can think of a few alternatives:

Quoted 8 lines

* Make the trait Property more flexible to allow each implementor to specify what its output type for the read function is, via an associated type. I really don't like this idea, because overly generic APIs can mess with type inference and become less ergonomic because of it.

* Use propert_present instead. That doesn't perfectly express the intention, because it doesn't warn if the property is present but has a type other than bool.

Right. I've been cleaning up the tree to use of_property_read_bool() on bools and of_property_present() on non-bools, so don't want to go back to 1 function. The C code now warns on a mismatch.

Quoted 8 lines

* Add an additional inherent method property_read_bool, which returns a plain bool instead of PropertyGuard<bool>. Then there will be three slightly different ways to read a bool: property_present, property_read_bool and property_read::<bool>. Maybe that's confusing.

* Add property_read_bool and remove impl Property for bool. That would avoid confusion between property_read_bool and property_read::<bool>, only the former would work.

I think I would go with this option. Easier to add another way later than remove one.

Rob

maintainer
#52 lore raw

Re: [PATCH 10/10] samples: rust: platform: Add property read examples

In reply to Remo Senekowitsch
Quoted 36 lines · Remo Senekowitsch
samples/rust/rust_driver_platform.rs +22 -1
@@ -2,7 +2,7 @@
 
 //! Rust Platform driver sample.
 
-use kernel::{c_str, of, platform, prelude::*};
+use kernel::{c_str, of, platform, prelude::*, str::CString};
 
 struct SampleDriver {
     pdev: platform::Device,
@@ -28,6 +28,60 @@ fn probe(pdev: &mut platform::Device, info: Option<&Self::IdInfo>) -> Result<Pin
             dev_info!(pdev.as_ref(), "Probed with info: '{}'.\n", info.0);
         }
 
+        let dev = pdev.as_ref();
+        if let Ok(idx) = dev.property_match_string(c_str!("compatible"), c_str!("test,rust-device"))
+        {
+            dev_info!(pdev.as_ref(), "matched compatible string idx = {}\n", idx);
+        }
+
+        if let Ok(str) = dev
+            .property_read::<CString>(c_str!("compatible"))
+            .required()
+        {
+            dev_info!(pdev.as_ref(), "compatible string = {:?}\n", str);
+        }
+
+        let prop = dev
+            .property_read::<bool>(c_str!("test,bool-prop"))
+            .required()?;
+        dev_info!(dev, "bool prop is {}\n", prop);
+
+        if dev.property_present(c_str!("test,u32-prop")) {
+            dev_info!(dev, "'test,u32-prop' is present\n");
+        }

samples/rust/rust_driver_platform.rs fn probe(pdev: &mut platform::Device, info: Option<&Self::IdInfo>) -> Result<Pin · line 61

Quoted 10 lines
samples/rust/rust_driver_platform.rs +10 -0
+
+        let prop = dev
+            .property_read::<u32>(c_str!("test,u32-optional-prop"))
+            .or(0x12);
+        dev_info!(
+            dev,
+            "'test,u32-optional-prop' is {:#x} (default = {:#x})\n",
+            prop,
+            0x12
+        );

Printing the default value looks more like a test, rather than a sample.

Quoted 6 lines
samples/rust/rust_driver_platform.rs +6 -0
+
+        // Missing property without a default will print an error
+        let _ = dev
+            .property_read::<u32>(c_str!("test,u32-required-prop"))
+            .required()?;
+

samples/rust/rust_driver_platform.rs fn probe(pdev: &mut platform::Device, info: Option<&Self::IdInfo>) -> Result<Pin · line 77

Quoted 10 lines
samples/rust/rust_driver_platform.rs +10 -0
+        let prop: u32 = dev.property_read(c_str!("test,u32-prop")).required()?;
+        dev_info!(dev, "'test,u32-prop' is {:#x}\n", prop);
+
+        let prop: [i16; 4] = dev.property_read(c_str!("test,i16-array")).required()?;
+        dev_info!(dev, "'test,i16-array' is {:?}\n", prop);
+        dev_info!(
+            dev,
+            "'test,i16-array' length is {}\n",
+            dev.property_count_elem::<u16>(c_str!("test,i16-array"))
+                .unwrap()

Please no unwrap() in the kernel, it may panic.

samples/rust/rust_driver_platform.rs fn probe(pdev: &mut platform::Device, info: Option<&Self::IdInfo>) -> Result<Pin · line 84

Quoted 7 lines
samples/rust/rust_driver_platform.rs +7 -0
+        );
+
+        let prop: KVec<i16> = dev
+            .property_read_array_vec(c_str!("test,i16-array"), 4)?
+            .required()?;
+        dev_info!(dev, "'test,i16-array' is KVec {:?}\n", prop);
+

Please move this code to a new function, e.g.

	impl SampleDriver {
	   fn properties_parse(dev: &device::Device)) -> Result;
	}

in order to keep probe() as clean as possible. If we put too much stuff in there, it might become too confusing to serve as a simple example showing how to implement a platform driver in Rust.

Besides that, are we sure we really want to print everything?

#53 lore raw
In reply to Remo Senekowitsch

Hi thanks for sending these in.

On Wed, Mar 26, 2025 at 12:13 PM Remo Senekowitsch Wrote:

Quoted 2 lines

This is my first time posting to the mailing list, please let me know if I did anything wrong.

you probably will have to resubmit in the patch canonical format[1]

I would recomend reading the docs but in summary:
- all of your patches should have the subsystem in the subject line.
- patch 6 is missing a commit message
- commit messages should be written in the imperitive tone
- a couple of your commit messages give a reason for the changes but do not have a summary of the changes
- for your v2 you should add a diffstat to your cover letter running git format-patch --base=auto --cover-letter does this automatically for you

Andrew

[1] https://www.kernel.org/doc/html/latest/process/submitting-patches.html#the-canonical-patch-format

#54 lore raw
Quoted 15 lines · Andrew Ballance

Hi thanks for sending these in.

On Wed, Mar 26, 2025 at 12:13 PM Remo Senekowitsch Wrote:

Quoted 2 lines

This is my first time posting to the mailing list, please let me know if I did anything wrong.

you probably will have to resubmit in the patch canonical format[1]

I would recomend reading the docs but in summary:
- all of your patches should have the subsystem in the subject line.
- patch 6 is missing a commit message
- commit messages should be written in the imperitive tone
- a couple of your commit messages give a reason for the changes but do not have a summary of the changes

Quoted 3 lines

- for your v2 you should add a diffstat to your cover letter running git format-patch --base=auto --cover-letter does this automatically for you

Also -v<X> gives the proper versioning.

Quoted 1 line

[1] https://www.kernel.org/doc/html/latest/process/submitting-patches.html#the-canonical-patch-format

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#55 lore raw

v2 cover More Rust bindings for device property reads

In reply to Remo Senekowitsch

This adds more Rust bindings for reading device properties, based on Rob Herring's work. I'm working on a driver[1] that uses these, but the driver has more dependencies than this.

Rob Herring and Dirk Behme did several review iterations over on Zulip already[1], I'm thankful for their great input.

This is my second-ever patch series. I read through the documentation for sending patches[2] carefully, trying to apply everything to the best of my understanding. Please point out any specifics if I got something wrong.

Best regards,
Remo

changes in v2:
- Add property.rs to maintainers file
- Add and improve lots of safety comments
- Use __dev_fwnode istead of dev_fwnode to express the intent better
- Make get_child_by_name not return a PropertyGuard
- Squash the patch with the generic read method
- Squash the PropertyGuard patch
- Remove arrayvec, abstract over fwnode_reference_args instead
- Log the full fwnode path in case of a missing property
- Remove integer trait and export of fwnode_property_read_int_array These are not needed now that a macro is used to associate each integer type with its corresponding read_*_array function.
- Replace impl Property for bool with a standalone function property_read_bool with an infallible function signature
- Extract parsing of properties into separate function in sample platform driver

Remo Senekowitsch (5):
rust: Move property_present to separate file
rust: Add bindings for reading device properties
rust: property: Add child accessor and iterator
rust: property: Add property_get_reference_args
samples: rust: platform: Add property read examples

 MAINTAINERS                                  |   1 +
 drivers/of/unittest-data/tests-platform.dtsi |   3 +
 rust/helpers/helpers.c                       |   1 +
 rust/helpers/property.c                      |   8 +
 rust/kernel/device.rs                        |   7 -
 rust/kernel/lib.rs                           |   1 +
 rust/kernel/property.rs                      | 608 +++++++++++++++++++
 samples/rust/rust_driver_platform.rs         |  69 ++-
 8 files changed, 688 insertions(+), 10 deletions(-)
 create mode 100644 rust/helpers/property.c
 create mode 100644 rust/kernel/property.rs
Signature · 1 line
2.49.0
#56 lore raw

PATCH v2 1/5 rust: Move property_present to separate file

Not all property-related APIs can be exposed directly on a device. For example, iterating over child nodes of a device will yield fwnode_handle. Thus, in order to access properties on these child nodes, duplicate the APIs on a fwnode as they are in C.

---
 MAINTAINERS             |  1 +
 rust/helpers/helpers.c  |  1 +
 rust/helpers/property.c |  8 +++++
 rust/kernel/device.rs   |  7 ----
 rust/kernel/lib.rs      |  1 +
 rust/kernel/property.rs | 75 +++++++++++++++++++++++++++++++++++++++++
 6 files changed, 86 insertions(+), 7 deletions(-)
 create mode 100644 rust/helpers/property.c
 create mode 100644 rust/kernel/property.rs
MAINTAINERS +1 -0
index c8d9e8187..a34471662 100644
@@ -7117,6 +7117,7 @@ F:	rust/kernel/device_id.rs
 F:	rust/kernel/devres.rs
 F:	rust/kernel/driver.rs
 F:	rust/kernel/platform.rs
+F:	rust/kernel/property.rs
 F:	samples/rust/rust_driver_platform.rs
 
 DRIVERS FOR OMAP ADAPTIVE VOLTAGE SCALING (AVS)
rust/helpers/helpers.c +1 -0
index 0640b7e11..b4eec5bf2 100644
@@ -23,6 +23,7 @@
 #include "platform.c"
 #include "pci.c"
 #include "pid_namespace.c"
+#include "property.c"
 #include "rbtree.c"
 #include "rcu.c"
 #include "refcount.c"
rust/helpers/property.c new file +8 -0
new file mode 100644
index 000000000..08f68e2da
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/property.h>
+
+void rust_helper_fwnode_handle_put(struct fwnode_handle *fwnode)
+{
+	fwnode_handle_put(fwnode);
+}
rust/kernel/device.rs +0 -7
index db2d9658b..d5e6a19ff 100644
@@ -6,7 +6,6 @@
 
 use crate::{
     bindings,
-    str::CStr,
     types::{ARef, Opaque},
 };
 use core::{fmt, ptr};
@@ -181,12 +180,6 @@ unsafe fn printk(&self, klevel: &[u8], msg: fmt::Arguments<'_>) {
             )
         };
     }
-
-    /// 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.
-        unsafe { bindings::device_property_present(self.as_raw().cast_const(), name.as_char_ptr()) }
-    }
 }
 
 // SAFETY: Instances of `Device` are always reference-counted.
rust/kernel/lib.rs +1 -0
index 496ed32b0..ca233fd20 100644
@@ -67,6 +67,7 @@
 pub mod platform;
 pub mod prelude;
 pub mod print;
+pub mod property;
 pub mod rbtree;
 pub mod revocable;
 pub mod security;
rust/kernel/property.rs new file +75 -0

75 lines not on this page · raw message

Signature · 1 line
2.49.0
maintainer
#57 lore raw

Re: [PATCH v2 1/5] rust: Move property_present to separate file

Quoted 4 lines · Remo Senekowitsch

Not all property-related APIs can be exposed directly on a device. For example, iterating over child nodes of a device will yield fwnode_handle. Thus, in order to access properties on these child nodes, duplicate the APIs on a fwnode as they are in C.

What do you mean with "duplicate the APIs"?

Also, this patch does three separate things.

(1) move property_present() to separate file
(2) implement FwNode abstraction
(3) implement Device::fwnode()

I'd rather keep property_present() in device.rs and also move fwnode() into device.rs, even though in C it's in property.c. impl Device blocks should be in device.rs.

Hence, please split this in two patches, one for (2) and one for (3).

Quoted 9 lines

+++ b/rust/kernel/property.rs

rust/kernel/property.rs new file +7 -0
@@ -0,0 +1,75 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Unified device property interface.
+//!
+//! C header: [`include/linux/property.h`](srctree/include/linux/property.h)
+
+use core::ptr;

rust/kernel/property.rs line 17

Quoted 10 lines
rust/kernel/property.rs new file +10 -0
+
+use crate::{bindings, device::Device, str::CStr, types::Opaque};
+
+impl Device {
+    /// Obtain the fwnode corresponding to the device.
+    fn fwnode(&self) -> &FwNode {
+        // SAFETY: `self` is valid.
+        let fwnode_handle = unsafe { bindings::__dev_fwnode(self.as_raw()) };
+        if fwnode_handle.is_null() {
+            panic!("fwnode_handle cannot be null");

Please don't use panic!() unless you hit an error condition that is impossible to handle.

If __dev_fwnode() can ever return NULL, make this function return Option<&FwNode> instead.

#58 lore raw

Re: [PATCH v2 1/5] rust: Move property_present to separate file

Quoted 7 lines · Danilo Krummrich
Quoted 4 lines · Remo Senekowitsch

Not all property-related APIs can be exposed directly on a device. For example, iterating over child nodes of a device will yield fwnode_handle. Thus, in order to access properties on these child nodes, duplicate the APIs on a fwnode as they are in C.

What do you mean with "duplicate the APIs"?

The property related methods on Device and FwNode are mostly the same. Same names, same function signatures. The methods on Device simply call self.fwnode() and then the same method on FwNode. I was asked to do this so driver authors don't have to write dev.fwnode().read_propert() etc. every time they want to do something on the device itself. This is the case here for property_present as well as the methods added in the following patches.

Quoted 9 lines

Also, this patch does three separate things.

(1) move property_present() to separate file
(2) implement FwNode abstraction
(3) implement Device::fwnode()

I'd rather keep property_present() in device.rs and also move fwnode() into device.rs, even though in C it's in property.c. impl Device blocks should be in device.rs.

I was asked to move it by Rob Herring on Zulip[1]. Dirk Behme also agreed with it[2]. I have no strong opinion on this, hopefully you can come to an agreement. Whatever it is, I'll happily implement it.

Quoted 23 lines
Quoted 9 lines

+++ b/rust/kernel/property.rs

rust/kernel/property.rs new file +7 -0
@@ -0,0 +1,75 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Unified device property interface.
+//!
+//! C header: [`include/linux/property.h`](srctree/include/linux/property.h)
+
+use core::ptr;

rust/kernel/property.rs line 17

Quoted 10 lines
rust/kernel/property.rs new file +10 -0
+
+use crate::{bindings, device::Device, str::CStr, types::Opaque};
+
+impl Device {
+    /// Obtain the fwnode corresponding to the device.
+    fn fwnode(&self) -> &FwNode {
+        // SAFETY: `self` is valid.
+        let fwnode_handle = unsafe { bindings::__dev_fwnode(self.as_raw()) };
+        if fwnode_handle.is_null() {
+            panic!("fwnode_handle cannot be null");

Please don't use panic!() unless you hit an error condition that is impossible to handle.

Quoted 2 lines

If __dev_fwnode() can ever return NULL, make this function return Option<&FwNode> instead.

Rob Herring mentioned this in the previous thread already:

Quoted 3 lines

Users/drivers testing fwnode_handle/of_node for NULL is pretty common. Though often that's a legacy code path, so maybe not allowing NULL is fine for now.

I asked a follow-up question about how to proceed but got no reply[3].

Best regards,
Remo

maintainer
#59 lore raw

Re: [PATCH v2 1/5] rust: Move property_present to separate file

Quoted 9 lines · Remo Senekowitsch
Quoted 7 lines · Danilo Krummrich
Quoted 4 lines · Remo Senekowitsch

Not all property-related APIs can be exposed directly on a device. For example, iterating over child nodes of a device will yield fwnode_handle. Thus, in order to access properties on these child nodes, duplicate the APIs on a fwnode as they are in C.

What do you mean with "duplicate the APIs"?

Quoted 7 lines

The property related methods on Device and FwNode are mostly the same. Same names, same function signatures. The methods on Device simply call self.fwnode() and then the same method on FwNode. I was asked to do this so driver authors don't have to write dev.fwnode().read_propert() etc. every time they want to do something on the device itself. This is the case here for property_present as well as the methods added in the following patches.

Yeah, I noticed when I continued with patch 2 of this series.

Quoted 6 lines
Quoted 6 lines

Also, this patch does three separate things.

(1) move property_present() to separate file
(2) implement FwNode abstraction
(3) implement Device::fwnode()

Quoted 7 lines
Quoted 3 lines

I'd rather keep property_present() in device.rs and also move fwnode() into device.rs, even though in C it's in property.c. impl Device blocks should be in device.rs.

I was asked to move it by Rob Herring on Zulip[1]. Dirk Behme also agreed with it[2]. I have no strong opinion on this, hopefully you can come to an agreement. Whatever it is, I'll happily implement it.

Please also see my comment in patch 2. If we think device.rs becomes too messy with that, we should convert device.rs to its own module, i.e. rust/kernel/device/mod.rs and then have rust/kernel/device/property.rs instead.

Quoted 26 lines
Quoted 23 lines
Quoted 9 lines

+++ b/rust/kernel/property.rs

rust/kernel/property.rs new file +7 -0
@@ -0,0 +1,75 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Unified device property interface.
+//!
+//! C header: [`include/linux/property.h`](srctree/include/linux/property.h)
+
+use core::ptr;

rust/kernel/property.rs line 17

Quoted 10 lines
rust/kernel/property.rs new file +10 -0
+
+use crate::{bindings, device::Device, str::CStr, types::Opaque};
+
+impl Device {
+    /// Obtain the fwnode corresponding to the device.
+    fn fwnode(&self) -> &FwNode {
+        // SAFETY: `self` is valid.
+        let fwnode_handle = unsafe { bindings::__dev_fwnode(self.as_raw()) };
+        if fwnode_handle.is_null() {
+            panic!("fwnode_handle cannot be null");

Please don't use panic!() unless you hit an error condition that is impossible to handle.

Quoted 2 lines

If __dev_fwnode() can ever return NULL, make this function return Option<&FwNode> instead.

Quoted 7 lines

Rob Herring mentioned this in the previous thread already:

Quoted 3 lines

Users/drivers testing fwnode_handle/of_node for NULL is pretty common. Though often that's a legacy code path, so maybe not allowing NULL is fine for now.

I asked a follow-up question about how to proceed but got no reply[3].

I NULL is a possible return value of __dev_fwnode() (which seems to be the case), there's no other option than makeing fwnode() fallible.

Quoted 7 lines

Best regards,
Remo

#60 lore raw

Re: [PATCH v2 1/5] rust: Move property_present to separate file

Quoted 19 lines · Danilo Krummrich
Quoted 18 lines · Remo Senekowitsch
Quoted 9 lines · Danilo Krummrich

rust/kernel/property.rs line 17

Quoted 7 lines · Remo Senekowitsch
rust/kernel/property.rs new file +7 -0
+impl Device {
+    /// Obtain the fwnode corresponding to the device.
+    fn fwnode(&self) -> &FwNode {
+        // SAFETY: `self` is valid.
+        let fwnode_handle = unsafe { bindings::__dev_fwnode(self.as_raw()) };
+        if fwnode_handle.is_null() {
+            panic!("fwnode_handle cannot be null");
Quoted 5 lines

Please don't use panic!() unless you hit an error condition that is impossible to handle.

If __dev_fwnode() can ever return NULL, make this function return Option<&FwNode> instead.

Rob Herring mentioned this in the previous thread already:

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Users/drivers testing fwnode_handle/of_node for NULL is pretty common. Though often that's a legacy code path, so maybe not allowing NULL is fine for now.

I asked a follow-up question about how to proceed but got no reply[3].

I NULL is a possible return value of __dev_fwnode() (which seems to be the case), there's no other option than makeing fwnode() fallible.

That makes sense. In that case, I think we shouldn't duplicate the methods on Device and FwNode, because their signatures would be different. E.g. Device::property_present would have to return Option<bool> while FwNode::property_present would return bool. I think it would be cleaner to force driver authors to do something like if let Some(fwnode) = dev.fwnode() { /* read properties */ }.

#61 lore raw

PATCH v2 2/5 rust: Add bindings for reading device properties

In reply to Remo Senekowitsch

The device property API is a firmware agnostic API for reading properties from firmware (DT/ACPI) devices nodes and swnodes.

While the C API takes a pointer to a caller allocated variable/buffer, the rust API is designed to return a value and can be used in struct initialization. Rust generics are also utilized to support different types of properties where appropriate.

The PropertyGuard is a way to force users to specify whether a property is supposed to be required or not. This allows us to move error logging of missing required properties into core, preventing a lot of boilerplate in drivers.

---
 rust/kernel/property.rs | 385 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 383 insertions(+), 2 deletions(-)
rust/kernel/property.rs +383 -2
index f6e6c980d..0d4ea3168 100644
@@ -4,9 +4,17 @@
 //!
 //! C header: [`include/linux/property.h`](srctree/include/linux/property.h)
 
-use core::ptr;
+use core::{mem::MaybeUninit, ptr};
 
-use crate::{bindings, device::Device, str::CStr, types::Opaque};
+use crate::{
+    alloc::KVec,
+    bindings, c_str,
+    device::Device,
+    error::{to_result, Result},
+    prelude::*,
+    str::{BStr, CStr, CString},
+    types::Opaque,
+};
 
 impl Device {
     /// Obtain the fwnode corresponding to the device.
@@ -28,6 +36,38 @@ fn fwnode(&self) -> &FwNode {
     pub fn property_present(&self, name: &CStr) -> bool {
         self.fwnode().property_present(name)
     }
+
+    /// Returns firmware property `name` boolean value
+    pub fn property_read_bool(&self, name: &CStr) -> bool {
+        self.fwnode().property_read_bool(name)
+    }
+
+    /// Returns the index of matching string `match_str` for firmware string property `name`
+    pub fn property_match_string(&self, name: &CStr, match_str: &CStr) -> Result<usize> {
+        self.fwnode().property_match_string(name, match_str)
+    }
+
+    /// Returns firmware property `name` integer array values in a KVec
+    pub fn property_read_array_vec<'fwnode, 'name, T: PropertyInt>(
+        &'fwnode self,
+        name: &'name CStr,
+        len: usize,
+    ) -> Result<PropertyGuard<'fwnode, 'name, KVec<T>>> {
+        self.fwnode().property_read_array_vec(name, len)
+    }
+
+    /// Returns integer array length for firmware property `name`
+    pub fn property_count_elem<T: PropertyInt>(&self, name: &CStr) -> Result<usize> {
+        self.fwnode().property_count_elem::<T>(name)
+    }
+
+    /// Returns firmware property `name` integer scalar value
+    pub fn property_read<'fwnode, 'name, T: Property>(
+        &'fwnode self,
+        name: &'name CStr,
+    ) -> PropertyGuard<'fwnode, 'name, T> {
+        self.fwnode().property_read(name)
+    }
 }
 
 /// A reference-counted fwnode_handle.
@@ -59,6 +99,150 @@ pub fn property_present(&self, name: &CStr) -> bool {
         // SAFETY: By the invariant of `CStr`, `name` is null-terminated.
         unsafe { bindings::fwnode_property_present(self.as_raw().cast_const(), name.as_char_ptr()) }
     }
+
+    /// Returns firmware property `name` boolean value
+    pub fn property_read_bool(&self, name: &CStr) -> bool {
+        // SAFETY: `name` is non-null and null-terminated. `self.as_raw()` is valid
+        // because `self` is valid.
+        unsafe { bindings::fwnode_property_read_bool(self.as_raw(), name.as_char_ptr()) }
+    }
+
+    /// Returns the index of matching string `match_str` for firmware string property `name`
+    pub fn property_match_string(&self, name: &CStr, match_str: &CStr) -> Result<usize> {
+        // SAFETY: `name` and `match_str` are non-null and null-terminated. `self.as_raw` is
+        // valid because `self` is valid.
+        let ret = unsafe {
+            bindings::fwnode_property_match_string(
+                self.as_raw(),
+                name.as_char_ptr(),
+                match_str.as_char_ptr(),
+            )
+        };
+        to_result(ret)?;
+        Ok(ret as usize)
+    }
+
+    /// Returns firmware property `name` integer array values in a KVec
+    pub fn property_read_array_vec<'fwnode, 'name, T: PropertyInt>(
+        &'fwnode self,
+        name: &'name CStr,
+        len: usize,
+    ) -> Result<PropertyGuard<'fwnode, 'name, KVec<T>>> {
+        let mut val: KVec<T> = KVec::with_capacity(len, GFP_KERNEL)?;
+
+        // SAFETY: `val.as_mut_ptr()` is valid because `KVec::with_capacity`
+        // didn't return an error and it has at least space for `len` number
+        // of elements.
+        let err = unsafe { read_array_out_param::<T>(self, name, val.as_mut_ptr(), len) };
+        let res = if err < 0 {
+            Err(Error::from_errno(err))
+        } else {
+            // SAFETY: fwnode_property_read_int_array() writes exactly `len` entries on success
+            unsafe { val.set_len(len) }
+            Ok(val)
+        };
+        Ok(PropertyGuard {
+            inner: res,
+            fwnode: self,
+            name,
+        })
+    }
+
+    /// Returns integer array length for firmware property `name`
+    pub fn property_count_elem<T: PropertyInt>(&self, name: &CStr) -> Result<usize> {
+        // SAFETY: `out_param` is allowed to be null because `len` is zero.
+        let ret = unsafe { read_array_out_param::<T>(self, name, ptr::null_mut(), 0) };
+        to_result(ret)?;
+        Ok(ret as usize)
+    }
+
+    /// Returns the value of firmware property `name`.
+    ///
+    /// This method is generic over the type of value to read. Informally,
+    /// the types that can be read are booleans, strings, unsigned integers and
+    /// arrays of unsigned integers.
+    ///
+    /// Reading a `KVec` of integers is done with the separate
+    /// method [`Self::property_read_array_vec`], because it takes an
+    /// additional `len` argument.
+    ///
+    /// When reading a boolean, this method never fails. A missing property
+    /// is interpreted as `false`, whereas a present property is interpreted
+    /// as `true`.
+    ///
+    /// For more precise documentation about what types can be read, see
+    /// the [implementors of Property][Property#implementors] and [its
+    /// implementations on foreign types][Property#foreign-impls].
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// # use crate::{device::Device, types::CString};
+    /// fn examples(dev: &Device) -> Result {
+    ///     let fwnode = dev.fwnode();
+    ///     let b: bool = fwnode.property_read("some-bool").required()?;
+    ///     if let Some(s) = fwnode.property_read::<CString>("some-str").optional() {
+    ///         // ...
+    ///     }
+    /// }
+    /// ```
+    pub fn property_read<'fwnode, 'name, T: Property>(
+        &'fwnode self,
+        name: &'name CStr,
+    ) -> PropertyGuard<'fwnode, 'name, T> {
+        PropertyGuard {
+            inner: T::read(self, name),
+            fwnode: self,
+            name,
+        }
+    }
+
+    /// helper used to display name or path of a fwnode
+    ///
+    /// # Safety
+    ///
+    /// Callers must provide a valid format string for a fwnode.
+    unsafe fn fmt(&self, f: &mut core::fmt::Formatter<'_>, fmt_str: &CStr) -> core::fmt::Result {
+        let mut buf = [0; 256];
+        // SAFETY: `buf` is valid and `buf.len()` is its length. `self.as_raw()` is
+        // valid because `self` is valid.
+        let written = unsafe {
+            bindings::scnprintf(buf.as_mut_ptr(), buf.len(), fmt_str.as_ptr(), self.as_raw())
+        };
+        // SAFETY: `written` is smaller or equal to `buf.len()`.
+        let b: &[u8] = unsafe { core::slice::from_raw_parts(buf.as_ptr(), written as usize) };
+        write!(f, "{}", BStr::from_bytes(b))
+    }
+
+    /// 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 + use<'_> {
+        struct FwNodeDisplayName<'a>(&'a FwNode);
+
+        impl core::fmt::Display for FwNodeDisplayName<'_> {
+            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+                // SAFETY: "%pfwP" is a valid format string for fwnode
+                unsafe { self.0.fmt(f, c_str!("%pfwP")) }
+            }
+        }
+
+        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 + use<'_> {
+        struct FwNodeDisplayPath<'a>(&'a FwNode);
+
+        impl core::fmt::Display for FwNodeDisplayPath<'_> {
+            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+                // SAFETY: "%pfwf" is a valid format string for fwnode
+                unsafe { self.0.fmt(f, c_str!("%pfwf")) }
+            }
+        }
+
+        FwNodeDisplayPath(self)
+    }
 }
 
 // SAFETY: Instances of `FwNode` are always reference-counted.
@@ -73,3 +257,200 @@ unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
         unsafe { bindings::fwnode_handle_put(obj.cast().as_ptr()) }
     }
 }
+
+/// Implemented for several types that can be read as properties.
+///
+/// Informally, this is implemented for strings, integers and arrays of
+/// integers. It's used to make [`FwNode::property_read`] generic over the
+/// type of property being read. There are also two dedicated methods to read
+/// other types, because they require more specialized function signatures:
+/// - [`property_read_bool`](Device::property_read_bool)
+/// - [`property_read_array_vec`](Device::property_read_array_vec)
+pub trait Property: Sized {
+    /// Used to make [`FwNode::property_read`] generic.
+    fn read(fwnode: &FwNode, name: &CStr) -> Result<Self>;
+}
+
+impl Property for CString {
+    fn read(fwnode: &FwNode, name: &CStr) -> Result<Self> {
+        let mut str: *mut u8 = ptr::null_mut();
+        let pstr: *mut _ = &mut str;
+
+        // SAFETY: `name` is non-null and null-terminated. `fwnode.as_raw` is
+        // valid because `fwnode` is valid.
+        let ret = unsafe {
+            bindings::fwnode_property_read_string(fwnode.as_raw(), name.as_char_ptr(), pstr.cast())
+        };
+        to_result(ret)?;
+
+        // SAFETY: `pstr` contains a non-null ptr on success
+        let str = unsafe { CStr::from_char_ptr(*pstr) };
+        Ok(str.try_into()?)
+    }
+}
+/// Implemented for all integers that can be read as properties.
+///
+/// This helper trait is needed on top of the existing [`Property`]
+/// trait to associate the integer types of various sizes with their
+/// corresponding `fwnode_property_read_*_array` functions.
+pub trait PropertyInt: Copy {
+    /// # Safety
+    ///
+    /// Callers must uphold the same safety invariants as for the various
+    /// `fwnode_property_read_*_array` functions.
+    unsafe fn read_array(
+        fwnode: *const bindings::fwnode_handle,
+        propname: *const ffi::c_char,
+        val: *mut Self,
+        nval: usize,
+    ) -> ffi::c_int;
+}
+// This macro generates implementations of the traits `Property` and
+// `PropertyInt` for integers of various sizes. Its input is a list
+// of pairs separated by commas. The first element of the pair is the
+// type of the integer, the second one is the name of its corresponding
+// `fwnode_property_read_*_array` function.
+macro_rules! impl_property_for_int {
+    ($($int:ty: $f:ident),* $(,)?) => { $(
+        impl PropertyInt for $int {
+            unsafe fn read_array(
+                fwnode: *const bindings::fwnode_handle,
+                propname: *const ffi::c_char,
+                val: *mut Self,
+                nval: usize,
+            ) -> ffi::c_int {
+                // SAFETY: The safety invariants on the trait require
+                // callers to uphold the invariants of the functions
+                // this macro is called with.
+                unsafe {
+                    bindings::$f(fwnode, propname, val.cast(), nval)
+                }
+            }
+        }
+    )* };
+}
+impl_property_for_int! {
+    u8: fwnode_property_read_u8_array,
+    u16: fwnode_property_read_u16_array,
+    u32: fwnode_property_read_u32_array,
+    u64: fwnode_property_read_u64_array,
+    i8: fwnode_property_read_u8_array,
+    i16: fwnode_property_read_u16_array,
+    i32: fwnode_property_read_u32_array,
+    i64: fwnode_property_read_u64_array,
+}
+/// # Safety
+///
+/// Callers must ensure that if `len` is non-zero, `out_param` must be
+/// valid and point to memory that has enough space to hold at least
+/// `len` number of elements.
+unsafe fn read_array_out_param<T: PropertyInt>(
+    fwnode: &FwNode,
+    name: &CStr,
+    out_param: *mut T,
+    len: usize,
+) -> ffi::c_int {
+    // SAFETY: `name` is non-null and null-terminated.
+    // `fwnode.as_raw` is valid because `fwnode` is valid.
+    // `out_param` is valid and has enough space for at least
+    // `len` number of elements as per the safety requirement.
+    unsafe { T::read_array(fwnode.as_raw(), name.as_char_ptr(), out_param, len) }
+}
+impl<T: PropertyInt, const N: usize> Property for [T; N] {
+    fn read(fwnode: &FwNode, name: &CStr) -> Result<Self> {
+        let mut val: [MaybeUninit<T>; N] = [const { MaybeUninit::uninit() }; N];
+
+        // SAFETY: `val.as_mut_ptr()` is valid and points to enough space for
+        // `N` elements. Casting from `*mut MaybeUninit<T>` to `*mut T` is safe
+        // because `MaybeUninit<T>` has the same memory layout as `T`.
+        let ret = unsafe { read_array_out_param::<T>(fwnode, name, val.as_mut_ptr().cast(), N) };
+        to_result(ret)?;
+
+        // SAFETY: `val` is always initialized when
+        // fwnode_property_read_<T>_array is successful.
+        Ok(val.map(|v| unsafe { v.assume_init() }))
+    }
+}
+impl<T: PropertyInt> Property for T {
+    fn read(fwnode: &FwNode, name: &CStr) -> Result<Self> {
+        let val: [_; 1] = <[T; 1] as Property>::read(fwnode, name)?;
+        Ok(val[0])
+    }
+}
+
+/// A helper for reading device properties.
+///
+/// Use [`Self::required`] if a missing property is considered a bug and
+/// [`Self::optional`] otherwise.
+///
+/// For convenience, [`Self::or`] and [`Self::or_default`] are provided.
+pub struct PropertyGuard<'fwnode, 'name, T> {
+    /// The result of reading the property.
+    inner: Result<T>,
+    /// The fwnode of the property, used for logging in the "required" case.
+    fwnode: &'fwnode FwNode,
+    /// The name of the property, used for logging in the "required" case.
+    name: &'name CStr,
+}
+
+impl<T> PropertyGuard<'_, '_, T> {
+    /// Access the property, indicating it is required.
+    ///
+    /// If the property is not present, the error is automatically logged. If a
+    /// missing property is not an error, use [`Self::optional`] instead.
+    pub fn required(self) -> Result<T> {
+        if self.inner.is_err() {
+            // Get the device associated with the fwnode for device-associated
+            // logging.
+            // TODO: Are we allowed to do this? The field `fwnode_handle.dev`
+            // has a somewhat vague comment, which could mean we're not
+            // supposed to access it:
+            // https://elixir.bootlin.com/linux/v6.13.6/source/include/linux/fwnode.h#L51
+            // SAFETY: According to the invariant of FwNode, it is valid.
+            let dev = unsafe { (*self.fwnode.as_raw()).dev };
+
+            if dev.is_null() {
+                pr_err!(
+                    "{}: property '{}' is missing\n",
+                    self.fwnode.display_path(),
+                    self.name
+                );
+            } else {
+                // SAFETY: If dev is not null, it points to a valid device.
+                let dev: &Device = unsafe { &*dev.cast() };
+                dev_err!(
+                    dev,
+                    "{}: property '{}' is missing\n",
+                    self.fwnode.display_path(),
+                    self.name
+                );
+            };
+        }
+        self.inner
+    }
+
+    /// Access the property, indicating it is optional.
+    ///
+    /// In contrast to [`Self::required`], no error message is logged if
+    /// the property is not present.
+    pub fn optional(self) -> Option<T> {
+        self.inner.ok()
+    }
+
+    /// Access the property or the specified default value.
+    ///
+    /// Do not pass a sentinel value as default to detect a missing property.
+    /// Use [`Self::required`] or [`Self::optional`] instead.
+    pub fn or(self, default: T) -> T {
+        self.inner.unwrap_or(default)
+    }
+}
+
+impl<T: Default> PropertyGuard<'_, '_, T> {
+    /// Access the property or a default value.
+    ///
+    /// Use [`Self::or`] to specify a custom default value.
+    pub fn or_default(self) -> T {
+        self.inner.unwrap_or_default()
+    }
+}
Signature · 1 line
2.49.0
maintainer
#62 lore raw

Re: [PATCH v2 2/5] rust: Add bindings for reading device properties

Quoted 8 lines · Remo Senekowitsch

The device property API is a firmware agnostic API for reading properties from firmware (DT/ACPI) devices nodes and swnodes.

While the C API takes a pointer to a caller allocated variable/buffer, the rust API is designed to return a value and can be used in struct initialization. Rust generics are also utilized to support different types of properties where appropriate.

Quoted 4 lines

The PropertyGuard is a way to force users to specify whether a property is supposed to be required or not. This allows us to move error logging of missing required properties into core, preventing a lot of boilerplate in drivers.

The patch adds a lot of thing, i.e.
* implement PropertyInt
* implement PropertyGuard
* extend FwNode by a lot of functions
* extend Device by some property functions

I see that from v1 a lot of things have been squashed, likely because there are a few circular dependencies. Is there really no reasonable way to break this down a bit?

Quoted 58 lines
---
 rust/kernel/property.rs | 385 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 383 insertions(+), 2 deletions(-)
rust/kernel/property.rs +33 -2
index f6e6c980d..0d4ea3168 100644
@@ -4,9 +4,17 @@
 //!
 //! C header: [`include/linux/property.h`](srctree/include/linux/property.h)
 
-use core::ptr;
+use core::{mem::MaybeUninit, ptr};
 
-use crate::{bindings, device::Device, str::CStr, types::Opaque};
+use crate::{
+    alloc::KVec,
+    bindings, c_str,
+    device::Device,
+    error::{to_result, Result},
+    prelude::*,
+    str::{BStr, CStr, CString},
+    types::Opaque,
+};
 
 impl Device {
     /// Obtain the fwnode corresponding to the device.
@@ -28,6 +36,38 @@ fn fwnode(&self) -> &FwNode {
     pub fn property_present(&self, name: &CStr) -> bool {
         self.fwnode().property_present(name)
     }
+
+    /// Returns firmware property `name` boolean value
+    pub fn property_read_bool(&self, name: &CStr) -> bool {
+        self.fwnode().property_read_bool(name)
+    }
+
+    /// Returns the index of matching string `match_str` for firmware string property `name`
+    pub fn property_match_string(&self, name: &CStr, match_str: &CStr) -> Result<usize> {
+        self.fwnode().property_match_string(name, match_str)
+    }
+
+    /// Returns firmware property `name` integer array values in a KVec
+    pub fn property_read_array_vec<'fwnode, 'name, T: PropertyInt>(
+        &'fwnode self,
+        name: &'name CStr,
+        len: usize,
+    ) -> Result<PropertyGuard<'fwnode, 'name, KVec<T>>> {
+        self.fwnode().property_read_array_vec(name, len)
+    }
+
+    /// Returns integer array length for firmware property `name`
+    pub fn property_count_elem<T: PropertyInt>(&self, name: &CStr) -> Result<usize> {
+        self.fwnode().property_count_elem::<T>(name)

rust/kernel/property.rs fn fwnode(&self) -> &FwNode { · line 71

Quoted 10 lines
rust/kernel/property.rs +9 -0
+    }
+
+    /// Returns firmware property `name` integer scalar value
+    pub fn property_read<'fwnode, 'name, T: Property>(
+        &'fwnode self,
+        name: &'name CStr,
+    ) -> PropertyGuard<'fwnode, 'name, T> {
+        self.fwnode().property_read(name)
+    }
 }

Okay, I start to see why you have all those Device functions in a separate file. :)

I think we should move device.rs into its own module, i.e. rust/kernel/device/mod.rs and then create rust/kernel/device/property.rs.

Quoted 106 lines
rust/kernel/property.rs +100 -0
 /// A reference-counted fwnode_handle.
@@ -59,6 +99,150 @@ pub fn property_present(&self, name: &CStr) -> bool {
         // SAFETY: By the invariant of `CStr`, `name` is null-terminated.
         unsafe { bindings::fwnode_property_present(self.as_raw().cast_const(), name.as_char_ptr()) }
     }
+
+    /// Returns firmware property `name` boolean value
+    pub fn property_read_bool(&self, name: &CStr) -> bool {
+        // SAFETY: `name` is non-null and null-terminated. `self.as_raw()` is valid
+        // because `self` is valid.
+        unsafe { bindings::fwnode_property_read_bool(self.as_raw(), name.as_char_ptr()) }
+    }
+
+    /// Returns the index of matching string `match_str` for firmware string property `name`
+    pub fn property_match_string(&self, name: &CStr, match_str: &CStr) -> Result<usize> {
+        // SAFETY: `name` and `match_str` are non-null and null-terminated. `self.as_raw` is
+        // valid because `self` is valid.
+        let ret = unsafe {
+            bindings::fwnode_property_match_string(
+                self.as_raw(),
+                name.as_char_ptr(),
+                match_str.as_char_ptr(),
+            )
+        };
+        to_result(ret)?;
+        Ok(ret as usize)
+    }
+
+    /// Returns firmware property `name` integer array values in a KVec
+    pub fn property_read_array_vec<'fwnode, 'name, T: PropertyInt>(
+        &'fwnode self,
+        name: &'name CStr,
+        len: usize,
+    ) -> Result<PropertyGuard<'fwnode, 'name, KVec<T>>> {
+        let mut val: KVec<T> = KVec::with_capacity(len, GFP_KERNEL)?;
+
+        // SAFETY: `val.as_mut_ptr()` is valid because `KVec::with_capacity`
+        // didn't return an error and it has at least space for `len` number
+        // of elements.
+        let err = unsafe { read_array_out_param::<T>(self, name, val.as_mut_ptr(), len) };
+        let res = if err < 0 {
+            Err(Error::from_errno(err))
+        } else {
+            // SAFETY: fwnode_property_read_int_array() writes exactly `len` entries on success
+            unsafe { val.set_len(len) }
+            Ok(val)
+        };
+        Ok(PropertyGuard {
+            inner: res,
+            fwnode: self,
+            name,
+        })
+    }
+
+    /// Returns integer array length for firmware property `name`
+    pub fn property_count_elem<T: PropertyInt>(&self, name: &CStr) -> Result<usize> {
+        // SAFETY: `out_param` is allowed to be null because `len` is zero.
+        let ret = unsafe { read_array_out_param::<T>(self, name, ptr::null_mut(), 0) };
+        to_result(ret)?;
+        Ok(ret as usize)
+    }
+
+    /// Returns the value of firmware property `name`.
+    ///
+    /// This method is generic over the type of value to read. Informally,
+    /// the types that can be read are booleans, strings, unsigned integers and
+    /// arrays of unsigned integers.
+    ///
+    /// Reading a `KVec` of integers is done with the separate
+    /// method [`Self::property_read_array_vec`], because it takes an
+    /// additional `len` argument.
+    ///
+    /// When reading a boolean, this method never fails. A missing property
+    /// is interpreted as `false`, whereas a present property is interpreted
+    /// as `true`.
+    ///
+    /// For more precise documentation about what types can be read, see
+    /// the [implementors of Property][Property#implementors] and [its
+    /// implementations on foreign types][Property#foreign-impls].
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// # use crate::{device::Device, types::CString};
+    /// fn examples(dev: &Device) -> Result {
+    ///     let fwnode = dev.fwnode();
+    ///     let b: bool = fwnode.property_read("some-bool").required()?;
+    ///     if let Some(s) = fwnode.property_read::<CString>("some-str").optional() {
+    ///         // ...
+    ///     }
+    /// }
+    /// ```
+    pub fn property_read<'fwnode, 'name, T: Property>(
+        &'fwnode self,
+        name: &'name CStr,
+    ) -> PropertyGuard<'fwnode, 'name, T> {
+        PropertyGuard {
+            inner: T::read(self, name),
+            fwnode: self,
+            name,
+        }
+    }
+
+    /// helper used to display name or path of a fwnode
+    ///

rust/kernel/property.rs pub fn property_present(&self, name: &CStr) -> bool { · line 211

Quoted 10 lines
rust/kernel/property.rs +10 -0
+    /// # Safety
+    ///
+    /// Callers must provide a valid format string for a fwnode.
+    unsafe fn fmt(&self, f: &mut core::fmt::Formatter<'_>, fmt_str: &CStr) -> core::fmt::Result {
+        let mut buf = [0; 256];
+        // SAFETY: `buf` is valid and `buf.len()` is its length. `self.as_raw()` is
+        // valid because `self` is valid.
+        let written = unsafe {
+            bindings::scnprintf(buf.as_mut_ptr(), buf.len(), fmt_str.as_ptr(), self.as_raw())
+        };

Why do we need this? Can't we use write! right away?

Quoted 62 lines
rust/kernel/property.rs +55 -0
+        // SAFETY: `written` is smaller or equal to `buf.len()`.
+        let b: &[u8] = unsafe { core::slice::from_raw_parts(buf.as_ptr(), written as usize) };
+        write!(f, "{}", BStr::from_bytes(b))
+    }
+
+    /// 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 + use<'_> {
+        struct FwNodeDisplayName<'a>(&'a FwNode);
+
+        impl core::fmt::Display for FwNodeDisplayName<'_> {
+            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+                // SAFETY: "%pfwP" is a valid format string for fwnode
+                unsafe { self.0.fmt(f, c_str!("%pfwP")) }
+            }
+        }
+
+        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 + use<'_> {
+        struct FwNodeDisplayPath<'a>(&'a FwNode);
+
+        impl core::fmt::Display for FwNodeDisplayPath<'_> {
+            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+                // SAFETY: "%pfwf" is a valid format string for fwnode
+                unsafe { self.0.fmt(f, c_str!("%pfwf")) }
+            }
+        }
+
+        FwNodeDisplayPath(self)
+    }
 }
 
 // SAFETY: Instances of `FwNode` are always reference-counted.
@@ -73,3 +257,200 @@ unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
         unsafe { bindings::fwnode_handle_put(obj.cast().as_ptr()) }
     }
 }
+
+/// Implemented for several types that can be read as properties.
+///
+/// Informally, this is implemented for strings, integers and arrays of
+/// integers. It's used to make [`FwNode::property_read`] generic over the
+/// type of property being read. There are also two dedicated methods to read
+/// other types, because they require more specialized function signatures:
+/// - [`property_read_bool`](Device::property_read_bool)
+/// - [`property_read_array_vec`](Device::property_read_array_vec)
+pub trait Property: Sized {
+    /// Used to make [`FwNode::property_read`] generic.
+    fn read(fwnode: &FwNode, name: &CStr) -> Result<Self>;
+}
+
+impl Property for CString {
+    fn read(fwnode: &FwNode, name: &CStr) -> Result<Self> {
+        let mut str: *mut u8 = ptr::null_mut();
+        let pstr: *mut _ = &mut str;
+
+        // SAFETY: `name` is non-null and null-terminated. `fwnode.as_raw` is
+        // valid because `fwnode` is valid.

rust/kernel/property.rs unsafe fn dec_ref(obj: ptr::NonNull<Self>) { · line 290

Quoted 10 lines
rust/kernel/property.rs +10 -0
+        let ret = unsafe {
+            bindings::fwnode_property_read_string(fwnode.as_raw(), name.as_char_ptr(), pstr.cast())
+        };
+        to_result(ret)?;
+
+        // SAFETY: `pstr` contains a non-null ptr on success
+        let str = unsafe { CStr::from_char_ptr(*pstr) };
+        Ok(str.try_into()?)
+    }
+}

I think it would be pretty weird to have a function CString::read() that takes a FwNode argument, no? Same for all the other types below.

I assume you do this for

	pub fn property_read<'fwnode, 'name, T: Property>(
	   &'fwnode self,
	   name: &'name CStr,
	)

but given that you have to do the separate impls anyways, is there so much value having the generic variant? You could still generate all the property_read_{int}() variants with a macro.

If you really want a generic property_read(), I think you should create new types instead and implement the Property trait for them instead.

Quoted 110 lines
rust/kernel/property.rs +110 -0
+/// Implemented for all integers that can be read as properties.
+///
+/// This helper trait is needed on top of the existing [`Property`]
+/// trait to associate the integer types of various sizes with their
+/// corresponding `fwnode_property_read_*_array` functions.
+pub trait PropertyInt: Copy {
+    /// # Safety
+    ///
+    /// Callers must uphold the same safety invariants as for the various
+    /// `fwnode_property_read_*_array` functions.
+    unsafe fn read_array(
+        fwnode: *const bindings::fwnode_handle,
+        propname: *const ffi::c_char,
+        val: *mut Self,
+        nval: usize,
+    ) -> ffi::c_int;
+}
+// This macro generates implementations of the traits `Property` and
+// `PropertyInt` for integers of various sizes. Its input is a list
+// of pairs separated by commas. The first element of the pair is the
+// type of the integer, the second one is the name of its corresponding
+// `fwnode_property_read_*_array` function.
+macro_rules! impl_property_for_int {
+    ($($int:ty: $f:ident),* $(,)?) => { $(
+        impl PropertyInt for $int {
+            unsafe fn read_array(
+                fwnode: *const bindings::fwnode_handle,
+                propname: *const ffi::c_char,
+                val: *mut Self,
+                nval: usize,
+            ) -> ffi::c_int {
+                // SAFETY: The safety invariants on the trait require
+                // callers to uphold the invariants of the functions
+                // this macro is called with.
+                unsafe {
+                    bindings::$f(fwnode, propname, val.cast(), nval)
+                }
+            }
+        }
+    )* };
+}
+impl_property_for_int! {
+    u8: fwnode_property_read_u8_array,
+    u16: fwnode_property_read_u16_array,
+    u32: fwnode_property_read_u32_array,
+    u64: fwnode_property_read_u64_array,
+    i8: fwnode_property_read_u8_array,
+    i16: fwnode_property_read_u16_array,
+    i32: fwnode_property_read_u32_array,
+    i64: fwnode_property_read_u64_array,
+}
+/// # Safety
+///
+/// Callers must ensure that if `len` is non-zero, `out_param` must be
+/// valid and point to memory that has enough space to hold at least
+/// `len` number of elements.
+unsafe fn read_array_out_param<T: PropertyInt>(
+    fwnode: &FwNode,
+    name: &CStr,
+    out_param: *mut T,
+    len: usize,
+) -> ffi::c_int {
+    // SAFETY: `name` is non-null and null-terminated.
+    // `fwnode.as_raw` is valid because `fwnode` is valid.
+    // `out_param` is valid and has enough space for at least
+    // `len` number of elements as per the safety requirement.
+    unsafe { T::read_array(fwnode.as_raw(), name.as_char_ptr(), out_param, len) }
+}
+impl<T: PropertyInt, const N: usize> Property for [T; N] {
+    fn read(fwnode: &FwNode, name: &CStr) -> Result<Self> {
+        let mut val: [MaybeUninit<T>; N] = [const { MaybeUninit::uninit() }; N];
+
+        // SAFETY: `val.as_mut_ptr()` is valid and points to enough space for
+        // `N` elements. Casting from `*mut MaybeUninit<T>` to `*mut T` is safe
+        // because `MaybeUninit<T>` has the same memory layout as `T`.
+        let ret = unsafe { read_array_out_param::<T>(fwnode, name, val.as_mut_ptr().cast(), N) };
+        to_result(ret)?;
+
+        // SAFETY: `val` is always initialized when
+        // fwnode_property_read_<T>_array is successful.
+        Ok(val.map(|v| unsafe { v.assume_init() }))
+    }
+}
+impl<T: PropertyInt> Property for T {
+    fn read(fwnode: &FwNode, name: &CStr) -> Result<Self> {
+        let val: [_; 1] = <[T; 1] as Property>::read(fwnode, name)?;
+        Ok(val[0])
+    }
+}
+
+/// A helper for reading device properties.
+///
+/// Use [`Self::required`] if a missing property is considered a bug and
+/// [`Self::optional`] otherwise.
+///
+/// For convenience, [`Self::or`] and [`Self::or_default`] are provided.
+pub struct PropertyGuard<'fwnode, 'name, T> {
+    /// The result of reading the property.
+    inner: Result<T>,
+    /// The fwnode of the property, used for logging in the "required" case.
+    fwnode: &'fwnode FwNode,
+    /// The name of the property, used for logging in the "required" case.
+    name: &'name CStr,
+}
+
+impl<T> PropertyGuard<'_, '_, T> {
+    /// Access the property, indicating it is required.
+    ///
+    /// If the property is not present, the error is automatically logged. If a
+    /// missing property is not an error, use [`Self::optional`] instead.

rust/kernel/property.rs unsafe fn dec_ref(obj: ptr::NonNull<Self>) { · line 410

Quoted 10 lines
rust/kernel/property.rs +10 -0
+    pub fn required(self) -> Result<T> {
+        if self.inner.is_err() {
+            // Get the device associated with the fwnode for device-associated
+            // logging.
+            // TODO: Are we allowed to do this? The field `fwnode_handle.dev`
+            // has a somewhat vague comment, which could mean we're not
+            // supposed to access it:
+            // https://elixir.bootlin.com/linux/v6.13.6/source/include/linux/fwnode.h#L51
+            // SAFETY: According to the invariant of FwNode, it is valid.
+            let dev = unsafe { (*self.fwnode.as_raw()).dev };

I don't think this is valid it do, AFAICS, a firmware node handle doesn't own a reference to the device pointer.

Quoted 49 lines
rust/kernel/property.rs +46 -0
+
+            if dev.is_null() {
+                pr_err!(
+                    "{}: property '{}' is missing\n",
+                    self.fwnode.display_path(),
+                    self.name
+                );
+            } else {
+                // SAFETY: If dev is not null, it points to a valid device.
+                let dev: &Device = unsafe { &*dev.cast() };
+                dev_err!(
+                    dev,
+                    "{}: property '{}' is missing\n",
+                    self.fwnode.display_path(),
+                    self.name
+                );
+            };
+        }
+        self.inner
+    }
+
+    /// Access the property, indicating it is optional.
+    ///
+    /// In contrast to [`Self::required`], no error message is logged if
+    /// the property is not present.
+    pub fn optional(self) -> Option<T> {
+        self.inner.ok()
+    }
+
+    /// Access the property or the specified default value.
+    ///
+    /// Do not pass a sentinel value as default to detect a missing property.
+    /// Use [`Self::required`] or [`Self::optional`] instead.
+    pub fn or(self, default: T) -> T {
+        self.inner.unwrap_or(default)
+    }
+}
+
+impl<T: Default> PropertyGuard<'_, '_, T> {
+    /// Access the property or a default value.
+    ///
+    /// Use [`Self::or`] to specify a custom default value.
+    pub fn or_default(self) -> T {
+        self.inner.unwrap_or_default()
+    }
+}
Signature · 1 line
2.49.0
maintainer
#63 lore raw

Re: [PATCH v2 2/5] rust: Add bindings for reading device properties

Quoted 9 lines · Danilo Krummrich
Quoted 8 lines · Remo Senekowitsch

The device property API is a firmware agnostic API for reading properties from firmware (DT/ACPI) devices nodes and swnodes.

While the C API takes a pointer to a caller allocated variable/buffer, the rust API is designed to return a value and can be used in struct initialization. Rust generics are also utilized to support different types of properties where appropriate.

Quoted 7 lines
Quoted 4 lines

The PropertyGuard is a way to force users to specify whether a property is supposed to be required or not. This allows us to move error logging of missing required properties into core, preventing a lot of boilerplate in drivers.

The patch adds a lot of thing, i.e.
* implement PropertyInt

I meant the Property trait and all its impls of course. :)

Quoted 3 lines

* implement PropertyGuard
* extend FwNode by a lot of functions
* extend Device by some property functions

#64 lore raw

Re: [PATCH v2 2/5] rust: Add bindings for reading device properties

In reply to Danilo Krummrich
Quoted 20 lines · Danilo Krummrich
Quoted 8 lines · Remo Senekowitsch

The device property API is a firmware agnostic API for reading properties from firmware (DT/ACPI) devices nodes and swnodes.

While the C API takes a pointer to a caller allocated variable/buffer, the rust API is designed to return a value and can be used in struct initialization. Rust generics are also utilized to support different types of properties where appropriate.

Quoted 4 lines

The PropertyGuard is a way to force users to specify whether a property is supposed to be required or not. This allows us to move error logging of missing required properties into core, preventing a lot of boilerplate in drivers.

The patch adds a lot of thing, i.e.
* implement PropertyInt
* implement PropertyGuard
* extend FwNode by a lot of functions
* extend Device by some property functions

Quoted 3 lines

I see that from v1 a lot of things have been squashed, likely because there are a few circular dependencies. Is there really no reasonable way to break this down a bit?

I was explicitly asked to do this in the previous thread[1]. I'm happy to invest time into organizing files and commits exactly the way people want, but squashing and splitting the same commits back and forth between subsequent patch series is a waste of my time.

Do reviewers not typically read the review comments of others as well? What can I do to avoid this situation and make progress instead of running in circles?

Quoted 13 lines

rust/kernel/property.rs pub fn property_present(&self, name: &CStr) -> bool { · line 211

Quoted 12 lines
rust/kernel/property.rs +12 -0
+    /// helper used to display name or path of a fwnode
+    ///
+    /// # Safety
+    ///
+    /// Callers must provide a valid format string for a fwnode.
+    unsafe fn fmt(&self, f: &mut core::fmt::Formatter<'_>, fmt_str: &CStr) -> core::fmt::Result {
+        let mut buf = [0; 256];
+        // SAFETY: `buf` is valid and `buf.len()` is its length. `self.as_raw()` is
+        // valid because `self` is valid.
+        let written = unsafe {
+            bindings::scnprintf(buf.as_mut_ptr(), buf.len(), fmt_str.as_ptr(), self.as_raw())
+        };
Quoted 1 line

Why do we need this? Can't we use write! right away?

I don't know how, can you be more specific? I'm not too familiar with how these formatting specifiers work under the hood, but on the face of it, Rust and C seem very different.

Quoted 83 lines
Quoted 62 lines
rust/kernel/property.rs +55 -0
+        // SAFETY: `written` is smaller or equal to `buf.len()`.
+        let b: &[u8] = unsafe { core::slice::from_raw_parts(buf.as_ptr(), written as usize) };
+        write!(f, "{}", BStr::from_bytes(b))
+    }
+
+    /// 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 + use<'_> {
+        struct FwNodeDisplayName<'a>(&'a FwNode);
+
+        impl core::fmt::Display for FwNodeDisplayName<'_> {
+            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+                // SAFETY: "%pfwP" is a valid format string for fwnode
+                unsafe { self.0.fmt(f, c_str!("%pfwP")) }
+            }
+        }
+
+        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 + use<'_> {
+        struct FwNodeDisplayPath<'a>(&'a FwNode);
+
+        impl core::fmt::Display for FwNodeDisplayPath<'_> {
+            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+                // SAFETY: "%pfwf" is a valid format string for fwnode
+                unsafe { self.0.fmt(f, c_str!("%pfwf")) }
+            }
+        }
+
+        FwNodeDisplayPath(self)
+    }
 }
 
 // SAFETY: Instances of `FwNode` are always reference-counted.
@@ -73,3 +257,200 @@ unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
         unsafe { bindings::fwnode_handle_put(obj.cast().as_ptr()) }
     }
 }
+
+/// Implemented for several types that can be read as properties.
+///
+/// Informally, this is implemented for strings, integers and arrays of
+/// integers. It's used to make [`FwNode::property_read`] generic over the
+/// type of property being read. There are also two dedicated methods to read
+/// other types, because they require more specialized function signatures:
+/// - [`property_read_bool`](Device::property_read_bool)
+/// - [`property_read_array_vec`](Device::property_read_array_vec)
+pub trait Property: Sized {
+    /// Used to make [`FwNode::property_read`] generic.
+    fn read(fwnode: &FwNode, name: &CStr) -> Result<Self>;
+}
+
+impl Property for CString {
+    fn read(fwnode: &FwNode, name: &CStr) -> Result<Self> {
+        let mut str: *mut u8 = ptr::null_mut();
+        let pstr: *mut _ = &mut str;
+
+        // SAFETY: `name` is non-null and null-terminated. `fwnode.as_raw` is
+        // valid because `fwnode` is valid.

rust/kernel/property.rs unsafe fn dec_ref(obj: ptr::NonNull<Self>) { · line 290

Quoted 10 lines
rust/kernel/property.rs +10 -0
+        let ret = unsafe {
+            bindings::fwnode_property_read_string(fwnode.as_raw(), name.as_char_ptr(), pstr.cast())
+        };
+        to_result(ret)?;
+
+        // SAFETY: `pstr` contains a non-null ptr on success
+        let str = unsafe { CStr::from_char_ptr(*pstr) };
+        Ok(str.try_into()?)
+    }
+}

I think it would be pretty weird to have a function CString::read() that takes a FwNode argument, no? Same for all the other types below.

I assume you do this for

	pub fn property_read<'fwnode, 'name, T: Property>(
	   &'fwnode self,
	   name: &'name CStr,
	)
Quoted 6 lines

but given that you have to do the separate impls anyways, is there so much value having the generic variant? You could still generate all the property_read_{int}() variants with a macro.

If you really want a generic property_read(), I think you should create new types instead and implement the Property trait for them instead.

Yeah, that would be workable. On the other hand, it's not unusual in Rust to implement traits on foreign types, right? If the problem is the non-descriptive name "read" then we can change it to something more verbose. Maybe CStr::read_from_fwnode_property or something. It's not meant to be used directly, a verbose name wouldn't cause any damage.

I think making the function generic can be nice to work with, especially if type inference is working. But I'm fine with either.

maintainer
#65 lore raw

Re: [PATCH v2 2/5] rust: Add bindings for reading device properties

Quoted 21 lines · Remo Senekowitsch
Quoted 20 lines · Danilo Krummrich
Quoted 8 lines · Remo Senekowitsch

The device property API is a firmware agnostic API for reading properties from firmware (DT/ACPI) devices nodes and swnodes.

While the C API takes a pointer to a caller allocated variable/buffer, the rust API is designed to return a value and can be used in struct initialization. Rust generics are also utilized to support different types of properties where appropriate.

Quoted 4 lines

The PropertyGuard is a way to force users to specify whether a property is supposed to be required or not. This allows us to move error logging of missing required properties into core, preventing a lot of boilerplate in drivers.

The patch adds a lot of thing, i.e.
* implement PropertyInt
* implement PropertyGuard
* extend FwNode by a lot of functions
* extend Device by some property functions

Quoted 5 lines
Quoted 3 lines

I see that from v1 a lot of things have been squashed, likely because there are a few circular dependencies. Is there really no reasonable way to break this down a bit?

I was explicitly asked to do this in the previous thread[1].

I'm well aware that you were asked to do so and that one reason was that subsequent patches started deleting code that was added in previous ones (hence my suspicion of circular dependencies and that splitting up things might not be super trivial).

Quoted 4 lines

I'm happy
to invest time into organizing files and commits exactly the way people want, but squashing and splitting the same commits back and forth between subsequent patch series is a waste of my time.

I don't think you were asked to go back and forth, but whether you see a reasonable way to break things down a bit, where "reasonable" means without deleting code that was just added.

Quoted 1 line

Do reviewers not typically read the review comments of others as well?

I think mostly they do, but maintainers and reviewers are rather busy people. So, I don't think you can expect everyone to follow every thread, especially when they get lengthy.

Quoted 2 lines

What can I do to avoid this situation and make progress instead of running in circles?

I suggest to investigate whether it can be split it up in a reasonable way and subsequently answer the question.

With your contribution you attempt to add a rather large portion of pretty core code. This isn't an easy task and quite some discussion is totally expected; please don't get frustrated, the series goes pretty well. :)

Quoted 16 lines
Quoted 13 lines
Quoted 12 lines
rust/kernel/property.rs +12 -0
+    /// helper used to display name or path of a fwnode
+    ///
+    /// # Safety
+    ///
+    /// Callers must provide a valid format string for a fwnode.
+    unsafe fn fmt(&self, f: &mut core::fmt::Formatter<'_>, fmt_str: &CStr) -> core::fmt::Result {
+        let mut buf = [0; 256];
+        // SAFETY: `buf` is valid and `buf.len()` is its length. `self.as_raw()` is
+        // valid because `self` is valid.
+        let written = unsafe {
+            bindings::scnprintf(buf.as_mut_ptr(), buf.len(), fmt_str.as_ptr(), self.as_raw())
+        };
Quoted 5 lines
Quoted 1 line

Why do we need this? Can't we use write! right away?

I don't know how, can you be more specific? I'm not too familiar with how these formatting specifiers work under the hood, but on the face of it, Rust and C seem very different.

See below.

Quoted 15 lines
Quoted 15 lines
Quoted 5 lines
rust/kernel/property.rs +4 -0
+        // SAFETY: `written` is smaller or equal to `buf.len()`.
+        let b: &[u8] = unsafe { core::slice::from_raw_parts(buf.as_ptr(), written as usize) };
+        write!(f, "{}", BStr::from_bytes(b))
+    }

rust/kernel/property.rs pub fn property_present(&self, name: &CStr) -> bool { · line 225

Quoted 10 lines
rust/kernel/property.rs +10 -0
+
+    /// 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 + use<'_> {
+        struct FwNodeDisplayName<'a>(&'a FwNode);
+
+        impl core::fmt::Display for FwNodeDisplayName<'_> {
+            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+                // SAFETY: "%pfwP" is a valid format string for fwnode
+                unsafe { self.0.fmt(f, c_str!("%pfwP")) }

I think this could just use write!() and fwnode_get_name(), right?

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
rust/kernel/property.rs +7 -0
+            }
+        }
+
+        FwNodeDisplayName(self)
+    }
+
+    /// Returns an object that implements [`Display`](core::fmt::Display) for
Quoted 8 lines
Quoted 8 lines

rust/kernel/property.rs pub fn property_present(&self, name: &CStr) -> bool { · line 240

Quoted 8 lines
rust/kernel/property.rs +8 -0
+    /// printing the full path of a node.
+    pub fn display_path(&self) -> impl core::fmt::Display + use<'_> {
+        struct FwNodeDisplayPath<'a>(&'a FwNode);
+
+        impl core::fmt::Display for FwNodeDisplayPath<'_> {
+            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+                // SAFETY: "%pfwf" is a valid format string for fwnode
+                unsafe { self.0.fmt(f, c_str!("%pfwf")) }

This one is indeed a bit more tricky, because it comes from fwnode_full_name_string() in lib/vsprintf.c.

Maybe it would be better to replicate the loop within fwnode_full_name_string() and call write! from there.

Quoted 58 lines
Quoted 58 lines
Quoted 33 lines
rust/kernel/property.rs +26 -0
+            }
+        }
+
+        FwNodeDisplayPath(self)
+    }
 }
 
 // SAFETY: Instances of `FwNode` are always reference-counted.
@@ -73,3 +257,200 @@ unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
         unsafe { bindings::fwnode_handle_put(obj.cast().as_ptr()) }
     }
 }
+
+/// Implemented for several types that can be read as properties.
+///
+/// Informally, this is implemented for strings, integers and arrays of
+/// integers. It's used to make [`FwNode::property_read`] generic over the
+/// type of property being read. There are also two dedicated methods to read
+/// other types, because they require more specialized function signatures:
+/// - [`property_read_bool`](Device::property_read_bool)
+/// - [`property_read_array_vec`](Device::property_read_array_vec)
+pub trait Property: Sized {
+    /// Used to make [`FwNode::property_read`] generic.
+    fn read(fwnode: &FwNode, name: &CStr) -> Result<Self>;
+}
+
+impl Property for CString {
+    fn read(fwnode: &FwNode, name: &CStr) -> Result<Self> {
+        let mut str: *mut u8 = ptr::null_mut();
+        let pstr: *mut _ = &mut str;
+
+        // SAFETY: `name` is non-null and null-terminated. `fwnode.as_raw` is
+        // valid because `fwnode` is valid.

rust/kernel/property.rs unsafe fn dec_ref(obj: ptr::NonNull<Self>) { · line 290

Quoted 10 lines
rust/kernel/property.rs +10 -0
+        let ret = unsafe {
+            bindings::fwnode_property_read_string(fwnode.as_raw(), name.as_char_ptr(), pstr.cast())
+        };
+        to_result(ret)?;
+
+        // SAFETY: `pstr` contains a non-null ptr on success
+        let str = unsafe { CStr::from_char_ptr(*pstr) };
+        Ok(str.try_into()?)
+    }
+}

I think it would be pretty weird to have a function CString::read() that takes a FwNode argument, no? Same for all the other types below.

I assume you do this for

	pub fn property_read<'fwnode, 'name, T: Property>(
	   &'fwnode self,
	   name: &'name CStr,
	)

but given that you have to do the separate impls anyways, is there so much value having the generic variant? You could still generate all the property_read_{int}() variants with a macro.

Quoted 8 lines
Quoted 2 lines

If you really want a generic property_read(), I think you should create new types instead and implement the Property trait for them instead.

Yeah, that would be workable. On the other hand, it's not unusual in Rust to implement traits on foreign types, right? If the problem is the non-descriptive name "read" then we can change it to something more verbose. Maybe CStr::read_from_fwnode_property or something. It's not meant to be used directly, a verbose name wouldn't cause any damage.

Yeah, if we keep this approach, I'd prefer a more descriptive name.

However, I'd like to hear some more opinions from other members of the Rust team on this one.

#66 lore raw

Re: [PATCH v2 2/5] rust: Add bindings for reading device properties

Quoted 33 lines · Danilo Krummrich
Quoted 21 lines · Remo Senekowitsch
Quoted 20 lines · Danilo Krummrich
Quoted 8 lines · Remo Senekowitsch

The device property API is a firmware agnostic API for reading properties from firmware (DT/ACPI) devices nodes and swnodes.

While the C API takes a pointer to a caller allocated variable/buffer, the rust API is designed to return a value and can be used in struct initialization. Rust generics are also utilized to support different types of properties where appropriate.

Quoted 4 lines

The PropertyGuard is a way to force users to specify whether a property is supposed to be required or not. This allows us to move error logging of missing required properties into core, preventing a lot of boilerplate in drivers.

The patch adds a lot of thing, i.e.
* implement PropertyInt
* implement PropertyGuard
* extend FwNode by a lot of functions
* extend Device by some property functions

Quoted 5 lines
Quoted 3 lines

I see that from v1 a lot of things have been squashed, likely because there are a few circular dependencies. Is there really no reasonable way to break this down a bit?

I was explicitly asked to do this in the previous thread[1].

I'm well aware that you were asked to do so and that one reason was that subsequent patches started deleting code that was added in previous ones (hence my suspicion of circular dependencies and that splitting up things might not be super trivial).

Quoted 8 lines
Quoted 4 lines

I'm happy
to invest time into organizing files and commits exactly the way people want, but squashing and splitting the same commits back and forth between subsequent patch series is a waste of my time.

I don't think you were asked to go back and forth, but whether you see a reasonable way to break things down a bit, where "reasonable" means without deleting code that was just added.

I was asked to squash two specific commits. The first was making the read method generic. That was the one that deleted much code. Totally reasonable, and the generic stuff might be discarded anyway, so I won't be moving stuff back and forth.

However, the second commit was the one introducing PropertyGuard. That was a beautifully atomic commit, no circular dependencies in sight. If this commit is to be split up, one of the smaller ones would without doubt look exactly the same as the one before. I provided a link[1] to the exact email telling me to squash that exact patch to avoid any confusion.

Quoted 11 lines
Quoted 1 line

Do reviewers not typically read the review comments of others as well?

I think mostly they do, but maintainers and reviewers are rather busy people. So, I don't think you can expect everyone to follow every thread, especially when they get lengthy.

Quoted 2 lines

What can I do to avoid this situation and make progress instead of running in circles?

I suggest to investigate whether it can be split it up in a reasonable way and subsequently answer the question.

The point is that I agree with you that the PropertyGuard patch can be split out. And possibly more: I think a reasonable person could make a separate commit for every property_read_<type> method. And I'm happy to do that if it's the way to go. But if I do that, send a v3 and then someone else asks me to squash it again (because they didn't read this exchange between you and me...) then I would've wasted my time.

Quoted 3 lines

With your contribution you attempt to add a rather large portion of pretty core code. This isn't an easy task and quite some discussion is totally expected; please don't get frustrated, the series goes pretty well. :)

I'm trying, but I can't say I have a good first impression of doing code review over a mailing list. Doing open-heart surgery with a pickfork and writing documents in Microsoft Word both seem like more productive and enjoyable activities to me.

maintainer
#67 lore raw

Re: [PATCH v2 2/5] rust: Add bindings for reading device properties

Quoted 49 lines · Remo Senekowitsch
Quoted 33 lines · Danilo Krummrich
Quoted 21 lines · Remo Senekowitsch
Quoted 20 lines · Danilo Krummrich
Quoted 8 lines · Remo Senekowitsch

The device property API is a firmware agnostic API for reading properties from firmware (DT/ACPI) devices nodes and swnodes.

While the C API takes a pointer to a caller allocated variable/buffer, the rust API is designed to return a value and can be used in struct initialization. Rust generics are also utilized to support different types of properties where appropriate.

Quoted 4 lines

The PropertyGuard is a way to force users to specify whether a property is supposed to be required or not. This allows us to move error logging of missing required properties into core, preventing a lot of boilerplate in drivers.

The patch adds a lot of thing, i.e.
* implement PropertyInt
* implement PropertyGuard
* extend FwNode by a lot of functions
* extend Device by some property functions

Quoted 5 lines
Quoted 3 lines

I see that from v1 a lot of things have been squashed, likely because there are a few circular dependencies. Is there really no reasonable way to break this down a bit?

I was explicitly asked to do this in the previous thread[1].

I'm well aware that you were asked to do so and that one reason was that subsequent patches started deleting code that was added in previous ones (hence my suspicion of circular dependencies and that splitting up things might not be super trivial).

Quoted 8 lines
Quoted 4 lines

I'm happy
to invest time into organizing files and commits exactly the way people want, but squashing and splitting the same commits back and forth between subsequent patch series is a waste of my time.

I don't think you were asked to go back and forth, but whether you see a reasonable way to break things down a bit, where "reasonable" means without deleting code that was just added.

I was asked to squash two specific commits. The first was making the read method generic. That was the one that deleted much code. Totally reasonable, and the generic stuff might be discarded anyway, so I won't be moving stuff back and forth.

Quoted 6 lines

However, the second commit was the one introducing PropertyGuard. That was a beautifully atomic commit, no circular dependencies in sight. If this commit is to be split up, one of the smaller ones would without doubt look exactly the same as the one before. I provided a link[1] to the exact email telling me to squash that exact patch to avoid any confusion.

Adding PropertyGuard changes the API. I don't think it makes sense to review the API without it and then again with it.

It might be reasonable to split adding the fwnode bindings and then the Device version. But those are in separate files anyway, so it's not really making review easier.

Quoted 5 lines
Quoted 5 lines
Quoted 1 line

Do reviewers not typically read the review comments of others as well?

I think mostly they do, but maintainers and reviewers are rather busy people. So, I don't think you can expect everyone to follow every thread, especially when they get lengthy.

Please understand maintainers are reviewing 10s to 100s of patches a week. I don't necessarily remember my own comments from last week.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

What can I do to avoid this situation and make progress instead of running in circles?

I suggest to investigate whether it can be split it up in a reasonable way and subsequently answer the question.

Quoted 6 lines

The point is that I agree with you that the PropertyGuard patch can be split out. And possibly more: I think a reasonable person could make a separate commit for every property_read_<type> method. And I'm happy to do that if it's the way to go. But if I do that, send a v3 and then someone else asks me to squash it again (because they didn't read this exchange between you and me...) then I would've wasted my time.

A commit per method really seems excessive to me. I prefer to look at the API as a whole, and to do that with separate patches only makes that harder.

A reasonable split here is possibly splitting the fwnode and the Device versions of the API. In any case, I think we've discussed this enough and I don't care to discuss it more, so whatever reasonable split you come up with is fine with me.

Quoted 8 lines
Quoted 3 lines

With your contribution you attempt to add a rather large portion of pretty core code. This isn't an easy task and quite some discussion is totally expected; please don't get frustrated, the series goes pretty well. :)

I'm trying, but I can't say I have a good first impression of doing code review over a mailing list. Doing open-heart surgery with a pickfork and writing documents in Microsoft Word both seem like more productive and enjoyable activities to me.

Mailing lists are a good scapegoat, but you can have 2 reviewers with 2 different opinions anywhere. That's just the nature of a large, distributed project with lots of reviewers.

Rob

maintainer
#68 lore raw

Re: [PATCH v2 2/5] rust: Add bindings for reading device properties

Quoted 5 lines · Rob Herring

A reasonable split here is possibly splitting the fwnode and the Device versions of the API. In any case, I think we've discussed this enough and I don't care to discuss it more, so whatever reasonable split you come up with is fine with me.

+1

Let's wait for some more feedback on the Property trait impl for primitives, CString, etc. before moving on.

#69 lore raw

Re: [PATCH v2 2/5] rust: Add bindings for reading device properties

In reply to Rob Herring
Quoted 3 lines · Rob Herring

Adding PropertyGuard changes the API. I don't think it makes sense to review the API without it and then again with it.

We could add the PropertyGuard first and introduce the users later. Then they are in separate patches and the API doesn't change.

reviewer
#70 lore raw

Re: [PATCH v2 2/5] rust: Add bindings for reading device properties

In reply to Danilo Krummrich
Quoted 189 lines · On Tue, 15 Apr 2025 11:48:07 +0200 Danilo Krummrich <[email protected]> wrote:
Quoted 21 lines · Remo Senekowitsch
Quoted 20 lines · Danilo Krummrich
Quoted 8 lines · Remo Senekowitsch

The device property API is a firmware agnostic API for reading properties from firmware (DT/ACPI) devices nodes and swnodes.

While the C API takes a pointer to a caller allocated variable/buffer, the rust API is designed to return a value and can be used in struct initialization. Rust generics are also utilized to support different types of properties where appropriate.

Quoted 4 lines

The PropertyGuard is a way to force users to specify whether a property is supposed to be required or not. This allows us to move error logging of missing required properties into core, preventing a lot of boilerplate in drivers.

The patch adds a lot of thing, i.e.
* implement PropertyInt
* implement PropertyGuard
* extend FwNode by a lot of functions
* extend Device by some property functions

Quoted 5 lines
Quoted 3 lines

I see that from v1 a lot of things have been squashed, likely because there are a few circular dependencies. Is there really no reasonable way to break this down a bit?

I was explicitly asked to do this in the previous thread[1].

I'm well aware that you were asked to do so and that one reason was that subsequent patches started deleting code that was added in previous ones (hence my suspicion of circular dependencies and that splitting up things might not be super trivial).

Quoted 4 lines

I'm happy
to invest time into organizing files and commits exactly the way people want, but squashing and splitting the same commits back and forth between subsequent patch series is a waste of my time.

I don't think you were asked to go back and forth, but whether you see a reasonable way to break things down a bit, where "reasonable" means without deleting code that was just added.

Quoted 1 line

Do reviewers not typically read the review comments of others as well?

I think mostly they do, but maintainers and reviewers are rather busy people. So, I don't think you can expect everyone to follow every thread, especially when they get lengthy.

Quoted 2 lines

What can I do to avoid this situation and make progress instead of running in circles?

I suggest to investigate whether it can be split it up in a reasonable way and subsequently answer the question.

With your contribution you attempt to add a rather large portion of pretty core code. This isn't an easy task and quite some discussion is totally expected; please don't get frustrated, the series goes pretty well. :)

Quoted 16 lines
Quoted 13 lines
Quoted 12 lines
rust/kernel/property.rs +12 -0
+    /// helper used to display name or path of a fwnode
+    ///
+    /// # Safety
+    ///
+    /// Callers must provide a valid format string for a fwnode.
+    unsafe fn fmt(&self, f: &mut core::fmt::Formatter<'_>, fmt_str: &CStr) -> core::fmt::Result {
+        let mut buf = [0; 256];
+        // SAFETY: `buf` is valid and `buf.len()` is its length. `self.as_raw()` is
+        // valid because `self` is valid.
+        let written = unsafe {
+            bindings::scnprintf(buf.as_mut_ptr(), buf.len(), fmt_str.as_ptr(), self.as_raw())
+        };  
Quoted 5 lines
Quoted 1 line

Why do we need this? Can't we use write! right away?

I don't know how, can you be more specific? I'm not too familiar with how these formatting specifiers work under the hood, but on the face of it, Rust and C seem very different.

See below.

Quoted 15 lines
Quoted 15 lines
Quoted 5 lines
rust/kernel/property.rs +4 -0
+        // SAFETY: `written` is smaller or equal to `buf.len()`.
+        let b: &[u8] = unsafe { core::slice::from_raw_parts(buf.as_ptr(), written as usize) };
+        write!(f, "{}", BStr::from_bytes(b))
+    }

rust/kernel/property.rs pub fn property_present(&self, name: &CStr) -> bool { · line 225

Quoted 10 lines
rust/kernel/property.rs +10 -0
+
+    /// 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 + use<'_> {
+        struct FwNodeDisplayName<'a>(&'a FwNode);
+
+        impl core::fmt::Display for FwNodeDisplayName<'_> {
+            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+                // SAFETY: "%pfwP" is a valid format string for fwnode
+                unsafe { self.0.fmt(f, c_str!("%pfwP")) }  

I think this could just use write!() and fwnode_get_name(), right?

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
rust/kernel/property.rs +7 -0
+            }
+        }
+
+        FwNodeDisplayName(self)
+    }
+
+    /// Returns an object that implements [`Display`](core::fmt::Display) for
Quoted 8 lines
Quoted 8 lines

rust/kernel/property.rs pub fn property_present(&self, name: &CStr) -> bool { · line 240

Quoted 8 lines
rust/kernel/property.rs +8 -0
+    /// printing the full path of a node.
+    pub fn display_path(&self) -> impl core::fmt::Display + use<'_> {
+        struct FwNodeDisplayPath<'a>(&'a FwNode);
+
+        impl core::fmt::Display for FwNodeDisplayPath<'_> {
+            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+                // SAFETY: "%pfwf" is a valid format string for fwnode
+                unsafe { self.0.fmt(f, c_str!("%pfwf")) }  

This one is indeed a bit more tricky, because it comes from fwnode_full_name_string() in lib/vsprintf.c.

Maybe it would be better to replicate the loop within fwnode_full_name_string() and call write! from there.

Quoted 58 lines
Quoted 58 lines
Quoted 33 lines
rust/kernel/property.rs +26 -0
+            }
+        }
+
+        FwNodeDisplayPath(self)
+    }
 }
 
 // SAFETY: Instances of `FwNode` are always reference-counted.
@@ -73,3 +257,200 @@ unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
         unsafe { bindings::fwnode_handle_put(obj.cast().as_ptr()) }
     }
 }
+
+/// Implemented for several types that can be read as properties.
+///
+/// Informally, this is implemented for strings, integers and arrays of
+/// integers. It's used to make [`FwNode::property_read`] generic over the
+/// type of property being read. There are also two dedicated methods to read
+/// other types, because they require more specialized function signatures:
+/// - [`property_read_bool`](Device::property_read_bool)
+/// - [`property_read_array_vec`](Device::property_read_array_vec)
+pub trait Property: Sized {
+    /// Used to make [`FwNode::property_read`] generic.
+    fn read(fwnode: &FwNode, name: &CStr) -> Result<Self>;
+}
+
+impl Property for CString {
+    fn read(fwnode: &FwNode, name: &CStr) -> Result<Self> {
+        let mut str: *mut u8 = ptr::null_mut();
+        let pstr: *mut _ = &mut str;
+
+        // SAFETY: `name` is non-null and null-terminated. `fwnode.as_raw` is
+        // valid because `fwnode` is valid.

rust/kernel/property.rs unsafe fn dec_ref(obj: ptr::NonNull<Self>) { · line 290

Quoted 10 lines
rust/kernel/property.rs +10 -0
+        let ret = unsafe {
+            bindings::fwnode_property_read_string(fwnode.as_raw(), name.as_char_ptr(), pstr.cast())
+        };
+        to_result(ret)?;
+
+        // SAFETY: `pstr` contains a non-null ptr on success
+        let str = unsafe { CStr::from_char_ptr(*pstr) };
+        Ok(str.try_into()?)
+    }
+}  

I think it would be pretty weird to have a function CString::read() that takes a FwNode argument, no? Same for all the other types below.

I assume you do this for

	pub fn property_read<'fwnode, 'name, T: Property>(
	   &'fwnode self,
	   name: &'name CStr,
	)

but given that you have to do the separate impls anyways, is there so much value having the generic variant? You could still generate all the property_read_{int}() variants with a macro.

Quoted 8 lines
Quoted 2 lines

If you really want a generic property_read(), I think you should create new types instead and implement the Property trait for them instead.

Yeah, that would be workable. On the other hand, it's not unusual in Rust to implement traits on foreign types, right? If the problem is the non-descriptive name "read" then we can change it to something more verbose. Maybe CStr::read_from_fwnode_property or something. It's not meant to be used directly, a verbose name wouldn't cause any damage.

Quoted 4 lines

Yeah, if we keep this approach, I'd prefer a more descriptive name.

However, I'd like to hear some more opinions from other members of the Rust team on this one.

I think the trait approach is what people quite typically use. The additional functions are usually not an issue, as they won't be available if you don't import the trait. Using names like read_from_fwnode_property would be remove any concern for me.

Best,
Gary

#71 lore raw

Re: [PATCH v2 2/5] rust: Add bindings for reading device properties

In reply to Remo Senekowitsch

Hi Remo,

Quoted 24 lines · Remo Senekowitsch

The device property API is a firmware agnostic API for reading properties from firmware (DT/ACPI) devices nodes and swnodes.

While the C API takes a pointer to a caller allocated variable/buffer, the rust API is designed to return a value and can be used in struct initialization. Rust generics are also utilized to support different types of properties where appropriate.

The PropertyGuard is a way to force users to specify whether a property is supposed to be required or not. This allows us to move error logging of missing required properties into core, preventing a lot of boilerplate in drivers.

---
 rust/kernel/property.rs | 385 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 383 insertions(+), 2 deletions(-)
rust/kernel/property.rs +0 -0
index f6e6c980d..0d4ea3168 100644

...

Quoted 10 lines
rust/kernel/property.rs +10 -0
+    /// helper used to display name or path of a fwnode
+    ///
+    /// # Safety
+    ///
+    /// Callers must provide a valid format string for a fwnode.
+    unsafe fn fmt(&self, f: &mut core::fmt::Formatter<'_>, fmt_str: &CStr) -> core::fmt::Result {
+        let mut buf = [0; 256];
+        // SAFETY: `buf` is valid and `buf.len()` is its length. `self.as_raw()` is
+        // valid because `self` is valid.
+        let written = unsafe {

rust/kernel/property.rs

Quoted 10 lines
rust/kernel/property.rs +10 -0
+            bindings::scnprintf(buf.as_mut_ptr(), buf.len(), fmt_str.as_ptr(), self.as_raw())
+        };
+        // SAFETY: `written` is smaller or equal to `buf.len()`.
+        let b: &[u8] = unsafe { core::slice::from_raw_parts(buf.as_ptr(), written as usize) };
+        write!(f, "{}", BStr::from_bytes(b))
+    }
+
+    /// 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 + use<'_> {

I don't know about the details but with rustc 1.81 [1] I'm getting [2]. Just doing what is proposed seems to "fix" it:

rust/kernel/lib.rs +1 -0
index 3b1af034e902e..eadf7501d499b 100644
@@ -26,6 +26,7 @@
 #![feature(const_mut_refs)]
 #![feature(const_ptr_write)]
 #![feature(const_refs_to_cell)]
+#![feature(precise_capturing)]
 
 // Ensure conditional compilation based on the kernel configuration works;
 // otherwise we may silently break things like initcall handling.

Just want to mention it because I think the minimal rustc version we have to support is 1.78.

Best regards

Dirk

P.S.: Many thanks for working on this! :)

[1]

$ rustc --version
rustc 1.81.0 (eeb90cda1 2024-09-04)

[2]

error[E0658]: precise captures on impl Trait are experimental
--> rust/kernel/property.rs:256:61

    |
256 |     pub fn display_name(&self) -> impl core::fmt::Display + use<'_> {
    |                                                             ^^^
    |
    = note: see issue #123432

<https://github.com/rust-lang/rust/issues/123432> for more information
= help: add #![feature(precise_capturing)] to the crate attributes
to enable
= note: this compiler was built on 2024-09-04; consider upgrading it
if it is out of date

error[E0658]: precise captures on impl Trait are experimental
--> rust/kernel/property.rs:271:61

    |
271 |     pub fn display_path(&self) -> impl core::fmt::Display + use<'_> {
    |                                                             ^^^
    |
    = note: see issue #123432

<https://github.com/rust-lang/rust/issues/123432> for more information
= help: add #![feature(precise_capturing)] to the crate attributes
to enable
= note: this compiler was built on 2024-09-04; consider upgrading it
if it is out of date

error: aborting due to 2 previous errors

#72 lore raw

Re: [PATCH v2 2/5] rust: Add bindings for reading device properties

Hi Dirk,

Quoted 66 lines · Dirk Behme

Hi Remo,

Quoted 24 lines · Remo Senekowitsch

The device property API is a firmware agnostic API for reading properties from firmware (DT/ACPI) devices nodes and swnodes.

While the C API takes a pointer to a caller allocated variable/buffer, the rust API is designed to return a value and can be used in struct initialization. Rust generics are also utilized to support different types of properties where appropriate.

The PropertyGuard is a way to force users to specify whether a property is supposed to be required or not. This allows us to move error logging of missing required properties into core, preventing a lot of boilerplate in drivers.

---
 rust/kernel/property.rs | 385 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 383 insertions(+), 2 deletions(-)
rust/kernel/property.rs +0 -0
index f6e6c980d..0d4ea3168 100644

...

Quoted 10 lines
rust/kernel/property.rs +10 -0
+    /// helper used to display name or path of a fwnode
+    ///
+    /// # Safety
+    ///
+    /// Callers must provide a valid format string for a fwnode.
+    unsafe fn fmt(&self, f: &mut core::fmt::Formatter<'_>, fmt_str: &CStr) -> core::fmt::Result {
+        let mut buf = [0; 256];
+        // SAFETY: `buf` is valid and `buf.len()` is its length. `self.as_raw()` is
+        // valid because `self` is valid.
+        let written = unsafe {

rust/kernel/property.rs

Quoted 10 lines
rust/kernel/property.rs +10 -0
+            bindings::scnprintf(buf.as_mut_ptr(), buf.len(), fmt_str.as_ptr(), self.as_raw())
+        };
+        // SAFETY: `written` is smaller or equal to `buf.len()`.
+        let b: &[u8] = unsafe { core::slice::from_raw_parts(buf.as_ptr(), written as usize) };
+        write!(f, "{}", BStr::from_bytes(b))
+    }
+
+    /// 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 + use<'_> {

I don't know about the details but with rustc 1.81 [1] I'm getting [2]. Just doing what is proposed seems to "fix" it:

rust/kernel/lib.rs +1 -0
index 3b1af034e902e..eadf7501d499b 100644
@@ -26,6 +26,7 @@
 #![feature(const_mut_refs)]
 #![feature(const_ptr_write)]
 #![feature(const_refs_to_cell)]
+#![feature(precise_capturing)]
 
 // Ensure conditional compilation based on the kernel configuration works;
 // otherwise we may silently break things like initcall handling.
Quoted 2 lines

Just want to mention it because I think the minimal rustc version we have to support is 1.78.

Thanks for catching this! I'll make sure to compile with 1.78 from now on. Luckily we don't need to activate an unstable feature, there is an "older" capturing syntax that works here. ('_ instead of use<'_>)

Quoted 33 lines

Best regards

Dirk

P.S.: Many thanks for working on this! :)

[1]

$ rustc --version
rustc 1.81.0 (eeb90cda1 2024-09-04)

[2]

error[E0658]: precise captures on impl Trait are experimental
--> rust/kernel/property.rs:256:61

    |
256 |     pub fn display_name(&self) -> impl core::fmt::Display + use<'_> {
    |                                                             ^^^
    |
    = note: see issue #123432

<https://github.com/rust-lang/rust/issues/123432> for more information
= help: add #![feature(precise_capturing)] to the crate attributes
to enable
= note: this compiler was built on 2024-09-04; consider upgrading it
if it is out of date

error[E0658]: precise captures on impl Trait are experimental
--> rust/kernel/property.rs:271:61

    |
271 |     pub fn display_path(&self) -> impl core::fmt::Display + use<'_> {
    |                                                             ^^^
    |
    = note: see issue #123432
Quoted 7 lines

<https://github.com/rust-lang/rust/issues/123432> for more information
= help: add #![feature(precise_capturing)] to the crate attributes
to enable
= note: this compiler was built on 2024-09-04; consider upgrading it
if it is out of date

error: aborting due to 2 previous errors

--
Best regards,
Remo

#73 lore raw

Re: [PATCH v2 2/5] rust: Add bindings for reading device properties

In reply to Remo Senekowitsch

Hi Remo,

Quoted 25 lines · Remo Senekowitsch

The device property API is a firmware agnostic API for reading properties from firmware (DT/ACPI) devices nodes and swnodes.

While the C API takes a pointer to a caller allocated variable/buffer, the rust API is designed to return a value and can be used in struct initialization. Rust generics are also utilized to support different types of properties where appropriate.

The PropertyGuard is a way to force users to specify whether a property is supposed to be required or not. This allows us to move error logging of missing required properties into core, preventing a lot of boilerplate in drivers.

---
 rust/kernel/property.rs | 385 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 383 insertions(+), 2 deletions(-)
rust/kernel/property.rs +0 -0
index f6e6c980d..0d4ea3168 100644
@@ -4,9 +4,17 @@

....

Quoted 20 lines
rust/kernel/property.rs +20 -0
+    /// Returns the value of firmware property `name`.
+    ///
+    /// This method is generic over the type of value to read. Informally,
+    /// the types that can be read are booleans, strings, unsigned integers and
+    /// arrays of unsigned integers.
+    ///
+    /// Reading a `KVec` of integers is done with the separate
+    /// method [`Self::property_read_array_vec`], because it takes an
+    /// additional `len` argument.
+    ///
+    /// When reading a boolean, this method never fails. A missing property
+    /// is interpreted as `false`, whereas a present property is interpreted
+    /// as `true`.
+    ///
+    /// For more precise documentation about what types can be read, see
+    /// the [implementors of Property][Property#implementors] and [its
+    /// implementations on foreign types][Property#foreign-impls].
+    ///
+    /// # Examples
+    ///

rust/kernel/property.rs

Quoted 10 lines
rust/kernel/property.rs +10 -0
+    /// ```
+    /// # use crate::{device::Device, types::CString};
+    /// fn examples(dev: &Device) -> Result {
+    ///     let fwnode = dev.fwnode();
+    ///     let b: bool = fwnode.property_read("some-bool").required()?;
+    ///     if let Some(s) = fwnode.property_read::<CString>("some-str").optional() {
+    ///         // ...
+    ///     }
+    /// }
+    /// ```

With CONFIG_RUST_KERNEL_DOCTESTS=y I had to change this example to [1] to make it buildable.

Best regards

Dirk

[1]

rust/kernel/property.rs +0 -0
index 17ad176378206..bd43c910786d6 100644
@@ -214,11 +214,10 @@ pub fn property_count_elem<T: PropertyInt>(&self,

name: &CStr) -> Result<usize>

     /// # Examples
     ///
     /// ```
-    /// # use crate::{device::Device, types::CString};
-    /// fn examples(dev: &Device) -> Result {
-    ///     let fwnode = dev.fwnode();
-    ///     let b: bool = fwnode.property_read("some-bool").required()?;
-    ///     if let Some(s) =
fwnode.property_read::<CString>("some-str").optional() {
+    /// # use kernel::{c_str, property::FwNode, str::CString};
+    /// fn examples(fwnode: &FwNode) -> () {
+    ///     let b: bool = fwnode.property_read_bool(c_str!("some-bool"));
+    ///     if let Some(s) =
fwnode.property_read::<CString>(c_str!("some-str")).optional() {
     ///         // ...
     ///     }
     /// }
#74 lore raw

PATCH v2 3/5 rust: property: Add child accessor and iterator

In reply to Remo Senekowitsch

Allow Rust drivers to access children of a fwnode either by name or by iterating over all of them.

In C, there is the function fwnode_get_next_child_node for iteration and the macro fwnode_for_each_child_node that helps with handling the pointers. Instead of a macro, a native iterator is used in Rust such that regular for-loops can be used.

---
 rust/kernel/property.rs | 75 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 74 insertions(+), 1 deletion(-)
rust/kernel/property.rs +74 -1
index 0d4ea3168..d9fb773b6 100644
@@ -13,7 +13,7 @@
     error::{to_result, Result},
     prelude::*,
     str::{BStr, CStr, CString},
-    types::Opaque,
+    types::{ARef, Opaque},
 };
 
 impl Device {
@@ -68,6 +68,16 @@ pub fn property_read<'fwnode, 'name, T: Property>(
     ) -> PropertyGuard<'fwnode, 'name, T> {
         self.fwnode().property_read(name)
     }
+
+    /// Returns first matching named child node handle.
+    pub fn get_child_by_name(&self, name: &CStr) -> Option<ARef<FwNode>> {
+        self.fwnode().get_child_by_name(name)
+    }
+
+    /// Returns an iterator over a node's children.
+    pub fn children<'a>(&'a self) -> impl Iterator<Item = ARef<FwNode>> + 'a {
+        self.fwnode().children()
+    }
 }
 
 /// A reference-counted fwnode_handle.
@@ -89,6 +99,22 @@ pub fn property_read<'fwnode, 'name, T: Property>(
 pub struct FwNode(Opaque<bindings::fwnode_handle>);
 
 impl FwNode {
+    /// # Safety
+    ///
+    /// Callers must ensure that the reference count was incremented at least
+    /// once, and that they are properly relinquishing one increment. That is,
+    /// if there is only one increment, callers must not use the underlying
+    /// object anymore -- it is only safe to do so via the newly created
+    /// [`ARef`].
+    unsafe fn from_raw(raw: *mut bindings::fwnode_handle) -> ARef<Self> {
+        // SAFETY: As per the safety requirement, raw has an incremented
+        // refcount which won't be decremented elsewhere. It also it not null.
+        // It is safe to cast from a `*mut fwnode_handle` to `*mut FwNode`,
+        // because `FwNode` is  defined as a `#[repr(transparent)]` wrapper
+        // around `fwnode_handle`.
+        unsafe { ARef::from_raw(ptr::NonNull::new_unchecked(raw.cast())) }
+    }
+
     /// Obtain the raw `struct fwnode_handle *`.
     pub(crate) fn as_raw(&self) -> *mut bindings::fwnode_handle {
         self.0.get()
@@ -243,6 +269,53 @@ fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
 
         FwNodeDisplayPath(self)
     }
+
+    /// Returns first matching named child node handle.
+    pub fn get_child_by_name(&self, name: &CStr) -> Option<ARef<Self>> {
+        // SAFETY: `self` and `name` are valid.
+        let child =
+            unsafe { bindings::fwnode_get_named_child_node(self.as_raw(), name.as_char_ptr()) };
+        if child.is_null() {
+            return None;
+        }
+        // SAFETY: `fwnode_get_named_child_node` returns a pointer with refcount incremented.
+        Some(unsafe { Self::from_raw(child) })
+    }
+
+    /// Returns an iterator over a node's children.
+    pub fn children<'a>(&'a self) -> impl Iterator<Item = ARef<FwNode>> + 'a {
+        let mut prev: Option<ARef<FwNode>> = None;
+
+        core::iter::from_fn(move || {
+            let prev_ptr = match prev.take() {
+                None => ptr::null_mut(),
+                Some(prev) => {
+                    // We will pass `prev` to `fwnode_get_next_child_node`,
+                    // which decrements its refcount, so we use
+                    // `ARef::into_raw` to avoid decrementing the refcount
+                    // twice.
+                    let prev = ARef::into_raw(prev);
+                    prev.as_ptr().cast()
+                }
+            };
+            // SAFETY: `self.as_raw()` is valid. `prev_ptr` may be null,
+            // which is allowed and corresponds to getting the first child.
+            // Otherwise, `prev_ptr` is valid, as it is the stored return
+            // value from the previous invocation. `prev_ptr` has its refount
+            // incremented and it won't be decremented anymore on the Rust
+            // side. `fwnode_get_next_child_node` decrements the refcount of
+            // `prev_ptr`, so the refcount is handled correctly.
+            let next = unsafe { bindings::fwnode_get_next_child_node(self.as_raw(), prev_ptr) };
+            if next.is_null() {
+                return None;
+            }
+            // SAFETY: `fwnode_get_next_child_node` returns a pointer with
+            // refcount incremented.
+            let next = unsafe { FwNode::from_raw(next) };
+            prev = Some(next.clone());
+            Some(next)
+        })
+    }
 }
 
 // SAFETY: Instances of `FwNode` are always reference-counted.
Signature · 1 line
2.49.0
maintainer
#75 lore raw

Re: [PATCH v2 3/5] rust: property: Add child accessor and iterator

Quoted 6 lines · Remo Senekowitsch

impl Device {

rust/kernel/property.rs +1 -0
@@ -68,6 +68,16 @@ pub fn property_read<'fwnode, 'name, T: Property>(
     ) -> PropertyGuard<'fwnode, 'name, T> {
         self.fwnode().property_read(name)
     }
+

rust/kernel/property.rs pub fn property_read<'fwnode, 'name, T: Property>( · line 81

Quoted 10 lines
rust/kernel/property.rs +9 -0
+    /// Returns first matching named child node handle.
+    pub fn get_child_by_name(&self, name: &CStr) -> Option<ARef<FwNode>> {
+        self.fwnode().get_child_by_name(name)
+    }
+
+    /// Returns an iterator over a node's children.
+    pub fn children<'a>(&'a self) -> impl Iterator<Item = ARef<FwNode>> + 'a {
+        self.fwnode().children()
+    }
 }

Since those functions are within the impl Device block, please move them to device.rs.

rust/kernel/property.rs pub fn property_read<'fwnode, 'name, T: Property>( · line 108

Quoted 13 lines
rust/kernel/property.rs +7 -0
 /// A reference-counted fwnode_handle.
@@ -89,6 +99,22 @@ pub fn property_read<'fwnode, 'name, T: Property>(
 pub struct FwNode(Opaque<bindings::fwnode_handle>);
 
 impl FwNode {
+    /// # Safety
+    ///
+    /// Callers must ensure that the reference count was incremented at least
+    /// once, and that they are properly relinquishing one increment. That is,
+    /// if there is only one increment, callers must not use the underlying
+    /// object anymore -- it is only safe to do so via the newly created
+    /// [`ARef`].

Please compile multiple safety requirements into a list.

rust/kernel/property.rs pub fn property_read<'fwnode, 'name, T: Property>( · line 111

Quoted 3 lines
rust/kernel/property.rs +3 -0
+    unsafe fn from_raw(raw: *mut bindings::fwnode_handle) -> ARef<Self> {
+        // SAFETY: As per the safety requirement, raw has an incremented
+        // refcount which won't be decremented elsewhere. It also it not null.

Same here.

rust/kernel/property.rs pub fn property_read<'fwnode, 'name, T: Property>( · line 114

Quoted 3 lines
rust/kernel/property.rs +3 -0
+        // It is safe to cast from a `*mut fwnode_handle` to `*mut FwNode`,
+        // because `FwNode` is  defined as a `#[repr(transparent)]` wrapper
+        // around `fwnode_handle`.

This should be CAST: instead.

Quoted 7 lines
rust/kernel/property.rs +3 -0
+        unsafe { ARef::from_raw(ptr::NonNull::new_unchecked(raw.cast())) }
+    }
+
     /// Obtain the raw `struct fwnode_handle *`.
     pub(crate) fn as_raw(&self) -> *mut bindings::fwnode_handle {
         self.0.get()

rust/kernel/property.rs fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { · line 275

Quoted 7 lines
rust/kernel/property.rs +4 -0
@@ -243,6 +269,53 @@ fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
 
         FwNodeDisplayPath(self)
     }
+
+    /// Returns first matching named child node handle.
+    pub fn get_child_by_name(&self, name: &CStr) -> Option<ARef<Self>> {
+        // SAFETY: `self` and `name` are valid.

I'd say that &self is valid by its type invariant.

rust/kernel/property.rs fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { · line 281

Quoted 6 lines
rust/kernel/property.rs +6 -0
+        let child =
+            unsafe { bindings::fwnode_get_named_child_node(self.as_raw(), name.as_char_ptr()) };
+        if child.is_null() {
+            return None;
+        }
+        // SAFETY: `fwnode_get_named_child_node` returns a pointer with refcount incremented.

Please cover all safety requirements from from_raw().

Quoted 16 lines
rust/kernel/property.rs +16 -0
+        Some(unsafe { Self::from_raw(child) })
+    }
+
+    /// Returns an iterator over a node's children.
+    pub fn children<'a>(&'a self) -> impl Iterator<Item = ARef<FwNode>> + 'a {
+        let mut prev: Option<ARef<FwNode>> = None;
+
+        core::iter::from_fn(move || {
+            let prev_ptr = match prev.take() {
+                None => ptr::null_mut(),
+                Some(prev) => {
+                    // We will pass `prev` to `fwnode_get_next_child_node`,
+                    // which decrements its refcount, so we use
+                    // `ARef::into_raw` to avoid decrementing the refcount
+                    // twice.
+                    let prev = ARef::into_raw(prev);

rust/kernel/property.rs fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { · line 307

Quoted 10 lines
rust/kernel/property.rs +10 -0
+                    prev.as_ptr().cast()
+                }
+            };
+            // SAFETY: `self.as_raw()` is valid. `prev_ptr` may be null,
+            // which is allowed and corresponds to getting the first child.
+            // Otherwise, `prev_ptr` is valid, as it is the stored return
+            // value from the previous invocation. `prev_ptr` has its refount
+            // incremented and it won't be decremented anymore on the Rust
+            // side. `fwnode_get_next_child_node` decrements the refcount of
+            // `prev_ptr`, so the refcount is handled correctly.

Please also compile this into a list.

rust/kernel/property.rs fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { · line 313

Quoted 6 lines
rust/kernel/property.rs +6 -0
+            let next = unsafe { bindings::fwnode_get_next_child_node(self.as_raw(), prev_ptr) };
+            if next.is_null() {
+                return None;
+            }
+            // SAFETY: `fwnode_get_next_child_node` returns a pointer with
+            // refcount incremented.

You rather need to justify why the next you pass into from_raw() is valid.

Quoted 11 lines
rust/kernel/property.rs +5 -0
+            let next = unsafe { FwNode::from_raw(next) };
+            prev = Some(next.clone());
+            Some(next)
+        })
+    }
 }
 
 // SAFETY: Instances of `FwNode` are always reference-counted.
Signature · 1 line
2.49.0
#76 lore raw

PATCH v2 4/5 rust: property: Add property_get_reference_args

In reply to Remo Senekowitsch

Allow Rust code to read reference args from device properties. The wrapper type FwNodeReferenceArgs allows callers to access the buffer of read args safely.

---
 rust/kernel/property.rs | 79 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 79 insertions(+)
rust/kernel/property.rs +79 -0
index d9fb773b6..17ad17637 100644
@@ -78,6 +78,17 @@ pub fn get_child_by_name(&self, name: &CStr) -> Option<ARef<FwNode>> {
     pub fn children<'a>(&'a self) -> impl Iterator<Item = ARef<FwNode>> + 'a {
         self.fwnode().children()
     }
+
+    /// Finds a reference with arguments.
+    pub fn property_get_reference_args(
+        &self,
+        prop: &CStr,
+        nargs: NArgs<'_>,
+        index: u32,
+    ) -> Result<FwNodeReferenceArgs> {
+        self.fwnode()
+            .property_get_reference_args(prop, nargs, index)
+    }
 }
 
 /// A reference-counted fwnode_handle.
@@ -316,6 +327,65 @@ pub fn children<'a>(&'a self) -> impl Iterator<Item = ARef<FwNode>> + 'a {
             Some(next)
         })
     }
+
+    /// Finds a reference with arguments.
+    pub fn property_get_reference_args(
+        &self,
+        prop: &CStr,
+        nargs: NArgs<'_>,
+        index: u32,
+    ) -> Result<FwNodeReferenceArgs> {
+        let mut out_args = FwNodeReferenceArgs::default();
+
+        let (nargs_prop, nargs) = match nargs {
+            NArgs::Prop(nargs_prop) => (nargs_prop.as_char_ptr(), 0),
+            NArgs::N(nargs) => (ptr::null(), nargs),
+        };
+
+        // SAFETY: `self.0.get()` is valid. `prop.as_char_ptr()` is valid and
+        // zero-terminated. `nargs_prop` is valid and zero-terminated if `nargs`
+        // is zero, otherwise it is allowed to be a null-pointer.
+        let ret = unsafe {
+            bindings::fwnode_property_get_reference_args(
+                self.0.get(),
+                prop.as_char_ptr(),
+                nargs_prop,
+                nargs,
+                index,
+                &mut out_args.0,
+            )
+        };
+        to_result(ret)?;
+
+        Ok(out_args)
+    }
+}
+
+/// The return value of `property_get_reference_args`.
+///
+/// - [`Device::property_get_reference_args`]
+/// - [`FwNode::property_get_reference_args`]
+#[repr(transparent)]
+#[derive(Copy, Clone, Default)]
+pub struct FwNodeReferenceArgs(bindings::fwnode_reference_args);
+
+impl FwNodeReferenceArgs {
+    /// Returns the slice of reference arguments.
+    pub fn as_slice(&self) -> &[u64] {
+        // SAFETY: As per the safety invariant of FwNodeReferenceArgs, `nargs`
+        // is the number of elements in `args` that is valid.
+        unsafe { core::slice::from_raw_parts(self.0.args.as_ptr(), self.0.nargs as usize) }
+    }
+
+    /// Returns the number of reference arguments.
+    pub fn len(&self) -> usize {
+        self.0.nargs as usize
+    }
+
+    /// Returns `true` if there are no reference arguments.
+    pub fn is_empty(&self) -> bool {
+        self.0.nargs == 0
+    }
 }
 
 // SAFETY: Instances of `FwNode` are always reference-counted.
@@ -451,6 +521,15 @@ fn read(fwnode: &FwNode, name: &CStr) -> Result<Self> {
     }
 }
 
+/// The number of arguments of a reference.
+pub enum NArgs<'a> {
+    /// The name of the property of the reference indicating the number of
+    /// arguments.
+    Prop(&'a CStr),
+    /// The known number of arguments.
+    N(u32),
+}
+
 /// A helper for reading device properties.
 ///
 /// Use [`Self::required`] if a missing property is considered a bug and
Signature · 1 line
2.49.0
#77 lore raw

PATCH v2 5/5 samples: rust: platform: Add property read examples

In reply to Remo Senekowitsch

Add some example usage of the device property read methods for DT/ACPI/swnode properties.

---
 drivers/of/unittest-data/tests-platform.dtsi |  3 +
 samples/rust/rust_driver_platform.rs         | 69 +++++++++++++++++++-
 2 files changed, 69 insertions(+), 3 deletions(-)
drivers/of/unittest-data/tests-platform.dtsi +3 -0
index 4171f43cf..50a51f38a 100644
@@ -37,6 +37,9 @@ dev@100 {
 			test-device@2 {
 				compatible = "test,rust-device";
 				reg = <0x2>;
+
+				test,u32-prop = <0xdeadbeef>;
+				test,i16-array = /bits/ 16 <1 2 (-3) (-4)>;
 			};
 		};
 
samples/rust/rust_driver_platform.rs +66 -3

91 lines not on this page · raw message

Signature · 1 line
2.49.0
#78 lore raw

Re: [PATCH v2 5/5] samples: rust: platform: Add property read examples

Hi Remo,

Quoted 16 lines · Remo Senekowitsch

Add some example usage of the device property read methods for DT/ACPI/swnode properties.

---
 drivers/of/unittest-data/tests-platform.dtsi |  3 +
 samples/rust/rust_driver_platform.rs         | 69 +++++++++++++++++++-
 2 files changed, 69 insertions(+), 3 deletions(-)
drivers/of/unittest-data/tests-platform.dtsi +0 -0
index 4171f43cf..50a51f38a 100644

drivers/of/unittest-data/tests-platform.dtsi dev@100 { · line 42

Quoted 6 lines
drivers/of/unittest-data/tests-platform.dtsi +3 -0
@@ -37,6 +37,9 @@ dev@100 {
 			test-device@2 {
 				compatible = "test,rust-device";
 				reg = <0x2>;
+
+				test,u32-prop = <0xdeadbeef>;
+				test,i16-array = /bits/ 16 <1 2 (-3) (-4)>;

Is "test,u32-required-prop" missing here? See below ...

Quoted 63 lines
drivers/of/unittest-data/tests-platform.dtsi +0 -0
 			};
 		};
 
samples/rust/rust_driver_platform.rs +32 -3
index 8120609e2..0284f1840 100644
@@ -2,7 +2,7 @@
 
 //! Rust Platform driver sample.
 
-use kernel::{c_str, of, platform, prelude::*};
+use kernel::{c_str, of, platform, prelude::*, str::CString};
 
 struct SampleDriver {
     pdev: platform::Device,
@@ -22,18 +22,81 @@ impl platform::Driver for SampleDriver {
     const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
 
     fn probe(pdev: &mut platform::Device, info: Option<&Self::IdInfo>) -> Result<Pin<KBox<Self>>> {
-        dev_dbg!(pdev.as_ref(), "Probe Rust Platform driver sample.\n");
+        let dev = pdev.as_ref();
+
+        dev_dbg!(dev, "Probe Rust Platform driver sample.\n");
 
         if let Some(info) = info {
-            dev_info!(pdev.as_ref(), "Probed with info: '{}'.\n", info.0);
+            dev_info!(dev, "Probed with info: '{}'.\n", info.0);
         }
 
+        Self::properties_parse(dev)?;
+
         let drvdata = KBox::new(Self { pdev: pdev.clone() }, GFP_KERNEL)?;
 
         Ok(drvdata.into())
     }
 }
 
+impl SampleDriver {
+    fn properties_parse(dev: &kernel::device::Device) -> Result<()> {
+        if let Ok(idx) = dev.property_match_string(c_str!("compatible"), c_str!("test,rust-device"))
+        {
+            dev_info!(dev, "matched compatible string idx = {}\n", idx);
+        }
+
+        if let Ok(str) = dev
+            .property_read::<CString>(c_str!("compatible"))
+            .required()
+        {
+            dev_info!(dev, "compatible string = {:?}\n", str);
+        }
+
+        let prop = dev.property_read_bool(c_str!("test,bool-prop"));
+        dev_info!(dev, "bool prop is {}\n", prop);
+
+        if dev.property_present(c_str!("test,u32-prop")) {
+            dev_info!(dev, "'test,u32-prop' is present\n");
+        }
+
+        let prop = dev
+            .property_read::<u32>(c_str!("test,u32-optional-prop"))
+            .or(0x12);
+        dev_info!(

samples/rust/rust_driver_platform.rs impl platform::Driver for SampleDriver { · line 75

Quoted 10 lines
samples/rust/rust_driver_platform.rs +10 -0
+            dev,
+            "'test,u32-optional-prop' is {:#x} (default = {:#x})\n",
+            prop,
+            0x12
+        );
+
+        // Missing property without a default will print an error
+        let _ = dev
+            .property_read::<u32>(c_str!("test,u32-required-prop"))
+            .required()?;

On x86 using above dtsi it seems it error exits here:

[    2.178227] rust_driver_platform

testcase-data:platform-tests:test-device@2: Probed with info: '42'.

[    2.178715] rust_driver_platform

testcase-data:platform-tests:test-device@2: matched compatible string idx = 0

[    2.179122] rust_driver_platform

testcase-data:platform-tests:test-device@2: compatible string = "test,rust-device"

[    2.179563] rust_driver_platform

testcase-data:platform-tests:test-device@2: bool prop is false

[    2.179841] rust_driver_platform

testcase-data:platform-tests:test-device@2: 'test,u32-prop' is present

[    2.180200] rust_driver_platform

testcase-data:platform-tests:test-device@2: 'test,u32-optional-prop' is 0x12 (default = 0x12)

[    2.180598] rust_driver_platform
testcase-data:platform-tests:test-device@2:
/testcase-data/platform-tests/test-device@2: property
'test,u32-required-prop' is missing
[    2.181244] rust_driver_platform

testcase-data:platform-tests:test-device@2: probe with driver rust_driver_platform failed with error -22

I'm not sure if this is what we want?

Best regards

Dirk

Quoted 23 lines
samples/rust/rust_driver_platform.rs +23 -0
+        let prop: u32 = dev.property_read(c_str!("test,u32-prop")).required()?;
+        dev_info!(dev, "'test,u32-prop' is {:#x}\n", prop);
+
+        // TODO: remove or replace with u16? `Property` is not implemented for
+        // unsigned integers, as suggested by Andy Shevchenko.
+
+        let prop: [i16; 4] = dev.property_read(c_str!("test,i16-array")).required()?;
+        dev_info!(dev, "'test,i16-array' is {:?}\n", prop);
+        dev_info!(
+            dev,
+            "'test,i16-array' length is {}\n",
+            dev.property_count_elem::<u16>(c_str!("test,i16-array"))?,
+        );
+
+        let prop: KVec<i16> = dev
+            .property_read_array_vec(c_str!("test,i16-array"), 4)?
+            .required()?;
+        dev_info!(dev, "'test,i16-array' is KVec {:?}\n", prop);
+
+        Ok(())
+    }
+}
+
#79 lore raw

Re: [PATCH v2 5/5] samples: rust: platform: Add property read examples

Quoted 14 lines · Dirk Behme

Hi Remo,

Quoted 11 lines · Remo Senekowitsch

Add some example usage of the device property read methods for DT/ACPI/swnode properties.

---
 drivers/of/unittest-data/tests-platform.dtsi |  3 +
 samples/rust/rust_driver_platform.rs         | 69 +++++++++++++++++++-
 2 files changed, 69 insertions(+), 3 deletions(-)
Quoted 14 lines

drivers/of/unittest-data/tests-platform.dtsi dev@100 { · line 42

Quoted 11 lines
drivers/of/unittest-data/tests-platform.dtsi +3 -0
index 4171f43cf..50a51f38a 100644
@@ -37,6 +37,9 @@ dev@100 {
 			test-device@2 {
 				compatible = "test,rust-device";
 				reg = <0x2>;
+
+				test,u32-prop = <0xdeadbeef>;
+				test,i16-array = /bits/ 16 <1 2 (-3) (-4)>;

Is "test,u32-required-prop" missing here? See below ...

Ah, no, sorry, "test,u32-required-prop" isn't there intentionally to demonstrate the error handling on missing required properties. In this case I would propose to just remove the '?' to make the sample code not exit on this intended error:

(quoted diff) +0 -0

b/samples/rust/rust_driver_platform.rs
index 01902956e1ca7..b6cbd721a8882 100644

samples/rust/rust_driver_platform.rs +0 -0
@@ -75,7 +75,7 @@ fn properties_parse(dev: &kernel::device::Device) ->
Result<()> {
         // Missing property without a default will print an error
         let _ = dev
             .property_read::<u32>(c_str!("test,u32-required-prop"))

- .required()?;
+ .required();

         let prop: u32 =
dev.property_read(c_str!("test,u32-prop")).required()?;
         dev_info!(dev, "'test,u32-prop' is {:#x}\n", prop);

Best regards

Dirk

#80 lore raw
In reply to Remo Senekowitsch
Quoted 2 lines · Remo Senekowitsch

changes in v2:

If possible (no need to resend, it is just a suggestion), please submit new versions (especially multi-patch ones) in a new thread, otherwise in some clients (including lore.kernel.org) it gets harder to read.

Quoted 1 line

By the way, since I am here: to link to docs, nowadays there is docs.kernel.org which is nicer/shorter:

https://docs.kernel.org/process/submitting-patches.html#the-canonical-patch-format

I hope that helps!

Cheers,
Miguel

#81 lore raw
Quoted 8 lines · Miguel Ojeda
Quoted 2 lines · Remo Senekowitsch

changes in v2:

If possible (no need to resend, it is just a suggestion), please submit new versions (especially multi-patch ones) in a new thread, otherwise in some clients (including lore.kernel.org) it gets harder to read.

Okay, will do next time.

Junio Hamano recently wrote on the Git mailing list[1] that he wants his contributors to do this, so the original message-ID can be used as a "patch set ID". A quick glace at the archive[2] confirms that people work this way. I assumed the kernel community would appreciate this as well.

Best regards,
Remo

#82 lore raw
Quoted 6 lines · Remo Senekowitsch

Junio Hamano recently wrote on the Git mailing list[1] that he wants his contributors to do this, so the original message-ID can be used as a "patch set ID". A quick glace at the archive[2] confirms that people work this way. I assumed the kernel community would appreciate this as well.

I see, thanks for the reference!

I don't think it is a good idea for long series, given how some clients render things, e.g. GMail just shows the new version in a single linear thread and it gets really confusing.

Even in a nested/tree render, like Lore's option, it is harder to see and/or compare the versions, in my opinion. Perhaps Lore could be improved here (e.g. offering (auto-)folding of versions) -- most of the time we only care about the last version (and perhaps last - 1) anyway.

I think b4 behavior of subject+author works OK, but an explicit change-id would be nice. Reusing the Message-ID for that would only really work well if we could control the UI for everyone, I think.

Cheers,
Miguel