Skip to contentSkip to conversation

PM: sleep: Allow pm_restrict_gfp_mask() stacking

PATCH In review Rafael J. Wysocki 3 participants 4 messages Oct 28, 2025 – Oct 30, 2025

Subsystems (2)
lore
Share & tools
lore

Rafael J. Wysocki

author maintainer
#0 lore raw

PATCH 1/1 PM: sleep: Allow pm_restrict_gfp_mask() stacking

From: Rafael J. Wysocki <[email protected]>

Allow pm_restrict_gfp_mask() to be called many times in a row to avoid issues with calling dpm_suspend_start() when the GFP mask has been already restricted.

Only the first invocation of pm_restrict_gfp_mask() will actually restrict the GFP mask and the subsequent calls will warn if there is a mismatch between the expected allowed GFP mask and the actual one.

Moreover, if pm_restrict_gfp_mask() is called many times in a row, pm_restore_gfp_mask() needs to be called matching number of times in a row to actually restore the GFP mask. Calling it when the GFP mask has not been restricted will cause it to warn.

This is necessary for the GFP mask restriction starting in hibernation_snapshot() to continue throughout the entire hibernation flow until it completes or it is aborted (either by a wakeup event or by an error).

Fixes 449c9c02537a1 ("PM: hibernate: Restrict GFP mask in hibernation_snapshot()")
Fixes 469d80a3712c ("PM: hibernate: Fix hybrid-sleep")

---

This supersedes

https://lore.kernel.org/linux-pm/[email protected]/

as it allows the GFP mask to be restricted across the entire hibernation path.

---
 kernel/power/hibernate.c |    4 ----
 kernel/power/main.c      |   22 +++++++++++++++++-----
 2 files changed, 17 insertions(+), 9 deletions(-)
kernel/power/hibernate.c +0 -4
@@ -706,7 +706,6 @@ static void power_down(void)
 
 #ifdef CONFIG_SUSPEND
 	if (hibernation_mode == HIBERNATION_SUSPEND) {
-		pm_restore_gfp_mask();
 		error = suspend_devices_and_enter(mem_sleep_current);
 		if (!error)
 			goto exit;
@@ -746,9 +745,6 @@ static void power_down(void)
 		cpu_relax();
 
 exit:
-	/* Match the pm_restore_gfp_mask() call in hibernate(). */
-	pm_restrict_gfp_mask();
-
 	/* Restore swap signature. */
 	error = swsusp_unmark();
 	if (error)
kernel/power/main.c +17 -5
@@ -31,23 +31,35 @@
  * held, unless the suspend/hibernate code is guaranteed not to run in parallel
  * with that modification).
  */
+static unsigned int saved_gfp_count;
 static gfp_t saved_gfp_mask;
 
 void pm_restore_gfp_mask(void)
 {
 	WARN_ON(!mutex_is_locked(&system_transition_mutex));
-	if (saved_gfp_mask) {
-		gfp_allowed_mask = saved_gfp_mask;
-		saved_gfp_mask = 0;
-	}
+
+	if (WARN_ON(!saved_gfp_count) || --saved_gfp_count)
+		return;
+
+	gfp_allowed_mask = saved_gfp_mask;
+	saved_gfp_mask = 0;
+
+	pm_pr_dbg("GFP mask restored\n");
 }
 
 void pm_restrict_gfp_mask(void)
 {
 	WARN_ON(!mutex_is_locked(&system_transition_mutex));
-	WARN_ON(saved_gfp_mask);
+
+	if (saved_gfp_count++) {
+		WARN_ON((saved_gfp_mask & ~(__GFP_IO | __GFP_FS)) != gfp_allowed_mask);
+		return;
+	}
+
 	saved_gfp_mask = gfp_allowed_mask;
 	gfp_allowed_mask &= ~(__GFP_IO | __GFP_FS);
+
+	pm_pr_dbg("GFP mask restricted\n");
 }
 
 unsigned int lock_system_sleep(void)
Reviewed-by Tested-by on v1 1/1
#1 lore raw
Quoted 20 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Allow pm_restrict_gfp_mask() to be called many times in a row to avoid issues with calling dpm_suspend_start() when the GFP mask has been already restricted.

Only the first invocation of pm_restrict_gfp_mask() will actually restrict the GFP mask and the subsequent calls will warn if there is a mismatch between the expected allowed GFP mask and the actual one.

Moreover, if pm_restrict_gfp_mask() is called many times in a row, pm_restore_gfp_mask() needs to be called matching number of times in a row to actually restore the GFP mask. Calling it when the GFP mask has not been restricted will cause it to warn.

This is necessary for the GFP mask restriction starting in hibernation_snapshot() to continue throughout the entire hibernation flow until it completes or it is aborted (either by a wakeup event or by an error).

Quoted 6 lines
Fixes 449c9c02537a1 ("PM: hibernate: Restrict GFP mask in hibernation_snapshot()")
Fixes 469d80a3712c ("PM: hibernate: Fix hybrid-sleep")

Great idea. Looks good to me, and it passes the S4 tests on my side.

Quoted 79 lines

---

This supersedes

https://lore.kernel.org/linux-pm/[email protected]/

as it allows the GFP mask to be restricted across the entire hibernation path.

---
  kernel/power/hibernate.c |    4 ----
  kernel/power/main.c      |   22 +++++++++++++++++-----
  2 files changed, 17 insertions(+), 9 deletions(-)
kernel/power/hibernate.c +0 -4
@@ -706,7 +706,6 @@ static void power_down(void)
 
  #ifdef CONFIG_SUSPEND
  	if (hibernation_mode == HIBERNATION_SUSPEND) {
-		pm_restore_gfp_mask();
  		error = suspend_devices_and_enter(mem_sleep_current);
  		if (!error)
  			goto exit;
@@ -746,9 +745,6 @@ static void power_down(void)
  		cpu_relax();
 
  exit:
-	/* Match the pm_restore_gfp_mask() call in hibernate(). */
-	pm_restrict_gfp_mask();
-
  	/* Restore swap signature. */
  	error = swsusp_unmark();
  	if (error)
kernel/power/main.c +17 -5
@@ -31,23 +31,35 @@
   * held, unless the suspend/hibernate code is guaranteed not to run in parallel
   * with that modification).
   */
+static unsigned int saved_gfp_count;
  static gfp_t saved_gfp_mask;
 
  void pm_restore_gfp_mask(void)
  {
  	WARN_ON(!mutex_is_locked(&system_transition_mutex));
-	if (saved_gfp_mask) {
-		gfp_allowed_mask = saved_gfp_mask;
-		saved_gfp_mask = 0;
-	}
+
+	if (WARN_ON(!saved_gfp_count) || --saved_gfp_count)
+		return;
+
+	gfp_allowed_mask = saved_gfp_mask;
+	saved_gfp_mask = 0;
+
+	pm_pr_dbg("GFP mask restored\n");
  }
 
  void pm_restrict_gfp_mask(void)
  {
  	WARN_ON(!mutex_is_locked(&system_transition_mutex));
-	WARN_ON(saved_gfp_mask);
+
+	if (saved_gfp_count++) {
+		WARN_ON((saved_gfp_mask & ~(__GFP_IO | __GFP_FS)) != gfp_allowed_mask);
+		return;
+	}
+
  	saved_gfp_mask = gfp_allowed_mask;
  	gfp_allowed_mask &= ~(__GFP_IO | __GFP_FS);
+
+	pm_pr_dbg("GFP mask restricted\n");
  }
 
  unsigned int lock_system_sleep(void)
author maintainer
#2 lore raw
Quoted 29 lines · Mario Limonciello
Quoted 20 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Allow pm_restrict_gfp_mask() to be called many times in a row to avoid issues with calling dpm_suspend_start() when the GFP mask has been already restricted.

Only the first invocation of pm_restrict_gfp_mask() will actually restrict the GFP mask and the subsequent calls will warn if there is a mismatch between the expected allowed GFP mask and the actual one.

Moreover, if pm_restrict_gfp_mask() is called many times in a row, pm_restore_gfp_mask() needs to be called matching number of times in a row to actually restore the GFP mask. Calling it when the GFP mask has not been restricted will cause it to warn.

This is necessary for the GFP mask restriction starting in hibernation_snapshot() to continue throughout the entire hibernation flow until it completes or it is aborted (either by a wakeup event or by an error).

Quoted 6 lines
Fixes 449c9c02537a1 ("PM: hibernate: Restrict GFP mask in hibernation_snapshot()")
Fixes 469d80a3712c ("PM: hibernate: Fix hybrid-sleep")
Quoted 4 lines

Great idea. Looks good to me, and it passes the S4 tests on my side.

Great, thanks!

Tested-by on v1 1/1
#3 lore raw
In reply to Rafael J. Wysocki
Quoted 26 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Allow pm_restrict_gfp_mask() to be called many times in a row to avoid issues with calling dpm_suspend_start() when the GFP mask has been already restricted.

Only the first invocation of pm_restrict_gfp_mask() will actually restrict the GFP mask and the subsequent calls will warn if there is a mismatch between the expected allowed GFP mask and the actual one.

Moreover, if pm_restrict_gfp_mask() is called many times in a row, pm_restore_gfp_mask() needs to be called matching number of times in a row to actually restore the GFP mask. Calling it when the GFP mask has not been restricted will cause it to warn.

This is necessary for the GFP mask restriction starting in hibernation_snapshot() to continue throughout the entire hibernation flow until it completes or it is aborted (either by a wakeup event or by an error).

Fixes 449c9c02537a1 ("PM: hibernate: Restrict GFP mask in hibernation_snapshot()")
Fixes 469d80a3712c ("PM: hibernate: Fix hybrid-sleep")
Quoted 7 lines

---

This supersedes

https://lore.kernel.org/linux-pm/[email protected]/

as it allows the GFP mask to be restricted across the entire hibernation path.

(Helgaas told me about "b4 shazam" and I've been applying all kinds of interesting patches willy-nilly lately :) )

Quoted 72 lines
---
  kernel/power/hibernate.c |    4 ----
  kernel/power/main.c      |   22 +++++++++++++++++-----
  2 files changed, 17 insertions(+), 9 deletions(-)
kernel/power/hibernate.c +0 -4
@@ -706,7 +706,6 @@ static void power_down(void)
 
  #ifdef CONFIG_SUSPEND
  	if (hibernation_mode == HIBERNATION_SUSPEND) {
-		pm_restore_gfp_mask();
  		error = suspend_devices_and_enter(mem_sleep_current);
  		if (!error)
  			goto exit;
@@ -746,9 +745,6 @@ static void power_down(void)
  		cpu_relax();
 
  exit:
-	/* Match the pm_restore_gfp_mask() call in hibernate(). */
-	pm_restrict_gfp_mask();
-
  	/* Restore swap signature. */
  	error = swsusp_unmark();
  	if (error)
kernel/power/main.c +17 -5
@@ -31,23 +31,35 @@
   * held, unless the suspend/hibernate code is guaranteed not to run in parallel
   * with that modification).
   */
+static unsigned int saved_gfp_count;
  static gfp_t saved_gfp_mask;
 
  void pm_restore_gfp_mask(void)
  {
  	WARN_ON(!mutex_is_locked(&system_transition_mutex));
-	if (saved_gfp_mask) {
-		gfp_allowed_mask = saved_gfp_mask;
-		saved_gfp_mask = 0;
-	}
+
+	if (WARN_ON(!saved_gfp_count) || --saved_gfp_count)
+		return;
+
+	gfp_allowed_mask = saved_gfp_mask;
+	saved_gfp_mask = 0;
+
+	pm_pr_dbg("GFP mask restored\n");
  }
 
  void pm_restrict_gfp_mask(void)
  {
  	WARN_ON(!mutex_is_locked(&system_transition_mutex));
-	WARN_ON(saved_gfp_mask);
+
+	if (saved_gfp_count++) {
+		WARN_ON((saved_gfp_mask & ~(__GFP_IO | __GFP_FS)) != gfp_allowed_mask);
+		return;
+	}
+
  	saved_gfp_mask = gfp_allowed_mask;
  	gfp_allowed_mask &= ~(__GFP_IO | __GFP_FS);
+
+	pm_pr_dbg("GFP mask restricted\n");
  }
 
  unsigned int lock_system_sleep(void)
Signature · 1 line
Kenneth R. Crudup / Sr. SW Engineer, Scott County Consulting, Orange County CA