[PATCH 0/3] ethtool: Add ethtool_puts()

2023-10-25 Thread Justin Stitt
Hi,

This series aims to implement ethtool_puts() and send out a wave 1 of
conversions from ethtool_sprintf(). There's also a checkpatch patch
included to check for the cases listed below.

This was sparked from recent discussion here [1]

The conversions are used in cases where ethtool_sprintf() was being used
with just two arguments:
|   ethtool_sprintf(&data, buffer[i].name);
or when it's used with format string: "%s"
|   ethtool_sprintf(&data, "%s", buffer[i].name);
which both now become:
|   ethtool_puts(&data, buffer[i].name);

The first case commonly triggers a -Wformat-security warning with Clang
due to potential problems with format flags present in the strings [3].

The second is just a bit weird with a plain-ol' "%s".

Note that I have some outstanding patches [2] (some picked up) that use
the second case of ethtool_sprintf(). I went with this approach to clean
up some strncpy() uses and avoid -Wformat-security warnings before
discussion about implementing ...puts() arose. I will probably let the
ones that have been picked up land but send new versions for others.

Wave 1 changes found with Cocci [4] and grep [5].

[1]: https://lore.kernel.org/all/202310141935.B326C9E@keescook/
[2]: https://lore.kernel.org/all/?q=dfb%3Aethtool_sprintf+AND+f%3Ajustinstitt
[3]: https://lore.kernel.org/all/202310101528.9496539BE@keescook/
[4]: (script authored by Kees)
@replace_2_args@
identifier BUF;
expression VAR;
@@

-   ethtool_sprintf
+   ethtool_puts
(&BUF, VAR)

@replace_3_args@
identifier BUF;
expression VAR;
@@

-   ethtool_sprintf(&BUF, "%s", VAR)
+   ethtool_puts(&BUF, VAR)
[5]: $ rg "ethtool_sprintf\(\s*[^,)]+\s*,\s*[^,)]+\s*\)"

Signed-off-by: Justin Stitt 
---
Justin Stitt (3):
  ethtool: Implement ethtool_puts()
  treewide: Convert some ethtool_sprintf() to ethtool_puts()
  checkpatch: add ethtool_sprintf rules

 drivers/net/ethernet/amazon/ena/ena_ethtool.c  |  4 +-
 drivers/net/ethernet/brocade/bna/bnad_ethtool.c|  2 +-
 .../net/ethernet/fungible/funeth/funeth_ethtool.c  |  8 +--
 drivers/net/ethernet/hisilicon/hns/hns_dsaf_gmac.c |  2 +-
 .../net/ethernet/hisilicon/hns/hns_dsaf_xgmac.c|  2 +-
 drivers/net/ethernet/hisilicon/hns/hns_ethtool.c   | 66 +++---
 drivers/net/ethernet/intel/i40e/i40e_ethtool.c |  4 +-
 drivers/net/ethernet/intel/ice/ice_ethtool.c   | 10 ++--
 drivers/net/ethernet/intel/igb/igb_ethtool.c   |  6 +-
 drivers/net/ethernet/intel/igc/igc_ethtool.c   |  6 +-
 drivers/net/ethernet/intel/ixgbe/ixgbe_ethtool.c   |  5 +-
 .../net/ethernet/netronome/nfp/nfp_net_ethtool.c   | 44 +++
 drivers/net/ethernet/pensando/ionic/ionic_stats.c  |  4 +-
 drivers/net/hyperv/netvsc_drv.c|  4 +-
 drivers/net/vmxnet3/vmxnet3_ethtool.c  | 10 ++--
 include/linux/ethtool.h| 13 +
 net/ethtool/ioctl.c|  7 +++
 scripts/checkpatch.pl  | 13 +
 18 files changed, 120 insertions(+), 90 deletions(-)
---
base-commit: d88520ad73b79e71e3ddf08de335b8520ae41c5c
change-id: 20231025-ethtool_puts_impl-a1479ffbc7e0

Best regards,
--
Justin Stitt 




[PATCH 1/3] ethtool: Implement ethtool_puts()

2023-10-25 Thread Justin Stitt
Use strscpy() to implement ethtool_puts().

Functionally the same as ethtool_sprintf() when it's used with two
arguments or with just "%s" format specifier.

Signed-off-by: Justin Stitt 
---
 include/linux/ethtool.h | 13 +
 net/ethtool/ioctl.c |  7 +++
 2 files changed, 20 insertions(+)

diff --git a/include/linux/ethtool.h b/include/linux/ethtool.h
index 62b61527bcc4..fdd65050bf1b 100644
--- a/include/linux/ethtool.h
+++ b/include/linux/ethtool.h
@@ -1052,4 +1052,17 @@ static inline int ethtool_mm_frag_size_min_to_add(u32 
val_min, u32 *val_add,
  * next string.
  */
 extern __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...);
+
+/**
+ * ethtool_puts - Write string to ethtool string data
+ * @data: Pointer to start of string to update
+ * @str: String to write
+ *
+ * Write string to data. Update data to point at start of next
+ * string.
+ *
+ * Prefer this function to ethtool_sprintf() when given only
+ * two arguments or if @fmt is just "%s".
+ */
+extern void ethtool_puts(u8 **data, const char *str);
 #endif /* _LINUX_ETHTOOL_H */
diff --git a/net/ethtool/ioctl.c b/net/ethtool/ioctl.c
index 0b0ce4f81c01..abdf05edf804 100644
--- a/net/ethtool/ioctl.c
+++ b/net/ethtool/ioctl.c
@@ -1991,6 +1991,13 @@ __printf(2, 3) void ethtool_sprintf(u8 **data, const 
char *fmt, ...)
 }
 EXPORT_SYMBOL(ethtool_sprintf);
 
+void ethtool_puts(u8 **data, const char *str)
+{
+   strscpy(*data, str, ETH_GSTRING_LEN);
+   *data += ETH_GSTRING_LEN;
+}
+EXPORT_SYMBOL(ethtool_puts);
+
 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
 {
struct ethtool_value id;

-- 
2.42.0.758.gaed0368e0e-goog




[PATCH 2/3] treewide: Convert some ethtool_sprintf() to ethtool_puts()

2023-10-25 Thread Justin Stitt
This patch converts some basic cases of ethtool_sprintf() to
ethtool_puts().

The conversions are used in cases where ethtool_sprintf() was being used
with just two arguments:
|   ethtool_sprintf(&data, buffer[i].name);
or when it's used with format string: "%s"
|   ethtool_sprintf(&data, "%s", buffer[i].name);
which both now become:
|   ethtool_puts(&data, buffer[i].name);

There are some outstanding patches [1] that I've sent using plain "%s"
with ethtool_sprintf() that should be ethtool_puts() now. Some have been
picked up as-is but I will send new versions for the others.

[1]: https://lore.kernel.org/all/?q=dfb%3Aethtool_sprintf+AND+f%3Ajustinstitt

Signed-off-by: Justin Stitt 
---
 drivers/net/ethernet/amazon/ena/ena_ethtool.c  |  4 +-
 drivers/net/ethernet/brocade/bna/bnad_ethtool.c|  2 +-
 .../net/ethernet/fungible/funeth/funeth_ethtool.c  |  8 +--
 drivers/net/ethernet/hisilicon/hns/hns_dsaf_gmac.c |  2 +-
 .../net/ethernet/hisilicon/hns/hns_dsaf_xgmac.c|  2 +-
 drivers/net/ethernet/hisilicon/hns/hns_ethtool.c   | 66 +++---
 drivers/net/ethernet/intel/i40e/i40e_ethtool.c |  4 +-
 drivers/net/ethernet/intel/ice/ice_ethtool.c   | 10 ++--
 drivers/net/ethernet/intel/igb/igb_ethtool.c   |  6 +-
 drivers/net/ethernet/intel/igc/igc_ethtool.c   |  6 +-
 drivers/net/ethernet/intel/ixgbe/ixgbe_ethtool.c   |  5 +-
 .../net/ethernet/netronome/nfp/nfp_net_ethtool.c   | 44 +++
 drivers/net/ethernet/pensando/ionic/ionic_stats.c  |  4 +-
 drivers/net/hyperv/netvsc_drv.c|  4 +-
 drivers/net/vmxnet3/vmxnet3_ethtool.c  | 10 ++--
 15 files changed, 87 insertions(+), 90 deletions(-)

diff --git a/drivers/net/ethernet/amazon/ena/ena_ethtool.c 
b/drivers/net/ethernet/amazon/ena/ena_ethtool.c
index d671df4b76bc..e3ef081aa42b 100644
--- a/drivers/net/ethernet/amazon/ena/ena_ethtool.c
+++ b/drivers/net/ethernet/amazon/ena/ena_ethtool.c
@@ -299,13 +299,13 @@ static void ena_get_strings(struct ena_adapter *adapter,
 
for (i = 0; i < ENA_STATS_ARRAY_GLOBAL; i++) {
ena_stats = &ena_stats_global_strings[i];
-   ethtool_sprintf(&data, ena_stats->name);
+   ethtool_puts(&data, ena_stats->name);
}
 
if (eni_stats_needed) {
for (i = 0; i < ENA_STATS_ARRAY_ENI(adapter); i++) {
ena_stats = &ena_stats_eni_strings[i];
-   ethtool_sprintf(&data, ena_stats->name);
+   ethtool_puts(&data, ena_stats->name);
}
}
 
diff --git a/drivers/net/ethernet/brocade/bna/bnad_ethtool.c 
b/drivers/net/ethernet/brocade/bna/bnad_ethtool.c
index df10edff5603..d1ad6c9f8140 100644
--- a/drivers/net/ethernet/brocade/bna/bnad_ethtool.c
+++ b/drivers/net/ethernet/brocade/bna/bnad_ethtool.c
@@ -608,7 +608,7 @@ bnad_get_strings(struct net_device *netdev, u32 stringset, 
u8 *string)
 
for (i = 0; i < BNAD_ETHTOOL_STATS_NUM; i++) {
BUG_ON(!(strlen(bnad_net_stats_strings[i]) < ETH_GSTRING_LEN));
-   ethtool_sprintf(&string, bnad_net_stats_strings[i]);
+   ethtool_puts(&string, bnad_net_stats_strings[i]);
}
 
bmap = bna_tx_rid_mask(&bnad->bna);
diff --git a/drivers/net/ethernet/fungible/funeth/funeth_ethtool.c 
b/drivers/net/ethernet/fungible/funeth/funeth_ethtool.c
index 31aa185f4d17..091c93bd7587 100644
--- a/drivers/net/ethernet/fungible/funeth/funeth_ethtool.c
+++ b/drivers/net/ethernet/fungible/funeth/funeth_ethtool.c
@@ -655,7 +655,7 @@ static void fun_get_strings(struct net_device *netdev, u32 
sset, u8 *data)
i);
}
for (j = 0; j < ARRAY_SIZE(txq_stat_names); j++)
-   ethtool_sprintf(&p, txq_stat_names[j]);
+   ethtool_puts(&p, txq_stat_names[j]);
 
for (i = 0; i < fp->num_xdpqs; i++) {
for (j = 0; j < ARRAY_SIZE(xdpq_stat_names); j++)
@@ -663,7 +663,7 @@ static void fun_get_strings(struct net_device *netdev, u32 
sset, u8 *data)
xdpq_stat_names[j], i);
}
for (j = 0; j < ARRAY_SIZE(xdpq_stat_names); j++)
-   ethtool_sprintf(&p, xdpq_stat_names[j]);
+   ethtool_puts(&p, xdpq_stat_names[j]);
 
for (i = 0; i < netdev->real_num_rx_queues; i++) {
for (j = 0; j < ARRAY_SIZE(rxq_stat_names); j++)
@@ -671,10 +671,10 @@ static void fun_get_strings(struct net_device *netdev, 
u32 sset, u8 *data)
i);
}
for (j = 0; j < ARRAY_SIZE(rxq_stat_names); j++)
-   ethtoo

[PATCH 3/3] checkpatch: add ethtool_sprintf rules

2023-10-25 Thread Justin Stitt
Add some warnings for using ethtool_sprintf() where a simple
ethtool_puts() would suffice.

The two cases are:

1) Use ethtool_sprintf() with just two arguments:
|   ethtool_sprintf(&data, driver[i].name);
or
2) Use ethtool_sprintf() with a standalone "%s" fmt string:
|   ethtool_sprintf(&data, "%s", driver[i].name);

The former may cause -Wformat-security warnings while the latter is just
not preferred. Both are safely in the category of warnings, not errors.

Signed-off-by: Justin Stitt 
---
 scripts/checkpatch.pl | 13 +
 1 file changed, 13 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 7d16f863edf1..1ba9ce778746 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -7020,6 +7020,19 @@ sub process {
 "Prefer strscpy, strscpy_pad, or __nonstring over 
strncpy - see: https://github.com/KSPP/linux/issues/90\n"; . $herecurr);
}
 
+# ethtool_sprintf uses that should likely be ethtool_puts
+   if (   $line =~ 
/\bethtool_sprintf\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/   ) {
+   WARN("ETHTOOL_SPRINTF",
+"Prefer ethtool_puts over ethtool_sprintf with 
only two arguments" . $herecurr);
+   }
+
+   # use $rawline because $line loses %s via sanitization and thus 
we can't match against it.
+   if (   $rawline =~ 
/\bethtool_sprintf\s*\(\s*$FuncArg\s*,\s*\"\%s\"\s*,\s*$FuncArg\s*\)/   ) {
+   WARN("ETHTOOL_SPRINTF2",
+"Prefer ethtool_puts over ethtool_sprintf with 
standalone \"%s\" specifier" . $herecurr);
+   }
+
+
 # typecasts on min/max could be min_t/max_t
if ($perl_version_ok &&
defined $stat &&

-- 
2.42.0.758.gaed0368e0e-goog




Re: [PATCH 2/3] treewide: Convert some ethtool_sprintf() to ethtool_puts()

2023-10-25 Thread Justin Stitt
On Wed, Oct 25, 2023 at 4:51 PM Joe Perches  wrote:
>
> On Wed, 2023-10-25 at 23:40 +, Justin Stitt wrote:
> > This patch converts some basic cases of ethtool_sprintf() to
> > ethtool_puts().
> >
> > The conversions are used in cases where ethtool_sprintf() was being used
> > with just two arguments:
> > >   ethtool_sprintf(&data, buffer[i].name);
>
> OK.
>
> > or when it's used with format string: "%s"
> > >   ethtool_sprintf(&data, "%s", buffer[i].name);
> > > which both now become:
> > >   ethtool_puts(&data, buffer[i].name);
>
> Why do you want this conversion?
> Is it not possible for .name to contain a formatting field?

The case of using just two arguments to a ethtool_sprintf
call may cause -Wformat-security warnings. If it did indeed
have format specifiers then we would have more format
specifiers than arguments. Not ideal.

The second case of having a standalone "%s" isn't
necessarily bad or wrong. I used this exact approach to
replace some strncpy() usage in net drivers [1].

I'm working off guidance from Andrew Lunn [2] and Kees
who said it may be a good idea to tidy this up with a puts().

All in all, this patch doesn't do much but fix some warnings
and provide a more obvious interface. The number of
actual replacements are relatively low (around 20ish) so
I was hoping to sneak them in via this series.

>

[1]: https://lore.kernel.org/all/?q=dfb%3Aethtool_sprintf+AND+f%3Ajustinstitt
[2]: https://lore.kernel.org/all/a958d35e-98b6-4a95-b505-776482d11...@lunn.ch/

Thanks
Justin



[PATCH next v2 0/3] ethtool: Add ethtool_puts()

2023-10-26 Thread Justin Stitt
Hi,

This series aims to implement ethtool_puts() and send out a wave 1 of
conversions from ethtool_sprintf(). There's also a checkpatch patch
included to check for the cases listed below.

This was sparked from recent discussion here [1]

The conversions are used in cases where ethtool_sprintf() was being used
with just two arguments:
|   ethtool_sprintf(&data, buffer[i].name);
or when it's used with format string: "%s"
|   ethtool_sprintf(&data, "%s", buffer[i].name);
which both now become:
|   ethtool_puts(&data, buffer[i].name);

The first case commonly triggers a -Wformat-security warning with Clang
due to potential problems with format flags present in the strings [3].

The second is just a bit weird with a plain-ol' "%s".

v2 (and newer) of this patch is targeted at linux-next so that we can
catch some of the patches I sent [2] using this "%s" pattern and replace
them before they hit mainline.

Changes found with Cocci [4] and grep [5].

[1]: https://lore.kernel.org/all/202310141935.B326C9E@keescook/
[2]: https://lore.kernel.org/all/?q=dfb%3Aethtool_sprintf+AND+f%3Ajustinstitt
[3]: https://lore.kernel.org/all/202310101528.9496539BE@keescook/
[4]: (script authored by Kees w/ modifications from Joe)
@replace_2_args@
expression BUF;
expression VAR;
@@

-   ethtool_sprintf(BUF, VAR)
+   ethtool_puts(BUF, VAR)

@replace_3_args@
expression BUF;
expression VAR;
@@

-   ethtool_sprintf(BUF, "%s", VAR)
+   ethtool_puts(BUF, VAR)

-   ethtool_sprintf(&BUF, "%s", VAR)
+   ethtool_puts(&BUF, VAR)

[5]: $ rg "ethtool_sprintf\(\s*[^,)]+\s*,\s*[^,)]+\s*\)"

Signed-off-by: Justin Stitt 
---
Changes in v2:
- wrap lines better in replacement (thanks Joe, Kees)
- add --fix to checkpatch (thanks Joe)
- clean up checkpatch formatting (thanks Joe, et al.)
- rebase against next
- Link to v1: 
https://lore.kernel.org/r/20231025-ethtool_puts_impl-v1-0-6a53a93d3...@google.com

---
Justin Stitt (3):
  ethtool: Implement ethtool_puts()
  checkpatch: add ethtool_sprintf rules
  treewide: Convert some ethtool_sprintf() to ethtool_puts()

 drivers/net/dsa/lantiq_gswip.c |  2 +-
 drivers/net/dsa/mt7530.c   |  2 +-
 drivers/net/dsa/qca/qca8k-common.c |  2 +-
 drivers/net/dsa/realtek/rtl8365mb.c|  2 +-
 drivers/net/dsa/realtek/rtl8366-core.c |  2 +-
 drivers/net/dsa/vitesse-vsc73xx-core.c |  8 +--
 drivers/net/ethernet/amazon/ena/ena_ethtool.c  |  4 +-
 drivers/net/ethernet/brocade/bna/bnad_ethtool.c|  2 +-
 drivers/net/ethernet/freescale/fec_main.c  |  4 +-
 .../net/ethernet/fungible/funeth/funeth_ethtool.c  |  8 +--
 drivers/net/ethernet/hisilicon/hns/hns_dsaf_gmac.c |  2 +-
 .../net/ethernet/hisilicon/hns/hns_dsaf_xgmac.c|  2 +-
 drivers/net/ethernet/hisilicon/hns/hns_ethtool.c   | 65 +++---
 drivers/net/ethernet/intel/i40e/i40e_ethtool.c |  6 +-
 drivers/net/ethernet/intel/iavf/iavf_ethtool.c |  3 +-
 drivers/net/ethernet/intel/ice/ice_ethtool.c   |  9 +--
 drivers/net/ethernet/intel/idpf/idpf_ethtool.c |  2 +-
 drivers/net/ethernet/intel/igb/igb_ethtool.c   |  6 +-
 drivers/net/ethernet/intel/igc/igc_ethtool.c   |  6 +-
 drivers/net/ethernet/intel/ixgbe/ixgbe_ethtool.c   |  5 +-
 .../net/ethernet/microchip/sparx5/sparx5_ethtool.c |  2 +-
 .../net/ethernet/netronome/nfp/nfp_net_ethtool.c   | 44 +++
 drivers/net/ethernet/pensando/ionic/ionic_stats.c  |  4 +-
 drivers/net/ethernet/wangxun/libwx/wx_ethtool.c|  2 +-
 drivers/net/hyperv/netvsc_drv.c|  4 +-
 drivers/net/phy/nxp-tja11xx.c  |  2 +-
 drivers/net/phy/smsc.c |  2 +-
 drivers/net/vmxnet3/vmxnet3_ethtool.c  | 10 ++--
 include/linux/ethtool.h| 34 +++
 net/ethtool/ioctl.c|  7 +++
 scripts/checkpatch.pl  | 19 +++
 31 files changed, 149 insertions(+), 123 deletions(-)
---
base-commit: 2ef7141596eed0b4b45ef18b3626f428a6b0a822
change-id: 20231025-ethtool_puts_impl-a1479ffbc7e0

Best regards,
--
Justin Stitt 




[PATCH next v2 2/3] checkpatch: add ethtool_sprintf rules

2023-10-26 Thread Justin Stitt
Add some warnings for using ethtool_sprintf() where a simple
ethtool_puts() would suffice.

The two cases are:

1) Use ethtool_sprintf() with just two arguments:
|   ethtool_sprintf(&data, driver[i].name);
or
2) Use ethtool_sprintf() with a standalone "%s" fmt string:
|   ethtool_sprintf(&data, "%s", driver[i].name);

The former may cause -Wformat-security warnings while the latter is just
not preferred. Both are safely in the category of warnings, not errors.

Signed-off-by: Justin Stitt 
---
 scripts/checkpatch.pl | 19 +++
 1 file changed, 19 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 25fdb7fda112..22f007131337 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -7011,6 +7011,25 @@ sub process {
 "Prefer strscpy, strscpy_pad, or __nonstring over 
strncpy - see: https://github.com/KSPP/linux/issues/90\n"; . $herecurr);
}
 
+# ethtool_sprintf uses that should likely be ethtool_puts
+   if ($line =~ 
/\bethtool_sprintf\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
+   if(WARN("ETHTOOL_SPRINTF",
+  "Prefer ethtool_puts over ethtool_sprintf with only 
two arguments\n" . $herecurr) &&
+ $fix) {
+ $fixed[$fixlinenr] =~ s/ethtool_sprintf\s*\(/ethtool_puts\(/;
+   }
+   }
+
+   # use $rawline because $line loses %s via sanitization and thus 
we can't match against it.
+   if ($rawline =~ 
/\bethtool_sprintf\s*\(\s*$FuncArg\s*,\s*\"\%s\"\s*,\s*$FuncArg\s*\)/) {
+   if(WARN("ETHTOOL_SPRINTF",
+  "Prefer ethtool_puts over ethtool_sprintf with 
standalone \"%s\" specifier\n" . $herecurr) &&
+ $fix) {
+ $fixed[$fixlinenr] =~ 
s/ethtool_sprintf\s*\(\s*(.*?),.*?,(.*?)\)/ethtool_puts\($1,$2)/;
+   }
+   }
+
+
 # typecasts on min/max could be min_t/max_t
if ($perl_version_ok &&
defined $stat &&

-- 
2.42.0.820.g83a721a137-goog




[PATCH next v2 1/3] ethtool: Implement ethtool_puts()

2023-10-26 Thread Justin Stitt
Use strscpy() to implement ethtool_puts().

Functionally the same as ethtool_sprintf() when it's used with two
arguments or with just "%s" format specifier.

Signed-off-by: Justin Stitt 
---
 include/linux/ethtool.h | 34 +++---
 net/ethtool/ioctl.c |  7 +++
 2 files changed, 30 insertions(+), 11 deletions(-)

diff --git a/include/linux/ethtool.h b/include/linux/ethtool.h
index 226a36ed5aa1..7129dd2e227c 100644
--- a/include/linux/ethtool.h
+++ b/include/linux/ethtool.h
@@ -1053,22 +1053,34 @@ static inline int ethtool_mm_frag_size_min_to_add(u32 
val_min, u32 *val_add,
  */
 extern __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...);
 
+/**
+ * ethtool_puts - Write string to ethtool string data
+ * @data: Pointer to start of string to update
+ * @str: String to write
+ *
+ * Write string to data. Update data to point at start of next
+ * string.
+ *
+ * Prefer this function to ethtool_sprintf() when given only
+ * two arguments or if @fmt is just "%s".
+ */
+extern void ethtool_puts(u8 **data, const char *str);
+
 /* Link mode to forced speed capabilities maps */
 struct ethtool_forced_speed_map {
-   u32 speed;
+   u32 speed;
__ETHTOOL_DECLARE_LINK_MODE_MASK(caps);
 
-   const u32   *cap_arr;
-   u32 arr_size;
+   const u32 *cap_arr;
+   u32 arr_size;
 };
 
-#define ETHTOOL_FORCED_SPEED_MAP(prefix, value)
\
-{  \
-   .speed  = SPEED_##value,\
-   .cap_arr= prefix##_##value, \
-   .arr_size   = ARRAY_SIZE(prefix##_##value), \
-}
+#define ETHTOOL_FORCED_SPEED_MAP(prefix, value)  \
+   {\
+   .speed = SPEED_##value, .cap_arr = prefix##_##value, \
+   .arr_size = ARRAY_SIZE(prefix##_##value),\
+   }
 
-void
-ethtool_forced_speed_maps_init(struct ethtool_forced_speed_map *maps, u32 
size);
+void ethtool_forced_speed_maps_init(struct ethtool_forced_speed_map *maps,
+   u32 size);
 #endif /* _LINUX_ETHTOOL_H */
diff --git a/net/ethtool/ioctl.c b/net/ethtool/ioctl.c
index 0b0ce4f81c01..abdf05edf804 100644
--- a/net/ethtool/ioctl.c
+++ b/net/ethtool/ioctl.c
@@ -1991,6 +1991,13 @@ __printf(2, 3) void ethtool_sprintf(u8 **data, const 
char *fmt, ...)
 }
 EXPORT_SYMBOL(ethtool_sprintf);
 
+void ethtool_puts(u8 **data, const char *str)
+{
+   strscpy(*data, str, ETH_GSTRING_LEN);
+   *data += ETH_GSTRING_LEN;
+}
+EXPORT_SYMBOL(ethtool_puts);
+
 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
 {
struct ethtool_value id;

-- 
2.42.0.820.g83a721a137-goog




[PATCH next v2 3/3] treewide: Convert some ethtool_sprintf() to ethtool_puts()

2023-10-26 Thread Justin Stitt
This patch converts some basic cases of ethtool_sprintf() to
ethtool_puts().

The conversions are used in cases where ethtool_sprintf() was being used
with just two arguments:
|   ethtool_sprintf(&data, buffer[i].name);
or when it's used with format string: "%s"
|   ethtool_sprintf(&data, "%s", buffer[i].name);
which both now become:
|   ethtool_puts(&data, buffer[i].name);

Signed-off-by: Justin Stitt 
---
 drivers/net/dsa/lantiq_gswip.c |  2 +-
 drivers/net/dsa/mt7530.c   |  2 +-
 drivers/net/dsa/qca/qca8k-common.c |  2 +-
 drivers/net/dsa/realtek/rtl8365mb.c|  2 +-
 drivers/net/dsa/realtek/rtl8366-core.c |  2 +-
 drivers/net/dsa/vitesse-vsc73xx-core.c |  8 +--
 drivers/net/ethernet/amazon/ena/ena_ethtool.c  |  4 +-
 drivers/net/ethernet/brocade/bna/bnad_ethtool.c|  2 +-
 drivers/net/ethernet/freescale/fec_main.c  |  4 +-
 .../net/ethernet/fungible/funeth/funeth_ethtool.c  |  8 +--
 drivers/net/ethernet/hisilicon/hns/hns_dsaf_gmac.c |  2 +-
 .../net/ethernet/hisilicon/hns/hns_dsaf_xgmac.c|  2 +-
 drivers/net/ethernet/hisilicon/hns/hns_ethtool.c   | 65 +++---
 drivers/net/ethernet/intel/i40e/i40e_ethtool.c |  6 +-
 drivers/net/ethernet/intel/iavf/iavf_ethtool.c |  3 +-
 drivers/net/ethernet/intel/ice/ice_ethtool.c   |  9 +--
 drivers/net/ethernet/intel/idpf/idpf_ethtool.c |  2 +-
 drivers/net/ethernet/intel/igb/igb_ethtool.c   |  6 +-
 drivers/net/ethernet/intel/igc/igc_ethtool.c   |  6 +-
 drivers/net/ethernet/intel/ixgbe/ixgbe_ethtool.c   |  5 +-
 .../net/ethernet/microchip/sparx5/sparx5_ethtool.c |  2 +-
 .../net/ethernet/netronome/nfp/nfp_net_ethtool.c   | 44 +++
 drivers/net/ethernet/pensando/ionic/ionic_stats.c  |  4 +-
 drivers/net/ethernet/wangxun/libwx/wx_ethtool.c|  2 +-
 drivers/net/hyperv/netvsc_drv.c|  4 +-
 drivers/net/phy/nxp-tja11xx.c  |  2 +-
 drivers/net/phy/smsc.c |  2 +-
 drivers/net/vmxnet3/vmxnet3_ethtool.c  | 10 ++--
 28 files changed, 100 insertions(+), 112 deletions(-)

diff --git a/drivers/net/dsa/lantiq_gswip.c b/drivers/net/dsa/lantiq_gswip.c
index 9c185c9f0963..05a017c9ef3d 100644
--- a/drivers/net/dsa/lantiq_gswip.c
+++ b/drivers/net/dsa/lantiq_gswip.c
@@ -1759,7 +1759,7 @@ static void gswip_get_strings(struct dsa_switch *ds, int 
port, u32 stringset,
return;
 
for (i = 0; i < ARRAY_SIZE(gswip_rmon_cnt); i++)
-   ethtool_sprintf(&data, "%s", gswip_rmon_cnt[i].name);
+   ethtool_puts(&data, gswip_rmon_cnt[i].name);
 }
 
 static u32 gswip_bcm_ram_entry_read(struct gswip_priv *priv, u32 table,
diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c
index d27c6b70a2f6..391c4dbdff42 100644
--- a/drivers/net/dsa/mt7530.c
+++ b/drivers/net/dsa/mt7530.c
@@ -836,7 +836,7 @@ mt7530_get_strings(struct dsa_switch *ds, int port, u32 
stringset,
return;
 
for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++)
-   ethtool_sprintf(&data, "%s", mt7530_mib[i].name);
+   ethtool_puts(&data, mt7530_mib[i].name);
 }
 
 static void
diff --git a/drivers/net/dsa/qca/qca8k-common.c 
b/drivers/net/dsa/qca/qca8k-common.c
index 9243eff8918d..2358cd399c7e 100644
--- a/drivers/net/dsa/qca/qca8k-common.c
+++ b/drivers/net/dsa/qca/qca8k-common.c
@@ -487,7 +487,7 @@ void qca8k_get_strings(struct dsa_switch *ds, int port, u32 
stringset,
return;
 
for (i = 0; i < priv->info->mib_count; i++)
-   ethtool_sprintf(&data, "%s", ar8327_mib[i].name);
+   ethtool_puts(&data, ar8327_mib[i].name);
 }
 
 void qca8k_get_ethtool_stats(struct dsa_switch *ds, int port,
diff --git a/drivers/net/dsa/realtek/rtl8365mb.c 
b/drivers/net/dsa/realtek/rtl8365mb.c
index 0875e4fc9f57..b072045eb154 100644
--- a/drivers/net/dsa/realtek/rtl8365mb.c
+++ b/drivers/net/dsa/realtek/rtl8365mb.c
@@ -1303,7 +1303,7 @@ static void rtl8365mb_get_strings(struct dsa_switch *ds, 
int port, u32 stringset
 
for (i = 0; i < RTL8365MB_MIB_END; i++) {
struct rtl8365mb_mib_counter *mib = &rtl8365mb_mib_counters[i];
-   ethtool_sprintf(&data, "%s", mib->name);
+   ethtool_puts(&data, mib->name);
}
 }
 
diff --git a/drivers/net/dsa/realtek/rtl8366-core.c 
b/drivers/net/dsa/realtek/rtl8366-core.c
index 82e267b8fddb..59f98d2c8769 100644
--- a/drivers/net/dsa/realtek/rtl8366-core.c
+++ b/drivers/net/dsa/realtek/rtl8366-core.c
@@ -401,7 +401,7 @@ void rtl8366_get_strings(struct dsa_switch *ds, int port, 
u32 stringset,
return;
 
for (i = 0; i < priv->num_mib_counters; i++)
-   ethtool_sprintf(&data, "%s", pri

Re: [PATCH next v2 1/3] ethtool: Implement ethtool_puts()

2023-10-26 Thread Justin Stitt
On Thu, Oct 26, 2023 at 3:02 PM Vladimir Oltean  wrote:
>
> Hi Justin,
>
> On Thu, Oct 26, 2023 at 09:56:07PM +0000, Justin Stitt wrote:
> > Use strscpy() to implement ethtool_puts().
> >
> > Functionally the same as ethtool_sprintf() when it's used with two
> > arguments or with just "%s" format specifier.
> >
> > Signed-off-by: Justin Stitt 
> > ---
> >  include/linux/ethtool.h | 34 +++---
> >  net/ethtool/ioctl.c |  7 +++
> >  2 files changed, 30 insertions(+), 11 deletions(-)
> >
> > diff --git a/include/linux/ethtool.h b/include/linux/ethtool.h
> > index 226a36ed5aa1..7129dd2e227c 100644
> > --- a/include/linux/ethtool.h
> > +++ b/include/linux/ethtool.h
> > @@ -1053,22 +1053,34 @@ static inline int 
> > ethtool_mm_frag_size_min_to_add(u32 val_min, u32 *val_add,
> >   */
> >  extern __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, 
> > ...);
> >
> > +/**
> > + * ethtool_puts - Write string to ethtool string data
> > + * @data: Pointer to start of string to update
> > + * @str: String to write
> > + *
> > + * Write string to data. Update data to point at start of next
> > + * string.
> > + *
> > + * Prefer this function to ethtool_sprintf() when given only
> > + * two arguments or if @fmt is just "%s".
> > + */
> > +extern void ethtool_puts(u8 **data, const char *str);
> > +
> >  /* Link mode to forced speed capabilities maps */
> >  struct ethtool_forced_speed_map {
> > - u32 speed;
> > + u32 speed;
> >   __ETHTOOL_DECLARE_LINK_MODE_MASK(caps);
> >
> > - const u32   *cap_arr;
> > - u32 arr_size;
> > + const u32 *cap_arr;
> > + u32 arr_size;
> >  };
> >
> > -#define ETHTOOL_FORCED_SPEED_MAP(prefix, value)
> >   \
> > -{\
> > - .speed  = SPEED_##value,\
> > - .cap_arr= prefix##_##value, \
> > - .arr_size   = ARRAY_SIZE(prefix##_##value), \
> > -}
> > +#define ETHTOOL_FORCED_SPEED_MAP(prefix, value)  \
> > + {\
> > + .speed = SPEED_##value, .cap_arr = prefix##_##value, \
> > + .arr_size = ARRAY_SIZE(prefix##_##value),\
> > + }
> >
> > -void
> > -ethtool_forced_speed_maps_init(struct ethtool_forced_speed_map *maps, u32 
> > size);
> > +void ethtool_forced_speed_maps_init(struct ethtool_forced_speed_map *maps,
> > + u32 size);
> >  #endif /* _LINUX_ETHTOOL_H */
>
> Maybe this is due to an incorrect rebase conflict resolution, but you
> shouldn't have touched any of the ethtool force speed maps.

Ah, I did have a conflict and resolved by simply moving the hunks
out of each other's way. Trivial resolution.

Should I undo this? I want my patch against next since it's targeting
some stuff in-flight over there. BUT, I also want ethtool_puts() to be
directly below ethtool_sprintf() in the source code. What to do?

>
> Please wait for at least 24 hours to pass before posting a new version,
> to allow for more comments to come in.

Ok :)

Thanks
Justin



Re: [PATCH next v2 1/3] ethtool: Implement ethtool_puts()

2023-10-26 Thread Justin Stitt
On Thu, Oct 26, 2023 at 3:09 PM Justin Stitt  wrote:
>
> On Thu, Oct 26, 2023 at 3:02 PM Vladimir Oltean  wrote:
> >
> > Hi Justin,
> >
> > On Thu, Oct 26, 2023 at 09:56:07PM +, Justin Stitt wrote:
> > > Use strscpy() to implement ethtool_puts().
> > >
> > > Functionally the same as ethtool_sprintf() when it's used with two
> > > arguments or with just "%s" format specifier.
> > >
> > > Signed-off-by: Justin Stitt 
> > > ---
> > >  include/linux/ethtool.h | 34 +++---
> > >  net/ethtool/ioctl.c |  7 +++
> > >  2 files changed, 30 insertions(+), 11 deletions(-)
> > >
> > > diff --git a/include/linux/ethtool.h b/include/linux/ethtool.h
> > > index 226a36ed5aa1..7129dd2e227c 100644
> > > --- a/include/linux/ethtool.h
> > > +++ b/include/linux/ethtool.h
> > > @@ -1053,22 +1053,34 @@ static inline int 
> > > ethtool_mm_frag_size_min_to_add(u32 val_min, u32 *val_add,
> > >   */
> > >  extern __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, 
> > > ...);
> > >
> > > +/**
> > > + * ethtool_puts - Write string to ethtool string data
> > > + * @data: Pointer to start of string to update
> > > + * @str: String to write
> > > + *
> > > + * Write string to data. Update data to point at start of next
> > > + * string.
> > > + *
> > > + * Prefer this function to ethtool_sprintf() when given only
> > > + * two arguments or if @fmt is just "%s".
> > > + */
> > > +extern void ethtool_puts(u8 **data, const char *str);
> > > +
> > >  /* Link mode to forced speed capabilities maps */
> > >  struct ethtool_forced_speed_map {
> > > - u32 speed;
> > > + u32 speed;
> > >   __ETHTOOL_DECLARE_LINK_MODE_MASK(caps);
> > >
> > > - const u32   *cap_arr;
> > > - u32 arr_size;
> > > + const u32 *cap_arr;
> > > + u32 arr_size;
> > >  };
> > >
> > > -#define ETHTOOL_FORCED_SPEED_MAP(prefix, value)  
> > > \
> > > -{\
> > > - .speed  = SPEED_##value,\
> > > - .cap_arr= prefix##_##value, \
> > > - .arr_size   = ARRAY_SIZE(prefix##_##value), \
> > > -}
> > > +#define ETHTOOL_FORCED_SPEED_MAP(prefix, value)  \
> > > + {\
> > > + .speed = SPEED_##value, .cap_arr = prefix##_##value, \
> > > + .arr_size = ARRAY_SIZE(prefix##_##value),\
> > > + }
> > >
> > > -void
> > > -ethtool_forced_speed_maps_init(struct ethtool_forced_speed_map *maps, 
> > > u32 size);
> > > +void ethtool_forced_speed_maps_init(struct ethtool_forced_speed_map 
> > > *maps,
> > > + u32 size);
> > >  #endif /* _LINUX_ETHTOOL_H */
> >
> > Maybe this is due to an incorrect rebase conflict resolution, but you
> > shouldn't have touched any of the ethtool force speed maps.
>
> Ah, I did have a conflict and resolved by simply moving the hunks
> out of each other's way. Trivial resolution.
>
> Should I undo this? I want my patch against next since it's targeting
> some stuff in-flight over there. BUT, I also want ethtool_puts() to be
> directly below ethtool_sprintf() in the source code. What to do?

Oh, I just realized my auto formatter had a field day with that function.
I will rectify this in a new version after waiting 24hrs for comments to
trickle in as well.

>
> >
> > Please wait for at least 24 hours to pass before posting a new version,
> > to allow for more comments to come in.
>
> Ok :)
>
> Thanks
> Justin

Thanks
Justin



Re: [PATCH next v2 2/3] checkpatch: add ethtool_sprintf rules

2023-10-26 Thread Justin Stitt
On Thu, Oct 26, 2023 at 3:12 PM Vladimir Oltean  wrote:
>
> On Thu, Oct 26, 2023 at 09:56:08PM +, Justin Stitt wrote:
> > Add some warnings for using ethtool_sprintf() where a simple
> > ethtool_puts() would suffice.
> >
> > The two cases are:
> >
> > 1) Use ethtool_sprintf() with just two arguments:
> > |   ethtool_sprintf(&data, driver[i].name);
> > or
> > 2) Use ethtool_sprintf() with a standalone "%s" fmt string:
> > |   ethtool_sprintf(&data, "%s", driver[i].name);
> >
> > The former may cause -Wformat-security warnings while the latter is just
> > not preferred. Both are safely in the category of warnings, not errors.
> >
> > Signed-off-by: Justin Stitt 
> > ---
> >  scripts/checkpatch.pl | 19 +++
> >  1 file changed, 19 insertions(+)
> >
> > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> > index 25fdb7fda112..22f007131337 100755
> > --- a/scripts/checkpatch.pl
> > +++ b/scripts/checkpatch.pl
> > @@ -7011,6 +7011,25 @@ sub process {
> >"Prefer strscpy, strscpy_pad, or __nonstring 
> > over strncpy - see: https://github.com/KSPP/linux/issues/90\n"; . $herecurr);
> >   }
> >
> > +# ethtool_sprintf uses that should likely be ethtool_puts
> > + if ($line =~ 
> > /\bethtool_sprintf\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
> > + if(WARN("ETHTOOL_SPRINTF",
> > +"Prefer ethtool_puts over ethtool_sprintf with 
> > only two arguments\n" . $herecurr) &&
> > + $fix) {
> > + $fixed[$fixlinenr] =~ s/ethtool_sprintf\s*\(/ethtool_puts\(/;
> > +   }
> > + }
> > +
> > + # use $rawline because $line loses %s via sanitization and 
> > thus we can't match against it.
> > + if ($rawline =~ 
> > /\bethtool_sprintf\s*\(\s*$FuncArg\s*,\s*\"\%s\"\s*,\s*$FuncArg\s*\)/) {
> > + if(WARN("ETHTOOL_SPRINTF",
> > +"Prefer ethtool_puts over ethtool_sprintf with 
> > standalone \"%s\" specifier\n" . $herecurr) &&
> > + $fix) {
> > + $fixed[$fixlinenr] =~ 
> > s/ethtool_sprintf\s*\(\s*(.*?),.*?,(.*?)\)/ethtool_puts\($1,$2)/;
> > +   }
> > + }
> > +
> > +
> >  # typecasts on min/max could be min_t/max_t
> >   if ($perl_version_ok &&
> >   defined $stat &&
> >
> > --
> > 2.42.0.820.g83a721a137-goog
> >
>
> I don't really know Perl, but does the indentation and coding style here
> conform to any rules, or is it just free-form? The rest of the script
> looks almost as you'd expect from C. This is unreadable to me.

There was some discussion here [1] but AFAICT I need to use EMACS
or configure my vim in a very particular way to get the same formatting

But yeah, look around line 7000 -- lots of this pattern matching code is
pretty hard to read. Not sure there's much to be done as far as readability
is concerned.

[1]: 
https://lore.kernel.org/all/137a309b313cc8a295f3affc704f0da049f233aa.ca...@perches.com/

Thanks
Justin



Re: [PATCH next v2 0/3] ethtool: Add ethtool_puts()

2023-10-27 Thread Justin Stitt
On Thu, Oct 26, 2023 at 5:25 PM Andrew Lunn  wrote:
>
> > Changes in v2:
> > - wrap lines better in replacement (thanks Joe, Kees)
> > - add --fix to checkpatch (thanks Joe)
> > - clean up checkpatch formatting (thanks Joe, et al.)
> > - rebase against next
>
> Please could you explain the rebase against next? As Vladimir pointed
> out, all the patches are to drivers/net, so anything in flight should
> be in net-next, merged by the netdev Maintainers.

OK, should v3 be against net-next? I opted for next as a catch-all.

>
> Andrew

Thanks
Justin



Re: [PATCH next v2 1/3] ethtool: Implement ethtool_puts()

2023-10-27 Thread Justin Stitt
On Thu, Oct 26, 2023 at 3:25 PM Vladimir Oltean  wrote:
>
> On Thu, Oct 26, 2023 at 03:09:59PM -0700, Justin Stitt wrote:
> > Should I undo this? I want my patch against next since it's targeting
> > some stuff in-flight over there. BUT, I also want ethtool_puts() to be
> > directly below ethtool_sprintf() in the source code. What to do?
>
> (removing everyone except the lists from CC, I don't want to go to email
> arest because of spamming too many recipients)
>
> What is the stuff in-flight in next that this is targeting?
>
> And why would anything prevent you from putting ethtool_puts() directly
> below ethtool_sprintf()?

The in-flight stuff consists of patches I sent changing some strncpy() usage
to

ethtool_sprintf(&data, "%s", something[i].name);

We can see them here [1]. I went for this approach initially but then
discussion came up about introducing ethtool_puts() which now
made my patches (some accepted into next already) semi-outdated
and in need of another swap from sprintf->puts() -- hence this series.

As far as the rebase, I simply took my commits and placed them on
top of next/master and got merge conflicts when ethtool_puts()
was placed below ethtool_sprintf(). All I have to do is move the hunks
around but since I formatted the file it's appearing in the diff. v3 will
be a clean diff.


[1]: https://lore.kernel.org/all/?q=dfb:ethtool_sprintf%20AND%20f:justinstitt

Thanks
Justin



Re: [PATCH next v2 2/3] checkpatch: add ethtool_sprintf rules

2023-10-27 Thread Justin Stitt
On Thu, Oct 26, 2023 at 3:39 PM Joe Perches  wrote:
>
> On Thu, 2023-10-26 at 21:56 +, Justin Stitt wrote:
> > Add some warnings for using ethtool_sprintf() where a simple
> > ethtool_puts() would suffice.
> []
> > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> []
> > @@ -7011,6 +7011,25 @@ sub process {
> >"Prefer strscpy, strscpy_pad, or __nonstring 
> > over strncpy - see: https://github.com/KSPP/linux/issues/90\n"; . $herecurr);
> >   }
> >
> > +# ethtool_sprintf uses that should likely be ethtool_puts
> > + if ($line =~ 
> > /\bethtool_sprintf\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
> > + if(WARN("ETHTOOL_SPRINTF",
> > +"Prefer ethtool_puts over ethtool_sprintf with 
> > only two arguments\n" . $herecurr) &&
> > + $fix) {
> > + $fixed[$fixlinenr] =~ s/ethtool_sprintf\s*\(/ethtool_puts\(/;
> > +   }
> > + }
> > +
> > + # use $rawline because $line loses %s via sanitization and 
> > thus we can't match against it.
> > + if ($rawline =~ 
> > /\bethtool_sprintf\s*\(\s*$FuncArg\s*,\s*\"\%s\"\s*,\s*$FuncArg\s*\)/) {
> > + if(WARN("ETHTOOL_SPRINTF",
> > +"Prefer ethtool_puts over ethtool_sprintf with 
> > standalone \"%s\" specifier\n" . $herecurr) &&
> > + $fix) {
> > + $fixed[$fixlinenr] =~ 
> > s/ethtool_sprintf\s*\(\s*(.*?),.*?,(.*?)\)/ethtool_puts\($1,$2)/;
>
> Thanks, but:
>
> This fix wouldn't work if the first argument was itself a function
> with multiple arguments.
>
> And this is whitespace formatted incorrectly.
>
> It:
>
> o needs a space after if
> o needs a space after the comma in the fix
> o needs to use the appropriate amount and tabs for indentation
> o needs alignment to open parentheses
> o the backslashes are not required in the fix block
>
> Do you want me to push what I wrote in the link below?
> https://lore.kernel.org/lkml/7eec92d9e72d28e7b5202f41b02a383eb28ddd26.ca...@perches.com/

Ah, I didn't see you provided a diff in previous thread.

Yeah you can push it but it's not really a standalone so perhaps I'll
just steal the diff and
wrap into v3?

>
> > +   }
> > + }
> > +
> > +
> >  # typecasts on min/max could be min_t/max_t
> >   if ($perl_version_ok &&
> >   defined $stat &&
> >
>

Thanks
Justin



[PATCH net-next v3 1/3] ethtool: Implement ethtool_puts()

2023-10-27 Thread Justin Stitt
Use strscpy() to implement ethtool_puts().

Functionally the same as ethtool_sprintf() when it's used with two
arguments or with just "%s" format specifier.

Signed-off-by: Justin Stitt 
---
 include/linux/ethtool.h | 13 +
 net/ethtool/ioctl.c |  7 +++
 2 files changed, 20 insertions(+)

diff --git a/include/linux/ethtool.h b/include/linux/ethtool.h
index 226a36ed5aa1..e340ed822cc2 100644
--- a/include/linux/ethtool.h
+++ b/include/linux/ethtool.h
@@ -1053,6 +1053,19 @@ static inline int ethtool_mm_frag_size_min_to_add(u32 
val_min, u32 *val_add,
  */
 extern __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...);
 
+/**
+ * ethtool_puts - Write string to ethtool string data
+ * @data: Pointer to start of string to update
+ * @str: String to write
+ *
+ * Write string to data. Update data to point at start of next
+ * string.
+ *
+ * Prefer this function to ethtool_sprintf() when given only
+ * two arguments or if @fmt is just "%s".
+ */
+extern void ethtool_puts(u8 **data, const char *str);
+
 /* Link mode to forced speed capabilities maps */
 struct ethtool_forced_speed_map {
u32 speed;
diff --git a/net/ethtool/ioctl.c b/net/ethtool/ioctl.c
index 0b0ce4f81c01..abdf05edf804 100644
--- a/net/ethtool/ioctl.c
+++ b/net/ethtool/ioctl.c
@@ -1991,6 +1991,13 @@ __printf(2, 3) void ethtool_sprintf(u8 **data, const 
char *fmt, ...)
 }
 EXPORT_SYMBOL(ethtool_sprintf);
 
+void ethtool_puts(u8 **data, const char *str)
+{
+   strscpy(*data, str, ETH_GSTRING_LEN);
+   *data += ETH_GSTRING_LEN;
+}
+EXPORT_SYMBOL(ethtool_puts);
+
 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
 {
struct ethtool_value id;

-- 
2.42.0.820.g83a721a137-goog




[PATCH net-next v3 0/3] ethtool: Add ethtool_puts()

2023-10-27 Thread Justin Stitt
Hi,

This series aims to implement ethtool_puts() and send out a wave 1 of
conversions from ethtool_sprintf(). There's also a checkpatch patch
included to check for the cases listed below.

This was sparked from recent discussion here [1]

The conversions are used in cases where ethtool_sprintf() was being used
with just two arguments:
|   ethtool_sprintf(&data, buffer[i].name);
or when it's used with format string: "%s"
|   ethtool_sprintf(&data, "%s", buffer[i].name);
which both now become:
|   ethtool_puts(&data, buffer[i].name);

The first case commonly triggers a -Wformat-security warning with Clang
due to potential problems with format flags present in the strings [3].

The second is just a bit weird with a plain-ol' "%s".

v2 (and newer) of this patch is targeted at linux-next so that we can
catch some of the patches I sent [2] using this "%s" pattern and replace
them before they hit mainline.

Changes found with Cocci [4] and grep [5].

[1]: https://lore.kernel.org/all/202310141935.B326C9E@keescook/
[2]: https://lore.kernel.org/all/?q=dfb%3Aethtool_sprintf+AND+f%3Ajustinstitt
[3]: https://lore.kernel.org/all/202310101528.9496539BE@keescook/
[4]: (script authored by Kees w/ modifications from Joe)
@replace_2_args@
expression BUF;
expression VAR;
@@

-   ethtool_sprintf(BUF, VAR)
+   ethtool_puts(BUF, VAR)

@replace_3_args@
expression BUF;
expression VAR;
@@

-   ethtool_sprintf(BUF, "%s", VAR)
+   ethtool_puts(BUF, VAR)

-   ethtool_sprintf(&BUF, "%s", VAR)
+   ethtool_puts(&BUF, VAR)

[5]: $ rg "ethtool_sprintf\(\s*[^,)]+\s*,\s*[^,)]+\s*\)"

Signed-off-by: Justin Stitt 
---
Changes in v3:
- fix force_speed_maps merge conflict + formatting (thanks Vladimir)
- rebase onto net-next (thanks Andrew, Vladimir)
- change subject (thanks Vladimir)
- fix checkpatch formatting + implementation (thanks Joe)
- Link to v2: 
https://lore.kernel.org/r/20231026-ethtool_puts_impl-v2-0-0d67cbdd0...@google.com

Changes in v2:
- wrap lines better in replacement (thanks Joe, Kees)
- add --fix to checkpatch (thanks Joe)
- clean up checkpatch formatting (thanks Joe, et al.)
- rebase against next
- Link to v1: 
https://lore.kernel.org/r/20231025-ethtool_puts_impl-v1-0-6a53a93d3...@google.com

---
Justin Stitt (3):
  ethtool: Implement ethtool_puts()
  checkpatch: add ethtool_sprintf rules
  net: Convert some ethtool_sprintf() to ethtool_puts()

 drivers/net/dsa/lantiq_gswip.c |  2 +-
 drivers/net/dsa/mt7530.c   |  2 +-
 drivers/net/dsa/qca/qca8k-common.c |  2 +-
 drivers/net/dsa/realtek/rtl8365mb.c|  2 +-
 drivers/net/dsa/realtek/rtl8366-core.c |  2 +-
 drivers/net/dsa/vitesse-vsc73xx-core.c |  8 +--
 drivers/net/ethernet/amazon/ena/ena_ethtool.c  |  4 +-
 drivers/net/ethernet/brocade/bna/bnad_ethtool.c|  2 +-
 drivers/net/ethernet/freescale/fec_main.c  |  4 +-
 .../net/ethernet/fungible/funeth/funeth_ethtool.c  |  8 +--
 drivers/net/ethernet/hisilicon/hns/hns_dsaf_gmac.c |  2 +-
 .../net/ethernet/hisilicon/hns/hns_dsaf_xgmac.c|  2 +-
 drivers/net/ethernet/hisilicon/hns/hns_ethtool.c   | 65 +++---
 drivers/net/ethernet/intel/i40e/i40e_ethtool.c |  6 +-
 drivers/net/ethernet/intel/iavf/iavf_ethtool.c |  3 +-
 drivers/net/ethernet/intel/ice/ice_ethtool.c   |  9 +--
 drivers/net/ethernet/intel/idpf/idpf_ethtool.c |  2 +-
 drivers/net/ethernet/intel/igb/igb_ethtool.c   |  6 +-
 drivers/net/ethernet/intel/igc/igc_ethtool.c   |  6 +-
 drivers/net/ethernet/intel/ixgbe/ixgbe_ethtool.c   |  5 +-
 .../net/ethernet/microchip/sparx5/sparx5_ethtool.c |  2 +-
 .../net/ethernet/netronome/nfp/nfp_net_ethtool.c   | 44 +++
 drivers/net/ethernet/pensando/ionic/ionic_stats.c  |  4 +-
 drivers/net/ethernet/wangxun/libwx/wx_ethtool.c|  2 +-
 drivers/net/hyperv/netvsc_drv.c|  4 +-
 drivers/net/phy/nxp-tja11xx.c  |  2 +-
 drivers/net/phy/smsc.c |  2 +-
 drivers/net/vmxnet3/vmxnet3_ethtool.c  | 10 ++--
 include/linux/ethtool.h| 13 +
 net/ethtool/ioctl.c|  7 +++
 scripts/checkpatch.pl  | 19 +++
 31 files changed, 139 insertions(+), 112 deletions(-)
---
base-commit: 3a04927f8d4b7a4f008f04af41e31173002eb1ea
change-id: 20231025-ethtool_puts_impl-a1479ffbc7e0

Best regards,
--
Justin Stitt 




[PATCH net-next v3 2/3] checkpatch: add ethtool_sprintf rules

2023-10-27 Thread Justin Stitt
Add some warnings for using ethtool_sprintf() where a simple
ethtool_puts() would suffice.

The two cases are:

1) Use ethtool_sprintf() with just two arguments:
|   ethtool_sprintf(&data, driver[i].name);
or
2) Use ethtool_sprintf() with a standalone "%s" fmt string:
|   ethtool_sprintf(&data, "%s", driver[i].name);

The former may cause -Wformat-security warnings while the latter is just
not preferred. Both are safely in the category of warnings, not errors.

Signed-off-by: Justin Stitt 
---
 scripts/checkpatch.pl | 19 +++
 1 file changed, 19 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 7d16f863edf1..9369ce1d15c5 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -7020,6 +7020,25 @@ sub process {
 "Prefer strscpy, strscpy_pad, or __nonstring over 
strncpy - see: https://github.com/KSPP/linux/issues/90\n"; . $herecurr);
}
 
+# ethtool_sprintf uses that should likely be ethtool_puts
+   if ($line =~ 
/\bethtool_sprintf\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
+   if (WARN("PREFER_ETHTOOL_PUTS",
+"Prefer ethtool_puts over ethtool_sprintf with 
only two arguments\n" . $herecurr) &&
+   $fix) {
+   $fixed[$fixlinenr] =~ 
s/\bethtool_sprintf\s*\(\s*($FuncArg)\s*,\s*($FuncArg)/ethtool_puts($1, $7)/;
+   }
+   }
+
+   # use $rawline because $line loses %s via sanitization and thus 
we can't match against it.
+   if ($rawline =~ 
/\bethtool_sprintf\s*\(\s*$FuncArg\s*,\s*\"\%s\"\s*,\s*$FuncArg\s*\)/) {
+   if (WARN("PREFER_ETHTOOL_PUTS",
+"Prefer ethtool_puts over ethtool_sprintf with 
standalone \"%s\" specifier\n" . $herecurr) &&
+   $fix) {
+   $fixed[$fixlinenr] =~ 
s/\bethtool_sprintf\s*\(\s*($FuncArg)\s*,\s*"\%s"\s*,\s*($FuncArg)/ethtool_puts($1,
 $7)/;
+   }
+   }
+
+
 # typecasts on min/max could be min_t/max_t
if ($perl_version_ok &&
defined $stat &&

-- 
2.42.0.820.g83a721a137-goog




[PATCH net-next v3 3/3] net: Convert some ethtool_sprintf() to ethtool_puts()

2023-10-27 Thread Justin Stitt
This patch converts some basic cases of ethtool_sprintf() to
ethtool_puts().

The conversions are used in cases where ethtool_sprintf() was being used
with just two arguments:
|   ethtool_sprintf(&data, buffer[i].name);
or when it's used with format string: "%s"
|   ethtool_sprintf(&data, "%s", buffer[i].name);
which both now become:
|   ethtool_puts(&data, buffer[i].name);

Signed-off-by: Justin Stitt 
---
 drivers/net/dsa/lantiq_gswip.c |  2 +-
 drivers/net/dsa/mt7530.c   |  2 +-
 drivers/net/dsa/qca/qca8k-common.c |  2 +-
 drivers/net/dsa/realtek/rtl8365mb.c|  2 +-
 drivers/net/dsa/realtek/rtl8366-core.c |  2 +-
 drivers/net/dsa/vitesse-vsc73xx-core.c |  8 +--
 drivers/net/ethernet/amazon/ena/ena_ethtool.c  |  4 +-
 drivers/net/ethernet/brocade/bna/bnad_ethtool.c|  2 +-
 drivers/net/ethernet/freescale/fec_main.c  |  4 +-
 .../net/ethernet/fungible/funeth/funeth_ethtool.c  |  8 +--
 drivers/net/ethernet/hisilicon/hns/hns_dsaf_gmac.c |  2 +-
 .../net/ethernet/hisilicon/hns/hns_dsaf_xgmac.c|  2 +-
 drivers/net/ethernet/hisilicon/hns/hns_ethtool.c   | 65 +++---
 drivers/net/ethernet/intel/i40e/i40e_ethtool.c |  6 +-
 drivers/net/ethernet/intel/iavf/iavf_ethtool.c |  3 +-
 drivers/net/ethernet/intel/ice/ice_ethtool.c   |  9 +--
 drivers/net/ethernet/intel/idpf/idpf_ethtool.c |  2 +-
 drivers/net/ethernet/intel/igb/igb_ethtool.c   |  6 +-
 drivers/net/ethernet/intel/igc/igc_ethtool.c   |  6 +-
 drivers/net/ethernet/intel/ixgbe/ixgbe_ethtool.c   |  5 +-
 .../net/ethernet/microchip/sparx5/sparx5_ethtool.c |  2 +-
 .../net/ethernet/netronome/nfp/nfp_net_ethtool.c   | 44 +++
 drivers/net/ethernet/pensando/ionic/ionic_stats.c  |  4 +-
 drivers/net/ethernet/wangxun/libwx/wx_ethtool.c|  2 +-
 drivers/net/hyperv/netvsc_drv.c|  4 +-
 drivers/net/phy/nxp-tja11xx.c  |  2 +-
 drivers/net/phy/smsc.c |  2 +-
 drivers/net/vmxnet3/vmxnet3_ethtool.c  | 10 ++--
 28 files changed, 100 insertions(+), 112 deletions(-)

diff --git a/drivers/net/dsa/lantiq_gswip.c b/drivers/net/dsa/lantiq_gswip.c
index 9c185c9f0963..05a017c9ef3d 100644
--- a/drivers/net/dsa/lantiq_gswip.c
+++ b/drivers/net/dsa/lantiq_gswip.c
@@ -1759,7 +1759,7 @@ static void gswip_get_strings(struct dsa_switch *ds, int 
port, u32 stringset,
return;
 
for (i = 0; i < ARRAY_SIZE(gswip_rmon_cnt); i++)
-   ethtool_sprintf(&data, "%s", gswip_rmon_cnt[i].name);
+   ethtool_puts(&data, gswip_rmon_cnt[i].name);
 }
 
 static u32 gswip_bcm_ram_entry_read(struct gswip_priv *priv, u32 table,
diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c
index d27c6b70a2f6..391c4dbdff42 100644
--- a/drivers/net/dsa/mt7530.c
+++ b/drivers/net/dsa/mt7530.c
@@ -836,7 +836,7 @@ mt7530_get_strings(struct dsa_switch *ds, int port, u32 
stringset,
return;
 
for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++)
-   ethtool_sprintf(&data, "%s", mt7530_mib[i].name);
+   ethtool_puts(&data, mt7530_mib[i].name);
 }
 
 static void
diff --git a/drivers/net/dsa/qca/qca8k-common.c 
b/drivers/net/dsa/qca/qca8k-common.c
index 9243eff8918d..2358cd399c7e 100644
--- a/drivers/net/dsa/qca/qca8k-common.c
+++ b/drivers/net/dsa/qca/qca8k-common.c
@@ -487,7 +487,7 @@ void qca8k_get_strings(struct dsa_switch *ds, int port, u32 
stringset,
return;
 
for (i = 0; i < priv->info->mib_count; i++)
-   ethtool_sprintf(&data, "%s", ar8327_mib[i].name);
+   ethtool_puts(&data, ar8327_mib[i].name);
 }
 
 void qca8k_get_ethtool_stats(struct dsa_switch *ds, int port,
diff --git a/drivers/net/dsa/realtek/rtl8365mb.c 
b/drivers/net/dsa/realtek/rtl8365mb.c
index 0875e4fc9f57..b072045eb154 100644
--- a/drivers/net/dsa/realtek/rtl8365mb.c
+++ b/drivers/net/dsa/realtek/rtl8365mb.c
@@ -1303,7 +1303,7 @@ static void rtl8365mb_get_strings(struct dsa_switch *ds, 
int port, u32 stringset
 
for (i = 0; i < RTL8365MB_MIB_END; i++) {
struct rtl8365mb_mib_counter *mib = &rtl8365mb_mib_counters[i];
-   ethtool_sprintf(&data, "%s", mib->name);
+   ethtool_puts(&data, mib->name);
}
 }
 
diff --git a/drivers/net/dsa/realtek/rtl8366-core.c 
b/drivers/net/dsa/realtek/rtl8366-core.c
index 82e267b8fddb..59f98d2c8769 100644
--- a/drivers/net/dsa/realtek/rtl8366-core.c
+++ b/drivers/net/dsa/realtek/rtl8366-core.c
@@ -401,7 +401,7 @@ void rtl8366_get_strings(struct dsa_switch *ds, int port, 
u32 stringset,
return;
 
for (i = 0; i < priv->num_mib_counters; i++)
-   ethtool_sprintf(&data, "%s", pri

Re: [PATCH net-next v3 1/3] ethtool: Implement ethtool_puts()

2023-10-27 Thread Justin Stitt
On Fri, Oct 27, 2023 at 4:43 PM Andrew Lunn  wrote:
>
> > +/**
> > + * ethtool_puts - Write string to ethtool string data
> > + * @data: Pointer to start of string to update
>
> Isn't it actually a pointer to a pointer to the start of string to
> update?

I suppose.

FWIW, I just copy-pasted the sprintf doc and tweaked:
/**
 * ethtool_sprintf - Write formatted string to ethtool string data
 * @data: Pointer to start of string to update
 * @fmt: Format of string to write
 *
 * Write formatted string to data. Update data to point at start of
 * next string.
 */


>
> > +extern void ethtool_puts(u8 **data, const char *str);
>
> Andrew



[PATCH net-next v4 1/3] ethtool: Implement ethtool_puts()

2023-11-02 Thread Justin Stitt
Use strscpy() to implement ethtool_puts().

Functionally the same as ethtool_sprintf() when it's used with two
arguments or with just "%s" format specifier.

Signed-off-by: Justin Stitt 
---
 include/linux/ethtool.h | 13 +
 net/ethtool/ioctl.c |  7 +++
 2 files changed, 20 insertions(+)

diff --git a/include/linux/ethtool.h b/include/linux/ethtool.h
index 226a36ed5aa1..7fc0826d443f 100644
--- a/include/linux/ethtool.h
+++ b/include/linux/ethtool.h
@@ -1053,6 +1053,19 @@ static inline int ethtool_mm_frag_size_min_to_add(u32 
val_min, u32 *val_add,
  */
 extern __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...);
 
+/**
+ * ethtool_puts - Write string to ethtool string data
+ * @data: Pointer to a pointer to the start of string to update
+ * @str: String to write
+ *
+ * Write string to *data. Update *data to point at start of
+ * next string.
+ *
+ * Prefer this function to ethtool_sprintf() when given only
+ * two arguments or if @fmt is just "%s".
+ */
+extern void ethtool_puts(u8 **data, const char *str);
+
 /* Link mode to forced speed capabilities maps */
 struct ethtool_forced_speed_map {
u32 speed;
diff --git a/net/ethtool/ioctl.c b/net/ethtool/ioctl.c
index 0b0ce4f81c01..abdf05edf804 100644
--- a/net/ethtool/ioctl.c
+++ b/net/ethtool/ioctl.c
@@ -1991,6 +1991,13 @@ __printf(2, 3) void ethtool_sprintf(u8 **data, const 
char *fmt, ...)
 }
 EXPORT_SYMBOL(ethtool_sprintf);
 
+void ethtool_puts(u8 **data, const char *str)
+{
+   strscpy(*data, str, ETH_GSTRING_LEN);
+   *data += ETH_GSTRING_LEN;
+}
+EXPORT_SYMBOL(ethtool_puts);
+
 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
 {
struct ethtool_value id;

-- 
2.42.0.869.gea05f2083d-goog




[PATCH net-next v4 3/3] net: Convert some ethtool_sprintf() to ethtool_puts()

2023-11-02 Thread Justin Stitt
This patch converts some basic cases of ethtool_sprintf() to
ethtool_puts().

The conversions are used in cases where ethtool_sprintf() was being used
with just two arguments:
|   ethtool_sprintf(&data, buffer[i].name);
or when it's used with format string: "%s"
|   ethtool_sprintf(&data, "%s", buffer[i].name);
which both now become:
|   ethtool_puts(&data, buffer[i].name);

Signed-off-by: Justin Stitt 
---
 drivers/net/dsa/lantiq_gswip.c |  2 +-
 drivers/net/dsa/mt7530.c   |  2 +-
 drivers/net/dsa/qca/qca8k-common.c |  2 +-
 drivers/net/dsa/realtek/rtl8365mb.c|  2 +-
 drivers/net/dsa/realtek/rtl8366-core.c |  2 +-
 drivers/net/dsa/vitesse-vsc73xx-core.c |  8 +--
 drivers/net/ethernet/amazon/ena/ena_ethtool.c  |  4 +-
 drivers/net/ethernet/brocade/bna/bnad_ethtool.c|  2 +-
 drivers/net/ethernet/freescale/fec_main.c  |  4 +-
 .../net/ethernet/fungible/funeth/funeth_ethtool.c  |  8 +--
 drivers/net/ethernet/hisilicon/hns/hns_dsaf_gmac.c |  2 +-
 .../net/ethernet/hisilicon/hns/hns_dsaf_xgmac.c|  2 +-
 drivers/net/ethernet/hisilicon/hns/hns_ethtool.c   | 65 +++---
 drivers/net/ethernet/intel/i40e/i40e_ethtool.c |  6 +-
 drivers/net/ethernet/intel/iavf/iavf_ethtool.c |  3 +-
 drivers/net/ethernet/intel/ice/ice_ethtool.c   |  9 +--
 drivers/net/ethernet/intel/idpf/idpf_ethtool.c |  2 +-
 drivers/net/ethernet/intel/igb/igb_ethtool.c   |  6 +-
 drivers/net/ethernet/intel/igc/igc_ethtool.c   |  6 +-
 drivers/net/ethernet/intel/ixgbe/ixgbe_ethtool.c   |  5 +-
 .../net/ethernet/microchip/sparx5/sparx5_ethtool.c |  2 +-
 .../net/ethernet/netronome/nfp/nfp_net_ethtool.c   | 44 +++
 drivers/net/ethernet/pensando/ionic/ionic_stats.c  |  4 +-
 drivers/net/ethernet/wangxun/libwx/wx_ethtool.c|  2 +-
 drivers/net/hyperv/netvsc_drv.c|  4 +-
 drivers/net/phy/nxp-tja11xx.c  |  2 +-
 drivers/net/phy/smsc.c |  2 +-
 drivers/net/vmxnet3/vmxnet3_ethtool.c  | 10 ++--
 28 files changed, 100 insertions(+), 112 deletions(-)

diff --git a/drivers/net/dsa/lantiq_gswip.c b/drivers/net/dsa/lantiq_gswip.c
index 9c185c9f0963..05a017c9ef3d 100644
--- a/drivers/net/dsa/lantiq_gswip.c
+++ b/drivers/net/dsa/lantiq_gswip.c
@@ -1759,7 +1759,7 @@ static void gswip_get_strings(struct dsa_switch *ds, int 
port, u32 stringset,
return;
 
for (i = 0; i < ARRAY_SIZE(gswip_rmon_cnt); i++)
-   ethtool_sprintf(&data, "%s", gswip_rmon_cnt[i].name);
+   ethtool_puts(&data, gswip_rmon_cnt[i].name);
 }
 
 static u32 gswip_bcm_ram_entry_read(struct gswip_priv *priv, u32 table,
diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c
index d27c6b70a2f6..391c4dbdff42 100644
--- a/drivers/net/dsa/mt7530.c
+++ b/drivers/net/dsa/mt7530.c
@@ -836,7 +836,7 @@ mt7530_get_strings(struct dsa_switch *ds, int port, u32 
stringset,
return;
 
for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++)
-   ethtool_sprintf(&data, "%s", mt7530_mib[i].name);
+   ethtool_puts(&data, mt7530_mib[i].name);
 }
 
 static void
diff --git a/drivers/net/dsa/qca/qca8k-common.c 
b/drivers/net/dsa/qca/qca8k-common.c
index 9243eff8918d..2358cd399c7e 100644
--- a/drivers/net/dsa/qca/qca8k-common.c
+++ b/drivers/net/dsa/qca/qca8k-common.c
@@ -487,7 +487,7 @@ void qca8k_get_strings(struct dsa_switch *ds, int port, u32 
stringset,
return;
 
for (i = 0; i < priv->info->mib_count; i++)
-   ethtool_sprintf(&data, "%s", ar8327_mib[i].name);
+   ethtool_puts(&data, ar8327_mib[i].name);
 }
 
 void qca8k_get_ethtool_stats(struct dsa_switch *ds, int port,
diff --git a/drivers/net/dsa/realtek/rtl8365mb.c 
b/drivers/net/dsa/realtek/rtl8365mb.c
index 0875e4fc9f57..b072045eb154 100644
--- a/drivers/net/dsa/realtek/rtl8365mb.c
+++ b/drivers/net/dsa/realtek/rtl8365mb.c
@@ -1303,7 +1303,7 @@ static void rtl8365mb_get_strings(struct dsa_switch *ds, 
int port, u32 stringset
 
for (i = 0; i < RTL8365MB_MIB_END; i++) {
struct rtl8365mb_mib_counter *mib = &rtl8365mb_mib_counters[i];
-   ethtool_sprintf(&data, "%s", mib->name);
+   ethtool_puts(&data, mib->name);
}
 }
 
diff --git a/drivers/net/dsa/realtek/rtl8366-core.c 
b/drivers/net/dsa/realtek/rtl8366-core.c
index 82e267b8fddb..59f98d2c8769 100644
--- a/drivers/net/dsa/realtek/rtl8366-core.c
+++ b/drivers/net/dsa/realtek/rtl8366-core.c
@@ -401,7 +401,7 @@ void rtl8366_get_strings(struct dsa_switch *ds, int port, 
u32 stringset,
return;
 
for (i = 0; i < priv->num_mib_counters; i++)
-   ethtool_sprintf(&data, "%s", pri

[PATCH net-next v4 2/3] checkpatch: add ethtool_sprintf rules

2023-11-02 Thread Justin Stitt
Add some warnings for using ethtool_sprintf() where a simple
ethtool_puts() would suffice.

The two cases are:

1) Use ethtool_sprintf() with just two arguments:
|   ethtool_sprintf(&data, driver[i].name);
or
2) Use ethtool_sprintf() with a standalone "%s" fmt string:
|   ethtool_sprintf(&data, "%s", driver[i].name);

The former may cause -Wformat-security warnings while the latter is just
not preferred. Both are safely in the category of warnings, not errors.

Signed-off-by: Justin Stitt 
---
 scripts/checkpatch.pl | 19 +++
 1 file changed, 19 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 7d16f863edf1..9369ce1d15c5 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -7020,6 +7020,25 @@ sub process {
 "Prefer strscpy, strscpy_pad, or __nonstring over 
strncpy - see: https://github.com/KSPP/linux/issues/90\n"; . $herecurr);
}
 
+# ethtool_sprintf uses that should likely be ethtool_puts
+   if ($line =~ 
/\bethtool_sprintf\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
+   if (WARN("PREFER_ETHTOOL_PUTS",
+"Prefer ethtool_puts over ethtool_sprintf with 
only two arguments\n" . $herecurr) &&
+   $fix) {
+   $fixed[$fixlinenr] =~ 
s/\bethtool_sprintf\s*\(\s*($FuncArg)\s*,\s*($FuncArg)/ethtool_puts($1, $7)/;
+   }
+   }
+
+   # use $rawline because $line loses %s via sanitization and thus 
we can't match against it.
+   if ($rawline =~ 
/\bethtool_sprintf\s*\(\s*$FuncArg\s*,\s*\"\%s\"\s*,\s*$FuncArg\s*\)/) {
+   if (WARN("PREFER_ETHTOOL_PUTS",
+"Prefer ethtool_puts over ethtool_sprintf with 
standalone \"%s\" specifier\n" . $herecurr) &&
+   $fix) {
+   $fixed[$fixlinenr] =~ 
s/\bethtool_sprintf\s*\(\s*($FuncArg)\s*,\s*"\%s"\s*,\s*($FuncArg)/ethtool_puts($1,
 $7)/;
+   }
+   }
+
+
 # typecasts on min/max could be min_t/max_t
if ($perl_version_ok &&
defined $stat &&

-- 
2.42.0.869.gea05f2083d-goog




[PATCH net-next v4 0/3] ethtool: Add ethtool_puts()

2023-11-02 Thread Justin Stitt
Hi,

This series aims to implement ethtool_puts() and send out a wave 1 of
conversions from ethtool_sprintf(). There's also a checkpatch patch
included to check for the cases listed below.

This was sparked from recent discussion here [1]

The conversions are used in cases where ethtool_sprintf() was being used
with just two arguments:
|   ethtool_sprintf(&data, buffer[i].name);
or when it's used with format string: "%s"
|   ethtool_sprintf(&data, "%s", buffer[i].name);
which both now become:
|   ethtool_puts(&data, buffer[i].name);

The first case commonly triggers a -Wformat-security warning with Clang
due to potential problems with format flags present in the strings [3].

The second is just a bit weird with a plain-ol' "%s".

Changes found with Cocci [4] and grep [5].

[1]: https://lore.kernel.org/all/202310141935.B326C9E@keescook/
[2]: https://lore.kernel.org/all/?q=dfb%3Aethtool_sprintf+AND+f%3Ajustinstitt
[3]: https://lore.kernel.org/all/202310101528.9496539BE@keescook/
[4]: (script authored by Kees w/ modifications from Joe)
@replace_2_args@
expression BUF;
expression VAR;
@@

-   ethtool_sprintf(BUF, VAR)
+   ethtool_puts(BUF, VAR)

@replace_3_args@
expression BUF;
expression VAR;
@@

-   ethtool_sprintf(BUF, "%s", VAR)
+   ethtool_puts(BUF, VAR)

-   ethtool_sprintf(&BUF, "%s", VAR)
+   ethtool_puts(&BUF, VAR)

[5]: $ rg "ethtool_sprintf\(\s*[^,)]+\s*,\s*[^,)]+\s*\)"

Signed-off-by: Justin Stitt 
---
Changes in v4:
- update documentation to match:
  https://lore.kernel.org/all/20231028192511.11-1-and...@lunn.ch/

- Link to v3: 
https://lore.kernel.org/r/20231027-ethtool_puts_impl-v3-0-3466ac679...@google.com

Changes in v3:
- fix force_speed_maps merge conflict + formatting (thanks Vladimir)
- rebase onto net-next (thanks Andrew, Vladimir)
- change subject (thanks Vladimir)
- fix checkpatch formatting + implementation (thanks Joe)
- Link to v2: 
https://lore.kernel.org/r/20231026-ethtool_puts_impl-v2-0-0d67cbdd0...@google.com

Changes in v2:
- wrap lines better in replacement (thanks Joe, Kees)
- add --fix to checkpatch (thanks Joe)
- clean up checkpatch formatting (thanks Joe, et al.)
- rebase against next
- Link to v1: 
https://lore.kernel.org/r/20231025-ethtool_puts_impl-v1-0-6a53a93d3...@google.com

---
Justin Stitt (3):
  ethtool: Implement ethtool_puts()
  checkpatch: add ethtool_sprintf rules
  net: Convert some ethtool_sprintf() to ethtool_puts()

 drivers/net/dsa/lantiq_gswip.c |  2 +-
 drivers/net/dsa/mt7530.c   |  2 +-
 drivers/net/dsa/qca/qca8k-common.c |  2 +-
 drivers/net/dsa/realtek/rtl8365mb.c|  2 +-
 drivers/net/dsa/realtek/rtl8366-core.c |  2 +-
 drivers/net/dsa/vitesse-vsc73xx-core.c |  8 +--
 drivers/net/ethernet/amazon/ena/ena_ethtool.c  |  4 +-
 drivers/net/ethernet/brocade/bna/bnad_ethtool.c|  2 +-
 drivers/net/ethernet/freescale/fec_main.c  |  4 +-
 .../net/ethernet/fungible/funeth/funeth_ethtool.c  |  8 +--
 drivers/net/ethernet/hisilicon/hns/hns_dsaf_gmac.c |  2 +-
 .../net/ethernet/hisilicon/hns/hns_dsaf_xgmac.c|  2 +-
 drivers/net/ethernet/hisilicon/hns/hns_ethtool.c   | 65 +++---
 drivers/net/ethernet/intel/i40e/i40e_ethtool.c |  6 +-
 drivers/net/ethernet/intel/iavf/iavf_ethtool.c |  3 +-
 drivers/net/ethernet/intel/ice/ice_ethtool.c   |  9 +--
 drivers/net/ethernet/intel/idpf/idpf_ethtool.c |  2 +-
 drivers/net/ethernet/intel/igb/igb_ethtool.c   |  6 +-
 drivers/net/ethernet/intel/igc/igc_ethtool.c   |  6 +-
 drivers/net/ethernet/intel/ixgbe/ixgbe_ethtool.c   |  5 +-
 .../net/ethernet/microchip/sparx5/sparx5_ethtool.c |  2 +-
 .../net/ethernet/netronome/nfp/nfp_net_ethtool.c   | 44 +++
 drivers/net/ethernet/pensando/ionic/ionic_stats.c  |  4 +-
 drivers/net/ethernet/wangxun/libwx/wx_ethtool.c|  2 +-
 drivers/net/hyperv/netvsc_drv.c|  4 +-
 drivers/net/phy/nxp-tja11xx.c  |  2 +-
 drivers/net/phy/smsc.c |  2 +-
 drivers/net/vmxnet3/vmxnet3_ethtool.c  | 10 ++--
 include/linux/ethtool.h| 13 +
 net/ethtool/ioctl.c|  7 +++
 scripts/checkpatch.pl  | 19 +++
 31 files changed, 139 insertions(+), 112 deletions(-)
---
base-commit: 3a04927f8d4b7a4f008f04af41e31173002eb1ea
change-id: 20231025-ethtool_puts_impl-a1479ffbc7e0

Best regards,
--
Justin Stitt