From patchwork Sun Nov 20 17:19:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe JAILLET X-Patchwork-Id: 23437 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp1203826wrr; Sun, 20 Nov 2022 09:28:37 -0800 (PST) X-Google-Smtp-Source: AA0mqf54t8CUbBaxIZ1rR8unCfDp+GWd0AIT7zRwoSxDDwwgXqX5fGkgGM9LtBJ8fTicrjcWIcXw X-Received: by 2002:a17:90b:4ccd:b0:218:b47b:9b69 with SMTP id nd13-20020a17090b4ccd00b00218b47b9b69mr948978pjb.240.1668965317438; Sun, 20 Nov 2022 09:28:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668965317; cv=none; d=google.com; s=arc-20160816; b=FzWswkdxbjNAR5MkQbHGHe7ygU9XnkPh/MMFWwkMwAHu5YxyVtnby4CmDwn6K+Wtlr A6Pn7A2esRKj6DDBtIGHBlxtMaIJHoE1JYpNozMexWy2UH3StIUj/kqSaaeuUUO4HXwo zhXkx2IQBhmTRQzPzBM0ekhzChylZfoLrBCA2jkVIUnzHw4VQaThJ4qNqNab/JJ8tNCN s57OT6zY534A5bs59w0QvRip0IrHqdciII+g4tTxsj8HWmoDpO+mhXVWHk/p2CFwGdud ipaGvOGYA1TSbyCRD2FkvDL4SvhlYLgfpmR4XbHPTxdj3wSGVk1sCN6PuqLsjYPG3YPY Uouw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=Rq28k7Zgu7nq9gZV5fOkB70i2zUb87JVOHG/4N+r1hU=; b=XUA4xiGkiOPWL1UGfz09Qc9D3dkPbNTWVv/GwBOuXkKdqpn2s8zd/tGYunfMb4xpMm MkXuVKcEb5hbm74EzSQzmqPu7BblWgOKPYRvul/dczAWrJfCW/88dbCtJTe5Zr9bFR4P tcDr1uBChgLiKbUW3JMYfwpNrOiaXr3hX5JgupDYFXR+cRQM7zOsESDqUaRodb4q0hgv apWqijLJ/tdUyLgyQA/D2SGkXOVjy07zIwCuyUgWwLVQCEHmUP33GCRcN/O1azyQc4Vc SW8Y4r3ITaHkqYWH2AXZ3SD07kD0PERYKfjTtvx/f2WoGzj1yWsdA7sKANChasD0n7rb +tEw== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gp20-20020a17090adf1400b0020b0b8df3d2si7686677pjb.57.2022.11.20.09.28.23; Sun, 20 Nov 2022 09:28:37 -0800 (PST) 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 S229558AbiKTRTM (ORCPT + 99 others); Sun, 20 Nov 2022 12:19:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229553AbiKTRTK (ORCPT ); Sun, 20 Nov 2022 12:19:10 -0500 Received: from smtp.smtpout.orange.fr (smtp-21.smtpout.orange.fr [80.12.242.21]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C3D0C23168 for ; Sun, 20 Nov 2022 09:19:09 -0800 (PST) Received: from pop-os.home ([86.243.100.34]) by smtp.orange.fr with ESMTPA id wnyBohc3Ubw2uwnyFoEd9Y; Sun, 20 Nov 2022 18:19:07 +0100 X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sun, 20 Nov 2022 18:19:07 +0100 X-ME-IP: 86.243.100.34 From: Christophe JAILLET To: "Rafael J. Wysocki" , Viresh Kumar , Thierry Reding , Jonathan Hunter Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET , linux-pm@vger.kernel.org, linux-tegra@vger.kernel.org Subject: [PATCH] cpufreq: tegra186: Use flexible array to simplify memory allocation Date: Sun, 20 Nov 2022 18:19:02 +0100 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750036976685430162?= X-GMAIL-MSGID: =?utf-8?q?1750036976685430162?= Use flexible array to simplify memory allocation. It saves some memory, avoids an indirection when reading the 'clusters' array and removes some LoC. Detailed explanation: ==================== Knowing that: - each devm_ allocation over-allocates 40 bytes for internal needs - Some rounding is done by the memory allocator on 8, 16, 32, 64, 96, 128, 192, 256, 512, 1024, 2048, 4096, 8192 boundaries and that: - sizeof(struct tegra186_cpufreq_data) = 24 - sizeof(struct tegra186_cpufreq_cluster) = 16 Memory allocations in tegra186_cpufreq_probe() are: data: (24 + 40) = 64 => 64 bytes data->clusters: (2 * 16 + 40) = 72 => 96 bytes So a total of 160 bytes are allocated. 56 for the real need, 80 for internal uses and 24 are wasted. If 'struct tegra186_cpufreq_data' is reordered so that 'clusters' is a flexible array: - it saves one pointer in the structure - only one allocation is needed So, only 96 bytes are allocated: 16 + 2 * 16 + 40 = 88 => 96 bytes Signed-off-by: Christophe JAILLET --- Compile tested only --- drivers/cpufreq/tegra186-cpufreq.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/drivers/cpufreq/tegra186-cpufreq.c b/drivers/cpufreq/tegra186-cpufreq.c index 6c88827f4e62..f98f53bf1011 100644 --- a/drivers/cpufreq/tegra186-cpufreq.c +++ b/drivers/cpufreq/tegra186-cpufreq.c @@ -65,8 +65,8 @@ struct tegra186_cpufreq_cluster { struct tegra186_cpufreq_data { void __iomem *regs; - struct tegra186_cpufreq_cluster *clusters; const struct tegra186_cpufreq_cpu *cpus; + struct tegra186_cpufreq_cluster clusters[]; }; static int tegra186_cpufreq_init(struct cpufreq_policy *policy) @@ -221,15 +221,12 @@ static int tegra186_cpufreq_probe(struct platform_device *pdev) struct tegra_bpmp *bpmp; unsigned int i = 0, err; - data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); + data = devm_kzalloc(&pdev->dev, + struct_size(data, clusters, TEGRA186_NUM_CLUSTERS), + GFP_KERNEL); if (!data) return -ENOMEM; - data->clusters = devm_kcalloc(&pdev->dev, TEGRA186_NUM_CLUSTERS, - sizeof(*data->clusters), GFP_KERNEL); - if (!data->clusters) - return -ENOMEM; - data->cpus = tegra186_cpus; bpmp = tegra_bpmp_get(&pdev->dev);