From nobody Mon Feb 9 14:14:55 2026 Received: from mail-pl1-f195.google.com (mail-pl1-f195.google.com [209.85.214.195]) (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 656F03054FD for ; Fri, 14 Nov 2025 09:25:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.195 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763112308; cv=none; b=A13eeIbW6VPRGB74Fu1JyQicC6flTcPdoemACjc3AQ5TmIPZDs1bGg1dI9vUHDWsxV9Cm4839b2D0+9wkFHL0p27KBC1sNexrWbU1en43LenxhRFhDlGXPvWH4j/2QuYoQXomwLDnC8pHSFsi0zLDO/IVJUekKWf9HoAo/iz1pU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763112308; c=relaxed/simple; bh=S0ECY3J4eY0S5MNW8rLP5hJohbui7zvgamXvzag6zbQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DEblW1KalBzDhYrtzQ+ZPuueE8Kyt1gqPObt5/epGFjSA+PBnWwnrVJvvN27aSY2hQu6nUPtyh7B/s/QU7kDTVdEYCgAZfHCmCeazzOWyI1EQi1HN1AFez2FDkmaMtxNR78t0s2ENnmAR53H5msah1yli4ix2YeK/fXELq5ROTg= 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=aEROZlaW; arc=none smtp.client-ip=209.85.214.195 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="aEROZlaW" Received: by mail-pl1-f195.google.com with SMTP id d9443c01a7336-297e982506fso19955055ad.2 for ; Fri, 14 Nov 2025 01:25:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763112304; x=1763717104; 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=y5AEyxkBUzWQX1qPT1Lf5tl2SWOdn4lIjFoqvWNFFWs=; b=aEROZlaWHJLaj7IaklCRtTDlcCI2NHdeeEeSwRlV20jKz0x88Vf/sALXcUw88b/7Py aA0gSKPXMWQ2sFFOb+KdE65/HQit0w/OV6AreHSXgmebytivLE/VcYlUnee831kZ8BR4 89XjWAKeVCj092zA7Vi4HBebQtopfI46yeTVgXc6Oh+8Xx8tiHkLKBAu+HgavvRlTZ9q 5Ud7RGX038yfwfkLvnOcH06ofFEhZmjRt9hvYSbaVRTLYCV1JkvUxegULl/npM/UdYRZ Mfo1JcXgYFhcUMZOyRej/9LxhQpJoMYkYkG30FjhmWIbPkDGr4sS/0wbA9YimTo2OrGi otlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763112304; x=1763717104; 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=y5AEyxkBUzWQX1qPT1Lf5tl2SWOdn4lIjFoqvWNFFWs=; b=JzoCpwa6YxXumKVclHomUd/GPsmCCc1i3Ntc5MFKCV5OxcKqAH67PoUTym6kglMy2S xR48Ul8oN7hPgqH6AoJ8pm1HWNUnRCyS2NYqE9/hyZPhubkj1HHEBFTOK+9lK/lXzhm+ YEIJFO07GOBNV6XLKWBdLD4iTkJJ0kTKg5rNfegUF/3Xj/DK4gQYQK6oXnmISYuT7vsF WpDj69XruNAXQn2GS6PFCDsqqPDWQbAKdweLiHHclritu9QYqoEHA2lobpbfX8IKlXdp SSZ0OswGOCtBS57Im5gDhBCmUWMNnbnDBcO8w4VCxaz2rOopOxrMPqB5z3GX+aU1/DpO VPUw== X-Forwarded-Encrypted: i=1; AJvYcCXITiAMj2HAEOY1cDo84tMPTRBVJiKjgygg9Vg7gkCfPn6Xi/FaKzLdBSqh68gJrTH2+fgd4qSNaPrDVec=@vger.kernel.org X-Gm-Message-State: AOJu0YwkQ02/2cQl9IEHv7Lei2xBEOvHbRxL1DupqY1M0m16y+DAL21H nDfXvMZ0QOahmAqhEUmh3rEVABjWuHKO/CqeQfELlQ214L9UjLU6ftco X-Gm-Gg: ASbGncs2jZDCW2TShOszgWa4K3rckNrOGWzZ5FKzowY7LQvwiYdo4Y5qPJ+cUJY1g90 3lveeMIa/0IB1F4syKSSJiQPr/oofaapX/ddIHPNotzq438CNE0+BCmSmJINXqOFJmRCWsHV7RL ibzPht3lv43EEGDT1JeAn+r+c0vE9E0qpI4AZN1XqPeUsVQM4t56qcJC9y9q+Jkolw2CbMKGyYM RznxWizcQzP+on/16o7LTIejAyxp52qvykCmWjCWtKl0ZfpTToFR7ewd4tEwLQ1m2jLFexZ7p+E j6LTOwBSYfmO2loz2acMorre47miF4YPtAVRptuhjbH1Jdo6iw2rfI2fxIuJV50aosxNn+iWhr3 Y9tCoLDgYrUlA56dPNJgL0M0lpvAKdIdliqTMxXvOeBwNgxRz0/CZbyWgthzJL9afhPY1K5bnaB vK1lgIAOjCL3E= X-Google-Smtp-Source: AGHT+IFnh52crzYkS3TAz7VEtpDOKOIs0IFpocAnXLP18gr0pPhgLCo1zXXKhdHJbPjGce+rZZ8OBQ== X-Received: by 2002:a17:902:da83:b0:298:90f:5b01 with SMTP id d9443c01a7336-2986a76f3e0mr26574065ad.52.1763112303652; Fri, 14 Nov 2025 01:25:03 -0800 (PST) Received: from 7950hx ([43.129.244.20]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2985c2346dasm50451525ad.7.2025.11.14.01.24.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Nov 2025 01:25:03 -0800 (PST) From: Menglong Dong X-Google-Original-From: Menglong Dong To: ast@kernel.org, rostedt@goodmis.org Cc: daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, mhiramat@kernel.org, mark.rutland@arm.com, mathieu.desnoyers@efficios.com, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH RFC bpf-next 1/7] ftrace: introduce FTRACE_OPS_FL_JMP Date: Fri, 14 Nov 2025 17:24:44 +0800 Message-ID: <20251114092450.172024-2-dongml2@chinatelecom.cn> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20251114092450.172024-1-dongml2@chinatelecom.cn> References: <20251114092450.172024-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" For now, the "nop" will be replaced with a "call" instruction when a function is hooked by the ftrace. However, sometimes the "call" can break the RSB and introduce extra overhead. Therefore, introduce the flag FTRACE_OPS_FL_JMP, which indicate that the ftrace_ops should be called with a "jmp" instead of "call". For now, it is only used by the direct call case. When a direct ftrace_ops is marked with FTRACE_OPS_FL_JMP, the last bit of the ops->direct_call will be set to 1. Therefore, we can tell if we should use "jmp" for the callback in ftrace_call_replace(). Signed-off-by: Menglong Dong --- include/linux/ftrace.h | 33 +++++++++++++++++++++++++++++++++ kernel/trace/Kconfig | 12 ++++++++++++ kernel/trace/ftrace.c | 9 ++++++++- 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h index 7ded7df6e9b5..14705dec1b08 100644 --- a/include/linux/ftrace.h +++ b/include/linux/ftrace.h @@ -351,6 +351,7 @@ enum { FTRACE_OPS_FL_DIRECT =3D BIT(17), FTRACE_OPS_FL_SUBOP =3D BIT(18), FTRACE_OPS_FL_GRAPH =3D BIT(19), + FTRACE_OPS_FL_JMP =3D BIT(20), }; =20 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_ARGS @@ -569,6 +570,38 @@ static inline void arch_ftrace_set_direct_caller(struc= t ftrace_regs *fregs, unsigned long addr) { } #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */ =20 +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_JMP +static inline bool ftrace_is_jmp(unsigned long addr) +{ + return addr & 1; +} + +static inline unsigned long ftrace_jmp_set(unsigned long addr) +{ + return addr | 1UL; +} + +static inline unsigned long ftrace_jmp_get(unsigned long addr) +{ + return addr & ~1UL; +} +#else +static inline bool ftrace_is_jmp(unsigned long addr) +{ + return false; +} + +static inline unsigned long ftrace_jmp_set(unsigned long addr) +{ + return addr; +} + +static inline unsigned long ftrace_jmp_get(unsigned long addr) +{ + return addr; +} +#endif /* CONFIG_DYNAMIC_FTRACE_WITH_JMP */ + #ifdef CONFIG_STACK_TRACER =20 int stack_trace_sysctl(const struct ctl_table *table, int write, void *buf= fer, diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig index d2c79da81e4f..4661b9e606e0 100644 --- a/kernel/trace/Kconfig +++ b/kernel/trace/Kconfig @@ -80,6 +80,12 @@ config HAVE_DYNAMIC_FTRACE_NO_PATCHABLE If the architecture generates __patchable_function_entries sections but does not want them included in the ftrace locations. =20 +config HAVE_DYNAMIC_FTRACE_WITH_JMP + bool + help + If the architecture supports to replace the __fentry__ with a + "jmp" instruction. + config HAVE_SYSCALL_TRACEPOINTS bool help @@ -330,6 +336,12 @@ config DYNAMIC_FTRACE_WITH_ARGS depends on DYNAMIC_FTRACE depends on HAVE_DYNAMIC_FTRACE_WITH_ARGS =20 +config DYNAMIC_FTRACE_WITH_JMP + def_bool y + depends on DYNAMIC_FTRACE + depends on DYNAMIC_FTRACE_WITH_DIRECT_CALLS + depends on HAVE_DYNAMIC_FTRACE_WITH_JMP + config FPROBE bool "Kernel Function Probe (fprobe)" depends on HAVE_FUNCTION_GRAPH_FREGS && HAVE_FTRACE_GRAPH_FUNC diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index efb5ce32298f..8d7b2a7f4b15 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -5938,7 +5938,8 @@ static void remove_direct_functions_hash(struct ftrac= e_hash *hash, unsigned long for (i =3D 0; i < size; i++) { hlist_for_each_entry(entry, &hash->buckets[i], hlist) { del =3D __ftrace_lookup_ip(direct_functions, entry->ip); - if (del && del->direct =3D=3D addr) { + if (del && ftrace_jmp_get(del->direct) =3D=3D + ftrace_jmp_get(addr)) { remove_hash_entry(direct_functions, del); kfree(del); } @@ -5994,6 +5995,9 @@ int register_ftrace_direct(struct ftrace_ops *ops, un= signed long addr) =20 mutex_lock(&direct_mutex); =20 + if (ops->flags & FTRACE_OPS_FL_JMP) + addr =3D ftrace_jmp_set(addr); + /* Make sure requested entries are not already registered.. */ size =3D 1 << hash->size_bits; for (i =3D 0; i < size; i++) { @@ -6117,6 +6121,9 @@ __modify_ftrace_direct(struct ftrace_ops *ops, unsign= ed long addr) =20 lockdep_assert_held_once(&direct_mutex); =20 + if (ops->flags & FTRACE_OPS_FL_JMP) + addr =3D ftrace_jmp_set(addr); + /* Enable the tmp_ops to have the same functions as the direct ops */ ftrace_ops_init(&tmp_ops); tmp_ops.func_hash =3D ops->func_hash; --=20 2.51.2