spi: bitbang: Let spi_bitbang_start() take a reference to master

Many drivers that use bitbang library have a leak on probe error paths.
This is because once a spi_master_get() call succeeds, we need an additional
spi_master_put() call to free the memory.

Fix this issue by moving the code taking a reference to master to
spi_bitbang_start(), so spi_bitbang_start() will take a reference to master on
success. With this change, the caller is responsible for calling
spi_bitbang_stop() to decrement the reference and spi_master_put() as
counterpart of spi_alloc_master() to prevent a memory leak.

So now we have below patten for drivers using bitbang library:

probe:
spi_alloc_master        -> Init reference count to 1
spi_bitbang_start       -> Increment reference count
remove:
spi_bitbang_stop        -> Decrement reference count
spi_master_put          -> Decrement reference count (reference count reaches 0)

Fixup all users accordingly.

Signed-off-by: Axel Lin <axel.lin@ingics.com>
Suggested-by: Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>
Acked-by: Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>
Signed-off-by: Mark Brown <broonie@linaro.org>
This commit is contained in:
Axel Lin 2013-09-10 15:43:41 +08:00 коммит произвёл Mark Brown
Родитель d0e639c9e0
Коммит 702a4879ec
18 изменённых файлов: 33 добавлений и 26 удалений

Просмотреть файл

@ -219,7 +219,7 @@ static int altera_spi_probe(struct platform_device *pdev)
platform_set_drvdata(pdev, hw); platform_set_drvdata(pdev, hw);
/* setup the state for the bitbang driver */ /* setup the state for the bitbang driver */
hw->bitbang.master = spi_master_get(master); hw->bitbang.master = master;
if (!hw->bitbang.master) if (!hw->bitbang.master)
return err; return err;
hw->bitbang.chipselect = altera_spi_chipsel; hw->bitbang.chipselect = altera_spi_chipsel;

Просмотреть файл

@ -231,7 +231,7 @@ static int ath79_spi_probe(struct platform_device *pdev)
master->num_chipselect = pdata->num_chipselect; master->num_chipselect = pdata->num_chipselect;
} }
sp->bitbang.master = spi_master_get(master); sp->bitbang.master = master;
sp->bitbang.chipselect = ath79_spi_chipselect; sp->bitbang.chipselect = ath79_spi_chipselect;
sp->bitbang.txrx_word[SPI_MODE_0] = ath79_spi_txrx_mode0; sp->bitbang.txrx_word[SPI_MODE_0] = ath79_spi_txrx_mode0;
sp->bitbang.setup_transfer = spi_bitbang_setup_transfer; sp->bitbang.setup_transfer = spi_bitbang_setup_transfer;

Просмотреть файл

@ -775,7 +775,7 @@ static int au1550_spi_probe(struct platform_device *pdev)
hw = spi_master_get_devdata(master); hw = spi_master_get_devdata(master);
hw->master = spi_master_get(master); hw->master = master;
hw->pdata = dev_get_platdata(&pdev->dev); hw->pdata = dev_get_platdata(&pdev->dev);
hw->dev = &pdev->dev; hw->dev = &pdev->dev;

Просмотреть файл

@ -414,10 +414,16 @@ static int spi_bitbang_unprepare_hardware(struct spi_master *spi)
* This routine registers the spi_master, which will process requests in a * This routine registers the spi_master, which will process requests in a
* dedicated task, keeping IRQs unblocked most of the time. To stop * dedicated task, keeping IRQs unblocked most of the time. To stop
* processing those requests, call spi_bitbang_stop(). * processing those requests, call spi_bitbang_stop().
*
* On success, this routine will take a reference to master. The caller is
* responsible for calling spi_bitbang_stop() to decrement the reference and
* spi_master_put() as counterpart of spi_alloc_master() to prevent a memory
* leak.
*/ */
int spi_bitbang_start(struct spi_bitbang *bitbang) int spi_bitbang_start(struct spi_bitbang *bitbang)
{ {
struct spi_master *master = bitbang->master; struct spi_master *master = bitbang->master;
int ret;
if (!master || !bitbang->chipselect) if (!master || !bitbang->chipselect)
return -EINVAL; return -EINVAL;
@ -449,7 +455,11 @@ int spi_bitbang_start(struct spi_bitbang *bitbang)
/* driver may get busy before register() returns, especially /* driver may get busy before register() returns, especially
* if someone registered boardinfo for devices * if someone registered boardinfo for devices
*/ */
return spi_register_master(master); ret = spi_register_master(spi_master_get(master));
if (ret)
spi_master_put(master);
return 0;
} }
EXPORT_SYMBOL_GPL(spi_bitbang_start); EXPORT_SYMBOL_GPL(spi_bitbang_start);

Просмотреть файл

@ -225,7 +225,7 @@ static void butterfly_attach(struct parport *p)
master->bus_num = 42; master->bus_num = 42;
master->num_chipselect = 2; master->num_chipselect = 2;
pp->bitbang.master = spi_master_get(master); pp->bitbang.master = master;
pp->bitbang.chipselect = butterfly_chipselect; pp->bitbang.chipselect = butterfly_chipselect;
pp->bitbang.txrx_word[SPI_MODE_0] = butterfly_txrx_word_mode0; pp->bitbang.txrx_word[SPI_MODE_0] = butterfly_txrx_word_mode0;

Просмотреть файл

@ -916,7 +916,7 @@ static int davinci_spi_probe(struct platform_device *pdev)
if (ret) if (ret)
goto unmap_io; goto unmap_io;
dspi->bitbang.master = spi_master_get(master); dspi->bitbang.master = master;
if (dspi->bitbang.master == NULL) { if (dspi->bitbang.master == NULL) {
ret = -ENODEV; ret = -ENODEV;
goto irq_free; goto irq_free;
@ -925,7 +925,7 @@ static int davinci_spi_probe(struct platform_device *pdev)
dspi->clk = clk_get(&pdev->dev, NULL); dspi->clk = clk_get(&pdev->dev, NULL);
if (IS_ERR(dspi->clk)) { if (IS_ERR(dspi->clk)) {
ret = -ENODEV; ret = -ENODEV;
goto put_master; goto irq_free;
} }
clk_prepare_enable(dspi->clk); clk_prepare_enable(dspi->clk);
@ -1015,8 +1015,6 @@ free_dma:
free_clk: free_clk:
clk_disable_unprepare(dspi->clk); clk_disable_unprepare(dspi->clk);
clk_put(dspi->clk); clk_put(dspi->clk);
put_master:
spi_master_put(master);
irq_free: irq_free:
free_irq(dspi->irq, dspi); free_irq(dspi->irq, dspi);
unmap_io: unmap_io:
@ -1024,7 +1022,7 @@ unmap_io:
release_region: release_region:
release_mem_region(dspi->pbase, resource_size(r)); release_mem_region(dspi->pbase, resource_size(r));
free_master: free_master:
kfree(master); spi_master_put(master);
err: err:
return ret; return ret;
} }
@ -1051,11 +1049,11 @@ static int davinci_spi_remove(struct platform_device *pdev)
clk_disable_unprepare(dspi->clk); clk_disable_unprepare(dspi->clk);
clk_put(dspi->clk); clk_put(dspi->clk);
spi_master_put(master);
free_irq(dspi->irq, dspi); free_irq(dspi->irq, dspi);
iounmap(dspi->base); iounmap(dspi->base);
r = platform_get_resource(pdev, IORESOURCE_MEM, 0); r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
release_mem_region(dspi->pbase, resource_size(r)); release_mem_region(dspi->pbase, resource_size(r));
spi_master_put(master);
return 0; return 0;
} }

Просмотреть файл

@ -347,7 +347,7 @@ static int efm32_spi_probe(struct platform_device *pdev)
ddata = spi_master_get_devdata(master); ddata = spi_master_get_devdata(master);
ddata->bitbang.master = spi_master_get(master); ddata->bitbang.master = master;
ddata->bitbang.chipselect = efm32_spi_chipselect; ddata->bitbang.chipselect = efm32_spi_chipselect;
ddata->bitbang.setup_transfer = efm32_spi_setup_transfer; ddata->bitbang.setup_transfer = efm32_spi_setup_transfer;
ddata->bitbang.txrx_bufs = efm32_spi_txrx_bufs; ddata->bitbang.txrx_bufs = efm32_spi_txrx_bufs;

Просмотреть файл

@ -450,7 +450,7 @@ static int dspi_probe(struct platform_device *pdev)
dspi = spi_master_get_devdata(master); dspi = spi_master_get_devdata(master);
dspi->pdev = pdev; dspi->pdev = pdev;
dspi->bitbang.master = spi_master_get(master); dspi->bitbang.master = master;
dspi->bitbang.chipselect = dspi_chipselect; dspi->bitbang.chipselect = dspi_chipselect;
dspi->bitbang.setup_transfer = dspi_setup_transfer; dspi->bitbang.setup_transfer = dspi_setup_transfer;
dspi->bitbang.txrx_bufs = dspi_txrx_transfer; dspi->bitbang.txrx_bufs = dspi_txrx_transfer;

Просмотреть файл

@ -467,7 +467,7 @@ static int spi_gpio_probe(struct platform_device *pdev)
} }
#endif #endif
spi_gpio->bitbang.master = spi_master_get(master); spi_gpio->bitbang.master = master;
spi_gpio->bitbang.chipselect = spi_gpio_chipselect; spi_gpio->bitbang.chipselect = spi_gpio_chipselect;
if ((master_flags & (SPI_MASTER_NO_TX | SPI_MASTER_NO_RX)) == 0) { if ((master_flags & (SPI_MASTER_NO_TX | SPI_MASTER_NO_RX)) == 0) {
@ -486,7 +486,6 @@ static int spi_gpio_probe(struct platform_device *pdev)
status = spi_bitbang_start(&spi_gpio->bitbang); status = spi_bitbang_start(&spi_gpio->bitbang);
if (status < 0) { if (status < 0) {
spi_master_put(spi_gpio->bitbang.master);
gpio_free: gpio_free:
if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
gpio_free(SPI_MISO_GPIO); gpio_free(SPI_MISO_GPIO);
@ -510,13 +509,13 @@ static int spi_gpio_remove(struct platform_device *pdev)
/* stop() unregisters child devices too */ /* stop() unregisters child devices too */
status = spi_bitbang_stop(&spi_gpio->bitbang); status = spi_bitbang_stop(&spi_gpio->bitbang);
spi_master_put(spi_gpio->bitbang.master);
if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
gpio_free(SPI_MISO_GPIO); gpio_free(SPI_MISO_GPIO);
if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
gpio_free(SPI_MOSI_GPIO); gpio_free(SPI_MOSI_GPIO);
gpio_free(SPI_SCK_GPIO); gpio_free(SPI_SCK_GPIO);
spi_master_put(spi_gpio->bitbang.master);
return status; return status;
} }

Просмотреть файл

@ -786,7 +786,7 @@ static int spi_imx_probe(struct platform_device *pdev)
master->num_chipselect = num_cs; master->num_chipselect = num_cs;
spi_imx = spi_master_get_devdata(master); spi_imx = spi_master_get_devdata(master);
spi_imx->bitbang.master = spi_master_get(master); spi_imx->bitbang.master = master;
for (i = 0; i < master->num_chipselect; i++) { for (i = 0; i < master->num_chipselect; i++) {
int cs_gpio = of_get_named_gpio(np, "cs-gpios", i); int cs_gpio = of_get_named_gpio(np, "cs-gpios", i);

Просмотреть файл

@ -222,7 +222,7 @@ static void spi_lm70llp_attach(struct parport *p)
/* /*
* SPI and bitbang hookup. * SPI and bitbang hookup.
*/ */
pp->bitbang.master = spi_master_get(master); pp->bitbang.master = master;
pp->bitbang.chipselect = lm70_chipselect; pp->bitbang.chipselect = lm70_chipselect;
pp->bitbang.txrx_word[SPI_MODE_0] = lm70_txrx; pp->bitbang.txrx_word[SPI_MODE_0] = lm70_txrx;
pp->bitbang.flags = SPI_3WIRE; pp->bitbang.flags = SPI_3WIRE;

Просмотреть файл

@ -349,7 +349,7 @@ static int nuc900_spi_probe(struct platform_device *pdev)
} }
hw = spi_master_get_devdata(master); hw = spi_master_get_devdata(master);
hw->master = spi_master_get(master); hw->master = master;
hw->pdata = dev_get_platdata(&pdev->dev); hw->pdata = dev_get_platdata(&pdev->dev);
hw->dev = &pdev->dev; hw->dev = &pdev->dev;
@ -435,7 +435,6 @@ err_iomap:
kfree(hw->ioarea); kfree(hw->ioarea);
err_pdata: err_pdata:
spi_master_put(hw->master); spi_master_put(hw->master);
err_nomem: err_nomem:
return err; return err;
} }

Просмотреть файл

@ -306,7 +306,7 @@ static int tiny_spi_probe(struct platform_device *pdev)
platform_set_drvdata(pdev, hw); platform_set_drvdata(pdev, hw);
/* setup the state for the bitbang driver */ /* setup the state for the bitbang driver */
hw->bitbang.master = spi_master_get(master); hw->bitbang.master = master;
if (!hw->bitbang.master) if (!hw->bitbang.master)
return err; return err;
hw->bitbang.setup_transfer = tiny_spi_setup_transfer; hw->bitbang.setup_transfer = tiny_spi_setup_transfer;

Просмотреть файл

@ -396,7 +396,7 @@ static int spi_ppc4xx_of_probe(struct platform_device *op)
master->dev.of_node = np; master->dev.of_node = np;
platform_set_drvdata(op, master); platform_set_drvdata(op, master);
hw = spi_master_get_devdata(master); hw = spi_master_get_devdata(master);
hw->master = spi_master_get(master); hw->master = master;
hw->dev = dev; hw->dev = dev;
init_completion(&hw->done); init_completion(&hw->done);
@ -558,6 +558,7 @@ static int spi_ppc4xx_of_remove(struct platform_device *op)
free_irq(hw->irqnum, hw); free_irq(hw->irqnum, hw);
iounmap(hw->regs); iounmap(hw->regs);
free_gpios(hw); free_gpios(hw);
spi_master_put(master);
return 0; return 0;
} }

Просмотреть файл

@ -524,7 +524,7 @@ static int s3c24xx_spi_probe(struct platform_device *pdev)
hw = spi_master_get_devdata(master); hw = spi_master_get_devdata(master);
memset(hw, 0, sizeof(struct s3c24xx_spi)); memset(hw, 0, sizeof(struct s3c24xx_spi));
hw->master = spi_master_get(master); hw->master = master;
hw->pdata = pdata = dev_get_platdata(&pdev->dev); hw->pdata = pdata = dev_get_platdata(&pdev->dev);
hw->dev = &pdev->dev; hw->dev = &pdev->dev;

Просмотреть файл

@ -133,7 +133,7 @@ static int sh_sci_spi_probe(struct platform_device *dev)
sp->info = dev_get_platdata(&dev->dev); sp->info = dev_get_platdata(&dev->dev);
/* setup spi bitbang adaptor */ /* setup spi bitbang adaptor */
sp->bitbang.master = spi_master_get(master); sp->bitbang.master = master;
sp->bitbang.master->bus_num = sp->info->bus_num; sp->bitbang.master->bus_num = sp->info->bus_num;
sp->bitbang.master->num_chipselect = sp->info->num_chipselect; sp->bitbang.master->num_chipselect = sp->info->num_chipselect;
sp->bitbang.chipselect = sh_sci_spi_chipselect; sp->bitbang.chipselect = sh_sci_spi_chipselect;

Просмотреть файл

@ -632,7 +632,7 @@ static int spi_sirfsoc_probe(struct platform_device *pdev)
if (ret) if (ret)
goto free_master; goto free_master;
sspi->bitbang.master = spi_master_get(master); sspi->bitbang.master = master;
sspi->bitbang.chipselect = spi_sirfsoc_chipselect; sspi->bitbang.chipselect = spi_sirfsoc_chipselect;
sspi->bitbang.setup_transfer = spi_sirfsoc_setup_transfer; sspi->bitbang.setup_transfer = spi_sirfsoc_setup_transfer;
sspi->bitbang.txrx_bufs = spi_sirfsoc_transfer; sspi->bitbang.txrx_bufs = spi_sirfsoc_transfer;

Просмотреть файл

@ -372,7 +372,7 @@ static int xilinx_spi_probe(struct platform_device *pdev)
master->mode_bits = SPI_CPOL | SPI_CPHA; master->mode_bits = SPI_CPOL | SPI_CPHA;
xspi = spi_master_get_devdata(master); xspi = spi_master_get_devdata(master);
xspi->bitbang.master = spi_master_get(master); xspi->bitbang.master = master;
xspi->bitbang.chipselect = xilinx_spi_chipselect; xspi->bitbang.chipselect = xilinx_spi_chipselect;
xspi->bitbang.setup_transfer = xilinx_spi_setup_transfer; xspi->bitbang.setup_transfer = xilinx_spi_setup_transfer;
xspi->bitbang.txrx_bufs = xilinx_spi_txrx_bufs; xspi->bitbang.txrx_bufs = xilinx_spi_txrx_bufs;