From nobody Sun Apr 26 08:12:48 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1350CC433EF for ; Mon, 20 Jun 2022 16:50:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233099AbiFTQuU (ORCPT ); Mon, 20 Jun 2022 12:50:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231396AbiFTQuS (ORCPT ); Mon, 20 Jun 2022 12:50:18 -0400 Received: from mail-il1-x14a.google.com (mail-il1-x14a.google.com [IPv6:2607:f8b0:4864:20::14a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B95B21758B for ; Mon, 20 Jun 2022 09:50:17 -0700 (PDT) Received: by mail-il1-x14a.google.com with SMTP id s6-20020a056e021a0600b002d8fcba296aso3971096ild.20 for ; Mon, 20 Jun 2022 09:50:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=ExmBzSKpLkiCHa59QsqYQ/MSjLYvSFah4s+gXGLxJqg=; b=YPF0ZpgHCqKySH8864zZhz8jjEywgWvK0N2eUVokfhTYL9opzubKr6lpU+aEV2Mt33 6F7mvs7DFJpAtBqyOQK5B1ZGhjpyNSN7GGDq14uIcQrfP6RtdeZMDDJTXnPOJ+eVU8sc dMZDwRSkv5JYW8U17L7SqX1kJB0LcJB70pbUGK3rO1T2rD9qp/AZLuLDMC7XlpZBPwsJ v0aBxLFLCGUdmNQZJw1a2Uv5Zf/FT/+YyXYWlORlUWuRSRzcnKX+BYEDtWUxMOvRl9t3 5qLBIzUR9OOEN3+/b39OiDXyxrXQmIcv69lth7biygb2Cl633/n7srzCZMCrxWbrf4qi 7Tdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=ExmBzSKpLkiCHa59QsqYQ/MSjLYvSFah4s+gXGLxJqg=; b=VXqfmWBsQ0HDjY9eBSH8ajT4Qt99OsHJVsMQ5dbUVq2litBsaWCWNhB5PPffQ1w3H+ m6mXFHmK+koKlZays+2Pqwz5dyQb0P7AWbJ0JCLxf9oevQdrrPwAa8d/AIbnZLwsYQoT 8J+/eGhtfS6u/cZ7Kj6imfhttN9ivkHKXydgWE9o5a/8L6byd+Lg9Ck7YzTcgL6lOGXR 1yM31YSCojLevea0S8LdfksYvcJnaYODnwMD6rtdBc25bK5HiR2w+psf6EFtBw++aRDV K0kpUjroqOsnNgLjFDdpMihEph3+EYo7DCs77oyWGooUj9QB1S+aZ5yVWVrRHn6f0Gz1 X0jA== X-Gm-Message-State: AJIora9I+WN9pYqgz9cyXoHBP/+oa4lbq5sB/t+v19YTPFnUQX6z+GgN VejVcn7s49fk+zP+bU6i7ZrhmwIM0uOmoQ== X-Google-Smtp-Source: AGRyM1vGM7WoTsCDjAm/pRnXDblKRzisQgoA2GpoRfzaTLDl0iP0ZMsyTomuWIie58fRybYOHkVAbMJibswRjQ== X-Received: from riochico.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:b3e]) (user=rsilvera job=sendgmr) by 2002:a92:902:0:b0:2d8:ea84:e37c with SMTP id y2-20020a920902000000b002d8ea84e37cmr8562357ilg.214.1655743817146; Mon, 20 Jun 2022 09:50:17 -0700 (PDT) Date: Mon, 20 Jun 2022 16:49:53 +0000 Message-Id: <20220620164953.1503934-1-rsilvera@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.37.0.rc0.104.g0611611a94-goog Subject: [PATCH] perf: Adjust perf-inject output data offset for backward compatibility From: Raul Silvera 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@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- tools/perf/builtin-inject.c | 2 +- tools/perf/util/header.c | 16 +++++++++++++++- tools/perf/util/header.h | 2 ++ 3 files changed, 18 insertions(+), 2 deletions(-) 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 =3D perf_event__repipe_tracing_data; } =20 - output_data_offset =3D session->header.data_offset; + output_data_offset =3D perf_session__data_offset(session->evlist); =20 if (inject->build_id_all) { inject->tool.mmap =3D perf_event__repipe_buildid_mmap; diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index 53332da100e8..93706ae827f1 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -3660,7 +3660,7 @@ static int perf_session__do_write_header(struct perf_= session *session, .size =3D evlist->core.nr_entries * sizeof(f_attr), }, .data =3D { - .offset =3D header->data_offset, + .offset =3D attr_offset + evlist->core.nr_entries * sizeof(f_attr), .size =3D header->data_size, }, /* event_types is ignored, store zeros */ @@ -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); } =20 +size_t perf_session__data_offset(const struct evlist *evlist) +{ + struct evsel *evsel; + size_t data_offset; + + data_offset =3D sizeof(struct perf_file_header); + evlist__for_each_entry(evlist, evsel) { + data_offset +=3D evsel->core.ids * sizeof(u64); + } + data_offset +=3D 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 *se= ssion, int fd, struct feat_copier *fc); =20 +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); --=20 2.37.0.rc0.104.g0611611a94-goog