mbox series

[v2,00/20] perf/core: Generalise event exclusion checking

Message ID 1543230756-15319-1-git-send-email-andrew.murray@arm.com
Headers show
Series perf/core: Generalise event exclusion checking | expand

Message

Andrew Murray Nov. 26, 2018, 11:12 a.m. UTC
Many PMU drivers do not have the capability to exclude counting events
that occur in specific contexts such as idle, kernel, guest, etc. These
drivers indicate this by returning an error in their event_init upon
testing the events attribute flags.

However this approach requires that each time a new event modifier is
added to perf, all the perf drivers need to be modified to indicate that
they don't support the attribute. This results in additional boiler-plate
code common to many drivers that needs to be maintained. Furthermore the
drivers are not consistent with regards to the error value they return
when reporting unsupported attributes.

This patchset allow PMU drivers to advertise their ability to exclude
based on context via a new capability: PERF_PMU_CAP_EXCLUDE. This allows
the perf core to reject requests for exclusion events where there is no
support in the PMU.

This is a functional change, in particular:

 - Some drivers will now additionally (but correctly) report unsupported
   exclusion flags. It's typical for existing userspace tools such as
   perf to handle such errors by retrying the system call without the
   unsupported flags.

 - Drivers that do not support any exclusion that previously reported
   -EPERM or -EOPNOTSUPP will now report -EINVAL - this is consistent
   with the majority and results in userspace perf retrying without
   exclusion.

All drivers touched by this patchset have been compile tested.

Changes from v1:

 - Changed approach from explicitly rejecting events in unsupporting PMU
   drivers to explicitly advertising a capability in PMU drivers that
   do support exclusion events

 - Added additional information to tools/perf/design.txt

 - Rename event_has_exclude_flags to event_has_any_exclude_flag and
   update commit log to reflect it's a function

Andrew Murray (20):
  perf/doc: update design.txt for exclude_{host|guest} flags
  perf/core: add function to test for event exclusion flags
  perf/core: add PERF_PMU_CAP_EXCLUDE for exclusion capable PMUs
  perf/hw_breakpoint: perf/core: advertise PMU exclusion capability
  alpha: perf/core: remove unnecessary checks for exclusion
  arc: perf/core: advertise PMU exclusion capability
  arm: perf: conditionally advertise PMU exclusion capability
  arm: perf/core: remove unnecessary checks for exclusion
  drivers/perf: perf/core: remove unnecessary checks for exclusion
  drivers/perf: perf/core: remove unnecessary checks for exclusion
  drivers/perf: perf/core: advertise PMU exclusion capability
  mips: perf/core: advertise PMU exclusion capability
  powerpc: perf/core: advertise PMU exclusion capability
  powerpc: perf/core: remove unnecessary checks for exclusion
  s390: perf/events: advertise PMU exclusion capability
  sparc: perf/core: advertise PMU exclusion capability
  x86: perf/core: remove unnecessary checks for exclusion
  x86: perf/core remove unnecessary checks for exclusion
  x86: perf/core: advertise PMU exclusion capability
  perf/core: remove unused perf_flags

 arch/alpha/kernel/perf_event.c           |  6 ------
 arch/arc/kernel/perf_event.c             |  1 +
 arch/arm/mach-imx/mmdc.c                 |  8 +-------
 arch/arm/mm/cache-l2x0-pmu.c             |  8 --------
 arch/mips/kernel/perf_event_mipsxx.c     |  1 +
 arch/powerpc/perf/core-book3s.c          |  1 +
 arch/powerpc/perf/core-fsl-emb.c         |  1 +
 arch/powerpc/perf/hv-24x7.c              |  9 ---------
 arch/powerpc/perf/hv-gpci.c              |  9 ---------
 arch/powerpc/perf/imc-pmu.c              | 18 ------------------
 arch/s390/kernel/perf_cpum_cf.c          |  1 +
 arch/s390/kernel/perf_cpum_sf.c          |  2 ++
 arch/sparc/kernel/perf_event.c           |  1 +
 arch/x86/events/amd/ibs.c                | 12 ------------
 arch/x86/events/amd/iommu.c              |  5 -----
 arch/x86/events/amd/power.c              |  9 +--------
 arch/x86/events/amd/uncore.c             |  5 -----
 arch/x86/events/core.c                   |  2 ++
 arch/x86/events/intel/bts.c              |  2 +-
 arch/x86/events/intel/cstate.c           |  8 +-------
 arch/x86/events/intel/pt.c               |  4 +++-
 arch/x86/events/intel/rapl.c             |  8 +-------
 arch/x86/events/intel/uncore.c           |  8 --------
 arch/x86/events/intel/uncore_snb.c       |  8 +-------
 arch/x86/events/msr.c                    |  8 +-------
 drivers/perf/arm-cci.c                   |  9 ---------
 drivers/perf/arm-ccn.c                   |  5 +----
 drivers/perf/arm_dsu_pmu.c               |  8 +-------
 drivers/perf/arm_pmu.c                   | 15 +++++----------
 drivers/perf/arm_spe_pmu.c               |  3 ++-
 drivers/perf/hisilicon/hisi_uncore_pmu.c |  9 ---------
 drivers/perf/qcom_l2_pmu.c               |  8 --------
 drivers/perf/qcom_l3_pmu.c               |  7 -------
 drivers/perf/xgene_pmu.c                 |  5 -----
 include/linux/perf_event.h               | 10 ++++++++++
 include/uapi/linux/perf_event.h          |  2 --
 kernel/events/core.c                     |  9 +++++++++
 kernel/events/hw_breakpoint.c            |  2 ++
 tools/include/uapi/linux/perf_event.h    |  2 --
 tools/perf/design.txt                    |  4 ++++
 40 files changed, 54 insertions(+), 189 deletions(-)

Comments

Robin Murphy Nov. 26, 2018, 2:10 p.m. UTC | #1
Hi Andrew,

On 26/11/2018 11:12, Andrew Murray wrote:
> Many PMU drivers do not have the capability to exclude counting events
> that occur in specific contexts such as idle, kernel, guest, etc. These
> drivers indicate this by returning an error in their event_init upon
> testing the events attribute flags. This approach is error prone and
> often inconsistent.
> 
> Let's instead allow PMU drivers to advertise their ability to exclude
> based on context via a new capability: PERF_PMU_CAP_EXCLUDE. This
> allows the perf core to reject requests for exclusion events where
> there is no support in the PMU.
> 
> Signed-off-by: Andrew Murray <andrew.murray@arm.com>
> ---
>   include/linux/perf_event.h | 1 +
>   kernel/events/core.c       | 9 +++++++++
>   2 files changed, 10 insertions(+)
> 
> diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
> index b2e806f..69b3d65 100644
> --- a/include/linux/perf_event.h
> +++ b/include/linux/perf_event.h
> @@ -244,6 +244,7 @@ struct perf_event;
>   #define PERF_PMU_CAP_EXCLUSIVE			0x10
>   #define PERF_PMU_CAP_ITRACE			0x20
>   #define PERF_PMU_CAP_HETEROGENEOUS_CPUS		0x40
> +#define PERF_PMU_CAP_EXCLUDE			0x80
>   
>   /**
>    * struct pmu - generic performance monitoring unit
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 5a97f34..9afb33c 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -9743,6 +9743,15 @@ static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
>   	if (ctx)
>   		perf_event_ctx_unlock(event->group_leader, ctx);
>   
> +	if (!ret) {
> +		if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUDE) &&
> +				event_has_any_exclude_flag(event)) {

Technically this is a bisection-breaker, since no driver has this 
capability yet - ideally, this patch should come after all the ones 
introducing it to the relevant drivers (with the removal of the 
now-redundant code from the other drivers at the end).

Alternatively, since we already have several other negative 
capabilities, unless there's a strong feeling against adding any more 
then it might work out simpler to flip it to PERF_PMU_CAP_NO_EXCLUDE, 
such that we only need to introduce the core check then directly replace 
the open-coded event checks with the capability in the appropriate 
drivers, and need not touch the exclusion-supporting ones at all.

Robin.

> +			if (event->destroy)
> +				event->destroy(event);
> +			ret = -EINVAL;
> +		}
> +	}
> +
>   	if (ret)
>   		module_put(pmu->module);
>   
>
Andrew Murray Nov. 26, 2018, 2:55 p.m. UTC | #2
On Mon, Nov 26, 2018 at 02:10:24PM +0000, Robin Murphy wrote:
> Hi Andrew,
> 
> On 26/11/2018 11:12, Andrew Murray wrote:
> > Many PMU drivers do not have the capability to exclude counting events
> > that occur in specific contexts such as idle, kernel, guest, etc. These
> > drivers indicate this by returning an error in their event_init upon
> > testing the events attribute flags. This approach is error prone and
> > often inconsistent.
> > 
> > Let's instead allow PMU drivers to advertise their ability to exclude
> > based on context via a new capability: PERF_PMU_CAP_EXCLUDE. This
> > allows the perf core to reject requests for exclusion events where
> > there is no support in the PMU.
> > 
> > Signed-off-by: Andrew Murray <andrew.murray@arm.com>
> > ---
> >   include/linux/perf_event.h | 1 +
> >   kernel/events/core.c       | 9 +++++++++
> >   2 files changed, 10 insertions(+)
> > 
> > diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
> > index b2e806f..69b3d65 100644
> > --- a/include/linux/perf_event.h
> > +++ b/include/linux/perf_event.h
> > @@ -244,6 +244,7 @@ struct perf_event;
> >   #define PERF_PMU_CAP_EXCLUSIVE			0x10
> >   #define PERF_PMU_CAP_ITRACE			0x20
> >   #define PERF_PMU_CAP_HETEROGENEOUS_CPUS		0x40
> > +#define PERF_PMU_CAP_EXCLUDE			0x80
> >   /**
> >    * struct pmu - generic performance monitoring unit
> > diff --git a/kernel/events/core.c b/kernel/events/core.c
> > index 5a97f34..9afb33c 100644
> > --- a/kernel/events/core.c
> > +++ b/kernel/events/core.c
> > @@ -9743,6 +9743,15 @@ static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
> >   	if (ctx)
> >   		perf_event_ctx_unlock(event->group_leader, ctx);
> > +	if (!ret) {
> > +		if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUDE) &&
> > +				event_has_any_exclude_flag(event)) {
> 
> Technically this is a bisection-breaker, since no driver has this capability
> yet - ideally, this patch should come after all the ones introducing it to
> the relevant drivers (with the removal of the now-redundant code from the
> other drivers at the end).

Indeed. Thought it is possible to first introduce the capability, update the
relevant drivers to advertise it, then add the change to core.c and finally
remove the unnecessary error checks as a result of using the new capability.
This approach could be bisection-proof.

> 
> Alternatively, since we already have several other negative capabilities,
> unless there's a strong feeling against adding any more then it might work
> out simpler to flip it to PERF_PMU_CAP_NO_EXCLUDE, such that we only need to
> introduce the core check then directly replace the open-coded event checks
> with the capability in the appropriate drivers, and need not touch the
> exclusion-supporting ones at all.

This would certaintly be less risky and invasive (e.g. compare the number of
files touched between this v2 and the previous v1).

I'm happy with either approach.

Thanks,

Andrew Murray

> 
> Robin.
> 
> > +			if (event->destroy)
> > +				event->destroy(event);
> > +			ret = -EINVAL;
> > +		}
> > +	}
> > +
> >   	if (ret)
> >   		module_put(pmu->module);
> >
David Miller Nov. 26, 2018, 6 p.m. UTC | #3
From: Andrew Murray <andrew.murray@arm.com>
Date: Mon, 26 Nov 2018 11:12:32 +0000

> The SPARC PMU has the capability to exclude events based on context
>  - let's advertise that we support the PERF_PMU_CAP_EXCLUDE
> capability to ensure that perf doesn't prevent us from handling
> events where any exclusion flags are set.
> 
> Signed-off-by: Andrew Murray <andrew.murray@arm.com>

Acked-by: David S. Miller <davem@davemloft.net>
Suzuki K Poulose Nov. 26, 2018, 6:22 p.m. UTC | #4
Hi Andrew,

On 26/11/2018 11:12, Andrew Murray wrote:
> Update design.txt to reflect the presence of the exclude_host
> and exclude_guest perf flags.
> 
> Signed-off-by: Andrew Murray <andrew.murray@arm.com>

Thanks a lot for adding this !

> ---
>   tools/perf/design.txt | 4 ++++
>   1 file changed, 4 insertions(+)
> 
> diff --git a/tools/perf/design.txt b/tools/perf/design.txt
> index a28dca2..5b2b23b 100644
> --- a/tools/perf/design.txt
> +++ b/tools/perf/design.txt
> @@ -222,6 +222,10 @@ The 'exclude_user', 'exclude_kernel' and 'exclude_hv' bits provide a
>   way to request that counting of events be restricted to times when the
>   CPU is in user, kernel and/or hypervisor mode.
>   
> +Furthermore the 'exclude_host' and 'exclude_guest' bits provide a way
> +to request counting of events restricted to guest and host contexts when
> +using KVM virtualisation.

minor nit: could we generalise this to :

"using Linux as the hypervisor".

Otherwise, looks good to me.

Cheers
Suzuki
Hendrik Brueckner Nov. 27, 2018, 8:19 a.m. UTC | #5
On Mon, Nov 26, 2018 at 11:12:31AM +0000, Andrew Murray wrote:
> The s390 cpum_cf and cpum_sf PMUs have the capability to exclude
> events based on context. Let's advertise that we support the
> PERF_PMU_CAP_EXCLUDE capability to ensure that perf doesn't
> prevent us from handling events where any exclusion flags are set.
> 
> Signed-off-by: Andrew Murray <andrew.murray@arm.com>
> ---
>  arch/s390/kernel/perf_cpum_cf.c | 1 +
>  arch/s390/kernel/perf_cpum_sf.c | 2 ++
>  2 files changed, 3 insertions(+)

Reviewed-by: Hendrik Brueckner <brueckner@linux.ibm.com>