diff mbox series

[rdma-rc,v1] overflow: Fix -Wtype-limits compilation warnings

Message ID 20190306123717.5334-1-leon@kernel.org (mailing list archive)
State Superseded
Headers show
Series [rdma-rc,v1] overflow: Fix -Wtype-limits compilation warnings | expand

Commit Message

Leon Romanovsky March 6, 2019, 12:37 p.m. UTC
From: Leon Romanovsky <leonro@mellanox.com>

Attempt to use check_shl_overflow() with inputs of unsigned type
produces the following compilation warnings.

drivers/infiniband/hw/mlx5/qp.c: In function _set_user_rq_size_:
./include/linux/overflow.h:230:6: warning: comparison of unsigned
expression >= 0 is always true [-Wtype-limits]
   _s >= 0 && _s < 8 * sizeof(*d) ? _s : 0;  \
      ^~
drivers/infiniband/hw/mlx5/qp.c:5820:6: note: in expansion of macro _check_shl_overflow_
  if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift,
&rwq->buf_size))
      ^~~~~~~~~~~~~~~~~~
./include/linux/overflow.h:232:26: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
  (_to_shift != _s || *_d < 0 || _a < 0 ||   \
                          ^
drivers/infiniband/hw/mlx5/qp.c:5820:6: note: in expansion of macro _check_shl_overflow_
  if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift, &rwq->buf_size))
      ^~~~~~~~~~~~~~~~~~
./include/linux/overflow.h:232:36: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
  (_to_shift != _s || *_d < 0 || _a < 0 ||   \
                                    ^
drivers/infiniband/hw/mlx5/qp.c:5820:6: note: in expansion of macro _check_shl_overflow_
  if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift,&rwq->buf_size))
      ^~~~~~~~~~~~~~~~~~

Fixes: 0c66847793d1 ("overflow.h: Add arithmetic shift helper")
Signed-off-by: Leon Romanovsky <leonro@mellanox.com>
---
Changelog v0->v1:
 * fixed wrong checks
---
 include/linux/overflow.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

--
2.19.1

Comments

Jason Gunthorpe March 6, 2019, 4:44 p.m. UTC | #1
On Wed, Mar 06, 2019 at 02:37:17PM +0200, Leon Romanovsky wrote:
> From: Leon Romanovsky <leonro@mellanox.com>
> 
> Attempt to use check_shl_overflow() with inputs of unsigned type
> produces the following compilation warnings.
> 
> drivers/infiniband/hw/mlx5/qp.c: In function _set_user_rq_size_:
> ./include/linux/overflow.h:230:6: warning: comparison of unsigned
> expression >= 0 is always true [-Wtype-limits]
>    _s >= 0 && _s < 8 * sizeof(*d) ? _s : 0;  \
>       ^~
> drivers/infiniband/hw/mlx5/qp.c:5820:6: note: in expansion of macro _check_shl_overflow_
>   if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift,
> &rwq->buf_size))
>       ^~~~~~~~~~~~~~~~~~
> ./include/linux/overflow.h:232:26: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
>   (_to_shift != _s || *_d < 0 || _a < 0 ||   \
>                           ^
> drivers/infiniband/hw/mlx5/qp.c:5820:6: note: in expansion of macro _check_shl_overflow_
>   if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift, &rwq->buf_size))
>       ^~~~~~~~~~~~~~~~~~
> ./include/linux/overflow.h:232:36: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
>   (_to_shift != _s || *_d < 0 || _a < 0 ||   \
>                                     ^
> drivers/infiniband/hw/mlx5/qp.c:5820:6: note: in expansion of macro _check_shl_overflow_
>   if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift,&rwq->buf_size))
>       ^~~~~~~~~~~~~~~~~~
> 
> Fixes: 0c66847793d1 ("overflow.h: Add arithmetic shift helper")
> Signed-off-by: Leon Romanovsky <leonro@mellanox.com>
> Changelog v0->v1:
>  * fixed wrong checks
>  include/linux/overflow.h | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/include/linux/overflow.h b/include/linux/overflow.h
> index 40b48e2133cb..a47fb6046c0a 100644
> +++ b/include/linux/overflow.h
> @@ -227,10 +227,10 @@
>  	typeof(d) _d = d;						\
>  	u64 _a_full = _a;						\
>  	unsigned int _to_shift =					\
> -		_s >= 0 && _s < 8 * sizeof(*d) ? _s : 0;		\
> +		(_s == 0 || _s > 0) && _s < 8 * sizeof(*d) ? _s : 0;	\

I think this is just trading a tautological compare gcc happens to
know about today (_s >= 0) with one it currently doesn't (_s == 0 ||
_s > 0)

Maybe cc the various people that helped review this macro and see if
we can find another solution?

Jason
Leon Romanovsky March 6, 2019, 5:16 p.m. UTC | #2
On Wed, Mar 06, 2019 at 04:44:04PM +0000, Jason Gunthorpe wrote:
> On Wed, Mar 06, 2019 at 02:37:17PM +0200, Leon Romanovsky wrote:
> > From: Leon Romanovsky <leonro@mellanox.com>
> >
> > Attempt to use check_shl_overflow() with inputs of unsigned type
> > produces the following compilation warnings.
> >
> > drivers/infiniband/hw/mlx5/qp.c: In function _set_user_rq_size_:
> > ./include/linux/overflow.h:230:6: warning: comparison of unsigned
> > expression >= 0 is always true [-Wtype-limits]
> >    _s >= 0 && _s < 8 * sizeof(*d) ? _s : 0;  \
> >       ^~
> > drivers/infiniband/hw/mlx5/qp.c:5820:6: note: in expansion of macro _check_shl_overflow_
> >   if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift,
> > &rwq->buf_size))
> >       ^~~~~~~~~~~~~~~~~~
> > ./include/linux/overflow.h:232:26: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
> >   (_to_shift != _s || *_d < 0 || _a < 0 ||   \
> >                           ^
> > drivers/infiniband/hw/mlx5/qp.c:5820:6: note: in expansion of macro _check_shl_overflow_
> >   if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift, &rwq->buf_size))
> >       ^~~~~~~~~~~~~~~~~~
> > ./include/linux/overflow.h:232:36: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
> >   (_to_shift != _s || *_d < 0 || _a < 0 ||   \
> >                                     ^
> > drivers/infiniband/hw/mlx5/qp.c:5820:6: note: in expansion of macro _check_shl_overflow_
> >   if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift,&rwq->buf_size))
> >       ^~~~~~~~~~~~~~~~~~
> >
> > Fixes: 0c66847793d1 ("overflow.h: Add arithmetic shift helper")
> > Signed-off-by: Leon Romanovsky <leonro@mellanox.com>
> > Changelog v0->v1:
> >  * fixed wrong checks
> >  include/linux/overflow.h | 6 +++---
> >  1 file changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/include/linux/overflow.h b/include/linux/overflow.h
> > index 40b48e2133cb..a47fb6046c0a 100644
> > +++ b/include/linux/overflow.h
> > @@ -227,10 +227,10 @@
> >  	typeof(d) _d = d;						\
> >  	u64 _a_full = _a;						\
> >  	unsigned int _to_shift =					\
> > -		_s >= 0 && _s < 8 * sizeof(*d) ? _s : 0;		\
> > +		(_s == 0 || _s > 0) && _s < 8 * sizeof(*d) ? _s : 0;	\
>
> I think this is just trading a tautological compare gcc happens to
> know about today (_s >= 0) with one it currently doesn't (_s == 0 ||
> _s > 0)
>
> Maybe cc the various people that helped review this macro and see if
> we can find another solution?

I would be glad to see other solution, but I don't know such.

>
> Jason
Leon Romanovsky March 6, 2019, 5:26 p.m. UTC | #3
Kees, Rasmus,

Do you have any idea how to fix this warning while we are compiling
check_shl_overflow() user with W=1?

https://patchwork.kernel.org/patch/10841079/

Thanks

On Wed, Mar 06, 2019 at 02:37:17PM +0200, Leon Romanovsky wrote:
> From: Leon Romanovsky <leonro@mellanox.com>
>
> Attempt to use check_shl_overflow() with inputs of unsigned type
> produces the following compilation warnings.
>
> drivers/infiniband/hw/mlx5/qp.c: In function _set_user_rq_size_:
> ./include/linux/overflow.h:230:6: warning: comparison of unsigned
> expression >= 0 is always true [-Wtype-limits]
>    _s >= 0 && _s < 8 * sizeof(*d) ? _s : 0;  \
>       ^~
> drivers/infiniband/hw/mlx5/qp.c:5820:6: note: in expansion of macro _check_shl_overflow_
>   if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift,
> &rwq->buf_size))
>       ^~~~~~~~~~~~~~~~~~
> ./include/linux/overflow.h:232:26: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
>   (_to_shift != _s || *_d < 0 || _a < 0 ||   \
>                           ^
> drivers/infiniband/hw/mlx5/qp.c:5820:6: note: in expansion of macro _check_shl_overflow_
>   if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift, &rwq->buf_size))
>       ^~~~~~~~~~~~~~~~~~
> ./include/linux/overflow.h:232:36: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
>   (_to_shift != _s || *_d < 0 || _a < 0 ||   \
>                                     ^
> drivers/infiniband/hw/mlx5/qp.c:5820:6: note: in expansion of macro _check_shl_overflow_
>   if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift,&rwq->buf_size))
>       ^~~~~~~~~~~~~~~~~~
>
> Fixes: 0c66847793d1 ("overflow.h: Add arithmetic shift helper")
> Signed-off-by: Leon Romanovsky <leonro@mellanox.com>
> ---
> Changelog v0->v1:
>  * fixed wrong checks
> ---
>  include/linux/overflow.h | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/include/linux/overflow.h b/include/linux/overflow.h
> index 40b48e2133cb..a47fb6046c0a 100644
> --- a/include/linux/overflow.h
> +++ b/include/linux/overflow.h
> @@ -227,10 +227,10 @@
>  	typeof(d) _d = d;						\
>  	u64 _a_full = _a;						\
>  	unsigned int _to_shift =					\
> -		_s >= 0 && _s < 8 * sizeof(*d) ? _s : 0;		\
> +		(_s == 0 || _s > 0) && _s < 8 * sizeof(*d) ? _s : 0;	\
>  	*_d = (_a_full << _to_shift);					\
> -	(_to_shift != _s || *_d < 0 || _a < 0 ||			\
> -		(*_d >> _to_shift) != _a);				\
> +	(_to_shift != _s || !(*_d > 0 || *_d == 0) ||			\
> +	!(_a > 0 || _a == 0) ||	(*_d >> _to_shift) != _a);		\
>  })
>
>  /**
> --
> 2.19.1
>
Bart Van Assche March 7, 2019, 4:11 p.m. UTC | #4
On Wed, 2019-03-06 at 14:37 +0200, Leon Romanovsky wrote:
> From: Leon Romanovsky <leonro@mellanox.com>
> 
> Attempt to use check_shl_overflow() with inputs of unsigned type
> produces the following compilation warnings.
> 
> drivers/infiniband/hw/mlx5/qp.c: In function _set_user_rq_size_:
> ./include/linux/overflow.h:230:6: warning: comparison of unsigned
> expression >= 0 is always true [-Wtype-limits]
>    _s >= 0 && _s < 8 * sizeof(*d) ? _s : 0;  \
>       ^~
> drivers/infiniband/hw/mlx5/qp.c:5820:6: note: in expansion of macro _check_shl_overflow_
>   if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift,
> &rwq->buf_size))
>       ^~~~~~~~~~~~~~~~~~
> ./include/linux/overflow.h:232:26: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
>   (_to_shift != _s || *_d < 0 || _a < 0 ||   \
>                           ^
> drivers/infiniband/hw/mlx5/qp.c:5820:6: note: in expansion of macro _check_shl_overflow_
>   if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift, &rwq->buf_size))
>       ^~~~~~~~~~~~~~~~~~
> ./include/linux/overflow.h:232:36: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
>   (_to_shift != _s || *_d < 0 || _a < 0 ||   \
>                                     ^
> drivers/infiniband/hw/mlx5/qp.c:5820:6: note: in expansion of macro _check_shl_overflow_
>   if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift,&rwq->buf_size))
>       ^~~~~~~~~~~~~~~~~~
> 
> Fixes: 0c66847793d1 ("overflow.h: Add arithmetic shift helper")
> Signed-off-by: Leon Romanovsky <leonro@mellanox.com>
> ---
> Changelog v0->v1:
>  * fixed wrong checks
> ---
>  include/linux/overflow.h | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/include/linux/overflow.h b/include/linux/overflow.h
> index 40b48e2133cb..a47fb6046c0a 100644
> --- a/include/linux/overflow.h
> +++ b/include/linux/overflow.h
> @@ -227,10 +227,10 @@
>  	typeof(d) _d = d;						\
>  	u64 _a_full = _a;						\
>  	unsigned int _to_shift =					\
> -		_s >= 0 && _s < 8 * sizeof(*d) ? _s : 0;		\
> +		(_s == 0 || _s > 0) && _s < 8 * sizeof(*d) ? _s : 0;	\
>  	*_d = (_a_full << _to_shift);					\
> -	(_to_shift != _s || *_d < 0 || _a < 0 ||			\
> -		(*_d >> _to_shift) != _a);				\
> +	(_to_shift != _s || !(*_d > 0 || *_d == 0) ||			\
> +	!(_a > 0 || _a == 0) ||	(*_d >> _to_shift) != _a);		\
>  })
> 
>  /**

Reviewed-by: Bart Van Assche <bvanassche@acm.org>
Bart Van Assche March 7, 2019, 4:14 p.m. UTC | #5
On Wed, 2019-03-06 at 16:44 +0000, Jason Gunthorpe wrote:
> On Wed, Mar 06, 2019 at 02:37:17PM +0200, Leon Romanovsky wrote:
> > diff --git a/include/linux/overflow.h b/include/linux/overflow.h
> > index 40b48e2133cb..a47fb6046c0a 100644
> > +++ b/include/linux/overflow.h
> > @@ -227,10 +227,10 @@
> >  	typeof(d) _d = d;						\
> >  	u64 _a_full = _a;						\
> >  	unsigned int _to_shift =					\
> > -		_s >= 0 && _s < 8 * sizeof(*d) ? _s : 0;		\
> > +		(_s == 0 || _s > 0) && _s < 8 * sizeof(*d) ? _s : 0;	\
> 
> I think this is just trading a tautological compare gcc happens to
> know about today (_s >= 0) with one it currently doesn't (_s == 0 ||
> _s > 0)

Since it is uncommon to write (_s >= 0) as (_s == 0 || _s > 0) my hope is
that the gcc maintainers won't consider it interesting to make gcc report
a warning about the latter expression.

Bart.
Leon Romanovsky March 7, 2019, 4:48 p.m. UTC | #6
On Thu, Mar 07, 2019 at 08:14:01AM -0800, Bart Van Assche wrote:
> On Wed, 2019-03-06 at 16:44 +0000, Jason Gunthorpe wrote:
> > On Wed, Mar 06, 2019 at 02:37:17PM +0200, Leon Romanovsky wrote:
> > > diff --git a/include/linux/overflow.h b/include/linux/overflow.h
> > > index 40b48e2133cb..a47fb6046c0a 100644
> > > +++ b/include/linux/overflow.h
> > > @@ -227,10 +227,10 @@
> > >  	typeof(d) _d = d;						\
> > >  	u64 _a_full = _a;						\
> > >  	unsigned int _to_shift =					\
> > > -		_s >= 0 && _s < 8 * sizeof(*d) ? _s : 0;		\
> > > +		(_s == 0 || _s > 0) && _s < 8 * sizeof(*d) ? _s : 0;	\
> >
> > I think this is just trading a tautological compare gcc happens to
> > know about today (_s >= 0) with one it currently doesn't (_s == 0 ||
> > _s > 0)
>
> Since it is uncommon to write (_s >= 0) as (_s == 0 || _s > 0) my hope is
> that the gcc maintainers won't consider it interesting to make gcc report
> a warning about the latter expression.

Thanks Bart,
Once they do such report, we will find a way to overcome it too.
It is unlikely that it will happen in near future.

Thanks

>
> Bart.
diff mbox series

Patch

diff --git a/include/linux/overflow.h b/include/linux/overflow.h
index 40b48e2133cb..a47fb6046c0a 100644
--- a/include/linux/overflow.h
+++ b/include/linux/overflow.h
@@ -227,10 +227,10 @@ 
 	typeof(d) _d = d;						\
 	u64 _a_full = _a;						\
 	unsigned int _to_shift =					\
-		_s >= 0 && _s < 8 * sizeof(*d) ? _s : 0;		\
+		(_s == 0 || _s > 0) && _s < 8 * sizeof(*d) ? _s : 0;	\
 	*_d = (_a_full << _to_shift);					\
-	(_to_shift != _s || *_d < 0 || _a < 0 ||			\
-		(*_d >> _to_shift) != _a);				\
+	(_to_shift != _s || !(*_d > 0 || *_d == 0) ||			\
+	!(_a > 0 || _a == 0) ||	(*_d >> _to_shift) != _a);		\
 })

 /**