Skip to contentSkip to conversation

mm/memblock: Add "reserve_mem" to reserved named memory at boot up

PATCH Superseded Steven Rostedt 6 participants 18 messages Jun 6, 2024 – Jun 12, 2024

Subsystems (6)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

v2 cover mm/memblock: Add "reserve_mem" to reserved named memory at boot up

Reserve unspecified location of physical memory from kernel command line

Background:

In ChromeOS, we have 1 MB of pstore ramoops reserved so that we can extract dmesg output and some other information when a crash happens in the field. (This is only done when the user selects "Allow Google to collect data for
improving the system"). But there are cases when there's a bug that
requires more data to be retrieved to figure out what is happening. We would like to increase the pstore size, either temporarily, or maybe even permanently. The pstore on these devices are at a fixed location in RAM (as the RAM is not cleared on soft reboots nor crashes). The location is chosen by the BIOS (coreboot) and passed to the kernel via ACPI tables on x86. There's a driver that queries for this to initialize the pstore for ChromeOS:

See drivers/platform/chrome/chromeos_pstore.c

Problem:

The problem is that, even though there's a process to change the kernel on these systems, and is done regularly to install updates, the firmware is updated much less frequently. Choosing the place in RAM also takes special care, and may be in a different address for different boards. Updating the size via firmware is a large effort and not something that many are willing to do for a temporary pstore size change.

Requirement:

Need a way to reserve memory that will be at a consistent location for every boot, if the kernel and system are the same. Does not need to work if rebooting to a different kernel, or if the system can change the memory layout between boots.

The reserved memory can not be an hard coded address, as the same kernel / command line needs to run on several different machines. The picked memory reservation just needs to be the same for a given machine, but may be different for different machines.

Solution:

The solution I have come up with is to introduce a new "reserve_mem=" kernel command line. This parameter takes the following format:

reserve_mem=nn:align:label

Where nn is the size of memory to reserve, the align is the alignment of that memory, and label is the way for other sub-systems to find that memory. This way the kernel command line could have:

reserve_mem=12M:4096:oops ramoops.mem_name=oops

At boot up, the kernel will search for 12 megabytes in usable memory regions with an alignment of 4096. It will start at the highest regions and work its way down (for those old devices that want access to lower address DMA). When it finds a region, it will save it off in a small table and mark it with the "oops" label. Then the pstore ramoops sub-system could ask for that memory and location, and it will map itself there.

This prototype allows for 8 different mappings (which may be overkill, 4 is probably plenty) with 16 byte size to store the label.

I have tested this and it works for us to solve the above problem. We can update the kernel and command line and increase the size of pstore without needing to update the firmware, or knowing every memory layout of each board. I only tested this locally, it has not been tested in the field.

Changes since the v1: https://lore.kernel.org/all/[email protected]/

- Updated the change log of the first patch as well as added an entry into kernel-parameters.txt about how reserve_mem is for soft reboots and may not be reliable.

git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git
reserve-mem

Head SHA1: 94c7d2d9093e9a7a899215c65adf28180d44a247

Steven Rostedt (Google) (2):
mm/memblock: Add "reserve_mem" to reserved named memory at boot up pstore/ramoops: Add ramoops.mem_name= command line option

----

 Documentation/admin-guide/kernel-parameters.txt | 20 +++++
 fs/pstore/ram.c                                 | 15 ++++
 include/linux/mm.h                              |  2 +
 mm/memblock.c                                   | 97 +++++++++++++++++++++++++
 4 files changed, 134 insertions(+)
author maintainer
#1 lore raw

PATCH v2 1/2 mm/memblock: Add "reserve_mem" to reserved named memory at boot up

From: "Steven Rostedt (Google)" <[email protected]>

In order to allow for requesting a memory region that can be used for things like pstore on multiple machines where the memory layout is not the same, add a new option to the kernel command line called "reserve_mem".

The format is: reserve_mem=nn:align:name

Where it will find nn amount of memory at the given alignment of align. The name field is to allow another subsystem to retrieve where the memory was found. For example:

reserve_mem=12M:4096:oops ramoops.mem_name=oops

Where ramoops.mem_name will tell ramoops that memory was reserved for it via the reserve_mem option and it can find it by calling:

  if (reserve_mem_find_by_name("oops", &start, &size)) {
	// start holds the start address and size holds the size given

This is typically used for systems that do not wipe the RAM, and this command line will try to reserve the same physical memory on soft reboots. Note, it is not guaranteed to be the same location. For example, if KASLR places the kernel at the location of where the RAM reservation was from a previous boot, the new reservation will be at a different location. Any subsystem using this feature must add a way to verify that the contents of the physical memory is from a previous boot, as there may be cases where the memory will not be located at the same location.

Not all systems may work either. There could be bit flips if the reboot goes through the BIOS. Using kexec to reboot the machine is likely to have better results in such cases.

---
 .../admin-guide/kernel-parameters.txt         | 20 ++++
 include/linux/mm.h                            |  2 +
 mm/memblock.c                                 | 97 +++++++++++++++++++
 3 files changed, 119 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +20 -0
index b600df82669d..4b2f7fb8de66 100644
@@ -5710,6 +5710,26 @@
 			them.  If <base> is less than 0x10000, the region
 			is assumed to be I/O ports; otherwise it is memory.
 
+	reserve_mem=	[RAM]
+			Format: nn[KNG]:<align>:<label>
+			Reserve physical memory and label it with a name that
+			other subsystems can use to access it. This is typically
+			used for systems that do not wipe the RAM, and this command
+			line will try to reserve the same physical memory on
+			soft reboots. Note, it is not guaranteed to be the same
+			location. For example, if KASLR places the kernel at the
+			location of where the RAM reservation was from a previous
+			boot, the new reservation will be at a different location.
+			Any subsystem using this feature must add a way to verify
+			that the contents of the physical memory is from a previous
+			boot, as there may be cases where the memory will not be
+			located at the same location.
+
+			The format is size:align:label for example, to request
+			12 megabytes of 4096 alignment for ramoops:
+
+			reserver_mem=12M:4096:oops ramoops.mem_name=oops
+
 	reservetop=	[X86-32,EARLY]
 			Format: nn[KMG]
 			Reserves a hole at the top of the kernel virtual
include/linux/mm.h +2 -0
index 9849dfda44d4..b4455cc02f2c 100644
@@ -4263,4 +4263,6 @@ static inline bool pfn_is_unaccepted_memory(unsigned long pfn)
 void vma_pgtable_walk_begin(struct vm_area_struct *vma);
 void vma_pgtable_walk_end(struct vm_area_struct *vma);
 
+int reserve_mem_find_by_name(const char *name, unsigned long *start, unsigned long *size);
+
 #endif /* _LINUX_MM_H */
mm/memblock.c +97 -0

103 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Hi Steve, thanks for the patch! Some suggestions/fixes below, inline.

Documentation/admin-guide/kernel-parameters.txt line 5731

Quoted 6 lines · Steven Rostedt

[...]

Documentation/admin-guide/kernel-parameters.txt +5 -0
+
+			The format is size:align:label for example, to request
+			12 megabytes of 4096 alignment for ramoops:
+
+			reserver_mem=12M:4096:oops ramoops.mem_name=oops

s/reserver/reserve

Quoted 16 lines

[...]
+ * reserve_mem_find_by_name - Find reserved memory region with a given name
+ * @name: The name that is attached to a reserved memory region
+ * @start: If found, holds the start address
+ * @size: If found, holds the size of the address.
+ *
+ * Returns: 1 if found or 0 if not found.
+ */

mm/memblock.c +8 -0
+int reserve_mem_find_by_name(const char *name, unsigned long *start, unsigned long *size)
+{
+	struct reserve_mem_table *map;
+	int i;
+
+	for (i = 0; i < reserved_mem_count; i++) {
+		map = &reserved_mem_table[i];
+		if (!map->size)

mm/memblock.c void __init memblock_free_all(void) · line 2302

Quoted 10 lines
mm/memblock.c +10 -0
+			continue;
+		if (strcmp(name, map->name) == 0) {
+			*start = map->start;
+			*size = map->size;
+			return 1;
+		}
+	}
+	return 0;
+}
+

An EXPORT_SYMBOL_GPL(reserve_mem_find_by_name) is needed here, or else ramoops fails to build as module - at least it worked with this exporting in my build of 6.10.0-rc2 =)

Cheers,

Guilherme

#3 lore raw
In reply to Steven Rostedt
Quoted 90 lines · Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

In order to allow for requesting a memory region that can be used for things like pstore on multiple machines where the memory layout is not the same, add a new option to the kernel command line called "reserve_mem".

The format is: reserve_mem=nn:align:name

Where it will find nn amount of memory at the given alignment of align. The name field is to allow another subsystem to retrieve where the memory was found. For example:

reserve_mem=12M:4096:oops ramoops.mem_name=oops

Where ramoops.mem_name will tell ramoops that memory was reserved for it via the reserve_mem option and it can find it by calling:

 if (reserve_mem_find_by_name("oops", &start, &size)) {
	// start holds the start address and size holds the size given

This is typically used for systems that do not wipe the RAM, and this command line will try to reserve the same physical memory on soft reboots. Note, it is not guaranteed to be the same location. For example, if KASLR places the kernel at the location of where the RAM reservation was from a previous boot, the new reservation will be at a different location. Any subsystem using this feature must add a way to verify that the contents of the physical memory is from a previous boot, as there may be cases where the memory will not be located at the same location.

Not all systems may work either. There could be bit flips if the reboot goes through the BIOS. Using kexec to reboot the machine is likely to have better results in such cases.

---

.../admin-guide/kernel-parameters.txt         | 20 ++++
include/linux/mm.h                            |  2 +
mm/memblock.c                                 | 97 +++++++++++++++++++
3 files changed, 119 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +20 -0
index b600df82669d..4b2f7fb8de66 100644
@@ -5710,6 +5710,26 @@
 			them.  If <base> is less than 0x10000, the region
 			is assumed to be I/O ports; otherwise it is memory.
 
+	reserve_mem=	[RAM]
+			Format: nn[KNG]:<align>:<label>
+			Reserve physical memory and label it with a name that
+			other subsystems can use to access it. This is typically
+			used for systems that do not wipe the RAM, and this command
+			line will try to reserve the same physical memory on
+			soft reboots. Note, it is not guaranteed to be the same
+			location. For example, if KASLR places the kernel at the
+			location of where the RAM reservation was from a previous
+			boot, the new reservation will be at a different location.
+			Any subsystem using this feature must add a way to verify
+			that the contents of the physical memory is from a previous
+			boot, as there may be cases where the memory will not be
+			located at the same location.
+
+			The format is size:align:label for example, to request
+			12 megabytes of 4096 alignment for ramoops:
+
+			reserver_mem=12M:4096:oops ramoops.mem_name=oops
+
 	reservetop=	[X86-32,EARLY]
 			Format: nn[KMG]
 			Reserves a hole at the top of the kernel virtual
include/linux/mm.h +2 -0
index 9849dfda44d4..b4455cc02f2c 100644
@@ -4263,4 +4263,6 @@ static inline bool pfn_is_unaccepted_memory(unsigned long pfn)
 void vma_pgtable_walk_begin(struct vm_area_struct *vma);
 void vma_pgtable_walk_end(struct vm_area_struct *vma);
 
+int reserve_mem_find_by_name(const char *name, unsigned long *start, unsigned long *size);
+
 #endif /* _LINUX_MM_H */
mm/memblock.c +0 -0
index d09136e040d3..a8bf0ee9e2b4 100644

mm/memblock.c void __init memblock_free_all(void) · line 2249

Quoted 6 lines
mm/memblock.c +3 -0
@@ -2244,6 +2244,103 @@ void __init memblock_free_all(void)
 	totalram_pages_add(pages);
 }
 
+/* Keep a table to reserve named memory */
+#define RESERVE_MEM_MAX_ENTRIES		8
+#define RESERVE_MEM_NAME_SIZE		16
                                        ^

Suggest to align with previous line.

mm/memblock.c void __init memblock_free_all(void) · line 2253

Quoted 4 lines
mm/memblock.c +4 -0
+struct reserve_mem_table {
+	char			name[RESERVE_MEM_NAME_SIZE];
+	unsigned long		start;
+	unsigned long		size;

phys_addr_t looks more precise?

mm/memblock.c void __init memblock_free_all(void) · line 2256

Quoted 3 lines
mm/memblock.c +3 -0
+};
+static struct reserve_mem_table reserved_mem_table[RESERVE_MEM_MAX_ENTRIES];
+static int reserved_mem_count;

Seems no matter we use this feature or not, these memory would be occupied?

mm/memblock.c void __init memblock_free_all(void) · line 2268

Quoted 12 lines
mm/memblock.c +12 -0
+
+/* Add wildcard region with a lookup name */
+static int __init reserved_mem_add(unsigned long start, unsigned long size,
+				   const char *name)
+{
+	struct reserve_mem_table *map;
+
+	if (!name || !name[0] || strlen(name) >= RESERVE_MEM_NAME_SIZE)
+		return -EINVAL;
+
+	if (reserved_mem_count >= RESERVE_MEM_MAX_ENTRIES)
+		return -1;

return ENOSPC? Not good at it, but a raw value maybe not a good practice.

Also, we'd better do this check before allocation.

Quoted 26 lines
mm/memblock.c +26 -0
+
+	map = &reserved_mem_table[reserved_mem_count++];
+	map->start = start;
+	map->size = size;
+	strscpy(map->name, name);
+	return 0;
+}
+
+/**
+ * reserve_mem_find_by_name - Find reserved memory region with a given name
+ * @name: The name that is attached to a reserved memory region
+ * @start: If found, holds the start address
+ * @size: If found, holds the size of the address.
+ *
+ * Returns: 1 if found or 0 if not found.
+ */
+int reserve_mem_find_by_name(const char *name, unsigned long *start, unsigned long *size)
+{
+	struct reserve_mem_table *map;
+	int i;
+
+	for (i = 0; i < reserved_mem_count; i++) {
+		map = &reserved_mem_table[i];
+		if (!map->size)
+			continue;
+		if (strcmp(name, map->name) == 0) {

mm/memblock.c void __init memblock_free_all(void) · line 2304

Quoted 10 lines
mm/memblock.c +10 -0
+			*start = map->start;
+			*size = map->size;
+			return 1;
+		}
+	}
+	return 0;
+}
+
+/*
+ * Parse early_reserve_mem=nn:align:name

early_reserve_mem or reserve_mem ?

Quoted 12 lines
mm/memblock.c +12 -0
+ */
+static int __init reserve_mem(char *p)
+{
+	phys_addr_t start, size, align;
+	char *oldp;
+	int err;
+
+	if (!p)
+		return -EINVAL;
+
+	oldp = p;
+	size = memparse(p, &p);

mm/memblock.c void __init memblock_free_all(void) · line 2326

Quoted 10 lines
mm/memblock.c +10 -0
+	if (p == oldp)
+		return -EINVAL;
+
+	if (*p != ':')
+		return -EINVAL;
+
+	align = memparse(p+1, &p);
+	if (*p != ':')
+		return -EINVAL;
+

Better to check if the name is valid here.

Make sure command line parameters are valid before doing the allocation.

Quoted 4 lines
mm/memblock.c +4 -0
+	start = memblock_phys_alloc(size, align);
+	if (!start)
+		return -ENOMEM;
+

mm/memblock.c void __init memblock_free_all(void) · line 2340

Quoted 10 lines
mm/memblock.c +10 -0
+	p++;
+	err = reserved_mem_add(start, size, p);
+	if (err) {
+		memblock_phys_free(start, size);
+		return err;
+	}
+
+	p += strlen(p);
+
+	return *p == '\0' ? 0: -EINVAL;

We won't free the memory if return -EINVAL?

Quoted 10 lines
mm/memblock.c +3 -0
+}
+__setup("reserve_mem=", reserve_mem);
+
 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
 static const char * const flagname[] = {
 	[ilog2(MEMBLOCK_HOTPLUG)] = "HOTPLUG",
Signature · 1 line
2.43.0
Signature · 2 lines
Wei Yang
Help you, Help me
#4 lore raw

On 6/11/24 07:40, Wei Yang wrote:
[ ... ]

Quoted 6 lines

mm/memblock.c void __init memblock_free_all(void) · line 2249

Quoted 3 lines
mm/memblock.c +3 -0
+/* Keep a table to reserve named memory */
+#define RESERVE_MEM_MAX_ENTRIES		8
+#define RESERVE_MEM_NAME_SIZE		16
                                         ^

Suggest to align with previous line.

It _is_ aligned. It just looks unaligned because of the "+" at the beginning of the patch.

Guenter

author maintainer
#5 lore raw
In reply to Wei Yang

On Tue, 11 Jun 2024 14:40:29 +0000
Wei Yang <[email protected]> wrote:

Missed this just before sending out v3 :-p

Quoted 8 lines
Quoted 8 lines
mm/memblock.c +0 -0
index d09136e040d3..a8bf0ee9e2b4 100644
@@ -2244,6 +2244,103 @@ void __init memblock_free_all(void)
 	totalram_pages_add(pages);
 }
 
Quoted 5 lines

mm/memblock.c void __init memblock_free_all(void) · line 2249

Quoted 3 lines
mm/memblock.c +3 -0
+/* Keep a table to reserve named memory */
+#define RESERVE_MEM_MAX_ENTRIES		8
+#define RESERVE_MEM_NAME_SIZE		16  
                                        ^

Suggest to align with previous line.

It is. But because the patch adds a "+", it pushed the "8" out another tab.

Quoted 7 lines

mm/memblock.c void __init memblock_free_all(void) · line 2253

Quoted 5 lines
mm/memblock.c +4 -0
+struct reserve_mem_table {
+	char			name[RESERVE_MEM_NAME_SIZE];
+	unsigned long		start;
+	unsigned long		size;  

phys_addr_t looks more precise?

For just the start variable, correct? I'm OK with updating that.

Quoted 6 lines

mm/memblock.c void __init memblock_free_all(void) · line 2256

Quoted 4 lines
mm/memblock.c +3 -0
+};
+static struct reserve_mem_table reserved_mem_table[RESERVE_MEM_MAX_ENTRIES];
+static int reserved_mem_count;  

Seems no matter we use this feature or not, these memory would be occupied?

Yes, because allocation may screw it up as well. I could add a CONFIG around it, so that those that do not want this could configure it out. But since it's just a total of (16 + 8 + 8) * 8 = 256 bytes, I'm not sure it's much of a worry to add the complexities to save that much space. As the code to save it may likely be bigger.

Quoted 15 lines

mm/memblock.c void __init memblock_free_all(void) · line 2268

Quoted 13 lines
mm/memblock.c +12 -0
+
+/* Add wildcard region with a lookup name */
+static int __init reserved_mem_add(unsigned long start, unsigned long size,
+				   const char *name)
+{
+	struct reserve_mem_table *map;
+
+	if (!name || !name[0] || strlen(name) >= RESERVE_MEM_NAME_SIZE)
+		return -EINVAL;
+
+	if (reserved_mem_count >= RESERVE_MEM_MAX_ENTRIES)
+		return -1;  

return ENOSPC? Not good at it, but a raw value maybe not a good practice.

This is what gets returned by the command line parser. It only cares if it is zero or not.

Quoted 2 lines

Also, we'd better do this check before allocation.

What allocation?

Quoted 31 lines
Quoted 31 lines
mm/memblock.c +30 -0
+
+	map = &reserved_mem_table[reserved_mem_count++];
+	map->start = start;
+	map->size = size;
+	strscpy(map->name, name);
+	return 0;
+}
+
+/**
+ * reserve_mem_find_by_name - Find reserved memory region with a given name
+ * @name: The name that is attached to a reserved memory region
+ * @start: If found, holds the start address
+ * @size: If found, holds the size of the address.
+ *
+ * Returns: 1 if found or 0 if not found.
+ */
+int reserve_mem_find_by_name(const char *name, unsigned long *start, unsigned long *size)
+{
+	struct reserve_mem_table *map;
+	int i;
+
+	for (i = 0; i < reserved_mem_count; i++) {
+		map = &reserved_mem_table[i];
+		if (!map->size)
+			continue;
+		if (strcmp(name, map->name) == 0) {
+			*start = map->start;
+			*size = map->size;
+			return 1;
+		}
Quoted 8 lines

mm/memblock.c void __init memblock_free_all(void) · line 2304

Quoted 6 lines
mm/memblock.c +6 -0
+	}
+	return 0;
+}
+
+/*
+ * Parse early_reserve_mem=nn:align:name  

early_reserve_mem or reserve_mem ?

Oops, that was the original name. I'll change that.

Quoted 5 lines

mm/memblock.c void __init memblock_free_all(void) · line 2308

Quoted 5 lines
mm/memblock.c +4 -0
+ */
+static int __init reserve_mem(char *p)
+{
+	phys_addr_t start, size, align;

Hmm, I wonder if I should change size and align to unsigned long?

Quoted 12 lines
Quoted 12 lines
mm/memblock.c +12 -0
+	char *oldp;
+	int err;
+
+	if (!p)
+		return -EINVAL;
+
+	oldp = p;
+	size = memparse(p, &p);
+	if (p == oldp)
+		return -EINVAL;
+
+	if (*p != ':')
Quoted 8 lines

mm/memblock.c void __init memblock_free_all(void) · line 2326

Quoted 6 lines
mm/memblock.c +6 -0
+		return -EINVAL;
+
+	align = memparse(p+1, &p);
+	if (*p != ':')
+		return -EINVAL;
+  

Better to check if the name is valid here.

You mean that it has text and is not blank?

Quoted 2 lines

Make sure command line parameters are valid before doing the allocation.

You mean that size is non zero?

I don't know if we care what the align is. Zero is valid.

Quoted 17 lines
Quoted 5 lines
mm/memblock.c +4 -0
+	start = memblock_phys_alloc(size, align);
+	if (!start)
+		return -ENOMEM;
+

mm/memblock.c void __init memblock_free_all(void) · line 2340

Quoted 10 lines
mm/memblock.c +10 -0
+	p++;
+	err = reserved_mem_add(start, size, p);
+	if (err) {
+		memblock_phys_free(start, size);
+		return err;
+	}
+
+	p += strlen(p);
+
+	return *p == '\0' ? 0: -EINVAL;  

We won't free the memory if return -EINVAL?

I guess we can do this check before the allocation, like you suggested.

Thanks for the review.

-- Steve

Quoted 12 lines
Quoted 11 lines
mm/memblock.c +3 -0
+}
+__setup("reserve_mem=", reserve_mem);
+
 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
 static const char * const flagname[] = {
 	[ilog2(MEMBLOCK_HOTPLUG)] = "HOTPLUG",
Signature · 1 line
2.43.0
maintainer
#6 lore raw
Quoted 11 lines · Steven Rostedt
Quoted 7 lines · On Tue, 11 Jun 2024 14:40:29 +0000 Wei Yang <[email protected]> wrote:

mm/memblock.c void __init memblock_free_all(void) · line 2326

Quoted 5 lines
mm/memblock.c +5 -0
+
+	align = memparse(p+1, &p);
+	if (*p != ':')
+		return -EINVAL;
+  

Make sure command line parameters are valid before doing the allocation.

Quoted 3 lines

You mean that size is non zero?

I don't know if we care what the align is. Zero is valid.

memblock won't like zero align, it should be SMP_CACHE_BYTES at least. No point requiring it from user, just update the alignment if the user passed zero.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
mm/memblock.c +4 -0
+	start = memblock_phys_alloc(size, align);
+	if (!start)
+		return -ENOMEM;
+
Signature · 2 lines
Sincerely yours,
Mike.
author maintainer
#7 lore raw
Quoted 5 lines · On Tue, 11 Jun 2024 19:30:47 +0300 Mike Rapoport <[email protected]> wrote:
Quoted 1 line

I don't know if we care what the align is. Zero is valid.

memblock won't like zero align, it should be SMP_CACHE_BYTES at least. No point requiring it from user, just update the alignment if the user passed zero.

Thanks, will do in v4.

-- Steve

author maintainer
#8 lore raw
In reply to Steven Rostedt
Quoted 4 lines · On Tue, 11 Jun 2024 11:12:18 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 4 lines
Quoted 4 lines
mm/memblock.c +4 -0
+	p++;
+	err = reserved_mem_add(start, size, p);
+	if (err) {
+		memblock_phys_free(start, size);
Quoted 8 lines
Quoted 8 lines

mm/memblock.c void __init memblock_free_all(void) · line 2340

Quoted 6 lines
mm/memblock.c +6 -0
+		return err;
+	}
+
+	p += strlen(p);
+
+	return *p == '\0' ? 0: -EINVAL;    

We won't free the memory if return -EINVAL?

I actually copied this from parse_memmap_one() in arch/x86/kernel/e820.c and now looking at it, it's a pretty stupid check.

It does: p += strlen(p); which requires p ending with '\0'. So this will likely bug if there is no '\0'.

I'm going to remove this, but still check to make sure that the name has some length before the allocation.

-- Steve

#9 lore raw
In reply to Steven Rostedt
Quoted 22 lines · Steven Rostedt

On Tue, 11 Jun 2024 14:40:29 +0000
Wei Yang <[email protected]> wrote:

Missed this just before sending out v3 :-p

Quoted 8 lines
Quoted 8 lines
mm/memblock.c +0 -0
index d09136e040d3..a8bf0ee9e2b4 100644
@@ -2244,6 +2244,103 @@ void __init memblock_free_all(void)
 	totalram_pages_add(pages);
 }
 
Quoted 5 lines

mm/memblock.c void __init memblock_free_all(void) · line 2249

Quoted 3 lines
mm/memblock.c +3 -0
+/* Keep a table to reserve named memory */
+#define RESERVE_MEM_MAX_ENTRIES		8
+#define RESERVE_MEM_NAME_SIZE		16  
                                        ^

Suggest to align with previous line.

It is. But because the patch adds a "+", it pushed the "8" out another tab.

Quoted 1 line
Quoted 9 lines
Quoted 6 lines

mm/memblock.c void __init memblock_free_all(void) · line 2253

Quoted 4 lines
mm/memblock.c +4 -0
+struct reserve_mem_table {
+	char			name[RESERVE_MEM_NAME_SIZE];
+	unsigned long		start;
+	unsigned long		size;  

phys_addr_t looks more precise?

For just the start variable, correct? I'm OK with updating that.

Both start and size. When you look at the definition of memblock_region, both are defined as phys_addr_t.

Quoted 7 lines
Quoted 6 lines

mm/memblock.c void __init memblock_free_all(void) · line 2256

Quoted 4 lines
mm/memblock.c +3 -0
+};
+static struct reserve_mem_table reserved_mem_table[RESERVE_MEM_MAX_ENTRIES];
+static int reserved_mem_count;  

Seems no matter we use this feature or not, these memory would be occupied?

Quoted 6 lines

Yes, because allocation may screw it up as well. I could add a CONFIG around it, so that those that do not want this could configure it out. But since it's just a total of (16 + 8 + 8) * 8 = 256 bytes, I'm not sure it's much of a worry to add the complexities to save that much space. As the code to save it may likely be bigger.

If Mike feel good to it, I am ok.

Quoted 16 lines
Quoted 15 lines

mm/memblock.c void __init memblock_free_all(void) · line 2268

Quoted 13 lines
mm/memblock.c +12 -0
+
+/* Add wildcard region with a lookup name */
+static int __init reserved_mem_add(unsigned long start, unsigned long size,
+				   const char *name)
+{
+	struct reserve_mem_table *map;
+
+	if (!name || !name[0] || strlen(name) >= RESERVE_MEM_NAME_SIZE)
+		return -EINVAL;
+
+	if (reserved_mem_count >= RESERVE_MEM_MAX_ENTRIES)
+		return -1;  

return ENOSPC? Not good at it, but a raw value maybe not a good practice.

Quoted 8 lines

This is what gets returned by the command line parser. It only cares if it is zero or not.

Quoted 2 lines

Also, we'd better do this check before allocation.

What allocation?

You call reserved_mem_add() after memblock_phys_alloc().
My suggestion is do those sanity check before calling memblock_phys_alloc().

Quoted 43 lines
Quoted 31 lines
Quoted 31 lines
mm/memblock.c +30 -0
+
+	map = &reserved_mem_table[reserved_mem_count++];
+	map->start = start;
+	map->size = size;
+	strscpy(map->name, name);
+	return 0;
+}
+
+/**
+ * reserve_mem_find_by_name - Find reserved memory region with a given name
+ * @name: The name that is attached to a reserved memory region
+ * @start: If found, holds the start address
+ * @size: If found, holds the size of the address.
+ *
+ * Returns: 1 if found or 0 if not found.
+ */
+int reserve_mem_find_by_name(const char *name, unsigned long *start, unsigned long *size)
+{
+	struct reserve_mem_table *map;
+	int i;
+
+	for (i = 0; i < reserved_mem_count; i++) {
+		map = &reserved_mem_table[i];
+		if (!map->size)
+			continue;
+		if (strcmp(name, map->name) == 0) {
+			*start = map->start;
+			*size = map->size;
+			return 1;
+		}
Quoted 8 lines

mm/memblock.c void __init memblock_free_all(void) · line 2304

Quoted 6 lines
mm/memblock.c +6 -0
+	}
+	return 0;
+}
+
+/*
+ * Parse early_reserve_mem=nn:align:name  

early_reserve_mem or reserve_mem ?

Oops, that was the original name. I'll change that.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

mm/memblock.c void __init memblock_free_all(void) · line 2308

Quoted 4 lines
mm/memblock.c +4 -0
+ */
+static int __init reserve_mem(char *p)
+{
+	phys_addr_t start, size, align;

Hmm, I wonder if I should change size and align to unsigned long?

I grep the kernel, some use u64, some use unsigned long. I think it is ok to use unsigned long here.

Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
mm/memblock.c +12 -0
+	char *oldp;
+	int err;
+
+	if (!p)
+		return -EINVAL;
+
+	oldp = p;
+	size = memparse(p, &p);
+	if (p == oldp)
+		return -EINVAL;
+
+	if (*p != ':')
Quoted 8 lines

mm/memblock.c void __init memblock_free_all(void) · line 2326

Quoted 6 lines
mm/memblock.c +6 -0
+		return -EINVAL;
+
+	align = memparse(p+1, &p);
+	if (*p != ':')
+		return -EINVAL;
+  

Better to check if the name is valid here.

Quoted 7 lines

You mean that it has text and is not blank?

Quoted 2 lines

Make sure command line parameters are valid before doing the allocation.

You mean that size is non zero?

I mean do those sanity check before real allocation.

Quoted 2 lines

I don't know if we care what the align is. Zero is valid.

memblock internal would check the alignment. If it is zero, it will change to SMP_CACHE_BYTES with dump_stack().

Quoted 37 lines
Quoted 17 lines
Quoted 5 lines
mm/memblock.c +4 -0
+	start = memblock_phys_alloc(size, align);
+	if (!start)
+		return -ENOMEM;
+

mm/memblock.c void __init memblock_free_all(void) · line 2340

Quoted 10 lines
mm/memblock.c +10 -0
+	p++;
+	err = reserved_mem_add(start, size, p);
+	if (err) {
+		memblock_phys_free(start, size);
+		return err;
+	}
+
+	p += strlen(p);
+
+	return *p == '\0' ? 0: -EINVAL;  

We won't free the memory if return -EINVAL?

I guess we can do this check before the allocation, like you suggested.

Thanks for the review.

-- Steve

Quoted 12 lines
Quoted 11 lines
mm/memblock.c +3 -0
+}
+__setup("reserve_mem=", reserve_mem);
+
 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
 static const char * const flagname[] = {
 	[ilog2(MEMBLOCK_HOTPLUG)] = "HOTPLUG",
Signature · 1 line
2.43.0
Signature · 2 lines
Wei Yang
Help you, Help me
author maintainer
#10 lore raw
Quoted 7 lines · On Wed, 12 Jun 2024 07:23:40 +0000 Wei Yang <[email protected]> wrote:
Quoted 7 lines
Quoted 6 lines

mm/memblock.c void __init memblock_free_all(void) · line 2253

Quoted 4 lines
mm/memblock.c +4 -0
+struct reserve_mem_table {
+	char			name[RESERVE_MEM_NAME_SIZE];
+	unsigned long		start;
+	unsigned long		size;    

phys_addr_t looks more precise?

Quoted 5 lines
Quoted 2 lines

For just the start variable, correct? I'm OK with updating that.

Both start and size. When you look at the definition of memblock_region, both are defined as phys_addr_t.

I ended up keeping everything phys_addr_t.

Quoted 37 lines
Quoted 8 lines
Quoted 7 lines

mm/memblock.c void __init memblock_free_all(void) · line 2256

Quoted 5 lines
mm/memblock.c +3 -0
+};
+static struct reserve_mem_table reserved_mem_table[RESERVE_MEM_MAX_ENTRIES];
+static int reserved_mem_count;    

Seems no matter we use this feature or not, these memory would be occupied?

Quoted 6 lines

Yes, because allocation may screw it up as well. I could add a CONFIG around it, so that those that do not want this could configure it out. But since it's just a total of (16 + 8 + 8) * 8 = 256 bytes, I'm not sure it's much of a worry to add the complexities to save that much space. As the code to save it may likely be bigger.

If Mike feel good to it, I am ok.

Quoted 20 lines
Quoted 15 lines

mm/memblock.c void __init memblock_free_all(void) · line 2268

Quoted 13 lines
mm/memblock.c +12 -0
+
+/* Add wildcard region with a lookup name */
+static int __init reserved_mem_add(unsigned long start, unsigned long size,
+				   const char *name)
+{
+	struct reserve_mem_table *map;
+
+	if (!name || !name[0] || strlen(name) >= RESERVE_MEM_NAME_SIZE)
+		return -EINVAL;
+
+	if (reserved_mem_count >= RESERVE_MEM_MAX_ENTRIES)
+		return -1;    

return ENOSPC? Not good at it, but a raw value maybe not a good practice.

This is what gets returned by the command line parser. It only cares if it is zero or not.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Also, we'd better do this check before allocation.

What allocation?

You call reserved_mem_add() after memblock_phys_alloc().
My suggestion is do those sanity check before calling memblock_phys_alloc().

Yeah, I did add more checks before the allocation happens.

Quoted 52 lines
Quoted 44 lines
Quoted 32 lines
Quoted 32 lines
mm/memblock.c +30 -0
+
+	map = &reserved_mem_table[reserved_mem_count++];
+	map->start = start;
+	map->size = size;
+	strscpy(map->name, name);
+	return 0;
+}
+
+/**
+ * reserve_mem_find_by_name - Find reserved memory region with a given name
+ * @name: The name that is attached to a reserved memory region
+ * @start: If found, holds the start address
+ * @size: If found, holds the size of the address.
+ *
+ * Returns: 1 if found or 0 if not found.
+ */
+int reserve_mem_find_by_name(const char *name, unsigned long *start, unsigned long *size)
+{
+	struct reserve_mem_table *map;
+	int i;
+
+	for (i = 0; i < reserved_mem_count; i++) {
+		map = &reserved_mem_table[i];
+		if (!map->size)
+			continue;
+		if (strcmp(name, map->name) == 0) {
+			*start = map->start;
+			*size = map->size;
+			return 1;
+		}
Quoted 8 lines

mm/memblock.c void __init memblock_free_all(void) · line 2304

Quoted 6 lines
mm/memblock.c +6 -0
+	}
+	return 0;
+}
+
+/*
+ * Parse early_reserve_mem=nn:align:name    

early_reserve_mem or reserve_mem ?

Oops, that was the original name. I'll change that.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

mm/memblock.c void __init memblock_free_all(void) · line 2308

Quoted 4 lines

+ */

mm/memblock.c +3 -0
+static int __init reserve_mem(char *p)
+{
+	phys_addr_t start, size, align;  

Hmm, I wonder if I should change size and align to unsigned long?

Quoted 2 lines

I grep the kernel, some use u64, some use unsigned long. I think it is ok to use unsigned long here.

For consistency, I switched them all to phys_addr_t.

Quoted 25 lines
Quoted 25 lines
Quoted 13 lines
Quoted 13 lines
mm/memblock.c +12 -0
+	char *oldp;
+	int err;
+
+	if (!p)
+		return -EINVAL;
+
+	oldp = p;
+	size = memparse(p, &p);
+	if (p == oldp)
+		return -EINVAL;
+
+	if (*p != ':')
Quoted 8 lines

mm/memblock.c void __init memblock_free_all(void) · line 2326

Quoted 6 lines
mm/memblock.c +6 -0
+		return -EINVAL;
+
+	align = memparse(p+1, &p);
+	if (*p != ':')
+		return -EINVAL;
+    

Better to check if the name is valid here.

You mean that it has text and is not blank?

Quoted 1 line
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Make sure command line parameters are valid before doing the allocation.

You mean that size is non zero?

I mean do those sanity check before real allocation.

Yep, I hope I caught everything (of course I need to check if the name exists first).

Quoted 6 lines
Quoted 3 lines

I don't know if we care what the align is. Zero is valid.

memblock internal would check the alignment. If it is zero, it will change to SMP_CACHE_BYTES with dump_stack().

I saw that and added:

	if (align < SMP_CACHE_BYTES)
		align = SMP_CACHE_BYTES;

so that SMP_CACHE_BYTES will be the minimum alignment.

Thanks for looking at this.

-- Steve

#11 lore raw
In reply to Steven Rostedt
Quoted 105 lines · Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

In order to allow for requesting a memory region that can be used for things like pstore on multiple machines where the memory layout is not the same, add a new option to the kernel command line called "reserve_mem".

The format is: reserve_mem=nn:align:name

Where it will find nn amount of memory at the given alignment of align. The name field is to allow another subsystem to retrieve where the memory was found. For example:

reserve_mem=12M:4096:oops ramoops.mem_name=oops

Where ramoops.mem_name will tell ramoops that memory was reserved for it via the reserve_mem option and it can find it by calling:

 if (reserve_mem_find_by_name("oops", &start, &size)) {
	// start holds the start address and size holds the size given

This is typically used for systems that do not wipe the RAM, and this command line will try to reserve the same physical memory on soft reboots. Note, it is not guaranteed to be the same location. For example, if KASLR places the kernel at the location of where the RAM reservation was from a previous boot, the new reservation will be at a different location. Any subsystem using this feature must add a way to verify that the contents of the physical memory is from a previous boot, as there may be cases where the memory will not be located at the same location.

Not all systems may work either. There could be bit flips if the reboot goes through the BIOS. Using kexec to reboot the machine is likely to have better results in such cases.

---

.../admin-guide/kernel-parameters.txt         | 20 ++++
include/linux/mm.h                            |  2 +
mm/memblock.c                                 | 97 +++++++++++++++++++
3 files changed, 119 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +20 -0
index b600df82669d..4b2f7fb8de66 100644
@@ -5710,6 +5710,26 @@
 			them.  If <base> is less than 0x10000, the region
 			is assumed to be I/O ports; otherwise it is memory.
 
+	reserve_mem=	[RAM]
+			Format: nn[KNG]:<align>:<label>
+			Reserve physical memory and label it with a name that
+			other subsystems can use to access it. This is typically
+			used for systems that do not wipe the RAM, and this command
+			line will try to reserve the same physical memory on
+			soft reboots. Note, it is not guaranteed to be the same
+			location. For example, if KASLR places the kernel at the
+			location of where the RAM reservation was from a previous
+			boot, the new reservation will be at a different location.
+			Any subsystem using this feature must add a way to verify
+			that the contents of the physical memory is from a previous
+			boot, as there may be cases where the memory will not be
+			located at the same location.
+
+			The format is size:align:label for example, to request
+			12 megabytes of 4096 alignment for ramoops:
+
+			reserver_mem=12M:4096:oops ramoops.mem_name=oops
+
 	reservetop=	[X86-32,EARLY]
 			Format: nn[KMG]
 			Reserves a hole at the top of the kernel virtual
include/linux/mm.h +2 -0
index 9849dfda44d4..b4455cc02f2c 100644
@@ -4263,4 +4263,6 @@ static inline bool pfn_is_unaccepted_memory(unsigned long pfn)
 void vma_pgtable_walk_begin(struct vm_area_struct *vma);
 void vma_pgtable_walk_end(struct vm_area_struct *vma);
 
+int reserve_mem_find_by_name(const char *name, unsigned long *start, unsigned long *size);
+
 #endif /* _LINUX_MM_H */
mm/memblock.c +12 -0
index d09136e040d3..a8bf0ee9e2b4 100644
@@ -2244,6 +2244,103 @@ void __init memblock_free_all(void)
 	totalram_pages_add(pages);
 }
 
+/* Keep a table to reserve named memory */
+#define RESERVE_MEM_MAX_ENTRIES		8
+#define RESERVE_MEM_NAME_SIZE		16
+struct reserve_mem_table {
+	char			name[RESERVE_MEM_NAME_SIZE];
+	unsigned long		start;
+	unsigned long		size;
+};
+static struct reserve_mem_table reserved_mem_table[RESERVE_MEM_MAX_ENTRIES];
+static int reserved_mem_count;
+
+/* Add wildcard region with a lookup name */

mm/memblock.c void __init memblock_free_all(void) · line 2268

Quoted 10 lines
mm/memblock.c +10 -0
+static int __init reserved_mem_add(unsigned long start, unsigned long size,
+				   const char *name)
+{
+	struct reserve_mem_table *map;
+
+	if (!name || !name[0] || strlen(name) >= RESERVE_MEM_NAME_SIZE)
+		return -EINVAL;
+
+	if (reserved_mem_count >= RESERVE_MEM_MAX_ENTRIES)
+		return -1;

Another thing come to my mind: could we specify several reserve_mem on the command line?

If so, we may need to check whether names conflict.

Quoted 82 lines
mm/memblock.c +75 -0
+
+	map = &reserved_mem_table[reserved_mem_count++];
+	map->start = start;
+	map->size = size;
+	strscpy(map->name, name);
+	return 0;
+}
+
+/**
+ * reserve_mem_find_by_name - Find reserved memory region with a given name
+ * @name: The name that is attached to a reserved memory region
+ * @start: If found, holds the start address
+ * @size: If found, holds the size of the address.
+ *
+ * Returns: 1 if found or 0 if not found.
+ */
+int reserve_mem_find_by_name(const char *name, unsigned long *start, unsigned long *size)
+{
+	struct reserve_mem_table *map;
+	int i;
+
+	for (i = 0; i < reserved_mem_count; i++) {
+		map = &reserved_mem_table[i];
+		if (!map->size)
+			continue;
+		if (strcmp(name, map->name) == 0) {
+			*start = map->start;
+			*size = map->size;
+			return 1;
+		}
+	}
+	return 0;
+}
+
+/*
+ * Parse early_reserve_mem=nn:align:name
+ */
+static int __init reserve_mem(char *p)
+{
+	phys_addr_t start, size, align;
+	char *oldp;
+	int err;
+
+	if (!p)
+		return -EINVAL;
+
+	oldp = p;
+	size = memparse(p, &p);
+	if (p == oldp)
+		return -EINVAL;
+
+	if (*p != ':')
+		return -EINVAL;
+
+	align = memparse(p+1, &p);
+	if (*p != ':')
+		return -EINVAL;
+
+	start = memblock_phys_alloc(size, align);
+	if (!start)
+		return -ENOMEM;
+
+	p++;
+	err = reserved_mem_add(start, size, p);
+	if (err) {
+		memblock_phys_free(start, size);
+		return err;
+	}
+
+	p += strlen(p);
+
+	return *p == '\0' ? 0: -EINVAL;
+}
+__setup("reserve_mem=", reserve_mem);
+
 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
 static const char * const flagname[] = {
 	[ilog2(MEMBLOCK_HOTPLUG)] = "HOTPLUG",
Signature · 1 line
2.43.0
Signature · 2 lines
Wei Yang
Help you, Help me
author maintainer
#12 lore raw
Quoted 6 lines · On Wed, 12 Jun 2024 07:30:49 +0000 Wei Yang <[email protected]> wrote:
Quoted 6 lines
mm/memblock.c +6 -0
+/* Add wildcard region with a lookup name */
+static int __init reserved_mem_add(unsigned long start, unsigned long size,
+				   const char *name)
+{
+	struct reserve_mem_table *map;
+
Quoted 8 lines

mm/memblock.c void __init memblock_free_all(void) · line 2268

Quoted 5 lines
mm/memblock.c +5 -0
+	if (!name || !name[0] || strlen(name) >= RESERVE_MEM_NAME_SIZE)
+		return -EINVAL;
+
+	if (reserved_mem_count >= RESERVE_MEM_MAX_ENTRIES)
+		return -1;  

Another thing come to my mind: could we specify several reserve_mem on the command line?

Yes, in fact I have this on my command line that I test with:

reserve_mem=2M:0:oops ramoops.mem_name=oops ramoops.console_size=0x100000 reserve_mem=12M:4096:trace trace_instance=boot_mapped@trace

Quoted 2 lines

If so, we may need to check whether names conflict.

Yeah, I can add that.

Thanks,

-- Steve

author maintainer
#13 lore raw

PATCH v2 2/2 pstore/ramoops: Add ramoops.mem_name= command line option

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

Add a method to find a region specified by reserve_mem=nn:align:name for ramoops. Adding a kernel command line parameter:

reserve_mem=12M:4096:oops ramoops.mem_name=oops

Will use the size and location defined by the memmap parameter where it finds the memory and labels it "oops". The "oops" in the ramoops option is used to search for it.

This allows for arbitrary RAM to be used for ramoops if it is known that the memory is not cleared on kernel crashes or soft reboots.

---
 fs/pstore/ram.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
fs/pstore/ram.c +15 -0
index b1a455f42e93..bae8486045eb 100644
@@ -50,6 +50,11 @@ module_param_hw(mem_address, ullong, other, 0400);
 MODULE_PARM_DESC(mem_address,
 		"start of reserved RAM used to store oops/panic logs");
 
+static char *mem_name;
+module_param_named(mem_name, mem_name, charp, 0400);
+MODULE_PARM_DESC(mem_name,
+		"name of kernel param that holds addr (builtin only)");
+
 static ulong mem_size;
 module_param(mem_size, ulong, 0400);
 MODULE_PARM_DESC(mem_size,
@@ -914,6 +919,16 @@ static void __init ramoops_register_dummy(void)
 {
 	struct ramoops_platform_data pdata;
 
+	if (mem_name) {
+		unsigned long start;
+		unsigned long size;
+
+		if (reserve_mem_find_by_name(mem_name, &start, &size)) {
+			mem_address = start;
+			mem_size = size;
+		}
+	}
+
 	/*
 	 * Prepare a dummy platform data structure to carry the module
 	 * parameters. If mem_size isn't set, then there are no module
Signature · 1 line
2.43.0
#14 lore raw

Re: [PATCH v2 2/2] pstore/ramoops: Add ramoops.mem_name= command line option

Hi Steve, thanks for the patch! A suggestion/fix below, inline.

fs/pstore/ram.c module_param_hw(mem_address, ullong, other, 0400); · line 56

Quoted 5 lines · Steven Rostedt

[...]

fs/pstore/ram.c +4 -0
+static char *mem_name;
+module_param_named(mem_name, mem_name, charp, 0400);
+MODULE_PARM_DESC(mem_name,
+		"name of kernel param that holds addr (builtin only)");

s/ (builtin only)//

Likely a vestige of a previous version heh
Cheers,

Guilherme

reviewer Tested-by on the v2 series
#15 lore raw
In reply to Steven Rostedt
Quoted 22 lines · Steven Rostedt

Reserve unspecified location of physical memory from kernel command line [...]
Solution:

The solution I have come up with is to introduce a new "reserve_mem=" kernel command line. This parameter takes the following format:

reserve_mem=nn:align:label

Where nn is the size of memory to reserve, the align is the alignment of that memory, and label is the way for other sub-systems to find that memory. This way the kernel command line could have:

reserve_mem=12M:4096:oops ramoops.mem_name=oops

At boot up, the kernel will search for 12 megabytes in usable memory regions with an alignment of 4096. It will start at the highest regions and work its way down (for those old devices that want access to lower address DMA). When it finds a region, it will save it off in a small table and mark it with the "oops" label. Then the pstore ramoops sub-system could ask for that memory and location, and it will map itself there.

Quoted 8 lines

This prototype allows for 8 different mappings (which may be overkill, 4 is probably plenty) with 16 byte size to store the label.

I have tested this and it works for us to solve the above problem. We can update the kernel and command line and increase the size of pstore without needing to update the firmware, or knowing every memory layout of each board. I only tested this locally, it has not been tested in the field.

Hi Steve, first of all, thanks for this work! This is much appreciated. The kdumpst tooling (Arch Linux) makes use of pstore when available, and the recommendation so far was to reserve memory somehow, like "mem=" or use kdump instead, if no free RAM area was available.

With your solution, things get way more "elegant". Also, I think we all know pstore is not 100% reliable, specially the RAM backend due to already mentioned reasons (like FW memory retraining, ECC memory, etc), but it's great we have a mechanism to **try it**. If it works, awesome - for statistical analysis, this is very useful; pstore has been used with success in the Steam Deck, for example.

With all that said, I've tested your patches on top of 6.10-rc2 in 2 qemu VMs (one running legacy BIOS - seabios - and the other UEFI - using ovmf) and on Steam Deck, and it's working flawlessly. I've tested only using ramoops as module.

Some code review in the patches themselves (like a missing EXPORT_SYMBOL_GPL), but all in all, that's a great addition! Feel free to add my:

Thanks,

Guilherme

maintainer
#16 lore raw
Quoted 49 lines · Guilherme G. Piccoli
Quoted 22 lines · Steven Rostedt

Reserve unspecified location of physical memory from kernel command line [...]
Solution:

The solution I have come up with is to introduce a new "reserve_mem=" kernel command line. This parameter takes the following format:

reserve_mem=nn:align:label

Where nn is the size of memory to reserve, the align is the alignment of that memory, and label is the way for other sub-systems to find that memory. This way the kernel command line could have:

reserve_mem=12M:4096:oops ramoops.mem_name=oops

At boot up, the kernel will search for 12 megabytes in usable memory regions with an alignment of 4096. It will start at the highest regions and work its way down (for those old devices that want access to lower address DMA). When it finds a region, it will save it off in a small table and mark it with the "oops" label. Then the pstore ramoops sub-system could ask for that memory and location, and it will map itself there.

Quoted 8 lines

This prototype allows for 8 different mappings (which may be overkill, 4 is probably plenty) with 16 byte size to store the label.

I have tested this and it works for us to solve the above problem. We can update the kernel and command line and increase the size of pstore without needing to update the firmware, or knowing every memory layout of each board. I only tested this locally, it has not been tested in the field.

Hi Steve, first of all, thanks for this work! This is much appreciated. The kdumpst tooling (Arch Linux) makes use of pstore when available, and the recommendation so far was to reserve memory somehow, like "mem=" or use kdump instead, if no free RAM area was available.

With your solution, things get way more "elegant". Also, I think we all know pstore is not 100% reliable, specially the RAM backend due to already mentioned reasons (like FW memory retraining, ECC memory, etc), but it's great we have a mechanism to **try it**. If it works, awesome - for statistical analysis, this is very useful; pstore has been used with success in the Steam Deck, for example.

With all that said, I've tested your patches on top of 6.10-rc2 in 2 qemu VMs (one running legacy BIOS - seabios - and the other UEFI - using ovmf) and on Steam Deck, and it's working flawlessly. I've tested only using ramoops as module.

Quoted 5 lines

Some code review in the patches themselves (like a missing EXPORT_SYMBOL_GPL), but all in all, that's a great addition! Feel free to add my:

Yeah, I think this looks good as long as it's understood to be a "best effort", and will radically simplify doing qemu testing, etc. I expect I can take v3 into -next with the fixes Guilherme noted.

-Kees

Signature · 1 line
Kees Cook
author maintainer
#17 lore raw
In reply to Guilherme G. Piccoli
Quoted 5 lines · On Fri, 7 Jun 2024 16:54:41 -0300 "Guilherme G. Piccoli" <[email protected]>…

Some code review in the patches themselves (like a missing EXPORT_SYMBOL_GPL), but all in all, that's a great addition! Feel free to add my:

Thanks a lot Guilherme! Much appreciated.

I'll send out a v3 with your comments addressed. And may even add parts of this email in the change logs on how it does work in various cases.

-- Steve