kmemleak-test.c 2.6 KB

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