* [PATCH net-next V2 RESEND] tcp: shrink per-packet memset in __tcp_transmit_skb()
@ 2026-03-04 11:15 Keita Morisaki
2026-03-04 18:09 ` Matthieu Baerts
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Keita Morisaki @ 2026-03-04 11:15 UTC (permalink / raw)
To: Eric Dumazet, Neal Cardwell, David S . Miller, David Ahern,
Jakub Kicinski, Paolo Abeni
Cc: Kuniyuki Iwashima, Simon Horman, Jakub Sitnicki, Matthieu Baerts,
Mat Martineau, Geliang Tang, netdev, linux-kernel, bpf, mptcp,
Keita Morisaki
Use struct_group() to group the three fields in tcp_out_options that are
read unconditionally by tcp_options_write() and bpf_skops_write_hdr_opt()
(mss, bpf_opt_len, num_sack_blocks), then replace the full-struct memset
with a targeted memset of only that group.
struct tcp_out_options is 40 bytes without MPTCP and 96 bytes with
CONFIG_MPTCP=y (typical distro config). Every remaining field is either
assigned before first use by tcp_established_options()/tcp_syn_options(),
or gated behind its OPTION_* flag in tcp_options_write(). This memset
runs on every transmitted TCP packet, so shrinking it from 96 (or 40)
bytes to 4 bytes reduces per-packet overhead on the hot path.
Assembly comparison (x86-64, GCC 13, CONFIG_MPTCP=y):
Before: rep stos zeroing 96 bytes (5 instructions, 12 8-byte stores)
After: movl $0x0 zeroing 4 bytes (1 instruction, 1 store)
Also add opts->options = 0 at the top of tcp_syn_options(), which
already used |= without a prior clear. tcp_established_options() already
clears opts->options at its top.
Reviewed-by: Jakub Sitnicki <jakub@cloudflare.com>
Signed-off-by: Keita Morisaki <kmta1236@gmail.com>
---
net/ipv4/tcp_output.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c
index 326b58ff1118d..63ee037f46e50 100644
--- a/net/ipv4/tcp_output.c
+++ b/net/ipv4/tcp_output.c
@@ -429,14 +429,19 @@ static void smc_options_write(__be32 *ptr, u16 *options)
}
struct tcp_out_options {
+ /* Following group is cleared in __tcp_transmit_skb() */
+ struct_group(cleared,
+ u16 mss; /* 0 to disable */
+ u8 bpf_opt_len; /* length of BPF hdr option */
+ u8 num_sack_blocks; /* number of SACK blocks to include */
+ );
+
+ /* Caution: following fields are not cleared in __tcp_transmit_skb() */
u16 options; /* bit field of OPTION_* */
- u16 mss; /* 0 to disable */
u8 ws; /* window scale, 0 to disable */
- u8 num_sack_blocks; /* number of SACK blocks to include */
u8 num_accecn_fields:7, /* number of AccECN fields needed */
use_synack_ecn_bytes:1; /* Use synack_ecn_bytes or not */
u8 hash_size; /* bytes in hash_location */
- u8 bpf_opt_len; /* length of BPF hdr option */
__u8 *hash_location; /* temporary pointer, overloaded */
__u32 tsval, tsecr; /* need to include OPTION_TS */
struct tcp_fastopen_cookie *fastopen_cookie; /* Fast open cookie */
@@ -965,6 +970,8 @@ static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb,
struct tcp_fastopen_request *fastopen = tp->fastopen_req;
bool timestamps;
+ opts->options = 0;
+
/* Better than switch (key.type) as it has static branches */
if (tcp_key_is_md5(key)) {
timestamps = false;
@@ -1549,7 +1556,7 @@ static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb,
inet = inet_sk(sk);
tcb = TCP_SKB_CB(skb);
- memset(&opts, 0, sizeof(opts));
+ memset(&opts.cleared, 0, sizeof(opts.cleared));
tcp_get_current_key(sk, &key);
if (unlikely(tcb->tcp_flags & TCPHDR_SYN)) {
base-commit: af4e9ef3d78420feb8fe58cd9a1ab80c501b3c08
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH net-next V2 RESEND] tcp: shrink per-packet memset in __tcp_transmit_skb()
2026-03-04 11:15 [PATCH net-next V2 RESEND] tcp: shrink per-packet memset in __tcp_transmit_skb() Keita Morisaki
@ 2026-03-04 18:09 ` Matthieu Baerts
2026-03-04 18:15 ` Eric Dumazet
2026-03-05 4:41 ` Kuniyuki Iwashima
2026-03-06 23:25 ` Jakub Kicinski
2 siblings, 1 reply; 5+ messages in thread
From: Matthieu Baerts @ 2026-03-04 18:09 UTC (permalink / raw)
To: Keita Morisaki
Cc: Kuniyuki Iwashima, Simon Horman, Jakub Sitnicki, Mat Martineau,
Geliang Tang, netdev, linux-kernel, bpf, mptcp, Eric Dumazet,
Neal Cardwell, David S . Miller, David Ahern, Jakub Kicinski,
Paolo Abeni
Hi Keita,
On 04/03/2026 12:15, Keita Morisaki wrote:
> Use struct_group() to group the three fields in tcp_out_options that are
> read unconditionally by tcp_options_write() and bpf_skops_write_hdr_opt()
> (mss, bpf_opt_len, num_sack_blocks), then replace the full-struct memset
> with a targeted memset of only that group.
>
> struct tcp_out_options is 40 bytes without MPTCP and 96 bytes with
> CONFIG_MPTCP=y (typical distro config). Every remaining field is either
> assigned before first use by tcp_established_options()/tcp_syn_options(),
> or gated behind its OPTION_* flag in tcp_options_write(). This memset
> runs on every transmitted TCP packet, so shrinking it from 96 (or 40)
> bytes to 4 bytes reduces per-packet overhead on the hot path.
>
> Assembly comparison (x86-64, GCC 13, CONFIG_MPTCP=y):
>
> Before: rep stos zeroing 96 bytes (5 instructions, 12 8-byte stores)
> After: movl $0x0 zeroing 4 bytes (1 instruction, 1 store)
>
> Also add opts->options = 0 at the top of tcp_syn_options(), which
> already used |= without a prior clear. tcp_established_options() already
> clears opts->options at its top.
Thank you for this patch! It looks good to me:
Acked-by: Matthieu Baerts (NGI0) <matttbe@kernel.org>
Cheers,
Matt
--
Sponsored by the NGI0 Core fund.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH net-next V2 RESEND] tcp: shrink per-packet memset in __tcp_transmit_skb()
2026-03-04 18:09 ` Matthieu Baerts
@ 2026-03-04 18:15 ` Eric Dumazet
0 siblings, 0 replies; 5+ messages in thread
From: Eric Dumazet @ 2026-03-04 18:15 UTC (permalink / raw)
To: Matthieu Baerts
Cc: Keita Morisaki, Kuniyuki Iwashima, Simon Horman, Jakub Sitnicki,
Mat Martineau, Geliang Tang, netdev, linux-kernel, bpf, mptcp,
Neal Cardwell, David S . Miller, David Ahern, Jakub Kicinski,
Paolo Abeni
On Wed, Mar 4, 2026 at 7:09 PM Matthieu Baerts <matttbe@kernel.org> wrote:
>
> Hi Keita,
>
> On 04/03/2026 12:15, Keita Morisaki wrote:
> > Use struct_group() to group the three fields in tcp_out_options that are
> > read unconditionally by tcp_options_write() and bpf_skops_write_hdr_opt()
> > (mss, bpf_opt_len, num_sack_blocks), then replace the full-struct memset
> > with a targeted memset of only that group.
> >
> > struct tcp_out_options is 40 bytes without MPTCP and 96 bytes with
> > CONFIG_MPTCP=y (typical distro config). Every remaining field is either
> > assigned before first use by tcp_established_options()/tcp_syn_options(),
> > or gated behind its OPTION_* flag in tcp_options_write(). This memset
> > runs on every transmitted TCP packet, so shrinking it from 96 (or 40)
> > bytes to 4 bytes reduces per-packet overhead on the hot path.
> >
> > Assembly comparison (x86-64, GCC 13, CONFIG_MPTCP=y):
> >
> > Before: rep stos zeroing 96 bytes (5 instructions, 12 8-byte stores)
> > After: movl $0x0 zeroing 4 bytes (1 instruction, 1 store)
> >
> > Also add opts->options = 0 at the top of tcp_syn_options(), which
> > already used |= without a prior clear. tcp_established_options() already
> > clears opts->options at its top.
>
> Thank you for this patch! It looks good to me:
>
> Acked-by: Matthieu Baerts (NGI0) <matttbe@kernel.org>
Apparently my Reviewed-by: tag was lost.
Reviewed-by: Eric Dumazet <edumazet@google.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH net-next V2 RESEND] tcp: shrink per-packet memset in __tcp_transmit_skb()
2026-03-04 11:15 [PATCH net-next V2 RESEND] tcp: shrink per-packet memset in __tcp_transmit_skb() Keita Morisaki
2026-03-04 18:09 ` Matthieu Baerts
@ 2026-03-05 4:41 ` Kuniyuki Iwashima
2026-03-06 23:25 ` Jakub Kicinski
2 siblings, 0 replies; 5+ messages in thread
From: Kuniyuki Iwashima @ 2026-03-05 4:41 UTC (permalink / raw)
To: Keita Morisaki
Cc: Eric Dumazet, Neal Cardwell, David S . Miller, David Ahern,
Jakub Kicinski, Paolo Abeni, Simon Horman, Jakub Sitnicki,
Matthieu Baerts, Mat Martineau, Geliang Tang, netdev,
linux-kernel, bpf, mptcp
On Wed, Mar 4, 2026 at 3:15 AM Keita Morisaki <kmta1236@gmail.com> wrote:
>
> Use struct_group() to group the three fields in tcp_out_options that are
> read unconditionally by tcp_options_write() and bpf_skops_write_hdr_opt()
> (mss, bpf_opt_len, num_sack_blocks), then replace the full-struct memset
> with a targeted memset of only that group.
>
> struct tcp_out_options is 40 bytes without MPTCP and 96 bytes with
> CONFIG_MPTCP=y (typical distro config). Every remaining field is either
> assigned before first use by tcp_established_options()/tcp_syn_options(),
> or gated behind its OPTION_* flag in tcp_options_write(). This memset
> runs on every transmitted TCP packet, so shrinking it from 96 (or 40)
> bytes to 4 bytes reduces per-packet overhead on the hot path.
>
> Assembly comparison (x86-64, GCC 13, CONFIG_MPTCP=y):
>
> Before: rep stos zeroing 96 bytes (5 instructions, 12 8-byte stores)
> After: movl $0x0 zeroing 4 bytes (1 instruction, 1 store)
>
> Also add opts->options = 0 at the top of tcp_syn_options(), which
> already used |= without a prior clear. tcp_established_options() already
> clears opts->options at its top.
>
> Reviewed-by: Jakub Sitnicki <jakub@cloudflare.com>
> Signed-off-by: Keita Morisaki <kmta1236@gmail.com>
Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH net-next V2 RESEND] tcp: shrink per-packet memset in __tcp_transmit_skb()
2026-03-04 11:15 [PATCH net-next V2 RESEND] tcp: shrink per-packet memset in __tcp_transmit_skb() Keita Morisaki
2026-03-04 18:09 ` Matthieu Baerts
2026-03-05 4:41 ` Kuniyuki Iwashima
@ 2026-03-06 23:25 ` Jakub Kicinski
2 siblings, 0 replies; 5+ messages in thread
From: Jakub Kicinski @ 2026-03-06 23:25 UTC (permalink / raw)
To: Keita Morisaki
Cc: Eric Dumazet, Neal Cardwell, David S . Miller, David Ahern,
Paolo Abeni, Kuniyuki Iwashima, Simon Horman, Jakub Sitnicki,
Matthieu Baerts, Mat Martineau, Geliang Tang, netdev,
linux-kernel, bpf, mptcp
On Wed, 4 Mar 2026 20:15:17 +0900 Keita Morisaki wrote:
> Use struct_group() to group the three fields in tcp_out_options that are
> read unconditionally by tcp_options_write() and bpf_skops_write_hdr_opt()
> (mss, bpf_opt_len, num_sack_blocks), then replace the full-struct memset
> with a targeted memset of only that group.
>
> struct tcp_out_options is 40 bytes without MPTCP and 96 bytes with
> CONFIG_MPTCP=y (typical distro config). Every remaining field is either
> assigned before first use by tcp_established_options()/tcp_syn_options(),
> or gated behind its OPTION_* flag in tcp_options_write(). This memset
> runs on every transmitted TCP packet, so shrinking it from 96 (or 40)
> bytes to 4 bytes reduces per-packet overhead on the hot path.
>
> Assembly comparison (x86-64, GCC 13, CONFIG_MPTCP=y):
>
> Before: rep stos zeroing 96 bytes (5 instructions, 12 8-byte stores)
> After: movl $0x0 zeroing 4 bytes (1 instruction, 1 store)
>
> Also add opts->options = 0 at the top of tcp_syn_options(), which
> already used |= without a prior clear. tcp_established_options() already
> clears opts->options at its top.
>
> Reviewed-by: Jakub Sitnicki <jakub@cloudflare.com>
> Signed-off-by: Keita Morisaki <kmta1236@gmail.com>
Applied (cfcceb7a39f), thanks!
--
pw-bot: accept
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-03-06 23:25 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-04 11:15 [PATCH net-next V2 RESEND] tcp: shrink per-packet memset in __tcp_transmit_skb() Keita Morisaki
2026-03-04 18:09 ` Matthieu Baerts
2026-03-04 18:15 ` Eric Dumazet
2026-03-05 4:41 ` Kuniyuki Iwashima
2026-03-06 23:25 ` Jakub Kicinski
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox