From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754117AbdJIMEp (ORCPT ); Mon, 9 Oct 2017 08:04:45 -0400 Received: from mail-wm0-f42.google.com ([74.125.82.42]:48288 "EHLO mail-wm0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751621AbdJIMEn (ORCPT ); Mon, 9 Oct 2017 08:04:43 -0400 X-Google-Smtp-Source: AOwi7QB4McPwle5Mda9LxuN567/dJhJo7AmWFkMjITAoN9D3rySYje+n8rG08HaOQGoQD5UFG+8+dg== Subject: Re: [PATCH 1/2] ARM: cpuidle: refine failure handling in init flow To: Leo Yan , "Rafael J. Wysocki" , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Stefan Wahren References: <1504507934-14218-1-git-send-email-leo.yan@linaro.org> From: Daniel Lezcano Message-ID: <435dbae2-fc35-722e-85d5-17ca401c2e81@linaro.org> Date: Mon, 9 Oct 2017 14:04:40 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: <1504507934-14218-1-git-send-email-leo.yan@linaro.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 04/09/2017 08:52, Leo Yan wrote: > After applied Stefan Wahren patch ("ARM: cpuidle: Avoid memleak if init > fail") there have no memleak issue, but the code is not consistent to > handle initialization failure between driver registration and device > registration. And when device registration fails, it misses to > unregister the driver. > > So this patch is to refine failure handling in init flow, it adds two > 'goto' tags: when register device fails, it goto 'init_dev_fail' tag and > free 'dev' structure and unregister driver; when register driver fails, > it goto 'init_drv_fail' tag and free 'drv' structure. > > Cc: Daniel Lezcano > Cc: Stefan Wahren > Signed-off-by: Leo Yan > --- > drivers/cpuidle/cpuidle-arm.c | 25 ++++++++++++++++--------- > 1 file changed, 16 insertions(+), 9 deletions(-) > > diff --git a/drivers/cpuidle/cpuidle-arm.c b/drivers/cpuidle/cpuidle-arm.c > index 52a7505..f419f6a 100644 > --- a/drivers/cpuidle/cpuidle-arm.c > +++ b/drivers/cpuidle/cpuidle-arm.c > @@ -86,10 +86,13 @@ static int __init arm_idle_init(void) > > for_each_possible_cpu(cpu) { > > + drv = NULL; ^^^^^ This initialization is not needed. > + dev = NULL; > + > drv = kmemdup(&arm_idle_driver, sizeof(*drv), GFP_KERNEL); > if (!drv) { > ret = -ENOMEM; > - goto out_fail; > + goto init_drv_fail; Here we can jump directly to out_fail, no ? > } > > drv->cpumask = (struct cpumask *)cpumask_of(cpu); > @@ -104,13 +107,13 @@ static int __init arm_idle_init(void) > ret = dt_init_idle_driver(drv, arm_idle_state_match, 1); > if (ret <= 0) { > ret = ret ? : -ENODEV; > - goto init_fail; > + goto init_drv_fail; goto out_kfree_drv; > } > > ret = cpuidle_register_driver(drv); > if (ret) { > pr_err("Failed to register cpuidle driver\n"); > - goto init_fail; > + goto init_drv_fail; goto out_unregister_drv; etc ... > return 0; > -init_fail: > + > +init_dev_fail: > + kfree(dev); > + cpuidle_unregister_driver(drv); > + > +init_drv_fail: > kfree(drv); > -out_fail: > + So, the code should end up with: out_kfree_dev: kfree(dev); out_unregister_drv: cpuidle_unregister_drv(drv); out_kfree_drv: kfree(drv); > while (--cpu >= 0) { > dev = per_cpu(cpuidle_devices, cpu); > cpuidle_unregister_device(dev); > Perhaps it could nicer to create a function with the rollback embedded: for_each_possible_cpu(cpu) { ret = arm_idle_init_cpu(cpu) if (ret) goto out_fail; } return 0; out_fail: while (--cpu >= 0) { cpuidle_unregister_device(per_cpu(cpuidle_devices,cpu)); cpuidle_unregister_driver(cpuidle_get_cpu_driver(dev)); kfree(dev); kfree(drv); } return ret; And arm_idle_init_cpu(int cpu) does what is currently in the loop content. -- Linaro.org │ Open source software for ARM SoCs Follow Linaro: Facebook | Twitter | Blog