#include <linux/module.h> #include <linux/init.h> #include <linux/slab.h> #include <linux/miscdevice.h> #include <linux/gpio/consumer.h> #include <asm-generic/gpio.h> #include <linux/gpio/consumer.h> static int __init my_gpio_init(void){ int ret = 0; char *p = NULL; printk(KERN_ERR"==sfl: enter my_gpio_init==\n"); p = kcalloc(0xff, 0, GFP_KERNEL); if (!p) { printk(KERN_ERR"==sfl: p==NULL==\n"); } else { printk(KERN_ERR"==sfl: p==%p\n", p); p[0] = 5; printk(KERN_ERR"==sfl: p[0]==%d\n", p[0]); } kfree(p); return 0; } static void __exit my_gpio_exit(void){ printk(KERN_ERR"==enter my_gpio_exit==\n"); } module_init(my_gpio_init); module_exit(my_gpio_exit); MODULE_LICENSE("GPL");
insmod插入时打印如下:
root@ggs:/mytest# insmod test.ko [ 1040.877617] ==sfl: enter my_gpio_init== [ 1040.881570] ==sfl: p==0000000000000010 [ 1040.885381] Unable to handle kernel NULL pointer dereference at virtual address 00000010
返回的地址竟然不是NULL!!,而是10
本文参考链接:https://www.cnblogs.com/hellokitty2/p/9850725.html