[PATCH] perf/x86/intel: Fix minor memleak on Skylake perf initialization

From: Andi Kleen
Date: Wed Dec 27 2017 - 17:49:31 EST


From: Andi Kleen <ak@xxxxxxxxxxxxxxx>

Tommi reports:

I'm seeing this kmemleak report in v4.15-rc4:

unreferenced object 0xffff8801f3d5d720 (size 64):
comm "swapper/0", pid 1, jiffies 4294667312 (age 2687.423s)
hex dump (first 32 bytes):
60 d1 41 ad ff ff ff ff 20 d1 41 ad ff ff ff ff `.A..... .A.....
80 d0 41 ad ff ff ff ff 40 d0 41 ad ff ff ff ff ..A.....@.A.....
backtrace:
[<00000000ab01886a>] intel_pmu_init+0x1844/0x1d38
[<00000000cf20c5f0>] init_hw_perf_events+0x8c/0x66f
[<00000000dd69189d>] do_one_initcall+0x7b/0x1d0
[<000000008ee1f02a>] kernel_init_freeable+0x163/0x2f9
[<00000000e32ca8d9>] kernel_init+0xf/0x120
[<0000000038a99264>] ret_from_fork+0x24/0x30
[<00000000ace6d26f>] 0xffffffffffffffff

$ ./scripts/faddr2line vmlinux intel_pmu_init+0x1844/0x1d38
intel_pmu_init+0x1844/0x1d38:
intel_pmu_init at arch/x86/events/intel/core.c:4296

Which matches line:
extra_attr = merge_attr(extra_attr, skl_format_attr);

So looks like "extra_attr" is leaked here.

Free the attribute in this case.

Reported-by: Tommi Rantala <tommi.t.rantala@xxxxxxxxx>
Cc: Tommi Rantala <tommi.t.rantala@xxxxxxxxx>
Fixes: a5df70c354c26 ( perf/x86: Only show format attributes)
Signed-off-by: Andi Kleen <ak@xxxxxxxxxxxxxxx>
---
arch/x86/events/intel/core.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index 09c26a4f139c..71321f48e1e6 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -3855,6 +3855,7 @@ __init int intel_pmu_init(void)
struct extra_reg *er;
int version, i;
struct attribute **extra_attr = NULL;
+ bool extra_attr_allocated = false;
char *name;

if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
@@ -4294,6 +4295,7 @@ __init int intel_pmu_init(void)
extra_attr = boot_cpu_has(X86_FEATURE_RTM) ?
hsw_format_attr : nhm_format_attr;
extra_attr = merge_attr(extra_attr, skl_format_attr);
+ extra_attr_allocated = true;
x86_pmu.cpu_events = get_hsw_events_attrs();
intel_pmu_pebs_data_source_skl(
boot_cpu_data.x86_model == INTEL_FAM6_SKYLAKE_X);
@@ -4324,6 +4326,8 @@ __init int intel_pmu_init(void)
if (version >= 2 && extra_attr) {
x86_pmu.format_attrs = merge_attr(intel_arch3_formats_attr,
extra_attr);
+ if (extra_attr_allocated)
+ kfree(extra_attr);
WARN_ON(!x86_pmu.format_attrs);
}

--
2.14.3