Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp784323rwd; Mon, 12 Jun 2023 23:42:11 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7V+eqZ+hYkzPj84tqJduXLI+EZG6EFF8uOPhcM+D9RZQ/HQp9oPKuOeK3M2LS1cHYUN0DD X-Received: by 2002:a05:6808:1187:b0:39c:429f:972e with SMTP id j7-20020a056808118700b0039c429f972emr7165716oil.6.1686638531041; Mon, 12 Jun 2023 23:42:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686638531; cv=none; d=google.com; s=arc-20160816; b=dJuib2fMCQWZLLvGggnKR50upVVxDKMwMVeRIp91u8RiahMOE5NZoqlWPICmN9X+hZ 24VnjoVEtTNVwxQTK1Ik5Dzn4KtOSQd7NpKk9liOUNJfbTxzDNpODeLlsRcT83QbNOPc W/dP9Cc51+M/DEEj1YEbtJKRK9q6DzzbX8ENe3wR8j+YQsCrPTF3zfmLXqWmz8EJXpSC 7P+BKJ4bajZl0rrOLbwfAADop/59VDWck1KjbNSRvvnc3zIW0VEJ70Lkigw/rWRo2y8W 1gFtorccx9Qd9aGyINJm2T+p+gUAl4ybHXLRzoKwfjBlAuErlmF7vr5r+DvsmNrNGjYZ /xfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=jUAKQgtDQGyOjoFAu+eSqNnBvj/spc7BWrU1wp/VwJw=; b=oNkUeRzUuYsugKZB6CzMdVOcidVUi24RDAXzz9RIT6TDuhxNOe8TaKK/bCVexQ16TS Xtncc5ZRoovmKLKBoYZt1BaIWxvfW0LXY7tL+WoK7OCe4UJ+0ItaClMG1VjwlvRC9t1u 5gLlcjEzIatZ/i+tdgeBoiQbW3kS0UV04r2sILzWNy9/BOUivFIf/hYvi/Wfqso6SWid ApRZbkTtv9GmQ7xThat5P+B0x2wEl0EIMkDIkM6m/PMUe266Plv9mxtcHdl3RhLaol/b VjFQx0nR0+o6ww2ijFfqyeJM38cnVDlkHHtTCKQHFbfG/KAZYdxH8tHyYNgurbs+1jIy BnzA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k64-20020a638443000000b0054fbd7dd962si179463pgd.260.2023.06.12.23.41.59; Mon, 12 Jun 2023 23:42:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239873AbjFMF4W convert rfc822-to-8bit (ORCPT + 99 others); Tue, 13 Jun 2023 01:56:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234474AbjFMF4U (ORCPT ); Tue, 13 Jun 2023 01:56:20 -0400 Received: from mail-vk1-f170.google.com (mail-vk1-f170.google.com [209.85.221.170]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26E9618E; Mon, 12 Jun 2023 22:56:19 -0700 (PDT) Received: by mail-vk1-f170.google.com with SMTP id 71dfb90a1353d-4652fcb2ac1so2100964e0c.1; Mon, 12 Jun 2023 22:56:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686635778; x=1689227778; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CXoP/SfXeEJ/3wkZUjDn660T9VLgrFL+TBUwgZAoObU=; b=iqLgDpZLQwdyissvPRLHjcy3JmFLoxoZlVg6GNaG/3XM1VcZQG8XM7fsRYdsJ9qe3x h6dYWzAGUqVJpGGxy2rbb5eLFrkr4TdRt68ARWnvpbwNZ88kaoogpzWG5vKw28m5Zc2z j2MW6uNU91YIjggpLap+bCd6gZdInnhL5yD2S0cTNsIot0GQxYSRY85L9P723AWjNF2+ s0LXlBa3Cr+AqwVuue2V1AvKXF5bpxnM06mpVMq6gdjGkddC60p5cwJ7iu5H0eW9F22t Uk8B6Ec/O2Psjc9g8xA3kaKFFxdODwQyeZjpjwEIkg/w4uQD4aVcyD34lwhR+l/ZbSB8 FWLw== X-Gm-Message-State: AC+VfDyY3m5uxPLsxL/yO1LqtMgVoIFFUql31wF6aalpcZtw0GY5Rk2Z nLa94QzqGF2YkvD6BzoWLzCysoxWm1jhdhXimWE= X-Received: by 2002:a1f:4386:0:b0:45d:5914:80b with SMTP id q128-20020a1f4386000000b0045d5914080bmr5911210vka.6.1686635778061; Mon, 12 Jun 2023 22:56:18 -0700 (PDT) MIME-Version: 1.0 References: <20230612090713.652690195@infradead.org> <20230612093539.371360635@infradead.org> In-Reply-To: <20230612093539.371360635@infradead.org> From: Namhyung Kim Date: Mon, 12 Jun 2023 22:56:06 -0700 Message-ID: Subject: Re: [PATCH v3 26/57] perf: Simplify event_function*() To: Peter Zijlstra Cc: torvalds@linux-foundation.org, keescook@chromium.org, gregkh@linuxfoundation.org, pbonzini@redhat.com, masahiroy@kernel.org, nathan@kernel.org, ndesaulniers@google.com, nicolas@fjasle.eu, catalin.marinas@arm.com, will@kernel.org, vkoul@kernel.org, trix@redhat.com, ojeda@kernel.org, mingo@redhat.com, longman@redhat.com, boqun.feng@gmail.com, dennis@kernel.org, tj@kernel.org, cl@linux.com, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, bristot@redhat.com, vschneid@redhat.com, paulmck@kernel.org, frederic@kernel.org, quic_neeraju@quicinc.com, joel@joelfernandes.org, josh@joshtriplett.org, mathieu.desnoyers@efficios.com, jiangshanlai@gmail.com, rientjes@google.com, vbabka@suse.cz, roman.gushchin@linux.dev, 42.hyeyoo@gmail.com, apw@canonical.com, joe@perches.com, dwaipayanray1@gmail.com, lukas.bulwahn@gmail.com, john.johansen@canonical.com, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, dmaengine@vger.kernel.org, llvm@lists.linux.dev, linux-perf-users@vger.kernel.org, rcu@vger.kernel.org, linux-security-module@vger.kernel.org, tglx@linutronix.de, ravi.bangoria@amd.com, error27@gmail.com, luc.vanoostenryck@gmail.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Peter, On Mon, Jun 12, 2023 at 2:39 AM Peter Zijlstra wrote: > > Use guards to reduce gotos and simplify control flow. > > Signed-off-by: Peter Zijlstra (Intel) > --- > kernel/events/core.c | 39 ++++++++++++++++++++++++++------------- > 1 file changed, 26 insertions(+), 13 deletions(-) > > --- a/kernel/events/core.c > +++ b/kernel/events/core.c > @@ -214,6 +214,25 @@ struct event_function_struct { > void *data; > }; > > +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) > +{ > + if (_T->cpuctx) > + perf_ctx_unlock(_T->cpuctx, _T->ctx); Shouldn't it be called unconditionally? Thanks, Namhyung > +} > + > +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 = info; > @@ -224,17 +243,15 @@ static int event_function(void *info) > int ret = 0; > > lockdep_assert_irqs_disabled(); > + guard(perf_ctx_lock)(cpuctx, task_ctx); > > - 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 != current) { > - ret = -ESRCH; > - goto unlock; > - } > + if (ctx->task != current) > + return -ESRCH; > > /* > * We only use event_function_call() on established contexts, > @@ -254,8 +271,6 @@ static int event_function(void *info) > } > > efs->func(event, cpuctx, ctx, efs->data); > -unlock: > - perf_ctx_unlock(cpuctx, task_ctx); > > return ret; > } > @@ -329,11 +344,11 @@ static void event_function_local(struct > task_ctx = ctx; > } > > - perf_ctx_lock(cpuctx, task_ctx); > + guard(perf_ctx_lock)(cpuctx, task_ctx); > > task = ctx->task; > if (task == TASK_TOMBSTONE) > - goto unlock; > + return; > > if (task) { > /* > @@ -343,18 +358,16 @@ static void event_function_local(struct > */ > if (ctx->is_active) { > if (WARN_ON_ONCE(task != current)) > - goto unlock; > + return; > > if (WARN_ON_ONCE(cpuctx->task_ctx != ctx)) > - goto unlock; > + return; > } > } else { > WARN_ON_ONCE(&cpuctx->ctx != ctx); > } > > func(event, cpuctx, ctx, data); > -unlock: > - perf_ctx_unlock(cpuctx, task_ctx); > } > > #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\ > >