* [PATCH v2] perf tools: Avoid unaligned pointer operations
@ 2024-11-28 1:03 Namhyung Kim
2024-11-28 14:59 ` Liang, Kan
0 siblings, 1 reply; 3+ messages in thread
From: Namhyung Kim @ 2024-11-28 1:03 UTC (permalink / raw)
To: Arnaldo Carvalho de Melo, Ian Rogers, Kan Liang
Cc: Jiri Olsa, Adrian Hunter, Peter Zijlstra, Ingo Molnar, LKML,
linux-perf-users
The sample data is 64-bit aligned basically but raw data starts with
32-bit length field and data follows. In perf_event__synthesize_sample
it treats the sample data as a 64-bit array. And it needs some trick
to update the raw data properly.
But it seems some compilers are not happy with this and the program dies
siliently. I found the sample parsing test failed without any messages
on affected systems.
Let's update the code to use a 32-bit pointer directly and make sure the
result is 64-bit aligned again. No functional changes intended.
Reviewed-by: Ian Rogers <irogers@google.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
v2) use '%' instead of '/' to check alignment
tools/perf/util/synthetic-events.c | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c
index a58444c4aed1f1ea..6923b0d5efede4a7 100644
--- a/tools/perf/util/synthetic-events.c
+++ b/tools/perf/util/synthetic-events.c
@@ -1686,12 +1686,16 @@ int perf_event__synthesize_sample(union perf_event *event, u64 type, u64 read_fo
}
if (type & PERF_SAMPLE_RAW) {
- u.val32[0] = sample->raw_size;
- *array = u.val64;
- array = (void *)array + sizeof(u32);
+ u32 *array32 = (void *)array;
+
+ *array32 = sample->raw_size;
+ array32++;
+
+ memcpy(array32, sample->raw_data, sample->raw_size);
+ array = (void *)(array32 + (sample->raw_size / sizeof(u32)));
- memcpy(array, sample->raw_data, sample->raw_size);
- array = (void *)array + sample->raw_size;
+ /* make sure the array is 64-bit aligned */
+ BUG_ON(((long)array) % sizeof(u64));
}
if (type & PERF_SAMPLE_BRANCH_STACK) {
--
2.47.0.338.g60cca15819-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2] perf tools: Avoid unaligned pointer operations
2024-11-28 1:03 [PATCH v2] perf tools: Avoid unaligned pointer operations Namhyung Kim
@ 2024-11-28 14:59 ` Liang, Kan
2024-12-13 20:57 ` Ian Rogers
0 siblings, 1 reply; 3+ messages in thread
From: Liang, Kan @ 2024-11-28 14:59 UTC (permalink / raw)
To: Namhyung Kim, Arnaldo Carvalho de Melo, Ian Rogers
Cc: Jiri Olsa, Adrian Hunter, Peter Zijlstra, Ingo Molnar, LKML,
linux-perf-users
On 2024-11-27 8:03 p.m., Namhyung Kim wrote:
> The sample data is 64-bit aligned basically but raw data starts with
> 32-bit length field and data follows. In perf_event__synthesize_sample
> it treats the sample data as a 64-bit array. And it needs some trick
> to update the raw data properly.
>
> But it seems some compilers are not happy with this and the program dies
> siliently. I found the sample parsing test failed without any messages
> on affected systems.
>
> Let's update the code to use a 32-bit pointer directly and make sure the
> result is 64-bit aligned again. No functional changes intended.
>
> Reviewed-by: Ian Rogers <irogers@google.com>
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Reviewed-by: Kan Liang <kan.liang@linux.intel.com>
Thanks,
Kan
> ---
> v2) use '%' instead of '/' to check alignment
>
> tools/perf/util/synthetic-events.c | 14 +++++++++-----
> 1 file changed, 9 insertions(+), 5 deletions(-)
>
> diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c
> index a58444c4aed1f1ea..6923b0d5efede4a7 100644
> --- a/tools/perf/util/synthetic-events.c
> +++ b/tools/perf/util/synthetic-events.c
> @@ -1686,12 +1686,16 @@ int perf_event__synthesize_sample(union perf_event *event, u64 type, u64 read_fo
> }
>
> if (type & PERF_SAMPLE_RAW) {
> - u.val32[0] = sample->raw_size;
> - *array = u.val64;
> - array = (void *)array + sizeof(u32);
> + u32 *array32 = (void *)array;
> +
> + *array32 = sample->raw_size;
> + array32++;
> +
> + memcpy(array32, sample->raw_data, sample->raw_size);
> + array = (void *)(array32 + (sample->raw_size / sizeof(u32)));
>
> - memcpy(array, sample->raw_data, sample->raw_size);
> - array = (void *)array + sample->raw_size;
> + /* make sure the array is 64-bit aligned */
> + BUG_ON(((long)array) % sizeof(u64));
> }
>
> if (type & PERF_SAMPLE_BRANCH_STACK) {
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] perf tools: Avoid unaligned pointer operations
2024-11-28 14:59 ` Liang, Kan
@ 2024-12-13 20:57 ` Ian Rogers
0 siblings, 0 replies; 3+ messages in thread
From: Ian Rogers @ 2024-12-13 20:57 UTC (permalink / raw)
To: Liang, Kan
Cc: Namhyung Kim, Arnaldo Carvalho de Melo, Jiri Olsa, Adrian Hunter,
Peter Zijlstra, Ingo Molnar, LKML, linux-perf-users
On Thu, Nov 28, 2024 at 6:59 AM Liang, Kan <kan.liang@linux.intel.com> wrote:
>
> On 2024-11-27 8:03 p.m., Namhyung Kim wrote:
> > The sample data is 64-bit aligned basically but raw data starts with
> > 32-bit length field and data follows. In perf_event__synthesize_sample
> > it treats the sample data as a 64-bit array. And it needs some trick
> > to update the raw data properly.
> >
> > But it seems some compilers are not happy with this and the program dies
> > siliently. I found the sample parsing test failed without any messages
> > on affected systems.
> >
> > Let's update the code to use a 32-bit pointer directly and make sure the
> > result is 64-bit aligned again. No functional changes intended.
> >
> > Reviewed-by: Ian Rogers <irogers@google.com>
> > Signed-off-by: Namhyung Kim <namhyung@kernel.org>
>
> Reviewed-by: Kan Liang <kan.liang@linux.intel.com>
nit: The commit message subject is somewhat generic. Perhaps "perf
synthetic-events: Avoid unaligned accesses for raw samples".
Thanks,
Ian
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-12-13 20:58 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-28 1:03 [PATCH v2] perf tools: Avoid unaligned pointer operations Namhyung Kim
2024-11-28 14:59 ` Liang, Kan
2024-12-13 20:57 ` Ian Rogers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).