[PATCH v2 4/4] PM: suspend: Simplify state check using sleep_state_supported()

Zihuan Zhang posted 4 patches 3 months, 3 weeks ago
[PATCH v2 4/4] PM: suspend: Simplify state check using sleep_state_supported()
Posted by Zihuan Zhang 3 months, 3 weeks ago
Currently enter_state() open-codes state validation using
`if (state == PM_SUSPEND_TO_IDLE) ... else if (!valid_state(state)) ...`.

This can be simplified by calling sleep_state_supported(), which already
encodes this logic. This improves clarity and reduces duplication.

Signed-off-by: Zihuan Zhang <zhangzihuan@kylinos.cn>
---
 kernel/power/suspend.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/kernel/power/suspend.c b/kernel/power/suspend.c
index 16172ca22f21..b95c7a80ef20 100644
--- a/kernel/power/suspend.c
+++ b/kernel/power/suspend.c
@@ -571,16 +571,10 @@ static int enter_state(suspend_state_t state)
 	int error;
 
 	trace_suspend_resume(TPS("suspend_enter"), state, true);
-	if (state == PM_SUSPEND_TO_IDLE) {
-#ifdef CONFIG_PM_DEBUG
-		if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) {
-			pr_warn("Unsupported test mode for suspend to idle, please choose none/freezer/devices/platform.\n");
-			return -EAGAIN;
-		}
-#endif
-	} else if (!valid_state(state)) {
-		return -EINVAL;
-	}
+
+	if (!sleep_state_supported(state))
+		return -ENOSYS;
+
 	if (!mutex_trylock(&system_transition_mutex))
 		return -EBUSY;
 
-- 
2.25.1
Re: [PATCH v2 4/4] PM: suspend: Simplify state check using sleep_state_supported()
Posted by Rafael J. Wysocki 3 months, 1 week ago
On Thu, Jun 19, 2025 at 5:54 AM Zihuan Zhang <zhangzihuan@kylinos.cn> wrote:
>
> Currently enter_state() open-codes state validation using
> `if (state == PM_SUSPEND_TO_IDLE) ... else if (!valid_state(state)) ...`.
>
> This can be simplified by calling sleep_state_supported(), which already
> encodes this logic. This improves clarity and reduces duplication.
>
> Signed-off-by: Zihuan Zhang <zhangzihuan@kylinos.cn>
> ---
>  kernel/power/suspend.c | 14 ++++----------
>  1 file changed, 4 insertions(+), 10 deletions(-)
>
> diff --git a/kernel/power/suspend.c b/kernel/power/suspend.c
> index 16172ca22f21..b95c7a80ef20 100644
> --- a/kernel/power/suspend.c
> +++ b/kernel/power/suspend.c
> @@ -571,16 +571,10 @@ static int enter_state(suspend_state_t state)
>         int error;
>
>         trace_suspend_resume(TPS("suspend_enter"), state, true);
> -       if (state == PM_SUSPEND_TO_IDLE) {
> -#ifdef CONFIG_PM_DEBUG
> -               if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) {
> -                       pr_warn("Unsupported test mode for suspend to idle, please choose none/freezer/devices/platform.\n");
> -                       return -EAGAIN;
> -               }
> -#endif
> -       } else if (!valid_state(state)) {
> -               return -EINVAL;
> -       }
> +
> +       if (!sleep_state_supported(state))
> +               return -ENOSYS;
> +

The code before and after is obviously not the same, so no.

If you do a cleanup, don't change the behavior.

If you want to change the behavior, there needs to be a good enough
reason.  A cleanup is not it.

>         if (!mutex_trylock(&system_transition_mutex))
>                 return -EBUSY;
>
> --