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 D85A982899 for ; Sun, 5 Apr 2026 12:53:28 +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=1775393610; cv=none; b=U0iBIctJDGj8bCWDk7m5z4eWou/e+xx+/32OFvyTXS79a/gLvvQ20upOcnuxP9nUp3vcan14v3quRGDUIaoKQ4wZ42WLSV3dqSXfvXvTF7l/1smxJOt2jNqjNAFcEfvuKdvDlEP1kEzTjjHV1ACK4taNfCWwO02SIYDNXou362M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775393610; c=relaxed/simple; bh=htMVPQQBtrtxFxnD0//lX5HGQvAiU090DXKaAq62AGg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Vyu/nLuQ3WdqLuI2Jhyjc/mvCoLq+932c1AJcN5BeuE1S3A0pTn0NysHfsOtlMAPyYUoSp1MoZleZwE28nRgdY8XdKZMmEfuUAs//2Tk0sZW95eSfBmX4o3MddeezhMfHDTWWqUjUwhcSvpCvktW1Ja4d8eb7mrh2vRxPHZvm5o= 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=NAVnRRWc; 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="NAVnRRWc" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-35c124d2613so1749896a91.2 for ; Sun, 05 Apr 2026 05:53:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1775393608; x=1775998408; 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=iSQZfRjRjpxozRWOnZuEHxRe4aXVCopNiKXhl9pDTQQ=; b=NAVnRRWcQex1DfmzgM9rEPT4kALlczygt0fXqO5jYzLQd3lpTKuxtJcegG6PE+8WDN 7oC5WZjWXdd1RwUDBriqFG3Fh1ODdbhh16ibA/Th4eyZ8sf0R0XeGUEoI3Mujz3gGuRR B4cRI63xkVJKKtUCCug/EgwMSSRrSBf6XpZ7p4P9MdurxfY6TB4yGkoESbCsmh6MhgqY 1Syjyu+7FL0Pl7yR+Hikbp6I7GH6l974WhQ8wPKsi0Xg7TJhE6FxqaD0wIQURPYXI+2T uA62tNF/6EU1nXvjry++BUlwUsh9h1XDu8qGGmnj2N1QV/x+ro3PZMfI3Qrl3dovjOSj EhCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775393608; x=1775998408; 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=iSQZfRjRjpxozRWOnZuEHxRe4aXVCopNiKXhl9pDTQQ=; b=kuEZk3erx1jybPnXcOvCn0+YMzCuOPT6M6ocyi3Zd19el1Z/rKEgSeYQNLZePrVR9q yJpJrCacY5T+J5mrHYpRKvdZxefE1KRsGk1v9v67PASm4wWsivM6mAxrWg9aSrZNRLCg iO04wIKKL0xK8C2kNcc4frKkp186e+a7uLQdCtJ8Zms1ULjGMvY7Pk6/ajr28aJB0kKg Un14wIBVa0QNojxeEeTBHWlm8ItklrJt4Kcmp29H3M4HbB/50XkDM4TQ9Vg2b3n/hjeE Eu4SeFi0acU34+OYRrl2zjhOfH0mlFMuVeySbPb9jeqXz31EnAPIr+f/U9AZFHUS/ewG V14g== X-Forwarded-Encrypted: i=1; AJvYcCWuR3epwQdoj6MPSFD4SHsYfcH+uo0LVht1ls+BUDzgaSehggVtn6fysqNfwm1K5gEB4itjQPqqYF1+8yU=@vger.kernel.org X-Gm-Message-State: AOJu0YycSmEu4OLvpozkfM51uOI0ilKYa6LBHY08oVgHInIfpVeAlVzk UTYhLYrarJ1UYDrY+gcPDL1CziyjFS1LPNKQ0Bbb/iL1YWusOr4ygRI/S6Vb/30WD4w= X-Gm-Gg: AeBDiev1Jreb+P3/DMm6fvP46+EDNd7lm7fC0PkMEjGClM23X78L7nxWmiA7ljlAep/ tskeiPUHthsrYwsvulsnNawT3iDCczBivfwBdPhomELswTkpjHGtcoy3aLeZ90QWj3nhQ8vtbIK AYxyv61iIcoSDDCyAJj27fyMTBfiM5SdBWRUuufEbKYpLuk/V43dobcQjPo2/Ttu8eAF/BgARQO OkS1F5zsrfS7AhOHG94dpewwwtsJNb9QLY9tfrHoKI0SMqIm1DA8Ynj1aiMA+NIHIPExRJXBjNe UTQ8et2drCy/TT1irN08CYUJgG4Rwii1M8SEBBe6E999CYVnCzR5xPo3hwMWb2Cfr/NiOCe7imk b09syHigMv7QJuv0LQGXqJ43iy5bvdmX8RplNvvKANJ55ZRNjIsUKabIEcyuJXvFbEiXlwOS4tM hVKwiPAwYphbxQP7Zi6/o/iFLhsyoo2QNT1OHmUcxzQ18= X-Received: by 2002:a17:90b:4c4b:b0:35d:93ff:2854 with SMTP id 98e67ed59e1d1-35de680e77fmr9867916a91.8.1775393608080; Sun, 05 Apr 2026 05:53:28 -0700 (PDT) Received: from n232-176-004.byted.org ([36.110.163.97]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-35de66b4808sm3748505a91.2.2026.04.05.05.53.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Apr 2026 05:53:27 -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 01/49] mm/sparse: fix vmemmap accounting imbalance on memory hotplug error Date: Sun, 5 Apr 2026 20:51:52 +0800 Message-Id: <20260405125240.2558577-2-songmuchun@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260405125240.2558577-1-songmuchun@bytedance.com> References: <20260405125240.2558577-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 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)); 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