Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3886127rwb; Tue, 16 Aug 2022 10:19:14 -0700 (PDT) X-Google-Smtp-Source: AA6agR5JrW4UBqHaVsbsbT6HLoUjW5Sk7pmbF0cbnh8bv50yzhYi9ANpj7748voBb5jM4+JaG7IV X-Received: by 2002:a17:907:2da4:b0:731:2772:59d9 with SMTP id gt36-20020a1709072da400b00731277259d9mr14161808ejc.751.1660670354390; Tue, 16 Aug 2022 10:19:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660670354; cv=none; d=google.com; s=arc-20160816; b=mwUQB4Eaa75gmWEjcyuih4G7QOiGDmg4cSrW2zepvB3s3SCNV62q55aSM3+lXUaVja S8iUUrWZ/fyKKBS9z9PakjMtTJDfnFohhgZGG3vxFJ506QliirwPdmNzidGCG4Iu4+3i GRcwBKJjtkOJGQc34EimjOKyAYmEFefoqdUaza30Vvp88VUCGygXSdI116wW82lp0bDK gi0MdSPkWf3ahpL/8zayLNIsYqahKehR8k+CIqQ1BuxCHjbfK3kcuMa0Io/xJ0Do2S72 MbTlb8FtvO/ui8g7MG8ARpuf1wWWlY3YDtrSrwS1HlXfj/nBoGj+uATFXWb1kXecxG3H Qz9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date; bh=wKBhRyAIeGKPpNH2RJmFm3yX/oPmExIEvXeo5+0lRuo=; b=PYjfU+AzfYAm6M4T40BzxE8CokBpqFHC++MuF3HnjLXpw3PqfGZTphOv19LARE17c0 oiGqh1P7wYZ9eB/KoqirRpWThL5QEZ4Aq5NuIb/hT9Y/XFhBByk73qvojOL9Rn3pg550 sMrq23XLYap3+0DdIccI5FUv06I6S44q1QkeSFmexvjozmHem/zcgtepdrXAKks5G5Eq aIpcVF1oRJUU/C+XDpaCRd+ZvDfHb3hN37rQ0kJo5EVJ3teEMb00lVghpEq9iR85sYRS JzqpTUYABj8q5q6xs536oq8x8072YbWhqoMEVCMPYe9/KfNmQsYkDkFgGvbA1WYgZbrf KNnA== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dt16-20020a170907729000b00708f4d9dbdbsi10798254ejc.79.2022.08.16.10.18.40; Tue, 16 Aug 2022 10:19:14 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236322AbiHPQZ4 (ORCPT + 99 others); Tue, 16 Aug 2022 12:25:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236237AbiHPQZz (ORCPT ); Tue, 16 Aug 2022 12:25:55 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33AD6EE0D for ; Tue, 16 Aug 2022 09:25:54 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C4E1B6124E for ; Tue, 16 Aug 2022 16:25:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 350C4C433D6; Tue, 16 Aug 2022 16:25:52 +0000 (UTC) Date: Tue, 16 Aug 2022 12:25:59 -0400 From: Steven Rostedt To: Krister Johansen Cc: Ingo Molnar , Masami Hiramatsu , David Reaver , linux-kernel@vger.kernel.org, Jiri Olsa , Peter Zijlstra , Frederic Weisbecker Subject: Re: [PATCH 1/1] tracing: fix a WARN from trace_event_dyn_put_ref Message-ID: <20220816122559.17869abc@gandalf.local.home> In-Reply-To: <4e43a4eece5f382d1636397fb3c0208f2afe81fc.1660347763.git.kjlx@templeofstupid.com> References: <4e43a4eece5f382d1636397fb3c0208f2afe81fc.1660347763.git.kjlx@templeofstupid.com> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-6.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham 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 [ Added the authors of this code to the Cc ] On Fri, 12 Aug 2022 17:02:20 -0700 Krister Johansen wrote: > The code in perf_trace_init takes a reference on a trace_event_call that is > looked up as part of the function call. If perf_trace_event_int fails, > however, perf_trace_event_unreg can decrement that refcount from underneath > perf_trace_init. This means that in some failure cases, perf_trace_init > can trigger the WARN in trace_dynevent.c which attempts to guard against > zero reference counts going negative. > > The author can reproduce this problem readily by running perf record in a > loop against a series of uprobes with no other users. Killing the record > process before it can finish its setup is enough to trigger this warn > within a few seconds. > > This patch leaves the behavior in perf_trace_event_unreg unchanged, but > moves most of the code in that function to perf_trace_event_cleanup. The > unreg function retains the ability to drop the refcount on the tp_event, > but cleanup does not. This modification is based upon the observation that > all of the other callers of perf_trace_event_init don't bother with > manipulating a reference count on the tp_events that they create. For > those callers, the trace_event_put_ref was already a no-op. > > Signed-off-by: Krister Johansen > Reviewed-by: David Reaver > Fixes: 1d18538e6a092 "tracing: Have dynamic events have a ref counter" > CC: stable@vger.kernel.org # 5.15, 5.18, 5.19 > --- > kernel/trace/trace_event_perf.c | 14 ++++++++++---- > 1 file changed, 10 insertions(+), 4 deletions(-) > > diff --git a/kernel/trace/trace_event_perf.c b/kernel/trace/trace_event_perf.c > index a114549720d6..7762bfd268cd 100644 > --- a/kernel/trace/trace_event_perf.c > +++ b/kernel/trace/trace_event_perf.c > @@ -151,13 +151,13 @@ static int perf_trace_event_reg(struct trace_event_call *tp_event, > return ret; > } > > -static void perf_trace_event_unreg(struct perf_event *p_event) > +static void perf_trace_event_cleanup(struct perf_event *p_event) > { > struct trace_event_call *tp_event = p_event->tp_event; > int i; > > if (--tp_event->perf_refcount > 0) > - goto out; > + return; > > tp_event->class->reg(tp_event, TRACE_REG_PERF_UNREGISTER, NULL); > > @@ -176,7 +176,13 @@ static void perf_trace_event_unreg(struct perf_event *p_event) > perf_trace_buf[i] = NULL; > } > } > -out: > +} > + > +static void perf_trace_event_unreg(struct perf_event *p_event) > +{ > + struct trace_event_call *tp_event = p_event->tp_event; > + > + perf_trace_event_cleanup(p_event); > trace_event_put_ref(tp_event); > } > > @@ -207,7 +213,7 @@ static int perf_trace_event_init(struct trace_event_call *tp_event, > > ret = perf_trace_event_open(p_event); > if (ret) { > - perf_trace_event_unreg(p_event); > + perf_trace_event_cleanup(p_event); The only problem I have with this patch is the loss of symmetry. Where perf_trace_event_reg() returns successful, so unreg() should be the function you call on failure. Since perf_trace_event_reg() is only called from perf_trace_event_init() let's move the perf_trace_event_open() into the perf_trace_event_reg() and have the unreg still do the clean up on failure. This way we keep the symmetry between *_reg() and *_unreg(). And then the init function will not have to do any clean up, and can just end with: return perf_trace_event_reg(tp_event, p_event); Thanks, -- Steve > return ret; > } >