Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp1169918pxb; Thu, 21 Oct 2021 17:31:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxjNg/czKv2yN5cIw+WX5kqxmQKYh5wdhxi8+X+42dTHqEviST74TJMcvTlsvkDfkJS7cx3 X-Received: by 2002:a50:9fac:: with SMTP id c41mr7145963edf.206.1634862684596; Thu, 21 Oct 2021 17:31:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634862684; cv=none; d=google.com; s=arc-20160816; b=N3zFxTXBcC2keyi7xPI9c6YYgOXMfFcJF19caFJdKmaa8IrcaEaiI3004lHl58xzAj NvTc/WV1O9idHYavpjhlI4DilLlpXVN8I5QFfmhzQNTa7a2o0BuuiwYtizSqSRQYmsta mxp3izABd50j0zgzyuXWkbg37OLiv0S+SuZ5qzfajnSsQWOAD/3PpThkm228opB+ToMV vQN8KQ/2vWrr50QZf718vfWS7MWYQKZ9u7BuS0bq6zTXeXnvgihezQ9cpBPnvZuDVwVv 0VaoiDFNBZQH2zfw2ZHrNoxzEuUtU3S2Ax5lXndoSRwNmByFHR+1t1m79peMPIQ66Log FjnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version; bh=HnL8LEaWnSsWA9HfguV8dZWptDtYCS/c4aEJvvgRowc=; b=uDF3Xyv1ElcscngVNEQ4KZiprcBA8I6lY+pI4dM60KPsHgvIdlWo2taQIdVTLFdG0Y IxfSXZxnZ1KHYv+OkW3ZA2psIcoIGBK3Crc8H3ZahUZg4aaB1BgKluWmwU1KvaugoL9z vvCyAnk8LFvljr8wrmMEXOy8xSbSKePNcEggiIG9ZIJHnkibj99bsuGrfrgkGCWtXIQg I1Nle+r41uh0ap1cvVuGil1Wt9Dp6HjuSGusmDkEa+Szy6OEMPhZSje1xEAT3QF5TYpr x6UTC1brqWXo/env3W/ZhuKWiB6UzHMPNzfOzJr7ZFyo/RKbLyn8iVGTBpsGC1JGgAm8 D9jw== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a19si9633602edb.104.2021.10.21.17.31.00; Thu, 21 Oct 2021 17:31:24 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232004AbhJVA3N (ORCPT + 99 others); Thu, 21 Oct 2021 20:29:13 -0400 Received: from mail-qt1-f179.google.com ([209.85.160.179]:43588 "EHLO mail-qt1-f179.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231518AbhJVA3M (ORCPT ); Thu, 21 Oct 2021 20:29:12 -0400 Received: by mail-qt1-f179.google.com with SMTP id r17so2129478qtx.10; Thu, 21 Oct 2021 17:26:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=HnL8LEaWnSsWA9HfguV8dZWptDtYCS/c4aEJvvgRowc=; b=xvZ1aKPlRC2VhsexDgLd6S7Izt5/TLSMUzlNoEYlw1cSf6iq/EVyCQCxiw6OpYpPPU n8Bz9bhmpSLJB0ot10WlWnymUAj/LxHzcVwZExDY7s6fPsVXdOsHbpVf/OidSvv5Hosd oQfimZPjQuaDW8Tdlk8xaxsgx0cE+6vysG6CqynWJgw9yQCfo7AHJt6chnSuol0xBihZ jYYHjpJ5+a3PO5kt3bPacjLV5W1ug67Kgxlp7AeHSCG1YWtNfIfrhQ3ZTO3bkUJt9Ig6 uw7Bfg/IG1TqKB+qUuxSOtJWjnxKTaKwaj5VyIb39KHLdR+ko5NQHT+4OsxKbwR3jqDq uoBw== X-Gm-Message-State: AOAM5334pP8n6haho/+Nb5Xr0Er1BkGhWfdgKe3A9HGUDBSdfcIlVVv7 de2huhOtYL26DRXF6ZNvv16+mUV4oME= X-Received: by 2002:ac8:7d87:: with SMTP id c7mr672070qtd.413.1634862415456; Thu, 21 Oct 2021 17:26:55 -0700 (PDT) Received: from mail-qk1-f178.google.com (mail-qk1-f178.google.com. [209.85.222.178]) by smtp.gmail.com with ESMTPSA id b127sm3312121qkg.42.2021.10.21.17.26.54 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 21 Oct 2021 17:26:55 -0700 (PDT) Received: by mail-qk1-f178.google.com with SMTP id bp7so3035451qkb.12; Thu, 21 Oct 2021 17:26:54 -0700 (PDT) X-Received: by 2002:a37:bd7:: with SMTP id 206mr7129325qkl.297.1634862414371; Thu, 21 Oct 2021 17:26:54 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Li Yang Date: Thu, 21 Oct 2021 19:26:42 -0500 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] soc: fsl: guts: Fix a resource leak in the error handling path of 'fsl_guts_probe()' To: Christophe JAILLET Cc: linuxppc-dev , "moderated list:ARM/FREESCALE IMX / MXC ARM ARCHITECTURE" , lkml , kernel-janitors@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Aug 18, 2021 at 4:23 PM Christophe JAILLET wrote: > > If an error occurs after 'of_find_node_by_path()', the reference taken for > 'root' will never be released and some memory will leak. Thanks for finding this. This truly is a problem. > > 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; We probably only need to hold the reference when we do get "machine" from the device tree, otherwise we can put it directly. Or maybe we just maintain a local copy of string machine which means we can release the reference right away? > + > 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 >