From c19c06f3b05a220cb638050c0e06578a09ad64ad Mon Sep 17 00:00:00 2001 From: behlendo Date: Fri, 14 Mar 2008 20:56:26 +0000 Subject: [PATCH] Fix kmem memory accounting Adjust kmem slab interface to make a copy of the slab name before passing it on to the linux slab (we free it latter too) git-svn-id: https://outreach.scidac.gov/svn/spl/trunk@47 7e1ea52c-4ff2-0310-8f11-9dd32ca42a1c --- include/sys/kmem.h | 43 +++++++++++++++++++++----------------- modules/spl/spl-generic.c | 17 +++++++++++++++ modules/spl/spl-kmem.c | 26 ++++++++++++++++++----- modules/splat/splat-kmem.c | 12 +++++++++++ 4 files changed, 74 insertions(+), 24 deletions(-) diff --git a/include/sys/kmem.h b/include/sys/kmem.h index 2560b380a2..cce2a05e74 100644 --- a/include/sys/kmem.h +++ b/include/sys/kmem.h @@ -24,30 +24,32 @@ extern "C" { #define KM_FLAGS __GFP_BITS_MASK #ifdef DEBUG_KMEM -extern atomic_t kmem_alloc_used; -extern unsigned int kmem_alloc_max; -extern atomic_t vmem_alloc_used; -extern unsigned int vmem_alloc_max; +extern atomic64_t kmem_alloc_used; +extern unsigned long kmem_alloc_max; +extern atomic64_t vmem_alloc_used; +extern unsigned long vmem_alloc_max; +extern int kmem_warning_flag; #define __kmem_alloc(size, flags, allocator) \ ({ void *_ptr_; \ \ /* Marked unlikely because we should never be doing this */ \ - if (unlikely((size) > (PAGE_SIZE * 2))) \ + if (unlikely((size) > (PAGE_SIZE * 2)) && kmem_warning_flag) \ printk("Warning: kmem_alloc(%d, 0x%x) large alloc at %s:%d " \ - "(%d/%d)\n", (int)(size), (int)(flags), \ + "(%ld/%ld)\n", (int)(size), (int)(flags), \ __FILE__, __LINE__, \ - atomic_read(&kmem_alloc_used), kmem_alloc_max); \ + atomic64_read(&kmem_alloc_used), kmem_alloc_max); \ \ _ptr_ = (void *)allocator((size), (flags)); \ if (_ptr_ == NULL) { \ printk("Warning: kmem_alloc(%d, 0x%x) failed at %s:%d " \ - "(%d/%d)\n", (int)(size), (int)(flags), \ + "(%ld/%ld)\n", (int)(size), (int)(flags), \ __FILE__, __LINE__, \ - atomic_read(&kmem_alloc_used), kmem_alloc_max); \ - atomic_add((size), &kmem_alloc_used); \ - if (unlikely(atomic_read(&kmem_alloc_used) > kmem_alloc_max)) \ - kmem_alloc_max = atomic_read(&kmem_alloc_used); \ + atomic64_read(&kmem_alloc_used), kmem_alloc_max); \ + } else { \ + atomic64_add((size), &kmem_alloc_used); \ + if (unlikely(atomic64_read(&kmem_alloc_used)>kmem_alloc_max)) \ + kmem_alloc_max = atomic64_read(&kmem_alloc_used); \ } \ \ _ptr_; \ @@ -59,7 +61,7 @@ extern unsigned int vmem_alloc_max; #define kmem_free(ptr, size) \ ({ \ BUG_ON(!(ptr) || (size) < 0); \ - atomic_sub((size), &kmem_alloc_used); \ + atomic64_sub((size), &kmem_alloc_used); \ memset(ptr, 0x5a, (size)); /* Poison */ \ kfree(ptr); \ }) @@ -72,12 +74,13 @@ extern unsigned int vmem_alloc_max; _ptr_ = (void *)vmalloc((size)); \ if (_ptr_ == NULL) { \ printk("Warning: vmem_alloc(%d, 0x%x) failed at %s:%d " \ - "(%d/%d)\n", (int)(size), (int)(flags), \ + "(%ld/%ld)\n", (int)(size), (int)(flags), \ __FILE__, __LINE__, \ - atomic_read(&vmem_alloc_used), vmem_alloc_max); \ - atomic_add((size), &vmem_alloc_used); \ - if (unlikely(atomic_read(&vmem_alloc_used) > vmem_alloc_max)) \ - vmem_alloc_max = atomic_read(&vmem_alloc_used); \ + atomic64_read(&vmem_alloc_used), vmem_alloc_max); \ + } else { \ + atomic64_add((size), &vmem_alloc_used); \ + if (unlikely(atomic64_read(&vmem_alloc_used)>vmem_alloc_max)) \ + vmem_alloc_max = atomic64_read(&vmem_alloc_used); \ } \ \ _ptr_; \ @@ -88,7 +91,7 @@ extern unsigned int vmem_alloc_max; #define vmem_free(ptr, size) \ ({ \ BUG_ON(!(ptr) || (size) < 0); \ - atomic_sub((size), &vmem_alloc_used); \ + atomic64_sub((size), &vmem_alloc_used); \ memset(ptr, 0x5a, (size)); /* Poison */ \ vfree(ptr); \ }) @@ -186,6 +189,8 @@ typedef int (*kmem_constructor_t)(void *, void *, int); typedef void (*kmem_destructor_t)(void *, void *); typedef void (*kmem_reclaim_t)(void *); +extern int kmem_set_warning(int flag); + extern kmem_cache_t * __kmem_cache_create(char *name, size_t size, size_t align, kmem_constructor_t constructor, diff --git a/modules/spl/spl-generic.c b/modules/spl/spl-generic.c index cae4223f33..9f48dd1a5a 100644 --- a/modules/spl/spl-generic.c +++ b/modules/spl/spl-generic.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "config.h" /* @@ -66,12 +67,28 @@ static int __init spl_init(void) strcpy(hw_serial, "007f0100"); /* loopback */ printk(KERN_INFO "spl: Loaded Solaris Porting Layer v%s\n", VERSION); +#ifdef DEBUG_KMEM + atomic64_set(&kmem_alloc_used, 0); + atomic64_set(&vmem_alloc_used, 0); +#endif + return 0; } static void spl_fini(void) { vn_fini(); + +#ifdef DEBUG_KMEM + if (atomic64_read(&kmem_alloc_used) != 0) + printk("Warning: kmem leaked %ld/%ld bytes\n", + atomic_read(&kmem_alloc_used), kmem_alloc_max); + + if (atomic64_read(&vmem_alloc_used) != 0) + printk("Warning: vmem leaked %ld/%ld bytes\n", + atomic_read(&vmem_alloc_used), vmem_alloc_max); +#endif + return; } diff --git a/modules/spl/spl-kmem.c b/modules/spl/spl-kmem.c index d3cb2c93a0..c8808105b2 100644 --- a/modules/spl/spl-kmem.c +++ b/modules/spl/spl-kmem.c @@ -5,16 +5,23 @@ */ #ifdef DEBUG_KMEM /* Shim layer memory accounting */ -atomic_t kmem_alloc_used; -unsigned int kmem_alloc_max; -atomic_t vmem_alloc_used; -unsigned int vmem_alloc_max; +atomic64_t kmem_alloc_used; +unsigned long kmem_alloc_max = 0; +atomic64_t vmem_alloc_used; +unsigned long vmem_alloc_max = 0; +int kmem_warning_flag = 1; EXPORT_SYMBOL(kmem_alloc_used); EXPORT_SYMBOL(kmem_alloc_max); EXPORT_SYMBOL(vmem_alloc_used); EXPORT_SYMBOL(vmem_alloc_max); +EXPORT_SYMBOL(kmem_warning_flag); + +int kmem_set_warning(int flag) { return (kmem_warning_flag = !!flag); } +#else +int kmem_set_warning(int flag) { return 0; } #endif +EXPORT_SYMBOL(kmem_set_warning); /* * Slab allocation interfaces @@ -187,11 +194,17 @@ __kmem_cache_create(char *name, size_t size, size_t align, kmem_cache_t *cache; kmem_cache_cb_t *kcc; int shrinker_flag = 0; + char *cache_name; /* FIXME: - Option currently unsupported by shim layer */ BUG_ON(vmp); - cache = kmem_cache_create(name, size, align, flags, + cache_name = kzalloc(strlen(name) + 1, GFP_KERNEL); + if (cache_name == NULL) + return NULL; + + strcpy(cache_name, name); + cache = kmem_cache_create(cache_name, size, align, flags, kmem_cache_generic_constructor, kmem_cache_generic_destructor); if (cache == NULL) @@ -230,6 +243,7 @@ void __kmem_cache_destroy(kmem_cache_t *cache) { kmem_cache_cb_t *kcc; + char *name; spin_lock(&kmem_cache_cb_lock); kcc = kmem_cache_find_cache_cb(cache); @@ -237,8 +251,10 @@ __kmem_cache_destroy(kmem_cache_t *cache) if (kcc == NULL) return; + name = (char *)kmem_cache_name(cache); kmem_cache_destroy(cache); kmem_cache_remove_cache_cb(kcc); + kfree(name); /* Unregister generic shrinker on removal of all caches */ spin_lock(&kmem_cache_cb_lock); diff --git a/modules/splat/splat-kmem.c b/modules/splat/splat-kmem.c index 7115ca42ce..f9f6964dea 100644 --- a/modules/splat/splat-kmem.c +++ b/modules/splat/splat-kmem.c @@ -35,6 +35,10 @@ splat_kmem_test1(struct file *file, void *arg) int size = PAGE_SIZE; int i, count, rc = 0; + /* We are intentionally going to push kmem_alloc to its max + * allocation size, so suppress the console warnings for now */ + kmem_set_warning(0); + while ((!rc) && (size <= (PAGE_SIZE * 32))) { count = 0; @@ -57,6 +61,8 @@ splat_kmem_test1(struct file *file, void *arg) size *= 2; } + kmem_set_warning(1); + return rc; } @@ -67,6 +73,10 @@ splat_kmem_test2(struct file *file, void *arg) int size = PAGE_SIZE; int i, j, count, rc = 0; + /* We are intentionally going to push kmem_alloc to its max + * allocation size, so suppress the console warnings for now */ + kmem_set_warning(0); + while ((!rc) && (size <= (PAGE_SIZE * 32))) { count = 0; @@ -101,6 +111,8 @@ splat_kmem_test2(struct file *file, void *arg) size *= 2; } + kmem_set_warning(1); + return rc; }