Skip to contentSkip to conversation

net: ena: two cleanups

PATCH In review Rosen Penev 3 participants 6 messages Nov 1, 2024 – Nov 5, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover net: ena: two cleanups

Simplify some code.

Rosen Penev (2):
net: ena: remove devm from ethtool
net: ena: simplify some pointer addition

 drivers/net/ethernet/amazon/ena/ena_ethtool.c | 31 ++++++++-----------
 1 file changed, 13 insertions(+), 18 deletions(-)
Signature · 1 line
2.47.0
#1 lore raw

PATCH 1/2 net: ena: remove devm from ethtool

There's no need for devm bloat here. In addition, these are freed right before the function exits.

Also swapped kcalloc order for consistency.

---
 drivers/net/ethernet/amazon/ena/ena_ethtool.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
drivers/net/ethernet/amazon/ena/ena_ethtool.c +5 -9
index e1c622b40a27..fa9d7b8ec00d 100644
@@ -1128,22 +1128,18 @@ static void ena_dump_stats_ex(struct ena_adapter *adapter, u8 *buf)
 		return;
 	}
 
-	strings_buf = devm_kcalloc(&adapter->pdev->dev,
-				   ETH_GSTRING_LEN, strings_num,
-				   GFP_ATOMIC);
+	strings_buf = kcalloc(strings_num, ETH_GSTRING_LEN, GFP_ATOMIC);
 	if (!strings_buf) {
 		netif_err(adapter, drv, netdev,
 			  "Failed to allocate strings_buf\n");
 		return;
 	}
 
-	data_buf = devm_kcalloc(&adapter->pdev->dev,
-				strings_num, sizeof(u64),
-				GFP_ATOMIC);
+	data_buf = kcalloc(strings_num, sizeof(u64), GFP_ATOMIC);
 	if (!data_buf) {
 		netif_err(adapter, drv, netdev,
 			  "Failed to allocate data buf\n");
-		devm_kfree(&adapter->pdev->dev, strings_buf);
+		kfree(strings_buf);
 		return;
 	}
 
@@ -1165,8 +1161,8 @@ static void ena_dump_stats_ex(struct ena_adapter *adapter, u8 *buf)
 				  strings_buf + i * ETH_GSTRING_LEN,
 				  data_buf[i]);
 
-	devm_kfree(&adapter->pdev->dev, strings_buf);
-	devm_kfree(&adapter->pdev->dev, data_buf);
+	kfree(strings_buf);
+	kfree(data_buf);
 }
 
 void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf)
Signature · 1 line
2.47.0
#3 lore raw

PATCH 2/2 net: ena: simplify some pointer addition

In reply to Rosen Penev

Use ethtool_sprintf to simplify the code.

Because strings_buf is separate from buf, it needs to be incremented separately.

---
 drivers/net/ethernet/amazon/ena/ena_ethtool.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
drivers/net/ethernet/amazon/ena/ena_ethtool.c +8 -9
index fa9d7b8ec00d..96fa55a88faf 100644
@@ -1120,7 +1120,7 @@ static void ena_dump_stats_ex(struct ena_adapter *adapter, u8 *buf)
 	u8 *strings_buf;
 	u64 *data_buf;
 	int strings_num;
-	int i, rc;
+	int i;
 
 	strings_num = ena_get_sw_stats_count(adapter);
 	if (strings_num <= 0) {
@@ -1149,17 +1149,16 @@ static void ena_dump_stats_ex(struct ena_adapter *adapter, u8 *buf)
 	/* If there is a buffer, dump stats, otherwise print them to dmesg */
 	if (buf)
 		for (i = 0; i < strings_num; i++) {
-			rc = snprintf(buf, ETH_GSTRING_LEN + sizeof(u64),
-				      "%s %llu\n",
-				      strings_buf + i * ETH_GSTRING_LEN,
-				      data_buf[i]);
-			buf += rc;
+			ethtool_sprintf(&buf, "%s %llu\n", strings_buf,
+					data_buf[i]);
+			strings_buf += ETH_GSTRING_LEN;
 		}
 	else
-		for (i = 0; i < strings_num; i++)
+		for (i = 0; i < strings_num; i++) {
 			netif_err(adapter, drv, netdev, "%s: %llu\n",
-				  strings_buf + i * ETH_GSTRING_LEN,
-				  data_buf[i]);
+				  strings_buf, data_buf[i]);
+			strings_buf += ETH_GSTRING_LEN;
+		}
 
 	kfree(strings_buf);
 	kfree(data_buf);
Signature · 1 line
2.47.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH net-next 1/2] net: ena: remove devm from ethtool

In reply to Rosen Penev
Quoted 16 lines · Rosen Penev

There's no need for devm bloat here. In addition, these are freed right before the function exits.

Also swapped kcalloc order for consistency.

---
 drivers/net/ethernet/amazon/ena/ena_ethtool.c | 14 
 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
drivers/net/ethernet/amazon/ena/ena_ethtool.c +0 -0
index e1c622b40a27..fa9d7b8ec00d 100644

drivers/net/ethernet/amazon/ena/ena_ethtool.c static void ena_dump_stats_ex(struct · line 1129

Quoted 2 lines
drivers/net/ethernet/amazon/ena/ena_ethtool.c +0 -0
@@ -1128,22 +1128,18 @@ static void ena_dump_stats_ex(struct 
 ena_adapter *adapter, u8 *buf)
 		return;

lgtm. thanks you for submitting this patch

#4 lore raw

Re: [PATCH net-next 2/2] net: ena: simplify some pointer addition

In reply to Rosen Penev
Quoted 50 lines · Rosen Penev

Use ethtool_sprintf to simplify the code.

Because strings_buf is separate from buf, it needs to be incremented separately.

---
 drivers/net/ethernet/amazon/ena/ena_ethtool.c | 17 
 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
drivers/net/ethernet/amazon/ena/ena_ethtool.c +1 -1
index fa9d7b8ec00d..96fa55a88faf 100644
@@ -1120,7 +1120,7 @@ static void ena_dump_stats_ex(struct 
 ena_adapter *adapter, u8 *buf)
 	u8 *strings_buf;
 	u64 *data_buf;
 	int strings_num;
-	int i, rc;
+	int i;
 
 	strings_num = ena_get_sw_stats_count(adapter);
drivers/net/ethernet/amazon/ena/ena_ethtool.c +4 -7
 	if (strings_num <= 0) {
@@ -1149,17 +1149,16 @@ static void ena_dump_stats_ex(struct 
 ena_adapter *adapter, u8 *buf)
 	/* If there is a buffer, dump stats, otherwise print them 
 to dmesg */
 	if (buf)
 		for (i = 0; i < strings_num; i++) {
-			rc = snprintf(buf, ETH_GSTRING_LEN + 
 sizeof(u64),
-				      "%s %llu\n",
-				      strings_buf + i * 
 ETH_GSTRING_LEN,
-				      data_buf[i]);
-			buf += rc;
+			ethtool_sprintf(&buf, "%s %llu\n", 
 strings_buf,
+					data_buf[i]);
+			strings_buf += ETH_GSTRING_LEN;
 		}
 	else
-		for (i = 0; i < strings_num; i++)
+		for (i = 0; i < strings_num; i++) {
 			netif_err(adapter, drv, netdev, "%s: 
 %llu\n",
-				  strings_buf + i * 

ETH_GSTRING_LEN,

drivers/net/ethernet/amazon/ena/ena_ethtool.c static void ena_dump_stats_ex(struct ena_adapter *adapter, u8 *buf) · line 1164

Quoted 7 lines
drivers/net/ethernet/amazon/ena/ena_ethtool.c +3 -1
-				  data_buf[i]);
+				  strings_buf, data_buf[i]);
+			strings_buf += ETH_GSTRING_LEN;
+		}
 
 	kfree(strings_buf);
 	kfree(data_buf);

Thank you for submitting the patch. If I'm not mistaken, there are some bugs introduced here:

1. You update string_buf pointer itself, but later you pass the variable to kfree, this would likely end up freeing the wrong address (/causing a
kernel panic)
2. The ethtool_sprintf increases the buf pointer by ETH_GSTRING_LEN, while the previous code increased it by (ETH_GSTRING_LEN + sizeof(u64)) bytes.
This causes a corruption in the buffer.

This function and mechanism is already planned for an overhaul in a future patch. We prefer to leave this patch out.

Thanks, Shay

#5 lore raw
In reply to Rosen Penev
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 8 lines · On Fri, 1 Nov 2024 14:48:26 -0700 you wrote:

Simplify some code.

Rosen Penev (2):
net: ena: remove devm from ethtool
net: ena: simplify some pointer addition

 drivers/net/ethernet/amazon/ena/ena_ethtool.c | 31 ++++++++-----------
 1 file changed, 13 insertions(+), 18 deletions(-)

Here is the summary with links:
- [net-next,1/2] net: ena: remove devm from ethtool

    https://git.kernel.org/netdev/net-next/c/d2068805f688

- [net-next,2/2] net: ena: simplify some pointer addition (no matching commit)

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html