Add a KUnit suite with a mock driver of two owners that differ in
clock_id: a pin of one owner with a firmware node and a pin of the other
owner with a link to it. The tests check that:

- the link is reported for both pins only while both are registered,
  whichever of them is registered first
- the pin at the other end gets a change notification whenever the link
  appears or disappears
- a pin keeps its link when it is unregistered and registered again
- for a pin registered with two DPLLs of its owner, only the first
  registration and the last unregistration change the link
- a pin with neither a link nor a firmware node is not linked or notified
- a duplicate link is rejected, also on a registered pin, and a new
  link cannot be added to a registered pin

The tests build the pin message with dpll_cmd_pin_get_one() under
dpll_lock, so both are exported for KUnit.

Run them with:

  tools/testing/kunit/kunit.py run --kunitconfig=drivers/dpll

Signed-off-by: Ivan Vecera <[email protected]>
---
 drivers/dpll/.kunitconfig       |   5 +
 drivers/dpll/Kconfig            |  16 ++
 drivers/dpll/Makefile           |   2 +
 drivers/dpll/dpll_core.c        |   2 +
 drivers/dpll/dpll_netlink.c     |   4 +-
 drivers/dpll/dpll_netlink.h     |   6 +
 drivers/dpll/tests/dpll_kunit.c | 419 ++++++++++++++++++++++++++++++++
 7 files changed, 453 insertions(+), 1 deletion(-)
 create mode 100644 drivers/dpll/.kunitconfig
 create mode 100644 drivers/dpll/tests/dpll_kunit.c

diff --git a/drivers/dpll/.kunitconfig b/drivers/dpll/.kunitconfig
new file mode 100644
index 00000000000000..017c9d5b593a4f
--- /dev/null
+++ b/drivers/dpll/.kunitconfig
@@ -0,0 +1,5 @@
+CONFIG_KUNIT=y
+CONFIG_NET=y
+CONFIG_DPLL_KUNIT_TEST=y
+CONFIG_DEBUG_KERNEL=y
+CONFIG_PROVE_LOCKING=y
diff --git a/drivers/dpll/Kconfig b/drivers/dpll/Kconfig
index be98969f040ab6..6f1ffc3a6e7717 100644
--- a/drivers/dpll/Kconfig
+++ b/drivers/dpll/Kconfig
@@ -23,6 +23,22 @@ config DPLL_REFCNT_TRACKER
 
          If unsure, say N.
 
+config DPLL_KUNIT_TEST
+       tristate "KUnit tests for DPLL core" if !KUNIT_ALL_TESTS
+       depends on KUNIT && NET
+       select DPLL
+       default KUNIT_ALL_TESTS
+       help
+         KUnit tests for the DPLL core. They use a mock driver to check
+         that links between pins of two devices are reported and notified
+         while the pins at both ends are registered.
+
+         The mock devices and pins are registered with the DPLL subsystem,
+         so user space sees them and their notifications while the tests
+         run.
+
+         If unsure, say N.
+
 source "drivers/dpll/zl3073x/Kconfig"
 
 endmenu
diff --git a/drivers/dpll/Makefile b/drivers/dpll/Makefile
index 9e7a3a3e592e81..ef3081be2a113b 100644
--- a/drivers/dpll/Makefile
+++ b/drivers/dpll/Makefile
@@ -8,4 +8,6 @@ dpll-y                  += dpll_core.o
 dpll-y                  += dpll_netlink.o
 dpll-y                  += dpll_nl.o
 
+obj-$(CONFIG_DPLL_KUNIT_TEST)  += tests/dpll_kunit.o
+
 obj-$(CONFIG_ZL3073X)  += zl3073x/
diff --git a/drivers/dpll/dpll_core.c b/drivers/dpll/dpll_core.c
index 58a5a4a41a0b72..7fd84b0e90e993 100644
--- a/drivers/dpll/dpll_core.c
+++ b/drivers/dpll/dpll_core.c
@@ -8,6 +8,7 @@
 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
+#include <kunit/visibility.h>
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/idr.h>
@@ -21,6 +22,7 @@
 
 /* Mutex lock to protect DPLL subsystem devices and pins */
 DEFINE_MUTEX(dpll_lock);
+EXPORT_SYMBOL_IF_KUNIT(dpll_lock);
 
 DEFINE_XARRAY_FLAGS(dpll_device_xa, XA_FLAGS_ALLOC);
 DEFINE_XARRAY_FLAGS(dpll_pin_xa, XA_FLAGS_ALLOC);
diff --git a/drivers/dpll/dpll_netlink.c b/drivers/dpll/dpll_netlink.c
index 016f3026d2f2e5..931ea2027cf68f 100644
--- a/drivers/dpll/dpll_netlink.c
+++ b/drivers/dpll/dpll_netlink.c
@@ -6,6 +6,7 @@
  *  Copyright (c) 2023 Intel and affiliates
  *
  */
+#include <kunit/visibility.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/netdevice.h>
@@ -735,7 +736,7 @@ static int dpll_msg_add_pin_links(struct sk_buff *msg, 
struct dpll_pin *pin)
        return 0;
 }
 
-static int
+VISIBLE_IF_KUNIT int
 dpll_cmd_pin_get_one(struct sk_buff *msg, struct dpll_pin *pin,
                     struct netlink_ext_ack *extack)
 {
@@ -811,6 +812,7 @@ dpll_cmd_pin_get_one(struct sk_buff *msg, struct dpll_pin 
*pin,
 
        return dpll_msg_add_pin_links(msg, pin);
 }
+EXPORT_SYMBOL_IF_KUNIT(dpll_cmd_pin_get_one);
 
 static int
 dpll_device_get_one(struct dpll_device *dpll, struct sk_buff *msg,
diff --git a/drivers/dpll/dpll_netlink.h b/drivers/dpll/dpll_netlink.h
index 4f63aa58789a17..bc20bcd248a512 100644
--- a/drivers/dpll/dpll_netlink.h
+++ b/drivers/dpll/dpll_netlink.h
@@ -11,3 +11,9 @@ int dpll_device_delete_ntf(struct dpll_device *dpll);
 int dpll_pin_create_ntf(struct dpll_pin *pin, u64 src_clock_id);
 
 int dpll_pin_delete_ntf(struct dpll_pin *pin, u64 src_clock_id);
+
+#if IS_ENABLED(CONFIG_KUNIT)
+/* For KUnit tests, call with dpll_lock held for a registered pin */
+int dpll_cmd_pin_get_one(struct sk_buff *msg, struct dpll_pin *pin,
+                        struct netlink_ext_ack *extack);
+#endif
diff --git a/drivers/dpll/tests/dpll_kunit.c b/drivers/dpll/tests/dpll_kunit.c
new file mode 100644
index 00000000000000..7fb50fe0eea9a5
--- /dev/null
+++ b/drivers/dpll/tests/dpll_kunit.c
@@ -0,0 +1,419 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit tests for links between DPLL pins.
+ *
+ * Each test starts with two owners that differ in clock_id:
+ *
+ * Owner A: devices A0 and A1, pin P with firmware node F, pin U with
+ *          neither a firmware node nor a link
+ * Owner B: device B0, pin C with a link to F
+ *
+ * The devices are registered, the pins are not.
+ */
+
+#include <kunit/fwnode.h>
+#include <kunit/test.h>
+#include <linux/dpll.h>
+#include <linux/notifier.h>
+#include <net/netlink.h>
+
+#include "../dpll_core.h"
+#include "../dpll_netlink.h"
+
+MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
+
+#define DT_CLOCK_ID_A          0xd011a
+#define DT_CLOCK_ID_B          0xd011b
+#define DT_MAX_LINKS           4
+
+struct dt_dev {
+       struct dpll_device *dpll;
+       dpll_tracker tracker;
+       bool registered;
+};
+
+struct dt_pin {
+       struct dpll_pin *pin;
+       dpll_tracker tracker;
+       int changes;            /* DPLL_PIN_CHANGED notifications */
+};
+
+struct dt_ctx {
+       struct dt_dev a0, a1, b0;
+       struct dt_pin p, u, c;
+       struct fwnode_handle *fwnode;
+       struct notifier_block nb;
+       bool nb_registered;
+       bool p_a0, p_a1, u_a0, c_b0;    /* registrations */
+};
+
+static int dt_mode_get(const struct dpll_device *dpll, void *dpll_priv,
+                      enum dpll_mode *mode, struct netlink_ext_ack *extack)
+{
+       *mode = DPLL_MODE_MANUAL;
+       return 0;
+}
+
+static int dt_lock_status_get(const struct dpll_device *dpll, void *dpll_priv,
+                             enum dpll_lock_status *status,
+                             enum dpll_lock_status_error *status_error,
+                             struct netlink_ext_ack *extack)
+{
+       *status = DPLL_LOCK_STATUS_UNLOCKED;
+       return 0;
+}
+
+static const struct dpll_device_ops dt_dev_ops = {
+       .mode_get = dt_mode_get,
+       .lock_status_get = dt_lock_status_get,
+};
+
+static int dt_state_get(const struct dpll_pin *pin, void *pin_priv,
+                       const struct dpll_device *dpll, void *dpll_priv,
+                       enum dpll_pin_state *state,
+                       struct netlink_ext_ack *extack)
+{
+       *state = DPLL_PIN_STATE_CONNECTED;
+       return 0;
+}
+
+static int dt_direction_get(const struct dpll_pin *pin, void *pin_priv,
+                           const struct dpll_device *dpll, void *dpll_priv,
+                           enum dpll_pin_direction *direction,
+                           struct netlink_ext_ack *extack)
+{
+       *direction = DPLL_PIN_DIRECTION_INPUT;
+       return 0;
+}
+
+static const struct dpll_pin_ops dt_pin_ops = {
+       .state_on_dpll_get = dt_state_get,
+       .direction_get = dt_direction_get,
+};
+
+static const struct dpll_pin_properties dt_pin_prop = {
+       .type = DPLL_PIN_TYPE_EXT,
+};
+
+static int dt_notify(struct notifier_block *nb, unsigned long action,
+                    void *data)
+{
+       struct dt_ctx *ctx = container_of(nb, struct dt_ctx, nb);
+       struct dpll_pin_notifier_info *info = data;
+
+       if (action != DPLL_PIN_CHANGED)
+               return NOTIFY_DONE;
+       if (info->pin == ctx->p.pin)
+               ctx->p.changes++;
+       else if (info->pin == ctx->u.pin)
+               ctx->u.changes++;
+       else if (info->pin == ctx->c.pin)
+               ctx->c.changes++;
+
+       return NOTIFY_OK;
+}
+
+static void dt_dev_init(struct kunit *test, struct dt_dev *dev, u64 clock_id,
+                       u32 idx)
+{
+       int ret;
+
+       dev->dpll = dpll_device_get(clock_id, idx, THIS_MODULE, &dev->tracker);
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev->dpll);
+       ret = dpll_device_register(dev->dpll, DPLL_TYPE_EEC, &dt_dev_ops, dev);
+       KUNIT_ASSERT_EQ(test, ret, 0);
+       dev->registered = true;
+}
+
+static void dt_dev_fini(struct dt_dev *dev)
+{
+       if (IS_ERR_OR_NULL(dev->dpll))
+               return;
+       if (dev->registered)
+               dpll_device_unregister(dev->dpll, &dt_dev_ops, dev);
+       dpll_device_put(dev->dpll, &dev->tracker);
+}
+
+static void dt_pin_get(struct kunit *test, struct dt_pin *pin, u64 clock_id,
+                      u32 idx)
+{
+       pin->pin = dpll_pin_get(clock_id, idx, THIS_MODULE, &dt_pin_prop,
+                               &pin->tracker);
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pin->pin);
+}
+
+static void dt_pin_put(struct dt_pin *pin)
+{
+       if (!IS_ERR_OR_NULL(pin->pin))
+               dpll_pin_put(pin->pin, &pin->tracker);
+}
+
+static void dt_reg(struct kunit *test, struct dt_dev *dev, struct dt_pin *pin,
+                  bool *registered)
+{
+       int ret;
+
+       ret = dpll_pin_register(dev->dpll, pin->pin, &dt_pin_ops, pin);
+       KUNIT_ASSERT_EQ(test, ret, 0);
+       *registered = true;
+}
+
+static void dt_unreg(struct dt_dev *dev, struct dt_pin *pin, bool *registered)
+{
+       if (!*registered)
+               return;
+       dpll_pin_unregister(dev->dpll, pin->pin, &dt_pin_ops, pin);
+       *registered = false;
+}
+
+/* Fill @ids with the pins reported as linked with @pin, return their count */
+static int dt_links(struct kunit *test, struct dt_pin *pin, u32 *ids)
+{
+       struct sk_buff *msg;
+       struct nlattr *attr;
+       int n = 0, rem, ret;
+
+       msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
+       KUNIT_ASSERT_NOT_NULL(test, msg);
+       mutex_lock(&dpll_lock);
+       ret = dpll_cmd_pin_get_one(msg, pin->pin, NULL);
+       mutex_unlock(&dpll_lock);
+       if (!ret)
+               nla_for_each_attr_type(attr, DPLL_A_PIN_LINK,
+                                      (struct nlattr *)msg->data, msg->len,
+                                      rem) {
+                       struct nlattr *id = nla_find_nested(attr,
+                                                           DPLL_A_PIN_ID);
+
+                       if (id && n < DT_MAX_LINKS)
+                               ids[n] = nla_get_u32(id);
+                       n++;
+               }
+       nlmsg_free(msg);
+       KUNIT_ASSERT_EQ(test, ret, 0);
+       KUNIT_ASSERT_LE(test, n, DT_MAX_LINKS);
+
+       return n;
+}
+
+/* Check that @pin is reported as linked with @peer only */
+static void dt_expect_link(struct kunit *test, struct dt_pin *pin,
+                          struct dt_pin *peer)
+{
+       u32 ids[DT_MAX_LINKS];
+
+       KUNIT_ASSERT_EQ(test, dt_links(test, pin, ids), 1);
+       KUNIT_EXPECT_EQ(test, ids[0], peer->pin->id);
+}
+
+static void dt_expect_no_link(struct kunit *test, struct dt_pin *pin)
+{
+       u32 ids[DT_MAX_LINKS];
+
+       KUNIT_EXPECT_EQ(test, dt_links(test, pin, ids), 0);
+}
+
+static int dt_init(struct kunit *test)
+{
+       struct dt_ctx *ctx;
+       int ret;
+
+       ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+       KUNIT_ASSERT_NOT_NULL(test, ctx);
+       test->priv = ctx;
+
+       ctx->fwnode = kunit_fwnode_create_software_node(test, NULL, NULL);
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->fwnode);
+
+       dt_dev_init(test, &ctx->a0, DT_CLOCK_ID_A, 0);
+       dt_dev_init(test, &ctx->a1, DT_CLOCK_ID_A, 1);
+       dt_dev_init(test, &ctx->b0, DT_CLOCK_ID_B, 0);
+       dt_pin_get(test, &ctx->p, DT_CLOCK_ID_A, 0);
+       dt_pin_get(test, &ctx->u, DT_CLOCK_ID_A, 1);
+       dt_pin_get(test, &ctx->c, DT_CLOCK_ID_B, 0);
+       dpll_pin_fwnode_set(ctx->p.pin, ctx->fwnode);
+       ret = dpll_pin_link_add(ctx->c.pin, ctx->fwnode);
+       KUNIT_ASSERT_EQ(test, ret, 0);
+
+       ctx->nb.notifier_call = dt_notify;
+       ret = register_dpll_notifier(&ctx->nb);
+       KUNIT_ASSERT_EQ(test, ret, 0);
+       ctx->nb_registered = true;
+
+       return 0;
+}
+
+static void dt_exit(struct kunit *test)
+{
+       struct dt_ctx *ctx = test->priv;
+
+       if (!ctx)
+               return;
+       if (ctx->nb_registered)
+               unregister_dpll_notifier(&ctx->nb);
+       dt_unreg(&ctx->a0, &ctx->p, &ctx->p_a0);
+       dt_unreg(&ctx->a1, &ctx->p, &ctx->p_a1);
+       dt_unreg(&ctx->a0, &ctx->u, &ctx->u_a0);
+       dt_unreg(&ctx->b0, &ctx->c, &ctx->c_b0);
+       dt_pin_put(&ctx->c);
+       dt_pin_put(&ctx->u);
+       dt_pin_put(&ctx->p);
+       dt_dev_fini(&ctx->b0);
+       dt_dev_fini(&ctx->a1);
+       dt_dev_fini(&ctx->a0);
+}
+
+/* The pin with the link is registered before the pin it names */
+static void dt_test_link_first(struct kunit *test)
+{
+       struct dt_ctx *ctx = test->priv;
+
+       dt_reg(test, &ctx->b0, &ctx->c, &ctx->c_b0);
+       dt_expect_no_link(test, &ctx->c);
+
+       dt_reg(test, &ctx->a0, &ctx->p, &ctx->p_a0);
+       KUNIT_EXPECT_EQ(test, ctx->c.changes, 1);
+       dt_expect_link(test, &ctx->c, &ctx->p);
+       dt_expect_link(test, &ctx->p, &ctx->c);
+}
+
+/* The pin named by the link is registered first */
+static void dt_test_link_last(struct kunit *test)
+{
+       struct dt_ctx *ctx = test->priv;
+
+       dt_reg(test, &ctx->a0, &ctx->p, &ctx->p_a0);
+       dt_expect_no_link(test, &ctx->p);
+
+       dt_reg(test, &ctx->b0, &ctx->c, &ctx->c_b0);
+       KUNIT_EXPECT_EQ(test, ctx->p.changes, 1);
+       dt_expect_link(test, &ctx->p, &ctx->c);
+       dt_expect_link(test, &ctx->c, &ctx->p);
+}
+
+/* The pin named by the link goes away and comes back */
+static void dt_test_peer_reregister(struct kunit *test)
+{
+       struct dt_ctx *ctx = test->priv;
+
+       dt_reg(test, &ctx->b0, &ctx->c, &ctx->c_b0);
+       dt_reg(test, &ctx->a0, &ctx->p, &ctx->p_a0);
+       KUNIT_EXPECT_EQ(test, ctx->c.changes, 1);
+
+       dt_unreg(&ctx->a0, &ctx->p, &ctx->p_a0);
+       KUNIT_EXPECT_EQ(test, ctx->c.changes, 2);
+       dt_expect_no_link(test, &ctx->c);
+
+       dt_reg(test, &ctx->a0, &ctx->p, &ctx->p_a0);
+       KUNIT_EXPECT_EQ(test, ctx->c.changes, 3);
+       dt_expect_link(test, &ctx->c, &ctx->p);
+}
+
+/* The link stays with its pin when the pin is unregistered */
+static void dt_test_link_reregister(struct kunit *test)
+{
+       struct dt_ctx *ctx = test->priv;
+
+       dt_reg(test, &ctx->a0, &ctx->p, &ctx->p_a0);
+       dt_reg(test, &ctx->b0, &ctx->c, &ctx->c_b0);
+       KUNIT_EXPECT_EQ(test, ctx->p.changes, 1);
+
+       dt_unreg(&ctx->b0, &ctx->c, &ctx->c_b0);
+       KUNIT_EXPECT_EQ(test, ctx->p.changes, 2);
+       dt_expect_no_link(test, &ctx->p);
+
+       dt_reg(test, &ctx->b0, &ctx->c, &ctx->c_b0);
+       KUNIT_EXPECT_EQ(test, ctx->p.changes, 3);
+       dt_expect_link(test, &ctx->p, &ctx->c);
+}
+
+/* Only the first registration and the last unregistration change the link */
+static void dt_test_peer_two_dplls(struct kunit *test)
+{
+       struct dt_ctx *ctx = test->priv;
+
+       dt_reg(test, &ctx->b0, &ctx->c, &ctx->c_b0);
+       dt_reg(test, &ctx->a0, &ctx->p, &ctx->p_a0);
+       dt_reg(test, &ctx->a1, &ctx->p, &ctx->p_a1);
+       KUNIT_EXPECT_EQ(test, ctx->c.changes, 1);
+
+       dt_unreg(&ctx->a0, &ctx->p, &ctx->p_a0);
+       KUNIT_EXPECT_EQ(test, ctx->c.changes, 1);
+       dt_expect_link(test, &ctx->c, &ctx->p);
+
+       dt_unreg(&ctx->a1, &ctx->p, &ctx->p_a1);
+       KUNIT_EXPECT_EQ(test, ctx->c.changes, 2);
+       dt_expect_no_link(test, &ctx->c);
+}
+
+/* A pin without a link and a firmware node is not linked or notified */
+static void dt_test_unrelated(struct kunit *test)
+{
+       struct dt_ctx *ctx = test->priv;
+
+       dt_reg(test, &ctx->a0, &ctx->u, &ctx->u_a0);
+       dt_reg(test, &ctx->b0, &ctx->c, &ctx->c_b0);
+       dt_reg(test, &ctx->a0, &ctx->p, &ctx->p_a0);
+       dt_unreg(&ctx->a0, &ctx->p, &ctx->p_a0);
+
+       KUNIT_EXPECT_EQ(test, ctx->u.changes, 0);
+       dt_expect_no_link(test, &ctx->u);
+       dt_expect_no_link(test, &ctx->c);
+}
+
+/* A duplicate link is rejected, also on a registered pin */
+static void dt_test_add_duplicate(struct kunit *test)
+{
+       struct dt_ctx *ctx = test->priv;
+       int ret;
+
+       ret = dpll_pin_link_add(ctx->c.pin, ctx->fwnode);
+       KUNIT_EXPECT_EQ(test, ret, -EEXIST);
+       dt_reg(test, &ctx->b0, &ctx->c, &ctx->c_b0);
+
+       kunit_warning_suppress(test) {
+               ret = dpll_pin_link_add(ctx->c.pin, ctx->fwnode);
+               KUNIT_EXPECT_EQ(test, ret, -EEXIST);
+               KUNIT_EXPECT_SUPPRESSED_WARNING_COUNT(test, 0);
+       }
+}
+
+static void dt_test_add_registered(struct kunit *test)
+{
+       struct dt_ctx *ctx = test->priv;
+       struct fwnode_handle *other;
+
+       other = kunit_fwnode_create_software_node(test, NULL, NULL);
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, other);
+       dt_reg(test, &ctx->b0, &ctx->c, &ctx->c_b0);
+
+       kunit_warning_suppress(test) {
+               KUNIT_EXPECT_EQ(test, dpll_pin_link_add(ctx->c.pin, other),
+                               -EBUSY);
+               KUNIT_EXPECT_SUPPRESSED_WARNING_COUNT(test, 1);
+       }
+}
+
+static struct kunit_case dt_cases[] = {
+       KUNIT_CASE(dt_test_link_first),
+       KUNIT_CASE(dt_test_link_last),
+       KUNIT_CASE(dt_test_peer_reregister),
+       KUNIT_CASE(dt_test_link_reregister),
+       KUNIT_CASE(dt_test_peer_two_dplls),
+       KUNIT_CASE(dt_test_unrelated),
+       KUNIT_CASE(dt_test_add_duplicate),
+       KUNIT_CASE(dt_test_add_registered),
+       {}
+};
+
+static struct kunit_suite dt_suite = {
+       .name = "dpll_pin_link",
+       .init = dt_init,
+       .exit = dt_exit,
+       .test_cases = dt_cases,
+};
+
+kunit_test_suite(dt_suite);
+
+MODULE_DESCRIPTION("KUnit tests for links between DPLL pins");
+MODULE_LICENSE("GPL");
-- 
2.56.1

Reply via email to