I recently started working with Linux and wrote my first device driver for a hardware chip controlled by a host CPU running Linux 2.6.x kernel.
- The user space process makes an IOCTL call with pointer to a user memory buffer.
- The kernel device driver in the big switch-case of IOCTL, first does a copy to kernel space
- The driver then processes the memory, eventually writing words to the hardware blocks as appropriate
The memory copying is a performance hit because of limited cycles available on this embedded CPU. How does one avoid unnecessary memory copy between user space and kernel space? Suppose there is a buffer in the user-space that needs to be read by the kernel space? I believe that the typical code in the kernel mode device driver is:
....
copy_from_user(kernel_dst_buffer, user_src_buffer, length);
use kernel_dst_buffer;
...
I want to avoid the copy_from_user and was trying to read up my "Linux Device Drivers book by Corbet/Rubini" and search online as well. It appears that one of the following will be used:
get_user_pages
mmap
shm_get
but I can't figure out. It appears extremely difficult/complicated for a simple task of sharing memory between user/kernel modes. Is that how it is? I couldn't even get any sample code for sharing memory though there appears to be sample code for just about everything. Maybe my search strings are broken.
Any pointers?
Thanks a lot!
Gaurav