All of lore.kernel.org
 help / color / mirror / Atom feed
From: Will Deacon <will.deacon@arm.com>
To: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Vineet Gupta <vineet.gupta1@synopsys.com>,
	Anthony Yznaga <anthony.yznaga@oracle.com>,
	Andrey Ryabinin <aryabinin@virtuozzo.com>,
	Pavel Machek <pavel@ucw.cz>, Ido Schimmel <idosch@mellanox.com>,
	Vadim Pasternak <vadimp@mellanox.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] bitops.h: sanitize rotate primitives
Date: Tue, 16 Apr 2019 16:21:11 +0100	[thread overview]
Message-ID: <20190416152111.GC4187@fuggles.cambridge.arm.com> (raw)
In-Reply-To: <20190410211906.2190-1-linux@rasmusvillemoes.dk>

On Wed, Apr 10, 2019 at 11:19:06PM +0200, Rasmus Villemoes wrote:
> The ror32 implementation (word >> shift) | (word << (32 - shift) has
> undefined behaviour if shift is outside the [1, 31] range. Similarly
> for the 64 bit variants. Most callers pass a compile-time
> constant (naturally in that range), but there's an UBSAN report that
> these may actually be called with a shift count of 0.
> 
> Instead of special-casing that, we can make them DTRT for all values
> of shift while also avoiding UB. For some reason, this was already
> partly done for rol32 (which was well-defined for [0, 31]). gcc 8
> recognizes these patterns as rotates, so for example
> 
> __u32 rol32(__u32 word, unsigned int shift)
> {
> 	return (word << (shift & 31)) | (word >> ((-shift) & 31));
> }
> 
> compiles to
> 
> 0000000000000020 <rol32>:
>   20:   89 f8                   mov    %edi,%eax
>   22:   89 f1                   mov    %esi,%ecx
>   24:   d3 c0                   rol    %cl,%eax
>   26:   c3                      retq
> 
> Older compilers unfortunately do not do as well, but this only affects
> the small minority of users that don't pass constants.
> 
> Due to integer promotions, ro[lr]8 were already well-defined for
> shifts in [0, 8], and ro[lr]16 were mostly well-defined for shifts in
> [0, 16] (only mostly - u16 gets promoted to _signed_ int, so if bit 15
> is set, word << 16 is undefined). For consistency, update those as
> well.
> 
> Reported-by: Ido Schimmel <idosch@mellanox.com>
> Cc: Vadim Pasternak <vadimp@mellanox.com>
> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> ---
>  include/linux/bitops.h | 16 ++++++++--------
>  1 file changed, 8 insertions(+), 8 deletions(-)

Reviewed-by: Will Deacon <will.deacon@arm.com>

I guess it would be possible to roll some of this up into macros using
sizeof, but perhaps that would make things even more difficult for the
compiler.

Will

      parent reply	other threads:[~2019-04-16 15:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-10 21:19 [PATCH] bitops.h: sanitize rotate primitives Rasmus Villemoes
2019-04-11 17:49 ` Ido Schimmel
2019-04-16 15:21 ` Will Deacon [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190416152111.GC4187@fuggles.cambridge.arm.com \
    --to=will.deacon@arm.com \
    --cc=akpm@linux-foundation.org \
    --cc=anthony.yznaga@oracle.com \
    --cc=aryabinin@virtuozzo.com \
    --cc=idosch@mellanox.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=pavel@ucw.cz \
    --cc=vadimp@mellanox.com \
    --cc=vineet.gupta1@synopsys.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.