Linux Documentation
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: james.hilliard1@gmail.com
Cc: andrew@lunn.ch, hkallweit1@gmail.com, linux@armlinux.org.uk,
	davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
	pabeni@redhat.com, horms@kernel.org, corbet@lwn.net,
	skhan@linuxfoundation.org, rdunlap@infradead.org,
	o.rempel@pengutronix.de, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org
Subject: Re: [PATCH net-next v6 3/3] net: phy: acx00: report receive statistics
Date: Thu, 19 Nov 2026 12:00:19 +0000	[thread overview]
Message-ID: <179508961966.3136.18071124735934068397@kernel.org> (raw)
In-Reply-To: <20261116-submit-acx00-phy-stats-v1-v6-3-8feef8e76cd5@gmail.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 3

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] Only the RX byte counter gets a plausibility check: samples >=
  ACX00_RX_BYTES_WRAP are skipped without moving the baseline.
- [Low] In __acx00_ephy_config_init() (drivers/net/phy/xpowers/acx00.c),
  the new statistics baseline snapshot goes through…
- [Low] In __acx00_ephy_update_stats(), stats_last[ACX00_STAT_RX_PACKETS]
  and stats_start[ACX00_STAT_RX_PACKETS] only advance when…
- [Low] Counter baselines are only invalidated inside
  acx00_ephy_soft_reset() (and power-off/resume), under priv->state_lock.

--

net: phy: acx00: report receive statistics

This commit adds receive statistics to the AC200/AC300 PHY driver. It
reports rx_packets, rx_errors and rx_bytes through get_phy_stats, using
counters that update_stats samples from vendor page 9 and adds into
64-bit totals with wrap handling. Baselines are invalidated across soft
reset, power-off and resume, and deltas are discarded after gaps of 320
seconds or more.

> diff --git a/drivers/net/phy/xpowers/acx00.c b/drivers/net/phy/xpowers/acx00.c
> index cee6c7ff0278e..0015816fad078 100644
> --- a/drivers/net/phy/xpowers/acx00.c
> +++ b/drivers/net/phy/xpowers/acx00.c

[ ... ]

> @@ -110,6 +138,169 @@ static int acx00_ephy_write_page(struct phy_device *phydev, int page)

[ ... ]

> +/* Return a mask of usable counters, or a negative MDIO error. */
> +static int acx00_ephy_read_counts(struct phy_device *phydev, u32 *counts)
> +{

[ ... ]

> +	/* Bracket the packet counter with an unchanged CRC count. */
> +	for (i = 0; i < ACX00_COUNTER_READ_ATTEMPTS; i++) {
> +		ret = acx00_ephy_read_counter(phydev, ACX00_PAGE9_RX_PACKETS_HIGH_REG,
> +					      &packets);
> +		if (ret < 0)
> +			return ret;
> +		if (ret)
> +			break;
> +		ret = acx00_ephy_read_counter(phydev, ACX00_PAGE9_RX_CRC_ERRORS_HIGH_REG,
> +					      &errors);
> +		if (ret < 0)
> +			return ret;
> +		if (ret)
> +			break;
> +		if (errors == counts[ACX00_STAT_RX_CRC_ERRORS]) {
> +			/* Both hardware counters and their difference wrap at 2^32. */
> +			counts[ACX00_STAT_RX_PACKETS] = packets - errors;
> +			return BIT(ACX00_STAT_RX_PACKETS) | BIT(ACX00_STAT_RX_CRC_ERRORS);
> +		}
> +		counts[ACX00_STAT_RX_CRC_ERRORS] = errors;
> +	}
> +
> +	/* Keep reporting errors even when no coherent packet sample is possible. */
> +	return BIT(ACX00_STAT_RX_CRC_ERRORS);

[Severity: Low]
When the CRC count changes during all three bracket attempts, only the CRC
bit is returned. This leaves stats_last[ACX00_STAT_RX_PACKETS] and
stats_start[ACX00_STAT_RX_PACKETS] at their old values.

On a link with a sustained high CRC-error rate, this could happen on every
poll for 320 seconds or more:

_phy_state_machine()->phy_update_stats()->acx00_ephy_update_stats()

When a coherent pair finally arrives, __acx00_ephy_update_stats() checks
the interval against the stale timestamp:

	if ((priv->stats_valid & BIT(i)) &&
	    ktime_ms_delta(ktime_get_boottime(), priv->stats_start[i]) <
	    (s64)ACX00_STATS_MAX_INTERVAL_MS) {
		...
	}
	priv->stats_last[i] = counts[i];
	priv->stats_start[i] = start;

The delta is dropped, but the baseline still moves forward. Wouldn't all
good frames received since the last coherent sample then be permanently
missing from rx_packets?

That seems to conflict with the commit message:

    A later coherent pair accounts for the intervening successful frames.

The 320 second window comes from the 4e9 byte-counter modulus. At about
148,810 frames/s at 100 Mb/s, the 32-bit packets-minus-errors value would
take hours to wrap. Could the frame counters use a longer window?

> +}

[ ... ]

> +	ret = acx00_ephy_read_counter(phydev, ACX00_PAGE9_RX_BYTES_HIGH_REG,
> +				      &counts[ACX00_STAT_RX_BYTES]);
> +	if (ret < 0)
> +		goto out_restore_page;
> +	if (!ret && counts[ACX00_STAT_RX_BYTES] < ACX00_RX_BYTES_WRAP)
> +		valid |= BIT(ACX00_STAT_RX_BYTES);
> +	ret = valid;
> +
> +out_restore_page:
> +	ret = phy_restore_page(phydev, oldpage, ret);
> +	if (ret < 0)
> +		return ret;
> +
> +	for (i = 0; i < ACX00_NUM_STATS; i++) {
> +		u64 count;
> +
> +		if (!(ret & BIT(i)))
> +			continue;
> +		count = counts[i];
> +
> +		/* Include sleep and read time; unseen cycles cannot be recovered. */
> +		if ((priv->stats_valid & BIT(i)) &&
> +		    ktime_ms_delta(ktime_get_boottime(), priv->stats_start[i]) <
> +		    (s64)ACX00_STATS_MAX_INTERVAL_MS) {
> +			if (count < priv->stats_last[i])
> +				count += i == ACX00_STAT_RX_BYTES ?
> +					ACX00_RX_BYTES_WRAP : BIT_ULL(32);
> +			priv->stats[i] += count - priv->stats_last[i];

[Severity: Medium]
Can a single non-monotonic frame or CRC sample permanently add about 2^32
to rx_packets or rx_errors?

Only the byte counter has a plausibility check: samples at or above
ACX00_RX_BYTES_WRAP are skipped. For the CRC counter and the derived
packets-minus-errors value, any sample below stats_last[] counts as a
full 2^32 wrap, and the resulting delta has no upper bound.

An all-ones read is one way this can happen. Suppose the PHY stops
responding on an MDIO controller that returns 0xffff without an error.
acx00_ephy_read_counter() then sees high == check == 0xffff and returns
0xffffffff as a valid sample.

The CRC count reads 0xffffffff on both bracket reads, so packets - errors
is 0 and rx_packets gains 2^32 - N. The next good sample wraps again, and
both totals stay around 2^32 too high.

The other way is if the hardware updates the packet and CRC counters at
different times for the same bad frame. The bracket in
acx00_ephy_read_counts() could then see the frame in packets but not yet
in errors, or the reverse:

	if (errors == counts[ACX00_STAT_RX_CRC_ERRORS]) {
		/* Both hardware counters and their difference wrap at 2^32. */
		counts[ACX00_STAT_RX_PACKETS] = packets - errors;

On the next sample, the -1 would become a jump of 2^32 - 1. Is it known
that the two counters update together?

The commit message bounds the rate at "one frame per received byte". Under
that bound, any real frame delta within the 320 second window is below
4e9. Could frame and CRC deltas in [4e9, 2^32) be rejected, the same way
out-of-range byte samples are?

> +		}
> +		priv->stats_last[i] = counts[i];
> +		priv->stats_start[i] = start;
> +		priv->stats_valid |= BIT(i);
> +	}

[ ... ]

> @@ -350,6 +545,10 @@ static int acx00_ephy_soft_reset(struct phy_device *phydev)
>  
>  	mutex_lock(&priv->state_lock);
>  
> +	/* phylib may already have reset the counters before calling us. */
> +	priv->stats_active = false;
> +	priv->stats_valid = 0;
> +

[Severity: Low]
Does this invalidation happen early enough when phy_mii_ioctl() triggers
the reset?

For SIOCSMIIREG on MII_BMCR with BMCR_RESET, drivers/net/phy/phy.c does:

phy_mii_ioctl()
	mdiobus_write(phydev->mdio.bus, prtad, devad, val);

	if (prtad == phydev->mdio.addr &&
	    devad == MII_BMCR &&
	    val & BMCR_RESET)
		return phy_init_hw(phydev);

Neither call holds phydev->lock. _phy_state_machine() can therefore run
phy_update_stats()->acx00_ephy_update_stats() after the raw reset write
but before acx00_ephy_soft_reset() takes state_lock. stats_active and
stats_valid are still set at that point.

This comment assumes the reset clears the page 9 counters. If it does,
the new values are lower than stats_last[], and __acx00_ephy_update_stats()
treats them as a wrap:

	if (count < priv->stats_last[i])
		count += i == ACX00_STAT_RX_BYTES ?
			ACX00_RX_BYTES_WRAP : BIT_ULL(32);

The totals are kept across resets. Would rx_packets, rx_errors and
rx_bytes then stay inflated by about 2^32 or 4e9?

The window is narrow and needs CAP_NET_ADMIN.

[ ... ]

> @@ -475,10 +674,21 @@ static int __acx00_ephy_config_init(struct phy_device *phydev)

[ ... ]

> +	if (!priv->stats_active) {
> +		/* Establish a new baseline after reset, retaining the software total. */
> +		priv->stats_active = true;
> +		acx00_ephy_snapshot_stats(phydev);
> +	}
>  
> -	return genphy_restart_aneg(phydev);
> +	return 0;
>  }

[Severity: Low]
Should MDIO and page-restore errors from this snapshot be propagated?

acx00_ephy_snapshot_stats() drops every negative return from
__acx00_ephy_update_stats():

	ret = __acx00_ephy_update_stats(phydev);
	if (ret)
		phydev_dbg(phydev, "failed to snapshot statistics: %pe\n", ERR_PTR(ret));

That includes a phy_restore_page() failure after
phy_select_page(phydev, ACX00_PAGE_9).

__acx00_ephy_config_init() then returns 0, so acx00_ephy_config_init()
skips its power-off path. phy_init_hw() and resume report success while
the page register may still be on page 9. Later phy_select_page() calls
would then save page 9 as oldpage and restore back to it.

Every other paged access in config_init propagates the phy_restore_page()
result. The commit message also says:

    Real MDIO errors, including page-restoration failures, still propagate.

That holds for the .update_stats callback, but not for this snapshot.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116-submit-acx00-phy-stats-v1-v6-0-8feef8e76cd5%40gmail.com

      reply	other threads:[~2026-11-19 12:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16  8:59 [PATCH net-next v6 0/3] net: phy: report ACx00 receive statistics James Hilliard
2026-11-16  8:59 ` [PATCH net-next v6 1/3] docs: networking: correct the PHY statistics group name James Hilliard
2026-11-16  8:59 ` [PATCH net-next v6 2/3] ethtool: include errored frames in PHY receive byte counts James Hilliard
2026-11-16 13:12   ` Oleksij Rempel
2026-11-16 13:23     ` Andrew Lunn
2026-11-17  2:35     ` James Hilliard
2026-11-19 12:00   ` netdev-bot+sashiko
2026-11-16  8:59 ` [PATCH net-next v6 3/3] net: phy: acx00: report receive statistics James Hilliard
2026-11-19 12:00   ` netdev-bot+sashiko [this message]

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=179508961966.3136.18071124735934068397@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=andrew@lunn.ch \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=hkallweit1@gmail.com \
    --cc=horms@kernel.org \
    --cc=james.hilliard1@gmail.com \
    --cc=kuba@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=netdev@vger.kernel.org \
    --cc=o.rempel@pengutronix.de \
    --cc=pabeni@redhat.com \
    --cc=rdunlap@infradead.org \
    --cc=skhan@linuxfoundation.org \
    /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