Skip to content

Day80 - IRQ Bottom Half and Deferred Work

Date: 2026-06-18

Objective

Learn how Linux defers interrupt processing from IRQ context to process context.

Topics covered:

  • Top Half
  • Bottom Half
  • Workqueue
  • Delayed Work
  • Threaded IRQ
  • IRQ_WAKE_THREAD
  • request_threaded_irq()
  • Workqueue vs Threaded IRQ

Key Concepts

Top Half

The Top Half runs in interrupt context.

Responsibilities:

  • Acknowledge interrupt source
  • Perform minimal processing
  • Wake deferred execution mechanism

Restrictions:

  • Must execute quickly
  • Cannot sleep
  • Should avoid lengthy operations

Typical flow:

IRQ
Top Half ISR
Return

Bottom Half

The Bottom Half runs in process context.

Responsibilities:

  • Deferred processing
  • Device state updates
  • Data transfer and parsing
  • Operations that may sleep

Typical flow:

IRQ
Top Half
Bottom Half

Workqueue

Workqueue defers processing to a worker thread.

Flow:

IRQ
ISR
queue_work()
Worker Thread
work->func()

Characteristics:

  • Shared worker thread
  • Runs in process context
  • Sleep allowed
  • Common deferred work mechanism

Delayed Work

Delayed Work combines a timer with Workqueue.

Flow:

schedule_delayed_work()
      Timer Delay
       queue_work()
       Worker Thread

Common use cases:

  • GPIO debounce
  • Retry operations
  • Delayed state transitions

Threaded IRQ

Threaded IRQ executes interrupt handling in a dedicated IRQ thread.

Flow:

IRQ
Top Half
IRQ_WAKE_THREAD
IRQ Thread
thread_fn()

Characteristics:

  • Dedicated IRQ thread
  • Runs in process context
  • Sleep allowed
  • Lower latency than shared workqueue

Workqueue vs Threaded IRQ

Item Workqueue Threaded IRQ
Worker Shared Dedicated
Trigger queue_work() IRQ_WAKE_THREAD
Context Process Context Process Context
Sleep Allowed Yes Yes
Typical Use Deferred Work Interrupt Processing
Ownership Workqueue Subsystem IRQ Subsystem
Latency Usually Higher Usually Lower

Lab Summary

Lab1 - Workqueue Bottom Half

Implemented:

  • work_struct
  • workqueue
  • queue_work()
  • Worker thread

Verified:

ISR
queue_work()
Worker Thread
work->func()

Lab2 - Delayed Work

Implemented:

  • delayed_work
  • schedule_delayed_work()
  • cancel_delayed_work()
  • mod_delayed_work()

Verified:

schedule_delayed_work()
Timer Delay
queue_work()
Bottom Half

GPIO debounce pattern:

IRQ
mod_delayed_work()
Last IRQ + Delay
Bottom Half

Lab3 - Threaded IRQ

Implemented:

  • request_threaded_irq()
  • IRQ_WAKE_THREAD
  • IRQ thread
  • thread_fn()

Verified:

IRQ
Top Half
IRQ_WAKE_THREAD
IRQ Thread
thread_fn()

Files

New:

utils/gpio-sim/workqueue.h
utils/gpio-sim/workqueue.c

utils/gpio-sim/delayed_work.h
utils/gpio-sim/delayed_work.c

utils/kernel_macro.h

Updated:

utils/gpio-sim/irq.h
utils/gpio-sim/irq.c

Result

Successfully implemented and verified three common Linux Bottom Half mechanisms:

  • Workqueue
  • Delayed Work
  • Threaded IRQ

Built a complete simulation path from interrupt handling to deferred processing.


Next Step

Day81 - Workqueue Internals

Topics:

  • System Workqueue
  • alloc_workqueue()
  • Worker Pool
  • Concurrency Control
  • flush_work()
  • cancel_work_sync()
  • Workqueue Lifecycle
  • Workqueue vs kthread