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.
Read some other modules that do what you want.
There are lots of options for how to do this in the Linux kernel, including:
/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.
There are couple of well known methods to communicate from user space to kernel space.
/proc
, /sys
, /configfs
, /debugfs
Standardread()
,write()
, open()
, close()
, fork()
ioctl
for char drivers. With copy_to_user
and copy_from_user
netlink
socket - mostly used by network subsystemudev
and uevent
based event mechanisim from kernel space to user
space. Read more https://stackoverflow.com/a/23149574/775964
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/
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With