blob: 4130e48649bb166d7b4a192bb8932492a17b8a1f [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 Heo0c9af092011-11-21 12:32:24 -080025 spin_lock_irq(&freezer_lock);
26 if (!freezing(current)) {
27 spin_unlock_irq(&freezer_lock);
Tejun Heoa0acae02011-11-21 12:32:22 -080028 return was_frozen;
Matt Helsley8174f152008-10-18 20:27:19 -070029 }
Tejun Heo0c9af092011-11-21 12:32:24 -080030 if (!(current->flags & PF_NOFREEZE))
31 current->flags |= PF_FROZEN;
32 clear_freeze_flag(current);
33 spin_unlock_irq(&freezer_lock);
34
Matt Helsley8174f152008-10-18 20:27:19 -070035 save = current->state;
36 pr_debug("%s entered refrigerator\n", current->comm);
37
38 spin_lock_irq(&current->sighand->siglock);
39 recalc_sigpending(); /* We sent fake signal, clean it up */
40 spin_unlock_irq(&current->sighand->siglock);
41
Thomas Gleixner6301cb92009-07-17 14:15:47 +020042 /* prevent accounting of that task to load */
43 current->flags |= PF_FREEZING;
44
Matt Helsley8174f152008-10-18 20:27:19 -070045 for (;;) {
46 set_current_state(TASK_UNINTERRUPTIBLE);
Tejun Heo8a32c442011-11-21 12:32:23 -080047 if (!frozen(current) ||
48 (check_kthr_stop && kthread_should_stop()))
Matt Helsley8174f152008-10-18 20:27:19 -070049 break;
Tejun Heoa0acae02011-11-21 12:32:22 -080050 was_frozen = true;
Matt Helsley8174f152008-10-18 20:27:19 -070051 schedule();
52 }
Thomas Gleixner6301cb92009-07-17 14:15:47 +020053
54 /* Remove the accounting blocker */
55 current->flags &= ~PF_FREEZING;
56
Matt Helsley8174f152008-10-18 20:27:19 -070057 pr_debug("%s left refrigerator\n", current->comm);
Tejun Heo50fb4f7f2011-11-21 12:32:22 -080058
59 /*
60 * Restore saved task state before returning. The mb'd version
61 * needs to be used; otherwise, it might silently break
62 * synchronization which depends on ordered task state change.
63 */
64 set_current_state(save);
Tejun Heoa0acae02011-11-21 12:32:22 -080065
66 return was_frozen;
Matt Helsley8174f152008-10-18 20:27:19 -070067}
Tejun Heoa0acae02011-11-21 12:32:22 -080068EXPORT_SYMBOL(__refrigerator);
Matt Helsley8174f152008-10-18 20:27:19 -070069
70static void fake_signal_wake_up(struct task_struct *p)
71{
72 unsigned long flags;
73
74 spin_lock_irqsave(&p->sighand->siglock, flags);
Tejun Heod6cc7682011-11-04 01:04:52 +010075 signal_wake_up(p, 0);
Matt Helsley8174f152008-10-18 20:27:19 -070076 spin_unlock_irqrestore(&p->sighand->siglock, flags);
77}
78
79/**
80 * freeze_task - send a freeze request to given task
81 * @p: task to send the request to
82 * @sig_only: if set, the request will only be sent if the task has the
83 * PF_FREEZER_NOSIG flag unset
84 * Return value: 'false', if @sig_only is set and the task has
85 * PF_FREEZER_NOSIG set or the task is frozen, 'true', otherwise
86 *
87 * The freeze request is sent by setting the tasks's TIF_FREEZE flag and
88 * either sending a fake signal to it or waking it up, depending on whether
89 * or not it has PF_FREEZER_NOSIG set. If @sig_only is set and the task
90 * has PF_FREEZER_NOSIG set (ie. it is a typical kernel thread), its
91 * TIF_FREEZE flag will not be set.
92 */
93bool freeze_task(struct task_struct *p, bool sig_only)
94{
Tejun Heo0c9af092011-11-21 12:32:24 -080095 unsigned long flags;
96 bool ret = false;
Matt Helsley8174f152008-10-18 20:27:19 -070097
Tejun Heo0c9af092011-11-21 12:32:24 -080098 spin_lock_irqsave(&freezer_lock, flags);
99
100 if (sig_only && !should_send_signal(p))
101 goto out_unlock;
102
103 if (frozen(p))
104 goto out_unlock;
105
106 set_freeze_flag(p);
Matt Helsley8174f152008-10-18 20:27:19 -0700107
108 if (should_send_signal(p)) {
Tejun Heo8cfe400c2010-11-26 23:07:27 +0100109 fake_signal_wake_up(p);
110 /*
111 * fake_signal_wake_up() goes through p's scheduler
112 * lock and guarantees that TASK_STOPPED/TRACED ->
113 * TASK_RUNNING transition can't race with task state
114 * testing in try_to_freeze_tasks().
115 */
Matt Helsley8174f152008-10-18 20:27:19 -0700116 } else {
117 wake_up_state(p, TASK_INTERRUPTIBLE);
118 }
Tejun Heo0c9af092011-11-21 12:32:24 -0800119 ret = true;
120out_unlock:
121 spin_unlock_irqrestore(&freezer_lock, flags);
122 return ret;
Matt Helsley8174f152008-10-18 20:27:19 -0700123}
124
125void cancel_freezing(struct task_struct *p)
126{
127 unsigned long flags;
128
Tejun Heo0c9af092011-11-21 12:32:24 -0800129 spin_lock_irqsave(&freezer_lock, flags);
Matt Helsley8174f152008-10-18 20:27:19 -0700130 if (freezing(p)) {
131 pr_debug(" clean up: %s\n", p->comm);
132 clear_freeze_flag(p);
Tejun Heo0c9af092011-11-21 12:32:24 -0800133 spin_lock(&p->sighand->siglock);
Matt Helsley8174f152008-10-18 20:27:19 -0700134 recalc_sigpending_and_wake(p);
Tejun Heo0c9af092011-11-21 12:32:24 -0800135 spin_unlock(&p->sighand->siglock);
Matt Helsley8174f152008-10-18 20:27:19 -0700136 }
Tejun Heo0c9af092011-11-21 12:32:24 -0800137 spin_unlock_irqrestore(&freezer_lock, flags);
Matt Helsley8174f152008-10-18 20:27:19 -0700138}
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700139
Li Zefan00c2e632008-10-29 14:00:53 -0700140/*
Tejun Heoa5be2d02011-11-21 12:32:23 -0800141 * Wake up a frozen task
Li Zefan00c2e632008-10-29 14:00:53 -0700142 *
143 * task_lock() is needed to prevent the race with refrigerator() which may
144 * occur if the freezing of tasks fails. Namely, without the lock, if the
145 * freezing of tasks failed, thaw_tasks() might have run before a task in
146 * refrigerator() could call frozen_process(), in which case the task would be
147 * frozen and no one would thaw it.
148 */
Tejun Heoa5be2d02011-11-21 12:32:23 -0800149void __thaw_task(struct task_struct *p)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700150{
Tejun Heo0c9af092011-11-21 12:32:24 -0800151 unsigned long flags;
Tejun Heoa5be2d02011-11-21 12:32:23 -0800152
Tejun Heo0c9af092011-11-21 12:32:24 -0800153 spin_lock_irqsave(&freezer_lock, flags);
154 if (frozen(p)) {
Tejun Heoa5be2d02011-11-21 12:32:23 -0800155 p->flags &= ~PF_FROZEN;
Tejun Heoa5be2d02011-11-21 12:32:23 -0800156 wake_up_process(p);
Tejun Heo0c9af092011-11-21 12:32:24 -0800157 } else {
158 clear_freeze_flag(p);
159 }
160 spin_unlock_irqrestore(&freezer_lock, flags);
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700161}