Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp3853203imm; Mon, 20 Aug 2018 05:58:43 -0700 (PDT) X-Google-Smtp-Source: AA+uWPweFdPIfNeET/xUGpieaFgQqWje3mxCNl8JwoDbXTWQZrTxUMw4YwvmB5bZwk0PQB1lGd2v X-Received: by 2002:a17:902:6f10:: with SMTP id w16-v6mr26277530plk.216.1534769923720; Mon, 20 Aug 2018 05:58:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534769923; cv=none; d=google.com; s=arc-20160816; b=NmVMENwjh9c+qCqqhgmszhqjsbi7AHS2fy+nK8kAhwKGt2vAi8hxhxpgyaS0gMd80X DbPCOmFKsMCTbYPiL/2O5vMTOCSGEpnMW3b1VAsvOTtEBF+8boih1P+XvBMUHOtsvr5/ fryab77NIeZ6aziKAwXT84UjP3lgKXROWXhUoLD5j78xX4AgLxVS1rVqUqsiYiMGlbcM PkcBIYW9Zp59Vu8iEKNiNHoUY4vTAeukRvHieSA89aL3vGxhbKmNQuyWk9x8h8bW612R Bcxur1BkDRDebuAcM5200pve1z1DBll+OVBQr1FTlIf41IjvIMxCAPtomjI1+0lr41Yn wyDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=+JWpRwmklD3JHWlOO4k/hp+ALH+L9mYSPz7bHW1xowk=; b=P7f14F63t88xATc6p/2zmqjr5JGdAYRMc8WVMqW8Pm03ebKW6BKynm17BAkWkm1ICz LXNVnY9dFZyq1oo69Gio8RBRhIrv8dcyibtNghEwoJwjE2QOFmhmKq7CLeWc8Xu6bWgE gna3UcR4fKks3l63HLEy9ANq5NpxcG9P/w8JzUE5Qupq2XmktXJo1Bm9P1bbHkFzEjQ1 iH5BC2c07VkP6u+dpFkK7/ZPsdsn4yHIRcRTo2MIRVWsWlrLYqgz2jBnHlyR0jjEMLeV DQFAt1xV3eeIEQvt+bwVSfNg7YFxfM92lmm/4F6k/eL3utZpYO31N2PDY157O2Bkp5bs pQ5A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d11-v6si9413863pgi.221.2018.08.20.05.58.28; Mon, 20 Aug 2018 05:58:43 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726842AbeHTP7W (ORCPT + 99 others); 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-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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); -- 2.7.4