Skip to content

Commit 9e38e69

Browse files
gaurijhangianiLorenzo Pieralisi
authored andcommitted
PCI: tegra: Fix OF node reference leak
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 <nishkadg.linux@gmail.com> [lorenzo.pieralisi@arm.com: rewrote commit log] Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
1 parent 5f9e832 commit 9e38e69

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

‎drivers/pci/controller/pci-tegra.c‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,14 +2237,15 @@ static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
22372237
err = of_pci_get_devfn(port);
22382238
if (err < 0) {
22392239
dev_err(dev, "failed to parse address: %d\n", err);
2240-
return err;
2240+
goto err_node_put;
22412241
}
22422242

22432243
index = PCI_SLOT(err);
22442244

22452245
if (index < 1 || index > soc->num_ports) {
22462246
dev_err(dev, "invalid port number: %d\n", index);
2247-
return -EINVAL;
2247+
err = -EINVAL;
2248+
goto err_node_put;
22482249
}
22492250

22502251
index--;
@@ -2253,12 +2254,13 @@ static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
22532254
if (err < 0) {
22542255
dev_err(dev, "failed to parse # of lanes: %d\n",
22552256
err);
2256-
return err;
2257+
goto err_node_put;
22572258
}
22582259

22592260
if (value > 16) {
22602261
dev_err(dev, "invalid # of lanes: %u\n", value);
2261-
return -EINVAL;
2262+
err = -EINVAL;
2263+
goto err_node_put;
22622264
}
22632265

22642266
lanes |= value << (index << 3);
@@ -2272,13 +2274,15 @@ static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
22722274
lane += value;
22732275

22742276
rp = devm_kzalloc(dev, sizeof(*rp), GFP_KERNEL);
2275-
if (!rp)
2276-
return -ENOMEM;
2277+
if (!rp) {
2278+
err = -ENOMEM;
2279+
goto err_node_put;
2280+
}
22772281

22782282
err = of_address_to_resource(port, 0, &rp->regs);
22792283
if (err < 0) {
22802284
dev_err(dev, "failed to parse address: %d\n", err);
2281-
return err;
2285+
goto err_node_put;
22822286
}
22832287

22842288
INIT_LIST_HEAD(&rp->list);
@@ -2330,6 +2334,10 @@ static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
23302334
return err;
23312335

23322336
return 0;
2337+
2338+
err_node_put:
2339+
of_node_put(port);
2340+
return err;
23332341
}
23342342

23352343
/*

0 commit comments

Comments
 (0)