From nobody Wed Jun 17 05:14:11 2026 Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) (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 6DCDA1F5821 for ; Thu, 23 Apr 2026 07:19:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776928769; cv=none; b=HPFDAddeSTATGNLw7V7SvH6wPZjuIQuMcHKLoRuT4/BmNt7vjKd/9o0u91ZaPpEcjzMxubIvjtmTkjO10wJbboA9qPEDcqfIElwVGECeFhl22MYE99zxaHDkNzt8VlKeZzHUOqrHp01CyEpxfPeXMq4j4wK1hNNFnFTAUYxCXgU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776928769; c=relaxed/simple; bh=VcAvFxkCmWuWI49GWMOBujiCIDymHaDPwyWgTANXF5E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AB3UxT/9KH4zrjDfJXmAr7Oge5cTbHaiK6Shgf+zJRDCDXXwGRf7AzoI6KGx33h6UokBGx6A8qxZaCWtpgLWro3m30M8V0M4GzrTix4alviIwAErnTVdQ52iea4gyQHWQuY/dKbuvViGwBelgh9IjuHPfo9wOpB1kaAdOpic0Pg= 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=C2pG9CEK; arc=none smtp.client-ip=209.85.216.43 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="C2pG9CEK" Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-35da01fc0baso3975732a91.2 for ; Thu, 23 Apr 2026 00:19:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1776928767; x=1777533567; 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=aXDuw3SgDm2p1+utjq7Iir9F7HkLAKGsFULdDpxe88Y=; b=C2pG9CEKpTnRMxi6kpWnKFIepvLEf3vrRvtDx5ZBuSv/slbJ32mIq0y/lEKsNN5Yw5 KuOCqW76H3B+i0UU0cRJ46ykYdWRLL/hvfNQBTyEEQt0RgS0tZJCirVKZ81vkTSwr2ud kqUSTDU6x/Wt5bGxVVDxwobNqI3mLki/O7IpouaeXxit1Lm9CANUpiOaiYvtNKip+WpB AuCJIQf7+tGvVYe+JeSp/w+dffA82HLtm4XHVW6DByevq0rrQuXwhBUwVLS8Bu/NWnNE OceUT9cuvWEXgy/GKBILLhUJhWsJM1jZCBoSiL7zJQOx7FzpxZYhjlhY6f++nZjE2evH wA4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776928767; x=1777533567; 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=aXDuw3SgDm2p1+utjq7Iir9F7HkLAKGsFULdDpxe88Y=; b=n+0+ktDmNcwiV3x7g5xNLa6rmyIIOHdwDWFD4bgpvJXK1ZomS7oVSRRqNI2j8DyO/d bo752ztaIbQI4UukQE4oENq0FWI2dWEBkO0/aQAD9yF1wMXSvbEl2Z8lurGYUG/RTo6q rxsuYTDpELrM0hZRaX+6hqe+NuFH1gdXXG9BHzKks12Tl+XuKFzJRm8brBzT97MknCC5 +V3nl4s28x/Y+GDTHljdbTm/3vCEPvfCMo6ccqjDE8qFDL0XYUQA5eLDnXApX+H+AYm9 ZAYd4X1p4oomEAV/vzSvzqMBaPbKT92LhrC381LyITFbyzl433BNVQNE50TPSC8s8uMT xWWw== X-Forwarded-Encrypted: i=1; AFNElJ/WjBQO400FxpWWAoNJ6wbRrnXeSAc9tup+2qzM/W1Amql9HM01pCj/WEj+SvVnOTWuU+YVNFRwX22J0hI=@vger.kernel.org X-Gm-Message-State: AOJu0Yy8dpThFmA1C5UFvUIARChpm9rn6Zzs3OSxszsDwCjvuVQX1ySo hjnlTXClhri4DY38CWHXHrY/AFPeB4+dQmMJdfCT6Vf/63ynrvfSn/E2EgDygv8hUdE= X-Gm-Gg: AeBDietmQVmf8XIX2zmuzmN4NQq/oiREwGMaKaja9F/oERJyC3rzIEHE9+wGMIbDadC Gm1RrftuPGrgGhRpgPhx4Prf9HU1mnGTDj2gGLxi9D7A9Dx/+tEHWyTqiZsL9nkyeG1+Z7AjUBW ELV0Bkwaqh3YSV2q6EEA/NzhkRtkOtCNqbAR8yCrL80ZB+X11DJM6Q8P8+VYt7hypz0bi3+aR7m f4YPsk30HtXbVVxlsHlJGdnXtNXLznFxkVz5Or0lqUjKzZBD313MRcoI67jEUrLQqEQ+G57z60m OuNIsQP8Dmsq916wsgK4KzreyM7jS5z9eQMD7q6drIOrd88tWukpWc01PaF+85TjdFmv23b7oz5 4uP5l+NqueKa+lV7QBeDC/n7AJ0PDgR8rAcpj8HltnNXxA9vRN13ozDHCFs6LmSyPrQ3ig+XS44 0laGkf6CsH9SRuFhYhroc6pa2zZXdtrrVbkUAxXTE4fMGuc8DfaFP4pg== X-Received: by 2002:a17:90b:2fc4:b0:35f:b6d3:da7d with SMTP id 98e67ed59e1d1-361404635e6mr24991060a91.17.1776928766629; Thu, 23 Apr 2026 00:19:26 -0700 (PDT) Received: from n232-176-004.byted.org ([36.110.163.97]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3613fbd970fsm7092372a91.14.2026.04.23.00.19.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Apr 2026 00:19:26 -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 v5 v5 1/6] mm/sparse-vmemmap: Fix vmemmap accounting underflow Date: Thu, 23 Apr 2026 15:19:06 +0800 Message-Id: <20260423071911.1962859-2-songmuchun@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260423071911.1962859-1-songmuchun@bytedance.com> References: <20260423071911.1962859-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") 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 Wed Jun 17 05:14:11 2026 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) (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 85B0A1D5151 for ; Thu, 23 Apr 2026 07:19:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776928773; cv=none; b=vGIQELx7mlXHN7vM1CLlC6V5CaPftAGzQShEWrVGCSG1zFiaiOiHare37n+bhdB2xgukkS/JT2VSGWZVdS2PY/aZUhd/3NmTxduDOOHaKyrf5CRBvLIDdSaBuI67IlAUh5EyQ9j0decSdllE9J+rKf1ssjH2Suh0z/5hN3QnkZM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776928773; c=relaxed/simple; bh=nWYa3Y+lmS5vFke+YBG5gJWmrhX0dejtPlMuGx+Jzo4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LrTcZxSUlv6sYVHWdrR8bCuaBtgv2oTQA7jVYy2SKqLwUk9A8htaoQV0Zo2PBSwwOXqgZ2yexk73jwIbGhbXZKXzVlQb0QFgs+fQ8+wVQJ6ihwPDEhfSeB6oyHF44D5x8OYWFin3HIret8v+7AvwjZ/odol02Oi8jAhKhrAxugw= 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=DXm9qxmj; arc=none smtp.client-ip=209.85.216.47 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="DXm9qxmj" Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-358e3cc5e7eso3752328a91.0 for ; Thu, 23 Apr 2026 00:19:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1776928772; x=1777533572; 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=h2yTHOksAu1ksVjTp1X1iUbz+KUOcAtP7+OrJQBUO/Q=; b=DXm9qxmjFbziAIOt6bplFqfPD7E+La6hbzX/Vj0mu7Tl4KgY8wolyAoDwy3vP46l/g RSEKkRm2K3f68r8VMvpQtS/WbDVtKdn6/c6JJEWgvXqroalIXzvWDq52GrpMtkr/Zq6w w2jXlfkec35f+YHYLtZTcR+YVrKA8bsTv+CrOQXZqhvp0pGCRbPtgX0jlYF9Os5qUDnP Ou/kVpFTwDWayjAS74XHzSaE0PS4s+tLON8VMm6Cq44ZMMMzJjmjJqnQRYnorbcJR4zq z/0yO8JKZemDvfP3LPwBhUcnQpyGvhjiLAe7BybTRvRkC4zrOv6C2BjloTBfc+HNTTie RRhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776928772; x=1777533572; 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=h2yTHOksAu1ksVjTp1X1iUbz+KUOcAtP7+OrJQBUO/Q=; b=Q5nAe6zScQbMxvh5CzAW4wCwmVhfXSyQVRvSD2GpOyR9DZufJg8OiBBCPtI8suFCSg ZPW4yYY3u/qeN/ZFX/ZQPd1mjD/nPTBYqAlTL/eebAj6rG6l7qKXkThL1cditJmSVb5Y ETKwExGSMrZDGEz/Pz9eE7m2bzq3MYKI4hH5vgoc84eWxaRZ7N9pLDAv+80Pnn1cUBcT aC/Xwm5l3l5kHyNG1sffiqiTyL5xbd5ofSZM2YaaBBvBZqo1GwKDrjgNUS8D2dMmuIgr oJkLig/bkaRH3nU9qB+OO+9B/6cGKE+SzxDEWe6RiwK+JIUBWy5uVmzzSGcbMcvdq2t4 DN+Q== X-Forwarded-Encrypted: i=1; AFNElJ/rJG2bJTW5hyT1T61QAqKWhyx1yfxH39sq50EqX+7ZyvYYiPnWwKDCYpSA5UMKXmQ4ZAVP51yvUa+D9zk=@vger.kernel.org X-Gm-Message-State: AOJu0YyVIz0e+/KzbJE8Hjb40iwwZVHBXqc0btUPyulZQCNfT52iitt/ qQFY4jy+rc2fr/V2BjP2DV05SfmRD42orVRvTh5s6Ee8UShhuECMDY86PM18lKZdMlw= X-Gm-Gg: AeBDievHd3x5rZEzq3/BFWEIGHc9QEXMUrqSP34H5sEdf5I5ehy9aTQ2csQLtyBrXq9 xXYLC1AhuyJFq2gKnriz89pIMzWwO3kPuQsKzLtViUBMIFMtTikHk6CCV6Hb40jQLbzqiyoJMyo vNEY8AL8APWMNWjou6qvPCU4RCSlgumtf3mchZh5JnNf1ncea2hG8jVyljmFGNF5AyDlpMpRyYm v4wHwQWbONNyqVlZJtfSjesk1n/nPEhUIR2HmPnBqlVHOMbsI6nv15/BLc/MQ814WMgsUNwlrjL mlG52Q5mGwKowqa0Uy92zb2kX4NFY5PUcNxdN6sDKfq08654iM+SvSQCVgooNd/oc6Gw3u/wx6Y 1O3u3gi7OK5/msF8qlyARSJpLakUvAdrUma5i96GjzPflzrXepSaQeMiudH43eo5vfV/MK7pT+g jRs7rgIq4AGnATQWq1JiOnCy/qh3bCOizEqyvxYebh6mYnzU7PKYsL0Q== X-Received: by 2002:a17:90b:3d01:b0:35a:cf:64a6 with SMTP id 98e67ed59e1d1-361404af0a4mr28368736a91.23.1776928771838; Thu, 23 Apr 2026 00:19:31 -0700 (PDT) Received: from n232-176-004.byted.org ([36.110.163.97]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3613fbd970fsm7092372a91.14.2026.04.23.00.19.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Apr 2026 00:19:31 -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 v5 v5 2/6] mm/memory_hotplug: Fix incorrect altmap passing in error path Date: Thu, 23 Apr 2026 15:19:07 +0800 Message-Id: <20260423071911.1962859-3-songmuchun@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260423071911.1962859-1-songmuchun@bytedance.com> References: <20260423071911.1962859-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") 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 Wed Jun 17 05:14:11 2026 Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) (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 0CD151E0B9C for ; Thu, 23 Apr 2026 07:19:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776928779; cv=none; b=lP0jKcVhpKUkhV9I40jUvD4JDR7MPCM1OKG+8tG6FwHOcJZhRut+proeb4WlVFLUkPEoigTMhgB47nWasoBIaS11mwAlZ8EWmhafszZ1IgUBh+0b8rj5vkJvf9YLjYNa+y7BrFr1H9dQGcUXXMXrrTe/njFYKuUcV8P90dagruQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776928779; c=relaxed/simple; bh=sDA9tg5KnuPIw4R0NQAaAviIVzNYRfvOuY4bGkMoGbk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZLKnQAvK3A5fKQyZ/DHweTDSYD62z8dgt13NLE204Yz+YDgb0aXhr/A3JGZM8AkrqkMs4eZJYckAHN5KGN/v1MteZUuWzntnQNNLqrhYu7TrraU4OVKXl1fEJ0wQob1TkBaiebzblfGaTgY89KGkosbUWHupy7TFxR/TsfctyTw= 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=agYBE5uu; arc=none smtp.client-ip=209.85.216.43 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="agYBE5uu" Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-356337f058aso4054363a91.2 for ; Thu, 23 Apr 2026 00:19:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1776928777; x=1777533577; 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=4tVvEy0n8Q8boUwkyPz7NIWKrGY6MN5PZ12u/TRimec=; b=agYBE5uuHZ5mktg+YIMnT2VeuP0E9fSVlO1A6k8u0+mETiQI98O1Bm0mww5mmCX7eW GMVDTev6ZHpU2VwhRRImugiRUpFydZ6dUkoDoCbicxzLf7Ek2q43HS4KtHUrbs8k+zTD ascJaccoCdQHD0cMI9A6OdkVpMzTz9LckY81wMcK0oYI1UIo1Y0arYi0ueU8t1f/7xmy Gx2ys69+Tr0/S4QaHxH+NQIc88RpJEwZ9/I8mOzFlV5AC+3X3iGBblW2iI/DJseu5COC BBmTdkB/Y3EVkvitdeo/VECGUVUO2P5Us2ajZ/gALIWsg2Rnjwfm7w950H9/Zys75+5u 3VZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776928777; x=1777533577; 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=4tVvEy0n8Q8boUwkyPz7NIWKrGY6MN5PZ12u/TRimec=; b=YYGPlKk7PgdJpXuywvM4GpSfF/gCJzNzNFVOr1+jjtWZuc2EYI2d63l1Cv4lJiCURW 6i/0Se2cSGMa+j52BKqxSUR0h+4Glh9ga03YpypSTtFnCIXkFjcmyZi9pNJ4ojaR+Td4 cVbbM86xw7EQyGADrikyv+AgHBqjlcVud+rgBcJqD8rJew/kDW1Xoij3f2Fd95ffrSgL xFbPE494h7FKNQilOF/T9bvhKVPrrLmxa3xNlEK46MgbvXO4Dim9kSjxs+3FgrOCIigC N/PIOvUxUuWSf6/PLET8Wh4wFwAkhj6PrS5rdBwjXAl0ep+prspy6gQN2V1bQLNu6949 6LQA== X-Forwarded-Encrypted: i=1; AFNElJ/dwh0d0HdvkVh3mFIfifz2S4Yj+CRIpbRtPt/Y3kebQSW/7CMRe6lJUWa7BhIMG7/qq6kYisF2hIAYQaI=@vger.kernel.org X-Gm-Message-State: AOJu0YzfhZnFPfLjq32ldnr33IRFOeRyzLHuqnqNt6qMDxhglT2mB3ih FCidcX6Gx1sslr/72XSgvN6bHU0IOJMQlRwsB8SjVxMZeRDWVWmfZtO0gbKmzKf2uiR7w97wN8P p8ojNGmA= X-Gm-Gg: AeBDiesKaKvB/xrluL7qrWT/9pbUndpFSfqZwgKsIm8kYQDYIeC5uMt1DrLXYG5T/Q7 WdQ8ehd+DpGgeq8KtJSuOlPJgiYznEUhTULi9zIbnT3MBzgsRUSeBwqrc0ZfacytvzQNx8EPx7I djHk38IF2qWO3Yq1CTjVng/lY2bbREIAzM9ntFIsx9qi5mJO70S0VNkaYpei0HbvTG51TRdktPH pLdAtEceCZSAWAqA/N4sMO/cbNV/W5ja6ef2cWZ/CwUyNbCVErAlluOeeCBgbfi1e7wunUiNj54 SdZJLgnOO+Xt6D4UvxiCgmfvm+QJ7XsIBVxKjx5T9/XsncT1YBDDB2lNK2vlyy+zWxVTc+oHr6R vyttoc9y7WQfyzu/+3KPhl6cuL/81MOjaZioczprJUFLI8aAv3JYZeYN8YUJAdRycy/qEn5w96G ksARWbT8UnKZ82YsvWYfzCOpyxfwxH/KD1RVsUAJ2XbYoO29gVtAVTYniR8djXUKx4 X-Received: by 2002:a17:90b:270a:b0:35d:a8d9:3b4 with SMTP id 98e67ed59e1d1-361403c35b5mr25387912a91.4.1776928777199; Thu, 23 Apr 2026 00:19:37 -0700 (PDT) Received: from n232-176-004.byted.org ([36.110.163.97]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3613fbd970fsm7092372a91.14.2026.04.23.00.19.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Apr 2026 00:19:36 -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 v5 v5 3/6] mm/sparse-vmemmap: Pass @pgmap argument to memory deactivation paths Date: Thu, 23 Apr 2026 15:19:08 +0800 Message-Id: <20260423071911.1962859-4-songmuchun@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260423071911.1962859-1-songmuchun@bytedance.com> References: <20260423071911.1962859-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 00f3822b6e47..c9c57f08fa2c 100644 --- a/arch/loongarch/mm/init.c +++ b/arch/loongarch/mm/init.c @@ -86,7 +86,8 @@ int arch_add_memory(int nid, u64 start, u64 size, struct = 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; @@ -95,7 +96,7 @@ void arch_remove_memory(u64 start, u64 size, struct vmem_= 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 Wed Jun 17 05:14:11 2026 Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) (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 DA4B3145A05 for ; Thu, 23 Apr 2026 07:19:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776928784; cv=none; b=DI40/g+upU8M3DLsbzFPHP8Wm8/JD7AfbgD2u3fbRwzZorvjitxnw8OBRrkL6d+ylUrqPAU4laY0l80NO3pH4wFD5N+Z8VbZaqMn+V56EjeJPludDm+wu6PYwi4mSqEdehrUv5dI6fqoaFUYzcocHcYfk+KYZivLHaOvfLRvgis= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776928784; c=relaxed/simple; bh=Ze9LMjhWl4y+WdkJ0sInuSPz5CKqbKVq9DGD1GpLr94=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jAbEJUYCXeASk68wHKebQ2hR++y31pgMtUtXPqFtsKuf47OGJ4keoAlv1hyRVfMQ5RbzvoArcDhtHlTUJwJS46LmiwarQHh+WL4qlRLdQbUWP7irReyKyG9Y1tXI6e/m+zIammzL9vccVQ3nlYEZKGklIdhB94CMI1DZ0Q8kNQc= 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=dY1rc5mu; arc=none smtp.client-ip=209.85.216.48 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="dY1rc5mu" Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-35691a231a7so4333105a91.3 for ; Thu, 23 Apr 2026 00:19:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1776928782; x=1777533582; 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=hLTLnoewwRhtugCkWIDfQqYKu4KougM+msAio3RkTvg=; b=dY1rc5muSmCrJe7+lnyOhbJYAB58FQnNHO9stsWz0SgIli10RRkTaBOWvSzi7GwNhI kILg0poJ7w8SWhU7Xgk032WVKhW/W+evCKAYri7VNdmhT5+BRqvnlrxVgJmcrKBm5fe4 BFH6ZseGRJ5+OQGeCPPE08pZffJEGUrNN7WcJyUQsZLjMHKbj1+tRn+Jb2UHu6UV0UB4 KelF7jtZjhBUUvjqSSdCfKHWeYOlRJUCM/wI42eXGHmwvCmDasO358o9GwUXycmd9mSs 4td2GaPPSppUNh+IBpJx4dMYzhRT4Z2600KkpZfrUCwBHHD/JGBv4JfQo+Qz5uUJZJJw iViw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776928782; x=1777533582; 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=hLTLnoewwRhtugCkWIDfQqYKu4KougM+msAio3RkTvg=; b=hDCE0u76j4wjLEcPLDQpGk9LU0zB/Nuxk+X5KDTLn+Pu6lw0ZL0wjA7nSl5MWfxOZj FvAnP3Jfw/UYHxoU67Lg2HJdDWgC5MH+FotQ/y10ZNpFdBZCpO3Ok6+R3mS3xqlsIdjA SQ5PaONkqSaHq31T/qUYfEdjap64RPHtfL7p1zbFxVIhlJmm1LkGKPNGRK+ZF7YZ9up8 pwZoBw4t6K/2hn5JMFvXzCCnvuwPkzwuRbULN/T7mcDWI9IYgwl7ymTIA06SNBay0f7g lRSEc7sE1D2tJgnRpR/KcZolFy2BgF4Yt79r2aOll3YOAkj0ngcHnJbMT03qA4rVzgy1 6P8w== X-Forwarded-Encrypted: i=1; AFNElJ941TQoZEdqxSQQOlBn1M9emzrrRXg6Vg4pRyXRF8rDvIno5uVW1du8t9UgRJi0I8tPVbm4tE0CQsqySdw=@vger.kernel.org X-Gm-Message-State: AOJu0YwgSC+3FkROxZOqVVYdy5EJO+q8OJA5VSevi8UF/d93fKWrHFXt L39MbMIKrzhyc6qV3ljDM9oB/Lgajua/4d4eXnSjtk0M+teIfIxqisXbUhLdmRm7iGs= X-Gm-Gg: AeBDievDCHtViiNhUMCSScaEVtHY/jUlVTSQLwaeajSUWvyG/O+A+3fa0GcY5xbGz3g 2cdtjppaKcoCskhOxZeYjrkpZMr821FmelhqEaAkOtFgIUYJ9axT5H/iHOyY4AkKuQogWqaPuu8 IZk1efdS1oDxYFqheumQBy/j5xWd1p4CYNLI+Bl+75DqsHVDvkQOKludjPEhMqVBn4+7BP8iqBp LvwjrMRXr/dIryf3oyhXPQpYv52nusRCt+QXo8rIfKeep14NW0wn0rN9OSu6QRmbAHd9sgUFBT7 5o6XI4IFQaeHrcSSyD07wtpW4Yvassxw8Ji0SNz3rXa+Xz82k/9ZvdpNJMClT3jaKneosoVRAqP Y4mOdeH0S1kIVhD0WjXX1n1GbkaKCorrl4dHIHS8T+E0rmg88bbnxFNz5Nt+/ALLR+vj0ReMr36 NFDmfiqZQbFAKKAEql7PCDJ1F4ivFr709d278kcCm7/LBm1y5PHIQkcg== X-Received: by 2002:a17:90b:1d8a:b0:35f:b4c1:91f6 with SMTP id 98e67ed59e1d1-361403f2807mr27201547a91.10.1776928782107; Thu, 23 Apr 2026 00:19:42 -0700 (PDT) Received: from n232-176-004.byted.org ([36.110.163.97]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3613fbd970fsm7092372a91.14.2026.04.23.00.19.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Apr 2026 00:19:41 -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 v5 v5 4/6] mm/sparse-vmemmap: Fix DAX vmemmap accounting with optimization Date: Thu, 23 Apr 2026 15:19:09 +0800 Message-Id: <20260423071911.1962859-5-songmuchun@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260423071911.1962859-1-songmuchun@bytedance.com> References: <20260423071911.1962859-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_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") Signed-off-by: Muchun Song Acked-by: Mike Rapoport (Microsoft) Acked-by: Oscar Salvador --- mm/sparse-vmemmap.c | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c index 3340f6d30b01..979d71158c9b 100644 --- a/mm/sparse-vmemmap.c +++ b/mm/sparse-vmemmap.c @@ -652,6 +652,28 @@ 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_compound)); + VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION)); + VM_WARN_ON_ONCE(pfn_to_section_nr(pfn) !=3D pfn_to_section_nr(pfn + nr_pa= ges - 1)); + + if (!vmemmap_can_optimize(altmap, pgmap)) + return DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE); + + if (order < PFN_SECTION_SHIFT) + return VMEMMAP_RESERVE_NR * nr_pages / pages_per_compound; + + 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 +681,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 +692,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 +700,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 Wed Jun 17 05:14:11 2026 Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) (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 13714238C0A for ; Thu, 23 Apr 2026 07:19:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776928789; cv=none; b=AutZQi8ylwaEs3v+IwqNI1Nf56MmB5qpWjKrkxl1YuMKnzbHyw5XDY41xwnMBGH0c0Jyjnk8YskfA0AGqazJw9VSSt6JNmu766NVoHGPc0vf9FLIYKgqQ4duYJzrMHQ3vpGeA7db27F5WTAT7Nc9QTQvF+yLQHWC3ADjdaojre8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776928789; c=relaxed/simple; bh=uLTlLDwqPz72m25OUXhsR1fgWhGnQ9gcvw1RspKAq3w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EhWLpyhyLUIGzOphyL40LDbzALIJRje7fz3VjQkxvlbT964ZOZr1LR+cQef4/85yOAWNqZS0r9JnaXXnSHgAucwipxvpfyTkxSeaq00A+rKCAWYXp0a6VWZjKv65x0ePuCQgiX+XqanZ1TJjc5N2R3FLwPOTSUNGRJeea3ijpKs= 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=FQX1vdo+; arc=none smtp.client-ip=209.85.216.45 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="FQX1vdo+" Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-3585ec417f6so3439051a91.1 for ; Thu, 23 Apr 2026 00:19:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1776928787; x=1777533587; 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=0E6l7+8OUQ4Tga+WuEBz2tFLFSj0y7aaAPGsd1rn1yk=; b=FQX1vdo+CY4pllXZIoG5C61jymw456tzLqXALFUnJxyWduIJVx7egdlGZ+/+3iAztn 4xPgYU4dFmbXmEA+tzgKTKxxI8skD2t8VJAw/V0AfW/rYMEklXsw5fwj9+BN489Ee/Ec UajRtPE+v8JW0zY36wXxQRVDq/F8ENkma2rnwmjiLJpKt3P0kRZ03mtJMeE5Cib3MTKc pPmpEaILJvM/p2zsxAxAOSlrsMfzOGeyBJQKqgwO3jVyZlhlWG/yZ8kbmzR3uUzz79zN g4SZSpiPYbCmG+dC8ERcQejOrRsfgowqKGyqxKcQlJT494s1V4w+aXr+Gyhr9M10P4t8 ljtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776928787; x=1777533587; 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=0E6l7+8OUQ4Tga+WuEBz2tFLFSj0y7aaAPGsd1rn1yk=; b=Heom5oJU+LmK3cfosKk2uveTcrDjceSwQe7w/VQta9jTdnu+hxRG5hi9yhUPE/yGhs pjYWxhcgcYkRS6o8iYbCib2LN3bPtigZ2d3gv8746qkPs9wm3k9as5yxw9zSVRiuzDWT N9HUH2FTMm84PULp80+4K/wqldya0gwUo6FyxuOx4K8mvTi3tQFNK4L8Vuqn2sPB8/5H nsRU9A4TAzBokwRRSIjIDPiCLlVMgeYxqGohBsj9HCl21FNJo3ia1BKIMvbPhszvfile QAVB2gc1IGAI0/SItR0Vz5SOfFt6pfwlorbDo1aB6VGMvtlwv6BmxONcF2lw+yna+cgD GtVw== X-Forwarded-Encrypted: i=1; AFNElJ9Hdj9BW8ffwb0hjcxg6LgJs2asCm+1lfVBDvD5jszatzygOcIMCQRlIQom6a4I8tr/SVhLHstxVd/BSlg=@vger.kernel.org X-Gm-Message-State: AOJu0YxVYc8F5xy4vO4kmaSf3/rwgfg/31JvJsasF0ZsL6m/jdfheavx WzvPcedcnOp0wiBh82a7dsq6Ps+KkBtCnD3Kg43L3nQut2rk1is3GneNYDdzO/iX/Ao= X-Gm-Gg: AeBDievosbCPITAApNkJzjEyGdcHQiK/W0l7vJU1GgN/Uj7x4NC3kVxekoWW5we+ost 8KC+E8E8q23SaVU6QN9qmUEOktp14TOdd6fh96ZXkZ5BaPyS6wpj1K3UFPALS5nsdJ7zubiUQd8 jKmHLjTW50zMyq0x+GxUheL2DMS/lZuw24hVPX3AUgdfEZaNk06TO0ycEjevlMH2Mk+uksrXUL/ iqdTtaB5KeSkv5tECqYhYOTvoTSVYDPhZl8lSs7e0eGeFz0/lwEqUkz3rdIkSdWj/aN7NBEAMbP OWGWnI/4Qp1WZ8HFf7tadapNQ4dE4lXtlLM/uVQgp1OQZBPd4TBE8pqymoZTlhqDJ1XpB0Qh4xG RkovhXqOafjdcaoQjjHGgPqTXwSqxP4j3RgrKgH7RDh3n6DgLd7ydGXumNdkLAGj3fMsTMAXn1+ 7GZ59tAMTy1DA+t2PVIhlA3jQedIj1nnm1RDasOXotbWsbcynY8sx8cw== X-Received: by 2002:a17:90b:3ec5:b0:35f:b4c1:91ea with SMTP id 98e67ed59e1d1-361402ee818mr20270410a91.13.1776928787371; Thu, 23 Apr 2026 00:19:47 -0700 (PDT) Received: from n232-176-004.byted.org ([36.110.163.97]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3613fbd970fsm7092372a91.14.2026.04.23.00.19.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Apr 2026 00:19:46 -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 v5 v5 5/6] mm/mm_init: Fix pageblock migratetype for ZONE_DEVICE compound pages Date: Thu, 23 Apr 2026 15:19:10 +0800 Message-Id: <20260423071911.1962859-6-songmuchun@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260423071911.1962859-1-songmuchun@bytedance.com> References: <20260423071911.1962859-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") 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 Wed Jun 17 05:14:11 2026 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) (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 C77C0220F2A for ; Thu, 23 Apr 2026 07:19:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776928797; cv=none; b=czDVRDjpthg+QqkGM6vBGTl7MATPQiF3GHGBHrUKn4pIDM7gsOkBlwz+EIvtsDqiRPvdREHUbfnYGcjmjSvOKCJpQWFLLTPSe70heXipbgC5ltZSs7/CRZOnPUgY53GrVS41EmPiBhpSIfG6khrgAW/Kt0JjT2L5mRn2nYoyiF8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776928797; c=relaxed/simple; bh=8ymok11dAFcdJljdsKAUDTFjuOvzOJTN9Pfa9YRA0/k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SI908YmqhgyO5ocs0+ngb+KfMSb5gYLSENeUpePlYitUvnVenwpsudgA15v1naXJ87xUuPJZ79M2YKQRvZJqTv3Q689n31EWDNt//YsJ+p+THmoZ+uUM16InRhyBykGEwv9MuTLEWNhA8tVeNlwe7jWdNhZBE5djCqynTeaXnoo= 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=Vbt0IeeJ; arc=none smtp.client-ip=209.85.216.46 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="Vbt0IeeJ" Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-3590042fa8eso5015607a91.1 for ; Thu, 23 Apr 2026 00:19:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1776928792; x=1777533592; 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=+V18KNwF7QBH5+p7vjE1eHKkHO/d1EPWETOYBsNfjQo=; b=Vbt0IeeJWJP/2dMozi5R6JIxQ1B96Ny+2xmWZRQz74+WG09NaWYHO4j7pIP6fdJ8xC emrbWXoHtrlqpIJZcqqL/Zzl3aiXFSrwT3CeX5Nqk9s1dHRkHWZNR4XUdkDSVpzYQpDe uDJjxVAdaMy53kbt3fYYrfBSbdGlc6Ysu0/sFiUul+j6rbnKTIeI5BTA1bkUBzg9ChmU 4b/615e+AApM2q9waHdQ4ev834TgvVUq4v9GeyrPXk2p2iLwo6t7NbDi1bEa7LZ3EkwV WXnQod+mpK4OeXD/eo+Wy7PK3Z4XgFFMWA3K0Wmo+eqYUiNBsndFVWkWuiwZMGnB1ssr 8Fow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776928792; x=1777533592; 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=+V18KNwF7QBH5+p7vjE1eHKkHO/d1EPWETOYBsNfjQo=; b=VEZ1+HyhJHeTCnbJ6feKykm6ZmpHRBXIYPp5UOYXMDpF0dWNjrfu5EDYnlgNwugj9I p0quezQZv4bjiBQL3tZUwD0MDMlHHqUKtRYMdU9ganzEeVYNRHkMKLgxFKj1sxH+e3XR IfvM2LXUW/G/uC6veOsM2MXVjAZbhLaTTS0hQ1y3kERq1rFNX/xQ4VfsrBlp/8fNN0nb bgyjij5HdfE0j5c7fc9EAuklyQ6Mmyd0Mo1q0kfLQic4s6RrhniTOLVcjdkY1sw6Ql+e 1L5DT89T5sRjyBYPpeSXLrUfVF0wZcs8SfGRkkLqij4rb6hGaN6MYGTZ89fd3w6Um8VE lWdA== X-Forwarded-Encrypted: i=1; AFNElJ8MXw87O6YZs41VjtiV49ZN0RmYsLdHiu8Y940nJ4iATUab1RrhA1b/i7lJLad5VndZwelroHy90sFJXgM=@vger.kernel.org X-Gm-Message-State: AOJu0Yy352xMQndAd5M9th+r5jzZggCC0E1qfxY2YJEDc+9iF757ZIB+ Qdfy2tQScLr75M1CFeD+VfAg6u1P8SXaDL+n9GDOPaHzJHjkE/sqXtLCrAwob0XQljk= X-Gm-Gg: AeBDiesFlr/3735qbvt6mlx2UaFTsu5MCReK5S03JpTVlz/ITCGQkVNgzPJea+6jHT6 tmFE7wv/ukrFR4LJRk2iR0WGYGvANHTaMQfSKQqGP0gv8XZWVWgRIpx39ZvTjqmNQyGaP1+JAwe pGw0ND1x4L0/u9TX5KbBHBGsr5bqU4NRKt0QXizFyP0Hv6A8INnj4e+1x5TFn8X20j5rXDJYTEy HGggp4sh0i+U7F/CaBRWbt2wLD8JJWyRSniJ5EBrl2/34xuy3gG/Vtg4Z+0Yc2M0NNJ8zaqmkya RRthjx36/RVPJVQTJXRXJIjZG8XzH6KKPBiTmNVvKV4y3dEasmYYVg/U+i0V7osvXy/yimt8iU8 ZKE+6VCLlKt3RjVWPxdU16QCBWD0mpaMJYb0X4ycZPqxklAHh1eOPgWNeuIfele0oct4j0vzGry Ya4k6YQTfQFQIeJz6RDqwX/6NZhkCR0KhoOHgEqB558bmwAuY+iSgCQQ== X-Received: by 2002:a17:90a:d40d:b0:35b:e5ce:73bb with SMTP id 98e67ed59e1d1-361403d61d5mr25963174a91.1.1776928792148; Thu, 23 Apr 2026 00:19:52 -0700 (PDT) Received: from n232-176-004.byted.org ([36.110.163.97]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3613fbd970fsm7092372a91.14.2026.04.23.00.19.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Apr 2026 00:19:51 -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 v5 v5 6/6] mm/mm_init: Fix uninitialized struct pages for ZONE_DEVICE Date: Thu, 23 Apr 2026 15:19:11 +0800 Message-Id: <20260423071911.1962859-7-songmuchun@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260423071911.1962859-1-songmuchun@bytedance.com> References: <20260423071911.1962859-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") Signed-off-by: Muchun Song Acked-by: David Hildenbrand (Arm) --- 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