2 * cgroup_freezer.c - control group freezer subsystem
4 * Copyright IBM Corporation, 2007
6 * Author : Cedric Le Goater <clg@fr.ibm.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2.1 of the GNU Lesser General Public License
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it would be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 #include <linux/module.h>
18 #include <linux/cgroup.h>
20 #include <linux/uaccess.h>
21 #include <linux/freezer.h>
22 #include <linux/seq_file.h>
31 struct cgroup_subsys_state css
;
32 enum freezer_state state
;
33 spinlock_t lock
; /* protects _writes_ to state */
36 static inline struct freezer
*cgroup_freezer(
37 struct cgroup
*cgroup
)
40 cgroup_subsys_state(cgroup
, freezer_subsys_id
),
44 static inline struct freezer
*task_freezer(struct task_struct
*task
)
46 return container_of(task_subsys_state(task
, freezer_subsys_id
),
50 int cgroup_freezing_or_frozen(struct task_struct
*task
)
52 struct freezer
*freezer
;
53 enum freezer_state state
;
56 freezer
= task_freezer(task
);
57 if (!freezer
->css
.cgroup
->parent
)
58 state
= CGROUP_THAWED
; /* root cgroup can't be frozen */
60 state
= freezer
->state
;
63 return (state
== CGROUP_FREEZING
) || (state
== CGROUP_FROZEN
);
67 * cgroups_write_string() limits the size of freezer state strings to
68 * CGROUP_LOCAL_BUFFER_SIZE
70 static const char *freezer_state_strs
[] = {
78 * Transitions are caused by userspace writes to the freezer.state file.
79 * The values in parenthesis are state labels. The rest are edge labels.
81 * (THAWED) --FROZEN--> (FREEZING) --FROZEN--> (FROZEN)
83 * | \_______THAWED_______/ |
84 * \__________________________THAWED____________/
87 struct cgroup_subsys freezer_subsys
;
89 /* Locks taken and their ordering
90 * ------------------------------
92 * cgroup_mutex (AKA cgroup_lock)
93 * task->alloc_lock (AKA task_lock)
95 * task->sighand->siglock
97 * cgroup code forces css_set_lock to be taken before task->alloc_lock
99 * freezer_create(), freezer_destroy():
100 * cgroup_mutex [ by cgroup core ]
106 * task->alloc_lock (to get task's cgroup)
108 * freezer_fork() (preserving fork() performance means can't take cgroup_mutex):
109 * task->alloc_lock (to get task's cgroup)
111 * sighand->siglock (if the cgroup is freezing)
116 * read_lock css_set_lock (cgroup iterator start)
118 * freezer_write() (freeze):
121 * read_lock css_set_lock (cgroup iterator start)
124 * freezer_write() (unfreeze):
127 * read_lock css_set_lock (cgroup iterator start)
128 * task->alloc_lock (to prevent races with freeze_task())
131 static struct cgroup_subsys_state
*freezer_create(struct cgroup_subsys
*ss
,
132 struct cgroup
*cgroup
)
134 struct freezer
*freezer
;
136 freezer
= kzalloc(sizeof(struct freezer
), GFP_KERNEL
);
138 return ERR_PTR(-ENOMEM
);
140 spin_lock_init(&freezer
->lock
);
141 freezer
->state
= CGROUP_THAWED
;
142 return &freezer
->css
;
145 static void freezer_destroy(struct cgroup_subsys
*ss
,
146 struct cgroup
*cgroup
)
148 kfree(cgroup_freezer(cgroup
));
151 /* Task is frozen or will freeze immediately when next it gets woken */
152 static bool is_task_frozen_enough(struct task_struct
*task
)
154 return frozen(task
) ||
155 (task_is_stopped_or_traced(task
) && freezing(task
));
159 * The call to cgroup_lock() in the freezer.state write method prevents
160 * a write to that file racing against an attach, and hence the
161 * can_attach() result will remain valid until the attach completes.
163 static int freezer_can_attach(struct cgroup_subsys
*ss
,
164 struct cgroup
*new_cgroup
,
165 struct task_struct
*task
, bool threadgroup
)
167 struct freezer
*freezer
;
169 if ((current
!= task
) && (!capable(CAP_SYS_ADMIN
))) {
170 const struct cred
*cred
= current_cred(), *tcred
;
172 tcred
= __task_cred(task
);
173 if (cred
->euid
!= tcred
->uid
&& cred
->euid
!= tcred
->suid
)
178 * Anything frozen can't move or be moved to/from.
180 * Since orig_freezer->state == FROZEN means that @task has been
181 * frozen, so it's sufficient to check the latter condition.
184 if (is_task_frozen_enough(task
))
187 freezer
= cgroup_freezer(new_cgroup
);
188 if (freezer
->state
== CGROUP_FROZEN
)
192 struct task_struct
*c
;
195 list_for_each_entry_rcu(c
, &task
->thread_group
, thread_group
) {
196 if (is_task_frozen_enough(c
)) {
207 static void freezer_fork(struct cgroup_subsys
*ss
, struct task_struct
*task
)
209 struct freezer
*freezer
;
212 * No lock is needed, since the task isn't on tasklist yet,
213 * so it can't be moved to another cgroup, which means the
214 * freezer won't be removed and will be valid during this
217 freezer
= task_freezer(task
);
220 * The root cgroup is non-freezable, so we can skip the
223 if (!freezer
->css
.cgroup
->parent
)
226 spin_lock_irq(&freezer
->lock
);
227 BUG_ON(freezer
->state
== CGROUP_FROZEN
);
229 /* Locking avoids race with FREEZING -> THAWED transitions. */
230 if (freezer
->state
== CGROUP_FREEZING
)
231 freeze_task(task
, true);
232 spin_unlock_irq(&freezer
->lock
);
236 * caller must hold freezer->lock
238 static void update_freezer_state(struct cgroup
*cgroup
,
239 struct freezer
*freezer
)
241 struct cgroup_iter it
;
242 struct task_struct
*task
;
243 unsigned int nfrozen
= 0, ntotal
= 0;
245 cgroup_iter_start(cgroup
, &it
);
246 while ((task
= cgroup_iter_next(cgroup
, &it
))) {
248 if (is_task_frozen_enough(task
))
253 * Transition to FROZEN when no new tasks can be added ensures
254 * that we never exist in the FROZEN state while there are unfrozen
257 if (nfrozen
== ntotal
)
258 freezer
->state
= CGROUP_FROZEN
;
259 else if (nfrozen
> 0)
260 freezer
->state
= CGROUP_FREEZING
;
262 freezer
->state
= CGROUP_THAWED
;
263 cgroup_iter_end(cgroup
, &it
);
266 static int freezer_read(struct cgroup
*cgroup
, struct cftype
*cft
,
269 struct freezer
*freezer
;
270 enum freezer_state state
;
272 if (!cgroup_lock_live_group(cgroup
))
275 freezer
= cgroup_freezer(cgroup
);
276 spin_lock_irq(&freezer
->lock
);
277 state
= freezer
->state
;
278 if (state
== CGROUP_FREEZING
) {
279 /* We change from FREEZING to FROZEN lazily if the cgroup was
280 * only partially frozen when we exitted write. */
281 update_freezer_state(cgroup
, freezer
);
282 state
= freezer
->state
;
284 spin_unlock_irq(&freezer
->lock
);
287 seq_puts(m
, freezer_state_strs
[state
]);
292 static int try_to_freeze_cgroup(struct cgroup
*cgroup
, struct freezer
*freezer
)
294 struct cgroup_iter it
;
295 struct task_struct
*task
;
296 unsigned int num_cant_freeze_now
= 0;
298 freezer
->state
= CGROUP_FREEZING
;
299 cgroup_iter_start(cgroup
, &it
);
300 while ((task
= cgroup_iter_next(cgroup
, &it
))) {
301 if (!freeze_task(task
, true))
303 if (is_task_frozen_enough(task
))
305 if (!freezing(task
) && !freezer_should_skip(task
))
306 num_cant_freeze_now
++;
308 cgroup_iter_end(cgroup
, &it
);
310 return num_cant_freeze_now
? -EBUSY
: 0;
313 static void unfreeze_cgroup(struct cgroup
*cgroup
, struct freezer
*freezer
)
315 struct cgroup_iter it
;
316 struct task_struct
*task
;
318 cgroup_iter_start(cgroup
, &it
);
319 while ((task
= cgroup_iter_next(cgroup
, &it
))) {
322 cgroup_iter_end(cgroup
, &it
);
324 freezer
->state
= CGROUP_THAWED
;
327 static int freezer_change_state(struct cgroup
*cgroup
,
328 enum freezer_state goal_state
)
330 struct freezer
*freezer
;
333 freezer
= cgroup_freezer(cgroup
);
335 spin_lock_irq(&freezer
->lock
);
337 update_freezer_state(cgroup
, freezer
);
338 if (goal_state
== freezer
->state
)
341 switch (goal_state
) {
343 unfreeze_cgroup(cgroup
, freezer
);
346 retval
= try_to_freeze_cgroup(cgroup
, freezer
);
352 spin_unlock_irq(&freezer
->lock
);
357 static int freezer_write(struct cgroup
*cgroup
,
362 enum freezer_state goal_state
;
364 if (strcmp(buffer
, freezer_state_strs
[CGROUP_THAWED
]) == 0)
365 goal_state
= CGROUP_THAWED
;
366 else if (strcmp(buffer
, freezer_state_strs
[CGROUP_FROZEN
]) == 0)
367 goal_state
= CGROUP_FROZEN
;
371 if (!cgroup_lock_live_group(cgroup
))
373 retval
= freezer_change_state(cgroup
, goal_state
);
378 static struct cftype files
[] = {
381 .read_seq_string
= freezer_read
,
382 .write_string
= freezer_write
,
386 static int freezer_populate(struct cgroup_subsys
*ss
, struct cgroup
*cgroup
)
390 return cgroup_add_files(cgroup
, ss
, files
, ARRAY_SIZE(files
));
393 struct cgroup_subsys freezer_subsys
= {
395 .create
= freezer_create
,
396 .destroy
= freezer_destroy
,
397 .populate
= freezer_populate
,
398 .subsys_id
= freezer_subsys_id
,
399 .can_attach
= freezer_can_attach
,
401 .fork
= freezer_fork
,