Skip to contentSkip to conversation

perf: Rework the PMU methods

tip:perf/core In review tip-bot for Peter Zijlstra 3 participants 11 messages Sep 9, 2010 – Sep 15, 2010

Subsystems (10)
Arm (32-bit) x86 PowerPC perf events +6 more subsystems
lore
Share & tools
lore
#0 lore raw
Commit-ID:  a4eaf7f14675cb512d69f0c928055e73d0c6d252
Gitweb:     http://git.kernel.org/tip/a4eaf7f14675cb512d69f0c928055e73d0c6d252
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Wed, 16 Jun 2010 14:37:10 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 9 Sep 2010 20:46:30 +0200

perf: Rework the PMU methods

Replace pmu::{enable,disable,start,stop,unthrottle} with pmu::{add,del,start,stop}, all of which take a flags argument.

The new interface extends the capability to stop a counter while keeping it scheduled on the PMU. We replace the throttled state with the generic stopped state.

This also allows us to efficiently stop/start counters over certain code paths (like IRQ handlers).

It also allows scheduling a counter without it starting, allowing for a generic frozen state (useful for rotating stopped counters).

The stopped state is implemented in two different ways, depending on how the architecture implemented the throttled state:

1) We disable the counter:
a) the pmu has per-counter enable bits, we flip that
b) we program a NOP event, preserving the counter state

2) We store the counter state and ignore all read/overflow events

Cc paulus <[email protected]>
Cc stephane eranian <[email protected]>
Cc Robert Richter <[email protected]>
Cc Will Deacon <[email protected]>
Cc Paul Mundt <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Lin Ming <[email protected]>
Cc Yanmin <[email protected]>
Cc Deng-Cheng Zhu <[email protected]>
Cc David Miller <[email protected]>
Cc Michael Cree <[email protected]>

LKML-Reference: <new-submission>

---
 arch/alpha/kernel/perf_event.c            |   71 +++++++++++----
 arch/arm/kernel/perf_event.c              |   96 +++++++++++++-------
 arch/powerpc/kernel/perf_event.c          |  105 ++++++++++++++--------
 arch/powerpc/kernel/perf_event_fsl_emb.c  |  107 ++++++++++++++---------
 arch/sh/kernel/perf_event.c               |   75 +++++++++++-----
 arch/sparc/kernel/perf_event.c            |  109 ++++++++++++++---------
 arch/x86/kernel/cpu/perf_event.c          |  106 +++++++++++++---------
 arch/x86/kernel/cpu/perf_event_intel.c    |    2 +-
 arch/x86/kernel/cpu/perf_event_intel_ds.c |    2 +-
 include/linux/ftrace_event.h              |    4 +-
 include/linux/perf_event.h                |   54 +++++++++---
 kernel/hw_breakpoint.c                    |   29 ++++++-
 kernel/perf_event.c                       |  140 +++++++++++++++--------------
 kernel/trace/trace_event_perf.c           |    7 +-
 14 files changed, 576 insertions(+), 331 deletions(-)
arch/alpha/kernel/perf_event.c +53 -18
index 3e26073..380ef02 100644
@@ -307,7 +307,7 @@ again:
 			     new_raw_count) != prev_raw_count)
 		goto again;
 
-	delta = (new_raw_count  - (prev_raw_count & alpha_pmu->pmc_count_mask[idx])) + ovf;
+	delta = (new_raw_count - (prev_raw_count & alpha_pmu->pmc_count_mask[idx])) + ovf;
 
 	/* It is possible on very rare occasions that the PMC has overflowed
 	 * but the interrupt is yet to come.  Detect and fix this situation.
@@ -402,14 +402,13 @@ static void maybe_change_configuration(struct cpu_hw_events *cpuc)
 		struct hw_perf_event *hwc = &pe->hw;
 		int idx = hwc->idx;
 
-		if (cpuc->current_idx[j] != PMC_NO_INDEX) {
-			cpuc->idx_mask |= (1<<cpuc->current_idx[j]);
-			continue;
+		if (cpuc->current_idx[j] == PMC_NO_INDEX) {
+			alpha_perf_event_set_period(pe, hwc, idx);
+			cpuc->current_idx[j] = idx;
 		}
 
-		alpha_perf_event_set_period(pe, hwc, idx);
-		cpuc->current_idx[j] = idx;
-		cpuc->idx_mask |= (1<<cpuc->current_idx[j]);
+		if (!(hwc->state & PERF_HES_STOPPED))
+			cpuc->idx_mask |= (1<<cpuc->current_idx[j]);
 	}
 	cpuc->config = cpuc->event[0]->hw.config_base;
 }
@@ -420,7 +419,7 @@ static void maybe_change_configuration(struct cpu_hw_events *cpuc)
  *  - this function is called from outside this module via the pmu struct
  *    returned from perf event initialisation.
  */
-static int alpha_pmu_enable(struct perf_event *event)
+static int alpha_pmu_add(struct perf_event *event, int flags)
 {
 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
 	int n0;
@@ -455,6 +454,10 @@ static int alpha_pmu_enable(struct perf_event *event)
 		}
 	}
 
+	hwc->state = PERF_HES_UPTODATE;
+	if (!(flags & PERF_EF_START))
+		hwc->state |= PERF_HES_STOPPED;
+
 	local_irq_restore(flags);
 	perf_pmu_enable(event->pmu);
 
@@ -467,7 +470,7 @@ static int alpha_pmu_enable(struct perf_event *event)
  *  - this function is called from outside this module via the pmu struct
  *    returned from perf event initialisation.
  */
-static void alpha_pmu_disable(struct perf_event *event)
+static void alpha_pmu_del(struct perf_event *event, int flags)
 {
 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
 	struct hw_perf_event *hwc = &event->hw;
@@ -514,13 +517,44 @@ static void alpha_pmu_read(struct perf_event *event)
 }
 
 
-static void alpha_pmu_unthrottle(struct perf_event *event)
+static void alpha_pmu_stop(struct perf_event *event, int flags)
 {
 	struct hw_perf_event *hwc = &event->hw;
 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
 
+	if (!(hwc->state & PERF_HES_STOPPED)) {
+		cpuc->idx_mask &= !(1UL<<hwc->idx);
+		hwc->state |= PERF_HES_STOPPED;
+	}
+
+	if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
+		alpha_perf_event_update(event, hwc, hwc->idx, 0);
+		hwc->state |= PERF_HES_UPTODATE;
+	}
+
+	if (cpuc->enabled)
+		wrperfmon(PERFMON_CMD_ENABLE, (1UL<<hwc->idx));
+}
+
+
+static void alpha_pmu_start(struct perf_event *event, int flags)
+{
+	struct hw_perf_event *hwc = &event->hw;
+	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
+
+	if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
+		return;
+
+	if (flags & PERF_EF_RELOAD) {
+		WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
+		alpha_perf_event_set_period(event, hwc, hwc->idx);
+	}
+
+	hwc->state = 0;
+
 	cpuc->idx_mask |= 1UL<<hwc->idx;
-	wrperfmon(PERFMON_CMD_ENABLE, (1UL<<hwc->idx));
+	if (cpuc->enabled)
+		wrperfmon(PERFMON_CMD_ENABLE, (1UL<<hwc->idx));
 }
 
 
@@ -671,7 +705,7 @@ static int alpha_pmu_event_init(struct perf_event *event)
 /*
  * Main entry point - enable HW performance counters.
  */
-static void alpha_pmu_pmu_enable(struct pmu *pmu)
+static void alpha_pmu_enable(struct pmu *pmu)
 {
 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
 
@@ -697,7 +731,7 @@ static void alpha_pmu_pmu_enable(struct pmu *pmu)
  * Main entry point - disable HW performance counters.
  */
 
-static void alpha_pmu_pmu_disable(struct pmu *pmu)
+static void alpha_pmu_disable(struct pmu *pmu)
 {
 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
 
@@ -711,13 +745,14 @@ static void alpha_pmu_pmu_disable(struct pmu *pmu)
 }
 
 static struct pmu pmu = {
-	.pmu_enable	= alpha_pmu_pmu_enable,
-	.pmu_disable	= alpha_pmu_pmu_disable,
+	.pmu_enable	= alpha_pmu_enable,
+	.pmu_disable	= alpha_pmu_disable,
 	.event_init	= alpha_pmu_event_init,
-	.enable		= alpha_pmu_enable,
-	.disable	= alpha_pmu_disable,
+	.add		= alpha_pmu_add,
+	.del		= alpha_pmu_del,
+	.start		= alpha_pmu_start,
+	.stop		= alpha_pmu_stop,
 	.read		= alpha_pmu_read,
-	.unthrottle	= alpha_pmu_unthrottle,
 };
 
 
arch/arm/kernel/perf_event.c +61 -35

153 lines not on this page · raw message

arch/powerpc/kernel/perf_event.c +68 -37

189 lines not on this page · raw message

arch/powerpc/kernel/perf_event_fsl_emb.c +66 -41

184 lines not on this page · raw message

arch/sh/kernel/perf_event.c +51 -24

138 lines not on this page · raw message

arch/sparc/kernel/perf_event.c +67 -42

184 lines not on this page · raw message

arch/x86/kernel/cpu/perf_event.c +61 -45

200 lines not on this page · raw message

arch/x86/kernel/cpu/perf_event_intel.c +1 -1
index ee05c90..82395f2 100644
@@ -763,7 +763,7 @@ again:
 		data.period = event->hw.last_period;
 
 		if (perf_event_overflow(event, 1, &data, regs))
-			x86_pmu_stop(event);
+			x86_pmu_stop(event, 0);
 	}
 
 	/*
arch/x86/kernel/cpu/perf_event_intel_ds.c +1 -1
index 18018d1..9893a2f 100644
@@ -491,7 +491,7 @@ static void __intel_pmu_pebs_event(struct perf_event *event,
 		regs.flags &= ~PERF_EFLAGS_EXACT;
 
 	if (perf_event_overflow(event, 1, &data, &regs))
-		x86_pmu_stop(event);
+		x86_pmu_stop(event, 0);
 }
 
 static void intel_pmu_drain_pebs_core(struct pt_regs *iregs)
include/linux/ftrace_event.h +2 -2
index 5f8ad7b..8beabb9 100644
@@ -252,8 +252,8 @@ DECLARE_PER_CPU(struct pt_regs, perf_trace_regs);
 
 extern int  perf_trace_init(struct perf_event *event);
 extern void perf_trace_destroy(struct perf_event *event);
-extern int  perf_trace_enable(struct perf_event *event);
-extern void perf_trace_disable(struct perf_event *event);
+extern int  perf_trace_add(struct perf_event *event, int flags);
+extern void perf_trace_del(struct perf_event *event, int flags);
 extern int  ftrace_profile_set_filter(struct perf_event *event, int event_id,
 				     char *filter_str);
 extern void ftrace_profile_free_filter(struct perf_event *event);
include/linux/perf_event.h +41 -13

102 lines not on this page · raw message

kernel/hw_breakpoint.c +27 -2
index e9c5cfa..6f15009 100644
@@ -586,10 +586,35 @@ static int hw_breakpoint_event_init(struct perf_event *bp)
 	return 0;
 }
 
+static int hw_breakpoint_add(struct perf_event *bp, int flags)
+{
+	if (!(flags & PERF_EF_START))
+		bp->hw.state = PERF_HES_STOPPED;
+
+	return arch_install_hw_breakpoint(bp);
+}
+
+static void hw_breakpoint_del(struct perf_event *bp, int flags)
+{
+	arch_uninstall_hw_breakpoint(bp);
+}
+
+static void hw_breakpoint_start(struct perf_event *bp, int flags)
+{
+	bp->hw.state = 0;
+}
+
+static void hw_breakpoint_stop(struct perf_event *bp, int flags)
+{
+	bp->hw.state = PERF_HES_STOPPED;
+}
+
 static struct pmu perf_breakpoint = {
 	.event_init	= hw_breakpoint_event_init,
-	.enable		= arch_install_hw_breakpoint,
-	.disable	= arch_uninstall_hw_breakpoint,
+	.add		= hw_breakpoint_add,
+	.del		= hw_breakpoint_del,
+	.start		= hw_breakpoint_start,
+	.stop		= hw_breakpoint_stop,
 	.read		= hw_breakpoint_pmu_read,
 };
 
kernel/perf_event.c +72 -68

301 lines not on this page · raw message

kernel/trace/trace_event_perf.c +5 -2
index f3bbcd1..39c059c 100644
@@ -101,7 +101,7 @@ int perf_trace_init(struct perf_event *p_event)
 	return ret;
 }
 
-int perf_trace_enable(struct perf_event *p_event)
+int perf_trace_add(struct perf_event *p_event, int flags)
 {
 	struct ftrace_event_call *tp_event = p_event->tp_event;
 	struct hlist_head __percpu *pcpu_list;
@@ -111,13 +111,16 @@ int perf_trace_enable(struct perf_event *p_event)
 	if (WARN_ON_ONCE(!pcpu_list))
 		return -EINVAL;
 
+	if (!(flags & PERF_EF_START))
+		p_event->hw.state = PERF_HES_STOPPED;
+
 	list = this_cpu_ptr(pcpu_list);
 	hlist_add_head_rcu(&p_event->hlist_entry, list);
 
 	return 0;
 }
 
-void perf_trace_disable(struct perf_event *p_event)
+void perf_trace_del(struct perf_event *p_event, int flags)
 {
 	hlist_del_rcu(&p_event->hlist_entry);
 }
#1 lore raw
Quoted 11 lines · tip-bot for Peter Zijlstra
Commit-ID:  a4eaf7f14675cb512d69f0c928055e73d0c6d252
Gitweb:     http://git.kernel.org/tip/a4eaf7f14675cb512d69f0c928055e73d0c6d252
Author:     Peter Zijlstra<[email protected]>
AuthorDate: Wed, 16 Jun 2010 14:37:10 +0200
Committer:  Ingo Molnar<[email protected]>
CommitDate: Thu, 9 Sep 2010 20:46:30 +0200

perf: Rework the PMU methods

Replace pmu::{enable,disable,start,stop,unthrottle} with pmu::{add,del,start,stop}, all of which take a flags argument.

Regarding the new function alpha_pmu_stop() in arch/alpha/kernel/perf_event.c:

arch/alpha/kernel/perf_event.c static void alpha_pmu_read(struct perf_event *event)

Quoted 8 lines
arch/alpha/kernel/perf_event.c +3 -1
-static void alpha_pmu_unthrottle(struct perf_event *event)
+static void alpha_pmu_stop(struct perf_event *event, int flags)
  {
  	struct hw_perf_event *hwc =&event->hw;
  	struct cpu_hw_events *cpuc =&__get_cpu_var(cpu_hw_events);
 
+	if (!(hwc->state&  PERF_HES_STOPPED)) {
+		cpuc->idx_mask&= !(1UL<<hwc->idx);
                                 ^

Presumably ones complement (rather than logical not) is meant.

arch/alpha/kernel/perf_event.c static void alpha_pmu_read(struct perf_event *event) · line 536

Quoted 10 lines
arch/alpha/kernel/perf_event.c +10 -0
+		hwc->state |= PERF_HES_STOPPED;
+	}
+
+	if ((flags&  PERF_EF_UPDATE)&&  !(hwc->state&  PERF_HES_UPTODATE)) {
+		alpha_perf_event_update(event, hwc, hwc->idx, 0);
+		hwc->state |= PERF_HES_UPTODATE;
+	}
+
+	if (cpuc->enabled)
+		wrperfmon(PERFMON_CMD_ENABLE, (1UL<<hwc->idx));

By the name of the function (alpha_pmu_stop) I assume that the intent is to stop the specific PMC here. The above fails to do that. When wrperfmon() is used with PERFMON_CMD_ENABLE it enables the PMCs with set bits in the second argument. It does not stop the others. To do that wrperfmon() must be called with PERFMON_CMD_DISABLE and the corresponding PMC bits set to disable the PMC.

Cheers
Michael.

maintainer
#2 lore raw
Quoted 22 lines · Michael Cree
Quoted 11 lines · tip-bot for Peter Zijlstra
Commit-ID:  a4eaf7f14675cb512d69f0c928055e73d0c6d252
Gitweb:     http://git.kernel.org/tip/a4eaf7f14675cb512d69f0c928055e73d0c6d252
Author:     Peter Zijlstra<[email protected]>
AuthorDate: Wed, 16 Jun 2010 14:37:10 +0200
Committer:  Ingo Molnar<[email protected]>
CommitDate: Thu, 9 Sep 2010 20:46:30 +0200

perf: Rework the PMU methods

Replace pmu::{enable,disable,start,stop,unthrottle} with pmu::{add,del,start,stop}, all of which take a flags argument.

Regarding the new function alpha_pmu_stop() in
arch/alpha/kernel/perf_event.c:

Quoted 6 lines
arch/alpha/kernel/perf_event.c +1 -1
-static void alpha_pmu_unthrottle(struct perf_event *event)
+static void alpha_pmu_stop(struct perf_event *event, int flags)
  {
  	struct hw_perf_event *hwc =&event->hw;
  	struct cpu_hw_events *cpuc =&__get_cpu_var(cpu_hw_events);
Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (!(hwc->state&  PERF_HES_STOPPED)) {
+		cpuc->idx_mask&= !(1UL<<hwc->idx);
                                  ^

Presumably ones complement (rather than logical not) is meant.

Yes, typo that, sorry.

Quoted 10 lines
Quoted 10 lines
arch/alpha/kernel/perf_event.c +9 -0
+		hwc->state |= PERF_HES_STOPPED;
+	}
+
+	if ((flags&  PERF_EF_UPDATE)&&  !(hwc->state&  PERF_HES_UPTODATE)) {
+		alpha_perf_event_update(event, hwc, hwc->idx, 0);
+		hwc->state |= PERF_HES_UPTODATE;
+	}
+
+	if (cpuc->enabled)
Quoted 8 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+		wrperfmon(PERFMON_CMD_ENABLE, (1UL<<hwc->idx));

By the name of the function (alpha_pmu_stop) I assume that the intent is to stop the specific PMC here. The above fails to do that. When wrperfmon() is used with PERFMON_CMD_ENABLE it enables the PMCs with set bits in the second argument. It does not stop the others. To do that wrperfmon() must be called with PERFMON_CMD_DISABLE and the corresponding PMC bits set to disable the PMC.

Right, so ->add()/->del() schedule the event onto the pmu and deal with any resource issues where needed. ->stop()/->start() simply leave the event on the pmu with all resources in tact, but ensure it doesn't actually count.

Depending on the PMU there's various ways of achieving that, some PMUs can't disable counter, for those we simply take a counter reading and disable the interrupt, and reset the counter to the previous value on ->start again and enable the interrupt.

Or when we can't even disable the interrupt, we program it to the longest possible period, etc..

Apparently ALPHA can nicely disable things, except I seem to have misunderstood the way how, I assumed it had an enable register, and writing a 0 to the idx position would stop it.

Could you provide a patch that makes ALPHA work again, or would you like me to take another stab at it?

#3 lore raw
Quoted 14 lines · Peter Zijlstra
Quoted 13 lines · Michael Cree
Quoted 11 lines · tip-bot for Peter Zijlstra
Commit-ID:  a4eaf7f14675cb512d69f0c928055e73d0c6d252
Gitweb:     http://git.kernel.org/tip/a4eaf7f14675cb512d69f0c928055e73d0c6d252
Author:     Peter Zijlstra<[email protected]>
AuthorDate: Wed, 16 Jun 2010 14:37:10 +0200
Committer:  Ingo Molnar<[email protected]>
CommitDate: Thu, 9 Sep 2010 20:46:30 +0200

perf: Rework the PMU methods

Replace pmu::{enable,disable,start,stop,unthrottle} with pmu::{add,del,start,stop}, all of which take a flags argument.

Quoted 6 lines
Quoted 3 lines

Regarding the new function alpha_pmu_stop() in
arch/alpha/kernel/perf_event.c:

Could you provide a patch that makes ALPHA work again, or would you like me to take another stab at it?

Yes, done. I also took the liberty to fix an undefined variable and multiple defined variable errors that were exposed by compilation. Will reply to this with the patch.

I've also tested it on a UP alpha. It worked well for a little while but after running 'perf top' for a number of seconds I got the following warning:

[  287.952977] WARNING: at arch/alpha/kernel/perf_event.c:546 alpha_pmu_start+0xd0/0x120()
[  287.952977] Modules linked in: radeon ttm drm_kms_helper hwmon cfbcopyarea cfbimgblt cfbfillrect parport_pc parport nfsd exportfs nfs lockd sunrpc ipv6 loop snd_hda_codec_atihdmi snd_hda_intel snd_hda_codec snd_pcm snd_seq snd_timer snd_seq_device ftdi_sio sg usbserial sr_mod snd cdrom r8169 ohci1394 soundcore ohci_hcd ehci_hcd ieee1394 mii snd_page_alloc ata_generic pcspkr tulip evdev usbcore pata_cypress
[  287.952977] fffffc000078fb80 fffffc00007961c8 fffffc000031dbb0 fffffc002780f400
[  287.952977]        0000000000000000 0000000000000400 00000000000ee6b3 0000000000000000
[  287.952977]        fffffc000036cf28 fffffc002780f400 fffffc003f006240 fffffc003f343538
[  287.952977]        fffffc000036d124 0000000000000001 fffffc003f006200 0000000000000000
[  287.952977]        fffffc003f0062c8 0000000000000000 fffffc002780f410 fffffc003f0062c8
[  287.952977]        fffffc000034d118 fffffc003f0062c8 fffffc0000795b28 fffffc0000795b40
[  287.952977] Trace:
[  287.952977] [<fffffc000031dbb0>] alpha_pmu_start+0xd0/0x120
[  287.952977] [<fffffc000036cf28>] perf_ctx_adjust_freq+0x138/0x150
[  287.952977] [<fffffc000036d124>] perf_event_context_tick+0x1e4/0x210
[  287.952977] [<fffffc000034d118>] hrtimer_run_queues+0x1b8/0x2f0
[  287.952977] [<fffffc0000338a48>] run_local_timers+0x18/0x40
[  287.952977] [<fffffc0000338aac>] update_process_times+0x3c/0xb0
[  287.952977] [<fffffc000031895c>] timer_interrupt+0x9c/0x100
[  287.952977] [<fffffc0000361450>] handle_IRQ_event+0x70/0x1a0
[  287.952977] [<fffffc0000361648>] __do_IRQ+0xc8/0x160
[  287.952977] [<fffffc0000315374>] handle_irq+0x54/0xb0
[  287.952977] [<fffffc0000315b04>] do_entInt+0xc4/0x1e0
[  287.952977] [<fffffc0000310c40>] ret_from_sys_call+0x0/0x10
[  287.952977] [<fffffc00003ab414>] file_free_rcu+0x54/0x70
[  287.952977] [<fffffc00003bd0ec>] estimate_accuracy+0xdc/0x120
[  287.952977] [<fffffc0000313108>] cpu_idle+0x48/0x60
[  287.952977] [<fffffc0000367e90>] perf_event_task_sched_in+0x0/0x60
[  287.952977] [<fffffc00003130f0>] cpu_idle+0x30/0x60
[  287.952977] [<fffffc00006338d0>] rest_init+0xb0/0xd0
[  287.952977] [<fffffc000031001c>] _stext+0x1c/0x20
[  287.952977]
[  287.952977] ---[ end trace 6f10adce9e4129fa ]---

which is from the line in alpha_pmu_start() that checks that PERF_HES_STOPPED is set.

I see that the backtrace is from the Alpha timer_interrupt() code which goes something like this:

[do some stuff updating timer deltas then...]

#ifndef CONFIG_SMP
        while (nticks--)
                update_process_times(user_mode(get_irq_regs()));

#endif

        if (test_perf_event_pending()) {
                clear_perf_event_pending();
                perf_event_do_pending();
        }

        return IRQ_HANDLED;
}

When I added the code for handle pending events to the timer interrupt I hadn't realised that update_process_times() could call back into the perf code. I'm speculating here, but could it be that there is pending work to stop the HW counter, but the call to re-start it is beating the call to stop it?

Cheers
Michael.

#4 lore raw

PATCH 1/1 alpha: Fix HW performance counters to be stopped properly.

Also fix a few compile errors due to undefined and duplicated variables.

---
 arch/alpha/kernel/perf_event.c |   17 +++++++++--------
 1 files changed, 9 insertions(+), 8 deletions(-)
arch/alpha/kernel/perf_event.c +9 -8
index a25fe9e..41f204f 100644
@@ -422,9 +422,10 @@ static void maybe_change_configuration(struct cpu_hw_events *cpuc)
 static int alpha_pmu_add(struct perf_event *event, int flags)
 {
 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
+	struct hw_perf_event *hwc = &event->hw;
 	int n0;
 	int ret;
-	unsigned long flags;
+	unsigned long irq_flags;
 
 	/*
 	 * The Sparc code has the IRQ disable first followed by the perf
@@ -435,7 +436,7 @@ static int alpha_pmu_add(struct perf_event *event, int flags)
 	 * final PMI to occur before we disable interrupts.
 	 */
 	perf_pmu_disable(event->pmu);
-	local_irq_save(flags);
+	local_irq_save(irq_flags);
 
 	/* Default to error to be returned */
 	ret = -EAGAIN;
@@ -458,7 +459,7 @@ static int alpha_pmu_add(struct perf_event *event, int flags)
 	if (!(flags & PERF_EF_START))
 		hwc->state |= PERF_HES_STOPPED;
 
-	local_irq_restore(flags);
+	local_irq_restore(irq_flags);
 	perf_pmu_enable(event->pmu);
 
 	return ret;
@@ -474,11 +475,11 @@ static void alpha_pmu_del(struct perf_event *event, int flags)
 {
 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
 	struct hw_perf_event *hwc = &event->hw;
-	unsigned long flags;
+	unsigned long irq_flags;
 	int j;
 
 	perf_pmu_disable(event->pmu);
-	local_irq_save(flags);
+	local_irq_save(irq_flags);
 
 	for (j = 0; j < cpuc->n_events; j++) {
 		if (event == cpuc->event[j]) {
@@ -504,7 +505,7 @@ static void alpha_pmu_del(struct perf_event *event, int flags)
 		}
 	}
 
-	local_irq_restore(flags);
+	local_irq_restore(irq_flags);
 	perf_pmu_enable(event->pmu);
 }
 
@@ -523,7 +524,7 @@ static void alpha_pmu_stop(struct perf_event *event, int flags)
 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
 
 	if (!(hwc->state & PERF_HES_STOPPED)) {
-		cpuc->idx_mask &= !(1UL<<hwc->idx);
+		cpuc->idx_mask &= ~(1UL<<hwc->idx);
 		hwc->state |= PERF_HES_STOPPED;
 	}
 
@@ -533,7 +534,7 @@ static void alpha_pmu_stop(struct perf_event *event, int flags)
 	}
 
 	if (cpuc->enabled)
-		wrperfmon(PERFMON_CMD_ENABLE, (1UL<<hwc->idx));
+		wrperfmon(PERFMON_CMD_DISABLE, (1UL<<hwc->idx));
 }
 
 
Signature · 1 line
1.7.1
maintainer
#6 lore raw
In reply to Michael Cree
Quoted 3 lines · Michael Cree

Yes, done. I also took the liberty to fix an undefined variable and multiple defined variable errors that were exposed by compilation. Will reply to this with the patch.

Thanks, and sorry for messing up Alpha that bad.. I have an alpha compiler and I really through I compile tested it :/

Quoted 3 lines

I've also tested it on a UP alpha. It worked well for a little while but after running 'perf top' for a number of seconds I got the following warning:

<snip warn>

Quoted 22 lines

which is from the line in alpha_pmu_start() that checks that PERF_HES_STOPPED is set.

I see that the backtrace is from the Alpha timer_interrupt() code which goes something like this:

[do some stuff updating timer deltas then...]

#ifndef CONFIG_SMP
         while (nticks--)
                 update_process_times(user_mode(get_irq_regs()));

#endif

         if (test_perf_event_pending()) {
                 clear_perf_event_pending();
                 perf_event_do_pending();
         }

         return IRQ_HANDLED;
}
Quoted 5 lines

When I added the code for handle pending events to the timer interrupt I hadn't realised that update_process_times() could call back into the perf code. I'm speculating here, but could it be that there is pending work to stop the HW counter, but the call to re-start it is beating the call to stop it?

Right, so the ->start() call came from perf_ctx_adjust_freq(), which depending on whether perf_adjust_period() gets inlined, can have two such calls.

Assuming it didn't inline (there's two callsites, which should defeat the inline static functions with a single callsite heuristic), you hit the unthrottle() call.

Ahh, the alpha throttle call should be using the fancy new stop function too (will fold into your earlier patch if it indeed works):

As to the point you raised above, yes, I think it would be prudent to call perf_event_do_pending() before update_process_times().

---

arch/alpha/kernel/perf_event.c +3 -3
Index: linux-2.6/arch/alpha/kernel/perf_event.c
===================================================================
@@ -825,14 +825,14 @@ static void alpha_perf_event_irq_handler
 			break;
 	}
 
+	event = cpuc->event[j];
+
 	if (unlikely(j == cpuc->n_events)) {
 		/* This can occur if the event is disabled right on a PMC overflow. */
-		wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
+		alpha_pmu_stop(event, 0);
 		return;
 	}
 
-	event = cpuc->event[j];
-
 	if (unlikely(!event)) {
 		/* This should never occur! */
 		irq_err_count++;
maintainer
#7 lore raw
Quoted 51 lines · Peter Zijlstra
Quoted 3 lines · Michael Cree

Yes, done. I also took the liberty to fix an undefined variable and multiple defined variable errors that were exposed by compilation. Will reply to this with the patch.

Thanks, and sorry for messing up Alpha that bad.. I have an alpha compiler and I really through I compile tested it :/

Quoted 3 lines

I've also tested it on a UP alpha. It worked well for a little while but after running 'perf top' for a number of seconds I got the following warning:

<snip warn>

Quoted 22 lines

which is from the line in alpha_pmu_start() that checks that PERF_HES_STOPPED is set.

I see that the backtrace is from the Alpha timer_interrupt() code which goes something like this:

[do some stuff updating timer deltas then...]

#ifndef CONFIG_SMP
         while (nticks--)
                 update_process_times(user_mode(get_irq_regs()));

#endif

         if (test_perf_event_pending()) {
                 clear_perf_event_pending();
                 perf_event_do_pending();
         }

         return IRQ_HANDLED;
}
Quoted 5 lines

When I added the code for handle pending events to the timer interrupt I hadn't realised that update_process_times() could call back into the perf code. I'm speculating here, but could it be that there is pending work to stop the HW counter, but the call to re-start it is beating the call to stop it?

Right, so the ->start() call came from perf_ctx_adjust_freq(), which depending on whether perf_adjust_period() gets inlined, can have two such calls.

Assuming it didn't inline (there's two callsites, which should defeat the inline static functions with a single callsite heuristic), you hit the unthrottle() call.

Quoted 8 lines

Ahh, the alpha throttle call should be using the fancy new stop function too (will fold into your earlier patch if it indeed works):

As to the point you raised above, yes, I think it would be prudent to call perf_event_do_pending() before update_process_times().

Damn I suck.. Please try this one.

---

arch/alpha/kernel/perf_event.c +1 -1
Index: linux-2.6/arch/alpha/kernel/perf_event.c
===================================================================
@@ -850,7 +850,7 @@ static void alpha_perf_event_irq_handler
 			/* Interrupts coming too quickly; "throttle" the
 			 * counter, i.e., disable it for a little while.
 			 */
-			cpuc->idx_mask &= ~(1UL<<idx);
+			alpha_pmu_stop(event, 0);
 		}
 	}
 	wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
#8 lore raw
Quoted 24 lines · Peter Zijlstra
Quoted 6 lines · Peter Zijlstra
Quoted 3 lines · Michael Cree

I've also tested it on a UP alpha. It worked well for a little while but after running 'perf top' for a number of seconds I got the following warning:

Quoted 8 lines

Ahh, the alpha throttle call should be using the fancy new stop function too (will fold into your earlier patch if it indeed works):

As to the point you raised above, yes, I think it would be prudent to call perf_event_do_pending() before update_process_times().

Damn I suck.. Please try this one.

---
Index: linux-2.6/arch/alpha/kernel/perf_event.c
===================================================================

arch/alpha/kernel/perf_event.c static void alpha_perf_event_irq_handler · line 856

Quoted 8 lines
arch/alpha/kernel/perf_event.c +1 -1
@@ -850,7 +850,7 @@ static void alpha_perf_event_irq_handler
  			/* Interrupts coming too quickly; "throttle" the
  			 * counter, i.e., disable it for a little while.
  			 */
-			cpuc->idx_mask&= ~(1UL<<idx);
+			alpha_pmu_stop(event, 0);
  		}
  	}
  	wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);

Thanks, that does the trick. I have had it running for a while without any problems... Well, except for a new problem which I describe below. Anyway the above patch has fixed the original problem so can be rolled into my earlier one as you suggest.

I haven't shifted perf_event_do_pending() before update_process_times() in the timer interrupt yet. When I get around to it I'll send a patch through the Alpha maintainer as this is independent of the work on the core perf event code.

Now to the new problem I saw: I accidently reran 'perf top' while it was already running, and the second instance of 'perf top' seg-faulted and the kernel OOPSed with the following:

[  818.575752] Unable to handle kernel paging request at virtual address 0000000000000060
[  818.575752] perf(4935): Oops 0
[  818.575752] pc = [<fffffc000036b8b8>]  ra = [<fffffc000036ba3c>]  ps = 0000    Not tainted
[  818.575752] pc is at put_ctx+0x18/0xb0
[  818.575752] ra is at free_event+0xec/0x220
[  818.575752] v0 = 0000000000000000  t0 = 0000000000000000  t1 = 0000000000000002
[  818.575752] t2 = 0000000000000000  t3 = 0000000000000000  t4 = fffffc000063e4b0
[  818.575752] t5 = fffffc0034837000  t6 = fffffc0034837000  t7 = fffffc003abe0000
[  818.575752] s0 = 0000000000000000  s1 = 0000000000000000  s2 = 0000000000000000
[  818.575752] s3 = ffffffffffffffff  s4 = 0000000000000000  s5 = 0000000000000053
[  818.575752] s6 = fffffc0034836c00
[  818.575752] a0 = 0000000000000000  a1 = fffffc00311a6980  a2 = 0000000000000000
[  818.575752] a3 = 0000000000000001  a4 = fffffc0000797110  a5 = 00000001200055f8
[  818.575752] t8 = 0000000000000001  t9 = 00000001200396f4  t10= 0000000000000000
[  818.575752] t11= 000000000000000a  pv = fffffc000031d650  at = fffffc000036f8bc
[  818.575752] gp = fffffc00007d6e40  sp = fffffc003abe3e18
[  818.575752] Disabling lock debugging due to kernel taint
[  818.575752] Trace:
[  818.575752] [<fffffc000036ba3c>] free_event+0xec/0x220
[  818.575752] [<fffffc000036fa24>] SyS_perf_event_open+0x2a4/0x6f0
[  818.575752] [<fffffc0000310c24>] entSys+0xa4/0xc0
[  818.575752]
[  818.575752] Code: 27bb0047  23bdb5a0  23defff0  b53e0008  b75e0000 47f00409 <a8500060> 40403121

The first instance of 'perf top' kept running happily.

Cheers
Michael.

maintainer
#9 lore raw
Quoted 35 lines · Michael Cree
[  818.575752] Unable to handle kernel paging request at virtual address 
0000000000000060
[  818.575752] perf(4935): Oops 0
[  818.575752] pc = [<fffffc000036b8b8>]  ra = [<fffffc000036ba3c>]  ps 
= 0000    Not tainted
[  818.575752] pc is at put_ctx+0x18/0xb0
[  818.575752] ra is at free_event+0xec/0x220
[  818.575752] v0 = 0000000000000000  t0 = 0000000000000000  t1 = 
0000000000000002
[  818.575752] t2 = 0000000000000000  t3 = 0000000000000000  t4 = 
fffffc000063e4b0
[  818.575752] t5 = fffffc0034837000  t6 = fffffc0034837000  t7 = 
fffffc003abe0000
[  818.575752] s0 = 0000000000000000  s1 = 0000000000000000  s2 = 
0000000000000000
[  818.575752] s3 = ffffffffffffffff  s4 = 0000000000000000  s5 = 
0000000000000053
[  818.575752] s6 = fffffc0034836c00
[  818.575752] a0 = 0000000000000000  a1 = fffffc00311a6980  a2 = 
0000000000000000
[  818.575752] a3 = 0000000000000001  a4 = fffffc0000797110  a5 = 
00000001200055f8
[  818.575752] t8 = 0000000000000001  t9 = 00000001200396f4  t10= 
0000000000000000
[  818.575752] t11= 000000000000000a  pv = fffffc000031d650  at = 
fffffc000036f8bc
[  818.575752] gp = fffffc00007d6e40  sp = fffffc003abe3e18
[  818.575752] Disabling lock debugging due to kernel taint
[  818.575752] Trace:
[  818.575752] [<fffffc000036ba3c>] free_event+0xec/0x220
[  818.575752] [<fffffc000036fa24>] SyS_perf_event_open+0x2a4/0x6f0
[  818.575752] [<fffffc0000310c24>] entSys+0xa4/0xc0
[  818.575752]
[  818.575752] Code: 27bb0047  23bdb5a0  23defff0  b53e0008  b75e0000 
Quoted 1 line

47f00409 <a8500060> 40403121

Does the below cure that?

---
commit 0c67b40872326a5340cab51d79a192a5fbaeb484

Author: Peter Zijlstra <[email protected]>
Date:   Mon Sep 13 11:15:58 2010 +0200

perf: Fix free_event()

With the context rework stuff we can actually end up freeing an event before it gets attached to a context.

LKML-Reference: <new-submission>

kernel/perf_event.c +3 -1
index bc46bff..440f9ca 100644
@@ -2150,7 +2150,9 @@ static void free_event(struct perf_event *event)
 	if (event->destroy)
 		event->destroy(event);
 
-	put_ctx(event->ctx);
+	if (event->ctx)
+		put_ctx(event->ctx);
+
 	call_rcu(&event->rcu_head, free_event_rcu);
 }
 
#5 lore raw

[tip:perf/core] alpha: Fix HW performance counters to be stopped properly

In reply to Michael Cree
Commit-ID:  65175c07653534294257f75baa03a36edad86870
Gitweb:     http://git.kernel.org/tip/65175c07653534294257f75baa03a36edad86870
Author:     Michael Cree <[email protected]>
AuthorDate: Sun, 12 Sep 2010 17:37:24 +1200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 15 Sep 2010 10:43:59 +0200

alpha: Fix HW performance counters to be stopped properly

Also fix a few compile errors due to undefined and duplicated variables.

LKML-Reference: <[email protected]>

---
 arch/alpha/kernel/perf_event.c |   19 ++++++++++---------
 1 files changed, 10 insertions(+), 9 deletions(-)
arch/alpha/kernel/perf_event.c +10 -9
index a25fe9e..1cc4968 100644
@@ -422,9 +422,10 @@ static void maybe_change_configuration(struct cpu_hw_events *cpuc)
 static int alpha_pmu_add(struct perf_event *event, int flags)
 {
 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
+	struct hw_perf_event *hwc = &event->hw;
 	int n0;
 	int ret;
-	unsigned long flags;
+	unsigned long irq_flags;
 
 	/*
 	 * The Sparc code has the IRQ disable first followed by the perf
@@ -435,7 +436,7 @@ static int alpha_pmu_add(struct perf_event *event, int flags)
 	 * final PMI to occur before we disable interrupts.
 	 */
 	perf_pmu_disable(event->pmu);
-	local_irq_save(flags);
+	local_irq_save(irq_flags);
 
 	/* Default to error to be returned */
 	ret = -EAGAIN;
@@ -458,7 +459,7 @@ static int alpha_pmu_add(struct perf_event *event, int flags)
 	if (!(flags & PERF_EF_START))
 		hwc->state |= PERF_HES_STOPPED;
 
-	local_irq_restore(flags);
+	local_irq_restore(irq_flags);
 	perf_pmu_enable(event->pmu);
 
 	return ret;
@@ -474,11 +475,11 @@ static void alpha_pmu_del(struct perf_event *event, int flags)
 {
 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
 	struct hw_perf_event *hwc = &event->hw;
-	unsigned long flags;
+	unsigned long irq_flags;
 	int j;
 
 	perf_pmu_disable(event->pmu);
-	local_irq_save(flags);
+	local_irq_save(irq_flags);
 
 	for (j = 0; j < cpuc->n_events; j++) {
 		if (event == cpuc->event[j]) {
@@ -504,7 +505,7 @@ static void alpha_pmu_del(struct perf_event *event, int flags)
 		}
 	}
 
-	local_irq_restore(flags);
+	local_irq_restore(irq_flags);
 	perf_pmu_enable(event->pmu);
 }
 
@@ -523,7 +524,7 @@ static void alpha_pmu_stop(struct perf_event *event, int flags)
 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
 
 	if (!(hwc->state & PERF_HES_STOPPED)) {
-		cpuc->idx_mask &= !(1UL<<hwc->idx);
+		cpuc->idx_mask &= ~(1UL<<hwc->idx);
 		hwc->state |= PERF_HES_STOPPED;
 	}
 
@@ -533,7 +534,7 @@ static void alpha_pmu_stop(struct perf_event *event, int flags)
 	}
 
 	if (cpuc->enabled)
-		wrperfmon(PERFMON_CMD_ENABLE, (1UL<<hwc->idx));
+		wrperfmon(PERFMON_CMD_DISABLE, (1UL<<hwc->idx));
 }
 
 
@@ -849,7 +850,7 @@ static void alpha_perf_event_irq_handler(unsigned long la_ptr,
 			/* Interrupts coming too quickly; "throttle" the
 			 * counter, i.e., disable it for a little while.
 			 */
-			cpuc->idx_mask &= ~(1UL<<idx);
+			alpha_pmu_stop(event, 0);
 		}
 	}
 	wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
#10 lore raw
In reply to Peter Zijlstra
Quoted 6 lines · Peter Zijlstra
Quoted 4 lines · Michael Cree
[  818.575752] Unable to handle kernel paging request at virtual address
0000000000000060
[  818.575752] perf(4935): Oops 0
Quoted 8 lines

Does the below cure that?

---
commit 0c67b40872326a5340cab51d79a192a5fbaeb484

Author: Peter Zijlstra <[email protected]>
Date:   Mon Sep 13 11:15:58 2010 +0200

perf: Fix free_event()

Yes, thanks, that's fixed it.

Cheers
Michael.