Skip to content

Commit b7ffecb

Browse files
shakeelbtehcaster
authored andcommitted
memcg: slub: fix SUnreclaim for post charged objects
Large kmalloc directly allocates from the page allocator and then use lruvec_stat_mod_folio() to increment the unreclaimable slab stats for global and memcg. However when post memcg charging of slab objects was added in commit 9028cde ("memcg: add charging of already allocated slab objects"), it missed to correctly handle the unreclaimable slab stats for memcg. One user visisble effect of that bug is that the node level unreclaimable slab stat will work correctly but the memcg level stat can underflow as kernel correctly handles the free path but the charge path missed to increment the memcg level unreclaimable slab stat. Let's fix by correctly handle in the post charge code path. Fixes: 9028cde ("memcg: add charging of already allocated slab objects") Signed-off-by: Shakeel Butt <[email protected]> Cc: <[email protected]> Signed-off-by: Vlastimil Babka <[email protected]>
1 parent 40384c8 commit b7ffecb

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

mm/slub.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,9 +2189,24 @@ bool memcg_slab_post_charge(void *p, gfp_t flags)
21892189

21902190
folio = virt_to_folio(p);
21912191
if (!folio_test_slab(folio)) {
2192-
return folio_memcg_kmem(folio) ||
2193-
(__memcg_kmem_charge_page(folio_page(folio, 0), flags,
2194-
folio_order(folio)) == 0);
2192+
int size;
2193+
2194+
if (folio_memcg_kmem(folio))
2195+
return true;
2196+
2197+
if (__memcg_kmem_charge_page(folio_page(folio, 0), flags,
2198+
folio_order(folio)))
2199+
return false;
2200+
2201+
/*
2202+
* This folio has already been accounted in the global stats but
2203+
* not in the memcg stats. So, subtract from the global and use
2204+
* the interface which adds to both global and memcg stats.
2205+
*/
2206+
size = folio_size(folio);
2207+
node_stat_mod_folio(folio, NR_SLAB_UNRECLAIMABLE_B, -size);
2208+
lruvec_stat_mod_folio(folio, NR_SLAB_UNRECLAIMABLE_B, size);
2209+
return true;
21952210
}
21962211

21972212
slab = folio_slab(folio);

0 commit comments

Comments
 (0)