Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communication between kernel threads in a linux kernel module

I'm just beginning to learn the tricks of making a kernel module on linux kernel 2.6. What I'm looking to do is have 3 kernel threads, called the slaves, that need to send data to a 4th kernel thread, called master, and receive their respective responses. The slaves can request at any time, which means I will need some sort of a queue structure and a way to redirect responses to the correct thread.

First I looked at implementing my own queue structure to queue incoming requests - but how do I signal the master of this? I don't want the master to keep polling (as in the case of spinlocks/semaphores). I have a feeling there is a better way to communicate between threads.

Due to lack of documentation (and admittedly inferior searching skills), I'm at a loss on how to implement this. Can you point me in the right direction?

like image 715
Ram Avatar asked Oct 10 '22 22:10

Ram


1 Answers

You are facing two distinct problems:

  1. The actual communication between the slaves and the master. You can use the FIFO implementation in the kernel (kernel/kfifo.c).
  2. You need demultiplexing for the master without busy-waiting/polling. You can do it just like in userspace, via poll/epoll on an "event file descriptor" (eventfd). Take a look at the kernel-level API in include/linux/eventfd.h (the implementation is in fs/eventfd.h).

You should probably use a [kfifo, event file] pair for each slave thread. The master thread blocks in a do_poll() call and, when woken up, is able to use the right FIFO based on the fd that was "signaled". Take a look at fs/select.c to have an idea about how you should call do_poll().

You might want to use mutexes to guard the FIFO.

like image 57
Mircea Avatar answered Oct 12 '22 12:10

Mircea