Skip to content

Commit 9e0d0dd

Browse files
AlexGhitiakpm00
authored andcommitted
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: 2732acd ("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>
1 parent 26d3dca commit 9e0d0dd

1 file changed

Lines changed: 4 additions & 5 deletions

File tree

mm/swap_state.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,10 @@ static struct folio *__swap_cache_prepare_and_add(swp_entry_t entry,
494494

495495
__folio_set_locked(folio);
496496
__folio_set_swapbacked(folio);
497+
498+
if (!charged && mem_cgroup_swapin_charge_folio(folio, NULL, gfp, entry))
499+
goto failed;
500+
497501
for (;;) {
498502
ret = swap_cache_add_folio(folio, entry, &shadow);
499503
if (!ret)
@@ -514,11 +518,6 @@ static struct folio *__swap_cache_prepare_and_add(swp_entry_t entry,
514518
goto failed;
515519
}
516520

517-
if (!charged && mem_cgroup_swapin_charge_folio(folio, NULL, gfp, entry)) {
518-
swap_cache_del_folio(folio);
519-
goto failed;
520-
}
521-
522521
memcg1_swapin(entry, folio_nr_pages(folio));
523522
if (shadow)
524523
workingset_refault(folio, shadow);

0 commit comments

Comments
 (0)