Skip to content

Commit 2ec6f20

Browse files
l1kbroonie
authored andcommitted
spi: Cleanup on failure of initial setup
Commit c7299fe ("spi: Fix spi device unregister flow") changed the SPI core's behavior if the ->setup() hook returns an error upon adding an spi_device: Before, the ->cleanup() hook was invoked to free any allocations that were made by ->setup(). With the commit, that's no longer the case, so the ->setup() hook is expected to free the allocations itself. I've identified 5 drivers which depend on the old behavior and am fixing them up hereinafter: spi-bitbang.c spi-fsl-spi.c spi-omap-uwire.c spi-omap2-mcspi.c spi-pxa2xx.c Importantly, ->setup() is not only invoked on spi_device *addition*: It may subsequently be called to *change* SPI parameters. If changing these SPI parameters fails, freeing memory allocations would be wrong. That should only be done if the spi_device is finally destroyed. I am therefore using a bool "initial_setup" in 4 of the affected drivers to differentiate between the invocation on *adding* the spi_device and any subsequent invocations: spi-bitbang.c spi-fsl-spi.c spi-omap-uwire.c spi-omap2-mcspi.c In spi-pxa2xx.c, it seems the ->setup() hook can only fail on spi_device addition, not any subsequent calls. It therefore doesn't need the bool. It's worth noting that 5 other drivers already perform a cleanup if the ->setup() hook fails. Before c7299fe, they caused a double-free if ->setup() failed on spi_device addition. Since the commit, they're fine. These drivers are: spi-mpc512x-psc.c spi-pl022.c spi-s3c64xx.c spi-st-ssc4.c spi-tegra114.c (spi-pxa2xx.c also already performs a cleanup, but only in one of several error paths.) Fixes: c7299fe ("spi: Fix spi device unregister flow") Signed-off-by: Lukas Wunner <[email protected]> Cc: Saravana Kannan <[email protected]> Acked-by: Andy Shevchenko <[email protected]> # pxa2xx Link: https://lore.kernel.org/r/f76a0599469f265b69c371538794101fa37b5536.1622149321.git.lukas@wunner.de Signed-off-by: Mark Brown <[email protected]>
1 parent 13817d4 commit 2ec6f20

File tree

5 files changed

+54
-19
lines changed

5 files changed

+54
-19
lines changed

drivers/spi/spi-bitbang.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ int spi_bitbang_setup(struct spi_device *spi)
184184
{
185185
struct spi_bitbang_cs *cs = spi->controller_state;
186186
struct spi_bitbang *bitbang;
187+
bool initial_setup = false;
188+
int retval;
187189

188190
bitbang = spi_master_get_devdata(spi->master);
189191

@@ -192,22 +194,30 @@ int spi_bitbang_setup(struct spi_device *spi)
192194
if (!cs)
193195
return -ENOMEM;
194196
spi->controller_state = cs;
197+
initial_setup = true;
195198
}
196199

197200
/* per-word shift register access, in hardware or bitbanging */
198201
cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
199-
if (!cs->txrx_word)
200-
return -EINVAL;
202+
if (!cs->txrx_word) {
203+
retval = -EINVAL;
204+
goto err_free;
205+
}
201206

202207
if (bitbang->setup_transfer) {
203-
int retval = bitbang->setup_transfer(spi, NULL);
208+
retval = bitbang->setup_transfer(spi, NULL);
204209
if (retval < 0)
205-
return retval;
210+
goto err_free;
206211
}
207212

208213
dev_dbg(&spi->dev, "%s, %u nsec/bit\n", __func__, 2 * cs->nsecs);
209214

210215
return 0;
216+
217+
err_free:
218+
if (initial_setup)
219+
kfree(cs);
220+
return retval;
211221
}
212222
EXPORT_SYMBOL_GPL(spi_bitbang_setup);
213223

drivers/spi/spi-fsl-spi.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ static int fsl_spi_setup(struct spi_device *spi)
440440
{
441441
struct mpc8xxx_spi *mpc8xxx_spi;
442442
struct fsl_spi_reg __iomem *reg_base;
443+
bool initial_setup = false;
443444
int retval;
444445
u32 hw_mode;
445446
struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
@@ -452,6 +453,7 @@ static int fsl_spi_setup(struct spi_device *spi)
452453
if (!cs)
453454
return -ENOMEM;
454455
spi_set_ctldata(spi, cs);
456+
initial_setup = true;
455457
}
456458
mpc8xxx_spi = spi_master_get_devdata(spi->master);
457459

@@ -475,6 +477,8 @@ static int fsl_spi_setup(struct spi_device *spi)
475477
retval = fsl_spi_setup_transfer(spi, NULL);
476478
if (retval < 0) {
477479
cs->hw_mode = hw_mode; /* Restore settings */
480+
if (initial_setup)
481+
kfree(cs);
478482
return retval;
479483
}
480484

drivers/spi/spi-omap-uwire.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,15 +424,22 @@ static int uwire_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
424424
static int uwire_setup(struct spi_device *spi)
425425
{
426426
struct uwire_state *ust = spi->controller_state;
427+
bool initial_setup = false;
428+
int status;
427429

428430
if (ust == NULL) {
429431
ust = kzalloc(sizeof(*ust), GFP_KERNEL);
430432
if (ust == NULL)
431433
return -ENOMEM;
432434
spi->controller_state = ust;
435+
initial_setup = true;
433436
}
434437

435-
return uwire_setup_transfer(spi, NULL);
438+
status = uwire_setup_transfer(spi, NULL);
439+
if (status && initial_setup)
440+
kfree(ust);
441+
442+
return status;
436443
}
437444

438445
static void uwire_cleanup(struct spi_device *spi)

drivers/spi/spi-omap2-mcspi.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,8 +1032,22 @@ static void omap2_mcspi_release_dma(struct spi_master *master)
10321032
}
10331033
}
10341034

1035+
static void omap2_mcspi_cleanup(struct spi_device *spi)
1036+
{
1037+
struct omap2_mcspi_cs *cs;
1038+
1039+
if (spi->controller_state) {
1040+
/* Unlink controller state from context save list */
1041+
cs = spi->controller_state;
1042+
list_del(&cs->node);
1043+
1044+
kfree(cs);
1045+
}
1046+
}
1047+
10351048
static int omap2_mcspi_setup(struct spi_device *spi)
10361049
{
1050+
bool initial_setup = false;
10371051
int ret;
10381052
struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
10391053
struct omap2_mcspi_regs *ctx = &mcspi->ctx;
@@ -1051,35 +1065,28 @@ static int omap2_mcspi_setup(struct spi_device *spi)
10511065
spi->controller_state = cs;
10521066
/* Link this to context save list */
10531067
list_add_tail(&cs->node, &ctx->cs);
1068+
initial_setup = true;
10541069
}
10551070

10561071
ret = pm_runtime_get_sync(mcspi->dev);
10571072
if (ret < 0) {
10581073
pm_runtime_put_noidle(mcspi->dev);
1074+
if (initial_setup)
1075+
omap2_mcspi_cleanup(spi);
10591076

10601077
return ret;
10611078
}
10621079

10631080
ret = omap2_mcspi_setup_transfer(spi, NULL);
1081+
if (ret && initial_setup)
1082+
omap2_mcspi_cleanup(spi);
1083+
10641084
pm_runtime_mark_last_busy(mcspi->dev);
10651085
pm_runtime_put_autosuspend(mcspi->dev);
10661086

10671087
return ret;
10681088
}
10691089

1070-
static void omap2_mcspi_cleanup(struct spi_device *spi)
1071-
{
1072-
struct omap2_mcspi_cs *cs;
1073-
1074-
if (spi->controller_state) {
1075-
/* Unlink controller state from context save list */
1076-
cs = spi->controller_state;
1077-
list_del(&cs->node);
1078-
1079-
kfree(cs);
1080-
}
1081-
}
1082-
10831090
static irqreturn_t omap2_mcspi_irq_handler(int irq, void *data)
10841091
{
10851092
struct omap2_mcspi *mcspi = data;

drivers/spi/spi-pxa2xx.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,8 @@ static int setup_cs(struct spi_device *spi, struct chip_data *chip,
12541254
chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
12551255

12561256
err = gpiod_direction_output(gpiod, !chip->gpio_cs_inverted);
1257+
if (err)
1258+
gpiod_put(chip->gpiod_cs);
12571259
}
12581260

12591261
return err;
@@ -1267,6 +1269,7 @@ static int setup(struct spi_device *spi)
12671269
struct driver_data *drv_data =
12681270
spi_controller_get_devdata(spi->controller);
12691271
uint tx_thres, tx_hi_thres, rx_thres;
1272+
int err;
12701273

12711274
switch (drv_data->ssp_type) {
12721275
case QUARK_X1000_SSP:
@@ -1413,7 +1416,11 @@ static int setup(struct spi_device *spi)
14131416
if (drv_data->ssp_type == CE4100_SSP)
14141417
return 0;
14151418

1416-
return setup_cs(spi, chip, chip_info);
1419+
err = setup_cs(spi, chip, chip_info);
1420+
if (err)
1421+
kfree(chip);
1422+
1423+
return err;
14171424
}
14181425

14191426
static void cleanup(struct spi_device *spi)

0 commit comments

Comments
 (0)