2020-11-11 07:17:47

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 00/22] perf arm-spe: Refactor decoding & dumping flow

This is patch set v8 for refactoring Arm SPE trace decoding and dumping.

This version addresses Andre's comment to pass parameter '&buf_len' at
the last call arm_spe_pkt_snprintf() in the function arm_spe_pkt_desc().

This patch set is cleanly applied on the top of perf/core branch
with commit 644bf4b0f7ac ("perf jevents: Add test for arch std events").

I retested this patch set on Hisilicon D06 platform with commands
"perf report -D" and "perf script", compared the decoding results
between with this patch set and without this patch set, "diff" tool
shows the result as expected.

Changes from v7:
- Changed to pass '&buf_len' for the last call arm_spe_pkt_snprintf() in
the patch 07/22 (Andre).

Changes from v6:
- Removed the redundant comma from the string in the patch 21/22 "perf
arm_spe: Decode memory tagging properties" (Dave);
- Refined the return value for arm_spe_pkt_desc(): returns 0 for
success, otherwise returns non zero for failures; handle error code at
the end of function arm_spe_pkt_desc(); this is accomplished in the
new patch 07/22 "perf arm-spe: Consolidate arm_spe_pkt_desc()'s
return value" (Dave).

Changes from v5:
- Directly bail out arm_spe_pkt_snprintf() if any error occurred
(Andre).

Changes from v4:
- Implemented a cumulative error for arm_spe_pkt_snprintf() and changed
to condense code for printing strings (Dave);
- Changed to check payload bits [55:52] for parse kernel address
(Andre).

Changes from v3:
- Refined arm_spe_payload_len() and removed macro SPE_HEADER_SZ()
(Andre);
- Refined packet header index macros (Andre);
- Added patch "perf arm_spe: Fixup top byte for data virtual address" to
fixup the data virtual address for 64KB pages and refined comments for
the fixup (Andre);
- Added Andre's review tag (using "b4 am" command);
- Changed the macros to SPE_PKT_IS_XXX() format to check operation types
(Andre).


Andre Przywara (1):
perf arm_spe: Decode memory tagging properties

Leo Yan (20):
perf arm-spe: Include bitops.h for BIT() macro
perf arm-spe: Fix a typo in comment
perf arm-spe: Refactor payload size calculation
perf arm-spe: Refactor arm_spe_get_events()
perf arm-spe: Fix packet length handling
perf arm-spe: Refactor printing string to buffer
perf arm-spe: Consolidate arm_spe_pkt_desc()'s return value
perf arm-spe: Refactor packet header parsing
perf arm-spe: Add new function arm_spe_pkt_desc_addr()
perf arm-spe: Refactor address packet handling
perf arm_spe: Fixup top byte for data virtual address
perf arm-spe: Refactor context packet handling
perf arm-spe: Add new function arm_spe_pkt_desc_counter()
perf arm-spe: Refactor counter packet handling
perf arm-spe: Add new function arm_spe_pkt_desc_event()
perf arm-spe: Refactor event type handling
perf arm-spe: Remove size condition checking for events
perf arm-spe: Add new function arm_spe_pkt_desc_op_type()
perf arm-spe: Refactor operation packet handling
perf arm-spe: Add more sub classes for operation packet

Wei Li (1):
perf arm-spe: Add support for ARMv8.3-SPE

.../util/arm-spe-decoder/arm-spe-decoder.c | 59 +-
.../util/arm-spe-decoder/arm-spe-decoder.h | 17 -
.../arm-spe-decoder/arm-spe-pkt-decoder.c | 601 ++++++++++--------
.../arm-spe-decoder/arm-spe-pkt-decoder.h | 122 +++-
tools/perf/util/arm-spe.c | 2 +-
5 files changed, 479 insertions(+), 322 deletions(-)

--
2.17.1


2020-11-11 07:18:07

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 10/22] perf arm-spe: Refactor address packet handling

This patch is to refactor address packet handling, it defines macros for
address packet's header and payload, these macros are used by decoder
and the dump flow.

Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
---
.../util/arm-spe-decoder/arm-spe-decoder.c | 29 ++++++++-------
.../arm-spe-decoder/arm-spe-pkt-decoder.c | 26 +++++++-------
.../arm-spe-decoder/arm-spe-pkt-decoder.h | 35 ++++++++++++-------
3 files changed, 48 insertions(+), 42 deletions(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c
index cc18a1e8c212..776b3e6628bb 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c
@@ -24,36 +24,35 @@

static u64 arm_spe_calc_ip(int index, u64 payload)
{
- u8 *addr = (u8 *)&payload;
- int ns, el;
+ u64 ns, el;

/* Instruction virtual address or Branch target address */
if (index == SPE_ADDR_PKT_HDR_INDEX_INS ||
index == SPE_ADDR_PKT_HDR_INDEX_BRANCH) {
- ns = addr[7] & SPE_ADDR_PKT_NS;
- el = (addr[7] & SPE_ADDR_PKT_EL_MASK) >> SPE_ADDR_PKT_EL_OFFSET;
+ ns = SPE_ADDR_PKT_GET_NS(payload);
+ el = SPE_ADDR_PKT_GET_EL(payload);
+
+ /* Clean highest byte */
+ payload = SPE_ADDR_PKT_ADDR_GET_BYTES_0_6(payload);

/* Fill highest byte for EL1 or EL2 (VHE) mode */
if (ns && (el == SPE_ADDR_PKT_EL1 || el == SPE_ADDR_PKT_EL2))
- addr[7] = 0xff;
- /* Clean highest byte for other cases */
- else
- addr[7] = 0x0;
+ payload |= 0xffULL << SPE_ADDR_PKT_ADDR_BYTE7_SHIFT;

/* Data access virtual address */
} else if (index == SPE_ADDR_PKT_HDR_INDEX_DATA_VIRT) {

+ /* Clean tags */
+ payload = SPE_ADDR_PKT_ADDR_GET_BYTES_0_6(payload);
+
/* Fill highest byte if bits [48..55] is 0xff */
- if (addr[6] == 0xff)
- addr[7] = 0xff;
- /* Otherwise, cleanup tags */
- else
- addr[7] = 0x0;
+ if (SPE_ADDR_PKT_ADDR_GET_BYTE_6(payload) == 0xffULL)
+ payload |= 0xffULL << SPE_ADDR_PKT_ADDR_BYTE7_SHIFT;

/* Data access physical address */
} else if (index == SPE_ADDR_PKT_HDR_INDEX_DATA_PHYS) {
- /* Cleanup byte 7 */
- addr[7] = 0x0;
+ /* Clean highest byte */
+ payload = SPE_ADDR_PKT_ADDR_GET_BYTES_0_6(payload);
} else {
pr_err("unsupported address packet index: 0x%x\n", index);
}
diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index 0aa15632e87b..05d63e77f741 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -13,9 +13,6 @@

#include "arm-spe-pkt-decoder.h"

-#define NS_FLAG BIT(63)
-#define EL_FLAG (BIT(62) | BIT(61))
-
#if __BYTE_ORDER == __BIG_ENDIAN
#define le16_to_cpu bswap_16
#define le32_to_cpu bswap_32
@@ -167,10 +164,11 @@ static int arm_spe_get_addr(const unsigned char *buf, size_t len,
const unsigned char ext_hdr, struct arm_spe_pkt *packet)
{
packet->type = ARM_SPE_ADDRESS;
+
if (ext_hdr)
- packet->index = ((buf[0] & 0x3) << 3) | (buf[1] & 0x7);
+ packet->index = SPE_HDR_EXTENDED_INDEX(buf[0], buf[1]);
else
- packet->index = buf[0] & 0x7;
+ packet->index = SPE_HDR_SHORT_INDEX(buf[0]);

return arm_spe_get_payload(buf, len, ext_hdr, packet);
}
@@ -296,22 +294,22 @@ static int arm_spe_pkt_desc_addr(const struct arm_spe_pkt *packet,
int err = 0;

switch (idx) {
- case 0:
- case 1:
- ns = !!(packet->payload & NS_FLAG);
- el = (packet->payload & EL_FLAG) >> 61;
- payload &= ~(0xffULL << 56);
+ case SPE_ADDR_PKT_HDR_INDEX_INS:
+ case SPE_ADDR_PKT_HDR_INDEX_BRANCH:
+ ns = !!SPE_ADDR_PKT_GET_NS(payload);
+ el = SPE_ADDR_PKT_GET_EL(payload);
+ payload = SPE_ADDR_PKT_ADDR_GET_BYTES_0_6(payload);
arm_spe_pkt_snprintf(&err, &buf, &buf_len,
"%s 0x%llx el%d ns=%d",
(idx == 1) ? "TGT" : "PC", payload, el, ns);
break;
- case 2:
+ case SPE_ADDR_PKT_HDR_INDEX_DATA_VIRT:
arm_spe_pkt_snprintf(&err, &buf, &buf_len,
"VA 0x%llx", payload);
break;
- case 3:
- ns = !!(packet->payload & NS_FLAG);
- payload &= ~(0xffULL << 56);
+ case SPE_ADDR_PKT_HDR_INDEX_DATA_PHYS:
+ ns = !!SPE_ADDR_PKT_GET_NS(payload);
+ payload = SPE_ADDR_PKT_ADDR_GET_BYTES_0_6(payload);
arm_spe_pkt_snprintf(&err, &buf, &buf_len,
"PA 0x%llx ns=%d", payload, ns);
break;
diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
index 129f43405eb1..f97d6840be3a 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
@@ -56,19 +56,28 @@ struct arm_spe_pkt {
#define SPE_HEADER0_COUNTER 0x98
#define SPE_HEADER1_ALIGNMENT 0x0

-#define SPE_ADDR_PKT_HDR_INDEX_INS (0x0)
-#define SPE_ADDR_PKT_HDR_INDEX_BRANCH (0x1)
-#define SPE_ADDR_PKT_HDR_INDEX_DATA_VIRT (0x2)
-#define SPE_ADDR_PKT_HDR_INDEX_DATA_PHYS (0x3)
-
-#define SPE_ADDR_PKT_NS BIT(7)
-#define SPE_ADDR_PKT_CH BIT(6)
-#define SPE_ADDR_PKT_EL_OFFSET (5)
-#define SPE_ADDR_PKT_EL_MASK (0x3 << SPE_ADDR_PKT_EL_OFFSET)
-#define SPE_ADDR_PKT_EL0 (0)
-#define SPE_ADDR_PKT_EL1 (1)
-#define SPE_ADDR_PKT_EL2 (2)
-#define SPE_ADDR_PKT_EL3 (3)
+#define SPE_HDR_SHORT_INDEX(h) ((h) & GENMASK_ULL(2, 0))
+#define SPE_HDR_EXTENDED_INDEX(h0, h1) (((h0) & GENMASK_ULL(1, 0)) << 3 | \
+ SPE_HDR_SHORT_INDEX(h1))
+
+/* Address packet header */
+#define SPE_ADDR_PKT_HDR_INDEX_INS 0x0
+#define SPE_ADDR_PKT_HDR_INDEX_BRANCH 0x1
+#define SPE_ADDR_PKT_HDR_INDEX_DATA_VIRT 0x2
+#define SPE_ADDR_PKT_HDR_INDEX_DATA_PHYS 0x3
+
+/* Address packet payload */
+#define SPE_ADDR_PKT_ADDR_BYTE7_SHIFT 56
+#define SPE_ADDR_PKT_ADDR_GET_BYTES_0_6(v) ((v) & GENMASK_ULL(55, 0))
+#define SPE_ADDR_PKT_ADDR_GET_BYTE_6(v) (((v) & GENMASK_ULL(55, 48)) >> 48)
+
+#define SPE_ADDR_PKT_GET_NS(v) (((v) & BIT_ULL(63)) >> 63)
+#define SPE_ADDR_PKT_GET_EL(v) (((v) & GENMASK_ULL(62, 61)) >> 61)
+
+#define SPE_ADDR_PKT_EL0 0
+#define SPE_ADDR_PKT_EL1 1
+#define SPE_ADDR_PKT_EL2 2
+#define SPE_ADDR_PKT_EL3 3

const char *arm_spe_pkt_name(enum arm_spe_pkt_type);

--
2.17.1

2020-11-11 07:18:34

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 06/22] perf arm-spe: Refactor printing string to buffer

When outputs strings to the decoding buffer with function snprintf(),
SPE decoder needs to detects if any error returns from snprintf() and if
so needs to directly bail out. If snprintf() returns success, it needs
to update buffer pointer and reduce the buffer length so can continue to
output the next string into the consequent memory space.

This complex logics are spreading in the function arm_spe_pkt_desc() so
there has many duplicate codes for handling error detecting, increment
buffer pointer and decrement buffer size.

To avoid the duplicate code, this patch introduces a new helper function
arm_spe_pkt_snprintf() which is used to wrap up the complex logics, and
it's used by the caller arm_spe_pkt_desc().

This patch also moves the variable 'blen' as the function's local
variable, this allows to remove the unnecessary braces and improve the
readability.

Suggested-by: Dave Martin <[email protected]>
Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
---
.../arm-spe-decoder/arm-spe-pkt-decoder.c | 260 +++++++++---------
1 file changed, 126 insertions(+), 134 deletions(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index 04fd7fd7c15f..1970686f7020 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -9,6 +9,7 @@
#include <endian.h>
#include <byteswap.h>
#include <linux/bitops.h>
+#include <stdarg.h>

#include "arm-spe-pkt-decoder.h"

@@ -258,192 +259,183 @@ int arm_spe_get_packet(const unsigned char *buf, size_t len,
return ret;
}

+static int arm_spe_pkt_snprintf(int *err, char **buf_p, size_t *blen,
+ const char *fmt, ...)
+{
+ va_list ap;
+ int ret;
+
+ /* Bail out if any error occurred */
+ if (err && *err)
+ return *err;
+
+ va_start(ap, fmt);
+ ret = vsnprintf(*buf_p, *blen, fmt, ap);
+ va_end(ap);
+
+ if (ret < 0) {
+ if (err && !*err)
+ *err = ret;
+
+ /*
+ * A return value of (*blen - 1) or more means that the
+ * output was truncated and the buffer is overrun.
+ */
+ } else if (ret >= ((int)*blen - 1)) {
+ (*buf_p)[*blen - 1] = '\0';
+
+ /*
+ * Set *err to 'ret' to avoid overflow if tries to
+ * fill this buffer sequentially.
+ */
+ if (err && !*err)
+ *err = ret;
+ } else {
+ *buf_p += ret;
+ *blen -= ret;
+ }
+
+ return ret;
+}
+
int arm_spe_pkt_desc(const struct arm_spe_pkt *packet, char *buf,
size_t buf_len)
{
int ret, ns, el, idx = packet->index;
unsigned long long payload = packet->payload;
const char *name = arm_spe_pkt_name(packet->type);
+ size_t blen = buf_len;
+ int err = 0;

switch (packet->type) {
case ARM_SPE_BAD:
case ARM_SPE_PAD:
case ARM_SPE_END:
- return snprintf(buf, buf_len, "%s", name);
- case ARM_SPE_EVENTS: {
- size_t blen = buf_len;
-
- ret = 0;
- ret = snprintf(buf, buf_len, "EV");
- buf += ret;
- blen -= ret;
- if (payload & 0x1) {
- ret = snprintf(buf, buf_len, " EXCEPTION-GEN");
- buf += ret;
- blen -= ret;
- }
- if (payload & 0x2) {
- ret = snprintf(buf, buf_len, " RETIRED");
- buf += ret;
- blen -= ret;
- }
- if (payload & 0x4) {
- ret = snprintf(buf, buf_len, " L1D-ACCESS");
- buf += ret;
- blen -= ret;
- }
- if (payload & 0x8) {
- ret = snprintf(buf, buf_len, " L1D-REFILL");
- buf += ret;
- blen -= ret;
- }
- if (payload & 0x10) {
- ret = snprintf(buf, buf_len, " TLB-ACCESS");
- buf += ret;
- blen -= ret;
- }
- if (payload & 0x20) {
- ret = snprintf(buf, buf_len, " TLB-REFILL");
- buf += ret;
- blen -= ret;
- }
- if (payload & 0x40) {
- ret = snprintf(buf, buf_len, " NOT-TAKEN");
- buf += ret;
- blen -= ret;
- }
- if (payload & 0x80) {
- ret = snprintf(buf, buf_len, " MISPRED");
- buf += ret;
- blen -= ret;
- }
+ return arm_spe_pkt_snprintf(&err, &buf, &blen, "%s", name);
+ case ARM_SPE_EVENTS:
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "EV");
+
+ if (payload & 0x1)
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " EXCEPTION-GEN");
+ if (payload & 0x2)
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " RETIRED");
+ if (payload & 0x4)
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " L1D-ACCESS");
+ if (payload & 0x8)
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " L1D-REFILL");
+ if (payload & 0x10)
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " TLB-ACCESS");
+ if (payload & 0x20)
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " TLB-REFILL");
+ if (payload & 0x40)
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " NOT-TAKEN");
+ if (payload & 0x80)
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " MISPRED");
if (idx > 1) {
- if (payload & 0x100) {
- ret = snprintf(buf, buf_len, " LLC-ACCESS");
- buf += ret;
- blen -= ret;
- }
- if (payload & 0x200) {
- ret = snprintf(buf, buf_len, " LLC-REFILL");
- buf += ret;
- blen -= ret;
- }
- if (payload & 0x400) {
- ret = snprintf(buf, buf_len, " REMOTE-ACCESS");
- buf += ret;
- blen -= ret;
- }
+ if (payload & 0x100)
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " LLC-ACCESS");
+ if (payload & 0x200)
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " LLC-REFILL");
+ if (payload & 0x400)
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " REMOTE-ACCESS");
}
if (ret < 0)
return ret;
blen -= ret;
return buf_len - blen;
- }
case ARM_SPE_OP_TYPE:
switch (idx) {
- case 0: return snprintf(buf, buf_len, "%s", payload & 0x1 ?
- "COND-SELECT" : "INSN-OTHER");
- case 1: {
- size_t blen = buf_len;
+ case 0:
+ return arm_spe_pkt_snprintf(&err, &buf, &blen,
+ payload & 0x1 ? "COND-SELECT" : "INSN-OTHER");
+ case 1:
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen,
+ payload & 0x1 ? "ST" : "LD");

- if (payload & 0x1)
- ret = snprintf(buf, buf_len, "ST");
- else
- ret = snprintf(buf, buf_len, "LD");
- buf += ret;
- blen -= ret;
if (payload & 0x2) {
- if (payload & 0x4) {
- ret = snprintf(buf, buf_len, " AT");
- buf += ret;
- blen -= ret;
- }
- if (payload & 0x8) {
- ret = snprintf(buf, buf_len, " EXCL");
- buf += ret;
- blen -= ret;
- }
- if (payload & 0x10) {
- ret = snprintf(buf, buf_len, " AR");
- buf += ret;
- blen -= ret;
- }
+ if (payload & 0x4)
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " AT");
+ if (payload & 0x8)
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " EXCL");
+ if (payload & 0x10)
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " AR");
} else if (payload & 0x4) {
- ret = snprintf(buf, buf_len, " SIMD-FP");
- buf += ret;
- blen -= ret;
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " SIMD-FP");
}
+
if (ret < 0)
return ret;
blen -= ret;
return buf_len - blen;
- }
- case 2: {
- size_t blen = buf_len;

- ret = snprintf(buf, buf_len, "B");
- buf += ret;
- blen -= ret;
- if (payload & 0x1) {
- ret = snprintf(buf, buf_len, " COND");
- buf += ret;
- blen -= ret;
- }
- if (payload & 0x2) {
- ret = snprintf(buf, buf_len, " IND");
- buf += ret;
- blen -= ret;
- }
+ case 2:
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "B");
+
+ if (payload & 0x1)
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " COND");
+ if (payload & 0x2)
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " IND");
+
if (ret < 0)
return ret;
blen -= ret;
return buf_len - blen;
- }
- default: return 0;
+
+ default:
+ return 0;
}
case ARM_SPE_DATA_SOURCE:
case ARM_SPE_TIMESTAMP:
- return snprintf(buf, buf_len, "%s %lld", name, payload);
+ return arm_spe_pkt_snprintf(&err, &buf, &blen, "%s %lld", name, payload);
case ARM_SPE_ADDRESS:
switch (idx) {
case 0:
- case 1: ns = !!(packet->payload & NS_FLAG);
+ case 1:
+ ns = !!(packet->payload & NS_FLAG);
el = (packet->payload & EL_FLAG) >> 61;
payload &= ~(0xffULL << 56);
- return snprintf(buf, buf_len, "%s 0x%llx el%d ns=%d",
+ return arm_spe_pkt_snprintf(&err, &buf, &blen,
+ "%s 0x%llx el%d ns=%d",
(idx == 1) ? "TGT" : "PC", payload, el, ns);
- case 2: return snprintf(buf, buf_len, "VA 0x%llx", payload);
- case 3: ns = !!(packet->payload & NS_FLAG);
+ case 2:
+ return arm_spe_pkt_snprintf(&err, &buf, &blen,
+ "VA 0x%llx", payload);
+ case 3:
+ ns = !!(packet->payload & NS_FLAG);
payload &= ~(0xffULL << 56);
- return snprintf(buf, buf_len, "PA 0x%llx ns=%d",
- payload, ns);
- default: return 0;
+ return arm_spe_pkt_snprintf(&err, &buf, &blen,
+ "PA 0x%llx ns=%d", payload, ns);
+ default:
+ return 0;
}
case ARM_SPE_CONTEXT:
- return snprintf(buf, buf_len, "%s 0x%lx el%d", name,
- (unsigned long)payload, idx + 1);
- case ARM_SPE_COUNTER: {
- size_t blen = buf_len;
-
- ret = snprintf(buf, buf_len, "%s %d ", name,
- (unsigned short)payload);
- buf += ret;
- blen -= ret;
+ return arm_spe_pkt_snprintf(&err, &buf, &blen, "%s 0x%lx el%d",
+ name, (unsigned long)payload, idx + 1);
+ case ARM_SPE_COUNTER:
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "%s %d ", name,
+ (unsigned short)payload);
switch (idx) {
- case 0: ret = snprintf(buf, buf_len, "TOT"); break;
- case 1: ret = snprintf(buf, buf_len, "ISSUE"); break;
- case 2: ret = snprintf(buf, buf_len, "XLAT"); break;
- default: ret = 0;
+ case 0:
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "TOT");
+ break;
+ case 1:
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "ISSUE");
+ break;
+ case 2:
+ ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "XLAT");
+ break;
+ default:
+ ret = 0;
+ break;
}
if (ret < 0)
return ret;
blen -= ret;
return buf_len - blen;
- }
default:
break;
}

- return snprintf(buf, buf_len, "%s 0x%llx (%d)",
- name, payload, packet->index);
+ return arm_spe_pkt_snprintf(&err, &buf, &blen, "%s 0x%llx (%d)",
+ name, payload, packet->index);
}
--
2.17.1

2020-11-11 07:19:45

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 04/22] perf arm-spe: Refactor arm_spe_get_events()

In function arm_spe_get_events(), the event packet's 'index' is assigned
as payload length, but the flow is not directive: it firstly gets the
packet length from the return value of arm_spe_get_payload(), the value
includes header length (1) and payload length:

int ret = arm_spe_get_payload(buf, len, packet);

and then reduces header length from packet length, so finally get the
payload length:

packet->index = ret - 1;

To simplify the code, this patch directly assigns payload length to
event packet's index; and at the end it calls arm_spe_get_payload() to
return the payload value.

Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
---
tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index 06b3eec4494e..f1b4cb008837 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -136,8 +136,6 @@ static int arm_spe_get_timestamp(const unsigned char *buf, size_t len,
static int arm_spe_get_events(const unsigned char *buf, size_t len,
struct arm_spe_pkt *packet)
{
- int ret = arm_spe_get_payload(buf, len, packet);
-
packet->type = ARM_SPE_EVENTS;

/* we use index to identify Events with a less number of
@@ -145,9 +143,9 @@ static int arm_spe_get_events(const unsigned char *buf, size_t len,
* LLC-REFILL, and REMOTE-ACCESS events are identified if
* index > 1.
*/
- packet->index = ret - 1;
+ packet->index = arm_spe_payload_len(buf[0]);

- return ret;
+ return arm_spe_get_payload(buf, len, packet);
}

static int arm_spe_get_data_source(const unsigned char *buf, size_t len,
--
2.17.1

2020-11-11 07:20:03

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 19/22] perf arm-spe: Refactor operation packet handling

Defines macros for operation packet header and formats (support sub
classes for 'other', 'branch', 'load and store', etc). Uses these
macros for operation packet decoding and dumping.

Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
---
.../arm-spe-decoder/arm-spe-pkt-decoder.c | 26 ++++++++++---------
.../arm-spe-decoder/arm-spe-pkt-decoder.h | 23 ++++++++++++++++
2 files changed, 37 insertions(+), 12 deletions(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index 4f93b75c87e6..5fe1c5e8094d 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -144,7 +144,7 @@ static int arm_spe_get_op_type(const unsigned char *buf, size_t len,
struct arm_spe_pkt *packet)
{
packet->type = ARM_SPE_OP_TYPE;
- packet->index = buf[0] & 0x3;
+ packet->index = SPE_OP_PKT_HDR_CLASS(buf[0]);
return arm_spe_get_payload(buf, len, 0, packet);
}

@@ -328,31 +328,33 @@ static int arm_spe_pkt_desc_op_type(const struct arm_spe_pkt *packet,
int err = 0;

switch (packet->index) {
- case 0:
+ case SPE_OP_PKT_HDR_CLASS_OTHER:
arm_spe_pkt_snprintf(&err, &buf, &buf_len,
- payload & 0x1 ? "COND-SELECT" : "INSN-OTHER");
+ payload & SPE_OP_PKT_COND ? "COND-SELECT" : "INSN-OTHER");
break;
- case 1:
+ case SPE_OP_PKT_HDR_CLASS_LD_ST_ATOMIC:
arm_spe_pkt_snprintf(&err, &buf, &buf_len,
payload & 0x1 ? "ST" : "LD");

- if (payload & 0x2) {
- if (payload & 0x4)
+ if (SPE_OP_PKT_IS_LDST_ATOMIC(payload)) {
+ if (payload & SPE_OP_PKT_AT)
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " AT");
- if (payload & 0x8)
+ if (payload & SPE_OP_PKT_EXCL)
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " EXCL");
- if (payload & 0x10)
+ if (payload & SPE_OP_PKT_AR)
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " AR");
- } else if (payload & 0x4) {
+ } else if (SPE_OP_PKT_LDST_SUBCLASS_GET(payload) ==
+ SPE_OP_PKT_LDST_SUBCLASS_SIMD_FP) {
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " SIMD-FP");
}
break;
- case 2:
+ case SPE_OP_PKT_HDR_CLASS_BR_ERET:
arm_spe_pkt_snprintf(&err, &buf, &buf_len, "B");

- if (payload & 0x1)
+ if (payload & SPE_OP_PKT_COND)
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " COND");
- if (payload & 0x2)
+
+ if (SPE_OP_PKT_IS_INDIRECT_BRANCH(payload))
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " IND");

break;
diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
index 42ed4e61ede2..7032fc141ad4 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
@@ -105,6 +105,29 @@ enum arm_spe_events {
EV_EMPTY_PREDICATE = 18,
};

+/* Operation packet header */
+#define SPE_OP_PKT_HDR_CLASS(h) ((h) & GENMASK_ULL(1, 0))
+#define SPE_OP_PKT_HDR_CLASS_OTHER 0x0
+#define SPE_OP_PKT_HDR_CLASS_LD_ST_ATOMIC 0x1
+#define SPE_OP_PKT_HDR_CLASS_BR_ERET 0x2
+
+#define SPE_OP_PKT_COND BIT(0)
+
+#define SPE_OP_PKT_LDST_SUBCLASS_GET(v) ((v) & GENMASK_ULL(7, 1))
+#define SPE_OP_PKT_LDST_SUBCLASS_GP_REG 0x0
+#define SPE_OP_PKT_LDST_SUBCLASS_SIMD_FP 0x4
+#define SPE_OP_PKT_LDST_SUBCLASS_UNSPEC_REG 0x10
+#define SPE_OP_PKT_LDST_SUBCLASS_NV_SYSREG 0x30
+
+#define SPE_OP_PKT_IS_LDST_ATOMIC(v) (((v) & (GENMASK_ULL(7, 5) | BIT(1))) == 0x2)
+
+#define SPE_OP_PKT_AR BIT(4)
+#define SPE_OP_PKT_EXCL BIT(3)
+#define SPE_OP_PKT_AT BIT(2)
+#define SPE_OP_PKT_ST BIT(0)
+
+#define SPE_OP_PKT_IS_INDIRECT_BRANCH(v) (((v) & GENMASK_ULL(7, 1)) == 0x2)
+
const char *arm_spe_pkt_name(enum arm_spe_pkt_type);

int arm_spe_get_packet(const unsigned char *buf, size_t len,
--
2.17.1

2020-11-11 07:20:49

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 22/22] perf arm-spe: Add support for ARMv8.3-SPE

From: Wei Li <[email protected]>

This patch is to support Armv8.3 extension for SPE, it adds alignment
field in the Events packet and it supports the Scalable Vector Extension
(SVE) for Operation packet and Events packet with two additions:

- The vector length for SVE operations in the Operation Type packet;
- The incomplete predicate and empty predicate fields in the Events
packet.

Signed-off-by: Wei Li <[email protected]>
Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
---
.../arm-spe-decoder/arm-spe-pkt-decoder.c | 36 +++++++++++++++++--
.../arm-spe-decoder/arm-spe-pkt-decoder.h | 16 +++++++++
2 files changed, 50 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index cbbbbefdc52b..afb6d9fe9eae 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -317,6 +317,12 @@ static int arm_spe_pkt_desc_event(const struct arm_spe_pkt *packet,
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " LLC-REFILL");
if (payload & BIT(EV_REMOTE_ACCESS))
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " REMOTE-ACCESS");
+ if (payload & BIT(EV_ALIGNMENT))
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " ALIGNMENT");
+ if (payload & BIT(EV_PARTIAL_PREDICATE))
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " SVE-PARTIAL-PRED");
+ if (payload & BIT(EV_EMPTY_PREDICATE))
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " SVE-EMPTY-PRED");

return err;
}
@@ -329,8 +335,23 @@ static int arm_spe_pkt_desc_op_type(const struct arm_spe_pkt *packet,

switch (packet->index) {
case SPE_OP_PKT_HDR_CLASS_OTHER:
- arm_spe_pkt_snprintf(&err, &buf, &buf_len,
- payload & SPE_OP_PKT_COND ? "COND-SELECT" : "INSN-OTHER");
+ if (SPE_OP_PKT_IS_OTHER_SVE_OP(payload)) {
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, "SVE-OTHER");
+
+ /* SVE effective vector length */
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " EVLEN %d",
+ SPE_OP_PKG_SVE_EVL(payload));
+
+ if (payload & SPE_OP_PKT_SVE_FP)
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " FP");
+ if (payload & SPE_OP_PKT_SVE_PRED)
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " PRED");
+ } else {
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, "OTHER");
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " %s",
+ payload & SPE_OP_PKT_COND ?
+ "COND-SELECT" : "INSN-OTHER");
+ }
break;
case SPE_OP_PKT_HDR_CLASS_LD_ST_ATOMIC:
arm_spe_pkt_snprintf(&err, &buf, &buf_len,
@@ -361,6 +382,17 @@ static int arm_spe_pkt_desc_op_type(const struct arm_spe_pkt *packet,
default:
break;
}
+
+ if (SPE_OP_PKT_IS_LDST_SVE(payload)) {
+ /* SVE effective vector length */
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " EVLEN %d",
+ SPE_OP_PKG_SVE_EVL(payload));
+
+ if (payload & SPE_OP_PKT_SVE_PRED)
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " PRED");
+ if (payload & SPE_OP_PKT_SVE_SG)
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " SG");
+ }
break;
case SPE_OP_PKT_HDR_CLASS_BR_ERET:
arm_spe_pkt_snprintf(&err, &buf, &buf_len, "B");
diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
index 1ad14885c2a1..9b970e7bf1e2 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
@@ -113,6 +113,8 @@ enum arm_spe_events {
#define SPE_OP_PKT_HDR_CLASS_LD_ST_ATOMIC 0x1
#define SPE_OP_PKT_HDR_CLASS_BR_ERET 0x2

+#define SPE_OP_PKT_IS_OTHER_SVE_OP(v) (((v) & (BIT(7) | BIT(3) | BIT(0))) == 0x8)
+
#define SPE_OP_PKT_COND BIT(0)

#define SPE_OP_PKT_LDST_SUBCLASS_GET(v) ((v) & GENMASK_ULL(7, 1))
@@ -128,6 +130,20 @@ enum arm_spe_events {
#define SPE_OP_PKT_AT BIT(2)
#define SPE_OP_PKT_ST BIT(0)

+#define SPE_OP_PKT_IS_LDST_SVE(v) (((v) & (BIT(3) | BIT(1))) == 0x8)
+
+#define SPE_OP_PKT_SVE_SG BIT(7)
+/*
+ * SVE effective vector length (EVL) is stored in byte 0 bits [6:4];
+ * the length is rounded up to a power of two and use 32 as one step,
+ * so EVL calculation is:
+ *
+ * 32 * (2 ^ bits [6:4]) = 32 << (bits [6:4])
+ */
+#define SPE_OP_PKG_SVE_EVL(v) (32 << (((v) & GENMASK_ULL(6, 4)) >> 4))
+#define SPE_OP_PKT_SVE_PRED BIT(2)
+#define SPE_OP_PKT_SVE_FP BIT(1)
+
#define SPE_OP_PKT_IS_INDIRECT_BRANCH(v) (((v) & GENMASK_ULL(7, 1)) == 0x2)

const char *arm_spe_pkt_name(enum arm_spe_pkt_type);
--
2.17.1

2020-11-11 07:21:00

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 21/22] perf arm_spe: Decode memory tagging properties

From: Andre Przywara <[email protected]>

When SPE records a physical address, it can additionally tag the event
with information from the Memory Tagging architecture extension.

Decode the two additional fields in the SPE event payload.

[leoy: Refined patch to use predefined macros]

Signed-off-by: Andre Przywara <[email protected]>
Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Dave Martin <[email protected]>
---
tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c | 6 +++++-
tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h | 2 ++
2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index 55c4bf330b96..cbbbbefdc52b 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -385,6 +385,7 @@ static int arm_spe_pkt_desc_addr(const struct arm_spe_pkt *packet,
char *buf, size_t buf_len)
{
int ns, el, idx = packet->index;
+ int ch, pat;
u64 payload = packet->payload;
int err = 0;

@@ -404,9 +405,12 @@ static int arm_spe_pkt_desc_addr(const struct arm_spe_pkt *packet,
break;
case SPE_ADDR_PKT_HDR_INDEX_DATA_PHYS:
ns = !!SPE_ADDR_PKT_GET_NS(payload);
+ ch = !!SPE_ADDR_PKT_GET_CH(payload);
+ pat = SPE_ADDR_PKT_GET_PAT(payload);
payload = SPE_ADDR_PKT_ADDR_GET_BYTES_0_6(payload);
arm_spe_pkt_snprintf(&err, &buf, &buf_len,
- "PA 0x%llx ns=%d", payload, ns);
+ "PA 0x%llx ns=%d ch=%d pat=%x",
+ payload, ns, ch, pat);
break;
default:
/* Unknown index */
diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
index 7032fc141ad4..1ad14885c2a1 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
@@ -73,6 +73,8 @@ struct arm_spe_pkt {

#define SPE_ADDR_PKT_GET_NS(v) (((v) & BIT_ULL(63)) >> 63)
#define SPE_ADDR_PKT_GET_EL(v) (((v) & GENMASK_ULL(62, 61)) >> 61)
+#define SPE_ADDR_PKT_GET_CH(v) (((v) & BIT_ULL(62)) >> 62)
+#define SPE_ADDR_PKT_GET_PAT(v) (((v) & GENMASK_ULL(59, 56)) >> 56)

#define SPE_ADDR_PKT_EL0 0
#define SPE_ADDR_PKT_EL1 1
--
2.17.1

2020-11-11 07:21:30

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 20/22] perf arm-spe: Add more sub classes for operation packet

For the operation type packet payload with load/store class, it misses
to support these sub classes:

- A load/store targeting the general-purpose registers;
- A load/store targeting unspecified registers;
- The ARMv8.4 nested virtualisation extension can redirect system
register accesses to a memory page controlled by the hypervisor.
The SPE profiling feature in newer implementations can tag those
memory accesses accordingly.

Add the bit pattern describing load/store sub classes, so that the perf
tool can decode it properly.

Inspired by Andre Przywara, refined the commit log and code for more
clear description.

Co-developed-by: Andre Przywara <[email protected]>
Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
---
.../util/arm-spe-decoder/arm-spe-pkt-decoder.c | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index 5fe1c5e8094d..55c4bf330b96 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -343,9 +343,23 @@ static int arm_spe_pkt_desc_op_type(const struct arm_spe_pkt *packet,
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " EXCL");
if (payload & SPE_OP_PKT_AR)
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " AR");
- } else if (SPE_OP_PKT_LDST_SUBCLASS_GET(payload) ==
- SPE_OP_PKT_LDST_SUBCLASS_SIMD_FP) {
+ }
+
+ switch (SPE_OP_PKT_LDST_SUBCLASS_GET(payload)) {
+ case SPE_OP_PKT_LDST_SUBCLASS_SIMD_FP:
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " SIMD-FP");
+ break;
+ case SPE_OP_PKT_LDST_SUBCLASS_GP_REG:
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " GP-REG");
+ break;
+ case SPE_OP_PKT_LDST_SUBCLASS_UNSPEC_REG:
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " UNSPEC-REG");
+ break;
+ case SPE_OP_PKT_LDST_SUBCLASS_NV_SYSREG:
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " NV-SYSREG");
+ break;
+ default:
+ break;
}
break;
case SPE_OP_PKT_HDR_CLASS_BR_ERET:
--
2.17.1

2020-11-11 07:23:56

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 15/22] perf arm-spe: Add new function arm_spe_pkt_desc_event()

This patch moves out the event packet parsing from arm_spe_pkt_desc()
to the new function arm_spe_pkt_desc_event().

Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
---
.../arm-spe-decoder/arm-spe-pkt-decoder.c | 63 +++++++++++--------
1 file changed, 37 insertions(+), 26 deletions(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index 5178bbe64422..af87d3c8cb50 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -287,6 +287,42 @@ static int arm_spe_pkt_snprintf(int *err, char **buf_p, size_t *blen,
return ret;
}

+static int arm_spe_pkt_desc_event(const struct arm_spe_pkt *packet,
+ char *buf, size_t buf_len)
+{
+ u64 payload = packet->payload;
+ int err = 0;
+
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, "EV");
+
+ if (payload & 0x1)
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " EXCEPTION-GEN");
+ if (payload & 0x2)
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " RETIRED");
+ if (payload & 0x4)
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " L1D-ACCESS");
+ if (payload & 0x8)
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " L1D-REFILL");
+ if (payload & 0x10)
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " TLB-ACCESS");
+ if (payload & 0x20)
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " TLB-REFILL");
+ if (payload & 0x40)
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " NOT-TAKEN");
+ if (payload & 0x80)
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " MISPRED");
+ if (packet->index > 1) {
+ if (payload & 0x100)
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " LLC-ACCESS");
+ if (payload & 0x200)
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " LLC-REFILL");
+ if (payload & 0x400)
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " REMOTE-ACCESS");
+ }
+
+ return err;
+}
+
static int arm_spe_pkt_desc_addr(const struct arm_spe_pkt *packet,
char *buf, size_t buf_len)
{
@@ -367,32 +403,7 @@ int arm_spe_pkt_desc(const struct arm_spe_pkt *packet, char *buf,
arm_spe_pkt_snprintf(&err, &buf, &blen, "%s", name);
break;
case ARM_SPE_EVENTS:
- arm_spe_pkt_snprintf(&err, &buf, &blen, "EV");
-
- if (payload & 0x1)
- arm_spe_pkt_snprintf(&err, &buf, &blen, " EXCEPTION-GEN");
- if (payload & 0x2)
- arm_spe_pkt_snprintf(&err, &buf, &blen, " RETIRED");
- if (payload & 0x4)
- arm_spe_pkt_snprintf(&err, &buf, &blen, " L1D-ACCESS");
- if (payload & 0x8)
- arm_spe_pkt_snprintf(&err, &buf, &blen, " L1D-REFILL");
- if (payload & 0x10)
- arm_spe_pkt_snprintf(&err, &buf, &blen, " TLB-ACCESS");
- if (payload & 0x20)
- arm_spe_pkt_snprintf(&err, &buf, &blen, " TLB-REFILL");
- if (payload & 0x40)
- arm_spe_pkt_snprintf(&err, &buf, &blen, " NOT-TAKEN");
- if (payload & 0x80)
- arm_spe_pkt_snprintf(&err, &buf, &blen, " MISPRED");
- if (idx > 1) {
- if (payload & 0x100)
- arm_spe_pkt_snprintf(&err, &buf, &blen, " LLC-ACCESS");
- if (payload & 0x200)
- arm_spe_pkt_snprintf(&err, &buf, &blen, " LLC-REFILL");
- if (payload & 0x400)
- arm_spe_pkt_snprintf(&err, &buf, &blen, " REMOTE-ACCESS");
- }
+ err = arm_spe_pkt_desc_event(packet, buf, buf_len);
break;
case ARM_SPE_OP_TYPE:
switch (idx) {
--
2.17.1

2020-11-11 07:24:19

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 13/22] perf arm-spe: Add new function arm_spe_pkt_desc_counter()

This patch moves out the counter packet parsing code from
arm_spe_pkt_desc() to the new function arm_spe_pkt_desc_counter().

Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
---
.../arm-spe-decoder/arm-spe-pkt-decoder.c | 43 ++++++++++++-------
1 file changed, 28 insertions(+), 15 deletions(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index 7b0f654e5cd6..e8c9da1d4280 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -322,6 +322,33 @@ static int arm_spe_pkt_desc_addr(const struct arm_spe_pkt *packet,
return err;
}

+static int arm_spe_pkt_desc_counter(const struct arm_spe_pkt *packet,
+ char *buf, size_t buf_len)
+{
+ u64 payload = packet->payload;
+ const char *name = arm_spe_pkt_name(packet->type);
+ int err = 0;
+
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, "%s %d ", name,
+ (unsigned short)payload);
+
+ switch (packet->index) {
+ case 0:
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, "TOT");
+ break;
+ case 1:
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, "ISSUE");
+ break;
+ case 2:
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, "XLAT");
+ break;
+ default:
+ break;
+ }
+
+ return err;
+}
+
int arm_spe_pkt_desc(const struct arm_spe_pkt *packet, char *buf,
size_t buf_len)
{
@@ -414,21 +441,7 @@ int arm_spe_pkt_desc(const struct arm_spe_pkt *packet, char *buf,
name, (unsigned long)payload, idx + 1);
break;
case ARM_SPE_COUNTER:
- arm_spe_pkt_snprintf(&err, &buf, &blen, "%s %d ", name,
- (unsigned short)payload);
- switch (idx) {
- case 0:
- arm_spe_pkt_snprintf(&err, &buf, &blen, "TOT");
- break;
- case 1:
- arm_spe_pkt_snprintf(&err, &buf, &blen, "ISSUE");
- break;
- case 2:
- arm_spe_pkt_snprintf(&err, &buf, &blen, "XLAT");
- break;
- default:
- break;
- }
+ err = arm_spe_pkt_desc_counter(packet, buf, buf_len);
break;
default:
/* Unknown index */
--
2.17.1

2020-11-11 07:24:21

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 11/22] perf arm_spe: Fixup top byte for data virtual address

To establish a valid address from the address packet payload and finally
the address value can be used for parsing data symbol in DSO, current
code uses 0xff to replace the tag in the top byte of data virtual
address.

So far the code only fixups top byte for the memory layouts with 4KB
pages, it misses to support memory layouts with 64KB pages.

This patch adds the conditions for checking bits [55:52] are 0xf, if
detects the pattern it will fill 0xff into the top byte of the address,
also adds comment to explain the fixing up.

Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
---
.../util/arm-spe-decoder/arm-spe-decoder.c | 20 ++++++++++++++++---
1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c
index 776b3e6628bb..cac2ef79c025 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c
@@ -24,7 +24,7 @@

static u64 arm_spe_calc_ip(int index, u64 payload)
{
- u64 ns, el;
+ u64 ns, el, val;

/* Instruction virtual address or Branch target address */
if (index == SPE_ADDR_PKT_HDR_INDEX_INS ||
@@ -45,8 +45,22 @@ static u64 arm_spe_calc_ip(int index, u64 payload)
/* Clean tags */
payload = SPE_ADDR_PKT_ADDR_GET_BYTES_0_6(payload);

- /* Fill highest byte if bits [48..55] is 0xff */
- if (SPE_ADDR_PKT_ADDR_GET_BYTE_6(payload) == 0xffULL)
+ /*
+ * Armv8 ARM (ARM DDI 0487F.c), chapter "D10.2.1 Address packet"
+ * defines the data virtual address payload format, the top byte
+ * (bits [63:56]) is assigned as top-byte tag; so we only can
+ * retrieve address value from bits [55:0].
+ *
+ * According to Documentation/arm64/memory.rst, if detects the
+ * specific pattern in bits [55:52] of payload which falls in
+ * the kernel space, should fixup the top byte and this allows
+ * perf tool to parse DSO symbol for data address correctly.
+ *
+ * For this reason, if detects the bits [55:52] is 0xf, will
+ * fill 0xff into the top byte.
+ */
+ val = SPE_ADDR_PKT_ADDR_GET_BYTE_6(payload);
+ if ((val & 0xf0ULL) == 0xf0ULL)
payload |= 0xffULL << SPE_ADDR_PKT_ADDR_BYTE7_SHIFT;

/* Data access physical address */
--
2.17.1

2020-11-11 07:24:21

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 12/22] perf arm-spe: Refactor context packet handling

Minor refactoring to use macro for index mask.

Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
---
tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c | 2 +-
tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h | 3 +++
2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index 05d63e77f741..7b0f654e5cd6 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -136,7 +136,7 @@ static int arm_spe_get_context(const unsigned char *buf, size_t len,
struct arm_spe_pkt *packet)
{
packet->type = ARM_SPE_CONTEXT;
- packet->index = buf[0] & 0x3;
+ packet->index = SPE_CTX_PKT_HDR_INDEX(buf[0]);
return arm_spe_get_payload(buf, len, 0, packet);
}

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
index f97d6840be3a..9bc876bffd35 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
@@ -79,6 +79,9 @@ struct arm_spe_pkt {
#define SPE_ADDR_PKT_EL2 2
#define SPE_ADDR_PKT_EL3 3

+/* Context packet header */
+#define SPE_CTX_PKT_HDR_INDEX(h) ((h) & GENMASK_ULL(1, 0))
+
const char *arm_spe_pkt_name(enum arm_spe_pkt_type);

int arm_spe_get_packet(const unsigned char *buf, size_t len,
--
2.17.1

2020-11-11 07:25:24

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 01/22] perf arm-spe: Include bitops.h for BIT() macro

Include header linux/bitops.h, directly use its BIT() macro and remove
the self defined macros.

Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
---
tools/perf/util/arm-spe-decoder/arm-spe-decoder.c | 5 +----
tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c | 3 +--
2 files changed, 2 insertions(+), 6 deletions(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c
index 93e063f22be5..cc18a1e8c212 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c
@@ -12,6 +12,7 @@
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
+#include <linux/bitops.h>
#include <linux/compiler.h>
#include <linux/zalloc.h>

@@ -21,10 +22,6 @@

#include "arm-spe-decoder.h"

-#ifndef BIT
-#define BIT(n) (1UL << (n))
-#endif
-
static u64 arm_spe_calc_ip(int index, u64 payload)
{
u8 *addr = (u8 *)&payload;
diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index b94001b756c7..46ddb53a6457 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -8,11 +8,10 @@
#include <string.h>
#include <endian.h>
#include <byteswap.h>
+#include <linux/bitops.h>

#include "arm-spe-pkt-decoder.h"

-#define BIT(n) (1ULL << (n))
-
#define NS_FLAG BIT(63)
#define EL_FLAG (BIT(62) | BIT(61))

--
2.17.1

2020-11-11 07:25:24

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 02/22] perf arm-spe: Fix a typo in comment

Fix a typo: s/iff/if.

Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
---
tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index 46ddb53a6457..7c7b5eb09fba 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -142,7 +142,7 @@ static int arm_spe_get_events(const unsigned char *buf, size_t len,

/* we use index to identify Events with a less number of
* comparisons in arm_spe_pkt_desc(): E.g., the LLC-ACCESS,
- * LLC-REFILL, and REMOTE-ACCESS events are identified iff
+ * LLC-REFILL, and REMOTE-ACCESS events are identified if
* index > 1.
*/
packet->index = ret - 1;
--
2.17.1

2020-11-11 07:25:24

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 07/22] perf arm-spe: Consolidate arm_spe_pkt_desc()'s return value

arm_spe_pkt_desc() returns the length of consumed the buffer for
the success case; otherwise, it delivers the return value from
arm_spe_pkt_snprintf(), and returns the last return value if there have
multiple calling arm_spe_pkt_snprintf().

Since arm_spe_pkt_snprintf() has the same semantics with vsnprintf() for
the return value, and vsnprintf() might return value equals to or bigger
than the parameter 'size' to indicate the truncation. Because the
return value is >= 0 when the string is truncated, this condition will
be returned up the stack as "success".

This patch simplifies the return value for arm_spe_pkt_desc(): '0' means
success and other values mean an error has occurred. To realize this,
it relies on arm_spe_pkt_snprintf()'s parameter 'err', the 'err' is a
cumulative value, returns its final value if printing buffer is called
for one time or multiple times.

To unify the error value generation, this patch handles error in a
central place, rather than directly bailing out in switch-cases,
it returns error at the end of arm_spe_pkt_desc().

This patch changes the caller arm_spe_dump() to respect the updated
return value semantics of arm_spe_pkt_desc().

Suggested-by: Dave Martin <[email protected]>
Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
---
.../arm-spe-decoder/arm-spe-pkt-decoder.c | 128 +++++++++---------
tools/perf/util/arm-spe.c | 2 +-
2 files changed, 68 insertions(+), 62 deletions(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index 1970686f7020..424ff5862aa1 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -301,9 +301,10 @@ static int arm_spe_pkt_snprintf(int *err, char **buf_p, size_t *blen,
int arm_spe_pkt_desc(const struct arm_spe_pkt *packet, char *buf,
size_t buf_len)
{
- int ret, ns, el, idx = packet->index;
+ int ns, el, idx = packet->index;
unsigned long long payload = packet->payload;
const char *name = arm_spe_pkt_name(packet->type);
+ char *buf_orig = buf;
size_t blen = buf_len;
int err = 0;

@@ -311,82 +312,76 @@ int arm_spe_pkt_desc(const struct arm_spe_pkt *packet, char *buf,
case ARM_SPE_BAD:
case ARM_SPE_PAD:
case ARM_SPE_END:
- return arm_spe_pkt_snprintf(&err, &buf, &blen, "%s", name);
+ arm_spe_pkt_snprintf(&err, &buf, &blen, "%s", name);
+ break;
case ARM_SPE_EVENTS:
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "EV");
+ arm_spe_pkt_snprintf(&err, &buf, &blen, "EV");

if (payload & 0x1)
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " EXCEPTION-GEN");
+ arm_spe_pkt_snprintf(&err, &buf, &blen, " EXCEPTION-GEN");
if (payload & 0x2)
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " RETIRED");
+ arm_spe_pkt_snprintf(&err, &buf, &blen, " RETIRED");
if (payload & 0x4)
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " L1D-ACCESS");
+ arm_spe_pkt_snprintf(&err, &buf, &blen, " L1D-ACCESS");
if (payload & 0x8)
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " L1D-REFILL");
+ arm_spe_pkt_snprintf(&err, &buf, &blen, " L1D-REFILL");
if (payload & 0x10)
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " TLB-ACCESS");
+ arm_spe_pkt_snprintf(&err, &buf, &blen, " TLB-ACCESS");
if (payload & 0x20)
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " TLB-REFILL");
+ arm_spe_pkt_snprintf(&err, &buf, &blen, " TLB-REFILL");
if (payload & 0x40)
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " NOT-TAKEN");
+ arm_spe_pkt_snprintf(&err, &buf, &blen, " NOT-TAKEN");
if (payload & 0x80)
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " MISPRED");
+ arm_spe_pkt_snprintf(&err, &buf, &blen, " MISPRED");
if (idx > 1) {
if (payload & 0x100)
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " LLC-ACCESS");
+ arm_spe_pkt_snprintf(&err, &buf, &blen, " LLC-ACCESS");
if (payload & 0x200)
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " LLC-REFILL");
+ arm_spe_pkt_snprintf(&err, &buf, &blen, " LLC-REFILL");
if (payload & 0x400)
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " REMOTE-ACCESS");
+ arm_spe_pkt_snprintf(&err, &buf, &blen, " REMOTE-ACCESS");
}
- if (ret < 0)
- return ret;
- blen -= ret;
- return buf_len - blen;
+ break;
case ARM_SPE_OP_TYPE:
switch (idx) {
case 0:
- return arm_spe_pkt_snprintf(&err, &buf, &blen,
+ arm_spe_pkt_snprintf(&err, &buf, &blen,
payload & 0x1 ? "COND-SELECT" : "INSN-OTHER");
+ break;
case 1:
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen,
- payload & 0x1 ? "ST" : "LD");
+ arm_spe_pkt_snprintf(&err, &buf, &blen,
+ payload & 0x1 ? "ST" : "LD");

if (payload & 0x2) {
if (payload & 0x4)
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " AT");
+ arm_spe_pkt_snprintf(&err, &buf, &blen, " AT");
if (payload & 0x8)
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " EXCL");
+ arm_spe_pkt_snprintf(&err, &buf, &blen, " EXCL");
if (payload & 0x10)
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " AR");
+ arm_spe_pkt_snprintf(&err, &buf, &blen, " AR");
} else if (payload & 0x4) {
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " SIMD-FP");
+ arm_spe_pkt_snprintf(&err, &buf, &blen, " SIMD-FP");
}
-
- if (ret < 0)
- return ret;
- blen -= ret;
- return buf_len - blen;
-
+ break;
case 2:
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "B");
+ arm_spe_pkt_snprintf(&err, &buf, &blen, "B");

if (payload & 0x1)
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " COND");
+ arm_spe_pkt_snprintf(&err, &buf, &blen, " COND");
if (payload & 0x2)
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " IND");
-
- if (ret < 0)
- return ret;
- blen -= ret;
- return buf_len - blen;
+ arm_spe_pkt_snprintf(&err, &buf, &blen, " IND");

+ break;
default:
- return 0;
+ /* Unknown index */
+ err = -1;
+ break;
}
+ break;
case ARM_SPE_DATA_SOURCE:
case ARM_SPE_TIMESTAMP:
- return arm_spe_pkt_snprintf(&err, &buf, &blen, "%s %lld", name, payload);
+ arm_spe_pkt_snprintf(&err, &buf, &blen, "%s %lld", name, payload);
+ break;
case ARM_SPE_ADDRESS:
switch (idx) {
case 0:
@@ -394,48 +389,59 @@ int arm_spe_pkt_desc(const struct arm_spe_pkt *packet, char *buf,
ns = !!(packet->payload & NS_FLAG);
el = (packet->payload & EL_FLAG) >> 61;
payload &= ~(0xffULL << 56);
- return arm_spe_pkt_snprintf(&err, &buf, &blen,
+ arm_spe_pkt_snprintf(&err, &buf, &blen,
"%s 0x%llx el%d ns=%d",
(idx == 1) ? "TGT" : "PC", payload, el, ns);
+ break;
case 2:
- return arm_spe_pkt_snprintf(&err, &buf, &blen,
- "VA 0x%llx", payload);
+ arm_spe_pkt_snprintf(&err, &buf, &blen,
+ "VA 0x%llx", payload);
+ break;
case 3:
ns = !!(packet->payload & NS_FLAG);
payload &= ~(0xffULL << 56);
- return arm_spe_pkt_snprintf(&err, &buf, &blen,
- "PA 0x%llx ns=%d", payload, ns);
+ arm_spe_pkt_snprintf(&err, &buf, &blen,
+ "PA 0x%llx ns=%d", payload, ns);
+ break;
default:
- return 0;
+ /* Unknown index */
+ err = -1;
+ break;
}
+ break;
case ARM_SPE_CONTEXT:
- return arm_spe_pkt_snprintf(&err, &buf, &blen, "%s 0x%lx el%d",
- name, (unsigned long)payload, idx + 1);
+ arm_spe_pkt_snprintf(&err, &buf, &blen, "%s 0x%lx el%d",
+ name, (unsigned long)payload, idx + 1);
+ break;
case ARM_SPE_COUNTER:
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "%s %d ", name,
- (unsigned short)payload);
+ arm_spe_pkt_snprintf(&err, &buf, &blen, "%s %d ", name,
+ (unsigned short)payload);
switch (idx) {
case 0:
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "TOT");
+ arm_spe_pkt_snprintf(&err, &buf, &blen, "TOT");
break;
case 1:
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "ISSUE");
+ arm_spe_pkt_snprintf(&err, &buf, &blen, "ISSUE");
break;
case 2:
- ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "XLAT");
+ arm_spe_pkt_snprintf(&err, &buf, &blen, "XLAT");
break;
default:
- ret = 0;
break;
}
- if (ret < 0)
- return ret;
- blen -= ret;
- return buf_len - blen;
+ break;
default:
+ /* Unknown index */
+ err = -1;
break;
}

- return arm_spe_pkt_snprintf(&err, &buf, &blen, "%s 0x%llx (%d)",
- name, payload, packet->index);
+ /* Output raw data if detect any error */
+ if (err) {
+ err = 0;
+ arm_spe_pkt_snprintf(&err, &buf_orig, &buf_len, "%s 0x%llx (%d)",
+ name, payload, packet->index);
+ }
+
+ return err;
}
diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c
index 3882a5360ada..8901a1656a41 100644
--- a/tools/perf/util/arm-spe.c
+++ b/tools/perf/util/arm-spe.c
@@ -113,7 +113,7 @@ static void arm_spe_dump(struct arm_spe *spe __maybe_unused,
if (ret > 0) {
ret = arm_spe_pkt_desc(&packet, desc,
ARM_SPE_PKT_DESC_MAX);
- if (ret > 0)
+ if (!ret)
color_fprintf(stdout, color, " %s\n", desc);
} else {
color_fprintf(stdout, color, " Bad packet!\n");
--
2.17.1

2020-11-11 07:25:24

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 17/22] perf arm-spe: Remove size condition checking for events

In the Armv8 ARM (ARM DDI 0487F.c), chapter "D10.2.6 Events packet", it
describes the event bit is valid with specific payload requirement. For
example, the Last Level cache access event, the bit is defined as:

E[8], byte 1 bit [0], when SZ == 0b01 , when SZ == 0b10 ,
or when SZ == 0b11

It requires the payload size is at least 2 bytes, when byte 1 (start
counting from 0) is valid, E[8] (bit 0 in byte 1) can be used for LLC
access event type. For safety, the code checks the condition for
payload size firstly, if meet the requirement for payload size, then
continue to parse event type.

If review function arm_spe_get_payload(), it has used cast, so any bytes
beyond the valid size have been set to zeros.

For this reason, we don't need to check payload size anymore afterwards
when parse events, thus this patch removes payload size conditions.

Suggested-by: Andre Przywara <[email protected]>
Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
---
tools/perf/util/arm-spe-decoder/arm-spe-decoder.c | 9 +++------
.../util/arm-spe-decoder/arm-spe-pkt-decoder.c | 14 ++++++--------
2 files changed, 9 insertions(+), 14 deletions(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c
index cac2ef79c025..90d575cee1b9 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c
@@ -192,16 +192,13 @@ static int arm_spe_read_record(struct arm_spe_decoder *decoder)
if (payload & BIT(EV_TLB_ACCESS))
decoder->record.type |= ARM_SPE_TLB_ACCESS;

- if ((idx == 2 || idx == 4 || idx == 8) &&
- (payload & BIT(EV_LLC_MISS)))
+ if (payload & BIT(EV_LLC_MISS))
decoder->record.type |= ARM_SPE_LLC_MISS;

- if ((idx == 2 || idx == 4 || idx == 8) &&
- (payload & BIT(EV_LLC_ACCESS)))
+ if (payload & BIT(EV_LLC_ACCESS))
decoder->record.type |= ARM_SPE_LLC_ACCESS;

- if ((idx == 2 || idx == 4 || idx == 8) &&
- (payload & BIT(EV_REMOTE_ACCESS)))
+ if (payload & BIT(EV_REMOTE_ACCESS))
decoder->record.type |= ARM_SPE_REMOTE_ACCESS;

if (payload & BIT(EV_MISPRED))
diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index 2ebbd75d9b6a..64811021f36c 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -311,14 +311,12 @@ static int arm_spe_pkt_desc_event(const struct arm_spe_pkt *packet,
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " NOT-TAKEN");
if (payload & BIT(EV_MISPRED))
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " MISPRED");
- if (packet->index > 1) {
- if (payload & BIT(EV_LLC_ACCESS))
- arm_spe_pkt_snprintf(&err, &buf, &buf_len, " LLC-ACCESS");
- if (payload & BIT(EV_LLC_MISS))
- arm_spe_pkt_snprintf(&err, &buf, &buf_len, " LLC-REFILL");
- if (payload & BIT(EV_REMOTE_ACCESS))
- arm_spe_pkt_snprintf(&err, &buf, &buf_len, " REMOTE-ACCESS");
- }
+ if (payload & BIT(EV_LLC_ACCESS))
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " LLC-ACCESS");
+ if (payload & BIT(EV_LLC_MISS))
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " LLC-REFILL");
+ if (payload & BIT(EV_REMOTE_ACCESS))
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " REMOTE-ACCESS");

return err;
}
--
2.17.1

2020-11-11 07:25:48

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 09/22] perf arm-spe: Add new function arm_spe_pkt_desc_addr()

This patch moves out the address parsing code from arm_spe_pkt_desc()
and uses the new introduced function arm_spe_pkt_desc_addr() to process
address packet.

Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
---
.../arm-spe-decoder/arm-spe-pkt-decoder.c | 64 +++++++++++--------
1 file changed, 38 insertions(+), 26 deletions(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index a43880e06547..0aa15632e87b 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -288,10 +288,46 @@ static int arm_spe_pkt_snprintf(int *err, char **buf_p, size_t *blen,
return ret;
}

+static int arm_spe_pkt_desc_addr(const struct arm_spe_pkt *packet,
+ char *buf, size_t buf_len)
+{
+ int ns, el, idx = packet->index;
+ u64 payload = packet->payload;
+ int err = 0;
+
+ switch (idx) {
+ case 0:
+ case 1:
+ ns = !!(packet->payload & NS_FLAG);
+ el = (packet->payload & EL_FLAG) >> 61;
+ payload &= ~(0xffULL << 56);
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len,
+ "%s 0x%llx el%d ns=%d",
+ (idx == 1) ? "TGT" : "PC", payload, el, ns);
+ break;
+ case 2:
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len,
+ "VA 0x%llx", payload);
+ break;
+ case 3:
+ ns = !!(packet->payload & NS_FLAG);
+ payload &= ~(0xffULL << 56);
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len,
+ "PA 0x%llx ns=%d", payload, ns);
+ break;
+ default:
+ /* Unknown index */
+ err = -1;
+ break;
+ }
+
+ return err;
+}
+
int arm_spe_pkt_desc(const struct arm_spe_pkt *packet, char *buf,
size_t buf_len)
{
- int ns, el, idx = packet->index;
+ int idx = packet->index;
unsigned long long payload = packet->payload;
const char *name = arm_spe_pkt_name(packet->type);
char *buf_orig = buf;
@@ -373,31 +409,7 @@ int arm_spe_pkt_desc(const struct arm_spe_pkt *packet, char *buf,
arm_spe_pkt_snprintf(&err, &buf, &blen, "%s %lld", name, payload);
break;
case ARM_SPE_ADDRESS:
- switch (idx) {
- case 0:
- case 1:
- ns = !!(packet->payload & NS_FLAG);
- el = (packet->payload & EL_FLAG) >> 61;
- payload &= ~(0xffULL << 56);
- arm_spe_pkt_snprintf(&err, &buf, &blen,
- "%s 0x%llx el%d ns=%d",
- (idx == 1) ? "TGT" : "PC", payload, el, ns);
- break;
- case 2:
- arm_spe_pkt_snprintf(&err, &buf, &blen,
- "VA 0x%llx", payload);
- break;
- case 3:
- ns = !!(packet->payload & NS_FLAG);
- payload &= ~(0xffULL << 56);
- arm_spe_pkt_snprintf(&err, &buf, &blen,
- "PA 0x%llx ns=%d", payload, ns);
- break;
- default:
- /* Unknown index */
- err = -1;
- break;
- }
+ err = arm_spe_pkt_desc_addr(packet, buf, buf_len);
break;
case ARM_SPE_CONTEXT:
arm_spe_pkt_snprintf(&err, &buf, &blen, "%s 0x%lx el%d",
--
2.17.1

2020-11-11 07:25:48

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 16/22] perf arm-spe: Refactor event type handling

Move the enums of event types to arm-spe-pkt-decoder.h, thus function
arm_spe_pkt_desc() can them for bitmasks.

Suggested-by: Andre Przywara <[email protected]>
Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
---
.../util/arm-spe-decoder/arm-spe-decoder.h | 17 --------------
.../arm-spe-decoder/arm-spe-pkt-decoder.c | 22 +++++++++----------
.../arm-spe-decoder/arm-spe-pkt-decoder.h | 18 +++++++++++++++
3 files changed, 29 insertions(+), 28 deletions(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-decoder.h b/tools/perf/util/arm-spe-decoder/arm-spe-decoder.h
index a5111a8d4360..24727b8ca7ff 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-decoder.h
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-decoder.h
@@ -13,23 +13,6 @@

#include "arm-spe-pkt-decoder.h"

-enum arm_spe_events {
- EV_EXCEPTION_GEN = 0,
- EV_RETIRED = 1,
- EV_L1D_ACCESS = 2,
- EV_L1D_REFILL = 3,
- EV_TLB_ACCESS = 4,
- EV_TLB_WALK = 5,
- EV_NOT_TAKEN = 6,
- EV_MISPRED = 7,
- EV_LLC_ACCESS = 8,
- EV_LLC_MISS = 9,
- EV_REMOTE_ACCESS = 10,
- EV_ALIGNMENT = 11,
- EV_PARTIAL_PREDICATE = 17,
- EV_EMPTY_PREDICATE = 18,
-};
-
enum arm_spe_sample_type {
ARM_SPE_L1D_ACCESS = 1 << 0,
ARM_SPE_L1D_MISS = 1 << 1,
diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index af87d3c8cb50..2ebbd75d9b6a 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -295,28 +295,28 @@ static int arm_spe_pkt_desc_event(const struct arm_spe_pkt *packet,

arm_spe_pkt_snprintf(&err, &buf, &buf_len, "EV");

- if (payload & 0x1)
+ if (payload & BIT(EV_EXCEPTION_GEN))
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " EXCEPTION-GEN");
- if (payload & 0x2)
+ if (payload & BIT(EV_RETIRED))
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " RETIRED");
- if (payload & 0x4)
+ if (payload & BIT(EV_L1D_ACCESS))
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " L1D-ACCESS");
- if (payload & 0x8)
+ if (payload & BIT(EV_L1D_REFILL))
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " L1D-REFILL");
- if (payload & 0x10)
+ if (payload & BIT(EV_TLB_ACCESS))
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " TLB-ACCESS");
- if (payload & 0x20)
+ if (payload & BIT(EV_TLB_WALK))
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " TLB-REFILL");
- if (payload & 0x40)
+ if (payload & BIT(EV_NOT_TAKEN))
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " NOT-TAKEN");
- if (payload & 0x80)
+ if (payload & BIT(EV_MISPRED))
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " MISPRED");
if (packet->index > 1) {
- if (payload & 0x100)
+ if (payload & BIT(EV_LLC_ACCESS))
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " LLC-ACCESS");
- if (payload & 0x200)
+ if (payload & BIT(EV_LLC_MISS))
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " LLC-REFILL");
- if (payload & 0x400)
+ if (payload & BIT(EV_REMOTE_ACCESS))
arm_spe_pkt_snprintf(&err, &buf, &buf_len, " REMOTE-ACCESS");
}

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
index 7d8e34e35f05..42ed4e61ede2 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
@@ -87,6 +87,24 @@ struct arm_spe_pkt {
#define SPE_CNT_PKT_HDR_INDEX_ISSUE_LAT 0x1
#define SPE_CNT_PKT_HDR_INDEX_TRANS_LAT 0x2

+/* Event packet payload */
+enum arm_spe_events {
+ EV_EXCEPTION_GEN = 0,
+ EV_RETIRED = 1,
+ EV_L1D_ACCESS = 2,
+ EV_L1D_REFILL = 3,
+ EV_TLB_ACCESS = 4,
+ EV_TLB_WALK = 5,
+ EV_NOT_TAKEN = 6,
+ EV_MISPRED = 7,
+ EV_LLC_ACCESS = 8,
+ EV_LLC_MISS = 9,
+ EV_REMOTE_ACCESS = 10,
+ EV_ALIGNMENT = 11,
+ EV_PARTIAL_PREDICATE = 17,
+ EV_EMPTY_PREDICATE = 18,
+};
+
const char *arm_spe_pkt_name(enum arm_spe_pkt_type);

int arm_spe_get_packet(const unsigned char *buf, size_t len,
--
2.17.1

2020-11-11 07:25:49

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 18/22] perf arm-spe: Add new function arm_spe_pkt_desc_op_type()

The operation type packet is complex and contains subclass; the parsing
flow causes deep indentation; for more readable, this patch introduces
a new function arm_spe_pkt_desc_op_type() which is used for operation
type parsing.

Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
---
.../arm-spe-decoder/arm-spe-pkt-decoder.c | 79 +++++++++++--------
1 file changed, 45 insertions(+), 34 deletions(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index 64811021f36c..4f93b75c87e6 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -321,6 +321,50 @@ static int arm_spe_pkt_desc_event(const struct arm_spe_pkt *packet,
return err;
}

+static int arm_spe_pkt_desc_op_type(const struct arm_spe_pkt *packet,
+ char *buf, size_t buf_len)
+{
+ u64 payload = packet->payload;
+ int err = 0;
+
+ switch (packet->index) {
+ case 0:
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len,
+ payload & 0x1 ? "COND-SELECT" : "INSN-OTHER");
+ break;
+ case 1:
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len,
+ payload & 0x1 ? "ST" : "LD");
+
+ if (payload & 0x2) {
+ if (payload & 0x4)
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " AT");
+ if (payload & 0x8)
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " EXCL");
+ if (payload & 0x10)
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " AR");
+ } else if (payload & 0x4) {
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " SIMD-FP");
+ }
+ break;
+ case 2:
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, "B");
+
+ if (payload & 0x1)
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " COND");
+ if (payload & 0x2)
+ arm_spe_pkt_snprintf(&err, &buf, &buf_len, " IND");
+
+ break;
+ default:
+ /* Unknown index */
+ err = -1;
+ break;
+ }
+
+ return err;
+}
+
static int arm_spe_pkt_desc_addr(const struct arm_spe_pkt *packet,
char *buf, size_t buf_len)
{
@@ -404,40 +448,7 @@ int arm_spe_pkt_desc(const struct arm_spe_pkt *packet, char *buf,
err = arm_spe_pkt_desc_event(packet, buf, buf_len);
break;
case ARM_SPE_OP_TYPE:
- switch (idx) {
- case 0:
- arm_spe_pkt_snprintf(&err, &buf, &blen,
- payload & 0x1 ? "COND-SELECT" : "INSN-OTHER");
- break;
- case 1:
- arm_spe_pkt_snprintf(&err, &buf, &blen,
- payload & 0x1 ? "ST" : "LD");
-
- if (payload & 0x2) {
- if (payload & 0x4)
- arm_spe_pkt_snprintf(&err, &buf, &blen, " AT");
- if (payload & 0x8)
- arm_spe_pkt_snprintf(&err, &buf, &blen, " EXCL");
- if (payload & 0x10)
- arm_spe_pkt_snprintf(&err, &buf, &blen, " AR");
- } else if (payload & 0x4) {
- arm_spe_pkt_snprintf(&err, &buf, &blen, " SIMD-FP");
- }
- break;
- case 2:
- arm_spe_pkt_snprintf(&err, &buf, &blen, "B");
-
- if (payload & 0x1)
- arm_spe_pkt_snprintf(&err, &buf, &blen, " COND");
- if (payload & 0x2)
- arm_spe_pkt_snprintf(&err, &buf, &blen, " IND");
-
- break;
- default:
- /* Unknown index */
- err = -1;
- break;
- }
+ err = arm_spe_pkt_desc_op_type(packet, buf, buf_len);
break;
case ARM_SPE_DATA_SOURCE:
case ARM_SPE_TIMESTAMP:
--
2.17.1

2020-11-11 07:26:36

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 14/22] perf arm-spe: Refactor counter packet handling

This patch defines macros for counter packet header, and uses macros to
replace hard code values in functions arm_spe_get_counter() and
arm_spe_pkt_desc().

In the function arm_spe_get_counter(), adds a new line for more
readable.

Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
---
tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c | 11 ++++++-----
tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h | 5 +++++
2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index e8c9da1d4280..5178bbe64422 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -152,10 +152,11 @@ static int arm_spe_get_counter(const unsigned char *buf, size_t len,
const unsigned char ext_hdr, struct arm_spe_pkt *packet)
{
packet->type = ARM_SPE_COUNTER;
+
if (ext_hdr)
- packet->index = ((buf[0] & 0x3) << 3) | (buf[1] & 0x7);
+ packet->index = SPE_HDR_EXTENDED_INDEX(buf[0], buf[1]);
else
- packet->index = buf[0] & 0x7;
+ packet->index = SPE_HDR_SHORT_INDEX(buf[0]);

return arm_spe_get_payload(buf, len, ext_hdr, packet);
}
@@ -333,13 +334,13 @@ static int arm_spe_pkt_desc_counter(const struct arm_spe_pkt *packet,
(unsigned short)payload);

switch (packet->index) {
- case 0:
+ case SPE_CNT_PKT_HDR_INDEX_TOTAL_LAT:
arm_spe_pkt_snprintf(&err, &buf, &buf_len, "TOT");
break;
- case 1:
+ case SPE_CNT_PKT_HDR_INDEX_ISSUE_LAT:
arm_spe_pkt_snprintf(&err, &buf, &buf_len, "ISSUE");
break;
- case 2:
+ case SPE_CNT_PKT_HDR_INDEX_TRANS_LAT:
arm_spe_pkt_snprintf(&err, &buf, &buf_len, "XLAT");
break;
default:
diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
index 9bc876bffd35..7d8e34e35f05 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
@@ -82,6 +82,11 @@ struct arm_spe_pkt {
/* Context packet header */
#define SPE_CTX_PKT_HDR_INDEX(h) ((h) & GENMASK_ULL(1, 0))

+/* Counter packet header */
+#define SPE_CNT_PKT_HDR_INDEX_TOTAL_LAT 0x0
+#define SPE_CNT_PKT_HDR_INDEX_ISSUE_LAT 0x1
+#define SPE_CNT_PKT_HDR_INDEX_TRANS_LAT 0x2
+
const char *arm_spe_pkt_name(enum arm_spe_pkt_type);

int arm_spe_get_packet(const unsigned char *buf, size_t len,
--
2.17.1

2020-11-11 07:27:22

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 03/22] perf arm-spe: Refactor payload size calculation

This patch defines macro to extract "sz" field from header, and renames
the function payloadlen() to arm_spe_payload_len().

Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
---
.../util/arm-spe-decoder/arm-spe-pkt-decoder.c | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index 7c7b5eb09fba..06b3eec4494e 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -69,22 +69,22 @@ const char *arm_spe_pkt_name(enum arm_spe_pkt_type type)
return arm_spe_packet_name[type];
}

-/* return ARM SPE payload size from its encoding,
- * which is in bits 5:4 of the byte.
- * 00 : byte
- * 01 : halfword (2)
- * 10 : word (4)
- * 11 : doubleword (8)
+/*
+ * Extracts the field "sz" from header bits and converts to bytes:
+ * 00 : byte (1)
+ * 01 : halfword (2)
+ * 10 : word (4)
+ * 11 : doubleword (8)
*/
-static int payloadlen(unsigned char byte)
+static unsigned int arm_spe_payload_len(unsigned char hdr)
{
- return 1 << ((byte & 0x30) >> 4);
+ return 1U << ((hdr & GENMASK_ULL(5, 4)) >> 4);
}

static int arm_spe_get_payload(const unsigned char *buf, size_t len,
struct arm_spe_pkt *packet)
{
- size_t payload_len = payloadlen(buf[0]);
+ size_t payload_len = arm_spe_payload_len(buf[0]);

if (len < 1 + payload_len)
return ARM_SPE_NEED_MORE_BYTES;
--
2.17.1

2020-11-11 07:27:29

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 05/22] perf arm-spe: Fix packet length handling

When processing address packet and counter packet, if the packet
contains extended header, it misses to account the extra one byte for
header length calculation, thus returns the wrong packet length.

To correct the packet length calculation, one possible fixing is simply
to plus extra 1 for extended header, but will spread some duplicate code
in the flows for processing address packet and counter packet.
Alternatively, we can refine the function arm_spe_get_payload() to not
only support short header and allow it to support extended header, and
rely on it for the packet length calculation.

So this patch refactors function arm_spe_get_payload() with a new
argument 'ext_hdr' for support extended header; the packet processing
flows can invoke this function to unify the packet length calculation.

Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
---
.../arm-spe-decoder/arm-spe-pkt-decoder.c | 34 +++++++------------
1 file changed, 12 insertions(+), 22 deletions(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index f1b4cb008837..04fd7fd7c15f 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -82,14 +82,15 @@ static unsigned int arm_spe_payload_len(unsigned char hdr)
}

static int arm_spe_get_payload(const unsigned char *buf, size_t len,
+ unsigned char ext_hdr,
struct arm_spe_pkt *packet)
{
- size_t payload_len = arm_spe_payload_len(buf[0]);
+ size_t payload_len = arm_spe_payload_len(buf[ext_hdr]);

- if (len < 1 + payload_len)
+ if (len < 1 + ext_hdr + payload_len)
return ARM_SPE_NEED_MORE_BYTES;

- buf++;
+ buf += 1 + ext_hdr;

switch (payload_len) {
case 1: packet->payload = *(uint8_t *)buf; break;
@@ -99,7 +100,7 @@ static int arm_spe_get_payload(const unsigned char *buf, size_t len,
default: return ARM_SPE_BAD_PACKET;
}

- return 1 + payload_len;
+ return 1 + ext_hdr + payload_len;
}

static int arm_spe_get_pad(struct arm_spe_pkt *packet)
@@ -130,7 +131,7 @@ static int arm_spe_get_timestamp(const unsigned char *buf, size_t len,
struct arm_spe_pkt *packet)
{
packet->type = ARM_SPE_TIMESTAMP;
- return arm_spe_get_payload(buf, len, packet);
+ return arm_spe_get_payload(buf, len, 0, packet);
}

static int arm_spe_get_events(const unsigned char *buf, size_t len,
@@ -145,14 +146,14 @@ static int arm_spe_get_events(const unsigned char *buf, size_t len,
*/
packet->index = arm_spe_payload_len(buf[0]);

- return arm_spe_get_payload(buf, len, packet);
+ return arm_spe_get_payload(buf, len, 0, packet);
}

static int arm_spe_get_data_source(const unsigned char *buf, size_t len,
struct arm_spe_pkt *packet)
{
packet->type = ARM_SPE_DATA_SOURCE;
- return arm_spe_get_payload(buf, len, packet);
+ return arm_spe_get_payload(buf, len, 0, packet);
}

static int arm_spe_get_context(const unsigned char *buf, size_t len,
@@ -160,8 +161,7 @@ static int arm_spe_get_context(const unsigned char *buf, size_t len,
{
packet->type = ARM_SPE_CONTEXT;
packet->index = buf[0] & 0x3;
-
- return arm_spe_get_payload(buf, len, packet);
+ return arm_spe_get_payload(buf, len, 0, packet);
}

static int arm_spe_get_op_type(const unsigned char *buf, size_t len,
@@ -169,41 +169,31 @@ static int arm_spe_get_op_type(const unsigned char *buf, size_t len,
{
packet->type = ARM_SPE_OP_TYPE;
packet->index = buf[0] & 0x3;
- return arm_spe_get_payload(buf, len, packet);
+ return arm_spe_get_payload(buf, len, 0, packet);
}

static int arm_spe_get_counter(const unsigned char *buf, size_t len,
const unsigned char ext_hdr, struct arm_spe_pkt *packet)
{
- if (len < 2)
- return ARM_SPE_NEED_MORE_BYTES;
-
packet->type = ARM_SPE_COUNTER;
if (ext_hdr)
packet->index = ((buf[0] & 0x3) << 3) | (buf[1] & 0x7);
else
packet->index = buf[0] & 0x7;

- packet->payload = le16_to_cpu(*(uint16_t *)(buf + 1));
-
- return 1 + ext_hdr + 2;
+ return arm_spe_get_payload(buf, len, ext_hdr, packet);
}

static int arm_spe_get_addr(const unsigned char *buf, size_t len,
const unsigned char ext_hdr, struct arm_spe_pkt *packet)
{
- if (len < 8)
- return ARM_SPE_NEED_MORE_BYTES;
-
packet->type = ARM_SPE_ADDRESS;
if (ext_hdr)
packet->index = ((buf[0] & 0x3) << 3) | (buf[1] & 0x7);
else
packet->index = buf[0] & 0x7;

- memcpy_le64(&packet->payload, buf + 1, 8);
-
- return 1 + ext_hdr + 8;
+ return arm_spe_get_payload(buf, len, ext_hdr, packet);
}

static int arm_spe_do_get_packet(const unsigned char *buf, size_t len,
--
2.17.1

2020-11-11 07:27:30

by Leo Yan

[permalink] [raw]
Subject: [PATCH v8 08/22] perf arm-spe: Refactor packet header parsing

The packet header parsing uses the hard coded values and it uses nested
if-else statements.

To improve the readability, this patch refactors the macros for packet
header format so it removes the hard coded values. Furthermore, based
on the new mask macros it reduces the nested if-else statements and
changes to use the flat conditions checking, this is directive and can
easily map to the descriptions in ARMv8-a architecture reference manual
(ARM DDI 0487E.a), chapter 'D10.1.5 Statistical Profiling Extension
protocol packet headers'.

Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
---
.../arm-spe-decoder/arm-spe-pkt-decoder.c | 92 +++++++++----------
.../arm-spe-decoder/arm-spe-pkt-decoder.h | 20 ++++
2 files changed, 61 insertions(+), 51 deletions(-)

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index 424ff5862aa1..a43880e06547 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -16,28 +16,6 @@
#define NS_FLAG BIT(63)
#define EL_FLAG (BIT(62) | BIT(61))

-#define SPE_HEADER0_PAD 0x0
-#define SPE_HEADER0_END 0x1
-#define SPE_HEADER0_ADDRESS 0x30 /* address packet (short) */
-#define SPE_HEADER0_ADDRESS_MASK 0x38
-#define SPE_HEADER0_COUNTER 0x18 /* counter packet (short) */
-#define SPE_HEADER0_COUNTER_MASK 0x38
-#define SPE_HEADER0_TIMESTAMP 0x71
-#define SPE_HEADER0_TIMESTAMP 0x71
-#define SPE_HEADER0_EVENTS 0x2
-#define SPE_HEADER0_EVENTS_MASK 0xf
-#define SPE_HEADER0_SOURCE 0x3
-#define SPE_HEADER0_SOURCE_MASK 0xf
-#define SPE_HEADER0_CONTEXT 0x24
-#define SPE_HEADER0_CONTEXT_MASK 0x3c
-#define SPE_HEADER0_OP_TYPE 0x8
-#define SPE_HEADER0_OP_TYPE_MASK 0x3c
-#define SPE_HEADER1_ALIGNMENT 0x0
-#define SPE_HEADER1_ADDRESS 0xb0 /* address packet (extended) */
-#define SPE_HEADER1_ADDRESS_MASK 0xf8
-#define SPE_HEADER1_COUNTER 0x98 /* counter packet (extended) */
-#define SPE_HEADER1_COUNTER_MASK 0xf8
-
#if __BYTE_ORDER == __BIG_ENDIAN
#define le16_to_cpu bswap_16
#define le32_to_cpu bswap_32
@@ -200,46 +178,58 @@ static int arm_spe_get_addr(const unsigned char *buf, size_t len,
static int arm_spe_do_get_packet(const unsigned char *buf, size_t len,
struct arm_spe_pkt *packet)
{
- unsigned int byte;
+ unsigned int hdr;
+ unsigned char ext_hdr = 0;

memset(packet, 0, sizeof(struct arm_spe_pkt));

if (!len)
return ARM_SPE_NEED_MORE_BYTES;

- byte = buf[0];
- if (byte == SPE_HEADER0_PAD)
+ hdr = buf[0];
+
+ if (hdr == SPE_HEADER0_PAD)
return arm_spe_get_pad(packet);
- else if (byte == SPE_HEADER0_END) /* no timestamp at end of record */
+
+ if (hdr == SPE_HEADER0_END) /* no timestamp at end of record */
return arm_spe_get_end(packet);
- else if (byte & 0xc0 /* 0y11xxxxxx */) {
- if (byte & 0x80) {
- if ((byte & SPE_HEADER0_ADDRESS_MASK) == SPE_HEADER0_ADDRESS)
- return arm_spe_get_addr(buf, len, 0, packet);
- if ((byte & SPE_HEADER0_COUNTER_MASK) == SPE_HEADER0_COUNTER)
- return arm_spe_get_counter(buf, len, 0, packet);
- } else
- if (byte == SPE_HEADER0_TIMESTAMP)
- return arm_spe_get_timestamp(buf, len, packet);
- else if ((byte & SPE_HEADER0_EVENTS_MASK) == SPE_HEADER0_EVENTS)
- return arm_spe_get_events(buf, len, packet);
- else if ((byte & SPE_HEADER0_SOURCE_MASK) == SPE_HEADER0_SOURCE)
- return arm_spe_get_data_source(buf, len, packet);
- else if ((byte & SPE_HEADER0_CONTEXT_MASK) == SPE_HEADER0_CONTEXT)
- return arm_spe_get_context(buf, len, packet);
- else if ((byte & SPE_HEADER0_OP_TYPE_MASK) == SPE_HEADER0_OP_TYPE)
- return arm_spe_get_op_type(buf, len, packet);
- } else if ((byte & 0xe0) == 0x20 /* 0y001xxxxx */) {
- /* 16-bit header */
- byte = buf[1];
- if (byte == SPE_HEADER1_ALIGNMENT)
+
+ if (hdr == SPE_HEADER0_TIMESTAMP)
+ return arm_spe_get_timestamp(buf, len, packet);
+
+ if ((hdr & SPE_HEADER0_MASK1) == SPE_HEADER0_EVENTS)
+ return arm_spe_get_events(buf, len, packet);
+
+ if ((hdr & SPE_HEADER0_MASK1) == SPE_HEADER0_SOURCE)
+ return arm_spe_get_data_source(buf, len, packet);
+
+ if ((hdr & SPE_HEADER0_MASK2) == SPE_HEADER0_CONTEXT)
+ return arm_spe_get_context(buf, len, packet);
+
+ if ((hdr & SPE_HEADER0_MASK2) == SPE_HEADER0_OP_TYPE)
+ return arm_spe_get_op_type(buf, len, packet);
+
+ if ((hdr & SPE_HEADER0_MASK2) == SPE_HEADER0_EXTENDED) {
+ /* 16-bit extended format header */
+ ext_hdr = 1;
+
+ hdr = buf[1];
+ if (hdr == SPE_HEADER1_ALIGNMENT)
return arm_spe_get_alignment(buf, len, packet);
- else if ((byte & SPE_HEADER1_ADDRESS_MASK) == SPE_HEADER1_ADDRESS)
- return arm_spe_get_addr(buf, len, 1, packet);
- else if ((byte & SPE_HEADER1_COUNTER_MASK) == SPE_HEADER1_COUNTER)
- return arm_spe_get_counter(buf, len, 1, packet);
}

+ /*
+ * The short format header's byte 0 or the extended format header's
+ * byte 1 has been assigned to 'hdr', which uses the same encoding for
+ * address packet and counter packet, so don't need to distinguish if
+ * it's short format or extended format and handle in once.
+ */
+ if ((hdr & SPE_HEADER0_MASK3) == SPE_HEADER0_ADDRESS)
+ return arm_spe_get_addr(buf, len, ext_hdr, packet);
+
+ if ((hdr & SPE_HEADER0_MASK3) == SPE_HEADER0_COUNTER)
+ return arm_spe_get_counter(buf, len, ext_hdr, packet);
+
return ARM_SPE_BAD_PACKET;
}

diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
index 4c870521b8eb..129f43405eb1 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
@@ -36,6 +36,26 @@ struct arm_spe_pkt {
uint64_t payload;
};

+/* Short header (HEADER0) and extended header (HEADER1) */
+#define SPE_HEADER0_PAD 0x0
+#define SPE_HEADER0_END 0x1
+#define SPE_HEADER0_TIMESTAMP 0x71
+/* Mask for event & data source */
+#define SPE_HEADER0_MASK1 (GENMASK_ULL(7, 6) | GENMASK_ULL(3, 0))
+#define SPE_HEADER0_EVENTS 0x42
+#define SPE_HEADER0_SOURCE 0x43
+/* Mask for context & operation */
+#define SPE_HEADER0_MASK2 GENMASK_ULL(7, 2)
+#define SPE_HEADER0_CONTEXT 0x64
+#define SPE_HEADER0_OP_TYPE 0x48
+/* Mask for extended format */
+#define SPE_HEADER0_EXTENDED 0x20
+/* Mask for address & counter */
+#define SPE_HEADER0_MASK3 GENMASK_ULL(7, 3)
+#define SPE_HEADER0_ADDRESS 0xb0
+#define SPE_HEADER0_COUNTER 0x98
+#define SPE_HEADER1_ALIGNMENT 0x0
+
#define SPE_ADDR_PKT_HDR_INDEX_INS (0x0)
#define SPE_ADDR_PKT_HDR_INDEX_BRANCH (0x1)
#define SPE_ADDR_PKT_HDR_INDEX_DATA_VIRT (0x2)
--
2.17.1

2020-11-11 10:17:54

by Andre Przywara

[permalink] [raw]
Subject: Re: [PATCH v8 00/22] perf arm-spe: Refactor decoding & dumping flow

On 11/11/2020 07:11, Leo Yan wrote:

Hi Arnaldo, Ingo, Peter, (whoever feels responsible for taking this)

> This is patch set v8 for refactoring Arm SPE trace decoding and dumping.
I have reviewed every patch of this in anger, and am now fine with this
series. Given the bugs fixed, the improvements it brings in terms of
readability and maintainability, and the low risk it has on breaking
things, I would be happy to see it merged.

Thanks,
Andre.

> This version addresses Andre's comment to pass parameter '&buf_len' at
> the last call arm_spe_pkt_snprintf() in the function arm_spe_pkt_desc().
>
> This patch set is cleanly applied on the top of perf/core branch
> with commit 644bf4b0f7ac ("perf jevents: Add test for arch std events").
>
> I retested this patch set on Hisilicon D06 platform with commands
> "perf report -D" and "perf script", compared the decoding results
> between with this patch set and without this patch set, "diff" tool
> shows the result as expected.
>
> Changes from v7:
> - Changed to pass '&buf_len' for the last call arm_spe_pkt_snprintf() in
> the patch 07/22 (Andre).
>
> Changes from v6:
> - Removed the redundant comma from the string in the patch 21/22 "perf
> arm_spe: Decode memory tagging properties" (Dave);
> - Refined the return value for arm_spe_pkt_desc(): returns 0 for
> success, otherwise returns non zero for failures; handle error code at
> the end of function arm_spe_pkt_desc(); this is accomplished in the
> new patch 07/22 "perf arm-spe: Consolidate arm_spe_pkt_desc()'s
> return value" (Dave).
>
> Changes from v5:
> - Directly bail out arm_spe_pkt_snprintf() if any error occurred
> (Andre).
>
> Changes from v4:
> - Implemented a cumulative error for arm_spe_pkt_snprintf() and changed
> to condense code for printing strings (Dave);
> - Changed to check payload bits [55:52] for parse kernel address
> (Andre).
>
> Changes from v3:
> - Refined arm_spe_payload_len() and removed macro SPE_HEADER_SZ()
> (Andre);
> - Refined packet header index macros (Andre);
> - Added patch "perf arm_spe: Fixup top byte for data virtual address" to
> fixup the data virtual address for 64KB pages and refined comments for
> the fixup (Andre);
> - Added Andre's review tag (using "b4 am" command);
> - Changed the macros to SPE_PKT_IS_XXX() format to check operation types
> (Andre).
>
>
> Andre Przywara (1):
> perf arm_spe: Decode memory tagging properties
>
> Leo Yan (20):
> perf arm-spe: Include bitops.h for BIT() macro
> perf arm-spe: Fix a typo in comment
> perf arm-spe: Refactor payload size calculation
> perf arm-spe: Refactor arm_spe_get_events()
> perf arm-spe: Fix packet length handling
> perf arm-spe: Refactor printing string to buffer
> perf arm-spe: Consolidate arm_spe_pkt_desc()'s return value
> perf arm-spe: Refactor packet header parsing
> perf arm-spe: Add new function arm_spe_pkt_desc_addr()
> perf arm-spe: Refactor address packet handling
> perf arm_spe: Fixup top byte for data virtual address
> perf arm-spe: Refactor context packet handling
> perf arm-spe: Add new function arm_spe_pkt_desc_counter()
> perf arm-spe: Refactor counter packet handling
> perf arm-spe: Add new function arm_spe_pkt_desc_event()
> perf arm-spe: Refactor event type handling
> perf arm-spe: Remove size condition checking for events
> perf arm-spe: Add new function arm_spe_pkt_desc_op_type()
> perf arm-spe: Refactor operation packet handling
> perf arm-spe: Add more sub classes for operation packet
>
> Wei Li (1):
> perf arm-spe: Add support for ARMv8.3-SPE
>
> .../util/arm-spe-decoder/arm-spe-decoder.c | 59 +-
> .../util/arm-spe-decoder/arm-spe-decoder.h | 17 -
> .../arm-spe-decoder/arm-spe-pkt-decoder.c | 601 ++++++++++--------
> .../arm-spe-decoder/arm-spe-pkt-decoder.h | 122 +++-
> tools/perf/util/arm-spe.c | 2 +-
> 5 files changed, 479 insertions(+), 322 deletions(-)
>

2020-11-11 12:03:38

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v8 00/22] perf arm-spe: Refactor decoding & dumping flow

Em Wed, Nov 11, 2020 at 10:13:52AM +0000, Andr? Przywara escreveu:
> On 11/11/2020 07:11, Leo Yan wrote:
>
> Hi Arnaldo, Ingo, Peter, (whoever feels responsible for taking this)
>
> > This is patch set v8 for refactoring Arm SPE trace decoding and dumping.
> I have reviewed every patch of this in anger, and am now fine with this
> series. Given the bugs fixed, the improvements it brings in terms of
> readability and maintainability, and the low risk it has on breaking
> things, I would be happy to see it merged.

Ok, I'll have it in perf/core for v5.11, thanks!

- Arnaldo

> Thanks,
> Andre.
>
> > This version addresses Andre's comment to pass parameter '&buf_len' at
> > the last call arm_spe_pkt_snprintf() in the function arm_spe_pkt_desc().
> >
> > This patch set is cleanly applied on the top of perf/core branch
> > with commit 644bf4b0f7ac ("perf jevents: Add test for arch std events").
> >
> > I retested this patch set on Hisilicon D06 platform with commands
> > "perf report -D" and "perf script", compared the decoding results
> > between with this patch set and without this patch set, "diff" tool
> > shows the result as expected.
> >
> > Changes from v7:
> > - Changed to pass '&buf_len' for the last call arm_spe_pkt_snprintf() in
> > the patch 07/22 (Andre).
> >
> > Changes from v6:
> > - Removed the redundant comma from the string in the patch 21/22 "perf
> > arm_spe: Decode memory tagging properties" (Dave);
> > - Refined the return value for arm_spe_pkt_desc(): returns 0 for
> > success, otherwise returns non zero for failures; handle error code at
> > the end of function arm_spe_pkt_desc(); this is accomplished in the
> > new patch 07/22 "perf arm-spe: Consolidate arm_spe_pkt_desc()'s
> > return value" (Dave).
> >
> > Changes from v5:
> > - Directly bail out arm_spe_pkt_snprintf() if any error occurred
> > (Andre).
> >
> > Changes from v4:
> > - Implemented a cumulative error for arm_spe_pkt_snprintf() and changed
> > to condense code for printing strings (Dave);
> > - Changed to check payload bits [55:52] for parse kernel address
> > (Andre).
> >
> > Changes from v3:
> > - Refined arm_spe_payload_len() and removed macro SPE_HEADER_SZ()
> > (Andre);
> > - Refined packet header index macros (Andre);
> > - Added patch "perf arm_spe: Fixup top byte for data virtual address" to
> > fixup the data virtual address for 64KB pages and refined comments for
> > the fixup (Andre);
> > - Added Andre's review tag (using "b4 am" command);
> > - Changed the macros to SPE_PKT_IS_XXX() format to check operation types
> > (Andre).
> >
> >
> > Andre Przywara (1):
> > perf arm_spe: Decode memory tagging properties
> >
> > Leo Yan (20):
> > perf arm-spe: Include bitops.h for BIT() macro
> > perf arm-spe: Fix a typo in comment
> > perf arm-spe: Refactor payload size calculation
> > perf arm-spe: Refactor arm_spe_get_events()
> > perf arm-spe: Fix packet length handling
> > perf arm-spe: Refactor printing string to buffer
> > perf arm-spe: Consolidate arm_spe_pkt_desc()'s return value
> > perf arm-spe: Refactor packet header parsing
> > perf arm-spe: Add new function arm_spe_pkt_desc_addr()
> > perf arm-spe: Refactor address packet handling
> > perf arm_spe: Fixup top byte for data virtual address
> > perf arm-spe: Refactor context packet handling
> > perf arm-spe: Add new function arm_spe_pkt_desc_counter()
> > perf arm-spe: Refactor counter packet handling
> > perf arm-spe: Add new function arm_spe_pkt_desc_event()
> > perf arm-spe: Refactor event type handling
> > perf arm-spe: Remove size condition checking for events
> > perf arm-spe: Add new function arm_spe_pkt_desc_op_type()
> > perf arm-spe: Refactor operation packet handling
> > perf arm-spe: Add more sub classes for operation packet
> >
> > Wei Li (1):
> > perf arm-spe: Add support for ARMv8.3-SPE
> >
> > .../util/arm-spe-decoder/arm-spe-decoder.c | 59 +-
> > .../util/arm-spe-decoder/arm-spe-decoder.h | 17 -
> > .../arm-spe-decoder/arm-spe-pkt-decoder.c | 601 ++++++++++--------
> > .../arm-spe-decoder/arm-spe-pkt-decoder.h | 122 +++-
> > tools/perf/util/arm-spe.c | 2 +-
> > 5 files changed, 479 insertions(+), 322 deletions(-)
> >
>

--

- Arnaldo

2020-11-11 15:38:57

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v8 06/22] perf arm-spe: Refactor printing string to buffer

Em Wed, Nov 11, 2020 at 03:11:33PM +0800, Leo Yan escreveu:
> When outputs strings to the decoding buffer with function snprintf(),
> SPE decoder needs to detects if any error returns from snprintf() and if
> so needs to directly bail out. If snprintf() returns success, it needs
> to update buffer pointer and reduce the buffer length so can continue to
> output the next string into the consequent memory space.
>
> This complex logics are spreading in the function arm_spe_pkt_desc() so
> there has many duplicate codes for handling error detecting, increment
> buffer pointer and decrement buffer size.
>
> To avoid the duplicate code, this patch introduces a new helper function
> arm_spe_pkt_snprintf() which is used to wrap up the complex logics, and
> it's used by the caller arm_spe_pkt_desc().
>
> This patch also moves the variable 'blen' as the function's local
> variable, this allows to remove the unnecessary braces and improve the
> readability.
>
> Suggested-by: Dave Martin <[email protected]>
> Signed-off-by: Leo Yan <[email protected]>
> Reviewed-by: Andre Przywara <[email protected]>
> ---
> .../arm-spe-decoder/arm-spe-pkt-decoder.c | 260 +++++++++---------
> 1 file changed, 126 insertions(+), 134 deletions(-)
>
> diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
> index 04fd7fd7c15f..1970686f7020 100644
> --- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
> +++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
> @@ -9,6 +9,7 @@
> #include <endian.h>
> #include <byteswap.h>
> #include <linux/bitops.h>
> +#include <stdarg.h>
>
> #include "arm-spe-pkt-decoder.h"
>
> @@ -258,192 +259,183 @@ int arm_spe_get_packet(const unsigned char *buf, size_t len,
> return ret;
> }
>
> +static int arm_spe_pkt_snprintf(int *err, char **buf_p, size_t *blen,
> + const char *fmt, ...)
> +{
> + va_list ap;
> + int ret;
> +
> + /* Bail out if any error occurred */
> + if (err && *err)
> + return *err;
> +
> + va_start(ap, fmt);
> + ret = vsnprintf(*buf_p, *blen, fmt, ap);
> + va_end(ap);
> +
> + if (ret < 0) {
> + if (err && !*err)
> + *err = ret;
> +
> + /*
> + * A return value of (*blen - 1) or more means that the
> + * output was truncated and the buffer is overrun.
> + */
> + } else if (ret >= ((int)*blen - 1)) {
> + (*buf_p)[*blen - 1] = '\0';
> +
> + /*
> + * Set *err to 'ret' to avoid overflow if tries to
> + * fill this buffer sequentially.
> + */
> + if (err && !*err)
> + *err = ret;
> + } else {
> + *buf_p += ret;
> + *blen -= ret;
> + }
> +
> + return ret;
> +}
> +
> int arm_spe_pkt_desc(const struct arm_spe_pkt *packet, char *buf,
> size_t buf_len)
> {
> int ret, ns, el, idx = packet->index;
> unsigned long long payload = packet->payload;
> const char *name = arm_spe_pkt_name(packet->type);
> + size_t blen = buf_len;
> + int err = 0;
>
> switch (packet->type) {
> case ARM_SPE_BAD:
> case ARM_SPE_PAD:
> case ARM_SPE_END:
> - return snprintf(buf, buf_len, "%s", name);
> - case ARM_SPE_EVENTS: {
> - size_t blen = buf_len;
> -
> - ret = 0;
> - ret = snprintf(buf, buf_len, "EV");
> - buf += ret;
> - blen -= ret;
> - if (payload & 0x1) {
> - ret = snprintf(buf, buf_len, " EXCEPTION-GEN");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x2) {
> - ret = snprintf(buf, buf_len, " RETIRED");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x4) {
> - ret = snprintf(buf, buf_len, " L1D-ACCESS");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x8) {
> - ret = snprintf(buf, buf_len, " L1D-REFILL");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x10) {
> - ret = snprintf(buf, buf_len, " TLB-ACCESS");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x20) {
> - ret = snprintf(buf, buf_len, " TLB-REFILL");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x40) {
> - ret = snprintf(buf, buf_len, " NOT-TAKEN");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x80) {
> - ret = snprintf(buf, buf_len, " MISPRED");
> - buf += ret;
> - blen -= ret;
> - }
> + return arm_spe_pkt_snprintf(&err, &buf, &blen, "%s", name);
> + case ARM_SPE_EVENTS:
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "EV");
> +
> + if (payload & 0x1)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " EXCEPTION-GEN");

Isn't this 'ret +=' ? Otherwise if any of these arm_spe_pkt_snprintf()
calls are made the previous 'ret' value is simply discarded. Can you
clarify this?

I applied all patches before this one.

- Arnaldo

> + if (payload & 0x2)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " RETIRED");
> + if (payload & 0x4)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " L1D-ACCESS");
> + if (payload & 0x8)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " L1D-REFILL");
> + if (payload & 0x10)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " TLB-ACCESS");
> + if (payload & 0x20)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " TLB-REFILL");
> + if (payload & 0x40)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " NOT-TAKEN");
> + if (payload & 0x80)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " MISPRED");
> if (idx > 1) {
> - if (payload & 0x100) {
> - ret = snprintf(buf, buf_len, " LLC-ACCESS");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x200) {
> - ret = snprintf(buf, buf_len, " LLC-REFILL");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x400) {
> - ret = snprintf(buf, buf_len, " REMOTE-ACCESS");
> - buf += ret;
> - blen -= ret;
> - }
> + if (payload & 0x100)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " LLC-ACCESS");
> + if (payload & 0x200)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " LLC-REFILL");
> + if (payload & 0x400)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " REMOTE-ACCESS");
> }
> if (ret < 0)
> return ret;
> blen -= ret;
> return buf_len - blen;
> - }
> case ARM_SPE_OP_TYPE:
> switch (idx) {
> - case 0: return snprintf(buf, buf_len, "%s", payload & 0x1 ?
> - "COND-SELECT" : "INSN-OTHER");
> - case 1: {
> - size_t blen = buf_len;
> + case 0:
> + return arm_spe_pkt_snprintf(&err, &buf, &blen,
> + payload & 0x1 ? "COND-SELECT" : "INSN-OTHER");
> + case 1:
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen,
> + payload & 0x1 ? "ST" : "LD");
>
> - if (payload & 0x1)
> - ret = snprintf(buf, buf_len, "ST");
> - else
> - ret = snprintf(buf, buf_len, "LD");
> - buf += ret;
> - blen -= ret;
> if (payload & 0x2) {
> - if (payload & 0x4) {
> - ret = snprintf(buf, buf_len, " AT");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x8) {
> - ret = snprintf(buf, buf_len, " EXCL");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x10) {
> - ret = snprintf(buf, buf_len, " AR");
> - buf += ret;
> - blen -= ret;
> - }
> + if (payload & 0x4)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " AT");
> + if (payload & 0x8)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " EXCL");
> + if (payload & 0x10)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " AR");
> } else if (payload & 0x4) {
> - ret = snprintf(buf, buf_len, " SIMD-FP");
> - buf += ret;
> - blen -= ret;
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " SIMD-FP");
> }
> +
> if (ret < 0)
> return ret;
> blen -= ret;
> return buf_len - blen;
> - }
> - case 2: {
> - size_t blen = buf_len;
>
> - ret = snprintf(buf, buf_len, "B");
> - buf += ret;
> - blen -= ret;
> - if (payload & 0x1) {
> - ret = snprintf(buf, buf_len, " COND");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x2) {
> - ret = snprintf(buf, buf_len, " IND");
> - buf += ret;
> - blen -= ret;
> - }
> + case 2:
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "B");
> +
> + if (payload & 0x1)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " COND");
> + if (payload & 0x2)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " IND");
> +
> if (ret < 0)
> return ret;
> blen -= ret;
> return buf_len - blen;
> - }
> - default: return 0;
> +
> + default:
> + return 0;
> }
> case ARM_SPE_DATA_SOURCE:
> case ARM_SPE_TIMESTAMP:
> - return snprintf(buf, buf_len, "%s %lld", name, payload);
> + return arm_spe_pkt_snprintf(&err, &buf, &blen, "%s %lld", name, payload);
> case ARM_SPE_ADDRESS:
> switch (idx) {
> case 0:
> - case 1: ns = !!(packet->payload & NS_FLAG);
> + case 1:
> + ns = !!(packet->payload & NS_FLAG);
> el = (packet->payload & EL_FLAG) >> 61;
> payload &= ~(0xffULL << 56);
> - return snprintf(buf, buf_len, "%s 0x%llx el%d ns=%d",
> + return arm_spe_pkt_snprintf(&err, &buf, &blen,
> + "%s 0x%llx el%d ns=%d",
> (idx == 1) ? "TGT" : "PC", payload, el, ns);
> - case 2: return snprintf(buf, buf_len, "VA 0x%llx", payload);
> - case 3: ns = !!(packet->payload & NS_FLAG);
> + case 2:
> + return arm_spe_pkt_snprintf(&err, &buf, &blen,
> + "VA 0x%llx", payload);
> + case 3:
> + ns = !!(packet->payload & NS_FLAG);
> payload &= ~(0xffULL << 56);
> - return snprintf(buf, buf_len, "PA 0x%llx ns=%d",
> - payload, ns);
> - default: return 0;
> + return arm_spe_pkt_snprintf(&err, &buf, &blen,
> + "PA 0x%llx ns=%d", payload, ns);
> + default:
> + return 0;
> }
> case ARM_SPE_CONTEXT:
> - return snprintf(buf, buf_len, "%s 0x%lx el%d", name,
> - (unsigned long)payload, idx + 1);
> - case ARM_SPE_COUNTER: {
> - size_t blen = buf_len;
> -
> - ret = snprintf(buf, buf_len, "%s %d ", name,
> - (unsigned short)payload);
> - buf += ret;
> - blen -= ret;
> + return arm_spe_pkt_snprintf(&err, &buf, &blen, "%s 0x%lx el%d",
> + name, (unsigned long)payload, idx + 1);
> + case ARM_SPE_COUNTER:
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "%s %d ", name,
> + (unsigned short)payload);
> switch (idx) {
> - case 0: ret = snprintf(buf, buf_len, "TOT"); break;
> - case 1: ret = snprintf(buf, buf_len, "ISSUE"); break;
> - case 2: ret = snprintf(buf, buf_len, "XLAT"); break;
> - default: ret = 0;
> + case 0:
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "TOT");
> + break;
> + case 1:
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "ISSUE");
> + break;
> + case 2:
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "XLAT");
> + break;
> + default:
> + ret = 0;
> + break;
> }
> if (ret < 0)
> return ret;
> blen -= ret;
> return buf_len - blen;
> - }
> default:
> break;
> }
>
> - return snprintf(buf, buf_len, "%s 0x%llx (%d)",
> - name, payload, packet->index);
> + return arm_spe_pkt_snprintf(&err, &buf, &blen, "%s 0x%llx (%d)",
> + name, payload, packet->index);
> }
> --
> 2.17.1
>

--

- Arnaldo

2020-11-11 15:47:28

by Andre Przywara

[permalink] [raw]
Subject: Re: [PATCH v8 06/22] perf arm-spe: Refactor printing string to buffer

On 11/11/2020 15:35, Arnaldo Carvalho de Melo wrote:

Hi Arnaldo,

thanks for taking a look!

> Em Wed, Nov 11, 2020 at 03:11:33PM +0800, Leo Yan escreveu:
>> When outputs strings to the decoding buffer with function snprintf(),
>> SPE decoder needs to detects if any error returns from snprintf() and if
>> so needs to directly bail out. If snprintf() returns success, it needs
>> to update buffer pointer and reduce the buffer length so can continue to
>> output the next string into the consequent memory space.
>>
>> This complex logics are spreading in the function arm_spe_pkt_desc() so
>> there has many duplicate codes for handling error detecting, increment
>> buffer pointer and decrement buffer size.
>>
>> To avoid the duplicate code, this patch introduces a new helper function
>> arm_spe_pkt_snprintf() which is used to wrap up the complex logics, and
>> it's used by the caller arm_spe_pkt_desc().
>>
>> This patch also moves the variable 'blen' as the function's local
>> variable, this allows to remove the unnecessary braces and improve the
>> readability.
>>
>> Suggested-by: Dave Martin <[email protected]>
>> Signed-off-by: Leo Yan <[email protected]>
>> Reviewed-by: Andre Przywara <[email protected]>
>> ---
>> .../arm-spe-decoder/arm-spe-pkt-decoder.c | 260 +++++++++---------
>> 1 file changed, 126 insertions(+), 134 deletions(-)
>>
>> diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
>> index 04fd7fd7c15f..1970686f7020 100644
>> --- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
>> +++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
>> @@ -9,6 +9,7 @@
>> #include <endian.h>
>> #include <byteswap.h>
>> #include <linux/bitops.h>
>> +#include <stdarg.h>
>>
>> #include "arm-spe-pkt-decoder.h"
>>
>> @@ -258,192 +259,183 @@ int arm_spe_get_packet(const unsigned char *buf, size_t len,
>> return ret;
>> }
>>
>> +static int arm_spe_pkt_snprintf(int *err, char **buf_p, size_t *blen,
>> + const char *fmt, ...)
>> +{
>> + va_list ap;
>> + int ret;
>> +
>> + /* Bail out if any error occurred */
>> + if (err && *err)
>> + return *err;
>> +
>> + va_start(ap, fmt);
>> + ret = vsnprintf(*buf_p, *blen, fmt, ap);
>> + va_end(ap);
>> +
>> + if (ret < 0) {
>> + if (err && !*err)
>> + *err = ret;
>> +
>> + /*
>> + * A return value of (*blen - 1) or more means that the
>> + * output was truncated and the buffer is overrun.
>> + */
>> + } else if (ret >= ((int)*blen - 1)) {
>> + (*buf_p)[*blen - 1] = '\0';
>> +
>> + /*
>> + * Set *err to 'ret' to avoid overflow if tries to
>> + * fill this buffer sequentially.
>> + */
>> + if (err && !*err)
>> + *err = ret;
>> + } else {
>> + *buf_p += ret;
>> + *blen -= ret;
>> + }
>> +
>> + return ret;
>> +}
>> +
>> int arm_spe_pkt_desc(const struct arm_spe_pkt *packet, char *buf,
>> size_t buf_len)
>> {
>> int ret, ns, el, idx = packet->index;
>> unsigned long long payload = packet->payload;
>> const char *name = arm_spe_pkt_name(packet->type);
>> + size_t blen = buf_len;
>> + int err = 0;
>>
>> switch (packet->type) {
>> case ARM_SPE_BAD:
>> case ARM_SPE_PAD:
>> case ARM_SPE_END:
>> - return snprintf(buf, buf_len, "%s", name);
>> - case ARM_SPE_EVENTS: {
>> - size_t blen = buf_len;
>> -
>> - ret = 0;
>> - ret = snprintf(buf, buf_len, "EV");
>> - buf += ret;
>> - blen -= ret;
>> - if (payload & 0x1) {
>> - ret = snprintf(buf, buf_len, " EXCEPTION-GEN");
>> - buf += ret;
>> - blen -= ret;
>> - }
>> - if (payload & 0x2) {
>> - ret = snprintf(buf, buf_len, " RETIRED");
>> - buf += ret;
>> - blen -= ret;
>> - }
>> - if (payload & 0x4) {
>> - ret = snprintf(buf, buf_len, " L1D-ACCESS");
>> - buf += ret;
>> - blen -= ret;
>> - }
>> - if (payload & 0x8) {
>> - ret = snprintf(buf, buf_len, " L1D-REFILL");
>> - buf += ret;
>> - blen -= ret;
>> - }
>> - if (payload & 0x10) {
>> - ret = snprintf(buf, buf_len, " TLB-ACCESS");
>> - buf += ret;
>> - blen -= ret;
>> - }
>> - if (payload & 0x20) {
>> - ret = snprintf(buf, buf_len, " TLB-REFILL");
>> - buf += ret;
>> - blen -= ret;
>> - }
>> - if (payload & 0x40) {
>> - ret = snprintf(buf, buf_len, " NOT-TAKEN");
>> - buf += ret;
>> - blen -= ret;
>> - }
>> - if (payload & 0x80) {
>> - ret = snprintf(buf, buf_len, " MISPRED");
>> - buf += ret;
>> - blen -= ret;
>> - }
>> + return arm_spe_pkt_snprintf(&err, &buf, &blen, "%s", name);
>> + case ARM_SPE_EVENTS:
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "EV");
>> +
>> + if (payload & 0x1)
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " EXCEPTION-GEN");
>
> Isn't this 'ret +=' ? Otherwise if any of these arm_spe_pkt_snprintf()
> calls are made the previous 'ret' value is simply discarded. Can you
> clarify this?

ret is the same as err. If err is negative (from previous calls), we
return that straight away, so it does nothing but propagating the error.
That redundancy gets cleaned up in the next patch.

This patch split is somewhat cumbersome, but was done to simplify review
(originally 06 and 07 were one patch). The combined patch made it hard
to match the individual changes. If you feel unsure about it, or you
feel it looks better, you could also merge both 06/22 and 07/22 into a
single patch.

Hope that helps.

Cheers,
Andre.



>
> I applied all patches before this one.
>
> - Arnaldo
>
>> + if (payload & 0x2)
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " RETIRED");
>> + if (payload & 0x4)
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " L1D-ACCESS");
>> + if (payload & 0x8)
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " L1D-REFILL");
>> + if (payload & 0x10)
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " TLB-ACCESS");
>> + if (payload & 0x20)
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " TLB-REFILL");
>> + if (payload & 0x40)
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " NOT-TAKEN");
>> + if (payload & 0x80)
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " MISPRED");
>> if (idx > 1) {
>> - if (payload & 0x100) {
>> - ret = snprintf(buf, buf_len, " LLC-ACCESS");
>> - buf += ret;
>> - blen -= ret;
>> - }
>> - if (payload & 0x200) {
>> - ret = snprintf(buf, buf_len, " LLC-REFILL");
>> - buf += ret;
>> - blen -= ret;
>> - }
>> - if (payload & 0x400) {
>> - ret = snprintf(buf, buf_len, " REMOTE-ACCESS");
>> - buf += ret;
>> - blen -= ret;
>> - }
>> + if (payload & 0x100)
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " LLC-ACCESS");
>> + if (payload & 0x200)
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " LLC-REFILL");
>> + if (payload & 0x400)
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " REMOTE-ACCESS");
>> }
>> if (ret < 0)
>> return ret;
>> blen -= ret;
>> return buf_len - blen;
>> - }
>> case ARM_SPE_OP_TYPE:
>> switch (idx) {
>> - case 0: return snprintf(buf, buf_len, "%s", payload & 0x1 ?
>> - "COND-SELECT" : "INSN-OTHER");
>> - case 1: {
>> - size_t blen = buf_len;
>> + case 0:
>> + return arm_spe_pkt_snprintf(&err, &buf, &blen,
>> + payload & 0x1 ? "COND-SELECT" : "INSN-OTHER");
>> + case 1:
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen,
>> + payload & 0x1 ? "ST" : "LD");
>>
>> - if (payload & 0x1)
>> - ret = snprintf(buf, buf_len, "ST");
>> - else
>> - ret = snprintf(buf, buf_len, "LD");
>> - buf += ret;
>> - blen -= ret;
>> if (payload & 0x2) {
>> - if (payload & 0x4) {
>> - ret = snprintf(buf, buf_len, " AT");
>> - buf += ret;
>> - blen -= ret;
>> - }
>> - if (payload & 0x8) {
>> - ret = snprintf(buf, buf_len, " EXCL");
>> - buf += ret;
>> - blen -= ret;
>> - }
>> - if (payload & 0x10) {
>> - ret = snprintf(buf, buf_len, " AR");
>> - buf += ret;
>> - blen -= ret;
>> - }
>> + if (payload & 0x4)
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " AT");
>> + if (payload & 0x8)
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " EXCL");
>> + if (payload & 0x10)
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " AR");
>> } else if (payload & 0x4) {
>> - ret = snprintf(buf, buf_len, " SIMD-FP");
>> - buf += ret;
>> - blen -= ret;
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " SIMD-FP");
>> }
>> +
>> if (ret < 0)
>> return ret;
>> blen -= ret;
>> return buf_len - blen;
>> - }
>> - case 2: {
>> - size_t blen = buf_len;
>>
>> - ret = snprintf(buf, buf_len, "B");
>> - buf += ret;
>> - blen -= ret;
>> - if (payload & 0x1) {
>> - ret = snprintf(buf, buf_len, " COND");
>> - buf += ret;
>> - blen -= ret;
>> - }
>> - if (payload & 0x2) {
>> - ret = snprintf(buf, buf_len, " IND");
>> - buf += ret;
>> - blen -= ret;
>> - }
>> + case 2:
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "B");
>> +
>> + if (payload & 0x1)
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " COND");
>> + if (payload & 0x2)
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " IND");
>> +
>> if (ret < 0)
>> return ret;
>> blen -= ret;
>> return buf_len - blen;
>> - }
>> - default: return 0;
>> +
>> + default:
>> + return 0;
>> }
>> case ARM_SPE_DATA_SOURCE:
>> case ARM_SPE_TIMESTAMP:
>> - return snprintf(buf, buf_len, "%s %lld", name, payload);
>> + return arm_spe_pkt_snprintf(&err, &buf, &blen, "%s %lld", name, payload);
>> case ARM_SPE_ADDRESS:
>> switch (idx) {
>> case 0:
>> - case 1: ns = !!(packet->payload & NS_FLAG);
>> + case 1:
>> + ns = !!(packet->payload & NS_FLAG);
>> el = (packet->payload & EL_FLAG) >> 61;
>> payload &= ~(0xffULL << 56);
>> - return snprintf(buf, buf_len, "%s 0x%llx el%d ns=%d",
>> + return arm_spe_pkt_snprintf(&err, &buf, &blen,
>> + "%s 0x%llx el%d ns=%d",
>> (idx == 1) ? "TGT" : "PC", payload, el, ns);
>> - case 2: return snprintf(buf, buf_len, "VA 0x%llx", payload);
>> - case 3: ns = !!(packet->payload & NS_FLAG);
>> + case 2:
>> + return arm_spe_pkt_snprintf(&err, &buf, &blen,
>> + "VA 0x%llx", payload);
>> + case 3:
>> + ns = !!(packet->payload & NS_FLAG);
>> payload &= ~(0xffULL << 56);
>> - return snprintf(buf, buf_len, "PA 0x%llx ns=%d",
>> - payload, ns);
>> - default: return 0;
>> + return arm_spe_pkt_snprintf(&err, &buf, &blen,
>> + "PA 0x%llx ns=%d", payload, ns);
>> + default:
>> + return 0;
>> }
>> case ARM_SPE_CONTEXT:
>> - return snprintf(buf, buf_len, "%s 0x%lx el%d", name,
>> - (unsigned long)payload, idx + 1);
>> - case ARM_SPE_COUNTER: {
>> - size_t blen = buf_len;
>> -
>> - ret = snprintf(buf, buf_len, "%s %d ", name,
>> - (unsigned short)payload);
>> - buf += ret;
>> - blen -= ret;
>> + return arm_spe_pkt_snprintf(&err, &buf, &blen, "%s 0x%lx el%d",
>> + name, (unsigned long)payload, idx + 1);
>> + case ARM_SPE_COUNTER:
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "%s %d ", name,
>> + (unsigned short)payload);
>> switch (idx) {
>> - case 0: ret = snprintf(buf, buf_len, "TOT"); break;
>> - case 1: ret = snprintf(buf, buf_len, "ISSUE"); break;
>> - case 2: ret = snprintf(buf, buf_len, "XLAT"); break;
>> - default: ret = 0;
>> + case 0:
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "TOT");
>> + break;
>> + case 1:
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "ISSUE");
>> + break;
>> + case 2:
>> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "XLAT");
>> + break;
>> + default:
>> + ret = 0;
>> + break;
>> }
>> if (ret < 0)
>> return ret;
>> blen -= ret;
>> return buf_len - blen;
>> - }
>> default:
>> break;
>> }
>>
>> - return snprintf(buf, buf_len, "%s 0x%llx (%d)",
>> - name, payload, packet->index);
>> + return arm_spe_pkt_snprintf(&err, &buf, &blen, "%s 0x%llx (%d)",
>> + name, payload, packet->index);
>> }
>> --
>> 2.17.1
>>
>

2020-11-11 15:55:31

by Dave Martin

[permalink] [raw]
Subject: Re: [PATCH v8 06/22] perf arm-spe: Refactor printing string to buffer

On Wed, Nov 11, 2020 at 07:11:33AM +0000, Leo Yan wrote:
> When outputs strings to the decoding buffer with function snprintf(),
> SPE decoder needs to detects if any error returns from snprintf() and if
> so needs to directly bail out. If snprintf() returns success, it needs
> to update buffer pointer and reduce the buffer length so can continue to
> output the next string into the consequent memory space.
>
> This complex logics are spreading in the function arm_spe_pkt_desc() so
> there has many duplicate codes for handling error detecting, increment
> buffer pointer and decrement buffer size.
>
> To avoid the duplicate code, this patch introduces a new helper function
> arm_spe_pkt_snprintf() which is used to wrap up the complex logics, and
> it's used by the caller arm_spe_pkt_desc().
>
> This patch also moves the variable 'blen' as the function's local
> variable, this allows to remove the unnecessary braces and improve the
> readability.
>
> Suggested-by: Dave Martin <[email protected]>
> Signed-off-by: Leo Yan <[email protected]>
> Reviewed-by: Andre Przywara <[email protected]>

Mostly looks fine to me now, thought there are a few potentionalu
issues -- comments below.

Cheers
---Dave

> ---
> .../arm-spe-decoder/arm-spe-pkt-decoder.c | 260 +++++++++---------
> 1 file changed, 126 insertions(+), 134 deletions(-)
>
> diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
> index 04fd7fd7c15f..1970686f7020 100644
> --- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
> +++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
> @@ -9,6 +9,7 @@
> #include <endian.h>
> #include <byteswap.h>
> #include <linux/bitops.h>
> +#include <stdarg.h>
>
> #include "arm-spe-pkt-decoder.h"
>
> @@ -258,192 +259,183 @@ int arm_spe_get_packet(const unsigned char *buf, size_t len,
> return ret;
> }
>
> +static int arm_spe_pkt_snprintf(int *err, char **buf_p, size_t *blen,
> + const char *fmt, ...)
> +{
> + va_list ap;
> + int ret;
> +
> + /* Bail out if any error occurred */
> + if (err && *err)
> + return *err;
> +
> + va_start(ap, fmt);
> + ret = vsnprintf(*buf_p, *blen, fmt, ap);
> + va_end(ap);
> +
> + if (ret < 0) {
> + if (err && !*err)
> + *err = ret;
> +
> + /*
> + * A return value of (*blen - 1) or more means that the
> + * output was truncated and the buffer is overrun.
> + */

(*blen - 1) chars, + 1 for '\0', is exactly *blen bytes. So ret ==
*blen - 1 is not an error.

> + } else if (ret >= ((int)*blen - 1)) {

So I suggest

if (ret >= *blen)

here.

Nit: If gcc moans about signedness in the comparison, I think it's
preferable to say

if ((size_t)ret >= *blen)

rather than casting *blen to an int. We already know that ret >= 0, and
UINT_MAX always fits in a size_t. On this code path it probably doesn't
matter in practice through, since *blen will be much less than INT_MAX.
vsnprintf() probably doesn't cope gracefully with super-large buffers
anyway, and the ISO C standards don't describe this situation
adequately.

If gcc doesn't warn, just drop the cast.

> + (*buf_p)[*blen - 1] = '\0';
> +
> + /*
> + * Set *err to 'ret' to avoid overflow if tries to
> + * fill this buffer sequentially.
> + */
> + if (err && !*err)
> + *err = ret;
> + } else {
> + *buf_p += ret;
> + *blen -= ret;
> + }
> +
> + return ret;
> +}
> +
> int arm_spe_pkt_desc(const struct arm_spe_pkt *packet, char *buf,
> size_t buf_len)
> {
> int ret, ns, el, idx = packet->index;
> unsigned long long payload = packet->payload;
> const char *name = arm_spe_pkt_name(packet->type);
> + size_t blen = buf_len;
> + int err = 0;
>
> switch (packet->type) {
> case ARM_SPE_BAD:
> case ARM_SPE_PAD:
> case ARM_SPE_END:
> - return snprintf(buf, buf_len, "%s", name);
> - case ARM_SPE_EVENTS: {
> - size_t blen = buf_len;
> -
> - ret = 0;
> - ret = snprintf(buf, buf_len, "EV");
> - buf += ret;
> - blen -= ret;
> - if (payload & 0x1) {
> - ret = snprintf(buf, buf_len, " EXCEPTION-GEN");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x2) {
> - ret = snprintf(buf, buf_len, " RETIRED");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x4) {
> - ret = snprintf(buf, buf_len, " L1D-ACCESS");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x8) {
> - ret = snprintf(buf, buf_len, " L1D-REFILL");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x10) {
> - ret = snprintf(buf, buf_len, " TLB-ACCESS");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x20) {
> - ret = snprintf(buf, buf_len, " TLB-REFILL");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x40) {
> - ret = snprintf(buf, buf_len, " NOT-TAKEN");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x80) {
> - ret = snprintf(buf, buf_len, " MISPRED");
> - buf += ret;
> - blen -= ret;
> - }
> + return arm_spe_pkt_snprintf(&err, &buf, &blen, "%s", name);
> + case ARM_SPE_EVENTS:
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "EV");
> +
> + if (payload & 0x1)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " EXCEPTION-GEN");
> + if (payload & 0x2)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " RETIRED");
> + if (payload & 0x4)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " L1D-ACCESS");
> + if (payload & 0x8)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " L1D-REFILL");
> + if (payload & 0x10)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " TLB-ACCESS");
> + if (payload & 0x20)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " TLB-REFILL");
> + if (payload & 0x40)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " NOT-TAKEN");
> + if (payload & 0x80)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " MISPRED");
> if (idx > 1) {
> - if (payload & 0x100) {
> - ret = snprintf(buf, buf_len, " LLC-ACCESS");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x200) {
> - ret = snprintf(buf, buf_len, " LLC-REFILL");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x400) {
> - ret = snprintf(buf, buf_len, " REMOTE-ACCESS");
> - buf += ret;
> - blen -= ret;
> - }
> + if (payload & 0x100)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " LLC-ACCESS");
> + if (payload & 0x200)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " LLC-REFILL");
> + if (payload & 0x400)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " REMOTE-ACCESS");
> }
> if (ret < 0)
> return ret;
> blen -= ret;

I thought that arm_spe_pkt_snprintf() already did this subtraction?

Also, arm_spe_pkt_snprintf() can return a value >= blen if it overflows
the buffer, so I think this subtraction can underflow away.

If you always do the subtraction at the and of arm_spe_pkt_snprintf()
rather than outside, and if arm_spe_pkt_snprintf() subtracts no more
than blen - 1, then I think this all just works.

> return buf_len - blen;
> - }
> case ARM_SPE_OP_TYPE:
> switch (idx) {
> - case 0: return snprintf(buf, buf_len, "%s", payload & 0x1 ?
> - "COND-SELECT" : "INSN-OTHER");
> - case 1: {
> - size_t blen = buf_len;
> + case 0:
> + return arm_spe_pkt_snprintf(&err, &buf, &blen,
> + payload & 0x1 ? "COND-SELECT" : "INSN-OTHER");
> + case 1:
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen,
> + payload & 0x1 ? "ST" : "LD");
>
> - if (payload & 0x1)
> - ret = snprintf(buf, buf_len, "ST");
> - else
> - ret = snprintf(buf, buf_len, "LD");
> - buf += ret;
> - blen -= ret;
> if (payload & 0x2) {
> - if (payload & 0x4) {
> - ret = snprintf(buf, buf_len, " AT");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x8) {
> - ret = snprintf(buf, buf_len, " EXCL");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x10) {
> - ret = snprintf(buf, buf_len, " AR");
> - buf += ret;
> - blen -= ret;
> - }
> + if (payload & 0x4)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " AT");
> + if (payload & 0x8)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " EXCL");
> + if (payload & 0x10)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " AR");
> } else if (payload & 0x4) {
> - ret = snprintf(buf, buf_len, " SIMD-FP");
> - buf += ret;
> - blen -= ret;
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " SIMD-FP");
> }
> +
> if (ret < 0)
> return ret;
> blen -= ret;

Same here.

> return buf_len - blen;
> - }
> - case 2: {
> - size_t blen = buf_len;
>
> - ret = snprintf(buf, buf_len, "B");
> - buf += ret;
> - blen -= ret;
> - if (payload & 0x1) {
> - ret = snprintf(buf, buf_len, " COND");
> - buf += ret;
> - blen -= ret;
> - }
> - if (payload & 0x2) {
> - ret = snprintf(buf, buf_len, " IND");
> - buf += ret;
> - blen -= ret;
> - }
> + case 2:
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "B");
> +
> + if (payload & 0x1)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " COND");
> + if (payload & 0x2)
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " IND");
> +
> if (ret < 0)
> return ret;
> blen -= ret;
> return buf_len - blen;
> - }
> - default: return 0;
> +
> + default:
> + return 0;
> }
> case ARM_SPE_DATA_SOURCE:
> case ARM_SPE_TIMESTAMP:
> - return snprintf(buf, buf_len, "%s %lld", name, payload);
> + return arm_spe_pkt_snprintf(&err, &buf, &blen, "%s %lld", name, payload);
> case ARM_SPE_ADDRESS:
> switch (idx) {
> case 0:
> - case 1: ns = !!(packet->payload & NS_FLAG);
> + case 1:
> + ns = !!(packet->payload & NS_FLAG);
> el = (packet->payload & EL_FLAG) >> 61;
> payload &= ~(0xffULL << 56);
> - return snprintf(buf, buf_len, "%s 0x%llx el%d ns=%d",
> + return arm_spe_pkt_snprintf(&err, &buf, &blen,
> + "%s 0x%llx el%d ns=%d",
> (idx == 1) ? "TGT" : "PC", payload, el, ns);
> - case 2: return snprintf(buf, buf_len, "VA 0x%llx", payload);
> - case 3: ns = !!(packet->payload & NS_FLAG);
> + case 2:
> + return arm_spe_pkt_snprintf(&err, &buf, &blen,
> + "VA 0x%llx", payload);
> + case 3:
> + ns = !!(packet->payload & NS_FLAG);
> payload &= ~(0xffULL << 56);
> - return snprintf(buf, buf_len, "PA 0x%llx ns=%d",
> - payload, ns);
> - default: return 0;
> + return arm_spe_pkt_snprintf(&err, &buf, &blen,
> + "PA 0x%llx ns=%d", payload, ns);
> + default:
> + return 0;
> }
> case ARM_SPE_CONTEXT:
> - return snprintf(buf, buf_len, "%s 0x%lx el%d", name,
> - (unsigned long)payload, idx + 1);
> - case ARM_SPE_COUNTER: {
> - size_t blen = buf_len;
> -
> - ret = snprintf(buf, buf_len, "%s %d ", name,
> - (unsigned short)payload);
> - buf += ret;
> - blen -= ret;
> + return arm_spe_pkt_snprintf(&err, &buf, &blen, "%s 0x%lx el%d",
> + name, (unsigned long)payload, idx + 1);
> + case ARM_SPE_COUNTER:
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "%s %d ", name,
> + (unsigned short)payload);
> switch (idx) {
> - case 0: ret = snprintf(buf, buf_len, "TOT"); break;
> - case 1: ret = snprintf(buf, buf_len, "ISSUE"); break;
> - case 2: ret = snprintf(buf, buf_len, "XLAT"); break;
> - default: ret = 0;
> + case 0:
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "TOT");
> + break;
> + case 1:
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "ISSUE");
> + break;
> + case 2:
> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "XLAT");
> + break;
> + default:
> + ret = 0;
> + break;
> }
> if (ret < 0)
> return ret;
> blen -= ret;
> return buf_len - blen;

Is it my imagination, or does every case that doesn't end in a break
have the same sequence here?

Can we just break and put

if (err)
return err;

at the end of the big switch?

I'm assuming that the caller treats any nonzero value as an error -- I
seem to remeber you mentioning this previously?

[...]

Cheers
---Dave

2020-11-11 16:00:58

by Dave Martin

[permalink] [raw]
Subject: Re: [PATCH v8 06/22] perf arm-spe: Refactor printing string to buffer

On Wed, Nov 11, 2020 at 03:53:20PM +0000, Dave Martin wrote:
> On Wed, Nov 11, 2020 at 07:11:33AM +0000, Leo Yan wrote:
> > When outputs strings to the decoding buffer with function snprintf(),
> > SPE decoder needs to detects if any error returns from snprintf() and if
> > so needs to directly bail out. If snprintf() returns success, it needs
> > to update buffer pointer and reduce the buffer length so can continue to
> > output the next string into the consequent memory space.
> >
> > This complex logics are spreading in the function arm_spe_pkt_desc() so
> > there has many duplicate codes for handling error detecting, increment
> > buffer pointer and decrement buffer size.
> >
> > To avoid the duplicate code, this patch introduces a new helper function
> > arm_spe_pkt_snprintf() which is used to wrap up the complex logics, and
> > it's used by the caller arm_spe_pkt_desc().
> >
> > This patch also moves the variable 'blen' as the function's local
> > variable, this allows to remove the unnecessary braces and improve the
> > readability.
> >
> > Suggested-by: Dave Martin <[email protected]>
> > Signed-off-by: Leo Yan <[email protected]>
> > Reviewed-by: Andre Przywara <[email protected]>
>
> Mostly looks fine to me now, thought there are a few potentionalu
> issues -- comments below.

Hmm, looks like patch 7 anticipated some of my comments here.

Rather than fixing up patch 6, maybe it would be better to squash these
patches together after all... sorry!

[...]

Cheers
---Dave

2020-11-11 16:13:22

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v8 00/22] perf arm-spe: Refactor decoding & dumping flow

Em Wed, Nov 11, 2020 at 03:11:27PM +0800, Leo Yan escreveu:
> This is patch set v8 for refactoring Arm SPE trace decoding and dumping.
>
> This version addresses Andre's comment to pass parameter '&buf_len' at
> the last call arm_spe_pkt_snprintf() in the function arm_spe_pkt_desc().
>
> This patch set is cleanly applied on the top of perf/core branch
> with commit 644bf4b0f7ac ("perf jevents: Add test for arch std events").
>
> I retested this patch set on Hisilicon D06 platform with commands
> "perf report -D" and "perf script", compared the decoding results
> between with this patch set and without this patch set, "diff" tool
> shows the result as expected.

With the patches I applied I'm getting:

util/arm-spe-decoder/arm-spe-pkt-decoder.c: In function 'arm_spe_pkt_desc':
util/arm-spe-decoder/arm-spe-pkt-decoder.c:410:3: error: left shift count >= width of type [-Werror]
case 1: ns = !!(packet->payload & NS_FLAG);
^
util/arm-spe-decoder/arm-spe-pkt-decoder.c:411:4: error: left shift count >= width of type [-Werror]
el = (packet->payload & EL_FLAG) >> 61;
^
util/arm-spe-decoder/arm-spe-pkt-decoder.c:411:4: error: left shift count >= width of type [-Werror]
util/arm-spe-decoder/arm-spe-pkt-decoder.c:416:3: error: left shift count >= width of type [-Werror]
case 3: ns = !!(packet->payload & NS_FLAG);
^
CC /tmp/build/perf/util/arm-spe-decoder/arm-spe-decoder.o


On:

16 11.70 android-ndk:r12b-arm : FAIL arm-linux-androideabi-gcc (GCC) 4.9.x 20150123 (prerelease)
17 11.32 android-ndk:r15c-arm : FAIL arm-linux-androideabi-gcc (GCC) 4.9.x 20150123 (prerelease)

That were building ok before, builds still under way, perhaps its just
on these old systems...

- Arnaldo

> Changes from v7:
> - Changed to pass '&buf_len' for the last call arm_spe_pkt_snprintf() in
> the patch 07/22 (Andre).
>
> Changes from v6:
> - Removed the redundant comma from the string in the patch 21/22 "perf
> arm_spe: Decode memory tagging properties" (Dave);
> - Refined the return value for arm_spe_pkt_desc(): returns 0 for
> success, otherwise returns non zero for failures; handle error code at
> the end of function arm_spe_pkt_desc(); this is accomplished in the
> new patch 07/22 "perf arm-spe: Consolidate arm_spe_pkt_desc()'s
> return value" (Dave).
>
> Changes from v5:
> - Directly bail out arm_spe_pkt_snprintf() if any error occurred
> (Andre).
>
> Changes from v4:
> - Implemented a cumulative error for arm_spe_pkt_snprintf() and changed
> to condense code for printing strings (Dave);
> - Changed to check payload bits [55:52] for parse kernel address
> (Andre).
>
> Changes from v3:
> - Refined arm_spe_payload_len() and removed macro SPE_HEADER_SZ()
> (Andre);
> - Refined packet header index macros (Andre);
> - Added patch "perf arm_spe: Fixup top byte for data virtual address" to
> fixup the data virtual address for 64KB pages and refined comments for
> the fixup (Andre);
> - Added Andre's review tag (using "b4 am" command);
> - Changed the macros to SPE_PKT_IS_XXX() format to check operation types
> (Andre).
>
>
> Andre Przywara (1):
> perf arm_spe: Decode memory tagging properties
>
> Leo Yan (20):
> perf arm-spe: Include bitops.h for BIT() macro
> perf arm-spe: Fix a typo in comment
> perf arm-spe: Refactor payload size calculation
> perf arm-spe: Refactor arm_spe_get_events()
> perf arm-spe: Fix packet length handling
> perf arm-spe: Refactor printing string to buffer
> perf arm-spe: Consolidate arm_spe_pkt_desc()'s return value
> perf arm-spe: Refactor packet header parsing
> perf arm-spe: Add new function arm_spe_pkt_desc_addr()
> perf arm-spe: Refactor address packet handling
> perf arm_spe: Fixup top byte for data virtual address
> perf arm-spe: Refactor context packet handling
> perf arm-spe: Add new function arm_spe_pkt_desc_counter()
> perf arm-spe: Refactor counter packet handling
> perf arm-spe: Add new function arm_spe_pkt_desc_event()
> perf arm-spe: Refactor event type handling
> perf arm-spe: Remove size condition checking for events
> perf arm-spe: Add new function arm_spe_pkt_desc_op_type()
> perf arm-spe: Refactor operation packet handling
> perf arm-spe: Add more sub classes for operation packet
>
> Wei Li (1):
> perf arm-spe: Add support for ARMv8.3-SPE
>
> .../util/arm-spe-decoder/arm-spe-decoder.c | 59 +-
> .../util/arm-spe-decoder/arm-spe-decoder.h | 17 -
> .../arm-spe-decoder/arm-spe-pkt-decoder.c | 601 ++++++++++--------
> .../arm-spe-decoder/arm-spe-pkt-decoder.h | 122 +++-
> tools/perf/util/arm-spe.c | 2 +-
> 5 files changed, 479 insertions(+), 322 deletions(-)
>
> --
> 2.17.1
>

--

- Arnaldo

2020-11-11 16:19:40

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v8 00/22] perf arm-spe: Refactor decoding & dumping flow

Em Wed, Nov 11, 2020 at 01:10:51PM -0300, Arnaldo Carvalho de Melo escreveu:
> Em Wed, Nov 11, 2020 at 03:11:27PM +0800, Leo Yan escreveu:
> > This is patch set v8 for refactoring Arm SPE trace decoding and dumping.
> >
> > This version addresses Andre's comment to pass parameter '&buf_len' at
> > the last call arm_spe_pkt_snprintf() in the function arm_spe_pkt_desc().
> >
> > This patch set is cleanly applied on the top of perf/core branch
> > with commit 644bf4b0f7ac ("perf jevents: Add test for arch std events").
> >
> > I retested this patch set on Hisilicon D06 platform with commands
> > "perf report -D" and "perf script", compared the decoding results
> > between with this patch set and without this patch set, "diff" tool
> > shows the result as expected.
>
> With the patches I applied I'm getting:
>
> util/arm-spe-decoder/arm-spe-pkt-decoder.c: In function 'arm_spe_pkt_desc':
> util/arm-spe-decoder/arm-spe-pkt-decoder.c:410:3: error: left shift count >= width of type [-Werror]
> case 1: ns = !!(packet->payload & NS_FLAG);
> ^
> util/arm-spe-decoder/arm-spe-pkt-decoder.c:411:4: error: left shift count >= width of type [-Werror]
> el = (packet->payload & EL_FLAG) >> 61;
> ^
> util/arm-spe-decoder/arm-spe-pkt-decoder.c:411:4: error: left shift count >= width of type [-Werror]
> util/arm-spe-decoder/arm-spe-pkt-decoder.c:416:3: error: left shift count >= width of type [-Werror]
> case 3: ns = !!(packet->payload & NS_FLAG);
> ^
> CC /tmp/build/perf/util/arm-spe-decoder/arm-spe-decoder.o
>
>
> On:
>
> 16 11.70 android-ndk:r12b-arm : FAIL arm-linux-androideabi-gcc (GCC) 4.9.x 20150123 (prerelease)
> 17 11.32 android-ndk:r15c-arm : FAIL arm-linux-androideabi-gcc (GCC) 4.9.x 20150123 (prerelease)
>
> That were building ok before, builds still under way, perhaps its just
> on these old systems...

[acme@five perf]$ git bisect good
cc6fa07fb1458cca3741919774eb050976471000 is the first bad commit
commit cc6fa07fb1458cca3741919774eb050976471000
Author: Leo Yan <[email protected]>
Date: Wed Nov 11 15:11:28 2020 +0800

perf arm-spe: Include bitops.h for BIT() macro

Include header linux/bitops.h, directly use its BIT() macro and remove
the self defined macros.

Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Andre Przywara <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>

tools/perf/util/arm-spe-decoder/arm-spe-decoder.c | 5 +----
tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c | 3 +--
2 files changed, 2 insertions(+), 6 deletions(-)
[acme@five perf]$

2020-11-11 16:25:23

by Andre Przywara

[permalink] [raw]
Subject: Re: [PATCH v8 00/22] perf arm-spe: Refactor decoding & dumping flow

On 11/11/2020 16:15, Arnaldo Carvalho de Melo wrote:
> Em Wed, Nov 11, 2020 at 01:10:51PM -0300, Arnaldo Carvalho de Melo escreveu:
>> Em Wed, Nov 11, 2020 at 03:11:27PM +0800, Leo Yan escreveu:
>>> This is patch set v8 for refactoring Arm SPE trace decoding and dumping.
>>>
>>> This version addresses Andre's comment to pass parameter '&buf_len' at
>>> the last call arm_spe_pkt_snprintf() in the function arm_spe_pkt_desc().
>>>
>>> This patch set is cleanly applied on the top of perf/core branch
>>> with commit 644bf4b0f7ac ("perf jevents: Add test for arch std events").
>>>
>>> I retested this patch set on Hisilicon D06 platform with commands
>>> "perf report -D" and "perf script", compared the decoding results
>>> between with this patch set and without this patch set, "diff" tool
>>> shows the result as expected.
>>
>> With the patches I applied I'm getting:
>>
>> util/arm-spe-decoder/arm-spe-pkt-decoder.c: In function 'arm_spe_pkt_desc':
>> util/arm-spe-decoder/arm-spe-pkt-decoder.c:410:3: error: left shift count >= width of type [-Werror]
>> case 1: ns = !!(packet->payload & NS_FLAG);
>> ^
>> util/arm-spe-decoder/arm-spe-pkt-decoder.c:411:4: error: left shift count >= width of type [-Werror]
>> el = (packet->payload & EL_FLAG) >> 61;
>> ^
>> util/arm-spe-decoder/arm-spe-pkt-decoder.c:411:4: error: left shift count >= width of type [-Werror]
>> util/arm-spe-decoder/arm-spe-pkt-decoder.c:416:3: error: left shift count >= width of type [-Werror]
>> case 3: ns = !!(packet->payload & NS_FLAG);
>> ^
>> CC /tmp/build/perf/util/arm-spe-decoder/arm-spe-decoder.o
>>
>>
>> On:
>>
>> 16 11.70 android-ndk:r12b-arm : FAIL arm-linux-androideabi-gcc (GCC) 4.9.x 20150123 (prerelease)
>> 17 11.32 android-ndk:r15c-arm : FAIL arm-linux-androideabi-gcc (GCC) 4.9.x 20150123 (prerelease)
>>
>> That were building ok before, builds still under way, perhaps its just
>> on these old systems...
>
> [acme@five perf]$ git bisect good
> cc6fa07fb1458cca3741919774eb050976471000 is the first bad commit
> commit cc6fa07fb1458cca3741919774eb050976471000
> Author: Leo Yan <[email protected]>
> Date: Wed Nov 11 15:11:28 2020 +0800
>
> perf arm-spe: Include bitops.h for BIT() macro
>
> Include header linux/bitops.h, directly use its BIT() macro and remove
> the self defined macros.
>
> Signed-off-by: Leo Yan <[email protected]>
> Reviewed-by: Andre Przywara <[email protected]>
> Link: https://lore.kernel.org/r/[email protected]
> Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
>
> tools/perf/util/arm-spe-decoder/arm-spe-decoder.c | 5 +----
> tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c | 3 +--
> 2 files changed, 2 insertions(+), 6 deletions(-)


Ah, thanks! I think I mentioned the missing usage of BIT_ULL() in an
earlier review, and thought this was fixed. Possibly this gets fixed in
a later patch in this series, and is a temporary regression?

How do you want to handle this? Shall Leo resend, amending this patch
(and merging 06 and 07 on the way ;-)?

Cheers,
Andre

2020-11-11 17:41:26

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v8 06/22] perf arm-spe: Refactor printing string to buffer

Em Wed, Nov 11, 2020 at 03:45:23PM +0000, Andr? Przywara escreveu:
> On 11/11/2020 15:35, Arnaldo Carvalho de Melo wrote:
>
> Hi Arnaldo,
>
> thanks for taking a look!
>
> > Em Wed, Nov 11, 2020 at 03:11:33PM +0800, Leo Yan escreveu:
> >> When outputs strings to the decoding buffer with function snprintf(),
> >> SPE decoder needs to detects if any error returns from snprintf() and if
> >> so needs to directly bail out. If snprintf() returns success, it needs
> >> to update buffer pointer and reduce the buffer length so can continue to
> >> output the next string into the consequent memory space.
> >>
> >> This complex logics are spreading in the function arm_spe_pkt_desc() so
> >> there has many duplicate codes for handling error detecting, increment
> >> buffer pointer and decrement buffer size.
> >>
> >> To avoid the duplicate code, this patch introduces a new helper function
> >> arm_spe_pkt_snprintf() which is used to wrap up the complex logics, and
> >> it's used by the caller arm_spe_pkt_desc().
> >>
> >> This patch also moves the variable 'blen' as the function's local
> >> variable, this allows to remove the unnecessary braces and improve the
> >> readability.
> >>
> >> Suggested-by: Dave Martin <[email protected]>
> >> Signed-off-by: Leo Yan <[email protected]>
> >> Reviewed-by: Andre Przywara <[email protected]>
> >> ---
> >> .../arm-spe-decoder/arm-spe-pkt-decoder.c | 260 +++++++++---------
> >> 1 file changed, 126 insertions(+), 134 deletions(-)
> >>
> >> diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
> >> index 04fd7fd7c15f..1970686f7020 100644
> >> --- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
> >> +++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
> >> @@ -9,6 +9,7 @@
> >> #include <endian.h>
> >> #include <byteswap.h>
> >> #include <linux/bitops.h>
> >> +#include <stdarg.h>
> >>
> >> #include "arm-spe-pkt-decoder.h"
> >>
> >> @@ -258,192 +259,183 @@ int arm_spe_get_packet(const unsigned char *buf, size_t len,
> >> return ret;
> >> }
> >>
> >> +static int arm_spe_pkt_snprintf(int *err, char **buf_p, size_t *blen,
> >> + const char *fmt, ...)
> >> +{
> >> + va_list ap;
> >> + int ret;
> >> +
> >> + /* Bail out if any error occurred */
> >> + if (err && *err)
> >> + return *err;
> >> +
> >> + va_start(ap, fmt);
> >> + ret = vsnprintf(*buf_p, *blen, fmt, ap);
> >> + va_end(ap);
> >> +
> >> + if (ret < 0) {
> >> + if (err && !*err)
> >> + *err = ret;
> >> +
> >> + /*
> >> + * A return value of (*blen - 1) or more means that the
> >> + * output was truncated and the buffer is overrun.
> >> + */
> >> + } else if (ret >= ((int)*blen - 1)) {
> >> + (*buf_p)[*blen - 1] = '\0';
> >> +
> >> + /*
> >> + * Set *err to 'ret' to avoid overflow if tries to
> >> + * fill this buffer sequentially.
> >> + */
> >> + if (err && !*err)
> >> + *err = ret;
> >> + } else {
> >> + *buf_p += ret;
> >> + *blen -= ret;
> >> + }
> >> +
> >> + return ret;
> >> +}
> >> +
> >> int arm_spe_pkt_desc(const struct arm_spe_pkt *packet, char *buf,
> >> size_t buf_len)
> >> {
> >> int ret, ns, el, idx = packet->index;
> >> unsigned long long payload = packet->payload;
> >> const char *name = arm_spe_pkt_name(packet->type);
> >> + size_t blen = buf_len;
> >> + int err = 0;
> >>
> >> switch (packet->type) {
> >> case ARM_SPE_BAD:
> >> case ARM_SPE_PAD:
> >> case ARM_SPE_END:
> >> - return snprintf(buf, buf_len, "%s", name);
> >> - case ARM_SPE_EVENTS: {
> >> - size_t blen = buf_len;
> >> -
> >> - ret = 0;
> >> - ret = snprintf(buf, buf_len, "EV");
> >> - buf += ret;
> >> - blen -= ret;
> >> - if (payload & 0x1) {
> >> - ret = snprintf(buf, buf_len, " EXCEPTION-GEN");
> >> - buf += ret;
> >> - blen -= ret;
> >> - }
> >> - if (payload & 0x2) {
> >> - ret = snprintf(buf, buf_len, " RETIRED");
> >> - buf += ret;
> >> - blen -= ret;
> >> - }
> >> - if (payload & 0x4) {
> >> - ret = snprintf(buf, buf_len, " L1D-ACCESS");
> >> - buf += ret;
> >> - blen -= ret;
> >> - }
> >> - if (payload & 0x8) {
> >> - ret = snprintf(buf, buf_len, " L1D-REFILL");
> >> - buf += ret;
> >> - blen -= ret;
> >> - }
> >> - if (payload & 0x10) {
> >> - ret = snprintf(buf, buf_len, " TLB-ACCESS");
> >> - buf += ret;
> >> - blen -= ret;
> >> - }
> >> - if (payload & 0x20) {
> >> - ret = snprintf(buf, buf_len, " TLB-REFILL");
> >> - buf += ret;
> >> - blen -= ret;
> >> - }
> >> - if (payload & 0x40) {
> >> - ret = snprintf(buf, buf_len, " NOT-TAKEN");
> >> - buf += ret;
> >> - blen -= ret;
> >> - }
> >> - if (payload & 0x80) {
> >> - ret = snprintf(buf, buf_len, " MISPRED");
> >> - buf += ret;
> >> - blen -= ret;
> >> - }
> >> + return arm_spe_pkt_snprintf(&err, &buf, &blen, "%s", name);
> >> + case ARM_SPE_EVENTS:
> >> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "EV");
> >> +
> >> + if (payload & 0x1)
> >> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " EXCEPTION-GEN");
> >
> > Isn't this 'ret +=' ? Otherwise if any of these arm_spe_pkt_snprintf()
> > calls are made the previous 'ret' value is simply discarded. Can you
> > clarify this?
>
> ret is the same as err. If err is negative (from previous calls), we
> return that straight away, so it does nothing but propagating the error.

Usually the return of a snprintf is used to account for buffer space, ok
I'll have to read it, which I shouldn't as snprintf has a well defined
meaning...

Ok, now that I look at it, I realize it is not a snprintf() routine, but
something with different semantics, that will look at a pointer to an
integer and then do nothing if it comes with some error, etc, confusing
:-/

> That redundancy gets cleaned up in the next patch.

I'll fixup the bitops part and try to continue.

- Arnaldo

> This patch split is somewhat cumbersome, but was done to simplify review
> (originally 06 and 07 were one patch). The combined patch made it hard
> to match the individual changes. If you feel unsure about it, or you
> feel it looks better, you could also merge both 06/22 and 07/22 into a
> single patch.
>
> Hope that helps.

2020-11-11 17:42:26

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v8 06/22] perf arm-spe: Refactor printing string to buffer

Em Wed, Nov 11, 2020 at 03:58:27PM +0000, Dave Martin escreveu:
> On Wed, Nov 11, 2020 at 03:53:20PM +0000, Dave Martin wrote:
> > On Wed, Nov 11, 2020 at 07:11:33AM +0000, Leo Yan wrote:
> > > When outputs strings to the decoding buffer with function snprintf(),
> > > SPE decoder needs to detects if any error returns from snprintf() and if
> > > so needs to directly bail out. If snprintf() returns success, it needs
> > > to update buffer pointer and reduce the buffer length so can continue to
> > > output the next string into the consequent memory space.
> > >
> > > This complex logics are spreading in the function arm_spe_pkt_desc() so
> > > there has many duplicate codes for handling error detecting, increment
> > > buffer pointer and decrement buffer size.
> > >
> > > To avoid the duplicate code, this patch introduces a new helper function
> > > arm_spe_pkt_snprintf() which is used to wrap up the complex logics, and
> > > it's used by the caller arm_spe_pkt_desc().
> > >
> > > This patch also moves the variable 'blen' as the function's local
> > > variable, this allows to remove the unnecessary braces and improve the
> > > readability.
> > >
> > > Suggested-by: Dave Martin <[email protected]>
> > > Signed-off-by: Leo Yan <[email protected]>
> > > Reviewed-by: Andre Przywara <[email protected]>
> >
> > Mostly looks fine to me now, thought there are a few potentionalu
> > issues -- comments below.
>
> Hmm, looks like patch 7 anticipated some of my comments here.
>
> Rather than fixing up patch 6, maybe it would be better to squash these
> patches together after all... sorry!

I'll take a look and probably do that, as it is what Andre suggests.

- Arnaldo

2020-11-11 17:48:38

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v8 00/22] perf arm-spe: Refactor decoding & dumping flow

Em Wed, Nov 11, 2020 at 04:20:26PM +0000, Andr? Przywara escreveu:
> On 11/11/2020 16:15, Arnaldo Carvalho de Melo wrote:
> > Em Wed, Nov 11, 2020 at 01:10:51PM -0300, Arnaldo Carvalho de Melo escreveu:
> >> Em Wed, Nov 11, 2020 at 03:11:27PM +0800, Leo Yan escreveu:
> >>> This is patch set v8 for refactoring Arm SPE trace decoding and dumping.
> >>>
> >>> This version addresses Andre's comment to pass parameter '&buf_len' at
> >>> the last call arm_spe_pkt_snprintf() in the function arm_spe_pkt_desc().
> >>>
> >>> This patch set is cleanly applied on the top of perf/core branch
> >>> with commit 644bf4b0f7ac ("perf jevents: Add test for arch std events").
> >>>
> >>> I retested this patch set on Hisilicon D06 platform with commands
> >>> "perf report -D" and "perf script", compared the decoding results
> >>> between with this patch set and without this patch set, "diff" tool
> >>> shows the result as expected.
> >>
> >> With the patches I applied I'm getting:
> >>
> >> util/arm-spe-decoder/arm-spe-pkt-decoder.c: In function 'arm_spe_pkt_desc':
> >> util/arm-spe-decoder/arm-spe-pkt-decoder.c:410:3: error: left shift count >= width of type [-Werror]
> >> case 1: ns = !!(packet->payload & NS_FLAG);
> >> ^
> >> util/arm-spe-decoder/arm-spe-pkt-decoder.c:411:4: error: left shift count >= width of type [-Werror]
> >> el = (packet->payload & EL_FLAG) >> 61;
> >> ^
> >> util/arm-spe-decoder/arm-spe-pkt-decoder.c:411:4: error: left shift count >= width of type [-Werror]
> >> util/arm-spe-decoder/arm-spe-pkt-decoder.c:416:3: error: left shift count >= width of type [-Werror]
> >> case 3: ns = !!(packet->payload & NS_FLAG);
> >> ^
> >> CC /tmp/build/perf/util/arm-spe-decoder/arm-spe-decoder.o
> >>
> >>
> >> On:
> >>
> >> 16 11.70 android-ndk:r12b-arm : FAIL arm-linux-androideabi-gcc (GCC) 4.9.x 20150123 (prerelease)
> >> 17 11.32 android-ndk:r15c-arm : FAIL arm-linux-androideabi-gcc (GCC) 4.9.x 20150123 (prerelease)
> >>
> >> That were building ok before, builds still under way, perhaps its just
> >> on these old systems...
> >
> > [acme@five perf]$ git bisect good
> > cc6fa07fb1458cca3741919774eb050976471000 is the first bad commit
> > commit cc6fa07fb1458cca3741919774eb050976471000
> > Author: Leo Yan <[email protected]>
> > Date: Wed Nov 11 15:11:28 2020 +0800
> >
> > perf arm-spe: Include bitops.h for BIT() macro
> >
> > Include header linux/bitops.h, directly use its BIT() macro and remove
> > the self defined macros.
> >
> > Signed-off-by: Leo Yan <[email protected]>
> > Reviewed-by: Andre Przywara <[email protected]>
> > Link: https://lore.kernel.org/r/[email protected]
> > Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
> >
> > tools/perf/util/arm-spe-decoder/arm-spe-decoder.c | 5 +----
> > tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c | 3 +--
> > 2 files changed, 2 insertions(+), 6 deletions(-)
>
>
> Ah, thanks! I think I mentioned the missing usage of BIT_ULL() in an
> earlier review, and thought this was fixed. Possibly this gets fixed in
> a later patch in this series, and is a temporary regression?

you mean this on that patch that ditches the local BIT() macro, right?

[acme@five perf]$ vim tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
[acme@five perf]$ git diff
diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index 46ddb53a645714bb..5f65a3a70c577207 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -12,8 +12,8 @@

#include "arm-spe-pkt-decoder.h"

-#define NS_FLAG BIT(63)
-#define EL_FLAG (BIT(62) | BIT(61))
+#define NS_FLAG BIT_ULL(63)
+#define EL_FLAG (BIT_ULL(62) | BIT_ULL(61))

#define SPE_HEADER0_PAD 0x0
#define SPE_HEADER0_END 0x1
[acme@five perf]$

> How do you want to handle this? Shall Leo resend, amending this patch
> (and merging 06 and 07 on the way ;-)?

2020-11-11 17:55:27

by Andre Przywara

[permalink] [raw]
Subject: Re: [PATCH v8 00/22] perf arm-spe: Refactor decoding & dumping flow

On 11/11/2020 17:44, Arnaldo Carvalho de Melo wrote:
> Em Wed, Nov 11, 2020 at 04:20:26PM +0000, Andr� Przywara escreveu:
>> On 11/11/2020 16:15, Arnaldo Carvalho de Melo wrote:
>>> Em Wed, Nov 11, 2020 at 01:10:51PM -0300, Arnaldo Carvalho de Melo escreveu:
>>>> Em Wed, Nov 11, 2020 at 03:11:27PM +0800, Leo Yan escreveu:
>>>>> This is patch set v8 for refactoring Arm SPE trace decoding and dumping.
>>>>>
>>>>> This version addresses Andre's comment to pass parameter '&buf_len' at
>>>>> the last call arm_spe_pkt_snprintf() in the function arm_spe_pkt_desc().
>>>>>
>>>>> This patch set is cleanly applied on the top of perf/core branch
>>>>> with commit 644bf4b0f7ac ("perf jevents: Add test for arch std events").
>>>>>
>>>>> I retested this patch set on Hisilicon D06 platform with commands
>>>>> "perf report -D" and "perf script", compared the decoding results
>>>>> between with this patch set and without this patch set, "diff" tool
>>>>> shows the result as expected.
>>>>
>>>> With the patches I applied I'm getting:
>>>>
>>>> util/arm-spe-decoder/arm-spe-pkt-decoder.c: In function 'arm_spe_pkt_desc':
>>>> util/arm-spe-decoder/arm-spe-pkt-decoder.c:410:3: error: left shift count >= width of type [-Werror]
>>>> case 1: ns = !!(packet->payload & NS_FLAG);
>>>> ^
>>>> util/arm-spe-decoder/arm-spe-pkt-decoder.c:411:4: error: left shift count >= width of type [-Werror]
>>>> el = (packet->payload & EL_FLAG) >> 61;
>>>> ^
>>>> util/arm-spe-decoder/arm-spe-pkt-decoder.c:411:4: error: left shift count >= width of type [-Werror]
>>>> util/arm-spe-decoder/arm-spe-pkt-decoder.c:416:3: error: left shift count >= width of type [-Werror]
>>>> case 3: ns = !!(packet->payload & NS_FLAG);
>>>> ^
>>>> CC /tmp/build/perf/util/arm-spe-decoder/arm-spe-decoder.o
>>>>
>>>>
>>>> On:
>>>>
>>>> 16 11.70 android-ndk:r12b-arm : FAIL arm-linux-androideabi-gcc (GCC) 4.9.x 20150123 (prerelease)
>>>> 17 11.32 android-ndk:r15c-arm : FAIL arm-linux-androideabi-gcc (GCC) 4.9.x 20150123 (prerelease)
>>>>
>>>> That were building ok before, builds still under way, perhaps its just
>>>> on these old systems...
>>>
>>> [acme@five perf]$ git bisect good
>>> cc6fa07fb1458cca3741919774eb050976471000 is the first bad commit
>>> commit cc6fa07fb1458cca3741919774eb050976471000
>>> Author: Leo Yan <[email protected]>
>>> Date: Wed Nov 11 15:11:28 2020 +0800
>>>
>>> perf arm-spe: Include bitops.h for BIT() macro
>>>
>>> Include header linux/bitops.h, directly use its BIT() macro and remove
>>> the self defined macros.
>>>
>>> Signed-off-by: Leo Yan <[email protected]>
>>> Reviewed-by: Andre Przywara <[email protected]>
>>> Link: https://lore.kernel.org/r/[email protected]
>>> Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
>>>
>>> tools/perf/util/arm-spe-decoder/arm-spe-decoder.c | 5 +----
>>> tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c | 3 +--
>>> 2 files changed, 2 insertions(+), 6 deletions(-)
>>
>>
>> Ah, thanks! I think I mentioned the missing usage of BIT_ULL() in an
>> earlier review, and thought this was fixed. Possibly this gets fixed in
>> a later patch in this series, and is a temporary regression?
>
> you mean this on that patch that ditches the local BIT() macro, right?
>
> [acme@five perf]$ vim tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
> [acme@five perf]$ git diff
> diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
> index 46ddb53a645714bb..5f65a3a70c577207 100644
> --- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
> +++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
> @@ -12,8 +12,8 @@
>
> #include "arm-spe-pkt-decoder.h"
>
> -#define NS_FLAG BIT(63)
> -#define EL_FLAG (BIT(62) | BIT(61))
> +#define NS_FLAG BIT_ULL(63)
> +#define EL_FLAG (BIT_ULL(62) | BIT_ULL(61))
>
> #define SPE_HEADER0_PAD 0x0
> #define SPE_HEADER0_END 0x1

Yes, that basically happens in patch 10/22, so this will then
(trivially) clash when you rebase.

Thanks!
Andre.


> [acme@five perf]$
>
>> How do you want to handle this? Shall Leo resend, amending this patch
>> (and merging 06 and 07 on the way ;-)?

2020-11-11 18:00:38

by Dave Martin

[permalink] [raw]
Subject: Re: [PATCH v8 06/22] perf arm-spe: Refactor printing string to buffer


On Wed, Nov 11, 2020 at 05:39:22PM +0000, Arnaldo Carvalho de Melo wrote:
> Em Wed, Nov 11, 2020 at 03:45:23PM +0000, Andr� Przywara escreveu:
> > On 11/11/2020 15:35, Arnaldo Carvalho de Melo wrote:
> >
> > Hi Arnaldo,
> >
> > thanks for taking a look!
> >
> > > Em Wed, Nov 11, 2020 at 03:11:33PM +0800, Leo Yan escreveu:
> > >> When outputs strings to the decoding buffer with function snprintf(),
> > >> SPE decoder needs to detects if any error returns from snprintf() and if
> > >> so needs to directly bail out. If snprintf() returns success, it needs
> > >> to update buffer pointer and reduce the buffer length so can continue to
> > >> output the next string into the consequent memory space.
> > >>
> > >> This complex logics are spreading in the function arm_spe_pkt_desc() so
> > >> there has many duplicate codes for handling error detecting, increment
> > >> buffer pointer and decrement buffer size.
> > >>
> > >> To avoid the duplicate code, this patch introduces a new helper function
> > >> arm_spe_pkt_snprintf() which is used to wrap up the complex logics, and
> > >> it's used by the caller arm_spe_pkt_desc().
> > >>
> > >> This patch also moves the variable 'blen' as the function's local
> > >> variable, this allows to remove the unnecessary braces and improve the
> > >> readability.
> > >>
> > >> Suggested-by: Dave Martin <[email protected]>
> > >> Signed-off-by: Leo Yan <[email protected]>
> > >> Reviewed-by: Andre Przywara <[email protected]>
> > >> ---
> > >> .../arm-spe-decoder/arm-spe-pkt-decoder.c | 260 +++++++++---------
> > >> 1 file changed, 126 insertions(+), 134 deletions(-)
> > >>
> > >> diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
> > >> index 04fd7fd7c15f..1970686f7020 100644
> > >> --- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
> > >> +++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
> > >> @@ -9,6 +9,7 @@
> > >> #include <endian.h>
> > >> #include <byteswap.h>
> > >> #include <linux/bitops.h>
> > >> +#include <stdarg.h>
> > >>
> > >> #include "arm-spe-pkt-decoder.h"
> > >>
> > >> @@ -258,192 +259,183 @@ int arm_spe_get_packet(const unsigned char *buf, size_t len,
> > >> return ret;
> > >> }
> > >>
> > >> +static int arm_spe_pkt_snprintf(int *err, char **buf_p, size_t *blen,
> > >> + const char *fmt, ...)
> > >> +{
> > >> + va_list ap;
> > >> + int ret;
> > >> +
> > >> + /* Bail out if any error occurred */
> > >> + if (err && *err)
> > >> + return *err;
> > >> +
> > >> + va_start(ap, fmt);
> > >> + ret = vsnprintf(*buf_p, *blen, fmt, ap);
> > >> + va_end(ap);
> > >> +
> > >> + if (ret < 0) {
> > >> + if (err && !*err)
> > >> + *err = ret;
> > >> +
> > >> + /*
> > >> + * A return value of (*blen - 1) or more means that the
> > >> + * output was truncated and the buffer is overrun.
> > >> + */
> > >> + } else if (ret >= ((int)*blen - 1)) {
> > >> + (*buf_p)[*blen - 1] = '\0';
> > >> +
> > >> + /*
> > >> + * Set *err to 'ret' to avoid overflow if tries to
> > >> + * fill this buffer sequentially.
> > >> + */
> > >> + if (err && !*err)
> > >> + *err = ret;
> > >> + } else {
> > >> + *buf_p += ret;
> > >> + *blen -= ret;
> > >> + }
> > >> +
> > >> + return ret;
> > >> +}
> > >> +
> > >> int arm_spe_pkt_desc(const struct arm_spe_pkt *packet, char *buf,
> > >> size_t buf_len)
> > >> {
> > >> int ret, ns, el, idx = packet->index;
> > >> unsigned long long payload = packet->payload;
> > >> const char *name = arm_spe_pkt_name(packet->type);
> > >> + size_t blen = buf_len;
> > >> + int err = 0;
> > >>
> > >> switch (packet->type) {
> > >> case ARM_SPE_BAD:
> > >> case ARM_SPE_PAD:
> > >> case ARM_SPE_END:
> > >> - return snprintf(buf, buf_len, "%s", name);
> > >> - case ARM_SPE_EVENTS: {
> > >> - size_t blen = buf_len;
> > >> -
> > >> - ret = 0;
> > >> - ret = snprintf(buf, buf_len, "EV");
> > >> - buf += ret;
> > >> - blen -= ret;
> > >> - if (payload & 0x1) {
> > >> - ret = snprintf(buf, buf_len, " EXCEPTION-GEN");
> > >> - buf += ret;
> > >> - blen -= ret;
> > >> - }
> > >> - if (payload & 0x2) {
> > >> - ret = snprintf(buf, buf_len, " RETIRED");
> > >> - buf += ret;
> > >> - blen -= ret;
> > >> - }
> > >> - if (payload & 0x4) {
> > >> - ret = snprintf(buf, buf_len, " L1D-ACCESS");
> > >> - buf += ret;
> > >> - blen -= ret;
> > >> - }
> > >> - if (payload & 0x8) {
> > >> - ret = snprintf(buf, buf_len, " L1D-REFILL");
> > >> - buf += ret;
> > >> - blen -= ret;
> > >> - }
> > >> - if (payload & 0x10) {
> > >> - ret = snprintf(buf, buf_len, " TLB-ACCESS");
> > >> - buf += ret;
> > >> - blen -= ret;
> > >> - }
> > >> - if (payload & 0x20) {
> > >> - ret = snprintf(buf, buf_len, " TLB-REFILL");
> > >> - buf += ret;
> > >> - blen -= ret;
> > >> - }
> > >> - if (payload & 0x40) {
> > >> - ret = snprintf(buf, buf_len, " NOT-TAKEN");
> > >> - buf += ret;
> > >> - blen -= ret;
> > >> - }
> > >> - if (payload & 0x80) {
> > >> - ret = snprintf(buf, buf_len, " MISPRED");
> > >> - buf += ret;
> > >> - blen -= ret;
> > >> - }
> > >> + return arm_spe_pkt_snprintf(&err, &buf, &blen, "%s", name);
> > >> + case ARM_SPE_EVENTS:
> > >> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, "EV");
> > >> +
> > >> + if (payload & 0x1)
> > >> + ret = arm_spe_pkt_snprintf(&err, &buf, &blen, " EXCEPTION-GEN");
> > >
> > > Isn't this 'ret +=' ? Otherwise if any of these arm_spe_pkt_snprintf()
> > > calls are made the previous 'ret' value is simply discarded. Can you
> > > clarify this?
> >
> > ret is the same as err. If err is negative (from previous calls), we
> > return that straight away, so it does nothing but propagating the error.
>
> Usually the return of a snprintf is used to account for buffer space, ok
> I'll have to read it, which I shouldn't as snprintf has a well defined
> meaning...
>
> Ok, now that I look at it, I realize it is not a snprintf() routine, but
> something with different semantics, that will look at a pointer to an
> integer and then do nothing if it comes with some error, etc, confusing
> :-/

Would you be happier if the function were renamed?

Originally we were aiming for snprintf() semantics, but this still
spawns a lot of boilerplate code and encourages mistakes in the local
caller here -- hence the current sticky error approach.

So maybe the name should now be less "snprintf"-like.

Cheers
---Dave

2020-11-11 18:05:02

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v8 06/22] perf arm-spe: Refactor printing string to buffer

Em Wed, Nov 11, 2020 at 05:58:27PM +0000, Dave Martin escreveu:
>
> On Wed, Nov 11, 2020 at 05:39:22PM +0000, Arnaldo Carvalho de Melo wrote:
> > Em Wed, Nov 11, 2020 at 03:45:23PM +0000, Andr� Przywara escreveu:
> > > On 11/11/2020 15:35, Arnaldo Carvalho de Melo wrote:
> > > > Isn't this 'ret +=' ? Otherwise if any of these arm_spe_pkt_snprintf()
> > > > calls are made the previous 'ret' value is simply discarded. Can you
> > > > clarify this?

> > > ret is the same as err. If err is negative (from previous calls), we
> > > return that straight away, so it does nothing but propagating the error.

> > Usually the return of a snprintf is used to account for buffer space, ok
> > I'll have to read it, which I shouldn't as snprintf has a well defined
> > meaning...

> > Ok, now that I look at it, I realize it is not a snprintf() routine, but
> > something with different semantics, that will look at a pointer to an
> > integer and then do nothing if it comes with some error, etc, confusing
> > :-/

> Would you be happier if the function were renamed?

> Originally we were aiming for snprintf() semantics, but this still
> spawns a lot of boilerplate code and encourages mistakes in the local
> caller here -- hence the current sticky error approach.

> So maybe the name should now be less "snprintf"-like.

Please, its important to stick to semantics for such well known type of
routines, helps reviewing, etc.

I'll keep the series up to that point and will run my build tests, then
push it publicly to acme/perf/core and you can go from there, ok?

I've changed the BIT() to BIT_ULL() as Andre suggested and I'm testing
it again.

- Arnaldo

2020-11-11 18:05:21

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v8 06/22] perf arm-spe: Refactor printing string to buffer

Em Wed, Nov 11, 2020 at 03:01:27PM -0300, Arnaldo Carvalho de Melo escreveu:
> Em Wed, Nov 11, 2020 at 05:58:27PM +0000, Dave Martin escreveu:
> >
> > On Wed, Nov 11, 2020 at 05:39:22PM +0000, Arnaldo Carvalho de Melo wrote:
> > > Em Wed, Nov 11, 2020 at 03:45:23PM +0000, Andr� Przywara escreveu:
> > > > On 11/11/2020 15:35, Arnaldo Carvalho de Melo wrote:
> > > > > Isn't this 'ret +=' ? Otherwise if any of these arm_spe_pkt_snprintf()
> > > > > calls are made the previous 'ret' value is simply discarded. Can you
> > > > > clarify this?
>
> > > > ret is the same as err. If err is negative (from previous calls), we
> > > > return that straight away, so it does nothing but propagating the error.
>
> > > Usually the return of a snprintf is used to account for buffer space, ok
> > > I'll have to read it, which I shouldn't as snprintf has a well defined
> > > meaning...
>
> > > Ok, now that I look at it, I realize it is not a snprintf() routine, but
> > > something with different semantics, that will look at a pointer to an
> > > integer and then do nothing if it comes with some error, etc, confusing
> > > :-/
>
> > Would you be happier if the function were renamed?
>
> > Originally we were aiming for snprintf() semantics, but this still
> > spawns a lot of boilerplate code and encourages mistakes in the local
> > caller here -- hence the current sticky error approach.
>
> > So maybe the name should now be less "snprintf"-like.
>
> Please, its important to stick to semantics for such well known type of
> routines, helps reviewing, etc.
>
> I'll keep the series up to that point and will run my build tests, then
> push it publicly to acme/perf/core and you can go from there, ok?
>
> I've changed the BIT() to BIT_ULL() as Andre suggested and I'm testing
> it again.

To make it clear, this is what I have locally:

0a04244cabc5560c (HEAD -> perf/core) perf arm-spe: Fix packet length handling
b65577baf4829092 perf arm-spe: Refactor arm_spe_get_events()
b2ded2e2e2764e50 perf arm-spe: Refactor payload size calculation
903b659436b70692 perf arm-spe: Fix a typo in comment
c185f1cde46653cd perf arm-spe: Include bitops.h for BIT() macro
40714c58630aaaf1 perf mem: Support ARM SPE events
c825f7885178f994 perf c2c: Support AUX trace
13e5df1e3f1ba1a9 perf mem: Support AUX trace
014a771c7867fda5 perf auxtrace: Add itrace option '-M' for memory events
436cce00710a3f23 perf mem: Only initialize memory event for recording
8b8173b45a7a9709 perf c2c: Support memory event PERF_MEM_EVENTS__LOAD_STORE
4ba2452cd88f39da perf mem: Support new memory event PERF_MEM_EVENTS__LOAD_STORE
eaf6aaeec5fa301c perf mem: Introduce weak function perf_mem_events__ptr()
f9f16dfbe76e63ba perf mem: Search event name with more flexible path
644bf4b0f7acde64 (tag: perf-tools-tests-v5.11-2020-11-04, acme/perf/core) perf jevents: Add test for arch std events

The perf-tools-tests-v5.11-2020-11-04, is in git.kernel.org, as it was
tested, etc, test results are in that signed tag, as usual for some
months.

- Arnaldo

2020-11-11 18:10:53

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v8 06/22] perf arm-spe: Refactor printing string to buffer

Em Wed, Nov 11, 2020 at 03:02:48PM -0300, Arnaldo Carvalho de Melo escreveu:
> > I'll keep the series up to that point and will run my build tests, then
> > push it publicly to acme/perf/core and you can go from there, ok?

> > I've changed the BIT() to BIT_ULL() as Andre suggested and I'm testing
> > it again.

> To make it clear, this is what I have locally:

> 0a04244cabc5560c (HEAD -> perf/core) perf arm-spe: Fix packet length handling
> b65577baf4829092 perf arm-spe: Refactor arm_spe_get_events()
> b2ded2e2e2764e50 perf arm-spe: Refactor payload size calculation
> 903b659436b70692 perf arm-spe: Fix a typo in comment
> c185f1cde46653cd perf arm-spe: Include bitops.h for BIT() macro
> 40714c58630aaaf1 perf mem: Support ARM SPE events
> c825f7885178f994 perf c2c: Support AUX trace
> 13e5df1e3f1ba1a9 perf mem: Support AUX trace
> 014a771c7867fda5 perf auxtrace: Add itrace option '-M' for memory events
> 436cce00710a3f23 perf mem: Only initialize memory event for recording
> 8b8173b45a7a9709 perf c2c: Support memory event PERF_MEM_EVENTS__LOAD_STORE
> 4ba2452cd88f39da perf mem: Support new memory event PERF_MEM_EVENTS__LOAD_STORE
> eaf6aaeec5fa301c perf mem: Introduce weak function perf_mem_events__ptr()
> f9f16dfbe76e63ba perf mem: Search event name with more flexible path
> 644bf4b0f7acde64 (tag: perf-tools-tests-v5.11-2020-11-04, acme/perf/core) perf jevents: Add test for arch std events

So with the above it works with at least these:

[perfbuilder@five ~]$ dm android-ndk:r15c-arm ubuntu:18.04-x-arm
1 22.37 android-ndk:r15c-arm : Ok arm-linux-androideabi-gcc (GCC) 4.9.x 20150123 (prerelease)
2 28.52 ubuntu:18.04-x-arm : Ok arm-linux-gnueabihf-gcc (Ubuntu/Linaro 7.5.0-3ubuntu1~18.04) 7.5.0
[perfbuilder@five ~]$

previously it was failing in all 32-bit build test containers:

[perfbuilder@five linux-perf-tools-build]$ grep FAIL dm.log/summary
android-ndk:r12b-arm: FAIL
android-ndk:r15c-arm: FAIL
fedora:24-x-ARC-uClibc: FAIL
fedora:30-x-ARC-uClibc: FAIL
ubuntu:16.04-x-arm: FAIL
ubuntu:16.04-x-powerpc: FAIL
ubuntu:18.04-x-arm: FAIL
ubuntu:18.04-x-m68k: FAIL
ubuntu:18.04-x-powerpc: FAIL
ubuntu:18.04-x-sh4: FAIL
ubuntu:19.10-x-hppa: FAIL
[perfbuilder@five linux-perf-tools-build]$

I'll redo the full set of tests and push perf/core publicly.

- Arnaldo

2020-11-12 01:47:08

by David Laight

[permalink] [raw]
Subject: RE: [PATCH v8 06/22] perf arm-spe: Refactor printing string to buffer

From: Dave Martin
> Sent: 11 November 2020 17:58
>
> On Wed, Nov 11, 2020 at 05:39:22PM +0000, Arnaldo Carvalho de Melo wrote:
> > Em Wed, Nov 11, 2020 at 03:45:23PM +0000, Andr� Przywara escreveu:
> > > On 11/11/2020 15:35, Arnaldo Carvalho de Melo wrote:
> > >
> > > Hi Arnaldo,
> > >
> > > thanks for taking a look!
> > >
> > > > Em Wed, Nov 11, 2020 at 03:11:33PM +0800, Leo Yan escreveu:
> > > >> When outputs strings to the decoding buffer with function snprintf(),
> > > >> SPE decoder needs to detects if any error returns from snprintf() and if
> > > >> so needs to directly bail out. If snprintf() returns success, it needs
> > > >> to update buffer pointer and reduce the buffer length so can continue to
> > > >> output the next string into the consequent memory space.
> > > >>
> > > >> This complex logics are spreading in the function arm_spe_pkt_desc() so
> > > >> there has many duplicate codes for handling error detecting, increment
> > > >> buffer pointer and decrement buffer size.
> > > >>
> > > >> To avoid the duplicate code, this patch introduces a new helper function
> > > >> arm_spe_pkt_snprintf() which is used to wrap up the complex logics, and
> > > >> it's used by the caller arm_spe_pkt_desc().
> > > >>
> > > >> This patch also moves the variable 'blen' as the function's local
> > > >> variable, this allows to remove the unnecessary braces and improve the
> > > >> readability.
> > > >>
> > > >> Suggested-by: Dave Martin <[email protected]>
> > > >> Signed-off-by: Leo Yan <[email protected]>
> > > >> Reviewed-by: Andre Przywara <[email protected]>
> > > >> ---
> > > >> .../arm-spe-decoder/arm-spe-pkt-decoder.c | 260 +++++++++---------
> > > >> 1 file changed, 126 insertions(+), 134 deletions(-)
> > > >>
> > > >> diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-
> decoder/arm-spe-pkt-decoder.c
> > > >> index 04fd7fd7c15f..1970686f7020 100644
> > > >> --- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
> > > >> +++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
> > > >> @@ -9,6 +9,7 @@
> > > >> #include <endian.h>
> > > >> #include <byteswap.h>
> > > >> #include <linux/bitops.h>
> > > >> +#include <stdarg.h>
> > > >>
> > > >> #include "arm-spe-pkt-decoder.h"
> > > >>
> > > >> @@ -258,192 +259,183 @@ int arm_spe_get_packet(const unsigned char *buf, size_t len,
> > > >> return ret;
> > > >> }
> > > >>
> > > >> +static int arm_spe_pkt_snprintf(int *err, char **buf_p, size_t *blen,
> > > >> + const char *fmt, ...)
> > > >> +{
> > > >> + va_list ap;
> > > >> + int ret;
> > > >> +
> > > >> + /* Bail out if any error occurred */
> > > >> + if (err && *err)
> > > >> + return *err;
> > > >> +
> > > >> + va_start(ap, fmt);
> > > >> + ret = vsnprintf(*buf_p, *blen, fmt, ap);
> > > >> + va_end(ap);
> > > >> +
> > > >> + if (ret < 0) {
> > > >> + if (err && !*err)
> > > >> + *err = ret;
> > > >> +
> > > >> + /*
> > > >> + * A return value of (*blen - 1) or more means that the
> > > >> + * output was truncated and the buffer is overrun.
> > > >> + */
> > > >> + } else if (ret >= ((int)*blen - 1)) {
> > > >> + (*buf_p)[*blen - 1] = '\0';
> > > >> +
> > > >> + /*
> > > >> + * Set *err to 'ret' to avoid overflow if tries to
> > > >> + * fill this buffer sequentially.
> > > >> + */
> > > >> + if (err && !*err)
> > > >> + *err = ret;
> > > >> + } else {
> > > >> + *buf_p += ret;
> > > >> + *blen -= ret;
> > > >> + }
> > > >> +
> > > >> + return ret;
> > > >> +}
> > > >> +

I'm not entirely sure that snprintf() can actually return a negative value.

Every implementation (except the microsoft one) also always writes a '\0'
even when the buffer is too short.

A simple wrapper that lets you append output and detect overflow is:
ret = vsnprintf(buf, len, ...);
if (ret < 0)
/* just in case */
return 0;
return ret > len ? len : ret;

So on overflow the sum of the lengths is equal to the buffer size
(ie includes the terminating '\0'.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

2020-11-12 05:37:32

by Leo Yan

[permalink] [raw]
Subject: Re: [PATCH v8 06/22] perf arm-spe: Refactor printing string to buffer

On Wed, Nov 11, 2020 at 03:01:27PM -0300, Arnaldo Carvalho de Melo wrote:
> Em Wed, Nov 11, 2020 at 05:58:27PM +0000, Dave Martin escreveu:
> >
> > On Wed, Nov 11, 2020 at 05:39:22PM +0000, Arnaldo Carvalho de Melo wrote:
> > > Em Wed, Nov 11, 2020 at 03:45:23PM +0000, Andr� Przywara escreveu:
> > > > On 11/11/2020 15:35, Arnaldo Carvalho de Melo wrote:
> > > > > Isn't this 'ret +=' ? Otherwise if any of these arm_spe_pkt_snprintf()
> > > > > calls are made the previous 'ret' value is simply discarded. Can you
> > > > > clarify this?
>
> > > > ret is the same as err. If err is negative (from previous calls), we
> > > > return that straight away, so it does nothing but propagating the error.
>
> > > Usually the return of a snprintf is used to account for buffer space, ok
> > > I'll have to read it, which I shouldn't as snprintf has a well defined
> > > meaning...
>
> > > Ok, now that I look at it, I realize it is not a snprintf() routine, but
> > > something with different semantics, that will look at a pointer to an
> > > integer and then do nothing if it comes with some error, etc, confusing
> > > :-/
>
> > Would you be happier if the function were renamed?
>
> > Originally we were aiming for snprintf() semantics, but this still
> > spawns a lot of boilerplate code and encourages mistakes in the local
> > caller here -- hence the current sticky error approach.
>
> > So maybe the name should now be less "snprintf"-like.
>
> Please, its important to stick to semantics for such well known type of
> routines, helps reviewing, etc.

My bad, will change the function name to arm_spe_pkt_out_string().

> I'll keep the series up to that point and will run my build tests, then
> push it publicly to acme/perf/core and you can go from there, ok?

Will follow up and rebase patches for next version.

> I've changed the BIT() to BIT_ULL() as Andre suggested and I'm testing
> it again.

I worry that consumed your (Arnaldo/Andre/Dave) much time, but very
appreciate you guy's helping.

Thanks,
Leo

2020-11-12 05:38:41

by Leo Yan

[permalink] [raw]
Subject: Re: [PATCH v8 06/22] perf arm-spe: Refactor printing string to buffer

Hi David,

On Wed, Nov 11, 2020 at 11:03:47PM +0000, David Laight wrote:
> From: Dave Martin
> > Sent: 11 November 2020 17:58
> >
> > On Wed, Nov 11, 2020 at 05:39:22PM +0000, Arnaldo Carvalho de Melo wrote:
> > > Em Wed, Nov 11, 2020 at 03:45:23PM +0000, Andr� Przywara escreveu:
> > > > On 11/11/2020 15:35, Arnaldo Carvalho de Melo wrote:
> > > >
> > > > Hi Arnaldo,
> > > >
> > > > thanks for taking a look!
> > > >
> > > > > Em Wed, Nov 11, 2020 at 03:11:33PM +0800, Leo Yan escreveu:
> > > > >> When outputs strings to the decoding buffer with function snprintf(),
> > > > >> SPE decoder needs to detects if any error returns from snprintf() and if
> > > > >> so needs to directly bail out. If snprintf() returns success, it needs
> > > > >> to update buffer pointer and reduce the buffer length so can continue to
> > > > >> output the next string into the consequent memory space.
> > > > >>
> > > > >> This complex logics are spreading in the function arm_spe_pkt_desc() so
> > > > >> there has many duplicate codes for handling error detecting, increment
> > > > >> buffer pointer and decrement buffer size.
> > > > >>
> > > > >> To avoid the duplicate code, this patch introduces a new helper function
> > > > >> arm_spe_pkt_snprintf() which is used to wrap up the complex logics, and
> > > > >> it's used by the caller arm_spe_pkt_desc().
> > > > >>
> > > > >> This patch also moves the variable 'blen' as the function's local
> > > > >> variable, this allows to remove the unnecessary braces and improve the
> > > > >> readability.
> > > > >>
> > > > >> Suggested-by: Dave Martin <[email protected]>
> > > > >> Signed-off-by: Leo Yan <[email protected]>
> > > > >> Reviewed-by: Andre Przywara <[email protected]>
> > > > >> ---
> > > > >> .../arm-spe-decoder/arm-spe-pkt-decoder.c | 260 +++++++++---------
> > > > >> 1 file changed, 126 insertions(+), 134 deletions(-)
> > > > >>
> > > > >> diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-
> > decoder/arm-spe-pkt-decoder.c
> > > > >> index 04fd7fd7c15f..1970686f7020 100644
> > > > >> --- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
> > > > >> +++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
> > > > >> @@ -9,6 +9,7 @@
> > > > >> #include <endian.h>
> > > > >> #include <byteswap.h>
> > > > >> #include <linux/bitops.h>
> > > > >> +#include <stdarg.h>
> > > > >>
> > > > >> #include "arm-spe-pkt-decoder.h"
> > > > >>
> > > > >> @@ -258,192 +259,183 @@ int arm_spe_get_packet(const unsigned char *buf, size_t len,
> > > > >> return ret;
> > > > >> }
> > > > >>
> > > > >> +static int arm_spe_pkt_snprintf(int *err, char **buf_p, size_t *blen,
> > > > >> + const char *fmt, ...)
> > > > >> +{
> > > > >> + va_list ap;
> > > > >> + int ret;
> > > > >> +
> > > > >> + /* Bail out if any error occurred */
> > > > >> + if (err && *err)
> > > > >> + return *err;
> > > > >> +
> > > > >> + va_start(ap, fmt);
> > > > >> + ret = vsnprintf(*buf_p, *blen, fmt, ap);
> > > > >> + va_end(ap);
> > > > >> +
> > > > >> + if (ret < 0) {
> > > > >> + if (err && !*err)
> > > > >> + *err = ret;
> > > > >> +
> > > > >> + /*
> > > > >> + * A return value of (*blen - 1) or more means that the
> > > > >> + * output was truncated and the buffer is overrun.
> > > > >> + */
> > > > >> + } else if (ret >= ((int)*blen - 1)) {
> > > > >> + (*buf_p)[*blen - 1] = '\0';
> > > > >> +
> > > > >> + /*
> > > > >> + * Set *err to 'ret' to avoid overflow if tries to
> > > > >> + * fill this buffer sequentially.
> > > > >> + */
> > > > >> + if (err && !*err)
> > > > >> + *err = ret;
> > > > >> + } else {
> > > > >> + *buf_p += ret;
> > > > >> + *blen -= ret;
> > > > >> + }
> > > > >> +
> > > > >> + return ret;
> > > > >> +}
> > > > >> +
>
> I'm not entirely sure that snprintf() can actually return a negative value.
>
> Every implementation (except the microsoft one) also always writes a '\0'
> even when the buffer is too short.
>
> A simple wrapper that lets you append output and detect overflow is:
> ret = vsnprintf(buf, len, ...);
> if (ret < 0)
> /* just in case */
> return 0;
> return ret > len ? len : ret;
>
> So on overflow the sum of the lengths is equal to the buffer size
> (ie includes the terminating '\0'.

We had some discussion for the return value in the old patch set, since
we want to keep the same scenmatics for the return value with
vsnprintf(), so the function arm_spe_pkt_snprintf() directly delivers
the return value from vsnprintf().

And if look at the patch 07/22, you could find the 'ret' value is not
really used at the end; the parameter 'err' is used as an accumulative
error value and it will be returned to up stack (0 means success and
non-zero means any failure).

For these two reasons, I'd like to keep as it is rather than converting
to other values.

Thanks for suggestion!
Leo