Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 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 | |
Fabian Frederick | f59428a | 2014-06-06 14:38:26 -0700 | [diff] [blame] | 21 | #define pr_fmt(fmt) "kmemleak: " fmt |
| 22 | |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 23 | #include <linux/init.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/vmalloc.h> |
| 28 | #include <linux/list.h> |
| 29 | #include <linux/percpu.h> |
| 30 | #include <linux/fdtable.h> |
| 31 | |
| 32 | #include <linux/kmemleak.h> |
| 33 | |
| 34 | struct test_node { |
| 35 | long header[25]; |
| 36 | struct list_head list; |
| 37 | long footer[25]; |
| 38 | }; |
| 39 | |
| 40 | static LIST_HEAD(test_list); |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 41 | static DEFINE_PER_CPU(void *, kmemleak_test_pointer); |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * Some very simple testing. This function needs to be extended for |
| 45 | * proper testing. |
| 46 | */ |
| 47 | static int __init kmemleak_test_init(void) |
| 48 | { |
| 49 | struct test_node *elem; |
| 50 | int i; |
| 51 | |
| 52 | printk(KERN_INFO "Kmemleak testing\n"); |
| 53 | |
| 54 | /* make some orphan objects */ |
Fabian Frederick | f59428a | 2014-06-06 14:38:26 -0700 | [diff] [blame] | 55 | pr_info("kmalloc(32) = %p\n", kmalloc(32, GFP_KERNEL)); |
| 56 | pr_info("kmalloc(32) = %p\n", kmalloc(32, GFP_KERNEL)); |
| 57 | pr_info("kmalloc(1024) = %p\n", kmalloc(1024, GFP_KERNEL)); |
| 58 | pr_info("kmalloc(1024) = %p\n", kmalloc(1024, GFP_KERNEL)); |
| 59 | pr_info("kmalloc(2048) = %p\n", kmalloc(2048, GFP_KERNEL)); |
| 60 | pr_info("kmalloc(2048) = %p\n", kmalloc(2048, GFP_KERNEL)); |
| 61 | pr_info("kmalloc(4096) = %p\n", kmalloc(4096, GFP_KERNEL)); |
| 62 | pr_info("kmalloc(4096) = %p\n", kmalloc(4096, GFP_KERNEL)); |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 63 | #ifndef CONFIG_MODULES |
Fabian Frederick | f59428a | 2014-06-06 14:38:26 -0700 | [diff] [blame] | 64 | pr_info("kmem_cache_alloc(files_cachep) = %p\n", |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 65 | kmem_cache_alloc(files_cachep, GFP_KERNEL)); |
Fabian Frederick | f59428a | 2014-06-06 14:38:26 -0700 | [diff] [blame] | 66 | pr_info("kmem_cache_alloc(files_cachep) = %p\n", |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 67 | kmem_cache_alloc(files_cachep, GFP_KERNEL)); |
| 68 | #endif |
Fabian Frederick | f59428a | 2014-06-06 14:38:26 -0700 | [diff] [blame] | 69 | pr_info("vmalloc(64) = %p\n", vmalloc(64)); |
| 70 | pr_info("vmalloc(64) = %p\n", vmalloc(64)); |
| 71 | pr_info("vmalloc(64) = %p\n", vmalloc(64)); |
| 72 | pr_info("vmalloc(64) = %p\n", vmalloc(64)); |
| 73 | pr_info("vmalloc(64) = %p\n", vmalloc(64)); |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * Add elements to a list. They should only appear as orphan |
| 77 | * after the module is removed. |
| 78 | */ |
| 79 | for (i = 0; i < 10; i++) { |
Jesper Juhl | 0a08739 | 2010-10-30 23:43:05 +0200 | [diff] [blame] | 80 | elem = kzalloc(sizeof(*elem), GFP_KERNEL); |
Fabian Frederick | f59428a | 2014-06-06 14:38:26 -0700 | [diff] [blame] | 81 | pr_info("kzalloc(sizeof(*elem)) = %p\n", elem); |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 82 | if (!elem) |
| 83 | return -ENOMEM; |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 84 | INIT_LIST_HEAD(&elem->list); |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 85 | list_add_tail(&elem->list, &test_list); |
| 86 | } |
| 87 | |
| 88 | for_each_possible_cpu(i) { |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 89 | per_cpu(kmemleak_test_pointer, i) = kmalloc(129, GFP_KERNEL); |
Fabian Frederick | f59428a | 2014-06-06 14:38:26 -0700 | [diff] [blame] | 90 | pr_info("kmalloc(129) = %p\n", |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 91 | per_cpu(kmemleak_test_pointer, i)); |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 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"); |