x86,perf: P4 PMU -- protect sensible procedures from preemption
Subsystems (2)
PATCH 1/1 x86,perf: P4 PMU -- protect sensible procedures from preemption
Steven reported
| | I'm getting: | | Pid: 3477, comm: perf Not tainted 2.6.34-rc6 #2727 | Call Trace: | [<ffffffff811c7565>] debug_smp_processor_id+0xd5/0xf0 | [<ffffffff81019874>] p4_hw_config+0x2b/0x15c | [<ffffffff8107acbc>] ? trace_hardirqs_on_caller+0x12b/0x14f | [<ffffffff81019143>] hw_perf_event_init+0x468/0x7be | [<ffffffff810782fd>] ? debug_mutex_init+0x31/0x3c | [<ffffffff810c68b2>] T.850+0x273/0x42e | [<ffffffff810c6cab>] sys_perf_event_open+0x23e/0x3f1 | [<ffffffff81009e6a>] ? sysret_check+0x2e/0x69 | [<ffffffff81009e32>] system_call_fastpath+0x16/0x1b | | When running perf record in latest tip/perf/core |
Due to the fact that p4 counters are shared between HT threads we synthetically divide the whole set of counters into two non-intersected subsets. And while we're borrowing counters from these subsets we should not be preempted. So use get_cpu/put_cpu pair.
--- arch/x86/kernel/cpu/perf_event_p4.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/perf_event_p4.c +6 -3
@@ -421,7 +421,7 @@ static u64 p4_pmu_event_map(int hw_eventstatic int p4_hw_config (struct perf_event *event) { -int cpu =raw_smp_processor_id (); +int cpu =get_cpu ();u32 escr, cccr;/*
@@ -440,7 +440,7 @@ static int p4_hw_config(struct perf_even event->hw.config =p4_set_ht_bit (event->hw.config);if (event->attr.type !=PERF_TYPE_RAW ) -return 0 ; + goto out;/* * We don't control raw events so it's up to the caller
@@ -455,6 +455,8 @@ static int p4_hw_config(struct perf_even (p4_config_pack_escr (P4_ESCR_MASK_HT ) |p4_config_pack_cccr (P4_CCCR_MASK_HT )); +out: +put_cpu ();return 0 ; }
@@ -741,7 +743,7 @@ static int p4_pmu_schedule_events(struct {unsigned long used_mask[BITS_TO_LONGS (X86_PMC_IDX_MAX )];unsigned long escr_mask[BITS_TO_LONGS (ARCH_P4_TOTAL_ESCR )]; -int cpu =raw_smp_processor_id (); +int cpu =get_cpu ();struct hw_perf_event *hwc;struct p4_event_bind *bind;unsigned int i, thread, num;
@@ -777,6 +779,7 @@ reserve: } done: +put_cpu ();return num ? -ENOSPC :0 ; }
Quoted 77 lines · Cyrill Gorcunov
Steven reported
| | I'm getting: | | Pid: 3477, comm: perf Not tainted 2.6.34-rc6 #2727 | Call Trace: | [<ffffffff811c7565>] debug_smp_processor_id+0xd5/0xf0 | [<ffffffff81019874>] p4_hw_config+0x2b/0x15c | [<ffffffff8107acbc>] ? trace_hardirqs_on_caller+0x12b/0x14f | [<ffffffff81019143>] hw_perf_event_init+0x468/0x7be | [<ffffffff810782fd>] ? debug_mutex_init+0x31/0x3c | [<ffffffff810c68b2>] T.850+0x273/0x42e | [<ffffffff810c6cab>] sys_perf_event_open+0x23e/0x3f1 | [<ffffffff81009e6a>] ? sysret_check+0x2e/0x69 | [<ffffffff81009e32>] system_call_fastpath+0x16/0x1b | | When running perf record in latest tip/perf/core |Due to the fact that p4 counters are shared between HT threads we synthetically divide the whole set of counters into two non-intersected subsets. And while we're borrowing counters from these subsets we should not be preempted. So use get_cpu/put_cpu pair.
Reported-by Steven Rostedt <[email protected]>Tested-by Steven Rostedt <[email protected]>CC Steven Rostedt <[email protected]>CC Peter Zijlstra <[email protected]>CC Ingo Molnar <[email protected]>CC Frederic Weisbecker <[email protected]>Signed-off-by Cyrill Gorcunov <[email protected]>--- arch/x86/kernel/cpu/perf_event_p4.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)Index:
linux-2.6.git/arch/x86/kernel/cpu/perf_event_p4.c
=====================================================================arch/x86/kernel/cpu/perf_event_p4.c +5
-3@@ -421,7 +421,7 @@ static u64 p4_pmu_event_map(int hw_eventstatic int p4_hw_config (struct perf_event *event) { -int cpu =raw_smp_processor_id (); +int cpu =get_cpu ();u32 escr, cccr;/* @@ -440,7 +440,7 @@ static int p4_hw_config(struct perf_even event->hw.config =p4_set_ht_bit (event->hw.config);if (event->attr.type !=PERF_TYPE_RAW ) -return 0 ; + goto out;/* * We don't control raw events so it's up to the caller @@ -455,6 +455,8 @@ static int p4_hw_config(struct perf_even (p4_config_pack_escr (P4_ESCR_MASK_HT ) |p4_config_pack_cccr (P4_CCCR_MASK_HT )); +out: +put_cpu ();return 0 ; }@@ -741,7 +743,7 @@ static int p4_pmu_schedule_events(struct {unsigned long used_mask[BITS_TO_LONGS (X86_PMC_IDX_MAX )];unsigned long escr_mask[BITS_TO_LONGS (ARCH_P4_TOTAL_ESCR )]; -int cpu =raw_smp_processor_id (); +int cpu =get_cpu ();struct hw_perf_event *hwc;struct p4_event_bind *bind;unsigned int i, thread, num;
arch/x86/kernel/cpu/perf_event_p4.c reserve: · line 784
Quoted 6 lines
arch/x86/kernel/cpu/perf_event_p4.c +1
-0@@ -777,6 +779,7 @@ reserve: } done: +put_cpu ();return num ? -ENOSPC :0 ; }
That's no big deal. But I think the schedule_events() is called on pmu::enable() time, when preemption is already disabled.
On Wed, May 05, 2010 at 06:57:34PM +0200, Frederic Weisbecker wrote:
...
Quoted 12 lines
Quoted 12 lines
arch/x86/kernel/cpu/perf_event_p4.c +1
-1@@ -741,7 +743,7 @@ static int p4_pmu_schedule_events(struct {unsigned long used_mask[BITS_TO_LONGS (X86_PMC_IDX_MAX )];unsigned long escr_mask[BITS_TO_LONGS (ARCH_P4_TOTAL_ESCR )]; -int cpu =raw_smp_processor_id (); +int cpu =get_cpu ();struct hw_perf_event *hwc;struct p4_event_bind *bind;unsigned int i, thread, num;@@ -777,6 +779,7 @@ reserve: }
Quoted 8 lines
arch/x86/kernel/cpu/perf_event_p4.c reserve: · line 784
Quoted 4 lines
arch/x86/kernel/cpu/perf_event_p4.c +1
-0done: +put_cpu ();return num ? -ENOSPC :0 ; }That's no big deal. But I think the
schedule_events()is called onpmu::enable()time, when preemption is already disabled.
We'll be on a safe side using get/put_cpu here (ie in case if something get changed one day).
-- Cyrill
Quoted 23 lines · Cyrill Gorcunov
On Wed, May 05, 2010 at 06:57:34PM +0200, Frederic Weisbecker wrote:
...Quoted 12 lines
Quoted 12 lines
arch/x86/kernel/cpu/perf_event_p4.c +1
-1@@ -741,7 +743,7 @@ static int p4_pmu_schedule_events(struct {unsigned long used_mask[BITS_TO_LONGS (X86_PMC_IDX_MAX )];unsigned long escr_mask[BITS_TO_LONGS (ARCH_P4_TOTAL_ESCR )]; -int cpu =raw_smp_processor_id (); +int cpu =get_cpu ();struct hw_perf_event *hwc;struct p4_event_bind *bind;unsigned int i, thread, num;@@ -777,6 +779,7 @@ reserve: }Quoted 8 lines
arch/x86/kernel/cpu/perf_event_p4.c reserve: · line 784
Quoted 4 lines
arch/x86/kernel/cpu/perf_event_p4.c +1
-0done: +put_cpu ();return num ? -ENOSPC :0 ; }That's no big deal. But I think the
schedule_events()is called onpmu::enable()time, when preemption is already disabled.
Quoted 4 lines
We'll be on a safe side using get/put_cpu here (ie in case if something get changed one day).
-- Cyrill
Sure, no problem with that.
Quoted 23 lines · Cyrill Gorcunov
On Wed, May 05, 2010 at 06:57:34PM +0200, Frederic Weisbecker wrote:
...Quoted 12 lines
Quoted 12 lines
arch/x86/kernel/cpu/perf_event_p4.c +1
-1@@ -741,7 +743,7 @@ static int p4_pmu_schedule_events(struct {unsigned long used_mask[BITS_TO_LONGS (X86_PMC_IDX_MAX )];unsigned long escr_mask[BITS_TO_LONGS (ARCH_P4_TOTAL_ESCR )]; -int cpu =raw_smp_processor_id (); +int cpu =get_cpu ();struct hw_perf_event *hwc;struct p4_event_bind *bind;unsigned int i, thread, num;@@ -777,6 +779,7 @@ reserve: }Quoted 8 lines
arch/x86/kernel/cpu/perf_event_p4.c reserve: · line 784
Quoted 4 lines
arch/x86/kernel/cpu/perf_event_p4.c +1
-0done: +put_cpu ();return num ? -ENOSPC :0 ; }That's no big deal. But I think the
schedule_events()is called onpmu::enable()time, when preemption is already disabled.
Quoted 2 lines
We'll be on a safe side using get/put_cpu here (ie in case if something get changed one day).
hm, when 'something gets changed one day' we'll see a warning when using unsafe primitives.
So if preemption is always off here we really should not add extra runtime overhead via get_cpu()/put_cpu().
So wouldnt it be better (and faster) to disable preemption in hw_perf_event_init(), which seems to be the bit missing?
Ingo
Quoted 32 lines · Ingo Molnar
Quoted 23 lines · Cyrill Gorcunov
On Wed, May 05, 2010 at 06:57:34PM +0200, Frederic Weisbecker wrote:
...Quoted 12 lines
Quoted 12 lines
arch/x86/kernel/cpu/perf_event_p4.c +1
-1@@ -741,7 +743,7 @@ static int p4_pmu_schedule_events(struct { unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; unsigned long escr_mask[BITS_TO_LONGS(ARCH_P4_TOTAL_ESCR)]; -int cpu =raw_smp_processor_id (); +int cpu =get_cpu (); struct hw_perf_event *hwc; struct p4_event_bind *bind; unsigned int i, thread, num;@@ -777,6 +779,7 @@ reserve: }Quoted 8 lines
Quoted 4 lines
done:
+put_cpu();
return num ?-ENOSPC: 0;
}That's no big deal. But I think the
schedule_events()is called onpmu::enable()time, when preemption is already disabled.Quoted 2 lines
We'll be on a safe side using get/put_cpu here (ie in case if something get changed one day).
hm, when 'something gets changed one day' we'll see a warning when using unsafe primitives.
Quoted 8 lines
So if preemption is always off here we really should not add extra runtime overhead via get_cpu()/put_cpu().
So wouldnt it be better (and faster) to disable preemption in
hw_perf_event_init(), which seems to be the bit missing?Ingo
the thing are that p4 is only snippet here which is sensible to preemtion, and hw_perf_event_init is executing with preemtion off (but i could miss the details here, dont have code under my hands at moment, so PeterZ help is needed ;) but more important reason why i've saved get/put here is that otherwise i would not have rights to put tested-by tag, since it would not be the patch Steven has tested. We could make a patch on top of this one, or we could drop this one, make new with explicit preemt off in caller and use smp_processor_id in p4 schedule routine. What is preferred?
Quoted 42 lines · Cyrill Gorcunov
Quoted 32 lines · Ingo Molnar
Quoted 23 lines · Cyrill Gorcunov
On Wed, May 05, 2010 at 06:57:34PM +0200, Frederic Weisbecker wrote:
...Quoted 12 lines
Quoted 12 lines
(quoted diff) +1
-1@@ -741,7 +743,7 @@ static int p4_pmu_schedule_events(struct ?{ ? unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; ? unsigned long escr_mask[BITS_TO_LONGS(ARCH_P4_TOTAL_ESCR)]; - int cpu = raw_smp_processor_id(); + int cpu = get_cpu(); ? struct hw_perf_event *hwc; ? struct p4_event_bind *bind; ? unsigned int i, thread, num;@@ -777,6 +779,7 @@ reserve: ? }Quoted 8 lines
Quoted 4 lines
?done:
+put_cpu();
? return num ?-ENOSPC: 0;
?}That's no big deal. But I think the
schedule_events()is called onpmu::enable()time, when preemption is already disabled.Quoted 2 lines
We'll be on a safe side using get/put_cpu here (ie in case if something get changed one day).
hm, when 'something gets changed one day' we'll see a warning when using unsafe primitives.
Quoted 8 lines
So if preemption is always off here we really should not add extra runtime overhead via get_cpu()/put_cpu().
So wouldnt it be better (and faster) to disable preemption in
hw_perf_event_init(), which seems to be the bit missing?? ? ? ?Ingo
Quoted 8 lines
the thing are that p4 is only snippet here which is sensible to preemtion, and
hw_perf_event_initis executing with preemtion off (but i could miss the details here, dont have code under my hands at moment, so PeterZ help is needed ;) but more important reason why i've saved get/put here is that otherwise i would not have rights to put tested-by tag, since it would not be the patch Steven has tested. We could make a patch on top of this one, or we could drop this one, make new with explicit preemt off in caller and usesmp_processor_idin p4 schedule routine. What is preferred?
We want the one with the least runtime overhead. These are instrumentation routines, so we want to optimize them as much as possible.
Thanks,
Ingo
Quoted 54 lines · Ingo Molnar
Quoted 42 lines · Cyrill Gorcunov
Quoted 32 lines · Ingo Molnar
Quoted 23 lines · Cyrill Gorcunov
On Wed, May 05, 2010 at 06:57:34PM +0200, Frederic Weisbecker wrote:
...Quoted 12 lines
Quoted 12 lines
(quoted diff) +1
-1@@ -741,7 +743,7 @@ static int p4_pmu_schedule_events(struct ?{ ? unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; ? unsigned long escr_mask[BITS_TO_LONGS(ARCH_P4_TOTAL_ESCR)]; - int cpu = raw_smp_processor_id(); + int cpu = get_cpu(); ? struct hw_perf_event *hwc; ? struct p4_event_bind *bind; ? unsigned int i, thread, num;@@ -777,6 +779,7 @@ reserve: ? }Quoted 8 lines
Quoted 4 lines
?done:
+put_cpu();
? return num ?-ENOSPC: 0;
?}That's no big deal. But I think the
schedule_events()is called onpmu::enable()time, when preemption is already disabled.Quoted 2 lines
We'll be on a safe side using get/put_cpu here (ie in case if something get changed one day).
hm, when 'something gets changed one day' we'll see a warning when using unsafe primitives.
Quoted 8 lines
So if preemption is always off here we really should not add extra runtime overhead via get_cpu()/put_cpu().
So wouldnt it be better (and faster) to disable preemption in
hw_perf_event_init(), which seems to be the bit missing?? ? ? ?Ingo
Quoted 8 lines
the thing are that p4 is only snippet here which is sensible to preemtion, and
hw_perf_event_initis executing with preemtion off (but i could miss the details here, dont have code under my hands at moment, so PeterZ help is needed ;) but more important reason why i've saved get/put here is that otherwise i would not have rights to put tested-by tag, since it would not be the patch Steven has tested. We could make a patch on top of this one, or we could drop this one, make new with explicit preemt off in caller and usesmp_processor_idin p4 schedule routine. What is preferred?
Quoted 7 lines
We want the one with the least runtime overhead. These are instrumentation routines, so we want to optimize them as much as possible.
Thanks,
Ingo
ok, Ingo, dont apply this patch then for a while.
Quoted 11 lines · Cyrill Gorcunov
Quoted 9 lines · Ingo Molnar
Quoted 8 lines
the thing are that p4 is only snippet here which is sensible to preemtion, and
hw_perf_event_initis executing with preemtion off (but i could miss the details here, dont have code under my hands at moment, so PeterZ help is needed ;) but more important reason why i've saved get/put here is that otherwise i would not have rights to put tested-by tag, since it would not be the patch Steven has tested. We could make a patch on top of this one, or we could drop this one, make new with explicit preemt off in caller and usesmp_processor_idin p4 schedule routine. What is preferred?
Quoted 2 lines
Quoted 2 lines
We want the one with the least runtime overhead. These are instrumentation routines, so we want to optimize them as much as possible.
Yeah, my point was either disable preemption or keep the checks. In other words, if you don't disable preemption, do not use raw_smp_procesor_id(), because then we will not catch it if it changes in the future.
Quoted 1 line
ok, Ingo, dont apply this patch then for a while.
Send another patch, I'll test it again ;-)
-- Steve
On Thu, May 06, 2010 at 09:45:24AM -0400, Steven Rostedt wrote:
...
Quoted 9 lines
Quoted 2 lines
Quoted 2 lines
We want the one with the least runtime overhead. These are instrumentation routines, so we want to optimize them as much as possible.
Yeah, my point was either disable preemption or keep the checks. In other words, if you don't disable preemption, do not use
raw_smp_procesor_id(), because then we will not catch it if it changes in the future.
Quoted 7 lines
Quoted 1 line
ok, Ingo, dont apply this patch then for a while.
Send another patch, I'll test it again ;-)
-- Steve
Ingo, Steven, it seems we have potential preemtion available in perf_event.c:validate_group:x86_pmu.schedule_events() which is reached via syscall from userspace perf_event_open() call, so get_cpu is still needed. But I'm a bit messed with call graph at the moment :(
-- Cyrill
Quoted 19 lines · Cyrill Gorcunov
On Thu, May 06, 2010 at 09:45:24AM -0400, Steven Rostedt wrote:
...Quoted 9 lines
Quoted 2 lines
Quoted 2 lines
We want the one with the least runtime overhead. These are instrumentation routines, so we want to optimize them as much as possible.
Yeah, my point was either disable preemption or keep the checks. In other words, if you don't disable preemption, do not use
raw_smp_procesor_id(), because then we will not catch it if it changes in the future.Quoted 7 lines
Quoted 1 line
ok, Ingo, dont apply this patch then for a while.
Send another patch, I'll test it again ;-)
-- Steve
Quoted 7 lines
Ingo, Steven, it seems we have potential preemtion available in perf_event.c:validate_group:x86_pmu.schedule_events() which is reached via syscall from userspace
perf_event_open()call, soget_cpuis still needed. But I'm a bit messed with call graph at the moment :(-- Cyrill
Steve, while I'm diving through call graph could you give this patch a shot? If preemtion happens -- it'll trigger it fast.
-- Cyrill
--- arch/x86/kernel/cpu/perf_event_p4.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/perf_event_p4.c +5 -3
@@ -421,7 +421,7 @@ static u64 p4_pmu_event_map(int hw_eventstatic int p4_hw_config (struct perf_event *event) { -int cpu =raw_smp_processor_id (); +int cpu =get_cpu ();u32 escr, cccr;/*
@@ -440,7 +440,7 @@ static int p4_hw_config(struct perf_even event->hw.config =p4_set_ht_bit (event->hw.config);if (event->attr.type !=PERF_TYPE_RAW ) -return 0 ; + goto out;/* * We don't control raw events so it's up to the caller
@@ -455,6 +455,8 @@ static int p4_hw_config(struct perf_even (p4_config_pack_escr (P4_ESCR_MASK_HT ) |p4_config_pack_cccr (P4_CCCR_MASK_HT )); +out: +put_cpu ();return 0 ; }
@@ -741,7 +743,7 @@ static int p4_pmu_schedule_events(struct {unsigned long used_mask[BITS_TO_LONGS (X86_PMC_IDX_MAX )];unsigned long escr_mask[BITS_TO_LONGS (ARCH_P4_TOTAL_ESCR )]; -int cpu =raw_smp_processor_id (); +int cpu =smp_processor_id ();struct hw_perf_event *hwc;struct p4_event_bind *bind;unsigned int i, thread, num;
Steven Rostedt
Tested-byQuoted 26 lines · Cyrill Gorcunov
Quoted 25 lines · Cyrill Gorcunov
On Thu, May 06, 2010 at 09:45:24AM -0400, Steven Rostedt wrote:
...Quoted 9 lines
Quoted 2 lines
Quoted 2 lines
We want the one with the least runtime overhead. These are instrumentation routines, so we want to optimize them as much as possible.
Yeah, my point was either disable preemption or keep the checks. In other words, if you don't disable preemption, do not use
raw_smp_procesor_id(), because then we will not catch it if it changes in the future.Quoted 7 lines
Quoted 1 line
ok, Ingo, dont apply this patch then for a while.
Send another patch, I'll test it again ;-)
-- Steve
Ingo, Steven, it seems we have potential preemtion available in perf_event.c:validate_group:x86_pmu.schedule_events() which is reached via syscall from userspace
perf_event_open()call, soget_cpuis still needed. But I'm a bit messed with call graph at the moment :(
Quoted 4 lines
Quoted 1 line
-- Cyrill
Steve, while I'm diving through call graph could you give this patch a shot? If preemtion happens -- it'll trigger it fast.
This one works too.
-- Steve
Quoted 46 lines
-- Cyrill
--- arch/x86/kernel/cpu/perf_event_p4.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)Index:
linux-2.6.git/arch/x86/kernel/cpu/perf_event_p4.c
=====================================================================arch/x86/kernel/cpu/perf_event_p4.c +5
-3@@ -421,7 +421,7 @@ static u64 p4_pmu_event_map(int hw_eventstatic int p4_hw_config (struct perf_event *event) { -int cpu =raw_smp_processor_id (); +int cpu =get_cpu ();u32 escr, cccr;/* @@ -440,7 +440,7 @@ static int p4_hw_config(struct perf_even event->hw.config =p4_set_ht_bit (event->hw.config);if (event->attr.type !=PERF_TYPE_RAW ) -return 0 ; + goto out;/* * We don't control raw events so it's up to the caller @@ -455,6 +455,8 @@ static int p4_hw_config(struct perf_even (p4_config_pack_escr (P4_ESCR_MASK_HT ) |p4_config_pack_cccr (P4_CCCR_MASK_HT )); +out: +put_cpu ();return 0 ; }@@ -741,7 +743,7 @@ static int p4_pmu_schedule_events(struct {unsigned long used_mask[BITS_TO_LONGS (X86_PMC_IDX_MAX )];unsigned long escr_mask[BITS_TO_LONGS (ARCH_P4_TOTAL_ESCR )]; -int cpu =raw_smp_processor_id (); +int cpu =smp_processor_id ();struct hw_perf_event *hwc;struct p4_event_bind *bind;unsigned int i, thread, num;
On Thu, May 06, 2010 at 02:32:33PM -0400, Steven Rostedt wrote:
[...]
Quoted 10 lines
Quoted 3 lines
Steve, while I'm diving through call graph could you give this patch a shot? If preemtion happens -- it'll trigger it fast.
This one works too.
Tested-by Steven Rostedt <[email protected]>-- Steve
Thanks a huge, Steven! I'll tinkle a bit more with p4 pmu and will send a series of patches with this one included.
-- Cyrill