Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1302968 > unrolled thread

[PATCH 1/8] perf: Add PERF_SAMPLE_PHYS_ADDR

Started bykan.liang@intel.com
First post2016-01-06 19:30 +0100
Last post2016-01-08 22:20 +0100
Articles 11 — 4 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH 1/8] perf: Add PERF_SAMPLE_PHYS_ADDR kan.liang@intel.com - 2016-01-06 19:30 +0100
    [PATCH 8/8] perf test: add test case for PERF_SAMPLE_PHYS_ADDR kan.liang@intel.com - 2016-01-06 19:30 +0100
    [PATCH 4/8] perf mem: add option phys-data to record physical address kan.liang@intel.com - 2016-01-06 19:30 +0100
    [PATCH 7/8] perf script: support physical addresses in script kan.liang@intel.com - 2016-01-06 19:30 +0100
      Re: [PATCH 7/8] perf script: support physical addresses in script Jiri Olsa <jolsa@redhat.com> - 2016-01-07 11:00 +0100
    Re: [PATCH 1/8] perf: Add PERF_SAMPLE_PHYS_ADDR Stephane Eranian <eranian@google.com> - 2016-01-06 20:30 +0100
    Re: [PATCH 1/8] perf: Add PERF_SAMPLE_PHYS_ADDR Jiri Olsa <jolsa@redhat.com> - 2016-01-07 09:40 +0100
      RE: [PATCH 1/8] perf: Add PERF_SAMPLE_PHYS_ADDR "Liang, Kan" <kan.liang@intel.com> - 2016-01-07 17:00 +0100
        Re: [PATCH 1/8] perf: Add PERF_SAMPLE_PHYS_ADDR Jiri Olsa <jolsa@redhat.com> - 2016-01-07 20:40 +0100
    Re: [PATCH 1/8] perf: Add PERF_SAMPLE_PHYS_ADDR Stephane Eranian <eranian@google.com> - 2016-01-07 23:00 +0100
      RE: [PATCH 1/8] perf: Add PERF_SAMPLE_PHYS_ADDR "Liang, Kan" <kan.liang@intel.com> - 2016-01-08 22:20 +0100

#1302968 — [PATCH 1/8] perf: Add PERF_SAMPLE_PHYS_ADDR

Fromkan.liang@intel.com
Date2016-01-06 19:30 +0100
Subject[PATCH 1/8] perf: Add PERF_SAMPLE_PHYS_ADDR
Message-ID<qO10l-4M5-3@gated-at.bofh.it>
From: Kan Liang <kan.liang@intel.com>

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.

Signed-off-by: Kan Liang <kan.liang@intel.com>
---
 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(-)

diff --git a/arch/x86/kernel/cpu/perf_event.h b/arch/x86/kernel/cpu/perf_event.h
index 799e6bd..164de68 100644
--- a/arch/x86/kernel/cpu/perf_event.h
+++ b/arch/x86/kernel/cpu/perf_event.h
@@ -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.
diff --git a/arch/x86/kernel/cpu/perf_event_intel_ds.c b/arch/x86/kernel/cpu/perf_event_intel_ds.c
index 5db1c77..2e333dc 100644
--- a/arch/x86/kernel/cpu/perf_event_intel_ds.c
+++ b/arch/x86/kernel/cpu/perf_event_intel_ds.c
@@ -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)
diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index f9828a4..d9c0527 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -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,
diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h
index 1afe962..5afc572 100644
--- a/include/uapi/linux/perf_event.h
+++ b/include/uapi/linux/perf_event.h
@@ -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,
diff --git a/kernel/events/core.c b/kernel/events/core.c
index a627f36..9a922a2 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -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

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [next] | [standalone]


#1302970 — [PATCH 8/8] perf test: add test case for PERF_SAMPLE_PHYS_ADDR

Fromkan.liang@intel.com
Date2016-01-06 19:30 +0100
Subject[PATCH 8/8] perf test: add test case for PERF_SAMPLE_PHYS_ADDR
Message-ID<qO10n-4M5-45@gated-at.bofh.it>
In reply to#1302968
From: Kan Liang <kan.liang@intel.com>

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

Signed-off-by: Kan Liang <kan.liang@intel.com>
---
 tools/perf/tests/sample-parsing.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/tools/perf/tests/sample-parsing.c b/tools/perf/tests/sample-parsing.c
index 5f23710..8813fa1 100644
--- a/tools/perf/tests/sample-parsing.c
+++ b/tools/perf/tests/sample-parsing.c
@@ -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;
 	}
-- 
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1302973 — [PATCH 4/8] perf mem: add option phys-data to record physical address

Fromkan.liang@intel.com
Date2016-01-06 19:30 +0100
Subject[PATCH 4/8] perf mem: add option phys-data to record physical address
Message-ID<qO10o-4M5-49@gated-at.bofh.it>
In reply to#1302968
From: Kan Liang <kan.liang@intel.com>

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

Signed-off-by: Kan Liang <kan.liang@intel.com>
---
 tools/perf/builtin-mem.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/tools/perf/builtin-mem.c b/tools/perf/builtin-mem.c
index 3901700..7d5ff3a 100644
--- a/tools/perf/builtin-mem.c
+++ b/tools/perf/builtin-mem.c
@@ -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 };
-- 
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1302974 — [PATCH 7/8] perf script: support physical addresses in script

Fromkan.liang@intel.com
Date2016-01-06 19:30 +0100
Subject[PATCH 7/8] perf script: support physical addresses in script
Message-ID<qO10o-4M5-47@gated-at.bofh.it>
In reply to#1302968
From: Kan Liang <kan.liang@intel.com>

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.

Signed-off-by: Kan Liang <kan.liang@intel.com>
---
 tools/perf/builtin-script.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index c298cdc..1e292a7 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -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...]",
-- 
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


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

FromJiri Olsa <jolsa@redhat.com>
Date2016-01-07 11:00 +0100
SubjectRe: [PATCH 7/8] perf script: support physical addresses in script
Message-ID<qOfwn-6lG-19@gated-at.bofh.it>
In reply to#1302974
On Wed, Jan 06, 2016 at 06:04:36AM -0500, kan.liang@intel.com wrote:
> From: Kan Liang <kan.liang@intel.com>
> 
> 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.
> 
> Signed-off-by: Kan Liang <kan.liang@intel.com>
> ---
>  tools/perf/builtin-script.c | 19 ++++++++++++++++++-
>  1 file changed, 18 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
> index c298cdc..1e292a7 100644
> --- a/tools/perf/builtin-script.c
> +++ b/tools/perf/builtin-script.c
> @@ -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;
> +	}

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
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1303016

FromStephane Eranian <eranian@google.com>
Date2016-01-06 20:30 +0100
Message-ID<qO1Wq-5oZ-13@gated-at.bofh.it>
In reply to#1302968
On Wed, Jan 6, 2016 at 3:04 AM,  <kan.liang@intel.com> wrote:
> From: Kan Liang <kan.liang@intel.com>
>
> 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.
>
> Signed-off-by: Kan Liang <kan.liang@intel.com>
> ---
>  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(-)
>
> diff --git a/arch/x86/kernel/cpu/perf_event.h b/arch/x86/kernel/cpu/perf_event.h
> index 799e6bd..164de68 100644
> --- a/arch/x86/kernel/cpu/perf_event.h
> +++ b/arch/x86/kernel/cpu/perf_event.h
> @@ -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.
> diff --git a/arch/x86/kernel/cpu/perf_event_intel_ds.c b/arch/x86/kernel/cpu/perf_event_intel_ds.c
> index 5db1c77..2e333dc 100644
> --- a/arch/x86/kernel/cpu/perf_event_intel_ds.c
> +++ b/arch/x86/kernel/cpu/perf_event_intel_ds.c
> @@ -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)
> diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
> index f9828a4..d9c0527 100644
> --- a/include/linux/perf_event.h
> +++ b/include/linux/perf_event.h
> @@ -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,
> diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h
> index 1afe962..5afc572 100644
> --- a/include/uapi/linux/perf_event.h
> +++ b/include/uapi/linux/perf_event.h
> @@ -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

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.

>          * };
>          */
>         PERF_RECORD_SAMPLE                      = 9,
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index a627f36..9a922a2 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -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);
> +
Need to order the put calls according to the order you define in the
header file,
unless this logic has changed recently.

>         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
>
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1303367

FromJiri Olsa <jolsa@redhat.com>
Date2016-01-07 09:40 +0100
Message-ID<qOegV-5zh-1@gated-at.bofh.it>
In reply to#1302968
On Wed, Jan 06, 2016 at 06:04:30AM -0500, kan.liang@intel.com wrote:
> From: Kan Liang <kan.liang@intel.com>
> 
> 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.
> 
> Signed-off-by: Kan Liang <kan.liang@intel.com>
> ---
>  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(-)
> 
> diff --git a/arch/x86/kernel/cpu/perf_event.h b/arch/x86/kernel/cpu/perf_event.h
> index 799e6bd..164de68 100644
> --- a/arch/x86/kernel/cpu/perf_event.h
> +++ b/arch/x86/kernel/cpu/perf_event.h
> @@ -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
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1303706

From"Liang, Kan" <kan.liang@intel.com>
Date2016-01-07 17:00 +0100
Message-ID<qOl8M-1I5-71@gated-at.bofh.it>
In reply to#1303367

> On Wed, Jan 06, 2016 at 06:04:30AM -0500, kan.liang@intel.com wrote:
> > From: Kan Liang <kan.liang@intel.com>
> >
> > 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.
> >
> > Signed-off-by: Kan Liang <kan.liang@intel.com>
> > ---
> >  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(-)
> >
> > diff --git a/arch/x86/kernel/cpu/perf_event.h
> > b/arch/x86/kernel/cpu/perf_event.h
> > index 799e6bd..164de68 100644
> > --- a/arch/x86/kernel/cpu/perf_event.h
> > +++ b/arch/x86/kernel/cpu/perf_event.h
> > @@ -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
> 

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

> [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
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1303845

FromJiri Olsa <jolsa@redhat.com>
Date2016-01-07 20:40 +0100
Message-ID<qOozE-4bb-17@gated-at.bofh.it>
In reply to#1303706
On Thu, Jan 07, 2016 at 03:55:23PM +0000, Liang, Kan wrote:
> 
> 
> > On Wed, Jan 06, 2016 at 06:04:30AM -0500, kan.liang@intel.com wrote:
> > > From: Kan Liang <kan.liang@intel.com>
> > >
> > > 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.
> > >
> > > Signed-off-by: Kan Liang <kan.liang@intel.com>
> > > ---
> > >  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(-)
> > >
> > > diff --git a/arch/x86/kernel/cpu/perf_event.h
> > > b/arch/x86/kernel/cpu/perf_event.h
> > > index 799e6bd..164de68 100644
> > > --- a/arch/x86/kernel/cpu/perf_event.h
> > > +++ b/arch/x86/kernel/cpu/perf_event.h
> > > @@ -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
> > 
> 
> 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.

yep, could be done later

jirka

[toc] | [prev] | [next] | [standalone]


#1303939

FromStephane Eranian <eranian@google.com>
Date2016-01-07 23:00 +0100
Message-ID<qOqLa-5yd-53@gated-at.bofh.it>
In reply to#1302968
On Wed, Jan 6, 2016 at 3:04 AM,  <kan.liang@intel.com> wrote:
> From: Kan Liang <kan.liang@intel.com>
>
> 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.

> Signed-off-by: Kan Liang <kan.liang@intel.com>
> ---
>  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(-)
>
> diff --git a/arch/x86/kernel/cpu/perf_event.h b/arch/x86/kernel/cpu/perf_event.h
> index 799e6bd..164de68 100644
> --- a/arch/x86/kernel/cpu/perf_event.h
> +++ b/arch/x86/kernel/cpu/perf_event.h
> @@ -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.
> diff --git a/arch/x86/kernel/cpu/perf_event_intel_ds.c b/arch/x86/kernel/cpu/perf_event_intel_ds.c
> index 5db1c77..2e333dc 100644
> --- a/arch/x86/kernel/cpu/perf_event_intel_ds.c
> +++ b/arch/x86/kernel/cpu/perf_event_intel_ds.c
> @@ -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)
> diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
> index f9828a4..d9c0527 100644
> --- a/include/linux/perf_event.h
> +++ b/include/linux/perf_event.h
> @@ -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,
> diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h
> index 1afe962..5afc572 100644
> --- a/include/uapi/linux/perf_event.h
> +++ b/include/uapi/linux/perf_event.h
> @@ -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,
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index a627f36..9a922a2 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -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
>

[toc] | [prev] | [next] | [standalone]


#1305018

From"Liang, Kan" <kan.liang@intel.com>
Date2016-01-08 22:20 +0100
Message-ID<qOMBY-42d-9@gated-at.bofh.it>
In reply to#1303939
> 
> On Wed, Jan 6, 2016 at 3:04 AM,  <kan.liang@intel.com> wrote:
> > From: Kan Liang <kan.liang@intel.com>
> >
> > 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.

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. 

> 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

> 
> Getting code and data physical addresses would provide better coverage
> of the memory subsystem usage.
> 
> > Signed-off-by: Kan Liang <kan.liang@intel.com>
> > ---
> >  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(-)
> >
> > diff --git a/arch/x86/kernel/cpu/perf_event.h
> > b/arch/x86/kernel/cpu/perf_event.h
> > index 799e6bd..164de68 100644
> > --- a/arch/x86/kernel/cpu/perf_event.h
> > +++ b/arch/x86/kernel/cpu/perf_event.h
> > @@ -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.
> > diff --git a/arch/x86/kernel/cpu/perf_event_intel_ds.c
> > b/arch/x86/kernel/cpu/perf_event_intel_ds.c
> > index 5db1c77..2e333dc 100644
> > --- a/arch/x86/kernel/cpu/perf_event_intel_ds.c
> > +++ b/arch/x86/kernel/cpu/perf_event_intel_ds.c
> > @@ -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) diff
> > --git a/include/linux/perf_event.h b/include/linux/perf_event.h index
> > f9828a4..d9c0527 100644
> > --- a/include/linux/perf_event.h
> > +++ b/include/linux/perf_event.h
> > @@ -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,
> > diff --git a/include/uapi/linux/perf_event.h
> > b/include/uapi/linux/perf_event.h index 1afe962..5afc572 100644
> > --- a/include/uapi/linux/perf_event.h
> > +++ b/include/uapi/linux/perf_event.h
> > @@ -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,
> > diff --git a/kernel/events/core.c b/kernel/events/core.c index
> > a627f36..9a922a2 100644
> > --- a/kernel/events/core.c
> > +++ b/kernel/events/core.c
> > @@ -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
> >

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web