]> git.baikalelectronics.ru Git - kernel.git/commitdiff
macb: Don't unregister clks unconditionally
authorStephen Boyd <sboyd@kernel.org>
Sat, 4 Jan 2020 00:19:21 +0000 (16:19 -0800)
committerDavid S. Miller <davem@davemloft.net>
Sun, 5 Jan 2020 23:09:58 +0000 (15:09 -0800)
The only clk init function in this driver that register a clk is
fu540_c000_clk_init(), and thus we need to unregister the clk when this
driver is removed on that platform. Other init functions, for example
macb_clk_init(), don't register clks and therefore we shouldn't
unregister the clks when this driver is removed. Convert this
registration path to devm so it gets auto-unregistered when this driver
is removed and drop the clk_unregister() calls in driver remove (and
error paths) so that we don't erroneously remove a clk from the system
that isn't registered by this driver.

Otherwise we get strange crashes with a use-after-free when the
devm_clk_get() call in macb_clk_init() calls clk_put() on a clk pointer
that has become invalid because it is freed in clk_unregister().

Cc: Nicolas Ferre <nicolas.ferre@microchip.com>
Cc: Yash Shah <yash.shah@sifive.com>
Reported-by: Guenter Roeck <linux@roeck-us.net>
Fixes: 8889bf335e79 ("macb: Add support for SiFive FU540-C000")
Signed-off-by: Stephen Boyd <sboyd@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/ethernet/cadence/macb_main.c

index c5ee363ca5dc36f2512d3acabc401ceab3b968e2..a0503b99dc790a1833a0cb32299aa5b2dfafd8a4 100644 (file)
@@ -4088,7 +4088,7 @@ static int fu540_c000_clk_init(struct platform_device *pdev, struct clk **pclk,
        mgmt->rate = 0;
        mgmt->hw.init = &init;
 
-       *tx_clk = clk_register(NULL, &mgmt->hw);
+       *tx_clk = devm_clk_register(&pdev->dev, &mgmt->hw);
        if (IS_ERR(*tx_clk))
                return PTR_ERR(*tx_clk);
 
@@ -4416,7 +4416,6 @@ err_out_free_netdev:
 
 err_disable_clocks:
        clk_disable_unprepare(tx_clk);
-       clk_unregister(tx_clk);
        clk_disable_unprepare(hclk);
        clk_disable_unprepare(pclk);
        clk_disable_unprepare(rx_clk);
@@ -4446,7 +4445,6 @@ static int macb_remove(struct platform_device *pdev)
                pm_runtime_dont_use_autosuspend(&pdev->dev);
                if (!pm_runtime_suspended(&pdev->dev)) {
                        clk_disable_unprepare(bp->tx_clk);
-                       clk_unregister(bp->tx_clk);
                        clk_disable_unprepare(bp->hclk);
                        clk_disable_unprepare(bp->pclk);
                        clk_disable_unprepare(bp->rx_clk);