From nobody Mon Oct 6 03:17:11 2025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DA5A82D46D3; Fri, 25 Jul 2025 09:59:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753437574; cv=none; b=lseyRK6Cdj/wF5EiZ0KIZCyknh8MUtw5NL+LLLbWj5IhyUp+0TBngsNdpBZFuuIgeFq8ewEDpQzsP5I6WlEq9/Etvrytk0zjLbdR9QEUw/d8oxvMN+qi6s0dM47rC6dV9VNqk0aGCSaBkd83JM10mwcquwE4yc7+x5Mx8EnYqsI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753437574; c=relaxed/simple; bh=5VhjTk5cRVzoBPnaiyr/CQD1RNUacBsxaN4yCOc1nkQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=PsjyRgF/VlJ7SIefR/V7/Xgz995xdxzAFwG1lPBqy2eMgiqDk04m2EB8aAHOdje+Noq8oWXrtqN02szXc/ImLG4zM6U7KDNpf+PDWbs2sXSk23OK71xJcAJqRF0WNJt6wqnVuSv3Lbx2RU14xGQIdgoKguj3oToZlKGyXw5cRCI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 8212F1A00; Fri, 25 Jul 2025 02:59:25 -0700 (PDT) Received: from e132581.arm.com (e132581.arm.com [10.1.196.87]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 470893F5A1; Fri, 25 Jul 2025 02:59:28 -0700 (PDT) From: Leo Yan Date: Fri, 25 Jul 2025 10:59:10 +0100 Subject: [PATCH PATCH v2 v3 1/6] perf/core: Make perf_event_aux_pause() as external function 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 Message-Id: <20250725-perf_aux_pause_resume_bpf_rebase-v3-1-9fc84c0f4b3a@arm.com> References: <20250725-perf_aux_pause_resume_bpf_rebase-v3-0-9fc84c0f4b3a@arm.com> In-Reply-To: <20250725-perf_aux_pause_resume_bpf_rebase-v3-0-9fc84c0f4b3a@arm.com> To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , KP Singh , Matt Bobrowski , Song Liu , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Yonghong Song , John Fastabend , Stanislav Fomichev , Hao Luo , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , James Clark , Suzuki K Poulose , Mike Leach Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Leo Yan X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1753437563; l=1435; i=leo.yan@arm.com; s=20250604; h=from:subject:message-id; bh=5VhjTk5cRVzoBPnaiyr/CQD1RNUacBsxaN4yCOc1nkQ=; b=kiSNU+vHB6WnZWaJiag6LSxlH9NMwCoNT5UcP6qc4w3S3mLtZKYI5uCvrqY47N6X2Grw2h4VV zDWbahm5T4qALFz6lGjGec5rtpp+kEZ6uGkaSNBrbvhyoYH6QryZ+dN X-Developer-Key: i=leo.yan@arm.com; a=ed25519; pk=k4BaDbvkCXzBFA7Nw184KHGP5thju8lKqJYIrOWxDhI= Expose perf_event_aux_pause() as an external function, this will be used by BPF kfunc in a sequential change. Signed-off-by: Leo Yan --- include/linux/perf_event.h | 1 + kernel/events/core.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index ec9d96025683958e909bb2463439dc69634f4ceb..9445c36e9f23e9090c7bf26c808= 5d2e4f308d38b 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -1252,6 +1252,7 @@ extern int perf_event_read_local(struct perf_event *e= vent, u64 *value, u64 *enabled, u64 *running); extern u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running); +extern void perf_event_aux_pause(struct perf_event *event, bool pause); =20 extern struct perf_callchain_entry *perf_callchain(struct perf_event *even= t, struct pt_regs *regs); =20 diff --git a/kernel/events/core.c b/kernel/events/core.c index 22fdf0c187cd970f59ad8d6de9c1b231f68ec2cb..75c194007ace911e6f91a27738c= 04dd0840bb3fb 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -8409,7 +8409,7 @@ static void __perf_event_aux_pause(struct perf_event = *event, bool pause) } } =20 -static void perf_event_aux_pause(struct perf_event *event, bool pause) +void perf_event_aux_pause(struct perf_event *event, bool pause) { struct perf_buffer *rb; =20 --=20 2.34.1 From nobody Mon Oct 6 03:17:11 2025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 36F6F2D3A86; Fri, 25 Jul 2025 09:59:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753437579; cv=none; b=Zau1eq/BL7GBlNERJPP3eKHh8utysHv40V9rXwT0Ko0qXB9agtdlAxm+qGm7HaBjTLqtgvB2PLjqTSAHPZLFtBfEv9oQuziEx33hYomd7x4+ZCXoqZLvXoSD1QOpR/gFow5860z6XRWwPdrImoMJekjQvl2ZEMQFM5FvsRSGKJc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753437579; c=relaxed/simple; bh=7cyml8Yx9LrT/GiCd2nhThh6sBVeJwNVx9qws069z5M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=pNcfE7zhsQX89Gw33WJ/Myalf3q7prn1ZBIm85Nh1h3htuCec0acyVZNn/HmhDYRQ8YlxbMCogj66fzOnKWuCZhIpxPtz/yCxivrP0Hre3rpG8WOcv/cKrtdcYLc9fFu3cgLzaejeonMXpd2htBnFr5yuzQO5UizntdOhx0Djlw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B28242C1E; Fri, 25 Jul 2025 02:59:29 -0700 (PDT) Received: from e132581.arm.com (e132581.arm.com [10.1.196.87]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 76FA73F5A1; Fri, 25 Jul 2025 02:59:32 -0700 (PDT) From: Leo Yan Date: Fri, 25 Jul 2025 10:59:11 +0100 Subject: [PATCH PATCH v2 v3 2/6] bpf: Add bpf_perf_event_aux_pause kfunc 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 Message-Id: <20250725-perf_aux_pause_resume_bpf_rebase-v3-2-9fc84c0f4b3a@arm.com> References: <20250725-perf_aux_pause_resume_bpf_rebase-v3-0-9fc84c0f4b3a@arm.com> In-Reply-To: <20250725-perf_aux_pause_resume_bpf_rebase-v3-0-9fc84c0f4b3a@arm.com> To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , KP Singh , Matt Bobrowski , Song Liu , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Yonghong Song , John Fastabend , Stanislav Fomichev , Hao Luo , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , James Clark , Suzuki K Poulose , Mike Leach Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Leo Yan X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1753437563; l=2404; i=leo.yan@arm.com; s=20250604; h=from:subject:message-id; bh=7cyml8Yx9LrT/GiCd2nhThh6sBVeJwNVx9qws069z5M=; b=JYqLML7SHSMelwq1PJ4hapZFH0FA4mFrU/XqMUt2CrldyoR2iXIciV4dD+REi7KslvDqXGTk7 8dppHPJ9OZuCGdmiCbldtU6kfpus2+oLX9L/FcopL9bg3I9yfSUiP44 X-Developer-Key: i=leo.yan@arm.com; a=ed25519; pk=k4BaDbvkCXzBFA7Nw184KHGP5thju8lKqJYIrOWxDhI= The bpf_perf_event_aux_pause kfunc is introduced for pause and resume the Perf AUX trace used by eBPF programs. An attached tracepoint (e.g., ftrace tracepoint or a dynamic tracepoint using uprobe or kprobe) can invoke bpf_perf_event_aux_pause() to pause or resume AUX trace. This is useful for fine-grained tracing. Signed-off-by: Leo Yan --- kernel/trace/bpf_trace.c | 55 ++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 55 insertions(+) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 3ae52978cae61a5d60b43c764d3e267bd32e1085..fbed1f9a92cce666e2a61d6bfce= aabfd5efade1c 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -704,6 +704,61 @@ static const struct bpf_func_proto bpf_perf_event_outp= ut_proto =3D { .arg5_type =3D ARG_CONST_SIZE_OR_ZERO, }; =20 +__bpf_kfunc_start_defs(); + +__bpf_kfunc int bpf_perf_event_aux_pause(void *p__map, u64 flags, bool pau= se) +{ + struct bpf_map *map =3D p__map; + struct bpf_array *array =3D container_of(map, struct bpf_array, map); + unsigned int cpu =3D smp_processor_id(); + u64 index =3D flags & BPF_F_INDEX_MASK; + struct bpf_event_entry *ee; + + if (map->map_type !=3D BPF_MAP_TYPE_PERF_EVENT_ARRAY) + return -EINVAL; + + /* Disabling IRQ avoids race condition with perf event flows. */ + guard(irqsave)(); + + if (unlikely(flags & ~(BPF_F_INDEX_MASK))) + return -EINVAL; + + if (index =3D=3D BPF_F_CURRENT_CPU) + index =3D cpu; + + if (unlikely(index >=3D array->map.max_entries)) + return -E2BIG; + + ee =3D READ_ONCE(array->ptrs[index]); + if (!ee) + return -ENOENT; + + if (!has_aux(ee->event)) + return -EINVAL; + + perf_event_aux_pause(ee->event, pause); + return 0; +} + +__bpf_kfunc_end_defs(); + +BTF_KFUNCS_START(perf_event_kfunc_set_ids) +BTF_ID_FLAGS(func, bpf_perf_event_aux_pause, KF_TRUSTED_ARGS) +BTF_KFUNCS_END(perf_event_kfunc_set_ids) + +static const struct btf_kfunc_id_set bpf_perf_event_kfunc_set =3D { + .owner =3D THIS_MODULE, + .set =3D &perf_event_kfunc_set_ids, +}; + +static int __init bpf_perf_event_kfuncs_init(void) +{ + return register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, + &bpf_perf_event_kfunc_set); +} + +late_initcall(bpf_perf_event_kfuncs_init); + static DEFINE_PER_CPU(int, bpf_event_output_nest_level); struct bpf_nested_pt_regs { struct pt_regs regs[3]; --=20 2.34.1 From nobody Mon Oct 6 03:17:11 2025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1FA272D7803; Fri, 25 Jul 2025 09:59:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753437582; cv=none; b=s+OEMwmU0vKngMBndwkhcO23WMk6BJnLy3mtYriwDSlza5XOaKtNo20Wo/SRsweYPsmxCy6PnBZMQbHfgAB1MqOBhlYzqqBmFAmjGM+HS6e1Ej2esZj6Erx2eQq3pEanRxVB82qa48qp5nZWchiDhlI8dMyHVadgE/SHwZHVra4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753437582; c=relaxed/simple; bh=K4Y/xektt9LHbwI/1jiSOKd8+Loae6R9dN1QrrSROJw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=G4ezmbe4y7h0UGQ6G3UFGZPvODs5cVtduSUezA3FzqN560Hyuc23fvAFeQJpl/UGVvyuAObzDeaYqWJvghAGeHygcNcp+TdC+enjK28vJcH/ZtB9iEoC93zKbitfsmJUwQ99iFhUWh2Fw1in9cRhMmAF2d4NIsszHEfeNL82oRY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id DFA8E176C; Fri, 25 Jul 2025 02:59:33 -0700 (PDT) Received: from e132581.arm.com (e132581.arm.com [10.1.196.87]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A7A823F5A1; Fri, 25 Jul 2025 02:59:36 -0700 (PDT) From: Leo Yan Date: Fri, 25 Jul 2025 10:59:12 +0100 Subject: [PATCH PATCH v2 v3 3/6] perf: auxtrace: Control AUX pause and resume with BPF 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 Message-Id: <20250725-perf_aux_pause_resume_bpf_rebase-v3-3-9fc84c0f4b3a@arm.com> References: <20250725-perf_aux_pause_resume_bpf_rebase-v3-0-9fc84c0f4b3a@arm.com> In-Reply-To: <20250725-perf_aux_pause_resume_bpf_rebase-v3-0-9fc84c0f4b3a@arm.com> To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , KP Singh , Matt Bobrowski , Song Liu , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Yonghong Song , John Fastabend , Stanislav Fomichev , Hao Luo , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , James Clark , Suzuki K Poulose , Mike Leach Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Leo Yan X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1753437563; l=4551; i=leo.yan@arm.com; s=20250604; h=from:subject:message-id; bh=K4Y/xektt9LHbwI/1jiSOKd8+Loae6R9dN1QrrSROJw=; b=1pw4YiDpzn2aTqVCxEWPImRNX+pUF2tL0d/ELRFJdOy47Mp40IGEIYmH0OanmzBszllp5+yX/ 0YZ9AT+nOLsBWPR6oVphFS96EvP72NExGLB+69VnZoJDIw55E4sMqkP X-Developer-Key: i=leo.yan@arm.com; a=ed25519; pk=k4BaDbvkCXzBFA7Nw184KHGP5thju8lKqJYIrOWxDhI= Introduce a BPF program to trigger AUX pause and resume. Once a attached tracepoint is hit, the BPF program calls the bpf_perf_event_aux_pause() kfunc for controlling AUX trace. Signed-off-by: Leo Yan --- tools/perf/Makefile.perf | 1 + tools/perf/util/bpf_skel/auxtrace_pause.bpf.c | 156 ++++++++++++++++++++++= ++++ 2 files changed, 157 insertions(+) diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf index d4c7031b01a77f4a42326e4c9a88d8a352143178..8fdd24ba4c25ff4a69925a8e0c8= 5bc78dd4fda47 100644 --- a/tools/perf/Makefile.perf +++ b/tools/perf/Makefile.perf @@ -1179,6 +1179,7 @@ SKELETONS +=3D $(SKEL_OUT)/kwork_trace.skel.h $(SKEL_= OUT)/sample_filter.skel.h SKELETONS +=3D $(SKEL_OUT)/kwork_top.skel.h $(SKEL_OUT)/syscall_summary.sk= el.h SKELETONS +=3D $(SKEL_OUT)/bench_uprobe.skel.h SKELETONS +=3D $(SKEL_OUT)/augmented_raw_syscalls.skel.h +SKELETONS +=3D $(SKEL_OUT)/auxtrace_pause.skel.h =20 $(SKEL_TMP_OUT) $(LIBAPI_OUTPUT) $(LIBBPF_OUTPUT) $(LIBPERF_OUTPUT) $(LIBS= UBCMD_OUTPUT) $(LIBSYMBOL_OUTPUT): $(Q)$(MKDIR) -p $@ diff --git a/tools/perf/util/bpf_skel/auxtrace_pause.bpf.c b/tools/perf/uti= l/bpf_skel/auxtrace_pause.bpf.c new file mode 100644 index 0000000000000000000000000000000000000000..37f8e1d096a3098415f64690146= 52dea948044d2 --- /dev/null +++ b/tools/perf/util/bpf_skel/auxtrace_pause.bpf.c @@ -0,0 +1,156 @@ +// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +// Copyright 2025 Arm Limited +#include "vmlinux.h" +#include +#include + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(__u8)); + __uint(max_entries, 1); +} cpu_filter SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(__u8)); + __uint(max_entries, 1); +} task_filter SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(int)); + __uint(max_entries, 1); +} events SEC(".maps"); + +int enabled =3D 0; + +const volatile int has_cpu =3D 0; +const volatile int has_task =3D 0; +const volatile int per_thread =3D 0; + +int bpf_perf_event_aux_pause(void *map, u64 flags, bool pause) __ksym; + +static int event_aux_pause(void) +{ + __u64 flag; + __u32 cpu =3D bpf_get_smp_processor_id(); + + if (!enabled) + return 0; + + if (has_cpu) { + __u8 *ok; + + ok =3D bpf_map_lookup_elem(&cpu_filter, &cpu); + if (!ok) + return 0; + } + + if (has_task) { + __u32 pid =3D bpf_get_current_pid_tgid() & 0xffffffff; + __u8 *ok; + + ok =3D bpf_map_lookup_elem(&task_filter, &pid); + if (!ok) + return 0; + } + + flag =3D per_thread ? 0 : BPF_F_CURRENT_CPU; + bpf_perf_event_aux_pause(&events, flag, true); + return 0; +} + +static int event_aux_resume(void) +{ + __u64 flag; + __u32 cpu =3D bpf_get_smp_processor_id(); + + if (!enabled) + return 0; + + if (has_cpu) { + __u8 *ok; + + ok =3D bpf_map_lookup_elem(&cpu_filter, &cpu); + if (!ok) + return 0; + } + + if (has_task) { + __u32 pid =3D bpf_get_current_pid_tgid() & 0xffffffff; + __u8 *ok; + + ok =3D bpf_map_lookup_elem(&task_filter, &pid); + if (!ok) + return 0; + } + + flag =3D per_thread ? 0 : BPF_F_CURRENT_CPU; + bpf_perf_event_aux_pause(&events, flag, false); + return 0; +} + +SEC("kprobe/func_pause") +int BPF_PROG(kprobe_event_pause) +{ + return event_aux_pause(); +} + +SEC("kprobe/func_resume") +int BPF_PROG(kprobe_event_resume) +{ + return event_aux_resume(); +} + +SEC("kretprobe/func_pause") +int BPF_PROG(kretprobe_event_pause) +{ + return event_aux_pause(); +} + +SEC("kretprobe/func_resume") +int BPF_PROG(kretprobe_event_resume) +{ + return event_aux_resume(); +} + +SEC("uprobe/func_pause") +int BPF_PROG(uprobe_event_pause) +{ + return event_aux_pause(); +} + +SEC("uprobe/func_resume") +int BPF_PROG(uprobe_event_resume) +{ + return event_aux_resume(); +} + +SEC("uretprobe/func_pause") +int BPF_PROG(uretprobe_event_pause) +{ + return event_aux_pause(); +} + +SEC("uretprobe/func_resume") +int BPF_PROG(uretprobe_event_resume) +{ + return event_aux_resume(); +} + +SEC("tp/func_pause") +int BPF_PROG(tp_event_pause) +{ + return event_aux_pause(); +} + +SEC("tp/func_resume") +int BPF_PROG(tp_event_resume) +{ + return event_aux_resume(); +} + +char LICENSE[] SEC("license") =3D "Dual BSD/GPL"; --=20 2.34.1 From nobody Mon Oct 6 03:17:11 2025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 451DA2749F8; Fri, 25 Jul 2025 09:59:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753437587; cv=none; b=ovKWcWmtCHKCdU1OILKK4ny2koJ4XekU6OV18LDY2iGt/3zlSxksbeWhUGliShtwsl6uGkgusS3PyvYOTvl2JDawFiBFz/wjnCmEFzJA4FWeyNlmozeam3Yo1gB8IwHw4pffpIA4ZuX8ZjgjAI7n3qCj5Ib5khUnusA3+mu8tmg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753437587; c=relaxed/simple; bh=OwmCar/KYMErQOlBy3AccdX0oNiUo/QTaV+xvmFAxKw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nt34a7ZFwHdPzk7l8RXYwJyRYQZHp+8wEmlBxUkWUhctj7jlxnqBo4JRL0vl/DbEqHGt4nK4YWujeX7YaWHd2xu+BsFErA4bJ37M0my0dI7/ndzuLTdVc5gmzoQbPStTA/sZOcDY0Hu4tBhC26nEqaVvNP6FUBVpnAeCrz8FhJg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 3D2391A00; Fri, 25 Jul 2025 02:59:38 -0700 (PDT) Received: from e132581.arm.com (e132581.arm.com [10.1.196.87]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D7A863F5A1; Fri, 25 Jul 2025 02:59:40 -0700 (PDT) From: Leo Yan Date: Fri, 25 Jul 2025 10:59:13 +0100 Subject: [PATCH PATCH v2 v3 4/6] perf: auxtrace: Add BPF userspace program for AUX pause and resume 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 Message-Id: <20250725-perf_aux_pause_resume_bpf_rebase-v3-4-9fc84c0f4b3a@arm.com> References: <20250725-perf_aux_pause_resume_bpf_rebase-v3-0-9fc84c0f4b3a@arm.com> In-Reply-To: <20250725-perf_aux_pause_resume_bpf_rebase-v3-0-9fc84c0f4b3a@arm.com> To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , KP Singh , Matt Bobrowski , Song Liu , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Yonghong Song , John Fastabend , Stanislav Fomichev , Hao Luo , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , James Clark , Suzuki K Poulose , Mike Leach Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Leo Yan X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1753437563; l=13437; i=leo.yan@arm.com; s=20250604; h=from:subject:message-id; bh=OwmCar/KYMErQOlBy3AccdX0oNiUo/QTaV+xvmFAxKw=; b=ZES5hq/JmXOk5Z/7FOKFTPQRJwC/cSKxKVdFmblkwDMy9KqLaTxCxgMukq9//U6NdBZwzaoq+ L0LhXCdmSClD6q3P6G3Ro2CUIcSQoqJOh8sVg0ud4vsp7EVDAZOgznc X-Developer-Key: i=leo.yan@arm.com; a=ed25519; pk=k4BaDbvkCXzBFA7Nw184KHGP5thju8lKqJYIrOWxDhI= This commit adds support for the BPF userspace program for AUX pause and resume. A list is maintained to track trigger points; each trigger point attaches to BPF programs when a session is opened and detaches when the session is closed. auxtrace__update_bpf_map() updates the AUX perf event pointer in the BPF map. The BPF kernel program then retrieves the event handler from the map to control AUX tracing. The auxtrace__set_bpf_filter() function updates the CPU and task filters for the BPF kernel program. Signed-off-by: Leo Yan --- tools/perf/util/Build | 4 + tools/perf/util/auxtrace.h | 43 ++++ tools/perf/util/bpf_auxtrace_pause.c | 408 +++++++++++++++++++++++++++++++= ++++ 3 files changed, 455 insertions(+) diff --git a/tools/perf/util/Build b/tools/perf/util/Build index 7910d908c814feec5e5e008f3a8b45384d796432..8ab29136344c3d37178f94aa1bd= 4b70ab54a7ab4 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -186,6 +186,10 @@ ifeq ($(CONFIG_LIBTRACEEVENT),y) perf-util-$(CONFIG_PERF_BPF_SKEL) +=3D bpf_kwork_top.o endif =20 +ifeq ($(CONFIG_AUXTRACE),y) + perf-util-$(CONFIG_PERF_BPF_SKEL) +=3D bpf_auxtrace_pause.o +endif + perf-util-$(CONFIG_LIBELF) +=3D symbol-elf.o perf-util-$(CONFIG_LIBELF) +=3D probe-file.o perf-util-$(CONFIG_LIBELF) +=3D probe-event.o diff --git a/tools/perf/util/auxtrace.h b/tools/perf/util/auxtrace.h index b0db84d27b255dc2f1aff446012598b045bbd5d3..52831e501dea1ebe476aed103a9= 20b77d400e5f7 100644 --- a/tools/perf/util/auxtrace.h +++ b/tools/perf/util/auxtrace.h @@ -907,4 +907,47 @@ void itrace_synth_opts__clear_time_range(struct itrace= _synth_opts *opts =20 #endif =20 +#if defined(HAVE_AUXTRACE_SUPPORT) && defined(HAVE_BPF_SKEL) + +int auxtrace__prepare_bpf(struct auxtrace_record *itr, const char *str); +int auxtrace__set_bpf_filter(struct evlist *evlist, struct record_opts *op= ts); +int auxtrace__enable_bpf(void); +int auxtrace__cleanup_bpf(void); +int auxtrace__update_bpf_map(struct evsel *evsel, int cpu_map_idx, int fd); + +#else /* HAVE_AUXTRACE_SUPPORT && HAVE_BPF_SKEL */ + +static inline int auxtrace__prepare_bpf(struct auxtrace_record *itr + __maybe_unused, + const char *str __maybe_unused) +{ + return -EINVAL; +} + +static inline int auxtrace__set_bpf_filter(struct evlist *evlist __maybe_u= nused, + struct record_opts *opts + __maybe_unused) +{ + return -EINVAL; +} + +static inline int auxtrace__enable_bpf(void) +{ + return -EINVAL; +} + +static inline int auxtrace__cleanup_bpf(void) +{ + return -EINVAL; +} + +static int auxtrace__update_bpf_map(struct evsel *evsel __maybe_unused, + int cpu_map_idx __maybe_unused, + int fd __maybe_unused) +{ + return -EINVAL; +} + +#endif + #endif diff --git a/tools/perf/util/bpf_auxtrace_pause.c b/tools/perf/util/bpf_aux= trace_pause.c new file mode 100644 index 0000000000000000000000000000000000000000..ed77b1e19dcf9da65cacf98def3= 49c0ce9f83d46 --- /dev/null +++ b/tools/perf/util/bpf_auxtrace_pause.c @@ -0,0 +1,408 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* Copyright 2024 Arm Limited */ + +#include +#include +#include +#include + +#include + +#include "util/auxtrace.h" +#include "util/cpumap.h" +#include "util/thread_map.h" +#include "util/debug.h" +#include "util/evlist.h" +#include "util/bpf_counter.h" +#include "util/record.h" +#include "util/target.h" + +#include "util/bpf_skel/auxtrace_pause.skel.h" + +/* The valid controlling type is "p" (pause) and "r" (resume) */ +#define is_attach_kprobe(str) \ + (!strcmp((str), "kprobe") || !strcmp((str), "kretprobe")) +#define is_attach_uprobe(str) \ + (!strcmp((str), "uprobe") || !strcmp((str), "uretprobe")) +#define is_attach_tracepoint(str) \ + (!strcmp((str), "tp") || !strcmp((str), "tracepoint")) + +/* The valid controlling type is "p" (pause) and "r" (resume) */ +#define is_valid_ctrl_type(str) \ + (!strcmp((str), "p") || !strcmp((str), "r")) + +static struct auxtrace_pause_bpf *skel; + +struct trigger_entry { + struct list_head list; + char *arg0; + char *arg1; + char *arg2; + char *arg3; +}; + +static int trigger_entry_num; +static LIST_HEAD(trigger_list); +static struct bpf_link **trigger_links; + +static void auxtrace__free_bpf_trigger_list(void) +{ + struct trigger_entry *entry, *next; + + list_for_each_entry_safe(entry, next, &trigger_list, list) { + free(entry->arg0); + free(entry->arg1); + free(entry->arg2); + free(entry->arg3); + free(entry); + } + + trigger_entry_num =3D 0; +} + +static int auxtrace__alloc_bpf_trigger_list(const char *str) +{ + char *cmd_str; + char *substr, *saveptr1; + struct trigger_entry *entry; + int ret =3D 0; + + if (!str) + return -EINVAL; + + cmd_str =3D strdup(str); + if (!cmd_str) + return -ENOMEM; + + substr =3D strtok_r(cmd_str, ",", &saveptr1); + for ( ; substr !=3D NULL; substr =3D strtok_r(NULL, ",", &saveptr1)) { + char *fmt1_str, *fmt2_str, *fmt3_str, *fmt4_str, *fmt; + + entry =3D zalloc(sizeof(*entry)); + if (!entry) { + ret =3D -ENOMEM; + goto out; + } + + /* + * A trigger is expressed with several fields with separator ":". + * The first field is specified for attach types, it can be one + * of types listed below: + * kprobe / kretprobe + * uprobe / uretprobe + * tp / tracepoint + * + * The kprobe and kretprobe trigger format is: + * {kprobe|kretprobe}:{p|r}:function_name + * + * The uprobe and uretprobe trigger format is: + * {uprobe|uretprobe}:{p|r}:executable:function_name + * + * Tracepoint trigger format is: + * {tp|tracepoint}:{p|r}:category:tracepint_name + * + * The last field is used to express the controlling type: "p" + * means aux pause and "r" is for aux resume. + */ + fmt1_str =3D strtok_r(substr, ":", &fmt); + fmt2_str =3D strtok_r(NULL, ":", &fmt); + fmt3_str =3D strtok_r(NULL, ":", &fmt); + if (!fmt1_str || !fmt2_str || !fmt3_str) { + pr_err("Failed to parse bpf aux pause string: %s\n", + substr); + ret =3D -EINVAL; + goto out; + } + + entry->arg0 =3D strdup(fmt1_str); + entry->arg1 =3D strdup(fmt2_str); + entry->arg2 =3D strdup(fmt3_str); + if (!entry->arg0 || !entry->arg1 || !entry->arg2) { + ret =3D -ENOMEM; + goto out; + } + + if (!is_attach_kprobe(entry->arg0) && + !is_attach_uprobe(entry->arg0) && + !is_attach_tracepoint(entry->arg0)) { + pr_err("Failed to support bpf aux pause attach: %s\n", + entry->arg0); + ret =3D -EINVAL; + goto out; + } + + if (!is_valid_ctrl_type(entry->arg1)) { + pr_err("Failed to support bpf aux pause ctrl: %s\n", + entry->arg1); + ret =3D -EINVAL; + goto out; + } + + if (!is_attach_kprobe(entry->arg0)) { + fmt4_str =3D strtok_r(NULL, ":", &fmt); + if (!fmt4_str) { + ret =3D -ENOMEM; + goto out; + } + + entry->arg3 =3D strdup(fmt4_str); + if (!entry->arg3) { + ret =3D -ENOMEM; + goto out; + } + } + + if (ret) + goto out; + + list_add(&entry->list, &trigger_list); + trigger_entry_num++; + } + + free(cmd_str); + return 0; + +out: + free(cmd_str); + if (entry) { + free(entry->arg0); + free(entry->arg1); + free(entry->arg2); + free(entry->arg3); + free(entry); + } + auxtrace__free_bpf_trigger_list(); + return ret; +} + +int auxtrace__prepare_bpf(struct auxtrace_record *itr, const char *str) +{ + int ret; + + if (!itr || !str) + return 0; + + skel =3D auxtrace_pause_bpf__open(); + if (!skel) { + pr_err("Failed to open func latency skeleton\n"); + return -1; + } + + ret =3D auxtrace__alloc_bpf_trigger_list(str); + if (ret) { + auxtrace_pause_bpf__destroy(skel); + skel =3D NULL; + return ret; + } + + return 0; +} + +static struct bpf_link *auxtrace__attach_bpf_prog(struct trigger_entry *en= try) +{ + struct bpf_link *link =3D NULL; + LIBBPF_OPTS(bpf_uprobe_opts, uprobe_opts); + + if (!strcmp(entry->arg0, "kprobe")) { + if (!strcmp(entry->arg1, "p")) { + link =3D bpf_program__attach_kprobe( + skel->progs.kprobe_event_pause, + false, entry->arg2); + } else if (!strcmp(entry->arg1, "r")) { + link =3D bpf_program__attach_kprobe( + skel->progs.kprobe_event_resume, + false, entry->arg2); + } + } else if (!strcmp(entry->arg0, "kretprobe")) { + if (!strcmp(entry->arg1, "p")) { + link =3D bpf_program__attach_kprobe( + skel->progs.kretprobe_event_pause, + true, entry->arg2); + } else if (!strcmp(entry->arg1, "r")) { + link =3D bpf_program__attach_kprobe( + skel->progs.kretprobe_event_resume, + true, entry->arg2); + } + } else if (!strcmp(entry->arg0, "uprobe")) { + uprobe_opts.func_name =3D entry->arg3; + uprobe_opts.retprobe =3D false; + if (!strcmp(entry->arg1, "p")) { + link =3D bpf_program__attach_uprobe_opts( + skel->progs.uprobe_event_pause, + -1, entry->arg2, 0, &uprobe_opts); + } else if (!strcmp(entry->arg1, "r")) { + link =3D bpf_program__attach_uprobe_opts( + skel->progs.uprobe_event_resume, + -1, entry->arg2, 0, &uprobe_opts); + } + } else if (!strcmp(entry->arg0, "uretprobe")) { + uprobe_opts.func_name =3D entry->arg3; + uprobe_opts.retprobe =3D true; + if (!strcmp(entry->arg1, "p")) { + link =3D bpf_program__attach_uprobe_opts( + skel->progs.uretprobe_event_pause, + -1, entry->arg2, 0, &uprobe_opts); + } else if (!strcmp(entry->arg1, "r")) { + link =3D bpf_program__attach_uprobe_opts( + skel->progs.uretprobe_event_resume, + -1, entry->arg2, 0, &uprobe_opts); + } + + } else if (is_attach_tracepoint(entry->arg0)) { + if (!strcmp(entry->arg1, "p")) { + link =3D bpf_program__attach_tracepoint( + skel->progs.tp_event_pause, + entry->arg2, entry->arg3); + } else if (!strcmp(entry->arg1, "r")) { + link =3D bpf_program__attach_tracepoint( + skel->progs.tp_event_resume, + entry->arg2, entry->arg3); + } + } + + return link; +} + +int auxtrace__set_bpf_filter(struct evlist *evlist, struct record_opts *op= ts) +{ + int fd, err; + int i, ncpus =3D 1, ntasks =3D 1; + struct trigger_entry *trigger_entry; + struct target *target; + + if (!skel) + return 0; + + if (!opts) + return -EINVAL; + + target =3D &opts->target; + + if (target__has_cpu(target)) { + ncpus =3D perf_cpu_map__nr(evlist->core.user_requested_cpus); + bpf_map__set_max_entries(skel->maps.cpu_filter, ncpus); + skel->rodata->has_cpu =3D 1; + } + + if (target__has_task(target) || target__none(target)) { + ntasks =3D perf_thread_map__nr(evlist->core.threads); + bpf_map__set_max_entries(skel->maps.task_filter, ntasks); + skel->rodata->has_task =3D 1; + } + + if (target->per_thread) + skel->rodata->per_thread =3D 1; + + bpf_map__set_max_entries(skel->maps.events, libbpf_num_possible_cpus()); + + err =3D auxtrace_pause_bpf__load(skel); + if (err) { + pr_err("Failed to load func latency skeleton: %d\n", err); + goto out; + } + + if (target__has_cpu(target)) { + u32 cpu; + u8 val =3D 1; + + fd =3D bpf_map__fd(skel->maps.cpu_filter); + + for (i =3D 0; i < ncpus; i++) { + cpu =3D perf_cpu_map__cpu(evlist->core.user_requested_cpus, i).cpu; + bpf_map_update_elem(fd, &cpu, &val, BPF_ANY); + } + } + + if (target__has_task(target) || target__none(target)) { + u32 pid; + u8 val =3D 1; + + fd =3D bpf_map__fd(skel->maps.task_filter); + + for (i =3D 0; i < ntasks; i++) { + pid =3D perf_thread_map__pid(evlist->core.threads, i); + bpf_map_update_elem(fd, &pid, &val, BPF_ANY); + } + } + + trigger_links =3D zalloc(sizeof(*trigger_links) * trigger_entry_num); + if (!trigger_links) + return -ENOMEM; + + i =3D 0; + list_for_each_entry(trigger_entry, &trigger_list, list) { + trigger_links[i] =3D auxtrace__attach_bpf_prog(trigger_entry); + err =3D libbpf_get_error(trigger_links[i]); + if (err) { + pr_err("Failed to attach bpf program to aux pause entry\n"); + pr_err(" arg0=3D%s arg1=3D%s arg2=3D%s arg3=3D%s\n", + trigger_entry->arg0, trigger_entry->arg1, + trigger_entry->arg2, trigger_entry->arg3); + trigger_links[i] =3D NULL; + goto out; + } + i++; + } + + return 0; + +out: + for (i =3D 0; i < trigger_entry_num; i++) { + if (!trigger_links[i]) + continue; + bpf_link__destroy(trigger_links[i]); + } + + return err; +} + +int auxtrace__enable_bpf(void) +{ + if (!skel) + return 0; + + skel->bss->enabled =3D 1; + return 0; +} + +int auxtrace__cleanup_bpf(void) +{ + int i; + + if (!skel) + return 0; + + for (i =3D 0; i < trigger_entry_num; i++) { + if (!trigger_links[i]) + continue; + bpf_link__destroy(trigger_links[i]); + } + + auxtrace__free_bpf_trigger_list(); + auxtrace_pause_bpf__destroy(skel); + return 0; +} + +int auxtrace__update_bpf_map(struct evsel *evsel, int cpu_map_idx, int fd) +{ + int ret; + + if (!skel) + return 0; + + if (!evsel->needs_auxtrace_mmap) + return 0; + + ret =3D bpf_map_update_elem(bpf_map__fd(skel->maps.events), + &cpu_map_idx, &fd, BPF_ANY); + if (ret) { + pr_err("Failed to update BPF map for auxtrace: %s.\n", + strerror(errno)); + if (errno =3D=3D EOPNOTSUPP) + pr_err(" Try to disable inherit mode with option '-i'.\n"); + return ret; + } + + return 0; +} --=20 2.34.1 From nobody Mon Oct 6 03:17:11 2025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B1F5F2D9EE1; Fri, 25 Jul 2025 09:59:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753437591; cv=none; b=Lo9PIV8tYHqUQtghqcH7Rwu77FYmoVTL4TQ6mH1qnpf4hEmTs4voIPZ6yEBDwxAt8IA+cJDnVVOdrvElVUyg10/RjuwaseNfpazyCiyhTZOgZeVJv0qsKCBp/iV/U+T9qhuaRCt+K524EUwK+UGD1Pqwk3f4dtNi+nSxmKxRHUA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753437591; c=relaxed/simple; bh=ifOQzVmV3VBtSmlMlao+ybmiD4XeSzgUN/D+ollwJ44=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=IlkVNanw57aNzVB/1xEo1n9OjfcMxkHsf3/r/S+Ff6vMbK45iR5YOwamFIkiDih2rB2vCBhP8KxAHCbMY/wmJMTC2vaujfaw1T1/z+cVPGsIy/h1x2LZS6CQzF/mcv5KFvhrf4iAsjXXUeNO2yjCUNZ8PtjLAflElGLsUK0M6K8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6A0652C1E; Fri, 25 Jul 2025 02:59:42 -0700 (PDT) Received: from e132581.arm.com (e132581.arm.com [10.1.196.87]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 322D93F5A1; Fri, 25 Jul 2025 02:59:45 -0700 (PDT) From: Leo Yan Date: Fri, 25 Jul 2025 10:59:14 +0100 Subject: [PATCH PATCH v2 v3 5/6] perf record: Support AUX pause and resume with BPF 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 Message-Id: <20250725-perf_aux_pause_resume_bpf_rebase-v3-5-9fc84c0f4b3a@arm.com> References: <20250725-perf_aux_pause_resume_bpf_rebase-v3-0-9fc84c0f4b3a@arm.com> In-Reply-To: <20250725-perf_aux_pause_resume_bpf_rebase-v3-0-9fc84c0f4b3a@arm.com> To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , KP Singh , Matt Bobrowski , Song Liu , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Yonghong Song , John Fastabend , Stanislav Fomichev , Hao Luo , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , James Clark , Suzuki K Poulose , Mike Leach Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Leo Yan X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1753437563; l=4080; i=leo.yan@arm.com; s=20250604; h=from:subject:message-id; bh=ifOQzVmV3VBtSmlMlao+ybmiD4XeSzgUN/D+ollwJ44=; b=sZLYcv07+IXjUiorbYN/M8Y+RLK5sgKTYn5u4BFju7OsEKZkL/t+I+WxfkgXsvab6jTlrMat4 yS+DmyllY0kAIAf7oMrCipw90jk4w70kZDyBg4cs/ZdZnKaOUIT9kBh X-Developer-Key: i=leo.yan@arm.com; a=ed25519; pk=k4BaDbvkCXzBFA7Nw184KHGP5thju8lKqJYIrOWxDhI= This commit introduces an option "--bpf-aux-pause" for loading BPF program to trigger AUX pause and resume. After: perf record -e cs_etm/aux-action=3Dstart-paused/ \ --bpf-aux-pause=3D"kretprobe:p:__arm64_sys_openat,kprobe:r:__arm64_sys_= openat,tp:r:sched:sched_switch" \ -a -- ls perf record -e cs_etm/aux-action=3Dstart-paused/ \ --bpf-aux-pause=3D"kretprobe:p:__arm64_sys_openat,kprobe:r:__arm64_sys_= openat,tp:r:sched:sched_switch" \ --per-thread -- ls Signed-off-by: Leo Yan --- tools/perf/builtin-record.c | 20 +++++++++++++++++++- tools/perf/util/evsel.c | 6 ++++++ tools/perf/util/record.h | 1 + 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 8059bce85a510b2dddc66f1b8b0013276840eddc..793609c0a59e8abcd248608f55e= 0af03a253138d 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -880,7 +880,12 @@ static int record__auxtrace_init(struct record *rec) if (err) return err; =20 - return auxtrace_parse_filters(rec->evlist); + err =3D auxtrace_parse_filters(rec->evlist); + if (err) + return err; + + return auxtrace__prepare_bpf(rec->itr, + rec->opts.auxtrace_bpf_aux_pause_opts); } =20 #else @@ -2506,6 +2511,10 @@ static int __cmd_record(struct record *rec, int argc= , const char **argv) =20 evlist__config(rec->evlist, opts, &callchain_param); =20 + err =3D auxtrace__set_bpf_filter(rec->evlist, opts); + if (err) + goto out_free_threads; + /* Debug message used by test scripts */ pr_debug3("perf record opening and mmapping events\n"); if (record__open(rec) !=3D 0) { @@ -2579,6 +2588,9 @@ static int __cmd_record(struct record *rec, int argc,= const char **argv) if (record__start_threads(rec)) goto out_free_threads; =20 + if (auxtrace__enable_bpf()) + goto out_free_threads; + /* * When perf is starting the traced process, all the events * (apart from group members) have enable_on_exec=3D1 set, @@ -2907,6 +2919,7 @@ static int __cmd_record(struct record *rec, int argc,= const char **argv) } =20 out_delete_session: + auxtrace__cleanup_bpf(); #ifdef HAVE_EVENTFD_SUPPORT if (done_fd >=3D 0) { fd =3D done_fd; @@ -3629,6 +3642,11 @@ static struct option __record_options[] =3D { OPT_CALLBACK(0, "off-cpu-thresh", &record.opts, "ms", "Dump off-cpu samples if off-cpu time exceeds this threshold (in mi= lliseconds). (Default: 500ms)", record__parse_off_cpu_thresh), + OPT_STRING_OPTARG(0, "bpf-aux-pause", &record.opts.auxtrace_bpf_aux_pause= _opts, + "{kprobe|kretprobe}:{p|r}:function_name\n" + "\t\t\t {uprobe|uretprobe}:{p|r}:executable:function_name\n" + "\t\t\t {tp|tracepoint}:{p|r}:category:tracepoint\n", + "Enable AUX pause with BPF backend", ""), OPT_END() }; =20 diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index d55482f094bf95ac7b5c5173c1341baeb0fa9c93..f240e48f41a3e7ca5ba81733efc= 58a25c5c829ba 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -2650,6 +2650,12 @@ static int evsel__open_cpu(struct evsel *evsel, stru= ct perf_cpu_map *cpus, =20 bpf_counter__install_pe(evsel, idx, fd); =20 + /* Update event info into BPF map for AUX trace */ + if (auxtrace__update_bpf_map(evsel, idx, fd)) { + err =3D -EINVAL; + goto out_close; + } + if (unlikely(test_attr__enabled())) { test_attr__open(&evsel->core.attr, pid, cpu, fd, group_fd, evsel->open_flags); diff --git a/tools/perf/util/record.h b/tools/perf/util/record.h index ea3a6c4657eefb743dc3d54b0b791ea39117cc10..1b1c2ed7fcadae8b56408b3b1b1= 54faef3996eb3 100644 --- a/tools/perf/util/record.h +++ b/tools/perf/util/record.h @@ -65,6 +65,7 @@ struct record_opts { size_t auxtrace_snapshot_size; const char *auxtrace_snapshot_opts; const char *auxtrace_sample_opts; + const char *auxtrace_bpf_aux_pause_opts; bool sample_transaction; bool use_clockid; clockid_t clockid; --=20 2.34.1 From nobody Mon Oct 6 03:17:11 2025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 043FA2D375D; Fri, 25 Jul 2025 09:59:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753437595; cv=none; b=eKSZK9ewrLXBLGqxxmwBPwg5c73CqnDU9kocfp7rz+KH0JTXJrU0qulzoiu7RgRs9e7yucsOhlGkpzkaghDH5FEZZ7W21/v8a+cHjIaEWTQNm3GyfBOsWKbqpFzPD3BO6HOUOI/wzOTLSYZcsfbaRTzqKk9HHRSlwXjuiqPIJVg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753437595; c=relaxed/simple; bh=+3gjGeeKe7DyZ3/goDLLn+ZLf1jbvy7KWHPofuCLFbE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=oOZUPnGuVQrXdmI0wOdVe+uitM10rUcSfCJunsi60B7g4CTvyfi/w1pLk5JKfLtojqFCspemCn0gXBiyyUMGALOXTGmz2Gs1BkLTfPdq/Aw1K8yqD5mGVDLiepZfybK8MhF+zlN/kkmaJmM7WLCZmD+dnqk4WR8hslnELedk4M8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 99BA6176C; Fri, 25 Jul 2025 02:59:46 -0700 (PDT) Received: from e132581.arm.com (e132581.arm.com [10.1.196.87]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 623723F5A1; Fri, 25 Jul 2025 02:59:49 -0700 (PDT) From: Leo Yan Date: Fri, 25 Jul 2025 10:59:15 +0100 Subject: [PATCH PATCH v2 v3 6/6] perf docs: Document AUX pause and resume with BPF 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 Message-Id: <20250725-perf_aux_pause_resume_bpf_rebase-v3-6-9fc84c0f4b3a@arm.com> References: <20250725-perf_aux_pause_resume_bpf_rebase-v3-0-9fc84c0f4b3a@arm.com> In-Reply-To: <20250725-perf_aux_pause_resume_bpf_rebase-v3-0-9fc84c0f4b3a@arm.com> To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , KP Singh , Matt Bobrowski , Song Liu , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Yonghong Song , John Fastabend , Stanislav Fomichev , Hao Luo , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , James Clark , Suzuki K Poulose , Mike Leach Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Leo Yan X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1753437563; l=3165; i=leo.yan@arm.com; s=20250604; h=from:subject:message-id; bh=+3gjGeeKe7DyZ3/goDLLn+ZLf1jbvy7KWHPofuCLFbE=; b=YZ1S6Xoav56pMejraXEEdJ6z6iYmHtig2FFXReV2g8kwgTJFMV1s33x61Zo+WUvJxzlYQuKYf k6MBoxmqGrgDF3E/UM3KSyUGdGEoZooxShRtmAM5G1qotRqgSg/AONW X-Developer-Key: i=leo.yan@arm.com; a=ed25519; pk=k4BaDbvkCXzBFA7Nw184KHGP5thju8lKqJYIrOWxDhI= Documents the usage of the --bpf-aux-pause option and provides examples. Signed-off-by: Leo Yan --- tools/perf/Documentation/perf-record.txt | 51 ++++++++++++++++++++++++++++= ++++ 1 file changed, 51 insertions(+) diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Document= ation/perf-record.txt index 612612fa2d8041b94860035ed9cb01557a20b6b7..5aee20bfd03bda72bddabf42005= b9678309414ad 100644 --- a/tools/perf/Documentation/perf-record.txt +++ b/tools/perf/Documentation/perf-record.txt @@ -544,6 +544,57 @@ must be an AUX area event. Samples on other events wil= l be created containing data from the AUX area. Optionally sample size may be specified, otherwise= it defaults to 4KiB. =20 +--bpf-aux-pause=3D[=3DOPTIONS]:: +Specify trace events for triggering AUX pause with a BPF program. A trace = event +can be static ftrace tracepoint, or dynamic tracepoint by using kprobe, +kretprobe, uprobe or uretprobe. This option must be enabled in combination= with +the "aux-action=3Dstart-paused" configuration in an AUX event. + +For attaching a kprobe or kretprobe event, the format is: + + {kprobe|kretprobe}:{p|r}:function_name + +The format for attaching a uprobe or uretprobe event is: + + {uprobe|uretprobe}:{p|r}:executable:function_name + +The format for attaching a tracepoint is: + + {tp|tracepoint}:{p|r}:category:tracepint + +The first field is for the trace event type. It supports five types: kprob= e, +kretprobe, uprobe, uretprobe, and tracepoint ('tp' is also supported as an +abbreviation for "tracepoint"). The second field specifies whether the act= ion is +pause ("p") or resume ("r"). + +For probes, the "function_name" field is used to specify a function name. = In +particular, for a uprobe or uretprobe, an executable path must also be pro= vided. +In the case of a ftrace tracepoint, the "category" and "tracepoint" fields= are +used together to provide complete tracepoint information. + +The '--bpf-aux-pause' option does not support inherit mode. In the default +trace mode, it needs to be combined with the '-i' or '--no-inherit' option= to +disable inherit mode. + +The syntax supports multiple trace events, with each separated by a comma = (,). +For example, users can set up AUX pause on a kernel function with kretprob= e and +AUX resume on a tracepoint with the syntax below: + + For default trace mode (with inherit mode disabled): + perf record -e cs_etm/aux-action=3Dstart-paused/ \ + --bpf-aux-pause=3D"kretprobe:p:__arm64_sys_openat,tp:r:sched:sched_swi= tch" \ + -i ... + + For system wide trace mode: + perf record -e cs_etm/aux-action=3Dstart-paused/ \ + --bpf-aux-pause=3D"kretprobe:p:__arm64_sys_openat,tp:r:sched:sched_swi= tch" \ + -a ... + + For trace with uprobe and uretprobe: + perf record -e cs_etm/aux-action=3Dstart-paused/ \ + --bpf-aux-pause=3D"uretprobe:p:~/sort:bubble_sort,uprobe:r:~/sort:bubb= le_sort" \ + -i --per-thread -- ~/sort + --proc-map-timeout:: When processing pre-existing threads /proc/XXX/mmap, it may take a long ti= me, because the file may be huge. A time out is needed in such cases. --=20 2.34.1