Message ID | 20231120222832.4063882-2-masahiroy@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2b07:b0:403:3b70:6f57 with SMTP id io7csp255597vqb; Mon, 20 Nov 2023 14:28:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IGvFuZ8fPi38AD52SjT8R094Sze4q8IrdJScDP4J7fnQoRtG7qm+5IyonUVmT7sXhk282Sx X-Received: by 2002:a17:903:22c1:b0:1ce:67fa:b380 with SMTP id y1-20020a17090322c100b001ce67fab380mr1137691plg.34.1700519329172; Mon, 20 Nov 2023 14:28:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700519329; cv=none; d=google.com; s=arc-20160816; b=wL9zveNZ5480Zi/sXU0OYYw92EFoVmtjw4XUNr25VsqIKFzVBwzEbxL4JyJTBA27tE oLDdvEngx9l2afXVYahFax9S/lyRdHYe+99mhZ/g4RGUdhDB4IsqSVBojeaZ/UZW9KQs r7S3+hn9K8L0sIWDDu6Aio5hzG/6cSoge30WO/NJCShE/yhFFWUMU7khZLomOUo12Cu7 QgPmY/BJBQMhU9NarXr5yqhh+AuQ4N7QYRwEtINcOyJBNh0VEzfthILgoVIaMu4AnK1g rVGMat7MuMkHTUXzpIgJtLVW/sRCra7tK3BLfn0H8cOFTebOcD8Xx/AcsLfZ7uKQe+ox xGWA== 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 Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id 11-20020a170902c14b00b001cc0e39373bsi8599660plj.32.2023.11.20.14.28.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Nov 2023 14:28:49 -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 3FDCF80941E7; Mon, 20 Nov 2023 14:28:48 -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 S232552AbjKTW2o (ORCPT <rfc822;heyuhang3455@gmail.com> + 27 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 <rfc822;linux-kernel@vger.kernel.org>); 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 <linux-kernel@vger.kernel.org>; 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 <masahiroy@kernel.org> To: Linus Walleij <linus.walleij@linaro.org>, Bartosz Golaszewski <brgl@bgdev.pl>, linux-gpio@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada <masahiroy@kernel.org> 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: <linux-kernel.vger.kernel.org> 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:48 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783123755917952797 X-GMAIL-MSGID: 1783123755917952797 |
Series |
pinctrl: pinconf-generic: clean up pinconf_parse_dt_config()
|
|
Commit Message
Masahiro Yamada
Nov. 20, 2023, 10:28 p.m. UTC
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 <masahiroy@kernel.org>
---
drivers/pinctrl/pinconf-generic.c | 25 ++++++++-----------------
1 file changed, 8 insertions(+), 17 deletions(-)
Comments
On Tue, Nov 21, 2023 at 7:28 AM Masahiro Yamada <masahiroy@kernel.org> wrote: > > 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 <masahiroy@kernel.org> Sorry, I retract this patch set. krealloc() does not save any memory when the new_size is smaller than the current size. > --- > > 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 >
Hi Masahiro, On Tue, Nov 21, 2023 at 11:21 AM Masahiro Yamada <masahiroy@kernel.org> wrote: > On Tue, Nov 21, 2023 at 7:28 AM Masahiro Yamada <masahiroy@kernel.org> wrote: > > > > 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 <masahiroy@kernel.org> > > Sorry, I retract this patch set. > > krealloc() does not save any memory > when the new_size is smaller than the current size. But the first part where you switch to kmalloc_array() is still a nice change. The fact that we use kmemdup to be able to also shrink the allocation is a bit of an oddity I guess, but let's run this patch by Andy Shevchenko, and ask what he thinks about simply introducing kmemdup_array() or if he has other ideas for this. Yours, Linus Walleij
On Fri, Nov 24, 2023 at 11:06:50AM +0100, Linus Walleij wrote: > On Tue, Nov 21, 2023 at 11:21 AM Masahiro Yamada <masahiroy@kernel.org> wrote: > > On Tue, Nov 21, 2023 at 7:28 AM Masahiro Yamada <masahiroy@kernel.org> wrote: > > > > > > 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 <masahiroy@kernel.org> > > > > Sorry, I retract this patch set. > > > > krealloc() does not save any memory > > when the new_size is smaller than the current size. > > But the first part where you switch to kmalloc_array() is still a nice change. > > The fact that we use kmemdup to be able to also shrink the allocation is a > bit of an oddity I guess, but let's run this patch by Andy Shevchenko, and > ask what he thinks about simply introducing kmemdup_array() or if he > has other ideas for this. https://lore.kernel.org/all/20231017052322.2636-2-kkartik@nvidia.com/
On Fri, Nov 24, 2023 at 8:25 PM Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote: > > On Fri, Nov 24, 2023 at 11:06:50AM +0100, Linus Walleij wrote: > > On Tue, Nov 21, 2023 at 11:21 AM Masahiro Yamada <masahiroy@kernel.org> wrote: > > > On Tue, Nov 21, 2023 at 7:28 AM Masahiro Yamada <masahiroy@kernel.org> wrote: > > > > > > > > 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 <masahiroy@kernel.org> > > > > > > Sorry, I retract this patch set. > > > > > > krealloc() does not save any memory > > > when the new_size is smaller than the current size. > > > > But the first part where you switch to kmalloc_array() is still a nice change. > > > > The fact that we use kmemdup to be able to also shrink the allocation is a > > bit of an oddity I guess, but let's run this patch by Andy Shevchenko, and > > ask what he thinks about simply introducing kmemdup_array() or if he > > has other ideas for this. > > https://lore.kernel.org/all/20231017052322.2636-2-kkartik@nvidia.com/ Ok, I will come back when kmemdup_array() is upstreamed. > -- > With Best Regards, > Andy Shevchenko > >
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);