From: scott.k.mitch1@gmail.com
To: dev@dpdk.org
Cc: mb@smartsharesystems.com, stephen@networkplumber.org,
Scott <scott_mitchell@apple.com>,
Scott Mitchell <scott.k.mitch1@gmail.com>
Subject: [PATCH v12 3/3] eal/net: add workaround for GCC optimization bug
Date: Fri, 9 Jan 2026 20:56:51 -0500 [thread overview]
Message-ID: <20260110015651.26201-4-scott.k.mitch1@gmail.com> (raw)
In-Reply-To: <20260110015651.26201-1-scott.k.mitch1@gmail.com>
From: Scott <scott_mitchell@apple.com>
GCC has a bug where it incorrectly elides struct initialization in
inline functions when strict aliasing is enabled (-O2/-O3/-Os), causing
reads from uninitialized memory. This affects both designated initializers
and manual field assignment.
Add RTE_FORCE_INIT_BARRIER macro that uses an asm volatile memory barrier
to prevent the compiler from incorrectly optimizing away struct
initialization. Apply the workaround to pseudo-header checksum functions
in rte_ip4.h, rte_ip6.h, hinic driver, and mlx5 driver.
Signed-off-by: Scott Mitchell <scott.k.mitch1@gmail.com>
---
drivers/net/hinic/hinic_pmd_tx.c | 2 ++
drivers/net/mlx5/mlx5_flow_dv.c | 2 ++
lib/eal/include/rte_common.h | 14 ++++++++++++++
lib/net/rte_ip4.h | 1 +
lib/net/rte_ip6.h | 1 +
5 files changed, 20 insertions(+)
diff --git a/drivers/net/hinic/hinic_pmd_tx.c b/drivers/net/hinic/hinic_pmd_tx.c
index 22fb0bffaf..570715531d 100644
--- a/drivers/net/hinic/hinic_pmd_tx.c
+++ b/drivers/net/hinic/hinic_pmd_tx.c
@@ -725,6 +725,7 @@ hinic_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
rte_cpu_to_be_16(rte_be_to_cpu_16(ipv4_hdr->total_length) -
rte_ipv4_hdr_len(ipv4_hdr));
}
+ RTE_FORCE_INIT_BARRIER(psd_hdr);
return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
}
@@ -743,6 +744,7 @@ hinic_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
else
psd_hdr.len = ipv6_hdr->payload_len;
+ RTE_FORCE_INIT_BARRIER(psd_hdr);
sum = __rte_raw_cksum(&ipv6_hdr->src_addr,
sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr), 0);
sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
diff --git a/drivers/net/mlx5/mlx5_flow_dv.c b/drivers/net/mlx5/mlx5_flow_dv.c
index 47f6d28410..1eeeb6747f 100644
--- a/drivers/net/mlx5/mlx5_flow_dv.c
+++ b/drivers/net/mlx5/mlx5_flow_dv.c
@@ -4445,6 +4445,8 @@ __flow_encap_decap_resource_register(struct rte_eth_dev *dev,
.reserve = 0,
}
};
+ RTE_FORCE_INIT_BARRIER(encap_decap_key);
+
struct mlx5_flow_cb_ctx ctx = {
.error = error,
.data = resource,
diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h
index 00d428e295..d58e856d96 100644
--- a/lib/eal/include/rte_common.h
+++ b/lib/eal/include/rte_common.h
@@ -555,6 +555,20 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
#define __rte_no_ubsan_alignment
#endif
+/**
+ * Force struct initialization to prevent GCC optimization bug.
+ * GCC has a bug where it incorrectly elides struct initialization in
+ * inline functions when strict aliasing is enabled, causing reads from
+ * uninitialized memory. This memory barrier prevents the misoptimization.
+ */
+#ifdef RTE_CC_GCC
+#define RTE_FORCE_INIT_BARRIER(var) do { \
+ asm volatile("" : "+m" (var)); \
+} while (0)
+#else
+#define RTE_FORCE_INIT_BARRIER(var)
+#endif
+
/*********** Macros for pointer arithmetic ********/
/**
diff --git a/lib/net/rte_ip4.h b/lib/net/rte_ip4.h
index 822a660cfb..a0839c584e 100644
--- a/lib/net/rte_ip4.h
+++ b/lib/net/rte_ip4.h
@@ -238,6 +238,7 @@ rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
psd_hdr.len = rte_cpu_to_be_16((uint16_t)(l3_len -
rte_ipv4_hdr_len(ipv4_hdr)));
}
+ RTE_FORCE_INIT_BARRIER(psd_hdr);
return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
}
diff --git a/lib/net/rte_ip6.h b/lib/net/rte_ip6.h
index d1abf1f5d5..902f100a44 100644
--- a/lib/net/rte_ip6.h
+++ b/lib/net/rte_ip6.h
@@ -572,6 +572,7 @@ rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
else
psd_hdr.len = ipv6_hdr->payload_len;
+ RTE_FORCE_INIT_BARRIER(psd_hdr);
sum = __rte_raw_cksum(&ipv6_hdr->src_addr,
sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
0);
--
2.39.5 (Apple Git-154)
next prev parent reply other threads:[~2026-01-10 1:57 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-10 1:56 [PATCH v12 0/3] net: optimize raw checksum computation scott.k.mitch1
2026-01-10 1:56 ` [PATCH v12 1/3] net: optimize __rte_raw_cksum and add tests scott.k.mitch1
2026-01-10 2:28 ` Scott Mitchell
2026-01-10 14:47 ` Morten Brørup
2026-01-10 1:56 ` [PATCH v12 2/3] eal: add workaround for UBSAN alignment false positive scott.k.mitch1
2026-01-10 15:02 ` Morten Brørup
2026-01-10 1:56 ` scott.k.mitch1 [this message]
2026-01-10 15:29 ` [PATCH v12 3/3] eal/net: add workaround for GCC optimization bug Morten Brørup
2026-01-11 6:21 ` Scott Mitchell
2026-01-10 16:59 ` [PATCH v12 0/3] net: optimize raw checksum computation Stephen Hemminger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260110015651.26201-4-scott.k.mitch1@gmail.com \
--to=scott.k.mitch1@gmail.com \
--cc=dev@dpdk.org \
--cc=mb@smartsharesystems.com \
--cc=scott_mitchell@apple.com \
--cc=stephen@networkplumber.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).