This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit bec2b2a11e85d58f5d696be69e6508ad2d5c358d
Author: Honghao Xiang <[email protected]>
AuthorDate: Sat Nov 28 08:17:50 2026 +0000

    power: pm: normalize the pm_changestate() result in the callee
    
    A prepare() callback vetoes a transition with any non-zero value, but
    pm_changestate() passed that value through unchanged: a positive rejection
    reached the caller, while most callers test only 'ret < 0'.  The
    prepare-failure statistics had the same blind spot, since
    pm_stats_preparefail() started timing only for a negative result.
    
    Return OK or a negative errno from pm_changestate() and map a positive
    veto to -EBUSY, which is what in-tree prepare() callbacks already use for
    "not ready for this state".  Test the statistics helper against 'ret != OK'
    so a positive veto is counted too.  pm_idle() keeps its upstream form.
    
    Signed-off-by: Honghao Xiang <[email protected]>
---
 drivers/power/pm/pm_changestate.c | 14 +++++++++++++-
 drivers/power/pm/pm_idle.c        |  6 +++---
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/drivers/power/pm/pm_changestate.c 
b/drivers/power/pm/pm_changestate.c
index 791c02607d0..580fee2eb84 100644
--- a/drivers/power/pm/pm_changestate.c
+++ b/drivers/power/pm/pm_changestate.c
@@ -27,6 +27,7 @@
 #include <nuttx/config.h>
 
 #include <assert.h>
+#include <errno.h>
 #include <stdlib.h>
 
 #include <nuttx/power/pm.h>
@@ -119,7 +120,7 @@ static void pm_stats_preparefail(int domain,
       pf->state = PM_RESTORE;
     }
 
-  if (ret < 0)
+  if (ret != OK)
     {
       clock_systime_timespec(&pf->start);
       pf->state = newstate;
@@ -368,6 +369,17 @@ int pm_changestate(int domain, enum pm_state_e newstate)
   /* Restore the interrupt state */
 
   pm_domain_unlock(domain, flags);
+
+  /* A prepare() callback may reject the transition with any non-zero value.
+   * Normalize a positive rejection to a negative errno so that the return
+   * value always follows the NuttX convention of OK or -errno.
+   */
+
+  if (ret > 0)
+    {
+      ret = -EBUSY;
+    }
+
   return ret;
 }
 
diff --git a/drivers/power/pm/pm_idle.c b/drivers/power/pm/pm_idle.c
index d8c85436d91..8ffe82afd4f 100644
--- a/drivers/power/pm/pm_idle.c
+++ b/drivers/power/pm/pm_idle.c
@@ -158,7 +158,7 @@ void pm_idle(pm_idle_handler_t handler)
   newstate = pm_checkstate(domain);
 
   ret = pm_changestate(domain, newstate);
-  if (ret != OK)
+  if (ret < 0)
     {
       newstate = oldstate;
     }
@@ -185,7 +185,7 @@ void pm_idle(pm_idle_handler_t handler)
     {
       systemstate = pm_checkstate(PM_IDLE_DOMAIN);
       ret         = pm_changestate(PM_IDLE_DOMAIN, systemstate);
-      if (ret != OK)
+      if (ret < 0)
         {
           systemstate = PM_NORMAL;
         }
@@ -241,7 +241,7 @@ void pm_idle(pm_idle_handler_t handler)
 
   newstate = pm_checkstate(PM_IDLE_DOMAIN);
   ret      = pm_changestate(PM_IDLE_DOMAIN, newstate);
-  if (ret != OK)
+  if (ret < 0)
     {
       newstate = PM_NORMAL;
     }

Reply via email to