xhh233 opened a new issue, #22209:
URL: https://github.com/apache/nuttx/issues/22209
# Case: pm_idle() ignores a positive non-zero prepare() veto
Status: prepared, not submitted to upstream.
Scope: public NuttX sources only; no vendor identifiers, no private
addresses.
Evidence level: E1_src for this case (hosted; the unmodified upstream `.c`
files are compiled before the patch, and the patched build applies
`fix.patch`
to a throw-away copy of the same source); not a real board / `sim:nsh` run.
## Summary
The `prepare()` contract aborts a state transition on any non-zero return
value. `pm_changestate()` follows that contract: it reverts the domain state
and returns the non-zero value. In the non-SMP `pm_idle()`, however, only
`ret < 0` is treated as a veto. A driver that returns `+1` (a legal non-zero
veto) therefore gets the transition reverted, but `pm_idle()` keeps the
refused low-power state and passes it to its handler.
## Trigger conditions
1. `pm_checkstate()` selects a low-power target (the reproducer fixes
`PM_SLEEP`).
2. A registered driver's `prepare()` returns a positive non-zero value for
that target (the reproducer returns `+1`).
3. `pm_changestate()` reverts the domain state and returns `+1`.
4. `pm_idle()` evaluates `ret < 0` as false, so `newstate` stays at the
refused state and is passed to the idle handler.
## Reachability
This is a host API-level reproducer: `pm_idle()` is called directly with a
harness handler. In the pinned tree, `pm_idle()` is called from architecture
initialization (for example `arch/arm64/src/qemu/qemu_initialize.c` and
`arch/arm64/src/a527/a527_initialize.c`). Whether a real driver returns a
positive non-zero value depends on that driver; this report does not claim a
specific vehicle driver does so. Only the non-SMP branch is exercised; the
SMP
branch has the same `ret < 0` pattern at two sites and is not run here. The
patch changes only the comparison, not the fallback: the SMP CPU branch keeps
`oldstate` (as in upstream commit `46d555e`), while the system-domain and
non-SMP branches keep `PM_NORMAL`.
## Affected code
- `drivers/power/pm/pm_idle.c`: non-SMP path checks `ret < 0` instead of
`ret != OK`.
- `drivers/power/pm/pm_changestate.c`: aborts and reverts on any non-zero
prepare return, returning that value unchanged.
- `include/nuttx/power/pm.h`: documents that non-zero means the state change
is aborted.
## Impact (hosted demonstration)
The handler receives `PM_SLEEP` (value 3) although the domain state was
reverted to `PM_NORMAL` (value 0). This is a mismatch between what the idle
path reports and the state actually committed. The statement is at the state
machine level: entry into the hardware low-power state was not observed. The
reproducer does not claim a crash or a target-board failure.
## Reproduction (host)
The reproducer compiles the unmodified `pm_idle.c` together with the real
`pm_changestate.c`, registers one callback whose `prepare(PM_SLEEP)` returns
`+1`, and calls `pm_idle()`:
```
prepare_sleep_calls=1
prepare_normal_calls=1
domain_state_after=0
idle_handler_state=3
verdict=VETO_SWALLOWED
```
With the `ret != OK` condition:
```
idle_handler_state=0
verdict=VETO_HONORED
```
## Suggested fix
Treat any non-zero return from `pm_changestate()` as a veto in `pm_idle()`:
change the three `ret < 0` checks (non-SMP and the two SMP sites) to
`ret != OK`. Keep the existing fallbacks: the SMP CPU branch uses `oldstate`,
consistent with upstream commit `46d555e`, while the system-domain and
non-SMP branches use `PM_NORMAL`. This makes `pm_idle()` match the documented
`prepare()` contract without changing the rollback target.
## Relation to upstream
The code is present in the pinned NuttX 13.1.0 tree (commit
`2b5c0de5e933281c209f954a63b99cf10c132346`). This case does not modify
upstream sources; `fix.patch` is only dry-run against the frozen tree.
## Novelty
As of 2026-11-23, a related upstream commit exists (`46d555e5d4`, "pm_idle:
fix when prepare failed, possible relax error"): it changes the failure
fallback from `PM_NORMAL` to `oldstate` but still tests `ret < 0`, so it does
not cover a positive non-zero veto. No public report matching this exact `+1`
case was found (search expressions and request counts are recorded in
`../NOVELTY-PM4.md`). This has not been confirmed by upstream.
## Evidence files
`README.md`, `ISSUE-DRAFT-EN.md`, `before.log`, `after.log`, `expected.txt`,
`fix.patch`, `driver.c`, `build.sh`, `run.sh`, `validate.sh`, `src/`,
`shim/`,
`PROVENANCE.md`, `SRC-SHA256SUMS`, `SHA256SUMS`.
<details><summary>Host reproducer and before/after logs</summary>
```c
/* SPDX-License-Identifier: Apache-2.0
*
* PM-03 hosted driver: exercises the UNMODIFIED upstream
* drivers/power/pm/pm_idle.c (with the real pm_changestate.c) against a
* minimal kernel shim.
*
* Scenario:
* - domain 0 starts in PM_NORMAL; pm_checkstate() reports PM_SLEEP.
* - the registered driver's prepare() vetoes PM_SLEEP by returning +1.
* - pm_changestate() aborts and reverts, returning +1.
* - pm_idle() decides from that return value what state to hand to the
* idle handler.
*
* Upstream (unpatched) behaviour: pm_idle() only treats ret < 0 as a veto,
* although the prepare() contract aborts on any non-zero value. The +1
* veto is swallowed and the handler receives PM_SLEEP although the domain
* state was reverted to PM_NORMAL.
*/
#include <nuttx/config.h>
#include <stdio.h>
#include <string.h>
#include <nuttx/power/pm.h>
#include <nuttx/queue.h>
#include "pm.h"
struct pm_domain_s g_pmdomains[CONFIG_PM_NDOMAINS];
static struct pm_callback_s g_cb;
static enum pm_state_e g_handler_state = PM_NORMAL;
static int g_prep_sleep;
static int g_prep_normal;
static int g_notify;
static int g_gov_statechanged;
static void gov_statechanged(int domain, enum pm_state_e newstate)
{
(void)domain;
(void)newstate;
g_gov_statechanged++;
}
static const struct pm_governor_s g_gov =
{
NULL,
NULL,
gov_statechanged,
};
static int veto_prepare(FAR struct pm_callback_s *cb, int domain,
enum pm_state_e pmstate)
{
(void)cb;
(void)domain;
if (pmstate == PM_SLEEP)
{
g_prep_sleep++;
return 1;
}
if (pmstate == PM_NORMAL)
{
g_prep_normal++;
}
return 0;
}
static void count_notify(FAR struct pm_callback_s *cb, int domain,
enum pm_state_e pmstate)
{
(void)cb;
(void)domain;
(void)pmstate;
g_notify++;
}
static void idle_handler(enum pm_state_e systemstate)
{
g_handler_state = systemstate;
}
int main(void)
{
int domain_state;
memset(g_pmdomains, 0, sizeof(g_pmdomains));
dq_init(&g_pmdomains[0].registry);
g_pmdomains[0].state = PM_NORMAL;
g_pmdomains[0].governor = &g_gov;
memset(&g_cb, 0, sizeof(g_cb));
g_cb.prepare = veto_prepare;
g_cb.notify = count_notify;
dq_addlast(&g_cb.entry, &g_pmdomains[0].registry);
printf("case=PM-03\n");
printf("upstream_object=src/drivers/power/pm/pm_idle.c\n");
pm_idle(idle_handler);
domain_state = g_pmdomains[0].state;
printf("checkstate=PM_SLEEP\n");
printf("prepare_sleep_calls=%d\n", g_prep_sleep);
printf("prepare_normal_calls=%d\n", g_prep_normal);
printf("domain_state_after=%d\n", domain_state);
printf("idle_handler_state=%d\n", (int)g_handler_state);
printf("governor_statechanged=%d\n", g_gov_statechanged);
printf("notify_calls=%d\n", g_notify);
printf("verdict=%s\n",
(domain_state == PM_NORMAL && g_handler_state == PM_NORMAL)
? "VETO_HONORED"
: "VETO_SWALLOWED");
return 0;
}
```
```text
# before
case=PM-03
upstream_object=src/drivers/power/pm/pm_idle.c
checkstate=PM_SLEEP
prepare_sleep_calls=1
prepare_normal_calls=1
domain_state_after=0
idle_handler_state=3
governor_statechanged=2
notify_calls=2
verdict=VETO_SWALLOWED
# after
case=PM-03
upstream_object=src/drivers/power/pm/pm_idle.c
checkstate=PM_SLEEP
prepare_sleep_calls=1
prepare_normal_calls=1
domain_state_after=0
idle_handler_state=0
governor_statechanged=2
notify_calls=2
verdict=VETO_HONORED
```
</details>
Candidate patch:
```diff
--- a/drivers/power/pm/pm_idle.c
+++ b/drivers/power/pm/pm_idle.c
@@ -157,7 +157,7 @@
newstate = pm_checkstate(domain);
ret = pm_changestate(domain, newstate);
- if (ret < 0)
+ if (ret != OK)
{
newstate = oldstate;
}
@@ -184,7 +184,7 @@
{
systemstate = pm_checkstate(PM_IDLE_DOMAIN);
ret = pm_changestate(PM_IDLE_DOMAIN, systemstate);
- if (ret < 0)
+ if (ret != OK)
{
systemstate = PM_NORMAL;
}
@@ -240,7 +240,7 @@
newstate = pm_checkstate(PM_IDLE_DOMAIN);
ret = pm_changestate(PM_IDLE_DOMAIN, newstate);
- if (ret < 0)
+ if (ret != OK)
{
newstate = PM_NORMAL;
}
```
--
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]