Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to communicate a kernel module with a user space program?

This question seems to be simple, but I want to send an event to notify my user space program that the module buffer is ready to be read.

For example, I have a buffer in my kernel module and its data will be consumed by the user space program. If all the data was consumed, the kernel module has to notify my program when a new data arrived.

It's a typically problem of producer/consumer. The producer is a kernel module and the consumer is a user space program.

Today, I send a signal to my program (event) and access the data buffer using the ioctl function. But I don't know if this approach is good enough to solve this kind of problem. I'm afraid to use netlink or memory mapping unnecessarily to solve this.

like image 692
jcfaracco Avatar asked Jan 07 '14 15:01

jcfaracco


3 Answers

Read some other modules that do what you want.

There are lots of options for how to do this in the Linux kernel, including:

  • virtual filesystems, e.g. /proc, /sys, configfs, relayfs (really look at relayfs)
  • netlink
  • blocking syscalls
  • poll() / epoll() & related

/proc is probably the easiest to start with since it has been around forever and there is a ton of documentation on how to use it. Create a virtual file that maps to your buffer, then have your userspace app open an fd and use select. Simple and ubiquitous. There are more modern and "better" ways - they will inevitably be described in terms of /proc + select() so learning those first will teach you something useful.

like image 91
Tobert Avatar answered Nov 08 '22 16:11

Tobert


There are couple of well known methods to communicate from user space to kernel space.

  • Virtual file system like /proc, /sys, /configfs, /debugfs Standard
  • system call like read(),write(), open(), close(), fork()
  • ioctl for char drivers. With copy_to_user and copy_from_user
  • netlink socket - mostly used by network subsystem
  • Use udev and uevent based event mechanisim from kernel space to user space. Read more https://stackoverflow.com/a/23149574/775964
  • Sending signal from kernal space to user space thread. Full example at https://embetronicx.com/tutorials/linux/device-drivers/sending-signal-from-linux-device-driver-to-user-space/
  • Using common memory which can be used by user space/kernel space both like ION memory or DMABuf read more here https://lwn.net/Articles/480055

Above listed all methods use common method to call remote procedure call (RPC) from user space to kernal space.

If you can understand this http://articles.manugarg.com/systemcallinlinux2_6.html then you can develope your own framework in linux kernel for user space to kernel space communication.

like image 20
Jeegar Patel Avatar answered Nov 08 '22 14:11

Jeegar Patel


For sharing status information of your kernel module, I suggest using any one of the virtual filesystems available (eg: /proc, /sys or if you're debugging debugfs).

However, if you want to transfer large amounts of data to and fro the kernel, then you can look at using netlink sockets or use mmap which is probably the fastest mechanism for transferring data although you will have to implement your own notification mechanism to inform the userspace program when data is ready and vice-versa (for consumption).

See this link for all possible options available under Linux.

like image 9
Mandeep Sandhu Avatar answered Nov 08 '22 14:11

Mandeep Sandhu