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> | |
8a32c441 | 12 | #include <linux/kthread.h> |
8174f150 | 13 | |
0c9af092 TH |
14 | /* protects freezing and frozen transitions */ |
15 | static DEFINE_SPINLOCK(freezer_lock); | |
8174f150 MH |
16 | |
17 | /* Refrigerator is place where frozen processes are stored :-). */ | |
8a32c441 | 18 | bool __refrigerator(bool check_kthr_stop) |
8174f150 MH |
19 | { |
20 | /* Hmm, should we be allowed to suspend when there are realtime | |
21 | processes around? */ | |
a0acae0e | 22 | bool was_frozen = false; |
8174f150 MH |
23 | long save; |
24 | ||
0c9af092 TH |
25 | spin_lock_irq(&freezer_lock); |
26 | if (!freezing(current)) { | |
27 | spin_unlock_irq(&freezer_lock); | |
a0acae0e | 28 | return was_frozen; |
8174f150 | 29 | } |
0c9af092 TH |
30 | if (!(current->flags & PF_NOFREEZE)) |
31 | current->flags |= PF_FROZEN; | |
32 | clear_freeze_flag(current); | |
33 | spin_unlock_irq(&freezer_lock); | |
34 | ||
8174f150 MH |
35 | save = current->state; |
36 | pr_debug("%s entered refrigerator\n", current->comm); | |
37 | ||
38 | spin_lock_irq(¤t->sighand->siglock); | |
39 | recalc_sigpending(); /* We sent fake signal, clean it up */ | |
40 | spin_unlock_irq(¤t->sighand->siglock); | |
41 | ||
6301cb95 TG |
42 | /* prevent accounting of that task to load */ |
43 | current->flags |= PF_FREEZING; | |
44 | ||
8174f150 MH |
45 | for (;;) { |
46 | set_current_state(TASK_UNINTERRUPTIBLE); | |
8a32c441 TH |
47 | if (!frozen(current) || |
48 | (check_kthr_stop && kthread_should_stop())) | |
8174f150 | 49 | break; |
a0acae0e | 50 | was_frozen = true; |
8174f150 MH |
51 | schedule(); |
52 | } | |
6301cb95 TG |
53 | |
54 | /* Remove the accounting blocker */ | |
55 | current->flags &= ~PF_FREEZING; | |
56 | ||
8174f150 | 57 | pr_debug("%s left refrigerator\n", current->comm); |
50fb4f7f TH |
58 | |
59 | /* | |
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. | |
63 | */ | |
64 | set_current_state(save); | |
a0acae0e TH |
65 | |
66 | return was_frozen; | |
8174f150 | 67 | } |
a0acae0e | 68 | EXPORT_SYMBOL(__refrigerator); |
8174f150 MH |
69 | |
70 | static void fake_signal_wake_up(struct task_struct *p) | |
71 | { | |
72 | unsigned long flags; | |
73 | ||
74 | spin_lock_irqsave(&p->sighand->siglock, flags); | |
d6cc7685 | 75 | signal_wake_up(p, 0); |
8174f150 MH |
76 | spin_unlock_irqrestore(&p->sighand->siglock, flags); |
77 | } | |
78 | ||
79 | /** | |
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 | |
86 | * | |
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. | |
92 | */ | |
93 | bool freeze_task(struct task_struct *p, bool sig_only) | |
94 | { | |
0c9af092 TH |
95 | unsigned long flags; |
96 | bool ret = false; | |
97 | ||
98 | spin_lock_irqsave(&freezer_lock, flags); | |
99 | ||
100 | if (sig_only && !should_send_signal(p)) | |
101 | goto out_unlock; | |
102 | ||
103 | if (frozen(p)) | |
104 | goto out_unlock; | |
105 | ||
106 | set_freeze_flag(p); | |
8174f150 MH |
107 | |
108 | if (should_send_signal(p)) { | |
8cfe400c TH |
109 | fake_signal_wake_up(p); |
110 | /* | |
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(). | |
115 | */ | |
8174f150 MH |
116 | } else { |
117 | wake_up_state(p, TASK_INTERRUPTIBLE); | |
118 | } | |
0c9af092 TH |
119 | ret = true; |
120 | out_unlock: | |
121 | spin_unlock_irqrestore(&freezer_lock, flags); | |
122 | return ret; | |
8174f150 MH |
123 | } |
124 | ||
125 | void cancel_freezing(struct task_struct *p) | |
126 | { | |
127 | unsigned long flags; | |
128 | ||
0c9af092 | 129 | spin_lock_irqsave(&freezer_lock, flags); |
8174f150 MH |
130 | if (freezing(p)) { |
131 | pr_debug(" clean up: %s\n", p->comm); | |
132 | clear_freeze_flag(p); | |
0c9af092 | 133 | spin_lock(&p->sighand->siglock); |
8174f150 | 134 | recalc_sigpending_and_wake(p); |
0c9af092 | 135 | spin_unlock(&p->sighand->siglock); |
8174f150 | 136 | } |
0c9af092 | 137 | spin_unlock_irqrestore(&freezer_lock, flags); |
8174f150 | 138 | } |
dc52ddc0 | 139 | |
00c2e63c | 140 | /* |
a5be2d0d | 141 | * Wake up a frozen task |
00c2e63c LZ |
142 | * |
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. | |
148 | */ | |
a5be2d0d | 149 | void __thaw_task(struct task_struct *p) |
dc52ddc0 | 150 | { |
0c9af092 | 151 | unsigned long flags; |
a5be2d0d | 152 | |
0c9af092 TH |
153 | spin_lock_irqsave(&freezer_lock, flags); |
154 | if (frozen(p)) { | |
a5be2d0d | 155 | p->flags &= ~PF_FROZEN; |
a5be2d0d | 156 | wake_up_process(p); |
0c9af092 TH |
157 | } else { |
158 | clear_freeze_flag(p); | |
159 | } | |
160 | spin_unlock_irqrestore(&freezer_lock, flags); | |
dc52ddc0 | 161 | } |