Full name: Berkeley Packet Filter map.
Definition
A BPF map is a data structure that the host of eBPF §2.1 (extended Berkeley Packet Filter) programs creates and owns, and that a program reads and writes only through operations the host provides. Most maps hold key-value pairs: a key, a fixed-size byte string naming an entry, and a value, a fixed-size byte string stored under it. A map outlives each run of a program, so it keeps state between runs and passes data between programs and the software that manages them. Linux defines the best-known map types.
The idea
Each run of an eBPF program starts with fresh registers and a fresh stack, so nothing it computes survives the run. A program that limits each client to 100 messages per second must remember every client’s count between messages, and an administrator wants to read the counts and change the limit without reloading the program.
A raw pointer to some global memory would solve the first problem and destroy safety: the program could write wherever the pointer leads, and the host could never free that memory. A map keeps the host in control. The host fixes the size of every key and value when it creates the map, so a checker knows exactly how many bytes the program may touch through a value; the host decides when the map is freed; and every access goes through an operation the host defines.
How it works
In Linux a map is used as follows.
Create. A user-space program, one running outside the kernel, asks the kernel through the
bpfsystem call, the kernel’s entry point for eBPF requests, to create a map of a given type, key size, value size and maximum number of entries. The kernel returns a file descriptor §15.21, a small number by which the process refers to the map.Refer. An eBPF program names the map by that file descriptor in a wide immediate instruction §2.10, the 16-byte instruction that loads a 64-bit value, with a marker in its source register field saying that the value is a map’s file descriptor. While loading the program, the kernel replaces the number with the address of the map.
Look up. The program calls the helper function §2.19
bpf_map_lookup_elemwith the map and the address of a key. The helper returns the address of the value stored in the map, not a copy, or zero if no entry has that key.Check. The eBPF verifier §3.3, the kernel’s load-time checker, rejects a program that uses the result before comparing it with zero, and afterwards allows accesses only at offsets 0 to the value size minus 1.
Update and delete.
bpf_map_update_elemstores a value under a key; its flagBPF_ANYcreates or replaces,BPF_NOEXISTonly creates, andBPF_EXISTonly replaces.bpf_map_delete_elemremoves an entry.Share. User-space programs perform the same operations through the system call, which copies keys and values in and out; they never receive an address inside the kernel.
Free. The kernel counts references to a map from file descriptors, from loaded programs that use it, and from a name pinned in the BPF file system, and frees the map when the last reference goes.
Common types are the hash map, a hash table §14.7 with keys of any fixed size; the array §14.5, whose keys are the integers 0 to the maximum minus 1 and whose entries all exist from creation, filled with zero bytes, and are never deleted; and per-CPU variants of both, which keep a separate value for each processor so that processors never share a value.
Worked example
Sizing. A rate limiter §9.38 uses a hash map with 4-byte client numbers as keys, 8-byte counters as values, and at most 1,024 entries. Keys and values alone take bytes, and the kernel adds bookkeeping for each entry. A lookup returns the address of an 8-byte value, so the verifier accepts accesses that stay within offsets 0 to 7 from it and rejects any access that touches offset 8.
A lost update. The counter for client 5 holds 3. Two messages from client 5 arrive together and are handled on processors A and B. A reads 3; B reads 3; A writes ; B writes . Two messages arrived, so the count should be , but it is 4. Every address was valid, so memory safety held. What failed is that reading, adding and writing are three steps and the other processor ran between them: a race condition §9.2, an error whose result depends on how concurrent steps interleave. An atomic operation §9.8, one that completes as a single indivisible step, fixes it: eBPF has an atomic add instruction, which a C program reaches through __sync_fetch_and_add. With it, A makes the count 4 and B makes it 5. A value with several fields that must change together can instead hold a spinlock §9.7, a lock that waits by looping, taken with bpf_spin_lock.
A stale copy. A user-space tool copies the counter of client 5 when it holds 3, and the program then raises it to 4. The copy still says 3: safe to keep, since no one can free it from under the tool, but no longer true of the map.
Stated precisely
Let a Linux map have key size and value size bytes. Precondition of a lookup from a program: the first argument refers to a map the program was loaded with, and the second points to bytes the program may read. Result: zero if the map holds no entry for those key bytes; otherwise an address such that the bytes to hold the entry’s value. The program may read and write those bytes until its current run ends. The kernel does not free the memory behind during that run, even if the entry is deleted meanwhile; for hash-map entries that are freed on deletion, it delays the freeing with read-copy update §9.20 (RCU), a scheme that waits until every running reader has finished. Not guaranteed: that the bytes keep their values, since other processors and user space may change them at any time, or that the entry still exists.
A pointer cannot outlive the run. The verifier either rejects storing the address into a map or lets it be stored only as a plain number, and it never allows a load or store through a number read back from a map. Recent kernels add special typed pointer fields to map values, managed by dedicated helpers; ordinary values have no such fields.
Limits and common mistakes
Map rules belong to a runtime. A key-value table in another eBPF runtime may return copies instead of addresses, may lack deletion, or may run only one program at a time; read the interface of the runtime in use.
Memory safety is not consistency. The verifier keeps every access inside the value; it prevents neither a lost update nor a decision based on a value that another processor is changing.
On a hash map, the update helper replaces an entry as a whole, so a reader sees the old value or the new one; writing through a looked-up address changes the value in place, field by field.
A map is partial: a key can have no entry. A program must handle the zero result, for example by inserting a starting value with BPF_NOEXIST. Treating a missing entry as a zero value without creating it invents state the map does not hold.
A full hash map rejects new keys. An attacker who can create keys, for example by sending from many addresses, can fill the map and lock out legitimate clients, so the maximum size and the reaction to a full map are design decisions.
Questions answered
Can a program keep pointers into mutable map values?
A program can keep a pointer into a map value for the rest of its current run, but not from one run to the next. In Linux, the lookup helper bpf_map_lookup_elem returns the address of the stored value itself, or zero if the key has no entry. After comparing the address with zero, the program may read and write the value through it until the run ends; the eBPF verifier, the kernel’s load-time checker, enforces both the comparison and the value’s bounds. The kernel keeps the memory valid that long, even if the entry is deleted meanwhile, but other processors and user space can change the bytes at any moment, so a read-modify-write sequence needs an atomic instruction or a spinlock stored in the value, or updates can be lost.
The verifier never treats a number read back from a map as an address, so a pointer cannot be carried into a later run, and each run must look the entry up again. Other runtimes may differ: an interface that returns copies removes the lifetime question but delivers a snapshot that can be out of date. For any runtime the answer comes from its contract: whether a lookup returns an address or a copy, how long an address stays valid, and what may change the value meanwhile.
Related entries: Temporal memory safety (§2.29), Guest persistent state (§2.36), Atomic operation (§9.8), Race condition (§9.2), Read-copy update (RCU) (§9.20), Object lifetime (§4.1), Hash table (§14.7), Helper function (§2.19), Copy-only helper (§2.22), eBPF verifier (§3.3). Further reading: [2].