From nobody Mon Jun 8 17:58:55 2026 Received: from smtpout-02.galae.net (smtpout-02.galae.net [185.246.84.56]) (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 A31004315F for ; Wed, 27 May 2026 19:12:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.246.84.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779909170; cv=none; b=Bs8V483fBRT2bUF0E/70I2J5BXRYaDYFPcFlk1ZPS0bWBV0L9hBUYU6vfuEtjAuyWWl8czmJ64N7YHl32ExLo1SGq56v16zCNou4LLGQtsKfoDhxU/BLDPZTv6ZE1vpMy4/c4t+UvXn0hI3lx4/SyWZICf7Vv6RsA6h0v4gN/j8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779909170; c=relaxed/simple; bh=OMBhp36SGhBUxYsENPfb9uF3VzY8cW8Q8Xw5p9ehcs8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=BM9mYb7WCd6gSthAawDdpegRzrRH7lFMOB2rRW/93aaumgKfrm70v5N+Z9UBA+7xXOjw1d6Zf47GPi6ByLWyWT0aOAE2kYom1gX6zom0+TLKVOcpygpq5dIZ0u3vCbnv0Yfc4GoBGnF6SZt8f6thtLBbNr/jug5sGfHq05V8gmA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=OffYYVPf; arc=none smtp.client-ip=185.246.84.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="OffYYVPf" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-02.galae.net (Postfix) with ESMTPS id D31951A36F4; Wed, 27 May 2026 19:12:44 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 9282A601A1; Wed, 27 May 2026 19:12:44 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 1697A10888BE4; Wed, 27 May 2026 21:12:38 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1779909163; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding; bh=pqol5pOcp80VinncolPKrRnV5MzpAOgtnDii24qMsFA=; b=OffYYVPfGFCF2dx9rYcIraiIMq+j7bOIll6Qp7FRxkUrDxdIt410u8733cY+NqOiejfA0H 1uAeQI6/4IFZs8Zcv+kM6tsLUGlqD49XqYMd25HLACFjPIBLJQMC3s7FihDKpTSWXO7Kv8 13ar0VGPk+K9c6lIwHgSRP0Qv9+da1ZagwHopr5Bptzw9oFKGmvtsSVEDo37Rq7UJtfLsv 5oYh9c6E8JfwLKqk+7zGRU38il8afy96ie9wOwVP9yvoJrp2rFSoUrr26fRoQZtShFB34H Ezud/a+RvKwm2whD1hidf/fSnLKMd5QQrTMu6yhrCI2DAT7LTygHdoVYJqtKKw== From: =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= Date: Wed, 27 May 2026 21:12:31 +0200 Subject: [PATCH bpf-next] x86/ftrace: relocate %rip-relative percpu refs in dynamic trampolines 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: <20260527-fix_call_depth_in_trampoline-v1-1-1c1abc8ae310@bootlin.com> X-B4-Tracking: v=1; b=H4sIAAAAAAAC/yXM4QrCIBSG4VsZ53eCGVurW4khenZsJ8yJ2hiM3 fusfr58H88GmRJThnuzQaKFM8+hxvnUAE4mPEnwWBuUVJ1s1VU4XjUa7/VIsUyagy7JvOPsOZD opcW2u10k9g4qERPV/49/gI1OBFoLDP8lf+yLsHx12PcDDo4qhYoAAAA= X-Change-ID: 20260527-fix_call_depth_in_trampoline-80bc56930c8f To: Steven Rostedt , Masami Hiramatsu , Mark Rutland , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Uros Bizjak Cc: Thomas Petazzoni , Ingo Molnar , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, bpf@vger.kernel.org, ebpf@linuxfoundation.org, Bastien Curutchet , =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= X-Mailer: b4 0.15.2 X-Last-TLS-Session-Version: TLSv1.3 With CONFIG_CALL_DEPTH_TRACKING enabled on an x86 retbleed-affected platform (eg: Skylake), with retbleed=3Dstuff, registering a dynamic ftrace trampoline crashes on the first call into the traced function: [ 9.630365] BUG: unable to handle page fault for address: ffff88817ae1= 8880 [ 9.630365] #PF: supervisor write access in kernel mode [ 9.630365] #PF: error_code(0x0002) - not-present page [ 9.630365] PGD 4b53067 P4D 4b53067 PUD 0 [ 9.630365] Oops: Oops: 0002 [#1] SMP PTI [ 9.630365] CPU: 3 UID: 0 PID: 187 Comm: usleep Not tainted 7.0.10 #24= 3 PREEMPT(full) [ 9.630365] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIO= S Arch Linux 1.17.0-2-2 04/01/2014 [ 9.630365] RIP: 0010:0xffffffffc0400058 [ 9.630365] Code: 24 78 00 00 00 00 48 89 ea 48 89 54 24 20 48 8b b4 2= 4 b8 00 00 00 48 8b bc 24 b0 00 00 00 48 89 bc 24 80 00 00 00 48 83 ef 05 <= 65> 48 c1 3d 1f a8 b6 02 05 48 8b 15 f6 00 00 00 4c 89 3c 24 4c 89 [ 9.630365] RSP: 0018:ffffc90000a3fe60 EFLAGS: 00010382 [ 9.630365] RAX: ffffffffffffffff RBX: ffffc90000a3ff58 RCX: 000000000= 0000000 [ 9.630365] RDX: ffffc90000a3ff48 RSI: ffffffff82198e40 RDI: ffffffff8= 13f5654 [ 9.630365] RBP: ffffc90000a3ff48 R08: 0000000000000000 R09: 000000000= 0000000 [ 9.630365] R10: 0000000000000000 R11: 0000000000000001 R12: ffff88810= 30d3780 [ 9.630365] R13: 00000000000000e6 R14: 0000000000000000 R15: 000000000= 0000000 [ 9.630365] FS: 00007f081d131740(0000) GS:ffff8881b7eae000(0000) knlG= S:0000000000000000 [ 9.630365] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 9.630365] CR2: ffff88817ae18880 CR3: 00000001033dc006 CR4: 000000000= 03706f0 [ 9.630365] Call Trace: [ 9.630365] [ 9.630365] ? find_held_lock+0x2b/0x80 [ 9.630365] ? exc_page_fault+0x74/0x220 [ 9.630365] ? lock_release+0xe1/0x320 [ 9.630365] ? __x64_sys_clock_nanosleep+0x9/0x1a0 [ 9.630365] ? lockdep_hardirqs_on_prepare+0xd9/0x190 [ 9.630365] ? trace_hardirqs_on+0x18/0x100 [ 9.630365] __x64_sys_clock_nanosleep+0x9/0x1a0 [ 9.630365] do_syscall_64+0x100/0x5f0 [ 9.630365] ? exc_page_fault+0x1e0/0x220 [ 9.630365] ? call_depth_return_thunk+0x2a/0xd0 [ 9.630365] entry_SYSCALL_64_after_hwframe+0x76/0x7e [ 9.630365] RIP: 0033:0x7f081d20ad83 [ 9.630365] Code: ff ff c3 0f 1f 40 00 83 ff 03 74 7b 83 ff 02 b8 fa f= f ff ff 49 89 ca 0f 44 f8 80 3d c6 d2 10 00 00 74 14 b8 e6 00 00 00 0f 05 <= f7> d8 c3 66 2e 0f 1f 84 00 00 00 00 00 48 83 ec 28 48 89 54 24 10 [ 9.630365] RSP: 002b:00007ffd539e1328 EFLAGS: 00000202 ORIG_RAX: 0000= 0000000000e6 [ 9.630365] RAX: ffffffffffffffda RBX: 0000000000000103 RCX: 00007f081= d20ad83 [ 9.630365] RDX: 00007ffd539e1340 RSI: 0000000000000000 RDI: 000000000= 0000000 [ 9.630365] RBP: 00007ffd539e14f8 R08: 0000000000000000 R09: 000000000= 0000000 [ 9.630365] R10: 0000000000000000 R11: 0000000000000202 R12: 000000000= 0000000 [ 9.630365] R13: 00007ffd539e1510 R14: 00007f081d370000 R15: 00005619c= 9d78338 [ 9.630365] [ 9.630365] Modules linked in: [ 9.630365] CR2: ffff88817ae18880 [ 9.630365] ---[ end trace 0000000000000000 ]--- [ 9.630365] RIP: 0010:0xffffffffc0400058 [ 9.630365] Code: 24 78 00 00 00 00 48 89 ea 48 89 54 24 20 48 8b b4 2= 4 b8 00 00 00 48 8b bc 24 b0 00 00 00 48 89 bc 24 80 00 00 00 48 83 ef 05 <= 65> 48 c1 3d 1f a8 b6 02 05 48 8b 15 f6 00 00 00 4c 89 3c 24 4c 89 [ 9.630365] RSP: 0018:ffffc90000a3fe60 EFLAGS: 00010382 [ 9.630365] RAX: ffffffffffffffff RBX: ffffc90000a3ff58 RCX: 000000000= 0000000 [ 9.630365] RDX: ffffc90000a3ff48 RSI: ffffffff82198e40 RDI: ffffffff8= 13f5654 [ 9.630365] RBP: ffffc90000a3ff48 R08: 0000000000000000 R09: 000000000= 0000000 [ 9.630365] R10: 0000000000000000 R11: 0000000000000001 R12: ffff88810= 30d3780 [ 9.630365] R13: 00000000000000e6 R14: 0000000000000000 R15: 000000000= 0000000 [ 9.630365] FS: 00007f081d131740(0000) GS:ffff8881b7eae000(0000) knlG= S:0000000000000000 [ 9.630365] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 9.630365] CR2: ffff88817ae18880 CR3: 00000001033dc006 CR4: 000000000= 03706f0 [ 9.630365] Kernel panic - not syncing: Fatal exception This small reproducer allows to easily trigger the crash: # echo 'p __x64_sys_clock_nanosleep' > /sys/kernel/tracing/kprobe_events # echo 1 > /sys/kernel/tracing/events/kprobes/p___x64_sys_clock_nanosleep= _0/enable # usleep 1 Monitoring the crash under GDB points to the exact instruction in charge of incrementing the call depth: sarq $5, %gs:__x86_call_depth(%rip) This instruction matches the one inserted by the ftrace_regs_caller from ftrace_64.S. This emitted code was likely working fine until the introduction of commit 59bec00ace28 ("x86/percpu: Introduce %rip-relative addressing to PER_CPU_VAR()"): it has made the call depth accounting addressing relative to $rip, instead of being based on an absolute address. As this code exact location depends on where the trampoline lives in memory, the corresponding displacement needs to be adjusted at runtime to actually correctly find the per-cpu __x86_call_depth value, otherwise the targeted address is wrong, leading to the page fault seen above. Fix the %rip-relative displacement of the copied CALL_DEPTH_ACCOUNT instruction (from ftrace_regs_caller) by calling text_poke_apply_relocation(), as it is done for example by the x86 BPF JIT compiler through x86_call_depth_emit_accounting(). This corrects both CALL_DEPTH_ACCOUNT slots, in ftrace_caller and ftrace_regs_caller. Fixes: 59bec00ace28 ("x86/percpu: Introduce %rip-relative addressing to PER= _CPU_VAR()") Signed-off-by: Alexis Lothor=C3=A9 (eBPF Foundation) Acked-by: Peter Zijlstra (Intel) --- arch/x86/kernel/ftrace.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/arch/x86/kernel/ftrace.c b/arch/x86/kernel/ftrace.c index 0543b57f54ee..357df1b2922c 100644 --- a/arch/x86/kernel/ftrace.c +++ b/arch/x86/kernel/ftrace.c @@ -375,6 +375,13 @@ create_trampoline(struct ftrace_ops *ops, unsigned int= *tramp_size) goto fail; } =20 + /* + * Generated trampoline may contain rip-relative addressing which + * displacement needs to be fixed + */ + text_poke_apply_relocation(trampoline, trampoline, size, + (void *)start_offset, size); + /* * The address of the ftrace_ops that is used for this trampoline * is stored at the end of the trampoline. This will be used to --- base-commit: aef70d0806e39b83f1fbecc32c72cc328751292a change-id: 20260527-fix_call_depth_in_trampoline-80bc56930c8f Best regards, -- =20 Alexis Lothor=C3=A9 (eBPF Foundation)