[PATCH v2 3/4] PM: suspend: Assign pm_suspend_target_state earlier for valid debug logs

Zihuan Zhang posted 4 patches 3 months, 3 weeks ago
[PATCH v2 3/4] PM: suspend: Assign pm_suspend_target_state earlier for valid debug logs
Posted by Zihuan Zhang 3 months, 3 weeks ago
pm_suspend_target_state is used in debug logs inside enter_state(), but
it is only assigned inside suspend_devices_and_enter(), which is too late.

This causes early pm_pr_dbg() output to either show incorrect state or
nothing at all, making suspend debugging harder.

Assign pm_suspend_target_state at the beginning of enter_state() to ensure
early log output is meaningful.

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

diff --git a/kernel/power/suspend.c b/kernel/power/suspend.c
index 76b141b9aac0..16172ca22f21 100644
--- a/kernel/power/suspend.c
+++ b/kernel/power/suspend.c
@@ -584,6 +584,8 @@ static int enter_state(suspend_state_t state)
 	if (!mutex_trylock(&system_transition_mutex))
 		return -EBUSY;
 
+	pm_suspend_target_state = state;
+
 	if (state == PM_SUSPEND_TO_IDLE)
 		s2idle_begin();
 
@@ -616,6 +618,7 @@ static int enter_state(suspend_state_t state)
 	suspend_finish();
  Unlock:
 	filesystems_thaw();
+	pm_suspend_target_state = PM_SUSPEND_ON;
 	mutex_unlock(&system_transition_mutex);
 	return error;
 }
-- 
2.25.1
Re: [PATCH v2 3/4] PM: suspend: Assign pm_suspend_target_state earlier for valid debug logs
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:
>
> pm_suspend_target_state is used in debug logs inside enter_state(), but
> it is only assigned inside suspend_devices_and_enter(), which is too late.
>
> This causes early pm_pr_dbg() output to either show incorrect state or
> nothing at all, making suspend debugging harder.
>
> Assign pm_suspend_target_state at the beginning of enter_state() to ensure
> early log output is meaningful.
>
> Signed-off-by: Zihuan Zhang <zhangzihuan@kylinos.cn>
> ---
>  kernel/power/suspend.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/kernel/power/suspend.c b/kernel/power/suspend.c
> index 76b141b9aac0..16172ca22f21 100644
> --- a/kernel/power/suspend.c
> +++ b/kernel/power/suspend.c
> @@ -584,6 +584,8 @@ static int enter_state(suspend_state_t state)
>         if (!mutex_trylock(&system_transition_mutex))
>                 return -EBUSY;
>
> +       pm_suspend_target_state = state;
> +
>         if (state == PM_SUSPEND_TO_IDLE)
>                 s2idle_begin();
>
> @@ -616,6 +618,7 @@ static int enter_state(suspend_state_t state)
>         suspend_finish();
>   Unlock:
>         filesystems_thaw();
> +       pm_suspend_target_state = PM_SUSPEND_ON;
>         mutex_unlock(&system_transition_mutex);
>         return error;
>  }
> --

Good catch, but you should remove the existing assignments at the same time.
Re: [PATCH v2 3/4] PM: suspend: Assign pm_suspend_target_state earlier for valid debug logs
Posted by Zihuan Zhang 3 months, 1 week ago
Hi Rafael,

在 2025/7/3 02:58, Rafael J. Wysocki 写道:                       
      
>> On Thu, Jun 19, 2025 at 5:54 AM Zihuan Zhang <zhangzihuan@kylinos.cn> wrote:
>>
>> pm_suspend_target_state is used in debug logs inside enter_state(), but
>> it is only assigned inside suspend_devices_and_enter(), which is too late..
>>
>> This causes early pm_pr_dbg() output to either show incorrect state or
>> nothing at all, making suspend debugging harder.
>>
>> Assign pm_suspend_target_state at the beginning of enter_state() to ensure
>> early log output is meaningful.
>>
>> Signed-off-by: Zihuan Zhang
>>   ---
>> kernel/power/suspend.c | 3 +++
>> 1 file changed, 3 insertions(+)
>>
>> diff --git a/kernel/power/suspend.c b/kernel/power/suspend.c
>> index 76b141b9aac0..16172ca22f21 100644
>> --- a/kernel/power/suspend.c
>> +++ b/kernel/power/suspend.c
>> @@ -584,6 +584,8 @@ static int enter_state(suspend_state_t state)
>> if (!mutex_trylock(&system_transition_mutex))
>> return -EBUSY;
>>
>> + pm_suspend_target_state = state;
>> +
>> if (state == PM_SUSPEND_TO_IDLE)
>> s2idle_begin();
>>
>> @@ -616,6 +618,7 @@ static int enter_state(suspend_state_t state)
>> suspend_finish();
>> Unlock:
>> filesystems_thaw();
>> + pm_suspend_target_state = PM_SUSPEND_ON;
>> mutex_unlock(&system_transition_mutex);
>> return error;
>> }}
>>
>> --
>>                      
> Good catch, but you should remove the existing assignments at the
> same time.  
Thanks for your review and feedback.

You’re right that ideally pm_suspend_target_state should only be 
assigned once in enter_state(), and I initially considered removing the 
original assignment in suspend_devices_and_enter() as well.

However, I noticed that some drivers and subsystems may rely on the 
value of pm_suspend_target_state later in the suspend path, not just for 
logging but also for decision-making (e.g. conditional behavior based on 
suspend state). Because of this, I was cautious about removing the 
original assignment inside suspend_devices_and_enter() without verifying 
all potential dependencies.

Would you consider it acceptable to keep both assignments for now — one 
early for logging purposes, and one later to ensure correctness and 
compatibility — or do you think it’s preferable to remove the later one 
and carefully audit all usage sites?

Happy to adjust accordingly based on your recommendation.

Best regards,
Zihuan Zhang