Skip to content

Commit 94902ee

Browse files
Peter Zijlstragregkh
authored andcommitted
perf: Rework perf_event_exit_event()
[ Upstream commit ef54c1a ] Make perf_event_exit_event() more robust, such that we can use it from other contexts. Specifically the up and coming remove_on_exec. For this to work we need to address a few issues. Remove_on_exec will not destroy the entire context, so we cannot rely on TASK_TOMBSTONE to disable event_function_call() and we thus have to use perf_remove_from_context(). When using perf_remove_from_context(), there's two races to consider. The first is against close(), where we can have concurrent tear-down of the event. The second is against child_list iteration, which should not find a half baked event. To address this, teach perf_remove_from_context() to special case !ctx->is_active and about DETACH_CHILD. [ elver@google.com: fix racing parent/child exit in sync_child_event(). ] Signed-off-by: Marco Elver <elver@google.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lkml.kernel.org/r/20210408103605.1676875-2-elver@google.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 2c4de79 commit 94902ee

2 files changed

Lines changed: 80 additions & 63 deletions

File tree

‎include/linux/perf_event.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@ struct swevent_hlist {
607607
#define PERF_ATTACH_TASK_DATA 0x08
608608
#define PERF_ATTACH_ITRACE 0x10
609609
#define PERF_ATTACH_SCHED_CB 0x20
610+
#define PERF_ATTACH_CHILD 0x40
610611

611612
struct perf_cgroup;
612613
struct perf_buffer;

‎kernel/events/core.c‎

Lines changed: 79 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2209,6 +2209,26 @@ static void perf_group_detach(struct perf_event *event)
22092209
perf_event__header_size(leader);
22102210
}
22112211

2212+
static void sync_child_event(struct perf_event *child_event);
2213+
2214+
static void perf_child_detach(struct perf_event *event)
2215+
{
2216+
struct perf_event *parent_event = event->parent;
2217+
2218+
if (!(event->attach_state & PERF_ATTACH_CHILD))
2219+
return;
2220+
2221+
event->attach_state &= ~PERF_ATTACH_CHILD;
2222+
2223+
if (WARN_ON_ONCE(!parent_event))
2224+
return;
2225+
2226+
lockdep_assert_held(&parent_event->child_mutex);
2227+
2228+
sync_child_event(event);
2229+
list_del_init(&event->child_list);
2230+
}
2231+
22122232
static bool is_orphaned_event(struct perf_event *event)
22132233
{
22142234
return event->state == PERF_EVENT_STATE_DEAD;
@@ -2316,6 +2336,7 @@ group_sched_out(struct perf_event *group_event,
23162336
}
23172337

23182338
#define DETACH_GROUP 0x01UL
2339+
#define DETACH_CHILD 0x02UL
23192340

23202341
/*
23212342
* Cross CPU call to remove a performance event
@@ -2339,6 +2360,8 @@ __perf_remove_from_context(struct perf_event *event,
23392360
event_sched_out(event, cpuctx, ctx);
23402361
if (flags & DETACH_GROUP)
23412362
perf_group_detach(event);
2363+
if (flags & DETACH_CHILD)
2364+
perf_child_detach(event);
23422365
list_del_event(event, ctx);
23432366

23442367
if (!ctx->nr_events && ctx->is_active) {
@@ -2367,25 +2390,21 @@ static void perf_remove_from_context(struct perf_event *event, unsigned long fla
23672390

23682391
lockdep_assert_held(&ctx->mutex);
23692392

2370-
event_function_call(event, __perf_remove_from_context, (void *)flags);
2371-
23722393
/*
2373-
* The above event_function_call() can NO-OP when it hits
2374-
* TASK_TOMBSTONE. In that case we must already have been detached
2375-
* from the context (by perf_event_exit_event()) but the grouping
2376-
* might still be in-tact.
2394+
* Because of perf_event_exit_task(), perf_remove_from_context() ought
2395+
* to work in the face of TASK_TOMBSTONE, unlike every other
2396+
* event_function_call() user.
23772397
*/
2378-
WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
2379-
if ((flags & DETACH_GROUP) &&
2380-
(event->attach_state & PERF_ATTACH_GROUP)) {
2381-
/*
2382-
* Since in that case we cannot possibly be scheduled, simply
2383-
* detach now.
2384-
*/
2385-
raw_spin_lock_irq(&ctx->lock);
2386-
perf_group_detach(event);
2398+
raw_spin_lock_irq(&ctx->lock);
2399+
if (!ctx->is_active) {
2400+
__perf_remove_from_context(event, __get_cpu_context(ctx),
2401+
ctx, (void *)flags);
23872402
raw_spin_unlock_irq(&ctx->lock);
2403+
return;
23882404
}
2405+
raw_spin_unlock_irq(&ctx->lock);
2406+
2407+
event_function_call(event, __perf_remove_from_context, (void *)flags);
23892408
}
23902409

23912410
/*
@@ -12249,14 +12268,17 @@ void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
1224912268
}
1225012269
EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
1225112270

12252-
static void sync_child_event(struct perf_event *child_event,
12253-
struct task_struct *child)
12271+
static void sync_child_event(struct perf_event *child_event)
1225412272
{
1225512273
struct perf_event *parent_event = child_event->parent;
1225612274
u64 child_val;
1225712275

12258-
if (child_event->attr.inherit_stat)
12259-
perf_event_read_event(child_event, child);
12276+
if (child_event->attr.inherit_stat) {
12277+
struct task_struct *task = child_event->ctx->task;
12278+
12279+
if (task && task != TASK_TOMBSTONE)
12280+
perf_event_read_event(child_event, task);
12281+
}
1226012282

1226112283
child_val = perf_event_count(child_event);
1226212284

@@ -12271,60 +12293,53 @@ static void sync_child_event(struct perf_event *child_event,
1227112293
}
1227212294

1227312295
static void
12274-
perf_event_exit_event(struct perf_event *child_event,
12275-
struct perf_event_context *child_ctx,
12276-
struct task_struct *child)
12296+
perf_event_exit_event(struct perf_event *event, struct perf_event_context *ctx)
1227712297
{
12278-
struct perf_event *parent_event = child_event->parent;
12298+
struct perf_event *parent_event = event->parent;
12299+
unsigned long detach_flags = 0;
1227912300

12280-
/*
12281-
* Do not destroy the 'original' grouping; because of the context
12282-
* switch optimization the original events could've ended up in a
12283-
* random child task.
12284-
*
12285-
* If we were to destroy the original group, all group related
12286-
* operations would cease to function properly after this random
12287-
* child dies.
12288-
*
12289-
* Do destroy all inherited groups, we don't care about those
12290-
* and being thorough is better.
12291-
*/
12292-
raw_spin_lock_irq(&child_ctx->lock);
12293-
WARN_ON_ONCE(child_ctx->is_active);
12301+
if (parent_event) {
12302+
/*
12303+
* Do not destroy the 'original' grouping; because of the
12304+
* context switch optimization the original events could've
12305+
* ended up in a random child task.
12306+
*
12307+
* If we were to destroy the original group, all group related
12308+
* operations would cease to function properly after this
12309+
* random child dies.
12310+
*
12311+
* Do destroy all inherited groups, we don't care about those
12312+
* and being thorough is better.
12313+
*/
12314+
detach_flags = DETACH_GROUP | DETACH_CHILD;
12315+
mutex_lock(&parent_event->child_mutex);
12316+
}
1229412317

12295-
if (parent_event)
12296-
perf_group_detach(child_event);
12297-
list_del_event(child_event, child_ctx);
12298-
perf_event_set_state(child_event, PERF_EVENT_STATE_EXIT); /* is_event_hup() */
12299-
raw_spin_unlock_irq(&child_ctx->lock);
12318+
perf_remove_from_context(event, detach_flags);
12319+
12320+
raw_spin_lock_irq(&ctx->lock);
12321+
if (event->state > PERF_EVENT_STATE_EXIT)
12322+
perf_event_set_state(event, PERF_EVENT_STATE_EXIT);
12323+
raw_spin_unlock_irq(&ctx->lock);
1230012324

1230112325
/*
12302-
* Parent events are governed by their filedesc, retain them.
12326+
* Child events can be freed.
1230312327
*/
12304-
if (!parent_event) {
12305-
perf_event_wakeup(child_event);
12328+
if (parent_event) {
12329+
mutex_unlock(&parent_event->child_mutex);
12330+
/*
12331+
* Kick perf_poll() for is_event_hup();
12332+
*/
12333+
perf_event_wakeup(parent_event);
12334+
free_event(event);
12335+
put_event(parent_event);
1230612336
return;
1230712337
}
12308-
/*
12309-
* Child events can be cleaned up.
12310-
*/
12311-
12312-
sync_child_event(child_event, child);
1231312338

1231412339
/*
12315-
* Remove this event from the parent's list
12316-
*/
12317-
WARN_ON_ONCE(parent_event->ctx->parent_ctx);
12318-
mutex_lock(&parent_event->child_mutex);
12319-
list_del_init(&child_event->child_list);
12320-
mutex_unlock(&parent_event->child_mutex);
12321-
12322-
/*
12323-
* Kick perf_poll() for is_event_hup().
12340+
* Parent events are governed by their filedesc, retain them.
1232412341
*/
12325-
perf_event_wakeup(parent_event);
12326-
free_event(child_event);
12327-
put_event(parent_event);
12342+
perf_event_wakeup(event);
1232812343
}
1232912344

1233012345
static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
@@ -12381,7 +12396,7 @@ static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
1238112396
perf_event_task(child, child_ctx, 0);
1238212397

1238312398
list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
12384-
perf_event_exit_event(child_event, child_ctx, child);
12399+
perf_event_exit_event(child_event, child_ctx);
1238512400

1238612401
mutex_unlock(&child_ctx->mutex);
1238712402

@@ -12641,6 +12656,7 @@ inherit_event(struct perf_event *parent_event,
1264112656
*/
1264212657
raw_spin_lock_irqsave(&child_ctx->lock, flags);
1264312658
add_event_to_ctx(child_event, child_ctx);
12659+
child_event->attach_state |= PERF_ATTACH_CHILD;
1264412660
raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
1264512661

1264612662
/*

0 commit comments

Comments
 (0)