zzby0 opened a new pull request, #21636: URL: https://github.com/apache/nuttx/pull/21636
*Note: Please adhere to [Contributing Guidelines](https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md).* ## Summary When auto update is enabled for a PM domain (`pm_auto_update(domain, true)`), every call to `pm_activity()`, `pm_stay()`, `pm_relax()` or the `pm_wakelock_*()` APIs ends up in `pm_auto_updatestate_cb()`, which does: ```c flags = pm_domain_lock(domain); newstate = pm_checkstate(domain); /* governor checkstate takes the lock again */ pm_changestate(domain, newstate); /* pm_changestate() takes the lock again */ pm_domain_unlock(domain, flags); ``` The domain lock is a non-recursive spinlock, so on SMP the CPU spins forever on the second acquisition. This PR fixes it in three steps: 1. **driver/pm: move domain lock from governor to common API** The activity, greedy and stability governors took the domain lock inside their `checkstate` callback. Move the lock into `pm_checkstate()`, so the locking is done in the common API for both check and change. 2. **driver/pm: combine check&change state into update API** Add `pm_updatestate()`, which takes the domain lock once and does the governor check and the state change under it. 3. **driver/pm: fix pm_auto_update deadlock** Use `pm_updatestate()` in `pm_auto_updatestate_cb()`. Also add a missing blank line after a declaration in `pm_autoupdate.c` (nxstyle). ## Impact * Fixes a hard hang on any PM domain with auto update enabled (SMP). * New public API `pm_updatestate(int domain)` in `include/nuttx/power/pm.h`. * The behavior of `pm_checkstate()` and `pm_changestate()` is unchanged for callers. * Governor `checkstate` callbacks are now always called with the domain lock held. Out-of-tree governors that take the domain lock in `checkstate` must drop it. * No impact on build, documentation or boards. ## Testing Host: Ubuntu 22.04, Linux 6.8.0 x86_64, gcc 13.4.0. nuttx-apps: master (ee3d67104d1). Config: `sim:smp` with `CONFIG_SMP_NCPUS=2`, `CONFIG_PM=y`, `CONFIG_PM_NDOMAINS=3`, `CONFIG_TESTING_CMOCKA=y`, `CONFIG_TESTING_DRIVER_TEST=y`. I used a small cmocka test (local only, not part of this PR) that enables auto update on domain 0 and then calls `pm_stay`/`pm_relax`/`pm_activity` in a loop: ```c pm_auto_update(TEST_DOMAIN, true); for (i = 0; i < 100; i++) { pm_stay(TEST_DOMAIN, PM_NORMAL); pm_relax(TEST_DOMAIN, PM_NORMAL); pm_activity(TEST_DOMAIN, 10); } pm_auto_update(TEST_DOMAIN, false); ``` Before this PR (master 066314c6373), the test hangs on the first call and the sim has to be killed: ``` nsh> cmocka_driver_pm_autoupdate [CPU0] [==========] tests: Running 1 test(s). [CPU0] [ RUN ] drivertest_pm_autoupdate (no further output, killed after 45s) ``` After this PR: ``` nsh> cmocka_driver_pm_autoupdate [CPU1] [==========] tests: Running 1 test(s). [CPU1] [ RUN ] drivertest_pm_autoupdate autoupdate: 100 iterations done, state=0 [CPU1] [ OK ] drivertest_pm_autoupdate [CPU1] [==========] tests: 1 test(s) run. [CPU1] [ PASSED ] 1 test(s). nsh> poweroff ``` Also checked: * `./tools/checkpatch.sh -g master..HEAD` passes. * Each of the three commits builds on its own for `sim:smp` + `CONFIG_PM=y`. * `sim:nsh` + `CONFIG_PM=y` builds. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
