10

I am a new comer to Linux Kernel Module programming. From the material that I have read so far, I have found that there are 3 ways for a user program to request services or to communicate with a Linux Kernel Module

  1. a device file in /dev
  2. a file in /proc file system
  3. ioctl() call

Question: What other options do we have for communication between user program and linux kernel module?

skaffman
  • 398,947
  • 96
  • 818
  • 769
binW
  • 13,220
  • 11
  • 56
  • 69

5 Answers5

10

Your option 3) is really a sub-option of option 1) - ioctl() is one way of interacting with a device file (read() and write() being the usual ways).

Two other ways worth considering are:

  • The sysfs filesystem;
  • Netlink sockets.
caf
  • 233,326
  • 40
  • 323
  • 462
  • Netlink sockets look promising but haven't been able to find a working example which uses netlink sockets. Managed to write my own but still lot of questions unanswered. – binW Jul 23 '10 at 11:18
  • 1
    @binW I've also linked to a runnable example at: https://stackoverflow.com/a/51206012/895245 Socket vs `poll` asked at: https://stackoverflow.com/questions/16727212/how-are-netlink-sockets-in-the-linux-kernel-different-from-polling-from-userland – Ciro Santilli OurBigBook.com Jul 06 '18 at 08:25
6

Basically, many standard IPC mechanisms — cf. http://en.wikipedia.org/wiki/Inter-process_communication — can be used:

  1. File and memory-mapped file: a device file (as above) or similarly special file in /dev, procfs, sysfs, debugfs, or a filesystem of your own, cartesian product with read/write, ioctl, mmap

  2. Possibly signals (for use with a kthread)

  3. Sockets: using a protocol of choice: TCP, UDP (cf. knfsd, but likely not too easy), PF_LOCAL, or Netlink (many subinterfaces - base netlink, genetlink, Connector, ...)

Furthermore,

 4. System calls (not really usable from modules though)

 5. Network interfaces (akin to tun).

Working examples of Netlink — just to name a few — can be found for example in

  • git://git.netfilter.org/libmnl (userspace side)
  • net/core/rtnetlink.c (base netlink)
  • net/netfilter/nf_conntrack_netlink.c (nfnetlink)
  • fs/quota/netlink.c (genetlink)
user502515
  • 4,346
  • 24
  • 20
4

This Linux document gives some of the ways in which the kernel and user space can interact(communicate). They are the following.

  • Procfs, sysfs, and similar mechanisms. This includes /dev entries as well, and all the methods in which kernel space exposes a file in user space (/proc, /dev, etc. entries are basically files exposed from the kernel space).
  • Socket based mechanisms. Netlink is a type of socket, which is meant specially for communication between user space and kernel space.
  • System calls.
  • Upcalls. The kernel executes a code in user space. For example spawning a new process.
  • mmap - Memory mapping a region of kernel memory to user space. This allows both the kernel, and the user space to read/write to the same memory area.

Other than these, the following list adds some other mechanisms I know.

  • Interrupts. The user space can raise interrupts to talk to kernel space. For example some CPUs use int80 to make system calls (while others may use a different mechanism like syscall instruction). The kernel has to define the corresponding interrupt handler in advance.
  • vDSO/vsyscall - These are mechanisms in Linux kernel to optimize execution of some system calls. The idea is to have a shared memory region, and when a process makes a system call, the user space library gets data from this region, instead of actually calling the corresponding system call. This saves context switch overhead.
Sahil Singh
  • 3,352
  • 39
  • 62
4

This includes all types with examples :)

http://people.ee.ethz.ch/~arkeller/linux/kernel_user_space_howto.html

Santi1986
  • 463
  • 1
  • 4
  • 10
  • 1
    Your right the link contains a lot of useful information but links are not answers. Please see http://stackoverflow.com/questions/how-to-answer which explains why plain links aren't what SO wants. – stsquad Jan 12 '12 at 12:32
  • i answered your ""What other options do we have for communication between user program and linux kernel module?"" part of the question..if you had asked wot other options and how do they work i would i ve written a couple of pages about them,the reason why i gave you that link as it has working examples along with them, which i thought was self explanatory. – Santi1986 Jan 16 '12 at 10:09
  • 1
    As the stackoverflow pages explain links to external pages can be transitory. If the linked to page disappears the answer suddenly becomes useless. No one is suggesting you need to write an essay but a summary of the options would have made it a more useful answer. – stsquad Jan 16 '12 at 14:30
  • 1
    Well the situation described by @stsquad happened. It's a dead link. – Qeek Sep 21 '17 at 08:24
  • @Qeek academics pages are the most breakful ones :-) Here are a bunch of GitHub + Stack Exchange links that will never break: https://stackoverflow.com/a/51206012/895245 – Ciro Santilli OurBigBook.com Jul 06 '18 at 08:25