From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 2AABA43E44; Thu, 11 Apr 2024 14:44:40 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id ED76D402A8; Thu, 11 Apr 2024 14:44:39 +0200 (CEST) Received: from dkmailrelay1.smartsharesystems.com (smartserver.smartsharesystems.com [77.243.40.215]) by mails.dpdk.org (Postfix) with ESMTP id 36DE24029C for ; Thu, 11 Apr 2024 14:44:39 +0200 (CEST) Received: from smartserver.smartsharesystems.com (smartserver.smartsharesys.local [192.168.4.10]) by dkmailrelay1.smartsharesystems.com (Postfix) with ESMTP id 027A021181; Thu, 11 Apr 2024 14:44:39 +0200 (CEST) Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Subject: RE: [PATCH v3] ethdev: fix strict aliasing lead to link cannot be up Date: Thu, 11 Apr 2024 14:44:35 +0200 X-MimeOLE: Produced By Microsoft Exchange V6.5 Message-ID: <98CBD80474FA8B44BF855DF32C47DC35E9F398@smartserver.smartshare.dk> In-Reply-To: <20240411120408.2397-1-fengchengwen@huawei.com> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [PATCH v3] ethdev: fix strict aliasing lead to link cannot be up Thread-Index: AdqMCKywhxt268M0Sh+dEQIwzrHiMwAASUGA References: <20240411030749.41874-1-fengchengwen@huawei.com> <20240411120408.2397-1-fengchengwen@huawei.com> From: =?iso-8859-1?Q?Morten_Br=F8rup?= To: "Chengwen Feng" , , Cc: , , , 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 > From: Chengwen Feng [mailto:fengchengwen@huawei.com] > Sent: Thursday, 11 April 2024 14.04 >=20 > 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. >=20 > This commit use union to avoid such aliasing violation. >=20 > [1] Strict aliasing problem with rte_eth_linkstatus_set() > https://marc.info/?l=3Ddpdk-dev&m=3D171274148514777&w=3D3 >=20 > Cc: stable@dpdk.org >=20 > Signed-off-by: Chengwen Feng > Signed-off-by: Dengdui Huang >=20 > --- > v3: fix checkpatch warning "missing --in-reply-to". > v2: add RTE_ATOMIC(uint64_t) wrap which address Morten's comment. >=20 > --- > lib/ethdev/ethdev_driver.h | 23 +++++++---------------- > lib/ethdev/rte_ethdev.h | 16 ++++++++++------ > 2 files changed, 17 insertions(+), 22 deletions(-) >=20 > 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 =3D (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) !=3D sizeof(uint64_t)); > + struct rte_eth_link old_link; >=20 > - orig.val64 =3D rte_atomic_exchange_explicit(dev_link, *(const > uint64_t *)new_link, > - rte_memory_order_seq_cst); > + old_link.val64 =3D rte_atomic_exchange_explicit(&dev->data- > >dev_link.val64, You are right; old_link has local scope and is on the stack, so atomic = store is not required. And since rte_eth_linkstatus_set() is an internal function called from = the driver only, it is probably safe to assume that *new_link is on the = caller's stack and doesn't change while being accessed by this function. I guess that new_link is passed by reference for performance and = future-proofing reasons; it could have been passed by value instead. If = it was passed by value, atomic access would certainly not be required. In other words: You are right here too; new_link does not require atomic = load. > + new_link->val64, > + rte_memory_order_seq_cst); >=20 > - return (orig.link.link_status =3D=3D new_link->link_status) ? -1 : = 0; > + return (old_link.link_status =3D=3D new_link->link_status) ? -1 : 0; > } >=20 > /** > @@ -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 =3D (uint64_t __rte_atomic *)&(dev->data- > >dev_link); > - uint64_t *dst =3D (uint64_t *)link; > - > - RTE_BUILD_BUG_ON(sizeof(*link) !=3D sizeof(uint64_t)); > - > - *dst =3D rte_atomic_load_explicit(src, rte_memory_order_seq_cst); > + link->val64 =3D rte_atomic_load_explicit(&dev->data->dev_link.val64, > + rte_memory_order_seq_cst); It is not safe to assume that the link pointer points to local memory on = the caller's stack. The link pointer might point to a shared memory area, used by multiple = threads/processes, so it needs to be stored atomically using = rte_atomic_store_explicit(&link->val64, ..., rte_memory_order_seq_cst). > } >=20 > /** > diff --git a/lib/ethdev/rte_ethdev.h b/lib/ethdev/rte_ethdev.h > index 147257d6a2..ccf43e468a 100644 > --- a/lib/ethdev/rte_ethdev.h > +++ b/lib/ethdev/rte_ethdev.h > @@ -332,12 +332,16 @@ 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 */ > + 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] */ > + }; > + }; > }; >=20 > /**@{@name Link negotiation > -- > 2.17.1