[PATCH 1/7] of: free unflattened overlays after application

Ahmad Fatoum a.fatoum at pengutronix.de
Fri May 17 00:48:18 PDT 2024


of_unflatten_dtb allocates an unflattened tree and of_overlay_apply_tree
copies it into another tree. The unflattened tree is then usually
leaked. Add a function that takes care of these two steps and then frees
the tree.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 arch/riscv/boards/riscvemu/board.c |  4 +---
 common/boards/qemu-virt/board.c    |  8 +++-----
 drivers/of/overlay.c               | 12 ++++++++++++
 include/of.h                       |  8 ++++++++
 test/self/regulator.c              |  7 +++----
 5 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/arch/riscv/boards/riscvemu/board.c b/arch/riscv/boards/riscvemu/board.c
index afd6608ac522..a81e0baaaf0a 100644
--- a/arch/riscv/boards/riscvemu/board.c
+++ b/arch/riscv/boards/riscvemu/board.c
@@ -38,12 +38,10 @@ extern char __dtbo_riscvemu_sram_start[];
 static int riscvemu_probe(struct device *dev)
 {
 	struct device_node *of_chosen;
-	struct device_node *overlay;
 	struct riscvemu_priv *priv;
 	u64 start;
 
-	overlay = of_unflatten_dtb(__dtbo_riscvemu_sram_start, INT_MAX);
-	of_overlay_apply_tree(dev->of_node, overlay);
+	of_overlay_apply_dtbo(dev->of_node, __dtbo_riscvemu_sram_start);
 	/* of_probe() will happen later at of_populate_initcall */
 
 	if (IS_ENABLED(CONFIG_CMD_TUTORIAL))
diff --git a/common/boards/qemu-virt/board.c b/common/boards/qemu-virt/board.c
index 4f2f7374c56a..9882b0c31a3c 100644
--- a/common/boards/qemu-virt/board.c
+++ b/common/boards/qemu-virt/board.c
@@ -54,7 +54,7 @@ BAREBOX_DEEP_PROBE_ENABLE(virt_of_match);
 static int virt_board_driver_init(void)
 {
 	struct device_node *root = of_get_root_node();
-	struct device_node *flash, *overlay, *pubkey;
+	struct device_node *flash, *pubkey;
 	const struct of_device_id *id;
 	void (*init)(void);
 
@@ -72,10 +72,8 @@ static int virt_board_driver_init(void)
 	 * configurations, where the first flash bank is secure-world only
 	 */
 	flash = of_find_node_by_path(PARTS_TARGET_PATH_STR);
-	if (flash && of_device_is_available(flash)) {
-		overlay = of_unflatten_dtb(__dtbo_qemu_virt_flash_start, INT_MAX);
-		of_overlay_apply_tree(root, overlay);
-	}
+	if (flash && of_device_is_available(flash))
+		of_overlay_apply_dtbo(root, __dtbo_qemu_virt_flash_start);
 
 	pubkey = of_unflatten_dtb(__dtb_fitimage_pubkey_start, INT_MAX);
 	of_merge_nodes(root, pubkey);
diff --git a/drivers/of/overlay.c b/drivers/of/overlay.c
index 73c7a91db9b5..91b88c3f1b57 100644
--- a/drivers/of/overlay.c
+++ b/drivers/of/overlay.c
@@ -334,6 +334,18 @@ int of_overlay_apply_file(struct device_node *root, const char *filename,
 	return ret;
 }
 
+int of_overlay_apply_dtbo(struct device_node *root, const void *dtbo)
+{
+	struct device_node *overlay;
+	int ret;
+
+	overlay = of_unflatten_dtb(dtbo, INT_MAX);
+	ret = of_overlay_apply_tree(root, overlay);
+	of_delete_node(overlay);
+
+	return ret;
+}
+
 static int of_overlay_fixup(struct device_node *root, void *data)
 {
 	struct device_node *overlay = data;
diff --git a/include/of.h b/include/of.h
index f99cff5cf54a..55f2c0cbdedf 100644
--- a/include/of.h
+++ b/include/of.h
@@ -1311,6 +1311,8 @@ int of_overlay_apply_tree(struct device_node *root,
 			  struct device_node *overlay);
 int of_overlay_apply_file(struct device_node *root, const char *filename,
 			  bool filter);
+int of_overlay_apply_dtbo(struct device_node *root,
+			  const void *dtbo);
 int of_register_overlay(struct device_node *overlay);
 int of_process_overlay(struct device_node *root,
 		    struct device_node *overlay,
@@ -1342,6 +1344,12 @@ static inline int of_overlay_apply_file(struct device_node *root,
 	return -ENOSYS;
 }
 
+static inline int of_overlay_apply_dtbo(struct device_node *root,
+					const void *dtbo)
+{
+	return -ENOSYS;
+}
+
 static inline int of_register_overlay(struct device_node *overlay)
 {
 	return -ENOSYS;
diff --git a/test/self/regulator.c b/test/self/regulator.c
index bcbcbe33e12f..d009a610f5f4 100644
--- a/test/self/regulator.c
+++ b/test/self/regulator.c
@@ -166,7 +166,6 @@ static struct device *dev;
 static void test_regulator(void)
 {
 	extern char __dtbo_test_regulator_start[];
-	struct device_node *overlay;
 	int ret;
 
 	if (!dev) {
@@ -174,10 +173,10 @@ static void test_regulator(void)
 		if (ret)
 			return;
 
-		overlay = of_unflatten_dtb(__dtbo_test_regulator_start, INT_MAX);
-		if (WARN_ON(IS_ERR(overlay)))
+		ret = of_overlay_apply_dtbo(of_get_root_node(), __dtbo_test_regulator_start);
+		if (WARN_ON(ret))
 			return;
-		of_overlay_apply_tree(of_get_root_node(), overlay);
+
 		of_probe();
 
 		dev = of_find_device_by_node_path("/regulator-self-test-pmic");
-- 
2.39.2




More information about the barebox mailing list