[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20180801161431.GC10665@mellanox.com>
Date: Wed, 1 Aug 2018 10:14:31 -0600
From: Jason Gunthorpe <jgg@...lanox.com>
To: Peter Zijlstra <peterz@...radead.org>
Cc: Rasmus Villemoes <linux@...musvillemoes.dk>,
Leon Romanovsky <leon@...nel.org>,
Doug Ledford <dledford@...hat.com>,
Kees Cook <keescook@...omium.org>,
Leon Romanovsky <leonro@...lanox.com>,
RDMA mailing list <linux-rdma@...r.kernel.org>,
Hadar Hen Zion <hadarh@...lanox.com>,
Matan Barak <matanb@...lanox.com>,
Michael J Ruhl <michael.j.ruhl@...el.com>,
Noa Osherovich <noaos@...lanox.com>,
Raed Salem <raeds@...lanox.com>,
Yishai Hadas <yishaih@...lanox.com>,
Saeed Mahameed <saeedm@...lanox.com>,
linux-netdev <netdev@...r.kernel.org>,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH rdma-next 08/12] overflow.h: Add arithmetic shift helper
On Wed, Aug 01, 2018 at 11:36:03AM +0200, Peter Zijlstra wrote:
> On Tue, Jun 26, 2018 at 11:54:35AM -0600, Jason Gunthorpe wrote:
>
> > What about more like this?
> >
> > check_shift_overflow(a, s, d) ({
>
> Should that not be: check_shl_overflow() ? Just 'shift' lacks a
> direction.
Yes, that makes sense.
> > // Shift is always performed on the machine's largest unsigned
> > u64 _a = a;
> > typeof(s) _s = s;
> > typeof(d) _d = d;
> >
> > // Make s safe against UB
> > unsigned int _to_shift = _s >= 0 && _s < 8*sizeof(*d) : _s ? 0;
>
> Should we not do a gcc-plugin or something that fixes that particular
> UB? Shift acting all retarded like that is just annoying. I feel we
> should eliminate UBs from the language where possible, like
> -fno-strict-overflow mandates 2s complement.
No idea, if someone does this they can remove the above overhead..
> > *_d = (_a << _to_shift);
> >
> > // s is malformed
> > (_to_shift != _s ||
>
> Not strictly an overflow though, just not expected behaviour.
'overflow' here means the math didn't work, ie
C = A << B
has a C that does not match A << B done on infinite precision. It is
not limited to checking overflow.
> > // d is a signed type and became negative
> > *_d < 0 ||
>
> Only a problem if it wasn't negative to start out with.
> > // a is a signed type and was negative
> > _a < 0 ||
>
> Why would that be a problem? You can shift left negative values just
> fine. The only problem is when you run out of sign bits.
These are both a problem because of how the macro is setup, nobody had
an idea how to make this work with different types and allow for
negatives to work properly.
You could define this, but since there is no usecase..
> > // Not invertable means a was truncated during shifting
> > (*_d >> _to_shift) != a))
> > })
>
> And I'm not exactly seeing the use case for this macro. What's the point
> of a shift-left if you cannot truncate bits. I suppose it's in the name
> _overflow, but still.
It is basically a specialized case of check_mul_overflow where the
multiply is known to be a power of 2.
Jason
Powered by blists - more mailing lists