blob: 11e32d419dec625a760eeba149546265eb51013b [file] [log] [blame]
Matt Helsley8174f152008-10-18 20:27:19 -07001/*
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 Gortmaker9984de12011-05-23 14:51:41 -04009#include <linux/export.h>
Matt Helsley8174f152008-10-18 20:27:19 -070010#include <linux/syscalls.h>
11#include <linux/freezer.h>
Tejun Heo8a32c442011-11-21 12:32:23 -080012#include <linux/kthread.h>
Matt Helsley8174f152008-10-18 20:27:19 -070013
Tejun Heo0c9af092011-11-21 12:32:24 -080014/* protects freezing and frozen transitions */
15static DEFINE_SPINLOCK(freezer_lock);
Matt Helsley8174f152008-10-18 20:27:19 -070016
17/* Refrigerator is place where frozen processes are stored :-). */
Tejun Heo8a32c442011-11-21 12:32:23 -080018bool __refrigerator(bool check_kthr_stop)
Matt Helsley8174f152008-10-18 20:27:19 -070019{
20 /* Hmm, should we be allowed to suspend when there are realtime
21 processes around? */
Tejun Heoa0acae02011-11-21 12:32:22 -080022 bool was_frozen = false;
Matt Helsley8174f152008-10-18 20:27:19 -070023 long save;
24
Tejun Heo69074832011-11-21 12:32:24 -080025 /*
26 * Enter FROZEN. If NOFREEZE, schedule immediate thawing by
27 * clearing freezing.
28 */
Tejun Heo0c9af092011-11-21 12:32:24 -080029 spin_lock_irq(&freezer_lock);
Tejun Heo69074832011-11-21 12:32:24 -080030repeat:
Tejun Heo0c9af092011-11-21 12:32:24 -080031 if (!freezing(current)) {
32 spin_unlock_irq(&freezer_lock);
Tejun Heoa0acae02011-11-21 12:32:22 -080033 return was_frozen;
Matt Helsley8174f152008-10-18 20:27:19 -070034 }
Tejun Heo69074832011-11-21 12:32:24 -080035 if (current->flags & PF_NOFREEZE)
36 clear_freeze_flag(current);
37 current->flags |= PF_FROZEN;
Tejun Heo0c9af092011-11-21 12:32:24 -080038 spin_unlock_irq(&freezer_lock);
39
Matt Helsley8174f152008-10-18 20:27:19 -070040 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 for (;;) {
48 set_current_state(TASK_UNINTERRUPTIBLE);
Tejun Heo69074832011-11-21 12:32:24 -080049 if (!freezing(current) ||
Tejun Heo8a32c442011-11-21 12:32:23 -080050 (check_kthr_stop && kthread_should_stop()))
Matt Helsley8174f152008-10-18 20:27:19 -070051 break;
Tejun Heoa0acae02011-11-21 12:32:22 -080052 was_frozen = true;
Matt Helsley8174f152008-10-18 20:27:19 -070053 schedule();
54 }
Thomas Gleixner6301cb92009-07-17 14:15:47 +020055
Tejun Heo69074832011-11-21 12:32:24 -080056 /* 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 Helsley8174f152008-10-18 20:27:19 -070063 pr_debug("%s left refrigerator\n", current->comm);
Tejun Heo50fb4f7f2011-11-21 12:32:22 -080064
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 Heoa0acae02011-11-21 12:32:22 -080071
72 return was_frozen;
Matt Helsley8174f152008-10-18 20:27:19 -070073}
Tejun Heoa0acae02011-11-21 12:32:22 -080074EXPORT_SYMBOL(__refrigerator);
Matt Helsley8174f152008-10-18 20:27:19 -070075
76static void fake_signal_wake_up(struct task_struct *p)
77{
78 unsigned long flags;
79
80 spin_lock_irqsave(&p->sighand->siglock, flags);
Tejun Heod6cc7682011-11-04 01:04:52 +010081 signal_wake_up(p, 0);
Matt Helsley8174f152008-10-18 20:27:19 -070082 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 */
99bool freeze_task(struct task_struct *p, bool sig_only)
100{
Tejun Heo0c9af092011-11-21 12:32:24 -0800101 unsigned long flags;
102 bool ret = false;
Matt Helsley8174f152008-10-18 20:27:19 -0700103
Tejun Heo0c9af092011-11-21 12:32:24 -0800104 spin_lock_irqsave(&freezer_lock, flags);
105
Tejun Heo85f1d472011-11-21 12:32:24 -0800106 if ((p->flags & PF_NOFREEZE) ||
107 (sig_only && !should_send_signal(p)))
Tejun Heo0c9af092011-11-21 12:32:24 -0800108 goto out_unlock;
109
110 if (frozen(p))
111 goto out_unlock;
112
113 set_freeze_flag(p);
Matt Helsley8174f152008-10-18 20:27:19 -0700114
115 if (should_send_signal(p)) {
Tejun Heo8cfe4002010-11-26 23:07:27 +0100116 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 Helsley8174f152008-10-18 20:27:19 -0700123 } else {
124 wake_up_state(p, TASK_INTERRUPTIBLE);
125 }
Tejun Heo0c9af092011-11-21 12:32:24 -0800126 ret = true;
127out_unlock:
128 spin_unlock_irqrestore(&freezer_lock, flags);
129 return ret;
Matt Helsley8174f152008-10-18 20:27:19 -0700130}
131
Tejun Heoa5be2d02011-11-21 12:32:23 -0800132void __thaw_task(struct task_struct *p)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700133{
Tejun Heo0c9af092011-11-21 12:32:24 -0800134 unsigned long flags;
Tejun Heoa5be2d02011-11-21 12:32:23 -0800135
Tejun Heo69074832011-11-21 12:32:24 -0800136 /*
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 Heo03afed82011-11-21 12:32:24 -0800141 *
142 * If !FROZEN, @p hasn't reached refrigerator, recalc sigpending to
143 * avoid leaving dangling TIF_SIGPENDING behind.
Tejun Heo69074832011-11-21 12:32:24 -0800144 */
Tejun Heo0c9af092011-11-21 12:32:24 -0800145 spin_lock_irqsave(&freezer_lock, flags);
Tejun Heo69074832011-11-21 12:32:24 -0800146 clear_freeze_flag(p);
Tejun Heo03afed82011-11-21 12:32:24 -0800147 if (frozen(p)) {
Tejun Heoa5be2d02011-11-21 12:32:23 -0800148 wake_up_process(p);
Tejun Heo03afed82011-11-21 12:32:24 -0800149 } else {
150 spin_lock(&p->sighand->siglock);
151 recalc_sigpending_and_wake(p);
152 spin_unlock(&p->sighand->siglock);
153 }
Tejun Heo0c9af092011-11-21 12:32:24 -0800154 spin_unlock_irqrestore(&freezer_lock, flags);
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700155}