From patchwork Sun Jun 18 05:30:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yajun Deng X-Patchwork-Id: 13283754 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 26F76EB64D8 for ; Sun, 18 Jun 2023 05:30:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 862396B0072; Sun, 18 Jun 2023 01:30:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 813BF8E0002; Sun, 18 Jun 2023 01:30:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 702378E0001; Sun, 18 Jun 2023 01:30:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 5FA436B0072 for ; Sun, 18 Jun 2023 01:30:20 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D3DF71C90DD for ; Sun, 18 Jun 2023 05:30:19 +0000 (UTC) X-FDA: 80914742958.23.FA634DD Received: from out-48.mta0.migadu.com (out-48.mta0.migadu.com [91.218.175.48]) by imf11.hostedemail.com (Postfix) with ESMTP id D5C3A4000B for ; Sun, 18 Jun 2023 05:30:17 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=LeJfEXum; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf11.hostedemail.com: domain of yajun.deng@linux.dev designates 91.218.175.48 as permitted sender) smtp.mailfrom=yajun.deng@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687066218; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=aBYmdBEH1qB16YD6TQS5RIcWSGINKfp80AeFzQ6XOsg=; b=p1x8qUlfnSAeUnkbNqElL9DbNZHuRFiobr2urEQu7oIfytMW7OCqrQ0U2+gcWvAFMYxxaP sGmsBW1wSlAMNGEWqtHAyjtyS3QZpIOrT+mIeOMh0sigM2Mb6ZighoyKnuJWJS5onVgFJ8 k1/8HOegiLxl6FFFsO80ILwXCEk01tE= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=LeJfEXum; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf11.hostedemail.com: domain of yajun.deng@linux.dev designates 91.218.175.48 as permitted sender) smtp.mailfrom=yajun.deng@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687066218; a=rsa-sha256; cv=none; b=lwXo5MkrwG0FuEcCLoE2CdC8taS8dUSgZgf67Hnnr7IgH54ay+Kwryi7WR08Dseh+1AzOy LKqZye9FEmWCt00z2smsE8vVVGw2xeT0GtgJjTVvflx9uJGcK2evEpGjEtsyuFr5tBrUvp nathzKwOmoOayb7tZBZdppDvBBXDe7Y= 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=1687066215; 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=aBYmdBEH1qB16YD6TQS5RIcWSGINKfp80AeFzQ6XOsg=; b=LeJfEXumxxsSmSrwA0/6hbN57mvPaEOYLtVkUp8a/TJOPWUAPG8qb6FLyim8+6JeHI+yCG aKNFs7on1MViZ4jWWaAkOl1l9kvJ7LsMDQMeYm8dLPGtsN4CfrmVqXsZ2m3CEyJ1cEsDmV Niape9HU++wF2Rz6h2u8UEW0rlZnJLA= From: Yajun Deng To: akpm@linux-foundation.org, rppt@kernel.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yajun Deng , kernel test robot Subject: [PATCH v3] mm: pass nid to reserve_bootmem_region() Date: Sun, 18 Jun 2023 13:30:00 +0800 Message-Id: <20230618053000.2930604-1-yajun.deng@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: D5C3A4000B X-Stat-Signature: dh9bqsczkoxgkq3edcnnkiyi4xzzdcur X-Rspam-User: X-HE-Tag: 1687066217-854100 X-HE-Meta: U2FsdGVkX196E5t3NYIap5yIXZgAWgIfGJMI4ckeubIsyFh6Ssm73YchGhxoXotWv89GRn85u0ZK2KoQTgdnKNymp4zvtCMH0DPnSIZkk2r/ItxxGhlsJ/9Ypa5hm+cuN8cGGYCNbLMhG9DTC90sv8kTiYhm1abbxXYhuodCYpZAE6dHDKpYzrfZjXrhvIGf9o4vNrhG51kH1Fzpf4hSKzPYRli/DYi2w39nizQafMFqAPXs/2hgmSIjI61FnU3xFQK9+IhslbJOIpVKjI5FAHEYVDoirUcJgFvt00vJ1dSb8UhVPQbPx5Ltwaw4iD0jw94SHmwrd9xIoQONEzNAQ3xQzp+254gpxCCki3QsU0/x8d1bUwKLWYNs8/AKHVv1Uzc2EUWwTAY06sKgZz5i45MLKaUPbXrdvcdlAEZFKK3keOklfIaj2svbRcVQQ2xP1mXwY7Me/vgAKhLlOAq//C8969DnJbx1/bj7VyTfd5Qgu6a0Ou+WqGNEPkeMs1hnV2blJREqUQFxogxO9TVy0O7HTC5YmNT0+5le933dfZlLCRDMf0W1JDg5i9dr8I4z+pEoW1jAtfehL99Ytsyu6bbJLxRaSFfDJWza7kqxU1oifWcOp0z016Y2XgAWMBRXEp0vA3lNWZEVFMuwTHD11ZnP/DNKx38HqorYXOPXQVOKX5EenoB78QKOP1bjcMpuqJ5KJZxQLl9kd7esIqVOLYiTOjObi/wuLS0pG5LJhaYLtjazqvfj+DFPRqrP5y8v4TZwEv2A87PA2gZfbW500whpUEJGiOXLSsA7rsbipou666MdMbQH+c/+pMgTT4CMMn8FiTSMrui3jP3vP5iccFbFzkAbRnE2yEko1VrTvZ5X7kEO3VJEuPpWZvapODr0n63aqYaGbz4zF5V2E1mhOX6pDcWkpPBXWP0LaVEB8D4v22hU5Q/inymiH5zrU258fOIV4HX4inZtVgzySqZ UYcrP61l hSWr+2pyfCexez2c98jyeSJsDOeMP0un/YelGh+EkDsztkeRO1VH/J3xmiKHRsvc33efmTF232sszZ3mEQJnbnQyPflUeJ+nAHhr9m43F4GE8FfHVsf4VZkgNw+QEWwo00JlLmYNLYusgUTApaBJBuFqA2MkOeyEyHZfw2HTjl0dtTv/8D6+iZfAKBDC0O9tObSgd3hBH2Uidk59Qqe6xa2yW/A== 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: early_pfn_to_nid() is called frequently in init_reserved_page(), it returns the node id of the PFN. These PFN are probably from the same memory region, they have the same node id. It's 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(). Also, set nid on all reserved pages before doing this, as some reserved memory regions may not be set nid. The most beneficial function is memmap_init_reserved_pages() if CONFIG_DEFERRED_STRUCT_PAGE_INIT is enabled. The following data was tested on an x86 machine with 190GB of RAM. before: memmap_init_reserved_pages() 67ms after: memmap_init_reserved_pages() 20ms Signed-off-by: Yajun Deng Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-kbuild-all/202306160145.juJMr3Bi-lkp@intel.com Reviewed-by: Mike Rapoport (IBM) --- include/linux/mm.h | 3 ++- mm/memblock.c | 31 +++++++++++++++++++++---------- mm/mm_init.c | 27 +++++++++++++++------------ 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index fdd966b11f79..a7a0e692d44d 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2960,7 +2960,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..f9e61e565a53 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -2091,19 +2091,30 @@ static void __init memmap_init_reserved_pages(void) { struct memblock_region *region; phys_addr_t start, end; - u64 i; + int nid; + + /* + * set nid on all reserved pages and also treat struct + * pages for the NOMAP regions as PageReserved + */ + for_each_mem_region(region) { + nid = memblock_get_region_node(region); + start = region->base; + end = start + region->size; + + if (memblock_is_nomap(region)) + reserve_bootmem_region(start, end, nid); + + memblock_set_node(start, end, &memblock.reserved, nid); + } /* initialize struct pages for the reserved regions */ - for_each_reserved_mem_range(i, &start, &end) - reserve_bootmem_region(start, end); + for_each_reserved_mem_region(region) { + nid = memblock_get_region_node(region); + start = region->base; + end = start + region->size; - /* 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); - } + reserve_bootmem_region(start, end, nid); } } diff --git a/mm/mm_init.c b/mm/mm_init.c index d393631599a7..06cd5f93f9e7 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,7 +735,8 @@ 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); @@ -747,7 +745,7 @@ void __meminit reserve_bootmem_region(phys_addr_t start, phys_addr_t end) 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 +2577,12 @@ 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; + + if (IS_ENABLED(CONFIG_DEFERRED_STRUCT_PAGE_INIT)) + nid = early_pfn_to_nid(pfn); + + if (!early_page_initialised(pfn, nid)) return; if (!kmsan_memblock_free_pages(page, order)) { /* KMSAN will take care of these pages. */