Skip to content

Commit ce95b70

Browse files
bjarki-andreasenkartben
authored andcommitted
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 <bjarki.andreasen@nordicsemi.no>
1 parent 21b785d commit ce95b70

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
@@ -103,6 +103,12 @@ static int runtime_suspend(const struct device *dev, bool async,
103103
}
104104

105105
pm->base.state = PM_DEVICE_STATE_SUSPENDED;
106+
107+
/* Now put the domain */
108+
if (atomic_test_bit(&dev->pm_base->flags, PM_DEVICE_FLAG_PD_CLAIMED)) {
109+
(void)pm_device_runtime_put(PM_DOMAIN(dev->pm_base));
110+
atomic_clear_bit(&dev->pm_base->flags, PM_DEVICE_FLAG_PD_CLAIMED);
111+
}
106112
}
107113

108114
unlock:
@@ -139,6 +145,7 @@ static void runtime_suspend_work(struct k_work *work)
139145
if ((ret == 0) &&
140146
atomic_test_bit(&pm->base.flags, PM_DEVICE_FLAG_PD_CLAIMED)) {
141147
(void)pm_device_runtime_put(PM_DOMAIN(&pm->base));
148+
atomic_clear_bit(&pm->base.flags, PM_DEVICE_FLAG_PD_CLAIMED);
142149
}
143150

144151
__ASSERT(ret == 0, "Could not suspend device (%d)", ret);
@@ -224,7 +231,7 @@ int pm_device_runtime_get(const struct device *dev)
224231
*/
225232
const struct device *domain = PM_DOMAIN(&pm->base);
226233

227-
if (domain != NULL) {
234+
if (domain != NULL && !atomic_test_bit(&dev->pm_base->flags, PM_DEVICE_FLAG_PD_CLAIMED)) {
228235
ret = pm_device_runtime_get(domain);
229236
if (ret != 0) {
230237
goto unlock;
@@ -350,14 +357,6 @@ int pm_device_runtime_put(const struct device *dev)
350357
k_spin_unlock(&pm_sync->lock, k);
351358
} else {
352359
ret = runtime_suspend(dev, false, K_NO_WAIT);
353-
354-
/*
355-
* Now put the domain
356-
*/
357-
if ((ret == 0) &&
358-
atomic_test_bit(&dev->pm_base->flags, PM_DEVICE_FLAG_PD_CLAIMED)) {
359-
ret = pm_device_runtime_put(PM_DOMAIN(dev->pm_base));
360-
}
361360
}
362361
SYS_PORT_TRACING_FUNC_EXIT(pm, device_runtime_put, dev, ret);
363362

0 commit comments

Comments
 (0)