Skip to content

bpf: Never allocate ct_buffers on the stack - #45589

Merged
pchaigno merged 1 commit into
mainfrom
pr/pchaigno/dont-alloc-ct_buffers_on_stack
Apr 24, 2026
Merged

bpf: Never allocate ct_buffers on the stack#45589
pchaigno merged 1 commit into
mainfrom
pr/pchaigno/dont-alloc-ct_buffers_on_stack

Conversation

@pchaigno

@pchaigno pchaigno commented Apr 24, 2026

Copy link
Copy Markdown
Member

The ct_buffer object is used in a BPF map to pass lots of information across tail calls after the conntrack lookup is done. As a result, it's a huge object of 96 bytes (measured on the stack). We should never allocate it on the stack or we'll quickly run out of space.

There's also no need to allocate it on the stack because it's already in a BPF array map. So we can simply lookup the single-entry from the map and populate that instead of doing a map update from a stack object.

Results for stack sizes

image

Side effect on complexity

image

Thankfully, the impact on complexity is rather positive. It even gives us a good reduction for our largest BPF program, going from 717582 instructions to 633288.


Fixes: #23831.

The ct_buffer object is used in a BPF map to pass lots of information
across tail calls after the ct lookup is done. As a result, it's a huge
object of 96 bytes (measured on the stack). We should never allocate it
on the stack or we'll quickly run out of space.

There's also no need to allocate it on the stack because it's already in
a BPF array map. So we can simply lookup the single-entry from the map
and populate that instead of doing a map update from a stack object.

This change reduces the stack sizes of tail_handle_ipv{4,6}_from_netdev
from 296 and 352 bytes to 232 and 256 bytes respectively.

Fixes: 1085ae2 ("bpf: Split handle_ipv6 in bpf_host.c after ct_lookup6")
Fixes: 3f356b0 ("bpf: Split handle_ipv4 in bpf_host.c after ct_lookup4")
Signed-off-by: Paul Chaignon <[email protected]>
@pchaigno pchaigno added area/datapath Impacts bpf/ or low-level forwarding details, including map management and monitor messages. kind/cleanup This includes no functional changes. release-note/misc This PR makes changes that have no direct user impact. labels Apr 24, 2026
@pchaigno

Copy link
Copy Markdown
Member Author

/test

@pchaigno
pchaigno marked this pull request as ready for review April 24, 2026 08:41
@pchaigno
pchaigno requested a review from a team as a code owner April 24, 2026 08:41
@pchaigno
pchaigno requested a review from jrife April 24, 2026 08:41
@pchaigno
pchaigno enabled auto-merge April 24, 2026 08:42

@jrife jrife left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice improvement, thanks. I was a little concerned about something maybe not getting zeroed when it should be but it looks like for all paths where cilium_tail_call_buffer* is read it has a preceding lookup for that packet.

Something similar should be possible in bpf_lxc, no?

@pchaigno
pchaigno added this pull request to the merge queue Apr 24, 2026
@maintainer-s-little-helper maintainer-s-little-helper Bot added the ready-to-merge This PR has passed all tests and received consensus from code owners to merge. label Apr 24, 2026
@pchaigno

Copy link
Copy Markdown
Member Author

Something similar should be possible in bpf_lxc, no?

Oh, I thought bpf_lxc didn't have this issue. Ok, I'll send a followup PR for that.

Merged via the queue into main with commit 524a591 Apr 24, 2026
564 of 587 checks passed
@pchaigno
pchaigno deleted the pr/pchaigno/dont-alloc-ct_buffers_on_stack branch April 24, 2026 15:02
@julianwiedmann julianwiedmann added the backport-pending/1.19 The backport for Cilium 1.19.x for this PR is in progress. label Aug 6, 2026
@github-actions github-actions Bot added backport-done/1.19 The backport for Cilium 1.19.x for this PR is done. and removed backport-pending/1.19 The backport for Cilium 1.19.x for this PR is in progress. labels Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/datapath Impacts bpf/ or low-level forwarding details, including map management and monitor messages. backport-done/1.19 The backport for Cilium 1.19.x for this PR is done. kind/cleanup This includes no functional changes. ready-to-merge This PR has passed all tests and received consensus from code owners to merge. release-note/misc This PR makes changes that have no direct user impact.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants