Skip to contentSkip to conversation

perf: Add support for memory region/range reporting

PATCH Superseded Thomas Falcon 4 participants 27 messages Jul 14 – Jul 31 perf events

v2 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v2 cover perf: Add support for memory region/range reporting

Add support for two memory-related reporting features in the perf tool:

1. Memory region reporting in perf-c2c and perf-script subcommands.
2. Memory range data in perf.data file header and perf-c2c subcommand.

Memory region reporting was introduced as part of support for the Off-module Response facility (OMR) [1], which provides a new data source encoding supporting "up to 8 fine-grained memory regions in addition to the cache region, offering more detailed insights into memory access regions."

Memory Range support was introduced with the addition of the ACPI Memory Range and Region Mapping (MRRM) [2] table. It provides a base address plus a length value for each memory range, as well as NUMA node and local and remote "region ID's" so that "platform firmware can indicate the type of memory for each range."[3]

Include a change allowing PERF_MEM_LVLNUM_L0 to be printed in perf-mem output as well.

[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/lkml/[email protected]/
[3]: MRRM definition allow for future expansion for the OS to assign these region IDs.

Changes in v2:
-- Added missing newline in WARN_ONCE message in c2c_he__set_mem_region() -- increased buffers passed to perf_script__meminfo_scnprintf() from 150
to 200 bytes to avoid silent truncation
-- Added check for NULL return of sysfs__mountpoint() when parsing
memory ranges in sysfs
-- increased MAX_MEMORY_RANGES sanity check from 64 to 256 based on
ACPI MRRM table implementation in Linux kernel
-- added comment for MAX_MEMORY_RANGES to clarify that it is a sanity check
for malformed perf.data files
-- removed a line of code was removed from util/env.h but was added back in
v1 due to bad rebase

Dapeng Mi (3):
perf mem: Add support for printing PERF_MEM_LVLNUM_L0 perf tools: Show memory region in perf-c2c subcommand perf tools: Show memory region in perf-script subcommand

Thomas Falcon (3):
perf mem: Fix size tracking for mem_lvl's in
perf_script__meminfo_scnprintf()
perf header: Support memory ranges
perf c2c: print memory region data with stdio output

 tools/perf/Documentation/perf-record.txt      |   2 +-
 .../Documentation/perf.data-file-format.txt   |  13 ++
 tools/perf/builtin-c2c.c                      |  86 +++++++-
 tools/perf/builtin-inject.c                   |   1 +
 tools/perf/builtin-script.c                   |   8 +-
 tools/perf/util/bpf-filter.l                  |   1 +
 tools/perf/util/env.c                         |   1 +
 tools/perf/util/env.h                         |  10 +
 tools/perf/util/header.c                      | 195 ++++++++++++++++++
 tools/perf/util/header.h                      |   1 +
 tools/perf/util/mem-events.c                  |  75 ++++++-
 tools/perf/util/mem-events.h                  |   1 +
 .../scripting-engines/trace-event-python.c    |   4 +-
 13 files changed, 388 insertions(+), 10 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/6 perf mem: Fix size tracking for mem_lvl's in perf_script__meminfo_scnprintf()

When printing memory info in perf script, the entire size of the buffer is passed to perf_mem__lvl_scnprintf() instead of the remaining size. Pass the remaining buffer size instead.

This issue was detected by sashiko during an internal code review.

Assisted-by Sashiko:gemini-3.1-pro-preview
Fixes fdefc3750e847 ("perf mem: Print memory operation type")
---
 tools/perf/util/mem-events.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/mem-events.c +1 -1
index 0b49fce251fc..4e490f9cd348 100644
@@ -610,7 +610,7 @@ int perf_script__meminfo_scnprintf(char *out, size_t sz, const struct mem_info *
 	i += scnprintf(out, sz, "|OP ");
 	i += perf_mem__op_scnprintf(out + i, sz - i, mem_info);
 	i += scnprintf(out + i, sz - i, "|LVL ");
-	i += perf_mem__lvl_scnprintf(out + i, sz, mem_info);
+	i += perf_mem__lvl_scnprintf(out + i, sz - i, mem_info);
 	i += scnprintf(out + i, sz - i, "|SNP ");
 	i += perf_mem__snp_scnprintf(out + i, sz - i, mem_info);
 	i += scnprintf(out + i, sz - i, "|TLB ");
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v2 1/6] perf mem: Fix size tracking for mem_lvl's in perf_script__meminfo_scnprintf()

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Severe pre-existing stack buffer overflows and integer underflows in perf_mem__*_scnprintf functions due to unsafe strcat usage and missing bounds checks. --

--- Patch [1]: [PATCH v2 1/6] perf mem: Fix size tracking for mem_lvl's in perf_script__meminfo_scnprintf() --- commit cf8f02c307060f141740bb3567a69f77761e0f5f
Author: Thomas Falcon <[email protected]>

perf mem: Fix size tracking for mem_lvl's in perf_script__meminfo_scnprintf()

When printing memory info in perf script, the entire size of the buffer is passed to perf_mem__lvl_scnprintf() instead of the remaining size. Pass the remaining buffer size instead.

tools/perf/util/mem-events.c int perf_script__meminfo_scnprintf(char *out, size_t sz, const struct mem_info * · line 613

Quoted 10 lines
tools/perf/util/mem-events.c +1 -1
index 0b49fce251fcc..4e490f9cd3488 100644
@@ -610,7 +610,7 @@ int perf_script__meminfo_scnprintf(char *out, size_t sz, const struct mem_info *
 	i += scnprintf(out, sz, "|OP ");
 	i += perf_mem__op_scnprintf(out + i, sz - i, mem_info);
 	i += scnprintf(out + i, sz - i, "|LVL ");
-	i += perf_mem__lvl_scnprintf(out + i, sz, mem_info);
+	i += perf_mem__lvl_scnprintf(out + i, sz - i, mem_info);

[Severity: High]
This is a pre-existing issue, but does passing sz - i here risk an integer underflow and stack buffer overflow in the callee if the buffer is already full?

If previous fields exhaust the buffer, sz - i can be 0 (or wrap around if i > sz). If 0 is passed to perf_mem__lvl_scnprintf(), the size decrement at the start of the function will underflow:

tools/perf/util/mem-events.c:perf_mem__lvl_scnprintf() {
        sz -= 1; /* -1 for null termination */
        out[0] = '\0';
        ...
        strcat(out, "Remote ");
}

When sz underflows to SIZE_MAX, out[0] = '\0' writes out of bounds, and the unbounded strcat operations can corrupt the stack if a malformed or untrusted perf.data file sets multiple bits in data_src.

Does this same unsafe pattern also exist in perf_mem__snp_scnprintf, perf_mem__tlb_scnprintf, and perf_mem__blk_scnprintf, which all start with the same sz -= 1 decrement?

Quoted 3 lines
tools/perf/util/mem-events.c +0 -0
 	i += scnprintf(out + i, sz - i, "|SNP ");
 	i += perf_mem__snp_scnprintf(out + i, sz - i, mem_info);
 	i += scnprintf(out + i, sz - i, "|TLB ");
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH v2 1/6] perf mem: Fix size tracking for mem_lvl's in perf_script__meminfo_scnprintf()

Quoted 7 lines · On Tue, 2026-07-14 at 00:51 +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Severe pre-existing stack buffer overflows and integer underflows in perf_mem__*_scnprintf functions due to unsafe strcat usage and missing bounds checks.

Since these are pre-existing issues, can they be handled in a separate patch series?

Thanks,
Tom

Quoted 62 lines

--

--- Patch [1]: [PATCH v2 1/6] perf mem: Fix size tracking for mem_lvl's in perf_script__meminfo_scnprintf() --- commit cf8f02c307060f141740bb3567a69f77761e0f5f
Author: Thomas Falcon <[email protected]>

perf mem: Fix size tracking for mem_lvl's in
perf_script__meminfo_scnprintf()

When printing memory info in perf script, the entire size of the buffer is passed to perf_mem__lvl_scnprintf() instead of the remaining
size. Pass the remaining buffer size instead.

tools/perf/util/mem-events.c int perf_script__meminfo_scnprintf(char *out, size_t sz, const struct mem_info * · line 613

Quoted 12 lines
tools/perf/util/mem- renamed from tools/perf/util/mem-events.c +0 -0

events.c
index 0b49fce251fcc..4e490f9cd3488 100644

tools/perf/util/mem-events.c +0 -0
@@ -610,7 +610,7 @@ int perf_script__meminfo_scnprintf(char *out,

size_t sz, const struct mem_info *
  i += scnprintf(out, sz, "|OP ");
  i += perf_mem__op_scnprintf(out + i, sz - i, mem_info); i += scnprintf(out + i, sz - i, "|LVL ");

tools/perf/util/mem-events.c +1 -1
-	i += perf_mem__lvl_scnprintf(out + i, sz, mem_info);
+	i += perf_mem__lvl_scnprintf(out + i, sz - i, mem_info);

[Severity: High]
This is a pre-existing issue, but does passing sz - i here risk an integer
underflow and stack buffer overflow in the callee if the buffer is already
full?

If previous fields exhaust the buffer, sz - i can be 0 (or wrap around if
i > sz). If 0 is passed to perf_mem__lvl_scnprintf(), the size decrement
at the start of the function will underflow:

tools/perf/util/mem-events.c:perf_mem__lvl_scnprintf() { sz -= 1; /* -1 for null termination */ out[0] = '\0';
        ...
        strcat(out, "Remote ");
}

When sz underflows to SIZE_MAX, out[0] = '\0' writes out of bounds, and
the unbounded strcat operations can corrupt the stack if a malformed or
untrusted perf.data file sets multiple bits in data_src.

Does this same unsafe pattern also exist in perf_mem__snp_scnprintf, perf_mem__tlb_scnprintf, and perf_mem__blk_scnprintf, which all start with the same sz -= 1 decrement?

Quoted 3 lines

  i += scnprintf(out + i, sz - i, "|SNP ");
  i += perf_mem__snp_scnprintf(out + i, sz - i, mem_info); i += scnprintf(out + i, sz - i, "|TLB ");

Reviewed-by on v2 1/6
#4 lore raw

Re: [PATCH v2 1/6] perf mem: Fix size tracking for mem_lvl's in perf_script__meminfo_scnprintf()

In reply to Thomas Falcon

LGTM.

Quoted 26 lines · Thomas Falcon

When printing memory info in perf script, the entire size of the buffer is passed to perf_mem__lvl_scnprintf() instead of the remaining size. Pass the remaining buffer size instead.

This issue was detected by sashiko during an internal code review.

Assisted-by Sashiko:gemini-3.1-pro-preview
Fixes fdefc3750e847 ("perf mem: Print memory operation type")
---
 tools/perf/util/mem-events.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/mem-events.c +1 -1
index 0b49fce251fc..4e490f9cd348 100644
@@ -610,7 +610,7 @@ int perf_script__meminfo_scnprintf(char *out, size_t sz, const struct mem_info *
 	i += scnprintf(out, sz, "|OP ");
 	i += perf_mem__op_scnprintf(out + i, sz - i, mem_info);
 	i += scnprintf(out + i, sz - i, "|LVL ");
-	i += perf_mem__lvl_scnprintf(out + i, sz, mem_info);
+	i += perf_mem__lvl_scnprintf(out + i, sz - i, mem_info);
 	i += scnprintf(out + i, sz - i, "|SNP ");
 	i += perf_mem__snp_scnprintf(out + i, sz - i, mem_info);
 	i += scnprintf(out + i, sz - i, "|TLB ");
#5 lore raw

PATCH v2 2/6 perf mem: Add support for printing PERF_MEM_LVLNUM_L0

In reply to Thomas Falcon

From: Dapeng Mi <[email protected]>

Add support for printing PERF_MEM_LVLNUM_L0 in perf mem report.

Assisted-by Sashiko:gemini-3.1-pro-preview
---
 tools/perf/Documentation/perf-record.txt | 2 +-
 tools/perf/util/bpf-filter.l             | 1 +
 tools/perf/util/mem-events.c             | 5 +++++
 tools/perf/util/mem-events.h             | 1 +
 4 files changed, 8 insertions(+), 1 deletion(-)
tools/perf/Documentation/perf-record.txt +1 -1
index 178f483140ed..b54032efe41c 100644
@@ -212,7 +212,7 @@ OPTIONS
 	The <value> can be one of:
 	  <number> (for any term)
 	  na, load, store, pfetch, exec (for mem_op)
-	  l1, l2, l3, l4, cxl, io, any_cache, lfb, ram, pmem (for mem_lvl)
+	  l0, l1, l2, l3, l4, cxl, io, any_cache, lfb, ram, pmem (for mem_lvl)
 	  na, none, hit, miss, hitm, fwd, peer (for mem_snoop)
 	  remote (for mem_remote)
 	  na, locked (for mem_locked)
tools/perf/util/bpf-filter.l +1 -0
index 6aa65ade3385..1be9df6550fc 100644
@@ -131,6 +131,7 @@ store		{ return constant(PERF_MEM_OP_STORE); }
 pfetch		{ return constant(PERF_MEM_OP_PFETCH); }
 exec		{ return constant(PERF_MEM_OP_EXEC); }
 
+l0		{ return constant(PERF_MEM_LVLNUM_L0); }
 l1		{ return constant(PERF_MEM_LVLNUM_L1); }
 l2		{ return constant(PERF_MEM_LVLNUM_L2); }
 l3		{ return constant(PERF_MEM_LVLNUM_L3); }
tools/perf/util/mem-events.c +5 -0
index 4e490f9cd348..4fd48fd20055 100644
@@ -391,6 +391,7 @@ static const char * const mem_lvlnum[] = {
 	[PERF_MEM_LVLNUM_L4] = "L4",
 	[PERF_MEM_LVLNUM_L2_MHB] = "L2 MHB",
 	[PERF_MEM_LVLNUM_MSC] = "Memory-side Cache",
+	[PERF_MEM_LVLNUM_L0] = "L0",
 	[PERF_MEM_LVLNUM_UNC] = "Uncached",
 	[PERF_MEM_LVLNUM_CXL] = "CXL",
 	[PERF_MEM_LVLNUM_IO] = "I/O",
@@ -831,6 +832,8 @@ int mem_stat_index(const enum mem_stat_type mst, const u64 val)
 		}
 	case PERF_MEM_STAT_CACHE:
 		switch (src.mem_lvl_num) {
+		case PERF_MEM_LVLNUM_L0:
+			return MEM_STAT_CACHE_L0;
 		case PERF_MEM_LVLNUM_L1:
 			return MEM_STAT_CACHE_L1;
 		case PERF_MEM_LVLNUM_L2:
@@ -915,6 +918,8 @@ const char *mem_stat_name(const enum mem_stat_type mst, const int idx)
 		}
 	case PERF_MEM_STAT_CACHE:
 		switch (idx) {
+		case MEM_STAT_CACHE_L0:
+			return "L0";
 		case MEM_STAT_CACHE_L1:
 			return "L1";
 		case MEM_STAT_CACHE_L2:
tools/perf/util/mem-events.h +1 -0
index 5b98076904b0..daa22748f9fe 100644
@@ -109,6 +109,7 @@ enum mem_stat_op {
 };
 
 enum mem_stat_cache {
+	MEM_STAT_CACHE_L0,
 	MEM_STAT_CACHE_L1,
 	MEM_STAT_CACHE_L2,
 	MEM_STAT_CACHE_L3,
Signature · 1 line
2.43.0
Reviewed-by on v2 2/6
#6 lore raw

Re: [PATCH v2 2/6] perf mem: Add support for printing PERF_MEM_LVLNUM_L0

LGTM. Thanks.

Quoted 81 lines · Thomas Falcon

From: Dapeng Mi <[email protected]>

Add support for printing PERF_MEM_LVLNUM_L0 in perf mem report.

Assisted-by Sashiko:gemini-3.1-pro-preview
---
 tools/perf/Documentation/perf-record.txt | 2 +-
 tools/perf/util/bpf-filter.l             | 1 +
 tools/perf/util/mem-events.c             | 5 +++++
 tools/perf/util/mem-events.h             | 1 +
 4 files changed, 8 insertions(+), 1 deletion(-)
tools/perf/Documentation/perf-record.txt +1 -1
index 178f483140ed..b54032efe41c 100644
@@ -212,7 +212,7 @@ OPTIONS
 	The <value> can be one of:
 	  <number> (for any term)
 	  na, load, store, pfetch, exec (for mem_op)
-	  l1, l2, l3, l4, cxl, io, any_cache, lfb, ram, pmem (for mem_lvl)
+	  l0, l1, l2, l3, l4, cxl, io, any_cache, lfb, ram, pmem (for mem_lvl)
 	  na, none, hit, miss, hitm, fwd, peer (for mem_snoop)
 	  remote (for mem_remote)
 	  na, locked (for mem_locked)
tools/perf/util/bpf-filter.l +1 -0
index 6aa65ade3385..1be9df6550fc 100644
@@ -131,6 +131,7 @@ store		{ return constant(PERF_MEM_OP_STORE); }
 pfetch		{ return constant(PERF_MEM_OP_PFETCH); }
 exec		{ return constant(PERF_MEM_OP_EXEC); }
 
+l0		{ return constant(PERF_MEM_LVLNUM_L0); }
 l1		{ return constant(PERF_MEM_LVLNUM_L1); }
 l2		{ return constant(PERF_MEM_LVLNUM_L2); }
 l3		{ return constant(PERF_MEM_LVLNUM_L3); }
tools/perf/util/mem-events.c +5 -0
index 4e490f9cd348..4fd48fd20055 100644
@@ -391,6 +391,7 @@ static const char * const mem_lvlnum[] = {
 	[PERF_MEM_LVLNUM_L4] = "L4",
 	[PERF_MEM_LVLNUM_L2_MHB] = "L2 MHB",
 	[PERF_MEM_LVLNUM_MSC] = "Memory-side Cache",
+	[PERF_MEM_LVLNUM_L0] = "L0",
 	[PERF_MEM_LVLNUM_UNC] = "Uncached",
 	[PERF_MEM_LVLNUM_CXL] = "CXL",
 	[PERF_MEM_LVLNUM_IO] = "I/O",
@@ -831,6 +832,8 @@ int mem_stat_index(const enum mem_stat_type mst, const u64 val)
 		}
 	case PERF_MEM_STAT_CACHE:
 		switch (src.mem_lvl_num) {
+		case PERF_MEM_LVLNUM_L0:
+			return MEM_STAT_CACHE_L0;
 		case PERF_MEM_LVLNUM_L1:
 			return MEM_STAT_CACHE_L1;
 		case PERF_MEM_LVLNUM_L2:
@@ -915,6 +918,8 @@ const char *mem_stat_name(const enum mem_stat_type mst, const int idx)
 		}
 	case PERF_MEM_STAT_CACHE:
 		switch (idx) {
+		case MEM_STAT_CACHE_L0:
+			return "L0";
 		case MEM_STAT_CACHE_L1:
 			return "L1";
 		case MEM_STAT_CACHE_L2:
tools/perf/util/mem-events.h +1 -0
index 5b98076904b0..daa22748f9fe 100644
@@ -109,6 +109,7 @@ enum mem_stat_op {
 };
 
 enum mem_stat_cache {
+	MEM_STAT_CACHE_L0,
 	MEM_STAT_CACHE_L1,
 	MEM_STAT_CACHE_L2,
 	MEM_STAT_CACHE_L3,
maintainer
#7 lore raw

Re: [PATCH v2 2/6] perf mem: Add support for printing PERF_MEM_LVLNUM_L0

In reply to Thomas Falcon

Hello,

Quoted 3 lines · Thomas Falcon

From: Dapeng Mi <[email protected]>

Add support for printing PERF_MEM_LVLNUM_L0 in perf mem report.

What's the L0, by the way?

Thanks,
Namhyung

Quoted 81 lines
Assisted-by Sashiko:gemini-3.1-pro-preview
---
 tools/perf/Documentation/perf-record.txt | 2 +-
 tools/perf/util/bpf-filter.l             | 1 +
 tools/perf/util/mem-events.c             | 5 +++++
 tools/perf/util/mem-events.h             | 1 +
 4 files changed, 8 insertions(+), 1 deletion(-)
tools/perf/Documentation/perf-record.txt +1 -1
index 178f483140ed..b54032efe41c 100644
@@ -212,7 +212,7 @@ OPTIONS
 	The <value> can be one of:
 	  <number> (for any term)
 	  na, load, store, pfetch, exec (for mem_op)
-	  l1, l2, l3, l4, cxl, io, any_cache, lfb, ram, pmem (for mem_lvl)
+	  l0, l1, l2, l3, l4, cxl, io, any_cache, lfb, ram, pmem (for mem_lvl)
 	  na, none, hit, miss, hitm, fwd, peer (for mem_snoop)
 	  remote (for mem_remote)
 	  na, locked (for mem_locked)
tools/perf/util/bpf-filter.l +1 -0
index 6aa65ade3385..1be9df6550fc 100644
@@ -131,6 +131,7 @@ store		{ return constant(PERF_MEM_OP_STORE); }
 pfetch		{ return constant(PERF_MEM_OP_PFETCH); }
 exec		{ return constant(PERF_MEM_OP_EXEC); }
 
+l0		{ return constant(PERF_MEM_LVLNUM_L0); }
 l1		{ return constant(PERF_MEM_LVLNUM_L1); }
 l2		{ return constant(PERF_MEM_LVLNUM_L2); }
 l3		{ return constant(PERF_MEM_LVLNUM_L3); }
tools/perf/util/mem-events.c +5 -0
index 4e490f9cd348..4fd48fd20055 100644
@@ -391,6 +391,7 @@ static const char * const mem_lvlnum[] = {
 	[PERF_MEM_LVLNUM_L4] = "L4",
 	[PERF_MEM_LVLNUM_L2_MHB] = "L2 MHB",
 	[PERF_MEM_LVLNUM_MSC] = "Memory-side Cache",
+	[PERF_MEM_LVLNUM_L0] = "L0",
 	[PERF_MEM_LVLNUM_UNC] = "Uncached",
 	[PERF_MEM_LVLNUM_CXL] = "CXL",
 	[PERF_MEM_LVLNUM_IO] = "I/O",
@@ -831,6 +832,8 @@ int mem_stat_index(const enum mem_stat_type mst, const u64 val)
 		}
 	case PERF_MEM_STAT_CACHE:
 		switch (src.mem_lvl_num) {
+		case PERF_MEM_LVLNUM_L0:
+			return MEM_STAT_CACHE_L0;
 		case PERF_MEM_LVLNUM_L1:
 			return MEM_STAT_CACHE_L1;
 		case PERF_MEM_LVLNUM_L2:
@@ -915,6 +918,8 @@ const char *mem_stat_name(const enum mem_stat_type mst, const int idx)
 		}
 	case PERF_MEM_STAT_CACHE:
 		switch (idx) {
+		case MEM_STAT_CACHE_L0:
+			return "L0";
 		case MEM_STAT_CACHE_L1:
 			return "L1";
 		case MEM_STAT_CACHE_L2:
tools/perf/util/mem-events.h +1 -0
index 5b98076904b0..daa22748f9fe 100644
@@ -109,6 +109,7 @@ enum mem_stat_op {
 };
 
 enum mem_stat_cache {
+	MEM_STAT_CACHE_L0,
 	MEM_STAT_CACHE_L1,
 	MEM_STAT_CACHE_L2,
 	MEM_STAT_CACHE_L3,
Signature · 1 line
2.43.0
#8 lore raw

Re: [PATCH v2 2/6] perf mem: Add support for printing PERF_MEM_LVLNUM_L0

Quoted 7 lines · Namhyung Kim

Hello,

Quoted 3 lines · Thomas Falcon

From: Dapeng Mi <[email protected]>

Add support for printing PERF_MEM_LVLNUM_L0 in perf mem report.

What's the L0, by the way?

Per my understanding, the L0 cache is newly added a small piece of cache which is the closest, lowest-latency memory cache tied directly to the execution pipeline.

The table "Table 9-4. Data Source Field Encodings for Panther Cove and Coyote Cove Microarchitectures" in the ISE doc chapter "9.2.1 Panther Cove and Coyote Cove Microarchitectures Memory Auxiliary Field Layout" indicates the code "01H" means the "L0 Hit - Minimal latency core cache hit. This request was satisfied by the L0 data cache."

https://www.intel.com/content/www/us/en/content-details/922690/intel-architecture-instruction-set-extensions-programming-reference.html

Quoted 84 lines

Thanks,
Namhyung

Quoted 80 lines
Assisted-by Sashiko:gemini-3.1-pro-preview
---
 tools/perf/Documentation/perf-record.txt | 2 +-
 tools/perf/util/bpf-filter.l             | 1 +
 tools/perf/util/mem-events.c             | 5 +++++
 tools/perf/util/mem-events.h             | 1 +
 4 files changed, 8 insertions(+), 1 deletion(-)
tools/perf/Documentation/perf-record.txt +1 -1
index 178f483140ed..b54032efe41c 100644
@@ -212,7 +212,7 @@ OPTIONS
 	The <value> can be one of:
 	  <number> (for any term)
 	  na, load, store, pfetch, exec (for mem_op)
-	  l1, l2, l3, l4, cxl, io, any_cache, lfb, ram, pmem (for mem_lvl)
+	  l0, l1, l2, l3, l4, cxl, io, any_cache, lfb, ram, pmem (for mem_lvl)
 	  na, none, hit, miss, hitm, fwd, peer (for mem_snoop)
 	  remote (for mem_remote)
 	  na, locked (for mem_locked)
tools/perf/util/bpf-filter.l +1 -0
index 6aa65ade3385..1be9df6550fc 100644
@@ -131,6 +131,7 @@ store		{ return constant(PERF_MEM_OP_STORE); }
 pfetch		{ return constant(PERF_MEM_OP_PFETCH); }
 exec		{ return constant(PERF_MEM_OP_EXEC); }
 
+l0		{ return constant(PERF_MEM_LVLNUM_L0); }
 l1		{ return constant(PERF_MEM_LVLNUM_L1); }
 l2		{ return constant(PERF_MEM_LVLNUM_L2); }
 l3		{ return constant(PERF_MEM_LVLNUM_L3); }
tools/perf/util/mem-events.c +5 -0
index 4e490f9cd348..4fd48fd20055 100644
@@ -391,6 +391,7 @@ static const char * const mem_lvlnum[] = {
 	[PERF_MEM_LVLNUM_L4] = "L4",
 	[PERF_MEM_LVLNUM_L2_MHB] = "L2 MHB",
 	[PERF_MEM_LVLNUM_MSC] = "Memory-side Cache",
+	[PERF_MEM_LVLNUM_L0] = "L0",
 	[PERF_MEM_LVLNUM_UNC] = "Uncached",
 	[PERF_MEM_LVLNUM_CXL] = "CXL",
 	[PERF_MEM_LVLNUM_IO] = "I/O",
@@ -831,6 +832,8 @@ int mem_stat_index(const enum mem_stat_type mst, const u64 val)
 		}
 	case PERF_MEM_STAT_CACHE:
 		switch (src.mem_lvl_num) {
+		case PERF_MEM_LVLNUM_L0:
+			return MEM_STAT_CACHE_L0;
 		case PERF_MEM_LVLNUM_L1:
 			return MEM_STAT_CACHE_L1;
 		case PERF_MEM_LVLNUM_L2:
@@ -915,6 +918,8 @@ const char *mem_stat_name(const enum mem_stat_type mst, const int idx)
 		}
 	case PERF_MEM_STAT_CACHE:
 		switch (idx) {
+		case MEM_STAT_CACHE_L0:
+			return "L0";
 		case MEM_STAT_CACHE_L1:
 			return "L1";
 		case MEM_STAT_CACHE_L2:
tools/perf/util/mem-events.h +1 -0
index 5b98076904b0..daa22748f9fe 100644
@@ -109,6 +109,7 @@ enum mem_stat_op {
 };
 
 enum mem_stat_cache {
+	MEM_STAT_CACHE_L0,
 	MEM_STAT_CACHE_L1,
 	MEM_STAT_CACHE_L2,
 	MEM_STAT_CACHE_L3,
Signature · 1 line
2.43.0
maintainer
#9 lore raw

Re: [PATCH v2 2/6] perf mem: Add support for printing PERF_MEM_LVLNUM_L0

Quoted 14 lines · Dapeng
Quoted 7 lines · Namhyung Kim

Hello,

Quoted 3 lines · Thomas Falcon

From: Dapeng Mi <[email protected]>

Add support for printing PERF_MEM_LVLNUM_L0 in perf mem report.

What's the L0, by the way?

Per my understanding, the L0 cache is newly added a small piece of cache which is the closest, lowest-latency memory cache tied directly to the execution pipeline.

Quoted 7 lines

The table "Table 9-4. Data Source Field Encodings for Panther Cove and Coyote Cove Microarchitectures" in the ISE doc chapter "9.2.1 Panther Cove and Coyote Cove Microarchitectures Memory Auxiliary Field Layout" indicates the code "01H" means the "L0 Hit - Minimal latency core cache hit. This request was satisfied by the L0 data cache."

https://www.intel.com/content/www/us/en/content-details/922690/intel-architecture-instruction-set-extensions-programming-reference.html

Interesting, thanks for the explanation!

Thanks,
Namhyung

#10 lore raw

PATCH v2 3/6 perf tools: Show memory region in perf-c2c subcommand

In reply to Thomas Falcon

From: Dapeng Mi <[email protected]>

Add memory region field to the cacheline list view to help users identify the memory region to which the cacheline belongs. The memory region field was included with the introduction of support for the Off-module Response facility (OMR) [1] in Intel's Diamond Rapids and Nova Lake architectures.

An example of the new perf c2c output including the memory region field is shown below:

Shared Data Cache Line Table (112 entries, sorted on Total HITMs)

       --------------- Cacheline --------------      Tot  ------- Load Hitm -------    Total    Total    Total
Index             Address  Region  Node  PA cnt     Hitm    Total  LclHitm  RmtHitm  records    Loads   Stores
    0  0xff1b7032a1e8c5c0  N/A        1      47    2.25%       50       44        6     1024     1023        1
    1  0xff1b6ff3255bb880  N/A        0      11    1.62%       36       34        2       96       93        4
    2  0xff1b70328e3b9880  N/A        1      13    1.49%       33       33        0      100       91        9
    3  0xff1b70328b023800  N/A        1       1    1.40%       31       14       17       52       48        4
    4  0xff1b70328e3b9c00  N/A        1       1    1.31%       29       27        2       67       33       34
    5  0xff1b70328b0237c0  N/A        1       1    1.26%       28       10       18      154      150        4
    6  0xff1b6ff3255bbc00  N/A        0       1    1.13%       25       25        0       48       25       23
    7  0xff1b6ff3255bba40  N/A        0       1    0.99%       22       22        0       46       23       23
    8  0xff3ab9ba50255c80  N/A      N/A       0    0.77%       17       15        2       35       35        0
    9  0xff3ab9ba503e3040  N/A      N/A       0    0.77%       17       11        6       37       37        1
   10  0xff1b703289e88f40  N/A        1      33    0.72%       16        9        7       69       63        6
   11  0xff1b70328e3b9a40  N/A        1       1    0.68%       15       15        0       43       17       26
   12  0xff1b7032c9fd6a40  N/A        1      15    0.68%       15       15        0       57       54        4
   13  0xff1b7032a1e8c980  N/A        1      27    0.54%       12       11        1      761      761        0
   14  0xff1b70727ffd57c0  N/A        1       7    0.54%       12       12        0      219      218        1
   15  0xffffffffaefe2380  N/A        1       1    0.50%       11        8        3       14       14        0

Note: DMR simics does not support memory regions. Since the output is captured on SPR, the memory region field shows "N/A" for all cachelines.

[1]: https://lore.kernel.org/all/[email protected]/

Assisted-by Sashiko:gemini-3.1-pro-preview

---
Changes in v2:
-- Added missing new line in WARN_ONCE message in c2c_he__set_mem_region()

---
 tools/perf/builtin-c2c.c | 56 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 54 insertions(+), 2 deletions(-)
tools/perf/builtin-c2c.c +54 -2
index c9584dbedf77..cba2e9f21ae7 100644
@@ -74,6 +74,7 @@ struct c2c_hist_entry {
 	unsigned long		*nodeset;
 	struct c2c_stats	*node_stats;
 	unsigned int		 cacheline_idx;
+	unsigned int		 mem_region;
 
 	struct compute_stats	 cstats;
 
@@ -281,6 +282,18 @@ static void c2c_he__set_node(struct c2c_hist_entry *c2c_he,
 	}
 }
 
+static void c2c_he__set_mem_region(struct c2c_hist_entry *c2c_he,
+				   unsigned int mem_region)
+{
+	if (WARN_ONCE(mem_region > PERF_MEM_REGION_MEM7,
+		      "WARNING: invalid memory region ID\n"))
+		return;
+
+	/* Update mem_region only if it really accesses memory */
+	if (mem_region >= PERF_MEM_REGION_MMIO)
+		c2c_he->mem_region = mem_region;
+}
+
 static void compute_stats(struct c2c_hist_entry *c2c_he,
 			  struct c2c_stats *stats,
 			  u64 weight)
@@ -339,6 +352,7 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused,
 	struct addr_location al;
 	struct mem_info *mi = NULL;
 	struct callchain_cursor *cursor;
+	unsigned int mem_region;
 	int ret;
 
 	addr_location__init(&al);
@@ -366,6 +380,7 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused,
 	}
 
 	c2c_decode_stats(&stats, mi);
+	mem_region = mem_info__data_src(mi)->mem_region;
 
 	he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
 				  &al, NULL, NULL, mi, NULL,
@@ -382,6 +397,7 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused,
 	c2c_he__set_cpu(c2c_he, sample);
 	c2c_he__set_node(c2c_he, sample);
 	c2c_he__set_evsel(c2c_he, evsel);
+	c2c_he__set_mem_region(c2c_he, mem_region);
 
 	hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
 
@@ -435,6 +451,7 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused,
 		c2c_he__set_cpu(c2c_he, sample);
 		c2c_he__set_node(c2c_he, sample);
 		c2c_he__set_evsel(c2c_he, evsel);
+		c2c_he__set_mem_region(c2c_he, mem_region);
 
 		hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
 		ret = hist_entry__append_callchain(he, sample);
@@ -603,6 +620,29 @@ dcacheline_node_count(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
 	return scnprintf(hpp->buf, hpp->size, "%*lu", width, c2c_he->paddr_cnt);
 }
 
+static int
+dcacheline_node_mem_region(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
+			   struct hist_entry *he)
+{
+	int width = c2c_width(fmt, hpp, he->hists);
+	struct c2c_hist_entry *c2c_he;
+	unsigned int mem_region;
+	char buf[20];
+
+	c2c_he = container_of(he, struct c2c_hist_entry, he);
+	mem_region = c2c_he->mem_region;
+
+	if (mem_region == PERF_MEM_REGION_NA)
+		scnprintf(buf, sizeof(buf),  "N/A");
+	/* mem_region could only be >= PERF_MEM_REGION_MMIO */
+	else if (mem_region == PERF_MEM_REGION_MMIO)
+		scnprintf(buf, sizeof(buf), "MMIO");
+	else
+		scnprintf(buf, sizeof(buf), "0x%x", mem_region - 0x8);
+
+	return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
+}
+
 static int offset_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
 			struct hist_entry *he)
 {
@@ -1425,7 +1465,7 @@ cl_idx_empty_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
 	}
 
 static struct c2c_dimension dim_dcacheline = {
-	.header		= HEADER_SPAN("--- Cacheline ----", "Address", 2),
+	.header		= HEADER_SPAN("--- Cacheline ----", "Address", 3),
 	.name		= "dcacheline",
 	.cmp		= dcacheline_cmp,
 	.entry		= dcacheline_entry,
@@ -1440,6 +1480,14 @@ static struct c2c_dimension dim_dcacheline_node = {
 	.width		= 4,
 };
 
+static struct c2c_dimension dim_dcacheline_mem_region = {
+	.header		= HEADER_LOW("Region"),
+	.name		= "dcacheline_mem_region",
+	.cmp		= empty_cmp,
+	.entry		= dcacheline_node_mem_region,
+	.width		= 6,
+};
+
 static struct c2c_dimension dim_dcacheline_count = {
 	.header		= HEADER_LOW("PA cnt"),
 	.name		= "dcacheline_count",
@@ -1871,6 +1919,7 @@ static struct c2c_dimension dim_dcacheline_num_empty = {
 
 static struct c2c_dimension *dimensions[] = {
 	&dim_dcacheline,
+	&dim_dcacheline_mem_region,
 	&dim_dcacheline_node,
 	&dim_dcacheline_count,
 	&dim_offset,
@@ -2898,8 +2947,9 @@ static int ui_quirks(void)
 	/* Fix the zero line for dcacheline column. */
 	buf = fill_line(chk_double_cl ? "Double-Cacheline" : "Cacheline",
 				dim_dcacheline.width +
+				dim_dcacheline_mem_region.width +
 				dim_dcacheline_node.width +
-				dim_dcacheline_count.width + 4);
+				dim_dcacheline_count.width + 6);
 	if (!buf)
 		return -ENOMEM;
 
@@ -3319,6 +3369,7 @@ static int perf_c2c__report(int argc, const char **argv)
 	if (c2c.display != DISPLAY_SNP_PEER)
 		output_str = "cl_idx,"
 			     "dcacheline,"
+			     "dcacheline_mem_region,"
 			     "dcacheline_node,"
 			     "dcacheline_count,"
 			     "percent_costly_snoop,"
@@ -3334,6 +3385,7 @@ static int perf_c2c__report(int argc, const char **argv)
 	else
 		output_str = "cl_idx,"
 			     "dcacheline,"
+			     "dcacheline_mem_region,"
 			     "dcacheline_node,"
 			     "dcacheline_count,"
 			     "percent_costly_snoop,"
Signature · 1 line
2.43.0
#11 lore raw

Re: [PATCH v2 3/6] perf tools: Show memory region in perf-c2c subcommand

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Arbitrary upper bound on UAPI memory region ID breaks forward compatibility for future memory…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Arbitrary upper bound on UAPI memory region ID breaks forward compatibility for future memory tiering topologies. --

--- Patch [3]: [PATCH v2 3/6] perf tools: Show memory region in perf-c2c subcommand --- commit 06c5065732519d0c60d1739bb8a421f9120486b9
Author: Dapeng Mi <[email protected]>

perf tools: Show memory region in perf-c2c subcommand

This commit adds a memory region field to the cacheline list view in perf-c2c. The new field helps identify the memory region to which the cacheline belongs.

Quoted 4 lines
tools/perf/builtin-c2c.c +0 -0
index c9584dbedf77a..cba2e9f21ae7c 100644

[ ... ]

tools/perf/builtin-c2c.c static void c2c_he__set_node(struct c2c_hist_entry *c2c_he, · line 290

Quoted 10 lines
tools/perf/builtin-c2c.c +6 -0
@@ -281,6 +282,18 @@ static void c2c_he__set_node(struct c2c_hist_entry *c2c_he,
 	}
 }
 
+static void c2c_he__set_mem_region(struct c2c_hist_entry *c2c_he,
+				   unsigned int mem_region)
+{
+	if (WARN_ONCE(mem_region > PERF_MEM_REGION_MEM7,
+		      "WARNING: invalid memory region ID\n"))
+		return;

[Severity: Medium]
Does this arbitrary upper bound break forward compatibility for future memory tiering topologies?

The UAPI field provides 5 bits, allowing for memory region IDs up to 31. By rejecting regions greater than PERF_MEM_REGION_MEM7, valid region IDs from future kernels or hardware architectures will be dropped and displayed as "N/A", while also triggering a spurious warning.

It looks like the new display logic in dcacheline_node_mem_region() can already mathematically scale to handle these higher values gracefully.

Quoted 6 lines
tools/perf/builtin-c2c.c +6 -0
+
+	/* Update mem_region only if it really accesses memory */
+	if (mem_region >= PERF_MEM_REGION_MMIO)
+		c2c_he->mem_region = mem_region;
+}
+
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#12 lore raw

Re: [PATCH v2 3/6] perf tools: Show memory region in perf-c2c subcommand

Quoted 52 lines · On Tue, 2026-07-14 at 00:56 +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Arbitrary upper bound on UAPI memory region ID breaks forward compatibility for future memory tiering topologies. --

--- Patch [3]: [PATCH v2 3/6] perf tools: Show memory region in perf- c2c subcommand ---
commit 06c5065732519d0c60d1739bb8a421f9120486b9
Author: Dapeng Mi <[email protected]>

perf tools: Show memory region in perf-c2c subcommand

This commit adds a memory region field to the cacheline list view in perf-c2c. The new field helps identify the memory region to which the cacheline belongs.

Link:
https://lore.kernel.org/all/[email protected]/

Quoted 4 lines
tools/perf/builtin-c2c.c +0 -0
index c9584dbedf77a..cba2e9f21ae7c 100644

[ ... ]

tools/perf/builtin-c2c.c static void c2c_he__set_node(struct c2c_hist_entry *c2c_he, · line 290

Quoted 11 lines
tools/perf/builtin-c2c.c +0 -0
@@ -281,6 +282,18 @@ static void c2c_he__set_node(struct
 c2c_hist_entry *c2c_he,
  	}
  }

tools/perf/builtin-c2c.c +6 -0
+static void c2c_he__set_mem_region(struct c2c_hist_entry *c2c_he,
+				   unsigned int mem_region)
+{
+	if (WARN_ONCE(mem_region > PERF_MEM_REGION_MEM7,
+		      "WARNING: invalid memory region ID\n"))
+		return;

[Severity: Medium]
Does this arbitrary upper bound break forward compatibility for future memory
tiering topologies?

The UAPI field provides 5 bits, allowing for memory region IDs up to
31. By
rejecting regions greater than PERF_MEM_REGION_MEM7, valid region IDs from
future kernels or hardware architectures will be dropped and displayed as
"N/A", while also triggering a spurious warning.

Quoted 4 lines

It looks like the new display logic in dcacheline_node_mem_region() can
already mathematically scale to handle these higher values gracefully.

Currently, only eight memory regions are available on supported architectures. These definitions can be expanded when future architectures that support more regions become available.

Thanks,
Tom

Quoted 8 lines
Quoted 7 lines
tools/perf/builtin-c2c.c +6 -0
+
+	/* Update mem_region only if it really accesses memory */
+	if (mem_region >= PERF_MEM_REGION_MMIO)
+		c2c_he->mem_region = mem_region;
+}
+
maintainer
#13 lore raw

Re: [PATCH v2 3/6] perf tools: Show memory region in perf-c2c subcommand

In reply to Thomas Falcon
Quoted 31 lines · Thomas Falcon

From: Dapeng Mi <[email protected]>

Add memory region field to the cacheline list view to help users identify the memory region to which the cacheline belongs. The memory region field was included with the introduction of support for the Off-module Response facility (OMR) [1] in Intel's Diamond Rapids and Nova Lake architectures.

An example of the new perf c2c output including the memory region field is shown below:

Shared Data Cache Line Table (112 entries, sorted on Total HITMs)

       --------------- Cacheline --------------      Tot  ------- Load Hitm -------    Total    Total    Total
Index             Address  Region  Node  PA cnt     Hitm    Total  LclHitm  RmtHitm  records    Loads   Stores
    0  0xff1b7032a1e8c5c0  N/A        1      47    2.25%       50       44        6     1024     1023        1
    1  0xff1b6ff3255bb880  N/A        0      11    1.62%       36       34        2       96       93        4
    2  0xff1b70328e3b9880  N/A        1      13    1.49%       33       33        0      100       91        9
    3  0xff1b70328b023800  N/A        1       1    1.40%       31       14       17       52       48        4
    4  0xff1b70328e3b9c00  N/A        1       1    1.31%       29       27        2       67       33       34
    5  0xff1b70328b0237c0  N/A        1       1    1.26%       28       10       18      154      150        4
    6  0xff1b6ff3255bbc00  N/A        0       1    1.13%       25       25        0       48       25       23
    7  0xff1b6ff3255bba40  N/A        0       1    0.99%       22       22        0       46       23       23
    8  0xff3ab9ba50255c80  N/A      N/A       0    0.77%       17       15        2       35       35        0
    9  0xff3ab9ba503e3040  N/A      N/A       0    0.77%       17       11        6       37       37        1
   10  0xff1b703289e88f40  N/A        1      33    0.72%       16        9        7       69       63        6
   11  0xff1b70328e3b9a40  N/A        1       1    0.68%       15       15        0       43       17       26
   12  0xff1b7032c9fd6a40  N/A        1      15    0.68%       15       15        0       57       54        4
   13  0xff1b7032a1e8c980  N/A        1      27    0.54%       12       11        1      761      761        0
   14  0xff1b70727ffd57c0  N/A        1       7    0.54%       12       12        0      219      218        1
   15  0xffffffffaefe2380  N/A        1       1    0.50%       11        8        3       14       14        0
Quoted 2 lines

Note: DMR simics does not support memory regions. Since the output is captured on SPR, the memory region field shows "N/A" for all cachelines.

My concern is that it increase the length of data and makes it harder to read. Can it be conditional on the feature bit?

Also I'm curious if a cacheline can belong to different regions. In that case, will it have multiple entries in the output?

Thanks,
Namhyung

Quoted 168 lines

[1]: https://lore.kernel.org/all/[email protected]/

Assisted-by Sashiko:gemini-3.1-pro-preview

---
Changes in v2:
-- Added missing new line in WARN_ONCE message in c2c_he__set_mem_region()

---
 tools/perf/builtin-c2c.c | 56 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 54 insertions(+), 2 deletions(-)
tools/perf/builtin-c2c.c +54 -2
index c9584dbedf77..cba2e9f21ae7 100644
@@ -74,6 +74,7 @@ struct c2c_hist_entry {
 	unsigned long		*nodeset;
 	struct c2c_stats	*node_stats;
 	unsigned int		 cacheline_idx;
+	unsigned int		 mem_region;
 
 	struct compute_stats	 cstats;
 
@@ -281,6 +282,18 @@ static void c2c_he__set_node(struct c2c_hist_entry *c2c_he,
 	}
 }
 
+static void c2c_he__set_mem_region(struct c2c_hist_entry *c2c_he,
+				   unsigned int mem_region)
+{
+	if (WARN_ONCE(mem_region > PERF_MEM_REGION_MEM7,
+		      "WARNING: invalid memory region ID\n"))
+		return;
+
+	/* Update mem_region only if it really accesses memory */
+	if (mem_region >= PERF_MEM_REGION_MMIO)
+		c2c_he->mem_region = mem_region;
+}
+
 static void compute_stats(struct c2c_hist_entry *c2c_he,
 			  struct c2c_stats *stats,
 			  u64 weight)
@@ -339,6 +352,7 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused,
 	struct addr_location al;
 	struct mem_info *mi = NULL;
 	struct callchain_cursor *cursor;
+	unsigned int mem_region;
 	int ret;
 
 	addr_location__init(&al);
@@ -366,6 +380,7 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused,
 	}
 
 	c2c_decode_stats(&stats, mi);
+	mem_region = mem_info__data_src(mi)->mem_region;
 
 	he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
 				  &al, NULL, NULL, mi, NULL,
@@ -382,6 +397,7 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused,
 	c2c_he__set_cpu(c2c_he, sample);
 	c2c_he__set_node(c2c_he, sample);
 	c2c_he__set_evsel(c2c_he, evsel);
+	c2c_he__set_mem_region(c2c_he, mem_region);
 
 	hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
 
@@ -435,6 +451,7 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused,
 		c2c_he__set_cpu(c2c_he, sample);
 		c2c_he__set_node(c2c_he, sample);
 		c2c_he__set_evsel(c2c_he, evsel);
+		c2c_he__set_mem_region(c2c_he, mem_region);
 
 		hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
 		ret = hist_entry__append_callchain(he, sample);
@@ -603,6 +620,29 @@ dcacheline_node_count(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
 	return scnprintf(hpp->buf, hpp->size, "%*lu", width, c2c_he->paddr_cnt);
 }
 
+static int
+dcacheline_node_mem_region(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
+			   struct hist_entry *he)
+{
+	int width = c2c_width(fmt, hpp, he->hists);
+	struct c2c_hist_entry *c2c_he;
+	unsigned int mem_region;
+	char buf[20];
+
+	c2c_he = container_of(he, struct c2c_hist_entry, he);
+	mem_region = c2c_he->mem_region;
+
+	if (mem_region == PERF_MEM_REGION_NA)
+		scnprintf(buf, sizeof(buf),  "N/A");
+	/* mem_region could only be >= PERF_MEM_REGION_MMIO */
+	else if (mem_region == PERF_MEM_REGION_MMIO)
+		scnprintf(buf, sizeof(buf), "MMIO");
+	else
+		scnprintf(buf, sizeof(buf), "0x%x", mem_region - 0x8);
+
+	return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
+}
+
 static int offset_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
 			struct hist_entry *he)
 {
@@ -1425,7 +1465,7 @@ cl_idx_empty_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
 	}
 
 static struct c2c_dimension dim_dcacheline = {
-	.header		= HEADER_SPAN("--- Cacheline ----", "Address", 2),
+	.header		= HEADER_SPAN("--- Cacheline ----", "Address", 3),
 	.name		= "dcacheline",
 	.cmp		= dcacheline_cmp,
 	.entry		= dcacheline_entry,
@@ -1440,6 +1480,14 @@ static struct c2c_dimension dim_dcacheline_node = {
 	.width		= 4,
 };
 
+static struct c2c_dimension dim_dcacheline_mem_region = {
+	.header		= HEADER_LOW("Region"),
+	.name		= "dcacheline_mem_region",
+	.cmp		= empty_cmp,
+	.entry		= dcacheline_node_mem_region,
+	.width		= 6,
+};
+
 static struct c2c_dimension dim_dcacheline_count = {
 	.header		= HEADER_LOW("PA cnt"),
 	.name		= "dcacheline_count",
@@ -1871,6 +1919,7 @@ static struct c2c_dimension dim_dcacheline_num_empty = {
 
 static struct c2c_dimension *dimensions[] = {
 	&dim_dcacheline,
+	&dim_dcacheline_mem_region,
 	&dim_dcacheline_node,
 	&dim_dcacheline_count,
 	&dim_offset,
@@ -2898,8 +2947,9 @@ static int ui_quirks(void)
 	/* Fix the zero line for dcacheline column. */
 	buf = fill_line(chk_double_cl ? "Double-Cacheline" : "Cacheline",
 				dim_dcacheline.width +
+				dim_dcacheline_mem_region.width +
 				dim_dcacheline_node.width +
-				dim_dcacheline_count.width + 4);
+				dim_dcacheline_count.width + 6);
 	if (!buf)
 		return -ENOMEM;
 
@@ -3319,6 +3369,7 @@ static int perf_c2c__report(int argc, const char **argv)
 	if (c2c.display != DISPLAY_SNP_PEER)
 		output_str = "cl_idx,"
 			     "dcacheline,"
+			     "dcacheline_mem_region,"
 			     "dcacheline_node,"
 			     "dcacheline_count,"
 			     "percent_costly_snoop,"
@@ -3334,6 +3385,7 @@ static int perf_c2c__report(int argc, const char **argv)
 	else
 		output_str = "cl_idx,"
 			     "dcacheline,"
+			     "dcacheline_mem_region,"
 			     "dcacheline_node,"
 			     "dcacheline_count,"
 			     "percent_costly_snoop,"
Signature · 1 line
2.43.0
#14 lore raw

Re: [PATCH v2 3/6] perf tools: Show memory region in perf-c2c subcommand

Quoted 32 lines · Namhyung Kim
Quoted 31 lines · Thomas Falcon

From: Dapeng Mi <[email protected]>

Add memory region field to the cacheline list view to help users identify the memory region to which the cacheline belongs. The memory region field was included with the introduction of support for the Off-module Response facility (OMR) [1] in Intel's Diamond Rapids and Nova Lake architectures.

An example of the new perf c2c output including the memory region field is shown below:

Shared Data Cache Line Table (112 entries, sorted on Total HITMs)

       --------------- Cacheline --------------      Tot  ------- Load Hitm -------    Total    Total    Total
Index             Address  Region  Node  PA cnt     Hitm    Total  LclHitm  RmtHitm  records    Loads   Stores
    0  0xff1b7032a1e8c5c0  N/A        1      47    2.25%       50       44        6     1024     1023        1
    1  0xff1b6ff3255bb880  N/A        0      11    1.62%       36       34        2       96       93        4
    2  0xff1b70328e3b9880  N/A        1      13    1.49%       33       33        0      100       91        9
    3  0xff1b70328b023800  N/A        1       1    1.40%       31       14       17       52       48        4
    4  0xff1b70328e3b9c00  N/A        1       1    1.31%       29       27        2       67       33       34
    5  0xff1b70328b0237c0  N/A        1       1    1.26%       28       10       18      154      150        4
    6  0xff1b6ff3255bbc00  N/A        0       1    1.13%       25       25        0       48       25       23
    7  0xff1b6ff3255bba40  N/A        0       1    0.99%       22       22        0       46       23       23
    8  0xff3ab9ba50255c80  N/A      N/A       0    0.77%       17       15        2       35       35        0
    9  0xff3ab9ba503e3040  N/A      N/A       0    0.77%       17       11        6       37       37        1
   10  0xff1b703289e88f40  N/A        1      33    0.72%       16        9        7       69       63        6
   11  0xff1b70328e3b9a40  N/A        1       1    0.68%       15       15        0       43       17       26
   12  0xff1b7032c9fd6a40  N/A        1      15    0.68%       15       15        0       57       54        4
   13  0xff1b7032a1e8c980  N/A        1      27    0.54%       12       11        1      761      761        0
   14  0xff1b70727ffd57c0  N/A        1       7    0.54%       12       12        0      219      218        1
   15  0xffffffffaefe2380  N/A        1       1    0.50%       11        8        3       14       14        0
Quoted 4 lines
Quoted 2 lines

Note: DMR simics does not support memory regions. Since the output is captured on SPR, the memory region field shows "N/A" for all cachelines.

My concern is that it increase the length of data and makes it harder to read. Can it be conditional on the feature bit?

Sure.

Quoted 3 lines

Also I'm curious if a cacheline can belong to different regions. In that case, will it have multiple entries in the output?

Suppose it's possible, let's double check.

Quoted 171 lines

Thanks,
Namhyung

Quoted 167 lines

[1]: https://lore.kernel.org/all/[email protected]/

Assisted-by Sashiko:gemini-3.1-pro-preview

---
Changes in v2:
-- Added missing new line in WARN_ONCE message in c2c_he__set_mem_region()

---
 tools/perf/builtin-c2c.c | 56 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 54 insertions(+), 2 deletions(-)
tools/perf/builtin-c2c.c +54 -2
index c9584dbedf77..cba2e9f21ae7 100644
@@ -74,6 +74,7 @@ struct c2c_hist_entry {
 	unsigned long		*nodeset;
 	struct c2c_stats	*node_stats;
 	unsigned int		 cacheline_idx;
+	unsigned int		 mem_region;
 
 	struct compute_stats	 cstats;
 
@@ -281,6 +282,18 @@ static void c2c_he__set_node(struct c2c_hist_entry *c2c_he,
 	}
 }
 
+static void c2c_he__set_mem_region(struct c2c_hist_entry *c2c_he,
+				   unsigned int mem_region)
+{
+	if (WARN_ONCE(mem_region > PERF_MEM_REGION_MEM7,
+		      "WARNING: invalid memory region ID\n"))
+		return;
+
+	/* Update mem_region only if it really accesses memory */
+	if (mem_region >= PERF_MEM_REGION_MMIO)
+		c2c_he->mem_region = mem_region;
+}
+
 static void compute_stats(struct c2c_hist_entry *c2c_he,
 			  struct c2c_stats *stats,
 			  u64 weight)
@@ -339,6 +352,7 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused,
 	struct addr_location al;
 	struct mem_info *mi = NULL;
 	struct callchain_cursor *cursor;
+	unsigned int mem_region;
 	int ret;
 
 	addr_location__init(&al);
@@ -366,6 +380,7 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused,
 	}
 
 	c2c_decode_stats(&stats, mi);
+	mem_region = mem_info__data_src(mi)->mem_region;
 
 	he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
 				  &al, NULL, NULL, mi, NULL,
@@ -382,6 +397,7 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused,
 	c2c_he__set_cpu(c2c_he, sample);
 	c2c_he__set_node(c2c_he, sample);
 	c2c_he__set_evsel(c2c_he, evsel);
+	c2c_he__set_mem_region(c2c_he, mem_region);
 
 	hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
 
@@ -435,6 +451,7 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused,
 		c2c_he__set_cpu(c2c_he, sample);
 		c2c_he__set_node(c2c_he, sample);
 		c2c_he__set_evsel(c2c_he, evsel);
+		c2c_he__set_mem_region(c2c_he, mem_region);
 
 		hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
 		ret = hist_entry__append_callchain(he, sample);
@@ -603,6 +620,29 @@ dcacheline_node_count(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
 	return scnprintf(hpp->buf, hpp->size, "%*lu", width, c2c_he->paddr_cnt);
 }
 
+static int
+dcacheline_node_mem_region(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
+			   struct hist_entry *he)
+{
+	int width = c2c_width(fmt, hpp, he->hists);
+	struct c2c_hist_entry *c2c_he;
+	unsigned int mem_region;
+	char buf[20];
+
+	c2c_he = container_of(he, struct c2c_hist_entry, he);
+	mem_region = c2c_he->mem_region;
+
+	if (mem_region == PERF_MEM_REGION_NA)
+		scnprintf(buf, sizeof(buf),  "N/A");
+	/* mem_region could only be >= PERF_MEM_REGION_MMIO */
+	else if (mem_region == PERF_MEM_REGION_MMIO)
+		scnprintf(buf, sizeof(buf), "MMIO");
+	else
+		scnprintf(buf, sizeof(buf), "0x%x", mem_region - 0x8);
+
+	return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
+}
+
 static int offset_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
 			struct hist_entry *he)
 {
@@ -1425,7 +1465,7 @@ cl_idx_empty_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
 	}
 
 static struct c2c_dimension dim_dcacheline = {
-	.header		= HEADER_SPAN("--- Cacheline ----", "Address", 2),
+	.header		= HEADER_SPAN("--- Cacheline ----", "Address", 3),
 	.name		= "dcacheline",
 	.cmp		= dcacheline_cmp,
 	.entry		= dcacheline_entry,
@@ -1440,6 +1480,14 @@ static struct c2c_dimension dim_dcacheline_node = {
 	.width		= 4,
 };
 
+static struct c2c_dimension dim_dcacheline_mem_region = {
+	.header		= HEADER_LOW("Region"),
+	.name		= "dcacheline_mem_region",
+	.cmp		= empty_cmp,
+	.entry		= dcacheline_node_mem_region,
+	.width		= 6,
+};
+
 static struct c2c_dimension dim_dcacheline_count = {
 	.header		= HEADER_LOW("PA cnt"),
 	.name		= "dcacheline_count",
@@ -1871,6 +1919,7 @@ static struct c2c_dimension dim_dcacheline_num_empty = {
 
 static struct c2c_dimension *dimensions[] = {
 	&dim_dcacheline,
+	&dim_dcacheline_mem_region,
 	&dim_dcacheline_node,
 	&dim_dcacheline_count,
 	&dim_offset,
@@ -2898,8 +2947,9 @@ static int ui_quirks(void)
 	/* Fix the zero line for dcacheline column. */
 	buf = fill_line(chk_double_cl ? "Double-Cacheline" : "Cacheline",
 				dim_dcacheline.width +
+				dim_dcacheline_mem_region.width +
 				dim_dcacheline_node.width +
-				dim_dcacheline_count.width + 4);
+				dim_dcacheline_count.width + 6);
 	if (!buf)
 		return -ENOMEM;
 
@@ -3319,6 +3369,7 @@ static int perf_c2c__report(int argc, const char **argv)
 	if (c2c.display != DISPLAY_SNP_PEER)
 		output_str = "cl_idx,"
 			     "dcacheline,"
+			     "dcacheline_mem_region,"
 			     "dcacheline_node,"
 			     "dcacheline_count,"
 			     "percent_costly_snoop,"
@@ -3334,6 +3385,7 @@ static int perf_c2c__report(int argc, const char **argv)
 	else
 		output_str = "cl_idx,"
 			     "dcacheline,"
+			     "dcacheline_mem_region,"
 			     "dcacheline_node,"
 			     "dcacheline_count,"
 			     "percent_costly_snoop,"
Signature · 1 line
2.43.0
#15 lore raw

PATCH v2 4/6 perf tools: Show memory region in perf-script subcommand

In reply to Thomas Falcon

From: Dapeng Mi <[email protected]>

Show the memory region in perf-script subcommand. Memory region is found in the mem_region field of the memory information data source. This field was included with the introduction of support for the Off-module Response facility (OMR) [1] in Intel's Diamond Rapids and Nova Lake Architectures.

An example of perf-script output with the new memory region field is shown below:

random      10e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  N/A|Region  N/A                           7f32ae64a2ec
random    2411a68201042 |OP LOAD|LVL RAM hit|SNP Hit|TLB L1 or L2 hit|LCK No|BLK  N/A|Region  Mem-1                          561de7f8910a
random      10e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  N/A|Region  N/A                           7f32ae64a2d0
random      20e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  Data|Region  N/A                          7f32ae64a2d0
random      20e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  Data|Region  N/A                          7f32ae64a2d0
random      10e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  N/A|Region  N/A                           7f32ae64a2ec
random      10e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  N/A|Region  N/A                           7f32ae64a2d0
random      10e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  N/A|Region  N/A                           7f32ae64a2d0
random    2411a68201042 |OP LOAD|LVL RAM hit|SNP Hit|TLB L1 or L2 hit|LCK No|BLK  N/A|Region  Mem-1                          561de7f8910a
random      10e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  N/A|Region  N/A                           7f32ae64a2ec
random      10e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  N/A|Region  N/A                           7f32ae64a2d0

[1]: https://lore.kernel.org/all/[email protected]/

---
Changes in v2:
-- increased buffers passed to perf_script__meminfo_scnprintf() from 150
to 200 bytes to avoid silent truncation

---
 tools/perf/builtin-script.c                   |  8 +--
 tools/perf/util/mem-events.c                  | 68 +++++++++++++++++++
 .../scripting-engines/trace-event-python.c    |  4 +-
 3 files changed, 74 insertions(+), 6 deletions(-)
tools/perf/builtin-script.c +4 -4
index f91d8b1fbd01..ba8ec0093f4f 100644
@@ -2081,8 +2081,8 @@ static int evlist__max_name_len(struct evlist *evlist)
 static int data_src__fprintf(u64 data_src, FILE *fp)
 {
 	struct mem_info *mi = mem_info__new();
-	char decode[100];
-	char out[100];
+	char decode[200];
+	char out[200];
 	static int maxlen;
 	int len;
 
@@ -2090,10 +2090,10 @@ static int data_src__fprintf(u64 data_src, FILE *fp)
 		return -ENOMEM;
 
 	mem_info__data_src(mi)->val = data_src;
-	perf_script__meminfo_scnprintf(decode, 100, mi);
+	perf_script__meminfo_scnprintf(decode, 200, mi);
 	mem_info__put(mi);
 
-	len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
+	len = scnprintf(out, 200, "%16" PRIx64 " %s", data_src, decode);
 	if (maxlen < len)
 		maxlen = len;
 
tools/perf/util/mem-events.c +68 -0

80 lines not on this page · raw message

tools/perf/util/scripting-engines/trace-event-python.c +2 -2
index 8f832ae316ca..57ec259897e4 100644
@@ -695,7 +695,7 @@ static void set_sample_datasrc_in_dict(PyObject *dict,
 				      struct perf_sample *sample)
 {
 	struct mem_info *mi = mem_info__new();
-	char decode[100];
+	char decode[200];
 
 	if (!mi)
 		Py_FatalError("couldn't create mem-info");
@@ -704,7 +704,7 @@ static void set_sample_datasrc_in_dict(PyObject *dict,
 			PyLong_FromUnsignedLongLong(sample->data_src));
 
 	mem_info__data_src(mi)->val = sample->data_src;
-	perf_script__meminfo_scnprintf(decode, 100, mi);
+	perf_script__meminfo_scnprintf(decode, 200, mi);
 	mem_info__put(mi);
 
 	pydict_set_item_string_decref(dict, "datasrc_decode",
Signature · 1 line
2.43.0
#16 lore raw

Re: [PATCH v2 4/6] perf tools: Show memory region in perf-script subcommand

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [Medium] Silent truncation in data_src__fprintf due to inadequately sized destination buffer.

Pre-existing issues:
- [High] Stack buffer overflow in perf_script__meminfo_scnprintf and its helpers due to unsafe string operations and integer underflow.
- [High] Out-of-bounds array read in perf_mem__lvl_scnprintf when accessing mem_hops without bounds checking. --

--- Patch [4]: [PATCH v2 4/6] perf tools: Show memory region in perf-script subcommand --- commit 032c0a587669b230205736e968c969992f006ba6
Author: Dapeng Mi <[email protected]>

perf tools: Show memory region in perf-script subcommand

This commit adds the memory region to the output of the perf-script subcommand. The memory region is extracted from the mem_region field of the memory information data source.

Quoted 16 lines
tools/perf/builtin-script.c +2 -2
index 9ac29bdc3cd54..58aaf1dbf6eda 100644
@@ -2081,8 +2081,8 @@ static int evlist__max_name_len(struct evlist *evlist)
 static int data_src__fprintf(u64 data_src, FILE *fp)
 {
 	struct mem_info *mi = mem_info__new();
-	char decode[100];
-	char out[100];
+	char decode[200];
+	char out[200];
 	static int maxlen;
 	int len;
 

tools/perf/builtin-script.c static int data_src__fprintf(u64 data_src, FILE *fp) · line 2096

Quoted 9 lines
tools/perf/builtin-script.c +2 -2
@@ -2090,10 +2090,10 @@ static int data_src__fprintf(u64 data_src, FILE *fp)
 		return -ENOMEM;
 
 	mem_info__data_src(mi)->val = data_src;
-	perf_script__meminfo_scnprintf(decode, 100, mi);
+	perf_script__meminfo_scnprintf(decode, 200, mi);
 	mem_info__put(mi);
 
-	len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
+	len = scnprintf(out, 200, "%16" PRIx64 " %s", data_src, decode);

[Severity: Medium]
Is the out buffer sized correctly to prevent truncation?

The commit message mentions increasing the buffers to avoid silent truncation, but the out buffer is exactly the same size as the decode buffer. Since scnprintf() adds a 17-character prefix before appending the decode string, won't the output still be truncated if the decode string utilizes its full capacity?

Quoted 2 lines
tools/perf/builtin-script.c +0 -0
 	if (maxlen < len)
 		maxlen = len;

[ ... ]

Quoted 5 lines
tools/perf/util/mem-events.c +0 -0
index 4fd48fd200552..ff08f1fee0e33 100644

tools/perf/util/mem-events.c int perf_mem__blk_scnprintf(char *out, size_t sz, const struct mem_info *mem_inf · line 613

Quoted 10 lines
tools/perf/util/mem-events.c +7 -0
@@ -604,6 +604,72 @@ int perf_mem__blk_scnprintf(char *out, size_t sz, const struct mem_info *mem_inf
 	return l;
 }
 
+static int perf_mem__region_scnprintf(char *out, size_t sz, const struct mem_info *mem_info)
+{
+	size_t l = 0;
+	u64 mem = PERF_MEM_REGION_NA;
+
+	sz -= 1; /* -1 for null termination */
+	out[0] = '\0';

[Severity: High]
This isn't a bug introduced by this patch, but could this write out of bounds if sz is 0?

In perf_script__meminfo_scnprintf(), earlier calls like perf_mem__lvl_scnprintf() can write past the sz limit because they use strcat(out, " or ") without checking the boundary, and they return lengths larger than the remaining size.

This allows the accumulated index i to exceed sz. When perf_mem__region_scnprintf() is eventually called, sz - i can underflow or evaluate to 0.

If sz is 0, sz -= 1 will underflow, and out[0] = '\0' will write a null byte out of bounds relative to the 200-byte stack buffer. Can a crafted perf.data file trigger a stack overflow this way?

Quoted 5 lines
tools/perf/util/mem-events.c +5 -0
+
+	if (mem_info)
+		mem = mem_info__const_data_src(mem_info)->mem_region;
+
+	switch (mem) {

[ ... ]

tools/perf/util/mem-events.c int perf_script__meminfo_scnprintf(char *out, size_t sz, const struct mem_info * · line 693

Quoted 9 lines
tools/perf/util/mem-events.c +2 -0
@@ -620,6 +686,8 @@ int perf_script__meminfo_scnprintf(char *out, size_t sz, const struct mem_info *
 	i += perf_mem__lck_scnprintf(out + i, sz - i, mem_info);
 	i += scnprintf(out + i, sz - i, "|BLK ");
 	i += perf_mem__blk_scnprintf(out + i, sz - i, mem_info);
+	i += scnprintf(out + i, sz - i, "|Region ");
+	i += perf_mem__region_scnprintf(out + i, sz - i, mem_info);
 
 	return i;
 }

[Severity: High]
This is a pre-existing issue, but I noticed a potential out-of-bounds read in perf_mem__lvl_scnprintf() which is called earlier in this function.

The mem_hops string array is defined with 5 elements. However, data_src.mem_hops is a 3-bit bitfield, meaning it can take integer values from 0 to 7.

If a crafted perf.data file sets data_src.mem_hops to 5, 6, or 7, does this cause an out-of-bounds pointer read when accessing mem_hops[data_src.mem_hops]?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
maintainer
#17 lore raw

Re: [PATCH v2 4/6] perf tools: Show memory region in perf-script subcommand

In reply to Thomas Falcon
Quoted 143 lines · Thomas Falcon

From: Dapeng Mi <[email protected]>

Show the memory region in perf-script subcommand. Memory region is found in the mem_region field of the memory information data source. This field was included with the introduction of support for the Off-module Response facility (OMR) [1] in Intel's Diamond Rapids and Nova Lake Architectures.

An example of perf-script output with the new memory region field is shown below:

random      10e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  N/A|Region  N/A                           7f32ae64a2ec
random    2411a68201042 |OP LOAD|LVL RAM hit|SNP Hit|TLB L1 or L2 hit|LCK No|BLK  N/A|Region  Mem-1                          561de7f8910a
random      10e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  N/A|Region  N/A                           7f32ae64a2d0
random      20e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  Data|Region  N/A                          7f32ae64a2d0
random      20e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  Data|Region  N/A                          7f32ae64a2d0
random      10e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  N/A|Region  N/A                           7f32ae64a2ec
random      10e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  N/A|Region  N/A                           7f32ae64a2d0
random      10e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  N/A|Region  N/A                           7f32ae64a2d0
random    2411a68201042 |OP LOAD|LVL RAM hit|SNP Hit|TLB L1 or L2 hit|LCK No|BLK  N/A|Region  Mem-1                          561de7f8910a
random      10e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  N/A|Region  N/A                           7f32ae64a2ec
random      10e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  N/A|Region  N/A                           7f32ae64a2d0

[1]: https://lore.kernel.org/all/[email protected]/

---
Changes in v2:
-- increased buffers passed to perf_script__meminfo_scnprintf() from 150
to 200 bytes to avoid silent truncation

---
 tools/perf/builtin-script.c                   |  8 +--
 tools/perf/util/mem-events.c                  | 68 +++++++++++++++++++
 .../scripting-engines/trace-event-python.c    |  4 +-
 3 files changed, 74 insertions(+), 6 deletions(-)
tools/perf/builtin-script.c +4 -4
index f91d8b1fbd01..ba8ec0093f4f 100644
@@ -2081,8 +2081,8 @@ static int evlist__max_name_len(struct evlist *evlist)
 static int data_src__fprintf(u64 data_src, FILE *fp)
 {
 	struct mem_info *mi = mem_info__new();
-	char decode[100];
-	char out[100];
+	char decode[200];
+	char out[200];
 	static int maxlen;
 	int len;
 
@@ -2090,10 +2090,10 @@ static int data_src__fprintf(u64 data_src, FILE *fp)
 		return -ENOMEM;
 
 	mem_info__data_src(mi)->val = data_src;
-	perf_script__meminfo_scnprintf(decode, 100, mi);
+	perf_script__meminfo_scnprintf(decode, 200, mi);
 	mem_info__put(mi);
 
-	len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
+	len = scnprintf(out, 200, "%16" PRIx64 " %s", data_src, decode);
 	if (maxlen < len)
 		maxlen = len;
 
tools/perf/util/mem-events.c +66 -0
index 4fd48fd20055..ff08f1fee0e3 100644
@@ -604,6 +604,72 @@ int perf_mem__blk_scnprintf(char *out, size_t sz, const struct mem_info *mem_inf
 	return l;
 }
 
+static int perf_mem__region_scnprintf(char *out, size_t sz, const struct mem_info *mem_info)
+{
+	size_t l = 0;
+	u64 mem = PERF_MEM_REGION_NA;
+
+	sz -= 1; /* -1 for null termination */
+	out[0] = '\0';
+
+	if (mem_info)
+		mem = mem_info__const_data_src(mem_info)->mem_region;
+
+	switch (mem) {
+	case PERF_MEM_REGION_NA:
+	case PERF_MEM_REGION_RSVD:
+		l += scnprintf(out + l, sz - l, " N/A");
+		break;
+	case PERF_MEM_REGION_L_SHARE:
+		l += scnprintf(out + l, sz - l, " Local-shared-cache");
+		break;
+	case PERF_MEM_REGION_L_NON_SHARE:
+		l += scnprintf(out + l, sz - l, " Local-non-shared-cache");
+		break;
+	case PERF_MEM_REGION_O_IO:
+		l += scnprintf(out + l, sz - l, " Other-IO");
+		break;
+	case PERF_MEM_REGION_O_SHARE:
+		l += scnprintf(out + l, sz - l, " Other-shared-cache");
+		break;
+	case PERF_MEM_REGION_O_NON_SHARE:
+		l += scnprintf(out + l, sz - l, " Other-non-shared-cache");
+		break;
+	case PERF_MEM_REGION_MMIO:
+		l += scnprintf(out + l, sz - l, " MMIO");
+		break;
+	case PERF_MEM_REGION_MEM0:
+		l += scnprintf(out + l, sz - l, " Mem-0");
+		break;
+	case PERF_MEM_REGION_MEM1:
+		l += scnprintf(out + l, sz - l, " Mem-1");
+		break;
+	case PERF_MEM_REGION_MEM2:
+		l += scnprintf(out + l, sz - l, " Mem-2");
+		break;
+	case PERF_MEM_REGION_MEM3:
+		l += scnprintf(out + l, sz - l, " Mem-3");
+		break;
+	case PERF_MEM_REGION_MEM4:
+		l += scnprintf(out + l, sz - l, " Mem-4");
+		break;
+	case PERF_MEM_REGION_MEM5:
+		l += scnprintf(out + l, sz - l, " Mem-5");
+		break;
+	case PERF_MEM_REGION_MEM6:
+		l += scnprintf(out + l, sz - l, " Mem-6");
+		break;
+	case PERF_MEM_REGION_MEM7:
+		l += scnprintf(out + l, sz - l, " Mem-7");
+		break;
+	default:
+		l += scnprintf(out + l, sz - l, " N/A");
+		break;
+	}
+
+	return l;
+}
+
 int perf_script__meminfo_scnprintf(char *out, size_t sz, const struct mem_info *mem_info)
 {
 	int i = 0;

tools/perf/util/mem-events.c int perf_script__meminfo_scnprintf(char *out, size_t sz, const struct mem_info * · line 690

Quoted 5 lines
tools/perf/util/mem-events.c +2 -0
@@ -620,6 +686,8 @@ int perf_script__meminfo_scnprintf(char *out, size_t sz, const struct mem_info *
 	i += perf_mem__lck_scnprintf(out + i, sz - i, mem_info);
 	i += scnprintf(out + i, sz - i, "|BLK ");
 	i += perf_mem__blk_scnprintf(out + i, sz - i, mem_info);
+	i += scnprintf(out + i, sz - i, "|Region ");
+	i += perf_mem__region_scnprintf(out + i, sz - i, mem_info);

Same thing. Would be nice if it can be conditional.

Thanks,
Namhyung

Quoted 28 lines
tools/perf/util/mem-events.c +0 -0
 	return i;
 }
tools/perf/util/scripting-engines/trace-event-python.c +2 -2
index 8f832ae316ca..57ec259897e4 100644
@@ -695,7 +695,7 @@ static void set_sample_datasrc_in_dict(PyObject *dict,
 				      struct perf_sample *sample)
 {
 	struct mem_info *mi = mem_info__new();
-	char decode[100];
+	char decode[200];
 
 	if (!mi)
 		Py_FatalError("couldn't create mem-info");
@@ -704,7 +704,7 @@ static void set_sample_datasrc_in_dict(PyObject *dict,
 			PyLong_FromUnsignedLongLong(sample->data_src));
 
 	mem_info__data_src(mi)->val = sample->data_src;
-	perf_script__meminfo_scnprintf(decode, 100, mi);
+	perf_script__meminfo_scnprintf(decode, 200, mi);
 	mem_info__put(mi);
 
 	pydict_set_item_string_decref(dict, "datasrc_decode",
Signature · 1 line
2.43.0
#18 lore raw

Re: [PATCH v2 4/6] perf tools: Show memory region in perf-script subcommand

Quoted 142 lines · Namhyung Kim
Quoted 141 lines · Thomas Falcon

From: Dapeng Mi <[email protected]>

Show the memory region in perf-script subcommand. Memory region is found in the mem_region field of the memory information data source. This field was included with the introduction of support for the Off-module Response facility (OMR) [1] in Intel's Diamond Rapids and Nova Lake Architectures.

An example of perf-script output with the new memory region field is shown below:

random      10e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  N/A|Region  N/A                           7f32ae64a2ec
random    2411a68201042 |OP LOAD|LVL RAM hit|SNP Hit|TLB L1 or L2 hit|LCK No|BLK  N/A|Region  Mem-1                          561de7f8910a
random      10e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  N/A|Region  N/A                           7f32ae64a2d0
random      20e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  Data|Region  N/A                          7f32ae64a2d0
random      20e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  Data|Region  N/A                          7f32ae64a2d0
random      10e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  N/A|Region  N/A                           7f32ae64a2ec
random      10e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  N/A|Region  N/A                           7f32ae64a2d0
random      10e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  N/A|Region  N/A                           7f32ae64a2d0
random    2411a68201042 |OP LOAD|LVL RAM hit|SNP Hit|TLB L1 or L2 hit|LCK No|BLK  N/A|Region  Mem-1                          561de7f8910a
random      10e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  N/A|Region  N/A                           7f32ae64a2ec
random      10e6a100042 |OP LOAD|LVL L0 hit|SNP None|TLB L1 or L2 hit|LCK Yes|BLK  N/A|Region  N/A                           7f32ae64a2d0

[1]: https://lore.kernel.org/all/[email protected]/

---
Changes in v2:
-- increased buffers passed to perf_script__meminfo_scnprintf() from 150
to 200 bytes to avoid silent truncation

---
 tools/perf/builtin-script.c                   |  8 +--
 tools/perf/util/mem-events.c                  | 68 +++++++++++++++++++
 .../scripting-engines/trace-event-python.c    |  4 +-
 3 files changed, 74 insertions(+), 6 deletions(-)
tools/perf/builtin-script.c +4 -4
index f91d8b1fbd01..ba8ec0093f4f 100644
@@ -2081,8 +2081,8 @@ static int evlist__max_name_len(struct evlist *evlist)
 static int data_src__fprintf(u64 data_src, FILE *fp)
 {
 	struct mem_info *mi = mem_info__new();
-	char decode[100];
-	char out[100];
+	char decode[200];
+	char out[200];
 	static int maxlen;
 	int len;
 
@@ -2090,10 +2090,10 @@ static int data_src__fprintf(u64 data_src, FILE *fp)
 		return -ENOMEM;
 
 	mem_info__data_src(mi)->val = data_src;
-	perf_script__meminfo_scnprintf(decode, 100, mi);
+	perf_script__meminfo_scnprintf(decode, 200, mi);
 	mem_info__put(mi);
 
-	len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
+	len = scnprintf(out, 200, "%16" PRIx64 " %s", data_src, decode);
 	if (maxlen < len)
 		maxlen = len;
 
tools/perf/util/mem-events.c +66 -0
index 4fd48fd20055..ff08f1fee0e3 100644
@@ -604,6 +604,72 @@ int perf_mem__blk_scnprintf(char *out, size_t sz, const struct mem_info *mem_inf
 	return l;
 }
 
+static int perf_mem__region_scnprintf(char *out, size_t sz, const struct mem_info *mem_info)
+{
+	size_t l = 0;
+	u64 mem = PERF_MEM_REGION_NA;
+
+	sz -= 1; /* -1 for null termination */
+	out[0] = '\0';
+
+	if (mem_info)
+		mem = mem_info__const_data_src(mem_info)->mem_region;
+
+	switch (mem) {
+	case PERF_MEM_REGION_NA:
+	case PERF_MEM_REGION_RSVD:
+		l += scnprintf(out + l, sz - l, " N/A");
+		break;
+	case PERF_MEM_REGION_L_SHARE:
+		l += scnprintf(out + l, sz - l, " Local-shared-cache");
+		break;
+	case PERF_MEM_REGION_L_NON_SHARE:
+		l += scnprintf(out + l, sz - l, " Local-non-shared-cache");
+		break;
+	case PERF_MEM_REGION_O_IO:
+		l += scnprintf(out + l, sz - l, " Other-IO");
+		break;
+	case PERF_MEM_REGION_O_SHARE:
+		l += scnprintf(out + l, sz - l, " Other-shared-cache");
+		break;
+	case PERF_MEM_REGION_O_NON_SHARE:
+		l += scnprintf(out + l, sz - l, " Other-non-shared-cache");
+		break;
+	case PERF_MEM_REGION_MMIO:
+		l += scnprintf(out + l, sz - l, " MMIO");
+		break;
+	case PERF_MEM_REGION_MEM0:
+		l += scnprintf(out + l, sz - l, " Mem-0");
+		break;
+	case PERF_MEM_REGION_MEM1:
+		l += scnprintf(out + l, sz - l, " Mem-1");
+		break;
+	case PERF_MEM_REGION_MEM2:
+		l += scnprintf(out + l, sz - l, " Mem-2");
+		break;
+	case PERF_MEM_REGION_MEM3:
+		l += scnprintf(out + l, sz - l, " Mem-3");
+		break;
+	case PERF_MEM_REGION_MEM4:
+		l += scnprintf(out + l, sz - l, " Mem-4");
+		break;
+	case PERF_MEM_REGION_MEM5:
+		l += scnprintf(out + l, sz - l, " Mem-5");
+		break;
+	case PERF_MEM_REGION_MEM6:
+		l += scnprintf(out + l, sz - l, " Mem-6");
+		break;
+	case PERF_MEM_REGION_MEM7:
+		l += scnprintf(out + l, sz - l, " Mem-7");
+		break;
+	default:
+		l += scnprintf(out + l, sz - l, " N/A");
+		break;
+	}
+
+	return l;
+}
+
 int perf_script__meminfo_scnprintf(char *out, size_t sz, const struct mem_info *mem_info)
 {
Quoted 8 lines

tools/perf/util/mem-events.c int perf_script__meminfo_scnprintf(char *out, size_t sz, const struct mem_info * · line 690

Quoted 7 lines
tools/perf/util/mem-events.c +2 -0
 	int i = 0;
@@ -620,6 +686,8 @@ int perf_script__meminfo_scnprintf(char *out, size_t sz, const struct mem_info *
 	i += perf_mem__lck_scnprintf(out + i, sz - i, mem_info);
 	i += scnprintf(out + i, sz - i, "|BLK ");
 	i += perf_mem__blk_scnprintf(out + i, sz - i, mem_info);
+	i += scnprintf(out + i, sz - i, "|Region ");
+	i += perf_mem__region_scnprintf(out + i, sz - i, mem_info);

Same thing. Would be nice if it can be conditional.

Sure. Thanks.

Quoted 32 lines

Thanks,
Namhyung

Quoted 28 lines
tools/perf/util/mem-events.c +0 -0
 	return i;
 }
tools/perf/util/scripting-engines/trace-event-python.c +2 -2
index 8f832ae316ca..57ec259897e4 100644
@@ -695,7 +695,7 @@ static void set_sample_datasrc_in_dict(PyObject *dict,
 				      struct perf_sample *sample)
 {
 	struct mem_info *mi = mem_info__new();
-	char decode[100];
+	char decode[200];
 
 	if (!mi)
 		Py_FatalError("couldn't create mem-info");
@@ -704,7 +704,7 @@ static void set_sample_datasrc_in_dict(PyObject *dict,
 			PyLong_FromUnsignedLongLong(sample->data_src));
 
 	mem_info__data_src(mi)->val = sample->data_src;
-	perf_script__meminfo_scnprintf(decode, 100, mi);
+	perf_script__meminfo_scnprintf(decode, 200, mi);
 	mem_info__put(mi);
 
 	pydict_set_item_string_decref(dict, "datasrc_decode",
Signature · 1 line
2.43.0
#19 lore raw

PATCH v2 5/6 perf header: Support memory ranges

In reply to Thomas Falcon

Memory ranges were created to track different types of memory, such as persistent, high bandwidth, or CXL-attached, that may be present on a system for performance monitoring and resource control purposes.

Memory range data are parsed from the ACPI MRRM table and exposed to userspace tools via sysfs [1]. Memory range data is read from:

/sys/firmware/acpi/memory_ranges/rangeX

With the following attributes:

	u64 base;
	u64 length;
	int node;
	u8  local_region_id;
	u8  remote_region_id;

Read memory range data from sysfs if present and save it in the header of the perf data file under a new feature bit, HEADER_MEMORY_RANGES (35). Memory range data can be viewed with the --header or --header-only options of perf-report and perf-script.

[1]: https://lore.kernel.org/lkml/[email protected]/

Assisted-by Sashiko:gemini-3.1-pro-preview

---
Changes in v2:
-- Added check for NULL return of sysfs__mountpoint() when parsing
memory ranges in sysfs
-- increased MAX_MEMORY_RANGES sanity check from 64 to 256 based on
ACPI MRRM table implementation in Linux kernel
-- added comment for MAX_MEMORY_RANGES to clarify that it is a sanity check
for malformed perf.data files
-- removed a line of code was removed from util/env.h but was added back in
v1 due to bad rebase

---
 .../Documentation/perf.data-file-format.txt   |  13 ++
 tools/perf/builtin-inject.c                   |   1 +
 tools/perf/util/env.c                         |   1 +
 tools/perf/util/env.h                         |  10 +
 tools/perf/util/header.c                      | 195 ++++++++++++++++++
 tools/perf/util/header.h                      |   1 +
 6 files changed, 221 insertions(+)
tools/perf/Documentation/perf.data-file-format.txt +13 -0
index b90cba9168f8..1f8e7d8e9ae2 100644
@@ -478,6 +478,19 @@ The size of the cacheline in bytes. Format:
 
 	unsigned int cln_size;
 
+	HEADER_MEMORY_RANGES = 35,
+
+List of memory ranges. The format of the data is as below.
+
+u32 nr_memory_ranges;
+struct memory_range {
+	u64 base;
+	u64 length;
+	int node;
+	u8 local_region_id;
+	u8 remote_region_id;
+}; [nr_memory_ranges]
+
 	other bits are reserved and should be ignored for now
 	HEADER_FEAT_BITS	= 256,
 
tools/perf/builtin-inject.c +1 -0
index 70bbfad5653e..169617a884d3 100644
@@ -2306,6 +2306,7 @@ static bool keep_feat(struct perf_inject *inject, int feat)
 	case HEADER_PMU_CAPS:
 	case HEADER_CPU_DOMAIN_INFO:
 	case HEADER_CLN_SIZE:
+	case HEADER_MEMORY_RANGES:
 		return true;
 	/* Information that can be updated */
 	case HEADER_BUILD_ID:
tools/perf/util/env.c +1 -0
index c0e2b9d5f0b2..50993b0dedc5 100644
@@ -297,6 +297,7 @@ void perf_env__exit(struct perf_env *env)
 		zfree(&env->pmu_caps[i].pmu_name);
 	}
 	zfree(&env->pmu_caps);
+	zfree(&env->memory_ranges);
 	free_cpu_domain_info(env->cpu_domain, env->schedstat_version, env->nr_cpus_avail);
 }
 
tools/perf/util/env.h +10 -0
index 7acca39b42ff..23d35425efe3 100644
@@ -68,6 +68,14 @@ struct cpu_domain_map {
 	struct domain_info	**domains;
 };
 
+struct memory_range {
+	u64 base;
+	u64 length;
+	int node;
+	u8 local_region_id;
+	u8 remote_region_id;
+};
+
 struct perf_env {
 	char			*hostname;
 	char			*os_release;
@@ -100,6 +108,7 @@ struct perf_env {
 	int			nr_cpu_pmu_caps;
 	int			nr_hybrid_nodes;
 	int			nr_pmus_with_caps;
+	int			nr_memory_ranges;
 	char			*cmdline;
 	const char		**cmdline_argv;
 	char			*sibling_cores;
@@ -122,6 +131,7 @@ struct perf_env {
 	unsigned long long	 memory_bsize;
 	struct hybrid_node	*hybrid_nodes;
 	struct pmu_caps		*pmu_caps;
+	struct memory_range	*memory_ranges;
 #ifdef HAVE_LIBBPF_SUPPORT
 	/*
 	 * bpf_info_lock protects bpf rbtrees. This is needed because the
tools/perf/util/header.c +195 -0

225 lines not on this page · raw message

tools/perf/util/header.h +1 -0
index 5e03f884b7cc..765037762758 100644
@@ -56,6 +56,7 @@ enum {
 	HEADER_CPU_DOMAIN_INFO,
 	HEADER_E_MACHINE,
 	HEADER_CLN_SIZE,
+	HEADER_MEMORY_RANGES,
 	HEADER_LAST_FEATURE,
 	HEADER_FEAT_BITS	= 256,
 };
Signature · 1 line
2.43.0
maintainer
#20 lore raw

Re: [PATCH v2 5/6] perf header: Support memory ranges

Quoted 18 lines · Thomas Falcon

Memory ranges were created to track different types of memory, such as persistent, high bandwidth, or CXL-attached, that may be present on a system for performance monitoring and resource control purposes.

Memory range data are parsed from the ACPI MRRM table and exposed to userspace tools via sysfs [1]. Memory range data is read from:

/sys/firmware/acpi/memory_ranges/rangeX

With the following attributes:

	u64 base;
	u64 length;
	int node;
	u8  local_region_id;
	u8  remote_region_id;
Quoted 4 lines

Read memory range data from sysfs if present and save it in the header of the perf data file under a new feature bit, HEADER_MEMORY_RANGES (35). Memory range data can be viewed with the --header or --header-only options of perf-report and perf-script.

Can you please add an example output here?

Quoted 16 lines

[1]: https://lore.kernel.org/lkml/[email protected]/

Assisted-by Sashiko:gemini-3.1-pro-preview

---
Changes in v2:
-- Added check for NULL return of sysfs__mountpoint() when parsing
memory ranges in sysfs
-- increased MAX_MEMORY_RANGES sanity check from 64 to 256 based on
ACPI MRRM table implementation in Linux kernel
-- added comment for MAX_MEMORY_RANGES to clarify that it is a sanity check
for malformed perf.data files
-- removed a line of code was removed from util/env.h but was added back in
v1 due to bad rebase
---

[SNIP]

Quoted 13 lines
tools/perf/util/header.c +1 -0
index e90e541f546b..0669898362d6 100644
@@ -92,6 +92,7 @@
 #define MAX_PMU_CAPS		512
 #define MAX_PMU_MAPPINGS	4096
 #define MAX_SCHED_DOMAINS	64
+#define MAX_MEMORY_RANGES	256
 
 /*
  * magic2 = "PERFILE2"

tools/perf/util/header.c static int write_cpu_domain_info(struct feat_fd *ff, · line 1897

Quoted 6 lines
tools/perf/util/header.c +3 -0
@@ -1891,6 +1892,130 @@ static int write_cpu_domain_info(struct feat_fd *ff,
 	return ret;
 }
 
+static int memory_range__read(struct memory_range *range, int idx)
+{
+	char path[PATH_MAX], file[PATH_MAX];

I hope we can reduce the number of buffers for the pathnames as it already has another in the caller. Ideally it'd be great if it can use openat() with an FD for the directory.

tools/perf/util/header.c static int write_cpu_domain_info(struct feat_fd *ff, · line 1904

Quoted 7 lines
tools/perf/util/header.c +7 -0
+	struct stat st;
+	int tmp;
+
+	scnprintf(path, PATH_MAX, "firmware/acpi/memory_ranges/range%d", idx);
+	scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
+	if (stat(file, &st))
+		return -1;

It seems this check is redundant.

Quoted 8 lines
tools/perf/util/header.c +8 -0
+
+	scnprintf(file, PATH_MAX, "%s/base", path);
+	if (sysfs__read_xll(file, (unsigned long long *) &range->base))
+		return -1;
+
+	scnprintf(file, PATH_MAX, "%s/length", path);
+	if (sysfs__read_xll(file, (unsigned long long *) &range->length))
+		return -1;

tools/perf/util/header.c static int write_cpu_domain_info(struct feat_fd *ff, · line 1922

Quoted 10 lines
tools/perf/util/header.c +10 -0
+
+	scnprintf(file, PATH_MAX, "%s/node", path);
+	if (sysfs__read_int(file, (int *) &range->node))
+		return -1;
+
+	scnprintf(file, PATH_MAX, "%s/local_region_id", path);
+	if (sysfs__read_int(file, &tmp))
+		return -1;
+
+	if (tmp < 0 || tmp > 255)

Probably better to compare with MAX_MEMORY_REGIONS.

tools/perf/util/header.c static int write_cpu_domain_info(struct feat_fd *ff, · line 1930

Quoted 8 lines
tools/perf/util/header.c +8 -0
+		return -1;
+	range->local_region_id = tmp;
+
+	scnprintf(file, PATH_MAX, "%s/remote_region_id", path);
+	if (sysfs__read_int(file, &tmp))
+		return -1;
+
+	if (tmp < 0 || tmp > 255)

Ditto.

Quoted 12 lines
tools/perf/util/header.c +12 -0
+		return -1;
+	range->remote_region_id = tmp;
+
+	return 0;
+}
+
+static int memory_range__parse(struct memory_range **ranges)
+{
+	const char *sysfs = sysfs__mountpoint();
+	int i, err, nr_memory_ranges = 0;
+	char path[PATH_MAX];
+	struct stat st;

tools/perf/util/header.c static int write_cpu_domain_info(struct feat_fd *ff, · line 1952

Quoted 10 lines
tools/perf/util/header.c +10 -0
+
+	if (!sysfs)
+		return 0;
+
+	scnprintf(path, PATH_MAX, "%s/firmware/acpi/memory_ranges", sysfs);
+	if (stat(path, &st))
+		return 0;
+
+	while (1) {
+		scnprintf(path, PATH_MAX, "%s/firmware/acpi/memory_ranges/range%d", sysfs, nr_memory_ranges);

This line looks too long.

tools/perf/util/header.c static int write_cpu_domain_info(struct feat_fd *ff, · line 1962

Quoted 10 lines
tools/perf/util/header.c +10 -0
+		if (stat(path, &st))
+			break;
+
+		nr_memory_ranges++;
+	}
+
+	if (nr_memory_ranges == 0)
+		return 0;
+
+	*ranges = zalloc(nr_memory_ranges * sizeof(struct memory_range));

I think calloc() is preferred when you multiply the size and the count.

Quoted 20 lines
tools/perf/util/header.c +20 -0
+	if (!(*ranges))
+		return -ENOMEM;
+
+	for (i = 0; i < nr_memory_ranges; i++) {
+		struct memory_range range;
+
+		err = memory_range__read(&range, i);
+		if (err < 0)
+			goto out_error;
+
+		(*ranges)[i] = range;
+	}
+
+	return nr_memory_ranges;
+
+out_error:
+	zfree(ranges);
+	return -1;
+}
+

[SNIP]

Quoted 15 lines
tools/perf/util/header.c +15 -0
+static int process_memory_ranges(struct feat_fd *ff, void *data __maybe_unused)
+{
+	struct perf_env *env = &ff->ph->env;
+	struct memory_range *ranges, *r;
+	u32 nr_memory_ranges, i;
+
+	if (do_read_u32(ff, &nr_memory_ranges))
+		return -1;
+
+	if (!nr_memory_ranges) {
+		pr_debug("memory ranges not available\n");
+		return 0;
+	}
+
+	/* According to version 1.1 of the ACPI MRRM table, the maximum

tools/perf/util/header.c static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unused · line 4370

Quoted 10 lines
tools/perf/util/header.c +10 -0
+	 * number of memory regions can be at most 255. Do a sanity check
+	 * here to guard against a malformed perf.data file.
+	 */
+	if (nr_memory_ranges >= MAX_MEMORY_RANGES) {
+		pr_err("Invalid memory_ranges: nr_memory_ranges (%u) > %u\n",
+		       nr_memory_ranges, MAX_MEMORY_RANGES);
+		return -1;
+	}
+
+	ranges = zalloc(nr_memory_ranges * sizeof(*ranges));

Please use calloc().

Thanks,
Namhyung

Quoted 53 lines
tools/perf/util/header.c +28 -0
+	if (!ranges)
+		return -1;
+
+	for (i = 0; i < nr_memory_ranges; i++) {
+		r = &ranges[i];
+
+		if (do_read_u64(ff, &r->base))
+			goto error;
+		if (do_read_u64(ff, &r->length))
+			goto error;
+		if (do_read_u32(ff, (u32 *) &r->node))
+			goto error;
+		if (__do_read(ff, &r->local_region_id, sizeof(u8)))
+			goto error;
+		if (__do_read(ff, &r->remote_region_id, sizeof(u8)))
+			goto error;
+	}
+
+	env->memory_ranges = ranges;
+	env->nr_memory_ranges = nr_memory_ranges;
+
+	return 0;
+error:
+	zfree(&ranges);
+	return -1;
+}
+
 #define FEAT_OPR(n, func, __full_only) \
 	[HEADER_##n] = {					\
 		.name	    = __stringify(n),			\
@@ -4265,6 +4459,7 @@ const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE] = {
 	FEAT_OPR(CPU_DOMAIN_INFO,	cpu_domain_info,	true),
 	FEAT_OPR(E_MACHINE,	e_machine,	false),
 	FEAT_OPR(CLN_SIZE,	cln_size,	false),
+	FEAT_OPR(MEMORY_RANGES,	memory_ranges,	false),
 };
 
 struct header_print_data {
tools/perf/util/header.h +1 -0
index 5e03f884b7cc..765037762758 100644
@@ -56,6 +56,7 @@ enum {
 	HEADER_CPU_DOMAIN_INFO,
 	HEADER_E_MACHINE,
 	HEADER_CLN_SIZE,
+	HEADER_MEMORY_RANGES,
 	HEADER_LAST_FEATURE,
 	HEADER_FEAT_BITS	= 256,
 };
Signature · 1 line
2.43.0
#21 lore raw

Re: [PATCH v2 5/6] perf header: Support memory ranges

Quoted 19 lines · Namhyung Kim
Quoted 18 lines · Thomas Falcon

Memory ranges were created to track different types of memory, such as persistent, high bandwidth, or CXL-attached, that may be present on a system for performance monitoring and resource control purposes.

Memory range data are parsed from the ACPI MRRM table and exposed to userspace tools via sysfs [1]. Memory range data is read from:

/sys/firmware/acpi/memory_ranges/rangeX

With the following attributes:

	u64 base;
	u64 length;
	int node;
	u8  local_region_id;
	u8  remote_region_id;
Quoted 6 lines
Quoted 4 lines

Read memory range data from sysfs if present and save it in the header of the perf data file under a new feature bit, HEADER_MEMORY_RANGES (35). Memory range data can be viewed with the --header or --header-only options of perf-report and perf-script.

Can you please add an example output here?

Hi, thanks for reviewing. To be honest, I have not been able to get access to supported hardware yet so the only example I can provide would look like this (on a non-capable system)

# memory ranges (nr 0):

I will look into getting access to one and, in the meantime, make the other changes you requested.

Thanks,
Tom

Quoted 254 lines
Quoted 20 lines

[1]:
https://lore.kernel.org/lkml/[email protected]/

Assisted-by Sashiko:gemini-3.1-pro-preview

---
Changes in v2:
-- Added check for NULL return of sysfs__mountpoint() when parsing memory ranges in sysfs
-- increased MAX_MEMORY_RANGES sanity check from 64 to 256 based on ACPI MRRM table implementation in Linux kernel
-- added comment for MAX_MEMORY_RANGES to clarify that it is a sanity check
   for malformed perf.data files
-- removed a line of code was removed from util/env.h but was added back in
   v1 due to bad rebase
---

[SNIP]

Quoted 17 lines
tools/perf/util/header.c +1 -0
index e90e541f546b..0669898362d6 100644
@@ -92,6 +92,7 @@
  #define MAX_PMU_CAPS		512
  #define MAX_PMU_MAPPINGS	4096
  #define MAX_SCHED_DOMAINS	64
+#define MAX_MEMORY_RANGES	256


 /*
  * magic2 = "PERFILE2"

tools/perf/util/header.c +0 -0
@@ -1891,6 +1892,130 @@ static int write_cpu_domain_info(struct
 feat_fd *ff,
  	return ret;
  }

tools/perf/util/header.c static int write_cpu_domain_info(struct feat_fd *ff, · line 1897

Quoted 3 lines
tools/perf/util/header.c +3 -0
+static int memory_range__read(struct memory_range *range, int idx)
+{
+	char path[PATH_MAX], file[PATH_MAX];

I hope we can reduce the number of buffers for the pathnames as it already has another in the caller.  Ideally it'd be great if it can use
openat() with an FD for the directory.

tools/perf/util/header.c

Quoted 9 lines
tools/perf/util/header.c +4 -0
+	struct stat st;
+	int tmp;
+
+	scnprintf(path, PATH_MAX,

"firmware/acpi/memory_ranges/range%d", idx);

tools/perf/util/header.c +1 -0
+	scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(),

path);

tools/perf/util/header.c +2 -0
+	if (stat(file, &st))
+		return -1;

It seems this check is redundant.

Quoted 10 lines
tools/perf/util/header.c +3 -0
+
+	scnprintf(file, PATH_MAX, "%s/base", path);
+	if (sysfs__read_xll(file, (unsigned long long *) &range-
Quoted 1 line

base))

tools/perf/util/header.c +4 -0
+		return -1;
+
+	scnprintf(file, PATH_MAX, "%s/length", path);
+	if (sysfs__read_xll(file, (unsigned long long *) &range-
Quoted 1 line

length))

tools/perf/util/header.c +1 -0
+		return -1;

tools/perf/util/header.c static int write_cpu_domain_info(struct feat_fd *ff, · line 1922

Quoted 10 lines
tools/perf/util/header.c +10 -0
+
+	scnprintf(file, PATH_MAX, "%s/node", path);
+	if (sysfs__read_int(file, (int *) &range->node))
+		return -1;
+
+	scnprintf(file, PATH_MAX, "%s/local_region_id", path);
+	if (sysfs__read_int(file, &tmp))
+		return -1;
+
+	if (tmp < 0 || tmp > 255)

Probably better to compare with MAX_MEMORY_REGIONS.

tools/perf/util/header.c static int write_cpu_domain_info(struct feat_fd *ff, · line 1930

Quoted 8 lines
tools/perf/util/header.c +8 -0
+		return -1;
+	range->local_region_id = tmp;
+
+	scnprintf(file, PATH_MAX, "%s/remote_region_id", path);
+	if (sysfs__read_int(file, &tmp))
+		return -1;
+
+	if (tmp < 0 || tmp > 255)

Ditto.

Quoted 23 lines
tools/perf/util/header.c +17 -0
+		return -1;
+	range->remote_region_id = tmp;
+
+	return 0;
+}
+
+static int memory_range__parse(struct memory_range **ranges)
+{
+	const char *sysfs = sysfs__mountpoint();
+	int i, err, nr_memory_ranges = 0;
+	char path[PATH_MAX];
+	struct stat st;
+
+	if (!sysfs)
+		return 0;
+
+	scnprintf(path, PATH_MAX,

"%s/firmware/acpi/memory_ranges", sysfs);

tools/perf/util/header.c +5 -0
+	if (stat(path, &st))
+		return 0;
+
+	while (1) {
+		scnprintf(path, PATH_MAX,
Quoted 1 line

"%s/firmware/acpi/memory_ranges/range%d", sysfs, nr_memory_ranges);

This line looks too long.

Quoted 11 lines
tools/perf/util/header.c +10 -0
+		if (stat(path, &st))
+			break;
+
+		nr_memory_ranges++;
+	}
+
+	if (nr_memory_ranges == 0)
+		return 0;
+
+	*ranges = zalloc(nr_memory_ranges * sizeof(struct

memory_range));

I think calloc() is preferred when you multiply the size and the count.

Quoted 20 lines
tools/perf/util/header.c +20 -0
+	if (!(*ranges))
+		return -ENOMEM;
+
+	for (i = 0; i < nr_memory_ranges; i++) {
+		struct memory_range range;
+
+		err = memory_range__read(&range, i);
+		if (err < 0)
+			goto out_error;
+
+		(*ranges)[i] = range;
+	}
+
+	return nr_memory_ranges;
+
+out_error:
+	zfree(ranges);
+	return -1;
+}
+

[SNIP]

Quoted 24 lines

+static int process_memory_ranges(struct feat_fd *ff, void *data __maybe_unused)

tools/perf/util/header.c +14 -0
+{
+	struct perf_env *env = &ff->ph->env;
+	struct memory_range *ranges, *r;
+	u32 nr_memory_ranges, i;
+
+	if (do_read_u32(ff, &nr_memory_ranges))
+		return -1;
+
+	if (!nr_memory_ranges) {
+		pr_debug("memory ranges not available\n");
+		return 0;
+	}
+
+	/* According to version 1.1 of the ACPI MRRM table, the

maximum

tools/perf/util/header.c +1 -0
+	 * number of memory regions can be at most 255. Do a

sanity check

tools/perf/util/header.c +4 -0
+	 * here to guard against a malformed perf.data file.
+	 */
+	if (nr_memory_ranges >= MAX_MEMORY_RANGES) {
+		pr_err("Invalid memory_ranges: nr_memory_ranges

(%u) > %u\n",

tools/perf/util/header.c static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unused · line 4370

Quoted 5 lines
tools/perf/util/header.c +5 -0
+		       nr_memory_ranges, MAX_MEMORY_RANGES);
+		return -1;
+	}
+
+	ranges = zalloc(nr_memory_ranges * sizeof(*ranges));

Please use calloc().

Thanks,
Namhyung

Quoted 57 lines
tools/perf/util/header.c +13 -0
+	if (!ranges)
+		return -1;
+
+	for (i = 0; i < nr_memory_ranges; i++) {
+		r = &ranges[i];
+
+		if (do_read_u64(ff, &r->base))
+			goto error;
+		if (do_read_u64(ff, &r->length))
+			goto error;
+		if (do_read_u32(ff, (u32 *) &r->node))
+			goto error;
+		if (__do_read(ff, &r->local_region_id,

sizeof(u8)))

tools/perf/util/header.c +2 -0
+			goto error;
+		if (__do_read(ff, &r->remote_region_id,

sizeof(u8)))

tools/perf/util/header.c +12 -0
+			goto error;
+	}
+
+	env->memory_ranges = ranges;
+	env->nr_memory_ranges = nr_memory_ranges;
+
+	return 0;
+error:
+	zfree(&ranges);
+	return -1;
+}
+

 #define FEAT_OPR(n, func, __full_only) \
  [HEADER_##n] = { \
  .name     = __stringify(n), \

tools/perf/util/header.c +0 -0
@@ -4265,6 +4459,7 @@ const struct perf_header_feature_ops
 feat_ops[HEADER_LAST_FEATURE] = {

  FEAT_OPR(CPU_DOMAIN_INFO, cpu_domain_info, tr
ue),
  FEAT_OPR(E_MACHINE, e_machine, false),
  FEAT_OPR(CLN_SIZE, cln_size, false),

tools/perf/util/header.c +1 -0
+	FEAT_OPR(MEMORY_RANGES,	memory_ranges,	false),

 };

 struct header_print_data {

tools/perf/util/header.h +1 -0
index 5e03f884b7cc..765037762758 100644
@@ -56,6 +56,7 @@ enum {
  	HEADER_CPU_DOMAIN_INFO,
  	HEADER_E_MACHINE,
  	HEADER_CLN_SIZE,
+	HEADER_MEMORY_RANGES,
  	HEADER_LAST_FEATURE,
  	HEADER_FEAT_BITS	= 256,
  };
Signature · 1 line
2.43.0
maintainer
#22 lore raw

Re: [PATCH v2 5/6] perf header: Support memory ranges

Quoted 27 lines · Thomas
Quoted 19 lines · Namhyung Kim
Quoted 18 lines · Thomas Falcon

Memory ranges were created to track different types of memory, such as persistent, high bandwidth, or CXL-attached, that may be present on a system for performance monitoring and resource control purposes.

Memory range data are parsed from the ACPI MRRM table and exposed to userspace tools via sysfs [1]. Memory range data is read from:

/sys/firmware/acpi/memory_ranges/rangeX

With the following attributes:

	u64 base;
	u64 length;
	int node;
	u8  local_region_id;
	u8  remote_region_id;
Quoted 6 lines
Quoted 4 lines

Read memory range data from sysfs if present and save it in the header of the perf data file under a new feature bit, HEADER_MEMORY_RANGES (35). Memory range data can be viewed with the --header or --header-only options of perf-report and perf-script.

Can you please add an example output here?

Quoted 8 lines

Hi, thanks for reviewing. To be honest, I have not been able to get access to supported hardware yet so the only example I can provide would look like this (on a non-capable system)

# memory ranges (nr 0):

I will look into getting access to one and, in the meantime, make the other changes you requested.

Thanks, it'd be nice to see a real output. :)

Namhyung

#23 lore raw

PATCH v2 6/6 perf c2c: print memory region data with stdio output

In reply to Thomas Falcon

Print memory range data in perf-c2c subcommand. Only available in stdio mode currently. If memory ranges are not supported or present, print nothing.

Assisted-by Sashiko:gemini-3.1-pro-preview
---
 tools/perf/builtin-c2c.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
tools/perf/builtin-c2c.c +30 -0
index cba2e9f21ae7..68348a93bd33 100644
@@ -2634,6 +2634,34 @@ static void print_c2c_info(FILE *out, struct perf_session *session)
 	fprintf(out, "  Cacheline data grouping           : %s\n", c2c.cl_sort);
 }
 
+
+static void print_memory_ranges_info(FILE *out, struct perf_session *session)
+{
+	struct perf_env *env = perf_session__env(session);
+	int nr_ranges = 0;
+
+	if (!perf_header__has_feat(&session->header, HEADER_MEMORY_RANGES))
+		return;
+	nr_ranges = env->nr_memory_ranges;
+	if (nr_ranges == 0) {
+		pr_debug("No memory ranges found, skipping\n");
+		return;
+	}
+
+	fprintf(out, "\n");
+	fprintf(out, "=================================================\n");
+	fprintf(out, "                  Memory Ranges                  \n");
+	fprintf(out, "=================================================\n");
+
+	for (int i = 0; i < nr_ranges; i++) {
+		struct memory_range *r = &env->memory_ranges[i];
+
+		fprintf(out, "Range %d: [0x%016" PRIx64 "-0x%016" PRIx64 "] Node %d, local region id %u, remote region id %u\n",
+			i, r->base, r->base + r->length - 1, r->node,
+			r->local_region_id, r->remote_region_id);
+	}
+}
+
 static void perf_c2c__hists_fprintf(FILE *out, struct perf_session *session)
 {
 	setup_pager();
@@ -2647,6 +2675,8 @@ static void perf_c2c__hists_fprintf(FILE *out, struct perf_session *session)
 	if (c2c.stats_only)
 		return;
 
+	print_memory_ranges_info(out, session);
+
 	fprintf(out, "\n");
 	fprintf(out, "=================================================\n");
 	fprintf(out, "           Shared Data Cache Line Table          \n");
Signature · 1 line
2.43.0
#24 lore raw

Re: [PATCH v2 6/6] perf c2c: print memory region data with stdio output

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The perf c2c tool structure omits the `.feature` callback, breaking `perf_env` initialization in…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The perf c2c tool structure omits the .feature callback, breaking perf_env initialization in pipe mode.
- [Low] Implicit reliance on POSIX libc headers violates musl build compatibility rules (missing <stdio.h>). --

--- Patch [6]: [PATCH v2 6/6] perf c2c: print memory region data with stdio output --- commit 210b29f2c6ae9c7cc910fd9994d1e0b92078eec2
Author: Thomas Falcon <[email protected]>

perf c2c: print memory region data with stdio output

Print memory range data in perf-c2c subcommand. Only available in stdio mode currently. If memory ranges are not supported or present, print nothing.

tools/perf/builtin-c2c.c static void print_c2c_info(FILE *out, struct perf_session *session) · line 2639

Quoted 11 lines
tools/perf/builtin-c2c.c +3 -0
index cba2e9f21ae7c..68348a93bd331 100644
@@ -2634,6 +2634,34 @@ static void print_c2c_info(FILE *out, struct perf_session *session)
 	fprintf(out, "  Cacheline data grouping           : %s\n", c2c.cl_sort);
 }
 
+
+static void print_memory_ranges_info(FILE *out, struct perf_session *session)
+{

[Severity: Low]
Does this code rely on implicit header inclusion?

The new print_memory_ranges_info() function uses FILE and fprintf, but the patch doesn't explicitly include <stdio.h>. Can this cause the code to fail to compile under musl libc due to missing declarations?

Quoted 14 lines
tools/perf/builtin-c2c.c +14 -0
+	struct perf_env *env = perf_session__env(session);
+	int nr_ranges = 0;
+
+	if (!perf_header__has_feat(&session->header, HEADER_MEMORY_RANGES))
+		return;
+	nr_ranges = env->nr_memory_ranges;
+	if (nr_ranges == 0) {
+		pr_debug("No memory ranges found, skipping\n");
+		return;
+	}
+
+	fprintf(out, "\n");
+	fprintf(out, "=================================================\n");
+	fprintf(out, "                  Memory Ranges                  \n");

tools/perf/builtin-c2c.c static void print_c2c_info(FILE *out, struct perf_session *session) · line 2663

Quoted 10 lines
tools/perf/builtin-c2c.c +10 -0
+	fprintf(out, "=================================================\n");
+
+	for (int i = 0; i < nr_ranges; i++) {
+		struct memory_range *r = &env->memory_ranges[i];
+
+		fprintf(out, "Range %d: [0x%016" PRIx64 "-0x%016" PRIx64 "] Node %d, local region id %u, remote region id %u\n",
+			i, r->base, r->base + r->length - 1, r->node,
+			r->local_region_id, r->remote_region_id);
+	}
+}

[Severity: Medium]
Will this silently fail to display memory ranges when perf is run in pipe mode?

The tool initializes its callbacks in perf_c2c__report() like this:

	perf_tool__init(&c2c.tool, /*ordered_events=*/true);
	c2c.tool.sample		= process_sample_event;
	c2c.tool.mmap		= perf_event__process_mmap;

Because the .feature callback is missing here, feature events are dropped in pipe mode, so perf_env is not populated with memory ranges. Although the callback omission was pre-existing, doesn't it actively break the functionality introduced in this patch?

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#25 lore raw

Re: [PATCH v2 6/6] perf c2c: print memory region data with stdio output

Quoted 39 lines · On Tue, 2026-07-14 at 00:57 +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The perf c2c tool structure omits the .feature callback, breaking perf_env initialization in pipe mode.
- [Low] Implicit reliance on POSIX libc headers violates musl build compatibility rules (missing <stdio.h>).
--

--- Patch [6]: [PATCH v2 6/6] perf c2c: print memory region data with stdio output ---
commit 210b29f2c6ae9c7cc910fd9994d1e0b92078eec2
Author: Thomas Falcon <[email protected]>

    perf c2c: print memory region data with stdio output

    Print memory range data in perf-c2c subcommand. Only available in stdio
    mode currently. If memory ranges are not supported or present, print
    nothing.

Quoted 12 lines
tools/perf/builtin-c2c.c +0 -0
index cba2e9f21ae7c..68348a93bd331 100644
@@ -2634,6 +2634,34 @@ static void print_c2c_info(FILE *out, struct
 perf_session *session)
  	fprintf(out, "  Cacheline data grouping           : %s\n",
 c2c.cl_sort);
  }

tools/perf/builtin-c2c.c +2 -0
+
+static void print_memory_ranges_info(FILE *out, struct
Quoted 2 lines

perf_session *session)
+{

[Severity: Low]
Does this code rely on implicit header inclusion?

Quoted 5 lines

The new print_memory_ranges_info() function uses FILE and fprintf, but the
patch doesn't explicitly include <stdio.h>. Can this cause the code to fail to
compile under musl libc due to missing declarations?

Existing functions (print_c2c__display_stats(), print_shared_cacheline_info(), print_c2c_info()) use FILE and fprintf() without any issue.

Quoted 28 lines
Quoted 28 lines
tools/perf/builtin-c2c.c +4 -0
+	struct perf_env *env = perf_session__env(session);
+	int nr_ranges = 0;
+
+	if (!perf_header__has_feat(&session->header,

HEADER_MEMORY_RANGES))

tools/perf/builtin-c2c.c +9 -0
+		return;
+	nr_ranges = env->nr_memory_ranges;
+	if (nr_ranges == 0) {
+		pr_debug("No memory ranges found, skipping\n");
+		return;
+	}
+
+	fprintf(out, "\n");
+	fprintf(out,

"=================================================\n");

tools/perf/builtin-c2c.c +1 -0
+	fprintf(out, "                  Memory

Ranges                  \n");

tools/perf/builtin-c2c.c +1 -0
+	fprintf(out,

"=================================================\n");

tools/perf/builtin-c2c.c +5 -0
+
+	for (int i = 0; i < nr_ranges; i++) {
+		struct memory_range *r = &env->memory_ranges[i];
+
+		fprintf(out, "Range %d: [0x%016" PRIx64 "-0x%016"

PRIx64 "] Node %d, local region id %u, remote region id %u\n",

tools/perf/builtin-c2c.c +1 -0
+			i, r->base, r->base + r->length - 1, r-
Quoted 1 line

node,

Quoted 8 lines

tools/perf/builtin-c2c.c static void print_c2c_info(FILE *out, struct perf_session *session) · line 2663

Quoted 3 lines
tools/perf/builtin-c2c.c +3 -0
+			r->local_region_id, r->remote_region_id);
+	}
+}

[Severity: Medium]
Will this silently fail to display memory ranges when perf is run in pipe
mode?

Pipe mode is not supported on perf-c2c.

Thanks,
Tom

Quoted 16 lines

The tool initializes its callbacks in perf_c2c__report() like this:

	perf_tool__init(&c2c.tool, /*ordered_events=*/true);
	c2c.tool.sample		= process_sample_event;
	c2c.tool.mmap		= perf_event__process_mmap;

Because the .feature callback is missing here, feature events are dropped in
pipe mode, so perf_env is not populated with memory ranges. Although the
callback omission was pre-existing, doesn't it actively break the functionality introduced in this patch?

[ ... ]

maintainer
#26 lore raw

Re: [PATCH v2 6/6] perf c2c: print memory region data with stdio output

In reply to Thomas Falcon
Quoted 3 lines · Thomas Falcon

Print memory range data in perf-c2c subcommand. Only available in stdio mode currently. If memory ranges are not supported or present, print nothing.

Can you please add an example output?

Thanks,
Namhyung

Quoted 58 lines
Assisted-by Sashiko:gemini-3.1-pro-preview
---
 tools/perf/builtin-c2c.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
tools/perf/builtin-c2c.c +30 -0
index cba2e9f21ae7..68348a93bd33 100644
@@ -2634,6 +2634,34 @@ static void print_c2c_info(FILE *out, struct perf_session *session)
 	fprintf(out, "  Cacheline data grouping           : %s\n", c2c.cl_sort);
 }
 
+
+static void print_memory_ranges_info(FILE *out, struct perf_session *session)
+{
+	struct perf_env *env = perf_session__env(session);
+	int nr_ranges = 0;
+
+	if (!perf_header__has_feat(&session->header, HEADER_MEMORY_RANGES))
+		return;
+	nr_ranges = env->nr_memory_ranges;
+	if (nr_ranges == 0) {
+		pr_debug("No memory ranges found, skipping\n");
+		return;
+	}
+
+	fprintf(out, "\n");
+	fprintf(out, "=================================================\n");
+	fprintf(out, "                  Memory Ranges                  \n");
+	fprintf(out, "=================================================\n");
+
+	for (int i = 0; i < nr_ranges; i++) {
+		struct memory_range *r = &env->memory_ranges[i];
+
+		fprintf(out, "Range %d: [0x%016" PRIx64 "-0x%016" PRIx64 "] Node %d, local region id %u, remote region id %u\n",
+			i, r->base, r->base + r->length - 1, r->node,
+			r->local_region_id, r->remote_region_id);
+	}
+}
+
 static void perf_c2c__hists_fprintf(FILE *out, struct perf_session *session)
 {
 	setup_pager();
@@ -2647,6 +2675,8 @@ static void perf_c2c__hists_fprintf(FILE *out, struct perf_session *session)
 	if (c2c.stats_only)
 		return;
 
+	print_memory_ranges_info(out, session);
+
 	fprintf(out, "\n");
 	fprintf(out, "=================================================\n");
 	fprintf(out, "           Shared Data Cache Line Table          \n");
Signature · 1 line
2.43.0