From nobody Fri Dec 19 20:32:45 2025 Received: from out-176.mta1.migadu.com (out-176.mta1.migadu.com [95.215.58.176]) (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 E348F2EBDC8 for ; Tue, 14 Oct 2025 10:02:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760436140; cv=none; b=EMIhWXsNRy3WxcoYoHrK6ZS4h+N5g68EsGBy8/mJdg2Fq5GldTeBWpE6VuYQPxeeG9+9f79Zm+xc4HQisqgmjvJjYXYdoemEVRRxn5eNBxpsY+gEbQ5g/ipKi1vmem/AmJYRKVOktOrXS9pXKgzhceSHeMSL+4umqqXOGP/KyNc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760436140; c=relaxed/simple; bh=/BfyvLc3alET4VUbm8IcEp9DdnDvoMNBCmAfIzP2ToM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MeVvnNwsL16Tt0In+avrxcR38ASN6roO1fLJnhWdQTGOnwNINsCQLKqVexSebqAvmia47rji+HfjqlNNGhgX4iprcpWO/xoRjVm82dcDQUtCqeRTl5Q/pGioKkb35P2FPHEKrCGlOb9VGm7CsMLVllQ0djo+j0d7dixRqxN8TEY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=Ttdc/aAp; arc=none smtp.client-ip=95.215.58.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="Ttdc/aAp" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1760436126; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Rx3Mu1f9RcQnyEwI6I+qDuteJEIdnrA8Zyt1Wu+roas=; b=Ttdc/aApuXS/Ecc5RLs3pEQFQ5mIR2JdOhsnjhK2jySu6usKcezCqS6ZzpMEQDZ8qnBgEC OIRzmoXKz24GLHcANj5//2qSYXGYwhpxdaQC1LRi9gHOWL8MLNAnfUnFqWlfkLRzAGvli5 FqRhxZrQVUY3RZI+vtS9UNkGvXr5P6Q= From: Tao Chen To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, kan.liang@linux.intel.com, song@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, Tao Chen Subject: [RFC PATCH bpf-next v2 1/2] perf: Use extern perf_callchain_entry for get_perf_callchain Date: Tue, 14 Oct 2025 18:01:27 +0800 Message-ID: <20251014100128.2721104-2-chen.dylane@linux.dev> In-Reply-To: <20251014100128.2721104-1-chen.dylane@linux.dev> References: <20251014100128.2721104-1-chen.dylane@linux.dev> 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 X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" From bpf stack map, we want to use our own buffers to avoid unnecessary copy, so let us pass it directly. BPF will use this in the next patch. Signed-off-by: Tao Chen --- include/linux/perf_event.h | 4 ++-- kernel/bpf/stackmap.c | 4 ++-- kernel/events/callchain.c | 13 +++++++++---- kernel/events/core.c | 2 +- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index fd1d91017b9..b144da7d803 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -1719,8 +1719,8 @@ DECLARE_PER_CPU(struct perf_callchain_entry, perf_cal= lchain_entry); extern void perf_callchain_user(struct perf_callchain_entry_ctx *entry, st= ruct pt_regs *regs); extern void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, = struct pt_regs *regs); extern struct perf_callchain_entry * -get_perf_callchain(struct pt_regs *regs, bool kernel, bool user, - u32 max_stack, bool crosstask, bool add_mark); +get_perf_callchain(struct pt_regs *regs, struct perf_callchain_entry *exte= rnal_entry, + bool kernel, bool user, u32 max_stack, bool crosstask, bool add_mark); extern int get_callchain_buffers(int max_stack); extern void put_callchain_buffers(void); extern struct perf_callchain_entry *get_callchain_entry(int *rctx); diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c index 4d53cdd1374..94e46b7f340 100644 --- a/kernel/bpf/stackmap.c +++ b/kernel/bpf/stackmap.c @@ -314,7 +314,7 @@ BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, str= uct bpf_map *, map, if (max_depth > sysctl_perf_event_max_stack) max_depth =3D sysctl_perf_event_max_stack; =20 - trace =3D get_perf_callchain(regs, kernel, user, max_depth, + trace =3D get_perf_callchain(regs, NULL, kernel, user, max_depth, false, false); =20 if (unlikely(!trace)) @@ -451,7 +451,7 @@ static long __bpf_get_stack(struct pt_regs *regs, struc= t task_struct *task, else if (kernel && task) trace =3D get_callchain_entry_for_task(task, max_depth); else - trace =3D get_perf_callchain(regs, kernel, user, max_depth, + trace =3D get_perf_callchain(regs, NULL, kernel, user, max_depth, crosstask, false); =20 if (unlikely(!trace) || trace->nr < skip) { diff --git a/kernel/events/callchain.c b/kernel/events/callchain.c index 808c0d7a31f..851e8f9d026 100644 --- a/kernel/events/callchain.c +++ b/kernel/events/callchain.c @@ -217,8 +217,8 @@ static void fixup_uretprobe_trampoline_entries(struct p= erf_callchain_entry *entr } =20 struct perf_callchain_entry * -get_perf_callchain(struct pt_regs *regs, bool kernel, bool user, - u32 max_stack, bool crosstask, bool add_mark) +get_perf_callchain(struct pt_regs *regs, struct perf_callchain_entry *exte= rnal_entry, + bool kernel, bool user, u32 max_stack, bool crosstask, bool add_mark) { struct perf_callchain_entry *entry; struct perf_callchain_entry_ctx ctx; @@ -228,7 +228,11 @@ get_perf_callchain(struct pt_regs *regs, bool kernel, = bool user, if (crosstask && user && !kernel) return NULL; =20 - entry =3D get_callchain_entry(&rctx); + if (external_entry) + entry =3D external_entry; + else + entry =3D get_callchain_entry(&rctx); + if (!entry) return NULL; =20 @@ -260,7 +264,8 @@ get_perf_callchain(struct pt_regs *regs, bool kernel, b= ool user, } =20 exit_put: - put_callchain_entry(rctx); + if (!external_entry) + put_callchain_entry(rctx); =20 return entry; } diff --git a/kernel/events/core.c b/kernel/events/core.c index 7541f6f85fc..5d8e146003a 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -8217,7 +8217,7 @@ perf_callchain(struct perf_event *event, struct pt_re= gs *regs) if (!kernel && !user) return &__empty_callchain; =20 - callchain =3D get_perf_callchain(regs, kernel, user, + callchain =3D get_perf_callchain(regs, NULL, kernel, user, max_stack, crosstask, true); return callchain ?: &__empty_callchain; } --=20 2.48.1 From nobody Fri Dec 19 20:32:45 2025 Received: from out-180.mta0.migadu.com (out-180.mta0.migadu.com [91.218.175.180]) (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 6418D2E7178 for ; Tue, 14 Oct 2025 10:02:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760436152; cv=none; b=fE2mr8dTGaWVUqI7jv9ccK3f7Jwp1jk8POJlsQYIVTD+OKIykwciVbhYCxIAh52Z7Gv7ezv+Uj/H0xe4uhrybUXIsDKAq5joJSb2156j0hoY+mFuFuQRLOjxwrj85M8gc5eNxeNMXizl+A6//0v1QE5vLJaTM/JjLWtWrdrFTl0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760436152; c=relaxed/simple; bh=0wWR0ARb4iaG639FGoaQ+ToOnHpIAuK/jCQvqFiWndc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=r0UTPyRh6bcwnNZ21hozy8ZSQA6gamQIGf1oxOQSKOHOoWnO0he9nLNmng9plsBcaEhDHEhUo3spjH5Vi7fLhtoMka1mBCwnGwFkxHRhA/OCLx2lkRW5TPFXSWMGJ2NUX1NsieqeYmHY4AospseO++U0hSJH6QVaFjwloeRLceA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=BgDme0t1; arc=none smtp.client-ip=91.218.175.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="BgDme0t1" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1760436148; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=c5ndA3xQnObfPBbFY+gazZ3jKhUuFbAJBqRdxeUz2SY=; b=BgDme0t1X3IIwCSP/mSRpoFXkV/aNxj4PIHchclA87Cr0qWau48TNBkqN2fo9y4wj9yt5n qsPsoLuBt1QhnaWEAmXA4t6XBkkZ71ojvCUomi+TRKCT/57kCVNOrjENgd/bNgKXH4WDRI YotbnjI3tznYWPVWg8irAioX8w+1+YI= From: Tao Chen To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, kan.liang@linux.intel.com, song@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, Tao Chen Subject: [RFC PATCH bpf-next v2 2/2] bpf: Pass external callchain entry to get_perf_callchain Date: Tue, 14 Oct 2025 18:01:28 +0800 Message-ID: <20251014100128.2721104-3-chen.dylane@linux.dev> In-Reply-To: <20251014100128.2721104-1-chen.dylane@linux.dev> References: <20251014100128.2721104-1-chen.dylane@linux.dev> 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 X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" As Alexei noted, get_perf_callchain() return values may be reused if a task is preempted after the BPF program enters migrate disable mode. Drawing on the per-cpu design of bpf_perf_callchain_entries, stack-allocated memory of bpf_perf_callchain_entry is used here. Signed-off-by: Tao Chen --- kernel/bpf/stackmap.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c index 94e46b7f340..acd72c021c0 100644 --- a/kernel/bpf/stackmap.c +++ b/kernel/bpf/stackmap.c @@ -31,6 +31,11 @@ struct bpf_stack_map { struct stack_map_bucket *buckets[] __counted_by(n_buckets); }; =20 +struct bpf_perf_callchain_entry { + u64 nr; + u64 ip[PERF_MAX_STACK_DEPTH]; +}; + static inline bool stack_map_use_build_id(struct bpf_map *map) { return (map->map_flags & BPF_F_STACK_BUILD_ID); @@ -305,6 +310,7 @@ BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, str= uct bpf_map *, map, bool user =3D flags & BPF_F_USER_STACK; struct perf_callchain_entry *trace; bool kernel =3D !user; + struct bpf_perf_callchain_entry entry =3D { 0 }; =20 if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK | BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID))) @@ -314,12 +320,8 @@ BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, st= ruct bpf_map *, map, if (max_depth > sysctl_perf_event_max_stack) max_depth =3D sysctl_perf_event_max_stack; =20 - trace =3D get_perf_callchain(regs, NULL, kernel, user, max_depth, - false, false); - - if (unlikely(!trace)) - /* couldn't fetch the stack trace */ - return -EFAULT; + trace =3D get_perf_callchain(regs, (struct perf_callchain_entry *)&entry, + kernel, user, max_depth, false, false); =20 return __bpf_get_stackid(map, trace, flags); } @@ -412,6 +414,7 @@ static long __bpf_get_stack(struct pt_regs *regs, struc= t task_struct *task, u32 skip =3D flags & BPF_F_SKIP_FIELD_MASK; bool user =3D flags & BPF_F_USER_STACK; struct perf_callchain_entry *trace; + struct bpf_perf_callchain_entry entry =3D { 0 }; bool kernel =3D !user; int err =3D -EINVAL; u64 *ips; @@ -451,8 +454,8 @@ static long __bpf_get_stack(struct pt_regs *regs, struc= t task_struct *task, else if (kernel && task) trace =3D get_callchain_entry_for_task(task, max_depth); else - trace =3D get_perf_callchain(regs, NULL, kernel, user, max_depth, - crosstask, false); + trace =3D get_perf_callchain(regs, (struct perf_callchain_entry *)&entry, + kernel, user, max_depth, crosstask, false); =20 if (unlikely(!trace) || trace->nr < skip) { if (may_fault) --=20 2.48.1