* Re: [PATCH RFC] mm/memblock: Fix reserve_mem allocation overlapping KHO scratch regions
2025-11-30 17:29 [PATCH RFC] mm/memblock: Fix reserve_mem allocation overlapping KHO scratch regions Swaraj Gaikwad
@ 2025-11-30 15:03 ` Mike Rapoport
2025-11-30 21:15 ` Swaraj Gaikwad
2025-12-01 19:07 ` kernel test robot
2025-12-01 20:31 ` kernel test robot
2 siblings, 1 reply; 6+ messages in thread
From: Mike Rapoport @ 2025-11-30 15:03 UTC (permalink / raw)
To: Swaraj Gaikwad
Cc: Andrew Morton,
open list:MEMBLOCK AND MEMORY MANAGEMENT INITIALIZATION,
open list, skhan, david.hunter.linux
Hi,
On Sun, Nov 30, 2025 at 05:29:39PM +0000, Swaraj Gaikwad wrote:
> Currently, `reserve_mem=` does not check for overlap with these KHO
> scratch areas. As a result, a memblock allocation may land inside a
> KHO-provided scratch region, leading to corruption or loss of the data.
> Noted by the following TODO:
> /* TODO: Allocation must be outside of scratch region */
> This RFC proposes extending `reserve_mem()` to allocate memory *only* in
> gaps outside the KHO scratch intervals. The logic is:
>
> 1. Walk through all KHO scratch ranges (kho_scratch[]).
> 2. Attempt allocation in each safe gap:
> [curr_start_addr, scratch_start)
> 3. If not found, attempt to allocate after the last scratch block.
> 4. If all attempts fail, return -ENOMEM.
>
> The allocation is done via `memblock_phys_alloc_range()`, which already
> supports constrained range allocation and preserves alignment guarantees.
>
> This is posted as an RFC because I would like feedback on:
>
> - Whether the allocation-gap scanning approach is acceptable.
> - Whether this logic belongs in reserve_mem() or should be abstracted
> into a helper for reuse.
> - I would appreciate guidance on testing this change.
So this is completely untested?
Kernel documentation asks for submitters to test their code:
https://docs.kernel.org/process/submit-checklist.html#test-your-code
Please study the code you are changing to understand how it should be
tested and don't submit untested patches.
> Signed-off-by: Swaraj Gaikwad <swarajgaikwad1925@gmail.com>
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH RFC] mm/memblock: Fix reserve_mem allocation overlapping KHO scratch regions
@ 2025-11-30 17:29 Swaraj Gaikwad
2025-11-30 15:03 ` Mike Rapoport
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Swaraj Gaikwad @ 2025-11-30 17:29 UTC (permalink / raw)
To: Mike Rapoport, Andrew Morton,
open list:MEMBLOCK AND MEMORY MANAGEMENT INITIALIZATION,
open list
Cc: skhan, david.hunter.linux, Swaraj Gaikwad
Currently, `reserve_mem=` does not check for overlap with these KHO
scratch areas. As a result, a memblock allocation may land inside a
KHO-provided scratch region, leading to corruption or loss of the data.
Noted by the following TODO:
/* TODO: Allocation must be outside of scratch region */
This RFC proposes extending `reserve_mem()` to allocate memory *only* in
gaps outside the KHO scratch intervals. The logic is:
1. Walk through all KHO scratch ranges (kho_scratch[]).
2. Attempt allocation in each safe gap:
[curr_start_addr, scratch_start)
3. If not found, attempt to allocate after the last scratch block.
4. If all attempts fail, return -ENOMEM.
The allocation is done via `memblock_phys_alloc_range()`, which already
supports constrained range allocation and preserves alignment guarantees.
This is posted as an RFC because I would like feedback on:
- Whether the allocation-gap scanning approach is acceptable.
- Whether this logic belongs in reserve_mem() or should be abstracted
into a helper for reuse.
- I would appreciate guidance on testing this change.
Signed-off-by: Swaraj Gaikwad <swarajgaikwad1925@gmail.com>
---
mm/memblock.c | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/mm/memblock.c b/mm/memblock.c
index e23e16618e9b..7605a0b2b64e 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -2684,8 +2684,22 @@ static int __init reserve_mem(char *p)
if (reserve_mem_kho_revive(name, size, align))
return 1;
- /* TODO: Allocation must be outside of scratch region */
- start = memblock_phys_alloc(size, align);
+ phys_addr_t scratch_start, scratch_end;
+ phys_addr_t curr_start_addr = 0;
+ phys_addr_t alloc_end_addr = MEMBLOCK_ALLOC_ACCESSIBLE;
+ unsigned int i;
+
+ for (i = 0; i < kho_scratch_cnt; i++) {
+ scratch_start = kho_scratch[i].addr;
+ scratch_end = kho_scratch[i].addr + kho_scratch[i].size;
+ alloc_end_addr = scratch_start;
+ if (alloc_end_addr > curr_start_addr) {
+ start = memblock_phys_alloc_range(size, align, curr_start_addr, alloc_end_addr);
+ if (start)
+ break;
+ }
+ curr_start_addr = scratch_end;
+ }
if (!start)
return -ENOMEM;
base-commit: 2178727587e1eaa930b8266377119ed6043067df
--
2.52.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH RFC] mm/memblock: Fix reserve_mem allocation overlapping KHO scratch regions
2025-11-30 15:03 ` Mike Rapoport
@ 2025-11-30 21:15 ` Swaraj Gaikwad
2025-12-01 6:50 ` Mike Rapoport
0 siblings, 1 reply; 6+ messages in thread
From: Swaraj Gaikwad @ 2025-11-30 21:15 UTC (permalink / raw)
To: rppt
Cc: akpm, david.hunter.linux, linux-kernel, linux-mm, skhan,
swarajgaikwad1925
Hi Mike,
Thanks for the feedback.
This patch was sent as an RFC because I wanted to confirm whether the
overall approach is acceptable before preparing a fully tested version.
I will send a properly tested patch once I get confirmation that this
direction makes sense.
I’m reviewing how reserve_mem= paths are exercised so I can build an
appropriate test setup. Any guidance on recommended testing for this
area is appreciated.
Thanks,
Swaraj
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH RFC] mm/memblock: Fix reserve_mem allocation overlapping KHO scratch regions
2025-11-30 21:15 ` Swaraj Gaikwad
@ 2025-12-01 6:50 ` Mike Rapoport
0 siblings, 0 replies; 6+ messages in thread
From: Mike Rapoport @ 2025-12-01 6:50 UTC (permalink / raw)
To: Swaraj Gaikwad; +Cc: akpm, david.hunter.linux, linux-kernel, linux-mm, skhan
Hi Swaraj,
On Sun, Nov 30, 2025 at 09:15:39PM +0000, Swaraj Gaikwad wrote:
> Hi Mike,
>
> Thanks for the feedback.
>
> This patch was sent as an RFC because I wanted to confirm whether the
> overall approach is acceptable before preparing a fully tested version.
> I will send a properly tested patch once I get confirmation that this
> direction makes sense.
I didn't look into the details, but I think what you propose won't work.
The first kernel allocates KHO scratch after parsing reserve_mem and the
second kernel allocates everything from KHO scratch at the time
early_params are parsed.
> I’m reviewing how reserve_mem= paths are exercised so I can build an
> appropriate test setup. Any guidance on recommended testing for this
> area is appreciated.
In general to test changes to memblock and other early code I use qemu and
some scripts around it.
> Thanks,
> Swaraj
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH RFC] mm/memblock: Fix reserve_mem allocation overlapping KHO scratch regions
2025-11-30 17:29 [PATCH RFC] mm/memblock: Fix reserve_mem allocation overlapping KHO scratch regions Swaraj Gaikwad
2025-11-30 15:03 ` Mike Rapoport
@ 2025-12-01 19:07 ` kernel test robot
2025-12-01 20:31 ` kernel test robot
2 siblings, 0 replies; 6+ messages in thread
From: kernel test robot @ 2025-12-01 19:07 UTC (permalink / raw)
To: Swaraj Gaikwad; +Cc: oe-kbuild-all
Hi Swaraj,
[This is a private test report for your RFC patch.]
kernel test robot noticed the following build errors:
[auto build test ERROR on 2178727587e1eaa930b8266377119ed6043067df]
url: https://github.com/intel-lab-lkp/linux/commits/Swaraj-Gaikwad/mm-memblock-Fix-reserve_mem-allocation-overlapping-KHO-scratch-regions/20251130-200138
base: 2178727587e1eaa930b8266377119ed6043067df
patch link: https://lore.kernel.org/r/20251130172939.574999-1-swarajgaikwad1925%40gmail.com
patch subject: [PATCH RFC] mm/memblock: Fix reserve_mem allocation overlapping KHO scratch regions
config: parisc-allmodconfig (https://download.01.org/0day-ci/archive/20251202/202512020241.cG6YFnyc-lkp@intel.com/config)
compiler: hppa-linux-gcc (GCC) 15.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251202/202512020241.cG6YFnyc-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202512020241.cG6YFnyc-lkp@intel.com/
All errors (new ones prefixed by >>):
mm/memblock.c: In function 'reserve_mem':
>> mm/memblock.c:2692:25: error: 'kho_scratch_cnt' undeclared (first use in this function); did you mean 'kho_scratch_only'?
2692 | for (i = 0; i < kho_scratch_cnt; i++) {
| ^~~~~~~~~~~~~~~
| kho_scratch_only
mm/memblock.c:2692:25: note: each undeclared identifier is reported only once for each function it appears in
>> mm/memblock.c:2693:33: error: 'kho_scratch' undeclared (first use in this function)
2693 | scratch_start = kho_scratch[i].addr;
| ^~~~~~~~~~~
vim +2692 mm/memblock.c
2627
2628 /*
2629 * Parse reserve_mem=nn:align:name
2630 */
2631 static int __init reserve_mem(char *p)
2632 {
2633 phys_addr_t start, size, align, tmp;
2634 char *name;
2635 char *oldp;
2636 int len;
2637
2638 if (!p)
2639 return -EINVAL;
2640
2641 /* Check if there's room for more reserved memory */
2642 if (reserved_mem_count >= RESERVE_MEM_MAX_ENTRIES)
2643 return -EBUSY;
2644
2645 oldp = p;
2646 size = memparse(p, &p);
2647 if (!size || p == oldp)
2648 return -EINVAL;
2649
2650 if (*p != ':')
2651 return -EINVAL;
2652
2653 align = memparse(p+1, &p);
2654 if (*p != ':')
2655 return -EINVAL;
2656
2657 /*
2658 * memblock_phys_alloc() doesn't like a zero size align,
2659 * but it is OK for this command to have it.
2660 */
2661 if (align < SMP_CACHE_BYTES)
2662 align = SMP_CACHE_BYTES;
2663
2664 name = p + 1;
2665 len = strlen(name);
2666
2667 /* name needs to have length but not too big */
2668 if (!len || len >= RESERVE_MEM_NAME_SIZE)
2669 return -EINVAL;
2670
2671 /* Make sure that name has text */
2672 for (p = name; *p; p++) {
2673 if (!isspace(*p))
2674 break;
2675 }
2676 if (!*p)
2677 return -EINVAL;
2678
2679 /* Make sure the name is not already used */
2680 if (reserve_mem_find_by_name(name, &start, &tmp))
2681 return -EBUSY;
2682
2683 /* Pick previous allocations up from KHO if available */
2684 if (reserve_mem_kho_revive(name, size, align))
2685 return 1;
2686
2687 phys_addr_t scratch_start, scratch_end;
2688 phys_addr_t curr_start_addr = 0;
2689 phys_addr_t alloc_end_addr = MEMBLOCK_ALLOC_ACCESSIBLE;
2690 unsigned int i;
2691
> 2692 for (i = 0; i < kho_scratch_cnt; i++) {
> 2693 scratch_start = kho_scratch[i].addr;
2694 scratch_end = kho_scratch[i].addr + kho_scratch[i].size;
2695 alloc_end_addr = scratch_start;
2696 if (alloc_end_addr > curr_start_addr) {
2697 start = memblock_phys_alloc_range(size, align, curr_start_addr, alloc_end_addr);
2698 if (start)
2699 break;
2700 }
2701 curr_start_addr = scratch_end;
2702 }
2703 if (!start)
2704 return -ENOMEM;
2705
2706 reserved_mem_add(start, size, name);
2707
2708 return 1;
2709 }
2710 __setup("reserve_mem=", reserve_mem);
2711
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH RFC] mm/memblock: Fix reserve_mem allocation overlapping KHO scratch regions
2025-11-30 17:29 [PATCH RFC] mm/memblock: Fix reserve_mem allocation overlapping KHO scratch regions Swaraj Gaikwad
2025-11-30 15:03 ` Mike Rapoport
2025-12-01 19:07 ` kernel test robot
@ 2025-12-01 20:31 ` kernel test robot
2 siblings, 0 replies; 6+ messages in thread
From: kernel test robot @ 2025-12-01 20:31 UTC (permalink / raw)
To: Swaraj Gaikwad; +Cc: llvm, oe-kbuild-all
Hi Swaraj,
[This is a private test report for your RFC patch.]
kernel test robot noticed the following build errors:
[auto build test ERROR on 2178727587e1eaa930b8266377119ed6043067df]
url: https://github.com/intel-lab-lkp/linux/commits/Swaraj-Gaikwad/mm-memblock-Fix-reserve_mem-allocation-overlapping-KHO-scratch-regions/20251130-200138
base: 2178727587e1eaa930b8266377119ed6043067df
patch link: https://lore.kernel.org/r/20251130172939.574999-1-swarajgaikwad1925%40gmail.com
patch subject: [PATCH RFC] mm/memblock: Fix reserve_mem allocation overlapping KHO scratch regions
config: loongarch-allmodconfig (https://download.01.org/0day-ci/archive/20251202/202512020406.fLEkXsNR-lkp@intel.com/config)
compiler: clang version 19.1.7 (https://github.com/llvm/llvm-project cd708029e0b2869e80abe31ddb175f7c35361f90)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251202/202512020406.fLEkXsNR-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202512020406.fLEkXsNR-lkp@intel.com/
All errors (new ones prefixed by >>):
>> mm/memblock.c:2692:18: error: use of undeclared identifier 'kho_scratch_cnt'
2692 | for (i = 0; i < kho_scratch_cnt; i++) {
| ^
>> mm/memblock.c:2693:19: error: use of undeclared identifier 'kho_scratch'
2693 | scratch_start = kho_scratch[i].addr;
| ^
mm/memblock.c:2694:17: error: use of undeclared identifier 'kho_scratch'
2694 | scratch_end = kho_scratch[i].addr + kho_scratch[i].size;
| ^
mm/memblock.c:2694:39: error: use of undeclared identifier 'kho_scratch'
2694 | scratch_end = kho_scratch[i].addr + kho_scratch[i].size;
| ^
4 errors generated.
vim +/kho_scratch_cnt +2692 mm/memblock.c
2627
2628 /*
2629 * Parse reserve_mem=nn:align:name
2630 */
2631 static int __init reserve_mem(char *p)
2632 {
2633 phys_addr_t start, size, align, tmp;
2634 char *name;
2635 char *oldp;
2636 int len;
2637
2638 if (!p)
2639 return -EINVAL;
2640
2641 /* Check if there's room for more reserved memory */
2642 if (reserved_mem_count >= RESERVE_MEM_MAX_ENTRIES)
2643 return -EBUSY;
2644
2645 oldp = p;
2646 size = memparse(p, &p);
2647 if (!size || p == oldp)
2648 return -EINVAL;
2649
2650 if (*p != ':')
2651 return -EINVAL;
2652
2653 align = memparse(p+1, &p);
2654 if (*p != ':')
2655 return -EINVAL;
2656
2657 /*
2658 * memblock_phys_alloc() doesn't like a zero size align,
2659 * but it is OK for this command to have it.
2660 */
2661 if (align < SMP_CACHE_BYTES)
2662 align = SMP_CACHE_BYTES;
2663
2664 name = p + 1;
2665 len = strlen(name);
2666
2667 /* name needs to have length but not too big */
2668 if (!len || len >= RESERVE_MEM_NAME_SIZE)
2669 return -EINVAL;
2670
2671 /* Make sure that name has text */
2672 for (p = name; *p; p++) {
2673 if (!isspace(*p))
2674 break;
2675 }
2676 if (!*p)
2677 return -EINVAL;
2678
2679 /* Make sure the name is not already used */
2680 if (reserve_mem_find_by_name(name, &start, &tmp))
2681 return -EBUSY;
2682
2683 /* Pick previous allocations up from KHO if available */
2684 if (reserve_mem_kho_revive(name, size, align))
2685 return 1;
2686
2687 phys_addr_t scratch_start, scratch_end;
2688 phys_addr_t curr_start_addr = 0;
2689 phys_addr_t alloc_end_addr = MEMBLOCK_ALLOC_ACCESSIBLE;
2690 unsigned int i;
2691
> 2692 for (i = 0; i < kho_scratch_cnt; i++) {
> 2693 scratch_start = kho_scratch[i].addr;
2694 scratch_end = kho_scratch[i].addr + kho_scratch[i].size;
2695 alloc_end_addr = scratch_start;
2696 if (alloc_end_addr > curr_start_addr) {
2697 start = memblock_phys_alloc_range(size, align, curr_start_addr, alloc_end_addr);
2698 if (start)
2699 break;
2700 }
2701 curr_start_addr = scratch_end;
2702 }
2703 if (!start)
2704 return -ENOMEM;
2705
2706 reserved_mem_add(start, size, name);
2707
2708 return 1;
2709 }
2710 __setup("reserve_mem=", reserve_mem);
2711
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2025-12-01 20:31 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-30 17:29 [PATCH RFC] mm/memblock: Fix reserve_mem allocation overlapping KHO scratch regions Swaraj Gaikwad
2025-11-30 15:03 ` Mike Rapoport
2025-11-30 21:15 ` Swaraj Gaikwad
2025-12-01 6:50 ` Mike Rapoport
2025-12-01 19:07 ` kernel test robot
2025-12-01 20:31 ` kernel test robot
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.