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: 143223 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp5375711vqi; Fri, 22 Sep 2023 00:10:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEWagYux1dbXRLn6vwp/qcai9+pnu7CzMRYq4uq0ylYagDXY7xgAPgLt4vv0Xfru9uvU36G X-Received: by 2002:a1f:4903:0:b0:48d:b7c:56c8 with SMTP id w3-20020a1f4903000000b0048d0b7c56c8mr7415080vka.0.1695366646238; Fri, 22 Sep 2023 00:10:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695366646; cv=none; d=google.com; s=arc-20160816; b=WwnOJ++7lU7SMn1iMXjAl4JE39xx+VROxQToquUCLgZNlnKm3FTIXqjpF5K7hWsJVm o5e7XPO7XtWGcR9QEUH5uF++Zj8+NxQkWtLMjPjgcfgaYuDLlWI/jRZ0++QFzkQHsvzN NQfkm4Yl0FaXsCF5BGAKic2wq0ieQ23mJvOZHVRUW6xWCuU0BtkXPVMCtlD2nHmUbiJX rVzJsKByr1IqSCZaAbyEgQuB6v7WLnyUHbH5leMgMl5/Y8v/IruTlo7gVI8uopMc+prl Mx6rjza5infCVq5M4kFbDUaY8uqNmj3BIO/mOOU0TsqQ+yT1AshHDEfKlW4cAxyqdQL3 M4Ew== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=YklfUpteE5FTVxoZIVXyNtzYnXnzzLxI2l0gg0fMR4U=; fh=KSy1dDf6lUvgdoPctzpy47/Mjv6bi14N3nvpkZLQDdc=; b=CsApJ7akrX5wLqNx8H37wbbAtdjhCm+kvYlbQ6RUWnJGrd5SA+xMuJK4x6au+R/FcN oVU+zMXrZdUyzx45K4cDxTLnAFx/UX60xDTU6vUII84dGLiJ3hPWaZc+NL1Yw0CidUkc tr+8lTw41a7MRiclu8QA0KTvqVtWd4ixUzwFKZzIjy+GVikte7xF89ht2Ou66+YJKLib y4fqvcFr+lyv5qrssovRPzDjZTvsGRTwWqfpyepncOqet4iSetu51YXqqqIX0AtNlkkH 8V4j6WRm3nRmiB+zQhn7NyvbGQBdObMAh2Vu5jABHrLFbZSEUnX1hXBPNMni5RsSKBAD 8ssQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=AFYpk1uq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id 21-20020a630d55000000b005649cee422esi3182345pgn.464.2023.09.22.00.10.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 00:10:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=AFYpk1uq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 6742783BB5A6; Fri, 22 Sep 2023 00:10:43 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230348AbjIVHKb (ORCPT + 30 others); Fri, 22 Sep 2023 03:10:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230445AbjIVHK1 (ORCPT ); Fri, 22 Sep 2023 03:10:27 -0400 Received: from out-210.mta0.migadu.com (out-210.mta0.migadu.com [91.218.175.210]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 68167198 for ; Fri, 22 Sep 2023 00:10:19 -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=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-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Fri, 22 Sep 2023 00:10:43 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777720776276975492 X-GMAIL-MSGID: 1777720776276975492 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);