Skip to content

Commit 4019fa7

Browse files
pm: device_runtime: fix unbalanced domain get/put
pm_device_runtime has inconsistent behavior regarding getting and putting a device's domain. This commit aligns it to get the domain only once, once device is resumed, and put only once, once device is suspended. Signed-off-by: Bjarki Arge Andreasen <[email protected]>
1 parent 6d4bd2e commit 4019fa7

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

subsys/pm/device_runtime.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ static int runtime_suspend(const struct device *dev, bool async,
135135
}
136136

137137
pm->base.state = PM_DEVICE_STATE_SUSPENDED;
138+
139+
/* Now put the domain */
140+
if (atomic_test_bit(&dev->pm_base->flags, PM_DEVICE_FLAG_PD_CLAIMED)) {
141+
(void)pm_device_runtime_put(PM_DOMAIN(dev->pm_base));
142+
atomic_clear_bit(&dev->pm_base->flags, PM_DEVICE_FLAG_PD_CLAIMED);
143+
}
138144
}
139145

140146
unlock:
@@ -171,6 +177,7 @@ static void runtime_suspend_work(struct k_work *work)
171177
if ((ret == 0) &&
172178
atomic_test_bit(&pm->base.flags, PM_DEVICE_FLAG_PD_CLAIMED)) {
173179
(void)pm_device_runtime_put(PM_DOMAIN(&pm->base));
180+
atomic_clear_bit(&pm->base.flags, PM_DEVICE_FLAG_PD_CLAIMED);
174181
}
175182

176183
__ASSERT(ret == 0, "Could not suspend device (%d)", ret);
@@ -256,7 +263,7 @@ int pm_device_runtime_get(const struct device *dev)
256263
*/
257264
const struct device *domain = PM_DOMAIN(&pm->base);
258265

259-
if (domain != NULL) {
266+
if (domain != NULL && !atomic_test_bit(&dev->pm_base->flags, PM_DEVICE_FLAG_PD_CLAIMED)) {
260267
ret = pm_device_runtime_get(domain);
261268
if (ret != 0) {
262269
goto unlock;
@@ -382,14 +389,6 @@ int pm_device_runtime_put(const struct device *dev)
382389
k_spin_unlock(&pm_sync->lock, k);
383390
} else {
384391
ret = runtime_suspend(dev, false, K_NO_WAIT);
385-
386-
/*
387-
* Now put the domain
388-
*/
389-
if ((ret == 0) &&
390-
atomic_test_bit(&dev->pm_base->flags, PM_DEVICE_FLAG_PD_CLAIMED)) {
391-
ret = pm_device_runtime_put(PM_DOMAIN(dev->pm_base));
392-
}
393392
}
394393
SYS_PORT_TRACING_FUNC_EXIT(pm, device_runtime_put, dev, ret);
395394

0 commit comments

Comments
 (0)