简体   繁体   English

为什么在尝试从内核模块访问共享内存时出现“未处理的错误:不精确的外部异常终止”?

[英]Why do i get “Unhandled fault: imprecise external abort” while trying to access shared memory from my kernel module?

I have this in a kernel module: 我在内核模块中有这个:

/*global scope declared*/ 
static int array[10]={1,2,3,4,5,6,7,8,9,10};

and I have functions for open close read and write works perfectly, i want to share the array[8] with the user space application in the bottom of this page. 并且我具有用于打开关闭读写操作的功能,我想与本页底部的用户空间应用程序共享array[8]

in the kernel module: 在内核模块中:

static int *my_mmap (struct file *filep, struct vm_area_struct *vma ) {

    if (remap_pfn_range(vma,
                vma->vm_start,
                virt_to_phys(array)>> PAGE_SHIFT,
                10,
                vma->vm_page_prot) < 0) {
        printk("remap_pfn_range failed\n");
        return -EIO;
    }


    return 0;

the application in user-space's source code: 用户空间源代码中的应用程序:

#define LEN (64*1024)
/* prototype for thread routine */

#define FILE_PATH "/dev/my_module"


int main() 
{
    int i=0;
    int fd = open(FILE_PATH,O_RDWR);    
    int* vadr = mmap(0, LEN, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);

    for (i=0;i<10;++i){

        printf("%d",*(vadr+i));
    }

    return 0;
}

This is wrong on so many levels I don't know where to even start :-) 在很多级别上这都是错误的,我什至不知道从哪里开始:-)

  1. virt_to_phys may doesn't actually work on vmalloc allocated memory, which is what Linux uses on most platform for dynamic module data section. virt_to_phys可能实际上不适用于vmalloc分配的内存,这是Linux在大多数平台上用于动态模块数据部分的功能。
  2. Even if it did, the data might not be contiguous in physical memory at all. 即使是这样,数据在物理内存中也可能根本不连续。
  3. The array may not be aligned on page boundary. 该数组可能不在页面边界上对齐。
  4. Your data structure is 40 bytes, you are mapping 10 pages of random kernel memory. 您的数据结构为40字节,您正在映射10页随机内核内存。
  5. If the processor has a virtually indexed, physically tagged (VIPT) cache, you might run into cache congruency issues when there are multiple virtual mappings to the same physical address. 如果处理器具有虚拟索引的物理标记(VIPT)缓存,则当多个虚拟映射到同一物理地址时,可能会遇到缓存一致性问题。

There are probably more problems, but this is what pops to mind. 可能还有更多问题,但这是我想到的。

The right thing to do is not share data between kernel and user space, but copy it over using copy_to_user and friends, unless you really know what you are doing and why. 正确的做法不是在内核空间和用户空间之间共享数据,而是使用copy_to_user和好友复制数据,除非您真的知道自己在做什么以及为什么。

If you really must share the memory, then allocate a free page, map it from kernel space (eg kmap) and from user space (like you did) and hope that your platform doesn't have a VIPT cache. 如果您确实必须共享内存,则分配一个空闲页面,从内核空间(例如kmap)和用户空间(就像您一样)映射它,并希望您的平台没有VIPT缓存。

I'm a relative newcomer to kernel programming so I may be missing something but isn't it possible for you to use copy_to_user in this case? 我是内核编程的一个相对较新的人,因此我可能会缺少一些东西,但是在这种情况下您是否可以使用copy_to_user?

    unsigned long copy_to_user(void __user * to, const void * from, unsigned long n); 

In brief 简单来说

to = address in userspace (destination)
from = address in kernel (source)
n = number of bytes to copy 

您的数组是char数组,但是您的用户空间程序正在将其作为int数组进行访问。

Refer to Implementing mmap for transferring data from user space to kernel space for a guide on how to share data between kernel and user space properly. 有关如何在内核空间和用户空间之间正确共享数据的指南,请参阅实现将mmap从用户空间传输到内核空间的mmap I have it implemented in my custom driver and it works fine. 我已经在我的自定义驱动程序中实现了它,并且效果很好。 If you don't need the speed though, using copy_to_user is safer and easier. 如果您不需要速度,则使用copy_to_user更安全,更轻松。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在尝试 insmod 内核模块探测 do_fork 时出现 -38 错误? - Why do I get -38 error, while trying to insmod a kernel module probing do_fork? 如何使用内核模块直接访问保留内存? - How do I directly access reserved memory with a kernel module? 为什么即使我可以访问内存也会出现分段错误(核心转储)? - why do I get Segmentation fault (core dumped) even when I have access to the memory? 为什么在 c linux 中使用共享 memory 尝试 IPC 时出现分段错误 - Why am i getting segmentation fault when trying IPC using shared memory in c linux 内核模块内存访问 - Kernel Module memory access 不正确的内存访问:为什么我的内核*没有*崩溃 - Incorrect memory access: why is my kernel *not* crashing 为什么在尝试打印数组时出现分段错误? - Why do I get segmentation fault when trying to print an array? 为什么在使用ioctl的内核模块中会收到此编译警告? - Why do I get this compilation warning in a kernel module with ioctl? 为什么在尝试比较两个字符串时会出现分段错误? - Why do I get segmentation fault on trying to compare two strings? 尝试将数组从stdin复制到2d数组时,为什么会出现分段错误? - Why do I get a segmentation fault when trying to copy an array from stdin to a 2d array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM