Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communication between Linux kernel and user space program

I'm currently writing a Linux kernel module, and have problems implementing its communication with user space programs.

This kernel module needs to receive tasks issued by a user space program, and send results back to user space program after completion. The user space program should be blocked while the kernel module is doing its job.

I think a kernel-user space IPC or an Unix socket would be sweet, but I had no luck finding an example by Google.

Currently my ugly solution is to export a chardev and let user space program write requests to the device file, and read results from it. But I can only issue one request per open() call, and this is causing new problems. I really need an IPC or socket-like thing. Thanks!

like image 620
Santa Zhang Avatar asked Nov 16 '11 02:11

Santa Zhang


People also ask

How does user space and kernel space communicate?

1) If using netlink, user process only needs to add a new type of netlink protocol definition, and then user process can exchange data with kernel through socket API; 2) Netlink is an asynchronous communication mechanism. The information transmitted between kernel and user space saves in the socket buffer queue.

How does user communicate with kernel?

The user application will be able to communicate with the kernel module and exchange data. The user application uses ioctl calls to send data to the kernel module. In the following example, these ioctl calls can be used to send application-specific details or send any updates at a later point of time.

Who is establish communication between user and kernel in Linux?

Unicast is useful to establish a 1:1 communication channel between a kernel subsystem and one user-space process. Typically, unicast channels are used to send commands to kernel-space, to receive the result of commands, and to request some information to a given kernel subsystem.

Can we link user space applications to kernel space directly?

User space programs cannot access system resources directly so access is handled on the program's behalf by the operating system kernel. The user space programs typically make such requests of the operating system through system calls. Kernel threads, processes, stack do not mean the same thing.


1 Answers

There are several ways to implement this.

The easiest is to use the proc file interface to communicate, especially if the message and the result are less than one page in size.

General Sequence would be as under:

  • Implement proc_open(), proc_read() and proc_write(); proc_close();
  • Open and close can implement locking so that only one instance of userspace program can actually access the module request engine.

  • the task request is sent via a write to the proc file,

  • The write function will return successfully if the module understands the command, before returning the program will initialize the request processing, the processing can actually take place when the proc file is read if it is trivial. If the processing is significantly complex then i suggest that you read up on bottom halves1 (you can simply start a working queue).

  • The read either triggers the "processing you want the module to do". or waits for the BH to finish the processing in case you do it that way. You can use a spinlock or a mutex to control flow.

  • The kernel processing returns the result upon completion.

like image 135
Ahmed Masud Avatar answered Sep 21 '22 22:09

Ahmed Masud