Skip to content

Commit

Permalink
Add BPF map reuse function
Browse files Browse the repository at this point in the history
Wrap `bpf_map__reuse_fd` to reuse a BPF map accross BPF programs. This
is useful in complex programs that long chains of calls, for example,
via `bpf_tail_call` that may be loaded in different programs and share
state via reused maps.

Test Plan
=========

Our global state map, called `heap` shared by multiple programs

```
[javierhonduco@fedora parca-agent]$ sudo bpftool map  | grep heap
3403: percpu_array  name heap  flags 0x0
```

```
2812: perf_event  name walk_user_stacktrace_impl  tag 25cb7e352f197144  gpl
        loaded_at 2023-08-01T16:06:01+0100  uid 0
        xlated 7984B  jited 4827B  memlock 12288B  map_ids 3399,3411,3401,3407,3400,3408,3409,3402,3406,3405,3403
        btf_id 1993
        pids parca-agent(1049424)
        metadata:
                name = "parca-agent (https://github.com/parca-dev/parca-agent)"
2814: perf_event  name profile_cpu  tag 3f2053ff90208ef1  gpl
        loaded_at 2023-08-01T16:06:02+0100  uid 0
        xlated 4368B  jited 2850B  memlock 8192B  map_ids 3411,3404,3399,3401,3408,3409,3405,3407,3403,3402
        btf_id 1993
        pids parca-agent(1049424)
        metadata:
                name = "parca-agent (https://github.com/parca-dev/parca-agent)"
2816: perf_event  name walk_ruby_stack  tag 97bb3ee23c47e22d  gpl
        loaded_at 2023-08-01T16:06:02+0100  uid 0
        xlated 87600B  jited 56572B  memlock 98304B  map_ids 3413,3414,3419,3415,3399,3403,3420,3417,3418
        btf_id 1994
        pids parca-agent(1049424)
```

Map reuse will soon become a load loading functionality for Parca Agent.
We are thoroughly testing it, across multiple kernels and with ASAN
enabled, too.

If there were any problems in the future we will quickly notice them and
will be happy to submit bugfixes.

Signed-off-by: Francisco Javier Honduvilla Coto <[email protected]>
  • Loading branch information
javierhonduco committed Aug 1, 2023
1 parent d217980 commit 7ea3e99
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions libbpfgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,14 @@ func (b *BPFMap) Type() MapType {
return MapType(C.bpf_map__type(b.bpfMap))
}

func (b *BPFMap) MapReuseFd(fd int) error {
errC := C.bpf_map__reuse_fd(b.bpfMap, C.int(fd))
if errC != 0 {
return fmt.Errorf("could not reuse bpf map: %w", syscall.Errno(-errC))
}
return nil
}

// SetType is used to set the type of a bpf map that isn't associated
// with a file descriptor already. If the map is already associated
// with a file descriptor the libbpf API will return error code EBUSY
Expand Down

0 comments on commit 7ea3e99

Please sign in to comment.