diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c index 56d27baf93ab..982cfe5af225 100644 --- a/mm/memcontrol-v1.c +++ b/mm/memcontrol-v1.c @@ -614,6 +614,7 @@ void memcg1_swapout(struct folio *folio, swp_entry_t entry) { struct mem_cgroup *memcg, *swap_memcg; unsigned int nr_entries; + unsigned short oldid; VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); VM_BUG_ON_FOLIO(folio_ref_count(folio), folio); @@ -630,6 +631,16 @@ void memcg1_swapout(struct folio *folio, swp_entry_t entry) if (!memcg) return; + /* + * Check if this swap entry is already recorded. This can happen + * when MADV_PAGEOUT is called multiple times on pages that remain + * in swapcache, reusing the same swap entries. + */ + oldid = lookup_swap_cgroup_id(entry); + if (oldid == mem_cgroup_id(memcg)) + return; + VM_WARN_ON_ONCE(oldid != 0); + /* * In case the memcg owning these pages has been offlined and doesn't * have an ID allocated to it anymore, charge the closest online