From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932282AbcBVWUm (ORCPT ); Mon, 22 Feb 2016 17:20:42 -0500 Received: from www.linutronix.de ([62.245.132.108]:36864 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932124AbcBVWUi (ORCPT ); Mon, 22 Feb 2016 17:20:38 -0500 Message-Id: <20160222221012.231222076@linutronix.de> User-Agent: quilt/0.63-1 Date: Mon, 22 Feb 2016 22:19:21 -0000 From: Thomas Gleixner To: LKML Cc: Peter Zijlstra , x86@kernel.org, Borislav Petkov , Stephane Eranian , Harish Chegondi , Kan Liang , Andi Kleen , Jacob Pan Subject: [patch V3 19/28] x86/perf/intel/rapl: Add proper error handling References: <20160222220733.632098221@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15 Content-Disposition: inline; filename=x86-perf-intel-rapl--Add-proper-error-handling.patch X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1,SHORTCIRCUIT=-0.0001 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Like uncore the rapl driver lacks error handling. It leaks memory and leaves the hotplug notifier registered. Add the proper error checks, cleanup the memory and register the hotplug notifier only on success. Signed-off-by: Thomas Gleixner --- arch/x86/kernel/cpu/perf_event_intel_rapl.c | 29 ++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) Index: b/arch/x86/kernel/cpu/perf_event_intel_rapl.c =================================================================== --- a/arch/x86/kernel/cpu/perf_event_intel_rapl.c +++ b/arch/x86/kernel/cpu/perf_event_intel_rapl.c @@ -686,6 +686,14 @@ static int rapl_check_hw_unit(void) return 0; } +static void __init cleanup_rapl_pmus(void) +{ + int cpu; + + for_each_online_cpu(cpu) + kfree(per_cpu(rapl_pmu, cpu)); +} + static const struct x86_cpu_id rapl_cpu_match[] = { [0] = { .vendor = X86_VENDOR_INTEL, .family = 6 }, [1] = {}, @@ -702,7 +710,7 @@ static int __init rapl_pmu_init(void) * check for Intel processor family 6 */ if (!x86_match_cpu(rapl_cpu_match)) - return 0; + return -ENODEV; /* check supported CPU */ switch (boot_cpu_data.x86_model) { @@ -734,8 +742,9 @@ static int __init rapl_pmu_init(void) break; default: /* unsupported */ - return 0; + return -ENODEV; } + ret = rapl_check_hw_unit(); if (ret) return ret; @@ -743,6 +752,7 @@ static int __init rapl_pmu_init(void) /* run cpu model quirks */ for (quirk = rapl_quirks; quirk; quirk = quirk->next) quirk->func(); + cpu_notifier_register_begin(); for_each_online_cpu(cpu) { @@ -752,15 +762,14 @@ static int __init rapl_pmu_init(void) rapl_cpu_init(cpu); } - __perf_cpu_notifier(rapl_cpu_notifier); - ret = perf_pmu_register(&rapl_pmu_class, "power", -1); if (WARN_ON(ret)) { pr_info("RAPL PMU detected, registration failed (%d), RAPL PMU disabled\n", ret); - cpu_notifier_register_done(); - return -1; + goto out; } + __perf_cpu_notifier(rapl_cpu_notifier); + pmu = __this_cpu_read(rapl_pmu); pr_info("RAPL PMU detected," @@ -775,9 +784,13 @@ static int __init rapl_pmu_init(void) rapl_domain_names[i], rapl_hw_unit[i]); } } -out: - cpu_notifier_register_done(); + cpu_notifier_register_done(); return 0; + +out: + cleanup_rapl_pmus(); + cpu_notifier_register_done(); + return ret; } device_initcall(rapl_pmu_init);