public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Stephen Boyd <sboyd@kernel.org>
To: Daniel Latypov <dlatypov@google.com>
Cc: Michael Turquette <mturquette@baylibre.com>,
	linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org,
	Brendan Higgins <brendanhiggins@google.com>,
	kunit-dev@googlegroups.com
Subject: Re: [PATCH] clk: gate: Add some kunit test suites
Date: Tue, 18 Jan 2022 17:20:50 -0800	[thread overview]
Message-ID: <20220119012051.E3738C340E0@smtp.kernel.org> (raw)
In-Reply-To: <CAGS_qxpbOM4KuRe_SZ+es7K49_dV+2A1rwKX9bvjeGfSn04s6w@mail.gmail.com>

Quoting Daniel Latypov (2022-01-15 13:48:42)
> On Sat, Jan 15, 2022 at 12:07 AM Stephen Boyd <sboyd@kernel.org> wrote:
> >
> > Test various parts of the clk gate implementation with the kunit testing
> > framework.
> >
> > Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
> > Cc: <kunit-dev@googlegroups.com>
> > Signed-off-by: Stephen Boyd <sboyd@kernel.org>
> 
> Nice!
> Some minor nits and suggestions re kunit usage below.
> 
> Acked-by: Daniel Latypov <dlatypov@google.com>
> 
> > ---
> >
> > This is a resend of the RFC[1] from almost two years ago! It will be
> > merged after the merge window closes.
> >
> > [1] https://lore.kernel.org/r/20200408035637.110858-1-sboyd@kernel.org
> >
> >  drivers/clk/Kconfig         |   8 +
> >  drivers/clk/Makefile        |   1 +
> >  drivers/clk/clk-gate-test.c | 481 ++++++++++++++++++++++++++++++++++++
> >  3 files changed, 490 insertions(+)
> >  create mode 100644 drivers/clk/clk-gate-test.c
> >
> > diff --git a/drivers/clk/Kconfig b/drivers/clk/Kconfig
> > index c5b3dc97396a..41e560249370 100644
> > --- a/drivers/clk/Kconfig
> > +++ b/drivers/clk/Kconfig
> > @@ -421,4 +421,12 @@ source "drivers/clk/x86/Kconfig"
> >  source "drivers/clk/xilinx/Kconfig"
> >  source "drivers/clk/zynqmp/Kconfig"
> >
> > +# Kunit test cases
> > +config CLK_GATE_TEST
> > +       tristate "Basic gate type Kunit test"
> > +       depends on KUNIT
> > +       default KUNIT
> > +       help
> > +         Kunit test for the basic clk gate type.
> 
> minor nit: since the previous version, there is now
> https://www.kernel.org/doc/html/latest/dev-tools/kunit/style.html#test-kconfig-entries
> 
> so something like:
> config CLK_GATE_KUNIT_TEST
>   tristate "Basic gate type KUnit test" if !KUNIT_ALL_TESTS
>   depends on KUNIT
>   default KUNIT_ALL_TESTS
> ...
> 
> would be the way to go.

Got it. Thanks!

> 
> On a related note, you could add a .kunitconfig file to make running
> this easier:
> $ cat drivers/clk/.kunitconfig
> CONFIG_KUNIT=y
> CONFIG_COMMON_CLK=y
> CONFIG_CLK_GATE_TEST=y

Sure that works for me. I was using my own kunitconfig file and then
running all 'clk*' tests. This would make it easier I suppose. Too bad
the pattern match can't figure out what dependencies to enable.

> 
> $ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/clk
> ...
> Testing complete. Passed: 17, Failed: 0, Crashed: 0, Skipped: 0, Errors: 0
> 
> There's not much in the way of dependencies here so it doesn't help that much.
> But it is an option if you want a one-liner way to be able to run the test.
> 
> > +
> >  endif
> > diff --git a/drivers/clk/Makefile b/drivers/clk/Makefile
> > index e42312121e51..dcdb75712940 100644
> > --- a/drivers/clk/Makefile
> > +++ b/drivers/clk/Makefile
> > @@ -6,6 +6,7 @@ obj-$(CONFIG_COMMON_CLK)        += clk-divider.o
> >  obj-$(CONFIG_COMMON_CLK)       += clk-fixed-factor.o
> >  obj-$(CONFIG_COMMON_CLK)       += clk-fixed-rate.o
> >  obj-$(CONFIG_COMMON_CLK)       += clk-gate.o
> > +obj-$(CONFIG_CLK_GATE_TEST)    += clk-gate-test.o
> >  obj-$(CONFIG_COMMON_CLK)       += clk-multiplier.o
> >  obj-$(CONFIG_COMMON_CLK)       += clk-mux.o
> >  obj-$(CONFIG_COMMON_CLK)       += clk-composite.o
> > diff --git a/drivers/clk/clk-gate-test.c b/drivers/clk/clk-gate-test.c
> > new file mode 100644
> > index 000000000000..b499c2ffa815
> > --- /dev/null
> > +++ b/drivers/clk/clk-gate-test.c
> 
> again a minor nit: clk_gate_test.c or clk_gate_kunit.c would be the
> preferred names now:
> https://www.kernel.org/doc/html/latest/dev-tools/kunit/style.html#test-kconfig-entries
> 
> Note that KUnit itself doesn't follow its own naming guidelines unfortunately.

How about clk-gate_test.c then? I'd like it to match the clk-gate.c
file but can support the _test suffix.

> 
> > @@ -0,0 +1,481 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +/*
> > + * Kunit test for clk gate basic type
> > + */
> > +#include <linux/clk.h>
> > +#include <linux/clk-provider.h>
> > +#include <linux/platform_device.h>
> > +
> > +#include <kunit/test.h>
> > +
> > +static void clk_gate_register_test_dev(struct kunit *test)
> > +{
> > +       struct clk_hw *ret;
> > +       struct platform_device *pdev;
> > +
> > +       pdev = platform_device_register_simple("test_gate_device", -1, NULL, 0);
> > +       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
> > +
> > +       ret = clk_hw_register_gate(&pdev->dev, "test_gate", NULL, 0, NULL,
> > +                                  0, 0, NULL);
> > +       KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ret);
> 
> I think we want ASSERT here, otherwise we segfault below.

Fixed.

> 
> > +       KUNIT_EXPECT_STREQ(test, "test_gate", clk_hw_get_name(ret));
> > +       KUNIT_EXPECT_EQ(test, 0UL, clk_hw_get_flags(ret));
> > +
[...]
> > +
> > +static struct clk_gate_test_context *clk_gate_test_alloc_ctx(struct kunit *test)
> > +{
> > +       struct clk_gate_test_context *ctx;
> > +
> > +       test->priv = ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
> 
> It looks like kunit_kzalloc() here would work as well.
> It should also be a bit safer, i.e. we won't leak ctx if
> clk_hw_register_fixed_rate() errors out in the init func.

Ok. 

> 
> > +       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
> > +       ctx->fake_mem = (void __force __iomem *)&ctx->fake_reg;
> > +
> > +       return ctx;
> > +}
> > +
> > +static void clk_gate_test_parent_rate(struct kunit *test)
> > +{
> > +       struct clk_gate_test_context *ctx = test->priv;
> > +       struct clk_hw *parent = ctx->parent;
> > +       struct clk_hw *hw = ctx->hw;
> > +       unsigned long prate = clk_hw_get_rate(parent);
> > +       unsigned long rate = clk_hw_get_rate(hw);
> > +
> > +       KUNIT_EXPECT_EQ(test, prate, rate);
> > +}
> > +
> > +static void clk_gate_test_enable(struct kunit *test)
> > +{
> > +       struct clk_gate_test_context *ctx = test->priv;
> > +       struct clk_hw *parent = ctx->parent;
> > +       struct clk_hw *hw = ctx->hw;
> > +       struct clk *clk = hw->clk;
> > +       int ret;
> > +       u32 enable_val = BIT(5);
> > +
> > +       ret = clk_prepare_enable(clk);
> > +       KUNIT_ASSERT_EQ(test, ret, 0);
> 
> optional: in the cases where it's short enough, I'd personally favor
> KUNIT_ASSERT_EQ(test, clk_prepare_enable(clk), 0);
> 
> That way we get more context in the assertion failure messages.

Makes sense.

  reply	other threads:[~2022-01-19  1:20 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-15  8:06 [PATCH] clk: gate: Add some kunit test suites Stephen Boyd
2022-01-15 21:48 ` Daniel Latypov
2022-01-19  1:20   ` Stephen Boyd [this message]
2022-01-19  1:30     ` Daniel Latypov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220119012051.E3738C340E0@smtp.kernel.org \
    --to=sboyd@kernel.org \
    --cc=brendanhiggins@google.com \
    --cc=dlatypov@google.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mturquette@baylibre.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox