From: Leo Yan <leo.yan@arm.com>
To: Adrian Hunter <adrian.hunter@intel.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
"Liang, Kan" <kan.liang@linux.intel.com>,
James Clark <james.clark@linaro.org>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v1 1/5] libperf cpumap: Correct reference count for perf_cpu_map__merge()
Date: Fri, 11 Oct 2024 10:34:38 +0100 [thread overview]
Message-ID: <91690d5c-7f46-4f6d-8140-c6013d84a0f1@arm.com> (raw)
In-Reply-To: <c30c5139-6f77-4a30-bba8-b9372949aec7@intel.com>
Hi Adrian,
On 10/10/24 18:41, Adrian Hunter wrote:>
> On 25/09/24 22:53, Leo Yan wrote:
>> 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 = perf_cpu_map__new("1,2");
>> struct perf_cpu_map *b = perf_cpu_map__new("2");
>>
>> /* 'm' and 'a' point to the same CPU map */
>> struct perf_cpu_map *m = 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 = perf_cpu_map__new("1,2");
>> struct perf_cpu_map *b = perf_cpu_map__new("3");
>>
>> struct perf_cpu_map *m = 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.
>
> The 2 non-test uses of perf_cpu_map__merge both do:
>
> a = perf_cpu_map__merge(a, b);
>
> so another way to make the API less misleading would be
> to introduce:
>
> err = perf_cpu_map__merge_in(&a, b);
>
> where:
>
> int perf_cpu_map__merge_in(struct perf_cpu_map **orig, struct perf_cpu_map *other)
> {
> struct perf_cpu_map *result = perf_cpu_map__merge(*orig, other);
>
> if (!result)
> return -ENOMEM;
>
> *orig = result;
> return 0;
> }
>
> without any changes to perf_cpu_map__merge().
Just wandering why we cannot do the same thing for the perf_cpu_map__merge()
function?
int perf_cpu_map__merge_in(struct perf_cpu_map **orig,
struct perf_cpu_map *other)
This can allow us to avoid any confusion in the first place. And we don't need
to maintain two functions for the same thing.
Thanks,
Leo
next prev parent reply other threads:[~2024-10-11 9:34 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-25 19:53 [PATCH v1 0/5] perf cpumap: Correct " Leo Yan
2024-09-25 19:53 ` [PATCH v1 1/5] libperf cpumap: Correct reference count " Leo Yan
2024-10-10 17:41 ` Adrian Hunter
2024-10-11 9:34 ` Leo Yan [this message]
2024-10-11 9:40 ` Leo Yan
2024-10-11 9:46 ` Adrian Hunter
2024-10-11 9:51 ` Leo Yan
2024-09-25 19:53 ` [PATCH v1 2/5] perf: Release old CPU maps after merging Leo Yan
2024-09-25 19:53 ` [PATCH v1 3/5] perf cpumap: Update CPU map merging test Leo Yan
2024-09-25 19:53 ` [PATCH v1 4/5] perf cpumap: Add more tests for CPU map merging Leo Yan
2024-09-25 19:53 ` [PATCH v1 5/5] perf cpumap: Add checking for reference counter Leo Yan
2024-10-10 15:01 ` [PATCH v1 0/5] perf cpumap: Correct for perf_cpu_map__merge() Leo Yan
2024-10-10 16:10 ` Namhyung Kim
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=91690d5c-7f46-4f6d-8140-c6013d84a0f1@arm.com \
--to=leo.yan@arm.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=kan.liang@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=namhyung@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®