From nobody Thu Oct 2 04:49:35 2025 Received: from out-188.mta1.migadu.com (out-188.mta1.migadu.com [95.215.58.188]) (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 B8656A920 for ; Mon, 22 Sep 2025 07:54:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.188 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758527667; cv=none; b=V3GYEZjkOt21nsa5mWea7Zi8MkB4D29VPdGTy7oFbUnCUcrNIPnHoyuEoWkxYQvDThUOsNLhV9PLBuuc8hPgD4VPSuoyH4zrnaewHkzB2zzIQ8zaRvEQEyImUpgyu2dJ5RdAOUF/a8VBItUSeZBWUCgItwpd3XgD/1KBvnk215s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758527667; c=relaxed/simple; bh=R28uxE32gqNwzcXDbG30ZDmSivgFql4fRGlFePdTlx0=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=JN4eDLCMJAcJ2vndHUoGQ2h2dlYJutSNmi4N+UvA6yCXeDyxaMVkToR4Qj13U63GLR2HZhQQMhSiA0ePGLDMDzPh7c+2f9k6OZtuto+/7NEboVzCZPBmqTTfTsXOZAFhnWubhxwVtbe3rc52d8N9J/yRKcjEV/cOHuNOF3NjtJE= 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=kJAqmp7N; arc=none smtp.client-ip=95.215.58.188 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="kJAqmp7N" 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=1758527662; 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; bh=/F+if3ZmRE6mryxGtP8tu86KfXMMdeV8BX6DdblMOk4=; b=kJAqmp7NaxLfxx26YA6GVjs4ktc1ebWFEKRMDv4BWqK1OxIn6erNAm8cmR23qOYFb0kAix QyuuQqZ/KJEdkj9zV0u7CjV+OibDS+I5EO3cNCtBfEgrM3eKjOtEwe64XQDSJEPsdQxba+ v0kHkr4jM1Wh3labWYO68T5uRjLuBag= From: Tao Chen To: song@kernel.org, jolsa@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: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Tao Chen Subject: [PATCH bpf-next] bpf: Add preempt_disable to protect get_perf_callchain Date: Mon, 22 Sep 2025 15:53:33 +0800 Message-ID: <20250922075333.1452803-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 suggested, the return value from get_perf_callchain() may be reused if another task preempts and requests the stack after BPF program switched to migrate disable. Reported-by: Alexei Starovoitov Signed-off-by: Tao Chen --- kernel/bpf/stackmap.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c index 2e182a3ac4c..07892320906 100644 --- a/kernel/bpf/stackmap.c +++ b/kernel/bpf/stackmap.c @@ -314,8 +314,10 @@ 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 + preempt_disable(); trace =3D get_perf_callchain(regs, 0, kernel, user, max_depth, false, false); + preempt_enable(); =20 if (unlikely(!trace)) /* couldn't fetch the stack trace */ @@ -443,9 +445,7 @@ static long __bpf_get_stack(struct pt_regs *regs, struc= t task_struct *task, if (sysctl_perf_event_max_stack < max_depth) max_depth =3D sysctl_perf_event_max_stack; =20 - if (may_fault) - rcu_read_lock(); /* need RCU for perf's callchain below */ - + preempt_disable(); if (trace_in) trace =3D trace_in; else if (kernel && task) @@ -455,8 +455,7 @@ static long __bpf_get_stack(struct pt_regs *regs, struc= t task_struct *task, crosstask, false); =20 if (unlikely(!trace) || trace->nr < skip) { - if (may_fault) - rcu_read_unlock(); + preempt_enable(); goto err_fault; } =20 @@ -474,10 +473,7 @@ static long __bpf_get_stack(struct pt_regs *regs, stru= ct task_struct *task, } else { memcpy(buf, ips, copy_len); } - - /* trace/ips should not be dereferenced after this point */ - if (may_fault) - rcu_read_unlock(); + preempt_enable(); =20 if (user_build_id) stack_map_get_build_id_offset(buf, trace_nr, user, may_fault); --=20 2.48.1