Skip to contentSkip to conversation

perf/x86: x86_schedule_events(): avoid 512 byte stack variable

PATCH New Tim Gardner 1 participant 1 message Feb 8, 2013

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

PATCH 1/1 perf/x86: x86_schedule_events(): avoid 512 byte stack variable

x86_schedule_events() creates a 512 byte automatic variable when compiled for 64 bit. Dynamically allocate this array to avoid possible stack corruption. Smatch analysis:

arch/x86/kernel/cpu/perf_event.c:727 x86_schedule_events() warn:
'constraints' puts 512 bytes on stack

Cc Peter Zijlstra <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc <[email protected]> # 2.6.34.y and higher

---

This large stack variable was introduced with 63b146490befc027a7e0923e333269e68b20d380 in 2.6.34. Since it has been around for awhile I don't know if its really a problem on this code path, but it does consume a good size chunk of the kernel stack.

Applies cleanly to 3.3.y and higher. Needs backport for older kernels.

 arch/x86/kernel/cpu/perf_event.c |    9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/perf_event.c +8 -1
index bf0f01a..1f2005e 100644
@@ -718,11 +718,15 @@ int perf_assign_events(struct event_constraint **constraints, int n,
 
 int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
 {
-	struct event_constraint *c, *constraints[X86_PMC_IDX_MAX];
+	struct event_constraint *c, **constraints;
 	unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
 	int i, wmin, wmax, num = 0;
 	struct hw_perf_event *hwc;
 
+	constraints = kmalloc(X86_PMC_IDX_MAX*sizeof(*constraints), GFP_ATOMIC);
+	if (!constraints)
+		return -ENOMEM;
+
 	bitmap_zero(used_mask, X86_PMC_IDX_MAX);
 
 	for (i = 0, wmin = X86_PMC_IDX_MAX, wmax = 0; i < n; i++) {
@@ -770,6 +774,9 @@ int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
 				x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]);
 		}
 	}
+
+	kfree(constraints);
+
 	return num ? -EINVAL : 0;
 }
 
Signature · 1 line
1.7.9.5