开发者

How can I access memory at known physical address inside a kernel module?

开发者 https://www.devze.com 2023-01-26 07:43 出处:网络
I am trying to work on this problem: A user spaces program keeps polling a buffer to get requests from a kernel module and serves it and then responses to the kernel.

I am trying to work on this problem: A user spaces program keeps polling a buffer to get requests from a kernel module and serves it and then responses to the kernel.

I want to make the solution much faster, so instead of creating a device file and communicating via it, I allocate a memory buffer from the user space and mark it as pinned, so the memory pages never get swapped out. Then the user space invokes a special syscall to tell the kernel about the memory buffer so that the kernel module can get the physical address of that buffer. (because the user space program may be context-switched out and hence the virtual address means nothing if the kernel module accesses the buffer at that time.)

When the module wants to send request, it needs put the request to the buffer via physical address. The question is: How can I access the buffer inside the kernel mo开发者_运维技巧dule via its physical address.

I noticed there is get_user_pages, but don't know how to use it, or maybe there are other better methods?

Thanks.


You are better off doing this the other way around - have the kernel allocate the buffer, then allow the userspace program to map it into its address space using mmap().


Finally I figured out how to handle this problem...

Quite simple but may be not safe.

use phys_to_virt, which calls __va(pa), to get the virtual address in kernel and I can access that buffer. And because the buffer is pinned, that can guarantee the physical location is available.

What's more, I don't need s special syscall to tell the kernel the buffer's info. Instead, a proc file is enough because I just need tell the kernel once.

0

精彩评论

暂无评论...
验证码 换一张
取 消