diff mbox series

[net,v2,1/4] net: wangxun: fix the incorrect display of queue number in statistics

Message ID 20240429102519.25096-2-jiawenwu@trustnetic.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series Wangxun fixes | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 926 this patch: 926
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 6 of 6 maintainers
netdev/build_clang success Errors and warnings before: 937 this patch: 937
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 937 this patch: 937
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 64 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-04-29--15-00 (tests: 994)

Commit Message

Jiawen Wu April 29, 2024, 10:25 a.m. UTC
When using ethtool -S to print hardware statistics, the number of
Rx/Tx queues printed is greater than the number of queues actually
used.

Fixes: 46b92e10d631 ("net: libwx: support hardware statistics")
Signed-off-by: Jiawen Wu <jiawenwu@trustnetic.com>
---
 .../net/ethernet/wangxun/libwx/wx_ethtool.c   | 23 +++++++++----------
 1 file changed, 11 insertions(+), 12 deletions(-)

Comments

Jakub Kicinski May 1, 2024, 1:59 a.m. UTC | #1
On Mon, 29 Apr 2024 18:25:16 +0800 Jiawen Wu wrote:
> When using ethtool -S to print hardware statistics, the number of
> Rx/Tx queues printed is greater than the number of queues actually
> used.

The ethtool API fetches the number of stats and the values in an
unsafe, non-atomic way. If someone increases the number of queues
while someone else is fetching the stats the memory of the latter
process will get corrupted. The code is correct as is.
Jiawen Wu May 9, 2024, 2:39 a.m. UTC | #2
On Wed, May 1, 2024 10:00 AM, Jakub Kicinski wrote:
> On Mon, 29 Apr 2024 18:25:16 +0800 Jiawen Wu wrote:
> > When using ethtool -S to print hardware statistics, the number of
> > Rx/Tx queues printed is greater than the number of queues actually
> > used.
> 
> The ethtool API fetches the number of stats and the values in an
> unsafe, non-atomic way. If someone increases the number of queues
> while someone else is fetching the stats the memory of the latter
> process will get corrupted. The code is correct as is.

So should we keep the old code, showing stats with fixed maximum
number of queues?
Jakub Kicinski May 9, 2024, 3:19 p.m. UTC | #3
On Thu, 9 May 2024 10:39:22 +0800 Jiawen Wu wrote:
> > The ethtool API fetches the number of stats and the values in an
> > unsafe, non-atomic way. If someone increases the number of queues
> > while someone else is fetching the stats the memory of the latter
> > process will get corrupted. The code is correct as is.  
> 
> So should we keep the old code, showing stats with fixed maximum
> number of queues?

Yes.
diff mbox series

Patch

diff --git a/drivers/net/ethernet/wangxun/libwx/wx_ethtool.c b/drivers/net/ethernet/wangxun/libwx/wx_ethtool.c
index cc3bec42ed8e..803c7f1da9a4 100644
--- a/drivers/net/ethernet/wangxun/libwx/wx_ethtool.c
+++ b/drivers/net/ethernet/wangxun/libwx/wx_ethtool.c
@@ -59,9 +59,12 @@  static const struct wx_stats wx_gstrings_stats[] = {
 
 int wx_get_sset_count(struct net_device *netdev, int sset)
 {
+	struct wx *wx = netdev_priv(netdev);
+
 	switch (sset) {
 	case ETH_SS_STATS:
-		return WX_STATS_LEN;
+		return WX_STATS_LEN - (WX_NUM_RX_QUEUES - wx->num_tx_queues) *
+		       (sizeof(struct wx_queue_stats) / sizeof(u64)) * 2;
 	default:
 		return -EOPNOTSUPP;
 	}
@@ -70,6 +73,7 @@  EXPORT_SYMBOL(wx_get_sset_count);
 
 void wx_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
 {
+	struct wx *wx = netdev_priv(netdev);
 	u8 *p = data;
 	int i;
 
@@ -77,11 +81,11 @@  void wx_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
 	case ETH_SS_STATS:
 		for (i = 0; i < WX_GLOBAL_STATS_LEN; i++)
 			ethtool_puts(&p, wx_gstrings_stats[i].stat_string);
-		for (i = 0; i < netdev->num_tx_queues; i++) {
+		for (i = 0; i < wx->num_tx_queues; i++) {
 			ethtool_sprintf(&p, "tx_queue_%u_packets", i);
 			ethtool_sprintf(&p, "tx_queue_%u_bytes", i);
 		}
-		for (i = 0; i < WX_NUM_RX_QUEUES; i++) {
+		for (i = 0; i < wx->num_rx_queues; i++) {
 			ethtool_sprintf(&p, "rx_queue_%u_packets", i);
 			ethtool_sprintf(&p, "rx_queue_%u_bytes", i);
 		}
@@ -107,7 +111,7 @@  void wx_get_ethtool_stats(struct net_device *netdev,
 			   sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
 	}
 
-	for (j = 0; j < netdev->num_tx_queues; j++) {
+	for (j = 0; j < wx->num_tx_queues; j++) {
 		ring = wx->tx_ring[j];
 		if (!ring) {
 			data[i++] = 0;
@@ -122,7 +126,7 @@  void wx_get_ethtool_stats(struct net_device *netdev,
 		} while (u64_stats_fetch_retry(&ring->syncp, start));
 		i += 2;
 	}
-	for (j = 0; j < WX_NUM_RX_QUEUES; j++) {
+	for (j = 0; j < wx->num_rx_queues; j++) {
 		ring = wx->rx_ring[j];
 		if (!ring) {
 			data[i++] = 0;
@@ -177,13 +181,8 @@  void wx_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
 	strscpy(info->driver, wx->driver_name, sizeof(info->driver));
 	strscpy(info->fw_version, wx->eeprom_id, sizeof(info->fw_version));
 	strscpy(info->bus_info, pci_name(wx->pdev), sizeof(info->bus_info));
-	if (wx->num_tx_queues <= WX_NUM_TX_QUEUES) {
-		info->n_stats = WX_STATS_LEN -
-				   (WX_NUM_TX_QUEUES - wx->num_tx_queues) *
-				   (sizeof(struct wx_queue_stats) / sizeof(u64)) * 2;
-	} else {
-		info->n_stats = WX_STATS_LEN;
-	}
+	info->n_stats = WX_STATS_LEN - (WX_NUM_TX_QUEUES - wx->num_tx_queues) *
+			(sizeof(struct wx_queue_stats) / sizeof(u64)) * 2;
 }
 EXPORT_SYMBOL(wx_get_drvinfo);