[PATCH net v3 07/14] net: pse-pd: do not tie a shared power domain to its creator's devres

From: Carlo Szelinsky

Date: Tue Nov 03 2026 - 17:06:53 EST


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")
Signed-off-by: Carlo Szelinsky <github@xxxxxxxxxxxx>
---
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 36f7dba8190a..113a34b09386 100644
--- a/drivers/net/pse-pd/pse_core.c
+++ b/drivers/net/pse-pd/pse_core.c
@@ -872,17 +872,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
@@ -893,10 +917,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;

@@ -906,24 +931,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(dev, sizeof(*pw_d), GFP_KERNEL);
+ 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;
@@ -969,7 +1000,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;
@@ -978,6 +1009,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;
}
@@ -992,6 +1024,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;
}
@@ -1949,15 +1988,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