of: reserved_mem: several fixes about reserved memory
v3 cover of: reserved_mem: several fixes about reserved memory
From: Wandun Chen <[email protected]>
This series fixes several error-handling issues in the reserved-memory initialization paths.
The first two patches fix cleanup of no-map regions after driver initialization failure.
Static reserved-memory nodes are reserved during the early DT scan but initialized later. The third patch tags regions whose early reservation succeeded, so the late scan can skip the nodes whose early reservation failed.
The last two patches reject overlapping static regions. Without these checks, overlapping nodes can be initialized over the same physical memory, result in data corrupt.
Sashiko reported these issues in [1] [2] [3].
[1] https://sashiko.dev/#/message/20260814090305.4C8741F00A3D%40smtp.kernel.org
[2] https://sashiko.dev/#/message/20260814084718.29C341F000E9%40smtp.kernel.org
[3] https://sashiko.dev/#/message/20260806100605.2C2C01F000E9%40smtp.kernel.org
v2 --> v3:
1. Rework the mechanism that checks in the late scan whether the early reservation succeeded (patches 3-5, suggested by Marek, thanks).
Patch 3 adds a new memblock flag MEMBLOCK_RSRV_RMEM, which is set when the early reservation of a static region succeeds and checked in the late scan.
Patches 4 and 5 are reworked to reject regions that overlap or are contained by an existing reservation. The code makes a little different from what was acked in v2, so the Acked-by tags for these patches are not carried over.
2. Reorder the patches: the two cleanup fixes in v2 now come first. In v3, the first patch now introduces the 'dynamic' distinction in fdt_init_reserved_mem_node(), which the following patche 3 build on, so the series reads more fluently.
v1 --> v2:
1. Rework failed-node tracking in patch 1: do not track zero-sized nodes, and keep a reserved_mem slot when tracking overflows.
2. Reject static reserved regions overlapping existing no-map regions.
3. Keep MEMBLOCK_NOMAP flag for static no-map regions when init failure.
Wandun Chen (5):
of: reserved_mem: release dynamically allocated no-map region on init
failure
of: reserved_mem: retain static no-map memory on init failure
of: reserved_mem: skip init for regions whose early reservation failed
of: reserved_mem: reject static regions overlapping no-map memory
of: reserved_mem: reject static mapped regions overlapping existing
reservations
drivers/of/of_reserved_mem.c | 86 +++++++++++++++++++++++++------- include/linux/memblock.h | 7 +++ mm/memblock.c | 95 ++++++++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+), 18 deletions(-)
Signature · 1 line
2.43.0
PATCH v3 1/5 of: reserved_mem: release dynamically allocated no-map region on init failure
From: Wandun Chen <[email protected]>
Dynamically reserved-memory regions are added to memblock.reserved by memblock_phys_alloc_range() during __reserved_mem_alloc_size(). When a reserved-memory region's driver initialization fails, fdt_init_reserved_mem_node() cleans up the reservation. For no-map regions it only calls memblock_clear_nomap(), leaving the range in memblock.reserved and unavailable for normal memory use.
Fix it by freeing the region on init failure when it was dynamically allocated.
Sashiko found this issue in [1].
--- drivers/of/of_reserved_mem.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/of/of_reserved_mem.c +8 -5
@@ -112,7 +112,8 @@ static int __init alloc_reserved_mem_array(void) }static void fdt_init_reserved_mem_node (unsigned long node,const char *uname, -phys_addr_t base,phys_addr_t size); +phys_addr_t base,phys_addr_t size, +bool dynamic);static int fdt_validate_reserved_mem_node (unsigned long node,phys_addr_t *align); static int fdt_fixup_reserved_mem_node(unsigned long node,
@@ -308,7 +309,7 @@ void __init fdt_scan_reserved_mem_late(void)if (size) { uname =fdt_get_name (fdt, child,NULL ); -fdt_init_reserved_mem_node (child, uname, base, size); +fdt_init_reserved_mem_node (child, uname, base, size, false); } }
@@ -518,7 +519,7 @@ static int __init __reserved_mem_alloc_size(unsigned long node, const char *unam }fdt_fixup_reserved_mem_node (node, base, size); -fdt_init_reserved_mem_node (node, uname, base, size); +fdt_init_reserved_mem_node (node, uname, base, size, true);return 0 ; }
@@ -627,13 +628,15 @@ static int __init __reserved_mem_init_node(struct reserved_mem *rmem,* @uname: name of the reserved memory node * @base: base address of the reserved memory region * @size: size of the reserved memory region +* @dynamic: whether the region was dynamically allocated * * This function calls the region-specific initialization function for a * reserved memory region and saves all region-specific data to the * reserved_mem array to allow of_reserved_mem_lookup() to find it. */ static void __init fdt_init_reserved_mem_node (unsigned long node,const char *uname, -phys_addr_t base,phys_addr_t size) +phys_addr_t base,phys_addr_t size, +bool dynamic) {int err =0 ;bool nomap;
@@ -659,7 +662,7 @@ static void __init fdt_init_reserved_mem_node(unsigned long node, const char *unif (nomap)memblock_clear_nomap (rmem->base, rmem->size); -else +if (dynamic || !nomap)memblock_phys_free (rmem->base, rmem->size);return ; }else {
Signature · 1 line
2.43.0
PATCH v3 2/5 of: reserved_mem: retain static no-map memory on init failure
In reply to Wandun ChenFrom: Wandun Chen <[email protected]>
Static no-map reserved-memory regions are initialized after paging_init(). If initialization fails, MEMBLOCK_NOMAP would be cleared, and the memory is available to the buddy allocator even though it was excluded from the kernel linear mapping.
Clear MEMBLOCK_NOMAP only for dynamically allocated regions, which are initialized before paging_init() and can safely be made available to the kernel linear mapping.
Sashiko found this issue in [1].
--- drivers/of/of_reserved_mem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/of/of_reserved_mem.c +1 -1
@@ -660,7 +660,7 @@ static void __init fdt_init_reserved_mem_node(unsigned long node, const char *unpr_info ("node %s compatible matching fail\n" , rmem->name); rmem->name =NULL ; -if (nomap) +if (dynamic && nomap)memblock_clear_nomap (rmem->base, rmem->size);if (dynamic || !nomap)memblock_phys_free (rmem->base, rmem->size);
Signature · 1 line
2.43.0
PATCH v3 3/5 of: reserved_mem: skip init for regions whose early reservation failed
In reply to Wandun ChenFrom: Wandun Chen <[email protected]>
__reserved_mem_reserve_reg() discards the error from early_init_dt_reserve_memory() and returns 0 unconditionally, so the caller counts the node in total_reserved_mem_cnt and the late scan initializes it without checking whether the early reservation actually succeeded. A region whose reservation failed is then handed to a device assuming the memory is protected.
Tag each region in memblock with MEMBLOCK_RSRV_RMEM when its early reservation succeeds, so the late scan can check whether the node's own reservation succeeded and skip those that failed.
--- drivers/of/of_reserved_mem.c | 46 ++++++++++++++++++------ include/linux/memblock.h | 6 ++++ mm/memblock.c | 69 ++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 11 deletions(-)
drivers/of/of_reserved_mem.c +35 -11
@@ -122,6 +122,8 @@ static int fdt_fixup_reserved_mem_node(unsigned long node,static int __init early_init_dt_reserve_memory (phys_addr_t base,phys_addr_t size,bool nomap) { +int ret; +if (nomap) {/* * If the memory is already reserved (by another region), we
@@ -132,9 +134,16 @@ static int __init early_init_dt_reserve_memory(phys_addr_t base, memblock_is_region_reserved(base, size))return -EBUSY ; -return memblock_mark_nomap (base, size); + ret =memblock_mark_nomap (base, size); +if (!ret) +memblock_mark_rsrv_rmem (base, size); +return ret; } -return memblock_reserve (base, size); + + ret =memblock_reserve (base, size); +if (!ret) +memblock_mark_rsrv_rmem (base, size); +return ret; }/*
@@ -168,14 +177,19 @@ static int __init __reserved_mem_reserve_reg(unsigned long node, base = b; size = s; -if (size &&early_init_dt_reserve_memory (base, size, nomap) ==0 ) { -fdt_fixup_reserved_mem_node (node, base, size); -pr_debug ("Reserved memory: reserved region for node '%s': base %pa, size %lu MiB\n" , - uname, &base, (unsigned long )(size /SZ_1M )); - }else { +if (!size) +return -EINVAL ; + + err =early_init_dt_reserve_memory (base, size, nomap); +if (err) {pr_err ("Reserved memory: failed to reserve memory for node '%s': base %pa, size %lu MiB\n" , uname, &base, (unsigned long )(size /SZ_1M )); +return err; } + +fdt_fixup_reserved_mem_node (node, base, size); +pr_debug ("Reserved memory: reserved region for node '%s': base %pa, size %lu MiB\n" , + uname, &base, (unsigned long )(size /SZ_1M ));return 0 ; }
@@ -288,6 +302,7 @@ void __init fdt_scan_reserved_mem_late(void)fdt_for_each_subnode (child, fdt, node) {const __be32 *prop;const char *uname; +bool nomap;u64 b, s;int ret;int len;
@@ -307,10 +322,15 @@ void __init fdt_scan_reserved_mem_late(void) base = b; size = s; -if (size) { - uname =fdt_get_name (fdt, child,NULL ); -fdt_init_reserved_mem_node (child, uname, base, size, false); - } +if (!size) +continue ; + + nomap =of_get_flat_dt_prop (child,"no-map" ,NULL ) !=NULL ; +if (!memblock_is_region_rsrv_rmem (base, size, nomap)) +continue ; + + uname =fdt_get_name (fdt, child,NULL ); +fdt_init_reserved_mem_node (child, uname, base, size, false); }/* check for overlapping reserved regions */
@@ -662,8 +682,12 @@ static void __init fdt_init_reserved_mem_node(unsigned long node, const char *unif (dynamic && nomap)memblock_clear_nomap (rmem->base, rmem->size); +if (dynamic || !nomap)memblock_phys_free (rmem->base, rmem->size); + +if (!dynamic) +memblock_clear_rsrv_rmem (rmem->base, rmem->size);return ; }else {phys_addr_t end = rmem->base + rmem->size -1 ;
include/linux/memblock.h +6 -0
@@ -52,6 +52,7 @@ extern unsigned long long max_possible_pfn;* kernel that we know is good to use. It is the only memory that * allocations may happen from in this phase. * @MEMBLOCK_RSRV_HUGETLB: memory is reserved for hugetlb pages +* @MEMBLOCK_RSRV_RMEM: memory reserved by a static /reserved-memory node */ enum memblock_flags {MEMBLOCK_NONE =0x0 ,/* No special request */
@@ -63,6 +64,7 @@ enum memblock_flags {MEMBLOCK_RSRV_KERN =0x20 ,/* memory reserved for kernel use */ MEMBLOCK_KHO_SCRATCH =0x40 ,/* scratch memory for kexec handover */ MEMBLOCK_RSRV_HUGETLB =0x80 ,/* memory reserved for hugetlb pages */ + MEMBLOCK_RSRV_RMEM = 0x100, /* static /reserved-memory node */ };/**
@@ -160,6 +162,10 @@ int memblock_reserved_mark_noinit(phys_addr_t base, phys_addr_t size);int memblock_reserved_mark_kern (phys_addr_t base,phys_addr_t size);int memblock_mark_kho_scratch (phys_addr_t base,phys_addr_t size);int memblock_clear_kho_scratch (phys_addr_t base,phys_addr_t size); +int memblock_mark_rsrv_rmem (phys_addr_t base,phys_addr_t size); +int memblock_clear_rsrv_rmem (phys_addr_t base,phys_addr_t size); +bool memblock_is_region_rsrv_rmem (phys_addr_t base,phys_addr_t size, +bool nomap);void memblock_free (void *ptr,size_t size);
mm/memblock.c +69 -0
87 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v3 4/5 of: reserved_mem: reject static regions overlapping no-map memory
In reply to Wandun ChenFrom: Wandun Chen <[email protected]>
Static no-map reserved-memory regions are marked in memblock.memory rather than memblock.reserved. So the reservation overlap check does not reject a static region that overlaps existing no-map memory.
Both regions can then be initialized and hand the same physical memory to different reserved-memory drivers. So reject a static no-map region that overlaps existing reserved no-map memory, and skip a no-map region in the late scan that overlaps one already reserved.
Sashiko found this issue in [1].
--- drivers/of/of_reserved_mem.c | 23 +++++++++++++++++++++-- include/linux/memblock.h | 1 + mm/memblock.c | 26 ++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-)
drivers/of/of_reserved_mem.c +21 -2
@@ -131,7 +131,8 @@ static int __init early_init_dt_reserve_memory(phys_addr_t base,* if the region isn't memory as it won't be mapped. */ if (memblock_overlaps_region (&memblock.memory, base, size) && -memblock_is_region_reserved (base, size)) + (memblock_is_region_reserved (base, size) || +memblock_overlaps_nomap_region (base, size)))return -EBUSY ; ret =memblock_mark_nomap (base, size);
@@ -266,6 +267,20 @@ static void __init __rmem_check_for_overlap(void) } } +static bool __init rmem_overlaps_check (phys_addr_t base,phys_addr_t size, +int start) +{ +int i; + +for (i = start; i < reserved_mem_count; i++) { +struct reserved_mem *r = &reserved_mem[i]; + +if (memblock_addrs_overlap (base, size, r->base, r->size)) +return true; + } +return false; +} +/** * fdt_scan_reserved_mem_late() - Scan FDT and initialize remaining reserved * memory regions.
@@ -279,7 +294,7 @@ void __init fdt_scan_reserved_mem_late(void) {const void *fdt = initial_boot_params;phys_addr_t base, size; -int node, child; +int node, child, static_reserved_start;if (!fdt)return ;
@@ -299,6 +314,8 @@ void __init fdt_scan_reserved_mem_late(void)return ; } + static_reserved_start = reserved_mem_count; +fdt_for_each_subnode (child, fdt, node) {const __be32 *prop;const char *uname;
@@ -326,6 +343,8 @@ void __init fdt_scan_reserved_mem_late(void)continue ; nomap =of_get_flat_dt_prop (child,"no-map" ,NULL ) !=NULL ; +if (nomap &&rmem_overlaps_check (base, size, static_reserved_start)) +continue ;if (!memblock_is_region_rsrv_rmem (base, size, nomap))continue ;
include/linux/memblock.h +1 -0
@@ -504,6 +504,7 @@ bool memblock_is_map_memory(phys_addr_t addr);bool memblock_is_region_memory (phys_addr_t base,phys_addr_t size);bool memblock_is_reserved (phys_addr_t addr);bool memblock_is_region_reserved (phys_addr_t base,phys_addr_t size); +bool memblock_overlaps_nomap_region (phys_addr_t base,phys_addr_t size);void memblock_dump_all (void );
mm/memblock.c +26 -0
@@ -2222,6 +2222,32 @@ bool __init_memblock memblock_is_region_rsrv_rmem(phys_addr_t base,return true; } +/** +* memblock_overlaps_nomap_region - check if a region intersects no-map memory +* @base: base of region to check +* @size: size of region to check +* +* Check if the region [@base, @base + @size) intersects a memory block +* marked %MEMBLOCK_NOMAP. +* +* Return: +* True if they intersect, false if not. +*/ +bool __init_memblock memblock_overlaps_nomap_region(phys_addr_t base, +phys_addr_t size) +{ +struct memblock_region *region; + +memblock_cap_size (base, &size); +for_each_mem_region (region) { +if (memblock_is_nomap (region) && +memblock_addrs_overlap (base, size, region->base, region->size)) +return true; + } + +return false; +} + void __init_memblock memblock_trim_memory(phys_addr_t align) {phys_addr_t start, end, orig_start, orig_end;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v3 4/5] of: reserved_mem: reject static regions overlapping no-map memory
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] of: reserved_mem: integer overflow in rmem_overlaps_check bypasses overlap detection --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] of: reserved_mem: integer overflow in rmem_overlaps_check bypasses overlap detection --
commit bab4bcea3e71f149872f45d815c003c14a811a0a
Author: Wandun Chen <[email protected]>
of: reserved_mem: reject static regions overlapping no-map memory
This commit updates the reserved memory initialization to reject static no-map regions that overlap with existing reserved no-map memory. It also skips no-map regions during the late scan if they overlap with already reserved ones, preventing multiple drivers from mapping the same physical memory.
Quoted 4 lines
drivers/of/of_reserved_mem.c +0
-0
[ ... ]
Quoted 4 lines
drivers/of/of_reserved_mem.c +0
-0@@ -266,6 +267,20 @@ static void __init __rmem_check_for_overlap(void) } }
drivers/of/of_reserved_mem.c static void __init __rmem_check_for_overlap(void) · line 279
Quoted 10 lines
drivers/of/of_reserved_mem.c +10
-0+static bool __init rmem_overlaps_check (phys_addr_t base,phys_addr_t size, +int start) +{ +int i; + +for (i = start; i < reserved_mem_count; i++) { +struct reserved_mem *r = &reserved_mem[i]; + +if (memblock_addrs_overlap (base, size, r->base, r->size)) +return true;
[Severity: High]
Does this code allow an integer wrap-around to bypass the overlap check?
The size variable read from the device tree is used without being bounded by memblock_cap_size() before calling memblock_addrs_overlap().
If a crafted device tree or overlay defines a static reserved memory node with a size large enough to cause base + size to wrap around phys_addr_t, the internal addition in memblock_addrs_overlap() could wrap. This would cause it to incorrectly return false, bypassing this overlap check.
The region then reaches memblock_is_region_rsrv_rmem(), which does cap the size and finds the valid overlap marked by an earlier valid region, allowing multiple reserved memory regions to be mapped to the exact same physical memory.
Quoted 4 lines
drivers/of/of_reserved_mem.c +4
-0+ } +return false; +} +
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v3 5/5 of: reserved_mem: reject static mapped regions overlapping existing reservations
In reply to Wandun ChenFrom: Wandun Chen <[email protected]>
memblock_reserve() permits overlapping reservations, so a statically placed region whose 'reg' overlaps an existing one is accepted. When initialization of a statically placed reserved-memory region overlapping an existing reservation fails, memblock_phys_free() returns the overlap to the buddy allocator, corrupting that memory.
Reject the overlap up front. Dynamically allocated regions are unaffected, as they are allocated from free memory and so cannot overlap an existing reservation.
Sashiko found this issue in [1].
--- drivers/of/of_reserved_mem.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/of/of_reserved_mem.c +5 -1
@@ -141,6 +141,10 @@ static int __init early_init_dt_reserve_memory(phys_addr_t base,return ret; } +if (memblock_is_region_reserved (base, size) || +memblock_overlaps_nomap_region (base, size)) +return -EBUSY ; + ret =memblock_reserve (base, size);if (!ret)memblock_mark_rsrv_rmem (base, size);
@@ -343,7 +347,7 @@ void __init fdt_scan_reserved_mem_late(void)continue ; nomap =of_get_flat_dt_prop (child,"no-map" ,NULL ) !=NULL ; -if (nomap &&rmem_overlaps_check (base, size, static_reserved_start)) +if (rmem_overlaps_check (base, size, static_reserved_start))continue ;if (!memblock_is_region_rsrv_rmem (base, size, nomap))continue ;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v3 5/5] of: reserved_mem: reject static mapped regions overlapping existing reservations
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] of/reserved_mem: valid overlapping reservations are broken by unconditional rejection --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] of/reserved_mem: valid overlapping reservations are broken by unconditional rejection --
commit f3ecc5d72c441ce7fd915e30544e07c69215f6c4
Author: Wandun Chen <[email protected]>
of: reserved_mem: reject static mapped regions overlapping existing reservations
This commit modifies early_init_dt_reserve_memory() to reject static reserved-memory regions that overlap with existing memblock reservations. It aims to prevent memory corruption when initialization of a statically placed region fails and memblock_phys_free() returns the overlap to the buddy allocator.
drivers/of/of_reserved_mem.c static int __init early_init_dt_reserve_memory(phys_addr_t base, · line 147
Quoted 12 lines
drivers/of/of_reserved_mem.c +4
-0@@ -141,6 +141,10 @@ static int __init early_init_dt_reserve_memory(phys_addr_t base,return ret; } +if (memblock_is_region_reserved (base, size) || +memblock_overlaps_nomap_region (base, size)) +return -EBUSY ; +
[Severity: High]
Does this code unconditionally reject reserved-memory nodes that perfectly describe memory already reserved by the architecture or EFI setup?
If a bootloader (like U-Boot or EFI) reserves a memory region (e.g. EFI_RESERVED_TYPE) and also passes a /reserved-memory DT node describing the same region to ensure compatibility, it looks like early_init_dt_reserve_memory() will now find the region already reserved in memblock and return -EBUSY. This bypasses memblock_mark_rsrv_rmem().
Later in the boot process, fdt_scan_reserved_mem_late() checks for the MEMBLOCK_RSRV_RMEM flag on the region:
drivers/of/of_reserved_mem.c:fdt_scan_reserved_mem_late() {
...
if (!memblock_is_region_rsrv_rmem(base, size, nomap))
continue;
...
}If the flag is missing, it skips calling fdt_init_reserved_mem_node(). Could this cause valid overlapping DT nodes to be silently discarded, causing devices relying on these nodes (like DRM display drivers using an EFI framebuffer) to fail to probe because of_reserved_mem_lookup() will return NULL?
Additionally, does the existing check in the if (nomap) block earlier in early_init_dt_reserve_memory() have the same effect? It appears to reject no-map DT nodes if the region was already marked nomap by EFI, effectively destroying those DT nodes as well.
Quoted 3 lines
drivers/of/of_reserved_mem.c +0
-0ret =memblock_reserve (base, size);if (!ret)memblock_mark_rsrv_rmem (base, size);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Mike Rapoport
maintainerQuoted 23 lines · Wandun Chen
From: Wandun Chen <[email protected]>
This series fixes several error-handling issues in the reserved-memory initialization paths.
The first two patches fix cleanup of no-map regions after driver initialization failure.
Static reserved-memory nodes are reserved during the early DT scan but initialized later. The third patch tags regions whose early reservation succeeded, so the late scan can skip the nodes whose early reservation failed.
The last two patches reject overlapping static regions. Without these checks, overlapping nodes can be initialized over the same physical memory, result in data corrupt.
Sashiko reported these issues in [1] [2] [3].
[1] https://sashiko.dev/#/message/20260814090305.4C8741F00A3D%40smtp.kernel.org
[2] https://sashiko.dev/#/message/20260814084718.29C341F000E9%40smtp.kernel.org
[3] https://sashiko.dev/#/message/20260806100605.2C2C01F000E9%40smtp.kernel.org
Quoted 7 lines
v2 --> v3:
1. Rework the mechanism that checks in the late scan whether the early reservation succeeded (patches 3-5, suggested by Marek, thanks).Patch 3 adds a new memblock flag
MEMBLOCK_RSRV_RMEM, which is set when the early reservation of a static region succeeds and checked in the late scan.
Can we keep this local to of_reserved_mem please?
Quoted 34 lines
Patches 4 and 5 are reworked to reject regions that overlap or are contained by an existing reservation. The code makes a little different from what was acked in v2, so the Acked-by tags for these patches are not carried over.
2. Reorder the patches: the two cleanup fixes in v2 now come first. In v3, the first patch now introduces the 'dynamic' distinction in
fdt_init_reserved_mem_node(), which the following patche 3 build on, so the series reads more fluently.v1 --> v2:
1. Rework failed-node tracking in patch 1: do not track zero-sized nodes, and keep areserved_memslot when tracking overflows.
2. Reject static reserved regions overlapping existing no-map regions.
3. KeepMEMBLOCK_NOMAPflag for static no-map regions when init failure.Wandun Chen (5):
of:reserved_mem: release dynamically allocated no-map region on init
failure
of:reserved_mem: retain static no-map memory on init failure
of:reserved_mem: skip init for regions whose early reservation failed
of:reserved_mem: reject static regions overlapping no-map memory
of:reserved_mem: reject static mapped regions overlapping existing
reservationsdrivers/of/of_reserved_mem.c | 86 +++++++++++++++++++++++++------- include/linux/memblock.h | 7 +++ mm/memblock.c | 95 ++++++++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+), 18 deletions(-)Signature · 1 line
2.43.0
Signature · 2 lines
Sincerely yours, Mike.
Quoted 28 lines · Mike Rapoport
Quoted 27 lines · Wandun Chen
From: Wandun Chen <[email protected]>
This series fixes several error-handling issues in the reserved-memory initialization paths.
The first two patches fix cleanup of no-map regions after driver initialization failure.
Static reserved-memory nodes are reserved during the early DT scan but initialized later. The third patch tags regions whose early reservation succeeded, so the late scan can skip the nodes whose early reservation failed.
The last two patches reject overlapping static regions. Without these checks, overlapping nodes can be initialized over the same physical memory, result in data corrupt.
Sashiko reported these issues in [1] [2] [3].
[1] https://sashiko.dev/#/message/20260814090305.4C8741F00A3D%40smtp.kernel.org
[2] https://sashiko.dev/#/message/20260814084718.29C341F000E9%40smtp.kernel.org
[3] https://sashiko.dev/#/message/20260806100605.2C2C01F000E9%40smtp.kernel.orgv2 --> v3:
1. Rework the mechanism that checks in the late scan whether the early reservation succeeded (patches 3-5, suggested by Marek, thanks).
Quoted 5 lines
Quoted 3 lines
Patch 3 adds a new memblock flag
MEMBLOCK_RSRV_RMEM, which is set when the early reservation of a static region succeeds and checked in the late scan.Can we keep this local to
of_reserved_memplease?
Probably not. I do not see a way to keep this entirely local to of_reserved_mem while handling the issue robustly.
I previously implemented an approach in of_reserved_mem that records static reserved-memory nodes whose early reservation failed in a local array [1]. However, the early scan runs before paging_init(), so the array cannot be dynamically expanded. If the number of failed nodes exceeds the array size, some failures cannot be recorded and the issue remains, and that is why Marek said "partial solution", although in practice having that many failed nodes is unlikely.
To handle this robustly, the late scan needs a way to determine whether the corresponding early reservation actually succeeded. Current approach uses memblock to retain that state.
[1] https://lore.kernel.org/lkml/[email protected]/
Best regards,
Wandun
Quoted 36 lines
Quoted 35 lines
Patches 4 and 5 are reworked to reject regions that overlap or are contained by an existing reservation. The code makes a little different from what was acked in v2, so the Acked-by tags for these patches are not carried over.
2. Reorder the patches: the two cleanup fixes in v2 now come first. In v3, the first patch now introduces the 'dynamic' distinction in
fdt_init_reserved_mem_node(), which the following patche 3 build on, so the series reads more fluently.v1 --> v2:
1. Rework failed-node tracking in patch 1: do not track zero-sized nodes, and keep areserved_memslot when tracking overflows.
2. Reject static reserved regions overlapping existing no-map regions.
3. KeepMEMBLOCK_NOMAPflag for static no-map regions when init failure.Wandun Chen (5):
of:reserved_mem: release dynamically allocated no-map region on init
failure
of:reserved_mem: retain static no-map memory on init failure
of:reserved_mem: skip init for regions whose early reservation failed
of:reserved_mem: reject static regions overlapping no-map memory
of:reserved_mem: reject static mapped regions overlapping existing
reservationsdrivers/of/of_reserved_mem.c | 86 +++++++++++++++++++++++++------- include/linux/memblock.h | 7 +++ mm/memblock.c | 95 ++++++++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+), 18 deletions(-)Signature · 1 line
2.43.0
Mike Rapoport
maintainerQuoted 40 lines · Wandun
Quoted 28 lines · Mike Rapoport
Quoted 27 lines · Wandun Chen
From: Wandun Chen <[email protected]>
This series fixes several error-handling issues in the reserved-memory initialization paths.
The first two patches fix cleanup of no-map regions after driver initialization failure.
Static reserved-memory nodes are reserved during the early DT scan but initialized later. The third patch tags regions whose early reservation succeeded, so the late scan can skip the nodes whose early reservation failed.
The last two patches reject overlapping static regions. Without these checks, overlapping nodes can be initialized over the same physical memory, result in data corrupt.
Sashiko reported these issues in [1] [2] [3].
[1] https://sashiko.dev/#/message/20260814090305.4C8741F00A3D%40smtp.kernel.org
[2] https://sashiko.dev/#/message/20260814084718.29C341F000E9%40smtp.kernel.org
[3] https://sashiko.dev/#/message/20260806100605.2C2C01F000E9%40smtp.kernel.orgv2 --> v3:
1. Rework the mechanism that checks in the late scan whether the early reservation succeeded (patches 3-5, suggested by Marek, thanks).Quoted 5 lines
Quoted 3 lines
Patch 3 adds a new memblock flag
MEMBLOCK_RSRV_RMEM, which is set when the early reservation of a static region succeeds and checked in the late scan.Can we keep this local to
of_reserved_memplease?Probably not. I do not see a way to keep this entirely local to
of_reserved_memwhile handling the issue robustly.
Quoted 7 lines
I previously implemented an approach in
of_reserved_memthat records static reserved-memory nodes whose early reservation failed in a local array [1]. However, the early scan runs beforepaging_init(), so the array cannot be dynamically expanded. If the number of failed nodes exceeds the array size, some failures cannot be recorded and the issue remains, and that is why Marek said "partial solution", although in practice having that many failed nodes is unlikely.
Even before paging_init() there is memblock_alloc().
Quoted 3 lines
To handle this robustly, the late scan needs a way to determine whether the corresponding early reservation actually succeeded. Current approach uses memblock to retain that state.
I can't say I like the idea of keeping this state in memblock. This add flags and code to memblock to deal with corner cases of bad firmware that reports weird memory layouts, and once there is a flag in the common infrastructure, people tend to abuse it.
Quoted 44 lines
[1] https://lore.kernel.org/lkml/[email protected]/
Best regards,
WandunQuoted 36 lines
Quoted 35 lines
Patches 4 and 5 are reworked to reject regions that overlap or are contained by an existing reservation. The code makes a little different from what was acked in v2, so the Acked-by tags for these patches are not carried over.
2. Reorder the patches: the two cleanup fixes in v2 now come first. In v3, the first patch now introduces the 'dynamic' distinction in
fdt_init_reserved_mem_node(), which the following patche 3 build on, so the series reads more fluently.v1 --> v2:
1. Rework failed-node tracking in patch 1: do not track zero-sized nodes, and keep areserved_memslot when tracking overflows.
2. Reject static reserved regions overlapping existing no-map regions.
3. KeepMEMBLOCK_NOMAPflag for static no-map regions when init failure.Wandun Chen (5):
of:reserved_mem: release dynamically allocated no-map region on init
failure
of:reserved_mem: retain static no-map memory on init failure
of:reserved_mem: skip init for regions whose early reservation failed
of:reserved_mem: reject static regions overlapping no-map memory
of:reserved_mem: reject static mapped regions overlapping existing
reservationsdrivers/of/of_reserved_mem.c | 86 +++++++++++++++++++++++++------- include/linux/memblock.h | 7 +++ mm/memblock.c | 95 ++++++++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+), 18 deletions(-)Signature · 1 line
2.43.0
Signature · 2 lines
Sincerely yours, Mike.
Quoted 37 lines · Mike Rapoport
Quoted 36 lines · Wandun
Quoted 24 lines · Mike Rapoport
Quoted 23 lines · Wandun Chen
From: Wandun Chen <[email protected]>
This series fixes several error-handling issues in the reserved-memory initialization paths.
The first two patches fix cleanup of no-map regions after driver initialization failure.
Static reserved-memory nodes are reserved during the early DT scan but initialized later. The third patch tags regions whose early reservation succeeded, so the late scan can skip the nodes whose early reservation failed.
The last two patches reject overlapping static regions. Without these checks, overlapping nodes can be initialized over the same physical memory, result in data corrupt.
Sashiko reported these issues in [1] [2] [3].
[1] https://protect2.fireeye.com/v1/url?k=7e2f7441-1fa46164-7e2eff0e-74fe485cbff6-619f01488e6b7010&q=1&e=f3c07e6c-275b-4aa1-aec1-d931cda5f11a&u=https%3A%2F%2Fsashiko.dev%2F%23%2Fmessage%2F20260814090305.4C8741F00A3D%2540smtp.kernel.org
[2] https://protect2.fireeye.com/v1/url?k=a92ba57b-c8a0b05e-a92a2e34-74fe485cbff6-5f58f4b0f32080b9&q=1&e=f3c07e6c-275b-4aa1-aec1-d931cda5f11a&u=https%3A%2F%2Fsashiko.dev%2F%23%2Fmessage%2F20260814084718.29C341F000E9%2540smtp.kernel.org
[3] https://protect2.fireeye.com/v1/url?k=4f58aae4-2ed3bfc1-4f5921ab-74fe485cbff6-f4c4b3d0ed61b93e&q=1&e=f3c07e6c-275b-4aa1-aec1-d931cda5f11a&u=https%3A%2F%2Fsashiko.dev%2F%23%2Fmessage%2F20260806100605.2C2C01F000E9%2540smtp.kernel.orgQuoted 8 lines
Quoted 7 lines
v2 --> v3:
1. Rework the mechanism that checks in the late scan whether the early reservation succeeded (patches 3-5, suggested by Marek, thanks).Patch 3 adds a new memblock flag
MEMBLOCK_RSRV_RMEM, which is set when the early reservation of a static region succeeds and checked in the late scan.Can we keep this local to
of_reserved_memplease?Probably not. I do not see a way to keep this entirely local to
of_reserved_memwhile handling the issue robustly.
Quoted 8 lines
Quoted 7 lines
I previously implemented an approach in
of_reserved_memthat records static reserved-memory nodes whose early reservation failed in a local array [1]. However, the early scan runs beforepaging_init(), so the array cannot be dynamically expanded. If the number of failed nodes exceeds the array size, some failures cannot be recorded and the issue remains, and that is why Marek said "partial solution", although in practice having that many failed nodes is unlikely.Even before
paging_init()there ismemblock_alloc().
One can call it, but such memory cannot be dereferenced/accessed for example on ARM64, because it is not yet mapped in the linear map.
Quoted 7 lines
Quoted 3 lines
To handle this robustly, the late scan needs a way to determine whether the corresponding early reservation actually succeeded. Current approach uses memblock to retain that state.
I can't say I like the idea of keeping this state in memblock. This add flags and code to memblock to deal with corner cases of bad firmware that reports weird memory layouts, and once there is a flag in the common infrastructure, people tend to abuse it.
So far I found no better place to store the information about successful region reservation.
Best regards
Signature · 2 lines
Marek Szyprowski, PhD Samsung R&D Institute Poland
Mike Rapoport
maintainerQuoted 25 lines · Marek Szyprowski
Quoted 24 lines · Mike Rapoport
Quoted 23 lines · Wandun
Quoted 5 lines
Quoted 5 lines
From: Wandun Chen <[email protected]>
This series fixes several error-handling issues in the reserved-memory initialization paths.
Quoted 8 lines
Quoted 7 lines
v2 --> v3:
1. Rework the mechanism that checks in the late scan whether the early reservation succeeded (patches 3-5, suggested by Marek, thanks).Patch 3 adds a new memblock flag
MEMBLOCK_RSRV_RMEM, which is set when the early reservation of a static region succeeds and checked in the late scan.Can we keep this local to
of_reserved_memplease?Probably not. I do not see a way to keep this entirely local to
of_reserved_memwhile handling the issue robustly.I previously implemented an approach in
of_reserved_memthat records static reserved-memory nodes whose early reservation failed in a local array [1]. However, the early scan runs beforepaging_init(), so the array cannot be dynamically expanded. If the number of failed nodes exceeds the array size, some failures cannot be recorded and the issue remains, and that is why Marek said "partial solution", although in practice having that many failed nodes is unlikely.
Quoted 4 lines
Quoted 1 line
Even before
paging_init()there ismemblock_alloc().One can call it, but such memory cannot be dereferenced/accessed for example on ARM64, because it is not yet mapped in the linear map.
Can't we use early_memremap() for it?
If there are many regions that need MEMBLOCK_RSRV_RMEM set, memblock will need to allocate memory. If that memory is still not mapped we'll end up with a crash in memblock.
Quoted 10 lines
Quoted 7 lines
Quoted 3 lines
To handle this robustly, the late scan needs a way to determine whether the corresponding early reservation actually succeeded. Current approach uses memblock to retain that state.
I can't say I like the idea of keeping this state in memblock. This add flags and code to memblock to deal with corner cases of bad firmware that reports weird memory layouts, and once there is a flag in the common infrastructure, people tend to abuse it.
So far I found no better place to store the information about successful region reservation.
We can have reserve_failed_nodes larger than MAX_RESERVED_REGIONS, it's __initdata and anyway discarded after boot.
But even a DT with more than 64 bad regions seems broken enough to WARN() and ignore remaining errors.
Quoted 3 lines
Best regards
Signature · 1 line
Marek Szyprowski, PhD
Signature · 2 lines
Sincerely yours, Mike.
Quoted 26 lines · Mike Rapoport
Quoted 25 lines · Marek Szyprowski
Quoted 24 lines · Mike Rapoport
Quoted 23 lines · Wandun
Quoted 5 lines
Quoted 5 lines
From: Wandun Chen <[email protected]>
This series fixes several error-handling issues in the reserved-memory initialization paths.
Quoted 8 lines
Quoted 7 lines
v2 --> v3:
1. Rework the mechanism that checks in the late scan whether the early reservation succeeded (patches 3-5, suggested by Marek, thanks).Patch 3 adds a new memblock flag
MEMBLOCK_RSRV_RMEM, which is set when the early reservation of a static region succeeds and checked in the late scan.Can we keep this local to
of_reserved_memplease?Probably not. I do not see a way to keep this entirely local to
of_reserved_memwhile handling the issue robustly.I previously implemented an approach in
of_reserved_memthat records static reserved-memory nodes whose early reservation failed in a local array [1]. However, the early scan runs beforepaging_init(), so the array cannot be dynamically expanded. If the number of failed nodes exceeds the array size, some failures cannot be recorded and the issue remains, and that is why Marek said "partial solution", although in practice having that many failed nodes is unlikely.
Quoted 4 lines
Quoted 3 lines
Quoted 1 line
Even before
paging_init()there ismemblock_alloc().One can call it, but such memory cannot be dereferenced/accessed for example on ARM64, because it is not yet mapped in the linear map.
Can't we use
early_memremap()for it?
I didn't know that this is a valid, allowed use case for early_memremap(). If so, then this is definitely the best approach, as it also removes the horrible two pass initialization introduced by commit 8a6e02d0c00e ("of: reserved_mem: Restructure how the reserved memory regions are processed").
I've backed such refactor with LLM and it looks very promising:
https://lore.kernel.org/all/[email protected]/
It works fine on ARM, ARM64 and RiscV64. :)
Best regards
Signature · 2 lines
Marek Szyprowski, PhD Samsung R&D Institute Poland