Skip to content

Commit 4c84191

Browse files
rpptgregkh
authored andcommitted
mm/page_alloc.c: refactor initialization of struct page for holes in memory layout
commit 0740a50 upstream. There could be struct pages that are not backed by actual physical memory. This can happen when the actual memory bank is not a multiple of SECTION_SIZE or when an architecture does not register memory holes reserved by the firmware as memblock.memory. Such pages are currently initialized using init_unavailable_mem() function that iterates through PFNs in holes in memblock.memory and if there is a struct page corresponding to a PFN, the fields of this page are set to default values and it is marked as Reserved. init_unavailable_mem() does not take into account zone and node the page belongs to and sets both zone and node links in struct page to zero. Before commit 73a6e47 ("mm: memmap_init: iterate over memblock regions rather that check each PFN") the holes inside a zone were re-initialized during memmap_init() and got their zone/node links right. However, after that commit nothing updates the struct pages representing such holes. On a system that has firmware reserved holes in a zone above ZONE_DMA, for instance in a configuration below: # grep -A1 E820 /proc/iomem 7a17b000-7a216fff : Unknown E820 type 7a217000-7bffffff : System RAM unset zone link in struct page will trigger VM_BUG_ON_PAGE(!zone_spans_pfn(page_zone(page), pfn), page); in set_pfnblock_flags_mask() when called with a struct page from a range other than E820_TYPE_RAM because there are pages in the range of ZONE_DMA32 but the unset zone link in struct page makes them appear as a part of ZONE_DMA. Interleave initialization of the unavailable pages with the normal initialization of memory map, so that zone and node information will be properly set on struct pages that are not backed by the actual memory. With this change the pages for holes inside a zone will get proper zone/node links and the pages that are not spanned by any node will get links to the adjacent zone/node. The holes between nodes will be prepended to the zone/node above the hole and the trailing pages in the last section that will be appended to the zone/node below. [akpm@linux-foundation.org: don't initialize static to zero, use %llu for u64] Link: https://lkml.kernel.org/r/20210225224351.7356-2-rppt@kernel.org Fixes: 73a6e47 ("mm: memmap_init: iterate over memblock regions rather that check each PFN") Signed-off-by: Mike Rapoport <rppt@linux.ibm.com> Reported-by: Qian Cai <cai@lca.pw> Reported-by: Andrea Arcangeli <aarcange@redhat.com> Reviewed-by: Baoquan He <bhe@redhat.com> Acked-by: Vlastimil Babka <vbabka@suse.cz> Reviewed-by: David Hildenbrand <david@redhat.com> Cc: Borislav Petkov <bp@alien8.de> Cc: Chris Wilson <chris@chris-wilson.co.uk> Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Łukasz Majczak <lma@semihalf.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Mel Gorman <mgorman@suse.de> Cc: Michal Hocko <mhocko@kernel.org> Cc: "Sarvela, Tomi P" <tomi.p.sarvela@intel.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Mike Rapoport <rppt@linux.ibm.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent e7afadd commit 4c84191

1 file changed

Lines changed: 75 additions & 83 deletions

File tree

‎mm/page_alloc.c‎

Lines changed: 75 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -6189,13 +6189,66 @@ static void __meminit zone_init_free_lists(struct zone *zone)
61896189
}
61906190
}
61916191

6192+
#if !defined(CONFIG_FLAT_NODE_MEM_MAP)
6193+
/*
6194+
* Only struct pages that correspond to ranges defined by memblock.memory
6195+
* are zeroed and initialized by going through __init_single_page() during
6196+
* memmap_init_zone().
6197+
*
6198+
* But, there could be struct pages that correspond to holes in
6199+
* memblock.memory. This can happen because of the following reasons:
6200+
* - physical memory bank size is not necessarily the exact multiple of the
6201+
* arbitrary section size
6202+
* - early reserved memory may not be listed in memblock.memory
6203+
* - memory layouts defined with memmap= kernel parameter may not align
6204+
* nicely with memmap sections
6205+
*
6206+
* Explicitly initialize those struct pages so that:
6207+
* - PG_Reserved is set
6208+
* - zone and node links point to zone and node that span the page if the
6209+
* hole is in the middle of a zone
6210+
* - zone and node links point to adjacent zone/node if the hole falls on
6211+
* the zone boundary; the pages in such holes will be prepended to the
6212+
* zone/node above the hole except for the trailing pages in the last
6213+
* section that will be appended to the zone/node below.
6214+
*/
6215+
static u64 __meminit init_unavailable_range(unsigned long spfn,
6216+
unsigned long epfn,
6217+
int zone, int node)
6218+
{
6219+
unsigned long pfn;
6220+
u64 pgcnt = 0;
6221+
6222+
for (pfn = spfn; pfn < epfn; pfn++) {
6223+
if (!pfn_valid(ALIGN_DOWN(pfn, pageblock_nr_pages))) {
6224+
pfn = ALIGN_DOWN(pfn, pageblock_nr_pages)
6225+
+ pageblock_nr_pages - 1;
6226+
continue;
6227+
}
6228+
__init_single_page(pfn_to_page(pfn), pfn, zone, node);
6229+
__SetPageReserved(pfn_to_page(pfn));
6230+
pgcnt++;
6231+
}
6232+
6233+
return pgcnt;
6234+
}
6235+
#else
6236+
static inline u64 init_unavailable_range(unsigned long spfn, unsigned long epfn,
6237+
int zone, int node)
6238+
{
6239+
return 0;
6240+
}
6241+
#endif
6242+
61926243
void __meminit __weak memmap_init(unsigned long size, int nid,
61936244
unsigned long zone,
61946245
unsigned long range_start_pfn)
61956246
{
6247+
static unsigned long hole_pfn;
61966248
unsigned long start_pfn, end_pfn;
61976249
unsigned long range_end_pfn = range_start_pfn + size;
61986250
int i;
6251+
u64 pgcnt = 0;
61996252

62006253
for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
62016254
start_pfn = clamp(start_pfn, range_start_pfn, range_end_pfn);
@@ -6206,7 +6259,29 @@ void __meminit __weak memmap_init(unsigned long size, int nid,
62066259
memmap_init_zone(size, nid, zone, start_pfn, range_end_pfn,
62076260
MEMINIT_EARLY, NULL, MIGRATE_MOVABLE);
62086261
}
6262+
6263+
if (hole_pfn < start_pfn)
6264+
pgcnt += init_unavailable_range(hole_pfn, start_pfn,
6265+
zone, nid);
6266+
hole_pfn = end_pfn;
62096267
}
6268+
6269+
#ifdef CONFIG_SPARSEMEM
6270+
/*
6271+
* Initialize the hole in the range [zone_end_pfn, section_end].
6272+
* If zone boundary falls in the middle of a section, this hole
6273+
* will be re-initialized during the call to this function for the
6274+
* higher zone.
6275+
*/
6276+
end_pfn = round_up(range_end_pfn, PAGES_PER_SECTION);
6277+
if (hole_pfn < end_pfn)
6278+
pgcnt += init_unavailable_range(hole_pfn, end_pfn,
6279+
zone, nid);
6280+
#endif
6281+
6282+
if (pgcnt)
6283+
pr_info(" %s zone: %llu pages in unavailable ranges\n",
6284+
zone_names[zone], pgcnt);
62106285
}
62116286

62126287
static int zone_batchsize(struct zone *zone)
@@ -6999,88 +7074,6 @@ void __init free_area_init_memoryless_node(int nid)
69997074
free_area_init_node(nid);
70007075
}
70017076

7002-
#if !defined(CONFIG_FLAT_NODE_MEM_MAP)
7003-
/*
7004-
* Initialize all valid struct pages in the range [spfn, epfn) and mark them
7005-
* PageReserved(). Return the number of struct pages that were initialized.
7006-
*/
7007-
static u64 __init init_unavailable_range(unsigned long spfn, unsigned long epfn)
7008-
{
7009-
unsigned long pfn;
7010-
u64 pgcnt = 0;
7011-
7012-
for (pfn = spfn; pfn < epfn; pfn++) {
7013-
if (!pfn_valid(ALIGN_DOWN(pfn, pageblock_nr_pages))) {
7014-
pfn = ALIGN_DOWN(pfn, pageblock_nr_pages)
7015-
+ pageblock_nr_pages - 1;
7016-
continue;
7017-
}
7018-
/*
7019-
* Use a fake node/zone (0) for now. Some of these pages
7020-
* (in memblock.reserved but not in memblock.memory) will
7021-
* get re-initialized via reserve_bootmem_region() later.
7022-
*/
7023-
__init_single_page(pfn_to_page(pfn), pfn, 0, 0);
7024-
__SetPageReserved(pfn_to_page(pfn));
7025-
pgcnt++;
7026-
}
7027-
7028-
return pgcnt;
7029-
}
7030-
7031-
/*
7032-
* Only struct pages that are backed by physical memory are zeroed and
7033-
* initialized by going through __init_single_page(). But, there are some
7034-
* struct pages which are reserved in memblock allocator and their fields
7035-
* may be accessed (for example page_to_pfn() on some configuration accesses
7036-
* flags). We must explicitly initialize those struct pages.
7037-
*
7038-
* This function also addresses a similar issue where struct pages are left
7039-
* uninitialized because the physical address range is not covered by
7040-
* memblock.memory or memblock.reserved. That could happen when memblock
7041-
* layout is manually configured via memmap=, or when the highest physical
7042-
* address (max_pfn) does not end on a section boundary.
7043-
*/
7044-
static void __init init_unavailable_mem(void)
7045-
{
7046-
phys_addr_t start, end;
7047-
u64 i, pgcnt;
7048-
phys_addr_t next = 0;
7049-
7050-
/*
7051-
* Loop through unavailable ranges not covered by memblock.memory.
7052-
*/
7053-
pgcnt = 0;
7054-
for_each_mem_range(i, &start, &end) {
7055-
if (next < start)
7056-
pgcnt += init_unavailable_range(PFN_DOWN(next),
7057-
PFN_UP(start));
7058-
next = end;
7059-
}
7060-
7061-
/*
7062-
* Early sections always have a fully populated memmap for the whole
7063-
* section - see pfn_valid(). If the last section has holes at the
7064-
* end and that section is marked "online", the memmap will be
7065-
* considered initialized. Make sure that memmap has a well defined
7066-
* state.
7067-
*/
7068-
pgcnt += init_unavailable_range(PFN_DOWN(next),
7069-
round_up(max_pfn, PAGES_PER_SECTION));
7070-
7071-
/*
7072-
* Struct pages that do not have backing memory. This could be because
7073-
* firmware is using some of this memory, or for some other reasons.
7074-
*/
7075-
if (pgcnt)
7076-
pr_info("Zeroed struct page in unavailable ranges: %lld pages", pgcnt);
7077-
}
7078-
#else
7079-
static inline void __init init_unavailable_mem(void)
7080-
{
7081-
}
7082-
#endif /* !CONFIG_FLAT_NODE_MEM_MAP */
7083-
70847077
#if MAX_NUMNODES > 1
70857078
/*
70867079
* Figure out the number of possible node ids.
@@ -7504,7 +7497,6 @@ void __init free_area_init(unsigned long *max_zone_pfn)
75047497
/* Initialise every node */
75057498
mminit_verify_pageflags_layout();
75067499
setup_nr_node_ids();
7507-
init_unavailable_mem();
75087500
for_each_online_node(nid) {
75097501
pg_data_t *pgdat = NODE_DATA(nid);
75107502
free_area_init_node(nid);

0 commit comments

Comments
 (0)