Skip to content

Commit 8868c03

Browse files
Pratyush Yadavbroonie
authored andcommitted
spi: spi-mem: check if data buffers are on stack
The buffers passed in the data phase must be DMA-able. Programmers often don't realise this requirement and pass in buffers that reside on the stack. This can be hard to spot when reviewing code. Reject ops if their data buffer is on the stack to avoid this. Signed-off-by: Pratyush Yadav <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 8c235cc commit 8868c03

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

drivers/spi/spi-mem.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/pm_runtime.h>
1111
#include <linux/spi/spi.h>
1212
#include <linux/spi/spi-mem.h>
13+
#include <linux/sched/task_stack.h>
1314

1415
#include "internals.h"
1516

@@ -211,6 +212,15 @@ static int spi_mem_check_op(const struct spi_mem_op *op)
211212
!spi_mem_buswidth_is_valid(op->data.buswidth))
212213
return -EINVAL;
213214

215+
/* Buffers must be DMA-able. */
216+
if (WARN_ON_ONCE(op->data.dir == SPI_MEM_DATA_IN &&
217+
object_is_on_stack(op->data.buf.in)))
218+
return -EINVAL;
219+
220+
if (WARN_ON_ONCE(op->data.dir == SPI_MEM_DATA_OUT &&
221+
object_is_on_stack(op->data.buf.out)))
222+
return -EINVAL;
223+
214224
return 0;
215225
}
216226

0 commit comments

Comments
 (0)