From patchwork Wed Sep 7 05:46:33 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heiner Kallweit X-Patchwork-Id: 9318199 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 B04F860869 for ; Wed, 7 Sep 2016 05:46:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A0F9929018 for ; Wed, 7 Sep 2016 05:46:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 95A092901D; Wed, 7 Sep 2016 05:46: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=-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 6F8E82901C for ; Wed, 7 Sep 2016 05:46:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750841AbcIGFqp (ORCPT ); Wed, 7 Sep 2016 01:46:45 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:33051 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750820AbcIGFqp (ORCPT ); Wed, 7 Sep 2016 01:46:45 -0400 Received: by mail-wm0-f68.google.com with SMTP id w207so1149571wmw.0 for ; Tue, 06 Sep 2016 22:46:44 -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=m6IOie/1mm+KD348xF1DX3ndvCSJIqocFag+relTDIo=; b=IwOyXMc5BKnkYKsVQBZThHoqEmJtRDtv9bJOyMt3R1KM2Ly4kG1/KsH8hgQGVfM+5F pCi6PRZvkATYHDcosPKtgIJ9/CcTyIA7R5GnpEhEl1AGukCkyORUs2Idegcp9cQPpSA3 bn1UsArXB7DI7lunhyonDNcp5WwNONTdiYOnOPxByQru9k7/4BP7+p1ZJ5fPhDptJcP+ TIRVCcKKX5yw1i+nokFSuXW/JvaW1yMoXkNxwg5BYQc9xJJmi1Xz8nPFKFd1plXDSCR+ EUQOKaBV51JZcVI8l6ExAPRqbjefhrG4KRhHPAABwT9cPvEqmhqZyGnxK/qC1BBgbi1H dzIA== 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=m6IOie/1mm+KD348xF1DX3ndvCSJIqocFag+relTDIo=; b=ETfo5ifA9j2UtbQ0013pTeq7B0u6awMGrl2K8aPEwVx4ZgbpyUn6qd4uttcfBRvLrb 9Uhs84iu4WhVQ51BN8aPM0maZHnn2ajQK28A0lhOsIVeKLV3iZlo+Zz0DBMpWx85hxmV ot6754c3u6BomFJunQmOVzEvHpLDi2yu9l3o7zz3p+zqTlwqpBU3Pq387se7oyuDP3OC wah3zL2WNITfWmCkiT26kx6dfelBkTNeHN2mgwe9Cv0UzxdlTyP2ifA6q8mU5LvJsjkz A7rYhREvj8AT3mJm2R4K4+0dhguHPap+LiIcFExN7fRjcSKJzYREevL+gVdFVt/ncCLM 3XxA== X-Gm-Message-State: AE9vXwNnek73IueaJwLN3npDcjBYjzumFPmQ894rkhGEZinrT4PyLJiDjl0pI2FPDvB4OA== X-Received: by 10.195.11.104 with SMTP id eh8mr39118577wjd.128.1473227203431; Tue, 06 Sep 2016 22:46:43 -0700 (PDT) Received: from ?IPv6:2003:62:5f43:9500:244b:3b01:c694:b85d? (p200300625F439500244B3B01C694B85D.dip0.t-ipconnect.de. [2003:62:5f43:9500:244b:3b01:c694:b85d]) by smtp.googlemail.com with ESMTPSA id bk7sm33432298wjc.36.2016.09.06.22.46.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Sep 2016 22:46:42 -0700 (PDT) From: Heiner Kallweit Subject: [PATCH v3 04/18] spi: fsl-espi: pre-allocate message buffer To: Mark Brown References: Cc: "linux-spi@vger.kernel.org" , Gabor Juhos Message-ID: <14371aa8-17f2-69a2-99d4-cd095a87545c@gmail.com> Date: Wed, 7 Sep 2016 07:46:33 +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 for 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. I slightly modified the original patch to re-initialize the buffer at the beginning of each transfer. Signed-off-by: Gabor Juhos Signed-off-by: Felix Fietkau Signed-off-by: Heiner Kallweit --- v2: - fixed coding style issue - fixed commit message v3: - changed Signed-off-by to include original author --- 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;