Re: [PATCH v2 12/15] platform/chrome: use krealloc() for `din` and `dout`

From: Tzung-Bi Shih
Date: Wed Jun 08 2022 - 01:16:55 EST


On Tue, Jun 07, 2022 at 12:06:23PM -0700, Guenter Roeck wrote:
> On Tue, Jun 7, 2022 at 7:57 AM Tzung-Bi Shih <tzungbi@xxxxxxxxxx> wrote:
> >
> > Use krealloc() to re-allocate `din` and `dout`. Don't use devm variant
> > because the two buffers could be re-allocated multiple times during
> > runtime. Their life cycles aren't quite aligned to the device's.
>
> While this saves a few lines of code, it is runtime-expensive:
> krealloc() copies the old data, which is a waste of time/resources.
> Maybe it would be better to just use kfree() followed by kzalloc().

Ack. I wasn't aware of the memcpy(). Let's don't use it.

> > @@ -469,9 +469,9 @@ static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
> > */
> > int cros_ec_query_all(struct cros_ec_device *ec_dev)
> > {
> > - struct device *dev = ec_dev->dev;
> > u32 ver_mask = 0;
> > int ret;
> > + u8 *din, *dout;
> >
> > /* First try sending with proto v3. */
> > if (!cros_ec_get_proto_info(ec_dev, CROS_EC_DEV_EC_INDEX)) {
> > @@ -492,21 +492,15 @@ int cros_ec_query_all(struct cros_ec_device *ec_dev)
> > }
> > }
> >
> > - devm_kfree(dev, ec_dev->din);
> > - devm_kfree(dev, ec_dev->dout);
> > -
> > - ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
> > - if (!ec_dev->din) {
> > - ret = -ENOMEM;
> > - goto exit;
> > - }
> > + din = krealloc(ec_dev->din, ec_dev->din_size, GFP_KERNEL);
> > + if (!din)
> > + return -ENOMEM;
>
> I would suggest assigning the values directly; the new variables don't
> really add value.

checkpatch.pl generated a warning for it. See [1].

Will remove the krealloc()s anyway.

[1]: https://github.com/torvalds/linux/commit/972fdea2e6ece7578915d386a5447bc78d3fb8b8