From patchwork Fri Sep 2 20:02:59 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heiner Kallweit X-Patchwork-Id: 9311589 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C7F3860760 for ; Fri, 2 Sep 2016 20:03:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B931329858 for ; Fri, 2 Sep 2016 20:03:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AE2E32985A; Fri, 2 Sep 2016 20:03:49 +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=-5.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID 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 1237C29858 for ; Fri, 2 Sep 2016 20:03:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932803AbcIBUDs (ORCPT ); Fri, 2 Sep 2016 16:03:48 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:33596 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932750AbcIBUDr (ORCPT ); Fri, 2 Sep 2016 16:03:47 -0400 Received: by mail-wm0-f68.google.com with SMTP id w207so4164872wmw.0 for ; Fri, 02 Sep 2016 13:03:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:subject:to:references:cc:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=Lvb4hldcq8saRQbjwuQu+QM4M62eDyPDuwlkG3j0Uis=; b=mhyBkTD1+vOlu4IOCKu6C8wpt4yn3v+xatmyi7TA3V7WzzqdUT95dsbteiBen7mcQS RpBS5AprBsyOZm4Bf6nP4QhdRCidm58oEs1s6NszS3Q4WzzvUI8Ifi7FITiuOhYgzWp4 tA8KPeNEK7qtQg7K1UuoL3JAd1gCYy6v5vF6RxxK749ACVlW1oWTiGNe8/+dUKCAw4aw hjjTwc0x1tOboQJgDyB9T+qXYEdqJ2/1S3ETu9So7S12YKNLmvaW5X+GB3End+M+y6F2 HMWVE4qHVu62p8Z32bMdWWvBI/6Md1Rb/380hjem79Gbh1R8ffCmWhsWaVulhlf1Cjqj GBuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:subject:to:references:cc:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=Lvb4hldcq8saRQbjwuQu+QM4M62eDyPDuwlkG3j0Uis=; b=KKIPKKCx+8UvywBCU+d66wOsY9RPyzB+1zd6w6Qnt1fv8FAlBcg0iAkCRIuzKVySTp 6od/5c1RFXDJ8HBr6d+HLEEshWQKmwZbbNaWedAQYs576ePH1YIMtG7ZpXKRVK7NbzAH JE14GuzuRg+aPyx23N5CdniYVD7gx6E4cKamfxc2uQUYMMdRG+qpon2BtkoCQ7M1gChZ VRXppOsLMG9/TmRSnwF4ukM5DWf9AV8RoJq4JJfl+Gdmnp6LbkwcyLlCpqxagyKObJ/6 KGpfCHBtNu0LJ7VL4NrhoYR+3Fy/Ay0ECoZF4uV27GodPypVyQrL++hf3ENUEfJ5H19N IOUw== X-Gm-Message-State: AE9vXwONL2/Mik1R0UtiNd6p3g5X2PgnnC+nOrOjP9VCe567Uxmw9HAVEJ+v0cOODCTUyw== X-Received: by 10.194.60.2 with SMTP id d2mr22515908wjr.179.1472846625928; Fri, 02 Sep 2016 13:03:45 -0700 (PDT) Received: from ?IPv6:2003:62:5f43:9500:39ed:a1fb:9a43:e02d? (p200300625F43950039EDA1FB9A43E02D.dip0.t-ipconnect.de. [2003:62:5f43:9500:39ed:a1fb:9a43:e02d]) by smtp.googlemail.com with ESMTPSA id w8sm11946062wjj.45.2016.09.02.13.03.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 02 Sep 2016 13:03:45 -0700 (PDT) From: Heiner Kallweit Subject: [PATCH v2 03/23] spi: fsl-espi: pre-allocate message buffer To: Mark Brown References: Cc: "linux-spi@vger.kernel.org" , Gabor Juhos Message-ID: Date: Fri, 2 Sep 2016 22:02:59 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 MIME-Version: 1.0 In-Reply-To: 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 Currently the driver allocates a 64kb buffer with each single message. On systems with little and fragmented memory this can result in memory allocation errors. Solve this by pre-allocating a buffer. This patch was developed in OpenWRT long ago, however it never made it upstream. Author: Gabor Juhos I slightly modified the original patch to re-initialize the buffer at the beginning of each transfer. Signed-off-by: Heiner Kallweit --- v2: - changed order of patches 2 and 3 to do the trivial changes first - fixed coding style issue - fixed commit message --- drivers/spi/spi-fsl-espi.c | 41 ++++++++++++++++++----------------------- drivers/spi/spi-fsl-lib.h | 1 + 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/drivers/spi/spi-fsl-espi.c b/drivers/spi/spi-fsl-espi.c index a9004fe..a3a75ae 100644 --- a/drivers/spi/spi-fsl-espi.c +++ b/drivers/spi/spi-fsl-espi.c @@ -300,57 +300,44 @@ static void fsl_espi_do_trans(struct spi_message *m, static void fsl_espi_cmd_trans(struct spi_message *m, struct fsl_espi_transfer *trans, u8 *rx_buff) { + struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master); struct spi_transfer *t; - u8 *local_buf; int i = 0; struct fsl_espi_transfer *espi_trans = trans; - local_buf = kzalloc(SPCOM_TRANLEN_MAX, GFP_KERNEL); - if (!local_buf) { - espi_trans->status = -ENOMEM; - return; - } - list_for_each_entry(t, &m->transfers, transfer_list) { if (t->tx_buf) { - memcpy(local_buf + i, t->tx_buf, t->len); + memcpy(mspi->local_buf + i, t->tx_buf, t->len); i += t->len; } } - espi_trans->tx_buf = local_buf; - espi_trans->rx_buf = local_buf; + espi_trans->tx_buf = mspi->local_buf; + espi_trans->rx_buf = mspi->local_buf; fsl_espi_do_trans(m, espi_trans); espi_trans->actual_length = espi_trans->len; - kfree(local_buf); } static void fsl_espi_rw_trans(struct spi_message *m, struct fsl_espi_transfer *trans, u8 *rx_buff) { + struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master); struct spi_transfer *t; - u8 *local_buf; unsigned int tx_only = 0; int i = 0; - local_buf = kzalloc(SPCOM_TRANLEN_MAX, GFP_KERNEL); - if (!local_buf) { - trans->status = -ENOMEM; - return; - } - list_for_each_entry(t, &m->transfers, transfer_list) { if (t->tx_buf) { - memcpy(local_buf + i, t->tx_buf, t->len); + memcpy(mspi->local_buf + i, t->tx_buf, t->len); i += t->len; if (!t->rx_buf) tx_only += t->len; } } - trans->tx_buf = local_buf; - trans->rx_buf = local_buf; + trans->tx_buf = mspi->local_buf; + trans->rx_buf = mspi->local_buf; fsl_espi_do_trans(m, trans); if (!trans->status) { @@ -360,18 +347,19 @@ static void fsl_espi_rw_trans(struct spi_message *m, trans->len - tx_only); trans->actual_length += trans->len; } - - kfree(local_buf); } static int fsl_espi_do_one_msg(struct spi_master *master, struct spi_message *m) { + struct mpc8xxx_spi *mspi = spi_master_get_devdata(master); struct spi_transfer *t; u8 *rx_buf = NULL; unsigned int xfer_len = 0; struct fsl_espi_transfer espi_trans; + memset(mspi->local_buf, 0, SPCOM_TRANLEN_MAX); + list_for_each_entry(t, &m->transfers, transfer_list) { if (t->rx_buf) rx_buf = t->rx_buf; @@ -617,6 +605,13 @@ static struct spi_master * fsl_espi_probe(struct device *dev, mpc8xxx_spi = spi_master_get_devdata(master); + mpc8xxx_spi->local_buf = + devm_kmalloc(dev, SPCOM_TRANLEN_MAX, GFP_KERNEL); + if (!mpc8xxx_spi->local_buf) { + ret = -ENOMEM; + goto err_probe; + } + mpc8xxx_spi->reg_base = devm_ioremap_resource(dev, mem); if (IS_ERR(mpc8xxx_spi->reg_base)) { ret = PTR_ERR(mpc8xxx_spi->reg_base); diff --git a/drivers/spi/spi-fsl-lib.h b/drivers/spi/spi-fsl-lib.h index 84f5dcb..065b9db 100644 --- a/drivers/spi/spi-fsl-lib.h +++ b/drivers/spi/spi-fsl-lib.h @@ -30,6 +30,7 @@ struct mpc8xxx_spi { void *rx; #if IS_ENABLED(CONFIG_SPI_FSL_ESPI) int len; + u8 *local_buf; #endif int subblock;