All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] mtd: devices: m25p80: Make sure the buffer passed in op is DMA-able
@ 2018-09-17 14:31 Boris Brezillon
  2018-09-18  7:59 ` Jarkko Nikula
  0 siblings, 1 reply; 3+ messages in thread
From: Boris Brezillon @ 2018-09-17 14:31 UTC (permalink / raw)
  To: David Woodhouse, Brian Norris, Boris Brezillon, Marek Vasut,
	Richard Weinberger, linux-mtd
  Cc: Jarkko Nikula, stable

As documented in spi-mem.h, spi_mem_op->data.buf.{in,out} must be
DMA-able, and commit 4120f8d158ef ("mtd: spi-nor: Use the spi_mem_xx()
API") failed to follow this rule as buffers passed to
->{read,write}_reg() are usually placed on the stack.

Fix that by allocating a scratch buffer and copying the data around.

Fixes: 4120f8d158ef ("mtd: spi-nor: Use the spi_mem_xx() API")
Reported-by: Jarkko Nikula <jarkko.nikula@linux.intel.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Boris Brezillon <boris.brezillon@bootlin.com>
---
Note that the ->{read,write}() path is still buggy since nothing
guarantees that buffers passed by the MTD layer to the SPI NOR layer
are DMA-able, but this is a long-standing issue which we'll have to
address at the spi-nor level (this layer can choose the bounce buffer
size based on nor->page_size).

Changes in v2:
- Copy the data from scratchbuf in the ->read_reg() path
---
 drivers/mtd/devices/m25p80.c | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)

diff --git a/drivers/mtd/devices/m25p80.c b/drivers/mtd/devices/m25p80.c
index cbfafc453274..270d3c9580c5 100644
--- a/drivers/mtd/devices/m25p80.c
+++ b/drivers/mtd/devices/m25p80.c
@@ -39,13 +39,23 @@ static int m25p80_read_reg(struct spi_nor *nor, u8 code, u8 *val, int len)
 	struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(code, 1),
 					  SPI_MEM_OP_NO_ADDR,
 					  SPI_MEM_OP_NO_DUMMY,
-					  SPI_MEM_OP_DATA_IN(len, val, 1));
+					  SPI_MEM_OP_DATA_IN(len, NULL, 1));
+	void *scratchbuf;
 	int ret;
 
+	scratchbuf = kmalloc(len, GFP_KERNEL);
+	if (!scratchbuf)
+		return -ENOMEM;
+
+	op.data.buf.in = scratchbuf;
 	ret = spi_mem_exec_op(flash->spimem, &op);
 	if (ret < 0)
 		dev_err(&flash->spimem->spi->dev, "error %d reading %x\n", ret,
 			code);
+	else
+		memcpy(val, scratchbuf, len);
+
+	kfree(scratchbuf);
 
 	return ret;
 }
@@ -56,9 +66,19 @@ static int m25p80_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
 	struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 1),
 					  SPI_MEM_OP_NO_ADDR,
 					  SPI_MEM_OP_NO_DUMMY,
-					  SPI_MEM_OP_DATA_OUT(len, buf, 1));
+					  SPI_MEM_OP_DATA_OUT(len, NULL, 1));
+	void *scratchbuf;
+	int ret;
 
-	return spi_mem_exec_op(flash->spimem, &op);
+	scratchbuf = kmemdup(buf, len, GFP_KERNEL);
+	if (!scratchbuf)
+		return -ENOMEM;
+
+	op.data.buf.out = scratchbuf;
+	ret = spi_mem_exec_op(flash->spimem, &op);
+	kfree(scratchbuf);
+
+	return ret;
 }
 
 static ssize_t m25p80_write(struct spi_nor *nor, loff_t to, size_t len,
-- 
2.14.1

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

* Re: [PATCH v2] mtd: devices: m25p80: Make sure the buffer passed in op is DMA-able
  2018-09-17 14:31 [PATCH v2] mtd: devices: m25p80: Make sure the buffer passed in op is DMA-able Boris Brezillon
@ 2018-09-18  7:59 ` Jarkko Nikula
  2018-09-18  9:18   ` Boris Brezillon
  0 siblings, 1 reply; 3+ messages in thread
From: Jarkko Nikula @ 2018-09-18  7:59 UTC (permalink / raw)
  To: Boris Brezillon, David Woodhouse, Brian Norris, Marek Vasut,
	Richard Weinberger, linux-mtd
  Cc: stable

On 09/17/2018 05:31 PM, Boris Brezillon wrote:
> As documented in spi-mem.h, spi_mem_op->data.buf.{in,out} must be
> DMA-able, and commit 4120f8d158ef ("mtd: spi-nor: Use the spi_mem_xx()
> API") failed to follow this rule as buffers passed to
> ->{read,write}_reg() are usually placed on the stack.
> 
> Fix that by allocating a scratch buffer and copying the data around.
> 
> Fixes: 4120f8d158ef ("mtd: spi-nor: Use the spi_mem_xx() API")
> Reported-by: Jarkko Nikula <jarkko.nikula@linux.intel.com>
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Boris Brezillon <boris.brezillon@bootlin.com>
> ---
> Note that the ->{read,write}() path is still buggy since nothing
> guarantees that buffers passed by the MTD layer to the SPI NOR layer
> are DMA-able, but this is a long-standing issue which we'll have to
> address at the spi-nor level (this layer can choose the bounce buffer
> size based on nor->page_size).
> 
> Changes in v2:
> - Copy the data from scratchbuf in the ->read_reg() path
> ---
>   drivers/mtd/devices/m25p80.c | 26 +++++++++++++++++++++++---
>   1 file changed, 23 insertions(+), 3 deletions(-)
> 
Tested-by: Jarkko Nikula <jarkko.nikula@linux.intel.com>
Reviewed-by: Jarkko Nikula <jarkko.nikula@linux.intel.com>

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

* Re: [PATCH v2] mtd: devices: m25p80: Make sure the buffer passed in op is DMA-able
  2018-09-18  7:59 ` Jarkko Nikula
@ 2018-09-18  9:18   ` Boris Brezillon
  0 siblings, 0 replies; 3+ messages in thread
From: Boris Brezillon @ 2018-09-18  9:18 UTC (permalink / raw)
  To: Jarkko Nikula
  Cc: David Woodhouse, Brian Norris, Marek Vasut, Richard Weinberger,
	linux-mtd, stable

On Tue, 18 Sep 2018 10:59:17 +0300
Jarkko Nikula <jarkko.nikula@linux.intel.com> wrote:

> On 09/17/2018 05:31 PM, Boris Brezillon wrote:
> > As documented in spi-mem.h, spi_mem_op->data.buf.{in,out} must be
> > DMA-able, and commit 4120f8d158ef ("mtd: spi-nor: Use the spi_mem_xx()
> > API") failed to follow this rule as buffers passed to  
> > ->{read,write}_reg() are usually placed on the stack.  
> > 
> > Fix that by allocating a scratch buffer and copying the data around.
> > 
> > Fixes: 4120f8d158ef ("mtd: spi-nor: Use the spi_mem_xx() API")
> > Reported-by: Jarkko Nikula <jarkko.nikula@linux.intel.com>
> > Cc: <stable@vger.kernel.org>
> > Signed-off-by: Boris Brezillon <boris.brezillon@bootlin.com>
> > ---
> > Note that the ->{read,write}() path is still buggy since nothing
> > guarantees that buffers passed by the MTD layer to the SPI NOR layer
> > are DMA-able, but this is a long-standing issue which we'll have to
> > address at the spi-nor level (this layer can choose the bounce buffer
> > size based on nor->page_size).
> > 
> > Changes in v2:
> > - Copy the data from scratchbuf in the ->read_reg() path
> > ---
> >   drivers/mtd/devices/m25p80.c | 26 +++++++++++++++++++++++---
> >   1 file changed, 23 insertions(+), 3 deletions(-)
> >   
> Tested-by: Jarkko Nikula <jarkko.nikula@linux.intel.com>
> Reviewed-by: Jarkko Nikula <jarkko.nikula@linux.intel.com>

Queued to mtd/master. Thanks for testing and reviewing the patch.

Regards,

Boris

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

end of thread, other threads:[~2018-09-18 14:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-09-17 14:31 [PATCH v2] mtd: devices: m25p80: Make sure the buffer passed in op is DMA-able Boris Brezillon
2018-09-18  7:59 ` Jarkko Nikula
2018-09-18  9:18   ` Boris Brezillon

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.