Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp2871557rwb; Mon, 19 Sep 2022 11:12:39 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7sl0DQ1q0M9xMUaY5iirjGs4efEGGXiXfa+C9ZOubSsdirIrwgvxQFXOFD2SiEPBgxO8DP X-Received: by 2002:a63:e105:0:b0:438:b084:78ad with SMTP id z5-20020a63e105000000b00438b08478admr16576774pgh.391.1663611159006; Mon, 19 Sep 2022 11:12:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663611158; cv=none; d=google.com; s=arc-20160816; b=AdPNpyDEACg3zkag75tzLJSlz9xtctmfYaVF1aTkkPpCs2FRZ3KHfyiFOLA65iy7Pb +WvWGDV7AxPjMELYyAmfq48oQAQcMrkpwKFMwLWbIY2VIh0FcKk7J1nllvO/QvEMsnJr jUHxY+CmVSA8khgicI/GxOoDB7LxxHdsJFm/e5KOh7CdDDPxKXH3TUtHOe5WbGaBL4od broPXWvLchaE7gFMfs5KYLAE5Wom2UhvMswlprs7u7nfAsmL0PPGBWVvo3VV9WEkxvqA EtmsBvgFyCMDQqlb6H0+W4mWwWFOE3W5J36JNL7BdW912NjyvbOUXZPvQuv9iXl06Alg gPnA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=O+qhD5l3KrpEipBQdIfKAcg93m0YOyH180T6ICcDcIU=; b=R8BuGn2J9t9EDceRHREleR4LwVDg90150Cj6Cot/Y7RJDEkp+Vdr5Tz+PpHazaLhkV 4lMpHc5qFdbClIylKdRkbBNd6rdfxVasZGb9o/5IIlti6OpEhdL6eZPKRwALqY9KjaB4 x6zDZ2foQFjftUdA8v8dIi8M9vepri/quy4wz5l+uvmGBxY0hHT9hhQMGu49jkHNrg+d 0D8CSR+XZhSMECZ+Ouy9niNYxAj7Q2E1WzwRigP+uMtzjB8cAx0Ax6OeUntCoUmatH3l f6oNi7VIbc3a06ROVPTeNI14gFCIuXGvm8RK068sTKCK7d1/I51JSW8FJEl+zolkKXFT 7+Ng== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n6-20020a170903110600b001789fa8f90esi5061219plh.408.2022.09.19.11.12.26; Mon, 19 Sep 2022 11:12:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230255AbiISRdh (ORCPT + 99 others); Mon, 19 Sep 2022 13:33:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230076AbiISRdf (ORCPT ); Mon, 19 Sep 2022 13:33:35 -0400 Received: from smtp.smtpout.orange.fr (smtp-21.smtpout.orange.fr [80.12.242.21]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AFB143D594 for ; Mon, 19 Sep 2022 10:33:33 -0700 (PDT) Received: from [192.168.1.18] ([90.11.190.129]) by smtp.orange.fr with ESMTPA id aKeAoPhYzAOp2aKeAoyF6y; Mon, 19 Sep 2022 19:33:31 +0200 X-ME-Helo: [192.168.1.18] X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Mon, 19 Sep 2022 19:33:31 +0200 X-ME-IP: 90.11.190.129 Message-ID: Date: Mon, 19 Sep 2022 19:33:30 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: Re: [PATCH] Input: applespi - avoid wasting some memory Content-Language: en-US To: Johan Hovold Cc: Dmitry Torokhov , linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, linux-input@vger.kernel.org References: <0db94f84920663f3bd45a73e2ae73950627a377f.1663506472.git.christophe.jaillet@wanadoo.fr> From: Christophe JAILLET In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Le 19/09/2022 à 09:02, Johan Hovold a écrit : > On Sun, Sep 18, 2022 at 03:08:17PM +0200, Christophe JAILLET wrote: >> 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; > > This kind of change is generally broken in case DMA can be involved. > > Allocating the transfer buffers separately makes sure that alignment > requirements are met and avoids hard-to-debug memory corruption issues. > > Johan > Got it. I'll keep away from it. Thanks for the feed-back and explanation. CJ