Skip to content

Commit

Permalink
spi: bitbang: Let spi_bitbang_start() take a reference to master
Browse files Browse the repository at this point in the history
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 <[email protected]>
Suggested-by: Uwe Kleine-Koenig <[email protected]>
Acked-by: Uwe Kleine-Koenig <[email protected]>
Signed-off-by: Mark Brown <[email protected]>
  • Loading branch information
AxelLin authored and broonie committed Oct 7, 2013
1 parent d0e639c commit 702a487
Show file tree
Hide file tree
Showing 18 changed files with 33 additions and 26 deletions.
2 changes: 1 addition & 1 deletion drivers/spi/spi-altera.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ static int altera_spi_probe(struct platform_device *pdev)
platform_set_drvdata(pdev, hw);

/* setup the state for the bitbang driver */
hw->bitbang.master = spi_master_get(master);
hw->bitbang.master = master;
if (!hw->bitbang.master)
return err;
hw->bitbang.chipselect = altera_spi_chipsel;
Expand Down
2 changes: 1 addition & 1 deletion drivers/spi/spi-ath79.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ static int ath79_spi_probe(struct platform_device *pdev)
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.txrx_word[SPI_MODE_0] = ath79_spi_txrx_mode0;
sp->bitbang.setup_transfer = spi_bitbang_setup_transfer;
Expand Down
2 changes: 1 addition & 1 deletion drivers/spi/spi-au1550.c
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ static int au1550_spi_probe(struct platform_device *pdev)

hw = spi_master_get_devdata(master);

hw->master = spi_master_get(master);
hw->master = master;
hw->pdata = dev_get_platdata(&pdev->dev);
hw->dev = &pdev->dev;

Expand Down
12 changes: 11 additions & 1 deletion drivers/spi/spi-bitbang.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
* dedicated task, keeping IRQs unblocked most of the time. To 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)
{
struct spi_master *master = bitbang->master;
int ret;

if (!master || !bitbang->chipselect)
return -EINVAL;
Expand Down Expand Up @@ -449,7 +455,11 @@ int spi_bitbang_start(struct spi_bitbang *bitbang)
/* driver may get busy before register() returns, especially
* 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);

Expand Down
2 changes: 1 addition & 1 deletion drivers/spi/spi-butterfly.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ static void butterfly_attach(struct parport *p)
master->bus_num = 42;
master->num_chipselect = 2;

pp->bitbang.master = spi_master_get(master);
pp->bitbang.master = master;
pp->bitbang.chipselect = butterfly_chipselect;
pp->bitbang.txrx_word[SPI_MODE_0] = butterfly_txrx_word_mode0;

Expand Down
10 changes: 4 additions & 6 deletions drivers/spi/spi-davinci.c
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ static int davinci_spi_probe(struct platform_device *pdev)
if (ret)
goto unmap_io;

dspi->bitbang.master = spi_master_get(master);
dspi->bitbang.master = master;
if (dspi->bitbang.master == NULL) {
ret = -ENODEV;
goto irq_free;
Expand All @@ -925,7 +925,7 @@ static int davinci_spi_probe(struct platform_device *pdev)
dspi->clk = clk_get(&pdev->dev, NULL);
if (IS_ERR(dspi->clk)) {
ret = -ENODEV;
goto put_master;
goto irq_free;
}
clk_prepare_enable(dspi->clk);

Expand Down Expand Up @@ -1015,16 +1015,14 @@ static int davinci_spi_probe(struct platform_device *pdev)
free_clk:
clk_disable_unprepare(dspi->clk);
clk_put(dspi->clk);
put_master:
spi_master_put(master);
irq_free:
free_irq(dspi->irq, dspi);
unmap_io:
iounmap(dspi->base);
release_region:
release_mem_region(dspi->pbase, resource_size(r));
free_master:
kfree(master);
spi_master_put(master);
err:
return ret;
}
Expand All @@ -1051,11 +1049,11 @@ static int davinci_spi_remove(struct platform_device *pdev)

clk_disable_unprepare(dspi->clk);
clk_put(dspi->clk);
spi_master_put(master);
free_irq(dspi->irq, dspi);
iounmap(dspi->base);
r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
release_mem_region(dspi->pbase, resource_size(r));
spi_master_put(master);

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/spi/spi-efm32.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ static int efm32_spi_probe(struct platform_device *pdev)

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.setup_transfer = efm32_spi_setup_transfer;
ddata->bitbang.txrx_bufs = efm32_spi_txrx_bufs;
Expand Down
2 changes: 1 addition & 1 deletion drivers/spi/spi-fsl-dspi.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ static int dspi_probe(struct platform_device *pdev)

dspi = spi_master_get_devdata(master);
dspi->pdev = pdev;
dspi->bitbang.master = spi_master_get(master);
dspi->bitbang.master = master;
dspi->bitbang.chipselect = dspi_chipselect;
dspi->bitbang.setup_transfer = dspi_setup_transfer;
dspi->bitbang.txrx_bufs = dspi_txrx_transfer;
Expand Down
5 changes: 2 additions & 3 deletions drivers/spi/spi-gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ static int spi_gpio_probe(struct platform_device *pdev)
}
#endif

spi_gpio->bitbang.master = spi_master_get(master);
spi_gpio->bitbang.master = master;
spi_gpio->bitbang.chipselect = spi_gpio_chipselect;

if ((master_flags & (SPI_MASTER_NO_TX | SPI_MASTER_NO_RX)) == 0) {
Expand All @@ -486,7 +486,6 @@ static int spi_gpio_probe(struct platform_device *pdev)

status = spi_bitbang_start(&spi_gpio->bitbang);
if (status < 0) {
spi_master_put(spi_gpio->bitbang.master);
gpio_free:
if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
gpio_free(SPI_MISO_GPIO);
Expand All @@ -510,13 +509,13 @@ static int spi_gpio_remove(struct platform_device *pdev)

/* stop() unregisters child devices too */
status = spi_bitbang_stop(&spi_gpio->bitbang);
spi_master_put(spi_gpio->bitbang.master);

if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
gpio_free(SPI_MISO_GPIO);
if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
gpio_free(SPI_MOSI_GPIO);
gpio_free(SPI_SCK_GPIO);
spi_master_put(spi_gpio->bitbang.master);

return status;
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/spi/spi-imx.c
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ static int spi_imx_probe(struct platform_device *pdev)
master->num_chipselect = num_cs;

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++) {
int cs_gpio = of_get_named_gpio(np, "cs-gpios", i);
Expand Down
2 changes: 1 addition & 1 deletion drivers/spi/spi-lm70llp.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ static void spi_lm70llp_attach(struct parport *p)
/*
* SPI and bitbang hookup.
*/
pp->bitbang.master = spi_master_get(master);
pp->bitbang.master = master;
pp->bitbang.chipselect = lm70_chipselect;
pp->bitbang.txrx_word[SPI_MODE_0] = lm70_txrx;
pp->bitbang.flags = SPI_3WIRE;
Expand Down
3 changes: 1 addition & 2 deletions drivers/spi/spi-nuc900.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ static int nuc900_spi_probe(struct platform_device *pdev)
}

hw = spi_master_get_devdata(master);
hw->master = spi_master_get(master);
hw->master = master;
hw->pdata = dev_get_platdata(&pdev->dev);
hw->dev = &pdev->dev;

Expand Down Expand Up @@ -435,7 +435,6 @@ static int nuc900_spi_probe(struct platform_device *pdev)
kfree(hw->ioarea);
err_pdata:
spi_master_put(hw->master);

err_nomem:
return err;
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/spi/spi-oc-tiny.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ static int tiny_spi_probe(struct platform_device *pdev)
platform_set_drvdata(pdev, hw);

/* setup the state for the bitbang driver */
hw->bitbang.master = spi_master_get(master);
hw->bitbang.master = master;
if (!hw->bitbang.master)
return err;
hw->bitbang.setup_transfer = tiny_spi_setup_transfer;
Expand Down
3 changes: 2 additions & 1 deletion drivers/spi/spi-ppc4xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ static int spi_ppc4xx_of_probe(struct platform_device *op)
master->dev.of_node = np;
platform_set_drvdata(op, master);
hw = spi_master_get_devdata(master);
hw->master = spi_master_get(master);
hw->master = master;
hw->dev = dev;

init_completion(&hw->done);
Expand Down Expand Up @@ -558,6 +558,7 @@ static int spi_ppc4xx_of_remove(struct platform_device *op)
free_irq(hw->irqnum, hw);
iounmap(hw->regs);
free_gpios(hw);
spi_master_put(master);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion drivers/spi/spi-s3c24xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ static int s3c24xx_spi_probe(struct platform_device *pdev)
hw = spi_master_get_devdata(master);
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->dev = &pdev->dev;

Expand Down
2 changes: 1 addition & 1 deletion drivers/spi/spi-sh-sci.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ static int sh_sci_spi_probe(struct platform_device *dev)
sp->info = dev_get_platdata(&dev->dev);

/* 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->num_chipselect = sp->info->num_chipselect;
sp->bitbang.chipselect = sh_sci_spi_chipselect;
Expand Down
2 changes: 1 addition & 1 deletion drivers/spi/spi-sirf.c
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ static int spi_sirfsoc_probe(struct platform_device *pdev)
if (ret)
goto free_master;

sspi->bitbang.master = spi_master_get(master);
sspi->bitbang.master = master;
sspi->bitbang.chipselect = spi_sirfsoc_chipselect;
sspi->bitbang.setup_transfer = spi_sirfsoc_setup_transfer;
sspi->bitbang.txrx_bufs = spi_sirfsoc_transfer;
Expand Down
2 changes: 1 addition & 1 deletion drivers/spi/spi-xilinx.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ static int xilinx_spi_probe(struct platform_device *pdev)
master->mode_bits = SPI_CPOL | SPI_CPHA;

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.setup_transfer = xilinx_spi_setup_transfer;
xspi->bitbang.txrx_bufs = xilinx_spi_txrx_bufs;
Expand Down

0 comments on commit 702a487

Please sign in to comment.