Commit 053ca37c authored by Bjorn Helgaas's avatar Bjorn Helgaas

PCI: j721e: Initialize pcie->cdns_pcie before using it

Christian reported a NULL pointer dereference in j721e_pcie_probe() caused
by 19e86382 ("PCI: j721e: Drop redundant struct device *"), which
removed struct j721e_pcie.dev since there's another copy in struct
cdns_pcie.dev reachable via j721e_pcie->cdns_pcie->dev.

The problem is that j721e_pcie->cdns_pcie was dereferenced before being
initialized:

  j721e_pcie_probe
    pcie = devm_kzalloc()             # struct j721e_pcie
    j721e_pcie_ctrl_init(pcie)
      dev = pcie->cdns_pcie->dev      <-- dereference cdns_pcie
    switch (mode) {
    case PCI_MODE_RC:
      cdns_pcie = ...                 # alloc as part of pci_host_bridge
      pcie->cdns_pcie = cdns_pcie     <-- initialize pcie->cdns_pcie

Move the cdns_pcie initialization earlier so it is done before it is used.
This also simplifies the error exits.

Fixes: 19e86382 ("PCI: j721e: Drop redundant struct device *")
Link: https://lore.kernel.org/r/20220127222951.GA144828@bhelgaas
Link: https://lore.kernel.org/r/20220124122132.435743-1-christian.gmeiner@gmail.comReported-by: default avatarChristian Gmeiner <christian.gmeiner@gmail.com>
Tested-by: default avatarChristian Gmeiner <christian.gmeiner@gmail.com>
Signed-off-by: default avatarBjorn Helgaas <bhelgaas@google.com>
parent e783362e
...@@ -356,8 +356,8 @@ static int j721e_pcie_probe(struct platform_device *pdev) ...@@ -356,8 +356,8 @@ static int j721e_pcie_probe(struct platform_device *pdev)
const struct j721e_pcie_data *data; const struct j721e_pcie_data *data;
struct cdns_pcie *cdns_pcie; struct cdns_pcie *cdns_pcie;
struct j721e_pcie *pcie; struct j721e_pcie *pcie;
struct cdns_pcie_rc *rc; struct cdns_pcie_rc *rc = NULL;
struct cdns_pcie_ep *ep; struct cdns_pcie_ep *ep = NULL;
struct gpio_desc *gpiod; struct gpio_desc *gpiod;
void __iomem *base; void __iomem *base;
struct clk *clk; struct clk *clk;
...@@ -376,6 +376,46 @@ static int j721e_pcie_probe(struct platform_device *pdev) ...@@ -376,6 +376,46 @@ static int j721e_pcie_probe(struct platform_device *pdev)
if (!pcie) if (!pcie)
return -ENOMEM; return -ENOMEM;
switch (mode) {
case PCI_MODE_RC:
if (!IS_ENABLED(CONFIG_PCIE_CADENCE_HOST))
return -ENODEV;
bridge = devm_pci_alloc_host_bridge(dev, sizeof(*rc));
if (!bridge)
return -ENOMEM;
if (!data->byte_access_allowed)
bridge->ops = &cdns_ti_pcie_host_ops;
rc = pci_host_bridge_priv(bridge);
rc->quirk_retrain_flag = data->quirk_retrain_flag;
rc->quirk_detect_quiet_flag = data->quirk_detect_quiet_flag;
cdns_pcie = &rc->pcie;
cdns_pcie->dev = dev;
cdns_pcie->ops = &j721e_pcie_ops;
pcie->cdns_pcie = cdns_pcie;
break;
case PCI_MODE_EP:
if (!IS_ENABLED(CONFIG_PCIE_CADENCE_EP))
return -ENODEV;
ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
if (!ep)
return -ENOMEM;
ep->quirk_detect_quiet_flag = data->quirk_detect_quiet_flag;
cdns_pcie = &ep->pcie;
cdns_pcie->dev = dev;
cdns_pcie->ops = &j721e_pcie_ops;
pcie->cdns_pcie = cdns_pcie;
break;
default:
dev_err(dev, "INVALID device type %d\n", mode);
return 0;
}
pcie->mode = mode; pcie->mode = mode;
pcie->linkdown_irq_regfield = data->linkdown_irq_regfield; pcie->linkdown_irq_regfield = data->linkdown_irq_regfield;
...@@ -426,28 +466,6 @@ static int j721e_pcie_probe(struct platform_device *pdev) ...@@ -426,28 +466,6 @@ static int j721e_pcie_probe(struct platform_device *pdev)
switch (mode) { switch (mode) {
case PCI_MODE_RC: case PCI_MODE_RC:
if (!IS_ENABLED(CONFIG_PCIE_CADENCE_HOST)) {
ret = -ENODEV;
goto err_get_sync;
}
bridge = devm_pci_alloc_host_bridge(dev, sizeof(*rc));
if (!bridge) {
ret = -ENOMEM;
goto err_get_sync;
}
if (!data->byte_access_allowed)
bridge->ops = &cdns_ti_pcie_host_ops;
rc = pci_host_bridge_priv(bridge);
rc->quirk_retrain_flag = data->quirk_retrain_flag;
rc->quirk_detect_quiet_flag = data->quirk_detect_quiet_flag;
cdns_pcie = &rc->pcie;
cdns_pcie->dev = dev;
cdns_pcie->ops = &j721e_pcie_ops;
pcie->cdns_pcie = cdns_pcie;
gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
if (IS_ERR(gpiod)) { if (IS_ERR(gpiod)) {
ret = PTR_ERR(gpiod); ret = PTR_ERR(gpiod);
...@@ -497,23 +515,6 @@ static int j721e_pcie_probe(struct platform_device *pdev) ...@@ -497,23 +515,6 @@ static int j721e_pcie_probe(struct platform_device *pdev)
break; break;
case PCI_MODE_EP: case PCI_MODE_EP:
if (!IS_ENABLED(CONFIG_PCIE_CADENCE_EP)) {
ret = -ENODEV;
goto err_get_sync;
}
ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
if (!ep) {
ret = -ENOMEM;
goto err_get_sync;
}
ep->quirk_detect_quiet_flag = data->quirk_detect_quiet_flag;
cdns_pcie = &ep->pcie;
cdns_pcie->dev = dev;
cdns_pcie->ops = &j721e_pcie_ops;
pcie->cdns_pcie = cdns_pcie;
ret = cdns_pcie_init_phy(dev, cdns_pcie); ret = cdns_pcie_init_phy(dev, cdns_pcie);
if (ret) { if (ret) {
dev_err(dev, "Failed to init phy\n"); dev_err(dev, "Failed to init phy\n");
...@@ -525,8 +526,6 @@ static int j721e_pcie_probe(struct platform_device *pdev) ...@@ -525,8 +526,6 @@ static int j721e_pcie_probe(struct platform_device *pdev)
goto err_pcie_setup; goto err_pcie_setup;
break; break;
default:
dev_err(dev, "INVALID device type %d\n", mode);
} }
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment