linux/include
Rasmus Villemoes ef4d6f6b27 include/linux/bitops.h: sanitize rotate primitives
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.

Link: http://lkml.kernel.org/r/20190410211906.2190-1-linux@rasmusvillemoes.dk
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Reported-by: Ido Schimmel <idosch@mellanox.com>
Tested-by: Ido Schimmel <idosch@mellanox.com>
Reviewed-by: Will Deacon <will.deacon@arm.com>
Cc: Vadim Pasternak <vadimp@mellanox.com>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Jacek Anaszewski <jacek.anaszewski@gmail.com>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-05-14 19:52:49 -07:00
..
acpi Driver core/kobject patches for 5.2-rc1 2019-05-07 13:01:40 -07:00
asm-generic treewide: remove SPDX "WITH Linux-syscall-note" from kernel-space headers 2019-05-14 19:52:48 -07:00
clocksource
crypto
drm drm pull request for 5.2 2019-05-08 21:35:19 -07:00
dt-bindings We have a couple new features and changes in the core clk framework this time 2019-05-09 14:50:09 -07:00
keys
kvm
linux include/linux/bitops.h: sanitize rotate primitives 2019-05-14 19:52:49 -07:00
math-emu
media drm pull request for 5.2 2019-05-08 21:35:19 -07:00
memory
misc ocxl: Provide global MMIO accessors for external drivers 2019-05-03 02:55:02 +10:00
net net: dsa: Remove the now unused DSA_SKB_CB_COPY() macro 2019-05-12 13:19:46 -07:00
pcmcia
ras
rdma RDMA: Add EFA related definitions 2019-05-06 13:47:50 -03:00
scsi
soc This pull request contains the following changes for MTD: 2019-05-12 17:57:52 -04:00
sound sound updates for 5.2-rc1 2019-05-09 08:26:55 -07:00
target
trace Merge branch 'akpm' (patches from Andrew) 2019-05-14 10:10:55 -07:00
uapi pci-v5.2-changes 2019-05-14 10:30:10 -07:00
video
xen