]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blame - releases/4.9.176/0012-bitops-avoid-integer-overflow-in-GENMASK-_ULL.patch
Linux 4.9.176
[thirdparty/kernel/stable-queue.git] / releases / 4.9.176 / 0012-bitops-avoid-integer-overflow-in-GENMASK-_ULL.patch
CommitLineData
6fa88700
GKH
1From a2783e3dab88b3062ed1612187b90c7fc36d67a9 Mon Sep 17 00:00:00 2001
2From: Matthias Kaehlcke <mka@chromium.org>
3Date: Fri, 8 Sep 2017 16:14:33 -0700
4Subject: [PATCH 12/76] bitops: avoid integer overflow in GENMASK(_ULL)
5
6commit c32ee3d9abd284b4fcaacc250b101f93829c7bae upstream.
7
8GENMASK(_ULL) performs a left-shift of ~0UL(L), which technically
9results in an integer overflow. clang raises a warning if the overflow
10occurs in a preprocessor expression. Clear the low-order bits through a
11substraction instead of the left-shift to avoid the overflow.
12
13(akpm: no change in .text size in my testing)
14
15Link: http://lkml.kernel.org/r/20170803212020.24939-1-mka@chromium.org
16Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
17Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
18Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
19Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
20Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
21---
22 include/linux/bitops.h | 5 +++--
23 1 file changed, 3 insertions(+), 2 deletions(-)
24
25diff --git a/include/linux/bitops.h b/include/linux/bitops.h
26index a83c822c35c2..8fbe259b197c 100644
27--- a/include/linux/bitops.h
28+++ b/include/linux/bitops.h
29@@ -19,10 +19,11 @@
30 * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
31 */
32 #define GENMASK(h, l) \
33- (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
34+ (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
35
36 #define GENMASK_ULL(h, l) \
37- (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
38+ (((~0ULL) - (1ULL << (l)) + 1) & \
39+ (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
40
41 extern unsigned int __sw_hweight8(unsigned int w);
42 extern unsigned int __sw_hweight16(unsigned int w);
43--
442.21.0
45