DPDK patches and discussions
 help / color / mirror / Atom feed
From: Chengwen Feng <fengchengwen@huawei.com>
To: <thomas@monjalon.net>, <ferruh.yigit@amd.com>
Cc: <dev@dpdk.org>, <huangdengdui@huawei.com>,
	<mb@smartsharesystems.com>, <stephen@networkplumber.org>,
	<roretzla@linux.microsoft.com>
Subject: [PATCH v5] ethdev: fix strict aliasing lead to link cannot be up
Date: Sat, 13 Apr 2024 08:04:40 +0000	[thread overview]
Message-ID: <20240413080440.19831-1-fengchengwen@huawei.com> (raw)
In-Reply-To: <20240411030749.41874-1-fengchengwen@huawei.com>

Fix a problem introduced by a compiler upgrade (from gcc10 to gcc12.3),
which will lead the hns3 NIC can't link up. The root cause is strict
aliasing violation in rte_eth_linkstatus_set() with hns3 driver, see
[1] for more details.

This commit use union to avoid such aliasing violation.

Note: DPDK CI report compiler error (see [2] for more details):
../drivers/net/cxgbe/cxgbe_ethdev.c:214:9: error: missing braces around
initializer [-Werror=missing-braces]
  struct rte_eth_link new_link = { 0 };
So this commit replace { 0 } with memset in cxgbe.

[1] Strict aliasing problem with rte_eth_linkstatus_set()
    https://marc.info/?l=dpdk-dev&m=171274148514777&w=3
[2] https://mails.dpdk.org/archives/test-report/2024-April/637966.html

Cc: stable@dpdk.org

Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
Signed-off-by: Dengdui Huang <huangdengdui@huawei.com>

---
v5: fix DPDK CI compiler error due to GCC extension [-Werror=pedantic]
v4: fix DPDK CI compiler error with cxgbe.
v3: fix checkpatch warning "missing --in-reply-to".
v2: add RTE_ATOMIC(uint64_t) wrap which address Morten's comment.

---
 drivers/net/cxgbe/cxgbe_ethdev.c |  3 ++-
 lib/ethdev/ethdev_driver.h       | 23 +++++++----------------
 lib/ethdev/rte_ethdev.h          | 17 +++++++++++------
 3 files changed, 20 insertions(+), 23 deletions(-)

diff --git a/drivers/net/cxgbe/cxgbe_ethdev.c b/drivers/net/cxgbe/cxgbe_ethdev.c
index a27b9b266e..9b6a3651f9 100644
--- a/drivers/net/cxgbe/cxgbe_ethdev.c
+++ b/drivers/net/cxgbe/cxgbe_ethdev.c
@@ -211,9 +211,9 @@ int cxgbe_dev_link_update(struct rte_eth_dev *eth_dev,
 	unsigned int i, work_done, budget = 32;
 	struct link_config *lc = &pi->link_cfg;
 	struct adapter *adapter = pi->adapter;
-	struct rte_eth_link new_link = { 0 };
 	u8 old_link = pi->link_cfg.link_ok;
 	struct sge *s = &adapter->sge;
+	struct rte_eth_link new_link;
 
 	for (i = 0; i < CXGBE_LINK_STATUS_POLL_CNT; i++) {
 		if (!s->fw_evtq.desc)
@@ -232,6 +232,7 @@ int cxgbe_dev_link_update(struct rte_eth_dev *eth_dev,
 		rte_delay_ms(CXGBE_LINK_STATUS_POLL_MS);
 	}
 
+	memset(&new_link, 0, sizeof(new_link));
 	new_link.link_status = cxgbe_force_linkup(adapter) ?
 			       RTE_ETH_LINK_UP : pi->link_cfg.link_ok;
 	new_link.link_autoneg = (lc->link_caps & FW_PORT_CAP32_ANEG) ? 1 : 0;
diff --git a/lib/ethdev/ethdev_driver.h b/lib/ethdev/ethdev_driver.h
index 0dbf2dd6a2..9d831d5c84 100644
--- a/lib/ethdev/ethdev_driver.h
+++ b/lib/ethdev/ethdev_driver.h
@@ -1674,18 +1674,13 @@ static inline int
 rte_eth_linkstatus_set(struct rte_eth_dev *dev,
 		       const struct rte_eth_link *new_link)
 {
-	RTE_ATOMIC(uint64_t) *dev_link = (uint64_t __rte_atomic *)&(dev->data->dev_link);
-	union {
-		uint64_t val64;
-		struct rte_eth_link link;
-	} orig;
-
-	RTE_BUILD_BUG_ON(sizeof(*new_link) != sizeof(uint64_t));
+	struct rte_eth_link old_link;
 
-	orig.val64 = rte_atomic_exchange_explicit(dev_link, *(const uint64_t *)new_link,
-					rte_memory_order_seq_cst);
+	old_link.val64 = rte_atomic_exchange_explicit(&dev->data->dev_link.val64,
+						      new_link->val64,
+						      rte_memory_order_seq_cst);
 
-	return (orig.link.link_status == new_link->link_status) ? -1 : 0;
+	return (old_link.link_status == new_link->link_status) ? -1 : 0;
 }
 
 /**
@@ -1701,12 +1696,8 @@ static inline void
 rte_eth_linkstatus_get(const struct rte_eth_dev *dev,
 		       struct rte_eth_link *link)
 {
-	RTE_ATOMIC(uint64_t) *src = (uint64_t __rte_atomic *)&(dev->data->dev_link);
-	uint64_t *dst = (uint64_t *)link;
-
-	RTE_BUILD_BUG_ON(sizeof(*link) != sizeof(uint64_t));
-
-	*dst = rte_atomic_load_explicit(src, rte_memory_order_seq_cst);
+	link->val64 = rte_atomic_load_explicit(&dev->data->dev_link.val64,
+					       rte_memory_order_seq_cst);
 }
 
 /**
diff --git a/lib/ethdev/rte_ethdev.h b/lib/ethdev/rte_ethdev.h
index 147257d6a2..548fada1c7 100644
--- a/lib/ethdev/rte_ethdev.h
+++ b/lib/ethdev/rte_ethdev.h
@@ -332,12 +332,17 @@ struct rte_eth_stats {
 /**
  * A structure used to retrieve link-level information of an Ethernet port.
  */
-__extension__
-struct __rte_aligned(8) rte_eth_link { /**< aligned for atomic64 read/write */
-	uint32_t link_speed;        /**< RTE_ETH_SPEED_NUM_ */
-	uint16_t link_duplex  : 1;  /**< RTE_ETH_LINK_[HALF/FULL]_DUPLEX */
-	uint16_t link_autoneg : 1;  /**< RTE_ETH_LINK_[AUTONEG/FIXED] */
-	uint16_t link_status  : 1;  /**< RTE_ETH_LINK_[DOWN/UP] */
+struct rte_eth_link {
+	union {
+		RTE_ATOMIC(uint64_t) val64; /**< used for atomic64 read/write */
+		__extension__
+		struct {
+			uint32_t link_speed;	    /**< RTE_ETH_SPEED_NUM_ */
+			uint16_t link_duplex  : 1;  /**< RTE_ETH_LINK_[HALF/FULL]_DUPLEX */
+			uint16_t link_autoneg : 1;  /**< RTE_ETH_LINK_[AUTONEG/FIXED] */
+			uint16_t link_status  : 1;  /**< RTE_ETH_LINK_[DOWN/UP] */
+		};
+	};
 };
 
 /**@{@name Link negotiation
-- 
2.17.1


  parent reply	other threads:[~2024-04-13  8:07 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-11  3:07 [PATCH] " Chengwen Feng
2024-04-11  6:53 ` Morten Brørup
2024-04-11  6:58 ` Morten Brørup
2024-04-11 11:57   ` fengchengwen
2024-04-11 12:44     ` Morten Brørup
2024-04-11 12:04 ` [PATCH v3] " Chengwen Feng
2024-04-11 12:44   ` Morten Brørup
2024-04-12  3:27     ` fengchengwen
2024-04-12  7:24       ` Morten Brørup
2024-04-11 15:05 ` [PATCH] " Stephen Hemminger
2024-04-12  8:16   ` fengchengwen
2024-04-12  8:49 ` [PATCH v4] " Chengwen Feng
2024-04-13  8:04 ` Chengwen Feng [this message]
2024-04-13  8:48 ` [PATCH v6] " Chengwen Feng
2024-04-15 13:15   ` Morten Brørup
2024-04-18  7:28 ` [PATCH v7] " Chengwen Feng
2024-04-19 15:15   ` Ferruh Yigit
2024-04-19 15:25   ` Ferruh Yigit
2024-04-22  6:42     ` fengchengwen
2024-04-22  6:38 ` [PATCH v8] " Chengwen Feng
2024-04-22 10:54   ` Ferruh Yigit

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=20240413080440.19831-1-fengchengwen@huawei.com \
    --to=fengchengwen@huawei.com \
    --cc=dev@dpdk.org \
    --cc=ferruh.yigit@amd.com \
    --cc=huangdengdui@huawei.com \
    --cc=mb@smartsharesystems.com \
    --cc=roretzla@linux.microsoft.com \
    --cc=stephen@networkplumber.org \
    --cc=thomas@monjalon.net \
    /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).