From nobody Fri Jun 19 07:51:22 2026 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 EED7B3B2AA for ; Sun, 26 Apr 2026 09:27:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777195632; cv=none; b=EK/1Bna7BqJxGYoD7yBv6rLZ9nUdtKjFBlQaUCdSPgrfH0qBfNoMnQXKa+yNheDaVPqKnoWkPRGmswO148Qxj9Ruz93f3wrQ/NQQ/MXdHA6kl9rPLKUVzAnkUh/6jLCQ4QYoYf/6Fombcr6or2tdQ0tC8f0AfWLfXsyLMC8hMH4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777195632; c=relaxed/simple; bh=0PLb5zFObm442FFPCMDt2iC8dud5KQqM0ijr0fGCJws=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ctpXyuhRS1WL2kAKdMGTWmZ4dsg9unKC41ci3Ka6MRuc6BqYwfcLCIZtRDvfpcWraVw3DkkZU+02eD362+o2Tqx6ssvROY8LlPcSQ4fk1XG8iYsnNJ5225vrcB9GEDcYp8goV4BUBxtfSPPzeEGYDTIRdjjrqorsen6TiIcWlS8= 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=Zd6m3n52; arc=none smtp.client-ip=209.85.214.173 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="Zd6m3n52" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2b299b3c739so42526905ad.3 for ; Sun, 26 Apr 2026 02:27:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1777195630; x=1777800430; 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; bh=Odp+99JHCbpwHHM6ds3EE8rv22wzvZDnJ+uzSMS+VHA=; b=Zd6m3n52Hy0maXKER73IjA/PFZidCrhgVUoUuwR61TFPmrVI1DzQLeWAQyhexd4hHe jltRbKVLQbLrVkkqTSQhdXqR4cJ1l4BI4DoOVpKja3swYjwP1jS0QD64pidg7jZj9oo0 LOS84UmfaRQOIfrMsF90ZUaKGPI5CTmeCKoXYmIAaFhXXocZEQr1c0tG5xvvlsyulFPc 5SRDid09LbAVTJG9uO36VgB36IOyAXdkEJ4ujw19yCBOrE2HmcKmsdaJe1Vpt1EukAr1 g0EpXh/uLCNnYBsDRrMJN2chcK45esV5j4Jydi6J+WPgHxnwXxXqw2lW9NDLpegHeS/c CXlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777195630; x=1777800430; 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; bh=Odp+99JHCbpwHHM6ds3EE8rv22wzvZDnJ+uzSMS+VHA=; b=ItEn+bLrPGVXpXUHwLL66GnZ2saBEq0QgKjDCcof75Rurs06GF7HTUGRDxf+8qHG6g 2ts5WpFRCbuNhaDS5lsIijVjkPV7TSkN3IkRFNO3klijP0dbA4ug5Cf+krHs9xf7+OVp xBSv9KQSHvdk/SqP1cFbaxPuLEdS2tlPjgq5ECo5j1GS9PvpArVNsiNdYTOAHd+Ip4Vr f1B5Zfp9dhb/7y39sXx2UG8s/vudntkWgJRKUxqFXVA38TkpHIS//5Hwcc+dVKuMxeBT jlaWIGjbgy8mUHmP68J5z1jg0P1mt/HTZ3urOzZYoHJckAVNMadGCsuLIagyJBtN4YFr v4yg== X-Forwarded-Encrypted: i=1; AFNElJ8dUGV1tzJooIMdZOsq+DK8Z3ugqhs55dF9B1sYhh45gNyhVK1rvqtOC1Hjjjgsku9r8hEmKa/WBPc3SP8=@vger.kernel.org X-Gm-Message-State: AOJu0Yyo2SAigQLfijsLehIkdc9MMO4vdL0auRPxL46+S7oNDZcwm9ag 2QeyQhmBQsJ3lN+Wk/WZfvl9ByAZmXa621VJhBPQ2tIFijajWHoRbM8UYyz9zvGltBI= X-Gm-Gg: AeBDieuXjAL9scB1TKeod2rq4+WIuCqyiFwxDxQxBxU31Rw+dmq4Lgq9GDhgW4Et+0m ZcvtSob2F43hTn2HHLv6+8yrENpb8cNsTLlSkaugXC5ghSRlpSgI8fRUEN8KM9+cjMLkhYLJ2h1 8tTbQ0syBF1zwFj005FJSxcpDRZRBtPQum7pfanof0E6welupprYIN+InHBSywwiN9NEguFVF8a cfrfID4zPAC3cWhz9ZZ4cXFRYerP8UfplhFLjsQ4yth4nXUUFDmNLQp1da7oLYYqFFLkckf8BjA vK3nDruL+/1M/WA7RbsEE9Nhqlqd1JniFea3HgyynpSqN1vcFIgssqggNXa1idoTA5zO6Smkq1h lUYzL0zoNNSZaAr5L53gkvEwm44Ui056C7INd/A+72AL3B2CqkCHvLRMQBb/gXCJhhtqtIkU5v0 gq4W3IkW5ECdkZWhER8vfoOtVMIPfN X-Received: by 2002:a17:902:cf0f:b0:2b7:a1ff:b239 with SMTP id d9443c01a7336-2b7a1ffb4b5mr144455245ad.14.1777195630176; Sun, 26 Apr 2026 02:27:10 -0700 (PDT) Received: from n232-176-004.byted.org ([240e:83:200::34a]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b5fab0caa9sm270352885ad.40.2026.04.26.02.27.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Apr 2026 02:27:09 -0700 (PDT) From: Muchun Song To: Andrew Morton , David Hildenbrand , Muchun Song , Oscar Salvador , Michael Ellerman , Madhavan Srinivasan Cc: Lorenzo Stoakes , "Liam R . Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Nicholas Piggin , Christophe Leroy , aneesh.kumar@linux.ibm.com, joao.m.martins@oracle.com, linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, Muchun Song , stable@vger.kernel.org Subject: [PATCH v7 1/6] mm/sparse-vmemmap: Fix vmemmap accounting underflow Date: Sun, 26 Apr 2026 17:26:35 +0800 Message-Id: <20260426092640.375967-2-songmuchun@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260426092640.375967-1-songmuchun@bytedance.com> References: <20260426092640.375967-1-songmuchun@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In section_activate(), if populate_section_memmap() fails, the error handling path calls section_deactivate() to roll back the state. This causes a vmemmap accounting imbalance. Since commit c3576889d87b ("mm: fix accounting of memmap pages"), memmap pages are accounted for only after populate_section_memmap() succeeds. However, the failure path unconditionally calls section_deactivate(), which decreases the vmemmap count. Consequently, a failure in populate_section_memmap() leads to an accounting underflow, incorrectly reducing the system's tracked vmemmap usage. Fix this more thoroughly by moving all accounting calls into the lower level functions that actually perform the vmemmap allocation and freeing: - populate_section_memmap() accounts for newly allocated vmemmap pages - depopulate_section_memmap() unaccounts when vmemmap is freed This ensures proper accounting in all code paths, including error handling and early section cases. Fixes: c3576889d87b ("mm: fix accounting of memmap pages") Cc: stable@vger.kernel.org Signed-off-by: Muchun Song Acked-by: Mike Rapoport (Microsoft) Acked-by: Oscar Salvador Acked-by: David Hildenbrand (Arm) --- mm/sparse-vmemmap.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c index 6eadb9d116e4..a7b11248b989 100644 --- a/mm/sparse-vmemmap.c +++ b/mm/sparse-vmemmap.c @@ -656,7 +656,12 @@ static struct page * __meminit populate_section_memmap= (unsigned long pfn, unsigned long nr_pages, int nid, struct vmem_altmap *altmap, struct dev_pagemap *pgmap) { - return __populate_section_memmap(pfn, nr_pages, nid, altmap, pgmap); + struct page *page =3D __populate_section_memmap(pfn, nr_pages, nid, altma= p, + pgmap); + + memmap_pages_add(DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE)); + + return page; } =20 static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_= pages, @@ -665,13 +670,17 @@ static void depopulate_section_memmap(unsigned long p= fn, unsigned long nr_pages, unsigned long start =3D (unsigned long) pfn_to_page(pfn); unsigned long end =3D start + nr_pages * sizeof(struct page); =20 + memmap_pages_add(-1L * (DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE= _SIZE))); vmemmap_free(start, end, altmap); } + static void free_map_bootmem(struct page *memmap) { unsigned long start =3D (unsigned long)memmap; unsigned long end =3D (unsigned long)(memmap + PAGES_PER_SECTION); =20 + memmap_boot_pages_add(-1L * (DIV_ROUND_UP(PAGES_PER_SECTION * sizeof(stru= ct page), + PAGE_SIZE))); vmemmap_free(start, end, NULL); } =20 @@ -774,14 +783,10 @@ static void section_deactivate(unsigned long pfn, uns= igned long nr_pages, * The memmap of early sections is always fully populated. See * section_activate() and pfn_valid() . */ - if (!section_is_early) { - memmap_pages_add(-1L * (DIV_ROUND_UP(nr_pages * sizeof(struct page), PAG= E_SIZE))); + if (!section_is_early) depopulate_section_memmap(pfn, nr_pages, altmap); - } else if (memmap) { - memmap_boot_pages_add(-1L * (DIV_ROUND_UP(nr_pages * sizeof(struct page), - PAGE_SIZE))); + else if (memmap) free_map_bootmem(memmap); - } =20 if (empty) ms->section_mem_map =3D (unsigned long)NULL; @@ -826,7 +831,6 @@ static struct page * __meminit section_activate(int nid= , unsigned long pfn, section_deactivate(pfn, nr_pages, altmap); return ERR_PTR(-ENOMEM); } - memmap_pages_add(DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE)); =20 return memmap; } --=20 2.20.1 From nobody Fri Jun 19 07:51:22 2026 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 1AF82334C27 for ; Sun, 26 Apr 2026 09:27:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777195639; cv=none; b=b5MTIPsT32e+WahQ9PEWNpZcx3yUuTTHi5AFqmJN4CePHEvjH2jy84sqyD+vbwZEVGmFoj8e8ghJe7CP6/u80fshZQ20B1a6q73ZvtAJSfuMGajXUu4c6xQYyloWULlND7FVBwfV1UOk12QP6L2R747BuP1DQtrB7VZxCrK2hHA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777195639; c=relaxed/simple; bh=9/CO8vx+V21oREl0OLJTEhDPgpJAF4/FGvx8E6oUzqk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=d2c8av0pAjbdBdqysHqgHCLzvWIGkaQGV4uLsjeRMNdlaX34vyFpBXWyfwQLQX/LjufsH3pe1+iIDEJ/frA2Ml8Q0Q6vIRzuHy0vcApL44yv8LQeGv2n7H59RZPx47hGakrMr36aNim+ybjnLnPyywDhAByRlWeRcUwAmjeZ6lA= 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=hWvnjP+G; arc=none smtp.client-ip=209.85.214.175 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="hWvnjP+G" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-2b7adb38d65so30869025ad.2 for ; Sun, 26 Apr 2026 02:27:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1777195637; x=1777800437; 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; bh=A4lJZQuHOoBwkCuOEzBR659IeprHDGyqMQmHH4+zCQA=; b=hWvnjP+G6aiArIbv0+lIuyeJK1VuZ47oP7cMAVqTfF4J3vKJn+cJCsD+9HEnH1pPET dIdZdmsmzOf9Ds3XZTvJ1+tIslu6su9ZZN/aUubs6xzRU/BG7cF4gltYOKEudK4AvrVt H8Dlqp+pBKQlIOfVVh/9XFRiOB0Yn2Wy8u/FuSEUhP92y6Qs9AIg8S6sD3/nVLvfnprc sCXerimm/IYL9G6PnP8i+Pmlm5lnLf4zvOn57BB35nlHGlDKUBzSFVXgUGPGL7nwQ2e0 TucobcaoQcQsGSPzshMDWdSwnwgMSnKgMlspzT9EMcQGHoj3OiEt5QiK7ApuFU4XB4lU Q1mA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777195637; x=1777800437; 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; bh=A4lJZQuHOoBwkCuOEzBR659IeprHDGyqMQmHH4+zCQA=; b=qIjrBrCvmjjUo3LuK1ldfCTnU67cjWLj114sZgcX5iIt0xFj12gr760AvG4Embuiba 1PXE6bnncAX8SjqEaZYuasEPUWinLiW74pNmbGcg6HizsO2KbFZDdE5umVvbfDhtOnMP 7c8mN1A4IApY2LRpbtRTrTdWtxS1SBY9GoNKmlLpbFvoJl+5sshkMKzHfr4+smuvCA59 jLa0M1pPPA4/FdZgn9WCKFkH4m4SiMUuA0UysES4zfS76H8uWmYOxCn3p5aNZtAnP7QI hDs3pqLtACGxRmdJweFXWMjuMWdcboR5168aJS7mFggsdMK2cFPoB2CyKiBabWbMoGDv 2KJw== X-Forwarded-Encrypted: i=1; AFNElJ93uVl59RVfLOL7NAjnb5njcHfwm3fR1vO2uYFyutaYn7iMzmbTBQY7Z9UZrVHQPoPZIB9BhuPUPEbvvlY=@vger.kernel.org X-Gm-Message-State: AOJu0Yz8t6ExrJueSGafwp6g93XZWo84VtN7tz/hI01cAIR/k2DXgOXW ibNxdf1EOr79vAqmt8l9ahX2jNqJ600k8ZZNlunA3UFJHLxBzFE9DaVEPmwvLINDGqU= X-Gm-Gg: AeBDieszO1rhOjglk4j0j6czZXayyKjv3WkY7fqXS/c/hINNLdJcQPua4EfyVuI4Tan 4CtyAUtaLYl7WSkY3c6fttUbO8ovXT62jiaHHhVXAF5yMkfi5mAtn8UuMJo791qC8JXS7BYIE7/ FyrRJ7Yp67jwQu+FOKeTyDPnuX7m22Uw3+vgRpvGYUwyuIEkmeYtW4gJ9Yep91sr5A44wKIsk01 xvJ8XsUaqmrEFBbCndAgyE4tkCU6hrtIyMhRRbd0R7zcEdcmNLTZ3SAaIPOnI5iMGsqWX6t1FWb o0daK5pwwwT+B/iQW8lxg/hZdAyuP03gvwElDqcgeFj6ZwTGInul8e2Tlcu7a277+JUdwOIAYjM fdqxDLPKMUiJ9NWyYCpQDdS8j9+Mr933am4s7mYnBtHuQ+Rv5Jt5/VLoxUYa7ob0cYdiqKXAmaO ZnA0gW7RVurz9+QaTOgZgDEodlHELY X-Received: by 2002:a17:903:37c4:b0:2b7:abc0:3bd7 with SMTP id d9443c01a7336-2b7abc0448fmr145875755ad.9.1777195637394; Sun, 26 Apr 2026 02:27:17 -0700 (PDT) Received: from n232-176-004.byted.org ([240e:83:200::34a]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b5fab0caa9sm270352885ad.40.2026.04.26.02.27.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Apr 2026 02:27:17 -0700 (PDT) From: Muchun Song To: Andrew Morton , David Hildenbrand , Muchun Song , Oscar Salvador , Michael Ellerman , Madhavan Srinivasan Cc: Lorenzo Stoakes , "Liam R . Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Nicholas Piggin , Christophe Leroy , aneesh.kumar@linux.ibm.com, joao.m.martins@oracle.com, linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, Muchun Song , stable@vger.kernel.org Subject: [PATCH v7 2/6] mm/memory_hotplug: Fix incorrect altmap passing in error path Date: Sun, 26 Apr 2026 17:26:36 +0800 Message-Id: <20260426092640.375967-3-songmuchun@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260426092640.375967-1-songmuchun@bytedance.com> References: <20260426092640.375967-1-songmuchun@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In create_altmaps_and_memory_blocks(), when arch_add_memory() succeeds with memmap_on_memory enabled, the vmemmap pages are allocated from params.altmap. If create_memory_block_devices() subsequently fails, the error path calls arch_remove_memory() with a NULL altmap instead of params.altmap. This is a bug that could lead to memory corruption. Since altmap is NULL, vmemmap_free() falls back to freeing the vmemmap pages into the system buddy allocator via free_pages() instead of the altmap. arch_remove_memory() then immediately destroys the physical linear mapping for this memory. This injects unowned pages into the buddy allocator, causing machine checks or memory corruption if the system later attempts to allocate and use those freed pages. Fix this by passing params.altmap to arch_remove_memory() in the error path. Fixes: 6b8f0798b85a ("mm/memory_hotplug: split memmap_on_memory requests ac= ross memblocks") Cc: stable@vger.kernel.org Signed-off-by: Muchun Song Acked-by: David Hildenbrand (Arm) --- mm/memory_hotplug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 2a943ec57c85..0bad2aed2bde 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -1468,7 +1468,7 @@ static int create_altmaps_and_memory_blocks(int nid, = struct memory_group *group, ret =3D create_memory_block_devices(cur_start, memblock_size, nid, params.altmap, group); if (ret) { - arch_remove_memory(cur_start, memblock_size, NULL); + arch_remove_memory(cur_start, memblock_size, params.altmap); kfree(params.altmap); goto out; } --=20 2.20.1 From nobody Fri Jun 19 07:51:22 2026 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 1CBE642AA6 for ; Sun, 26 Apr 2026 09:27:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777195646; cv=none; b=f//i4eY3gR4i4bOX3eSYR+taKZy6wmsm92mwDiGLZgo+FjUNW/emsEgoWMFoh5KSs9ml13orh6Qb/3EaAyi2QcVAZcVTYJ5VxFE/tfLFoUik0mR6p9+w0v5Ef1qWx8+6wDb4QKNglqvwRkuOA+QKJPLSWSc6VAJKB+SYKC7ycoo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777195646; c=relaxed/simple; bh=i0KJZhhHvDw+LF1BAESlq5/h5j1QAqAOFqFynWrFyN4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=N+u24gr6TkXv9H1s831sOWGvx1/B3+6cDNp2ZipOEVFxj/QJR1ETFD7lHOZqWivM7PVBC8+GL2vFYjcn1/i7KUyH6WrAvFmvoo81sVdSj0YRJf5W/LdgSqNAFacFXUrqtKYc5qTaGWEOEND9UXELPNsvYuZm5yyYog2u8k58kpg= 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=DGWjXOot; arc=none smtp.client-ip=209.85.214.174 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="DGWjXOot" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-2b2589c26e3so86858095ad.1 for ; Sun, 26 Apr 2026 02:27:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1777195643; x=1777800443; 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; bh=Zr+2FRsXkOuluyT7nhch5RLSTRKRMggfn+zHryGzliY=; b=DGWjXOotBdU1bcWrmPKPPBGVvPUhamIyrw5vpCI6F2teqPP6qii2Y797DZqGAWa9tI dUx5NwBbEdH9//Ur/qLP8GWHr/VmOvHR/x2k9plea4isS0nkjP134haszk6CxyWhShFy /xbe4i171lufpAszKM1gOlYpil0Ppxc6qrA9PamclaTlPoAPmTcOEHCtD9GLUGjIY5cE v0JnMfd68Ui1+LFLLKdZmiesom8iAI3WSqulBa6SGrnwbcPWfTXMx7t49qvC/SFMUiGV 4uDdbbeAOsf8S6AiJvAWqbmdtp4UkWZT6Zv4bOiiVkrfcn6qhw9cMNeF065cBc2zvHNC UT7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777195643; x=1777800443; 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; bh=Zr+2FRsXkOuluyT7nhch5RLSTRKRMggfn+zHryGzliY=; b=MTPNia31GTGKQeiS55u+T25W6MjowrOUcDoJbLMl+4CrGithNlI1LytR9loGCeAtcK J8y+xgcvZ7NJwmbg5AUO+kvGOefA4yu9+9hZ8/jKu0bihIHWZGbIImVacNgsVJMcTQR8 Vc7jG60IaPFMQcfy2+vdY/MzsIKSRI9eUHsalo7jstw+EzD9KbnNuczTiuwKKeVmlOd1 groim3anVV/a9vqvMqbut/2aix80pN1glY32UWy37kuXPxSK9akcVz+3ARmcrWC72wwa sRL566nHRLIO4ZL2RqHcyMMc7/A2mEttJKBqKDY3/4yT+L2UJJ8+xSund4g8L3eboXxL slqw== X-Forwarded-Encrypted: i=1; AFNElJ/FyO8YgjKg4jdN6nyQXJLr6hHBtam9BU6Xj4ik2jk9UUAhr38AsYy+/fBHm/Xm9E13yUtD1K9IssuAITE=@vger.kernel.org X-Gm-Message-State: AOJu0Yx24HMoNKy7ostnc/XzrEKF72kGP3YmRAqvYl5zkO8PAa5d6IVL HIZ8g/Id4P3JnYFBBXT7DUDM2rrutjJrsZtI9/gctab78me3g+LI7kfkJr8JpAXP9hM= X-Gm-Gg: AeBDiet6ReXw/ASNooY2F5WmehO+okeRMkBain6QcPD9VOvq4ZrcgnaRI3S6rzS2b+z VJreuKFYXtoolthdhhwn19qJgjwc9dG9mIOf+kvAHt9ggwfkXFi2K3up0sK1+dGLKfZy2QDOYg0 9KcPA9iywKHKIymHN8GmccmZVUB3STUNUyThVsp2ValM0DQ6wKsO0zRpqT52aJQx7gsNmnbQuks rjeTjz4kBg37jn7dW3jzG3hpIRXfOcVAfHpLXB2hgV7t8OWFU1iLOjzG6ouW+mxUDg1c3OTeIAg 72KY9PMeUjyKd5AOzi8jnoHk+3YYiApCCYqeyQKm/pZnefs2EBZp4wl7ChASlC0a54n3P//0Vil oRdkkrEdOqD7UBQ1ciKnOGk7r9dIalLZ099H+sXQj3SoFfcOZRdrxq8dZb2g1kob91up8b7mPwz AuG/+57sjJh3q3IpxMs1mbOnBrnSW7 X-Received: by 2002:a17:902:9696:b0:2b0:4fb6:85ce with SMTP id d9443c01a7336-2b5f9f7cec4mr281168935ad.21.1777195643190; Sun, 26 Apr 2026 02:27:23 -0700 (PDT) Received: from n232-176-004.byted.org ([240e:83:200::34a]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b5fab0caa9sm270352885ad.40.2026.04.26.02.27.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Apr 2026 02:27:22 -0700 (PDT) From: Muchun Song To: Andrew Morton , David Hildenbrand , Muchun Song , Oscar Salvador , Michael Ellerman , Madhavan Srinivasan Cc: Lorenzo Stoakes , "Liam R . Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Nicholas Piggin , Christophe Leroy , aneesh.kumar@linux.ibm.com, joao.m.martins@oracle.com, linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, Muchun Song Subject: [PATCH v7 3/6] mm/sparse-vmemmap: Pass @pgmap argument to memory deactivation paths Date: Sun, 26 Apr 2026 17:26:37 +0800 Message-Id: <20260426092640.375967-4-songmuchun@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260426092640.375967-1-songmuchun@bytedance.com> References: <20260426092640.375967-1-songmuchun@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, the memory hot-remove call chain -- arch_remove_memory(), __remove_pages(), sparse_remove_section() and section_deactivate() -- does not carry the struct dev_pagemap pointer. This prevents the lower levels from knowing whether the section was originally populated with vmemmap optimizations (e.g., DAX with vmemmap optimization enabled). Without this information, we cannot call vmemmap_can_optimize() to determine if the vmemmap pages were optimized. As a result, the vmemmap page accounting during teardown will mistakenly assume a non-optimized allocation, leading to incorrect memmap statistics. To lay the groundwork for fixing the vmemmap page accounting, we need to pass the @pgmap pointer down to the deactivation location. Plumb the @pgmap argument through the APIs of arch_remove_memory(), __remove_pages() and sparse_remove_section(), mirroring the corresponding *_activate() paths. Signed-off-by: Muchun Song Acked-by: Mike Rapoport (Microsoft) Reviewed-by: Oscar Salvador Acked-by: David Hildenbrand (Arm) --- arch/arm64/mm/mmu.c | 5 +++-- arch/loongarch/mm/init.c | 5 +++-- arch/powerpc/mm/mem.c | 5 +++-- arch/riscv/mm/init.c | 5 +++-- arch/s390/mm/init.c | 5 +++-- arch/x86/mm/init_64.c | 5 +++-- include/linux/memory_hotplug.h | 8 +++++--- mm/memory_hotplug.c | 13 +++++++------ mm/memremap.c | 4 ++-- mm/sparse-vmemmap.c | 12 ++++++------ 10 files changed, 38 insertions(+), 29 deletions(-) diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c index dd85e093ffdb..e5a42b7a0160 100644 --- a/arch/arm64/mm/mmu.c +++ b/arch/arm64/mm/mmu.c @@ -2024,12 +2024,13 @@ int arch_add_memory(int nid, u64 start, u64 size, return ret; } =20 -void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) +void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap, + struct dev_pagemap *pgmap) { unsigned long start_pfn =3D start >> PAGE_SHIFT; unsigned long nr_pages =3D size >> PAGE_SHIFT; =20 - __remove_pages(start_pfn, nr_pages, altmap); + __remove_pages(start_pfn, nr_pages, altmap, pgmap); __remove_pgd_mapping(swapper_pg_dir, __phys_to_virt(start), size); } =20 diff --git a/arch/loongarch/mm/init.c b/arch/loongarch/mm/init.c index 3f9ab54114c5..055ecd2c8fd9 100644 --- a/arch/loongarch/mm/init.c +++ b/arch/loongarch/mm/init.c @@ -119,7 +119,8 @@ int arch_add_memory(int nid, u64 start, u64 size, struc= t mhp_params *params) return ret; } =20 -void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) +void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap, + struct dev_pagemap *pgmap) { unsigned long start_pfn =3D start >> PAGE_SHIFT; unsigned long nr_pages =3D size >> PAGE_SHIFT; @@ -128,7 +129,7 @@ void arch_remove_memory(u64 start, u64 size, struct vme= m_altmap *altmap) /* With altmap the first mapped page is offset from @start */ if (altmap) page +=3D vmem_altmap_offset(altmap); - __remove_pages(start_pfn, nr_pages, altmap); + __remove_pages(start_pfn, nr_pages, altmap, pgmap); } #endif =20 diff --git a/arch/powerpc/mm/mem.c b/arch/powerpc/mm/mem.c index 648d0c5602ec..4c1afab91996 100644 --- a/arch/powerpc/mm/mem.c +++ b/arch/powerpc/mm/mem.c @@ -158,12 +158,13 @@ int __ref arch_add_memory(int nid, u64 start, u64 siz= e, return rc; } =20 -void __ref arch_remove_memory(u64 start, u64 size, struct vmem_altmap *alt= map) +void __ref arch_remove_memory(u64 start, u64 size, struct vmem_altmap *alt= map, + struct dev_pagemap *pgmap) { unsigned long start_pfn =3D start >> PAGE_SHIFT; unsigned long nr_pages =3D size >> PAGE_SHIFT; =20 - __remove_pages(start_pfn, nr_pages, altmap); + __remove_pages(start_pfn, nr_pages, altmap, pgmap); arch_remove_linear_mapping(start, size); } #endif diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index decd7df40fa4..b0092fb842a3 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -1717,9 +1717,10 @@ int __ref arch_add_memory(int nid, u64 start, u64 si= ze, struct mhp_params *param return ret; } =20 -void __ref arch_remove_memory(u64 start, u64 size, struct vmem_altmap *alt= map) +void __ref arch_remove_memory(u64 start, u64 size, struct vmem_altmap *alt= map, + struct dev_pagemap *pgmap) { - __remove_pages(start >> PAGE_SHIFT, size >> PAGE_SHIFT, altmap); + __remove_pages(start >> PAGE_SHIFT, size >> PAGE_SHIFT, altmap, pgmap); remove_linear_mapping(start, size); flush_tlb_all(); } diff --git a/arch/s390/mm/init.c b/arch/s390/mm/init.c index 1f72efc2a579..11a689423440 100644 --- a/arch/s390/mm/init.c +++ b/arch/s390/mm/init.c @@ -276,12 +276,13 @@ int arch_add_memory(int nid, u64 start, u64 size, return rc; } =20 -void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) +void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap, + struct dev_pagemap *pgmap) { unsigned long start_pfn =3D start >> PAGE_SHIFT; unsigned long nr_pages =3D size >> PAGE_SHIFT; =20 - __remove_pages(start_pfn, nr_pages, altmap); + __remove_pages(start_pfn, nr_pages, altmap, pgmap); vmem_remove_mapping(start, size); } #endif /* CONFIG_MEMORY_HOTPLUG */ diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c index df2261fa4f98..77b889b71cf3 100644 --- a/arch/x86/mm/init_64.c +++ b/arch/x86/mm/init_64.c @@ -1288,12 +1288,13 @@ kernel_physical_mapping_remove(unsigned long start,= unsigned long end) remove_pagetable(start, end, true, NULL); } =20 -void __ref arch_remove_memory(u64 start, u64 size, struct vmem_altmap *alt= map) +void __ref arch_remove_memory(u64 start, u64 size, struct vmem_altmap *alt= map, + struct dev_pagemap *pgmap) { unsigned long start_pfn =3D start >> PAGE_SHIFT; unsigned long nr_pages =3D size >> PAGE_SHIFT; =20 - __remove_pages(start_pfn, nr_pages, altmap); + __remove_pages(start_pfn, nr_pages, altmap, pgmap); kernel_physical_mapping_remove(start, start + size); } #endif /* CONFIG_MEMORY_HOTPLUG */ diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h index 815e908c4135..7c9d66729c60 100644 --- a/include/linux/memory_hotplug.h +++ b/include/linux/memory_hotplug.h @@ -135,9 +135,10 @@ static inline bool movable_node_is_enabled(void) return movable_node_enabled; } =20 -extern void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *al= tmap); +extern void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *al= tmap, + struct dev_pagemap *pgmap); extern void __remove_pages(unsigned long start_pfn, unsigned long nr_pages, - struct vmem_altmap *altmap); + struct vmem_altmap *altmap, struct dev_pagemap *pgmap); =20 /* reasonably generic interface to expand the physical pages */ extern int __add_pages(int nid, unsigned long start_pfn, unsigned long nr_= pages, @@ -307,7 +308,8 @@ extern int sparse_add_section(int nid, unsigned long pf= n, unsigned long nr_pages, struct vmem_altmap *altmap, struct dev_pagemap *pgmap); extern void sparse_remove_section(unsigned long pfn, unsigned long nr_page= s, - struct vmem_altmap *altmap); + struct vmem_altmap *altmap, + struct dev_pagemap *pgmap); extern struct zone *zone_for_pfn_range(enum mmop online_type, int nid, struct memory_group *group, unsigned long start_pfn, unsigned long nr_pages); diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 0bad2aed2bde..7bfdc3a99688 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -576,6 +576,7 @@ void remove_pfn_range_from_zone(struct zone *zone, * @pfn: starting pageframe (must be aligned to start of a section) * @nr_pages: number of pages to remove (must be multiple of section size) * @altmap: alternative device page map or %NULL if default memmap is used + * @pgmap: device page map or %NULL if not ZONE_DEVICE * * Generic helper function to remove section mappings and sysfs entries * for the section of the memory we are removing. Caller needs to make @@ -583,7 +584,7 @@ void remove_pfn_range_from_zone(struct zone *zone, * calling offline_pages(). */ void __remove_pages(unsigned long pfn, unsigned long nr_pages, - struct vmem_altmap *altmap) + struct vmem_altmap *altmap, struct dev_pagemap *pgmap) { const unsigned long end_pfn =3D pfn + nr_pages; unsigned long cur_nr_pages; @@ -598,7 +599,7 @@ void __remove_pages(unsigned long pfn, unsigned long nr= _pages, /* Select all remaining pages up to the next section boundary */ cur_nr_pages =3D min(end_pfn - pfn, SECTION_ALIGN_UP(pfn + 1) - pfn); - sparse_remove_section(pfn, cur_nr_pages, altmap); + sparse_remove_section(pfn, cur_nr_pages, altmap, pgmap); } } =20 @@ -1425,7 +1426,7 @@ static void remove_memory_blocks_and_altmaps(u64 star= t, u64 size) =20 remove_memory_block_devices(cur_start, memblock_size); =20 - arch_remove_memory(cur_start, memblock_size, altmap); + arch_remove_memory(cur_start, memblock_size, altmap, NULL); =20 /* Verify that all vmemmap pages have actually been freed. */ WARN(altmap->alloc, "Altmap not fully unmapped"); @@ -1468,7 +1469,7 @@ static int create_altmaps_and_memory_blocks(int nid, = struct memory_group *group, ret =3D create_memory_block_devices(cur_start, memblock_size, nid, params.altmap, group); if (ret) { - arch_remove_memory(cur_start, memblock_size, params.altmap); + arch_remove_memory(cur_start, memblock_size, params.altmap, NULL); kfree(params.altmap); goto out; } @@ -1554,7 +1555,7 @@ int add_memory_resource(int nid, struct resource *res= , mhp_t mhp_flags) /* create memory block devices after memory was added */ ret =3D create_memory_block_devices(start, size, nid, NULL, group); if (ret) { - arch_remove_memory(start, size, params.altmap); + arch_remove_memory(start, size, params.altmap, NULL); goto error; } } @@ -2266,7 +2267,7 @@ static int try_remove_memory(u64 start, u64 size) * No altmaps present, do the removal directly */ remove_memory_block_devices(start, size); - arch_remove_memory(start, size, NULL); + arch_remove_memory(start, size, NULL, NULL); } else { /* all memblocks in the range have altmaps */ remove_memory_blocks_and_altmaps(start, size); diff --git a/mm/memremap.c b/mm/memremap.c index 053842d45cb1..81766d822400 100644 --- a/mm/memremap.c +++ b/mm/memremap.c @@ -97,10 +97,10 @@ static void pageunmap_range(struct dev_pagemap *pgmap, = int range_id) PHYS_PFN(range_len(range))); if (pgmap->type =3D=3D MEMORY_DEVICE_PRIVATE) { __remove_pages(PHYS_PFN(range->start), - PHYS_PFN(range_len(range)), NULL); + PHYS_PFN(range_len(range)), NULL, pgmap); } else { arch_remove_memory(range->start, range_len(range), - pgmap_altmap(pgmap)); + pgmap_altmap(pgmap), pgmap); kasan_remove_zero_shadow(__va(range->start), range_len(range)); } mem_hotplug_done(); diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c index a7b11248b989..3340f6d30b01 100644 --- a/mm/sparse-vmemmap.c +++ b/mm/sparse-vmemmap.c @@ -665,7 +665,7 @@ static struct page * __meminit populate_section_memmap(= unsigned long pfn, } =20 static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_= pages, - struct vmem_altmap *altmap) + struct vmem_altmap *altmap, struct dev_pagemap *pgmap) { unsigned long start =3D (unsigned long) pfn_to_page(pfn); unsigned long end =3D start + nr_pages * sizeof(struct page); @@ -746,7 +746,7 @@ static int fill_subsection_map(unsigned long pfn, unsig= ned long nr_pages) * usage map, but still need to free the vmemmap range. */ static void section_deactivate(unsigned long pfn, unsigned long nr_pages, - struct vmem_altmap *altmap) + struct vmem_altmap *altmap, struct dev_pagemap *pgmap) { struct mem_section *ms =3D __pfn_to_section(pfn); bool section_is_early =3D early_section(ms); @@ -784,7 +784,7 @@ static void section_deactivate(unsigned long pfn, unsig= ned long nr_pages, * section_activate() and pfn_valid() . */ if (!section_is_early) - depopulate_section_memmap(pfn, nr_pages, altmap); + depopulate_section_memmap(pfn, nr_pages, altmap, pgmap); else if (memmap) free_map_bootmem(memmap); =20 @@ -828,7 +828,7 @@ static struct page * __meminit section_activate(int nid= , unsigned long pfn, =20 memmap =3D populate_section_memmap(pfn, nr_pages, nid, altmap, pgmap); if (!memmap) { - section_deactivate(pfn, nr_pages, altmap); + section_deactivate(pfn, nr_pages, altmap, pgmap); return ERR_PTR(-ENOMEM); } =20 @@ -889,13 +889,13 @@ int __meminit sparse_add_section(int nid, unsigned lo= ng start_pfn, } =20 void sparse_remove_section(unsigned long pfn, unsigned long nr_pages, - struct vmem_altmap *altmap) + struct vmem_altmap *altmap, struct dev_pagemap *pgmap) { struct mem_section *ms =3D __pfn_to_section(pfn); =20 if (WARN_ON_ONCE(!valid_section(ms))) return; =20 - section_deactivate(pfn, nr_pages, altmap); + section_deactivate(pfn, nr_pages, altmap, pgmap); } #endif /* CONFIG_MEMORY_HOTPLUG */ --=20 2.20.1 From nobody Fri Jun 19 07:51:22 2026 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (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 4339D330B29 for ; Sun, 26 Apr 2026 09:27:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777195651; cv=none; b=pT8YYRoVCxtn2N1oSNWvT7pBUUJBGl3xCOdE7ekY8Yizur2OebirdcmiQivL+ys4CLr3CS8ucHV87wdY/erQ9QJ8TH5HkYfBbaqfLZsf/uv6/j5PON4J3uXdRpIWS17J9zIQp1ytYKmnhMX7Y+fhtoNqJh+6Nr6WaLaRHhqqMI8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777195651; c=relaxed/simple; bh=qTrK7sGxk9cj5ND2z4IoB1RRjTFuoKl+iErfAtNDycI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gFxZlrWghkRFsRriYxZlPdAdKmEH18V79ldxLr7HZttwVdEjMttr4+eBuEnDOm5zbdEiCSJmYxrnKpWs+IbQ18ZSOYFCnIpBzgJwEfNdvdamxQOJLuZzdwpEaBRu6jD7jUmnxrYlNQhY+HLOL4fEpv2fgDrPE2xoa1IjpFlDIF8= 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=ClSoy0D4; arc=none smtp.client-ip=209.85.214.180 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="ClSoy0D4" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-2b7adb38d65so30869845ad.2 for ; Sun, 26 Apr 2026 02:27:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1777195650; x=1777800450; 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; bh=CoquneTBQ8S9NM0KYy8UH3zDqZYMfaQ5136cWznpdGc=; b=ClSoy0D4jenxkFLpRU3XF7dpySadpYx9CcHSMUh+xoWey7IiAWH7ZNqdmSS91Xa0Ez Dtyh6rWLJ6xeN+x2HM6Eun1fhfhZYwq2HLOgdvkBodEjhMKXfZuxx+vrlGW9U660QnBM rAqMyFi+NWgkxv7vptQYS5RbBJA4PQIwtNnbTSog8wjlb9FBHlfE6eFrnrh8xaorPXnt NBTb7nIRVh2OkNZcfv/jIGoLu3Uw+80bA4IFgeoLw8W+2CuZm0yIHn2UTJbd3mVJW74a T5wLNPHdVLau9XLfM0igrDbXKc1tbA80KTHzEU1Hz5ngl8bs3jtBXBNIfhHKqHmaTw1t 7u+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777195650; x=1777800450; 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; bh=CoquneTBQ8S9NM0KYy8UH3zDqZYMfaQ5136cWznpdGc=; b=ejdRo2+BaByp0wLk/FQgNvUoB6CtbwDkG34pupSSFEDewWWJOI+WHPtECn8qShIpwu Jmd0TNqnF2Zr7ZzDTk5L17VhDaUnsX2SoqupyEU2Yvhg0hp4ylKm0lsj24cK2JUwyGq1 3ZkeElovMAjasG9QoWd5m1qL0huWX/7jWZ6Ko10pX2nMGmqqv+wTRZVoCizMHwnWr1BV yEpGWo0J8o15EusRJ1s4WCHqIFSh/8gaQ8FnWsC6iVccZ1IHftN+4m+NBSSCfpkDbT3x bgnyd8tG7MA0pb91+1oZA/qsNS68cpA+2Zb6CIFPsi6x4iO8JFIHUjEPnMmBbVEhR3iF uwWQ== X-Forwarded-Encrypted: i=1; AFNElJ+s74S1PR7WSRgYmzXwcoCx1Tuo/ETH0UgAWYnCNKq3s4NIsoqDlUAt5FapzqATYTrhRxaPGMPBpMuXDK4=@vger.kernel.org X-Gm-Message-State: AOJu0Yz7F45bawMLKWi1mIRrfX8F4Iw2MQMTViiVosYr/fw2D/kgO1lM 7sG9HU703lknA18yCqpHH4BbUHvFx/Znd+dlFGzHy0ahPp/sqQBB4r5Q+FmrMaVXA3Y= X-Gm-Gg: AeBDiet3L0eLj6Yvr+56ssOjeyzbVgjNiycXjksSAYHTSK2IraMYWeDBGDaqnP8rK+0 6h982oyFndWkv48EfreHfaWsBVtt80ABy+a/+RtaWREDury8RAvGeDJvy5V6PfFTbLXfU2d1PTo r4zDvZlQ71yXNUjEsMiSLum/oghHGpdf4bbJHYztttXwvT616pZ9XqicvZvJRZ4UbJ5/nt7nmF6 BxtV0bCP2FZx/LQENVRnSFC8t3jO7PNDGLG7VMU58hKPX3JElyZQP0dCRPuCsdIQNHvPNyEaJvT EP/B+cR6AEDuWIt9Ybf/sBNWhg0koe6YnXkXJE7ka26sgO+ngXrPeDvnki4wD26NCupVjk9RmGL NsxGgBKKbphSaXd+XoUqAvh+db9WFvimR7MeltxJ5sDWBBGMAOIkq7HiWUG4nSG2RH+ISRCS7Fi AgKIsZFjBQLVIUfi3vZQC9wRU6anLF X-Received: by 2002:a17:902:e746:b0:2b0:5a4c:7263 with SMTP id d9443c01a7336-2b5f9ef646amr454625165ad.18.1777195649505; Sun, 26 Apr 2026 02:27:29 -0700 (PDT) Received: from n232-176-004.byted.org ([240e:83:200::34a]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b5fab0caa9sm270352885ad.40.2026.04.26.02.27.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Apr 2026 02:27:29 -0700 (PDT) From: Muchun Song To: Andrew Morton , David Hildenbrand , Muchun Song , Oscar Salvador , Michael Ellerman , Madhavan Srinivasan Cc: Lorenzo Stoakes , "Liam R . Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Nicholas Piggin , Christophe Leroy , aneesh.kumar@linux.ibm.com, joao.m.martins@oracle.com, linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, Muchun Song , stable@vger.kernel.org Subject: [PATCH v7 4/6] mm/sparse-vmemmap: Fix DAX vmemmap accounting with optimization Date: Sun, 26 Apr 2026 17:26:38 +0800 Message-Id: <20260426092640.375967-5-songmuchun@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260426092640.375967-1-songmuchun@bytedance.com> References: <20260426092640.375967-1-songmuchun@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When vmemmap optimization is enabled for DAX, the nr_memmap_pages counter in /proc/vmstat is incorrect. The current code always accounts for the full, non-optimized vmemmap size, but vmemmap optimization reduces the actual number of vmemmap pages by reusing tail pages. This causes the system to overcount vmemmap usage, leading to inaccurate page statistics in /proc/vmstat. Fix this by introducing section_nr_vmemmap_pages(), which returns the exact vmemmap page count for a given pfn range based on whether optimization is in effect. Fixes: 15995a352474 ("mm: report per-page metadata information") Cc: stable@vger.kernel.org Signed-off-by: Muchun Song Acked-by: Mike Rapoport (Microsoft) Acked-by: Oscar Salvador Acked-by: David Hildenbrand (Arm) --- v6 -> v7: - Refine the alignment assertions in section_nr_vmemmap_pages(). --- mm/sparse-vmemmap.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c index 3340f6d30b01..01f448607bad 100644 --- a/mm/sparse-vmemmap.c +++ b/mm/sparse-vmemmap.c @@ -652,6 +652,31 @@ void offline_mem_sections(unsigned long start_pfn, uns= igned long end_pfn) } } =20 +static int __meminit section_nr_vmemmap_pages(unsigned long pfn, unsigned = long nr_pages, + struct vmem_altmap *altmap, struct dev_pagemap *pgmap) +{ + const unsigned int order =3D pgmap ? pgmap->vmemmap_shift : 0; + const unsigned long pages_per_compound =3D 1UL << order; + + VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SUBSECTION)); + + if (!vmemmap_can_optimize(altmap, pgmap)) + return DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE); + + if (order < PFN_SECTION_SHIFT) { + VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, pages_per_compound)); + return VMEMMAP_RESERVE_NR * nr_pages / pages_per_compound; + } + + VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION)); + VM_WARN_ON_ONCE(nr_pages > PAGES_PER_SECTION); + + if (IS_ALIGNED(pfn, pages_per_compound)) + return VMEMMAP_RESERVE_NR; + + return 0; +} + static struct page * __meminit populate_section_memmap(unsigned long pfn, unsigned long nr_pages, int nid, struct vmem_altmap *altmap, struct dev_pagemap *pgmap) @@ -659,7 +684,7 @@ static struct page * __meminit populate_section_memmap(= unsigned long pfn, struct page *page =3D __populate_section_memmap(pfn, nr_pages, nid, altma= p, pgmap); =20 - memmap_pages_add(DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE)); + memmap_pages_add(section_nr_vmemmap_pages(pfn, nr_pages, altmap, pgmap)); =20 return page; } @@ -670,7 +695,7 @@ static void depopulate_section_memmap(unsigned long pfn= , unsigned long nr_pages, unsigned long start =3D (unsigned long) pfn_to_page(pfn); unsigned long end =3D start + nr_pages * sizeof(struct page); =20 - memmap_pages_add(-1L * (DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE= _SIZE))); + memmap_pages_add(-section_nr_vmemmap_pages(pfn, nr_pages, altmap, pgmap)); vmemmap_free(start, end, altmap); } =20 @@ -678,9 +703,10 @@ static void free_map_bootmem(struct page *memmap) { unsigned long start =3D (unsigned long)memmap; unsigned long end =3D (unsigned long)(memmap + PAGES_PER_SECTION); + unsigned long pfn =3D page_to_pfn(memmap); =20 - memmap_boot_pages_add(-1L * (DIV_ROUND_UP(PAGES_PER_SECTION * sizeof(stru= ct page), - PAGE_SIZE))); + memmap_boot_pages_add(-section_nr_vmemmap_pages(pfn, PAGES_PER_SECTION, + NULL, NULL)); vmemmap_free(start, end, NULL); } =20 --=20 2.20.1 From nobody Fri Jun 19 07:51:22 2026 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 02A30346AC0 for ; Sun, 26 Apr 2026 09:27:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777195656; cv=none; b=cioKS43cD433DfvAM1TO2SjIRBJ+d+7ZzIiTZvhvWOYCcd6HyWIyMO80iiger51hYj4WrEBi4FW8oK67Lgw2RSxoHlE7obPzR6ztomRI0HNLoQp6qo+SMPw9mkm/NKpFo79Av/AnR4x/Kl2ymttsrz+v9pH3Il1rqqxrqDgXHPY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777195656; c=relaxed/simple; bh=I09DVnwUmT2FgjMlL1sbr+fqXGO2fhcK42PNEcwA7S4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=F0KpnyZpdtwteOB/5Lg/TFoX06dKTCchWsUuQqd4cJI1Ebuiwi9Naj977i4qBFUOQys4WyiRvHnSrA1YbnMeRjrZyYCuNBn/kDS1O/c8Acv1TTuc/2JQBRBerAkLY4CSp0gkH31BZmdFN/drymmSd7DnzeHGsl49WfF2iwcWAkI= 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=CV9sSo4v; arc=none smtp.client-ip=209.85.214.177 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="CV9sSo4v" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-2b788a98557so41725965ad.2 for ; Sun, 26 Apr 2026 02:27:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1777195654; x=1777800454; 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; bh=X8jAPi1Ug7YtuxDodtBim0VZYTDek6DS5vd5ro+n0/g=; b=CV9sSo4vcajq7s81xB9BEfrNcdptGVmM8h6nm1AHNRYBH56rFuvsig/uG1JTzSwvhN kyWlETA/ZcLPj4K1pcgNFvmSmDF2Yz0N8QTmbCxVr6xv32dkQwCaoDjCKmMKRgpVBHFD C3nTZiA2/InQDOUPZE103HJqcsKKer7eIozsrZ8Vzi9E1JObbYSULQfZtXfZR2P5rjM/ MUrC0DPlU8yO+RtfG9MewKKV7xZ65oMI18SfjHSJ7c+LPf8DShud6GmHy2cEJSnQpBYJ WWACcKAg/f8VHOdm2Qki0ABe4E34OIDoW4rEfcwbEjSKjUAtQ0PrTdNI2eKZFq0xBPTZ lEJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777195654; x=1777800454; 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; bh=X8jAPi1Ug7YtuxDodtBim0VZYTDek6DS5vd5ro+n0/g=; b=hqmRA3ziAa+ki272kWxsikVrrWm0e9DkWl4g18y1oQwCJp0rmjXD/mCuTYysIDJdTL f5jXkNNQ95BgW5YzZ0vZ0woY6iYn0MtUCMA/EfuoOpc3wESlAZBh07l5TEDpL+cU15Bm i9nZZdm0o63cU6uakmrudpE/q72iE6HnWWfsFykziqyXUHq0u00PGPkJZa9pYKrgn+zs DNDWGxBqsnmO62mJI7OQK+4W7s6LoVNkMZBBdF9YjF5tBMD3v0XCYHfzkBJF1EtbrKtn zJmTmZOEv/IluYp8RbB8AaswNsM4qV4pM9EEOhrGX4KGgYbpgVOMJcr4FZPzazy9hfcH nVug== X-Forwarded-Encrypted: i=1; AFNElJ/2WacQ5tpc2zc1Of4mLtdRxBGJMLOuePc0VPRLdbsRbBppM96hx9575WlavtXTOUi1F5ovSqWVg0ZNoY4=@vger.kernel.org X-Gm-Message-State: AOJu0Yz7d1pLhMq2G1/9ciLQH0FBpcoLSq1yaMgHt7kKkfQ+SpmMdaln hEDryIYiPKaYDeEskEmbmqdGPCUNeCXf2ln5QtWbKVxsIyi+Gkr+dlriGTCitBEyWv8= X-Gm-Gg: AeBDieuCw/TXL6fwLm7250IDPfJInTW9diGcpL4K5vk4Z5Fkwg3IS8aPpopnxlaL3qY TxTtLaS2hRTKNQWGgdrF5AiRntuVZdOo3eRlehq92F84bFZFaxVpg1VCwk/4+VR1BPmIV0753v3 nYYuGf4N2cfJMw9QlIjIQeb6f9chC4b4w7G6tLJ5V9QhG+ALCKcpJR0iCOBFE0m25kgC83ScNva ibEVDt4I1b1KfIwNQkrIEdpR9uoRsioGRxn/fsPcoQjg5C482dUeiJdmn8DPrrJSEDOfpceMq02 aCShMcGCCmkdaAldlWyXZPN8UF4CYq+Eo4xKNXWAglg6ZSyA1QP+on6NJAPnK2QFblcLKEk5xVW 5VdbgsejsWQ4k9SYxSVPsbHIvlqWs0Mw9/V5UI8MwHnyJ3rz026mLTy38G4BjUpBUIWgbJsbKAh 3hzsof99MPqDQ8yfIksmgw4UUjShhg X-Received: by 2002:a17:902:da8b:b0:2b9:547d:ccc8 with SMTP id d9443c01a7336-2b9547dce05mr31133985ad.30.1777195654294; Sun, 26 Apr 2026 02:27:34 -0700 (PDT) Received: from n232-176-004.byted.org ([240e:83:200::34a]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b5fab0caa9sm270352885ad.40.2026.04.26.02.27.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Apr 2026 02:27:33 -0700 (PDT) From: Muchun Song To: Andrew Morton , David Hildenbrand , Muchun Song , Oscar Salvador , Michael Ellerman , Madhavan Srinivasan Cc: Lorenzo Stoakes , "Liam R . Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Nicholas Piggin , Christophe Leroy , aneesh.kumar@linux.ibm.com, joao.m.martins@oracle.com, linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, Muchun Song , stable@vger.kernel.org Subject: [PATCH v7 5/6] mm/mm_init: Fix pageblock migratetype for ZONE_DEVICE compound pages Date: Sun, 26 Apr 2026 17:26:39 +0800 Message-Id: <20260426092640.375967-6-songmuchun@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260426092640.375967-1-songmuchun@bytedance.com> References: <20260426092640.375967-1-songmuchun@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The memmap_init_zone_device() function only initializes the migratetype of the first pageblock of a compound page. If the compound page size exceeds pageblock_nr_pages (e.g., 1GB hugepages with 2MB pageblocks), subsequent pageblocks in the compound page remain uninitialized. Move the migratetype initialization out of __init_zone_device_page() and into a separate pageblock_migratetype_init_range() function. This iterates over the entire PFN range of the memory, ensuring that all pageblocks are correctly initialized. Also remove the stale confusing comment about MEMINIT_HOTPLUG above the migratetype setting since it is an obsolete relic from commit 966cf44f637e ("mm: defer ZONE_DEVICE page initialization to the point where we init pgmap") and no longer makes sense here. Fixes: c4386bd8ee3a ("mm/memremap: add ZONE_DEVICE support for compound pag= es") Cc: stable@vger.kernel.org Signed-off-by: Muchun Song Reviewed-by: Mike Rapoport (Microsoft) Reviewed-by: Oscar Salvador Acked-by: David Hildenbrand (Arm) --- mm/mm_init.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/mm/mm_init.c b/mm/mm_init.c index f9f8e1af921c..cfc76953e249 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -674,6 +674,20 @@ static inline void fixup_hashdist(void) static inline void fixup_hashdist(void) {} #endif /* CONFIG_NUMA */ =20 +#ifdef CONFIG_ZONE_DEVICE +static __meminit void pageblock_migratetype_init_range(unsigned long pfn, + unsigned long nr_pages, int migratetype) +{ + const unsigned long end =3D pfn + nr_pages; + + for (pfn =3D pageblock_align(pfn); pfn < end; pfn +=3D pageblock_nr_pages= ) { + init_pageblock_migratetype(pfn_to_page(pfn), migratetype, false); + if (IS_ALIGNED(pfn, PAGES_PER_SECTION)) + cond_resched(); + } +} +#endif + /* * Initialize a reserved page unconditionally, finding its zone first. */ @@ -1011,21 +1025,6 @@ static void __ref __init_zone_device_page(struct pag= e *page, unsigned long pfn, page_folio(page)->pgmap =3D pgmap; page->zone_device_data =3D NULL; =20 - /* - * Mark the block movable so that blocks are reserved for - * movable at startup. This will force kernel allocations - * to reserve their blocks rather than leaking throughout - * the address space during boot when many long-lived - * kernel allocations are made. - * - * Please note that MEMINIT_HOTPLUG path doesn't clear memmap - * because this is done early in section_activate() - */ - if (pageblock_aligned(pfn)) { - init_pageblock_migratetype(page, MIGRATE_MOVABLE, false); - cond_resched(); - } - /* * ZONE_DEVICE pages other than MEMORY_TYPE_GENERIC are released * directly to the driver page allocator which will set the page count @@ -1122,6 +1121,9 @@ void __ref memmap_init_zone_device(struct zone *zone, =20 __init_zone_device_page(page, pfn, zone_idx, nid, pgmap); =20 + if (IS_ALIGNED(pfn, PAGES_PER_SECTION)) + cond_resched(); + if (pfns_per_compound =3D=3D 1) continue; =20 @@ -1129,6 +1131,8 @@ void __ref memmap_init_zone_device(struct zone *zone, compound_nr_pages(altmap, pgmap)); } =20 + pageblock_migratetype_init_range(start_pfn, nr_pages, MIGRATE_MOVABLE); + pr_debug("%s initialised %lu pages in %ums\n", __func__, nr_pages, jiffies_to_msecs(jiffies - start)); } --=20 2.20.1 From nobody Fri Jun 19 07:51:22 2026 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 8060031F98D for ; Sun, 26 Apr 2026 09:27:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777195661; cv=none; b=LSXTQZHzKt0hSuSfxoORp8AB9+rWzO03VkMc4F+zSnn2BPVUtSWhwFoCVdqu3Sa1CdA6YwcbCSx1eG+X0La6iqq1uEddf4JahDcHHFUCZpcNXXa77xV3BIzbHDZAmW/521Hjl4AeOgsIZuCaeefu2Ti6iaX5yOicieZmtFVNLRU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777195661; c=relaxed/simple; bh=ywbIjiAmJOpr0oswLhWAUDJeFXDBn1/X+WWcoQ+ww1k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IIZIc1CFCGQrbukfEI0C5ndqBOXaNYIdaH5KMigVL72CGdYRwSfPmQ3DWddduCvhBsvM6haDGuMXQPTiAy/Ku4hYBNQJa/5MZuIW4A/oPoxZbiBJYHGUVpwGZnI72mQ+IsZymr6JIbvSGYPsWbiJBG/Op5hIW9+jI9OznIapc0U= 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=MQh9XJ/P; arc=none smtp.client-ip=209.85.214.179 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="MQh9XJ/P" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2b7adb38d65so30870425ad.2 for ; Sun, 26 Apr 2026 02:27:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1777195660; x=1777800460; 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; bh=sKHGjwyqOWL6rVsBt0Sw/YSvydX7Gt+2C2Tpk5skGPU=; b=MQh9XJ/Pazwy6fHSn8fEfl68CZW3nrFka/xAXMkqtUgqVQ8qcz8KuPE3w9Dxtaowxm Yd8Zr0b3wpfkDS1rIq2dQUWov6DAhbWtEK1nNBMxbJ1It6Y2Q6LVenqhDV/Tx0qGjtpV 1b20t67ugwXbVv181N7/cwmeTpFnEOd1HmORriHF88PlcpBWBtog/aewQe0Yg8okKbIU lrY38mKrP/afZSng8iB7IYfMvPuW8eQSfVm2NDEA8inhbaV9nePEGDHWXdxgTNjmjCVA qSCu+axhniZT7d1QRsFnQaBvwDBYVpEMUyEl5eb7fCxeheIbdAaSfWmSey+JJBpBHXjO 4/TQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777195660; x=1777800460; 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; bh=sKHGjwyqOWL6rVsBt0Sw/YSvydX7Gt+2C2Tpk5skGPU=; b=EbZ66CVgDLrkSLJqv4fzzREbZwMZrq+xfPCnoBCPgDpHlb4Lo0EenxOHXISJZkw30h u/4Ru5htPYSS/by1v5B0GU2DePpeWI0vEPMIu33lIOqD0pwhijceL/lVsX3M29QOlAvw NLgtYRr52VlzWD4l5tDmK3fJcbdHZI+1FQuw85fKR4W20eu3MSEC43ksU8gAGNOOwg7J pUzvIodGzXiLQOPYySl9cnx6GQK6qH64VItbwq1ornc6+TiUyr1hbElWWEtPUkoUjooQ f3MpCWWM9eOIuGJ1bVZTAfLzSmncdjTANS6hpt/bor9GIcxfdZyTPvZ50++yVr4ShzK3 dBkg== X-Forwarded-Encrypted: i=1; AFNElJ8+2mTS5IqdS6umZ3jVnFumnXlSublNSaH1VTwuw9pYhu97v20b6LlGRcuMplLDBGkWbb7mjKagWC5W7fA=@vger.kernel.org X-Gm-Message-State: AOJu0YzsLXflB6btGtfcwgbqPE45faB/bVFnOurqFvcy5XZf2NcAg7Ib OtEwClv36pNbzHAbzLC7BLd3qQCrpNvyNNTZOn5wIqjtzm5Qh2lXV1IjkNdYwdw6dsg= X-Gm-Gg: AeBDietsHQH02NxNxKsSKf6G8n2BwclOAIEOKJbq92GW2O103pUTOdJi/8sh7HNjsBi Oy5MsQTSzT4xnWKBV6TBigqWQGzJ3g1gjcP1/4K+Egi7qxsAKowTxQ99VkMQHIlCWAdb8kwWcUp SPYRm7iKhkHpcGfWKv51kfKTv5m7d5xiF0yuANb9rej+ZVqF2H0eDuIc14Mz5bKbLBuBnTbKOE6 0M1Bb4xFoJ/Q2FdxCTA97pJ48i8EDSSxPvAsT9UaC9cypDTn73XjFviiaw3NBca2osJXuozqP7y I2kCkL6xsRUNJ1qfqC+VmGgxBqufZ0lGIk4KJb1lc/L/0Q5KGfL63+QWOdKx9heRhCHfPe6xO9w Dp1RKodbzish/CsXvfDw2GeF5DTXdG2NbdQRB8l0rph3u68V/iOI2zDPwjSos0cldoObVebxGgd uEDtg6RgYG91rivSY5OqArWHMY+qxE X-Received: by 2002:a17:902:d652:b0:2b4:62bd:ee3 with SMTP id d9443c01a7336-2b5f9f5eac7mr310134635ad.33.1777195659828; Sun, 26 Apr 2026 02:27:39 -0700 (PDT) Received: from n232-176-004.byted.org ([240e:83:200::34a]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b5fab0caa9sm270352885ad.40.2026.04.26.02.27.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Apr 2026 02:27:39 -0700 (PDT) From: Muchun Song To: Andrew Morton , David Hildenbrand , Muchun Song , Oscar Salvador , Michael Ellerman , Madhavan Srinivasan Cc: Lorenzo Stoakes , "Liam R . Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Nicholas Piggin , Christophe Leroy , aneesh.kumar@linux.ibm.com, joao.m.martins@oracle.com, linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, Muchun Song , stable@vger.kernel.org Subject: [PATCH v7 6/6] mm/mm_init: Fix uninitialized struct pages for ZONE_DEVICE Date: Sun, 26 Apr 2026 17:26:40 +0800 Message-Id: <20260426092640.375967-7-songmuchun@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260426092640.375967-1-songmuchun@bytedance.com> References: <20260426092640.375967-1-songmuchun@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If DAX memory is hotplugged into an unoccupied subsection of an early section, section_activate() reuses the unoptimized boot memmap. However, compound_nr_pages() still assumes that vmemmap optimization is in effect and initializes only the reduced number of struct pages. As a result, the remaining tail struct pages are left uninitialized, which can later lead to unexpected behavior or crashes. Fix this by treating early sections as unoptimized when calculating how many struct pages to initialize. Fixes: 6fd3620b3428 ("mm/page_alloc: reuse tail struct pages for compound d= evmaps") Cc: stable@vger.kernel.org Signed-off-by: Muchun Song Acked-by: David Hildenbrand (Arm) Acked-by: Mike Rapoport (Microsoft) --- mm/mm_init.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/mm/mm_init.c b/mm/mm_init.c index cfc76953e249..bd466a3c10c8 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -1055,10 +1055,17 @@ static void __ref __init_zone_device_page(struct pa= ge *page, unsigned long pfn, * of how the sparse_vmemmap internals handle compound pages in the lack * of an altmap. See vmemmap_populate_compound_pages(). */ -static inline unsigned long compound_nr_pages(struct vmem_altmap *altmap, +static inline unsigned long compound_nr_pages(unsigned long pfn, + struct vmem_altmap *altmap, struct dev_pagemap *pgmap) { - if (!vmemmap_can_optimize(altmap, pgmap)) + /* + * If DAX memory is hot-plugged into an unoccupied subsection + * of an early section, the unoptimized boot memmap is reused. + * See section_activate(). + */ + if (early_section(__pfn_to_section(pfn)) || + !vmemmap_can_optimize(altmap, pgmap)) return pgmap_vmemmap_nr(pgmap); =20 return VMEMMAP_RESERVE_NR * (PAGE_SIZE / sizeof(struct page)); @@ -1128,7 +1135,7 @@ void __ref memmap_init_zone_device(struct zone *zone, continue; =20 memmap_init_compound(page, pfn, zone_idx, nid, pgmap, - compound_nr_pages(altmap, pgmap)); + compound_nr_pages(pfn, altmap, pgmap)); } =20 pageblock_migratetype_init_range(start_pfn, nr_pages, MIGRATE_MOVABLE); --=20 2.20.1