Avoid memory copying between user space and kernel space
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.
1. The user space process makes an IOCTL call with pointer to a user memory buffer.
2. The kernel device driver in the big switch-case of IOCTL, first does a copy to kernel space
3. 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);
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:
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! I am an absolute beginner so apologies in advance if this is standard knowledge.