Skip to content

Commit 5edee61

Browse files
committed
cgroup: cgroup_subsys->fork() should be called after the task is added to css_set
cgroup core has a bug which violates a basic rule about event notifications - when a new entity needs to be added, you add that to the notification list first and then make the new entity conform to the current state. If done in the reverse order, an event happening inbetween will be lost. cgroup_subsys->fork() is invoked way before the new task is added to the css_set. Currently, cgroup_freezer is the only user of ->fork() and uses it to make new tasks conform to the current state of the freezer. If FROZEN state is requested while fork is in progress between cgroup_fork_callbacks() and cgroup_post_fork(), the child could escape freezing - the cgroup isn't frozen when ->fork() is called and the freezer couldn't see the new task on the css_set. This patch moves cgroup_subsys->fork() invocation to cgroup_post_fork() after the new task is added to the css_set. cgroup_fork_callbacks() is removed. Because now a task may be migrated during cgroup_subsys->fork(), freezer_fork() is updated so that it adheres to the usual RCU locking and the rather pointless comment on why locking can be different there is removed (if it doesn't make anything simpler, why even bother?). Signed-off-by: Tejun Heo <tj@kernel.org> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Rafael J. Wysocki <rjw@sisk.pl> Cc: stable@vger.kernel.org
1 parent ddffeb8 commit 5edee61

4 files changed

Lines changed: 35 additions & 50 deletions

File tree

‎include/linux/cgroup.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ extern int cgroup_lock_is_held(void);
3434
extern bool cgroup_lock_live_group(struct cgroup *cgrp);
3535
extern void cgroup_unlock(void);
3636
extern void cgroup_fork(struct task_struct *p);
37-
extern void cgroup_fork_callbacks(struct task_struct *p);
3837
extern void cgroup_post_fork(struct task_struct *p);
3938
extern void cgroup_exit(struct task_struct *p, int run_callbacks);
4039
extern int cgroupstats_build(struct cgroupstats *stats,

‎kernel/cgroup.c‎

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4843,45 +4843,20 @@ void cgroup_fork(struct task_struct *child)
48434843
INIT_LIST_HEAD(&child->cg_list);
48444844
}
48454845

4846-
/**
4847-
* cgroup_fork_callbacks - run fork callbacks
4848-
* @child: the new task
4849-
*
4850-
* Called on a new task very soon before adding it to the
4851-
* tasklist. No need to take any locks since no-one can
4852-
* be operating on this task.
4853-
*/
4854-
void cgroup_fork_callbacks(struct task_struct *child)
4855-
{
4856-
if (need_forkexit_callback) {
4857-
int i;
4858-
for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4859-
struct cgroup_subsys *ss = subsys[i];
4860-
4861-
/*
4862-
* forkexit callbacks are only supported for
4863-
* builtin subsystems.
4864-
*/
4865-
if (!ss || ss->module)
4866-
continue;
4867-
4868-
if (ss->fork)
4869-
ss->fork(child);
4870-
}
4871-
}
4872-
}
4873-
48744846
/**
48754847
* cgroup_post_fork - called on a new task after adding it to the task list
48764848
* @child: the task in question
48774849
*
4878-
* Adds the task to the list running through its css_set if necessary.
4879-
* Has to be after the task is visible on the task list in case we race
4880-
* with the first call to cgroup_iter_start() - to guarantee that the
4881-
* new task ends up on its list.
4850+
* Adds the task to the list running through its css_set if necessary and
4851+
* call the subsystem fork() callbacks. Has to be after the task is
4852+
* visible on the task list in case we race with the first call to
4853+
* cgroup_iter_start() - to guarantee that the new task ends up on its
4854+
* list.
48824855
*/
48834856
void cgroup_post_fork(struct task_struct *child)
48844857
{
4858+
int i;
4859+
48854860
/*
48864861
* use_task_css_set_links is set to 1 before we walk the tasklist
48874862
* under the tasklist_lock and we read it here after we added the child
@@ -4910,7 +4885,30 @@ void cgroup_post_fork(struct task_struct *child)
49104885
}
49114886
write_unlock(&css_set_lock);
49124887
}
4888+
4889+
/*
4890+
* Call ss->fork(). This must happen after @child is linked on
4891+
* css_set; otherwise, @child might change state between ->fork()
4892+
* and addition to css_set.
4893+
*/
4894+
if (need_forkexit_callback) {
4895+
for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4896+
struct cgroup_subsys *ss = subsys[i];
4897+
4898+
/*
4899+
* fork/exit callbacks are supported only for
4900+
* builtin subsystems and we don't need further
4901+
* synchronization as they never go away.
4902+
*/
4903+
if (!ss || ss->module)
4904+
continue;
4905+
4906+
if (ss->fork)
4907+
ss->fork(child);
4908+
}
4909+
}
49134910
}
4911+
49144912
/**
49154913
* cgroup_exit - detach cgroup from exiting task
49164914
* @tsk: pointer to task_struct of exiting process

‎kernel/cgroup_freezer.c‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,31 +186,26 @@ static void freezer_fork(struct task_struct *task)
186186
{
187187
struct freezer *freezer;
188188

189-
/*
190-
* No lock is needed, since the task isn't on tasklist yet,
191-
* so it can't be moved to another cgroup, which means the
192-
* freezer won't be removed and will be valid during this
193-
* function call. Nevertheless, apply RCU read-side critical
194-
* section to suppress RCU lockdep false positives.
195-
*/
196189
rcu_read_lock();
197190
freezer = task_freezer(task);
198-
rcu_read_unlock();
199191

200192
/*
201193
* The root cgroup is non-freezable, so we can skip the
202194
* following check.
203195
*/
204196
if (!freezer->css.cgroup->parent)
205-
return;
197+
goto out;
206198

207199
spin_lock_irq(&freezer->lock);
208200
BUG_ON(freezer->state == CGROUP_FROZEN);
209201

210202
/* Locking avoids race with FREEZING -> THAWED transitions. */
211203
if (freezer->state == CGROUP_FREEZING)
212204
freeze_task(task);
205+
213206
spin_unlock_irq(&freezer->lock);
207+
out:
208+
rcu_read_unlock();
214209
}
215210

216211
/*

‎kernel/fork.c‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,6 @@ static struct task_struct *copy_process(unsigned long clone_flags,
11351135
{
11361136
int retval;
11371137
struct task_struct *p;
1138-
int cgroup_callbacks_done = 0;
11391138

11401139
if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
11411140
return ERR_PTR(-EINVAL);
@@ -1393,12 +1392,6 @@ static struct task_struct *copy_process(unsigned long clone_flags,
13931392
INIT_LIST_HEAD(&p->thread_group);
13941393
p->task_works = NULL;
13951394

1396-
/* Now that the task is set up, run cgroup callbacks if
1397-
* necessary. We need to run them before the task is visible
1398-
* on the tasklist. */
1399-
cgroup_fork_callbacks(p);
1400-
cgroup_callbacks_done = 1;
1401-
14021395
/* Need tasklist lock for parent etc handling! */
14031396
write_lock_irq(&tasklist_lock);
14041397

@@ -1503,7 +1496,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
15031496
#endif
15041497
if (clone_flags & CLONE_THREAD)
15051498
threadgroup_change_end(current);
1506-
cgroup_exit(p, cgroup_callbacks_done);
1499+
cgroup_exit(p, 0);
15071500
delayacct_tsk_free(p);
15081501
module_put(task_thread_info(p)->exec_domain->module);
15091502
bad_fork_cleanup_count:

0 commit comments

Comments
 (0)