From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pz2-f12.google.com (mail-pz2-f12.google.com [74.125.228.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1722D28DB54 for ; Tue, 8 Sep 2026 03:04:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.228.12 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788836671; cv=none; b=aYafYBF6bmUJiakO2PXaXCUdt/5eZXqDx45/bUHSiX1AKJOXZd8ioZqxe4QVyuf2GzrcnRNyPMPLakSfRfpBQIXaTcPEZmyEfpjK8g3C9zLFeY/TmeSz6ihNj8D1B3GZ9StCb+seLgq6IjaFdmb+Nh/pao49ulYTLedi21he510= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788836671; c=relaxed/simple; bh=6iR+I4+CRd6OErCQvwrzJO+09x/z03jI1dFumA0VSuw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bgPS1YXynb/2FcmlG63KX+FAspAM7suz/meocZTgnaiRDJKZLKawlUgEU+BfxbYK+3iIfk9TSLWQFJpYWkH2etb5KhJX9HF6+Lb4thAYOvhtcpInuGU49sMU1NMvo17qFO4tyW7ChSW8oyyW3+ORT+5oVQZFBb4sIqdwsUyYJqE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=ffhFmlrJ; arc=none smtp.client-ip=74.125.228.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="ffhFmlrJ" Received: by mail-pz2-f12.google.com with SMTP id d2e1a72fcca58-8623e5d435cso182358b3a.1 for ; Mon, 07 Sep 2026 20:04:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1788836666; x=1789441466; darn=vger.kernel.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=uEGgQCKJvggw1Narb1WaslYNRsxnnnRwg14YCHWkvvU=; b=ffhFmlrJMMvyXs9uH9zMivqVlVdM+B+314G62SuVXMeHJHMSmZi2yg4+zpLBsYRmqf mkWXkvXUvV10wJbODubnWBa0ccd2gV2ptQ0ZqpX2AOlj8s4OhNPxcYxx5FrS7aAo7TEq Ak7bUm/f6082b0GTvBTETBdvyXo6vsktAGhFWLdksiGAkWnvyO4eco4AEo5/b8+7MGLD qtCJBw8oGAXpbOdASHtWSfVp8ZLxU/c9II+oIs48uRN1m+s+ctVCdzBe4XHS7garOfva D87ZruF2lrb10eWw4qeKqnzZKOgG/HqPVC48+4rAmLGi/NrL4GH/aR011u11paXRLVV2 0D7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788836666; x=1789441466; 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=uEGgQCKJvggw1Narb1WaslYNRsxnnnRwg14YCHWkvvU=; b=gnOnZV/G5ow6L+1SAC+X/nJJ2kBMplhfR7dmuNp1WDXguWSOctpORzEmVuwQ/QjZPU uoEOvFmifdf+BQvQrMc+nmDjS7JvNqexRsI3teMD9yL61FEAR8ZRYGErh4BJvaIToYNK E2IHSVVtzlvnKcZTFAi5MWW3nxWnLPBEaGUlD+tw1gH8YYtGPE0sgDoucbq6NR6czHYm 29LxHjmdFfeb3UVSpqMZn4ON0FgxgWxiE/DEej0st+BWN6Gq2ijFnqUGtJYvPurwafDa iRNxKE7slRBY2DK1YgRvPjoTd8Sb5tKVv1g4JuO21wr/8LV/ATXcCHEBJrils4J2gKau Ns8A== X-Forwarded-Encrypted: i=1; AKwUvBznuHQdljPAYE1gYK64AU9Zqoai9TfM2VvUW+02QOpVlceej0vKUS8fCA6OE/oFCkZrBXPtpbm6NxA=@vger.kernel.org X-Gm-Message-State: AFuF++mNri9ICOthZWzj15NOZ5qAGztFFDw80i7vOysfQOTKgW/+T/ie nZ5LIhAu6ytlEdedkaq4slQ9/uONo1OwMw0rXFSIvORRfA47d4uhGo/uoqoKTqt4831jzoTUq+r feOkH X-Gm-Gg: AYBFou14cGrFnGogadGGLiBzLnve+E5yVRdFAN7cDjEOb1jJxENU0wgzoSNO9AiynSU 3hW+q0aDD4jBA2WRMs71iamooU+XCI8dm5S8CWsF+qzM1zs7yfSZ2q0Mg26drnr+7+VBsO54UcU IqPc+aKXEzv02zgyCCITb6HsY35D/+nb5vZf3YHgo87d/5TMX+CbTa+bqFDxf2x5w7cWSforWuW G3gK5cNWyGO4HdRGmRRLwAQKHNWVUT8UVEnDkhR+l8r04PfL4i+yGgph1u+sVeR1eyR1+sIUbt2 YE3HLmsUtwbBPEEM3SbjI+rpqHYW+4tYK4OAVBkgHgb4rwlkIvK3i/HhYYMR93iWuWqkixMxND0 G840IDbKzLw9lWUF0YBNxdgR0BAWL7XpNXVn3Fcr+tVHytJ5L6s3s8+e1E1pY9hPptY5AGThV36 T+HVrptEYq8kw4yFCq2BI7qm44X4F+cTvbXfzMOZZxPGBkMtsT+nmMgAjhf0NZW82/XTPoBYoIx QgBpFdl/U76InksulLPZb9vZru49kV4fGE= X-Received: by 2002:a05:6a00:1d9a:b0:847:852f:c28a with SMTP id d2e1a72fcca58-86698ee5c1bmr4207907b3a.5.1788836666149; Mon, 07 Sep 2026 20:04:26 -0700 (PDT) Received: from G6L4RL2QG9.bytedance.net ([61.213.176.9]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-86152a358a2sm4868234b3a.29.2026.09.07.20.04.22 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 07 Sep 2026 20:04:25 -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 v2 06/11] mm/sparse-vmemmap: switch device DAX to shared tail vmemmap pages Date: Tue, 8 Sep 2026 11:03:30 +0800 Message-ID: <20260908030335.96549-7-songmuchun@bytedance.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260908030335.96549-1-songmuchun@bytedance.com> References: <20260908030335.96549-1-songmuchun@bytedance.com> Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit HugeTLB vmemmap optimization now uses per-zone shared tail vmemmap pages. Device DAX has not been switched to that mechanism yet. Switch device DAX to vmemmap_shared_tail_page() as well. This aligns DAX with HugeTLB by using the common per-zone shared tail vmemmap page. The optimization is enabled only for DEV-DAX through pgmap->vmemmap_shift, which is assigned when a DEV-DAX device is initialized. Unlike FS-DAX, DEV-DAX does not modify tail struct pages, so sharing them is safe. Since the shared tail page can now back ZONE_DEVICE vmemmap mappings, initialize its entries with PG_reserved for device zones. Also skip poisoning vmemmap-optimizable sections while their struct pages may be shared. Signed-off-by: Muchun Song --- v2: - Explain why sharing tail vmemmap pages is safe for DEV-DAX (suggested by Qi Zheng) --- include/linux/mmzone.h | 10 +++++++++ mm/memory_hotplug.c | 5 +++-- mm/sparse-vmemmap.c | 47 ++++++++++++++---------------------------- 3 files changed, 28 insertions(+), 34 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index d3778ba976a5..03ed9232f186 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -1686,11 +1686,21 @@ static inline bool zone_is_zone_device(const struct zone *zone) { return zone_idx(zone) == ZONE_DEVICE; } + +static inline struct zone *device_zone(int nid) +{ + return &NODE_DATA(nid)->node_zones[ZONE_DEVICE]; +} #else static inline bool zone_is_zone_device(const struct zone *zone) { return false; } + +static inline struct zone *device_zone(int nid) +{ + return NULL; +} #endif /* diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index b428da66d279..0db0379826df 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -554,8 +554,9 @@ void remove_pfn_range_from_zone(struct zone *zone, /* Select all remaining pages up to the next section boundary */ cur_nr_pages = min(end_pfn - pfn, SECTION_ALIGN_UP(pfn + 1) - pfn); - page_init_poison(pfn_to_page(pfn), - sizeof(struct page) * cur_nr_pages); + if (!section_vmemmap_optimizable(__pfn_to_section(pfn))) + page_init_poison(pfn_to_page(pfn), + sizeof(struct page) * cur_nr_pages); } /* diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c index aed1e7429daa..0201877a7f80 100644 --- a/mm/sparse-vmemmap.c +++ b/mm/sparse-vmemmap.c @@ -193,6 +193,8 @@ struct page __ref *vmemmap_shared_tail_page(unsigned int order, struct zone *zon set_page_node(page, zone_to_nid(zone)); set_page_zone(page, zone_idx(zone)); prep_compound_tail(page, NULL, order); + if (zone_is_zone_device(zone)) + __SetPageReserved(page); } page = virt_to_page(addr); @@ -490,23 +492,6 @@ static bool __meminit reuse_compound_section(unsigned long start_pfn, return !IS_ALIGNED(offset, nr_pages) && nr_pages > PAGES_PER_SUBSECTION; } -static pte_t * __meminit compound_section_tail_page(unsigned long addr) -{ - pte_t *pte; - - addr -= PAGE_SIZE; - - /* - * Assuming sections are populated sequentially, the previous section's - * page data can be reused. - */ - pte = pte_offset_kernel(pmd_off_k(addr), addr); - if (!pte) - return NULL; - - return pte; -} - static int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn, unsigned long start, unsigned long end, int node, @@ -516,21 +501,18 @@ static int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn, pte_t *pte; int rc; unsigned long flags = VMEMMAP_POPULATE_DAX; + struct page *page; + unsigned int order = pfn_to_section_order(start_pfn); - if (reuse_compound_section(start_pfn, pgmap)) { - pte = compound_section_tail_page(start); - if (!pte) - return -ENOMEM; + page = vmemmap_shared_tail_page(order, device_zone(node)); + if (!page) + return -ENOMEM; - /* - * Reuse the page that was populated in the prior iteration - * with just tail struct pages. - */ + if (reuse_compound_section(start_pfn, pgmap)) return vmemmap_populate_range(start, end, node, NULL, - pte_pfn(ptep_get(pte)), flags); - } + page_to_pfn(page), flags); - size = min(end - start, pgmap_vmemmap_nr(pgmap) * sizeof(struct page)); + size = min(end - start, (1UL << order) * sizeof(struct page)); for (addr = start; addr < end; addr += size) { unsigned long next, last = addr + size; @@ -546,12 +528,12 @@ static int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn, return -ENOMEM; /* - * Reuse the previous page for the rest of tail pages + * Reuse the shared page for the rest of tail pages * See layout diagram in Documentation/mm/vmemmap_dedup.rst */ next += PAGE_SIZE; rc = vmemmap_populate_range(next, last, node, NULL, - pte_pfn(ptep_get(pte)), flags); + page_to_pfn(page), flags); if (rc) return -ENOMEM; } @@ -883,13 +865,14 @@ int __meminit sparse_add_section(int nid, unsigned long start_pfn, if (IS_ERR(memmap)) return PTR_ERR(memmap); + ms = __nr_to_section(section_nr); /* * Poison uninitialized struct pages in order to catch invalid flags * combinations. */ - page_init_poison(memmap, sizeof(struct page) * nr_pages); + if (!section_vmemmap_optimizable(ms)) + page_init_poison(memmap, sizeof(struct page) * nr_pages); - ms = __nr_to_section(section_nr); __section_mark_present(ms, section_nr); /* Align memmap to section boundary in the subsection case */ -- 2.54.0