Skip to contentSkip to conversation

x86, perf: Use a new PMU ack sequence

PATCH In review Andi Kleen 4 participants 6 messages Oct 21, 2015 – Oct 22, 2015

Subsystems (2)
lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

PATCH 1/3 x86, perf: Use a new PMU ack sequence

From: Andi Kleen <[email protected]>

The SKL PMU code had a problem with LBR freezing. When a counter overflows already in the PMI handler, the LBR would be frozen early and not be unfrozen until the next PMI. This means we would get stale LBR information.

Depending on the workload this could happen for a few percent of the PMIs for cycles in adaptive frequency mode, because the frequency algorithm regularly goes down to very low periods.

This patch implements a new PMU ack sequence that avoids this problem. The new sequence is:

- (counters are disabled with GLOBAL_CTRL)
There should be no further increments of the counters by later instructions; and thus no additional PMI (and thus no additional freezing).

- ack the APIC

Clear the APIC PMI LVT entry so that any later interrupt is delivered and is not lost due to the PMI LVT entry being masked. A lost PMI interrupt could lead to LBRs staying frozen without entering the PMI handler

- Ack the PMU counters. This unfreezes the LBRs on Skylake (but not on earlier CPUs which rely on DEBUGCTL writes for this)

- Reenable counters

The WRMSR will start the counters counting again (and will be ordered after the APIC LVT PMI entry write since WRMSR is architecturally serializing). Since the APIC PMI LVT is unmasked, any PMI which is caused by these perfmon counters will trigger an NMI (but the delivery may be delayed until after the next IRET)

One side effect is that the old retry loop is not possible anymore, as the counters stay unacked for the majority of the PMI handler, but that is not a big loss, as "profiling" the PMI was always a bit dubious. For the old ack sequence it is still supported.

The new sequence is now used unconditionally on all Intel Core/Atom CPUs. The old irq loop check is removed. Instead we rely on the generic nmi maximum duration check in the NMI code, together with the perf enforcement of the maximum sampling rate, to stop any runaway counters. We also assume that any counter can be stopped by setting a sufficiently large overflow value.

v2:
Use new ack sequence unconditionally. Remove pmu reset code.

---
 arch/x86/kernel/cpu/perf_event.h       |  1 -
 arch/x86/kernel/cpu/perf_event_intel.c | 82 +++++-----------------------------
 2 files changed, 11 insertions(+), 72 deletions(-)
arch/x86/kernel/cpu/perf_event.h +0 -1
index 499f533..6388540 100644
@@ -550,7 +550,6 @@ struct x86_pmu {
 	struct event_constraint *event_constraints;
 	struct x86_pmu_quirk *quirks;
 	int		perfctr_second_write;
-	bool		late_ack;
 	unsigned	(*limit_period)(struct perf_event *event, unsigned l);
 
 	/*
arch/x86/kernel/cpu/perf_event_intel.c +11 -71

137 lines not on this page · raw message

Signature · 1 line
2.4.3
author
#1 lore raw

PATCH 2/3 x86, perf: Factor out BTS enable/disable functions

From: Andi Kleen <[email protected]>

Move BTS enable/disable into own functions. Used by the next patch.

---
 arch/x86/kernel/cpu/perf_event_intel.c | 43 ++++++++++++++++++++--------------
 1 file changed, 26 insertions(+), 17 deletions(-)
arch/x86/kernel/cpu/perf_event_intel.c +26 -17
index 68a37b5..31323cc 100644
@@ -1457,20 +1457,39 @@ static __initconst const u64 slm_hw_cache_event_ids
  },
 };
 
-/*
- * Use from PMIs where the LBRs are already disabled.
- */
-static void __intel_pmu_disable_all(void)
+static inline void intel_pmu_maybe_disable_bts(void)
 {
 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
 
-	wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
-
 	if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask))
 		intel_pmu_disable_bts();
 	else
 		intel_bts_disable_local();
+}
+
+static inline void intel_pmu_maybe_enable_bts(void)
+{
+	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
 
+	if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask)) {
+		struct perf_event *event =
+			cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
+
+		if (WARN_ON_ONCE(!event))
+			return;
+
+		intel_pmu_enable_bts(event->hw.config);
+	} else
+		intel_bts_enable_local();
+}
+
+/*
+ * Use from PMIs where the LBRs are already disabled.
+ */
+static void __intel_pmu_disable_all(void)
+{
+	wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
+	intel_pmu_maybe_disable_bts();
 	intel_pmu_pebs_disable_all();
 }
 
@@ -1488,17 +1507,7 @@ static void __intel_pmu_enable_all(int added, bool pmi)
 	intel_pmu_lbr_enable_all(pmi);
 	wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL,
 			x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_guest_mask);
-
-	if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask)) {
-		struct perf_event *event =
-			cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
-
-		if (WARN_ON_ONCE(!event))
-			return;
-
-		intel_pmu_enable_bts(event->hw.config);
-	} else
-		intel_bts_enable_local();
+	intel_pmu_maybe_enable_bts();
 }
 
 static void intel_pmu_enable_all(int added)
Signature · 1 line
2.4.3
author
#2 lore raw

PATCH 3/3 perf, x86: Use counter freezing with Arch Perfmon v4

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

Now that we have reliable LBR unfreezing we can also use real counter freezing.

Arch Perfmon v4 has an improved counter freezing implementation.

With counter freezing the PMU automatically "freezes" all counters on a counter overflow, so that the PMI handler does not need to explicitly disable the counter.

With arch perfmon 4 the freeze bits are explicit bits in GLOBAL_STATUS now, which avoids a range of races in the previous implementation, and also allows avoiding an extra write to GLOBAL_CTRL.

Advantages of counter freezes:
- It avoids a couple of costly extra MSR writes in the PMI handler
- It makes the PMI handler more accurate, as all counters get frozen atomically as soon as any counter overflows. So there is much less counting of the PMI handler itself.

With the freezing we don't need to disable or enable counters or PEBS. Only BTS which does not support auto-freezing still needs to be explicitly disabled.

The "status counter ack" serves as the reenable action, by clearing the freeze bits in the status register.

So previously for a PEBS counter the PMI would do (each line a MSR access)

disable global ctrl
disable pebs
read status
ack status
read status again
reenable global ctrl
reenable pebs

(5x WRMSR, 2x RDMSR)

With the new counter freezing support this is simplified to:

read status
ack

(1x WRMSR, 1x RDMSR)

One issue is that the hardware doesn't like changing the period in freeze mode. To avoid any issues here we only use counter freezing when all counters are not in frequency mode, but have a fixed period. This is kept track of with new per CPU state.

In frequency mode the old mode is still used.

Performance:

When profiling a kernel build on Skylake with different perf options, measuring the length of all NMI handlers using the nmi handler trace point:

(lower is better)

perf options    `           avg     max   delta
-                           962   37248
-c 100000                   445   31217   -53%  with counter freezing
-g                         1753   47312
-g -c 100000                966   33698   -44%  with counter freezing
--call-graph lbr           3770   37164
--call-graph lbr -c 100000 2433   36930   -35%  with counter freezing
--c.g. dwarf               2055   33598
--c.g. dwarf -c 100000     1478   30491   -28%  with counter freezing

So the average cost of a NMI handler is cut down significantly with freezing.

At least on this workload this makes -g competitive with the previous non -g.

The max cost isn't really improved, since that is dominated by other overhead (such as frequency changes)

---
 arch/x86/include/asm/msr-index.h       |  1 +
 arch/x86/kernel/cpu/perf_event.h       |  5 +++
 arch/x86/kernel/cpu/perf_event_intel.c | 76 +++++++++++++++++++++++++++++++++-
 3 files changed, 80 insertions(+), 2 deletions(-)
arch/x86/include/asm/msr-index.h +1 -0
index b8c14bb..3dc12f9 100644
@@ -140,6 +140,7 @@
 #define DEBUGCTLMSR_BTS_OFF_OS		(1UL <<  9)
 #define DEBUGCTLMSR_BTS_OFF_USR		(1UL << 10)
 #define DEBUGCTLMSR_FREEZE_LBRS_ON_PMI	(1UL << 11)
+#define DEBUGCTLMSR_FREEZE_PERFMON_ON_PMI (1UL << 12)
 
 #define MSR_PEBS_FRONTEND		0x000003f7
 
arch/x86/kernel/cpu/perf_event.h +5 -0
index 6388540..d871c94 100644
@@ -240,6 +240,11 @@ struct cpu_hw_events {
 	int excl_thread_id; /* 0 or 1 */
 
 	/*
+	 * Counter freezing state.
+	 */
+	int				frozen_enabled;
+
+	/*
 	 * AMD specific bits
 	 */
 	struct amd_nb			*amd_nb;
arch/x86/kernel/cpu/perf_event_intel.c +74 -2

121 lines not on this page · raw message

Signature · 1 line
2.4.3
maintainer
#3 lore raw
In reply to Andi Kleen
Quoted 47 lines · Andi Kleen

From: Andi Kleen <[email protected]>

The SKL PMU code had a problem with LBR freezing. When a counter overflows already in the PMI handler, the LBR would be frozen early and not be unfrozen until the next PMI. This means we would get stale LBR information.

Depending on the workload this could happen for a few percent of the PMIs for cycles in adaptive frequency mode, because the frequency algorithm regularly goes down to very low periods.

This patch implements a new PMU ack sequence that avoids this problem. The new sequence is:

- (counters are disabled with GLOBAL_CTRL)
There should be no further increments of the counters by later instructions; and thus no additional PMI (and thus no additional freezing).

- ack the APIC

Clear the APIC PMI LVT entry so that any later interrupt is delivered and is not lost due to the PMI LVT entry being masked. A lost PMI interrupt could lead to LBRs staying frozen without entering the PMI handler

- Ack the PMU counters. This unfreezes the LBRs on Skylake (but not on earlier CPUs which rely on DEBUGCTL writes for this)

- Reenable counters

The WRMSR will start the counters counting again (and will be ordered after the APIC LVT PMI entry write since WRMSR is architecturally serializing). Since the APIC PMI LVT is unmasked, any PMI which is caused by these perfmon counters will trigger an NMI (but the delivery may be delayed until after the next IRET)

One side effect is that the old retry loop is not possible anymore, as the counters stay unacked for the majority of the PMI handler, but that is not a big loss, as "profiling" the PMI was always a bit dubious. For the old ack sequence it is still supported.

The new sequence is now used unconditionally on all Intel Core/Atom CPUs. The old irq loop check is removed. Instead we rely on the generic nmi maximum duration check in the NMI code, together with the perf enforcement of the maximum sampling rate, to stop any runaway counters. We also assume that any counter can be stopped by setting a sufficiently large overflow value.

Quoted 2 lines

v2:
Use new ack sequence unconditionally. Remove pmu reset code.

So this is not something we can easily revert if things go bad. Esp. since you build on it with the next patches.

Also, teach your editor to wrap at 72 chars for Changelogs.

#4 lore raw
Quoted 5 lines
Quoted 2 lines

v2:
Use new ack sequence unconditionally. Remove pmu reset code.

So this is not something we can easily revert if things go bad. Esp. since you build on it with the next patches.

Ok, and?

You want me to go back to the previous patch? That one is easily undoable (just disable the flag for the model)

Another alternative would be to fork the PMI handler into a new and an old version, that is switchable.

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only
#5 lore raw
Quoted 7 lines · Andi Kleen
Quoted 5 lines
Quoted 2 lines

v2:
Use new ack sequence unconditionally. Remove pmu reset code.

So this is not something we can easily revert if things go bad. Esp. since you build on it with the next patches.

Ok, and?

Sigh, you are being disruptive again.

Quoted 5 lines

You want me to go back to the previous patch? That one is easily undoable (just disable the flag for the model)

Another alternative would be to fork the PMI handler into a new and an old version, that is switchable.

Here you pretend that you didn't read the sane solution that was suggested to you just three days ago:

http://lkml.kernel.org/r/[email protected]

" > > Ingo, do you want to first merge the safe patch and then clean up?

Quoted 2 lines

Yeah, would be nice to structure it that way, out of general paranoia.

"

I.e. first apply the safe approach, then, after the dependent changes, clean it up by introducing the dangerous change.

The thing is, I'm close to summarily NAK-ing any patches from you to the perf subsystem, due to the unacceptably low quality patches combined with obtuse passive-aggressive obstruction you are routinely burdening maintainers with.

Btw., I noticed that you routinely don't Cc me to perf patches. Please always Cc:
me to perf patches (both kernel and tooling patches). I still will not apply them directly, only if another perf maintainer signs off on them, but I'd like to have a record of all your submissions.

Thanks,

Ingo