Re: [PATCH net-next v1 0/6] net: avoid stack canaries in TCP/IPv4 fast paths
From: Xuanqiang Luo
Date: Fri Nov 27 2026 - 01:18:32 EST
On 2026/11/26 at 17:48 gang.yan@xxxxxxxxx wrote:
November 24, 2026 at 9:06 PM, "Xuanqiang Luo" <xuanqiang.luo@xxxxxxxxx mailto:xuanqiang.luo@xxxxxxxxx?to=%22Xuanqiang%20Luo%22%20%3Cxuanqiang.luo%40linux.dev%3E > wrote:Hi Gang,
From: Xuanqiang Luo <luoxuanqiang@xxxxxxxxxx>
With CONFIG_STACKPROTECTOR_STRONG=y, a function that has a local array or
passes the address of one of its locals to another function needs a stack
canary. The canary is set up and checked on every call, also when the
local is only used on a path that almost never runs.
This series removes the canary from functions of the TCP/IPv4 fast paths,
by returning results by value, by using storage that is not on the stack,
or by not calling a function that has nothing to do.
Patch 1 returns the two mptcp_space() values in a structure, so that
__tcp_select_window() no longer needs a canary.
Patch 2 changes ip_neigh_gw4() to take a pointer to the address, like
ip_neigh_gw6(), so that ip_finish_output2() no longer needs a canary.
Patch 3 uses pskb_may_pull() in icmp_filter(), so that
raw_local_deliver() no longer needs a canary.
Patch 4 turns skb_try_coalesce() into an inline wrapper over a function
returning its results by value, so that its callers no longer need a
canary because of it.
Patch 5 returns the result of the TCP receive coalescing by value, so
that tcp_rcv_established() and tcp_data_queue() no longer need a canary.
Patch 6 calls bpf_skops_write_hdr_opt(), which needs a canary, only if a
BPF program reserved room for TCP options.
The bloat-o-meter numbers are from gcc 16.2 on x86_64. The functions
named in patches 1 to 5 also lose their canary with clang 23.1.6 on
x86_64, with and without ThinLTO, and on arm64.
Hi Xuanqiang,
Thanks for this series. Besides the code size, do you have any runtime perf
numbers (TCP_RR, cycles per packet...)? I think it may help the maintainers
judge the real gain.
I omitted runtime numbers from the cover letter because earlier cross-NIC
TCP_RR runs were within boot-to-boot noise, and I did not want to overstate
the gain. I re-ran the full 6-patch series against its base commit
(a4c450594f5d) in a tighter setup on an x86_64 server (Hygon C86-4G,
ConnectX-5 10GbE mlx5, GCC 12.3, CONFIG_STACKPROTECTOR_STRONG=y, identical
.config, nokaslr, boost off, isolated CPUs/IRQs, busy_read=0, 3 alternating
boots per kernel x 4 runs):
1. Single-CPU loopback 1-byte TCP_RR (both endpoints on 1 CPU running the
kernel under test, 99.9% busy with no cross-CPU lock contention; each
RR counts 2 TX + 2 RX = 4 Tcp InSegs+OutSegs):
- instructions:k / tx: 33,562.3 -> 33,410.6 (-151.8, -0.45%, CV 0.01%)
- instructions:k / seg: 8,372.8 -> 8,336.1 ( -36.8, -0.44%, CV 0.02%)
- branches:k / tx: 7,627.9 -> 7,604.4 ( -23.5, -0.31%, CV 0.03%)
- cycles:k / tx: 48,883.8 -> 48,613.8 (-270.0, -0.55%)
- throughput: 52,804 -> 53,068 tps (+0.50%)
2. Cross-host 10GbE mlx5 8-flow 1-byte TCP_RR (server CPU-bound on 1 app
CPU + 1 IRQ CPU, client pinned on a fixed peer kernel):
- instructions:k / pkt: 12,040.6 -> 11,946.0 ( -94.6, -0.79%, CV 0.43%)
- cycles:k / pkt: 19,652.2 -> 19,450.1 (-202.1, -1.03%, CV 0.52%)
- throughput: 79,455 -> 79,883 tps (+0.54%)
3. Loopback small-write TCP_STREAM (-m 512 -D, 2 CPUs):
- instructions:k / seg: 5,805.6 -> 5,713.7 ( -91.9, -1.58%)
- cycles:k / seg: 8,538.5 -> 8,416.8 (-121.7, -1.42%)
- throughput: 641.8 -> 650.5 Mbit/s (+1.37%)
(Note: in the 2-CPU / multi-flow cases above, perf shows part of the
larger per-packet drop comes from fewer spinlock-slowpath and idle-loop
samples in addition to the direct TCP path savings; test 1 isolates
the direct instruction/branch reduction.)
4. perf record (cycles:k and instructions:k) confirms
bpf_skops_write_hdr_opt() drops from 0.04-0.12% to 0.00% across all
profiled workloads, with consistent drops in tcp_rcv_established(),
__tcp_transmit_skb(), and __tcp_select_window().
One caveat to be transparent: on a loopback 1-byte UDP_RR control (which
only exercises patch 2's ip_finish_output2() canary removal),
instructions:k/tx changed by the expected -23.2 insns/tx (-0.07%) and
branches:k/tx was flat (+0.02%), yet wall-clock tps/cycles still shifted
by -0.80%/+0.68% due to fixed code-layout/IPC differences under nokaslr.
So the reliable runtime signal here is the deterministic drop in kernel
instructions and branches (~152 insns / ~23.5 branches per loopback TCP_RR)
and the disappearance of bpf_skops_write_hdr_opt() from the profile, while
wall-clock TCP_RR tps/cycles (~0.5%) remain in the same range as
code-alignment effects on this machine.
Thanks,
Xuanqiang