From patchwork Thu Apr 18 07:28:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: fengchengwen X-Patchwork-Id: 139480 X-Patchwork-Delegate: ferruh.yigit@amd.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 2C42D43E9C; Thu, 18 Apr 2024 09:30:45 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id E5591402D6; Thu, 18 Apr 2024 09:30:44 +0200 (CEST) Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by mails.dpdk.org (Postfix) with ESMTP id D254940042 for ; Thu, 18 Apr 2024 09:30:38 +0200 (CEST) Received: from mail.maildlp.com (unknown [172.19.88.194]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VKqCx29TZzYdqB; Thu, 18 Apr 2024 15:29:33 +0800 (CST) Received: from dggpeml500024.china.huawei.com (unknown [7.185.36.10]) by mail.maildlp.com (Postfix) with ESMTPS id 175FF1403D5; Thu, 18 Apr 2024 15:30:35 +0800 (CST) Received: from localhost.localdomain (10.50.165.33) by dggpeml500024.china.huawei.com (7.185.36.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Thu, 18 Apr 2024 15:30:34 +0800 From: Chengwen Feng To: , CC: , , , , Subject: [PATCH v7] ethdev: fix strict aliasing lead to link cannot be up Date: Thu, 18 Apr 2024 07:28:18 +0000 Message-ID: <20240418072818.2074-1-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240411030749.41874-1-fengchengwen@huawei.com> References: <20240411030749.41874-1-fengchengwen@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.50.165.33] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpeml500024.china.huawei.com (7.185.36.10) X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org 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 }; The same error with qos_sched example: ../examples/qos_sched/init.c:338:10: error: missing braces around initializer [-Werror=missing-braces] struct rte_eth_link link = {0}; So this commit replace { 0 } with memset in cxgbe and qos_sched. [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 Signed-off-by: Dengdui Huang Acked-by: Morten Brørup --- v7: add rte_atomic_store_explicit to get link status which address Morten's comment, and add Morten's acked-by. v6: fix DPDK CI compiler error with qos_sched. 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 ++- examples/qos_sched/init.c | 3 ++- lib/ethdev/ethdev_driver.h | 25 +++++++++---------------- lib/ethdev/rte_ethdev.h | 17 +++++++++++------ 4 files changed, 24 insertions(+), 24 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/examples/qos_sched/init.c b/examples/qos_sched/init.c index d8abae635a..32964fd57e 100644 --- a/examples/qos_sched/init.c +++ b/examples/qos_sched/init.c @@ -335,7 +335,7 @@ int app_init(void) for(i = 0; i < nb_pfc; i++) { uint32_t socket = rte_lcore_to_socket_id(qos_conf[i].rx_core); struct rte_ring *ring; - struct rte_eth_link link = {0}; + struct rte_eth_link link; int retry_count = 100, retry_delay = 100; /* try every 100ms for 10 sec */ snprintf(ring_name, MAX_NAME_LEN, "ring-%u-%u", i, qos_conf[i].rx_core); @@ -367,6 +367,7 @@ int app_init(void) app_init_port(qos_conf[i].rx_port, qos_conf[i].mbuf_pool); app_init_port(qos_conf[i].tx_port, qos_conf[i].mbuf_pool); + memset(&link, 0, sizeof(link)); rte_eth_link_get(qos_conf[i].tx_port, &link); if (link.link_status == 0) printf("Waiting for link on port %u\n", qos_conf[i].tx_port); diff --git a/lib/ethdev/ethdev_driver.h b/lib/ethdev/ethdev_driver.h index 0dbf2dd6a2..a7159cd1b1 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,10 @@ 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); + rte_atomic_store_explicit(&link->val64, + rte_atomic_load_explicit(&dev->data->dev_link.val64, + rte_memory_order_seq_cst), + 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