blob: 732f14f5944f32bf996c418cce89710dd39a0b31 [file] [log] [blame]
Matt Helsley8174f152008-10-18 20:27:19 -07001/*
2 * kernel/freezer.c - Function to freeze a process
3 *
4 * Originally from kernel/power/process.c
5 */
6
7#include <linux/interrupt.h>
8#include <linux/suspend.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -04009#include <linux/export.h>
Matt Helsley8174f152008-10-18 20:27:19 -070010#include <linux/syscalls.h>
11#include <linux/freezer.h>
12
13/*
14 * freezing is complete, mark current process as frozen
15 */
16static inline void frozen_process(void)
17{
18 if (!unlikely(current->flags & PF_NOFREEZE)) {
19 current->flags |= PF_FROZEN;
Mike Frysingeree940d82011-04-25 12:33:15 +020020 smp_wmb();
Matt Helsley8174f152008-10-18 20:27:19 -070021 }
22 clear_freeze_flag(current);
23}
24
25/* Refrigerator is place where frozen processes are stored :-). */
Tejun Heoa0acae02011-11-21 12:32:22 -080026bool __refrigerator(void)
Matt Helsley8174f152008-10-18 20:27:19 -070027{
28 /* Hmm, should we be allowed to suspend when there are realtime
29 processes around? */
Tejun Heoa0acae02011-11-21 12:32:22 -080030 bool was_frozen = false;
Matt Helsley8174f152008-10-18 20:27:19 -070031 long save;
32
33 task_lock(current);
34 if (freezing(current)) {
35 frozen_process();
36 task_unlock(current);
37 } else {
38 task_unlock(current);
Tejun Heoa0acae02011-11-21 12:32:22 -080039 return was_frozen;
Matt Helsley8174f152008-10-18 20:27:19 -070040 }
41 save = current->state;
42 pr_debug("%s entered refrigerator\n", current->comm);
43
44 spin_lock_irq(&current->sighand->siglock);
45 recalc_sigpending(); /* We sent fake signal, clean it up */
46 spin_unlock_irq(&current->sighand->siglock);
47
Thomas Gleixner6301cb92009-07-17 14:15:47 +020048 /* prevent accounting of that task to load */
49 current->flags |= PF_FREEZING;
50
Matt Helsley8174f152008-10-18 20:27:19 -070051 for (;;) {
52 set_current_state(TASK_UNINTERRUPTIBLE);
53 if (!frozen(current))
54 break;
Tejun Heoa0acae02011-11-21 12:32:22 -080055 was_frozen = true;
Matt Helsley8174f152008-10-18 20:27:19 -070056 schedule();
57 }
Thomas Gleixner6301cb92009-07-17 14:15:47 +020058
59 /* Remove the accounting blocker */
60 current->flags &= ~PF_FREEZING;
61
Matt Helsley8174f152008-10-18 20:27:19 -070062 pr_debug("%s left refrigerator\n", current->comm);
Tejun Heo50fb4f7f2011-11-21 12:32:22 -080063
64 /*
65 * Restore saved task state before returning. The mb'd version
66 * needs to be used; otherwise, it might silently break
67 * synchronization which depends on ordered task state change.
68 */
69 set_current_state(save);
Tejun Heoa0acae02011-11-21 12:32:22 -080070
71 return was_frozen;
Matt Helsley8174f152008-10-18 20:27:19 -070072}
Tejun Heoa0acae02011-11-21 12:32:22 -080073EXPORT_SYMBOL(__refrigerator);
Matt Helsley8174f152008-10-18 20:27:19 -070074
75static void fake_signal_wake_up(struct task_struct *p)
76{
77 unsigned long flags;
78
79 spin_lock_irqsave(&p->sighand->siglock, flags);
Tejun Heod6cc7682011-11-04 01:04:52 +010080 signal_wake_up(p, 0);
Matt Helsley8174f152008-10-18 20:27:19 -070081 spin_unlock_irqrestore(&p->sighand->siglock, flags);
82}
83
84/**
85 * freeze_task - send a freeze request to given task
86 * @p: task to send the request to
87 * @sig_only: if set, the request will only be sent if the task has the
88 * PF_FREEZER_NOSIG flag unset
89 * Return value: 'false', if @sig_only is set and the task has
90 * PF_FREEZER_NOSIG set or the task is frozen, 'true', otherwise
91 *
92 * The freeze request is sent by setting the tasks's TIF_FREEZE flag and
93 * either sending a fake signal to it or waking it up, depending on whether
94 * or not it has PF_FREEZER_NOSIG set. If @sig_only is set and the task
95 * has PF_FREEZER_NOSIG set (ie. it is a typical kernel thread), its
96 * TIF_FREEZE flag will not be set.
97 */
98bool freeze_task(struct task_struct *p, bool sig_only)
99{
100 /*
101 * We first check if the task is freezing and next if it has already
102 * been frozen to avoid the race with frozen_process() which first marks
103 * the task as frozen and next clears its TIF_FREEZE.
104 */
105 if (!freezing(p)) {
Mike Frysingeree940d82011-04-25 12:33:15 +0200106 smp_rmb();
Matt Helsley8174f152008-10-18 20:27:19 -0700107 if (frozen(p))
108 return false;
109
110 if (!sig_only || should_send_signal(p))
111 set_freeze_flag(p);
112 else
113 return false;
114 }
115
116 if (should_send_signal(p)) {
Tejun Heo8cfe400c2010-11-26 23:07:27 +0100117 fake_signal_wake_up(p);
118 /*
119 * fake_signal_wake_up() goes through p's scheduler
120 * lock and guarantees that TASK_STOPPED/TRACED ->
121 * TASK_RUNNING transition can't race with task state
122 * testing in try_to_freeze_tasks().
123 */
Matt Helsley8174f152008-10-18 20:27:19 -0700124 } else if (sig_only) {
125 return false;
126 } else {
127 wake_up_state(p, TASK_INTERRUPTIBLE);
128 }
129
130 return true;
131}
132
133void cancel_freezing(struct task_struct *p)
134{
135 unsigned long flags;
136
137 if (freezing(p)) {
138 pr_debug(" clean up: %s\n", p->comm);
139 clear_freeze_flag(p);
140 spin_lock_irqsave(&p->sighand->siglock, flags);
141 recalc_sigpending_and_wake(p);
142 spin_unlock_irqrestore(&p->sighand->siglock, flags);
143 }
144}
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700145
Li Zefan00c2e632008-10-29 14:00:53 -0700146static int __thaw_process(struct task_struct *p)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700147{
148 if (frozen(p)) {
149 p->flags &= ~PF_FROZEN;
150 return 1;
151 }
152 clear_freeze_flag(p);
153 return 0;
154}
155
Li Zefan00c2e632008-10-29 14:00:53 -0700156/*
157 * Wake up a frozen process
158 *
159 * task_lock() is needed to prevent the race with refrigerator() which may
160 * occur if the freezing of tasks fails. Namely, without the lock, if the
161 * freezing of tasks failed, thaw_tasks() might have run before a task in
162 * refrigerator() could call frozen_process(), in which case the task would be
163 * frozen and no one would thaw it.
164 */
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700165int thaw_process(struct task_struct *p)
166{
167 task_lock(p);
168 if (__thaw_process(p) == 1) {
169 task_unlock(p);
170 wake_up_process(p);
171 return 1;
172 }
173 task_unlock(p);
174 return 0;
175}
176EXPORT_SYMBOL(thaw_process);