* [PATCH v1] perf sched: Avoid union type punning undefined behavior
@ 2025-09-14 18:33 Ian Rogers
2025-09-30 20:05 ` Ian Rogers
0 siblings, 1 reply; 3+ messages in thread
From: Ian Rogers @ 2025-09-14 18:33 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, Kan Liang, linux-perf-users,
linux-kernel
A union is used to set the priv value in thread (a void*) to a boolean
value through type punning. Undefined behavior sanitizer fails on this.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/builtin-sched.c | 19 ++++---------------
1 file changed, 4 insertions(+), 15 deletions(-)
diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index f166d6cbc083..eca3b1c58c4b 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -1532,35 +1532,24 @@ static int process_sched_wakeup_ignore(const struct perf_tool *tool __maybe_unus
return 0;
}
-union map_priv {
- void *ptr;
- bool color;
-};
-
static bool thread__has_color(struct thread *thread)
{
- union map_priv priv = {
- .ptr = thread__priv(thread),
- };
-
- return priv.color;
+ return thread__priv(thread) != NULL;
}
static struct thread*
map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid)
{
struct thread *thread = machine__findnew_thread(machine, pid, tid);
- union map_priv priv = {
- .color = false,
- };
+ bool color = false;
if (!sched->map.color_pids || !thread || thread__priv(thread))
return thread;
if (thread_map__has(sched->map.color_pids, tid))
- priv.color = true;
+ color = true;
- thread__set_priv(thread, priv.ptr);
+ thread__set_priv(thread, color ? ((void*)1) : NULL);
return thread;
}
--
2.51.0.384.g4c02a37b29-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v1] perf sched: Avoid union type punning undefined behavior
2025-09-14 18:33 [PATCH v1] perf sched: Avoid union type punning undefined behavior Ian Rogers
@ 2025-09-30 20:05 ` Ian Rogers
2025-10-01 13:38 ` Arnaldo Carvalho de Melo
0 siblings, 1 reply; 3+ messages in thread
From: Ian Rogers @ 2025-09-30 20:05 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, Kan Liang, linux-perf-users,
linux-kernel
On Sun, Sep 14, 2025 at 11:33 AM Ian Rogers <irogers@google.com> wrote:
>
> A union is used to set the priv value in thread (a void*) to a boolean
> value through type punning. Undefined behavior sanitizer fails on this.
>
> Signed-off-by: Ian Rogers <irogers@google.com>
Ping.
Thanks,
Ian
> ---
> tools/perf/builtin-sched.c | 19 ++++---------------
> 1 file changed, 4 insertions(+), 15 deletions(-)
>
> diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
> index f166d6cbc083..eca3b1c58c4b 100644
> --- a/tools/perf/builtin-sched.c
> +++ b/tools/perf/builtin-sched.c
> @@ -1532,35 +1532,24 @@ static int process_sched_wakeup_ignore(const struct perf_tool *tool __maybe_unus
> return 0;
> }
>
> -union map_priv {
> - void *ptr;
> - bool color;
> -};
> -
> static bool thread__has_color(struct thread *thread)
> {
> - union map_priv priv = {
> - .ptr = thread__priv(thread),
> - };
> -
> - return priv.color;
> + return thread__priv(thread) != NULL;
> }
>
> static struct thread*
> map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid)
> {
> struct thread *thread = machine__findnew_thread(machine, pid, tid);
> - union map_priv priv = {
> - .color = false,
> - };
> + bool color = false;
>
> if (!sched->map.color_pids || !thread || thread__priv(thread))
> return thread;
>
> if (thread_map__has(sched->map.color_pids, tid))
> - priv.color = true;
> + color = true;
>
> - thread__set_priv(thread, priv.ptr);
> + thread__set_priv(thread, color ? ((void*)1) : NULL);
> return thread;
> }
>
> --
> 2.51.0.384.g4c02a37b29-goog
>
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v1] perf sched: Avoid union type punning undefined behavior
2025-09-30 20:05 ` Ian Rogers
@ 2025-10-01 13:38 ` Arnaldo Carvalho de Melo
0 siblings, 0 replies; 3+ messages in thread
From: Arnaldo Carvalho de Melo @ 2025-10-01 13:38 UTC (permalink / raw)
To: Ian Rogers
Cc: Peter Zijlstra, Ingo Molnar, Namhyung Kim, Mark Rutland,
Alexander Shishkin, Jiri Olsa, Adrian Hunter, Kan Liang,
linux-perf-users, linux-kernel
On Tue, Sep 30, 2025 at 01:05:33PM -0700, Ian Rogers wrote:
> On Sun, Sep 14, 2025 at 11:33 AM Ian Rogers <irogers@google.com> wrote:
> >
> > A union is used to set the priv value in thread (a void*) to a boolean
> > value through type punning. Undefined behavior sanitizer fails on this.
> >
> > Signed-off-by: Ian Rogers <irogers@google.com>
>
> Ping.
Thanks, applied to perf-tools-next,
- Arnaldo
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-10-01 13:38 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-14 18:33 [PATCH v1] perf sched: Avoid union type punning undefined behavior Ian Rogers
2025-09-30 20:05 ` Ian Rogers
2025-10-01 13:38 ` Arnaldo Carvalho de Melo
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.