Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1515747pxj; Sat, 12 Jun 2021 10:49:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyPIOH0uG8RcZ6aI6qN+7BIEfopCYaOPvnXaDgZ+TD3EsDMGNqsSpeeerXjmeowe6wNbeee X-Received: by 2002:a17:906:f6cb:: with SMTP id jo11mr8628384ejb.439.1623520171607; Sat, 12 Jun 2021 10:49:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623520171; cv=none; d=google.com; s=arc-20160816; b=tg9Si5DJxyHllWrA1Ko0pZCGBKa1FbvmfOi5m3YXtlqud9ZyEykVMc95Chy1kQjFzW IIbcW8nch/1PImSyUbcCQZZ+seRvZHEnepeG6SKSED4uIuk3M5SSTYS5eXp+ch1evDRf CKXokSVijhzHB0GLowfaLz8oGhrNonutfyBxiQxEbHgR9c+R5he6PZrAuEKU5qHSYAkG txTEDznN0z4mbrO/6BNiEspGAItG8cQQLn/VRy/5Lf6DM9QL9eiGgVsyi8bLKbcWL7li dI62Chkolfq/5gf90XCY+diEOJdqPeSf+eqxANlJllpYAW1P7U4D8VaJG7mKdfYpGJv7 9c7w== 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:date:subject:cc:to:from :dkim-signature; bh=75mouw3BFiq70jHhBUAkpXJpGq2eH3Umtp3XMvh/izM=; b=Wf2UVluiUtABvJiGSUuY0jy09k2O/IyBBlx9Ahvgch3tO3wNCP3+nQbJvXmcb1ywLp jzZCV3bk42Y00p/YnHs2i+aV3laoSt0jIJ9fWC04Z5QyiWjRGENP7a4VAdQJuQYf0gBF uw3hM1tyAAVNtBCktDiSXRodIP7cGCKFZqKoRnbOTqOAPATGfYGHl7FbBJMsI3VMk+9C Y3StuNO4xAIV3Zu7fRReZ/WIhXy+u7xCaMIpat+DWERT/uM+txddDsW4MwiWddMdHkGx Y292IqNdy80E4YNsVeaz6Zx5dlVHyJIfH+gg6VF7yfQrMttGZPWSFbuLCGm9Eur18zSg LX6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=QNeaRBMl; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b19si5568677ede.547.2021.06.12.10.49.08; Sat, 12 Jun 2021 10:49: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=@gmail.com header.s=20161025 header.b=QNeaRBMl; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229874AbhFLRsQ (ORCPT + 99 others); Sat, 12 Jun 2021 13:48:16 -0400 Received: from mail-wm1-f43.google.com ([209.85.128.43]:37422 "EHLO mail-wm1-f43.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229572AbhFLRsP (ORCPT ); Sat, 12 Jun 2021 13:48:15 -0400 Received: by mail-wm1-f43.google.com with SMTP id f16-20020a05600c1550b02901b00c1be4abso10421609wmg.2; Sat, 12 Jun 2021 10:46:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=75mouw3BFiq70jHhBUAkpXJpGq2eH3Umtp3XMvh/izM=; b=QNeaRBMlgWsTqyJVbHik85vIm39QVMvDWe2pzmH92pwSCPQsg9sAC1eXoatsz+iorl MDaGxjKWEloqO6GYMQdI+SujA+qv2lE509kL6xKKZmzNlvrMBNCwbDGO6EYvy/4H94Sr Fy9wopCfHlvNf/l/Fe/99J/65IC+dJveMwypjb6De2G6jG9BZO1Y2rqOQz0OfMyentNT gKzettbSYW1F/11jx36sGqfQ0buSMcOTO4SF2dub8VaDegkmloLJlEWZz6F7enGk/2PS JZ9pdY4VwlrAa5dUxmyzvHyrcTWVpOSn4+esS6Yoaehyta1FqSfu3WDO99dg+HPqLMv1 q1DQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=75mouw3BFiq70jHhBUAkpXJpGq2eH3Umtp3XMvh/izM=; b=jH6Se/zNct+/BLt/mjEc4DrW6CuUJkHuPbBTAMphyT9NYWQ4eWOAQIeU6Tj7+LmWBG ikjLg8Z3K9EWiaDBF6rG82Darym/KL3ldcGaU+IukuvTfIE5pNNZGPHC8sfXeJL1p501 PX/0+Blk0q2nP7fjx4VO7sysl2V1YKLNiwxlRiOByvkrUIZ+Z72iZ9mNZxZwvvKjxf6m VbBaEfytRG/oLHa/d6g/TakAlxadnRyIPQUNHOjTtmvuvoiW/gZc8CJ6SPpKrIxHcWBv JU6vwUI5X4EuWWOARVj5c7eiapVy5wsq9V73nC3wDCSE4Ji8YE7Xs/BxBBHoROf7uonx AWNQ== X-Gm-Message-State: AOAM530XDXVJSoO3j0AjjR0rTPaRB98G/gPNz2wBJ1hk5+uiuC2X+pBa ooCX1tERR6y7OAN6ZK7IAKM= X-Received: by 2002:a05:600c:3555:: with SMTP id i21mr11037678wmq.51.1623519900818; Sat, 12 Jun 2021 10:45:00 -0700 (PDT) Received: from honeypot.lan ([2001:b07:6456:fd99:ced0:db1c:53e1:191e]) by smtp.googlemail.com with ESMTPSA id z11sm11147366wrs.7.2021.06.12.10.44.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 12 Jun 2021 10:44:59 -0700 (PDT) From: Riccardo Mancini To: Arnaldo Carvalho de Melo Cc: Namhyung Kim , Ian Rogers , Riccardo Mancini , Peter Zijlstra , Ingo Molnar , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Andi Kleen , Tommi Rantala , Jiapeng Chong , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] perf ksymbol: fix memory leak: decrease refcount of map and dso Date: Sat, 12 Jun 2021 19:37:48 +0200 Message-Id: <20210612173751.188582-1-rickyman7@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ASan reported a memory leak of BPF-related ksymbols map and dso. The leak is caused by refount never reaching 0, due to missing __put calls in the function machine__process_ksymbol_register. Once the dso is inserted in the map, dso__put should be called (map__new2 increases the refcount to 2). The same thing applies for the map when it's inserted into 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 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 3ff4936a15a4..da19be7da284 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -776,10 +776,10 @@ static int machine__process_ksymbol_register(struct machine *machine, if (dso) { dso->kernel = DSO_SPACE__KERNEL; map = map__new2(0, dso); + dso__put(dso); } if (!dso || !map) { - dso__put(dso); return -ENOMEM; } @@ -792,6 +792,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.23.0