mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/7] of: free unflattened overlays after application
@ 2024-05-17  7:48 Ahmad Fatoum
  2024-05-17  7:48 ` [PATCH 2/7] of: overlay: do not leak fixed up path Ahmad Fatoum
                   ` (6 more replies)
  0 siblings, 7 replies; 9+ messages in thread
From: Ahmad Fatoum @ 2024-05-17  7:48 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

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




^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 2/7] of: overlay: do not leak fixed up path
  2024-05-17  7:48 [PATCH 1/7] of: free unflattened overlays after application Ahmad Fatoum
@ 2024-05-17  7:48 ` Ahmad Fatoum
  2024-05-17  7:48 ` [PATCH 3/7] test: self: digest: don't leak digest buffers Ahmad Fatoum
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Ahmad Fatoum @ 2024-05-17  7:48 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

new_path is freshly allocated in every iteration and then leaked
after of_property_write_string() takes a copy from it.

Fix the leak by freeing it before it goes out of scope.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/of/overlay.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/of/overlay.c b/drivers/of/overlay.c
index 91b88c3f1b57..867a7a369739 100644
--- a/drivers/of/overlay.c
+++ b/drivers/of/overlay.c
@@ -151,6 +151,7 @@ static int of_overlay_apply_symbols(struct device_node *root,
 		pr_debug("add symbol %s with new path %s\n",
 			 prop->name, new_path);
 		of_property_write_string(root_symbols, prop->name, new_path);
+		free(new_path);
 	}
 
 	return 0;
-- 
2.39.2




^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 3/7] test: self: digest: don't leak digest buffers
  2024-05-17  7:48 [PATCH 1/7] of: free unflattened overlays after application Ahmad Fatoum
  2024-05-17  7:48 ` [PATCH 2/7] of: overlay: do not leak fixed up path Ahmad Fatoum
@ 2024-05-17  7:48 ` Ahmad Fatoum
  2024-05-17  7:48 ` [PATCH 4/7] test: self: ramfs: fix memory leak Ahmad Fatoum
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Ahmad Fatoum @ 2024-05-17  7:48 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

The selftest leaks memory by letting the pointers go out of scope.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 test/self/digest.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/test/self/digest.c b/test/self/digest.c
index 4cda5b09637b..32815c85ba70 100644
--- a/test/self/digest.c
+++ b/test/self/digest.c
@@ -47,7 +47,7 @@ static void __test_digest(bool option,
 			  const char *algo, struct digest_test_case *t,
 			  const char *func, int line)
 {
-	unsigned char *output, *digest;
+	unsigned char *output = NULL, *digest = NULL;
 	struct digest *d;
 	int hash_len, digest_len;
 	u64 start;
@@ -103,8 +103,14 @@ static void __test_digest(bool option,
 		goto fail;
 	}
 
+	digest_free(d);
+	free(digest);
+	free(output);
 	return;
 fail:
+	digest_free(d);
+	free(digest);
+	free(output);
 	failed_tests++;
 }
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 4/7] test: self: ramfs: fix memory leak
  2024-05-17  7:48 [PATCH 1/7] of: free unflattened overlays after application Ahmad Fatoum
  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 ` Ahmad Fatoum
  2024-05-17  7:48 ` [PATCH 5/7] regulator: of_regulator: remove unused allocation Ahmad Fatoum
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Ahmad Fatoum @ 2024-05-17  7:48 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Like many other tests, ramfs also leaks memory. Fix this.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 test/self/ramfs.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/test/self/ramfs.c b/test/self/ramfs.c
index 1bc2b3b068f0..239e3e690740 100644
--- a/test/self/ramfs.c
+++ b/test/self/ramfs.c
@@ -58,7 +58,7 @@ static void test_ramfs(void)
 	char *content = NULL;
 	char *oldpwd = NULL;
 	DIR *dir = NULL;
-	const char *dname;
+	char *dname;
 	struct stat st;
 	int i, j, ret, fd;
 	struct dirent *d;
@@ -196,6 +196,8 @@ static void test_ramfs(void)
 			expect_success(memcmp(buf, ARRAY_AND_SIZE(hello)),
 				       "read_file() content");
 		}
+
+		free(buf);
 	}
 
 out:
@@ -209,5 +211,6 @@ static void test_ramfs(void)
 
 	dir = opendir(dname);
 	expect_fail(dir ? 0 : -EISDIR, "opening removed directory");
+	free(dname);
 }
 bselftest(core, test_ramfs);
-- 
2.39.2




^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 5/7] regulator: of_regulator: remove unused allocation
  2024-05-17  7:48 [PATCH 1/7] of: free unflattened overlays after application Ahmad Fatoum
                   ` (2 preceding siblings ...)
  2024-05-17  7:48 ` [PATCH 4/7] test: self: ramfs: fix memory leak Ahmad Fatoum
@ 2024-05-17  7:48 ` Ahmad Fatoum
  2024-05-21  6:30   ` Sascha Hauer
  2024-05-17  7:48 ` [PATCH 6/7] globalvar: fix memory leak Ahmad Fatoum
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 9+ messages in thread
From: Ahmad Fatoum @ 2024-05-17  7:48 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

We leak this allocation, but we don't actually need it, so drop it.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/regulator/of_regulator.c | 6 ------
 1 file changed, 6 deletions(-)

diff --git a/drivers/regulator/of_regulator.c b/drivers/regulator/of_regulator.c
index 10f75a4f1c60..c037f85c439d 100644
--- a/drivers/regulator/of_regulator.c
+++ b/drivers/regulator/of_regulator.c
@@ -41,16 +41,10 @@ int of_regulator_match(struct device *dev, struct device_node *node,
 	unsigned int i;
 	const char *name;
 	struct device_node *child;
-	struct devm_of_regulator_matches *devm_matches;
 
 	if (!dev || !node)
 		return -EINVAL;
 
-	devm_matches = xzalloc(sizeof(struct devm_of_regulator_matches));
-
-	devm_matches->matches = matches;
-	devm_matches->num_matches = num_matches;
-
 	for (i = 0; i < num_matches; i++) {
 		struct of_regulator_match *match = &matches[i];
 		match->of_node = NULL;
-- 
2.39.2




^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 6/7] globalvar: fix memory leak
  2024-05-17  7:48 [PATCH 1/7] of: free unflattened overlays after application Ahmad Fatoum
                   ` (3 preceding siblings ...)
  2024-05-17  7:48 ` [PATCH 5/7] regulator: of_regulator: remove unused allocation Ahmad Fatoum
@ 2024-05-17  7:48 ` 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
  6 siblings, 0 replies; 9+ messages in thread
From: Ahmad Fatoum @ 2024-05-17  7:48 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

val is copied and then goes out of scope, thereby leaking the buffer it
points at. Free it before that happens.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/globalvar.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/common/globalvar.c b/common/globalvar.c
index a83529f98fe0..79624adcfe6f 100644
--- a/common/globalvar.c
+++ b/common/globalvar.c
@@ -370,6 +370,7 @@ int nvvar_load(void)
 		if (ret)
 			pr_err("failed to create nv variable %s: %s\n",
 					n, strerror(-ret));
+		free(val);
 	}
 
 	closedir(dir);
-- 
2.39.2




^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 7/7] power: reset: reboot-mode: fix memory leak
  2024-05-17  7:48 [PATCH 1/7] of: free unflattened overlays after application Ahmad Fatoum
                   ` (4 preceding siblings ...)
  2024-05-17  7:48 ` [PATCH 6/7] globalvar: fix memory leak Ahmad Fatoum
@ 2024-05-17  7:48 ` Ahmad Fatoum
  2024-05-21  6:29 ` [PATCH 1/7] of: free unflattened overlays after application Sascha Hauer
  6 siblings, 0 replies; 9+ messages in thread
From: Ahmad Fatoum @ 2024-05-17  7:48 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

magicbuf is only used for comparisons and debug output in
reboot_mode_register() and is then leaked after its return.

Free it instead once no longer needed.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/power/reset/nvmem-reboot-mode.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/power/reset/nvmem-reboot-mode.c b/drivers/power/reset/nvmem-reboot-mode.c
index 1a9422800ef7..446951dd77c7 100644
--- a/drivers/power/reset/nvmem-reboot-mode.c
+++ b/drivers/power/reset/nvmem-reboot-mode.c
@@ -71,6 +71,8 @@ static int nvmem_reboot_mode_probe(struct device *dev)
 	if (ret)
 		dev_err(dev, "can't register reboot mode\n");
 
+	free(magicbuf);
+
 	return ret;
 }
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/7] of: free unflattened overlays after application
  2024-05-17  7:48 [PATCH 1/7] of: free unflattened overlays after application Ahmad Fatoum
                   ` (5 preceding siblings ...)
  2024-05-17  7:48 ` [PATCH 7/7] power: reset: reboot-mode: " Ahmad Fatoum
@ 2024-05-21  6:29 ` Sascha Hauer
  6 siblings, 0 replies; 9+ messages in thread
From: Sascha Hauer @ 2024-05-21  6:29 UTC (permalink / raw)
  To: barebox, Ahmad Fatoum


On Fri, 17 May 2024 09:48:18 +0200, Ahmad Fatoum wrote:
> 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.
> 
> 

Applied, thanks!

[1/7] of: free unflattened overlays after application
      https://git.pengutronix.de/cgit/barebox/commit/?id=21598db0d106 (link may not be stable)
[2/7] of: overlay: do not leak fixed up path
      https://git.pengutronix.de/cgit/barebox/commit/?id=92b4598183fb (link may not be stable)
[3/7] test: self: digest: don't leak digest buffers
      https://git.pengutronix.de/cgit/barebox/commit/?id=920740d28004 (link may not be stable)
[4/7] test: self: ramfs: fix memory leak
      https://git.pengutronix.de/cgit/barebox/commit/?id=3451af15b4cb (link may not be stable)
[5/7] regulator: of_regulator: remove unused allocation
      https://git.pengutronix.de/cgit/barebox/commit/?id=ae990b192979 (link may not be stable)
[6/7] globalvar: fix memory leak
      https://git.pengutronix.de/cgit/barebox/commit/?id=4dcae371c292 (link may not be stable)
[7/7] power: reset: reboot-mode: fix memory leak
      https://git.pengutronix.de/cgit/barebox/commit/?id=22f770fb9041 (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 5/7] regulator: of_regulator: remove unused allocation
  2024-05-17  7:48 ` [PATCH 5/7] regulator: of_regulator: remove unused allocation Ahmad Fatoum
@ 2024-05-21  6:30   ` Sascha Hauer
  0 siblings, 0 replies; 9+ messages in thread
From: Sascha Hauer @ 2024-05-21  6:30 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Fri, May 17, 2024 at 09:48:22AM +0200, Ahmad Fatoum wrote:
> We leak this allocation, but we don't actually need it, so drop it.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  drivers/regulator/of_regulator.c | 6 ------
>  1 file changed, 6 deletions(-)
> 
> diff --git a/drivers/regulator/of_regulator.c b/drivers/regulator/of_regulator.c
> index 10f75a4f1c60..c037f85c439d 100644
> --- a/drivers/regulator/of_regulator.c
> +++ b/drivers/regulator/of_regulator.c
> @@ -41,16 +41,10 @@ int of_regulator_match(struct device *dev, struct device_node *node,
>  	unsigned int i;
>  	const char *name;
>  	struct device_node *child;
> -	struct devm_of_regulator_matches *devm_matches;
>  
>  	if (!dev || !node)
>  		return -EINVAL;
>  
> -	devm_matches = xzalloc(sizeof(struct devm_of_regulator_matches));
> -
> -	devm_matches->matches = matches;
> -	devm_matches->num_matches = num_matches;
> -

Removed the definition of struct devm_of_regulator_matches along with
this patch.

Sascha

>  	for (i = 0; i < num_matches; i++) {
>  		struct of_regulator_match *match = &matches[i];
>  		match->of_node = NULL;
> -- 
> 2.39.2
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2024-05-21  6:31 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-17  7:48 [PATCH 1/7] of: free unflattened overlays after application Ahmad Fatoum
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox