mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: kan.liang@intel.com
Cc: mingo@redhat.com, linux-kernel@vger.kernel.org, acme@kernel.org,
	jolsa@redhat.com, tglx@linutronix.de, eranian@google.com,
	ak@linux.intel.com, mpe@ellerman.id.au, maddy@linux.vnet.ibm.com
Subject: Re: [PATCH V6] perf: Add PERF_SAMPLE_PHYS_ADDR
Date: Wed, 23 Aug 2017 16:33:08 +0200	[thread overview]
Message-ID: <20170823143308.v27pgjagxjyd65ci@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <1503498166-3887-1-git-send-email-kan.liang@intel.com>


Michael, Maddy,

Since PPC implements PERF_SAMPLE_ADDR this affects you guys too, please
have a look.

On Wed, Aug 23, 2017 at 10:22:46AM -0400, 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 very important to collect address maps with physical
> addresses. For example, 3D XPoint access can only be found by filtering
> the physical address.
> However, perf doesn't collect physical address information in sampling.
> 
> The virtual address which stored in data->addr can be used to calculate
> the physical address.
> For kernel direct mapping addresses, virt_to_phys is used to convert the
> virtual addresses 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.
> A new sample type PERF_SAMPLE_PHYS_ADDR is introduced to expose the
> physical addresses.

Please reflow that..

> Signed-off-by: Kan Liang <kan.liang@intel.com>
> ---
> 
> This patch is kernel patch.
> The user space patch can be found here.
> https://www.spinics.net/lists/kernel/msg2587093.html
> 
> Changes since V5
>  - Move virt_to_phys to generic code (PeterZ)
> 
>  arch/x86/events/perf_event.h    |  2 +-
>  include/linux/perf_event.h      |  2 ++
>  include/uapi/linux/perf_event.h |  4 +++-
>  kernel/events/core.c            | 47 +++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 53 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h
> index 476aec3..65bb91e 100644
> --- a/arch/x86/events/perf_event.h
> +++ b/arch/x86/events/perf_event.h
> @@ -91,7 +91,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/include/linux/perf_event.h b/include/linux/perf_event.h
> index b14095b..74fb87e 100644
> --- a/include/linux/perf_event.h
> +++ b/include/linux/perf_event.h
> @@ -944,6 +944,8 @@ struct perf_sample_data {
>  
>  	struct perf_regs		regs_intr;
>  	u64				stack_user_size;
> +
> +	u64				phys_addr;
>  } ____cacheline_aligned;
>  
>  /* default value for data source */
> diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h
> index 642db5f..cbea02f 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 */
>  };
>  
>  /*
> @@ -814,6 +815,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 d704e23..b991af3 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -1570,6 +1570,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;
>  }
>  
> @@ -6012,6 +6015,9 @@ void perf_output_sample(struct perf_output_handle *handle,
>  		}
>  	}
>  
> +	if (sample_type & PERF_SAMPLE_PHYS_ADDR)
> +		perf_output_put(handle, data->phys_addr);
> +
>  	if (!event->attr.watermark) {
>  		int wakeup_events = event->attr.wakeup_events;
>  
> @@ -6027,6 +6033,38 @@ void perf_output_sample(struct perf_output_handle *handle,
>  	}
>  }
>  
> +static u64 perf_virt_to_phys(u64 virt)
> +{
> +	u64 phys_addr = 0;
> +	struct page *p = NULL;
> +
> +	if (!virt)
> +		return 0;
> +
> +	if (virt >= TASK_SIZE) {
> +		/* If it's vmalloc()d memory, leave phys_addr as 0 */
> +		if (virt_addr_valid(virt) &&
> +		    !(virt >= VMALLOC_START && virt < VMALLOC_END))
> +			phys_addr = (u64)virt_to_phys((void *)(uintptr_t)virt);
> +	} 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(virt, 1, 0, &p) == 1))
> +			phys_addr = page_to_phys(p) + virt % PAGE_SIZE;
> +
> +		if (p)
> +			put_page(p);
> +	}
> +
> +	return phys_addr;
> +}

Michael, does this work for PPC as is?

> +
>  void perf_prepare_sample(struct perf_event_header *header,
>  			 struct perf_sample_data *data,
>  			 struct perf_event *event,
> @@ -6145,6 +6183,9 @@ void perf_prepare_sample(struct perf_event_header *header,
>  
>  		header->size += size;
>  	}
> +
> +	if (sample_type & PERF_SAMPLE_PHYS_ADDR)
> +		data->phys_addr = perf_virt_to_phys(data->addr);

Only problem with this now is that it requires SAMPLE_ADDR to also be
set in order to obtain data->addr.

Either fix all to set data->attr for (SAMPLE_ADDR || SAMPLE_PHYS_ADDR)
or mandate SAMPLE_ADDR when SAMPLE_PHYS_ADDR.

>  }
>  
>  static void __always_inline
> @@ -9892,6 +9933,12 @@ SYSCALL_DEFINE5(perf_event_open,
>  			return -EINVAL;
>  	}
>  
> +	/* Only privileged users can get kernel addresses */
> +	if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR) &&
> +	    perf_paranoid_kernel() &&
> +	    !capable(CAP_SYS_ADMIN))
> +		return -EACCES;
> +
>  	if (!attr.sample_max_stack)
>  		attr.sample_max_stack = sysctl_perf_event_max_stack;
>  
> -- 
> 2.4.3
> 

  reply	other threads:[~2017-08-23 14:33 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-23 14:22 kan.liang
2017-08-23 14:33 ` Peter Zijlstra [this message]
2017-08-23 14:39   ` Peter Zijlstra
2017-08-23 17:00     ` Stephane Eranian
2017-08-31 16:44       ` Arnaldo Carvalho de Melo
2017-08-23 18:01     ` Liang, Kan
2017-08-23 18:26       ` Peter Zijlstra
2017-08-24  1:26   ` Michael Ellerman
2017-08-24  8:56     ` Peter Zijlstra
2017-08-24  9:52       ` Michael Ellerman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170823143308.v27pgjagxjyd65ci@hirez.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=acme@kernel.org \
    --cc=ak@linux.intel.com \
    --cc=eranian@google.com \
    --cc=jolsa@redhat.com \
    --cc=kan.liang@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maddy@linux.vnet.ibm.com \
    --cc=mingo@redhat.com \
    --cc=mpe@ellerman.id.au \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®