From nobody Wed Dec 31 15:00:59 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C55EC4332F for ; Thu, 2 Nov 2023 15:33:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234875AbjKBPdG (ORCPT ); Thu, 2 Nov 2023 11:33:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343650AbjKBPdA (ORCPT ); Thu, 2 Nov 2023 11:33:00 -0400 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 71EA2182 for ; Thu, 2 Nov 2023 08:32:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-Id:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=gJ0cDE/AWPvauqfBTyp4RE5Jcb6F1MLuevN6bVvwMR4=; b=E9j9R2VdzYpvPH7rwdpNxxMJZL Mc9Uj6K4UmJMbMKg2oBTNF6i6wR6vGVAtWk2bocVeGTSyak6Q+0BB7ILnNi0m3iO37oLJpqIg4XUt La3ST7Gss5YpASywS+2izd9ykg95moqR9MXiDwf2fm1k/qImKpzMOvG28ngIkGF5qweLTe2SWmhU8 v7vJ96pRbrcuHvG+YWR/0Zo5UnaZbbW9iB/gAk3zta5iAMsK3cEB1CWVf8d0AUpzjZ+0f12XihsUw GqDigxj3W1t5lYUcVEHpHs5MwElyg6fUyJZd9H4Snik/8eRKymF61DRXGMNnaW0HsiEzmpo6a/y2J Dx9Xz99A==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1qyZgV-006gQM-26; Thu, 02 Nov 2023 15:32:41 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 0) id 49CE63015B5; Thu, 2 Nov 2023 16:32:39 +0100 (CET) Message-Id: <20231102152018.177137916@infradead.org> User-Agent: quilt/0.65 Date: Thu, 02 Nov 2023 16:09:23 +0100 From: Peter Zijlstra To: mingo@kernel.org Cc: linux-kernel@vger.kernel.org, peterz@infradead.org, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, namhyung@kernel.org, irogers@google.com, adrian.hunter@intel.com Subject: [PATCH 04/13] perf: Simplify event_function*() References: <20231102150919.719936610@infradead.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use guards to reduce gotos and simplify control flow. Signed-off-by: Peter Zijlstra (Intel) --- kernel/events/core.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -214,6 +214,19 @@ struct event_function_struct { void *data; }; =20 +typedef struct { + struct perf_cpu_context *cpuctx; + struct perf_event_context *ctx; +} class_perf_ctx_lock_t; + +static inline void class_perf_ctx_lock_destructor(class_perf_ctx_lock_t *_= T) +{ perf_ctx_unlock(_T->cpuctx, _T->ctx); } + +static inline class_perf_ctx_lock_t +class_perf_ctx_lock_constructor(struct perf_cpu_context *cpuctx, + struct perf_event_context *ctx) +{ perf_ctx_lock(cpuctx, ctx); return (class_perf_ctx_lock_t){ cpuctx, ctx = }; } + static int event_function(void *info) { struct event_function_struct *efs =3D info; @@ -221,20 +234,17 @@ static int event_function(void *info) struct perf_event_context *ctx =3D event->ctx; struct perf_cpu_context *cpuctx =3D this_cpu_ptr(&perf_cpu_context); struct perf_event_context *task_ctx =3D cpuctx->task_ctx; - int ret =3D 0; =20 lockdep_assert_irqs_disabled(); + guard(perf_ctx_lock)(cpuctx, task_ctx); =20 - perf_ctx_lock(cpuctx, task_ctx); /* * Since we do the IPI call without holding ctx->lock things can have * changed, double check we hit the task we set out to hit. */ if (ctx->task) { - if (ctx->task !=3D current) { - ret =3D -ESRCH; - goto unlock; - } + if (ctx->task !=3D current) + return -ESRCH; =20 /* * We only use event_function_call() on established contexts, @@ -254,10 +264,8 @@ static int event_function(void *info) } =20 efs->func(event, cpuctx, ctx, efs->data); -unlock: - perf_ctx_unlock(cpuctx, task_ctx); =20 - return ret; + return 0; } =20 static void event_function_call(struct perf_event *event, event_f func, vo= id *data) @@ -329,11 +337,11 @@ static void event_function_local(struct task_ctx =3D ctx; } =20 - perf_ctx_lock(cpuctx, task_ctx); + guard(perf_ctx_lock)(cpuctx, task_ctx); =20 task =3D ctx->task; if (task =3D=3D TASK_TOMBSTONE) - goto unlock; + return; =20 if (task) { /* @@ -343,18 +351,16 @@ static void event_function_local(struct */ if (ctx->is_active) { if (WARN_ON_ONCE(task !=3D current)) - goto unlock; + return; =20 if (WARN_ON_ONCE(cpuctx->task_ctx !=3D ctx)) - goto unlock; + return; } } else { WARN_ON_ONCE(&cpuctx->ctx !=3D ctx); } =20 func(event, cpuctx, ctx, data); -unlock: - perf_ctx_unlock(cpuctx, task_ctx); } =20 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\