diff mbox series

[net-next,v1] net: mvneta: Avoid the misuse of the '_t' variants

Message ID 20240905063645.586354-1-yanzhen@vivo.com (mailing list archive)
State Rejected
Delegated to: Netdev Maintainers
Headers show
Series [net-next,v1] net: mvneta: Avoid the misuse of the '_t' variants | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
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: 16 this patch: 16
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 5 of 5 maintainers
netdev/build_clang success Errors and warnings before: 16 this patch: 16
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: 16 this patch: 16
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 10 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-09-06--21-00 (tests: 721)

Commit Message

Yan Zhen Sept. 5, 2024, 6:36 a.m. UTC
Type conversions (especially when narrowing down types) can lead to 
unexpected behavior and should be done with extreme caution.

In this case if someone tries to set the tx_pending to 65536(0x10000),
after forcing it to convert to u16, it becomes 0x0000, they will get
the minimum supported size and not the maximum.

Based on previous discussions [1], such behavior may confuse users or even
create unexpected errors.

[1] https://lore.kernel.org/netdev/d23dfbf563714d7090d163a075ca9a51@AcuMS.aculab.com/

Signed-off-by: Yan Zhen <yanzhen@vivo.com>
---
 drivers/net/ethernet/marvell/mvneta.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

David Laight Sept. 5, 2024, 8:30 a.m. UTC | #1
From: Yan Zhen
> Sent: 05 September 2024 07:37
> 
> Type conversions (especially when narrowing down types) can lead to
> unexpected behavior and should be done with extreme caution.
> 
> In this case if someone tries to set the tx_pending to 65536(0x10000),
> after forcing it to convert to u16, it becomes 0x0000, they will get
> the minimum supported size and not the maximum.
> 
> Based on previous discussions [1], such behavior may confuse users or even
> create unexpected errors.
> 
> [1] https://lore.kernel.org/netdev/d23dfbf563714d7090d163a075ca9a51@AcuMS.aculab.com/
> 
> Signed-off-by: Yan Zhen <yanzhen@vivo.com>

Reviewed-by: david.laight@aculab.com

> ---
>  drivers/net/ethernet/marvell/mvneta.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/ethernet/marvell/mvneta.c b/drivers/net/ethernet/marvell/mvneta.c
> index d72b2d5f96db..b41de182cc88 100644
> --- a/drivers/net/ethernet/marvell/mvneta.c
> +++ b/drivers/net/ethernet/marvell/mvneta.c
> @@ -4753,8 +4753,8 @@ mvneta_ethtool_set_ringparam(struct net_device *dev,
>  	pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
>  		ring->rx_pending : MVNETA_MAX_RXD;
> 
> -	pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
> -				   MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
> +	pp->tx_ring_size = clamp(ring->tx_pending,
> +				 MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
>  	if (pp->tx_ring_size != ring->tx_pending)
>  		netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
>  			    pp->tx_ring_size, ring->tx_pending);
> --
> 2.34.1

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
Jakub Kicinski Sept. 7, 2024, 1:55 a.m. UTC | #2
On Thu,  5 Sep 2024 14:36:45 +0800 Yan Zhen wrote:
> In this case if someone tries to set the tx_pending to 65536(0x10000),
> after forcing it to convert to u16, it becomes 0x0000, they will get
> the minimum supported size and not the maximum.

Core validates against tx_max_pending before calling the driver.
diff mbox series

Patch

diff --git a/drivers/net/ethernet/marvell/mvneta.c b/drivers/net/ethernet/marvell/mvneta.c
index d72b2d5f96db..b41de182cc88 100644
--- a/drivers/net/ethernet/marvell/mvneta.c
+++ b/drivers/net/ethernet/marvell/mvneta.c
@@ -4753,8 +4753,8 @@  mvneta_ethtool_set_ringparam(struct net_device *dev,
 	pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
 		ring->rx_pending : MVNETA_MAX_RXD;
 
-	pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
-				   MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
+	pp->tx_ring_size = clamp(ring->tx_pending,
+				 MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
 	if (pp->tx_ring_size != ring->tx_pending)
 		netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
 			    pp->tx_ring_size, ring->tx_pending);