Skip to contentSkip to conversation

freezer: Check frozen() before freezing() in freeze_task()

PATCH Applied Samuel Wu 3 participants 3 messages Nov 15 – Nov 20 Freezer

lore
Share & tools
lore

Samuel Wu

author
#0 lore raw

PATCH 1/1 freezer: Check frozen() before freezing() in freeze_task()

During the freeze stage of suspend, there are multiple passes over the same list of tasks. Each pass tries to freeze non-frozen tasks, meaning that the number of frozen tasks increase monotonically, and that each subsequent pass will mostly be iterating over frozen tasks. Therefore, it is faster to *first* check that a task is frozen before other checks, especially since the frozen check is computationally trivial.

Check frozen(p) before !freezing(p) to short circuit the more expensive freezing_slow_path() calls, while retaining identical return semantics.

On a v7.3 Pixel 9 device, this consistently reduces freeze_processes latency by ~10% (~4.5 ms).

---
 kernel/freezer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/freezer.c +1 -1
index a76bf957fb32..1adf09321389 100644
@@ -166,7 +166,7 @@ bool freeze_task(struct task_struct *p)
 	unsigned long flags;
 
 	spin_lock_irqsave(&freezer_lock, flags);
-	if (!freezing(p) || frozen(p) || __freeze_task(p)) {
+	if (frozen(p) || !freezing(p) || __freeze_task(p)) {
 		spin_unlock_irqrestore(&freezer_lock, flags);
 		return false;
 	}
Signature · 1 line
2.56.0.679.gc527c65306-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Samuel Wu

During the freeze stage of suspend, there are multiple passes over the same list of tasks. Each pass tries to freeze non-frozen tasks, meaning that the number of frozen tasks increase monotonically, and that each subsequent pass will mostly be iterating over frozen tasks. Therefore, it is faster to *first* check that a task is frozen before other checks, especially since the frozen check is computationally trivial.

Quoted 5 lines

Check frozen(p) before !freezing(p) to short circuit the more expensive freezing_slow_path() calls, while retaining identical return semantics.

On a v7.3 Pixel 9 device, this consistently reduces freeze_processes latency by ~10% (~4.5 ms).

7.3 PIXEL 9??? WHAT HOW? THE BASE ON STOCK IS 6.12!!!! WOW! (Please do for pixel 7, I want flair :c)

Quoted 11 lines

---
kernel/freezer.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
kernel/freezer.c +0 -0
index a76bf957fb32..1adf09321389 100644

kernel/freezer.c bool freeze_task(struct task_struct *p) · line 172

Quoted 8 lines
kernel/freezer.c +1 -1
@@ -166,7 +166,7 @@ bool freeze_task(struct task_struct *p)
 	unsigned long flags;
 
 	spin_lock_irqsave(&freezer_lock, flags);
-	if (!freezing(p) || frozen(p) || __freeze_task(p)) {
+	if (frozen(p) || !freezing(p) || __freeze_task(p)) {
 		spin_unlock_irqrestore(&freezer_lock, flags);
 		return false;
 	}

Good good.

Quoted 1 line

--- Thanks!

maintainer Applied
#2 lore raw
Quoted 40 lines · Bradley Morgan
Quoted 7 lines · Samuel Wu

During the freeze stage of suspend, there are multiple passes over the same list of tasks. Each pass tries to freeze non-frozen tasks, meaning that the number of frozen tasks increase monotonically, and that each subsequent pass will mostly be iterating over frozen tasks. Therefore, it is faster to *first* check that a task is frozen before other checks, especially since the frozen check is computationally trivial.

Quoted 5 lines

Check frozen(p) before !freezing(p) to short circuit the more expensive freezing_slow_path() calls, while retaining identical return semantics.

On a v7.3 Pixel 9 device, this consistently reduces freeze_processes latency by ~10% (~4.5 ms).

7.3 PIXEL 9??? WHAT HOW? THE BASE ON STOCK IS 6.12!!!! WOW! (Please do for pixel 7, I want flair :c)

Quoted 11 lines

---
kernel/freezer.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
kernel/freezer.c +0 -0
index a76bf957fb32..1adf09321389 100644

kernel/freezer.c bool freeze_task(struct task_struct *p) · line 172

Quoted 8 lines
kernel/freezer.c +1 -1
@@ -166,7 +166,7 @@ bool freeze_task(struct task_struct *p)
      unsigned long flags;
 
      spin_lock_irqsave(&freezer_lock, flags);
-      if (!freezing(p) || frozen(p) || __freeze_task(p)) {
+      if (frozen(p) || !freezing(p) || __freeze_task(p)) {
              spin_unlock_irqrestore(&freezer_lock, flags);
              return false;
      }

Good good.

Quoted 1 line

Applied as 7.5 material, thanks!