From nobody Mon Jun 8 05:24:57 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 20C9F282F1B; Tue, 2 Jun 2026 23:57:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780444646; cv=none; b=lXxj8roahreMzisPT+gxO0UF7Jwps2kOnkpDqLxKu8bU9IqppBgu5fwwHLZf8DMAMsEMn2inAM75O+K3WkseadFZz8+iVgklkvV0Esxp6zMZrxQ/LGA7cKb8lmGKotJZ9y+4R7g4MyUrnYwwngt47/RWsiwrxdwjIP8OUy95dtQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780444646; c=relaxed/simple; bh=jXidNDA8LRO9dK6i/qGjx0uk2g///hUp3PxSO1/BqN0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OSkS+njLz1B5PoqxhdkblEWmgvtwsATqlHK2IhE/F9aj04W5yjLTHVuTiQv8Mj+hNw633WAORCeqiTXbwwJohaf/WgOw1t/9r+2/r1lt77qgSVdQZKMHvWwd8+16ZvANN8othSI42Rj380jRnmeJYeRCL1ZA9RAlJJ8py12A4YE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KxVx1PAh; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="KxVx1PAh" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 76B2A1F00898; Tue, 2 Jun 2026 23:57:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1780444645; bh=lOcDbCc7Iramj69iqATBMmpIIl4u6Ltd0M2lP0mgQmo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=KxVx1PAhzaKToyX9kKJBxRA1pYefvzc99i3SxzwT6SN/ONPu+VGd0CwqHkWCWHlV8 SZOuX0G6F+LBV5+FDGuBR8Teb9/uC//mhIQF/kD/iAKgmUp45CCXPj4pjScHmS8ayV Jh5w70OvPpg2l5AkeUExgz84VcWARKKQ8wZIpJthlX5L++3YLRqHM4B9zV04MvbY8I K5me/TlZPuqCoY19NMbGJbB1ShcgAawxtTwX1YRNJI5iKinTggkxlgi41E+gNH7Kav l+aioWRS71XWhx6bIjpYsj0sIxI7AEasH5eKTDWWn1QnlFX2O45vXiV4EqzPc+A28z rMDStUoKaz7FQ== From: Arnaldo Carvalho de Melo To: Namhyung Kim Cc: Ingo Molnar , Thomas Gleixner , James Clark , Jiri Olsa , Ian Rogers , Adrian Hunter , Clark Williams , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Arnaldo Carvalho de Melo , "Claude Opus 4.6" Subject: [PATCH 1/8] perf sample: Add file_offset field to struct perf_sample Date: Tue, 2 Jun 2026 20:57:00 -0300 Message-ID: <20260602235709.1541603-2-acme@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260602235709.1541603-1-acme@kernel.org> References: <20260602235709.1541603-1-acme@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Arnaldo Carvalho de Melo Add a file_offset field to struct perf_sample so that event processing callbacks can report the byte offset of the problematic event in perf.data, letting users cross-reference with 'perf report -D' output. Set sample.file_offset in perf_session__deliver_event(), which is the common entry point for both file mode (mmap'd offset) and pipe mode (running byte counter from __perf_session__process_pipe_events). The assignment is placed after evsel__parse_sample(), which zeroes the struct via memset. Preserve file_offset through the deferred callchain delivery path by storing it in struct deferred_event and restoring it after evlist__parse_sample() in both evlist__deliver_deferred_callchain() and session__flush_deferred_samples(). Subsequent patches will use this field in skip/stop warning messages. Assisted-by: Claude Opus 4.6 Signed-off-by: Arnaldo Carvalho de Melo Reviewed-by: Ian Rogers --- tools/perf/util/sample.h | 2 ++ tools/perf/util/session.c | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/tools/perf/util/sample.h b/tools/perf/util/sample.h index e556c9b656ea9cd6..c4eae8b2fd06035a 100644 --- a/tools/perf/util/sample.h +++ b/tools/perf/util/sample.h @@ -158,6 +158,8 @@ struct perf_sample { u64 code_page_size; /** @cgroup: The sample event PERF_SAMPLE_CGROUP value. */ u64 cgroup; + /** @file_offset: Byte offset of this event in the perf.data file. */ + u64 file_offset; /** @flags: Extra flag data from auxiliary events like intel-pt. */ u32 flags; /** @machine_pid: The guest machine pid derived from the sample id. */ diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index e2e821b77766dbfc..7996787d742e32c6 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -1824,6 +1824,7 @@ static int evlist__deliver_sample(struct evlist *evli= st, const struct perf_tool struct deferred_event { struct list_head list; union perf_event *event; + u64 file_offset; }; =20 /* @@ -1858,6 +1859,7 @@ static int evlist__deliver_deferred_callchain(struct = evlist *evlist, perf_sample__exit(&orig_sample); break; } + orig_sample.file_offset =3D de->file_offset; =20 if (sample->tid !=3D orig_sample.tid) { perf_sample__exit(&orig_sample); @@ -1906,6 +1908,7 @@ static int session__flush_deferred_samples(struct per= f_session *session, perf_sample__exit(&sample); break; } + sample.file_offset =3D de->file_offset; =20 sample.evsel =3D evlist__id2evsel(evlist, sample.id); ret =3D evlist__deliver_sample(evlist, tool, de->event, @@ -1984,6 +1987,7 @@ static int machines__deliver_event(struct machines *m= achines, return -ENOMEM; } memcpy(de->event, event, sz); + de->file_offset =3D sample->file_offset; list_add_tail(&de->list, &evlist->deferred_samples); return 0; } @@ -2126,6 +2130,7 @@ static int perf_session__deliver_event(struct perf_se= ssion *session, pr_err("Can't parse sample, err =3D %d\n", ret); goto out; } + sample.file_offset =3D file_offset; /* * evsel__parse_sample() doesn't populate machine_pid/vcpu, * which are needed by machines__find_for_cpumode() to --=20 2.54.0 From nobody Mon Jun 8 05:24:57 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DA50D3C76AF; Tue, 2 Jun 2026 23:57:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780444651; cv=none; b=KdORGgONZgpepcQh1Tjydqauyopf0n6O4BNfJMGZUSATS51Tt6th1lijX/Bq/BZ/xh3F9Y7j/1buWnVaZzWVfC5NdWETgrX+CTjzRstezZsFuK/4uMeTsf5S3mwXWxMsLbXRlFJIjlrpXzkW+pN20V2zss8LArtMAhb4GCKQJbk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780444651; c=relaxed/simple; bh=FizgIGoqxIYieXEQd67/TFnY4gePAIZsmdbaF6lKoWk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XEkVBiFQps0tuGFhg2qqi3X30MZPnkPXI7W+jSeBOc/YvtctrWGrnSqQEHbhnYAPY5neS8ToxF3kp1BgGS99ym1lXMOAoPCsC8kFjFSYpJV/+5NPaq+VO8NbUPHUYUeuyGHW1fqXZ7iFcF6Q/2G/RBbU15cqe25LKIB5KWS7zN0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Vgsr6AdF; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Vgsr6AdF" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A919A1F00893; Tue, 2 Jun 2026 23:57:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1780444649; bh=78qyVSIOLPMH4ADifqB4NJQ/0qW2+qa/hZS/twONP98=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Vgsr6AdFg5ObLEnl7w5fPW5wZrgbzhpyjX/gqSNBg5zpXq/1YWKcxaTNJqYTuFNX+ XAduxqHG6ToID7/tfE9nLDQLVLaWzZg4z60cablK4qtKr9Ay8j8txxh/brp+GI9CgC uCqoeGBrwSQJcXvhe5EgU5vD2+lj7pGRqxj8mc1dyN/R0jhZ96jqKlimabSVdbI+7u Hwa4Hb//Ag2Gushecnw5siGH57fQEKLc1rLQnKZ+Gah0rsR7NC5jTl+vyA4HTTdYxD dldu2ETp+6d0xrBBLA5ujY/WzqMp1mJSpE067fcnuFyhvI4FdeYy66anaIeAHnxAEQ KAukl43HN+tnQ== From: Arnaldo Carvalho de Melo To: Namhyung Kim Cc: Ingo Molnar , Thomas Gleixner , James Clark , Jiri Olsa , Ian Rogers , Adrian Hunter , Clark Williams , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Arnaldo Carvalho de Melo , "Claude Opus 4.6" Subject: [PATCH 2/8] perf session: Include file offset in event skip/stop messages Date: Tue, 2 Jun 2026 20:57:01 -0300 Message-ID: <20260602235709.1541603-3-acme@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260602235709.1541603-1-acme@kernel.org> References: <20260602235709.1541603-1-acme@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Arnaldo Carvalho de Melo Add 'at offset %#' to all warning and error messages in session.c that fire when events are skipped or processing stops due to validation failures. This lets users cross-reference with 'perf report -D' output to inspect the surrounding records and understand the corruption context. Covers messages in perf_session__process_event() (alignment, min size, swap failure), perf_session__deliver_event() (no evsel, parse failure, CPU clamping), machines__deliver_event() (NAMESPACES, TEXT_POKE, null-terminated string checks for MMAP/MMAP2/COMM/CGROUP/KSYMBOL), and perf_session__process_user_event() (THREAD_MAP, CPU_MAP, STAT_CONFIG, BPF_METADATA, HEADER_BUILD_ID). Assisted-by: Claude Opus 4.6 Signed-off-by: Arnaldo Carvalho de Melo Reviewed-by: Ian Rogers --- tools/perf/util/session.c | 112 ++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 52 deletions(-) diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index 7996787d742e32c6..e4efb75509278a4e 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -1931,13 +1931,14 @@ static int session__flush_deferred_samples(struct p= erf_session *session, * read-only (MAP_SHARED + PROT_READ) so we cannot write a * null byte in place; skip the event instead. */ -static bool perf_event__check_nul(const char *str, const void *end, const = char *event_name) +static bool perf_event__check_nul(const char *str, const void *end, + const char *event_name, u64 file_offset) { size_t max_len =3D (const char *)end - str; =20 if (max_len =3D=3D 0 || strnlen(str, max_len) =3D=3D max_len) { - pr_warning("WARNING: PERF_RECORD_%s: string not null-terminated, skippin= g event\n", - event_name); + pr_warning("WARNING: at offset %#" PRIx64 ": PERF_RECORD_%s: string not = null-terminated, skipping event\n", + file_offset, event_name); return false; } =20 @@ -1995,7 +1996,7 @@ static int machines__deliver_event(struct machines *m= achines, case PERF_RECORD_MMAP: if (!perf_event__check_nul(event->mmap.filename, (void *)event + event->header.size, - "MMAP")) + "MMAP", file_offset)) return 0; return tool->mmap(tool, event, sample, machine); case PERF_RECORD_MMAP2: @@ -2003,13 +2004,13 @@ static int machines__deliver_event(struct machines = *machines, ++evlist->stats.nr_proc_map_timeout; if (!perf_event__check_nul(event->mmap2.filename, (void *)event + event->header.size, - "MMAP2")) + "MMAP2", file_offset)) return 0; return tool->mmap2(tool, event, sample, machine); case PERF_RECORD_COMM: if (!perf_event__check_nul(event->comm.comm, (void *)event + event->header.size, - "COMM")) + "COMM", file_offset)) return 0; return tool->comm(tool, event, sample, machine); case PERF_RECORD_NAMESPACES: { @@ -2027,8 +2028,8 @@ static int machines__deliver_event(struct machines *m= achines, * cross-endian path. */ if (event->namespaces.nr_namespaces > max_nr) { - pr_warning("WARNING: PERF_RECORD_NAMESPACES: nr_namespaces %" PRIu64 " = exceeds payload (max %" PRIu64 "), skipping\n", - (u64)event->namespaces.nr_namespaces, max_nr); + pr_warning("WARNING: at offset %#" PRIx64 ": PERF_RECORD_NAMESPACES: nr= _namespaces %" PRIu64 " exceeds payload (max %" PRIu64 "), skipping\n", + file_offset, (u64)event->namespaces.nr_namespaces, max_nr); return 0; } return tool->namespaces(tool, event, sample, machine); @@ -2036,7 +2037,7 @@ static int machines__deliver_event(struct machines *m= achines, case PERF_RECORD_CGROUP: if (!perf_event__check_nul(event->cgroup.path, (void *)event + event->header.size, - "CGROUP")) + "CGROUP", file_offset)) return 0; return tool->cgroup(tool, event, sample, machine); case PERF_RECORD_FORK: @@ -2078,7 +2079,7 @@ static int machines__deliver_event(struct machines *m= achines, case PERF_RECORD_KSYMBOL: if (!perf_event__check_nul(event->ksymbol.name, (void *)event + event->header.size, - "KSYMBOL")) + "KSYMBOL", file_offset)) return 0; return tool->ksymbol(tool, event, sample, machine); case PERF_RECORD_BPF_EVENT: @@ -2090,7 +2091,8 @@ static int machines__deliver_event(struct machines *m= achines, event->text_poke.new_len; =20 if (event->header.size < text_poke_len) { - pr_warning("WARNING: PERF_RECORD_TEXT_POKE: old_len+new_len exceeds eve= nt, skipping\n"); + pr_warning("WARNING: at offset %#" PRIx64 ": PERF_RECORD_TEXT_POKE: old= _len+new_len exceeds event, skipping\n", + file_offset); return 0; } return tool->text_poke(tool, event, sample, machine); @@ -2120,14 +2122,17 @@ static int perf_session__deliver_event(struct perf_= session *session, perf_sample__init(&sample, /*all=3D*/false); evsel =3D evlist__event2evsel(session->evlist, event); if (!evsel) { - pr_err("No evsel found for event type %u\n", + pr_err("ERROR: at offset %#" PRIx64 ": no evsel found for %s (%u) event\= n", + file_offset, perf_event__name(event->header.type), event->header.type); ret =3D -EFAULT; goto out; } ret =3D evsel__parse_sample(evsel, event, &sample); if (ret) { - pr_err("Can't parse sample, err =3D %d\n", ret); + pr_err("ERROR: at offset %#" PRIx64 ": can't parse %s (%u) sample, err = =3D %d\n", + file_offset, perf_event__name(event->header.type), + event->header.type, ret); goto out; } sample.file_offset =3D file_offset; @@ -2204,8 +2209,8 @@ static int perf_session__deliver_event(struct perf_se= ssion *session, * Downstream array users (timechart, kwork) have * their own per-callback bounds checks. */ - pr_warning_once("WARNING: sample CPU %u >=3D nr_cpus_avail %u, clamping= to 0\n", - sample.cpu, nr_cpus_avail); + pr_warning_once("WARNING: at offset %#" PRIx64 ": sample CPU %u >=3D nr= _cpus_avail %u, clamping to 0\n", + file_offset, sample.cpu, nr_cpus_avail); sample.cpu =3D 0; } } @@ -2278,7 +2283,7 @@ static s64 perf_session__process_user_event(struct pe= rf_session *session, case PERF_RECORD_HEADER_BUILD_ID: if (!perf_event__check_nul(event->build_id.filename, (void *)event + event_size, - "HEADER_BUILD_ID")) { + "HEADER_BUILD_ID", file_offset)) { err =3D 0; break; } @@ -2311,8 +2316,8 @@ static s64 perf_session__process_user_event(struct pe= rf_session *session, u64 max_nr; =20 if (event_size < sizeof(event->thread_map)) { - pr_err("PERF_RECORD_THREAD_MAP: header.size (%u) too small\n", - event_size); + pr_err("ERROR: at offset %#" PRIx64 ": PERF_RECORD_THREAD_MAP: header.s= ize (%u) too small\n", + file_offset, event_size); err =3D -EINVAL; break; } @@ -2320,8 +2325,8 @@ static s64 perf_session__process_user_event(struct pe= rf_session *session, max_nr =3D (event_size - sizeof(event->thread_map)) / sizeof(event->thread_map.entries[0]); if (event->thread_map.nr > max_nr) { - pr_err("PERF_RECORD_THREAD_MAP: nr %" PRIu64 " exceeds max %" PRIu64 "\= n", - (u64)event->thread_map.nr, max_nr); + pr_err("ERROR: at offset %#" PRIx64 ": PERF_RECORD_THREAD_MAP: nr %" PR= Iu64 " exceeds max %" PRIu64 "\n", + file_offset, (u64)event->thread_map.nr, max_nr); err =3D -EINVAL; break; } @@ -2345,8 +2350,8 @@ static s64 perf_session__process_user_event(struct pe= rf_session *session, sizeof(data->cpus_data.cpu[0]); =20 if (data->cpus_data.nr > max_nr) { - pr_warning("WARNING: PERF_RECORD_CPU_MAP: nr %u exceeds payload (max %= u), skipping\n", - data->cpus_data.nr, max_nr); + pr_warning("WARNING: at offset %#" PRIx64 ": PERF_RECORD_CPU_MAP: nr %= u exceeds payload (max %u), skipping\n", + file_offset, data->cpus_data.nr, max_nr); err =3D 0; goto out; } @@ -2359,8 +2364,8 @@ static s64 perf_session__process_user_event(struct pe= rf_session *session, sizeof(data->mask32_data.mask[0]); =20 if (data->mask32_data.nr > max_nr) { - pr_warning("WARNING: PERF_RECORD_CPU_MAP mask32: nr %u exceeds payloa= d (max %u), skipping\n", - data->mask32_data.nr, max_nr); + pr_warning("WARNING: at offset %#" PRIx64 ": PERF_RECORD_CPU_MAP mask= 32: nr %u exceeds payload (max %u), skipping\n", + file_offset, data->mask32_data.nr, max_nr); err =3D 0; goto out; } @@ -2375,14 +2380,14 @@ static s64 perf_session__process_user_event(struct = perf_session *session, mask64_data.mask)) / sizeof(data->mask64_data.mask[0]); if (data->mask64_data.nr > max_nr) { - pr_warning("WARNING: PERF_RECORD_CPU_MAP mask64: nr %u exceeds payloa= d (max %u), skipping\n", - data->mask64_data.nr, max_nr); + pr_warning("WARNING: at offset %#" PRIx64 ": PERF_RECORD_CPU_MAP mask= 64: nr %u exceeds payload (max %u), skipping\n", + file_offset, data->mask64_data.nr, max_nr); err =3D 0; goto out; } } else { - pr_warning("WARNING: PERF_RECORD_CPU_MAP: unsupported long_size %u, sk= ipping\n", - data->mask32_data.long_size); + pr_warning("WARNING: at offset %#" PRIx64 ": PERF_RECORD_CPU_MAP: unsu= pported long_size %u, skipping\n", + file_offset, data->mask32_data.long_size); err =3D 0; goto out; } @@ -2404,8 +2409,8 @@ static s64 perf_session__process_user_event(struct pe= rf_session *session, * cannot clamp nr in place. Skip the event instead. */ if (event->stat_config.nr > max_nr) { - pr_warning("WARNING: PERF_RECORD_STAT_CONFIG: nr %" PRIu64 " exceeds pa= yload (max %" PRIu64 "), skipping\n", - (u64)event->stat_config.nr, max_nr); + pr_warning("WARNING: at offset %#" PRIx64 ": PERF_RECORD_STAT_CONFIG: n= r %" PRIu64 " exceeds payload (max %" PRIu64 "), skipping\n", + file_offset, (u64)event->stat_config.nr, max_nr); err =3D 0; goto out; } @@ -2446,8 +2451,8 @@ static s64 perf_session__process_user_event(struct pe= rf_session *session, u64 nr_entries, max_entries; =20 if (event_size < sizeof(event->bpf_metadata)) { - pr_warning("WARNING: PERF_RECORD_BPF_METADATA: header.size (%u) too sma= ll, skipping\n", - event_size); + pr_warning("WARNING: at offset %#" PRIx64 ": PERF_RECORD_BPF_METADATA: = header.size (%u) too small, skipping\n", + file_offset, event_size); err =3D 0; break; } @@ -2458,7 +2463,8 @@ static s64 perf_session__process_user_event(struct pe= rf_session *session, */ if (strnlen(event->bpf_metadata.prog_name, BPF_PROG_NAME_LEN) =3D=3D BPF_PROG_NAME_LEN) { - pr_warning("WARNING: PERF_RECORD_BPF_METADATA: prog_name not null-termi= nated, skipping\n"); + pr_warning("WARNING: at offset %#" PRIx64 ": PERF_RECORD_BPF_METADATA: = prog_name not null-terminated, skipping\n", + file_offset); err =3D 0; break; } @@ -2467,8 +2473,8 @@ static s64 perf_session__process_user_event(struct pe= rf_session *session, max_entries =3D (event_size - sizeof(event->bpf_metadata)) / sizeof(event->bpf_metadata.entries[0]); if (nr_entries > max_entries) { - pr_warning("WARNING: PERF_RECORD_BPF_METADATA: nr_entries %" PRIu64 " e= xceeds max %" PRIu64 ", skipping\n", - nr_entries, max_entries); + pr_warning("WARNING: at offset %#" PRIx64 ": PERF_RECORD_BPF_METADATA: = nr_entries %" PRIu64 " exceeds max %" PRIu64 ", skipping\n", + file_offset, nr_entries, max_entries); err =3D 0; break; } @@ -2478,7 +2484,8 @@ static s64 perf_session__process_user_event(struct pe= rf_session *session, BPF_METADATA_KEY_LEN) =3D=3D BPF_METADATA_KEY_LEN || strnlen(event->bpf_metadata.entries[i].value, BPF_METADATA_VALUE_LEN) =3D=3D BPF_METADATA_VALUE_LEN) { - pr_warning("WARNING: PERF_RECORD_BPF_METADATA: entry %" PRIu64 " key/v= alue not null-terminated, skipping\n", i); + pr_warning("WARNING: at offset %#" PRIx64 ": PERF_RECORD_BPF_METADATA:= entry %" PRIu64 " key/value not null-terminated, skipping\n", + file_offset, i); err =3D 0; goto out; } @@ -2752,22 +2759,22 @@ int perf_session__peek_event(struct perf_session *s= ession, off_t file_offset, event->header.type !=3D PERF_RECORD_HEADER_TRACING_DATA && event->header.type !=3D PERF_RECORD_COMPRESSED && event->header.type !=3D PERF_RECORD_HEADER_FEATURE) { - pr_warning("WARNING: peek_event: event type %u size %u not aligned to %z= u\n", - event->header.type, - event->header.size, sizeof(u64)); + pr_warning("WARNING: at offset %#" PRIx64 ": %s (%u) event size %u not a= ligned to %zu\n", + (u64)file_offset, perf_event__name(event->header.type), + event->header.type, event->header.size, sizeof(u64)); return -1; } =20 if (event->header.type >=3D PERF_RECORD_HEADER_MAX) { - pr_warning("WARNING: peek_event: unsupported event type %u, skipping\n", - event->header.type); + pr_warning("WARNING: at offset %#" PRIx64 ": unsupported event type %u, = skipping\n", + (u64)file_offset, event->header.type); return 0; } =20 if (perf_event__too_small(event, &min_sz)) { - pr_warning("WARNING: peek_event: %s event size %u too small (min %u)\n", - perf_event__name(event->header.type), - event->header.size, min_sz); + pr_warning("WARNING: at offset %#" PRIx64 ": %s (%u) event size %u too s= mall (min %u)\n", + (u64)file_offset, perf_event__name(event->header.type), + event->header.type, event->header.size, min_sz); return -1; } =20 @@ -2883,9 +2890,9 @@ static s64 perf_session__process_event(struct perf_se= ssion *session, event->header.type !=3D PERF_RECORD_HEADER_TRACING_DATA && event->header.type !=3D PERF_RECORD_COMPRESSED && event->header.type !=3D PERF_RECORD_HEADER_FEATURE) { - pr_err("ERROR: %s event size %u is not 8-byte aligned, aborting\n", - perf_event__name(event->header.type), - event->header.size); + pr_err("ERROR: at offset %#" PRIx64 ": %s (%u) event size %u is not 8-by= te aligned, aborting\n", + file_offset, perf_event__name(event->header.type), + event->header.type, event->header.size); return -EINVAL; } =20 @@ -2905,16 +2912,17 @@ static s64 perf_session__process_event(struct perf_= session *session, * can be safely stepped over without misaligning the stream. */ if (perf_event__too_small(event, &min_sz)) { - pr_warning("WARNING: %s event size %u too small (min %u), skipping\n", - perf_event__name(event->header.type), - event->header.size, min_sz); + pr_warning("WARNING: at offset %#" PRIx64 ": %s (%u) event size %u too s= mall (min %u), skipping\n", + file_offset, perf_event__name(event->header.type), + event->header.type, event->header.size, min_sz); return 0; } =20 if (session->header.needs_swap && event_swap(event, evlist__sample_id_all(evlist))) { - pr_warning("WARNING: swap failed for %s event, skipping\n", - perf_event__name(event->header.type)); + pr_warning("WARNING: at offset %#" PRIx64 ": swap failed for %s (%u) eve= nt, skipping\n", + file_offset, perf_event__name(event->header.type), + event->header.type); return 0; } =20 --=20 2.54.0 From nobody Mon Jun 8 05:24:57 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B93743CB2FC; Tue, 2 Jun 2026 23:57:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780444655; cv=none; b=aHeLNRTWwE2vnFGcH78eTi9zVJlKA78YZohN9dEerEU/WBc0THEt/+6Zog/X5iFU8QlvV7hU6rR5gdQLKWxRVnj0ukEOq4pnih/syxYDk3Amez1VcTIpHlG0+mQTjTLDLP29LUnO/rXD+CDwzBvPzTm8+BF1qPjRoXrzvNL80No= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780444655; c=relaxed/simple; bh=KZwkJkZjsY/11Do1faVM/vH/2MqM0m1kOtXth5gvPCc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=HKe45FtumBCeSxwREVRU+bEwUNMsVrWmGs3YxcSvYU9WIzgU91TLQDWb45fyQyjP9Fdh+xUYYaW+tyd0oZxVySxYWg64JnpahnUsfBiq4GjOYR1qv+6jmvR2ekIZ5CDcJiGKD3BE8ZkPI2sY9YxS1tjYJXVW0AoCaWBqm+FfA1I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VUqLZjk/; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="VUqLZjk/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 306011F00898; Tue, 2 Jun 2026 23:57:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1780444654; bh=10fYTi30u0rj6y2S/wsCfMswDglZ5K7K2lvEl1gVHVk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=VUqLZjk/t5PHeMK9ABQBiCLy0lXUcOiZv0utIWHQt2UU93BFI5xApc8ns4qLlqVY9 Ku+xV9uwQv6XqYyfnmsbBZJUfJxG1QBx68sJkaN9i7ntxQTjmnF2otlMtIwAlkSXRE UVeJOYNMVXZklNZqwNnLoCqilhXs/fR8utYI/PJJVjwEvQRxsWD2WD+k5sOBrkEH0D OTx82co0PVzs4krlg/9cukLAXQxo4C2M8N6eL/VapSYhW5E1m8rLLIWBNcjo60MO/a 7qr8intDkkbMereyP9TJnwo+9NevUlT/T2s5XFEui+LdLKrs1dY32AxmDR08F/eH9m TZQVfq4shNHdQ== From: Arnaldo Carvalho de Melo To: Namhyung Kim Cc: Ingo Molnar , Thomas Gleixner , James Clark , Jiri Olsa , Ian Rogers , Adrian Hunter , Clark Williams , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Arnaldo Carvalho de Melo , "Claude Opus 4.6" Subject: [PATCH 3/8] perf sched: Include file offset in event skip messages Date: Tue, 2 Jun 2026 20:57:02 -0300 Message-ID: <20260602235709.1541603-4-acme@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260602235709.1541603-1-acme@kernel.org> References: <20260602235709.1541603-1-acme@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Arnaldo Carvalho de Melo Add the perf.data file offset to the CPU out-of-bounds and machine__resolve failure messages emitted when samples are skipped in process_sched_switch_event(), process_sched_runtime_event(), and timehist_sched_change_event(). Also switch event type from raw integer to perf_event__name() string for readability. Assisted-by: Claude Opus 4.6 Signed-off-by: Arnaldo Carvalho de Melo Reviewed-by: Ian Rogers --- tools/perf/builtin-sched.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c index 812a1b0d56d6e5f3..9ec8e049e19b0038 100644 --- a/tools/perf/builtin-sched.c +++ b/tools/perf/builtin-sched.c @@ -1792,8 +1792,10 @@ static int process_sched_switch_event(const struct p= erf_tool *tool, u32 prev_pid =3D perf_sample__intval(sample, "prev_pid"), next_pid =3D perf_sample__intval(sample, "next_pid"); =20 + /* perf.data is untrusted input =E2=80=94 CPU may be absent or corrupted = */ if (this_cpu < 0 || this_cpu >=3D MAX_CPUS) { - pr_warning("Out-of-bound sample CPU %d. Skipping sample\n", this_cpu); + pr_warning("WARNING: at offset %#" PRIx64 ": out-of-bound sample CPU %d,= skipping sample\n", + sample->file_offset, this_cpu); return 0; } =20 @@ -1819,8 +1821,10 @@ static int process_sched_runtime_event(const struct = perf_tool *tool, { struct perf_sched *sched =3D container_of(tool, struct perf_sched, tool); =20 + /* perf.data is untrusted input =E2=80=94 CPU may be absent or corrupted = */ if (sample->cpu >=3D MAX_CPUS) { - pr_warning("Out-of-bound sample CPU %u. Skipping sample\n", sample->cpu); + pr_warning("WARNING: at offset %#" PRIx64 ": out-of-bound sample CPU %u,= skipping sample\n", + sample->file_offset, sample->cpu); return 0; } =20 @@ -2786,15 +2790,18 @@ static int timehist_sched_change_event(const struct= perf_tool *tool, int rc =3D 0; const char state =3D perf_sample__taskstate(sample, "prev_state"); =20 + /* perf.data is untrusted input =E2=80=94 CPU may be absent or corrupted = */ if (sample->cpu >=3D MAX_CPUS) { - pr_warning("Out-of-bound sample CPU %d. Skipping sample\n", sample->cpu); + pr_warning("WARNING: at offset %#" PRIx64 ": out-of-bound sample CPU %d,= skipping sample\n", + sample->file_offset, sample->cpu); return 0; } =20 addr_location__init(&al); if (machine__resolve(machine, &al, sample) < 0) { - pr_err("problem processing %d event. skipping it\n", - event->header.type); + pr_err("problem processing %s (%u) event at offset %#" PRIx64 ", skippin= g it\n", + perf_event__name(event->header.type), event->header.type, + sample->file_offset); rc =3D -1; goto out; } --=20 2.54.0 From nobody Mon Jun 8 05:24:57 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C33283B38BE; Tue, 2 Jun 2026 23:57:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780444660; cv=none; b=Ih+k+hUCLPmGsiknFIVMazWtLdpSPZBqum4NKrehJBOXkSf69jfkkjTwrQSELDQqxXvXVJcqISFFw3AGvqOR0lLcexpvu6VaYIqnsYX/YcDvcC5dZ9JKAUmLuSlO9eK6XDoSEOY5eo8DFgYgmuhg8ASZreyROps9DqPEPYXEJzE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780444660; c=relaxed/simple; bh=7DHsLM7XidbkkjIOsgAiF7oDdpCGOyzhpG3B7CYMD4c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=F93NNnFGS2rsBakpucICiQ75cqZCqs5Xx/tzgn/U8qHtYbhXbzAXS/b7DXk8qL4jDoWtCMFFplrN0Y4DO5WuS+wp3T1ExGVf6CEv09aVNQqtxCLSuX0paUA4uHdV/Or6Tw6USg1LdDyPJ4gGLIy9UClyK3zwrKhbVcM/zf/TGJg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=b34wN5qb; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="b34wN5qb" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1BF761F00893; Tue, 2 Jun 2026 23:57:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1780444659; bh=GKDHO0PxxBqgohG98+Cq1neCIrILOrnO2K6UBft8lAI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=b34wN5qbzUyAk4EhABnL6BUY9ZI5LuMR0LtzdxiL+fgrlv2na847VbzIR58u6lj19 DYeVit7Ago1elHx7rRYQQ7myLva2Q3lXISX+vOaAI6TM2qQm0vJnGtJ8qQk6lTDHbH mZHMn6Eavvr8FQM/TbQENOzA1tXLHZNMDDq81jf6rMHKJAr0WbDIw0zxI5yKMRVyIX 4EfOZdxj5A0mCEktYaYmyB8iptOP5UbTNjQXW4im4RQjEuBvukzUJJx2QcgDoVccpI nwrVxvLq0anlFX63XZLCZguh3WOPipH5ZzZ/BX4heP4+qSrbjHWKhMSv+KUc78MZYg qqQj1mWYLfFIg== From: Arnaldo Carvalho de Melo To: Namhyung Kim Cc: Ingo Molnar , Thomas Gleixner , James Clark , Jiri Olsa , Ian Rogers , Adrian Hunter , Clark Williams , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Arnaldo Carvalho de Melo , "Claude Opus 4.6" Subject: [PATCH 4/8] perf timechart: Include file offset in CPU bounds check messages Date: Tue, 2 Jun 2026 20:57:03 -0300 Message-ID: <20260602235709.1541603-5-acme@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260602235709.1541603-1-acme@kernel.org> References: <20260602235709.1541603-1-acme@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Arnaldo Carvalho de Melo Add the perf.data file offset to the out-of-bounds CPU debug messages in process_sample_cpu_idle(), process_sample_cpu_frequency(), process_sample_sched_wakeup(), and process_sample_sched_switch(). Assisted-by: Claude Opus 4.6 Signed-off-by: Arnaldo Carvalho de Melo Reviewed-by: Ian Rogers --- tools/perf/builtin-timechart.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/tools/perf/builtin-timechart.c b/tools/perf/builtin-timechart.c index 630756bebe3242dc..071987241a528ba4 100644 --- a/tools/perf/builtin-timechart.c +++ b/tools/perf/builtin-timechart.c @@ -605,8 +605,10 @@ process_sample_cpu_idle(struct timechart *tchart __may= be_unused, u32 state =3D perf_sample__intval(sample, "state"); u32 cpu_id =3D perf_sample__intval(sample, "cpu_id"); =20 + /* perf.data is untrusted input =E2=80=94 cpu_id may be corrupted */ if (cpu_id >=3D MAX_CPUS) { - pr_debug("Out-of-bounds cpu_id %u\n", cpu_id); + pr_debug("at offset %#" PRIx64 ": out-of-bounds cpu_id %u\n", + sample->file_offset, cpu_id); return -1; } if (state =3D=3D (u32)PWR_EVENT_EXIT) @@ -624,8 +626,10 @@ process_sample_cpu_frequency(struct timechart *tchart, u32 state =3D perf_sample__intval(sample, "state"); u32 cpu_id =3D perf_sample__intval(sample, "cpu_id"); =20 + /* perf.data is untrusted input =E2=80=94 cpu_id may be corrupted */ if (cpu_id >=3D MAX_CPUS) { - pr_debug("Out-of-bounds cpu_id %u\n", cpu_id); + pr_debug("at offset %#" PRIx64 ": out-of-bounds cpu_id %u\n", + sample->file_offset, cpu_id); return -1; } p_state_change(tchart, cpu_id, sample->time, state); @@ -641,8 +645,10 @@ process_sample_sched_wakeup(struct timechart *tchart, int waker =3D perf_sample__intval(sample, "common_pid"); int wakee =3D perf_sample__intval(sample, "pid"); =20 + /* perf.data is untrusted input =E2=80=94 CPU may be absent or corrupted = */ if (sample->cpu >=3D MAX_CPUS) { - pr_debug("Out-of-bounds cpu %u\n", sample->cpu); + pr_debug("at offset %#" PRIx64 ": out-of-bounds cpu %u\n", + sample->file_offset, sample->cpu); return -1; } sched_wakeup(tchart, sample->cpu, sample->time, waker, wakee, flags, back= trace); @@ -658,8 +664,10 @@ process_sample_sched_switch(struct timechart *tchart, int next_pid =3D perf_sample__intval(sample, "next_pid"); u64 prev_state =3D perf_sample__intval(sample, "prev_state"); =20 + /* perf.data is untrusted input =E2=80=94 CPU may be absent or corrupted = */ if (sample->cpu >=3D MAX_CPUS) { - pr_debug("Out-of-bounds cpu %u\n", sample->cpu); + pr_debug("at offset %#" PRIx64 ": out-of-bounds cpu %u\n", + sample->file_offset, sample->cpu); return -1; } sched_switch(tchart, sample->cpu, sample->time, prev_pid, next_pid, @@ -676,6 +684,7 @@ process_sample_power_start(struct timechart *tchart __m= aybe_unused, u64 cpu_id =3D perf_sample__intval(sample, "cpu_id"); u64 value =3D perf_sample__intval(sample, "value"); =20 + /* perf.data is untrusted input =E2=80=94 cpu_id may be corrupted */ if (cpu_id >=3D MAX_CPUS) { pr_debug("Out-of-bounds cpu_id %llu\n", (unsigned long long)cpu_id); return -1; @@ -689,6 +698,7 @@ process_sample_power_end(struct timechart *tchart, struct perf_sample *sample, const char *backtrace __maybe_unused) { + /* perf.data is untrusted input =E2=80=94 CPU may be absent or corrupted = */ if (sample->cpu >=3D MAX_CPUS) { pr_debug("Out-of-bounds cpu %u\n", sample->cpu); return -1; @@ -705,6 +715,7 @@ process_sample_power_frequency(struct timechart *tchart, u64 cpu_id =3D perf_sample__intval(sample, "cpu_id"); u64 value =3D perf_sample__intval(sample, "value"); =20 + /* perf.data is untrusted input =E2=80=94 cpu_id may be corrupted */ if (cpu_id >=3D MAX_CPUS) { pr_debug("Out-of-bounds cpu_id %llu\n", (unsigned long long)cpu_id); return -1; --=20 2.54.0 From nobody Mon Jun 8 05:24:57 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9247B3B38BE; Tue, 2 Jun 2026 23:57:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780444665; cv=none; b=KAKOGFN3R027OqN7CJt0+cU7UUEa4lCfAhg3/5+SdLv0eqgtHDjbOZSB8mLr4THeLQJTZy41rpvAfMYa7+K6JUQMy8xnld1DbmO7+a1gcWeQEstI9K6DKw04kLJgysSAomjbw+5VogRlevU4RhC3RrUvxGjDTI6f1Q/nZxwWNN8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780444665; c=relaxed/simple; bh=uzetH1C5DDfYWzo0efCsn0CQiTXNOSNURdVjptgmA78=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UtouS8o6H8NC9M08Mcy6QrQ4cMzlMMLwojd6zwQtAkfZnElHmKC2OBsz5YcliJWPyd2dxWtUcmQyPQjGuJMEYhDRNyAXInQbWyfBqbFf5CUCnP13KsBNeFynq6zNtXjwyhz8Y0a6m9g/tUHevRaJWR9/GmywohVgWoEcN0R7R/Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jfnEwQ6W; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="jfnEwQ6W" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 201EC1F00898; Tue, 2 Jun 2026 23:57:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1780444664; bh=F3OKixCrqV9hoH2+zZgSn9a6/Zjkz4ow6yk629oAjNY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=jfnEwQ6WEidrTTU+w9d/RunFBbi9cnkN7Kt/9LWByUpHGQPee54bzBaB+TeUUmjOD GyCrhj9resbB6qj0EPwe6L/g6t9zzyC7rwglXoxC0nPzScwTqFnjT7KWDLGdOq1JlO CL96G5BaKomQxyQHDT1wko+IkHbekzZxrK2oGBUFoHEWD1/oxzKZrPFO5qBSfdN0bP QFI5q+KZrZWAURont3AeEMptISon4HFVAXUKLR/C5njjSf4lILuJMHTxyX1iwuB1CJ 4OHt5gm5OvGtcH/9pxdgt5pp6NDYUVH0FMDTdYTl+UkqWXXkvtwhPdrx5kDUBjChrW lwclx1BMCAHDg== From: Arnaldo Carvalho de Melo To: Namhyung Kim Cc: Ingo Molnar , Thomas Gleixner , James Clark , Jiri Olsa , Ian Rogers , Adrian Hunter , Clark Williams , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Arnaldo Carvalho de Melo , "Claude Opus 4.6" Subject: [PATCH 5/8] perf tools: Include file offset and event type name in skip messages Date: Tue, 2 Jun 2026 20:57:04 -0300 Message-ID: <20260602235709.1541603-6-acme@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260602235709.1541603-1-acme@kernel.org> References: <20260602235709.1541603-1-acme@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Arnaldo Carvalho de Melo Add the perf.data file offset and use perf_event__name() instead of raw event type integers in the 'problem processing event, skipping it' messages emitted by process_sample_event() callbacks across annotate, c2c, diff, kmem, kvm, kwork, lock, report, script, and build-id. This lets users cross-reference skipped events with 'perf report -D' output. Also add explicit #include "util/event.h" and where needed to avoid depending on transitive includes. Assisted-by: Claude Opus 4.6 Signed-off-by: Arnaldo Carvalho de Melo Reviewed-by: Ian Rogers --- tools/perf/builtin-annotate.c | 5 +++-- tools/perf/builtin-c2c.c | 5 +++-- tools/perf/builtin-diff.c | 8 +++++--- tools/perf/builtin-kmem.c | 6 ++++-- tools/perf/builtin-kvm.c | 9 ++++++--- tools/perf/builtin-kwork.c | 4 +++- tools/perf/builtin-lock.c | 6 ++++-- tools/perf/builtin-report.c | 9 ++++++--- tools/perf/builtin-script.c | 10 ++++++---- tools/perf/util/build-id.c | 5 +++-- 10 files changed, 43 insertions(+), 24 deletions(-) diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c index 5f450c8093c09210..b918f9eed5fd2441 100644 --- a/tools/perf/builtin-annotate.c +++ b/tools/perf/builtin-annotate.c @@ -288,8 +288,9 @@ static int process_sample_event(const struct perf_tool = *tool, =20 addr_location__init(&al); if (machine__resolve(machine, &al, sample) < 0) { - pr_warning("problem processing %d event, skipping it.\n", - event->header.type); + pr_warning("problem processing %s (%u) event at offset %#" PRIx64 ", ski= pping it.\n", + perf_event__name(event->header.type), event->header.type, + sample->file_offset); ret =3D -1; goto out_put; } diff --git a/tools/perf/builtin-c2c.c b/tools/perf/builtin-c2c.c index 36f38694992386ad..d3503be9350c03bb 100644 --- a/tools/perf/builtin-c2c.c +++ b/tools/perf/builtin-c2c.c @@ -328,8 +328,9 @@ static int process_sample_event(const struct perf_tool = *tool __maybe_unused, =20 addr_location__init(&al); if (machine__resolve(machine, &al, sample) < 0) { - pr_debug("problem processing %d event, skipping it.\n", - event->header.type); + pr_debug("problem processing %s (%u) event at offset %#" PRIx64 ", skipp= ing it.\n", + perf_event__name(event->header.type), event->header.type, + sample->file_offset); ret =3D -1; goto out; } diff --git a/tools/perf/builtin-diff.c b/tools/perf/builtin-diff.c index b4ff863b304ca046..9592f44b6545bab6 100644 --- a/tools/perf/builtin-diff.c +++ b/tools/perf/builtin-diff.c @@ -409,8 +409,9 @@ static int diff__process_sample_event(const struct perf= _tool *tool, =20 addr_location__init(&al); if (machine__resolve(machine, &al, sample) < 0) { - pr_warning("problem processing %d event, skipping it.\n", - event->header.type); + pr_warning("problem processing %s (%u) event at offset %#" PRIx64 ", ski= pping it.\n", + perf_event__name(event->header.type), event->header.type, + sample->file_offset); ret =3D -1; goto out; } @@ -436,7 +437,8 @@ static int diff__process_sample_event(const struct perf= _tool *tool, case COMPUTE_STREAM: if (hist_entry_iter__add(&iter, &al, PERF_MAX_STACK_DEPTH, NULL)) { - pr_debug("problem adding hist entry, skipping event\n"); + pr_debug("problem adding hist entry at offset %#" PRIx64 ", skipping ev= ent\n", + sample->file_offset); goto out; } break; diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c index 33585e353efe56cc..e1b2f5bc1ba8d887 100644 --- a/tools/perf/builtin-kmem.c +++ b/tools/perf/builtin-kmem.c @@ -22,6 +22,7 @@ #include "util/cpumap.h" =20 #include "util/debug.h" +#include "util/event.h" #include "util/string2.h" #include "util/util.h" =20 @@ -987,8 +988,9 @@ static int process_sample_event(const struct perf_tool = *tool __maybe_unused, sample->tid); =20 if (thread =3D=3D NULL) { - pr_debug("problem processing %d event, skipping it.\n", - event->header.type); + pr_debug("problem processing %s (%u) event at offset %#" PRIx64 ", skipp= ing it.\n", + perf_event__name(event->header.type), event->header.type, + sample->file_offset); return -1; } =20 diff --git a/tools/perf/builtin-kvm.c b/tools/perf/builtin-kvm.c index dd2ed21596aa59f9..394302ebdb161077 100644 --- a/tools/perf/builtin-kvm.c +++ b/tools/perf/builtin-kvm.c @@ -22,6 +22,7 @@ #include "util/synthetic-events.h" #include "util/top.h" #include "util/data.h" +#include "util/event.h" #include "util/ordered-events.h" #include "util/kvm-stat.h" #include "util/util.h" @@ -1141,14 +1142,16 @@ static int process_sample_event(const struct perf_t= ool *tool, return 0; =20 if (machine__resolve(machine, &kvm->al, sample) < 0) { - pr_warning("Fail to resolve address location, skip sample.\n"); + pr_warning("WARNING: at offset %#" PRIx64 ": fail to resolve address loc= ation, skipping sample\n", + sample->file_offset); return 0; } =20 thread =3D machine__findnew_thread(machine, sample->pid, sample->tid); if (thread =3D=3D NULL) { - pr_debug("problem processing %d event, skipping it.\n", - event->header.type); + pr_debug("problem processing %s (%u) event at offset %#" PRIx64 ", skipp= ing it.\n", + perf_event__name(event->header.type), event->header.type, + sample->file_offset); return -1; } =20 diff --git a/tools/perf/builtin-kwork.c b/tools/perf/builtin-kwork.c index 99dc293a0744726e..110de3507d48160c 100644 --- a/tools/perf/builtin-kwork.c +++ b/tools/perf/builtin-kwork.c @@ -9,6 +9,7 @@ #include "perf.h" =20 #include "util/data.h" +#include "util/event.h" #include "util/evlist.h" #include "util/evsel.h" #include "util/header.h" @@ -897,7 +898,8 @@ static int timehist_exit_event(struct perf_kwork *kwork, =20 addr_location__init(&al); if (machine__resolve(machine, &al, sample) < 0) { - pr_debug("Problem processing event, skipping it\n"); + pr_debug("problem processing event at offset %#" PRIx64 ", skipping it\n= ", + sample->file_offset); ret =3D -1; goto out; } diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c index 94a8c35abb0bc991..5841d43be9718414 100644 --- a/tools/perf/builtin-lock.c +++ b/tools/perf/builtin-lock.c @@ -21,6 +21,7 @@ #include "util/tracepoint.h" =20 #include "util/debug.h" +#include "util/event.h" #include "util/session.h" #include "util/tool.h" #include "util/data.h" @@ -1433,8 +1434,9 @@ static int process_sample_event(const struct perf_too= l *tool __maybe_unused, sample->tid); =20 if (thread =3D=3D NULL) { - pr_debug("problem processing %d event, skipping it.\n", - event->header.type); + pr_debug("problem processing %s (%u) event at offset %#" PRIx64 ", skipp= ing it.\n", + perf_event__name(event->header.type), event->header.type, + sample->file_offset); return -1; } =20 diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index 973d97af85019e6e..cd052aa78132b65f 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -27,6 +27,7 @@ =20 #include "perf.h" #include "util/debug.h" +#include "util/event.h" #include "util/evlist.h" #include "util/evsel.h" #include "util/evswitch.h" @@ -284,8 +285,9 @@ static int process_sample_event(const struct perf_tool = *tool, =20 addr_location__init(&al); if (machine__resolve(machine, &al, sample) < 0) { - pr_debug("problem processing %d event, skipping it.\n", - event->header.type); + pr_debug("problem processing %s (%u) event at offset %#" PRIx64 ", skipp= ing it.\n", + perf_event__name(event->header.type), event->header.type, + sample->file_offset); ret =3D -1; goto out_put; } @@ -332,7 +334,8 @@ static int process_sample_event(const struct perf_tool = *tool, =20 ret =3D hist_entry_iter__add(&iter, &al, rep->max_stack, rep); if (ret < 0) - pr_debug("problem adding hist entry, skipping event\n"); + pr_debug("problem adding hist entry at offset %#" PRIx64 ", skipping eve= nt\n", + sample->file_offset); out_put: addr_location__exit(&al); return ret; diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index 5124edf2b7a692b2..f4aa255fc3297f90 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -2693,8 +2693,9 @@ static int process_sample_event(const struct perf_too= l *tool, goto out_put; =20 if (!al.thread && machine__resolve(machine, &al, sample) < 0) { - pr_err("problem processing %d event, skipping it.\n", - event->header.type); + pr_err("problem processing %s (%u) event at offset %#" PRIx64 ", skippin= g it.\n", + perf_event__name(event->header.type), event->header.type, + sample->file_offset); ret =3D -1; goto out_put; } @@ -2775,8 +2776,9 @@ static int process_deferred_sample_event(const struct= perf_tool *tool, goto out_put; =20 if (machine__resolve(machine, &al, sample) < 0) { - pr_err("problem processing %d event, skipping it.\n", - event->header.type); + pr_err("problem processing %s (%u) event at offset %#" PRIx64 ", skippin= g it.\n", + perf_event__name(event->header.type), event->header.type, + sample->file_offset); ret =3D -1; goto out_put; } diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c index af4d874f13810ffe..8c0a9ae932aa5798 100644 --- a/tools/perf/util/build-id.c +++ b/tools/perf/util/build-id.c @@ -10,6 +10,7 @@ #include "util.h" // lsdir(), mkdir_p(), rm_rf() #include #include +#include #include #include #include @@ -62,8 +63,8 @@ int build_id__mark_dso_hit(const struct perf_tool *tool _= _maybe_unused, sample->tid); =20 if (thread =3D=3D NULL) { - pr_err("problem processing %d event, skipping it.\n", - event->header.type); + pr_err("problem processing %s event at offset %#" PRIx64 ", skipping it.= \n", + perf_event__name(event->header.type), sample->file_offset); return -1; } =20 --=20 2.54.0 From nobody Mon Jun 8 05:24:57 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D21993CB2FC; Tue, 2 Jun 2026 23:57:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780444670; cv=none; b=qZmm10Pai7m2oMIo24ddDyhmdOh56tgDiQxmq8mAbKUxMLoKcvn1VN4/fUjNZKAXDMUbZLWJVOn9z2Qun8zYOl7M6u2fAlEdsXiOa6HEyI9gNS3UfzC+9MMO8eROZuTvCc3cenOKaNMjVkGRuz/bJpqmgO9aB4Tg6cOBcI/tfCc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780444670; c=relaxed/simple; bh=XairhkinOlLgJ183KSKtqLr/kj2basHlueOJfjWbfhU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Ai3qf7zFMuQUoIfmHYo90xmlCVziOS8ekkp+jg9jIuXmqHTRCeCK5VLJAP06Y99+DtyWOl2RmiXF4D85A4n/VzrKNO+ZcKVA13cda7NfjLl0EgNBDWnK6FJ1J0ui3FO/L7MAP11ObNpJvWCtE52xunrgFdi3FX9D6pdCiakalsU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Q0l1MYmd; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Q0l1MYmd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2452E1F00893; Tue, 2 Jun 2026 23:57:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1780444669; bh=7lWCIY/S0jG4bknSkEfEJyJMreQkwBYC0pOxtklnA1s=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Q0l1MYmdndIK39Bi8GVucvTcth2OHo3TeMz59qnWRVNN3B7OIiKLtThA8ovR4WhUy bX/Z0VfiPp2BJ7DJ2Ugb9MmT4kn7WIPLNuBlb/5JouVGaoncT3qvOGK9N8JZFzBiQq QWgR16z3P5Miu1//4QLDvkMYYUIcmF1u28GKNz4qCcXNvlBFctro4ajeCEF5ucHio4 VjZRQbgavDWe3vqL2I3Kt9FnPawQJ4oRKbcV4oo/IETf3xMsrhGvzZwC/+pGttlHap i0kkVY1t7xjMabnZRDQT7aFOR77fDt8WiMXgBPJtYulxpkqsgCxgmhLEM+cd7ike+D a5fu3H4OlxC2Q== From: Arnaldo Carvalho de Melo To: Namhyung Kim Cc: Ingo Molnar , Thomas Gleixner , James Clark , Jiri Olsa , Ian Rogers , Adrian Hunter , Clark Williams , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Arnaldo Carvalho de Melo , sashiko-bot@kernel.org, "Claude Opus 4.6" Subject: [PATCH 6/8] perf timechart: Fix cat_backtrace() use-after-free on corrupted callchain Date: Tue, 2 Jun 2026 20:57:05 -0300 Message-ID: <20260602235709.1541603-7-acme@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260602235709.1541603-1-acme@kernel.org> References: <20260602235709.1541603-1-acme@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Arnaldo Carvalho de Melo cat_backtrace() uses open_memstream() to build a backtrace string. When an invalid callchain context is encountered, zfree(&p) frees the memstream buffer, then the exit path calls fclose(f), which flushes to the already-freed buffer =E2=80=94 a use-after-free. The functi= on then returns a dangling pointer that the caller passes to a handler and subsequently double-frees. Fix by replacing the zfree(&p) with a 'corrupted' flag. At the exit label, always fclose(f) first (which finalizes the buffer), then conditionally free it when corrupted. This ensures the memstream contract is honored: the buffer remains valid until fclose(). While here, update the machine__resolve failure message to include file_offset and the event type name, matching the pattern from the preceding series. Also update the three legacy power event handlers under SUPPORT_OLD_POWER_EVENTS to include file_offset in their out-of-bounds CPU messages for consistency. Reported-by: sashiko-bot@kernel.org # Running on a local machine Assisted-by: Claude Opus 4.6 Signed-off-by: Arnaldo Carvalho de Melo Reviewed-by: Ian Rogers --- tools/perf/builtin-timechart.c | 36 ++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/tools/perf/builtin-timechart.c b/tools/perf/builtin-timechart.c index 071987241a528ba4..85a9ad0455aecccd 100644 --- a/tools/perf/builtin-timechart.c +++ b/tools/perf/builtin-timechart.c @@ -489,6 +489,10 @@ static void sched_switch(struct timechart *tchart, int= cpu, u64 timestamp, } } =20 +/* + * Returns a malloc'd backtrace string built via open_memstream, or NULL + * on error. Caller must free() the returned pointer. + */ static char *cat_backtrace(union perf_event *event, struct perf_sample *sample, struct machine *machine) @@ -500,6 +504,7 @@ static char *cat_backtrace(union perf_event *event, u8 cpumode =3D PERF_RECORD_MISC_USER; struct ip_callchain *chain =3D sample->callchain; FILE *f =3D open_memstream(&p, &p_len); + bool corrupted =3D false; =20 if (!f) { perror("open_memstream error"); @@ -511,8 +516,9 @@ static char *cat_backtrace(union perf_event *event, goto exit; =20 if (machine__resolve(machine, &al, sample) < 0) { - fprintf(stderr, "problem processing %d event, skipping it.\n", - event->header.type); + pr_err("problem processing %s (%u) event at offset %#" PRIx64 ", skippin= g it.\n", + perf_event__name(event->header.type), event->header.type, + sample->file_offset); goto exit; } =20 @@ -537,14 +543,8 @@ static char *cat_backtrace(union perf_event *event, cpumode =3D PERF_RECORD_MISC_USER; break; default: - pr_debug("invalid callchain context: " - "%"PRId64"\n", (s64) ip); - - /* - * It seems the callchain is corrupted. - * Discard all. - */ - zfree(&p); + pr_debug("invalid callchain context: %" PRId64 "\n", (s64) ip); + corrupted =3D true; goto exit; } continue; @@ -561,7 +561,14 @@ static char *cat_backtrace(union perf_event *event, } exit: addr_location__exit(&al); + /* + * fclose() on an open_memstream always sets p to a valid buffer, + * even if nothing was written =E2=80=94 see open_memstream(3). So p is + * never NULL after fclose and we need the flag to discard it. + */ fclose(f); + if (corrupted) + zfree(&p); =20 return p; } @@ -686,7 +693,8 @@ process_sample_power_start(struct timechart *tchart __m= aybe_unused, =20 /* perf.data is untrusted input =E2=80=94 cpu_id may be corrupted */ if (cpu_id >=3D MAX_CPUS) { - pr_debug("Out-of-bounds cpu_id %llu\n", (unsigned long long)cpu_id); + pr_debug("at offset %#" PRIx64 ": out-of-bounds cpu_id %llu\n", + sample->file_offset, (unsigned long long)cpu_id); return -1; } c_state_start(cpu_id, sample->time, value); @@ -700,7 +708,8 @@ process_sample_power_end(struct timechart *tchart, { /* perf.data is untrusted input =E2=80=94 CPU may be absent or corrupted = */ if (sample->cpu >=3D MAX_CPUS) { - pr_debug("Out-of-bounds cpu %u\n", sample->cpu); + pr_debug("at offset %#" PRIx64 ": out-of-bounds cpu %u\n", + sample->file_offset, sample->cpu); return -1; } c_state_end(tchart, sample->cpu, sample->time); @@ -717,7 +726,8 @@ process_sample_power_frequency(struct timechart *tchart, =20 /* perf.data is untrusted input =E2=80=94 cpu_id may be corrupted */ if (cpu_id >=3D MAX_CPUS) { - pr_debug("Out-of-bounds cpu_id %llu\n", (unsigned long long)cpu_id); + pr_debug("at offset %#" PRIx64 ": out-of-bounds cpu_id %llu\n", + sample->file_offset, (unsigned long long)cpu_id); return -1; } p_state_change(tchart, cpu_id, sample->time, value); --=20 2.54.0 From nobody Mon Jun 8 05:24:57 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1C1C13CAE93; Tue, 2 Jun 2026 23:57:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780444676; cv=none; b=Wz+0+xD8i7zDoOjd16Zh9f+IhA+WVOFKJXtnZDdxVUORXcshi6ExhB0qjeX3hdB3U8U7jWSokIlgwX4iNmPDJuEKqo3HPLSnZrGsxS9pBOa5W5f1F/OsZp65Yw+v/c4khQl/94fuSpYCrns1zmZzz+w6d0XMZcHZt+Kpyv43AJA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780444676; c=relaxed/simple; bh=3W+M9g0IM0OKXb/XsOgrm7hbrT6yi1m+2Bb9CW8vooc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=V5gc4u89lTTutsdUUaDC97SuOhUytPAuBxlmCVrjy1RSxNknhk88N8NOJSblA02nYiF8BxDQJQRFyz/NU2BNCSmL0NJUm0rhLkxlcbDibDPB6syV7p5f7Ez9QoLK3S1mareIwSLj4CJX8qFnoB++03pTSdwXMS5dK+o7YcgauiE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=C0TXo37p; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="C0TXo37p" Received: by smtp.kernel.org (Postfix) with ESMTPSA id ED2BD1F00898; Tue, 2 Jun 2026 23:57:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1780444674; bh=PlgokHVzlvqNn7AcOeXGgDaNmWunftIleGMUmqpUrIw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=C0TXo37pooi5t5BHR7D0doJhRFAKzmAE7EF5RzA0+kLvKmvh5e+bZHEJap801jvYO 0gLGjvwT6MKlOB5UlaQP1L0OGYYyKfBtojdK/Lw9LH0ePHse5KhN3xYQngnh6OQBUD yPN7XaoqiL837nUJU6CeNR9D7FgdwXdqINFz8iSaXjswBg2nOiDlh6UWQi8a+c0Ct4 m7QGVtYP0Omp384G0yo+jfC2772QwmetlP0DKiztYZvDyoGK0la0gS4fEKsibxAXG2 V+0tzD123Ol95//4HgGetiHVUomS72EdIa47TGJSw/qvEhXmeFvFVZJnu3WPe/R+Cr 62sW+SIVFt9DQ== From: Arnaldo Carvalho de Melo To: Namhyung Kim Cc: Ingo Molnar , Thomas Gleixner , James Clark , Jiri Olsa , Ian Rogers , Adrian Hunter , Clark Williams , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Arnaldo Carvalho de Melo , sashiko-bot@kernel.org, "Claude Opus 4.6" Subject: [PATCH 7/8] perf sched: Replace BUG_ON on invalid CPU with graceful skip Date: Tue, 2 Jun 2026 20:57:06 -0300 Message-ID: <20260602235709.1541603-8-acme@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260602235709.1541603-1-acme@kernel.org> References: <20260602235709.1541603-1-acme@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Arnaldo Carvalho de Melo latency_switch_event(), latency_runtime_event(), and map_switch_event() use BUG_ON(cpu >=3D MAX_CPUS || cpu < 0) to validate the sample CPU. When PERF_SAMPLE_CPU is absent from the sample type, evsel__parse_sample() initializes sample->cpu to (u32)-1. Casting this to int yields -1, which triggers the BUG_ON and aborts perf sched. The central CPU validation in perf_session__deliver_event() intentionally preserves the (u32)-1 sentinel for downstream tools like perf script and perf inject, so leaf callbacks must handle it themselves. Replace the three BUG_ON calls with graceful skips using pr_warning(), matching the existing pattern in process_sched_switch_event() and process_sched_runtime_event() earlier in the same file. Include the file offset for cross-referencing with perf report -D. Reported-by: sashiko-bot@kernel.org # Running on a local machine Assisted-by: Claude Opus 4.6 Signed-off-by: Arnaldo Carvalho de Melo Reviewed-by: Ian Rogers --- tools/perf/builtin-sched.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c index 9ec8e049e19b0038..81833d169470582b 100644 --- a/tools/perf/builtin-sched.c +++ b/tools/perf/builtin-sched.c @@ -1145,7 +1145,12 @@ static int latency_switch_event(struct perf_sched *s= ched, int cpu =3D sample->cpu, err =3D -1; s64 delta; =20 - BUG_ON(cpu >=3D MAX_CPUS || cpu < 0); + /* perf.data is untrusted input =E2=80=94 CPU may be absent or corrupted = */ + if (cpu >=3D MAX_CPUS || cpu < 0) { + pr_warning("WARNING: at offset %#" PRIx64 ": out-of-bound sample CPU %d,= skipping sample\n", + sample->file_offset, cpu); + return 0; + } =20 timestamp0 =3D sched->cpu_last_switched[cpu]; sched->cpu_last_switched[cpu] =3D timestamp; @@ -1215,7 +1220,13 @@ static int latency_runtime_event(struct perf_sched *= sched, if (thread =3D=3D NULL) return -1; =20 - BUG_ON(cpu >=3D MAX_CPUS || cpu < 0); + /* perf.data is untrusted input =E2=80=94 CPU may be absent or corrupted = */ + if (cpu >=3D MAX_CPUS || cpu < 0) { + pr_warning("WARNING: at offset %#" PRIx64 ": out-of-bound sample CPU %d,= skipping sample\n", + sample->file_offset, cpu); + err =3D 0; + goto out_put; + } if (!atoms) { if (thread_atoms_insert(sched, thread)) goto out_put; @@ -1640,7 +1651,12 @@ static int map_switch_event(struct perf_sched *sched= , struct perf_sample *sampl const char *str; int ret =3D -1; =20 - BUG_ON(this_cpu.cpu >=3D MAX_CPUS || this_cpu.cpu < 0); + /* perf.data is untrusted input =E2=80=94 CPU may be absent or corrupted = */ + if (this_cpu.cpu >=3D MAX_CPUS || this_cpu.cpu < 0) { + pr_warning("WARNING: at offset %#" PRIx64 ": out-of-bound sample CPU %d,= skipping sample\n", + sample->file_offset, this_cpu.cpu); + return 0; + } =20 if (this_cpu.cpu > sched->max_cpu.cpu) sched->max_cpu =3D this_cpu; --=20 2.54.0 From nobody Mon Jun 8 05:24:57 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E812D3CAE93; Tue, 2 Jun 2026 23:57:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780444681; cv=none; b=Z4a/TZdr8clJgUuMK+ztGZoj9KjR9NkFuWxqS82cQK5LkGFHQB1M9CizPbSAH8IkLGnh13m4oM5hTJUXqNG2fivoRvNi/UYB+PwxDlvEsKUNjZlX/Y4PHz2vHDdVQLdxlMH0seiUXPOTFbWRasIujCdJQRpKbxhjBihAPPOpGQI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780444681; c=relaxed/simple; bh=QtXAGfzdOo6wDZhvcxzLc7g+4g07Cn858eyEp2pWTyE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=KVFqhf+eTa7NXgqo+AODgnibw9qUAeJekYC+tN8vGZQzyk9g9Tx2nuNT7ogxNiqdToaTJgbHy+3OZgBUROxRE7045dlg4QlCrOVy7e4shN94lu51/9vbefr/ddDDAcKd8y926IFun2NnBv9gPAmdudA9LW3Q9x6zcwatd7qlwFY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=L7QGOr0m; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="L7QGOr0m" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 620071F00893; Tue, 2 Jun 2026 23:57:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1780444679; bh=eXg4kIoAYKSbZy+VQ+kEWyv7CpcOjI0CHDdUrVD0IXc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=L7QGOr0mA03g2z/lqU/LEVc3UNmBzxefEJXhVbaR1lYI2XIWBs6QZlSkFq0Yi4QWt ZMayOiOiWku378heYSqquJVlIeTDzmcppbiPqDWVOfG3w+PrBryZ1Gc3ZLwZGtuwUR 2XAnlAs96bfg2FfeBiEnyYmzjEG7hwYr1m2dERqiSU3fOOOOkcVUHRnPNcp71VD7y/ tjYRIvSzPofcdmJh03WSJJfkEDePeHuDSuQPQ0ackx/+tR8pm1MCpSlYEHG5nKRXhf 2GeuGCGs3zrFsSg4xLNsnIjVc82O4H5kAuirqsAfx8YC6fGnDTL5fvmmCD9y6NEzAz P5UJMKQ4bi9fA== From: Arnaldo Carvalho de Melo To: Namhyung Kim Cc: Ingo Molnar , Thomas Gleixner , James Clark , Jiri Olsa , Ian Rogers , Adrian Hunter , Clark Williams , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Arnaldo Carvalho de Melo , "Claude Opus 4.6" Subject: [PATCH 8/8] perf test: Add file offset diagnostic test for corrupted perf.data Date: Tue, 2 Jun 2026 20:57:07 -0300 Message-ID: <20260602235709.1541603-9-acme@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260602235709.1541603-1-acme@kernel.org> References: <20260602235709.1541603-1-acme@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Arnaldo Carvalho de Melo Add a shell test that verifies the file_offset diagnostic messages work correctly when perf encounters corrupted events. The test corrupts a MMAP2 event's size field in a recorded perf.data file, then checks that perf report produces warning messages that include both the file offset (e.g. "at offset 0x2738:") and the event type name with numeric id (e.g. "MMAP2 (10)"). This exercises the diagnostic improvements from the file_offset series, which retrofitted all skip/stop/error messages to include the position and type of the problematic event. Assisted-by: Claude Opus 4.6 Signed-off-by: Arnaldo Carvalho de Melo Reviewed-by: Ian Rogers --- .../shell/data_file_offset_diagnostics.sh | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100755 tools/perf/tests/shell/data_file_offset_diagnostics.sh diff --git a/tools/perf/tests/shell/data_file_offset_diagnostics.sh b/tools= /perf/tests/shell/data_file_offset_diagnostics.sh new file mode 100755 index 0000000000000000..031f49480d999d8c --- /dev/null +++ b/tools/perf/tests/shell/data_file_offset_diagnostics.sh @@ -0,0 +1,82 @@ +#!/bin/bash +# Test that perf report includes file offsets and event type names in diag= nostic messages. +# SPDX-License-Identifier: GPL-2.0 +# +# The file_offset diagnostic series adds "at offset 0x...: TYPE (N)" +# to all skip/stop/error messages. This test corrupts an event's size +# field in a perf.data file, then verifies the resulting warning +# includes the file offset and event type. + +err=3D0 + +cleanup() { + [ -n "${perfdata}" ] && rm -f "${perfdata}" "${perfdata}.old" + rm -f "${corrupted}" "${stderrfile}" + trap - EXIT TERM INT +} +trap 'cleanup; exit 1' TERM INT +trap cleanup EXIT + +perfdata=3D$(mktemp /tmp/__perf_test.perf.data.XXXXX) || exit 2 +corrupted=3D$(mktemp /tmp/__perf_test.perf.data.XXXXX) || exit 2 +stderrfile=3D$(mktemp /tmp/__perf_test.perf.data.XXXXX) || exit 2 + +if ! perf record -o "${perfdata}" -- perf test -w noploop 2>/dev/null; then + echo "Skip: perf record failed" + cleanup + exit 2 +fi + +# Find the file offset of the first MMAP2 event via perf report -D. +# Format: "timestamp 0xOFFSET [0xSIZE]: PERF_RECORD_MMAP2 ..." +mmap2_line=3D$(perf report -D -i "${perfdata}" 2>/dev/null | grep "PERF_RE= CORD_MMAP2" | head -1) +if [ -z "${mmap2_line}" ]; then + echo "Skip: no MMAP2 events found in perf.data" + cleanup + exit 2 +fi + +mmap2_offset=3D$(echo "${mmap2_line}" | awk '{print $2}') +mmap2_offset_dec=3D$((mmap2_offset)) + +# Copy the file and corrupt the MMAP2 event's size field. +# perf_event_header layout: type(u32) misc(u16) size(u16) +# Set size to 16 (0x10 0x00 little-endian) =E2=80=94 below the MMAP2 +# minimum, which triggers the "event size too small" warning. +cp "${perfdata}" "${corrupted}" +printf '\x10\x00' | dd of=3D"${corrupted}" bs=3D1 seek=3D$((mmap2_offset_d= ec + 6)) conv=3Dnotrunc 2>/dev/null + +perf report -i "${corrupted}" --stdio > /dev/null 2> "${stderrfile}" + +# Check that warnings include "at offset 0x..." +if grep -q "at offset 0x" "${stderrfile}"; then + echo "File offset in diagnostics [Success]" +else + echo "File offset in diagnostics [Failed: no 'at offset 0x...' found]" + echo " stderr was:" + head -5 "${stderrfile}" + err=3D1 +fi + +# Check that the event type name and numeric id appear: "MMAP2 (10)" +if grep -q "MMAP2 (10)" "${stderrfile}"; then + echo "Event type name in diagnostics [Success]" +else + echo "Event type name in diagnostics [Failed: no 'MMAP2 (10)' found]" + echo " stderr was:" + head -5 "${stderrfile}" + err=3D1 +fi + +# Check that the reported offset matches the actual corruption point +if grep -q "at offset ${mmap2_offset}:" "${stderrfile}"; then + echo "Correct offset reported [Success]" +else + echo "Correct offset reported [Failed: expected offset ${mmap2_offset}]" + echo " stderr was:" + head -5 "${stderrfile}" + err=3D1 +fi + +cleanup +exit ${err} --=20 2.54.0