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