Commit 61b40ce
net: dsa: bcm_sf2: Fix possible memory leak in bcm_sf2_mdio_register()
In bcm_sf2_mdio_register(), the class_find_device() will call get_device()
to increment reference count for priv->master_mii_bus->dev if
of_mdio_find_bus() succeeds. If mdiobus_alloc() or mdiobus_register()
fails, it will call get_device() twice without decrement reference count
for the device. And it is the same if bcm_sf2_mdio_register() succeeds but
fails in bcm_sf2_sw_probe(), or if bcm_sf2_sw_probe() succeeds. If the
reference count has not decremented to zero, the dev related resource will
not be freed.
So remove the get_device() in bcm_sf2_mdio_register(), and call
put_device() if mdiobus_alloc() or mdiobus_register() fails and in
bcm_sf2_mdio_unregister() to solve the issue.
And as Simon suggested, unwind from errors for bcm_sf2_mdio_register() and
just return 0 if it succeeds to make it cleaner.
Fixes: 461cd1b ("net: dsa: bcm_sf2: Register our slave MDIO bus")
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
Suggested-by: Simon Horman <horms@kernel.org>
Reviewed-by: Simon Horman <horms@kernel.org>
Reviewed-by: Florian Fainelli <florian.fainelli@broadcom.com>
Link: https://lore.kernel.org/r/20231011032419.2423290-1-ruanjinjie@huawei.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>1 parent dda5e1e commit 61b40ce
1 file changed
+15
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
617 | 617 | | |
618 | 618 | | |
619 | 619 | | |
620 | | - | |
621 | | - | |
| 620 | + | |
| 621 | + | |
622 | 622 | | |
623 | 623 | | |
624 | | - | |
625 | 624 | | |
626 | 625 | | |
627 | 626 | | |
628 | 627 | | |
629 | | - | |
630 | | - | |
| 628 | + | |
| 629 | + | |
631 | 630 | | |
632 | 631 | | |
633 | 632 | | |
| |||
684 | 683 | | |
685 | 684 | | |
686 | 685 | | |
687 | | - | |
688 | | - | |
689 | | - | |
690 | | - | |
| 686 | + | |
| 687 | + | |
691 | 688 | | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
692 | 697 | | |
693 | 698 | | |
694 | 699 | | |
695 | 700 | | |
696 | 701 | | |
697 | 702 | | |
698 | 703 | | |
| 704 | + | |
699 | 705 | | |
700 | 706 | | |
701 | 707 | | |
| |||
0 commit comments