All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Arnaldo Carvalho de Melo" <acme@kernel.org>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH 06/11] perf sched: Fix idle-hist callchain display using wrong rb_first variant
Date: Tue, 09 Jun 2026 01:18:18 +0000	[thread overview]
Message-ID: <20260609011819.3FAEB1F00899@smtp.kernel.org> (raw)
In-Reply-To: <20260609010526.1998472-7-acme@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] In `perf sched map`, using the `--color-pids` option causes a segmentation fault and an invalid free due to type confusion on `thread__priv()`.
--

commit 52b25a8ed38e1c2d458f164827bbcfd55e08f69b
Author: Arnaldo Carvalho de Melo <acme@redhat.com>

perf sched: Fix idle-hist callchain display using wrong rb_first variant

This commit fixes a bug in the idle-hist callchain display where
rb_first_cached() was used on a tree populated with rb_insert_color(). The
tree lacks the leftmost cache, so rb_first_cached() returned NULL and dropped
the callchain summary. The fix restores the original behavior by using
rb_first() directly on the rb_root.

> diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
> index ded511d851880..85f11d388392d 100644
> --- a/tools/perf/builtin-sched.c
> +++ b/tools/perf/builtin-sched.c
> @@ -3130,7 +3130,8 @@ static size_t timehist_print_idlehist_callchain(struct rb_root_cached *root)
>  	size_t ret = 0;
>  	FILE *fp = stdout;
>  	struct callchain_node *chain;
> -	struct rb_node *rb_node = rb_first_cached(root);
> +	/* sort() uses rb_insert_color() on rb_root, not rb_root_cached */
> +	struct rb_node *rb_node = rb_first(&root->rb_root);

[Severity: High]
This isn't a bug introduced by this patch, but does the --color-pids
option in perf sched map cause a segmentation fault and invalid free
due to type confusion on thread__priv()?

When --color-pids is used, map__findnew_thread() sets
thread__priv(thread) to the scalar value (void*)1 to mark the thread
for coloring:

tools/perf/builtin-sched.c:map__findnew_thread() {
    ...
    if (thread_map__has(sched->map.color_pids, tid))
        color = true;

    thread__set_priv(thread, color ? ((void*)1) : NULL);
    ...
}

However, the rest of the perf sched code expects thread__priv() to
hold a valid pointer to a struct thread_runtime. When
map_switch_event() calls thread__get_runtime(), it retrieves the
(void*)1 pointer and skips initialization because it is not NULL:

tools/perf/builtin-sched.c:thread__get_runtime() {
    ...
    tr = thread__priv(thread);
    if (tr == NULL) {
        tr = thread__init_runtime(thread);
    ...
}

Then map_switch_event() attempts to read tr->shortname[0], which
dereferences the invalid address and causes an immediate segmentation
fault:

tools/perf/builtin-sched.c:map_switch_event() {
    ...
    tr = thread__get_runtime(sched_in);
    if (tr == NULL)
        goto out;
    ...
    if (!tr->shortname[0]) {
    ...
}

Furthermore, because cmd_sched() registers free() as the destructor
for thread priv data:

tools/perf/builtin-sched.c:cmd_sched() {
    ...
    thread__set_priv_destructor(free);
    ...
}

Tearing down the session will attempt to call free((void*)1),
corrupting the heap.

Could struct thread_runtime be expanded to include a coloring flag,
or could the color state be tracked outside the thread__priv pointer
to prevent this type confusion?

>  
>  	printf("  %16s  %8s  %s\n", "Idle time (msec)", "Count", "Callchains");
>  	printf("  %.16s  %.8s  %.50s\n", graph_dotted_line, graph_dotted_line,

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260609010526.1998472-1-acme@kernel.org?part=6

  reply	other threads:[~2026-06-09  1:18 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-09  1:05 [PATCHES v4 00/11] perf tools: Assorted fixes Arnaldo Carvalho de Melo
2026-06-09  1:05 ` [PATCH 01/11] perf tools: Fix get_max_num() size_t underflow on empty sysfs file Arnaldo Carvalho de Melo
2026-06-09  1:24   ` sashiko-bot
2026-06-09  1:05 ` [PATCH 02/11] perf tools: Use scnprintf() in cpu_map__snprint() to prevent overflow Arnaldo Carvalho de Melo
2026-06-09  1:05 ` [PATCH 03/11] perf tools: Use perf_env__get_cpu_topology() in machine__resolve() Arnaldo Carvalho de Melo
2026-06-09  1:22   ` sashiko-bot
2026-06-09  1:05 ` [PATCH 04/11] perf tools: NULL bitmap pointers after bitmap_free() Arnaldo Carvalho de Melo
2026-06-09  1:23   ` sashiko-bot
2026-06-09  1:05 ` [PATCH 05/11] perf sched: Bounds-check prio before test_bit() in timehist Arnaldo Carvalho de Melo
2026-06-09  1:05 ` [PATCH 06/11] perf sched: Fix idle-hist callchain display using wrong rb_first variant Arnaldo Carvalho de Melo
2026-06-09  1:18   ` sashiko-bot [this message]
2026-06-09  1:05 ` [PATCH 07/11] perf tools: Add O_CLOEXEC to open() calls in DSO and ELF code Arnaldo Carvalho de Melo
2026-06-09  1:16   ` sashiko-bot
2026-06-09  1:05 ` [PATCH 08/11] perf bpf: Use scnprintf() in snprintf_hex() and synthesize_bpf_prog_name() Arnaldo Carvalho de Melo
2026-06-09  1:18   ` sashiko-bot
2026-06-09  1:05 ` [PATCH 09/11] perf hists: Fix snprintf() in hists__scnprintf_title() UID filter path Arnaldo Carvalho de Melo
2026-06-09  1:23   ` sashiko-bot
2026-06-09  1:05 ` [PATCH 10/11] perf tools: Use scnprintf() in build_id__snprintf() and hwmon read_events() Arnaldo Carvalho de Melo
2026-06-09  1:17   ` sashiko-bot
2026-06-09  1:05 ` [PATCH 11/11] libperf: Document code simplification case for widening struct perf_cpu Arnaldo Carvalho de Melo
  -- strict thread matches above, loose matches on Subject: below --
2026-06-08 20:17 [PATCHES v3 00/11] perf tools: Assorted fixes Arnaldo Carvalho de Melo
2026-06-08 20:17 ` [PATCH 06/11] perf sched: Fix idle-hist callchain display using wrong rb_first variant Arnaldo Carvalho de Melo
2026-06-08 21:59   ` Ian Rogers

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=20260609011819.3FAEB1F00899@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=acme@kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.