Skip to contentSkip to conversation

perf,x86: fix uninitialized pt_regs in intel_pmu_drain_bts_buffer()

PATCH Superseded Stephane Eranian 2 participants 5 messages Mar 18, 2013 – Mar 19, 2013

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf,x86: fix uninitialized pt_regs in intel_pmu_drain_bts_buffer()

This patch fixes an uninitialized pt_regs struct in drain BTS function. The pt_regs struct is propagated all the way to the code_get_segment() function from perf_instruction_pointer() and may get garbage.

We cannot simply inherit the actual pt_regs from the interrupt because BTS must be flushed on context-switch or when the associated event is disabled. And there we do not have a pt_regs handy.

Setting pt_regs to all zeroes may not be the best option but it is not clear what else to do given where the drain_bts_buffer() is called from.

---

arch/x86/kernel/cpu/perf_event_intel_ds.c +2 -0
index b05a575..208f0c8 100644
@@ -302,6 +302,8 @@ int intel_pmu_drain_bts_buffer(void)
 	struct perf_sample_data data;
 	struct pt_regs regs;
 
+	memset(&regs, 0, sizeof(regs));
+
 	if (!event)
 		return 0;
 
maintainer
#1 lore raw
Quoted 22 lines · Stephane Eranian

This patch fixes an uninitialized pt_regs struct in drain BTS function. The pt_regs struct is propagated all the way to the code_get_segment() function from perf_instruction_pointer() and may get garbage.

We cannot simply inherit the actual pt_regs from the interrupt because BTS must be flushed on context-switch or when the associated event is disabled. And there we do not have a pt_regs handy.

Setting pt_regs to all zeroes may not be the best option but it is not clear what else to do given where the drain_bts_buffer() is called from.

---

arch/x86/kernel/cpu/perf_event_intel_ds.c +0 -0
index b05a575..208f0c8 100644

arch/x86/kernel/cpu/perf_event_intel_ds.c int intel_pmu_drain_bts_buffer(void) · line 309

Quoted 8 lines
arch/x86/kernel/cpu/perf_event_intel_ds.c +2 -0
@@ -302,6 +302,8 @@ int intel_pmu_drain_bts_buffer(void)
 	struct perf_sample_data data;
 	struct pt_regs regs;
 
+	memset(&regs, 0, sizeof(regs));
+
 	if (!event)
 		return 0;
 

Should we not replace:

	regs.ip     = 0;

with that memset? It avoids the memset work in a few cases and removes the then superfluous clearing of the IP field.

#2 lore raw
Quoted 34 lines · Peter Zijlstra
Quoted 22 lines · Stephane Eranian

This patch fixes an uninitialized pt_regs struct in drain BTS function. The pt_regs struct is propagated all the way to the code_get_segment() function from perf_instruction_pointer() and may get garbage.

We cannot simply inherit the actual pt_regs from the interrupt because BTS must be flushed on context-switch or when the associated event is disabled. And there we do not have a pt_regs handy.

Setting pt_regs to all zeroes may not be the best option but it is not clear what else to do given where the drain_bts_buffer() is called from.

---

arch/x86/kernel/cpu/perf_event_intel_ds.c +0 -0
index b05a575..208f0c8 100644

arch/x86/kernel/cpu/perf_event_intel_ds.c int intel_pmu_drain_bts_buffer(void) · line 309

Quoted 8 lines
arch/x86/kernel/cpu/perf_event_intel_ds.c +2 -0
@@ -302,6 +302,8 @@ int intel_pmu_drain_bts_buffer(void)
      struct perf_sample_data data;
      struct pt_regs regs;
 
+     memset(&regs, 0, sizeof(regs));
+
      if (!event)
              return 0;
 

Should we not replace:

Quoted 5 lines
        regs.ip     = 0;

with that memset? It avoids the memset work in a few cases and removes the then superfluous clearing of the IP field.

We could drop it because it's covered by the memset().

The issue here was that you eventually end up in code_segment_base() which looks at other uninitialized fields in pt_regs and may cause breakage.

maintainer
#3 lore raw
Quoted 9 lines · Stephane Eranian
Quoted 5 lines

Should we not replace:

        regs.ip     = 0;

with that memset? It avoids the memset work in a few cases and

removes

Quoted 2 lines

the then superfluous clearing of the IP field.

We could drop it because it's covered by the memset().

Yeah, but also place the memset a little lower than you did, avoids the stack writes when not needed.

Quoted 3 lines

The issue here was that you eventually end up in code_segment_base() which
looks at other uninitialized fields in pt_regs and may cause breakage.

Right, your changelog said so ;-)

#4 lore raw
Quoted 5 lines · Peter Zijlstra
Quoted 4 lines · Stephane Eranian
Quoted 4 lines

Should we not replace:

        regs.ip     = 0;
Quoted 9 lines
Quoted 5 lines
Quoted 1 line

with that memset? It avoids the memset work in a few cases and

removes

Quoted 2 lines

the then superfluous clearing of the IP field.

We could drop it because it's covered by the memset().

Yeah, but also place the memset a little lower than you did, avoids the stack writes when not needed.

Ok, I get it now. Yes, we can move it after the test and drop reg.val = 0.

Quoted 6 lines
Quoted 3 lines

The issue here was that you eventually end up in code_segment_base() which
looks at other uninitialized fields in pt_regs and may cause breakage.

Right, your changelog said so ;-)