From patchwork Thu Dec 23 07:45:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12698004 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D2BEEC433F5 for ; Thu, 23 Dec 2021 08:05:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=LmxDQhzehypLwZ/uLOKJ0NHbejA6dL//BAIHsjlqwCg=; b=SRPAxzjl/sz00AmYGoMAxY1Ntf akYzK/ujGJUuYO8LPpNMYd1vCoy/rnT2BhVQllRSUkic+aaMXfpVD5SfKfXldODfnAcR31E2woa/a 2QbpuTgpmoDvNRgw+aIaosbSlusX/3KTNa/HaierrpiX4ZD3BlZqesDYHozsL6ABl5mVkMZ/AX0Q4 QEcsrUDgCfLXLFcbghZ/OfgGW5xmxLbH6jghyPncSqWOiy3h+pauI7EKNOj65aa+D3L9N/jqbLSI4 D3z26G549NW61VZly7J8NfxUCq7LQbCGsBuM9ihOKbuMP+rNIgTRQwwvVvj/vpVJpUMZikNlF2w0/ mmuYP6Ag==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n0J4Z-00C69w-Ru; Thu, 23 Dec 2021 08:03:36 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1n0Iod-00Bznf-9P for linux-arm-kernel@lists.infradead.org; Thu, 23 Dec 2021 07:47:08 +0000 Received: by mail-yb1-xb49.google.com with SMTP id e131-20020a25d389000000b005fb5e6eb757so8558852ybf.22 for ; Wed, 22 Dec 2021 23:47:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=J5Uxwz+NmtGEEgwvjL1sqJyX0/ppatr3BMcnhgtMNdI=; b=P+SChYI6l4zrhclWgOqBwpBXlMLUJiQXf8pyYNraVwtkdhs7KWxi4SHRuEs8Dijfgf kB1zINsX/1g3a6FuZyqmfEelCd7gyfYYCTtIpCFy2FxhV/ZOBHyDptAOnFsp6C3HVdhZ WkVyOVFxTKqSjMdxYGMUpMXp9RUfPetQ9mrev4LzHC+t+v611w5UC2P5sKBbddh+XkzM pg59NoJ+Ls4g/9bHvkjqV+KQiVTf3LaRBoNk7NzWdeke3jAlFx+a+gTeOmLXbtjIkBQC mTMKim6DKViJkpsGVDSFhFS1zlCWZl3Kwh+HIU/qNb+pACBdW1rcVEGSg+8ihB9wPwgx RKCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=J5Uxwz+NmtGEEgwvjL1sqJyX0/ppatr3BMcnhgtMNdI=; b=XvCyZX/Y6MjsI/WqZ2ztmPUU7VhjeZ7q8apDyNAsCn2zsgJAtB4cb7kQDqvc+u0T15 PIR8pQtZB1mCG0NVtOV614MyVwjMwiS5+Ef6Qr6WjTzwUgOiL+lmo880BjP0g+afSM2X /9PLwPQIaFf0nfxzTkrWTuxxUmIkeU0KiHT7hHWcobEwc9kz/mppTZbd0En7BFL2KkRx mjmkk8WmJ+CdFkyBIzKY6/mOyuNOe68PIPCE84TF8BfG6MpySidj5MElB9I7TVXkf7fq 2L8UZw4awgvRm6g1R0UZ+TuBto92Cgauh3fhCqKq/Y/L2qjzE4U9fSr6prMf536W30Da z1jw== X-Gm-Message-State: AOAM533r31AmDTdEekJhwcHg1/1jrtcJF7y8tGIEw1jDJnTODX0dFGx4 Bu9kJQ3Ba7a6vxl0MNYxpNOsdHfLn0M5 X-Google-Smtp-Source: ABdhPJzuyhZb4oXRwSxm+ExXDmsSz287nUms3ZLP6USDOSqq/SCugWDf3aY2ieELM5n9Z8Xd11OZuvQJU437 X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:202:4fef:90ff:589d:24ca]) (user=irogers job=sendgmr) by 2002:a25:ac23:: with SMTP id w35mr1680442ybi.341.1640245625867; Wed, 22 Dec 2021 23:47:05 -0800 (PST) Date: Wed, 22 Dec 2021 23:45:16 -0800 In-Reply-To: <20211223074541.3318938-1-irogers@google.com> Message-Id: <20211223074541.3318938-24-irogers@google.com> Mime-Version: 1.0 References: <20211223074541.3318938-1-irogers@google.com> X-Mailer: git-send-email 2.34.1.307.g9b7440fafd-goog Subject: [PATCH v2 23/48] perf stat: Fix memory leak in check_per_pkg From: Ian Rogers To: Andi Kleen , Jiri Olsa , Namhyung Kim , John Garry , Kajol Jain , "Paul A . Clarke" , Arnaldo Carvalho de Melo , Riccardo Mancini , Kan Liang , Peter Zijlstra , Ingo Molnar , Mark Rutland , Alexander Shishkin , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Vineet Singh , James Clark , Mathieu Poirier , Suzuki K Poulose , Mike Leach , Leo Yan , coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org Cc: eranian@google.com, Ian Rogers X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211222_234707_372094_7B1CE26F X-CRM114-Status: GOOD ( 12.79 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org If the key is already present then free the key used for lookup. Found with: $ perf stat -M IO_Read_BW /bin/true ==1749112==ERROR: LeakSanitizer: detected memory leaks Direct leak of 32 byte(s) in 4 object(s) allocated from: #0 0x7f6f6fa7d7cf in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145 #1 0x55acecd9d7a6 in check_per_pkg util/stat.c:343 #2 0x55acecd9d9c5 in process_counter_values util/stat.c:365 #3 0x55acecd9e0ab in process_counter_maps util/stat.c:421 #4 0x55acecd9e292 in perf_stat_process_counter util/stat.c:443 #5 0x55aceca8553e in read_counters ./tools/perf/builtin-stat.c:470 #6 0x55aceca88fe3 in __run_perf_stat ./tools/perf/builtin-stat.c:1023 #7 0x55aceca89146 in run_perf_stat ./tools/perf/builtin-stat.c:1048 #8 0x55aceca90858 in cmd_stat ./tools/perf/builtin-stat.c:2555 #9 0x55acecc05fa5 in run_builtin ./tools/perf/perf.c:313 #10 0x55acecc064fe in handle_internal_command ./tools/perf/perf.c:365 #11 0x55acecc068bb in run_argv ./tools/perf/perf.c:409 #12 0x55acecc070aa in main ./tools/perf/perf.c:539 Reviewed-by: James Clark Signed-off-by: Ian Rogers --- tools/perf/util/stat.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/stat.c b/tools/perf/util/stat.c index 5c24aca0968c..c69b221f5e3e 100644 --- a/tools/perf/util/stat.c +++ b/tools/perf/util/stat.c @@ -345,9 +345,10 @@ static int check_per_pkg(struct evsel *counter, return -ENOMEM; *key = (uint64_t)d << 32 | s; - if (hashmap__find(mask, (void *)key, NULL)) + if (hashmap__find(mask, (void *)key, NULL)) { *skip = true; - else + free(key); + } else ret = hashmap__add(mask, (void *)key, (void *)1); return ret;