From nobody Sat Sep 13 18:29:30 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 93180C38142 for ; Tue, 31 Jan 2023 14:23:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231968AbjAaOX0 (ORCPT ); Tue, 31 Jan 2023 09:23:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54406 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231761AbjAaOXF (ORCPT ); Tue, 31 Jan 2023 09:23:05 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF9CD51C5A; Tue, 31 Jan 2023 06:22:42 -0800 (PST) Date: Tue, 31 Jan 2023 14:22:41 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1675174961; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QpNIJKaiSPxzce4dG4J7cfut+YjJQ42C6IM37ZHcyFo=; b=gDW+5nJyeqOY4XyKnp0+yZR+EhXwFJRcm+Fv9YIV+E3frxXexh+/xENK+f7DpC8VaYddbm 5yZTXLMLqY0kcSo0rdB2ica2nYTLwji9seAdF1iSRGFTlALm/CX3wRX+WmDeCAhd4I0hzl 2TmdARkqiNWliM9UE5CjeOGlQbG72pwJOlem35C/Ry5V76efPlt+ZyzxwL2N9hcla9KMth NNViBIlnojHEQeUOGoKVnCrOypsbz118Gob7zYGgfvCONTPd73l8txvyj/BBQStVSnB/Vr 3H+eG9pztDaZwHh/xX/Pc6ssn4tnDrnCK36mJlyJA+fjjmxrJoPTTANps1aNAA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1675174961; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QpNIJKaiSPxzce4dG4J7cfut+YjJQ42C6IM37ZHcyFo=; b=gQdFoaxwSU37KGdOlSZHmc+8rY2NbO2X3BJlvzK4rzKcvSeDY1/pWg9X7+RRcpSZJuytTB e9ZvBoCzUPZMO0DA== From: "tip-bot2 for Peter Zijlstra" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/alternatives] x86/static_call: Add support for Jcc tail-calls Cc: "Erhard F." , "Peter Zijlstra (Intel)" , Ingo Molnar , "Masami Hiramatsu (Google)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: References: MIME-Version: 1.0 Message-ID: <167517496101.4906.9654598039677692808.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the x86/alternatives branch of ti= p: Commit-ID: 923510c88d2b7d947c4217835fd9ca6bd65cc56c Gitweb: https://git.kernel.org/tip/923510c88d2b7d947c4217835fd9ca6bd= 65cc56c Author: Peter Zijlstra AuthorDate: Thu, 26 Jan 2023 16:34:27 +01:00 Committer: Ingo Molnar CommitterDate: Tue, 31 Jan 2023 15:05:31 +01:00 x86/static_call: Add support for Jcc tail-calls Clang likes to create conditional tail calls like: 0000000000000350 : 350: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1) 351: R_X86_64_= NONE __fentry__-0x4 355: 48 83 bf 20 01 00 00 00 cmpq $0x0,0x120(%rdi) 35d: 0f 85 00 00 00 00 jne 363 = 35f: R_X86_64_PLT32 __SCT__amd_pmu_branch_add-0x4 363: e9 00 00 00 00 jmp 368 = 364: R_X86_64_PLT32 __x86_return_thunk-0x4 Where 0x35d is a static call site that's turned into a conditional tail-call using the Jcc class of instructions. Teach the in-line static call text patching about this. Notably, since there is no conditional-ret, in that case patch the Jcc to point at an empty stub function that does the ret -- or the return thunk when needed. Reported-by: "Erhard F." Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Ingo Molnar Reviewed-by: Masami Hiramatsu (Google) Link: https://lore.kernel.org/r/Y9Kdg9QjHkr9G5b5@hirez.programming.kicks-as= s.net --- arch/x86/kernel/static_call.c | 50 +++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/static_call.c b/arch/x86/kernel/static_call.c index 2ebc338..b70670a 100644 --- a/arch/x86/kernel/static_call.c +++ b/arch/x86/kernel/static_call.c @@ -9,6 +9,7 @@ enum insn_type { NOP =3D 1, /* site cond-call */ JMP =3D 2, /* tramp / site tail-call */ RET =3D 3, /* tramp / site cond-tail-call */ + JCC =3D 4, }; =20 /* @@ -25,12 +26,40 @@ static const u8 xor5rax[] =3D { 0x2e, 0x2e, 0x2e, 0x31,= 0xc0 }; =20 static const u8 retinsn[] =3D { RET_INSN_OPCODE, 0xcc, 0xcc, 0xcc, 0xcc }; =20 +static u8 __is_Jcc(u8 *insn) /* Jcc.d32 */ +{ + u8 ret =3D 0; + + if (insn[0] =3D=3D 0x0f) { + u8 tmp =3D insn[1]; + if ((tmp & 0xf0) =3D=3D 0x80) + ret =3D tmp; + } + + return ret; +} + +extern void __static_call_return(void); + +asm (".global __static_call_return\n\t" + ".type __static_call_return, @function\n\t" + ASM_FUNC_ALIGN "\n\t" + "__static_call_return:\n\t" + ANNOTATE_NOENDBR + ANNOTATE_RETPOLINE_SAFE + "ret; int3\n\t" + ".size __static_call_return, . - __static_call_return \n\t"); + static void __ref __static_call_transform(void *insn, enum insn_type type, void *func, bool modinit) { const void *emulate =3D NULL; int size =3D CALL_INSN_SIZE; const void *code; + u8 op, buf[6]; + + if ((type =3D=3D JMP || type =3D=3D RET) && (op =3D __is_Jcc(insn))) + type =3D JCC; =20 switch (type) { case CALL: @@ -57,6 +86,20 @@ static void __ref __static_call_transform(void *insn, en= um insn_type type, else code =3D &retinsn; break; + + case JCC: + if (!func) { + func =3D __static_call_return; + if (cpu_feature_enabled(X86_FEATURE_RETHUNK)) + func =3D x86_return_thunk; + } + + buf[0] =3D 0x0f; + __text_gen_insn(buf+1, op, insn+1, func, 5); + code =3D buf; + size =3D 6; + + break; } =20 if (memcmp(insn, code, size) =3D=3D 0) @@ -68,9 +111,9 @@ static void __ref __static_call_transform(void *insn, en= um insn_type type, text_poke_bp(insn, code, size, emulate); } =20 -static void __static_call_validate(void *insn, bool tail, bool tramp) +static void __static_call_validate(u8 *insn, bool tail, bool tramp) { - u8 opcode =3D *(u8 *)insn; + u8 opcode =3D insn[0]; =20 if (tramp && memcmp(insn+5, tramp_ud, 3)) { pr_err("trampoline signature fail"); @@ -79,7 +122,8 @@ static void __static_call_validate(void *insn, bool tail= , bool tramp) =20 if (tail) { if (opcode =3D=3D JMP32_INSN_OPCODE || - opcode =3D=3D RET_INSN_OPCODE) + opcode =3D=3D RET_INSN_OPCODE || + __is_Jcc(insn)) return; } else { if (opcode =3D=3D CALL_INSN_OPCODE ||