lib: Add debugfs file for tracking memory allocations

Add caller information to memory allocation calls and
create /sys/kernel/debug/mempool/map to show the current set of
allocations across all memory pools.

Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
diff --git a/arch/arm/mach-msm/memory.c b/arch/arm/mach-msm/memory.c
index 2d01795..dc146b3 100644
--- a/arch/arm/mach-msm/memory.c
+++ b/arch/arm/mach-msm/memory.c
@@ -361,7 +361,8 @@
 unsigned long allocate_contiguous_ebi_nomap(unsigned long size,
 	unsigned long align)
 {
-	return allocate_contiguous_memory_nomap(size, get_ebi_memtype(), align);
+	return _allocate_contiguous_memory_nomap(size, get_ebi_memtype(),
+		align, __builtin_return_address(0));
 }
 EXPORT_SYMBOL(allocate_contiguous_ebi_nomap);
 
@@ -402,10 +403,12 @@
 		return -EINVAL;
 	}
 
-	paddr = allocate_contiguous_memory_nomap(size, memtype, align);
+	paddr = _allocate_contiguous_memory_nomap(size, memtype, align,
+		__builtin_return_address(0));
+
 	if (!paddr && pmem_memtype == PMEM_MEMTYPE_SMI)
-		paddr = allocate_contiguous_memory_nomap(size,
-			ebi1_memtype, align);
+		paddr = _allocate_contiguous_memory_nomap(size,
+			ebi1_memtype, align, __builtin_return_address(0));
 
 	if (!paddr)
 		return -ENOMEM;
diff --git a/include/linux/memory_alloc.h b/include/linux/memory_alloc.h
index 88e64ce..e7049f8 100644
--- a/include/linux/memory_alloc.h
+++ b/include/linux/memory_alloc.h
@@ -23,6 +23,7 @@
 	unsigned long paddr;
 	unsigned long size;
 	unsigned long free;
+	unsigned int id;
 };
 
 struct alloc {
@@ -31,6 +32,7 @@
 	unsigned long paddr;
 	struct mem_pool *mpool;
 	unsigned long len;
+	void *caller;
 };
 
 struct mem_pool *initialize_memory_pool(unsigned long start,
@@ -39,6 +41,9 @@
 void *allocate_contiguous_memory(unsigned long size,
 	int mem_type, unsigned long align, int cached);
 
+unsigned long _allocate_contiguous_memory_nomap(unsigned long size,
+	int mem_type, unsigned long align, void *caller);
+
 unsigned long allocate_contiguous_memory_nomap(unsigned long size,
 	int mem_type, unsigned long align);
 
diff --git a/lib/memory_alloc.c b/lib/memory_alloc.c
index 2e020de..d931e14 100644
--- a/lib/memory_alloc.c
+++ b/lib/memory_alloc.c
@@ -19,6 +19,9 @@
 #include <linux/module.h>
 #include <linux/err.h>
 #include <linux/log2.h>
+#include <linux/debugfs.h>
+#include <linux/seq_file.h>
+
 
 #define MAX_MEMPOOLS 8
 
@@ -28,6 +31,59 @@
 static struct rb_root alloc_root;
 static struct mutex alloc_mutex;
 
+static void *s_start(struct seq_file *m, loff_t *pos)
+	__acquires(&alloc_mutex)
+{
+	loff_t n = *pos;
+	struct rb_node *r;
+
+	mutex_lock(&alloc_mutex);
+	r = rb_first(&alloc_root);
+
+	while (n > 0 && r) {
+		n--;
+		r = rb_next(r);
+	}
+	if (!n)
+		return r;
+	return NULL;
+}
+
+static void *s_next(struct seq_file *m, void *p, loff_t *pos)
+{
+	struct rb_node *r = p;
+	++*pos;
+	return rb_next(r);
+}
+
+static void s_stop(struct seq_file *m, void *p)
+	__releases(&alloc_mutex)
+{
+	mutex_unlock(&alloc_mutex);
+}
+
+static int s_show(struct seq_file *m, void *p)
+{
+	struct rb_node *r = p;
+	struct alloc *node = rb_entry(r, struct alloc, rb_node);
+
+	seq_printf(m, "0x%lx 0x%p %ld %u %pS\n", node->paddr, node->vaddr,
+		   node->len, node->mpool->id, node->caller);
+	return 0;
+}
+
+static const struct seq_operations mempool_op = {
+	.start = s_start,
+	.next = s_next,
+	.stop = s_stop,
+	.show = s_show,
+};
+
+static int mempool_open(struct inode *inode, struct file *file)
+{
+	return seq_open(file, &mempool_op);
+}
+
 static struct alloc *find_alloc(void *addr)
 {
 	struct rb_root *root = &alloc_root;
@@ -111,7 +167,7 @@
 }
 
 static void *__alloc(struct mem_pool *mpool, unsigned long size,
-	unsigned long align, int cached)
+	unsigned long align, int cached, void *caller)
 {
 	unsigned long paddr;
 	void __iomem *vaddr;
@@ -142,6 +198,7 @@
 	node->paddr = paddr;
 	node->len = aligned_size;
 	node->mpool = mpool;
+	node->caller = caller;
 	if (add_alloc(node))
 		goto out_kfree;
 
@@ -204,6 +261,7 @@
 	mpools[id].paddr = start;
 	mpools[id].size = size;
 	mpools[id].free = size;
+	mpools[id].id = id;
 	mutex_unlock(&mpools[id].pool_mutex);
 
 	pr_info("memory pool %d (start %lx size %lx) initialized\n",
@@ -221,13 +279,14 @@
 	mpool = mem_type_to_memory_pool(mem_type);
 	if (!mpool)
 		return NULL;
-	return __alloc(mpool, aligned_size, align, cached);
+	return __alloc(mpool, aligned_size, align, cached,
+		__builtin_return_address(0));
 
 }
 EXPORT_SYMBOL_GPL(allocate_contiguous_memory);
 
-unsigned long allocate_contiguous_memory_nomap(unsigned long size,
-	int mem_type, unsigned long align)
+unsigned long _allocate_contiguous_memory_nomap(unsigned long size,
+	int mem_type, unsigned long align, void *caller)
 {
 	unsigned long paddr;
 	unsigned long aligned_size;
@@ -261,6 +320,7 @@
 	node->vaddr = (void *)paddr;
 	node->len = aligned_size;
 	node->mpool = mpool;
+	node->caller = caller;
 	if (add_alloc(node))
 		goto out_kfree;
 
@@ -272,6 +332,14 @@
 	gen_pool_free(mpool->gpool, paddr, aligned_size);
 	return 0;
 }
+EXPORT_SYMBOL_GPL(_allocate_contiguous_memory_nomap);
+
+unsigned long allocate_contiguous_memory_nomap(unsigned long size,
+	int mem_type, unsigned long align)
+{
+	return _allocate_contiguous_memory_nomap(size, mem_type, align,
+		__builtin_return_address(0));
+}
 EXPORT_SYMBOL_GPL(allocate_contiguous_memory_nomap);
 
 void free_contiguous_memory(void *addr)
@@ -314,6 +382,14 @@
 }
 EXPORT_SYMBOL_GPL(memory_pool_node_len);
 
+static const struct file_operations mempool_operations = {
+	.owner		= THIS_MODULE,
+	.open           = mempool_open,
+	.read           = seq_read,
+	.llseek         = seq_lseek,
+	.release        = seq_release_private,
+};
+
 int __init memory_pool_init(void)
 {
 	int i;
@@ -324,5 +400,26 @@
 		mutex_init(&mpools[i].pool_mutex);
 		mpools[i].gpool = NULL;
 	}
+
 	return 0;
 }
+
+static int __init debugfs_mempool_init(void)
+{
+	struct dentry *entry, *dir = debugfs_create_dir("mempool", NULL);
+
+	if (!dir) {
+		pr_err("Cannot create /sys/kernel/debug/mempool");
+		return -EINVAL;
+	}
+
+	entry = debugfs_create_file("map", S_IRUSR, dir,
+		NULL, &mempool_operations);
+
+	if (!entry)
+		pr_err("Cannot create /sys/kernel/debug/mempool/map");
+
+	return entry ? 0 : -EINVAL;
+}
+
+module_init(debugfs_mempool_init);