From: Catalin Marinas <catalin.marinas@arm.com>
To: linux-kernel@vger.kernel.org
Cc: Pekka Enberg <penberg@cs.helsinki.fi>
Subject: [PATCH 03/14] kmemleak: Add the slab memory allocation/freeing hooks
Date: Fri, 24 Apr 2009 17:41:15 +0100 [thread overview]
Message-ID: <20090424164115.4476.39183.stgit@pc1117.cambridge.arm.com> (raw)
In-Reply-To: <20090424163500.4476.62146.stgit@pc1117.cambridge.arm.com>
This patch adds the callbacks to kmemleak_(alloc|free) functions from
the slab allocator. The patch also adds the SLAB_NOLEAKTRACE flag to
avoid recursive calls to kmemleak when it allocates its own data
structures.
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
Reviewed-by: Pekka Enberg <penberg@cs.helsinki.fi>
---
include/linux/slab.h | 2 ++
mm/slab.c | 32 ++++++++++++++++++++++++++++++--
2 files changed, 32 insertions(+), 2 deletions(-)
diff --git a/include/linux/slab.h b/include/linux/slab.h
index 24c5602..4880306 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -62,6 +62,8 @@
# define SLAB_DEBUG_OBJECTS 0x00000000UL
#endif
+#define SLAB_NOLEAKTRACE 0x00800000UL /* Avoid kmemleak tracing */
+
/* The following flags affect the page allocator grouping pages by mobility */
#define SLAB_RECLAIM_ACCOUNT 0x00020000UL /* Objects are reclaimable */
#define SLAB_TEMPORARY SLAB_RECLAIM_ACCOUNT /* Objects are short-lived */
diff --git a/mm/slab.c b/mm/slab.c
index 9a90b00..d51454e 100644
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -107,6 +107,7 @@
#include <linux/string.h>
#include <linux/uaccess.h>
#include <linux/nodemask.h>
+#include <linux/kmemleak.h>
#include <linux/mempolicy.h>
#include <linux/mutex.h>
#include <linux/fault-inject.h>
@@ -178,13 +179,13 @@
SLAB_STORE_USER | \
SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD | \
- SLAB_DEBUG_OBJECTS)
+ SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE)
#else
# define CREATE_MASK (SLAB_HWCACHE_ALIGN | \
SLAB_CACHE_DMA | \
SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD | \
- SLAB_DEBUG_OBJECTS)
+ SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE)
#endif
/*
@@ -964,6 +965,14 @@ static struct array_cache *alloc_arraycache(int node, int entries,
struct array_cache *nc = NULL;
nc = kmalloc_node(memsize, GFP_KERNEL, node);
+ /*
+ * The array_cache structures contain pointers to free object.
+ * However, when such objects are allocated or transfered to another
+ * cache the pointers are not cleared and they could be counted as
+ * valid references during a kmemleak scan. Therefore, kmemleak must
+ * not scan such objects.
+ */
+ kmemleak_no_scan(nc);
if (nc) {
nc->avail = 0;
nc->limit = entries;
@@ -2621,6 +2630,14 @@ static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
/* Slab management obj is off-slab. */
slabp = kmem_cache_alloc_node(cachep->slabp_cache,
local_flags, nodeid);
+ /*
+ * If the first object in the slab is leaked (it's allocated
+ * but no one has a reference to it), we want to make sure
+ * kmemleak does not treat the ->s_mem pointer as a reference
+ * to the object. Otherwise we will not report the leak.
+ */
+ kmemleak_scan_area(slabp, offsetof(struct slab, list),
+ sizeof(struct list_head), local_flags);
if (!slabp)
return NULL;
} else {
@@ -3141,6 +3158,12 @@ static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
STATS_INC_ALLOCMISS(cachep);
objp = cache_alloc_refill(cachep, flags);
}
+ /*
+ * To avoid a false negative, if an object that is in one of the
+ * per-CPU caches is leaked, we need to make sure kmemleak doesn't
+ * treat the array pointers as a reference to the object.
+ */
+ kmemleak_erase(&ac->entry[ac->avail]);
return objp;
}
@@ -3360,6 +3383,8 @@ __cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
out:
local_irq_restore(save_flags);
ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
+ kmemleak_alloc_recursive(ptr, obj_size(cachep), 1, cachep->flags,
+ flags);
if (unlikely((flags & __GFP_ZERO) && ptr))
memset(ptr, 0, obj_size(cachep));
@@ -3415,6 +3440,8 @@ __cache_alloc(struct kmem_cache *cachep, gfp_t flags, void *caller)
objp = __do_cache_alloc(cachep, flags);
local_irq_restore(save_flags);
objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
+ kmemleak_alloc_recursive(objp, obj_size(cachep), 1, cachep->flags,
+ flags);
prefetchw(objp);
if (unlikely((flags & __GFP_ZERO) && objp))
@@ -3530,6 +3557,7 @@ static inline void __cache_free(struct kmem_cache *cachep, void *objp)
struct array_cache *ac = cpu_cache_get(cachep);
check_irq_off();
+ kmemleak_free_recursive(objp, cachep->flags);
objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));
/*
next prev parent reply other threads:[~2009-04-24 16:42 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-04-24 16:40 [PATCH 00/14] Kernel memory leak detector Catalin Marinas
2009-04-24 16:41 ` [PATCH 01/14] kmemleak: Add the base support Catalin Marinas
2009-04-24 16:41 ` [PATCH 02/14] kmemleak: Add documentation on the memory leak detector Catalin Marinas
2009-04-24 16:41 ` Catalin Marinas [this message]
2009-04-24 16:41 ` [PATCH 04/14] kmemleak: Add the slob memory allocation/freeing hooks Catalin Marinas
2009-04-24 16:41 ` [PATCH 05/14] kmemleak: Add the slub " Catalin Marinas
2009-04-24 16:41 ` [PATCH 06/14] kmemleak: Add the vmalloc " Catalin Marinas
2009-04-24 16:41 ` [PATCH 07/14] kmemleak: Add kmemleak_alloc callback from alloc_large_system_hash Catalin Marinas
2009-04-24 16:41 ` [PATCH 08/14] kmemleak: Add modules support Catalin Marinas
2009-04-24 16:41 ` [PATCH 09/14] x86: Provide _sdata in the vmlinux_*.lds.S files Catalin Marinas
2009-04-26 11:13 ` Ingo Molnar
2009-04-24 16:41 ` [PATCH 10/14] arm: Provide _sdata and __bss_stop in the vmlinux.lds.S file Catalin Marinas
2009-04-24 16:42 ` [PATCH 11/14] kmemleak: Remove some of the kmemleak false positives Catalin Marinas
2009-04-24 16:42 ` [PATCH 12/14] kmemleak: Enable the building of the memory leak detector Catalin Marinas
2009-04-24 23:02 ` David Rientjes
2009-04-27 11:19 ` Catalin Marinas
2009-04-24 16:42 ` [PATCH 13/14] kmemleak: Simple testing module for kmemleak Catalin Marinas
2009-04-24 16:42 ` [PATCH 14/14] kmemleak: Add the corresponding MAINTAINERS entry Catalin Marinas
-- strict thread matches above, loose matches on Subject: below --
2008-12-19 18:12 [PATCH 00/14] Kernel memory leak detector Catalin Marinas
2008-12-19 18:13 ` [PATCH 03/14] kmemleak: Add the slab memory allocation/freeing hooks Catalin Marinas
2008-12-19 20:05 ` Pekka Enberg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20090424164115.4476.39183.stgit@pc1117.cambridge.arm.com \
--to=catalin.marinas@arm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=penberg@cs.helsinki.fi \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox