mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git
synced 2024-12-28 16:52:18 +00:00
f5d39b0208
Rewrite the core freezer to behave better wrt thawing and be simpler in general. By replacing PF_FROZEN with TASK_FROZEN, a special block state, it is ensured frozen tasks stay frozen until thawed and don't randomly wake up early, as is currently possible. As such, it does away with PF_FROZEN and PF_FREEZER_SKIP, freeing up two PF_flags (yay!). Specifically; the current scheme works a little like: freezer_do_not_count(); schedule(); freezer_count(); And either the task is blocked, or it lands in try_to_freezer() through freezer_count(). Now, when it is blocked, the freezer considers it frozen and continues. However, on thawing, once pm_freezing is cleared, freezer_count() stops working, and any random/spurious wakeup will let a task run before its time. That is, thawing tries to thaw things in explicit order; kernel threads and workqueues before doing bringing SMP back before userspace etc.. However due to the above mentioned races it is entirely possible for userspace tasks to thaw (by accident) before SMP is back. This can be a fatal problem in asymmetric ISA architectures (eg ARMv9) where the userspace task requires a special CPU to run. As said; replace this with a special task state TASK_FROZEN and add the following state transitions: TASK_FREEZABLE -> TASK_FROZEN __TASK_STOPPED -> TASK_FROZEN __TASK_TRACED -> TASK_FROZEN The new TASK_FREEZABLE can be set on any state part of TASK_NORMAL (IOW. TASK_INTERRUPTIBLE and TASK_UNINTERRUPTIBLE) -- any such state is already required to deal with spurious wakeups and the freezer causes one such when thawing the task (since the original state is lost). The special __TASK_{STOPPED,TRACED} states *can* be restored since their canonical state is in ->jobctl. With this, frozen tasks need an explicit TASK_FROZEN wakeup and are free of undue (early / spurious) wakeups. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Ingo Molnar <mingo@kernel.org> Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Link: https://lore.kernel.org/r/20220822114649.055452969@infradead.org
91 lines
2.3 KiB
C
91 lines
2.3 KiB
C
/* SPDX-License-Identifier: GPL-2.0 */
|
|
/* Freezer declarations */
|
|
|
|
#ifndef FREEZER_H_INCLUDED
|
|
#define FREEZER_H_INCLUDED
|
|
|
|
#include <linux/debug_locks.h>
|
|
#include <linux/sched.h>
|
|
#include <linux/wait.h>
|
|
#include <linux/atomic.h>
|
|
#include <linux/jump_label.h>
|
|
|
|
#ifdef CONFIG_FREEZER
|
|
DECLARE_STATIC_KEY_FALSE(freezer_active);
|
|
|
|
extern bool pm_freezing; /* PM freezing in effect */
|
|
extern bool pm_nosig_freezing; /* PM nosig freezing in effect */
|
|
|
|
/*
|
|
* Timeout for stopping processes
|
|
*/
|
|
extern unsigned int freeze_timeout_msecs;
|
|
|
|
/*
|
|
* Check if a process has been frozen
|
|
*/
|
|
extern bool frozen(struct task_struct *p);
|
|
|
|
extern bool freezing_slow_path(struct task_struct *p);
|
|
|
|
/*
|
|
* Check if there is a request to freeze a process
|
|
*/
|
|
static inline bool freezing(struct task_struct *p)
|
|
{
|
|
if (static_branch_unlikely(&freezer_active))
|
|
return freezing_slow_path(p);
|
|
|
|
return false;
|
|
}
|
|
|
|
/* Takes and releases task alloc lock using task_lock() */
|
|
extern void __thaw_task(struct task_struct *t);
|
|
|
|
extern bool __refrigerator(bool check_kthr_stop);
|
|
extern int freeze_processes(void);
|
|
extern int freeze_kernel_threads(void);
|
|
extern void thaw_processes(void);
|
|
extern void thaw_kernel_threads(void);
|
|
|
|
static inline bool try_to_freeze(void)
|
|
{
|
|
might_sleep();
|
|
if (likely(!freezing(current)))
|
|
return false;
|
|
if (!(current->flags & PF_NOFREEZE))
|
|
debug_check_no_locks_held();
|
|
return __refrigerator(false);
|
|
}
|
|
|
|
extern bool freeze_task(struct task_struct *p);
|
|
extern bool set_freezable(void);
|
|
|
|
#ifdef CONFIG_CGROUP_FREEZER
|
|
extern bool cgroup_freezing(struct task_struct *task);
|
|
#else /* !CONFIG_CGROUP_FREEZER */
|
|
static inline bool cgroup_freezing(struct task_struct *task)
|
|
{
|
|
return false;
|
|
}
|
|
#endif /* !CONFIG_CGROUP_FREEZER */
|
|
|
|
#else /* !CONFIG_FREEZER */
|
|
static inline bool frozen(struct task_struct *p) { return false; }
|
|
static inline bool freezing(struct task_struct *p) { return false; }
|
|
static inline void __thaw_task(struct task_struct *t) {}
|
|
|
|
static inline bool __refrigerator(bool check_kthr_stop) { return false; }
|
|
static inline int freeze_processes(void) { return -ENOSYS; }
|
|
static inline int freeze_kernel_threads(void) { return -ENOSYS; }
|
|
static inline void thaw_processes(void) {}
|
|
static inline void thaw_kernel_threads(void) {}
|
|
|
|
static inline bool try_to_freeze(void) { return false; }
|
|
|
|
static inline void set_freezable(void) {}
|
|
|
|
#endif /* !CONFIG_FREEZER */
|
|
|
|
#endif /* FREEZER_H_INCLUDED */
|