1

I am writing a device driver which uses character device file to copy data from user space buffer which is allocated by using malloc to kernel buffer. Currently using copy_from_user api to copy user data to kernel buffer. Trying to find a way to avoid data copying between user and kernel spaces. Is there any way to access user space buffer (allocated by malloc) in kernel space without using copy_from_user?

3
  • 3
    I'm not sure whether I should post this as an answer since your question makes an impression that you are seeking specifically for some way to access user-space buffers created by malloc. However, such goal-setting might not be useful. Instead, there is a possibility for a user-space application to use mmap() system call to access some piece of device memory directly. At the same time the device driver must implement mmap callback of struct file_operations. If you like the idea, you may refer to a far more broad explanation in LDD3 book: makelinux.net/ldd3/chp-15-sect-2 .
    – user8549610
    Sep 12, 2017 at 17:33
  • 1
    Also, Linux kernel has such a facility as PACKET_MMAP which, of course, is not an example of a character device, but it's also built on top of direct memory mapping thus allowing user space applications work with network sockets more efficiently as they can grab/send packets directly without expensive data copying between kernel- and user-space. It's just a bright example of mmap() technique and, as I pointed out in the previous comment, it seems like a character devices may deploy the same approach.
    – user8549610
    Sep 12, 2017 at 17:41
  • You should be able to use DMA if your system allows for that. Sep 13, 2017 at 1:19

2 Answers 2

3

Let's start by answering the question you've asked. Yes, you can access memory malloced by the userspace from the kernel. I'm not sure of the technicalities of how, but if your kernel code runs in the context of the calling thread, it might be possible to simply use the user space pointer given to you.

Please don't do that, however.

The reason you shouldn't do that is that if the user space pointer is bad, or the memory is too short, or any other problem exists, the user space process may need to crash with a segmentation fault. Sadly, you are not running user space code, you are running kernel space. The kernel equivalent of segmentation fault is a kernel panic. Don't inflict that on your users.

A better approach is to use a mechanism where userspace writes the data once, and then the character device can simply use it. Having user space mmap the page was mentioned in the comments. A method that might be more standard to the way file descriptors work would be to implement splice_read in the device's fileops struct.

Now, splice is not an easy interface to work with, but the main advantage is that if your source is also splice aware, the user can pass data directly from the source to your driver without it ever passing through user space.

If you want to save the copies, I suggest you go with one of those solutions. Again, do not access the user supplied pointer directly unless you know what you're doing.

0

get_user_pages() API can be used to pin the user pages from swapped out from physical memory and kernel can access this memory area by accessing physical pages of corresponding virtual address.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.