2022-04-22 18:13:05

by Timothy Hayes

[permalink] [raw]
Subject: [PATCH 2/3] perf: arm-spe: Fix SPE events with phys addresses

This patch corrects a bug whereby SPE collection is invoked with
pa_enable=1 but synthesized events fail to show physical addresses.

Signed-off-by: Timothy Hayes <[email protected]>
---
tools/perf/arch/arm64/util/arm-spe.c | 10 ++++++++++
tools/perf/util/arm-spe.c | 3 ++-
2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/tools/perf/arch/arm64/util/arm-spe.c b/tools/perf/arch/arm64/util/arm-spe.c
index af4d63af8072..e8b577d33e53 100644
--- a/tools/perf/arch/arm64/util/arm-spe.c
+++ b/tools/perf/arch/arm64/util/arm-spe.c
@@ -148,6 +148,7 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,
bool privileged = perf_event_paranoid_check(-1);
struct evsel *tracking_evsel;
int err;
+ u64 bit;

sper->evlist = evlist;

@@ -245,6 +246,15 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,
*/
evsel__set_sample_bit(arm_spe_evsel, DATA_SRC);

+ /*
+ * The PHYS_ADDR flag does not affect the driver behaviour, it is used to
+ * inform that the resulting output's SPE samples contain physical addresses
+ * where applicable.
+ */
+ bit = perf_pmu__format_bits(&arm_spe_pmu->format, "pa_enable");
+ if (arm_spe_evsel->core.attr.config & bit)
+ evsel__set_sample_bit(arm_spe_evsel, PHYS_ADDR);
+
/* Add dummy event to keep tracking */
err = parse_events(evlist, "dummy:u", NULL);
if (err)
diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c
index 151cc38a171c..1a80151baed9 100644
--- a/tools/perf/util/arm-spe.c
+++ b/tools/perf/util/arm-spe.c
@@ -1033,7 +1033,8 @@ arm_spe_synth_events(struct arm_spe *spe, struct perf_session *session)
memset(&attr, 0, sizeof(struct perf_event_attr));
attr.size = sizeof(struct perf_event_attr);
attr.type = PERF_TYPE_HARDWARE;
- attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
+ attr.sample_type = evsel->core.attr.sample_type &
+ (PERF_SAMPLE_MASK | PERF_SAMPLE_PHYS_ADDR);
attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
PERF_SAMPLE_PERIOD | PERF_SAMPLE_DATA_SRC |
PERF_SAMPLE_WEIGHT | PERF_SAMPLE_ADDR;
--
2.25.1


2022-04-24 16:52:41

by Leo Yan

[permalink] [raw]
Subject: Re: [PATCH 2/3] perf: arm-spe: Fix SPE events with phys addresses

Hi Timothy,

On Thu, Apr 21, 2022 at 05:52:04PM +0100, Timothy Hayes wrote:
> This patch corrects a bug whereby SPE collection is invoked with
> pa_enable=1 but synthesized events fail to show physical addresses.
>
> Signed-off-by: Timothy Hayes <[email protected]>
> ---
> tools/perf/arch/arm64/util/arm-spe.c | 10 ++++++++++
> tools/perf/util/arm-spe.c | 3 ++-
> 2 files changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/tools/perf/arch/arm64/util/arm-spe.c b/tools/perf/arch/arm64/util/arm-spe.c
> index af4d63af8072..e8b577d33e53 100644
> --- a/tools/perf/arch/arm64/util/arm-spe.c
> +++ b/tools/perf/arch/arm64/util/arm-spe.c
> @@ -148,6 +148,7 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,
> bool privileged = perf_event_paranoid_check(-1);
> struct evsel *tracking_evsel;
> int err;
> + u64 bit;
>
> sper->evlist = evlist;
>
> @@ -245,6 +246,15 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,
> */
> evsel__set_sample_bit(arm_spe_evsel, DATA_SRC);
>
> + /*
> + * The PHYS_ADDR flag does not affect the driver behaviour, it is used to
> + * inform that the resulting output's SPE samples contain physical addresses
> + * where applicable.
> + */
> + bit = perf_pmu__format_bits(&arm_spe_pmu->format, "pa_enable");
> + if (arm_spe_evsel->core.attr.config & bit)
> + evsel__set_sample_bit(arm_spe_evsel, PHYS_ADDR);
> +
> /* Add dummy event to keep tracking */
> err = parse_events(evlist, "dummy:u", NULL);
> if (err)
> diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c
> index 151cc38a171c..1a80151baed9 100644
> --- a/tools/perf/util/arm-spe.c
> +++ b/tools/perf/util/arm-spe.c
> @@ -1033,7 +1033,8 @@ arm_spe_synth_events(struct arm_spe *spe, struct perf_session *session)
> memset(&attr, 0, sizeof(struct perf_event_attr));
> attr.size = sizeof(struct perf_event_attr);
> attr.type = PERF_TYPE_HARDWARE;
> - attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
> + attr.sample_type = evsel->core.attr.sample_type &
> + (PERF_SAMPLE_MASK | PERF_SAMPLE_PHYS_ADDR);

I verified this patch and I can confirm the physical address can be
dumped successfully.

I have a more general question, seems to me, we need to change the
macro PERF_SAMPLE_MASK in the file util/event.h as below, so
here doesn't need to 'or' the flag PERF_SAMPLE_PHYS_ADDR anymore.

@Arnaldo, @Jiri, could you confirm if this is the right way to move
forward? I am not sure why PERF_SAMPLE_MASK doesn't contain the bit
PERF_SAMPLE_PHYS_ADDR in current code.

diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h
index cdd72e05fd28..c905ac32ebad 100644
--- a/tools/perf/util/event.h
+++ b/tools/perf/util/event.h
@@ -39,7 +39,7 @@ struct perf_event_attr;
PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR | \
PERF_SAMPLE_ID | PERF_SAMPLE_STREAM_ID | \
PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD | \
- PERF_SAMPLE_IDENTIFIER)
+ PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_PHYS_ADDR)

Thanks,
Leo

> attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
> PERF_SAMPLE_PERIOD | PERF_SAMPLE_DATA_SRC |
> PERF_SAMPLE_WEIGHT | PERF_SAMPLE_ADDR;
> --
> 2.25.1
>

2022-04-25 16:27:09

by James Clark

[permalink] [raw]
Subject: Re: [PATCH 2/3] perf: arm-spe: Fix SPE events with phys addresses



On 24/04/2022 13:59, Leo Yan wrote:
> Hi Timothy,
>
> On Thu, Apr 21, 2022 at 05:52:04PM +0100, Timothy Hayes wrote:
>> This patch corrects a bug whereby SPE collection is invoked with
>> pa_enable=1 but synthesized events fail to show physical addresses.
>>
>> Signed-off-by: Timothy Hayes <[email protected]>
>> ---
>> tools/perf/arch/arm64/util/arm-spe.c | 10 ++++++++++
>> tools/perf/util/arm-spe.c | 3 ++-
>> 2 files changed, 12 insertions(+), 1 deletion(-)
>>
>> diff --git a/tools/perf/arch/arm64/util/arm-spe.c b/tools/perf/arch/arm64/util/arm-spe.c
>> index af4d63af8072..e8b577d33e53 100644
>> --- a/tools/perf/arch/arm64/util/arm-spe.c
>> +++ b/tools/perf/arch/arm64/util/arm-spe.c
>> @@ -148,6 +148,7 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,
>> bool privileged = perf_event_paranoid_check(-1);
>> struct evsel *tracking_evsel;
>> int err;
>> + u64 bit;
>>
>> sper->evlist = evlist;
>>
>> @@ -245,6 +246,15 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,
>> */
>> evsel__set_sample_bit(arm_spe_evsel, DATA_SRC);
>>
>> + /*
>> + * The PHYS_ADDR flag does not affect the driver behaviour, it is used to
>> + * inform that the resulting output's SPE samples contain physical addresses
>> + * where applicable.
>> + */
>> + bit = perf_pmu__format_bits(&arm_spe_pmu->format, "pa_enable");
>> + if (arm_spe_evsel->core.attr.config & bit)
>> + evsel__set_sample_bit(arm_spe_evsel, PHYS_ADDR);
>> +
>> /* Add dummy event to keep tracking */
>> err = parse_events(evlist, "dummy:u", NULL);
>> if (err)
>> diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c
>> index 151cc38a171c..1a80151baed9 100644
>> --- a/tools/perf/util/arm-spe.c
>> +++ b/tools/perf/util/arm-spe.c
>> @@ -1033,7 +1033,8 @@ arm_spe_synth_events(struct arm_spe *spe, struct perf_session *session)
>> memset(&attr, 0, sizeof(struct perf_event_attr));
>> attr.size = sizeof(struct perf_event_attr);
>> attr.type = PERF_TYPE_HARDWARE;
>> - attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
>> + attr.sample_type = evsel->core.attr.sample_type &
>> + (PERF_SAMPLE_MASK | PERF_SAMPLE_PHYS_ADDR);
>
> I verified this patch and I can confirm the physical address can be
> dumped successfully.
>
> I have a more general question, seems to me, we need to change the
> macro PERF_SAMPLE_MASK in the file util/event.h as below, so
> here doesn't need to 'or' the flag PERF_SAMPLE_PHYS_ADDR anymore.
>
> @Arnaldo, @Jiri, could you confirm if this is the right way to move
> forward? I am not sure why PERF_SAMPLE_MASK doesn't contain the bit
> PERF_SAMPLE_PHYS_ADDR in current code.

I think there is a reason that PERF_SAMPLE_MASK is a subset of all the
bits. This comment below suggests it. Is it so the mask only includes fields
that are 64bits? That makes the __evsel__sample_size() function a simple
multiplication of a count of all the fields that are 64bits.

static int
perf_event__check_size(union perf_event *event, unsigned int sample_size)
{
/*
* The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
* up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
* check the format does not go past the end of the event.
*/
if (sample_size + sizeof(event->header) > event->header.size)
return -EFAULT;

return 0;
}

Having said that, the mask was updated once to add PERF_SAMPLE_IDENTIFIER to
it, so that comment is slightly out of date now.


>
> diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h
> index cdd72e05fd28..c905ac32ebad 100644
> --- a/tools/perf/util/event.h
> +++ b/tools/perf/util/event.h
> @@ -39,7 +39,7 @@ struct perf_event_attr;
> PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR | \
> PERF_SAMPLE_ID | PERF_SAMPLE_STREAM_ID | \
> PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD | \
> - PERF_SAMPLE_IDENTIFIER)
> + PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_PHYS_ADDR)
>
> Thanks,
> Leo
>
>> attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
>> PERF_SAMPLE_PERIOD | PERF_SAMPLE_DATA_SRC |
>> PERF_SAMPLE_WEIGHT | PERF_SAMPLE_ADDR;
>> --
>> 2.25.1
>>

2022-04-26 19:02:56

by Leo Yan

[permalink] [raw]
Subject: Re: [PATCH 2/3] perf: arm-spe: Fix SPE events with phys addresses

On Mon, Apr 25, 2022 at 10:12:36AM +0100, James Clark wrote:

[...]

> >> diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c
> >> index 151cc38a171c..1a80151baed9 100644
> >> --- a/tools/perf/util/arm-spe.c
> >> +++ b/tools/perf/util/arm-spe.c
> >> @@ -1033,7 +1033,8 @@ arm_spe_synth_events(struct arm_spe *spe, struct perf_session *session)
> >> memset(&attr, 0, sizeof(struct perf_event_attr));
> >> attr.size = sizeof(struct perf_event_attr);
> >> attr.type = PERF_TYPE_HARDWARE;
> >> - attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
> >> + attr.sample_type = evsel->core.attr.sample_type &
> >> + (PERF_SAMPLE_MASK | PERF_SAMPLE_PHYS_ADDR);
> >
> > I verified this patch and I can confirm the physical address can be
> > dumped successfully.
> >
> > I have a more general question, seems to me, we need to change the
> > macro PERF_SAMPLE_MASK in the file util/event.h as below, so
> > here doesn't need to 'or' the flag PERF_SAMPLE_PHYS_ADDR anymore.
> >
> > @Arnaldo, @Jiri, could you confirm if this is the right way to move
> > forward? I am not sure why PERF_SAMPLE_MASK doesn't contain the bit
> > PERF_SAMPLE_PHYS_ADDR in current code.
>
> I think there is a reason that PERF_SAMPLE_MASK is a subset of all the
> bits. This comment below suggests it. Is it so the mask only includes fields
> that are 64bits? That makes the __evsel__sample_size() function a simple
> multiplication of a count of all the fields that are 64bits.

After reading code, seems the conclusion "a count of all the fields
that are 64bits" is not valid. PERF_SAMPLE_MASK contains bits
PERF_SAMPLE_IP and PERF_SAMPLE_TID, the corresponding fields 'pid'
and 'tid' in the structure perf_sample are u32 type.

> static int
> perf_event__check_size(union perf_event *event, unsigned int sample_size)
> {
> /*
> * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
> * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
> * check the format does not go past the end of the event.
> */
> if (sample_size + sizeof(event->header) > event->header.size)
> return -EFAULT;

Okay, thanks for sharing the info, it does show that it's deliberately to
not contain all fields in PERF_SAMPLE_MASK. If so, this patch is fine
for me:

Reviewed-by: Leo Yan <[email protected]>


> return 0;
> }
>
> Having said that, the mask was updated once to add PERF_SAMPLE_IDENTIFIER to
> it, so that comment is slightly out of date now.
>
>
> >
> > diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h
> > index cdd72e05fd28..c905ac32ebad 100644
> > --- a/tools/perf/util/event.h
> > +++ b/tools/perf/util/event.h
> > @@ -39,7 +39,7 @@ struct perf_event_attr;
> > PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR | \
> > PERF_SAMPLE_ID | PERF_SAMPLE_STREAM_ID | \
> > PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD | \
> > - PERF_SAMPLE_IDENTIFIER)
> > + PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_PHYS_ADDR)
> >
> > Thanks,
> > Leo
> >
> >> attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
> >> PERF_SAMPLE_PERIOD | PERF_SAMPLE_DATA_SRC |
> >> PERF_SAMPLE_WEIGHT | PERF_SAMPLE_ADDR;
> >> --
> >> 2.25.1
> >>