* [PATCH v1 1/2] PCI: of: Fix memory leak when of_changeset_create_node() failed
@ 2023-09-28 16:47 Lizhi Hou
2023-09-28 16:47 ` [PATCH v1 2/2] PCI: of_property: Fix uninitialized variable when of_irq_parse_raw() failed Lizhi Hou
0 siblings, 1 reply; 4+ messages in thread
From: Lizhi Hou @ 2023-09-28 16:47 UTC (permalink / raw)
To: linux-pci, devicetree, linux-kernel, herve.codina,
Jonathan.Cameron
Cc: Lizhi Hou, bhelgaas, robh
Destroy and free cset when failure happens.
Fixes: 407d1a51921e ("PCI: Create device tree node for bridge")
Reported-by: Herve Codina <herve.codina@bootlin.com>
Closes: https://lore.kernel.org/all/20230911171319.495bb837@bootlin.com/
Signed-off-by: Lizhi Hou <lizhi.hou@amd.com>
---
drivers/pci/of.c | 19 +++++++++++--------
1 file changed, 11 insertions(+), 8 deletions(-)
diff --git a/drivers/pci/of.c b/drivers/pci/of.c
index 2af64bcb7da3..51e3dd0ea5ab 100644
--- a/drivers/pci/of.c
+++ b/drivers/pci/of.c
@@ -657,30 +657,33 @@ void of_pci_make_dev_node(struct pci_dev *pdev)
cset = kmalloc(sizeof(*cset), GFP_KERNEL);
if (!cset)
- goto failed;
+ goto out_free_name;
of_changeset_init(cset);
np = of_changeset_create_node(cset, ppnode, name);
if (!np)
- goto failed;
- np->data = cset;
+ goto out_destroy_cset;
ret = of_pci_add_properties(pdev, cset, np);
if (ret)
- goto failed;
+ goto out_free_node;
ret = of_changeset_apply(cset);
if (ret)
- goto failed;
+ goto out_free_node;
+ np->data = cset;
pdev->dev.of_node = np;
kfree(name);
return;
-failed:
- if (np)
- of_node_put(np);
+out_free_node:
+ of_node_put(np);
+out_destroy_cset:
+ of_changeset_destroy(cset);
+ kfree(cset);
+out_free_name:
kfree(name);
}
#endif
--
2.34.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH v1 2/2] PCI: of_property: Fix uninitialized variable when of_irq_parse_raw() failed 2023-09-28 16:47 [PATCH v1 1/2] PCI: of: Fix memory leak when of_changeset_create_node() failed Lizhi Hou @ 2023-09-28 16:47 ` Lizhi Hou 2023-09-29 16:27 ` Bjorn Helgaas 0 siblings, 1 reply; 4+ messages in thread From: Lizhi Hou @ 2023-09-28 16:47 UTC (permalink / raw) To: linux-pci, devicetree, linux-kernel, herve.codina, Jonathan.Cameron Cc: Lizhi Hou, bhelgaas, robh In function of_pci_prop_intr_map(), addr_sz[i] will be uninitialized if of_irq_parse_raw() returns failure. Add addr_sz array initialization. And when parsing irq failed, skip generating interrupt-map pair for the pin. Fixes: 407d1a51921e ("PCI: Create device tree node for bridge") Reported-by: Jonathan Cameron <Jonathan.Cameron@Huawei.com> Closes: https://lore.kernel.org/all/20230911154856.000076c3@Huawei.com/ Reviewed-by: Herve Codina <herve.codina@bootlin.com> Signed-off-by: Lizhi Hou <lizhi.hou@amd.com> --- drivers/pci/of_property.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/drivers/pci/of_property.c b/drivers/pci/of_property.c index 710ec35ba4a1..c2c7334152bc 100644 --- a/drivers/pci/of_property.c +++ b/drivers/pci/of_property.c @@ -186,8 +186,8 @@ static int of_pci_prop_interrupts(struct pci_dev *pdev, static int of_pci_prop_intr_map(struct pci_dev *pdev, struct of_changeset *ocs, struct device_node *np) { + u32 i, addr_sz[OF_PCI_MAX_INT_PIN] = { 0 }, map_sz = 0; struct of_phandle_args out_irq[OF_PCI_MAX_INT_PIN]; - u32 i, addr_sz[OF_PCI_MAX_INT_PIN], map_sz = 0; __be32 laddr[OF_PCI_ADDRESS_CELLS] = { 0 }; u32 int_map_mask[] = { 0xffff00, 0, 0, 7 }; struct device_node *pnode; @@ -213,33 +213,44 @@ static int of_pci_prop_intr_map(struct pci_dev *pdev, struct of_changeset *ocs, out_irq[i].args[0] = pin; ret = of_irq_parse_raw(laddr, &out_irq[i]); if (ret) { - pci_err(pdev, "parse irq %d failed, ret %d", pin, ret); + out_irq[i].np = NULL; + pci_dbg(pdev, "parse irq %d failed, ret %d", pin, ret); continue; } - ret = of_property_read_u32(out_irq[i].np, "#address-cells", - &addr_sz[i]); - if (ret) - addr_sz[i] = 0; + of_property_read_u32(out_irq[i].np, "#address-cells", + &addr_sz[i]); } list_for_each_entry(child, &pdev->subordinate->devices, bus_list) { for (pin = 1; pin <= OF_PCI_MAX_INT_PIN; pin++) { i = pci_swizzle_interrupt_pin(child, pin) - 1; + if (!out_irq[i].np) + continue; map_sz += 5 + addr_sz[i] + out_irq[i].args_count; } } + /* + * Parsing interrupt failed for all pins. In this case, it does not + * need to generate interrupt-map property. + */ + if (!map_sz) + return 0; + int_map = kcalloc(map_sz, sizeof(u32), GFP_KERNEL); mapp = int_map; list_for_each_entry(child, &pdev->subordinate->devices, bus_list) { for (pin = 1; pin <= OF_PCI_MAX_INT_PIN; pin++) { + i = pci_swizzle_interrupt_pin(child, pin) - 1; + if (!out_irq[i].np) + continue; + *mapp = (child->bus->number << 16) | (child->devfn << 8); mapp += OF_PCI_ADDRESS_CELLS; *mapp = pin; mapp++; - i = pci_swizzle_interrupt_pin(child, pin) - 1; *mapp = out_irq[i].np->phandle; mapp++; if (addr_sz[i]) { -- 2.34.1 ^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v1 2/2] PCI: of_property: Fix uninitialized variable when of_irq_parse_raw() failed 2023-09-28 16:47 ` [PATCH v1 2/2] PCI: of_property: Fix uninitialized variable when of_irq_parse_raw() failed Lizhi Hou @ 2023-09-29 16:27 ` Bjorn Helgaas 2023-09-29 16:40 ` Lizhi Hou 0 siblings, 1 reply; 4+ messages in thread From: Bjorn Helgaas @ 2023-09-29 16:27 UTC (permalink / raw) To: Lizhi Hou Cc: linux-pci, devicetree, linux-kernel, herve.codina, Jonathan.Cameron, bhelgaas, robh On Thu, Sep 28, 2023 at 09:47:11AM -0700, Lizhi Hou wrote: > In function of_pci_prop_intr_map(), addr_sz[i] will be uninitialized if > of_irq_parse_raw() returns failure. Add addr_sz array initialization. And > when parsing irq failed, skip generating interrupt-map pair for the pin. Would you mind splitting this into two? It sounds like it fixes two problems: (1) using uninitialized addr_sz[] and (2) skip generating interrupt-map pair. There's also something going on with the pci_swizzle_interrupt_pin() change; maybe that should be a third patch so you can explain what's going on there? IIUC this is actually a *v2* (not a v1), since you posted it previously as https://lore.kernel.org/r/1694801287-17217-2-git-send-email-lizhi.hou@amd.com so the next round should be labeled v3. > Fixes: 407d1a51921e ("PCI: Create device tree node for bridge") > Reported-by: Jonathan Cameron <Jonathan.Cameron@Huawei.com> > Closes: https://lore.kernel.org/all/20230911154856.000076c3@Huawei.com/ > Reviewed-by: Herve Codina <herve.codina@bootlin.com> > Signed-off-by: Lizhi Hou <lizhi.hou@amd.com> > --- > drivers/pci/of_property.c | 25 ++++++++++++++++++------- > 1 file changed, 18 insertions(+), 7 deletions(-) > > diff --git a/drivers/pci/of_property.c b/drivers/pci/of_property.c > index 710ec35ba4a1..c2c7334152bc 100644 > --- a/drivers/pci/of_property.c > +++ b/drivers/pci/of_property.c > @@ -186,8 +186,8 @@ static int of_pci_prop_interrupts(struct pci_dev *pdev, > static int of_pci_prop_intr_map(struct pci_dev *pdev, struct of_changeset *ocs, > struct device_node *np) > { > + u32 i, addr_sz[OF_PCI_MAX_INT_PIN] = { 0 }, map_sz = 0; > struct of_phandle_args out_irq[OF_PCI_MAX_INT_PIN]; > - u32 i, addr_sz[OF_PCI_MAX_INT_PIN], map_sz = 0; > __be32 laddr[OF_PCI_ADDRESS_CELLS] = { 0 }; > u32 int_map_mask[] = { 0xffff00, 0, 0, 7 }; > struct device_node *pnode; > @@ -213,33 +213,44 @@ static int of_pci_prop_intr_map(struct pci_dev *pdev, struct of_changeset *ocs, > out_irq[i].args[0] = pin; > ret = of_irq_parse_raw(laddr, &out_irq[i]); > if (ret) { > - pci_err(pdev, "parse irq %d failed, ret %d", pin, ret); > + out_irq[i].np = NULL; > + pci_dbg(pdev, "parse irq %d failed, ret %d", pin, ret); > continue; > } > - ret = of_property_read_u32(out_irq[i].np, "#address-cells", > - &addr_sz[i]); > - if (ret) > - addr_sz[i] = 0; > + of_property_read_u32(out_irq[i].np, "#address-cells", > + &addr_sz[i]); > } > > list_for_each_entry(child, &pdev->subordinate->devices, bus_list) { > for (pin = 1; pin <= OF_PCI_MAX_INT_PIN; pin++) { > i = pci_swizzle_interrupt_pin(child, pin) - 1; > + if (!out_irq[i].np) > + continue; > map_sz += 5 + addr_sz[i] + out_irq[i].args_count; > } > } > > + /* > + * Parsing interrupt failed for all pins. In this case, it does not > + * need to generate interrupt-map property. > + */ > + if (!map_sz) > + return 0; > + > int_map = kcalloc(map_sz, sizeof(u32), GFP_KERNEL); > mapp = int_map; > > list_for_each_entry(child, &pdev->subordinate->devices, bus_list) { > for (pin = 1; pin <= OF_PCI_MAX_INT_PIN; pin++) { > + i = pci_swizzle_interrupt_pin(child, pin) - 1; > + if (!out_irq[i].np) > + continue; > + > *mapp = (child->bus->number << 16) | > (child->devfn << 8); > mapp += OF_PCI_ADDRESS_CELLS; > *mapp = pin; > mapp++; > - i = pci_swizzle_interrupt_pin(child, pin) - 1; > *mapp = out_irq[i].np->phandle; > mapp++; > if (addr_sz[i]) { > -- > 2.34.1 > ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v1 2/2] PCI: of_property: Fix uninitialized variable when of_irq_parse_raw() failed 2023-09-29 16:27 ` Bjorn Helgaas @ 2023-09-29 16:40 ` Lizhi Hou 0 siblings, 0 replies; 4+ messages in thread From: Lizhi Hou @ 2023-09-29 16:40 UTC (permalink / raw) To: Bjorn Helgaas Cc: linux-pci, devicetree, linux-kernel, herve.codina, Jonathan.Cameron, bhelgaas, robh On 9/29/23 09:27, Bjorn Helgaas wrote: > On Thu, Sep 28, 2023 at 09:47:11AM -0700, Lizhi Hou wrote: >> In function of_pci_prop_intr_map(), addr_sz[i] will be uninitialized if >> of_irq_parse_raw() returns failure. Add addr_sz array initialization. And >> when parsing irq failed, skip generating interrupt-map pair for the pin. > Would you mind splitting this into two? It sounds like it fixes two > problems: (1) using uninitialized addr_sz[] and (2) skip generating > interrupt-map pair. Ok. I will split it into two patches and mark them 'v3'. > > There's also something going on with the pci_swizzle_interrupt_pin() > change; maybe that should be a third patch so you can explain what's > going on there? It is all about to skip the pair which can not be parsed by of_irq_parse_raw(). pci_swizzle_interrupt_pin() is moved to the beginning of the loop to skip the non parsed pin. Thanks, Lizhi > > IIUC this is actually a *v2* (not a v1), since you posted it > previously as > https://lore.kernel.org/r/1694801287-17217-2-git-send-email-lizhi.hou@amd.com > so the next round should be labeled v3. > >> Fixes: 407d1a51921e ("PCI: Create device tree node for bridge") >> Reported-by: Jonathan Cameron <Jonathan.Cameron@Huawei.com> >> Closes: https://lore.kernel.org/all/20230911154856.000076c3@Huawei.com/ >> Reviewed-by: Herve Codina <herve.codina@bootlin.com> >> Signed-off-by: Lizhi Hou <lizhi.hou@amd.com> >> --- >> drivers/pci/of_property.c | 25 ++++++++++++++++++------- >> 1 file changed, 18 insertions(+), 7 deletions(-) >> >> diff --git a/drivers/pci/of_property.c b/drivers/pci/of_property.c >> index 710ec35ba4a1..c2c7334152bc 100644 >> --- a/drivers/pci/of_property.c >> +++ b/drivers/pci/of_property.c >> @@ -186,8 +186,8 @@ static int of_pci_prop_interrupts(struct pci_dev *pdev, >> static int of_pci_prop_intr_map(struct pci_dev *pdev, struct of_changeset *ocs, >> struct device_node *np) >> { >> + u32 i, addr_sz[OF_PCI_MAX_INT_PIN] = { 0 }, map_sz = 0; >> struct of_phandle_args out_irq[OF_PCI_MAX_INT_PIN]; >> - u32 i, addr_sz[OF_PCI_MAX_INT_PIN], map_sz = 0; >> __be32 laddr[OF_PCI_ADDRESS_CELLS] = { 0 }; >> u32 int_map_mask[] = { 0xffff00, 0, 0, 7 }; >> struct device_node *pnode; >> @@ -213,33 +213,44 @@ static int of_pci_prop_intr_map(struct pci_dev *pdev, struct of_changeset *ocs, >> out_irq[i].args[0] = pin; >> ret = of_irq_parse_raw(laddr, &out_irq[i]); >> if (ret) { >> - pci_err(pdev, "parse irq %d failed, ret %d", pin, ret); >> + out_irq[i].np = NULL; >> + pci_dbg(pdev, "parse irq %d failed, ret %d", pin, ret); >> continue; >> } >> - ret = of_property_read_u32(out_irq[i].np, "#address-cells", >> - &addr_sz[i]); >> - if (ret) >> - addr_sz[i] = 0; >> + of_property_read_u32(out_irq[i].np, "#address-cells", >> + &addr_sz[i]); >> } >> >> list_for_each_entry(child, &pdev->subordinate->devices, bus_list) { >> for (pin = 1; pin <= OF_PCI_MAX_INT_PIN; pin++) { >> i = pci_swizzle_interrupt_pin(child, pin) - 1; >> + if (!out_irq[i].np) >> + continue; >> map_sz += 5 + addr_sz[i] + out_irq[i].args_count; >> } >> } >> >> + /* >> + * Parsing interrupt failed for all pins. In this case, it does not >> + * need to generate interrupt-map property. >> + */ >> + if (!map_sz) >> + return 0; >> + >> int_map = kcalloc(map_sz, sizeof(u32), GFP_KERNEL); >> mapp = int_map; >> >> list_for_each_entry(child, &pdev->subordinate->devices, bus_list) { >> for (pin = 1; pin <= OF_PCI_MAX_INT_PIN; pin++) { >> + i = pci_swizzle_interrupt_pin(child, pin) - 1; >> + if (!out_irq[i].np) >> + continue; >> + >> *mapp = (child->bus->number << 16) | >> (child->devfn << 8); >> mapp += OF_PCI_ADDRESS_CELLS; >> *mapp = pin; >> mapp++; >> - i = pci_swizzle_interrupt_pin(child, pin) - 1; >> *mapp = out_irq[i].np->phandle; >> mapp++; >> if (addr_sz[i]) { >> -- >> 2.34.1 >> ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-09-29 16:40 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2023-09-28 16:47 [PATCH v1 1/2] PCI: of: Fix memory leak when of_changeset_create_node() failed Lizhi Hou 2023-09-28 16:47 ` [PATCH v1 2/2] PCI: of_property: Fix uninitialized variable when of_irq_parse_raw() failed Lizhi Hou 2023-09-29 16:27 ` Bjorn Helgaas 2023-09-29 16:40 ` Lizhi Hou
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).