views:

433

answers:

4

I know that information exchange can happen via following interfaces between kernel and user space programs

  • system calls

  • ioctls

  • /proc & /sys

  • netlink

I want to find out

  • If I have missed any other interface?

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

  • Which one is the recommended way to communicate? (I think its netlink, but still would love to hear opinions)

+3  A: 

Shared Memory between kernel and usespace is doable.

http://kerneltrap.org/node/14326

For instructions/examples.

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

All this really depends on what data you are sharing, is it concurrently accessed and what the data is structured like. Calls may be enough for simple data.

http://stackoverflow.com/questions/313342/linux-kernel-proc-fifo-pipe

Might also help

good luck

Aiden Bell
A: 

You can obviously do shared memory with copy_from_user etc, you can easily set up a character device driver basically all you have to do is make a file_operation structures but this is by far not the fastest way. I have no benchmarks but system calls on moderns systems should be the fastest. My reasoning is that its what's been most optimized for. It used to be that to get to from user -> kernel one had to create an interrupt, which would then go to the Interrupt table(an array) then locate the interrupt handlex(0x80) and then go to kernel mode. This was really slow, and then came the .sysenter instruction, which basically makes this process really fast. Without going into details, .sysenter reads form a register CS:EIP immediately and the change is quite fast. Shared memory on the contrary requires writing to and reading from memory, which is infinitely more expensive than reading from a register.

daniel
Surely you meant "several orders of magnitude" instead of "infinitely" [/nitpick]
Piskvor
a system call still requires a context switch and saving/restoring registers, regardless of whether you `int` or `sysenter`. shmem writes go into the CPU cache, not directly to memory, so are fast unless cache misses.
Peter Cordes
+2  A: 

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

You can't beat memory mapping for large amount of data, because there is no memcopy like operation involved, the physical underlying memory is effectively shared between kernel and userspace. Of course, like in all shared memory mechanism, you have to provide some synchronisation so that kernel and userspace don't think they have ownership at the same time.

shodanex
+1  A: 

You may also consider relay (formerly relayfs):

"Basically relayfs is just a bunch of per-cpu kernel buffers that can be efficiently written into from kernel code. These buffers are represented as files which can be mmap'ed and directly read from in user space. The purpose of this setup is to provide the simplest possible mechanism allowing potentially large amounts of data to be logged in the kernel and 'relayed' to user space."

http://relayfs.sourceforge.net/

Denes Tarjan
Thanks. But it seems as if its one way traffic. I also want to transfer back to kernel.
Methos