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> |
Tejun Heo | 8a32c44 | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 12 | #include <linux/kthread.h> |
Matt Helsley | 8174f15 | 2008-10-18 20:27:19 -0700 | [diff] [blame] | 13 | |
Tejun Heo | 0c9af09 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 14 | /* protects freezing and frozen transitions */ |
| 15 | static DEFINE_SPINLOCK(freezer_lock); |
Matt Helsley | 8174f15 | 2008-10-18 20:27:19 -0700 | [diff] [blame] | 16 | |
| 17 | /* Refrigerator is place where frozen processes are stored :-). */ |
Tejun Heo | 8a32c44 | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 18 | bool __refrigerator(bool check_kthr_stop) |
Matt Helsley | 8174f15 | 2008-10-18 20:27:19 -0700 | [diff] [blame] | 19 | { |
| 20 | /* Hmm, should we be allowed to suspend when there are realtime |
| 21 | processes around? */ |
Tejun Heo | a0acae0 | 2011-11-21 12:32:22 -0800 | [diff] [blame] | 22 | bool was_frozen = false; |
Matt Helsley | 8174f15 | 2008-10-18 20:27:19 -0700 | [diff] [blame] | 23 | long save; |
| 24 | |
Tejun Heo | 6907483 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 25 | /* |
| 26 | * Enter FROZEN. If NOFREEZE, schedule immediate thawing by |
| 27 | * clearing freezing. |
| 28 | */ |
Tejun Heo | 0c9af09 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 29 | spin_lock_irq(&freezer_lock); |
Tejun Heo | 6907483 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 30 | repeat: |
Tejun Heo | 0c9af09 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 31 | if (!freezing(current)) { |
| 32 | spin_unlock_irq(&freezer_lock); |
Tejun Heo | a0acae0 | 2011-11-21 12:32:22 -0800 | [diff] [blame] | 33 | return was_frozen; |
Matt Helsley | 8174f15 | 2008-10-18 20:27:19 -0700 | [diff] [blame] | 34 | } |
Tejun Heo | 6907483 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 35 | if (current->flags & PF_NOFREEZE) |
| 36 | clear_freeze_flag(current); |
| 37 | current->flags |= PF_FROZEN; |
Tejun Heo | 0c9af09 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 38 | spin_unlock_irq(&freezer_lock); |
| 39 | |
Matt Helsley | 8174f15 | 2008-10-18 20:27:19 -0700 | [diff] [blame] | 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); |
Tejun Heo | 6907483 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 49 | if (!freezing(current) || |
Tejun Heo | 8a32c44 | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 50 | (check_kthr_stop && kthread_should_stop())) |
Matt Helsley | 8174f15 | 2008-10-18 20:27:19 -0700 | [diff] [blame] | 51 | break; |
Tejun Heo | a0acae0 | 2011-11-21 12:32:22 -0800 | [diff] [blame] | 52 | was_frozen = true; |
Matt Helsley | 8174f15 | 2008-10-18 20:27:19 -0700 | [diff] [blame] | 53 | schedule(); |
| 54 | } |
Thomas Gleixner | 6301cb9 | 2009-07-17 14:15:47 +0200 | [diff] [blame] | 55 | |
Tejun Heo | 6907483 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 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 | |
Matt Helsley | 8174f15 | 2008-10-18 20:27:19 -0700 | [diff] [blame] | 63 | pr_debug("%s left refrigerator\n", current->comm); |
Tejun Heo | 50fb4f7f | 2011-11-21 12:32:22 -0800 | [diff] [blame] | 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); |
Tejun Heo | a0acae0 | 2011-11-21 12:32:22 -0800 | [diff] [blame] | 71 | |
| 72 | return was_frozen; |
Matt Helsley | 8174f15 | 2008-10-18 20:27:19 -0700 | [diff] [blame] | 73 | } |
Tejun Heo | a0acae0 | 2011-11-21 12:32:22 -0800 | [diff] [blame] | 74 | EXPORT_SYMBOL(__refrigerator); |
Matt Helsley | 8174f15 | 2008-10-18 20:27:19 -0700 | [diff] [blame] | 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); |
Tejun Heo | d6cc768 | 2011-11-04 01:04:52 +0100 | [diff] [blame] | 81 | signal_wake_up(p, 0); |
Matt Helsley | 8174f15 | 2008-10-18 20:27:19 -0700 | [diff] [blame] | 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 | { |
Tejun Heo | 0c9af09 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 101 | unsigned long flags; |
| 102 | bool ret = false; |
Matt Helsley | 8174f15 | 2008-10-18 20:27:19 -0700 | [diff] [blame] | 103 | |
Tejun Heo | 0c9af09 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 104 | spin_lock_irqsave(&freezer_lock, flags); |
| 105 | |
Tejun Heo | 85f1d47 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 106 | if ((p->flags & PF_NOFREEZE) || |
| 107 | (sig_only && !should_send_signal(p))) |
Tejun Heo | 0c9af09 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 108 | goto out_unlock; |
| 109 | |
| 110 | if (frozen(p)) |
| 111 | goto out_unlock; |
| 112 | |
| 113 | set_freeze_flag(p); |
Matt Helsley | 8174f15 | 2008-10-18 20:27:19 -0700 | [diff] [blame] | 114 | |
| 115 | if (should_send_signal(p)) { |
Tejun Heo | 8cfe400 | 2010-11-26 23:07:27 +0100 | [diff] [blame] | 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 | */ |
Matt Helsley | 8174f15 | 2008-10-18 20:27:19 -0700 | [diff] [blame] | 123 | } else { |
| 124 | wake_up_state(p, TASK_INTERRUPTIBLE); |
| 125 | } |
Tejun Heo | 0c9af09 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 126 | ret = true; |
| 127 | out_unlock: |
| 128 | spin_unlock_irqrestore(&freezer_lock, flags); |
| 129 | return ret; |
Matt Helsley | 8174f15 | 2008-10-18 20:27:19 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Tejun Heo | a5be2d0 | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 132 | void __thaw_task(struct task_struct *p) |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 133 | { |
Tejun Heo | 0c9af09 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 134 | unsigned long flags; |
Tejun Heo | a5be2d0 | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 135 | |
Tejun Heo | 6907483 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 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. |
Tejun Heo | 03afed8 | 2011-11-21 12:32:24 -0800 | [diff] [blame^] | 141 | * |
| 142 | * If !FROZEN, @p hasn't reached refrigerator, recalc sigpending to |
| 143 | * avoid leaving dangling TIF_SIGPENDING behind. |
Tejun Heo | 6907483 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 144 | */ |
Tejun Heo | 0c9af09 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 145 | spin_lock_irqsave(&freezer_lock, flags); |
Tejun Heo | 6907483 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 146 | clear_freeze_flag(p); |
Tejun Heo | 03afed8 | 2011-11-21 12:32:24 -0800 | [diff] [blame^] | 147 | if (frozen(p)) { |
Tejun Heo | a5be2d0 | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 148 | wake_up_process(p); |
Tejun Heo | 03afed8 | 2011-11-21 12:32:24 -0800 | [diff] [blame^] | 149 | } else { |
| 150 | spin_lock(&p->sighand->siglock); |
| 151 | recalc_sigpending_and_wake(p); |
| 152 | spin_unlock(&p->sighand->siglock); |
| 153 | } |
Tejun Heo | 0c9af09 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 154 | spin_unlock_irqrestore(&freezer_lock, flags); |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 155 | } |