kmemleak-test.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/list.h>
  26. #include <linux/percpu.h>
  27. #include <linux/fdtable.h>
  28. #include <linux/kmemleak.h>
  29. struct test_node {
  30. long header[25];
  31. struct list_head list;
  32. long footer[25];
  33. };
  34. static LIST_HEAD(test_list);
  35. static DEFINE_PER_CPU(void *, kmemleak_test_pointer);
  36. /*
  37. * Some very simple testing. This function needs to be extended for
  38. * proper testing.
  39. */
  40. static int __init kmemleak_test_init(void)
  41. {
  42. struct test_node *elem;
  43. int i;
  44. printk(KERN_INFO "Kmemleak testing\n");
  45. /* make some orphan objects */
  46. pr_info("kmemleak: kmalloc(32) = %p\n", kmalloc(32, GFP_KERNEL));
  47. pr_info("kmemleak: kmalloc(32) = %p\n", kmalloc(32, GFP_KERNEL));
  48. pr_info("kmemleak: kmalloc(1024) = %p\n", kmalloc(1024, GFP_KERNEL));
  49. pr_info("kmemleak: kmalloc(1024) = %p\n", kmalloc(1024, GFP_KERNEL));
  50. pr_info("kmemleak: kmalloc(2048) = %p\n", kmalloc(2048, GFP_KERNEL));
  51. pr_info("kmemleak: kmalloc(2048) = %p\n", kmalloc(2048, GFP_KERNEL));
  52. pr_info("kmemleak: kmalloc(4096) = %p\n", kmalloc(4096, GFP_KERNEL));
  53. pr_info("kmemleak: kmalloc(4096) = %p\n", kmalloc(4096, GFP_KERNEL));
  54. #ifndef CONFIG_MODULES
  55. pr_info("kmemleak: kmem_cache_alloc(files_cachep) = %p\n",
  56. kmem_cache_alloc(files_cachep, GFP_KERNEL));
  57. pr_info("kmemleak: kmem_cache_alloc(files_cachep) = %p\n",
  58. kmem_cache_alloc(files_cachep, GFP_KERNEL));
  59. #endif
  60. pr_info("kmemleak: vmalloc(64) = %p\n", vmalloc(64));
  61. pr_info("kmemleak: vmalloc(64) = %p\n", vmalloc(64));
  62. pr_info("kmemleak: vmalloc(64) = %p\n", vmalloc(64));
  63. pr_info("kmemleak: vmalloc(64) = %p\n", vmalloc(64));
  64. pr_info("kmemleak: vmalloc(64) = %p\n", vmalloc(64));
  65. /*
  66. * Add elements to a list. They should only appear as orphan
  67. * after the module is removed.
  68. */
  69. for (i = 0; i < 10; i++) {
  70. elem = kzalloc(sizeof(*elem), GFP_KERNEL);
  71. pr_info("kmemleak: kzalloc(sizeof(*elem)) = %p\n", elem);
  72. if (!elem)
  73. return -ENOMEM;
  74. INIT_LIST_HEAD(&elem->list);
  75. list_add_tail(&elem->list, &test_list);
  76. }
  77. for_each_possible_cpu(i) {
  78. per_cpu(kmemleak_test_pointer, i) = kmalloc(129, GFP_KERNEL);
  79. pr_info("kmemleak: kmalloc(129) = %p\n",
  80. per_cpu(kmemleak_test_pointer, i));
  81. }
  82. return 0;
  83. }
  84. module_init(kmemleak_test_init);
  85. static void __exit kmemleak_test_exit(void)
  86. {
  87. struct test_node *elem, *tmp;
  88. /*
  89. * Remove the list elements without actually freeing the
  90. * memory.
  91. */
  92. list_for_each_entry_safe(elem, tmp, &test_list, list)
  93. list_del(&elem->list);
  94. }
  95. module_exit(kmemleak_test_exit);
  96. MODULE_LICENSE("GPL");