kmemleak-test.c 3.2 KB

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