diff mbox series

[v1,08/12] PM: sleep: stats: Use locking in dpm_save_failed_dev()

Message ID 3798847.kQq0lBPeGt@kreacher
State New
Headers show
Series None | expand

Commit Message

Rafael J. Wysocki Jan. 22, 2024, 11:33 a.m. UTC
From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

Because dpm_save_failed_dev() may be called simultaneously by multiple
failing device PM functions, the state of the suspend_stats fields
updated by it may become inconsistent.

Prevent that from happening by using a lock in dpm_save_failed_dev().

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
 kernel/power/main.c |    5 +++++
 1 file changed, 5 insertions(+)

Comments

Stanislaw Gruszka Jan. 25, 2024, 7:59 a.m. UTC | #1
On Mon, Jan 22, 2024 at 12:33:53PM +0100, Rafael J. Wysocki wrote:
> From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> 
> Because dpm_save_failed_dev() may be called simultaneously by multiple
> failing device PM functions, the state of the suspend_stats fields
> updated by it may become inconsistent.
> 
> Prevent that from happening by using a lock in dpm_save_failed_dev().
> 
> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Reviewed-by: Stanislaw Gruszka <stanislaw.gruszka@linux.intel.com>

> ---
>  kernel/power/main.c |    5 +++++
>  1 file changed, 5 insertions(+)
> 
> Index: linux-pm/kernel/power/main.c
> ===================================================================
> --- linux-pm.orig/kernel/power/main.c
> +++ linux-pm/kernel/power/main.c
> @@ -323,13 +323,18 @@ struct suspend_stats {
>  };
>  
>  static struct suspend_stats suspend_stats;
> +static DEFINE_MUTEX(suspend_stats_lock);
>  
>  void dpm_save_failed_dev(const char *name)
>  {
> +	mutex_lock(&suspend_stats_lock);
> +
>  	strscpy(suspend_stats.failed_devs[suspend_stats.last_failed_dev],
>  		name, sizeof(suspend_stats.failed_devs[0]));
>  	suspend_stats.last_failed_dev++;
>  	suspend_stats.last_failed_dev %= REC_FAILED_NUM;
> +
> +	mutex_unlock(&suspend_stats_lock);
>  }
>  
>  void dpm_save_failed_step(enum suspend_stat_step step)
> 
> 
> 
>
diff mbox series

Patch

Index: linux-pm/kernel/power/main.c
===================================================================
--- linux-pm.orig/kernel/power/main.c
+++ linux-pm/kernel/power/main.c
@@ -323,13 +323,18 @@  struct suspend_stats {
 };
 
 static struct suspend_stats suspend_stats;
+static DEFINE_MUTEX(suspend_stats_lock);
 
 void dpm_save_failed_dev(const char *name)
 {
+	mutex_lock(&suspend_stats_lock);
+
 	strscpy(suspend_stats.failed_devs[suspend_stats.last_failed_dev],
 		name, sizeof(suspend_stats.failed_devs[0]));
 	suspend_stats.last_failed_dev++;
 	suspend_stats.last_failed_dev %= REC_FAILED_NUM;
+
+	mutex_unlock(&suspend_stats_lock);
 }
 
 void dpm_save_failed_step(enum suspend_stat_step step)