Skip to content

Commit b75bfc4

Browse files
Peter Zijlstrabwhacks
authored andcommitted
sched: Fix race in task_group()
commit 8323f26 upstream Stefan reported a crash on a kernel before a3e5d10 ("sched: Don't call task_group() too many times in set_task_rq()"), he found the reason to be that the multiple task_group() invocations in set_task_rq() returned different values. Looking at all that I found a lack of serialization and plain wrong comments. The below tries to fix it using an extra pointer which is updated under the appropriate scheduler locks. Its not pretty, but I can't really see another way given how all the cgroup stuff works. Reported-and-tested-by: Stefan Bader <stefan.bader@canonical.com> Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Link: http://lkml.kernel.org/r/1340364965.18025.71.camel@twins Signed-off-by: Ingo Molnar <mingo@kernel.org> (backported to previous file names and layout) Signed-off-by: Stefan Bader <stefan.bader@canonical.com> Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
1 parent 46159f6 commit b75bfc4

3 files changed

Lines changed: 33 additions & 16 deletions

File tree

‎include/linux/init_task.h‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,17 @@ extern struct group_info init_groups;
124124

125125
extern struct cred init_cred;
126126

127+
extern struct task_group root_task_group;
128+
129+
#ifdef CONFIG_CGROUP_SCHED
130+
# define INIT_CGROUP_SCHED(tsk) \
131+
.sched_task_group = &root_task_group,
132+
#else
133+
# define INIT_CGROUP_SCHED(tsk)
134+
#endif
135+
127136
#ifdef CONFIG_PERF_EVENTS
128-
# define INIT_PERF_EVENTS(tsk) \
137+
# define INIT_PERF_EVENTS(tsk) \
129138
.perf_event_mutex = \
130139
__MUTEX_INITIALIZER(tsk.perf_event_mutex), \
131140
.perf_event_list = LIST_HEAD_INIT(tsk.perf_event_list),
@@ -162,6 +171,7 @@ extern struct cred init_cred;
162171
}, \
163172
.tasks = LIST_HEAD_INIT(tsk.tasks), \
164173
INIT_PUSHABLE_TASKS(tsk) \
174+
INIT_CGROUP_SCHED(tsk) \
165175
.ptraced = LIST_HEAD_INIT(tsk.ptraced), \
166176
.ptrace_entry = LIST_HEAD_INIT(tsk.ptrace_entry), \
167177
.real_parent = &tsk, \

‎include/linux/sched.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,9 @@ struct task_struct {
12361236
const struct sched_class *sched_class;
12371237
struct sched_entity se;
12381238
struct sched_rt_entity rt;
1239+
#ifdef CONFIG_CGROUP_SCHED
1240+
struct task_group *sched_task_group;
1241+
#endif
12391242

12401243
#ifdef CONFIG_PREEMPT_NOTIFIERS
12411244
/* list of struct preempt_notifier: */
@@ -2646,7 +2649,7 @@ extern int sched_group_set_rt_period(struct task_group *tg,
26462649
extern long sched_group_rt_period(struct task_group *tg);
26472650
extern int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk);
26482651
#endif
2649-
#endif
2652+
#endif /* CONFIG_CGROUP_SCHED */
26502653

26512654
extern int task_can_switch_user(struct user_struct *up,
26522655
struct task_struct *tsk);

‎kernel/sched.c‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -746,22 +746,19 @@ static inline int cpu_of(struct rq *rq)
746746
/*
747747
* Return the group to which this tasks belongs.
748748
*
749-
* We use task_subsys_state_check() and extend the RCU verification with
750-
* pi->lock and rq->lock because cpu_cgroup_attach() holds those locks for each
751-
* task it moves into the cgroup. Therefore by holding either of those locks,
752-
* we pin the task to the current cgroup.
749+
* We cannot use task_subsys_state() and friends because the cgroup
750+
* subsystem changes that value before the cgroup_subsys::attach() method
751+
* is called, therefore we cannot pin it and might observe the wrong value.
752+
*
753+
* The same is true for autogroup's p->signal->autogroup->tg, the autogroup
754+
* core changes this before calling sched_move_task().
755+
*
756+
* Instead we use a 'copy' which is updated from sched_move_task() while
757+
* holding both task_struct::pi_lock and rq::lock.
753758
*/
754759
static inline struct task_group *task_group(struct task_struct *p)
755760
{
756-
struct task_group *tg;
757-
struct cgroup_subsys_state *css;
758-
759-
css = task_subsys_state_check(p, cpu_cgroup_subsys_id,
760-
lockdep_is_held(&p->pi_lock) ||
761-
lockdep_is_held(&task_rq(p)->lock));
762-
tg = container_of(css, struct task_group, css);
763-
764-
return autogroup_task_group(p, tg);
761+
return p->sched_task_group;
765762
}
766763

767764
/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
@@ -2372,7 +2369,7 @@ void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
23722369
* a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks.
23732370
*
23742371
* sched_move_task() holds both and thus holding either pins the cgroup,
2375-
* see set_task_rq().
2372+
* see task_group().
23762373
*
23772374
* Furthermore, all task_rq users should acquire both locks, see
23782375
* task_rq_lock().
@@ -8952,6 +8949,7 @@ void sched_destroy_group(struct task_group *tg)
89528949
*/
89538950
void sched_move_task(struct task_struct *tsk)
89548951
{
8952+
struct task_group *tg;
89558953
int on_rq, running;
89568954
unsigned long flags;
89578955
struct rq *rq;
@@ -8966,6 +8964,12 @@ void sched_move_task(struct task_struct *tsk)
89668964
if (unlikely(running))
89678965
tsk->sched_class->put_prev_task(rq, tsk);
89688966

8967+
tg = container_of(task_subsys_state_check(tsk, cpu_cgroup_subsys_id,
8968+
lockdep_is_held(&tsk->sighand->siglock)),
8969+
struct task_group, css);
8970+
tg = autogroup_task_group(tsk, tg);
8971+
tsk->sched_task_group = tg;
8972+
89698973
#ifdef CONFIG_FAIR_GROUP_SCHED
89708974
if (tsk->sched_class->task_move_group)
89718975
tsk->sched_class->task_move_group(tsk, on_rq);

0 commit comments

Comments
 (0)