Skip to contentSkip to conversation

tracing: Fixes to bootconfig memory handling

PATCH In review Steven Rostedt 2 participants 5 messages Sep 14, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover tracing: Fixes to bootconfig memory handling

Some memory management fixes to the bootconfig code.

git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-trace.git
for-next

Head SHA1: 8e9f0934a07e699044d422ca9cfb553f25c72b41

Masami Hiramatsu (2):
bootconfig: Fix to check the xbc_node is used before free it
bootconfig: Free copied bootconfig data after boot

----

 init/main.c      | 8 ++++++++
 lib/bootconfig.c | 3 ++-
 2 files changed, 10 insertions(+), 1 deletion(-)
#1 lore raw

PATCH 1/2 bootconfig: Fix to check the xbc_node is used before free it

From: Masami Hiramatsu <[email protected]>

Fix to check the xbc_node is used before calling memblock_free() because passing NULL to phys_addr() will cause a panic. This will happen if user doesn't pass any bootconfig to the kernel, because kernel will call xbc_destroy_all() after booting.

Fixes 40caa127f3c7 ("init: bootconfig: Remove all bootconfig data when the init memory is removed")
---
 lib/bootconfig.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/bootconfig.c +2 -1
index f8419cff1147..4f8849706ef6 100644
@@ -792,7 +792,8 @@ void __init xbc_destroy_all(void)
 	xbc_data = NULL;
 	xbc_data_size = 0;
 	xbc_node_num = 0;
-	memblock_free(__pa(xbc_nodes), sizeof(struct xbc_node) * XBC_NODE_MAX);
+	if (xbc_nodes)
+		memblock_free(__pa(xbc_nodes), sizeof(struct xbc_node) * XBC_NODE_MAX);
 	xbc_nodes = NULL;
 	brace_index = 0;
 }
Signature · 1 line
2.32.0
Tested-by on v1 1/2
#2 lore raw

Re: [for-linus][PATCH 1/2] bootconfig: Fix to check the xbc_node is used before free it

Quoted 8 lines · Steven Rostedt

From: Masami Hiramatsu <[email protected]>

Fix to check the xbc_node is used before calling memblock_free() because passing NULL to phys_addr() will cause a panic. This will happen if user doesn't pass any bootconfig to the kernel, because kernel will call xbc_destroy_all() after booting.

Quoted 6 lines
Fixes 40caa127f3c7 ("init: bootconfig: Remove all bootconfig data when the init memory is removed")

Should have replied here.

details:
https://lore.kernel.org/all/[email protected]/

Quoted 19 lines
---
 lib/bootconfig.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/bootconfig.c +2 -1
index f8419cff1147..4f8849706ef6 100644
@@ -792,7 +792,8 @@ void __init xbc_destroy_all(void)
 	xbc_data = NULL;
 	xbc_data_size = 0;
 	xbc_node_num = 0;
-	memblock_free(__pa(xbc_nodes), sizeof(struct xbc_node) * XBC_NODE_MAX);
+	if (xbc_nodes)
+		memblock_free(__pa(xbc_nodes), sizeof(struct xbc_node) * XBC_NODE_MAX);
 	xbc_nodes = NULL;
 	brace_index = 0;
 }
#3 lore raw

Re: [for-linus][PATCH 1/2] bootconfig: Fix to check the xbc_node is used before free it

Quoted 3 lines · On Tue, 14 Sep 2021 18:11:01 +0200 Vlastimil Babka <[email protected]> wrote:

Should have replied here.

Thanks, but I already sent a pull request to Linus.

Too late to add the tag. :-/

-- Steve

#4 lore raw

PATCH 2/2 bootconfig: Free copied bootconfig data after boot

In reply to Steven Rostedt

From: Masami Hiramatsu <[email protected]>

Free copied bootconfig data after booting kernel because that data will not be used anymore.

commit 40caa127f3c7 ("init: bootconfig: Remove all bootconfig data when the init memory is removed") freed the bootconfig xbc_node array after booting kernel, but forgot to free the bootconfig data itself. This fixes that to free the bootconfig data too.

This also frees the bootconfig data if the bootconfig data parsing failed.

Fixes 40caa127f3c7 ("init: bootconfig: Remove all bootconfig data when the init memory is removed")
---
 init/main.c | 8 ++++++++
 1 file changed, 8 insertions(+)
init/main.c +8 -0
index d08caed17c7f..ddbcb372225a 100644
@@ -319,6 +319,8 @@ static void * __init get_boot_config_from_initrd(u32 *_size, u32 *_csum)
 #ifdef CONFIG_BOOT_CONFIG
 
 static char xbc_namebuf[XBC_KEYLEN_MAX] __initdata;
+static void *init_xbc_data_copy __initdata;
+static phys_addr_t init_xbc_data_size __initdata;
 
 #define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0)
 
@@ -458,18 +460,24 @@ static void __init setup_boot_config(void)
 		else
 			pr_err("Failed to parse bootconfig: %s at %d.\n",
 				msg, pos);
+		memblock_free(__pa(copy), size + 1);
 	} else {
 		pr_info("Load bootconfig: %d bytes %d nodes\n", size, ret);
 		/* keys starting with "kernel." are passed via cmdline */
 		extra_command_line = xbc_make_cmdline("kernel");
 		/* Also, "init." keys are init arguments */
 		extra_init_args = xbc_make_cmdline("init");
+		init_xbc_data_copy = copy;
+		init_xbc_data_size = size + 1;
 	}
 	return;
 }
 
 static void __init exit_boot_config(void)
 {
+	if (!init_xbc_data_copy)
+		return;
+	memblock_free(__pa(init_xbc_data_copy), init_xbc_data_size);
 	xbc_destroy_all();
 }
 
Signature · 1 line
2.32.0