* [PATCH] sfc: replace min/max nesting with clamp()
@ 2025-08-12 6:50 Xichao Zhao
2025-08-12 18:06 ` Joe Damato
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Xichao Zhao @ 2025-08-12 6:50 UTC (permalink / raw)
To: ecree.xilinx, andrew+netdev, davem, edumazet, kuba, pabeni
Cc: netdev, linux-net-drivers, linux-kernel, Xichao Zhao
The clamp() macro explicitly expresses the intent of constraining
a value within bounds.Therefore, replacing min(max(a, b), c) with
clamp(val, lo, hi) can improve code readability.
Signed-off-by: Xichao Zhao <zhao.xichao@vivo.com>
---
drivers/net/ethernet/sfc/efx_channels.c | 4 ++--
drivers/net/ethernet/sfc/falcon/efx.c | 5 ++---
drivers/net/ethernet/sfc/siena/efx_channels.c | 4 ++--
3 files changed, 6 insertions(+), 7 deletions(-)
diff --git a/drivers/net/ethernet/sfc/efx_channels.c b/drivers/net/ethernet/sfc/efx_channels.c
index 06b4f52713ef..0f66324ed351 100644
--- a/drivers/net/ethernet/sfc/efx_channels.c
+++ b/drivers/net/ethernet/sfc/efx_channels.c
@@ -216,8 +216,8 @@ static int efx_allocate_msix_channels(struct efx_nic *efx,
if (efx_separate_tx_channels) {
efx->n_tx_channels =
- min(max(n_channels / 2, 1U),
- efx->max_tx_channels);
+ clamp(n_channels / 2, 1U,
+ efx->max_tx_channels);
efx->tx_channel_offset =
n_channels - efx->n_tx_channels;
efx->n_rx_channels =
diff --git a/drivers/net/ethernet/sfc/falcon/efx.c b/drivers/net/ethernet/sfc/falcon/efx.c
index b07f7e4e2877..d19fbf8732ff 100644
--- a/drivers/net/ethernet/sfc/falcon/efx.c
+++ b/drivers/net/ethernet/sfc/falcon/efx.c
@@ -1394,9 +1394,8 @@ static int ef4_probe_interrupts(struct ef4_nic *efx)
if (n_channels > extra_channels)
n_channels -= extra_channels;
if (ef4_separate_tx_channels) {
- efx->n_tx_channels = min(max(n_channels / 2,
- 1U),
- efx->max_tx_channels);
+ efx->n_tx_channels = clamp(n_channels / 2, 1U,
+ efx->max_tx_channels);
efx->n_rx_channels = max(n_channels -
efx->n_tx_channels,
1U);
diff --git a/drivers/net/ethernet/sfc/siena/efx_channels.c b/drivers/net/ethernet/sfc/siena/efx_channels.c
index d120b3c83ac0..703419866d18 100644
--- a/drivers/net/ethernet/sfc/siena/efx_channels.c
+++ b/drivers/net/ethernet/sfc/siena/efx_channels.c
@@ -217,8 +217,8 @@ static int efx_allocate_msix_channels(struct efx_nic *efx,
if (efx_siena_separate_tx_channels) {
efx->n_tx_channels =
- min(max(n_channels / 2, 1U),
- efx->max_tx_channels);
+ clamp(n_channels / 2, 1U,
+ efx->max_tx_channels);
efx->tx_channel_offset =
n_channels - efx->n_tx_channels;
efx->n_rx_channels =
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] sfc: replace min/max nesting with clamp()
2025-08-12 6:50 [PATCH] sfc: replace min/max nesting with clamp() Xichao Zhao
@ 2025-08-12 18:06 ` Joe Damato
2025-08-14 13:31 ` Edward Cree
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Joe Damato @ 2025-08-12 18:06 UTC (permalink / raw)
To: Xichao Zhao
Cc: ecree.xilinx, andrew+netdev, davem, edumazet, kuba, pabeni,
netdev, linux-net-drivers, linux-kernel
On Tue, Aug 12, 2025 at 02:50:26PM +0800, Xichao Zhao wrote:
> The clamp() macro explicitly expresses the intent of constraining
> a value within bounds.Therefore, replacing min(max(a, b), c) with
> clamp(val, lo, hi) can improve code readability.
>
> Signed-off-by: Xichao Zhao <zhao.xichao@vivo.com>
> ---
> drivers/net/ethernet/sfc/efx_channels.c | 4 ++--
> drivers/net/ethernet/sfc/falcon/efx.c | 5 ++---
> drivers/net/ethernet/sfc/siena/efx_channels.c | 4 ++--
> 3 files changed, 6 insertions(+), 7 deletions(-)
>
Reviewed-by: Joe Damato <joe@dama.to>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] sfc: replace min/max nesting with clamp()
2025-08-12 6:50 [PATCH] sfc: replace min/max nesting with clamp() Xichao Zhao
2025-08-12 18:06 ` Joe Damato
@ 2025-08-14 13:31 ` Edward Cree
2025-08-15 1:00 ` patchwork-bot+netdevbpf
2025-08-17 14:42 ` David Laight
3 siblings, 0 replies; 5+ messages in thread
From: Edward Cree @ 2025-08-14 13:31 UTC (permalink / raw)
To: Xichao Zhao, andrew+netdev, davem, edumazet, kuba, pabeni
Cc: netdev, linux-net-drivers, linux-kernel
On 12/08/2025 07:50, Xichao Zhao wrote:
> The clamp() macro explicitly expresses the intent of constraining
> a value within bounds.Therefore, replacing min(max(a, b), c) with
> clamp(val, lo, hi) can improve code readability.
>
> Signed-off-by: Xichao Zhao <zhao.xichao@vivo.com>
Reviewed-by: Edward Cree <ecree.xilinx@gmail.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] sfc: replace min/max nesting with clamp()
2025-08-12 6:50 [PATCH] sfc: replace min/max nesting with clamp() Xichao Zhao
2025-08-12 18:06 ` Joe Damato
2025-08-14 13:31 ` Edward Cree
@ 2025-08-15 1:00 ` patchwork-bot+netdevbpf
2025-08-17 14:42 ` David Laight
3 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-08-15 1:00 UTC (permalink / raw)
To: Xichao Zhao
Cc: ecree.xilinx, andrew+netdev, davem, edumazet, kuba, pabeni,
netdev, linux-net-drivers, linux-kernel
Hello:
This patch was applied to netdev/net-next.git (main)
by Jakub Kicinski <kuba@kernel.org>:
On Tue, 12 Aug 2025 14:50:26 +0800 you wrote:
> The clamp() macro explicitly expresses the intent of constraining
> a value within bounds.Therefore, replacing min(max(a, b), c) with
> clamp(val, lo, hi) can improve code readability.
>
> Signed-off-by: Xichao Zhao <zhao.xichao@vivo.com>
> ---
> drivers/net/ethernet/sfc/efx_channels.c | 4 ++--
> drivers/net/ethernet/sfc/falcon/efx.c | 5 ++---
> drivers/net/ethernet/sfc/siena/efx_channels.c | 4 ++--
> 3 files changed, 6 insertions(+), 7 deletions(-)
Here is the summary with links:
- sfc: replace min/max nesting with clamp()
https://git.kernel.org/netdev/net-next/c/6398d8a856fb
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] sfc: replace min/max nesting with clamp()
2025-08-12 6:50 [PATCH] sfc: replace min/max nesting with clamp() Xichao Zhao
` (2 preceding siblings ...)
2025-08-15 1:00 ` patchwork-bot+netdevbpf
@ 2025-08-17 14:42 ` David Laight
3 siblings, 0 replies; 5+ messages in thread
From: David Laight @ 2025-08-17 14:42 UTC (permalink / raw)
To: Xichao Zhao
Cc: ecree.xilinx, andrew+netdev, davem, edumazet, kuba, pabeni,
netdev, linux-net-drivers, linux-kernel
On Tue, 12 Aug 2025 14:50:26 +0800
Xichao Zhao <zhao.xichao@vivo.com> wrote:
> The clamp() macro explicitly expresses the intent of constraining
> a value within bounds.Therefore, replacing min(max(a, b), c) with
> clamp(val, lo, hi) can improve code readability.
I think you can do a better job of the line wraps?
The first and third won't exceed 80 cols split onto two lines.
They might be 86 on one line - plausibly ok (but I like 80 and they are splittable).
David
>
> Signed-off-by: Xichao Zhao <zhao.xichao@vivo.com>
> ---
> drivers/net/ethernet/sfc/efx_channels.c | 4 ++--
> drivers/net/ethernet/sfc/falcon/efx.c | 5 ++---
> drivers/net/ethernet/sfc/siena/efx_channels.c | 4 ++--
> 3 files changed, 6 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/net/ethernet/sfc/efx_channels.c b/drivers/net/ethernet/sfc/efx_channels.c
> index 06b4f52713ef..0f66324ed351 100644
> --- a/drivers/net/ethernet/sfc/efx_channels.c
> +++ b/drivers/net/ethernet/sfc/efx_channels.c
> @@ -216,8 +216,8 @@ static int efx_allocate_msix_channels(struct efx_nic *efx,
>
> if (efx_separate_tx_channels) {
> efx->n_tx_channels =
> - min(max(n_channels / 2, 1U),
> - efx->max_tx_channels);
> + clamp(n_channels / 2, 1U,
> + efx->max_tx_channels);
> efx->tx_channel_offset =
> n_channels - efx->n_tx_channels;
> efx->n_rx_channels =
> diff --git a/drivers/net/ethernet/sfc/falcon/efx.c b/drivers/net/ethernet/sfc/falcon/efx.c
> index b07f7e4e2877..d19fbf8732ff 100644
> --- a/drivers/net/ethernet/sfc/falcon/efx.c
> +++ b/drivers/net/ethernet/sfc/falcon/efx.c
> @@ -1394,9 +1394,8 @@ static int ef4_probe_interrupts(struct ef4_nic *efx)
> if (n_channels > extra_channels)
> n_channels -= extra_channels;
> if (ef4_separate_tx_channels) {
> - efx->n_tx_channels = min(max(n_channels / 2,
> - 1U),
> - efx->max_tx_channels);
> + efx->n_tx_channels = clamp(n_channels / 2, 1U,
> + efx->max_tx_channels);
> efx->n_rx_channels = max(n_channels -
> efx->n_tx_channels,
> 1U);
> diff --git a/drivers/net/ethernet/sfc/siena/efx_channels.c b/drivers/net/ethernet/sfc/siena/efx_channels.c
> index d120b3c83ac0..703419866d18 100644
> --- a/drivers/net/ethernet/sfc/siena/efx_channels.c
> +++ b/drivers/net/ethernet/sfc/siena/efx_channels.c
> @@ -217,8 +217,8 @@ static int efx_allocate_msix_channels(struct efx_nic *efx,
>
> if (efx_siena_separate_tx_channels) {
> efx->n_tx_channels =
> - min(max(n_channels / 2, 1U),
> - efx->max_tx_channels);
> + clamp(n_channels / 2, 1U,
> + efx->max_tx_channels);
> efx->tx_channel_offset =
> n_channels - efx->n_tx_channels;
> efx->n_rx_channels =
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-08-17 14:42 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-12 6:50 [PATCH] sfc: replace min/max nesting with clamp() Xichao Zhao
2025-08-12 18:06 ` Joe Damato
2025-08-14 13:31 ` Edward Cree
2025-08-15 1:00 ` patchwork-bot+netdevbpf
2025-08-17 14:42 ` David Laight
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).