From nobody Tue Jun 16 02:37:07 2026 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (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 E01473859C9 for ; Tue, 28 Apr 2026 08:19:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777364351; cv=none; b=qhUBScN5584qRwUKk1eFWw000pnTARdgdn0Ir9jUQi9NCCQ2cFxjlof+DZAtyPhtVaMYaYXvWhCe5b83acxuDqwg/4Q/e3qh3t4UmLezigWDdJYzngJ34H7S2fJ71e1Vgnnj9qECpvVmVosatw1ju/Tj8Q9+P6FPOTtEtfg4w68= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777364351; c=relaxed/simple; bh=0PLb5zFObm442FFPCMDt2iC8dud5KQqM0ijr0fGCJws=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=blDaTbrW+C704CSRHN7bREME7wjeXhmK6/ileASU7jK/gij/xNfvj3/OQ2t4WyjRfmVwqPRqkaak5noa9DX/PRGWW1HeyXI9acZcdav7J3ctHxE6pnViO2QmjxN99JrUPwFJrFWXMzebXo95/4IvUd2MEwgS8RfM37TuGxlkkxc= 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=DEDbZV9P; arc=none smtp.client-ip=209.85.216.52 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="DEDbZV9P" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-35da8d037a5so5265756a91.0 for ; Tue, 28 Apr 2026 01:19:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1777364349; x=1777969149; 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=DEDbZV9PInNIYCP2ZcJyJV+p5hB/hr7iMDPIDLMVodgpBrCgi+/i5MXm/QOyLnkhX9 Xz9Db3TRVzgZOqrch35P6WZehwxrUSVO/jxP3kAmx+zm3aHeFkK/UqXgjIuRDP+CaCFf jA3R7HVaPFi9+LxzA0Kv9+GilOt7A7gh6jwv8X4qfhrG9Lq6mqELTuuOny5lvrp/hBNX pXEl+47rYOXFMhc2WYr7jyrq0IZPBf3nSWM8BOoHkAAiwTiKymDbwS6KHcsk6SJ22fHY 1WsQnuoE5Kwm8ELMImE5RaNWAN+stfHWKodCFgb1M2rKmqDLQbSMGD9hcJECDNrFB/Dw SD5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777364349; x=1777969149; 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=oJa8tKbbFyYFRgJhW1WcTDN3TMQt607k7+bWQ9LqS4e5pzTRXMa/rp6VpkhHCJBC2v VGDVbBNeMk5tn6aalmLPqqyqapjSll/AenD5nHYaUMG7Ahre7wdoSQH3LOG6CDEzj78C Y8kUH2cVN0OxYEz7uH9JJG+I38P3G0dfhl/bI6cClAj4elKxfQ/zfAKg8BHDrYL0d4+F YuA215FfOcZg8mI/WJ9uJcyVIvvLTEqETuu2VmKjPI8DcMkeGYg32SkViYNJMXRTps2/ 6bLMD4lmIdFiGJauwnvGajF/NCzO0xJnijF3cJxl+zI9BJtecO+Ln140ubYCybpUPBP0 LhFA== X-Forwarded-Encrypted: i=1; AFNElJ8ebzw5/hH82EVePH+TI7p2G1Tibs5OQ6c0REn3vb/wxHHRtdE9LC+Fn59s0AL0WzunD9Fws1EYbCQ+M7I=@vger.kernel.org X-Gm-Message-State: AOJu0YzQ3WIFCfnZKCz2ELVrDWW8uvAcq4fjz4gSMIPdu+ft/uMzJSwR QIOBnvaGDqIT9A+2qiS/GqTN54e+qrc6UXh0bTubPiVdXqDhkMkufmXXtn6XHfwEWhM= X-Gm-Gg: AeBDieuw2BGHxN7MJJ8dpkQcOc4I9wko+mGEqGugwc1yfAG0iPsIirufRXi8UbQ5Qhd oYs/HpkVOCcgYs7cWmIBB+Y7aJWBbDjAcutrm02kBsjz0L+HfSGi/By6rQ32W3AJAtU2XLdYmzp NDFXhxtCVdC0Bwwxpt+NluDzhU2sDlacI0YFX/dzq0nSySsU3cuIAiB2HIukKBm3nWm1IPLwk6Z 87l//bZOItknNFNYZfkobQROLP3HOwosvKyPVBdR66UI1DQ3rGDIz887n0MojqWEAjMogIHeHXh cUs1cnr/K6rwuqu2Gdpi5+QmgcuxqNTJ8zrUjDJ40e0j2aG+KIJ5Opk6p5MeDbH108x0/2TbGj6 Sf0yxsyQFLEbPEpsW67TR9Un95bOBwpDNJl5JRHaJ8GYuJjNjlHheva73bytnC4L4Y5C9X6uuHI 2nIxVgLAE8GYAfZBwJW/2nIHToOAduCqaHIyN+kHCc6NznsmDoJbTBdXY= X-Received: by 2002:a17:90a:d64e:b0:35d:8fdb:4f26 with SMTP id 98e67ed59e1d1-36491f89e50mr2246840a91.1.1777364349228; Tue, 28 Apr 2026 01:19:09 -0700 (PDT) Received: from n232-176-004.byted.org ([36.110.163.101]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3649003650esm2181356a91.8.2026.04.28.01.19.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Apr 2026 01:19:08 -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 v8 1/6] mm/sparse-vmemmap: Fix vmemmap accounting underflow Date: Tue, 28 Apr 2026 16:18:50 +0800 Message-Id: <20260428081855.1249045-2-songmuchun@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260428081855.1249045-1-songmuchun@bytedance.com> References: <20260428081855.1249045-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) Acked-by: Liam R. Howlett --- 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 Tue Jun 16 02:37:07 2026 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.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 CE51D3D3481 for ; Tue, 28 Apr 2026 08:19:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777364356; cv=none; b=D4aCIipJaL+JGDQWEpJppyNX2f+3gz/nhRZUcOBB6qIH6Pn/nC90/fjUCu/4y0jMRdEFWWlfJiei32zRSahmq3ka6srJe3RiHWv4m5m2zUF2FVjXOvAyu+xAHKWHwwJVasgZK6rT5hKtQNzMD76rOx+H2oSYGUdiQjCPxnWlTGk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777364356; c=relaxed/simple; bh=T4yKhyHGB1poxPphLLlyVRDB+bCKixNLLBGYdEPxJas=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RneErVeLDTdPiFVDrjdnAVM/bCN2cdIM0/QPLjhoiUSBL+WzshZ4c05AfFXDNoqwore1/wynmsNCe0rMVgGijpOkXJx2ACu/J2QdR+I4JgofKswH69fYAmpmG0yXGoT3mAlXqrmTQENQmbaHnGnltBEJpUBB4ph4YX6uDhPRupQ= 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=bnbZl8YO; arc=none smtp.client-ip=209.85.215.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="bnbZl8YO" Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-c76c60c7502so4398030a12.0 for ; Tue, 28 Apr 2026 01:19:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1777364354; x=1777969154; 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=HM345nkBQh5gaxmYsV6Qr69X0RaLtEwieY1kCbsL1/U=; b=bnbZl8YOqfGj4yJfTP1VKSpo3Bb1/YLehDdTWm89VE8SAisnzo2OAtnVpDOsWfCUsa R0XUYMjnPSPcNUfrHT1iE4N/TLlYoSoBArRBTgMCM8a3EU4xlk2wizncVp02N9QbYUGG xIpqxNPrwodmjI8CxSSzgKZ8B68HCCRgzpRxvjYNWGXSP9MfV9/Hlg0xrNmwms8HH1Tb FViZ5/MyJglwS1wkvqO0d4VdFsTNpOCKLWBDV38DSlnVktBVfi/5rboj5wJkJ8tfjifT jV6+vI/1OUcOqHK+lIlDJfBI2DQQMfmdQLnOYVvjyMiBmzsXuIAGJG0Y5DfTTaEe+MbK Tj/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777364354; x=1777969154; 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=HM345nkBQh5gaxmYsV6Qr69X0RaLtEwieY1kCbsL1/U=; b=Axapj6XlBlZoa5wxD/Jjow/l4k/aL/Ejc8Zqa/3l5EFNKBWfIuhbGZlJgrId7MsB0N +cq3mi5PvjN7K2Ir7tKjVdJcEzbq0fDdZMsXogqGv1MHYaefbdEw2T3eccpmv5RSIWuB B2gqiudFej8SzUQio3sJlY2FnOWrEdY5HukcreS8Zy45AxPv1zkTYiEqgxiTiszE1Mzd 3rJLuZ87uRRlY3Ufra7aTbpeDVnaSuJ1IXb8DQ1K2X3+IAKrurXzJsxuuJqLGeb/ygIH 5JotWdLpgqlLv00hCm2tYHq7W0WGE73J5q1ibgbCXG6aB73B3nd0Hg0nzXpnfXWbFRsA CEPg== X-Forwarded-Encrypted: i=1; AFNElJ98XcdQXky3+Onkb0mhOj9rBTEpPWsoyuJOn3eLc8qqbLTo1lyM9mFpsz7/rdZRY/N16Sa1vQdWRH13DAc=@vger.kernel.org X-Gm-Message-State: AOJu0YyBOxXLDf892uUpELMcqxHB+fibf6yOyya6id9A4oShIR2/0N0L 6Tij4arrtojhimEFg7Rn2N8LwBN03hdyO0JoNxaMHYMRRvsUAgWBKu0nmwi9WLnXPMA= X-Gm-Gg: AeBDies2jF+LoWSUZnne4zmtFI+AOLSulbOuoTGRYgmTDnLJtE0jB7RlbpVELG9zaQ/ HtV7HgssLYIfGV5aqxyYU4JsYageIrTyK6LwjNUF/SnJ1wFB1BjwD6ITAXcNMgO3KlUN6OuH4qO +7igiIYQYtjFmDaGGZcj0z0qPWVofA3BLtenFS/Dw6dh1+6wFgK2uRJ59gu7PZv4/RJSaXtQ1NC AiKpwHpwHLN2vXfc1pNg6mXc5p5tkyNCkNzuWFo1QiS4IgCIiZaDCH7J301ZRDWItBtV3NmuzZb SfTBrKiicURx6w85yPuQ13YfcooY5VICcf7Zb5FNzrYLEblbCMQDPhjp1iz9eIACAuobcdgnVzQ uQY4v/B+rkQGkgxg8bDOdWuZWVyy7vrn4IRqqOFxkCM8m7rra0cJI6Q1DtMTL30cH0UPE4JkmoI 4PLIx7TNqAjyE4AP/dpp7qoyIDegtQk3ILlk1u5jTuQbTPPu07s0/Jl8g= X-Received: by 2002:a17:90b:5804:b0:35f:b230:5889 with SMTP id 98e67ed59e1d1-36491ccd608mr1594024a91.6.1777364354086; Tue, 28 Apr 2026 01:19:14 -0700 (PDT) Received: from n232-176-004.byted.org ([36.110.163.101]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3649003650esm2181356a91.8.2026.04.28.01.19.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Apr 2026 01:19:13 -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 v8 2/6] mm/memory_hotplug: Fix incorrect altmap passing in error path Date: Tue, 28 Apr 2026 16:18:51 +0800 Message-Id: <20260428081855.1249045-3-songmuchun@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260428081855.1249045-1-songmuchun@bytedance.com> References: <20260428081855.1249045-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) Acked-by: Liam R. Howlett Reviewed-by: Georgi Djakov --- 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 4426abb05655..e3352284f635 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -1469,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, NULL); + arch_remove_memory(cur_start, memblock_size, params.altmap); kfree(params.altmap); goto out; } --=20 2.20.1 From nobody Tue Jun 16 02:37:07 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 C4ED73D47B1 for ; Tue, 28 Apr 2026 08:19:19 +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=1777364361; cv=none; b=D37pdBtWyGxCfCiozT7kIegNrsCj9VmCoqTGNN+sOAmscXMZ+QErcT7wf0w9cjWUasxnLNxdokucZBNKqFh8BnRIe8/LpByeaiHuULZaguOZvSYC7sxIFvTpgI4WgfqCyIsxjmoLUtC8AHZO3TFz4vXWoDAn+tKJn2WMeoJMPAw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777364361; c=relaxed/simple; bh=kq0ejb2jGxbR+oeCmnW0+QnF6ud/XUDbjmtCh/rsP18=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fMi0xq0GSK3Wb+nOyiiOCSedh6kTm+0FmT5sy0FXGquVeIa1GIn5fUD+s2rwv3LrOoVvS3u6wmWyGEnQ2/xrE5LnrjqE0a3CcNFtDCNKKZnWyyib+SBXG2UPlZfbR86Ftg+GmknxVbFVMMoDUVS68wTTPcilxhH9qd0uL+AZaEQ= 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=hUdtpEVf; 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="hUdtpEVf" Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-35d9f68d011so7373260a91.2 for ; Tue, 28 Apr 2026 01:19:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1777364359; x=1777969159; 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=dF2JCDHRyvpz2fSA/d3/Si4g4v2SbytGBie1HIthSGA=; b=hUdtpEVfve1c5ZafR15hODk4/VqExOfbNXFrle4VkgclfZHgSNqPEYzSMxm5I2YO+J TOl4IXG/k4hpQ+IWdfvbSAgesACLFd04y+sUB1Fa+D1ayFv0pkZOrpglXmjpLfi3/rh7 CgBA/4OwkBUZ+qGG/fjT71mFM30zXZrUulLTrtJxlusRWA9KC/WJtD1WIv/8AkYMEHRG GrsG4Ioi6Qoz/gw+lydG/z+b4R8hEVG0mn/6+MM7A/Dw19IdwE6VUpk4xIV5765LDV7C C8zuF1D/YORrQyb+j3sns/5EPh65cH3uRJmaIAVFytvhpKng2ekQDvkzfzqOTwNOsVOT qczA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777364359; x=1777969159; 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=dF2JCDHRyvpz2fSA/d3/Si4g4v2SbytGBie1HIthSGA=; b=ov/41NCik3oFq0XjGOFLeKafFchk5CAEf/5+FVE7njcQSCcLzWnNhjyL/Ae6PzYmxF 6w1IiWZkyXIV+ASpvDE31CHNH2qQUw4HFqyjBVNdfflCzaWRQ+z2xuF2M3YUgxa4IECK YbkDMrAc8BQ9W16Uw02Nw2AGaRqEgpVUipdZRoiSb4GEcs1byVFJe3jNCg//f5LEc75f rxMkfxpf8RFvlN8EYZvj1MFAuw/BlaB0f69qmznKov4i79ule58kyLiSQO5jMQCEqrAp uzuTpEnrEhbQeXPUytN5AI/ApUi5VOj8Bwa37sKGzThP+S/ujngDw1lwVhRWtVveWl3k xVrA== X-Forwarded-Encrypted: i=1; AFNElJ/w3xPtxeS/ey7JTvJ+6XQFZAT8T0Y2TZAbd9YoN8VwEoNocR4FfT+IhBZd1yp1dKBxF1F9KTuU/5z2pCU=@vger.kernel.org X-Gm-Message-State: AOJu0YwPnURupL2707snX6ZlvwwdpDAy9P311svofbYbGY87Abj2u2QF irPUA2RxAB85P5L+JqF9nEyV+Ehg+N0kdL3BGc8izGD50UBSIzzryqXJ0Dnrmoue9IY= X-Gm-Gg: AeBDietHiltCV0Ydhv2Uh9XBrE3uVoiLDBe4dzeAKKwJ/My7VKt20dOrd4n9W6GzSLy h1Ao3W36R89i4pDvvu7uBPkCUJtFxRMGFVL1PxXSq8KRjzIEVmyUrSQ8rWOFvwY2XivcEf9t/U6 4coL80hTLFM/LVRoGFf1E5QbbYYJ9A4vSYIXV0LsjB7nVnznWnv2e9AUW+4lvKQ3cV4HgQTi0K7 D0QaE/aeZ1lnlaIlShC0nRQFZCsozWAMItQNcwJbUaOkhnjaXN7by1m00RC51a5965uXNouYW6m t4y3FzrrvIiKxDWnp01DWNEuCxZy1x4lw3nJWSUh6BB+3xiJQIu7CujlzxirBNQLTF07JHO+6ku MJyLsAEdc1bM84+aNjSJUwYPac8O48IxgeB4z+rQTWz930+Dtm2oc0VNCsxUoUxuoAl8Fun373/ fnwVBkeB2/fJWwc1xepa1iYWJmHoE9LelVelSfh1HeCfJfExCmq35rX3M= X-Received: by 2002:a17:90b:1cc8:b0:362:e51c:fd9c with SMTP id 98e67ed59e1d1-3649200d617mr2345614a91.17.1777364358798; Tue, 28 Apr 2026 01:19:18 -0700 (PDT) Received: from n232-176-004.byted.org ([36.110.163.101]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3649003650esm2181356a91.8.2026.04.28.01.19.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Apr 2026 01:19:18 -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 v8 3/6] mm/sparse-vmemmap: Pass @pgmap argument to memory deactivation paths Date: Tue, 28 Apr 2026 16:18:52 +0800 Message-Id: <20260428081855.1249045-4-songmuchun@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260428081855.1249045-1-songmuchun@bytedance.com> References: <20260428081855.1249045-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) Acked-by: Liam R. Howlett --- 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 e3352284f635..fbb9cae10a8a 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 @@ -1426,7 +1427,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"); @@ -1469,7 +1470,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; } @@ -1555,7 +1556,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; } } @@ -2267,7 +2268,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 Tue Jun 16 02:37:07 2026 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (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 A39E03D3314 for ; Tue, 28 Apr 2026 08:19:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777364367; cv=none; b=gOn8OgyUjKCNWsIvEJDeedWMzdEOIKA+LP8tymb2V7dUwOKsT+hTcMe3/m4OPg0/eC+8THEgNnvb+25sLySMPgzhHVU7Jq3hGUNOivoDqkF0Q1oKbAPax/XqYKXsHplzsuMQY7Xk8+yLw6+MlzVHy/a2kphBCTyb8oN4s2y38U8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777364367; c=relaxed/simple; bh=C4xXtEXsz0ixhX8JiretKLDxPoi22KeWMdBYVODC6is=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VRE+cutVXNeOiOB3np9sYh1ZwyEjZHKB39NsXJ3X+aZUVewh6QOG5SrN+wH/x12p++LpO7Gm+ho5ZiQrTUoYT8NyCdU4aYgOBzo7ztULO0fbOfo9l5YPBTPs3/Q5zQxzPeFVqE81P9xO/n6cb2Qe+9GdcRRqLr6XpPnzy+qcXDc= 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=Xcv6nuLZ; arc=none smtp.client-ip=209.85.216.51 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="Xcv6nuLZ" Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-35fc258aaa4so6937279a91.2 for ; Tue, 28 Apr 2026 01:19:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1777364365; x=1777969165; 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=rL183+C/B0Npg+SiHlF1+/ufB8oZmfwNupE6AmX40Ic=; b=Xcv6nuLZbW6bEKophNnpsam4Jk5GgH8DfF2UZvjY1c2UsFrqRHPpsxBRYWSezuy+ME EKAlKINuZu/ABcAk8XuGI90py0qfbpHS+4VVrqOD6AGjKnV3e6UGYklTYsBgN7qCsCKF M3MTDZ1Jj7o34O7BMBeCRJZvGOEhN+ESzRLi8L+coVbUPgtG1BJpmd6y/bNuTVaowKS8 LTOO21n6fmRn3m60ttJX+oIqGTSELvJmcRfRdDhpNdsBo8CBmCQoZ++mgRXMjmqTGSZJ 24pjfGz7nbjWqLccYn0RVUMdW2xssX/FcSeM8jkQ9cxty9g10JjtgqRDeJ2HDWORS0DX lZjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777364365; x=1777969165; 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=rL183+C/B0Npg+SiHlF1+/ufB8oZmfwNupE6AmX40Ic=; b=i69MADirZsFo8iYYp/jmhkSFkiL4AW8GMgmFnz4FJupgajpJ6GorMq372S97YL+5t+ AXNDQAnuHhyc5F67iqZlG7la3cO/0V5BMFOGBSuBKLxSBdaKgUiUnGrsTXo31BnUWnco 8nS4nKHQ3zv/RaXgOficHHZv02dP0B55oRDiPnPp+Nt3isI7ricoQ3EtjTmorr1zyqSk EEViutSlVzmzsTDbuBJ3hEUizWGD5dXrQ/xYu/aG27VPcmDwzGvnz8MeZ5JPOHpPZlnD T8EuwgR03FgclY5/YzE7LmJLjxdN9CzUQbknfcmA+5kUIZ/xJt6tSP927r0F38ovizE7 mPLw== X-Forwarded-Encrypted: i=1; AFNElJ8CthsK9qnosG3E2QdFlpUX7V8ZHkSgNhHFJvXuQQ96v5szZATYPUHmD5+SFVwYvigfEgvu5fkZZsYH98g=@vger.kernel.org X-Gm-Message-State: AOJu0YxZLs3TFnPMZFj5JvP+zqs/aACntGaqkqmSYDuadHY/NolhXD73 /c3OxQezdatAuMjn9xbMSvoMsMDaJ9BLMvQIublrGZEr6z/buPbgByOMqXApwQFjiLo= X-Gm-Gg: AeBDieteRL02liaN3xLSMJbOjXUwAY946ryLSHdnP/5Qx9sz0SsaTKtMqKctfMWB+Jr d9HXROuLm5qgMolLyB0O6yXZxZXCSxWiLzpTa79ghwJq4gvkhkU1rQYZqnH2UzmHkn5yyTGFZXI DcSvBboxwZSd6tCTPFUnT3GvUL/anlELCI1+BCHNohqTwTAYzAYG0Yevys2sFghtpFyzTvQ/4ML tFRDHKtRtM69NEGx0UudOHaMK6QPw9+d207APog6WZuLFlmxgIdlOaFIjCfaG+BMR9YcQekyUxe PCA4AB+Lhtz42ZcVkxzHCaod3+bRs8Fxc9wOlN6FyqrRUhKyYCvqn0QOkEOldSlW2xP5qL2FcwM jVhMWa2yt97OGWAPk178aSLEXivgB2ra4DAfTzfUkmdEE/rrvkPHkUVOpcc8c6in0eT20PyxP50 yYY1fttuaYIbjJm8G3Hx7yHDXhKFVgrTWthQ+wxwl+njMono4I7qvZT2TByJLAaZplSg== X-Received: by 2002:a17:90b:56cf:b0:35b:96bb:47ba with SMTP id 98e67ed59e1d1-364921a915dmr2295268a91.26.1777364364837; Tue, 28 Apr 2026 01:19:24 -0700 (PDT) Received: from n232-176-004.byted.org ([36.110.163.101]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3649003650esm2181356a91.8.2026.04.28.01.19.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Apr 2026 01:19:24 -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 v8 4/6] mm/sparse-vmemmap: Fix DAX vmemmap accounting with optimization Date: Tue, 28 Apr 2026 16:18:53 +0800 Message-Id: <20260428081855.1249045-5-songmuchun@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260428081855.1249045-1-songmuchun@bytedance.com> References: <20260428081855.1249045-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) Acked-by: Liam R. Howlett --- v7-v8: - Move VM_WARN_ON_ONCE(nr_pages > PAGES_PER_SECTION); to the top of section_nr_vmemmap_pages(). - Add Acked-by from David. --- 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..932082296e8d 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)); + VM_WARN_ON_ONCE(nr_pages > PAGES_PER_SECTION); + + 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)); + + 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 Tue Jun 16 02:37:07 2026 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) (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 E7DBA314D26 for ; Tue, 28 Apr 2026 08:19:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777364373; cv=none; b=nOlBDS0C2cKThrXwVPWFNJI/AZpjknpLJew8S8QM4ewUTYZ/2dOyNvAFpGpPOV+BXcbhsT3rwu6yU3T8oGTAEcO8iw3Q2L1aMWXa1mc4af1VxrGnzS9RyVe4CQe91V2Ca73kSkxPl/hRhUQSwLT1DMKjlV+p9Wjpvt1zhN2XaAo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777364373; c=relaxed/simple; bh=I09DVnwUmT2FgjMlL1sbr+fqXGO2fhcK42PNEcwA7S4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=exiBKioClsiPhd+WGPEBPRSntGbZL2iWL1xWnUZpnunzhLRNomoJjyllLGJNXmqoxM5TeqjqzapZb+pKt71XkiRy4dotE/DL2SWDR4hYUW+Iax/ZdRJxzHj7YWAPene/GvLm5cx/PRi74K1txq88BSHXytXg4oh0+r38Quv5KnM= 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=UuoZxxdF; arc=none smtp.client-ip=209.85.216.49 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="UuoZxxdF" Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-362e30526f8so2065112a91.3 for ; Tue, 28 Apr 2026 01:19:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1777364371; x=1777969171; 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=UuoZxxdFvqi3tYk964lJaaEa0q2ohWRuPhcP8tD9feMemxnE/tk+hCn6pqCwHNcZVZ B8Z8b1gA6bHZDCtZOA1nJFI8VWTm74l7xFcWHRGdThwvDKWp9JqMDdb5ctlLjmejxH5y f1lwlqqmjfam8mF5cG4iuYjpljZWkp8hwggPt1ZDEuxM25slrjRiG7PVYZ9hVONJkMT6 3vOgnw535O9xKwfilVUEY1Fx6BU4fEZKNYf9AMQjKRQqKlrryYeVvO+tRz5cHs/1sYly YtmvbagFyei+bKEPydR7d/ubr/LhU31xTWJnN3MzZaePbiwWXS/1uD9KLnYLq0wB7X0M 7MIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777364371; x=1777969171; 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=jxWiQVW6sGawpSmPAWhp6XT0B16/h2F0UDdIHVBgqJcPh9FTlOhs0mI9YpPUm6JzxH ih0kxKTdCUjatsxuM/2WyP0FMTDapyTG5dUNlrUdxXq+ZBNl/r3aKCEYnfPTmZqGPL2K DiYsXkzDYqpvZIY3tzxMLKObzcoCixPBzWmNO15CoIxL+fNcP6FtY9QY8zzT9PoVQxUa a4WXX8tPHAmhyXpGXHO11Mt27BhLNUMW4Fa93bBtx3WxRsZZffcrzZPMlQRxi3OJhUjy kfLMJkmA+Y0f/1NqsNgPYWMd9DqkqtijSOZ99l/kuYF1xcPA1rw4PQpq+6Pp7trItdMG 3wEw== X-Forwarded-Encrypted: i=1; AFNElJ85Ts9aAlYdsyWBzpE6LyT4fmtPKic/tdUuvM/uCvtpURQ9A6ExfKIxFs20jgKPEtORavXCitRdy6UMmiE=@vger.kernel.org X-Gm-Message-State: AOJu0Yz5Fz/nxGvEvmg58dvfQx4DRwZDn/X+qjaySexpSTAB8LZoFO66 95MQrqoVSm/LvItETIMOycelDpSF3dqq1d90NJ0kk1uXFVv/0N667N0MWCeDkon8uPI= X-Gm-Gg: AeBDievnfcix5Sea6tGXGgBQrA8+bpcVp/5RbZknmdah55LChDljjqcooRHN1A67uxW uGszy7L8zJbyXbeRcgEgnsDNfdqVrIOaf8mItgy3N0ToZqvrST5LqdBisQ29yeGGhCoqf363Ncb Xw/Iv9MemmV6oiJLzVGA7DGXGFA0wcl/gD8auehqOegK9OdkPX6I09AL/wVR09Sth7lE892RmBD ge8bk76c1I4HsB50izluYapiATNTIB5AFAU0NKd32wjTHIuLtpCaBcVsyTEvpuf91M8efeU5jj2 PRD6h/gAcuAMbsBNUI+l9HzryMOpPxJ36S6SJoKMFsjiICBUYHH7GZ2o3iW+U8x1fxoxBhl3kYM 0KUG0d0nHqCkZ12lmMWVr95IcJbr1BROnOGiBDQIxzl8skPO6+/C9nj7FkHw0Q0fkK7TjNWAyh1 snfwGkjJv0dVbhFtJv7ID8e5X1MfhfJRQ3uYKrELqshPdV+9ccNsxoK9E= X-Received: by 2002:a17:90b:17c1:b0:35d:a3b4:2ef6 with SMTP id 98e67ed59e1d1-364920601f9mr2483211a91.21.1777364371205; Tue, 28 Apr 2026 01:19:31 -0700 (PDT) Received: from n232-176-004.byted.org ([36.110.163.101]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3649003650esm2181356a91.8.2026.04.28.01.19.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Apr 2026 01:19:30 -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 v8 5/6] mm/mm_init: Fix pageblock migratetype for ZONE_DEVICE compound pages Date: Tue, 28 Apr 2026 16:18:54 +0800 Message-Id: <20260428081855.1249045-6-songmuchun@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260428081855.1249045-1-songmuchun@bytedance.com> References: <20260428081855.1249045-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) Acked-by: Liam R. Howlett --- 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 Tue Jun 16 02:37:07 2026 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (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 2140A314D26 for ; Tue, 28 Apr 2026 08:19:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777364378; cv=none; b=exeI8MLn2lGrOGoyGJ4/54iiHje8Aj+q2WxcUaa8hGy1z+/gtfoDxuLKrQ4c6xewyS4fHPgrgDr5MDyVYJHnL/cUiJE+4wf1PM1ODZiHH+SJTgZmrkRIwXcInfXnvAgyMY7JY9QxvjUQnyC4ZLl7OPkFkV9VD9fihguvGZYZSQI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777364378; c=relaxed/simple; bh=ywbIjiAmJOpr0oswLhWAUDJeFXDBn1/X+WWcoQ+ww1k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tK2A6rnK3RcdoFlwU9aEXrBCq427yA0pnsqK2W7s8iI3b/5SUXXhRs67UfMZl5xO0McnsKuO2iCWNo54WIYDtAJibRtD4Zgu+BqxlcFY7+C4AamBTMl8pnN6SUEhuqsYdL3KrU7jZeimq+NSFOtP6z08qYDhTIiSCbPt2mk+AyI= 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=g79PBT1P; arc=none smtp.client-ip=209.85.216.42 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="g79PBT1P" Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-3590042fa8eso8527127a91.1 for ; Tue, 28 Apr 2026 01:19:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1777364376; x=1777969176; 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=g79PBT1Pk67KA1ExCBnIF9gO23i41Rg1dXuPffVEjI3lNQX7BtqeVyfBTgXSqtg4Z4 F/1C/KsemEPg/HKR97esYM9bZ0kTb5OJwXBAIRDxqHygv0h362IXma4Y4r6W6mvK/hzE 0In5Mr/9KjmOyFQKrgHHAGvv0XRnu8G0joNGpvzWeHip4wWZkOsGL6yMm+TnDXFXHW1V VedcuG35b/CAN3cGyiARLmiDCDOiidfOrOBZxSZnY1cik29zq30SbsundI8o+2tAYJ7M kBBpRnmQ59RwUf6Q5LlJA/ToITS41acxRZjMIlW7UdEo9QiMna8+EhjNVWwEZigm24XJ bbZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777364376; x=1777969176; 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=hEhfHhhHhGBYrSu7PXYP1tzcwA5zYT0f098xPKuT2PEMIregtt43qorEboOCYxl8mn KFan8qjDLhHLG26HKuY0RV/UUa7PzdxtZ9pPhIuoLmMuqgEOR5P85Y01raEt6oZP5ue+ AGv5E6g+tW621+al/2B0NJf9k39I98cIQmEPcY4g/5Wp0iTR5JIPKJAKC9T71xv6HHJ9 mMPeKkC6PqT78C5HQ9XE8iirQYvzDfe1ACNWHQp+o0DZT+SU6Czh6dktXKrzRrtSMakf x5Z5/KS+YyT9OaT/HOpFtgG9E0gSUvhWXz+mv6TnaKv7QHUK7sS/O9cV8sv/XOk6l6GY vseQ== X-Forwarded-Encrypted: i=1; AFNElJ/+21khVOTAM3En5eDRpAFTA+tmIv6Ow14a2Gw5uVqnHKwOIQnxCaOnFqvD5N7pjdsnZFiXMCY3ioZUjQM=@vger.kernel.org X-Gm-Message-State: AOJu0YwXUtUw1rLfzohVRAxn8yQw79mma6165BmGEDE2MegJNh+jVsTU J1B8whQw3n9kL9S3IuJeI57n0yw2vJ+mj0ebIEI7ryHHcpIYG0LJuX+pcg7UjpdyeIY= X-Gm-Gg: AeBDieuKR6FwuTPD47Eqx1rQvSHlOo+U4LEYv7D/wLjrAMUwLwHCwllT4T7tSVzSgtp HmgD1qwTQx8hPEbdY8vcTq6hiWjpL9f0rX7WQxl0GCKDafgezsG3/MHzvpsvgztad46++R7cu85 6G9nfQA+KRidlcpIFSyTKDdQIJzCQvZxjN44/gXUF0CW9bBQf+UmVqkDxmYPfGMvRlzw9D4r6gV R+lkKZQUecAAgwK9KiJsywc3hArMU3lle93+N53Ib15EQeMXoygfqrdYyZgmMvGe5VxWHTZqVWx P8C1NpLKP0OKyUGcg9I/P4L2CVS7HPEwlhuIA/DLW7DsMBMjdKUnjXWnTfmDdERoGnUemG+JVOi RYAERlfLGhGPgWBcSg5rXzTYmZjqFN+0zRFOpYR20Y0Rl+IxEWhb9ayuJBt4p4n5mSQxUKkqCZZ qZd2wbyXYl3UCbHxTKqUIj9jJoZi53nHc/0Ig+8QvZ+vDnL1xd3BcSZvQ= X-Received: by 2002:a17:90b:5808:b0:35b:e5ce:73bb with SMTP id 98e67ed59e1d1-36491fbf11dmr2272598a91.1.1777364376405; Tue, 28 Apr 2026 01:19:36 -0700 (PDT) Received: from n232-176-004.byted.org ([36.110.163.101]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3649003650esm2181356a91.8.2026.04.28.01.19.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Apr 2026 01: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 , stable@vger.kernel.org Subject: [PATCH v8 6/6] mm/mm_init: Fix uninitialized struct pages for ZONE_DEVICE Date: Tue, 28 Apr 2026 16:18:55 +0800 Message-Id: <20260428081855.1249045-7-songmuchun@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260428081855.1249045-1-songmuchun@bytedance.com> References: <20260428081855.1249045-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) Acked-by: Liam R. Howlett --- 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