Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * samples/kmemleak/kmemleak-test.c
0004  *
0005  * Copyright (C) 2008 ARM Limited
0006  * Written by Catalin Marinas <catalin.marinas@arm.com>
0007  */
0008 
0009 #define pr_fmt(fmt) "kmemleak: " fmt
0010 
0011 #include <linux/init.h>
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/slab.h>
0015 #include <linux/vmalloc.h>
0016 #include <linux/list.h>
0017 #include <linux/percpu.h>
0018 #include <linux/fdtable.h>
0019 
0020 #include <linux/kmemleak.h>
0021 
0022 struct test_node {
0023     long header[25];
0024     struct list_head list;
0025     long footer[25];
0026 };
0027 
0028 static LIST_HEAD(test_list);
0029 static DEFINE_PER_CPU(void *, kmemleak_test_pointer);
0030 
0031 /*
0032  * Some very simple testing. This function needs to be extended for
0033  * proper testing.
0034  */
0035 static int __init kmemleak_test_init(void)
0036 {
0037     struct test_node *elem;
0038     int i;
0039 
0040     pr_info("Kmemleak testing\n");
0041 
0042     /* make some orphan objects */
0043     pr_info("kmalloc(32) = %p\n", kmalloc(32, GFP_KERNEL));
0044     pr_info("kmalloc(32) = %p\n", kmalloc(32, GFP_KERNEL));
0045     pr_info("kmalloc(1024) = %p\n", kmalloc(1024, GFP_KERNEL));
0046     pr_info("kmalloc(1024) = %p\n", kmalloc(1024, GFP_KERNEL));
0047     pr_info("kmalloc(2048) = %p\n", kmalloc(2048, GFP_KERNEL));
0048     pr_info("kmalloc(2048) = %p\n", kmalloc(2048, GFP_KERNEL));
0049     pr_info("kmalloc(4096) = %p\n", kmalloc(4096, GFP_KERNEL));
0050     pr_info("kmalloc(4096) = %p\n", kmalloc(4096, GFP_KERNEL));
0051 #ifndef CONFIG_MODULES
0052     pr_info("kmem_cache_alloc(files_cachep) = %p\n",
0053         kmem_cache_alloc(files_cachep, GFP_KERNEL));
0054     pr_info("kmem_cache_alloc(files_cachep) = %p\n",
0055         kmem_cache_alloc(files_cachep, GFP_KERNEL));
0056 #endif
0057     pr_info("vmalloc(64) = %p\n", vmalloc(64));
0058     pr_info("vmalloc(64) = %p\n", vmalloc(64));
0059     pr_info("vmalloc(64) = %p\n", vmalloc(64));
0060     pr_info("vmalloc(64) = %p\n", vmalloc(64));
0061     pr_info("vmalloc(64) = %p\n", vmalloc(64));
0062 
0063     /*
0064      * Add elements to a list. They should only appear as orphan
0065      * after the module is removed.
0066      */
0067     for (i = 0; i < 10; i++) {
0068         elem = kzalloc(sizeof(*elem), GFP_KERNEL);
0069         pr_info("kzalloc(sizeof(*elem)) = %p\n", elem);
0070         if (!elem)
0071             return -ENOMEM;
0072         INIT_LIST_HEAD(&elem->list);
0073         list_add_tail(&elem->list, &test_list);
0074     }
0075 
0076     for_each_possible_cpu(i) {
0077         per_cpu(kmemleak_test_pointer, i) = kmalloc(129, GFP_KERNEL);
0078         pr_info("kmalloc(129) = %p\n",
0079             per_cpu(kmemleak_test_pointer, i));
0080     }
0081 
0082     return 0;
0083 }
0084 module_init(kmemleak_test_init);
0085 
0086 static void __exit kmemleak_test_exit(void)
0087 {
0088     struct test_node *elem, *tmp;
0089 
0090     /*
0091      * Remove the list elements without actually freeing the
0092      * memory.
0093      */
0094     list_for_each_entry_safe(elem, tmp, &test_list, list)
0095         list_del(&elem->list);
0096 }
0097 module_exit(kmemleak_test_exit);
0098 
0099 MODULE_LICENSE("GPL");