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