Skip to contentSkip to conversation

Tracing memory leaks (slabs) in 2.6.9+ kernels?

DISCUSSION Justin Schoeman 5 participants 10 messages Mar 2, 2005 – Mar 4, 2005 Memory management

lore
Share & tools
lore
#0 lore raw

Hi,

I am having a problem with memory leaking on a patched kernel. In order to pinpoint the leak, I would like to try to trace the allocation points for the memory.

I have found some vague references to patches that allow the user to dump the caller address for slab allocations, but I cannot find the patch itself.

Can anybody please point me in the right direction - either for that patch, or any other way to track down leaking slabs?

Thank you,
Justin Schoeman

#1 lore raw
Quoted 11 lines · Justin Schoeman

I am having a problem with memory leaking on a patched kernel. In order
to pinpoint the leak, I would like to try to trace the allocation points for the memory.

I have found some vague references to patches that allow the user to dump the caller address for slab allocations, but I cannot find the patch itself.

Can anybody please point me in the right direction - either for that patch, or any other way to track down leaking slabs?

From: Manfred Spraul <[email protected]>

With the patch applied,

	echo "size-4096 0 0 0" > /proc/slabinfo

walks the objects in the size-4096 slab, printing out the calling address of whoever allocated that object.

It is for leak detection.

---

 25-akpm/mm/slab.c |   40 ++++++++++++++++++++++++++++++++++++++--
 1 files changed, 38 insertions(+), 2 deletions(-)
mm/slab.c renamed from mm/slab.c~slab-leak-detector +38 -2
diff -puN mm/slab.c~slab-leak-detector mm/slab.c
@@ -2116,6 +2116,15 @@ cache_alloc_debugcheck_after(kmem_cache_
 		*dbg_redzone1(cachep, objp) = RED_ACTIVE;
 		*dbg_redzone2(cachep, objp) = RED_ACTIVE;
 	}
+	{
+		int objnr;
+		struct slab *slabp;
+
+		slabp = GET_PAGE_SLAB(virt_to_page(objp));
+
+		objnr = (objp - slabp->s_mem) / cachep->objsize;
+		slab_bufctl(slabp)[objnr] = (unsigned long)caller;
+	}
 	objp += obj_dbghead(cachep);
 	if (cachep->ctor && cachep->flags & SLAB_POISON) {
 		unsigned long	ctor_flags = SLAB_CTOR_CONSTRUCTOR;
@@ -2179,12 +2188,14 @@ static void free_block(kmem_cache_t *cac
 		objnr = (objp - slabp->s_mem) / cachep->objsize;
 		check_slabp(cachep, slabp);
 #if DEBUG
+#if 0
 		if (slab_bufctl(slabp)[objnr] != BUFCTL_FREE) {
 			printk(KERN_ERR "slab: double free detected in cache '%s', objp %p.\n",
 						cachep->name, objp);
 			BUG();
 		}
 #endif
+#endif
 		slab_bufctl(slabp)[objnr] = slabp->free;
 		slabp->free = objnr;
 		STATS_DEC_ACTIVE(cachep);
@@ -2998,6 +3009,29 @@ struct seq_operations slabinfo_op = {
 	.show	= s_show,
 };
 
+static void do_dump_slabp(kmem_cache_t *cachep)
+{
+#if DEBUG
+	struct list_head *q;
+
+	check_irq_on();
+	spin_lock_irq(&cachep->spinlock);
+	list_for_each(q,&cachep->lists.slabs_full) {
+		struct slab *slabp;
+		int i;
+		slabp = list_entry(q, struct slab, list);
+		for (i = 0; i < cachep->num; i++) {
+			unsigned long sym = slab_bufctl(slabp)[i];
+
+			printk("obj %p/%d: %p", slabp, i, (void *)sym);
+			print_symbol(" <%s>", sym);
+			printk("\n");
+		}
+	}
+	spin_unlock_irq(&cachep->spinlock);
+#endif
+}
+
 #define MAX_SLABINFO_WRITE 128
 /**
  * slabinfo_write - Tuning for the slab allocator
@@ -3038,9 +3072,11 @@ ssize_t slabinfo_write(struct file *file
 			    batchcount < 1 ||
 			    batchcount > limit ||
 			    shared < 0) {
-				res = -EINVAL;
+				do_dump_slabp(cachep);
+				res = 0;
 			} else {
-				res = do_tune_cpucache(cachep, limit, batchcount, shared);
+				res = do_tune_cpucache(cachep, limit,
+							batchcount, shared);
 			}
 			break;
 		}

_

#2 lore raw

mm/slab.c cache_alloc_debugcheck_after(kmem_cache_ · line 2126

Quoted 1 line · Andrew Morton
mm/slab.c renamed from mm/slab.c~slab-leak-detector +1 -0
+		slab_bufctl(slabp)[objnr] = (unsigned long)caller;

Umm... this patch looks strange..

slab_bufctl() returns "kmem_bufctl_t *", but kmem_bufctl_t is "unsigned short".

I guess that this debug patch was broken by something else...

Signature · 1 line
OGAWA Hirofumi <[email protected]>
#3 lore raw
Quoted 9 lines · OGAWA Hirofumi

Review

Quoted 1 line · Andrew Morton
(quoted diff) +1 -0
+		slab_bufctl(slabp)[objnr] = (unsigned long)caller;

Umm... this patch looks strange..

slab_bufctl() returns "kmem_bufctl_t *", but kmem_bufctl_t is "unsigned short".

Good point. This seems to work.

From: Manfred Spraul <[email protected]>

With the patch applied,

	echo "size-4096 0 0 0" > /proc/slabinfo

walks the objects in the size-4096 slab, printing out the calling address of whoever allocated that object.

It is for leak detection.

---

 25-akpm/include/asm-alpha/types.h   |    2 -
 25-akpm/include/asm-arm/types.h     |    2 -
 25-akpm/include/asm-arm26/types.h   |    2 -
 25-akpm/include/asm-cris/types.h    |    2 -
 25-akpm/include/asm-frv/types.h     |    2 -
 25-akpm/include/asm-h8300/types.h   |    2 -
 25-akpm/include/asm-i386/types.h    |    2 -
 25-akpm/include/asm-ia64/types.h    |    2 -
 25-akpm/include/asm-m32r/types.h    |    2 -
 25-akpm/include/asm-m68k/types.h    |    2 -
 25-akpm/include/asm-mips/types.h    |    2 -
 25-akpm/include/asm-parisc/types.h  |    2 -
 25-akpm/include/asm-ppc/types.h     |    2 -
 25-akpm/include/asm-ppc64/types.h   |    2 -
 25-akpm/include/asm-s390/types.h    |    2 -
 25-akpm/include/asm-sh/types.h      |    2 -
 25-akpm/include/asm-sh64/types.h    |    2 -
 25-akpm/include/asm-sparc/types.h   |    2 -
 25-akpm/include/asm-sparc64/types.h |    2 -
 25-akpm/include/asm-v850/types.h    |    2 -
 25-akpm/include/asm-x86_64/types.h  |    2 -
 25-akpm/mm/slab.c                   |   40 ++++++++++++++++++++++++++++++++++--
 22 files changed, 59 insertions(+), 23 deletions(-)
include/asm-alpha/types.h renamed from include/asm-alpha/types.h~slab-leak-detector +1 -1
diff -puN include/asm-alpha/types.h~slab-leak-detector include/asm-alpha/types.h
@@ -56,7 +56,7 @@ typedef unsigned long u64;
 typedef u64 dma_addr_t;
 typedef u64 dma64_addr_t;
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 #endif /* __KERNEL__ */
include/asm-arm26/types.h renamed from include/asm-arm26/types.h~slab-leak-detector +1 -1
diff -puN include/asm-arm26/types.h~slab-leak-detector include/asm-arm26/types.h
@@ -52,7 +52,7 @@ typedef unsigned long long u64;
 typedef u32 dma_addr_t;
 typedef u32 dma64_addr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-arm/types.h renamed from include/asm-arm/types.h~slab-leak-detector +1 -1
diff -puN include/asm-arm/types.h~slab-leak-detector include/asm-arm/types.h
@@ -52,7 +52,7 @@ typedef unsigned long long u64;
 typedef u32 dma_addr_t;
 typedef u32 dma64_addr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-cris/types.h renamed from include/asm-cris/types.h~slab-leak-detector +1 -1
diff -puN include/asm-cris/types.h~slab-leak-detector include/asm-cris/types.h
@@ -52,7 +52,7 @@ typedef unsigned long long u64;
 typedef u32 dma_addr_t;
 typedef u32 dma64_addr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-frv/types.h renamed from include/asm-frv/types.h~slab-leak-detector +1 -1
diff -puN include/asm-frv/types.h~slab-leak-detector include/asm-frv/types.h
@@ -65,7 +65,7 @@ typedef u64 u_quad_t;
 
 typedef u32 dma_addr_t;
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-h8300/types.h renamed from include/asm-h8300/types.h~slab-leak-detector +1 -1
diff -puN include/asm-h8300/types.h~slab-leak-detector include/asm-h8300/types.h
@@ -58,7 +58,7 @@ typedef u32 dma_addr_t;
 #define HAVE_SECTOR_T
 typedef u64 sector_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __KERNEL__ */
 
include/asm-i386/types.h renamed from include/asm-i386/types.h~slab-leak-detector +1 -1
diff -puN include/asm-i386/types.h~slab-leak-detector include/asm-i386/types.h
@@ -63,7 +63,7 @@ typedef u64 sector_t;
 #define HAVE_SECTOR_T
 #endif
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-ia64/types.h renamed from include/asm-ia64/types.h~slab-leak-detector +1 -1
diff -puN include/asm-ia64/types.h~slab-leak-detector include/asm-ia64/types.h
@@ -67,7 +67,7 @@ typedef __u64 u64;
 
 typedef u64 dma_addr_t;
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 # endif /* __KERNEL__ */
 #endif /* !__ASSEMBLY__ */
include/asm-m32r/types.h renamed from include/asm-m32r/types.h~slab-leak-detector +1 -1
diff -puN include/asm-m32r/types.h~slab-leak-detector include/asm-m32r/types.h
@@ -55,7 +55,7 @@ typedef unsigned long long u64;
 typedef u32 dma_addr_t;
 typedef u64 dma64_addr_t;
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-m68k/types.h renamed from include/asm-m68k/types.h~slab-leak-detector +1 -1
diff -puN include/asm-m68k/types.h~slab-leak-detector include/asm-m68k/types.h
@@ -60,7 +60,7 @@ typedef unsigned long long u64;
 typedef u32 dma_addr_t;
 typedef u32 dma64_addr_t;
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-mips/types.h renamed from include/asm-mips/types.h~slab-leak-detector +1 -1
diff -puN include/asm-mips/types.h~slab-leak-detector include/asm-mips/types.h
@@ -99,7 +99,7 @@ typedef u64 sector_t;
 #define HAVE_SECTOR_T
 #endif
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-parisc/types.h renamed from include/asm-parisc/types.h~slab-leak-detector +1 -1
diff -puN include/asm-parisc/types.h~slab-leak-detector include/asm-parisc/types.h
@@ -56,7 +56,7 @@ typedef unsigned long long u64;
 typedef u32 dma_addr_t;
 typedef u64 dma64_addr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-ppc64/types.h renamed from include/asm-ppc64/types.h~slab-leak-detector +1 -1
diff -puN include/asm-ppc64/types.h~slab-leak-detector include/asm-ppc64/types.h
@@ -72,7 +72,7 @@ typedef struct {
 	unsigned long env;
 } func_descr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 #endif /* __ASSEMBLY__ */
 
 #endif /* __KERNEL__ */
include/asm-ppc/types.h renamed from include/asm-ppc/types.h~slab-leak-detector +1 -1
diff -puN include/asm-ppc/types.h~slab-leak-detector include/asm-ppc/types.h
@@ -62,7 +62,7 @@ typedef u64 sector_t;
 #define HAVE_SECTOR_T
 #endif
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-s390/types.h renamed from include/asm-s390/types.h~slab-leak-detector +1 -1
diff -puN include/asm-s390/types.h~slab-leak-detector include/asm-s390/types.h
@@ -79,7 +79,7 @@ typedef unsigned  long u64;
 
 typedef u32 dma_addr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #ifndef __s390x__
 typedef union {
include/asm-sh64/types.h renamed from include/asm-sh64/types.h~slab-leak-detector +1 -1
diff -puN include/asm-sh64/types.h~slab-leak-detector include/asm-sh64/types.h
@@ -65,7 +65,7 @@ typedef u32 dma_addr_t;
 #endif
 typedef u64 dma64_addr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-sh/types.h renamed from include/asm-sh/types.h~slab-leak-detector +1 -1
diff -puN include/asm-sh/types.h~slab-leak-detector include/asm-sh/types.h
@@ -58,7 +58,7 @@ typedef u64 sector_t;
 #define HAVE_SECTOR_T
 #endif
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-sparc64/types.h renamed from include/asm-sparc64/types.h~slab-leak-detector +1 -1
diff -puN include/asm-sparc64/types.h~slab-leak-detector include/asm-sparc64/types.h
@@ -56,7 +56,7 @@ typedef unsigned long u64;
 typedef u32 dma_addr_t;
 typedef u64 dma64_addr_t;
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-sparc/types.h renamed from include/asm-sparc/types.h~slab-leak-detector +1 -1
diff -puN include/asm-sparc/types.h~slab-leak-detector include/asm-sparc/types.h
@@ -54,7 +54,7 @@ typedef unsigned long long u64;
 typedef u32 dma_addr_t;
 typedef u32 dma64_addr_t;
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-v850/types.h renamed from include/asm-v850/types.h~slab-leak-detector +1 -1
diff -puN include/asm-v850/types.h~slab-leak-detector include/asm-v850/types.h
@@ -59,7 +59,7 @@ typedef unsigned long long u64;
 
 typedef u32 dma_addr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* !__ASSEMBLY__ */
 
include/asm-x86_64/types.h renamed from include/asm-x86_64/types.h~slab-leak-detector +1 -1
diff -puN include/asm-x86_64/types.h~slab-leak-detector include/asm-x86_64/types.h
@@ -51,7 +51,7 @@ typedef u64 dma_addr_t;
 typedef u64 sector_t;
 #define HAVE_SECTOR_T
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
mm/slab.c renamed from mm/slab.c~slab-leak-detector +38 -2
diff -puN mm/slab.c~slab-leak-detector mm/slab.c
@@ -2116,6 +2116,15 @@ cache_alloc_debugcheck_after(kmem_cache_
 		*dbg_redzone1(cachep, objp) = RED_ACTIVE;
 		*dbg_redzone2(cachep, objp) = RED_ACTIVE;
 	}
+	{
+		int objnr;
+		struct slab *slabp;
+
+		slabp = GET_PAGE_SLAB(virt_to_page(objp));
+
+		objnr = (objp - slabp->s_mem) / cachep->objsize;
+		slab_bufctl(slabp)[objnr] = (unsigned long)caller;
+	}
 	objp += obj_dbghead(cachep);
 	if (cachep->ctor && cachep->flags & SLAB_POISON) {
 		unsigned long	ctor_flags = SLAB_CTOR_CONSTRUCTOR;
@@ -2179,12 +2188,14 @@ static void free_block(kmem_cache_t *cac
 		objnr = (objp - slabp->s_mem) / cachep->objsize;
 		check_slabp(cachep, slabp);
 #if DEBUG
+#if 0
 		if (slab_bufctl(slabp)[objnr] != BUFCTL_FREE) {
 			printk(KERN_ERR "slab: double free detected in cache '%s', objp %p.\n",
 						cachep->name, objp);
 			BUG();
 		}
 #endif
+#endif
 		slab_bufctl(slabp)[objnr] = slabp->free;
 		slabp->free = objnr;
 		STATS_DEC_ACTIVE(cachep);
@@ -2998,6 +3009,29 @@ struct seq_operations slabinfo_op = {
 	.show	= s_show,
 };
 
+static void do_dump_slabp(kmem_cache_t *cachep)
+{
+#if DEBUG
+	struct list_head *q;
+
+	check_irq_on();
+	spin_lock_irq(&cachep->spinlock);
+	list_for_each(q,&cachep->lists.slabs_full) {
+		struct slab *slabp;
+		int i;
+		slabp = list_entry(q, struct slab, list);
+		for (i = 0; i < cachep->num; i++) {
+			unsigned long sym = slab_bufctl(slabp)[i];
+
+			printk("obj %p/%d: %p", slabp, i, (void *)sym);
+			print_symbol(" <%s>", sym);
+			printk("\n");
+		}
+	}
+	spin_unlock_irq(&cachep->spinlock);
+#endif
+}
+
 #define MAX_SLABINFO_WRITE 128
 /**
  * slabinfo_write - Tuning for the slab allocator
@@ -3038,9 +3072,11 @@ ssize_t slabinfo_write(struct file *file
 			    batchcount < 1 ||
 			    batchcount > limit ||
 			    shared < 0) {
-				res = -EINVAL;
+				do_dump_slabp(cachep);
+				res = 0;
 			} else {
-				res = do_tune_cpucache(cachep, limit, batchcount, shared);
+				res = do_tune_cpucache(cachep, limit,
+							batchcount, shared);
 			}
 			break;
 		}

_

#4 lore raw
Quoted 9 lines · Andrew Morton
Quoted 6 lines · OGAWA Hirofumi

Review

Quoted 1 line · Andrew Morton
(quoted diff) +1 -0
+		slab_bufctl(slabp)[objnr] = (unsigned long)caller;

Umm... this patch looks strange..

Quoted 9 lines
Quoted 4 lines

slab_bufctl() returns "kmem_bufctl_t *", but kmem_bufctl_t is "unsigned short".

Good point. This seems to work.

Two updates are needed for the leak detection in recent kernels:
- set kmem_bufctl_t back to unsigned long
- relax the check in check_slabuse, something like the attached patch.

Note that the patch is not tested.

--
Manfred

mm/slab.c +3 -11
@@ -2645,18 +2642,10 @@
 		red1 = *dbg_redzone1(cachep, objp);
 		red2 = *dbg_redzone2(cachep, objp);
 
-		/* simplest case: marked as inactive */
-		if (red1 == RED_INACTIVE && red2 == RED_INACTIVE)
-			continue;
-
-		/* tricky case: if the bufctl value is BUFCTL_ALLOC, then
-		 * the object is either allocated or somewhere in a cpu
-		 * cache. The cpu caches are lockless and there might be
-		 * a concurrent alloc/free call, thus we must accept random
-		 * combinations of RED_ACTIVE and _INACTIVE
+		/* leak detection stores the caller address in the bufctl,
+		 * thus random combinations of active and inactive are ok
 		 */
-		if (slab_bufctl(slabp)[i] == BUFCTL_ALLOC &&
-				(red1 == RED_INACTIVE || red1 == RED_ACTIVE) &&
+		if ((red1 == RED_INACTIVE || red1 == RED_ACTIVE) &&
 				(red2 == RED_INACTIVE || red2 == RED_ACTIVE))
 			continue;
 
#5 lore raw
In reply to Andrew Morton

OK - I have the patch working now, but there seems to be a flaw in the address reporting. When I look up the reported address in /proc/kallsyms, then look in the objdump of the module, the reported adress _does_not_ point to a call.

Am I missing something simple here?

Justin

Quoted 20 lines · Andrew Morton
Quoted 5 lines · Justin Schoeman

I am having a problem with memory leaking on a patched kernel. In order to pinpoint the leak, I would like to try to trace the allocation points for the memory.

I have found some vague references to patches that allow the user to dump the caller address for slab allocations, but I cannot find the patch itself.

Can anybody please point me in the right direction - either for that patch, or any other way to track down leaking slabs?

From: Manfred Spraul <[email protected]>

With the patch applied,

	echo "size-4096 0 0 0" > /proc/slabinfo

walks the objects in the size-4096 slab, printing out the calling address of whoever allocated that object.

It is for leak detection.

...

#6 lore raw
Quoted 7 lines · Justin Schoeman

OK - I have the patch working now, but there seems to be a flaw in the
address reporting. When I look up the reported address in /proc/kallsyms, then look in the objdump of the module, the reported adress _does_not_ point to a call.

Am I missing something simple here?

Use the latest version of the patch (below). Link the module into the kernel. Enable CONFIG_KALLSYMS.

include/asm-alpha/types.h renamed from include/asm-alpha/types.h~slab-leak-detector +1 -1
@@ -56,7 +56,7 @@ typedef unsigned long u64;
 typedef u64 dma_addr_t;
 typedef u64 dma64_addr_t;
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 #endif /* __KERNEL__ */
include/asm-arm26/types.h renamed from include/asm-arm26/types.h~slab-leak-detector +1 -1
diff -puN include/asm-arm26/types.h~slab-leak-detector include/asm-arm26/types.h
@@ -52,7 +52,7 @@ typedef unsigned long long u64;
 typedef u32 dma_addr_t;
 typedef u32 dma64_addr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-arm/types.h renamed from include/asm-arm/types.h~slab-leak-detector +1 -1
diff -puN include/asm-arm/types.h~slab-leak-detector include/asm-arm/types.h
@@ -52,7 +52,7 @@ typedef unsigned long long u64;
 typedef u32 dma_addr_t;
 typedef u32 dma64_addr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-cris/types.h renamed from include/asm-cris/types.h~slab-leak-detector +1 -1
diff -puN include/asm-cris/types.h~slab-leak-detector include/asm-cris/types.h
@@ -52,7 +52,7 @@ typedef unsigned long long u64;
 typedef u32 dma_addr_t;
 typedef u32 dma64_addr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-frv/types.h renamed from include/asm-frv/types.h~slab-leak-detector +1 -1
diff -puN include/asm-frv/types.h~slab-leak-detector include/asm-frv/types.h
@@ -65,7 +65,7 @@ typedef u64 u_quad_t;
 
 typedef u32 dma_addr_t;
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-h8300/types.h renamed from include/asm-h8300/types.h~slab-leak-detector +1 -1
diff -puN include/asm-h8300/types.h~slab-leak-detector include/asm-h8300/types.h
@@ -58,7 +58,7 @@ typedef u32 dma_addr_t;
 #define HAVE_SECTOR_T
 typedef u64 sector_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __KERNEL__ */
 
include/asm-i386/types.h renamed from include/asm-i386/types.h~slab-leak-detector +1 -1
diff -puN include/asm-i386/types.h~slab-leak-detector include/asm-i386/types.h
@@ -63,7 +63,7 @@ typedef u64 sector_t;
 #define HAVE_SECTOR_T
 #endif
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-ia64/types.h renamed from include/asm-ia64/types.h~slab-leak-detector +1 -1
diff -puN include/asm-ia64/types.h~slab-leak-detector include/asm-ia64/types.h
@@ -67,7 +67,7 @@ typedef __u64 u64;
 
 typedef u64 dma_addr_t;
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 # endif /* __KERNEL__ */
 #endif /* !__ASSEMBLY__ */
include/asm-m32r/types.h renamed from include/asm-m32r/types.h~slab-leak-detector +1 -1
diff -puN include/asm-m32r/types.h~slab-leak-detector include/asm-m32r/types.h
@@ -55,7 +55,7 @@ typedef unsigned long long u64;
 typedef u32 dma_addr_t;
 typedef u64 dma64_addr_t;
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-m68k/types.h renamed from include/asm-m68k/types.h~slab-leak-detector +1 -1
diff -puN include/asm-m68k/types.h~slab-leak-detector include/asm-m68k/types.h
@@ -60,7 +60,7 @@ typedef unsigned long long u64;
 typedef u32 dma_addr_t;
 typedef u32 dma64_addr_t;
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-mips/types.h renamed from include/asm-mips/types.h~slab-leak-detector +1 -1
diff -puN include/asm-mips/types.h~slab-leak-detector include/asm-mips/types.h
@@ -99,7 +99,7 @@ typedef u64 sector_t;
 #define HAVE_SECTOR_T
 #endif
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-parisc/types.h renamed from include/asm-parisc/types.h~slab-leak-detector +1 -1
diff -puN include/asm-parisc/types.h~slab-leak-detector include/asm-parisc/types.h
@@ -56,7 +56,7 @@ typedef unsigned long long u64;
 typedef u32 dma_addr_t;
 typedef u64 dma64_addr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-ppc64/types.h renamed from include/asm-ppc64/types.h~slab-leak-detector +1 -1
diff -puN include/asm-ppc64/types.h~slab-leak-detector include/asm-ppc64/types.h
@@ -72,7 +72,7 @@ typedef struct {
 	unsigned long env;
 } func_descr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 #endif /* __ASSEMBLY__ */
 
 #endif /* __KERNEL__ */
include/asm-ppc/types.h renamed from include/asm-ppc/types.h~slab-leak-detector +1 -1
diff -puN include/asm-ppc/types.h~slab-leak-detector include/asm-ppc/types.h
@@ -62,7 +62,7 @@ typedef u64 sector_t;
 #define HAVE_SECTOR_T
 #endif
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-s390/types.h renamed from include/asm-s390/types.h~slab-leak-detector +1 -1
diff -puN include/asm-s390/types.h~slab-leak-detector include/asm-s390/types.h
@@ -79,7 +79,7 @@ typedef unsigned  long u64;
 
 typedef u32 dma_addr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #ifndef __s390x__
 typedef union {
include/asm-sh64/types.h renamed from include/asm-sh64/types.h~slab-leak-detector +1 -1
diff -puN include/asm-sh64/types.h~slab-leak-detector include/asm-sh64/types.h
@@ -65,7 +65,7 @@ typedef u32 dma_addr_t;
 #endif
 typedef u64 dma64_addr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-sh/types.h renamed from include/asm-sh/types.h~slab-leak-detector +1 -1
diff -puN include/asm-sh/types.h~slab-leak-detector include/asm-sh/types.h
@@ -58,7 +58,7 @@ typedef u64 sector_t;
 #define HAVE_SECTOR_T
 #endif
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-sparc64/types.h renamed from include/asm-sparc64/types.h~slab-leak-detector +1 -1
diff -puN include/asm-sparc64/types.h~slab-leak-detector include/asm-sparc64/types.h
@@ -56,7 +56,7 @@ typedef unsigned long u64;
 typedef u32 dma_addr_t;
 typedef u64 dma64_addr_t;
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-sparc/types.h renamed from include/asm-sparc/types.h~slab-leak-detector +1 -1
diff -puN include/asm-sparc/types.h~slab-leak-detector include/asm-sparc/types.h
@@ -54,7 +54,7 @@ typedef unsigned long long u64;
 typedef u32 dma_addr_t;
 typedef u32 dma64_addr_t;
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
include/asm-v850/types.h renamed from include/asm-v850/types.h~slab-leak-detector +1 -1
diff -puN include/asm-v850/types.h~slab-leak-detector include/asm-v850/types.h
@@ -59,7 +59,7 @@ typedef unsigned long long u64;
 
 typedef u32 dma_addr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* !__ASSEMBLY__ */
 
include/asm-x86_64/types.h renamed from include/asm-x86_64/types.h~slab-leak-detector +1 -1
diff -puN include/asm-x86_64/types.h~slab-leak-detector include/asm-x86_64/types.h
@@ -51,7 +51,7 @@ typedef u64 dma_addr_t;
 typedef u64 sector_t;
 #define HAVE_SECTOR_T
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 
mm/slab.c renamed from mm/slab.c~slab-leak-detector +38 -2
diff -puN mm/slab.c~slab-leak-detector mm/slab.c
@@ -2116,6 +2116,15 @@ cache_alloc_debugcheck_after(kmem_cache_
 		*dbg_redzone1(cachep, objp) = RED_ACTIVE;
 		*dbg_redzone2(cachep, objp) = RED_ACTIVE;
 	}
+	{
+		int objnr;
+		struct slab *slabp;
+
+		slabp = GET_PAGE_SLAB(virt_to_page(objp));
+
+		objnr = (objp - slabp->s_mem) / cachep->objsize;
+		slab_bufctl(slabp)[objnr] = (unsigned long)caller;
+	}
 	objp += obj_dbghead(cachep);
 	if (cachep->ctor && cachep->flags & SLAB_POISON) {
 		unsigned long	ctor_flags = SLAB_CTOR_CONSTRUCTOR;
@@ -2179,12 +2188,14 @@ static void free_block(kmem_cache_t *cac
 		objnr = (objp - slabp->s_mem) / cachep->objsize;
 		check_slabp(cachep, slabp);
 #if DEBUG
+#if 0
 		if (slab_bufctl(slabp)[objnr] != BUFCTL_FREE) {
 			printk(KERN_ERR "slab: double free detected in cache '%s', objp %p.\n",
 						cachep->name, objp);
 			BUG();
 		}
 #endif
+#endif
 		slab_bufctl(slabp)[objnr] = slabp->free;
 		slabp->free = objnr;
 		STATS_DEC_ACTIVE(cachep);
@@ -2998,6 +3009,29 @@ struct seq_operations slabinfo_op = {
 	.show	= s_show,
 };
 
+static void do_dump_slabp(kmem_cache_t *cachep)
+{
+#if DEBUG
+	struct list_head *q;
+
+	check_irq_on();
+	spin_lock_irq(&cachep->spinlock);
+	list_for_each(q,&cachep->lists.slabs_full) {
+		struct slab *slabp;
+		int i;
+		slabp = list_entry(q, struct slab, list);
+		for (i = 0; i < cachep->num; i++) {
+			unsigned long sym = slab_bufctl(slabp)[i];
+
+			printk("obj %p/%d: %p", slabp, i, (void *)sym);
+			print_symbol(" <%s>", sym);
+			printk("\n");
+		}
+	}
+	spin_unlock_irq(&cachep->spinlock);
+#endif
+}
+
 #define MAX_SLABINFO_WRITE 128
 /**
  * slabinfo_write - Tuning for the slab allocator
@@ -3038,9 +3072,11 @@ ssize_t slabinfo_write(struct file *file
 			    batchcount < 1 ||
 			    batchcount > limit ||
 			    shared < 0) {
-				res = -EINVAL;
+				do_dump_slabp(cachep);
+				res = 0;
 			} else {
-				res = do_tune_cpucache(cachep, limit, batchcount, shared);
+				res = do_tune_cpucache(cachep, limit,
+							batchcount, shared);
 			}
 			break;
 		}

_

#7 lore raw

Thanks everybody for your help. I have at least located the site of the leak - now I just need to find out why the destructor is not being called ;-).

Will the slab debugger make it into the kernel as a standard compile time option? It is a _really_ usefull tool to have around.

Thanks again,
Justin

Quoted 347 lines · Andrew Morton
Quoted 3 lines · Justin Schoeman

OK - I have the patch working now, but there seems to be a flaw in the address reporting. When I look up the reported address in /proc/kallsyms, then look in the objdump of the module, the reported adress _does_not_ point to a call.

Am I missing something simple here?

Use the latest version of the patch (below). Link the module into the kernel. Enable CONFIG_KALLSYMS.

include/asm-alpha/types.h renamed from include/asm-alpha/types.h~slab-leak-detector +1 -1
@@ -56,7 +56,7 @@ typedef unsigned long u64;
 typedef u64 dma_addr_t;
 typedef u64 dma64_addr_t;
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 #endif /* __KERNEL__ */

diff -puN include/asm-arm26/types.h~slab-leak-detector include/asm-arm26/types.h

include/asm-arm26/types.h renamed from include/asm-arm26/types.h~slab-leak-detector +1 -1
@@ -52,7 +52,7 @@ typedef unsigned long long u64;
 typedef u32 dma_addr_t;
 typedef u32 dma64_addr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 

diff -puN include/asm-arm/types.h~slab-leak-detector include/asm-arm/types.h

include/asm-arm/types.h renamed from include/asm-arm/types.h~slab-leak-detector +1 -1
@@ -52,7 +52,7 @@ typedef unsigned long long u64;
 typedef u32 dma_addr_t;
 typedef u32 dma64_addr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 

diff -puN include/asm-cris/types.h~slab-leak-detector include/asm-cris/types.h

include/asm-cris/types.h renamed from include/asm-cris/types.h~slab-leak-detector +1 -1
@@ -52,7 +52,7 @@ typedef unsigned long long u64;
 typedef u32 dma_addr_t;
 typedef u32 dma64_addr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 

diff -puN include/asm-frv/types.h~slab-leak-detector include/asm-frv/types.h

include/asm-frv/types.h renamed from include/asm-frv/types.h~slab-leak-detector +1 -1
@@ -65,7 +65,7 @@ typedef u64 u_quad_t;
 
 typedef u32 dma_addr_t;
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 

diff -puN include/asm-h8300/types.h~slab-leak-detector include/asm-h8300/types.h

include/asm-h8300/types.h renamed from include/asm-h8300/types.h~slab-leak-detector +1 -1
@@ -58,7 +58,7 @@ typedef u32 dma_addr_t;
 #define HAVE_SECTOR_T
 typedef u64 sector_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __KERNEL__ */
 

diff -puN include/asm-i386/types.h~slab-leak-detector include/asm-i386/types.h

include/asm-i386/types.h renamed from include/asm-i386/types.h~slab-leak-detector +1 -1
@@ -63,7 +63,7 @@ typedef u64 sector_t;
 #define HAVE_SECTOR_T
 #endif
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 

diff -puN include/asm-ia64/types.h~slab-leak-detector include/asm-ia64/types.h

include/asm-ia64/types.h renamed from include/asm-ia64/types.h~slab-leak-detector +1 -1
@@ -67,7 +67,7 @@ typedef __u64 u64;
 
 typedef u64 dma_addr_t;
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 # endif /* __KERNEL__ */
 #endif /* !__ASSEMBLY__ */

diff -puN include/asm-m32r/types.h~slab-leak-detector include/asm-m32r/types.h

include/asm-m32r/types.h renamed from include/asm-m32r/types.h~slab-leak-detector +1 -1
@@ -55,7 +55,7 @@ typedef unsigned long long u64;
 typedef u32 dma_addr_t;
 typedef u64 dma64_addr_t;
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 

diff -puN include/asm-m68k/types.h~slab-leak-detector include/asm-m68k/types.h

include/asm-m68k/types.h renamed from include/asm-m68k/types.h~slab-leak-detector +1 -1
@@ -60,7 +60,7 @@ typedef unsigned long long u64;
 typedef u32 dma_addr_t;
 typedef u32 dma64_addr_t;
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 

diff -puN include/asm-mips/types.h~slab-leak-detector include/asm-mips/types.h

include/asm-mips/types.h renamed from include/asm-mips/types.h~slab-leak-detector +1 -1
@@ -99,7 +99,7 @@ typedef u64 sector_t;
 #define HAVE_SECTOR_T
 #endif
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 

diff -puN include/asm-parisc/types.h~slab-leak-detector include/asm-parisc/types.h

include/asm-parisc/types.h renamed from include/asm-parisc/types.h~slab-leak-detector +1 -1
@@ -56,7 +56,7 @@ typedef unsigned long long u64;
 typedef u32 dma_addr_t;
 typedef u64 dma64_addr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 

diff -puN include/asm-ppc64/types.h~slab-leak-detector include/asm-ppc64/types.h

include/asm-ppc64/types.h renamed from include/asm-ppc64/types.h~slab-leak-detector +1 -1
@@ -72,7 +72,7 @@ typedef struct {
 	unsigned long env;
 } func_descr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 #endif /* __ASSEMBLY__ */
 
 #endif /* __KERNEL__ */

diff -puN include/asm-ppc/types.h~slab-leak-detector include/asm-ppc/types.h

include/asm-ppc/types.h renamed from include/asm-ppc/types.h~slab-leak-detector +1 -1
@@ -62,7 +62,7 @@ typedef u64 sector_t;
 #define HAVE_SECTOR_T
 #endif
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 

diff -puN include/asm-s390/types.h~slab-leak-detector include/asm-s390/types.h

include/asm-s390/types.h renamed from include/asm-s390/types.h~slab-leak-detector +1 -1
@@ -79,7 +79,7 @@ typedef unsigned  long u64;
 
 typedef u32 dma_addr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #ifndef __s390x__
 typedef union {

diff -puN include/asm-sh64/types.h~slab-leak-detector include/asm-sh64/types.h

include/asm-sh64/types.h renamed from include/asm-sh64/types.h~slab-leak-detector +1 -1
@@ -65,7 +65,7 @@ typedef u32 dma_addr_t;
 #endif
 typedef u64 dma64_addr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 

diff -puN include/asm-sh/types.h~slab-leak-detector include/asm-sh/types.h

include/asm-sh/types.h renamed from include/asm-sh/types.h~slab-leak-detector +1 -1
@@ -58,7 +58,7 @@ typedef u64 sector_t;
 #define HAVE_SECTOR_T
 #endif
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 

diff -puN include/asm-sparc64/types.h~slab-leak-detector include/asm-sparc64/types.h

include/asm-sparc64/types.h renamed from include/asm-sparc64/types.h~slab-leak-detector +1 -1
@@ -56,7 +56,7 @@ typedef unsigned long u64;
 typedef u32 dma_addr_t;
 typedef u64 dma64_addr_t;
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 

diff -puN include/asm-sparc/types.h~slab-leak-detector include/asm-sparc/types.h

include/asm-sparc/types.h renamed from include/asm-sparc/types.h~slab-leak-detector +1 -1
@@ -54,7 +54,7 @@ typedef unsigned long long u64;
 typedef u32 dma_addr_t;
 typedef u32 dma64_addr_t;
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 

diff -puN include/asm-v850/types.h~slab-leak-detector include/asm-v850/types.h

include/asm-v850/types.h renamed from include/asm-v850/types.h~slab-leak-detector +1 -1
@@ -59,7 +59,7 @@ typedef unsigned long long u64;
 
 typedef u32 dma_addr_t;
 
-typedef unsigned int kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* !__ASSEMBLY__ */
 

diff -puN include/asm-x86_64/types.h~slab-leak-detector include/asm-x86_64/types.h

include/asm-x86_64/types.h renamed from include/asm-x86_64/types.h~slab-leak-detector +1 -1
@@ -51,7 +51,7 @@ typedef u64 dma_addr_t;
 typedef u64 sector_t;
 #define HAVE_SECTOR_T
 
-typedef unsigned short kmem_bufctl_t;
+typedef unsigned long kmem_bufctl_t;
 
 #endif /* __ASSEMBLY__ */
 

diff -puN mm/slab.c~slab-leak-detector mm/slab.c

mm/slab.c renamed from mm/slab.c~slab-leak-detector +38 -2
@@ -2116,6 +2116,15 @@ cache_alloc_debugcheck_after(kmem_cache_
 		*dbg_redzone1(cachep, objp) = RED_ACTIVE;
 		*dbg_redzone2(cachep, objp) = RED_ACTIVE;
 	}
+	{
+		int objnr;
+		struct slab *slabp;
+
+		slabp = GET_PAGE_SLAB(virt_to_page(objp));
+
+		objnr = (objp - slabp->s_mem) / cachep->objsize;
+		slab_bufctl(slabp)[objnr] = (unsigned long)caller;
+	}
 	objp += obj_dbghead(cachep);
 	if (cachep->ctor && cachep->flags & SLAB_POISON) {
 		unsigned long	ctor_flags = SLAB_CTOR_CONSTRUCTOR;
@@ -2179,12 +2188,14 @@ static void free_block(kmem_cache_t *cac
 		objnr = (objp - slabp->s_mem) / cachep->objsize;
 		check_slabp(cachep, slabp);
 #if DEBUG
+#if 0
 		if (slab_bufctl(slabp)[objnr] != BUFCTL_FREE) {
 			printk(KERN_ERR "slab: double free detected in cache '%s', objp %p.\n",
 						cachep->name, objp);
 			BUG();
 		}
 #endif
+#endif
 		slab_bufctl(slabp)[objnr] = slabp->free;
 		slabp->free = objnr;
 		STATS_DEC_ACTIVE(cachep);
@@ -2998,6 +3009,29 @@ struct seq_operations slabinfo_op = {
 	.show	= s_show,
 };
 
+static void do_dump_slabp(kmem_cache_t *cachep)
+{
+#if DEBUG
+	struct list_head *q;
+
+	check_irq_on();
+	spin_lock_irq(&cachep->spinlock);
+	list_for_each(q,&cachep->lists.slabs_full) {
+		struct slab *slabp;
+		int i;
+		slabp = list_entry(q, struct slab, list);
+		for (i = 0; i < cachep->num; i++) {
+			unsigned long sym = slab_bufctl(slabp)[i];
+
+			printk("obj %p/%d: %p", slabp, i, (void *)sym);
+			print_symbol(" <%s>", sym);
+			printk("\n");
+		}
+	}
+	spin_unlock_irq(&cachep->spinlock);
+#endif
+}
+
 #define MAX_SLABINFO_WRITE 128
 /**
  * slabinfo_write - Tuning for the slab allocator
@@ -3038,9 +3072,11 @@ ssize_t slabinfo_write(struct file *file
 			    batchcount < 1 ||
 			    batchcount > limit ||
 			    shared < 0) {
-				res = -EINVAL;
+				do_dump_slabp(cachep);
+				res = 0;
 			} else {
-				res = do_tune_cpucache(cachep, limit, batchcount, shared);
+				res = do_tune_cpucache(cachep, limit,
+							batchcount, shared);
 			}
 			break;
 		}

_

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#8 lore raw
Quoted 3 lines · Justin Schoeman

Will the slab debugger make it into the kernel as a standard compile
time option? It is a _really_ usefull tool to have around.

It has a slight downside: it unconditionally changes the type of kmem_bufctl_t to unsigned long, which wastes two or four bytes per page of slab.

I suppose we could fix that up with suitable use of CONFIG_DEBUG_SLAB.

#9 lore raw
In reply to Justin Schoeman
Quoted 8 lines

I am having a problem with memory leaking on a patched kernel. In order to pinpoint the leak, I would like to try to trace the allocation points for the memory.

I have found some vague references to patches that allow the user to dump the caller address for slab allocations, but I cannot find the patch itself.

Quoted 5 lines

Can anybody please point me in the right direction - either for that patch, or any other way to track down leaking slabs?

From akpm:

Could you please use this patch? Make sure that you enable CONFIG_FRAME_POINTER (might not be needed for __builtin_return_address(0), but let's be sure). Also enable CONFIG_DEBUG_SLAB.

From: Manfred Spraul <[email protected]>

With the patch applied,

	echo "size-4096 0 0 0" > /proc/slabinfo

walks the objects in the size-4096 slab, printing out the calling address of whoever allocated that object.

It is for leak detection.

mm/slab.c renamed from mm/slab.c~slab-leak-detector +38 -2
diff -puN mm/slab.c~slab-leak-detector mm/slab.c
@@ -2116,6 +2116,15 @@ cache_alloc_debugcheck_after(kmem_cache_
 		*dbg_redzone1(cachep, objp) = RED_ACTIVE;
 		*dbg_redzone2(cachep, objp) = RED_ACTIVE;
 	}
+	{
+		int objnr;
+		struct slab *slabp;
+
+		slabp = GET_PAGE_SLAB(virt_to_page(objp));
+
+		objnr = (objp - slabp->s_mem) / cachep->objsize;
+		slab_bufctl(slabp)[objnr] = (unsigned long)caller;
+	}
 	objp += obj_dbghead(cachep);
 	if (cachep->ctor && cachep->flags & SLAB_POISON) {
 		unsigned long	ctor_flags = SLAB_CTOR_CONSTRUCTOR;
@@ -2179,12 +2188,14 @@ static void free_block(kmem_cache_t *cac
 		objnr = (objp - slabp->s_mem) / cachep->objsize;
 		check_slabp(cachep, slabp);
 #if DEBUG
+#if 0
 		if (slab_bufctl(slabp)[objnr] != BUFCTL_FREE) {
 			printk(KERN_ERR "slab: double free detected in cache '%s', objp %p.\n",
 						cachep->name, objp);
 			BUG();
 		}
 #endif
+#endif
 		slab_bufctl(slabp)[objnr] = slabp->free;
 		slabp->free = objnr;
 		STATS_DEC_ACTIVE(cachep);
@@ -2998,6 +3009,29 @@ struct seq_operations slabinfo_op = {
 	.show	= s_show,
 };
 
+static void do_dump_slabp(kmem_cache_t *cachep)
+{
+#if DEBUG
+	struct list_head *q;
+
+	check_irq_on();
+	spin_lock_irq(&cachep->spinlock);
+	list_for_each(q,&cachep->lists.slabs_full) {
+		struct slab *slabp;
+		int i;
+		slabp = list_entry(q, struct slab, list);
+		for (i = 0; i < cachep->num; i++) {
+			unsigned long sym = slab_bufctl(slabp)[i];
+
+			printk("obj %p/%d: %p", slabp, i, (void *)sym);
+			print_symbol(" <%s>", sym);
+			printk("\n");
+		}
+	}
+	spin_unlock_irq(&cachep->spinlock);
+#endif
+}
+
 #define MAX_SLABINFO_WRITE 128
 /**
  * slabinfo_write - Tuning for the slab allocator
@@ -3038,9 +3072,11 @@ ssize_t slabinfo_write(struct file *file
 			    batchcount < 1 ||
 			    batchcount > limit ||
 			    shared < 0) {
-				res = -EINVAL;
+				do_dump_slabp(cachep);
+				res = 0;
 			} else {
-				res = do_tune_cpucache(cachep, limit, batchcount, shared);
+				res = do_tune_cpucache(cachep, limit,
+							batchcount, shared);
 			}
 			break;
 		}

_