From mboxrd@z Thu Jan 1 00:00:00 1970 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 lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A8D50C88E42 for ; Fri, 11 Sep 2026 05:04:29 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [127.0.0.1]) by lists.ozlabs.org (Postfix) with ESMTP id 4hh2Wp1jyHz2yps; Fri, 11 Sep 2026 15:04:06 +1000 (AEST) Authentication-Results: lists.ozlabs.org; arc=none smtp.remote-ip="2607:f8b0:4864:20::102d" ARC-Seal: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1789103046; cv=none; b=kjXWlkTTT1lJV/XklAMv7GvdX3dG140saar2IW3QdrRKdD4YWDcwZMNjTs863XhRArm2VptXeE04Legp9tgMlFeOi2nyQ9MrfHFBmLEpjU7Wniiy3Hkl0RwIw7u71HxnaubSeBt7/1F/1fTnnD1RUYVRmdK6t9sq//l19vmwoPy/wnf0UnwiSuU/IoQUbCFuemQLbUaaUWMIFFqdhT8Vi5cnHe0hs4wL4aWo7pqW5buHHWleLHjp/VvIjZZUQxLs08H3ADQLAToaO9xH8ABO5/3LlZSJAqht/B1WQIuRAi2qAvQ1x743//2uIwH5oOPIjYdwDPIKG0L4d5Qr0CO0KQ== ARC-Message-Signature: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1789103046; c=relaxed/relaxed; bh=9h5irsdiFbTSRTtgQf8+DpZjFaqFHIv3CtCmLZFKqxE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RvXIcKgJoHv0Ql8DY3DulUySHn2qaCrqYGxDB0t6H/ACNXj5eAWaU/zjQ/+wP7j/H0KBGP0jZAla9H2UbWlUU7ihB2t9GHsqk6kVM7mV/xV2IliB5dpEDlQXLJLV3EVCaXHb5E+teDjQLwd+fav6amCtv07s4gh0L4bp+6Mkw4q3Y7C3q61FI68htKfdcQFi8Y+rngVUBNHOhq8tJoPSskbePBsm74LbSbRoZO6aeGFQmFoCQkhr3cnRiuENqi0P27KigN/d4h1A2Xbzq6ybNDslQvI1SfOHmSsclW53aZqjejrqxTN6xQkZXE9mphmiNIzIeXJrpIv4PMpp/s8PEg== ARC-Authentication-Results: i=1; lists.ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; dkim=pass (2048-bit key; unprotected) header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=google header.b=DoH2+kyn; dkim-atps=neutral; spf=pass (client-ip=2607:f8b0:4864:20::102d; helo=mail-pj1-x102d.google.com; envelope-from=songmuchun@bytedance.com; receiver=lists.ozlabs.org) smtp.mailfrom=bytedance.com Authentication-Results: lists.ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=google header.b=DoH2+kyn; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=bytedance.com (client-ip=2607:f8b0:4864:20::102d; helo=mail-pj1-x102d.google.com; envelope-from=songmuchun@bytedance.com; receiver=lists.ozlabs.org) Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4hh2Wn3FG0z2ynZ for ; Fri, 11 Sep 2026 15:04:05 +1000 (AEST) Received: by mail-pj1-x102d.google.com with SMTP id 98e67ed59e1d1-398b3d66515so650063a91.0 for ; Thu, 10 Sep 2026 22:04:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1789103043; x=1789707843; darn=lists.ozlabs.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=9h5irsdiFbTSRTtgQf8+DpZjFaqFHIv3CtCmLZFKqxE=; b=DoH2+kynXsijjU04saaOShy7zr7q/cEgcPgHmL1NGn5kPjIgE7ZzEpXw5Ich4cuQvg 6E6nVwZfqBblJqpK7+2IkxJy7JrIWSo+vrBv9nes3e9Rvgs9WdDxA/YaVIsfnHvGlP2o Uf7L5jgRspPmAN3HXoYHswLzmlFhfsqTqVDKIt6gIRzIhplBYfdxSwIKPpP7U8WbVcVr ENESf3gVv8WtXWHYBG6/8HararQ12vEziYLpMFAYfCv/9w94Rihkfv6JAcWZxbYzLptN dggVpJbSJcajJpUbHgxunJ6+Zdk+qCwHiAEw/siMPJwaBI8ZSo5FtsKmrGAgjMu3dOoT G3vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789103043; x=1789707843; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=9h5irsdiFbTSRTtgQf8+DpZjFaqFHIv3CtCmLZFKqxE=; b=dnI1jgiQai9CAsYab6g+sWBWfZA5oy0kFMoMwdJ7FzgTMFzdfoXL8P/FVI/+pWoyef KRAzoAAGswRMJeHEYwQkCIaT7IdmvoMG0EI2ktABcvOUCTfdyALdcNn/jrOQWOTZGCqe dpiW5y0o6tlPFibg7eG6z3i/VVd+iJSjBR7iBEWI6kFcngF45AnvX05iDmbAWVCFDHZC ekM983btqMq50rygewk1qV74Qf8AzS2z1u7C/8lqM6JfjC8Tf67tsiVLZCAb8j1bELdQ lKdL2n52vw8t+rmNhUOWfuRkOeZN9X8zscFxp9gYI0ITE+ZEVeicaUZ8SjOvMMKZP3zo RxTg== X-Forwarded-Encrypted: i=1; AKwUvBy3iTuzVdDuqMMj3AtreOl/m/0DaA/Qd174hva6XNrC+5bPw8NVdR45bPKy3NYW5fbniP8EBkJdBilubr4=@lists.ozlabs.org X-Gm-Message-State: AFuF++lDTyicC3c+1VyR4WQ7cF0itjaEiVMk4AUp0VNbOTHGYOs4tBxS d8fkxnSbES8IDnqVkCT8mm/LdXUx1xZLlKlco7IX8MwRifVOkA5f+qrOIMb1cFilGJg= X-Gm-Gg: AYBFou1SdoebSIMQ2gMZ+DkFL4DuLFlkl55aWKXRoR9buSZcwoBxc+5Kwl+drJ+Y+H5 GfThbklrmmqVD7T7nI9+Zimrjkht62vMzzDq9FXbLSAPgsMTDlN2CYtx5aCprK7cw89hOikxFsi NKU43TqKvmQRYscZO3a0mDbSrQM/4xO+NaoWl/r0+8szIlPKYEjIM6d8Oj2nKRa2d4BgNy5x116 HdWWSwuMvcce+tP/R1IIPcbpaJ+SLpZO1dAIQUJQiu1bcxmONla0LJ4NrjeP4FaUE+qr9oMzXYH Kxu07wX+YU1RI+Mktlt5mXZOWROw2YbsFOqeQ0Z509bv8wIT2toa4dpIM4iFkuzgv9JI7KO/h32 pFDwgIoQ6s0lOcd/HMYLzJYx7ZQIxg/XKXebKCWe/HQWXzqO7YEkjo8qfQ/JSNJTbOcPG92FQ7E jEpiAaaTucFlBJJZuUWseDWheuPmZKaVJRL8sVd405AaZaEpyNousK7QAyId9JdqGOSTRY6suV4 vVX0QnrShew26BYAb4m41epPQR9k3BCilribIjxkqCOKtY= X-Received: by 2002:a17:90b:1648:b0:36b:bec8:94c5 with SMTP id 98e67ed59e1d1-39d9bec50b9mr3746365a91.10.1789103041109; Thu, 10 Sep 2026 22:04:01 -0700 (PDT) Received: from G6L4RL2QG9.bytedance.net ([61.213.176.11]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-39d99531b16sm2651561a91.14.2026.09.10.22.03.56 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 10 Sep 2026 22:04:00 -0700 (PDT) From: Muchun Song To: Andrew Morton , David Hildenbrand , Oscar Salvador , Madhavan Srinivasan , Michael Ellerman , Jonathan Corbet Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-doc@vger.kernel.org, Muchun Song , Lorenzo Stoakes , Mike Rapoport , Qi Zheng , Nicholas Piggin , Christophe Leroy , Randy Dunlap , Muchun Song Subject: [PATCH v3 07/11] mm/sparse-vmemmap: move vmemmap optimization helpers to a public header Date: Fri, 11 Sep 2026 13:02:24 +0800 Message-ID: <20260911050228.58884-8-songmuchun@bytedance.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260911050228.58884-1-songmuchun@bytedance.com> References: <20260911050228.58884-1-songmuchun@bytedance.com> X-Mailing-List: linuxppc-dev@lists.ozlabs.org List-Id: List-Help: List-Owner: List-Post: List-Archive: , List-Subscribe: , , List-Unsubscribe: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The vmemmap optimization helpers currently live in mm/sparse.h, which is an internal MM header. That works for MM code, but prevents powerpc from using the same interfaces without including a private header. Move the declarations and inline helpers to include/linux/vmemmap-optimization.h. This is a preparatory change for powerpc, which has its own vmemmap optimization implementation and needs to use the common vmemmap optimization interfaces from architecture code. Signed-off-by: Muchun Song Acked-by: Qi Zheng --- v3: - Update the subject and commit message to describe common vmemmap optimization helpers - Collect Acked-by from Qi Zheng v2: - Fix missing header dependencies. --- MAINTAINERS | 1 + include/linux/vmemmap-optimization.h | 94 ++++++++++++++++++++++++++++ mm/hugetlb.c | 2 +- mm/hugetlb_vmemmap.c | 2 +- mm/sparse.h | 76 +--------------------- 5 files changed, 98 insertions(+), 77 deletions(-) create mode 100644 include/linux/vmemmap-optimization.h diff --git a/MAINTAINERS b/MAINTAINERS index 3ef018673b5e..e4412c3d8d45 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -12096,6 +12096,7 @@ F: Documentation/mm/hugetlbfs_reserv.rst F: Documentation/mm/vmemmap_dedup.rst F: fs/hugetlbfs/ F: include/linux/hugetlb.h +F: include/linux/vmemmap-optimization.h F: include/trace/events/hugetlbfs.h F: mm/hugetlb.c F: mm/hugetlb_cgroup.c diff --git a/include/linux/vmemmap-optimization.h b/include/linux/vmemmap-optimization.h new file mode 100644 index 000000000000..2327929467f3 --- /dev/null +++ b/include/linux/vmemmap-optimization.h @@ -0,0 +1,94 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * vmemmap-optimization.h + * + * Generic vmemmap optimization declarations. + * + * Author: Muchun Song + */ +#ifndef _LINUX_VMEMMAP_OPTIMIZATION_H +#define _LINUX_VMEMMAP_OPTIMIZATION_H + +#include +#include +#include +#include + +#ifdef CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION +static inline unsigned int section_compound_order(const struct mem_section *section) +{ + return section->compound_page_order; +} + +static inline void section_set_compound_order(struct mem_section *section, + unsigned int order) +{ + VM_WARN_ON(section_compound_order(section) && order && + section_compound_order(section) != order); + section->compound_page_order = order; +} + +static inline void section_set_compound_order_range(unsigned long pfn, + unsigned long nr_pages, unsigned int order) +{ + unsigned long section_nr = pfn_to_section_nr(pfn); + + if (!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION)) + return; + + for (unsigned long i = 0; i < nr_pages / PAGES_PER_SECTION; i++) + section_set_compound_order(__nr_to_section(section_nr + i), order); +} + +static inline unsigned int pfn_to_section_compound_order(unsigned long pfn) +{ + return section_compound_order(__pfn_to_section(pfn)); +} +#else +static inline unsigned int section_compound_order(const struct mem_section *section) +{ + return 0; +} + +static inline void section_set_compound_order(struct mem_section *section, + unsigned int order) +{ +} + +static inline void section_set_compound_order_range(unsigned long pfn, + unsigned long nr_pages, unsigned int order) +{ +} + +static inline unsigned int pfn_to_section_compound_order(unsigned long pfn) +{ + return 0; +} +#endif /* CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION */ + +static inline bool vmemmap_optimizable_pfn(unsigned long pfn) +{ + const unsigned int order = pfn_to_section_compound_order(pfn); + const unsigned long nr_pages = 1UL << order; + + if (!is_power_of_2(sizeof(struct page))) + return false; + + return (pfn & (nr_pages - 1)) >= VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES; +} + +static inline bool vmemmap_optimizable_order(unsigned int order) +{ + if (!IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION)) + return false; + + if (!is_power_of_2(sizeof(struct page))) + return false; + + return order >= VMEMMAP_OPTIMIZATION_MIN_ORDER; +} + +#ifdef CONFIG_SPARSEMEM_VMEMMAP +struct page *vmemmap_shared_tail_page(unsigned int order, struct zone *zone); +#endif /* CONFIG_SPARSEMEM_VMEMMAP */ +#endif /* _LINUX_VMEMMAP_OPTIMIZATION_H */ diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 0d101ef934fc..7af55e1544e9 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -52,7 +53,6 @@ #include "hugetlb_cma.h" #include "hugetlb_internal.h" #include "mm_init.h" -#include "sparse.h" #include #define HUGE_BOOTMEM_ZONES_VALID BIT(0) diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 5ddf06b83c96..a27f46fffb54 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -15,10 +15,10 @@ #include #include #include +#include #include #include "hugetlb_vmemmap.h" -#include "sparse.h" #include "internal.h" /** diff --git a/mm/sparse.h b/mm/sparse.h index a250bea4088e..a5111087ee3a 100644 --- a/mm/sparse.h +++ b/mm/sparse.h @@ -9,80 +9,7 @@ #define __MM_SPARSE_H #include - -#ifdef CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION -static inline unsigned int section_compound_order(const struct mem_section *section) -{ - return section->compound_page_order; -} - -static inline void section_set_compound_order(struct mem_section *section, - unsigned int order) -{ - VM_WARN_ON(section_compound_order(section) && order && - section_compound_order(section) != order); - section->compound_page_order = order; -} - -static inline void section_set_compound_order_range(unsigned long pfn, - unsigned long nr_pages, unsigned int order) -{ - unsigned long section_nr = pfn_to_section_nr(pfn); - - if (!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION)) - return; - - for (unsigned long i = 0; i < nr_pages / PAGES_PER_SECTION; i++) - section_set_compound_order(__nr_to_section(section_nr + i), order); -} - -static inline unsigned int pfn_to_section_compound_order(unsigned long pfn) -{ - return section_compound_order(__pfn_to_section(pfn)); -} -#else -static inline unsigned int section_compound_order(const struct mem_section *section) -{ - return 0; -} - -static inline void section_set_compound_order(struct mem_section *section, - unsigned int order) -{ -} - -static inline void section_set_compound_order_range(unsigned long pfn, - unsigned long nr_pages, unsigned int order) -{ -} - -static inline unsigned int pfn_to_section_compound_order(unsigned long pfn) -{ - return 0; -} -#endif - -static inline bool vmemmap_optimizable_pfn(unsigned long pfn) -{ - const unsigned int order = pfn_to_section_compound_order(pfn); - const unsigned long nr_pages = 1UL << order; - - if (!is_power_of_2(sizeof(struct page))) - return false; - - return (pfn & (nr_pages - 1)) >= VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES; -} - -static inline bool vmemmap_optimizable_order(unsigned int order) -{ - if (!IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION)) - return false; - - if (!is_power_of_2(sizeof(struct page))) - return false; - - return order >= VMEMMAP_OPTIMIZATION_MIN_ORDER; -} +#include /* * mm/sparse.c @@ -142,7 +69,6 @@ static inline void sparse_sections_init(void) {} * mm/sparse-vmemmap.c */ #ifdef CONFIG_SPARSEMEM_VMEMMAP -struct page *vmemmap_shared_tail_page(unsigned int order, struct zone *zone); void sparse_init_subsection_map(void); int section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages, struct vmem_altmap *altmap, struct dev_pagemap *pgmap); -- 2.54.0