[PATCH v3 52/57] perf: Simplify perf_event_init_context()

From: Peter Zijlstra
Date: Mon Jun 12 2023 - 06:00:29 EST



Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
---
kernel/events/core.c | 29 +++++++++++------------------
1 file changed, 11 insertions(+), 18 deletions(-)

--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -1450,6 +1450,10 @@ static void perf_unpin_context(struct pe
raw_spin_unlock_irqrestore(&ctx->lock, flags);
}

+DEFINE_CLASS(pin_task_ctx, struct perf_event_context *,
+ if (_T) { perf_unpin_context(_T); put_ctx(_T); },
+ perf_pin_task_context(task), struct task_struct *task)
+
/*
* Update the record of the current time in a context.
*/
@@ -7939,18 +7943,13 @@ static void perf_event_addr_filters_exec

void perf_event_exec(void)
{
- struct perf_event_context *ctx;
-
- ctx = perf_pin_task_context(current);
+ CLASS(pin_task_ctx, ctx)(current);
if (!ctx)
return;

perf_event_enable_on_exec(ctx);
perf_event_remove_on_exec(ctx);
perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL, true);
-
- perf_unpin_context(ctx);
- put_ctx(ctx);
}

struct remote_output {
@@ -13226,8 +13225,7 @@ inherit_task_group(struct perf_event *ev
*/
static int perf_event_init_context(struct task_struct *child, u64 clone_flags)
{
- struct perf_event_context *child_ctx, *parent_ctx;
- struct perf_event_context *cloned_ctx;
+ struct perf_event_context *child_ctx, *cloned_ctx;
struct perf_event *event;
struct task_struct *parent = current;
int inherited_all = 1;
@@ -13241,7 +13239,7 @@ static int perf_event_init_context(struc
* If the parent's context is a clone, pin it so it won't get
* swapped under us.
*/
- parent_ctx = perf_pin_task_context(parent);
+ CLASS(pin_task_ctx, parent_ctx)(parent);
if (!parent_ctx)
return 0;

@@ -13256,7 +13254,7 @@ static int perf_event_init_context(struc
* Lock the parent list. No need to lock the child - not PID
* hashed yet and not running, so nobody can access it.
*/
- mutex_lock(&parent_ctx->mutex);
+ guard(mutex)(&parent_ctx->mutex);

/*
* We dont have to disable NMIs - we are only looking at
@@ -13266,7 +13264,7 @@ static int perf_event_init_context(struc
ret = inherit_task_group(event, parent, parent_ctx,
child, clone_flags, &inherited_all);
if (ret)
- goto out_unlock;
+ return ret;
}

/*
@@ -13282,7 +13280,7 @@ static int perf_event_init_context(struc
ret = inherit_task_group(event, parent, parent_ctx,
child, clone_flags, &inherited_all);
if (ret)
- goto out_unlock;
+ return ret;
}

raw_spin_lock_irqsave(&parent_ctx->lock, flags);
@@ -13310,13 +13308,8 @@ static int perf_event_init_context(struc
}

raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
-out_unlock:
- mutex_unlock(&parent_ctx->mutex);
-
- perf_unpin_context(parent_ctx);
- put_ctx(parent_ctx);

- return ret;
+ return 0;
}

/*