From patchwork Fri Sep 22 07:09:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yajun Deng X-Patchwork-Id: 13395077 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 8F2DCE7D0D6 for ; Fri, 22 Sep 2023 07:10:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3221F6B028E; Fri, 22 Sep 2023 03:10:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2D18E6B028F; Fri, 22 Sep 2023 03:10:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1C1086B0290; Fri, 22 Sep 2023 03:10:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 0C88F6B028E for ; Fri, 22 Sep 2023 03:10:21 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E5C5840976 for ; Fri, 22 Sep 2023 07:10:20 +0000 (UTC) X-FDA: 81263359800.03.564A2AD Received: from out-212.mta0.migadu.com (out-212.mta0.migadu.com [91.218.175.212]) by imf04.hostedemail.com (Postfix) with ESMTP id 284914001D for ; Fri, 22 Sep 2023 07:10:18 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=AFYpk1uq; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of yajun.deng@linux.dev designates 91.218.175.212 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=1695366619; 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:in-reply-to:references:references:dkim-signature; bh=YklfUpteE5FTVxoZIVXyNtzYnXnzzLxI2l0gg0fMR4U=; b=Wj5SoT3g+Wnwvh1rQHKWD+MtayyR/ZrL4BR7+OGYwqmghyi+gXlP84LSxdVaAvZuIA9wPX /i0V7tNObu6DxeGP3NawPQi/JIN/9i/fuuX/SX4V8XqTLBI57IefTazZIgjSz4OfRlCxxZ l9u2DHEzkU0T2MwY9kPveRF0v1axADo= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=AFYpk1uq; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of yajun.deng@linux.dev designates 91.218.175.212 as permitted sender) smtp.mailfrom=yajun.deng@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695366619; a=rsa-sha256; cv=none; b=OdeB+tx9WQx8CMlrOezQLmymqVZSpJxAweuIt/8oEdM7WT8TXWeaPrPihAj1c0867kPE4g ojkCVswUGYY2pGaRf1mesVc8lnSVzivE9yYYD6JH6crM5U74w1guNHIH0BE2UDKcepcQsj HjYkMqWMnFbybYVVpA9Ne5k23p4Z4Gg= 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=1695366617; 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: in-reply-to:in-reply-to:references:references; bh=YklfUpteE5FTVxoZIVXyNtzYnXnzzLxI2l0gg0fMR4U=; b=AFYpk1uq44SipDFiOBO/r4CBpTdJf11xLhKC3MBb7kaBEY5+vQeckQrlIU+3y3vfuW33xS T2mK50ZEPqJJLFI3vGfPNpey/+U8/pdmze2yWsY1x79c7yi/xA5LllfZ122JnR+CSx0glc RY5VQg6TmowLlVqe3uOL2GyJOGwdJA4= From: Yajun Deng To: akpm@linux-foundation.org, mike.kravetz@oracle.com, muchun.song@linux.dev, glider@google.com, elver@google.com, dvyukov@google.com, rppt@kernel.org, david@redhat.com, osalvador@suse.de Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, Yajun Deng Subject: [PATCH 3/4] mm: Set page count and mark page reserved in reserve_bootmem_region Date: Fri, 22 Sep 2023 15:09:22 +0800 Message-Id: <20230922070923.355656-4-yajun.deng@linux.dev> In-Reply-To: <20230922070923.355656-1-yajun.deng@linux.dev> References: <20230922070923.355656-1-yajun.deng@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 284914001D X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: qtodnxbgmr3stgxc8grebiph1xpz9nw4 X-HE-Tag: 1695366618-425087 X-HE-Meta: U2FsdGVkX18mhjO9w7w3CIpN8iB2xFlRstY0TPptqFzdI9c5ChSt83Psm3lYznJk7hmEVUCp/OjgqmxhGO6rbO80bMyBuvUDQeZY1SwLi34PjwfinQvpXTFq5DYCtYvCkdS8cjXSEtXD48ULYx7LeHPyaypUe1w8LqddJJWMEp9kduVoLx7NhlVe1yvSfB4tkZYFqMEwcyLW+VIq+dm3SD6U305NAs/0KiOkEVQdpRhuvTGwDDNLjus4P+pdT/hLbqjiOvlUP9XiH/UqQet6fiYOlQnxG3TKaQp9D8FWZUeLvLxqiQp7b8cy+WYjhuCwhx1gWupaGnf+SoYUPs3q3zA1TtwIXfTNyZqLpI/xpQyUoz4u37W1SA49eL6PBDVZtgAwwmVv0VOsI/dGB9zzfHGKr1LDE9fO9x6y284abeCxRCHaIxDBD5wuDTURZGdVL73gKxHgJdK399eu+i7Kr+nwja1UdwDkAYKlueS9Cr1qbp5FydKaVcTZE8BtqRNeWKF7T3cUHwQxJUYB0/t2P+5UOijjrv62VF7lfVd3ia7+0PB8viqAgm6clo0dnN3PWdQr9Ug/FZE8e7Ty2DUs/k/frnPUFyMmLZYc9YU7epCbQOcD+mPYiXsoMfhHAh9Ualn+3P5wBOD+2gvT+f2uprla2aa9psGD236pFN9dytixQLIxakRSXE+N/yuSPCYQ9U+rgQJFHkwGL2YF7mTAJXoGslsBzZCTqvH5aGpZiBNwnVZIEJyQO/3K6Y02JhYad/I9RH/FJb3ap8OMIVW2d3dliRy1WrOaehJme3MKjMGk4O+ApgGbDqNI0AUkglCWQiN8p6AihgOzEbC2j1EUR7549t5PK3SpqbqJNtKt8EveqGHLNlqkCNRh0wnZohxtSG2e9qrSmlD1gsQGjZ+sk76pxrc697AooVtnuQuVBsQcC49TyLd/EF1oc5yMe5a9I1BiU61acgWlR5Dgyvv mZJ6mE8z rfMb0ZTFeUy9w/J+WwjwhQEobSWkxZBHs9OOwQhJm01QRvJn+UKvInYbU16dq774kHNaVU41uo32Oqv0WHnHCo1vV2izUZdQGokHdlJAk4qMoHxdYSbb/YwejAJ9R3l3sRvHo 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: memmap_init_range() would set page count of all pages, but the free pages count would be reset in __free_pages_core(). These two are opposite operations. It's unnecessary and time-consuming when it's in MEMINIT_EARLY context. Set page count and mark page reserved in reserve_bootmem_region when in MEMINIT_EARLY context, and change the context from MEMINIT_LATE to MEMINIT_EARLY in __free_pages_memory. At the same time, the init list head in reserve_bootmem_region isn't need. As it already done in __init_single_page. The following data was tested on an x86 machine with 190GB of RAM. before: free_low_memory_core_early() 342ms after: free_low_memory_core_early() 286ms Signed-off-by: Yajun Deng --- mm/memblock.c | 2 +- mm/mm_init.c | 20 ++++++++++++++------ mm/page_alloc.c | 8 +++++--- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/mm/memblock.c b/mm/memblock.c index a32364366bb2..9276f1819982 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -2089,7 +2089,7 @@ static void __init __free_pages_memory(unsigned long start, unsigned long end) while (start + (1UL << order) > end) order--; - memblock_free_pages(start, order, MEMINIT_LATE); + memblock_free_pages(start, order, MEMINIT_EARLY); start += (1UL << order); } diff --git a/mm/mm_init.c b/mm/mm_init.c index 0a4437aae30d..1cc310f706a9 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -718,7 +718,7 @@ static void __meminit init_reserved_page(unsigned long pfn, int nid) if (zone_spans_pfn(zone, pfn)) break; } - __init_single_page(pfn_to_page(pfn), pfn, zid, nid, true, false); + __init_single_page(pfn_to_page(pfn), pfn, zid, nid, false, false); } #else static inline void pgdat_set_deferred_range(pg_data_t *pgdat) {} @@ -756,8 +756,8 @@ void __meminit reserve_bootmem_region(phys_addr_t start, init_reserved_page(start_pfn, nid); - /* Avoid false-positive PageTail() */ - INIT_LIST_HEAD(&page->lru); + /* Set page count for the reserve region */ + init_page_count(page); /* * no need for atomic set_bit because the struct @@ -888,9 +888,17 @@ void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone } page = pfn_to_page(pfn); - __init_single_page(page, pfn, zone, nid, true, false); - if (context == MEMINIT_HOTPLUG) - __SetPageReserved(page); + + /* If the context is MEMINIT_EARLY, we will set page count and + * mark page reserved in reserve_bootmem_region, the free region + * wouldn't have page count and reserved flag and we don't + * need to reset pages count and clear reserved flag in + * __free_pages_core. + */ + if (context == MEMINIT_EARLY) + __init_single_page(page, pfn, zone, nid, false, false); + else + __init_single_page(page, pfn, zone, nid, true, true); /* * Usually, we want to mark the pageblock MIGRATE_MOVABLE, diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 6c4f4531bee0..6ac58c5f3b00 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1285,9 +1285,11 @@ void __free_pages_core(struct page *page, unsigned int order, enum meminit_conte unsigned int loop; /* - * When initializing the memmap, __init_single_page() sets the refcount - * of all pages to 1 ("allocated"/"not free"). We have to set the - * refcount of all involved pages to 0. + * When initializing the memmap, memmap_init_range sets the refcount + * of all pages to 1 ("allocated"/"not free") in hotplug context. We + * have to set the refcount of all involved pages to 0. Otherwise, + * we don't do it, as reserve_bootmem_region only set the refcount on + * reserve region ("allocated") in early context. */ if (context != MEMINIT_EARLY) { prefetchw(p);