linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] spi: spi-mem: check if data buffers are on stack
@ 2022-04-20 10:20 Pratyush Yadav
  2022-04-20 13:04 ` Mark Brown
  2022-04-21 15:29 ` Mark Brown
  0 siblings, 2 replies; 5+ messages in thread
From: Pratyush Yadav @ 2022-04-20 10:20 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>
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(+)

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;
 }

--
2.34.1


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

* Re: [PATCH v2] spi: spi-mem: check if data buffers are on stack
  2022-04-20 10:20 [PATCH v2] spi: spi-mem: check if data buffers are on stack Pratyush Yadav
@ 2022-04-20 13:04 ` Mark Brown
  2022-04-21  7:10   ` Pratyush Yadav
  2022-04-21 15:29 ` Mark Brown
  1 sibling, 1 reply; 5+ messages in thread
From: Mark Brown @ 2022-04-20 13:04 UTC (permalink / raw)
  To: Pratyush Yadav
  Cc: Tudor Ambarus, Michael Walle, Miquel Raynal, Takahiro Kuwano,
	linux-spi, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 808 bytes --]

On Wed, Apr 20, 2022 at 03:50:22PM +0530, Pratyush Yadav wrote:
> 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.

Since this is now an isolated patch does that mean whatever meant that I
acked rather than applying this patch has gone in and I can now apply it
directly?

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH v2] spi: spi-mem: check if data buffers are on stack
  2022-04-20 13:04 ` Mark Brown
@ 2022-04-21  7:10   ` Pratyush Yadav
  2022-04-21  7:14     ` Miquel Raynal
  0 siblings, 1 reply; 5+ messages in thread
From: Pratyush Yadav @ 2022-04-21  7:10 UTC (permalink / raw)
  To: Mark Brown
  Cc: Tudor Ambarus, Michael Walle, Miquel Raynal, Takahiro Kuwano,
	linux-spi, linux-kernel

On 20/04/22 02:04PM, Mark Brown wrote:
> On Wed, Apr 20, 2022 at 03:50:22PM +0530, Pratyush Yadav wrote:
> > 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.
> 
> Since this is now an isolated patch does that mean whatever meant that I
> acked rather than applying this patch has gone in and I can now apply it
> directly?

Yes, you should be able to apply it directly. Miquel's spi-mem-ecc 
branch is now in 5.18-rc1 and later. Though this patch never conflicted 
with that branch to begin with. The spi-mem-ecc branch does not touch 
spi_mem_check_op() as far as I can see.

-- 
Regards,
Pratyush Yadav
Texas Instruments Inc.

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

* Re: [PATCH v2] spi: spi-mem: check if data buffers are on stack
  2022-04-21  7:10   ` Pratyush Yadav
@ 2022-04-21  7:14     ` Miquel Raynal
  0 siblings, 0 replies; 5+ messages in thread
From: Miquel Raynal @ 2022-04-21  7:14 UTC (permalink / raw)
  To: Pratyush Yadav
  Cc: Mark Brown, Tudor Ambarus, Michael Walle, Takahiro Kuwano,
	linux-spi, linux-kernel

Hey,

p.yadav@ti.com wrote on Thu, 21 Apr 2022 12:40:56 +0530:

> On 20/04/22 02:04PM, Mark Brown wrote:
> > On Wed, Apr 20, 2022 at 03:50:22PM +0530, Pratyush Yadav wrote:  
> > > 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.  
> > 
> > Since this is now an isolated patch does that mean whatever meant that I
> > acked rather than applying this patch has gone in and I can now apply it
> > directly?  
> 
> Yes, you should be able to apply it directly. Miquel's spi-mem-ecc 
> branch is now in 5.18-rc1 and later. Though this patch never conflicted 
> with that branch to begin with. The spi-mem-ecc branch does not touch 
> spi_mem_check_op() as far as I can see.

It did during a few version AFAIR, but not the final ones anymore.

But it's upstream now, so please feel free to apply.

Thanks,
Miquèl

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

* Re: [PATCH v2] spi: spi-mem: check if data buffers are on stack
  2022-04-20 10:20 [PATCH v2] spi: spi-mem: check if data buffers are on stack Pratyush Yadav
  2022-04-20 13:04 ` Mark Brown
@ 2022-04-21 15:29 ` Mark Brown
  1 sibling, 0 replies; 5+ messages in thread
From: Mark Brown @ 2022-04-21 15:29 UTC (permalink / raw)
  To: p.yadav
  Cc: tudor.ambarus, linux-spi, tkuw584924, michael, miquel.raynal,
	linux-kernel

On Wed, 20 Apr 2022 15:50:22 +0530, Pratyush Yadav wrote:
> 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.
> 
> 

Applied to

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-next

Thanks!

[1/1] spi: spi-mem: check if data buffers are on stack
      commit: 8868c03f3ca584abec7bb53d6c3f7c5ab6b60949

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark

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

end of thread, other threads:[~2022-04-21 15:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-20 10:20 [PATCH v2] spi: spi-mem: check if data buffers are on stack Pratyush Yadav
2022-04-20 13:04 ` Mark Brown
2022-04-21  7:10   ` Pratyush Yadav
2022-04-21  7:14     ` Miquel Raynal
2022-04-21 15:29 ` Mark Brown

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).