|
我写了一个模块,反复映射内存,然后去除映射,结果总是出错,错误为:
Nov 22 14:22:15 node2 kernel: do_munmap error =-22 invalid argument
Nov 22 14:22:15 node2 kernel: end i=348837
以下为内核模块代码,请大家帮忙看看:
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <asm/mman.h>
static int __init tpgoff_init(void)
{
int error = 0;
unsigned long ubuf=0;
size_t count = 0;
unsigned long i=0;
unsigned long t=1000000;
printk("tpgoff kernel module init!\n");
count = 1024 * 1024;
for (i=0;i<t;i++) {
ubuf = 0;
down_write(¤t->mm->mmap_sem);
ubuf = do_mmap_pgoff(NULL, 0, count, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, 0);
up_write(¤t->mm->mmap_sem);
if (ubuf==0) {
printk("ubuf=0\n");
return 0;
}
if (ubuf!=0) {
error = do_munmap(NULL, ubuf, count);
if (error) {
printk("do_munmap error =%d\n",error);
break;
}
}
}
printk("end i=%ld\n",i);
return error;
}
static void __exit tpgoff_cleanup(void)
{
printk("tpgoff kernel module exit!\n");
}
module_init(tpgoff_init);
module_exit(tpgoff_cleanup);
MODULE_LICENSE("Dual BSD/GPL"); |
|