使用do_mmap_pgoff出现的问题,急!
我写了一个模块,反复映射内存,然后去除映射,结果总是出错,错误为:
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");
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");
作者: 眼眼rhine 发布时间: 2007-11-22
代码没有排好版,抱歉!
作者: 眼眼rhine 发布时间: 2007-11-22
不知道,很奇怪.帮忙顶
作者: wuqixuan 发布时间: 2007-11-22
问题解决:
将
error = do_munmap(NULL, ubuf, count);
改为:
error = do_munmap(current->mm, ubuf, count);
即可。
在取消映射时,由于第一个参数传的为NULL,其实并没有取消映射。
但更深层的原因估计还得看do_munmap实现才能确认。
将
error = do_munmap(NULL, ubuf, count);
改为:
error = do_munmap(current->mm, ubuf, count);
即可。
在取消映射时,由于第一个参数传的为NULL,其实并没有取消映射。
但更深层的原因估计还得看do_munmap实现才能确认。
作者: 眼眼rhine 发布时间: 2007-11-23
另外,在取消映射时也需要使用
down_write(¤t->mm->mmap_sem);
和
up_write(¤t->mm->mmap_sem);
down_write(¤t->mm->mmap_sem);
和
up_write(¤t->mm->mmap_sem);
作者: 眼眼rhine 发布时间: 2007-11-23
谢谢
很需要
很需要
作者: tigerish_d 发布时间: 2010-04-13
修改如下:没有出错,打印:end i=1000000
复制代码
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/kernel.h>
- #include <linux/mm.h>
- #include <asm/mman.h>
- #include <linux/rwsem.h>
- #include <linux/err.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_ANON, 0);
- up_write(¤t->mm->mmap_sem);
-
- if (IS_ERR((void*)ubuf))
- {
- printk(KERN_INFO"do_mmap_pgoff failed\n");
- break;
- }
- else
- {
- down_write(¤t->mm->mmap_sem);
- error = do_munmap(current->mm, ubuf, count);
- up_write(¤t->mm->mmap_sem);
- 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("GPL");
作者: todaygood 发布时间: 2011-03-01