Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp5326960imu; Wed, 26 Dec 2018 00:21:49 -0800 (PST) X-Google-Smtp-Source: ALg8bN4L683FjskHa3qBWjlbMu1qrskAV0Sll2UvJAFf97KtrwX51HY18bPaJ1yJfLcEwuQb2XRS X-Received: by 2002:a17:902:15a8:: with SMTP id m37mr19141990pla.129.1545812509014; Wed, 26 Dec 2018 00:21:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545812508; cv=none; d=google.com; s=arc-20160816; b=scRN9QbutzRVdKo+AURq7c3VSsvlSeIRlq0FeGn4RJj/kJjzPNZTlPtpj4jPEYuQ7O Aj9twGCKwjUpCmovL2ZewR9WFceL2lqbmoW3Cs60FG3MTXDY2BMUzc+5gTQ/hW/Shq5G 01ek0QY11wc/1W9g72l/2velE9Z09bTwtouFydnvXeGh50xGS7tR3Pi9w6F4+6Z1s9jS YriKXNtDg4udxXBtMecA6Hi1vBtrdkTQ4duqIYMJzYhLGQG6sbzmmPFBG+W+uFOXcUyj SRj02j67Q3bHBp5DZYG6wfLTS4yIFps7yMyH6T1WyiySkbBORhIE2nP3B5aIpk4H2Yob 2rFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=WbMedAxAt/0UtubocTfbB4q11fjZgKuWqf8h2OGv9cM=; b=09uoYjVIHce3+H8k03d74gYfOD5UxC/qIIAJAZKoM4nFB4FkzFa5J47WdvVJTokmof mtJmfydc2kF/yaw1Z6ZwEacNEnacNVdep3u0HASaZon2qgRu7lLmZMAkEFcjPPB/lyUg 8xeVULSU2UZp0XwdTPrnj99vyNfM20hnMOH++bd4+qZ4wc0KpExsXbdU/QZ+cz9tuiLl uPIkUDy6ILcaO+QEEdPbywetFdJ8qpnPZpPaXmi5Dp0qtZ/h1FJlbwWfTnKfBXzgZlg6 Mgu3H8TddbbPRttWXIAxwsC1+hXkgNALr2leiciEWGrkMdtTypNo5bqR2mFuMXc1eYnL uKpA== 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 b7si10391458plb.234.2018.12.26.00.21.33; Wed, 26 Dec 2018 00:21: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 S1726052AbeLZIFt (ORCPT + 99 others); Wed, 26 Dec 2018 03:05:49 -0500 Received: from mxhk.zte.com.cn ([63.217.80.70]:14106 "EHLO mxhk.zte.com.cn" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725955AbeLZIFt (ORCPT ); Wed, 26 Dec 2018 03:05:49 -0500 Received: from mse01.zte.com.cn (unknown [10.30.3.20]) by Forcepoint Email with ESMTPS id 20545F00A1964C612F03; Wed, 26 Dec 2018 16:05:46 +0800 (CST) Received: from notes_smtp.zte.com.cn ([10.30.1.239]) by mse01.zte.com.cn with ESMTP id wBQ85YYN028016; Wed, 26 Dec 2018 16:05:34 +0800 (GMT-8) (envelope-from peng.hao2@zte.com.cn) Received: from localhost.localdomain.localdomain ([10.74.120.59]) by szsmtp06.zte.com.cn (Lotus Domino Release 8.5.3FP6) with ESMTP id 2018122616055657-18869967 ; Wed, 26 Dec 2018 16:05:56 +0800 From: Peng Hao To: qiang.zhao@nxp.com, leoyang.li@nxp.com Cc: linux-kernel@vger.kernel.org, Wen Yang , Julia Lawall , netdev@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH v3] soc/fsl/qe: fix err handling of ucc_of_parse_tdm Date: Wed, 26 Dec 2018 16:26:29 +0800 Message-Id: <1545812789-77690-1-git-send-email-peng.hao2@zte.com.cn> X-Mailer: git-send-email 1.8.3.1 X-MIMETrack: Itemize by SMTP Server on SZSMTP06/server/zte_ltd(Release 8.5.3FP6|November 21, 2013) at 2018-12-26 16:05:56, Serialize by Router on notes_smtp/zte_ltd(Release 9.0.1FP7|August 17, 2016) at 2018-12-26 16:05:26, Serialize complete at 2018-12-26 16:05:26 X-MAIL: mse01.zte.com.cn wBQ85YYN028016 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wen Yang Currently there are some issues with the ucc_of_parse_tdm function: 1, a possible null pointer dereference in ucc_of_parse_tdm, detected by the semantic patch deref_null.cocci, with the following warning: drivers/soc/fsl/qe/qe_tdm.c:177:21-24: ERROR: pdev is NULL but dereferenced. 2, dev gets modified, so in any case that devm_iounmap() will fail even when the new pdev is valid, because the iomap was done with a different pdev. 3, there is no driver bind with the "fsl,t1040-qe-si" or "fsl,t1040-qe-siram" device. So allocating resources using devm_*() with these devices won't provide a cleanup path for these resources when the caller fails. This patch fixes them. Suggested-by: Li Yang Suggested-by: Christophe LEROY Signed-off-by: Wen Yang Reviewed-by: Peng Hao CC: Julia Lawall CC: Zhao Qiang CC: netdev@vger.kernel.org CC: linuxppc-dev@lists.ozlabs.org CC: linux-kernel@vger.kernel.org --- drivers/net/wan/fsl_ucc_hdlc.c | 48 +++++++++++++++++++++++++++++++++++- drivers/soc/fsl/qe/qe_tdm.c | 55 ------------------------------------------ 2 files changed, 47 insertions(+), 56 deletions(-) diff --git a/drivers/net/wan/fsl_ucc_hdlc.c b/drivers/net/wan/fsl_ucc_hdlc.c index 839fa77..8ce4921f 100644 --- a/drivers/net/wan/fsl_ucc_hdlc.c +++ b/drivers/net/wan/fsl_ucc_hdlc.c @@ -1057,6 +1057,27 @@ static const struct net_device_ops uhdlc_ops = { .ndo_tx_timeout = uhdlc_tx_timeout, }; +static int ucc_get_resource_by_nodename(char *name, struct resource *res) +{ + struct device_node *np; + struct platform_device *pdev; + + np = of_find_compatible_node(NULL, NULL, name); + if (!np) + return -EINVAL; + + pdev = of_find_device_by_node(np); + if (!pdev) { + pr_err("%pOFn: failed to lookup pdev\n", np); + of_node_put(np); + return -EINVAL; + } + + of_node_put(np); + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + return 0; +} + static int ucc_hdlc_probe(struct platform_device *pdev) { struct device_node *np = pdev->dev.of_node; @@ -1070,6 +1091,7 @@ static int ucc_hdlc_probe(struct platform_device *pdev) const char *sprop; int ret; u32 val; + static int siram_init_flag; ret = of_property_read_u32_index(np, "cell-index", 0, &val); if (ret) { @@ -1151,6 +1173,27 @@ static int ucc_hdlc_probe(struct platform_device *pdev) ret = ucc_of_parse_tdm(np, utdm, ut_info); if (ret) goto free_utdm; + + ret = ucc_get_resource_by_nodename("fsl,t1040-qe-si", &res); + if (ret) + goto free_utdm; + utdm->si_regs = ioremap(res.start, resource_size(&res)); + if (!utdm->si_regs) { + ret = -ENOMEM; + goto free_utdm; + } + ret = ucc_get_resource_by_nodename("fsl,t1040-qe-siram", &res); + if (ret) + goto unmap_si_regs; + utdm->siram = ioremap(res.start, resource_size(&res)); + if (!utdm->siram) { + ret = -ENOMEM; + goto unmap_si_regs; + } + if (siram_init_flag == 0) { + memset_io(utdm->siram, 0, resource_size(&res)); + siram_init_flag = 1; + } } if (of_property_read_u16(np, "fsl,hmask", &uhdlc_priv->hmask)) @@ -1159,7 +1202,7 @@ static int ucc_hdlc_probe(struct platform_device *pdev) ret = uhdlc_init(uhdlc_priv); if (ret) { dev_err(&pdev->dev, "Failed to init uhdlc\n"); - goto free_utdm; + goto undo_uhdlc_init; } dev = alloc_hdlcdev(uhdlc_priv); @@ -1188,6 +1231,9 @@ static int ucc_hdlc_probe(struct platform_device *pdev) free_dev: free_netdev(dev); undo_uhdlc_init: + iounmap(utdm->siram); +unmap_si_regs: + iounmap(utdm->si_regs); free_utdm: if (uhdlc_priv->tsa) kfree(utdm); diff --git a/drivers/soc/fsl/qe/qe_tdm.c b/drivers/soc/fsl/qe/qe_tdm.c index f78c346..76480df 100644 --- a/drivers/soc/fsl/qe/qe_tdm.c +++ b/drivers/soc/fsl/qe/qe_tdm.c @@ -44,10 +44,6 @@ int ucc_of_parse_tdm(struct device_node *np, struct ucc_tdm *utdm, const char *sprop; int ret = 0; u32 val; - struct resource *res; - struct device_node *np2; - static int siram_init_flag; - struct platform_device *pdev; sprop = of_get_property(np, "fsl,rx-sync-clock", NULL); if (sprop) { @@ -124,57 +120,6 @@ int ucc_of_parse_tdm(struct device_node *np, struct ucc_tdm *utdm, utdm->siram_entry_id = val; set_si_param(utdm, ut_info); - - np2 = of_find_compatible_node(NULL, NULL, "fsl,t1040-qe-si"); - if (!np2) - return -EINVAL; - - pdev = of_find_device_by_node(np2); - if (!pdev) { - pr_err("%pOFn: failed to lookup pdev\n", np2); - of_node_put(np2); - return -EINVAL; - } - - of_node_put(np2); - res = platform_get_resource(pdev, IORESOURCE_MEM, 0); - utdm->si_regs = devm_ioremap_resource(&pdev->dev, res); - if (IS_ERR(utdm->si_regs)) { - ret = PTR_ERR(utdm->si_regs); - goto err_miss_siram_property; - } - - np2 = of_find_compatible_node(NULL, NULL, "fsl,t1040-qe-siram"); - if (!np2) { - ret = -EINVAL; - goto err_miss_siram_property; - } - - pdev = of_find_device_by_node(np2); - if (!pdev) { - ret = -EINVAL; - pr_err("%pOFn: failed to lookup pdev\n", np2); - of_node_put(np2); - goto err_miss_siram_property; - } - - of_node_put(np2); - res = platform_get_resource(pdev, IORESOURCE_MEM, 0); - utdm->siram = devm_ioremap_resource(&pdev->dev, res); - if (IS_ERR(utdm->siram)) { - ret = PTR_ERR(utdm->siram); - goto err_miss_siram_property; - } - - if (siram_init_flag == 0) { - memset_io(utdm->siram, 0, resource_size(res)); - siram_init_flag = 1; - } - - return ret; - -err_miss_siram_property: - devm_iounmap(&pdev->dev, utdm->si_regs); return ret; } EXPORT_SYMBOL(ucc_of_parse_tdm); -- 2.9.5