From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3D256C49EA6 for ; Thu, 24 Jun 2021 06:23:15 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id AD169613ED for ; Thu, 24 Jun 2021 06:23:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AD169613ED Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 693A76B008A; Thu, 24 Jun 2021 02:23:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6449D8D000E; Thu, 24 Jun 2021 02:23:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4E55C8D0008; Thu, 24 Jun 2021 02:23:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0119.hostedemail.com [216.40.44.119]) by kanga.kvack.org (Postfix) with ESMTP id 08E2E6B008A for ; Thu, 24 Jun 2021 02:23:12 -0400 (EDT) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 2AE3A181AF5C3 for ; Thu, 24 Jun 2021 06:23:13 +0000 (UTC) X-FDA: 78287625066.24.CD50C73 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf27.hostedemail.com (Postfix) with ESMTP id C0CB28019111 for ; Thu, 24 Jun 2021 06:23:12 +0000 (UTC) Received: by mail.kernel.org (Postfix) with ESMTPSA id 8FF95613EC; Thu, 24 Jun 2021 06:23:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1624515791; bh=gSOLRpys7/I6tHKjxLQhKmFMO1O29ImcyVQatrMGc9M=; h=From:To:Cc:Subject:Date:From; b=iHAcw5D3jk57lgAw73SkmsFOWyPSODiJRTAoWSKLAeQrvB3UZq2CH22myCxPzN/Ag 3y6xycvS1MrUv9Z5TEk3iHws6r65Ho2pS+DhPU/kK3zDX4/aq3k94GtqRTJDf+Cuzl vLwtGmsM4IJAHngmT3lHQgWeqRWepc36pfwN4/5ZwQmWWNJCyzN08d+Afq+fywajta cVT9s196/lYw10hPRH0A0buwR/CRhgpnQEUDLpeUDomVURcWBv0KtgLtmhOGVjRm8u R/fhn4K/bXr/m5GYi84tHSHpm6dubhV8lqmQ5BprNNjJclCgU4dvlDZb3lf9qsYIZS lA+CwU9YTw3vQ== From: Mike Rapoport To: Andrew Morton Cc: alfalco@gmail.com, Borislav Petkov , Mike Rapoport , Mike Rapoport , robert.shteynfeld@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH] mm/page_alloc: fix memory map initialization for descending nodes Date: Thu, 24 Jun 2021 09:23:05 +0300 Message-Id: <20210624062305.10940-1-rppt@kernel.org> X-Mailer: git-send-email 2.28.0 MIME-Version: 1.0 X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: C0CB28019111 Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=iHAcw5D3; spf=pass (imf27.hostedemail.com: domain of rppt@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org X-Stat-Signature: p5abb14qs6gfipogmizsgua876xeqxse X-HE-Tag: 1624515792-568725 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Mike Rapoport 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 i= n the middle of a section and will be considered as a hole during the initialization of the last section in node 1. Reorder order of the memory map initialization so that the outer loop wil= l always iterate over populated memory regions in the ascending order and t= he 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. Link: https://bugzilla.kernel.org/show_bug.cgi?id=3D213073 Fixes: 0740a50b9baa ("mm/page_alloc.c: refactor initialization of struct = page for holes in memory layout") Signed-off-by: Mike Rapoport --- include/linux/mm.h | 1 - mm/page_alloc.c | 94 ++++++++++++++++++++++++++++------------------ 2 files changed, 58 insertions(+), 37 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 8ae31622deef..9afb8998e7e5 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2474,7 +2474,6 @@ extern void set_dma_reserve(unsigned long new_dma_r= eserve); extern void memmap_init_range(unsigned long, int, unsigned long, unsigned long, unsigned long, enum meminit_context, struct vmem_altmap *, int migratetype); -extern void memmap_init_zone(struct zone *zone); extern void setup_per_zone_wmarks(void); extern int __meminit init_per_zone_wmark_min(void); extern void mem_init(void); diff --git a/mm/page_alloc.c b/mm/page_alloc.c index d1f5de1c1283..f6ea180ae119 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -6396,7 +6396,7 @@ void __ref memmap_init_zone_device(struct zone *zon= e, return; =20 /* - * The call to memmap_init_zone should have already taken care + * The call to memmap_init should have already taken care * of the pages reserved for the memmap, so we can just jump to * the end of that region and start processing the device pages. */ @@ -6461,7 +6461,7 @@ static void __meminit zone_init_free_lists(struct z= one *zone) /* * Only struct pages that correspond to ranges defined by memblock.memor= y * are zeroed and initialized by going through __init_single_page() duri= ng - * memmap_init_zone(). + * memmap_init_zone_range(). * * But, there could be struct pages that correspond to holes in * memblock.memory. This can happen because of the following reasons: @@ -6480,9 +6480,9 @@ static void __meminit zone_init_free_lists(struct z= one *zone) * zone/node above the hole except for the trailing pages in the last * section that will be appended to the zone/node below. */ -static u64 __meminit init_unavailable_range(unsigned long spfn, - unsigned long epfn, - int zone, int node) +static void __init init_unavailable_range(unsigned long spfn, + unsigned long epfn, + int zone, int node) { unsigned long pfn; u64 pgcnt =3D 0; @@ -6498,56 +6498,77 @@ static u64 __meminit init_unavailable_range(unsig= ned long spfn, pgcnt++; } =20 - return pgcnt; + if (pgcnt) + pr_info("On node %d, zone %s: %lld pages in unavailable ranges", + node, zone_names[zone], pgcnt); } #else -static inline u64 init_unavailable_range(unsigned long spfn, unsigned lo= ng epfn, - int zone, int node) +static inline void init_unavailable_range(unsigned long spfn, + unsigned long epfn, + int zone, int node) { - return 0; } #endif =20 -void __meminit __weak memmap_init_zone(struct zone *zone) +static void __init memmap_init_zone_range(struct zone *zone, + unsigned long start_pfn, + unsigned long end_pfn, + unsigned long *hole_pfn) { unsigned long zone_start_pfn =3D zone->zone_start_pfn; unsigned long zone_end_pfn =3D zone_start_pfn + zone->spanned_pages; - int i, nid =3D zone_to_nid(zone), zone_id =3D zone_idx(zone); - static unsigned long hole_pfn; + int nid =3D zone_to_nid(zone), zone_id =3D zone_idx(zone); + + start_pfn =3D clamp(start_pfn, zone_start_pfn, zone_end_pfn); + end_pfn =3D clamp(end_pfn, zone_start_pfn, zone_end_pfn); + + if (start_pfn >=3D end_pfn) + return; + + memmap_init_range(end_pfn - start_pfn, nid, zone_id, start_pfn, + zone_end_pfn, MEMINIT_EARLY, NULL, MIGRATE_MOVABLE); + + if (*hole_pfn < start_pfn) + init_unavailable_range(*hole_pfn, start_pfn, zone_id, nid); + + *hole_pfn =3D end_pfn; +} + +static void __init memmap_init(void) +{ unsigned long start_pfn, end_pfn; - u64 pgcnt =3D 0; + unsigned long hole_pfn =3D 0; + int i, j, zone_id, nid; =20 - for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) { - start_pfn =3D clamp(start_pfn, zone_start_pfn, zone_end_pfn); - end_pfn =3D clamp(end_pfn, zone_start_pfn, zone_end_pfn); + for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) { + struct pglist_data *node =3D NODE_DATA(nid); + + for (j =3D 0; j < MAX_NR_ZONES; j++) { + struct zone *zone =3D node->node_zones + j; =20 - if (end_pfn > start_pfn) - memmap_init_range(end_pfn - start_pfn, nid, - zone_id, start_pfn, zone_end_pfn, - MEMINIT_EARLY, NULL, MIGRATE_MOVABLE); + if (!populated_zone(zone)) + continue; =20 - if (hole_pfn < start_pfn) - pgcnt +=3D init_unavailable_range(hole_pfn, start_pfn, - zone_id, nid); - hole_pfn =3D end_pfn; + memmap_init_zone_range(zone, start_pfn, end_pfn, + &hole_pfn); + zone_id =3D j; + } } =20 #ifdef CONFIG_SPARSEMEM /* - * Initialize the hole in the range [zone_end_pfn, section_end]. - * If zone boundary falls in the middle of a section, this hole - * will be re-initialized during the call to this function for the - * higher zone. + * Initialize the memory map for hole in the range [memory_end, + * section_end]. + * Append the pages in this hole to the highest zone in the last + * node. + * The call to init_unavailable_range() is outside the ifdef to + * silence the compiler warining about zone_id set but not used; + * for FLATMEM it is is nop anyway */ - end_pfn =3D round_up(zone_end_pfn, PAGES_PER_SECTION); + end_pfn =3D round_up(end_pfn, PAGES_PER_SECTION); if (hole_pfn < end_pfn) - pgcnt +=3D init_unavailable_range(hole_pfn, end_pfn, - zone_id, nid); #endif - - if (pgcnt) - pr_info(" %s zone: %llu pages in unavailable ranges\n", - zone->name, pgcnt); + init_unavailable_range(hole_pfn, end_pfn, zone_id, nid); } =20 static int zone_batchsize(struct zone *zone) @@ -7250,7 +7271,6 @@ static void __init free_area_init_core(struct pglis= t_data *pgdat) set_pageblock_order(); setup_usemap(zone); init_currently_empty_zone(zone, zone->zone_start_pfn, size); - memmap_init_zone(zone); } } =20 @@ -7776,6 +7796,8 @@ void __init free_area_init(unsigned long *max_zone_= pfn) node_set_state(nid, N_MEMORY); check_for_memory(pgdat, nid); } + + memmap_init(); } =20 static int __init cmdline_parse_core(char *p, unsigned long *core, --=20 2.28.0