Re: [PATCH 2/2] perf report: Don't add to histogram when there is no thread found

From: Ian Rogers
Date: Tue Jun 27 2023 - 12:43:18 EST


On Mon, Jun 26, 2023 at 5:02 PM Namhyung Kim <namhyung@xxxxxxxxxx> wrote:
>
> On Mon, Jun 26, 2023 at 05:10:58PM +0100, James Clark wrote:
> > thread__find_map() chooses to exit without assigning a thread to the
> > addr_location in some scenarios, for example when there are samples from
> > a guest and perf_guest == false. This results in a segfault when adding
> > to the histogram because it uses unguarded accesses to the thread member
> > of the addr_location.
>
> Looking at the commit 0dd5041c9a0ea ("perf addr_location: Add
> init/exit/copy functions") that introduced the change, I'm not sure if
> it's the intend behavior.
>
> It might change maps and map, but not thread. Then I think no reason
> to not set the al->thread at the beginning.
>
> How about this? Ian?
> (I guess we can get rid of the duplicate 'al->map = NULL' part)

It seemed strange that we were failing to find a map (the function's
purpose) but then populating the address_location. The change below
brings back that somewhat odd behavior. I'm okay with reverting to the
old behavior, clearly there were users relying on it. We should
probably also copy maps and not just thread, as that was the previous
behavior.

Thanks,
Ian

> Thanks,
> Namhyung
>
>
> ---8<---
>
> diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
> index 3860b0c74829..4cbb092e0684 100644
> --- a/tools/perf/util/event.c
> +++ b/tools/perf/util/event.c
> @@ -581,15 +581,14 @@ struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
> maps__zput(al->maps);
> map__zput(al->map);
> thread__zput(al->thread);
> + al->thread = thread__get(thread);
>
> al->addr = addr;
> al->cpumode = cpumode;
> al->filtered = 0;
>
> - if (machine == NULL) {
> - al->map = NULL;
> + if (machine == NULL)
> return NULL;
> - }
>
> if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
> al->level = 'k';
> @@ -605,7 +604,6 @@ struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
> al->level = 'u';
> } else {
> al->level = 'H';
> - al->map = NULL;
>
> if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
> cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
> @@ -619,7 +617,6 @@ struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
> return NULL;
> }
> al->maps = maps__get(maps);
> - al->thread = thread__get(thread);
> al->map = map__get(maps__find(maps, al->addr));
> if (al->map != NULL) {
> /*