Received: by 2002:ac0:8845:0:0:0:0:0 with SMTP id g63csp2239804img; Wed, 27 Feb 2019 12:57:48 -0800 (PST) X-Google-Smtp-Source: AHgI3IbOQI4SccJ1lrivHnn994n0TVvnmHdMFjyuGfKJemU+DjYP9teMaDXrbsF8mEgcKbItwjpM X-Received: by 2002:a63:1945:: with SMTP id 5mr4030541pgz.412.1551301068697; Wed, 27 Feb 2019 12:57:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551301068; cv=none; d=google.com; s=arc-20160816; b=icCj5TaJsoVpE4ddFs/+9kJee3xs+cNbNlDfsuGbKB3/x1KpO0x3x5xO/biPsFq0DM pHuEJ7RQ2AiSkaOkfAP0C4nhes23bJZ6jjyzEPatA1bhZKayF1PKoYhU9ZydWJ/VdPCS 5vKLPdIrLHEla6INv5/U54ze0S3z7UN13mCHkrhwBkW1A4fFU5RMgAWt01cuYomw7ktn Cf6ZU7oaHyoGmGHF/qi+Up3BVTE7TundbZw2qYd70H+hQSItZlEQkSwKO1NZWTy9lpFg n8/kJCLFwtbBD1lQ24AULuKgvkGt5be+UMBfYT8+mf9tfw5uE+LczrDhdzhyZ9LyhdYU Q2GA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date; bh=XXlYqHQ96Wy+AV4T8EoAqWrBKx5ucArT7JsRIcYWYtA=; b=MzqGxpkVJIl2VqzfTbvicUTt7ZU+LP/l7eaArNnUqseVifG+FK7YQQ7V1EsVtnEUio ktCFt87bW8TeohdgTmRlD9W7R3BnvHXqVx1Mk9h7DrWvkZhKzGDJOOFKMOqfCFVaniDZ QQnSCoCyX88zsGClSLwgemnXXg5AEOK0yDIy/xvWuVS0l1io+zGYVtmTAOScdWgPsCo6 hX8CjwqBP32OZDElsUKIX/xr3m0NUdF63sT9/2tPLVCeCKuKotzx+XJsoKKuQnIbxfKo CK5EJ2+ryM5EgCbPVzrxqIcObM4uPtSHtJHsOXWTBtvISDPNvgD2CMtPVuoQSB/lqP3U w8rA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k62si15692817pge.236.2019.02.27.12.57.33; Wed, 27 Feb 2019 12:57:48 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730367AbfB0Uz6 (ORCPT + 99 others); Wed, 27 Feb 2019 15:55:58 -0500 Received: from mail2-relais-roc.national.inria.fr ([192.134.164.83]:33840 "EHLO mail2-relais-roc.national.inria.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727488AbfB0Uz5 (ORCPT ); Wed, 27 Feb 2019 15:55:57 -0500 X-IronPort-AV: E=Sophos;i="5.58,420,1544482800"; d="scan'208";a="371210492" Received: from abo-58-107-68.mrs.modulonet.fr (HELO hadrien) ([85.68.107.58]) by mail2-relais-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 27 Feb 2019 21:55:52 +0100 Date: Wed, 27 Feb 2019 21:55:52 +0100 (CET) From: Julia Lawall X-X-Sender: jll@hadrien To: Yoshihiro Shimoda cc: "kishon@ti.com" , "linux-kernel@vger.kernel.org" , "linux-renesas-soc@vger.kernel.org" Subject: RE: [PATCH] phy: renesas: rcar-gen2: Fix memory leak at error paths In-Reply-To: Message-ID: References: <1551250826-10302-1-git-send-email-yoshihiro.shimoda.uh@renesas.com> User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 27 Feb 2019, Yoshihiro Shimoda wrote: > Hello, > > > From: Julia Lawall, Sent: Wednesday, February 27, 2019 5:25 PM > > > > On Wed, 27 Feb 2019, Yoshihiro Shimoda wrote: > > > > > This patch fixes memory leak at error paths of the probe function. > > > In for_each_child_of_node, if the loop returns, the driver should > > > call of_put_node() before returns. > > > > > > Reported-by: Julia Lawall > > > Fixes: 1233f59f745 ("phy: Renesas R-Car Gen2 PHY driver") > > > Signed-off-by: Yoshihiro Shimoda > > > --- > > > drivers/phy/renesas/phy-rcar-gen2.c | 2 ++ > > > 1 file changed, 2 insertions(+) > > > > > > diff --git a/drivers/phy/renesas/phy-rcar-gen2.c b/drivers/phy/renesas/phy-rcar-gen2.c > > > index 72eeb06..570b4e4 100644 > > > --- a/drivers/phy/renesas/phy-rcar-gen2.c > > > +++ b/drivers/phy/renesas/phy-rcar-gen2.c > > > @@ -285,6 +285,7 @@ static int rcar_gen2_phy_probe(struct platform_device *pdev) > > > error = of_property_read_u32(np, "reg", &channel_num); > > > if (error || channel_num > 2) { > > > dev_err(dev, "Invalid \"reg\" property\n"); > > > + of_node_put(np); > > > return error; > > > } > > > channel->select_mask = select_mask[channel_num]; > > > @@ -300,6 +301,7 @@ static int rcar_gen2_phy_probe(struct platform_device *pdev) > > > &rcar_gen2_phy_ops); > > > if (IS_ERR(phy->phy)) { > > > dev_err(dev, "Failed to create PHY\n"); > > > + of_node_put(np); > > > return PTR_ERR(phy->phy); > > > } > > > phy_set_drvdata(phy->phy, phy); > > > > Hello, > > > > I was concerned about the assignment channel->of_node = np;. Because > > channels is allocated with a devm function, it will get freed on an error > > return, so this pointer doesn't matter. But don't you need an of_node_get > > on this assignment? Does the fact that you haven't seen a problem with > > this in testing mean that the field is actually never accessed? > > The channel->of_node will be used in the rcar_gen2_phy_xlate() as drv->channels[i].of_node. > The assignment is not used for any device tree APIs, just comparing the pointer. > So, I don't think this driver needs an of_node_get() on this assignment. > Is my understanding incorrect? I see that the value is only used for comparison. Thanks for pointing that out. If you are sure that the pointers are the same when the function is called as when the pointer to the possibly now freed device node was stored in the channels structure, then everything should be ok. julia > > --- > static struct phy *rcar_gen2_phy_xlate(struct device *dev, > struct of_phandle_args *args) > { > struct rcar_gen2_phy_driver *drv; > struct device_node *np = args->np; > int i; > > drv = dev_get_drvdata(dev); > if (!drv) > return ERR_PTR(-EINVAL); > > for (i = 0; i < drv->num_channels; i++) { > if (np == drv->channels[i].of_node) // <--- here only > break; > } > > if (i >= drv->num_channels || args->args[0] >= 2) > return ERR_PTR(-ENODEV); > > return drv->channels[i].phys[args->args[0]].phy; > } > --- > > Best regards, > Yoshihiro Shimoda > > > julia >