Skip to content

Commit ce6ee46

Browse files
rpptgregkh
authored andcommitted
mm/page_alloc: fix memory map initialization for descending nodes
commit 122e093 upstream. On systems with memory nodes sorted in descending order, for instance Dell Precision WorkStation T5500, the struct pages for higher PFNs and respectively lower nodes, could be overwritten by the initialization of struct pages corresponding to the holes in the memory sections. For example for the below memory layout [ 0.245624] Early memory node ranges [ 0.248496] node 1: [mem 0x0000000000001000-0x0000000000090fff] [ 0.251376] node 1: [mem 0x0000000000100000-0x00000000dbdf8fff] [ 0.254256] node 1: [mem 0x0000000100000000-0x0000001423ffffff] [ 0.257144] node 0: [mem 0x0000001424000000-0x0000002023ffffff] the range 0x1424000000 - 0x1428000000 in the beginning of node 0 starts in the middle of a section and will be considered as a hole during the initialization of the last section in node 1. The wrong initialization of the memory map causes panic on boot when CONFIG_DEBUG_VM is enabled. Reorder loop order of the memory map initialization so that the outer loop will always iterate over populated memory regions in the ascending order and the inner loop will select the zone corresponding to the PFN range. This way initialization of the struct pages for the memory holes will be always done for the ranges that are actually not populated. [akpm@linux-foundation.org: coding style fixes] Link: https://lkml.kernel.org/r/YNXlMqBbL+tBG7yq@kernel.org Link: https://bugzilla.kernel.org/show_bug.cgi?id=213073 Link: https://lkml.kernel.org/r/20210624062305.10940-1-rppt@kernel.org Fixes: 0740a50 ("mm/page_alloc.c: refactor initialization of struct page for holes in memory layout") Signed-off-by: Mike Rapoport <rppt@linux.ibm.com> Cc: Boris Petkov <bp@alien8.de> Cc: Robert Shteynfeld <robert.shteynfeld@gmail.com> Cc: Baoquan He <bhe@redhat.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: David Hildenbrand <david@redhat.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> [rppt: tweak for compatibility with IA64's override of memmap_init] Signed-off-by: Mike Rapoport <rppt@linux.ibm.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 9e1cf2d commit ce6ee46

3 files changed

Lines changed: 75 additions & 42 deletions

File tree

‎arch/ia64/include/asm/pgtable.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,9 @@ extern struct page *zero_page_memmap_ptr;
520520

521521
# ifdef CONFIG_VIRTUAL_MEM_MAP
522522
/* arch mem_map init routine is needed due to holes in a virtual mem_map */
523-
extern void memmap_init (unsigned long size, int nid, unsigned long zone,
524-
unsigned long start_pfn);
523+
void memmap_init(void);
524+
void arch_memmap_init(unsigned long size, int nid, unsigned long zone,
525+
unsigned long start_pfn);
525526
# endif /* CONFIG_VIRTUAL_MEM_MAP */
526527
# endif /* !__ASSEMBLY__ */
527528

‎arch/ia64/mm/init.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ virtual_memmap_init(u64 start, u64 end, void *arg)
542542
}
543543

544544
void __meminit
545-
memmap_init (unsigned long size, int nid, unsigned long zone,
545+
arch_memmap_init (unsigned long size, int nid, unsigned long zone,
546546
unsigned long start_pfn)
547547
{
548548
if (!vmem_map) {
@@ -562,6 +562,10 @@ memmap_init (unsigned long size, int nid, unsigned long zone,
562562
}
563563
}
564564

565+
void __init memmap_init(void)
566+
{
567+
}
568+
565569
int
566570
ia64_pfn_valid (unsigned long pfn)
567571
{

‎mm/page_alloc.c‎

Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6129,7 +6129,7 @@ void __ref memmap_init_zone_device(struct zone *zone,
61296129
return;
61306130

61316131
/*
6132-
* The call to memmap_init_zone should have already taken care
6132+
* The call to memmap_init should have already taken care
61336133
* of the pages reserved for the memmap, so we can just jump to
61346134
* the end of that region and start processing the device pages.
61356135
*/
@@ -6194,7 +6194,7 @@ static void __meminit zone_init_free_lists(struct zone *zone)
61946194
/*
61956195
* Only struct pages that correspond to ranges defined by memblock.memory
61966196
* are zeroed and initialized by going through __init_single_page() during
6197-
* memmap_init_zone().
6197+
* memmap_init_zone_range().
61986198
*
61996199
* But, there could be struct pages that correspond to holes in
62006200
* memblock.memory. This can happen because of the following reasons:
@@ -6213,9 +6213,9 @@ static void __meminit zone_init_free_lists(struct zone *zone)
62136213
* zone/node above the hole except for the trailing pages in the last
62146214
* section that will be appended to the zone/node below.
62156215
*/
6216-
static u64 __meminit init_unavailable_range(unsigned long spfn,
6217-
unsigned long epfn,
6218-
int zone, int node)
6216+
static void __init init_unavailable_range(unsigned long spfn,
6217+
unsigned long epfn,
6218+
int zone, int node)
62196219
{
62206220
unsigned long pfn;
62216221
u64 pgcnt = 0;
@@ -6231,58 +6231,84 @@ static u64 __meminit init_unavailable_range(unsigned long spfn,
62316231
pgcnt++;
62326232
}
62336233

6234-
return pgcnt;
6234+
if (pgcnt)
6235+
pr_info("On node %d, zone %s: %lld pages in unavailable ranges",
6236+
node, zone_names[zone], pgcnt);
62356237
}
62366238
#else
6237-
static inline u64 init_unavailable_range(unsigned long spfn, unsigned long epfn,
6238-
int zone, int node)
6239+
static inline void init_unavailable_range(unsigned long spfn,
6240+
unsigned long epfn,
6241+
int zone, int node)
62396242
{
6240-
return 0;
62416243
}
62426244
#endif
62436245

6244-
void __meminit __weak memmap_init(unsigned long size, int nid,
6245-
unsigned long zone,
6246-
unsigned long range_start_pfn)
6246+
static void __init memmap_init_zone_range(struct zone *zone,
6247+
unsigned long start_pfn,
6248+
unsigned long end_pfn,
6249+
unsigned long *hole_pfn)
6250+
{
6251+
unsigned long zone_start_pfn = zone->zone_start_pfn;
6252+
unsigned long zone_end_pfn = zone_start_pfn + zone->spanned_pages;
6253+
int nid = zone_to_nid(zone), zone_id = zone_idx(zone);
6254+
6255+
start_pfn = clamp(start_pfn, zone_start_pfn, zone_end_pfn);
6256+
end_pfn = clamp(end_pfn, zone_start_pfn, zone_end_pfn);
6257+
6258+
if (start_pfn >= end_pfn)
6259+
return;
6260+
6261+
memmap_init_zone(end_pfn - start_pfn, nid, zone_id, start_pfn,
6262+
zone_end_pfn, MEMINIT_EARLY, NULL, MIGRATE_MOVABLE);
6263+
6264+
if (*hole_pfn < start_pfn)
6265+
init_unavailable_range(*hole_pfn, start_pfn, zone_id, nid);
6266+
6267+
*hole_pfn = end_pfn;
6268+
}
6269+
6270+
void __init __weak memmap_init(void)
62476271
{
6248-
static unsigned long hole_pfn;
62496272
unsigned long start_pfn, end_pfn;
6250-
unsigned long range_end_pfn = range_start_pfn + size;
6251-
int i;
6252-
u64 pgcnt = 0;
6273+
unsigned long hole_pfn = 0;
6274+
int i, j, zone_id, nid;
62536275

6254-
for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
6255-
start_pfn = clamp(start_pfn, range_start_pfn, range_end_pfn);
6256-
end_pfn = clamp(end_pfn, range_start_pfn, range_end_pfn);
6276+
for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
6277+
struct pglist_data *node = NODE_DATA(nid);
62576278

6258-
if (end_pfn > start_pfn) {
6259-
size = end_pfn - start_pfn;
6260-
memmap_init_zone(size, nid, zone, start_pfn, range_end_pfn,
6261-
MEMINIT_EARLY, NULL, MIGRATE_MOVABLE);
6262-
}
6279+
for (j = 0; j < MAX_NR_ZONES; j++) {
6280+
struct zone *zone = node->node_zones + j;
6281+
6282+
if (!populated_zone(zone))
6283+
continue;
62636284

6264-
if (hole_pfn < start_pfn)
6265-
pgcnt += init_unavailable_range(hole_pfn, start_pfn,
6266-
zone, nid);
6267-
hole_pfn = end_pfn;
6285+
memmap_init_zone_range(zone, start_pfn, end_pfn,
6286+
&hole_pfn);
6287+
zone_id = j;
6288+
}
62686289
}
62696290

62706291
#ifdef CONFIG_SPARSEMEM
62716292
/*
6272-
* Initialize the hole in the range [zone_end_pfn, section_end].
6273-
* If zone boundary falls in the middle of a section, this hole
6274-
* will be re-initialized during the call to this function for the
6275-
* higher zone.
6293+
* Initialize the memory map for hole in the range [memory_end,
6294+
* section_end].
6295+
* Append the pages in this hole to the highest zone in the last
6296+
* node.
6297+
* The call to init_unavailable_range() is outside the ifdef to
6298+
* silence the compiler warining about zone_id set but not used;
6299+
* for FLATMEM it is a nop anyway
62766300
*/
6277-
end_pfn = round_up(range_end_pfn, PAGES_PER_SECTION);
6301+
end_pfn = round_up(end_pfn, PAGES_PER_SECTION);
62786302
if (hole_pfn < end_pfn)
6279-
pgcnt += init_unavailable_range(hole_pfn, end_pfn,
6280-
zone, nid);
62816303
#endif
6304+
init_unavailable_range(hole_pfn, end_pfn, zone_id, nid);
6305+
}
62826306

6283-
if (pgcnt)
6284-
pr_info(" %s zone: %llu pages in unavailable ranges\n",
6285-
zone_names[zone], pgcnt);
6307+
/* A stub for backwards compatibility with custom implementatin on IA-64 */
6308+
void __meminit __weak arch_memmap_init(unsigned long size, int nid,
6309+
unsigned long zone,
6310+
unsigned long range_start_pfn)
6311+
{
62866312
}
62876313

62886314
static int zone_batchsize(struct zone *zone)
@@ -6981,7 +7007,7 @@ static void __init free_area_init_core(struct pglist_data *pgdat)
69817007
set_pageblock_order();
69827008
setup_usemap(pgdat, zone, zone_start_pfn, size);
69837009
init_currently_empty_zone(zone, zone_start_pfn, size);
6984-
memmap_init(size, nid, j, zone_start_pfn);
7010+
arch_memmap_init(size, nid, j, zone_start_pfn);
69857011
}
69867012
}
69877013

@@ -7507,6 +7533,8 @@ void __init free_area_init(unsigned long *max_zone_pfn)
75077533
node_set_state(nid, N_MEMORY);
75087534
check_for_memory(pgdat, nid);
75097535
}
7536+
7537+
memmap_init();
75107538
}
75117539

75127540
static int __init cmdline_parse_core(char *p, unsigned long *core,

0 commit comments

Comments
 (0)