Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Large shared memory between Kernel space and user space

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.

like image 833
Aditya Avatar asked Jun 17 '14 21:06

Aditya


People also ask

How do I copy a large memory from kernel to user space in Linux?

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.

What is difference between user space and kernel space?

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.

Which function allows the transfer of data from kernel space to user space?

You can use the copy_from_user() and copy_to_user() functions to move data between kernel space and user space.

How does user space and kernel space communicate?

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.


1 Answers

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):

  • calculate the offset into the area with something like "(((unsigned long) vmf->virtual_address - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT))"
  • divide by PAGE_SIZE to calculate page index within the array
  • range check to make sure it's valid
  • pull struct page * from array
  • call get_page to do the mapping

You 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.)

like image 147
Gil Hamilton Avatar answered Sep 23 '22 12:09

Gil Hamilton