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.
Unified · Split
| @@ -494,6 +494,10 @@ static struct folio *__swap_cache_prepare_and_add(swp_entry_t entry, · 493 unmodified lines | |||
| 494 | 494 | ||
| 495 | __folio_set_locked(folio); | 495 | __folio_set_locked(folio); |
| 496 | __folio_set_swapbacked(folio); | 496 | __folio_set_swapbacked(folio); |
| 497 | |||
| 498 | if (!charged && mem_cgroup_swapin_charge_folio(folio, NULL, gfp, entry)) | ||
| 499 | goto failed; | ||
| 500 | |||
| 497 | for (;;) { | 501 | for (;;) { |
| 498 | ret = swap_cache_add_folio(folio, entry, &shadow); | 502 | ret = swap_cache_add_folio(folio, entry, &shadow); |
| 499 | if (!ret) | 503 | if (!ret) |
| @@ -514,11 +518,6 @@ static struct folio *__swap_cache_prepare_and_add(swp_entry_t entry, · 14 unmodified lines | |||
| 514 | goto failed; | 518 | goto failed; |
| 515 | } | 519 | } |
| 516 | 520 | ||
| 517 | if (!charged && mem_cgroup_swapin_charge_folio(folio, NULL, gfp, entry)) { | ||
| 518 | swap_cache_del_folio(folio); | ||
| 519 | goto failed; | ||
| 520 | } | ||
| 521 | |||
| 522 | memcg1_swapin(entry, folio_nr_pages(folio)); | 521 | memcg1_swapin(entry, folio_nr_pages(folio)); |
| 523 | if (shadow) | 522 | if (shadow) |
| 524 | workingset_refault(folio, shadow); | 523 | workingset_refault(folio, shadow); |