Re: [PATCH v2] perf/core: Optimize event reschedule for a PMU

From: Peter Zijlstra
Date: Tue Aug 06 2024 - 04:08:19 EST


On Tue, Aug 06, 2024 at 09:56:30AM +0200, Peter Zijlstra wrote:

> Does this help? What would be an easy reproducer?
>
> ---
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index c67fc43fe877..4a04611333d9 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -179,23 +179,27 @@ static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
> }
> }
>
> +static inline void __perf_ctx_unlock(struct perf_event_context *ctx)
> +{
> + /*
> + * If ctx_sched_in() didn't again set any ALL flags, clean up
> + * after ctx_sched_out() by clearing is_active.
> + */
> + if (ctx->is_active & EVENT_FROZEN) {
> + if (!(ctx->is_active & EVENT_ALL))
> + ctx->is_active = 0;
> + else
> + ctx->is_active &= ~EVENT_FROZEN;
> + }
> + raw_spin_unlock(&ctx->lock);
> +}
> +
> static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
> struct perf_event_context *ctx)
> {
> - if (ctx) {
> - /*
> - * If ctx_sched_in() didn't again set any ALL flags, clean up
> - * after ctx_sched_out() by clearing is_active.
> - */
> - if (ctx->is_active & EVENT_FROZEN) {
> - if (!(ctx->is_active & EVENT_ALL))
> - ctx->is_active = 0;
> - else
> - ctx->is_active &= ~EVENT_FROZEN;
> - }
> - raw_spin_unlock(&ctx->lock);
> - }
> - raw_spin_unlock(&cpuctx->ctx.lock);
> + if (ctx)
> + __perf_ctx_unlock(ctx);
> + __perf_ctx_unlock(&cpuctx->ctx.lock);

Obviously that wants to be just: &cpuctx->ctx :-)

> }
>
> #define TASK_TOMBSTONE ((void *)-1L)