Skip to contentSkip to conversation

perf: Add PERF_SAMPLE_PHYS_ADDR

PATCH Superseded [email protected] 3 participants 17 messages Jan 6, 2016 – Jan 8, 2016

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/8 perf: Add PERF_SAMPLE_PHYS_ADDR

From: Kan Liang <[email protected]>

For understanding how the workload maps to memory channels and hardware behavior, it's useful to collect address maps with physical addresses. This is not intended for detecting page sharing (which can be already done using the mmap inode), but for lower level hardware behavior studies.
Perf supports load latency/DLA which can only collect virtual addresses. This patch add a new sample type PERF_SAMPLE_PHYS_ADDR to expose the physical addresses.
For kernel direct mapping addresses, the patch uses virt_to_phys to convert the virtual addresses from DLA to physical address. For user virtual addresses, __get_user_pages_fast is used to walk the pages tables for user physical address.
This does not work for vmalloc addresses. Right now these are not resolved, but code to do that could be added.
For security, the physical address can only be exposed to root or privileged user.

---
 arch/x86/kernel/cpu/perf_event.h          |  2 +-
 arch/x86/kernel/cpu/perf_event_intel_ds.c | 23 +++++++++++++++++++++++
 include/linux/perf_event.h                |  3 +++
 include/uapi/linux/perf_event.h           |  4 +++-
 kernel/events/core.c                      | 11 +++++++++++
 5 files changed, 41 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/perf_event.h +1 -1
index 799e6bd..164de68 100644
@@ -90,7 +90,7 @@ struct amd_nb {
 	(PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_ADDR | \
 	PERF_SAMPLE_ID | PERF_SAMPLE_CPU | PERF_SAMPLE_STREAM_ID | \
 	PERF_SAMPLE_DATA_SRC | PERF_SAMPLE_IDENTIFIER | \
-	PERF_SAMPLE_TRANSACTION)
+	PERF_SAMPLE_TRANSACTION | PERF_SAMPLE_PHYS_ADDR)
 
 /*
  * A debug store configuration.
arch/x86/kernel/cpu/perf_event_intel_ds.c +23 -0
index 5db1c77..2e333dc 100644
@@ -986,6 +986,7 @@ static void setup_pebs_sample_data(struct perf_event *event,
 	u64 sample_type;
 	int fll, fst, dsrc;
 	int fl = event->hw.flags;
+	struct page *p = NULL;
 
 	if (pebs == NULL)
 		return;
@@ -1071,6 +1072,28 @@ static void setup_pebs_sample_data(struct perf_event *event,
 	    x86_pmu.intel_cap.pebs_format >= 1)
 		data->addr = pebs->dla;
 
+	if ((sample_type & PERF_SAMPLE_PHYS_ADDR) && (data->addr != 0)) {
+		if (data->addr >= TASK_SIZE) {
+			/* If it's vmalloc()d memory, leave phys_addr as 0 */
+			if (virt_addr_valid(data->addr) &&
+			    !(data->addr >= VMALLOC_START && data->addr < VMALLOC_END))
+				data->phys_addr = (u64)virt_to_phys((void *)data->addr);
+		} else {
+			/*
+			 * Walking the pages tables for user address.
+			 * Interrupts are disabled, so it prevents any tear down
+			 * of the page tables.
+			 * Try IRQ-safe __get_user_pages_fast first.
+			 * If failed, leave phys_addr as 0.
+			 */
+			if ((current->mm != NULL) &&
+			    (__get_user_pages_fast(data->addr, 1, 0, &p) == 1))
+				data->phys_addr = page_to_phys(p) + data->addr % PAGE_SIZE;
+
+			if (p)
+				put_page(p);
+		}
+	}
 	if (x86_pmu.intel_cap.pebs_format >= 2) {
 		/* Only set the TSX weight when no memory weight. */
 		if ((sample_type & PERF_SAMPLE_WEIGHT) && !fll)
include/linux/perf_event.h +3 -0
index f9828a4..d9c0527 100644
@@ -795,6 +795,8 @@ struct perf_sample_data {
 
 	struct perf_regs		regs_intr;
 	u64				stack_user_size;
+
+	u64				phys_addr;
 } ____cacheline_aligned;
 
 /* default value for data source */
@@ -815,6 +817,7 @@ static inline void perf_sample_data_init(struct perf_sample_data *data,
 	data->weight = 0;
 	data->data_src.val = PERF_MEM_NA;
 	data->txn = 0;
+	data->phys_addr = 0;
 }
 
 extern void perf_output_sample(struct perf_output_handle *handle,
include/uapi/linux/perf_event.h +3 -1
index 1afe962..5afc572 100644
@@ -139,8 +139,9 @@ enum perf_event_sample_format {
 	PERF_SAMPLE_IDENTIFIER			= 1U << 16,
 	PERF_SAMPLE_TRANSACTION			= 1U << 17,
 	PERF_SAMPLE_REGS_INTR			= 1U << 18,
+	PERF_SAMPLE_PHYS_ADDR			= 1U << 19,
 
-	PERF_SAMPLE_MAX = 1U << 19,		/* non-ABI */
+	PERF_SAMPLE_MAX = 1U << 20,		/* non-ABI */
 };
 
 /*
@@ -767,6 +768,7 @@ enum perf_event_type {
 	 *	{ u64			transaction; } && PERF_SAMPLE_TRANSACTION
 	 *	{ u64			abi; # enum perf_sample_regs_abi
 	 *	  u64			regs[weight(mask)]; } && PERF_SAMPLE_REGS_INTR
+	 *	{ u64			phys_addr;} && PERF_SAMPLE_PHYS_ADDR
 	 * };
 	 */
 	PERF_RECORD_SAMPLE			= 9,
kernel/events/core.c +11 -0
index a627f36..9a922a2 100644
@@ -1334,6 +1334,9 @@ static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
 	if (sample_type & PERF_SAMPLE_TRANSACTION)
 		size += sizeof(data->txn);
 
+	if (sample_type & PERF_SAMPLE_PHYS_ADDR)
+		size += sizeof(data->phys_addr);
+
 	event->header_size = size;
 }
 
@@ -5432,6 +5435,9 @@ void perf_output_sample(struct perf_output_handle *handle,
 	if (sample_type & PERF_SAMPLE_DATA_SRC)
 		perf_output_put(handle, data->data_src.val);
 
+	if (sample_type & PERF_SAMPLE_PHYS_ADDR)
+		perf_output_put(handle, data->phys_addr);
+
 	if (sample_type & PERF_SAMPLE_TRANSACTION)
 		perf_output_put(handle, data->txn);
 
@@ -8269,6 +8275,11 @@ SYSCALL_DEFINE5(perf_event_open,
 			return -EINVAL;
 	}
 
+	/* Only privileged users can get kernel addresses */
+	if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR) &&
+	    !capable(CAP_SYS_ADMIN))
+		return -EACCES;
+
 	/*
 	 * In cgroup mode, the pid argument is used to pass the fd
 	 * opened to the cgroup directory in cgroupfs. The cpu argument
Signature · 1 line
1.8.3.1
#10 lore raw

PATCH 8/8 perf test: add test case for PERF_SAMPLE_PHYS_ADDR

From: Kan Liang <[email protected]>

Extend sample-parsing test cases to support new sample type PERF_SAMPLE_PHYS_ADDR.

---
 tools/perf/tests/sample-parsing.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/perf/tests/sample-parsing.c +4 -1
index 5f23710..8813fa1 100644
@@ -45,6 +45,9 @@ static bool samples_same(const struct perf_sample *s1,
 	if (type & PERF_SAMPLE_ADDR)
 		COMP(addr);
 
+	if (type & PERF_SAMPLE_PHYS_ADDR)
+		COMP(phys_addr);
+
 	if (type & PERF_SAMPLE_ID)
 		COMP(id);
 
@@ -303,7 +306,7 @@ int test__sample_parsing(int subtest __maybe_unused)
 	 * were added.  Please actually update the test rather than just change
 	 * the condition below.
 	 */
-	if (PERF_SAMPLE_MAX > PERF_SAMPLE_REGS_INTR << 1) {
+	if (PERF_SAMPLE_MAX > PERF_SAMPLE_PHYS_ADDR << 1) {
 		pr_debug("sample format has changed, some new PERF_SAMPLE_ bit was introduced - test needs updating\n");
 		return -1;
 	}
Signature · 1 line
1.8.3.1
#7 lore raw

PATCH 6/8 perf mem: dump physical addresses

In reply to [email protected]

From: Kan Liang <[email protected]>

perf mem report should support dumping physical addresses by applying -p or --phys-data.

---
 tools/perf/Documentation/perf-mem.txt |  4 ++
 tools/perf/builtin-mem.c              | 81 ++++++++++++++++++++++++-----------
 2 files changed, 61 insertions(+), 24 deletions(-)
tools/perf/Documentation/perf-mem.txt +4 -0
index 43310d8..929575a 100644
@@ -48,6 +48,10 @@ OPTIONS
 	option can be passed in record mode. It will be interpreted the same way as perf
 	record.
 
+-p::
+--phys-data::
+	Record/Report/Dump sample physical addresses
+
 SEE ALSO
 --------
 linkperf:perf-record[1], linkperf:perf-report[1]
tools/perf/builtin-mem.c +57 -24

101 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#8 lore raw

PATCH 7/8 perf script: support physical addresses in script

In reply to [email protected]

From: Kan Liang <[email protected]>

perf script print out physical addresses by applying phys_addr. Only display physical address when virtual address is selected. The physical address will be printed out right after virtual address.

---
 tools/perf/builtin-script.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
tools/perf/builtin-script.c +18 -1
index c298cdc..1e292a7 100644
@@ -58,6 +58,7 @@ enum perf_output_field {
 	PERF_OUTPUT_IREGS	    = 1U << 14,
 	PERF_OUTPUT_BRSTACK	    = 1U << 15,
 	PERF_OUTPUT_BRSTACKSYM	    = 1U << 16,
+	PERF_OUTPUT_PHYS_ADDR	    = 1U << 17,
 };
 
 struct output_option {
@@ -81,6 +82,7 @@ struct output_option {
 	{.str = "iregs", .field = PERF_OUTPUT_IREGS},
 	{.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
 	{.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
+	{.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
 };
 
 /* default set to maintain compatibility with current format */
@@ -242,6 +244,12 @@ static int perf_evsel__check_attr(struct perf_evsel *evsel,
 					   PERF_OUTPUT_ADDR, allow_user_set))
 		return -EINVAL;
 
+	if (PRINT_FIELD(PHYS_ADDR) && !PRINT_FIELD(ADDR)) {
+		pr_err("Display of sample physical address"
+		       "but sample address is not selected.\n");
+		return -EINVAL;
+	}
+
 	if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
 		pr_err("Display of symbols requested but neither sample IP nor "
 			   "sample address\nis selected. Hence, no addresses to convert "
@@ -290,6 +298,11 @@ static int perf_evsel__check_attr(struct perf_evsel *evsel,
 					PERF_OUTPUT_IREGS))
 		return -EINVAL;
 
+	if (PRINT_FIELD(PHYS_ADDR) &&
+		perf_evsel__do_check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
+					   PERF_OUTPUT_PHYS_ADDR, allow_user_set))
+		return -EINVAL;
+
 	return 0;
 }
 
@@ -653,6 +666,9 @@ static void process_event(struct perf_script *script __maybe_unused, union perf_
 	if (PRINT_FIELD(ADDR))
 		print_sample_addr(event, sample, thread, attr);
 
+	if (PRINT_FIELD(PHYS_ADDR))
+		printf("%16" PRIx64, sample->phys_addr);
+
 	if (PRINT_FIELD(IP)) {
 		if (!symbol_conf.use_callchain)
 			printf(" ");
@@ -1893,7 +1909,8 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
 		     "comma separated output fields prepend with 'type:'. "
 		     "Valid types: hw,sw,trace,raw. "
 		     "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
-		     "addr,symoff,period,iregs,brstack,brstacksym,flags", parse_output_fields),
+		     "addr,symoff,period,iregs,brstack,brstacksym,flags,phys_addr",
+		     parse_output_fields),
 	OPT_BOOLEAN('a', "all-cpus", &system_wide,
 		    "system-wide collection from all CPUs"),
 	OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 2/8 perf tools: add option to record sample physical address

In reply to [email protected]

From: Kan Liang <[email protected]>

Add new option --phys-data for perf record to record sample physical address. Once the option is applied, it implies that --data option is also applied to record sample virtual address.

---
 tools/perf/Documentation/perf-record.txt |  6 +++++-
 tools/perf/builtin-record.c              | 10 +++++++++-
 tools/perf/perf.h                        |  1 +
 tools/perf/util/event.h                  |  1 +
 tools/perf/util/evsel.c                  | 17 +++++++++++++++++
 tools/perf/util/session.c                |  3 +++
 6 files changed, 36 insertions(+), 2 deletions(-)
tools/perf/Documentation/perf-record.txt +5 -1
index 3a1a32f..e40c9df 100644
@@ -181,7 +181,11 @@ OPTIONS
 
 -d::
 --data::
-	Record the sample addresses.
+	Record the sample virtual addresses.
+
+--phys-data::
+	Record the sample physical addresses which implies
+	the virtual address is also recorded.
 
 -T::
 --timestamp::
tools/perf/builtin-record.c +9 -1
index 9c5cdc2c4..b552f50 100644
@@ -1082,7 +1082,8 @@ struct option __record_options[] = {
 	OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"),
 	OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat,
 		    "per thread counts"),
-	OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"),
+	OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample virtual addresses"),
+	OPT_BOOLEAN(0, "phys-data", &record.opts.sample_phys_addr, "Record the sample physical addresses"),
 	OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time,
 			&record.opts.sample_time_set,
 			"Record the sample timestamps"),
@@ -1223,6 +1224,13 @@ int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
 		goto out_symbol_exit;
 	}
 
+	/*
+	 * Record the sample physical addr which also implies
+	 * the virtual addr is recorded.
+	 */
+	if (rec->opts.sample_phys_addr)
+		rec->opts.sample_address = true;
+
 	if (rec->opts.target.tid && !rec->opts.no_inherit_set)
 		rec->opts.no_inherit = true;
 
tools/perf/perf.h +1 -0
index 90129ac..edfb938 100644
@@ -49,6 +49,7 @@ struct record_opts {
 	bool	     no_samples;
 	bool	     raw_samples;
 	bool	     sample_address;
+	bool	     sample_phys_addr;
 	bool	     sample_weight;
 	bool	     sample_time;
 	bool	     sample_time_set;
tools/perf/util/event.h +1 -0
index b7ffb7e..0715883 100644
@@ -190,6 +190,7 @@ struct perf_sample {
 	u32 cpu;
 	u32 raw_size;
 	u64 data_src;
+	u64 phys_addr;
 	u32 flags;
 	u16 insn_len;
 	void *raw_data;
tools/perf/util/evsel.c +17 -0
index 544e440..32f7b4f 100644
@@ -838,6 +838,9 @@ void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
 	if (opts->sample_address)
 		perf_evsel__set_sample_bit(evsel, DATA_SRC);
 
+	if (opts->sample_phys_addr)
+		perf_evsel__set_sample_bit(evsel, PHYS_ADDR);
+
 	if (opts->no_buffering) {
 		attr->watermark = 0;
 		attr->wakeup_events = 1;
@@ -1847,6 +1850,12 @@ int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
 		array++;
 	}
 
+	data->phys_addr = 0;
+	if (type & PERF_SAMPLE_PHYS_ADDR) {
+		data->phys_addr = *array;
+		array++;
+	}
+
 	data->transaction = 0;
 	if (type & PERF_SAMPLE_TRANSACTION) {
 		OVERFLOW_CHECK_u64(array);
@@ -1963,6 +1972,9 @@ size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
 	if (type & PERF_SAMPLE_DATA_SRC)
 		result += sizeof(u64);
 
+	if (type & PERF_SAMPLE_PHYS_ADDR)
+		result += sizeof(u64);
+
 	if (type & PERF_SAMPLE_TRANSACTION)
 		result += sizeof(u64);
 
@@ -2149,6 +2161,11 @@ int perf_event__synthesize_sample(union perf_event *event, u64 type,
 		array++;
 	}
 
+	if (type & PERF_SAMPLE_PHYS_ADDR) {
+		*array = sample->phys_addr;
+		array++;
+	}
+
 	if (type & PERF_SAMPLE_TRANSACTION) {
 		*array = sample->transaction;
 		array++;
tools/perf/util/session.c +3 -0
index d5636ba..670ff4f 100644
@@ -1122,6 +1122,9 @@ static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
 	if (sample_type & PERF_SAMPLE_DATA_SRC)
 		printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
 
+	if (sample_type & PERF_SAMPLE_PHYS_ADDR)
+		printf(" .. phys_addr: 0x%"PRIx64"\n", sample->phys_addr);
+
 	if (sample_type & PERF_SAMPLE_TRANSACTION)
 		printf("... transaction: %" PRIx64 "\n", sample->transaction);
 
Signature · 1 line
1.8.3.1
#5 lore raw

PATCH 5/8 perf mem: report physical addresses

In reply to [email protected]

From: Kan Liang <[email protected]>

perf mem report should support physical addresses by applying -p or --phys-data. The default mem sort order for physical addresses is added accordingly.

---
 tools/perf/builtin-mem.c | 15 +++++++++++----
 tools/perf/util/sort.c   |  1 +
 tools/perf/util/sort.h   |  1 +
 3 files changed, 13 insertions(+), 4 deletions(-)
tools/perf/builtin-mem.c +11 -4
index 7d5ff3a..a81bdc2 100644
@@ -6,6 +6,7 @@
 #include "util/tool.h"
 #include "util/session.h"
 #include "util/data.h"
+#include "util/sort.h"
 
 #define MEM_OPERATION_LOAD	0x1
 #define MEM_OPERATION_STORE	0x2
@@ -176,9 +177,15 @@ static int report_events(int argc, const char **argv, struct perf_mem *mem)
 	 * there is no weight (cost) associated with stores, so don't print
 	 * the column
 	 */
-	if (!(mem->operation & MEM_OPERATION_LOAD))
-		rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
-				"dso_daddr,tlb,locked";
+	if (!(mem->operation & MEM_OPERATION_LOAD)) {
+		if (mem->phys_addr)
+			rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
+					"dso_daddr,tlb,locked,phys_daddr";
+		else
+			rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
+					"dso_daddr,tlb,locked";
+	} else if (mem->phys_addr)
+		rep_argv[i++] = default_phys_mem_sort_order;
 
 	for (j = 1; j < argc; j++, i++)
 		rep_argv[i] = argv[j];
@@ -294,7 +301,7 @@ int cmd_mem(int argc, const char **argv, const char *prefix __maybe_unused)
 		   "separator for columns, no spaces will be added"
 		   " between columns '.' is reserved."),
 	OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"),
-	OPT_BOOLEAN('p', "phys-data", &mem.phys_addr, "Record sample physical addresses"),
+	OPT_BOOLEAN('p', "phys-data", &mem.phys_addr, "Record/Report sample physical addresses"),
 	OPT_END()
 	};
 	const char *const mem_subcommands[] = { "record", "report", NULL };
tools/perf/util/sort.c +1 -0
index 4ce6827..c9e7088 100644
@@ -13,6 +13,7 @@ const char	*parent_pattern = default_parent_pattern;
 const char	default_sort_order[] = "comm,dso,symbol";
 const char	default_branch_sort_order[] = "comm,dso_from,symbol_from,symbol_to,cycles";
 const char	default_mem_sort_order[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked";
+const char	default_phys_mem_sort_order[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked,phys_daddr";
 const char	default_top_sort_order[] = "dso,symbol";
 const char	default_diff_sort_order[] = "dso,symbol";
 const char	default_tracepoint_sort_order[] = "trace";
tools/perf/util/sort.h +1 -0
index f6e1781..4421f76 100644
@@ -46,6 +46,7 @@ extern struct sort_entry sort_sym_from;
 extern struct sort_entry sort_sym_to;
 extern enum sort_type sort__first_dimension;
 extern const char default_mem_sort_order[];
+extern const char default_phys_mem_sort_order[];
 
 struct he_stat {
 	u64			period;
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH 4/8 perf mem: add option phys-data to record physical address

In reply to [email protected]

From: Kan Liang <[email protected]>

Add option phys-data in perf mem to record physical address

---
 tools/perf/builtin-mem.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/perf/builtin-mem.c +5 -0
index 3901700..7d5ff3a 100644
@@ -16,6 +16,7 @@ struct perf_mem {
 	bool			hide_unresolved;
 	bool			dump_raw;
 	bool			force;
+	bool			phys_addr;
 	int			operation;
 	const char		*cpu_list;
 	DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
@@ -39,6 +40,9 @@ static int __cmd_record(int argc, const char **argv, struct perf_mem *mem)
 
 	rec_argv[i++] = "-d";
 
+	if (mem->phys_addr)
+		rec_argv[i++] = "--phys-data";
+
 	if (mem->operation & MEM_OPERATION_LOAD) {
 		rec_argv[i++] = "-e";
 		rec_argv[i++] = "cpu/mem-loads/pp";
@@ -290,6 +294,7 @@ int cmd_mem(int argc, const char **argv, const char *prefix __maybe_unused)
 		   "separator for columns, no spaces will be added"
 		   " between columns '.' is reserved."),
 	OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"),
+	OPT_BOOLEAN('p', "phys-data", &mem.phys_addr, "Record sample physical addresses"),
 	OPT_END()
 	};
 	const char *const mem_subcommands[] = { "record", "report", NULL };
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH 3/8 perf tools: add sort option phys_daddr

In reply to [email protected]

From: Kan Liang <[email protected]>

Add a new sort option phys_daddr for --mem-mode sort. With this option applied, perf can sort and report by sample's physical address.

---
 tools/perf/Documentation/perf-report.txt |  1 +
 tools/perf/util/hist.c                   |  3 +++
 tools/perf/util/hist.h                   |  1 +
 tools/perf/util/machine.c                |  7 ++++--
 tools/perf/util/sort.c                   | 42 ++++++++++++++++++++++++++++++++
 tools/perf/util/sort.h                   |  1 +
 tools/perf/util/symbol.h                 |  1 +
 7 files changed, 54 insertions(+), 2 deletions(-)
tools/perf/Documentation/perf-report.txt +1 -0
index 8a301f6..91c387e 100644
@@ -163,6 +163,7 @@ OPTIONS
 	- mem: type of memory access for the data at the time of sample
 	- snoop: type of snoop (if any) for the data at the time of sample
 	- dcacheline: the cacheline the data address is on at the time of sample
+	- phys_daddr: physical address of data being executed on at the time of sample
 
 	And default sort keys are changed to local_weight, mem, sym, dso,
 	symbol_daddr, dso_daddr, snoop, tlb, locked, see '--mem-mode'.
tools/perf/util/hist.c +3 -0
index afc9b8f..3051f15 100644
@@ -152,6 +152,9 @@ void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
 			symlen = unresolved_col_width + 4 + 2;
 			hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
 		}
+
+		hists__new_col_len(hists, HISTC_MEM_PHYS_DADDR, unresolved_col_width + 4 + 2);
+
 	} else {
 		symlen = unresolved_col_width + 4 + 2;
 		hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
tools/perf/util/hist.h +1 -0
index cb8f373..3bb574a 100644
@@ -44,6 +44,7 @@ enum hist_column {
 	HISTC_GLOBAL_WEIGHT,
 	HISTC_MEM_DADDR_SYMBOL,
 	HISTC_MEM_DADDR_DSO,
+	HISTC_MEM_PHYS_DADDR,
 	HISTC_MEM_LOCKED,
 	HISTC_MEM_TLB,
 	HISTC_MEM_LVL,
tools/perf/util/machine.c +5 -2
index ad79297..6a516cc 100644
@@ -1560,10 +1560,12 @@ static void ip__resolve_ams(struct thread *thread,
 	ams->al_addr = al.addr;
 	ams->sym = al.sym;
 	ams->map = al.map;
+	ams->phys_addr = 0;
 }
 
 static void ip__resolve_data(struct thread *thread,
-			     u8 m, struct addr_map_symbol *ams, u64 addr)
+			     u8 m, struct addr_map_symbol *ams,
+			     u64 addr, u64 phys_addr)
 {
 	struct addr_location al;
 
@@ -1583,6 +1585,7 @@ static void ip__resolve_data(struct thread *thread,
 	ams->al_addr = al.addr;
 	ams->sym = al.sym;
 	ams->map = al.map;
+	ams->phys_addr = phys_addr;
 }
 
 struct mem_info *sample__resolve_mem(struct perf_sample *sample,
@@ -1594,7 +1597,7 @@ struct mem_info *sample__resolve_mem(struct perf_sample *sample,
 		return NULL;
 
 	ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
-	ip__resolve_data(al->thread, al->cpumode, &mi->daddr, sample->addr);
+	ip__resolve_data(al->thread, al->cpumode, &mi->daddr, sample->addr, sample->phys_addr);
 	mi->data_src.val = sample->data_src;
 
 	return mi;
tools/perf/util/sort.c +42 -0

60 lines not on this page · raw message

tools/perf/util/sort.h +1 -0
index dec536b..f6e1781 100644
@@ -207,6 +207,7 @@ enum sort_type {
 	SORT_MEM_SNOOP,
 	SORT_MEM_DCACHELINE,
 	SORT_MEM_IADDR_SYMBOL,
+	SORT_MEM_PHYS_DADDR,
 };
 
 /*
tools/perf/util/symbol.h +1 -0
index ccd1caa..c35c3f0 100644
@@ -170,6 +170,7 @@ struct addr_map_symbol {
 	struct symbol *sym;
 	u64	      addr;
 	u64	      al_addr;
+	u64	      phys_addr;
 };
 
 struct branch_info {
Signature · 1 line
1.8.3.1
#11 lore raw
In reply to [email protected]
Quoted 119 lines · On Wed, Jan 6, 2016 at 3:04 AM, <[email protected]> wrote:

From: Kan Liang <[email protected]>

For understanding how the workload maps to memory channels and hardware behavior, it's useful to collect address maps with physical addresses. This is not intended for detecting page sharing (which can be already done using the mmap inode), but for lower level hardware behavior studies.
Perf supports load latency/DLA which can only collect virtual addresses. This patch add a new sample type PERF_SAMPLE_PHYS_ADDR to expose the physical addresses.
For kernel direct mapping addresses, the patch uses virt_to_phys to convert the virtual addresses from DLA to physical address. For user virtual addresses, __get_user_pages_fast is used to walk the pages tables for user physical address.
This does not work for vmalloc addresses. Right now these are not resolved, but code to do that could be added.
For security, the physical address can only be exposed to root or privileged user.

---
 arch/x86/kernel/cpu/perf_event.h          |  2 +-
 arch/x86/kernel/cpu/perf_event_intel_ds.c | 23 +++++++++++++++++++++++
 include/linux/perf_event.h                |  3 +++
 include/uapi/linux/perf_event.h           |  4 +++-
 kernel/events/core.c                      | 11 +++++++++++
 5 files changed, 41 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/perf_event.h +1 -1
index 799e6bd..164de68 100644
@@ -90,7 +90,7 @@ struct amd_nb {
        (PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_ADDR | \
        PERF_SAMPLE_ID | PERF_SAMPLE_CPU | PERF_SAMPLE_STREAM_ID | \
        PERF_SAMPLE_DATA_SRC | PERF_SAMPLE_IDENTIFIER | \
-       PERF_SAMPLE_TRANSACTION)
+       PERF_SAMPLE_TRANSACTION | PERF_SAMPLE_PHYS_ADDR)
 
 /*
  * A debug store configuration.
arch/x86/kernel/cpu/perf_event_intel_ds.c +23 -0
index 5db1c77..2e333dc 100644
@@ -986,6 +986,7 @@ static void setup_pebs_sample_data(struct perf_event *event,
        u64 sample_type;
        int fll, fst, dsrc;
        int fl = event->hw.flags;
+       struct page *p = NULL;
 
        if (pebs == NULL)
                return;
@@ -1071,6 +1072,28 @@ static void setup_pebs_sample_data(struct perf_event *event,
            x86_pmu.intel_cap.pebs_format >= 1)
                data->addr = pebs->dla;
 
+       if ((sample_type & PERF_SAMPLE_PHYS_ADDR) && (data->addr != 0)) {
+               if (data->addr >= TASK_SIZE) {
+                       /* If it's vmalloc()d memory, leave phys_addr as 0 */
+                       if (virt_addr_valid(data->addr) &&
+                           !(data->addr >= VMALLOC_START && data->addr < VMALLOC_END))
+                               data->phys_addr = (u64)virt_to_phys((void *)data->addr);
+               } else {
+                       /*
+                        * Walking the pages tables for user address.
+                        * Interrupts are disabled, so it prevents any tear down
+                        * of the page tables.
+                        * Try IRQ-safe __get_user_pages_fast first.
+                        * If failed, leave phys_addr as 0.
+                        */
+                       if ((current->mm != NULL) &&
+                           (__get_user_pages_fast(data->addr, 1, 0, &p) == 1))
+                               data->phys_addr = page_to_phys(p) + data->addr % PAGE_SIZE;
+
+                       if (p)
+                               put_page(p);
+               }
+       }
        if (x86_pmu.intel_cap.pebs_format >= 2) {
                /* Only set the TSX weight when no memory weight. */
                if ((sample_type & PERF_SAMPLE_WEIGHT) && !fll)
include/linux/perf_event.h +3 -0
index f9828a4..d9c0527 100644
@@ -795,6 +795,8 @@ struct perf_sample_data {
 
        struct perf_regs                regs_intr;
        u64                             stack_user_size;
+
+       u64                             phys_addr;
 } ____cacheline_aligned;
 
 /* default value for data source */
@@ -815,6 +817,7 @@ static inline void perf_sample_data_init(struct perf_sample_data *data,
        data->weight = 0;
        data->data_src.val = PERF_MEM_NA;
        data->txn = 0;
+       data->phys_addr = 0;
 }
 
 extern void perf_output_sample(struct perf_output_handle *handle,
include/uapi/linux/perf_event.h +2 -1
index 1afe962..5afc572 100644
@@ -139,8 +139,9 @@ enum perf_event_sample_format {
        PERF_SAMPLE_IDENTIFIER                  = 1U << 16,
        PERF_SAMPLE_TRANSACTION                 = 1U << 17,
        PERF_SAMPLE_REGS_INTR                   = 1U << 18,
+       PERF_SAMPLE_PHYS_ADDR                   = 1U << 19,
 
-       PERF_SAMPLE_MAX = 1U << 19,             /* non-ABI */
+       PERF_SAMPLE_MAX = 1U << 20,             /* non-ABI */
 };
 
 /*

include/uapi/linux/perf_event.h enum perf_event_type { · line 771

Quoted 4 lines
include/uapi/linux/perf_event.h +1 -0
@@ -767,6 +768,7 @@ enum perf_event_type {
         *      { u64                   transaction; } && PERF_SAMPLE_TRANSACTION
         *      { u64                   abi; # enum perf_sample_regs_abi
         *        u64                   regs[weight(mask)]; } && PERF_SAMPLE_REGS_INTR
+        *      { u64                   phys_addr;} && PERF_SAMPLE_PHYS_ADDR

If you say, PHYS_ADDR appears after SAMPLE_REGS_INTR then you have do respect that order in the perf_output_sample() routine, otherwise the tool cannot parse the record correctly.

Quoted 18 lines
include/uapi/linux/perf_event.h +0 -0
         * };
         */
        PERF_RECORD_SAMPLE                      = 9,
kernel/events/core.c +3 -0
index a627f36..9a922a2 100644
@@ -1334,6 +1334,9 @@ static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
        if (sample_type & PERF_SAMPLE_TRANSACTION)
                size += sizeof(data->txn);
 
+       if (sample_type & PERF_SAMPLE_PHYS_ADDR)
+               size += sizeof(data->phys_addr);
+
        event->header_size = size;
 }
 

kernel/events/core.c void perf_output_sample(struct perf_output_handle *handle, · line 5440

Quoted 6 lines
kernel/events/core.c +3 -0
@@ -5432,6 +5435,9 @@ void perf_output_sample(struct perf_output_handle *handle,
        if (sample_type & PERF_SAMPLE_DATA_SRC)
                perf_output_put(handle, data->data_src.val);
 
+       if (sample_type & PERF_SAMPLE_PHYS_ADDR)
+               perf_output_put(handle, data->phys_addr);
+

Need to order the put calls according to the order you define in the header file,
unless this logic has changed recently.

Quoted 18 lines
kernel/events/core.c +5 -1
        if (sample_type & PERF_SAMPLE_TRANSACTION)
                perf_output_put(handle, data->txn);
 
@@ -8269,6 +8275,11 @@ SYSCALL_DEFINE5(perf_event_open,
                        return -EINVAL;
        }
 
+       /* Only privileged users can get kernel addresses */
+       if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR) &&
+           !capable(CAP_SYS_ADMIN))
+               return -EACCES;
+
        /*
         * In cgroup mode, the pid argument is used to pass the fd
         * opened to the cgroup directory in cgroupfs. The cpu argument
--

1.8.3.1

#6 lore raw

Re: [PATCH 5/8] perf mem: report physical addresses

In reply to [email protected]
Quoted 31 lines · On Wed, Jan 06, 2016 at 06:04:34AM -0500, [email protected] wrote:

From: Kan Liang <[email protected]>

perf mem report should support physical addresses by applying -p or --phys-data. The default mem sort order for physical addresses is added accordingly.

---
 tools/perf/builtin-mem.c | 15 +++++++++++----
 tools/perf/util/sort.c   |  1 +
 tools/perf/util/sort.h   |  1 +
 3 files changed, 13 insertions(+), 4 deletions(-)
tools/perf/builtin-mem.c +1 -2
index 7d5ff3a..a81bdc2 100644
@@ -6,6 +6,7 @@
 #include "util/tool.h"
 #include "util/session.h"
 #include "util/data.h"
+#include "util/sort.h"
 
 #define MEM_OPERATION_LOAD	0x1
 #define MEM_OPERATION_STORE	0x2
@@ -176,9 +177,15 @@ static int report_events(int argc, const char **argv, struct perf_mem *mem)
 	 * there is no weight (cost) associated with stores, so don't print
 	 * the column
 	 */
-	if (!(mem->operation & MEM_OPERATION_LOAD))
-		rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"

tools/perf/builtin-mem.c static int report_events(int argc, const char **argv, struct perf_mem *mem) · line 188

Quoted 10 lines
tools/perf/builtin-mem.c +9 -1
-				"dso_daddr,tlb,locked";
+	if (!(mem->operation & MEM_OPERATION_LOAD)) {
+		if (mem->phys_addr)
+			rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
+					"dso_daddr,tlb,locked,phys_daddr";
+		else
+			rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
+					"dso_daddr,tlb,locked";
+	} else if (mem->phys_addr)
+		rep_argv[i++] = default_phys_mem_sort_order;

should this have "--sort=" as prefix ?

thanks,
jirka

#12 lore raw
In reply to [email protected]
Quoted 33 lines · On Wed, Jan 06, 2016 at 06:04:30AM -0500, [email protected] wrote:

From: Kan Liang <[email protected]>

For understanding how the workload maps to memory channels and hardware behavior, it's useful to collect address maps with physical addresses. This is not intended for detecting page sharing (which can be already done using the mmap inode), but for lower level hardware behavior studies.
Perf supports load latency/DLA which can only collect virtual addresses. This patch add a new sample type PERF_SAMPLE_PHYS_ADDR to expose the physical addresses.
For kernel direct mapping addresses, the patch uses virt_to_phys to convert the virtual addresses from DLA to physical address. For user virtual addresses, __get_user_pages_fast is used to walk the pages tables for user physical address.
This does not work for vmalloc addresses. Right now these are not resolved, but code to do that could be added.
For security, the physical address can only be exposed to root or privileged user.

---
 arch/x86/kernel/cpu/perf_event.h          |  2 +-
 arch/x86/kernel/cpu/perf_event_intel_ds.c | 23 +++++++++++++++++++++++
 include/linux/perf_event.h                |  3 +++
 include/uapi/linux/perf_event.h           |  4 +++-
 kernel/events/core.c                      | 11 +++++++++++
 5 files changed, 41 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/perf_event.h +0 -0
index 799e6bd..164de68 100644

arch/x86/kernel/cpu/perf_event.h struct amd_nb { · line 93

Quoted 5 lines
arch/x86/kernel/cpu/perf_event.h +1 -1
@@ -90,7 +90,7 @@ struct amd_nb {
 	(PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_ADDR | \
 	PERF_SAMPLE_ID | PERF_SAMPLE_CPU | PERF_SAMPLE_STREAM_ID | \
 	PERF_SAMPLE_DATA_SRC | PERF_SAMPLE_IDENTIFIER | \
-	PERF_SAMPLE_TRANSACTION)
+	PERF_SAMPLE_TRANSACTION | PERF_SAMPLE_PHYS_ADDR)

could you please add some probe code to detect kernel support, so we get some sensible error message

[jolsa@krava perf]$ ./perf mem -p record ls
Error:
The sys_perf_event_open() syscall returned with 22 (Invalid argument) for event (cpu/mem-loads/pp). /bin/dmesg may provide additional information.
No CONFIG_PERF_EVENTS=y kernel support configured?

thanks,
jirka

#9 lore raw

Re: [PATCH 7/8] perf script: support physical addresses in script

In reply to [email protected]
Quoted 32 lines · On Wed, Jan 06, 2016 at 06:04:36AM -0500, [email protected] wrote:

From: Kan Liang <[email protected]>

perf script print out physical addresses by applying phys_addr. Only display physical address when virtual address is selected. The physical address will be printed out right after virtual address.

---
 tools/perf/builtin-script.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
tools/perf/builtin-script.c +2 -0
index c298cdc..1e292a7 100644
@@ -58,6 +58,7 @@ enum perf_output_field {
 	PERF_OUTPUT_IREGS	    = 1U << 14,
 	PERF_OUTPUT_BRSTACK	    = 1U << 15,
 	PERF_OUTPUT_BRSTACKSYM	    = 1U << 16,
+	PERF_OUTPUT_PHYS_ADDR	    = 1U << 17,
 };
 
 struct output_option {
@@ -81,6 +82,7 @@ struct output_option {
 	{.str = "iregs", .field = PERF_OUTPUT_IREGS},
 	{.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
 	{.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
+	{.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
 };
 
 /* default set to maintain compatibility with current format */

tools/perf/builtin-script.c static int perf_evsel__check_attr(struct perf_evsel *evsel, · line 251

Quoted 8 lines
tools/perf/builtin-script.c +5 -0
@@ -242,6 +244,12 @@ static int perf_evsel__check_attr(struct perf_evsel *evsel,
 					   PERF_OUTPUT_ADDR, allow_user_set))
 		return -EINVAL;
 
+	if (PRINT_FIELD(PHYS_ADDR) && !PRINT_FIELD(ADDR)) {
+		pr_err("Display of sample physical address"
+		       "but sample address is not selected.\n");
+		return -EINVAL;
+	}

hum, why the ADDR matter in here? we have PHYS_ADDR already stored in perf.data so we should be all set to display it

[jolsa@ibm-x3650m4-01 perf]$ ./perf script -F addr,phys_addr | head -3

ffff8804744e5de9       4744e5de9
ffff88047fca5e90       47fca5e90
ffff8804710cfcb8       4710cfcb8

[jolsa@ibm-x3650m4-01 perf]$ ./perf script -F phys_addr Display of sample physical addressbut sample address is not selected.

also you need to add extra space behind 'address':

thanks,
jirka

#2 lore raw

Re: [PATCH 2/8] perf tools: add option to record sample physical address

In reply to [email protected]

On Wed, Jan 06, 2016 at 06:04:31AM -0500, [email protected] wrote:

SNIP

tools/perf/util/session.c static void dump_sample(struct perf_evsel *evsel, union perf_event *event, · line 1126

Quoted 10 lines
tools/perf/util/session.c +2 -0
index d5636ba..670ff4f 100644
@@ -1122,6 +1122,9 @@ static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
 	if (sample_type & PERF_SAMPLE_DATA_SRC)
 		printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
 
+	if (sample_type & PERF_SAMPLE_PHYS_ADDR)
+		printf(" .. phys_addr: 0x%"PRIx64"\n", sample->phys_addr);

could you also please add PERF_SAMPLE_PHYS_ADDR __p_sample_type so perf evlist displays it?

thanks,
jirka

author
#13 lore raw
In reply to Jiri Olsa
Quoted 43 lines · On Wed, Jan 06, 2016 at 06:04:30AM -0500, [email protected] wrote:
Quoted 4 lines

From: Kan Liang <[email protected]>

For understanding how the workload maps to memory channels and hardware behavior, it's useful to collect address maps with physical

addresses.

Quoted 5 lines

This is not intended for detecting page sharing (which can be already done using the mmap inode), but for lower level hardware behavior studies.
Perf supports load latency/DLA which can only collect virtual addresses. This patch add a new sample type PERF_SAMPLE_PHYS_ADDR to expose

the

Quoted 10 lines

physical addresses.
For kernel direct mapping addresses, the patch uses virt_to_phys to convert the virtual addresses from DLA to physical address. For user virtual addresses, __get_user_pages_fast is used to walk the pages tables for user physical address.
This does not work for vmalloc addresses. Right now these are not resolved, but code to do that could be added.
For security, the physical address can only be exposed to root or privileged user.

Quoted 4 lines
---
 arch/x86/kernel/cpu/perf_event.h          |  2 +-
 arch/x86/kernel/cpu/perf_event_intel_ds.c | 23

+++++++++++++++++++++++

arch/x86/kernel/cpu/perf_event.h struct amd_nb { · line 91

Quoted 13 lines
 include/linux/perf_event.h                |  3 +++
 include/uapi/linux/perf_event.h           |  4 +++-
 kernel/events/core.c                      | 11 +++++++++++
 5 files changed, 41 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/perf_event.h +0 -0
index 799e6bd..164de68 100644
@@ -90,7 +90,7 @@ struct amd_nb {
 	(PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_ADDR | \
 	PERF_SAMPLE_ID | PERF_SAMPLE_CPU |

PERF_SAMPLE_STREAM_ID | \

Quoted 7 lines

arch/x86/kernel/cpu/perf_event.h struct amd_nb { · line 93

Quoted 3 lines
arch/x86/kernel/cpu/perf_event.h +1 -1
 	PERF_SAMPLE_DATA_SRC | PERF_SAMPLE_IDENTIFIER | \
-	PERF_SAMPLE_TRANSACTION)
+	PERF_SAMPLE_TRANSACTION | PERF_SAMPLE_PHYS_ADDR)

could you please add some probe code to detect kernel support, so we get some sensible error message

I'm not quite sure what kind of probe code do you mean.
Yes, the error message below is not accurate. It could be some errors in sample_type, read_format, branch_sample_type or something else. Because they share the same error code -EINVAL. Perf tool cannot know more.
I think we may print some error kernel messages when invalid attr is detected. So the user can get more detail messages from dmesg.

Or you want me to add some extra code to detect the kernel capability before opening event? If so, I think we may do it in separate patches.

Thanks,
Kan

Quoted 9 lines

[jolsa@krava perf]$ ./perf mem -p record ls
Error:
The sys_perf_event_open() syscall returned with 22 (Invalid argument) for event (cpu/mem-loads/pp).
/bin/dmesg may provide additional information.
No CONFIG_PERF_EVENTS=y kernel support configured?

thanks,
jirka

#14 lore raw
Quoted 59 lines · Kan
Quoted 43 lines · On Wed, Jan 06, 2016 at 06:04:30AM -0500, [email protected] wrote:
Quoted 4 lines

From: Kan Liang <[email protected]>

For understanding how the workload maps to memory channels and hardware behavior, it's useful to collect address maps with physical

addresses.

Quoted 5 lines

This is not intended for detecting page sharing (which can be already done using the mmap inode), but for lower level hardware behavior studies.
Perf supports load latency/DLA which can only collect virtual addresses. This patch add a new sample type PERF_SAMPLE_PHYS_ADDR to expose

the

Quoted 10 lines

physical addresses.
For kernel direct mapping addresses, the patch uses virt_to_phys to convert the virtual addresses from DLA to physical address. For user virtual addresses, __get_user_pages_fast is used to walk the pages tables for user physical address.
This does not work for vmalloc addresses. Right now these are not resolved, but code to do that could be added.
For security, the physical address can only be exposed to root or privileged user.

Quoted 4 lines
---
 arch/x86/kernel/cpu/perf_event.h          |  2 +-
 arch/x86/kernel/cpu/perf_event_intel_ds.c | 23

+++++++++++++++++++++++

arch/x86/kernel/cpu/perf_event.h struct amd_nb { · line 91

Quoted 13 lines
 include/linux/perf_event.h                |  3 +++
 include/uapi/linux/perf_event.h           |  4 +++-
 kernel/events/core.c                      | 11 +++++++++++
 5 files changed, 41 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/perf_event.h +0 -0
index 799e6bd..164de68 100644
@@ -90,7 +90,7 @@ struct amd_nb {
 	(PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_ADDR | \
 	PERF_SAMPLE_ID | PERF_SAMPLE_CPU |

PERF_SAMPLE_STREAM_ID | \

Quoted 7 lines

arch/x86/kernel/cpu/perf_event.h struct amd_nb { · line 93

Quoted 3 lines
arch/x86/kernel/cpu/perf_event.h +1 -1
 	PERF_SAMPLE_DATA_SRC | PERF_SAMPLE_IDENTIFIER | \
-	PERF_SAMPLE_TRANSACTION)
+	PERF_SAMPLE_TRANSACTION | PERF_SAMPLE_PHYS_ADDR)

could you please add some probe code to detect kernel support, so we get some sensible error message

I'm not quite sure what kind of probe code do you mean.
Yes, the error message below is not accurate. It could be some errors in sample_type, read_format, branch_sample_type or something else. Because they share the same error code -EINVAL. Perf tool cannot know more.
I think we may print some error kernel messages when invalid attr is detected. So the user can get more detail messages from dmesg.

Quoted 2 lines

Or you want me to add some extra code to detect the kernel capability before opening event? If so, I think we may do it in separate patches.

yep, could be done later

jirka

#15 lore raw
In reply to [email protected]
Quoted 19 lines · On Wed, Jan 6, 2016 at 3:04 AM, <[email protected]> wrote:

From: Kan Liang <[email protected]>

For understanding how the workload maps to memory channels and hardware behavior, it's useful to collect address maps with physical addresses. This is not intended for detecting page sharing (which can be already done using the mmap inode), but for lower level hardware behavior studies.
Perf supports load latency/DLA which can only collect virtual addresses. This patch add a new sample type PERF_SAMPLE_PHYS_ADDR to expose the physical addresses.
For kernel direct mapping addresses, the patch uses virt_to_phys to convert the virtual addresses from DLA to physical address. For user virtual addresses, __get_user_pages_fast is used to walk the pages tables for user physical address.
This does not work for vmalloc addresses. Right now these are not resolved, but code to do that could be added.
For security, the physical address can only be exposed to root or privileged user.

The other limitation of your patch series is that it is providing physical address for data only. It does not cover code, i.e., it is not possible to obtain the physical address of a sampled IP. So maybe the new sample_type should be renamed to PERF_SAMPLE_DATA_PHYS_ADDR instead.
Should you add physical addresses for IP (no need for PEBS for this), then you'd have to have a new name, anyway.

Getting code and data physical addresses would provide better coverage of the memory subsystem usage.

Quoted 146 lines
---
 arch/x86/kernel/cpu/perf_event.h          |  2 +-
 arch/x86/kernel/cpu/perf_event_intel_ds.c | 23 +++++++++++++++++++++++
 include/linux/perf_event.h                |  3 +++
 include/uapi/linux/perf_event.h           |  4 +++-
 kernel/events/core.c                      | 11 +++++++++++
 5 files changed, 41 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/perf_event.h +1 -1
index 799e6bd..164de68 100644
@@ -90,7 +90,7 @@ struct amd_nb {
        (PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_ADDR | \
        PERF_SAMPLE_ID | PERF_SAMPLE_CPU | PERF_SAMPLE_STREAM_ID | \
        PERF_SAMPLE_DATA_SRC | PERF_SAMPLE_IDENTIFIER | \
-       PERF_SAMPLE_TRANSACTION)
+       PERF_SAMPLE_TRANSACTION | PERF_SAMPLE_PHYS_ADDR)
 
 /*
  * A debug store configuration.
arch/x86/kernel/cpu/perf_event_intel_ds.c +23 -0
index 5db1c77..2e333dc 100644
@@ -986,6 +986,7 @@ static void setup_pebs_sample_data(struct perf_event *event,
        u64 sample_type;
        int fll, fst, dsrc;
        int fl = event->hw.flags;
+       struct page *p = NULL;
 
        if (pebs == NULL)
                return;
@@ -1071,6 +1072,28 @@ static void setup_pebs_sample_data(struct perf_event *event,
            x86_pmu.intel_cap.pebs_format >= 1)
                data->addr = pebs->dla;
 
+       if ((sample_type & PERF_SAMPLE_PHYS_ADDR) && (data->addr != 0)) {
+               if (data->addr >= TASK_SIZE) {
+                       /* If it's vmalloc()d memory, leave phys_addr as 0 */
+                       if (virt_addr_valid(data->addr) &&
+                           !(data->addr >= VMALLOC_START && data->addr < VMALLOC_END))
+                               data->phys_addr = (u64)virt_to_phys((void *)data->addr);
+               } else {
+                       /*
+                        * Walking the pages tables for user address.
+                        * Interrupts are disabled, so it prevents any tear down
+                        * of the page tables.
+                        * Try IRQ-safe __get_user_pages_fast first.
+                        * If failed, leave phys_addr as 0.
+                        */
+                       if ((current->mm != NULL) &&
+                           (__get_user_pages_fast(data->addr, 1, 0, &p) == 1))
+                               data->phys_addr = page_to_phys(p) + data->addr % PAGE_SIZE;
+
+                       if (p)
+                               put_page(p);
+               }
+       }
        if (x86_pmu.intel_cap.pebs_format >= 2) {
                /* Only set the TSX weight when no memory weight. */
                if ((sample_type & PERF_SAMPLE_WEIGHT) && !fll)
include/linux/perf_event.h +3 -0
index f9828a4..d9c0527 100644
@@ -795,6 +795,8 @@ struct perf_sample_data {
 
        struct perf_regs                regs_intr;
        u64                             stack_user_size;
+
+       u64                             phys_addr;
 } ____cacheline_aligned;
 
 /* default value for data source */
@@ -815,6 +817,7 @@ static inline void perf_sample_data_init(struct perf_sample_data *data,
        data->weight = 0;
        data->data_src.val = PERF_MEM_NA;
        data->txn = 0;
+       data->phys_addr = 0;
 }
 
 extern void perf_output_sample(struct perf_output_handle *handle,
include/uapi/linux/perf_event.h +3 -1
index 1afe962..5afc572 100644
@@ -139,8 +139,9 @@ enum perf_event_sample_format {
        PERF_SAMPLE_IDENTIFIER                  = 1U << 16,
        PERF_SAMPLE_TRANSACTION                 = 1U << 17,
        PERF_SAMPLE_REGS_INTR                   = 1U << 18,
+       PERF_SAMPLE_PHYS_ADDR                   = 1U << 19,
 
-       PERF_SAMPLE_MAX = 1U << 19,             /* non-ABI */
+       PERF_SAMPLE_MAX = 1U << 20,             /* non-ABI */
 };
 
 /*
@@ -767,6 +768,7 @@ enum perf_event_type {
         *      { u64                   transaction; } && PERF_SAMPLE_TRANSACTION
         *      { u64                   abi; # enum perf_sample_regs_abi
         *        u64                   regs[weight(mask)]; } && PERF_SAMPLE_REGS_INTR
+        *      { u64                   phys_addr;} && PERF_SAMPLE_PHYS_ADDR
         * };
         */
        PERF_RECORD_SAMPLE                      = 9,
kernel/events/core.c +11 -1
index a627f36..9a922a2 100644
@@ -1334,6 +1334,9 @@ static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
        if (sample_type & PERF_SAMPLE_TRANSACTION)
                size += sizeof(data->txn);
 
+       if (sample_type & PERF_SAMPLE_PHYS_ADDR)
+               size += sizeof(data->phys_addr);
+
        event->header_size = size;
 }
 
@@ -5432,6 +5435,9 @@ void perf_output_sample(struct perf_output_handle *handle,
        if (sample_type & PERF_SAMPLE_DATA_SRC)
                perf_output_put(handle, data->data_src.val);
 
+       if (sample_type & PERF_SAMPLE_PHYS_ADDR)
+               perf_output_put(handle, data->phys_addr);
+
        if (sample_type & PERF_SAMPLE_TRANSACTION)
                perf_output_put(handle, data->txn);
 
@@ -8269,6 +8275,11 @@ SYSCALL_DEFINE5(perf_event_open,
                        return -EINVAL;
        }
 
+       /* Only privileged users can get kernel addresses */
+       if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR) &&
+           !capable(CAP_SYS_ADMIN))
+               return -EACCES;
+
        /*
         * In cgroup mode, the pid argument is used to pass the fd
         * opened to the cgroup directory in cgroupfs. The cpu argument
--

1.8.3.1

author
#16 lore raw
Quoted 24 lines · On Wed, Jan 6, 2016 at 3:04 AM, <[email protected]> wrote:
Quoted 4 lines

From: Kan Liang <[email protected]>

For understanding how the workload maps to memory channels and hardware behavior, it's useful to collect address maps with physical

addresses.

Quoted 5 lines

This is not intended for detecting page sharing (which can be already done using the mmap inode), but for lower level hardware behavior studies.
Perf supports load latency/DLA which can only collect virtual addresses. This patch add a new sample type PERF_SAMPLE_PHYS_ADDR to expose

the

Quoted 10 lines

physical addresses.
For kernel direct mapping addresses, the patch uses virt_to_phys to convert the virtual addresses from DLA to physical address. For user virtual addresses, __get_user_pages_fast is used to walk the pages tables for user physical address.
This does not work for vmalloc addresses. Right now these are not resolved, but code to do that could be added.
For security, the physical address can only be exposed to root or privileged user.

Quoted 5 lines

The other limitation of your patch series is that it is providing physical address for data only. It does not cover code, i.e., it is not possible to obtain the physical address of a sampled IP. So maybe the new sample_type should be renamed to PERF_SAMPLE_DATA_PHYS_ADDR instead.

We use sample_type PERF_SAMPLE_ADDR for the data virtual address. So in this patch I choose a similar name PERF_SAMPLE_PHYS_ADDR for data physical address. I think it should not lead to ambiguity.

Quoted 2 lines

Should you add physical addresses for IP (no need for PEBS for this), then you'd have to have a new name, anyway.

We use another sample_type PERF_SAMPLE_IP to get data->ip.
So I think we may name PERF_SAMPLE_PHYS_IP as the new sample_type for the physical addresses of IP
Also, since code and data are from different sample_type, I think it may be better to implement the PERF_SAMPLE_PHYS_IP in a separate patch later.

Thanks,
Kan

Quoted 165 lines

Getting code and data physical addresses would provide better coverage of the memory subsystem usage.

Quoted 4 lines
---
 arch/x86/kernel/cpu/perf_event.h          |  2 +-
 arch/x86/kernel/cpu/perf_event_intel_ds.c | 23

+++++++++++++++++++++++

arch/x86/kernel/cpu/perf_event.h struct amd_nb { · line 91

Quoted 13 lines
 include/linux/perf_event.h                |  3 +++
 include/uapi/linux/perf_event.h           |  4 +++-
 kernel/events/core.c                      | 11 +++++++++++
 5 files changed, 41 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/perf_event.h +0 -0
index 799e6bd..164de68 100644
@@ -90,7 +90,7 @@ struct amd_nb {
        (PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_ADDR | \
        PERF_SAMPLE_ID | PERF_SAMPLE_CPU |

PERF_SAMPLE_STREAM_ID | \

arch/x86/kernel/cpu/perf_event_intel_ds.c static void setup_pebs_sample_data(struct

Quoted 12 lines
arch/x86/kernel/cpu/perf_event.h +1 -1
        PERF_SAMPLE_DATA_SRC | PERF_SAMPLE_IDENTIFIER | \
-       PERF_SAMPLE_TRANSACTION)
+       PERF_SAMPLE_TRANSACTION | PERF_SAMPLE_PHYS_ADDR)
 
 /*
  * A debug store configuration.
arch/x86/kernel/cpu/perf_event_intel_ds.c +0 -0
index 5db1c77..2e333dc 100644
@@ -986,6 +986,7 @@ static void setup_pebs_sample_data(struct

perf_event *event,

arch/x86/kernel/cpu/perf_event_intel_ds.c static void setup_pebs_sample_data(struct

Quoted 8 lines
arch/x86/kernel/cpu/perf_event_intel_ds.c +1 -0
        u64 sample_type;
        int fll, fst, dsrc;
        int fl = event->hw.flags;
+       struct page *p = NULL;
 
        if (pebs == NULL)
                return;
@@ -1071,6 +1072,28 @@ static void setup_pebs_sample_data(struct

perf_event *event,

arch/x86/kernel/cpu/perf_event_intel_ds.c static void setup_pebs_sample_data(struct perf_event *event,

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_ds.c +1 -0
            x86_pmu.intel_cap.pebs_format >= 1)
                data->addr = pebs->dla;
 
+       if ((sample_type & PERF_SAMPLE_PHYS_ADDR) && (data->addr !=

0)) {

arch/x86/kernel/cpu/perf_event_intel_ds.c static void setup_pebs_sample_data(struct perf_event *event,

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_ds.c +4 -0
+               if (data->addr >= TASK_SIZE) {
+                       /* If it's vmalloc()d memory, leave phys_addr as 0 */
+                       if (virt_addr_valid(data->addr) &&
+                           !(data->addr >= VMALLOC_START && data->addr <

VMALLOC_END))

include/linux/perf_event.h static inline void perf_sample_data_init(struct

Quoted 36 lines
arch/x86/kernel/cpu/perf_event_intel_ds.c +18 -1
+                               data->phys_addr = (u64)virt_to_phys((void *)data-
 addr);
+               } else {
+                       /*
+                        * Walking the pages tables for user address.
+                        * Interrupts are disabled, so it prevents any tear down
+                        * of the page tables.
+                        * Try IRQ-safe __get_user_pages_fast first.
+                        * If failed, leave phys_addr as 0.
+                        */
+                       if ((current->mm != NULL) &&
+                           (__get_user_pages_fast(data->addr, 1, 0, &p) == 1))
+                               data->phys_addr = page_to_phys(p) +
+ data->addr % PAGE_SIZE;
+
+                       if (p)
+                               put_page(p);
+               }
+       }
        if (x86_pmu.intel_cap.pebs_format >= 2) {
                /* Only set the TSX weight when no memory weight. */
                if ((sample_type & PERF_SAMPLE_WEIGHT) && !fll) diff
--git a/include/linux/perf_event.h b/include/linux/perf_event.h index

f9828a4..d9c0527 100644

include/linux/perf_event.h +2 -0
@@ -795,6 +795,8 @@ struct perf_sample_data {
 
        struct perf_regs                regs_intr;
        u64                             stack_user_size;
+
+       u64                             phys_addr;
 } ____cacheline_aligned;
 
 /* default value for data source */
@@ -815,6 +817,7 @@ static inline void perf_sample_data_init(struct

perf_sample_data *data,

Quoted 23 lines
include/linux/perf_event.h +1 -0
        data->weight = 0;
        data->data_src.val = PERF_MEM_NA;
        data->txn = 0;
+       data->phys_addr = 0;
 }
 
 extern void perf_output_sample(struct perf_output_handle *handle,
(quoted diff) +0 -0

b/include/uapi/linux/perf_event.h index 1afe962..5afc572 100644

include/uapi/linux/perf_event.h +2 -1
@@ -139,8 +139,9 @@ enum perf_event_sample_format {
        PERF_SAMPLE_IDENTIFIER                  = 1U << 16,
        PERF_SAMPLE_TRANSACTION                 = 1U << 17,
        PERF_SAMPLE_REGS_INTR                   = 1U << 18,
+       PERF_SAMPLE_PHYS_ADDR                   = 1U << 19,
 
-       PERF_SAMPLE_MAX = 1U << 19,             /* non-ABI */
+       PERF_SAMPLE_MAX = 1U << 20,             /* non-ABI */
 };
 
 /*

include/uapi/linux/perf_event.h enum perf_event_type { · line 770

Quoted 3 lines
include/uapi/linux/perf_event.h +0 -0
@@ -767,6 +768,7 @@ enum perf_event_type {
         *      { u64                   transaction; } && PERF_SAMPLE_TRANSACTION
         *      { u64                   abi; # enum perf_sample_regs_abi
         *        u64                   regs[weight(mask)]; } &&

PERF_SAMPLE_REGS_INTR

kernel/events/core.c static void __perf_event_header_size(struct

Quoted 9 lines
include/uapi/linux/perf_event.h +1 -0
+        *      { u64                   phys_addr;} && PERF_SAMPLE_PHYS_ADDR
         * };
         */
        PERF_RECORD_SAMPLE                      = 9,
kernel/events/core.c index renamed from kernel/events/core.c +0 -0

a627f36..9a922a2 100644

kernel/events/core.c +0 -0
@@ -1334,6 +1334,9 @@ static void __perf_event_header_size(struct

perf_event *event, u64 sample_type)

kernel/events/core.c void perf_output_sample(struct

Quoted 10 lines
kernel/events/core.c +3 -0
        if (sample_type & PERF_SAMPLE_TRANSACTION)
                size += sizeof(data->txn);
 
+       if (sample_type & PERF_SAMPLE_PHYS_ADDR)
+               size += sizeof(data->phys_addr);
+
        event->header_size = size;
 }
 
@@ -5432,6 +5435,9 @@ void perf_output_sample(struct

perf_output_handle *handle,

Quoted 25 lines
kernel/events/core.c +8 -0
        if (sample_type & PERF_SAMPLE_DATA_SRC)
                perf_output_put(handle, data->data_src.val);
 
+       if (sample_type & PERF_SAMPLE_PHYS_ADDR)
+               perf_output_put(handle, data->phys_addr);
+
        if (sample_type & PERF_SAMPLE_TRANSACTION)
                perf_output_put(handle, data->txn);
 
@@ -8269,6 +8275,11 @@ SYSCALL_DEFINE5(perf_event_open,
                        return -EINVAL;
        }
 
+       /* Only privileged users can get kernel addresses */
+       if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR) &&
+           !capable(CAP_SYS_ADMIN))
+               return -EACCES;
+
        /*
         * In cgroup mode, the pid argument is used to pass the fd
         * opened to the cgroup directory in cgroupfs. The cpu

argument
--
1.8.3.1