freezer: use dedicated lock instead of task_lock() + memory barrier
[linux-2.6-block.git] / kernel / freezer.c
... / ...
CommitLineData
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#include <linux/kthread.h>
13
14/* protects freezing and frozen transitions */
15static DEFINE_SPINLOCK(freezer_lock);
16
17/* Refrigerator is place where frozen processes are stored :-). */
18bool __refrigerator(bool check_kthr_stop)
19{
20 /* Hmm, should we be allowed to suspend when there are realtime
21 processes around? */
22 bool was_frozen = false;
23 long save;
24
25 spin_lock_irq(&freezer_lock);
26 if (!freezing(current)) {
27 spin_unlock_irq(&freezer_lock);
28 return was_frozen;
29 }
30 if (!(current->flags & PF_NOFREEZE))
31 current->flags |= PF_FROZEN;
32 clear_freeze_flag(current);
33 spin_unlock_irq(&freezer_lock);
34
35 save = current->state;
36 pr_debug("%s entered refrigerator\n", current->comm);
37
38 spin_lock_irq(&current->sighand->siglock);
39 recalc_sigpending(); /* We sent fake signal, clean it up */
40 spin_unlock_irq(&current->sighand->siglock);
41
42 /* prevent accounting of that task to load */
43 current->flags |= PF_FREEZING;
44
45 for (;;) {
46 set_current_state(TASK_UNINTERRUPTIBLE);
47 if (!frozen(current) ||
48 (check_kthr_stop && kthread_should_stop()))
49 break;
50 was_frozen = true;
51 schedule();
52 }
53
54 /* Remove the accounting blocker */
55 current->flags &= ~PF_FREEZING;
56
57 pr_debug("%s left refrigerator\n", current->comm);
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);
65
66 return was_frozen;
67}
68EXPORT_SYMBOL(__refrigerator);
69
70static void fake_signal_wake_up(struct task_struct *p)
71{
72 unsigned long flags;
73
74 spin_lock_irqsave(&p->sighand->siglock, flags);
75 signal_wake_up(p, 0);
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 */
93bool freeze_task(struct task_struct *p, bool sig_only)
94{
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);
107
108 if (should_send_signal(p)) {
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 */
116 } else {
117 wake_up_state(p, TASK_INTERRUPTIBLE);
118 }
119 ret = true;
120out_unlock:
121 spin_unlock_irqrestore(&freezer_lock, flags);
122 return ret;
123}
124
125void cancel_freezing(struct task_struct *p)
126{
127 unsigned long flags;
128
129 spin_lock_irqsave(&freezer_lock, flags);
130 if (freezing(p)) {
131 pr_debug(" clean up: %s\n", p->comm);
132 clear_freeze_flag(p);
133 spin_lock(&p->sighand->siglock);
134 recalc_sigpending_and_wake(p);
135 spin_unlock(&p->sighand->siglock);
136 }
137 spin_unlock_irqrestore(&freezer_lock, flags);
138}
139
140/*
141 * Wake up a frozen task
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 */
149void __thaw_task(struct task_struct *p)
150{
151 unsigned long flags;
152
153 spin_lock_irqsave(&freezer_lock, flags);
154 if (frozen(p)) {
155 p->flags &= ~PF_FROZEN;
156 wake_up_process(p);
157 } else {
158 clear_freeze_flag(p);
159 }
160 spin_unlock_irqrestore(&freezer_lock, flags);
161}