]>
Commit | Line | Data |
---|---|---|
8174f150 MH |
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> | |
9984de1a | 9 | #include <linux/export.h> |
8174f150 MH |
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; | |
ee940d8d | 20 | smp_wmb(); |
8174f150 MH |
21 | } |
22 | clear_freeze_flag(current); | |
23 | } | |
24 | ||
25 | /* Refrigerator is place where frozen processes are stored :-). */ | |
26 | void refrigerator(void) | |
27 | { | |
28 | /* Hmm, should we be allowed to suspend when there are realtime | |
29 | processes around? */ | |
30 | long save; | |
31 | ||
32 | task_lock(current); | |
33 | if (freezing(current)) { | |
34 | frozen_process(); | |
35 | task_unlock(current); | |
36 | } else { | |
37 | task_unlock(current); | |
38 | return; | |
39 | } | |
40 | save = current->state; | |
41 | pr_debug("%s entered refrigerator\n", current->comm); | |
42 | ||
43 | spin_lock_irq(¤t->sighand->siglock); | |
44 | recalc_sigpending(); /* We sent fake signal, clean it up */ | |
45 | spin_unlock_irq(¤t->sighand->siglock); | |
46 | ||
6301cb95 TG |
47 | /* prevent accounting of that task to load */ |
48 | current->flags |= PF_FREEZING; | |
49 | ||
8174f150 MH |
50 | for (;;) { |
51 | set_current_state(TASK_UNINTERRUPTIBLE); | |
52 | if (!frozen(current)) | |
53 | break; | |
54 | schedule(); | |
55 | } | |
6301cb95 TG |
56 | |
57 | /* Remove the accounting blocker */ | |
58 | current->flags &= ~PF_FREEZING; | |
59 | ||
8174f150 | 60 | pr_debug("%s left refrigerator\n", current->comm); |
50fb4f7f TH |
61 | |
62 | /* | |
63 | * Restore saved task state before returning. The mb'd version | |
64 | * needs to be used; otherwise, it might silently break | |
65 | * synchronization which depends on ordered task state change. | |
66 | */ | |
67 | set_current_state(save); | |
8174f150 MH |
68 | } |
69 | EXPORT_SYMBOL(refrigerator); | |
70 | ||
71 | static void fake_signal_wake_up(struct task_struct *p) | |
72 | { | |
73 | unsigned long flags; | |
74 | ||
75 | spin_lock_irqsave(&p->sighand->siglock, flags); | |
d6cc7685 | 76 | signal_wake_up(p, 0); |
8174f150 MH |
77 | spin_unlock_irqrestore(&p->sighand->siglock, flags); |
78 | } | |
79 | ||
80 | /** | |
81 | * freeze_task - send a freeze request to given task | |
82 | * @p: task to send the request to | |
83 | * @sig_only: if set, the request will only be sent if the task has the | |
84 | * PF_FREEZER_NOSIG flag unset | |
85 | * Return value: 'false', if @sig_only is set and the task has | |
86 | * PF_FREEZER_NOSIG set or the task is frozen, 'true', otherwise | |
87 | * | |
88 | * The freeze request is sent by setting the tasks's TIF_FREEZE flag and | |
89 | * either sending a fake signal to it or waking it up, depending on whether | |
90 | * or not it has PF_FREEZER_NOSIG set. If @sig_only is set and the task | |
91 | * has PF_FREEZER_NOSIG set (ie. it is a typical kernel thread), its | |
92 | * TIF_FREEZE flag will not be set. | |
93 | */ | |
94 | bool freeze_task(struct task_struct *p, bool sig_only) | |
95 | { | |
96 | /* | |
97 | * We first check if the task is freezing and next if it has already | |
98 | * been frozen to avoid the race with frozen_process() which first marks | |
99 | * the task as frozen and next clears its TIF_FREEZE. | |
100 | */ | |
101 | if (!freezing(p)) { | |
ee940d8d | 102 | smp_rmb(); |
8174f150 MH |
103 | if (frozen(p)) |
104 | return false; | |
105 | ||
106 | if (!sig_only || should_send_signal(p)) | |
107 | set_freeze_flag(p); | |
108 | else | |
109 | return false; | |
110 | } | |
111 | ||
112 | if (should_send_signal(p)) { | |
8cfe400c TH |
113 | fake_signal_wake_up(p); |
114 | /* | |
115 | * fake_signal_wake_up() goes through p's scheduler | |
116 | * lock and guarantees that TASK_STOPPED/TRACED -> | |
117 | * TASK_RUNNING transition can't race with task state | |
118 | * testing in try_to_freeze_tasks(). | |
119 | */ | |
8174f150 MH |
120 | } else if (sig_only) { |
121 | return false; | |
122 | } else { | |
123 | wake_up_state(p, TASK_INTERRUPTIBLE); | |
124 | } | |
125 | ||
126 | return true; | |
127 | } | |
128 | ||
129 | void cancel_freezing(struct task_struct *p) | |
130 | { | |
131 | unsigned long flags; | |
132 | ||
133 | if (freezing(p)) { | |
134 | pr_debug(" clean up: %s\n", p->comm); | |
135 | clear_freeze_flag(p); | |
136 | spin_lock_irqsave(&p->sighand->siglock, flags); | |
137 | recalc_sigpending_and_wake(p); | |
138 | spin_unlock_irqrestore(&p->sighand->siglock, flags); | |
139 | } | |
140 | } | |
dc52ddc0 | 141 | |
00c2e63c | 142 | static int __thaw_process(struct task_struct *p) |
dc52ddc0 MH |
143 | { |
144 | if (frozen(p)) { | |
145 | p->flags &= ~PF_FROZEN; | |
146 | return 1; | |
147 | } | |
148 | clear_freeze_flag(p); | |
149 | return 0; | |
150 | } | |
151 | ||
00c2e63c LZ |
152 | /* |
153 | * Wake up a frozen process | |
154 | * | |
155 | * task_lock() is needed to prevent the race with refrigerator() which may | |
156 | * occur if the freezing of tasks fails. Namely, without the lock, if the | |
157 | * freezing of tasks failed, thaw_tasks() might have run before a task in | |
158 | * refrigerator() could call frozen_process(), in which case the task would be | |
159 | * frozen and no one would thaw it. | |
160 | */ | |
dc52ddc0 MH |
161 | int thaw_process(struct task_struct *p) |
162 | { | |
163 | task_lock(p); | |
164 | if (__thaw_process(p) == 1) { | |
165 | task_unlock(p); | |
166 | wake_up_process(p); | |
167 | return 1; | |
168 | } | |
169 | task_unlock(p); | |
170 | return 0; | |
171 | } | |
172 | EXPORT_SYMBOL(thaw_process); |