mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 1/7] of: free unflattened overlays after application
Date: Fri, 17 May 2024 09:48:18 +0200	[thread overview]
Message-ID: <20240517074824.767767-1-a.fatoum@pengutronix.de> (raw)

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@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




             reply	other threads:[~2024-05-17  7:49 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-17  7:48 Ahmad Fatoum [this message]
2024-05-17  7:48 ` [PATCH 2/7] of: overlay: do not leak fixed up path Ahmad Fatoum
2024-05-17  7:48 ` [PATCH 3/7] test: self: digest: don't leak digest buffers Ahmad Fatoum
2024-05-17  7:48 ` [PATCH 4/7] test: self: ramfs: fix memory leak Ahmad Fatoum
2024-05-17  7:48 ` [PATCH 5/7] regulator: of_regulator: remove unused allocation Ahmad Fatoum
2024-05-21  6:30   ` Sascha Hauer
2024-05-17  7:48 ` [PATCH 6/7] globalvar: fix memory leak Ahmad Fatoum
2024-05-17  7:48 ` [PATCH 7/7] power: reset: reboot-mode: " Ahmad Fatoum
2024-05-21  6:29 ` [PATCH 1/7] of: free unflattened overlays after application Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240517074824.767767-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox