freezer: unexport refrigerator() and update try_to_freeze() slightly
[linux-2.6-block.git] / kernel / freezer.c
1 /*
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>
9 #include <linux/export.h>
10 #include <linux/syscalls.h>
11 #include <linux/freezer.h>
12
13 /*
14  * freezing is complete, mark current process as frozen
15  */
16 static inline void frozen_process(void)
17 {
18         if (!unlikely(current->flags & PF_NOFREEZE)) {
19                 current->flags |= PF_FROZEN;
20                 smp_wmb();
21         }
22         clear_freeze_flag(current);
23 }
24
25 /* Refrigerator is place where frozen processes are stored :-). */
26 bool __refrigerator(void)
27 {
28         /* Hmm, should we be allowed to suspend when there are realtime
29            processes around? */
30         bool was_frozen = false;
31         long save;
32
33         task_lock(current);
34         if (freezing(current)) {
35                 frozen_process();
36                 task_unlock(current);
37         } else {
38                 task_unlock(current);
39                 return was_frozen;
40         }
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
48         /* prevent accounting of that task to load */
49         current->flags |= PF_FREEZING;
50
51         for (;;) {
52                 set_current_state(TASK_UNINTERRUPTIBLE);
53                 if (!frozen(current))
54                         break;
55                 was_frozen = true;
56                 schedule();
57         }
58
59         /* Remove the accounting blocker */
60         current->flags &= ~PF_FREEZING;
61
62         pr_debug("%s left refrigerator\n", current->comm);
63
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);
70
71         return was_frozen;
72 }
73 EXPORT_SYMBOL(__refrigerator);
74
75 static void fake_signal_wake_up(struct task_struct *p)
76 {
77         unsigned long flags;
78
79         spin_lock_irqsave(&p->sighand->siglock, flags);
80         signal_wake_up(p, 0);
81         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  */
98 bool 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)) {
106                 smp_rmb();
107                 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)) {
117                 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                  */
124         } else if (sig_only) {
125                 return false;
126         } else {
127                 wake_up_state(p, TASK_INTERRUPTIBLE);
128         }
129
130         return true;
131 }
132
133 void 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 }
145
146 static int __thaw_process(struct task_struct *p)
147 {
148         if (frozen(p)) {
149                 p->flags &= ~PF_FROZEN;
150                 return 1;
151         }
152         clear_freeze_flag(p);
153         return 0;
154 }
155
156 /*
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  */
165 int 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 }
176 EXPORT_SYMBOL(thaw_process);