Skip to content

Commit 494c1df

Browse files
Waiman-Longtorvalds
authored andcommitted
mm: memcg/slab: create a new set of kmalloc-cg-<n> caches
There are currently two problems in the way the objcg pointer array (memcg_data) in the page structure is being allocated and freed. On its allocation, it is possible that the allocated objcg pointer array comes from the same slab that requires memory accounting. If this happens, the slab will never become empty again as there is at least one object left (the obj_cgroup array) in the slab. When it is freed, the objcg pointer array object may be the last one in its slab and hence causes kfree() to be called again. With the right workload, the slab cache may be set up in a way that allows the recursive kfree() calling loop to nest deep enough to cause a kernel stack overflow and panic the system. One way to solve this problem is to split the kmalloc-<n> caches (KMALLOC_NORMAL) into two separate sets - a new set of kmalloc-<n> (KMALLOC_NORMAL) caches for unaccounted objects only and a new set of kmalloc-cg-<n> (KMALLOC_CGROUP) caches for accounted objects only. All the other caches can still allow a mix of accounted and unaccounted objects. With this change, all the objcg pointer array objects will come from KMALLOC_NORMAL caches which won't have their objcg pointer arrays. So both the recursive kfree() problem and non-freeable slab problem are gone. Since both the KMALLOC_NORMAL and KMALLOC_CGROUP caches no longer have mixed accounted and unaccounted objects, this will slightly reduce the number of objcg pointer arrays that need to be allocated and save a bit of memory. On the other hand, creating a new set of kmalloc caches does have the effect of reducing cache utilization. So it is properly a wash. The new KMALLOC_CGROUP is added between KMALLOC_NORMAL and KMALLOC_RECLAIM so that the first for loop in create_kmalloc_caches() will include the newly added caches without change. [vbabka@suse.cz: don't create kmalloc-cg caches with cgroup.memory=nokmem] Link: https://lkml.kernel.org/r/20210512145107.6208-1-longman@redhat.com [akpm@linux-foundation.org: un-fat-finger v5 delta creation] [longman@redhat.com: disable cache merging for KMALLOC_NORMAL caches] Link: https://lkml.kernel.org/r/20210505200610.13943-4-longman@redhat.com Link: https://lkml.kernel.org/r/20210512145107.6208-1-longman@redhat.com Link: https://lkml.kernel.org/r/20210505200610.13943-3-longman@redhat.com Signed-off-by: Waiman Long <longman@redhat.com> Signed-off-by: Vlastimil Babka <vbabka@suse.cz> Suggested-by: Vlastimil Babka <vbabka@suse.cz> Reviewed-by: Shakeel Butt <shakeelb@google.com> Acked-by: Roman Gushchin <guro@fb.com> Cc: Christoph Lameter <cl@linux.com> Cc: David Rientjes <rientjes@google.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> Cc: Michal Hocko <mhocko@kernel.org> Cc: Pekka Enberg <penberg@kernel.org> Cc: Vladimir Davydov <vdavydov.dev@gmail.com> [longman@redhat.com: fix for CONFIG_ZONE_DMA=n] Suggested-by: Roman Gushchin <guro@fb.com> Reviewed-by: Vlastimil Babka <vbabka@suse.cz> Cc: Vladimir Davydov <vdavydov.dev@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 41eb5df commit 494c1df

File tree

4 files changed

+62
-19
lines changed

4 files changed

+62
-19
lines changed

include/linux/slab.h

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,21 @@ static inline void __check_heap_object(const void *ptr, unsigned long n,
305305
/*
306306
* Whenever changing this, take care of that kmalloc_type() and
307307
* create_kmalloc_caches() still work as intended.
308+
*
309+
* KMALLOC_NORMAL can contain only unaccounted objects whereas KMALLOC_CGROUP
310+
* is for accounted but unreclaimable and non-dma objects. All the other
311+
* kmem caches can have both accounted and unaccounted objects.
308312
*/
309313
enum kmalloc_cache_type {
310314
KMALLOC_NORMAL = 0,
315+
#ifndef CONFIG_ZONE_DMA
316+
KMALLOC_DMA = KMALLOC_NORMAL,
317+
#endif
318+
#ifndef CONFIG_MEMCG_KMEM
319+
KMALLOC_CGROUP = KMALLOC_NORMAL,
320+
#else
321+
KMALLOC_CGROUP,
322+
#endif
311323
KMALLOC_RECLAIM,
312324
#ifdef CONFIG_ZONE_DMA
313325
KMALLOC_DMA,
@@ -319,24 +331,36 @@ enum kmalloc_cache_type {
319331
extern struct kmem_cache *
320332
kmalloc_caches[NR_KMALLOC_TYPES][KMALLOC_SHIFT_HIGH + 1];
321333

334+
/*
335+
* Define gfp bits that should not be set for KMALLOC_NORMAL.
336+
*/
337+
#define KMALLOC_NOT_NORMAL_BITS \
338+
(__GFP_RECLAIMABLE | \
339+
(IS_ENABLED(CONFIG_ZONE_DMA) ? __GFP_DMA : 0) | \
340+
(IS_ENABLED(CONFIG_MEMCG_KMEM) ? __GFP_ACCOUNT : 0))
341+
322342
static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
323343
{
324-
#ifdef CONFIG_ZONE_DMA
325344
/*
326345
* The most common case is KMALLOC_NORMAL, so test for it
327-
* with a single branch for both flags.
346+
* with a single branch for all the relevant flags.
328347
*/
329-
if (likely((flags & (__GFP_DMA | __GFP_RECLAIMABLE)) == 0))
348+
if (likely((flags & KMALLOC_NOT_NORMAL_BITS) == 0))
330349
return KMALLOC_NORMAL;
331350

332351
/*
333-
* At least one of the flags has to be set. If both are, __GFP_DMA
334-
* is more important.
352+
* At least one of the flags has to be set. Their priorities in
353+
* decreasing order are:
354+
* 1) __GFP_DMA
355+
* 2) __GFP_RECLAIMABLE
356+
* 3) __GFP_ACCOUNT
335357
*/
336-
return flags & __GFP_DMA ? KMALLOC_DMA : KMALLOC_RECLAIM;
337-
#else
338-
return flags & __GFP_RECLAIMABLE ? KMALLOC_RECLAIM : KMALLOC_NORMAL;
339-
#endif
358+
if (IS_ENABLED(CONFIG_ZONE_DMA) && (flags & __GFP_DMA))
359+
return KMALLOC_DMA;
360+
if (!IS_ENABLED(CONFIG_MEMCG_KMEM) || (flags & __GFP_RECLAIMABLE))
361+
return KMALLOC_RECLAIM;
362+
else
363+
return KMALLOC_CGROUP;
340364
}
341365

342366
/*

mm/internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ extern void putback_lru_page(struct page *page);
115115
*/
116116
extern pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address);
117117

118+
/*
119+
* in mm/memcontrol.c:
120+
*/
121+
extern bool cgroup_memory_nokmem;
122+
118123
/*
119124
* in mm/page_alloc.c
120125
*/

mm/memcontrol.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ DEFINE_PER_CPU(struct mem_cgroup *, int_active_memcg);
8383
static bool cgroup_memory_nosocket;
8484

8585
/* Kernel memory accounting disabled? */
86-
static bool cgroup_memory_nokmem;
86+
bool cgroup_memory_nokmem;
8787

8888
/* Whether the swap controller is active */
8989
#ifdef CONFIG_MEMCG_SWAP

mm/slab_common.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -738,21 +738,25 @@ struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
738738
}
739739

740740
#ifdef CONFIG_ZONE_DMA
741-
#define INIT_KMALLOC_INFO(__size, __short_size) \
742-
{ \
743-
.name[KMALLOC_NORMAL] = "kmalloc-" #__short_size, \
744-
.name[KMALLOC_RECLAIM] = "kmalloc-rcl-" #__short_size, \
745-
.name[KMALLOC_DMA] = "dma-kmalloc-" #__short_size, \
746-
.size = __size, \
747-
}
741+
#define KMALLOC_DMA_NAME(sz) .name[KMALLOC_DMA] = "dma-kmalloc-" #sz,
742+
#else
743+
#define KMALLOC_DMA_NAME(sz)
744+
#endif
745+
746+
#ifdef CONFIG_MEMCG_KMEM
747+
#define KMALLOC_CGROUP_NAME(sz) .name[KMALLOC_CGROUP] = "kmalloc-cg-" #sz,
748748
#else
749+
#define KMALLOC_CGROUP_NAME(sz)
750+
#endif
751+
749752
#define INIT_KMALLOC_INFO(__size, __short_size) \
750753
{ \
751754
.name[KMALLOC_NORMAL] = "kmalloc-" #__short_size, \
752755
.name[KMALLOC_RECLAIM] = "kmalloc-rcl-" #__short_size, \
756+
KMALLOC_CGROUP_NAME(__short_size) \
757+
KMALLOC_DMA_NAME(__short_size) \
753758
.size = __size, \
754759
}
755-
#endif
756760

757761
/*
758762
* kmalloc_info[] is to make slub_debug=,kmalloc-xx option work at boot time.
@@ -838,8 +842,15 @@ void __init setup_kmalloc_cache_index_table(void)
838842
static void __init
839843
new_kmalloc_cache(int idx, enum kmalloc_cache_type type, slab_flags_t flags)
840844
{
841-
if (type == KMALLOC_RECLAIM)
845+
if (type == KMALLOC_RECLAIM) {
842846
flags |= SLAB_RECLAIM_ACCOUNT;
847+
} else if (IS_ENABLED(CONFIG_MEMCG_KMEM) && (type == KMALLOC_CGROUP)) {
848+
if (cgroup_memory_nokmem) {
849+
kmalloc_caches[type][idx] = kmalloc_caches[KMALLOC_NORMAL][idx];
850+
return;
851+
}
852+
flags |= SLAB_ACCOUNT;
853+
}
843854

844855
kmalloc_caches[type][idx] = create_kmalloc_cache(
845856
kmalloc_info[idx].name[type],
@@ -857,6 +868,9 @@ void __init create_kmalloc_caches(slab_flags_t flags)
857868
int i;
858869
enum kmalloc_cache_type type;
859870

871+
/*
872+
* Including KMALLOC_CGROUP if CONFIG_MEMCG_KMEM defined
873+
*/
860874
for (type = KMALLOC_NORMAL; type <= KMALLOC_RECLAIM; type++) {
861875
for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
862876
if (!kmalloc_caches[type][i])

0 commit comments

Comments
 (0)