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

5

10

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.

Hexapla answered 7/1, 2014 at 15:32 Comment(4)
What about placing the the process in a wait_queue and waking it from the kernel thread when they're ready to go?Lentz
Well, it depends, if your module an I/O driver?Decurved
@PauloBu, your suggestion is good. I will consider it. =)Hexapla
@Hexapla I'm glad I could help. Providing an answer to these question will be difficult though since is very specific and deep in content :)Lentz
E
16

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.

Eridanus answered 7/1, 2014 at 18:15 Comment(0)
P
9

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.

Papilionaceous answered 4/4, 2014 at 12:57 Comment(0)
C
2

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

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.

Carbide answered 28/3, 2021 at 5:4 Comment(0)
H
0

You can define a variable in user space and map that to kernel. When the data is ready, kernel will set that variable. In user space, the application can poll the variable. When set, an ioctl can be done to read the data from the kernel. The drawback is that you will have to poll the variable in user space in every defined time but the upside is that you can avoid the locking mechanisms.

Holbrooke answered 3/7, 2020 at 7:58 Comment(0)
T
0

You can achieve this with Poll or select system call in the userspace and poll fops in the kernel space. It will be blocked with the poll system call from the userspace. When the data is ready send notification/event to userspace from kernel space. On receiving this event, process can read the data.

Towle answered 15/3, 2021 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.