From nobody Tue Apr 7 16:13:04 2026 Received: from out-170.mta0.migadu.com (out-170.mta0.migadu.com [91.218.175.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E19F8286D57 for ; Thu, 12 Mar 2026 12:48:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773319714; cv=none; b=tT2tJJ0zuwIjdy0o1KDeV0bYtIvsene9sBQD8pR78UNGUP7rYRpf/zkcO3RrojjbtphCco7M7snLXy67widhiGVSaYbliFI4AzfKdm/uKDudD9r8aILMJp+p+F/OubY7Ci442/rW7oRMR5Gdrf1OIzBCmZMbtQxZjjGyGp0U1xI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773319714; c=relaxed/simple; bh=DqyrX0jzjSnAv4nvDlX0KV9J5mdZoV6WJSm2RMlPy2c=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=pYz7acX5dh7eAYPIkAU4WeZZA5pG7m4pvLD65myi/77qg2r9etmS3uQbpOfpu5F94v7uuSDSs5R1ycq8ABGr7ZyLrkA+nS/PKsFSzpjtzMubUW7FDQd6bP0VTxkCJPiOR8RnMLZ7VjenQ9SkPBg2MsLxCth7q+weskBSBtQNZjg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=C17PRAjV; arc=none smtp.client-ip=91.218.175.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="C17PRAjV" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1773319709; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=DxC3T2vZotQIwHfuvMRzjvgydJ1z+FABP6OfVopqphY=; b=C17PRAjVnnwzB1xwomthP960p0r2HOxqrFAZrle8A/wbF9DGx7Z+9mTOaRQvIO/IRN9eRm X8TyPwVlnWca4E5sg3vSwwH/3d5iwGMMnWg4pr/CbBlmr7KdM/hImrWdymsaYIjRvVtiWM hCBypba0S1zymW32KXWM9VKfKNMw5bc= From: Jiayuan Chen To: netdev@vger.kernel.org Cc: Jiayuan Chen , Eric Dumazet , Neal Cardwell , Kuniyuki Iwashima , "David S. Miller" , David Ahern , Jakub Kicinski , Paolo Abeni , Simon Horman , linux-kernel@vger.kernel.org Subject: [PATCH net-next v1] tcp: remove redundant memset in hot paths with CONFIG_INIT_STACK_ALL_ZERO Date: Thu, 12 Mar 2026 20:48:06 +0800 Message-ID: <20260312124807.186341-1-jiayuan.chen@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Modern compilers with CONFIG_INIT_STACK_ALL_ZERO enabled (-ftrivial-auto-var-init=3Dzero) 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 `=3D {0}` initializers for stack variables in TCP hot path functions. With `=3D {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=3D30075907, After=3D30075759, chg -0.00% No functional change intended. Signed-off-by: Jiayuan Chen --- 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 =3D {0}; const __u8 *md5_hash_location =3D NULL; const struct tcp_ao_hdr *aoh; - struct ip_reply_arg arg; + struct ip_reply_arg arg =3D {0}; #ifdef CONFIG_TCP_MD5SIG struct tcp_md5sig_key *key =3D NULL; unsigned char newhash[16]; @@ -751,7 +751,6 @@ static void tcp_v4_send_reset(const struct sock *sk, st= ruct sk_buff *skb, return; =20 /* Swap the send and the receive. */ - memset(&rep, 0, sizeof(rep)); rep.th.dest =3D th->source; rep.th.source =3D th->dest; rep.th.doff =3D sizeof(struct tcphdr) / 4; @@ -765,7 +764,6 @@ static void tcp_v4_send_reset(const struct sock *sk, st= ruct sk_buff *skb, skb->len - (th->doff << 2)); } =20 - memset(&arg, 0, sizeof(arg)); arg.iov[0].iov_base =3D (unsigned char *)&rep; arg.iov[0].iov_len =3D sizeof(rep.th); =20 @@ -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 =3D {0}; struct net *net =3D sock_net(sk); - struct ip_reply_arg arg; + struct ip_reply_arg arg =3D {0}; struct sock *ctl_sk; u64 transmit_time; =20 - memset(&rep.th, 0, sizeof(struct tcphdr)); - memset(&arg, 0, sizeof(arg)); - arg.iov[0].iov_base =3D (unsigned char *)&rep; arg.iov[0].iov_len =3D 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 =3D {0}; unsigned int tcp_options_size, tcp_header_size; struct sk_buff *oskb =3D NULL; struct tcp_key key; @@ -1566,7 +1566,6 @@ static int __tcp_transmit_skb(struct sock *sk, struct= sk_buff *skb, =20 inet =3D inet_sk(sk); tcb =3D TCP_SKB_CB(skb); - memset(&opts.cleared, 0, sizeof(opts.cleared)); =20 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 =3D inet_rsk(req); const struct tcp_sock *tp =3D tcp_sk(sk); - struct tcp_out_options opts; + struct tcp_out_options opts =3D {0}; struct tcp_key key =3D {}; 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, =20 mss =3D tcp_mss_clamp(tp, dst_metric_advmss(dst)); =20 - memset(&opts, 0, sizeof(opts)); now =3D tcp_clock_ns(); #ifdef CONFIG_SYN_COOKIES if (unlikely(synack_type =3D=3D 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 =3D {0}; u32 mark =3D 0; =20 if (tsecr) @@ -922,7 +922,6 @@ static void tcp_v6_send_response(const struct sock *sk,= struct sk_buff *skb, u32 } #endif =20 - memset(&fl6, 0, sizeof(fl6)); fl6.daddr =3D ipv6_hdr(skb)->saddr; fl6.saddr =3D ipv6_hdr(skb)->daddr; fl6.flowlabel =3D label; --=20 2.43.0