From nobody Fri Nov 29 00:56:27 2024 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DE40D146A83; Wed, 25 Sep 2024 19:53:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727294037; cv=none; b=BuHlfOJTrF5jbDolfpQmHOCtOgbBpzpGRmWzZ4zX2r33I071IOVINMW0VcdFo2B4QeEBdhaojTVGJg7KRo8Emgg0NA4CjJmRhMJUtkk7IYBXsCownnj8hvE4XxYEWMosQ4RWCLM3PeusnPyOmgNdHEObb06gVcv7Aqdqw90NU4E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727294037; c=relaxed/simple; bh=1kP3y9v4x9gt4KRH/ael6EWdQPP4TyqLqmnTHACQqRc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lFATn+tMTVJlrLRvACkvlTJxnXmEqNM9UaGD7g4+ciqH7mqBgdplmwKOl0J/s+O0c+5PEQ+5IrAXLA+ZVrIU9bQCnz4dlz+hlJNpp/LWVf8mxQjOXrH+qAyg9FLrBper5wpKovC2N5JpM5ECq+npzKGkOvwUh/dr3Rn03jq1Tps= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 961BC1576; Wed, 25 Sep 2024 12:54:23 -0700 (PDT) Received: from e132581.cambridge.arm.com (e132581.arm.com [10.2.76.71]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 5D2F63F64C; Wed, 25 Sep 2024 12:53:52 -0700 (PDT) From: Leo Yan To: Arnaldo Carvalho de Melo , Adrian Hunter , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , "Liang, Kan" , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Leo Yan Subject: [PATCH v1 1/5] libperf cpumap: Correct reference count for perf_cpu_map__merge() Date: Wed, 25 Sep 2024 20:53:21 +0100 Message-Id: <20240925195325.426533-2-leo.yan@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240925195325.426533-1-leo.yan@arm.com> References: <20240925195325.426533-1-leo.yan@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The perf_cpu_map__merge() function has two arguments, 'orig' and 'other', and it returns results for three different cases: Case 1: 'other' is a subset of 'orig'. Case 2: 'orig' is a subset of 'other'. Case 3: 'orig' and 'other' are not subsets of each other. The result combinations are: +--------+-------------+-----------+-----------+ | Cases | Result | orig | other | +--------+-------------+-----------+-----------+ | Case1 | orig | No change | No change | +--------+-------------+-----------+-----------+ | Case2 | other | No change | refcnt++ | +--------+-------------+-----------+-----------+ | Case3 | New CPU map | refcnt-- | No change | +--------+-------------+-----------+-----------+ Both Case 1 and Case 3 have a risk of releasing maps unexpectedly. This is because the reference counter operations are not consistent crossing different cases and leads to difficulty for callers handling them. For Case 1, because 'other' is a subset of 'orig', 'orig' is returned as the merging result, but its refcnt is not incremented. This can lead to the map being released repeatedly: struct perf_cpu_map *a =3D perf_cpu_map__new("1,2"); struct perf_cpu_map *b =3D perf_cpu_map__new("2"); /* 'm' and 'a' point to the same CPU map */ struct perf_cpu_map *m =3D perf_cpu_map__merge(a, b); ... perf_cpu_map__put(m); -> Release the map perf_cpu_map__put(b); perf_cpu_map__put(a); -> Release the same merged again For Case 3, it is possible that the CPU map pointed to by 'orig' can be released twice: within the function and outside of it. struct perf_cpu_map *a =3D perf_cpu_map__new("1,2"); struct perf_cpu_map *b =3D perf_cpu_map__new("3"); struct perf_cpu_map *m =3D perf_cpu_map__merge(a, b); `> 'm' is new allocated map. But 'a' has been released in the function. ... perf_cpu_map__put(m); perf_cpu_map__put(b); perf_cpu_map__put(a); -> Release the 'a' map again This commit increases the reference counter if a passed map is returned. For the case of a newly allocated map, it does not change the reference counter for passed maps. Signed-off-by: Leo Yan --- tools/lib/perf/cpumap.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/tools/lib/perf/cpumap.c b/tools/lib/perf/cpumap.c index cae799ad44e1..3f80eade8b1c 100644 --- a/tools/lib/perf/cpumap.c +++ b/tools/lib/perf/cpumap.c @@ -438,9 +438,7 @@ bool perf_cpu_map__is_subset(const struct perf_cpu_map = *a, const struct perf_cpu /* * Merge two cpumaps * - * orig either gets freed and replaced with a new map, or reused - * with no reference count change (similar to "realloc") - * other has its reference count increased. + * Return a new map, or reused with its reference count increased. */ =20 struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, @@ -452,11 +450,9 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_c= pu_map *orig, struct perf_cpu_map *merged; =20 if (perf_cpu_map__is_subset(orig, other)) - return orig; - if (perf_cpu_map__is_subset(other, orig)) { - perf_cpu_map__put(orig); + return perf_cpu_map__get(orig); + if (perf_cpu_map__is_subset(other, orig)) return perf_cpu_map__get(other); - } =20 tmp_len =3D __perf_cpu_map__nr(orig) + __perf_cpu_map__nr(other); tmp_cpus =3D malloc(tmp_len * sizeof(struct perf_cpu)); @@ -483,7 +479,6 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_cp= u_map *orig, =20 merged =3D cpu_map__trim_new(k, tmp_cpus); free(tmp_cpus); - perf_cpu_map__put(orig); return merged; } =20 --=20 2.34.1 From nobody Fri Nov 29 00:56:27 2024 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B3CA31494BB; Wed, 25 Sep 2024 19:53:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727294038; cv=none; b=J/6ns3sSGuRjGjCAL4IjhbMRngV1HvYFJ6Bcbb+T/fc6+Rkzxit5WHtvHLPVRtPbLKZx1EYIFoQLy4Q614aJzohTvdIM4yNyVsT+Z/vhsbXc0yjf083y8cdqlq10l6xO9m/3A2TugEPeTXnBwlarc+meUKnw1ayirnWZwLNl25k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727294038; c=relaxed/simple; bh=nAUhPuhlSPAQZG/JZFwPNxlaDFCicwpSIukdsNOdHeQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Rq5wDXYDY0ztpCsItehgmOg78XG2iZ+rYULPkxWFxgFvzpHX1dYNfAn0ZU15iXbrhfEmncSCj2aQC40zmWsDgBGOraKQUpY4KwaA3C5eMGyCOybNUOGHDVP6buC8pEskWR644r/Q4G1+seWHXnnn8094lhdFTMyziPEpLnfYtNk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 9D19D150C; Wed, 25 Sep 2024 12:54:25 -0700 (PDT) Received: from e132581.cambridge.arm.com (e132581.arm.com [10.2.76.71]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 64D583F64C; Wed, 25 Sep 2024 12:53:54 -0700 (PDT) From: Leo Yan To: Arnaldo Carvalho de Melo , Adrian Hunter , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , "Liang, Kan" , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Leo Yan Subject: [PATCH v1 2/5] perf: Release old CPU maps after merging Date: Wed, 25 Sep 2024 20:53:22 +0100 Message-Id: <20240925195325.426533-3-leo.yan@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240925195325.426533-1-leo.yan@arm.com> References: <20240925195325.426533-1-leo.yan@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" After CPU maps are merged, the old CPU map will not automatically released. This commit adds a new variable to record old CPU map, after merging the new allocated map is returned, and release the old CPU map. Signed-off-by: Leo Yan --- tools/lib/perf/evlist.c | 4 ++++ tools/perf/util/mem-events.c | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/tools/lib/perf/evlist.c b/tools/lib/perf/evlist.c index c6d67fc9e57e..37920e0b0cd6 100644 --- a/tools/lib/perf/evlist.c +++ b/tools/lib/perf/evlist.c @@ -36,6 +36,8 @@ void perf_evlist__init(struct perf_evlist *evlist) static void __perf_evlist__propagate_maps(struct perf_evlist *evlist, struct perf_evsel *evsel) { + struct perf_cpu_map *old_all_cpus; + if (evsel->system_wide) { /* System wide: set the cpu map of the evsel to all online CPUs. */ perf_cpu_map__put(evsel->cpus); @@ -75,7 +77,9 @@ static void __perf_evlist__propagate_maps(struct perf_evl= ist *evlist, evsel->threads =3D perf_thread_map__get(evlist->threads); } =20 + old_all_cpus =3D evlist->all_cpus; evlist->all_cpus =3D perf_cpu_map__merge(evlist->all_cpus, evsel->cpus); + perf_cpu_map__put(old_all_cpus); } =20 static void perf_evlist__propagate_maps(struct perf_evlist *evlist) diff --git a/tools/perf/util/mem-events.c b/tools/perf/util/mem-events.c index 051feb93ed8d..016a1f4adb5d 100644 --- a/tools/perf/util/mem-events.c +++ b/tools/perf/util/mem-events.c @@ -257,7 +257,7 @@ int perf_mem_events__record_args(const char **rec_argv,= int *argv_nr) int i =3D *argv_nr; const char *s; char *copy; - struct perf_cpu_map *cpu_map =3D NULL; + struct perf_cpu_map *cpu_map =3D NULL, *old_cpu_map; =20 while ((pmu =3D perf_pmus__scan_mem(pmu)) !=3D NULL) { for (int j =3D 0; j < PERF_MEM_EVENTS__MAX; j++) { @@ -283,7 +283,9 @@ int perf_mem_events__record_args(const char **rec_argv,= int *argv_nr) rec_argv[i++] =3D "-e"; rec_argv[i++] =3D copy; =20 + old_cpu_map =3D cpu_map; cpu_map =3D perf_cpu_map__merge(cpu_map, pmu->cpus); + perf_cpu_map__put(old_cpu_map); } } =20 --=20 2.34.1 From nobody Fri Nov 29 00:56:27 2024 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B9D8314EC4B; Wed, 25 Sep 2024 19:53:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727294040; cv=none; b=IM5hwuWgIKoXddI6tmn1ZWFWtkm13leYAwPr3duyqC3DGsg8arNLXuoqXKPSOObCSftN+GVWNHZwEz7ZTzt5/jSi4NupRoQFvoNNVuZuU9aVNEn4I5g7CS7tLdZWLpW8w0xA2stA1QuwpJfr+mWYmZ8aWtf9QcsSpJS1n9AmIGE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727294040; c=relaxed/simple; bh=DoemBfODLc0dawppKg7pBI/hDNePdWLlFYwV99vZgeM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Ejp9FBD37aLMHX1D5pXGEIDcK/+NvazgQN7coQDiTaQC3JgMosJmCgMfzgKkqNRaP71Wxls1VJXKLziZG6vxmr3KR+Sm94kaaDHsz2+R/mDZR9X8vqUHvC3onARHp6CLGzG8GyVwXjvP2MMjf6f91sF0IqOBBB47zFKjCdxYfxg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B492B1576; Wed, 25 Sep 2024 12:54:27 -0700 (PDT) Received: from e132581.cambridge.arm.com (e132581.arm.com [10.2.76.71]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 6BFFC3F64C; Wed, 25 Sep 2024 12:53:56 -0700 (PDT) From: Leo Yan To: Arnaldo Carvalho de Melo , Adrian Hunter , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , "Liang, Kan" , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Leo Yan Subject: [PATCH v1 3/5] perf cpumap: Update CPU map merging test Date: Wed, 25 Sep 2024 20:53:23 +0100 Message-Id: <20240925195325.426533-4-leo.yan@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240925195325.426533-1-leo.yan@arm.com> References: <20240925195325.426533-1-leo.yan@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Since the semantics of perf_cpu_map__merge() have changed, the two arguments will not been released automatically in the function. Update the test to release all CPU maps. Signed-off-by: Leo Yan --- tools/perf/tests/cpumap.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/perf/tests/cpumap.c b/tools/perf/tests/cpumap.c index 2f0168b2a5a9..ed07ef6d7e33 100644 --- a/tools/perf/tests/cpumap.c +++ b/tools/perf/tests/cpumap.c @@ -166,6 +166,7 @@ static int test__cpu_map_merge(struct test_suite *test = __maybe_unused, int subte TEST_ASSERT_VAL("failed to merge map: bad nr", perf_cpu_map__nr(c) =3D=3D= 5); cpu_map__snprint(c, buf, sizeof(buf)); TEST_ASSERT_VAL("failed to merge map: bad result", !strcmp(buf, "1-2,4-5,= 7")); + perf_cpu_map__put(a); perf_cpu_map__put(b); perf_cpu_map__put(c); return 0; --=20 2.34.1 From nobody Fri Nov 29 00:56:27 2024 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C8D6C158552; Wed, 25 Sep 2024 19:54:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727294042; cv=none; b=lv4AxWTmdiQlb2+So90SXl1W46cPN+Qz5a2b50uZeS6NfB/ZUxUZms055vah4GfrM/cTUhViP7ChK1M1q3tbHS7D0mazgBpAS7BCygmrma464ihrep9eY7E8LkU/tFCOuSgMm6wrby9p5Q+tkd+kzU08xGUCjc5AajdBK6CtoPc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727294042; c=relaxed/simple; bh=CHzNKVXu1RqZbmV2PfE2l0lyp9I7aLbzynLWgxBwRO4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VeDcmTPGEhk9wrby78Y40nhY8TCPFSeO+zJf0i3rs0Y0LGqz2Y2Xx9wyLdWdKd09rGxPmlyBr36D0zMx2o7WRmUrltfLMYg+Vzd+dR2NIHgSNrZvknIaMyfFdTP8CPCrt7nuGYIk7HPZIfm7OnAE+kqWgUM2OaHGjvsDjVEmyM0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B3BE3150C; Wed, 25 Sep 2024 12:54:29 -0700 (PDT) Received: from e132581.cambridge.arm.com (e132581.arm.com [10.2.76.71]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 7C2213F64C; Wed, 25 Sep 2024 12:53:58 -0700 (PDT) From: Leo Yan To: Arnaldo Carvalho de Melo , Adrian Hunter , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , "Liang, Kan" , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Leo Yan Subject: [PATCH v1 4/5] perf cpumap: Add more tests for CPU map merging Date: Wed, 25 Sep 2024 20:53:24 +0100 Message-Id: <20240925195325.426533-5-leo.yan@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240925195325.426533-1-leo.yan@arm.com> References: <20240925195325.426533-1-leo.yan@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add additional tests for CPU map merging to cover more cases. These tests include different types of arguments, such as when one CPU map is a subset of another, as well as cases with or without overlap between the two maps. Signed-off-by: Leo Yan --- tools/perf/tests/cpumap.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/tools/perf/tests/cpumap.c b/tools/perf/tests/cpumap.c index ed07ef6d7e33..d5b6c450f5c9 100644 --- a/tools/perf/tests/cpumap.c +++ b/tools/perf/tests/cpumap.c @@ -156,22 +156,46 @@ static int test__cpu_map_print(struct test_suite *tes= t __maybe_unused, int subte return 0; } =20 -static int test__cpu_map_merge(struct test_suite *test __maybe_unused, int= subtest __maybe_unused) +static int __test__cpu_map_merge(const char *lhs, const char *rhs, int nr,= const char *expected) { - struct perf_cpu_map *a =3D perf_cpu_map__new("4,2,1"); - struct perf_cpu_map *b =3D perf_cpu_map__new("4,5,7"); + struct perf_cpu_map *a =3D perf_cpu_map__new(lhs); + struct perf_cpu_map *b =3D perf_cpu_map__new(rhs); struct perf_cpu_map *c =3D perf_cpu_map__merge(a, b); char buf[100]; =20 - TEST_ASSERT_VAL("failed to merge map: bad nr", perf_cpu_map__nr(c) =3D=3D= 5); + TEST_ASSERT_VAL("failed to merge map: bad nr", perf_cpu_map__nr(c) =3D=3D= nr); cpu_map__snprint(c, buf, sizeof(buf)); - TEST_ASSERT_VAL("failed to merge map: bad result", !strcmp(buf, "1-2,4-5,= 7")); + TEST_ASSERT_VAL("failed to merge map: bad result", !strcmp(buf, expected)= ); perf_cpu_map__put(a); perf_cpu_map__put(b); perf_cpu_map__put(c); return 0; } =20 +static int test__cpu_map_merge(struct test_suite *test __maybe_unused, + int subtest __maybe_unused) +{ + int ret; + + ret =3D __test__cpu_map_merge("4,2,1", "4,5,7", 5, "1-2,4-5,7"); + if (ret) + return ret; + ret =3D __test__cpu_map_merge("1-8", "6-9", 9, "1-9"); + if (ret) + return ret; + ret =3D __test__cpu_map_merge("1-8,12-20", "6-9,15", 18, "1-9,12-20"); + if (ret) + return ret; + ret =3D __test__cpu_map_merge("4,2,1", "1", 3, "1-2,4"); + if (ret) + return ret; + ret =3D __test__cpu_map_merge("1", "4,2,1", 3, "1-2,4"); + if (ret) + return ret; + ret =3D __test__cpu_map_merge("1", "1", 1, "1"); + return ret; +} + static int __test__cpu_map_intersect(const char *lhs, const char *rhs, int= nr, const char *expected) { struct perf_cpu_map *a =3D perf_cpu_map__new(lhs); --=20 2.34.1 From nobody Fri Nov 29 00:56:27 2024 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D5E8E165F1C; Wed, 25 Sep 2024 19:54:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727294044; cv=none; b=EBlqtGiKtg23AePM5ngqO+xn/nAj4dtc1gfh413R94Bj0zAiTbS/P1B4Zv/n4xgXVNNBNOnqurEY4ENoNlrhxgQ3oOirwQWgzuUKFT1lT+R0GkY6vHz1BuiFTyNauwaMk57J2BwNcM7z+8UdJODTRzN8kqkmyA/yvCTZlf5w8CA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727294044; c=relaxed/simple; bh=XYqMdQ221ClMLf6vRpxFS9OZ5NIjBc81FRZpkGdMeHk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=D9NsVw+MoJkEp6KWhLcLWcnKukfbV/o8FlhQ32DG/gqDzafS9XRYWx3yfPGaXkqAq8zKpgnPhFM0FFRnXEmvtieBpcxiIHWI3b8RpvZmo5xfMtSiv0qv8FWomaXIMWDC0OaODHVRWAj6LBx1mfgQzChxYBHXBrA/krGh+IcwtZI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id BABDA1576; Wed, 25 Sep 2024 12:54:31 -0700 (PDT) Received: from e132581.cambridge.arm.com (e132581.arm.com [10.2.76.71]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 826353F64C; Wed, 25 Sep 2024 12:54:00 -0700 (PDT) From: Leo Yan To: Arnaldo Carvalho de Melo , Adrian Hunter , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , "Liang, Kan" , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Leo Yan Subject: [PATCH v1 5/5] perf cpumap: Add checking for reference counter Date: Wed, 25 Sep 2024 20:53:25 +0100 Message-Id: <20240925195325.426533-6-leo.yan@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240925195325.426533-1-leo.yan@arm.com> References: <20240925195325.426533-1-leo.yan@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" For the CPU map merging and intersection cases, add an extra check for the reference counter before releasing the last CPU map. Signed-off-by: Leo Yan --- tools/perf/tests/cpumap.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tools/perf/tests/cpumap.c b/tools/perf/tests/cpumap.c index d5b6c450f5c9..0bcf603a0ccf 100644 --- a/tools/perf/tests/cpumap.c +++ b/tools/perf/tests/cpumap.c @@ -168,6 +168,16 @@ static int __test__cpu_map_merge(const char *lhs, cons= t char *rhs, int nr, const TEST_ASSERT_VAL("failed to merge map: bad result", !strcmp(buf, expected)= ); perf_cpu_map__put(a); perf_cpu_map__put(b); + + /* + * If 'b' is a superset of 'a', 'c' points to the same map with the + * map 'b'. In this case, the two owners 'b' and 'c' both point to the + * same map. The owner 'b' has released the resource above but 'c' + * still keeps the ownership, so the reference counter should be 1. + */ + TEST_ASSERT_VAL("unexpected refcnt: bad result", + refcount_read(perf_cpu_map__refcnt(c)) =3D=3D 1); + perf_cpu_map__put(c); return 0; } @@ -208,6 +218,16 @@ static int __test__cpu_map_intersect(const char *lhs, = const char *rhs, int nr, c TEST_ASSERT_VAL("failed to intersect map: bad result", !strcmp(buf, expec= ted)); perf_cpu_map__put(a); perf_cpu_map__put(b); + + /* + * If 'b' is a subset of 'a', 'c' points to the same map with the + * map 'b'. In this case, the two owners 'b' and 'c' both point to the + * same map. The owner 'b' has released the resource above but 'c' + * still keeps the ownership, so the reference counter should be 1. + */ + TEST_ASSERT_VAL("unexpected refcnt: bad result", + refcount_read(perf_cpu_map__refcnt(c)) =3D=3D 1); + perf_cpu_map__put(c); return 0; } --=20 2.34.1