Skip to content

Commit 2e9bd48

Browse files
rgushchintorvalds
authored andcommitted
mm: memcg/slab: pre-allocate obj_cgroups for slab caches with SLAB_ACCOUNT
In general it's unknown in advance if a slab page will contain accounted objects or not. In order to avoid memory waste, an obj_cgroup vector is allocated dynamically when a need to account of a new object arises. Such approach is memory efficient, but requires an expensive cmpxchg() to set up the memcg/objcgs pointer, because an allocation can race with a different allocation on another cpu. But in some common cases it's known for sure that a slab page will contain accounted objects: if the page belongs to a slab cache with a SLAB_ACCOUNT flag set. It includes such popular objects like vm_area_struct, anon_vma, task_struct, etc. In such cases we can pre-allocate the objcgs vector and simple assign it to the page without any atomic operations, because at this early stage the page is not visible to anyone else. A very simplistic benchmark (allocating 10000000 64-bytes objects in a row) shows ~15% win. In the real life it seems that most workloads are not very sensitive to the speed of (accounted) slab allocations. [guro@fb.com: open-code set_page_objcgs() and add some comments, by Johannes] Link: https://lkml.kernel.org/r/20201113001926.GA2934489@carbon.dhcp.thefacebook.com [akpm@linux-foundation.org: fix it for mm-slub-call-account_slab_page-after-slab-page-initialization-fix.patch] Link: https://lkml.kernel.org/r/20201110195753.530157-2-guro@fb.com Signed-off-by: Roman Gushchin <guro@fb.com> Acked-by: Johannes Weiner <hannes@cmpxchg.org> Reviewed-by: Shakeel Butt <shakeelb@google.com> Cc: Michal Hocko <mhocko@kernel.org> Cc: Christoph Lameter <cl@linux.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent cad8320 commit 2e9bd48

File tree

5 files changed

+31
-29
lines changed

5 files changed

+31
-29
lines changed

include/linux/memcontrol.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -475,19 +475,6 @@ static inline struct obj_cgroup **page_objcgs_check(struct page *page)
475475
return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
476476
}
477477

478-
/*
479-
* set_page_objcgs - associate a page with a object cgroups vector
480-
* @page: a pointer to the page struct
481-
* @objcgs: a pointer to the object cgroups vector
482-
*
483-
* Atomically associates a page with a vector of object cgroups.
484-
*/
485-
static inline bool set_page_objcgs(struct page *page,
486-
struct obj_cgroup **objcgs)
487-
{
488-
return !cmpxchg(&page->memcg_data, 0, (unsigned long)objcgs |
489-
MEMCG_DATA_OBJCGS);
490-
}
491478
#else
492479
static inline struct obj_cgroup **page_objcgs(struct page *page)
493480
{
@@ -498,12 +485,6 @@ static inline struct obj_cgroup **page_objcgs_check(struct page *page)
498485
{
499486
return NULL;
500487
}
501-
502-
static inline bool set_page_objcgs(struct page *page,
503-
struct obj_cgroup **objcgs)
504-
{
505-
return true;
506-
}
507488
#endif
508489

509490
static __always_inline bool memcg_stat_item_in_bytes(int idx)

mm/memcontrol.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2935,21 +2935,36 @@ static void commit_charge(struct page *page, struct mem_cgroup *memcg)
29352935

29362936
#ifdef CONFIG_MEMCG_KMEM
29372937
int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s,
2938-
gfp_t gfp)
2938+
gfp_t gfp, bool new_page)
29392939
{
29402940
unsigned int objects = objs_per_slab_page(s, page);
2941+
unsigned long memcg_data;
29412942
void *vec;
29422943

29432944
vec = kcalloc_node(objects, sizeof(struct obj_cgroup *), gfp,
29442945
page_to_nid(page));
29452946
if (!vec)
29462947
return -ENOMEM;
29472948

2948-
if (!set_page_objcgs(page, vec))
2949+
memcg_data = (unsigned long) vec | MEMCG_DATA_OBJCGS;
2950+
if (new_page) {
2951+
/*
2952+
* If the slab page is brand new and nobody can yet access
2953+
* it's memcg_data, no synchronization is required and
2954+
* memcg_data can be simply assigned.
2955+
*/
2956+
page->memcg_data = memcg_data;
2957+
} else if (cmpxchg(&page->memcg_data, 0, memcg_data)) {
2958+
/*
2959+
* If the slab page is already in use, somebody can allocate
2960+
* and assign obj_cgroups in parallel. In this case the existing
2961+
* objcg vector should be reused.
2962+
*/
29492963
kfree(vec);
2950-
else
2951-
kmemleak_not_leak(vec);
2964+
return 0;
2965+
}
29522966

2967+
kmemleak_not_leak(vec);
29532968
return 0;
29542969
}
29552970

mm/slab.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,7 @@ static struct page *kmem_getpages(struct kmem_cache *cachep, gfp_t flags,
13791379
return NULL;
13801380
}
13811381

1382-
account_slab_page(page, cachep->gfporder, cachep);
1382+
account_slab_page(page, cachep->gfporder, cachep, flags);
13831383
__SetPageSlab(page);
13841384
/* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
13851385
if (sk_memalloc_socks() && page_is_pfmemalloc(page))

mm/slab.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t fla
238238

239239
#ifdef CONFIG_MEMCG_KMEM
240240
int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s,
241-
gfp_t gfp);
241+
gfp_t gfp, bool new_page);
242242

243243
static inline void memcg_free_page_obj_cgroups(struct page *page)
244244
{
@@ -315,7 +315,8 @@ static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
315315
page = virt_to_head_page(p[i]);
316316

317317
if (!page_objcgs(page) &&
318-
memcg_alloc_page_obj_cgroups(page, s, flags)) {
318+
memcg_alloc_page_obj_cgroups(page, s, flags,
319+
false)) {
319320
obj_cgroup_uncharge(objcg, obj_full_size(s));
320321
continue;
321322
}
@@ -379,7 +380,8 @@ static inline struct mem_cgroup *memcg_from_slab_obj(void *ptr)
379380
}
380381

381382
static inline int memcg_alloc_page_obj_cgroups(struct page *page,
382-
struct kmem_cache *s, gfp_t gfp)
383+
struct kmem_cache *s, gfp_t gfp,
384+
bool new_page)
383385
{
384386
return 0;
385387
}
@@ -420,8 +422,12 @@ static inline struct kmem_cache *virt_to_cache(const void *obj)
420422
}
421423

422424
static __always_inline void account_slab_page(struct page *page, int order,
423-
struct kmem_cache *s)
425+
struct kmem_cache *s,
426+
gfp_t gfp)
424427
{
428+
if (memcg_kmem_enabled() && (s->flags & SLAB_ACCOUNT))
429+
memcg_alloc_page_obj_cgroups(page, s, gfp, true);
430+
425431
mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s),
426432
PAGE_SIZE << order);
427433
}

mm/slub.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1785,7 +1785,7 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
17851785

17861786
page->objects = oo_objects(oo);
17871787

1788-
account_slab_page(page, oo_order(oo), s);
1788+
account_slab_page(page, oo_order(oo), s, flags);
17891789

17901790
page->slab_cache = s;
17911791
__SetPageSlab(page);

0 commit comments

Comments
 (0)