diff mbox series

[v2] spi: spi-mem: check if data buffers are on stack

Message ID 20220420102022.3310970-1-p.yadav@ti.com
State Accepted
Commit 8868c03f3ca584abec7bb53d6c3f7c5ab6b60949
Headers show
Series [v2] spi: spi-mem: check if data buffers are on stack | expand

Commit Message

Pratyush Yadav April 20, 2022, 10:20 a.m. UTC
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>
Acked-by: Mark Brown <broonie@kernel.org>
---

Changes in v2:
- Include task_stack.h. It might not get included indirectly on some
  platforms and can cause build failures.
- Add a WARN_ON_ONCE() for debuggability.
- Add Mark's Ack.

 drivers/spi/spi-mem.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

--
2.34.1
diff mbox series

Patch

diff --git a/drivers/spi/spi-mem.c b/drivers/spi/spi-mem.c
index 7d7091aa0c22..e8de4f5017cd 100644
--- a/drivers/spi/spi-mem.c
+++ b/drivers/spi/spi-mem.c
@@ -10,6 +10,7 @@ 
 #include <linux/pm_runtime.h>
 #include <linux/spi/spi.h>
 #include <linux/spi/spi-mem.h>
+#include <linux/sched/task_stack.h>

 #include "internals.h"

@@ -211,6 +212,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 (WARN_ON_ONCE(op->data.dir == SPI_MEM_DATA_IN &&
+			 object_is_on_stack(op->data.buf.in)))
+		return -EINVAL;
+
+	if (WARN_ON_ONCE(op->data.dir == SPI_MEM_DATA_OUT &&
+			 object_is_on_stack(op->data.buf.out)))
+		return -EINVAL;
+
 	return 0;
 }