]> git.baikalelectronics.ru Git - kernel.git/commitdiff
spi: spi-mem: check if data buffers are on stack
authorPratyush Yadav <p.yadav@ti.com>
Wed, 20 Apr 2022 10:20:22 +0000 (15:50 +0530)
committerMark Brown <broonie@kernel.org>
Thu, 21 Apr 2022 13:17:56 +0000 (14:17 +0100)
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>
Link: https://lore.kernel.org/r/20220420102022.3310970-1-p.yadav@ti.com
Signed-off-by: Mark Brown <broonie@kernel.org>
drivers/spi/spi-mem.c

index 0e8dafc62d94ff886f92e52c61287c8f3041ff37..60c968c1dc6e115fafac02a311781bc4e6d32b78 100644 (file)
@@ -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;
 }