tools/perf: fix the word selected in find_*_bit
Subsystems (2)
PATCH 1/1 tools/perf: fix the word selected in find_*_bit
When decoding the perf_regs mask in regs_dump__printf(),
we loop through the mask using find_first_bit and find_next_bit functions. And mask is of type "u64". But "u64" is send as a "unsigned long *" to lib functions along with sizeof().
While the exisitng code works fine in most of the case, when using a 32bit perf on a 64bit kernel (Big Endian), we end reading the wrong word. In find_first_bit(), one word at a time (based on BITS_PER_LONG) is loaded and
checked for any bit set. In 32bit BE userspace,BITS_PER_LONG turns out to be 32, and for a mask value of "0x00000000000000ff", find_first_bit will return 32, instead of 0. Reason for this is that, value in the word0 is all zeros and value
in word1 is 0xff. Ideally, second word in the mask should be loaded and searched. Patch swaps the word to look incase of 32bit BE.
--- tools/lib/find_bit.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
tools/lib/find_bit.c +17 -0
@@ -37,7 +37,12 @@ static unsigned long _find_next_bit(const unsigned long *addr,if (!nbits || start >= nbits)return nbits; +#if (__BYTE_ORDER == __BIG_ENDIAN) && (BITS_PER_LONG !=64 ) + tmp = addr[(((nbits -1 )/BITS_PER_LONG ) - (start /BITS_PER_LONG ))] + ^ invert; +#else tmp = addr[start /BITS_PER_LONG ] ^ invert; +#endif /* Handle 1st word. */ tmp &=BITMAP_FIRST_WORD_MASK (start);
@@ -48,7 +53,12 @@ static unsigned long _find_next_bit(const unsigned long *addr,if (start >= nbits)return nbits; +#if (__BYTE_ORDER == __BIG_ENDIAN) && (BITS_PER_LONG !=64 ) + tmp = addr[(((nbits -1 )/BITS_PER_LONG ) - (start /BITS_PER_LONG ))] + ^ invert; +#else tmp = addr[start /BITS_PER_LONG ] ^ invert; +#endif }return min (start +__ffs (tmp), nbits);
@@ -75,8 +85,15 @@ unsigned long find_first_bit(const unsigned long *addr, unsigned long size)unsigned long idx;for (idx =0 ; idx *BITS_PER_LONG < size; idx++) { +#if (__BYTE_ORDER == __BIG_ENDIAN) && (BITS_PER_LONG !=64 ) +if (addr[(((size-1 )/BITS_PER_LONG ) - idx)]) +return min (idx *BITS_PER_LONG + +__ffs (addr[(((size-1 )/BITS_PER_LONG ) - idx)]), + size); +#else if (addr[idx])return min (idx *BITS_PER_LONG +__ffs (addr[idx]), size); +#endif }return size;
--
1.9.1
tools/lib/find_bit.c static unsigned long _find_next_bit(const unsigned long *addr, · line 45
Quoted 6 lines · Madhavan Srinivasan
tools/lib/find_bit.c +5
-0+#if (__BYTE_ORDER == __BIG_ENDIAN) && (BITS_PER_LONG !=64 ) + tmp = addr[(((nbits -1 )/BITS_PER_LONG ) - (start /BITS_PER_LONG ))] + ^ invert; +#else tmp = addr[start /BITS_PER_LONG ] ^ invert; +#endif
Than you for diagnosing this problem, but I don't think the fix is correct.
1) It's not clear that all users of _find_next_bit and for_each_set_bit() want this change.
2) Is your code even correct? I'd think you'd want addr[x ^ 1]. Are you sure you shpuld be reversing the whole array, and not just the halves of each 64-bit word?
3) You've now broken the case of 32-bit big-endian kernel.
I think the proper solution is uglier than this. :-(
Hi Madhavan,
Quoted 5 lines · Madhavan Srinivasan
When decoding the
perf_regsmask inregs_dump__printf(),
we loop through the mask usingfind_first_bitandfind_next_bitfunctions. And mask is of type "u64". But "u64" is send as a "unsigned long *" to lib functions along withsizeof().
Quoted 9 lines
While the exisitng code works fine in most of the case, when using a 32bit perf on a 64bit kernel (Big Endian), we end reading the wrong word. In
find_first_bit(), one word at a time (based onBITS_PER_LONG) is loaded and
checked for any bit set. In 32bit BE userspace,BITS_PER_LONGturns out to be 32, and for a mask value of "0x00000000000000ff",find_first_bitwill return 32, instead of 0. Reason for this is that, value in the word0 is all zeros and value
in word1 is0xff. Ideally, second word in the mask should be loaded and searched. Patch swaps the word to look incase of 32bit BE.
I think this is not a problem of find_bit() at all. You have wrong typecast as the source of problem (tools/perf/util/session.c"):
940 static void regs_dump__printf(u64 mask, u64 *regs) 941 {
942 unsigned rid, i = 0;
943
944 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
^^^^ Here ^^^^945 u64 val = regs[i++];
946
947 printf(".... %-5s 0x%" PRIx64 "\n",
948 perf_reg_name(rid), val);
949 }
950 }
But for some reason you change correct find_bit()...
Though proper fix is like this for me:
static void regs_dump__printf(u64 mask, u64 *regs)
{
unsigned rid, i = 0;
unsigned long _mask[sizeof(mask)/sizeof(unsigned long)];
_mask[0] = mask & ULONG_MAX;
if (sizeof(mask) > sizeof(unsigned long))
_mask[1] = mask >> BITS_PER_LONG;
for_each_set_bit(rid, _mask, sizeof(mask) * BITS_PER_BYTE) {
u64 val = regs[i++];
printf(".... %-5s 0x%" PRIx64 "\n",
perf_reg_name(rid), val);
}
}Maybe there already is some macro doing the conversion for you...
Yury.
Quoted 64 lines
Cc Arnaldo Carvalho de Melo <[email protected]>Cc Adrian Hunter <[email protected]>Cc Borislav Petkov <[email protected]>Cc David Ahern <[email protected]>Cc George Spelvin <[email protected]>Cc Jiri Olsa <[email protected]>Cc Namhyung Kim <[email protected]>Cc Rasmus Villemoes <[email protected]>Cc Wang Nan <[email protected]>Cc Yury Norov <[email protected]>Cc Michael Ellerman <[email protected]>Signed-off-by Madhavan Srinivasan <[email protected]>--- tools/lib/find_bit.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)tools/lib/find_bit.c +17
-1@@ -37,7 +37,12 @@ static unsigned long _find_next_bit(const unsigned long *addr,if (!nbits || start >= nbits)return nbits; +#if (__BYTE_ORDER == __BIG_ENDIAN) && (BITS_PER_LONG !=64 ) + tmp = addr[(((nbits -1 )/BITS_PER_LONG ) - (start /BITS_PER_LONG ))] + ^ invert; +#else tmp = addr[start /BITS_PER_LONG ] ^ invert; +#endif /* Handle 1st word. */ tmp &=BITMAP_FIRST_WORD_MASK (start);@@ -48,7 +53,12 @@ static unsigned long _find_next_bit(const unsigned long *addr,if (start >= nbits)return nbits; +#if (__BYTE_ORDER == __BIG_ENDIAN) && (BITS_PER_LONG !=64 ) + tmp = addr[(((nbits -1 )/BITS_PER_LONG ) - (start /BITS_PER_LONG ))] + ^ invert; +#else tmp = addr[start /BITS_PER_LONG ] ^ invert; +#endif }return min (start +__ffs (tmp), nbits);@@ -75,8 +85,15 @@ unsigned long find_first_bit(const unsigned long *addr, unsigned long size)unsigned long idx;for (idx =0 ; idx *BITS_PER_LONG < size; idx++) { +#if (__BYTE_ORDER == __BIG_ENDIAN) && (BITS_PER_LONG !=64 ) +if (addr[(((size-1 )/BITS_PER_LONG ) - idx)]) +return min (idx *BITS_PER_LONG + +__ffs (addr[(((size-1 )/BITS_PER_LONG ) - idx)]), + size); +#else if (addr[idx])return min (idx *BITS_PER_LONG +__ffs (addr[idx]), size); +#endif }return size; --1.9.1
Quoted 55 lines · Yury Norov
Hi Madhavan,
Quoted 5 lines · Madhavan Srinivasan
When decoding the
perf_regsmask inregs_dump__printf(),
we loop through the mask usingfind_first_bitandfind_next_bitfunctions. And mask is of type "u64". But "u64" is send as a "unsigned long *" to lib functions along withsizeof().Quoted 9 lines
While the exisitng code works fine in most of the case, when using a 32bit perf on a 64bit kernel (Big Endian), we end reading the wrong word. In
find_first_bit(), one word at a time (based onBITS_PER_LONG) is loaded and
checked for any bit set. In 32bit BE userspace,BITS_PER_LONGturns out to be 32, and for a mask value of "0x00000000000000ff",find_first_bitwill return 32, instead of 0. Reason for this is that, value in the word0 is all zeros and value
in word1 is0xff. Ideally, second word in the mask should be loaded and searched. Patch swaps the word to look incase of 32bit BE.I think this is not a problem of
find_bit()at all. You have wrong typecast as the source of problem (tools/perf/util/session.c"):940 static void
regs_dump__printf(u64 mask, u64 *regs)941 {
942 unsigned rid, i = 0;
943944 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) { ^^^^ Here ^^^^945 u64 val = regs[i++];
946
947 printf(".... %-5s 0x%" PRIx64 "\n",
948perf_reg_name(rid), val);
949 }
950 }But for some reason you change correct
find_bit()...Though proper fix is like this for me:
static void
regs_dump__printf(u64 mask, u64 *regs){ unsigned rid, i = 0; unsigned long _mask[sizeof(mask)/sizeof(unsigned long)]; _mask[0] = mask & ULONG_MAX; if (sizeof(mask) > sizeof(unsigned long)) _mask[1] = mask >> BITS_PER_LONG; for_each_set_bit(rid, _mask, sizeof(mask) * BITS_PER_BYTE) { u64 val = regs[i++]; printf(".... %-5s 0x%" PRIx64 "\n", perf_reg_name(rid), val); } }
Quoted 1 line
Maybe there already is some macro doing the conversion for you...
yes it is, cpu_to_le64() is what you want
Quoted 67 lines
Yury.
Quoted 64 lines
Cc Arnaldo Carvalho de Melo <[email protected]>Cc Adrian Hunter <[email protected]>Cc Borislav Petkov <[email protected]>Cc David Ahern <[email protected]>Cc George Spelvin <[email protected]>Cc Jiri Olsa <[email protected]>Cc Namhyung Kim <[email protected]>Cc Rasmus Villemoes <[email protected]>Cc Wang Nan <[email protected]>Cc Yury Norov <[email protected]>Cc Michael Ellerman <[email protected]>Signed-off-by Madhavan Srinivasan <[email protected]>--- tools/lib/find_bit.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)tools/lib/find_bit.c +17
-1@@ -37,7 +37,12 @@ static unsigned long _find_next_bit(const unsigned long *addr,if (!nbits || start >= nbits)return nbits; +#if (__BYTE_ORDER == __BIG_ENDIAN) && (BITS_PER_LONG !=64 ) + tmp = addr[(((nbits -1 )/BITS_PER_LONG ) - (start /BITS_PER_LONG ))] + ^ invert; +#else tmp = addr[start /BITS_PER_LONG ] ^ invert; +#endif /* Handle 1st word. */ tmp &=BITMAP_FIRST_WORD_MASK (start);@@ -48,7 +53,12 @@ static unsigned long _find_next_bit(const unsigned long *addr,if (start >= nbits)return nbits; +#if (__BYTE_ORDER == __BIG_ENDIAN) && (BITS_PER_LONG !=64 ) + tmp = addr[(((nbits -1 )/BITS_PER_LONG ) - (start /BITS_PER_LONG ))] + ^ invert; +#else tmp = addr[start /BITS_PER_LONG ] ^ invert; +#endif }return min (start +__ffs (tmp), nbits);@@ -75,8 +85,15 @@ unsigned long find_first_bit(const unsigned long *addr, unsigned long size)unsigned long idx;for (idx =0 ; idx *BITS_PER_LONG < size; idx++) { +#if (__BYTE_ORDER == __BIG_ENDIAN) && (BITS_PER_LONG !=64 ) +if (addr[(((size-1 )/BITS_PER_LONG ) - idx)]) +return min (idx *BITS_PER_LONG + +__ffs (addr[(((size-1 )/BITS_PER_LONG ) - idx)]), + size); +#else if (addr[idx])return min (idx *BITS_PER_LONG +__ffs (addr[idx]), size); +#endif }return size; --1.9.1
Arnaldo Carvalho de Melo
maintainerEm Thu, Jun 16, 2016 at 12:11:04AM +0300, Yury Norov escreveu:
Quoted 4 lines · Yury Norov
Quoted 1 line
Maybe there already is some macro doing the conversion for you...
yes it is,
cpu_to_le64()is what you want
Beware that the cpu_to_le64() in tools/perf is bogus, we need to grab a copy from the kernel sources.
- Arnaldo
PATCH 1/1 tools include: Fix wrong macro definitions for cpu_to_le* for big endian
From: Wang Nan <[email protected]>
The cpu_to_le* macros in kernel.h are defined without considering endianese. This patch includes "byteoder/generic.h" instead to fix the bug, and removes redundant le64_to_cpu definition in intel-bts.c.
--- tools/include/linux/kernel.h | 5 ++--- tools/perf/util/intel-bts.c | 5 ----- 2 files changed, 2 insertions(+), 8 deletions(-)
tools/include/linux/kernel.h +2 -3
@@ -64,11 +64,10 @@#endif /* -* Both need more care to handle endianness +* Need more care to handle endianness * (Don't use bitmap_copy_le() for now) */ -#define cpu_to_le64 (x) (x) -#define cpu_to_le32 (x) (x) +#include <linux/byteorder/generic.h> static inline int vscnprintf (char *buf,size_t size,const char *fmt,va_list args)
tools/perf/util/intel-bts.c +0 -5
@@ -40,11 +40,6 @@#define INTEL_BTS_ERR_NOINSN 5#define INTEL_BTS_ERR_LOST 9 -#if __BYTE_ORDER == __BIG_ENDIAN -#define le64_to_cpu bswap_64 -#else -#define le64_to_cpu -#endif struct intel_bts {struct auxtrace auxtrace;
Signature · 1 line
1.8.3.4
Quoted 9 lines · Arnaldo Carvalho de Melo
Em Thu, Jun 16, 2016 at 12:11:04AM +0300, Yury Norov escreveu:
Quoted 3 lines · Yury Norov
Quoted 1 line
Maybe there already is some macro doing the conversion for you...
yes it is,
cpu_to_le64()is what you wantBeware that the
cpu_to_le64()intools/perfis bogus, we need to grab a copy from the kernel sources.- Arnaldo
[PATCH 1/2] tools include: Sync byteorder/generic.h
[PATCH 2/2] tools include: Fix wrong macro definitions for cpu_to_le* for big endian
Here're two patches related to this issue, sorry for wrongly sent two more reduntant mails.
Thank you.
PATCH 1/1 tools include: Fix wrong macro definitions for cpu_to_le* for big endian
In reply to Arnaldo Carvalho de MeloFrom: Wang Nan <[email protected]>
The cpu_to_le* macros in kernel.h are defined without considering endianese. This patch includes "byteoder/generic.h" instead to fix the bug, and removes redundant le64_to_cpu definition in intel-bts.c.
--- tools/include/linux/kernel.h | 5 ++--- tools/perf/util/intel-bts.c | 5 ----- 2 files changed, 2 insertions(+), 8 deletions(-)
tools/include/linux/kernel.h +2 -3
@@ -64,11 +64,10 @@#endif /* -* Both need more care to handle endianness +* Need more care to handle endianness * (Don't use bitmap_copy_le() for now) */ -#define cpu_to_le64 (x) (x) -#define cpu_to_le32 (x) (x) +#include <linux/byteorder/generic.h> static inline int vscnprintf (char *buf,size_t size,const char *fmt,va_list args)
tools/perf/util/intel-bts.c +0 -5
@@ -40,11 +40,6 @@#define INTEL_BTS_ERR_NOINSN 5#define INTEL_BTS_ERR_LOST 9 -#if __BYTE_ORDER == __BIG_ENDIAN -#define le64_to_cpu bswap_64 -#else -#define le64_to_cpu -#endif struct intel_bts {struct auxtrace auxtrace;
Signature · 1 line
1.8.3.4
PATCH 1/2 tools include: Sync byteorder/generic.h
From: Wang Nan <[email protected]>
This patch copies "include/linux/byteorder/generic.h" to "tools/include/linux/byteorder/generic.h" to enable other libraries to use macros in it.
tools/perf/MANIFEST is also updated for 'make perf-*-src-pkg'.
--- tools/include/linux/byteorder/generic.h | 48 +++++++++++++++++++++++++++++++++ tools/perf/MANIFEST | 1 + 2 files changed, 49 insertions(+) create mode 100644 tools/include/linux/byteorder/generic.h
tools/include/linux/byteorder/generic.h new file +48 -0
@@ -0,0 +1,48 @@ +#ifndef _TOOLS_LINUX_BYTEORDER_GENERIC_H +#define _TOOLS_LINUX_BYTEORDER_GENERIC_H + +#include <endian.h> +#include <byteswap.h> + +#define cpu_to_le64 __cpu_to_le64 +#define le64_to_cpu __le64_to_cpu +#define cpu_to_le32 __cpu_to_le32 +#define le32_to_cpu __le32_to_cpu +#define cpu_to_le16 __cpu_to_le16 +#define le16_to_cpu __le16_to_cpu +#define cpu_to_be64 __cpu_to_be64 +#define be64_to_cpu __be64_to_cpu +#define cpu_to_be32 __cpu_to_be32 +#define be32_to_cpu __be32_to_cpu +#define cpu_to_be16 __cpu_to_be16 +#define be16_to_cpu __be16_to_cpu + +#if __BYTE_ORDER == __BIG_ENDIAN +#define __cpu_to_le16 bswap_16 +#define __cpu_to_le32 bswap_32 +#define __cpu_to_le64 bswap_64 +#define __le16_to_cpu bswap_16 +#define __le32_to_cpu bswap_32 +#define __le64_to_cpu bswap_64 +#define __cpu_to_be16 +#define __cpu_to_be32 +#define __cpu_to_be64 +#define __be16_to_cpu +#define __be32_to_cpu +#define __be64_to_cpu +#else +#define __cpu_to_le16 +#define __cpu_to_le32 +#define __cpu_to_le64 +#define __le16_to_cpu +#define __le32_to_cpu +#define __le64_to_cpu +#define __cpu_to_be16 bswap_16 +#define __cpu_to_be32 bswap_32 +#define __cpu_to_be64 bswap_64 +#define __be16_to_cpu bswap_16 +#define __be32_to_cpu bswap_32 +#define __be64_to_cpu bswap_64 +#endif + +#endif /* _TOOLS_LINUX_BYTEORDER_GENERIC_H */
tools/perf/MANIFEST +1 -0
@@ -46,6 +46,7 @@ tools/include/asm-generic/bitops/hweight.h tools/include/asm-generic/bitops.h tools/include/linux/atomic.h tools/include/linux/bitops.h +tools/include/linux/byteorder/generic.h tools/include/linux/compiler.h tools/include/linux/filter.h tools/include/linux/hash.h
Signature · 1 line
1.8.5.2
PATCH 2/2 tools include: Fix wrong macro definitions for cpu_to_le* for big endian
In reply to He KuangFrom: Wang Nan <[email protected]>
The cpu_to_le* macros in kernel.h are defined without considering endianese. This patch includes "byteoder/generic.h" instead to fix the bug, and removes redundant le64_to_cpu definition in intel-bts.c.
--- tools/include/linux/kernel.h | 5 ++--- tools/perf/util/intel-bts.c | 5 ----- 2 files changed, 2 insertions(+), 8 deletions(-)
tools/include/linux/kernel.h +2 -3
@@ -64,11 +64,10 @@#endif /* -* Both need more care to handle endianness +* Need more care to handle endianness * (Don't use bitmap_copy_le() for now) */ -#define cpu_to_le64 (x) (x) -#define cpu_to_le32 (x) (x) +#include <linux/byteorder/generic.h> static inline int vscnprintf (char *buf,size_t size,const char *fmt,va_list args)
tools/perf/util/intel-bts.c +0 -5
@@ -40,11 +40,6 @@#define INTEL_BTS_ERR_NOINSN 5#define INTEL_BTS_ERR_LOST 9 -#if __BYTE_ORDER == __BIG_ENDIAN -#define le64_to_cpu bswap_64 -#else -#define le64_to_cpu -#endif struct intel_bts {struct auxtrace auxtrace;
Signature · 1 line
1.8.5.2
kbuild test robot
build botRe: [PATCH 2/2] tools include: Fix wrong macro definitions for cpu_to_le* for big endian
url: https://github.com/0day-ci/linux/commits/He-Kuang/tools-include-Fix-wrong-macro-definitions-for-cpu_to_le-for-big-endian/20160616-092924 config: x86_64-allmodconfig (attached as .config)…
Hi,
[auto build test ERROR on tip/perf/core]
[also build test ERROR on v4.7-rc3 next-20160615]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/He-Kuang/tools-include-Fix-wrong-macro-definitions-for-cpu_to_le-for-big-endian/20160616-092924
config: x86_64-allmodconfig (attached as .config)
compiler: gcc-6 (Debian 6.1.1-1) 6.1.1 20160430 reproduce:
# save the attached .config to linux build tree
make ARCH=x86_64 All errors (new ones prefixed by >>):
In file included from tools/include/linux/list.h:6:0,
from elf.h:23,
from special.h:22,
from special.c:26:
>> tools/include/linux/kernel.h:70:37: fatal error: linux/byteorder/generic.h: No such file or directory
compilation terminated.
In file included from tools/include/linux/list.h:6:0,
from elf.h:23,
from builtin-check.c:32:
>> tools/include/linux/kernel.h:70:37: fatal error: linux/byteorder/generic.h: No such file or directory
compilation terminated.
>> mv: cannot stat 'tools/objtool/.special.o.tmp': No such file or directory
make[4]: *** [tools/objtool/special.o] Error 1
>> mv: cannot stat 'tools/objtool/.builtin-check.o.tmp': No such file or directory
make[4]: *** [tools/objtool/builtin-check.o] Error 1
In file included from tools/include/linux/list.h:6:0,
from elf.h:23,
from elf.c:30:
>> tools/include/linux/kernel.h:70:37: fatal error: linux/byteorder/generic.h: No such file or directory
compilation terminated.
>> mv: cannot stat 'tools/objtool/.elf.o.tmp': No such file or directory
make[4]: *** [tools/objtool/elf.o] Error 1
In file included from tools/include/linux/list.h:6:0,
from arch/x86/../../elf.h:23,
from arch/x86/decode.c:26:
>> tools/include/linux/kernel.h:70:37: fatal error: linux/byteorder/generic.h: No such file or directory
compilation terminated.
>> mv: cannot stat 'tools/objtool/arch/x86/.decode.o.tmp': No such file or directory make[5]: *** [tools/objtool/arch/x86/decode.o] Error 1 make[5]: Target '__build' not remade because of errors. make[4]: *** [arch/x86] Error 2
make[4]: Target '__build' not remade because of errors. make[3]: *** [tools/objtool/objtool-in.o] Error 2 make[3]: Target 'all' not remade because of errors. make[2]: *** [objtool] Error 2
make[1]: *** [tools/objtool] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [sub-make] Error 2
vim +70 tools/include/linux/kernel.h
64 #endif
65
66 /*
67 * Need more care to handle endianness
68 * (Don't use bitmap_copy_le() for now)
69 */
> 70 #include <linux/byteorder/generic.h> 71
72 static inline int
73 vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Re: [PATCH 2/2] tools include: Fix wrong macro definitions for cpu_to_le* for big endian
In reply to He KuangQuoted 18 lines · He Kuang
From: Wang Nan <[email protected]>
The cpu_to_le* macros in
kernel.hare defined without considering endianese. This patch includes "byteoder/generic.h" instead to fix the bug, and removes redundantle64_to_cpudefinition inintel-bts.c.Signed-off-by Wang Nan <[email protected]>Signed-off-by He Kuang <[email protected]>--- tools/include/linux/kernel.h | 5 ++--- tools/perf/util/intel-bts.c | 5 ----- 2 files changed, 2 insertions(+), 8 deletions(-)tools/include/linux/kernel.h +0
-0
tools/include/linux/kernel.h line 68
Quoted 6 lines
tools/include/linux/kernel.h +1
-1@@ -64,11 +64,10 @@#endif /* -* Both need more care to handle endianness +* Need more care to handle endianness * (Don't use bitmap_copy_le() for now)
what's the purpose of this comment now?
Quoted 12 lines
tools/include/linux/kernel.h +1
-2*/ -#define cpu_to_le64 (x) (x) -#define cpu_to_le32 (x) (x) +#include <linux/byteorder/generic.h> static inline int vscnprintf (char *buf,size_t size,const char *fmt,va_list args)tools/perf/util/intel-bts.c +0
-0
tools/perf/util/intel-bts.c line 47
Quoted 8 lines
tools/perf/util/intel-bts.c +0
-5@@ -40,11 +40,6 @@#define INTEL_BTS_ERR_NOINSN 5#define INTEL_BTS_ERR_LOST 9 -#if __BYTE_ORDER == __BIG_ENDIAN -#define le64_to_cpu bswap_64 -#else -#define le64_to_cpu -#endif
the purpose of this patchset is to unify these macros right?
there're more conversion defines in:
util/intel-pt-decoder/intel-pt-pkt-decoder.c,
please remove them as well
thanks,
jirka
Re: [PATCH 1/2] tools include: Sync byteorder/generic.h
In reply to He KuangQuoted 5 lines · He Kuang
From: Wang Nan <[email protected]>
This patch copies "
include/linux/byteorder/generic.h" to "tools/include/linux/byteorder/generic.h" to enable other libraries to use macros in it.
it's not the file copied, as the changelog suggest, just several macros, please fix the changelog
thanks,
jirka
Quoted 80 lines
tools/perf/MANIFESTis also updated for 'make perf-*-src-pkg'.Signed-off-by Wang Nan <[email protected]>Signed-off-by He Kuang <[email protected]>--- tools/include/linux/byteorder/generic.h | 48 +++++++++++++++++++++++++++++++++ tools/perf/MANIFEST | 1 + 2 files changed, 49 insertions(+) create mode 100644 tools/include/linux/byteorder/generic.htools/include/linux/byteorder/generic.h new file +48
-0@@ -0,0 +1,48 @@ +#ifndef _TOOLS_LINUX_BYTEORDER_GENERIC_H +#define _TOOLS_LINUX_BYTEORDER_GENERIC_H + +#include <endian.h> +#include <byteswap.h> + +#define cpu_to_le64 __cpu_to_le64 +#define le64_to_cpu __le64_to_cpu +#define cpu_to_le32 __cpu_to_le32 +#define le32_to_cpu __le32_to_cpu +#define cpu_to_le16 __cpu_to_le16 +#define le16_to_cpu __le16_to_cpu +#define cpu_to_be64 __cpu_to_be64 +#define be64_to_cpu __be64_to_cpu +#define cpu_to_be32 __cpu_to_be32 +#define be32_to_cpu __be32_to_cpu +#define cpu_to_be16 __cpu_to_be16 +#define be16_to_cpu __be16_to_cpu + +#if __BYTE_ORDER == __BIG_ENDIAN +#define __cpu_to_le16 bswap_16 +#define __cpu_to_le32 bswap_32 +#define __cpu_to_le64 bswap_64 +#define __le16_to_cpu bswap_16 +#define __le32_to_cpu bswap_32 +#define __le64_to_cpu bswap_64 +#define __cpu_to_be16 +#define __cpu_to_be32 +#define __cpu_to_be64 +#define __be16_to_cpu +#define __be32_to_cpu +#define __be64_to_cpu +#else +#define __cpu_to_le16 +#define __cpu_to_le32 +#define __cpu_to_le64 +#define __le16_to_cpu +#define __le32_to_cpu +#define __le64_to_cpu +#define __cpu_to_be16 bswap_16 +#define __cpu_to_be32 bswap_32 +#define __cpu_to_be64 bswap_64 +#define __be16_to_cpu bswap_16 +#define __be32_to_cpu bswap_32 +#define __be64_to_cpu bswap_64 +#endif + +#endif /* _TOOLS_LINUX_BYTEORDER_GENERIC_H */ tools/perf/MANIFEST +1
-0@@ -46,6 +46,7 @@ tools/include/asm-generic/bitops/hweight.h tools/include/asm-generic/bitops.h tools/include/linux/atomic.h tools/include/linux/bitops.h +tools/include/linux/byteorder/generic.h tools/include/linux/compiler.h tools/include/linux/filter.h tools/include/linux/hash.hSignature · 1 line
1.8.5.2
Quoted 54 lines · Yury Norov
Hi Madhavan,
Quoted 5 lines · Madhavan Srinivasan
When decoding the
perf_regsmask inregs_dump__printf(),
we loop through the mask usingfind_first_bitandfind_next_bitfunctions. And mask is of type "u64". But "u64" is send as a "unsigned long *" to lib functions along withsizeof().Quoted 9 lines
While the exisitng code works fine in most of the case, when using a 32bit perf on a 64bit kernel (Big Endian), we end reading the wrong word. In
find_first_bit(), one word at a time (based onBITS_PER_LONG) is loaded and
checked for any bit set. In 32bit BE userspace,BITS_PER_LONGturns out to be 32, and for a mask value of "0x00000000000000ff",find_first_bitwill return 32, instead of 0. Reason for this is that, value in the word0 is all zeros and value
in word1 is0xff. Ideally, second word in the mask should be loaded and searched. Patch swaps the word to look incase of 32bit BE.I think this is not a problem of
find_bit()at all. You have wrong typecast as the source of problem (tools/perf/util/session.c"):940 static void
regs_dump__printf(u64 mask, u64 *regs)941 {
942 unsigned rid, i = 0;
943944 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) { ^^^^ Here ^^^^945 u64 val = regs[i++];
946
947 printf(".... %-5s 0x%" PRIx64 "\n",
948perf_reg_name(rid), val);
949 }
950 }But for some reason you change correct
find_bit()...Though proper fix is like this for me:
static void
regs_dump__printf(u64 mask, u64 *regs){ unsigned rid, i = 0; unsigned long _mask[sizeof(mask)/sizeof(unsigned long)]; _mask[0] = mask & ULONG_MAX; if (sizeof(mask) > sizeof(unsigned long)) _mask[1] = mask >> BITS_PER_LONG; for_each_set_bit(rid, _mask, sizeof(mask) * BITS_PER_BYTE) { u64 val = regs[i++]; printf(".... %-5s 0x%" PRIx64 "\n", perf_reg_name(rid), val); } }
Quoted 1 line
Maybe there already is some macro doing the conversion for you...
Agreed, but reason for proposing fix in lib side is to avoid conversion on each case (if any in future).
I will repost the fix as suggested.
Maddy
Quoted 66 lines
Yury.
Quoted 64 lines
Cc Arnaldo Carvalho de Melo <[email protected]>Cc Adrian Hunter <[email protected]>Cc Borislav Petkov <[email protected]>Cc David Ahern <[email protected]>Cc George Spelvin <[email protected]>Cc Jiri Olsa <[email protected]>Cc Namhyung Kim <[email protected]>Cc Rasmus Villemoes <[email protected]>Cc Wang Nan <[email protected]>Cc Yury Norov <[email protected]>Cc Michael Ellerman <[email protected]>Signed-off-by Madhavan Srinivasan <[email protected]>--- tools/lib/find_bit.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)tools/lib/find_bit.c +17
-1@@ -37,7 +37,12 @@ static unsigned long _find_next_bit(const unsigned long *addr,if (!nbits || start >= nbits)return nbits; +#if (__BYTE_ORDER == __BIG_ENDIAN) && (BITS_PER_LONG !=64 ) + tmp = addr[(((nbits -1 )/BITS_PER_LONG ) - (start /BITS_PER_LONG ))] + ^ invert; +#else tmp = addr[start /BITS_PER_LONG ] ^ invert; +#endif /* Handle 1st word. */ tmp &=BITMAP_FIRST_WORD_MASK (start);@@ -48,7 +53,12 @@ static unsigned long _find_next_bit(const unsigned long *addr,if (start >= nbits)return nbits; +#if (__BYTE_ORDER == __BIG_ENDIAN) && (BITS_PER_LONG !=64 ) + tmp = addr[(((nbits -1 )/BITS_PER_LONG ) - (start /BITS_PER_LONG ))] + ^ invert; +#else tmp = addr[start /BITS_PER_LONG ] ^ invert; +#endif }return min (start +__ffs (tmp), nbits);@@ -75,8 +85,15 @@ unsigned long find_first_bit(const unsigned long *addr, unsigned long size)unsigned long idx;for (idx =0 ; idx *BITS_PER_LONG < size; idx++) { +#if (__BYTE_ORDER == __BIG_ENDIAN) && (BITS_PER_LONG !=64 ) +if (addr[(((size-1 )/BITS_PER_LONG ) - idx)]) +return min (idx *BITS_PER_LONG + +__ffs (addr[(((size-1 )/BITS_PER_LONG ) - idx)]), + size); +#else if (addr[idx])return min (idx *BITS_PER_LONG +__ffs (addr[idx]), size); +#endif }return size; --1.9.1
Quoted 10 lines · George Spelvin
tools/lib/find_bit.c static unsigned long _find_next_bit(const unsigned long *addr, · line 45
Quoted 6 lines · Madhavan Srinivasan
tools/lib/find_bit.c +5
-0+#if (__BYTE_ORDER == __BIG_ENDIAN) && (BITS_PER_LONG !=64 ) + tmp = addr[(((nbits -1 )/BITS_PER_LONG ) - (start /BITS_PER_LONG ))] + ^ invert; +#else tmp = addr[start /BITS_PER_LONG ] ^ invert; +#endif Than you for diagnosing this problem, but I don't think the fix is correct.
Quoted 6 lines
1) It's not clear that all users of
_find_next_bitandfor_each_set_bit()want this change.
2) Is your code even correct? I'd think you'd want addr[x ^ 1]. Are you sure you shpuld be reversing the whole array, and not just the halves of each 64-bit word?
3) You've now broken the case of 32-bit big-endian kernel.
Yes. But looks like we havent hit this case yet. Will post a fix.
Maddy
Quoted 3 lines
I think the proper solution is uglier than this. :-(
Quoted 50 lines · Yury Norov
Quoted 49 lines · Yury Norov
Hi Madhavan,
Quoted 5 lines · Madhavan Srinivasan
When decoding the
perf_regsmask inregs_dump__printf(),
we loop through the mask usingfind_first_bitandfind_next_bitfunctions. And mask is of type "u64". But "u64" is send as a "unsigned long *" to lib functions along withsizeof().Quoted 9 lines
While the exisitng code works fine in most of the case, when using a 32bit perf on a 64bit kernel (Big Endian), we end reading the wrong word. In
find_first_bit(), one word at a time (based onBITS_PER_LONG) is loaded and
checked for any bit set. In 32bit BE userspace,BITS_PER_LONGturns out to be 32, and for a mask value of "0x00000000000000ff",find_first_bitwill return 32, instead of 0. Reason for this is that, value in the word0 is all zeros and value
in word1 is0xff. Ideally, second word in the mask should be loaded and searched. Patch swaps the word to look incase of 32bit BE.I think this is not a problem of
find_bit()at all. You have wrong typecast as the source of problem (tools/perf/util/session.c"):940 static void
regs_dump__printf(u64 mask, u64 *regs)941 {
942 unsigned rid, i = 0;
943944 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) { ^^^^ Here ^^^^945 u64 val = regs[i++];
946
947 printf(".... %-5s 0x%" PRIx64 "\n",
948perf_reg_name(rid), val);
949 }
950 }But for some reason you change correct
find_bit()...Though proper fix is like this for me:
static void
regs_dump__printf(u64 mask, u64 *regs){ unsigned rid, i = 0; unsigned long _mask[sizeof(mask)/sizeof(unsigned long)]; _mask[0] = mask & ULONG_MAX; if (sizeof(mask) > sizeof(unsigned long)) _mask[1] = mask >> BITS_PER_LONG; for_each_set_bit(rid, _mask, sizeof(mask) * BITS_PER_BYTE) { u64 val = regs[i++];
Quoted 7 lines
Quoted 6 lines
printf(".... %-5s 0x%" PRIx64 "\n", perf_reg_name(rid), val); } }Maybe there already is some macro doing the conversion for you...
yes it is,
cpu_to_le64()is what you want
no wait, on second look, cpu_to_le64() is not right. Because we will end up swapping within 32bit. But what you suggested looks to be fine. I can repost this with one minor tweak, right shift with 32 instead of BITS_PER_LONG (since I see compiler errors in 64bit).
Maddy
Quoted 67 lines
Quoted 67 lines
Yury.
Quoted 64 lines
Cc Arnaldo Carvalho de Melo <[email protected]>Cc Adrian Hunter <[email protected]>Cc Borislav Petkov <[email protected]>Cc David Ahern <[email protected]>Cc George Spelvin <[email protected]>Cc Jiri Olsa <[email protected]>Cc Namhyung Kim <[email protected]>Cc Rasmus Villemoes <[email protected]>Cc Wang Nan <[email protected]>Cc Yury Norov <[email protected]>Cc Michael Ellerman <[email protected]>Signed-off-by Madhavan Srinivasan <[email protected]>--- tools/lib/find_bit.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)tools/lib/find_bit.c +17
-1@@ -37,7 +37,12 @@ static unsigned long _find_next_bit(const unsigned long *addr,if (!nbits || start >= nbits)return nbits; +#if (__BYTE_ORDER == __BIG_ENDIAN) && (BITS_PER_LONG !=64 ) + tmp = addr[(((nbits -1 )/BITS_PER_LONG ) - (start /BITS_PER_LONG ))] + ^ invert; +#else tmp = addr[start /BITS_PER_LONG ] ^ invert; +#endif /* Handle 1st word. */ tmp &=BITMAP_FIRST_WORD_MASK (start);@@ -48,7 +53,12 @@ static unsigned long _find_next_bit(const unsigned long *addr,if (start >= nbits)return nbits; +#if (__BYTE_ORDER == __BIG_ENDIAN) && (BITS_PER_LONG !=64 ) + tmp = addr[(((nbits -1 )/BITS_PER_LONG ) - (start /BITS_PER_LONG ))] + ^ invert; +#else tmp = addr[start /BITS_PER_LONG ] ^ invert; +#endif }return min (start +__ffs (tmp), nbits);@@ -75,8 +85,15 @@ unsigned long find_first_bit(const unsigned long *addr, unsigned long size)unsigned long idx;for (idx =0 ; idx *BITS_PER_LONG < size; idx++) { +#if (__BYTE_ORDER == __BIG_ENDIAN) && (BITS_PER_LONG !=64 ) +if (addr[(((size-1 )/BITS_PER_LONG ) - idx)]) +return min (idx *BITS_PER_LONG + +__ffs (addr[(((size-1 )/BITS_PER_LONG ) - idx)]), + size); +#else if (addr[idx])return min (idx *BITS_PER_LONG +__ffs (addr[idx]), size); +#endif }return size; --1.9.1