7

Im writing a custom device driver in linux that has to be able to respond very rapidly on interrupts. Code to handle this already exists in a user-space implementation but that is too slow as it relies on software constantly checking the state of the interrupt line. After doing some research, I found that you can register these interrupt lines from a kernel module, and execute a function given by a function pointer. However the code we want to execute is in the user-space, is there a way to call a function in the user-space from a kernel space module?

4 Answers 4

7

You are out of luck with invoking user-space functions from the kernel since the kernel doesn't and isn't supposed to know about individual user-space application functions and logic, not to mention that each user-space application has its own memory layout, that no other process nor the kernel is allowed to invade in that way (shared objects are the exception here, but still you can't tap into that from the kernel space). What about the security model, you aren't supposed to run user-space code (which is automatically considered unsafe code in the kernel context) in the kernel context in the first place since that will break the security model of a kernel right there in that instant. Now considering all of the above mentioned, plus many other motives you might want to reconsider your approach and focus on Kernel <-> User-space IPC and Interfaces, the file system or the user-mode helper API(read bellow).

You can invoke user space apps from the kernel though, that using the usermode-helper API. The following IBM DeveloperWorks article should get you started on using the usermode-helper Linux kernel API:

Kernel APIs, Part 1: Invoking user-space applications from the kernel

4
  • 1
    Invoking userspace applications from kernel is not recommended practice for interrupt handling. It is meant to be used only on very exceptional cases.
    – kauppi
    Mar 9, 2011 at 15:40
  • @kauppi - you are right, already experienced a couple of nightmare scenarios flashing through my eyes the minute i read your comment...maybe the only solution to the op's is porting that userspace code to the kernel code or integrate the appropriate kernel<->user-space ipc/interface for that matter to achieve user-space and kernel-space communication if the former is not possible.
    – Shinnok
    Mar 9, 2011 at 15:44
  • Invoking user space code from the kernel sounds like the ultimate attack vector for malware.
    – Bo Persson
    Mar 9, 2011 at 17:18
  • Are you sure , Kernel mode doesn't have access to user mode memory space? I believe there is no restriction for kernel mode, it can access everything. Aug 25, 2016 at 1:33
5

I think the easiest way is to register a character device which becomes ready when the device has some data.

Any process which tries to read from this device, then gets put to sleep until the device is ready, then woken up, at which point it can do the appropriate thing.

If you just want to signal readyness, a reader could just read a single null byte.

The userspace program would then just need to execute a blocking read() call, and would be blocked appropriately, until you wake it up.

You will need to understand the kernel scheduler's wait queue mechanism to use this.

2

Sounds like your interrupt line is already available to userspace via gpiolib? (/sys/class/gpio/...)

Have you benchmarked if gpio edge triggering and poll() is fast enough for you? That way you don't have to poll the status from the userspace application but edge triggering will report it via poll(). See Documentation/gpio.txt in kernel source.

If the edge triggering via sysfs is not good enough, then the proper way is to develop a kernel driver that takes care of the time critical part and exports the results to userspace via a API (sysfs, device node, etc).

1

I am also facing the same problem, I read this document http://people.ee.ethz.ch/~arkeller/linux/multi/kernel_user_space_howto-6.html, so planning to use signals. In my case there is no chance of losing signals, because
1. the system is closed loop, after signals executed then only I will get another signal.
2. And I am using POSIX real-time signals.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.