diff mbox series

[v3,for-next,1/8] RDMA/hns: Use temporary variables to fix warning about hr_reg_write()

Message ID 1623915111-43630-2-git-send-email-liweihang@huawei.com (mailing list archive)
State Superseded
Headers show
Series RDMA/hns: Use new interfaces to write/read fields | expand

Commit Message

Weihang Li June 17, 2021, 7:31 a.m. UTC
From: Lang Cheng <chenglang@huawei.com>

Fix complains from sparse about "dubious: x & !y" when calling
hr_reg_write(ctx, field, !!val).

Signed-off-by: Lang Cheng <chenglang@huawei.com>
Signed-off-by: Weihang Li <liweihang@huawei.com>
---
 drivers/infiniband/hw/hns/hns_roce_common.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Jason Gunthorpe June 17, 2021, 10:41 p.m. UTC | #1
On Thu, Jun 17, 2021 at 03:31:44PM +0800, Weihang Li wrote:
> From: Lang Cheng <chenglang@huawei.com>
> 
> Fix complains from sparse about "dubious: x & !y" when calling
> hr_reg_write(ctx, field, !!val).

Where is this from?

I'm not convinced you should have the temporary here given how much
magics are involved in this stuff that rely on builtin_constant_p

Jason
Weihang Li June 18, 2021, 9:17 a.m. UTC | #2
On 2021/6/18 6:41, Jason Gunthorpe wrote:
> On Thu, Jun 17, 2021 at 03:31:44PM +0800, Weihang Li wrote:
>> From: Lang Cheng <chenglang@huawei.com>
>>
>> Fix complains from sparse about "dubious: x & !y" when calling
>> hr_reg_write(ctx, field, !!val).
> 
> Where is this from?
> 
> I'm not convinced you should have the temporary here given how much
> magics are involved in this stuff that rely on builtin_constant_p
> 
> Jason
> 

The warning comes from:

#define __BF_FIELD_CHECK(_mask, _reg, _val, _pfx)			\
		...

		BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ?		\
				 ~((_mask) >> __bf_shf(_mask)) & (_val) : 0, \
				 _pfx "value too large for the field"); \

I will change 'hr_reg_write(ctx, field, !!val)' to 'hr_reg_write(ctx, field, val
? 1 : 0)'. The latter is not as succinct as the former, but can avoid the sparse
warning.

Thanks
Weihang
diff mbox series

Patch

diff --git a/drivers/infiniband/hw/hns/hns_roce_common.h b/drivers/infiniband/hw/hns/hns_roce_common.h
index 3a5658f..eb2d670 100644
--- a/drivers/infiniband/hw/hns/hns_roce_common.h
+++ b/drivers/infiniband/hw/hns/hns_roce_common.h
@@ -79,9 +79,10 @@ 
 
 #define _hr_reg_write(ptr, field_type, field_h, field_l, val)                  \
 	({                                                                     \
+		u32 _val = val;                                                \
 		_hr_reg_clear(ptr, field_type, field_h, field_l);              \
 		*((__le32 *)ptr + (field_h) / 32) |= cpu_to_le32(FIELD_PREP(   \
-			GENMASK((field_h) % 32, (field_l) % 32), val));        \
+			GENMASK((field_h) % 32, (field_l) % 32), _val));       \
 	})
 
 #define hr_reg_write(ptr, field, val) _hr_reg_write(ptr, field, val)