Linux Power Management development
 help / color / mirror / Atom feed
* [PATCH next] sched,freezer: prevent tasks from escaping being frozen
@ 2025-07-03 13:34 Chen Ridong
  2025-07-03 17:01 ` Michal Koutný
  0 siblings, 1 reply; 15+ messages in thread
From: Chen Ridong @ 2025-07-03 13:34 UTC (permalink / raw)
  To: peterz, rafael, pavel, timvp, tj, mkoutny
  Cc: linux-pm, linux-kernel, lujialin4, chenridong

From: Chen Ridong <chenridong@huawei.com>

The commit cff5f49d433f ("cgroup_freezer: cgroup_freezing: Check if not
frozen") modified the cgroup_freezing() logic to also verify that the
FROZEN flag is not set, which affects the return value of the freezing()
function.

In __refrigerator(), the FROZEN flag is set before checking whether the
task should be frozen. This creates a race condition where:
1. The task's FROZEN flag is set.
2. The cgroup freezer state changes to FROZEN (Can be triggered by reading
   freezer.state).
3. freezing() is called and returns false.

As a result, the task may escape being frozen when it should be.

To fix this, move the setting of the FROZEN flag to occur just before
schedule(). This ensures the flag is only set when we're certain the
task must be switched out.

Fixes: cff5f49d433f ("cgroup_freezer: cgroup_freezing: Check if not frozen")
Reported-by: Zhong Jiawei<zhongjiawei1@huawei.com>
Signed-off-by: Chen Ridong <chenridong@huawei.com>
---
 kernel/freezer.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/kernel/freezer.c b/kernel/freezer.c
index 8d530d0949ff..89edd7550d27 100644
--- a/kernel/freezer.c
+++ b/kernel/freezer.c
@@ -71,12 +71,6 @@ bool __refrigerator(bool check_kthr_stop)
 	for (;;) {
 		bool freeze;
 
-		raw_spin_lock_irq(&current->pi_lock);
-		WRITE_ONCE(current->__state, TASK_FROZEN);
-		/* unstale saved_state so that __thaw_task() will wake us up */
-		current->saved_state = TASK_RUNNING;
-		raw_spin_unlock_irq(&current->pi_lock);
-
 		spin_lock_irq(&freezer_lock);
 		freeze = freezing(current) && !(check_kthr_stop && kthread_should_stop());
 		spin_unlock_irq(&freezer_lock);
@@ -84,6 +78,12 @@ bool __refrigerator(bool check_kthr_stop)
 		if (!freeze)
 			break;
 
+		raw_spin_lock_irq(&current->pi_lock);
+		WRITE_ONCE(current->__state, TASK_FROZEN);
+		/* unstale saved_state so that __thaw_task() will wake us up */
+		current->saved_state = TASK_RUNNING;
+		raw_spin_unlock_irq(&current->pi_lock);
+
 		was_frozen = true;
 		schedule();
 	}
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2025-07-11  0:51 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-03 13:34 [PATCH next] sched,freezer: prevent tasks from escaping being frozen Chen Ridong
2025-07-03 17:01 ` Michal Koutný
2025-07-04  3:02   ` Chen Ridong
2025-07-04  3:11     ` Chen Ridong
2025-07-04  7:57       ` Peter Zijlstra
2025-07-04 10:25         ` Chen Ridong
2025-07-07  4:02           ` Chen Ridong
2025-07-07 10:10             ` Peter Zijlstra
2025-07-07 11:32               ` Chen Ridong
2025-07-08  7:28                 ` Peter Zijlstra
2025-07-08 15:35                   ` Tim Van Patten
2025-07-10 15:44             ` Michal Koutný
2025-07-11  0:51               ` Chen Ridong
2025-07-07 16:38     ` Michal Koutný
2025-07-08  1:38       ` Chen Ridong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox