Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp932226rwd; Thu, 15 Jun 2023 04:16:39 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6lpoXpYaOQh3itOg37aVSOSB0HBeQkZ3CWb77NXT7utbrqLRiFMg87Qbf+U2puMFkuGuI7 X-Received: by 2002:a17:903:22ca:b0:1b3:cbc9:3bcc with SMTP id y10-20020a17090322ca00b001b3cbc93bccmr14462604plg.50.1686827799185; Thu, 15 Jun 2023 04:16:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686827799; cv=none; d=google.com; s=arc-20160816; b=Yoz2BUrVpdfl+NfPz2JV1VYbsNkPVaAYHEBcH7KCpxCiLgzSLJ0Qiub7vgB+834G0C K4aMazYs7K3LJIx5XrDjt+Fq5A6hqc13FIzKQh1No2K8WMxlqHeCOjMPOwD5vRzS69J+ XxL6oaIftwvYo+f+x1gEtdt1xpSFnVEHSt5ORsVEko5cEKkAlskuVNGH6WN7iXF/0BAz wkvu9QfaT30sSxL2S0yMWusFijUhPn9f6ZRFkZ7P7gEwDX1YwM9zELk/VipvTdjMbe06 Zt1wTYc7HbSxmIx86gtnt1Oah6bxm5mtCFgE2a7QaeJHSR2gNst661+h1QpHPGcZZV0H 97tg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=HXlRY/NnvVbWtv6wBhmhQHVgYiXefGsRRYFYStrDpdY=; b=jIQKbYAGMOoHcVfKx71tB8JGdJ6CYR5G8+DvASDVsIo8ZW9zy/kag09ymKJ1uBoIs/ 14ODxWcXNDLmNEfEytqBVQ+yLlOpqIep/fjUCSlAW4mn9V7yYtyODwrWc0TWGmoa7IxQ Dk9+2d3J2EdXJWe1vNKQCwB7Y+R4XufR6eEsyqz+PYPBTYnLWCBMVkHNomv0DnInw5B2 bHJVexiQZXCc2zPHctusDdkzV7ykjpgE7YgyPP3HKyJ5tXxkthODjEMZFCwZ6o0xnJwM LXUhfZ25Zoe89uMHfcIEe0xzv1/BpBB8nxbcSKvq0CSrZjWX/p6NsriyFkG/I3PpNHRm MWMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=IwVTiMVl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ky3-20020a170902f98300b001b06038e1b2si6439386plb.546.2023.06.15.04.16.26; Thu, 15 Jun 2023 04:16:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=IwVTiMVl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241906AbjFOLEO (ORCPT + 99 others); Thu, 15 Jun 2023 07:04:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238789AbjFOLEL (ORCPT ); Thu, 15 Jun 2023 07:04:11 -0400 Received: from out-37.mta0.migadu.com (out-37.mta0.migadu.com [91.218.175.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92B882711 for ; Thu, 15 Jun 2023 04:04:09 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1686827047; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=HXlRY/NnvVbWtv6wBhmhQHVgYiXefGsRRYFYStrDpdY=; b=IwVTiMVlmdoBf+k0CgBzwHvE5l+QAJfdPLPFzzIto9INy4+dzmm8mVOLpIgIe935EyPXo5 Eni9VHiT3AR3SV5BhfM8g9s0PLUHo26qpRkOgcK4A6KcE8Q3IXe2PyIx4K5mGmYh+Xz3Ys SdiZ2JNbST6+KC80ypO+aVW0mi3VimQ= From: Yajun Deng To: akpm@linux-foundation.org, rppt@kernel.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yajun Deng Subject: [PATCH] mm: pass nid to reserve_bootmem_region() Date: Thu, 15 Jun 2023 19:03:55 +0800 Message-Id: <20230615110355.1694461-1-yajun.deng@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org early_pfn_to_nid() is called frequently in init_reserved_page(), it is used to convert PFN to node id. These PFN may belong to the same memory region, they have the same node id. It is not necessary to call early_pfn_to_nid() for each PFN. Pass nid to reserve_bootmem_region() and drop the call to early_pfn_to_nid() in init_reserved_page(). The following data was tested on my machine, it has 190GB RAM, and enable CONFIG_DEFERRED_STRUCT_PAGE_INIT. before the patch: memmap_init_reserved_pages() 67ms after the patch: memmap_init_reserved_pages() 20ms Signed-off-by: Yajun Deng --- include/linux/mm.h | 3 ++- mm/memblock.c | 9 ++++++--- mm/mm_init.c | 31 +++++++++++++++++++------------ 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 17317b1673b0..39e72ca6bf22 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2964,7 +2964,8 @@ extern unsigned long free_reserved_area(void *start, void *end, extern void adjust_managed_page_count(struct page *page, long count); -extern void reserve_bootmem_region(phys_addr_t start, phys_addr_t end); +extern void reserve_bootmem_region(phys_addr_t start, + phys_addr_t end, int nid); /* Free the reserved page into the buddy system, so it gets managed. */ static inline void free_reserved_page(struct page *page) diff --git a/mm/memblock.c b/mm/memblock.c index ff0da1858778..9e9b9a3473f9 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -2091,18 +2091,21 @@ static void __init memmap_init_reserved_pages(void) { struct memblock_region *region; phys_addr_t start, end; + int nid; u64 i; /* initialize struct pages for the reserved regions */ - for_each_reserved_mem_range(i, &start, &end) - reserve_bootmem_region(start, end); + __for_each_mem_range(i, &memblock.reserved, NULL, NUMA_NO_NODE, + MEMBLOCK_NONE, &start, &end, &nid) + reserve_bootmem_region(start, end, nid); /* and also treat struct pages for the NOMAP regions as PageReserved */ for_each_mem_region(region) { if (memblock_is_nomap(region)) { start = region->base; end = start + region->size; - reserve_bootmem_region(start, end); + nid = region->nid; + reserve_bootmem_region(start, end, nid); } } } diff --git a/mm/mm_init.c b/mm/mm_init.c index d393631599a7..1499efbebc6f 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -646,10 +646,8 @@ static inline void pgdat_set_deferred_range(pg_data_t *pgdat) } /* Returns true if the struct page for the pfn is initialised */ -static inline bool __meminit early_page_initialised(unsigned long pfn) +static inline bool __meminit early_page_initialised(unsigned long pfn, int nid) { - int nid = early_pfn_to_nid(pfn); - if (node_online(nid) && pfn >= NODE_DATA(nid)->first_deferred_pfn) return false; @@ -695,15 +693,14 @@ defer_init(int nid, unsigned long pfn, unsigned long end_pfn) return false; } -static void __meminit init_reserved_page(unsigned long pfn) +static void __meminit init_reserved_page(unsigned long pfn, int nid) { pg_data_t *pgdat; - int nid, zid; + int zid; - if (early_page_initialised(pfn)) + if (early_page_initialised(pfn, nid)) return; - nid = early_pfn_to_nid(pfn); pgdat = NODE_DATA(nid); for (zid = 0; zid < MAX_NR_ZONES; zid++) { @@ -717,7 +714,7 @@ static void __meminit init_reserved_page(unsigned long pfn) #else static inline void pgdat_set_deferred_range(pg_data_t *pgdat) {} -static inline bool early_page_initialised(unsigned long pfn) +static inline bool early_page_initialised(unsigned long pfn, int nid) { return true; } @@ -727,7 +724,7 @@ static inline bool defer_init(int nid, unsigned long pfn, unsigned long end_pfn) return false; } -static inline void init_reserved_page(unsigned long pfn) +static inline void init_reserved_page(unsigned long pfn, int nid) { } #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ @@ -738,16 +735,20 @@ static inline void init_reserved_page(unsigned long pfn) * marks the pages PageReserved. The remaining valid pages are later * sent to the buddy page allocator. */ -void __meminit reserve_bootmem_region(phys_addr_t start, phys_addr_t end) +void __meminit reserve_bootmem_region(phys_addr_t start, + phys_addr_t end, int nid) { unsigned long start_pfn = PFN_DOWN(start); unsigned long end_pfn = PFN_UP(end); + if (nid == MAX_NUMNODES) + nid = first_online_node; + for (; start_pfn < end_pfn; start_pfn++) { if (pfn_valid(start_pfn)) { struct page *page = pfn_to_page(start_pfn); - init_reserved_page(start_pfn); + init_reserved_page(start_pfn, nid); /* Avoid false-positive PageTail() */ INIT_LIST_HEAD(&page->lru); @@ -2579,7 +2580,13 @@ void __init set_dma_reserve(unsigned long new_dma_reserve) void __init memblock_free_pages(struct page *page, unsigned long pfn, unsigned int order) { - if (!early_page_initialised(pfn)) + int nid = 0; + +#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT + nid = early_pfn_to_nid(pfn); +#endif + + if (!early_page_initialised(pfn, nid)) return; if (!kmsan_memblock_free_pages(page, order)) { /* KMSAN will take care of these pages. */ -- 2.25.1