[PATCH 9/9] mm: kmemleak: fix undetected leaks for page aligned objects
From: George Prekas
Date: Mon Jan 23 2023 - 12:05:48 EST
If kmalloc returns a page aligned object, then the object has 2
references: the pointer returned by kmalloc and page->s_mem of the first
page of the object. Account for this extra reference, so that kmemleak
can correctly detect leaks for page aligned objects.
Signed-off-by: George Prekas <george@xxxxxxxxxxxxx>
---
mm/slab.h | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/mm/slab.h b/mm/slab.h
index 7cc432969945..76341c7c048e 100644
--- a/mm/slab.h
+++ b/mm/slab.h
@@ -734,6 +734,7 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s,
unsigned int orig_size)
{
unsigned int zero_size = s->object_size;
+ int min_count;
size_t i;
flags &= gfp_allowed_mask;
@@ -761,7 +762,11 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s,
p[i] = kasan_slab_alloc(s, p[i], flags, init);
if (p[i] && init && !kasan_has_integrated_init())
memset(p[i], 0, zero_size);
- kmemleak_alloc_recursive(p[i], s->object_size, 1,
+ min_count = 1;
+ /* If p[i] is page aligned, then a page->s_mem refers to it. */
+ if (((uintptr_t)p[i] & ~PAGE_MASK) == 0)
+ min_count++;
+ kmemleak_alloc_recursive(p[i], s->object_size, min_count,
s->flags, flags);
kmsan_slab_alloc(s, p[i], flags);
}
--
2.37.1