Skip to content

Commit

Permalink
memcg: slub: fix SUnreclaim for post charged objects
Browse files Browse the repository at this point in the history
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 <shakeel.butt@linux.dev>
Cc: <stable@vger.kernel.org>
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
  • Loading branch information
Shakeel Butt authored and Vlastimil Babka committed Dec 10, 2024
1 parent 40384c8 commit b7ffecb
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions mm/slub.c
Original file line number Diff line number Diff line change
Expand Up @@ -2189,9 +2189,24 @@ bool memcg_slab_post_charge(void *p, gfp_t flags)

folio = virt_to_folio(p);
if (!folio_test_slab(folio)) {
return folio_memcg_kmem(folio) ||
(__memcg_kmem_charge_page(folio_page(folio, 0), flags,
folio_order(folio)) == 0);
int size;

if (folio_memcg_kmem(folio))
return true;

if (__memcg_kmem_charge_page(folio_page(folio, 0), flags,
folio_order(folio)))
return false;

/*
* This folio has already been accounted in the global stats but
* not in the memcg stats. So, subtract from the global and use
* the interface which adds to both global and memcg stats.
*/
size = folio_size(folio);
node_stat_mod_folio(folio, NR_SLAB_UNRECLAIMABLE_B, -size);
lruvec_stat_mod_folio(folio, NR_SLAB_UNRECLAIMABLE_B, size);
return true;
}

slab = folio_slab(folio);
Expand Down

0 comments on commit b7ffecb

Please sign in to comment.