From b562d73c7e30ffca52b6dbe65146e3f4cc1129a1 Mon Sep 17 00:00:00 2001 From: niuxiaoch Date: Mon, 23 Nov 2026 14:56:13 +0800 Subject: [pm] fix race condition and lock protection in rt_pm_device_register/unregister (#12075) * [pm] fix race condition and lock protection in rt_pm_device_register/unregister 1. Initialize `device_pm` fields before appending the node to `_pm.device_list` in `rt_pm_device_register()` to prevent exposing uninitialized pointers. 2. Protect `_pm.device_list` modifications with `rt_hw_interrupt_disable()` / `rt_hw_interrupt_enable()` while keeping `RT_KERNEL_MALLOC()` and `RT_KERNEL_FREE()` outside the interrupt-disabled critical section to avoid SMP hang (#10075). Fixes #10075 Signed-off-by: niuxiaoch * [pm] document locking rules for rt_pm_device_register/unregister Document that _pm.device_list updates are serialized with PM list walkers via rt_hw_interrupt_disable() (rt_cpus_lock() on SMP), and that the APIs must not be called from interrupt context or rt_device_pm_ops callbacks. Restore the original unregister comment wording. Signed-off-by: niuxiaoch --------- Signed-off-by: niuxiaoch --- components/drivers/pm/pm.c | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) (limited to 'components') diff --git a/components/drivers/pm/pm.c b/components/drivers/pm/pm.c index 290d8d4821..f957f6a6a1 100644 --- a/components/drivers/pm/pm.c +++ b/components/drivers/pm/pm.c @@ -831,17 +831,34 @@ rt_err_t rt_pm_sleep_light_release(rt_uint16_t module_id) * * @param device the device with PM feature * @param ops the PM ops for device + * + * @note Locking: _pm.device_list is protected by rt_hw_interrupt_disable() + * (mapped to rt_cpus_lock() when RT_USING_SMP is enabled). + * _pm_device_suspend()/_pm_device_resume() walk the list inside + * rt_pm_enter_critical(), and _pm_device_frequency_change() walks it + * inside rt_hw_interrupt_disable(), so list updates here are serialized + * with all PM list walkers. A BSP that overrides rt_pm_enter_critical() + * must provide at least the same exclusion as rt_hw_interrupt_disable(). + * @note Must not be called from interrupt context or from rt_device_pm_ops + * callbacks. */ void rt_pm_device_register(struct rt_device *device, const struct rt_device_pm_ops *ops) { + rt_base_t level; struct rt_device_pm *device_pm; - device_pm = RT_KERNEL_MALLOC(sizeof(struct rt_device_pm)); + RT_DEBUG_NOT_IN_INTERRUPT; + + device_pm = (struct rt_device_pm *)RT_KERNEL_MALLOC(sizeof(struct rt_device_pm)); if (device_pm != RT_NULL) { - rt_slist_append(&_pm.device_list, &device_pm->list); + rt_slist_init(&device_pm->list); device_pm->device = device; device_pm->ops = ops; + + level = rt_hw_interrupt_disable(); + rt_slist_append(&_pm.device_list, &device_pm->list); + rt_hw_interrupt_enable(level); } } @@ -849,21 +866,40 @@ void rt_pm_device_register(struct rt_device *device, const struct rt_device_pm_o * Unregister device from PM manager. * * @param device the device with PM feature + * + * @note The node is removed from _pm.device_list under the same lock used by + * the PM list walkers (see rt_pm_device_register()) and freed only after + * the lock is released. Walkers hold the lock for the whole traversal, + * so no walker can still reference the removed node. + * @note Must not be called from interrupt context or from rt_device_pm_ops + * callbacks. */ void rt_pm_device_unregister(struct rt_device *device) { + rt_base_t level; struct rt_device_pm *device_pm = RT_NULL; + struct rt_device_pm *found_pm = RT_NULL; rt_slist_t *node = RT_NULL; + + RT_DEBUG_NOT_IN_INTERRUPT; + + level = rt_hw_interrupt_disable(); for (node = rt_slist_first(&_pm.device_list); node; node = rt_slist_next(node)) { device_pm = rt_slist_entry(node, struct rt_device_pm, list); if (device_pm->device == device) { rt_slist_remove(&_pm.device_list, &device_pm->list); - RT_KERNEL_FREE(device_pm); + found_pm = device_pm; break; } } + rt_hw_interrupt_enable(level); + + if (found_pm != RT_NULL) + { + RT_KERNEL_FREE(found_pm); + } } /** -- cgit v1.3.1