If you run a selective analytics query over a large dataset sitting on an NVMe SSD, the bottleneck is rarely the host CPU itself. Most of the time and energy goes into pulling raw blocks off the flash channels, pushing them across PCIe into host RAM and scanning records that get thrown away immediately after. On selective queries, 90% to 99% of the bytes transferred never survive the filter.

Networking went through a similar shift about ten years ago with SmartNICs and eBPF/XDP, where packet filtering moved closer to the wire. On the storage side, the NVMe specification is trying to standardize the same idea with Computational Programs (TP4091) and Subsystem Local Memory (SLM). You ship a small sandboxed eBPF program to the storage controller and let it compact matching records locally before anything touches the host bus.

The catch is the software stack on the controller. Embedded Linux brings scheduling jitter and around 15 ms of transaction overhead, while running an eBPF interpreter in bare metal is too slow to keep up with internal flash channels. For my B.Sc. thesis, I wanted to see what happens when you throw away the OS completely and run a native eBPF-to-RISC-V (RV64IMA) JIT compiler bare-metal on real hardware.

Source code and benchmarks are on GitHub: luigimasdea/ebpf-nvme-jit.


Turning a VisionFive 2 into an AMP Prototype

Since I didn’t have an open commercial CSD controller I could flash with custom firmware, I turned a VisionFive 2 (StarFive JH7110 quad-core SiFive U74 at 1.5 GHz) into an Asymmetric Multiprocessing (AMP) testbed.

  • Host Domain (Harts 1 to 3 / CPUs 0 to 2): runs standard Linux SMP, handles storage I/O, loads the eBPF program and collects filtered results.
  • CSD Worker Domain (Hart 4 / Core 3): runs bare-metal with no kernel, no libc, no interrupts and virtual memory turned off (satp = 0).

AMP Architecture Overview on VisionFive 2

At boot, Linux is confined below 2.5 GB via mem=2500M. That leaves a 16 MB physical DRAM window at 0x222000000 completely outside the OS page allocator:

Physical DRAM Partition (0x222000000, 16 MB Reserved)
├── 0x222000000 (1 MB)  Firmware (.text, .rodata, .data, .bss)
├── 0x222100000 (1 MB)  Bare-metal Stack
├── 0x222200000 (2 MB)  Executable JIT Buffer (RWX)
├── 0x222400000 (1 MB)  VCON (4 KB) & Lock-Free NVMe SQ/CQ Ring Buffers
└── 0x222500000 (11 MB) SLM Namespace (2 MB Double Buffers A & B)

I didn’t want to reboot or juggle manual sysfs steps between test runs, so the host loader handles everything automatically. It maps 0x222000000 via /dev/mem, writes firmware.bin in place, offlines CPU 3 from Linux and inserts a small kernel module (vf2_kick.ko) that issues sbi_hart_start through OpenSBI HSM. Hart 4 wakes up directly in boot.S, sets up its stack and enters a tight polling loop on the Submission Queue, walking programs through the TP4091 lifecycle (LOAD, ACTIVATE, EXECUTE, UNLOAD).

Without a scheduler or interrupt handler preempting the core, execution variance stays within ±0.18%.


A Two-Pass JIT with Zero Allocations

Writing a JIT for a bare-metal firmware environment changes how you design the compiler. There is no malloc, no LLVM backend and no room for heavy optimization passes on short-lived filtering kernels. Everything runs out of static buffers.

The compiler translates standard eBPF bytecode into fixed 32-bit RV64IMA instructions in two linear sweeps:

  1. Pass 1 (PASS_ANALYZE): walks the eBPF instructions, computes native instruction offsets and records jump targets. Because I intentionally avoided compressed 16-bit RVC instructions, every emitted instruction is 4 bytes wide and branch offsets never shift between passes.
  2. Pass 2 (PASS_EMIT): writes raw machine words into the executable buffer at 0x222200000 and finishes with a fence.i instruction so the instruction cache sees the newly emitted code.

Full activation takes 33.7 µs, while NVMe queue dispatch takes 3.2 µs.

Register Caching (+11% Throughput)

eBPF only gives you 10 general-purpose registers (r0–r9) plus a read-only frame pointer (r10), which map directly to a0–a5, s1–s4 and fp on RISC-V. Once a filtering kernel grows beyond trivial predicates, Clang runs out of registers and starts spilling intermediate values to the stack (*(r10 - offset)).

RISC-V has 32 physical integer registers, so leaving half of them unused while hitting memory inside a tight record loop made no sense. I added a lightweight static escape analysis pass over r10. Whenever r10 is only used for direct stack loads and stores (meaning no pointer to a stack slot escapes), the JIT binds up to 13 stack slots to unused physical registers (s5–s11, t3–t6, a6–a7).

That optimization alone boosted filtering throughput by +11% with almost no extra compile time.

To stay spec-compliant without a runtime trap handler, the emitter also clears the upper 32 bits after every ALU32 operation (since addw/subw on RV64 sign-extend by default) and guards every DIV/MOD instruction against division by zero.


Benchmarks & Results

For data movement, the host and Hart 4 overlap I/O with compute through two 2 MB ping-pong buffers in SLM (Buffer A and Buffer B). While Hart 4 filters and compacts matching records in place inside one buffer, the next chunk is staged into the other.

When testing streaming behavior across chunk sizes, an interesting split showed up between external storage and simulated internal flash channels:

Optimal Chunk Size Inversion: Physical PCIe SSD vs. In-RAM ONFI Bus

Reading from a real M.2 NVMe SSD over ext4 (pread) peaks at 512 KiB chunks because smaller blocks double syscall and kernel overhead. On the simulated internal ONFI 5.0 channel (~80 µs DMA latency per 256 KiB without an OS in the path), the sweet spot drops to 256 KiB at 970 MB/s because fine-grained transfers ignite the double-buffering pipeline immediately.

In pure compute microbenchmarks inside SLM, the JIT reaches 2.1 GB/s in L1 cache and 1.6 GB/s in DRAM, landing around 61% to 64% of static GCC -O2 compiled for the host. Past the 2 MiB L2 boundary, DRAM bandwidth dominates anyway and narrows the gap.

MetricMeasured Result
NVMe Command Dispatch3.2 µs
Full JIT Activation Overhead33.7 µs
Execution Variance (Jitter)±0.18%
Compute Efficiency vs. Static GCC -O261%–64% (2.1 GB/s L1 peak, 1.6 GB/s DRAM)
In-Storage Filtering (ONFI 5.0 model, 256 KiB chunks)970 MB/s
End-to-End Speedup (1 GiB dataset, 3% selectivity)8.70x
PCIe Bus Traffic Reduction96.6%

Testing end-to-end across a 1 GiB dataset (65.5 million records) makes the trade-off very clear:

End-to-End Speedup and PCIe Bus Traffic Reduction

At 3% selectivity, compacting records inside SLM cuts PCIe transfer volume by 96.6%, yielding an 8.70x end-to-end speedup over pulling the whole dataset into host RAM first. Above roughly 25% selectivity, the amount of matching data starts saturating the platform’s PCIe return link (measured at 132.9 MB/s), so speedup gradually converges down toward 1.06x at 100%.


Wrapping Up & Limitations

Building the linker script, OpenSBI bootstrap, lock-free queues and opcode emitter together highlighted two practical takeaways:

  • Compile speed dominates for short-lived filters. Keeping the JIT two-pass and allocation-free keeps activation at 33.7 µs, which pays off much more than spending milliseconds on heavier optimization passes.
  • Hardware edge cases eat most of the debugging time. Tracking down silent hangs under satp = 0, sorting out fence.i synchronization and making sure ALU32 zero-extension matched eBPF semantics took far longer than writing the actual encoder.

The main limitations right now are that the JIT targets RV64IMA without the compressed (C) or vector (RVV) extensions. On the storage side, the internal flash channel is emulated in DRAM (~80 µs DMA model) alongside real M.2 pread tests rather than running on custom FPGA NAND hardware. Scaling the runtime across multiple cores and adding RVV SIMD emission for predicate evaluation are the obvious next steps.

If you want to read the JIT emitter (firmware/src/jit.c), check the OpenSBI core-kicker (tools/kick_core/vf2_kick.c) or run the benchmark suite on a VisionFive 2, the whole repo is Apache-2.0 licensed on GitHub: github.com/luigimasdea/ebpf-nvme-jit.