Skip to content

Commit d1b2cf6

Browse files
Bharata B Raotorvalds
authored andcommitted
mm: memcg/slab: uncharge during kmem_cache_free_bulk()
Object cgroup charging is done for all the objects during allocation, but during freeing, uncharging ends up happening for only one object in the case of bulk allocation/freeing. Fix this by having a separate call to uncharge all the objects from kmem_cache_free_bulk() and by modifying memcg_slab_free_hook() to take care of bulk uncharging. Fixes: 964d4bd ("mm: memcg/slab: save obj_cgroup for non-root slab objects" Signed-off-by: Bharata B Rao <bharata@linux.ibm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Acked-by: Roman Gushchin <guro@fb.com> Cc: Christoph Lameter <cl@linux.com> Cc: David Rientjes <rientjes@google.com> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Shakeel Butt <shakeelb@google.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Michal Hocko <mhocko@kernel.org> Cc: Tejun Heo <tj@kernel.org> Cc: <stable@vger.kernel.org> Link: https://lkml.kernel.org/r/20201009060423.390479-1-bharata@linux.ibm.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 7a52d4d commit d1b2cf6

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

mm/slab.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3438,7 +3438,7 @@ void ___cache_free(struct kmem_cache *cachep, void *objp,
34383438
memset(objp, 0, cachep->object_size);
34393439
kmemleak_free_recursive(objp, cachep->flags);
34403440
objp = cache_free_debugcheck(cachep, objp, caller);
3441-
memcg_slab_free_hook(cachep, virt_to_head_page(objp), objp);
3441+
memcg_slab_free_hook(cachep, &objp, 1);
34423442

34433443
/*
34443444
* Skip calling cache_free_alien() when the platform is not numa.

mm/slab.h

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -345,30 +345,42 @@ static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
345345
obj_cgroup_put(objcg);
346346
}
347347

348-
static inline void memcg_slab_free_hook(struct kmem_cache *s, struct page *page,
349-
void *p)
348+
static inline void memcg_slab_free_hook(struct kmem_cache *s_orig,
349+
void **p, int objects)
350350
{
351+
struct kmem_cache *s;
351352
struct obj_cgroup *objcg;
353+
struct page *page;
352354
unsigned int off;
355+
int i;
353356

354357
if (!memcg_kmem_enabled())
355358
return;
356359

357-
if (!page_has_obj_cgroups(page))
358-
return;
360+
for (i = 0; i < objects; i++) {
361+
if (unlikely(!p[i]))
362+
continue;
359363

360-
off = obj_to_index(s, page, p);
361-
objcg = page_obj_cgroups(page)[off];
362-
page_obj_cgroups(page)[off] = NULL;
364+
page = virt_to_head_page(p[i]);
365+
if (!page_has_obj_cgroups(page))
366+
continue;
363367

364-
if (!objcg)
365-
return;
368+
if (!s_orig)
369+
s = page->slab_cache;
370+
else
371+
s = s_orig;
366372

367-
obj_cgroup_uncharge(objcg, obj_full_size(s));
368-
mod_objcg_state(objcg, page_pgdat(page), cache_vmstat_idx(s),
369-
-obj_full_size(s));
373+
off = obj_to_index(s, page, p[i]);
374+
objcg = page_obj_cgroups(page)[off];
375+
if (!objcg)
376+
continue;
370377

371-
obj_cgroup_put(objcg);
378+
page_obj_cgroups(page)[off] = NULL;
379+
obj_cgroup_uncharge(objcg, obj_full_size(s));
380+
mod_objcg_state(objcg, page_pgdat(page), cache_vmstat_idx(s),
381+
-obj_full_size(s));
382+
obj_cgroup_put(objcg);
383+
}
372384
}
373385

374386
#else /* CONFIG_MEMCG_KMEM */
@@ -406,8 +418,8 @@ static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
406418
{
407419
}
408420

409-
static inline void memcg_slab_free_hook(struct kmem_cache *s, struct page *page,
410-
void *p)
421+
static inline void memcg_slab_free_hook(struct kmem_cache *s,
422+
void **p, int objects)
411423
{
412424
}
413425
#endif /* CONFIG_MEMCG_KMEM */

mm/slub.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3095,7 +3095,7 @@ static __always_inline void do_slab_free(struct kmem_cache *s,
30953095
struct kmem_cache_cpu *c;
30963096
unsigned long tid;
30973097

3098-
memcg_slab_free_hook(s, page, head);
3098+
memcg_slab_free_hook(s, &head, 1);
30993099
redo:
31003100
/*
31013101
* Determine the currently cpus per cpu slab.
@@ -3257,6 +3257,7 @@ void kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p)
32573257
if (WARN_ON(!size))
32583258
return;
32593259

3260+
memcg_slab_free_hook(s, p, size);
32603261
do {
32613262
struct detached_freelist df;
32623263

0 commit comments

Comments
 (0)