From patchwork Sun Sep 14 14:45:06 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Epler X-Patchwork-Id: 4901521 Return-Path: X-Original-To: patchwork-linux-spi@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 09945BEEA6 for ; Sun, 14 Sep 2014 15:06:41 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 3C2C12018E for ; Sun, 14 Sep 2014 15:06:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 598FC2020E for ; Sun, 14 Sep 2014 15:06:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752610AbaINPGj (ORCPT ); Sun, 14 Sep 2014 11:06:39 -0400 Received: from rrcs-76-79-27-186.west.biz.rr.com ([76.79.27.186]:50299 "EHLO rrcs-76-79-27-186.west.biz.rr.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752595AbaINPGi (ORCPT ); Sun, 14 Sep 2014 11:06:38 -0400 Received: from jepler by zaphod.unpythonic.net with local (Exim 4.80) (envelope-from ) id 1XTB36-0005RO-EC; Sun, 14 Sep 2014 09:45:08 -0500 From: Jeff Epler To: linux@vger.kernel.org, linux-spi@vger.kernel.org Subject: [RFC 2/4] spidev: Avoid runtime memory allocations Date: Sun, 14 Sep 2014 09:45:06 -0500 Message-Id: <1410705908-20847-3-git-send-email-jepler@unpythonic.net> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1410705908-20847-1-git-send-email-jepler@unpythonic.net> References: <1410705908-20847-1-git-send-email-jepler@unpythonic.net> Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP allocating bounce and k_xfers at every iteration was a source of excess delays. --- drivers/spi/spidev.c | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/drivers/spi/spidev.c b/drivers/spi/spidev.c index 2e0655d..dda7632 100644 --- a/drivers/spi/spidev.c +++ b/drivers/spi/spidev.c @@ -85,6 +85,8 @@ struct spidev_data { struct mutex buf_lock; unsigned users; u8 *buffer; + struct spi_ioc_transfer *bounce; + struct spi_transfer *k_xfers; }; static LIST_HEAD(device_list); @@ -227,9 +229,9 @@ static int spidev_message(struct spidev_data *spidev, int status = -EFAULT; spi_message_init(&msg); - k_xfers = kcalloc(n_xfers, sizeof(*k_tmp), GFP_KERNEL); - if (k_xfers == NULL) - return -ENOMEM; + if (n_xfers * sizeof(*k_tmp) > bufsiz) + return -ENOMEM; + k_xfers = spidev->k_xfers; /* Construct spi_message, copying any tx data to bounce buffer. * We walk the array of user-provided transfers, using each one @@ -302,7 +304,6 @@ static int spidev_message(struct spidev_data *spidev, status = total; done: - kfree(k_xfers); return status; } @@ -451,8 +452,12 @@ spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) if (n_ioc == 0) break; + if(tmp > bufsiz) { + retval = -EINVAL; + break; + } /* copy into scratch area */ - ioc = kmalloc(tmp, GFP_KERNEL); + ioc = spidev->bounce; if (!ioc) { retval = -ENOMEM; break; @@ -465,7 +470,6 @@ spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) /* translate to spi_message, execute */ retval = spidev_message(spidev, ioc, n_ioc); - kfree(ioc); break; } @@ -504,6 +508,19 @@ static int spidev_open(struct inode *inode, struct file *filp) dev_dbg(&spidev->spi->dev, "open/ENOMEM\n"); status = -ENOMEM; } + spidev->bounce = kmalloc(bufsiz, GFP_KERNEL); + if (!spidev->bounce) { + dev_dbg(&spidev->spi->dev, "open/ENOMEM\n"); + kfree(spidev->buffer); + status = -ENOMEM; + } + spidev->k_xfers = kmalloc(bufsiz, GFP_KERNEL); + if (!spidev->k_xfers) { + dev_dbg(&spidev->spi->dev, "open/ENOMEM\n"); + kfree(spidev->buffer); + kfree(spidev->bounce); + status = -ENOMEM; + } } if (status == 0) { spidev->users++; @@ -534,6 +551,12 @@ static int spidev_release(struct inode *inode, struct file *filp) kfree(spidev->buffer); spidev->buffer = NULL; + kfree(spidev->bounce); + spidev->bounce = NULL; + + kfree(spidev->k_xfers); + spidev->k_xfers = NULL; + /* ... after we unbound from the underlying device? */ spin_lock_irq(&spidev->spi_lock); dofree = (spidev->spi == NULL);