Skip to content

Commit fb72014

Browse files
committed
perf tools: Don't use code surrounded by __KERNEL__
We need to refactor code to be explicitely shared by the kernel and at least the tools/ userspace programs, so, till we do that, copy the bare minimum bitmap/bitops code needed by tools/perf. Reported-by: "H. Peter Anvin" <hpa@zytor.com> Cc: Frédéric Weisbecker <fweisbec@gmail.com> Cc: Mike Galbraith <efault@gmx.de> Cc: Paul Mackerras <paulus@samba.org> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> LKML-Reference: <new-submission> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent bc4b473 commit fb72014

7 files changed

Lines changed: 105 additions & 47 deletions

File tree

‎tools/perf/Makefile‎

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,9 @@ LIB_H += util/include/linux/rbtree.h
377377
LIB_H += util/include/linux/string.h
378378
LIB_H += util/include/linux/types.h
379379
LIB_H += util/include/asm/asm-offsets.h
380-
LIB_H += util/include/asm/bitops.h
381380
LIB_H += util/include/asm/bug.h
382381
LIB_H += util/include/asm/byteorder.h
382+
LIB_H += util/include/asm/hweight.h
383383
LIB_H += util/include/asm/swab.h
384384
LIB_H += util/include/asm/system.h
385385
LIB_H += util/include/asm/uaccess.h
@@ -435,7 +435,6 @@ LIB_OBJS += $(OUTPUT)util/path.o
435435
LIB_OBJS += $(OUTPUT)util/rbtree.o
436436
LIB_OBJS += $(OUTPUT)util/bitmap.o
437437
LIB_OBJS += $(OUTPUT)util/hweight.o
438-
LIB_OBJS += $(OUTPUT)util/find_next_bit.o
439438
LIB_OBJS += $(OUTPUT)util/run-command.o
440439
LIB_OBJS += $(OUTPUT)util/quote.o
441440
LIB_OBJS += $(OUTPUT)util/strbuf.o
@@ -948,19 +947,6 @@ $(OUTPUT)util/config.o: util/config.c $(OUTPUT)PERF-CFLAGS
948947
$(OUTPUT)util/rbtree.o: ../../lib/rbtree.c $(OUTPUT)PERF-CFLAGS
949948
$(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $<
950949

951-
# some perf warning policies can't fit to lib/bitmap.c, eg: it warns about variable shadowing
952-
# from <string.h> that comes from kernel headers wrapping.
953-
KBITMAP_FLAGS=`echo $(ALL_CFLAGS) | sed s/-Wshadow// | sed s/-Wswitch-default// | sed s/-Wextra//`
954-
955-
$(OUTPUT)util/bitmap.o: ../../lib/bitmap.c $(OUTPUT)PERF-CFLAGS
956-
$(QUIET_CC)$(CC) -o $@ -c $(KBITMAP_FLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $<
957-
958-
$(OUTPUT)util/hweight.o: ../../lib/hweight.c $(OUTPUT)PERF-CFLAGS
959-
$(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $<
960-
961-
$(OUTPUT)util/find_next_bit.o: ../../lib/find_next_bit.c $(OUTPUT)PERF-CFLAGS
962-
$(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $<
963-
964950
$(OUTPUT)util/scripting-engines/trace-event-perl.o: util/scripting-engines/trace-event-perl.c $(OUTPUT)PERF-CFLAGS
965951
$(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) $(PERL_EMBED_CCOPTS) -Wno-redundant-decls -Wno-strict-prototypes -Wno-unused-parameter -Wno-shadow $<
966952

‎tools/perf/util/bitmap.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* From lib/bitmap.c
3+
* Helper functions for bitmap.h.
4+
*
5+
* This source code is licensed under the GNU General Public License,
6+
* Version 2. See the file COPYING for more details.
7+
*/
8+
#include <linux/bitmap.h>
9+
10+
int __bitmap_weight(const unsigned long *bitmap, int bits)
11+
{
12+
int k, w = 0, lim = bits/BITS_PER_LONG;
13+
14+
for (k = 0; k < lim; k++)
15+
w += hweight_long(bitmap[k]);
16+
17+
if (bits % BITS_PER_LONG)
18+
w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
19+
20+
return w;
21+
}

‎tools/perf/util/hweight.c‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <linux/bitops.h>
2+
3+
/**
4+
* hweightN - returns the hamming weight of a N-bit word
5+
* @x: the word to weigh
6+
*
7+
* The Hamming Weight of a number is the total number of bits set in it.
8+
*/
9+
10+
unsigned int hweight32(unsigned int w)
11+
{
12+
unsigned int res = w - ((w >> 1) & 0x55555555);
13+
res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
14+
res = (res + (res >> 4)) & 0x0F0F0F0F;
15+
res = res + (res >> 8);
16+
return (res + (res >> 16)) & 0x000000FF;
17+
}
18+
19+
unsigned long hweight64(__u64 w)
20+
{
21+
#if BITS_PER_LONG == 32
22+
return hweight32((unsigned int)(w >> 32)) + hweight32((unsigned int)w);
23+
#elif BITS_PER_LONG == 64
24+
__u64 res = w - ((w >> 1) & 0x5555555555555555ul);
25+
res = (res & 0x3333333333333333ul) + ((res >> 2) & 0x3333333333333333ul);
26+
res = (res + (res >> 4)) & 0x0F0F0F0F0F0F0F0Ful;
27+
res = res + (res >> 8);
28+
res = res + (res >> 16);
29+
return (res + (res >> 32)) & 0x00000000000000FFul;
30+
#endif
31+
}

‎tools/perf/util/include/asm/bitops.h‎

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef PERF_HWEIGHT_H
2+
#define PERF_HWEIGHT_H
3+
4+
#include <linux/types.h>
5+
unsigned int hweight32(unsigned int w);
6+
unsigned long hweight64(__u64 w);
7+
8+
#endif /* PERF_HWEIGHT_H */
Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1-
#include "../../../../include/linux/bitmap.h"
2-
#include "../../../../include/asm-generic/bitops/find.h"
3-
#include <linux/errno.h>
1+
#ifndef _PERF_BITOPS_H
2+
#define _PERF_BITOPS_H
3+
4+
#include <string.h>
5+
#include <linux/bitops.h>
6+
7+
int __bitmap_weight(const unsigned long *bitmap, int bits);
8+
9+
#define BITMAP_LAST_WORD_MASK(nbits) \
10+
( \
11+
((nbits) % BITS_PER_LONG) ? \
12+
(1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
13+
)
14+
15+
#define small_const_nbits(nbits) \
16+
(__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
17+
18+
static inline void bitmap_zero(unsigned long *dst, int nbits)
19+
{
20+
if (small_const_nbits(nbits))
21+
*dst = 0UL;
22+
else {
23+
int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
24+
memset(dst, 0, len);
25+
}
26+
}
27+
28+
static inline int bitmap_weight(const unsigned long *src, int nbits)
29+
{
30+
if (small_const_nbits(nbits))
31+
return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
32+
return __bitmap_weight(src, nbits);
33+
}
34+
35+
#endif /* _PERF_BITOPS_H */
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#ifndef _PERF_LINUX_BITOPS_H_
22
#define _PERF_LINUX_BITOPS_H_
33

4-
#define __KERNEL__
4+
#include <linux/kernel.h>
5+
#include <asm/hweight.h>
56

6-
#define CONFIG_GENERIC_FIND_NEXT_BIT
7-
#define CONFIG_GENERIC_FIND_FIRST_BIT
8-
#include "../../../../include/linux/bitops.h"
9-
10-
#undef __KERNEL__
7+
#define BITS_PER_LONG __WORDSIZE
8+
#define BITS_PER_BYTE 8
9+
#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
1110

1211
static inline void set_bit(int nr, unsigned long *addr)
1312
{
@@ -20,10 +19,9 @@ static __always_inline int test_bit(unsigned int nr, const unsigned long *addr)
2019
(((unsigned long *)addr)[nr / BITS_PER_LONG])) != 0;
2120
}
2221

23-
unsigned long generic_find_next_zero_le_bit(const unsigned long *addr, unsigned
24-
long size, unsigned long offset);
25-
26-
unsigned long generic_find_next_le_bit(const unsigned long *addr, unsigned
27-
long size, unsigned long offset);
22+
static inline unsigned long hweight_long(unsigned long w)
23+
{
24+
return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
25+
}
2826

2927
#endif

0 commit comments

Comments
 (0)