Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp805927pxj; Thu, 3 Jun 2021 21:30:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzb/OgpoLyC2l8FlE5C/3nPUjZ0LSGvEpYUN1y+MPKlE64iQakBSs/IA6f4wG/St+JNlRvw X-Received: by 2002:a17:906:17c4:: with SMTP id u4mr2379227eje.481.1622781012185; Thu, 03 Jun 2021 21:30:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622781012; cv=none; d=google.com; s=arc-20160816; b=sBiOv0S9bi8kimDD0KMF7xLBCBYjI+6kBrfCSrIwYYLhSgJcUDxN/4ElkoCBZIZ3Sk cIgw79vbKfiKx991XFjz2q7IL0D5ghhsZgNXwOq92vRuzrqjFPYyjPCd/cXiwpvrXoUR 24np7Szdhi02i+foiN8XpDmG9KpL56Gu2gNKqyvG/3QaG3Ri9X1TCt/AZEbAT7aZ3JQO E5YgW59pOYMqHLx7VIK4qwYvQk6VPYRwmLLzKqIjshj4KE929i+sqkZXhh9bl0j4ri0L KRG+3fuNMGP11FkUi8FB+B6PQCGfQwfKBIVdUN+EemepluMuz5Rd4FrPWE1n3GkA0eA7 9mGA== 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=7UWd9c23gbJeqKN+x05xqP4SMfjB2/ClR1agz1P2O+o=; b=r3rlaPO7wvc31akzVRHQy9PQ1fAWAN1elg4/0wcuh3kTWDWn9HpwRO6r8ivJWu7tN6 BHk1TeFgKjnw0R3ORblmZ6EEi/gBIMnA8YvgjJcA7mX/0xMxWEy3deHL4MPbNWaCTQSQ uzL6P6t+h/5mQRF2GHilnobs5X7I+a9ko4ReBef1MK07O5yintcooPwD+6hLkJktn4AU QTckv74pxLpyWjYteeHD0XUw0s8IKCDYZ3I6igXCOwCrLiMCjuB3eKz9fAzeG6QB6njk 6js2TNprCG8+EDrdVYSvc+XP5Li+2gBLXG3XTbJXch6nJbsczJuzS6VVHaTMP1UqibEB uZeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=TiAICjH6; 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 j12si3744434ejy.26.2021.06.03.21.29.48; Thu, 03 Jun 2021 21:30:12 -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=TiAICjH6; 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 S229817AbhFDE2v (ORCPT + 99 others); Fri, 4 Jun 2021 00:28:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57242 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229452AbhFDE2v (ORCPT ); Fri, 4 Jun 2021 00:28:51 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7B9FFC06174A for ; Thu, 3 Jun 2021 21:26:54 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id z8so7863173wrp.12 for ; Thu, 03 Jun 2021 21:26:54 -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=7UWd9c23gbJeqKN+x05xqP4SMfjB2/ClR1agz1P2O+o=; b=TiAICjH6rsZIbGZ0XdCOAiZXNFUhWsTiGyyMuob+l8IGv9uNIN+jjSUy2wya5b37kH 8Lhl3dCFLzyC7SUYg1zjfdUrvJAI/mNDqguToa/NpRNQJMI4RNs1CQ/rWwXGeuXyDVuM bnV8VaIKZX0l6jxDyxlSiis1p/SqkDDAhXdFsadWl/iiw/AvvSWBMv0+MjTkO69rL8hS +cuN2Q3ULZWR3j02VXRXEh9N36nKVI69dkNYKY4qT0Vo91S6QDwXe9ZoRLI0LQHQZUQl rTFbUgivH8wU6CECRAVmwg8JdygB/aBXkIrTukFKkqYcM3uiqolcaQ6wQhVVWDh9z7At dM0g== 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=7UWd9c23gbJeqKN+x05xqP4SMfjB2/ClR1agz1P2O+o=; b=V1MD+ufx727BnEbgUUBHP7kGp0QQVsBlCiNDftWwzrgG05DQ4seTle1rjMetg7lQq0 s6Cg+m7830fNIN9yAX1DUq5uFRZlk82B5j+Xn0ebGdBznFTlJ78ZcNhxmc4W8FqmaZDu XxqaObN4dIRfblIKGPxikeBkjPH/plxrkxgz6WXFUxY1sMS2NsHWwa0y67/2VZmzTd+I 5xJKiMbRRqrPFZwxoNw9EHWUO0X9HjSrl4F2dzipnMGTLP/D5tBnyDPt1I79bhFgawBD 1odWG1S6GIX+Y5LKLqGUGE6JrYnSraKWFW17rrioL0DHGbsk+OGFNV2uLy2B1YetKNPf 9o+Q== X-Gm-Message-State: AOAM531LHAYoC92zbuwz40o1t2ctiAV9Fv7h7bOxBceN8YbQWvmGXgWb +QgIIQk9TxyCh1mmMNTMOlK/QWsp7YyGvZsb777LVw== X-Received: by 2002:a5d:4903:: with SMTP id x3mr1531894wrq.376.1622780812951; Thu, 03 Jun 2021 21:26:52 -0700 (PDT) MIME-Version: 1.0 References: <20210602231052.317048-1-rickyman7@gmail.com> In-Reply-To: <20210602231052.317048-1-rickyman7@gmail.com> From: Ian Rogers Date: Thu, 3 Jun 2021 21:26:40 -0700 Message-ID: Subject: Re: [PATCH] perf ksymbol: fix memory leak: decrease refcount of map and dso To: Riccardo Mancini Cc: Arnaldo Carvalho de Melo , Namhyung Kim , Peter Zijlstra , Ingo Molnar , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Andi Kleen , Tommi Rantala , linux-perf-users , LKML Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jun 2, 2021 at 4:15 PM Riccardo Mancini wrote: > > ASan reported a memory leak of BPF-related ksymbols map and dso. > The leak is caused by refcount never reaching 0, due to missing > __put calls in the function machine__process_ksymbol_register. > Once the dso is inserted in map, dso__put should be called, > since map__new2 has increased its refcount to 2. > The same thing applies for the map when it's inserted into the > rb-tree in maps (maps__insert increases the refcount to 2). > > $ sudo ./perf record -- sleep 5 > [ perf record: Woken up 1 times to write data ] > [ perf record: Captured and wrote 0.025 MB perf.data (8 samples) ] > > ================================================================= > ==297735==ERROR: LeakSanitizer: detected memory leaks > > Direct leak of 6992 byte(s) in 19 object(s) allocated from: > #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7) > #1 0x8e4e53 in map__new2 /home/user/linux/tools/perf/util/map.c:216:20 > #2 0x8cf68c in machine__process_ksymbol_register /home/user/linux/tools/perf/util/machine.c:778:10 > [...] > > Indirect leak of 8702 byte(s) in 19 object(s) allocated from: > #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7) > #1 0x8728d7 in dso__new_id /home/user/linux/tools/perf/util/dso.c:1256:20 > #2 0x872015 in dso__new /home/user/linux/tools/perf/util/dso.c:1295:9 > #3 0x8cf623 in machine__process_ksymbol_register /home/user/linux/tools/perf/util/machine.c:774:21 > [...] > > Indirect leak of 1520 byte(s) in 19 object(s) allocated from: > #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7) > #1 0x87b3da in symbol__new /home/user/linux/tools/perf/util/symbol.c:269:23 > #2 0x888954 in map__process_kallsym_symbol /home/user/linux/tools/perf/util/symbol.c:710:8 > [...] > > Indirect leak of 1406 byte(s) in 19 object(s) allocated from: > #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7) > #1 0x87b3da in symbol__new /home/user/linux/tools/perf/util/symbol.c:269:23 > #2 0x8cfbd8 in machine__process_ksymbol_register /home/user/linux/tools/perf/util/machine.c:803:8 > [...] > > Signed-off-by: Riccardo Mancini > --- > tools/perf/util/machine.c | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c > index 3ff4936a15a42..d5937778875e1 100644 > --- a/tools/perf/util/machine.c > +++ b/tools/perf/util/machine.c > @@ -776,6 +776,7 @@ static int machine__process_ksymbol_register(struct machine *machine, > if (dso) { > dso->kernel = DSO_SPACE__KERNEL; > map = map__new2(0, dso); > + dso__put(dso); Will this cause 2 puts if the map allocation fails? Perhaps this should be "if (map) dso__put(dso);". Thanks, Ian > } > > if (!dso || !map) { > @@ -792,6 +793,7 @@ static int machine__process_ksymbol_register(struct machine *machine, > map->start = event->ksymbol.addr; > map->end = map->start + event->ksymbol.len; > maps__insert(&machine->kmaps, map); > + map__put(map); > dso__set_loaded(dso); > > if (is_bpf_image(event->ksymbol.name)) { > -- > 2.31.1 >