From patchwork Sun Sep 4 07:58:02 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heiner Kallweit X-Patchwork-Id: 9312407 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 BAEDC600CA for ; Sun, 4 Sep 2016 08:07:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AA229287DA for ; Sun, 4 Sep 2016 08:07:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9EEB9288F0; Sun, 4 Sep 2016 08:07:32 +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=-4.4 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 79922287DA for ; Sun, 4 Sep 2016 08:07:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750922AbcIDIHa (ORCPT ); Sun, 4 Sep 2016 04:07:30 -0400 Received: from mail-wm0-f67.google.com ([74.125.82.67]:33253 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752022AbcIDIH1 (ORCPT ); Sun, 4 Sep 2016 04:07:27 -0400 Received: by mail-wm0-f67.google.com with SMTP id w207so8683181wmw.0 for ; Sun, 04 Sep 2016 01:07:26 -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=Lq1dM6skuujTPZLMlkKVvMCFL1XPNjIcuJJ+hQOpiKs=; b=c+FyGHs/EOBMwAPXC2z38JOzcrVNYZEQYgY1aoEYJ/EAhamoziVL8v0BYSCXZu2eh7 fa4uZ95QX8HA0ZqRmO1q9bQu/14w4JsFIsU07vI1nngSMLnkgkSqlYvhqDPR5U1YMpW4 xAusO/G9O/x3pu6YiKXrGrlZpi7oOsvYDws87XLA4MKzC7OZVhuwnWpSCKAAchfD4aBj /fpA2idvYv3S0jeYOU2nO+F4ItrZ8bksD82V9jTiEbQXQz+prR5OzhTbyqTN/nWHhY78 4kbjvGEIzfBwEoCvkbBCTwL3jtqd5GZEPvOcCi/sGcWphMJJ3u690+X5W81vwlo8gOCS ceNQ== 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=Lq1dM6skuujTPZLMlkKVvMCFL1XPNjIcuJJ+hQOpiKs=; b=moeSkpXmVERdSrv8h5yfoPZOgU7FUGRA64jLcCNuJZeVj6wlzLKYzCpr9UASjbDOXG G/UdSDgx2++rzLGKtphbpDLW2x5OQCCB6YRzll9Zt51zxDEu0z/+ldrcBiLGwQLcRcdT rNRxBoGuTI3T+A4W1NBaww+4J/JvcK02tQ+FHBtnX7Wz1lA7F9pn2Wrn/lFrgbEumZca D26e44LBkzXn3UaRFfpM+pQtKSeDXXYj7nTCGAe02Gd6gpWCvExGQxU4V/iKmyy7f3Xi z2nBNcoDiIZgkJI72QyVIatjOIytUTaFBgaQp6PLMC6INuB5RqlGMiD3VUQe9McUU8+N KSrw== X-Gm-Message-State: AE9vXwN+DVs+LOVKcBj2jIGkEDAKttYSX0lRNYne3glGYhWFSqpE6/kS7ql24y0yWw4Iwg== X-Received: by 10.28.57.4 with SMTP id g4mr7701088wma.75.1472976446056; Sun, 04 Sep 2016 01:07:26 -0700 (PDT) Received: from ?IPv6:2003:62:5f43:9500:c1f5:c528:a3d:f8c4? (p200300625F439500C1F5C5280A3DF8C4.dip0.t-ipconnect.de. [2003:62:5f43:9500:c1f5:c528:a3d:f8c4]) by smtp.googlemail.com with ESMTPSA id 137sm7184678wmi.16.2016.09.04.01.07.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Sep 2016 01:07:25 -0700 (PDT) From: Heiner Kallweit Subject: [PATCH v2 04/18] spi: fsl-espi: pre-allocate message buffer To: Mark Brown References: Cc: "linux-spi@vger.kernel.org" , Gabor Juhos Message-ID: <0e60b4b5-5016-e1c3-d29d-b81a41f64f48@gmail.com> Date: Sun, 4 Sep 2016 09:58:02 +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: - 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;