From patchwork Sun Dec 2 03:54:25 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 1829971 Return-Path: X-Original-To: patchwork-spi-devel-general@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from lists.sourceforge.net (lists.sourceforge.net [216.34.181.88]) by patchwork1.kernel.org (Postfix) with ESMTP id A0C5D400ED for ; Sun, 2 Dec 2012 03:54:44 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=sfs-ml-2.v29.ch3.sourceforge.com) by sfs-ml-2.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1Tf0dd-0005Kw-Ug; Sun, 02 Dec 2012 03:54:41 +0000 Received: from sog-mx-2.v43.ch3.sourceforge.com ([172.29.43.192] helo=mx.sourceforge.net) by sfs-ml-2.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1Tf0dd-0005Kr-5q for spi-devel-general@lists.sourceforge.net; Sun, 02 Dec 2012 03:54:41 +0000 X-ACL-Warn: Received: from opensource.wolfsonmicro.com ([80.75.67.52]) by sog-mx-2.v43.ch3.sourceforge.com with esmtps (TLSv1:AES256-SHA:256) (Exim 4.76) id 1Tf0db-0004B1-NK for spi-devel-general@lists.sourceforge.net; Sun, 02 Dec 2012 03:54:41 +0000 Received: from finisterre.wolfsonmicro.main (unknown [175.126.181.142]) by opensource.wolfsonmicro.com (Postfix) with ESMTPSA id 728FE75000C; Sun, 2 Dec 2012 03:54:32 +0000 (GMT) Received: from broonie by finisterre.wolfsonmicro.main with local (Exim 4.80) (envelope-from ) id 1Tf0dQ-0007dN-Nu; Sun, 02 Dec 2012 12:54:28 +0900 From: Mark Brown To: Grant Likely Subject: [PATCH] spi: Remove SPI_BUFSIZ restriction on spi_write_then_read() Date: Sun, 2 Dec 2012 12:54:25 +0900 Message-Id: <1354420465-29315-1-git-send-email-broonie@opensource.wolfsonmicro.com> X-Mailer: git-send-email 1.7.10.4 X-Spam-Score: -0.4 (/) X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -0.4 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain X-Headers-End: 1Tf0db-0004B1-NK Cc: spi-devel-general@lists.sourceforge.net, Mark Brown , linux-kernel@vger.kernel.org X-BeenThere: spi-devel-general@lists.sourceforge.net X-Mailman-Version: 2.1.9 Precedence: list List-Id: Linux SPI core/device drivers discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: spi-devel-general-bounces@lists.sourceforge.net In order to avoid constantly allocating and deallocating there is a fixed buffer which spi_write_then_read() uses for transfers, with an early error check to ensure that the transfer fits within the buffer. This limits the size of transfers to this size, currently max(32, SMP_CACHE_BYTES). Since we can dynamically allocate and in fact already have a fallback to do so when there is contention for the fixed buffer remove this restriction and instead dynamically allocate a suitably sized buffer if the transfer won't fit. Signed-off-by: Mark Brown Acked-by: Grant Likely --- drivers/spi/spi.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index c4f7d71..224b7bc 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -1646,12 +1646,18 @@ int spi_write_then_read(struct spi_device *spi, struct spi_transfer x[2]; u8 *local_buf; - /* Use preallocated DMA-safe buffer. We can't avoid copying here, - * (as a pure convenience thing), but we can keep heap costs - * out of the hot path ... + /* Use preallocated DMA-safe buffer if we can. We can't avoid + * copying here, (as a pure convenience thing), but we can + * keep heap costs out of the hot path unless someone else is + * using the pre-allocated buffer or the transfer is too large. */ - if ((n_tx + n_rx) > SPI_BUFSIZ) - return -EINVAL; + if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) { + local_buf = kmalloc(max(SPI_BUFSIZ, n_tx + n_rx), GFP_KERNEL); + if (!local_buf) + return -ENOMEM; + } else { + local_buf = buf; + } spi_message_init(&message); memset(x, 0, sizeof x); @@ -1664,14 +1670,6 @@ int spi_write_then_read(struct spi_device *spi, spi_message_add_tail(&x[1], &message); } - /* ... unless someone else is using the pre-allocated buffer */ - if (!mutex_trylock(&lock)) { - local_buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); - if (!local_buf) - return -ENOMEM; - } else - local_buf = buf; - memcpy(local_buf, txbuf, n_tx); x[0].tx_buf = local_buf; x[1].rx_buf = local_buf + n_tx;