2 * kernel/freezer.c - Function to freeze a process
4 * Originally from kernel/power/process.c
7 #include <linux/interrupt.h>
8 #include <linux/suspend.h>
9 #include <linux/export.h>
10 #include <linux/syscalls.h>
11 #include <linux/freezer.h>
12 #include <linux/kthread.h>
14 /* protects freezing and frozen transitions */
15 static DEFINE_SPINLOCK(freezer_lock);
17 /* Refrigerator is place where frozen processes are stored :-). */
18 bool __refrigerator(bool check_kthr_stop)
20 /* Hmm, should we be allowed to suspend when there are realtime
22 bool was_frozen = false;
25 spin_lock_irq(&freezer_lock);
26 if (!freezing(current)) {
27 spin_unlock_irq(&freezer_lock);
30 if (!(current->flags & PF_NOFREEZE))
31 current->flags |= PF_FROZEN;
32 clear_freeze_flag(current);
33 spin_unlock_irq(&freezer_lock);
35 save = current->state;
36 pr_debug("%s entered refrigerator\n", current->comm);
38 spin_lock_irq(¤t->sighand->siglock);
39 recalc_sigpending(); /* We sent fake signal, clean it up */
40 spin_unlock_irq(¤t->sighand->siglock);
42 /* prevent accounting of that task to load */
43 current->flags |= PF_FREEZING;
46 set_current_state(TASK_UNINTERRUPTIBLE);
47 if (!frozen(current) ||
48 (check_kthr_stop && kthread_should_stop()))
54 /* Remove the accounting blocker */
55 current->flags &= ~PF_FREEZING;
57 pr_debug("%s left refrigerator\n", current->comm);
60 * Restore saved task state before returning. The mb'd version
61 * needs to be used; otherwise, it might silently break
62 * synchronization which depends on ordered task state change.
64 set_current_state(save);
68 EXPORT_SYMBOL(__refrigerator);
70 static void fake_signal_wake_up(struct task_struct *p)
74 spin_lock_irqsave(&p->sighand->siglock, flags);
76 spin_unlock_irqrestore(&p->sighand->siglock, flags);
80 * freeze_task - send a freeze request to given task
81 * @p: task to send the request to
82 * @sig_only: if set, the request will only be sent if the task has the
83 * PF_FREEZER_NOSIG flag unset
84 * Return value: 'false', if @sig_only is set and the task has
85 * PF_FREEZER_NOSIG set or the task is frozen, 'true', otherwise
87 * The freeze request is sent by setting the tasks's TIF_FREEZE flag and
88 * either sending a fake signal to it or waking it up, depending on whether
89 * or not it has PF_FREEZER_NOSIG set. If @sig_only is set and the task
90 * has PF_FREEZER_NOSIG set (ie. it is a typical kernel thread), its
91 * TIF_FREEZE flag will not be set.
93 bool freeze_task(struct task_struct *p, bool sig_only)
98 spin_lock_irqsave(&freezer_lock, flags);
100 if (sig_only && !should_send_signal(p))
108 if (should_send_signal(p)) {
109 fake_signal_wake_up(p);
111 * fake_signal_wake_up() goes through p's scheduler
112 * lock and guarantees that TASK_STOPPED/TRACED ->
113 * TASK_RUNNING transition can't race with task state
114 * testing in try_to_freeze_tasks().
117 wake_up_state(p, TASK_INTERRUPTIBLE);
121 spin_unlock_irqrestore(&freezer_lock, flags);
125 void cancel_freezing(struct task_struct *p)
129 spin_lock_irqsave(&freezer_lock, flags);
131 pr_debug(" clean up: %s\n", p->comm);
132 clear_freeze_flag(p);
133 spin_lock(&p->sighand->siglock);
134 recalc_sigpending_and_wake(p);
135 spin_unlock(&p->sighand->siglock);
137 spin_unlock_irqrestore(&freezer_lock, flags);
141 * Wake up a frozen task
143 * task_lock() is needed to prevent the race with refrigerator() which may
144 * occur if the freezing of tasks fails. Namely, without the lock, if the
145 * freezing of tasks failed, thaw_tasks() might have run before a task in
146 * refrigerator() could call frozen_process(), in which case the task would be
147 * frozen and no one would thaw it.
149 void __thaw_task(struct task_struct *p)
153 spin_lock_irqsave(&freezer_lock, flags);
155 p->flags &= ~PF_FROZEN;
158 clear_freeze_flag(p);
160 spin_unlock_irqrestore(&freezer_lock, flags);