HeadlinesBriefing favicon HeadlinesBriefing.com

BPF Workqueues Enable Sleepable eBPF Operations

DEV Community •
×

BPF Workqueues, introduced by Benjamin Tissoires at Red Hat, fill a critical gap in eBPF programming. These workqueues, merged into Linux v6.10, allow eBPF programs to perform sleepable and blocking operations by running in process context rather than the restricted softirq context. This breakthrough enables asynchronous tasks that were previously impossible, such as handling HID device I/O with timing delays or performing background cleanup without crashing the system. Workqueues provide a robust solution for the 'fast path + slow path' pattern, where immediate, performance-critical operations can be separated from more expensive, delayed tasks.

The need for workqueues arose from the limitations of `bpf_timer`, which operates in softirq context and cannot sleep or allocate memory. For instance, implementing keyboard macro functionality with delays or re-initializing devices after system wake required operations that `bpf_timer` could not handle. Tissoires' workqueues address these issues by allowing eBPF programs to schedule asynchronous work that can safely perform blocking operations. This capability is particularly valuable for HID device handling, network packet processing, and security monitoring, where sleepable operations are essential.

The tutorial demonstrates a complete example of using BPF Workqueues, showing how to embed a workqueue in a map, initialize it, and schedule asynchronous execution. This example triggers on the `unlink` syscall, schedules work, and verifies that both the main path and workqueue callback execute correctly. The program includes a userspace component that orchestrates the test and checks the results, ensuring that the asynchronous callback runs as expected. This hands-on approach helps developers understand the practical applications and benefits of workqueues in eBPF programming.