Skip to contentSkip to conversation

perf, x86: Segregate PMU workaraunds into x86_pmu_quirk_ops structure

RFC In review Cyrill Gorcunov 3 participants 7 messages May 29, 2010 – Jun 1, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC 1/1 perf, x86: Segregate PMU workaraunds into x86_pmu_quirk_ops structure

Hi,

I would appreciate comments/complains on the following patch. The idea is to implement PMU quirks with minimal impact. At the moment two quirks are addressed - PEBS disabling on Clovertown and P4 performance counter double write. PEBS disabling already was there only moved to x86_pmu_quirk_ops. Note that I didn't use pointer to the structure intensionally but embed it into x86_pmu, if the structure grow we will need to use a pointer to the structure.

Comments, complains please? Perhaps there is some idea which allow to handle this all more gentle?

-- Cyrill
---
perf, x86: Segregate PMU workaraunds into x86_pmu_quirk_ops structure

This allow us to handle both Clovertown and Netburst quirks in a general fashion.

---
 arch/x86/kernel/cpu/perf_event.c       |   18 ++++++++--
 arch/x86/kernel/cpu/perf_event_intel.c |   55 ++++++++++++++++++---------------
 arch/x86/kernel/cpu/perf_event_p4.c    |   21 ++++++++++++
 3 files changed, 64 insertions(+), 30 deletions(-)
arch/x86/kernel/cpu/perf_event.c +14 -4
Index: linux-2.6.git/arch/x86/kernel/cpu/perf_event.c
=====================================================================
@@ -185,6 +185,11 @@ union perf_capabilities {
 	u64	capabilities;
 };
 
+struct x86_pmu_quirk_ops {
+	void		(*pmu_init)(void);
+	void		(*perfctr_write)(unsigned long addr, u64 value);
+};
+
 /*
  * struct x86_pmu - generic x86 pmu
  */
@@ -218,7 +223,8 @@ struct x86_pmu {
 	void		(*put_event_constraints)(struct cpu_hw_events *cpuc,
 						 struct perf_event *event);
 	struct event_constraint *event_constraints;
-	void		(*quirks)(void);
+
+	struct x86_pmu_quirk_ops quirks;
 
 	int		(*cpu_prepare)(int cpu);
 	void		(*cpu_starting)(int cpu);
@@ -924,7 +930,11 @@ x86_perf_event_set_period(struct perf_ev
 	 */
 	atomic64_set(&hwc->prev_count, (u64)-left);
 
-	wrmsrl(hwc->event_base + idx,
+	if (x86_pmu.quirks.perfctr_write)
+		x86_pmu.quirks.perfctr_write(hwc->event_base + idx,
+				(u64)(-left) & x86_pmu.cntval_mask);
+	else
+		wrmsrl(hwc->event_base + idx,
 			(u64)(-left) & x86_pmu.cntval_mask);
 
 	perf_event_update_userpage(event);
@@ -1316,8 +1326,8 @@ void __init init_hw_perf_events(void)
 
 	pr_cont("%s PMU driver.\n", x86_pmu.name);
 
-	if (x86_pmu.quirks)
-		x86_pmu.quirks();
+	if (x86_pmu.quirks.pmu_init)
+		x86_pmu.quirks.pmu_init();
 
 	if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
 		WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
arch/x86/kernel/cpu/perf_event_intel.c +30 -25

67 lines not on this page · raw message

arch/x86/kernel/cpu/perf_event_p4.c +20 -1
Index: linux-2.6.git/arch/x86/kernel/cpu/perf_event_p4.c
=====================================================================
@@ -804,6 +804,24 @@ done:
 	return num ? -ENOSPC : 0;
 }
 
+/*
+ * This handles errata N15 in intel doc 249199-029,
+ * the counter may not be updated correctly on write
+ * so we repeat write operation twice to do the trick
+ * (the official workaround didn't work)
+ *
+ * the former idea is taken from OProfile code
+ */
+static void p4_perfctr_write(unsigned long addr, u64 value)
+{
+	wrmsrl(addr, value);
+	wrmsrl(addr, value);
+}
+
+static __initconst const struct x86_pmu_quirk_ops p4_pmu_quirks = {
+	.perfctr_write = p4_perfctr_write,
+};
+
 static __initconst const struct x86_pmu p4_pmu = {
 	.name			= "Netburst P4/Xeon",
 	.handle_irq		= p4_pmu_handle_irq,
@@ -850,7 +868,8 @@ static __init int p4_pmu_init(void)
 
 	pr_cont("Netburst events, ");
 
-	x86_pmu = p4_pmu;
+	x86_pmu		= p4_pmu;
+	x86_pmu.quirks	= p4_pmu_quirks;
 
 	return 0;
 }
#1 lore raw
Quoted 8 lines · Cyrill Gorcunov

Hi,

I would appreciate comments/complains on the following patch. The idea is to implement PMU quirks with minimal impact. At the moment two quirks are addressed - PEBS disabling on Clovertown and P4 performance counter double write. PEBS disabling already was there only moved to x86_pmu_quirk_ops. Note that I didn't use pointer to the structure intensionally but embed it into x86_pmu, if the structure grow we will need to use a pointer to the structure.

The quirk functions add additional code and ops structures to the already existing model specific code. This quirks would be fine if we would could merge model specific code and get unified code. But these model specific code cannot be replaced. So I rather prefer to implement cpu errata in model specific code.

arch/x86/kernel/cpu/perf_event.c union perf_capabilities { · line 189

Quoted 6 lines
arch/x86/kernel/cpu/perf_event.c +2 -0
@@ -185,6 +185,11 @@ union perf_capabilities {
 	u64	capabilities;
 };
 
+struct x86_pmu_quirk_ops {
+	void		(*pmu_init)(void);

This init quirk could be much better handled in the model specific init code (intel_pmu_init()/amd_pmu_init()). I don't see a reason for adding the quirk first and then immediately calling it. The quirk function could be called directly instead.

arch/x86/kernel/cpu/perf_event.c union perf_capabilities { · line 190

Quoted 1 line
arch/x86/kernel/cpu/perf_event.c +1 -0
+	void		(*perfctr_write)(unsigned long addr, u64 value);

This one is difficult to avoid ...

arch/x86/kernel/cpu/perf_event.c x86_perf_event_set_period(struct perf_ev · line 937

Quoted 10 lines
arch/x86/kernel/cpu/perf_event.c +5 -1
@@ -924,7 +930,11 @@ x86_perf_event_set_period(struct perf_ev
 	 */
 	atomic64_set(&hwc->prev_count, (u64)-left);
 
-	wrmsrl(hwc->event_base + idx,
+	if (x86_pmu.quirks.perfctr_write)
+		x86_pmu.quirks.perfctr_write(hwc->event_base + idx,
+				(u64)(-left) & x86_pmu.cntval_mask);
+	else
+		wrmsrl(hwc->event_base + idx,

... but it introduces another check in the fast path. There are some options to avoid this. First we could see if we rather implement this in model specific interrupt handlers (there is p4_pmu_handle_irq()). Or, we implement an optimized check for perf quirks, maybe using ALTERNATIVE or jump labels.

I think we can handle both quirks, but if we start using and extending it more, it will have a performance impact and code will also more complicated. So, I think it is rather inappropriate as a general approach.

-Robert

Quoted 3 lines
arch/x86/kernel/cpu/perf_event.c +0 -0
 			(u64)(-left) & x86_pmu.cntval_mask);
 
 	perf_event_update_userpage(event);
Signature · 3 lines
Advanced Micro Devices, Inc.
Operating System Research Center
email: [email protected]
maintainer
#2 lore raw
Quoted 6 lines · Robert Richter

The quirk functions add additional code and ops structures to the already existing model specific code. This quirks would be fine if we would could merge model specific code and get unified code. But these model specific code cannot be replaced. So I rather prefer to implement cpu errata in model specific code.

The reason the existing quirk() function is called after *_pmu_init() is because of the funny way it all writes to the console.

#3 lore raw
In reply to Robert Richter
Quoted 10 lines · Robert Richter
Quoted 8 lines · Cyrill Gorcunov

Hi,

I would appreciate comments/complains on the following patch. The idea is to implement PMU quirks with minimal impact. At the moment two quirks are addressed - PEBS disabling on Clovertown and P4 performance counter double write. PEBS disabling already was there only moved to x86_pmu_quirk_ops. Note that I didn't use pointer to the structure intensionally but embed it into x86_pmu, if the structure grow we will need to use a pointer to the structure.

Quoted 6 lines

The quirk functions add additional code and ops structures to the already existing model specific code. This quirks would be fine if we would could merge model specific code and get unified code. But these model specific code cannot be replaced. So I rather prefer to implement cpu errata in model specific code.

agreed, but this quirks ops looked as more clear solution for me, at least in a sake of initiating the 'find something better' dialogue you know :)

Quoted 4 lines
Quoted 4 lines
arch/x86/kernel/cpu/perf_event.c +0 -0
@@ -185,6 +185,11 @@ union perf_capabilities {
 	u64	capabilities;
 };
 
Quoted 8 lines

arch/x86/kernel/cpu/perf_event.c union perf_capabilities { · line 189

Quoted 2 lines
arch/x86/kernel/cpu/perf_event.c +2 -0
+struct x86_pmu_quirk_ops {
+	void		(*pmu_init)(void);

This init quirk could be much better handled in the model specific init code (intel_pmu_init()/amd_pmu_init()). I don't see a reason for adding the quirk first and then immediately calling it. The quirk function could be called directly instead.

well, at moment we have only the one caller but if the number of callers increase _better_ to have it called via function pointer since it's easier to find out the callers in further and we're allowed to use such approach since this is a not fast path code. On the other hands I rather agree with you -- it's overzealous at the moment. /me: dropping idea on *pmu_init

Quoted 4 lines

arch/x86/kernel/cpu/perf_event.c union perf_capabilities { · line 190

Quoted 1 line
arch/x86/kernel/cpu/perf_event.c +1 -0
+	void		(*perfctr_write)(unsigned long addr, u64 value);

This one is difficult to avoid ...

unfortunately

Quoted 4 lines
Quoted 4 lines
arch/x86/kernel/cpu/perf_event.c +0 -0
@@ -924,7 +930,11 @@ x86_perf_event_set_period(struct perf_ev
 	 */
 	atomic64_set(&hwc->prev_count, (u64)-left);
 
Quoted 10 lines

arch/x86/kernel/cpu/perf_event.c x86_perf_event_set_period(struct perf_ev · line 937

Quoted 6 lines
arch/x86/kernel/cpu/perf_event.c +5 -1
-	wrmsrl(hwc->event_base + idx,
+	if (x86_pmu.quirks.perfctr_write)
+		x86_pmu.quirks.perfctr_write(hwc->event_base + idx,
+				(u64)(-left) & x86_pmu.cntval_mask);
+	else
+		wrmsrl(hwc->event_base + idx,

... but it introduces another check in the fast path. There are some options to avoid this. First we could see if we rather implement this in model specific interrupt handlers (there is p4_pmu_handle_irq()).

no, we can't, I thought about that, this code is called from several places.

Quoted 3 lines

Or, we implement an optimized check for perf quirks, maybe using ALTERNATIVE or jump labels.

yes! Robert, I completely forgot about alternatives. I guess that is exactly what we need! I'll try to implement.

Quoted 7 lines

I think we can handle both quirks, but if we start using and extending it more, it will have a performance impact and code will also more complicated. So, I think it is rather inappropriate as a general approach.

-Robert

Thanks a huge for comments, Robert!

Quoted 9 lines
Quoted 3 lines
arch/x86/kernel/cpu/perf_event.c +0 -0
 			(u64)(-left) & x86_pmu.cntval_mask);
 
 	perf_event_update_userpage(event);
Signature · 3 lines
Advanced Micro Devices, Inc.
Operating System Research Center
email: [email protected]

-- Cyrill

maintainer
#4 lore raw
In reply to Cyrill Gorcunov

arch/x86/kernel/cpu/perf_event.c x86_perf_event_set_period(struct perf_ev · line 938

Quoted 11 lines · Cyrill Gorcunov
arch/x86/kernel/cpu/perf_event.c +5 -1
@@ -924,7 +930,11 @@ x86_perf_event_set_period(struct perf_ev
         */
        atomic64_set(&hwc->prev_count, (u64)-left);
 
-       wrmsrl(hwc->event_base + idx,
+       if (x86_pmu.quirks.perfctr_write)
+               x86_pmu.quirks.perfctr_write(hwc->event_base + idx,
+                               (u64)(-left) & x86_pmu.cntval_mask);
+       else
+               wrmsrl(hwc->event_base + idx,
                        (u64)(-left) & x86_pmu.cntval_mask);

This bit is rather ugly,.. not quite sure how to clean it up though. Anybody got a bright idea?

#5 lore raw
Quoted 6 lines · Peter Zijlstra
Quoted 4 lines · Cyrill Gorcunov
(quoted diff) +0 -0
@@ -924,7 +930,11 @@ x86_perf_event_set_period(struct perf_ev
         */
        atomic64_set(&hwc->prev_count, (u64)-left);
 
Quoted 11 lines

Review x86_perf_event_set_period(struct perf_ev · line 938

Quoted 7 lines
(quoted diff) +5 -1
-       wrmsrl(hwc->event_base + idx,
+       if (x86_pmu.quirks.perfctr_write)
+               x86_pmu.quirks.perfctr_write(hwc->event_base + idx,
+                               (u64)(-left) & x86_pmu.cntval_mask);
+       else
+               wrmsrl(hwc->event_base + idx,
                        (u64)(-left) & x86_pmu.cntval_mask);

This bit is rather ugly,.. not quite sure how to clean it up though. Anybody got a bright idea?

Yes, I know, only a bit lighter solution could be like in patch below, alternative instructions bring mess (and considering we may have paravirt turned on -- even more mess), jump labels... I didn't find them in tree, in which file(s) they are? I mean, are they under review now or merged in some place?

So I guess plain test may be more-less fine here, hmm?

-- Cyrill
---
perf, x86: Make a second write to performance counter when needed

On Netburst cpu we need a second write to performance counter to be sure it's updated properly.

---
 arch/x86/kernel/cpu/perf_event.c    |   10 ++++++++++
 arch/x86/kernel/cpu/perf_event_p4.c |    9 +++++++++
 2 files changed, 19 insertions(+)
arch/x86/kernel/cpu/perf_event.c +10 -0
Index: linux-2.6.git/arch/x86/kernel/cpu/perf_event.c
=====================================================================
@@ -220,6 +220,7 @@ struct x86_pmu {
 						 struct perf_event *event);
 	struct event_constraint *event_constraints;
 	void		(*quirks)(void);
+	int		perfctr_second_write;
 
 	int		(*cpu_prepare)(int cpu);
 	void		(*cpu_starting)(int cpu);
@@ -926,6 +927,15 @@ x86_perf_event_set_period(struct perf_ev
 	atomic64_set(&hwc->prev_count, (u64)-left);
 
 	wrmsrl(hwc->event_base + idx,
+		(u64)(-left) & x86_pmu.cntval_mask);
+
+	/*
+	 * Due to erratum on certan cpu we need
+	 * a second write to be sure the register
+	 * is updated properly
+	 */
+	if (x86_pmu.perfctr_second_write)
+		wrmsrl(hwc->event_base + idx,
 			(u64)(-left) & x86_pmu.cntval_mask);
 
 	perf_event_update_userpage(event);
arch/x86/kernel/cpu/perf_event_p4.c +9 -0
Index: linux-2.6.git/arch/x86/kernel/cpu/perf_event_p4.c
=====================================================================
@@ -829,6 +829,15 @@ static __initconst const struct x86_pmu 
 	.max_period		= (1ULL << 39) - 1,
 	.hw_config		= p4_hw_config,
 	.schedule_events	= p4_pmu_schedule_events,
+	/*
+	 * This handles erratum N15 in intel doc 249199-029,
+	 * the counter may not be updated correctly on write
+	 * so we need a second write operation to do the trick
+	 * (the official workaround didn't work)
+	 *
+	 * the former idea is taken from OProfile code
+	 */
+	.perfctr_second_write	= 1,
 };
 
 static __init int p4_pmu_init(void)
#6 lore raw

On Mon, May 31, 2010 at 08:45:53PM +0400, Cyrill Gorcunov wrote:
...

Quoted 7 lines

So I guess plain test may be more-less fine here, hmm?

-- Cyrill
---
perf, x86: Make a second write to performance counter when needed

Quoted 9 lines

On Netburst cpu we need a second write to performance counter to be sure it's updated properly.

---
 arch/x86/kernel/cpu/perf_event.c    |   10 ++++++++++
 arch/x86/kernel/cpu/perf_event_p4.c |    9 +++++++++
 2 files changed, 19 insertions(+)

...

Ping? Any objections/ideas on this approach?

-- Cyrill