Thomas Gleixner | 4505153 | 2019-05-29 16:57:47 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 2 | /* |
| 3 | * mm/kmemleak-test.c |
| 4 | * |
| 5 | * Copyright (C) 2008 ARM Limited |
| 6 | * Written by Catalin Marinas <catalin.marinas@arm.com> |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Fabian Frederick | f59428a | 2014-06-06 14:38:26 -0700 | [diff] [blame] | 9 | #define pr_fmt(fmt) "kmemleak: " fmt |
| 10 | |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 11 | #include <linux/init.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/vmalloc.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/percpu.h> |
| 18 | #include <linux/fdtable.h> |
| 19 | |
| 20 | #include <linux/kmemleak.h> |
| 21 | |
| 22 | struct test_node { |
| 23 | long header[25]; |
| 24 | struct list_head list; |
| 25 | long footer[25]; |
| 26 | }; |
| 27 | |
| 28 | static LIST_HEAD(test_list); |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 29 | static DEFINE_PER_CPU(void *, kmemleak_test_pointer); |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | * Some very simple testing. This function needs to be extended for |
| 33 | * proper testing. |
| 34 | */ |
| 35 | static int __init kmemleak_test_init(void) |
| 36 | { |
| 37 | struct test_node *elem; |
| 38 | int i; |
| 39 | |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 40 | pr_info("Kmemleak testing\n"); |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 41 | |
| 42 | /* make some orphan objects */ |
Fabian Frederick | f59428a | 2014-06-06 14:38:26 -0700 | [diff] [blame] | 43 | pr_info("kmalloc(32) = %p\n", kmalloc(32, GFP_KERNEL)); |
| 44 | pr_info("kmalloc(32) = %p\n", kmalloc(32, GFP_KERNEL)); |
| 45 | pr_info("kmalloc(1024) = %p\n", kmalloc(1024, GFP_KERNEL)); |
| 46 | pr_info("kmalloc(1024) = %p\n", kmalloc(1024, GFP_KERNEL)); |
| 47 | pr_info("kmalloc(2048) = %p\n", kmalloc(2048, GFP_KERNEL)); |
| 48 | pr_info("kmalloc(2048) = %p\n", kmalloc(2048, GFP_KERNEL)); |
| 49 | pr_info("kmalloc(4096) = %p\n", kmalloc(4096, GFP_KERNEL)); |
| 50 | pr_info("kmalloc(4096) = %p\n", kmalloc(4096, GFP_KERNEL)); |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 51 | #ifndef CONFIG_MODULES |
Fabian Frederick | f59428a | 2014-06-06 14:38:26 -0700 | [diff] [blame] | 52 | pr_info("kmem_cache_alloc(files_cachep) = %p\n", |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 53 | kmem_cache_alloc(files_cachep, GFP_KERNEL)); |
Fabian Frederick | f59428a | 2014-06-06 14:38:26 -0700 | [diff] [blame] | 54 | pr_info("kmem_cache_alloc(files_cachep) = %p\n", |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 55 | kmem_cache_alloc(files_cachep, GFP_KERNEL)); |
| 56 | #endif |
Fabian Frederick | f59428a | 2014-06-06 14:38:26 -0700 | [diff] [blame] | 57 | pr_info("vmalloc(64) = %p\n", vmalloc(64)); |
| 58 | pr_info("vmalloc(64) = %p\n", vmalloc(64)); |
| 59 | pr_info("vmalloc(64) = %p\n", vmalloc(64)); |
| 60 | pr_info("vmalloc(64) = %p\n", vmalloc(64)); |
| 61 | pr_info("vmalloc(64) = %p\n", vmalloc(64)); |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 62 | |
| 63 | /* |
| 64 | * Add elements to a list. They should only appear as orphan |
| 65 | * after the module is removed. |
| 66 | */ |
| 67 | for (i = 0; i < 10; i++) { |
Jesper Juhl | 0a08739 | 2010-10-30 23:43:05 +0200 | [diff] [blame] | 68 | elem = kzalloc(sizeof(*elem), GFP_KERNEL); |
Fabian Frederick | f59428a | 2014-06-06 14:38:26 -0700 | [diff] [blame] | 69 | pr_info("kzalloc(sizeof(*elem)) = %p\n", elem); |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 70 | if (!elem) |
| 71 | return -ENOMEM; |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 72 | INIT_LIST_HEAD(&elem->list); |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 73 | list_add_tail(&elem->list, &test_list); |
| 74 | } |
| 75 | |
| 76 | for_each_possible_cpu(i) { |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 77 | per_cpu(kmemleak_test_pointer, i) = kmalloc(129, GFP_KERNEL); |
Fabian Frederick | f59428a | 2014-06-06 14:38:26 -0700 | [diff] [blame] | 78 | pr_info("kmalloc(129) = %p\n", |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 79 | per_cpu(kmemleak_test_pointer, i)); |
Catalin Marinas | 0822ee4 | 2009-06-11 13:24:14 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | module_init(kmemleak_test_init); |
| 85 | |
| 86 | static void __exit kmemleak_test_exit(void) |
| 87 | { |
| 88 | struct test_node *elem, *tmp; |
| 89 | |
| 90 | /* |
| 91 | * Remove the list elements without actually freeing the |
| 92 | * memory. |
| 93 | */ |
| 94 | list_for_each_entry_safe(elem, tmp, &test_list, list) |
| 95 | list_del(&elem->list); |
| 96 | } |
| 97 | module_exit(kmemleak_test_exit); |
| 98 | |
| 99 | MODULE_LICENSE("GPL"); |