* [PATCH net-next v1] tcp: remove redundant memset in hot paths with CONFIG_INIT_STACK_ALL_ZERO
@ 2026-03-12 12:48 Jiayuan Chen
2026-03-12 12:53 ` Eric Dumazet
0 siblings, 1 reply; 3+ messages in thread
From: Jiayuan Chen @ 2026-03-12 12:48 UTC (permalink / raw)
To: netdev
Cc: Jiayuan Chen, Eric Dumazet, Neal Cardwell, Kuniyuki Iwashima,
David S. Miller, David Ahern, Jakub Kicinski, Paolo Abeni,
Simon Horman, linux-kernel
Modern compilers with CONFIG_INIT_STACK_ALL_ZERO enabled
(-ftrivial-auto-var-init=zero) automatically zero-initialize all stack
variables via `rep stos` in the function prologue. However, the compiler
cannot eliminate explicit memset() calls on those same variables,
resulting in redundant zeroing of the same memory region.
Replace explicit memset() with `= {0}` initializers for stack variables
in TCP hot path functions. With `= {0}`, the compiler recognizes a single
initialization and generates only one zeroing sequence, whereas a
separate memset() after declaration always produces a second `rep stos`.
scripts/bloat-o-meter -t ../vmlinux.old vmlinux
add/remove: 0/0 grow/shrink: 0/5 up/down: 0/-148 (-148)
Function old new delta
__tcp_transmit_skb 5256 5247 -9
tcp_v6_send_response 2629 2612 -17
tcp_make_synack 3106 3089 -17
tcp_v4_send_ack 1465 1428 -37
tcp_v4_send_reset 3269 3201 -68
Total: Before=30075907, After=30075759, chg -0.00%
No functional change intended.
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
net/ipv4/tcp_ipv4.c | 13 ++++---------
net/ipv4/tcp_output.c | 6 ++----
net/ipv6/tcp_ipv6.c | 3 +--
3 files changed, 7 insertions(+), 15 deletions(-)
diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
index 2ea8253b737a..1a413ca0e3bf 100644
--- a/net/ipv4/tcp_ipv4.c
+++ b/net/ipv4/tcp_ipv4.c
@@ -726,10 +726,10 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb,
struct {
struct tcphdr th;
__be32 opt[REPLY_OPTIONS_LEN];
- } rep;
+ } rep = {0};
const __u8 *md5_hash_location = NULL;
const struct tcp_ao_hdr *aoh;
- struct ip_reply_arg arg;
+ struct ip_reply_arg arg = {0};
#ifdef CONFIG_TCP_MD5SIG
struct tcp_md5sig_key *key = NULL;
unsigned char newhash[16];
@@ -751,7 +751,6 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb,
return;
/* Swap the send and the receive. */
- memset(&rep, 0, sizeof(rep));
rep.th.dest = th->source;
rep.th.source = th->dest;
rep.th.doff = sizeof(struct tcphdr) / 4;
@@ -765,7 +764,6 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb,
skb->len - (th->doff << 2));
}
- memset(&arg, 0, sizeof(arg));
arg.iov[0].iov_base = (unsigned char *)&rep;
arg.iov[0].iov_len = sizeof(rep.th);
@@ -921,15 +919,12 @@ static void tcp_v4_send_ack(const struct sock *sk,
struct {
struct tcphdr th;
__be32 opt[(MAX_TCP_OPTION_SPACE >> 2)];
- } rep;
+ } rep = {0};
struct net *net = sock_net(sk);
- struct ip_reply_arg arg;
+ struct ip_reply_arg arg = {0};
struct sock *ctl_sk;
u64 transmit_time;
- memset(&rep.th, 0, sizeof(struct tcphdr));
- memset(&arg, 0, sizeof(arg));
-
arg.iov[0].iov_base = (unsigned char *)&rep;
arg.iov[0].iov_len = sizeof(rep.th);
if (tsecr) {
diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c
index 34a25ef61006..a0d5bed59f44 100644
--- a/net/ipv4/tcp_output.c
+++ b/net/ipv4/tcp_output.c
@@ -1533,7 +1533,7 @@ static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb,
struct inet_sock *inet;
struct tcp_sock *tp;
struct tcp_skb_cb *tcb;
- struct tcp_out_options opts;
+ struct tcp_out_options opts = {0};
unsigned int tcp_options_size, tcp_header_size;
struct sk_buff *oskb = NULL;
struct tcp_key key;
@@ -1566,7 +1566,6 @@ static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb,
inet = inet_sk(sk);
tcb = TCP_SKB_CB(skb);
- memset(&opts.cleared, 0, sizeof(opts.cleared));
tcp_get_current_key(sk, &key);
if (unlikely(tcb->tcp_flags & TCPHDR_SYN)) {
@@ -3934,7 +3933,7 @@ struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
{
struct inet_request_sock *ireq = inet_rsk(req);
const struct tcp_sock *tp = tcp_sk(sk);
- struct tcp_out_options opts;
+ struct tcp_out_options opts = {0};
struct tcp_key key = {};
struct sk_buff *skb;
int tcp_header_size;
@@ -3972,7 +3971,6 @@ struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
mss = tcp_mss_clamp(tp, dst_metric_advmss(dst));
- memset(&opts, 0, sizeof(opts));
now = tcp_clock_ns();
#ifdef CONFIG_SYN_COOKIES
if (unlikely(synack_type == TCP_SYNACK_COOKIE && ireq->tstamp_ok))
diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c
index 8dc3874e8b92..e801927a8ce3 100644
--- a/net/ipv6/tcp_ipv6.c
+++ b/net/ipv6/tcp_ipv6.c
@@ -847,7 +847,7 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32
struct dst_entry *dst;
struct sk_buff *buff;
struct tcphdr *t1;
- struct flowi6 fl6;
+ struct flowi6 fl6 = {0};
u32 mark = 0;
if (tsecr)
@@ -922,7 +922,6 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32
}
#endif
- memset(&fl6, 0, sizeof(fl6));
fl6.daddr = ipv6_hdr(skb)->saddr;
fl6.saddr = ipv6_hdr(skb)->daddr;
fl6.flowlabel = label;
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH net-next v1] tcp: remove redundant memset in hot paths with CONFIG_INIT_STACK_ALL_ZERO
2026-03-12 12:48 [PATCH net-next v1] tcp: remove redundant memset in hot paths with CONFIG_INIT_STACK_ALL_ZERO Jiayuan Chen
@ 2026-03-12 12:53 ` Eric Dumazet
2026-03-12 13:45 ` Jiayuan Chen
0 siblings, 1 reply; 3+ messages in thread
From: Eric Dumazet @ 2026-03-12 12:53 UTC (permalink / raw)
To: Jiayuan Chen
Cc: netdev, Neal Cardwell, Kuniyuki Iwashima, David S. Miller,
David Ahern, Jakub Kicinski, Paolo Abeni, Simon Horman,
linux-kernel
On Thu, Mar 12, 2026 at 1:48 PM Jiayuan Chen <jiayuan.chen@linux.dev> wrote:
>
> Modern compilers with CONFIG_INIT_STACK_ALL_ZERO enabled
> (-ftrivial-auto-var-init=zero) automatically zero-initialize all stack
> variables via `rep stos` in the function prologue. However, the compiler
> cannot eliminate explicit memset() calls on those same variables,
> resulting in redundant zeroing of the same memory region.
>
> Replace explicit memset() with `= {0}` initializers for stack variables
> in TCP hot path functions. With `= {0}`, the compiler recognizes a single
> initialization and generates only one zeroing sequence, whereas a
> separate memset() after declaration always produces a second `rep stos`.
No sorry. It seems you missed
commit cfcceb7a39fc10a6f896af8229bf81d96acb22cc
Author: Keita Morisaki <kmta1236@gmail.com>
Date: Wed Mar 4 20:15:17 2026 +0900
tcp: shrink per-packet memset in __tcp_transmit_skb()
No need to resend your patch, I do not think it is a good idea.
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH net-next v1] tcp: remove redundant memset in hot paths with CONFIG_INIT_STACK_ALL_ZERO
2026-03-12 12:53 ` Eric Dumazet
@ 2026-03-12 13:45 ` Jiayuan Chen
0 siblings, 0 replies; 3+ messages in thread
From: Jiayuan Chen @ 2026-03-12 13:45 UTC (permalink / raw)
To: Eric Dumazet, Jiayuan Chen
Cc: netdev, Neal Cardwell, Kuniyuki Iwashima, David S. Miller,
David Ahern, Jakub Kicinski, Paolo Abeni, Simon Horman,
linux-kernel
On 3/12/26 8:53 PM, Eric Dumazet wrote:
> On Thu, Mar 12, 2026 at 1:48 PM Jiayuan Chen <jiayuan.chen@linux.dev> wrote:
>> Modern compilers with CONFIG_INIT_STACK_ALL_ZERO enabled
>> (-ftrivial-auto-var-init=zero) automatically zero-initialize all stack
>> variables via `rep stos` in the function prologue. However, the compiler
>> cannot eliminate explicit memset() calls on those same variables,
>> resulting in redundant zeroing of the same memory region.
>>
>> Replace explicit memset() with `= {0}` initializers for stack variables
>> in TCP hot path functions. With `= {0}`, the compiler recognizes a single
>> initialization and generates only one zeroing sequence, whereas a
>> separate memset() after declaration always produces a second `rep stos`.
> No sorry. It seems you missed
>
> commit cfcceb7a39fc10a6f896af8229bf81d96acb22cc
> Author: Keita Morisaki <kmta1236@gmail.com>
> Date: Wed Mar 4 20:15:17 2026 +0900
>
> tcp: shrink per-packet memset in __tcp_transmit_skb()
>
> No need to resend your patch, I do not think it is a good idea.
Thanks. It seems this patch for __tcp_transmit_skb only saves a few bytes
of memset.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-03-12 13:46 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-12 12:48 [PATCH net-next v1] tcp: remove redundant memset in hot paths with CONFIG_INIT_STACK_ALL_ZERO Jiayuan Chen
2026-03-12 12:53 ` Eric Dumazet
2026-03-12 13:45 ` Jiayuan Chen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox