From nobody Mon Apr 6 09:15:06 2026 Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) (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 8B478233134 for ; Fri, 20 Mar 2026 05:06:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.196 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773983208; cv=none; b=qfDIS4UWA0p5rnwip3rLAJHZ/EtJR2DZctyFRiLpAcXR4yu8o5rYh04Ufws+Xggcu/ReWhWJnKtrmtS8fET4dhf32f+Tn4BDIwTm0oqoA+R8GmqcNQlTBe0asXd7WfrPWpjHw/69fL70u/WxCCOocESogSw4piT7dYZ4aMbma+g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773983208; c=relaxed/simple; bh=U4fOl4LEau1MPz3aE65umkaR9J2cWPvRVaP99R17raY=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=mBCnswLs/8SfIDXSDnKQ8geELiKeNCar1dDR7fu0ybbRoke3OXfI9xEX0xz6szlvi2Aigi9kIPNblxmxtFFg3suBDhclzZy3BmqZ/U5HFQLHoIZuALoshDnWoaUWE7RcW1XG5DFartSUGDapXCDWJpwYqcw584w6XG1boT36Byc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ghiti.fr; spf=pass smtp.mailfrom=ghiti.fr; arc=none smtp.client-ip=217.70.183.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ghiti.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ghiti.fr Received: by mail.gandi.net (Postfix) with ESMTPSA id DCD0E3E9B3; Fri, 20 Mar 2026 05:06:36 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , hannes@cmpxchg.org, kernel-team@meta.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Alexandre Ghiti Subject: [PATCH] mm/swap: fix swap cache memcg accounting Date: Fri, 20 Mar 2026 06:05:59 +0100 Message-ID: <20260320050601.1833108-1-alex@ghiti.fr> X-Mailer: git-send-email 2.53.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-GND-Sasl: alex@ghiti.fr X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgdeftdeltdduucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufffkffoggfgsedtkeertdertddtnecuhfhrohhmpeetlhgvgigrnhgurhgvucfihhhithhiuceorghlvgigsehghhhithhirdhfrheqnecuggftrfgrthhtvghrnhepfeffffdvveefjeeuhedvieekkeefhfejhefgkeejffdukeeigfekudehleehkeefnecukfhppedvtddtudemkeeiudemfeefkedvmegvfheltdemkeefuddumeeiugduudemsgdtgeejmeeghegttgenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepihhnvghtpedvtddtudemkeeiudemfeefkedvmegvfheltdemkeefuddumeeiugduudemsgdtgeejmeeghegttgdphhgvlhhopegrlhgvgihghhhithhiqdhfvgguohhrrgdqrffhgefokeeivdfrrdhlrghnpdhmrghilhhfrhhomheprghlvgigsehghhhithhirdhfrhdpqhhiugepffevffdtgfefgfelueefpdhmohguvgepshhmthhpohhuthdpnhgspghrtghpthhtohepuddvpdhrtghpthhtoheprghkphhmsehlihhnuhigqdhfohhunhgurghtihhonhdrohhrghdprhgtphhtthhopegthhhrihhslheskhgvrhhnvghlrdhorhhgpdhrtghpthhtohepkhgrshhonhhgsehtvghnt ggvnhhtrdgtohhmpdhrtghpthhtohepshhhihhkvghmvghngheshhhurgifvghitghlohhuugdrtghomhdprhgtphhtthhopehnphhhrghmtghssehgmhgrihhlrdgtohhmpdhrtghpthhtohepsghhvgesrhgvughhrghtrdgtohhm Content-Type: text/plain; charset="utf-8" The swap readahead path was recently refactored and while doing this, the order between the charging of the folio in the memcg and the addition of the folio in the swap cache was inverted. Since the accounting of the folio is done while adding the folio to the swap cache and the folio is not charged in the memcg yet, the accounting is then done at the node level, which is wrong. Fix this by charging the folio in the memcg before adding it to the swap ca= che. Fixes: 2732acda82c9 ("mm, swap: use swap cache as the swap in synchronize l= ayer") Signed-off-by: Alexandre Ghiti Acked-by: Chris Li Acked-by: Johannes Weiner Acked-by: Kairui Song Reviewed-by: Nhat Pham --- mm/swap_state.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/mm/swap_state.c b/mm/swap_state.c index 6d0eef7470be..48aff2c917c0 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -494,6 +494,10 @@ static struct folio *__swap_cache_prepare_and_add(swp_= entry_t entry, =20 __folio_set_locked(folio); __folio_set_swapbacked(folio); + + if (!charged && mem_cgroup_swapin_charge_folio(folio, NULL, gfp, entry)) + goto failed; + for (;;) { ret =3D swap_cache_add_folio(folio, entry, &shadow); if (!ret) @@ -514,11 +518,6 @@ static struct folio *__swap_cache_prepare_and_add(swp_= entry_t entry, goto failed; } =20 - if (!charged && mem_cgroup_swapin_charge_folio(folio, NULL, gfp, entry)) { - swap_cache_del_folio(folio); - goto failed; - } - memcg1_swapin(entry, folio_nr_pages(folio)); if (shadow) workingset_refault(folio, shadow); --=20 2.53.0