[PATCH] RDMA: replace ternary operator with min()

Guo Zhengkui posted 1 patch 4 years ago
drivers/infiniband/hw/cxgb4/cm.c   | 4 ++--
drivers/infiniband/sw/siw/siw_cm.c | 4 ++--
2 files changed, 4 insertions(+), 4 deletions(-)
[PATCH] RDMA: replace ternary operator with min()
Posted by Guo Zhengkui 4 years ago
Fix the following coccicheck warnings:

drivers/infiniband/sw/siw/siw_cm.c:1326:11-12: WARNING opportunity for min()
drivers/infiniband/sw/siw/siw_cm.c:488:11-12: WARNING opportunity for min()
drivers/infiniband/hw/cxgb4/cm.c:217:14-15: WARNING opportunity for min()
drivers/infiniband/hw/cxgb4/cm.c:232:14-15: WARNING opportunity for min()

min() macro is defined in include/linux/minmax.h. It avoids multiple
evaluations of the arguments when non-constant and performs strict
type-checking.

Signed-off-by: Guo Zhengkui <guozhengkui@vivo.com>
---
 drivers/infiniband/hw/cxgb4/cm.c   | 4 ++--
 drivers/infiniband/sw/siw/siw_cm.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/infiniband/hw/cxgb4/cm.c b/drivers/infiniband/hw/cxgb4/cm.c
index c16017f6e8db..167faa358800 100644
--- a/drivers/infiniband/hw/cxgb4/cm.c
+++ b/drivers/infiniband/hw/cxgb4/cm.c
@@ -214,7 +214,7 @@ static int c4iw_l2t_send(struct c4iw_rdev *rdev, struct sk_buff *skb,
 		kfree_skb(skb);
 	else if (error == NET_XMIT_DROP)
 		return -ENOMEM;
-	return error < 0 ? error : 0;
+	return min(error, 0);
 }
 
 int c4iw_ofld_send(struct c4iw_rdev *rdev, struct sk_buff *skb)
@@ -229,7 +229,7 @@ int c4iw_ofld_send(struct c4iw_rdev *rdev, struct sk_buff *skb)
 	error = cxgb4_ofld_send(rdev->lldi.ports[0], skb);
 	if (error < 0)
 		kfree_skb(skb);
-	return error < 0 ? error : 0;
+	return min(error, 0);
 }
 
 static void release_tid(struct c4iw_rdev *rdev, u32 hwtid, struct sk_buff *skb)
diff --git a/drivers/infiniband/sw/siw/siw_cm.c b/drivers/infiniband/sw/siw/siw_cm.c
index 17f34d584cd9..3f8181664050 100644
--- a/drivers/infiniband/sw/siw/siw_cm.c
+++ b/drivers/infiniband/sw/siw/siw_cm.c
@@ -485,7 +485,7 @@ static int siw_send_mpareqrep(struct siw_cep *cep, const void *pdata, u8 pd_len)
 
 	rv = kernel_sendmsg(s, &msg, iov, iovec_num + 1, mpa_len);
 
-	return rv < 0 ? rv : 0;
+	return min(rv, 0);
 }
 
 /*
@@ -1324,7 +1324,7 @@ static int kernel_bindconnect(struct socket *s, struct sockaddr *laddr,
 
 	rv = s->ops->connect(s, raddr, size, flags);
 
-	return rv < 0 ? rv : 0;
+	return min(rv, 0);
 }
 
 int siw_connect(struct iw_cm_id *id, struct iw_cm_conn_param *params)
-- 
2.20.1
Re: [PATCH] RDMA: replace ternary operator with min()
Posted by Jason Gunthorpe 4 years ago
On Thu, May 12, 2022 at 09:58:37PM +0800, Guo Zhengkui wrote:
> Fix the following coccicheck warnings:
> 
> drivers/infiniband/sw/siw/siw_cm.c:1326:11-12: WARNING opportunity for min()
> drivers/infiniband/sw/siw/siw_cm.c:488:11-12: WARNING opportunity for min()
> drivers/infiniband/hw/cxgb4/cm.c:217:14-15: WARNING opportunity for min()
> drivers/infiniband/hw/cxgb4/cm.c:232:14-15: WARNING opportunity for min()
> 
> min() macro is defined in include/linux/minmax.h. It avoids multiple
> evaluations of the arguments when non-constant and performs strict
> type-checking.

no, these are all error return patterns, if you want to change them to
something change them to

if (error < 0)
   return error;
return 0;

Jason
Re: [PATCH] RDMA: replace ternary operator with min()
Posted by Guo Zhengkui 4 years ago
On 2022/5/12 22:11, Jason Gunthorpe wrote:
> On Thu, May 12, 2022 at 09:58:37PM +0800, Guo Zhengkui wrote:
>> Fix the following coccicheck warnings:
>>
>> drivers/infiniband/sw/siw/siw_cm.c:1326:11-12: WARNING opportunity for min()
>> drivers/infiniband/sw/siw/siw_cm.c:488:11-12: WARNING opportunity for min()
>> drivers/infiniband/hw/cxgb4/cm.c:217:14-15: WARNING opportunity for min()
>> drivers/infiniband/hw/cxgb4/cm.c:232:14-15: WARNING opportunity for min()
>>
>> min() macro is defined in include/linux/minmax.h. It avoids multiple
>> evaluations of the arguments when non-constant and performs strict
>> type-checking.
> 
> no, these are all error return patterns, if you want to change them to
> something change them to
> 
> if (error < 0)
>     return error;
> return 0;

Do you mean we need to stress that they are error return patterns 
instead of taking "minimum value" style code?

Zhengkui

> 
> Jason