cgroup_freezer: make freezer->state mask of flags

freezer->state was an enum value - one of THAWED, FREEZING and FROZEN.
As the scheduled full hierarchy support requires more than one
freezing condition, switch it to mask of flags.  If FREEZING is not
set, it's thawed.  FREEZING is set if freezing or frozen.  If frozen,
both FREEZING and FROZEN are set.  Now that tasks can be attached to
an already frozen cgroup, this also makes freezing condition checks
more natural.

This patch doesn't introduce any behavior change.

Signed-off-by: Tejun Heo <tj@kernel.org>
Reviewed-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Reviewed-by: Michal Hocko <mhocko@suse.cz>
This commit is contained in:
Tejun Heo 2012-11-09 09:12:30 -08:00
parent 04a4ec3257
commit d6a2fe1342
1 changed files with 27 additions and 33 deletions

View File

@ -22,15 +22,14 @@
#include <linux/freezer.h> #include <linux/freezer.h>
#include <linux/seq_file.h> #include <linux/seq_file.h>
enum freezer_state { enum freezer_state_flags {
CGROUP_THAWED = 0, CGROUP_FREEZING = (1 << 1), /* this freezer is freezing */
CGROUP_FREEZING, CGROUP_FROZEN = (1 << 3), /* this and its descendants frozen */
CGROUP_FROZEN,
}; };
struct freezer { struct freezer {
struct cgroup_subsys_state css; struct cgroup_subsys_state css;
enum freezer_state state; unsigned int state;
spinlock_t lock; spinlock_t lock;
}; };
@ -48,12 +47,10 @@ static inline struct freezer *task_freezer(struct task_struct *task)
bool cgroup_freezing(struct task_struct *task) bool cgroup_freezing(struct task_struct *task)
{ {
enum freezer_state state;
bool ret; bool ret;
rcu_read_lock(); rcu_read_lock();
state = task_freezer(task)->state; ret = task_freezer(task)->state & CGROUP_FREEZING;
ret = state == CGROUP_FREEZING || state == CGROUP_FROZEN;
rcu_read_unlock(); rcu_read_unlock();
return ret; return ret;
@ -63,10 +60,13 @@ bool cgroup_freezing(struct task_struct *task)
* cgroups_write_string() limits the size of freezer state strings to * cgroups_write_string() limits the size of freezer state strings to
* CGROUP_LOCAL_BUFFER_SIZE * CGROUP_LOCAL_BUFFER_SIZE
*/ */
static const char *freezer_state_strs[] = { static const char *freezer_state_strs(unsigned int state)
"THAWED", {
"FREEZING", if (state & CGROUP_FROZEN)
"FROZEN", return "FROZEN";
if (state & CGROUP_FREEZING)
return "FREEZING";
return "THAWED";
}; };
/* /*
@ -91,7 +91,6 @@ static struct cgroup_subsys_state *freezer_create(struct cgroup *cgroup)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
spin_lock_init(&freezer->lock); spin_lock_init(&freezer->lock);
freezer->state = CGROUP_THAWED;
return &freezer->css; return &freezer->css;
} }
@ -99,7 +98,7 @@ static void freezer_destroy(struct cgroup *cgroup)
{ {
struct freezer *freezer = cgroup_freezer(cgroup); struct freezer *freezer = cgroup_freezer(cgroup);
if (freezer->state != CGROUP_THAWED) if (freezer->state & CGROUP_FREEZING)
atomic_dec(&system_freezing_cnt); atomic_dec(&system_freezing_cnt);
kfree(freezer); kfree(freezer);
} }
@ -129,15 +128,13 @@ static void freezer_attach(struct cgroup *new_cgrp, struct cgroup_taskset *tset)
* Tasks in @tset are on @new_cgrp but may not conform to its * Tasks in @tset are on @new_cgrp but may not conform to its
* current state before executing the following - !frozen tasks may * current state before executing the following - !frozen tasks may
* be visible in a FROZEN cgroup and frozen tasks in a THAWED one. * be visible in a FROZEN cgroup and frozen tasks in a THAWED one.
* This means that, to determine whether to freeze, one should test
* whether the state equals THAWED.
*/ */
cgroup_taskset_for_each(task, new_cgrp, tset) { cgroup_taskset_for_each(task, new_cgrp, tset) {
if (freezer->state == CGROUP_THAWED) { if (!(freezer->state & CGROUP_FREEZING)) {
__thaw_task(task); __thaw_task(task);
} else { } else {
freeze_task(task); freeze_task(task);
freezer->state = CGROUP_FREEZING; freezer->state &= ~CGROUP_FROZEN;
} }
} }
@ -159,11 +156,7 @@ static void freezer_fork(struct task_struct *task)
goto out; goto out;
spin_lock_irq(&freezer->lock); spin_lock_irq(&freezer->lock);
/* if (freezer->state & CGROUP_FREEZING)
* @task might have been just migrated into a FROZEN cgroup. Test
* equality with THAWED. Read the comment in freezer_attach().
*/
if (freezer->state != CGROUP_THAWED)
freeze_task(task); freeze_task(task);
spin_unlock_irq(&freezer->lock); spin_unlock_irq(&freezer->lock);
out: out:
@ -184,7 +177,8 @@ static void update_if_frozen(struct freezer *freezer)
struct cgroup_iter it; struct cgroup_iter it;
struct task_struct *task; struct task_struct *task;
if (freezer->state != CGROUP_FREEZING) if (!(freezer->state & CGROUP_FREEZING) ||
(freezer->state & CGROUP_FROZEN))
return; return;
cgroup_iter_start(cgroup, &it); cgroup_iter_start(cgroup, &it);
@ -202,7 +196,7 @@ static void update_if_frozen(struct freezer *freezer)
} }
} }
freezer->state = CGROUP_FROZEN; freezer->state |= CGROUP_FROZEN;
notyet: notyet:
cgroup_iter_end(cgroup, &it); cgroup_iter_end(cgroup, &it);
} }
@ -211,14 +205,14 @@ static int freezer_read(struct cgroup *cgroup, struct cftype *cft,
struct seq_file *m) struct seq_file *m)
{ {
struct freezer *freezer = cgroup_freezer(cgroup); struct freezer *freezer = cgroup_freezer(cgroup);
enum freezer_state state; unsigned int state;
spin_lock_irq(&freezer->lock); spin_lock_irq(&freezer->lock);
update_if_frozen(freezer); update_if_frozen(freezer);
state = freezer->state; state = freezer->state;
spin_unlock_irq(&freezer->lock); spin_unlock_irq(&freezer->lock);
seq_puts(m, freezer_state_strs[state]); seq_puts(m, freezer_state_strs(state));
seq_putc(m, '\n'); seq_putc(m, '\n');
return 0; return 0;
} }
@ -258,14 +252,14 @@ static void freezer_apply_state(struct freezer *freezer, bool freeze)
lockdep_assert_held(&freezer->lock); lockdep_assert_held(&freezer->lock);
if (freeze) { if (freeze) {
if (freezer->state == CGROUP_THAWED) if (!(freezer->state & CGROUP_FREEZING))
atomic_inc(&system_freezing_cnt); atomic_inc(&system_freezing_cnt);
freezer->state = CGROUP_FREEZING; freezer->state |= CGROUP_FREEZING;
freeze_cgroup(freezer); freeze_cgroup(freezer);
} else { } else {
if (freezer->state != CGROUP_THAWED) if (freezer->state & CGROUP_FREEZING)
atomic_dec(&system_freezing_cnt); atomic_dec(&system_freezing_cnt);
freezer->state = CGROUP_THAWED; freezer->state &= ~(CGROUP_FREEZING | CGROUP_FROZEN);
unfreeze_cgroup(freezer); unfreeze_cgroup(freezer);
} }
} }
@ -290,9 +284,9 @@ static int freezer_write(struct cgroup *cgroup, struct cftype *cft,
{ {
bool freeze; bool freeze;
if (strcmp(buffer, freezer_state_strs[CGROUP_THAWED]) == 0) if (strcmp(buffer, freezer_state_strs(0)) == 0)
freeze = false; freeze = false;
else if (strcmp(buffer, freezer_state_strs[CGROUP_FROZEN]) == 0) else if (strcmp(buffer, freezer_state_strs(CGROUP_FROZEN)) == 0)
freeze = true; freeze = true;
else else
return -EINVAL; return -EINVAL;