From nobody Sat Feb 7 07:09:57 2026 Received: from mail-pl1-f194.google.com (mail-pl1-f194.google.com [209.85.214.194]) (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 350F533C191 for ; Wed, 17 Dec 2025 09:55:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.194 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765965335; cv=none; b=NAZsEv+s1u7NJBDafKG5lGnHYobUSZ3lpFxTaqn7x2lvzsegjTmSZqMSg9x4Xodx98g+Nsb9pbFdaf4AbajLPGuAEq5CqF+10lTW42QgZRFKGay3vjiGv1kX45KJV7RXdR2wLBmNzhHc2h/OnZMMPx/NqdiMkwR60x8skBLcR+M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765965335; c=relaxed/simple; bh=OJ+pmI+L2FN9LNa/GQ8rbcja6XaPYSuA8kA30fwb/sU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=awl1+eLpAyzB3Sa2Bo/HwHYi+CVzKR6DEk1R8zzWSTqU9pqpdRePskViRe96BBaGl8llWsJKkpL8BhrC3bUi9SWnc5HiCeq3fqqNx8qqNvJ2lZMGaSp9Qk1/VXC7vkfhPITTQ9y/9nd4q2+9GT0hHk7sPOPAe5lNBlAEC6dbvPM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Um7d+U3u; arc=none smtp.client-ip=209.85.214.194 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Um7d+U3u" Received: by mail-pl1-f194.google.com with SMTP id d9443c01a7336-29efd139227so68482365ad.1 for ; Wed, 17 Dec 2025 01:55:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765965333; x=1766570133; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=m7ajHfBTTsej1M2RhwUNhnzHgFKm/6J2aHc6UMKkyQU=; b=Um7d+U3uWl5SYeKkDDoJIq2cnbVipGwOTFLm5gmF3jvW+Fq/EttGoefVrXTrsXThfw QwqQM9GdpF31ayFFqOBUqokVXB4t2yKkVXFSIGcoanl8oXgeTSjscuNcQ7O94kKIYqiz 5c3lbMvwTAi50BtBZGRdP4mssHe7lx2EGIJ/BDPaWLZhPAoQjwQIYxHR5hs/ErqZLfrg ZJ9OyTlsZtdJSNLHmODSw06gSRk9zcE+UE0oN7/4uL8aQRIBWhtkPME/VPZVBb5+CaNl 71AZbIgKc7KGGX+dth+wVYWyhzN9X+8Zm8h4qRl/y1+YwqUPzkD31INrGeALd422vKDU G+hg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765965333; x=1766570133; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=m7ajHfBTTsej1M2RhwUNhnzHgFKm/6J2aHc6UMKkyQU=; b=rEQqZgpq3Kq4CQbj5kQ9vr+XUyOGRW5SvxLJ4UQRJem/cRt8o/08GKMduaLEjwSSvn d/1fAcXIsWqbvMCEZcxoVQ9iLTpKwUM6IpN69Bc5rTOH+6D8FVGnexk+TGIyjzd5ycv/ a/OllnaARdoIHBvUD110A/YG21fDuS7jGQu4XI06ZsRMOCTESJhF+I8BUIb3tbOebS/w B0xZcs0TkFfkQ79TiKEa9LiiBYxxOT4h1HT1PzOv7PnaVPmy6kDKuOYT1Hsu1gwSn3my nryD4kxyOFPShTEF+WplLoJ5rAhpiuM15U1R2Z8cuF45SltgHpIEMrEzPfbkLOMFOZnx XG6w== X-Forwarded-Encrypted: i=1; AJvYcCXDIwFN++g07G3W7anwS3Z8+z7okvHYw88u0rGyzLw7gDdsJlnw//ylPZZ+fAOXbX5Rkack4FhlPwVwlL8=@vger.kernel.org X-Gm-Message-State: AOJu0Yz2SgvPzTLskOWYzQuPIbwNRF6f6/RUe1zeNpvaoJ8e0QT7rOev z4gHK4VSYC8bSIxG/A/blrEuj9k0CLrNeEIGGTSMvfyYRsOk3gHCBTfD X-Gm-Gg: AY/fxX7byYHrVpRYBd/126VxCV9JHNnUE6zQSTwvLOwlzUbaFMwFzCgAFXk9edwcvdU lLIclJhXOKgbEgjo8Yyrz2UazHq3bvSsEHTx2qQtJ9O8A3Z9UHTBh5Rz+KTvoQH2hLz0UuyS5e7 snHahvGUH9SW4x3I99+ML7bldJ1K/bDA1MSCXM8Zgb3gi2VIaXNbO62TrkszhPyEe7y/HKeBW/E 0ugG+Duzeyo9Cbe+9Z5P7ePk8/1CcjBxpUDdxqhLLo8JFZO8YjBbwr8ASMq/XKeeqlqSTn/DfFt F3rMbJ7Od8KhL5M8mnwrgZfe50esASU+Do9FOmmDPhksr4JIu1gJb2endqXI6AxnmKCmJ7P6tsc mJMKAVNYC2eR2JImeMdtzp7SmP0Vojtj4OSKX9fxyz3vNaPfP8KNHSrXsVakBdha3NVXfnxRh8g AWiuuYieE= X-Google-Smtp-Source: AGHT+IEWPBto4teSaddmB1eqJXnA1IXD2xBXoEGQddliKYYXcy7uKp4BcIOhpNQZDsftssScJ+vVfQ== X-Received: by 2002:a17:902:f54b:b0:2a0:8963:c147 with SMTP id d9443c01a7336-2a08963c3fbmr154947065ad.45.1765965333540; Wed, 17 Dec 2025 01:55:33 -0800 (PST) Received: from 7940hx ([43.129.244.20]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2a07fa0b1aasm140715945ad.3.2025.12.17.01.55.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Dec 2025 01:55:33 -0800 (PST) From: Menglong Dong X-Google-Original-From: Menglong Dong To: ast@kernel.org, andrii@kernel.org Cc: davem@davemloft.net, dsahern@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next v4 6/9] bpf,x86: add tracing session supporting for x86_64 Date: Wed, 17 Dec 2025 17:54:42 +0800 Message-ID: <20251217095445.218428-7-dongml2@chinatelecom.cn> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20251217095445.218428-1-dongml2@chinatelecom.cn> References: <20251217095445.218428-1-dongml2@chinatelecom.cn> 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 Content-Type: text/plain; charset="utf-8" Add BPF_TRACE_SESSION supporting to x86_64, including: 1. clear the return value in the stack before fentry to make the fentry of the fsession can only get 0 with bpf_get_func_ret(). If we can limit that bpf_get_func_ret() can only be used in the "bpf_fsession_is_return() =3D=3D true" code path, we don't need do this thing anymore. 2. clear all the session cookies' value in the stack. If we can make sure that the reading to session cookie can only be done after initialize in the verifier, we don't need this anymore. 2. store the index of the cookie to ctx[-1] before the calling to fsession 3. store the "is_return" flag to ctx[-1] before the calling to fexit of the fsession. Signed-off-by: Menglong Dong Co-developed-by: Leon Hwang Signed-off-by: Leon Hwang --- v4: - some adjustment to the 1st patch, such as we get the fsession prog from fentry and fexit hlist - remove the supporting of skipping fexit with fentry return non-zero v2: - add session cookie support - add the session stuff after return value, instead of before nr_args --- arch/x86/net/bpf_jit_comp.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index 8cbeefb26192..99b0223374bd 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -3086,12 +3086,17 @@ static int emit_cond_near_jump(u8 **pprog, void *fu= nc, void *ip, u8 jmp_cond) static int invoke_bpf(const struct btf_func_model *m, u8 **pprog, struct bpf_tramp_links *tl, int stack_size, int run_ctx_off, bool save_ret, - void *image, void *rw_image) + void *image, void *rw_image, u64 nr_regs) { int i; u8 *prog =3D *pprog; =20 for (i =3D 0; i < tl->nr_links; i++) { + if (tl->links[i]->link.prog->call_session_cookie) { + /* 'stack_size + 8' is the offset of nr_regs in stack */ + emit_st_r0_imm64(&prog, nr_regs, stack_size + 8); + nr_regs -=3D (1 << BPF_TRAMP_M_COOKIE); + } if (invoke_bpf_prog(m, &prog, tl->links[i], stack_size, run_ctx_off, save_ret, image, rw_image)) return -EINVAL; @@ -3208,8 +3213,9 @@ static int __arch_prepare_bpf_trampoline(struct bpf_t= ramp_image *im, void *rw_im struct bpf_tramp_links *tlinks, void *func_addr) { - int i, ret, nr_regs =3D m->nr_args, stack_size =3D 0; - int regs_off, nregs_off, ip_off, run_ctx_off, arg_stack_off, rbx_off; + int i, ret, nr_regs =3D m->nr_args, cookie_cnt, stack_size =3D 0; + int regs_off, nregs_off, ip_off, run_ctx_off, arg_stack_off, rbx_off, + cookie_off; struct bpf_tramp_links *fentry =3D &tlinks[BPF_TRAMP_FENTRY]; struct bpf_tramp_links *fexit =3D &tlinks[BPF_TRAMP_FEXIT]; struct bpf_tramp_links *fmod_ret =3D &tlinks[BPF_TRAMP_MODIFY_RETURN]; @@ -3282,6 +3288,11 @@ static int __arch_prepare_bpf_trampoline(struct bpf_= tramp_image *im, void *rw_im =20 ip_off =3D stack_size; =20 + cookie_cnt =3D bpf_fsession_cookie_cnt(tlinks); + /* room for session cookies */ + stack_size +=3D cookie_cnt * 8; + cookie_off =3D stack_size; + stack_size +=3D 8; rbx_off =3D stack_size; =20 @@ -3372,9 +3383,19 @@ static int __arch_prepare_bpf_trampoline(struct bpf_= tramp_image *im, void *rw_im } } =20 + if (bpf_fsession_cnt(tlinks)) { + /* clear all the session cookies' value */ + for (int i =3D 0; i < cookie_cnt; i++) + emit_st_r0_imm64(&prog, 0, cookie_off - 8 * i); + /* clear the return value to make sure fentry always get 0 */ + emit_st_r0_imm64(&prog, 0, 8); + nr_regs +=3D (((cookie_off - regs_off) / 8) << BPF_TRAMP_M_COOKIE); + } + if (fentry->nr_links) { if (invoke_bpf(m, &prog, fentry, regs_off, run_ctx_off, - flags & BPF_TRAMP_F_RET_FENTRY_RET, image, rw_image)) + flags & BPF_TRAMP_F_RET_FENTRY_RET, image, rw_image, + nr_regs)) return -EINVAL; } =20 @@ -3434,9 +3455,14 @@ static int __arch_prepare_bpf_trampoline(struct bpf_= tramp_image *im, void *rw_im } } =20 + /* set the "is_return" flag for fsession */ + nr_regs +=3D (1 << BPF_TRAMP_M_IS_RETURN); + if (bpf_fsession_cnt(tlinks)) + emit_st_r0_imm64(&prog, nr_regs, nregs_off); + if (fexit->nr_links) { if (invoke_bpf(m, &prog, fexit, regs_off, run_ctx_off, - false, image, rw_image)) { + false, image, rw_image, nr_regs)) { ret =3D -EINVAL; goto cleanup; } --=20 2.52.0