mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net-next v2 0/2] net: dsa: yt921x: Fix MIB overflow wraparound routine
@ 2025-10-25 17:13 David Yang
  2025-10-25 17:13 ` [PATCH net-next v2 1/2] " David Yang
  2025-10-25 17:13 ` [PATCH net-next v2 2/2] net: dsa: yt921x: Protect MIB stats with a lock David Yang
  0 siblings, 2 replies; 7+ messages in thread
From: David Yang @ 2025-10-25 17:13 UTC (permalink / raw)
  To: netdev
  Cc: David Yang, Andrew Lunn, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, linux-kernel

Fix warning reported by static checker.

v1: https://lore.kernel.org/r/20251024084918.1353031-1-mmyangfl@gmail.com
  - take suggestion from David Laight
  - protect MIB stats with a lock

David Yang (2):
  net: dsa: yt921x: Fix MIB overflow wraparound routine
  net: dsa: yt921x: Protect MIB stats with a lock

 drivers/net/dsa/yt921x.c | 76 +++++++++++++++++++++++++---------------
 drivers/net/dsa/yt921x.h |  4 +++
 2 files changed, 52 insertions(+), 28 deletions(-)

-- 
2.51.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH net-next v2 1/2] net: dsa: yt921x: Fix MIB overflow wraparound routine
  2025-10-25 17:13 [PATCH net-next v2 0/2] net: dsa: yt921x: Fix MIB overflow wraparound routine David Yang
@ 2025-10-25 17:13 ` David Yang
  2025-10-27  2:30   ` Andrew Lunn
  2025-10-25 17:13 ` [PATCH net-next v2 2/2] net: dsa: yt921x: Protect MIB stats with a lock David Yang
  1 sibling, 1 reply; 7+ messages in thread
From: David Yang @ 2025-10-25 17:13 UTC (permalink / raw)
  To: netdev
  Cc: David Yang, Andrew Lunn, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, linux-kernel,
	Dan Carpenter, David Laight

Reported by the following Smatch static checker warning:

  drivers/net/dsa/yt921x.c:702 yt921x_read_mib()
  warn: was expecting a 64 bit value instead of '(~0)'

Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Closes: https://lore.kernel.org/netdev/aPsjYKQMzpY0nSXm@stanley.mountain/
Suggested-by: David Laight <david.laight.linux@gmail.com>
Signed-off-by: David Yang <mmyangfl@gmail.com>
---
 drivers/net/dsa/yt921x.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/drivers/net/dsa/yt921x.c b/drivers/net/dsa/yt921x.c
index ab762ffc4661..97a7eeb4ea15 100644
--- a/drivers/net/dsa/yt921x.c
+++ b/drivers/net/dsa/yt921x.c
@@ -687,21 +687,22 @@ static int yt921x_read_mib(struct yt921x_priv *priv, int port)
 		const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];
 		u32 reg = YT921X_MIBn_DATA0(port) + desc->offset;
 		u64 *valp = &((u64 *)mib)[i];
-		u64 val = *valp;
+		u64 val;
 		u32 val0;
-		u32 val1;
 
 		res = yt921x_reg_read(priv, reg, &val0);
 		if (res)
 			break;
 
 		if (desc->size <= 1) {
-			if (val < (u32)val)
-				/* overflow */
-				val += (u64)U32_MAX + 1;
-			val &= ~U32_MAX;
-			val |= val0;
+			u64 old_val = *valp;
+
+			val = (old_val & ~(u64)U32_MAX) | val0;
+			if (val < old_val)
+				val += 1ull << 32;
 		} else {
+			u32 val1;
+
 			res = yt921x_reg_read(priv, reg + 4, &val1);
 			if (res)
 				break;
-- 
2.51.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH net-next v2 2/2] net: dsa: yt921x: Protect MIB stats with a lock
  2025-10-25 17:13 [PATCH net-next v2 0/2] net: dsa: yt921x: Fix MIB overflow wraparound routine David Yang
  2025-10-25 17:13 ` [PATCH net-next v2 1/2] " David Yang
@ 2025-10-25 17:13 ` David Yang
  1 sibling, 0 replies; 7+ messages in thread
From: David Yang @ 2025-10-25 17:13 UTC (permalink / raw)
  To: netdev
  Cc: David Yang, Andrew Lunn, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, linux-kernel

64bit variables might not be atomic on 32bit architectures, thus cannot
be made lock-free. Protect them with a spin lock since get_stats64()
cannot sleep.

Signed-off-by: David Yang <mmyangfl@gmail.com>
---
 drivers/net/dsa/yt921x.c | 63 ++++++++++++++++++++++++++--------------
 drivers/net/dsa/yt921x.h |  4 +++
 2 files changed, 45 insertions(+), 22 deletions(-)

diff --git a/drivers/net/dsa/yt921x.c b/drivers/net/dsa/yt921x.c
index 97a7eeb4ea15..e5d7c6820089 100644
--- a/drivers/net/dsa/yt921x.c
+++ b/drivers/net/dsa/yt921x.c
@@ -671,22 +671,16 @@ yt921x_mbus_ext_init(struct yt921x_priv *priv, struct device_node *mnp)
 static int yt921x_read_mib(struct yt921x_priv *priv, int port)
 {
 	struct yt921x_port *pp = &priv->ports[port];
+	struct yt921x_mib *mib_new = &pp->mib_new;
 	struct device *dev = to_device(priv);
 	struct yt921x_mib *mib = &pp->mib;
+	u64 rx_frames;
+	u64 tx_frames;
 	int res = 0;
 
-	/* Reading of yt921x_port::mib is not protected by a lock and it's vain
-	 * to keep its consistency, since we have to read registers one by one
-	 * and there is no way to make a snapshot of MIB stats.
-	 *
-	 * Writing (by this function only) is and should be protected by
-	 * reg_lock.
-	 */
-
 	for (size_t i = 0; i < ARRAY_SIZE(yt921x_mib_descs); i++) {
 		const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];
 		u32 reg = YT921X_MIBn_DATA0(port) + desc->offset;
-		u64 *valp = &((u64 *)mib)[i];
 		u64 val;
 		u32 val0;
 
@@ -695,7 +689,7 @@ static int yt921x_read_mib(struct yt921x_priv *priv, int port)
 			break;
 
 		if (desc->size <= 1) {
-			u64 old_val = *valp;
+			u64 old_val = ((u64 *)mib)[i];
 
 			val = (old_val & ~(u64)U32_MAX) | val0;
 			if (val < old_val)
@@ -709,22 +703,31 @@ static int yt921x_read_mib(struct yt921x_priv *priv, int port)
 			val = ((u64)val0 << 32) | val1;
 		}
 
-		WRITE_ONCE(*valp, val);
+		((u64 *)mib_new)[i] = val;
 	}
 
-	pp->rx_frames = mib->rx_64byte + mib->rx_65_127byte +
-			mib->rx_128_255byte + mib->rx_256_511byte +
-			mib->rx_512_1023byte + mib->rx_1024_1518byte +
-			mib->rx_jumbo;
-	pp->tx_frames = mib->tx_64byte + mib->tx_65_127byte +
-			mib->tx_128_255byte + mib->tx_256_511byte +
-			mib->tx_512_1023byte + mib->tx_1024_1518byte +
-			mib->tx_jumbo;
-
-	if (res)
+	if (res) {
 		dev_err(dev, "Failed to %s port %d: %i\n", "read stats for",
 			port, res);
-	return res;
+		return res;
+	}
+
+	rx_frames = mib->rx_64byte + mib->rx_65_127byte +
+		    mib->rx_128_255byte + mib->rx_256_511byte +
+		    mib->rx_512_1023byte + mib->rx_1024_1518byte +
+		    mib->rx_jumbo;
+	tx_frames = mib->tx_64byte + mib->tx_65_127byte +
+		    mib->tx_128_255byte + mib->tx_256_511byte +
+		    mib->tx_512_1023byte + mib->tx_1024_1518byte +
+		    mib->tx_jumbo;
+
+	spin_lock(&pp->stats_lock);
+	*mib = *mib_new;
+	pp->rx_frames = rx_frames;
+	pp->tx_frames = tx_frames;
+	spin_unlock(&pp->stats_lock);
+
+	return 0;
 }
 
 static void yt921x_poll_mib(struct work_struct *work)
@@ -773,6 +776,7 @@ yt921x_dsa_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data)
 	yt921x_read_mib(priv, port);
 	mutex_unlock(&priv->reg_lock);
 
+	spin_lock(&pp->stats_lock);
 	j = 0;
 	for (size_t i = 0; i < ARRAY_SIZE(yt921x_mib_descs); i++) {
 		const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];
@@ -783,6 +787,7 @@ yt921x_dsa_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data)
 		data[j] = ((u64 *)mib)[i];
 		j++;
 	}
+	spin_unlock(&pp->stats_lock);
 }
 
 static int yt921x_dsa_get_sset_count(struct dsa_switch *ds, int port, int sset)
@@ -814,6 +819,7 @@ yt921x_dsa_get_eth_mac_stats(struct dsa_switch *ds, int port,
 	yt921x_read_mib(priv, port);
 	mutex_unlock(&priv->reg_lock);
 
+	spin_lock(&pp->stats_lock);
 	mac_stats->FramesTransmittedOK = pp->tx_frames;
 	mac_stats->SingleCollisionFrames = mib->tx_single_collisions;
 	mac_stats->MultipleCollisionFrames = mib->tx_multiple_collisions;
@@ -836,6 +842,7 @@ yt921x_dsa_get_eth_mac_stats(struct dsa_switch *ds, int port,
 	/* mac_stats->InRangeLengthErrors */
 	/* mac_stats->OutOfRangeLengthField */
 	mac_stats->FrameTooLongErrors = mib->rx_oversize_errors;
+	spin_unlock(&pp->stats_lock);
 }
 
 static void
@@ -850,9 +857,11 @@ yt921x_dsa_get_eth_ctrl_stats(struct dsa_switch *ds, int port,
 	yt921x_read_mib(priv, port);
 	mutex_unlock(&priv->reg_lock);
 
+	spin_lock(&pp->stats_lock);
 	ctrl_stats->MACControlFramesTransmitted = mib->tx_pause;
 	ctrl_stats->MACControlFramesReceived = mib->rx_pause;
 	/* ctrl_stats->UnsupportedOpcodesReceived */
+	spin_unlock(&pp->stats_lock);
 }
 
 static const struct ethtool_rmon_hist_range yt921x_rmon_ranges[] = {
@@ -881,6 +890,8 @@ yt921x_dsa_get_rmon_stats(struct dsa_switch *ds, int port,
 
 	*ranges = yt921x_rmon_ranges;
 
+	spin_lock(&pp->stats_lock);
+
 	rmon_stats->undersize_pkts = mib->rx_undersize_errors;
 	rmon_stats->oversize_pkts = mib->rx_oversize_errors;
 	rmon_stats->fragments = mib->rx_alignment_errors;
@@ -901,6 +912,8 @@ yt921x_dsa_get_rmon_stats(struct dsa_switch *ds, int port,
 	rmon_stats->hist_tx[4] = mib->tx_512_1023byte;
 	rmon_stats->hist_tx[5] = mib->tx_1024_1518byte;
 	rmon_stats->hist_tx[6] = mib->tx_jumbo;
+
+	spin_unlock(&pp->stats_lock);
 }
 
 static void
@@ -911,6 +924,8 @@ yt921x_dsa_get_stats64(struct dsa_switch *ds, int port,
 	struct yt921x_port *pp = &priv->ports[port];
 	struct yt921x_mib *mib = &pp->mib;
 
+	spin_lock(&pp->stats_lock);
+
 	stats->rx_length_errors = mib->rx_undersize_errors +
 				  mib->rx_fragment_errors;
 	stats->rx_over_errors = mib->rx_oversize_errors;
@@ -937,6 +952,8 @@ yt921x_dsa_get_stats64(struct dsa_switch *ds, int port,
 	/* stats->tx_dropped */
 	stats->multicast = mib->rx_multicast;
 	stats->collisions = mib->tx_collisions;
+
+	spin_unlock(&pp->stats_lock);
 }
 
 static void
@@ -951,8 +968,10 @@ yt921x_dsa_get_pause_stats(struct dsa_switch *ds, int port,
 	yt921x_read_mib(priv, port);
 	mutex_unlock(&priv->reg_lock);
 
+	spin_lock(&pp->stats_lock);
 	pause_stats->tx_pause_frames = mib->tx_pause;
 	pause_stats->rx_pause_frames = mib->rx_pause;
+	spin_unlock(&pp->stats_lock);
 }
 
 static int
diff --git a/drivers/net/dsa/yt921x.h b/drivers/net/dsa/yt921x.h
index 3e85d90826fb..be69c3508da8 100644
--- a/drivers/net/dsa/yt921x.h
+++ b/drivers/net/dsa/yt921x.h
@@ -470,9 +470,13 @@ struct yt921x_port {
 	bool isolated;
 
 	struct delayed_work mib_read;
+	spinlock_t stats_lock;
 	struct yt921x_mib mib;
 	u64 rx_frames;
 	u64 tx_frames;
+
+	/* only used by read routine to avoid huge allocations on the stack */
+	struct yt921x_mib mib_new;
 };
 
 struct yt921x_reg_ops {
-- 
2.51.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH net-next v2 1/2] net: dsa: yt921x: Fix MIB overflow wraparound routine
  2025-10-25 17:13 ` [PATCH net-next v2 1/2] " David Yang
@ 2025-10-27  2:30   ` Andrew Lunn
  2025-10-27  2:51     ` Yangfl
  0 siblings, 1 reply; 7+ messages in thread
From: Andrew Lunn @ 2025-10-27  2:30 UTC (permalink / raw)
  To: David Yang
  Cc: netdev, Vladimir Oltean, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, linux-kernel, Dan Carpenter,
	David Laight

On Sun, Oct 26, 2025 at 01:13:10AM +0800, David Yang wrote:
> Reported by the following Smatch static checker warning:
> 
>   drivers/net/dsa/yt921x.c:702 yt921x_read_mib()
>   warn: was expecting a 64 bit value instead of '(~0)'
> 
> Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> Closes: https://lore.kernel.org/netdev/aPsjYKQMzpY0nSXm@stanley.mountain/
> Suggested-by: David Laight <david.laight.linux@gmail.com>
> Signed-off-by: David Yang <mmyangfl@gmail.com>
> ---
>  drivers/net/dsa/yt921x.c | 15 ++++++++-------
>  1 file changed, 8 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/net/dsa/yt921x.c b/drivers/net/dsa/yt921x.c
> index ab762ffc4661..97a7eeb4ea15 100644
> --- a/drivers/net/dsa/yt921x.c
> +++ b/drivers/net/dsa/yt921x.c
> @@ -687,21 +687,22 @@ static int yt921x_read_mib(struct yt921x_priv *priv, int port)
>  		const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];
>  		u32 reg = YT921X_MIBn_DATA0(port) + desc->offset;
>  		u64 *valp = &((u64 *)mib)[i];
> -		u64 val = *valp;
> +		u64 val;
>  		u32 val0;
> -		u32 val1;
>  
>  		res = yt921x_reg_read(priv, reg, &val0);
>  		if (res)
>  			break;
>  
>  		if (desc->size <= 1) {
> -			if (val < (u32)val)
> -				/* overflow */
> -				val += (u64)U32_MAX + 1;
> -			val &= ~U32_MAX;
> -			val |= val0;
> +			u64 old_val = *valp;
> +
> +			val = (old_val & ~(u64)U32_MAX) | val0;
> +			if (val < old_val)
> +				val += 1ull << 32;
>  		} else {
> +			u32 val1;
> +

What David suggested, https://lore.kernel.org/all/20251024132117.43f39504@pumpkin/ was

		if (desc->size <= 1) {
			u64 old_val = *valp;
			val = upper32_bits(old_val) | val0;
			if (val < old_val)
				val += 1ull << 32;
		}

I believe there is a minor typo here, it should be upper_32_bits(),
but what you implemented is not really what David suggested.

	Andrew

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH net-next v2 1/2] net: dsa: yt921x: Fix MIB overflow wraparound routine
  2025-10-27  2:30   ` Andrew Lunn
@ 2025-10-27  2:51     ` Yangfl
  2025-10-27 12:14       ` Andrew Lunn
  0 siblings, 1 reply; 7+ messages in thread
From: Yangfl @ 2025-10-27  2:51 UTC (permalink / raw)
  To: Andrew Lunn
  Cc: netdev, Vladimir Oltean, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, linux-kernel, Dan Carpenter,
	David Laight

On Mon, Oct 27, 2025 at 10:30 AM Andrew Lunn <andrew@lunn.ch> wrote:
>
> On Sun, Oct 26, 2025 at 01:13:10AM +0800, David Yang wrote:
> > Reported by the following Smatch static checker warning:
> >
> >   drivers/net/dsa/yt921x.c:702 yt921x_read_mib()
> >   warn: was expecting a 64 bit value instead of '(~0)'
> >
> > Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> > Closes: https://lore.kernel.org/netdev/aPsjYKQMzpY0nSXm@stanley.mountain/
> > Suggested-by: David Laight <david.laight.linux@gmail.com>
> > Signed-off-by: David Yang <mmyangfl@gmail.com>
> > ---
> >  drivers/net/dsa/yt921x.c | 15 ++++++++-------
> >  1 file changed, 8 insertions(+), 7 deletions(-)
> >
> > diff --git a/drivers/net/dsa/yt921x.c b/drivers/net/dsa/yt921x.c
> > index ab762ffc4661..97a7eeb4ea15 100644
> > --- a/drivers/net/dsa/yt921x.c
> > +++ b/drivers/net/dsa/yt921x.c
> > @@ -687,21 +687,22 @@ static int yt921x_read_mib(struct yt921x_priv *priv, int port)
> >               const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];
> >               u32 reg = YT921X_MIBn_DATA0(port) + desc->offset;
> >               u64 *valp = &((u64 *)mib)[i];
> > -             u64 val = *valp;
> > +             u64 val;
> >               u32 val0;
> > -             u32 val1;
> >
> >               res = yt921x_reg_read(priv, reg, &val0);
> >               if (res)
> >                       break;
> >
> >               if (desc->size <= 1) {
> > -                     if (val < (u32)val)
> > -                             /* overflow */
> > -                             val += (u64)U32_MAX + 1;
> > -                     val &= ~U32_MAX;
> > -                     val |= val0;
> > +                     u64 old_val = *valp;
> > +
> > +                     val = (old_val & ~(u64)U32_MAX) | val0;
> > +                     if (val < old_val)
> > +                             val += 1ull << 32;
> >               } else {
> > +                     u32 val1;
> > +
>
> What David suggested, https://lore.kernel.org/all/20251024132117.43f39504@pumpkin/ was
>
>                 if (desc->size <= 1) {
>                         u64 old_val = *valp;
>                         val = upper32_bits(old_val) | val0;
>                         if (val < old_val)
>                                 val += 1ull << 32;
>                 }
>
> I believe there is a minor typo here, it should be upper_32_bits(),
> but what you implemented is not really what David suggested.
>
>         Andrew

I didn't find the definition for upper32_bits, so...

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH net-next v2 1/2] net: dsa: yt921x: Fix MIB overflow wraparound routine
  2025-10-27  2:51     ` Yangfl
@ 2025-10-27 12:14       ` Andrew Lunn
  2025-10-27 14:24         ` Yangfl
  0 siblings, 1 reply; 7+ messages in thread
From: Andrew Lunn @ 2025-10-27 12:14 UTC (permalink / raw)
  To: Yangfl
  Cc: netdev, Vladimir Oltean, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, linux-kernel, Dan Carpenter,
	David Laight

> > > diff --git a/drivers/net/dsa/yt921x.c b/drivers/net/dsa/yt921x.c
> > > index ab762ffc4661..97a7eeb4ea15 100644
> > > --- a/drivers/net/dsa/yt921x.c
> > > +++ b/drivers/net/dsa/yt921x.c
> > > @@ -687,21 +687,22 @@ static int yt921x_read_mib(struct yt921x_priv *priv, int port)
> > >               const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];
> > >               u32 reg = YT921X_MIBn_DATA0(port) + desc->offset;
> > >               u64 *valp = &((u64 *)mib)[i];
> > > -             u64 val = *valp;
> > > +             u64 val;
> > >               u32 val0;
> > > -             u32 val1;
> > >
> > >               res = yt921x_reg_read(priv, reg, &val0);
> > >               if (res)
> > >                       break;
> > >
> > >               if (desc->size <= 1) {
> > > -                     if (val < (u32)val)
> > > -                             /* overflow */
> > > -                             val += (u64)U32_MAX + 1;
> > > -                     val &= ~U32_MAX;
> > > -                     val |= val0;
> > > +                     u64 old_val = *valp;
> > > +
> > > +                     val = (old_val & ~(u64)U32_MAX) | val0;
> > > +                     if (val < old_val)
> > > +                             val += 1ull << 32;
> > >               } else {
> > > +                     u32 val1;
> > > +
> >
> > What David suggested, https://lore.kernel.org/all/20251024132117.43f39504@pumpkin/ was
> >
> >                 if (desc->size <= 1) {
> >                         u64 old_val = *valp;
> >                         val = upper32_bits(old_val) | val0;
> >                         if (val < old_val)
> >                                 val += 1ull << 32;
> >                 }
> >
> > I believe there is a minor typo here, it should be upper_32_bits(),
> > but what you implemented is not really what David suggested.
> >
> >         Andrew
> 
> I didn't find the definition for upper32_bits, so...

You should of asked, or searched a bit harder, because what you
changed it to is different.

/**
 * upper_32_bits - return bits 32-63 of a number
 * @n: the number we're accessing
 *
 * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
 * the "right shift count >= width of type" warning when that quantity is
 * 32-bits.
 */
#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))

I don't see any shifting in your version.

And then i have to ask, which is correct?

How have you been testing this code? If this is TX bytes, for a 1G
link, it will overflow 32 bits in about 34 seconds. So a simple iperf
test could be used. If its TX packets, 64 byte packets could be done
in 5 hours.

	Andrew

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH net-next v2 1/2] net: dsa: yt921x: Fix MIB overflow wraparound routine
  2025-10-27 12:14       ` Andrew Lunn
@ 2025-10-27 14:24         ` Yangfl
  0 siblings, 0 replies; 7+ messages in thread
From: Yangfl @ 2025-10-27 14:24 UTC (permalink / raw)
  To: Andrew Lunn
  Cc: netdev, Vladimir Oltean, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, linux-kernel, Dan Carpenter,
	David Laight

On Mon, Oct 27, 2025 at 8:14 PM Andrew Lunn <andrew@lunn.ch> wrote:
>
> > > > diff --git a/drivers/net/dsa/yt921x.c b/drivers/net/dsa/yt921x.c
> > > > index ab762ffc4661..97a7eeb4ea15 100644
> > > > --- a/drivers/net/dsa/yt921x.c
> > > > +++ b/drivers/net/dsa/yt921x.c
> > > > @@ -687,21 +687,22 @@ static int yt921x_read_mib(struct yt921x_priv *priv, int port)
> > > >               const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];
> > > >               u32 reg = YT921X_MIBn_DATA0(port) + desc->offset;
> > > >               u64 *valp = &((u64 *)mib)[i];
> > > > -             u64 val = *valp;
> > > > +             u64 val;
> > > >               u32 val0;
> > > > -             u32 val1;
> > > >
> > > >               res = yt921x_reg_read(priv, reg, &val0);
> > > >               if (res)
> > > >                       break;
> > > >
> > > >               if (desc->size <= 1) {
> > > > -                     if (val < (u32)val)
> > > > -                             /* overflow */
> > > > -                             val += (u64)U32_MAX + 1;
> > > > -                     val &= ~U32_MAX;
> > > > -                     val |= val0;
> > > > +                     u64 old_val = *valp;
> > > > +
> > > > +                     val = (old_val & ~(u64)U32_MAX) | val0;
> > > > +                     if (val < old_val)
> > > > +                             val += 1ull << 32;
> > > >               } else {
> > > > +                     u32 val1;
> > > > +
> > >
> > > What David suggested, https://lore.kernel.org/all/20251024132117.43f39504@pumpkin/ was
> > >
> > >                 if (desc->size <= 1) {
> > >                         u64 old_val = *valp;
> > >                         val = upper32_bits(old_val) | val0;
> > >                         if (val < old_val)
> > >                                 val += 1ull << 32;
> > >                 }
> > >
> > > I believe there is a minor typo here, it should be upper_32_bits(),
> > > but what you implemented is not really what David suggested.
> > >
> > >         Andrew
> >
> > I didn't find the definition for upper32_bits, so...
>
> You should of asked, or searched a bit harder, because what you
> changed it to is different.
>
> /**
>  * upper_32_bits - return bits 32-63 of a number
>  * @n: the number we're accessing
>  *
>  * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
>  * the "right shift count >= width of type" warning when that quantity is
>  * 32-bits.
>  */
> #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
>
> I don't see any shifting in your version.
>
> And then i have to ask, which is correct?
>
> How have you been testing this code? If this is TX bytes, for a 1G
> link, it will overflow 32 bits in about 34 seconds. So a simple iperf
> test could be used. If its TX packets, 64 byte packets could be done
> in 5 hours.
>
>         Andrew

I used ping to check whether the statistics match expected values and
didn't realize iperf, I'll check that later.

> but what you implemented is not really what David suggested.

Shifting is clearly wrong here and I think they got upper_32_bits()
wrong too, but should or shouldn't I give credit to them
(Suggested-by), if I took most of, but not exactly all of their ideas?

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-10-27 14:25 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-10-25 17:13 [PATCH net-next v2 0/2] net: dsa: yt921x: Fix MIB overflow wraparound routine David Yang
2025-10-25 17:13 ` [PATCH net-next v2 1/2] " David Yang
2025-10-27  2:30   ` Andrew Lunn
2025-10-27  2:51     ` Yangfl
2025-10-27 12:14       ` Andrew Lunn
2025-10-27 14:24         ` Yangfl
2025-10-25 17:13 ` [PATCH net-next v2 2/2] net: dsa: yt921x: Protect MIB stats with a lock David Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®