From nobody Sat Feb 7 11:31:46 2026 Received: from out-179.mta1.migadu.com (out-179.mta1.migadu.com [95.215.58.179]) (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 0D5632E7166 for ; Mon, 26 Jan 2026 07:45:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769413516; cv=none; b=A9GnNlTqq0tyGzrrkFZH/zZI5O3jX2u/OKdRBoIPAH0HBvGiGcMRiAIIGXFjZwYm5cGxgG3jnqBjNEVLi9xD/d/OFdnWLLGifAVF4GF1m+E9FVZVWjDJVpy20/yDwWhOjoXyxGebjFPBAhiTZLuA2dWjdXEzmEGcAGaKR1gEfAw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769413516; c=relaxed/simple; bh=2OU+QeH0wHZFhok3gxIeOcNWQdtwUfU5x9jijEipvmM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=vGkXw0itmnIyZYUYsamH+fYcdbdLbO8JhIE6ymGVeQCLOc8jRrRS4LtO9w2UUYnBPN3vQZzkQnP5mHd0CEyDagA9dHifLxpyp521gMnKXfZxUGPQpYJu/c24ueZ5kLI6phDxDJtTNy/9XY8iUhSlJU3NFCRB65pZAXEU5ZGklTo= 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=BUlSSZnw; arc=none smtp.client-ip=95.215.58.179 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="BUlSSZnw" 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=1769413513; 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=EzuoEG3Lq0uyN71/5JLffXaHG9gMZoBp7DXFfx+Dmdw=; b=BUlSSZnwl61hfr1AgSUt6Qn8SkEs5JjwFF35/+SzZyaYUdI5UzxQOz0l+RM90TxM21ZtCW dFx2RV/gYWQVsfrEzGBTjxu+zIKfVjKSYTSwcSvZvPM//dcJYQnO5svuKVdU0X65Du7EHg YWv7bY3+lTsZpp+EyIQoL+/WOu1EG8g= 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: [PATCH bpf-next v8 1/3] perf: Add rctx in perf_callchain_entry Date: Mon, 26 Jan 2026 15:43:29 +0800 Message-ID: <20260126074331.815684-2-chen.dylane@linux.dev> In-Reply-To: <20260126074331.815684-1-chen.dylane@linux.dev> References: <20260126074331.815684-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" Record rctx inside the perf_callchain_entry itself, when callers of get_callchain_entry no longer care about the assignment of rctx, and will be used in the next patch. Suggested-by: Andrii Nakryiko Signed-off-by: Tao Chen --- include/linux/perf_event.h | 5 +++-- kernel/bpf/stackmap.c | 5 ++--- kernel/events/callchain.c | 27 ++++++++++++++++----------- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 9870d768db4..f0489843ebc 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -57,6 +57,7 @@ #include =20 struct perf_callchain_entry { + int rctx; u64 nr; u64 ip[]; /* /proc/sys/kernel/perf_event_max_stack */ }; @@ -1723,8 +1724,8 @@ get_perf_callchain(struct pt_regs *regs, bool kernel,= bool user, u32 max_stack, bool crosstask, bool add_mark, u64 defer_cookie); extern int get_callchain_buffers(int max_stack); extern void put_callchain_buffers(void); -extern struct perf_callchain_entry *get_callchain_entry(int *rctx); -extern void put_callchain_entry(int rctx); +extern struct perf_callchain_entry *get_callchain_entry(void); +extern void put_callchain_entry(struct perf_callchain_entry *entry); =20 extern int sysctl_perf_event_max_stack; extern int sysctl_perf_event_max_contexts_per_stack; diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c index da3d328f5c1..e77dcdc2164 100644 --- a/kernel/bpf/stackmap.c +++ b/kernel/bpf/stackmap.c @@ -214,9 +214,8 @@ get_callchain_entry_for_task(struct task_struct *task, = u32 max_depth) { #ifdef CONFIG_STACKTRACE struct perf_callchain_entry *entry; - int rctx; =20 - entry =3D get_callchain_entry(&rctx); + entry =3D get_callchain_entry(); =20 if (!entry) return NULL; @@ -238,7 +237,7 @@ get_callchain_entry_for_task(struct task_struct *task, = u32 max_depth) to[i] =3D (u64)(from[i]); } =20 - put_callchain_entry(rctx); + put_callchain_entry(entry); =20 return entry; #else /* CONFIG_STACKTRACE */ diff --git a/kernel/events/callchain.c b/kernel/events/callchain.c index b9c7e00725d..6cdbc5937b1 100644 --- a/kernel/events/callchain.c +++ b/kernel/events/callchain.c @@ -151,31 +151,36 @@ void put_callchain_buffers(void) } } =20 -struct perf_callchain_entry *get_callchain_entry(int *rctx) +struct perf_callchain_entry *get_callchain_entry(void) { int cpu; + int rctx; struct callchain_cpus_entries *entries; + struct perf_callchain_entry *entry; =20 - *rctx =3D get_recursion_context(this_cpu_ptr(callchain_recursion)); - if (*rctx =3D=3D -1) + rctx =3D get_recursion_context(this_cpu_ptr(callchain_recursion)); + if (rctx =3D=3D -1) return NULL; =20 entries =3D rcu_dereference(callchain_cpus_entries); if (!entries) { - put_recursion_context(this_cpu_ptr(callchain_recursion), *rctx); + put_recursion_context(this_cpu_ptr(callchain_recursion), rctx); return NULL; } =20 cpu =3D smp_processor_id(); =20 - return (((void *)entries->cpu_entries[cpu]) + - (*rctx * perf_callchain_entry__sizeof())); + entry =3D ((void *)entries->cpu_entries[cpu]) + + (rctx * perf_callchain_entry__sizeof()); + entry->rctx =3D rctx; + + return entry; } =20 void -put_callchain_entry(int rctx) +put_callchain_entry(struct perf_callchain_entry *entry) { - put_recursion_context(this_cpu_ptr(callchain_recursion), rctx); + put_recursion_context(this_cpu_ptr(callchain_recursion), entry->rctx); } =20 static void fixup_uretprobe_trampoline_entries(struct perf_callchain_entry= *entry, @@ -222,13 +227,13 @@ get_perf_callchain(struct pt_regs *regs, bool kernel,= bool user, { struct perf_callchain_entry *entry; struct perf_callchain_entry_ctx ctx; - int rctx, start_entry_idx; + int start_entry_idx; =20 /* crosstask is not supported for user stacks */ if (crosstask && user && !kernel) return NULL; =20 - entry =3D get_callchain_entry(&rctx); + entry =3D get_callchain_entry(); if (!entry) return NULL; =20 @@ -272,7 +277,7 @@ get_perf_callchain(struct pt_regs *regs, bool kernel, b= ool user, } =20 exit_put: - put_callchain_entry(rctx); + put_callchain_entry(entry); =20 return entry; } --=20 2.48.1 From nobody Sat Feb 7 11:31:46 2026 Received: from out-174.mta0.migadu.com (out-174.mta0.migadu.com [91.218.175.174]) (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 B6D3D30FC39 for ; Mon, 26 Jan 2026 07:45:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769413552; cv=none; b=Ap9wOr+pU0FTEIlGaG5i3StPRpcoGR/N7kX7E/zwWvT59yNh1T/fv/IMy++QyGHGuWUCiyTJ74dYW/r9ix0N/V7qtl6XiKVh5xf4G/D140ZHtsis1PeZySM1JYuzkfg80W6PUi+l9bDgQaYHfORwagsxe5uH+X/6/SP0sFQ5Hf4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769413552; c=relaxed/simple; bh=7FoC3r3AxO5ou9V4Xi9JtHx0tBIve9Uq6sQHQgXG9rM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aLwKK7oTm20rh/f1NI5riGcrx/MAhazw7KPPp7NWKBDfRgZJ8VD6toWjUYuXDSFPaurRtk0T1Fbu7Plxt5AWeTaOR0TQeM35SN+2FWbsHac1cMRAZe8E/dFY+4scnYtb1/NabZH0pUzkEnHkAomtXxwvkPN9Li2Wx7lXXzhCnAE= 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=Y1Z9UXC3; arc=none smtp.client-ip=91.218.175.174 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="Y1Z9UXC3" 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=1769413538; 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=Mo0+KCVD+P6cqk7ZzlvxjYRyhs7XL0k+jGz9ASrm76g=; b=Y1Z9UXC3tidrUHrbE6ucF9M/RYTZ+FfVvsbiuo5zONdFlnKpI0mEn0Aj0tK2vmW2+0E6Or 6C+fI4Ym4H/E49OkCz60y9qy1SdR3LOrcu0z4wgsP/xXIYaOGeJubuE0VeI6aHg1IWFCYd EnmUlQxvD5IpUDqUCltbNYx7zKQEiqg= 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: [PATCH bpf-next v8 2/3] perf: Refactor get_perf_callchain Date: Mon, 26 Jan 2026 15:43:30 +0800 Message-ID: <20260126074331.815684-3-chen.dylane@linux.dev> In-Reply-To: <20260126074331.815684-1-chen.dylane@linux.dev> References: <20260126074331.815684-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 ensure that the callchain buffer will not be overwritten by other preemptive tasks and we also aim to reduce the preempt disable interval, Based on the suggestions from Peter and Andrrii, export new API __get_perf_callchain and the usage scenarios are as follows from BPF side: preempt_disable() entry =3D get_callchain_entry() preempt_enable() __get_perf_callchain(entry) put_callchain_entry(entry) Suggested-by: Andrii Nakryiko Signed-off-by: Tao Chen --- include/linux/perf_event.h | 5 +++++ kernel/events/callchain.c | 34 ++++++++++++++++++++++------------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index f0489843ebc..7132fa97bb1 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -1722,6 +1722,11 @@ extern void perf_callchain_kernel(struct perf_callch= ain_entry_ctx *entry, struct extern struct perf_callchain_entry * get_perf_callchain(struct pt_regs *regs, bool kernel, bool user, u32 max_stack, bool crosstask, bool add_mark, u64 defer_cookie); + +extern int __get_perf_callchain(struct perf_callchain_entry *entry, struct= pt_regs *regs, + bool kernel, bool user, u32 max_stack, bool crosstask, bool add_mark, + u64 defer_cookie); + extern int get_callchain_buffers(int max_stack); extern void put_callchain_buffers(void); extern struct perf_callchain_entry *get_callchain_entry(void); diff --git a/kernel/events/callchain.c b/kernel/events/callchain.c index 6cdbc5937b1..f9789d10fa4 100644 --- a/kernel/events/callchain.c +++ b/kernel/events/callchain.c @@ -221,21 +221,15 @@ static void fixup_uretprobe_trampoline_entries(struct= perf_callchain_entry *entr #endif } =20 -struct perf_callchain_entry * -get_perf_callchain(struct pt_regs *regs, bool kernel, bool user, - u32 max_stack, bool crosstask, bool add_mark, u64 defer_cookie) +int __get_perf_callchain(struct perf_callchain_entry *entry, struct pt_reg= s *regs, bool kernel, + bool user, u32 max_stack, bool crosstask, bool add_mark, u64 defer_coo= kie) { - struct perf_callchain_entry *entry; struct perf_callchain_entry_ctx ctx; int start_entry_idx; =20 /* crosstask is not supported for user stacks */ if (crosstask && user && !kernel) - return NULL; - - entry =3D get_callchain_entry(); - if (!entry) - return NULL; + return -EINVAL; =20 ctx.entry =3D entry; ctx.max_stack =3D max_stack; @@ -252,7 +246,7 @@ get_perf_callchain(struct pt_regs *regs, bool kernel, b= ool user, if (user && !crosstask) { if (!user_mode(regs)) { if (current->flags & (PF_KTHREAD | PF_USER_WORKER)) - goto exit_put; + return 0; regs =3D task_pt_regs(current); } =20 @@ -265,7 +259,7 @@ get_perf_callchain(struct pt_regs *regs, bool kernel, b= ool user, */ perf_callchain_store_context(&ctx, PERF_CONTEXT_USER_DEFERRED); perf_callchain_store_context(&ctx, defer_cookie); - goto exit_put; + return 0; } =20 if (add_mark) @@ -275,9 +269,25 @@ get_perf_callchain(struct pt_regs *regs, bool kernel, = bool user, perf_callchain_user(&ctx, regs); fixup_uretprobe_trampoline_entries(entry, start_entry_idx); } + return 0; +} + +struct perf_callchain_entry * +get_perf_callchain(struct pt_regs *regs, bool kernel, bool user, + u32 max_stack, bool crosstask, bool add_mark, u64 defer_cookie) +{ + struct perf_callchain_entry *entry; + int ret; + + entry =3D get_callchain_entry(); + if (!entry) + return NULL; =20 -exit_put: + ret =3D __get_perf_callchain(entry, regs, kernel, user, max_stack, crosst= ask, add_mark, + defer_cookie); put_callchain_entry(entry); + if (ret) + entry =3D NULL; =20 return entry; } --=20 2.48.1 From nobody Sat Feb 7 11:31:46 2026 Received: from out-181.mta1.migadu.com (out-181.mta1.migadu.com [95.215.58.181]) (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 AC76930FC39 for ; Mon, 26 Jan 2026 07:46:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769413565; cv=none; b=A9qL6BwQVNrvF/8lfA4K1pzmNUbN18r/aPHMvl0hVG7gDwQV1LU9Nlqf9xg9s0VfpfbhJgSh5YUHCCeprq7iyC10votwRi22VJfd8Uww9Cizrs7yI8D80D0kr3J0XGPAWjXccMLFkApAsFNKtPNihhHzTkSu5DfpP+pLqSgcGpg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769413565; c=relaxed/simple; bh=NktD6tjtBJc8ZlKtG4yjDn3iJhKXY/1JyGPuSKcMr7Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HDKyk6fag2OSXmadZ2KPqll47Evgru2Psd7MGnw/snfKWSCb4TTzegILAkF8gSR7YFmgtX5mUSOOD+aJDNPNsAb5bXb0GQR5Tsj0OjFs/SRaZaTZRLX2YOxmUlFCyGgvhIN8quDq/QkLnsqLfTy8qUAGwwGdibJnudDPfxXeL58= 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=eVdtXpxD; arc=none smtp.client-ip=95.215.58.181 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="eVdtXpxD" 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=1769413561; 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=eGKZCIMuPLTUUetmg43YQkbWKXVbZVR0jeFtXTaplZM=; b=eVdtXpxDRjCOghcT7+o4hJKuG0Izhm5Xvkov/Nwg4mUMD13QaveVWXlIneAcsCK/PFfWbq o7i6wqRDXhInQRFS1sqUyi7Jo1oVY29uXft7ruaO3avYLhuYDhTV/SyLmte+EPRaIsRe5D yFaotQVUJwwJ6/qz+RF45M2J/t/L4z0= 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: [PATCH bpf-next v8 3/3] bpf: Hold ther perf callchain entry until used completely Date: Mon, 26 Jan 2026 15:43:31 +0800 Message-ID: <20260126074331.815684-4-chen.dylane@linux.dev> In-Reply-To: <20260126074331.815684-1-chen.dylane@linux.dev> References: <20260126074331.815684-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. The perf_callchain_entres has a small stack of entries, and we can reuse it as follows: 1. get the perf callchain entry 2. BPF use... 3. put the perf callchain entry And Peter suggested that get_recursion_context used with preemption disabled, so we should disable preemption at BPF side. Acked-by: Yonghong Song Signed-off-by: Tao Chen --- kernel/bpf/stackmap.c | 55 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c index e77dcdc2164..6bdee6cc05f 100644 --- a/kernel/bpf/stackmap.c +++ b/kernel/bpf/stackmap.c @@ -215,7 +215,9 @@ get_callchain_entry_for_task(struct task_struct *task, = u32 max_depth) #ifdef CONFIG_STACKTRACE struct perf_callchain_entry *entry; =20 + preempt_disable(); entry =3D get_callchain_entry(); + preempt_enable(); =20 if (!entry) return NULL; @@ -237,14 +239,40 @@ get_callchain_entry_for_task(struct task_struct *task= , u32 max_depth) to[i] =3D (u64)(from[i]); } =20 - put_callchain_entry(entry); - return entry; #else /* CONFIG_STACKTRACE */ return NULL; #endif } =20 +static struct perf_callchain_entry * +bpf_get_perf_callchain(struct pt_regs *regs, bool kernel, bool user, int m= ax_stack, + bool crosstask) +{ + struct perf_callchain_entry *entry; + int ret; + + preempt_disable(); + entry =3D get_callchain_entry(); + preempt_enable(); + + if (unlikely(!entry)) + return NULL; + + ret =3D __get_perf_callchain(entry, regs, kernel, user, max_stack, crosst= ask, false, 0); + if (ret) { + put_callchain_entry(entry); + return NULL; + } + + return entry; +} + +static void bpf_put_perf_callchain(struct perf_callchain_entry *entry) +{ + put_callchain_entry(entry); +} + static long __bpf_get_stackid(struct bpf_map *map, struct perf_callchain_entry *trace, u64 flags) { @@ -327,20 +355,23 @@ BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, s= truct bpf_map *, map, struct perf_callchain_entry *trace; bool kernel =3D !user; u32 max_depth; + int ret; =20 if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK | BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID))) return -EINVAL; =20 max_depth =3D stack_map_calculate_max_depth(map->value_size, elem_size, f= lags); - trace =3D get_perf_callchain(regs, kernel, user, max_depth, - false, false, 0); + trace =3D bpf_get_perf_callchain(regs, kernel, user, max_depth, false); =20 if (unlikely(!trace)) /* couldn't fetch the stack trace */ return -EFAULT; =20 - return __bpf_get_stackid(map, trace, flags); + ret =3D __bpf_get_stackid(map, trace, flags); + bpf_put_perf_callchain(trace); + + return ret; } =20 const struct bpf_func_proto bpf_get_stackid_proto =3D { @@ -468,13 +499,19 @@ static long __bpf_get_stack(struct pt_regs *regs, str= uct 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, - crosstask, false, 0); + trace =3D bpf_get_perf_callchain(regs, kernel, user, max_depth, crosstas= k); } =20 - if (unlikely(!trace) || trace->nr < skip) { + if (unlikely(!trace)) { + if (may_fault) + rcu_read_unlock(); + goto err_fault; + } + if (trace->nr < skip) { if (may_fault) rcu_read_unlock(); + if (!trace_in) + bpf_put_perf_callchain(trace); goto err_fault; } =20 @@ -495,6 +532,8 @@ static long __bpf_get_stack(struct pt_regs *regs, struc= t task_struct *task, /* trace/ips should not be dereferenced after this point */ if (may_fault) rcu_read_unlock(); + if (!trace_in) + bpf_put_perf_callchain(trace); =20 if (user_build_id) stack_map_get_build_id_offset(buf, trace_nr, user, may_fault); --=20 2.48.1