Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp4742pxb; Wed, 18 Aug 2021 14:23:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwEq1rIUNCGIErPqZmFhU3y8sghyxPZZOR5C4q1QH1k19Lm0dJ/Sfxjx8YpB7dmQSIgS28S X-Received: by 2002:a02:a709:: with SMTP id k9mr9762519jam.64.1629321810618; Wed, 18 Aug 2021 14:23:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629321810; cv=none; d=google.com; s=arc-20160816; b=ZIAzAHZhQ2gRGas9fC7Hntv8JaZ43NCIxs4kcnDiGJlA1Hz5O7GxfHKUH9h+YAMCnW q1QdUEPUCi+YoRBb1PmIeTUTwbUZplGl7C9YbG+21Y8cUvSr0A2LAhv0SoWF6EVtJpir FoRCtcjyGtwVmalM9J6KkAMtEI2V6p/q+fpSEgTFhlkJcVNTYbcdUImXjrZCs9Y/p4IX Iemg1qyfQXS4jHvCQcxTuvWQUWbkt5n3Vr6LxDNCLK3v9chi/3ZpwDVO57hKm/zY30+m uiPEMtgrJIWRWQwfGkApSQh6bv8u6pYiCKQTqqymKUgf5y4Zs+UyeTdeGkZ8/qZA78P3 WbeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=bKweOGsFN2gLFbEiP8mpcB5dmbEKOWcHU/PEwQGU828=; b=X/P/FPzjJZMDIf3h3BwRPBAMsXfm1sC5l7qKRTMq9dEahGrdluvG3X1d/tuXATi6WR rcHzoMaVDqSS2a9ZUSgBDTZs2DLeeLmvl34t+B21sFInwlDz3mr/ufATcSvs1x6qFux9 i3N4HxI/3EDM/+jVkV68y3+t7t9L6j/rlbjIE42RzFZj01Goo7hPJm/s/bTq3LoyPONM DcNmCb+YyuLfGe5pI719LzrgJbDg09fZMFeqQLlbC2E3aLshvjcPlOV5UCoFDUcHr5qD MGAEpydt0+PCTaw+w+qy3B+qqkIerTxcKS1W49uJqNXefsQylYEnCVj0WzIreCe34Xry DI4A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r17si1041006ilj.23.2021.08.18.14.23.17; Wed, 18 Aug 2021 14:23:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233920AbhHRVWk (ORCPT + 99 others); Wed, 18 Aug 2021 17:22:40 -0400 Received: from smtp05.smtpout.orange.fr ([80.12.242.127]:60361 "EHLO smtp.smtpout.orange.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229531AbhHRVWj (ORCPT ); Wed, 18 Aug 2021 17:22:39 -0400 Received: from pop-os.home ([90.126.253.178]) by mwinf5d40 with ME id j9N1250013riaq2039N1R2; Wed, 18 Aug 2021 23:22:02 +0200 X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Wed, 18 Aug 2021 23:22:02 +0200 X-ME-IP: 90.126.253.178 From: Christophe JAILLET To: leoyang.li@nxp.com Cc: linuxppc-dev@lists.ozlabs.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET Subject: [PATCH] soc: fsl: guts: Fix a resource leak in the error handling path of 'fsl_guts_probe()' Date: Wed, 18 Aug 2021 23:21:59 +0200 Message-Id: X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If an error occurs after 'of_find_node_by_path()', the reference taken for 'root' will never be released and some memory will leak. Instead of adding an error handling path and modifying all the 'return -SOMETHING' into 'goto errorpath', use 'devm_add_action_or_reset()' to release the reference when needed. Simplify the remove function accordingly. As an extra benefit, the 'root' global variable can now be removed as well. Fixes: 3c0d64e867ed ("soc: fsl: guts: reuse machine name from device tree") Signed-off-by: Christophe JAILLET --- Compile tested only --- drivers/soc/fsl/guts.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/drivers/soc/fsl/guts.c b/drivers/soc/fsl/guts.c index d5e9a5f2c087..4d9476c7b87c 100644 --- a/drivers/soc/fsl/guts.c +++ b/drivers/soc/fsl/guts.c @@ -28,7 +28,6 @@ struct fsl_soc_die_attr { static struct guts *guts; static struct soc_device_attribute soc_dev_attr; static struct soc_device *soc_dev; -static struct device_node *root; /* SoC die attribute definition for QorIQ platform */ @@ -136,14 +135,23 @@ static u32 fsl_guts_get_svr(void) return svr; } +static void fsl_guts_put_root(void *data) +{ + struct device_node *root = data; + + of_node_put(root); +} + static int fsl_guts_probe(struct platform_device *pdev) { struct device_node *np = pdev->dev.of_node; struct device *dev = &pdev->dev; + struct device_node *root; struct resource *res; const struct fsl_soc_die_attr *soc_die; const char *machine; u32 svr; + int ret; /* Initialize guts */ guts = devm_kzalloc(dev, sizeof(*guts), GFP_KERNEL); @@ -159,6 +167,10 @@ static int fsl_guts_probe(struct platform_device *pdev) /* Register soc device */ root = of_find_node_by_path("/"); + ret = devm_add_action_or_reset(dev, fsl_guts_put_root, root); + if (ret) + return ret; + if (of_property_read_string(root, "model", &machine)) of_property_read_string_index(root, "compatible", 0, &machine); if (machine) @@ -197,7 +209,7 @@ static int fsl_guts_probe(struct platform_device *pdev) static int fsl_guts_remove(struct platform_device *dev) { soc_device_unregister(soc_dev); - of_node_put(root); + return 0; } -- 2.30.2