From nobody Thu Dec 18 07:11:48 2025 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 1EEF4C54E76 for ; Mon, 20 Nov 2023 22:28:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232463AbjKTW2o (ORCPT ); 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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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-ge= neric.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 =3D 0; - int ret; =20 if (!np) return -EINVAL; @@ -256,7 +255,7 @@ int pinconf_generic_parse_dt_config(struct device_node = *np, max_cfg =3D ARRAY_SIZE(dt_params); if (pctldev) max_cfg +=3D pctldev->desc->num_custom_params; - cfg =3D kcalloc(max_cfg, sizeof(*cfg), GFP_KERNEL); + cfg =3D kmalloc_array(max_cfg, sizeof(*cfg), GFP_KERNEL); if (!cfg) return -ENOMEM; =20 @@ -266,30 +265,22 @@ int pinconf_generic_parse_dt_config(struct device_nod= e *np, parse_dt_cfg(np, pctldev->desc->custom_params, pctldev->desc->num_custom_params, cfg, &ncfg); =20 - ret =3D 0; - /* no configs found at all */ if (ncfg =3D=3D 0) { + kfree(cfg); *configs =3D NULL; *nconfigs =3D 0; - goto out; + return 0; } =20 - /* - * Now limit the number of configs to the real number of - * found properties. - */ - *configs =3D kmemdup(cfg, ncfg * sizeof(unsigned long), GFP_KERNEL); - if (!*configs) { - ret =3D -ENOMEM; - goto out; - } + /* Now resize the array to store the real number of found properties. */ + *configs =3D krealloc_array(cfg, ncfg, sizeof(unsigned long), GFP_KERNEL); + if (!*configs) + return -ENOMEM; =20 *nconfigs =3D ncfg; =20 -out: - kfree(cfg); - return ret; + return 0; } EXPORT_SYMBOL_GPL(pinconf_generic_parse_dt_config); =20 --=20 2.40.1 From nobody Thu Dec 18 07:11:48 2025 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 3A372C2BB3F for ; Mon, 20 Nov 2023 22:28:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232637AbjKTW2q (ORCPT ); Mon, 20 Nov 2023 17:28:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232539AbjKTW2n (ORCPT ); Mon, 20 Nov 2023 17:28:43 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87CCD97 for ; Mon, 20 Nov 2023 14:28:40 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 402C8C433CA; Mon, 20 Nov 2023 22:28:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700519320; bh=lm7/gVjjO8J4rGGfH0O5RParZfMnIY0MkOc5Mwd5oTw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nJEQgwWOw+C7D8EhdvrVMZGCGeW51hRnSqEgzzkovWfmgQZMVejOyWW90TicHb9cR gSuTbi++TH6DeMRHW/0c5TzfwIwNeGFQI1O05cgt+tppgBXm5jGFvRdjVgl7CEQPSG j34REcfh1n/ag9wx3CAbciOYaMpeSuTe626Yp/Sb2pNWrJWSJBYXuFiwTDY4NGDxjh I/i0WB+FRhUzRsrM2jO4NFPJpU4YMC2hdETD5U/ddBm1Ld6E0JV0RSvHSttknPFIbs /rxAFZzE2O7NnLymcPwM08tbl4uXprn6CswuwexRlXliDejuQl35/tfySjABuERuhH KRpgUP7nAUk3A== From: Masahiro Yamada To: Linus Walleij , Bartosz Golaszewski , linux-gpio@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada Subject: [PATCH 2/2] pinctrl: pinconf-generic: remove the special handling for no config case Date: Tue, 21 Nov 2023 07:28:32 +0900 Message-Id: <20231120222832.4063882-3-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" To further simplify pinconf_generic_parse_dt_config(), eliminate the handling of the case where no configuration is found. When ncfg is zero, krealloc_array() will set ZERO_SIZE_PTR to *configs, which is a natural approach for managing a zero-size buffer. This should have no impact because none of the callers accesses 'configs' when ncfg is zero. Also, it is safe to pass ZERO_SIZE_PTR to kfree(). Signed-off-by: Masahiro Yamada --- drivers/pinctrl/pinconf-generic.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/drivers/pinctrl/pinconf-generic.c b/drivers/pinctrl/pinconf-ge= neric.c index ba4fe2466e78..252d69ee2b68 100644 --- a/drivers/pinctrl/pinconf-generic.c +++ b/drivers/pinctrl/pinconf-generic.c @@ -265,14 +265,6 @@ 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); =20 - /* no configs found at all */ - if (ncfg =3D=3D 0) { - kfree(cfg); - *configs =3D NULL; - *nconfigs =3D 0; - return 0; - } - /* Now resize the array to store the real number of found properties. */ *configs =3D krealloc_array(cfg, ncfg, sizeof(unsigned long), GFP_KERNEL); if (!*configs) --=20 2.40.1