Received: by 2002:a89:413:0:b0:1fd:dba5:e537 with SMTP id m19csp691818lqs; Fri, 14 Jun 2024 02:52:30 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX/9WMnvZc/9xOUFDtSsFNOGd5BJooOppuhw5eOrDSjq5PoDN/zhQzs4niyPZv1BPXHeLaSi6XSCM7Dy7eLS1OW5oGGLF6JnHbXlZn3pQ== X-Google-Smtp-Source: AGHT+IFyJqpTc7/k/tn5CIGsN9wei5dc6jBa9XGvSKec16MSXEohWGUI2LvgMrA7CPEWnFrf4Rds X-Received: by 2002:a05:6a20:12c7:b0:1b8:2211:b7e0 with SMTP id adf61e73a8af0-1bae7e04517mr2375533637.20.1718358750711; Fri, 14 Jun 2024 02:52:30 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718358750; cv=pass; d=google.com; s=arc-20160816; b=M0Dv2bMTzJ6fF2e121T1USp1PUfkwgH51DL2FWIirtrBieHi4h7hgRSpKh4coRNi/C 1AOmXGbkutPk6maljhovb0dJ/QFIVeLVCnFQ9h1BZfKDhipk+XF+6S3xGN+wU1b0vLt3 40Jdg1W0OWBG5dEb74IvFhf1+l4xs7aJ/OEc1s1UBEHNt/lDIvAkB7KVY15T1RcFP9Kq ENkbrlEpyUmYAu0lZyJeWrPOk5Bqbhoq310hmu9N9AvlsAZJl/R3HMfzWEaeii3UR8+E IWTzu5sue+zHCQVw4WUc2Ci9SFrXv1cWhKePGno5O5sTSU8GfbhhyYoF2ZkazHD1vRxd gudg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=oi4TGYXP0ngnRdvQippr6Pp5M5V04OW5+2fuk5P6kdg=; fh=Ia4WPfL4lKwClaCEawMFpdkKl78f8AczTgt8dnd3Hvg=; b=G9QGol6H+pjvLdTYaUe2UGI8buDfvcsfvuIwaeR9B0uSiV3DtSaLLAHEXrdiekuK5y slCGcxSzkcQ54BlaHyBoBa+qZ+li5Qahl7+MfbrHl52Ps+j17I7SGbPB6qrBJC2dcfzO BXjdEQVhzF9bJqWcUgpTVXtDiBnVcWgH/EYcYWAB+ap9L6kwd0TeMZVQjLdM4v43Cy8X 7huSs+/LmnfIRNH+hUMP1bmt1B2qdO8lFT3nZ2/EWnncQwvXx3L5KJ1qHMxg/qiP0iCQ M6Kz7irfxm4peBc3yq6J1vBFb1Y3ZE2ciHUIHvgAaDuzoQHae3ivH6104imy9Faexknj fcew==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-214668-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-214668-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id 98e67ed59e1d1-2c4ab2ef7d0si5413836a91.82.2024.06.14.02.52.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 02:52:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-214668-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-214668-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-214668-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 20EA3B25460 for ; Fri, 14 Jun 2024 09:47:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5349E192B74; Fri, 14 Jun 2024 09:46:14 +0000 (UTC) Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EEE25190477; Fri, 14 Jun 2024 09:46:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.190 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718358373; cv=none; b=Li4xsMSJUkjxvCG+0HTNZkqScutCDh0rb8n/sY9C/IqY0QLl+3yM4hNXFSW/C/3UtHtYvjjrKw7NiBMAmK3hrmOckf7heNESEhio1CjhHsLFj3FsgWVCiKUxHIZbYvOUz1wEtqAKShZ/8rWM6/uyzgD8o9j/1HqhiuqTBy5x3MI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718358373; c=relaxed/simple; bh=BUESmvx+Cq07zK8KsbYwgMGM/ijq7/n/y0539SFZ2F4=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=sfVrox+/gF8Tg7XuySMgI4x19hm3iWgLkNcBayjxPRsTrdpjwDwOaTnIVXM9sGsR2yzfilQwAHD+g0NkZy2RxGcKFHKJt+jMqHck1nVHtc0VszUfJHKK54CibBLz3F/en/biqiYPz9+0Fgz1VEkHytDn/7snpKrDDDsPSdrQcQo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.190 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.17]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4W0vSp5Vt1z2Ck6M; Fri, 14 Jun 2024 17:42:18 +0800 (CST) Received: from dggpeml500002.china.huawei.com (unknown [7.185.36.158]) by mail.maildlp.com (Postfix) with ESMTPS id 1A5BF1A0188; Fri, 14 Jun 2024 17:46:08 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpeml500002.china.huawei.com (7.185.36.158) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Fri, 14 Jun 2024 17:46:07 +0800 From: Junhao He To: , , CC: , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH] perf pmus: Fixes always false when compare duplicates aliases Date: Fri, 14 Jun 2024 17:43:18 +0800 Message-ID: <20240614094318.11607-1-hejunhao3@huawei.com> X-Mailer: git-send-email 2.33.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpeml500002.china.huawei.com (7.185.36.158) In the previous loop, all the members in the aliases[j-1] have been freed and set to NULL. But in this loop, the function pmu_alias_is_duplicate() compares the aliases[j] with the aliases[j-1] that has already been disposed, so the function will always return false and duplicate aliases will never be discarded. If we find duplicate aliases, it skips the zfree aliases[j], which is accompanied by a memory leak. We can use the next aliases[j+1] to theck for duplicate aliases to fixes the aliases NULL pointer dereference, then goto zfree code snippet to release it. After patch testing: $ perf list --unit=hisi_sicl,cpa pmu uncore cpa: cpa_p0_rd_dat_32b [Number of read ops transmitted by the P0 port which size is 32 bytes. Unit: hisi_sicl,cpa] cpa_p0_rd_dat_64b [Number of read ops transmitted by the P0 port which size is 64 bytes. Unit: hisi_sicl,cpa] Fixes: c3245d2093c1 ("perf pmu: Abstract alias/event struct") Signed-off-by: Junhao He --- tools/perf/util/pmus.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c index b9b4c5eb5002..6907e3e7fbd1 100644 --- a/tools/perf/util/pmus.c +++ b/tools/perf/util/pmus.c @@ -477,8 +477,8 @@ void perf_pmus__print_pmu_events(const struct print_callbacks *print_cb, void *p qsort(aliases, len, sizeof(struct sevent), cmp_sevent); for (int j = 0; j < len; j++) { /* Skip duplicates */ - if (j > 0 && pmu_alias_is_duplicate(&aliases[j], &aliases[j - 1])) - continue; + if (j < len - 1 && pmu_alias_is_duplicate(&aliases[j], &aliases[j + 1])) + goto free; print_cb->print_event(print_state, aliases[j].pmu_name, @@ -491,6 +491,7 @@ void perf_pmus__print_pmu_events(const struct print_callbacks *print_cb, void *p aliases[j].desc, aliases[j].long_desc, aliases[j].encoding_desc); +free: zfree(&aliases[j].name); zfree(&aliases[j].alias); zfree(&aliases[j].scale_unit); -- 2.33.0