Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

+105 -47
+1 -15
tools/perf/Makefile
··· 377 377 LIB_H += util/include/linux/string.h 378 378 LIB_H += util/include/linux/types.h 379 379 LIB_H += util/include/asm/asm-offsets.h 380 - LIB_H += util/include/asm/bitops.h 381 380 LIB_H += util/include/asm/bug.h 382 381 LIB_H += util/include/asm/byteorder.h 382 + LIB_H += util/include/asm/hweight.h 383 383 LIB_H += util/include/asm/swab.h 384 384 LIB_H += util/include/asm/system.h 385 385 LIB_H += util/include/asm/uaccess.h ··· 435 435 LIB_OBJS += $(OUTPUT)util/rbtree.o 436 436 LIB_OBJS += $(OUTPUT)util/bitmap.o 437 437 LIB_OBJS += $(OUTPUT)util/hweight.o 438 - LIB_OBJS += $(OUTPUT)util/find_next_bit.o 439 438 LIB_OBJS += $(OUTPUT)util/run-command.o 440 439 LIB_OBJS += $(OUTPUT)util/quote.o 441 440 LIB_OBJS += $(OUTPUT)util/strbuf.o ··· 945 946 $(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $< 946 947 947 948 $(OUTPUT)util/rbtree.o: ../../lib/rbtree.c $(OUTPUT)PERF-CFLAGS 948 - $(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $< 949 - 950 - # some perf warning policies can't fit to lib/bitmap.c, eg: it warns about variable shadowing 951 - # from <string.h> that comes from kernel headers wrapping. 952 - KBITMAP_FLAGS=`echo $(ALL_CFLAGS) | sed s/-Wshadow// | sed s/-Wswitch-default// | sed s/-Wextra//` 953 - 954 - $(OUTPUT)util/bitmap.o: ../../lib/bitmap.c $(OUTPUT)PERF-CFLAGS 955 - $(QUIET_CC)$(CC) -o $@ -c $(KBITMAP_FLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $< 956 - 957 - $(OUTPUT)util/hweight.o: ../../lib/hweight.c $(OUTPUT)PERF-CFLAGS 958 - $(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $< 959 - 960 - $(OUTPUT)util/find_next_bit.o: ../../lib/find_next_bit.c $(OUTPUT)PERF-CFLAGS 961 949 $(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $< 962 950 963 951 $(OUTPUT)util/scripting-engines/trace-event-perl.o: util/scripting-engines/trace-event-perl.c $(OUTPUT)PERF-CFLAGS
+21
tools/perf/util/bitmap.c
··· 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 + }
+31
tools/perf/util/hweight.c
··· 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 + }
-18
tools/perf/util/include/asm/bitops.h
··· 1 - #ifndef _PERF_ASM_BITOPS_H_ 2 - #define _PERF_ASM_BITOPS_H_ 3 - 4 - #include <sys/types.h> 5 - #include "../../types.h" 6 - #include <linux/compiler.h> 7 - 8 - /* CHECKME: Not sure both always match */ 9 - #define BITS_PER_LONG __WORDSIZE 10 - 11 - #include "../../../../include/asm-generic/bitops/__fls.h" 12 - #include "../../../../include/asm-generic/bitops/fls.h" 13 - #include "../../../../include/asm-generic/bitops/fls64.h" 14 - #include "../../../../include/asm-generic/bitops/__ffs.h" 15 - #include "../../../../include/asm-generic/bitops/ffz.h" 16 - #include "../../../../include/asm-generic/bitops/hweight.h" 17 - 18 - #endif
+8
tools/perf/util/include/asm/hweight.h
··· 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 */
+35 -3
tools/perf/util/include/linux/bitmap.h
··· 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 */
+9 -11
tools/perf/util/include/linux/bitops.h
··· 1 1 #ifndef _PERF_LINUX_BITOPS_H_ 2 2 #define _PERF_LINUX_BITOPS_H_ 3 3 4 - #define __KERNEL__ 4 + #include <linux/kernel.h> 5 + #include <asm/hweight.h> 5 6 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)) 11 10 12 11 static inline void set_bit(int nr, unsigned long *addr) 13 12 { ··· 19 20 (((unsigned long *)addr)[nr / BITS_PER_LONG])) != 0; 20 21 } 21 22 22 - unsigned long generic_find_next_zero_le_bit(const unsigned long *addr, unsigned 23 - long size, unsigned long offset); 24 - 25 - unsigned long generic_find_next_le_bit(const unsigned long *addr, unsigned 26 - long size, unsigned long offset); 23 + static inline unsigned long hweight_long(unsigned long w) 24 + { 25 + return sizeof(w) == 4 ? hweight32(w) : hweight64(w); 26 + } 27 27 28 28 #endif