* [PATCH v2 1/2] rust: kernel: of: Add DeviceNode abstraction
2025-08-16 5:57 [PATCH v2 0/2] rust: Add abstractions for applying devicetree overlays Ayush Singh
@ 2025-08-16 5:57 ` Ayush Singh
2025-08-16 5:57 ` [PATCH v2 2/2] rust: kernel: of: Add overlay id abstraction Ayush Singh
2025-08-16 12:42 ` [PATCH v2 0/2] rust: Add abstractions for applying devicetree overlays Miguel Ojeda
2 siblings, 0 replies; 5+ messages in thread
From: Ayush Singh @ 2025-08-16 5:57 UTC (permalink / raw)
To: Jason Kridner, Deepak Khatri, Robert Nelson, Miguel Ojeda,
Dhruva Gole, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Greg Kroah-Hartman, Rafael J. Wysocki,
Rob Herring, Saravana Kannan, Benno Lossin, Benno Lossin
Cc: rust-for-linux, linux-kernel, devicetree, Ayush Singh
Allow getting struct device_node from struct fwnode_handle. This is
required when applying devicetree overlays using the
`of_overlay_fdt_apply`.
Signed-off-by: Ayush Singh <ayush@beagleboard.org>
---
rust/bindings/bindings_helper.h | 1 +
rust/helpers/of.c | 5 +++++
rust/kernel/device/property.rs | 18 ++++++++++++++++++
rust/kernel/of.rs | 12 ++++++++++++
4 files changed, 36 insertions(+)
diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
index 84d60635e8a9baef1f1a1b2752dc0fa044f8542f..f35764bf28647f6d567afcbe073c37b30a3b0284 100644
--- a/rust/bindings/bindings_helper.h
+++ b/rust/bindings/bindings_helper.h
@@ -58,6 +58,7 @@
#include <linux/jump_label.h>
#include <linux/mdio.h>
#include <linux/miscdevice.h>
+#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/pci.h>
#include <linux/phy.h>
diff --git a/rust/helpers/of.c b/rust/helpers/of.c
index 86b51167c913f96b626e55adb51bdac2a9f04f56..fba7a26dfd45a6d4350aa2a3e5b43ddbcd1b2c70 100644
--- a/rust/helpers/of.c
+++ b/rust/helpers/of.c
@@ -6,3 +6,8 @@ bool rust_helper_is_of_node(const struct fwnode_handle *fwnode)
{
return is_of_node(fwnode);
}
+
+struct device_node *rust_helper_to_of_node(const struct fwnode_handle *fwnode)
+{
+ return to_of_node(fwnode);
+}
diff --git a/rust/kernel/device/property.rs b/rust/kernel/device/property.rs
index 49ee12a906dbadbe932e207fc7a0d1d622125f5f..e19c1aebd5d5a7f0eedf8ef264d36b943eaf1fc5 100644
--- a/rust/kernel/device/property.rs
+++ b/rust/kernel/device/property.rs
@@ -68,6 +68,24 @@ pub fn is_of_node(&self) -> bool {
unsafe { bindings::is_of_node(self.as_raw()) }
}
+ /// Returns `DeviceNode` if `&self` is an OF node.
+ pub fn to_of_node(&self) -> Option<&crate::of::DeviceNode> {
+ // SAFETY: The type invariant of `Self` guarantees that `self.as_raw() is a pointer to a
+ // valid `struct fwnode_handle`.
+ let of_node = unsafe { bindings::to_of_node(self.as_raw()) };
+
+ if of_node.is_null() {
+ None
+ } else {
+ // SAFETY: `of_node` is valid. Its lifetime is tied to `&self`. We
+ // return a reference instead of an `ARef<DeviceNode>` because `to_of_node()`
+ // doesn't increment the refcount. It is safe to cast from a
+ // `struct device_node*` to a `*const DeviceNode` because `DeviceNode` is
+ // defined as a `#[repr(transparent)]` wrapper around `device_node`.
+ Some(unsafe { &*of_node.cast() })
+ }
+ }
+
/// Returns an object that implements [`Display`](core::fmt::Display) for
/// printing the name of a node.
///
diff --git a/rust/kernel/of.rs b/rust/kernel/of.rs
index b76b35265df2ea6b8b6dcf3d3dab5519d6092bf9..74d42dce06f0e9f67a0e5bd1287117b4966d4af9 100644
--- a/rust/kernel/of.rs
+++ b/rust/kernel/of.rs
@@ -6,6 +6,7 @@
bindings,
device_id::{RawDeviceId, RawDeviceIdIndex},
prelude::*,
+ types::Opaque
};
/// IdTable type for OF drivers.
@@ -63,3 +64,14 @@ macro_rules! of_device_table {
$crate::module_device_table!("of", $module_table_name, $table_name);
};
}
+
+/// Devicetree device node
+#[repr(transparent)]
+pub struct DeviceNode(Opaque<bindings::device_node>);
+
+impl DeviceNode {
+ /// Obtain the raw `struct device_node *`.
+ pub fn as_raw(&self) -> *mut bindings::device_node {
+ self.0.get()
+ }
+}
--
2.50.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v2 2/2] rust: kernel: of: Add overlay id abstraction
2025-08-16 5:57 [PATCH v2 0/2] rust: Add abstractions for applying devicetree overlays Ayush Singh
2025-08-16 5:57 ` [PATCH v2 1/2] rust: kernel: of: Add DeviceNode abstraction Ayush Singh
@ 2025-08-16 5:57 ` Ayush Singh
2025-08-17 5:50 ` kernel test robot
2025-08-16 12:42 ` [PATCH v2 0/2] rust: Add abstractions for applying devicetree overlays Miguel Ojeda
2 siblings, 1 reply; 5+ messages in thread
From: Ayush Singh @ 2025-08-16 5:57 UTC (permalink / raw)
To: Jason Kridner, Deepak Khatri, Robert Nelson, Miguel Ojeda,
Dhruva Gole, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Greg Kroah-Hartman, Rafael J. Wysocki,
Rob Herring, Saravana Kannan, Benno Lossin, Benno Lossin
Cc: rust-for-linux, linux-kernel, devicetree, Ayush Singh
Allow applying devicetree overlays from Rust.
The overlay is removed on Drop.
Signed-off-by: Ayush Singh <ayush@beagleboard.org>
---
rust/kernel/of.rs | 34 ++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/rust/kernel/of.rs b/rust/kernel/of.rs
index 74d42dce06f0e9f67a0e5bd1287117b4966d4af9..646148cc959fd5414271f9cf9023ef5d77a2ac2c 100644
--- a/rust/kernel/of.rs
+++ b/rust/kernel/of.rs
@@ -75,3 +75,37 @@ pub fn as_raw(&self) -> *mut bindings::device_node {
self.0.get()
}
}
+
+/// Devicetree overlay present in livetree.
+///
+/// The overlay is removed on Drop
+pub struct OvcsId(kernel::ffi::c_int);
+
+impl OvcsId {
+ /// Create and apply an overlay changeset to the live tree.
+ pub fn of_overlay_fdt_apply(overlay: &[u8], base: &DeviceNode) -> Result<Self> {
+ let mut ovcs_id: kernel::ffi::c_int = 0;
+
+ // Ensure that overlay length fits in u32
+ let Ok(overlay_len) = overlay.len().try_into() else {
+ return Err(crate::error::code::E2BIG);
+ };
+
+ crate::error::to_result(unsafe {
+ bindings::of_overlay_fdt_apply(
+ overlay.as_ptr().cast(),
+ overlay_len,
+ &mut ovcs_id,
+ base.as_raw(),
+ )
+ })?;
+
+ Ok(Self(ovcs_id))
+ }
+}
+
+impl Drop for OvcsId {
+ fn drop(&mut self) {
+ unsafe { bindings::of_overlay_remove(&mut self.0) };
+ }
+}
--
2.50.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2 2/2] rust: kernel: of: Add overlay id abstraction
2025-08-16 5:57 ` [PATCH v2 2/2] rust: kernel: of: Add overlay id abstraction Ayush Singh
@ 2025-08-17 5:50 ` kernel test robot
0 siblings, 0 replies; 5+ messages in thread
From: kernel test robot @ 2025-08-17 5:50 UTC (permalink / raw)
To: Ayush Singh, Jason Kridner, Deepak Khatri, Robert Nelson,
Miguel Ojeda, Dhruva Gole, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Greg Kroah-Hartman, Rafael J. Wysocki,
Rob Herring, Saravana Kannan, Benno Lossin
Cc: llvm, oe-kbuild-all, rust-for-linux, linux-kernel, devicetree,
Ayush Singh
Hi Ayush,
kernel test robot noticed the following build errors:
[auto build test ERROR on 931e46dcbc7e6035a90e9c4a27a84b660e083f0a]
url: https://github.com/intel-lab-lkp/linux/commits/Ayush-Singh/rust-kernel-of-Add-DeviceNode-abstraction/20250816-140122
base: 931e46dcbc7e6035a90e9c4a27a84b660e083f0a
patch link: https://lore.kernel.org/r/20250816-rust-overlay-abs-v2-2-48a2c8921df2%40beagleboard.org
patch subject: [PATCH v2 2/2] rust: kernel: of: Add overlay id abstraction
config: x86_64-rhel-9.4-rust (https://download.01.org/0day-ci/archive/20250817/202508171342.vNfvpebZ-lkp@intel.com/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
rustc: rustc 1.88.0 (6b00bc388 2025-06-23)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250817/202508171342.vNfvpebZ-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202508171342.vNfvpebZ-lkp@intel.com/
All errors (new ones prefixed by >>):
>> error[E0425]: cannot find function `of_overlay_fdt_apply` in crate `bindings`
--> rust/kernel/of.rs:95:23
|
95 | bindings::of_overlay_fdt_apply(
| ^^^^^^^^^^^^^^^^^^^^ not found in `bindings`
--
>> error[E0425]: cannot find function `of_overlay_remove` in crate `bindings`
--> rust/kernel/of.rs:109:28
|
109 | unsafe { bindings::of_overlay_remove(&mut self.0) };
| ^^^^^^^^^^^^^^^^^ not found in `bindings`
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2 0/2] rust: Add abstractions for applying devicetree overlays
2025-08-16 5:57 [PATCH v2 0/2] rust: Add abstractions for applying devicetree overlays Ayush Singh
2025-08-16 5:57 ` [PATCH v2 1/2] rust: kernel: of: Add DeviceNode abstraction Ayush Singh
2025-08-16 5:57 ` [PATCH v2 2/2] rust: kernel: of: Add overlay id abstraction Ayush Singh
@ 2025-08-16 12:42 ` Miguel Ojeda
2 siblings, 0 replies; 5+ messages in thread
From: Miguel Ojeda @ 2025-08-16 12:42 UTC (permalink / raw)
To: Ayush Singh
Cc: Jason Kridner, Deepak Khatri, Robert Nelson, Miguel Ojeda,
Dhruva Gole, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Greg Kroah-Hartman, Rafael J. Wysocki,
Rob Herring, Saravana Kannan, Benno Lossin, rust-for-linux,
linux-kernel, devicetree
On Sat, Aug 16, 2025 at 7:58 AM Ayush Singh <ayush@beagleboard.org> wrote:
>
> The kernel header currently seems to provide blank implementations of
> these methods when `CONFIG_OF_OVERLAY` is not enabled. But I am not sure
> what is rust-for-linux policy here.
In general, if a kernel feature/API has stubs that do nothing when not
configured, i.e. the API is always present (typically to simplify
callers), then it is likely the Rust side should also be available
unconditionally for similar reasons. We had a similar discussion
around debugfs recently.
Cheers,
Miguel
^ permalink raw reply [flat|nested] 5+ messages in thread