Message ID | 20230720205324.58702-4-andriy.shevchenko@linux.intel.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp3379625vqt; Thu, 20 Jul 2023 14:05:59 -0700 (PDT) X-Google-Smtp-Source: APBJJlHNOigKSVhEu6odYaFZGpy9oGbeTJDQQYG44gw8qyfK6scgl5DCEbHiJSXPSvCRR/snkGKO X-Received: by 2002:ac2:4439:0:b0:4f7:6453:f3f1 with SMTP id w25-20020ac24439000000b004f76453f3f1mr2307519lfl.15.1689887158874; Thu, 20 Jul 2023 14:05:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689887158; cv=none; d=google.com; s=arc-20160816; b=Vb6l9ilef7M83wUcH8ufFv5UYAh1MFTktg1v9Bm3qkOmU8MO7QLqPNWxuzMWN8Lp5M OLISCcEduOWfChbVG+RDBBecu+YVT8DNPRzqHOLbYaIFZJtSZxmuIIO13glktzLp0J/t Ayt2RFI7UAJ3pjfXh0w8MuA17Ysoq3UaJ19Ewfx+Vhj5ARXmdcNe0OP/6SfLCO5hrUEw Lg/J7+jNvlCh5idMRWLqd5i1pzjhAuQT4B34xFgi90yEWj3w9bdbKmorUsKMcdj3nxGd ez3w9lxODGtBWm8TdI3VFCNKEK9KZVqW0PtBCziUWfloI4Ng3MqQRpSh19Kvp5mUk3CP bALA== 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=rxWujKhgJ4rDUawexZo6TUq5ZXWld0TVO73Tp6TSTDU=; fh=UOk5tC92rpasvTHsS8FNeq1ESsZGN5rcMuNSzha6ebA=; b=jmkQoFv82sQ+uj+6YRA9qxCaWaJfdVX2SyXUTwuxov2mxMAVLeltblfrwVxGE8ToTK Weh9B+dqKvYCGQYmV1qVjEll+1lRIBx9brl61oYWFvfC3sI0LuDyFJbONMy9XsXrufls ao7vyeQi/ZlLZTf0u4eRnAucx7Zj0qgmLSNlJaENIH4n3PMAMvK9r/ZU4THXM8ikOtBt qEbT7O/cCgjzBRGyVAE38HsLL3Tbrk4Cg+yqPNrEoY1hYbO0Yq0tSb+I1detWrArdtHp 9894Qkv1uRbZH31z+pSNHwFl5D7hUhAymomQglQDXS5n4DOHWNI7yT+G3Ce9wERMXixZ 2JoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Q6LOFcAL; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v13-20020a056402184d00b00521a9d1c843si1316072edy.251.2023.07.20.14.05.26; Thu, 20 Jul 2023 14:05:58 -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; dkim=pass header.i=@intel.com header.s=Intel header.b=Q6LOFcAL; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229975AbjGTUxd (ORCPT <rfc822;assdfgzxcv4@gmail.com> + 99 others); Thu, 20 Jul 2023 16:53:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230003AbjGTUxZ (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 20 Jul 2023 16:53:25 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87B362727; Thu, 20 Jul 2023 13:53:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1689886404; x=1721422404; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=X7jl6NvFoo34AY/ks0sUG4wFGR4ZluKuJMLZIOkzWEQ=; b=Q6LOFcALRo2GzbOkyGa0U/2/TvFmUoEe8zfrVkzoNDBu7HMf8ndATBLh VLpUueWSiP+S/QGVbbnUOsG80OzX588SYGPNg6e5WFaot7lZXAr/RNips xNnibfesQZV7hCO9+tmDkl8CKCsTVeTjMwnTRf/rU4hPi8axVXNdX9S8R wck9m2gVKgw1a9PEH6SAcNhU27rdbMuv85Ar1eRPdfhwvOZRJYSzNCGVA NGeszOIZQxTbxTsuZkezGNmTgXPHo959vQkk3NNccTKZNb2aAVNV/cGb+ gcgXi57B58olKdu3B9vKPrjGlm9ATnExMZsCy8TQuKcOmGrrzM8+C0AO4 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10777"; a="356846041" X-IronPort-AV: E=Sophos;i="6.01,219,1684825200"; d="scan'208";a="356846041" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Jul 2023 13:53:22 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10777"; a="838273932" X-IronPort-AV: E=Sophos;i="6.01,219,1684825200"; d="scan'208";a="838273932" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga002.fm.intel.com with ESMTP; 20 Jul 2023 13:53:20 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id 47669516; Thu, 20 Jul 2023 23:53:28 +0300 (EEST) From: Andy Shevchenko <andriy.shevchenko@linux.intel.com> To: Andy Shevchenko <andriy.shevchenko@linux.intel.com>, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Jonathan Cameron <jic23@kernel.org>, Lars-Peter Clausen <lars@metafoo.de> Subject: [PATCH v1 3/8] iio: core: Switch to krealloc_array() Date: Thu, 20 Jul 2023 23:53:19 +0300 Message-Id: <20230720205324.58702-4-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.40.0.1.gaa8946217a0b In-Reply-To: <20230720205324.58702-1-andriy.shevchenko@linux.intel.com> References: <20230720205324.58702-1-andriy.shevchenko@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, 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-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771975117604223278 X-GMAIL-MSGID: 1771975117604223278 |
Series |
iio: core: A few code cleanups and documentation fixes
|
|
Commit Message
Andy Shevchenko
July 20, 2023, 8:53 p.m. UTC
Let the krealloc_array() copy the original data and
check for a multiplication overflow.
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
drivers/iio/industrialio-core.c | 20 +++++++-------------
1 file changed, 7 insertions(+), 13 deletions(-)
Comments
On Thu, 2023-07-20 at 23:53 +0300, Andy Shevchenko wrote: > Let the krealloc_array() copy the original data and > check for a multiplication overflow. > > Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> > --- > drivers/iio/industrialio-core.c | 20 +++++++------------- > 1 file changed, 7 insertions(+), 13 deletions(-) > > diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c > index 90e59223b178..be154879983e 100644 > --- a/drivers/iio/industrialio-core.c > +++ b/drivers/iio/industrialio-core.c > @@ -1465,7 +1465,7 @@ int iio_device_register_sysfs_group(struct iio_dev > *indio_dev, > const struct attribute_group **new, **old = iio_dev_opaque->groups; > unsigned int cnt = iio_dev_opaque->groupcounter; > > - new = krealloc(old, sizeof(*new) * (cnt + 2), GFP_KERNEL); > + new = krealloc_array(old, cnt + 2, sizeof(*new), GFP_KERNEL); > if (!new) > return -ENOMEM; > > @@ -1483,13 +1483,13 @@ static int iio_device_register_sysfs(struct iio_dev > *indio_dev) > { > struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); > int i, ret = 0, attrcount, attrn, attrcount_orig = 0; > + struct attribute **attrs, **attr, *clk = NULL; > struct iio_dev_attr *p; > - struct attribute **attr, *clk = NULL; > > /* First count elements in any existing group */ > - if (indio_dev->info->attrs) { > - attr = indio_dev->info->attrs->attrs; > - while (*attr++ != NULL) > + attrs = indio_dev->info->attrs ? indio_dev->info->attrs->attrs : NULL; > + if (attrs) { > + for (attr = attrs; *attr; attr++) > attrcount_orig++; not really related with the change... maybe just mention it in the commit? > } > attrcount = attrcount_orig; > @@ -1521,20 +1521,14 @@ static int iio_device_register_sysfs(struct iio_dev > *indio_dev) > if (clk) > attrcount++; > > + /* Copy across original attributes, and point to original binary > attributes */ > iio_dev_opaque->chan_attr_group.attrs = > - kcalloc(attrcount + 1, > - sizeof(iio_dev_opaque->chan_attr_group.attrs[0]), > - GFP_KERNEL); > + krealloc_array(attrs, attrcount + 1, sizeof(*attrs), > GFP_KERNEL); > if (iio_dev_opaque->chan_attr_group.attrs == NULL) { since you're here and you also already did some style cleanups above, maybe change it to 'if (!iio_dev_opaque->chan_attr_group.attrs)'? - Nuno Sá
On Fri, Jul 21, 2023 at 09:59:37AM +0200, Nuno Sá wrote: > On Thu, 2023-07-20 at 23:53 +0300, Andy Shevchenko wrote: ... > > + struct attribute **attrs, **attr, *clk = NULL; > > struct iio_dev_attr *p; > > - struct attribute **attr, *clk = NULL; > > > > /* First count elements in any existing group */ > > - if (indio_dev->info->attrs) { > > - attr = indio_dev->info->attrs->attrs; > > - while (*attr++ != NULL) > > + attrs = indio_dev->info->attrs ? indio_dev->info->attrs->attrs : NULL; > > + if (attrs) { > > + for (attr = attrs; *attr; attr++) > > attrcount_orig++; > not really related with the change... maybe just mention it in the commit? Hmm... It's related to make krealloc_array() to work as expected. > > } ... > > iio_dev_opaque->chan_attr_group.attrs = > > - kcalloc(attrcount + 1, > > - sizeof(iio_dev_opaque->chan_attr_group.attrs[0]), > > - GFP_KERNEL); > > + krealloc_array(attrs, attrcount + 1, sizeof(*attrs), > > GFP_KERNEL); > > if (iio_dev_opaque->chan_attr_group.attrs == NULL) { > > since you're here and you also already did some style cleanups above, maybe > change it to 'if (!iio_dev_opaque->chan_attr_group.attrs)'? I don't think it's related (but you can tell that this check related to the allocator, and since we touch it, we may touch this), if Jonathan wants this, I definitely do. ... Thank you for the review!
On Fri, 2023-07-21 at 13:14 +0300, Andy Shevchenko wrote: > On Fri, Jul 21, 2023 at 09:59:37AM +0200, Nuno Sá wrote: > > On Thu, 2023-07-20 at 23:53 +0300, Andy Shevchenko wrote: > > ... > > > > + struct attribute **attrs, **attr, *clk = NULL; > > > struct iio_dev_attr *p; > > > - struct attribute **attr, *clk = NULL; > > > > > > /* First count elements in any existing group */ > > > - if (indio_dev->info->attrs) { > > > - attr = indio_dev->info->attrs->attrs; > > > - while (*attr++ != NULL) > > > + attrs = indio_dev->info->attrs ? indio_dev->info->attrs->attrs : > > > NULL; > > > + if (attrs) { > > > + for (attr = attrs; *attr; attr++) > > > attrcount_orig++; > > > not really related with the change... maybe just mention it in the commit? > > Hmm... It's related to make krealloc_array() to work as expected. > Hmm, I think it's arguable :). while() -> for() it's not really needed unless I'm missing something. You could even initialize 'attrs' to NULL at declaration and keep the above diff minimum. That said, I actually prefer this style (even though some people don't like much the ternary operator). > > > } > > ... > > > > iio_dev_opaque->chan_attr_group.attrs = > > > - kcalloc(attrcount + 1, > > > - sizeof(iio_dev_opaque->chan_attr_group.attrs[0]), > > > - GFP_KERNEL); > > > + krealloc_array(attrs, attrcount + 1, sizeof(*attrs), > > > GFP_KERNEL); > > > if (iio_dev_opaque->chan_attr_group.attrs == NULL) { > > > > since you're here and you also already did some style cleanups above, maybe > > change it to 'if (!iio_dev_opaque->chan_attr_group.attrs)'? > > I don't think it's related (but you can tell that this check related to > the allocator, and since we touch it, we may touch this), if Jonathan > wants this, I definitely do. Fair enough... - Nuno Sá
On Fri, Jul 21, 2023 at 12:53:53PM +0200, Nuno Sá wrote: > On Fri, 2023-07-21 at 13:14 +0300, Andy Shevchenko wrote: > > On Fri, Jul 21, 2023 at 09:59:37AM +0200, Nuno Sá wrote: > > > On Thu, 2023-07-20 at 23:53 +0300, Andy Shevchenko wrote: ... > > > > + struct attribute **attrs, **attr, *clk = NULL; > > > > struct iio_dev_attr *p; > > > > - struct attribute **attr, *clk = NULL; > > > > > > > > /* First count elements in any existing group */ > > > > - if (indio_dev->info->attrs) { > > > > - attr = indio_dev->info->attrs->attrs; > > > > - while (*attr++ != NULL) > > > > + attrs = indio_dev->info->attrs ? indio_dev->info->attrs->attrs : > > > > NULL; > > > > + if (attrs) { > > > > + for (attr = attrs; *attr; attr++) > > > > attrcount_orig++; > > > > > not really related with the change... maybe just mention it in the commit? > > > > Hmm... It's related to make krealloc_array() to work as expected. > > > > Hmm, I think it's arguable :). while() -> for() it's not really needed unless > I'm missing something. You could even initialize 'attrs' to NULL at declaration > and keep the above diff minimum. I'm not a fan of the assignments in the declarations when it potentially can be disrupted by a chunk of code and reading the code itself may be harder due to an interruption for checking the initial value. Hence, having + attr = attrs; while (... != NULL) seems enough to be replaced with one liner for-loop. > That said, I actually prefer this style (even though some people don't like much > the ternary operator). Thanks! > > > > }
On Fri, Jul 21, 2023 at 02:28:36PM +0300, Andy Shevchenko wrote: > On Fri, Jul 21, 2023 at 12:53:53PM +0200, Nuno Sá wrote: > > On Fri, 2023-07-21 at 13:14 +0300, Andy Shevchenko wrote: > > > On Fri, Jul 21, 2023 at 09:59:37AM +0200, Nuno Sá wrote: > > > > On Thu, 2023-07-20 at 23:53 +0300, Andy Shevchenko wrote: ... > > > > > + struct attribute **attrs, **attr, *clk = NULL; > > > > > struct iio_dev_attr *p; > > > > > - struct attribute **attr, *clk = NULL; > > > > > > > > > > /* First count elements in any existing group */ > > > > > - if (indio_dev->info->attrs) { > > > > > - attr = indio_dev->info->attrs->attrs; > > > > > - while (*attr++ != NULL) > > > > > + attrs = indio_dev->info->attrs ? indio_dev->info->attrs->attrs : > > > > > NULL; > > > > > + if (attrs) { > > > > > + for (attr = attrs; *attr; attr++) > > > > > attrcount_orig++; > > > > > > > not really related with the change... maybe just mention it in the commit? > > > > > > Hmm... It's related to make krealloc_array() to work as expected. > > > > > > > Hmm, I think it's arguable :). while() -> for() it's not really needed unless > > I'm missing something. You could even initialize 'attrs' to NULL at declaration > > and keep the above diff minimum. > > I'm not a fan of the assignments in the declarations when it potentially can be > disrupted by a chunk of code and reading the code itself may be harder due to > an interruption for checking the initial value. Hence, having > > + attr = attrs; > while (... != NULL) > > seems enough to be replaced with one liner for-loop. Note that attrs is reused later, so the above assignment makes it cleaner that some value is assigned to it. With the original code it's not so obvious. > > That said, I actually prefer this style (even though some people don't like much > > the ternary operator). > > Thanks! > > > > > > }
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c index 90e59223b178..be154879983e 100644 --- a/drivers/iio/industrialio-core.c +++ b/drivers/iio/industrialio-core.c @@ -1465,7 +1465,7 @@ int iio_device_register_sysfs_group(struct iio_dev *indio_dev, const struct attribute_group **new, **old = iio_dev_opaque->groups; unsigned int cnt = iio_dev_opaque->groupcounter; - new = krealloc(old, sizeof(*new) * (cnt + 2), GFP_KERNEL); + new = krealloc_array(old, cnt + 2, sizeof(*new), GFP_KERNEL); if (!new) return -ENOMEM; @@ -1483,13 +1483,13 @@ static int iio_device_register_sysfs(struct iio_dev *indio_dev) { struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); int i, ret = 0, attrcount, attrn, attrcount_orig = 0; + struct attribute **attrs, **attr, *clk = NULL; struct iio_dev_attr *p; - struct attribute **attr, *clk = NULL; /* First count elements in any existing group */ - if (indio_dev->info->attrs) { - attr = indio_dev->info->attrs->attrs; - while (*attr++ != NULL) + attrs = indio_dev->info->attrs ? indio_dev->info->attrs->attrs : NULL; + if (attrs) { + for (attr = attrs; *attr; attr++) attrcount_orig++; } attrcount = attrcount_orig; @@ -1521,20 +1521,14 @@ static int iio_device_register_sysfs(struct iio_dev *indio_dev) if (clk) attrcount++; + /* Copy across original attributes, and point to original binary attributes */ iio_dev_opaque->chan_attr_group.attrs = - kcalloc(attrcount + 1, - sizeof(iio_dev_opaque->chan_attr_group.attrs[0]), - GFP_KERNEL); + krealloc_array(attrs, attrcount + 1, sizeof(*attrs), GFP_KERNEL); if (iio_dev_opaque->chan_attr_group.attrs == NULL) { ret = -ENOMEM; goto error_clear_attrs; } - /* Copy across original attributes, and point to original binary attributes */ if (indio_dev->info->attrs) { - memcpy(iio_dev_opaque->chan_attr_group.attrs, - indio_dev->info->attrs->attrs, - sizeof(iio_dev_opaque->chan_attr_group.attrs[0]) - *attrcount_orig); iio_dev_opaque->chan_attr_group.is_visible = indio_dev->info->attrs->is_visible; iio_dev_opaque->chan_attr_group.bin_attrs =