diff mbox series

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

Message ID 20241101214828.289752-3-rosenp@gmail.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series net: ena: two cleanups | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 3 this patch: 3
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 10 of 10 maintainers
netdev/build_clang success Errors and warnings before: 3 this patch: 3
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 No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 4 this patch: 4
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 32 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-11-03--21-00 (tests: 781)

Commit Message

Rosen Penev Nov. 1, 2024, 9:48 p.m. UTC
Use ethtool_sprintf to simplify the code.

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

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/net/ethernet/amazon/ena/ena_ethtool.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

Comments

Shay Agroskin Nov. 4, 2024, 1:36 p.m. UTC | #1
Rosen Penev <rosenp@gmail.com> writes:

> Use ethtool_sprintf to simplify the code.
>
> Because strings_buf is separate from buf, it needs to be 
> incremented
> separately.
>
> Signed-off-by: Rosen Penev <rosenp@gmail.com>
> ---
>  drivers/net/ethernet/amazon/ena/ena_ethtool.c | 17 
>  ++++++++---------
>  1 file changed, 8 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/net/ethernet/amazon/ena/ena_ethtool.c 
> b/drivers/net/ethernet/amazon/ena/ena_ethtool.c
> index fa9d7b8ec00d..96fa55a88faf 100644
> --- a/drivers/net/ethernet/amazon/ena/ena_ethtool.c
> +++ b/drivers/net/ethernet/amazon/ena/ena_ethtool.c
> @@ -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);

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
diff mbox series

Patch

diff --git a/drivers/net/ethernet/amazon/ena/ena_ethtool.c b/drivers/net/ethernet/amazon/ena/ena_ethtool.c
index fa9d7b8ec00d..96fa55a88faf 100644
--- a/drivers/net/ethernet/amazon/ena/ena_ethtool.c
+++ b/drivers/net/ethernet/amazon/ena/ena_ethtool.c
@@ -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);