From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F3ADE3DDDC6 for ; Wed, 15 Apr 2026 15:53:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776268416; cv=none; b=ZHRuiXIRow9L9NxXLN3xGIPt7gpjYQEVgzePcD+0EMFnW6msM+GilHjhu/ITiQhZ00GfRQuPNg6GknbwxWdkiiIPD/Rk932v2iY7XLlYzzWIo6N27+6ZInQikCzIdCylsTNhx2SHITdeTtEpDAsV2MUPJDhvA8Qy5LQuZxofOwE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776268416; c=relaxed/simple; bh=pF5Ty1TKfbXQTKmLGalKXImAXtzwkULsJDc+vi4BCZU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ph0VPMnvK7fQgQBI2vYVv53J1+NF2WbNSDYXUf35nfAiJ1HdIGednu92EQ/Pnwk9Hac0VUyZeO7R/yX0xMBG6Cm3VjK4Y/pMHA9hFJj2vvEDZwE9/BuP0wyRg0uCHaZj2UwNIwdv8JRRYexU+sCtBDV/x84ebwerJBGxnF/bWAM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DlGLUOug; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DlGLUOug" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8A646C19424; Wed, 15 Apr 2026 15:53:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776268415; bh=pF5Ty1TKfbXQTKmLGalKXImAXtzwkULsJDc+vi4BCZU=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=DlGLUOuga1MKiMMANQa+Xk/NacVG+N3rBbJfCtnyo19AEl1tE+4MksQOwjbEhbhPp jW7SzH/5Nyz1ZO+tC4wvPydch5UTMHICyGDxlOJm3ceDJeRhFvHkZ+8+LsUtLzoyhz 32fmq9wPfWG550vpxwBSqtJPFwaqyFBz7UFdwAH7je4GAH6yNHCNBfXGpn+pPkFSwb bx8T3rxSf1V08O+tncqh6acRwlEE0HsKP0+zmeWypjdet9c+vb+WXyd9YyI4LRlEoe Zdr9LirARf4S4xVxQIGPC3feUCBDloz+XHbrlXpQK1a912jqlO4/aLE6DCRJa98/Lt DPukwP+gwpx0A== Date: Wed, 15 Apr 2026 18:53:25 +0300 From: Mike Rapoport To: Muchun Song Cc: Andrew Morton , David Hildenbrand , Muchun Song , Oscar Salvador , Michael Ellerman , Madhavan Srinivasan , Lorenzo Stoakes , "Liam R . Howlett" , Vlastimil Babka , 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 Subject: Re: [PATCH v2 1/6] mm/sparse-vmemmap: Fix vmemmap accounting underflow Message-ID: References: <20260415111412.1003526-1-songmuchun@bytedance.com> <20260415111412.1003526-2-songmuchun@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260415111412.1003526-2-songmuchun@bytedance.com> On Wed, Apr 15, 2026 at 07:14:07PM +0800, Muchun Song wrote: > 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 Acked-by: Mike Rapoport (Microsoft) > --- > 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 > -- Sincerely yours, Mike.