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 7B3111684B4 for ; Mon, 13 Apr 2026 12:05:03 +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=1776081904; cv=none; b=REHCUAekPRcAe+d0hpvpqaq0iQwaLiToFKinw74rxW2utVIi7paa5D0El9m+6u+ce/GdFtZfaik1gSVv3mlhzA67A2yOlSdLZOa2q0XTTcXqACg3RP8PuqyzmvMsNQ640CDot9EKbdhTep4Bs15wn7s7NFCMGj9g0H4Gk/Xa5ME= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776081904; c=relaxed/simple; bh=cSo4sEdsNwlz/Iqs10U18EHsNdWWL3XI+9TPegcabV8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=P2d/LLTMKmTtcmhKDgLgDN9E9VRcnOYkI/0DOgXYL2zAE/lWCKo0QBIK+hIhJh4qm1/ceWpKJ358uluFanynSJ/sVGGSbizVm7XlrNQ07Xuu7PExX7OkgXQKlh2+0gKnIhgTYFXSj3hg0+R/8t3/rsbywe8iOdwK3CHTOGACjoA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=at+QCcd8; 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="at+QCcd8" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D0EFDC19421; Mon, 13 Apr 2026 12:04:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776081903; bh=cSo4sEdsNwlz/Iqs10U18EHsNdWWL3XI+9TPegcabV8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=at+QCcd8hldAMXHMewcQlTRTSnPiYtb+AMBMRvJ2S3KRydAkuWOSAT5L5UCYcSoX/ ROa9bELImeOFvQzvTvpDpcl4kesvAFWZEWDTyLABjzRsnNsdUqSTmHo8oGoko42yP1 ySfzT1F5fS+ieBGBLG+LF/vt9HIB747aetWSZlmf5ZsXJNo8GFsCT2SCUVjsNcF0+D Y6DQqlHoXOTwKiulifQcVyMaqFKxVh70yge0I5RxWAyvSyVsOM3ft5xcsQIebGm+6U 4yi40VjDnwrKe0gqOTVZYA/w2Lw0t9gyBbQ8clbAsFDUDIHE47hCCod4Zw1isILUZB tqQdQ9IAZeV3Q== Date: Mon, 13 Apr 2026 15:04:54 +0300 From: Mike Rapoport To: Muchun Song Cc: Muchun Song , Andrew Morton , David Hildenbrand , 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 01/49] mm/sparse: fix vmemmap accounting imbalance on memory hotplug error Message-ID: References: <20260405125240.2558577-1-songmuchun@bytedance.com> <20260405125240.2558577-2-songmuchun@bytedance.com> <35454ADD-C983-4577-997E-884266C56FB6@linux.dev> 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=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <35454ADD-C983-4577-997E-884266C56FB6@linux.dev> On Mon, Apr 13, 2026 at 05:49:17PM +0800, Muchun Song wrote: > > > > On Apr 13, 2026, at 17:35, Mike Rapoport wrote: > > > > On Mon, Apr 13, 2026 at 12:19:50PM +0300, Mike Rapoport wrote: > >> On Sun, Apr 05, 2026 at 08:51:52PM +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 > >>> approach introduces an accounting imbalance. > >>> > >>> Since the commit c3576889d87b ("mm: fix accounting of memmap pages"), > >>> memmap pages are accounted for only after populate_section_memmap() > >>> succeeds. However, section_deactivate() unconditionally decrements the > >>> vmemmap account. Consequently, a failure in populate_section_memmap() > >>> leads to a negative offset (underflow) in the system's vmemmap tracking. > >>> > >>> We can fix this by ensuring that the vmemmap accounting is incremented > >>> immediately before checking for the success of populate_section_memmap(). > >>> If populate_section_memmap() fails, the subsequent call to > >>> section_deactivate() will decrement the accounting, perfectly offsetting > >>> the increment and maintaining balance. > >>> > >>> Fixes: c3576889d87b ("mm: fix accounting of memmap pages") > >>> Signed-off-by: Muchun Song > >>> --- > >>> mm/sparse-vmemmap.c | 2 +- > >>> 1 file changed, 1 insertion(+), 1 deletion(-) > >>> > >>> diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c > >>> index 6eadb9d116e4..ee27d0c0efe2 100644 > >>> --- a/mm/sparse-vmemmap.c > >>> +++ b/mm/sparse-vmemmap.c > >>> @@ -822,11 +822,11 @@ static struct page * __meminit section_activate(int nid, unsigned long pfn, > >>> return pfn_to_page(pfn); > >>> > >>> memmap = populate_section_memmap(pfn, nr_pages, nid, altmap, pgmap); > >>> + memmap_pages_add(DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE)); > >> > >> This logically belongs to success path in populate_section_memmap(). If we > >> update the counter there, we won't need to temporarily increase it at all. > > > > Not strictly related to this patchset, but it seems, we can have a single > > memmap_boot_pages_add() in memmap_alloc() rather than to update the counter > > in memmap_alloc() callers. > > It will indeed become simpler and is a good cleanup direction, but there > is a slight change in semantics: the page tables used for vmemmap page > mapping will also be counted in memmap_boot_pages_add(). This might not > be an issue (after all, the size of the page tables is very small compared > to struct pages, right?). > > Additionally, I still lean toward making no changes to this patch, because > this is a pure bugfix patch — of course, it is meant to facilitate backporting > for those who need it. The cleanup would involve many more changes, so I > prefer to do that in a separate patch. What do you think? For this patch and easy backporting I still think that cleaner to have the counter incremented in populate_section_memmap() rather immediately after it. > Thanks, > Muchun. > > > > >>> if (!memmap) { > >>> 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. > > > > -- > > Sincerely yours, > > Mike. > > > -- Sincerely yours, Mike.