From nobody Sun Feb 8 09:37:31 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A7503C6FD1C for ; Thu, 23 Mar 2023 11:40:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229846AbjCWLkZ (ORCPT ); Thu, 23 Mar 2023 07:40:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229532AbjCWLkX (ORCPT ); Thu, 23 Mar 2023 07:40:23 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id DAAFEF746 for ; Thu, 23 Mar 2023 04:40:21 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id AB6D34B3; Thu, 23 Mar 2023 04:41:05 -0700 (PDT) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id E13953F766; Thu, 23 Mar 2023 04:40:20 -0700 (PDT) From: Mark Rutland To: linux-kernel@vger.kernel.org Cc: mark.rutland@arm.com, mhiramat@kernel.org, rostedt@goodmis.org Subject: [PATCH] tracepoint: Fix CFI failures with tp_stub_func Date: Thu, 23 Mar 2023 11:40:12 +0000 Message-Id: <20230323114012.2162285-1-mark.rutland@arm.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When CLANG_CFI is in use, using tracing will occasionally result in CFI failures, e.g. | CFI failure at syscall_trace_enter+0x66c/0x7d0 (target: tp_stub_func+0x0/= 0x2c; expected type: 0x4877830c) | Internal error: Oops - CFI: 00000000f200823a [#1] PREEMPT SMP | CPU: 2 PID: 118 Comm: klogd Not tainted 6.3.0-rc3-00002-gc242ea6f2f98 #2 | Hardware name: linux,dummy-virt (DT) | pstate: 30400005 (nzCV daif +PAN -UAO -TCO -DIT -SSBS BTYPE=3D--) | pc : syscall_trace_enter+0x66c/0x7d0 | lr : syscall_trace_enter+0x5e8/0x7d0 | sp : ffff800015ce7d80 | x29: ffff800015ce7d80 x28: ffff000017538000 x27: 0000000000000003 | x26: ffff8000084c9454 x25: ffff00001182bd10 x24: dfff800000000000 | x23: 1fffe00002ea7001 x22: ffff00001182bd10 x21: ffff000017538008 | x20: ffff000017538000 x19: ffff800015ce7eb0 x18: 0000000000000000 | x17: 000000004877830c x16: 00000000a540670c x15: 0000000000000000 | x14: 0000000000000000 x13: 0000000000000000 x12: ff80800008039d8c | x11: ff80800008039dd0 x10: 0000000000000000 x9 : 0000000000000000 | x8 : 0000000000000000 x7 : 0000000000000000 x6 : 0000000000000000 | x5 : 0000000000000000 x4 : 0000000000000000 x3 : 0000000000000000 | x2 : 00000000000000ce x1 : ffff800015ce7eb0 x0 : ffff800013d55000 | Call trace: | syscall_trace_enter+0x66c/0x7d0 | el0_svc_common+0x1dc/0x268 | do_el0_svc+0x70/0x1a4 | el0_svc+0x58/0x14c | el0t_64_sync_handler+0x84/0xf0 | el0t_64_sync+0x190/0x194 | Code: 72906191 72a90ef1 6b11021f 54000040 (d4304740) | ---[ end trace 0000000000000000 ]--- This happens because the function prototype of tp_sub_func doesn't match the prototype of the tracepoint function. As each tracepoint may have a distinct prototype, it's not possible to share a common stub function. Avoid this by creating a stub function for each tracepoint with the correct prototype, and using these rather than tp_stub_func. Signed-off-by: Mark Rutland Cc: Steven Rostedt Cc: Masami Hiramatsu stub; =20 if (WARN_ON(!tp_func->func)) return ERR_PTR(-EINVAL); @@ -193,7 +189,7 @@ func_add(struct tracepoint_func **funcs, struct tracepo= int_func *tp_func, if (old) { /* (N -> N+1), (N !=3D 0, 1) probes */ for (iter_probes =3D 0; old[iter_probes].func; iter_probes++) { - if (old[iter_probes].func =3D=3D tp_stub_func) + if (old[iter_probes].func =3D=3D stub_func) continue; /* Skip stub functions. */ if (old[iter_probes].func =3D=3D tp_func->func && old[iter_probes].data =3D=3D tp_func->data) @@ -208,7 +204,7 @@ func_add(struct tracepoint_func **funcs, struct tracepo= int_func *tp_func, if (old) { nr_probes =3D 0; for (iter_probes =3D 0; old[iter_probes].func; iter_probes++) { - if (old[iter_probes].func =3D=3D tp_stub_func) + if (old[iter_probes].func =3D=3D stub_func) continue; /* Insert before probes of lower priority */ if (pos < 0 && old[iter_probes].prio < prio) @@ -229,11 +225,12 @@ func_add(struct tracepoint_func **funcs, struct trace= point_func *tp_func, return old; } =20 -static void *func_remove(struct tracepoint_func **funcs, - struct tracepoint_func *tp_func) +static void *func_remove(struct tracepoint *tp, struct tracepoint_func **f= uncs, + struct tracepoint_func *tp_func) { int nr_probes =3D 0, nr_del =3D 0, i; struct tracepoint_func *old, *new; + void *stub_func =3D tp->stub; =20 old =3D *funcs; =20 @@ -246,7 +243,7 @@ static void *func_remove(struct tracepoint_func **funcs, for (nr_probes =3D 0; old[nr_probes].func; nr_probes++) { if ((old[nr_probes].func =3D=3D tp_func->func && old[nr_probes].data =3D=3D tp_func->data) || - old[nr_probes].func =3D=3D tp_stub_func) + old[nr_probes].func =3D=3D stub_func) nr_del++; } } @@ -269,7 +266,7 @@ static void *func_remove(struct tracepoint_func **funcs, for (i =3D 0; old[i].func; i++) { if ((old[i].func !=3D tp_func->func || old[i].data !=3D tp_func->data) && - old[i].func !=3D tp_stub_func) + old[i].func !=3D stub_func) new[j++] =3D old[i]; } new[nr_probes - nr_del].func =3D NULL; @@ -277,12 +274,12 @@ static void *func_remove(struct tracepoint_func **fun= cs, } else { /* * Failed to allocate, replace the old function - * with calls to tp_stub_func. + * with calls to stub_func. */ for (i =3D 0; old[i].func; i++) { if (old[i].func =3D=3D tp_func->func && old[i].data =3D=3D tp_func->data) - WRITE_ONCE(old[i].func, tp_stub_func); + WRITE_ONCE(old[i].func, stub_func); } *funcs =3D old; } @@ -335,7 +332,7 @@ static int tracepoint_add_func(struct tracepoint *tp, =20 tp_funcs =3D rcu_dereference_protected(tp->funcs, lockdep_is_held(&tracepoints_mutex)); - old =3D func_add(&tp_funcs, func, prio); + old =3D func_add(tp, &tp_funcs, func, prio); if (IS_ERR(old)) { WARN_ON_ONCE(warn && PTR_ERR(old) !=3D -ENOMEM); return PTR_ERR(old); @@ -400,7 +397,7 @@ static int tracepoint_remove_func(struct tracepoint *tp, =20 tp_funcs =3D rcu_dereference_protected(tp->funcs, lockdep_is_held(&tracepoints_mutex)); - old =3D func_remove(&tp_funcs, func); + old =3D func_remove(tp, &tp_funcs, func); if (WARN_ON_ONCE(IS_ERR(old))) return PTR_ERR(old); =20 --=20 2.30.2