From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) (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 DC79934C826 for ; Wed, 15 Apr 2026 11:14:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776251668; cv=none; b=B54m3dT3tx793FZ53Jh9Vo/SRrtV9NtV26FIMPI2QFiWOKgcqlkU2EE3Z1muIZfD5SbB6lvuQJXu/gb9Ytg1bO4MSZe9U/XaPqHnO6i4RWu1sktmHUVnr8zLIe+eKgOMvgrETGVksBi4+hB1vURbo/3fxkzHkVKB+RY31rZKAMo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776251668; c=relaxed/simple; bh=C/EhSahQ1RWLRKsO1SIZ1e5Tp3HJP9eth020COCsDUk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EKOKQO7cZjMWYpyDLLJ6TESoQIuUi1rVSq8yIRGMFoeug6mhpGyhcl3FJ0kfGQX0ZZuzwua/n5IlDiZBcU0fw+P+6M+jSJcMUO4G6EBmoJP5vtqFjEpeNknSN6SJ+W27ME5me0noY3w5Bom7ENk8o4OIik/c8sVHYQMf5FEcXbg= 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=bPlguRbH; arc=none smtp.client-ip=209.85.216.50 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="bPlguRbH" Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-35d9f68d011so4284097a91.2 for ; Wed, 15 Apr 2026 04:14:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1776251666; x=1776856466; 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=t2IwsSjULj2cdQGLjgGLVnmP99rI03lQ3ndkxR63O5c=; b=bPlguRbHRdKKB/EPXGXPNluUTi/xaYsVQrZfL66GwQtKmgOGwbwJjxT/VvJsLvJt+w A7TBVHlRXCIUXdYTefFfMtmdLerbsCeJBLsLVEL9YPXrkCafHHnBqJ+2YS8cAuQIXocC 80Yh1jOVc8lDcYTZCUNLUsm+LZgHAO0X57dJvmMHhoC4kR5aW925DexXpEUImerIPok3 i7rnilDqCsGfUxrRuRsgray9vQzOXz7IshXkjJ2CoFC/1DRiujUuoNxgdMx/6liFCna2 ZTgXplKJnCvBqornkHtHEntvhGf8NCNtxxFc1AYW+mFgxFU80JU9j0cGBvh6KhdVoQi5 W7jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776251666; x=1776856466; 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=t2IwsSjULj2cdQGLjgGLVnmP99rI03lQ3ndkxR63O5c=; b=EF1yvw1TQZOvGJpGiJTI4pd1ApiWjGaROhrR7OB/sakGroTaQShwDFaQ6+ms/QkIQY B1ZZMLX50Mg/GApB22uR4nGGECZq/66hBYGscUKScOfrbhpg2TndbMQnVKILPul0p9y0 c70HuXOZEDwYPp5pZX/9WANU+p8MXqsd0m+Zuob08QU2ZiRqBpGDwt0aF5sHpB9gInUf 5NLaUtDbrZTgZ08YiyY5iMJ1+KWnv47PiMn0lUqIFWaau5Ds1W83uovfwumKDrnCw/tP ZJuZbaOrzk2MiFlAWlqixadMomoWlXm9UdDitrexc34q83U6ZfTj9krC3jE5nt3ck1x2 swTA== X-Forwarded-Encrypted: i=1; AFNElJ/HcBMLs/Bi7FSISZJBxtCQ4CGCGaJaDULI2n3fGPU3It5rdB0pQlAPwRB9WkZj/6A/ud0mueOgpt2YqU0=@vger.kernel.org X-Gm-Message-State: AOJu0YyO6iMvNP3ihILI2vVJp4T6Dw26MhTej8azd2SilEyotimIaruZ /Sc+BvGmKa3cVbMXQra/A6gY1vLwJRloHd3BM9r635HlUpPaIuR0hGIsScwjQBZsmZA= X-Gm-Gg: AeBDies3r5YSIlmx6kc26MUEmo1xMnWoLj2/1Uqd/1E45jGlOqLyoacl1C76Ar9mdeq thybOT3fTLQC/DjHlawCunRXMU5vvc2dDwYaH9a2oeFCsl0J8j54d5ysAnE44FY8/nlGUiOw1QX ueMqaFcU9bIuedhpl1adfmdjyf75+C1g+RH2FgiAT9mI8nLveikSf5o0ZsF7gXZi3RHnS33mB/j WmgTewJZ+dq46SlmTrgadvXcAgl6zqmDsSrlzAw+BXOMo2/iWAhP6Y5gy6uWZJj+1hPNe9ds7C7 bDFMk8t3/LuP5OfRYE3EnAMjdzrvRCqjvuZ3l+XFWzl6Fa3o5XDNIuRlHSmhMOJVmKVFrAIBIRB RtLACHtjVd9Wbx/HNzVte17MbSjlJUyIzdxT5p7IKoOJFUsj0aA0LtE88sbvHRcrpOn9CD4AlOD xZNQXkQsQIZuj9YSSHL3l82OHlCby0hTEe34J9C0PoTUg= X-Received: by 2002:a17:90b:17cd:b0:35f:c1e1:a263 with SMTP id 98e67ed59e1d1-35fc1f0e974mr7643499a91.19.1776251666069; Wed, 15 Apr 2026 04:14:26 -0700 (PDT) Received: from n232-176-004.byted.org ([36.110.163.96]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b477fd3724sm19509485ad.0.2026.04.15.04.14.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Apr 2026 04:14:25 -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 v2 1/6] mm/sparse-vmemmap: Fix vmemmap accounting underflow Date: Wed, 15 Apr 2026 19:14:07 +0800 Message-Id: <20260415111412.1003526-2-songmuchun@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260415111412.1003526-1-songmuchun@bytedance.com> References: <20260415111412.1003526-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: 8bit 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 - free_map_bootmem() handles early bootmem section accounting 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 --- 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 = __populate_section_memmap(pfn, nr_pages, nid, altmap, + pgmap); + + memmap_pages_add(DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE)); + + return page; } static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages, @@ -665,13 +670,17 @@ static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages, unsigned long start = (unsigned long) pfn_to_page(pfn); unsigned long end = start + nr_pages * sizeof(struct page); + 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 = (unsigned long)memmap; unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION); + memmap_boot_pages_add(-1L * (DIV_ROUND_UP(PAGES_PER_SECTION * sizeof(struct page), + PAGE_SIZE))); vmemmap_free(start, end, NULL); } @@ -774,14 +783,10 @@ static void section_deactivate(unsigned long pfn, unsigned 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), PAGE_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); - } if (empty) ms->section_mem_map = (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)); return memmap; } -- 2.20.1