Skip to content

Commit 614e4c4

Browse files
Stephane Eranianingomolnar
authored andcommitted
perf/core: Robustify the perf_cgroup_from_task() RCU checks
This patch reinforces the lockdep checks performed by perf_cgroup_from_tsk() by passing the perf_event_context whenever possible. It is okay to not hold the RCU read lock when we know we hold the ctx->lock. This patch makes sure this property holds. In some functions, such as perf_cgroup_sched_in(), we do not pass the context because we are sure we are holding the RCU read lock. 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-3-git-send-email-eranian@google.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent ddaaf4e commit 614e4c4

3 files changed

Lines changed: 18 additions & 10 deletions

File tree

‎arch/x86/kernel/cpu/perf_event_intel_cqm.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ static bool __match_event(struct perf_event *a, struct perf_event *b)
298298
static inline struct perf_cgroup *event_to_cgroup(struct perf_event *event)
299299
{
300300
if (event->attach_state & PERF_ATTACH_TASK)
301-
return perf_cgroup_from_task(event->hw.target);
301+
return perf_cgroup_from_task(event->hw.target, event->ctx);
302302

303303
return event->cgrp;
304304
}

‎include/linux/perf_event.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,9 +697,11 @@ struct perf_cgroup {
697697
* if there is no cgroup event for the current CPU context.
698698
*/
699699
static inline struct perf_cgroup *
700-
perf_cgroup_from_task(struct task_struct *task)
700+
perf_cgroup_from_task(struct task_struct *task, struct perf_event_context *ctx)
701701
{
702-
return container_of(task_css(task, perf_event_cgrp_id),
702+
return container_of(task_css_check(task, perf_event_cgrp_id,
703+
ctx ? lockdep_is_held(&ctx->lock)
704+
: true),
703705
struct perf_cgroup, css);
704706
}
705707
#endif /* CONFIG_CGROUP_PERF */

‎kernel/events/core.c‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ static inline void update_cgrp_time_from_event(struct perf_event *event)
435435
if (!is_cgroup_event(event))
436436
return;
437437

438-
cgrp = perf_cgroup_from_task(current);
438+
cgrp = perf_cgroup_from_task(current, event->ctx);
439439
/*
440440
* Do not update time when cgroup is not active
441441
*/
@@ -458,7 +458,7 @@ perf_cgroup_set_timestamp(struct task_struct *task,
458458
if (!task || !ctx->nr_cgroups)
459459
return;
460460

461-
cgrp = perf_cgroup_from_task(task);
461+
cgrp = perf_cgroup_from_task(task, ctx);
462462
info = this_cpu_ptr(cgrp->info);
463463
info->timestamp = ctx->timestamp;
464464
}
@@ -521,8 +521,10 @@ static void perf_cgroup_switch(struct task_struct *task, int mode)
521521
* set cgrp before ctxsw in to allow
522522
* event_filter_match() to not have to pass
523523
* task around
524+
* we pass the cpuctx->ctx to perf_cgroup_from_task()
525+
* because cgorup events are only per-cpu
524526
*/
525-
cpuctx->cgrp = perf_cgroup_from_task(task);
527+
cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx);
526528
cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
527529
}
528530
perf_pmu_enable(cpuctx->ctx.pmu);
@@ -542,15 +544,17 @@ static inline void perf_cgroup_sched_out(struct task_struct *task,
542544
rcu_read_lock();
543545
/*
544546
* we come here when we know perf_cgroup_events > 0
547+
* we do not need to pass the ctx here because we know
548+
* we are holding the rcu lock
545549
*/
546-
cgrp1 = perf_cgroup_from_task(task);
550+
cgrp1 = perf_cgroup_from_task(task, NULL);
547551

548552
/*
549553
* next is NULL when called from perf_event_enable_on_exec()
550554
* that will systematically cause a cgroup_switch()
551555
*/
552556
if (next)
553-
cgrp2 = perf_cgroup_from_task(next);
557+
cgrp2 = perf_cgroup_from_task(next, NULL);
554558

555559
/*
556560
* only schedule out current cgroup events if we know
@@ -572,11 +576,13 @@ static inline void perf_cgroup_sched_in(struct task_struct *prev,
572576
rcu_read_lock();
573577
/*
574578
* we come here when we know perf_cgroup_events > 0
579+
* we do not need to pass the ctx here because we know
580+
* we are holding the rcu lock
575581
*/
576-
cgrp1 = perf_cgroup_from_task(task);
582+
cgrp1 = perf_cgroup_from_task(task, NULL);
577583

578584
/* prev can never be NULL */
579-
cgrp2 = perf_cgroup_from_task(prev);
585+
cgrp2 = perf_cgroup_from_task(prev, NULL);
580586

581587
/*
582588
* only need to schedule in cgroup events if we are changing

0 commit comments

Comments
 (0)