Skip to contentSkip to conversation

Small improvements to reserve_mem, take 2

PATCH Superseded Guilherme G. Piccoli 3 participants 9 messages Mar 4 – Mar 14

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover Small improvements to reserve_mem, take 2

Hi folks, this is the 2nd version of the patchset, V1 is
here: https://lore.kernel.org/r/[email protected]/

So, quick summary: we are adding
(a) Error messages if "reserve_mem=" fails;

(b) A debugfs file in case "reserve_mem=" succeeds, showing name and size of the allocation.

Use case of (a) is kinda obvious - we want to know if there are failures. Use case for (b) are users or tools like kdumpst[0] that might want to check if their reserve_mem allocation(s) succeeded in order to load ramoops with proper parameters, using the reserved areas.

Thanks a lot Mike for the great review so far! Questions or comments are welcome,

Guilherme G. Piccoli (2):
mm/memblock: Print out errors on reserve_mem parser mm/memblock: Add reserve_mem debugfs info

 mm/memblock.c | 65 ++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 54 insertions(+), 11 deletions(-)

[0] https://aur.archlinux.org/packages/kdumpst

Signature · 1 line
2.50.1
#1 lore raw

PATCH v2 1/2 mm/memblock: Print out errors on reserve_mem parser

The parsing of kernel parameter "reserve_mem=" is subject to multiple failures, like duplicate naming, malformed expression or even lack of available memory. Right now, all of these fail silently. Let's add some messages so the kernel log can provide useful information in case of failures.

Cc Andrew Morton <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Steven Rostedt <[email protected]>

---

V2: no changes.

 mm/memblock.c | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)
mm/memblock.c +19 -9
index b3ddfdec7a80..2d2646f7a120 100644
@@ -2642,23 +2642,25 @@ static int __init reserve_mem(char *p)
 	int len;
 
 	if (!p)
-		return -EINVAL;
+		goto err_param;
 
 	/* Check if there's room for more reserved memory */
-	if (reserved_mem_count >= RESERVE_MEM_MAX_ENTRIES)
+	if (reserved_mem_count >= RESERVE_MEM_MAX_ENTRIES) {
+		pr_err("reserve_mem: no more room for reserved memory\n");
 		return -EBUSY;
+	}
 
 	oldp = p;
 	size = memparse(p, &p);
 	if (!size || p == oldp)
-		return -EINVAL;
+		goto err_param;
 
 	if (*p != ':')
-		return -EINVAL;
+		goto err_param;
 
 	align = memparse(p+1, &p);
 	if (*p != ':')
-		return -EINVAL;
+		goto err_param;
 
 	/*
 	 * memblock_phys_alloc() doesn't like a zero size align,
@@ -2672,7 +2674,7 @@ static int __init reserve_mem(char *p)
 
 	/* name needs to have length but not too big */
 	if (!len || len >= RESERVE_MEM_NAME_SIZE)
-		return -EINVAL;
+		goto err_param;
 
 	/* Make sure that name has text */
 	for (p = name; *p; p++) {
@@ -2680,11 +2682,13 @@ static int __init reserve_mem(char *p)
 			break;
 	}
 	if (!*p)
-		return -EINVAL;
+		goto err_param;
 
 	/* Make sure the name is not already used */
-	if (reserve_mem_find_by_name(name, &start, &tmp))
+	if (reserve_mem_find_by_name(name, &start, &tmp)) {
+		pr_err("reserve_mem: name \"%s\" was already used\n", name);
 		return -EBUSY;
+	}
 
 	/* Pick previous allocations up from KHO if available */
 	if (reserve_mem_kho_revive(name, size, align))
@@ -2692,12 +2696,18 @@ static int __init reserve_mem(char *p)
 
 	/* TODO: Allocation must be outside of scratch region */
 	start = memblock_phys_alloc(size, align);
-	if (!start)
+	if (!start) {
+		pr_err("reserve_mem: memblock allocation failed\n");
 		return -ENOMEM;
+	}
 
 	reserved_mem_add(start, size, name);
 
 	return 1;
+err_param:
+	pr_err("reserve_mem: empty or malformed parameter\n");
+	return -EINVAL;
+
 }
 __setup("reserve_mem=", reserve_mem);
 
Signature · 1 line
2.50.1
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH V2 1/2] mm/memblock: Print out errors on reserve_mem parser

Quoted 5 lines · On Wed, 4 Mar 2026 17:14:10 -0300 "Guilherme G. Piccoli"…

The parsing of kernel parameter "reserve_mem=" is subject to multiple failures, like duplicate naming, malformed expression or even lack of available memory. Right now, all of these fail silently. Let's add some messages so the kernel log can provide useful information in case of failures.

Makes sense to me.

Quoted 5 lines
Cc Andrew Morton <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Steven Rostedt <[email protected]>
Quoted 76 lines

---

V2: no changes.

 mm/memblock.c | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)
mm/memblock.c +14 -9
index b3ddfdec7a80..2d2646f7a120 100644
@@ -2642,23 +2642,25 @@ static int __init reserve_mem(char *p)
 	int len;
 
 	if (!p)
-		return -EINVAL;
+		goto err_param;
 
 	/* Check if there's room for more reserved memory */
-	if (reserved_mem_count >= RESERVE_MEM_MAX_ENTRIES)
+	if (reserved_mem_count >= RESERVE_MEM_MAX_ENTRIES) {
+		pr_err("reserve_mem: no more room for reserved memory\n");
 		return -EBUSY;
+	}
 
 	oldp = p;
 	size = memparse(p, &p);
 	if (!size || p == oldp)
-		return -EINVAL;
+		goto err_param;
 
 	if (*p != ':')
-		return -EINVAL;
+		goto err_param;
 
 	align = memparse(p+1, &p);
 	if (*p != ':')
-		return -EINVAL;
+		goto err_param;
 
 	/*
 	 * memblock_phys_alloc() doesn't like a zero size align,
@@ -2672,7 +2674,7 @@ static int __init reserve_mem(char *p)
 
 	/* name needs to have length but not too big */
 	if (!len || len >= RESERVE_MEM_NAME_SIZE)
-		return -EINVAL;
+		goto err_param;
 
 	/* Make sure that name has text */
 	for (p = name; *p; p++) {
@@ -2680,11 +2682,13 @@ static int __init reserve_mem(char *p)
 			break;
 	}
 	if (!*p)
-		return -EINVAL;
+		goto err_param;
 
 	/* Make sure the name is not already used */
-	if (reserve_mem_find_by_name(name, &start, &tmp))
+	if (reserve_mem_find_by_name(name, &start, &tmp)) {
+		pr_err("reserve_mem: name \"%s\" was already used\n", name);
 		return -EBUSY;
+	}
 
 	/* Pick previous allocations up from KHO if available */
 	if (reserve_mem_kho_revive(name, size, align))
@@ -2692,12 +2696,18 @@ static int __init reserve_mem(char *p)
 
 	/* TODO: Allocation must be outside of scratch region */
 	start = memblock_phys_alloc(size, align);
-	if (!start)
+	if (!start) {
+		pr_err("reserve_mem: memblock allocation failed\n");

mm/memblock.c static int __init reserve_mem(char *p) · line 2710

Quoted 10 lines
mm/memblock.c +5 -0
 		return -ENOMEM;
+	}
 
 	reserved_mem_add(start, size, name);
 
 	return 1;
+err_param:
+	pr_err("reserve_mem: empty or malformed parameter\n");
+	return -EINVAL;
+

Nit. Above blank line seems not needed.

Quoted 5 lines
mm/memblock.c +0 -0
 }
 __setup("reserve_mem=", reserve_mem);
 
Signature · 1 line
2.50.1

Thanks,
SJ

#3 lore raw

Re: [PATCH V2 1/2] mm/memblock: Print out errors on reserve_mem parser

Quoted 11 lines · SeongJae Park
Quoted 5 lines · On Wed, 4 Mar 2026 17:14:10 -0300 "Guilherme G. Piccoli"…

The parsing of kernel parameter "reserve_mem=" is subject to multiple failures, like duplicate naming, malformed expression or even lack of available memory. Right now, all of these fail silently. Let's add some messages so the kernel log can provide useful information in case of failures.

Makes sense to me.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Cc Andrew Morton <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Steven Rostedt <[email protected]>

Hi SJ! thanks a lot for the review, I'll resubmit with your tag and fixing the unnecessary blank line.
Cheers,

Guilherme

#4 lore raw

PATCH v2 2/2 mm/memblock: Add reserve_mem debugfs info

In reply to Guilherme G. Piccoli

When using the "reserve_mem" parameter, users aim at having an area that (hopefully) persists across boots, so pstore infrastructure (like ramoops module) can make use of that to save oops/ftrace logs, for example.

There is no easy way to determine if this kernel parameter is properly set though; the kernel doesn't show information about this memory in memblock debugfs, neither in /proc/iomem nor dmesg. This is a relevant information for tools like kdumpst[0], to determine if it's reliable to use the reserved area as ramoops persistent storage; checking only /proc/cmdline is not sufficient as it doesn't tell if the reservation effectively succeeded or not.

Add here a new file under memblock debugfs showing properly set memory reservations, with name and size as passed to "reserve_mem". Notice that if no "reserve_mem=" is passed on command-line or if the reservation attempts fail, the file is not created.

[0] https://aur.archlinux.org/packages/kdumpst

Cc Andrew Morton <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Steven Rostedt <[email protected]>

---

Thanks a lot for the suggestions Mike! I'm not sure if you would prefer a Co-Developed-by or Suggested-by instead of CC, you helped a lot improving the code. Lemme know and either I can re-submit (with potential other changes) or even, you can change while merging.

V2: (all suggestions by Mike Rapoport)

- Commit message (showing use case);
- Drop ifdef on include "string_helpers.h";
- Don't show the address of reserve_mem, only name and size;
- Fixed flag names inside ARCH_KEEP_MEMBLOCK ifdef;
- Make use of its own show_attribute instead of refactoring the memblock one;
- Use sizeof() instead of magical numbers for the size;
- Don't show memblock directory if no reserve_mem succeeded and ARCH_KEEP_MEMBLOCK isn't defined (keeping current behavior).

 mm/memblock.c | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)
mm/memblock.c +35 -2
index 2d2646f7a120..d816796ab919 100644
@@ -17,6 +17,7 @@
 #include <linux/seq_file.h>
 #include <linux/memblock.h>
 #include <linux/mutex.h>
+#include <linux/string_helpers.h>
 
 #ifdef CONFIG_KEXEC_HANDOVER
 #include <linux/libfdt.h>
@@ -2711,7 +2712,8 @@ static int __init reserve_mem(char *p)
 }
 __setup("reserve_mem=", reserve_mem);
 
-#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
+#ifdef CONFIG_DEBUG_FS
+#ifdef CONFIG_ARCH_KEEP_MEMBLOCK
 static const char * const flagname[] = {
 	[ilog2(MEMBLOCK_HOTPLUG)] = "HOTPLUG",
 	[ilog2(MEMBLOCK_MIRROR)] = "MIRROR",
@@ -2758,10 +2760,40 @@ static int memblock_debug_show(struct seq_file *m, void *private)
 }
 DEFINE_SHOW_ATTRIBUTE(memblock_debug);
 
+#endif /* CONFIG_ARCH_KEEP_MEMBLOCK */
+
+static int memblock_reserve_mem_show(struct seq_file *m, void *private)
+{
+	struct reserve_mem_table *map;
+	char txtsz[16];
+
+	for (int i = 0; i < reserved_mem_count; i++) {
+		map = &reserved_mem_table[i];
+		if (!map->size)
+			continue;
+
+		memset(txtsz, 0, sizeof(txtsz));
+		string_get_size(map->size, 1, STRING_UNITS_2, txtsz, sizeof(txtsz));
+		seq_printf(m, "%s\t\t(%s)\n", map->name, txtsz);
+	}
+
+	return 0;
+}
+DEFINE_SHOW_ATTRIBUTE(memblock_reserve_mem);
+
 static int __init memblock_init_debugfs(void)
 {
-	struct dentry *root = debugfs_create_dir("memblock", NULL);
+	struct dentry *root;
 
+	if (!(IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK) || reserved_mem_count))
+		return 0;
+
+	root = debugfs_create_dir("memblock", NULL);
+
+	if (reserved_mem_count)
+		debugfs_create_file("reserve_mem_param", 0444, root, NULL,
+				    &memblock_reserve_mem_fops);
+#ifdef CONFIG_ARCH_KEEP_MEMBLOCK
 	debugfs_create_file("memory", 0444, root,
 			    &memblock.memory, &memblock_debug_fops);
 	debugfs_create_file("reserved", 0444, root,
@@ -2771,6 +2803,7 @@ static int __init memblock_init_debugfs(void)
 			    &memblock_debug_fops);
 #endif
 
+#endif /* CONFIG_ARCH_KEEP_MEMBLOCK */
 	return 0;
 }
 __initcall(memblock_init_debugfs);
Signature · 1 line
2.50.1
#5 lore raw

Re: [PATCH V2 2/2] mm/memblock: Add reserve_mem debugfs info

Quoted 5 lines · On Wed, 4 Mar 2026 17:14:11 -0300 "Guilherme G. Piccoli"…

When using the "reserve_mem" parameter, users aim at having an area that (hopefully) persists across boots, so pstore infrastructure (like ramoops module) can make use of that to save oops/ftrace logs, for example.

Quoted 7 lines

There is no easy way to determine if this kernel parameter is properly set though; the kernel doesn't show information about this memory in memblock debugfs, neither in /proc/iomem nor dmesg. This is a relevant information for tools like kdumpst[0], to determine if it's reliable to use the reserved area as ramoops persistent storage; checking only /proc/cmdline is not sufficient as it doesn't tell if the reservation effectively succeeded or not.

Asking out of curiosity. The previous patch has added the error log for failure cases. Could checking the kernel log to see if it was failed be an option?

I think this debugfs approach is easier to check for the user space, though. If that is the reason of this patch, adding the clarity would be nice for a theoretical case that debugfs cannot be mounted.

Quoted 80 lines

Add here a new file under memblock debugfs showing properly set memory reservations, with name and size as passed to "reserve_mem". Notice that if no "reserve_mem=" is passed on command-line or if the reservation attempts fail, the file is not created.

[0] https://aur.archlinux.org/packages/kdumpst

Cc Andrew Morton <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Steven Rostedt <[email protected]>

---

Thanks a lot for the suggestions Mike! I'm not sure if you would prefer a Co-Developed-by or Suggested-by instead of CC, you helped a lot improving the code. Lemme know and either I can re-submit (with potential other changes) or even, you can change while merging.

V2: (all suggestions by Mike Rapoport)

- Commit message (showing use case);
- Drop ifdef on include "string_helpers.h";
- Don't show the address of reserve_mem, only name and size;
- Fixed flag names inside ARCH_KEEP_MEMBLOCK ifdef;
- Make use of its own show_attribute instead of refactoring the memblock one;
- Use sizeof() instead of magical numbers for the size;
- Don't show memblock directory if no reserve_mem succeeded and ARCH_KEEP_MEMBLOCK isn't defined (keeping current behavior).

 mm/memblock.c | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)
mm/memblock.c +21 -1
index 2d2646f7a120..d816796ab919 100644
@@ -17,6 +17,7 @@
 #include <linux/seq_file.h>
 #include <linux/memblock.h>
 #include <linux/mutex.h>
+#include <linux/string_helpers.h>
 
 #ifdef CONFIG_KEXEC_HANDOVER
 #include <linux/libfdt.h>
@@ -2711,7 +2712,8 @@ static int __init reserve_mem(char *p)
 }
 __setup("reserve_mem=", reserve_mem);
 
-#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
+#ifdef CONFIG_DEBUG_FS
+#ifdef CONFIG_ARCH_KEEP_MEMBLOCK
 static const char * const flagname[] = {
 	[ilog2(MEMBLOCK_HOTPLUG)] = "HOTPLUG",
 	[ilog2(MEMBLOCK_MIRROR)] = "MIRROR",
@@ -2758,10 +2760,40 @@ static int memblock_debug_show(struct seq_file *m, void *private)
 }
 DEFINE_SHOW_ATTRIBUTE(memblock_debug);
 
+#endif /* CONFIG_ARCH_KEEP_MEMBLOCK */
+
+static int memblock_reserve_mem_show(struct seq_file *m, void *private)
+{
+	struct reserve_mem_table *map;
+	char txtsz[16];
+
+	for (int i = 0; i < reserved_mem_count; i++) {
+		map = &reserved_mem_table[i];
+		if (!map->size)
+			continue;
+
+		memset(txtsz, 0, sizeof(txtsz));
+		string_get_size(map->size, 1, STRING_UNITS_2, txtsz, sizeof(txtsz));
+		seq_printf(m, "%s\t\t(%s)\n", map->name, txtsz);
+	}
+
+	return 0;

mm/memblock.c static int memblock_debug_show(struct seq_file *m, void *private) · line 2789

Quoted 10 lines
mm/memblock.c +6 -1
+}
+DEFINE_SHOW_ATTRIBUTE(memblock_reserve_mem);
+
 static int __init memblock_init_debugfs(void)
 {
-	struct dentry *root = debugfs_create_dir("memblock", NULL);
+	struct dentry *root;
 
+	if (!(IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK) || reserved_mem_count))
+		return 0;

One trivial comment. I'd slightly prefer having one less parentheses level as a tradeoff of having one more exclamation mark, e.g.,

    if (!IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK) && !reserved_mem_count)
Quoted 11 lines
mm/memblock.c +7 -0
+
+	root = debugfs_create_dir("memblock", NULL);
+
+	if (reserved_mem_count)
+		debugfs_create_file("reserve_mem_param", 0444, root, NULL,
+				    &memblock_reserve_mem_fops);
+#ifdef CONFIG_ARCH_KEEP_MEMBLOCK
 	debugfs_create_file("memory", 0444, root,
 			    &memblock.memory, &memblock_debug_fops);
 	debugfs_create_file("reserved", 0444, root,

mm/memblock.c static int __init memblock_init_debugfs(void) · line 2806

Quoted 4 lines
mm/memblock.c +1 -0
@@ -2771,6 +2803,7 @@ static int __init memblock_init_debugfs(void)
 			    &memblock_debug_fops);
 #endif
 
+#endif /* CONFIG_ARCH_KEEP_MEMBLOCK */

So, we get two level of nested ifdef... I'm wondering if returning earlier when CONFIG_ARCH_KEEP_MEMBLOCK is undefined is easier to read. E.g.,

	#ifndef CONFIG_ARCH_KEEP_MEMBLOCK
		return 0;
	#endif

		debugfs_create_file("memory", 0444, root,
	[...]

mm/memblock.c static int __init memblock_init_debugfs(void) · line 2807

Quoted 1 line
mm/memblock.c +0 -0
 	return 0;

Very trivial comment. Why don't you keep the original blank line above the return statemtnt?

Quoted 4 lines
mm/memblock.c +0 -0
 }
 __initcall(memblock_init_debugfs);
Signature · 1 line
2.50.1

Thanks,
SJ

#6 lore raw

Re: [PATCH V2 2/2] mm/memblock: Add reserve_mem debugfs info

Hi SJ and Mike, thanks for both your review! I'll respond the 2 messages here.

First regarding Mike's concern from the other email:

Quoted 2 lines
Quoted 1 line

+#include <linux/string_helpers.h>

This will break tests in tools/testing/memblock, please add a stub there.

Sure, will do it!

Quoted 9 lines · SeongJae Park

[...]

Quoted 7 lines

There is no easy way to determine if this kernel parameter is properly set though; the kernel doesn't show information about this memory in memblock debugfs, neither in /proc/iomem nor dmesg. This is a relevant information for tools like kdumpst[0], to determine if it's reliable to use the reserved area as ramoops persistent storage; checking only /proc/cmdline is not sufficient as it doesn't tell if the reservation effectively succeeded or not.

Quoted 8 lines

Asking out of curiosity. The previous patch has added the error log for failure cases. Could checking the kernel log to see if it was failed be an option?

I think this debugfs approach is easier to check for the user space, though. If that is the reason of this patch, adding the clarity would be nice for a theoretical case that debugfs cannot be mounted.

Exactly that! Not only debugfs way is more convenient for checking that, but it feels a bit weird for me to assume the reservation worked by checking for errors and if none, all fine. It's valid, but as a matter of taste, I prefer checking the file. And not only that: imagine we have more than one setting in the cmdline, for other usages like ftrace. Easier to have the debugfs showing the succeeding ones, by name and size, ready to be used heheh

Do you have a suggestion on how should I mention this in the commit message? Is that discussion enough maybe, for future reference? I feel mentioning that userspace will use the information from the file seems enough for a commit message; but I'm totally open for your suggestions on how to improve the wording here

Quoted 8 lines

mm/memblock.c static int memblock_debug_show(struct seq_file *m, void *private) · line 2789

Quoted 2 lines
mm/memblock.c +2 -0
+	if (!(IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK) || reserved_mem_count))
+		return 0;

One trivial comment. I'd slightly prefer having one less parentheses level as a tradeoff of having one more exclamation mark, e.g.,

    if (!IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK) && !reserved_mem_count)

Good! Unless Mike is against that, I can easily change it.

Quoted 11 lines

So, we get two level of nested ifdef... I'm wondering if returning earlier when CONFIG_ARCH_KEEP_MEMBLOCK is undefined is easier to read. E.g.,

	#ifndef CONFIG_ARCH_KEEP_MEMBLOCK
		return 0;
	#endif

		debugfs_create_file("memory", 0444, root,
	[...]
Quoted 5 lines
Quoted 1 line

return 0;

Very trivial comment. Why don't you keep the original blank line above the return statemtnt?

About the ifdefs, as per Mike's comment, I'll rework it in a helper. And the blank like, I have no answer heh
Likely a braino? I can certainly keep it.

Thanks again you two for the suggestions,

Guilherme

#7 lore raw

Re: [PATCH V2 2/2] mm/memblock: Add reserve_mem debugfs info

Quoted 29 lines · On Fri, 13 Mar 2026 18:09:10 -0300 "Guilherme G. Piccoli"…

Hi SJ and Mike, thanks for both your review! I'll respond the 2 messages here.

First regarding Mike's concern from the other email:

Quoted 2 lines
Quoted 1 line

+#include <linux/string_helpers.h>

This will break tests in tools/testing/memblock, please add a stub there.

Sure, will do it!

Quoted 9 lines · SeongJae Park

[...]

Quoted 7 lines

There is no easy way to determine if this kernel parameter is properly set though; the kernel doesn't show information about this memory in memblock debugfs, neither in /proc/iomem nor dmesg. This is a relevant information for tools like kdumpst[0], to determine if it's reliable to use the reserved area as ramoops persistent storage; checking only /proc/cmdline is not sufficient as it doesn't tell if the reservation effectively succeeded or not.

Quoted 8 lines

Asking out of curiosity. The previous patch has added the error log for failure cases. Could checking the kernel log to see if it was failed be an option?

I think this debugfs approach is easier to check for the user space, though. If that is the reason of this patch, adding the clarity would be nice for a theoretical case that debugfs cannot be mounted.

Quoted 7 lines

Exactly that! Not only debugfs way is more convenient for checking that, but it feels a bit weird for me to assume the reservation worked by checking for errors and if none, all fine. It's valid, but as a matter of taste, I prefer checking the file. And not only that: imagine we have more than one setting in the cmdline, for other usages like ftrace. Easier to have the debugfs showing the succeeding ones, by name and size, ready to be used heheh

Makes sense to me, thank you for kindly clarifying this! :)

Quoted 6 lines

Do you have a suggestion on how should I mention this in the commit message? Is that discussion enough maybe, for future reference? I feel mentioning that userspace will use the information from the file seems enough for a commit message; but I'm totally open for your suggestions on how to improve the wording here

The above clarification on this thread is good enough for me.

Quoted 27 lines
Quoted 10 lines

mm/memblock.c static int memblock_debug_show(struct seq_file *m, void *private) · line 2789

Quoted 4 lines
mm/memblock.c +2 -0
+	if (!(IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK) || reserved_mem_count))
+		return 0;

One trivial comment. I'd slightly prefer having one less parentheses level as a tradeoff of having one more exclamation mark, e.g.,

    if (!IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK) && !reserved_mem_count)

Good! Unless Mike is against that, I can easily change it.

Quoted 13 lines

So, we get two level of nested ifdef... I'm wondering if returning earlier when CONFIG_ARCH_KEEP_MEMBLOCK is undefined is easier to read. E.g.,

	#ifndef CONFIG_ARCH_KEEP_MEMBLOCK
		return 0;
	#endif

		debugfs_create_file("memory", 0444, root,
	[...]
Quoted 1 line

return 0;

Quoted 7 lines
Quoted 3 lines

Very trivial comment. Why don't you keep the original blank line above the return statemtnt?

About the ifdefs, as per Mike's comment, I'll rework it in a helper. And the blank like, I have no answer heh
Likely a braino? I can certainly keep it.

Sounds good!

Quoted 2 lines

Thanks again you two for the suggestions,

:)

Thanks,
SJ

[...]

maintainer
#8 lore raw

Re: [PATCH V2 2/2] mm/memblock: Add reserve_mem debugfs info

In reply to Guilherme G. Piccoli

Hi Guilherme,

Quoted 53 lines · Guilherme G. Piccoli

When using the "reserve_mem" parameter, users aim at having an area that (hopefully) persists across boots, so pstore infrastructure (like ramoops module) can make use of that to save oops/ftrace logs, for example.

There is no easy way to determine if this kernel parameter is properly set though; the kernel doesn't show information about this memory in memblock debugfs, neither in /proc/iomem nor dmesg. This is a relevant information for tools like kdumpst[0], to determine if it's reliable to use the reserved area as ramoops persistent storage; checking only /proc/cmdline is not sufficient as it doesn't tell if the reservation effectively succeeded or not.

Add here a new file under memblock debugfs showing properly set memory reservations, with name and size as passed to "reserve_mem". Notice that if no "reserve_mem=" is passed on command-line or if the reservation attempts fail, the file is not created.

[0] https://aur.archlinux.org/packages/kdumpst

Cc Andrew Morton <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Steven Rostedt <[email protected]>

---

Thanks a lot for the suggestions Mike! I'm not sure if you would prefer a Co-Developed-by or Suggested-by instead of CC, you helped a lot improving the code. Lemme know and either I can re-submit (with potential other changes) or even, you can change while merging.

V2: (all suggestions by Mike Rapoport)

- Commit message (showing use case);
- Drop ifdef on include "string_helpers.h";
- Don't show the address of reserve_mem, only name and size;
- Fixed flag names inside ARCH_KEEP_MEMBLOCK ifdef;
- Make use of its own show_attribute instead of refactoring the memblock one;
- Use sizeof() instead of magical numbers for the size;
- Don't show memblock directory if no reserve_mem succeeded and ARCH_KEEP_MEMBLOCK isn't defined (keeping current behavior).

 mm/memblock.c | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)
mm/memblock.c +0 -0
index 2d2646f7a120..d816796ab919 100644

mm/memblock.c line 20

Quoted 4 lines
mm/memblock.c +1 -0
@@ -17,6 +17,7 @@
 #include <linux/seq_file.h>
 #include <linux/memblock.h>
 #include <linux/mutex.h>
+#include <linux/string_helpers.h>

This will break tests in tools/testing/memblock, please add a stub there.

Quoted 55 lines
mm/memblock.c +33 -2
 #ifdef CONFIG_KEXEC_HANDOVER
 #include <linux/libfdt.h>
@@ -2711,7 +2712,8 @@ static int __init reserve_mem(char *p)
 }
 __setup("reserve_mem=", reserve_mem);
 
-#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
+#ifdef CONFIG_DEBUG_FS
+#ifdef CONFIG_ARCH_KEEP_MEMBLOCK
 static const char * const flagname[] = {
 	[ilog2(MEMBLOCK_HOTPLUG)] = "HOTPLUG",
 	[ilog2(MEMBLOCK_MIRROR)] = "MIRROR",
@@ -2758,10 +2760,40 @@ static int memblock_debug_show(struct seq_file *m, void *private)
 }
 DEFINE_SHOW_ATTRIBUTE(memblock_debug);
 
+#endif /* CONFIG_ARCH_KEEP_MEMBLOCK */
+
+static int memblock_reserve_mem_show(struct seq_file *m, void *private)
+{
+	struct reserve_mem_table *map;
+	char txtsz[16];
+
+	for (int i = 0; i < reserved_mem_count; i++) {
+		map = &reserved_mem_table[i];
+		if (!map->size)
+			continue;
+
+		memset(txtsz, 0, sizeof(txtsz));
+		string_get_size(map->size, 1, STRING_UNITS_2, txtsz, sizeof(txtsz));
+		seq_printf(m, "%s\t\t(%s)\n", map->name, txtsz);
+	}
+
+	return 0;
+}
+DEFINE_SHOW_ATTRIBUTE(memblock_reserve_mem);
+
 static int __init memblock_init_debugfs(void)
 {
-	struct dentry *root = debugfs_create_dir("memblock", NULL);
+	struct dentry *root;
 
+	if (!(IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK) || reserved_mem_count))
+		return 0;
+
+	root = debugfs_create_dir("memblock", NULL);
+
+	if (reserved_mem_count)
+		debugfs_create_file("reserve_mem_param", 0444, root, NULL,
+				    &memblock_reserve_mem_fops);
+#ifdef CONFIG_ARCH_KEEP_MEMBLOCK
 	debugfs_create_file("memory", 0444, root,
 			    &memblock.memory, &memblock_debug_fops);
 	debugfs_create_file("reserved", 0444, root,

mm/memblock.c static int __init memblock_init_debugfs(void) · line 2806

Quoted 4 lines
mm/memblock.c +1 -0
@@ -2771,6 +2803,7 @@ static int __init memblock_init_debugfs(void)
 			    &memblock_debug_fops);
 #endif
 
+#endif /* CONFIG_ARCH_KEEP_MEMBLOCK */

It becomes too #ifdef'y :(
Let's put the creation of these files into a helper and add an empty stub when ARCH_KEEP_MEMBLOCK=n.

Quoted 6 lines
mm/memblock.c +0 -0
 	return 0;
 }
 __initcall(memblock_init_debugfs);
Signature · 1 line
2.50.1
Signature · 2 lines
Sincerely yours,
Mike.