From patchwork Fri Sep 22 07:09:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yajun Deng X-Patchwork-Id: 13395076 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 532B2E7D0D2 for ; Fri, 22 Sep 2023 07:10:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E6C986B028C; Fri, 22 Sep 2023 03:10:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E1BD06B028D; Fri, 22 Sep 2023 03:10:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D0B256B028E; Fri, 22 Sep 2023 03:10:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id C0B996B028C for ; Fri, 22 Sep 2023 03:10:12 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 844C5141032 for ; Fri, 22 Sep 2023 07:10:12 +0000 (UTC) X-FDA: 81263359464.30.B101F7B Received: from out-213.mta0.migadu.com (out-213.mta0.migadu.com [91.218.175.213]) by imf17.hostedemail.com (Postfix) with ESMTP id BD55F40011 for ; Fri, 22 Sep 2023 07:10:10 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=iSP4Wnij; spf=pass (imf17.hostedemail.com: domain of yajun.deng@linux.dev designates 91.218.175.213 as permitted sender) smtp.mailfrom=yajun.deng@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695366610; 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=eNDRWQx4rNnkd5izfMYcW2fBhjnfX3xJn3/1FZ3VThE=; b=S5tKw7Y3OXI/6OZxuG2Eeq+zwrZjqsFOloVANlUTlsQXVVRx9UNqMYcXHpzdaBrT0mGoSC 6AQQR/px9r6cN3ktJ9CbQDo2qTj/3gVBF/BLD8yzCst9PjQm8pVa4l39JXYKVUTmpNx5Tw 2+vrQ8CoGSCYvUpFfqmwinHl9aov2bw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695366610; a=rsa-sha256; cv=none; b=ntfWYZFDYQncUJfr/HIMDTODLteTNSXUC2h/4L5btR3SRWPBC6JKsAudy/iEzqtZJbtklU p7vOTqA3n/84opm37BrLprfZJjbahvVbxIvOqZeGKhqhyt7HD/wLJ7EcI+ROMGEb9UP0A9 ym2qqdZ3yI6rwGrOKOXQHVXfJrTSNZ4= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=iSP4Wnij; spf=pass (imf17.hostedemail.com: domain of yajun.deng@linux.dev designates 91.218.175.213 as permitted sender) smtp.mailfrom=yajun.deng@linux.dev; dmarc=pass (policy=none) header.from=linux.dev 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=1695366609; 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=eNDRWQx4rNnkd5izfMYcW2fBhjnfX3xJn3/1FZ3VThE=; b=iSP4WnijIwKLq3l9vadWpYjfJoJm3g4DerOWhwM35ttFjdkUM6l19oJCA3gNFvh1Mphrj3 HsqfWa/w9fO+8D4ekxGE+wh4qCZ9R7alts+HM2SsnLz/nxZodV+mCe9vbVcqanUTTx5BMC y/tcpUU+PnQQYCSvaVyj01COgaRrssQ= 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 2/4] mm: Introduce MEMINIT_LATE context Date: Fri, 22 Sep 2023 15:09:21 +0800 Message-Id: <20230922070923.355656-3-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-Stat-Signature: 9u9p9qno8jkskwoxgeiy7pfrprnkfb5i X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: BD55F40011 X-Rspam-User: X-HE-Tag: 1695366610-481978 X-HE-Meta: U2FsdGVkX1//00hFQZWLizIQovf8lEktRnqUYVdygXMgBFbQe03wyoYmqMcTkE3C10NeBQ+vIOR2DBGeWIAHBWjbpXj7IwtGpXyrLfqzEeQ1tZuYIjBY3KwK0XmVWWxhUI92906z/UsyMB2csQNrr7Des0C90MmfhknKb+qcCDAmujoqWrivG8JH2t5Fdo0DIzm3lylL+sOmwMMWD9ZJg2/OZj873QEJbTvv7cW2knoCeAUWWCmUVSZCVDStBBuWvh09lhfaSUpqQbYyO82WVBzrKKa/1t8Qi5wD4KKJLkLY9n+ZrO9UF5yxmFqyE0K+OtiMeJYfBd/P30sHxzp5vp7PkxoAPHqzmQ2ZYIqBpzQo4I961+4Bzl/WSfin7JOzc0Pr5ua/YhJlZj71v1joE8D7pJKLPOq+f4toia6ewcBoRWJBxWMhSOBTk6ZKH6kr8FWj65zXhFKabtF1AUnVZHlaPfIwcpMQJLUYAxelwWYjlCGrwoSotrCghx1SLj6wbwbQp/4q7WSTyogscuM8DzvkzfMalGY70i9NBKf+qmKtFBMLfPmGLz1nF4k3cklzs7/jcKBKpaZ8cGARcRdiBd9bMdry1iJRcewaBGAabHZwIj8Yiu1GdGfu59o8ne4b1okxpSm64khTayrIqM7z2J+5XLUzSrymmybL/Cs9OLNfFNX5lKaQla3H7iRYnno0lc4ueXGtEG8T3JU8wUakd1LxFladZci3U5oEG7yi7LKRb5WkafFT4puHlp4ISRkHff/CaIaMuJsZLCSdEJEV4IBAsKZm0bR87cqCzEwFsRJ59q7uZ5PSzWZ6D5S0ppG4jwW+V6/6ezVKEkAldDF50zhL6ToLzBLtfouJ7g5v82sG0j2nQp8Aa6qFW6hzdwPxPyBioALufQ8kwuNwYB+CtyxorOJxxMzP1sJk5zadjwzBSSF1nad+l08QzeifkNjzP5gI80CJgnC5Gfw2G5x YzMoeWqU Y864/JX6P0icdW9ZReYApvGWn87SEo9uetzfmpcXJOwXFLlgviemb+3zxfIAg8D8HM+E4RUNCUqbcJf44JieoH2rLOkjDh9rn3UnpgFEhwMHVwnedqzTbCpwQvr76REwzt6gE 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: __free_pages_core() will always reset pages count and clear reserved flag. It will consume a lot of time if there are a lot of pages. Introduce MEMINIT_LATE context, if the context is MEMINIT_EARLY, we don't need reset pages count and clear reserved flag. Signed-off-by: Yajun Deng --- include/linux/mmzone.h | 1 + mm/internal.h | 7 ++++--- mm/kmsan/init.c | 2 +- mm/memblock.c | 4 ++-- mm/memory_hotplug.c | 2 +- mm/mm_init.c | 11 ++++++----- mm/page_alloc.c | 14 ++++++++------ 7 files changed, 23 insertions(+), 18 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 1e9cf3aa1097..253e792d409f 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -1442,6 +1442,7 @@ bool zone_watermark_ok_safe(struct zone *z, unsigned int order, */ enum meminit_context { MEMINIT_EARLY, + MEMINIT_LATE, MEMINIT_HOTPLUG, }; diff --git a/mm/internal.h b/mm/internal.h index 8bded7f98493..31737196257c 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -394,9 +394,10 @@ static inline void clear_zone_contiguous(struct zone *zone) extern int __isolate_free_page(struct page *page, unsigned int order); extern void __putback_isolated_page(struct page *page, unsigned int order, int mt); -extern void memblock_free_pages(struct page *page, unsigned long pfn, - unsigned int order); -extern void __free_pages_core(struct page *page, unsigned int order); +extern void memblock_free_pages(unsigned long pfn, unsigned int order, + enum meminit_context context); +extern void __free_pages_core(struct page *page, unsigned int order, + enum meminit_context context); /* * This will have no effect, other than possibly generating a warning, if the diff --git a/mm/kmsan/init.c b/mm/kmsan/init.c index ffedf4dbc49d..b7ed98b854a6 100644 --- a/mm/kmsan/init.c +++ b/mm/kmsan/init.c @@ -172,7 +172,7 @@ static void do_collection(void) shadow = smallstack_pop(&collect); origin = smallstack_pop(&collect); kmsan_setup_meta(page, shadow, origin, collect.order); - __free_pages_core(page, collect.order); + __free_pages_core(page, collect.order, MEMINIT_LATE); } } diff --git a/mm/memblock.c b/mm/memblock.c index 5a88d6d24d79..a32364366bb2 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -1685,7 +1685,7 @@ void __init memblock_free_late(phys_addr_t base, phys_addr_t size) end = PFN_DOWN(base + size); for (; cursor < end; cursor++) { - memblock_free_pages(pfn_to_page(cursor), cursor, 0); + memblock_free_pages(cursor, 0, MEMINIT_LATE); totalram_pages_inc(); } } @@ -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(pfn_to_page(start), start, order); + memblock_free_pages(start, order, MEMINIT_LATE); start += (1UL << order); } diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 3b301c4023ff..d38548265f26 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -634,7 +634,7 @@ void generic_online_page(struct page *page, unsigned int order) * case in page freeing fast path. */ debug_pagealloc_map_pages(page, 1 << order); - __free_pages_core(page, order); + __free_pages_core(page, order, MEMINIT_HOTPLUG); totalram_pages_add(1UL << order); } EXPORT_SYMBOL_GPL(generic_online_page); diff --git a/mm/mm_init.c b/mm/mm_init.c index c40042098a82..0a4437aae30d 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -1976,7 +1976,7 @@ static void __init deferred_free_range(unsigned long pfn, if (nr_pages == MAX_ORDER_NR_PAGES && IS_MAX_ORDER_ALIGNED(pfn)) { for (i = 0; i < nr_pages; i += pageblock_nr_pages) set_pageblock_migratetype(page + i, MIGRATE_MOVABLE); - __free_pages_core(page, MAX_ORDER); + __free_pages_core(page, MAX_ORDER, MEMINIT_LATE); return; } @@ -1986,7 +1986,7 @@ static void __init deferred_free_range(unsigned long pfn, for (i = 0; i < nr_pages; i++, page++, pfn++) { if (pageblock_aligned(pfn)) set_pageblock_migratetype(page, MIGRATE_MOVABLE); - __free_pages_core(page, 0); + __free_pages_core(page, 0, MEMINIT_LATE); } } @@ -2568,9 +2568,10 @@ void __init set_dma_reserve(unsigned long new_dma_reserve) dma_reserve = new_dma_reserve; } -void __init memblock_free_pages(struct page *page, unsigned long pfn, - unsigned int order) +void __init memblock_free_pages(unsigned long pfn, unsigned int order, + enum meminit_context context) { + struct page *page = pfn_to_page(pfn); if (IS_ENABLED(CONFIG_DEFERRED_STRUCT_PAGE_INIT)) { int nid = early_pfn_to_nid(pfn); @@ -2583,7 +2584,7 @@ void __init memblock_free_pages(struct page *page, unsigned long pfn, /* KMSAN will take care of these pages. */ return; } - __free_pages_core(page, order); + __free_pages_core(page, order, context); } DEFINE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, init_on_alloc); diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 06be8821d833..6c4f4531bee0 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1278,7 +1278,7 @@ static void __free_pages_ok(struct page *page, unsigned int order, __count_vm_events(PGFREE, 1 << order); } -void __free_pages_core(struct page *page, unsigned int order) +void __free_pages_core(struct page *page, unsigned int order, enum meminit_context context) { unsigned int nr_pages = 1 << order; struct page *p = page; @@ -1289,14 +1289,16 @@ void __free_pages_core(struct page *page, unsigned int order) * of all pages to 1 ("allocated"/"not free"). We have to set the * refcount of all involved pages to 0. */ - prefetchw(p); - for (loop = 0; loop < (nr_pages - 1); loop++, p++) { - prefetchw(p + 1); + if (context != MEMINIT_EARLY) { + prefetchw(p); + for (loop = 0; loop < (nr_pages - 1); loop++, p++) { + prefetchw(p + 1); + __ClearPageReserved(p); + set_page_count(p, 0); + } __ClearPageReserved(p); set_page_count(p, 0); } - __ClearPageReserved(p); - set_page_count(p, 0); atomic_long_add(nr_pages, &page_zone(page)->managed_pages);