Skip to contentSkip to conversation

tcp: remove redundant memset in hot paths with CONFIG_INIT_STACK_ALL_ZERO

PATCH In review Jiayuan Chen 3 participants 3 messages Mar 12

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tcp: remove redundant memset in hot paths with CONFIG_INIT_STACK_ALL_ZERO

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.

---
 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(-)
net/ipv4/tcp_ipv4.c +4 -9
index 2ea8253b737a..1a413ca0e3bf 100644
@@ -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) {
net/ipv4/tcp_output.c +2 -4
index 34a25ef61006..a0d5bed59f44 100644
@@ -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))
net/ipv6/tcp_ipv6.c +1 -2
index 8dc3874e8b92..e801927a8ce3 100644
@@ -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;
Signature · 1 line
2.43.0
#1 lore raw
Quoted 11 lines · Jiayuan Chen

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 <[email protected]>
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.

#2 lore raw
Quoted 13 lines · Eric Dumazet
Quoted 10 lines · Jiayuan Chen

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

Quoted 7 lines

commit cfcceb7a39fc10a6f896af8229bf81d96acb22cc

Author: Keita Morisaki <[email protected]>
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.