Netdev List
 help / color / mirror / Atom feed
* [PATCH net-next v5 00/14] dpll: add a simulated DPLL device driver
@ 2026-11-25  9:05 Petr Oros
  2026-11-25  9:05 ` [PATCH net-next v5 01/14] dpll: dpllsim: Add " Petr Oros
                   ` (13 more replies)
  0 siblings, 14 replies; 15+ messages in thread
From: Petr Oros @ 2026-11-25  9:05 UTC (permalink / raw)
  To: netdev
  Cc: Petr Oros, Jonathan Corbet, Shuah Khan, Shuah Khan, Randy Dunlap,
	Jani Nikula, Vadim Fedorenko, Arkadiusz Kubalewski, Jiri Pirko,
	Ivan Vecera, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, linux-doc, linux-kselftest

The DPLL subsystem has no in-tree driver that can run without real
hardware, so core changes and userspace tooling are hard to exercise in
CI, and the pin-on-pin and reference sync paths see almost no coverage
outside the vendors' labs. This series adds dpllsim, a simulated DPLL
device driver, together with selftests that run against it.

The driver is controlled through configfs, following gpio-sim. Writing a
clock ID into a device directory and setting "live" to 1 registers a
DPLL device with the core. Pins are directories under pins/ with their
type, frequency, direction and priority as attributes, and all of them
are registered when the device goes live. The split between creating
the configuration and going live keeps the register and unregister
paths reachable from userspace, so they can be exercised repeatedly
without reloading the module.

The patches follow the shape of the zl3073x introduction. The
driver starts with the device alone, then gains pins and the static pin
operations one at a time (state, priority, frequency, phase adjustment),
and every feature patch is followed by the selftest that covers it. The
last two patches add concurrency stress tests and reference tracker
leak tests for the lifecycle paths.

The simulation loop (lock state machine, phase and frequency
measurements, monitoring features), the topology features (MUX pin
hierarchies, reference sync, media failure injection), netdevsim
integration and device mode setting follow in two further series once
this one is in, to stay within the 15 patch limit.

The driver and the selftests were developed with an LLM coding
assistant (Claude Code) working under my direction. I chose the
configfs design, the split into one feature per patch and the test
scenarios, and I reviewed every line. The assistant wrote most of the
code and the first drafts of the changelogs, and it found and fixed
bugs by running the suite on the lab host. The series then went
through several rounds of an LLM based patch reviewer whose findings
were fixed, and through a mutation test in which deliberately broken
driver and core code was caught by the tests.

The selftests live in tools/testing/selftests/drivers/dpll and run
with "make -C tools/testing/selftests TARGETS=drivers/dpll run_tests".
The feature, stress and reference leak scripts create dpllsim devices.
The basic script, which only reads, takes the first hardware DPLL device
when there is one and creates a dpllsim device otherwise. With
DPLL_DEV_ID=<id> the basic and feature scripts run against an existing
device, so the same suite exercises real hardware, and
DPLL_DRIVER=<module name> picks the first device of that driver, dpllsim
for a simulated one. The whole suite was run through the kselftest
runner in a KVM guest with CONFIG_KASAN, CONFIG_PROVE_LOCKING and
CONFIG_DPLL_REFCNT_TRACKER enabled, and the basic and feature scripts
were run with DPLL_DRIVER=ice against the DPLL of an ice E810 host,
where the pin state and priorities of the hardware are left as they
were. All pass with no warnings in dmesg, on ice the priority test skips
on the EEC device, whose spare pin sits at the lowest priority already.

---
v5:
- address the findings of the netdev AI review of v4 and the ones of
  v3 that v4 left open
- dpllsim: document the phase adjust range, its granularity and its
  reset, that a pin state set over netlink is not kept across a live
  transition either and the errors of a live write for the clock ID;
  the Kconfig help no longer promises pin topologies
- selftests: skip when dpllsim is built in, check before every test
  that the pin is still spare and do not let a second signal cut the
  teardown short, in the stress workers neither
- selftests: take a pin only when every device it is registered on
  shows which input it uses or reports unlocked, and leave the NCO
  input, which iproute2 prints as a pin of unknown type so far, and
  both pins of a connected reference sync pair alone
- selftests: leave manual mode out of the state test until the
  simulator can change its mode, and run the deferred restores only
  when the test ends early
- selftests: raise the priority of the test pin only when the
  selection does not consider it, so that it cannot take over from the
  reference, and try no priority above 255, which ice truncates to 8
  bits
- selftests: offer a disconnected pin to the selection of a device
  provided by the user only when it ranks below the reference on every
  device it is registered on, and step the phase adjust and the
  frequency by small amounts instead of going to the ends of the range
- selftests: write the current phase adjust back before the test, like
  the priority and the frequency
- selftests: fail when a device does not report its mode or a pin its
  state, both are mandatory
- selftests: do not hand a stress worker a clock-id range in which a
  leftover device sits, and fail the test when the devices of a worker
  cannot be removed
- selftests: report live objects without a tracked reference as leaks,
  move the ref_tracker probe test to the library, drop the final check
  of dpll_reftrack.sh, which repeated the one before it, and skip the
  leak check when a device was left behind
- selftests: keep the standard pin layout and the removal of what the
  ref_tracker probe leaves behind in the library, name the reason of a
  refused restore and skip a state flood of no changes
- MAINTAINERS: list the selftests under the DPLL subsystem as well
- dpllsim: say in the documentation that a failed live write does not
  name the pin
- selftests: the basic test goes without the lock for a run against a
  given device, a failed pin dump of the stress tests is one failure
  and a too narrow phase adjust range fails on the created device
- dpllsim: drop the initial reference of the pins group with the device
- selftests: make the run directory private, skip a stress test whose
  count is not a positive number and keep the deferred state restore
  armed when a restore is refused
- selftests: pass over the clock-ids of devices left behind by an
  earlier run, require mountpoint and fail a leak check whose tracker
  dir is gone
- selftests: skip on a failed device dump only when the kernel has no
  DPLL netlink family, and look a device the user named up in the dump
- selftests: report a value that cannot be read back as such, pass over
  clock-ids other simulated devices hold and return the status of a
  failed restore
- selftests: run the cleanup from the INT and TERM handlers, a signal
  that came while a script exited ended the exit trap before it, and
  track a device that fails half way and cannot be removed
- selftests: skip the state test when the selection took the pin since
  the spare check, and take a device that was there before the run for
  none of the run in the cleanup and in the leak check of the stress
  script
- dpllsim: say on deploy and undeploy that the callers hold the
  subsystem mutex
- selftests: fail the run when a deferred restore fails, a refused
  restore could leave a pin changed behind a skip
- dpllsim: say why the lock status error is NONE and split the
  documentation sentence on values set over netlink
- selftests: say why a failed exec redirection in the lock and the
  waits of the deploy cycle are safe
- selftests: skip the deferred restore of the priority, frequency and
  phase adjust tests once they restored the pin themselves
- selftests: run a deferred restore only while the pin is changed, take
  a refused restore of the priority, frequency and phase adjust tests
  like the state test does, and give the clock-id check a public name
- selftests: count the tuned pin as changed from before the set, so
  that a signal during the set restores it too, and track the device of
  the deploy cycle that fails half way
- selftests: say why the prereq check passes on a kernel without DPLL
  and why the priority test tries 1 and 7
- selftests: skip the priority test when the selection took the pin
  since the spare check, as the state test does, and move the range of
  a stress worker past every taken one
- correct comments and changelogs that did not match the code or the
  iproute2 dpll tool
- drop the tags of patches 6 and 8, whose tests were reworked, the
  other patches keep theirs

v4:
- address the findings of the netdev AI review of v3
- dpllsim: keep the priority and the frequency set over netlink apart
  from the configured values, so that every live transition starts from
  what configfs holds and the attributes keep showing it
- dpllsim: document the pin limit, the frequencies per pin type, the
  errors of the live write and what happens to values set over netlink;
  correct the comments on what the subsystem mutex covers
- selftests: tell a failed device or pin dump from a system without
  devices or pins, and fail instead of skipping when the simulated
  device cannot be created
- selftests: pick the spare input pin by all its parent devices, bail
  out before touching a pin whose current priority, frequency or phase
  adjust could not be written back, restore the reference after the
  test pin in manual mode, do not let a refusal hide an earlier failure
- selftests: signal only workers that are still children of the script,
  never delete a device named by the environment or one that existed
  before, record the mounts before mounting
- selftests: keep the files of freed objects out of the reference
  tracker snapshot, fail when the tracker is built in but records
  nothing, skip the final leak check when a device was left behind
- selftests: log deferred restores that fail, tell failed pin queries
  from missing attributes in the remaining places, fail a stress worker
  that a signal interrupted, enable CONFIG_IKCONFIG_PROC for the
  tracker probe
- drop the claim that configfs implies no stable ABI
- add the Reviewed-by given to patches 2, 5, 6 and 14 of v3 (Ivan
  Vecera) and keep the other tags, except on patches 7 and 9, where
  the behaviour of the driver changed and which ask for a new look

v3:
- move the selftests from tools/testing/selftests/drivers/net/dpll to
  tools/testing/selftests/drivers/dpll, with the shared library as a
  TEST_FILES entry of that directory, so that the tests sit where the
  drivers do
- let dpllsim refuse pin states that are not valid in the device mode:
  connected is refused in automatic mode, which the simulator runs in,
  as Documentation/driver-api/dpll.rst allows it there only for pins
  with the connected override capability (Ivan Vecera)
- make the pin state test follow the mode of the device under test and
  check on the simulator that connected is refused in automatic mode;
  cycle the stress tests through the states of automatic mode only
- add DPLL_DRIVER to pick the device of a given driver, or the simulator
  with DPLL_DRIVER=dpllsim, on a system that has hardware DPLL devices
- describe what the leak check waits for instead of a concurrent dump
  holding references, which nothing does (Ivan Vecera)
- carry the Reviewed-by and the set Acked-by of v2 over to the patches
  whose code did not change or only moved; patches 2, 5 and 6 changed
  and ask for a new look

v2:
- sort tools/testing/selftests/drivers/net/dpll/config alphabetically
  and make the scripts clean under the shellcheck run of the netdev CI
  (quoted exit and return values, shellcheck directives for the globals
  shared between the library and the scripts and for the optional
  argument of dpllsim_setup_standard_device())

v4: https://lore.kernel.org/all/20261112130359.840795-1-poros@redhat.com/
v3: https://lore.kernel.org/all/20261109093523.931201-1-poros@redhat.com/
v2: https://lore.kernel.org/all/20261105151852.1844305-1-poros@redhat.com/
v1: https://lore.kernel.org/all/20261103093910.596225-1-poros@redhat.com/
---

Petr Oros (14):
  dpll: dpllsim: Add simulated DPLL device driver
  selftests: dpll: add test library and device lifecycle tests
  dpll: dpllsim: Add configurable pins
  selftests: dpll: add pin enumeration and property tests
  dpll: dpllsim: Add support to set input pin state
  selftests: dpll: add pin state tests
  dpll: dpllsim: Add support to get/set priority on input pins
  selftests: dpll: add pin priority test
  dpll: dpllsim: Add support to set frequency on pins
  selftests: dpll: add pin frequency test
  dpll: dpllsim: Add support to adjust phase
  selftests: dpll: add pin phase adjust test
  selftests: dpll: add concurrency stress tests
  selftests: dpll: add reference leak tests

 Documentation/driver-api/dpllsim.rst          |  102 ++
 Documentation/driver-api/index.rst            |    1 +
 MAINTAINERS                                   |    9 +
 drivers/dpll/Kconfig                          |    1 +
 drivers/dpll/Makefile                         |    1 +
 drivers/dpll/dpllsim/Kconfig                  |   16 +
 drivers/dpll/dpllsim/Makefile                 |    4 +
 drivers/dpll/dpllsim/core.c                   |  483 +++++++
 drivers/dpll/dpllsim/dpll.c                   |  502 +++++++
 drivers/dpll/dpllsim/dpllsim.h                |  103 ++
 tools/testing/selftests/Makefile              |    1 +
 tools/testing/selftests/drivers/dpll/Makefile |   17 +
 tools/testing/selftests/drivers/dpll/config   |    8 +
 .../selftests/drivers/dpll/dpll_basic.sh      |  256 ++++
 .../selftests/drivers/dpll/dpll_features.sh   |  881 ++++++++++++
 .../selftests/drivers/dpll/dpll_lib.sh        | 1182 +++++++++++++++++
 .../selftests/drivers/dpll/dpll_reftrack.sh   |  111 ++
 .../selftests/drivers/dpll/dpll_stress.sh     |  639 +++++++++
 tools/testing/selftests/drivers/dpll/settings |    1 +
 19 files changed, 4318 insertions(+)
 create mode 100644 Documentation/driver-api/dpllsim.rst
 create mode 100644 drivers/dpll/dpllsim/Kconfig
 create mode 100644 drivers/dpll/dpllsim/Makefile
 create mode 100644 drivers/dpll/dpllsim/core.c
 create mode 100644 drivers/dpll/dpllsim/dpll.c
 create mode 100644 drivers/dpll/dpllsim/dpllsim.h
 create mode 100644 tools/testing/selftests/drivers/dpll/Makefile
 create mode 100644 tools/testing/selftests/drivers/dpll/config
 create mode 100755 tools/testing/selftests/drivers/dpll/dpll_basic.sh
 create mode 100755 tools/testing/selftests/drivers/dpll/dpll_features.sh
 create mode 100644 tools/testing/selftests/drivers/dpll/dpll_lib.sh
 create mode 100755 tools/testing/selftests/drivers/dpll/dpll_reftrack.sh
 create mode 100755 tools/testing/selftests/drivers/dpll/dpll_stress.sh
 create mode 100644 tools/testing/selftests/drivers/dpll/settings

-- 
2.56.1


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH net-next v5 01/14] dpll: dpllsim: Add simulated DPLL device driver
  2026-11-25  9:05 [PATCH net-next v5 00/14] dpll: add a simulated DPLL device driver Petr Oros
@ 2026-11-25  9:05 ` Petr Oros
  2026-11-25  9:05 ` [PATCH net-next v5 02/14] selftests: dpll: add test library and device lifecycle tests Petr Oros
                   ` (12 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: Petr Oros @ 2026-11-25  9:05 UTC (permalink / raw)
  To: netdev
  Cc: Petr Oros, Ivan Vecera, Jiri Pirko, Jonathan Corbet, Shuah Khan,
	Shuah Khan, Randy Dunlap, Jani Nikula, Vadim Fedorenko,
	Arkadiusz Kubalewski, Jiri Pirko, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, linux-doc,
	linux-kselftest

Add dpllsim, a driver providing simulated DPLL devices for testing the
DPLL subsystem and its userspace tooling without real hardware, in the
same spirit as netdevsim for network devices. The DPLL core identifies
a device by clock ID, index and module only, so the driver needs no bus
and no struct device at all.

Instances are created and configured at runtime through configfs,
following the gpio-sim precedent for objects that userspace creates on
demand. Creating a directory under /sys/kernel/config/dpllsim/ allocates
an instance, its clock_id attribute selects the clock ID, writing 1 to
live registers the DPLL device with the core and writing 0 unregisters
it again. Removing the directory tears a live instance down implicitly.
Keeping registration separate from creation lets a test register and
unregister the same device repeatedly, which is where the core bugs the
driver is meant to catch tend to live, and it allows the structural
attributes to be refused with EBUSY while the device is visible to
userspace.

Reading live reports whether the device is registered with the DPLL
subsystem, it turns 1 only once the whole live transition succeeded and
0 again as the first step of the teardown. The configuration is frozen
earlier, from the moment the transition obtains the device handle, so
that a configuration write racing with the transition is refused rather
than lost. The clock ID cannot race yet, its store takes the mutex of
the transition, the pin attributes of the following patches can.

The device registers as a PPS type DPLL with the two mandatory callbacks
only. mode_get reports automatic mode and lock_status_get reports
unlocked with no error. Pins come with the following patches and the
simulation of lock acquisition with a later series.

Document the configfs layout under Documentation/driver-api and add a
MAINTAINERS entry.

Reviewed-by: Ivan Vecera <ivecera@redhat.com>
Acked-by: Jiri Pirko <jiri@nvidia.com>
Signed-off-by: Petr Oros <poros@redhat.com>
---
v5:
- drop "arbitrary pin topologies" from the Kconfig help, the pins come
  with the following patches and are a flat list
- document the errors of a live write with a zero or an already used
  clock ID
- say in the changelog that the early freeze matters for the pin
  attributes of the following patches, the clock ID cannot race
- say in a comment why deploy takes the lock by hand, its unwind uses
  goto
- say on the prototypes of deploy and undeploy that the callers hold
  the subsystem mutex
- keep MODULE_LICENSE("GPL") next to the GPL-2.0-only SPDX tags,
  include/linux/module.h says that "GPL" states GPL v2 and not whether
  only or later, checkpatch prefers it to "GPL v2", zl3073x does the
  same
- keep the lock status error of the unlocked device at NONE and say so
  in a comment, the uAPI documents NONE as no error and netlink reports
  it as lock-status-error none, as mlx5 does when nothing failed

v4:
- drop the claim that configfs implies no stable ABI from the
  changelog, no code change

v4: https://lore.kernel.org/all/20261112130359.840795-2-poros@redhat.com/
v3: https://lore.kernel.org/all/20261109093523.931201-2-poros@redhat.com/
---
 Documentation/driver-api/dpllsim.rst |  42 +++++
 Documentation/driver-api/index.rst   |   1 +
 MAINTAINERS                          |   7 +
 drivers/dpll/Kconfig                 |   1 +
 drivers/dpll/Makefile                |   1 +
 drivers/dpll/dpllsim/Kconfig         |  16 ++
 drivers/dpll/dpllsim/Makefile        |   4 +
 drivers/dpll/dpllsim/core.c          | 222 +++++++++++++++++++++++++++
 drivers/dpll/dpllsim/dpll.c          |  89 +++++++++++
 drivers/dpll/dpllsim/dpllsim.h       |  59 +++++++
 10 files changed, 442 insertions(+)
 create mode 100644 Documentation/driver-api/dpllsim.rst
 create mode 100644 drivers/dpll/dpllsim/Kconfig
 create mode 100644 drivers/dpll/dpllsim/Makefile
 create mode 100644 drivers/dpll/dpllsim/core.c
 create mode 100644 drivers/dpll/dpllsim/dpll.c
 create mode 100644 drivers/dpll/dpllsim/dpllsim.h

diff --git a/Documentation/driver-api/dpllsim.rst b/Documentation/driver-api/dpllsim.rst
new file mode 100644
index 00000000000000..5683811ccfc4c0
--- /dev/null
+++ b/Documentation/driver-api/dpllsim.rst
@@ -0,0 +1,42 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+==========================
+DPLL simulator (dpllsim)
+==========================
+
+The dpllsim driver provides simulated DPLL devices for testing the DPLL
+subsystem and its userspace tooling without real hardware, similar to
+what netdevsim provides for network devices.
+
+Configuration interface
+=======================
+
+Devices are described in configfs under ``/sys/kernel/config/dpllsim/``.
+A directory created there is a device instance. Attributes are written
+while the instance is not live, then ``live`` is set to 1 to register
+the device with the DPLL subsystem. Writing 0 unregisters it again.
+Structural attributes return ``EBUSY`` while the instance is live.
+
+Device directory ``/sys/kernel/config/dpllsim/<dev>/``:
+
+=======================  =====================================================
+``clock_id``             Clock ID of the DPLL device. Writing 1 to ``live``
+                         fails with ``EINVAL`` while it is 0 and with
+                         ``EEXIST`` when another live instance uses it
+``live``                 1 registers the device, 0 unregisters it
+=======================  =====================================================
+
+Example
+=======
+
+Create a device and make it live::
+
+    cd /sys/kernel/config/dpllsim
+    mkdir dev0 && cd dev0
+    echo 100 > clock_id
+    echo 1 > live
+
+    dpll device show
+
+To remove the device, write 0 to ``live`` and remove the device
+directory.
diff --git a/Documentation/driver-api/index.rst b/Documentation/driver-api/index.rst
index aaf5d76ed074ed..e2271889e33400 100644
--- a/Documentation/driver-api/index.rst
+++ b/Documentation/driver-api/index.rst
@@ -87,6 +87,7 @@ Subsystem-specific APIs
    crypto/index
    dmaengine/index
    dpll
+   dpllsim
    edac
    extcon
    firmware/index
diff --git a/MAINTAINERS b/MAINTAINERS
index 97308edec53dd7..f714b4e61b418d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -8115,6 +8115,13 @@ F:	Documentation/networking/device_drivers/ethernet/freescale/dpaa2/switch-drive
 F:	drivers/net/ethernet/freescale/dpaa2/dpaa2-switch*
 F:	drivers/net/ethernet/freescale/dpaa2/dpsw*
 
+DPLL SIMULATOR DRIVER (dpllsim)
+M:	Petr Oros <poros@redhat.com>
+L:	netdev@vger.kernel.org
+S:	Maintained
+F:	Documentation/driver-api/dpllsim.rst
+F:	drivers/dpll/dpllsim/
+
 DPLL SUBSYSTEM
 M:	Vadim Fedorenko <vadim.fedorenko@linux.dev>
 M:	Arkadiusz Kubalewski <arkadiusz.kubalewski@intel.com>
diff --git a/drivers/dpll/Kconfig b/drivers/dpll/Kconfig
index be98969f040ab6..c6bd5392ce8753 100644
--- a/drivers/dpll/Kconfig
+++ b/drivers/dpll/Kconfig
@@ -24,5 +24,6 @@ config DPLL_REFCNT_TRACKER
 	  If unsure, say N.
 
 source "drivers/dpll/zl3073x/Kconfig"
+source "drivers/dpll/dpllsim/Kconfig"
 
 endmenu
diff --git a/drivers/dpll/Makefile b/drivers/dpll/Makefile
index 9e7a3a3e592e81..1ee1daa3a46cc7 100644
--- a/drivers/dpll/Makefile
+++ b/drivers/dpll/Makefile
@@ -9,3 +9,4 @@ dpll-y                  += dpll_netlink.o
 dpll-y                  += dpll_nl.o
 
 obj-$(CONFIG_ZL3073X)	+= zl3073x/
+obj-$(CONFIG_DPLLSIM)	+= dpllsim/
diff --git a/drivers/dpll/dpllsim/Kconfig b/drivers/dpll/dpllsim/Kconfig
new file mode 100644
index 00000000000000..49d14d49afe844
--- /dev/null
+++ b/drivers/dpll/dpllsim/Kconfig
@@ -0,0 +1,16 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+config DPLLSIM
+	tristate "DPLL simulator driver"
+	depends on NET
+	select DPLL
+	select CONFIGFS_FS
+	help
+	  This driver provides simulated DPLL devices for testing and
+	  development of the DPLL subsystem and its userspace tooling
+	  without real hardware. The devices are created and configured
+	  at runtime through configfs, similar to what gpio-sim provides
+	  for GPIO.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called dpllsim.
diff --git a/drivers/dpll/dpllsim/Makefile b/drivers/dpll/dpllsim/Makefile
new file mode 100644
index 00000000000000..905df2ee00874f
--- /dev/null
+++ b/drivers/dpll/dpllsim/Makefile
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+obj-$(CONFIG_DPLLSIM) += dpllsim.o
+dpllsim-y := core.o dpll.o
diff --git a/drivers/dpll/dpllsim/core.c b/drivers/dpll/dpllsim/core.c
new file mode 100644
index 00000000000000..779eea5f5b140c
--- /dev/null
+++ b/drivers/dpll/dpllsim/core.c
@@ -0,0 +1,222 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * DPLL simulator - configfs control plane
+ * Copyright (c) 2026 Red Hat, Inc.
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/cleanup.h>
+#include <linux/configfs.h>
+#include <linux/kstrtox.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+#include <linux/sysfs.h>
+
+#include "dpllsim.h"
+
+static struct configfs_subsystem dpllsim_subsys;
+
+static struct dpllsim_dev *to_dpllsim_dev(struct config_item *item)
+{
+	return container_of(to_config_group(item), struct dpllsim_dev, group);
+}
+
+/* The clock id is checked at live=1, so both share the subsystem mutex */
+static ssize_t dpllsim_dev_clock_id_show(struct config_item *item, char *page)
+{
+	struct dpllsim_dev *sim = to_dpllsim_dev(item);
+
+	guard(mutex)(&dpllsim_subsys.su_mutex);
+	return sysfs_emit(page, "%llu\n", sim->clock_id);
+}
+
+static ssize_t dpllsim_dev_clock_id_store(struct config_item *item,
+					  const char *page, size_t count)
+{
+	struct dpllsim_dev *sim = to_dpllsim_dev(item);
+	u64 val;
+	int err;
+
+	err = kstrtou64(page, 10, &val);
+	if (err)
+		return err;
+
+	guard(mutex)(&dpllsim_subsys.su_mutex);
+	if (dpllsim_dev_frozen(sim))
+		return -EBUSY;
+	sim->clock_id = val;
+	return count;
+}
+CONFIGFS_ATTR(dpllsim_dev_, clock_id);
+
+static ssize_t dpllsim_dev_live_show(struct config_item *item, char *page)
+{
+	struct dpllsim_dev *sim = to_dpllsim_dev(item);
+
+	guard(mutex)(&sim->lock);
+	return sysfs_emit(page, "%d\n", dpllsim_dev_live(sim));
+}
+
+/*
+ * Two live instances must not share a clock id, dpll_device_get() would
+ * hand out the same dpll_device to both of them instead of registering
+ * a new one.
+ */
+static bool dpllsim_clock_id_in_use(struct dpllsim_dev *sim)
+{
+	struct config_item *item;
+
+	lockdep_assert_held(&dpllsim_subsys.su_mutex);
+
+	list_for_each_entry(item, &dpllsim_subsys.su_group.cg_children,
+			    ci_entry) {
+		struct dpllsim_dev *other = to_dpllsim_dev(item);
+
+		if (other != sim && dpllsim_dev_live(other) &&
+		    other->clock_id == sim->clock_id)
+			return true;
+	}
+
+	return false;
+}
+
+/*
+ * The subsystem mutex excludes the live transitions of the other
+ * instances for the whole deploy sequence.
+ */
+static ssize_t dpllsim_dev_live_store(struct config_item *item,
+				      const char *page, size_t count)
+{
+	struct dpllsim_dev *sim = to_dpllsim_dev(item);
+	bool live;
+	int err;
+
+	err = kstrtobool(page, &live);
+	if (err)
+		return err;
+
+	guard(mutex)(&dpllsim_subsys.su_mutex);
+
+	if (live == dpllsim_dev_live(sim))
+		return count;
+
+	if (!live) {
+		dpllsim_dev_undeploy(sim);
+		return count;
+	}
+
+	if (!sim->clock_id)
+		return -EINVAL;
+	if (dpllsim_clock_id_in_use(sim))
+		return -EEXIST;
+
+	err = dpllsim_dev_deploy(sim);
+	if (err)
+		return err;
+
+	return count;
+}
+CONFIGFS_ATTR(dpllsim_dev_, live);
+
+static struct configfs_attribute *dpllsim_dev_attrs[] = {
+	&dpllsim_dev_attr_clock_id,
+	&dpllsim_dev_attr_live,
+	NULL,
+};
+
+static void dpllsim_dev_release(struct config_item *item)
+{
+	struct dpllsim_dev *sim = to_dpllsim_dev(item);
+
+	mutex_destroy(&sim->lock);
+	kfree(sim);
+}
+
+static const struct configfs_item_operations dpllsim_dev_item_ops = {
+	.release	= dpllsim_dev_release,
+};
+
+static const struct config_item_type dpllsim_dev_type = {
+	.ct_item_ops	= &dpllsim_dev_item_ops,
+	.ct_attrs	= dpllsim_dev_attrs,
+	.ct_owner	= THIS_MODULE,
+};
+
+static struct config_group *dpllsim_make_group(struct config_group *group,
+					       const char *name)
+{
+	struct dpllsim_dev *sim;
+
+	sim = kzalloc_obj(*sim);
+	if (!sim)
+		return ERR_PTR(-ENOMEM);
+
+	mutex_init(&sim->lock);
+	sim->mode = DPLL_MODE_AUTOMATIC;
+	sim->lock_status = DPLL_LOCK_STATUS_UNLOCKED;
+	/* uAPI value for no error, reported as lock-status-error none */
+	sim->lock_status_error = DPLL_LOCK_STATUS_ERROR_NONE;
+
+	config_group_init_type_name(&sim->group, name, &dpllsim_dev_type);
+
+	return &sim->group;
+}
+
+/* Runs under the subsystem mutex, unlike release */
+static void dpllsim_drop_item(struct config_group *group,
+			      struct config_item *item)
+{
+	struct dpllsim_dev *sim = to_dpllsim_dev(item);
+
+	if (dpllsim_dev_live(sim))
+		dpllsim_dev_undeploy(sim);
+	config_item_put(item);
+}
+
+static const struct configfs_group_operations dpllsim_group_ops = {
+	.make_group	= dpllsim_make_group,
+	.drop_item	= dpllsim_drop_item,
+};
+
+static const struct config_item_type dpllsim_type = {
+	.ct_group_ops	= &dpllsim_group_ops,
+	.ct_owner	= THIS_MODULE,
+};
+
+static struct configfs_subsystem dpllsim_subsys = {
+	.su_group = {
+		.cg_item = {
+			.ci_namebuf	= "dpllsim",
+			.ci_type	= &dpllsim_type,
+		},
+	},
+};
+
+static int __init dpllsim_init(void)
+{
+	int err;
+
+	config_group_init(&dpllsim_subsys.su_group);
+	mutex_init(&dpllsim_subsys.su_mutex);
+	err = configfs_register_subsystem(&dpllsim_subsys);
+	if (err) {
+		pr_err("failed to register the configfs subsystem: %d\n", err);
+		mutex_destroy(&dpllsim_subsys.su_mutex);
+	}
+
+	return err;
+}
+module_init(dpllsim_init);
+
+static void __exit dpllsim_exit(void)
+{
+	configfs_unregister_subsystem(&dpllsim_subsys);
+	mutex_destroy(&dpllsim_subsys.su_mutex);
+}
+module_exit(dpllsim_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Simulated DPLL device driver");
+MODULE_AUTHOR("Petr Oros <poros@redhat.com>");
diff --git a/drivers/dpll/dpllsim/dpll.c b/drivers/dpll/dpllsim/dpll.c
new file mode 100644
index 00000000000000..c127269fe9a232
--- /dev/null
+++ b/drivers/dpll/dpllsim/dpll.c
@@ -0,0 +1,89 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * DPLL simulator - DPLL subsystem interface
+ * Copyright (c) 2026 Red Hat, Inc.
+ */
+
+#include <linux/cleanup.h>
+#include <linux/dpll.h>
+#include <linux/module.h>
+
+#include "dpllsim.h"
+
+static int dpllsim_mode_get(const struct dpll_device *dpll, void *priv,
+			    enum dpll_mode *mode,
+			    struct netlink_ext_ack *extack)
+{
+	struct dpllsim_dev *sim = priv;
+
+	guard(mutex)(&sim->lock);
+	*mode = sim->mode;
+	return 0;
+}
+
+static int dpllsim_lock_status_get(const struct dpll_device *dpll, void *priv,
+				   enum dpll_lock_status *status,
+				   enum dpll_lock_status_error *status_error,
+				   struct netlink_ext_ack *extack)
+{
+	struct dpllsim_dev *sim = priv;
+
+	guard(mutex)(&sim->lock);
+	*status = sim->lock_status;
+	*status_error = sim->lock_status_error;
+	return 0;
+}
+
+static const struct dpll_device_ops dpllsim_dpll_ops = {
+	.mode_get = dpllsim_mode_get,
+	.lock_status_get = dpllsim_lock_status_get,
+};
+
+/*
+ * The unwind of a failed deploy uses goto, which include/linux/cleanup.h
+ * says not to mix with the scoped lock helpers, so the lock is taken by
+ * hand here.
+ */
+int dpllsim_dev_deploy(struct dpllsim_dev *sim)
+{
+	struct dpll_device *dpll;
+	int err;
+
+	dpll = dpll_device_get(sim->clock_id, 0, THIS_MODULE, &sim->tracker);
+	if (IS_ERR(dpll))
+		return PTR_ERR(dpll);
+
+	/* From here on the configuration is frozen */
+	mutex_lock(&sim->lock);
+	sim->dpll = dpll;
+	mutex_unlock(&sim->lock);
+
+	err = dpll_device_register(sim->dpll, DPLL_TYPE_PPS,
+				   &dpllsim_dpll_ops, sim);
+	if (err)
+		goto err_put_dpll;
+
+	mutex_lock(&sim->lock);
+	sim->registered = true;
+	mutex_unlock(&sim->lock);
+
+	return 0;
+
+err_put_dpll:
+	dpll_device_put(sim->dpll, &sim->tracker);
+	mutex_lock(&sim->lock);
+	sim->dpll = NULL;
+	mutex_unlock(&sim->lock);
+	return err;
+}
+
+void dpllsim_dev_undeploy(struct dpllsim_dev *sim)
+{
+	scoped_guard(mutex, &sim->lock)
+		sim->registered = false;
+
+	dpll_device_unregister(sim->dpll, &dpllsim_dpll_ops, sim);
+	dpll_device_put(sim->dpll, &sim->tracker);
+	scoped_guard(mutex, &sim->lock)
+		sim->dpll = NULL;
+}
diff --git a/drivers/dpll/dpllsim/dpllsim.h b/drivers/dpll/dpllsim/dpllsim.h
new file mode 100644
index 00000000000000..0b81bb2f769bee
--- /dev/null
+++ b/drivers/dpll/dpllsim/dpllsim.h
@@ -0,0 +1,59 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * DPLL simulator - internal header
+ * Copyright (c) 2026 Red Hat, Inc.
+ */
+
+#ifndef _DPLLSIM_H
+#define _DPLLSIM_H
+
+#include <linux/configfs.h>
+#include <linux/dpll.h>
+#include <linux/mutex.h>
+
+struct dpllsim_dev {
+	struct config_group group;
+
+	/*
+	 * Set from the start of a live transition until the device is
+	 * undeployed, written under both the subsystem mutex and @lock.
+	 * Freezes the configuration, see dpllsim_dev_frozen().
+	 */
+	struct dpll_device *dpll;
+	dpll_tracker tracker;
+	/*
+	 * True while the device is registered with the DPLL subsystem,
+	 * written under both the subsystem mutex and @lock. This is what
+	 * the live attribute reports, see dpllsim_dev_live().
+	 */
+	bool registered;
+
+	/* Guarded by the subsystem mutex */
+	u64 clock_id;
+
+	/* Simulated state, guarded by @lock */
+	enum dpll_mode mode;
+	enum dpll_lock_status lock_status;
+	enum dpll_lock_status_error lock_status_error;
+
+	/* Protects the simulated state */
+	struct mutex lock;
+};
+
+/* Configuration changes are refused from the start of a live transition */
+static inline bool dpllsim_dev_frozen(const struct dpllsim_dev *sim)
+{
+	return !!sim->dpll;
+}
+
+/* Live means registered with the DPLL subsystem */
+static inline bool dpllsim_dev_live(const struct dpllsim_dev *sim)
+{
+	return sim->registered;
+}
+
+/* Both are called with the subsystem mutex held */
+int dpllsim_dev_deploy(struct dpllsim_dev *sim);
+void dpllsim_dev_undeploy(struct dpllsim_dev *sim);
+
+#endif /* _DPLLSIM_H */
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH net-next v5 02/14] selftests: dpll: add test library and device lifecycle tests
  2026-11-25  9:05 [PATCH net-next v5 00/14] dpll: add a simulated DPLL device driver Petr Oros
  2026-11-25  9:05 ` [PATCH net-next v5 01/14] dpll: dpllsim: Add " Petr Oros
@ 2026-11-25  9:05 ` Petr Oros
  2026-11-25  9:05 ` [PATCH net-next v5 03/14] dpll: dpllsim: Add configurable pins Petr Oros
                   ` (11 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: Petr Oros @ 2026-11-25  9:05 UTC (permalink / raw)
  To: netdev
  Cc: Petr Oros, Ivan Vecera, Jonathan Corbet, Shuah Khan, Shuah Khan,
	Randy Dunlap, Jani Nikula, Vadim Fedorenko, Arkadiusz Kubalewski,
	Jiri Pirko, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, linux-doc, linux-kselftest

Add a shell library for DPLL selftests with wrappers around the iproute2
dpll tool and around the dpllsim configfs interface, and the first two
test scripts built on it. The library resolves devices by the clock ID
chosen at creation time, since netlink ids depend on what else exists on
the system, and the dpllsim helpers restrict the lookup to their module,
so that a device of another driver with the same clock ID is never
picked up. Every query runs the tool first and jq on what it printed, so
that a failed query is told apart from a missing attribute. That
includes the device dump the device selection reads. A dump that fails
is a failure, not a system without a device, unless the dpll tool
reports that the kernel has no DPLL subsystem, which is a skip. The
library loads the dpllsim module and mounts configfs when nothing else
did, and the cleanup of each script undoes the mount, removes the
devices the script created and runs on exit and on INT and TERM, so that
a timeout imposed by the harness still cleans up, and ignores any
further signal from then on, an exit inside it would end the teardown
half way. The module stays loaded. The scripts find the devices of the
simulator by the module name they report, which is "kernel" for a driver
that is built in, so they skip when dpllsim is not a module.

Two runs of scripts that create dpllsim devices would share the one
dpllsim directory and could draw the same clock IDs, so every such
script takes a lock before it mounts or creates anything and skips when
another run holds it. The lock file lives under /run rather than in
/tmp, since the scripts run as root and open the file for writing, and a
symlink planted in the world-writable /tmp would redirect that open. Its
directory is made private whatever the umask, for the same reason.

dpll_basic.sh checks that a device reports the mandatory attributes over
netlink. It runs against the device named by DPLL_DEV_ID, the first
device of the driver named by DPLL_DRIVER, or the first device that is
not a dpllsim one, and creates a dpllsim device when there is none or
when DPLL_DRIVER asks for dpllsim, so the same script serves the
simulator and real drivers and picks one of several drivers on a host. A
simulated device that cannot be created, registered or found on netlink
is a failure, only a missing module is a skip. dpll_stress.sh is
simulator only and exercises the lifecycle paths of the core by creating
and deleting devices in a loop and by cycling one device through
repeated register and unregister transitions. The new directory goes
into the dpllsim MAINTAINERS entry and into the one of the DPLL
subsystem, since the basic test checks the netlink interface of any DPLL
driver.

Reviewed-by: Ivan Vecera <ivecera@redhat.com>
Signed-off-by: Petr Oros <poros@redhat.com>
---
v5:
- skip when dpllsim is built in, the scripts find its devices by
  the module name, which is "kernel" for a built in driver
- ignore further signals in the INT and TERM handler and as the first
  thing of every cleanup, an exit inside the exit trap ends the
  teardown half way, and run the cleanup from the handler, a signal
  that came while the script exited ended the exit trap before the
  cleanup started
- say in the comments that a built in simulator is taken like any
  other device, what the unmount of configfs at exit means to others
  and that the name of a device is recorded before its directory is
  created
- describe the stress script in its header by what it is for, the
  header listed tests that later patches add
- correct two comments on the output of the tool, a query by id prints
  a flat object in every iproute2 version and the clock ID is always a
  JSON number
- list the selftest directory under the DPLL subsystem in MAINTAINERS
  as well, the basic test checks the netlink interface of any driver
- keep the name of a device whose directory cannot be removed after a
  failed clock ID write, so that the cleanup handler tries again
- take no lock in dpll_basic.sh for a run against a device the user
  named, by DPLL_DEV_ID or by a hardware driver in DPLL_DRIVER, it only
  reads that device, and say so in the library header
- create the run directory with mode 0700 and make an existing one
  private, whatever the umask, a symlink planted in a directory others
  can write to would redirect the open of the lock file
- skip a stress test whose count of rounds is not a positive number
- pass over a clock-id that a device in the dpllsim directory holds or
  ends the name of, a device left behind or one of someone else, the
  simulator refuses a second live instance with the same clock-id
- look a device the user named up in the dump, a device that is there
  but cannot be queried fails instead of passing for one that is absent
- require mountpoint, without it the cleanup cannot tell whether
  configfs is mounted
- skip on a failed device dump only when the dpll tool reports a kernel
  without the DPLL netlink family, a built in simulator or a kernel
  that has DPLL for another driver but no dpllsim took a broken dump
  for a skip
- track a stress device that fails half way and cannot be removed, the
  next device took over the name the cleanup handler retries
- record the devices that are there before the script, the cleanup took
  one of them for a device the script left behind and kept configfs
  mounted
- name the clock-id check dpllsim_clock_id_taken, the stress script of
  patch 13 uses it as well
- say in comments that a failed redirection makes exec return non-zero,
  bash exits for it only in POSIX mode, and that the waits of the
  deploy cycle cannot add up, the live write completes the transition
- track the device of the deploy cycle when it fails half way and cannot
  be removed, as for the stress devices
- a failed device dump is not made good by loading the simulator, DPLL
  is a bool its drivers select, so its netlink family is there from
  boot or not at all
- the rapid create and delete test deploys every device before it
  deletes it, so its loop goes through the register and unregister
  paths of the core as the description says
- keep the prereq check on the help of the dpll tool, every release
  prints it without connecting to the kernel, so a kernel without DPLL
  passes the check and the failed device dump is the skip it reports,
  and say so above the check

v4:
- tell a failed device dump from a system without a device, the dump
  is a failure unless the simulator is missing as well
- fail instead of skipping when the simulated device cannot be created,
  registered or found on netlink
- initialize DPLLSIM_CREATED_NAME, so that a value inherited from the
  environment cannot make a cleanup handler delete a device, and never
  record a directory that existed before as a device of the run
- record the configfs mount before mounting, so that a signal handled
  right after the mount still unmounts it
- correct the comments on the side effects of the library and on how it
  is used, drop the shellcheck directive of a function without
  arguments
- add the Reviewed-by given to v3 (Ivan Vecera)

v3:
- move the selftests to tools/testing/selftests/drivers/dpll, next to
  where the drivers live, with the library in that directory as a
  TEST_FILES entry; the TARGETS and MAINTAINERS entries follow
- add DPLL_DRIVER, which picks the first device of the named driver
  or, as dpllsim, a created simulated device on a system that has
  hardware DPLL devices
- the Reviewed-by and Acked-by of v2 are not carried over, the
  device selection changed

v2:
- sort the config file alphabetically (netdev CI check_selftest)
- quote the ksft_skip and ret expansions and add shellcheck directives
  for the globals read by the scripts, for ksft_skip, which comes from
  net/lib.sh, and for the optional argument of
  dpllsim_setup_standard_device() (netdev CI shellcheck)

v4: https://lore.kernel.org/all/20261112130359.840795-3-poros@redhat.com/
v3: https://lore.kernel.org/all/20261109093523.931201-3-poros@redhat.com/
v2: https://lore.kernel.org/all/20261105151852.1844305-3-poros@redhat.com/
v1: https://lore.kernel.org/all/20261103093910.596225-3-poros@redhat.com/
---
 MAINTAINERS                                   |   2 +
 tools/testing/selftests/Makefile              |   1 +
 tools/testing/selftests/drivers/dpll/Makefile |  15 +
 tools/testing/selftests/drivers/dpll/config   |   3 +
 .../selftests/drivers/dpll/dpll_basic.sh      | 107 +++++
 .../selftests/drivers/dpll/dpll_lib.sh        | 446 ++++++++++++++++++
 .../selftests/drivers/dpll/dpll_stress.sh     | 201 ++++++++
 tools/testing/selftests/drivers/dpll/settings |   1 +
 8 files changed, 776 insertions(+)
 create mode 100644 tools/testing/selftests/drivers/dpll/Makefile
 create mode 100644 tools/testing/selftests/drivers/dpll/config
 create mode 100755 tools/testing/selftests/drivers/dpll/dpll_basic.sh
 create mode 100644 tools/testing/selftests/drivers/dpll/dpll_lib.sh
 create mode 100755 tools/testing/selftests/drivers/dpll/dpll_stress.sh
 create mode 100644 tools/testing/selftests/drivers/dpll/settings

diff --git a/MAINTAINERS b/MAINTAINERS
index f714b4e61b418d..137b974d5819a7 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -8121,6 +8121,7 @@ L:	netdev@vger.kernel.org
 S:	Maintained
 F:	Documentation/driver-api/dpllsim.rst
 F:	drivers/dpll/dpllsim/
+F:	tools/testing/selftests/drivers/dpll/
 
 DPLL SUBSYSTEM
 M:	Vadim Fedorenko <vadim.fedorenko@linux.dev>
@@ -8136,6 +8137,7 @@ F:	Documentation/netlink/specs/dpll.yaml
 F:	drivers/dpll/
 F:	include/linux/dpll.h
 F:	include/uapi/linux/dpll.h
+F:	tools/testing/selftests/drivers/dpll/
 
 DRBD DRIVER
 M:	Philipp Reisner <philipp.reisner@linbit.com>
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index c55d7792b27c44..3978b20d87e0b3 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -20,6 +20,7 @@ TARGETS += devices/error_logs
 TARGETS += devices/probe
 TARGETS += dmabuf-heaps
 TARGETS += drivers/dma-buf
+TARGETS += drivers/dpll
 TARGETS += drivers/ntsync
 TARGETS += drivers/s390x/uvdevice
 TARGETS += drivers/net
diff --git a/tools/testing/selftests/drivers/dpll/Makefile b/tools/testing/selftests/drivers/dpll/Makefile
new file mode 100644
index 00000000000000..7e839ae71cd22f
--- /dev/null
+++ b/tools/testing/selftests/drivers/dpll/Makefile
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0
+
+TEST_INCLUDES := \
+	$(wildcard ../../net/lib/sh/*.sh) \
+	../../net/lib.sh \
+# end of TEST_INCLUDES
+
+TEST_FILES := dpll_lib.sh
+
+TEST_PROGS := \
+	dpll_basic.sh \
+	dpll_stress.sh \
+# end of TEST_PROGS
+
+include ../../lib.mk
diff --git a/tools/testing/selftests/drivers/dpll/config b/tools/testing/selftests/drivers/dpll/config
new file mode 100644
index 00000000000000..e2c90b6970bd43
--- /dev/null
+++ b/tools/testing/selftests/drivers/dpll/config
@@ -0,0 +1,3 @@
+CONFIG_CONFIGFS_FS=y
+CONFIG_DPLL=y
+CONFIG_DPLLSIM=m
diff --git a/tools/testing/selftests/drivers/dpll/dpll_basic.sh b/tools/testing/selftests/drivers/dpll/dpll_basic.sh
new file mode 100755
index 00000000000000..a713f3651a603a
--- /dev/null
+++ b/tools/testing/selftests/drivers/dpll/dpll_basic.sh
@@ -0,0 +1,107 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# Basic DPLL subsystem test. Read-only validation of the device properties
+# exposed over netlink. Runs against a hardware DPLL device when one is
+# present (or when DPLL_DEV_ID is set), otherwise against a freshly
+# created dpllsim device. DPLL_DRIVER names the driver to take the
+# device from, dpllsim for a created one although hardware is present.
+#
+# Copyright (c) 2026 Red Hat, Inc.
+
+SCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
+
+source "$SCRIPTDIR/../../net/lib.sh"
+source "$SCRIPTDIR/dpll_lib.sh"
+
+ALL_TESTS="
+	test_device_properties
+"
+
+test_device_properties()
+{
+	local json
+
+	RET=0
+
+	# A device that cannot be queried at all would be reported as one
+	# that misses every attribute, so bail out on that first.
+	if ! json=$(dpll_device_json "$DPLL_DEV_ID") || [ -z "$json" ]; then
+		check_err 1 "device query failed"
+		log_test "DPLL device properties"
+		return
+	fi
+
+	local field
+	for field in '.id' '.["module-name"]' '.["clock-id"]' '.type' \
+		     '.mode' '.["lock-status"]'; do
+		if ! jq -e "$field" <<< "$json" &> /dev/null; then
+			check_err 1 "device is missing the $field attribute"
+		fi
+	done
+
+	log_test "DPLL device properties"
+}
+
+##############################################################################
+
+cleanup()
+{
+	# A second signal must not abort the teardown half way.
+	trap '' INT TERM
+
+	if [ -n "$CREATED_DEVICE" ]; then
+		dpllsim_delete_device "$CREATED_DEVICE" ||
+			log_info "cannot delete device $CREATED_DEVICE"
+	fi
+	# The device whose creation a signal interrupted is recorded by the
+	# library alone, a double delete of a tracked one is harmless.
+	if [ -n "$DPLLSIM_CREATED_NAME" ]; then
+		dpllsim_delete_device "$DPLLSIM_CREATED_NAME" ||
+			log_info "cannot delete device $DPLLSIM_CREATED_NAME"
+	fi
+	dpll_lib_cleanup
+}
+
+dpll_lib_check_prereqs
+# Another run would find the devices of this one left behind, see the
+# lock in the library. A run against a device the user named, by
+# DPLL_DEV_ID or by a hardware driver in DPLL_DRIVER, only reads that
+# device and goes without the lock.
+if [ -z "${DPLL_DEV_ID:-}" ] &&
+   { [ -z "${DPLL_DRIVER:-}" ] || [ "$DPLL_DRIVER" = dpllsim ]; }; then
+	dpll_lib_lock
+fi
+
+CREATED_DEVICE=""
+# The module check may mount configfs, so the exit trap that unmounts it
+# again has to be in place before. INT and TERM run the cleanup as well,
+# so that a timeout imposed by the harness still removes the device, and
+# ignore any further signal, an exit inside the cleanup would end the
+# teardown half way. Their handler drops the exit trap and calls the
+# cleanup itself, a signal that comes while the script exits is handled
+# before the exit trap gets to the cleanup, and the exit of the handler
+# would end the exit trap there.
+trap cleanup EXIT
+trap 'trap "" INT TERM; trap - EXIT; cleanup; exit 1' INT TERM
+
+dpll_lib_init
+
+# With the module there, a device that cannot be created, registered or
+# found on netlink is a failure of what the test covers, not a skip.
+if [ -z "$DPLL_DEV_ID" ]; then
+	dpllsim_check_module
+	if ! dpllsim_setup_standard_device; then
+		echo "FAIL: cannot create dpllsim device"
+		exit "$ksft_fail"
+	fi
+	CREATED_DEVICE=$DPLLSIM_CREATED_NAME
+	DPLL_DEV_ID=$DPLLSIM_DEV_ID
+	DPLL_MODULE_NAME="dpllsim"
+fi
+
+log_info "testing DPLL device $DPLL_DEV_ID (module $DPLL_MODULE_NAME)"
+
+tests_run
+
+exit $EXIT_STATUS
diff --git a/tools/testing/selftests/drivers/dpll/dpll_lib.sh b/tools/testing/selftests/drivers/dpll/dpll_lib.sh
new file mode 100644
index 00000000000000..467527a4343afb
--- /dev/null
+++ b/tools/testing/selftests/drivers/dpll/dpll_lib.sh
@@ -0,0 +1,446 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# Helpers for DPLL selftests. Provides wrappers around the iproute2 dpll
+# tool and around the dpllsim configfs interface for creating virtual DPLL
+# devices. Sourcing this file only defines functions and variables. A
+# script calls dpll_lib_check_prereqs first, then dpll_lib_lock unless it
+# only reads a device the user named, and dpll_lib_init when it can run
+# against an existing device. net/lib.sh
+# must be sourced first, it provides ksft_skip, ksft_fail, busywait and
+# log_info.
+#
+# Copyright (c) 2026 Red Hat, Inc.
+#
+# shellcheck disable=SC2034,SC2154 # the globals set here are read by the
+# scripts and ksft_skip is defined by net/lib.sh
+
+CONFIGFS_DIR="/sys/kernel/config"
+DPLLSIM_CFG="$CONFIGFS_DIR/dpllsim"
+
+# Next clock-id handed out by dpllsim_alloc_clock_id. The lookups of the
+# dpllsim helpers match on the module name as well, which is what keeps
+# the tests away from the devices of other drivers whatever clock-id
+# those use. The random start only avoids collisions between runs, as
+# the devices are named after their clock-id in configfs.
+DPLLSIM_NEXT_CLOCK_ID=$((96000 + RANDOM))
+
+##############################################################################
+# Prerequisites and device selection
+
+dpll_lib_check_prereqs()
+{
+	if [ "$EUID" -ne 0 ]; then
+		echo "SKIP: must be run as root"
+		exit "$ksft_skip"
+	fi
+
+	local cmd
+	for cmd in jq dpll mountpoint; do
+		if ! command -v "$cmd" &> /dev/null; then
+			echo "SKIP: $cmd not found"
+			exit "$ksft_skip"
+		fi
+	done
+
+	# Every release of the dpll tool prints the help of an object
+	# without connecting to the kernel, main() skips the netlink init
+	# for it. A kernel without DPLL passes here and is a skip once the
+	# device dump of dpll_lib_init fails, see __dpll_lib_dump_failed.
+	if ! dpll device help 2>&1 | grep -q "show"; then
+		echo "SKIP: dpll tool too old, missing 'device show'"
+		exit "$ksft_skip"
+	fi
+}
+
+# The scripts that create dpllsim devices share the one dpllsim directory
+# and cannot run alongside each other, so each takes a lock on
+# DPLL_LOCK_FILE before it mounts or creates anything and skips when
+# another run holds it. The file lives under /run rather than in the
+# world-writable /tmp, where a planted symlink could redirect the open.
+DPLL_RUN_DIR="/run/dpll-selftest"
+DPLL_LOCK_FILE="$DPLL_RUN_DIR/lock"
+DPLL_LOCK_FD=""
+
+# Create DPLL_RUN_DIR private when it is missing and make an existing
+# one private too, whatever the umask, others that can write to it
+# could plant a symlink in place of the lock file. Skip when that fails.
+dpll_lib_run_dir()
+{
+	mkdir -m 0700 "$DPLL_RUN_DIR" 2>/dev/null
+	if ! [ -d "$DPLL_RUN_DIR" ] ||
+	   ! chmod 0700 "$DPLL_RUN_DIR" 2>/dev/null; then
+		echo "SKIP: cannot set up $DPLL_RUN_DIR"
+		exit "$ksft_skip"
+	fi
+}
+
+dpll_lib_lock()
+{
+	if ! command -v flock &> /dev/null; then
+		echo "SKIP: flock not found"
+		exit "$ksft_skip"
+	fi
+	dpll_lib_run_dir
+	# A redirection that fails makes exec return non-zero here, bash
+	# exits the shell for it only in POSIX mode.
+	if ! { exec {DPLL_LOCK_FD}> "$DPLL_LOCK_FILE"; } 2>/dev/null; then
+		echo "SKIP: cannot open $DPLL_LOCK_FILE"
+		exit "$ksft_skip"
+	fi
+	if ! flock -n "$DPLL_LOCK_FD"; then
+		echo "SKIP: another dpll selftest run is in progress"
+		exit "$ksft_skip"
+	fi
+}
+
+# Detect the DPLL device to test. DPLL_DEV_ID may be set by the user to
+# force a specific device. Otherwise DPLL_DRIVER names the driver whose
+# first device is taken, by the module name the device reports, dpllsim
+# standing for a simulated device the script creates, and a system
+# without a device of that driver is a skip. With neither set the first
+# device that is not a dpllsim one is taken, so that an instance of the
+# simulator left behind by an earlier run is not mistaken for hardware.
+# A simulator that is built in reports the module name "kernel", an
+# instance of it is taken like any other device. Sets DPLL_DEV_ID (may
+# end up empty) and DPLL_MODULE_NAME. A device dump that fails must not
+# be taken for a system without a device, see __dpll_lib_dump_failed.
+dpll_lib_init()
+{
+	local out
+
+	DPLL_MODULE_NAME=""
+
+	if [ -n "${DPLL_DEV_ID:-}" ]; then
+		# A device that is there but cannot be queried fails below,
+		# only one missing from the dump is a skip.
+		out=$(dpll device show -j 2>/dev/null) || __dpll_lib_dump_failed
+		if ! jq -e --arg id "$DPLL_DEV_ID" \
+		     'any(.device[]?; (.id | tostring) == $id)' \
+		     <<< "$out" > /dev/null; then
+			echo "SKIP: DPLL device \"$DPLL_DEV_ID\" not found"
+			exit "$ksft_skip"
+		fi
+	elif [ "${DPLL_DRIVER:-}" = "dpllsim" ]; then
+		DPLL_DEV_ID=""
+	elif [ -n "${DPLL_DRIVER:-}" ]; then
+		out=$(dpll device show -j 2>/dev/null) || __dpll_lib_dump_failed
+		DPLL_DEV_ID=$(jq -r --arg mod "$DPLL_DRIVER" '[.device[]? |
+				      select(.["module-name"] == $mod)
+				     ][0].id // empty' <<< "$out")
+		if [ -z "$DPLL_DEV_ID" ]; then
+			echo "SKIP: no DPLL device of driver \"$DPLL_DRIVER\""
+			exit "$ksft_skip"
+		fi
+	else
+		out=$(dpll device show -j 2>/dev/null) || __dpll_lib_dump_failed
+		DPLL_DEV_ID=$(jq -r '[.device[]? |
+				      select(.["module-name"] != "dpllsim")
+				     ][0].id // empty' <<< "$out")
+	fi
+
+	if [ -n "$DPLL_DEV_ID" ] &&
+	   ! DPLL_MODULE_NAME=$(dpll_device_get_module "$DPLL_DEV_ID"); then
+		echo "FAIL: cannot query DPLL device $DPLL_DEV_ID"
+		exit "$ksft_fail"
+	fi
+}
+
+# The device dump failed. The dpll tool reports a kernel without the
+# DPLL netlink family with the same message in every release, such a
+# kernel is a skip. With the family there the failed dump is a failure
+# of what the tests cover, whichever drivers provide the devices. The
+# simulator says nothing about it, other drivers select DPLL as well.
+__dpll_lib_dump_failed()
+{
+	if { dpll device show -j > /dev/null; } 2>&1 |
+	   grep -q "DPLL subsystem not available"; then
+		echo "SKIP: DPLL subsystem not available"
+		exit "$ksft_skip"
+	fi
+	echo "FAIL: cannot dump DPLL devices"
+	exit "$ksft_fail"
+}
+
+##############################################################################
+# Device queries
+#
+# "dpll device show id X -j" prints a flat object, a dump wraps its
+# objects in {"device":[...]}. The helpers normalize with
+# (.device[0]? // .) before extracting fields, so that either shape
+# works. The tool is run first and jq only on what it printed, so that a
+# failed query returns an error instead of the empty output a missing
+# field produces too.
+
+dpll_device_json()
+{
+	local dev_id=$1
+	local out
+
+	out=$(dpll device show id "$dev_id" -j 2>/dev/null) || return 1
+	jq '(.device[0]? // .)' <<< "$out"
+}
+
+__dpll_device_field()
+{
+	local dev_id=$1; shift
+	local out
+
+	out=$(dpll device show id "$dev_id" -j 2>/dev/null) || return 1
+	jq -r "(.device[0]? // .) | $1 // empty" <<< "$out"
+}
+
+dpll_device_get_module()
+{
+	__dpll_device_field "$1" '.["module-name"]'
+}
+
+# Look up the netlink device id by the clock-id chosen at create time,
+# restricted to the devices of the given module when one is named. The
+# clock-id is the only reliable handle on a device the test created, as
+# netlink ids depend on what else exists, and the module name keeps a
+# lookup for a dpllsim device from returning a device of another driver
+# that happens to use the same clock-id. The tool prints the clock-id as
+# a JSON number, which jq may round above 2^53. The clock-ids the tests
+# hand out stay far below that, so the comparison of its string form is
+# exact. Return 1 when the dump itself failed, a caller must not take
+# that for a device that is gone.
+# $1: clock-id, $2: module name (optional)
+dpll_device_get_id_by_clock_id()
+{
+	local clock_id=$1
+	local module=${2:-}
+	local out
+
+	out=$(dpll device show -j 2>/dev/null) || return 1
+	jq -r --arg cid "$clock_id" --arg mod "$module" \
+	   '[.device[]? |
+	     select((.["clock-id"] | tostring) == $cid and
+		    ($mod == "" or .["module-name"] == $mod))
+	    ][0].id // empty' <<< "$out"
+}
+
+# The two checks below fail when the lookup itself failed, so that a
+# failed dump neither counts as a device that is present nor as one
+# that is gone, the wait helpers keep polling instead.
+__dpll_device_present_by_clock_id()
+{
+	local dev_id
+
+	dev_id=$(dpll_device_get_id_by_clock_id "$@") || return 1
+	[ -n "$dev_id" ] && echo "$dev_id"
+}
+
+__dpll_device_absent_by_clock_id()
+{
+	local dev_id
+
+	dev_id=$(dpll_device_get_id_by_clock_id "$@") || return 1
+	[ -z "$dev_id" ]
+}
+
+# Wait until a device with the given clock-id (and module, when given) is
+# visible on netlink and print its netlink id. Callers must check the
+# exit status rather than the output, busywait prints the output of the
+# last failed check when it times out.
+dpll_device_wait_by_clock_id()
+{
+	busywait "$BUSYWAIT_TIMEOUT" __dpll_device_present_by_clock_id "$@"
+}
+
+dpll_device_wait_gone_by_clock_id()
+{
+	busywait "$BUSYWAIT_TIMEOUT" __dpll_device_absent_by_clock_id "$@"
+}
+
+##############################################################################
+# dpllsim configfs interface
+#
+# Each directory under $DPLLSIM_CFG is one simulated device. Writing 1 to
+# <dev>/live registers the device with the DPLL core, writing 0
+# unregisters it. The structure (clock_id) can only be changed while the
+# device is not live.
+#
+# dpllsim_available loads the dpllsim module and mounts configfs when
+# nothing else did. Scripts call dpll_lib_cleanup last from their exit
+# trap to undo the mount, the module stays loaded. The only other trace
+# the library leaves outside the dpllsim directory is DPLL_RUN_DIR with
+# the lock file.
+
+DPLLSIM_MOUNTED_CONFIGFS=0
+# Name of the device dpllsim_create_device created last. Set here, so
+# that a value inherited from the environment is never taken for a
+# device of this run by a cleanup handler.
+DPLLSIM_CREATED_NAME=""
+# The device directories in DPLLSIM_CFG when dpllsim_check_module found
+# the simulator, one per line. It stays unset until then, no script
+# creates a device before. See dpll_lib_cleanup.
+unset DPLLSIM_DEVICES_BEFORE
+
+dpllsim_available()
+{
+	[ -d "$DPLLSIM_CFG" ] && return 0
+
+	modprobe dpllsim &> /dev/null
+	if ! mountpoint -q "$CONFIGFS_DIR"; then
+		# Nothing had configfs mounted, so the cleanup unmounts it
+		# again. Something that starts to use it meanwhile without
+		# mounting it would lose it then, which is accepted, the
+		# tests are not meant to run next to such a user. The mount
+		# is recorded before it is made, so that a cleanup run by a
+		# signal right after it knows about it.
+		DPLLSIM_MOUNTED_CONFIGFS=1
+		mount -t configfs none "$CONFIGFS_DIR" &> /dev/null ||
+			DPLLSIM_MOUNTED_CONFIGFS=0
+	fi
+	[ -d "$DPLLSIM_CFG" ]
+}
+
+# Unmount configfs again when dpllsim_available mounted it. Call after
+# the devices of the script are gone. A dpllsim device the script could
+# not remove keeps the mount, so that what is left can be looked at. A
+# device that was there before the script is not one it left behind.
+dpll_lib_cleanup()
+{
+	local dev_path name
+
+	[ "$DPLLSIM_MOUNTED_CONFIGFS" -eq 1 ] || return 0
+	DPLLSIM_MOUNTED_CONFIGFS=0
+	# The mount recorded up front may have failed.
+	mountpoint -q "$CONFIGFS_DIR" || return 0
+
+	for dev_path in "$DPLLSIM_CFG"/*/; do
+		[ -d "$dev_path" ] || continue
+		# Without the record the script created no device, and one in
+		# the record was there before it.
+		[ -n "${DPLLSIM_DEVICES_BEFORE+set}" ] || break
+		grep -qxF -- "$dev_path" <<< "$DPLLSIM_DEVICES_BEFORE" &&
+			continue
+		name=$(basename "$dev_path")
+		log_info "device $name left behind, $CONFIGFS_DIR kept mounted"
+		return
+	done
+	umount "$CONFIGFS_DIR" 2>/dev/null ||
+		log_info "cannot unmount $CONFIGFS_DIR"
+}
+
+# The scripts find the devices of the simulator by the module name they
+# report, which is "kernel" for a driver that is built in, so the tests
+# need dpllsim as a module. Records the devices that are there already in
+# DPLLSIM_DEVICES_BEFORE.
+dpllsim_check_module()
+{
+	if ! dpllsim_available; then
+		echo "SKIP: dpllsim module not available"
+		exit "$ksft_skip"
+	fi
+	DPLLSIM_DEVICES_BEFORE=$(compgen -G "$DPLLSIM_CFG/*/")
+	if ! grep -q '^dpllsim ' /proc/modules 2>/dev/null; then
+		echo "SKIP: dpllsim is built in, the tests need it as a module"
+		exit "$ksft_skip"
+	fi
+}
+
+# $1: device name, $2: clock-id. Sets DPLLSIM_CREATED_NAME before the
+# directory is created, so that the cleanup handler of a script can
+# remove a device whose creation a signal interrupted before the script
+# recorded it. The name stays set once the device is complete, and when
+# the directory of a device that failed half way cannot be removed.
+dpllsim_create_device()
+{
+	local name=$1
+	local clock_id=$2
+	local dev_path="$DPLLSIM_CFG/$name"
+
+	# A directory of that name was left behind by something else and
+	# must never be recorded as a device of this run.
+	[ -e "$dev_path" ] && return 1
+
+	# Record the name before the directory exists, so that a cleanup
+	# run by a signal between the two still finds it.
+	DPLLSIM_CREATED_NAME=$name
+	if ! mkdir "$dev_path" 2>/dev/null; then
+		DPLLSIM_CREATED_NAME=""
+		return 1
+	fi
+	if ! echo "$clock_id" 2>/dev/null > "$dev_path/clock_id"; then
+		rmdir "$dev_path" 2>/dev/null && DPLLSIM_CREATED_NAME=""
+		return 1
+	fi
+}
+
+dpllsim_deploy()
+{
+	echo 1 2>/dev/null > "$DPLLSIM_CFG/$1/live"
+}
+
+dpllsim_undeploy()
+{
+	echo 0 2>/dev/null > "$DPLLSIM_CFG/$1/live"
+}
+
+# Take the device offline and remove it. Every step is attempted even
+# when an earlier one failed, the return value is non-zero when any did.
+# A device that does not exist counts as deleted.
+dpllsim_delete_device()
+{
+	local dev_path="$DPLLSIM_CFG/$1"
+	local ret=0
+
+	[ -d "$dev_path" ] || return 0
+
+	dpllsim_undeploy "$1" || ret=1
+	rmdir "$dev_path" || ret=1
+
+	return "$ret"
+}
+
+# Return 0 when a device in DPLLSIM_CFG, of this run or not, holds the
+# given clock-id or has a directory name that ends with it. The
+# simulator refuses a live instance with a clock-id another live one
+# uses, and the tests name their devices after their clock-id.
+dpllsim_clock_id_taken()
+{
+	local f
+
+	compgen -G "$DPLLSIM_CFG/*$1" > /dev/null && return 0
+	for f in "$DPLLSIM_CFG"/*/clock_id; do
+		[ -f "$f" ] || continue
+		[ "$(cat "$f" 2>/dev/null)" = "$1" ] && return 0
+	done
+	return 1
+}
+
+# Set DPLLSIM_CLOCK_ID to a clock-id unique within this test run that no
+# device in DPLLSIM_CFG takes yet, see dpllsim_clock_id_taken. Must
+# not be called from a subshell, the counter increment would be lost.
+dpllsim_alloc_clock_id()
+{
+	while dpllsim_clock_id_taken "$DPLLSIM_NEXT_CLOCK_ID"; do
+		DPLLSIM_NEXT_CLOCK_ID=$((DPLLSIM_NEXT_CLOCK_ID + 1))
+	done
+	DPLLSIM_CLOCK_ID=$((DPLLSIM_NEXT_CLOCK_ID++))
+}
+
+# Create a device, deploy it and resolve the netlink device id. Sets
+# DPLLSIM_CREATED_NAME, DPLLSIM_CLOCK_ID and DPLLSIM_DEV_ID.
+dpllsim_setup_standard_device()
+{
+	local clock_id name
+
+	dpllsim_alloc_clock_id
+	clock_id=$DPLLSIM_CLOCK_ID
+	name="std$clock_id"
+	dpllsim_create_device "$name" "$clock_id" || return 1
+
+	if ! dpllsim_deploy "$name"; then
+		dpllsim_delete_device "$name"
+		return 1
+	fi
+
+	if ! DPLLSIM_DEV_ID=$(dpll_device_wait_by_clock_id "$clock_id" \
+							  dpllsim); then
+		dpllsim_delete_device "$name"
+		return 1
+	fi
+}
diff --git a/tools/testing/selftests/drivers/dpll/dpll_stress.sh b/tools/testing/selftests/drivers/dpll/dpll_stress.sh
new file mode 100755
index 00000000000000..2c60ac9faefda4
--- /dev/null
+++ b/tools/testing/selftests/drivers/dpll/dpll_stress.sh
@@ -0,0 +1,201 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# DPLL subsystem stress test. Uses dpllsim to put the DPLL core under
+# load, the tests below say what each of them exercises.
+#
+# Copyright (c) 2026 Red Hat, Inc.
+
+SCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
+
+source "$SCRIPTDIR/../../net/lib.sh"
+source "$SCRIPTDIR/dpll_lib.sh"
+
+ALL_TESTS="
+	test_rapid_create_delete
+	test_deploy_cycle
+"
+
+STRESS_ITERATIONS=${STRESS_ITERATIONS:-20}
+
+CREATED_DEVICES=()
+
+track_device()
+{
+	CREATED_DEVICES+=("$1")
+}
+
+untrack_device()
+{
+	local name=$1 dev
+	local remaining=()
+
+	for dev in "${CREATED_DEVICES[@]}"; do
+		[ "$dev" = "$name" ] || remaining+=("$dev")
+	done
+	CREATED_DEVICES=("${remaining[@]}")
+}
+
+cleanup()
+{
+	# A second signal must not abort the teardown half way.
+	trap '' INT TERM
+
+	local dev
+
+	for dev in "${CREATED_DEVICES[@]}"; do
+		dpllsim_delete_device "$dev" ||
+			log_info "cannot delete device $dev"
+	done
+	# The device whose creation a signal interrupted is recorded by the
+	# library alone, a double delete of a tracked one is harmless.
+	if [ -n "$DPLLSIM_CREATED_NAME" ]; then
+		dpllsim_delete_device "$DPLLSIM_CREATED_NAME" ||
+			log_info "cannot delete device $DPLLSIM_CREATED_NAME"
+	fi
+	dpll_lib_cleanup
+}
+
+# Create a device named after the given clock-id and deploy it. Sets
+# DPLLSIM_CREATED_NAME. A device that fails half way and cannot be removed
+# is tracked, so that the cleanup handler tries again, the next device
+# takes DPLLSIM_CREATED_NAME over.
+create_stress_device()
+{
+	local clock_id=$1
+	local name="st$clock_id"
+
+	if ! dpllsim_create_device "$name" "$clock_id"; then
+		[ "$DPLLSIM_CREATED_NAME" = "$name" ] && track_device "$name"
+		return 1
+	fi
+
+	if ! dpllsim_deploy "$name"; then
+		dpllsim_delete_device "$name" || track_device "$name"
+		return 1
+	fi
+}
+
+##############################################################################
+
+# Log a skip of the test named $1 and return 1 unless every variable
+# named by the further arguments holds a positive number, a test of zero
+# rounds or devices would pass without testing anything.
+stress_counts_valid()
+{
+	local name=$1 var
+
+	shift
+	for var in "$@"; do
+		[[ "${!var}" =~ ^[1-9][0-9]*$ ]] && continue
+		log_test_skip "$name" "$var is not a positive number"
+		return 1
+	done
+}
+
+test_rapid_create_delete()
+{
+	local i clock_id name
+
+	RET=0
+
+	stress_counts_valid \
+	    "DPLL rapid create/delete ($STRESS_ITERATIONS iterations)" \
+	    STRESS_ITERATIONS || return
+
+	for i in $(seq 1 "$STRESS_ITERATIONS"); do
+		dpllsim_alloc_clock_id; clock_id=$DPLLSIM_CLOCK_ID
+
+		if ! create_stress_device "$clock_id"; then
+			check_err 1 "failed to create device (iteration $i)"
+			break
+		fi
+		name=$DPLLSIM_CREATED_NAME
+		track_device "$name"
+
+		if ! dpllsim_delete_device "$name"; then
+			check_err 1 "failed to delete device (iteration $i)"
+			break
+		fi
+		untrack_device "$name"
+	done
+
+	log_test "DPLL rapid create/delete ($STRESS_ITERATIONS iterations)"
+}
+
+test_deploy_cycle()
+{
+	local clock_id name i
+
+	RET=0
+
+	stress_counts_valid "DPLL deploy/undeploy cycling" \
+	    STRESS_ITERATIONS || return
+
+	dpllsim_alloc_clock_id; clock_id=$DPLLSIM_CLOCK_ID
+	name="cycle$clock_id"
+	if ! dpllsim_create_device "$name" "$clock_id"; then
+		# Kept for the cleanup handler when it failed half way and
+		# could not be removed, see create_stress_device.
+		[ "$DPLLSIM_CREATED_NAME" = "$name" ] && track_device "$name"
+		check_err 1 "cannot create device"
+		log_test "DPLL deploy/undeploy cycling"
+		return
+	fi
+	track_device "$name"
+
+	# The live write registers or unregisters the device before it
+	# returns, so a wait passes at its first look and one that times
+	# out ends the loop, the waits cannot add up to more than one
+	# timeout.
+	for i in $(seq 1 "$STRESS_ITERATIONS"); do
+		if ! dpllsim_deploy "$name"; then
+			check_err 1 "deploy failed (iteration $i)"
+			break
+		fi
+		if ! dpll_device_wait_by_clock_id "$clock_id" dpllsim \
+		     > /dev/null; then
+			check_err 1 "device not on netlink (iteration $i)"
+			break
+		fi
+		if ! dpllsim_undeploy "$name"; then
+			check_err 1 "undeploy failed (iteration $i)"
+			break
+		fi
+		if ! dpll_device_wait_gone_by_clock_id "$clock_id" dpllsim; then
+			check_err 1 "device still on netlink (iteration $i)"
+			break
+		fi
+	done
+
+	if dpllsim_delete_device "$name"; then
+		untrack_device "$name"
+	else
+		check_err 1 "cannot delete device $name"
+	fi
+
+	log_test "DPLL deploy/undeploy cycling"
+}
+
+##############################################################################
+
+dpll_lib_check_prereqs
+# Another run would find the devices of this one left behind, see the
+# lock in the library.
+dpll_lib_lock
+
+# The module check may mount configfs, so the exit trap that unmounts it
+# again has to be in place before. INT and TERM run the cleanup as well,
+# so that a timeout imposed by the harness still removes the devices, and
+# ignore any further signal, an exit inside the cleanup would end the
+# teardown half way. Their handler drops the exit trap and calls the
+# cleanup itself, a signal that comes while the script exits is handled
+# before the exit trap gets to the cleanup, and the exit of the handler
+# would end the exit trap there.
+trap cleanup EXIT
+trap 'trap "" INT TERM; trap - EXIT; cleanup; exit 1' INT TERM
+dpllsim_check_module
+
+tests_run
+
+exit $EXIT_STATUS
diff --git a/tools/testing/selftests/drivers/dpll/settings b/tools/testing/selftests/drivers/dpll/settings
new file mode 100644
index 00000000000000..a62d2fa1275c6b
--- /dev/null
+++ b/tools/testing/selftests/drivers/dpll/settings
@@ -0,0 +1 @@
+timeout=600
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH net-next v5 03/14] dpll: dpllsim: Add configurable pins
  2026-11-25  9:05 [PATCH net-next v5 00/14] dpll: add a simulated DPLL device driver Petr Oros
  2026-11-25  9:05 ` [PATCH net-next v5 01/14] dpll: dpllsim: Add " Petr Oros
  2026-11-25  9:05 ` [PATCH net-next v5 02/14] selftests: dpll: add test library and device lifecycle tests Petr Oros
@ 2026-11-25  9:05 ` Petr Oros
  2026-11-25  9:05 ` [PATCH net-next v5 04/14] selftests: dpll: add pin enumeration and property tests Petr Oros
                   ` (10 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: Petr Oros @ 2026-11-25  9:05 UTC (permalink / raw)
  To: netdev
  Cc: Petr Oros, Ivan Vecera, Jiri Pirko, Jonathan Corbet, Shuah Khan,
	Shuah Khan, Randy Dunlap, Jani Nikula, Vadim Fedorenko,
	Arkadiusz Kubalewski, Jiri Pirko, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, linux-doc,
	linux-kselftest

Pins are the unit of most DPLL netlink interaction, so let a device
instance carry a set of up to 128 of them. Each directory created under
the pins/ group of an instance is one pin with type, frequency and
direction attributes. The type takes gnss, int-oscillator, ext,
synce-eth-port or mux, the names iproute2 prints for these pin types,
the direction is input or output and the frequency must be one of the
frequencies the driver advertises as supported for the type. The pin
directory name becomes the board label of the pin, which gives userspace
a stable handle to find a pin by. The attributes are writable only while
the instance is not live and return EBUSY otherwise.

At live=1 the pins are ordered as created and each pin records its
index in that order, which is the pin index it is registered under.
Their frequencies are validated against the per type tables, a MUX pin
configured as output is rejected since a MUX pin stands for an input
selector, and all pin handles are obtained up front, so that a failure
leaves nothing half visible. The device is registered first and the
pins after it, so that every pin registration event reaches userspace
with a valid device, and a failure unwinds exactly what was registered
in reverse order. While the instance is live the pin items are held
with configfs_depend_item_unlocked(), which makes rmdir of a pin fail
with EBUSY until the instance is taken down. configfs calls make_item
and drop_item under the subsystem mutex but links and unlinks the pin
directory outside of it, so a live transition racing with a pin mkdir
or rmdir can find a pin it cannot hold and fails with ENOENT then.

The pins register with the mandatory callbacks only, direction_get and
state_on_dpll_get, plus frequency_get since the frequency is known from
the configuration. The input and output ops tables are identical here
and diverge when input pins gain their setters in the next patches. The
state is always reported as disconnected for now, the settable pin
features follow in separate patches.

Reviewed-by: Ivan Vecera <ivecera@redhat.com>
Acked-by: Jiri Pirko <jiri@nvidia.com>
Signed-off-by: Petr Oros <poros@redhat.com>
---
v5:
- name the pin types the simulator takes in the changelog, int-nco is
  not among them
- say in the documentation that the EINVAL of a failed live write does
  not name the pin
- say in the comment on deploy why the lock is taken by hand
- drop the initial reference of the pins group when the device is
  removed, with configfs_remove_default_groups() as fs/dlm does; the
  group is freed with the device and needs no release, as
  config_item_cleanup() calls one only for a type with item ops

v4:
- document the limit of 128 pins, that pins cannot be added to a live
  instance, the frequencies each pin type supports and the errors of
  the live write
- correct the comments on what the subsystem mutex covers, a live write
  racing with a pin mkdir or rmdir fails with ENOENT

v4: https://lore.kernel.org/all/20261112130359.840795-4-poros@redhat.com/
v3: https://lore.kernel.org/all/20261109093523.931201-4-poros@redhat.com/
---
 Documentation/driver-api/dpllsim.rst |  59 +++++-
 drivers/dpll/dpllsim/core.c          | 236 +++++++++++++++++++++-
 drivers/dpll/dpllsim/dpll.c          | 283 ++++++++++++++++++++++++++-
 drivers/dpll/dpllsim/dpllsim.h       |  32 +++
 4 files changed, 595 insertions(+), 15 deletions(-)

diff --git a/Documentation/driver-api/dpllsim.rst b/Documentation/driver-api/dpllsim.rst
index 5683811ccfc4c0..5b878ff51331c3 100644
--- a/Documentation/driver-api/dpllsim.rst
+++ b/Documentation/driver-api/dpllsim.rst
@@ -12,10 +12,13 @@ Configuration interface
 =======================
 
 Devices are described in configfs under ``/sys/kernel/config/dpllsim/``.
-A directory created there is a device instance. Attributes are written
-while the instance is not live, then ``live`` is set to 1 to register
-the device with the DPLL subsystem. Writing 0 unregisters it again.
-Structural attributes return ``EBUSY`` while the instance is live.
+A directory created there is a device instance, a directory created in
+its ``pins/`` subdirectory is a pin. Attributes are written while the
+instance is not live, then ``live`` is set to 1 to register the device
+and its pins with the DPLL subsystem. Writing 0 unregisters everything
+again. Structural attributes return ``EBUSY`` while the instance is live
+and pins of a live instance can neither be added nor removed. An
+instance holds up to 128 pins, creating one more fails with ``ENOSPC``.
 
 Device directory ``/sys/kernel/config/dpllsim/<dev>/``:
 
@@ -26,17 +29,59 @@ Device directory ``/sys/kernel/config/dpllsim/<dev>/``:
 ``live``                 1 registers the device, 0 unregisters it
 =======================  =====================================================
 
+Pin directory ``/sys/kernel/config/dpllsim/<dev>/pins/<pin>/``:
+
+=============  ===============================================================
+``type``       ``gnss``, ``int-oscillator``, ``ext``, ``synce-eth-port`` or
+               ``mux`` (default ``ext``)
+``frequency``  Frequency in Hz, must be supported by the pin type
+               (default 0, which is rejected)
+``direction``  ``input`` or ``output`` (default ``input``), a ``mux`` pin
+               must be an input
+=============  ===============================================================
+
+The pin directory name becomes the board label of the pin. Pins are
+registered in the order the directories were created.
+
+The frequencies a pin type supports:
+
+==================  ==========================================================
+``gnss``            1 Hz
+``int-oscillator``  25 MHz
+``synce-eth-port``  1.544 MHz, 2.048 MHz, 10 MHz and 25 MHz
+``mux``             same as ``synce-eth-port``
+``ext``             1 Hz, 10 MHz, 19.44 MHz, 25 MHz, 77.76 MHz, 125 MHz and
+                    156.25 MHz
+==================  ==========================================================
+
+The pin configuration is checked when 1 is written to ``live``. The
+write fails with ``EINVAL`` when a pin has a frequency its type does not
+support or when a ``mux`` pin is an output, and with ``ENOENT`` when a
+pin directory is being created or removed at the same time. An
+``EINVAL`` does not name the pin, the attributes of the pins show which
+one it is.
+
 Example
 =======
 
-Create a device and make it live::
+Create a device with a GNSS input, an external input and an external
+output, then make it live::
 
     cd /sys/kernel/config/dpllsim
     mkdir dev0 && cd dev0
     echo 100 > clock_id
+    mkdir pins/gnss0
+    echo gnss > pins/gnss0/type
+    echo 1 > pins/gnss0/frequency
+    mkdir pins/ext0
+    echo 10000000 > pins/ext0/frequency
+    mkdir pins/out0
+    echo 10000000 > pins/out0/frequency
+    echo output > pins/out0/direction
     echo 1 > live
 
     dpll device show
+    dpll pin show
 
-To remove the device, write 0 to ``live`` and remove the device
-directory.
+To remove the device, write 0 to ``live``, remove the pin directories
+and finally the device directory.
diff --git a/drivers/dpll/dpllsim/core.c b/drivers/dpll/dpllsim/core.c
index 779eea5f5b140c..3d0fba45323b6a 100644
--- a/drivers/dpll/dpllsim/core.c
+++ b/drivers/dpll/dpllsim/core.c
@@ -6,12 +6,14 @@
 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
+#include <linux/array_size.h>
 #include <linux/cleanup.h>
 #include <linux/configfs.h>
 #include <linux/kstrtox.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/slab.h>
+#include <linux/string.h>
 #include <linux/sysfs.h>
 
 #include "dpllsim.h"
@@ -23,6 +25,189 @@ static struct dpllsim_dev *to_dpllsim_dev(struct config_item *item)
 	return container_of(to_config_group(item), struct dpllsim_dev, group);
 }
 
+static struct dpllsim_pin *to_dpllsim_pin(struct config_item *item)
+{
+	return container_of(item, struct dpllsim_pin, item);
+}
+
+static const struct {
+	const char *name;
+	enum dpll_pin_type type;
+} dpllsim_pin_types[] = {
+	{ "gnss", DPLL_PIN_TYPE_GNSS },
+	{ "int-oscillator", DPLL_PIN_TYPE_INT_OSCILLATOR },
+	{ "ext", DPLL_PIN_TYPE_EXT },
+	{ "synce-eth-port", DPLL_PIN_TYPE_SYNCE_ETH_PORT },
+	{ "mux", DPLL_PIN_TYPE_MUX },
+};
+
+static ssize_t dpllsim_pin_type_show(struct config_item *item, char *page)
+{
+	struct dpllsim_pin *pin = to_dpllsim_pin(item);
+	int i;
+
+	guard(mutex)(&pin->dev->lock);
+	for (i = 0; i < ARRAY_SIZE(dpllsim_pin_types); i++)
+		if (dpllsim_pin_types[i].type == pin->type)
+			return sysfs_emit(page, "%s\n",
+					  dpllsim_pin_types[i].name);
+	return -EINVAL;
+}
+
+static ssize_t dpllsim_pin_type_store(struct config_item *item,
+				      const char *page, size_t count)
+{
+	struct dpllsim_pin *pin = to_dpllsim_pin(item);
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(dpllsim_pin_types); i++)
+		if (sysfs_streq(page, dpllsim_pin_types[i].name))
+			break;
+	if (i == ARRAY_SIZE(dpllsim_pin_types))
+		return -EINVAL;
+
+	guard(mutex)(&pin->dev->lock);
+	if (dpllsim_dev_frozen(pin->dev))
+		return -EBUSY;
+	pin->type = dpllsim_pin_types[i].type;
+	return count;
+}
+CONFIGFS_ATTR(dpllsim_pin_, type);
+
+static ssize_t dpllsim_pin_direction_show(struct config_item *item, char *page)
+{
+	struct dpllsim_pin *pin = to_dpllsim_pin(item);
+
+	guard(mutex)(&pin->dev->lock);
+	return sysfs_emit(page, "%s\n",
+			  pin->direction == DPLL_PIN_DIRECTION_INPUT ?
+			  "input" : "output");
+}
+
+static ssize_t dpllsim_pin_direction_store(struct config_item *item,
+					   const char *page, size_t count)
+{
+	struct dpllsim_pin *pin = to_dpllsim_pin(item);
+	enum dpll_pin_direction direction;
+
+	if (sysfs_streq(page, "input"))
+		direction = DPLL_PIN_DIRECTION_INPUT;
+	else if (sysfs_streq(page, "output"))
+		direction = DPLL_PIN_DIRECTION_OUTPUT;
+	else
+		return -EINVAL;
+
+	guard(mutex)(&pin->dev->lock);
+	if (dpllsim_dev_frozen(pin->dev))
+		return -EBUSY;
+	pin->direction = direction;
+	return count;
+}
+CONFIGFS_ATTR(dpllsim_pin_, direction);
+
+static ssize_t dpllsim_pin_frequency_show(struct config_item *item, char *page)
+{
+	struct dpllsim_pin *pin = to_dpllsim_pin(item);
+
+	guard(mutex)(&pin->dev->lock);
+	return sysfs_emit(page, "%llu\n", pin->frequency);
+}
+
+static ssize_t dpllsim_pin_frequency_store(struct config_item *item,
+					   const char *page, size_t count)
+{
+	struct dpllsim_pin *pin = to_dpllsim_pin(item);
+	u64 val;
+	int err;
+
+	err = kstrtou64(page, 10, &val);
+	if (err)
+		return err;
+
+	guard(mutex)(&pin->dev->lock);
+	if (dpllsim_dev_frozen(pin->dev))
+		return -EBUSY;
+	pin->frequency = val;
+	return count;
+}
+CONFIGFS_ATTR(dpllsim_pin_, frequency);
+
+static struct configfs_attribute *dpllsim_pin_attrs[] = {
+	&dpllsim_pin_attr_type,
+	&dpllsim_pin_attr_direction,
+	&dpllsim_pin_attr_frequency,
+	NULL,
+};
+
+static void dpllsim_pin_release(struct config_item *item)
+{
+	struct dpllsim_pin *pin = to_dpllsim_pin(item);
+
+	kfree(pin);
+}
+
+static const struct configfs_item_operations dpllsim_pin_item_ops = {
+	.release	= dpllsim_pin_release,
+};
+
+static const struct config_item_type dpllsim_pin_type = {
+	.ct_item_ops	= &dpllsim_pin_item_ops,
+	.ct_attrs	= dpllsim_pin_attrs,
+	.ct_owner	= THIS_MODULE,
+};
+
+static struct config_item *dpllsim_pins_make_item(struct config_group *group,
+						  const char *name)
+{
+	struct dpllsim_dev *sim = container_of(group, struct dpllsim_dev,
+					       pins_group);
+	struct dpllsim_pin *pin;
+
+	lockdep_assert_held(&dpllsim_subsys.su_mutex);
+
+	if (dpllsim_dev_frozen(sim))
+		return ERR_PTR(-EBUSY);
+	if (list_count_nodes(&sim->pin_list) >= DPLLSIM_MAX_PINS)
+		return ERR_PTR(-ENOSPC);
+
+	pin = kzalloc_obj(*pin);
+	if (!pin)
+		return ERR_PTR(-ENOMEM);
+
+	config_item_init_type_name(&pin->item, name, &dpllsim_pin_type);
+	pin->dev = sim;
+	pin->type = DPLL_PIN_TYPE_EXT;
+	pin->direction = DPLL_PIN_DIRECTION_INPUT;
+	list_add_tail(&pin->list, &sim->pin_list);
+
+	return &pin->item;
+}
+
+/*
+ * make_item and drop_item run under the subsystem mutex like the live
+ * transitions, which keeps pin_list consistent. configfs links the
+ * directory of a new pin only after make_item returned and unlinks the
+ * one of a removed pin before it calls drop_item, both without the
+ * mutex. A live transition in between finds a pin on the list that
+ * configfs_depend_item_unlocked() does not know and fails with -ENOENT.
+ */
+static void dpllsim_pins_drop_item(struct config_group *group,
+				   struct config_item *item)
+{
+	list_del(&to_dpllsim_pin(item)->list);
+	config_item_put(item);
+}
+
+static const struct configfs_group_operations dpllsim_pins_group_ops = {
+	.make_item	= dpllsim_pins_make_item,
+	.drop_item	= dpllsim_pins_drop_item,
+};
+
+static const struct config_item_type dpllsim_pins_type = {
+	.ct_group_ops	= &dpllsim_pins_group_ops,
+	.ct_owner	= THIS_MODULE,
+};
+
 /* The clock id is checked at live=1, so both share the subsystem mutex */
 static ssize_t dpllsim_dev_clock_id_show(struct config_item *item, char *page)
 {
@@ -82,9 +267,41 @@ static bool dpllsim_clock_id_in_use(struct dpllsim_dev *sim)
 	return false;
 }
 
+/* Pins of a live device cannot be removed */
+static void dpllsim_dev_undepend_pins(struct dpllsim_dev *sim,
+				      struct dpllsim_pin *last)
+{
+	struct dpllsim_pin *pin;
+
+	list_for_each_entry(pin, &sim->pin_list, list) {
+		if (pin == last)
+			break;
+		configfs_undepend_item_unlocked(&pin->item);
+	}
+}
+
+static int dpllsim_dev_depend_pins(struct dpllsim_dev *sim)
+{
+	struct dpllsim_pin *pin;
+	int err;
+
+	list_for_each_entry(pin, &sim->pin_list, list) {
+		err = configfs_depend_item_unlocked(&dpllsim_subsys,
+						    &pin->item);
+		if (err) {
+			dpllsim_dev_undepend_pins(sim, pin);
+			return err;
+		}
+	}
+
+	return 0;
+}
+
 /*
- * The subsystem mutex excludes the live transitions of the other
- * instances for the whole deploy sequence.
+ * The subsystem mutex keeps the make_item and drop_item callbacks of the
+ * pins and the live transitions of the other instances out for the whole
+ * deploy sequence, see dpllsim_pins_drop_item() for a pin mkdir or rmdir
+ * that is in flight around its callback.
  */
 static ssize_t dpllsim_dev_live_store(struct config_item *item,
 				      const char *page, size_t count)
@@ -104,6 +321,7 @@ static ssize_t dpllsim_dev_live_store(struct config_item *item,
 
 	if (!live) {
 		dpllsim_dev_undeploy(sim);
+		dpllsim_dev_undepend_pins(sim, NULL);
 		return count;
 	}
 
@@ -112,10 +330,16 @@ static ssize_t dpllsim_dev_live_store(struct config_item *item,
 	if (dpllsim_clock_id_in_use(sim))
 		return -EEXIST;
 
-	err = dpllsim_dev_deploy(sim);
+	err = dpllsim_dev_depend_pins(sim);
 	if (err)
 		return err;
 
+	err = dpllsim_dev_deploy(sim);
+	if (err) {
+		dpllsim_dev_undepend_pins(sim, NULL);
+		return err;
+	}
+
 	return count;
 }
 CONFIGFS_ATTR(dpllsim_dev_, live);
@@ -154,12 +378,16 @@ static struct config_group *dpllsim_make_group(struct config_group *group,
 		return ERR_PTR(-ENOMEM);
 
 	mutex_init(&sim->lock);
+	INIT_LIST_HEAD(&sim->pin_list);
 	sim->mode = DPLL_MODE_AUTOMATIC;
 	sim->lock_status = DPLL_LOCK_STATUS_UNLOCKED;
 	/* uAPI value for no error, reported as lock-status-error none */
 	sim->lock_status_error = DPLL_LOCK_STATUS_ERROR_NONE;
 
 	config_group_init_type_name(&sim->group, name, &dpllsim_dev_type);
+	config_group_init_type_name(&sim->pins_group, "pins",
+				    &dpllsim_pins_type);
+	configfs_add_default_group(&sim->pins_group, &sim->group);
 
 	return &sim->group;
 }
@@ -172,6 +400,8 @@ static void dpllsim_drop_item(struct config_group *group,
 
 	if (dpllsim_dev_live(sim))
 		dpllsim_dev_undeploy(sim);
+	/* Drops the initial reference of the pins group, embedded in sim */
+	configfs_remove_default_groups(&sim->group);
 	config_item_put(item);
 }
 
diff --git a/drivers/dpll/dpllsim/dpll.c b/drivers/dpll/dpllsim/dpll.c
index c127269fe9a232..7323747ccf2a62 100644
--- a/drivers/dpll/dpllsim/dpll.c
+++ b/drivers/dpll/dpllsim/dpll.c
@@ -4,12 +4,39 @@
  * Copyright (c) 2026 Red Hat, Inc.
  */
 
+#include <linux/array_size.h>
 #include <linux/cleanup.h>
 #include <linux/dpll.h>
 #include <linux/module.h>
+#include <linux/slab.h>
 
 #include "dpllsim.h"
 
+static struct dpll_pin_frequency dpllsim_freq_1pps[] = {
+	DPLL_PIN_FREQUENCY_RANGE(1, 1),
+};
+
+static struct dpll_pin_frequency dpllsim_freq_synce[] = {
+	DPLL_PIN_FREQUENCY_RANGE(1544000, 1544000),
+	DPLL_PIN_FREQUENCY_RANGE(2048000, 2048000),
+	DPLL_PIN_FREQUENCY_RANGE(10000000, 10000000),
+	DPLL_PIN_FREQUENCY_RANGE(25000000, 25000000),
+};
+
+static struct dpll_pin_frequency dpllsim_freq_ext[] = {
+	DPLL_PIN_FREQUENCY_RANGE(1, 1),
+	DPLL_PIN_FREQUENCY_RANGE(10000000, 10000000),
+	DPLL_PIN_FREQUENCY_RANGE(19440000, 19440000),
+	DPLL_PIN_FREQUENCY_RANGE(25000000, 25000000),
+	DPLL_PIN_FREQUENCY_RANGE(77760000, 77760000),
+	DPLL_PIN_FREQUENCY_RANGE(125000000, 125000000),
+	DPLL_PIN_FREQUENCY_RANGE(156250000, 156250000),
+};
+
+static struct dpll_pin_frequency dpllsim_freq_int_osc[] = {
+	DPLL_PIN_FREQUENCY_RANGE(25000000, 25000000),
+};
+
 static int dpllsim_mode_get(const struct dpll_device *dpll, void *priv,
 			    enum dpll_mode *mode,
 			    struct netlink_ext_ack *extack)
@@ -39,10 +66,230 @@ static const struct dpll_device_ops dpllsim_dpll_ops = {
 	.lock_status_get = dpllsim_lock_status_get,
 };
 
+static int dpllsim_pin_frequency_get(const struct dpll_pin *pin,
+				     void *pin_priv,
+				     const struct dpll_device *dpll,
+				     void *dpll_priv, u64 *frequency,
+				     struct netlink_ext_ack *extack)
+{
+	struct dpllsim_pin *sim_pin = pin_priv;
+
+	guard(mutex)(&sim_pin->dev->lock);
+	*frequency = sim_pin->frequency;
+	return 0;
+}
+
+static int dpllsim_pin_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)
+{
+	struct dpllsim_pin *sim_pin = pin_priv;
+
+	*direction = sim_pin->direction;
+	return 0;
+}
+
+static int dpllsim_pin_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)
+{
+	struct dpllsim_pin *sim_pin = pin_priv;
+
+	guard(mutex)(&sim_pin->dev->lock);
+	*state = sim_pin->state;
+	return 0;
+}
+
+static const struct dpll_pin_ops dpllsim_input_pin_ops = {
+	.frequency_get = dpllsim_pin_frequency_get,
+	.direction_get = dpllsim_pin_direction_get,
+	.state_on_dpll_get = dpllsim_pin_state_get,
+};
+
+static const struct dpll_pin_ops dpllsim_output_pin_ops = {
+	.frequency_get = dpllsim_pin_frequency_get,
+	.direction_get = dpllsim_pin_direction_get,
+	.state_on_dpll_get = dpllsim_pin_state_get,
+};
+
+static const struct dpll_pin_ops *
+dpllsim_pin_select_ops(const struct dpllsim_pin *pin)
+{
+	if (pin->direction == DPLL_PIN_DIRECTION_INPUT)
+		return &dpllsim_input_pin_ops;
+	return &dpllsim_output_pin_ops;
+}
+
+static void dpllsim_pin_props(struct dpllsim_pin *pin,
+			      struct dpll_pin_properties *prop)
+{
+	prop->type = pin->type;
+	prop->board_label = config_item_name(&pin->item);
+
+	switch (pin->type) {
+	case DPLL_PIN_TYPE_GNSS:
+		prop->freq_supported = dpllsim_freq_1pps;
+		prop->freq_supported_num = ARRAY_SIZE(dpllsim_freq_1pps);
+		break;
+	case DPLL_PIN_TYPE_INT_OSCILLATOR:
+		prop->freq_supported = dpllsim_freq_int_osc;
+		prop->freq_supported_num = ARRAY_SIZE(dpllsim_freq_int_osc);
+		break;
+	case DPLL_PIN_TYPE_MUX:
+	case DPLL_PIN_TYPE_SYNCE_ETH_PORT:
+		prop->freq_supported = dpllsim_freq_synce;
+		prop->freq_supported_num = ARRAY_SIZE(dpllsim_freq_synce);
+		break;
+	default:
+		prop->freq_supported = dpllsim_freq_ext;
+		prop->freq_supported_num = ARRAY_SIZE(dpllsim_freq_ext);
+		break;
+	}
+}
+
+static bool dpllsim_pin_frequency_valid(struct dpllsim_pin *pin)
+{
+	struct dpll_pin_properties prop = {};
+	int i;
+
+	dpllsim_pin_props(pin, &prop);
+	for (i = 0; i < prop.freq_supported_num; i++)
+		if (pin->frequency >= prop.freq_supported[i].min &&
+		    pin->frequency <= prop.freq_supported[i].max)
+			return true;
+
+	return false;
+}
+
+/* A MUX pin stands for an input selector, so it is an input itself */
+static bool dpllsim_pin_direction_valid(const struct dpllsim_pin *pin)
+{
+	return pin->type != DPLL_PIN_TYPE_MUX ||
+	       pin->direction == DPLL_PIN_DIRECTION_INPUT;
+}
+
+static void dpllsim_pin_reset(struct dpllsim_pin *pin)
+{
+	pin->ops = dpllsim_pin_select_ops(pin);
+	pin->state = DPLL_PIN_STATE_DISCONNECTED;
+}
+
+/* Build sim->pins in registration order, the order the pins were created */
+static int dpllsim_order_pins(struct dpllsim_dev *sim)
+{
+	struct dpllsim_pin *pin;
+	int count = 0, i;
+
+	list_for_each_entry(pin, &sim->pin_list, list) {
+		if (!dpllsim_pin_frequency_valid(pin) ||
+		    !dpllsim_pin_direction_valid(pin))
+			return -EINVAL;
+		count++;
+	}
+
+	sim->pins = kzalloc_objs(*sim->pins, count);
+	if (!sim->pins)
+		return -ENOMEM;
+
+	sim->num_pins = 0;
+	list_for_each_entry(pin, &sim->pin_list, list) {
+		pin->idx = sim->num_pins;
+		sim->pins[sim->num_pins++] = pin;
+	}
+
+	for (i = 0; i < sim->num_pins; i++)
+		dpllsim_pin_reset(sim->pins[i]);
+
+	return 0;
+}
+
+static void dpllsim_put_pins(struct dpllsim_dev *sim)
+{
+	int i;
+
+	for (i = sim->num_pins - 1; i >= 0; i--) {
+		struct dpllsim_pin *pin = sim->pins[i];
+
+		if (pin->dpll_pin) {
+			dpll_pin_put(pin->dpll_pin, &pin->tracker);
+			pin->dpll_pin = NULL;
+		}
+	}
+}
+
+static int dpllsim_get_pins(struct dpllsim_dev *sim)
+{
+	int i, err;
+
+	for (i = 0; i < sim->num_pins; i++) {
+		struct dpllsim_pin *pin = sim->pins[i];
+		struct dpll_pin_properties prop = {};
+		struct dpll_pin *dpll_pin;
+
+		dpllsim_pin_props(pin, &prop);
+		dpll_pin = dpll_pin_get(sim->clock_id, pin->idx, THIS_MODULE,
+					&prop, &pin->tracker);
+		if (IS_ERR(dpll_pin)) {
+			err = PTR_ERR(dpll_pin);
+			goto err_put_pins;
+		}
+		pin->dpll_pin = dpll_pin;
+	}
+
+	return 0;
+
+err_put_pins:
+	dpllsim_put_pins(sim);
+	return err;
+}
+
+static void dpllsim_unregister_pins(struct dpllsim_dev *sim)
+{
+	int i;
+
+	for (i = sim->num_pins - 1; i >= 0; i--) {
+		struct dpllsim_pin *pin = sim->pins[i];
+
+		if (pin->registered)
+			dpll_pin_unregister(sim->dpll, pin->dpll_pin, pin->ops,
+					    pin);
+		pin->registered = false;
+	}
+}
+
+static int dpllsim_register_pins(struct dpllsim_dev *sim)
+{
+	struct dpllsim_pin *pin;
+	int i, err;
+
+	for (i = 0; i < sim->num_pins; i++) {
+		pin = sim->pins[i];
+
+		err = dpll_pin_register(sim->dpll, pin->dpll_pin, pin->ops,
+					pin);
+		if (err)
+			goto err_unregister;
+		pin->registered = true;
+	}
+
+	return 0;
+
+err_unregister:
+	dpllsim_unregister_pins(sim);
+	return err;
+}
+
 /*
- * The unwind of a failed deploy uses goto, which include/linux/cleanup.h
- * says not to mix with the scoped lock helpers, so the lock is taken by
- * hand here.
+ * Registration order matters: the device must be registered before pins
+ * so that pin registration events reach userspace with a valid device,
+ * while pin handles must be obtained up front so that a failure leaves
+ * nothing half-visible. The unwind uses goto, which
+ * include/linux/cleanup.h says not to mix with the scoped lock helpers,
+ * so the lock is taken by hand here.
  */
 int dpllsim_dev_deploy(struct dpllsim_dev *sim)
 {
@@ -53,15 +300,27 @@ int dpllsim_dev_deploy(struct dpllsim_dev *sim)
 	if (IS_ERR(dpll))
 		return PTR_ERR(dpll);
 
-	/* From here on the configuration is frozen */
+	/* From here on the pin configuration is frozen */
 	mutex_lock(&sim->lock);
 	sim->dpll = dpll;
 	mutex_unlock(&sim->lock);
 
+	err = dpllsim_order_pins(sim);
+	if (err)
+		goto err_put_dpll;
+
+	err = dpllsim_get_pins(sim);
+	if (err)
+		goto err_free_pins;
+
 	err = dpll_device_register(sim->dpll, DPLL_TYPE_PPS,
 				   &dpllsim_dpll_ops, sim);
 	if (err)
-		goto err_put_dpll;
+		goto err_put_pins;
+
+	err = dpllsim_register_pins(sim);
+	if (err)
+		goto err_unregister_device;
 
 	mutex_lock(&sim->lock);
 	sim->registered = true;
@@ -69,6 +328,14 @@ int dpllsim_dev_deploy(struct dpllsim_dev *sim)
 
 	return 0;
 
+err_unregister_device:
+	dpll_device_unregister(sim->dpll, &dpllsim_dpll_ops, sim);
+err_put_pins:
+	dpllsim_put_pins(sim);
+err_free_pins:
+	kfree(sim->pins);
+	sim->pins = NULL;
+	sim->num_pins = 0;
 err_put_dpll:
 	dpll_device_put(sim->dpll, &sim->tracker);
 	mutex_lock(&sim->lock);
@@ -82,8 +349,14 @@ void dpllsim_dev_undeploy(struct dpllsim_dev *sim)
 	scoped_guard(mutex, &sim->lock)
 		sim->registered = false;
 
+	dpllsim_unregister_pins(sim);
 	dpll_device_unregister(sim->dpll, &dpllsim_dpll_ops, sim);
+	dpllsim_put_pins(sim);
 	dpll_device_put(sim->dpll, &sim->tracker);
 	scoped_guard(mutex, &sim->lock)
 		sim->dpll = NULL;
+
+	kfree(sim->pins);
+	sim->pins = NULL;
+	sim->num_pins = 0;
 }
diff --git a/drivers/dpll/dpllsim/dpllsim.h b/drivers/dpll/dpllsim/dpllsim.h
index 0b81bb2f769bee..af33afc70cb2ee 100644
--- a/drivers/dpll/dpllsim/dpllsim.h
+++ b/drivers/dpll/dpllsim/dpllsim.h
@@ -9,10 +9,39 @@
 
 #include <linux/configfs.h>
 #include <linux/dpll.h>
+#include <linux/list.h>
 #include <linux/mutex.h>
 
+#define DPLLSIM_MAX_PINS			128
+
+struct dpllsim_dev;
+
+struct dpllsim_pin {
+	struct config_item item;
+	struct list_head list;
+	struct dpllsim_dev *dev;
+
+	/* Configuration, writable only until dpllsim_dev_frozen() */
+	enum dpll_pin_type type;
+	enum dpll_pin_direction direction;
+	u64 frequency;
+
+	/* Resolved at live=1 */
+	int idx;
+	struct dpll_pin *dpll_pin;
+	const struct dpll_pin_ops *ops;
+	dpll_tracker tracker;
+	bool registered;
+
+	/* Simulated state, guarded by dev->lock */
+	enum dpll_pin_state state;
+};
+
 struct dpllsim_dev {
 	struct config_group group;
+	struct config_group pins_group;
+	/* Pin items, guarded by the configfs subsystem mutex */
+	struct list_head pin_list;
 
 	/*
 	 * Set from the start of a live transition until the device is
@@ -27,6 +56,9 @@ struct dpllsim_dev {
 	 * the live attribute reports, see dpllsim_dev_live().
 	 */
 	bool registered;
+	/* Pins in registration order, valid while @dpll is set */
+	struct dpllsim_pin **pins;
+	int num_pins;
 
 	/* Guarded by the subsystem mutex */
 	u64 clock_id;
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH net-next v5 04/14] selftests: dpll: add pin enumeration and property tests
  2026-11-25  9:05 [PATCH net-next v5 00/14] dpll: add a simulated DPLL device driver Petr Oros
                   ` (2 preceding siblings ...)
  2026-11-25  9:05 ` [PATCH net-next v5 03/14] dpll: dpllsim: Add configurable pins Petr Oros
@ 2026-11-25  9:05 ` Petr Oros
  2026-11-25  9:05 ` [PATCH net-next v5 05/14] dpll: dpllsim: Add support to set input pin state Petr Oros
                   ` (9 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: Petr Oros @ 2026-11-25  9:05 UTC (permalink / raw)
  To: netdev
  Cc: Petr Oros, Ivan Vecera, Jiri Pirko, Jonathan Corbet, Shuah Khan,
	Shuah Khan, Randy Dunlap, Jani Nikula, Vadim Fedorenko,
	Arkadiusz Kubalewski, Jiri Pirko, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, linux-doc,
	linux-kselftest

Extend the library with pin queries. Pins of a device are collected from
the full pin dump with jq, since the device filter of "pin show" is
spelled differently before and after iproute2 7.1, and the fields that
live in the parent-device nest are read through a helper that selects
the nest of the device under test. The helpers that work on the full
dump normalize its shape the same way the id-based ones do, so that a
tool printing a flat object instead of the dump wrapper works with all
of them. Like the device queries, every pin query runs the tool first
and jq on what it printed, so that a query which fails returns an error
instead of the empty output a missing attribute produces too. The
prerequisite check now also requires a tool that knows "pin show", so
that a tool too old for the pin queries is a skip up front rather than a
failed query in every test. The dpllsim helpers gain dpllsim_add_pin,
which creates a pin directory and writes its type, frequency and
direction, and the standard device grows a GNSS input, an external input
and an external output.

The helpers that print a list of pin ids are read through
dpll_pin_ids_read, which keeps their status where a process
substitution or a pipeline would drop it, so that a pin dump that fails
is reported as a failure rather than as a device without pins.

dpll_basic.sh checks that the enumeration yields a numeric id for every
pin of the device, the ids are taken from the dump as they are and jq
prints null for a pin without one, that every pin has an id and a type
and carries the direction in the parent-device nest, and that an input
pin reports a valid state. The property test does not go on to query
pins by an id that is not one, and reports a pin it cannot query at all
as one failure rather than as one per attribute. The frequency is not
required, a pin without a frequency callback does not report one. The
device the script created has the standard pins, so an empty pin list or
a missing input pin on it means a failed query or broken tooling rather
than a pinless device. The library gains dpll_log_test_skip_or_fail for
such conditions, which fails on the device the script created, recorded
in DPLL_CREATED_DEVICE next to DPLL_DEV_ID, and skips on a device the
user provided. The stress script creates its devices with a random set
of pins of every type and both directions, MUX pins always as inputs,
and the deploy cycle test adds the standard pin set through the helper
the standard device uses.

Reviewed-by: Ivan Vecera <ivecera@redhat.com>
Acked-by: Jiri Pirko <jiri@nvidia.com>
Signed-off-by: Petr Oros <poros@redhat.com>
---
v5:
- say in the comment and in the changelog that a failed query returns
  an error, it is not the status of the tool
- correct what both say about the device filter of pin show, it is
  spelled device up to iproute2 7.0 and parent-device from 7.1 on
- add dpllsim_add_standard_pins, the standard device and the deploy
  cycle test carried a copy of the layout each
- say in the comment on the pin queries that the getters of the
  parent-device nest take the device id first
- say in the comment on the pin state check that a pin registered on
  another pin is not among the pins found, it has no parent-device nest
- track a stress device whose pins cannot be added and that cannot be
  removed, see patch 2
- loop over the further pins of a stress device without seq
- name in the comments the check of dpll_cmd_pin_get_one() that gives a
  pin registered on another pin parent-pin nests and no parent-device
  nest, although dpll_pin_on_pin_register() adds it to the devices of
  its parent

v4:
- read the pin lists through dpll_pin_ids_read, which keeps the status
  of the dump, so that a failed dump is a failure and not a device
  without pins
- correct the comment on pins without a parent-device nest

v3:
- follow the move of the selftests to
  tools/testing/selftests/drivers/dpll

v2:
- quote the ksft_skip and ret expansions (netdev CI shellcheck)

v4: https://lore.kernel.org/all/20261112130359.840795-5-poros@redhat.com/
v3: https://lore.kernel.org/all/20261109093523.931201-5-poros@redhat.com/
v2: https://lore.kernel.org/all/20261105151852.1844305-5-poros@redhat.com/
v1: https://lore.kernel.org/all/20261103093910.596225-5-poros@redhat.com/
---
 .../selftests/drivers/dpll/dpll_basic.sh      | 169 +++++++++++++-
 .../selftests/drivers/dpll/dpll_lib.sh        | 212 +++++++++++++++++-
 .../selftests/drivers/dpll/dpll_stress.sh     |  48 +++-
 3 files changed, 402 insertions(+), 27 deletions(-)

diff --git a/tools/testing/selftests/drivers/dpll/dpll_basic.sh b/tools/testing/selftests/drivers/dpll/dpll_basic.sh
index a713f3651a603a..c503d8eab283fc 100755
--- a/tools/testing/selftests/drivers/dpll/dpll_basic.sh
+++ b/tools/testing/selftests/drivers/dpll/dpll_basic.sh
@@ -1,11 +1,12 @@
 #!/bin/bash
 # SPDX-License-Identifier: GPL-2.0
 #
-# Basic DPLL subsystem test. Read-only validation of the device properties
-# exposed over netlink. Runs against a hardware DPLL device when one is
-# present (or when DPLL_DEV_ID is set), otherwise against a freshly
-# created dpllsim device. DPLL_DRIVER names the driver to take the
-# device from, dpllsim for a created one although hardware is present.
+# Basic DPLL subsystem test. Read-only validation of device and pin
+# enumeration and of the properties exposed over netlink. Runs against a
+# hardware DPLL device when one is present (or when DPLL_DEV_ID is set),
+# otherwise against a freshly created dpllsim device. DPLL_DRIVER names
+# the driver to take the device from, dpllsim for a created one although
+# hardware is present.
 #
 # Copyright (c) 2026 Red Hat, Inc.
 
@@ -16,8 +17,29 @@ source "$SCRIPTDIR/dpll_lib.sh"
 
 ALL_TESTS="
 	test_device_properties
+	test_pin_enumeration
+	test_pin_properties
+	test_pin_state
 "
 
+# Fail on every entry of a pin id list that is not a number. The ids are
+# taken from the dump as they are, so a pin without one shows up as the
+# "null" jq prints for a missing field, not as an empty line. Return 1
+# when any entry is invalid.
+check_pin_ids()
+{
+	local id ret=0
+
+	for id in "$@"; do
+		if ! [[ "$id" =~ ^[0-9]+$ ]]; then
+			check_err 1 "invalid pin id '$id'"
+			ret=1
+		fi
+	done
+
+	return "$ret"
+}
+
 test_device_properties()
 {
 	local json
@@ -43,6 +65,134 @@ test_device_properties()
 	log_test "DPLL device properties"
 }
 
+test_pin_enumeration()
+{
+	local pin_ids
+
+	RET=0
+
+	# A dump that failed is not a device without pins.
+	if ! dpll_pin_ids_read pin_ids dpll_pin_get_ids "$DPLL_DEV_ID"; then
+		check_err 1 "pin dump failed"
+		log_test "DPLL pin enumeration"
+		return
+	fi
+	if [ "${#pin_ids[@]}" -eq 0 ]; then
+		dpll_log_test_skip_or_fail "DPLL pin enumeration" \
+		    "no pins found"
+		return
+	fi
+	check_pin_ids "${pin_ids[@]}"
+
+	log_test "DPLL pin enumeration (${#pin_ids[@]} pins)"
+}
+
+check_pin_properties()
+{
+	local pin_id=$1
+	local json field
+
+	# A pin that cannot be queried at all would be reported as one
+	# that misses every attribute, so bail out on that first.
+	if ! json=$(dpll_pin_json "$pin_id") || [ -z "$json" ]; then
+		check_err 1 "pin $pin_id query failed"
+		return
+	fi
+
+	# Frequency is optional, a pin without a frequency callback such
+	# as a recovered clock pin does not report it.
+	for field in '.id' '.type'; do
+		if ! jq -e "$field" <<< "$json" &> /dev/null; then
+			check_err 1 \
+			    "pin $pin_id is missing the $field attribute"
+		fi
+	done
+
+	# A pin registered on a parent pin has parent-pin nests and no
+	# parent-device nest, see test_pin_state. When the device under
+	# test is in the parent-device nest of a pin, that entry must carry
+	# the direction. A pin can be registered on more than one device,
+	# so match on parent-id instead of taking the first entry.
+	if jq -e --argjson dev "$DPLL_DEV_ID" \
+		'[.["parent-device"][]? | select(.["parent-id"] == $dev)] |
+		 length > 0' <<< "$json" &> /dev/null; then
+		if ! jq -e --argjson dev "$DPLL_DEV_ID" \
+			'.["parent-device"][]? |
+			 select(.["parent-id"] == $dev) | .direction' \
+			<<< "$json" &> /dev/null; then
+			check_err 1 \
+			    "pin $pin_id parent-device is missing direction"
+		fi
+	fi
+}
+
+test_pin_properties()
+{
+	local pin_ids pin_id
+
+	RET=0
+
+	if ! dpll_pin_ids_read pin_ids dpll_pin_get_ids "$DPLL_DEV_ID"; then
+		check_err 1 "pin dump failed"
+		log_test "DPLL pin properties"
+		return
+	fi
+	if [ "${#pin_ids[@]}" -eq 0 ]; then
+		dpll_log_test_skip_or_fail "DPLL pin properties" "no pins found"
+		return
+	fi
+	if ! check_pin_ids "${pin_ids[@]}"; then
+		log_test "DPLL pin properties"
+		return
+	fi
+
+	for pin_id in "${pin_ids[@]}"; do
+		check_pin_properties "$pin_id"
+	done
+
+	log_test "DPLL pin properties (${#pin_ids[@]} pins)"
+}
+
+test_pin_state()
+{
+	local pin_ids pin_id state
+
+	RET=0
+
+	if ! dpll_pin_ids_read pin_ids dpll_pin_find_by_direction \
+			       "$DPLL_DEV_ID" input; then
+		check_err 1 "pin dump failed"
+		log_test "DPLL pin state"
+		return
+	fi
+	pin_id=${pin_ids[0]:-}
+	if [ -z "$pin_id" ]; then
+		dpll_log_test_skip_or_fail "DPLL pin state" "no input pin"
+		return
+	fi
+
+	# The pin was found through its parent-device nest for this device
+	# and the state is mandatory there, so an empty value is a failed
+	# query rather than a state. A pin registered on another pin, a
+	# recovered clock of ice for one, is not among the pins found.
+	# dpll_pin_on_pin_register() adds it to the devices of its parent,
+	# but dpll_cmd_pin_get_one() puts out parent-device nests only for
+	# a pin without parent pins, a pin on a pin gets parent-pin nests.
+	state=$(dpll_pin_get_state "$DPLL_DEV_ID" "$pin_id")
+	case "$state" in
+	"")
+		check_err 1 "cannot read the state of pin $pin_id"
+		;;
+	connected|disconnected|selectable)
+		;;
+	*)
+		check_err 1 "pin $pin_id reports invalid state '$state'"
+		;;
+	esac
+
+	log_test "DPLL pin state"
+}
+
 ##############################################################################
 
 cleanup()
@@ -50,9 +200,9 @@ cleanup()
 	# A second signal must not abort the teardown half way.
 	trap '' INT TERM
 
-	if [ -n "$CREATED_DEVICE" ]; then
-		dpllsim_delete_device "$CREATED_DEVICE" ||
-			log_info "cannot delete device $CREATED_DEVICE"
+	if [ -n "$DPLL_CREATED_DEVICE" ]; then
+		dpllsim_delete_device "$DPLL_CREATED_DEVICE" ||
+			log_info "cannot delete device $DPLL_CREATED_DEVICE"
 	fi
 	# The device whose creation a signal interrupted is recorded by the
 	# library alone, a double delete of a tracked one is harmless.
@@ -73,7 +223,6 @@ if [ -z "${DPLL_DEV_ID:-}" ] &&
 	dpll_lib_lock
 fi
 
-CREATED_DEVICE=""
 # The module check may mount configfs, so the exit trap that unmounts it
 # again has to be in place before. INT and TERM run the cleanup as well,
 # so that a timeout imposed by the harness still removes the device, and
@@ -95,8 +244,8 @@ if [ -z "$DPLL_DEV_ID" ]; then
 		echo "FAIL: cannot create dpllsim device"
 		exit "$ksft_fail"
 	fi
-	CREATED_DEVICE=$DPLLSIM_CREATED_NAME
 	DPLL_DEV_ID=$DPLLSIM_DEV_ID
+	DPLL_CREATED_DEVICE=$DPLLSIM_CREATED_NAME
 	DPLL_MODULE_NAME="dpllsim"
 fi
 
diff --git a/tools/testing/selftests/drivers/dpll/dpll_lib.sh b/tools/testing/selftests/drivers/dpll/dpll_lib.sh
index 467527a4343afb..19e652a8e8da4e 100644
--- a/tools/testing/selftests/drivers/dpll/dpll_lib.sh
+++ b/tools/testing/selftests/drivers/dpll/dpll_lib.sh
@@ -51,6 +51,10 @@ dpll_lib_check_prereqs()
 		echo "SKIP: dpll tool too old, missing 'device show'"
 		exit "$ksft_skip"
 	fi
+	if ! dpll pin help 2>&1 | grep -q "show"; then
+		echo "SKIP: dpll tool too old, missing 'pin show'"
+		exit "$ksft_skip"
+	fi
 }
 
 # The scripts that create dpllsim devices share the one dpllsim directory
@@ -162,15 +166,39 @@ __dpll_lib_dump_failed()
 	exit "$ksft_fail"
 }
 
+# Name of the dpllsim device a script created as the device under test,
+# set by the script next to DPLL_DEV_ID. Empty when the user provided
+# the device.
+DPLL_CREATED_DEVICE=""
+
+# The layout and the capabilities of the device a script created are
+# known, so a missing pin, attribute or capability on it can only mean a
+# failed query or broken tooling. Report such a condition as a failure
+# on the created device and as a skip on a device the user provided,
+# where it is a property of the hardware.
+# $1: test name, $2: reason
+dpll_log_test_skip_or_fail()
+{
+	local name=$1
+	local reason=$2
+
+	if [ -n "$DPLL_CREATED_DEVICE" ]; then
+		check_err 1 "$reason on the device created by the test"
+		log_test "$name"
+	else
+		log_test_skip "$name" "$reason"
+	fi
+}
+
 ##############################################################################
 # Device queries
 #
 # "dpll device show id X -j" prints a flat object, a dump wraps its
 # objects in {"device":[...]}. The helpers normalize with
 # (.device[0]? // .) before extracting fields, so that either shape
-# works. The tool is run first and jq only on what it printed, so that a
-# failed query returns an error instead of the empty output a missing
-# field produces too.
+# works, same for pins. The device helpers run the tool first and jq
+# only on what it printed, so that a failed query returns an error
+# instead of the empty output a missing field produces too.
 
 dpll_device_json()
 {
@@ -253,12 +281,127 @@ dpll_device_wait_gone_by_clock_id()
 	busywait "$BUSYWAIT_TIMEOUT" __dpll_device_absent_by_clock_id "$@"
 }
 
+##############################################################################
+# Pin queries
+#
+# Attributes of the pin itself are read by pin id alone. Attributes that
+# live in the parent-device nest (state, prio, ...) take the device id
+# first and the pin id second, the device id selects the nest. The helpers
+# that work on the full pin dump normalize its shape like the id-based
+# ones do, with (.pin? // [.]), so that a flat object would work too.
+# Like the device queries, every helper runs the tool first and jq only
+# on what it printed, and returns an error when the tool failed.
+
+dpll_pin_json()
+{
+	local pin_id=$1
+	local out
+
+	out=$(dpll pin show id "$pin_id" -j 2>/dev/null) || return 1
+	jq '(.pin[0]? // .)' <<< "$out"
+}
+
+__dpll_pin_field()
+{
+	local pin_id=$1; shift
+	local out
+
+	out=$(dpll pin show id "$pin_id" -j 2>/dev/null) || return 1
+	jq -r "(.pin[0]? // .) | $1 // empty" <<< "$out"
+}
+
+# Fields living in the parent-device nest matching the given device
+__dpll_pin_dev_field()
+{
+	local dev_id=$1
+	local pin_id=$2; shift 2
+	local out
+
+	out=$(dpll pin show id "$pin_id" -j 2>/dev/null) || return 1
+	jq -r --argjson dev "$dev_id" \
+	   "(.pin[0]? // .) | .[\"parent-device\"][]? |
+	    select(.[\"parent-id\"] == \$dev) | $1 // empty" <<< "$out"
+}
+
+# Print ids of all pins belonging to the device, one per line: pins
+# registered on the device directly plus pins registered on those pins
+# (MUX children), followed recursively so that children of children are
+# included too. The filter of "pin show" by device is spelled "device"
+# in iproute2 up to 7.0 and "parent-device" from 7.1 on, so filter the
+# full dump with jq.
+dpll_pin_get_ids()
+{
+	local dev_id=$1
+	local out
+
+	out=$(dpll pin show -j 2>/dev/null) || return 1
+	jq -r --argjson dev "$dev_id" '
+		(.pin? // [.]) as $pins |
+		def children($ids):
+			[$pins[] | select(any(.["parent-pin"][]?;
+					     .["parent-id"] as $p |
+					     $ids | index($p))) | .id];
+		def closure($ids):
+			(($ids + children($ids)) | unique) as $n |
+			if ($n | length) == ($ids | length) then $ids
+			else closure($n) end;
+		closure([$pins[] |
+			 select(any(.["parent-device"][]?;
+				    .["parent-id"] == $dev)) | .id])
+		as $all |
+		$pins[] | select(.id as $i | $all | index($i)) | .id' \
+		<<< "$out"
+}
+
+# Print ids of all non-MUX pins of the device with the given direction
+# (input/output), one per line. Direction lives in the parent-device nest.
+dpll_pin_find_by_direction()
+{
+	local dev_id=$1
+	local dir=$2
+	local out
+
+	out=$(dpll pin show -j 2>/dev/null) || return 1
+	jq -r --argjson dev "$dev_id" --arg dir "$dir" \
+	   '(.pin? // [.])[] | select(.type != "mux") |
+	    select(.["parent-device"][]? |
+		   (.["parent-id"] == $dev and .direction == $dir)) |
+	    .id' <<< "$out"
+}
+
+# Run a helper that prints one pin id per line, dpll_pin_get_ids or
+# dpll_pin_find_by_direction with its arguments, and store the ids in the
+# array named by $1. Return 1 when the helper failed, the status a
+# process substitution or a pipeline would drop, so that a caller can
+# tell a failed dump from a device without such pins.
+# $1: array name, $2...: helper and its arguments
+dpll_pin_ids_read()
+{
+	local -n __dpll_ids=$1; shift
+	local out
+
+	__dpll_ids=()
+	out=$("$@") || return 1
+	[ -z "$out" ] || mapfile -t __dpll_ids <<< "$out"
+}
+
+dpll_pin_get_frequency()
+{
+	__dpll_pin_field "$1" '.frequency'
+}
+
+dpll_pin_get_state()
+{
+	__dpll_pin_dev_field "$1" "$2" '.state'
+}
+
 ##############################################################################
 # dpllsim configfs interface
 #
-# Each directory under $DPLLSIM_CFG is one simulated device. Writing 1 to
-# <dev>/live registers the device with the DPLL core, writing 0
-# unregisters it. The structure (clock_id) can only be changed while the
+# Each directory under $DPLLSIM_CFG is one simulated device; its pins are
+# directories under <dev>/pins/. Writing 1 to <dev>/live registers the
+# device and its pins with the DPLL core, writing 0 unregisters them. The
+# structure (pins, clock_id, pin attributes) can only be changed while the
 # device is not live.
 #
 # dpllsim_available loads the dpllsim module and mounts configfs when
@@ -369,6 +512,28 @@ dpllsim_create_device()
 	fi
 }
 
+# $1: device name, $2: pin name (becomes the board-label), $3: type (gnss,
+# int-oscillator, ext, synce-eth-port, mux), $4: frequency in Hz,
+# $5: direction (input/output)
+dpllsim_add_pin()
+{
+	local dev=$1
+	local pin=$2
+	local type=$3
+	local frequency=$4
+	local direction=$5
+	local pin_path="$DPLLSIM_CFG/$dev/pins/$pin"
+
+	mkdir "$pin_path" 2>/dev/null || return 1
+
+	if ! echo "$type" 2>/dev/null > "$pin_path/type" ||
+	   ! echo "$frequency" 2>/dev/null > "$pin_path/frequency" ||
+	   ! echo "$direction" 2>/dev/null > "$pin_path/direction"; then
+		rmdir "$pin_path" 2>/dev/null
+		return 1
+	fi
+}
+
 dpllsim_deploy()
 {
 	echo 1 2>/dev/null > "$DPLLSIM_CFG/$1/live"
@@ -379,17 +544,22 @@ dpllsim_undeploy()
 	echo 0 2>/dev/null > "$DPLLSIM_CFG/$1/live"
 }
 
-# Take the device offline and remove it. Every step is attempted even
-# when an earlier one failed, the return value is non-zero when any did.
-# A device that does not exist counts as deleted.
+# Take the device offline and remove it together with all its pins.
+# Every step is attempted even when an earlier one failed, the return
+# value is non-zero when any did. A device that does not exist counts as
+# deleted.
 dpllsim_delete_device()
 {
 	local dev_path="$DPLLSIM_CFG/$1"
-	local ret=0
+	local pin_path ret=0
 
 	[ -d "$dev_path" ] || return 0
 
 	dpllsim_undeploy "$1" || ret=1
+	for pin_path in "$dev_path"/pins/*/; do
+		[ -d "$pin_path" ] || continue
+		rmdir "$pin_path" || ret=1
+	done
 	rmdir "$dev_path" || ret=1
 
 	return "$ret"
@@ -422,8 +592,21 @@ dpllsim_alloc_clock_id()
 	DPLLSIM_CLOCK_ID=$((DPLLSIM_NEXT_CLOCK_ID++))
 }
 
-# Create a device, deploy it and resolve the netlink device id. Sets
-# DPLLSIM_CREATED_NAME, DPLLSIM_CLOCK_ID and DPLLSIM_DEV_ID.
+# Add the pins of the standard layout, see below, to a device that is
+# not live. Return 1 when a pin cannot be added.
+dpllsim_add_standard_pins()
+{
+	dpllsim_add_pin "$1" gnss0 gnss 1 input &&
+	dpllsim_add_pin "$1" ext0 ext 10000000 input &&
+	dpllsim_add_pin "$1" out0 ext 10000000 output
+}
+
+# Create a device with the standard 3-pin layout, deploy it and resolve
+# the netlink device id. Sets DPLLSIM_CREATED_NAME, DPLLSIM_CLOCK_ID and
+# DPLLSIM_DEV_ID.
+#   gnss0: GNSS, 1 Hz, input
+#   ext0: EXT, 10 MHz, input
+#   out0: EXT, 10 MHz, output
 dpllsim_setup_standard_device()
 {
 	local clock_id name
@@ -433,6 +616,11 @@ dpllsim_setup_standard_device()
 	name="std$clock_id"
 	dpllsim_create_device "$name" "$clock_id" || return 1
 
+	if ! dpllsim_add_standard_pins "$name"; then
+		dpllsim_delete_device "$name"
+		return 1
+	fi
+
 	if ! dpllsim_deploy "$name"; then
 		dpllsim_delete_device "$name"
 		return 1
diff --git a/tools/testing/selftests/drivers/dpll/dpll_stress.sh b/tools/testing/selftests/drivers/dpll/dpll_stress.sh
index 2c60ac9faefda4..55b2a1acde47a1 100755
--- a/tools/testing/selftests/drivers/dpll/dpll_stress.sh
+++ b/tools/testing/selftests/drivers/dpll/dpll_stress.sh
@@ -56,20 +56,52 @@ cleanup()
 	dpll_lib_cleanup
 }
 
-# Create a device named after the given clock-id and deploy it. Sets
-# DPLLSIM_CREATED_NAME. A device that fails half way and cannot be removed
-# is tracked, so that the cleanup handler tries again, the next device
-# takes DPLLSIM_CREATED_NAME over.
+# Pin type and a frequency the simulator supports for it.
+STRESS_PIN_KINDS=(
+	"gnss 1"
+	"ext 10000000"
+	"ext 25000000"
+	"synce-eth-port 10000000"
+	"int-oscillator 25000000"
+	"mux 2048000"
+)
+
+# Create a device named after the given clock-id with the given pin count
+# and deploy it. Sets DPLLSIM_CREATED_NAME. A device that fails half way
+# and cannot be removed is tracked, so that the cleanup handler tries
+# again, the next device takes DPLLSIM_CREATED_NAME over.
 create_stress_device()
 {
 	local clock_id=$1
+	local num_pins=$2
 	local name="st$clock_id"
+	local i type freq dir
+	local dirs=(input output)
 
 	if ! dpllsim_create_device "$name" "$clock_id"; then
 		[ "$DPLLSIM_CREATED_NAME" = "$name" ] && track_device "$name"
 		return 1
 	fi
 
+	# Always have at least one plain input pin, then random ones
+	if ! dpllsim_add_pin "$name" ext0 ext 10000000 input; then
+		dpllsim_delete_device "$name" || track_device "$name"
+		return 1
+	fi
+	for ((i = 1; i < num_pins; i++)); do
+		read -r type freq <<< \
+			"${STRESS_PIN_KINDS[RANDOM % ${#STRESS_PIN_KINDS[@]}]}"
+		# A MUX pin is an input selector, the simulator rejects an
+		# output one
+		dir=${dirs[RANDOM % 2]}
+		[ "$type" = mux ] && dir=input
+		if ! dpllsim_add_pin "$name" "pin$i" "$type" "$freq" \
+				     "$dir"; then
+			dpllsim_delete_device "$name" || track_device "$name"
+			return 1
+		fi
+	done
+
 	if ! dpllsim_deploy "$name"; then
 		dpllsim_delete_device "$name" || track_device "$name"
 		return 1
@@ -106,7 +138,7 @@ test_rapid_create_delete()
 	for i in $(seq 1 "$STRESS_ITERATIONS"); do
 		dpllsim_alloc_clock_id; clock_id=$DPLLSIM_CLOCK_ID
 
-		if ! create_stress_device "$clock_id"; then
+		if ! create_stress_device "$clock_id" 5; then
 			check_err 1 "failed to create device (iteration $i)"
 			break
 		fi
@@ -144,6 +176,12 @@ test_deploy_cycle()
 	fi
 	track_device "$name"
 
+	if ! dpllsim_add_standard_pins "$name"; then
+		check_err 1 "cannot add pins"
+		log_test "DPLL deploy/undeploy cycling"
+		return
+	fi
+
 	# The live write registers or unregisters the device before it
 	# returns, so a wait passes at its first look and one that times
 	# out ends the loop, the waits cannot add up to more than one
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH net-next v5 05/14] dpll: dpllsim: Add support to set input pin state
  2026-11-25  9:05 [PATCH net-next v5 00/14] dpll: add a simulated DPLL device driver Petr Oros
                   ` (3 preceding siblings ...)
  2026-11-25  9:05 ` [PATCH net-next v5 04/14] selftests: dpll: add pin enumeration and property tests Petr Oros
@ 2026-11-25  9:05 ` Petr Oros
  2026-11-25  9:05 ` [PATCH net-next v5 06/14] selftests: dpll: add pin state tests Petr Oros
                   ` (8 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: Petr Oros @ 2026-11-25  9:05 UTC (permalink / raw)
  To: netdev
  Cc: Petr Oros, Ivan Vecera, Jonathan Corbet, Shuah Khan, Shuah Khan,
	Randy Dunlap, Jani Nikula, Vadim Fedorenko, Arkadiusz Kubalewski,
	Jiri Pirko, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, linux-doc, linux-kselftest

Add support for setting the state of input pins on the device.
Implement the state_on_dpll_set callback for input pins and advertise
the state-can-change capability for them, so that userspace can move a
pin between the states valid for the device mode. The core checks the
capability only, the states a mode allows are up to the driver: in
automatic mode a pin is selectable or disconnected, in manual mode
connected or disconnected, as Documentation/driver-api/dpll.rst
describes them. The device reports automatic mode, so connected is
refused with -EINVAL and an extack message. The check is written for
both modes already, so that manual mode has nothing to add to it when
it arrives. The driver stores the requested state and reports it back
unchanged, it does not act on it yet. Output pins keep reporting their
state only.

Reviewed-by: Ivan Vecera <ivecera@redhat.com>
Signed-off-by: Petr Oros <poros@redhat.com>
---
v5:
- no changes

v4:
- no change, add the Reviewed-by given to v3 (Ivan Vecera)

v3:
- refuse pin states that are not valid in the device mode, so that
  connected is refused in automatic mode, the mode the device reports,
  with an extack message (Ivan Vecera)
- the Acked-by of v2 is not carried over, the callback changed

v4: https://lore.kernel.org/all/20261112130359.840795-6-poros@redhat.com/
v3: https://lore.kernel.org/all/20261109093523.931201-6-poros@redhat.com/
v2: https://lore.kernel.org/all/20261105151852.1844305-6-poros@redhat.com/
v1: https://lore.kernel.org/all/20261103093910.596225-6-poros@redhat.com/
---
 drivers/dpll/dpllsim/dpll.c | 42 +++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/drivers/dpll/dpllsim/dpll.c b/drivers/dpll/dpllsim/dpll.c
index 7323747ccf2a62..d838a2bbdf52f6 100644
--- a/drivers/dpll/dpllsim/dpll.c
+++ b/drivers/dpll/dpllsim/dpll.c
@@ -104,10 +104,46 @@ static int dpllsim_pin_state_get(const struct dpll_pin *pin, void *pin_priv,
 	return 0;
 }
 
+/*
+ * The states a user may request depend on the device mode: connected and
+ * disconnected select and deselect the reference in manual mode, selectable
+ * and disconnected offer a pin to or withdraw it from the automatic
+ * selection. The core checks the state-can-change capability only.
+ */
+static bool dpllsim_pin_state_valid(const struct dpllsim_dev *sim,
+				    enum dpll_pin_state state)
+{
+	if (state == DPLL_PIN_STATE_DISCONNECTED)
+		return true;
+	if (sim->mode == DPLL_MODE_MANUAL)
+		return state == DPLL_PIN_STATE_CONNECTED;
+	return state == DPLL_PIN_STATE_SELECTABLE;
+}
+
+static int dpllsim_pin_state_set(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)
+{
+	struct dpllsim_pin *sim_pin = pin_priv;
+	struct dpllsim_dev *sim = sim_pin->dev;
+
+	guard(mutex)(&sim->lock);
+	if (!dpllsim_pin_state_valid(sim, state)) {
+		NL_SET_ERR_MSG_FMT(extack, "Pin state not valid in %s mode",
+				   sim->mode == DPLL_MODE_MANUAL ?
+				   "manual" : "automatic");
+		return -EINVAL;
+	}
+	sim_pin->state = state;
+	return 0;
+}
+
 static const struct dpll_pin_ops dpllsim_input_pin_ops = {
 	.frequency_get = dpllsim_pin_frequency_get,
 	.direction_get = dpllsim_pin_direction_get,
 	.state_on_dpll_get = dpllsim_pin_state_get,
+	.state_on_dpll_set = dpllsim_pin_state_set,
 };
 
 static const struct dpll_pin_ops dpllsim_output_pin_ops = {
@@ -149,6 +185,12 @@ static void dpllsim_pin_props(struct dpllsim_pin *pin,
 		prop->freq_supported_num = ARRAY_SIZE(dpllsim_freq_ext);
 		break;
 	}
+
+	/* Capabilities follow the ops the pin is served by */
+	if (dpllsim_pin_select_ops(pin) == &dpllsim_input_pin_ops)
+		prop->capabilities = DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE;
+	else
+		prop->capabilities = 0;
 }
 
 static bool dpllsim_pin_frequency_valid(struct dpllsim_pin *pin)
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH net-next v5 06/14] selftests: dpll: add pin state tests
  2026-11-25  9:05 [PATCH net-next v5 00/14] dpll: add a simulated DPLL device driver Petr Oros
                   ` (4 preceding siblings ...)
  2026-11-25  9:05 ` [PATCH net-next v5 05/14] dpll: dpllsim: Add support to set input pin state Petr Oros
@ 2026-11-25  9:05 ` Petr Oros
  2026-11-25  9:05 ` [PATCH net-next v5 07/14] dpll: dpllsim: Add support to get/set priority on input pins Petr Oros
                   ` (7 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: Petr Oros @ 2026-11-25  9:05 UTC (permalink / raw)
  To: netdev
  Cc: Petr Oros, Jonathan Corbet, Shuah Khan, Shuah Khan, Randy Dunlap,
	Jani Nikula, Vadim Fedorenko, Arkadiusz Kubalewski, Jiri Pirko,
	Ivan Vecera, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, linux-doc, linux-kselftest

Add dpll_features.sh, a script that exercises individual pin callbacks
with get/set roundtrips over netlink. It creates a dpllsim device with
the standard pin layout by default or runs against the device named by
DPLL_DEV_ID or the first device of the driver named by DPLL_DRIVER, and
picks the pin to reconfigure up front, so that no test touches the
reference the device is locked to. The pin is an input that is
disconnected or selectable on the device and that none of its parent
devices reports as connected or, where the tool prints the operational
state, as active, since a pin can be registered on several devices. A
parent device that is locked or in holdover and shows no such input
hides its reference, so no pin is picked then, while an unlocked device
has no reference yet. The NCO input of zl3073x, which iproute2 prints as
a pin of unknown type so far, and both pins of a connected reference
sync pair are left alone. The comment above pin_is_spare has the
details. When the first input pin and another one both qualify the other
one is taken, drivers tend to list their primary reference first. Every
test checks again that the pin is spare before it changes it. A spare
pin can still be selected while a test reconfigures it, which the header
of the script points out.

The first test moves that pin between disconnected and selectable, the
states of automatic mode as Documentation/driver-api/dpll.rst defines
them, reads the state back after each step and restores the original
state at the end. On the device the script created it then checks that
connected is refused. Hardware drivers are not held to refusing
connected, ice accepts it in automatic mode, and a pin with the
state-connected-override capability would take over the reference. On a
device provided by the user a disconnected pin is offered to the
selection only when its priority ranks below the one of the reference on
every device it is registered on, it could take over otherwise, and the
readback accepts that the device picked the pin as its reference. Manual
mode, where connecting the pin makes it the reference, is left out until
the simulator can change its mode, a device provided by the user in
manual mode is a skip.

A driver may refuse a state the capability promises, for a constraint
the interface does not tell. That is a skip on a device provided by the
user, with the reason the tool reported, and a failure on the device the
script created, whose simulator accepts both states of its mode. A
refusal that follows a failed readback leaves the test failed,
dpll_log_test_skip_or_fail no longer turns a recorded failure into a
skip. The restore is deferred right after the original state is read. It
acts only while the pin is changed, when the test ends early or its own
restore was refused, one that gets to the end otherwise restores the pin
itself and checks it. The cleanup handler of every script that defers
runs the pending restores before it removes the devices the script
created, so that an interrupted run puts the device back as well. defer
reports nothing about what it runs, so the restores go through
dpll_restore, which logs one that fails, fails the run and returns its
status. A pin that cannot be queried is a failure, not a pin that is in
use or lacks a capability, and so is a device that does not report its
mode or the state of the pin, both are mandatory.

The stress script gains a test that floods one input pin of a fresh
device with changes between the states of automatic mode and ends on
selectable, the state a fresh pin is not in, so that the readback after
the flood catches a setter that stores nothing. The library gains the
state setter both use, which keeps the extack line of the tool in
DPLL_LAST_ERR so that a test can tell a refusal from a failure, the
device mode getter and a check whether the pin set usage of the tool
knows a keyword, so that a test skips on a tool too old for it rather
than every script requiring a newer tool.

Signed-off-by: Petr Oros <poros@redhat.com>
---
v5:
- take a pin a device reports active as its reference, zl3073x
  reports the reference of automatic mode as selectable
- pick a pin only when every device it is registered on shows which
  input it uses or reports unlocked, that is not locked to any input
  yet; a device that is locked or in holdover without one hides its
  reference and the pin cannot be told from it
- leave out the NCO input of zl3073x, which no iproute2 release names
  so far and prints as a pin of unknown type, and both pins of a
  connected reference sync pair, the sync source is named by the dump
  of the other pin only
- check right before the test that the pin is still spare, by all its
  parent devices and the operational state instead of its state on
  the device alone
- skip the state test when the state read before it is neither
  disconnected nor selectable, the selection may have taken the pin
  since the check, and say that zl3073x keeps reporting a pin it
  selected as selectable
- leave manual mode out of the state test until the simulator can
  change its mode, no device in this series could run it
- report a pin state that cannot be read back as such, not as a wrong
  state, in the state test and after the state flood
- return the status of a failed restore from dpll_restore,
  DEFER_PAUSE_ON_FAIL stops on it
- offer a disconnected pin to the automatic selection of a device
  provided by the user only when it ranks below the reference on
  every device it is registered on, ice shares the state of a pin
  between its devices, and not at all when one of them has no
  reference and would lock to it
- accept a reference picked by the selection in automatic mode only
  on a device provided by the user, the simulator selects no pin
- fail when the device does not report its mode or the pin its state,
  both are mandatory
- skip the deferred restores once the test restored the pin itself
- ignore further signals in the INT and TERM handler and run the
  cleanup from it, see patch 2
- correct the comment on what the keyword check of the pin set usage
  accepts
- call the script a pin feature test in its header, no device
  callback is exercised yet, and say there what the tests leave alone
- skip the state flood when STRESS_STATE_CHANGES is not a positive
  number, the readback had no change to compare with
- say in the comment on the lock that a run of the feature tests
  against a device the user provided takes it as well
- report a failed pin dump of the state flood once, it counted as a
  missing input pin as well
- keep the state that was set when a readback in the loop failed, the
  check of the refused state compared with an empty one
- keep the deferred restores armed when a restore of the test itself
  is refused on a run that failed before
- check the flood count with the helper of the other stress tests
- say in the header that a pin sharing its input with another one, as
  U.FL2 with SMA2 on E810-T, changes that one too
- fail the run when a deferred restore fails, a refused restore left
  the pin changed behind a skip
- run a deferred restore only while the pin is changed, a refused write
  of the value the pin still held failed the run; TUNE_CHANGED is
  raised before a set of another value, so that a signal during the set
  restores the pin too, and cleared when the set is refused or the
  restore of the test went through, tune_restore checks it for the
  priority, frequency and phase adjust tests as well
- keep the commit message in its four paragraphs on the choice of the
  pin, the state test, refusals and restores, and the flood test, and
  the changelog with one item per review comment
- the Reviewed-by of v4 is not carried over, the state test changed

v4:
- defer the reconnect of the reference before the restore of the test
  pin, deferred commands run last in first out
- do not let a refusal turn a test that already failed into a skip
- pick the spare pin by all its parent devices and by the operational
  state where the tool prints it, and leave it alone when the automatic
  selection took it in the meantime
- accept connected when a pin was set selectable in automatic mode,
  the device may pick the pin it was offered as its reference, also
  when the restore makes it selectable again
- end the state flood on selectable, so that the readback catches a
  setter that stores nothing
- fail when the pin dump fails or the simulated device cannot be created
- say in the header what the tests change on a device the user provided
- log a deferred restore that fails, defer drops the status of what it
  runs
- tell a pin that could not be queried from one that is in use or
  lacks a capability, and log the reason of a refusal that follows a
  failure
- take the number of state changes of the flood from one variable
- add the Reviewed-by given to v3 (Ivan Vecera)

v3:
- follow the mode of the device under test: disconnected and
  selectable in automatic mode, where the simulator is checked to
  refuse connected, connected and disconnected in manual mode, where
  the pin connected before is connected back at the end
- cycle the state flooding test through the states of automatic mode
- add the device mode getter to the library
- honor DPLL_DRIVER like dpll_basic.sh does
- follow the move of the selftests to
  tools/testing/selftests/drivers/dpll
- the Reviewed-by and Acked-by of v2 are not carried over, the
  state test changed

v2:
- quote the ksft_skip expansion (netdev CI shellcheck)

v4: https://lore.kernel.org/all/20261112130359.840795-7-poros@redhat.com/
v3: https://lore.kernel.org/all/20261109093523.931201-7-poros@redhat.com/
v2: https://lore.kernel.org/all/20261105151852.1844305-7-poros@redhat.com/
v1: https://lore.kernel.org/all/20261103093910.596225-7-poros@redhat.com/
---
 tools/testing/selftests/drivers/dpll/Makefile |   1 +
 .../selftests/drivers/dpll/dpll_features.sh   | 477 ++++++++++++++++++
 .../selftests/drivers/dpll/dpll_lib.sh        | 116 ++++-
 .../selftests/drivers/dpll/dpll_stress.sh     |  73 +++
 4 files changed, 662 insertions(+), 5 deletions(-)
 create mode 100755 tools/testing/selftests/drivers/dpll/dpll_features.sh

diff --git a/tools/testing/selftests/drivers/dpll/Makefile b/tools/testing/selftests/drivers/dpll/Makefile
index 7e839ae71cd22f..1349d078fa2660 100644
--- a/tools/testing/selftests/drivers/dpll/Makefile
+++ b/tools/testing/selftests/drivers/dpll/Makefile
@@ -9,6 +9,7 @@ TEST_FILES := dpll_lib.sh
 
 TEST_PROGS := \
 	dpll_basic.sh \
+	dpll_features.sh \
 	dpll_stress.sh \
 # end of TEST_PROGS
 
diff --git a/tools/testing/selftests/drivers/dpll/dpll_features.sh b/tools/testing/selftests/drivers/dpll/dpll_features.sh
new file mode 100755
index 00000000000000..61ebfd9e718922
--- /dev/null
+++ b/tools/testing/selftests/drivers/dpll/dpll_features.sh
@@ -0,0 +1,477 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# DPLL pin feature tests. Exercises individual dpll_pin_ops callbacks,
+# verifying get/set roundtrips through the netlink interface. By default
+# a dpllsim device with a known pin layout is created. Set DPLL_DEV_ID or
+# DPLL_DRIVER to run against an existing device instead, the checks that
+# hold for the simulator alone then run only when the script created the
+# device, which DPLL_DRIVER=dpllsim asks for.
+#
+# The tests reconfigure a spare input pin of the device and restore it
+# afterwards, see pin_is_spare. A pin is taken only when it can be told
+# from the reference of every device it is registered on, and the tests
+# leave alone what could change the input a device uses. The state test
+# runs in automatic mode only, and on a device the user provided it
+# offers a disconnected pin to the automatic selection only when it
+# ranks below the reference. A spare pin can still be selected while a
+# test reconfigures it, when the reference fails meanwhile or when an
+# unlocked device is about to lock to it. A pin that shares its input with
+# another one, as U.FL2 does with SMA2 on E810-T, does not show that
+# through the interface, and reconfiguring it changes the other pin too.
+# So do not point the script at a device that must not be disturbed.
+#
+# Copyright (c) 2026 Red Hat, Inc.
+
+SCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
+
+source "$SCRIPTDIR/../../net/lib.sh"
+source "$SCRIPTDIR/dpll_lib.sh"
+
+# The input pins of the device under test, read once it is known.
+input_pins=()
+
+ALL_TESTS="
+	test_pin_state_transitions
+"
+
+##############################################################################
+# Pin-level feature tests
+
+# 1 while a test that tunes PIN_TUNE_ID may have left the pin at another
+# value than the one it found. It is raised before a set of another value,
+# a signal is handled only once the set returned, and lowered when the set
+# was refused or the restore of the test went through.
+TUNE_CHANGED=0
+
+# Run a deferred restore of a test that tunes PIN_TUNE_ID while the pin is
+# changed, the test returned early or its own restore was refused. A pin
+# that holds the value the test found needs no write, and a device may
+# refuse one for a reason of its own, ice while it resets, which would
+# fail the run, see dpll_restore.
+# $1...: setter and its arguments
+tune_restore()
+{
+	[ "$TUNE_CHANGED" -eq 1 ] || return 0
+	dpll_restore "$@"
+}
+
+# Return 0 when the pin ranks below the reference on every device it is
+# registered on, a lower value ranks higher, so that offering it to the
+# automatic selection cannot make any of them switch to it. A device
+# that shows no reference counts against the pin, an unlocked one would
+# lock to it. Return 1 otherwise and 2 when the pins could not be dumped.
+# $1: pin id
+pin_ranks_below_refs()
+{
+	local out
+
+	out=$(dpll pin show -j 2>/dev/null) || return 2
+	jq -e --argjson id "$1" '
+		(.pin? // [.]) as $pins |
+		[$pins[] | select(.id == $id) | .["parent-device"][]?] as $on |
+		($on | length) > 0 and
+		all($on[]; . as $mine |
+			[$pins[] | .["parent-device"][]? |
+			 select(.["parent-id"] == $mine["parent-id"] and
+				.direction == "input" and
+				(.state == "connected" or
+				 .operstate == "active")) | .prio] as $refs |
+			($refs | length) > 0 and
+			($mine.prio | type) == "number" and
+			all($refs[]; type == "number" and . < $mine.prio))' \
+		<<< "$out" &> /dev/null || return 1
+}
+
+# Return 0 when PIN_TUNE_ID advertises the capability. Otherwise log the
+# test, as failed when the pin could not be queried and as skipped or
+# failed with the given reason when it lacks the capability, and return
+# 1.
+# $1: capability, $2: test name, $3: reason
+pin_tune_capable()
+{
+	dpll_pin_has_capability "$PIN_TUNE_ID" "$1"
+	case $? in
+	0)
+		return 0
+		;;
+	1)
+		dpll_log_test_skip_or_fail "$2" "$3"
+		;;
+	*)
+		check_err 1 "pin $PIN_TUNE_ID query failed"
+		log_test "$2"
+		;;
+	esac
+	return 1
+}
+
+# Return 0 when PIN_TUNE_ID is still a spare input, see pin_is_spare. The
+# automatic selection may take it while the tests run, a test leaves it
+# alone then. Otherwise log the test as skipped or failed, or as failed
+# when the devices or the pins could not be dumped, and return 1.
+# $1: test name
+pin_tune_spare()
+{
+	pin_is_spare "$DPLL_DEV_ID" "$PIN_TUNE_ID"
+	case $? in
+	0)
+		return 0
+		;;
+	1)
+		dpll_log_test_skip_or_fail "$1" \
+		    "the input pin is not spare any more"
+		;;
+	*)
+		check_err 1 "device or pin dump failed"
+		log_test "$1"
+		;;
+	esac
+	return 1
+}
+
+test_pin_state_transitions()
+{
+	local mode before state after reason may_pick changed
+
+	RET=0
+
+	if [ -z "$PIN_TUNE_ID" ]; then
+		dpll_log_test_skip_or_fail "DPLL pin state transitions" \
+		    "no spare input pin"
+		return
+	fi
+	if ! dpll_pin_set_supports state; then
+		log_test_skip "DPLL pin state transitions" \
+		    "dpll tool lacks pin set state"
+		return
+	fi
+	pin_tune_capable state-can-change "DPLL pin state transitions" \
+			 "state cannot change" || return
+
+	# In automatic mode selectable and disconnected offer a pin to the
+	# automatic selection or withdraw it, see
+	# Documentation/driver-api/dpll.rst. The simulator runs in automatic
+	# mode. Manual mode, where connecting the pin makes it the reference,
+	# is not covered until the simulator can change its mode, a device
+	# the user provided in that mode is a skip. The mode and the state of
+	# a pin on a device are mandatory, an empty one is a query that
+	# failed.
+	mode=$(dpll_device_get_mode "$DPLL_DEV_ID")
+	case "$mode" in
+	automatic)
+		;;
+	"")
+		check_err 1 "device mode not reported"
+		log_test "DPLL pin state transitions"
+		return
+		;;
+	*)
+		dpll_log_test_skip_or_fail "DPLL pin state transitions" \
+		    "device mode '$mode' not covered"
+		return
+		;;
+	esac
+
+	pin_tune_spare "DPLL pin state transitions" || return
+	before=$(dpll_pin_get_state "$DPLL_DEV_ID" "$PIN_TUNE_ID")
+	if [ -z "$before" ]; then
+		check_err 1 "pin state not reported"
+		log_test "DPLL pin state transitions"
+		return
+	fi
+	# The selection may have taken the pin since pin_tune_spare
+	# looked, ice reports it connected then. The test would withdraw
+	# the reference, and the restore would write connected, which ice
+	# takes for disabling the input.
+	case "$before" in
+	disconnected|selectable)
+		;;
+	*)
+		dpll_log_test_skip_or_fail "DPLL pin state transitions" \
+		    "the input pin is not spare any more"
+		return
+		;;
+	esac
+	# Offering a disconnected pin to the automatic selection hands it
+	# its priority, and with a better one than the reference it takes
+	# over once its signal qualifies. A driver may share the state of a
+	# pin between the devices it is registered on, ice does. So on a
+	# device the user provided the pin is offered only when it ranks
+	# below the reference on every one of them. A device without a
+	# reference would lock to the pin and counts against it.
+	if [ -z "$DPLL_CREATED_DEVICE" ] && [ "$before" = disconnected ]; then
+		pin_ranks_below_refs "$PIN_TUNE_ID"
+		case $? in
+		0)
+			;;
+		1)
+			log_test_skip "DPLL pin state transitions" \
+			    "the spare input pin could be selected"
+			return
+			;;
+		*)
+			check_err 1 "pin dump failed"
+			log_test "DPLL pin state transitions"
+			return
+			;;
+		esac
+	fi
+
+	# The pin is not connected, see how PIN_TUNE_ID is picked, and the
+	# test never connects it, the device keeps the reference it locked
+	# to. The deferred restore runs only while the pin is in another state
+	# than the test found, when the test returns early or its own
+	# restore was refused. One that gets to the end puts the pin back
+	# itself and checks it.
+	# A device the user provided may pick the pin it was offered in
+	# automatic mode as its reference, ice reports the pin connected
+	# then, zl3073x keeps reporting it selectable. The simulator selects
+	# no pin and reports the state it was set to.
+	may_pick=""
+	[ -z "$DPLL_CREATED_DEVICE" ] && may_pick=1
+	TUNE_CHANGED=0
+	defer tune_restore dpll_pin_set_state "$DPLL_DEV_ID" "$PIN_TUNE_ID" \
+	      "$before"
+
+	# A driver may refuse a state the capability promises, for a
+	# constraint the interface does not tell. On a device provided by
+	# the user that is a skip with the reason the tool reported, on
+	# the created device a failure, the simulator accepts both states.
+	# The test ends there, whichever set was refused, a state that was
+	# not set has nothing to read back and the deferred restore puts the
+	# pin back when an earlier set changed it. A refusal after a
+	# readback failed does not turn the test into a skip, see
+	# dpll_log_test_skip_or_fail. A refused restore is treated the same
+	# way when nothing failed before it, afterwards it is one more
+	# failure. When the deferred restore is refused as well the run
+	# fails, see dpll_restore.
+	for state in disconnected selectable; do
+		changed=$TUNE_CHANGED
+		[ "$state" = "$before" ] || TUNE_CHANGED=1
+		if ! dpll_pin_set_state "$DPLL_DEV_ID" "$PIN_TUNE_ID" \
+					"$state"; then
+			TUNE_CHANGED=$changed
+			reason=${DPLL_LAST_ERR%%$'\n'*}
+			dpll_log_test_skip_or_fail \
+			    "DPLL pin state transitions" \
+			    "cannot set pin state to $state${reason:+, $reason}"
+			return
+		fi
+		[ "$state" = "$before" ] && TUNE_CHANGED=0
+		after=$(dpll_pin_get_state "$DPLL_DEV_ID" "$PIN_TUNE_ID")
+		if [ -z "$after" ]; then
+			check_err 1 "cannot read the pin state back"
+		elif [ "$after" != "$state" ] &&
+		     [ "$may_pick/$state/$after" != 1/selectable/connected ]
+		then
+			check_err 1 "pin not $state, state '$after'"
+		fi
+	done
+	# A pin the selection picked may report connected. A readback that
+	# failed was reported above and leaves the state that was set.
+	if [ -n "$after" ]; then
+		state=$after
+	fi
+
+	# Connected is not a state of automatic mode and the simulator
+	# refuses it. Hardware drivers are not held to that, ice for one
+	# accepts it there, and a pin with the state-connected-override
+	# capability would take over the reference, so the check runs on
+	# the device the script created only.
+	if [ -n "$DPLL_CREATED_DEVICE" ]; then
+		changed=$TUNE_CHANGED
+		TUNE_CHANGED=1
+		if dpll_pin_set_state "$DPLL_DEV_ID" "$PIN_TUNE_ID" \
+					connected; then
+			check_err 1 "connected accepted in automatic mode"
+		else
+			TUNE_CHANGED=$changed
+		fi
+		after=$(dpll_pin_get_state "$DPLL_DEV_ID" "$PIN_TUNE_ID")
+		if [ -z "$after" ]; then
+			check_err 1 "cannot read the pin state back"
+		elif [ "$after" != "$state" ]; then
+			check_err 1 "refused state changed the pin to '$after'"
+		fi
+	fi
+
+	if ! dpll_pin_set_state "$DPLL_DEV_ID" "$PIN_TUNE_ID" "$before"; then
+		reason=${DPLL_LAST_ERR%%$'\n'*}
+		reason=${reason:+, $reason}
+		if [ "$RET" -eq 0 ]; then
+			dpll_log_test_skip_or_fail \
+			    "DPLL pin state transitions" \
+			    "cannot set pin state to $before$reason"
+			return
+		fi
+		check_err 1 "cannot restore pin state$reason"
+	else
+		TUNE_CHANGED=0
+	fi
+	# The automatic selection may pick a pin that is selectable again,
+	# like it may in the loop above.
+	after=$(dpll_pin_get_state "$DPLL_DEV_ID" "$PIN_TUNE_ID")
+	if [ -z "$after" ]; then
+		check_err 1 "cannot read the restored pin state back"
+	elif [ "$after" != "$before" ] &&
+	     [ "$may_pick/$before/$after" != 1/selectable/connected ]; then
+		check_err 1 "pin state not restored, got '$after'"
+	fi
+
+	log_test "DPLL pin state transitions"
+}
+
+##############################################################################
+
+cleanup()
+{
+	# A second signal must not abort the teardown half way.
+	trap '' INT TERM
+
+	# Run the pending restores of an interrupted test first, the device
+	# they apply to may be one the user provided.
+	defer_scopes_cleanup
+	if [ -n "$DPLL_CREATED_DEVICE" ]; then
+		dpllsim_delete_device "$DPLL_CREATED_DEVICE" ||
+			log_info "cannot delete device $DPLL_CREATED_DEVICE"
+	fi
+	# The device whose creation a signal interrupted is recorded by the
+	# library alone, a double delete of a tracked one is harmless.
+	if [ -n "$DPLLSIM_CREATED_NAME" ]; then
+		dpllsim_delete_device "$DPLLSIM_CREATED_NAME" ||
+			log_info "cannot delete device $DPLLSIM_CREATED_NAME"
+	fi
+	dpll_lib_cleanup
+}
+
+dpll_lib_check_prereqs
+# Another run would find the devices of this one left behind, see the
+# lock in the library.
+dpll_lib_lock
+
+# The module check may mount configfs, so the exit trap that unmounts it
+# again has to be in place before. INT and TERM run the cleanup as well,
+# so that a timeout imposed by the harness still restores the device, and
+# ignore any further signal, an exit inside the cleanup would end the
+# teardown half way. Their handler drops the exit trap and calls the
+# cleanup itself, a signal that comes while the script exits is handled
+# before the exit trap gets to the cleanup, and the exit of the handler
+# would end the exit trap there.
+trap cleanup EXIT
+trap 'trap "" INT TERM; trap - EXIT; cleanup; exit 1' INT TERM
+
+if [ -n "${DPLL_DEV_ID:-}" ] || [ -n "${DPLL_DRIVER:-}" ]; then
+	dpll_lib_init
+fi
+# With the module there, a device that cannot be created, registered or
+# found on netlink is a failure of what the tests cover, not a skip.
+if [ -z "${DPLL_DEV_ID:-}" ]; then
+	dpllsim_check_module
+	if ! dpllsim_setup_standard_device; then
+		echo "FAIL: cannot create dpllsim device"
+		exit "$ksft_fail"
+	fi
+	DPLL_DEV_ID=$DPLLSIM_DEV_ID
+	DPLL_CREATED_DEVICE=$DPLLSIM_CREATED_NAME
+	DPLL_MODULE_NAME="dpllsim"
+fi
+
+log_info "testing DPLL device $DPLL_DEV_ID (module $DPLL_MODULE_NAME)"
+
+# Return 0 when the pin is a spare input of the device, one a test can
+# reconfigure without touching a reference. That takes all of this:
+# - The pin is disconnected or selectable on the device, and no device it
+#   is registered on reports it as connected or, where the tool prints
+#   the operational state, as active. What a test changes on a pin, the
+#   frequency for one, is shared by all its devices.
+# - Every device it is registered on shows which input it uses, or
+#   reports the lock status unlocked, which the uAPI defines as not
+#   locked to any valid input yet. A device that is locked or in holdover
+#   has or had a reference, and when no input shows as connected or
+#   active the pin cannot be told from it. zl3073x marks the reference of
+#   automatic mode by the operational state alone, which iproute2 up to
+#   7.1 does not print.
+# - It is not the NCO input of zl3073x, connecting that puts the channel
+#   in NCO mode. No iproute2 release names that pin type so far, the
+#   tool prints it as unknown, so a pin of a type the tool cannot name
+#   is left alone as well.
+# - It is in no connected reference sync pair, neither as the pin that
+#   carries the base frequency, whose dump lists the pair, nor as the
+#   sync source, which only the dump of the other pin names.
+# Return 1 when the pin is not spare and 2 when the devices or the pins
+# could not be dumped.
+# $1: device id, $2: pin id
+pin_is_spare()
+{
+	local devs pins
+
+	devs=$(dpll device show -j 2>/dev/null) || return 2
+	pins=$(dpll pin show -j 2>/dev/null) || return 2
+	jq -e -s --argjson dev "$1" --argjson id "$2" '
+		(.[0].device? // [.[0]]) as $devs |
+		(.[1].pin? // [.[1]]) as $pins |
+		[$pins[] | .["parent-device"][]? |
+		 select(.direction == "input" and
+			(.state == "connected" or .operstate == "active")) |
+		 .["parent-id"]] as $shown |
+		[$devs[] | select(.["lock-status"] != "unlocked") | .id]
+		as $held |
+		($pins[] | select(.id == $id)) as $pin |
+		[$pin["parent-device"][]?] as $on |
+		$pin.type != "int-nco" and $pin.type != "unknown" and
+		all($pin["reference-sync"][]?; .state != "connected") and
+		all($pins[] | .["reference-sync"][]?;
+		    .id != $id or .state != "connected") and
+		any($on[]; .["parent-id"] == $dev and
+			   (.state == "disconnected" or
+			    .state == "selectable")) and
+		all($on[]; .state != "connected" and
+			   .operstate != "active" and
+			   (.["parent-id"] as $p |
+			    ($shown | index($p)) != null or
+			    ($held | index($p)) == null))' \
+		<<< "$devs"$'\n'"$pins" &> /dev/null || return 1
+}
+
+# PIN_TUNE_ID is the input pin the tests reconfigure. It must not be a
+# reference, so it is taken from the spare inputs, see pin_is_spare.
+# When the first dumped input pin and another one both qualify the other
+# one is taken, drivers tend to list their primary reference first.
+# Tests that need the pin skip when there is none rather than
+# reconfiguring a reference. A dump that fails is a failure, not a
+# device without a spare pin.
+pick_tune_pin()
+{
+	local pin_id
+
+	PIN_TUNE_ID=""
+	for pin_id in "${input_pins[@]}"; do
+		pin_is_spare "$DPLL_DEV_ID" "$pin_id"
+		case $? in
+		0)
+			;;
+		1)
+			continue
+			;;
+		*)
+			echo "FAIL: cannot dump the DPLL devices and pins"
+			exit "$ksft_fail"
+			;;
+		esac
+		PIN_TUNE_ID=$pin_id
+		[ "$pin_id" != "${input_pins[0]}" ] && return
+	done
+}
+
+# A dump that failed is not a device without input pins.
+if ! dpll_pin_ids_read input_pins dpll_pin_find_by_direction \
+		       "$DPLL_DEV_ID" input; then
+	echo "FAIL: cannot dump the pins of DPLL device $DPLL_DEV_ID"
+	exit "$ksft_fail"
+fi
+pick_tune_pin
+
+tests_run
+
+exit $EXIT_STATUS
diff --git a/tools/testing/selftests/drivers/dpll/dpll_lib.sh b/tools/testing/selftests/drivers/dpll/dpll_lib.sh
index 19e652a8e8da4e..6a80177e1e0306 100644
--- a/tools/testing/selftests/drivers/dpll/dpll_lib.sh
+++ b/tools/testing/selftests/drivers/dpll/dpll_lib.sh
@@ -7,8 +7,8 @@
 # script calls dpll_lib_check_prereqs first, then dpll_lib_lock unless it
 # only reads a device the user named, and dpll_lib_init when it can run
 # against an existing device. net/lib.sh
-# must be sourced first, it provides ksft_skip, ksft_fail, busywait and
-# log_info.
+# must be sourced first, the library uses its exit codes, its logging
+# and its busywait, the scripts its defer as well.
 #
 # Copyright (c) 2026 Red Hat, Inc.
 #
@@ -60,8 +60,10 @@ dpll_lib_check_prereqs()
 # The scripts that create dpllsim devices share the one dpllsim directory
 # and cannot run alongside each other, so each takes a lock on
 # DPLL_LOCK_FILE before it mounts or creates anything and skips when
-# another run holds it. The file lives under /run rather than in the
-# world-writable /tmp, where a planted symlink could redirect the open.
+# another run holds it. A run of the feature tests against a device the
+# user provided takes it as well, two of them would change the same
+# pins. The file lives under /run rather than in the world-writable
+# /tmp, where a planted symlink could redirect the open.
 DPLL_RUN_DIR="/run/dpll-selftest"
 DPLL_LOCK_FILE="$DPLL_RUN_DIR/lock"
 DPLL_LOCK_FD=""
@@ -98,6 +100,45 @@ dpll_lib_lock()
 	fi
 }
 
+# Print the usage block of the given command of the given object from
+# the help of the dpll tool, "dpll pin set" for instance. The help lists
+# one usage per command, each starting with "Usage: dpll <obj> <cmd>" or
+# with "dpll <obj> <cmd>" indented under the first, possibly wrapped
+# over further lines. The block starts at the line that names the
+# command, with a space after the name so that a command whose name
+# merely begins the same way does not match, and ends before the next
+# line that names another command of the object, or at the end of the
+# help when none follows. Return 1 when the help has no usage for the
+# command, in which case nothing is printed.
+__dpll_usage_block()
+{
+	local obj=$1
+	local cmd=$2
+
+	dpll "$obj" help 2>&1 | \
+		awk -v start="[[:space:]]dpll $obj $cmd([[:space:]]|$)" \
+		    -v other="[[:space:]]dpll $obj [a-z-]+([[:space:]]|$)" \
+		    '$0 ~ start { s = 1; print; next }
+		     s && $0 ~ other { exit }
+		     s { print }
+		     END { exit !s }'
+}
+
+# Return 0 when the dpll tool knows the given "pin set" keyword, such as
+# state or frequency. A test that sets something checks its keyword
+# first and skips on a tool that is too old for it. Only the "pin set"
+# usage is searched, a keyword that only "pin show" knows must not
+# count, and the keyword must follow a blank or an opening bracket and
+# be followed by a blank or the end of the line, a hyphenated compound
+# that contains it does not count.
+dpll_pin_set_supports()
+{
+	local usage
+
+	usage=$(__dpll_usage_block pin set) || return 1
+	grep -qE "(^|[[:space:][])$1([[:space:]]|$)" <<< "$usage"
+}
+
 # Detect the DPLL device to test. DPLL_DEV_ID may be set by the user to
 # force a specific device. Otherwise DPLL_DRIVER names the driver whose
 # first device is taken, by the module name the device reports, dpllsim
@@ -175,7 +216,8 @@ DPLL_CREATED_DEVICE=""
 # known, so a missing pin, attribute or capability on it can only mean a
 # failed query or broken tooling. Report such a condition as a failure
 # on the created device and as a skip on a device the user provided,
-# where it is a property of the hardware.
+# where it is a property of the hardware. A test that recorded a failure
+# before stays failed on either, a skip would hide that failure.
 # $1: test name, $2: reason
 dpll_log_test_skip_or_fail()
 {
@@ -185,6 +227,11 @@ dpll_log_test_skip_or_fail()
 	if [ -n "$DPLL_CREATED_DEVICE" ]; then
 		check_err 1 "$reason on the device created by the test"
 		log_test "$name"
+	elif [ "$RET" -ne 0 ]; then
+		# The failed test reports the message of its first failure,
+		# so this reason is logged next to it.
+		log_test "$name"
+		log_info "$name: $reason"
 	else
 		log_test_skip "$name" "$reason"
 	fi
@@ -223,6 +270,11 @@ dpll_device_get_module()
 	__dpll_device_field "$1" '.["module-name"]'
 }
 
+dpll_device_get_mode()
+{
+	__dpll_device_field "$1" '.mode'
+}
+
 # Look up the netlink device id by the clock-id chosen at create time,
 # restricted to the devices of the given module when one is named. The
 # clock-id is the only reliable handle on a device the test created, as
@@ -395,6 +447,60 @@ dpll_pin_get_state()
 	__dpll_pin_dev_field "$1" "$2" '.state'
 }
 
+# Return 0 when the pin advertises the given capability, named as
+# iproute2 prints it (state-can-change, priority-can-change, ...), 1
+# when it does not and 2 when the pin could not be queried.
+dpll_pin_has_capability()
+{
+	local pin_id=$1
+	local name=$2
+	local out
+
+	out=$(dpll pin show id "$pin_id" -j 2>/dev/null) || return 2
+	jq -e --arg name "$name" \
+	   '(.pin[0]? // .) | (.capabilities // []) | index($name) != null' \
+	   <<< "$out" &> /dev/null || return 1
+}
+
+##############################################################################
+# Pin setters
+#
+# Every setter returns the status of the tool and keeps what the tool
+# printed on stderr in DPLL_LAST_ERR, the "Error: ..." line that carries
+# the extack message of the kernel, so that a caller can tell a refusal
+# it may skip on from a failure.
+
+DPLL_LAST_ERR=""
+
+# Run a setter that puts a pin back and log when it fails. The scripts
+# defer their restores through this, defer reports nothing about what
+# it runs and a pin left changed would go unnoticed. A failed restore
+# fails the run, since the test that deferred it has logged its result
+# already and defer drops the status. The status of the setter is
+# returned as well, DEFER_PAUSE_ON_FAIL stops on a failed restore.
+# $1...: setter and its arguments
+dpll_restore()
+{
+	local reason
+
+	"$@" && return 0
+	reason=${DPLL_LAST_ERR%%$'\n'*}
+	log_info "restore failed: $*${reason:+, $reason}"
+	EXIT_STATUS=$(ksft_exit_status_merge "$EXIT_STATUS" "$ksft_fail")
+	return 1
+}
+
+# $3: connected / disconnected / selectable
+dpll_pin_set_state()
+{
+	local dev_id=$1
+	local pin_id=$2
+	local state=$3
+
+	DPLL_LAST_ERR=$(dpll pin set id "$pin_id" parent-device "$dev_id" \
+			     state "$state" 2>&1 >/dev/null)
+}
+
 ##############################################################################
 # dpllsim configfs interface
 #
diff --git a/tools/testing/selftests/drivers/dpll/dpll_stress.sh b/tools/testing/selftests/drivers/dpll/dpll_stress.sh
index 55b2a1acde47a1..3c859825750974 100755
--- a/tools/testing/selftests/drivers/dpll/dpll_stress.sh
+++ b/tools/testing/selftests/drivers/dpll/dpll_stress.sh
@@ -13,10 +13,12 @@ source "$SCRIPTDIR/dpll_lib.sh"
 
 ALL_TESTS="
 	test_rapid_create_delete
+	test_pin_state_flooding
 	test_deploy_cycle
 "
 
 STRESS_ITERATIONS=${STRESS_ITERATIONS:-20}
+STRESS_STATE_CHANGES=${STRESS_STATE_CHANGES:-50}
 
 CREATED_DEVICES=()
 
@@ -43,6 +45,7 @@ cleanup()
 
 	local dev
 
+	defer_scopes_cleanup
 	for dev in "${CREATED_DEVICES[@]}"; do
 		dpllsim_delete_device "$dev" ||
 			log_info "cannot delete device $dev"
@@ -155,6 +158,76 @@ test_rapid_create_delete()
 	log_test "DPLL rapid create/delete ($STRESS_ITERATIONS iterations)"
 }
 
+test_pin_state_flooding()
+{
+	local clock_id name dev_id pin_ids pin_id i last state fails=0
+	local changes=$STRESS_STATE_CHANGES
+	# The states of automatic mode, the mode the simulator runs in. A
+	# fresh pin is disconnected, so the flood ends on selectable, the
+	# last change takes states[0] whatever their number, and the
+	# readback after it catches a setter that stores nothing.
+	local states=(selectable disconnected)
+
+	RET=0
+
+	# The readback compares with the last change, so there has to be
+	# one.
+	stress_counts_valid "DPLL pin state flooding" STRESS_STATE_CHANGES ||
+		return
+	if ! dpll_pin_set_supports state; then
+		log_test_skip "DPLL pin state flooding" \
+			      "dpll tool lacks pin set state"
+		return
+	fi
+
+	dpllsim_alloc_clock_id; clock_id=$DPLLSIM_CLOCK_ID
+	if ! create_stress_device "$clock_id" 10; then
+		check_err 1 "cannot create device"
+		log_test "DPLL pin state flooding"
+		return
+	fi
+	name=$DPLLSIM_CREATED_NAME
+	track_device "$name"
+
+	if ! dev_id=$(dpll_device_wait_by_clock_id "$clock_id" dpllsim); then
+		check_err 1 "device not visible on netlink"
+	else
+		if ! dpll_pin_ids_read pin_ids dpll_pin_find_by_direction \
+				       "$dev_id" input; then
+			check_err 1 "pin dump failed"
+		elif [ "${#pin_ids[@]}" -eq 0 ]; then
+			check_err 1 "no input pin on stress device"
+		else
+			pin_id=${pin_ids[0]}
+			for i in $(seq 1 "$changes"); do
+				last=${states[(changes - i) % 2]}
+				dpll_pin_set_state "$dev_id" "$pin_id" \
+						   "$last" ||
+					fails=$((fails + 1))
+			done
+			if [ "$fails" -gt 0 ]; then
+				check_err 1 \
+				    "$fails of $changes state changes failed"
+			fi
+			state=$(dpll_pin_get_state "$dev_id" "$pin_id")
+			if [ -z "$state" ]; then
+				check_err 1 "cannot read the pin state back"
+			elif [ "$state" != "$last" ]; then
+				check_err 1 \
+				    "state expected '$last', got '$state'"
+			fi
+		fi
+	fi
+
+	if dpllsim_delete_device "$name"; then
+		untrack_device "$name"
+	else
+		check_err 1 "cannot delete device $name"
+	fi
+
+	log_test "DPLL pin state flooding"
+}
+
 test_deploy_cycle()
 {
 	local clock_id name i
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH net-next v5 07/14] dpll: dpllsim: Add support to get/set priority on input pins
  2026-11-25  9:05 [PATCH net-next v5 00/14] dpll: add a simulated DPLL device driver Petr Oros
                   ` (5 preceding siblings ...)
  2026-11-25  9:05 ` [PATCH net-next v5 06/14] selftests: dpll: add pin state tests Petr Oros
@ 2026-11-25  9:05 ` Petr Oros
  2026-11-25  9:05 ` [PATCH net-next v5 08/14] selftests: dpll: add pin priority test Petr Oros
                   ` (6 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: Petr Oros @ 2026-11-25  9:05 UTC (permalink / raw)
  To: netdev
  Cc: Petr Oros, Jonathan Corbet, Shuah Khan, Shuah Khan, Randy Dunlap,
	Jani Nikula, Vadim Fedorenko, Arkadiusz Kubalewski, Jiri Pirko,
	Ivan Vecera, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, linux-doc, linux-kselftest

Add support for getting and setting the priority of input pins.
Implement the prio_get and prio_set callbacks and advertise the
priority-can-change capability for input pins, which includes the MUX
type pins since those are always inputs.

The initial priority of a pin is configured through a new prio attribute
of the pin directory, which takes a value between 0 and 255 and defaults
to 128, so that a test can lay out the reference order before the device
goes live. Like the other pin attributes it is writable only while the
device is not live. Priority changes made over netlink are stored and
reported back, the driver does not select a reference by them. They are
kept apart from the configured value, so that every live transition
starts from what configfs holds, the way the pin state starts from
disconnected, and the attribute keeps showing what was configured. The
setter rejects a value above 255 with EINVAL and an extack message. A
priority is meaningful for input pins only, so live fails with EINVAL
when an output pin was given a priority other than the default, next to
the frequency validation.

Signed-off-by: Petr Oros <poros@redhat.com>
---
v5:
- correct a comparison in the changelog, the pin state starts from
  disconnected and not from configfs, no code change

v4:
- keep the priority set over netlink apart from the configured one,
  every live transition starts from the configured value and the
  attribute keeps showing it
- document that, and that the driver only stores the priority
- the Reviewed-by and Acked-by of v3 are not carried over, the
  behaviour of the driver changed

v4: https://lore.kernel.org/all/20261112130359.840795-8-poros@redhat.com/
v3: https://lore.kernel.org/all/20261109093523.931201-8-poros@redhat.com/
---
 Documentation/driver-api/dpllsim.rst | 18 ++++++++---
 drivers/dpll/dpllsim/core.c          | 31 +++++++++++++++++++
 drivers/dpll/dpllsim/dpll.c          | 46 ++++++++++++++++++++++++++--
 drivers/dpll/dpllsim/dpllsim.h       |  6 ++++
 4 files changed, 95 insertions(+), 6 deletions(-)

diff --git a/Documentation/driver-api/dpllsim.rst b/Documentation/driver-api/dpllsim.rst
index 5b878ff51331c3..ce71c8bad6b375 100644
--- a/Documentation/driver-api/dpllsim.rst
+++ b/Documentation/driver-api/dpllsim.rst
@@ -38,6 +38,8 @@ Pin directory ``/sys/kernel/config/dpllsim/<dev>/pins/<pin>/``:
                (default 0, which is rejected)
 ``direction``  ``input`` or ``output`` (default ``input``), a ``mux`` pin
                must be an input
+``prio``       Initial priority 0-255 of an input pin, output pins must
+               keep the default (default 128)
 =============  ===============================================================
 
 The pin directory name becomes the board label of the pin. Pins are
@@ -56,10 +58,16 @@ The frequencies a pin type supports:
 
 The pin configuration is checked when 1 is written to ``live``. The
 write fails with ``EINVAL`` when a pin has a frequency its type does not
-support or when a ``mux`` pin is an output, and with ``ENOENT`` when a
-pin directory is being created or removed at the same time. An
-``EINVAL`` does not name the pin, the attributes of the pins show which
-one it is.
+support, when a ``mux`` pin is an output or when an output pin has a
+priority other than the default, and with ``ENOENT`` when a pin
+directory is being created or removed at the same time. An ``EINVAL``
+does not name the pin, the attributes of the pins show which one it is.
+
+Every live transition starts from the configured values. A priority set
+over netlink while the instance is live is reported back over netlink,
+it is not written to the ``prio`` attribute and is gone once the
+instance is taken down. The driver stores the priority, it does not
+select a reference by it.
 
 Example
 =======
@@ -73,8 +81,10 @@ output, then make it live::
     mkdir pins/gnss0
     echo gnss > pins/gnss0/type
     echo 1 > pins/gnss0/frequency
+    echo 0 > pins/gnss0/prio
     mkdir pins/ext0
     echo 10000000 > pins/ext0/frequency
+    echo 1 > pins/ext0/prio
     mkdir pins/out0
     echo 10000000 > pins/out0/frequency
     echo output > pins/out0/direction
diff --git a/drivers/dpll/dpllsim/core.c b/drivers/dpll/dpllsim/core.c
index 3d0fba45323b6a..c7e0a218000403 100644
--- a/drivers/dpll/dpllsim/core.c
+++ b/drivers/dpll/dpllsim/core.c
@@ -132,10 +132,40 @@ static ssize_t dpllsim_pin_frequency_store(struct config_item *item,
 }
 CONFIGFS_ATTR(dpllsim_pin_, frequency);
 
+static ssize_t dpllsim_pin_prio_show(struct config_item *item, char *page)
+{
+	struct dpllsim_pin *pin = to_dpllsim_pin(item);
+
+	guard(mutex)(&pin->dev->lock);
+	return sysfs_emit(page, "%u\n", pin->cfg_prio);
+}
+
+static ssize_t dpllsim_pin_prio_store(struct config_item *item,
+				      const char *page, size_t count)
+{
+	struct dpllsim_pin *pin = to_dpllsim_pin(item);
+	u32 val;
+	int err;
+
+	err = kstrtou32(page, 10, &val);
+	if (err)
+		return err;
+	if (val > DPLLSIM_PIN_PRIO_MAX)
+		return -EINVAL;
+
+	guard(mutex)(&pin->dev->lock);
+	if (dpllsim_dev_frozen(pin->dev))
+		return -EBUSY;
+	pin->cfg_prio = val;
+	return count;
+}
+CONFIGFS_ATTR(dpllsim_pin_, prio);
+
 static struct configfs_attribute *dpllsim_pin_attrs[] = {
 	&dpllsim_pin_attr_type,
 	&dpllsim_pin_attr_direction,
 	&dpllsim_pin_attr_frequency,
+	&dpllsim_pin_attr_prio,
 	NULL,
 };
 
@@ -178,6 +208,7 @@ static struct config_item *dpllsim_pins_make_item(struct config_group *group,
 	pin->dev = sim;
 	pin->type = DPLL_PIN_TYPE_EXT;
 	pin->direction = DPLL_PIN_DIRECTION_INPUT;
+	pin->cfg_prio = DPLLSIM_DEFAULT_PIN_PRIO;
 	list_add_tail(&pin->list, &sim->pin_list);
 
 	return &pin->item;
diff --git a/drivers/dpll/dpllsim/dpll.c b/drivers/dpll/dpllsim/dpll.c
index d838a2bbdf52f6..ecc8dd7af46ef3 100644
--- a/drivers/dpll/dpllsim/dpll.c
+++ b/drivers/dpll/dpllsim/dpll.c
@@ -139,11 +139,43 @@ static int dpllsim_pin_state_set(const struct dpll_pin *pin, void *pin_priv,
 	return 0;
 }
 
+static int dpllsim_pin_prio_get(const struct dpll_pin *pin, void *pin_priv,
+				const struct dpll_device *dpll,
+				void *dpll_priv, u32 *prio,
+				struct netlink_ext_ack *extack)
+{
+	struct dpllsim_pin *sim_pin = pin_priv;
+
+	guard(mutex)(&sim_pin->dev->lock);
+	*prio = sim_pin->prio;
+	return 0;
+}
+
+static int dpllsim_pin_prio_set(const struct dpll_pin *pin, void *pin_priv,
+				const struct dpll_device *dpll,
+				void *dpll_priv, u32 prio,
+				struct netlink_ext_ack *extack)
+{
+	struct dpllsim_pin *sim_pin = pin_priv;
+
+	if (prio > DPLLSIM_PIN_PRIO_MAX) {
+		NL_SET_ERR_MSG_FMT(extack, "Pin priority must be in range 0-%u",
+				   DPLLSIM_PIN_PRIO_MAX);
+		return -EINVAL;
+	}
+
+	guard(mutex)(&sim_pin->dev->lock);
+	sim_pin->prio = prio;
+	return 0;
+}
+
 static const struct dpll_pin_ops dpllsim_input_pin_ops = {
 	.frequency_get = dpllsim_pin_frequency_get,
 	.direction_get = dpllsim_pin_direction_get,
 	.state_on_dpll_get = dpllsim_pin_state_get,
 	.state_on_dpll_set = dpllsim_pin_state_set,
+	.prio_get = dpllsim_pin_prio_get,
+	.prio_set = dpllsim_pin_prio_set,
 };
 
 static const struct dpll_pin_ops dpllsim_output_pin_ops = {
@@ -188,7 +220,8 @@ static void dpllsim_pin_props(struct dpllsim_pin *pin,
 
 	/* Capabilities follow the ops the pin is served by */
 	if (dpllsim_pin_select_ops(pin) == &dpllsim_input_pin_ops)
-		prop->capabilities = DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE;
+		prop->capabilities = DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE |
+				     DPLL_PIN_CAPABILITIES_PRIORITY_CAN_CHANGE;
 	else
 		prop->capabilities = 0;
 }
@@ -214,10 +247,18 @@ static bool dpllsim_pin_direction_valid(const struct dpllsim_pin *pin)
 	       pin->direction == DPLL_PIN_DIRECTION_INPUT;
 }
 
+/* Priority is meaningful for input pins only */
+static bool dpllsim_pin_prio_valid(const struct dpllsim_pin *pin)
+{
+	return pin->direction == DPLL_PIN_DIRECTION_INPUT ||
+	       pin->cfg_prio == DPLLSIM_DEFAULT_PIN_PRIO;
+}
+
 static void dpllsim_pin_reset(struct dpllsim_pin *pin)
 {
 	pin->ops = dpllsim_pin_select_ops(pin);
 	pin->state = DPLL_PIN_STATE_DISCONNECTED;
+	pin->prio = pin->cfg_prio;
 }
 
 /* Build sim->pins in registration order, the order the pins were created */
@@ -228,7 +269,8 @@ static int dpllsim_order_pins(struct dpllsim_dev *sim)
 
 	list_for_each_entry(pin, &sim->pin_list, list) {
 		if (!dpllsim_pin_frequency_valid(pin) ||
-		    !dpllsim_pin_direction_valid(pin))
+		    !dpllsim_pin_direction_valid(pin) ||
+		    !dpllsim_pin_prio_valid(pin))
 			return -EINVAL;
 		count++;
 	}
diff --git a/drivers/dpll/dpllsim/dpllsim.h b/drivers/dpll/dpllsim/dpllsim.h
index af33afc70cb2ee..44afcd9918c400 100644
--- a/drivers/dpll/dpllsim/dpllsim.h
+++ b/drivers/dpll/dpllsim/dpllsim.h
@@ -12,6 +12,9 @@
 #include <linux/list.h>
 #include <linux/mutex.h>
 
+#define DPLLSIM_DEFAULT_PIN_PRIO		128
+#define DPLLSIM_PIN_PRIO_MAX			255
+
 #define DPLLSIM_MAX_PINS			128
 
 struct dpllsim_dev;
@@ -25,6 +28,7 @@ struct dpllsim_pin {
 	enum dpll_pin_type type;
 	enum dpll_pin_direction direction;
 	u64 frequency;
+	u32 cfg_prio;
 
 	/* Resolved at live=1 */
 	int idx;
@@ -35,6 +39,8 @@ struct dpllsim_pin {
 
 	/* Simulated state, guarded by dev->lock */
 	enum dpll_pin_state state;
+	/* Starts from cfg_prio at live=1, changed by the netlink setter */
+	u32 prio;
 };
 
 struct dpllsim_dev {
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH net-next v5 08/14] selftests: dpll: add pin priority test
  2026-11-25  9:05 [PATCH net-next v5 00/14] dpll: add a simulated DPLL device driver Petr Oros
                   ` (6 preceding siblings ...)
  2026-11-25  9:05 ` [PATCH net-next v5 07/14] dpll: dpllsim: Add support to get/set priority on input pins Petr Oros
@ 2026-11-25  9:05 ` Petr Oros
  2026-11-25  9:05 ` [PATCH net-next v5 09/14] dpll: dpllsim: Add support to set frequency on pins Petr Oros
                   ` (5 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: Petr Oros @ 2026-11-25  9:05 UTC (permalink / raw)
  To: netdev
  Cc: Petr Oros, Jonathan Corbet, Shuah Khan, Shuah Khan, Randy Dunlap,
	Jani Nikula, Vadim Fedorenko, Arkadiusz Kubalewski, Jiri Pirko,
	Ivan Vecera, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, linux-doc, linux-kselftest

Add a test that sets a new priority on an input pin, reads it back and
restores the original value. The range of priorities a device accepts is
not advertised over netlink, so the test tries the two values above the
current one first, none above 255 though, ice hands the priority to its
firmware as 8 bits and a larger value would turn into a small one that
ranks high. A lower value ranks higher, and in automatic mode a
selectable pin that outranks the reference takes over from it once its
signal qualifies, so one below the current value and the small values 1
and 7 are tried only when the selection does not consider the pin, in
manual mode or when the pin is disconnected. A driver may still refuse
all of the values for a constraint the interface does not tell, so that
is a skip on a device provided by the user, with the reason the tool
reported, and a failure on the device the script created, whose
simulator accepts every value the test tries. A current value the device
does not take back could not be restored, so the test writes it back
once before it touches the pin, which changes nothing, and ends there
when that is refused. The test skips when the dpll tool lacks the prio
keyword of pin set, and the restore is deferred right after that write
so that an interrupted run restores the value too. A pin that does not
advertise the priority-can-change capability or does not report a
priority is a skip on a device provided by the user and a failure on the
device the script created, whose input pins have both.

The dpllsim_add_pin helper gains an optional priority argument, which is
written only when it is given, so that a pin without one keeps the
default of the simulator. A priority is accepted for input pins only,
so the callers pass one for those alone.

The standard device lays its inputs out as gnss0 with priority 0 and
ext0 with priority 1, the plain input pin every stress device has gets
priority 1 as well and its extra input pins get random ones.

Signed-off-by: Petr Oros <poros@redhat.com>
---
v5:
- try the values above the current priority first and the ones that
  rank higher only when the pin is out of the selection, a selectable
  pin that outranks the reference could take over from it
- try no value above 255, ice truncates the priority to 8 bits and
  256 would become 0, the highest one
- fail when the device does not report its mode or the pin its state
- check that the pin is still spare before the test, and fail when
  its priority cannot be queried
- correct the commit message on the candidates and on the priorities
  of the stress devices
- drop the claim about ice and 255 from the changelog and say that it
  is the input pins of the created device that have a priority
- name the reason the tool reported when the restore fails
- report a value that cannot be read back as such, not as a wrong one
- add the priorities in dpllsim_add_standard_pins, the one copy of the
  standard layout now
- say in the changelog that the plain input pin of the stress devices
  gets priority 1
- skip the deferred restore once the test restored the priority itself,
  ice refuses any write while it resets and a refused repeat would fail
  the run
- leave the deferred restore out when nothing was changed, and take a
  refused restore of the test like the state test does, a skip when
  nothing failed before it, the deferred one tries again
- count the pin as changed from before the set of the new value, see
  patch 6
- say why the last two candidates are 1 and 7, both lie in the 0 to 14
  zl3073x accepts and one of them differs from the current value
- skip the test when the state read before the candidates is neither
  disconnected nor selectable, as the state test does, the selection
  may have taken the pin since the spare check
- the Reviewed-by and Acked-by of v4 are not carried over, the test
  changed

v4:
- write the current priority back before the pin is touched and end
  the test when the device refuses it, the pin could not be restored
- say that the simulator accepts every value the test tries, not every
  priority, and drop "active reference" from the layout comment
- tell a failed capability query from a missing capability and log a
  deferred restore that fails

v3:
- follow the move of the selftests to
  tools/testing/selftests/drivers/dpll

v4: https://lore.kernel.org/all/20261112130359.840795-9-poros@redhat.com/
v3: https://lore.kernel.org/all/20261109093523.931201-9-poros@redhat.com/
v2: https://lore.kernel.org/all/20261105151852.1844305-9-poros@redhat.com/
v1: https://lore.kernel.org/all/20261103093910.596225-9-poros@redhat.com/
---
 .../selftests/drivers/dpll/dpll_features.sh   | 133 ++++++++++++++++++
 .../selftests/drivers/dpll/dpll_lib.sh        |  32 ++++-
 .../selftests/drivers/dpll/dpll_stress.sh     |  11 +-
 3 files changed, 166 insertions(+), 10 deletions(-)

diff --git a/tools/testing/selftests/drivers/dpll/dpll_features.sh b/tools/testing/selftests/drivers/dpll/dpll_features.sh
index 61ebfd9e718922..34e6b28ee224c5 100755
--- a/tools/testing/selftests/drivers/dpll/dpll_features.sh
+++ b/tools/testing/selftests/drivers/dpll/dpll_features.sh
@@ -33,6 +33,7 @@ input_pins=()
 
 ALL_TESTS="
 	test_pin_state_transitions
+	test_pin_priority_get_set
 "
 
 ##############################################################################
@@ -130,6 +131,138 @@ pin_tune_spare()
 	return 1
 }
 
+test_pin_priority_get_set()
+{
+	local before target after reason candidate mode state
+	local -a candidates
+
+	RET=0
+
+	if [ -z "$PIN_TUNE_ID" ]; then
+		dpll_log_test_skip_or_fail "DPLL pin priority set/get" \
+		    "no spare input pin"
+		return
+	fi
+	if ! dpll_pin_set_supports prio; then
+		log_test_skip "DPLL pin priority set/get" \
+		    "dpll tool lacks pin set prio"
+		return
+	fi
+	pin_tune_capable priority-can-change "DPLL pin priority set/get" \
+			 "priority cannot change" || return
+	pin_tune_spare "DPLL pin priority set/get" || return
+
+	# A query that failed is not a pin without a priority.
+	if ! before=$(dpll_pin_get_prio "$DPLL_DEV_ID" "$PIN_TUNE_ID"); then
+		check_err 1 "pin $PIN_TUNE_ID query failed"
+		log_test "DPLL pin priority set/get"
+		return
+	fi
+	if [ -z "$before" ]; then
+		dpll_log_test_skip_or_fail "DPLL pin priority set/get" \
+		    "prio not reported"
+		return
+	fi
+	# A device may report a priority it does not accept back, one that
+	# marks an unused pin for instance, and the pin could not be
+	# restored then. Writing the current value changes nothing, so
+	# that is tried before the pin is touched.
+	if ! dpll_pin_set_prio "$DPLL_DEV_ID" "$PIN_TUNE_ID" "$before"; then
+		reason=${DPLL_LAST_ERR%%$'\n'*}
+		dpll_log_test_skip_or_fail "DPLL pin priority set/get" \
+		    "cannot write the current prio back${reason:+, $reason}"
+		return
+	fi
+	TUNE_CHANGED=0
+	defer tune_restore dpll_pin_set_prio "$DPLL_DEV_ID" "$PIN_TUNE_ID" \
+	      "$before"
+
+	# A lower value ranks higher, and in automatic mode a pin that is
+	# selectable and outranks the reference takes over from it once its
+	# signal qualifies. So the values above the current one are tried
+	# first, and the ones that rank higher only when the selection does
+	# not consider the pin, in manual mode or when it is disconnected.
+	# The accepted range is not advertised, a refusal of all of them is
+	# a skip on a device the user provided, the simulator accepts every
+	# value tried here. No value above 255 is tried, ice hands the
+	# priority to its firmware as 8 bits and a larger value would turn
+	# into a small one, which ranks high. The last two, 1 and 7, lie in
+	# the 0 to 14 zl3073x accepts, and one of them differs from the
+	# current value whatever it is.
+	mode=$(dpll_device_get_mode "$DPLL_DEV_ID")
+	state=$(dpll_pin_get_state "$DPLL_DEV_ID" "$PIN_TUNE_ID")
+	if [ -z "$mode" ] || [ -z "$state" ]; then
+		check_err 1 "device mode or pin state not reported"
+		log_test "DPLL pin priority set/get"
+		return
+	fi
+	# The selection may have taken the pin since pin_tune_spare
+	# looked, as in the state test, and a priority that ranks lower
+	# could make the device leave it for another input.
+	case "$state" in
+	disconnected|selectable)
+		;;
+	*)
+		dpll_log_test_skip_or_fail "DPLL pin priority set/get" \
+		    "the input pin is not spare any more"
+		return
+		;;
+	esac
+	candidates=("$((before + 1))" "$((before + 2))")
+	if [ "$mode" != automatic ] || [ "$state" = disconnected ]; then
+		candidates+=("$((before - 1))" 1 7)
+	fi
+	target=""
+	reason="no value left to try"
+	for candidate in "${candidates[@]}"; do
+		[ "$candidate" -ge 0 ] && [ "$candidate" -le 255 ] &&
+			[ "$candidate" != "$before" ] || continue
+		TUNE_CHANGED=1
+		if dpll_pin_set_prio "$DPLL_DEV_ID" "$PIN_TUNE_ID" \
+				     "$candidate"; then
+			target=$candidate
+			break
+		fi
+		TUNE_CHANGED=0
+		reason=${DPLL_LAST_ERR%%$'\n'*}
+	done
+	if [ -z "$target" ]; then
+		dpll_log_test_skip_or_fail "DPLL pin priority set/get" \
+		    "cannot set a new prio${reason:+, $reason}"
+		return
+	fi
+	after=$(dpll_pin_get_prio "$DPLL_DEV_ID" "$PIN_TUNE_ID")
+	if [ -z "$after" ]; then
+		check_err 1 "cannot read the priority back"
+	elif [ "$after" != "$target" ]; then
+		check_err 1 "priority expected $target, got '$after'"
+	fi
+
+	# A refused restore is taken like in the state test, the deferred
+	# one tries again.
+	if ! dpll_pin_set_prio "$DPLL_DEV_ID" "$PIN_TUNE_ID" "$before"; then
+		reason=${DPLL_LAST_ERR%%$'\n'*}
+		reason=${reason:+, $reason}
+		if [ "$RET" -eq 0 ]; then
+			dpll_log_test_skip_or_fail \
+			    "DPLL pin priority set/get" \
+			    "cannot restore pin priority$reason"
+			return
+		fi
+		check_err 1 "cannot restore pin priority$reason"
+	else
+		TUNE_CHANGED=0
+	fi
+	after=$(dpll_pin_get_prio "$DPLL_DEV_ID" "$PIN_TUNE_ID")
+	if [ -z "$after" ]; then
+		check_err 1 "cannot read the restored priority back"
+	elif [ "$after" != "$before" ]; then
+		check_err 1 "priority not restored, got '$after'"
+	fi
+
+	log_test "DPLL pin priority set/get"
+}
+
 test_pin_state_transitions()
 {
 	local mode before state after reason may_pick changed
diff --git a/tools/testing/selftests/drivers/dpll/dpll_lib.sh b/tools/testing/selftests/drivers/dpll/dpll_lib.sh
index 6a80177e1e0306..b67dba21ba9b4b 100644
--- a/tools/testing/selftests/drivers/dpll/dpll_lib.sh
+++ b/tools/testing/selftests/drivers/dpll/dpll_lib.sh
@@ -462,6 +462,11 @@ dpll_pin_has_capability()
 	   <<< "$out" &> /dev/null || return 1
 }
 
+dpll_pin_get_prio()
+{
+	__dpll_pin_dev_field "$1" "$2" '.prio'
+}
+
 ##############################################################################
 # Pin setters
 #
@@ -501,6 +506,16 @@ dpll_pin_set_state()
 			     state "$state" 2>&1 >/dev/null)
 }
 
+dpll_pin_set_prio()
+{
+	local dev_id=$1
+	local pin_id=$2
+	local prio=$3
+
+	DPLL_LAST_ERR=$(dpll pin set id "$pin_id" parent-device "$dev_id" \
+			     prio "$prio" 2>&1 >/dev/null)
+}
+
 ##############################################################################
 # dpllsim configfs interface
 #
@@ -620,7 +635,9 @@ dpllsim_create_device()
 
 # $1: device name, $2: pin name (becomes the board-label), $3: type (gnss,
 # int-oscillator, ext, synce-eth-port, mux), $4: frequency in Hz,
-# $5: direction (input/output)
+# $5: direction (input/output), $6: prio (optional, input pins only).
+# The priority is written only when one is given, so a pin without one
+# keeps the default of the simulator.
 dpllsim_add_pin()
 {
 	local dev=$1
@@ -628,13 +645,16 @@ dpllsim_add_pin()
 	local type=$3
 	local frequency=$4
 	local direction=$5
+	local prio=${6:-}
 	local pin_path="$DPLLSIM_CFG/$dev/pins/$pin"
 
 	mkdir "$pin_path" 2>/dev/null || return 1
 
 	if ! echo "$type" 2>/dev/null > "$pin_path/type" ||
 	   ! echo "$frequency" 2>/dev/null > "$pin_path/frequency" ||
-	   ! echo "$direction" 2>/dev/null > "$pin_path/direction"; then
+	   ! echo "$direction" 2>/dev/null > "$pin_path/direction" ||
+	   { [ -n "$prio" ] &&
+	     ! echo "$prio" 2>/dev/null > "$pin_path/prio"; }; then
 		rmdir "$pin_path" 2>/dev/null
 		return 1
 	fi
@@ -702,16 +722,16 @@ dpllsim_alloc_clock_id()
 # not live. Return 1 when a pin cannot be added.
 dpllsim_add_standard_pins()
 {
-	dpllsim_add_pin "$1" gnss0 gnss 1 input &&
-	dpllsim_add_pin "$1" ext0 ext 10000000 input &&
+	dpllsim_add_pin "$1" gnss0 gnss 1 input 0 &&
+	dpllsim_add_pin "$1" ext0 ext 10000000 input 1 &&
 	dpllsim_add_pin "$1" out0 ext 10000000 output
 }
 
 # Create a device with the standard 3-pin layout, deploy it and resolve
 # the netlink device id. Sets DPLLSIM_CREATED_NAME, DPLLSIM_CLOCK_ID and
 # DPLLSIM_DEV_ID.
-#   gnss0: GNSS, 1 Hz, input
-#   ext0: EXT, 10 MHz, input
+#   gnss0: GNSS, 1 Hz, input, prio 0
+#   ext0: EXT, 10 MHz, input, prio 1
 #   out0: EXT, 10 MHz, output
 dpllsim_setup_standard_device()
 {
diff --git a/tools/testing/selftests/drivers/dpll/dpll_stress.sh b/tools/testing/selftests/drivers/dpll/dpll_stress.sh
index 3c859825750974..1596c3f1e70b55 100755
--- a/tools/testing/selftests/drivers/dpll/dpll_stress.sh
+++ b/tools/testing/selftests/drivers/dpll/dpll_stress.sh
@@ -78,7 +78,7 @@ create_stress_device()
 	local clock_id=$1
 	local num_pins=$2
 	local name="st$clock_id"
-	local i type freq dir
+	local i type freq dir prio
 	local dirs=(input output)
 
 	if ! dpllsim_create_device "$name" "$clock_id"; then
@@ -87,7 +87,7 @@ create_stress_device()
 	fi
 
 	# Always have at least one plain input pin, then random ones
-	if ! dpllsim_add_pin "$name" ext0 ext 10000000 input; then
+	if ! dpllsim_add_pin "$name" ext0 ext 10000000 input 1; then
 		dpllsim_delete_device "$name" || track_device "$name"
 		return 1
 	fi
@@ -98,8 +98,11 @@ create_stress_device()
 		# output one
 		dir=${dirs[RANDOM % 2]}
 		[ "$type" = mux ] && dir=input
-		if ! dpllsim_add_pin "$name" "pin$i" "$type" "$freq" \
-				     "$dir"; then
+		# Only input pins accept a priority
+		prio=""
+		[ "$dir" = input ] && prio=$((RANDOM % 128))
+		if ! dpllsim_add_pin "$name" "pin$i" "$type" "$freq" "$dir" \
+				     "$prio"; then
 			dpllsim_delete_device "$name" || track_device "$name"
 			return 1
 		fi
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH net-next v5 09/14] dpll: dpllsim: Add support to set frequency on pins
  2026-11-25  9:05 [PATCH net-next v5 00/14] dpll: add a simulated DPLL device driver Petr Oros
                   ` (7 preceding siblings ...)
  2026-11-25  9:05 ` [PATCH net-next v5 08/14] selftests: dpll: add pin priority test Petr Oros
@ 2026-11-25  9:05 ` Petr Oros
  2026-11-25  9:05 ` [PATCH net-next v5 10/14] selftests: dpll: add pin frequency test Petr Oros
                   ` (4 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: Petr Oros @ 2026-11-25  9:05 UTC (permalink / raw)
  To: netdev
  Cc: Petr Oros, Jonathan Corbet, Shuah Khan, Shuah Khan, Randy Dunlap,
	Jani Nikula, Vadim Fedorenko, Arkadiusz Kubalewski, Jiri Pirko,
	Ivan Vecera, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, linux-doc, linux-kselftest

Add support to set the frequency of input and output pins. The DPLL
core validates the requested value against the supported frequency
list the pin advertises, so the frequency_set callback only has to
store it. The same per type tables that validate the configured
frequency at live time serve as the supported list. The configfs
attribute is rejected with EBUSY while the device is live, so only the
netlink setter changes the frequency of a registered pin and the two
cannot race. The frequency set over netlink is kept apart from the
configured one, like the priority, so the attribute keeps showing what
was configured and every live transition starts from it.

Signed-off-by: Petr Oros <poros@redhat.com>
---
v5:
- no changes

v4:
- keep the frequency set over netlink apart from the configured one,
  like the priority
- document that values set over netlink are not written back to the
  attributes
- the Reviewed-by and Acked-by of v3 are not carried over, the
  behaviour of the driver changed

v4: https://lore.kernel.org/all/20261112130359.840795-10-poros@redhat.com/
v3: https://lore.kernel.org/all/20261109093523.931201-10-poros@redhat.com/
---
 Documentation/driver-api/dpllsim.rst | 10 +++++-----
 drivers/dpll/dpllsim/core.c          |  4 ++--
 drivers/dpll/dpllsim/dpll.c          | 20 ++++++++++++++++++--
 drivers/dpll/dpllsim/dpllsim.h       |  8 ++++++--
 4 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/Documentation/driver-api/dpllsim.rst b/Documentation/driver-api/dpllsim.rst
index ce71c8bad6b375..c36a35f0b39fa8 100644
--- a/Documentation/driver-api/dpllsim.rst
+++ b/Documentation/driver-api/dpllsim.rst
@@ -63,11 +63,11 @@ priority other than the default, and with ``ENOENT`` when a pin
 directory is being created or removed at the same time. An ``EINVAL``
 does not name the pin, the attributes of the pins show which one it is.
 
-Every live transition starts from the configured values. A priority set
-over netlink while the instance is live is reported back over netlink,
-it is not written to the ``prio`` attribute and is gone once the
-instance is taken down. The driver stores the priority, it does not
-select a reference by it.
+Every live transition starts from the configured values. A frequency or
+a priority set over netlink while the instance is live is reported back
+over netlink, it is not written to the ``frequency`` or ``prio``
+attribute and is gone once the instance is taken down. The driver
+stores the priority, it does not select a reference by it.
 
 Example
 =======
diff --git a/drivers/dpll/dpllsim/core.c b/drivers/dpll/dpllsim/core.c
index c7e0a218000403..795d4b8e98e911 100644
--- a/drivers/dpll/dpllsim/core.c
+++ b/drivers/dpll/dpllsim/core.c
@@ -110,7 +110,7 @@ static ssize_t dpllsim_pin_frequency_show(struct config_item *item, char *page)
 	struct dpllsim_pin *pin = to_dpllsim_pin(item);
 
 	guard(mutex)(&pin->dev->lock);
-	return sysfs_emit(page, "%llu\n", pin->frequency);
+	return sysfs_emit(page, "%llu\n", pin->cfg_frequency);
 }
 
 static ssize_t dpllsim_pin_frequency_store(struct config_item *item,
@@ -127,7 +127,7 @@ static ssize_t dpllsim_pin_frequency_store(struct config_item *item,
 	guard(mutex)(&pin->dev->lock);
 	if (dpllsim_dev_frozen(pin->dev))
 		return -EBUSY;
-	pin->frequency = val;
+	pin->cfg_frequency = val;
 	return count;
 }
 CONFIGFS_ATTR(dpllsim_pin_, frequency);
diff --git a/drivers/dpll/dpllsim/dpll.c b/drivers/dpll/dpllsim/dpll.c
index ecc8dd7af46ef3..3c6ce1a64217dc 100644
--- a/drivers/dpll/dpllsim/dpll.c
+++ b/drivers/dpll/dpllsim/dpll.c
@@ -79,6 +79,19 @@ static int dpllsim_pin_frequency_get(const struct dpll_pin *pin,
 	return 0;
 }
 
+static int dpllsim_pin_frequency_set(const struct dpll_pin *pin,
+				     void *pin_priv,
+				     const struct dpll_device *dpll,
+				     void *dpll_priv, u64 frequency,
+				     struct netlink_ext_ack *extack)
+{
+	struct dpllsim_pin *sim_pin = pin_priv;
+
+	guard(mutex)(&sim_pin->dev->lock);
+	sim_pin->frequency = frequency;
+	return 0;
+}
+
 static int dpllsim_pin_direction_get(const struct dpll_pin *pin,
 				     void *pin_priv,
 				     const struct dpll_device *dpll,
@@ -171,6 +184,7 @@ static int dpllsim_pin_prio_set(const struct dpll_pin *pin, void *pin_priv,
 
 static const struct dpll_pin_ops dpllsim_input_pin_ops = {
 	.frequency_get = dpllsim_pin_frequency_get,
+	.frequency_set = dpllsim_pin_frequency_set,
 	.direction_get = dpllsim_pin_direction_get,
 	.state_on_dpll_get = dpllsim_pin_state_get,
 	.state_on_dpll_set = dpllsim_pin_state_set,
@@ -180,6 +194,7 @@ static const struct dpll_pin_ops dpllsim_input_pin_ops = {
 
 static const struct dpll_pin_ops dpllsim_output_pin_ops = {
 	.frequency_get = dpllsim_pin_frequency_get,
+	.frequency_set = dpllsim_pin_frequency_set,
 	.direction_get = dpllsim_pin_direction_get,
 	.state_on_dpll_get = dpllsim_pin_state_get,
 };
@@ -233,8 +248,8 @@ static bool dpllsim_pin_frequency_valid(struct dpllsim_pin *pin)
 
 	dpllsim_pin_props(pin, &prop);
 	for (i = 0; i < prop.freq_supported_num; i++)
-		if (pin->frequency >= prop.freq_supported[i].min &&
-		    pin->frequency <= prop.freq_supported[i].max)
+		if (pin->cfg_frequency >= prop.freq_supported[i].min &&
+		    pin->cfg_frequency <= prop.freq_supported[i].max)
 			return true;
 
 	return false;
@@ -258,6 +273,7 @@ static void dpllsim_pin_reset(struct dpllsim_pin *pin)
 {
 	pin->ops = dpllsim_pin_select_ops(pin);
 	pin->state = DPLL_PIN_STATE_DISCONNECTED;
+	pin->frequency = pin->cfg_frequency;
 	pin->prio = pin->cfg_prio;
 }
 
diff --git a/drivers/dpll/dpllsim/dpllsim.h b/drivers/dpll/dpllsim/dpllsim.h
index 44afcd9918c400..fc5eef0324f56f 100644
--- a/drivers/dpll/dpllsim/dpllsim.h
+++ b/drivers/dpll/dpllsim/dpllsim.h
@@ -27,7 +27,7 @@ struct dpllsim_pin {
 	/* Configuration, writable only until dpllsim_dev_frozen() */
 	enum dpll_pin_type type;
 	enum dpll_pin_direction direction;
-	u64 frequency;
+	u64 cfg_frequency;
 	u32 cfg_prio;
 
 	/* Resolved at live=1 */
@@ -39,7 +39,11 @@ struct dpllsim_pin {
 
 	/* Simulated state, guarded by dev->lock */
 	enum dpll_pin_state state;
-	/* Starts from cfg_prio at live=1, changed by the netlink setter */
+	/*
+	 * Start from cfg_frequency and cfg_prio at live=1, changed by the
+	 * netlink setters
+	 */
+	u64 frequency;
 	u32 prio;
 };
 
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH net-next v5 10/14] selftests: dpll: add pin frequency test
  2026-11-25  9:05 [PATCH net-next v5 00/14] dpll: add a simulated DPLL device driver Petr Oros
                   ` (8 preceding siblings ...)
  2026-11-25  9:05 ` [PATCH net-next v5 09/14] dpll: dpllsim: Add support to set frequency on pins Petr Oros
@ 2026-11-25  9:05 ` Petr Oros
  2026-11-25  9:05 ` [PATCH net-next v5 11/14] dpll: dpllsim: Add support to adjust phase Petr Oros
                   ` (3 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: Petr Oros @ 2026-11-25  9:05 UTC (permalink / raw)
  To: netdev
  Cc: Petr Oros, Ivan Vecera, Jiri Pirko, Jonathan Corbet, Shuah Khan,
	Shuah Khan, Randy Dunlap, Jani Nikula, Vadim Fedorenko,
	Arkadiusz Kubalewski, Jiri Pirko, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, linux-doc,
	linux-kselftest

Add a test that picks a supported frequency other than the current one
from the frequency-supported list of an input pin, sets it, reads it
back and restores the original value. Both ends of every advertised
range are candidates, so that a pin advertising a single range wider
than one value still has one. On a device provided by the user the
candidate closest to the current frequency is taken, an end of a wide
range is far from what the input carries. It skips when the dpll tool
lacks the frequency keyword of pin set, and the restore is deferred
before the pin is changed so that an interrupted run restores it too.
The core refuses a frequency outside the supported ranges, so a pin
whose current frequency is not in them could not be put back, zl3073x
reports a sub-Hz reference as 0 Hz and does not offer that. The test
bails out on such a pin before it touches it. The frequency belongs to
the pin and not to one of its parent devices, which is why the pin the
script picks must not be the reference of any of them. A pin that
reports no frequency or advertises a single frequency only is a skip on
a device provided by the user and a failure on the device the script
created, whose ext pins advertise several, the gnss pin advertises 1 Hz
only.

The driver may refuse the candidate. For a driver without a frequency
setter the core refuses every supported value with EOPNOTSUPP, the
current one included, so the test writes that back before it defers the
restore and ends there when it is refused. A driver with constraints of
its own refuses values the advertised ranges allow. On a device provided
by the user a refused set is a skip that names the reason the tool
reported, which the setter keeps in DPLL_LAST_ERR, on the created device
the simulator accepts every advertised value and the refusal is a
failure. A refused restore is taken as in the state test, the deferred
one tries again.

Reviewed-by: Ivan Vecera <ivecera@redhat.com>
Acked-by: Jiri Pirko <jiri@nvidia.com>
Signed-off-by: Petr Oros <poros@redhat.com>
---
v5:
- on a device provided by the user take the supported frequency
  closest to the current one, an end of a wide range is far from what
  the input carries
- check that the pin is still spare before the test, and write the
  current frequency back before the restore is deferred
- correct the commit message, the divisibility rule of zl3073x is for
  output pins and the gnss pin of the simulator has one frequency
- say in the changelog that it is the core that refuses the values for
  a driver without a setter, and drop that case from the comment on a
  refused candidate, it ends at the write back now
- name the reason the tool reported when the restore fails
- report a value that cannot be read back as such, not as a wrong one
- a pin of a connected reference sync pair is not taken any more, see
  patch 6
- skip the deferred restore once the test restored the frequency
  itself, a refused repeat would fail the run
- leave the deferred restore out when nothing was changed, and take a
  refused restore of the test like the state test does, a skip when
  nothing failed before it, the deferred one tries again
- count the pin as changed from before the set of the new value, see
  patch 6
- keep the keys frequency-supported, frequency-min and frequency-max in
  the jq filter, every dpll release prints them and the netlink spec
  names them so
- say in the description that a refused restore is taken as in the
  state test, it still called it a failure on either device

v4:
- bail out before touching a pin whose current frequency is not among
  the supported ones, it could not be restored
- tell a failed query from a pin without a frequency, check that the
  value is a number and log a deferred restore that fails
- name the arguments of dpll_pin_set_frequency like the other setters

v3:
- follow the move of the selftests to
  tools/testing/selftests/drivers/dpll

v4: https://lore.kernel.org/all/20261112130359.840795-11-poros@redhat.com/
v3: https://lore.kernel.org/all/20261109093523.931201-11-poros@redhat.com/
v2: https://lore.kernel.org/all/20261105151852.1844305-11-poros@redhat.com/
v1: https://lore.kernel.org/all/20261103093910.596225-11-poros@redhat.com/
---
 .../selftests/drivers/dpll/dpll_features.sh   | 127 ++++++++++++++++++
 .../selftests/drivers/dpll/dpll_lib.sh        |  10 ++
 2 files changed, 137 insertions(+)

diff --git a/tools/testing/selftests/drivers/dpll/dpll_features.sh b/tools/testing/selftests/drivers/dpll/dpll_features.sh
index 34e6b28ee224c5..acaf61b36624c1 100755
--- a/tools/testing/selftests/drivers/dpll/dpll_features.sh
+++ b/tools/testing/selftests/drivers/dpll/dpll_features.sh
@@ -34,6 +34,7 @@ input_pins=()
 ALL_TESTS="
 	test_pin_state_transitions
 	test_pin_priority_get_set
+	test_pin_frequency_set_get
 "
 
 ##############################################################################
@@ -455,6 +456,132 @@ test_pin_state_transitions()
 	log_test "DPLL pin state transitions"
 }
 
+test_pin_frequency_set_get()
+{
+	local before target after reason json near=true
+
+	RET=0
+
+	if [ -z "$PIN_TUNE_ID" ]; then
+		dpll_log_test_skip_or_fail "DPLL pin frequency set/get" \
+		    "no spare input pin"
+		return
+	fi
+	if ! dpll_pin_set_supports frequency; then
+		log_test_skip "DPLL pin frequency set/get" \
+		    "dpll tool lacks pin set frequency"
+		return
+	fi
+	pin_tune_spare "DPLL pin frequency set/get" || return
+
+	# A query that failed is not a pin without a frequency.
+	if ! before=$(dpll_pin_get_frequency "$PIN_TUNE_ID"); then
+		check_err 1 "pin $PIN_TUNE_ID query failed"
+		log_test "DPLL pin frequency set/get"
+		return
+	fi
+	if [ -z "$before" ]; then
+		dpll_log_test_skip_or_fail "DPLL pin frequency set/get" \
+		    "frequency not reported"
+		return
+	fi
+	if ! [[ "$before" =~ ^[0-9]+$ ]]; then
+		check_err 1 "frequency '$before' is not a number"
+		log_test "DPLL pin frequency set/get"
+		return
+	fi
+	if ! json=$(dpll_pin_json "$PIN_TUNE_ID"); then
+		check_err 1 "pin $PIN_TUNE_ID query failed"
+		log_test "DPLL pin frequency set/get"
+		return
+	fi
+	# The core refuses a frequency outside the supported ranges, the
+	# current one included, and a driver may report one it does not
+	# offer, zl3073x reads a sub-Hz reference back as 0 Hz. Such a pin
+	# could not be restored, so bail out before it is touched.
+	if ! jq -e --argjson cur "$before" \
+		'any(.["frequency-supported"][]?;
+		     .["frequency-min"] <= $cur and
+		     .["frequency-max"] >= $cur)' <<< "$json" &> /dev/null; then
+		dpll_log_test_skip_or_fail "DPLL pin frequency set/get" \
+		    "current frequency is not a supported one"
+		return
+	fi
+	# A driver without a frequency setter refuses the current value as
+	# well, writing it changes nothing otherwise. The restore is
+	# deferred once that went through.
+	if ! dpll_pin_set_frequency "$PIN_TUNE_ID" "$before"; then
+		reason=${DPLL_LAST_ERR%%$'\n'*}
+		dpll_log_test_skip_or_fail "DPLL pin frequency set/get" \
+		    "cannot write the frequency back${reason:+, $reason}"
+		return
+	fi
+	TUNE_CHANGED=0
+	defer tune_restore dpll_pin_set_frequency "$PIN_TUNE_ID" "$before"
+
+	# Pick a supported frequency different from the current one. Both
+	# ends of every supported range are candidates, so a pin with a
+	# single range still has an alternative unless it is one value wide.
+	# On hardware the one closest to the current frequency is taken, an
+	# end of a wide range is far from what the input carries, the
+	# simulator is checked with the first one.
+	[ -n "$DPLL_CREATED_DEVICE" ] && near=false
+	target=$(jq -r --argjson cur "$before" --argjson near "$near" \
+		    '[.["frequency-supported"][]? |
+		      .["frequency-min"], .["frequency-max"] |
+		      numbers | select(. != $cur)] |
+		     if $near then min_by(if . > $cur then . - $cur
+					  else $cur - . end)
+		     else .[0] end // empty' <<< "$json")
+	if [ -z "$target" ]; then
+		dpll_log_test_skip_or_fail "DPLL pin frequency set/get" \
+		    "no alternative supported frequency"
+		return
+	fi
+
+	# A driver with constraints of its own may refuse the candidate, a
+	# skip on a device the user provided, the simulator accepts every
+	# advertised value.
+	TUNE_CHANGED=1
+	if ! dpll_pin_set_frequency "$PIN_TUNE_ID" "$target"; then
+		TUNE_CHANGED=0
+		reason=${DPLL_LAST_ERR%%$'\n'*}
+		dpll_log_test_skip_or_fail "DPLL pin frequency set/get" \
+		    "cannot set frequency=$target${reason:+, $reason}"
+		return
+	fi
+	after=$(dpll_pin_get_frequency "$PIN_TUNE_ID")
+	if [ -z "$after" ]; then
+		check_err 1 "cannot read the frequency back"
+	elif [ "$after" != "$target" ]; then
+		check_err 1 "frequency expected $target, got '$after'"
+	fi
+
+	# A refused restore is taken like in the state test, the deferred
+	# one tries again.
+	if ! dpll_pin_set_frequency "$PIN_TUNE_ID" "$before"; then
+		reason=${DPLL_LAST_ERR%%$'\n'*}
+		reason=${reason:+, $reason}
+		if [ "$RET" -eq 0 ]; then
+			dpll_log_test_skip_or_fail \
+			    "DPLL pin frequency set/get" \
+			    "cannot restore frequency$reason"
+			return
+		fi
+		check_err 1 "cannot restore frequency$reason"
+	else
+		TUNE_CHANGED=0
+	fi
+	after=$(dpll_pin_get_frequency "$PIN_TUNE_ID")
+	if [ -z "$after" ]; then
+		check_err 1 "cannot read the restored frequency back"
+	elif [ "$after" != "$before" ]; then
+		check_err 1 "frequency not restored, got '$after'"
+	fi
+
+	log_test "DPLL pin frequency set/get"
+}
+
 ##############################################################################
 
 cleanup()
diff --git a/tools/testing/selftests/drivers/dpll/dpll_lib.sh b/tools/testing/selftests/drivers/dpll/dpll_lib.sh
index b67dba21ba9b4b..3930f51a87d618 100644
--- a/tools/testing/selftests/drivers/dpll/dpll_lib.sh
+++ b/tools/testing/selftests/drivers/dpll/dpll_lib.sh
@@ -516,6 +516,16 @@ dpll_pin_set_prio()
 			     prio "$prio" 2>&1 >/dev/null)
 }
 
+# $2: frequency in Hz
+dpll_pin_set_frequency()
+{
+	local pin_id=$1
+	local frequency=$2
+
+	DPLL_LAST_ERR=$(dpll pin set id "$pin_id" frequency "$frequency" \
+			     2>&1 >/dev/null)
+}
+
 ##############################################################################
 # dpllsim configfs interface
 #
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH net-next v5 11/14] dpll: dpllsim: Add support to adjust phase
  2026-11-25  9:05 [PATCH net-next v5 00/14] dpll: add a simulated DPLL device driver Petr Oros
                   ` (9 preceding siblings ...)
  2026-11-25  9:05 ` [PATCH net-next v5 10/14] selftests: dpll: add pin frequency test Petr Oros
@ 2026-11-25  9:05 ` Petr Oros
  2026-11-25  9:05 ` [PATCH net-next v5 12/14] selftests: dpll: add pin phase adjust test Petr Oros
                   ` (2 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: Petr Oros @ 2026-11-25  9:05 UTC (permalink / raw)
  To: netdev
  Cc: Petr Oros, Ivan Vecera, Jiri Pirko, Jonathan Corbet, Shuah Khan,
	Shuah Khan, Randy Dunlap, Jani Nikula, Vadim Fedorenko,
	Arkadiusz Kubalewski, Jiri Pirko, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, linux-doc,
	linux-kselftest

Add support to get and set the phase adjustment of input and output
pins. The pins advertise a granularity of 100 ps and a symmetric range
whose ends are the largest multiples of the granularity that fit a
32-bit signed value, so that the advertised ends themselves are
settable, the core validates the requested value against both and the
driver stores it. The adjustment is reset to zero every time the device
goes live, so that repeated deploy cycles start from a known state. It
becomes the baseline of the simulated phase offset measurements once
those exist. The documentation of the simulator describes the range, the
granularity and the reset, and says that a pin state set over netlink is
not kept across a live transition either.

Reviewed-by: Ivan Vecera <ivecera@redhat.com>
Acked-by: Jiri Pirko <jiri@nvidia.com>
Signed-off-by: Petr Oros <poros@redhat.com>
---
v5:
- describe the phase adjust range, its granularity and its reset in
  the documentation, and that a pin state set over netlink is not
  kept across a live transition either
- give the phase adjust its own comment in the pin structure, it
  starts from 0 and has no configured value
- split the documentation sentence on values set over netlink, only an
  input pin takes a state and neither the state nor a phase adjust has
  a configfs attribute

v4: https://lore.kernel.org/all/20261112130359.840795-12-poros@redhat.com/
v3: https://lore.kernel.org/all/20261109093523.931201-12-poros@redhat.com/
v2: https://lore.kernel.org/all/20261105151852.1844305-12-poros@redhat.com/
v1: https://lore.kernel.org/all/20261103093910.596225-12-poros@redhat.com/
---
 Documentation/driver-api/dpllsim.rst | 13 ++++++---
 drivers/dpll/dpllsim/dpll.c          | 40 ++++++++++++++++++++++++++++
 drivers/dpll/dpllsim/dpllsim.h       |  2 ++
 3 files changed, 51 insertions(+), 4 deletions(-)

diff --git a/Documentation/driver-api/dpllsim.rst b/Documentation/driver-api/dpllsim.rst
index c36a35f0b39fa8..f8c357de78d9e0 100644
--- a/Documentation/driver-api/dpllsim.rst
+++ b/Documentation/driver-api/dpllsim.rst
@@ -63,11 +63,16 @@ priority other than the default, and with ``ENOENT`` when a pin
 directory is being created or removed at the same time. An ``EINVAL``
 does not name the pin, the attributes of the pins show which one it is.
 
-Every live transition starts from the configured values. A frequency or
-a priority set over netlink while the instance is live is reported back
+Every live transition starts from the configured values, and every pin
+starts disconnected and with a phase adjust of 0. A frequency or a
+priority set over netlink while the instance is live is reported back
 over netlink, it is not written to the ``frequency`` or ``prio``
-attribute and is gone once the instance is taken down. The driver
-stores the priority, it does not select a reference by it.
+attribute and is gone once the instance is taken down. The same holds
+for the state of an input pin and for a phase adjust set over netlink,
+neither has an attribute. The driver stores the priority, it does not
+select a reference by it.
+Input and output pins accept a phase adjust in steps of 100 ps between
+-2147483600 and 2147483600 ps.
 
 Example
 =======
diff --git a/drivers/dpll/dpllsim/dpll.c b/drivers/dpll/dpllsim/dpll.c
index 3c6ce1a64217dc..90150301805a45 100644
--- a/drivers/dpll/dpllsim/dpll.c
+++ b/drivers/dpll/dpllsim/dpll.c
@@ -12,6 +12,11 @@
 
 #include "dpllsim.h"
 
+#define DPLLSIM_PHASE_ADJUST_GRAN		100	/* ps */
+/* Largest multiple of the granularity, so the range ends are settable */
+#define DPLLSIM_PHASE_ADJUST_MAX \
+	(S32_MAX - S32_MAX % DPLLSIM_PHASE_ADJUST_GRAN)
+
 static struct dpll_pin_frequency dpllsim_freq_1pps[] = {
 	DPLL_PIN_FREQUENCY_RANGE(1, 1),
 };
@@ -182,6 +187,33 @@ static int dpllsim_pin_prio_set(const struct dpll_pin *pin, void *pin_priv,
 	return 0;
 }
 
+static int dpllsim_pin_phase_adjust_get(const struct dpll_pin *pin,
+					void *pin_priv,
+					const struct dpll_device *dpll,
+					void *dpll_priv, s32 *phase_adjust,
+					struct netlink_ext_ack *extack)
+{
+	struct dpllsim_pin *sim_pin = pin_priv;
+
+	guard(mutex)(&sim_pin->dev->lock);
+	*phase_adjust = sim_pin->phase_adjust;
+	return 0;
+}
+
+static int dpllsim_pin_phase_adjust_set(const struct dpll_pin *pin,
+					void *pin_priv,
+					const struct dpll_device *dpll,
+					void *dpll_priv,
+					const s32 phase_adjust,
+					struct netlink_ext_ack *extack)
+{
+	struct dpllsim_pin *sim_pin = pin_priv;
+
+	guard(mutex)(&sim_pin->dev->lock);
+	sim_pin->phase_adjust = phase_adjust;
+	return 0;
+}
+
 static const struct dpll_pin_ops dpllsim_input_pin_ops = {
 	.frequency_get = dpllsim_pin_frequency_get,
 	.frequency_set = dpllsim_pin_frequency_set,
@@ -190,6 +222,8 @@ static const struct dpll_pin_ops dpllsim_input_pin_ops = {
 	.state_on_dpll_set = dpllsim_pin_state_set,
 	.prio_get = dpllsim_pin_prio_get,
 	.prio_set = dpllsim_pin_prio_set,
+	.phase_adjust_get = dpllsim_pin_phase_adjust_get,
+	.phase_adjust_set = dpllsim_pin_phase_adjust_set,
 };
 
 static const struct dpll_pin_ops dpllsim_output_pin_ops = {
@@ -197,6 +231,8 @@ static const struct dpll_pin_ops dpllsim_output_pin_ops = {
 	.frequency_set = dpllsim_pin_frequency_set,
 	.direction_get = dpllsim_pin_direction_get,
 	.state_on_dpll_get = dpllsim_pin_state_get,
+	.phase_adjust_get = dpllsim_pin_phase_adjust_get,
+	.phase_adjust_set = dpllsim_pin_phase_adjust_set,
 };
 
 static const struct dpll_pin_ops *
@@ -212,6 +248,9 @@ static void dpllsim_pin_props(struct dpllsim_pin *pin,
 {
 	prop->type = pin->type;
 	prop->board_label = config_item_name(&pin->item);
+	prop->phase_range.min = -DPLLSIM_PHASE_ADJUST_MAX;
+	prop->phase_range.max = DPLLSIM_PHASE_ADJUST_MAX;
+	prop->phase_gran = DPLLSIM_PHASE_ADJUST_GRAN;
 
 	switch (pin->type) {
 	case DPLL_PIN_TYPE_GNSS:
@@ -275,6 +314,7 @@ static void dpllsim_pin_reset(struct dpllsim_pin *pin)
 	pin->state = DPLL_PIN_STATE_DISCONNECTED;
 	pin->frequency = pin->cfg_frequency;
 	pin->prio = pin->cfg_prio;
+	pin->phase_adjust = 0;
 }
 
 /* Build sim->pins in registration order, the order the pins were created */
diff --git a/drivers/dpll/dpllsim/dpllsim.h b/drivers/dpll/dpllsim/dpllsim.h
index fc5eef0324f56f..0443f148d83a56 100644
--- a/drivers/dpll/dpllsim/dpllsim.h
+++ b/drivers/dpll/dpllsim/dpllsim.h
@@ -45,6 +45,8 @@ struct dpllsim_pin {
 	 */
 	u64 frequency;
 	u32 prio;
+	/* Starts from 0 at live=1, changed by the netlink setter */
+	s32 phase_adjust;
 };
 
 struct dpllsim_dev {
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH net-next v5 12/14] selftests: dpll: add pin phase adjust test
  2026-11-25  9:05 [PATCH net-next v5 00/14] dpll: add a simulated DPLL device driver Petr Oros
                   ` (10 preceding siblings ...)
  2026-11-25  9:05 ` [PATCH net-next v5 11/14] dpll: dpllsim: Add support to adjust phase Petr Oros
@ 2026-11-25  9:05 ` Petr Oros
  2026-11-25  9:05 ` [PATCH net-next v5 13/14] selftests: dpll: add concurrency stress tests Petr Oros
  2026-11-25  9:05 ` [PATCH net-next v5 14/14] selftests: dpll: add reference leak tests Petr Oros
  13 siblings, 0 replies; 15+ messages in thread
From: Petr Oros @ 2026-11-25  9:05 UTC (permalink / raw)
  To: netdev
  Cc: Petr Oros, Ivan Vecera, Jiri Pirko, Jonathan Corbet, Shuah Khan,
	Shuah Khan, Randy Dunlap, Jani Nikula, Vadim Fedorenko,
	Arkadiusz Kubalewski, Jiri Pirko, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, linux-doc,
	linux-kselftest

Add a test that reads the phase adjustment range and granularity a pin
advertises, picks another value, sets it, reads it back and restores the
original value. On the device the script created that is the largest
multiple of the granularity inside the range, or the smallest one when
that is already the current value. On a device provided by the user it
is the value one granularity step away from the current one, the end of
the range can be a large phase step there, 2.1 ms on zl3073x. The test
skips when the dpll tool lacks the phase-adjust keyword of pin set. A
pin that reports no phase adjustment, no range or a range too narrow to
pick a different value is a skip on a device provided by the user and a
failure on the device the script created, whose pins report a wide
range.

The core refuses a value outside the advertised range and one that is
not a multiple of the granularity a driver reports, the current value
included, so a pin that sits at such a value could not be put back. The
test bails out on either before it touches the pin. It then writes the
current value back, which changes nothing and which the core refuses
with EOPNOTSUPP for a driver without a setter, and defers the restore
once that went through, so that an interrupted run restores the value
too.

The granularity is only reported by drivers that have the core enforce
one. Without it the target on the created device is computed with a
granularity of 1 and the step on hardware is 1 ns, and a driver may
refuse it for a granularity of its own or for any other constraint the
interface does not advertise. A refused set is therefore a skip on a
device provided by the user, with the reason the tool reported, and a
failure on the device the script created, whose simulator accepts every
value its range and granularity allow. The test ends there, a value that
was not set has nothing to read back.

Reviewed-by: Ivan Vecera <ivecera@redhat.com>
Acked-by: Jiri Pirko <jiri@nvidia.com>
Signed-off-by: Petr Oros <poros@redhat.com>
---
v5:
- on a device provided by the user step from the current phase adjust
  by the granularity or by 1 ns, the end of the range is a phase step
  of 2.1 ms on zl3073x
- check that the pin is still spare before the test
- write the current phase adjust back before the restore is deferred,
  like the priority and the frequency tests do
- name the reason the tool reported when the restore fails
- report a value that cannot be read back as such, not as a wrong one
- fail on the created device when the range is too narrow to pick
  another value, a skip there would hide a regression of the simulator
- skip the deferred restore once the test restored the phase adjust
  itself, a refused repeat would fail the run
- leave the deferred restore out when nothing was changed, and take a
  refused restore of the test like the state test does, a skip when
  nothing failed before it, the deferred one tries again
- count the pin as changed from before the set of the new value, see
  patch 6
- the 2.1 ms of zl3073x in the description is S32_MAX ps, the end of
  the phase adjust range the driver advertises

v4:
- bail out when the current value lies outside the advertised range,
  it could not be restored
- tell a failed query from a pin without a phase adjust or a range and
  log a deferred restore that fails

v3:
- follow the move of the selftests to
  tools/testing/selftests/drivers/dpll

v4: https://lore.kernel.org/all/20261112130359.840795-13-poros@redhat.com/
v3: https://lore.kernel.org/all/20261109093523.931201-13-poros@redhat.com/
v2: https://lore.kernel.org/all/20261105151852.1844305-13-poros@redhat.com/
v1: https://lore.kernel.org/all/20261103093910.596225-13-poros@redhat.com/
---
 .../selftests/drivers/dpll/dpll_features.sh   | 144 ++++++++++++++++++
 .../selftests/drivers/dpll/dpll_lib.sh        |  15 ++
 2 files changed, 159 insertions(+)

diff --git a/tools/testing/selftests/drivers/dpll/dpll_features.sh b/tools/testing/selftests/drivers/dpll/dpll_features.sh
index acaf61b36624c1..ebcd0d09293b7d 100755
--- a/tools/testing/selftests/drivers/dpll/dpll_features.sh
+++ b/tools/testing/selftests/drivers/dpll/dpll_features.sh
@@ -35,6 +35,7 @@ ALL_TESTS="
 	test_pin_state_transitions
 	test_pin_priority_get_set
 	test_pin_frequency_set_get
+	test_pin_phase_adjust_set_get
 "
 
 ##############################################################################
@@ -456,6 +457,149 @@ test_pin_state_transitions()
 	log_test "DPLL pin state transitions"
 }
 
+test_pin_phase_adjust_set_get()
+{
+	local before target after json min max gran step reason
+
+	RET=0
+
+	if [ -z "$PIN_TUNE_ID" ]; then
+		dpll_log_test_skip_or_fail "DPLL pin phase-adjust set/get" \
+		    "no spare input pin"
+		return
+	fi
+	if ! dpll_pin_set_supports phase-adjust; then
+		log_test_skip "DPLL pin phase-adjust set/get" \
+		    "dpll tool lacks pin set phase-adjust"
+		return
+	fi
+	pin_tune_spare "DPLL pin phase-adjust set/get" || return
+
+	# A query that failed is not a pin without a phase adjust.
+	if ! before=$(dpll_pin_get_phase_adjust "$PIN_TUNE_ID") ||
+	   ! json=$(dpll_pin_json "$PIN_TUNE_ID"); then
+		check_err 1 "pin $PIN_TUNE_ID query failed"
+		log_test "DPLL pin phase-adjust set/get"
+		return
+	fi
+	if [ -z "$before" ]; then
+		dpll_log_test_skip_or_fail "DPLL pin phase-adjust set/get" \
+		    "phase adjust not reported"
+		return
+	fi
+	if ! [[ "$before" =~ ^-?[0-9]+$ ]]; then
+		check_err 1 "phase adjust '$before' is not a number"
+		log_test "DPLL pin phase-adjust set/get"
+		return
+	fi
+
+	# Pick a target inside the advertised range different from the
+	# current value and a multiple of the granularity.
+	min=$(jq -r '.["phase-adjust-min"] // empty' <<< "$json")
+	max=$(jq -r '.["phase-adjust-max"] // empty' <<< "$json")
+	gran=$(jq -r '.["phase-adjust-gran"] // empty' <<< "$json")
+	if [ -z "$min" ] || [ -z "$max" ]; then
+		dpll_log_test_skip_or_fail "DPLL pin phase-adjust set/get" \
+		    "no adjust range"
+		return
+	fi
+	# The core refuses a value outside the range or off the
+	# granularity, the current one included. A pin that sits at such a
+	# value could not be put back, so bail out before the restore is
+	# scheduled.
+	if [ "$before" -lt "$min" ] || [ "$before" -gt "$max" ]; then
+		dpll_log_test_skip_or_fail "DPLL pin phase-adjust set/get" \
+		    "current phase adjust outside the adjust range"
+		return
+	fi
+	if [ -n "$gran" ] && [ $((before % gran)) -ne 0 ]; then
+		dpll_log_test_skip_or_fail "DPLL pin phase-adjust set/get" \
+		    "current phase adjust not a granularity multiple"
+		return
+	fi
+	# For a driver without a phase adjust setter the core refuses the
+	# current value as well, writing it changes nothing otherwise. The
+	# restore is deferred once that went through.
+	if ! dpll_pin_set_phase_adjust "$PIN_TUNE_ID" "$before"; then
+		reason=${DPLL_LAST_ERR%%$'\n'*}
+		dpll_log_test_skip_or_fail "DPLL pin phase-adjust set/get" \
+		    "cannot write the phase adjust back${reason:+, $reason}"
+		return
+	fi
+	TUNE_CHANGED=0
+	defer tune_restore dpll_pin_set_phase_adjust "$PIN_TUNE_ID" "$before"
+
+	if [ -n "$DPLL_CREATED_DEVICE" ]; then
+		# The simulator is checked at the ends of its range. Shell
+		# division truncates towards zero, so round the bounds towards
+		# the inside of the range explicitly, which matters for
+		# negative ones.
+		[ -n "$gran" ] || gran=1
+		target=$((max / gran * gran))
+		[ "$target" -gt "$max" ] && target=$((target - gran))
+		if [ "$target" = "$before" ]; then
+			target=$((min / gran * gran))
+			[ "$target" -lt "$min" ] && target=$((target + gran))
+		fi
+	else
+		# On hardware the end of the range can be a large phase step,
+		# 2.1 ms on zl3073x, which the outputs see once the pin becomes
+		# the reference. Step by the granularity, or by 1 ns when the
+		# driver reports none, towards the inside of the range.
+		step=${gran:-1000}
+		target=$((before + step))
+		[ "$target" -gt "$max" ] && target=$((before - step))
+	fi
+	if [ "$target" = "$before" ] || [ "$target" -gt "$max" ] ||
+	   [ "$target" -lt "$min" ]; then
+		dpll_log_test_skip_or_fail "DPLL pin phase-adjust set/get" \
+		    "range too narrow"
+		return
+	fi
+
+	# A driver may enforce a granularity it does not report or another
+	# constraint, a skip on a device the user provided, the simulator
+	# accepts every value its range and granularity allow.
+	TUNE_CHANGED=1
+	if ! dpll_pin_set_phase_adjust "$PIN_TUNE_ID" "$target"; then
+		TUNE_CHANGED=0
+		reason=${DPLL_LAST_ERR%%$'\n'*}
+		dpll_log_test_skip_or_fail "DPLL pin phase-adjust set/get" \
+		    "cannot set phase-adjust=$target${reason:+, $reason}"
+		return
+	fi
+	after=$(dpll_pin_get_phase_adjust "$PIN_TUNE_ID")
+	if [ -z "$after" ]; then
+		check_err 1 "cannot read the phase adjust back"
+	elif [ "$after" != "$target" ]; then
+		check_err 1 "phase-adjust expected $target, got '$after'"
+	fi
+
+	# A refused restore is taken like in the state test, the deferred
+	# one tries again.
+	if ! dpll_pin_set_phase_adjust "$PIN_TUNE_ID" "$before"; then
+		reason=${DPLL_LAST_ERR%%$'\n'*}
+		reason=${reason:+, $reason}
+		if [ "$RET" -eq 0 ]; then
+			dpll_log_test_skip_or_fail \
+			    "DPLL pin phase-adjust set/get" \
+			    "cannot restore phase-adjust$reason"
+			return
+		fi
+		check_err 1 "cannot restore phase-adjust$reason"
+	else
+		TUNE_CHANGED=0
+	fi
+	after=$(dpll_pin_get_phase_adjust "$PIN_TUNE_ID")
+	if [ -z "$after" ]; then
+		check_err 1 "cannot read the restored phase adjust back"
+	elif [ "$after" != "$before" ]; then
+		check_err 1 "phase-adjust not restored, got '$after'"
+	fi
+
+	log_test "DPLL pin phase-adjust set/get"
+}
+
 test_pin_frequency_set_get()
 {
 	local before target after reason json near=true
diff --git a/tools/testing/selftests/drivers/dpll/dpll_lib.sh b/tools/testing/selftests/drivers/dpll/dpll_lib.sh
index 3930f51a87d618..11152d958bca9d 100644
--- a/tools/testing/selftests/drivers/dpll/dpll_lib.sh
+++ b/tools/testing/selftests/drivers/dpll/dpll_lib.sh
@@ -467,6 +467,11 @@ dpll_pin_get_prio()
 	__dpll_pin_dev_field "$1" "$2" '.prio'
 }
 
+dpll_pin_get_phase_adjust()
+{
+	__dpll_pin_field "$1" '.["phase-adjust"]'
+}
+
 ##############################################################################
 # Pin setters
 #
@@ -526,6 +531,16 @@ dpll_pin_set_frequency()
 			     2>&1 >/dev/null)
 }
 
+# $2: phase adjust in ps
+dpll_pin_set_phase_adjust()
+{
+	local pin_id=$1
+	local phase_adjust=$2
+
+	DPLL_LAST_ERR=$(dpll pin set id "$pin_id" phase-adjust \
+			     "$phase_adjust" 2>&1 >/dev/null)
+}
+
 ##############################################################################
 # dpllsim configfs interface
 #
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH net-next v5 13/14] selftests: dpll: add concurrency stress tests
  2026-11-25  9:05 [PATCH net-next v5 00/14] dpll: add a simulated DPLL device driver Petr Oros
                   ` (11 preceding siblings ...)
  2026-11-25  9:05 ` [PATCH net-next v5 12/14] selftests: dpll: add pin phase adjust test Petr Oros
@ 2026-11-25  9:05 ` Petr Oros
  2026-11-25  9:05 ` [PATCH net-next v5 14/14] selftests: dpll: add reference leak tests Petr Oros
  13 siblings, 0 replies; 15+ messages in thread
From: Petr Oros @ 2026-11-25  9:05 UTC (permalink / raw)
  To: netdev
  Cc: Petr Oros, Ivan Vecera, Jiri Pirko, Jonathan Corbet, Shuah Khan,
	Shuah Khan, Randy Dunlap, Jani Nikula, Vadim Fedorenko,
	Arkadiusz Kubalewski, Jiri Pirko, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, linux-doc,
	linux-kselftest

Extend the stress script with tests that register and unregister
devices from several processes at once and that keep a number of
devices with many pins live at the same time, checking that every one
of them is visible on netlink with all its pins.

The workers run in subshells, so the parent cannot track the device a
worker has in flight. Each worker removes it from its own exit trap
instead, and the parent reserves a range of clock IDs for every worker,
so that the devices of a worker cannot collide with those of another
worker or of the parent and whatever a killed worker left behind can be
removed by name, at the end of the test and from the cleanup handler. A
range in which a device of such a name exists already, left behind by an
earlier run, is not handed to a worker, and a worker clears the name of
the device of the parent it inherits, so that neither is removed with
the devices of the worker. A worker whose devices cannot all be removed
is a failure and stays recorded for the cleanup handler. A helper that a
worker stuck in the kernel runs, the mkdir of a device for one, can
outlive the forced kill of the worker, and what it creates afterwards
stays behind. The handler stops the recorded workers together with the
jobs of the shell, gives them a few seconds to run their exit traps
before forcing the survivors, and goes on without a worker that does not
die, since one stuck in the kernel would hang the teardown. A worker
that a signal interrupts exits with the status of the signal, inside a
trap $? is the status of whatever ran last and the worker could pass for
one that finished. bash reaps a worker as soon as it exits, so a
recorded pid may have been handed out again by the time the handler
runs, and it signals a pid only while that is still a child of the
script. Deleting a device treats a part of it that is gone by the time
its step runs as removed, so that a removal running concurrently with
the one of a worker does not turn into a spurious failure. The workers
inherit the open lock file of the library, so the lock is held until the
script and every worker have exited.

Reviewed-by: Ivan Vecera <ivecera@redhat.com>
Acked-by: Jiri Pirko <jiri@nvidia.com>
Signed-off-by: Petr Oros <poros@redhat.com>
---
v5:
- say in the commit message that what a stuck helper creates after the
  forced kill stays behind, as the comment in the code does
- do not hand a worker a clock-id range in which a device of this
  name exists already, clear the inherited name of the device of the
  parent in the worker, and keep a worker whose devices could not be
  removed recorded and fail the test
- ignore further signals in the TERM and INT handlers of the workers
  as well, and remove the device in flight from the handlers, a signal
  that came while a worker exited ended its exit trap
- drop the reseeding of RANDOM in the workers, bash does that for a
  subshell, and a sentence about the jobs builtin that does not hold
- keep the name prefix of the stress devices in one place
- say in a comment that the INT trap of a worker takes effect, bash
  starts it with INT ignored but lets it trap the signal
- report a failed pin dump of the many devices test once
- say that the range allocator must not be called from a subshell
- skip the concurrency and many devices tests when their counts are
  not positive numbers, and the many devices test when
  STRESS_PINS_PER_DEVICE is above 128
- check the clock-ids of a worker against every device in the dpllsim
  directory, by name and by clock-id, not only against the names of its
  own devices
- a device the many devices test fails to create and cannot remove is
  tracked for the cleanup handler now, see patch 2
- call the clock-id check of the library by its public name, see patch 2
- move the range of a worker past every taken one as the allocator of
  the library does for a clock-id, the test failed after three ranges
  that devices of earlier runs held

v4:
- signal a recorded pid only while it is still a running child of the
  script, bash reaps finished workers on its own and the pid may have
  been reused
- rename the "maximum devices" test to "many devices" and check that
  every device shows all its pins
- say what happens to a helper of a worker that is stuck in the kernel
- exit a signalled worker with the status of the signal, it could pass
  for one that finished

v3:
- follow the move of the selftests to
  tools/testing/selftests/drivers/dpll

v2:
- quote the failed and ret expansions (netdev CI shellcheck)

v4: https://lore.kernel.org/all/20261112130359.840795-14-poros@redhat.com/
v3: https://lore.kernel.org/all/20261109093523.931201-14-poros@redhat.com/
v2: https://lore.kernel.org/all/20261105151852.1844305-14-poros@redhat.com/
v1: https://lore.kernel.org/all/20261103093910.596225-14-poros@redhat.com/
---
 .../selftests/drivers/dpll/dpll_lib.sh        |  30 +-
 .../selftests/drivers/dpll/dpll_stress.sh     | 289 +++++++++++++++++-
 2 files changed, 310 insertions(+), 9 deletions(-)

diff --git a/tools/testing/selftests/drivers/dpll/dpll_lib.sh b/tools/testing/selftests/drivers/dpll/dpll_lib.sh
index 11152d958bca9d..6f07837a7d9deb 100644
--- a/tools/testing/selftests/drivers/dpll/dpll_lib.sh
+++ b/tools/testing/selftests/drivers/dpll/dpll_lib.sh
@@ -62,8 +62,9 @@ dpll_lib_check_prereqs()
 # DPLL_LOCK_FILE before it mounts or creates anything and skips when
 # another run holds it. A run of the feature tests against a device the
 # user provided takes it as well, two of them would change the same
-# pins. The file lives under /run rather than in the world-writable
-# /tmp, where a planted symlink could redirect the open.
+# pins. The workers a script forks inherit the open file and keep the
+# lock until they exit. The file lives under /run rather than in the
+# world-writable /tmp, where a planted symlink could redirect the open.
 DPLL_RUN_DIR="/run/dpll-selftest"
 DPLL_LOCK_FILE="$DPLL_RUN_DIR/lock"
 DPLL_LOCK_FD=""
@@ -697,8 +698,12 @@ dpllsim_undeploy()
 
 # Take the device offline and remove it together with all its pins.
 # Every step is attempted even when an earlier one failed, the return
-# value is non-zero when any did. A device that does not exist counts as
-# deleted.
+# value is non-zero when any did. A device that does not exist counts
+# as deleted, and so does a part of it that is gone by the time its
+# step runs, the live file of the undeploy and the pin or device
+# directory of an rmdir, so that a removal running concurrently, or an
+# earlier one whose later steps failed, does not turn into a spurious
+# failure here.
 dpllsim_delete_device()
 {
 	local dev_path="$DPLLSIM_CFG/$1"
@@ -706,12 +711,12 @@ dpllsim_delete_device()
 
 	[ -d "$dev_path" ] || return 0
 
-	dpllsim_undeploy "$1" || ret=1
+	dpllsim_undeploy "$1" || [ ! -e "$dev_path/live" ] || ret=1
 	for pin_path in "$dev_path"/pins/*/; do
 		[ -d "$pin_path" ] || continue
-		rmdir "$pin_path" || ret=1
+		rmdir "$pin_path" || [ ! -d "$pin_path" ] || ret=1
 	done
-	rmdir "$dev_path" || ret=1
+	rmdir "$dev_path" || [ ! -d "$dev_path" ] || ret=1
 
 	return "$ret"
 }
@@ -743,6 +748,17 @@ dpllsim_alloc_clock_id()
 	DPLLSIM_CLOCK_ID=$((DPLLSIM_NEXT_CLOCK_ID++))
 }
 
+# Set DPLLSIM_CLOCK_ID to the first of $1 consecutive clock-ids unique
+# within this test run and move the allocator past them, for a worker
+# that derives the clock-ids of its devices from it. A device the parent
+# creates afterwards cannot collide with one of the worker. Must not be
+# called from a subshell, the move of the allocator would be lost.
+dpllsim_alloc_clock_id_range()
+{
+	DPLLSIM_CLOCK_ID=$DPLLSIM_NEXT_CLOCK_ID
+	DPLLSIM_NEXT_CLOCK_ID=$((DPLLSIM_NEXT_CLOCK_ID + $1))
+}
+
 # Add the pins of the standard layout, see below, to a device that is
 # not live. Return 1 when a pin cannot be added.
 dpllsim_add_standard_pins()
diff --git a/tools/testing/selftests/drivers/dpll/dpll_stress.sh b/tools/testing/selftests/drivers/dpll/dpll_stress.sh
index 1596c3f1e70b55..20b3132e8a3db0 100755
--- a/tools/testing/selftests/drivers/dpll/dpll_stress.sh
+++ b/tools/testing/selftests/drivers/dpll/dpll_stress.sh
@@ -13,14 +13,27 @@ source "$SCRIPTDIR/dpll_lib.sh"
 
 ALL_TESTS="
 	test_rapid_create_delete
+	test_concurrent_workers
 	test_pin_state_flooding
+	test_many_devices
 	test_deploy_cycle
 "
 
 STRESS_ITERATIONS=${STRESS_ITERATIONS:-20}
 STRESS_STATE_CHANGES=${STRESS_STATE_CHANGES:-50}
+STRESS_DEVICES=${STRESS_DEVICES:-5}
+# The simulator accepts up to 128 pins per device (DPLLSIM_MAX_PINS)
+STRESS_PINS_PER_DEVICE=${STRESS_PINS_PER_DEVICE:-10}
+STRESS_CONCURRENT_PROCS=${STRESS_CONCURRENT_PROCS:-4}
+STRESS_WORKER_ITERATIONS=${STRESS_WORKER_ITERATIONS:-10}
 
 CREATED_DEVICES=()
+BACKGROUND_PIDS=()
+# Prefix of the name of a stress device, the clock-id follows
+STRESS_DEV_PREFIX="st"
+# Base clock-ids of the workers in flight, their devices are named
+# $STRESS_DEV_PREFIX<base + iteration>
+WORKER_BASES=()
 
 track_device()
 {
@@ -38,18 +51,129 @@ untrack_device()
 	CREATED_DEVICES=("${remaining[@]}")
 }
 
+# Remove whatever a worker started from the given base clock-id left
+# behind, which is the device in flight when the worker had to be killed.
+# A device that does not exist counts as deleted. Return 1 when a device
+# could not be removed.
+delete_worker_devices()
+{
+	local base=$1
+	local i name ret=0
+
+	for i in $(seq 1 "$STRESS_WORKER_ITERATIONS"); do
+		name="$STRESS_DEV_PREFIX$((base + i))"
+		if ! dpllsim_delete_device "$name"; then
+			log_info "cannot delete device $name"
+			ret=1
+		fi
+	done
+	return "$ret"
+}
+
+# Return 0 when no device left behind by an earlier run uses one of the
+# clock-ids a worker started from the given base clock-id would take.
+# Such a device is not this run's, it must not be removed with the
+# devices of the worker, and it may hold the clock-id, see
+# dpllsim_alloc_clock_id.
+worker_names_free()
+{
+	local base=$1
+	local i
+
+	for i in $(seq 1 "$STRESS_WORKER_ITERATIONS"); do
+		dpllsim_clock_id_taken $((base + i)) && return 1
+	done
+	return 0
+}
+
+# Return 0 while the process is a running child of this shell. bash
+# reaps a background child as soon as it exits and only keeps its status
+# for a later wait, so the pid of a worker that is gone can be handed
+# out again. A process that reused it has another parent, which is how
+# it is told apart, and it must never be signalled. A child that has
+# exited but was not reaped yet is a zombie, state Z in
+# /proc/<pid>/stat, and counts as gone, or a wait on it would spin until
+# the forced kill.
+pid_running()
+{
+	local stat state ppid
+
+	# The state and the parent pid follow the parenthesized command
+	# name, which may contain spaces and parentheses. The ".*" is
+	# greedy, so the expression strips everything up to the last ") "
+	# of the line, and a ")" inside the name is harmless.
+	stat=$(sed 's/^.*) //' "/proc/$1/stat" 2>/dev/null) || return 1
+	read -r state ppid _ <<< "$stat"
+	[ "$ppid" = "$$" ] && [ "$state" != "Z" ]
+}
+
+# Return 0 once none of the given processes runs any more, 1 when some
+# still does after about five seconds.
+pids_gone()
+{
+	local i pid alive
+
+	for i in $(seq 1 50); do
+		alive=0
+		for pid in "$@"; do
+			pid_running "$pid" && alive=1
+		done
+		[ "$alive" -eq 0 ] && return 0
+		sleep 0.1
+	done
+	return 1
+}
+
 cleanup()
 {
 	# A second signal must not abort the teardown half way.
 	trap '' INT TERM
 
-	local dev
+	local pid dev base
+	local pids=() shell_jobs=()
+
+	# A worker forked right before the signal arrived may not be
+	# recorded yet, so the jobs of the shell are taken as well.
+	mapfile -t shell_jobs < <(jobs -p)
+	mapfile -t pids < <(printf '%s\n' "${BACKGROUND_PIDS[@]}" \
+				   "${shell_jobs[@]}" | grep . | sort -u)
+	# Only a process that is still a running child of this shell is
+	# signalled, see pid_running, a recorded pid may have been reused
+	# since its worker finished.
+	for pid in "${pids[@]}"; do
+		pid_running "$pid" && kill "$pid" 2>/dev/null
+	done
+	# Give the workers a few seconds to run their exit traps, then
+	# force the survivors and give those a few seconds to die. A
+	# worker stuck in the kernel does not die of the forced kill
+	# before whatever it waits for returns, and neither does a helper
+	# it runs, the mkdir of a device for one, which outlives the
+	# worker then. So only the workers that are gone are waited for
+	# and the teardown goes on without the rest. The removal of what
+	# such a worker or helper left behind can then fail or race with
+	# it, which is logged like any other failed removal, and what a
+	# stuck helper creates afterwards stays behind.
+	pids_gone "${pids[@]}"
+	for pid in "${pids[@]}"; do
+		pid_running "$pid" && kill -KILL "$pid" 2>/dev/null
+	done
+	pids_gone "${pids[@]}"
+	for pid in "${pids[@]}"; do
+		if pid_running "$pid"; then
+			log_info "worker $pid did not exit, giving up"
+		else
+			wait "$pid" 2>/dev/null
+		fi
+	done
 
 	defer_scopes_cleanup
 	for dev in "${CREATED_DEVICES[@]}"; do
 		dpllsim_delete_device "$dev" ||
 			log_info "cannot delete device $dev"
 	done
+	for base in "${WORKER_BASES[@]}"; do
+		delete_worker_devices "$base"
+	done
 	# The device whose creation a signal interrupted is recorded by the
 	# library alone, a double delete of a tracked one is harmless.
 	if [ -n "$DPLLSIM_CREATED_NAME" ]; then
@@ -77,7 +201,7 @@ create_stress_device()
 {
 	local clock_id=$1
 	local num_pins=$2
-	local name="st$clock_id"
+	local name="$STRESS_DEV_PREFIX$clock_id"
 	local i type freq dir prio
 	local dirs=(input output)
 
@@ -161,6 +285,110 @@ test_rapid_create_delete()
 	log_test "DPLL rapid create/delete ($STRESS_ITERATIONS iterations)"
 }
 
+# The exit trap of a worker. The worker runs in a subshell, so
+# track_device() cannot reach CREATED_DEVICES of the parent. Remove the
+# device in flight from the exit trap instead, or killing the worker
+# leaks it. The trap saves the exit status first, so that the removal
+# cannot mask a failed iteration. The handlers of TERM and INT pass it the
+# status a shell reports for the signal, inside a trap $? would be the
+# status of whatever ran last and an interrupted worker could pass for one
+# that finished. They ignore any further signal first, or a second one
+# would end the removal half way, and call it themselves, a signal that
+# comes while the worker exits is handled before the exit trap gets to the
+# removal, and the exit of the handler would end the exit trap there.
+worker_exit()
+{
+	local status=${1:-$?}
+
+	trap - EXIT
+	trap '' TERM INT
+	if [ -n "$DPLLSIM_CREATED_NAME" ]; then
+		dpllsim_delete_device "$DPLLSIM_CREATED_NAME"
+		DPLLSIM_CREATED_NAME=""
+	fi
+	exit "$status"
+}
+
+# Create and delete STRESS_WORKER_ITERATIONS devices with clock-ids
+# following the given base, in a subshell. Each worker derives its own
+# unique clock-ids from a range reserved for it, so its devices can never
+# be confused with those of another worker or of the parent. The parent
+# records the base of every worker, so that the device a killed worker
+# left behind can be removed by name.
+concurrent_worker()
+{
+	local base=$1
+	local i name failed=0
+
+	# The name of a device of the parent is inherited and must not be
+	# removed by the exit trap of the worker.
+	DPLLSIM_CREATED_NAME=""
+	trap worker_exit EXIT
+	# bash starts a worker with INT ignored, as it does every
+	# asynchronous command without job control, but lets the worker trap
+	# it. Only a signal ignored when the script started cannot be.
+	trap 'trap "" TERM INT; worker_exit 143' TERM
+	trap 'trap "" TERM INT; worker_exit 130' INT
+
+	for i in $(seq 1 "$STRESS_WORKER_ITERATIONS"); do
+		if ! create_stress_device $((base + i)) 3; then
+			failed=$((failed + 1))
+			continue
+		fi
+		name=$DPLLSIM_CREATED_NAME
+		dpllsim_delete_device "$name" ||
+			failed=$((failed + 1))
+	done
+
+	[ "$failed" -eq 0 ]
+}
+
+test_concurrent_workers()
+{
+	local pid base failed=0
+	local -a remaining=()
+
+	RET=0
+
+	stress_counts_valid \
+	    "DPLL concurrent workers ($STRESS_CONCURRENT_PROCS processes)" \
+	    STRESS_CONCURRENT_PROCS STRESS_WORKER_ITERATIONS || return
+
+	for _ in $(seq 1 "$STRESS_CONCURRENT_PROCS"); do
+		# Devices an earlier run left behind may hold clock-ids, the
+		# range moves past them as dpllsim_alloc_clock_id does for a
+		# single one. There are only so many, a free range comes.
+		while :; do
+			dpllsim_alloc_clock_id_range \
+				$((STRESS_WORKER_ITERATIONS + 1))
+			base=$DPLLSIM_CLOCK_ID
+			worker_names_free "$base" && break
+		done
+		WORKER_BASES+=("$base")
+		concurrent_worker "$base" &
+		BACKGROUND_PIDS+=($!)
+	done
+
+	for pid in "${BACKGROUND_PIDS[@]}"; do
+		wait "$pid" || failed=1
+	done
+	BACKGROUND_PIDS=()
+
+	check_err "$failed" "some concurrent workers failed"
+
+	# A worker whose devices could not all be removed stays recorded,
+	# so that the cleanup handler tries again.
+	for base in "${WORKER_BASES[@]}"; do
+		if ! delete_worker_devices "$base"; then
+			check_err 1 "cannot delete the devices of worker $base"
+			remaining+=("$base")
+		fi
+	done
+	WORKER_BASES=("${remaining[@]}")
+
+	log_test "DPLL concurrent workers ($STRESS_CONCURRENT_PROCS processes)"
+}
+
 test_pin_state_flooding()
 {
 	local clock_id name dev_id pin_ids pin_id i last state fails=0
@@ -231,6 +459,63 @@ test_pin_state_flooding()
 	log_test "DPLL pin state flooding"
 }
 
+test_many_devices()
+{
+	local i clock_id created=0 visible dev_id pin_ids
+	local test_devs=() clock_ids=()
+
+	RET=0
+
+	stress_counts_valid "DPLL many devices ($STRESS_DEVICES devices)" \
+	    STRESS_DEVICES STRESS_PINS_PER_DEVICE || return
+	if [ "$STRESS_PINS_PER_DEVICE" -gt 128 ]; then
+		log_test_skip "DPLL many devices ($STRESS_DEVICES devices)" \
+			      "STRESS_PINS_PER_DEVICE is above 128"
+		return
+	fi
+
+	for i in $(seq 1 "$STRESS_DEVICES"); do
+		dpllsim_alloc_clock_id; clock_id=$DPLLSIM_CLOCK_ID
+		create_stress_device "$clock_id" \
+				     "$STRESS_PINS_PER_DEVICE" || break
+		track_device "$DPLLSIM_CREATED_NAME"
+		test_devs+=("$DPLLSIM_CREATED_NAME")
+		clock_ids+=("$clock_id")
+		created=$((created + 1))
+	done
+
+	if [ "$created" -lt "$STRESS_DEVICES" ]; then
+		check_err 1 "created only $created of $STRESS_DEVICES devices"
+	fi
+
+	# Every created device must be visible on netlink with all its
+	# pins.
+	visible=0
+	for clock_id in "${clock_ids[@]}"; do
+		dev_id=$(dpll_device_wait_by_clock_id "$clock_id" dpllsim) ||
+			continue
+		visible=$((visible + 1))
+		if ! dpll_pin_ids_read pin_ids dpll_pin_get_ids "$dev_id"; then
+			check_err 1 "pin dump failed"
+		elif [ "${#pin_ids[@]}" -ne "$STRESS_PINS_PER_DEVICE" ]; then
+			check_err 1 "device $dev_id has ${#pin_ids[@]} pins"
+		fi
+	done
+	if [ "$visible" -ne "$created" ]; then
+		check_err 1 "only $visible of $created devices visible"
+	fi
+
+	for i in "${test_devs[@]}"; do
+		if dpllsim_delete_device "$i"; then
+			untrack_device "$i"
+		else
+			check_err 1 "cannot delete device $i"
+		fi
+	done
+
+	log_test "DPLL many devices ($STRESS_DEVICES devices)"
+}
+
 test_deploy_cycle()
 {
 	local clock_id name i
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH net-next v5 14/14] selftests: dpll: add reference leak tests
  2026-11-25  9:05 [PATCH net-next v5 00/14] dpll: add a simulated DPLL device driver Petr Oros
                   ` (12 preceding siblings ...)
  2026-11-25  9:05 ` [PATCH net-next v5 13/14] selftests: dpll: add concurrency stress tests Petr Oros
@ 2026-11-25  9:05 ` Petr Oros
  13 siblings, 0 replies; 15+ messages in thread
From: Petr Oros @ 2026-11-25  9:05 UTC (permalink / raw)
  To: netdev
  Cc: Petr Oros, Ivan Vecera, Jiri Pirko, Jonathan Corbet, Shuah Khan,
	Shuah Khan, Randy Dunlap, Jani Nikula, Vadim Fedorenko,
	Arkadiusz Kubalewski, Jiri Pirko, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, linux-doc,
	linux-kselftest

With CONFIG_DPLL_REFCNT_TRACKER the DPLL core keeps the stack traces of
unreleased device and pin references in debugfs. The entries do not name
their owner, so add library helpers that snapshot the entries present
before a test creates its devices and fail when an entry that is not in
the snapshot still holds references afterwards. An entry in the snapshot
belongs to an object of another driver and is ignored even when its
content changed, since other drivers take and drop references while the
tests run. The teardown of a device releases its references before the
write to live returns, so they are expected to be gone at the first
look; the check still polls for a while before it reports a leak, so
that it does not fail on a single read. Attributing by name means that a
leak of an earlier run goes unnoticed, that an object reusing the
address of a freed one from the snapshot can hide a leak, that an object
allocated at the address of a freed one before ref_tracker removed the
file of that one gets no file at all, so its leak is not seen, and that
an object another driver registers while a script runs is reported as a
leak, which is accepted, the entries carry nothing better to go by.
ref_tracker removes the file of a freed object from a work item and a
read of it fails until then, so the snapshot waits for such files to go
away and records only entries it can read, or the file of the probe
device could get into it and hide the next object allocated at that
address. A new entry that reads back empty belongs to an object that is
alive without a tracked reference and is reported as well. A second run
alongside would have its devices attributed to this one, which is one
more reason for the lock every script that creates devices takes.

Whether the tracker works is probed with a throwaway device, which has
to show entries while it is live and to drain them once it is deleted,
so that a kernel without the tracker is told apart from a broken
simulator and from a leak. With CONFIG_REF_TRACKER enabled by another
option the entries exist without CONFIG_DPLL_REFCNT_TRACKER too and just
stay empty, so a probe that saw no reference is a skip unless
/proc/config.gz says the tracker is built in, which makes it a failure.
The library provides the probe as a test, together with the removal of
what it leaves behind, and it runs as the first test of the stress
script, which gains a final leak check that is skipped when a device is
left behind in the dpllsim directory, and of the new dpll_reftrack.sh,
which registers a device, removes it and verifies that no reference
survived. The snapshot file lives next to the lock file under /run, for
the same reason the lock file does not live in /tmp. The library mounts
debugfs when nothing else did, like it does for configfs, and the
selftest config enables the tracker together with CONFIG_DEBUG_KERNEL,
which it depends on, debugfs and CONFIG_IKCONFIG_PROC for the look at
/proc/config.gz.

Reviewed-by: Ivan Vecera <ivecera@redhat.com>
Acked-by: Jiri Pirko <jiri@nvidia.com>
Signed-off-by: Petr Oros <poros@redhat.com>
---
v5:
- move the ref_tracker probe test to the library, the stress and the
  reference leak scripts carried a copy each
- report a new entry that reads back empty, its object is alive
  without a tracked reference
- drop the final check of dpll_reftrack.sh, it repeated the check of
  the test before it, and do not check for leaks after a failed
  removal
- skip the final check of the stress script when a device of the run
  is left in the dpllsim directory, a worker does not track it, but
  not for one that was there before, its references are in the
  snapshot, see patch 2 for the record of those devices
- list in the commit message that an object allocated at the address
  of a freed one before its file was removed is not seen
- ignore further signals in the INT and TERM handler and run the
  cleanup from it, see patch 2
- correct comments: the helper that lists the leak candidates takes a
  second argument, the probe returns 1 when no entry with a reference
  appeared, the entries exist without the DPLL tracker only with
  CONFIG_REF_TRACKER enabled by another option, and what is expected
  to be gone at the first look are the references
- say what the unmount of debugfs at exit means to others
- say in the changelog that the scripts creating devices take the lock
- fail the leak check when the tracker dir is gone since the probe, it
  could not look
- leave an entry that cannot be read out of the changed entries of
  other drivers, it belongs to a freed object
- say in the skips of the leak checks that the probe did not confirm
  the tracker, which a probe device that cannot be deleted leaves
  unconfirmed
- move the removal of what the probe test leaves behind and the
  globals of the probe test to the library
- say that the probe also skips when the kernel configuration cannot
  be read for want of zcat
- keep a probe device that failed half way and cannot be removed for
  the cleanup handler and fail the probe, it was taken for a directory
  left behind and retried
- keep the config sorted the way the netdev CI checks it, by the names
  with the underscores removed, which puts DPLL_REFCNT_TRACKER before
  DPLLSIM
- keep dpll_reftrack.sh a script of its own, the basic and feature
  scripts run against a device the user provides as well, and the
  stress script checks for leaks only once after all its tests
- leave STACKTRACE_SUPPORT out of the config, it has no prompt and the
  architecture selects it, where it is missing the probe skips

v4:
- wait for the files of freed objects to go away before a snapshot and
  record only entries that can be read
- a probe that saw no reference is a failure when /proc/config.gz says
  the tracker is built in, a skip otherwise
- skip the final leak check of the stress script when an earlier test
  left a device behind
- record the debugfs mount before mounting
- say that objects another driver registers during a run are reported
- enable CONFIG_IKCONFIG_PROC in the selftest config, the probe reads
  /proc/config.gz
- say that the probe device could not be created or deployed, not only
  created
- add the Reviewed-by given to v3 (Ivan Vecera)

v3:
- say what the leak check waits for instead of a concurrent dump
  holding references, which nothing does (Ivan Vecera)
- follow the move of the selftests to
  tools/testing/selftests/drivers/dpll

v2:
- keep the config file sorted and quote the ret expansions (netdev CI
  shellcheck)

v4: https://lore.kernel.org/all/20261112130359.840795-15-poros@redhat.com/
v3: https://lore.kernel.org/all/20261109093523.931201-15-poros@redhat.com/
v2: https://lore.kernel.org/all/20261105151852.1844305-15-poros@redhat.com/
v1: https://lore.kernel.org/all/20261103093910.596225-15-poros@redhat.com/
---
 tools/testing/selftests/drivers/dpll/Makefile |   1 +
 tools/testing/selftests/drivers/dpll/config   |   5 +
 .../selftests/drivers/dpll/dpll_lib.sh        | 425 +++++++++++++++++-
 .../selftests/drivers/dpll/dpll_reftrack.sh   | 111 +++++
 .../selftests/drivers/dpll/dpll_stress.sh     |  59 ++-
 5 files changed, 569 insertions(+), 32 deletions(-)
 create mode 100755 tools/testing/selftests/drivers/dpll/dpll_reftrack.sh

diff --git a/tools/testing/selftests/drivers/dpll/Makefile b/tools/testing/selftests/drivers/dpll/Makefile
index 1349d078fa2660..d292ea049edfa3 100644
--- a/tools/testing/selftests/drivers/dpll/Makefile
+++ b/tools/testing/selftests/drivers/dpll/Makefile
@@ -10,6 +10,7 @@ TEST_FILES := dpll_lib.sh
 TEST_PROGS := \
 	dpll_basic.sh \
 	dpll_features.sh \
+	dpll_reftrack.sh \
 	dpll_stress.sh \
 # end of TEST_PROGS
 
diff --git a/tools/testing/selftests/drivers/dpll/config b/tools/testing/selftests/drivers/dpll/config
index e2c90b6970bd43..69ff54875712c6 100644
--- a/tools/testing/selftests/drivers/dpll/config
+++ b/tools/testing/selftests/drivers/dpll/config
@@ -1,3 +1,8 @@
 CONFIG_CONFIGFS_FS=y
+CONFIG_DEBUG_FS=y
+CONFIG_DEBUG_KERNEL=y
 CONFIG_DPLL=y
+CONFIG_DPLL_REFCNT_TRACKER=y
 CONFIG_DPLLSIM=m
+CONFIG_IKCONFIG=y
+CONFIG_IKCONFIG_PROC=y
diff --git a/tools/testing/selftests/drivers/dpll/dpll_lib.sh b/tools/testing/selftests/drivers/dpll/dpll_lib.sh
index 6f07837a7d9deb..06402b3194a11b 100644
--- a/tools/testing/selftests/drivers/dpll/dpll_lib.sh
+++ b/tools/testing/selftests/drivers/dpll/dpll_lib.sh
@@ -17,6 +17,8 @@
 
 CONFIGFS_DIR="/sys/kernel/config"
 DPLLSIM_CFG="$CONFIGFS_DIR/dpllsim"
+DEBUGFS_DIR="/sys/kernel/debug"
+DPLL_REF_TRACKER_DIR="$DEBUGFS_DIR/ref_tracker"
 
 # Next clock-id handed out by dpllsim_alloc_clock_id. The lookups of the
 # dpllsim helpers match on the module name as well, which is what keeps
@@ -58,13 +60,14 @@ dpll_lib_check_prereqs()
 }
 
 # The scripts that create dpllsim devices share the one dpllsim directory
-# and cannot run alongside each other, so each takes a lock on
-# DPLL_LOCK_FILE before it mounts or creates anything and skips when
-# another run holds it. A run of the feature tests against a device the
-# user provided takes it as well, two of them would change the same
-# pins. The workers a script forks inherit the open file and keep the
-# lock until they exit. The file lives under /run rather than in the
-# world-writable /tmp, where a planted symlink could redirect the open.
+# and the leak check attributes every new ref_tracker entry to the run,
+# so each takes a lock on DPLL_LOCK_FILE before it mounts or creates
+# anything and skips when another run holds it. A run of the feature
+# tests against a device the user provided takes it as well, two of them
+# would change the same pins. The workers a script forks inherit the
+# open file and keep the lock until they exit. The files live under /run
+# rather than in the world-writable /tmp, where a planted symlink could
+# redirect the open.
 DPLL_RUN_DIR="/run/dpll-selftest"
 DPLL_LOCK_FILE="$DPLL_RUN_DIR/lock"
 DPLL_LOCK_FD=""
@@ -552,10 +555,11 @@ dpll_pin_set_phase_adjust()
 # device is not live.
 #
 # dpllsim_available loads the dpllsim module and mounts configfs when
-# nothing else did. Scripts call dpll_lib_cleanup last from their exit
-# trap to undo the mount, the module stays loaded. The only other trace
-# the library leaves outside the dpllsim directory is DPLL_RUN_DIR with
-# the lock file.
+# nothing else did, dpll_debugfs_available below does the same for
+# debugfs. Scripts call dpll_lib_cleanup last from their exit trap to
+# undo the mounts, the module stays loaded. The only other trace the
+# library leaves outside the dpllsim directory is DPLL_RUN_DIR with the
+# lock file.
 
 DPLLSIM_MOUNTED_CONFIGFS=0
 # Name of the device dpllsim_create_device created last. Set here, so
@@ -586,18 +590,29 @@ dpllsim_available()
 	[ -d "$DPLLSIM_CFG" ]
 }
 
-# Unmount configfs again when dpllsim_available mounted it. Call after
-# the devices of the script are gone. A dpllsim device the script could
-# not remove keeps the mount, so that what is left can be looked at. A
-# device that was there before the script is not one it left behind.
+__dpll_lib_umount()
+{
+	# The mount recorded up front may have failed.
+	mountpoint -q "$1" || return 0
+	umount "$1" 2>/dev/null || log_info "cannot unmount $1"
+}
+
+# Unmount configfs and debugfs again when the library mounted them. Call
+# after the devices of the script are gone. A dpllsim device the script
+# could not remove keeps both mounted, so that what is left can be
+# looked at, the device in configfs and the references it still holds
+# in the ref_tracker entries of debugfs. A device that was there before
+# the script is not one it left behind.
 dpll_lib_cleanup()
 {
-	local dev_path name
+	local dev_path name dir kept
+	local mounted=()
 
-	[ "$DPLLSIM_MOUNTED_CONFIGFS" -eq 1 ] || return 0
+	[ "$DPLLSIM_MOUNTED_CONFIGFS" -eq 1 ] && mounted+=("$CONFIGFS_DIR")
+	[ "$DPLL_MOUNTED_DEBUGFS" -eq 1 ] && mounted+=("$DEBUGFS_DIR")
 	DPLLSIM_MOUNTED_CONFIGFS=0
-	# The mount recorded up front may have failed.
-	mountpoint -q "$CONFIGFS_DIR" || return 0
+	DPLL_MOUNTED_DEBUGFS=0
+	[ "${#mounted[@]}" -gt 0 ] || return 0
 
 	for dev_path in "$DPLLSIM_CFG"/*/; do
 		[ -d "$dev_path" ] || continue
@@ -607,11 +622,14 @@ dpll_lib_cleanup()
 		grep -qxF -- "$dev_path" <<< "$DPLLSIM_DEVICES_BEFORE" &&
 			continue
 		name=$(basename "$dev_path")
-		log_info "device $name left behind, $CONFIGFS_DIR kept mounted"
+		kept=${mounted[*]}
+		kept="${kept/ / and } kept mounted"
+		log_info "device $name left behind, $kept"
 		return
 	done
-	umount "$CONFIGFS_DIR" 2>/dev/null ||
-		log_info "cannot unmount $CONFIGFS_DIR"
+	for dir in "${mounted[@]}"; do
+		__dpll_lib_umount "$dir"
+	done
 }
 
 # The scripts find the devices of the simulator by the module name they
@@ -799,3 +817,366 @@ dpllsim_setup_standard_device()
 		return 1
 	fi
 }
+
+##############################################################################
+# ref_tracker leak checking
+#
+# With CONFIG_DPLL_REFCNT_TRACKER=y the DPLL core creates regular files
+# named dpll_device@<ptr> / dpll_pin@<ptr> under the ref_tracker debugfs
+# dir. Reading one prints the stack traces of currently unreleased
+# references. A file that reads back empty belongs to an object that is
+# alive without a tracked reference, a file that cannot be read to an
+# object that was freed and waits for its file to be removed.
+#
+# The entries do not name their owner, so leaks are attributed by
+# snapshot, an entry of a live object whose name is not in the snapshot
+# taken before the test created its devices is a leak, an entry that is
+# in it belongs to another driver and is ignored even when its content
+# changed.
+# An object another driver registers while a script runs cannot be told
+# from one of the script and is reported as a leak, the scripts need a
+# system where no other DPLL driver is probed or reloaded meanwhile.
+#
+# ref_tracker removes the file of a freed object from a work item. Until
+# that ran the file is still there and a read of it fails. Such an entry
+# must not get into a snapshot, its name would hide a later object at the
+# same address. An object allocated at the address of a freed one before
+# the work item ran gets no file at all, ref_tracker_dir_debugfs() finds
+# the old one and creates none, so a leak of such an object is not seen,
+# which is accepted.
+
+DPLL_MOUNTED_DEBUGFS=0
+# The snapshot files are kept in DPLL_RUN_DIR, see the lock above for
+# why that is not /tmp, as refsnap.<pid>.<random>.
+DPLL_REF_SNAPSHOT_PREFIX="$DPLL_RUN_DIR/refsnap"
+# Name of the probe device while it exists, so that the cleanup handler
+# of a script can remove it when the probe could not.
+DPLLSIM_PROBE_DEVICE=""
+# The snapshot file and the verdict of dpll_test_ref_tracker_probe, see
+# there.
+REF_SNAPSHOT=""
+REF_TRACKER=0
+
+# Create the file a snapshot is recorded in and print its name. The
+# scripts take the lock first, which creates DPLL_RUN_DIR. A run that
+# is killed with SIGKILL cannot remove its file, so the files of earlier
+# runs whose process is gone are removed first, the pid of the run is
+# the component after the first dot of the name. A pid that was reused
+# by a process that lives long would keep such a file forever, so files
+# older than a day are removed whatever their pid is. Return 1 when the
+# file cannot be created.
+dpll_ref_tracker_snapshot_file()
+{
+	local file pid
+
+	find "$DPLL_RUN_DIR" -maxdepth 1 \
+	     -name "${DPLL_REF_SNAPSHOT_PREFIX##*/}.*" -mmin +1440 -delete \
+	     2>/dev/null
+	for file in "$DPLL_REF_SNAPSHOT_PREFIX".*; do
+		[ -f "$file" ] || continue
+		pid=${file#"$DPLL_REF_SNAPSHOT_PREFIX".}
+		pid=${pid%%.*}
+		[[ "$pid" =~ ^[0-9]+$ ]] || continue
+		[ -d "/proc/$pid" ] || rm -f "$file"
+	done
+	mktemp "$DPLL_REF_SNAPSHOT_PREFIX.$$.XXXXXX"
+}
+
+# Mount debugfs when nothing else did, so that the ref_tracker entries
+# can be read, and remember that for dpll_lib_cleanup. Return 0 when
+# debugfs is mounted afterwards, whether the tracker dir exists is for
+# the caller to check.
+dpll_debugfs_available()
+{
+	if ! mountpoint -q "$DEBUGFS_DIR"; then
+		# Nothing had debugfs mounted, so the cleanup unmounts it
+		# again like it does with configfs, see dpllsim_available
+		# for what that means to others. The mount is recorded
+		# before it is made, so that a cleanup run by a signal
+		# right after it knows about it.
+		DPLL_MOUNTED_DEBUGFS=1
+		mount -t debugfs none "$DEBUGFS_DIR" &> /dev/null ||
+			DPLL_MOUNTED_DEBUGFS=0
+	fi
+	mountpoint -q "$DEBUGFS_DIR"
+}
+
+__dpll_ref_tracker_hash()
+{
+	printf '%s' "$1" | md5sum | cut -d' ' -f1
+}
+
+# Return 0 when every dpll_device@* / dpll_pin@* entry can be read,
+# which means that no file of a freed object is waiting for its removal.
+__dpll_ref_tracker_reaped()
+{
+	local entry
+
+	for entry in "$DPLL_REF_TRACKER_DIR"/dpll_device@* \
+		     "$DPLL_REF_TRACKER_DIR"/dpll_pin@*; do
+		[ -f "$entry" ] || continue
+		cat "$entry" &> /dev/null || return 1
+	done
+}
+
+# Write "<name> <content hash>" for every dpll_device@* / dpll_pin@*
+# entry of a live object to the given file, one entry per line. The
+# files of freed objects are given a while to go away first, one that is
+# still there afterwards cannot be read and is left out. The file ends
+# up empty when the tracker dir is missing.
+dpll_ref_tracker_snapshot()
+{
+	local file=$1
+	local entry content
+
+	busywait "$BUSYWAIT_TIMEOUT" __dpll_ref_tracker_reaped > /dev/null
+	for entry in "$DPLL_REF_TRACKER_DIR"/dpll_device@* \
+		     "$DPLL_REF_TRACKER_DIR"/dpll_pin@*; do
+		[ -f "$entry" ] || continue
+		content=$(cat "$entry" 2>/dev/null) || continue
+		echo "$(basename "$entry")" \
+		     "$(__dpll_ref_tracker_hash "$content")"
+	done > "$file"
+}
+
+# Print the leak candidates, the entries with references whose name is
+# not in the snapshot, each followed by its content, and with a
+# non-empty $2 the ones that read back empty as well, their object is
+# alive without a tracked reference. Prints nothing when there is no
+# candidate.
+# $1: snapshot file, $2: report empty entries too (optional)
+__dpll_ref_tracker_new_entries()
+{
+	local file=$1
+	local alive=${2:-}
+	local entry name content
+
+	for entry in "$DPLL_REF_TRACKER_DIR"/dpll_device@* \
+		     "$DPLL_REF_TRACKER_DIR"/dpll_pin@*; do
+		[ -f "$entry" ] || continue
+		name=$(basename "$entry")
+		grep -q "^$name " "$file" && continue
+		content=$(cat "$entry" 2>/dev/null) || continue
+		if [ -z "$content" ]; then
+			[ -n "$alive" ] || continue
+			echo "$name still alive without a tracked reference"
+			continue
+		fi
+		echo "Unreleased references in $name:"
+		echo "$content"
+	done
+}
+
+# Return 0 when there is no leak candidate. Otherwise print the
+# candidates and return 1, busywait hands the output of its last failed
+# check to the caller when it times out.
+__dpll_ref_tracker_settled()
+{
+	local leaks
+
+	leaks=$(__dpll_ref_tracker_new_entries "$1" alive)
+	[ -z "$leaks" ] && return 0
+
+	echo "$leaks"
+	return 1
+}
+
+# Log the entries of the snapshot whose content changed since and record
+# the new content, so that the same change is logged once. An entry that
+# cannot be read belongs to a freed object and is left alone.
+__dpll_ref_tracker_note_changed()
+{
+	local file=$1
+	local entry name content hash changed=""
+
+	for entry in "$DPLL_REF_TRACKER_DIR"/dpll_device@* \
+		     "$DPLL_REF_TRACKER_DIR"/dpll_pin@*; do
+		[ -f "$entry" ] || continue
+		name=$(basename "$entry")
+		grep -q "^$name " "$file" || continue
+		content=$(cat "$entry" 2>/dev/null) || continue
+		hash=$(__dpll_ref_tracker_hash "$content")
+		grep -qx "$name $hash" "$file" && continue
+		sed -i "s/^$name .*/$name $hash/" "$file"
+		changed="$changed $name"
+	done
+
+	[ -n "$changed" ] && log_info \
+		"references of another driver changed (ignored):$changed"
+	return 0
+}
+
+# $1: snapshot file written by dpll_ref_tracker_snapshot before the
+# objects under test were created. Return 0 when no reference to an
+# object created since the snapshot is held any more. The teardown of a
+# device releases its references before the write to live returns, so
+# they are expected to be gone at the first look; the check still
+# polls for a while rather than failing on that look, so that a leak is
+# reported only when the references stay. A timeout means they never
+# drained, the candidates seen by the last check are then printed and
+# count as leaked. A tracker dir that is gone since the probe fails the
+# check as well, it cannot look.
+dpll_ref_tracker_check_leaks()
+{
+	local file=$1
+	local leaks ret=0
+
+	if ! [ -d "$DPLL_REF_TRACKER_DIR" ]; then
+		echo "$DPLL_REF_TRACKER_DIR is gone"
+		return 1
+	fi
+
+	if ! leaks=$(busywait "$BUSYWAIT_TIMEOUT" __dpll_ref_tracker_settled \
+			      "$file"); then
+		echo "References not released within $BUSYWAIT_TIMEOUT ms:"
+		echo "$leaks"
+		ret=1
+	fi
+	__dpll_ref_tracker_note_changed "$file"
+
+	return "$ret"
+}
+
+# Return 0 when the kernel configuration says that the DPLL reference
+# tracker is built in. With CONFIG_REF_TRACKER enabled by another
+# option the entries exist without it as well, they just stay empty, so
+# a probe that saw no reference cannot tell a kernel without the tracker
+# from a tracker that records nothing. Return 1 when the tracker is not
+# configured or when the configuration cannot be read, without
+# /proc/config.gz or zcat, a probe that saw no reference then skips.
+dpll_ref_tracker_configured()
+{
+	zcat /proc/config.gz 2>/dev/null |
+		grep -q '^CONFIG_DPLL_REFCNT_TRACKER=y'
+}
+
+# Probe whether the kernel tracks DPLL references with a throwaway
+# dpllsim device, the entries that exist before it are recorded in the
+# scratch file $1. Return 0 when the tracker works, 1 when no entry with
+# a reference appeared, which dpll_ref_tracker_configured helps to
+# interpret, 2 when the probe device could not be created or deployed, 3
+# when its entries did not drain after its deletion and 4 when it could
+# not be deleted, its name then stays in DPLLSIM_PROBE_DEVICE for the
+# cleanup handler to retry. Must not be called from a subshell, it
+# allocates clock-ids.
+dpll_ref_tracker_probe()
+{
+	local file=$1
+	local name attempt ret=2
+
+	dpll_ref_tracker_snapshot "$file"
+
+	# A directory of that name left behind by an earlier run takes a
+	# fresh clock-id, anything else is a broken simulator. One that
+	# failed half way and could not be removed again keeps its name
+	# in DPLLSIM_CREATED_NAME, the next device would take that over.
+	for attempt in 1 2 3; do
+		dpllsim_alloc_clock_id
+		name="probe$DPLLSIM_CLOCK_ID"
+		dpllsim_create_device "$name" "$DPLLSIM_CLOCK_ID" && break
+		if [ "$DPLLSIM_CREATED_NAME" = "$name" ]; then
+			DPLLSIM_PROBE_DEVICE=$name
+			log_info "cannot delete device $name"
+			return 4
+		fi
+		[ -d "$DPLLSIM_CFG/$name" ] || return 2
+		[ "$attempt" -lt 3 ] || return 2
+	done
+	DPLLSIM_PROBE_DEVICE=$name
+
+	if dpllsim_add_pin "$name" ext0 ext 10000000 input &&
+	   dpllsim_deploy "$name"; then
+		ret=1
+		[ -n "$(__dpll_ref_tracker_new_entries "$file")" ] && ret=0
+	fi
+
+	if dpllsim_delete_device "$name"; then
+		DPLLSIM_PROBE_DEVICE=""
+	else
+		log_info "cannot delete device $name"
+		return 4
+	fi
+	if [ "$ret" -eq 0 ] &&
+	   ! busywait "$BUSYWAIT_TIMEOUT" __dpll_ref_tracker_settled "$file" \
+		      > /dev/null; then
+		ret=3
+	fi
+	return "$ret"
+}
+
+# The ref_tracker probe test of the scripts that check for leaks. It
+# probes for the reference tracker and records the entries that exist
+# before the first test that creates devices, so that every entry that
+# appears from here on is attributed to the script. A probe device that
+# cannot be created says nothing about the tracker, the simulator is
+# broken. A probe device whose references do not drain is the kind of
+# leak the scripts look for, found before the first test. One that
+# cannot be deleted is a failure too, it stays recorded for the cleanup
+# handler to try again. It sets REF_SNAPSHOT, the snapshot file, and
+# REF_TRACKER, which is 0 when the leak checks have to skip.
+dpll_test_ref_tracker_probe()
+{
+	RET=0
+
+	REF_TRACKER=0
+	if ! dpll_debugfs_available || ! [ -d "$DPLL_REF_TRACKER_DIR" ]; then
+		log_test_skip "DPLL ref_tracker probe" \
+			      "ref_tracker debugfs not available"
+		return
+	fi
+
+	# The snapshot file doubles as the scratch file of the probe, so
+	# it is needed before anything is recorded.
+	if ! REF_SNAPSHOT=$(dpll_ref_tracker_snapshot_file); then
+		check_err 1 "cannot create a temporary file for the snapshot"
+		log_test "DPLL ref_tracker probe"
+		return
+	fi
+
+	dpll_ref_tracker_probe "$REF_SNAPSHOT"
+	case $? in
+	0)
+		REF_TRACKER=1
+		;;
+	1)
+		# The probe device held no tracked reference. That is a
+		# kernel without the tracker, unless its configuration
+		# says otherwise.
+		if ! dpll_ref_tracker_configured; then
+			log_test_skip "DPLL ref_tracker probe" \
+				      "no DPLL reference tracked"
+			return
+		fi
+		check_err 1 "tracker enabled but no reference recorded"
+		;;
+	3)
+		REF_TRACKER=1
+		check_err 1 "references of the probe device not released"
+		;;
+	4)
+		# The probe did not get to see the references drain and the
+		# tracker may not have recorded any, so the leak checks skip.
+		check_err 1 "cannot delete the dpllsim probe device"
+		;;
+	*)
+		check_err 1 "cannot create or deploy the dpllsim probe device"
+		;;
+	esac
+	dpll_ref_tracker_snapshot "$REF_SNAPSHOT"
+
+	log_test "DPLL ref_tracker probe"
+}
+
+# Remove what the probe test leaves behind, the probe device it could not
+# delete and the snapshot file. For the cleanup handler of a script that
+# runs the probe test, before dpll_lib_cleanup.
+dpll_ref_tracker_cleanup()
+{
+	if [ -n "$DPLLSIM_PROBE_DEVICE" ]; then
+		dpllsim_delete_device "$DPLLSIM_PROBE_DEVICE" ||
+			log_info "cannot delete device $DPLLSIM_PROBE_DEVICE"
+	fi
+	if [ -n "$REF_SNAPSHOT" ]; then
+		rm -f "$REF_SNAPSHOT"
+	fi
+}
diff --git a/tools/testing/selftests/drivers/dpll/dpll_reftrack.sh b/tools/testing/selftests/drivers/dpll/dpll_reftrack.sh
new file mode 100755
index 00000000000000..15bd56e18105fe
--- /dev/null
+++ b/tools/testing/selftests/drivers/dpll/dpll_reftrack.sh
@@ -0,0 +1,111 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# DPLL reference tracking test. With CONFIG_DPLL_REFCNT_TRACKER=y the DPLL
+# core tracks device and pin references through ref_tracker; the debugfs
+# files dpll_device@<ptr> / dpll_pin@<ptr> list the stack traces of
+# currently unreleased references. This test records the entries that
+# exist before it starts, registers dpllsim devices and verifies that
+# every entry that appeared since is released again after unregistration.
+#
+# Copyright (c) 2026 Red Hat, Inc.
+
+SCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
+
+source "$SCRIPTDIR/../../net/lib.sh"
+source "$SCRIPTDIR/dpll_lib.sh"
+
+ALL_TESTS="
+	dpll_test_ref_tracker_probe
+	test_basic_reftrack
+"
+
+CREATED_DEVICE=""
+
+# The device stays recorded when its removal fails, so that the cleanup
+# handler tries again.
+destroy_device()
+{
+	[ -n "$CREATED_DEVICE" ] || return 0
+
+	dpllsim_delete_device "$CREATED_DEVICE" || return 1
+	CREATED_DEVICE=""
+}
+
+cleanup()
+{
+	# A second signal must not abort the teardown half way.
+	trap '' INT TERM
+
+	defer_scopes_cleanup
+	destroy_device || log_info "cannot delete device $CREATED_DEVICE"
+	# The device whose creation a signal interrupted is recorded by the
+	# library alone, a double delete of a tracked one is harmless.
+	if [ -n "$DPLLSIM_CREATED_NAME" ]; then
+		dpllsim_delete_device "$DPLLSIM_CREATED_NAME" ||
+			log_info "cannot delete device $DPLLSIM_CREATED_NAME"
+	fi
+	dpll_ref_tracker_cleanup
+	dpll_lib_cleanup
+}
+
+##############################################################################
+
+test_basic_reftrack()
+{
+	RET=0
+
+	# The probe test found no tracker or could not confirm that it works.
+	if [ "$REF_TRACKER" -eq 0 ]; then
+		log_test_skip "DPLL basic reference tracking" \
+			      "reference tracker not confirmed by the probe"
+		return
+	fi
+
+	if ! dpllsim_setup_standard_device; then
+		check_err 1 "cannot create dpllsim device"
+		log_test "DPLL basic reference tracking"
+		return
+	fi
+	CREATED_DEVICE=$DPLLSIM_CREATED_NAME
+
+	# A device that could not be removed still holds its references,
+	# the failure to report is that one.
+	if ! destroy_device; then
+		check_err 1 "cannot delete device $CREATED_DEVICE"
+		log_test "DPLL basic reference tracking"
+		return
+	fi
+
+	if ! dpll_device_wait_gone_by_clock_id "$DPLLSIM_CLOCK_ID" dpllsim; then
+		check_err 1 "device still on netlink after delete"
+	fi
+
+	dpll_ref_tracker_check_leaks "$REF_SNAPSHOT"
+	check_err $? "leaked references after device removal"
+
+	log_test "DPLL basic reference tracking"
+}
+
+##############################################################################
+
+dpll_lib_check_prereqs
+# Another run would find the devices of this one left behind and see
+# them as leaks, see the lock in the library.
+dpll_lib_lock
+
+# The module check and the probe test may mount configfs and debugfs, so
+# the exit trap that unmounts them again has to be in place before. INT
+# and TERM run the cleanup as well, so that a timeout imposed by the
+# harness still removes the device, and ignore any further signal, an exit
+# inside the cleanup would end the teardown half way. Their handler drops
+# the exit trap and calls the cleanup itself, a signal that comes while
+# the script exits is handled before the exit trap gets to the cleanup,
+# and the exit of the handler would end the exit trap there.
+trap cleanup EXIT
+trap 'trap "" INT TERM; trap - EXIT; cleanup; exit 1' INT TERM
+dpllsim_check_module
+
+tests_run
+
+exit $EXIT_STATUS
diff --git a/tools/testing/selftests/drivers/dpll/dpll_stress.sh b/tools/testing/selftests/drivers/dpll/dpll_stress.sh
index 20b3132e8a3db0..caa18796657797 100755
--- a/tools/testing/selftests/drivers/dpll/dpll_stress.sh
+++ b/tools/testing/selftests/drivers/dpll/dpll_stress.sh
@@ -12,11 +12,13 @@ source "$SCRIPTDIR/../../net/lib.sh"
 source "$SCRIPTDIR/dpll_lib.sh"
 
 ALL_TESTS="
+	dpll_test_ref_tracker_probe
 	test_rapid_create_delete
 	test_concurrent_workers
 	test_pin_state_flooding
 	test_many_devices
 	test_deploy_cycle
+	test_ref_leaks
 "
 
 STRESS_ITERATIONS=${STRESS_ITERATIONS:-20}
@@ -180,6 +182,7 @@ cleanup()
 		dpllsim_delete_device "$DPLLSIM_CREATED_NAME" ||
 			log_info "cannot delete device $DPLLSIM_CREATED_NAME"
 	fi
+	dpll_ref_tracker_cleanup
 	dpll_lib_cleanup
 }
 
@@ -576,21 +579,57 @@ test_deploy_cycle()
 	log_test "DPLL deploy/undeploy cycling"
 }
 
+test_ref_leaks()
+{
+	local dev left=0
+
+	RET=0
+
+	# The probe test found no tracker or could not confirm that it works.
+	if [ "$REF_TRACKER" -eq 0 ]; then
+		log_test_skip "DPLL stress ref_tracker check" \
+			      "reference tracker not confirmed by the probe"
+		return
+	fi
+
+	# A device that could not be removed still holds its references,
+	# the failure to report is that one. A worker leaves such a
+	# device untracked, so the directory of the simulator is looked
+	# at, like the cleanup of the library does. A device that was
+	# there before the script is in DPLLSIM_DEVICES_BEFORE, its
+	# references are in the snapshot and do not count.
+	[ "${#CREATED_DEVICES[@]}" -gt 0 ] && left=1
+	for dev in "$DPLLSIM_CFG"/*/; do
+		[ -d "$dev" ] || continue
+		grep -qxF -- "$dev" <<< "$DPLLSIM_DEVICES_BEFORE" || left=1
+	done
+	if [ "$left" -eq 1 ]; then
+		log_test_skip "DPLL stress ref_tracker check" \
+			      "device from an earlier test still present"
+		return
+	fi
+
+	dpll_ref_tracker_check_leaks "$REF_SNAPSHOT"
+	check_err $? "leaked DPLL references after stress"
+
+	log_test "DPLL stress ref_tracker check"
+}
+
 ##############################################################################
 
 dpll_lib_check_prereqs
-# Another run would find the devices of this one left behind, see the
-# lock in the library.
+# Another run would find the devices of this one left behind and see
+# them as leaks, see the lock in the library.
 dpll_lib_lock
 
-# The module check may mount configfs, so the exit trap that unmounts it
-# again has to be in place before. INT and TERM run the cleanup as well,
-# so that a timeout imposed by the harness still removes the devices, and
-# ignore any further signal, an exit inside the cleanup would end the
-# teardown half way. Their handler drops the exit trap and calls the
-# cleanup itself, a signal that comes while the script exits is handled
-# before the exit trap gets to the cleanup, and the exit of the handler
-# would end the exit trap there.
+# The module check and the probe test may mount configfs and debugfs, so
+# the exit trap that unmounts them again has to be in place before. INT
+# and TERM run the cleanup as well, so that a timeout imposed by the
+# harness still removes the devices, and ignore any further signal, an
+# exit inside the cleanup would end the teardown half way. Their handler
+# drops the exit trap and calls the cleanup itself, a signal that comes
+# while the script exits is handled before the exit trap gets to the
+# cleanup, and the exit of the handler would end the exit trap there.
 trap cleanup EXIT
 trap 'trap "" INT TERM; trap - EXIT; cleanup; exit 1' INT TERM
 dpllsim_check_module
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2026-11-25  9:07 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  9:05 [PATCH net-next v5 00/14] dpll: add a simulated DPLL device driver Petr Oros
2026-11-25  9:05 ` [PATCH net-next v5 01/14] dpll: dpllsim: Add " Petr Oros
2026-11-25  9:05 ` [PATCH net-next v5 02/14] selftests: dpll: add test library and device lifecycle tests Petr Oros
2026-11-25  9:05 ` [PATCH net-next v5 03/14] dpll: dpllsim: Add configurable pins Petr Oros
2026-11-25  9:05 ` [PATCH net-next v5 04/14] selftests: dpll: add pin enumeration and property tests Petr Oros
2026-11-25  9:05 ` [PATCH net-next v5 05/14] dpll: dpllsim: Add support to set input pin state Petr Oros
2026-11-25  9:05 ` [PATCH net-next v5 06/14] selftests: dpll: add pin state tests Petr Oros
2026-11-25  9:05 ` [PATCH net-next v5 07/14] dpll: dpllsim: Add support to get/set priority on input pins Petr Oros
2026-11-25  9:05 ` [PATCH net-next v5 08/14] selftests: dpll: add pin priority test Petr Oros
2026-11-25  9:05 ` [PATCH net-next v5 09/14] dpll: dpllsim: Add support to set frequency on pins Petr Oros
2026-11-25  9:05 ` [PATCH net-next v5 10/14] selftests: dpll: add pin frequency test Petr Oros
2026-11-25  9:05 ` [PATCH net-next v5 11/14] dpll: dpllsim: Add support to adjust phase Petr Oros
2026-11-25  9:05 ` [PATCH net-next v5 12/14] selftests: dpll: add pin phase adjust test Petr Oros
2026-11-25  9:05 ` [PATCH net-next v5 13/14] selftests: dpll: add concurrency stress tests Petr Oros
2026-11-25  9:05 ` [PATCH net-next v5 14/14] selftests: dpll: add reference leak tests Petr Oros

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox