From patchwork Mon Aug 20 12:43:33 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuanhua Han X-Patchwork-Id: 10570301 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9ECE7920 for ; Mon, 20 Aug 2018 12:43:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8E9562902E for ; Mon, 20 Aug 2018 12:43:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 82B1129033; Mon, 20 Aug 2018 12:43:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0E50C2902F for ; Mon, 20 Aug 2018 12:43:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726788AbeHTP7W (ORCPT ); Mon, 20 Aug 2018 11:59:22 -0400 Received: from inva021.nxp.com ([92.121.34.21]:56872 "EHLO inva021.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726197AbeHTP7W (ORCPT ); Mon, 20 Aug 2018 11:59:22 -0400 Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 2D95920003D; Mon, 20 Aug 2018 14:43:51 +0200 (CEST) Received: from invc005.ap-rdc01.nxp.com (invc005.ap-rdc01.nxp.com [165.114.16.14]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 6C6F320000C; Mon, 20 Aug 2018 14:43:47 +0200 (CEST) Received: from mega.am.freescale.net (mega.ap.freescale.net [10.192.208.232]) by invc005.ap-rdc01.nxp.com (Postfix) with ESMTP id 7613A40339; Mon, 20 Aug 2018 20:43:42 +0800 (SGT) From: Chuanhua Han To: broonie@kernel.org, boris.brezillon@bootlin.com Cc: linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, jiafei.pan@nxp.com, zhiqiang.hou@nxp.com, Chuanhua Han Subject: [PATCH] mtd: m25p80: consider max message size when use the spi_mem_xx() API Date: Mon, 20 Aug 2018 20:43:33 +0800 Message-Id: <1534769013-43449-1-git-send-email-chuanhua.han@nxp.com> X-Mailer: git-send-email 2.7.4 X-Virus-Scanned: ClamAV using ClamSMTP Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Chuanhua Han --- 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);