[PATCH net v2 07/14] net: pse-pd: do not tie a shared power domain to its creator's devres
From: Carlo Szelinsky
Date: Tue Oct 20 2026 - 13:35:00 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.
Allocate the domain with kzalloc() and free it on its last kref_put().
A domain left over from a failed pse_register_pw_ds() is now leaked
instead of freed while still in the map.
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 | 43 ++++++++++++++++++++++++-----------
1 file changed, 30 insertions(+), 13 deletions(-)
diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
index 36f7dba8190a..13d6bd29525f 100644
--- a/drivers/net/pse-pd/pse_core.c
+++ b/drivers/net/pse-pd/pse_core.c
@@ -881,6 +881,7 @@ static void __pse_pw_d_release(struct kref *kref)
regulator_put(pw_d->supply);
xa_erase(&pse_pw_d_map, pw_d->id);
mutex_unlock(&pse_pw_d_mutex);
+ kfree(pw_d);
}
/**
@@ -893,10 +894,18 @@ 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)
+ /* Clear it before the put that may free the domain. The PI
+ * regulator ops and pse_isr() hold pcdev->lock while they
+ * use it.
+ */
+ mutex_lock(&pcdev->lock);
+ pw_d = pcdev->pi[i].pw_d;
+ pcdev->pi[i].pw_d = NULL;
+ mutex_unlock(&pcdev->lock);
+ 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 +915,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() 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 +984,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 +993,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;
}
@@ -1949,15 +1965,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