2010-05-17 22:18:00 +00:00
|
|
|
/*****************************************************************************\
|
|
|
|
* Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
|
|
|
|
* Copyright (C) 2007 The Regents of the University of California.
|
|
|
|
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
|
|
|
|
* Written by Brian Behlendorf <behlendorf1@llnl.gov>.
|
2008-05-26 04:38:26 +00:00
|
|
|
* UCRL-CODE-235197
|
|
|
|
*
|
2010-05-17 22:18:00 +00:00
|
|
|
* This file is part of the SPL, Solaris Porting Layer.
|
2013-03-05 01:26:55 +00:00
|
|
|
* For details, see <http://zfsonlinux.org/>.
|
2008-05-26 04:38:26 +00:00
|
|
|
*
|
2010-05-17 22:18:00 +00:00
|
|
|
* The SPL is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* The SPL is distributed in the hope that it will be useful, but WITHOUT
|
2008-05-26 04:38:26 +00:00
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2010-05-17 22:18:00 +00:00
|
|
|
* with the SPL. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*****************************************************************************
|
|
|
|
* Solaris Porting Layer (SPL) Kmem Implementation.
|
|
|
|
\*****************************************************************************/
|
2008-05-26 04:38:26 +00:00
|
|
|
|
2014-12-08 18:04:42 +00:00
|
|
|
#include <sys/debug.h>
|
2008-03-01 00:45:59 +00:00
|
|
|
#include <sys/kmem.h>
|
2014-12-08 18:04:42 +00:00
|
|
|
#include <sys/vmem.h>
|
2009-02-05 20:26:34 +00:00
|
|
|
|
2010-06-11 21:48:18 +00:00
|
|
|
int
|
|
|
|
kmem_debugging(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(kmem_debugging);
|
|
|
|
|
2010-06-24 16:41:59 +00:00
|
|
|
char *
|
|
|
|
kmem_vasprintf(const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
va_list aq;
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
do {
|
2010-07-20 20:51:42 +00:00
|
|
|
va_copy(aq, ap);
|
2010-06-24 16:41:59 +00:00
|
|
|
ptr = kvasprintf(GFP_KERNEL, fmt, aq);
|
2010-07-20 20:51:42 +00:00
|
|
|
va_end(aq);
|
2010-06-24 16:41:59 +00:00
|
|
|
} while (ptr == NULL);
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(kmem_vasprintf);
|
|
|
|
|
2010-06-11 21:48:18 +00:00
|
|
|
char *
|
|
|
|
kmem_asprintf(const char *fmt, ...)
|
|
|
|
{
|
2010-06-24 16:41:59 +00:00
|
|
|
va_list ap;
|
2010-06-11 21:48:18 +00:00
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
do {
|
2010-07-20 20:51:42 +00:00
|
|
|
va_start(ap, fmt);
|
2010-06-24 16:41:59 +00:00
|
|
|
ptr = kvasprintf(GFP_KERNEL, fmt, ap);
|
2010-07-20 20:51:42 +00:00
|
|
|
va_end(ap);
|
2010-06-11 21:48:18 +00:00
|
|
|
} while (ptr == NULL);
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(kmem_asprintf);
|
|
|
|
|
2010-07-26 22:47:55 +00:00
|
|
|
static char *
|
|
|
|
__strdup(const char *str, int flags)
|
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
n = strlen(str);
|
|
|
|
ptr = kmalloc_nofail(n + 1, flags);
|
|
|
|
if (ptr)
|
|
|
|
memcpy(ptr, str, n + 1);
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
strdup(const char *str)
|
|
|
|
{
|
|
|
|
return __strdup(str, KM_SLEEP);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strdup);
|
|
|
|
|
|
|
|
void
|
|
|
|
strfree(char *str)
|
|
|
|
{
|
2010-07-31 05:20:58 +00:00
|
|
|
kfree(str);
|
2010-07-26 22:47:55 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strfree);
|
|
|
|
|
2008-02-26 20:36:04 +00:00
|
|
|
/*
|
2008-06-13 23:41:06 +00:00
|
|
|
* Memory allocation interfaces and debugging for basic kmem_*
|
2009-10-30 20:58:51 +00:00
|
|
|
* and vmem_* style memory allocation. When DEBUG_KMEM is enabled
|
|
|
|
* the SPL will keep track of the total memory allocated, and
|
|
|
|
* report any memory leaked when the module is unloaded.
|
2008-02-26 20:36:04 +00:00
|
|
|
*/
|
|
|
|
#ifdef DEBUG_KMEM
|
2009-12-04 23:54:12 +00:00
|
|
|
|
2008-02-26 20:36:04 +00:00
|
|
|
/* Shim layer memory accounting */
|
2009-12-04 23:54:12 +00:00
|
|
|
# ifdef HAVE_ATOMIC64_T
|
2008-11-03 20:34:17 +00:00
|
|
|
atomic64_t kmem_alloc_used = ATOMIC64_INIT(0);
|
2008-11-03 21:06:04 +00:00
|
|
|
unsigned long long kmem_alloc_max = 0;
|
2010-07-26 22:47:55 +00:00
|
|
|
# else /* HAVE_ATOMIC64_T */
|
2009-12-04 23:54:12 +00:00
|
|
|
atomic_t kmem_alloc_used = ATOMIC_INIT(0);
|
|
|
|
unsigned long long kmem_alloc_max = 0;
|
2010-07-26 22:47:55 +00:00
|
|
|
# endif /* HAVE_ATOMIC64_T */
|
2008-03-14 19:04:41 +00:00
|
|
|
|
2008-06-27 21:40:11 +00:00
|
|
|
EXPORT_SYMBOL(kmem_alloc_used);
|
|
|
|
EXPORT_SYMBOL(kmem_alloc_max);
|
|
|
|
|
2009-10-30 20:58:51 +00:00
|
|
|
/* When DEBUG_KMEM_TRACKING is enabled not only will total bytes be tracked
|
|
|
|
* but also the location of every alloc and free. When the SPL module is
|
|
|
|
* unloaded a list of all leaked addresses and where they were allocated
|
|
|
|
* will be dumped to the console. Enabling this feature has a significant
|
|
|
|
* impact on performance but it makes finding memory leaks straight forward.
|
|
|
|
*
|
|
|
|
* Not surprisingly with debugging enabled the xmem_locks are very highly
|
|
|
|
* contended particularly on xfree(). If we want to run with this detailed
|
|
|
|
* debugging enabled for anything other than debugging we need to minimize
|
|
|
|
* the contention by moving to a lock per xmem_table entry model.
|
2008-11-03 21:06:04 +00:00
|
|
|
*/
|
2009-10-30 20:58:51 +00:00
|
|
|
# ifdef DEBUG_KMEM_TRACKING
|
2008-11-03 21:06:04 +00:00
|
|
|
|
|
|
|
# define KMEM_HASH_BITS 10
|
|
|
|
# define KMEM_TABLE_SIZE (1 << KMEM_HASH_BITS)
|
|
|
|
|
|
|
|
typedef struct kmem_debug {
|
|
|
|
struct hlist_node kd_hlist; /* Hash node linkage */
|
|
|
|
struct list_head kd_list; /* List of all allocations */
|
|
|
|
void *kd_addr; /* Allocation pointer */
|
|
|
|
size_t kd_size; /* Allocation size */
|
|
|
|
const char *kd_func; /* Allocation function */
|
|
|
|
int kd_line; /* Allocation line */
|
|
|
|
} kmem_debug_t;
|
|
|
|
|
2008-05-06 20:38:28 +00:00
|
|
|
spinlock_t kmem_lock;
|
|
|
|
struct hlist_head kmem_table[KMEM_TABLE_SIZE];
|
|
|
|
struct list_head kmem_list;
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(kmem_lock);
|
|
|
|
EXPORT_SYMBOL(kmem_table);
|
|
|
|
EXPORT_SYMBOL(kmem_list);
|
|
|
|
|
2008-11-03 21:06:04 +00:00
|
|
|
static kmem_debug_t *
|
2012-06-25 17:22:21 +00:00
|
|
|
kmem_del_init(spinlock_t *lock, struct hlist_head *table, int bits, const void *addr)
|
2008-11-03 21:06:04 +00:00
|
|
|
{
|
|
|
|
struct hlist_head *head;
|
|
|
|
struct hlist_node *node;
|
|
|
|
struct kmem_debug *p;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(lock, flags);
|
|
|
|
|
2013-07-03 17:14:35 +00:00
|
|
|
head = &table[hash_ptr((void *)addr, bits)];
|
|
|
|
hlist_for_each(node, head) {
|
|
|
|
p = list_entry(node, struct kmem_debug, kd_hlist);
|
2008-11-03 21:06:04 +00:00
|
|
|
if (p->kd_addr == addr) {
|
|
|
|
hlist_del_init(&p->kd_hlist);
|
|
|
|
list_del_init(&p->kd_list);
|
|
|
|
spin_unlock_irqrestore(lock, flags);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(lock, flags);
|
|
|
|
|
2014-11-05 22:30:35 +00:00
|
|
|
return (NULL);
|
2008-11-03 21:06:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
kmem_alloc_track(size_t size, int flags, const char *func, int line,
|
|
|
|
int node_alloc, int node)
|
|
|
|
{
|
|
|
|
void *ptr = NULL;
|
|
|
|
kmem_debug_t *dptr;
|
|
|
|
unsigned long irq_flags;
|
|
|
|
|
2010-07-26 22:47:55 +00:00
|
|
|
/* Function may be called with KM_NOSLEEP so failure is possible */
|
2009-11-12 23:11:24 +00:00
|
|
|
dptr = (kmem_debug_t *) kmalloc_nofail(sizeof(kmem_debug_t),
|
2008-11-03 21:06:04 +00:00
|
|
|
flags & ~__GFP_ZERO);
|
|
|
|
|
2010-07-26 22:47:55 +00:00
|
|
|
if (unlikely(dptr == NULL)) {
|
2014-11-05 22:30:35 +00:00
|
|
|
printk(KERN_WARNING "debug kmem_alloc(%ld, 0x%x) at %s:%d "
|
|
|
|
"failed (%lld/%llu)\n", sizeof(kmem_debug_t), flags,
|
|
|
|
func, line, kmem_alloc_used_read(), kmem_alloc_max);
|
2008-11-03 21:06:04 +00:00
|
|
|
} else {
|
2010-07-26 22:47:55 +00:00
|
|
|
/*
|
|
|
|
* Marked unlikely because we should never be doing this,
|
|
|
|
* we tolerate to up 2 pages but a single page is best.
|
|
|
|
*/
|
2010-05-20 21:16:59 +00:00
|
|
|
if (unlikely((size > PAGE_SIZE*2) && !(flags & KM_NODEBUG))) {
|
2014-11-05 22:30:35 +00:00
|
|
|
printk(KERN_WARNING "large kmem_alloc(%llu, 0x%x) "
|
|
|
|
"at %s:%d failed (%lld/%llu)\n",
|
|
|
|
(unsigned long long)size, flags, func, line,
|
2009-12-04 23:54:12 +00:00
|
|
|
kmem_alloc_used_read(), kmem_alloc_max);
|
2014-11-05 22:30:35 +00:00
|
|
|
spl_dumpstack();
|
2010-05-19 23:53:13 +00:00
|
|
|
}
|
2008-11-03 21:06:04 +00:00
|
|
|
|
2010-07-26 22:47:55 +00:00
|
|
|
/*
|
|
|
|
* We use __strdup() below because the string pointed to by
|
2008-11-03 22:02:15 +00:00
|
|
|
* __FUNCTION__ might not be available by the time we want
|
2010-07-26 22:47:55 +00:00
|
|
|
* to print it since the module might have been unloaded.
|
|
|
|
* This can only fail in the KM_NOSLEEP case.
|
|
|
|
*/
|
|
|
|
dptr->kd_func = __strdup(func, flags & ~__GFP_ZERO);
|
2008-11-03 22:02:15 +00:00
|
|
|
if (unlikely(dptr->kd_func == NULL)) {
|
|
|
|
kfree(dptr);
|
2014-11-05 22:30:35 +00:00
|
|
|
printk(KERN_WARNING "debug __strdup() at %s:%d "
|
|
|
|
"failed (%lld/%llu)\n", func, line,
|
|
|
|
kmem_alloc_used_read(), kmem_alloc_max);
|
2008-11-03 22:02:15 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2008-11-03 21:06:04 +00:00
|
|
|
/* Use the correct allocator */
|
|
|
|
if (node_alloc) {
|
|
|
|
ASSERT(!(flags & __GFP_ZERO));
|
2009-11-12 23:11:24 +00:00
|
|
|
ptr = kmalloc_node_nofail(size, flags, node);
|
2008-11-03 21:06:04 +00:00
|
|
|
} else if (flags & __GFP_ZERO) {
|
2009-11-12 23:11:24 +00:00
|
|
|
ptr = kzalloc_nofail(size, flags & ~__GFP_ZERO);
|
2008-11-03 21:06:04 +00:00
|
|
|
} else {
|
2009-11-12 23:11:24 +00:00
|
|
|
ptr = kmalloc_nofail(size, flags);
|
2008-11-03 21:06:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(ptr == NULL)) {
|
2008-11-03 22:02:15 +00:00
|
|
|
kfree(dptr->kd_func);
|
2008-11-03 21:06:04 +00:00
|
|
|
kfree(dptr);
|
2014-11-05 22:30:35 +00:00
|
|
|
printk(KERN_WARNING "kmem_alloc(%llu, 0x%x) "
|
|
|
|
"at %s:%d failed (%lld/%llu)\n",
|
2010-06-16 22:57:04 +00:00
|
|
|
(unsigned long long) size, flags, func, line,
|
2009-12-04 23:54:12 +00:00
|
|
|
kmem_alloc_used_read(), kmem_alloc_max);
|
2008-11-03 21:06:04 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2009-12-04 23:54:12 +00:00
|
|
|
kmem_alloc_used_add(size);
|
|
|
|
if (unlikely(kmem_alloc_used_read() > kmem_alloc_max))
|
|
|
|
kmem_alloc_max = kmem_alloc_used_read();
|
2008-11-03 21:06:04 +00:00
|
|
|
|
|
|
|
INIT_HLIST_NODE(&dptr->kd_hlist);
|
|
|
|
INIT_LIST_HEAD(&dptr->kd_list);
|
|
|
|
|
|
|
|
dptr->kd_addr = ptr;
|
|
|
|
dptr->kd_size = size;
|
|
|
|
dptr->kd_line = line;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&kmem_lock, irq_flags);
|
2013-07-03 17:14:35 +00:00
|
|
|
hlist_add_head(&dptr->kd_hlist,
|
2008-11-03 21:06:04 +00:00
|
|
|
&kmem_table[hash_ptr(ptr, KMEM_HASH_BITS)]);
|
|
|
|
list_add_tail(&dptr->kd_list, &kmem_list);
|
|
|
|
spin_unlock_irqrestore(&kmem_lock, irq_flags);
|
|
|
|
}
|
|
|
|
out:
|
2014-11-05 22:30:35 +00:00
|
|
|
return (ptr);
|
2008-11-03 21:06:04 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(kmem_alloc_track);
|
|
|
|
|
|
|
|
void
|
2012-06-25 17:22:21 +00:00
|
|
|
kmem_free_track(const void *ptr, size_t size)
|
2008-11-03 21:06:04 +00:00
|
|
|
{
|
|
|
|
kmem_debug_t *dptr;
|
|
|
|
|
|
|
|
ASSERTF(ptr || size > 0, "ptr: %p, size: %llu", ptr,
|
|
|
|
(unsigned long long) size);
|
|
|
|
|
2010-07-26 22:47:55 +00:00
|
|
|
/* Must exist in hash due to kmem_alloc() */
|
2014-11-05 22:30:35 +00:00
|
|
|
dptr = kmem_del_init(&kmem_lock, kmem_table, KMEM_HASH_BITS, ptr);
|
2010-07-26 22:47:55 +00:00
|
|
|
ASSERT(dptr);
|
2008-11-03 21:06:04 +00:00
|
|
|
|
|
|
|
/* Size must match */
|
|
|
|
ASSERTF(dptr->kd_size == size, "kd_size (%llu) != size (%llu), "
|
|
|
|
"kd_func = %s, kd_line = %d\n", (unsigned long long) dptr->kd_size,
|
|
|
|
(unsigned long long) size, dptr->kd_func, dptr->kd_line);
|
|
|
|
|
2009-12-04 23:54:12 +00:00
|
|
|
kmem_alloc_used_sub(size);
|
2008-11-03 22:02:15 +00:00
|
|
|
kfree(dptr->kd_func);
|
|
|
|
|
2013-07-03 17:14:35 +00:00
|
|
|
memset((void *)dptr, 0x5a, sizeof(kmem_debug_t));
|
2008-11-03 21:06:04 +00:00
|
|
|
kfree(dptr);
|
|
|
|
|
2013-07-03 17:14:35 +00:00
|
|
|
memset((void *)ptr, 0x5a, size);
|
2008-11-03 21:06:04 +00:00
|
|
|
kfree(ptr);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(kmem_free_track);
|
|
|
|
|
|
|
|
# else /* DEBUG_KMEM_TRACKING */
|
|
|
|
|
|
|
|
void *
|
|
|
|
kmem_alloc_debug(size_t size, int flags, const char *func, int line,
|
|
|
|
int node_alloc, int node)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
|
2010-07-26 22:47:55 +00:00
|
|
|
/*
|
|
|
|
* Marked unlikely because we should never be doing this,
|
|
|
|
* we tolerate to up 2 pages but a single page is best.
|
|
|
|
*/
|
2010-05-20 21:16:59 +00:00
|
|
|
if (unlikely((size > PAGE_SIZE * 2) && !(flags & KM_NODEBUG))) {
|
2014-11-05 22:30:35 +00:00
|
|
|
printk(KERN_WARNING
|
2010-07-26 22:47:55 +00:00
|
|
|
"large kmem_alloc(%llu, 0x%x) at %s:%d (%lld/%llu)\n",
|
2014-11-05 22:30:35 +00:00
|
|
|
(unsigned long long)size, flags, func, line,
|
|
|
|
(unsigned long long)kmem_alloc_used_read(), kmem_alloc_max);
|
|
|
|
spl_dumpstack();
|
2010-05-19 23:53:13 +00:00
|
|
|
}
|
2008-11-03 21:06:04 +00:00
|
|
|
|
|
|
|
/* Use the correct allocator */
|
|
|
|
if (node_alloc) {
|
|
|
|
ASSERT(!(flags & __GFP_ZERO));
|
2009-11-12 23:11:24 +00:00
|
|
|
ptr = kmalloc_node_nofail(size, flags, node);
|
2008-11-03 21:06:04 +00:00
|
|
|
} else if (flags & __GFP_ZERO) {
|
2009-11-12 23:11:24 +00:00
|
|
|
ptr = kzalloc_nofail(size, flags & (~__GFP_ZERO));
|
2008-11-03 21:06:04 +00:00
|
|
|
} else {
|
2009-11-12 23:11:24 +00:00
|
|
|
ptr = kmalloc_nofail(size, flags);
|
2008-11-03 21:06:04 +00:00
|
|
|
}
|
|
|
|
|
2010-07-26 22:47:55 +00:00
|
|
|
if (unlikely(ptr == NULL)) {
|
2014-11-05 22:30:35 +00:00
|
|
|
printk(KERN_WARNING
|
2010-06-16 22:57:04 +00:00
|
|
|
"kmem_alloc(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n",
|
2014-11-05 22:30:35 +00:00
|
|
|
(unsigned long long)size, flags, func, line,
|
|
|
|
(unsigned long long)kmem_alloc_used_read(), kmem_alloc_max);
|
2008-11-03 21:06:04 +00:00
|
|
|
} else {
|
2009-12-04 23:54:12 +00:00
|
|
|
kmem_alloc_used_add(size);
|
|
|
|
if (unlikely(kmem_alloc_used_read() > kmem_alloc_max))
|
|
|
|
kmem_alloc_max = kmem_alloc_used_read();
|
2008-11-03 21:06:04 +00:00
|
|
|
}
|
2010-07-26 22:47:55 +00:00
|
|
|
|
2014-11-05 22:30:35 +00:00
|
|
|
return (ptr);
|
2008-11-03 21:06:04 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(kmem_alloc_debug);
|
|
|
|
|
|
|
|
void
|
2012-06-25 17:22:21 +00:00
|
|
|
kmem_free_debug(const void *ptr, size_t size)
|
2008-11-03 21:06:04 +00:00
|
|
|
{
|
2014-11-05 22:30:35 +00:00
|
|
|
ASSERT(ptr || size > 0);
|
2009-12-04 23:54:12 +00:00
|
|
|
kmem_alloc_used_sub(size);
|
2008-11-03 21:06:04 +00:00
|
|
|
kfree(ptr);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(kmem_free_debug);
|
|
|
|
|
|
|
|
# endif /* DEBUG_KMEM_TRACKING */
|
|
|
|
#endif /* DEBUG_KMEM */
|
|
|
|
|
2014-12-08 18:04:42 +00:00
|
|
|
#if defined(DEBUG_KMEM) && defined(DEBUG_KMEM_TRACKING)
|
|
|
|
static char *
|
|
|
|
spl_sprintf_addr(kmem_debug_t *kd, char *str, int len, int min)
|
2008-06-28 05:04:46 +00:00
|
|
|
{
|
2014-12-08 18:04:42 +00:00
|
|
|
int size = ((len - 1) < kd->kd_size) ? (len - 1) : kd->kd_size;
|
|
|
|
int i, flag = 1;
|
2008-06-28 05:04:46 +00:00
|
|
|
|
2014-12-08 18:04:42 +00:00
|
|
|
ASSERT(str != NULL && len >= 17);
|
|
|
|
memset(str, 0, len);
|
2008-06-28 05:04:46 +00:00
|
|
|
|
2014-12-08 18:04:42 +00:00
|
|
|
/* Check for a fully printable string, and while we are at
|
|
|
|
* it place the printable characters in the passed buffer. */
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
str[i] = ((char *)(kd->kd_addr))[i];
|
|
|
|
if (isprint(str[i])) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
/* Minimum number of printable characters found
|
|
|
|
* to make it worthwhile to print this as ascii. */
|
|
|
|
if (i > min)
|
|
|
|
break;
|
2009-11-13 19:12:43 +00:00
|
|
|
|
2014-12-08 18:04:42 +00:00
|
|
|
flag = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-05-01 22:49:07 +00:00
|
|
|
|
2014-12-08 18:04:42 +00:00
|
|
|
if (!flag) {
|
|
|
|
sprintf(str, "%02x%02x%02x%02x%02x%02x%02x%02x",
|
|
|
|
*((uint8_t *)kd->kd_addr),
|
|
|
|
*((uint8_t *)kd->kd_addr + 2),
|
|
|
|
*((uint8_t *)kd->kd_addr + 4),
|
|
|
|
*((uint8_t *)kd->kd_addr + 6),
|
|
|
|
*((uint8_t *)kd->kd_addr + 8),
|
|
|
|
*((uint8_t *)kd->kd_addr + 10),
|
|
|
|
*((uint8_t *)kd->kd_addr + 12),
|
|
|
|
*((uint8_t *)kd->kd_addr + 14));
|
|
|
|
}
|
2009-11-13 19:12:43 +00:00
|
|
|
|
2014-12-08 18:04:42 +00:00
|
|
|
return str;
|
2009-11-13 19:12:43 +00:00
|
|
|
}
|
|
|
|
|
2014-12-08 18:04:42 +00:00
|
|
|
static int
|
|
|
|
spl_kmem_init_tracking(struct list_head *list, spinlock_t *lock, int size)
|
2009-11-13 19:12:43 +00:00
|
|
|
{
|
2014-12-08 18:04:42 +00:00
|
|
|
int i;
|
2009-11-13 19:12:43 +00:00
|
|
|
|
2014-12-08 18:04:42 +00:00
|
|
|
spin_lock_init(lock);
|
|
|
|
INIT_LIST_HEAD(list);
|
2009-11-13 19:12:43 +00:00
|
|
|
|
2014-12-08 18:04:42 +00:00
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
INIT_HLIST_HEAD(&kmem_table[i]);
|
2009-11-13 19:12:43 +00:00
|
|
|
|
2014-12-08 18:04:42 +00:00
|
|
|
return (0);
|
2008-06-28 05:04:46 +00:00
|
|
|
}
|
|
|
|
|
2014-12-08 18:04:42 +00:00
|
|
|
static void
|
|
|
|
spl_kmem_fini_tracking(struct list_head *list, spinlock_t *lock)
|
2008-06-28 05:04:46 +00:00
|
|
|
{
|
2014-12-08 18:04:42 +00:00
|
|
|
unsigned long flags;
|
|
|
|
kmem_debug_t *kd;
|
|
|
|
char str[17];
|
2008-07-01 03:28:54 +00:00
|
|
|
|
2014-12-08 18:04:42 +00:00
|
|
|
spin_lock_irqsave(lock, flags);
|
|
|
|
if (!list_empty(list))
|
|
|
|
printk(KERN_WARNING "%-16s %-5s %-16s %s:%s\n", "address",
|
|
|
|
"size", "data", "func", "line");
|
2008-06-28 05:04:46 +00:00
|
|
|
|
2014-12-08 18:04:42 +00:00
|
|
|
list_for_each_entry(kd, list, kd_list)
|
|
|
|
printk(KERN_WARNING "%p %-5d %-16s %s:%d\n", kd->kd_addr,
|
|
|
|
(int)kd->kd_size, spl_sprintf_addr(kd, str, 17, 8),
|
|
|
|
kd->kd_func, kd->kd_line);
|
2008-06-28 05:04:46 +00:00
|
|
|
|
2014-12-08 18:04:42 +00:00
|
|
|
spin_unlock_irqrestore(lock, flags);
|
2008-06-28 05:04:46 +00:00
|
|
|
}
|
2014-12-08 18:04:42 +00:00
|
|
|
#else /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
|
|
|
|
#define spl_kmem_init_tracking(list, lock, size)
|
|
|
|
#define spl_kmem_fini_tracking(list, lock)
|
|
|
|
#endif /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
|
2008-06-28 05:04:46 +00:00
|
|
|
|
2014-12-08 18:04:42 +00:00
|
|
|
int
|
|
|
|
spl_kmem_init(void)
|
2009-01-31 04:54:49 +00:00
|
|
|
{
|
2014-12-08 18:04:42 +00:00
|
|
|
int rc = 0;
|
2008-05-06 20:38:28 +00:00
|
|
|
|
2014-12-08 18:04:42 +00:00
|
|
|
#ifdef DEBUG_KMEM
|
|
|
|
kmem_alloc_used_set(0);
|
|
|
|
spl_kmem_init_tracking(&kmem_list, &kmem_lock, KMEM_TABLE_SIZE);
|
|
|
|
#endif
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2014-12-08 18:04:42 +00:00
|
|
|
return (rc);
|
2008-06-13 23:41:06 +00:00
|
|
|
}
|
2008-05-06 20:38:28 +00:00
|
|
|
|
2014-12-08 18:04:42 +00:00
|
|
|
void
|
|
|
|
spl_kmem_fini(void)
|
2008-06-13 23:41:06 +00:00
|
|
|
{
|
2008-06-27 21:40:11 +00:00
|
|
|
#ifdef DEBUG_KMEM
|
|
|
|
/* Display all unreclaimed memory addresses, including the
|
|
|
|
* allocation size and the first few bytes of what's located
|
|
|
|
* at that address to aid in debugging. Performance is not
|
|
|
|
* a serious concern here since it is module unload time. */
|
2009-12-04 23:54:12 +00:00
|
|
|
if (kmem_alloc_used_read() != 0)
|
2014-11-05 22:30:35 +00:00
|
|
|
printk(KERN_WARNING "kmem leaked %ld/%llu bytes\n",
|
2010-06-16 22:57:04 +00:00
|
|
|
kmem_alloc_used_read(), kmem_alloc_max);
|
2008-06-27 21:40:11 +00:00
|
|
|
|
|
|
|
spl_kmem_fini_tracking(&kmem_list, &kmem_lock);
|
|
|
|
#endif /* DEBUG_KMEM */
|
2008-03-18 04:56:43 +00:00
|
|
|
}
|