Skip to contentSkip to conversation

perf arm-spe: Unaligned pointer work around

PATCH Applied Ian Rogers 3 participants 4 messages May 14, 2024 – May 29, 2024 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf arm-spe: Unaligned pointer work around

Use get_unaligned_leXX instead of leXX_to_cpu to handle unaligned pointers. Such pointers occur with libFuzzer testing.

A similar change for intel-pt was done in:
https://lore.kernel.org/r/[email protected]

---
 .../arm-spe-decoder/arm-spe-pkt-decoder.c     | 23 ++++---------------
 1 file changed, 5 insertions(+), 18 deletions(-)
tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c +5 -18
index a454c6737563..7bf607d0f6d8 100644
@@ -10,24 +10,11 @@
 #include <byteswap.h>
 #include <linux/bitops.h>
 #include <stdarg.h>
+#include <linux/kernel.h>
+#include <asm-generic/unaligned.h>
 
 #include "arm-spe-pkt-decoder.h"
 
-#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
-#define le16_to_cpu bswap_16
-#define le32_to_cpu bswap_32
-#define le64_to_cpu bswap_64
-#define memcpy_le64(d, s, n) do { \
-	memcpy((d), (s), (n));    \
-	*(d) = le64_to_cpu(*(d)); \
-} while (0)
-#else
-#define le16_to_cpu
-#define le32_to_cpu
-#define le64_to_cpu
-#define memcpy_le64 memcpy
-#endif
-
 static const char * const arm_spe_packet_name[] = {
 	[ARM_SPE_PAD]		= "PAD",
 	[ARM_SPE_END]		= "END",
@@ -70,9 +57,9 @@ static int arm_spe_get_payload(const unsigned char *buf, size_t len,
 
 	switch (payload_len) {
 	case 1: packet->payload = *(uint8_t *)buf; break;
-	case 2: packet->payload = le16_to_cpu(*(uint16_t *)buf); break;
-	case 4: packet->payload = le32_to_cpu(*(uint32_t *)buf); break;
-	case 8: packet->payload = le64_to_cpu(*(uint64_t *)buf); break;
+	case 2: packet->payload = get_unaligned_le16(buf); break;
+	case 4: packet->payload = get_unaligned_le32(buf); break;
+	case 8: packet->payload = get_unaligned_le64(buf); break;
 	default: return ARM_SPE_BAD_PACKET;
 	}
 
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
author reviewer
#1 lore raw
Quoted 8 lines · Ian Rogers

Use get_unaligned_leXX instead of leXX_to_cpu to handle unaligned pointers. Such pointers occur with libFuzzer testing.

A similar change for intel-pt was done in:
https://lore.kernel.org/r/[email protected]

+Leo Yan +James Clark surprisingly missed by get_maintainer.pl.

Thanks,
Ian

Quoted 51 lines
---
 .../arm-spe-decoder/arm-spe-pkt-decoder.c     | 23 ++++---------------
 1 file changed, 5 insertions(+), 18 deletions(-)
tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c +5 -19
index a454c6737563..7bf607d0f6d8 100644
@@ -10,24 +10,11 @@
 #include <byteswap.h>
 #include <linux/bitops.h>
 #include <stdarg.h>
+#include <linux/kernel.h>
+#include <asm-generic/unaligned.h>
 
 #include "arm-spe-pkt-decoder.h"
 
-#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
-#define le16_to_cpu bswap_16
-#define le32_to_cpu bswap_32
-#define le64_to_cpu bswap_64
-#define memcpy_le64(d, s, n) do { \
-       memcpy((d), (s), (n));    \
-       *(d) = le64_to_cpu(*(d)); \
-} while (0)
-#else
-#define le16_to_cpu
-#define le32_to_cpu
-#define le64_to_cpu
-#define memcpy_le64 memcpy
-#endif
-
 static const char * const arm_spe_packet_name[] = {
        [ARM_SPE_PAD]           = "PAD",
        [ARM_SPE_END]           = "END",
@@ -70,9 +57,9 @@ static int arm_spe_get_payload(const unsigned char *buf, size_t len,
 
        switch (payload_len) {
        case 1: packet->payload = *(uint8_t *)buf; break;
-       case 2: packet->payload = le16_to_cpu(*(uint16_t *)buf); break;
-       case 4: packet->payload = le32_to_cpu(*(uint32_t *)buf); break;
-       case 8: packet->payload = le64_to_cpu(*(uint64_t *)buf); break;
+       case 2: packet->payload = get_unaligned_le16(buf); break;
+       case 4: packet->payload = get_unaligned_le32(buf); break;
+       case 8: packet->payload = get_unaligned_le64(buf); break;
        default: return ARM_SPE_BAD_PACKET;
        }
 
--

2.45.0.rc1.225.g2a3ae87e7f-goog

Reviewed-by on v1 1/1
#2 lore raw
In reply to Ian Rogers
Quoted 8 lines · Ian Rogers

Use get_unaligned_leXX instead of leXX_to_cpu to handle unaligned pointers. Such pointers occur with libFuzzer testing.

A similar change for intel-pt was done in:
https://lore.kernel.org/r/[email protected]

---

Quoted 47 lines
 .../arm-spe-decoder/arm-spe-pkt-decoder.c     | 23 ++++---------------
 1 file changed, 5 insertions(+), 18 deletions(-)
tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c +5 -18
index a454c6737563..7bf607d0f6d8 100644
@@ -10,24 +10,11 @@
 #include <byteswap.h>
 #include <linux/bitops.h>
 #include <stdarg.h>
+#include <linux/kernel.h>
+#include <asm-generic/unaligned.h>
 
 #include "arm-spe-pkt-decoder.h"
 
-#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
-#define le16_to_cpu bswap_16
-#define le32_to_cpu bswap_32
-#define le64_to_cpu bswap_64
-#define memcpy_le64(d, s, n) do { \
-	memcpy((d), (s), (n));    \
-	*(d) = le64_to_cpu(*(d)); \
-} while (0)
-#else
-#define le16_to_cpu
-#define le32_to_cpu
-#define le64_to_cpu
-#define memcpy_le64 memcpy
-#endif
-
 static const char * const arm_spe_packet_name[] = {
 	[ARM_SPE_PAD]		= "PAD",
 	[ARM_SPE_END]		= "END",
@@ -70,9 +57,9 @@ static int arm_spe_get_payload(const unsigned char *buf, size_t len,
 
 	switch (payload_len) {
 	case 1: packet->payload = *(uint8_t *)buf; break;
-	case 2: packet->payload = le16_to_cpu(*(uint16_t *)buf); break;
-	case 4: packet->payload = le32_to_cpu(*(uint32_t *)buf); break;
-	case 8: packet->payload = le64_to_cpu(*(uint64_t *)buf); break;
+	case 2: packet->payload = get_unaligned_le16(buf); break;
+	case 4: packet->payload = get_unaligned_le32(buf); break;
+	case 8: packet->payload = get_unaligned_le64(buf); break;
 	default: return ARM_SPE_BAD_PACKET;
 	}