diff mbox series

[net-next,7/8] net/rxrpc: Use min() to simplify the code

Message ID 20240824074033.2134514-8-lihongbo22@huawei.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series Use max/min to simplify the code | 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 fail Errors and warnings before: 14 this patch: 14
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers warning 2 maintainers not CCed: marc.dionne@auristor.com dhowells@redhat.com
netdev/build_clang fail Errors and warnings before: 15 this patch: 15
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 fail Errors and warnings before: 14 this patch: 14
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 9 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

Commit Message

Hongbo Li Aug. 24, 2024, 7:40 a.m. UTC
Let's use min() to simplify the code and fix the
Coccinelle/coccicheck warning reported by minmax.cocci.

Signed-off-by: Hongbo Li <lihongbo22@huawei.com>
---
 net/rxrpc/input.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Comments

David Howells Aug. 24, 2024, 12:06 p.m. UTC | #1
Hongbo Li <lihongbo22@huawei.com> wrote:

> -	summary.ack_reason = (sp->ack.reason < RXRPC_ACK__INVALID ?
> -			      sp->ack.reason : RXRPC_ACK__INVALID);
> +	summary.ack_reason = min(sp->ack.reason, RXRPC_ACK__INVALID);

Can you use umin() rather than min(), please?

Thanks,
David
Hongbo Li Aug. 26, 2024, 1:41 a.m. UTC | #2
On 2024/8/24 20:06, David Howells wrote:
> Hongbo Li <lihongbo22@huawei.com> wrote:
> 
>> -	summary.ack_reason = (sp->ack.reason < RXRPC_ACK__INVALID ?
>> -			      sp->ack.reason : RXRPC_ACK__INVALID);
>> +	summary.ack_reason = min(sp->ack.reason, RXRPC_ACK__INVALID);
> 
> Can you use umin() rather than min(), please?
> I see reason is u8, may I use min_t(u8, sp->ack.reason, RXRPC_ACK__INVALID);

Thanks,
Hongbo

> Thanks,
> David
> 
>
Hongbo Li Aug. 26, 2024, 2:50 a.m. UTC | #3
On 2024/8/24 20:06, David Howells wrote:
> Hongbo Li <lihongbo22@huawei.com> wrote:
> 
>> -	summary.ack_reason = (sp->ack.reason < RXRPC_ACK__INVALID ?
>> -			      sp->ack.reason : RXRPC_ACK__INVALID);
>> +	summary.ack_reason = min(sp->ack.reason, RXRPC_ACK__INVALID);
> 
> Can you use umin() rather than min(), please?
> 

I see reason is u8, so may I use min_t(u8, sp->ack.reason, 
RXRPC_ACK__INVALID)?

Thanks,
Hongbo


> Thanks,
> David
> 
>
Simon Horman Aug. 27, 2024, 5:58 p.m. UTC | #4
On Mon, Aug 26, 2024 at 10:50:03AM +0800, Hongbo Li wrote:
> 
> 
> On 2024/8/24 20:06, David Howells wrote:
> > Hongbo Li <lihongbo22@huawei.com> wrote:
> > 
> > > -	summary.ack_reason = (sp->ack.reason < RXRPC_ACK__INVALID ?
> > > -			      sp->ack.reason : RXRPC_ACK__INVALID);
> > > +	summary.ack_reason = min(sp->ack.reason, RXRPC_ACK__INVALID);
> > 
> > Can you use umin() rather than min(), please?
> > 
> 
> I see reason is u8, so may I use min_t(u8, sp->ack.reason,
> RXRPC_ACK__INVALID)?

I believe that umin was added precisely to avoid such constructions.

See: 80fcac55385c ("minmax: add umin(a, b) and umax(a, b)")
     https://git.kernel.org/torvalds/c/80fcac55385c
David Howells Aug. 28, 2024, 8:17 a.m. UTC | #5
Hongbo Li <lihongbo22@huawei.com> wrote:

> I see reason is u8, so may I use min_t(u8, sp->ack.reason,
> RXRPC_ACK__INVALID)?

No, please don't use min_t(<unsigned type>, ...) if umin() will do.  IIRC,
some arches can't do byte-level arithmetic.

Thanks,
David
David Laight Aug. 29, 2024, 4:46 p.m. UTC | #6
From: David Howells <dhowells@redhat.com>
> Sent: 28 August 2024 09:18
> 
> Hongbo Li <lihongbo22@huawei.com> wrote:
> 
> > I see reason is u8, so may I use min_t(u8, sp->ack.reason,
> > RXRPC_ACK__INVALID)?
> 
> No, please don't use min_t(<unsigned type>, ...) if umin() will do.  IIRC,
> some arches can't do byte-level arithmetic.

Not to mention all the places where the wrong type has been used and
significant bits masked off before the comparison.

Is there even a problem with min() here?
It should be fine unless sp->ack.reason is a signed type.
In which case things are probably horribly wrong if it is negative.

Looking at the code I'm not even sure that min() is right.
It really ought to be used for counters/sizes.
This is a bit like the (broken) suggestion of replacing:
	return rval < 0 ? rval : 0;
with
	return min(rval, 0);

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
diff mbox series

Patch

diff --git a/net/rxrpc/input.c b/net/rxrpc/input.c
index 16d49a861dbb..455aa0189b28 100644
--- a/net/rxrpc/input.c
+++ b/net/rxrpc/input.c
@@ -868,8 +868,7 @@  static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb)
 	prev_pkt	= sp->ack.prev_ack;
 	nr_acks		= sp->ack.nr_acks;
 	hard_ack	= first_soft_ack - 1;
-	summary.ack_reason = (sp->ack.reason < RXRPC_ACK__INVALID ?
-			      sp->ack.reason : RXRPC_ACK__INVALID);
+	summary.ack_reason = min(sp->ack.reason, RXRPC_ACK__INVALID);
 
 	trace_rxrpc_rx_ack(call, ack_serial, acked_serial,
 			   first_soft_ack, prev_pkt,