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:
@@ -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;
|
||||
@@ -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);
|
||||
|
||||
|
Reference in New Issue
Block a user