Skip to contentSkip to conversation

mm/slub: Avoid recursive loop with kmemleak

PATCH Applied Kees Cook 5 participants 8 messages Apr 25, 2024 – Apr 26, 2024

Subsystems (3)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 mm/slub: Avoid recursive loop with kmemleak

The system will immediate fill up stack and crash when both CONFIG_DEBUG_KMEMLEAK and CONFIG_MEM_ALLOC_PROFILING are enabled. Avoid allocation tagging of kmemleak caches, otherwise recursive allocation tracking occurs.

Fixes 279bb991b4d9 ("mm/slab: add allocation accounting into slab allocation and free paths")

---
Cc: Suren Baghdasaryan <[email protected]>
Cc: Kent Overstreet <[email protected]>
Cc: Catalin Marinas <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Christoph Lameter <[email protected]>
Cc: Pekka Enberg <[email protected]>
Cc: David Rientjes <[email protected]>
Cc: Joonsoo Kim <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: Roman Gushchin <[email protected]>
Cc: Hyeonggon Yoo <[email protected]>
Cc: [email protected]

---
 mm/kmemleak.c | 4 ++--
 mm/slub.c     | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
mm/kmemleak.c +2 -2
index c55c2cbb6837..fdcf01f62202 100644
@@ -463,7 +463,7 @@ static struct kmemleak_object *mem_pool_alloc(gfp_t gfp)
 
 	/* try the slab allocator first */
 	if (object_cache) {
-		object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp));
+		object = kmem_cache_alloc_noprof(object_cache, gfp_kmemleak_mask(gfp));
 		if (object)
 			return object;
 	}
@@ -947,7 +947,7 @@ static void add_scan_area(unsigned long ptr, size_t size, gfp_t gfp)
 	untagged_objp = (unsigned long)kasan_reset_tag((void *)object->pointer);
 
 	if (scan_area_cache)
-		area = kmem_cache_alloc(scan_area_cache, gfp_kmemleak_mask(gfp));
+		area = kmem_cache_alloc_noprof(scan_area_cache, gfp_kmemleak_mask(gfp));
 
 	raw_spin_lock_irqsave(&object->lock, flags);
 	if (!area) {
mm/slub.c +1 -1
index a94a0507e19c..9ae032ed17ed 100644
@@ -2016,7 +2016,7 @@ prepare_slab_obj_exts_hook(struct kmem_cache *s, gfp_t flags, void *p)
 	if (!p)
 		return NULL;
 
-	if (s->flags & SLAB_NO_OBJ_EXT)
+	if (s->flags & (SLAB_NO_OBJ_EXT | SLAB_NOLEAKTRACE))
 		return NULL;
 
 	if (flags & __GFP_NO_OBJ_EXT)
Signature · 1 line
2.34.1
#1 lore raw
Quoted 30 lines · Kees Cook

The system will immediate fill up stack and crash when both CONFIG_DEBUG_KMEMLEAK and CONFIG_MEM_ALLOC_PROFILING are enabled. Avoid allocation tagging of kmemleak caches, otherwise recursive allocation tracking occurs.

Fixes 279bb991b4d9 ("mm/slab: add allocation accounting into slab allocation and free paths")

---
Cc: Suren Baghdasaryan <[email protected]>
Cc: Kent Overstreet <[email protected]>
Cc: Catalin Marinas <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Christoph Lameter <[email protected]>
Cc: Pekka Enberg <[email protected]>
Cc: David Rientjes <[email protected]>
Cc: Joonsoo Kim <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: Roman Gushchin <[email protected]>
Cc: Hyeonggon Yoo <[email protected]>
Cc: [email protected]

---
 mm/kmemleak.c | 4 ++--
 mm/slub.c     | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
mm/kmemleak.c +0 -0
index c55c2cbb6837..fdcf01f62202 100644

mm/kmemleak.c static struct kmemleak_object *mem_pool_alloc(gfp_t gfp) · line 466

Quoted 5 lines
mm/kmemleak.c +1 -1
@@ -463,7 +463,7 @@ static struct kmemleak_object *mem_pool_alloc(gfp_t gfp)
 
 	/* try the slab allocator first */
 	if (object_cache) {
-		object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp));
+		object = kmem_cache_alloc_noprof(object_cache, gfp_kmemleak_mask(gfp));

What do these get accounted to, or does this now pop a warning with CONFIG_MEM_ALLOC_PROFILING_DEBUG?

Quoted 28 lines
mm/kmemleak.c +1 -1
 		if (object)
 			return object;
 	}
@@ -947,7 +947,7 @@ static void add_scan_area(unsigned long ptr, size_t size, gfp_t gfp)
 	untagged_objp = (unsigned long)kasan_reset_tag((void *)object->pointer);
 
 	if (scan_area_cache)
-		area = kmem_cache_alloc(scan_area_cache, gfp_kmemleak_mask(gfp));
+		area = kmem_cache_alloc_noprof(scan_area_cache, gfp_kmemleak_mask(gfp));
 
 	raw_spin_lock_irqsave(&object->lock, flags);
 	if (!area) {
mm/slub.c +1 -1
index a94a0507e19c..9ae032ed17ed 100644
@@ -2016,7 +2016,7 @@ prepare_slab_obj_exts_hook(struct kmem_cache *s, gfp_t flags, void *p)
 	if (!p)
 		return NULL;
 
-	if (s->flags & SLAB_NO_OBJ_EXT)
+	if (s->flags & (SLAB_NO_OBJ_EXT | SLAB_NOLEAKTRACE))
 		return NULL;
 
 	if (flags & __GFP_NO_OBJ_EXT)
Signature · 1 line
2.34.1
#2 lore raw
Quoted 33 lines · Kent Overstreet
Quoted 31 lines · Kees Cook

The system will immediate fill up stack and crash when both CONFIG_DEBUG_KMEMLEAK and CONFIG_MEM_ALLOC_PROFILING are enabled. Avoid allocation tagging of kmemleak caches, otherwise recursive allocation tracking occurs.

Fixes 279bb991b4d9 ("mm/slab: add allocation accounting into slab allocation and free paths")

---
Cc: Suren Baghdasaryan <[email protected]>
Cc: Kent Overstreet <[email protected]>
Cc: Catalin Marinas <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Christoph Lameter <[email protected]>
Cc: Pekka Enberg <[email protected]>
Cc: David Rientjes <[email protected]>
Cc: Joonsoo Kim <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: Roman Gushchin <[email protected]>
Cc: Hyeonggon Yoo <[email protected]>
Cc: [email protected]

---
 mm/kmemleak.c | 4 ++--
 mm/slub.c     | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
mm/kmemleak.c +0 -0
index c55c2cbb6837..fdcf01f62202 100644
@@ -463,7 +463,7 @@ static struct kmemleak_object *mem_pool_alloc(gfp_t gfp)
 
Quoted 7 lines

mm/kmemleak.c static struct kmemleak_object *mem_pool_alloc(gfp_t gfp) · line 466

Quoted 4 lines
mm/kmemleak.c +1 -1
      /* try the slab allocator first */
      if (object_cache) {
-             object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp));
+             object = kmem_cache_alloc_noprof(object_cache, gfp_kmemleak_mask(gfp));

What do these get accounted to, or does this now pop a warning with CONFIG_MEM_ALLOC_PROFILING_DEBUG?

Thanks for the fix, Kees!
I'll look into this recursion more closely to see if there is a better way to break it. As a stopgap measure seems ok to me. I also think it's unlikely that one would use both tracking mechanisms on the same system.

Quoted 29 lines
Quoted 29 lines
mm/kmemleak.c +1 -1
              if (object)
                      return object;
      }
@@ -947,7 +947,7 @@ static void add_scan_area(unsigned long ptr, size_t size, gfp_t gfp)
      untagged_objp = (unsigned long)kasan_reset_tag((void *)object->pointer);
 
      if (scan_area_cache)
-             area = kmem_cache_alloc(scan_area_cache, gfp_kmemleak_mask(gfp));
+             area = kmem_cache_alloc_noprof(scan_area_cache, gfp_kmemleak_mask(gfp));
 
      raw_spin_lock_irqsave(&object->lock, flags);
      if (!area) {
mm/slub.c +1 -2
index a94a0507e19c..9ae032ed17ed 100644
@@ -2016,7 +2016,7 @@ prepare_slab_obj_exts_hook(struct kmem_cache *s, gfp_t flags, void *p)
      if (!p)
              return NULL;
 
-     if (s->flags & SLAB_NO_OBJ_EXT)
+     if (s->flags & (SLAB_NO_OBJ_EXT | SLAB_NOLEAKTRACE))
              return NULL;
 
      if (flags & __GFP_NO_OBJ_EXT)
--

2.34.1

maintainer
#3 lore raw
Quoted 12 lines · On Thu, 25 Apr 2024 14:30:55 -0700 Suren Baghdasaryan <[email protected]> wrote:
Quoted 11 lines

mm/kmemleak.c static struct kmemleak_object *mem_pool_alloc(gfp_t gfp) · line 466

Quoted 8 lines
mm/kmemleak.c +1 -1
@@ -463,7 +463,7 @@ static struct kmemleak_object *mem_pool_alloc(gfp_t gfp)
 
      /* try the slab allocator first */
      if (object_cache) {
-             object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp));
+             object = kmem_cache_alloc_noprof(object_cache, gfp_kmemleak_mask(gfp));

What do these get accounted to, or does this now pop a warning with CONFIG_MEM_ALLOC_PROFILING_DEBUG?

Quoted 5 lines

Thanks for the fix, Kees!
I'll look into this recursion more closely to see if there is a better way to break it. As a stopgap measure seems ok to me. I also think it's unlikely that one would use both tracking mechanisms on the same system.

I'd really like to start building mm-stable without having to route around memprofiling. How about I include Kees's patch in that for now?

#4 lore raw
Quoted 14 lines · Andrew Morton
Quoted 12 lines · On Thu, 25 Apr 2024 14:30:55 -0700 Suren Baghdasaryan <[email protected]> wrote:
Quoted 11 lines

mm/kmemleak.c static struct kmemleak_object *mem_pool_alloc(gfp_t gfp) · line 466

Quoted 8 lines
mm/kmemleak.c +1 -1
@@ -463,7 +463,7 @@ static struct kmemleak_object *mem_pool_alloc(gfp_t gfp)
 
      /* try the slab allocator first */
      if (object_cache) {
-             object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp));
+             object = kmem_cache_alloc_noprof(object_cache, gfp_kmemleak_mask(gfp));

What do these get accounted to, or does this now pop a warning with CONFIG_MEM_ALLOC_PROFILING_DEBUG?

Quoted 8 lines
Quoted 5 lines

Thanks for the fix, Kees!
I'll look into this recursion more closely to see if there is a better way to break it. As a stopgap measure seems ok to me. I also think it's unlikely that one would use both tracking mechanisms on the same system.

I'd really like to start building mm-stable without having to route around memprofiling. How about I include Kees's patch in that for now?

Agreed

#5 lore raw
Quoted 22 lines · Kent Overstreet
Quoted 20 lines · Andrew Morton
Quoted 17 lines · On Thu, 25 Apr 2024 14:30:55 -0700 Suren Baghdasaryan <[email protected]> wrote:
Quoted 11 lines

mm/kmemleak.c static struct kmemleak_object *mem_pool_alloc(gfp_t gfp) · line 466

Quoted 8 lines
mm/kmemleak.c +1 -1
@@ -463,7 +463,7 @@ static struct kmemleak_object *mem_pool_alloc(gfp_t gfp)
 
      /* try the slab allocator first */
      if (object_cache) {
-             object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp));
+             object = kmem_cache_alloc_noprof(object_cache, gfp_kmemleak_mask(gfp));

What do these get accounted to, or does this now pop a warning with CONFIG_MEM_ALLOC_PROFILING_DEBUG?

Thanks for the fix, Kees!
I'll look into this recursion more closely to see if there is a better way to break it. As a stopgap measure seems ok to me. I also think it's unlikely that one would use both tracking mechanisms on the same system.

Quoted 4 lines
Quoted 2 lines

I'd really like to start building mm-stable without having to route around memprofiling. How about I include Kees's patch in that for now?

Agreed

Yes, please. When I figure out a better way, I'll post a separate patch. Thanks!

maintainer Acked-by on v1 1/1
#6 lore raw
In reply to Kees Cook
Quoted 7 lines · Kees Cook

The system will immediate fill up stack and crash when both CONFIG_DEBUG_KMEMLEAK and CONFIG_MEM_ALLOC_PROFILING are enabled. Avoid allocation tagging of kmemleak caches, otherwise recursive allocation tracking occurs.

Fixes 279bb991b4d9 ("mm/slab: add allocation accounting into slab allocation and free paths")

For the kmemleak bits:

author
#7 lore raw
Quoted 2 lines · Catalin Marinas
Quoted 1 line · Kees Cook

The system will immediate fill up stack and crash when both

Oops, typo from me: "immediately". You'd never guess I'm a native English speaker! :)

Quoted 10 lines
Quoted 6 lines

CONFIG_DEBUG_KMEMLEAK and CONFIG_MEM_ALLOC_PROFILING are enabled. Avoid allocation tagging of kmemleak caches, otherwise recursive allocation tracking occurs.

Fixes 279bb991b4d9 ("mm/slab: add allocation accounting into slab allocation and free paths")

For the kmemleak bits:

Thanks!

-Kees

Signature · 1 line
Kees Cook