Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4949252pxj; Tue, 22 Jun 2021 11:30:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzwo4YEUtyhMd9w5z4ouCrxFiLb81cDTA99BFUPEe4LDpSjQb0KADeFgHF+L6vPfIoQADUf X-Received: by 2002:a17:907:d06:: with SMTP id gn6mr5468379ejc.447.1624386608107; Tue, 22 Jun 2021 11:30:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624386608; cv=none; d=google.com; s=arc-20160816; b=AVU8Djhv1YuC0gpTfJfJBzpQYjpjNLhDLIaGgpydGPI6yTA48RTPp28qm67JXddDEW ag5oE+acT2L0ho9IqV0c+x7LB1Z5Oybbl3sP9cUf/TS3ZU1st5AiMQ20pJGhsypef4ek ov9V56AGNCwosT9Q+hqcDT+cHElhmcXM7A0EtMLyHbmEejzS75jnC2dnYikm0hR9Kez5 qj9CmbCZZCMteLrWcHdpIiMFnEjDf9AGH8qU9T8kM5YhpkJ57BjWO+6thP3yHnU9q0wh WtBv6+daNCSUJSyoOTXpRQeXf45yghPh3+7IL40I01FmmNmdDP1VMTV0BOOsD0zc6teK d4tQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=7ZRESarJDPkY6qjYc9HPNqJRL29Tb0Y5iNDm2R43Wwg=; b=jf1t6yu9BPnBuqq+HYKg/OuIbbowDJarAtwnz4cbHGBCr4luQxFfEdtwjWr1PGmmKN CU/YHN5aSiywN0sIj0wOcko3BZCeHyWfaUUnSOacxOJobYjEioHHOWlaL2Obx8NLAT+m q9bCSZhwV2e4kY9S7VUIMD5lVh1kXjZYMmV2XX3qdDNDzXXXVEXUC4YmM3aNKwyCA+nW KpJID3ByV7gXWdsGzp6klNo6C1PiXq0r0+J8Gilvw+1RkUsL9PIHBQaoiyoEtbcda7Kc nfqbED8cJDGocz0F4GqP6QMQ1yDhAuUylqPPDkbFkuoh0e2nWSalCR2ReovWZLmvqv4L G2iQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=vpYah1Zn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gv22si14721145ejc.345.2021.06.22.11.29.44; Tue, 22 Jun 2021 11:30:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=vpYah1Zn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230338AbhFVSay (ORCPT + 99 others); Tue, 22 Jun 2021 14:30:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44576 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230146AbhFVSax (ORCPT ); Tue, 22 Jun 2021 14:30:53 -0400 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83ED0C061756 for ; Tue, 22 Jun 2021 11:28:33 -0700 (PDT) Received: by mail-wm1-x336.google.com with SMTP id l7-20020a05600c1d07b02901b0e2ebd6deso2884589wms.1 for ; Tue, 22 Jun 2021 11:28:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=7ZRESarJDPkY6qjYc9HPNqJRL29Tb0Y5iNDm2R43Wwg=; b=vpYah1ZnJP4oxpRimY85t1NwjzRpYYu413Kk5vt8w+ZsIJV+ejZxFfo2eEdKTDf6M3 /meOClvKBeJfcCsndJz6CqHNBk8K3pi9aXjkmBzr+CnnRPORR2QTHqxlHMt98fAH0vAI cXh7BmZVQ51L3nAFemhS4EOxezqNlNRn2Z/S0Ng+3DpHCfd5voNw3VmxMInGTPpWR1o7 l6IIeK3XgOtfQ1caEznQyFNxSCzkZQY5mpcreiHFXr9wQwaH5VVkoxgJel7wYJ9srWxh xAPGrR3lNd9kDvcXjEsY5HzSQcLgrUsDM6e8vSQ/yXMjpBeCjDfRQ0fv2RBMFA1LAuSq Iaiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=7ZRESarJDPkY6qjYc9HPNqJRL29Tb0Y5iNDm2R43Wwg=; b=NuSGj2aQ9qLO5ZOW1NAOErCHVFE7v4ppLwMY1R/dF4v9Zt+MPy3VxnAWLUowyIaPMT vu3H/8Y2WEd5v7YZQE21vyr40YonwEIymZWxWKXSSUzvMeJsi9s6ij9iIUb+e0Vz2OVn GUl++hp9y56TC/VCInfwn23TIShK6ZKKEJkHP4dsKkSA4yaGSRCFB3cTvnp1Nikwr37Z fT9+shrqamynRLgZ2JDqYLwlScbW6mmkcQr2F1ituu34UuDcPg3DETmyHQYhpLsPtZyH ZDjIiSK0WUTLeqWrmOobGJ8UyG2SIarFyraxObE3a1HhSZgVto+pLWRh+YJd0N1L1Dqp 27dQ== X-Gm-Message-State: AOAM533ao0b97WvpyRrLPUNWuu2Sm7TAUQBbtFgQkkubRvbE/9+P0wUw VAobjVfT5Xf5u5F8o5NEZbAXboql/k0GTKTo5V3VeA== X-Received: by 2002:a05:600c:a01:: with SMTP id z1mr6203292wmp.77.1624386511920; Tue, 22 Jun 2021 11:28:31 -0700 (PDT) MIME-Version: 1.0 References: <20210621234317.235545-1-rickyman7@gmail.com> <20210621234317.235545-3-rickyman7@gmail.com> In-Reply-To: From: Ian Rogers Date: Tue, 22 Jun 2021 11:28:19 -0700 Message-ID: Subject: Re: [PATCH 2/2] perf script: delete evlist when deleting session To: Arnaldo Carvalho de Melo Cc: Riccardo Mancini , Namhyung Kim , Peter Zijlstra , Ingo Molnar , Mark Rutland , Alexander Shishkin , Jiri Olsa , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jun 22, 2021 at 10:42 AM Arnaldo Carvalho de Melo wrote: > > Em Tue, Jun 22, 2021 at 09:33:23AM -0700, Ian Rogers escreveu: > > On Tue, Jun 22, 2021 at 12:44 AM Riccardo Mancini wrote: > > > > > > Hi, > > > > > > thanks for your comments. > > > > > > On Mon, 2021-06-21 at 22:14 -0700, Ian Rogers wrote: > > > > On Mon, Jun 21, 2021 at 4:44 PM Riccardo Mancini wrote: > > > > > > > > > > ASan reports a memory leak related to session->evlist never being deleted. > > > > > The evlist member is not deleted in perf_session__delete, so it should be > > > > > deleted separately. > > > > > This patch adds the missing deletion in perf-script. > > > > > > > > > > Signed-off-by: Riccardo Mancini > > > > > --- > > > > > tools/perf/builtin-script.c | 2 +- > > > > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > > > > > > > diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c > > > > > index 1280cbfad4db..635a1d9cfc88 100644 > > > > > --- a/tools/perf/builtin-script.c > > > > > +++ b/tools/perf/builtin-script.c > > > > > @@ -3991,7 +3991,7 @@ int cmd_script(int argc, const char **argv) > > > > > zfree(&script.ptime_range); > > > > > } > > > > > > > > > > - evlist__free_stats(session->evlist); > > > > > > > > Should this be removed? > > > > > > Probably not. I originally thought this was already taken care of by > > > evlist__delete, but it's not. > > > Oddly, this issue is not causing a memory leak in my simple test. > > > > > > > > > > > > + evlist__delete(session->evlist); > > This looks like a bug, if it is a 'session' member, its a session method > that should delete it, probably perf_session__delete(). > > > > > If the perf session "owns" the evlist, would it be cleaner to add this > > > > to perf_session__delete? > > > > > > I thought about that too, but that's not always true. > > > E.g., in perf-record, __cmd_record calls perf_session__delete,then cmd_record > > > calls evlist__delete on rec->evlist, which points to the same location to which > > > session->evlist pointed. > > > > Agreed. I find it hard to understand the ownership properties in the > > perf code. The missing delete is an example of the owner of the evlist > > (the caller) not "knowing" it needed cleaning up. I'd like it if we > > documented things like perf_sessions' evlist to say not owned, user > > must clean up. The makes it unambiguous who has to take > > responsibility. Having things clean up after themselves is of course > > easiest, hence wanting this to be in perf_session__delete. > > This specific case, from just reading the description on this message, > looks just like a bug/thinko. Ack. Definitely worth merging the change. I think this is about the 7th address sanitizer bug Riccardo has fixed. Namhyung, Numfor, Luke and myself have also contributed similar fixes. We set up some fuzz testing on libtraceevent and there are currently 12 issues we've found there. The nice thing with sparse compared to address sanitizer is the compiler will point at the problem, you don't need to trigger an issue in a test. There are some complicated ownership rules in session and also in the reference counting issues that Riccardo has raised, so perhaps there's scope for some more comments or other tidy ups. Thanks, Ian > > Fwiw, I've been reading around things like sparse [1, 2] and Clang's > > similar analysis [3] that people have looked to use like sparse [4]. I > > don't see anything that handles memory allocation lifetimes, but > > perhaps something will feed into C's standards by way of C++ [5]. > > Perhaps people have ideas to rewrite in checked C or Rust :-) > > > > Some thoughts: > > 1) we can't have C++ as we're trying to follow kernel conventions [6] > > 2) we can't annotate code for things like sparse or thread safety > > analysis, as checking for memory errors is out of scope for them, the > > annotations don't exist, etc. > > 3) we can add comments, document the rules around pointers, perhaps > > even invent empty annotations that may one day help with automated > > checking. > > 4) we can try to clean up the ownership model to make bugs less likely. > > > > I've heard concerns on non-kernel projects about annotation litter and > > comments adding to complexity. I think your patch is good, it follows > > the existing conventions. I wonder if we can learn something from the > > fact the code was wrong to make it less likely we have wrong code in > > the future. I'd be interested to hear what others think. > > > > Thanks, > > Ian > > > > [1] https://lore.kernel.org/lkml/Pine.LNX.4.58.0410302005270.28839@ppc970.osdl.org/ > > [2] https://lwn.net/Articles/689907/ > > [3] https://clang.llvm.org/docs/ThreadSafetyAnalysis.html > > [4] https://www.openwall.com/lists/kernel-hardening/2019/05/20/3 > > [5] https://github.com/isocpp/CppCoreGuidelines/blob/master/docs/Lifetime.pdf > > [6] even concatenating a string is error prone in C :-( > > https://lore.kernel.org/lkml/YMzOpgZPJeC2jGKf@kernel.org/ > > > > > Thanks, > > > Riccardo > > > > > > > > > > > Thanks, > > > > Ian > > > > > > > > > perf_session__delete(session); > > > > > > > > > > if (script_started) > > > > > -- > > > > > 2.31.1 > > > > > > > > > > > > > -- > > - Arnaldo