PCI: of: Fix memory leak when of_changeset_create_node() failed
PATCH 1/2 PCI: of: Fix memory leak when of_changeset_create_node() failed
Destroy and free cset when failure happens.
--- drivers/pci/of.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)
drivers/pci/of.c +11 -8
@@ -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 failed_alloc_cset;of_changeset_init (cset); np =of_changeset_create_node (cset, ppnode, name);if (!np) - goto failed; - np->data = cset; + goto failed_create_node; ret =of_pci_add_properties (pdev, cset, np);if (ret) - goto failed; + goto failed_add_prop; ret =of_changeset_apply (cset);if (ret) - goto failed; + goto failed_add_prop; + np->data = cset; pdev->dev.of_node = np;kfree (name);return ; -failed: -if (np) -of_node_put (np); +failed_add_prop: +of_node_put (np); +failed_create_node: +of_changeset_destroy (cset); +kfree (cset); +failed_alloc_cset: kfree(name); }#endif
Signature · 1 line
2.34.1
PATCH 2/2 PCI: of_property: Fix uninitialized variable when of_irq_parse_raw() failed
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.
--- drivers/pci/of_property.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-)
drivers/pci/of_property.c +18 -7
@@ -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]) {
Signature · 1 line
2.34.1
Re: [PATCH 2/2] PCI: of_property: Fix uninitialized variable when of_irq_parse_raw() failed
Hi Lizhi,
Quoted 67 lines · On Fri, 15 Sep 2023 11:08:07 -0700 Lizhi Hou <[email protected]> wrote:
In function
of_pci_prop_intr_map(), addr_sz[i] will be uninitialized ifof_irq_parse_raw()returns failure. Addaddr_szarray 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 <[email protected]>Signed-off-by Lizhi Hou <[email protected]>--- drivers/pci/of_property.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-)drivers/pci/of_property.c +17
-6@@ -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 ;
drivers/pci/of_property.c static int of_pci_prop_intr_map(struct pci_dev *pdev, struct of_changeset *ocs, · line 256
Quoted 10 lines
drivers/pci/of_property.c +1
-1+ *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]) {
Best regards,
Hervé
Hi Lizhi,
Quoted 15 lines · On Fri, 15 Sep 2023 11:08:06 -0700 Lizhi Hou <[email protected]> wrote:
Destroy and free cset when failure happens.
Fixes 407d1a51921e ("PCI: Create device tree node for bridge")Reported-by Herve Codina <[email protected]>Signed-off-by Lizhi Hou <[email protected]>--- drivers/pci/of.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)drivers/pci/of.c +0
-0
drivers/pci/of.c void of_pci_make_dev_node(struct pci_dev *pdev) · line 660
Quoted 5 lines
drivers/pci/of.c +1
-1@@ -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 failed_alloc_cset;
Usually goto labels indicate what the goto does instead of where we come from.
https://elixir.bootlin.com/linux/v6.5/source/Documentation/process/coding-style.rst#L536
In this case, it should be "goto failed_free_name".
drivers/pci/of.c void of_pci_make_dev_node(struct pci_dev *pdev) · line 665
Quoted 7 lines
drivers/pci/of.c +1
-2of_changeset_init (cset); np =of_changeset_create_node (cset, ppnode, name);if (!np) - goto failed; - np->data = cset; + goto failed_create_node;
same comment
drivers/pci/of.c void of_pci_make_dev_node(struct pci_dev *pdev) · line 669
Quoted 5 lines
drivers/pci/of.c +1
-1ret =of_pci_add_properties (pdev, cset, np);if (ret) - goto failed; + goto failed_add_prop;
same comment
drivers/pci/of.c void of_pci_make_dev_node(struct pci_dev *pdev) · line 673
Quoted 5 lines
drivers/pci/of.c +1
-1ret =of_changeset_apply (cset);if (ret) - goto failed; + goto failed_add_prop;
same comment
Quoted 9 lines
drivers/pci/of.c +1
-2+ np->data = cset; pdev->dev.of_node = np;kfree (name);return ; -failed: -if (np)
drivers/pci/of.c void of_pci_make_dev_node(struct pci_dev *pdev) · line 689
Quoted 10 lines
drivers/pci/of.c +6
-1-of_node_put (np); +failed_add_prop: +of_node_put (np); +failed_create_node: +of_changeset_destroy (cset); +kfree (cset); +failed_alloc_cset: kfree(name); }#endif
Best regards,
Hervé