Skip to content

Commit ddaaf4e

Browse files
Stephane Eranianingomolnar
authored andcommitted
perf/core: Fix RCU problem with cgroup context switching code
The RCU checker detected RCU violation in the cgroup switching routines perf_cgroup_sched_in() and perf_cgroup_sched_out(). We were dereferencing cgroup from task without holding the RCU lock. Fix this by holding the RCU read lock. We move the locking from perf_cgroup_switch() to avoid double locking. Signed-off-by: Stephane Eranian <eranian@google.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Vince Weaver <vincent.weaver@maine.edu> Cc: edumazet@google.com Link: http://lkml.kernel.org/r/1447322404-10920-2-git-send-email-eranian@google.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 1ec2183 commit ddaaf4e

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

‎kernel/events/core.c‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,6 @@ static void perf_cgroup_switch(struct task_struct *task, int mode)
489489
* we reschedule only in the presence of cgroup
490490
* constrained events.
491491
*/
492-
rcu_read_lock();
493492

494493
list_for_each_entry_rcu(pmu, &pmus, entry) {
495494
cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
@@ -531,8 +530,6 @@ static void perf_cgroup_switch(struct task_struct *task, int mode)
531530
}
532531
}
533532

534-
rcu_read_unlock();
535-
536533
local_irq_restore(flags);
537534
}
538535

@@ -542,6 +539,7 @@ static inline void perf_cgroup_sched_out(struct task_struct *task,
542539
struct perf_cgroup *cgrp1;
543540
struct perf_cgroup *cgrp2 = NULL;
544541

542+
rcu_read_lock();
545543
/*
546544
* we come here when we know perf_cgroup_events > 0
547545
*/
@@ -561,6 +559,8 @@ static inline void perf_cgroup_sched_out(struct task_struct *task,
561559
*/
562560
if (cgrp1 != cgrp2)
563561
perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
562+
563+
rcu_read_unlock();
564564
}
565565

566566
static inline void perf_cgroup_sched_in(struct task_struct *prev,
@@ -569,6 +569,7 @@ static inline void perf_cgroup_sched_in(struct task_struct *prev,
569569
struct perf_cgroup *cgrp1;
570570
struct perf_cgroup *cgrp2 = NULL;
571571

572+
rcu_read_lock();
572573
/*
573574
* we come here when we know perf_cgroup_events > 0
574575
*/
@@ -584,6 +585,8 @@ static inline void perf_cgroup_sched_in(struct task_struct *prev,
584585
*/
585586
if (cgrp1 != cgrp2)
586587
perf_cgroup_switch(task, PERF_CGROUP_SWIN);
588+
589+
rcu_read_unlock();
587590
}
588591

589592
static inline int perf_cgroup_connect(int fd, struct perf_event *event,
@@ -9452,7 +9455,9 @@ static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
94529455
static int __perf_cgroup_move(void *info)
94539456
{
94549457
struct task_struct *task = info;
9458+
rcu_read_lock();
94559459
perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
9460+
rcu_read_unlock();
94569461
return 0;
94579462
}
94589463

0 commit comments

Comments
 (0)