Linux Kernel programming: trying to get vm_area_struct->vm_start crashes kernel

对着背影说爱祢 提交于 2019-12-05 17:22:12

Some kernel threads might not have mm filled - check p->mm for NULL.

Changed the code to check for null pointers:

m = p->mm;
if (m != 0) {
    v = m->mmap;
    if (v != 0) {
        long start = v->vm_start;
        printk("vm_start is %ld\n", start);
    }
}

All process related information can be found at /proc filesystem at the userspace level. Inside the kernel, these information are generated via fs/proc/*.c

http://lxr.linux.no/linux+v3.2.4/fs/proc/

Looking at the file task_mmu.c, which printing all the vm_start information u can observe that all handling of vm_start field always require the mmap_sem to be locked:

           down_read(&mm->mmap_sem);
            for (vma = mm->mmap; vma; vma = vma->vm_next) {
                    clear_refs_walk.private = vma;
...
                    walk_page_range(vma->vm_start, vma->vm_end,
                                    &clear_refs_walk);

For kernel threads mm will be null. So whenever you read the mm do it in the following manner.

    down_read(&p->mm->mmap_sem)
          if(mm) {
                 /* read the contents of mm*/
          }
    up_read(&p->mm->mmap_sem)

Also you may use get_task_mm(). With get_task_mm() you need not acquire the lock. Here is how you use it :

   struct mm_struct *mm;
   mm = get_task_mm(p);
   if (mm) {
           /* read the mm contents */
   }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!