mm/mm_init: fix and cleanup mirrored_kernelcore
cover mm/mm_init: fix and cleanup mirrored_kernelcore
When reviewing patch set "mm/memory_hotplug: optimize zone contiguous check when changing pfn range" [1], we notice mirrored_kernelcore introduced some special case to handle.
While during the discussion and test, it shows current mirrored_kernelcore implementation breaks other memmap_init() behavior:
* disturbs defer_init()
* some Movable zone range would be initialized twice
The reason is Zone Movable and Zone Normal overlaps and current logic doesn't handle it well.
As Yuan shows in [2], physical overlapped zone seems not exist in practice. If remove the possibility of overlapped zone, problem could be solved. And also could benefit zone contiguous optimization, IIUC.
Patch [1]: remove the overlapped zone, which fix the above two problem Patch [2]: remove overlap_memmap_init() as there is no overlapped zone, but
record current double initialize problem for reference
Patch [3]: remove absent pages calculation for mirrored_kernelcore
[1]: https://lore.kernel.org/all/[email protected]/T/#u
[2]: https://lore.kernel.org/all/[email protected]/T/#mb0bd07ffd562a5b029f0f07751e580ca339c5b51
Wei Yang (3):
mm/mm_init: adjust overlapped zone for mirror memory too mm/mm_init: remove overlap_memmap_init() as no overlapped zone mm/mm_init: remove special handling absent pages for
mirrored_kernelcore
mm/mm_init.c | 58 ++-------------------------------------------------- 1 file changed, 2 insertions(+), 56 deletions(-)
---
This series is based on one proposed fix for node_spanned_pages:
https://lore.kernel.org/linux-mm/[email protected]/
Signature · 1 line
2.34.1
RFC PATCH 1/3 mm/mm_init: adjust overlapped zone for mirror memory too
When mirrored_kernelcore is set, adjust_zone_range_for_zone_movable() would skip the adjustment if ZONE_MOVABLE starts within one zone.
This makes the zone, usually ZONE_NORMAL, ends the same as ZONE_MOVABLE.
For example, we have a system with following memory layout:
memory[0x0] [0x0000000000001000-0x000000000009efff], 0x000000000009e000 bytes on node 0 flags: 0x2 memory[0x1] [0x0000000000100000-0x00000000bffdefff], 0x00000000bfedf000 bytes on node 0 flags: 0x2 memory[0x2] [0x0000000100000000-0x000000013fffffff], 0x0000000040000000 bytes on node 0 flags: 0x2 memory[0x3] [0x0000000140000000-0x00000001bfffffff], 0x0000000080000000 bytes on node 0 flags: 0x0
With kernelcore=mirror set, current kernel set zone with below range:
Normal [100000, 1c0000]
Movable [140000, 1c0000]
This changes the expected behavior of defer_init(), because it only expect to defer_init() memory in last zone. As Zone Normal and Movable stops at the same end_pfn, defer_init() would set first_deferred_pfn in Zone Normal. And what is worse, pages belongs to Zone Normal would be counted as Zone Movable.
Node 0, zone Normal
spanned 786432
present 262144
managed 34390
Node 0, zone Movable
spanned 524288
present 524288
managed 715855 <- manage more pages than presentWhile when we look at the memory layout, we can see Zone Normal and Movable could be not overlapped if we adjust Zone Normal just other movable zone case. And we don't expect they would interleave.
After doing so, we have non-overlap Zones:
Normal [100000, 140000]
Movable [140000, 1c0000]
And the page locality looks correct:
Node 0, zone Normal
spanned 262144
present 262144
managed 233614
Node 0, zone Movable
spanned 524288
present 524288
managed 524242--- mm/mm_init.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
mm/mm_init.c +1 -2
@@ -1170,8 +1170,7 @@ static void __init adjust_zone_range_for_zone_movable(int nid, arch_zone_highest_possible_pfn[movable_zone]);/* Adjust for ZONE_MOVABLE starting within this range */ - }else if (!mirrored_kernelcore && - *zone_start_pfn < zone_movable_pfn[nid] && + }else if (*zone_start_pfn < zone_movable_pfn[nid] && *zone_end_pfn > zone_movable_pfn[nid]) { *zone_end_pfn = zone_movable_pfn[nid];
Signature · 1 line
2.34.1
RFC PATCH 2/3 mm/mm_init: remove overlap_memmap_init() as no overlapped zone
In reply to Wei YangWhen commit 342332e6a925 ("mm/page_alloc.c: introduce kernelcore=mirror option") is introduced, it would have overlapped Zone Normal and Zone Movable. And this original code would initialize overlapped range twice.
For example, if the memory layout is below:
memory[0x0] [0x0000000000001000-0x000000000009efff], 0x000000000009e000 bytes on node 0 flags: 0x2 memory[0x1] [0x0000000000100000-0x00000000bffdefff], 0x00000000bfedf000 bytes on node 0 flags: 0x2 memory[0x2] [0x0000000100000000-0x000000013fffffff], 0x0000000040000000 bytes on node 0 flags: 0x2 memory[0x3] [0x0000000140000000-0x00000001bfffffff], 0x0000000080000000 bytes on node 0 flags: 0x0
We would get Zone range with kernelcore=mirror set:
Normal [100000, 1c0000]
Movable [140000, 1c0000]
According to original logic, during memmap_init() it calls memmap_init_zone_range() with the same range from for_each_mem_pfn_range() for each populated zone. And then clamp the same memory range with specific zone boundary.
memmap_init()
for_each_mem_pfn_range(&start_pfn, &end_pfn)
for: zone = ZONE_DMA, ZONE_DEVICE
memmap_init_zone_range(zone, start_pfn, end_pfn) <-- same range, but different zone
memmap_init_range() <-- clamped with zone range
For the last memblock region:
[0x0000000140000000-0x00000001bfffffff]
It belongs to both Zone Normal and Zone Movable, which means the same range would be initialized twice by memmap_init_range(), but with different zone_id.
Original code is expected to skip overlapped range with overlap_memmap_init(). But it only skip when (zone == ZONE_MOVABLE), which in turn has no effect for this memory layout. So the last range is first initialized to Zone Normal, then to Zone Movable which is what we expect.
After previous cleanup, the overlapped zone is gone, so the overlap_memmap_init() is not necessary anymore.
--- mm/mm_init.c | 24 ------------------------ 1 file changed, 24 deletions(-)
mm/mm_init.c +0 -24
@@ -797,28 +797,6 @@ void __meminit init_deferred_page(unsigned long pfn, int nid)__init_deferred_page (pfn, nid); } -/* If zone is ZONE_MOVABLE but memory is mirrored, it is an overlapped init */ -static bool __meminit -overlap_memmap_init (unsigned long zone,unsigned long *pfn) -{ -static struct memblock_region *r__meminitdata ; - -if (mirrored_kernelcore && zone ==ZONE_MOVABLE ) { -if (!r || *pfn >=memblock_region_memory_end_pfn (r)) { -for_each_mem_region (r) { -if (*pfn <memblock_region_memory_end_pfn (r)) -break ; - } - } -if (*pfn >=memblock_region_memory_base_pfn (r) && -memblock_is_mirror (r)) { - *pfn =memblock_region_memory_end_pfn (r); -return true; - } - } -return false; -} -/* * Only struct pages that correspond to ranges defined by memblock.memory * are zeroed and initialized by going through __init_single_page() during
@@ -905,8 +883,6 @@ void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone* function. They do not exist on hotplugged memory. */ if (context ==MEMINIT_EARLY ) { -if (overlap_memmap_init (zone, &pfn)) -continue ;if (defer_init (nid, pfn, zone_end_pfn)) { deferred_struct_pages = true;break ;
Signature · 1 line
2.34.1
RFC PATCH 3/3 mm/mm_init: remove special handling absent pages for mirrored_kernelcore
In reply to Wei YangAfter making Zone Normal/Movable non-overlap, it is not necessary to calculate absent pages for mirrored_kernelcore specially.
--- mm/mm_init.c | 31 +------------------------------ 1 file changed, 1 insertion(+), 30 deletions(-)
mm/mm_init.c +1 -30
@@ -1195,40 +1195,11 @@ static unsigned long __init zone_absent_pages_in_node(int nid, unsigned long zone_start_pfn, unsigned long zone_end_pfn) { - unsigned long nr_absent; -/* zone is empty, we don't have any absent pages */ if (zone_start_pfn == zone_end_pfn)return 0 ; - nr_absent =__absent_pages_in_range (nid, zone_start_pfn, zone_end_pfn); - -/* -* ZONE_MOVABLE handling. -* Treat pages to be ZONE_MOVABLE in ZONE_NORMAL as absent pages -* and vice versa. -*/ -if (mirrored_kernelcore && zone_movable_pfn[nid]) { -unsigned long start_pfn, end_pfn; -struct memblock_region *r; - -for_each_mem_region (r) { - start_pfn =clamp (memblock_region_memory_base_pfn (r), - zone_start_pfn, zone_end_pfn); - end_pfn =clamp (memblock_region_memory_end_pfn (r), - zone_start_pfn, zone_end_pfn); - -if (zone_type ==ZONE_MOVABLE && -memblock_is_mirror (r)) - nr_absent += end_pfn - start_pfn; - -if (zone_type ==ZONE_NORMAL && - !memblock_is_mirror (r)) - nr_absent += end_pfn - start_pfn; - } - } - -return nr_absent; +return __absent_pages_in_range (nid, zone_start_pfn, zone_end_pfn); }/*
Signature · 1 line
2.34.1
Quoted 17 lines · Wei Yang
When reviewing patch set "
mm/memory_hotplug: optimize zone contiguous check when changing pfn range" [1], we noticemirrored_kernelcoreintroduced some special case to handle.While during the discussion and test, it shows current
mirrored_kernelcoreimplementation breaks othermemmap_init()behavior:* disturbs
defer_init()
* some Movable zone range would be initialized twiceThe reason is Zone Movable and Zone Normal overlaps and current logic doesn't handle it well.
As Yuan shows in [2], physical overlapped zone seems not exist in practice. If remove the possibility of overlapped zone, problem could be solved. And also could benefit zone contiguous optimization, IIUC.
Quoted 8 lines
Patch [1]: remove the overlapped zone, which fix the above two problem Patch [2]: remove
overlap_memmap_init()as there is no overlapped zone, but
record current double initialize problem for reference
Patch [3]: remove absent pages calculation formirrored_kernelcore[1]: https://lore.kernel.org/all/[email protected]/T/#u
[2]: https://lore.kernel.org/all/[email protected]/T/#mb0bd07ffd562a5b029f0f07751e580ca339c5b51
IIUC, Mike will send an alternative.
Signature · 3 lines
Cheers, David