[PATCH net v2] net: thunderx: avoid direct MTU assignment after WRITE_ONCE()

Alok Tiwari posted 1 patch 3 months, 1 week ago
There is a newer version of this series
drivers/net/ethernet/cavium/thunder/nicvf_main.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
[PATCH net v2] net: thunderx: avoid direct MTU assignment after WRITE_ONCE()
Posted by Alok Tiwari 3 months, 1 week ago
The current logic in nicvf_change_mtu() writes the new MTU to
netdev->mtu using WRITE_ONCE() before verifying if the hardware
update succeeds. However on hardware update failure, it attempts
to revert to the original MTU using a direct assignment
(netdev->mtu = orig_mtu)
which violates the intended of WRITE_ONCE protection introduced in
commit 1eb2cded45b3 ("net: annotate writes on dev->mtu from
ndo_change_mtu()")

Additionally, WRITE_ONCE(netdev->mtu, new_mtu) is unnecessarily
performed even when the device is not running.

Fix this by:
  Only writing netdev->mtu after successfully updating the hardware.
  Skipping hardware update when the device is down, and setting MTU
  directly.
  Remove unused variable orig_mtu.

This ensures that all writes to netdev->mtu are consistent with
WRITE_ONCE expectations and avoids unintended state corruption
on failure paths.

Signed-off-by: Alok Tiwari <alok.a.tiwari@oracle.com>
---
Note: This change is not tested due to hardware availability.
v1 -> v2
remove unused variable orig_mtu.
---
 drivers/net/ethernet/cavium/thunder/nicvf_main.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/net/ethernet/cavium/thunder/nicvf_main.c b/drivers/net/ethernet/cavium/thunder/nicvf_main.c
index aebb9fef3f6eb..633244bc00f35 100644
--- a/drivers/net/ethernet/cavium/thunder/nicvf_main.c
+++ b/drivers/net/ethernet/cavium/thunder/nicvf_main.c
@@ -1578,7 +1578,6 @@ int nicvf_open(struct net_device *netdev)
 static int nicvf_change_mtu(struct net_device *netdev, int new_mtu)
 {
 	struct nicvf *nic = netdev_priv(netdev);
-	int orig_mtu = netdev->mtu;
 
 	/* For now just support only the usual MTU sized frames,
 	 * plus some headroom for VLAN, QinQ.
@@ -1589,16 +1588,18 @@ static int nicvf_change_mtu(struct net_device *netdev, int new_mtu)
 		return -EINVAL;
 	}
 
-	WRITE_ONCE(netdev->mtu, new_mtu);
 
-	if (!netif_running(netdev))
+	if (!netif_running(netdev)) {
+		WRITE_ONCE(netdev->mtu, new_mtu);
 		return 0;
+	}
 
 	if (nicvf_update_hw_max_frs(nic, new_mtu)) {
-		netdev->mtu = orig_mtu;
 		return -EINVAL;
 	}
 
+	WRITE_ONCE(netdev->mtu, new_mtu);
+
 	return 0;
 }
 
-- 
2.46.0
Re: [PATCH net v2] net: thunderx: avoid direct MTU assignment after WRITE_ONCE()
Posted by Simon Horman 3 months, 1 week ago
On Sat, Jun 28, 2025 at 10:15:37PM -0700, Alok Tiwari wrote:

...

> @@ -1589,16 +1588,18 @@ static int nicvf_change_mtu(struct net_device *netdev, int new_mtu)
>  		return -EINVAL;
>  	}
>  
> -	WRITE_ONCE(netdev->mtu, new_mtu);
>  
> -	if (!netif_running(netdev))
> +	if (!netif_running(netdev)) {
> +		WRITE_ONCE(netdev->mtu, new_mtu);
>  		return 0;
> +	}
>  
>  	if (nicvf_update_hw_max_frs(nic, new_mtu)) {
> -		netdev->mtu = orig_mtu;
>  		return -EINVAL;
>  	}

nit: curly brackets should be removed here, but see further comment below.

>  
> +	WRITE_ONCE(netdev->mtu, new_mtu);
> +
>  	return 0;
>  }

Could this be more succinctly expressed as follows?
(Completely untested!)

	if (netif_running(netdev) && nicvf_update_hw_max_frs(nic, new_mtu))
		return -ENIVAL;

	WRITE_ONCE(netdev->mtu, new_mtu);

	return 0;