linux-trace-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 0/7] Fix the failure issue of the module_attach test case
@ 2025-12-17  6:14 Chenghao Duan
  2025-12-17  6:14 ` [PATCH v4 1/7] LoongArch: ftrace: Refactor register restoration in ftrace_common_return Chenghao Duan
                   ` (6 more replies)
  0 siblings, 7 replies; 15+ messages in thread
From: Chenghao Duan @ 2025-12-17  6:14 UTC (permalink / raw)
  To: yangtiezhu, rostedt, mhiramat, mark.rutland, hengqi.chen,
	chenhuacai
  Cc: kernel, zhangtianyang, masahiroy, linux-kernel, loongarch, bpf,
	duanchenghao, youling.tang, jianghaoran, vincent.mc.li,
	linux-trace-kernel

v4:
My sincere apologies for the misunderstanding in my interpretation of
Tiezhu's reply in the v2 email thread. After the release of v3, Tiezhu
reminded me again of the importance of splitting the patches, and I am
very grateful for this. Below are the changes made for v4.

Split the patch titled "LoongArch: BPF: Enhance trampoline support for
kernel and module tracing" into the following smaller patches by
functionality to facilitate code reading and review, while ensuring the
code logic and modification locations remain unchanged:
"LoongArch: BPF: Enable and fix trampoline-based tracing for module functions"
"LoongArch: BPF: Save return address register ra to t0 before trampoline"
"LoongArch: BPF: Adjust the jump offset of tail calls"
"LoongArch: BPF: Enhance the bpf_arch_text_poke() function"

--------------------------Changelog------------------------------------
v3:
links: https://lore.kernel.org/all/20251216094753.1317231-1-duanchenghao@kylinos.cn/
1. Adjust the position of fixup_exception() in the patch "LoongArch:
Enable exception fixup for specific ADE": move its invocation to within
the code block wrapped by irqentry_enter()/irqentry_exit().

2. Add the relevant test items to the patch commit log.

3. Adjust the sequence of patches

With the exception of the patch "LoongArch: Enable exception fixup for
specific ADE", no source code changes have been made in the other
patches.

v2:
links: https://lore.kernel.org/all/20251212091103.1247753-1-duanchenghao@kylinos.cn/
Referring to Tiezhu's suggestion, split the v1 patch titled
"LoongArch: Modify the jump logic of the trampoline" into three parts:
 (1) ftrace code
 (2) sample test
 (3) bpf code
The code logic and implementation remain unchanged.

v1:
links: https://lore.kernel.org/all/20251209093405.1309253-1-duanchenghao@kylinos.cn/
The following test cases under the tools/testing/selftests/bpf/
directory have passed the test:

./test_progs -t module_attach
./test_progs -t module_fentry_shadow
./test_progs -t subprogs
./test_progs -t subprogs_extable
./test_progs -t tailcalls
./test_progs -t struct_ops -d struct_ops_multi_pages
./test_progs -t fexit_bpf2bpf
./test_progs -t fexit_stress
./test_progs -t module_fentry_shadow
./test_progs -t fentry_test/fentry
./test_progs -t fexit_test/fexit
./test_progs -t fentry_fexit
./test_progs -t modify_return
./test_progs -t fexit_sleep
./test_progs -t test_overhead
./test_progs -t trampoline_count

Chenghao Duan (7):
  LoongArch: ftrace: Refactor register restoration in
    ftrace_common_return
  LoongArch: Enable exception fixup for specific ADE subcode
  LoongArch: BPF: Enable and fix trampoline-based tracing for module
    functions
  LoongArch: BPF: Save return address register ra to t0 before
    trampoline
  LoongArch: BPF: Adjust the jump offset of tail calls
  LoongArch: BPF: Enhance the bpf_arch_text_poke() function
  LoongArch: ftrace: Adjust register stack restore order in direct call
    trampolines

 arch/loongarch/kernel/mcount_dyn.S          | 14 +++++---
 arch/loongarch/kernel/traps.c               |  9 ++++-
 arch/loongarch/net/bpf_jit.c                | 38 +++++++++++++++------
 samples/ftrace/ftrace-direct-modify.c       |  8 ++---
 samples/ftrace/ftrace-direct-multi-modify.c |  8 ++---
 samples/ftrace/ftrace-direct-multi.c        |  4 +--
 samples/ftrace/ftrace-direct-too.c          |  4 +--
 samples/ftrace/ftrace-direct.c              |  4 +--
 8 files changed, 59 insertions(+), 30 deletions(-)

-- 
2.25.1


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v4 1/7] LoongArch: ftrace: Refactor register restoration in ftrace_common_return
  2025-12-17  6:14 [PATCH v4 0/7] Fix the failure issue of the module_attach test case Chenghao Duan
@ 2025-12-17  6:14 ` Chenghao Duan
  2025-12-17  6:55   ` bot+bpf-ci
  2025-12-17  6:14 ` [PATCH v4 2/7] LoongArch: Enable exception fixup for specific ADE subcode Chenghao Duan
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 15+ messages in thread
From: Chenghao Duan @ 2025-12-17  6:14 UTC (permalink / raw)
  To: yangtiezhu, rostedt, mhiramat, mark.rutland, hengqi.chen,
	chenhuacai
  Cc: kernel, zhangtianyang, masahiroy, linux-kernel, loongarch, bpf,
	duanchenghao, youling.tang, jianghaoran, vincent.mc.li,
	linux-trace-kernel

Refactor the register restoration sequence in the ftrace_common_return
function to clearly distinguish between the logic of normal returns and
direct call returns in function tracing scenarios. The logic is as
follows:
1. In the case of a normal return, the execution flow returns to the
traced function, and ftrace must ensure that the register data is
consistent with the state when the function was entered.
ra = parent return address; t0 = traced function return address.

2. In the case of a direct call return, the execution flow jumps to the
custom trampoline function, and ftrace must ensure that the register
data is consistent with the state when ftrace was entered.
ra = traced function return address; t0 = parent return address.

Fixes: 9cdc3b6a299c ("LoongArch: ftrace: Add direct call support")
Signed-off-by: Chenghao Duan <duanchenghao@kylinos.cn>
---
 arch/loongarch/kernel/mcount_dyn.S | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/arch/loongarch/kernel/mcount_dyn.S b/arch/loongarch/kernel/mcount_dyn.S
index d6b474ad1d5e..5729c20e5b8b 100644
--- a/arch/loongarch/kernel/mcount_dyn.S
+++ b/arch/loongarch/kernel/mcount_dyn.S
@@ -94,7 +94,6 @@ SYM_INNER_LABEL(ftrace_graph_call, SYM_L_GLOBAL)
  * at the callsite, so there is no need to restore the T series regs.
  */
 ftrace_common_return:
-	PTR_L		ra, sp, PT_R1
 	PTR_L		a0, sp, PT_R4
 	PTR_L		a1, sp, PT_R5
 	PTR_L		a2, sp, PT_R6
@@ -104,12 +103,17 @@ ftrace_common_return:
 	PTR_L		a6, sp, PT_R10
 	PTR_L		a7, sp, PT_R11
 	PTR_L		fp, sp, PT_R22
-	PTR_L		t0, sp, PT_ERA
 	PTR_L		t1, sp, PT_R13
-	PTR_ADDI	sp, sp, PT_SIZE
 	bnez		t1, .Ldirect
+
+	PTR_L		ra, sp, PT_R1
+	PTR_L		t0, sp, PT_ERA
+	PTR_ADDI	sp, sp, PT_SIZE
 	jr		t0
 .Ldirect:
+	PTR_L		t0, sp, PT_R1
+	PTR_L		ra, sp, PT_ERA
+	PTR_ADDI	sp, sp, PT_SIZE
 	jr		t1
 SYM_CODE_END(ftrace_common)
 
@@ -161,6 +165,8 @@ SYM_CODE_END(return_to_handler)
 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
 SYM_CODE_START(ftrace_stub_direct_tramp)
 	UNWIND_HINT_UNDEFINED
-	jr		t0
+	move		t1, ra
+	move		ra, t0
+	jr		t1
 SYM_CODE_END(ftrace_stub_direct_tramp)
 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v4 2/7] LoongArch: Enable exception fixup for specific ADE subcode
  2025-12-17  6:14 [PATCH v4 0/7] Fix the failure issue of the module_attach test case Chenghao Duan
  2025-12-17  6:14 ` [PATCH v4 1/7] LoongArch: ftrace: Refactor register restoration in ftrace_common_return Chenghao Duan
@ 2025-12-17  6:14 ` Chenghao Duan
  2025-12-17  6:14 ` [PATCH v4 3/7] LoongArch: BPF: Enable and fix trampoline-based tracing for module functions Chenghao Duan
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 15+ messages in thread
From: Chenghao Duan @ 2025-12-17  6:14 UTC (permalink / raw)
  To: yangtiezhu, rostedt, mhiramat, mark.rutland, hengqi.chen,
	chenhuacai
  Cc: kernel, zhangtianyang, masahiroy, linux-kernel, loongarch, bpf,
	duanchenghao, youling.tang, jianghaoran, vincent.mc.li,
	linux-trace-kernel

This patch allows the LoongArch BPF JIT to handle recoverable memory
access errors generated by BPF_PROBE_MEM* instructions.

When a BPF program performs memory access operations, the instructions
it executes may trigger ADEM exceptions. The kernel’s built-in BPF
exception table mechanism (EX_TYPE_BPF) will generate corresponding
exception fixup entries in the JIT compilation phase; however, the
architecture-specific trap handling function needs to proactively call
the common fixup routine to achieve exception recovery.

do_ade(): fix EX_TYPE_BPF memory access exceptions for BPF programs,
ensure safe execution.

Relevant test cases: illegal address access tests in module_attach and
subprogs_extable of selftests/bpf

Signed-off-by: Chenghao Duan <duanchenghao@kylinos.cn>
---
 arch/loongarch/kernel/traps.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/arch/loongarch/kernel/traps.c b/arch/loongarch/kernel/traps.c
index 004b8ebf0051..201c9a5532f4 100644
--- a/arch/loongarch/kernel/traps.c
+++ b/arch/loongarch/kernel/traps.c
@@ -534,11 +534,18 @@ asmlinkage void noinstr do_fpe(struct pt_regs *regs, unsigned long fcsr)
 
 asmlinkage void noinstr do_ade(struct pt_regs *regs)
 {
-	irqentry_state_t state = irqentry_enter(regs);
+	irqentry_state_t state;
+	unsigned int esubcode = FIELD_GET(CSR_ESTAT_ESUBCODE, regs->csr_estat);
+
+	state = irqentry_enter(regs);
+
+	if ((esubcode == EXSUBCODE_ADEM) && fixup_exception(regs))
+		goto out;
 
 	die_if_kernel("Kernel ade access", regs);
 	force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)regs->csr_badvaddr);
 
+out:
 	irqentry_exit(regs, state);
 }
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v4 3/7] LoongArch: BPF: Enable and fix trampoline-based tracing for module functions
  2025-12-17  6:14 [PATCH v4 0/7] Fix the failure issue of the module_attach test case Chenghao Duan
  2025-12-17  6:14 ` [PATCH v4 1/7] LoongArch: ftrace: Refactor register restoration in ftrace_common_return Chenghao Duan
  2025-12-17  6:14 ` [PATCH v4 2/7] LoongArch: Enable exception fixup for specific ADE subcode Chenghao Duan
@ 2025-12-17  6:14 ` Chenghao Duan
  2025-12-17  6:14 ` [PATCH v4 4/7] LoongArch: BPF: Save return address register ra to t0 before trampoline Chenghao Duan
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 15+ messages in thread
From: Chenghao Duan @ 2025-12-17  6:14 UTC (permalink / raw)
  To: yangtiezhu, rostedt, mhiramat, mark.rutland, hengqi.chen,
	chenhuacai
  Cc: kernel, zhangtianyang, masahiroy, linux-kernel, loongarch, bpf,
	duanchenghao, youling.tang, jianghaoran, vincent.mc.li,
	linux-trace-kernel

Remove the previous restrictions that blocked the tracing of kernel
module functions. Fix the issue that previously caused kernel lockups
when attempting to trace module functions.

Before entering the trampoline code, the return address register ra
shall store the address of the next assembly instruction after the
'bl trampoline' instruction, which is the traced function address, and
the register t0 shall store the parent function return address. Refine
the trampoline return logic to ensure that register data remains
correct when returning to both the traced function and the parent
function.

Before this patch was applied, the module_attach test in selftests/bpf
encountered a deadlock issue. This was caused by an incorrect jump
address after the trampoline execution, which resulted in an infinite
loop within the module function.

Fixes: 677e6123e3d2 ("LoongArch: BPF: Disable trampoline for kernel module function trace")

Signed-off-by: Chenghao Duan <duanchenghao@kylinos.cn>
---
 arch/loongarch/net/bpf_jit.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
index 8dc58781b8eb..76cd24646bec 100644
--- a/arch/loongarch/net/bpf_jit.c
+++ b/arch/loongarch/net/bpf_jit.c
@@ -1265,7 +1265,7 @@ static int emit_jump_or_nops(void *target, void *ip, u32 *insns, bool is_call)
 		return 0;
 	}
 
-	return emit_jump_and_link(&ctx, is_call ? LOONGARCH_GPR_T0 : LOONGARCH_GPR_ZERO, (u64)target);
+	return emit_jump_and_link(&ctx, is_call ? LOONGARCH_GPR_RA : LOONGARCH_GPR_ZERO, (u64)target);
 }
 
 static int emit_call(struct jit_ctx *ctx, u64 addr)
@@ -1622,14 +1622,12 @@ static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_i
 
 	/* To traced function */
 	/* Ftrace jump skips 2 NOP instructions */
-	if (is_kernel_text((unsigned long)orig_call))
+	if (is_kernel_text((unsigned long)orig_call) ||
+	    is_module_text_address((unsigned long)orig_call))
 		orig_call += LOONGARCH_FENTRY_NBYTES;
 	/* Direct jump skips 5 NOP instructions */
 	else if (is_bpf_text_address((unsigned long)orig_call))
 		orig_call += LOONGARCH_BPF_FENTRY_NBYTES;
-	/* Module tracing not supported - cause kernel lockups */
-	else if (is_module_text_address((unsigned long)orig_call))
-		return -ENOTSUPP;
 
 	if (flags & BPF_TRAMP_F_CALL_ORIG) {
 		move_addr(ctx, LOONGARCH_GPR_A0, (const u64)im);
@@ -1722,12 +1720,16 @@ static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_i
 		emit_insn(ctx, ldd, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, 0);
 		emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP, 16);
 
-		if (flags & BPF_TRAMP_F_SKIP_FRAME)
+		if (flags & BPF_TRAMP_F_SKIP_FRAME) {
 			/* return to parent function */
-			emit_insn(ctx, jirl, LOONGARCH_GPR_ZERO, LOONGARCH_GPR_RA, 0);
-		else
-			/* return to traced function */
+			move_reg(ctx, LOONGARCH_GPR_RA, LOONGARCH_GPR_T0);
 			emit_insn(ctx, jirl, LOONGARCH_GPR_ZERO, LOONGARCH_GPR_T0, 0);
+		} else {
+			/* return to traced function */
+			move_reg(ctx, LOONGARCH_GPR_T1, LOONGARCH_GPR_RA);
+			move_reg(ctx, LOONGARCH_GPR_RA, LOONGARCH_GPR_T0);
+			emit_insn(ctx, jirl, LOONGARCH_GPR_ZERO, LOONGARCH_GPR_T1, 0);
+		}
 	}
 
 	ret = ctx->idx;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v4 4/7] LoongArch: BPF: Save return address register ra to t0 before trampoline
  2025-12-17  6:14 [PATCH v4 0/7] Fix the failure issue of the module_attach test case Chenghao Duan
                   ` (2 preceding siblings ...)
  2025-12-17  6:14 ` [PATCH v4 3/7] LoongArch: BPF: Enable and fix trampoline-based tracing for module functions Chenghao Duan
@ 2025-12-17  6:14 ` Chenghao Duan
  2025-12-17  6:14 ` [PATCH v4 5/7] LoongArch: BPF: Adjust the jump offset of tail calls Chenghao Duan
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 15+ messages in thread
From: Chenghao Duan @ 2025-12-17  6:14 UTC (permalink / raw)
  To: yangtiezhu, rostedt, mhiramat, mark.rutland, hengqi.chen,
	chenhuacai
  Cc: kernel, zhangtianyang, masahiroy, linux-kernel, loongarch, bpf,
	duanchenghao, youling.tang, jianghaoran, vincent.mc.li,
	linux-trace-kernel

Modify the build_prologue function to ensure the return address
register ra is saved to t0 before entering trampoline operations.
This change ensures accurate return address handling when a BPF
program calls another BPF program, preventing errors in the
BPF-to-BPF call chain.

Signed-off-by: Chenghao Duan <duanchenghao@kylinos.cn>
---
 arch/loongarch/net/bpf_jit.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
index 76cd24646bec..c560d1e14b9d 100644
--- a/arch/loongarch/net/bpf_jit.c
+++ b/arch/loongarch/net/bpf_jit.c
@@ -139,6 +139,7 @@ static void build_prologue(struct jit_ctx *ctx)
 	stack_adjust = round_up(stack_adjust, 16);
 	stack_adjust += bpf_stack_adjust;
 
+	move_reg(ctx, LOONGARCH_GPR_T0, LOONGARCH_GPR_RA);
 	/* Reserve space for the move_imm + jirl instruction */
 	for (i = 0; i < LOONGARCH_LONG_JUMP_NINSNS; i++)
 		emit_insn(ctx, nop);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v4 5/7] LoongArch: BPF: Adjust the jump offset of tail calls
  2025-12-17  6:14 [PATCH v4 0/7] Fix the failure issue of the module_attach test case Chenghao Duan
                   ` (3 preceding siblings ...)
  2025-12-17  6:14 ` [PATCH v4 4/7] LoongArch: BPF: Save return address register ra to t0 before trampoline Chenghao Duan
@ 2025-12-17  6:14 ` Chenghao Duan
  2025-12-17  6:14 ` [PATCH v4 6/7] LoongArch: BPF: Enhance the bpf_arch_text_poke() function Chenghao Duan
  2025-12-17  6:14 ` [PATCH v4 7/7] LoongArch: ftrace: Adjust register stack restore order in direct call trampolines Chenghao Duan
  6 siblings, 0 replies; 15+ messages in thread
From: Chenghao Duan @ 2025-12-17  6:14 UTC (permalink / raw)
  To: yangtiezhu, rostedt, mhiramat, mark.rutland, hengqi.chen,
	chenhuacai
  Cc: kernel, zhangtianyang, masahiroy, linux-kernel, loongarch, bpf,
	duanchenghao, youling.tang, jianghaoran, vincent.mc.li,
	linux-trace-kernel

Call the next bpf prog and skip the first instruction of TCC
initialization.
A total of 7 instructions are skipped:
'move t0, ra'			1 inst
'move_imm + jirl'		5 inst
'addid REG_TCC, zero, 0'	1 inst

Relevant test cases: the tailcalls test item in selftests/bpf

Signed-off-by: Chenghao Duan <duanchenghao@kylinos.cn>
---
 arch/loongarch/net/bpf_jit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
index c560d1e14b9d..3dbabacc8856 100644
--- a/arch/loongarch/net/bpf_jit.c
+++ b/arch/loongarch/net/bpf_jit.c
@@ -239,7 +239,7 @@ static void __build_epilogue(struct jit_ctx *ctx, bool is_tail_call)
 		 * Call the next bpf prog and skip the first instruction
 		 * of TCC initialization.
 		 */
-		emit_insn(ctx, jirl, LOONGARCH_GPR_ZERO, LOONGARCH_GPR_T3, 6);
+		emit_insn(ctx, jirl, LOONGARCH_GPR_ZERO, LOONGARCH_GPR_T3, 7);
 	}
 }
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v4 6/7] LoongArch: BPF: Enhance the bpf_arch_text_poke() function
  2025-12-17  6:14 [PATCH v4 0/7] Fix the failure issue of the module_attach test case Chenghao Duan
                   ` (4 preceding siblings ...)
  2025-12-17  6:14 ` [PATCH v4 5/7] LoongArch: BPF: Adjust the jump offset of tail calls Chenghao Duan
@ 2025-12-17  6:14 ` Chenghao Duan
  2025-12-20 14:07   ` Hengqi Chen
  2025-12-17  6:14 ` [PATCH v4 7/7] LoongArch: ftrace: Adjust register stack restore order in direct call trampolines Chenghao Duan
  6 siblings, 1 reply; 15+ messages in thread
From: Chenghao Duan @ 2025-12-17  6:14 UTC (permalink / raw)
  To: yangtiezhu, rostedt, mhiramat, mark.rutland, hengqi.chen,
	chenhuacai
  Cc: kernel, zhangtianyang, masahiroy, linux-kernel, loongarch, bpf,
	duanchenghao, youling.tang, jianghaoran, vincent.mc.li,
	linux-trace-kernel

Enhance the bpf_arch_text_poke() function to enable accurate location
of BPF program entry points.

When modifying the entry point of a BPF program, skip the move t0, ra
instruction to ensure the correct logic and copy of the jump address.

Signed-off-by: Chenghao Duan <duanchenghao@kylinos.cn>
---
 arch/loongarch/net/bpf_jit.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
index 3dbabacc8856..0c16a1b18e8f 100644
--- a/arch/loongarch/net/bpf_jit.c
+++ b/arch/loongarch/net/bpf_jit.c
@@ -1290,6 +1290,10 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
 		       void *new_addr)
 {
 	int ret;
+	unsigned long size = 0;
+	unsigned long offset = 0;
+	char namebuf[KSYM_NAME_LEN];
+	void *image = NULL;
 	bool is_call;
 	u32 old_insns[LOONGARCH_LONG_JUMP_NINSNS] = {[0 ... 4] = INSN_NOP};
 	u32 new_insns[LOONGARCH_LONG_JUMP_NINSNS] = {[0 ... 4] = INSN_NOP};
@@ -1297,9 +1301,18 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
 	/* Only poking bpf text is supported. Since kernel function entry
 	 * is set up by ftrace, we rely on ftrace to poke kernel functions.
 	 */
-	if (!is_bpf_text_address((unsigned long)ip))
+	if (!__bpf_address_lookup((unsigned long)ip, &size, &offset, namebuf))
 		return -ENOTSUPP;
 
+	image = ip - offset;
+	/* zero offset means we're poking bpf prog entry */
+	if (offset == 0)
+		/* skip to the nop instruction in bpf prog entry:
+		 * move t0, ra
+		 * nop
+		 */
+		ip = image + LOONGARCH_INSN_SIZE;
+
 	is_call = old_t == BPF_MOD_CALL;
 	ret = emit_jump_or_nops(old_addr, ip, old_insns, is_call);
 	if (ret)
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v4 7/7] LoongArch: ftrace: Adjust register stack restore order in direct call trampolines
  2025-12-17  6:14 [PATCH v4 0/7] Fix the failure issue of the module_attach test case Chenghao Duan
                   ` (5 preceding siblings ...)
  2025-12-17  6:14 ` [PATCH v4 6/7] LoongArch: BPF: Enhance the bpf_arch_text_poke() function Chenghao Duan
@ 2025-12-17  6:14 ` Chenghao Duan
  6 siblings, 0 replies; 15+ messages in thread
From: Chenghao Duan @ 2025-12-17  6:14 UTC (permalink / raw)
  To: yangtiezhu, rostedt, mhiramat, mark.rutland, hengqi.chen,
	chenhuacai
  Cc: kernel, zhangtianyang, masahiroy, linux-kernel, loongarch, bpf,
	duanchenghao, youling.tang, jianghaoran, vincent.mc.li,
	linux-trace-kernel, Youling Tang

Ensure that in the ftrace direct call logic, the CPU register state
(with ra = parent return address) is restored to the correct state
after the execution of the custom trampoline function and before
returning to the traced function. Additionally, guarantee the
correctness of the jump logic for jr t0 (traced function address).

Reported-by: Youling Tang <tangyouling@kylinos.cn>
Signed-off-by: Chenghao Duan <duanchenghao@kylinos.cn>
Acked-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 samples/ftrace/ftrace-direct-modify.c       | 8 ++++----
 samples/ftrace/ftrace-direct-multi-modify.c | 8 ++++----
 samples/ftrace/ftrace-direct-multi.c        | 4 ++--
 samples/ftrace/ftrace-direct-too.c          | 4 ++--
 samples/ftrace/ftrace-direct.c              | 4 ++--
 5 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/samples/ftrace/ftrace-direct-modify.c b/samples/ftrace/ftrace-direct-modify.c
index da3a9f2091f5..1ba1927b548e 100644
--- a/samples/ftrace/ftrace-direct-modify.c
+++ b/samples/ftrace/ftrace-direct-modify.c
@@ -176,8 +176,8 @@ asm (
 "	st.d	$t0, $sp, 0\n"
 "	st.d	$ra, $sp, 8\n"
 "	bl	my_direct_func1\n"
-"	ld.d	$t0, $sp, 0\n"
-"	ld.d	$ra, $sp, 8\n"
+"	ld.d	$ra, $sp, 0\n"
+"	ld.d	$t0, $sp, 8\n"
 "	addi.d	$sp, $sp, 16\n"
 "	jr	$t0\n"
 "	.size		my_tramp1, .-my_tramp1\n"
@@ -189,8 +189,8 @@ asm (
 "	st.d	$t0, $sp, 0\n"
 "	st.d	$ra, $sp, 8\n"
 "	bl	my_direct_func2\n"
-"	ld.d	$t0, $sp, 0\n"
-"	ld.d	$ra, $sp, 8\n"
+"	ld.d	$ra, $sp, 0\n"
+"	ld.d	$t0, $sp, 8\n"
 "	addi.d	$sp, $sp, 16\n"
 "	jr	$t0\n"
 "	.size		my_tramp2, .-my_tramp2\n"
diff --git a/samples/ftrace/ftrace-direct-multi-modify.c b/samples/ftrace/ftrace-direct-multi-modify.c
index 8f7986d698d8..7a7822dfeb50 100644
--- a/samples/ftrace/ftrace-direct-multi-modify.c
+++ b/samples/ftrace/ftrace-direct-multi-modify.c
@@ -199,8 +199,8 @@ asm (
 "	move	$a0, $t0\n"
 "	bl	my_direct_func1\n"
 "	ld.d	$a0, $sp, 0\n"
-"	ld.d	$t0, $sp, 8\n"
-"	ld.d	$ra, $sp, 16\n"
+"	ld.d	$ra, $sp, 8\n"
+"	ld.d	$t0, $sp, 16\n"
 "	addi.d	$sp, $sp, 32\n"
 "	jr	$t0\n"
 "	.size		my_tramp1, .-my_tramp1\n"
@@ -215,8 +215,8 @@ asm (
 "	move	$a0, $t0\n"
 "	bl	my_direct_func2\n"
 "	ld.d	$a0, $sp, 0\n"
-"	ld.d	$t0, $sp, 8\n"
-"	ld.d	$ra, $sp, 16\n"
+"	ld.d	$ra, $sp, 8\n"
+"	ld.d	$t0, $sp, 16\n"
 "	addi.d	$sp, $sp, 32\n"
 "	jr	$t0\n"
 "	.size		my_tramp2, .-my_tramp2\n"
diff --git a/samples/ftrace/ftrace-direct-multi.c b/samples/ftrace/ftrace-direct-multi.c
index db326c81a27d..3fe6ddaf0b69 100644
--- a/samples/ftrace/ftrace-direct-multi.c
+++ b/samples/ftrace/ftrace-direct-multi.c
@@ -131,8 +131,8 @@ asm (
 "	move	$a0, $t0\n"
 "	bl	my_direct_func\n"
 "	ld.d	$a0, $sp, 0\n"
-"	ld.d	$t0, $sp, 8\n"
-"	ld.d	$ra, $sp, 16\n"
+"	ld.d	$ra, $sp, 8\n"
+"	ld.d	$t0, $sp, 16\n"
 "	addi.d	$sp, $sp, 32\n"
 "	jr	$t0\n"
 "	.size		my_tramp, .-my_tramp\n"
diff --git a/samples/ftrace/ftrace-direct-too.c b/samples/ftrace/ftrace-direct-too.c
index 3d0fa260332d..bf2411aa6fd7 100644
--- a/samples/ftrace/ftrace-direct-too.c
+++ b/samples/ftrace/ftrace-direct-too.c
@@ -143,8 +143,8 @@ asm (
 "	ld.d	$a0, $sp, 0\n"
 "	ld.d	$a1, $sp, 8\n"
 "	ld.d	$a2, $sp, 16\n"
-"	ld.d	$t0, $sp, 24\n"
-"	ld.d	$ra, $sp, 32\n"
+"	ld.d	$ra, $sp, 24\n"
+"	ld.d	$t0, $sp, 32\n"
 "	addi.d	$sp, $sp, 48\n"
 "	jr	$t0\n"
 "	.size		my_tramp, .-my_tramp\n"
diff --git a/samples/ftrace/ftrace-direct.c b/samples/ftrace/ftrace-direct.c
index 956834b0d19a..5368c8c39cbb 100644
--- a/samples/ftrace/ftrace-direct.c
+++ b/samples/ftrace/ftrace-direct.c
@@ -124,8 +124,8 @@ asm (
 "	st.d	$ra, $sp, 16\n"
 "	bl	my_direct_func\n"
 "	ld.d	$a0, $sp, 0\n"
-"	ld.d	$t0, $sp, 8\n"
-"	ld.d	$ra, $sp, 16\n"
+"	ld.d	$ra, $sp, 8\n"
+"	ld.d	$t0, $sp, 16\n"
 "	addi.d	$sp, $sp, 32\n"
 "	jr	$t0\n"
 "	.size		my_tramp, .-my_tramp\n"
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* Re: [PATCH v4 1/7] LoongArch: ftrace: Refactor register restoration in ftrace_common_return
  2025-12-17  6:14 ` [PATCH v4 1/7] LoongArch: ftrace: Refactor register restoration in ftrace_common_return Chenghao Duan
@ 2025-12-17  6:55   ` bot+bpf-ci
  2025-12-18  1:26     ` Chenghao Duan
  0 siblings, 1 reply; 15+ messages in thread
From: bot+bpf-ci @ 2025-12-17  6:55 UTC (permalink / raw)
  To: duanchenghao, yangtiezhu, rostedt, mhiramat, mark.rutland,
	hengqi.chen, chenhuacai
  Cc: kernel, zhangtianyang, masahiroy, linux-kernel, loongarch, bpf,
	duanchenghao, youling.tang, jianghaoran, vincent.mc.li,
	linux-trace-kernel, ast, andrii, daniel, martin.lau, eddyz87,
	yonghong.song, clm, ihor.solodrai

[-- Attachment #1: Type: text/plain, Size: 3096 bytes --]

> diff --git a/arch/loongarch/kernel/mcount_dyn.S b/arch/loongarch/kernel/mcount_dyn.S
> index d6b474ad1d5e..5729c20e5b8b 100644
> --- a/arch/loongarch/kernel/mcount_dyn.S
> +++ b/arch/loongarch/kernel/mcount_dyn.S
> @@ -94,7 +94,6 @@ SYM_INNER_LABEL(ftrace_graph_call, SYM_L_GLOBAL)
>   * at the callsite, so there is no need to restore the T series regs.
>   */
>  ftrace_common_return:
> -	PTR_L		ra, sp, PT_R1
>  	PTR_L		a0, sp, PT_R4
>  	PTR_L		a1, sp, PT_R5
>  	PTR_L		a2, sp, PT_R6
> @@ -104,12 +103,17 @@ ftrace_common_return:
>  	PTR_L		a6, sp, PT_R10
>  	PTR_L		a7, sp, PT_R11
>  	PTR_L		fp, sp, PT_R22
> -	PTR_L		t0, sp, PT_ERA
>  	PTR_L		t1, sp, PT_R13
> -	PTR_ADDI	sp, sp, PT_SIZE
>  	bnez		t1, .Ldirect
> +
> +	PTR_L		ra, sp, PT_R1
> +	PTR_L		t0, sp, PT_ERA
> +	PTR_ADDI	sp, sp, PT_SIZE
>  	jr		t0
>  .Ldirect:
> +	PTR_L		t0, sp, PT_R1
> +	PTR_L		ra, sp, PT_ERA
                     ^^^^^^^^^^^^^^^^^^^
Does this break existing ftrace direct trampolines?

Before this change, direct trampolines received:
  ra = parent return address (from PT_R1)
  t0 = traced function address (from PT_ERA)

After this change, direct trampolines receive:
  t0 = parent return address (from PT_R1)
  ra = traced function address (from PT_ERA)

The sample trampolines in samples/ftrace/ were written for the old
convention. For example, samples/ftrace/ftrace-direct.c does:

  my_tramp:
    st.d  $t0, $sp, 8    # Save what it thinks is traced func addr
    st.d  $ra, $sp, 16   # Save what it thinks is parent addr
    bl    my_direct_func
    ld.d  $t0, $sp, 8    # Restore
    ld.d  $ra, $sp, 16   # Restore
    jr    $t0            # Jump to what it thinks is traced function

With the new convention, the sample would save parent address in the
t0 slot, restore it to t0, and then jump to the parent instead of the
traced function, skipping the traced function entirely.

This was fixed later in commit 055e392f3be7 "LoongArch: ftrace: Adjust
register stack restore order in direct call trampolines" which updated
all the samples to match the new convention. However, any out-of-tree
direct trampolines written before this commit would be broken by this
change.

Was this ABI break intentional? The commit message mentions matching
"the state when ftrace was entered" but doesn't explain why breaking
compatibility was necessary. RISC-V uses the opposite convention
(t0 = traced func, ra = parent), which the original LoongArch code
matched.

> +	PTR_ADDI	sp, sp, PT_SIZE
>  	jr		t1
>  SYM_CODE_END(ftrace_common)

[ ... ]

> @@ -161,6 +165,8 @@ SYM_CODE_END(return_to_handler)
>  #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
>  SYM_CODE_START(ftrace_stub_direct_tramp)
>  	UNWIND_HINT_UNDEFINED
> -	jr		t0
> +	move		t1, ra
> +	move		ra, t0
> +	jr		t1
>  SYM_CODE_END(ftrace_stub_direct_tramp)
>  #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/20293821855

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v4 1/7] LoongArch: ftrace: Refactor register restoration in ftrace_common_return
  2025-12-17  6:55   ` bot+bpf-ci
@ 2025-12-18  1:26     ` Chenghao Duan
  2025-12-18 15:26       ` Chris Mason
  0 siblings, 1 reply; 15+ messages in thread
From: Chenghao Duan @ 2025-12-18  1:26 UTC (permalink / raw)
  To: bot+bpf-ci
  Cc: yangtiezhu, rostedt, mhiramat, mark.rutland, hengqi.chen,
	chenhuacai, kernel, zhangtianyang, masahiroy, linux-kernel,
	loongarch, bpf, youling.tang, jianghaoran, vincent.mc.li,
	linux-trace-kernel, ast, andrii, daniel, martin.lau, eddyz87,
	yonghong.song, clm, ihor.solodrai

On Wed, Dec 17, 2025 at 06:55:00AM +0000, bot+bpf-ci@kernel.org wrote:
> > diff --git a/arch/loongarch/kernel/mcount_dyn.S b/arch/loongarch/kernel/mcount_dyn.S
> > index d6b474ad1d5e..5729c20e5b8b 100644
> > --- a/arch/loongarch/kernel/mcount_dyn.S
> > +++ b/arch/loongarch/kernel/mcount_dyn.S
> > @@ -94,7 +94,6 @@ SYM_INNER_LABEL(ftrace_graph_call, SYM_L_GLOBAL)
> >   * at the callsite, so there is no need to restore the T series regs.
> >   */
> >  ftrace_common_return:
> > -	PTR_L		ra, sp, PT_R1
> >  	PTR_L		a0, sp, PT_R4
> >  	PTR_L		a1, sp, PT_R5
> >  	PTR_L		a2, sp, PT_R6
> > @@ -104,12 +103,17 @@ ftrace_common_return:
> >  	PTR_L		a6, sp, PT_R10
> >  	PTR_L		a7, sp, PT_R11
> >  	PTR_L		fp, sp, PT_R22
> > -	PTR_L		t0, sp, PT_ERA
> >  	PTR_L		t1, sp, PT_R13
> > -	PTR_ADDI	sp, sp, PT_SIZE
> >  	bnez		t1, .Ldirect
> > +
> > +	PTR_L		ra, sp, PT_R1
> > +	PTR_L		t0, sp, PT_ERA
> > +	PTR_ADDI	sp, sp, PT_SIZE
> >  	jr		t0
> >  .Ldirect:
> > +	PTR_L		t0, sp, PT_R1
> > +	PTR_L		ra, sp, PT_ERA
>                      ^^^^^^^^^^^^^^^^^^^
> Does this break existing ftrace direct trampolines?
> 
> Before this change, direct trampolines received:
>   ra = parent return address (from PT_R1)
>   t0 = traced function address (from PT_ERA)
> 
> After this change, direct trampolines receive:
>   t0 = parent return address (from PT_R1)
>   ra = traced function address (from PT_ERA)
> 
> The sample trampolines in samples/ftrace/ were written for the old
> convention. For example, samples/ftrace/ftrace-direct.c does:
> 
>   my_tramp:
>     st.d  $t0, $sp, 8    # Save what it thinks is traced func addr
>     st.d  $ra, $sp, 16   # Save what it thinks is parent addr
>     bl    my_direct_func
>     ld.d  $t0, $sp, 8    # Restore
>     ld.d  $ra, $sp, 16   # Restore
>     jr    $t0            # Jump to what it thinks is traced function
> 
> With the new convention, the sample would save parent address in the
> t0 slot, restore it to t0, and then jump to the parent instead of the
> traced function, skipping the traced function entirely.
> 
> This was fixed later in commit 055e392f3be7 "LoongArch: ftrace: Adjust
> register stack restore order in direct call trampolines" which updated
> all the samples to match the new convention. However, any out-of-tree
> direct trampolines written before this commit would be broken by this
> change.
> 
> Was this ABI break intentional? The commit message mentions matching
> "the state when ftrace was entered" but doesn't explain why breaking
> compatibility was necessary. RISC-V uses the opposite convention
> (t0 = traced func, ra = parent), which the original LoongArch code
> matched.

The patch "LoongArch: ftrace: Refactor register restoration in
ftrace_common_return" changed the conventions for t0 and ra after
entering the direct call function, which is exactly the reason forthe
changes made in this patch.

Chenghao

> 
> > +	PTR_ADDI	sp, sp, PT_SIZE
> >  	jr		t1
> >  SYM_CODE_END(ftrace_common)
> 
> [ ... ]
> 
> > @@ -161,6 +165,8 @@ SYM_CODE_END(return_to_handler)
> >  #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
> >  SYM_CODE_START(ftrace_stub_direct_tramp)
> >  	UNWIND_HINT_UNDEFINED
> > -	jr		t0
> > +	move		t1, ra
> > +	move		ra, t0
> > +	jr		t1
> >  SYM_CODE_END(ftrace_stub_direct_tramp)
> >  #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
> 
> 
> ---
> AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
> See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
> 
> CI run summary: https://github.com/kernel-patches/bpf/actions/runs/20293821855


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v4 1/7] LoongArch: ftrace: Refactor register restoration in ftrace_common_return
  2025-12-18  1:26     ` Chenghao Duan
@ 2025-12-18 15:26       ` Chris Mason
  0 siblings, 0 replies; 15+ messages in thread
From: Chris Mason @ 2025-12-18 15:26 UTC (permalink / raw)
  To: Chenghao Duan, bot+bpf-ci
  Cc: yangtiezhu, rostedt, mhiramat, mark.rutland, hengqi.chen,
	chenhuacai, kernel, zhangtianyang, masahiroy, linux-kernel,
	loongarch, bpf, youling.tang, jianghaoran, vincent.mc.li,
	linux-trace-kernel, ast, andrii, daniel, martin.lau, eddyz87,
	yonghong.song, ihor.solodrai

On 12/17/25 8:26 PM, Chenghao Duan wrote:
> On Wed, Dec 17, 2025 at 06:55:00AM +0000, bot+bpf-ci@kernel.org wrote:
>>> diff --git a/arch/loongarch/kernel/mcount_dyn.S b/arch/loongarch/kernel/mcount_dyn.S
>>> index d6b474ad1d5e..5729c20e5b8b 100644
>>> --- a/arch/loongarch/kernel/mcount_dyn.S
>>> +++ b/arch/loongarch/kernel/mcount_dyn.S
>>> @@ -94,7 +94,6 @@ SYM_INNER_LABEL(ftrace_graph_call, SYM_L_GLOBAL)
>>>   * at the callsite, so there is no need to restore the T series regs.
>>>   */
>>>  ftrace_common_return:
>>> -	PTR_L		ra, sp, PT_R1
>>>  	PTR_L		a0, sp, PT_R4
>>>  	PTR_L		a1, sp, PT_R5
>>>  	PTR_L		a2, sp, PT_R6
>>> @@ -104,12 +103,17 @@ ftrace_common_return:
>>>  	PTR_L		a6, sp, PT_R10
>>>  	PTR_L		a7, sp, PT_R11
>>>  	PTR_L		fp, sp, PT_R22
>>> -	PTR_L		t0, sp, PT_ERA
>>>  	PTR_L		t1, sp, PT_R13
>>> -	PTR_ADDI	sp, sp, PT_SIZE
>>>  	bnez		t1, .Ldirect
>>> +
>>> +	PTR_L		ra, sp, PT_R1
>>> +	PTR_L		t0, sp, PT_ERA
>>> +	PTR_ADDI	sp, sp, PT_SIZE
>>>  	jr		t0
>>>  .Ldirect:
>>> +	PTR_L		t0, sp, PT_R1
>>> +	PTR_L		ra, sp, PT_ERA
>>                      ^^^^^^^^^^^^^^^^^^^
>> Does this break existing ftrace direct trampolines?
>>
>> Before this change, direct trampolines received:
>>   ra = parent return address (from PT_R1)
>>   t0 = traced function address (from PT_ERA)
>>
>> After this change, direct trampolines receive:
>>   t0 = parent return address (from PT_R1)
>>   ra = traced function address (from PT_ERA)
>>
>> The sample trampolines in samples/ftrace/ were written for the old
>> convention. For example, samples/ftrace/ftrace-direct.c does:
>>
>>   my_tramp:
>>     st.d  $t0, $sp, 8    # Save what it thinks is traced func addr
>>     st.d  $ra, $sp, 16   # Save what it thinks is parent addr
>>     bl    my_direct_func
>>     ld.d  $t0, $sp, 8    # Restore
>>     ld.d  $ra, $sp, 16   # Restore
>>     jr    $t0            # Jump to what it thinks is traced function
>>
>> With the new convention, the sample would save parent address in the
>> t0 slot, restore it to t0, and then jump to the parent instead of the
>> traced function, skipping the traced function entirely.
>>
>> This was fixed later in commit 055e392f3be7 "LoongArch: ftrace: Adjust
>> register stack restore order in direct call trampolines" which updated
>> all the samples to match the new convention. However, any out-of-tree
>> direct trampolines written before this commit would be broken by this
>> change.
>>
>> Was this ABI break intentional? The commit message mentions matching
>> "the state when ftrace was entered" but doesn't explain why breaking
>> compatibility was necessary. RISC-V uses the opposite convention
>> (t0 = traced func, ra = parent), which the original LoongArch code
>> matched.
> 
> The patch "LoongArch: ftrace: Refactor register restoration in
> ftrace_common_return" changed the conventions for t0 and ra after
> entering the direct call function, which is exactly the reason forthe
> changes made in this patch.

I'll try to adjust the kinds of ABI breakage AI comments on.  It did
catch the other related changes from this series, but the additional
commentary wasn't useful.

Thanks,
Chris


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v4 6/7] LoongArch: BPF: Enhance the bpf_arch_text_poke() function
  2025-12-17  6:14 ` [PATCH v4 6/7] LoongArch: BPF: Enhance the bpf_arch_text_poke() function Chenghao Duan
@ 2025-12-20 14:07   ` Hengqi Chen
  2025-12-22  1:50     ` Chenghao Duan
  0 siblings, 1 reply; 15+ messages in thread
From: Hengqi Chen @ 2025-12-20 14:07 UTC (permalink / raw)
  To: Chenghao Duan
  Cc: yangtiezhu, rostedt, mhiramat, mark.rutland, chenhuacai, kernel,
	zhangtianyang, masahiroy, linux-kernel, loongarch, bpf,
	youling.tang, jianghaoran, vincent.mc.li, linux-trace-kernel

On Wed, Dec 17, 2025 at 2:15 PM Chenghao Duan <duanchenghao@kylinos.cn> wrote:
>
> Enhance the bpf_arch_text_poke() function to enable accurate location
> of BPF program entry points.
>
> When modifying the entry point of a BPF program, skip the move t0, ra
> instruction to ensure the correct logic and copy of the jump address.
>
> Signed-off-by: Chenghao Duan <duanchenghao@kylinos.cn>
> ---
>  arch/loongarch/net/bpf_jit.c | 15 ++++++++++++++-
>  1 file changed, 14 insertions(+), 1 deletion(-)
>
> diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
> index 3dbabacc8856..0c16a1b18e8f 100644
> --- a/arch/loongarch/net/bpf_jit.c
> +++ b/arch/loongarch/net/bpf_jit.c
> @@ -1290,6 +1290,10 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
>                        void *new_addr)

The signature of bpf_arch_text_poke() was changed in v6.19 ([1]), please rebase.

  [1]: https://github.com/torvalds/linux/commit/ae4a3160d19cd16b874737ebc1798c7bc2fe3c9e

>  {
>         int ret;
> +       unsigned long size = 0;
> +       unsigned long offset = 0;
> +       char namebuf[KSYM_NAME_LEN];
> +       void *image = NULL;
>         bool is_call;
>         u32 old_insns[LOONGARCH_LONG_JUMP_NINSNS] = {[0 ... 4] = INSN_NOP};
>         u32 new_insns[LOONGARCH_LONG_JUMP_NINSNS] = {[0 ... 4] = INSN_NOP};
> @@ -1297,9 +1301,18 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
>         /* Only poking bpf text is supported. Since kernel function entry
>          * is set up by ftrace, we rely on ftrace to poke kernel functions.
>          */
> -       if (!is_bpf_text_address((unsigned long)ip))
> +       if (!__bpf_address_lookup((unsigned long)ip, &size, &offset, namebuf))
>                 return -ENOTSUPP;
>
> +       image = ip - offset;
> +       /* zero offset means we're poking bpf prog entry */
> +       if (offset == 0)
> +               /* skip to the nop instruction in bpf prog entry:
> +                * move t0, ra
> +                * nop
> +                */
> +               ip = image + LOONGARCH_INSN_SIZE;
> +
>         is_call = old_t == BPF_MOD_CALL;
>         ret = emit_jump_or_nops(old_addr, ip, old_insns, is_call);
>         if (ret)
> --
> 2.25.1
>

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v4 6/7] LoongArch: BPF: Enhance the bpf_arch_text_poke() function
  2025-12-20 14:07   ` Hengqi Chen
@ 2025-12-22  1:50     ` Chenghao Duan
  2025-12-23  2:23       ` Hengqi Chen
  0 siblings, 1 reply; 15+ messages in thread
From: Chenghao Duan @ 2025-12-22  1:50 UTC (permalink / raw)
  To: Hengqi Chen
  Cc: yangtiezhu, rostedt, mhiramat, mark.rutland, chenhuacai, kernel,
	zhangtianyang, masahiroy, linux-kernel, loongarch, bpf,
	youling.tang, jianghaoran, vincent.mc.li, linux-trace-kernel

On Sat, Dec 20, 2025 at 10:07:25PM +0800, Hengqi Chen wrote:
> On Wed, Dec 17, 2025 at 2:15 PM Chenghao Duan <duanchenghao@kylinos.cn> wrote:
> >
> > Enhance the bpf_arch_text_poke() function to enable accurate location
> > of BPF program entry points.
> >
> > When modifying the entry point of a BPF program, skip the move t0, ra
> > instruction to ensure the correct logic and copy of the jump address.
> >
> > Signed-off-by: Chenghao Duan <duanchenghao@kylinos.cn>
> > ---
> >  arch/loongarch/net/bpf_jit.c | 15 ++++++++++++++-
> >  1 file changed, 14 insertions(+), 1 deletion(-)
> >
> > diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
> > index 3dbabacc8856..0c16a1b18e8f 100644
> > --- a/arch/loongarch/net/bpf_jit.c
> > +++ b/arch/loongarch/net/bpf_jit.c
> > @@ -1290,6 +1290,10 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
> >                        void *new_addr)
> 
> The signature of bpf_arch_text_poke() was changed in v6.19 ([1]), please rebase.
> 
>   [1]: https://github.com/torvalds/linux/commit/ae4a3160d19cd16b874737ebc1798c7bc2fe3c9e

Thank you for your review and for pointing out the API change in v6.19.

I believe my patch series already accounts for this. It was developed on
top of commit ae4a3160d19c ("bpf: specify the old and new poke_type for bpf_arch_text_poke"),
so all modifications to bpf_arch_text_poke() call sites within my
patches should already be using the updated signature.

Please let me know if you find any inconsistencies or if further
adjustments are needed.

Best regards,
Chenghao

> 
> >  {
> >         int ret;
> > +       unsigned long size = 0;
> > +       unsigned long offset = 0;
> > +       char namebuf[KSYM_NAME_LEN];
> > +       void *image = NULL;
> >         bool is_call;
> >         u32 old_insns[LOONGARCH_LONG_JUMP_NINSNS] = {[0 ... 4] = INSN_NOP};
> >         u32 new_insns[LOONGARCH_LONG_JUMP_NINSNS] = {[0 ... 4] = INSN_NOP};
> > @@ -1297,9 +1301,18 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
> >         /* Only poking bpf text is supported. Since kernel function entry
> >          * is set up by ftrace, we rely on ftrace to poke kernel functions.
> >          */
> > -       if (!is_bpf_text_address((unsigned long)ip))
> > +       if (!__bpf_address_lookup((unsigned long)ip, &size, &offset, namebuf))
> >                 return -ENOTSUPP;
> >
> > +       image = ip - offset;
> > +       /* zero offset means we're poking bpf prog entry */
> > +       if (offset == 0)
> > +               /* skip to the nop instruction in bpf prog entry:
> > +                * move t0, ra
> > +                * nop
> > +                */
> > +               ip = image + LOONGARCH_INSN_SIZE;
> > +
> >         is_call = old_t == BPF_MOD_CALL;
> >         ret = emit_jump_or_nops(old_addr, ip, old_insns, is_call);
> >         if (ret)
> > --
> > 2.25.1
> >

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v4 6/7] LoongArch: BPF: Enhance the bpf_arch_text_poke() function
  2025-12-22  1:50     ` Chenghao Duan
@ 2025-12-23  2:23       ` Hengqi Chen
  2025-12-23  8:49         ` Huacai Chen
  0 siblings, 1 reply; 15+ messages in thread
From: Hengqi Chen @ 2025-12-23  2:23 UTC (permalink / raw)
  To: Chenghao Duan
  Cc: yangtiezhu, rostedt, mhiramat, mark.rutland, chenhuacai, kernel,
	zhangtianyang, masahiroy, linux-kernel, loongarch, bpf,
	youling.tang, jianghaoran, vincent.mc.li, linux-trace-kernel

On Mon, Dec 22, 2025 at 9:50 AM Chenghao Duan <duanchenghao@kylinos.cn> wrote:
>
> On Sat, Dec 20, 2025 at 10:07:25PM +0800, Hengqi Chen wrote:
> > On Wed, Dec 17, 2025 at 2:15 PM Chenghao Duan <duanchenghao@kylinos.cn> wrote:
> > >
> > > Enhance the bpf_arch_text_poke() function to enable accurate location
> > > of BPF program entry points.
> > >
> > > When modifying the entry point of a BPF program, skip the move t0, ra
> > > instruction to ensure the correct logic and copy of the jump address.
> > >
> > > Signed-off-by: Chenghao Duan <duanchenghao@kylinos.cn>
> > > ---
> > >  arch/loongarch/net/bpf_jit.c | 15 ++++++++++++++-
> > >  1 file changed, 14 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
> > > index 3dbabacc8856..0c16a1b18e8f 100644
> > > --- a/arch/loongarch/net/bpf_jit.c
> > > +++ b/arch/loongarch/net/bpf_jit.c
> > > @@ -1290,6 +1290,10 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
> > >                        void *new_addr)
> >
> > The signature of bpf_arch_text_poke() was changed in v6.19 ([1]), please rebase.
> >
> >   [1]: https://github.com/torvalds/linux/commit/ae4a3160d19cd16b874737ebc1798c7bc2fe3c9e
>
> Thank you for your review and for pointing out the API change in v6.19.
>
> I believe my patch series already accounts for this. It was developed on
> top of commit ae4a3160d19c ("bpf: specify the old and new poke_type for bpf_arch_text_poke"),
> so all modifications to bpf_arch_text_poke() call sites within my
> patches should already be using the updated signature.

Fine, it seems like the LoongArch tree is not up-to-date.

>
> Please let me know if you find any inconsistencies or if further
> adjustments are needed.
>
> Best regards,
> Chenghao
>
> >
> > >  {
> > >         int ret;
> > > +       unsigned long size = 0;
> > > +       unsigned long offset = 0;
> > > +       char namebuf[KSYM_NAME_LEN];
> > > +       void *image = NULL;
> > >         bool is_call;
> > >         u32 old_insns[LOONGARCH_LONG_JUMP_NINSNS] = {[0 ... 4] = INSN_NOP};
> > >         u32 new_insns[LOONGARCH_LONG_JUMP_NINSNS] = {[0 ... 4] = INSN_NOP};
> > > @@ -1297,9 +1301,18 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
> > >         /* Only poking bpf text is supported. Since kernel function entry
> > >          * is set up by ftrace, we rely on ftrace to poke kernel functions.
> > >          */
> > > -       if (!is_bpf_text_address((unsigned long)ip))
> > > +       if (!__bpf_address_lookup((unsigned long)ip, &size, &offset, namebuf))
> > >                 return -ENOTSUPP;
> > >
> > > +       image = ip - offset;
> > > +       /* zero offset means we're poking bpf prog entry */
> > > +       if (offset == 0)
> > > +               /* skip to the nop instruction in bpf prog entry:
> > > +                * move t0, ra
> > > +                * nop
> > > +                */
> > > +               ip = image + LOONGARCH_INSN_SIZE;
> > > +
> > >         is_call = old_t == BPF_MOD_CALL;
> > >         ret = emit_jump_or_nops(old_addr, ip, old_insns, is_call);
> > >         if (ret)
> > > --
> > > 2.25.1
> > >

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v4 6/7] LoongArch: BPF: Enhance the bpf_arch_text_poke() function
  2025-12-23  2:23       ` Hengqi Chen
@ 2025-12-23  8:49         ` Huacai Chen
  0 siblings, 0 replies; 15+ messages in thread
From: Huacai Chen @ 2025-12-23  8:49 UTC (permalink / raw)
  To: Hengqi Chen
  Cc: Chenghao Duan, yangtiezhu, rostedt, mhiramat, mark.rutland,
	kernel, zhangtianyang, masahiroy, linux-kernel, loongarch, bpf,
	youling.tang, jianghaoran, vincent.mc.li, linux-trace-kernel

On Tue, Dec 23, 2025 at 10:23 AM Hengqi Chen <hengqi.chen@gmail.com> wrote:
>
> On Mon, Dec 22, 2025 at 9:50 AM Chenghao Duan <duanchenghao@kylinos.cn> wrote:
> >
> > On Sat, Dec 20, 2025 at 10:07:25PM +0800, Hengqi Chen wrote:
> > > On Wed, Dec 17, 2025 at 2:15 PM Chenghao Duan <duanchenghao@kylinos.cn> wrote:
> > > >
> > > > Enhance the bpf_arch_text_poke() function to enable accurate location
> > > > of BPF program entry points.
> > > >
> > > > When modifying the entry point of a BPF program, skip the move t0, ra
> > > > instruction to ensure the correct logic and copy of the jump address.
> > > >
> > > > Signed-off-by: Chenghao Duan <duanchenghao@kylinos.cn>
> > > > ---
> > > >  arch/loongarch/net/bpf_jit.c | 15 ++++++++++++++-
> > > >  1 file changed, 14 insertions(+), 1 deletion(-)
> > > >
> > > > diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
> > > > index 3dbabacc8856..0c16a1b18e8f 100644
> > > > --- a/arch/loongarch/net/bpf_jit.c
> > > > +++ b/arch/loongarch/net/bpf_jit.c
> > > > @@ -1290,6 +1290,10 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
> > > >                        void *new_addr)
> > >
> > > The signature of bpf_arch_text_poke() was changed in v6.19 ([1]), please rebase.
> > >
> > >   [1]: https://github.com/torvalds/linux/commit/ae4a3160d19cd16b874737ebc1798c7bc2fe3c9e
> >
> > Thank you for your review and for pointing out the API change in v6.19.
> >
> > I believe my patch series already accounts for this. It was developed on
> > top of commit ae4a3160d19c ("bpf: specify the old and new poke_type for bpf_arch_text_poke"),
> > so all modifications to bpf_arch_text_poke() call sites within my
> > patches should already be using the updated signature.
>
> Fine, it seems like the LoongArch tree is not up-to-date.
LoongArch tree now updated to 6.19-rc2, hope patches can be applied cleanly.

Huacai

>
> >
> > Please let me know if you find any inconsistencies or if further
> > adjustments are needed.
> >
> > Best regards,
> > Chenghao
> >
> > >
> > > >  {
> > > >         int ret;
> > > > +       unsigned long size = 0;
> > > > +       unsigned long offset = 0;
> > > > +       char namebuf[KSYM_NAME_LEN];
> > > > +       void *image = NULL;
> > > >         bool is_call;
> > > >         u32 old_insns[LOONGARCH_LONG_JUMP_NINSNS] = {[0 ... 4] = INSN_NOP};
> > > >         u32 new_insns[LOONGARCH_LONG_JUMP_NINSNS] = {[0 ... 4] = INSN_NOP};
> > > > @@ -1297,9 +1301,18 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
> > > >         /* Only poking bpf text is supported. Since kernel function entry
> > > >          * is set up by ftrace, we rely on ftrace to poke kernel functions.
> > > >          */
> > > > -       if (!is_bpf_text_address((unsigned long)ip))
> > > > +       if (!__bpf_address_lookup((unsigned long)ip, &size, &offset, namebuf))
> > > >                 return -ENOTSUPP;
> > > >
> > > > +       image = ip - offset;
> > > > +       /* zero offset means we're poking bpf prog entry */
> > > > +       if (offset == 0)
> > > > +               /* skip to the nop instruction in bpf prog entry:
> > > > +                * move t0, ra
> > > > +                * nop
> > > > +                */
> > > > +               ip = image + LOONGARCH_INSN_SIZE;
> > > > +
> > > >         is_call = old_t == BPF_MOD_CALL;
> > > >         ret = emit_jump_or_nops(old_addr, ip, old_insns, is_call);
> > > >         if (ret)
> > > > --
> > > > 2.25.1
> > > >
>

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2025-12-23  8:49 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-17  6:14 [PATCH v4 0/7] Fix the failure issue of the module_attach test case Chenghao Duan
2025-12-17  6:14 ` [PATCH v4 1/7] LoongArch: ftrace: Refactor register restoration in ftrace_common_return Chenghao Duan
2025-12-17  6:55   ` bot+bpf-ci
2025-12-18  1:26     ` Chenghao Duan
2025-12-18 15:26       ` Chris Mason
2025-12-17  6:14 ` [PATCH v4 2/7] LoongArch: Enable exception fixup for specific ADE subcode Chenghao Duan
2025-12-17  6:14 ` [PATCH v4 3/7] LoongArch: BPF: Enable and fix trampoline-based tracing for module functions Chenghao Duan
2025-12-17  6:14 ` [PATCH v4 4/7] LoongArch: BPF: Save return address register ra to t0 before trampoline Chenghao Duan
2025-12-17  6:14 ` [PATCH v4 5/7] LoongArch: BPF: Adjust the jump offset of tail calls Chenghao Duan
2025-12-17  6:14 ` [PATCH v4 6/7] LoongArch: BPF: Enhance the bpf_arch_text_poke() function Chenghao Duan
2025-12-20 14:07   ` Hengqi Chen
2025-12-22  1:50     ` Chenghao Duan
2025-12-23  2:23       ` Hengqi Chen
2025-12-23  8:49         ` Huacai Chen
2025-12-17  6:14 ` [PATCH v4 7/7] LoongArch: ftrace: Adjust register stack restore order in direct call trampolines Chenghao Duan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).