Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm/swap: fix swap cache memcg accounting

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 cache.

Link: https://lkml.kernel.org/r/20260320050601.1833108-1-alex@ghiti.fr
Fixes: 2732acda82c9 ("mm, swap: use swap cache as the swap in synchronize layer")
Signed-off-by: Alexandre Ghiti <alex@ghiti.fr>
Acked-by: Kairui Song <kasong@tencent.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Reviewed-by: Nhat Pham <nphamcs@gmail.com>
Acked-by: Chris Li <chrisl@kernel.org>
Cc: Alexandre Ghiti <alex@ghiti.fr>
Cc: Baoquan He <bhe@redhat.com>
Cc: Barry Song <baohua@kernel.org>
Cc: Kemeng Shi <shikemeng@huaweicloud.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Alexandre Ghiti and committed by
Andrew Morton
9e0d0ddf 26d3dca2

+4 -5
+4 -5
mm/swap_state.c
··· 494 494 495 495 __folio_set_locked(folio); 496 496 __folio_set_swapbacked(folio); 497 + 498 + if (!charged && mem_cgroup_swapin_charge_folio(folio, NULL, gfp, entry)) 499 + goto failed; 500 + 497 501 for (;;) { 498 502 ret = swap_cache_add_folio(folio, entry, &shadow); 499 503 if (!ret) ··· 516 512 swapcache = swap_cache_get_folio(entry); 517 513 if (swapcache) 518 514 goto failed; 519 - } 520 - 521 - if (!charged && mem_cgroup_swapin_charge_folio(folio, NULL, gfp, entry)) { 522 - swap_cache_del_folio(folio); 523 - goto failed; 524 515 } 525 516 526 517 memcg1_swapin(entry, folio_nr_pages(folio));