From nobody Thu Dec 18 23:30:08 2025 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (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 6907E2DA77F; Wed, 17 Dec 2025 06:14:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765952098; cv=none; b=dxyhOZZqrpWMpZP0mdaVTXT3ld6bLKb0kgA+F2RJEBqeVQGaR4TOdRIl9IgdXdTH2zJbKXPLpgATYhI7ygZGbAA8w/TEzAotEnwzOLxXMJZoyIqJSNgW7pKs6JIzpPdP0oZSBeyxlguuoIGmHAoDt5gFobXaufCuRoXT++nkIiM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765952098; c=relaxed/simple; bh=Zfloy8I/PvvV0GgsACcj4Xioz0vOdmYRRY1dbRe0Cjk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=f1i2yZHvfwIsiZdUApNxrJS2dPIW+QkkfuYq0OtT189aTtmurqYvSgR8yWHnJWjXDRA8TKwftQNWIagAfy7CwJWkEcIjCCv7CliEyqkGpEM8Xuph+u3VJzhQ7ei+bvk2KRC8N2ymCCFwgYsttrLXV+8Ibn0hN91Q3LFGcZFRbUY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: b03d542cdb0f11f0a38c85956e01ac42-20251217 X-CTIC-Tags: HR_CC_COUNT, HR_CC_DOMAIN_COUNT, HR_CC_NO_NAME, HR_CTE_8B, HR_CTT_MISS HR_DATE_H, HR_DATE_WKD, HR_DATE_ZONE, HR_FROM_NAME, HR_SJ_DIGIT_LEN HR_SJ_LANG, HR_SJ_LEN, HR_SJ_LETTER, HR_SJ_NOR_SYM, HR_SJ_PHRASE HR_SJ_PHRASE_LEN, HR_SJ_WS, HR_TO_COUNT, HR_TO_DOMAIN_COUNT, HR_TO_NO_NAME IP_UNTRUSTED, SRC_UNTRUSTED, IP_UNFAMILIAR, SRC_UNFAMILIAR, DN_TRUSTED SRC_TRUSTED, SA_EXISTED, SN_EXISTED, SPF_NOPASS, DKIM_NOPASS DMARC_NOPASS, CIE_BAD, CIE_GOOD_SPF, CIE_UNKNOWN, GTI_FG_BS GTI_RG_INFO, GTI_C_BU, AMN_GOOD, ABX_MISS_RDNS X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:4119027e-0bad-4eb7-bfb1-90a7e9005e4a,IP:10,U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:-5,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:5 X-CID-INFO: VERSION:1.3.6,REQID:4119027e-0bad-4eb7-bfb1-90a7e9005e4a,IP:10,URL :0,TC:0,Content:0,EDM:0,RT:0,SF:-5,FILE:0,BULK:0,RULE:Release_Ham,ACTION:r elease,TS:5 X-CID-META: VersionHash:a9d874c,CLOUDID:df328e767154f61dcf2d98f75131e4e8,BulkI D:251212171111BX2VBL8B,BulkQuantity:4,Recheck:0,SF:17|19|38|66|78|81|82|10 2|127|850|898,TC:nil,Content:0|15|50,EDM:-3,IP:-2,URL:0,File:nil,RT:nil,Bu lk:41,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0 ,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR,TF_CID_SPAM_FAS,TF_CID_SPAM_FSD X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: b03d542cdb0f11f0a38c85956e01ac42-20251217 X-User: duanchenghao@kylinos.cn Received: from localhost.localdomain [(183.242.174.20)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA) with ESMTP id 71023797; Wed, 17 Dec 2025 14:14:47 +0800 From: Chenghao Duan To: yangtiezhu@loongson.cn, rostedt@goodmis.org, mhiramat@kernel.org, mark.rutland@arm.com, hengqi.chen@gmail.com, chenhuacai@kernel.org Cc: kernel@xen0n.name, zhangtianyang@loongson.cn, masahiroy@kernel.org, linux-kernel@vger.kernel.org, loongarch@lists.linux.dev, bpf@vger.kernel.org, duanchenghao@kylinos.cn, youling.tang@linux.dev, jianghaoran@kylinos.cn, vincent.mc.li@gmail.com, linux-trace-kernel@vger.kernel.org Subject: [PATCH v4 1/7] LoongArch: ftrace: Refactor register restoration in ftrace_common_return Date: Wed, 17 Dec 2025 14:14:29 +0800 Message-Id: <20251217061435.802204-2-duanchenghao@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20251217061435.802204-1-duanchenghao@kylinos.cn> References: <20251217061435.802204-1-duanchenghao@kylinos.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" 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 =3D parent return address; t0 =3D 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 =3D traced function return address; t0 =3D parent return address. Fixes: 9cdc3b6a299c ("LoongArch: ftrace: Add direct call support") Signed-off-by: Chenghao Duan --- 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/mco= unt_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) =20 @@ -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 */ --=20 2.25.1 From nobody Thu Dec 18 23:30:08 2025 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (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 65A172DAFB0; Wed, 17 Dec 2025 06:14:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765952097; cv=none; b=b5CDrRIs5tFghcKOID6Vn8hChfE7E+jJjHi1MAiy8IqmbgJUr0x+ZiTu+kcSB//CY+/TaoYEpBEFGffbgRL4ItX9HDZxkw6kxfn3yLWkPHuiiHZ2MUx2zuyn7/voaSP8YQ4Ft9Ucc4h7kmH13TVRbnYsYJkDmcbnbhmlSFHQwmo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765952097; c=relaxed/simple; bh=r1I+bpErqlh++r/MFQe8qcmP4ACN25MMSukJfXZll64=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=iESn7vbiWpGrrxRJbXw2EMgm8ECmg56dF6ZSpmhzOevEic4MjPbMnepWYgwOGgaINANCcdRHdKJa4VS/66ho88bAhiF1UmBQlWdknp5pZqOHYehLx2kZGg75IEuFALT8lsp9K/oPZLXHu8QTx1N3tfYNdGoKzzXfswCGXJFkgx0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: b0a89976db0f11f0a38c85956e01ac42-20251217 X-CTIC-Tags: HR_CC_COUNT, HR_CC_DOMAIN_COUNT, HR_CC_NO_NAME, HR_CTE_8B, HR_CTT_TXT HR_DATE_H, HR_DATE_WKD, HR_DATE_ZONE, HR_FROM_NAME, HR_SJ_DIGIT_LEN HR_SJ_LANG, HR_SJ_LEN, HR_SJ_LETTER, HR_SJ_NOR_SYM, HR_SJ_PHRASE HR_SJ_PHRASE_LEN, HR_SJ_WS, HR_TO_COUNT, HR_TO_DOMAIN_COUNT, HR_TO_NO_NAME IP_UNTRUSTED, SRC_UNTRUSTED, IP_UNFAMILIAR, SRC_UNFAMILIAR, DN_TRUSTED SRC_TRUSTED, SA_EXISTED, SN_EXISTED, SPF_NOPASS, DKIM_NOPASS DMARC_NOPASS, CIE_BAD, CIE_GOOD_SPF, CIE_UNKNOWN, GTI_FG_BS GTI_RG_INFO, GTI_C_BU, AMN_GOOD, ABX_MISS_RDNS X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:8e401cd1-354d-4394-a7c3-c1672cd8df66,IP:10,U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:-5,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:5 X-CID-INFO: VERSION:1.3.6,REQID:8e401cd1-354d-4394-a7c3-c1672cd8df66,IP:10,URL :0,TC:0,Content:0,EDM:0,RT:0,SF:-5,FILE:0,BULK:0,RULE:Release_Ham,ACTION:r elease,TS:5 X-CID-META: VersionHash:a9d874c,CLOUDID:1524952e3b7d2bf701af38f7ac9cb77e,BulkI D:251217141448XN7Y5VUG,BulkQuantity:0,Recheck:0,SF:17|19|38|66|78|81|82|10 2|127|850|898,TC:nil,Content:0|15|50,EDM:-3,IP:-2,URL:0,File:nil,RT:nil,Bu lk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR: 0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_FSD,TF_CID_SPAM_SNR,TF_CID_SPAM_FAS X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: b0a89976db0f11f0a38c85956e01ac42-20251217 X-User: duanchenghao@kylinos.cn Received: from localhost.localdomain [(183.242.174.20)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA) with ESMTP id 1826887528; Wed, 17 Dec 2025 14:14:48 +0800 From: Chenghao Duan To: yangtiezhu@loongson.cn, rostedt@goodmis.org, mhiramat@kernel.org, mark.rutland@arm.com, hengqi.chen@gmail.com, chenhuacai@kernel.org Cc: kernel@xen0n.name, zhangtianyang@loongson.cn, masahiroy@kernel.org, linux-kernel@vger.kernel.org, loongarch@lists.linux.dev, bpf@vger.kernel.org, duanchenghao@kylinos.cn, youling.tang@linux.dev, jianghaoran@kylinos.cn, vincent.mc.li@gmail.com, linux-trace-kernel@vger.kernel.org Subject: [PATCH v4 2/7] LoongArch: Enable exception fixup for specific ADE subcode Date: Wed, 17 Dec 2025 14:14:30 +0800 Message-Id: <20251217061435.802204-3-duanchenghao@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20251217061435.802204-1-duanchenghao@kylinos.cn> References: <20251217061435.802204-1-duanchenghao@kylinos.cn> 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 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=E2=80=99s 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 --- 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) =20 asmlinkage void noinstr do_ade(struct pt_regs *regs) { - irqentry_state_t state =3D irqentry_enter(regs); + irqentry_state_t state; + unsigned int esubcode =3D FIELD_GET(CSR_ESTAT_ESUBCODE, regs->csr_estat); + + state =3D irqentry_enter(regs); + + if ((esubcode =3D=3D EXSUBCODE_ADEM) && fixup_exception(regs)) + goto out; =20 die_if_kernel("Kernel ade access", regs); force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)regs->csr_badvaddr); =20 +out: irqentry_exit(regs, state); } =20 --=20 2.25.1 From nobody Thu Dec 18 23:30:08 2025 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (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 E207B2DC33F; Wed, 17 Dec 2025 06:14:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765952100; cv=none; b=dn0ba/MzEvv2X+jsuorp0qIX8/3kwKCY9QAoRbtW7BWe2KjYF8nDYGF10eD7bBb7NnGjxwDPm9Ui5RCOGg6apaXCmWEJBC2uRap6gvci6kBQHopEcGNcjI8GHVYc/rR40lD2KnXOeDknbHf1MRS7ewydfONPuvvuBWda1JyqISs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765952100; c=relaxed/simple; bh=YQruH6xDYid2KbeZCuW/EBXAgV9ICz90owU6l1cryUA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=M1fglqy0rRyw+W/X3qJ28JeC2EF+PcYYkVSZ2mfH/3bQ8ZrPPNvx5C8TvvzztOmDvx2tVZUBdYujArE60U/4nwoKEjZCpx95uNeqXoT4lspCqYzKFDIUsIivONHBffLy7CuMJlSrB9aq3XTrUbBG38twXAG5iZzdiceNyhUZJEk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: b135e326db0f11f0a38c85956e01ac42-20251217 X-CTIC-Tags: HR_CC_COUNT, HR_CC_DOMAIN_COUNT, HR_CC_NO_NAME, HR_CTE_8B, HR_CTT_MISS HR_DATE_H, HR_DATE_WKD, HR_DATE_ZONE, HR_FROM_NAME, HR_SJ_DIGIT_LEN HR_SJ_LANG, HR_SJ_LEN, HR_SJ_LETTER, HR_SJ_NOR_SYM, HR_SJ_PHRASE HR_SJ_PHRASE_LEN, HR_SJ_WS, HR_TO_COUNT, HR_TO_DOMAIN_COUNT, HR_TO_NO_NAME IP_UNTRUSTED, SRC_UNTRUSTED, IP_UNFAMILIAR, SRC_UNFAMILIAR, DN_TRUSTED SRC_TRUSTED, SA_EXISTED, SN_EXISTED, SPF_NOPASS, DKIM_NOPASS DMARC_NOPASS, CIE_BAD, CIE_GOOD_SPF, CIE_UNKNOWN, GTI_FG_BS GTI_RG_INFO, GTI_C_BU, AMN_GOOD, ABX_MISS_RDNS X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:68546b5d-548d-4ec0-961d-bcc2aeb890f4,IP:10,U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:-5,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:5 X-CID-INFO: VERSION:1.3.6,REQID:68546b5d-548d-4ec0-961d-bcc2aeb890f4,IP:10,URL :0,TC:0,Content:0,EDM:0,RT:0,SF:-5,FILE:0,BULK:0,RULE:Release_Ham,ACTION:r elease,TS:5 X-CID-META: VersionHash:a9d874c,CLOUDID:1897838be0a3ac86b89d997081ea7b68,BulkI D:251217141451YB1P9FB7,BulkQuantity:0,Recheck:0,SF:17|19|38|66|78|81|82|10 2|127|850|898,TC:nil,Content:0|15|50,EDM:-3,IP:-2,URL:0,File:nil,RT:nil,Bu lk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR: 0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR,TF_CID_SPAM_FAS,TF_CID_SPAM_FSD X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: b135e326db0f11f0a38c85956e01ac42-20251217 X-User: duanchenghao@kylinos.cn Received: from localhost.localdomain [(183.242.174.20)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA) with ESMTP id 784059731; Wed, 17 Dec 2025 14:14:49 +0800 From: Chenghao Duan To: yangtiezhu@loongson.cn, rostedt@goodmis.org, mhiramat@kernel.org, mark.rutland@arm.com, hengqi.chen@gmail.com, chenhuacai@kernel.org Cc: kernel@xen0n.name, zhangtianyang@loongson.cn, masahiroy@kernel.org, linux-kernel@vger.kernel.org, loongarch@lists.linux.dev, bpf@vger.kernel.org, duanchenghao@kylinos.cn, youling.tang@linux.dev, jianghaoran@kylinos.cn, vincent.mc.li@gmail.com, linux-trace-kernel@vger.kernel.org Subject: [PATCH v4 3/7] LoongArch: BPF: Enable and fix trampoline-based tracing for module functions Date: Wed, 17 Dec 2025 14:14:31 +0800 Message-Id: <20251217061435.802204-4-duanchenghao@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20251217061435.802204-1-duanchenghao@kylinos.cn> References: <20251217061435.802204-1-duanchenghao@kylinos.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" 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 --- 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; } =20 - return emit_jump_and_link(&ctx, is_call ? LOONGARCH_GPR_T0 : LOONGARCH_GP= R_ZERO, (u64)target); + return emit_jump_and_link(&ctx, is_call ? LOONGARCH_GPR_RA : LOONGARCH_GP= R_ZERO, (u64)target); } =20 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 =20 /* 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 +=3D LOONGARCH_FENTRY_NBYTES; /* Direct jump skips 5 NOP instructions */ else if (is_bpf_text_address((unsigned long)orig_call)) orig_call +=3D LOONGARCH_BPF_FENTRY_NBYTES; - /* Module tracing not supported - cause kernel lockups */ - else if (is_module_text_address((unsigned long)orig_call)) - return -ENOTSUPP; =20 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); =20 - 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); + } } =20 ret =3D ctx->idx; --=20 2.25.1 From nobody Thu Dec 18 23:30:08 2025 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (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 617E22DAFAF; Wed, 17 Dec 2025 06:14:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765952098; cv=none; b=Zk4E6OfbnNbUr8wWZ8FLYiXI0PWhYs85oh49FY6JbtFb+lCvMT6kof75sRCQiDZf02tiTvHHciXIw+BMSHda0klRVxoQmSyuAuvOmdis3K7ZuWn9IaCbDgaG5eGwn7GpHnkdVE7Lgis71BjbEPMq7Sh4dtbw34wEO8psvsucZn0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765952098; c=relaxed/simple; bh=wZLOMETjgoJ2dGINqkm2V8I5bmIvypzJ/UXmJA+ST2I=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qqSQEberkUSZPsiqs/Qry/yAoN04gxBqh47WsDI+ibphItSxWDmxs9c7rC9Po/h2hSPqK4hf6mv1GTuGH0yym0p4tsaFOuKwEckCkm/5hhAZ/PJmBnPIfpPav41YkanE9lM23BYUaDpWCzdbqs2EHM68SXtKKKfC6fRcus61oE0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: b17e2dc0db0f11f0a38c85956e01ac42-20251217 X-CTIC-Tags: HR_CC_COUNT, HR_CC_DOMAIN_COUNT, HR_CC_NO_NAME, HR_CTE_8B, HR_CTT_MISS HR_DATE_H, HR_DATE_WKD, HR_DATE_ZONE, HR_FROM_NAME, HR_SJ_DIGIT_LEN HR_SJ_LANG, HR_SJ_LEN, HR_SJ_LETTER, HR_SJ_NOR_SYM, HR_SJ_PHRASE HR_SJ_PHRASE_LEN, HR_SJ_WS, HR_TO_COUNT, HR_TO_DOMAIN_COUNT, HR_TO_NO_NAME IP_UNTRUSTED, SRC_UNTRUSTED, IP_UNFAMILIAR, SRC_UNFAMILIAR, DN_TRUSTED SRC_TRUSTED, SA_EXISTED, SN_EXISTED, SPF_NOPASS, DKIM_NOPASS DMARC_NOPASS, CIE_BAD, CIE_GOOD_SPF, CIE_UNKNOWN, GTI_FG_BS GTI_RG_INFO, GTI_C_BU, AMN_GOOD, ABX_MISS_RDNS X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:54636725-8111-4a94-bf27-e8707e41cefc,IP:10,U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:-5,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:5 X-CID-INFO: VERSION:1.3.6,REQID:54636725-8111-4a94-bf27-e8707e41cefc,IP:10,URL :0,TC:0,Content:0,EDM:0,RT:0,SF:-5,FILE:0,BULK:0,RULE:Release_Ham,ACTION:r elease,TS:5 X-CID-META: VersionHash:a9d874c,CLOUDID:ba6e0229970adc7d7e96f99048600b0c,BulkI D:2512171414511WY777SV,BulkQuantity:0,Recheck:0,SF:17|19|38|66|78|81|82|10 2|127|850|898,TC:nil,Content:0|15|50,EDM:-3,IP:-2,URL:0,File:nil,RT:nil,Bu lk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR: 0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_FAS,TF_CID_SPAM_FSD,TF_CID_SPAM_SNR X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: b17e2dc0db0f11f0a38c85956e01ac42-20251217 X-User: duanchenghao@kylinos.cn Received: from localhost.localdomain [(183.242.174.20)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA) with ESMTP id 1517111480; Wed, 17 Dec 2025 14:14:49 +0800 From: Chenghao Duan To: yangtiezhu@loongson.cn, rostedt@goodmis.org, mhiramat@kernel.org, mark.rutland@arm.com, hengqi.chen@gmail.com, chenhuacai@kernel.org Cc: kernel@xen0n.name, zhangtianyang@loongson.cn, masahiroy@kernel.org, linux-kernel@vger.kernel.org, loongarch@lists.linux.dev, bpf@vger.kernel.org, duanchenghao@kylinos.cn, youling.tang@linux.dev, jianghaoran@kylinos.cn, vincent.mc.li@gmail.com, linux-trace-kernel@vger.kernel.org Subject: [PATCH v4 4/7] LoongArch: BPF: Save return address register ra to t0 before trampoline Date: Wed, 17 Dec 2025 14:14:32 +0800 Message-Id: <20251217061435.802204-5-duanchenghao@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20251217061435.802204-1-duanchenghao@kylinos.cn> References: <20251217061435.802204-1-duanchenghao@kylinos.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" 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 --- 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 =3D round_up(stack_adjust, 16); stack_adjust +=3D bpf_stack_adjust; =20 + move_reg(ctx, LOONGARCH_GPR_T0, LOONGARCH_GPR_RA); /* Reserve space for the move_imm + jirl instruction */ for (i =3D 0; i < LOONGARCH_LONG_JUMP_NINSNS; i++) emit_insn(ctx, nop); --=20 2.25.1 From nobody Thu Dec 18 23:30:08 2025 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (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 5B6EF2DC789; Wed, 17 Dec 2025 06:14:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765952100; cv=none; b=Z1VYzYokz/QgeF904oaVFSuhaZeiJz8RYihXjfoAxxWPR/9GX43r2kSasFiQkydYS/u683+zWk3ZG4uvwhoBGMXnxClrGEiH5uhkgUDuHKRcDcdlhk9tXe6WwOXacdk98hx+u/twWZL2QwOAcMsivhQyXkbpIb5mLreQv+Z3Whk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765952100; c=relaxed/simple; bh=31nWD7AkFVqOf9l6uFwF6wJyF8WR5Mlt+/DdpJzy4xg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=T4qS8fJ+fiMnGNlQuz30x8RDiEhqn8pF1HXAdDZJTUzBUk9lQXC6aWTP+UyfG48Tr4ovbWuXPlsPRR1NNDInYnBroZjwwOuS6W+odOahJPajxNE+WdCZitXMc0xFBOUWXoS1LULKnbF/oXvYOQBtjonnEAi9G2Plvk2iKqUwPbo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: b3eab8dadb0f11f0a38c85956e01ac42-20251217 X-CTIC-Tags: HR_CC_COUNT, HR_CC_DOMAIN_COUNT, HR_CC_NO_NAME, HR_CTE_8B, HR_CTT_MISS HR_DATE_H, HR_DATE_WKD, HR_DATE_ZONE, HR_FROM_NAME, HR_SJ_DIGIT_LEN HR_SJ_LANG, HR_SJ_LEN, HR_SJ_LETTER, HR_SJ_NOR_SYM, HR_SJ_PHRASE HR_SJ_PHRASE_LEN, HR_SJ_WS, HR_TO_COUNT, HR_TO_DOMAIN_COUNT, HR_TO_NO_NAME IP_UNTRUSTED, SRC_UNTRUSTED, IP_UNFAMILIAR, SRC_UNFAMILIAR, DN_TRUSTED SRC_TRUSTED, SA_EXISTED, SN_EXISTED, SPF_NOPASS, DKIM_NOPASS DMARC_NOPASS, CIE_BAD, CIE_GOOD_SPF, CIE_UNKNOWN, GTI_FG_BS GTI_RG_INFO, GTI_C_BU, AMN_GOOD, ABX_MISS_RDNS X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:8dfcca9e-0fe8-4bf7-a3f9-bc3c61f0690d,IP:10,U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:-5,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:5 X-CID-INFO: VERSION:1.3.6,REQID:8dfcca9e-0fe8-4bf7-a3f9-bc3c61f0690d,IP:10,URL :0,TC:0,Content:0,EDM:0,RT:0,SF:-5,FILE:0,BULK:0,RULE:Release_Ham,ACTION:r elease,TS:5 X-CID-META: VersionHash:a9d874c,CLOUDID:ed8cd19e53763638b1cef8c7582441f2,BulkI D:2512171414545J8W4XIJ,BulkQuantity:0,Recheck:0,SF:17|19|38|66|78|81|82|10 2|127|850|898,TC:nil,Content:0|15|50,EDM:-3,IP:-2,URL:0,File:nil,RT:nil,Bu lk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR: 0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR,TF_CID_SPAM_FAS,TF_CID_SPAM_FSD X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: b3eab8dadb0f11f0a38c85956e01ac42-20251217 X-User: duanchenghao@kylinos.cn Received: from localhost.localdomain [(183.242.174.20)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA) with ESMTP id 1856364418; Wed, 17 Dec 2025 14:14:53 +0800 From: Chenghao Duan To: yangtiezhu@loongson.cn, rostedt@goodmis.org, mhiramat@kernel.org, mark.rutland@arm.com, hengqi.chen@gmail.com, chenhuacai@kernel.org Cc: kernel@xen0n.name, zhangtianyang@loongson.cn, masahiroy@kernel.org, linux-kernel@vger.kernel.org, loongarch@lists.linux.dev, bpf@vger.kernel.org, duanchenghao@kylinos.cn, youling.tang@linux.dev, jianghaoran@kylinos.cn, vincent.mc.li@gmail.com, linux-trace-kernel@vger.kernel.org Subject: [PATCH v4 5/7] LoongArch: BPF: Adjust the jump offset of tail calls Date: Wed, 17 Dec 2025 14:14:33 +0800 Message-Id: <20251217061435.802204-6-duanchenghao@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20251217061435.802204-1-duanchenghao@kylinos.cn> References: <20251217061435.802204-1-duanchenghao@kylinos.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" 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 --- 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); } } =20 --=20 2.25.1 From nobody Thu Dec 18 23:30:08 2025 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (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 798ED2EB872; Wed, 17 Dec 2025 06:15:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765952103; cv=none; b=ovlSWCZtCZ5uspIlxzpVYveygMKEHstN6RACrE6RIabmv2Tz3AkvIDkYuU5++tMZyyATXODeyJY03+jc8L0ONvVnxZRQvczFe+F0rlmJHOUbw5h6SE23jp9Jd75rwXOWkP9dm0z68MVwF0McwggXONRAVn13PFY8BL+s3VUoNIM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765952103; c=relaxed/simple; bh=WLDcyUQpfa0qKW1T1Zd8X2+eNMhk9ImYjFtpapGBMWU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=trHSZX/4pbIt6mC0JaA605iOVveY5avEWYg4Fp69RDglcPP81MsENo6vcnVL9F42O07Qvqy/R0DBVcpIANb5/20kapiEf5vnWeK9JK4Pl6ymCGoZYxeLCaqcOWdYUOG3Lb7GSbHXw4KdC85FBU4Nc0gpEWNB82930m8LbA2MWEE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: b45ccc7cdb0f11f0a38c85956e01ac42-20251217 X-CTIC-Tags: HR_CC_COUNT, HR_CC_DOMAIN_COUNT, HR_CC_NO_NAME, HR_CTE_8B, HR_CTT_MISS HR_DATE_H, HR_DATE_WKD, HR_DATE_ZONE, HR_FROM_NAME, HR_SJ_DIGIT_LEN HR_SJ_LANG, HR_SJ_LEN, HR_SJ_LETTER, HR_SJ_NOR_SYM, HR_SJ_PHRASE HR_SJ_PHRASE_LEN, HR_SJ_WS, HR_TO_COUNT, HR_TO_DOMAIN_COUNT, HR_TO_NO_NAME IP_UNTRUSTED, SRC_UNTRUSTED, IP_UNFAMILIAR, SRC_UNFAMILIAR, DN_TRUSTED SRC_TRUSTED, SA_EXISTED, SN_EXISTED, SPF_NOPASS, DKIM_NOPASS DMARC_NOPASS, CIE_BAD, CIE_GOOD_SPF, CIE_UNKNOWN, GTI_FG_BS GTI_RG_INFO, GTI_C_BU, AMN_GOOD, ABX_MISS_RDNS X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:66f060b5-49c3-4c94-be10-6e82ec42ed6f,IP:10,U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:-5,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:5 X-CID-INFO: VERSION:1.3.6,REQID:66f060b5-49c3-4c94-be10-6e82ec42ed6f,IP:10,URL :0,TC:0,Content:0,EDM:0,RT:0,SF:-5,FILE:0,BULK:0,RULE:Release_Ham,ACTION:r elease,TS:5 X-CID-META: VersionHash:a9d874c,CLOUDID:9ed4d91d2c7b25ac4dd7b7b3ed3762e8,BulkI D:251217141457MPWBEX6H,BulkQuantity:0,Recheck:0,SF:17|19|38|66|78|81|82|10 2|127|850|898,TC:nil,Content:0|15|50,EDM:-3,IP:-2,URL:0,File:nil,RT:nil,Bu lk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR: 0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR,TF_CID_SPAM_FAS,TF_CID_SPAM_FSD X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: b45ccc7cdb0f11f0a38c85956e01ac42-20251217 X-User: duanchenghao@kylinos.cn Received: from localhost.localdomain [(183.242.174.20)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA) with ESMTP id 612073974; Wed, 17 Dec 2025 14:14:54 +0800 From: Chenghao Duan To: yangtiezhu@loongson.cn, rostedt@goodmis.org, mhiramat@kernel.org, mark.rutland@arm.com, hengqi.chen@gmail.com, chenhuacai@kernel.org Cc: kernel@xen0n.name, zhangtianyang@loongson.cn, masahiroy@kernel.org, linux-kernel@vger.kernel.org, loongarch@lists.linux.dev, bpf@vger.kernel.org, duanchenghao@kylinos.cn, youling.tang@linux.dev, jianghaoran@kylinos.cn, vincent.mc.li@gmail.com, linux-trace-kernel@vger.kernel.org Subject: [PATCH v4 6/7] LoongArch: BPF: Enhance the bpf_arch_text_poke() function Date: Wed, 17 Dec 2025 14:14:34 +0800 Message-Id: <20251217061435.802204-7-duanchenghao@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20251217061435.802204-1-duanchenghao@kylinos.cn> References: <20251217061435.802204-1-duanchenghao@kylinos.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" 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 --- 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 =3D 0; + unsigned long offset =3D 0; + char namebuf[KSYM_NAME_LEN]; + void *image =3D NULL; bool is_call; u32 old_insns[LOONGARCH_LONG_JUMP_NINSNS] =3D {[0 ... 4] =3D INSN_NOP}; u32 new_insns[LOONGARCH_LONG_JUMP_NINSNS] =3D {[0 ... 4] =3D 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; =20 + image =3D ip - offset; + /* zero offset means we're poking bpf prog entry */ + if (offset =3D=3D 0) + /* skip to the nop instruction in bpf prog entry: + * move t0, ra + * nop + */ + ip =3D image + LOONGARCH_INSN_SIZE; + is_call =3D old_t =3D=3D BPF_MOD_CALL; ret =3D emit_jump_or_nops(old_addr, ip, old_insns, is_call); if (ret) --=20 2.25.1 From nobody Thu Dec 18 23:30:08 2025 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (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 2046F2E8DFE; Wed, 17 Dec 2025 06:14:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765952101; cv=none; b=ELvHEK2DyNhjsZ2j8Qj/AtYpngwRYw7qSY9DvpsBwdEUnL+gr8sqkxc4BXO+fMKpTdsbUg6Gw/IkjJ8VLPhbS5jgIpFtDDTYX2PMNs8wYn6RTnK9/fawx3W/iDU6OnwNLSIYX/M9X+LQpi6kaUGXwzwjAKmq5rvnGJVr6DKGh8g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765952101; c=relaxed/simple; bh=AJK3e4Om/qHZWa//gA3ut0MGVVoNu/Yq0fpGEEG5CYQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bCYkxQTO5jQ9Iun89un3ge46epFyCYq8nFqORLhEy5p2M8qW12Jd5iIxFZe/dZg42FdTSF8G9ETvPyManmqwBqPQUYFUKXsvHHD3ABF3g9+JKxJvJ8nIK3Cl5HbDb4dTZzudvPtDmOCC6/JoAqN5ZlUCUeybO66uYO191221xKs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: b4db798cdb0f11f0a38c85956e01ac42-20251217 X-CTIC-Tags: HR_CC_COUNT, HR_CC_DOMAIN_COUNT, HR_CC_NAME, HR_CC_NO_NAME, HR_CTE_8B HR_CTT_MISS, HR_DATE_H, HR_DATE_WKD, HR_DATE_ZONE, HR_FROM_NAME HR_SJ_DIGIT_LEN, HR_SJ_LANG, HR_SJ_LEN, HR_SJ_LETTER, HR_SJ_NOR_SYM HR_SJ_PHRASE, HR_SJ_PHRASE_LEN, HR_SJ_WS, HR_TO_COUNT, HR_TO_DOMAIN_COUNT HR_TO_NO_NAME, IP_UNTRUSTED, SRC_UNTRUSTED, IP_UNFAMILIAR, SRC_UNFAMILIAR DN_TRUSTED, SRC_TRUSTED, SA_EXISTED, SN_EXISTED, SPF_NOPASS DKIM_NOPASS, DMARC_NOPASS, CIE_BAD, CIE_GOOD_SPF, CIE_UNKNOWN GTI_FG_BS, GTI_RG_INFO, GTI_C_BU, AMN_GOOD, ABX_MISS_RDNS X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:aab3346b-cfb8-477e-ac86-fbe105273118,IP:10,U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:-5,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:5 X-CID-INFO: VERSION:1.3.6,REQID:aab3346b-cfb8-477e-ac86-fbe105273118,IP:10,URL :0,TC:0,Content:0,EDM:0,RT:0,SF:-5,FILE:0,BULK:0,RULE:Release_Ham,ACTION:r elease,TS:5 X-CID-META: VersionHash:a9d874c,CLOUDID:6b38733b2e317b36c9c3630a19afa1a7,BulkI D:251217141457YR10BFQH,BulkQuantity:0,Recheck:0,SF:17|19|38|66|78|81|82|10 2|127|850|898,TC:nil,Content:0|15|50,EDM:-3,IP:-2,URL:0,File:nil,RT:nil,Bu lk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR: 0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR,TF_CID_SPAM_FAS,TF_CID_SPAM_FSD X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: b4db798cdb0f11f0a38c85956e01ac42-20251217 X-User: duanchenghao@kylinos.cn Received: from localhost.localdomain [(183.242.174.20)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA) with ESMTP id 312958035; Wed, 17 Dec 2025 14:14:55 +0800 From: Chenghao Duan To: yangtiezhu@loongson.cn, rostedt@goodmis.org, mhiramat@kernel.org, mark.rutland@arm.com, hengqi.chen@gmail.com, chenhuacai@kernel.org Cc: kernel@xen0n.name, zhangtianyang@loongson.cn, masahiroy@kernel.org, linux-kernel@vger.kernel.org, loongarch@lists.linux.dev, bpf@vger.kernel.org, duanchenghao@kylinos.cn, youling.tang@linux.dev, jianghaoran@kylinos.cn, vincent.mc.li@gmail.com, linux-trace-kernel@vger.kernel.org, Youling Tang Subject: [PATCH v4 7/7] LoongArch: ftrace: Adjust register stack restore order in direct call trampolines Date: Wed, 17 Dec 2025 14:14:35 +0800 Message-Id: <20251217061435.802204-8-duanchenghao@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20251217061435.802204-1-duanchenghao@kylinos.cn> References: <20251217061435.802204-1-duanchenghao@kylinos.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" Ensure that in the ftrace direct call logic, the CPU register state (with ra =3D 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 Signed-off-by: Chenghao Duan Acked-by: Steven Rostedt (Google) --- 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/f= trace-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-d= irect-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-dir= ect-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" --=20 2.25.1