Skip to contentSkip to conversation

tools/perf: fix the word selected in find_*_bit

PATCH In review Madhavan Srinivasan 7 participants 16 messages Jun 15, 2016 – Jun 16, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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.

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]>
---
 tools/lib/find_bit.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
tools/lib/find_bit.c +17 -0
index 9122a9e80046..996b3e04324f 100644
@@ -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

#1 lore raw

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. :-(

#3 lore raw
In reply to Madhavan Srinivasan

Hi Madhavan,

Quoted 5 lines · Madhavan Srinivasan

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().

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 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.

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]>
---
 tools/lib/find_bit.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
tools/lib/find_bit.c +17 -1
index 9122a9e80046..996b3e04324f 100644
@@ -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

#4 lore raw
Quoted 55 lines · Yury Norov

Hi Madhavan,

Quoted 5 lines · Madhavan Srinivasan

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().

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 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.

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);
        }
}
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]>
---
 tools/lib/find_bit.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
tools/lib/find_bit.c +17 -1
index 9122a9e80046..996b3e04324f 100644
@@ -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

#5 lore raw

Em 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

#6 lore raw

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
index 76df535..6145e41 100644
@@ -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
index 9df9960..0e632c4 100644
@@ -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
#7 lore raw
In reply to Arnaldo Carvalho de Melo
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 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/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.

#8 lore raw

PATCH 1/1 tools include: Fix wrong macro definitions for cpu_to_le* for big endian

In reply to Arnaldo Carvalho de Melo

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
index 76df535..6145e41 100644
@@ -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
index 9df9960..0e632c4 100644
@@ -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
#9 lore raw

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
new file mode 100644
index 0000000..41b4507
@@ -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
index 8c8c6b9..80ac3d4 100644
@@ -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
#11 lore raw

PATCH 2/2 tools include: Fix wrong macro definitions for cpu_to_le* for big endian

In reply to He Kuang

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
index 76df535..6145e41 100644
@@ -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
index 9df9960..0e632c4 100644
@@ -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
#12 lore raw

Re: [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

#13 lore raw

Re: [PATCH 2/2] tools include: Fix wrong macro definitions for cpu_to_le* for big endian

In reply to He Kuang
Quoted 18 lines · He Kuang

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 +0 -0
index 76df535..6145e41 100644

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
index 9df9960..0e632c4 100644

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

#10 lore raw

Re: [PATCH 1/2] tools include: Sync byteorder/generic.h

In reply to He Kuang
Quoted 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/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
new file mode 100644
index 0000000..41b4507
@@ -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
index 8c8c6b9..80ac3d4 100644
@@ -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
#15 lore raw
In reply to Yury Norov
Quoted 54 lines · Yury Norov

Hi Madhavan,

Quoted 5 lines · Madhavan Srinivasan

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().

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 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.

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);
        }
}
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]>
---
 tools/lib/find_bit.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
tools/lib/find_bit.c +17 -1
index 9122a9e80046..996b3e04324f 100644
@@ -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

#2 lore raw
In reply to George Spelvin
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_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.

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. :-(

#14 lore raw
In reply to Yury Norov
Quoted 50 lines · Yury Norov
Quoted 49 lines · Yury Norov

Hi Madhavan,

Quoted 5 lines · Madhavan Srinivasan

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().

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 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.

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++];
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]>
---
 tools/lib/find_bit.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
tools/lib/find_bit.c +17 -1
index 9122a9e80046..996b3e04324f 100644
@@ -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