Received: by 2002:a05:7412:40d:b0:e2:908c:2ebd with SMTP id 13csp10733rdf; Mon, 20 Nov 2023 14:28:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IG45vQ0o2HIe7EVmjz5/TyqSUgTW7XasU85ak7NvzKesGn8juSdjnbj0Fvt3Z+vKIvLjIPt X-Received: by 2002:a17:90a:d242:b0:285:3486:7fe8 with SMTP id o2-20020a17090ad24200b0028534867fe8mr1221053pjw.10.1700519327973; Mon, 20 Nov 2023 14:28:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700519327; cv=none; d=google.com; s=arc-20160816; b=S2HF6/Rz0W6k303MSnLBqszP1MSH0HVPghU3WXkagsaLqeFnN5gXHmWm8SU3/D6777 B27kM+Yd078AN8gFH8zIB70XZHSRkVzjucjFryCKumSrCLWaU9PNy010eex8d+BRnEV9 WSFAuK9vbbksBfbalTyPR3bgng5hpCualdMsllmELejkJVLZLSB3LvT9ZGeG83Fl8ns7 yu55uwmaoA4zcKEhX0fntASWh8RwB6jQC02U0393viYKDjzOCgTwbP9ViYvkyl+EpBkx NbO4B+zZIx2MJ/UBKQLmcKR7xtJGDBWDYtdi1EO7+zcvBTziDcqXAsE7NqhDwqYsYI6E CKtA== 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 :dkim-signature; bh=le1DTxFwJBwSLVczHlEbJ1qmGMhisFi+cnB/Mpt44S4=; fh=HQQk3d4NoxI9SpUlktd+4fu8zJW7NOgR72W71jUdFhg=; b=B/HCNdqi0OqV1Mt1lo03tBpG2klCX/OunzBb1D/OfJPxPwB7GwZcthFKAzsLlQn01U +O7LKtPFnZ2B97G/a1ILtYXq3ms15yr8fAC/JuQQd6WXH+kB4x6Ak9B6+2NRR30kxSGw wCNovnfbJZqCpKh+PCG24zODNnD0ORc5+fdMcwW1GMskfKUOZ2A8aOydcSdgADKIVxNZ lS1e2VEykQYUloGTmyhxPm3O2eBtx+GbLcseLc/KPrSQOmKOHSZt12f0/+tG5C3bwAwC YPxvjUWLtt7Xq6kJmugtMRQFKFoFnZqyLFo7vi1cyiUIv5yXpei+jDRi8KtIm+xCe0S0 6YQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=EBiHsOdD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id q36-20020a631f64000000b0059f0cebd04csi8639418pgm.722.2023.11.20.14.28.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Nov 2023 14:28:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=EBiHsOdD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id B9D13802C5B8; Mon, 20 Nov 2023 14:28:46 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232547AbjKTW2o (ORCPT + 99 others); Mon, 20 Nov 2023 17:28:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229952AbjKTW2m (ORCPT ); Mon, 20 Nov 2023 17:28:42 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3161497 for ; Mon, 20 Nov 2023 14:28:39 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E2BF6C433C7; Mon, 20 Nov 2023 22:28:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700519318; bh=bXCpdr2eVuRieBZCu88Dpd9XKIAOtGduXyMB0bsi/HE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EBiHsOdD3KFE8GxtQyGOtwJhdbYDQjLSx0xBZSXl51cmJVzogfiMVkEyyP7PPpEO9 xWHVCPPxW1dHmLOjzgmKL2NC7B979wXZ6sOxh+wbDp1Hv/EdERIH/fq7cjjx3xHBsK rwGI1ixxwnCkJxpxkzgwy35DIlR9IC2mHrJTFmrrrGQxiid4BJ+yViM59Y5/UVWlGd pTKP3Rs4G0oWgvOHuG0e/X++HLOyGqN+2VW5b3MRH0Lvm8rcTAZUFA27650gv0fagv ut/siDNbDzj217yHO+Nlosn++OjCQWQMIlT1e4oY7mfnFX8BRxv3nxK96WL/OWzazz 4/zqjeRsj5rxA== From: Masahiro Yamada To: Linus Walleij , Bartosz Golaszewski , linux-gpio@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada Subject: [PATCH 1/2] pinctrl: pinconf-generic: resize the pin config array directly Date: Tue, 21 Nov 2023 07:28:31 +0900 Message-Id: <20231120222832.4063882-2-masahiroy@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231120222832.4063882-1-masahiroy@kernel.org> References: <20231120222832.4063882-1-masahiroy@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 20 Nov 2023 14:28:46 -0800 (PST) pinconf_generic_parse_dt_config() allocates memory that is large enough to contain all the config parameters. Then, kmemdup() copies the found configs to the memory with the exact size. There is no need to allocate memory twice; you can directly resize the initial memory using krealloc_array(). I also changed kcalloc() to kmalloc_array() to keep the consistency with krealloc_array(). This change has no impact because you do not need to zero out the 'cfg' array. Signed-off-by: Masahiro Yamada --- drivers/pinctrl/pinconf-generic.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/drivers/pinctrl/pinconf-generic.c b/drivers/pinctrl/pinconf-generic.c index 8313cb5f3b3c..ba4fe2466e78 100644 --- a/drivers/pinctrl/pinconf-generic.c +++ b/drivers/pinctrl/pinconf-generic.c @@ -247,7 +247,6 @@ int pinconf_generic_parse_dt_config(struct device_node *np, { unsigned long *cfg; unsigned int max_cfg, ncfg = 0; - int ret; if (!np) return -EINVAL; @@ -256,7 +255,7 @@ int pinconf_generic_parse_dt_config(struct device_node *np, max_cfg = ARRAY_SIZE(dt_params); if (pctldev) max_cfg += pctldev->desc->num_custom_params; - cfg = kcalloc(max_cfg, sizeof(*cfg), GFP_KERNEL); + cfg = kmalloc_array(max_cfg, sizeof(*cfg), GFP_KERNEL); if (!cfg) return -ENOMEM; @@ -266,30 +265,22 @@ int pinconf_generic_parse_dt_config(struct device_node *np, parse_dt_cfg(np, pctldev->desc->custom_params, pctldev->desc->num_custom_params, cfg, &ncfg); - ret = 0; - /* no configs found at all */ if (ncfg == 0) { + kfree(cfg); *configs = NULL; *nconfigs = 0; - goto out; + return 0; } - /* - * Now limit the number of configs to the real number of - * found properties. - */ - *configs = kmemdup(cfg, ncfg * sizeof(unsigned long), GFP_KERNEL); - if (!*configs) { - ret = -ENOMEM; - goto out; - } + /* Now resize the array to store the real number of found properties. */ + *configs = krealloc_array(cfg, ncfg, sizeof(unsigned long), GFP_KERNEL); + if (!*configs) + return -ENOMEM; *nconfigs = ncfg; -out: - kfree(cfg); - return ret; + return 0; } EXPORT_SYMBOL_GPL(pinconf_generic_parse_dt_config); -- 2.40.1