I am working on a research project , and I have to share a large data structure between a kernel module and a user space program. The data structure can get very large, and since the application is performance critical, I tried using shared memory to reduce the overhead of serializing the structure(using other interfaces like NetLink). I currently made a test code based on the this link:
[http://people.ee.ethz.ch/~arkeller/linux/kernel_user_space_howto.html#s8][1]
They are using debugfs. I added the code in the link into my kernel module, and I wrote a custom user space program similar to theirs. I tried it with small sizes of my datastructure which worked perfectly. You can notice in the code, they are sharing only 1 page of memory. I wanted to know if there is an easy way to share much more memory than just one page.
The copy_to_user function copies a block of data from the kernel into user space. This function accepts a pointer to a user space buffer, a pointer to a kernel buffer, and a length defined in bytes. The function returns zero on success or non-zero to indicate the number of bytes that weren't transferred.
Kernel space is strictly reserved for running a privileged operating system kernel, kernel extensions, and most device drivers. In contrast, user space is the memory area where application software and some drivers execute.
You can use the copy_from_user() and copy_to_user() functions to move data between kernel space and user space.
And it uses proc, ioctl, and system call to realize the communication between Kernel and User Spaces. But these methods must keep user space synchronizing with kernel space. So, Netlink mechanism is used, and ONU logs are recommended, which is difficult for proc, ioctl, and system call.
There's not really much different in doing many pages.
Allocate more pages in the open (alloc_pages or a variant), store them in an array, then your fault handler will need to (based on the faulting address):
struct page *
from arrayYou can continue to use debugfs or, with a small amount of additional work in the module initialization, put a more standard character device frontend on it. (For that, nothing really needs to change outside of the module_init/module_exit parts.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With