* [PATCH v2] perf: Adjust perf-inject output data offset for backward compatibility
@ 2022-06-21 15:27 Raul Silvera
2022-06-22 8:36 ` Adrian Hunter
0 siblings, 1 reply; 3+ messages in thread
From: Raul Silvera @ 2022-06-21 15:27 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
Ian Rogers, Dave Marchevsky, Adrian Hunter, Colin Ian King,
Athira Rajeev, Raul Silvera
Cc: linux-perf-users, linux-kernel
When perf inject creates a new file, it reuses the data offset from the
input file. If there has been a change on the size of the header, as
happened in v5.12 -> v5.13, the new offsets will be wrong, resulting in
a corrupted output file.
This change adds the function perf_session__data_offset to compute the
data offset based on the current header size, and uses that instead of
the offset from the original input file.
Signed-off-by: Raul Silvera <rsilvera@google.com>
---
Changes since v1:
- Use the adjusted header->data_offset in do_write_header instead of
recomputing it.
---
tools/perf/builtin-inject.c | 2 +-
| 14 ++++++++++++++
| 2 ++
3 files changed, 17 insertions(+), 1 deletion(-)
diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index a75bf11585b5..1dfdcef36607 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -916,7 +916,7 @@ static int __cmd_inject(struct perf_inject *inject)
inject->tool.tracing_data = perf_event__repipe_tracing_data;
}
- output_data_offset = session->header.data_offset;
+ output_data_offset = perf_session__data_offset(session->evlist);
if (inject->build_id_all) {
inject->tool.mmap = perf_event__repipe_buildid_mmap;
--git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 53332da100e8..6ad629db63b7 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -3686,6 +3686,20 @@ int perf_session__write_header(struct perf_session *session,
return perf_session__do_write_header(session, evlist, fd, at_exit, NULL);
}
+size_t perf_session__data_offset(const struct evlist *evlist)
+{
+ struct evsel *evsel;
+ size_t data_offset;
+
+ data_offset = sizeof(struct perf_file_header);
+ evlist__for_each_entry(evlist, evsel) {
+ data_offset += evsel->core.ids * sizeof(u64);
+ }
+ data_offset += evlist->core.nr_entries * sizeof(struct perf_file_attr);
+
+ return data_offset;
+}
+
int perf_session__inject_header(struct perf_session *session,
struct evlist *evlist,
int fd,
--git a/tools/perf/util/header.h b/tools/perf/util/header.h
index 08563c1f1bff..56916dabce7b 100644
--- a/tools/perf/util/header.h
+++ b/tools/perf/util/header.h
@@ -136,6 +136,8 @@ int perf_session__inject_header(struct perf_session *session,
int fd,
struct feat_copier *fc);
+size_t perf_session__data_offset(const struct evlist *evlist);
+
void perf_header__set_feat(struct perf_header *header, int feat);
void perf_header__clear_feat(struct perf_header *header, int feat);
bool perf_header__has_feat(const struct perf_header *header, int feat);
--
2.37.0.rc0.104.g0611611a94-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2] perf: Adjust perf-inject output data offset for backward compatibility
2022-06-21 15:27 [PATCH v2] perf: Adjust perf-inject output data offset for backward compatibility Raul Silvera
@ 2022-06-22 8:36 ` Adrian Hunter
2022-06-23 14:46 ` Arnaldo Carvalho de Melo
0 siblings, 1 reply; 3+ messages in thread
From: Adrian Hunter @ 2022-06-22 8:36 UTC (permalink / raw)
To: Raul Silvera, Peter Zijlstra, Ingo Molnar,
Arnaldo Carvalho de Melo, Mark Rutland, Alexander Shishkin,
Jiri Olsa, Namhyung Kim, Ian Rogers, Dave Marchevsky,
Colin Ian King, Athira Rajeev
Cc: linux-perf-users, linux-kernel
On 21/06/22 18:27, Raul Silvera wrote:
> When perf inject creates a new file, it reuses the data offset from the
> input file. If there has been a change on the size of the header, as
> happened in v5.12 -> v5.13, the new offsets will be wrong, resulting in
> a corrupted output file.
>
> This change adds the function perf_session__data_offset to compute the
> data offset based on the current header size, and uses that instead of
> the offset from the original input file.
>
> Signed-off-by: Raul Silvera <rsilvera@google.com>
Acked-by: Adrian Hunter <adrian.hunter@intel.com>
> ---
> Changes since v1:
> - Use the adjusted header->data_offset in do_write_header instead of
> recomputing it.
> ---
> tools/perf/builtin-inject.c | 2 +-
> tools/perf/util/header.c | 14 ++++++++++++++
> tools/perf/util/header.h | 2 ++
> 3 files changed, 17 insertions(+), 1 deletion(-)
>
> diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
> index a75bf11585b5..1dfdcef36607 100644
> --- a/tools/perf/builtin-inject.c
> +++ b/tools/perf/builtin-inject.c
> @@ -916,7 +916,7 @@ static int __cmd_inject(struct perf_inject *inject)
> inject->tool.tracing_data = perf_event__repipe_tracing_data;
> }
>
> - output_data_offset = session->header.data_offset;
> + output_data_offset = perf_session__data_offset(session->evlist);
>
> if (inject->build_id_all) {
> inject->tool.mmap = perf_event__repipe_buildid_mmap;
> diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
> index 53332da100e8..6ad629db63b7 100644
> --- a/tools/perf/util/header.c
> +++ b/tools/perf/util/header.c
> @@ -3686,6 +3686,20 @@ int perf_session__write_header(struct perf_session *session,
> return perf_session__do_write_header(session, evlist, fd, at_exit, NULL);
> }
>
> +size_t perf_session__data_offset(const struct evlist *evlist)
> +{
> + struct evsel *evsel;
> + size_t data_offset;
> +
> + data_offset = sizeof(struct perf_file_header);
> + evlist__for_each_entry(evlist, evsel) {
> + data_offset += evsel->core.ids * sizeof(u64);
> + }
> + data_offset += evlist->core.nr_entries * sizeof(struct perf_file_attr);
> +
> + return data_offset;
> +}
> +
> int perf_session__inject_header(struct perf_session *session,
> struct evlist *evlist,
> int fd,
> diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h
> index 08563c1f1bff..56916dabce7b 100644
> --- a/tools/perf/util/header.h
> +++ b/tools/perf/util/header.h
> @@ -136,6 +136,8 @@ int perf_session__inject_header(struct perf_session *session,
> int fd,
> struct feat_copier *fc);
>
> +size_t perf_session__data_offset(const struct evlist *evlist);
> +
> void perf_header__set_feat(struct perf_header *header, int feat);
> void perf_header__clear_feat(struct perf_header *header, int feat);
> bool perf_header__has_feat(const struct perf_header *header, int feat);
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] perf: Adjust perf-inject output data offset for backward compatibility
2022-06-22 8:36 ` Adrian Hunter
@ 2022-06-23 14:46 ` Arnaldo Carvalho de Melo
0 siblings, 0 replies; 3+ messages in thread
From: Arnaldo Carvalho de Melo @ 2022-06-23 14:46 UTC (permalink / raw)
To: Adrian Hunter
Cc: Raul Silvera, Peter Zijlstra, Ingo Molnar, Mark Rutland,
Alexander Shishkin, Jiri Olsa, Namhyung Kim, Ian Rogers,
Dave Marchevsky, Colin Ian King, Athira Rajeev, linux-perf-users,
linux-kernel
Em Wed, Jun 22, 2022 at 11:36:58AM +0300, Adrian Hunter escreveu:
> On 21/06/22 18:27, Raul Silvera wrote:
> > When perf inject creates a new file, it reuses the data offset from the
> > input file. If there has been a change on the size of the header, as
> > happened in v5.12 -> v5.13, the new offsets will be wrong, resulting in
> > a corrupted output file.
> >
> > This change adds the function perf_session__data_offset to compute the
> > data offset based on the current header size, and uses that instead of
> > the offset from the original input file.
> >
> > Signed-off-by: Raul Silvera <rsilvera@google.com>
>
> Acked-by: Adrian Hunter <adrian.hunter@intel.com>
Thanks, applied.
- Arnaldo
> > ---
> > Changes since v1:
> > - Use the adjusted header->data_offset in do_write_header instead of
> > recomputing it.
> > ---
> > tools/perf/builtin-inject.c | 2 +-
> > tools/perf/util/header.c | 14 ++++++++++++++
> > tools/perf/util/header.h | 2 ++
> > 3 files changed, 17 insertions(+), 1 deletion(-)
> >
> > diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
> > index a75bf11585b5..1dfdcef36607 100644
> > --- a/tools/perf/builtin-inject.c
> > +++ b/tools/perf/builtin-inject.c
> > @@ -916,7 +916,7 @@ static int __cmd_inject(struct perf_inject *inject)
> > inject->tool.tracing_data = perf_event__repipe_tracing_data;
> > }
> >
> > - output_data_offset = session->header.data_offset;
> > + output_data_offset = perf_session__data_offset(session->evlist);
> >
> > if (inject->build_id_all) {
> > inject->tool.mmap = perf_event__repipe_buildid_mmap;
> > diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
> > index 53332da100e8..6ad629db63b7 100644
> > --- a/tools/perf/util/header.c
> > +++ b/tools/perf/util/header.c
> > @@ -3686,6 +3686,20 @@ int perf_session__write_header(struct perf_session *session,
> > return perf_session__do_write_header(session, evlist, fd, at_exit, NULL);
> > }
> >
> > +size_t perf_session__data_offset(const struct evlist *evlist)
> > +{
> > + struct evsel *evsel;
> > + size_t data_offset;
> > +
> > + data_offset = sizeof(struct perf_file_header);
> > + evlist__for_each_entry(evlist, evsel) {
> > + data_offset += evsel->core.ids * sizeof(u64);
> > + }
> > + data_offset += evlist->core.nr_entries * sizeof(struct perf_file_attr);
> > +
> > + return data_offset;
> > +}
> > +
> > int perf_session__inject_header(struct perf_session *session,
> > struct evlist *evlist,
> > int fd,
> > diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h
> > index 08563c1f1bff..56916dabce7b 100644
> > --- a/tools/perf/util/header.h
> > +++ b/tools/perf/util/header.h
> > @@ -136,6 +136,8 @@ int perf_session__inject_header(struct perf_session *session,
> > int fd,
> > struct feat_copier *fc);
> >
> > +size_t perf_session__data_offset(const struct evlist *evlist);
> > +
> > void perf_header__set_feat(struct perf_header *header, int feat);
> > void perf_header__clear_feat(struct perf_header *header, int feat);
> > bool perf_header__has_feat(const struct perf_header *header, int feat);
--
- Arnaldo
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-06-23 14:46 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-21 15:27 [PATCH v2] perf: Adjust perf-inject output data offset for backward compatibility Raul Silvera
2022-06-22 8:36 ` Adrian Hunter
2022-06-23 14:46 ` Arnaldo Carvalho de Melo
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).