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