使用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(&current->mm->mmap_sem);
                 ubuf = do_mmap_pgoff(NULL, 0, count, PROT_READ|PROT_WRITE,
                                MAP_PRIVATE|MAP_ANONYMOUS, 0);
                 up_write(&current->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实现才能确认。

作者: 眼眼rhine   发布时间: 2007-11-23

另外,在取消映射时也需要使用
down_write(&current->mm->mmap_sem);

up_write(&current->mm->mmap_sem);

作者: 眼眼rhine   发布时间: 2007-11-23

谢谢
很需要

作者: tigerish_d   发布时间: 2010-04-13

修改如下:没有出错,打印:end i=1000000
  1. #include <linux/module.h>
  2. #include <linux/init.h>
  3. #include <linux/kernel.h>
  4. #include <linux/mm.h>
  5. #include <asm/mman.h>
  6. #include <linux/rwsem.h>
  7. #include <linux/err.h>

  8. static int __init tpgoff_init(void)
  9. {
  10.       int error = 0;
  11.       unsigned long ubuf=0;
  12.       size_t count = 0;
  13.       unsigned long i=0;
  14.       unsigned long t=1000000;
  15.       
  16.        printk("tpgoff kernel module init!\n");

  17.     count = 1024 * 1024;
  18.     for (i=0;i<t;i++)
  19.     {
  20.              ubuf = 0;

  21.              down_write(&current->mm->mmap_sem);
  22.              ubuf = do_mmap_pgoff(NULL, 0, count, PROT_READ|PROT_WRITE,
  23.                                                     MAP_PRIVATE|MAP_ANON, 0);
  24.              up_write(&current->mm->mmap_sem);

  25.              if (IS_ERR((void*)ubuf))
  26.             {
  27.                  printk(KERN_INFO"do_mmap_pgoff  failed\n");
  28.                  break;
  29.             }
  30.             else               
  31.             {
  32.                 down_write(&current->mm->mmap_sem);
  33.                 error = do_munmap(current->mm, ubuf, count);
  34.                 up_write(&current->mm->mmap_sem);
  35.                 if (error)
  36.                 {
  37.                         printk("do_munmap error =%d\n",error);
  38.                         break;
  39.                 }
  40.             }
  41.     }

  42.   printk("end i=%ld\n",i);
  43.   return error;

  44. }


  45. static void __exit tpgoff_cleanup(void)
  46. {

  47.   printk("tpgoff kernel module exit!\n");

  48. }

  49. module_init(tpgoff_init);
  50. module_exit(tpgoff_cleanup);
  51. MODULE_LICENSE("GPL");
复制代码

作者: todaygood   发布时间: 2011-03-01