Introduction
Extended Berkeley Packet Filter (eBPF) has emerged as a critical technology for the Linux kernel, enabling high-performance networking, security, and observability without requiring kernel recompilation. However, the utility of eBPF is constrained by the Kernel Verifier, which enforces strict safety and complexity limits. Programs that exceed instruction count limits or exhibit complex memory access patterns are rejected.
While standard compilers like Clang generate functional eBPF bytecode, they often produce suboptimal sequences because they rely on fixed, manually written optimization patterns. Traditional superoptimizers attempt to find optimal instruction sequences through exhaustive search, but their high computational cost—often requiring hours for a single program—makes them impractical for standard development workflows. EPSO (Efficient BPF Superoptimizer) addresses this by combining the precision of superoptimization with the speed of rule-based caching.
System Architecture
EPSO introduces a dual-phase architecture designed to bridge the gap between slow, high-quality synthesis and fast, lower-quality manual rules.
- Offline Phase (Rule Discovery): EPSO analyzes a large corpus of sample eBPF programs to identify common instruction patterns. It uses an enumerative search to find the shortest semantically equivalent instruction sequences for these patterns, which are then stored as generalized rewrite rules.
- Online Phase (Optimization): When a developer compiles a new eBPF program, EPSO acts as a fast pattern matcher. It identifies code segments that match its pre-computed library of 795 rules and applies the optimizations instantly.
Core Methodologies
Semantic Slicing and Rule Abstraction
Rather than optimizing fixed windows of code, EPSO employs semantic slicing. This technique traces the computation chain of specific registers or memory regions to create "slices" of related instructions. By isolating these chains, EPSO can discover optimizations that might be obscured by interleaved, unrelated instructions.
To maximize the utility of discovered rules, EPSO performs Rule Abstraction. Discovered optimizations are normalized; for instance, a rule involving specific registers like r1 and r2 is abstracted to apply to any register pair (rA, rB) and relative memory offsets. This allows a relatively small library of rules to cover a vast array of real-world code patterns.
Search Pruning and Verifier-Awareness
To manage the massive search space during the offline phase, EPSO utilizes two primary pruning strategies:
- Distance Estimation: A heuristic that calculates the minimum instructions needed to reach a target state. If the required steps exceed the remaining search budget, the branch is pruned.
- Failed State Memoization: The system remembers "dead ends" in the search tree to avoid redundant exploration.
Crucially, EPSO is Verifier-Aware. It explicitly checks for kernel-specific constraints, such as 16-bit memory alignment, during synthesis. This ensures that the optimized code is not only faster but is guaranteed to pass the Linux kernel verifier.
Evaluation and Results
EPSO was evaluated against diverse real-world eBPF projects, including Cilium, Katran, and the Linux kernel samples, as well as the K2 and Merlin benchmark suites.
- Code Compactness: EPSO achieved an average 24.37% reduction in program size, with a peak reduction of 68.87%. It outperformed state-of-the-art tools like K2 across all benchmarks.
- Execution Efficiency: Optimized programs demonstrated a 6.60% average runtime speedup. In high-load networking scenarios, this translated to a 5.5% increase in Queries Per Second (QPS).
- Performance and Safety: In rule-matching mode, EPSO's overhead is negligible. Furthermore, 100% of the programs optimized by EPSO successfully passed the Linux kernel verifier.
Conclusion
EPSO demonstrates that superoptimization can be made practical for eBPF development by treating it as a data-gathering exercise. By offloading complex synthesis to an offline phase and utilizing semantic slicing for rule abstraction, EPSO provides a high-performance optimizer that significantly reduces program complexity and improves execution speed without sacrificing safety or developer productivity.
Summary updated