Skip to content

Commit 8174f15

Browse files
Matt Helsleytorvalds
authored andcommitted
container freezer: make refrigerator always available
Now that the TIF_FREEZE flag is available in all architectures, extract the refrigerator() and freeze_task() from kernel/power/process.c and make it available to all. The refrigerator() can now be used in a control group subsystem implementing a control group freezer. Signed-off-by: Cedric Le Goater <clg@fr.ibm.com> Signed-off-by: Matt Helsley <matthltc@us.ibm.com> Acked-by: Serge E. Hallyn <serue@us.ibm.com> Tested-by: Matt Helsley <matthltc@us.ibm.com> Cc: "Rafael J. Wysocki" <rjw@sisk.pl> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 83224b0 commit 8174f15

5 files changed

Lines changed: 137 additions & 119 deletions

File tree

‎include/linux/freezer.h‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <linux/sched.h>
77
#include <linux/wait.h>
88

9-
#ifdef CONFIG_PM_SLEEP
9+
#ifdef CONFIG_FREEZER
1010
/*
1111
* Check if a process has been frozen
1212
*/
@@ -39,6 +39,11 @@ static inline void clear_freeze_flag(struct task_struct *p)
3939
clear_tsk_thread_flag(p, TIF_FREEZE);
4040
}
4141

42+
static inline bool should_send_signal(struct task_struct *p)
43+
{
44+
return !(p->flags & PF_FREEZER_NOSIG);
45+
}
46+
4247
/*
4348
* Wake up a frozen process
4449
*
@@ -75,6 +80,9 @@ static inline int try_to_freeze(void)
7580
return 0;
7681
}
7782

83+
extern bool freeze_task(struct task_struct *p, bool sig_only);
84+
extern void cancel_freezing(struct task_struct *p);
85+
7886
/*
7987
* The PF_FREEZER_SKIP flag should be set by a vfork parent right before it
8088
* calls wait_for_completion(&vfork) and reset right after it returns from this
@@ -166,7 +174,7 @@ static inline void set_freezable_with_signal(void)
166174
} while (try_to_freeze()); \
167175
__retval; \
168176
})
169-
#else /* !CONFIG_PM_SLEEP */
177+
#else /* !CONFIG_FREEZER */
170178
static inline int frozen(struct task_struct *p) { return 0; }
171179
static inline int freezing(struct task_struct *p) { return 0; }
172180
static inline void set_freeze_flag(struct task_struct *p) {}
@@ -191,6 +199,6 @@ static inline void set_freezable_with_signal(void) {}
191199
#define wait_event_freezable_timeout(wq, condition, timeout) \
192200
wait_event_interruptible_timeout(wq, condition, timeout)
193201

194-
#endif /* !CONFIG_PM_SLEEP */
202+
#endif /* !CONFIG_FREEZER */
195203

196204
#endif /* FREEZER_H_INCLUDED */

‎kernel/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ CFLAGS_REMOVE_sched_clock.o = -pg
2424
CFLAGS_REMOVE_sched.o = -mno-spe -pg
2525
endif
2626

27+
obj-$(CONFIG_FREEZER) += freezer.o
2728
obj-$(CONFIG_PROFILING) += profile.o
2829
obj-$(CONFIG_SYSCTL_SYSCALL_CHECK) += sysctl_check.o
2930
obj-$(CONFIG_STACKTRACE) += stacktrace.o

‎kernel/freezer.c‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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>
9+
#include <linux/module.h>
10+
#include <linux/syscalls.h>
11+
#include <linux/freezer.h>
12+
13+
/*
14+
* freezing is complete, mark current process as frozen
15+
*/
16+
static inline void frozen_process(void)
17+
{
18+
if (!unlikely(current->flags & PF_NOFREEZE)) {
19+
current->flags |= PF_FROZEN;
20+
wmb();
21+
}
22+
clear_freeze_flag(current);
23+
}
24+
25+
/* Refrigerator is place where frozen processes are stored :-). */
26+
void refrigerator(void)
27+
{
28+
/* Hmm, should we be allowed to suspend when there are realtime
29+
processes around? */
30+
long save;
31+
32+
task_lock(current);
33+
if (freezing(current)) {
34+
frozen_process();
35+
task_unlock(current);
36+
} else {
37+
task_unlock(current);
38+
return;
39+
}
40+
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);
49+
if (!frozen(current))
50+
break;
51+
schedule();
52+
}
53+
pr_debug("%s left refrigerator\n", current->comm);
54+
__set_current_state(save);
55+
}
56+
EXPORT_SYMBOL(refrigerator);
57+
58+
static void fake_signal_wake_up(struct task_struct *p)
59+
{
60+
unsigned long flags;
61+
62+
spin_lock_irqsave(&p->sighand->siglock, flags);
63+
signal_wake_up(p, 0);
64+
spin_unlock_irqrestore(&p->sighand->siglock, flags);
65+
}
66+
67+
/**
68+
* freeze_task - send a freeze request to given task
69+
* @p: task to send the request to
70+
* @sig_only: if set, the request will only be sent if the task has the
71+
* PF_FREEZER_NOSIG flag unset
72+
* Return value: 'false', if @sig_only is set and the task has
73+
* PF_FREEZER_NOSIG set or the task is frozen, 'true', otherwise
74+
*
75+
* The freeze request is sent by setting the tasks's TIF_FREEZE flag and
76+
* either sending a fake signal to it or waking it up, depending on whether
77+
* or not it has PF_FREEZER_NOSIG set. If @sig_only is set and the task
78+
* has PF_FREEZER_NOSIG set (ie. it is a typical kernel thread), its
79+
* TIF_FREEZE flag will not be set.
80+
*/
81+
bool freeze_task(struct task_struct *p, bool sig_only)
82+
{
83+
/*
84+
* We first check if the task is freezing and next if it has already
85+
* been frozen to avoid the race with frozen_process() which first marks
86+
* the task as frozen and next clears its TIF_FREEZE.
87+
*/
88+
if (!freezing(p)) {
89+
rmb();
90+
if (frozen(p))
91+
return false;
92+
93+
if (!sig_only || should_send_signal(p))
94+
set_freeze_flag(p);
95+
else
96+
return false;
97+
}
98+
99+
if (should_send_signal(p)) {
100+
if (!signal_pending(p))
101+
fake_signal_wake_up(p);
102+
} else if (sig_only) {
103+
return false;
104+
} else {
105+
wake_up_state(p, TASK_INTERRUPTIBLE);
106+
}
107+
108+
return true;
109+
}
110+
111+
void cancel_freezing(struct task_struct *p)
112+
{
113+
unsigned long flags;
114+
115+
if (freezing(p)) {
116+
pr_debug(" clean up: %s\n", p->comm);
117+
clear_freeze_flag(p);
118+
spin_lock_irqsave(&p->sighand->siglock, flags);
119+
recalc_sigpending_and_wake(p);
120+
spin_unlock_irqrestore(&p->sighand->siglock, flags);
121+
}
122+
}

‎kernel/power/Kconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ config PM_SLEEP
8585
depends on SUSPEND || HIBERNATION || XEN_SAVE_RESTORE
8686
default y
8787

88+
config FREEZER
89+
def_bool PM_SLEEP
90+
8891
config SUSPEND
8992
bool "Suspend to RAM and standby"
9093
depends on PM && ARCH_SUSPEND_POSSIBLE

‎kernel/power/process.c‎

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -28,121 +28,6 @@ static inline int freezeable(struct task_struct * p)
2828
return 1;
2929
}
3030

31-
/*
32-
* freezing is complete, mark current process as frozen
33-
*/
34-
static inline void frozen_process(void)
35-
{
36-
if (!unlikely(current->flags & PF_NOFREEZE)) {
37-
current->flags |= PF_FROZEN;
38-
wmb();
39-
}
40-
clear_freeze_flag(current);
41-
}
42-
43-
/* Refrigerator is place where frozen processes are stored :-). */
44-
void refrigerator(void)
45-
{
46-
/* Hmm, should we be allowed to suspend when there are realtime
47-
processes around? */
48-
long save;
49-
50-
task_lock(current);
51-
if (freezing(current)) {
52-
frozen_process();
53-
task_unlock(current);
54-
} else {
55-
task_unlock(current);
56-
return;
57-
}
58-
save = current->state;
59-
pr_debug("%s entered refrigerator\n", current->comm);
60-
61-
spin_lock_irq(&current->sighand->siglock);
62-
recalc_sigpending(); /* We sent fake signal, clean it up */
63-
spin_unlock_irq(&current->sighand->siglock);
64-
65-
for (;;) {
66-
set_current_state(TASK_UNINTERRUPTIBLE);
67-
if (!frozen(current))
68-
break;
69-
schedule();
70-
}
71-
pr_debug("%s left refrigerator\n", current->comm);
72-
__set_current_state(save);
73-
}
74-
75-
static void fake_signal_wake_up(struct task_struct *p)
76-
{
77-
unsigned long flags;
78-
79-
spin_lock_irqsave(&p->sighand->siglock, flags);
80-
signal_wake_up(p, 0);
81-
spin_unlock_irqrestore(&p->sighand->siglock, flags);
82-
}
83-
84-
static inline bool should_send_signal(struct task_struct *p)
85-
{
86-
return !(p->flags & PF_FREEZER_NOSIG);
87-
}
88-
89-
/**
90-
* freeze_task - send a freeze request to given task
91-
* @p: task to send the request to
92-
* @sig_only: if set, the request will only be sent if the task has the
93-
* PF_FREEZER_NOSIG flag unset
94-
* Return value: 'false', if @sig_only is set and the task has
95-
* PF_FREEZER_NOSIG set or the task is frozen, 'true', otherwise
96-
*
97-
* The freeze request is sent by setting the tasks's TIF_FREEZE flag and
98-
* either sending a fake signal to it or waking it up, depending on whether
99-
* or not it has PF_FREEZER_NOSIG set. If @sig_only is set and the task
100-
* has PF_FREEZER_NOSIG set (ie. it is a typical kernel thread), its
101-
* TIF_FREEZE flag will not be set.
102-
*/
103-
static bool freeze_task(struct task_struct *p, bool sig_only)
104-
{
105-
/*
106-
* We first check if the task is freezing and next if it has already
107-
* been frozen to avoid the race with frozen_process() which first marks
108-
* the task as frozen and next clears its TIF_FREEZE.
109-
*/
110-
if (!freezing(p)) {
111-
rmb();
112-
if (frozen(p))
113-
return false;
114-
115-
if (!sig_only || should_send_signal(p))
116-
set_freeze_flag(p);
117-
else
118-
return false;
119-
}
120-
121-
if (should_send_signal(p)) {
122-
if (!signal_pending(p))
123-
fake_signal_wake_up(p);
124-
} else if (sig_only) {
125-
return false;
126-
} else {
127-
wake_up_state(p, TASK_INTERRUPTIBLE);
128-
}
129-
130-
return true;
131-
}
132-
133-
static void cancel_freezing(struct task_struct *p)
134-
{
135-
unsigned long flags;
136-
137-
if (freezing(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);
143-
}
144-
}
145-
14631
static int try_to_freeze_tasks(bool sig_only)
14732
{
14833
struct task_struct *g, *p;
@@ -264,4 +149,3 @@ void thaw_processes(void)
264149
printk("done.\n");
265150
}
266151

267-
EXPORT_SYMBOL(refrigerator);

0 commit comments

Comments
 (0)