Skip to contentSkip to conversation

perf: add qcom l2 cache perf events driver

PATCH Superseded Neil Leeder 2 participants 4 messages Feb 3, 2017 – Feb 7, 2017

Subsystems (4)

v9 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

PATCH v9 1/1 perf: add qcom l2 cache perf events driver

Adds perf events support for L2 cache PMU.

The L2 cache PMU driver is named 'l2cache_0' and can be used with perf events to profile L2 events such as cache hits and misses on Qualcomm Technologies processors.

---
v9:
Add support for maxcpus < all cpus:
Move some of probe functionality to hotplug online callback Call hotplug callbacks from probe
Make percpu cpu->cluster map dynamic and associated with PMU instead of global Use *sysreg_s() for portability reasons
Add documentation file
Rebase on 4.10-rc6
Misc cleanups

v8:
Various style changes for function names & code restructuring Replace dev_warn with ratelimited debug prints
Move hotplug registration before PMU registration Reload counters with a fixed value
Add column-exclusion check for events in same group Rebase on 4.10-rc3

v7:
Move to drivers/perf
Rebased on 4.9-rc1
Ran perf fuzzer against driver for 4 hours with no crashes

v6: restore accidentally dropped Kconfig dependencies

v5:
Fold the header and l2-accessors into .c file Use multi-instance framework for hotplug Change terminology from slice to cluster for clarity Remove unnecessary rmw sequence for enable registers Use prev_count in hwc rather than in slice Enforce all events in same group on same CPU Add comments, rename variables for clarity

v4:
Replace notifier with hotplug statemachine
Allocate PMU struct dynamically

v3:
Remove exports from l2-accessors
Change l2-accessors Kconfig to make it not user-selectable Reorder and remove unnecessary includes

v2:
Add the l2-accessors patch to this patchset, previously posted separately. Remove sampling and per-task functionality for this uncore PMU. Use cpumask to replace code which filtered events to one cpu per slice. Replace manual event filtering with filter_match callback.
Use a separate used_mask for event groups.
Add hotplug notifier for CPU and irq migration.
Remove extraneous synchronisation instructions.
Other miscellaneous cleanup.

 Documentation/perf/qcom_l2_pmu.txt |   38 ++
 drivers/perf/Kconfig               |    9 +
 drivers/perf/Makefile              |    1 +
 drivers/perf/qcom_l2_pmu.c         | 1031 ++++++++++++++++++++++++++++++++++++
 include/linux/cpuhotplug.h         |    1 +
 5 files changed, 1080 insertions(+)
 create mode 100644 Documentation/perf/qcom_l2_pmu.txt
 create mode 100644 drivers/perf/qcom_l2_pmu.c
Documentation/perf/qcom_l2_pmu.txt new file +38 -0
new file mode 100644
index 0000000..b25b976
@@ -0,0 +1,38 @@
+Qualcomm Technologies Level-2 Cache Performance Monitoring Unit (PMU)
+=====================================================================
+
+This driver supports the L2 cache clusters found in Qualcomm Technologies
+Centriq SoCs. There are multiple physical L2 cache clusters, each with their
+own PMU. Each cluster has one or more CPUs associated with it.
+
+There is one logical L2 PMU exposed, which aggregates the results from
+the physical PMUs.
+
+The driver provides a description of its available events and configuration
+options in sysfs, see /sys/devices/l2cache_0.
+
+The "format" directory describes the format of the events.
+
+Events can be envisioned as a 2-dimensional array. Each column represents
+a group of events. There are 8 groups. Only one entry from each
+group can be in use at a time. If multiple events from the same group
+are specified, the conflicting events cannot be counted at the same time.
+
+Events are specified as 0xCCG, where CC is 2 hex digits specifying
+the code (array row) and G specifies the group (column) 0-7.
+
+In addition there is a cycle counter event specified by the value 0xFE
+which is outside the above scheme.
+
+The driver provides a "cpumask" sysfs attribute which contains a mask
+consisting of one CPU per cluster which will be used to handle all the PMU
+events on that cluster.
+
+Examples for use with perf:
+
+  perf stat -e l2cache_0/config=0x001/,l2cache_0/config=0x042/ -a sleep 1
+
+  perf stat -e l2cache_0/config=0xfe/ -C 2 sleep 1
+
+The driver does not support sampling, therefore "perf record" will
+not work. Per-task perf sessions are not supported.
drivers/perf/Kconfig +9 -0
index 4d5c5f9..9365190 100644
@@ -12,6 +12,15 @@ config ARM_PMU
 	  Say y if you want to use CPU performance monitors on ARM-based
 	  systems.
 
+config QCOM_L2_PMU
+	bool "Qualcomm Technologies L2-cache PMU"
+	depends on ARCH_QCOM && ARM64 && PERF_EVENTS && ACPI
+	  help
+	  Provides support for the L2 cache performance monitor unit (PMU)
+	  in Qualcomm Technologies processors.
+	  Adds the L2 cache PMU into the perf events subsystem for
+	  monitoring L2 cache events.
+
 config XGENE_PMU
         depends on PERF_EVENTS && ARCH_XGENE
         bool "APM X-Gene SoC PMU"
drivers/perf/Makefile +1 -0
index b116e98..ef24833 100644
@@ -1,2 +1,3 @@
 obj-$(CONFIG_ARM_PMU) += arm_pmu.o
+obj-$(CONFIG_QCOM_L2_PMU)	+= qcom_l2_pmu.o
 obj-$(CONFIG_XGENE_PMU) += xgene_pmu.o
drivers/perf/qcom_l2_pmu.c new file +1031 -0

1,031 lines not on this page · raw message

include/linux/cpuhotplug.h +1 -0
index d936a00..b7d100e 100644
@@ -140,6 +140,7 @@ enum cpuhp_state {
 	CPUHP_AP_PERF_ARM_CCI_ONLINE,
 	CPUHP_AP_PERF_ARM_CCN_ONLINE,
 	CPUHP_AP_PERF_ARM_L2X0_ONLINE,
+	CPUHP_AP_PERF_ARM_QCOM_L2_ONLINE,
 	CPUHP_AP_WORKQUEUE_ONLINE,
 	CPUHP_AP_RCUTREE_ONLINE,
 	CPUHP_AP_ONLINE_DYN,
Signature · 3 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm Technologies Inc.
Qualcomm Technologies, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project.
maintainer
#1 lore raw

Hi Neil,

Thanks for addressing my comments so quickly; this is looking much better now. I have some comments below, which are mostly w.r.t. simple cleanups.

I still have concerns with the filter_match callback, which I've elaborated on below. I would be happy to take this (with the fixups) if we can drop the filter_match for now.

[..]

Review

Quoted 2 lines
(quoted diff) +2 -0
+#define reg_idx(reg, i)         ((i * IA_L2_REG_OFFSET) + reg##_BASE)
+

My bad, but we should wrap the i in parens to avoid bad expansions:

#define reg_idx(reg, i) (((i) * IA_L2_REG_OFFSET) + reg##_BASE)

[...]

Quoted 18 lines
drivers/perf/qcom_l2_pmu.c new file +18 -0
+static void cluster_pmu_reset_on_cluster(void *x)
+{
+	/* Reset all ctrs */
+	set_l2_indirect_reg(L2PMCR, L2PMCR_RESET_ALL);
+	set_l2_indirect_reg(L2PMCNTENCLR, l2_counter_present_mask);
+	set_l2_indirect_reg(L2PMINTENCLR, l2_counter_present_mask);
+	set_l2_indirect_reg(L2PMOVSCLR, l2_counter_present_mask);
+}
+
+static inline void cluster_pmu_reset(struct cluster_pmu *cluster)
+{
+	cpumask_t *mask = &cluster->cluster_cpus;
+
+	if (smp_call_function_any(mask, cluster_pmu_reset_on_cluster, NULL, 1))
+		dev_err(&cluster->l2cache_pmu->pdev->dev,
+			"Failed to reset on cluster with CPU%d\n",
+			cpumask_first(&cluster->cluster_cpus));
+
Quoted 1 line

}

We don't need the cross-call any more; this is called from a CPU in the relevant cluster in the hotplug online callback. Can we please replace both of these with:

static inline void cluster_pmu_reset(void)

{
	/* Reset all ctrs */
	set_l2_indirect_reg(L2PMCR, L2PMCR_RESET_ALL);
	set_l2_indirect_reg(L2PMCNTENCLR, l2_counter_present_mask);
	set_l2_indirect_reg(L2PMINTENCLR, l2_counter_present_mask);
	set_l2_indirect_reg(L2PMOVSCLR, l2_counter_present_mask);
}

... calling that directly from the hotplug online callback.

[...]

Quoted 7 lines
drivers/perf/qcom_l2_pmu.c new file +7 -0
+static irqreturn_t l2_cache_handle_irq(int irq_num, void *data)
+{
+	struct cluster_pmu *cluster = data;
+	int num_counters = cluster->l2cache_pmu->num_counters;
+	u32 ovsr;
+	int idx;
+

drivers/perf/qcom_l2_pmu.c line 436

Quoted 10 lines
drivers/perf/qcom_l2_pmu.c new file +10 -0
+	ovsr = cluster_pmu_getreset_ovsr();
+	if (!cluster_pmu_has_overflowed(ovsr))
+		return IRQ_NONE;
+
+	for_each_set_bit(idx, cluster->used_counters, num_counters) {
+		struct perf_event *event = cluster->events[idx];
+		struct hw_perf_event *hwc;
+
+		if (!cluster_pmu_counter_has_overflowed(ovsr, idx))
+			continue;

Can the OVSR have a bit set for an event which is no longer in the cluster->events array?

For example, if we get into l2_cache_event_del() with IRQs disabled, but the event overflows immediately before we disable the event, what happens once we exit l2_cache_event_del() and enable interrupts?

Do we need to clear the overflow bit when we remove an event?

Regardless, to save us future pain, can we please add:

		if (WARN_ON_ONCE(!event))
			continue;

... before we potentially try to dereference the event pointer in l2_cache_event_update(event).

Quoted 9 lines
drivers/perf/qcom_l2_pmu.c new file +9 -0
+
+		l2_cache_event_update(event);
+		hwc = &event->hw;
+
+		l2_cache_cluster_set_period(cluster, hwc);
+	}
+
+	return IRQ_HANDLED;
+}

[...]

Quoted 10 lines
drivers/perf/qcom_l2_pmu.c new file +10 -0
+static int l2_cache_filter_match(struct perf_event *event)
+{
+	struct hw_perf_event *hwc = &event->hw;
+	struct l2cache_pmu *l2cache_pmu = to_l2cache_pmu(event->pmu);
+	struct cluster_pmu *cluster = get_cluster_pmu(l2cache_pmu, event->cpu);
+	unsigned int group = L2_EVT_GROUP(hwc->config_base);
+
+	if (hwc->config_base == L2CYCLE_CTR_RAW_CODE)
+		return 1;
+

drivers/perf/qcom_l2_pmu.c line 693

Quoted 10 lines
drivers/perf/qcom_l2_pmu.c new file +10 -0
+	/*
+	 * Check for column exclusion: event column already in use by another
+	 * event. This is for events which are not in the same group.
+	 * Conflicting events in the same group are detected in event_init.
+	 */
+	if (test_bit(group, cluster->used_groups))
+		return 0;
+
+	return 1;
+}

I'm still concerned by this use of the filter_match callback, because it depends on the set of other active events, and can change as other events are scheduled in and out.

When we schedule in two conflicting events A and B in order, B will fail its filter match. When we scheduled out A and B in order, B will succeed its filter match.

The perf core does not expect this inconsistency, and this appears to break the timing update logic in event_sched_out(), when unconditionally called from ctx_sched_out() as part of perf_rotate_context().

I would feel much happier if we dropped l2_cache_filter_match(), at least for the timebeing, and handled this as we do for other cases of intra-pmu resource contention.

We can then consider the filter_match addition on its own at a later point.

[...]

Quoted 20 lines
drivers/perf/qcom_l2_pmu.c new file +20 -0
+static struct cluster_pmu *l2_cache_associate_cpu_with_cluster(
+	struct l2cache_pmu *l2cache_pmu, int cpu)
+{
+	u64 mpidr;
+	int cpu_cluster_id;
+	struct cluster_pmu *cluster;
+
+	/*
+	 * This assumes that the cluster_id is in MPIDR[aff1] for
+	 * single-threaded cores, and MPIDR[aff2] for multi-threaded
+	 * cores. This logic will have to be updated if this changes.
+	 */
+	mpidr = read_cpuid_mpidr();
+	if (mpidr & MPIDR_MT_BITMASK)
+		cpu_cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 2);
+	else
+		cpu_cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
+
+	list_for_each_entry(cluster, &l2cache_pmu->clusters, next) {
+		if (cluster->cluster_id == cpu_cluster_id) {

drivers/perf/qcom_l2_pmu.c line 787

Quoted 10 lines
drivers/perf/qcom_l2_pmu.c new file +10 -0
+			dev_info(&l2cache_pmu->pdev->dev,
+				 "CPU%d associated with cluster %d\n", cpu,
+				 cluster->cluster_id);
+			cpumask_set_cpu(cpu, &cluster->cluster_cpus);
+			*per_cpu_ptr(l2cache_pmu->pmu_cluster, cpu) = cluster;
+			return cluster;
+		}
+	}
+	return 0;
+}

Given the function prototype: s/0/NULL/

Otherwise, this looks fine. Thanks for reworking this.

[...]

drivers/perf/qcom_l2_pmu.c line 966

Quoted 5 lines
drivers/perf/qcom_l2_pmu.c new file +5 -0
+	/* Read cluster info and initialize each cluster */
+	err = device_for_each_child(&pdev->dev, l2cache_pmu,
+				    l2_cache_pmu_probe_cluster);
+	if (err < 0)
+		return err;

We never expect a positive return value, so this should be:

	if (err)
		return err;

Thanks,
Mark.

#2 lore raw

Hi Mark,
Thanks for those comments - I'll add the fixes.

Quoted 12 lines · Mark Rutland

I'm still concerned by this use of the filter_match callback, because it depends on the set of other active events, and can change as other events are scheduled in and out.

When we schedule in two conflicting events A and B in order, B will fail its filter match. When we scheduled out A and B in order, B will succeed its filter match.

The perf core does not expect this inconsistency, and this appears to break the timing update logic in event_sched_out(), when unconditionally called from ctx_sched_out() as part of perf_rotate_context().

Quoted 6 lines

I would feel much happier if we dropped l2_cache_filter_match(), at least for the timebeing, and handled this as we do for other cases of intra-pmu resource contention.

We can then consider the filter_match addition on its own at a later point.

So could this be detected in get_event_idx, the same way we handle counter resource contention? That would eliminate filter_match, and it's the same way its done in armv7 (arch/arm/kernel/perf_event_v7.c:krait_pmu_get_event_idx()).

It's less obvious to the user, but seeing 2 events being rotated so they each have ~50% coverage should provide a hint that the user did something wrong, so I think that would work.

Thanks,

Neil

Signature · 4 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm 
Technologies Inc.
Qualcomm Technologies, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project.
maintainer
#3 lore raw
Quoted 2 lines · Neil

Hi Mark,
Thanks for those comments - I'll add the fixes.

Cheers!

Quoted 17 lines · Mark Rutland
Quoted 16 lines

I'm still concerned by this use of the filter_match callback, because it depends on the set of other active events, and can change as other events are scheduled in and out.

When we schedule in two conflicting events A and B in order, B will fail its filter match. When we scheduled out A and B in order, B will succeed its filter match.

The perf core does not expect this inconsistency, and this appears to break the timing update logic in event_sched_out(), when unconditionally called from ctx_sched_out() as part of perf_rotate_context().

I would feel much happier if we dropped l2_cache_filter_match(), at least for the timebeing, and handled this as we do for other cases of intra-pmu resource contention.

Quoted 7 lines
Quoted 2 lines

We can then consider the filter_match addition on its own at a later point.

So could this be detected in get_event_idx, the same way we handle counter resource contention? That would eliminate filter_match, and it's the same way its done in armv7 (arch/arm/kernel/perf_event_v7.c:krait_pmu_get_event_idx()).

Returning -EAGAIN from event_get_ixd() in that case sounds good to me.

Thanks,
Mark.