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 | ||
6907483b TH |
25 | /* |
26 | * Enter FROZEN. If NOFREEZE, schedule immediate thawing by | |
27 | * clearing freezing. | |
28 | */ | |
0c9af092 | 29 | spin_lock_irq(&freezer_lock); |
6907483b | 30 | repeat: |
0c9af092 TH |
31 | if (!freezing(current)) { |
32 | spin_unlock_irq(&freezer_lock); | |
a0acae0e | 33 | return was_frozen; |
8174f150 | 34 | } |
6907483b TH |
35 | if (current->flags & PF_NOFREEZE) |
36 | clear_freeze_flag(current); | |
37 | current->flags |= PF_FROZEN; | |
0c9af092 TH |
38 | spin_unlock_irq(&freezer_lock); |
39 | ||
8174f150 MH |
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 | ||
47 | for (;;) { | |
48 | set_current_state(TASK_UNINTERRUPTIBLE); | |
6907483b | 49 | if (!freezing(current) || |
8a32c441 | 50 | (check_kthr_stop && kthread_should_stop())) |
8174f150 | 51 | break; |
a0acae0e | 52 | was_frozen = true; |
8174f150 MH |
53 | schedule(); |
54 | } | |
6301cb95 | 55 | |
6907483b TH |
56 | /* leave FROZEN */ |
57 | spin_lock_irq(&freezer_lock); | |
58 | if (freezing(current)) | |
59 | goto repeat; | |
60 | current->flags &= ~PF_FROZEN; | |
61 | spin_unlock_irq(&freezer_lock); | |
62 | ||
8174f150 | 63 | pr_debug("%s left refrigerator\n", current->comm); |
50fb4f7f TH |
64 | |
65 | /* | |
66 | * Restore saved task state before returning. The mb'd version | |
67 | * needs to be used; otherwise, it might silently break | |
68 | * synchronization which depends on ordered task state change. | |
69 | */ | |
70 | set_current_state(save); | |
a0acae0e TH |
71 | |
72 | return was_frozen; | |
8174f150 | 73 | } |
a0acae0e | 74 | EXPORT_SYMBOL(__refrigerator); |
8174f150 MH |
75 | |
76 | static void fake_signal_wake_up(struct task_struct *p) | |
77 | { | |
78 | unsigned long flags; | |
79 | ||
80 | spin_lock_irqsave(&p->sighand->siglock, flags); | |
d6cc7685 | 81 | signal_wake_up(p, 0); |
8174f150 MH |
82 | spin_unlock_irqrestore(&p->sighand->siglock, flags); |
83 | } | |
84 | ||
85 | /** | |
86 | * freeze_task - send a freeze request to given task | |
87 | * @p: task to send the request to | |
88 | * @sig_only: if set, the request will only be sent if the task has the | |
89 | * PF_FREEZER_NOSIG flag unset | |
90 | * Return value: 'false', if @sig_only is set and the task has | |
91 | * PF_FREEZER_NOSIG set or the task is frozen, 'true', otherwise | |
92 | * | |
93 | * The freeze request is sent by setting the tasks's TIF_FREEZE flag and | |
94 | * either sending a fake signal to it or waking it up, depending on whether | |
95 | * or not it has PF_FREEZER_NOSIG set. If @sig_only is set and the task | |
96 | * has PF_FREEZER_NOSIG set (ie. it is a typical kernel thread), its | |
97 | * TIF_FREEZE flag will not be set. | |
98 | */ | |
99 | bool freeze_task(struct task_struct *p, bool sig_only) | |
100 | { | |
0c9af092 TH |
101 | unsigned long flags; |
102 | bool ret = false; | |
103 | ||
104 | spin_lock_irqsave(&freezer_lock, flags); | |
105 | ||
85f1d476 TH |
106 | if ((p->flags & PF_NOFREEZE) || |
107 | (sig_only && !should_send_signal(p))) | |
0c9af092 TH |
108 | goto out_unlock; |
109 | ||
110 | if (frozen(p)) | |
111 | goto out_unlock; | |
112 | ||
113 | set_freeze_flag(p); | |
8174f150 MH |
114 | |
115 | if (should_send_signal(p)) { | |
8cfe400c TH |
116 | fake_signal_wake_up(p); |
117 | /* | |
118 | * fake_signal_wake_up() goes through p's scheduler | |
119 | * lock and guarantees that TASK_STOPPED/TRACED -> | |
120 | * TASK_RUNNING transition can't race with task state | |
121 | * testing in try_to_freeze_tasks(). | |
122 | */ | |
8174f150 MH |
123 | } else { |
124 | wake_up_state(p, TASK_INTERRUPTIBLE); | |
125 | } | |
0c9af092 TH |
126 | ret = true; |
127 | out_unlock: | |
128 | spin_unlock_irqrestore(&freezer_lock, flags); | |
129 | return ret; | |
8174f150 MH |
130 | } |
131 | ||
a5be2d0d | 132 | void __thaw_task(struct task_struct *p) |
dc52ddc0 | 133 | { |
0c9af092 | 134 | unsigned long flags; |
a5be2d0d | 135 | |
6907483b TH |
136 | /* |
137 | * Clear freezing and kick @p if FROZEN. Clearing is guaranteed to | |
138 | * be visible to @p as waking up implies wmb. Waking up inside | |
139 | * freezer_lock also prevents wakeups from leaking outside | |
140 | * refrigerator. | |
03afed8b TH |
141 | * |
142 | * If !FROZEN, @p hasn't reached refrigerator, recalc sigpending to | |
143 | * avoid leaving dangling TIF_SIGPENDING behind. | |
6907483b | 144 | */ |
0c9af092 | 145 | spin_lock_irqsave(&freezer_lock, flags); |
6907483b | 146 | clear_freeze_flag(p); |
03afed8b | 147 | if (frozen(p)) { |
a5be2d0d | 148 | wake_up_process(p); |
03afed8b TH |
149 | } else { |
150 | spin_lock(&p->sighand->siglock); | |
151 | recalc_sigpending_and_wake(p); | |
152 | spin_unlock(&p->sighand->siglock); | |
153 | } | |
0c9af092 | 154 | spin_unlock_irqrestore(&freezer_lock, flags); |
dc52ddc0 | 155 | } |