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