The kernel does not know that this particular interrupt is for a particular device.

The only thing it knows is that it must call irq_handler with raspi_gpio_devp as a parameter. (like this: irq_handler(irq, raspi_gpio_devp)).

If your irq line is shared, you should check if your device generated an IRQ or not. Code:

int irq_handler(int irq, void* dev_id) {
    struct raspi_gpio_dev *raspi_gpio_devp = (struct raspi_gpio_dev *) dev_id;
    if (!my_gpio_irq_occured(raspi_gpio_devp))
        return IRQ_NONE;
    /* do stuff here */
    return IRQ_HANDLED;
}

The interrupt handler runs in interrupt context. But you can access static variables declared outside the scope of the interrupt.

Usually, what an interrupt handler does is:

  • check interrupt status
  • retrieve information from the hardware and store it somewhere (a buffer/fifo for example)
  • wake_up() a kernel process waiting for that information

If you want to be really confident with the do and don't of interrupt handling, the best thing to read about is what a process is for the kernel.

An excellent book dealing with this is Linux Kernel Developpement by Robert Love.

Answer from Gilles Gregoire on Stack Overflow
🌐
O'Reilly
oreilly.com › library › view › linux-device-drivers › 0596005903 › ch10.html
10. Interrupt Handling - Linux Device Drivers, 3rd Edition [Book]
February 7, 2005 - Linux handles interrupts in much the same way that it handles signals in user space. For the most part, a driver need only register a handler for its device’s interrupts, and handle them properly when they arrive.
Authors   Jonathan CorbetAlessandro Rubini
Published   2005
Pages   636
🌐
XML.com
xml.com › ldd › chapter › book › ch09.html
Linux Device Drivers, 2nd Edition: Chapter 9: Interrupt Handling
In fact, even if the modem had been used earlier but wasn't in use at the time of the snapshot, it would not show up in the file; the serial ports are well behaved and release their interrupt handlers when the device is closed. The /proc/interrupts display shows how many interrupts have been delivered to each CPU on the system. As you can see from the output, the Linux kernel tries to divide interrupt traffic evenly across the processors, with some success. The final columns give information on the programmable interrupt controller that handles the interrupt (and which a driver writer need not worry about), and the name(s) of the device(s) that have registered handlers for the interrupt (as specified in the dev_name argument to request_irq).
Top answer
1 of 3
11

The kernel does not know that this particular interrupt is for a particular device.

The only thing it knows is that it must call irq_handler with raspi_gpio_devp as a parameter. (like this: irq_handler(irq, raspi_gpio_devp)).

If your irq line is shared, you should check if your device generated an IRQ or not. Code:

int irq_handler(int irq, void* dev_id) {
    struct raspi_gpio_dev *raspi_gpio_devp = (struct raspi_gpio_dev *) dev_id;
    if (!my_gpio_irq_occured(raspi_gpio_devp))
        return IRQ_NONE;
    /* do stuff here */
    return IRQ_HANDLED;
}

The interrupt handler runs in interrupt context. But you can access static variables declared outside the scope of the interrupt.

Usually, what an interrupt handler does is:

  • check interrupt status
  • retrieve information from the hardware and store it somewhere (a buffer/fifo for example)
  • wake_up() a kernel process waiting for that information

If you want to be really confident with the do and don't of interrupt handling, the best thing to read about is what a process is for the kernel.

An excellent book dealing with this is Linux Kernel Developpement by Robert Love.

2 of 3
7

The kernel doesn't know which device the interrupt pertains to. It is possible for a single interrupt to be shared among multiple devices. Previously this was quite common. It is becoming less so due to improved interrupt support in interrupt controllers and introduction of message-signaled interrupts. Your driver must determine whether the interrupt was from your device (i.e. whether your device needs "service").

You can provide context to your interrupt handler via the "void *arg" provided. This should never be process-specific context, because a process might exit leaving pointers dangling (i.e. referencing memory which has been freed and/or possibly reallocated for other purposes).

A global variable is not "in process context". It is in every context -- or no context if you prefer. When you hear "not in process context", that means a few things: (1) you cannot block/sleep (because what process would you be putting to sleep?), (2) you cannot make any references to user-space virtual addresses (because what would those references be pointing to?), (3) you cannot make references to "current task" (since there isn't one or it's unknown).

Typically, a driver's interrupt handler pushes or pulls data into "driver global" data areas from which/to which the process context end of the driver can transfer data.

🌐
Linux Kernel
kernel.org › doc › html › v4.12 › core-api › genericirq.html
Linux generic IRQ handling — The Linux Kernel documentation
The generic interrupt handling layer is designed to provide a complete abstraction of interrupt handling for device drivers. It is able to handle all the different types of interrupt controller hardware. Device drivers use generic API functions to request, enable, disable and free interrupts.
🌐
Linux Kernel Labs
linux-kernel-labs.github.io › refs › heads › master › labs › interrupts.html
I/O access and Interrupts — The Linux Kernel documentation
IRQF_ONESHOT interrupt will be reactivated after running the process context routine; Without this flag, the interrupt will be reactivated after running the handler routine in the context of the interrupt · Requesting the interrupt can be done either at the initialization of the driver (init_module()), when the device is probed, or when the device is used (e.g. during open). The following example performs the interrupt request for the COM1 serial port: #include <linux/interrupt.h> #define MY_BASEPORT 0x3F8 #define MY_IRQ 4 static my_init(void) { [...] struct my_device_data *my_data; int err; err = request_irq(MY_IRQ, my_handler, IRQF_SHARED, "com1", my_data); if (err < 0) { /* handle error*/ return err; } [...] }
🌐
EmbeTronicX
embetronicx.com › tutorials › linux › device-drivers › interrupts-in-linux-kernel
Interrupts in Linux Kernel - Linux Device Driver Tutorial Part 12
May 19, 2023 - The interrupt handler for a device ... In Linux, interrupt handlers are normal C functions, which match a specific prototype and thus enable the kernel to pass the handler information in a standard way....
🌐
EmbeTronicX
embetronicx.com › tutorials › linux › device-drivers › linux-device-driver-tutorial-part-13-interrupt-example-program-in-linux-kernel
Interrupts Example Program in Linux Kernel – Linux Device Driver Tutorial - Part 13
September 24, 2023 - The instruction will be executed while reading the device file of our driver (/dev/etx_device). Here I took the old source code from the sysfs tutorial. In that source, I have just added interrupt code like request_irq, free_irq along with the interrupt handler.
Find elsewhere
🌐
Linux Foundation
training.linuxfoundation.org › home › webinars › interrupt handling in linux device drivers
Interrupt Handling in Linux Device Drivers - Linux Foundation - Education
April 12, 2019 - Learn more about interrupt handling in this free “taste of training” from The Linux Foundation’s Training Program Director Jerry Cooperstein. This 20-minute clip provides useful information on “Interrupt Handling: Deferrable Functions and User Drivers.” The course covers top halves and bottom halves, methods for implementing bottom halves, how to create your own kernel threads, the most recent API for threaded interrupt handlers and how to do interrupt handling from inside user space instead of the kernel module.
🌐
O'Reilly
oreilly.com › library › view › linux-device-drivers › 0596000081 › ch09.html
9. Interrupt Handling - Linux Device Drivers, Second Edition [Book]
June 25, 2001 - Linux handles interrupts in much the same way that it handles signals in user space. For the most part, a driver need only register a handler for its device’s interrupts, and handle them properly when they arrive.
Authors   Jonathan CorbetAlessandro Rubini
Published   2001
Pages   592
🌐
Embien
embien.com › blog › advanced-interrupt-handling-in-Linux
Advanced Interrupt Handling Techniques in Linux
July 28, 2024 - It runs at a later time, when the system is less busy, with all interrupts enabled. This approach allows the top half to deal with new incoming interrupts without delay. Deferred interrupt handling is the cornerstone of advanced interrupt handling in production Linux drivers.
🌐
The Linux Documentation Project
tldp.org › LDP › tlk › dd › interrupts.html
Chapter 7 Interrupts and Interrupt Handling
The kernel's interrupt handling data structures are set up by the device drivers as they request control of the system's interrupts. To do this the device driver uses a set of Linux kernel services that are used to request an interrupt, enable it and to disable it.
🌐
O'Reilly
oreilly.com › library › view › linux-device-drivers › 0596000081 › ch14s07.html
The Interrupt Handler - Linux Device Drivers, Second Edition [Book]
June 25, 2001 - The Interrupt Handler Most hardware interfaces are controlled by means of an interrupt handler. The interface interrupts the processor to signal one of two possible events: a new... - Selection from Linux Device Drivers, Second Edition [Book]
Authors   Jonathan CorbetAlessandro Rubini
Published   2001
Pages   592
🌐
Embien
embien.com › blog › writing-interrupt-service-routines-for-linux
Writing Interrupt Service Routines for Linux
July 21, 2024 - The general steps involved in Linux ... the ISR: Use the request_irq() function to register our ISR with the Linux kernel, providing the necessary parameters such as the interrupt vector, the ISR function, and any relevant interrupt flags...
🌐
GitHub
github.com › naveenair1 › Linux_device_drivers › blob › master › Chapter__10_Interrupt_Handling.md
Linux_device_drivers/Chapter__10_Interrupt_Handling.md at master · naveenair1/Linux_device_drivers
Modules are also expected to be able to share interrupt lines with other drivers. The following instructions declared in <linux/interrupt.h> implement the interrupt registration interface with: int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *), unsigned long flags, const char *dev_name, void *dev_id); void free_irq(unsigned int irq, void *dev_id);
Author   naveenair1
🌐
Number Analytics
numberanalytics.com › blog › mastering-interrupt-handling-in-device-drivers
Mastering Interrupt Handling in Device Drivers
June 18, 2025 - To handle interrupts, device drivers must register interrupt handlers with the Linux kernel using the request_irq() function.
🌐
O'Reilly
oreilly.com › library › view › linux-kernel-programming › 9781801079518 › cfd1ca5d-cd0b-451d-8a35-31e65a09d2e4.xhtml
Handling Hardware Interrupts - Linux Kernel Programming Part 2 - Char Device Drivers and Kernel Synchronization [Book]
March 19, 2021 - Which to use whenDetermining which lock to use – in theoryDetermining which lock to use – in practiceUsing the mutex lockInitializing the mutex lockCorrectly using the mutex lockMutex lock and unlock APIs and their usageMutex lock – via [un]interruptible sleep?Mutex locking – an example driverThe mutex lock – a few remaining pointsMutex lock API variantsThe mutex trylock variantThe mutex interruptible and killable variantsThe mutex io variantThe semaphore and the mutexPriority inversion and the RT-mutexInternal designUsing the spinlockSpinlock – simple usageSpinlock – an example driverTest – sleep in an atomic contextTesting on a 5.4 debug kernelTesting on a 5.4 non-debug distro kernelLocking and interruptsUsing spinlocks – a quick summarySummaryQuestionsFurther reading
Author   Kaiwan N. Billimoria
Published   2021
Pages   452
🌐
Linux Kernel Labs
linux-kernel-labs.github.io › refs › heads › master › lectures › interrupts.html
Interrupts — The Linux Kernel documentation
In the first phase the kernel will run the generic interrupt handler that determines the interrupt number, the interrupt handler for this particular interrupt and the interrupt controller. At this point any timing critical actions will also be performed (e.g. acknowledge the interrupt at the interrupt controller level). Local processor interrupts are disabled for the duration of this phase and continue to be disabled in the next phase. In the second phase, all of the device driver's handlers associated with this interrupt will be executed.
🌐
O'Reilly
oreilly.com › library › view › linux-device-drivers › 9781785280009 › 839a48b3-9dd2-4b87-971f-115b09ee7d37.xhtml
The interrupt handler - Linux Device Drivers Development [Book]
October 20, 2017 - This consists of fetching the IRQ number from a DT, and mapping it into Linux IRQ, thus registering a function callback for it. The driver code to do this is quite simple: int irq = platform_get_irq(pdev, 0); ret = request_irq(irq, imx_rxint, ...
Author   John Madieu
Published   2017
Pages   586