如何使用mmap共享用户空间和内核线程 [英] How to use mmap to share user-space and kernel threads

查看:305
本文介绍了如何使用mmap共享用户空间和内核线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到一些合适的例子来解决我的问题.我想在用户空间和内核空间之间共享4K(4096)字节的数据.我发现了很多想法,这些想法表明我必须从内核分配内存并在用户空间中映射它.有人可以提供有关如何在Linux 2.6.38中进行操作的示例.有什么好的文件可以解释吗?

谢谢.

解决方案

您提出的方法是一种方法,但是由于用户空间不在您的控制范围内(意味着任何用户空间程序都有可能进入内核),因此您正在打开增加了来自用户空间的恶意攻击的机会.此处还介绍了这种基于内核的与用户空间共享内存的方法:

http://www.scs.ch/~frey/linux/memorymap. html

相反,如何在用户空间中分配内存,然后从内核使用API​​ copy_from_user()和copy_to_user()从用户空间内存中复制数据呢?如果要在不同进程之间共享内存,则可以始终使用与IPC相关的API来分配和定义内存,例如shmget()等.在这种情况下,内核源代码本身中有很多示例代码. /p>

例如

fs/checksum.c:丢失= __copy_from_user(dst,src,len);

I am having some trouble finding some suitable examples to solve my problem. I want to share 4K (4096) byte of data between user and kernel space. I found many ideas which says I have to allocate memory from kernel and mmap it in user-space. Can someone provide an example on how to do it in Linux 2.6.38. Is there any good document which explains it?

Thanks in advance.

解决方案

Your proposed way is one way, but as userspace is not within your control (meaning any userspace program have a possibility of poking into the kernel), you are opening up the opportunities for malicious attack from userspace. This kernel-based memory-sharing-with-userspace is also described here:

http://www.scs.ch/~frey/linux/memorymap.html

Instead, how about allocating memory in userspace, and then from kernel use the API copy_from_user() and copy_to_user() to copy to/from userspace memory? If u want to share the memory among the different processes, then u can always use IPC related API to allocate and define the memory, eg shmget() etc. And in this case there are lots of sample codes within the kernel source itself.

eg.

fs/checksum.c: missing = __copy_from_user(dst, src, len);

这篇关于如何使用mmap共享用户空间和内核线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆