Skip to content

Conversation

ti-mo
Copy link
Collaborator

@ti-mo ti-mo commented Nov 7, 2024

Pending the proposal in golang/go#70224, here's the current API proposal for returning Go pointers to 'off-heap' bpf map memory:

func MemoryPointer[T any](mm *Memory, off uint64) (*T, error)
func VariablePointer[T any](v *Variable) (*T, error)

We need a bit of help from the Go runtime to manage the lifecycle of the underlying memory mapping. Currently, this mmaps over a piece of Go heap to allow the GC to track pointers into the bpf map memory, obviating the need for tying the mmap's lifetime to an ebpf.Memory. Instead, any Go pointers into the mmap will keep it alive, removing the risk of use-after-free.

Clearly, this involves some dark arts, so we're not comfortable pushing this on users since Collection.Variables is populated by default. Any change in the runtime or allocator may break this, and the fallout means Go programs segfaulting without any hint as to why, which is not acceptable.

--- Edit ---

Since the runtime.AddManualMemoryCleanup proposal may take a while to implement, land and ship in a Go version we target, I've revived the patch set and put the heap-mapping behaviour behind CollectionOptions.UnsafeVariableExperiment. MemoryPointer is yet unexported, since Map.Memory() is typically called directly by the user and we'd need to store the feature flag in *Map, which doesn't feel ideal.

Alternatively, we could go for an env feature flag instead of CollectionOptions (EBPFUNSAFEVARIABLES=true?), which would allow us to export MemoryPointer and transparantly switch between implementations.

@ti-mo ti-mo mentioned this pull request Feb 18, 2025
@ti-mo ti-mo changed the title [WIP] variable: return native Go pointers to underlying bpf map values variable: return native Go pointers to underlying bpf map values Feb 25, 2025
@ti-mo ti-mo changed the title variable: return native Go pointers to underlying bpf map values variable: return native Go pointers to bpf map memory Feb 27, 2025
@ti-mo ti-mo force-pushed the tb/atomic-memory branch 3 times, most recently from 2d43b45 to 821aa80 Compare February 27, 2025 15:13
@ti-mo ti-mo marked this pull request as ready for review February 27, 2025 15:48
@ti-mo ti-mo requested a review from a team as a code owner February 27, 2025 15:48
Copy link
Collaborator

@lmb lmb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can either of you refresh my mind again what the killer feature for this is? Seems like @mejedi is interested in using this for slices?

@ti-mo ti-mo force-pushed the tb/atomic-memory branch from 821aa80 to fe8e5b1 Compare March 13, 2025 11:36
@ti-mo
Copy link
Collaborator Author

ti-mo commented Mar 13, 2025

Can either of you refresh my mind again what the killer feature for this is? Seems like @mejedi is interested in using this for slices?

I think the most pressing use case is atomics, which is currently not supported at all. Array variables currently work through Get() and Set(), but there's always a marshaling cost. Also, simply having the convenience of manipulating BPF variables like they are Go variables is nice.

@ti-mo ti-mo requested a review from lmb March 13, 2025 11:43
@ti-mo ti-mo force-pushed the tb/atomic-memory branch from fe8e5b1 to 7b7052b Compare March 27, 2025 14:30
Copy link
Collaborator

@lmb lmb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding all those tests, they look excellent to me! Left one thought around how to enable / disable this, feel free to deal with that as you wish. I have concerns around what types should be allowed, where I'd appreciate it if we can be even more conservative for a start.

@ti-mo ti-mo force-pushed the tb/atomic-memory branch 3 times, most recently from 17713b9 to 534173e Compare April 23, 2025 10:10
@ti-mo ti-mo force-pushed the tb/atomic-memory branch 3 times, most recently from e31d8af to 3869345 Compare May 5, 2025 12:06
ti-mo added 2 commits May 5, 2025 14:13
Signed-off-by: Timo Beckers <timo@isovalent.com>
Signed-off-by: Timo Beckers <timo@isovalent.com>
@ti-mo ti-mo force-pushed the tb/atomic-memory branch from 3869345 to 34c1aa1 Compare May 5, 2025 12:14
@ti-mo ti-mo merged commit 61d86ca into cilium:main May 5, 2025
30 of 31 checks passed
@ti-mo ti-mo deleted the tb/atomic-memory branch May 5, 2025 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants