Re: [PATCH 02/13] platform/chrome: cros_ec_proto: add Kunit tests for cros_ec_query_all()

From: Tzung-Bi Shih
Date: Mon Jun 06 2022 - 20:54:51 EST


On Mon, Jun 06, 2022 at 08:18:41AM -0700, Guenter Roeck wrote:
> On Mon, Jun 6, 2022 at 7:12 AM Tzung-Bi Shih <tzungbi@xxxxxxxxxx> wrote:
> > +static void cros_ec_proto_test_query_all_pretest(struct kunit *test)
> > +{
> > + struct cros_ec_proto_test_priv *priv = test->priv;
> > + struct cros_ec_device *ec_dev = &priv->ec_dev;
> > +
> > + /*
> > + * cros_ec_query_all() will free din and dout and allocate them again to fit the usage by
> > + * calling devm_kfree() and devm_kzalloc(). Set them to NULL as they aren't managed by
> > + * ec_dev->dev.
> > + */
> > + ec_dev->din = NULL;
> > + ec_dev->dout = NULL;
> > +}
> > +
> > +static void cros_ec_proto_test_query_all_normal(struct kunit *test)
> > +{
[...]
> > + cros_ec_proto_test_query_all_pretest(test);
> > + ret = cros_ec_query_all(ec_dev);
>
> Wouldn't it be better to implement a post_test function and have it
> call devm_kfree() if it is really necessary to release ->din and
> ->dout here ?
>
> Either case, I am not convinced that clearing / releasing din and dout
> is really needed. The device pointer should not change, after all, and
> either the next call to cros_ec_query_all() will release the pointers,
> or unloading the driver will do it.

The `din` and `dout` are not managed by `ec_dev->dev` but statically
initializing in cros_ec_proto_test_init()(see below).

cros_ec_proto_test_query_all_pretest() sets them to NULL to get rid of the
following warning (as devres_destroy() in devm_kfree() returns -ENOENT):
WARNING: CPU: 0 PID: 27 at drivers/base/devres.c:1058 devm_kfree

Oops, I just realized qemu still generated yet another warning:
Device '(null)' does not have a release() function, it is broken and ...
Will fix it in next version.

[...]
> > static int cros_ec_proto_test_init(struct kunit *test)
> > {
> > struct cros_ec_proto_test_priv *priv;
> > @@ -188,24 +902,48 @@ static int cros_ec_proto_test_init(struct kunit *test)
> > ec_dev->din = (u8 *)priv->din;
> > ec_dev->din_size = ARRAY_SIZE(priv->din);
> > ec_dev->proto_version = EC_HOST_REQUEST_VERSION;
> > + ec_dev->dev = kunit_kzalloc(test, sizeof(*ec_dev->dev), GFP_KERNEL);
> > + if (!ec_dev->dev)
> > + return -ENOMEM;
> > + device_initialize(ec_dev->dev);
> > + ec_dev->cmd_xfer = cros_kunit_ec_xfer_mock;
> > + ec_dev->pkt_xfer = cros_kunit_ec_xfer_mock;
> >
> > priv->msg = (struct cros_ec_command *)priv->_msg;
> >
> > + cros_kunit_mock_reset();
> > +
> > return 0;
> > }