[PATCH] rust: kernel: device: Add of_platform_populate/depopulate

Ayush Singh posted 1 patch 7 months, 3 weeks ago
rust/bindings/bindings_helper.h |  1 +
rust/kernel/device.rs           | 10 ++++++++++
2 files changed, 11 insertions(+)
[PATCH] rust: kernel: device: Add of_platform_populate/depopulate
Posted by Ayush Singh 7 months, 3 weeks ago
Add abstractions for managed of_platform_populate and
of_platform_depopulate.

Signed-off-by: Ayush Singh <ayush@beagleboard.org>
---
Allow calling platform_populate/depopulate from Rust code.

To see how the bindings look in usage, see my working tree [0] for a
connector driver  I am working on.

Open Questions
***************

1. Function names

The rust implementations are based on devm_* versions of these
functions, i.e of_platform_depopulate() is called when the device is
unbound. Since in case of Rust, these are methods on the Device struct,
I am not sure if the `devm_` prefix is required.

2. Maybe should be functions instead of methods?

Not sure what the policy is regarding this.

[0]: https://github.com/Ayush1325/linux/commits/b4/beagle-cape/
---
 rust/bindings/bindings_helper.h |  1 +
 rust/kernel/device.rs           | 10 ++++++++++
 2 files changed, 11 insertions(+)

diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
index 8a2add69e5d66d1c2ebed9d2c950380e61c48842..51ec0754960377e5fc6bc0703487bf2086eff0e6 100644
--- a/rust/bindings/bindings_helper.h
+++ b/rust/bindings/bindings_helper.h
@@ -25,6 +25,7 @@
 #include <linux/mdio.h>
 #include <linux/miscdevice.h>
 #include <linux/of_device.h>
+#include <linux/of_platform.h>
 #include <linux/pci.h>
 #include <linux/phy.h>
 #include <linux/pid_namespace.h>
diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index 40c1f549b0bae9fd9aa3f41539ccb69896c2560d..7186fe9658ff2a143a43bd6b3500c9a6d6ef9630 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -207,6 +207,16 @@ 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()) }
     }
+
+    /// Populate platform_devices from device tree data
+    pub fn of_platform_populate(&self) -> crate::error::Result<()> {
+        crate::error::to_result(unsafe { bindings::devm_of_platform_populate(self.as_raw()) })
+    }
+
+    /// Remove devices populated from device tree
+    pub fn of_platform_depopulate(&self) {
+        unsafe { bindings::devm_of_platform_depopulate(self.as_raw()) }
+    }
 }
 
 // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic

---
base-commit: 33035b665157558254b3c21c3f049fd728e72368
change-id: 20250428-rust-of-populate-b2f961783441

Best regards,
-- 
Ayush Singh <ayush@beagleboard.org>
Re: [PATCH] rust: kernel: device: Add of_platform_populate/depopulate
Posted by Danilo Krummrich 7 months, 3 weeks ago
On Mon, Apr 28, 2025 at 07:52:29PM +0530, Ayush Singh wrote:
> Open Questions
> ***************
> 
> 1. Function names
> 
> The rust implementations are based on devm_* versions of these
> functions, i.e of_platform_depopulate() is called when the device is
> unbound. Since in case of Rust, these are methods on the Device struct,
> I am not sure if the `devm_` prefix is required.

Please add the 'devm' prefix.

> 2. Maybe should be functions instead of methods?

Making those methods is the correct thing.

> +    /// Populate platform_devices from device tree data
> +    pub fn of_platform_populate(&self) -> crate::error::Result<()> {
> +        crate::error::to_result(unsafe { bindings::devm_of_platform_populate(self.as_raw()) })

As Greg mentioned, please add the missing safety comments. Additionally, I
suggest to import to_result().

Please also make sure to go through [1].

> +    }
> +
> +    /// Remove devices populated from device tree
> +    pub fn of_platform_depopulate(&self) {
> +        unsafe { bindings::devm_of_platform_depopulate(self.as_raw()) }
> +    }
>  }

Please move both of those methods into

	impl Device<Bound> {

which ensures that those methods can only be called for a bound device, which is
required by devres.

The impl block does not exist for Device yet, so you have to create it with your
patch.

- Danilo

[1] https://rust-for-linux.com/contributing#submit-checklist-addendum
Re: [PATCH] rust: kernel: device: Add of_platform_populate/depopulate
Posted by Greg Kroah-Hartman 7 months, 3 weeks ago
On Mon, Apr 28, 2025 at 07:52:29PM +0530, Ayush Singh wrote:
> Add abstractions for managed of_platform_populate and
> of_platform_depopulate.
> 
> Signed-off-by: Ayush Singh <ayush@beagleboard.org>
> ---
> Allow calling platform_populate/depopulate from Rust code.
> 
> To see how the bindings look in usage, see my working tree [0] for a
> connector driver  I am working on.
> 
> Open Questions
> ***************
> 
> 1. Function names
> 
> The rust implementations are based on devm_* versions of these
> functions, i.e of_platform_depopulate() is called when the device is
> unbound. Since in case of Rust, these are methods on the Device struct,
> I am not sure if the `devm_` prefix is required.
> 
> 2. Maybe should be functions instead of methods?
> 
> Not sure what the policy is regarding this.
> 
> [0]: https://github.com/Ayush1325/linux/commits/b4/beagle-cape/
> ---
>  rust/bindings/bindings_helper.h |  1 +
>  rust/kernel/device.rs           | 10 ++++++++++
>  2 files changed, 11 insertions(+)
> 
> diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
> index 8a2add69e5d66d1c2ebed9d2c950380e61c48842..51ec0754960377e5fc6bc0703487bf2086eff0e6 100644
> --- a/rust/bindings/bindings_helper.h
> +++ b/rust/bindings/bindings_helper.h
> @@ -25,6 +25,7 @@
>  #include <linux/mdio.h>
>  #include <linux/miscdevice.h>
>  #include <linux/of_device.h>
> +#include <linux/of_platform.h>
>  #include <linux/pci.h>
>  #include <linux/phy.h>
>  #include <linux/pid_namespace.h>
> diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
> index 40c1f549b0bae9fd9aa3f41539ccb69896c2560d..7186fe9658ff2a143a43bd6b3500c9a6d6ef9630 100644
> --- a/rust/kernel/device.rs
> +++ b/rust/kernel/device.rs
> @@ -207,6 +207,16 @@ 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()) }
>      }
> +
> +    /// Populate platform_devices from device tree data
> +    pub fn of_platform_populate(&self) -> crate::error::Result<()> {
> +        crate::error::to_result(unsafe { bindings::devm_of_platform_populate(self.as_raw()) })

Don't you have to document the unsafe stuff here?

> +    }
> +
> +    /// Remove devices populated from device tree
> +    pub fn of_platform_depopulate(&self) {
> +        unsafe { bindings::devm_of_platform_depopulate(self.as_raw()) }

Same here?

And I'm going to think that not using devm_ as part of the rust binding
name is going to do nothing but cause people confusion over time...

thanks,

greg k-h