From patchwork Sun Sep 18 13:08:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe JAILLET X-Patchwork-Id: 12979461 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D2F77C6FA82 for ; Sun, 18 Sep 2022 13:08:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229567AbiIRNIX (ORCPT ); Sun, 18 Sep 2022 09:08:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229550AbiIRNIW (ORCPT ); Sun, 18 Sep 2022 09:08:22 -0400 Received: from smtp.smtpout.orange.fr (smtp06.smtpout.orange.fr [80.12.242.128]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C46BE1F63E for ; Sun, 18 Sep 2022 06:08:20 -0700 (PDT) Received: from pop-os.home ([90.11.190.129]) by smtp.orange.fr with ESMTPA id Zu1yoxokbiBgAZu1yoePn2; Sun, 18 Sep 2022 15:08:18 +0200 X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sun, 18 Sep 2022 15:08:18 +0200 X-ME-IP: 90.11.190.129 From: Christophe JAILLET To: Dmitry Torokhov Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET , linux-input@vger.kernel.org Subject: [PATCH] Input: applespi - avoid wasting some memory Date: Sun, 18 Sep 2022 15:08:17 +0200 Message-Id: <0db94f84920663f3bd45a73e2ae73950627a377f.1663506472.git.christophe.jaillet@wanadoo.fr> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org When the 'struct applespi_data' structure is allocated at the beginning of applespi_probe(), 2504 bytes are allocated. Because of the way memory is allocated, it ends to a 4096 bytes allocation. So, about 1500 bytes are wasted. Later in this function, when 'tx_buffer', 'tx_status', 'rx_buffer' and 'msg_buf' are allocated, 256, 4, 256 and 512 bytes are requested (~1 ko). A devm_ memory allocation has a small overhead of 40 bytes. So, for the same reason as above, it ends to allocate 512, 64, 512 and 1024 (~2 ko). All that said, defining these 4 arrays as part of 'struct applespi_data' saves 2 ko of runtime memory. 3504 bytes are now requested, and 4096 really allocated. All these 4 arrays fit in the 'wasted' memory of the first allocation. Signed-off-by: Christophe JAILLET --- Compile tested only. --- drivers/input/keyboard/applespi.c | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/drivers/input/keyboard/applespi.c b/drivers/input/keyboard/applespi.c index fab5473ae5da..bee4ccfa2b05 100644 --- a/drivers/input/keyboard/applespi.c +++ b/drivers/input/keyboard/applespi.c @@ -373,11 +373,11 @@ struct applespi_data { struct input_dev *keyboard_input_dev; struct input_dev *touchpad_input_dev; - u8 *tx_buffer; - u8 *tx_status; - u8 *rx_buffer; + u8 tx_buffer[APPLESPI_PACKET_SIZE]; + u8 tx_status[APPLESPI_STATUS_SIZE]; + u8 rx_buffer[APPLESPI_PACKET_SIZE]; - u8 *msg_buf; + u8 msg_buf[MAX_PKTS_PER_MSG * APPLESPI_PACKET_SIZE]; unsigned int saved_msg_len; struct applespi_tp_info tp_info; @@ -1659,21 +1659,6 @@ static int applespi_probe(struct spi_device *spi) /* store the driver data */ spi_set_drvdata(spi, applespi); - /* create our buffers */ - applespi->tx_buffer = devm_kmalloc(&spi->dev, APPLESPI_PACKET_SIZE, - GFP_KERNEL); - applespi->tx_status = devm_kmalloc(&spi->dev, APPLESPI_STATUS_SIZE, - GFP_KERNEL); - applespi->rx_buffer = devm_kmalloc(&spi->dev, APPLESPI_PACKET_SIZE, - GFP_KERNEL); - applespi->msg_buf = devm_kmalloc_array(&spi->dev, MAX_PKTS_PER_MSG, - APPLESPI_PACKET_SIZE, - GFP_KERNEL); - - if (!applespi->tx_buffer || !applespi->tx_status || - !applespi->rx_buffer || !applespi->msg_buf) - return -ENOMEM; - /* set up our spi messages */ applespi_setup_read_txfrs(applespi); applespi_setup_write_txfrs(applespi);