brintos

brintos / linux-shallow public Read only

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