操作系统:openEuler 22.03 LTS
内核版本 5.10.0-60.18.0.50.oe2203.x86_64
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/mmdebug.h>
//#include <asm-generic/page.h>
#include <linux/mm.h>
static int *p = NULL;
static int __init kmalloc_test_init(void)
{
//p = kmalloc(PAGE_SIZE, GFP_KERNEL);
//printk(KERN_INFO "address: %p\n", p);
//p = kmalloc_array(1025, 4 * 1024, GFP_KERNEL);
struct page *page;
p = kzalloc(4 * 1024 * 1024, GFP_KERNEL);
page = virt_to_page(p);
printk(KERN_INFO "address: %p\n", p);
dump_page(page, "hyq - test");
return 0;
}
static void __exit kmalloc_test_exit(void)
{
printk("exit\n");
if (p != NULL)
kfree(p);
}
module_init(kmalloc_test_init);
module_exit(kmalloc_test_exit);
MODULE_LICENSE("GPL");
Makefile
obj-m+=kmalloc_test.o
CONFIG_MODULE_SIG=n
all:
make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) clean
运行结果: