[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250707084214.GD1613200@noisy.programming.kicks-ass.net>
Date: Mon, 7 Jul 2025 10:42:14 +0200
From: Peter Zijlstra <peterz@...radead.org>
To: Zihuan Zhang <zhangzihuan@...inos.cn>
Cc: "Rafael J. Wysocki" <rafael@...nel.org>, pavel@...nel.org,
len.brown@...el.com, linux-pm@...r.kernel.org,
linux-kernel@...r.kernel.org, Oleg Nesterov <oleg@...hat.com>
Subject: Re: [PATCH v3 1/1] PM / Freezer: Skip zombie/dead processes to
On Mon, Jul 07, 2025 at 09:00:12AM +0800, Zihuan Zhang wrote:
>
> 在 2025/7/4 17:21, Peter Zijlstra 写道:
> > You're missing the obvious. How about we 'fix' the PF_NOFREEZE handling
> > and help all cases that set that and not only zombies?
>
> It sounds like a good idea, but there’s a potential risk in relying solely
> on PF_NOFREEZE: it’s a mutable flag that can be set or cleared dynamically
> during runtime, even within the freeze window.
> If a task changes its PF_NOFREEZE state after passing the early check in
> try_to_freeze_task(), we might skip freezing it incorrectly, leading to
> inconsistent behavior or unexpected task escapes. This is particularly
> tricky for some kernel threads or exit paths where PF_NOFREEZE is
> manipulated as part of cleanup or teardown.
A quick browse through the code seems to suggest that for user tasks,
PF_NOFREEZE is set just like exit_state, once at death.
For kernel threads the situation is a little more complex; but typically
a kthread is spawned with PF_NOFREEZE set, and then some will clear it
again, but always before then calling a TASK_FREEZABLE wait.
The only thing I didn't fully investigate is this
{,un}lock_system_sleep() thing. But that would appear to need at least
the below fixlet.
diff --git a/kernel/power/main.c b/kernel/power/main.c
index 3d484630505a..a415e7d30a2c 100644
--- a/kernel/power/main.c
+++ b/kernel/power/main.c
@@ -52,8 +52,8 @@ void pm_restrict_gfp_mask(void)
unsigned int lock_system_sleep(void)
{
unsigned int flags = current->flags;
- current->flags |= PF_NOFREEZE;
mutex_lock(&system_transition_mutex);
+ current->flags |= PF_NOFREEZE;
return flags;
}
EXPORT_SYMBOL_GPL(lock_system_sleep);
Anyway, this seems to suggest something relatively simple like this here
should do:
diff --git a/kernel/freezer.c b/kernel/freezer.c
index 8d530d0949ff..8b7cecd17564 100644
--- a/kernel/freezer.c
+++ b/kernel/freezer.c
@@ -162,20 +162,22 @@ static bool __freeze_task(struct task_struct *p)
*/
bool freeze_task(struct task_struct *p)
{
- unsigned long flags;
-
- spin_lock_irqsave(&freezer_lock, flags);
- if (!freezing(p) || frozen(p) || __freeze_task(p)) {
- spin_unlock_irqrestore(&freezer_lock, flags);
+ /*
+ * User tasks get NOFREEZE in do_task_dead().
+ */
+ if ((p->flags & (PF_NOFREEZE | PF_KTHREAD)) == PF_NOFREEZE)
return false;
- }
- if (!(p->flags & PF_KTHREAD))
- fake_signal_wake_up(p);
- else
- wake_up_state(p, TASK_NORMAL);
+ scoped_guard (spinlock_irqsave, &freezer_lock) {
+ if (!freezing(p) || frozen(p) || __freeze_task(p))
+ return false;
+
+ if (!(p->flags & PF_KTHREAD))
+ fake_signal_wake_up(p);
+ else
+ wake_up_state(p, TASK_NORMAL);
+ }
- spin_unlock_irqrestore(&freezer_lock, flags);
return true;
}
Powered by blists - more mailing lists