Skip to content

Commit e32475c

Browse files
Gustav WiklanderSomasundaram Krishnasamy
authored andcommitted
spi: Fix memory leak on splited transfers
[ Upstream commit b59a7ca ] In the prepare_message callback the bus driver has the opportunity to split a transfer into smaller chunks. spi_map_msg is done after prepare_message. Function spi_res_release releases the splited transfers in the message. Therefore spi_res_release should be called after spi_map_msg. The previous try at this was commit c9ba7a1 which released the splited transfers after spi_finalize_current_message had been called. This introduced a race since the message struct could be out of scope because the spi_sync call got completed. Fixes this leak on spi bus driver spi-bcm2835.c when transfer size is greater than 65532: Kmemleak: sg_alloc_table+0x28/0xc8 spi_map_buf+0xa4/0x300 __spi_pump_messages+0x370/0x748 __spi_sync+0x1d4/0x270 spi_sync+0x34/0x58 spi_test_execute_msg+0x60/0x340 [spi_loopback_test] spi_test_run_iter+0x548/0x578 [spi_loopback_test] spi_test_run_test+0x94/0x140 [spi_loopback_test] spi_test_run_tests+0x150/0x180 [spi_loopback_test] spi_loopback_test_probe+0x50/0xd0 [spi_loopback_test] spi_drv_probe+0x84/0xe0 Signed-off-by: Gustav Wiklander <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mark Brown <[email protected]> Signed-off-by: Sasha Levin <[email protected]> (cherry picked from commit a8c374444f68ed71a71e14bd6da1e4b13d8c8f19)
1 parent ddbc3e9 commit e32475c

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

drivers/spi/spi.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,8 +1125,6 @@ static int spi_transfer_one_message(struct spi_controller *ctlr,
11251125
if (msg->status && ctlr->handle_err)
11261126
ctlr->handle_err(ctlr, msg);
11271127

1128-
spi_res_release(ctlr, msg);
1129-
11301128
spi_finalize_current_message(ctlr);
11311129

11321130
return ret;
@@ -1384,6 +1382,13 @@ void spi_finalize_current_message(struct spi_controller *ctlr)
13841382

13851383
spi_unmap_msg(ctlr, mesg);
13861384

1385+
/* In the prepare_messages callback the spi bus has the opportunity to
1386+
* split a transfer to smaller chunks.
1387+
* Release splited transfers here since spi_map_msg is done on the
1388+
* splited transfers.
1389+
*/
1390+
spi_res_release(ctlr, mesg);
1391+
13871392
if (ctlr->cur_msg_prepared && ctlr->unprepare_message) {
13881393
ret = ctlr->unprepare_message(ctlr, mesg);
13891394
if (ret) {

0 commit comments

Comments
 (0)