linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mtd: m25p80: consider max message size when use the spi_mem_xx() API
@ 2018-08-20 12:43 Chuanhua Han
  2018-08-20 13:01 ` David Laight
  0 siblings, 1 reply; 7+ messages in thread
From: Chuanhua Han @ 2018-08-20 12:43 UTC (permalink / raw)
  To: broonie, boris.brezillon
  Cc: linux-spi, linux-kernel, stable, jiafei.pan, zhiqiang.hou, Chuanhua Han

Signed-off-by: Chuanhua Han <chuanhua.han@nxp.com>
---
Changes in v3:
    Rename variable name "val" to "opcode_addr_dummy_sum".
    Place the legitimacy of the transfer size(i.e., "pi_max_message_size(mem->spi)" and
"opcode_addr_dummy_sum") into "if (! ctlr - > mem_ops | |! ctlr-> mem_ops->exec_op) {"
structure and add "spi_max_transfer_size(mem->spi) and opcode_addr_dummy_sum".
    Adjust the formatting alignment of your code.
    "(unsigned long)op->data.nbytes" was modified to "(unsigned long)(op->data.nbytes)".

Fixes: c36ff266dc82 ("spi: Extend the core to ease integration of SPI memory controllers")
---
 drivers/spi/spi-mem.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/drivers/spi/spi-mem.c b/drivers/spi/spi-mem.c
index 990770d..5ec2bc9 100644
--- a/drivers/spi/spi-mem.c
+++ b/drivers/spi/spi-mem.c
@@ -328,10 +328,24 @@ EXPORT_SYMBOL_GPL(spi_mem_exec_op);
 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
 {
 	struct spi_controller *ctlr = mem->spi->controller;
+	unsigned long opcode_addr_dummy_sum = sizeof(op->cmd.opcode) +
+					      op->addr.nbytes +
+					      op->dummy.nbytes;
 
 	if (ctlr->mem_ops && ctlr->mem_ops->adjust_op_size)
 		return ctlr->mem_ops->adjust_op_size(mem, op);
 
+	if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) {
+		if (spi_max_message_size(mem->spi) < opcode_addr_dummy_sum ||
+		    spi_max_transfer_size(mem->spi) < opcode_addr_dummy_sum)
+			return -EINVAL;
+
+		op->data.nbytes = min3((unsigned long)(op->data.nbytes),
+				       spi_max_transfer_size(mem->spi),
+				       spi_max_message_size(mem->spi) -
+				       opcode_addr_dummy_sum);
+	}
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(spi_mem_adjust_op_size);
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 7+ messages in thread
* [PATCH] mtd: m25p80: consider max message size when use the spi_mem_xx() API
@ 2018-08-20  9:43 Chuanhua Han
  2018-08-20 10:15 ` Boris Brezillon
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Chuanhua Han @ 2018-08-20  9:43 UTC (permalink / raw)
  To: broonie, boris.brezillon
  Cc: linux-spi, linux-kernel, stable, jiafei.pan, zhiqiang.hou, Chuanhua Han

Signed-off-by: Chuanhua Han <chuanhua.han@nxp.com>
---
Changes in v2:
	- Place the adjusted transfer bytes code in spi_mem_adjust_op_size()
	and  check spi_max_message_size(mem->spi) value before subtracting 
	opcode, addr and dummy bytes.
*fixes:
	spi: Extend the core to ease integration of SPI memory controllers
---
 drivers/spi/spi-mem.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/drivers/spi/spi-mem.c b/drivers/spi/spi-mem.c
index 990770d..f5e75d1 100644
--- a/drivers/spi/spi-mem.c
+++ b/drivers/spi/spi-mem.c
@@ -328,10 +328,21 @@ EXPORT_SYMBOL_GPL(spi_mem_exec_op);
 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
 {
 	struct spi_controller *ctlr = mem->spi->controller;
+	unsigned long val =	sizeof(op->cmd.opcode) +
+							  op->addr.nbytes +
+							  op->dummy.nbytes;
 
 	if (ctlr->mem_ops && ctlr->mem_ops->adjust_op_size)
 		return ctlr->mem_ops->adjust_op_size(mem, op);
 
+	if (spi_max_message_size(mem->spi) < val)
+		return -EINVAL;
+
+	if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op)
+		op->data.nbytes = min3((unsigned long)op->data.nbytes,
+				spi_max_transfer_size(mem->spi),
+				spi_max_message_size(mem->spi) - val);
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(spi_mem_adjust_op_size);
-- 
2.7.4


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

end of thread, other threads:[~2018-08-20 17:13 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-08-20 12:43 [PATCH] mtd: m25p80: consider max message size when use the spi_mem_xx() API Chuanhua Han
2018-08-20 13:01 ` David Laight
2018-08-20 13:54   ` Boris Brezillon
  -- strict thread matches above, loose matches on Subject: below --
2018-08-20  9:43 Chuanhua Han
2018-08-20 10:15 ` Boris Brezillon
2018-08-20 10:23 ` kbuild test robot
2018-08-20 17:13 ` Greg KH

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