[PATCH net-next v1 6/6] tcp: do not call bpf_skops_write_hdr_opt() unless needed

From: Xuanqiang Luo

Date: Tue Nov 24 2026 - 08:09:04 EST


From: Xuanqiang Luo <luoxuanqiang@xxxxxxxxxx>

__tcp_transmit_skb() calls bpf_skops_write_hdr_opt() for every skb. The
function returns at once if no BPF program reserved room for TCP options
(opts->bpf_opt_len == 0), which is the common case.

It has a struct bpf_sock_ops_kern on its stack. With
CONFIG_STACKPROTECTOR_STRONG=y, its stack canary is set up and checked
even when it has nothing to do.

Test opts.bpf_opt_len in its callers instead.

$ scripts/bloat-o-meter -t vmlinux.old vmlinux.new
add/remove: 0/0 grow/shrink: 2/2 up/down: 28/-196 (-168)
Function old new delta
tcp_make_synack 2006 2020 +14
__tcp_transmit_skb 4114 4128 +14
tcp_options_write.isra 1699 1651 -48
bpf_skops_write_hdr_opt.isra 1206 1058 -148
Total: Before=24067405, After=24067237, chg -0.00%

Signed-off-by: Xuanqiang Luo <luoxuanqiang@xxxxxxxxxx>
---
net/ipv4/tcp_output.c | 11 +++++------
1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c
index c2022fc9ad52..3c0be5a675ea 100644
--- a/net/ipv4/tcp_output.c
+++ b/net/ipv4/tcp_output.c
@@ -538,9 +538,6 @@ static void bpf_skops_write_hdr_opt(struct sock *sk, struct sk_buff *skb,
{
u8 first_opt_off, nr_written = 0, max_opt_len = opts->bpf_opt_len;

- if (likely(!max_opt_len))
- return;
-
first_opt_off = tcp_hdrlen(skb) - max_opt_len;

if (cgroup_bpf_enabled(CGROUP_SOCK_OPS) &&
@@ -1731,7 +1728,8 @@ static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb,
}

/* BPF prog is the last one writing header option */
- bpf_skops_write_hdr_opt(sk, skb, NULL, NULL, 0, &opts);
+ if (unlikely(opts.bpf_opt_len))
+ bpf_skops_write_hdr_opt(sk, skb, NULL, NULL, 0, &opts);

#if IS_ENABLED(CONFIG_IPV6)
if (likely(icsk->icsk_af_ops->net_header_len == sizeof(struct ipv6hdr)))
@@ -4146,8 +4144,9 @@ struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
rcu_read_unlock();
#endif

- bpf_skops_write_hdr_opt((struct sock *)sk, skb, req, syn_skb,
- synack_type, &opts);
+ if (unlikely(opts.bpf_opt_len))
+ bpf_skops_write_hdr_opt((struct sock *)sk, skb, req, syn_skb,
+ synack_type, &opts);

skb_set_delivery_time(skb, now, SKB_CLOCK_MONOTONIC);
tcp_add_tx_delay(skb, tp);
--
2.43.0