2 * kernel/freezer.c - Function to freeze a process
4 * Originally from kernel/power/process.c
7 #include <linux/interrupt.h>
8 #include <linux/suspend.h>
9 #include <linux/export.h>
10 #include <linux/syscalls.h>
11 #include <linux/freezer.h>
14 * freezing is complete, mark current process as frozen
16 static inline void frozen_process(void)
18 if (!unlikely(current
->flags
& PF_NOFREEZE
)) {
19 current
->flags
|= PF_FROZEN
;
22 clear_freeze_flag(current
);
25 /* Refrigerator is place where frozen processes are stored :-). */
26 bool __refrigerator(void)
28 /* Hmm, should we be allowed to suspend when there are realtime
30 bool was_frozen
= false;
34 if (freezing(current
)) {
41 save
= current
->state
;
42 pr_debug("%s entered refrigerator\n", current
->comm
);
44 spin_lock_irq(¤t
->sighand
->siglock
);
45 recalc_sigpending(); /* We sent fake signal, clean it up */
46 spin_unlock_irq(¤t
->sighand
->siglock
);
48 /* prevent accounting of that task to load */
49 current
->flags
|= PF_FREEZING
;
52 set_current_state(TASK_UNINTERRUPTIBLE
);
59 /* Remove the accounting blocker */
60 current
->flags
&= ~PF_FREEZING
;
62 pr_debug("%s left refrigerator\n", current
->comm
);
65 * Restore saved task state before returning. The mb'd version
66 * needs to be used; otherwise, it might silently break
67 * synchronization which depends on ordered task state change.
69 set_current_state(save
);
73 EXPORT_SYMBOL(__refrigerator
);
75 static void fake_signal_wake_up(struct task_struct
*p
)
79 spin_lock_irqsave(&p
->sighand
->siglock
, flags
);
81 spin_unlock_irqrestore(&p
->sighand
->siglock
, flags
);
85 * freeze_task - send a freeze request to given task
86 * @p: task to send the request to
87 * @sig_only: if set, the request will only be sent if the task has the
88 * PF_FREEZER_NOSIG flag unset
89 * Return value: 'false', if @sig_only is set and the task has
90 * PF_FREEZER_NOSIG set or the task is frozen, 'true', otherwise
92 * The freeze request is sent by setting the tasks's TIF_FREEZE flag and
93 * either sending a fake signal to it or waking it up, depending on whether
94 * or not it has PF_FREEZER_NOSIG set. If @sig_only is set and the task
95 * has PF_FREEZER_NOSIG set (ie. it is a typical kernel thread), its
96 * TIF_FREEZE flag will not be set.
98 bool freeze_task(struct task_struct
*p
, bool sig_only
)
101 * We first check if the task is freezing and next if it has already
102 * been frozen to avoid the race with frozen_process() which first marks
103 * the task as frozen and next clears its TIF_FREEZE.
110 if (!sig_only
|| should_send_signal(p
))
116 if (should_send_signal(p
)) {
117 fake_signal_wake_up(p
);
119 * fake_signal_wake_up() goes through p's scheduler
120 * lock and guarantees that TASK_STOPPED/TRACED ->
121 * TASK_RUNNING transition can't race with task state
122 * testing in try_to_freeze_tasks().
124 } else if (sig_only
) {
127 wake_up_state(p
, TASK_INTERRUPTIBLE
);
133 void cancel_freezing(struct task_struct
*p
)
138 pr_debug(" clean up: %s\n", p
->comm
);
139 clear_freeze_flag(p
);
140 spin_lock_irqsave(&p
->sighand
->siglock
, flags
);
141 recalc_sigpending_and_wake(p
);
142 spin_unlock_irqrestore(&p
->sighand
->siglock
, flags
);
146 static int __thaw_process(struct task_struct
*p
)
149 p
->flags
&= ~PF_FROZEN
;
152 clear_freeze_flag(p
);
157 * Wake up a frozen process
159 * task_lock() is needed to prevent the race with refrigerator() which may
160 * occur if the freezing of tasks fails. Namely, without the lock, if the
161 * freezing of tasks failed, thaw_tasks() might have run before a task in
162 * refrigerator() could call frozen_process(), in which case the task would be
163 * frozen and no one would thaw it.
165 int thaw_process(struct task_struct
*p
)
168 if (__thaw_process(p
) == 1) {
176 EXPORT_SYMBOL(thaw_process
);
This page took 0.050746 seconds and 5 git commands to generate.