Skip to content

Commit 10befea

Browse files
rgushchintorvalds
authored andcommitted
mm: memcg/slab: use a single set of kmem_caches for all allocations
Instead of having two sets of kmem_caches: one for system-wide and non-accounted allocations and the second one shared by all accounted allocations, we can use just one. The idea is simple: space for obj_cgroup metadata can be allocated on demand and filled only for accounted allocations. It allows to remove a bunch of code which is required to handle kmem_cache clones for accounted allocations. There is no more need to create them, accumulate statistics, propagate attributes, etc. It's a quite significant simplification. Also, because the total number of slab_caches is reduced almost twice (not all kmem_caches have a memcg clone), some additional memory savings are expected. On my devvm it additionally saves about 3.5% of slab memory. [guro@fb.com: fix build on MIPS] Link: http://lkml.kernel.org/r/20200717214810.3733082-1-guro@fb.com Suggested-by: Johannes Weiner <hannes@cmpxchg.org> Signed-off-by: Roman Gushchin <guro@fb.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Reviewed-by: Vlastimil Babka <vbabka@suse.cz> Reviewed-by: Shakeel Butt <shakeelb@google.com> Cc: Christoph Lameter <cl@linux.com> Cc: Michal Hocko <mhocko@kernel.org> Cc: Tejun Heo <tj@kernel.org> Cc: Naresh Kamboju <naresh.kamboju@linaro.org> Link: http://lkml.kernel.org/r/20200623174037.3951353-18-guro@fb.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 15999ee commit 10befea

File tree

8 files changed

+78
-590
lines changed

8 files changed

+78
-590
lines changed

include/linux/slab.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,6 @@ struct kmem_cache *kmem_cache_create_usercopy(const char *name,
155155
void kmem_cache_destroy(struct kmem_cache *);
156156
int kmem_cache_shrink(struct kmem_cache *);
157157

158-
void memcg_create_kmem_cache(struct kmem_cache *cachep);
159-
160158
/*
161159
* Please use this macro to create slab caches. Simply specify the
162160
* name of the structure and maybe some flags that are listed above.

include/linux/slab_def.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ struct kmem_cache {
7272
int obj_offset;
7373
#endif /* CONFIG_DEBUG_SLAB */
7474

75-
#ifdef CONFIG_MEMCG
76-
struct memcg_cache_params memcg_params;
77-
#endif
7875
#ifdef CONFIG_KASAN
7976
struct kasan_cache kasan_info;
8077
#endif

include/linux/slub_def.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,7 @@ struct kmem_cache {
108108
struct list_head list; /* List of slab caches */
109109
#ifdef CONFIG_SYSFS
110110
struct kobject kobj; /* For sysfs */
111-
struct work_struct kobj_remove_work;
112111
#endif
113-
#ifdef CONFIG_MEMCG
114-
struct memcg_cache_params memcg_params;
115-
/* For propagation, maximum size of a stored attr */
116-
unsigned int max_attr_size;
117-
#ifdef CONFIG_SYSFS
118-
struct kset *memcg_kset;
119-
#endif
120-
#endif
121-
122112
#ifdef CONFIG_SLAB_FREELIST_HARDENED
123113
unsigned long random;
124114
#endif

mm/memcontrol.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2800,6 +2800,26 @@ static void commit_charge(struct page *page, struct mem_cgroup *memcg)
28002800
}
28012801

28022802
#ifdef CONFIG_MEMCG_KMEM
2803+
int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s,
2804+
gfp_t gfp)
2805+
{
2806+
unsigned int objects = objs_per_slab_page(s, page);
2807+
void *vec;
2808+
2809+
vec = kcalloc_node(objects, sizeof(struct obj_cgroup *), gfp,
2810+
page_to_nid(page));
2811+
if (!vec)
2812+
return -ENOMEM;
2813+
2814+
if (cmpxchg(&page->obj_cgroups, NULL,
2815+
(struct obj_cgroup **) ((unsigned long)vec | 0x1UL)))
2816+
kfree(vec);
2817+
else
2818+
kmemleak_not_leak(vec);
2819+
2820+
return 0;
2821+
}
2822+
28032823
/*
28042824
* Returns a pointer to the memory cgroup to which the kernel object is charged.
28052825
*
@@ -2826,7 +2846,10 @@ struct mem_cgroup *mem_cgroup_from_obj(void *p)
28262846

28272847
off = obj_to_index(page->slab_cache, page, p);
28282848
objcg = page_obj_cgroups(page)[off];
2829-
return obj_cgroup_memcg(objcg);
2849+
if (objcg)
2850+
return obj_cgroup_memcg(objcg);
2851+
2852+
return NULL;
28302853
}
28312854

28322855
/* All other pages use page->mem_cgroup */

mm/slab.c

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

1382-
if (charge_slab_page(page, flags, cachep->gfporder, cachep)) {
1383-
__free_pages(page, cachep->gfporder);
1384-
return NULL;
1385-
}
1386-
1382+
charge_slab_page(page, flags, cachep->gfporder, cachep);
13871383
__SetPageSlab(page);
13881384
/* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
13891385
if (sk_memalloc_socks() && page_is_pfmemalloc(page))
@@ -3799,8 +3795,8 @@ static int setup_kmem_cache_nodes(struct kmem_cache *cachep, gfp_t gfp)
37993795
}
38003796

38013797
/* Always called with the slab_mutex held */
3802-
static int __do_tune_cpucache(struct kmem_cache *cachep, int limit,
3803-
int batchcount, int shared, gfp_t gfp)
3798+
static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
3799+
int batchcount, int shared, gfp_t gfp)
38043800
{
38053801
struct array_cache __percpu *cpu_cache, *prev;
38063802
int cpu;
@@ -3845,30 +3841,6 @@ static int __do_tune_cpucache(struct kmem_cache *cachep, int limit,
38453841
return setup_kmem_cache_nodes(cachep, gfp);
38463842
}
38473843

3848-
static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
3849-
int batchcount, int shared, gfp_t gfp)
3850-
{
3851-
int ret;
3852-
struct kmem_cache *c;
3853-
3854-
ret = __do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
3855-
3856-
if (slab_state < FULL)
3857-
return ret;
3858-
3859-
if ((ret < 0) || !is_root_cache(cachep))
3860-
return ret;
3861-
3862-
lockdep_assert_held(&slab_mutex);
3863-
c = memcg_cache(cachep);
3864-
if (c) {
3865-
/* return value determined by the root cache only */
3866-
__do_tune_cpucache(c, limit, batchcount, shared, gfp);
3867-
}
3868-
3869-
return ret;
3870-
}
3871-
38723844
/* Called with slab_mutex held always */
38733845
static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
38743846
{
@@ -3881,13 +3853,6 @@ static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
38813853
if (err)
38823854
goto end;
38833855

3884-
if (!is_root_cache(cachep)) {
3885-
struct kmem_cache *root = memcg_root_cache(cachep);
3886-
limit = root->limit;
3887-
shared = root->shared;
3888-
batchcount = root->batchcount;
3889-
}
3890-
38913856
if (limit && shared && batchcount)
38923857
goto skip_setup;
38933858
/*

0 commit comments

Comments
 (0)