linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] spi: spi-mem: check if data buffers are on stack
@ 2022-01-31 11:45 Pratyush Yadav
  2022-01-31 13:48 ` Miquel Raynal
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Pratyush Yadav @ 2022-01-31 11:45 UTC (permalink / raw)
  To: Mark Brown
  Cc: Pratyush Yadav, Tudor Ambarus, Michael Walle, Miquel Raynal,
	Takahiro Kuwano, linux-spi, linux-kernel

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 <p.yadav@ti.com>
---
 drivers/spi/spi-mem.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/drivers/spi/spi-mem.c b/drivers/spi/spi-mem.c
index 37f4443ce9a0..b3793a2979ee 100644
--- a/drivers/spi/spi-mem.c
+++ b/drivers/spi/spi-mem.c
@@ -207,6 +207,15 @@ static int spi_mem_check_op(const struct spi_mem_op *op)
 	    !spi_mem_buswidth_is_valid(op->data.buswidth))
 		return -EINVAL;
 
+	/* Buffers must be DMA-able. */
+	if (op->data.dir == SPI_MEM_DATA_IN &&
+	    object_is_on_stack(op->data.buf.in))
+		return -EINVAL;
+
+	if (op->data.dir == SPI_MEM_DATA_OUT &&
+	    object_is_on_stack(op->data.buf.out))
+		return -EINVAL;
+
 	return 0;
 }
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-02-01  8:02 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-01-31 11:45 [PATCH] spi: spi-mem: check if data buffers are on stack Pratyush Yadav
2022-01-31 13:48 ` Miquel Raynal
2022-01-31 13:55 ` Mark Brown
2022-01-31 17:07   ` Pratyush Yadav
2022-01-31 14:49 ` kernel test robot
2022-02-01  7:44 ` Tudor.Ambarus
2022-02-01  8:02   ` Pratyush Yadav

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).