Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp6021982ybn; Sun, 29 Sep 2019 10:37:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqzVhOZza+I2GG/pgk63/vOTqIocXUXTpQasShoyfudtkYFkn67wBeOD4NW45d01VW28IlQx X-Received: by 2002:a17:906:2ec8:: with SMTP id s8mr16619779eji.275.1569778653992; Sun, 29 Sep 2019 10:37:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569778653; cv=none; d=google.com; s=arc-20160816; b=iUeg20Y2ZYsmrRp0FYnZB6aDiVEOxhUw4yI1YjORwbVRmMxOoOGBr3wTu2f/zWV1EK TheWFk9rFu+xD9RTlHUD5Fu2SrRiUQ6AoBBjIvjef8LpQTrCqg5rGUI0Otr7ek+mTwgw HkwYd8bjUA+hzvVItbfe5OZnseoAG5XbUsQ+qz2isLopHgpzop7kVJsiKxw2Fd9BQXoE ocaAUrLwnIMPUsTvUKGfdrcvXCetC2J4KANeKxjm3SMs/XlOZaTg95W7eyG7UwEv/F9u Qvy51kYMSOlxuCdXXVluWoABwKoU2mSOHKlYNUQqUZGUucN5hZ6GOAtTWlhZ+H8KJr1U Sjiw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=mtXTgbTOuX/EsjCC1CVPgz+fP9EEFH/m40hrfTIGT9E=; b=SzwPJ2qwbIs/bEDuS0uxudgG7+A1RJZaGC2Epf3cFLYaWTN8P6K5eLYZebQCpapBA2 bgmV4i1Jlb+IOJ58akmSArFNU+n8Hy6HOBdM6C/lAJ23VqlgU/PIC1gGDV5gc0hCeqj7 3/6lfgzyHPQteCN92pXWfMKJPAfQrDZRv+e3wgm38e7BDP4+ARGFgP8MfxGD3V4y00eq nFFHFdDt8i+P4zERm1m1uWe5ah9am/uB4RXA7aWw66v3SdXECaNtQFfI/GWgEKy2w8XZ nJm8IwL4VZg4JPKHXLDqcJYDq/YA+H8WVgxJZ3JHudi6pP/TgCsvSQf+6/vtjA3JZ3fj iQoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=JRbw39HC; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m10si5451563eds.428.2019.09.29.10.37.10; Sun, 29 Sep 2019 10:37:33 -0700 (PDT) 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; dkim=pass header.i=@kernel.org header.s=default header.b=JRbw39HC; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730427AbfI2Rep (ORCPT + 99 others); Sun, 29 Sep 2019 13:34:45 -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: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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