|
|
Log in / Subscribe / Register

A helper library for BPF arenas

By Daroc Alden
June 24, 2026

LSFMM+BPF

BPF arenas are areas of memory (potentially shared with user space) where programs have free reign to build their own data structures, unburdened by the verifier's bounds checks. Many of those data structures are potentially usable in multiple programs. Emil Tsalapatis brought his work on libarena, a library containing generic utilities for use in BPF arenas, to the 2026 Linux Storage, Filesystem, Memory-Management, and BPF Summit. Although the library is already available as part of the kernel, it is still in its early stages and he has more work planned.

Tsalapatis works on sched_ext, a project that has seen him write a number of components based on BPF arenas that he believes could be reused. In particular, he sees potential for having a universally agreed-upon memory allocator for arenas, and a set of common data structures that use it. He also wants to see about adding better debugging capabilities to arenas; arenas may obviate the need to fight with the verifier, but that also means that there can be ordinary memory-safety problems that, while not a threat to the kernel, still need to be found and fixed.

[Emil Tsalapatis]

There are downsides to using arenas for everything, Tsalapatis admitted. Since BPF programs can write to arenas at any time, unchecked, the verifier cannot trust pointers to kernel objects stored in an arena to remain valid. Therefore, the verifier bans storing pointers to kernel objects in arenas at all; those pointers still need to be stored in other types of BPF map. Even with that caveat, he sees focusing on arenas as being worth the hassle. The end goal, he says, is to make BPF C as easy to write as normal C, and arenas are a big part of that.

He envisions libarena as providing a standard library for BPF programs. It would be plain C code that is compiled and linked into BPF programs in the normal way, not an additional kernel interface to maintain. That also means it is optional — small BPF programs would not need to link against it unless they used its features. The library source code would live directly in the kernel tree, and be tested against the verifier to make sure the algorithms and data structures it provides do verify. Since libarena corresponds to a kernel version, it can also make use of the latest verifier features without worry.

One member of the audience questioned how libarena would support migrating programs across kernel versions. BPF programs that use kernel interfaces can use "compile once — run everywhere" (CO-RE) relocations for that purpose, but these wouldn't work with code that is actually linked into BPF programs. Tsalapatis said that libarena wouldn't be supported on kernel versions prior to its introduction, but that with some care it could be made forward-compatible, so that people can build against the libarena version from the oldest kernel version that they wish to target, with no need for CO-RE relocations.

The questioner was doubtful; they are currently trying to fix a problem where a program verifies on the 6.9 kernel but not the 6.19 kernel, so forward compatibility is a real problem. Perhaps programs could link in the version of libarena corresponding to the version of the kernel in use, Tsalapatis suggested. The hope is to have a stable enough interface that linking different versions for different kernels should not cause problems.

He then explained how he envisioned programmers making use of the library. The source lives in the kernel tree, but he wants to periodically export it to a separate Git repository so that users can add it as a self-contained Git submodule, the same way that libbpf works. The build system would call into libarena's makefile to produce libarena.bpf.o. The main BPF program would include libarena's headers and link against the object.

Another audience member asked whether this would require old kernels to be added to BPF's continuous-integration testing, but Tsalapatis reiterated that backward compatibility was not a priority. Alexei Starovoitov suggested worrying about that for the 7.5 kernel, noting that libarena was in the early stages of development.

As of the 7.2 kernel, libarena will be available for experimental use. It contains a buddy allocator and some basic scaffolding for future work, but no actual data-structure implementations yet. It also has the hooks needed to work with Clang's address sanitizer, ASAN, although only with the in-development version of Clang. The allocator is usable, handling all sizes of allocation, but not particularly fast yet. Tsalapatis found writing the allocator to be a good case study for arenas, uncovering a lot of edge cases that needed to be addressed.

In the future, libarena's allocator might end up using the design of mimalloc instead of a plain buddy allocator. Mimalloc was designed for use with functional-language runtimes, and performs well with multithreading and short-lived allocations, two features that Tsalapatis expects to be important for performance in the BPF programs of the future. It's also a simple and efficient design, he said.

Previously, he had experimented with using a slab allocator for its reduced fragmentation, but he found it clunky for arbitrary programs. For sched_ext, it worked well because there were only "like three types" of structure needed, but that's not true of typical programs.

In the future, Tsalapatis would like to offer red-black trees, B-trees, bitmaps, and Lev-Chase queues (also known as Chase-Lev queues) as part of libarena at a minimum. Even if people start using large language models to write BPF programs, he thought there was value in having manually coded data structures in libarena that worked with the verifier. "In my experience, I've found that seeding agents with good code lets them one-shot hard problems."

He then went over what the ASAN hooks do. When an arena is created, pages are mapped lazily — touching a new page causes a page fault. The BPF program can listen for these events and use them to also populate a metadata region containing ASAN's information about which areas of memory are okay to access. Allocating a section of memory marks it as safe with an eight-byte granularity, and freeing it removes the mark. The compiler augments pointer dereferences to check the state of the metadata before loading or storing a value. The work needs the in-development version of Clang because the compiler previously assumed that the metadata memory was always in address space zero, but BPF arenas use address space one, so Tsalapatis had to add a flag for that. (Clang numbers the available address spaces, while GCC names them.)

One audience member asked whether Tsalapatis had considered including code in libarena to navigate some of the kernel data structures that are difficult to navigate correctly. He had considered it, he said, but thought that it was better to keep the scope of libarena small for now, and focus on providing the core things that every program would need. At that point, time for the session ran out, but work on libarena has continued since the talk, with some of the planned-on data structures now available to use.


Index entries for this article
KernelBPF
ConferenceStorage, Filesystem, Memory-Management and BPF Summit/2026


to post comments

Reintroducing all the problems of C

Posted Jun 24, 2026 18:50 UTC (Wed) by quotemstr (subscriber, #45331) [Link] (1 responses)

> He also wants to see about adding better debugging capabilities to arenas; arenas may obviate the need to fight with the verifier, but that also means that there can be ordinary memory-safety problems that, while not a threat to the kernel, still need to be found and fixed.

Once you have a number of components using raw pointers into arenas, you introduce all the same memory safety problems that plague normal C programs. When BPF programs start doing things like scheduling processes and making access control decisions, these bugs can become exploitable.

> make BPF C as easy to write as normal C

If normal C is easy to write, it's unsafe. If C is safe, it's neither normal nor easy to write. Better to make it a non-goal and provide dedicated map types for each data storage need. If the BPF program is complex enough that it needs to be able to malloc from an arena, the program should be a userspace program with a BPF interface. Otherwise, in the limit, the BPF world becomes just another *kind* of userspace, albeit with isolation enforced via code analysis instead of page tables and hardware privilege levels.

Instead of making BPF look more like userspace, I'd make it easier to integrate BPF *with* userspace, e.g. using io_uring to help BPF programs offload state management and policy enforcement to userspace helpers.

Reintroducing all the problems of C

Posted Jun 24, 2026 20:59 UTC (Wed) by kkdwvd (subscriber, #179603) [Link]

The main point is separation of responsibilities. The kernel verifier should simply ensure the kernel's availability, but it shouldn't get into the business statically analyzing memory safety for data structures the program maintains for its internal state. That is both hard to do (in a language independent way at the bytecode level), and also undesirable. We would have to enforce some sort of aliasing regime that is unnatural (except perhaps in Rust) to enable local reasoning.

In the future, we may want even stronger functional correctness for, say, BPF CPU schedulers. It would be easier to compose and lift existing toolchains (Dafny, Verus) in user space and compile them down to native languages (which then successively compile down to BPF bytecode), rather reinventing all that and shoving a SAT solver to discharge more complex verification queries from the kernel verifier. It may be so that not everyone needs such strong guarantees (esp. during development). Optionality and clear distinction of responsibilities seems like a more sustainable long term approach for the ecosystem.

> If normal C is easy to write, it's unsafe.

I think the way to address this is to use a better language than C to write kernel extensions, and layer memory safety for extension's own state on top of the guarantees BPF already ensures for the kernel resources and interfaces. Eventually the Rust frontend for BPF should become a thing (it's WIP), which would hopefully be an improvement. You could argue Rust is not perfect and still leaves much to be desired, but it has primitives that allow you to negate presence of memory safety issues (as long as unsafe code is not violating that property) in a decidable fashion for BPF program's arena accesses. The kernel verifier already presents a restricted interface into the kernel and makes sure things are correct when accessing kernel objects.

> Instead of making BPF look more like userspace, I'd make it easier to integrate BPF *with* userspace,

This is how it works already. Several use cases you pointed out (schedulers in particular) already have much of their logic in user space.

Here's your scheduled comparison with WASM

Posted Jun 24, 2026 20:21 UTC (Wed) by Cyberax (✭ supporter ✭, #52523) [Link]

Here's your regular scheduled comparison with WASM and yet another reminder of the perils of the NIH syndrome.

In WASM these kinds of arenas are called "memories": https://github.com/WebAssembly/spec/blob/wasm-3.0/proposa...

And they have similar limitations, it's not possible to put opaque object references into them. They have to stay in WASM tables.

BPF Arena Docs

Posted Jun 25, 2026 12:10 UTC (Thu) by RazeLighter777 (subscriber, #130021) [Link]

I really wish the BPF arenas were better documented... the official docs for https://docs.ebpf.io/linux/map-type/BPF_MAP_TYPE_ARENA/ is just a placeholder, despite being added in 6.9

It's difficult to find out the actual intentions by reading the source code, particularly with BPF, one of the most complex and actively developed kernel subsystems.


Copyright © 2026, Eklektix, Inc.
This article may be redistributed under the terms of the Creative Commons CC BY-SA 4.0 license
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds