linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] RDMA/hns: Use 64-bit arithmetic instead of 32-bit
@ 2018-10-18  8:02 Gustavo A. R. Silva
  2018-10-18 11:01 ` Leon Romanovsky
  2018-10-22 18:15 ` Jason Gunthorpe
  0 siblings, 2 replies; 6+ messages in thread
From: Gustavo A. R. Silva @ 2018-10-18  8:02 UTC (permalink / raw)
  To: Lijun Ou, Wei Hu(Xavier), Doug Ledford, Jason Gunthorpe
  Cc: linux-rdma, linux-kernel, Gustavo A. R. Silva

Cast *max_num_sg* to u64 in order to give the compiler complete
information about the proper arithmetic to use.

Notice that such variable is used in a context that expects an
expression of type u64 (64 bits, unsigned) and the following
expression is currently being evaluated using 32-bit
arithmetic:

length = max_num_sg * page_size;

Addresses-Coverity-ID: 1474517 ("Unintentional integer overflow")
Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
---
 drivers/infiniband/hw/hns/hns_roce_mr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/infiniband/hw/hns/hns_roce_mr.c b/drivers/infiniband/hw/hns/hns_roce_mr.c
index 521ad2a..d479d5e 100644
--- a/drivers/infiniband/hw/hns/hns_roce_mr.c
+++ b/drivers/infiniband/hw/hns/hns_roce_mr.c
@@ -1219,7 +1219,7 @@ struct ib_mr *hns_roce_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
 	int ret;
 
 	page_size = 1 << (hr_dev->caps.pbl_buf_pg_sz + PAGE_SHIFT);
-	length = max_num_sg * page_size;
+	length = (u64)max_num_sg * page_size;
 
 	if (mr_type != IB_MR_TYPE_MEM_REG)
 		return ERR_PTR(-EINVAL);
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] RDMA/hns: Use 64-bit arithmetic instead of 32-bit
  2018-10-18  8:02 [PATCH] RDMA/hns: Use 64-bit arithmetic instead of 32-bit Gustavo A. R. Silva
@ 2018-10-18 11:01 ` Leon Romanovsky
  2018-10-19  0:17   ` Doug Ledford
  2018-10-22 18:15 ` Jason Gunthorpe
  1 sibling, 1 reply; 6+ messages in thread
From: Leon Romanovsky @ 2018-10-18 11:01 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Lijun Ou, Wei Hu(Xavier),
	Doug Ledford, Jason Gunthorpe, linux-rdma, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1332 bytes --]

On Thu, Oct 18, 2018 at 10:02:58AM +0200, Gustavo A. R. Silva wrote:
> Cast *max_num_sg* to u64 in order to give the compiler complete
> information about the proper arithmetic to use.
>
> Notice that such variable is used in a context that expects an
> expression of type u64 (64 bits, unsigned) and the following
> expression is currently being evaluated using 32-bit
> arithmetic:

And what is wrong with that?
Please fix static analyzer tool instead of fixing proper C code.

Thanks

>
> length = max_num_sg * page_size;
>
> Addresses-Coverity-ID: 1474517 ("Unintentional integer overflow")
> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
> ---
>  drivers/infiniband/hw/hns/hns_roce_mr.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/infiniband/hw/hns/hns_roce_mr.c b/drivers/infiniband/hw/hns/hns_roce_mr.c
> index 521ad2a..d479d5e 100644
> --- a/drivers/infiniband/hw/hns/hns_roce_mr.c
> +++ b/drivers/infiniband/hw/hns/hns_roce_mr.c
> @@ -1219,7 +1219,7 @@ struct ib_mr *hns_roce_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
>  	int ret;
>
>  	page_size = 1 << (hr_dev->caps.pbl_buf_pg_sz + PAGE_SHIFT);
> -	length = max_num_sg * page_size;
> +	length = (u64)max_num_sg * page_size;
>
>  	if (mr_type != IB_MR_TYPE_MEM_REG)
>  		return ERR_PTR(-EINVAL);
> --
> 2.7.4
>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 801 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] RDMA/hns: Use 64-bit arithmetic instead of 32-bit
  2018-10-18 11:01 ` Leon Romanovsky
@ 2018-10-19  0:17   ` Doug Ledford
  2018-10-29 10:07     ` Leon Romanovsky
  0 siblings, 1 reply; 6+ messages in thread
From: Doug Ledford @ 2018-10-19  0:17 UTC (permalink / raw)
  To: Leon Romanovsky, Gustavo A. R. Silva
  Cc: Lijun Ou, Wei Hu(Xavier), Jason Gunthorpe, linux-rdma, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 2245 bytes --]

On Thu, 2018-10-18 at 14:01 +0300, Leon Romanovsky wrote:
> On Thu, Oct 18, 2018 at 10:02:58AM +0200, Gustavo A. R. Silva wrote:
> > Cast *max_num_sg* to u64 in order to give the compiler complete
> > information about the proper arithmetic to use.
> > 
> > Notice that such variable is used in a context that expects an
> > expression of type u64 (64 bits, unsigned) and the following
> > expression is currently being evaluated using 32-bit
> > arithmetic:
> 
> And what is wrong with that?
> Please fix static analyzer tool instead of fixing proper C code.

Judging on the static analyzer tool's message, I don't see anything
wrong with it.  The code contains a potential unintentional overflow
error.  The author might have been well aware of the overflow and not
cared and in that case this is valid C, but the analyzer has no way of
knowing that, so it flags it for review.  To silence the checker you
could either cast the arithmetic to u64, or cast length to u32.  Either
would clear up the ambiguity.  I guess I'm not seeing why you would
blame the static checker in this case, it did the best it is possible
for it to do.

> Thanks
> 
> > 
> > length = max_num_sg * page_size;
> > 
> > Addresses-Coverity-ID: 1474517 ("Unintentional integer overflow")
> > Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
> > ---
> >  drivers/infiniband/hw/hns/hns_roce_mr.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/infiniband/hw/hns/hns_roce_mr.c b/drivers/infiniband/hw/hns/hns_roce_mr.c
> > index 521ad2a..d479d5e 100644
> > --- a/drivers/infiniband/hw/hns/hns_roce_mr.c
> > +++ b/drivers/infiniband/hw/hns/hns_roce_mr.c
> > @@ -1219,7 +1219,7 @@ struct ib_mr *hns_roce_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
> >  	int ret;
> > 
> >  	page_size = 1 << (hr_dev->caps.pbl_buf_pg_sz + PAGE_SHIFT);
> > -	length = max_num_sg * page_size;
> > +	length = (u64)max_num_sg * page_size;
> > 
> >  	if (mr_type != IB_MR_TYPE_MEM_REG)
> >  		return ERR_PTR(-EINVAL);
> > --
> > 2.7.4
> > 

-- 
Doug Ledford <dledford@redhat.com>
    GPG KeyID: B826A3330E572FDD
    Key fingerprint = AE6B 1BDA 122B 23B4 265B  1274 B826 A333 0E57 2FDD

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] RDMA/hns: Use 64-bit arithmetic instead of 32-bit
  2018-10-18  8:02 [PATCH] RDMA/hns: Use 64-bit arithmetic instead of 32-bit Gustavo A. R. Silva
  2018-10-18 11:01 ` Leon Romanovsky
@ 2018-10-22 18:15 ` Jason Gunthorpe
  2018-10-22 18:22   ` Gustavo A. R. Silva
  1 sibling, 1 reply; 6+ messages in thread
From: Jason Gunthorpe @ 2018-10-22 18:15 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Lijun Ou, Wei Hu(Xavier), Doug Ledford, linux-rdma, linux-kernel

On Thu, Oct 18, 2018 at 10:02:58AM +0200, Gustavo A. R. Silva wrote:
> Cast *max_num_sg* to u64 in order to give the compiler complete
> information about the proper arithmetic to use.
> 
> Notice that such variable is used in a context that expects an
> expression of type u64 (64 bits, unsigned) and the following
> expression is currently being evaluated using 32-bit
> arithmetic:
> 
> length = max_num_sg * page_size;
> 
> Addresses-Coverity-ID: 1474517 ("Unintentional integer overflow")
> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
>  drivers/infiniband/hw/hns/hns_roce_mr.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/infiniband/hw/hns/hns_roce_mr.c b/drivers/infiniband/hw/hns/hns_roce_mr.c
> index 521ad2a..d479d5e 100644
> +++ b/drivers/infiniband/hw/hns/hns_roce_mr.c
> @@ -1219,7 +1219,7 @@ struct ib_mr *hns_roce_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
>  	int ret;
>  
>  	page_size = 1 << (hr_dev->caps.pbl_buf_pg_sz + PAGE_SHIFT);
> -	length = max_num_sg * page_size;
> +	length = (u64)max_num_sg * page_size;

This should be done with check_mul_overflow() which will also force
the input types to the correct thing.

alloc_mr is callable from userspace so the potential overflow here
should not be ignored.

Jason

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] RDMA/hns: Use 64-bit arithmetic instead of 32-bit
  2018-10-22 18:15 ` Jason Gunthorpe
@ 2018-10-22 18:22   ` Gustavo A. R. Silva
  0 siblings, 0 replies; 6+ messages in thread
From: Gustavo A. R. Silva @ 2018-10-22 18:22 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Lijun Ou, Wei Hu(Xavier), Doug Ledford, linux-rdma, linux-kernel



On 10/22/18 8:15 PM, Jason Gunthorpe wrote:
> On Thu, Oct 18, 2018 at 10:02:58AM +0200, Gustavo A. R. Silva wrote:
>> Cast *max_num_sg* to u64 in order to give the compiler complete
>> information about the proper arithmetic to use.
>>
>> Notice that such variable is used in a context that expects an
>> expression of type u64 (64 bits, unsigned) and the following
>> expression is currently being evaluated using 32-bit
>> arithmetic:
>>
>> length = max_num_sg * page_size;
>>
>> Addresses-Coverity-ID: 1474517 ("Unintentional integer overflow")
>> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
>>  drivers/infiniband/hw/hns/hns_roce_mr.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/infiniband/hw/hns/hns_roce_mr.c b/drivers/infiniband/hw/hns/hns_roce_mr.c
>> index 521ad2a..d479d5e 100644
>> +++ b/drivers/infiniband/hw/hns/hns_roce_mr.c
>> @@ -1219,7 +1219,7 @@ struct ib_mr *hns_roce_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
>>  	int ret;
>>  
>>  	page_size = 1 << (hr_dev->caps.pbl_buf_pg_sz + PAGE_SHIFT);
>> -	length = max_num_sg * page_size;
>> +	length = (u64)max_num_sg * page_size;
> 
> This should be done with check_mul_overflow() which will also force
> the input types to the correct thing.
> 

Yep, you're right. That's part of the new API for checking overflows.

I'll use that macro.

> alloc_mr is callable from userspace so the potential overflow here
> should not be ignored.
> 

Thanks for the feedback.
--
Gustavo


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] RDMA/hns: Use 64-bit arithmetic instead of 32-bit
  2018-10-19  0:17   ` Doug Ledford
@ 2018-10-29 10:07     ` Leon Romanovsky
  0 siblings, 0 replies; 6+ messages in thread
From: Leon Romanovsky @ 2018-10-29 10:07 UTC (permalink / raw)
  To: Doug Ledford
  Cc: Gustavo A. R. Silva, Lijun Ou, Wei Hu(Xavier),
	Jason Gunthorpe, linux-rdma, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1419 bytes --]

On Thu, Oct 18, 2018 at 08:17:10PM -0400, Doug Ledford wrote:
> On Thu, 2018-10-18 at 14:01 +0300, Leon Romanovsky wrote:
> > On Thu, Oct 18, 2018 at 10:02:58AM +0200, Gustavo A. R. Silva wrote:
> > > Cast *max_num_sg* to u64 in order to give the compiler complete
> > > information about the proper arithmetic to use.
> > >
> > > Notice that such variable is used in a context that expects an
> > > expression of type u64 (64 bits, unsigned) and the following
> > > expression is currently being evaluated using 32-bit
> > > arithmetic:
> >
> > And what is wrong with that?
> > Please fix static analyzer tool instead of fixing proper C code.
>
> Judging on the static analyzer tool's message, I don't see anything
> wrong with it.  The code contains a potential unintentional overflow
> error.  The author might have been well aware of the overflow and not
> cared and in that case this is valid C, but the analyzer has no way of
> knowing that, so it flags it for review.  To silence the checker you
> could either cast the arithmetic to u64, or cast length to u32.  Either
> would clear up the ambiguity.  I guess I'm not seeing why you would
> blame the static checker in this case, it did the best it is possible
> for it to do.

You are right, static analyzer tools have no way to understand that this
overflow isn't possible. I was over excited to go to my vacation hence my
response. Sorry about that.

Thanks

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 801 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-10-29 10:07 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-18  8:02 [PATCH] RDMA/hns: Use 64-bit arithmetic instead of 32-bit Gustavo A. R. Silva
2018-10-18 11:01 ` Leon Romanovsky
2018-10-19  0:17   ` Doug Ledford
2018-10-29 10:07     ` Leon Romanovsky
2018-10-22 18:15 ` Jason Gunthorpe
2018-10-22 18:22   ` Gustavo A. R. Silva

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).