diff options
| -rw-r--r-- | lib/Kconfig.debug | 10 | ||||
| -rw-r--r-- | mm/Makefile | 1 | ||||
| -rw-r--r-- | mm/kmemleak-test.c | 111 |
3 files changed, 122 insertions, 0 deletions
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 5cc3506574e9..116a35051be6 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug | |||
| @@ -358,6 +358,16 @@ config DEBUG_KMEMLEAK | |||
| 358 | In order to access the kmemleak file, debugfs needs to be | 358 | In order to access the kmemleak file, debugfs needs to be |
| 359 | mounted (usually at /sys/kernel/debug). | 359 | mounted (usually at /sys/kernel/debug). |
| 360 | 360 | ||
| 361 | config DEBUG_KMEMLEAK_TEST | ||
| 362 | tristate "Simple test for the kernel memory leak detector" | ||
| 363 | depends on DEBUG_KMEMLEAK | ||
| 364 | help | ||
| 365 | Say Y or M here to build a test for the kernel memory leak | ||
| 366 | detector. This option enables a module that explicitly leaks | ||
| 367 | memory. | ||
| 368 | |||
| 369 | If unsure, say N. | ||
| 370 | |||
| 361 | config DEBUG_PREEMPT | 371 | config DEBUG_PREEMPT |
| 362 | bool "Debug preemptible kernel" | 372 | bool "Debug preemptible kernel" |
| 363 | depends on DEBUG_KERNEL && PREEMPT && (TRACE_IRQFLAGS_SUPPORT || PPC64) | 373 | depends on DEBUG_KERNEL && PREEMPT && (TRACE_IRQFLAGS_SUPPORT || PPC64) |
diff --git a/mm/Makefile b/mm/Makefile index cb84a025fdbf..e89acb090b4d 100644 --- a/mm/Makefile +++ b/mm/Makefile | |||
| @@ -39,3 +39,4 @@ endif | |||
| 39 | obj-$(CONFIG_QUICKLIST) += quicklist.o | 39 | obj-$(CONFIG_QUICKLIST) += quicklist.o |
| 40 | obj-$(CONFIG_CGROUP_MEM_RES_CTLR) += memcontrol.o page_cgroup.o | 40 | obj-$(CONFIG_CGROUP_MEM_RES_CTLR) += memcontrol.o page_cgroup.o |
| 41 | obj-$(CONFIG_DEBUG_KMEMLEAK) += kmemleak.o | 41 | obj-$(CONFIG_DEBUG_KMEMLEAK) += kmemleak.o |
| 42 | obj-$(CONFIG_DEBUG_KMEMLEAK_TEST) += kmemleak-test.o | ||
diff --git a/mm/kmemleak-test.c b/mm/kmemleak-test.c new file mode 100644 index 000000000000..d5292fc6f523 --- /dev/null +++ b/mm/kmemleak-test.c | |||
| @@ -0,0 +1,111 @@ | |||
| 1 | /* | ||
| 2 | * mm/kmemleak-test.c | ||
| 3 | * | ||
| 4 | * Copyright (C) 2008 ARM Limited | ||
| 5 | * Written by Catalin Marinas <catalin.marinas@arm.com> | ||
| 6 | * | ||
| 7 | * This program is free software; you can redistribute it and/or modify | ||
| 8 | * it under the terms of the GNU General Public License version 2 as | ||
| 9 | * published by the Free Software Foundation. | ||
| 10 | * | ||
| 11 | * This program is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | * GNU General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU General Public License | ||
| 17 | * along with this program; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include <linux/init.h> | ||
| 22 | #include <linux/kernel.h> | ||
| 23 | #include <linux/module.h> | ||
| 24 | #include <linux/slab.h> | ||
| 25 | #include <linux/vmalloc.h> | ||
| 26 | #include <linux/list.h> | ||
| 27 | #include <linux/percpu.h> | ||
| 28 | #include <linux/fdtable.h> | ||
| 29 | |||
| 30 | #include <linux/kmemleak.h> | ||
| 31 | |||
| 32 | struct test_node { | ||
| 33 | long header[25]; | ||
| 34 | struct list_head list; | ||
| 35 | long footer[25]; | ||
| 36 | }; | ||
| 37 | |||
| 38 | static LIST_HEAD(test_list); | ||
| 39 | static DEFINE_PER_CPU(void *, test_pointer); | ||
| 40 | |||
| 41 | /* | ||
| 42 | * Some very simple testing. This function needs to be extended for | ||
| 43 | * proper testing. | ||
| 44 | */ | ||
| 45 | static int __init kmemleak_test_init(void) | ||
| 46 | { | ||
| 47 | struct test_node *elem; | ||
| 48 | int i; | ||
| 49 | |||
| 50 | printk(KERN_INFO "Kmemleak testing\n"); | ||
| 51 | |||
| 52 | /* make some orphan objects */ | ||
| 53 | pr_info("kmemleak: kmalloc(32) = %p\n", kmalloc(32, GFP_KERNEL)); | ||
| 54 | pr_info("kmemleak: kmalloc(32) = %p\n", kmalloc(32, GFP_KERNEL)); | ||
| 55 | pr_info("kmemleak: kmalloc(1024) = %p\n", kmalloc(1024, GFP_KERNEL)); | ||
| 56 | pr_info("kmemleak: kmalloc(1024) = %p\n", kmalloc(1024, GFP_KERNEL)); | ||
| 57 | pr_info("kmemleak: kmalloc(2048) = %p\n", kmalloc(2048, GFP_KERNEL)); | ||
| 58 | pr_info("kmemleak: kmalloc(2048) = %p\n", kmalloc(2048, GFP_KERNEL)); | ||
| 59 | pr_info("kmemleak: kmalloc(4096) = %p\n", kmalloc(4096, GFP_KERNEL)); | ||
| 60 | pr_info("kmemleak: kmalloc(4096) = %p\n", kmalloc(4096, GFP_KERNEL)); | ||
| 61 | #ifndef CONFIG_MODULES | ||
| 62 | pr_info("kmemleak: kmem_cache_alloc(files_cachep) = %p\n", | ||
| 63 | kmem_cache_alloc(files_cachep, GFP_KERNEL)); | ||
| 64 | pr_info("kmemleak: kmem_cache_alloc(files_cachep) = %p\n", | ||
| 65 | kmem_cache_alloc(files_cachep, GFP_KERNEL)); | ||
| 66 | #endif | ||
| 67 | pr_info("kmemleak: vmalloc(64) = %p\n", vmalloc(64)); | ||
| 68 | pr_info("kmemleak: vmalloc(64) = %p\n", vmalloc(64)); | ||
| 69 | pr_info("kmemleak: vmalloc(64) = %p\n", vmalloc(64)); | ||
| 70 | pr_info("kmemleak: vmalloc(64) = %p\n", vmalloc(64)); | ||
| 71 | pr_info("kmemleak: vmalloc(64) = %p\n", vmalloc(64)); | ||
| 72 | |||
| 73 | /* | ||
| 74 | * Add elements to a list. They should only appear as orphan | ||
| 75 | * after the module is removed. | ||
| 76 | */ | ||
| 77 | for (i = 0; i < 10; i++) { | ||
| 78 | elem = kmalloc(sizeof(*elem), GFP_KERNEL); | ||
| 79 | pr_info("kmemleak: kmalloc(sizeof(*elem)) = %p\n", elem); | ||
| 80 | if (!elem) | ||
| 81 | return -ENOMEM; | ||
| 82 | memset(elem, 0, sizeof(*elem)); | ||
| 83 | INIT_LIST_HEAD(&elem->list); | ||
| 84 | |||
| 85 | list_add_tail(&elem->list, &test_list); | ||
| 86 | } | ||
| 87 | |||
| 88 | for_each_possible_cpu(i) { | ||
| 89 | per_cpu(test_pointer, i) = kmalloc(129, GFP_KERNEL); | ||
| 90 | pr_info("kmemleak: kmalloc(129) = %p\n", | ||
| 91 | per_cpu(test_pointer, i)); | ||
| 92 | } | ||
| 93 | |||
| 94 | return 0; | ||
| 95 | } | ||
| 96 | module_init(kmemleak_test_init); | ||
| 97 | |||
| 98 | static void __exit kmemleak_test_exit(void) | ||
| 99 | { | ||
| 100 | struct test_node *elem, *tmp; | ||
| 101 | |||
| 102 | /* | ||
| 103 | * Remove the list elements without actually freeing the | ||
| 104 | * memory. | ||
| 105 | */ | ||
| 106 | list_for_each_entry_safe(elem, tmp, &test_list, list) | ||
| 107 | list_del(&elem->list); | ||
| 108 | } | ||
| 109 | module_exit(kmemleak_test_exit); | ||
| 110 | |||
| 111 | MODULE_LICENSE("GPL"); | ||
