Commit 22b4e111 authored by Tejun Heo's avatar Tejun Heo

cgroup_freezer: prepare for removal of TIF_FREEZE

TIF_FREEZE will be removed soon and freezing() will directly test
whether any freezing condition is in effect.  Make the following
changes in preparation.

* Rename cgroup_freezing_or_frozen() to cgroup_freezing() and make it
  return bool.

* Make cgroup_freezing() access task_freezer() under rcu read lock
  instead of task_lock().  This makes the state dereferencing racy
  against task moving to another cgroup; however, it was already racy
  without this change as ->state dereference wasn't synchronized.
  This will be later dealt with using attach hooks.

* freezer->state is now set before trying to push tasks into the
  target state.

-v2: Oleg pointed out that freeze_change_state() was setting
     freeze->state incorrectly to CGROUP_FROZEN instead of
     CGROUP_FREEZING.  Fixed.

-v3: Matt pointed out that setting CGROUP_FROZEN used to always invoke
     try_to_freeze_cgroup() regardless of the current state.  Patch
     updated such that the actual freeze/thaw operations are always
     performed on invocation.  This shouldn't make any difference
     unless something is broken.
Signed-off-by: default avatarTejun Heo <tj@kernel.org>
Acked-by: default avatarPaul Menage <paul@paulmenage.org>
Cc: Li Zefan <lizf@cn.fujitsu.com>
Cc: Oleg Nesterov <oleg@redhat.com>
parent 03afed8b
...@@ -63,11 +63,11 @@ static inline bool try_to_freeze(void) ...@@ -63,11 +63,11 @@ static inline bool try_to_freeze(void)
extern bool freeze_task(struct task_struct *p, bool sig_only); extern bool freeze_task(struct task_struct *p, bool sig_only);
#ifdef CONFIG_CGROUP_FREEZER #ifdef CONFIG_CGROUP_FREEZER
extern int cgroup_freezing_or_frozen(struct task_struct *task); extern bool cgroup_freezing(struct task_struct *task);
#else /* !CONFIG_CGROUP_FREEZER */ #else /* !CONFIG_CGROUP_FREEZER */
static inline int cgroup_freezing_or_frozen(struct task_struct *task) static inline bool cgroup_freezing(struct task_struct *task)
{ {
return 0; return false;
} }
#endif /* !CONFIG_CGROUP_FREEZER */ #endif /* !CONFIG_CGROUP_FREEZER */
......
...@@ -48,19 +48,17 @@ static inline struct freezer *task_freezer(struct task_struct *task) ...@@ -48,19 +48,17 @@ static inline struct freezer *task_freezer(struct task_struct *task)
struct freezer, css); struct freezer, css);
} }
static inline int __cgroup_freezing_or_frozen(struct task_struct *task) bool cgroup_freezing(struct task_struct *task)
{ {
enum freezer_state state = task_freezer(task)->state; enum freezer_state state;
return (state == CGROUP_FREEZING) || (state == CGROUP_FROZEN); bool ret;
}
int cgroup_freezing_or_frozen(struct task_struct *task) rcu_read_lock();
{ state = task_freezer(task)->state;
int result; ret = state == CGROUP_FREEZING || state == CGROUP_FROZEN;
task_lock(task); rcu_read_unlock();
result = __cgroup_freezing_or_frozen(task);
task_unlock(task); return ret;
return result;
} }
/* /*
...@@ -102,9 +100,6 @@ struct cgroup_subsys freezer_subsys; ...@@ -102,9 +100,6 @@ struct cgroup_subsys freezer_subsys;
* freezer_can_attach(): * freezer_can_attach():
* cgroup_mutex (held by caller of can_attach) * cgroup_mutex (held by caller of can_attach)
* *
* cgroup_freezing_or_frozen():
* task->alloc_lock (to get task's cgroup)
*
* freezer_fork() (preserving fork() performance means can't take cgroup_mutex): * freezer_fork() (preserving fork() performance means can't take cgroup_mutex):
* freezer->lock * freezer->lock
* sighand->siglock (if the cgroup is freezing) * sighand->siglock (if the cgroup is freezing)
...@@ -177,13 +172,7 @@ static int freezer_can_attach(struct cgroup_subsys *ss, ...@@ -177,13 +172,7 @@ static int freezer_can_attach(struct cgroup_subsys *ss,
static int freezer_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk) static int freezer_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
{ {
rcu_read_lock(); return cgroup_freezing(tsk) ? -EBUSY : 0;
if (__cgroup_freezing_or_frozen(tsk)) {
rcu_read_unlock();
return -EBUSY;
}
rcu_read_unlock();
return 0;
} }
static void freezer_fork(struct cgroup_subsys *ss, struct task_struct *task) static void freezer_fork(struct cgroup_subsys *ss, struct task_struct *task)
...@@ -279,7 +268,6 @@ static int try_to_freeze_cgroup(struct cgroup *cgroup, struct freezer *freezer) ...@@ -279,7 +268,6 @@ static int try_to_freeze_cgroup(struct cgroup *cgroup, struct freezer *freezer)
struct task_struct *task; struct task_struct *task;
unsigned int num_cant_freeze_now = 0; unsigned int num_cant_freeze_now = 0;
freezer->state = CGROUP_FREEZING;
cgroup_iter_start(cgroup, &it); cgroup_iter_start(cgroup, &it);
while ((task = cgroup_iter_next(cgroup, &it))) { while ((task = cgroup_iter_next(cgroup, &it))) {
if (!freeze_task(task, true)) if (!freeze_task(task, true))
...@@ -303,8 +291,6 @@ static void unfreeze_cgroup(struct cgroup *cgroup, struct freezer *freezer) ...@@ -303,8 +291,6 @@ static void unfreeze_cgroup(struct cgroup *cgroup, struct freezer *freezer)
while ((task = cgroup_iter_next(cgroup, &it))) while ((task = cgroup_iter_next(cgroup, &it)))
__thaw_task(task); __thaw_task(task);
cgroup_iter_end(cgroup, &it); cgroup_iter_end(cgroup, &it);
freezer->state = CGROUP_THAWED;
} }
static int freezer_change_state(struct cgroup *cgroup, static int freezer_change_state(struct cgroup *cgroup,
...@@ -318,20 +304,20 @@ static int freezer_change_state(struct cgroup *cgroup, ...@@ -318,20 +304,20 @@ static int freezer_change_state(struct cgroup *cgroup,
spin_lock_irq(&freezer->lock); spin_lock_irq(&freezer->lock);
update_if_frozen(cgroup, freezer); update_if_frozen(cgroup, freezer);
if (goal_state == freezer->state)
goto out;
switch (goal_state) { switch (goal_state) {
case CGROUP_THAWED: case CGROUP_THAWED:
freezer->state = CGROUP_THAWED;
unfreeze_cgroup(cgroup, freezer); unfreeze_cgroup(cgroup, freezer);
break; break;
case CGROUP_FROZEN: case CGROUP_FROZEN:
freezer->state = CGROUP_FREEZING;
retval = try_to_freeze_cgroup(cgroup, freezer); retval = try_to_freeze_cgroup(cgroup, freezer);
break; break;
default: default:
BUG(); BUG();
} }
out:
spin_unlock_irq(&freezer->lock); spin_unlock_irq(&freezer->lock);
return retval; return retval;
......
...@@ -170,7 +170,7 @@ void thaw_processes(void) ...@@ -170,7 +170,7 @@ void thaw_processes(void)
read_lock(&tasklist_lock); read_lock(&tasklist_lock);
do_each_thread(g, p) { do_each_thread(g, p) {
if (cgroup_freezing_or_frozen(p)) if (cgroup_freezing(p))
continue; continue;
__thaw_task(p); __thaw_task(p);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment