Back to home page

LXR

 
 

    


0001 /*
0002  * mm/kmemleak-test.c
0003  *
0004  * Copyright (C) 2008 ARM Limited
0005  * Written by Catalin Marinas <catalin.marinas@arm.com>
0006  *
0007  * This program is free software; you can redistribute it and/or modify
0008  * it under the terms of the GNU General Public License version 2 as
0009  * published by the Free Software Foundation.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program; if not, write to the Free Software
0018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0019  */
0020 
0021 #define pr_fmt(fmt) "kmemleak: " fmt
0022 
0023 #include <linux/init.h>
0024 #include <linux/kernel.h>
0025 #include <linux/module.h>
0026 #include <linux/slab.h>
0027 #include <linux/vmalloc.h>
0028 #include <linux/list.h>
0029 #include <linux/percpu.h>
0030 #include <linux/fdtable.h>
0031 
0032 #include <linux/kmemleak.h>
0033 
0034 struct test_node {
0035     long header[25];
0036     struct list_head list;
0037     long footer[25];
0038 };
0039 
0040 static LIST_HEAD(test_list);
0041 static DEFINE_PER_CPU(void *, kmemleak_test_pointer);
0042 
0043 /*
0044  * Some very simple testing. This function needs to be extended for
0045  * proper testing.
0046  */
0047 static int __init kmemleak_test_init(void)
0048 {
0049     struct test_node *elem;
0050     int i;
0051 
0052     pr_info("Kmemleak testing\n");
0053 
0054     /* make some orphan objects */
0055     pr_info("kmalloc(32) = %p\n", kmalloc(32, GFP_KERNEL));
0056     pr_info("kmalloc(32) = %p\n", kmalloc(32, GFP_KERNEL));
0057     pr_info("kmalloc(1024) = %p\n", kmalloc(1024, GFP_KERNEL));
0058     pr_info("kmalloc(1024) = %p\n", kmalloc(1024, GFP_KERNEL));
0059     pr_info("kmalloc(2048) = %p\n", kmalloc(2048, GFP_KERNEL));
0060     pr_info("kmalloc(2048) = %p\n", kmalloc(2048, GFP_KERNEL));
0061     pr_info("kmalloc(4096) = %p\n", kmalloc(4096, GFP_KERNEL));
0062     pr_info("kmalloc(4096) = %p\n", kmalloc(4096, GFP_KERNEL));
0063 #ifndef CONFIG_MODULES
0064     pr_info("kmem_cache_alloc(files_cachep) = %p\n",
0065         kmem_cache_alloc(files_cachep, GFP_KERNEL));
0066     pr_info("kmem_cache_alloc(files_cachep) = %p\n",
0067         kmem_cache_alloc(files_cachep, GFP_KERNEL));
0068 #endif
0069     pr_info("vmalloc(64) = %p\n", vmalloc(64));
0070     pr_info("vmalloc(64) = %p\n", vmalloc(64));
0071     pr_info("vmalloc(64) = %p\n", vmalloc(64));
0072     pr_info("vmalloc(64) = %p\n", vmalloc(64));
0073     pr_info("vmalloc(64) = %p\n", vmalloc(64));
0074 
0075     /*
0076      * Add elements to a list. They should only appear as orphan
0077      * after the module is removed.
0078      */
0079     for (i = 0; i < 10; i++) {
0080         elem = kzalloc(sizeof(*elem), GFP_KERNEL);
0081         pr_info("kzalloc(sizeof(*elem)) = %p\n", elem);
0082         if (!elem)
0083             return -ENOMEM;
0084         INIT_LIST_HEAD(&elem->list);
0085         list_add_tail(&elem->list, &test_list);
0086     }
0087 
0088     for_each_possible_cpu(i) {
0089         per_cpu(kmemleak_test_pointer, i) = kmalloc(129, GFP_KERNEL);
0090         pr_info("kmalloc(129) = %p\n",
0091             per_cpu(kmemleak_test_pointer, i));
0092     }
0093 
0094     return 0;
0095 }
0096 module_init(kmemleak_test_init);
0097 
0098 static void __exit kmemleak_test_exit(void)
0099 {
0100     struct test_node *elem, *tmp;
0101 
0102     /*
0103      * Remove the list elements without actually freeing the
0104      * memory.
0105      */
0106     list_for_each_entry_safe(elem, tmp, &test_list, list)
0107         list_del(&elem->list);
0108 }
0109 module_exit(kmemleak_test_exit);
0110 
0111 MODULE_LICENSE("GPL");