Skip to contentSkip to conversation

Add support for the ARMv8.2 Statistical Profiling Extension

PATCH Superseded Will Deacon 3 participants 12 messages Apr 6, 2017 – Apr 7, 2017

Subsystems (7)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add support for the ARMv8.2 Statistical Profiling Extension

Hi all,

This is the fourth posting of the patches previously posted here:

rfcv1: http://lists.infradead.org/pipermail/linux-arm-kernel/2017-January/476450.html
rfcv2: http://lists.infradead.org/pipermail/linux-arm-kernel/2017-January/479387.html

     v1: http://lists.infradead.org/pipermail/linux-arm-kernel/2017-January/483684.html

Changes since v1 include:

* Rebased onto new perf_aux_output_flag API (in -next via -tip and included here)
* Fixed allocation of COLLISION flag
* Fixed buffer draining in IRQ handler
* Rebased onto v4.11-rc5

The architecture documentation is now available here:

https://developer.arm.com/products/architecture/a-profile/docs/ddi0586/latest/arm-architecture-reference-manual-supplement-statistical-profiling-extension-for-armv8-a

and there's a high-level overview on this official ARM blog:

https://community.arm.com/processors/b/blog/posts/statistical-profiling-extension-for-armv8-a

All comments welcome,

Will

--->8

Will Deacon (6):
perf/core: Keep AUX flags in the output handle
genirq: export irq_get_percpu_devid_partition to modules perf/core: Export AUX buffer helpers to modules
perf/core: Add PERF_AUX_FLAG_COLLISION to report colliding samples drivers/perf: Add support for ARMv8.2 Statistical Profiling Extension
dt-bindings: Document devicetree binding for ARM SPE

 Documentation/devicetree/bindings/arm/spe-pmu.txt |   20 +
 arch/x86/events/intel/bts.c                       |   16 +-
 arch/x86/events/intel/pt.c                        |   17 +-
 arch/x86/events/intel/pt.h                        |    1 -
 drivers/hwtracing/coresight/coresight-etb10.c     |    9 +-
 drivers/hwtracing/coresight/coresight-etm-perf.c  |    9 +-
 drivers/hwtracing/coresight/coresight-priv.h      |    2 -
 drivers/hwtracing/coresight/coresight-tmc-etf.c   |    7 +-
 drivers/perf/Kconfig                              |    8 +
 drivers/perf/Makefile                             |    1 +
 drivers/perf/arm_spe_pmu.c                        | 1253 +++++++++++++++++++++
 include/linux/coresight.h                         |    2 +-
 include/linux/perf_event.h                        |    8 +-
 include/uapi/linux/perf_event.h                   |    1 +
 kernel/events/ring_buffer.c                       |   38 +-
 kernel/irq/irqdesc.c                              |    1 +
 16 files changed, 1344 insertions(+), 49 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/arm/spe-pmu.txt
 create mode 100644 drivers/perf/arm_spe_pmu.c
Signature · 1 line
2.1.4
#1 lore raw

PATCH v2 1/6 perf/core: Keep AUX flags in the output handle

In preparation for adding more flags to perf AUX records, introduce a separate API for setting the flags for a session, rather than appending more bool arguments to perf_aux_output_end. This allows to set each flag at the time a corresponding condition is detected, instead of tracking it in each driver's private state.

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Mathieu Poirier <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Vince Weaver <[email protected]>
Cc vince at deater.net
---
 arch/x86/events/intel/bts.c                      | 16 +++++------
 arch/x86/events/intel/pt.c                       | 17 ++++++------
 arch/x86/events/intel/pt.h                       |  1 -
 drivers/hwtracing/coresight/coresight-etb10.c    |  9 +++----
 drivers/hwtracing/coresight/coresight-etm-perf.c |  9 +++----
 drivers/hwtracing/coresight/coresight-priv.h     |  2 --
 drivers/hwtracing/coresight/coresight-tmc-etf.c  |  7 +++--
 include/linux/coresight.h                        |  2 +-
 include/linux/perf_event.h                       |  8 +++---
 kernel/events/ring_buffer.c                      | 34 ++++++++++++++++--------
 10 files changed, 56 insertions(+), 49 deletions(-)
arch/x86/events/intel/bts.c +7 -9
index 982c9e31daca..8ae8c5ce3a1f 100644
@@ -63,7 +63,6 @@ struct bts_buffer {
 	unsigned int	cur_buf;
 	bool		snapshot;
 	local_t		data_size;
-	local_t		lost;
 	local_t		head;
 	unsigned long	end;
 	void		**data_pages;
@@ -199,7 +198,8 @@ static void bts_update(struct bts_ctx *bts)
 			return;
 
 		if (ds->bts_index >= ds->bts_absolute_maximum)
-			local_inc(&buf->lost);
+			perf_aux_output_flag(&bts->handle,
+			                     PERF_AUX_FLAG_TRUNCATED);
 
 		/*
 		 * old and head are always in the same physical buffer, so we
@@ -276,7 +276,7 @@ static void bts_event_start(struct perf_event *event, int flags)
 	return;
 
 fail_end_stop:
-	perf_aux_output_end(&bts->handle, 0, false);
+	perf_aux_output_end(&bts->handle, 0);
 
 fail_stop:
 	event->hw.state = PERF_HES_STOPPED;
@@ -319,9 +319,8 @@ static void bts_event_stop(struct perf_event *event, int flags)
 				bts->handle.head =
 					local_xchg(&buf->data_size,
 						   buf->nr_pages << PAGE_SHIFT);
-
-			perf_aux_output_end(&bts->handle, local_xchg(&buf->data_size, 0),
-					    !!local_xchg(&buf->lost, 0));
+			perf_aux_output_end(&bts->handle,
+			                    local_xchg(&buf->data_size, 0));
 		}
 
 		cpuc->ds->bts_index = bts->ds_back.bts_buffer_base;
@@ -484,8 +483,7 @@ int intel_bts_interrupt(void)
 	if (old_head == local_read(&buf->head))
 		return handled;
 
-	perf_aux_output_end(&bts->handle, local_xchg(&buf->data_size, 0),
-			    !!local_xchg(&buf->lost, 0));
+	perf_aux_output_end(&bts->handle, local_xchg(&buf->data_size, 0));
 
 	buf = perf_aux_output_begin(&bts->handle, event);
 	if (buf)
@@ -500,7 +498,7 @@ int intel_bts_interrupt(void)
 			 * cleared handle::event
 			 */
 			barrier();
-			perf_aux_output_end(&bts->handle, 0, false);
+			perf_aux_output_end(&bts->handle, 0);
 		}
 	}
 
arch/x86/events/intel/pt.c +9 -8
index 5900471ee508..0218728be37a 100644
@@ -753,7 +753,8 @@ static void pt_handle_status(struct pt *pt)
 		 */
 		if (!pt_cap_get(PT_CAP_topa_multiple_entries) ||
 		    buf->output_off == sizes(TOPA_ENTRY(buf->cur, buf->cur_idx)->size)) {
-			local_inc(&buf->lost);
+			perf_aux_output_flag(&pt->handle,
+			                     PERF_AUX_FLAG_TRUNCATED);
 			advance++;
 		}
 	}
@@ -846,8 +847,10 @@ static int pt_buffer_reset_markers(struct pt_buffer *buf,
 
 	/* can't stop in the middle of an output region */
 	if (buf->output_off + handle->size + 1 <
-	    sizes(TOPA_ENTRY(buf->cur, buf->cur_idx)->size))
+	    sizes(TOPA_ENTRY(buf->cur, buf->cur_idx)->size)) {
+		perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
 		return -EINVAL;
+	}
 
 
 	/* single entry ToPA is handled by marking all regions STOP=1 INT=1 */
@@ -1192,8 +1195,7 @@ void intel_pt_interrupt(void)
 
 	pt_update_head(pt);
 
-	perf_aux_output_end(&pt->handle, local_xchg(&buf->data_size, 0),
-			    local_xchg(&buf->lost, 0));
+	perf_aux_output_end(&pt->handle, local_xchg(&buf->data_size, 0));
 
 	if (!event->hw.state) {
 		int ret;
@@ -1208,7 +1210,7 @@ void intel_pt_interrupt(void)
 		/* snapshot counters don't use PMI, so it's safe */
 		ret = pt_buffer_reset_markers(buf, &pt->handle);
 		if (ret) {
-			perf_aux_output_end(&pt->handle, 0, true);
+			perf_aux_output_end(&pt->handle, 0);
 			return;
 		}
 
@@ -1280,7 +1282,7 @@ static void pt_event_start(struct perf_event *event, int mode)
 	return;
 
 fail_end_stop:
-	perf_aux_output_end(&pt->handle, 0, true);
+	perf_aux_output_end(&pt->handle, 0);
 fail_stop:
 	hwc->state = PERF_HES_STOPPED;
 }
@@ -1321,8 +1323,7 @@ static void pt_event_stop(struct perf_event *event, int mode)
 			pt->handle.head =
 				local_xchg(&buf->data_size,
 					   buf->nr_pages << PAGE_SHIFT);
-		perf_aux_output_end(&pt->handle, local_xchg(&buf->data_size, 0),
-				    local_xchg(&buf->lost, 0));
+		perf_aux_output_end(&pt->handle, local_xchg(&buf->data_size, 0));
 	}
 }
 
arch/x86/events/intel/pt.h +0 -1
index 53473c21b554..b528e8f373e4 100644
@@ -143,7 +143,6 @@ struct pt_buffer {
 	size_t			output_off;
 	unsigned long		nr_pages;
 	local_t			data_size;
-	local_t			lost;
 	local64_t		head;
 	bool			snapshot;
 	unsigned long		stop_pos, intr_pos;
drivers/hwtracing/coresight/coresight-etb10.c +4 -5
index d7325c6534ad..979ea6ec7902 100644
@@ -321,7 +321,7 @@ static int etb_set_buffer(struct coresight_device *csdev,
 
 static unsigned long etb_reset_buffer(struct coresight_device *csdev,
 				      struct perf_output_handle *handle,
-				      void *sink_config, bool *lost)
+				      void *sink_config)
 {
 	unsigned long size = 0;
 	struct cs_buffers *buf = sink_config;
@@ -343,7 +343,6 @@ static unsigned long etb_reset_buffer(struct coresight_device *csdev,
 		 * resetting parameters here and squaring off with the ring
 		 * buffer API in the tracer PMU is fine.
 		 */
-		*lost = !!local_xchg(&buf->lost, 0);
 		size = local_xchg(&buf->data_size, 0);
 	}
 
@@ -385,7 +384,7 @@ static void etb_update_buffer(struct coresight_device *csdev,
 			(unsigned long)write_ptr);
 
 		write_ptr &= ~(ETB_FRAME_SIZE_WORDS - 1);
-		local_inc(&buf->lost);
+		perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
 	}
 
 	/*
@@ -396,7 +395,7 @@ static void etb_update_buffer(struct coresight_device *csdev,
 	 */
 	status = readl_relaxed(drvdata->base + ETB_STATUS_REG);
 	if (status & ETB_STATUS_RAM_FULL) {
-		local_inc(&buf->lost);
+		perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
 		to_read = capacity;
 		read_ptr = write_ptr;
 	} else {
@@ -429,7 +428,7 @@ static void etb_update_buffer(struct coresight_device *csdev,
 		if (read_ptr > (drvdata->buffer_depth - 1))
 			read_ptr -= drvdata->buffer_depth;
 		/* let the decoder know we've skipped ahead */
-		local_inc(&buf->lost);
+		perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
 	}
 
 	/* finally tell HW where we want to start reading from */
drivers/hwtracing/coresight/coresight-etm-perf.c +4 -5
index 26cfac3e6de7..288a423c1b27 100644
@@ -302,7 +302,8 @@ static void etm_event_start(struct perf_event *event, int flags)
 	return;
 
 fail_end_stop:
-	perf_aux_output_end(handle, 0, true);
+	perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
+	perf_aux_output_end(handle, 0);
 fail:
 	event->hw.state = PERF_HES_STOPPED;
 	goto out;
@@ -310,7 +311,6 @@ static void etm_event_start(struct perf_event *event, int flags)
 
 static void etm_event_stop(struct perf_event *event, int mode)
 {
-	bool lost;
 	int cpu = smp_processor_id();
 	unsigned long size;
 	struct coresight_device *sink, *csdev = per_cpu(csdev_src, cpu);
@@ -348,10 +348,9 @@ static void etm_event_stop(struct perf_event *event, int mode)
 			return;
 
 		size = sink_ops(sink)->reset_buffer(sink, handle,
-						    event_data->snk_config,
-						    &lost);
+						    event_data->snk_config);
 
-		perf_aux_output_end(handle, size, lost);
+		perf_aux_output_end(handle, size);
 	}
 
 	/* Disabling the path make its elements available to other sessions */
drivers/hwtracing/coresight/coresight-priv.h +0 -2
index ef9d8e93e3b2..5f662d82052c 100644
@@ -76,7 +76,6 @@ enum cs_mode {
  * @nr_pages:	max number of pages granted to us
  * @offset:	offset within the current buffer
  * @data_size:	how much we collected in this run
- * @lost:	other than zero if we had a HW buffer wrap around
  * @snapshot:	is this run in snapshot mode
  * @data_pages:	a handle the ring buffer
  */
@@ -85,7 +84,6 @@ struct cs_buffers {
 	unsigned int		nr_pages;
 	unsigned long		offset;
 	local_t			data_size;
-	local_t			lost;
 	bool			snapshot;
 	void			**data_pages;
 };
drivers/hwtracing/coresight/coresight-tmc-etf.c +3 -4
index 1549436e2492..aec61a6d5c63 100644
@@ -329,7 +329,7 @@ static int tmc_set_etf_buffer(struct coresight_device *csdev,
 
 static unsigned long tmc_reset_etf_buffer(struct coresight_device *csdev,
 					  struct perf_output_handle *handle,
-					  void *sink_config, bool *lost)
+					  void *sink_config)
 {
 	long size = 0;
 	struct cs_buffers *buf = sink_config;
@@ -350,7 +350,6 @@ static unsigned long tmc_reset_etf_buffer(struct coresight_device *csdev,
 		 * resetting parameters here and squaring off with the ring
 		 * buffer API in the tracer PMU is fine.
 		 */
-		*lost = !!local_xchg(&buf->lost, 0);
 		size = local_xchg(&buf->data_size, 0);
 	}
 
@@ -389,7 +388,7 @@ static void tmc_update_etf_buffer(struct coresight_device *csdev,
 	 */
 	status = readl_relaxed(drvdata->base + TMC_STS);
 	if (status & TMC_STS_FULL) {
-		local_inc(&buf->lost);
+		perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
 		to_read = drvdata->size;
 	} else {
 		to_read = CIRC_CNT(write_ptr, read_ptr, drvdata->size);
@@ -434,7 +433,7 @@ static void tmc_update_etf_buffer(struct coresight_device *csdev,
 			read_ptr -= drvdata->size;
 		/* Tell the HW */
 		writel_relaxed(read_ptr, drvdata->base + TMC_RRP);
-		local_inc(&buf->lost);
+		perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
 	}
 
 	cur = buf->cur;
include/linux/coresight.h +1 -1
index 2a5982c37dfb..035c16c9a505 100644
@@ -201,7 +201,7 @@ struct coresight_ops_sink {
 			  void *sink_config);
 	unsigned long (*reset_buffer)(struct coresight_device *csdev,
 				      struct perf_output_handle *handle,
-				      void *sink_config, bool *lost);
+				      void *sink_config);
 	void (*update_buffer)(struct coresight_device *csdev,
 			      struct perf_output_handle *handle,
 			      void *sink_config);
include/linux/perf_event.h +5 -3
index 000fdb211c7d..7f8cf0eba230 100644
@@ -801,6 +801,7 @@ struct perf_output_handle {
 	struct ring_buffer		*rb;
 	unsigned long			wakeup;
 	unsigned long			size;
+	u64				aux_flags;
 	union {
 		void			*addr;
 		unsigned long		head;
@@ -849,10 +850,11 @@ perf_cgroup_from_task(struct task_struct *task, struct perf_event_context *ctx)
 extern void *perf_aux_output_begin(struct perf_output_handle *handle,
 				   struct perf_event *event);
 extern void perf_aux_output_end(struct perf_output_handle *handle,
-				unsigned long size, bool truncated);
+				unsigned long size);
 extern int perf_aux_output_skip(struct perf_output_handle *handle,
 				unsigned long size);
 extern void *perf_get_aux(struct perf_output_handle *handle);
+extern void perf_aux_output_flag(struct perf_output_handle *handle, u64 flags);
 
 extern int perf_pmu_register(struct pmu *pmu, const char *name, int type);
 extern void perf_pmu_unregister(struct pmu *pmu);
@@ -1267,8 +1269,8 @@ static inline void *
 perf_aux_output_begin(struct perf_output_handle *handle,
 		      struct perf_event *event)				{ return NULL; }
 static inline void
-perf_aux_output_end(struct perf_output_handle *handle, unsigned long size,
-		    bool truncated)					{ }
+perf_aux_output_end(struct perf_output_handle *handle, unsigned long size)
+									{ }
 static inline int
 perf_aux_output_skip(struct perf_output_handle *handle,
 		     unsigned long size)				{ return -EINVAL; }
kernel/events/ring_buffer.c +23 -11
index 257fa460b846..9654e55c38d6 100644
@@ -297,6 +297,19 @@ ring_buffer_init(struct ring_buffer *rb, long watermark, int flags)
 		rb->paused = 1;
 }
 
+void perf_aux_output_flag(struct perf_output_handle *handle, u64 flags)
+{
+	/*
+	 * OVERWRITE is determined by perf_aux_output_end() and can't
+	 * be passed in directly.
+	 */
+	if (WARN_ON_ONCE(flags & PERF_AUX_FLAG_OVERWRITE))
+		return;
+
+	handle->aux_flags |= flags;
+}
+EXPORT_SYMBOL_GPL(perf_aux_output_flag);
+
 /*
  * This is called before hardware starts writing to the AUX area to
  * obtain an output handle and make sure there's room in the buffer.
@@ -360,6 +373,7 @@ void *perf_aux_output_begin(struct perf_output_handle *handle,
 	handle->event = event;
 	handle->head = aux_head;
 	handle->size = 0;
+	handle->aux_flags = 0;
 
 	/*
 	 * In overwrite mode, AUX data stores do not depend on aux_tail,
@@ -408,34 +422,32 @@ void *perf_aux_output_begin(struct perf_output_handle *handle,
  * of the AUX buffer management code is that after pmu::stop(), the AUX
  * transaction must be stopped and therefore drop the AUX reference count.
  */
-void perf_aux_output_end(struct perf_output_handle *handle, unsigned long size,
-			 bool truncated)
+void perf_aux_output_end(struct perf_output_handle *handle, unsigned long size)
 {
 	struct ring_buffer *rb = handle->rb;
-	bool wakeup = truncated;
+	bool wakeup = !!handle->aux_flags;
 	unsigned long aux_head;
-	u64 flags = 0;
-
-	if (truncated)
-		flags |= PERF_AUX_FLAG_TRUNCATED;
 
 	/* in overwrite mode, driver provides aux_head via handle */
 	if (rb->aux_overwrite) {
-		flags |= PERF_AUX_FLAG_OVERWRITE;
+		handle->aux_flags |= PERF_AUX_FLAG_OVERWRITE;
 
 		aux_head = handle->head;
 		local_set(&rb->aux_head, aux_head);
 	} else {
+		handle->aux_flags &= ~PERF_AUX_FLAG_OVERWRITE;
+
 		aux_head = local_read(&rb->aux_head);
 		local_add(size, &rb->aux_head);
 	}
 
-	if (size || flags) {
+	if (size || handle->aux_flags) {
 		/*
 		 * Only send RECORD_AUX if we have something useful to communicate
 		 */
 
-		perf_event_aux_event(handle->event, aux_head, size, flags);
+		perf_event_aux_event(handle->event, aux_head, size,
+		                     handle->aux_flags);
 	}
 
 	aux_head = rb->user_page->aux_head = local_read(&rb->aux_head);
@@ -446,7 +458,7 @@ void perf_aux_output_end(struct perf_output_handle *handle, unsigned long size,
 	}
 
 	if (wakeup) {
-		if (truncated)
+		if (handle->aux_flags & PERF_AUX_FLAG_TRUNCATED)
 			handle->event->pending_disable = 1;
 		perf_output_wakeup(handle);
 	}
Signature · 1 line
2.1.4
#2 lore raw

PATCH v2 2/6 genirq: export irq_get_percpu_devid_partition to modules

In reply to Will Deacon

Any modular driver using cluster-affine PPIs needs to be able to call irq_get_percpu_devid_partition so that it can enable the IRQ on the correct subset of CPUs.

This patch exports the symbol so that it can be called from within a module.

---
 kernel/irq/irqdesc.c | 1 +
 1 file changed, 1 insertion(+)
kernel/irq/irqdesc.c +1 -0
index 00bb0aeea1d0..1e6ae73eae59 100644
@@ -856,6 +856,7 @@ int irq_get_percpu_devid_partition(unsigned int irq, struct cpumask *affinity)
 
 	return 0;
 }
+EXPORT_SYMBOL_GPL(irq_get_percpu_devid_partition);
 
 void kstat_incr_irq_this_cpu(unsigned int irq)
 {
Signature · 1 line
2.1.4
#3 lore raw

PATCH v2 3/6 perf/core: Export AUX buffer helpers to modules

In reply to Will Deacon

Perf PMU drivers using AUX buffers cannot be built as modules unless the AUX helpers are exported.

This patch exports perf_aux_output_{begin,end,skip} and perf_get_aux to modules.

Cc Peter Zijlstra <[email protected]>
---
 kernel/events/ring_buffer.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/events/ring_buffer.c +4 -0
index 9654e55c38d6..f3ebafe06099 100644
@@ -411,6 +411,7 @@ void *perf_aux_output_begin(struct perf_output_handle *handle,
 
 	return NULL;
 }
+EXPORT_SYMBOL_GPL(perf_aux_output_begin);
 
 /*
  * Commit the data written by hardware into the ring buffer by adjusting
@@ -470,6 +471,7 @@ void perf_aux_output_end(struct perf_output_handle *handle, unsigned long size)
 	rb_free_aux(rb);
 	ring_buffer_put(rb);
 }
+EXPORT_SYMBOL_GPL(perf_aux_output_end);
 
 /*
  * Skip over a given number of bytes in the AUX buffer, due to, for example,
@@ -498,6 +500,7 @@ int perf_aux_output_skip(struct perf_output_handle *handle, unsigned long size)
 
 	return 0;
 }
+EXPORT_SYMBOL_GPL(perf_aux_output_skip);
 
 void *perf_get_aux(struct perf_output_handle *handle)
 {
@@ -507,6 +510,7 @@ void *perf_get_aux(struct perf_output_handle *handle)
 
 	return handle->rb->aux_priv;
 }
+EXPORT_SYMBOL_GPL(perf_get_aux);
 
 #define PERF_AUX_GFP	(GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY)
 
Signature · 1 line
2.1.4
#4 lore raw

PATCH v2 4/6 perf/core: Add PERF_AUX_FLAG_COLLISION to report colliding samples

In reply to Will Deacon

The ARM SPE architecture permits an implementation to ignore a sample if the sample is due to be taken whilst another sample is already being produced. In this case, it is desirable to report the collision to userspace, as they may want to lower the sample period.

This patch adds a PERF_AUX_FLAG_COLLISION flag, so that such events can be relayed to userspace.

---
 include/uapi/linux/perf_event.h | 1 +
 1 file changed, 1 insertion(+)
include/uapi/linux/perf_event.h +1 -0
index c66a485a24ac..f4e3a21cade5 100644
@@ -885,6 +885,7 @@ enum perf_callchain_context {
  */
 #define PERF_AUX_FLAG_TRUNCATED		0x01	/* record was truncated to fit */
 #define PERF_AUX_FLAG_OVERWRITE		0x02	/* snapshot from overwrite mode */
+#define PERF_AUX_FLAG_COLLISION		0x08	/* sample collided with another */
 
 #define PERF_FLAG_FD_NO_GROUP		(1UL << 0)
 #define PERF_FLAG_FD_OUTPUT		(1UL << 1)
Signature · 1 line
2.1.4
#5 lore raw

PATCH v2 5/6 drivers/perf: Add support for ARMv8.2 Statistical Profiling Extension

In reply to Will Deacon

The ARMv8.2 architecture introduces the optional Statistical Profiling Extension (SPE).

SPE can be used to profile a population of operations in the CPU pipeline after instruction decode. These are either architected instructions (i.e. a dynamic instruction trace) or CPU-specific uops and the choice is fixed statically in the hardware and advertised to userspace via caps/. Sampling is controlled using a sampling interval, similar to a regular PMU counter, but also with an optional random perturbation to avoid falling into patterns where you continuously profile the same instruction in a hot loop.

After each operation is decoded, the interval counter is decremented. When it hits zero, an operation is chosen for profiling and tracked within the pipeline until it retires. Along the way, information such as TLB lookups, cache misses, time spent to issue etc is captured in the form of a sample. The sample is then filtered according to certain criteria (e.g. load latency) that can be specified in the event config (described under format/) and, if the sample satisfies the filter, it is written out to memory as a record, otherwise it is discarded. Only one operation can be sampled at a time.

The in-memory buffer is linear and virtually addressed, raising an interrupt when it fills up. The PMU driver handles these interrupts to give the appearance of a ring buffer, as expected by the AUX code.

The in-memory trace-like format is self-describing (though not parseable in reverse) and written as a series of records, with each record corresponding to a sample and consisting of a sequence of packets. These packets are defined by the architecture, although some have CPU-specific fields for recording information specific to the microarchitecture.

As a simple example, a record generated for a branch instruction may consist of the following packets:

0 (Address) : Virtual PC of the branch instruction
1 (Type) : Conditional direct branch
2 (Counter) : Number of cycles taken from Dispatch to Issue 3 (Address) : Virtual branch target + condition flags 4 (Counter) : Number of cycles taken from Dispatch to Complete 5 (Events) : Mispredicted as not-taken
6 (END) : End of record

It is also possible to toggle properties such as timestamp packets in each record.

This patch adds support for SPE in the form of a new perf driver.

Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
---
 drivers/perf/Kconfig       |    8 +
 drivers/perf/Makefile      |    1 +
 drivers/perf/arm_spe_pmu.c | 1253 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 1262 insertions(+)
 create mode 100644 drivers/perf/arm_spe_pmu.c
drivers/perf/Kconfig +8 -0
index 93651907874f..d0fbd73478eb 100644
@@ -28,4 +28,12 @@ config XGENE_PMU
         help
           Say y if you want to use APM X-Gene SoC performance monitors.
 
+config ARM_SPE_PMU
+	tristate "Enable support for the ARMv8.2 Statistical Profiling Extension"
+	depends on PERF_EVENTS && ARM64
+	help
+	  Enable perf support for the ARMv8.2 Statistical Profiling
+	  Extension, which provides periodic sampling of operations in
+	  the CPU pipeline and reports this via the perf AUX interface.
+
 endmenu
drivers/perf/Makefile +1 -0
index ef24833c94a8..35fac04ef8c1 100644
@@ -1,3 +1,4 @@
 obj-$(CONFIG_ARM_PMU) += arm_pmu.o
 obj-$(CONFIG_QCOM_L2_PMU)	+= qcom_l2_pmu.o
 obj-$(CONFIG_XGENE_PMU) += xgene_pmu.o
+obj-$(CONFIG_ARM_SPE_PMU) += arm_spe_pmu.o
drivers/perf/arm_spe_pmu.c new file +1253 -0

1,253 lines not on this page · raw message

Signature · 1 line
2.1.4
#6 lore raw

Re: [PATCH v2 5/6] drivers/perf: Add support for ARMv8.2 Statistical Profiling Extension

On Thu, 6 Apr 2017 17:18:15 +0100
Will Deacon <[email protected]> wrote:

Hi Will,

Quoted 47 lines
drivers/perf/arm_spe_pmu.c new file +47 -0
+/* Perf callbacks */
+static int arm_spe_pmu_event_init(struct perf_event *event)
+{
+	u64 reg;
+	struct perf_event_attr *attr = &event->attr;
+	struct arm_spe_pmu *spe_pmu = to_spe_pmu(event->pmu);
+
+	/* This is, of course, deeply driver-specific */
+	if (attr->type != event->pmu->type)
+		return -ENOENT;
+
+	if (event->cpu >= 0 &&
+	    !cpumask_test_cpu(event->cpu, &spe_pmu->supported_cpus))
+		return -ENOENT;
+
+	if (arm_spe_event_to_pmsevfr(event) & PMSEVFR_EL1_RES0)
+		return -EOPNOTSUPP;
+
+	if (event->hw.sample_period < spe_pmu->min_period ||
+	    event->hw.sample_period & PMSIRR_EL1_IVAL_MASK)
+		return -EOPNOTSUPP;
+
+	if (attr->exclude_idle)
+		return -EOPNOTSUPP;
+
+	/*
+	 * Feedback-directed frequency throttling doesn't work when we
+	 * have a buffer of samples. We'd need to manually count the
+	 * samples in the buffer when it fills up and adjust the event
+	 * count to reflect that. Instead, force the user to specify a
+	 * sample period instead.
+	 */
+	if (attr->freq)
+		return -EINVAL;
+
+	if (is_kernel_in_hyp_mode()) {
+		if (attr->exclude_kernel != attr->exclude_hv)
+			return -EOPNOTSUPP;
+	} else if (!attr->exclude_hv) {
+		return -EOPNOTSUPP;
+	}
+
+	reg = arm_spe_event_to_pmsfcr(event);
+	if ((reg & BIT(PMSFCR_EL1_FE_SHIFT)) &&
+	    !(spe_pmu->features & SPE_PMU_FEAT_FILT_EVT))
+		return -EOPNOTSUPP;
+

drivers/perf/arm_spe_pmu.c line 718

Quoted 10 lines
drivers/perf/arm_spe_pmu.c new file +10 -0
+	if ((reg & BIT(PMSFCR_EL1_FT_SHIFT)) &&
+	    !(spe_pmu->features & SPE_PMU_FEAT_FILT_TYP))
+		return -EOPNOTSUPP;
+
+	if ((reg & BIT(PMSFCR_EL1_FL_SHIFT)) &&
+	    !(spe_pmu->features & SPE_PMU_FEAT_FILT_LAT))
+		return -EOPNOTSUPP;
+
+	return 0;
+}

Can you please explain why we're not emitting messages to dmesg here?:

https://patchwork.kernel.org/patch/9545979/

I sure find them useful.

Thanks,

Kim

maintainer
#7 lore raw

Re: [PATCH v2 5/6] drivers/perf: Add support for ARMv8.2 Statistical Profiling Extension

Quoted 65 lines · Kim Phillips

On Thu, 6 Apr 2017 17:18:15 +0100
Will Deacon <[email protected]> wrote:

Hi Will,

Quoted 47 lines
drivers/perf/arm_spe_pmu.c new file +47 -0
+/* Perf callbacks */
+static int arm_spe_pmu_event_init(struct perf_event *event)
+{
+	u64 reg;
+	struct perf_event_attr *attr = &event->attr;
+	struct arm_spe_pmu *spe_pmu = to_spe_pmu(event->pmu);
+
+	/* This is, of course, deeply driver-specific */
+	if (attr->type != event->pmu->type)
+		return -ENOENT;
+
+	if (event->cpu >= 0 &&
+	    !cpumask_test_cpu(event->cpu, &spe_pmu->supported_cpus))
+		return -ENOENT;
+
+	if (arm_spe_event_to_pmsevfr(event) & PMSEVFR_EL1_RES0)
+		return -EOPNOTSUPP;
+
+	if (event->hw.sample_period < spe_pmu->min_period ||
+	    event->hw.sample_period & PMSIRR_EL1_IVAL_MASK)
+		return -EOPNOTSUPP;
+
+	if (attr->exclude_idle)
+		return -EOPNOTSUPP;
+
+	/*
+	 * Feedback-directed frequency throttling doesn't work when we
+	 * have a buffer of samples. We'd need to manually count the
+	 * samples in the buffer when it fills up and adjust the event
+	 * count to reflect that. Instead, force the user to specify a
+	 * sample period instead.
+	 */
+	if (attr->freq)
+		return -EINVAL;
+
+	if (is_kernel_in_hyp_mode()) {
+		if (attr->exclude_kernel != attr->exclude_hv)
+			return -EOPNOTSUPP;
+	} else if (!attr->exclude_hv) {
+		return -EOPNOTSUPP;
+	}
+
+	reg = arm_spe_event_to_pmsfcr(event);
+	if ((reg & BIT(PMSFCR_EL1_FE_SHIFT)) &&
+	    !(spe_pmu->features & SPE_PMU_FEAT_FILT_EVT))
+		return -EOPNOTSUPP;
+

drivers/perf/arm_spe_pmu.c line 718

Quoted 10 lines
drivers/perf/arm_spe_pmu.c new file +10 -0
+	if ((reg & BIT(PMSFCR_EL1_FT_SHIFT)) &&
+	    !(spe_pmu->features & SPE_PMU_FEAT_FILT_TYP))
+		return -EOPNOTSUPP;
+
+	if ((reg & BIT(PMSFCR_EL1_FL_SHIFT)) &&
+	    !(spe_pmu->features & SPE_PMU_FEAT_FILT_LAT))
+		return -EOPNOTSUPP;
+
+	return 0;
+}

Can you please explain why we're not emitting messages to dmesg here?:

Quoted 1 line

https://patchwork.kernel.org/patch/9545979/

The above cases are not (system) errors, and using dev_err (even ratelimited) is certainly not appropriate. These are pr_debug() at best.

The dmesg is not always the appropriate place to dump such information, even if it happens to be convenient. As part of usual operation, dmesg should be very quiet, and we don't log messages elsewhere where the user passes some parameter the kernel does not like.

These messages are really only useful to those developing tools (such as yourself). There are some cases where they're actively harmful (e.g. when fuzzing).

Adding a single patch doesn't seem that difficult. Maybe we could add a pr_debug() or two.

Thanks,
Mark.

#8 lore raw

Re: [PATCH v2 5/6] drivers/perf: Add support for ARMv8.2 Statistical Profiling Extension

Quoted 66 lines · On Thu, 6 Apr 2017 19:45:04 +0100 Mark Rutland <[email protected]> wrote:
Quoted 65 lines · Kim Phillips

On Thu, 6 Apr 2017 17:18:15 +0100
Will Deacon <[email protected]> wrote:

Hi Will,

Quoted 47 lines
drivers/perf/arm_spe_pmu.c new file +47 -0
+/* Perf callbacks */
+static int arm_spe_pmu_event_init(struct perf_event *event)
+{
+	u64 reg;
+	struct perf_event_attr *attr = &event->attr;
+	struct arm_spe_pmu *spe_pmu = to_spe_pmu(event->pmu);
+
+	/* This is, of course, deeply driver-specific */
+	if (attr->type != event->pmu->type)
+		return -ENOENT;
+
+	if (event->cpu >= 0 &&
+	    !cpumask_test_cpu(event->cpu, &spe_pmu->supported_cpus))
+		return -ENOENT;
+
+	if (arm_spe_event_to_pmsevfr(event) & PMSEVFR_EL1_RES0)
+		return -EOPNOTSUPP;
+
+	if (event->hw.sample_period < spe_pmu->min_period ||
+	    event->hw.sample_period & PMSIRR_EL1_IVAL_MASK)
+		return -EOPNOTSUPP;
+
+	if (attr->exclude_idle)
+		return -EOPNOTSUPP;
+
+	/*
+	 * Feedback-directed frequency throttling doesn't work when we
+	 * have a buffer of samples. We'd need to manually count the
+	 * samples in the buffer when it fills up and adjust the event
+	 * count to reflect that. Instead, force the user to specify a
+	 * sample period instead.
+	 */
+	if (attr->freq)
+		return -EINVAL;
+
+	if (is_kernel_in_hyp_mode()) {
+		if (attr->exclude_kernel != attr->exclude_hv)
+			return -EOPNOTSUPP;
+	} else if (!attr->exclude_hv) {
+		return -EOPNOTSUPP;
+	}
+
+	reg = arm_spe_event_to_pmsfcr(event);
+	if ((reg & BIT(PMSFCR_EL1_FE_SHIFT)) &&
+	    !(spe_pmu->features & SPE_PMU_FEAT_FILT_EVT))
+		return -EOPNOTSUPP;
+

drivers/perf/arm_spe_pmu.c line 718

Quoted 10 lines
drivers/perf/arm_spe_pmu.c new file +10 -0
+	if ((reg & BIT(PMSFCR_EL1_FT_SHIFT)) &&
+	    !(spe_pmu->features & SPE_PMU_FEAT_FILT_TYP))
+		return -EOPNOTSUPP;
+
+	if ((reg & BIT(PMSFCR_EL1_FL_SHIFT)) &&
+	    !(spe_pmu->features & SPE_PMU_FEAT_FILT_LAT))
+		return -EOPNOTSUPP;
+
+	return 0;
+}

Can you please explain why we're not emitting messages to dmesg here?:

Quoted 4 lines
Quoted 1 line

https://patchwork.kernel.org/patch/9545979/

The above cases are not (system) errors, and using dev_err (even ratelimited) is certainly not appropriate. These are pr_debug() at best.

They are driver errors: the input parameters to the driver were bad, and it failed to execute. pr_debug - to me at least - should indicate progress during normal operation.

Quoted 7 lines

The dmesg is not always the appropriate place to dump such information, even if it happens to be convenient. As part of usual operation, dmesg should be very quiet, and we don't log messages elsewhere where the user passes some parameter the kernel does not like.

These messages are really only useful to those developing tools (such as yourself).

We had a customer come back with a simple usage failure because they were running a kernel with a different VHE configuration, blindly failing the hv exclusion check above. They had to manually modify the driver to find the cause. So it affects all users, not just me. Unless you're implying the above code be duplicated in the perf tool somehow?

Quoted 2 lines

There are some cases where they're actively harmful (e.g. when fuzzing).

I'd expect fuzzer users to be more amenable to manually modifying the driver rather than regular users of the driver.

Quoted 1 line

Adding a single patch doesn't seem that difficult.

Can we not address the issue in this original patch that causes it?

Quoted 1 line

Maybe we could add a pr_debug() or two.

If the diff I sent had too many, by all means, let's discuss in the form of a review.

Kim

maintainer
#9 lore raw

Re: [PATCH v2 5/6] drivers/perf: Add support for ARMv8.2 Statistical Profiling Extension

Hi Kim,

Quoted 12 lines · Kim Phillips
Quoted 10 lines · On Thu, 6 Apr 2017 19:45:04 +0100 Mark Rutland <[email protected]> wrote:
Quoted 9 lines · Kim Phillips

drivers/perf/arm_spe_pmu.c line 715

Quoted 3 lines · On Thu, 6 Apr 2017 17:18:15 +0100 Will Deacon <[email protected]> wrote:
drivers/perf/arm_spe_pmu.c new file +3 -0
+	if ((reg & BIT(PMSFCR_EL1_FL_SHIFT)) &&
+	    !(spe_pmu->features & SPE_PMU_FEAT_FILT_LAT))
+		return -EOPNOTSUPP;

Can you please explain why we're not emitting messages to dmesg here?:

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

https://patchwork.kernel.org/patch/9545979/

The above cases are not (system) errors, and using dev_err (even ratelimited) is certainly not appropriate. These are pr_debug() at best.

They are driver errors: the input parameters to the driver were bad, and it failed to execute.

This is not a driver error; the user requested something that the driver does not or cannot support, and the driver responded appropriately and correctly.

A *_err print is for when something is truly wrong at the system level, for example if the HW behaves unexpectedly, FW reports something invalid, or some kernel code invariant has been violated. It is not for every case where an error code is returned.

Quoted 2 lines

pr_debug - to me at least - should indicate progress during normal operation.

Quite frankly, the above is the normal operation of the driver. We don't pr_err in every syscall path when validating arguments provided by the user, and this is no different.

Quoted 5 lines
Quoted 5 lines

The dmesg is not always the appropriate place to dump such information, even if it happens to be convenient. As part of usual operation, dmesg should be very quiet, and we don't log messages elsewhere where the user passes some parameter the kernel does not like.

Quoted 7 lines
Quoted 2 lines

These messages are really only useful to those developing tools (such as yourself).

We had a customer come back with a simple usage failure because they were running a kernel with a different VHE configuration, blindly failing the hv exclusion check above. They had to manually modify the driver to find the cause. So it affects all users, not just me.

I agree that we can and should do something better for regular users. I disagree that dmesg is the solution.

What we need to do is expose information such that the tool can provide useful messages at the point of use, which are guaranteed to correspond to a particular action.

A user may not be aware of dmesg (e.g. if they're SSH'd in they're unlikely to see it), and cannot match messages to particular actions when there are multiple applications and/or users. So this doesn't solve the general case.

Quoted 2 lines

Unless you're implying the above code be duplicated in the perf tool somehow?

Some feature probing could be done by the tool. We already do that today for CPU PMUs. If you take a look at perf_evsel__open, you can see it automatically determines whether the kernel supports guest exclusion for CPU PMU events.

We might be able to do something similar by default to cater for the VHE case you mentioned above.

We could also expose information under sysfs to explicitly tell the tool what is and is not supported by the driver.

Quoted 5 lines
Quoted 2 lines

There are some cases where they're actively harmful (e.g. when fuzzing).

I'd expect fuzzer users to be more amenable to manually modifying the driver rather than regular users of the driver.

When fuzzing, I take a mainline, defconfig kernel, and run it through its paces. I don't touch each and every driver.

As above, prints are not the solution for regular users.

Thanks,
Mark.

#10 lore raw

Re: [PATCH v2 5/6] drivers/perf: Add support for ARMv8.2 Statistical Profiling Extension

Quoted 1 line · On Fri, 7 Apr 2017 12:31:07 +0100 Mark Rutland <[email protected]> wrote:

Hi Kim,

Hi Mark,

Quoted 21 lines · Kim Phillips
Quoted 12 lines
Quoted 10 lines · On Thu, 6 Apr 2017 19:45:04 +0100 Mark Rutland <[email protected]> wrote:
Quoted 9 lines · Kim Phillips

drivers/perf/arm_spe_pmu.c line 715

Quoted 3 lines · On Thu, 6 Apr 2017 17:18:15 +0100 Will Deacon <[email protected]> wrote:
drivers/perf/arm_spe_pmu.c new file +3 -0
+	if ((reg & BIT(PMSFCR_EL1_FL_SHIFT)) &&
+	    !(spe_pmu->features & SPE_PMU_FEAT_FILT_LAT))
+		return -EOPNOTSUPP;

Can you please explain why we're not emitting messages to dmesg here?:

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

https://patchwork.kernel.org/patch/9545979/

The above cases are not (system) errors, and using dev_err (even ratelimited) is certainly not appropriate. These are pr_debug() at best.

They are driver errors: the input parameters to the driver were bad, and it failed to execute.

Quoted 8 lines

This is not a driver error; the user requested something that the driver does not or cannot support, and the driver responded appropriately and correctly.

A *_err print is for when something is truly wrong at the system level, for example if the HW behaves unexpectedly, FW reports something invalid, or some kernel code invariant has been violated. It is not for every case where an error code is returned.

The driver is trying to report an error: in the above example, it's reporting that it cannot support an operation by returning -*E*OPNOTSUPP: an ERROR because it was unable to complete the request:
the request failed. Unlike e.g., a warning where something may not have been quite right, but went along with executing the operation anyway.

Quoted 6 lines
Quoted 2 lines

pr_debug - to me at least - should indicate progress during normal operation.

Quite frankly, the above is the normal operation of the driver. We don't pr_err in every syscall path when validating arguments provided by the user, and this is no different.

It is different because this particular system call's error reporting is notoriously bad, vis-a-vis this discussion.

debug-level messages such as pr_debug, dev_dbg can easily be hidden from the user, depending on the distro's default dmesg level preferences.

Quoted 16 lines
Quoted 5 lines
Quoted 5 lines

The dmesg is not always the appropriate place to dump such information, even if it happens to be convenient. As part of usual operation, dmesg should be very quiet, and we don't log messages elsewhere where the user passes some parameter the kernel does not like.

Quoted 7 lines
Quoted 2 lines

These messages are really only useful to those developing tools (such as yourself).

We had a customer come back with a simple usage failure because they were running a kernel with a different VHE configuration, blindly failing the hv exclusion check above. They had to manually modify the driver to find the cause. So it affects all users, not just me.

I agree that we can and should do something better for regular users. I disagree that dmesg is the solution.

Quoted 8 lines

What we need to do is expose information such that the tool can provide useful messages at the point of use, which are guaranteed to correspond to a particular action.

A user may not be aware of dmesg (e.g. if they're SSH'd in they're unlikely to see it), and cannot match messages to particular actions when there are multiple applications and/or users. So this doesn't solve the general case.

dmesg isn't the ultimate solution, no, but it's currently what we have.

Meanwhile, perf occasionally says things like:

"/bin/dmesg may provide additional information"

so people know to look there, for now at least.

Quoted 8 lines
Quoted 2 lines

Unless you're implying the above code be duplicated in the perf tool somehow?

Some feature probing could be done by the tool. We already do that today for CPU PMUs. If you take a look at perf_evsel__open, you can see it automatically determines whether the kernel supports guest exclusion for CPU PMU events.

Quoted 5 lines

We might be able to do something similar by default to cater for the VHE case you mentioned above.

We could also expose information under sysfs to explicitly tell the tool what is and is not supported by the driver.

This is essentially the gist of the code in question: that's exactly what it's doing, except it's not doing it via sysfs.

I think the best solution is what Will originally pointed me to during an earlier review round: adding capability to perf syscalls to return error strings:

https://lkml.org/lkml/2015/8/24/506

but we're not there yet.

Quoted 10 lines
Quoted 5 lines
Quoted 2 lines

There are some cases where they're actively harmful (e.g. when fuzzing).

I'd expect fuzzer users to be more amenable to manually modifying the driver rather than regular users of the driver.

When fuzzing, I take a mainline, defconfig kernel, and run it through its paces. I don't touch each and every driver.

As above, prints are not the solution for regular users.

In the context of this patch review, dmesg is all we have for now:
let's use it please.

Kim

#11 lore raw

PATCH v2 6/6 dt-bindings: Document devicetree binding for ARM SPE

In reply to Will Deacon

This patch documents the devicetree binding in use for ARM SPE.

Cc Mark Rutland <[email protected]>
Cc Rob Herring <[email protected]>
---
 Documentation/devicetree/bindings/arm/spe-pmu.txt | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/arm/spe-pmu.txt
Documentation/devicetree/bindings/arm/spe-pmu.txt new file +20 -0
new file mode 100644
index 000000000000..93372f2a7df9
@@ -0,0 +1,20 @@
+* ARMv8.2 Statistical Profiling Extension (SPE) Performance Monitor Units (PMU)
+
+ARMv8.2 introduces the optional Statistical Profiling Extension for collecting
+performance sample data using an in-memory trace buffer.
+
+** SPE Required properties:
+
+- compatible : should be one of:
+	       "arm,statistical-profiling-extension-v1"
+
+- interrupts : Exactly 1 PPI must be listed. For heterogeneous systems where
+               SPE is only supported on a subset of the CPUs, please consult
+	       the arm,gic-v3 binding for details on describing a PPI partition.
+
+** Example:
+
+spe-pmu {
+        compatible = "arm,statistical-profiling-extension-v1";
+        interrupts = <GIC_PPI 05 IRQ_TYPE_LEVEL_HIGH &part1>;
+};
Signature · 1 line
2.1.4