From mboxrd@z Thu Jan 1 00:00:00 1970 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: 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 This ensures proper accounting in all code paths, including error handling and early section cases. Fixes: c3576889d87b ("mm: fix accounting of memmap pages") Cc: stable@vger.kernel.org Signed-off-by: Muchun Song Acked-by: Mike Rapoport (Microsoft) Acked-by: Oscar Salvador Acked-by: David Hildenbrand (Arm) --- mm/sparse-vmemmap.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c index 6eadb9d116e4..a7b11248b989 100644 --- a/mm/sparse-vmemmap.c +++ b/mm/sparse-vmemmap.c @@ -656,7 +656,12 @@ static struct page * __meminit populate_section_memmap(unsigned long pfn, unsigned long nr_pages, int nid, struct vmem_altmap *altmap, struct dev_pagemap *pgmap) { - return __populate_section_memmap(pfn, nr_pages, nid, altmap, pgmap); + struct page *page = __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