* [PATCH v2 0/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up
@ 2024-06-06 15:01 Steven Rostedt
2024-06-06 15:01 ` [PATCH v2 1/2] " Steven Rostedt
` (2 more replies)
0 siblings, 3 replies; 18+ messages in thread
From: Steven Rostedt @ 2024-06-06 15:01 UTC (permalink / raw)
To: linux-kernel, linux-trace-kernel
Cc: Masami Hiramatsu, Mark Rutland, Mathieu Desnoyers, Andrew Morton,
Liam R. Howlett, Vlastimil Babka, Lorenzo Stoakes, linux-mm,
Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Peter Zijlstra, Kees Cook, Tony Luck,
Guilherme G. Piccoli, linux-hardening, Guenter Roeck,
Ross Zwisler, wklin, Vineeth Remanan Pillai, Joel Fernandes,
Suleiman Souhlal, Linus Torvalds, Catalin Marinas, Will Deacon,
Ard Biesheuvel
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/20240603233330.801075898@goodmis.org/
- 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(+)
^ permalink raw reply [flat|nested] 18+ messages in thread
* [PATCH v2 1/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up
2024-06-06 15:01 [PATCH v2 0/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up Steven Rostedt
@ 2024-06-06 15:01 ` Steven Rostedt
2024-06-07 19:35 ` Guilherme G. Piccoli
` (2 more replies)
2024-06-06 15:01 ` [PATCH v2 2/2] pstore/ramoops: Add ramoops.mem_name= command line option Steven Rostedt
2024-06-07 19:54 ` [PATCH v2 0/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up Guilherme G. Piccoli
2 siblings, 3 replies; 18+ messages in thread
From: Steven Rostedt @ 2024-06-06 15:01 UTC (permalink / raw)
To: linux-kernel, linux-trace-kernel
Cc: Masami Hiramatsu, Mark Rutland, Mathieu Desnoyers, Andrew Morton,
Liam R. Howlett, Vlastimil Babka, Lorenzo Stoakes, linux-mm,
Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Peter Zijlstra, Kees Cook, Tony Luck,
Guilherme G. Piccoli, linux-hardening, Guenter Roeck,
Ross Zwisler, wklin, Vineeth Remanan Pillai, Joel Fernandes,
Suleiman Souhlal, Linus Torvalds, Catalin Marinas, Will Deacon,
Ard Biesheuvel, Mike Rapoport
From: "Steven Rostedt (Google)" <rostedt@goodmis.org>
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.
Link: https://lore.kernel.org/all/ZjJVnZUX3NZiGW6q@kernel.org/
Suggested-by: Mike Rapoport <rppt@kernel.org>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
.../admin-guide/kernel-parameters.txt | 20 ++++
include/linux/mm.h | 2 +
mm/memblock.c | 97 +++++++++++++++++++
3 files changed, 119 insertions(+)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index b600df82669d..4b2f7fb8de66 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -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
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 9849dfda44d4..b4455cc02f2c 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -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 */
diff --git a/mm/memblock.c b/mm/memblock.c
index d09136e040d3..a8bf0ee9e2b4 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -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 */
+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;
+
+ 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",
--
2.43.0
^ permalink raw reply [flat|nested] 18+ messages in thread
* [PATCH v2 2/2] pstore/ramoops: Add ramoops.mem_name= command line option
2024-06-06 15:01 [PATCH v2 0/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up Steven Rostedt
2024-06-06 15:01 ` [PATCH v2 1/2] " Steven Rostedt
@ 2024-06-06 15:01 ` Steven Rostedt
2024-06-07 19:38 ` Guilherme G. Piccoli
2024-06-07 19:54 ` [PATCH v2 0/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up Guilherme G. Piccoli
2 siblings, 1 reply; 18+ messages in thread
From: Steven Rostedt @ 2024-06-06 15:01 UTC (permalink / raw)
To: linux-kernel, linux-trace-kernel
Cc: Masami Hiramatsu, Mark Rutland, Mathieu Desnoyers, Andrew Morton,
Liam R. Howlett, Vlastimil Babka, Lorenzo Stoakes, linux-mm,
Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Peter Zijlstra, Kees Cook, Tony Luck,
Guilherme G. Piccoli, linux-hardening, Guenter Roeck,
Ross Zwisler, wklin, Vineeth Remanan Pillai, Joel Fernandes,
Suleiman Souhlal, Linus Torvalds, Catalin Marinas, Will Deacon,
Ard Biesheuvel
From: "Steven Rostedt (Google)" <rostedt@goodmis.org>
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.
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
fs/pstore/ram.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/fs/pstore/ram.c b/fs/pstore/ram.c
index b1a455f42e93..bae8486045eb 100644
--- a/fs/pstore/ram.c
+++ b/fs/pstore/ram.c
@@ -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
--
2.43.0
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v2 1/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up
2024-06-06 15:01 ` [PATCH v2 1/2] " Steven Rostedt
@ 2024-06-07 19:35 ` Guilherme G. Piccoli
2024-06-11 14:40 ` Wei Yang
2024-06-12 7:30 ` Wei Yang
2 siblings, 0 replies; 18+ messages in thread
From: Guilherme G. Piccoli @ 2024-06-07 19:35 UTC (permalink / raw)
To: Steven Rostedt
Cc: linux-kernel, Masami Hiramatsu, Mark Rutland, Mathieu Desnoyers,
Andrew Morton, Liam R. Howlett, Vlastimil Babka, Lorenzo Stoakes,
linux-mm, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Kees Cook,
Tony Luck, linux-hardening, Guenter Roeck, Ross Zwisler, wklin,
Vineeth Remanan Pillai, Joel Fernandes, Suleiman Souhlal,
Linus Torvalds, Catalin Marinas, Will Deacon, Ard Biesheuvel,
Mike Rapoport, linux-trace-kernel
Hi Steve, thanks for the patch! Some suggestions/fixes below, inline.
On 06/06/2024 12:01, Steven Rostedt wrote:
> [...]
> +
> + 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
> [...]
> + * 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;
> +}
> +
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
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v2 2/2] pstore/ramoops: Add ramoops.mem_name= command line option
2024-06-06 15:01 ` [PATCH v2 2/2] pstore/ramoops: Add ramoops.mem_name= command line option Steven Rostedt
@ 2024-06-07 19:38 ` Guilherme G. Piccoli
0 siblings, 0 replies; 18+ messages in thread
From: Guilherme G. Piccoli @ 2024-06-07 19:38 UTC (permalink / raw)
To: Steven Rostedt
Cc: Masami Hiramatsu, Mark Rutland, Mathieu Desnoyers, Andrew Morton,
Liam R. Howlett, linux-trace-kernel, Vlastimil Babka,
Lorenzo Stoakes, linux-mm, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
Peter Zijlstra, Kees Cook, Tony Luck, linux-hardening,
Guenter Roeck, Ross Zwisler, wklin, Vineeth Remanan Pillai,
Joel Fernandes, Suleiman Souhlal, Linus Torvalds,
Catalin Marinas, Will Deacon, Ard Biesheuvel, linux-kernel
Hi Steve, thanks for the patch! A suggestion/fix below, inline.
On 06/06/2024 12:01, Steven Rostedt wrote:
> [...]
> +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
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v2 0/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up
2024-06-06 15:01 [PATCH v2 0/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up Steven Rostedt
2024-06-06 15:01 ` [PATCH v2 1/2] " Steven Rostedt
2024-06-06 15:01 ` [PATCH v2 2/2] pstore/ramoops: Add ramoops.mem_name= command line option Steven Rostedt
@ 2024-06-07 19:54 ` Guilherme G. Piccoli
2024-06-10 17:02 ` Kees Cook
2024-06-10 17:08 ` Steven Rostedt
2 siblings, 2 replies; 18+ messages in thread
From: Guilherme G. Piccoli @ 2024-06-07 19:54 UTC (permalink / raw)
To: Steven Rostedt, linux-kernel, linux-trace-kernel
Cc: Masami Hiramatsu, Mark Rutland, Mathieu Desnoyers, Andrew Morton,
Liam R. Howlett, Vlastimil Babka, Lorenzo Stoakes, linux-mm,
Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Peter Zijlstra, Kees Cook, Tony Luck,
linux-hardening, Guenter Roeck, Ross Zwisler, wklin,
Vineeth Remanan Pillai, Joel Fernandes, Suleiman Souhlal,
Linus Torvalds, Catalin Marinas, Will Deacon, Ard Biesheuvel
On 06/06/2024 12:01, Steven Rostedt wrote:
> 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.
>
> 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:
Tested-by: Guilherme G. Piccoli <gpiccoli@igalia.com>
Thanks,
Guilherme
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v2 0/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up
2024-06-07 19:54 ` [PATCH v2 0/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up Guilherme G. Piccoli
@ 2024-06-10 17:02 ` Kees Cook
2024-06-10 17:08 ` Steven Rostedt
1 sibling, 0 replies; 18+ messages in thread
From: Kees Cook @ 2024-06-10 17:02 UTC (permalink / raw)
To: Guilherme G. Piccoli
Cc: Steven Rostedt, linux-kernel, linux-trace-kernel,
Masami Hiramatsu, Mark Rutland, Mathieu Desnoyers, Andrew Morton,
Liam R. Howlett, Vlastimil Babka, Lorenzo Stoakes, linux-mm,
Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Peter Zijlstra, Tony Luck, linux-hardening,
Guenter Roeck, Ross Zwisler, wklin, Vineeth Remanan Pillai,
Joel Fernandes, Suleiman Souhlal, Linus Torvalds,
Catalin Marinas, Will Deacon, Ard Biesheuvel
On Fri, Jun 07, 2024 at 04:54:41PM -0300, Guilherme G. Piccoli wrote:
> On 06/06/2024 12:01, Steven Rostedt wrote:
> > 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.
> >
> > 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:
>
> Tested-by: Guilherme G. Piccoli <gpiccoli@igalia.com>
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
--
Kees Cook
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v2 0/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up
2024-06-07 19:54 ` [PATCH v2 0/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up Guilherme G. Piccoli
2024-06-10 17:02 ` Kees Cook
@ 2024-06-10 17:08 ` Steven Rostedt
1 sibling, 0 replies; 18+ messages in thread
From: Steven Rostedt @ 2024-06-10 17:08 UTC (permalink / raw)
To: Guilherme G. Piccoli
Cc: linux-kernel, linux-trace-kernel, Masami Hiramatsu, Mark Rutland,
Mathieu Desnoyers, Andrew Morton, Liam R. Howlett,
Vlastimil Babka, Lorenzo Stoakes, linux-mm, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
Peter Zijlstra, Kees Cook, Tony Luck, linux-hardening,
Guenter Roeck, Ross Zwisler, wklin, Vineeth Remanan Pillai,
Joel Fernandes, Suleiman Souhlal, Linus Torvalds,
Catalin Marinas, Will Deacon, Ard Biesheuvel
On Fri, 7 Jun 2024 16:54:41 -0300
"Guilherme G. Piccoli" <gpiccoli@igalia.com> wrote:
> 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:
>
> Tested-by: Guilherme G. Piccoli <gpiccoli@igalia.com>
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
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v2 1/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up
2024-06-06 15:01 ` [PATCH v2 1/2] " Steven Rostedt
2024-06-07 19:35 ` Guilherme G. Piccoli
@ 2024-06-11 14:40 ` Wei Yang
2024-06-11 14:58 ` Guenter Roeck
2024-06-11 15:12 ` Steven Rostedt
2024-06-12 7:30 ` Wei Yang
2 siblings, 2 replies; 18+ messages in thread
From: Wei Yang @ 2024-06-11 14:40 UTC (permalink / raw)
To: Steven Rostedt
Cc: linux-kernel, linux-trace-kernel, Masami Hiramatsu, Mark Rutland,
Mathieu Desnoyers, Andrew Morton, Liam R. Howlett,
Vlastimil Babka, Lorenzo Stoakes, linux-mm, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
Peter Zijlstra, Kees Cook, Tony Luck, Guilherme G. Piccoli,
linux-hardening, Guenter Roeck, Ross Zwisler, wklin,
Vineeth Remanan Pillai, Joel Fernandes, Suleiman Souhlal,
Linus Torvalds, Catalin Marinas, Will Deacon, Ard Biesheuvel,
Mike Rapoport
On Thu, Jun 06, 2024 at 11:01:44AM -0400, Steven Rostedt wrote:
>From: "Steven Rostedt (Google)" <rostedt@goodmis.org>
>
>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.
>
>Link: https://lore.kernel.org/all/ZjJVnZUX3NZiGW6q@kernel.org/
>
>Suggested-by: Mike Rapoport <rppt@kernel.org>
>Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
>---
> .../admin-guide/kernel-parameters.txt | 20 ++++
> include/linux/mm.h | 2 +
> mm/memblock.c | 97 +++++++++++++++++++
> 3 files changed, 119 insertions(+)
>
>diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
>index b600df82669d..4b2f7fb8de66 100644
>--- a/Documentation/admin-guide/kernel-parameters.txt
>+++ b/Documentation/admin-guide/kernel-parameters.txt
>@@ -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
>diff --git a/include/linux/mm.h b/include/linux/mm.h
>index 9849dfda44d4..b4455cc02f2c 100644
>--- a/include/linux/mm.h
>+++ b/include/linux/mm.h
>@@ -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 */
>diff --git a/mm/memblock.c b/mm/memblock.c
>index d09136e040d3..a8bf0ee9e2b4 100644
>--- a/mm/memblock.c
>+++ b/mm/memblock.c
>@@ -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.
>+struct reserve_mem_table {
>+ char name[RESERVE_MEM_NAME_SIZE];
>+ unsigned long start;
>+ unsigned long size;
phys_addr_t looks more precise?
>+};
>+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?
>+
>+/* 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.
>+
>+ 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
early_reserve_mem or reserve_mem ?
>+ */
>+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;
>+
Better to check if the name is valid here.
Make sure command line parameters are valid before doing the allocation.
>+ 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;
We won't free the memory if return -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",
>--
>2.43.0
>
>
--
Wei Yang
Help you, Help me
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v2 1/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up
2024-06-11 14:40 ` Wei Yang
@ 2024-06-11 14:58 ` Guenter Roeck
2024-06-11 15:12 ` Steven Rostedt
1 sibling, 0 replies; 18+ messages in thread
From: Guenter Roeck @ 2024-06-11 14:58 UTC (permalink / raw)
To: Wei Yang, Steven Rostedt
Cc: linux-kernel, linux-trace-kernel, Masami Hiramatsu, Mark Rutland,
Mathieu Desnoyers, Andrew Morton, Liam R. Howlett,
Vlastimil Babka, Lorenzo Stoakes, linux-mm, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
Peter Zijlstra, Kees Cook, Tony Luck, Guilherme G. Piccoli,
linux-hardening, Ross Zwisler, wklin, Vineeth Remanan Pillai,
Joel Fernandes, Suleiman Souhlal, Linus Torvalds,
Catalin Marinas, Will Deacon, Ard Biesheuvel, Mike Rapoport
On 6/11/24 07:40, Wei Yang wrote:
[ ... ]
>> +/* 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
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v2 1/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up
2024-06-11 14:40 ` Wei Yang
2024-06-11 14:58 ` Guenter Roeck
@ 2024-06-11 15:12 ` Steven Rostedt
2024-06-11 16:30 ` Mike Rapoport
` (2 more replies)
1 sibling, 3 replies; 18+ messages in thread
From: Steven Rostedt @ 2024-06-11 15:12 UTC (permalink / raw)
To: Wei Yang
Cc: linux-kernel, linux-trace-kernel, Masami Hiramatsu, Mark Rutland,
Mathieu Desnoyers, Andrew Morton, Liam R. Howlett,
Vlastimil Babka, Lorenzo Stoakes, linux-mm, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
Peter Zijlstra, Kees Cook, Tony Luck, Guilherme G. Piccoli,
linux-hardening, Guenter Roeck, Ross Zwisler, wklin,
Vineeth Remanan Pillai, Joel Fernandes, Suleiman Souhlal,
Linus Torvalds, Catalin Marinas, Will Deacon, Ard Biesheuvel,
Mike Rapoport
On Tue, 11 Jun 2024 14:40:29 +0000
Wei Yang <richard.weiyang@gmail.com> wrote:
Missed this just before sending out v3 :-p
> >diff --git a/mm/memblock.c b/mm/memblock.c
> >index d09136e040d3..a8bf0ee9e2b4 100644
> >--- a/mm/memblock.c
> >+++ b/mm/memblock.c
> >@@ -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.
It is. But because the patch adds a "+", it pushed the "8" out another tab.
>
> >+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.
>
> >+};
> >+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.
>
> >+
> >+/* 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.
>
> Also, we'd better do this check before allocation.
What allocation?
>
> >+
> >+ 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
>
> early_reserve_mem or reserve_mem ?
Oops, that was the original name. I'll change that.
>
> >+ */
> >+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?
> >+ 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;
> >+
>
> Better to check if the name is valid here.
You mean that it has text and is not blank?
>
> 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.
>
> >+ 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;
>
> 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
>
> >+}
> >+__setup("reserve_mem=", reserve_mem);
> >+
> > #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
> > static const char * const flagname[] = {
> > [ilog2(MEMBLOCK_HOTPLUG)] = "HOTPLUG",
> >--
> >2.43.0
> >
> >
>
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v2 1/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up
2024-06-11 15:12 ` Steven Rostedt
@ 2024-06-11 16:30 ` Mike Rapoport
2024-06-11 17:34 ` Steven Rostedt
2024-06-11 19:39 ` Steven Rostedt
2024-06-12 7:23 ` Wei Yang
2 siblings, 1 reply; 18+ messages in thread
From: Mike Rapoport @ 2024-06-11 16:30 UTC (permalink / raw)
To: Steven Rostedt
Cc: Wei Yang, linux-kernel, linux-trace-kernel, Masami Hiramatsu,
Mark Rutland, Mathieu Desnoyers, Andrew Morton, Liam R. Howlett,
Vlastimil Babka, Lorenzo Stoakes, linux-mm, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
Peter Zijlstra, Kees Cook, Tony Luck, Guilherme G. Piccoli,
linux-hardening, Guenter Roeck, Ross Zwisler, wklin,
Vineeth Remanan Pillai, Joel Fernandes, Suleiman Souhlal,
Linus Torvalds, Catalin Marinas, Will Deacon, Ard Biesheuvel
On Tue, Jun 11, 2024 at 11:12:18AM -0400, Steven Rostedt wrote:
> On Tue, 11 Jun 2024 14:40:29 +0000
> Wei Yang <richard.weiyang@gmail.com> wrote:
>
> > >+
> > >+ align = memparse(p+1, &p);
> > >+ if (*p != ':')
> > >+ return -EINVAL;
> > >+
> >
> > 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.
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.
> > >+ start = memblock_phys_alloc(size, align);
> > >+ if (!start)
> > >+ return -ENOMEM;
> > >+
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v2 1/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up
2024-06-11 16:30 ` Mike Rapoport
@ 2024-06-11 17:34 ` Steven Rostedt
0 siblings, 0 replies; 18+ messages in thread
From: Steven Rostedt @ 2024-06-11 17:34 UTC (permalink / raw)
To: Mike Rapoport
Cc: Wei Yang, linux-kernel, linux-trace-kernel, Masami Hiramatsu,
Mark Rutland, Mathieu Desnoyers, Andrew Morton, Liam R. Howlett,
Vlastimil Babka, Lorenzo Stoakes, linux-mm, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
Peter Zijlstra, Kees Cook, Tony Luck, Guilherme G. Piccoli,
linux-hardening, Guenter Roeck, Ross Zwisler, wklin,
Vineeth Remanan Pillai, Joel Fernandes, Suleiman Souhlal,
Linus Torvalds, Catalin Marinas, Will Deacon, Ard Biesheuvel
On Tue, 11 Jun 2024 19:30:47 +0300
Mike Rapoport <rppt@kernel.org> wrote:
> > 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
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v2 1/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up
2024-06-11 15:12 ` Steven Rostedt
2024-06-11 16:30 ` Mike Rapoport
@ 2024-06-11 19:39 ` Steven Rostedt
2024-06-12 7:23 ` Wei Yang
2 siblings, 0 replies; 18+ messages in thread
From: Steven Rostedt @ 2024-06-11 19:39 UTC (permalink / raw)
To: Wei Yang
Cc: linux-kernel, linux-trace-kernel, Masami Hiramatsu, Mark Rutland,
Mathieu Desnoyers, Andrew Morton, Liam R. Howlett,
Vlastimil Babka, Lorenzo Stoakes, linux-mm, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
Peter Zijlstra, Kees Cook, Tony Luck, Guilherme G. Piccoli,
linux-hardening, Guenter Roeck, Ross Zwisler, wklin,
Vineeth Remanan Pillai, Joel Fernandes, Suleiman Souhlal,
Linus Torvalds, Catalin Marinas, Will Deacon, Ard Biesheuvel,
Mike Rapoport
On Tue, 11 Jun 2024 11:12:18 -0400
Steven Rostedt <rostedt@goodmis.org> wrote:
> > >+ 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 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
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v2 1/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up
2024-06-11 15:12 ` Steven Rostedt
2024-06-11 16:30 ` Mike Rapoport
2024-06-11 19:39 ` Steven Rostedt
@ 2024-06-12 7:23 ` Wei Yang
2024-06-12 15:32 ` Steven Rostedt
2 siblings, 1 reply; 18+ messages in thread
From: Wei Yang @ 2024-06-12 7:23 UTC (permalink / raw)
To: Steven Rostedt
Cc: Wei Yang, linux-kernel, linux-trace-kernel, Masami Hiramatsu,
Mark Rutland, Mathieu Desnoyers, Andrew Morton, Liam R. Howlett,
Vlastimil Babka, Lorenzo Stoakes, linux-mm, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
Peter Zijlstra, Kees Cook, Tony Luck, Guilherme G. Piccoli,
linux-hardening, Guenter Roeck, Ross Zwisler, wklin,
Vineeth Remanan Pillai, Joel Fernandes, Suleiman Souhlal,
Linus Torvalds, Catalin Marinas, Will Deacon, Ard Biesheuvel,
Mike Rapoport
On Tue, Jun 11, 2024 at 11:12:18AM -0400, Steven Rostedt wrote:
>On Tue, 11 Jun 2024 14:40:29 +0000
>Wei Yang <richard.weiyang@gmail.com> wrote:
>
>Missed this just before sending out v3 :-p
>
>> >diff --git a/mm/memblock.c b/mm/memblock.c
>> >index d09136e040d3..a8bf0ee9e2b4 100644
>> >--- a/mm/memblock.c
>> >+++ b/mm/memblock.c
>> >@@ -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.
>
>It is. But because the patch adds a "+", it pushed the "8" out another tab.
>
>>
>> >+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.
>>
>> >+};
>> >+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.
>
If Mike feel good to it, I am ok.
>>
>> >+
>> >+/* 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.
>
>>
>> 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().
>>
>> >+
>> >+ 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
>>
>> early_reserve_mem or reserve_mem ?
>
>Oops, that was the original name. I'll change that.
>
>>
>> >+ */
>> >+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.
>> >+ 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;
>> >+
>>
>> Better to check if the name is valid here.
>
>You mean that it has text and is not blank?
>
>>
>> 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.
>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().
>>
>> >+ 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;
>>
>> 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
>
>
>>
>> >+}
>> >+__setup("reserve_mem=", reserve_mem);
>> >+
>> > #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
>> > static const char * const flagname[] = {
>> > [ilog2(MEMBLOCK_HOTPLUG)] = "HOTPLUG",
>> >--
>> >2.43.0
>> >
>> >
>>
--
Wei Yang
Help you, Help me
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v2 1/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up
2024-06-06 15:01 ` [PATCH v2 1/2] " Steven Rostedt
2024-06-07 19:35 ` Guilherme G. Piccoli
2024-06-11 14:40 ` Wei Yang
@ 2024-06-12 7:30 ` Wei Yang
2024-06-12 15:28 ` Steven Rostedt
2 siblings, 1 reply; 18+ messages in thread
From: Wei Yang @ 2024-06-12 7:30 UTC (permalink / raw)
To: Steven Rostedt
Cc: linux-kernel, linux-trace-kernel, Masami Hiramatsu, Mark Rutland,
Mathieu Desnoyers, Andrew Morton, Liam R. Howlett,
Vlastimil Babka, Lorenzo Stoakes, linux-mm, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
Peter Zijlstra, Kees Cook, Tony Luck, Guilherme G. Piccoli,
linux-hardening, Guenter Roeck, Ross Zwisler, wklin,
Vineeth Remanan Pillai, Joel Fernandes, Suleiman Souhlal,
Linus Torvalds, Catalin Marinas, Will Deacon, Ard Biesheuvel,
Mike Rapoport
On Thu, Jun 06, 2024 at 11:01:44AM -0400, Steven Rostedt wrote:
>From: "Steven Rostedt (Google)" <rostedt@goodmis.org>
>
>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.
>
>Link: https://lore.kernel.org/all/ZjJVnZUX3NZiGW6q@kernel.org/
>
>Suggested-by: Mike Rapoport <rppt@kernel.org>
>Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
>---
> .../admin-guide/kernel-parameters.txt | 20 ++++
> include/linux/mm.h | 2 +
> mm/memblock.c | 97 +++++++++++++++++++
> 3 files changed, 119 insertions(+)
>
>diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
>index b600df82669d..4b2f7fb8de66 100644
>--- a/Documentation/admin-guide/kernel-parameters.txt
>+++ b/Documentation/admin-guide/kernel-parameters.txt
>@@ -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
>diff --git a/include/linux/mm.h b/include/linux/mm.h
>index 9849dfda44d4..b4455cc02f2c 100644
>--- a/include/linux/mm.h
>+++ b/include/linux/mm.h
>@@ -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 */
>diff --git a/mm/memblock.c b/mm/memblock.c
>index d09136e040d3..a8bf0ee9e2b4 100644
>--- a/mm/memblock.c
>+++ b/mm/memblock.c
>@@ -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 */
>+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.
>+
>+ 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",
>--
>2.43.0
>
>
--
Wei Yang
Help you, Help me
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v2 1/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up
2024-06-12 7:30 ` Wei Yang
@ 2024-06-12 15:28 ` Steven Rostedt
0 siblings, 0 replies; 18+ messages in thread
From: Steven Rostedt @ 2024-06-12 15:28 UTC (permalink / raw)
To: Wei Yang
Cc: linux-kernel, linux-trace-kernel, Masami Hiramatsu, Mark Rutland,
Mathieu Desnoyers, Andrew Morton, Liam R. Howlett,
Vlastimil Babka, Lorenzo Stoakes, linux-mm, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
Peter Zijlstra, Kees Cook, Tony Luck, Guilherme G. Piccoli,
linux-hardening, Guenter Roeck, Ross Zwisler, wklin,
Vineeth Remanan Pillai, Joel Fernandes, Suleiman Souhlal,
Linus Torvalds, Catalin Marinas, Will Deacon, Ard Biesheuvel,
Mike Rapoport
On Wed, 12 Jun 2024 07:30:49 +0000
Wei Yang <richard.weiyang@gmail.com> wrote:
> >+/* 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;
>
> 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
>
> If so, we may need to check whether names conflict.
Yeah, I can add that.
Thanks,
-- Steve
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v2 1/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up
2024-06-12 7:23 ` Wei Yang
@ 2024-06-12 15:32 ` Steven Rostedt
0 siblings, 0 replies; 18+ messages in thread
From: Steven Rostedt @ 2024-06-12 15:32 UTC (permalink / raw)
To: Wei Yang
Cc: linux-kernel, linux-trace-kernel, Masami Hiramatsu, Mark Rutland,
Mathieu Desnoyers, Andrew Morton, Liam R. Howlett,
Vlastimil Babka, Lorenzo Stoakes, linux-mm, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
Peter Zijlstra, Kees Cook, Tony Luck, Guilherme G. Piccoli,
linux-hardening, Guenter Roeck, Ross Zwisler, wklin,
Vineeth Remanan Pillai, Joel Fernandes, Suleiman Souhlal,
Linus Torvalds, Catalin Marinas, Will Deacon, Ard Biesheuvel,
Mike Rapoport
On Wed, 12 Jun 2024 07:23:40 +0000
Wei Yang <richard.weiyang@gmail.com> wrote:
> >> >+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.
I ended up keeping everything phys_addr_t.
>
> >>
> >> >+};
> >> >+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.
> >
>
> If Mike feel good to it, I am ok.
>
> >>
> >> >+
> >> >+/* 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.
> >
> >>
> >> 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.
>
> >>
> >> >+
> >> >+ 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
> >>
> >> early_reserve_mem or reserve_mem ?
> >
> >Oops, that was the original name. I'll change that.
> >
> >>
> >> >+ */
> >> >+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.
For consistency, I switched them all to phys_addr_t.
>
> >> >+ 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;
> >> >+
> >>
> >> Better to check if the name is valid here.
> >
> >You mean that it has text and is not blank?
> >
> >>
> >> 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).
>
> >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
^ permalink raw reply [flat|nested] 18+ messages in thread
end of thread, other threads:[~2024-06-12 15:33 UTC | newest]
Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-06 15:01 [PATCH v2 0/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up Steven Rostedt
2024-06-06 15:01 ` [PATCH v2 1/2] " Steven Rostedt
2024-06-07 19:35 ` Guilherme G. Piccoli
2024-06-11 14:40 ` Wei Yang
2024-06-11 14:58 ` Guenter Roeck
2024-06-11 15:12 ` Steven Rostedt
2024-06-11 16:30 ` Mike Rapoport
2024-06-11 17:34 ` Steven Rostedt
2024-06-11 19:39 ` Steven Rostedt
2024-06-12 7:23 ` Wei Yang
2024-06-12 15:32 ` Steven Rostedt
2024-06-12 7:30 ` Wei Yang
2024-06-12 15:28 ` Steven Rostedt
2024-06-06 15:01 ` [PATCH v2 2/2] pstore/ramoops: Add ramoops.mem_name= command line option Steven Rostedt
2024-06-07 19:38 ` Guilherme G. Piccoli
2024-06-07 19:54 ` [PATCH v2 0/2] mm/memblock: Add "reserve_mem" to reserved named memory at boot up Guilherme G. Piccoli
2024-06-10 17:02 ` Kees Cook
2024-06-10 17:08 ` Steven Rostedt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox