2008-02-28 00:52:31 +00:00
|
|
|
#ifndef _SPL_KMEM_H
|
|
|
|
#define _SPL_KMEM_H
|
2008-02-26 20:36:04 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2008-03-14 19:04:41 +00:00
|
|
|
#define DEBUG_KMEM
|
2008-02-26 20:36:04 +00:00
|
|
|
#undef DEBUG_KMEM_UNIMPLEMENTED
|
|
|
|
|
2008-02-27 19:09:51 +00:00
|
|
|
#include <linux/module.h>
|
2008-02-26 20:36:04 +00:00
|
|
|
#include <linux/slab.h>
|
2008-03-14 19:04:41 +00:00
|
|
|
#include <linux/vmalloc.h>
|
2008-02-26 20:36:04 +00:00
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/spinlock.h>
|
2008-05-06 20:38:28 +00:00
|
|
|
#include <linux/rwsem.h>
|
|
|
|
#include <linux/hash.h>
|
|
|
|
#include <linux/ctype.h>
|
2008-04-21 17:29:47 +00:00
|
|
|
#include <sys/debug.h>
|
2008-02-26 20:36:04 +00:00
|
|
|
/*
|
|
|
|
* Memory allocation interfaces
|
|
|
|
*/
|
|
|
|
#define KM_SLEEP GFP_KERNEL
|
|
|
|
#define KM_NOSLEEP GFP_ATOMIC
|
|
|
|
#undef KM_PANIC /* No linux analog */
|
|
|
|
#define KM_PUSHPAGE (GFP_KERNEL | GFP_HIGH)
|
|
|
|
#define KM_VMFLAGS GFP_LEVEL_MASK
|
|
|
|
#define KM_FLAGS __GFP_BITS_MASK
|
|
|
|
|
|
|
|
#ifdef DEBUG_KMEM
|
2008-03-14 20:56:26 +00:00
|
|
|
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;
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2008-05-06 20:38:28 +00:00
|
|
|
#define KMEM_HASH_BITS 10
|
|
|
|
#define KMEM_TABLE_SIZE (1 << KMEM_HASH_BITS)
|
|
|
|
|
|
|
|
extern struct hlist_head kmem_table[KMEM_TABLE_SIZE];
|
|
|
|
extern struct list_head kmem_list;
|
|
|
|
extern spinlock_t kmem_lock;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
static __inline__ kmem_debug_t *
|
|
|
|
__kmem_del_init(void *addr)
|
|
|
|
{
|
|
|
|
struct hlist_head *head;
|
|
|
|
struct hlist_node *node;
|
|
|
|
struct kmem_debug *p;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&kmem_lock, flags);
|
|
|
|
head = &kmem_table[hash_ptr(addr, KMEM_HASH_BITS)];
|
|
|
|
hlist_for_each_entry_rcu(p, node, head, kd_hlist) {
|
|
|
|
if (p->kd_addr == addr) {
|
|
|
|
hlist_del_init(&p->kd_hlist);
|
|
|
|
list_del_init(&p->kd_list);
|
|
|
|
spin_unlock_irqrestore(&kmem_lock, flags);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&kmem_lock, flags);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-02-26 20:36:04 +00:00
|
|
|
#define __kmem_alloc(size, flags, allocator) \
|
2008-05-06 20:38:28 +00:00
|
|
|
({ void *_ptr_ = NULL; \
|
|
|
|
kmem_debug_t *_dptr_; \
|
|
|
|
unsigned long _flags_; \
|
2008-02-26 20:36:04 +00:00
|
|
|
\
|
2008-05-06 20:38:28 +00:00
|
|
|
_dptr_ = (kmem_debug_t *)kmalloc(sizeof(kmem_debug_t), (flags)); \
|
|
|
|
if (_dptr_ == NULL) { \
|
2008-04-24 17:41:23 +00:00
|
|
|
__CDEBUG_LIMIT(S_KMEM, D_WARNING, "Warning " \
|
2008-05-06 20:38:28 +00:00
|
|
|
"kmem_alloc(%d, 0x%x) debug failed\n", \
|
|
|
|
sizeof(kmem_debug_t), (int)(flags)); \
|
2008-03-14 20:56:26 +00:00
|
|
|
} else { \
|
2008-05-06 20:38:28 +00:00
|
|
|
/* Marked unlikely because we should never be doing this */ \
|
|
|
|
if (unlikely((size) > (PAGE_SIZE * 4)) && kmem_warning_flag) \
|
|
|
|
__CDEBUG_LIMIT(S_KMEM, D_WARNING, "Warning large " \
|
|
|
|
"kmem_alloc(%d, 0x%x) (%ld/%ld)\n", \
|
|
|
|
(int)(size), (int)(flags), \
|
|
|
|
atomic64_read(&kmem_alloc_used), \
|
|
|
|
kmem_alloc_max); \
|
|
|
|
\
|
|
|
|
_ptr_ = (void *)allocator((size), (flags)); \
|
|
|
|
if (_ptr_ == NULL) { \
|
|
|
|
kfree(_dptr_); \
|
|
|
|
__CDEBUG_LIMIT(S_KMEM, D_WARNING, "Warning " \
|
|
|
|
"kmem_alloc(%d, 0x%x) failed (%ld/" \
|
|
|
|
"%ld)\n", (int)(size), (int)(flags), \
|
|
|
|
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); \
|
|
|
|
\
|
|
|
|
INIT_HLIST_NODE(&_dptr_->kd_hlist); \
|
|
|
|
INIT_LIST_HEAD(&_dptr_->kd_list); \
|
|
|
|
_dptr_->kd_addr = _ptr_; \
|
|
|
|
_dptr_->kd_size = (size); \
|
|
|
|
_dptr_->kd_func = __FUNCTION__; \
|
|
|
|
_dptr_->kd_line = __LINE__; \
|
|
|
|
spin_lock_irqsave(&kmem_lock, _flags_); \
|
|
|
|
hlist_add_head_rcu(&_dptr_->kd_hlist, \
|
|
|
|
&kmem_table[hash_ptr(_ptr_, KMEM_HASH_BITS)]);\
|
|
|
|
list_add_tail(&_dptr_->kd_list, &kmem_list); \
|
|
|
|
spin_unlock_irqrestore(&kmem_lock, _flags_); \
|
|
|
|
\
|
|
|
|
__CDEBUG_LIMIT(S_KMEM, D_INFO, "kmem_alloc(" \
|
|
|
|
"%d, 0x%x) = %p (%ld/%ld)\n", \
|
|
|
|
(int)(size), (int)(flags), _ptr_, \
|
|
|
|
atomic64_read(&kmem_alloc_used), \
|
|
|
|
kmem_alloc_max); \
|
|
|
|
} \
|
2008-02-26 20:36:04 +00:00
|
|
|
} \
|
|
|
|
\
|
|
|
|
_ptr_; \
|
|
|
|
})
|
|
|
|
|
2008-03-31 23:04:07 +00:00
|
|
|
#define kmem_alloc(size, flags) __kmem_alloc((size), (flags), kmalloc)
|
|
|
|
#define kmem_zalloc(size, flags) __kmem_alloc((size), (flags), kzalloc)
|
2008-02-26 20:36:04 +00:00
|
|
|
|
|
|
|
#define kmem_free(ptr, size) \
|
|
|
|
({ \
|
2008-05-06 20:38:28 +00:00
|
|
|
kmem_debug_t *_dptr_; \
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT((ptr) || (size > 0)); \
|
2008-05-06 20:38:28 +00:00
|
|
|
\
|
|
|
|
_dptr_ = __kmem_del_init(ptr); \
|
|
|
|
ASSERT(_dptr_); /* Must exist in hash due to kmem_alloc() */ \
|
|
|
|
ASSERTF(_dptr_->kd_size == (size), "kd_size (%d) != size (%d), " \
|
|
|
|
"kd_func = %s, kd_line = %d\n", _dptr_->kd_size, (size), \
|
|
|
|
_dptr_->kd_func, _dptr_->kd_line); /* Size must match */ \
|
2008-03-14 20:56:26 +00:00
|
|
|
atomic64_sub((size), &kmem_alloc_used); \
|
2008-05-05 20:18:49 +00:00
|
|
|
__CDEBUG_LIMIT(S_KMEM, D_INFO, "kmem_free(%p, %d) (%ld/%ld)\n", \
|
|
|
|
(ptr), (int)(size), atomic64_read(&kmem_alloc_used), \
|
2008-04-24 20:21:07 +00:00
|
|
|
kmem_alloc_max); \
|
2008-05-06 20:38:28 +00:00
|
|
|
\
|
|
|
|
memset(_dptr_, 0x5a, sizeof(kmem_debug_t)); \
|
|
|
|
kfree(_dptr_); \
|
|
|
|
\
|
|
|
|
memset(ptr, 0x5a, (size)); \
|
2008-02-26 20:36:04 +00:00
|
|
|
kfree(ptr); \
|
|
|
|
})
|
|
|
|
|
2008-03-14 19:04:41 +00:00
|
|
|
#define __vmem_alloc(size, flags) \
|
|
|
|
({ void *_ptr_; \
|
|
|
|
\
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(flags & KM_SLEEP); \
|
2008-03-14 19:04:41 +00:00
|
|
|
\
|
2008-04-24 17:41:23 +00:00
|
|
|
_ptr_ = (void *)__vmalloc((size), \
|
|
|
|
(((flags) | __GFP_HIGHMEM) & ~__GFP_ZERO), \
|
|
|
|
PAGE_KERNEL); \
|
2008-03-14 19:04:41 +00:00
|
|
|
if (_ptr_ == NULL) { \
|
2008-04-24 17:41:23 +00:00
|
|
|
__CDEBUG_LIMIT(S_KMEM, D_WARNING, "Warning " \
|
2008-04-24 20:21:07 +00:00
|
|
|
"vmem_alloc(%d, 0x%x) failed (%ld/%ld)\n", \
|
|
|
|
(int)(size), (int)(flags), \
|
2008-04-24 17:41:23 +00:00
|
|
|
atomic64_read(&vmem_alloc_used), \
|
|
|
|
vmem_alloc_max); \
|
2008-03-14 20:56:26 +00:00
|
|
|
} else { \
|
2008-04-24 17:07:56 +00:00
|
|
|
if (flags & __GFP_ZERO) \
|
|
|
|
memset(_ptr_, 0, (size)); \
|
2008-04-24 17:41:23 +00:00
|
|
|
\
|
2008-03-14 20:56:26 +00:00
|
|
|
atomic64_add((size), &vmem_alloc_used); \
|
|
|
|
if (unlikely(atomic64_read(&vmem_alloc_used)>vmem_alloc_max)) \
|
|
|
|
vmem_alloc_max = atomic64_read(&vmem_alloc_used); \
|
2008-04-24 20:21:07 +00:00
|
|
|
\
|
2008-05-05 20:18:49 +00:00
|
|
|
__CDEBUG_LIMIT(S_KMEM, D_INFO, "vmem_alloc(%d, 0x%x) = %p " \
|
2008-04-24 20:21:07 +00:00
|
|
|
"(%ld/%ld)\n", (int)(size), (int)(flags), \
|
2008-05-05 20:18:49 +00:00
|
|
|
_ptr_, atomic64_read(&vmem_alloc_used), \
|
2008-04-24 20:21:07 +00:00
|
|
|
vmem_alloc_max); \
|
2008-03-14 19:04:41 +00:00
|
|
|
} \
|
|
|
|
\
|
|
|
|
_ptr_; \
|
|
|
|
})
|
|
|
|
|
2008-03-31 23:04:07 +00:00
|
|
|
#define vmem_alloc(size, flags) __vmem_alloc((size), (flags))
|
|
|
|
#define vmem_zalloc(size, flags) __vmem_alloc((size), ((flags) | \
|
|
|
|
__GFP_ZERO))
|
2008-03-14 19:04:41 +00:00
|
|
|
|
|
|
|
#define vmem_free(ptr, size) \
|
|
|
|
({ \
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT((ptr) || (size > 0)); \
|
2008-03-14 20:56:26 +00:00
|
|
|
atomic64_sub((size), &vmem_alloc_used); \
|
2008-05-05 20:18:49 +00:00
|
|
|
__CDEBUG_LIMIT(S_KMEM, D_INFO, "vmem_free(%p, %d) (%ld/%ld)\n", \
|
|
|
|
(ptr), (int)(size), atomic64_read(&vmem_alloc_used), \
|
2008-04-24 20:21:07 +00:00
|
|
|
vmem_alloc_max); \
|
2008-03-14 19:04:41 +00:00
|
|
|
memset(ptr, 0x5a, (size)); /* Poison */ \
|
|
|
|
vfree(ptr); \
|
|
|
|
})
|
2008-02-26 20:36:04 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2008-03-31 23:04:07 +00:00
|
|
|
#define kmem_alloc(size, flags) kmalloc((size), (flags))
|
|
|
|
#define kmem_zalloc(size, flags) kzalloc((size), (flags))
|
2008-03-07 20:48:44 +00:00
|
|
|
#define kmem_free(ptr, size) \
|
|
|
|
({ \
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT((ptr) || (size > 0)); \
|
2008-03-07 20:48:44 +00:00
|
|
|
kfree(ptr); \
|
|
|
|
})
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2008-03-31 23:04:07 +00:00
|
|
|
#define vmem_alloc(size, flags) __vmalloc((size), ((flags) | \
|
|
|
|
__GFP_HIGHMEM), PAGE_KERNEL)
|
|
|
|
#define vmem_zalloc(size, flags) __vmalloc((size), ((flags) | \
|
|
|
|
__GFP_HIGHMEM | __GFP_ZERO) \
|
|
|
|
PAGE_KERNEL)
|
2008-03-14 19:04:41 +00:00
|
|
|
#define vmem_free(ptr, size) \
|
|
|
|
({ \
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT((ptr) || (size > 0)); \
|
2008-03-14 19:04:41 +00:00
|
|
|
vfree(ptr); \
|
|
|
|
})
|
|
|
|
|
2008-02-26 20:36:04 +00:00
|
|
|
#endif /* DEBUG_KMEM */
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG_KMEM_UNIMPLEMENTED
|
|
|
|
static __inline__ void *
|
|
|
|
kmem_alloc_tryhard(size_t size, size_t *alloc_size, int kmflags)
|
|
|
|
{
|
|
|
|
#error "kmem_alloc_tryhard() not implemented"
|
|
|
|
}
|
|
|
|
#endif /* DEBUG_KMEM_UNIMPLEMENTED */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Slab allocation interfaces
|
|
|
|
*/
|
|
|
|
#undef KMC_NOTOUCH /* No linux analog */
|
2008-03-04 19:38:27 +00:00
|
|
|
#define KMC_NODEBUG 0x00000000 /* Default behavior */
|
2008-02-26 20:36:04 +00:00
|
|
|
#define KMC_NOMAGAZINE /* No linux analog */
|
|
|
|
#define KMC_NOHASH /* No linux analog */
|
|
|
|
#define KMC_QCACHE /* No linux analog */
|
|
|
|
|
|
|
|
#define KMC_REAP_CHUNK 256
|
|
|
|
#define KMC_DEFAULT_SEEKS DEFAULT_SEEKS
|
|
|
|
|
|
|
|
/* Defined by linux slab.h
|
|
|
|
* typedef struct kmem_cache_s kmem_cache_t;
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* No linux analog
|
|
|
|
* extern int kmem_ready;
|
|
|
|
* extern pgcnt_t kmem_reapahead;
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef DEBUG_KMEM_UNIMPLEMENTED
|
|
|
|
static __inline__ void kmem_init(void) {
|
|
|
|
#error "kmem_init() not implemented"
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ void kmem_thread_init(void) {
|
|
|
|
#error "kmem_thread_init() not implemented"
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ void kmem_mp_init(void) {
|
|
|
|
#error "kmem_mp_init() not implemented"
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ void kmem_reap_idspace(void) {
|
|
|
|
#error "kmem_reap_idspace() not implemented"
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ size_t kmem_avail(void) {
|
|
|
|
#error "kmem_avail() not implemented"
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ size_t kmem_maxavail(void) {
|
|
|
|
#error "kmem_maxavail() not implemented"
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ uint64_t kmem_cache_stat(kmem_cache_t *cache) {
|
|
|
|
#error "kmem_cache_stat() not implemented"
|
|
|
|
}
|
|
|
|
#endif /* DEBUG_KMEM_UNIMPLEMENTED */
|
|
|
|
|
|
|
|
/* XXX - Used by arc.c to adjust its memory footprint. We may want
|
|
|
|
* to use this hook in the future to adjust behavior based on
|
|
|
|
* debug levels. For now it's safe to always return 0.
|
|
|
|
*/
|
|
|
|
static __inline__ int
|
|
|
|
kmem_debugging(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef int (*kmem_constructor_t)(void *, void *, int);
|
|
|
|
typedef void (*kmem_destructor_t)(void *, void *);
|
|
|
|
typedef void (*kmem_reclaim_t)(void *);
|
|
|
|
|
2008-03-14 20:56:26 +00:00
|
|
|
extern int kmem_set_warning(int flag);
|
|
|
|
|
2008-02-27 19:09:51 +00:00
|
|
|
extern kmem_cache_t *
|
2008-02-26 20:36:04 +00:00
|
|
|
__kmem_cache_create(char *name, size_t size, size_t align,
|
2008-02-27 19:09:51 +00:00
|
|
|
kmem_constructor_t constructor,
|
|
|
|
kmem_destructor_t destructor,
|
|
|
|
kmem_reclaim_t reclaim,
|
2008-02-26 20:36:04 +00:00
|
|
|
void *priv, void *vmp, int flags);
|
|
|
|
|
2008-03-18 23:20:30 +00:00
|
|
|
int
|
2008-02-27 19:09:51 +00:00
|
|
|
extern __kmem_cache_destroy(kmem_cache_t *cache);
|
|
|
|
|
|
|
|
void
|
|
|
|
extern __kmem_reap(void);
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2008-03-18 04:56:43 +00:00
|
|
|
int kmem_init(void);
|
|
|
|
void kmem_fini(void);
|
|
|
|
|
2008-02-26 20:36:04 +00:00
|
|
|
#define kmem_cache_create(name,size,align,ctor,dtor,rclm,priv,vmp,flags) \
|
|
|
|
__kmem_cache_create(name,size,align,ctor,dtor,rclm,priv,vmp,flags)
|
|
|
|
#define kmem_cache_destroy(cache) __kmem_cache_destroy(cache)
|
|
|
|
#define kmem_cache_alloc(cache, flags) kmem_cache_alloc(cache, flags)
|
|
|
|
#define kmem_cache_free(cache, ptr) kmem_cache_free(cache, ptr)
|
|
|
|
#define kmem_cache_reap_now(cache) kmem_cache_shrink(cache)
|
2008-02-27 19:09:51 +00:00
|
|
|
#define kmem_reap() __kmem_reap()
|
2008-02-26 20:36:04 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-02-28 00:52:31 +00:00
|
|
|
#endif /* _SPL_KMEM_H */
|