freezer: test freezable conditions while holding freezer_lock
[linux-2.6.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 #include <linux/kthread.h>
13
14 /* protects freezing and frozen transitions */
15 static DEFINE_SPINLOCK(freezer_lock);
16
17 /* Refrigerator is place where frozen processes are stored :-). */
18 bool __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         /*
26          * Enter FROZEN.  If NOFREEZE, schedule immediate thawing by
27          * clearing freezing.
28          */
29         spin_lock_irq(&freezer_lock);
30 repeat:
31         if (!freezing(current)) {
32                 spin_unlock_irq(&freezer_lock);
33                 return was_frozen;
34         }
35         if (current->flags & PF_NOFREEZE)
36                 clear_freeze_flag(current);
37         current->flags |= PF_FROZEN;
38         spin_unlock_irq(&freezer_lock);
39
40         save = current->state;
41         pr_debug("%s entered refrigerator\n", current->comm);
42
43         spin_lock_irq(&current->sighand->siglock);
44         recalc_sigpending(); /* We sent fake signal, clean it up */
45         spin_unlock_irq(&current->sighand->siglock);
46
47         /* prevent accounting of that task to load */
48         current->flags |= PF_FREEZING;
49
50         for (;;) {
51                 set_current_state(TASK_UNINTERRUPTIBLE);
52                 if (!freezing(current) ||
53                     (check_kthr_stop && kthread_should_stop()))
54                         break;
55                 was_frozen = true;
56                 schedule();
57         }
58
59         /* Remove the accounting blocker */
60         current->flags &= ~PF_FREEZING;
61
62         /* leave FROZEN */
63         spin_lock_irq(&freezer_lock);
64         if (freezing(current))
65                 goto repeat;
66         current->flags &= ~PF_FROZEN;
67         spin_unlock_irq(&freezer_lock);
68
69         pr_debug("%s left refrigerator\n", current->comm);
70
71         /*
72          * Restore saved task state before returning.  The mb'd version
73          * needs to be used; otherwise, it might silently break
74          * synchronization which depends on ordered task state change.
75          */
76         set_current_state(save);
77
78         return was_frozen;
79 }
80 EXPORT_SYMBOL(__refrigerator);
81
82 static void fake_signal_wake_up(struct task_struct *p)
83 {
84         unsigned long flags;
85
86         spin_lock_irqsave(&p->sighand->siglock, flags);
87         signal_wake_up(p, 0);
88         spin_unlock_irqrestore(&p->sighand->siglock, flags);
89 }
90
91 /**
92  *      freeze_task - send a freeze request to given task
93  *      @p: task to send the request to
94  *      @sig_only: if set, the request will only be sent if the task has the
95  *              PF_FREEZER_NOSIG flag unset
96  *      Return value: 'false', if @sig_only is set and the task has
97  *              PF_FREEZER_NOSIG set or the task is frozen, 'true', otherwise
98  *
99  *      The freeze request is sent by setting the tasks's TIF_FREEZE flag and
100  *      either sending a fake signal to it or waking it up, depending on whether
101  *      or not it has PF_FREEZER_NOSIG set.  If @sig_only is set and the task
102  *      has PF_FREEZER_NOSIG set (ie. it is a typical kernel thread), its
103  *      TIF_FREEZE flag will not be set.
104  */
105 bool freeze_task(struct task_struct *p, bool sig_only)
106 {
107         unsigned long flags;
108         bool ret = false;
109
110         spin_lock_irqsave(&freezer_lock, flags);
111
112         if ((p->flags & PF_NOFREEZE) ||
113             (sig_only && !should_send_signal(p)))
114                 goto out_unlock;
115
116         if (frozen(p))
117                 goto out_unlock;
118
119         set_freeze_flag(p);
120
121         if (should_send_signal(p)) {
122                 fake_signal_wake_up(p);
123                 /*
124                  * fake_signal_wake_up() goes through p's scheduler
125                  * lock and guarantees that TASK_STOPPED/TRACED ->
126                  * TASK_RUNNING transition can't race with task state
127                  * testing in try_to_freeze_tasks().
128                  */
129         } else {
130                 wake_up_state(p, TASK_INTERRUPTIBLE);
131         }
132         ret = true;
133 out_unlock:
134         spin_unlock_irqrestore(&freezer_lock, flags);
135         return ret;
136 }
137
138 void cancel_freezing(struct task_struct *p)
139 {
140         unsigned long flags;
141
142         spin_lock_irqsave(&freezer_lock, flags);
143         if (freezing(p)) {
144                 pr_debug("  clean up: %s\n", p->comm);
145                 clear_freeze_flag(p);
146                 spin_lock(&p->sighand->siglock);
147                 recalc_sigpending_and_wake(p);
148                 spin_unlock(&p->sighand->siglock);
149         }
150         spin_unlock_irqrestore(&freezer_lock, flags);
151 }
152
153 void __thaw_task(struct task_struct *p)
154 {
155         unsigned long flags;
156
157         /*
158          * Clear freezing and kick @p if FROZEN.  Clearing is guaranteed to
159          * be visible to @p as waking up implies wmb.  Waking up inside
160          * freezer_lock also prevents wakeups from leaking outside
161          * refrigerator.
162          */
163         spin_lock_irqsave(&freezer_lock, flags);
164         clear_freeze_flag(p);
165         if (frozen(p))
166                 wake_up_process(p);
167         spin_unlock_irqrestore(&freezer_lock, flags);
168 }