Skip to content
Permalink
Browse files
spi: mt7621: Don't leak SPI master in probe error path
If the calls to device_reset() or devm_spi_register_controller() fail on
probe of the MediaTek MT7621 SPI driver, the spi_controller struct is
erroneously not freed.  Fix by switching over to the new
devm_spi_alloc_master() helper.

Moreover, the SYS clock is enabled on probe but not disabled if any of
the subsequent probe steps fails.

Finally, there's an ordering issue in mt7621_spi_remove() wherein
the spi_controller is unregistered after disabling the SYS clock.
The correct order is to call spi_unregister_controller() *before* this
teardown step because bus accesses may still be ongoing until that
function returns.

All of these bugs have existed since the driver was first introduced,
so it seems fair to fix them together in a single commit.

Fixes: 1ab7f2a ("staging: mt7621-spi: add mt7621 support")
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Cc: <stable@vger.kernel.org> # v4.17+: 5e844cc: spi: Introduce device-managed SPI controller allocation
Cc: <stable@vger.kernel.org> # v4.17+
  • Loading branch information
l1k committed Nov 15, 2020
1 parent 4a7efc3 commit 99e9a80eb65b1787cd055025129a45e24f2fc6ae
Showing 1 changed file with 14 additions and 4 deletions.
@@ -350,10 +350,11 @@ static int mt7621_spi_probe(struct platform_device *pdev)
if (status)
return status;

master = spi_alloc_master(&pdev->dev, sizeof(*rs));
master = devm_spi_alloc_master(&pdev->dev, sizeof(*rs));
if (!master) {
dev_info(&pdev->dev, "master allocation failed\n");
return -ENOMEM;
ret = -ENOMEM;
goto err_clk_disable;
}

master->mode_bits = SPI_LSB_FIRST;
@@ -377,10 +378,18 @@ static int mt7621_spi_probe(struct platform_device *pdev)
ret = device_reset(&pdev->dev);
if (ret) {
dev_err(&pdev->dev, "SPI reset failed!\n");
return ret;
goto err_clk_disable;
}

return devm_spi_register_controller(&pdev->dev, master);
ret = spi_register_controller(master);
if (ret)
goto err_clk_disable;

return 0;

err_clk_disable:
clk_disable_unprepare(rs->clk);
return ret;
}

static int mt7621_spi_remove(struct platform_device *pdev)
@@ -391,6 +400,7 @@ static int mt7621_spi_remove(struct platform_device *pdev)
master = dev_get_drvdata(&pdev->dev);
rs = spi_controller_get_devdata(master);

spi_unregister_controller(master);
clk_disable_unprepare(rs->clk);

return 0;

0 comments on commit 99e9a80

Please sign in to comment.