From nobody Fri Dec 19 09:30:39 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 13F9023E34F for ; Fri, 6 Jun 2025 21:52:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749246778; cv=none; b=fJav6VAEi66cAANs1Tj82ccDkEFLj+4legnFteqIEXgmOZ2/okOMf8QAIqaoJ44N5ir4K27Ot58zv6jJammZQP9sqmhu+HUfg99gBOd7/Z0eF18AYd0iA3/rt99pmd9/WUL7PL5QiQKLCuwfsUcnUo/mjdRqQ2WSDcmUlEyW/ZA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749246778; c=relaxed/simple; bh=OhUv3r9YK7S2vWeklKY7wXIyczrwBkYk68z5KVxE38M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZCfnAMpvmuW3bTGV85uTwcpsrmqYuAhc8+BM/xwuLVOVY7CYIAUWm49rSv6u7Xe+8mh5BwS4S0cWdVFWovWHsPl3nGLT1TUMqogUiOXuWVSRJn9MsU0qConIsHnueVZeoTk16BZKcKTakOQ38WGbNzL5izYz4ltClSjStzsOjTU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--blakejones.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=4qPF8LeJ; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--blakejones.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="4qPF8LeJ" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-234f1acc707so22378385ad.3 for ; Fri, 06 Jun 2025 14:52:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1749246776; x=1749851576; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=JS/Db2SUjK5/g3LbclVsWOHT1Smx8t5HOzo/CvKXbp4=; b=4qPF8LeJGIuGPOEENYNF/0NRGd0KoYHWcEFG7U3ywnkDjaD1Hd4OyKdRSlBgwzCEh7 OiSvEHG2lAjh/Ak2x4mPQ0Ylqe3m7EkiZQ5Dq8TAXJB39jjb3hgplQy+6iGtB18MB5zy m8KXh8MZxEc5g+466Qx5aUngHFKsPLM5QleIQ+PRh+ktmI0MrUZ0lZFfOpmI/uuO2Ha6 iNq25gFrPFg+IY6hUl9Y8IzTawYzE0fZuHbJ1kdacV0pyza8hBbSgnWcwrZ22F9ikcci bA1BIdo3ktOzLTtrO3ACI+cBaVoazqUx1I6BdFNI2XqCHYh8oHNGLoc1jYAjC1TpT/OZ FUaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749246776; x=1749851576; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JS/Db2SUjK5/g3LbclVsWOHT1Smx8t5HOzo/CvKXbp4=; b=sNt7gqf2Q7Tf9IZVtEOYTTbDyQ/vpL9Ecx/1lBomfonkqcKTjge4rGfi3iChXbuJq+ gC6AiDeIHYYrFKJPppYKfnxZUct8a5+LCOonyplHz0b0wTLdJsntAQyTXQrWSjFPwg6w WhlMT4ToNPS2P4ot3QJz1KxcDZfJ6TAJp9pSmWkJahQxp2ukqk0uoAr0r9t+SOa825h5 MKJXn0gbMSfbTa7yfG8Jv+Nrnj5L9VJMHaAIqFWYAO0WP8lWOyordO9E20Sh+4J3KWTa TfE5SD6Vr9x9jIZel6z1CSj7V0FF0BrH2h9zD4YScs6rQi+gy09nMArMGxb9xOLnRwSQ mP1A== X-Forwarded-Encrypted: i=1; AJvYcCWo1np2/QbLg3NumSXg7hIRILmexBKMZpPqPJoPeOIRsko4l3UkhucImWZdx3dq6NUUE87SayjWROphfM0=@vger.kernel.org X-Gm-Message-State: AOJu0Yyb/eBFYA9ptI7VeHOtw1tmceBYwkMbAqFi9XRF+TBSuj8st6d0 0jLSGwKXZul+/uFhxPJtstWy8hAw5zLnBRJ7fiOWL2NKt6qWuBYWw+4bD8xFYqLI3qBn7Hp0vmb JapTI5CsBFwGUkcVvP/sCsw== X-Google-Smtp-Source: AGHT+IGlHDIsXQ6iCILcEd7JatNtcpZ/R5Fn6/EzRFCZispX4sWEu4cmFjgXt5Xg05e2D5V/72S4Vg3WxP0+mCk4 X-Received: from plnz4.prod.google.com ([2002:a17:902:8344:b0:234:d2de:e601]) (user=blakejones job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:244c:b0:235:f3df:bc1f with SMTP id d9443c01a7336-23601dc4688mr76887345ad.36.1749246776340; Fri, 06 Jun 2025 14:52:56 -0700 (PDT) Date: Fri, 6 Jun 2025 14:52:44 -0700 In-Reply-To: <20250606215246.2419387-1-blakejones@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250606215246.2419387-1-blakejones@google.com> X-Mailer: git-send-email 2.50.0.rc0.604.gd4ff7b7c86-goog Message-ID: <20250606215246.2419387-4-blakejones@google.com> Subject: [PATCH v3 3/5] perf: collect BPF metadata from new programs From: Blake Jones To: Arnaldo Carvalho de Melo , Namhyung Kim , Ian Rogers , Jiri Olsa , Peter Zijlstra , Ingo Molnar Cc: Mark Rutland , Alexander Shishkin , Adrian Hunter , Kan Liang , Steven Rostedt , Tomas Glozar , James Clark , Leo Yan , Guilherme Amadio , Yang Jihong , Charlie Jenkins , Chun-Tse Shao , Aditya Gupta , Athira Rajeev , Zhongqiu Han , Andi Kleen , Dmitry Vyukov , Yujie Liu , Graham Woodward , Yicong Yang , Ben Gainey , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, Blake Jones Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This collects metadata for any BPF programs that were loaded during a "perf record" run, and emits it at the end of the run. Signed-off-by: Blake Jones --- tools/perf/builtin-record.c | 8 ++++++ tools/perf/util/bpf-event.c | 46 ++++++++++++++++++++++++++++++ tools/perf/util/bpf-event.h | 1 + tools/perf/util/env.c | 19 +++++++++++- tools/perf/util/env.h | 4 +++ tools/perf/util/header.c | 1 + tools/perf/util/synthetic-events.h | 2 ++ 7 files changed, 80 insertions(+), 1 deletion(-) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 0b566f300569..47adee0c2a1c 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -2162,6 +2162,12 @@ static int record__synthesize(struct record *rec, bo= ol tail) return err; } =20 +static void record__synthesize_final_bpf_metadata(struct record *rec) +{ + perf_event__synthesize_final_bpf_metadata(rec->session, + process_synthesized_event); +} + static int record__process_signal_event(union perf_event *event __maybe_un= used, void *data) { struct record *rec =3D data; @@ -2807,6 +2813,8 @@ static int __cmd_record(struct record *rec, int argc,= const char **argv) trigger_off(&auxtrace_snapshot_trigger); trigger_off(&switch_output_trigger); =20 + record__synthesize_final_bpf_metadata(rec); + if (opts->auxtrace_snapshot_on_exit) record__auxtrace_snapshot_exit(rec); =20 diff --git a/tools/perf/util/bpf-event.c b/tools/perf/util/bpf-event.c index 1f6e76ee6024..dc09a4730c50 100644 --- a/tools/perf/util/bpf-event.c +++ b/tools/perf/util/bpf-event.c @@ -472,6 +472,49 @@ void bpf_metadata_free(struct bpf_metadata *metadata _= _maybe_unused) =20 #endif /* HAVE_LIBBPF_STRINGS_SUPPORT */ =20 +struct bpf_metadata_final_ctx { + const struct perf_tool *tool; + perf_event__handler_t process; + struct machine *machine; +}; + +static void synthesize_final_bpf_metadata_cb(struct bpf_prog_info_node *no= de, + void *data) +{ + struct bpf_metadata_final_ctx *ctx =3D (struct bpf_metadata_final_ctx *)d= ata; + struct bpf_metadata *metadata =3D node->metadata; + int err; + + if (metadata =3D=3D NULL) + return; + err =3D synthesize_perf_record_bpf_metadata(metadata, ctx->tool, + ctx->process, ctx->machine); + if (err !=3D 0) { + const char *prog_name =3D metadata->prog_names[0]; + + if (prog_name !=3D NULL) + pr_warning("Couldn't synthesize final BPF metadata for %s.\n", prog_nam= e); + else + pr_warning("Couldn't synthesize final BPF metadata.\n"); + } + bpf_metadata_free(metadata); + node->metadata =3D NULL; +} + +void perf_event__synthesize_final_bpf_metadata(struct perf_session *sessio= n, + perf_event__handler_t process) +{ + struct perf_env *env =3D &session->header.env; + struct bpf_metadata_final_ctx ctx =3D { + .tool =3D session->tool, + .process =3D process, + .machine =3D &session->machines.host, + }; + + perf_env__iterate_bpf_prog_info(env, synthesize_final_bpf_metadata_cb, + &ctx); +} + /* * Synthesize PERF_RECORD_KSYMBOL and PERF_RECORD_BPF_EVENT for one bpf * program. One PERF_RECORD_BPF_EVENT is generated for the program. And @@ -612,6 +655,7 @@ static int perf_event__synthesize_one_bpf_prog(struct p= erf_session *session, } =20 info_node->info_linear =3D info_linear; + info_node->metadata =3D NULL; if (!perf_env__insert_bpf_prog_info(env, info_node)) { free(info_linear); free(info_node); @@ -803,6 +847,7 @@ static void perf_env__add_bpf_info(struct perf_env *env= , u32 id) arrays |=3D 1UL << PERF_BPIL_JITED_INSNS; arrays |=3D 1UL << PERF_BPIL_LINE_INFO; arrays |=3D 1UL << PERF_BPIL_JITED_LINE_INFO; + arrays |=3D 1UL << PERF_BPIL_MAP_IDS; =20 info_linear =3D get_bpf_prog_info_linear(fd, arrays); if (IS_ERR_OR_NULL(info_linear)) { @@ -815,6 +860,7 @@ static void perf_env__add_bpf_info(struct perf_env *env= , u32 id) info_node =3D malloc(sizeof(struct bpf_prog_info_node)); if (info_node) { info_node->info_linear =3D info_linear; + info_node->metadata =3D bpf_metadata_create(&info_linear->info); if (!perf_env__insert_bpf_prog_info(env, info_node)) { free(info_linear); free(info_node); diff --git a/tools/perf/util/bpf-event.h b/tools/perf/util/bpf-event.h index 16644b3aaba1..1ed0b36dc3b8 100644 --- a/tools/perf/util/bpf-event.h +++ b/tools/perf/util/bpf-event.h @@ -25,6 +25,7 @@ struct bpf_metadata { =20 struct bpf_prog_info_node { struct perf_bpil *info_linear; + struct bpf_metadata *metadata; struct rb_node rb_node; }; =20 diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c index 36411749e007..05a4f2657d72 100644 --- a/tools/perf/util/env.c +++ b/tools/perf/util/env.c @@ -3,8 +3,10 @@ #include "debug.h" #include "env.h" #include "util/header.h" -#include "linux/compiler.h" +#include "util/rwsem.h" +#include #include +#include #include #include #include "cgroup.h" @@ -89,6 +91,20 @@ struct bpf_prog_info_node *perf_env__find_bpf_prog_info(= struct perf_env *env, return node; } =20 +void perf_env__iterate_bpf_prog_info(struct perf_env *env, + void (*cb)(struct bpf_prog_info_node *node, + void *data), + void *data) +{ + struct rb_node *first; + + down_read(&env->bpf_progs.lock); + first =3D rb_first(&env->bpf_progs.infos); + for (struct rb_node *node =3D first; node !=3D NULL; node =3D rb_next(nod= e)) + (*cb)(rb_entry(node, struct bpf_prog_info_node, rb_node), data); + up_read(&env->bpf_progs.lock); +} + bool perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node) { bool ret; @@ -174,6 +190,7 @@ static void perf_env__purge_bpf(struct perf_env *env) next =3D rb_next(&node->rb_node); rb_erase(&node->rb_node, root); zfree(&node->info_linear); + bpf_metadata_free(node->metadata); free(node); } =20 diff --git a/tools/perf/util/env.h b/tools/perf/util/env.h index d90e343cf1fa..6819cb9b99ff 100644 --- a/tools/perf/util/env.h +++ b/tools/perf/util/env.h @@ -180,6 +180,10 @@ bool perf_env__insert_bpf_prog_info(struct perf_env *e= nv, struct bpf_prog_info_node *info_node); struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *e= nv, __u32 prog_id); +void perf_env__iterate_bpf_prog_info(struct perf_env *env, + void (*cb)(struct bpf_prog_info_node *node, + void *data), + void *data); bool perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node); bool __perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_nod= e); struct btf_node *perf_env__find_btf(struct perf_env *env, __u32 btf_id); diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index e3cdc3b7b4ab..7c477e2a93b3 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -3161,6 +3161,7 @@ static int process_bpf_prog_info(struct feat_fd *ff, = void *data __maybe_unused) /* after reading from file, translate offset to address */ bpil_offs_to_addr(info_linear); info_node->info_linear =3D info_linear; + info_node->metadata =3D NULL; if (!__perf_env__insert_bpf_prog_info(env, info_node)) { free(info_linear); free(info_node); diff --git a/tools/perf/util/synthetic-events.h b/tools/perf/util/synthetic= -events.h index b9c936b5cfeb..ee29615d68e5 100644 --- a/tools/perf/util/synthetic-events.h +++ b/tools/perf/util/synthetic-events.h @@ -92,6 +92,8 @@ int perf_event__synthesize_threads(const struct perf_tool= *tool, perf_event__han int perf_event__synthesize_tracing_data(const struct perf_tool *tool, int = fd, struct evlist *evlist, perf_event__handler_t process); int perf_event__synth_time_conv(const struct perf_event_mmap_page *pc, con= st struct perf_tool *tool, perf_event__handler_t process, struct machine *m= achine); pid_t perf_event__synthesize_comm(const struct perf_tool *tool, union perf= _event *event, pid_t pid, perf_event__handler_t process, struct machine *ma= chine); +void perf_event__synthesize_final_bpf_metadata(struct perf_session *sessio= n, + perf_event__handler_t process); =20 int perf_tool__process_synth_event(const struct perf_tool *tool, union per= f_event *event, struct machine *machine, perf_event__handler_t process); =20 --=20 2.50.0.rc0.604.gd4ff7b7c86-goog