Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4860951pxj; Tue, 22 Jun 2021 09:34:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx5rUSR8i5IeEpauhTOYc9dUIjJgBrUNkysMLsxID33IJYRMQ2Tq1Wm1nIFBRyyBaIqUgjq X-Received: by 2002:a5e:9602:: with SMTP id a2mr3642605ioq.146.1624379671537; Tue, 22 Jun 2021 09:34:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624379671; cv=none; d=google.com; s=arc-20160816; b=O76PZRxqhpOElXGT2itIcryLiERKxPCwAuIHxKRDVKl5Ki8X2ZMuBaNOkDtNA91Drd RwS9ckaKBiRfrBHjhfjaxKBtItNEWamJ9K9/ZJAwRF1M0GQc9Yny9kDd72uLRFRZsNjf 5U/lB1rtpP1xDWDKjvCIiZlPP61+usJ0k613P2iErSPhSkasLHd+wUS8Bm5nPyDRyYCW J8lN6l9ssbjtpBAo3K8Q9vkjk88yWkNVQJ1xEROQ7m6fPQuVuh581FqpCRHNXL8ubkp+ q6K0swhVv5o7hU/E2PTt4XTPWiC/moaNDrY8GvEM4Ko7OxlR9ii18BuRAIv1b9imDBha 4Aaw== 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=inSC13iBgpJglxK3ELtc1IM/WTbt8WqbVGB4cdlDbJM=; b=lsfcwrBxpzuA2Jm7QiA3+5NSpRgKEIhQbwdoJaPYFqM0JgDXgQUhyOBq5BUY7zcJrU UXUj9Y5pdnTEu7ZC0TM63R2Bx6M3tb8MtIv0pUopAJZ8yp79o+B+o4hD8QWsPJIFQylV 8N/xMRB6L2CEAuwQzp8v2ybCmeIMTa+4ykqgQZiDM5gD6Jbame+NVGZkoVZV26xkm5Sm CytlBAIIrwq8EmvzJfYsINTM8/ljZo0+S2BLHZ23S4O7VupJLshC/WwjRSEqtdQbXnsL K+RN/Spz/d948DUL0Zj2C9KffkQG+FtmZuXH84f9/wMBek+BCBGTrxld9lIRkT8Nc9pu kMJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=oiz6ZRUU; 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 m19si25038302jat.118.2021.06.22.09.34.17; Tue, 22 Jun 2021 09:34:31 -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=oiz6ZRUU; 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 S229948AbhFVQfx (ORCPT + 99 others); Tue, 22 Jun 2021 12:35:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229501AbhFVQfw (ORCPT ); Tue, 22 Jun 2021 12:35:52 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1211C061574 for ; Tue, 22 Jun 2021 09:33:36 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id c84so13191016wme.5 for ; Tue, 22 Jun 2021 09:33:36 -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=inSC13iBgpJglxK3ELtc1IM/WTbt8WqbVGB4cdlDbJM=; b=oiz6ZRUUe2OJ+WNOR2ml1jBZfrcw3QbLLlWIB0etFcnSwrKkN9izWcKvR72jpttb6V uvR+pLUe7PZ89vMKc3DhnctQ4/VSwpYuRmfQdE0+lrHOIw3+OcmYRYTHTYzxXK3NqObQ xBK2l0ZPcu7YodaZPSa4XaLQ4yTCEDEXuFuSJR35WOtLkxIY206z0eGizlGnIPZHqpAD 0D4mMpoDjWS3SP0/OEWwTcL+STivOH0Oew9Q1h/9Bu/TtePY7plfC4bzULOQHGRRbybo 4Q0MU+8+mOXp4TPYL8bjmufwZAGzck8+Q3Lo0gWsn4mWk3PQv3aHdbLj6zGXuLjojw7z fGpw== 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=inSC13iBgpJglxK3ELtc1IM/WTbt8WqbVGB4cdlDbJM=; b=YnB6k4bvzBluoHSmysVlUJBOr+ec9Mhh7tGHH5iNMgads5/o2Tg3xRmJUFA+SJ7UZp ON4sJjxHyoB4Mtqj6cruZ5Ri7HwvdTubCcP/r+H0/GXr345/eEtVcm1JFHnX5v1wHVjn WP7Rm+/jBPQoXSUIrkFVbm9vN5W5/hTsZU93ejJcl33sSSZZg8+k44jlvXbZwA4jXu/O peDw0sh4zb+8LW3h+F42JkL6/uvY9aPOrHWTgxSt5k33LHxV+hhbAvW7og7Hp35htkJd 76kWEQRyoilKiXs23WghBdpK7703QaKhv5Bwe8ODwLD4o3OXl67gJOf5lzbqT6naGJfO CApQ== X-Gm-Message-State: AOAM530aup7BaWVMe3ebLVZNFfwKmdFA2SrlhrucMBZl1i7XF/F2vZ94 YPiHInRWVGW1F4XWJ8bFHUYMOPQbUO/0qlPLhIV8+w== X-Received: by 2002:a05:600c:1d0a:: with SMTP id l10mr5669289wms.124.1624379615206; Tue, 22 Jun 2021 09:33:35 -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 09:33:23 -0700 Message-ID: Subject: Re: [PATCH 2/2] perf script: delete evlist when deleting session To: Riccardo Mancini Cc: Arnaldo Carvalho de Melo , 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 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); > > > > 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. 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 > > > > >