Skip to content

Commit fc92099

Browse files
committed
tools headers: Synchronize linux/bits.h with the kernel sources
To pick up the changes in this cset: 1e7933a ("uapi: Revert "bitops: avoid integer overflow in GENMASK(_ULL)"") 5b572e8 ("bits: introduce fixed-type BIT_U*()") 1940820 ("bits: introduce fixed-type GENMASK_U*()") 31299a5 ("bits: add comments and newlines to #if, #else and #endif directives") This addresses these perf build warnings: Warning: Kernel ABI header differences: diff -u tools/include/linux/bits.h include/linux/bits.h Please see tools/include/uapi/README for further details. Acked-by: Vincent Mailhol <[email protected]> Cc: I Hsin Cheng <[email protected]> Cc: Yury Norov <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Ian Rogers <[email protected]> Cc: James Clark <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Kan Liang <[email protected]> Cc: Lucas De Marchi <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Yury Norov <[email protected]> Link: https://lore.kernel.org/r/aEr0ZJ60EbshEy6p@x1 Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent bb6b414 commit fc92099

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

include/uapi/linux/bits.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
#ifndef _UAPI_LINUX_BITS_H
55
#define _UAPI_LINUX_BITS_H
66

7-
#define __GENMASK(h, l) (((~_UL(0)) << (l)) & (~_UL(0) >> (__BITS_PER_LONG - 1 - (h))))
7+
#define __GENMASK(h, l) (((~_UL(0)) << (l)) & (~_UL(0) >> (BITS_PER_LONG - 1 - (h))))
88

9-
#define __GENMASK_ULL(h, l) (((~_ULL(0)) << (l)) & (~_ULL(0) >> (__BITS_PER_LONG_LONG - 1 - (h))))
9+
#define __GENMASK_ULL(h, l) (((~_ULL(0)) << (l)) & (~_ULL(0) >> (BITS_PER_LONG_LONG - 1 - (h))))
1010

1111
#define __GENMASK_U128(h, l) \
1212
((_BIT128((h)) << 1) - (_BIT128(l)))

tools/include/linux/bits.h

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,76 @@
1212
#define BIT_ULL_MASK(nr) (ULL(1) << ((nr) % BITS_PER_LONG_LONG))
1313
#define BIT_ULL_WORD(nr) ((nr) / BITS_PER_LONG_LONG)
1414
#define BITS_PER_BYTE 8
15+
#define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE)
1516

1617
/*
1718
* Create a contiguous bitmask starting at bit position @l and ending at
1819
* position @h. For example
1920
* GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
2021
*/
2122
#if !defined(__ASSEMBLY__)
23+
24+
/*
25+
* Missing asm support
26+
*
27+
* GENMASK_U*() and BIT_U*() depend on BITS_PER_TYPE() which relies on sizeof(),
28+
* something not available in asm. Nevertheless, fixed width integers is a C
29+
* concept. Assembly code can rely on the long and long long versions instead.
30+
*/
31+
2232
#include <linux/build_bug.h>
2333
#include <linux/compiler.h>
34+
#include <linux/overflow.h>
35+
2436
#define GENMASK_INPUT_CHECK(h, l) BUILD_BUG_ON_ZERO(const_true((l) > (h)))
25-
#else
37+
38+
/*
39+
* Generate a mask for the specified type @t. Additional checks are made to
40+
* guarantee the value returned fits in that type, relying on
41+
* -Wshift-count-overflow compiler check to detect incompatible arguments.
42+
* For example, all these create build errors or warnings:
43+
*
44+
* - GENMASK(15, 20): wrong argument order
45+
* - GENMASK(72, 15): doesn't fit unsigned long
46+
* - GENMASK_U32(33, 15): doesn't fit in a u32
47+
*/
48+
#define GENMASK_TYPE(t, h, l) \
49+
((t)(GENMASK_INPUT_CHECK(h, l) + \
50+
(type_max(t) << (l) & \
51+
type_max(t) >> (BITS_PER_TYPE(t) - 1 - (h)))))
52+
53+
#define GENMASK_U8(h, l) GENMASK_TYPE(u8, h, l)
54+
#define GENMASK_U16(h, l) GENMASK_TYPE(u16, h, l)
55+
#define GENMASK_U32(h, l) GENMASK_TYPE(u32, h, l)
56+
#define GENMASK_U64(h, l) GENMASK_TYPE(u64, h, l)
57+
58+
/*
59+
* Fixed-type variants of BIT(), with additional checks like GENMASK_TYPE(). The
60+
* following examples generate compiler warnings due to -Wshift-count-overflow:
61+
*
62+
* - BIT_U8(8)
63+
* - BIT_U32(-1)
64+
* - BIT_U32(40)
65+
*/
66+
#define BIT_INPUT_CHECK(type, nr) \
67+
BUILD_BUG_ON_ZERO(const_true((nr) >= BITS_PER_TYPE(type)))
68+
69+
#define BIT_TYPE(type, nr) ((type)(BIT_INPUT_CHECK(type, nr) + BIT_ULL(nr)))
70+
71+
#define BIT_U8(nr) BIT_TYPE(u8, nr)
72+
#define BIT_U16(nr) BIT_TYPE(u16, nr)
73+
#define BIT_U32(nr) BIT_TYPE(u32, nr)
74+
#define BIT_U64(nr) BIT_TYPE(u64, nr)
75+
76+
#else /* defined(__ASSEMBLY__) */
77+
2678
/*
2779
* BUILD_BUG_ON_ZERO is not available in h files included from asm files,
2880
* disable the input check if that is the case.
2981
*/
3082
#define GENMASK_INPUT_CHECK(h, l) 0
31-
#endif
83+
84+
#endif /* !defined(__ASSEMBLY__) */
3285

3386
#define GENMASK(h, l) \
3487
(GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))

0 commit comments

Comments
 (0)