From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1DCC3C4360C for ; Sun, 29 Sep 2019 17:34:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id EA10421D56 for ; Sun, 29 Sep 2019 17:34:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1569778480; bh=E69jPBhERz8zrUisDraYKLSEfmvI3riaLDoldYjcMW4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=mu1yCk7evMZXiE2tFzw3iofeQGDKpgL2IQE+fLsJ1rmG1E5kSOJ3utjDzY9cEG2o8 KB83O1yaVx4FKIs9aXAo8/4HZrVZJkG49BdCe4t6H6ycf/a3DL6+CuAt9imCmLTxc0 YHP1jS5SA8/kriKVhKjEp/pbbBuPUO10BeBxCYcg= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730406AbfI2Rei (ORCPT ); Sun, 29 Sep 2019 13:34:38 -0400 Received: from mail.kernel.org ([198.145.29.99]:46644 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730403AbfI2Reh (ORCPT ); Sun, 29 Sep 2019 13:34:37 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 61C4221928; Sun, 29 Sep 2019 17:34:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1569778477; bh=E69jPBhERz8zrUisDraYKLSEfmvI3riaLDoldYjcMW4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JRbw39HCNLXllpG7UsQaOE3jEvFPx4qibskLstarVNVZQqiLIZCUNMq3oiOIjau8p sXTXj5FuWVCDn3Vpg7MNZUiLdBEE2wXRIaA+u9CCfV4m2m8woo8uXB/4Uv0I+BhJ3o g7/P2Y8eoVz+u40eqVFqwSP3uLwrsNg4KllyGW9Y= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Nishka Dasgupta , Lorenzo Pieralisi , Sasha Levin , linux-tegra@vger.kernel.org, linux-pci@vger.kernel.org Subject: [PATCH AUTOSEL 4.19 06/33] PCI: tegra: Fix OF node reference leak Date: Sun, 29 Sep 2019 13:33:54 -0400 Message-Id: <20190929173424.9361-6-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190929173424.9361-1-sashal@kernel.org> References: <20190929173424.9361-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Nishka Dasgupta [ Upstream commit 9e38e690ace3e7a22a81fc02652fc101efb340cf ] Each iteration of for_each_child_of_node() executes of_node_put() on the previous node, but in some return paths in the middle of the loop of_node_put() is missing thus causing a reference leak. Hence stash these mid-loop return values in a variable 'err' and add a new label err_node_put which executes of_node_put() on the previous node and returns 'err' on failure. Change mid-loop return statements to point to jump to this label to fix the reference leak. Issue found with Coccinelle. Signed-off-by: Nishka Dasgupta [lorenzo.pieralisi@arm.com: rewrote commit log] Signed-off-by: Lorenzo Pieralisi Signed-off-by: Sasha Levin --- drivers/pci/controller/pci-tegra.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/drivers/pci/controller/pci-tegra.c b/drivers/pci/controller/pci-tegra.c index f4f53d092e005..976eaa9a9f266 100644 --- a/drivers/pci/controller/pci-tegra.c +++ b/drivers/pci/controller/pci-tegra.c @@ -1975,14 +1975,15 @@ static int tegra_pcie_parse_dt(struct tegra_pcie *pcie) err = of_pci_get_devfn(port); if (err < 0) { dev_err(dev, "failed to parse address: %d\n", err); - return err; + goto err_node_put; } index = PCI_SLOT(err); if (index < 1 || index > soc->num_ports) { dev_err(dev, "invalid port number: %d\n", index); - return -EINVAL; + err = -EINVAL; + goto err_node_put; } index--; @@ -1991,12 +1992,13 @@ static int tegra_pcie_parse_dt(struct tegra_pcie *pcie) if (err < 0) { dev_err(dev, "failed to parse # of lanes: %d\n", err); - return err; + goto err_node_put; } if (value > 16) { dev_err(dev, "invalid # of lanes: %u\n", value); - return -EINVAL; + err = -EINVAL; + goto err_node_put; } lanes |= value << (index << 3); @@ -2010,13 +2012,15 @@ static int tegra_pcie_parse_dt(struct tegra_pcie *pcie) lane += value; rp = devm_kzalloc(dev, sizeof(*rp), GFP_KERNEL); - if (!rp) - return -ENOMEM; + if (!rp) { + err = -ENOMEM; + goto err_node_put; + } err = of_address_to_resource(port, 0, &rp->regs); if (err < 0) { dev_err(dev, "failed to parse address: %d\n", err); - return err; + goto err_node_put; } INIT_LIST_HEAD(&rp->list); @@ -2043,6 +2047,10 @@ static int tegra_pcie_parse_dt(struct tegra_pcie *pcie) return err; return 0; + +err_node_put: + of_node_put(port); + return err; } /* -- 2.20.1