* [PATCH net v5 00/14] net: pse-pd: assorted lifetime and error-path fixes
@ 2026-11-18 18:39 Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 01/14] net: pse-pd: size the notification kfifo after defaulting nr_lines Carlo Szelinsky
` (13 more replies)
0 siblings, 14 replies; 27+ messages in thread
From: Carlo Szelinsky @ 2026-11-18 18:39 UTC (permalink / raw)
To: Oleksij Rempel, Kory Maincent, Andrew Lunn, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Mark Brown, Liam Girdwood, Maxime Chevallier,
Kyle Swenson, Ross Cawston, netdev-bot+sashiko, netdev,
linux-kernel, Carlo Szelinsky
Fixes for the PSE core and the pd692x0 driver, found while working on
the PSE controller teardown. Most of them are about what happens when a
controller unbinds or fails to register.
1 The kfifo is too small: podl-pse-regulator and any controller with
0 or 1 lines cannot register.
2 Removes two checks that can never be true. Not a fix; it makes 3
smaller.
3 A module reference leaks when reading the port state fails.
4 A failed IRQ request stays in pcdev->irq, so unregister disables a
line another device may own.
5 The notification worker can read a PHY that was already freed.
6 Setting a power limit checks the PI state without the lock.
7 A power domain shared by two controllers is freed when the one that
created it unbinds, or when registration fails part way.
8 Unregister frees the PI array before it stops the IRQ handler and
the notification worker. The worker can also be queued again
during unregister.
9 Unregister frees the PI array while a lookup can still find the
controller.
10 The PI regulator ops can run after the PI array is freed.
11 A failed registration leaks the kfifo.
12 A shared power domain's supply handle goes stale when the controller
that created it unbinds.
13 pd692x0 leaks its manager power budget when probe fails.
14 pd692x0 crashes on a manager supply that is not resolved yet.
Some patches need earlier ones: 7 needs the locked check from 6, 10 to
12 build on 7 to 9, and 14 relies on 13 to give back a budget that was
already reserved.
Testing: every patch builds on its own with W=1 and is checkpatch
clean. 1, 5, 7, 10 and 12 were tested in QEMU (arm64, KASAN, lockdep,
kmemleak) with test-only fake PSE controllers: without the patch, the
failure in its commit message shows up; with the whole series, the
tests are clean, except for the older lockdep report and the reserved
budget, both listed below. For v5, the whole series was tested again.
The check that 8 adds to pse_set_config_isr() is not covered by these
tests, because the fake controllers have no IRQ. The other patches
were found by code inspection and are build-tested only.
I also tested v4 on real hardware: a Hasivo S1300WP-8XGT-4S+ switch
(RTL9313, two HS104 PSE controllers with an out-of-tree driver, eight
PIs), running OpenWrt with 6.18.55 plus this series:
- Power delivery, enable and disable with ethtool --set-pse, and the
delivering LED triggers all work, also on a cold boot where the PSE
controllers probe after the PHYs.
- Unbind/bind and rmmod/modprobe of the PSE driver with PHYs attached:
the PHYs drop and get back their pse_controls, the module refcount
goes back to 0, and there are no warnings.
- 50 rounds of unbind and bind of both controllers, while ethtool
--show-pse/--set-pse loops and LED sysfs reads run on the same
ports: all ports come back, and dmesg is clean.
v5 only rebases and changes text, so I did not run it again.
Ross Cawston's "net: pse-pd: don't enable a PI again while disabling
it" [1] applies on top of this series without conflicts. The tests
also hit two older bugs that this series does not fix:
- With the static strategy, turning off a PI to make room for a
waiting one retries the waiting PI from inside its own allocation,
which recurses until the kernel stack overflows. [1] removes that
retry.
- pse_isr() and setting a power limit hold pcdev->lock and then take
the supply's regulator lock, while regulator_enable() on a PI takes
them the other way round. Lockdep reports it when the supply lock
is contended. Kyle Swenson's RFC [2] removes the PI regulators, so
the second order goes away.
Older problems that reviews of this series found, not fixed here:
- A pse_control held by a PHY can outlive its controller. The PSE
notifier series for net-next fixes this.
- When a controller unbinds, its PIs are not switched off, and the
budget they hold stays reserved on the supply. This should use the
normal disable path, and belongs with the fix above.
- The notification worker uses the net_device without holding a
reference to it.
- Setting a power limit changes the budget and the hardware limit
under two separate locks, so pse_isr() can run in between.
- On tps23881, pse_controller_unregister() calls disable_irq() after
devres has already freed the IRQ.
- A PI that waits for budget, or was turned off to make room for
another one, can get budget reserved by a power limit change; that
budget is not freed when the PI is powered later.
- The PI regulators are visible before their power domain is set. A
PI enabled in that short time skips the budget check.
- pse_control_get_internal() reads the PI state from the hardware and
sets admin_state_enabled without pcdev->lock.
- In the regulator core, regulator_request_power_budget() first
reserves the budget on the parent supply. If the request then fails
on the regulator itself, the parent's part is not given back.
Changes in v5:
- Rebased on the current net/main. 7: only the context changed, after
the devm_kmalloc_obj() conversion; the code is the same.
- 6: the commit message says the budget is lost, not only reserved,
and a comment no longer says "powered" (Sashiko).
- v4: https://lore.kernel.org/netdev/20261111211731.3193796-1-github@szelinsky.de/
Changes in v4:
- Rebased on the current net/main.
- 5: shorter comments; the locking rule is now in the kernel-doc
(Kory, Sashiko).
- 8: pse_set_config_isr() does not try to power a PI once unregister
has started, so it no longer reports a false power control error
(Sashiko). Kory's Reviewed-by is kept, as this is a small change.
- 9: the comment in pse_controller_unregister() no longer promises
more than the code does.
- 10: the commit message names the patches it needs (Sashiko).
- Collected Reviewed-by tags.
- v3: https://lore.kernel.org/netdev/20261103220217.1131971-1-github@szelinsky.de/
Changes in v3:
- Rebased on the current net/main.
- 7 now also gives back the power domains when pse_register_pw_ds()
fails part way, moved here from v2 11. The release asserts that
pse_pw_d_mutex is held (Oleksij).
- 8 now sets pcdev->unregistering, moved here from v2 10, so the
worker cannot be queued again after the drain (Sashiko, Oleksij).
With that, it has a Fixes: tag again.
- 10 only keeps the PI array until the PI regulators are gone.
Switching off the PIs on unbind is dropped (Oleksij); this also
drops the lock order problem Sashiko found in it.
- 11 only frees the kfifo now, so its subject changed.
- 12: asserts that pse_pw_d_mutex is held in pse_pw_d_leave(),
renames pse_pw_d_release_locked() to pse_pw_d_release() (Oleksij),
and wraps two lines to 80 columns.
- Collected Acked-by tags. Dropped the tags on 7 and 8, as their code
changed.
- v2: https://lore.kernel.org/netdev/20261020171249.853085-1-github@szelinsky.de/
[1] https://lore.kernel.org/netdev/20261019220835.613-1-ross@r-sc.ca/
[2] https://lore.kernel.org/netdev/20261020-pse_regulator_fixes-v1-1-fd6084a22cba@est.tech/
Carlo Szelinsky (14):
net: pse-pd: size the notification kfifo after defaulting nr_lines
net: pse-pd: remove dead pi_get_admin_state() checks
net: pse-pd: fix module refcount leak in pse_control_get_internal()
net: pse-pd: do not keep a failed IRQ in pcdev->irq
net: pse-pd: do not reach a freed PHY from the notification worker
net: pse-pd: check the PI state under the lock when setting a power
limit
net: pse-pd: do not tie a shared power domain to its creator's devres
net: pse-pd: stop async event sources before freeing PI data in
unregister
net: pse-pd: unregister from the controller list before freeing PI
data
net: pse-pd: keep the PI data until the PI regulators are gone
net: pse-pd: free the notification kfifo when registration fails
net: pse-pd: use each PI's own supply handle for the power budget
net: pse-pd: pd692x0: release the manager power budget through devres
net: pse-pd: pd692x0: defer probe while a manager supply is unresolved
drivers/net/pse-pd/pd692x0.c | 31 +++-
drivers/net/pse-pd/pse_core.c | 303 +++++++++++++++++++++++-----------
include/linux/pse-pd/pse.h | 5 +
3 files changed, 235 insertions(+), 104 deletions(-)
base-commit: 2a324a58a084c5cfd7e5a2f55e0786bee170a4a5
--
2.43.0
^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH net v5 01/14] net: pse-pd: size the notification kfifo after defaulting nr_lines
2026-11-18 18:39 [PATCH net v5 00/14] net: pse-pd: assorted lifetime and error-path fixes Carlo Szelinsky
@ 2026-11-18 18:39 ` Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 02/14] net: pse-pd: remove dead pi_get_admin_state() checks Carlo Szelinsky
` (12 subsequent siblings)
13 siblings, 0 replies; 27+ messages in thread
From: Carlo Szelinsky @ 2026-11-18 18:39 UTC (permalink / raw)
To: Oleksij Rempel, Kory Maincent, Andrew Lunn, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Mark Brown, Liam Girdwood, Maxime Chevallier,
Kyle Swenson, Ross Cawston, netdev-bot+sashiko, netdev,
linux-kernel, Carlo Szelinsky
pse_controller_register() allocates the notification kfifo before it
turns nr_lines == 0 into 1, and kfifo_alloc() fails for a size below 2.
So a controller with nr_lines 0 or 1 cannot register. pse_regulator
(podl-pse-regulator) never sets nr_lines, so its probe has failed with
-EINVAL ever since the kfifo was added.
Apply the default first, and allocate at least two entries.
Triggered in QEMU (arm64) with a test-only PSE controller with
nr_lines 0: its probe failed with "failed to allocate kfifo
notifications". With this patch it registers.
Fixes: ffef61d6d273 ("net: pse-pd: Add support for budget evaluation strategies")
Reviewed-by: Kory Maincent <kory.maincent@bootlin.com>
Acked-by: Oleksij Rempel <o.rempel@pengutronix.de>
Signed-off-by: Carlo Szelinsky <github@szelinsky.de>
---
drivers/net/pse-pd/pse_core.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
index a3d4a3d7e525..972264b26036 100644
--- a/drivers/net/pse-pd/pse_core.c
+++ b/drivers/net/pse-pd/pse_core.c
@@ -1043,16 +1043,19 @@ int pse_controller_register(struct pse_controller_dev *pcdev)
mutex_init(&pcdev->lock);
INIT_LIST_HEAD(&pcdev->pse_control_head);
spin_lock_init(&pcdev->ntf_fifo_lock);
- ret = kfifo_alloc(&pcdev->ntf_fifo, pcdev->nr_lines, GFP_KERNEL);
+
+ if (!pcdev->nr_lines)
+ pcdev->nr_lines = 1;
+
+ /* kfifo_alloc() rejects a size that rounds up to less than 2 */
+ ret = kfifo_alloc(&pcdev->ntf_fifo, max(pcdev->nr_lines, 2U),
+ GFP_KERNEL);
if (ret) {
dev_err(pcdev->dev, "failed to allocate kfifo notifications\n");
return ret;
}
INIT_WORK(&pcdev->ntf_work, pse_send_ntf_worker);
- if (!pcdev->nr_lines)
- pcdev->nr_lines = 1;
-
if (!pcdev->ops->pi_get_admin_state ||
!pcdev->ops->pi_get_pw_status) {
dev_err(pcdev->dev,
--
2.43.0
^ permalink raw reply related [flat|nested] 27+ messages in thread
* [PATCH net v5 02/14] net: pse-pd: remove dead pi_get_admin_state() checks
2026-11-18 18:39 [PATCH net v5 00/14] net: pse-pd: assorted lifetime and error-path fixes Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 01/14] net: pse-pd: size the notification kfifo after defaulting nr_lines Carlo Szelinsky
@ 2026-11-18 18:39 ` Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 03/14] net: pse-pd: fix module refcount leak in pse_control_get_internal() Carlo Szelinsky
` (11 subsequent siblings)
13 siblings, 0 replies; 27+ messages in thread
From: Carlo Szelinsky @ 2026-11-18 18:39 UTC (permalink / raw)
To: Oleksij Rempel, Kory Maincent, Andrew Lunn, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Mark Brown, Liam Girdwood, Maxime Chevallier,
Kyle Swenson, Ross Cawston, netdev-bot+sashiko, netdev,
linux-kernel, Carlo Szelinsky
pse_controller_register() already fails when a driver has no
pi_get_admin_state() callback. So the checks for it in
pse_pi_is_enabled() and pse_control_get_internal() are never true.
Remove them.
No functional change. It makes the next patch, which fixes an error
path in pse_control_get_internal(), smaller.
Reviewed-by: Kory Maincent <kory.maincent@bootlin.com>
Acked-by: Oleksij Rempel <o.rempel@pengutronix.de>
Signed-off-by: Carlo Szelinsky <github@szelinsky.de>
---
drivers/net/pse-pd/pse_core.c | 10 ----------
1 file changed, 10 deletions(-)
diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
index 972264b26036..309adec93679 100644
--- a/drivers/net/pse-pd/pse_core.c
+++ b/drivers/net/pse-pd/pse_core.c
@@ -412,13 +412,8 @@ static bool pse_pw_d_is_sw_pw_control(struct pse_controller_dev *pcdev,
static int pse_pi_is_enabled(struct regulator_dev *rdev)
{
struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
- const struct pse_controller_ops *ops;
int id, ret;
- ops = pcdev->ops;
- if (!ops->pi_get_admin_state)
- return -EOPNOTSUPP;
-
id = rdev_get_id(rdev);
mutex_lock(&pcdev->lock);
if (pse_pw_d_is_sw_pw_control(pcdev, pcdev->pi[id].pw_d)) {
@@ -1423,11 +1418,6 @@ pse_control_get_internal(struct pse_controller_dev *pcdev, unsigned int index,
goto free_psec;
}
- if (!pcdev->ops->pi_get_admin_state) {
- ret = -EOPNOTSUPP;
- goto free_psec;
- }
-
/* Initialize admin_state_enabled before the regulator_get. This
* aims to have the right value reported in the first is_enabled
* call in case of control managed by software.
--
2.43.0
^ permalink raw reply related [flat|nested] 27+ messages in thread
* [PATCH net v5 03/14] net: pse-pd: fix module refcount leak in pse_control_get_internal()
2026-11-18 18:39 [PATCH net v5 00/14] net: pse-pd: assorted lifetime and error-path fixes Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 01/14] net: pse-pd: size the notification kfifo after defaulting nr_lines Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 02/14] net: pse-pd: remove dead pi_get_admin_state() checks Carlo Szelinsky
@ 2026-11-18 18:39 ` Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 04/14] net: pse-pd: do not keep a failed IRQ in pcdev->irq Carlo Szelinsky
` (10 subsequent siblings)
13 siblings, 0 replies; 27+ messages in thread
From: Carlo Szelinsky @ 2026-11-18 18:39 UTC (permalink / raw)
To: Oleksij Rempel, Kory Maincent, Andrew Lunn, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Mark Brown, Liam Girdwood, Maxime Chevallier,
Kyle Swenson, Ross Cawston, netdev-bot+sashiko, netdev,
linux-kernel, Carlo Szelinsky
pse_control_get_internal() takes a module reference, but when
pse_pi_is_hw_enabled() fails, it skips module_put(). This happens on
any I2C or UART error. Each time, one more reference leaks, and the
PSE driver can no longer be unloaded.
Send this error path to put_module, like the regulator_get_exclusive()
error path.
Found by code inspection. Not reproduced.
Fixes: ffef61d6d273 ("net: pse-pd: Add support for budget evaluation strategies")
Reviewed-by: Kory Maincent <kory.maincent@bootlin.com>
Acked-by: Oleksij Rempel <o.rempel@pengutronix.de>
Signed-off-by: Carlo Szelinsky <github@szelinsky.de>
---
drivers/net/pse-pd/pse_core.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
index 309adec93679..0cce17670d0b 100644
--- a/drivers/net/pse-pd/pse_core.c
+++ b/drivers/net/pse-pd/pse_core.c
@@ -1424,7 +1424,7 @@ pse_control_get_internal(struct pse_controller_dev *pcdev, unsigned int index,
*/
ret = pse_pi_is_hw_enabled(pcdev, index);
if (ret < 0)
- goto free_psec;
+ goto put_module;
pcdev->pi[index].admin_state_enabled = ret;
psec->ps = regulator_get_exclusive(pcdev->dev,
--
2.43.0
^ permalink raw reply related [flat|nested] 27+ messages in thread
* [PATCH net v5 04/14] net: pse-pd: do not keep a failed IRQ in pcdev->irq
2026-11-18 18:39 [PATCH net v5 00/14] net: pse-pd: assorted lifetime and error-path fixes Carlo Szelinsky
` (2 preceding siblings ...)
2026-11-18 18:39 ` [PATCH net v5 03/14] net: pse-pd: fix module refcount leak in pse_control_get_internal() Carlo Szelinsky
@ 2026-11-18 18:39 ` Carlo Szelinsky
2026-11-20 6:41 ` netdev-bot+sashiko
2026-11-18 18:39 ` [PATCH net v5 05/14] net: pse-pd: do not reach a freed PHY from the notification worker Carlo Szelinsky
` (9 subsequent siblings)
13 siblings, 1 reply; 27+ messages in thread
From: Carlo Szelinsky @ 2026-11-18 18:39 UTC (permalink / raw)
To: Oleksij Rempel, Kory Maincent, Andrew Lunn, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Mark Brown, Liam Girdwood, Maxime Chevallier,
Kyle Swenson, Ross Cawston, netdev-bot+sashiko, netdev,
linux-kernel, Carlo Szelinsky
devm_pse_irq_helper() keeps the IRQ number in pcdev->irq even when the
IRQ request failed. The failed probe then runs
pse_controller_unregister(), which calls disable_irq() on that line. If
another device owns it, that device stops getting interrupts.
Clear pcdev->irq when the request fails. It is now set before the
request, because pse_isr() can run as soon as the IRQ is enabled and
pse_pw_d_is_sw_pw_control() reads it.
Found by code inspection. Not reproduced.
Fixes: ffef61d6d273 ("net: pse-pd: Add support for budget evaluation strategies")
Reviewed-by: Kory Maincent <kory.maincent@bootlin.com>
Acked-by: Oleksij Rempel <o.rempel@pengutronix.de>
Signed-off-by: Carlo Szelinsky <github@szelinsky.de>
---
drivers/net/pse-pd/pse_core.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
index 0cce17670d0b..0894613515e0 100644
--- a/drivers/net/pse-pd/pse_core.c
+++ b/drivers/net/pse-pd/pse_core.c
@@ -1341,14 +1341,20 @@ int devm_pse_irq_helper(struct pse_controller_dev *pcdev, int irq,
if (!h->notifs_mask)
return -ENOMEM;
+ /* pse_isr() can run as soon as the IRQ is enabled, and
+ * pse_pw_d_is_sw_pw_control() reads pcdev->irq.
+ */
+ pcdev->irq = irq;
ret = devm_request_threaded_irq(dev, irq, NULL, pse_isr,
IRQF_ONESHOT | irq_flags,
irq_name, h);
- if (ret)
+ if (ret) {
+ pcdev->irq = 0;
dev_err(pcdev->dev, "Failed to request IRQ %d\n", irq);
+ return ret;
+ }
- pcdev->irq = irq;
- return ret;
+ return 0;
}
EXPORT_SYMBOL_GPL(devm_pse_irq_helper);
--
2.43.0
^ permalink raw reply related [flat|nested] 27+ messages in thread
* [PATCH net v5 05/14] net: pse-pd: do not reach a freed PHY from the notification worker
2026-11-18 18:39 [PATCH net v5 00/14] net: pse-pd: assorted lifetime and error-path fixes Carlo Szelinsky
` (3 preceding siblings ...)
2026-11-18 18:39 ` [PATCH net v5 04/14] net: pse-pd: do not keep a failed IRQ in pcdev->irq Carlo Szelinsky
@ 2026-11-18 18:39 ` Carlo Szelinsky
2026-11-20 6:41 ` netdev-bot+sashiko
2026-11-18 18:39 ` [PATCH net v5 06/14] net: pse-pd: check the PI state under the lock when setting a power limit Carlo Szelinsky
` (8 subsequent siblings)
13 siblings, 1 reply; 27+ messages in thread
From: Carlo Szelinsky @ 2026-11-18 18:39 UTC (permalink / raw)
To: Oleksij Rempel, Kory Maincent, Andrew Lunn, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Mark Brown, Liam Girdwood, Maxime Chevallier,
Kyle Swenson, Ross Cawston, netdev-bot+sashiko, netdev,
linux-kernel, Carlo Szelinsky
The notification worker holds a reference on the pse_control, but not
on the PHY that psec->attached_phydev points to. If the PHY is removed
while the worker runs, the pse_control survives and the worker reads the
freed PHY. The same happens without a race when two PHYs share one PI:
attached_phydev keeps pointing at the first PHY after it is removed.
pse_control_put() unlinks and frees a pse_control under pse_list_mutex,
and phy_device_remove() calls it before the PHY is freed. So instead of
taking a reference, let the worker look the pse_control up and use
attached_phydev under pse_list_mutex. This nests pse_list_mutex inside
rtnl; nothing under pse_list_mutex takes rtnl.
For a PI shared by two PHYs there is no single netdev to report to, so
clear attached_phydev when a second PHY takes the pse_control, and warn.
Events for that PI are then no longer reported, even after one PHY goes
away. Failing the second PHY instead would fail its whole MDIO bus.
Triggered in QEMU (arm64, KASAN) with a test-only PSE controller:
removing a PHY while the worker waits for rtnl, and removing one of
two PHYs on a shared PI, both give
BUG: KASAN: slab-use-after-free in pse_send_ntf_worker+0x2b0/0x2bc
With this patch both are gone.
Fixes: fc0e6db30941 ("net: pse-pd: Add support for reporting events")
Reviewed-by: Kory Maincent <kory.maincent@bootlin.com>
Acked-by: Oleksij Rempel <o.rempel@pengutronix.de>
Signed-off-by: Carlo Szelinsky <github@szelinsky.de>
---
drivers/net/pse-pd/pse_core.c | 42 ++++++++++++++++++++++++-----------
1 file changed, 29 insertions(+), 13 deletions(-)
diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
index 0894613515e0..b8cba6f86a7f 100644
--- a/drivers/net/pse-pd/pse_core.c
+++ b/drivers/net/pse-pd/pse_core.c
@@ -31,7 +31,9 @@ static DEFINE_MUTEX(pse_pw_d_mutex);
* @list: list entry for the pcdev's PSE controller list
* @id: ID of the PSE line in the PSE controller device
* @refcnt: Number of gets of this pse_control
- * @attached_phydev: PHY device pointer attached by the PSE control
+ * @attached_phydev: PHY device pointer attached by the PSE control, or NULL
+ * when several PHYs share the PI. Read it only under
+ * pse_list_mutex.
*/
struct pse_control {
struct pse_controller_dev *pcdev;
@@ -238,24 +240,22 @@ static int of_load_pse_pis(struct pse_controller_dev *pcdev)
* @pcdev: a pointer to the PSE
* @id: index of the PSE control
*
- * Return: pse_control pointer or NULL. The device returned has had a
- * reference added and the pointer is safe until the user calls
- * pse_control_put() to indicate they have finished with it.
+ * Must be called with pse_list_mutex held. No reference is taken: the
+ * pointer is only valid until pse_list_mutex is released.
+ *
+ * Return: pse_control pointer or NULL.
*/
static struct pse_control *
pse_control_find_by_id(struct pse_controller_dev *pcdev, int id)
{
struct pse_control *psec;
- mutex_lock(&pse_list_mutex);
+ lockdep_assert_held(&pse_list_mutex);
+
list_for_each_entry(psec, &pcdev->pse_control_head, list) {
- if (psec->id == id) {
- kref_get(&psec->refcnt);
- mutex_unlock(&pse_list_mutex);
+ if (psec->id == id)
return psec;
- }
}
- mutex_unlock(&pse_list_mutex);
return NULL;
}
@@ -263,11 +263,14 @@ pse_control_find_by_id(struct pse_controller_dev *pcdev, int id)
* pse_control_get_netdev - Return netdev associated to a PSE control
* @psec: PSE control pointer
*
- * Return: netdev pointer or NULL
+ * Must be called with rtnl and pse_list_mutex held.
+ *
+ * Return: netdev pointer, or NULL if no single PHY is attached
*/
static struct net_device *pse_control_get_netdev(struct pse_control *psec)
{
ASSERT_RTNL();
+ lockdep_assert_held(&pse_list_mutex);
if (!psec || !psec->attached_phydev)
return NULL;
@@ -1014,13 +1017,18 @@ static void pse_send_ntf_worker(struct work_struct *work)
struct net_device *netdev;
struct pse_control *psec;
- psec = pse_control_find_by_id(pcdev, ntf.id);
+ /* pse_control_put() frees the pse_control under
+ * pse_list_mutex before its PHY is freed, so the mutex
+ * keeps attached_phydev valid.
+ */
rtnl_lock();
+ mutex_lock(&pse_list_mutex);
+ psec = pse_control_find_by_id(pcdev, ntf.id);
netdev = pse_control_get_netdev(psec);
if (netdev)
ethnl_pse_send_ntf(netdev, ntf.notifs);
+ mutex_unlock(&pse_list_mutex);
rtnl_unlock();
- pse_control_put(psec);
}
}
@@ -1410,6 +1418,14 @@ pse_control_get_internal(struct pse_controller_dev *pcdev, unsigned int index,
list_for_each_entry(psec, &pcdev->pse_control_head, list) {
if (psec->id == index) {
+ if (psec->attached_phydev &&
+ psec->attached_phydev != phydev) {
+ dev_warn(pcdev->dev,
+ "PI %u shared by several PHYs, not reporting its events\n",
+ index);
+ psec->attached_phydev = NULL;
+ }
+
kref_get(&psec->refcnt);
return psec;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 27+ messages in thread
* [PATCH net v5 06/14] net: pse-pd: check the PI state under the lock when setting a power limit
2026-11-18 18:39 [PATCH net v5 00/14] net: pse-pd: assorted lifetime and error-path fixes Carlo Szelinsky
` (4 preceding siblings ...)
2026-11-18 18:39 ` [PATCH net v5 05/14] net: pse-pd: do not reach a freed PHY from the notification worker Carlo Szelinsky
@ 2026-11-18 18:39 ` Carlo Szelinsky
2026-11-20 6:41 ` netdev-bot+sashiko
2026-11-18 18:39 ` [PATCH net v5 07/14] net: pse-pd: do not tie a shared power domain to its creator's devres Carlo Szelinsky
` (7 subsequent siblings)
13 siblings, 1 reply; 27+ messages in thread
From: Carlo Szelinsky @ 2026-11-18 18:39 UTC (permalink / raw)
To: Oleksij Rempel, Kory Maincent, Andrew Lunn, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Mark Brown, Liam Girdwood, Maxime Chevallier,
Kyle Swenson, Ross Cawston, netdev-bot+sashiko, netdev,
linux-kernel, Carlo Szelinsky
pse_ethtool_set_pw_limit() checks whether the PI is powered without
pcdev->lock. pse_isr() changes that state under the lock, so if the PD
disconnects in between, budget is reserved for a port that is no longer
powered. The next classification overwrites pw_allocated_mW without
freeing that budget, so it is lost until the controller is bound again.
Do the check in pse_pi_update_pw_budget() under the lock, and report
whether the budget was changed, so the rollback after a failed
regulator_set_current_limit() runs only then. This also rolls back a
budget that started from zero, which the old code skipped.
Found by code inspection. Not reproduced.
Fixes: ffef61d6d273 ("net: pse-pd: Add support for budget evaluation strategies")
Reviewed-by: Kory Maincent <kory.maincent@bootlin.com>
Acked-by: Oleksij Rempel <o.rempel@pengutronix.de>
Signed-off-by: Carlo Szelinsky <github@szelinsky.de>
---
drivers/net/pse-pd/pse_core.c | 41 ++++++++++++++++++++++-------------
1 file changed, 26 insertions(+), 15 deletions(-)
diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
index b8cba6f86a7f..342fb27e3831 100644
--- a/drivers/net/pse-pd/pse_core.c
+++ b/drivers/net/pse-pd/pse_core.c
@@ -1813,22 +1813,35 @@ EXPORT_SYMBOL_GPL(pse_ethtool_set_config);
* @pcdev: a pointer to the PSE controller device
* @id: index of the PSE PI
* @pw_req: power requested
+ * @updated: set to true if the budget was changed
* @extack: extack for reporting useful error messages
*
- * Return: Previous power allocated on success and failure value on error
+ * Return: Power allocated before the update, or a negative error code.
+ * 0 is also returned when the budget was not changed, so callers
+ * check @updated before they roll back.
*/
static int pse_pi_update_pw_budget(struct pse_controller_dev *pcdev, int id,
- const unsigned int pw_req,
+ const unsigned int pw_req, bool *updated,
struct netlink_ext_ack *extack)
{
struct pse_pi *pi = &pcdev->pi[id];
int previous_pw_allocated;
int pw_diff, ret = 0;
+ *updated = false;
+
/* We don't want pw_allocated_mW value change in the middle of an
* power budget update
*/
mutex_lock(&pcdev->lock);
+ /* Only a PI under software power control that is enabled and has a
+ * PD detected gets a budget. Check this here, as pse_isr() changes
+ * it under the same lock.
+ */
+ if (!pse_pw_d_is_sw_pw_control(pcdev, pi->pw_d) ||
+ !pi->admin_state_enabled || !pi->isr_pd_detected)
+ goto out;
+
previous_pw_allocated = pi->pw_allocated_mW;
pw_diff = pw_req - previous_pw_allocated;
if (!pw_diff) {
@@ -1846,6 +1859,7 @@ static int pse_pi_update_pw_budget(struct pse_controller_dev *pcdev, int id,
regulator_free_power_budget(pi->pw_d->supply, -pw_diff);
}
pi->pw_allocated_mW = pw_req;
+ *updated = true;
ret = previous_pw_allocated;
out:
@@ -1866,6 +1880,7 @@ int pse_ethtool_set_pw_limit(struct pse_control *psec,
const unsigned int pw_limit)
{
int uV, uA, ret, previous_pw_allocated = 0;
+ bool updated;
s64 tmp_64;
if (pw_limit > MAX_PI_PW)
@@ -1890,23 +1905,19 @@ int pse_ethtool_set_pw_limit(struct pse_control *psec,
uA = DIV_ROUND_CLOSEST_ULL(tmp_64, uV);
/* Update power budget only in software power control case and
- * if a Power Device is powered.
+ * if the PI is enabled and has a PD detected.
*/
- if (pse_pw_d_is_sw_pw_control(psec->pcdev,
- psec->pcdev->pi[psec->id].pw_d) &&
- psec->pcdev->pi[psec->id].admin_state_enabled &&
- psec->pcdev->pi[psec->id].isr_pd_detected) {
- ret = pse_pi_update_pw_budget(psec->pcdev, psec->id,
- pw_limit, extack);
- if (ret < 0)
- return ret;
- previous_pw_allocated = ret;
- }
+ ret = pse_pi_update_pw_budget(psec->pcdev, psec->id, pw_limit,
+ &updated, extack);
+ if (ret < 0)
+ return ret;
+ previous_pw_allocated = ret;
ret = regulator_set_current_limit(psec->ps, 0, uA);
- if (ret < 0 && previous_pw_allocated) {
+ if (ret < 0 && updated) {
pse_pi_update_pw_budget(psec->pcdev, psec->id,
- previous_pw_allocated, extack);
+ previous_pw_allocated, &updated,
+ extack);
}
return ret;
--
2.43.0
^ permalink raw reply related [flat|nested] 27+ messages in thread
* [PATCH net v5 07/14] net: pse-pd: do not tie a shared power domain to its creator's devres
2026-11-18 18:39 [PATCH net v5 00/14] net: pse-pd: assorted lifetime and error-path fixes Carlo Szelinsky
` (5 preceding siblings ...)
2026-11-18 18:39 ` [PATCH net v5 06/14] net: pse-pd: check the PI state under the lock when setting a power limit Carlo Szelinsky
@ 2026-11-18 18:39 ` Carlo Szelinsky
2026-11-20 6:41 ` netdev-bot+sashiko
2026-11-18 18:39 ` [PATCH net v5 08/14] net: pse-pd: stop async event sources before freeing PI data in unregister Carlo Szelinsky
` (6 subsequent siblings)
13 siblings, 1 reply; 27+ messages in thread
From: Carlo Szelinsky @ 2026-11-18 18:39 UTC (permalink / raw)
To: Oleksij Rempel, Kory Maincent, Andrew Lunn, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Mark Brown, Liam Girdwood, Maxime Chevallier,
Kyle Swenson, Ross Cawston, netdev-bot+sashiko, netdev,
linux-kernel, Carlo Szelinsky
A power domain is shared by all controllers on the same supply and
counted with a kref, but it is devm memory of the controller that
created it. When that controller unbinds while another one still uses
the domain, devres frees it anyway, and the other controller and the
global map keep pointing at freed memory. The same happens when
pse_register_pw_ds() fails part way: the domains it already set up stay
in the map, and devres frees them.
Allocate the domain with kzalloc() and free it on its last kref_put().
If pse_register_pw_ds() fails, it now gives back every domain it took
while it still holds pse_pw_d_mutex, so no other controller can pick one
up first.
Because the domain can now be freed at the put, pse_flush_pw_ds() clears
pi[].pw_d under pcdev->lock before the put, and pse_ethtool_set_prio()
checks it under that lock. The PI regulator ops and pse_isr() use it
under the same lock.
Triggered in QEMU (arm64, KASAN) with two test-only PSE controllers
on one supply: after the one that created the domain unbinds, binding
it again gives
BUG: KASAN: slab-use-after-free in pse_controller_register+0xb98/0x1274
With this patch it is gone.
Fixes: 50f8b341d268 ("net: pse-pd: Add support for PSE power domains")
Reviewed-by: Kory Maincent <kory.maincent@bootlin.com>
Signed-off-by: Carlo Szelinsky <github@szelinsky.de>
---
drivers/net/pse-pd/pse_core.c | 68 +++++++++++++++++++++++++++--------
1 file changed, 54 insertions(+), 14 deletions(-)
diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
index 342fb27e3831..8041ad3e330b 100644
--- a/drivers/net/pse-pd/pse_core.c
+++ b/drivers/net/pse-pd/pse_core.c
@@ -875,17 +875,41 @@ devm_pse_pi_regulator_register(struct pse_controller_dev *pcdev,
return 0;
}
-static void __pse_pw_d_release(struct kref *kref)
+/* Release for a put made with pse_pw_d_mutex already held. */
+static void pse_pw_d_release_locked(struct kref *kref)
{
struct pse_power_domain *pw_d = container_of(kref,
struct pse_power_domain,
refcnt);
+ lockdep_assert_held(&pse_pw_d_mutex);
regulator_put(pw_d->supply);
xa_erase(&pse_pw_d_map, pw_d->id);
+ kfree(pw_d);
+}
+
+static void __pse_pw_d_release(struct kref *kref)
+{
+ pse_pw_d_release_locked(kref);
mutex_unlock(&pse_pw_d_mutex);
}
+/* Clear pi[id].pw_d before the domain's reference is dropped. The PI
+ * regulator ops and pse_isr() hold pcdev->lock while they use it.
+ */
+static struct pse_power_domain *
+pse_pi_take_pw_d(struct pse_controller_dev *pcdev, int id)
+{
+ struct pse_power_domain *pw_d;
+
+ mutex_lock(&pcdev->lock);
+ pw_d = pcdev->pi[id].pw_d;
+ pcdev->pi[id].pw_d = NULL;
+ mutex_unlock(&pcdev->lock);
+
+ return pw_d;
+}
+
/**
* pse_flush_pw_ds - flush all PSE power domains of a PSE
* @pcdev: a pointer to the initialized PSE controller device
@@ -896,10 +920,11 @@ static void pse_flush_pw_ds(struct pse_controller_dev *pcdev)
int i;
for (i = 0; i < pcdev->nr_lines; i++) {
- if (!pcdev->pi[i].pw_d)
+ pw_d = pse_pi_take_pw_d(pcdev, i);
+ if (!pw_d)
continue;
- pw_d = xa_load(&pse_pw_d_map, pcdev->pi[i].pw_d->id);
+ pw_d = xa_load(&pse_pw_d_map, pw_d->id);
if (!pw_d)
continue;
@@ -909,24 +934,30 @@ static void pse_flush_pw_ds(struct pse_controller_dev *pcdev)
}
/**
- * devm_pse_alloc_pw_d - allocate a new PSE power domain for a device
- * @dev: device that is registering this PSE power domain
+ * pse_alloc_pw_d - allocate a new PSE power domain
+ *
+ * A power domain is shared by every controller whose PIs sit on the same
+ * supply, so it must not be tied to the lifetime of the controller that
+ * happened to create it. It is freed by pse_pw_d_release_locked() on the
+ * last kref_put().
*
* Return: Pointer to the newly allocated PSE power domain or error pointers
*/
-static struct pse_power_domain *devm_pse_alloc_pw_d(struct device *dev)
+static struct pse_power_domain *pse_alloc_pw_d(void)
{
struct pse_power_domain *pw_d;
int index, ret;
- pw_d = devm_kzalloc_obj(dev, *pw_d);
+ pw_d = kzalloc_obj(*pw_d);
if (!pw_d)
return ERR_PTR(-ENOMEM);
ret = xa_alloc(&pse_pw_d_map, &index, pw_d, XA_LIMIT(1, PSE_PW_D_LIMIT),
GFP_KERNEL);
- if (ret)
+ if (ret) {
+ kfree(pw_d);
return ERR_PTR(ret);
+ }
kref_init(&pw_d->refcnt);
pw_d->id = index;
@@ -972,7 +1003,7 @@ static int pse_register_pw_ds(struct pse_controller_dev *pcdev)
continue;
}
- pw_d = devm_pse_alloc_pw_d(pcdev->dev);
+ pw_d = pse_alloc_pw_d();
if (IS_ERR(pw_d)) {
ret = PTR_ERR(pw_d);
goto out;
@@ -981,6 +1012,7 @@ static int pse_register_pw_ds(struct pse_controller_dev *pcdev)
supply = regulator_get(&rdev->dev, rdev->supply_name);
if (IS_ERR(supply)) {
xa_erase(&pse_pw_d_map, pw_d->id);
+ kfree(pw_d);
ret = PTR_ERR(supply);
goto out;
}
@@ -995,6 +1027,13 @@ static int pse_register_pw_ds(struct pse_controller_dev *pcdev)
}
out:
+ /* Undo this call's domains before another controller can find them */
+ while (ret && i--) {
+ struct pse_power_domain *pw_d = pse_pi_take_pw_d(pcdev, i);
+
+ if (pw_d)
+ kref_put(&pw_d->refcnt, pse_pw_d_release_locked);
+ }
mutex_unlock(&pse_pw_d_mutex);
return ret;
}
@@ -1941,15 +1980,16 @@ int pse_ethtool_set_prio(struct pse_control *psec,
const struct pse_controller_ops *ops;
int ret = 0;
- if (!pcdev->pi[psec->id].pw_d) {
- NL_SET_ERR_MSG(extack, "no power domain attached");
- return -EOPNOTSUPP;
- }
-
/* We don't want priority change in the middle of an
* enable/disable call or a priority mode change
*/
mutex_lock(&pcdev->lock);
+ if (!pcdev->pi[psec->id].pw_d) {
+ NL_SET_ERR_MSG(extack, "no power domain attached");
+ ret = -EOPNOTSUPP;
+ goto out;
+ }
+
switch (pcdev->pi[psec->id].pw_d->budget_eval_strategy) {
case PSE_BUDGET_EVAL_STRAT_STATIC:
if (prio >= pcdev->nr_lines) {
--
2.43.0
^ permalink raw reply related [flat|nested] 27+ messages in thread
* [PATCH net v5 08/14] net: pse-pd: stop async event sources before freeing PI data in unregister
2026-11-18 18:39 [PATCH net v5 00/14] net: pse-pd: assorted lifetime and error-path fixes Carlo Szelinsky
` (6 preceding siblings ...)
2026-11-18 18:39 ` [PATCH net v5 07/14] net: pse-pd: do not tie a shared power domain to its creator's devres Carlo Szelinsky
@ 2026-11-18 18:39 ` Carlo Szelinsky
2026-11-20 6:41 ` netdev-bot+sashiko
2026-11-18 18:39 ` [PATCH net v5 09/14] net: pse-pd: unregister from the controller list before freeing PI data Carlo Szelinsky
` (5 subsequent siblings)
13 siblings, 1 reply; 27+ messages in thread
From: Carlo Szelinsky @ 2026-11-18 18:39 UTC (permalink / raw)
To: Oleksij Rempel, Kory Maincent, Andrew Lunn, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Mark Brown, Liam Girdwood, Maxime Chevallier,
Kyle Swenson, Ross Cawston, netdev-bot+sashiko, netdev,
linux-kernel, Carlo Szelinsky
pse_controller_unregister() flushes the power domains and frees the PI
array before it disables the IRQ, while pse_isr() uses both. No in-tree
driver hits this: tps23881, the only user of devm_pse_irq_helper(),
requests its IRQ after it registers the controller, so devres frees
the IRQ before unregister runs. A driver that keeps its IRQ across
unregister would read freed memory.
Disable the IRQ and drain the notification worker first, and only then
flush the power domains and release the PI array.
The worker can also be queued again after the drain. With the static
budget strategy, powering a PI can switch off a PI of lower priority
through pse_disable_pi_pol(), which queues the worker. A PI is powered
from pse_isr(), from a priority change, from the retry after a disable,
and when a consumer enables it. All of these go through pse_pi_enable()
or _pse_pi_delivery_power_sw_pw_ctrl().
So pse_controller_unregister() now first sets pcdev->unregistering
under pcdev->lock. After that, both functions return -ENODEV, and
pse_isr() does not try to power a PI, so it does not report a false
power control error. Disabling a PI still works.
The next patches in this series build on this order.
Found by code inspection and AI review.
Fixes: ffef61d6d273 ("net: pse-pd: Add support for budget evaluation strategies")
Reviewed-by: Kory Maincent <kory.maincent@bootlin.com>
Signed-off-by: Carlo Szelinsky <github@szelinsky.de>
---
drivers/net/pse-pd/pse_core.c | 31 ++++++++++++++++++++++++++++---
include/linux/pse-pd/pse.h | 3 +++
2 files changed, 31 insertions(+), 3 deletions(-)
diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
index 8041ad3e330b..22f2d8041bd4 100644
--- a/drivers/net/pse-pd/pse_core.c
+++ b/drivers/net/pse-pd/pse_core.c
@@ -616,6 +616,13 @@ static int _pse_pi_delivery_power_sw_pw_ctrl(struct pse_controller_dev *pcdev,
struct pse_pi *pi = &pcdev->pi[id];
int ret, pw_req;
+ /* pse_isr(), a priority change and the retry after a disable all
+ * power a PI through here, so stop them here once unregistering
+ * has started.
+ */
+ if (pcdev->unregistering)
+ return -ENODEV;
+
if (!ops->pi_get_pw_req) {
/* No power allocation management */
ret = ops->pi_enable(pcdev, id);
@@ -672,6 +679,11 @@ static int pse_pi_enable(struct regulator_dev *rdev)
id = rdev_get_id(rdev);
mutex_lock(&pcdev->lock);
+ if (pcdev->unregistering) {
+ mutex_unlock(&pcdev->lock);
+ return -ENODEV;
+ }
+
if (pse_pw_d_is_sw_pw_control(pcdev, pcdev->pi[id].pw_d)) {
/* Manage enabled status by software.
* Real enable process will happen if a port is connected.
@@ -1158,11 +1170,22 @@ EXPORT_SYMBOL_GPL(pse_controller_register);
*/
void pse_controller_unregister(struct pse_controller_dev *pcdev)
{
- pse_flush_pw_ds(pcdev);
- pse_release_pis(pcdev);
+ /* From now on the PSE core must not power a PI, so nothing queues
+ * a new notification once the worker is drained below.
+ */
+ mutex_lock(&pcdev->lock);
+ pcdev->unregistering = true;
+ mutex_unlock(&pcdev->lock);
+
+ /* Stop the IRQ and the notification worker first: pse_isr() uses
+ * pcdev->pi and the power domains that pse_flush_pw_ds() drops,
+ * and the worker reads the kfifo that is freed below.
+ */
if (pcdev->irq)
disable_irq(pcdev->irq);
cancel_work_sync(&pcdev->ntf_work);
+ pse_flush_pw_ds(pcdev);
+ pse_release_pis(pcdev);
kfifo_free(&pcdev->ntf_fifo);
mutex_lock(&pse_list_mutex);
list_del(&pcdev->list);
@@ -1266,7 +1289,9 @@ static int pse_set_config_isr(struct pse_controller_dev *pcdev, int id,
struct netlink_ext_ack extack;
pcdev->pi[id].isr_pd_detected = true;
- if (pcdev->pi[id].admin_state_enabled) {
+ /* Powering a PI fails once unregister started, do not try */
+ if (pcdev->pi[id].admin_state_enabled &&
+ !pcdev->unregistering) {
ret = _pse_pi_delivery_power_sw_pw_ctrl(pcdev, id,
&extack);
if (ret == -ERANGE)
diff --git a/include/linux/pse-pd/pse.h b/include/linux/pse-pd/pse.h
index 4e5696cfade7..a917ac32dc0c 100644
--- a/include/linux/pse-pd/pse.h
+++ b/include/linux/pse-pd/pse.h
@@ -298,6 +298,8 @@ struct pse_ntf {
* @ntf_work: workqueue for PSE notification management
* @ntf_fifo: PSE notifications FIFO
* @ntf_fifo_lock: protect @ntf_fifo writer
+ * @unregistering: set under @lock when unregistering starts; the PSE
+ * core switches no PI on after that
*/
struct pse_controller_dev {
const struct pse_controller_ops *ops;
@@ -317,6 +319,7 @@ struct pse_controller_dev {
struct work_struct ntf_work;
DECLARE_KFIFO_PTR(ntf_fifo, struct pse_ntf);
spinlock_t ntf_fifo_lock; /* Protect @ntf_fifo writer */
+ bool unregistering;
};
/**
--
2.43.0
^ permalink raw reply related [flat|nested] 27+ messages in thread
* [PATCH net v5 09/14] net: pse-pd: unregister from the controller list before freeing PI data
2026-11-18 18:39 [PATCH net v5 00/14] net: pse-pd: assorted lifetime and error-path fixes Carlo Szelinsky
` (7 preceding siblings ...)
2026-11-18 18:39 ` [PATCH net v5 08/14] net: pse-pd: stop async event sources before freeing PI data in unregister Carlo Szelinsky
@ 2026-11-18 18:39 ` Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 10/14] net: pse-pd: keep the PI data until the PI regulators are gone Carlo Szelinsky
` (4 subsequent siblings)
13 siblings, 0 replies; 27+ messages in thread
From: Carlo Szelinsky @ 2026-11-18 18:39 UTC (permalink / raw)
To: Oleksij Rempel, Kory Maincent, Andrew Lunn, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Mark Brown, Liam Girdwood, Maxime Chevallier,
Kyle Swenson, Ross Cawston, netdev-bot+sashiko, netdev,
linux-kernel, Carlo Szelinsky
pse_controller_unregister() frees the PI array while the controller is
still on pse_controller_list, and only removes it from the list
afterwards. of_pse_control_get() walks that list under pse_list_mutex
and reads pcdev->pi[i].np in of_pse_match_pi(). A consumer that probes
at that moment can read the freed PI array.
Remove the controller from the list first, before the worker is
drained, the power domains are flushed and the PI array is freed. Both
sides take pse_list_mutex, so once the controller is off the list, no
new lookup can find it, and a lookup that is already running finishes
before the PI array is freed.
Found by code inspection. Not reproduced.
Fixes: 9be9567a7c59 ("net: pse-pd: Add support for PSE PIs")
Reviewed-by: Kory Maincent <kory.maincent@bootlin.com>
Acked-by: Oleksij Rempel <o.rempel@pengutronix.de>
Signed-off-by: Carlo Szelinsky <github@szelinsky.de>
---
drivers/net/pse-pd/pse_core.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
index 22f2d8041bd4..edca02438727 100644
--- a/drivers/net/pse-pd/pse_core.c
+++ b/drivers/net/pse-pd/pse_core.c
@@ -1183,13 +1183,16 @@ void pse_controller_unregister(struct pse_controller_dev *pcdev)
*/
if (pcdev->irq)
disable_irq(pcdev->irq);
+ /* Unlink before the PI array is freed: of_pse_control_get() walks
+ * the list and reads pcdev->pi[].
+ */
+ mutex_lock(&pse_list_mutex);
+ list_del(&pcdev->list);
+ mutex_unlock(&pse_list_mutex);
cancel_work_sync(&pcdev->ntf_work);
pse_flush_pw_ds(pcdev);
pse_release_pis(pcdev);
kfifo_free(&pcdev->ntf_fifo);
- mutex_lock(&pse_list_mutex);
- list_del(&pcdev->list);
- mutex_unlock(&pse_list_mutex);
}
EXPORT_SYMBOL_GPL(pse_controller_unregister);
--
2.43.0
^ permalink raw reply related [flat|nested] 27+ messages in thread
* [PATCH net v5 10/14] net: pse-pd: keep the PI data until the PI regulators are gone
2026-11-18 18:39 [PATCH net v5 00/14] net: pse-pd: assorted lifetime and error-path fixes Carlo Szelinsky
` (8 preceding siblings ...)
2026-11-18 18:39 ` [PATCH net v5 09/14] net: pse-pd: unregister from the controller list before freeing PI data Carlo Szelinsky
@ 2026-11-18 18:39 ` Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 11/14] net: pse-pd: free the notification kfifo when registration fails Carlo Szelinsky
` (3 subsequent siblings)
13 siblings, 0 replies; 27+ messages in thread
From: Carlo Szelinsky @ 2026-11-18 18:39 UTC (permalink / raw)
To: Oleksij Rempel, Kory Maincent, Andrew Lunn, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Mark Brown, Liam Girdwood, Maxime Chevallier,
Kyle Swenson, Ross Cawston, netdev-bot+sashiko, netdev,
linux-kernel, Carlo Szelinsky
pse_controller_unregister() frees the PI array, but the PI regulators
are only unregistered after it, by devres. Until then their ops still
run, for example on a read of the regulator's sysfs "state" or when a
consumer enables or disables the PI, and they read the freed array.
Free the PI array from a devm action that is added before the PI
regulators, so devres frees it after them. This also frees it when
registration fails, where it was leaked before.
A consumer that still holds the PI after the unbind is an older
problem that this patch does not fix, see the cover letter.
This needs "net: pse-pd: do not tie a shared power domain to its
creator's devres" and "net: pse-pd: stop async event sources before
freeing PI data in unregister". Without them, the regulator ops still
reach a freed power domain, or a kfifo and work item that are already
torn down.
Triggered in QEMU (arm64, KASAN) with a test-only PSE controller:
reading the PI regulators' sysfs "state" while the controller unbinds
gives
BUG: KASAN: slab-use-after-free in pse_pi_is_enabled+0x294/0x2d4
With this patch it is gone.
Fixes: d83e13761d5b ("net: pse-pd: Use regulator framework within PSE framework")
Reviewed-by: Kory Maincent <kory.maincent@bootlin.com>
Signed-off-by: Carlo Szelinsky <github@szelinsky.de>
---
drivers/net/pse-pd/pse_core.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
index edca02438727..134c273487ba 100644
--- a/drivers/net/pse-pd/pse_core.c
+++ b/drivers/net/pse-pd/pse_core.c
@@ -149,6 +149,11 @@ static void pse_release_pis(struct pse_controller_dev *pcdev)
kfree(pcdev->pi);
}
+static void devm_pse_release_pis(void *data)
+{
+ pse_release_pis(data);
+}
+
/**
* of_load_pse_pis - load all the PSE PIs
* @pcdev: a pointer to the PSE controller device
@@ -1121,6 +1126,13 @@ int pse_controller_register(struct pse_controller_dev *pcdev)
if (ret)
return ret;
+ /* The PI regulator ops use pcdev->pi until devres unregisters the
+ * regulators, so free it from devres too, after them.
+ */
+ ret = devm_add_action_or_reset(pcdev->dev, devm_pse_release_pis, pcdev);
+ if (ret)
+ return ret;
+
if (pcdev->ops->setup_pi_matrix) {
ret = pcdev->ops->setup_pi_matrix(pcdev);
if (ret)
@@ -1191,7 +1203,6 @@ void pse_controller_unregister(struct pse_controller_dev *pcdev)
mutex_unlock(&pse_list_mutex);
cancel_work_sync(&pcdev->ntf_work);
pse_flush_pw_ds(pcdev);
- pse_release_pis(pcdev);
kfifo_free(&pcdev->ntf_fifo);
}
EXPORT_SYMBOL_GPL(pse_controller_unregister);
--
2.43.0
^ permalink raw reply related [flat|nested] 27+ messages in thread
* [PATCH net v5 11/14] net: pse-pd: free the notification kfifo when registration fails
2026-11-18 18:39 [PATCH net v5 00/14] net: pse-pd: assorted lifetime and error-path fixes Carlo Szelinsky
` (9 preceding siblings ...)
2026-11-18 18:39 ` [PATCH net v5 10/14] net: pse-pd: keep the PI data until the PI regulators are gone Carlo Szelinsky
@ 2026-11-18 18:39 ` Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 12/14] net: pse-pd: use each PI's own supply handle for the power budget Carlo Szelinsky
` (2 subsequent siblings)
13 siblings, 0 replies; 27+ messages in thread
From: Carlo Szelinsky @ 2026-11-18 18:39 UTC (permalink / raw)
To: Oleksij Rempel, Kory Maincent, Andrew Lunn, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Mark Brown, Liam Girdwood, Maxime Chevallier,
Kyle Swenson, Ross Cawston, netdev-bot+sashiko, netdev,
linux-kernel, Carlo Szelinsky
When pse_controller_register() fails after it allocated the
notification kfifo, it returns without freeing it.
Free the kfifo on every error path.
Found by code inspection and AI review.
Fixes: ffef61d6d273 ("net: pse-pd: Add support for budget evaluation strategies")
Reviewed-by: Kory Maincent <kory.maincent@bootlin.com>
Signed-off-by: Carlo Szelinsky <github@szelinsky.de>
---
drivers/net/pse-pd/pse_core.c | 24 ++++++++++++++++--------
1 file changed, 16 insertions(+), 8 deletions(-)
diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
index 134c273487ba..c86e2c44e3ff 100644
--- a/drivers/net/pse-pd/pse_core.c
+++ b/drivers/net/pse-pd/pse_core.c
@@ -1119,24 +1119,25 @@ int pse_controller_register(struct pse_controller_dev *pcdev)
!pcdev->ops->pi_get_pw_status) {
dev_err(pcdev->dev,
"Mandatory status report callbacks are missing");
- return -EINVAL;
+ ret = -EINVAL;
+ goto free_kfifo;
}
ret = of_load_pse_pis(pcdev);
if (ret)
- return ret;
+ goto free_kfifo;
/* The PI regulator ops use pcdev->pi until devres unregisters the
* regulators, so free it from devres too, after them.
*/
ret = devm_add_action_or_reset(pcdev->dev, devm_pse_release_pis, pcdev);
if (ret)
- return ret;
+ goto free_kfifo;
if (pcdev->ops->setup_pi_matrix) {
ret = pcdev->ops->setup_pi_matrix(pcdev);
if (ret)
- return ret;
+ goto free_kfifo;
}
/* Each regulator name len is pcdev dev name + 7 char +
@@ -1153,26 +1154,33 @@ int pse_controller_register(struct pse_controller_dev *pcdev)
continue;
reg_name = devm_kzalloc(pcdev->dev, reg_name_len, GFP_KERNEL);
- if (!reg_name)
- return -ENOMEM;
+ if (!reg_name) {
+ ret = -ENOMEM;
+ goto free_kfifo;
+ }
snprintf(reg_name, reg_name_len, "pse-%s_pi%d",
dev_name(pcdev->dev), i);
ret = devm_pse_pi_regulator_register(pcdev, reg_name, i);
if (ret)
- return ret;
+ goto free_kfifo;
}
ret = pse_register_pw_ds(pcdev);
if (ret)
- return ret;
+ goto free_kfifo;
mutex_lock(&pse_list_mutex);
list_add(&pcdev->list, &pse_controller_list);
mutex_unlock(&pse_list_mutex);
return 0;
+
+free_kfifo:
+ cancel_work_sync(&pcdev->ntf_work);
+ kfifo_free(&pcdev->ntf_fifo);
+ return ret;
}
EXPORT_SYMBOL_GPL(pse_controller_register);
--
2.43.0
^ permalink raw reply related [flat|nested] 27+ messages in thread
* [PATCH net v5 12/14] net: pse-pd: use each PI's own supply handle for the power budget
2026-11-18 18:39 [PATCH net v5 00/14] net: pse-pd: assorted lifetime and error-path fixes Carlo Szelinsky
` (10 preceding siblings ...)
2026-11-18 18:39 ` [PATCH net v5 11/14] net: pse-pd: free the notification kfifo when registration fails Carlo Szelinsky
@ 2026-11-18 18:39 ` Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 13/14] net: pse-pd: pd692x0: release the manager power budget through devres Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 14/14] net: pse-pd: pd692x0: defer probe while a manager supply is unresolved Carlo Szelinsky
13 siblings, 0 replies; 27+ messages in thread
From: Carlo Szelinsky @ 2026-11-18 18:39 UTC (permalink / raw)
To: Oleksij Rempel, Kory Maincent, Andrew Lunn, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Mark Brown, Liam Girdwood, Maxime Chevallier,
Kyle Swenson, Ross Cawston, netdev-bot+sashiko, netdev,
linux-kernel, Carlo Szelinsky
A power domain shared by several controllers holds one supply handle.
Its consumer device is a PI regulator of the controller that created
the domain. When that controller unbinds, devres frees the regulator,
but the other controllers still use the domain and its handle, which
now points at a freed device. Reading the regulator summary in debugfs
then reads freed memory.
The domain does not need a handle of its own. The budget is counted in
the supply's regulator_dev, and each PI regulator already holds a
handle to that supply, which lives as long as the PI regulator. Use
that handle for the budget, and drop the domain's handle.
pse_flush_pw_ds() clears a PI's pw_d before devres unregisters the PI
regulator, so the budget is never used through a handle that is gone.
To find the domain of a new PI, compare its supply with the supply of a
PI already in the domain. For this, keep a list of the PIs of each
domain under pse_pw_d_mutex, which pse_flush_pw_ds() now holds for its
whole loop.
Triggered in QEMU (arm64, KASAN) with two test-only PSE controllers on
one supply: after the one that created the domain unbinds, reading
/sys/kernel/debug/regulator/regulator_summary gives
BUG: KASAN: slab-use-after-free in regulator_summary_show_subtree+0x770/0x9c4
With this patch the report is gone. A second test, where one controller
changes its power limit in a loop while the other one unbinds, is clean
too.
Fixes: 50f8b341d268 ("net: pse-pd: Add support for PSE power domains")
Reviewed-by: Kory Maincent <kory.maincent@bootlin.com>
Signed-off-by: Carlo Szelinsky <github@szelinsky.de>
---
drivers/net/pse-pd/pse_core.c | 70 +++++++++++++++++------------------
include/linux/pse-pd/pse.h | 2 +
2 files changed, 35 insertions(+), 37 deletions(-)
diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
index c86e2c44e3ff..0477cf6787bd 100644
--- a/drivers/net/pse-pd/pse_core.c
+++ b/drivers/net/pse-pd/pse_core.c
@@ -47,16 +47,17 @@ struct pse_control {
/**
* struct pse_power_domain - a PSE power domain
* @id: ID of the power domain
- * @supply: Power supply the Power Domain
* @refcnt: Number of gets of this pse_power_domain
* @budget_eval_strategy: Current power budget evaluation strategy of the
* power domain
+ * @pis: PIs that use the power domain, under pse_pw_d_mutex. The supply
+ * handles of their regulators all point at the same supply.
*/
struct pse_power_domain {
int id;
- struct regulator *supply;
struct kref refcnt;
u32 budget_eval_strategy;
+ struct list_head pis;
};
static int of_load_single_pse_pi_pairset(struct device_node *node,
@@ -446,7 +447,7 @@ static void pse_pi_deallocate_pw_budget(struct pse_pi *pi)
if (!pi->pw_d || !pi->pw_allocated_mW)
return;
- regulator_free_power_budget(pi->pw_d->supply, pi->pw_allocated_mW);
+ regulator_free_power_budget(pi->rdev->supply, pi->pw_allocated_mW);
pi->pw_allocated_mW = 0;
}
@@ -559,7 +560,8 @@ pse_pi_allocate_pw_budget_static_prio(struct pse_controller_dev *pcdev, int id,
int ret, _prio;
_prio = pcdev->nr_lines;
- while (regulator_request_power_budget(pi->pw_d->supply, pw_req) == -ERANGE) {
+ while (regulator_request_power_budget(pi->rdev->supply, pw_req) ==
+ -ERANGE) {
if (_prio <= pi->prio) {
NL_SET_ERR_MSG_FMT(extack,
"PI %d: not enough power budget available",
@@ -893,24 +895,17 @@ devm_pse_pi_regulator_register(struct pse_controller_dev *pcdev,
}
/* Release for a put made with pse_pw_d_mutex already held. */
-static void pse_pw_d_release_locked(struct kref *kref)
+static void pse_pw_d_release(struct kref *kref)
{
struct pse_power_domain *pw_d = container_of(kref,
struct pse_power_domain,
refcnt);
lockdep_assert_held(&pse_pw_d_mutex);
- regulator_put(pw_d->supply);
xa_erase(&pse_pw_d_map, pw_d->id);
kfree(pw_d);
}
-static void __pse_pw_d_release(struct kref *kref)
-{
- pse_pw_d_release_locked(kref);
- mutex_unlock(&pse_pw_d_mutex);
-}
-
/* Clear pi[id].pw_d before the domain's reference is dropped. The PI
* regulator ops and pse_isr() hold pcdev->lock while they use it.
*/
@@ -927,6 +922,14 @@ pse_pi_take_pw_d(struct pse_controller_dev *pcdev, int id)
return pw_d;
}
+/* Drop @pi from @pw_d, with pse_pw_d_mutex held */
+static void pse_pw_d_leave(struct pse_power_domain *pw_d, struct pse_pi *pi)
+{
+ lockdep_assert_held(&pse_pw_d_mutex);
+ list_del(&pi->pw_d_node);
+ kref_put(&pw_d->refcnt, pse_pw_d_release);
+}
+
/**
* pse_flush_pw_ds - flush all PSE power domains of a PSE
* @pcdev: a pointer to the initialized PSE controller device
@@ -936,18 +939,13 @@ static void pse_flush_pw_ds(struct pse_controller_dev *pcdev)
struct pse_power_domain *pw_d;
int i;
+ mutex_lock(&pse_pw_d_mutex);
for (i = 0; i < pcdev->nr_lines; i++) {
pw_d = pse_pi_take_pw_d(pcdev, i);
- if (!pw_d)
- continue;
-
- pw_d = xa_load(&pse_pw_d_map, pw_d->id);
- if (!pw_d)
- continue;
-
- kref_put_mutex(&pw_d->refcnt, __pse_pw_d_release,
- &pse_pw_d_mutex);
+ if (pw_d)
+ pse_pw_d_leave(pw_d, &pcdev->pi[i]);
}
+ mutex_unlock(&pse_pw_d_mutex);
}
/**
@@ -955,8 +953,8 @@ static void pse_flush_pw_ds(struct pse_controller_dev *pcdev)
*
* A power domain is shared by every controller whose PIs sit on the same
* supply, so it must not be tied to the lifetime of the controller that
- * happened to create it. It is freed by pse_pw_d_release_locked() on the
- * last kref_put().
+ * happened to create it. It is freed by pse_pw_d_release() on the last
+ * kref_put().
*
* Return: Pointer to the newly allocated PSE power domain or error pointers
*/
@@ -995,7 +993,6 @@ static int pse_register_pw_ds(struct pse_controller_dev *pcdev)
for (i = 0; i < pcdev->nr_lines; i++) {
struct regulator_dev *rdev = pcdev->pi[i].rdev;
struct pse_power_domain *pw_d;
- struct regulator *supply;
bool present = false;
unsigned long index;
@@ -1006,10 +1003,15 @@ static int pse_register_pw_ds(struct pse_controller_dev *pcdev)
continue;
xa_for_each(&pse_pw_d_map, index, pw_d) {
+ struct pse_pi *pi = list_first_entry(&pw_d->pis,
+ struct pse_pi,
+ pw_d_node);
+
/* Power supply already registered as a PSE power
* domain.
*/
- if (regulator_is_equal(pw_d->supply, rdev->supply)) {
+ if (regulator_is_equal(pi->rdev->supply,
+ rdev->supply)) {
present = true;
pcdev->pi[i].pw_d = pw_d;
break;
@@ -1017,6 +1019,7 @@ static int pse_register_pw_ds(struct pse_controller_dev *pcdev)
}
if (present) {
kref_get(&pw_d->refcnt);
+ list_add_tail(&pcdev->pi[i].pw_d_node, &pw_d->pis);
continue;
}
@@ -1026,20 +1029,13 @@ static int pse_register_pw_ds(struct pse_controller_dev *pcdev)
goto out;
}
- supply = regulator_get(&rdev->dev, rdev->supply_name);
- if (IS_ERR(supply)) {
- xa_erase(&pse_pw_d_map, pw_d->id);
- kfree(pw_d);
- ret = PTR_ERR(supply);
- goto out;
- }
-
- pw_d->supply = supply;
if (pcdev->supp_budget_eval_strategies)
pw_d->budget_eval_strategy = pcdev->supp_budget_eval_strategies;
else
pw_d->budget_eval_strategy = PSE_BUDGET_EVAL_STRAT_DISABLED;
kref_init(&pw_d->refcnt);
+ INIT_LIST_HEAD(&pw_d->pis);
+ list_add_tail(&pcdev->pi[i].pw_d_node, &pw_d->pis);
pcdev->pi[i].pw_d = pw_d;
}
@@ -1049,7 +1045,7 @@ static int pse_register_pw_ds(struct pse_controller_dev *pcdev)
struct pse_power_domain *pw_d = pse_pi_take_pw_d(pcdev, i);
if (pw_d)
- kref_put(&pw_d->refcnt, pse_pw_d_release_locked);
+ pse_pw_d_leave(pw_d, &pcdev->pi[i]);
}
mutex_unlock(&pse_pw_d_mutex);
return ret;
@@ -1933,7 +1929,7 @@ static int pse_pi_update_pw_budget(struct pse_controller_dev *pcdev, int id,
if (!pw_diff) {
goto out;
} else if (pw_diff > 0) {
- ret = regulator_request_power_budget(pi->pw_d->supply, pw_diff);
+ ret = regulator_request_power_budget(pi->rdev->supply, pw_diff);
if (ret) {
NL_SET_ERR_MSG_FMT(extack,
"PI %d: not enough power budget available",
@@ -1942,7 +1938,7 @@ static int pse_pi_update_pw_budget(struct pse_controller_dev *pcdev, int id,
}
} else {
- regulator_free_power_budget(pi->pw_d->supply, -pw_diff);
+ regulator_free_power_budget(pi->rdev->supply, -pw_diff);
}
pi->pw_allocated_mW = pw_req;
*updated = true;
diff --git a/include/linux/pse-pd/pse.h b/include/linux/pse-pd/pse.h
index a917ac32dc0c..17738ffcd224 100644
--- a/include/linux/pse-pd/pse.h
+++ b/include/linux/pse-pd/pse.h
@@ -247,6 +247,7 @@ struct pse_pi_pairset {
* @rdev: regulator represented by the PSE PI
* @admin_state_enabled: PI enabled state
* @pw_d: Power domain of the PSE PI
+ * @pw_d_node: entry in the list of PIs that use @pw_d
* @prio: Priority of the PSE PI. Used in static budget evaluation strategy
* @isr_pd_detected: PSE PI detection status managed by the interruption
* handler. This variable is relevant when the power enabled
@@ -261,6 +262,7 @@ struct pse_pi {
struct regulator_dev *rdev;
bool admin_state_enabled;
struct pse_power_domain *pw_d;
+ struct list_head pw_d_node;
int prio;
bool isr_pd_detected;
int pw_allocated_mW;
--
2.43.0
^ permalink raw reply related [flat|nested] 27+ messages in thread
* [PATCH net v5 13/14] net: pse-pd: pd692x0: release the manager power budget through devres
2026-11-18 18:39 [PATCH net v5 00/14] net: pse-pd: assorted lifetime and error-path fixes Carlo Szelinsky
` (11 preceding siblings ...)
2026-11-18 18:39 ` [PATCH net v5 12/14] net: pse-pd: use each PI's own supply handle for the power budget Carlo Szelinsky
@ 2026-11-18 18:39 ` Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 14/14] net: pse-pd: pd692x0: defer probe while a manager supply is unresolved Carlo Szelinsky
13 siblings, 0 replies; 27+ messages in thread
From: Carlo Szelinsky @ 2026-11-18 18:39 UTC (permalink / raw)
To: Oleksij Rempel, Kory Maincent, Andrew Lunn, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Mark Brown, Liam Girdwood, Maxime Chevallier,
Kyle Swenson, Ross Cawston, netdev-bot+sashiko, netdev,
linux-kernel, Carlo Szelinsky
pd692x0 reserves a power budget on each manager's supply in
setup_pi_matrix(), but gives it back only on that function's own errors
and in remove(). If probe fails later, or the request fails for one
manager after others succeeded, the budget stays reserved on vmain. Each
probe retry, e.g. after -EPROBE_DEFER, reserves it again until nothing
is left.
Release the budget from a devm action instead, and drop the explicit
release calls.
Found by code inspection. Not reproduced.
Fixes: 1c67f9c54cdc ("net: pse-pd: pd692x0: Fix power budget leak in manager setup error path")
Reviewed-by: Kory Maincent <kory.maincent@bootlin.com>
Signed-off-by: Carlo Szelinsky <github@szelinsky.de>
---
drivers/net/pse-pd/pd692x0.c | 22 +++++++++++++++-------
1 file changed, 15 insertions(+), 7 deletions(-)
diff --git a/drivers/net/pse-pd/pd692x0.c b/drivers/net/pse-pd/pd692x0.c
index 7958f5741463..019bc5861a8e 100644
--- a/drivers/net/pse-pd/pd692x0.c
+++ b/drivers/net/pse-pd/pd692x0.c
@@ -1223,8 +1223,9 @@ static void pd692x0_of_put_managers(struct pd692x0_priv *priv,
}
}
-static void pd692x0_managers_free_pw_budget(struct pd692x0_priv *priv)
+static void pd692x0_managers_free_pw_budget(void *data)
{
+ struct pd692x0_priv *priv = data;
int i;
for (i = 0; i < PD692X0_MAX_MANAGERS; i++) {
@@ -1278,31 +1279,39 @@ static int pd692x0_setup_pi_matrix(struct pse_controller_dev *pcdev)
if (ret)
goto err_of_managers;
+ /* Frees whatever pd692x0_req_managers_pw_budget() manages to
+ * request, on any later probe failure and on unbind. Added after
+ * the manager regulators so that it runs before devres unregisters
+ * them and their supply goes away.
+ */
+ ret = devm_add_action_or_reset(&priv->client->dev,
+ pd692x0_managers_free_pw_budget, priv);
+ if (ret)
+ goto err_of_managers;
+
ret = pd692x0_req_managers_pw_budget(priv);
if (ret)
goto err_of_managers;
ret = pd692x0_set_ports_matrix(priv, manager);
if (ret)
- goto err_managers_req_pw;
+ goto err_of_managers;
/* Do not init the conf if it is already saved */
if (!priv->cfg_saved) {
ret = pd692x0_hw_conf_init(priv);
if (ret)
- goto err_managers_req_pw;
+ goto err_of_managers;
ret = pd692x0_save_user_byte(priv);
if (ret)
- goto err_managers_req_pw;
+ goto err_of_managers;
}
pd692x0_of_put_managers(priv, manager);
kfree(manager);
return 0;
-err_managers_req_pw:
- pd692x0_managers_free_pw_budget(priv);
err_of_managers:
pd692x0_of_put_managers(priv, manager);
err_free_manager:
@@ -1854,7 +1863,6 @@ static void pd692x0_i2c_remove(struct i2c_client *client)
{
struct pd692x0_priv *priv = i2c_get_clientdata(client);
- pd692x0_managers_free_pw_budget(priv);
firmware_upload_unregister(priv->fwl);
}
--
2.43.0
^ permalink raw reply related [flat|nested] 27+ messages in thread
* [PATCH net v5 14/14] net: pse-pd: pd692x0: defer probe while a manager supply is unresolved
2026-11-18 18:39 [PATCH net v5 00/14] net: pse-pd: assorted lifetime and error-path fixes Carlo Szelinsky
` (12 preceding siblings ...)
2026-11-18 18:39 ` [PATCH net v5 13/14] net: pse-pd: pd692x0: release the manager power budget through devres Carlo Szelinsky
@ 2026-11-18 18:39 ` Carlo Szelinsky
13 siblings, 0 replies; 27+ messages in thread
From: Carlo Szelinsky @ 2026-11-18 18:39 UTC (permalink / raw)
To: Oleksij Rempel, Kory Maincent, Andrew Lunn, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Mark Brown, Liam Girdwood, Maxime Chevallier,
Kyle Swenson, Ross Cawston, netdev-bot+sashiko, netdev,
linux-kernel, Carlo Szelinsky
pd692x0_req_managers_pw_budget() uses each manager regulator's supply
without a NULL check. The supply is NULL while the vmain regulator is
described but not registered yet, because regulator_register() does not
wait for its supply. Probe then crashes.
Return -EPROBE_DEFER in that case.
Found by code inspection. Not reproduced.
Fixes: 359754013e6a ("net: pse-pd: pd692x0: Add support for PSE PI priority feature")
Reviewed-by: Kory Maincent <kory.maincent@bootlin.com>
Signed-off-by: Carlo Szelinsky <github@szelinsky.de>
---
drivers/net/pse-pd/pd692x0.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/drivers/net/pse-pd/pd692x0.c b/drivers/net/pse-pd/pd692x0.c
index 019bc5861a8e..667e0b818811 100644
--- a/drivers/net/pse-pd/pd692x0.c
+++ b/drivers/net/pse-pd/pd692x0.c
@@ -1055,6 +1055,15 @@ pd692x0_req_managers_pw_budget(struct pd692x0_priv *priv)
struct regulator *supply = priv->manager_reg[i]->supply;
int pw_budget;
+ /* The regulator core registers the manager regulator even
+ * when its vmain supply cannot be resolved yet and retries
+ * later, leaving ->supply NULL until then.
+ */
+ if (!supply)
+ return dev_err_probe(&priv->client->dev, -EPROBE_DEFER,
+ "manager %d: vmain supply not resolved\n",
+ i);
+
pw_budget = regulator_get_unclaimed_power_budget(supply);
if (!pw_budget)
/* Do nothing if no power budget */
--
2.43.0
^ permalink raw reply related [flat|nested] 27+ messages in thread
* Re: [PATCH net v5 04/14] net: pse-pd: do not keep a failed IRQ in pcdev->irq
2026-11-18 18:39 ` [PATCH net v5 04/14] net: pse-pd: do not keep a failed IRQ in pcdev->irq Carlo Szelinsky
@ 2026-11-20 6:41 ` netdev-bot+sashiko
2026-11-20 8:32 ` Carlo Szelinsky
0 siblings, 1 reply; 27+ messages in thread
From: netdev-bot+sashiko @ 2026-11-20 6:41 UTC (permalink / raw)
To: github
Cc: o.rempel, kory.maincent, andrew+netdev, davem, edumazet, kuba,
pabeni, horms, broonie, lgirdwood, maxime.chevallier,
kyle.swenson, ross, netdev-bot+sashiko, netdev, linux-kernel
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] This patch keeps pcdev->irq accurate only when the IRQ request
fails.
--
net: pse-pd: do not keep a failed IRQ in pcdev->irq
This patch clears pcdev->irq in devm_pse_irq_helper() when
devm_request_threaded_irq() fails. Without that, the failed probe's
pse_controller_unregister() calls disable_irq() on a line the controller
does not own. pcdev->irq is now set before the request because pse_isr()
can read it as soon as the IRQ is enabled.
> diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
> index 0cce17670d0b9..0894613515e0d 100644
> --- a/drivers/net/pse-pd/pse_core.c
> +++ b/drivers/net/pse-pd/pse_core.c
> @@ -1341,14 +1341,20 @@ int devm_pse_irq_helper(struct pse_controller_dev *pcdev, int irq,
> if (!h->notifs_mask)
> return -ENOMEM;
>
> + /* pse_isr() can run as soon as the IRQ is enabled, and
> + * pse_pw_d_is_sw_pw_control() reads pcdev->irq.
> + */
> + pcdev->irq = irq;
> ret = devm_request_threaded_irq(dev, irq, NULL, pse_isr,
> IRQF_ONESHOT | irq_flags,
> irq_name, h);
> - if (ret)
> + if (ret) {
> + pcdev->irq = 0;
> dev_err(pcdev->dev, "Failed to request IRQ %d\n", irq);
> + return ret;
> + }
[Severity: Medium]
This covers the failed request. Can pcdev->irq still go stale on a normal
unbind?
The only caller of devm_pse_irq_helper() is tps23881. It registers the
controller devres first and requests the IRQ after that:
drivers/net/pse-pd/tps23881.c:tps23881_i2c_probe() {
...
ret = devm_pse_controller_register(dev, &priv->pcdev);
...
return tps23881_setup_irq(priv, client->irq);
}
tps23881_setup_irq() then calls devm_pse_irq_helper(). Devres releases in
LIFO order, so on unbind or rmmod the teardown runs:
devres_release_all()
devm_irq_release()
free_irq()
devm_pse_controller_release()
pse_controller_unregister()
By that point pse_controller_unregister() no longer owns the line, but it
still does:
if (pcdev->irq)
disable_irq(pcdev->irq);
This is the same problem the commit message describes, except that the
stale pcdev->irq comes from devres teardown and not from a failed request.
Suppose another device requests the same line between free_irq() and
disable_irq(). __setup_irq() resets the depth to 0, and this disable_irq()
then leaves the line masked for that device, with no matching
enable_irq().
A future caller passing IRQF_SHARED through the exported irq_flags
argument would hit this on every unbind, because the other sharers stay on
the line after free_irq().
Could pcdev->irq also be cleared when the devm IRQ is released? One option
is a devm action registered before devm_request_threaded_irq() that resets
pcdev->irq. Then pse_controller_unregister() would skip disable_irq() on
this path too.
>
> - pcdev->irq = irq;
> - return ret;
> + return 0;
> }
> EXPORT_SYMBOL_GPL(devm_pse_irq_helper);
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118183957.2031311-1-github%40szelinsky.de
^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: [PATCH net v5 05/14] net: pse-pd: do not reach a freed PHY from the notification worker
2026-11-18 18:39 ` [PATCH net v5 05/14] net: pse-pd: do not reach a freed PHY from the notification worker Carlo Szelinsky
@ 2026-11-20 6:41 ` netdev-bot+sashiko
2026-11-20 8:33 ` Carlo Szelinsky
0 siblings, 1 reply; 27+ messages in thread
From: netdev-bot+sashiko @ 2026-11-20 6:41 UTC (permalink / raw)
To: github
Cc: o.rempel, kory.maincent, andrew+netdev, davem, edumazet, kuba,
pabeni, horms, broonie, lgirdwood, maxime.chevallier,
kyle.swenson, ross, netdev-bot+sashiko, netdev, linux-kernel
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The new kernel-doc implies that a NULL attached_phydev is
temporary, but the code makes it permanent.
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but
pse_send_ntf_worker() can still use a freed net_device.
--
net: pse-pd: do not reach a freed PHY from the notification worker
The notification worker used to keep a reference on the pse_control and
then dereference psec->attached_phydev, which could already be freed. With
this patch the worker looks up the pse_control and reads attached_phydev
under pse_list_mutex, nested inside rtnl. When a second PHY takes a shared
PI, pse_control_get_internal() now clears attached_phydev and warns.
> diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
> index 0894613515e0d..b8cba6f86a7fd 100644
> --- a/drivers/net/pse-pd/pse_core.c
> +++ b/drivers/net/pse-pd/pse_core.c
> @@ -31,7 +31,9 @@ static DEFINE_MUTEX(pse_pw_d_mutex);
> * @list: list entry for the pcdev's PSE controller list
> * @id: ID of the PSE line in the PSE controller device
> * @refcnt: Number of gets of this pse_control
> - * @attached_phydev: PHY device pointer attached by the PSE control
> + * @attached_phydev: PHY device pointer attached by the PSE control, or NULL
> + * when several PHYs share the PI. Read it only under
> + * pse_list_mutex.
[Severity: Low]
This isn't a bug, but "NULL when several PHYs share the PI" reads as if
the field is only NULL while the PI is shared. Is that what is intended?
The only writes to attached_phydev are the assignment when the pse_control
is created and the new clear in pse_control_get_internal(). Neither
pse_control_put() nor __pse_control_put_internal() sets it back.
So if PHY A and PHY B share a PI and B is removed, only A holds the
pse_control, but attached_phydev stays NULL. The commit message describes
this:
Events for that PI are then no longer reported, even after one PHY goes
away.
The new pse_control_get_netdev() kernel-doc has the same problem:
Return: netdev pointer, or NULL if no single PHY is attached
It also leaves out the NULL return when the single PHY has no
attached_dev.
Would wording like "NULL once the PI has been shared by several PHYs"
match the code better?
[ ... ]
> @@ -1014,13 +1017,18 @@ static void pse_send_ntf_worker(struct work_struct *work)
> struct net_device *netdev;
> struct pse_control *psec;
>
> - psec = pse_control_find_by_id(pcdev, ntf.id);
> + /* pse_control_put() frees the pse_control under
> + * pse_list_mutex before its PHY is freed, so the mutex
> + * keeps attached_phydev valid.
> + */
> rtnl_lock();
> + mutex_lock(&pse_list_mutex);
> + psec = pse_control_find_by_id(pcdev, ntf.id);
> netdev = pse_control_get_netdev(psec);
> if (netdev)
> ethnl_pse_send_ntf(netdev, ntf.notifs);
[Severity: High]
This is a pre-existing issue, not one introduced by this patch, but can
the worker still use a freed net_device here?
pse_control_get_netdev() returns psec->attached_phydev->attached_dev
without taking a netdev reference. ethnl_pse_send_ntf() then dereferences
it in ethnl_fill_reply_header() and ethnl_multicast().
Neither rtnl nor pse_list_mutex excludes the paths that clear attached_dev
or free the netdev. phy_detach_internal() clears it without needing rtnl:
drivers/net/phy/phy_device.c:phy_detach_internal() {
...
phydev->attached_dev->phydev = NULL;
phydev->attached_dev = NULL;
...
}
Its own comment says "Drivers may detach without RTNL while dev is
unregistered". free_netdev() on a NETREG_UNREGISTERED device does not need
rtnl either.
usbnet_disconnect() is one example of a remove path that does all of this
outside rtnl:
CPU0 (usbnet_disconnect) CPU1 (pse_send_ntf_worker)
unregister_netdev(net);
rtnl_lock();
mutex_lock(&pse_list_mutex);
netdev = pse_control_get_netdev(psec);
/* attached_dev still set */
->unbind()
smsc95xx_unbind()
phy_disconnect()
free_netdev(net);
ethnl_pse_send_ntf(netdev, ...);
The PHY stays on the MDIO bus in this sequence, so psec and attached_phydev
remain valid, and the new pse_list_mutex scheme does not cover the netdev.
Does the worker also need a lifetime guarantee for the netdev? For
example, a netdev reference taken when the PHY is attached, or a reg_state
check together with serialisation against phy_detach.
> + mutex_unlock(&pse_list_mutex);
> rtnl_unlock();
> - pse_control_put(psec);
> }
> }
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118183957.2031311-1-github%40szelinsky.de
^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: [PATCH net v5 06/14] net: pse-pd: check the PI state under the lock when setting a power limit
2026-11-18 18:39 ` [PATCH net v5 06/14] net: pse-pd: check the PI state under the lock when setting a power limit Carlo Szelinsky
@ 2026-11-20 6:41 ` netdev-bot+sashiko
2026-11-20 8:33 ` Carlo Szelinsky
0 siblings, 1 reply; 27+ messages in thread
From: netdev-bot+sashiko @ 2026-11-20 6:41 UTC (permalink / raw)
To: github
Cc: o.rempel, kory.maincent, andrew+netdev, davem, edumazet, kuba,
pabeni, horms, broonie, lgirdwood, maxime.chevallier,
kyle.swenson, ross, netdev-bot+sashiko, netdev, linux-kernel
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] The patch moves the PI-state check into
pse_pi_update_pw_budget(), under pcdev->lock, but the predicate is…
--
net: pse-pd: check the PI state under the lock when setting a power limit
This patch moves the "PI is enabled and has a PD detected" check from
pse_ethtool_set_pw_limit() into pse_pi_update_pw_budget(), so it runs
under pcdev->lock. It also adds an updated flag, so the rollback after a
failed regulator_set_current_limit() only runs when the budget was
actually changed.
> diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
> index b8cba6f86a7fd..342fb27e38319 100644
> --- a/drivers/net/pse-pd/pse_core.c
> +++ b/drivers/net/pse-pd/pse_core.c
[ ... ]
> /* We don't want pw_allocated_mW value change in the middle of an
> * power budget update
> */
> mutex_lock(&pcdev->lock);
> + /* Only a PI under software power control that is enabled and has a
> + * PD detected gets a budget. Check this here, as pse_isr() changes
> + * it under the same lock.
> + */
> + if (!pse_pw_d_is_sw_pw_control(pcdev, pi->pw_d) ||
> + !pi->admin_state_enabled || !pi->isr_pd_detected)
> + goto out;
> +
[Severity: High]
The check now runs under the lock. Is admin_state_enabled &&
isr_pd_detected enough to show that the PI is powered and has a budget?
pse_pi_is_admin_enable_pending() describes a state where both flags are
set but the PI is off in hardware and pw_allocated_mW is 0:
pse_pi_is_admin_enable_pending() {
if (!pcdev->pi[id].admin_state_enabled ||
!pcdev->pi[id].isr_pd_detected)
return false;
ret = pse_pi_is_hw_enabled(pcdev, id);
if (ret == 1)
return false;
return true;
}
It looks like this state can be reached without any race:
- On classification, pse_set_config_isr() sets isr_pd_detected = true.
If the static allocation then fails, it turns -ERANGE into 0.
pse_pi_enable() also sets admin_state_enabled = 1 on -ERANGE.
- pse_disable_pi_pol()->_pse_pi_disable() sheds a lower priority PI and
frees its budget, but leaves both flags set.
- If ops->pi_enable() fails after the budget was allocated, the budget
is freed but the flags stay set.
In that state, pse_ethtool_set_pw_limit()->pse_pi_update_pw_budget()
passes the new check. pw_diff = pw_limit - 0 is positive, so
regulator_request_power_budget() is called and pi->pw_allocated_mW =
pw_limit is recorded for a port that is not powered.
The voltage == 0 early return in pse_ethtool_set_pw_limit() doesn't
seem to stop this on tps23881, because tps23881_pi_get_voltage() reads
the controller input voltage.
Later, pse_ethtool_set_prio() calls pse_pw_d_retry_power_delivery(), as
does the disable or disconnect of another PI in the same domain. That
function picks exactly these admin enable pending PIs:
pse_pw_d_retry_power_delivery()
_pse_pi_delivery_power_sw_pw_ctrl()
pse_pi_allocate_pw_budget_static_prio()
pse_pi_allocate_pw_budget_static_prio() then requests pw_req again and
does:
pi->pw_allocated_mW = pw_req;
return 0;
It does not free the reservation made by set_pw_limit first. Doesn't
this leak the power budget on the supply until the controller is bound
again? That is the same leak the commit message describes.
Until that retry runs, shedding can't reclaim the reservation either:
pse_disable_pi_prio() skips PIs where pse_pi_is_hw_enabled() <= 0. As a
result, powered ports could be shed or refused power.
The later patches in the series, such as "use each PI's own supply
handle for the power budget", only change the supply handle in these
functions. The predicate and the overwrite still appear to be there at
the end of the series.
Would it work to check under the lock that the PI is actually powered
or budgeted, for example pse_pi_is_hw_enabled(pcdev, id) == 1 or
pw_allocated_mW != 0? Another option is for
pse_pi_allocate_pw_budget_static_prio() to free an existing
pw_allocated_mW before overwriting it.
> previous_pw_allocated = pi->pw_allocated_mW;
> pw_diff = pw_req - previous_pw_allocated;
> if (!pw_diff) {
[ ... ]
> @@ -1846,6 +1859,7 @@ static int pse_pi_update_pw_budget(struct pse_controller_dev *pcdev, int id,
> regulator_free_power_budget(pi->pw_d->supply, -pw_diff);
> }
> pi->pw_allocated_mW = pw_req;
> + *updated = true;
> ret = previous_pw_allocated;
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118183957.2031311-1-github%40szelinsky.de
^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: [PATCH net v5 07/14] net: pse-pd: do not tie a shared power domain to its creator's devres
2026-11-18 18:39 ` [PATCH net v5 07/14] net: pse-pd: do not tie a shared power domain to its creator's devres Carlo Szelinsky
@ 2026-11-20 6:41 ` netdev-bot+sashiko
2026-11-20 8:33 ` Carlo Szelinsky
0 siblings, 1 reply; 27+ messages in thread
From: netdev-bot+sashiko @ 2026-11-20 6:41 UTC (permalink / raw)
To: github
Cc: o.rempel, kory.maincent, andrew+netdev, davem, edumazet, kuba,
pabeni, horms, broonie, lgirdwood, maxime.chevallier,
kyle.swenson, ross, netdev-bot+sashiko, netdev, linux-kernel
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 (1) · Low: 0 (1)
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] The shared domain can now outlive its creator, but its supply
handle still cannot.
Pre-existing issues:
- [Medium] pse_flush_pw_ds() clears pcdev->pi[i].pw_d via
pse_pi_take_pw_d() and drops the domain reference without first…
- [Low] pse_register_pw_ds() publishes pcdev->pi[i].pw_d at line 997
(shared-domain branch) and line 1026 (new-domain branch) holding only…
--
net: pse-pd: do not tie a shared power domain to its creator's devres
This patch allocates PSE power domains with kzalloc() instead of devm
memory of the creating controller, and frees them on the last kref_put().
It also unwinds the domains on a partial pse_register_pw_ds() failure
while pse_pw_d_mutex is held. Before dropping the reference it clears
pi[].pw_d under pcdev->lock.
> diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
> index 342fb27e38319..8041ad3e330b9 100644
> --- a/drivers/net/pse-pd/pse_core.c
> +++ b/drivers/net/pse-pd/pse_core.c
> @@ -875,17 +875,41 @@ devm_pse_pi_regulator_register(struct pse_controller_dev *pcdev,
[ ... ]
> +/* Clear pi[id].pw_d before the domain's reference is dropped. The PI
> + * regulator ops and pse_isr() hold pcdev->lock while they use it.
> + */
> +static struct pse_power_domain *
> +pse_pi_take_pw_d(struct pse_controller_dev *pcdev, int id)
> +{
> + struct pse_power_domain *pw_d;
> +
> + mutex_lock(&pcdev->lock);
> + pw_d = pcdev->pi[id].pw_d;
> + pcdev->pi[id].pw_d = NULL;
^^^^
[Severity: Medium]
This is a pre-existing issue and was not introduced by this patch, but
does pse_flush_pw_ds() leak the PI's reserved power budget on the shared
supply?
After pse_pi_take_pw_d() sets pi[id].pw_d to NULL,
pse_pi_deallocate_pw_budget() does nothing:
static void pse_pi_deallocate_pw_budget(struct pse_pi *pi)
{
if (!pi->pw_d || !pi->pw_allocated_mW)
return;
...
}
pse_pi_update_pw_budget() also bails out, because
pse_pw_d_is_sw_pw_control(pcdev, NULL) is false.
pse_controller_unregister() does this:
pse_flush_pw_ds(pcdev);
pse_release_pis(pcdev);
Nothing on that path returns pi->pw_allocated_mW with
regulator_free_power_budget(). regulator_put() and regulator_unregister()
do not release it either.
Say a controller under software power control unbinds while a PD is
powered. Does the vpwr supply's pw_requested_mW then stay charged? A
sibling controller on the same supply would see less available budget,
and each unbind/rebind would make the drift worse.
The baseline pse_controller_unregister() never released per-PI budgets
either, and the same is true at the end of this series.
> + mutex_unlock(&pcdev->lock);
> +
> + return pw_d;
> +}
[ ... ]
> @@ -981,6 +1012,7 @@ static int pse_register_pw_ds(struct pse_controller_dev *pcdev)
> supply = regulator_get(&rdev->dev, rdev->supply_name);
^^^^
[Severity: High]
The new kernel-doc for pse_alloc_pw_d() says the domain "must not be tied
to the lifetime of the controller that happened to create it". Does that
hold for pw_d->supply?
The supply handle is taken with the creating controller's own PI
regulator device as the consumer. That rdev comes from
devm_regulator_register() on pcdev->dev, so it is freed when that
controller unbinds.
Take controller A, which creates domain D on supply S, and controller B,
which joins D through kref_get(). When A unbinds, pse_flush_pw_ds() drops
only A's references, so D and D->supply stay alive. Devres still
unregisters and frees A's rdev. D->supply then stays on S's consumer_list
with a stale ->dev.
Can a read of /sys/kernel/debug/regulator/regulator_summary then hit a
use-after-free in regulator_summary_show_subtree()? That function reads
consumer->dev->class and dev_name(consumer->dev).
When B later drops the last reference, this chain passes the same stale
dev to device_link_remove():
pse_pw_d_release_locked()->regulator_put()->destroy_regulator()
The dangling consumer also existed before this patch, back when the whole
domain was freed. The later commit in this series, "net: pse-pd: use
each PI's own supply handle for the power budget", removes pw_d->supply
and uses pi->rdev->supply instead. Its commit message shows this same
KASAN report in regulator_summary_show_subtree().
Could the kernel-doc here be adjusted, or could the commit note that it
depends on that later change for the supply handle?
> if (IS_ERR(supply)) {
> xa_erase(&pse_pw_d_map, pw_d->id);
> + kfree(pw_d);
> ret = PTR_ERR(supply);
> goto out;
> }
[ ... ]
> @@ -995,6 +1027,13 @@ static int pse_register_pw_ds(struct pse_controller_dev *pcdev)
> }
>
[Severity: Low]
This isn't a bug introduced by this patch, but the commit message says
the PI regulator ops use pw_d under pcdev->lock. Is the publication side
in pse_register_pw_ds() covered too?
Both the shared-domain branch and the new-domain branch store
pcdev->pi[i].pw_d with only pse_pw_d_mutex held:
pw_d->supply = supply;
if (pcdev->supp_budget_eval_strategies)
pw_d->budget_eval_strategy = pcdev->supp_budget_eval_strategies;
...
kref_init(&pw_d->refcnt);
pcdev->pi[i].pw_d = pw_d;
By this point devm_pse_pi_regulator_register() has already registered
the PI regulators. pse_pi_is_enabled() can be reached through the
regulator sysfs state attribute. It reads pi[id].pw_d and dereferences
budget_eval_strategy under pcdev->lock, which is a different lock from
the writer's.
On a weakly ordered CPU, could a reader see the new pointer before the
budget_eval_strategy store? The impact looks limited to a wrong state
value for a short window during probe, plus a possible KCSAN report.
The pointer only ever goes from NULL to a live object.
> out:
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118183957.2031311-1-github%40szelinsky.de
^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: [PATCH net v5 08/14] net: pse-pd: stop async event sources before freeing PI data in unregister
2026-11-18 18:39 ` [PATCH net v5 08/14] net: pse-pd: stop async event sources before freeing PI data in unregister Carlo Szelinsky
@ 2026-11-20 6:41 ` netdev-bot+sashiko
2026-11-20 8:33 ` Carlo Szelinsky
0 siblings, 1 reply; 27+ messages in thread
From: netdev-bot+sashiko @ 2026-11-20 6:41 UTC (permalink / raw)
To: github
Cc: o.rempel, kory.maincent, andrew+netdev, davem, edumazet, kuba,
pabeni, horms, broonie, lgirdwood, maxime.chevallier,
kyle.swenson, ross, netdev-bot+sashiko, netdev, linux-kernel
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] pse_controller_unregister() sets pcdev->unregistering = true and
nothing ever clears it.
Pre-existing issues:
- [High] At this commit, pse_controller_unregister() still calls
pse_release_pis(), which does kfree(pcdev->pi), at line 1188.
- [Medium] At this commit, pse_controller_register() returns directly
after kfifo_alloc() on several failures and leaks ntf_fifo: missing…
--
net: pse-pd: stop async event sources before freeing PI data in unregister
This patch reorders pse_controller_unregister() so that the IRQ is
disabled and the notification worker is drained before the power domains
are flushed and the PI array is freed. It also adds a
pcdev->unregistering flag, set under pcdev->lock. After it is set,
pse_pi_enable() and _pse_pi_delivery_power_sw_pw_ctrl() return -ENODEV,
so the worker can't be queued again after the drain.
> diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
> index 8041ad3e330b9..22f2d8041bd4b 100644
> --- a/drivers/net/pse-pd/pse_core.c
> +++ b/drivers/net/pse-pd/pse_core.c
[ ... ]
> @@ -1158,11 +1170,22 @@ EXPORT_SYMBOL_GPL(pse_controller_register);
> */
> void pse_controller_unregister(struct pse_controller_dev *pcdev)
> {
> - pse_flush_pw_ds(pcdev);
> - pse_release_pis(pcdev);
> + /* From now on the PSE core must not power a PI, so nothing queues
> + * a new notification once the worker is drained below.
> + */
> + mutex_lock(&pcdev->lock);
> + pcdev->unregistering = true;
> + mutex_unlock(&pcdev->lock);
[Severity: Low]
This is the only write to pcdev->unregistering, and nothing clears it
again. pse_controller_register() doesn't reset it either. It only
initializes these:
pse_controller_register() {
mutex_init(&pcdev->lock);
INIT_LIST_HEAD(&pcdev->pse_control_head);
spin_lock_init(&pcdev->ntf_fifo_lock);
...
}
If the same pse_controller_dev were registered again, wouldn't
pse_pi_enable() and _pse_pi_delivery_power_sw_pw_ctrl() always return
-ENODEV? pse_set_config_isr() would then never power a PI.
No in-tree caller does this. pd692x0, pse_regulator, realtek-pse-mcu,
si3474 and tps23881 all call devm_pse_controller_register() on a struct
allocated with devm_kzalloc. So each bind gets a zeroed pcdev. Other
fields are not reset on register either, for example no_of_pse_pi and
irq.
Could the flag be cleared in pse_controller_register()? Or could the
kernel-doc say that the core expects a zeroed pcdev for each
registration?
> +
> + /* Stop the IRQ and the notification worker first: pse_isr() uses
> + * pcdev->pi and the power domains that pse_flush_pw_ds() drops,
> + * and the worker reads the kfifo that is freed below.
> + */
> if (pcdev->irq)
> disable_irq(pcdev->irq);
> cancel_work_sync(&pcdev->ntf_work);
> + pse_flush_pw_ds(pcdev);
> + pse_release_pis(pcdev);
[Severity: High]
This is a pre-existing issue and was not introduced by this patch.
pse_release_pis() still does kfree(pcdev->pi) while other code can
still reach the array.
The PI regulators were registered with devm_pse_pi_regulator_register().
Devres removes them only after this function returns. pcdev also stays
on pse_controller_list until the list_del() further down.
In that window, can these read or write the freed pcdev->pi[]?
pse_pi_is_enabled()
pse_pi_disable()->_pse_pi_disable()
__pse_control_release()
of_pse_control_get()->of_pse_match_pi()
For example:
of_pse_control_get() {
...
list_for_each_entry(r, &pse_controller_list, list) {
if (!r->no_of_pse_pi) {
ret = of_pse_match_pi(r, args.np);
...
}
__pse_control_release() {
...
if (psec->pcdev->pi[psec->id].admin_state_enabled)
regulator_disable(psec->ps);
...
}
pse_release_pis() also doesn't take pcdev->lock, and it leaves
pcdev->pi pointing at freed memory.
This patch only moves the free after the IRQ and worker drain. It
doesn't make the window wider.
Later patches in this series fix it:
"net: pse-pd: unregister from the controller list before freeing PI
data" moves list_del() ahead of the drain and the frees.
"net: pse-pd: keep the PI data until the PI regulators are gone"
removes pse_release_pis() from unregister. The array is then freed
from a devm_pse_release_pis devres action, which runs after the PI
regulators are gone.
That later commit also says the series does not fix a consumer that
still holds a pse_control after the controller unbinds.
> kfifo_free(&pcdev->ntf_fifo);
[Severity: Medium]
This is also a pre-existing issue and was not introduced by this patch.
Is ntf_fifo leaked when pse_controller_register() fails after
kfifo_alloc()? Several error paths there return directly without
calling kfifo_free():
pse_controller_register() {
...
if (!pcdev->ops->pi_get_admin_state ||
!pcdev->ops->pi_get_pw_status) {
dev_err(pcdev->dev,
"Mandatory status report callbacks are missing");
return -EINVAL;
}
...
ret = pse_register_pw_ds(pcdev);
if (ret)
return ret;
...
}
The failures of of_load_pse_pis(), setup_pi_matrix(), devm_kzalloc()
and devm_pse_pi_regulator_register() return directly too. The later
failures leak pcdev->pi as well. These paths can be hit repeatedly
during -EPROBE_DEFER retries.
Later patches in this series fix it:
"net: pse-pd: free the notification kfifo when registration fails"
sends every error path to a free_kfifo label, which calls
cancel_work_sync() and kfifo_free().
"net: pse-pd: keep the PI data until the PI regulators are gone"
registers devm_pse_release_pis right after of_load_pse_pis()
succeeds. of_load_pse_pis() already frees the PI array on its own
errors.
> mutex_lock(&pse_list_mutex);
> list_del(&pcdev->list);
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118183957.2031311-1-github%40szelinsky.de
^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: [PATCH net v5 04/14] net: pse-pd: do not keep a failed IRQ in pcdev->irq
2026-11-20 6:41 ` netdev-bot+sashiko
@ 2026-11-20 8:32 ` Carlo Szelinsky
0 siblings, 0 replies; 27+ messages in thread
From: Carlo Szelinsky @ 2026-11-20 8:32 UTC (permalink / raw)
To: netdev-bot+sashiko
Cc: Kory Maincent, Oleksij Rempel, Andrew Lunn, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Mark Brown, Liam Girdwood, Maxime Chevallier, Kyle Swenson,
Ross Cawston, netdev, linux-kernel
On Fri, 20 Nov 2026 06:41:47 +0000, netdev-bot+sashiko@kernel.org wrote:
> This covers the failed request. Can pcdev->irq still go stale on a normal
> unbind?
Yes, but this problem is older than the patch. The patch only fixes
the failed request. The cover letter lists the unbind case, and I
will fix it in a follow-up.
Carlo
^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: [PATCH net v5 05/14] net: pse-pd: do not reach a freed PHY from the notification worker
2026-11-20 6:41 ` netdev-bot+sashiko
@ 2026-11-20 8:33 ` Carlo Szelinsky
0 siblings, 0 replies; 27+ messages in thread
From: Carlo Szelinsky @ 2026-11-20 8:33 UTC (permalink / raw)
To: netdev-bot+sashiko
Cc: Kory Maincent, Oleksij Rempel, Andrew Lunn, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Mark Brown, Liam Girdwood, Maxime Chevallier, Kyle Swenson,
Ross Cawston, netdev, linux-kernel
On Fri, 20 Nov 2026 06:41:48 +0000, netdev-bot+sashiko@kernel.org wrote:
> Would wording like "NULL once the PI has been shared by several PHYs"
> match the code better?
Yes, that is better. The Return: line also misses the case where the
PHY has no net_device. I will fix both texts in a follow-up.
> This is a pre-existing issue, not one introduced by this patch, but can
> the worker still use a freed net_device here?
Yes, but this is older than the patch, as on v3. The cover letter
lists it.
Carlo
^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: [PATCH net v5 06/14] net: pse-pd: check the PI state under the lock when setting a power limit
2026-11-20 6:41 ` netdev-bot+sashiko
@ 2026-11-20 8:33 ` Carlo Szelinsky
2026-11-20 9:32 ` Kory Maincent
0 siblings, 1 reply; 27+ messages in thread
From: Carlo Szelinsky @ 2026-11-20 8:33 UTC (permalink / raw)
To: netdev-bot+sashiko, Kory Maincent
Cc: Oleksij Rempel, Andrew Lunn, David S . Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Mark Brown,
Liam Girdwood, Maxime Chevallier, Kyle Swenson, Ross Cawston,
netdev, linux-kernel
On Fri, 20 Nov 2026 06:41:50 +0000, netdev-bot+sashiko@kernel.org wrote:
> The check now runs under the lock. Is admin_state_enabled &&
> isr_pd_detected enough to show that the PI is powered and has a budget?
No. This is the same point as on v3 and v4. The patch only moves the
old check under the lock. Kory agreed to leave it for a follow-up [1].
Kory, one question: we are at v5 now, and most new findings are older
problems outside this series. What do you suggest to get it merged
faster? I am happy to fix the older problems in follow-ups.
[1] https://lore.kernel.org/netdev/2b82341c-be67-43ef-9a0c-0bf0394b627a@bootlin.com/
Carlo
^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: [PATCH net v5 07/14] net: pse-pd: do not tie a shared power domain to its creator's devres
2026-11-20 6:41 ` netdev-bot+sashiko
@ 2026-11-20 8:33 ` Carlo Szelinsky
0 siblings, 0 replies; 27+ messages in thread
From: Carlo Szelinsky @ 2026-11-20 8:33 UTC (permalink / raw)
To: netdev-bot+sashiko
Cc: Kory Maincent, Oleksij Rempel, Andrew Lunn, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Mark Brown, Liam Girdwood, Maxime Chevallier, Kyle Swenson,
Ross Cawston, netdev, linux-kernel
On Fri, 20 Nov 2026 06:41:51 +0000, netdev-bot+sashiko@kernel.org wrote:
> The new kernel-doc for pse_alloc_pw_d() says the domain "must not be tied
> to the lifetime of the controller that happened to create it". Does that
> hold for pw_d->supply?
Not at this patch, but 12/14 removes pw_d->supply. This patch does
not make it worse: before it, the whole domain was freed with its
creator.
> Could the kernel-doc here be adjusted, or could the commit note that it
> depends on that later change for the supply handle?
7 and 12 have the same Fixes: tag, so a backport takes both. If
there is a new version, I will add a note to 7.
> This is a pre-existing issue and was not introduced by this patch, but
> does pse_flush_pw_ds() leak the PI's reserved power budget on the shared
> supply?
Yes, but this is older than this series. The cover letter lists it.
> This isn't a bug introduced by this patch, but the commit message says
> the PI regulator ops use pw_d under pcdev->lock. Is the publication side
> in pse_register_pw_ds() covered too?
No. This is older too, and it is the same short window during probe
as the cover letter item "The PI regulators are visible before their
power domain is set".
Carlo
^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: [PATCH net v5 08/14] net: pse-pd: stop async event sources before freeing PI data in unregister
2026-11-20 6:41 ` netdev-bot+sashiko
@ 2026-11-20 8:33 ` Carlo Szelinsky
0 siblings, 0 replies; 27+ messages in thread
From: Carlo Szelinsky @ 2026-11-20 8:33 UTC (permalink / raw)
To: netdev-bot+sashiko
Cc: Kory Maincent, Oleksij Rempel, Andrew Lunn, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Mark Brown, Liam Girdwood, Maxime Chevallier, Kyle Swenson,
Ross Cawston, netdev, linux-kernel
On Fri, 20 Nov 2026 06:41:53 +0000, netdev-bot+sashiko@kernel.org wrote:
> Could the flag be cleared in pse_controller_register()? Or could the
> kernel-doc say that the core expects a zeroed pcdev for each
> registration?
All in-tree drivers get a new, zeroed pcdev for each bind, and other
fields are not reset either. So I leave it as it is.
The other two points are fixed later in this series.
Carlo
^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: [PATCH net v5 06/14] net: pse-pd: check the PI state under the lock when setting a power limit
2026-11-20 8:33 ` Carlo Szelinsky
@ 2026-11-20 9:32 ` Kory Maincent
2026-11-20 10:08 ` Carlo Szelinsky
0 siblings, 1 reply; 27+ messages in thread
From: Kory Maincent @ 2026-11-20 9:32 UTC (permalink / raw)
To: Carlo Szelinsky, netdev-bot+sashiko
Cc: Oleksij Rempel, Andrew Lunn, David S . Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Mark Brown,
Liam Girdwood, Maxime Chevallier, Kyle Swenson, Ross Cawston,
netdev, linux-kernel
On 11/20/26 09:33, Carlo Szelinsky wrote:
> On Fri, 20 Nov 2026 06:41:50 +0000, netdev-bot+sashiko@kernel.org wrote:
>> The check now runs under the lock. Is admin_state_enabled &&
>> isr_pd_detected enough to show that the PI is powered and has a budget?
>
> No. This is the same point as on v3 and v4. The patch only moves the
> old check under the lock. Kory agreed to leave it for a follow-up [1].
>
> Kory, one question: we are at v5 now, and most new findings are older
> problems outside this series. What do you suggest to get it merged
> faster? I am happy to fix the older problems in follow-ups.
You have my R-by on all the series so we can only wait for a net
maintainer to take it. Jakub, Paolo, would it be possible to take it?
--
Köry Maincent, Bootlin
Embedded Linux and kernel engineering
https://bootlin.com
^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: [PATCH net v5 06/14] net: pse-pd: check the PI state under the lock when setting a power limit
2026-11-20 9:32 ` Kory Maincent
@ 2026-11-20 10:08 ` Carlo Szelinsky
0 siblings, 0 replies; 27+ messages in thread
From: Carlo Szelinsky @ 2026-11-20 10:08 UTC (permalink / raw)
To: Kory Maincent, Oleksij Rempel
Cc: netdev-bot+sashiko, Andrew Lunn, David S . Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Mark Brown,
Liam Girdwood, Maxime Chevallier, Kyle Swenson, Ross Cawston,
netdev, linux-kernel
On Fri, 20 Nov 2026 10:32:01 +0100, Kory Maincent wrote:
> You have my R-by on all the series so we can only wait for a net
> maintainer to take it. Jakub, Paolo, would it be possible to take it?
Thanks Kory for the quick reply, I appreciate it! I am looking forward
to Jakub's and Paolo's feedback.
Oleksij, feel free to share if you have anything to add.
Carlo
^ permalink raw reply [flat|nested] 27+ messages in thread
end of thread, other threads:[~2026-11-20 10:09 UTC | newest]
Thread overview: 27+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 18:39 [PATCH net v5 00/14] net: pse-pd: assorted lifetime and error-path fixes Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 01/14] net: pse-pd: size the notification kfifo after defaulting nr_lines Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 02/14] net: pse-pd: remove dead pi_get_admin_state() checks Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 03/14] net: pse-pd: fix module refcount leak in pse_control_get_internal() Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 04/14] net: pse-pd: do not keep a failed IRQ in pcdev->irq Carlo Szelinsky
2026-11-20 6:41 ` netdev-bot+sashiko
2026-11-20 8:32 ` Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 05/14] net: pse-pd: do not reach a freed PHY from the notification worker Carlo Szelinsky
2026-11-20 6:41 ` netdev-bot+sashiko
2026-11-20 8:33 ` Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 06/14] net: pse-pd: check the PI state under the lock when setting a power limit Carlo Szelinsky
2026-11-20 6:41 ` netdev-bot+sashiko
2026-11-20 8:33 ` Carlo Szelinsky
2026-11-20 9:32 ` Kory Maincent
2026-11-20 10:08 ` Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 07/14] net: pse-pd: do not tie a shared power domain to its creator's devres Carlo Szelinsky
2026-11-20 6:41 ` netdev-bot+sashiko
2026-11-20 8:33 ` Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 08/14] net: pse-pd: stop async event sources before freeing PI data in unregister Carlo Szelinsky
2026-11-20 6:41 ` netdev-bot+sashiko
2026-11-20 8:33 ` Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 09/14] net: pse-pd: unregister from the controller list before freeing PI data Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 10/14] net: pse-pd: keep the PI data until the PI regulators are gone Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 11/14] net: pse-pd: free the notification kfifo when registration fails Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 12/14] net: pse-pd: use each PI's own supply handle for the power budget Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 13/14] net: pse-pd: pd692x0: release the manager power budget through devres Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 14/14] net: pse-pd: pd692x0: defer probe while a manager supply is unresolved Carlo Szelinsky
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.