Skip to content

Commit 90c7a79

Browse files
avagintorvalds
authored andcommitted
kmemcg: don't allocate extra memory for root memcg_cache_params
The memcg_cache_params structure contains the common part and the union, which represents two different types of data: one for root cashes and another for child caches. The size of child data is fixed. The size of the memcg_caches array is calculated in runtime. Currently the size of memcg_cache_params for root caches is calculated incorrectly, because it includes the size of parameters for child caches. ssize_t size = memcg_caches_array_size(num_groups); size *= sizeof(void *); size += sizeof(struct memcg_cache_params); v2: Fix a typo in calculations Signed-off-by: Andrey Vagin <[email protected]> Cc: Glauber Costa <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Balbir Singh <[email protected]> Cc: KAMEZAWA Hiroyuki <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent e76b63f commit 90c7a79

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

mm/memcontrol.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3121,7 +3121,7 @@ int memcg_update_cache_size(struct kmem_cache *s, int num_groups)
31213121
ssize_t size = memcg_caches_array_size(num_groups);
31223122

31233123
size *= sizeof(void *);
3124-
size += sizeof(struct memcg_cache_params);
3124+
size += offsetof(struct memcg_cache_params, memcg_caches);
31253125

31263126
s->memcg_params = kzalloc(size, GFP_KERNEL);
31273127
if (!s->memcg_params) {
@@ -3164,13 +3164,16 @@ int memcg_update_cache_size(struct kmem_cache *s, int num_groups)
31643164
int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
31653165
struct kmem_cache *root_cache)
31663166
{
3167-
size_t size = sizeof(struct memcg_cache_params);
3167+
size_t size;
31683168

31693169
if (!memcg_kmem_enabled())
31703170
return 0;
31713171

3172-
if (!memcg)
3172+
if (!memcg) {
3173+
size = offsetof(struct memcg_cache_params, memcg_caches);
31733174
size += memcg_limited_groups_array_size * sizeof(void *);
3175+
} else
3176+
size = sizeof(struct memcg_cache_params);
31743177

31753178
s->memcg_params = kzalloc(size, GFP_KERNEL);
31763179
if (!s->memcg_params)

0 commit comments

Comments
 (0)