What is an ideal and fast way to communicate between the kernel and user space?

I know that information exchange can occur through the following interfaces between kernel and user programs

  • system calls

  • IOCTLs

  • / proc and / sys

  • Netlink

I wanna know

  • Should I miss any other interface?

  • Which one is the fastest way to share large amounts of data? (and if there is any document / mail / explanation supporting such a requirement that I can refer to)

  • Which of the recommended ways to communicate? (I think its netlink, but still would like to hear opinions)

+7
linux linux-kernel
source share
5 answers

The fastest way to exchange a huge amount of data is memory mapping. The mmap call can be used in the device file, and the corresponding kernel driver can then decide to map the kernel memory to the user's address space. A good example of this is the video drivers for Linux, and I believe the frame buffer driver works the same. For a good explanation of how the V4L2 driver works, you have:

You cannot beat memory mapping for large amounts of data because there is no memory operation, such as an operation, the physical core memory is effectively allocated between the kernel and user space. Of course, as with all shared memory mechanisms, you must ensure some synchronization so that the kernel and user space do not think that they have ownership right at the same time.

+6
source share

Shared memory between the kernel and used space is doable.

http://kerneltrap.org/node/14326

Instructions / examples.

You can also use named pipe , which are pretty fast.

It all depends on what data you use, whether it is available at the same time and what the data structures are. Calls may be enough for simple data.

Linux / proc kernel FIFO / pipe

May also help

luck

+5
source share

You can also consider relays (formerly relayfs):

β€œBasically, relayfs are just a bunch of per-cpu kernel buffers that can be effectively written from the kernel code. These buffers are represented as files that can be copied and directly read from user space. The setup should provide the simplest possible mechanism, allowing you to write potentially large amounts of data to the kernel and "relay" to user space.

http://relayfs.sourceforge.net/

+4
source share

Obviously, you can use shared memory using copy_from_user etc., you can easily configure the character device driver, basically all you have to do is create file_operation structures, but this is far from the fastest way. I do not have benchmarks, but the system calls of modern systems should be the fastest. My reasoning is that it was most optimized. It used to be that to access the user β†’ to the kernel, it was necessary to create an interrupt, which would then go to the interrupt table (array), then find the handlex interrupt (0x80), and then go into kernel mode. It was very slow, and then the .sysenter instruction appeared, which basically makes this process very fast. Without going into details, .sysenter reads the CS: EIP register form immediately and the change is pretty quick. Shared memory, on the other hand, requires writing and reading from memory, which is infinitely more expensive than reading from a register.

+1
source share

Here is a possible compilation of the entire possible interface, although in a sense they overlap with each other (for example, a socket and a system call effectively use system calls):

Procfs Sysfs Configfs Debugfs Sysctl devfs (eg, Character Devices) TCP/UDP Sockets Netlink Sockets Ioctl Kernel System Calls Signals Mmap 
0
source share

All Articles