Files
kernel_google_b1c1/include/linux
Rasmus Villemoes e186b19bc3 include/linux/bitops.h: sanitize rotate primitives
commit ef4d6f6b275c498f8e5626c99dbeefdc5027f843 upstream.

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>
Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-06-11 12:22:36 +02:00
..
2019-04-17 08:36:45 +02:00
2016-08-25 11:26:48 -04:00
2019-04-20 09:07:54 +02:00
2017-10-08 10:26:06 +02:00
2018-04-24 09:34:18 +02:00
2016-09-14 09:18:06 -06:00
2016-08-11 09:41:35 -06:00
2017-06-14 15:06:00 +02:00
2018-02-28 10:18:33 +01:00
2019-03-23 13:19:52 +01:00
2017-07-15 12:16:11 +02:00
2017-05-14 14:00:22 +02:00
2017-07-12 15:01:02 +02:00
2017-08-06 18:59:43 -07:00
2016-09-24 10:48:18 +02:00
2016-10-20 15:51:28 +11:00
2016-09-16 09:34:15 +01:00
2016-09-14 09:18:09 -06:00
2018-10-10 08:53:18 +02:00
2016-08-10 11:23:44 -04:00
2016-09-15 16:49:39 +02:00
2016-09-27 12:33:47 +02:00
2017-08-24 17:12:19 -07:00
2019-05-10 17:52:09 +02:00
2016-07-29 12:17:52 -07:00
2016-08-28 23:32:41 -04:00
2016-10-19 11:36:22 -06:00
2017-08-24 17:12:21 -07:00
2016-10-14 11:36:59 -07:00
2016-09-27 21:52:00 -04:00
2016-09-08 15:01:10 -07:00
2016-09-08 22:15:25 -07:00
2017-08-30 10:21:40 +02:00
2016-08-28 23:44:55 -04:00
2016-10-05 18:23:36 -04:00
2019-04-17 08:36:39 +02:00
2018-11-13 11:17:02 -08:00
2018-11-27 16:09:41 +01:00
2017-04-21 09:31:21 +02:00
2017-12-25 14:23:37 +01:00
2016-09-30 10:54:03 +02:00
2018-12-05 19:42:42 +01:00