From nobody Sun Sep 14 09:35:46 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 E9C51C05027 for ; Mon, 23 Jan 2023 21:07:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232894AbjAWVHF (ORCPT ); Mon, 23 Jan 2023 16:07:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232599AbjAWVHC (ORCPT ); Mon, 23 Jan 2023 16:07:02 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E58530E95 for ; Mon, 23 Jan 2023 13:07:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=pkk66lDIJdbY60ClGNOpgyRAJcMk6omCEJ+djxw5JoA=; b=qVtHjS1IZwObvMJEhV0hetLUHa +rbrAxQlZcVwT0dQnAArr5cbptj8fQDv4vnpEv8YMg7FKIaPFOEGv4ZFCxmFm6OOVVLvHWjS5HAp4 5eYb5dZRMKkD5qNJQ9EGuyETZM/0AWHKgmJR0ByGfZXrTNgbfGCNKaVNPw4Qi9bZgZISVMqCLbanB VzMLHomINHP63+4/SN8BzPN4sCB03UgUZc5QTKjY5yCI53lGkKRb8U1okcSbVA8xG2DVqin3Bb21/ GeQhsgbCuPhEn2z0dbKFltN9hknYu27I5cnEIQtVt0IWbj4p2CwGmo/dTc9xyLZ2vYGtum7JYTDE8 +nNPnVLg==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1pK41f-004Wrc-CF; Mon, 23 Jan 2023 21:06:47 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 6217B300673; Mon, 23 Jan 2023 22:06:45 +0100 (CET) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 0) id 2930E2038B0D9; Mon, 23 Jan 2023 22:06:45 +0100 (CET) Message-ID: <20230123210607.057678245@infradead.org> User-Agent: quilt/0.66 Date: Mon, 23 Jan 2023 21:59:16 +0100 From: Peter Zijlstra To: mingo@kernel.org Cc: tglx@linutronix.de, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, jpoimboe@kernel.org, jbaron@akamai.com, rostedt@goodmis.org, ardb@kernel.org, linux-kernel@vger.kernel.org, erhard_f@mailbox.org, ndesaulniers@google.com, mhiramat@kernel.org, sandipan.das@amd.com Subject: [PATCH 1/3] x86/alternative: Introduce int3_emulate_jcc() References: <20230123205915.751729592@infradead.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move the kprobe Jcc emulation into int3_emulate_jcc() so it can be used by more code -- specifically static_call() will need this. Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Masami Hiramatsu (Google) --- arch/x86/include/asm/text-patching.h | 31 ++++++++++++++++++++++++++++ arch/x86/kernel/kprobes/core.c | 38 +++++++-----------------------= ----- 2 files changed, 39 insertions(+), 30 deletions(-) --- a/arch/x86/include/asm/text-patching.h +++ b/arch/x86/include/asm/text-patching.h @@ -184,6 +184,37 @@ void int3_emulate_ret(struct pt_regs *re unsigned long ip =3D int3_emulate_pop(regs); int3_emulate_jmp(regs, ip); } + +static __always_inline +void int3_emulate_jcc(struct pt_regs *regs, u8 cc, unsigned long ip, unsig= ned long disp) +{ + static const unsigned long jcc_mask[6] =3D { + [0] =3D X86_EFLAGS_OF, + [1] =3D X86_EFLAGS_CF, + [2] =3D X86_EFLAGS_ZF, + [3] =3D X86_EFLAGS_CF | X86_EFLAGS_ZF, + [4] =3D X86_EFLAGS_SF, + [5] =3D X86_EFLAGS_PF, + }; + + bool invert =3D cc & 1; + bool match; + + if (cc < 0xc) { + match =3D regs->flags & jcc_mask[cc >> 1]; + } else { + match =3D ((regs->flags & X86_EFLAGS_SF) >> X86_EFLAGS_SF_BIT) ^ + ((regs->flags & X86_EFLAGS_OF) >> X86_EFLAGS_OF_BIT); + if (cc >=3D 0xe) + match =3D match || (regs->flags & X86_EFLAGS_ZF); + } + + if ((match && !invert) || (!match && invert)) + ip +=3D disp; + + int3_emulate_jmp(regs, ip); +} + #endif /* !CONFIG_UML_X86 */ =20 #endif /* _ASM_X86_TEXT_PATCHING_H */ --- a/arch/x86/kernel/kprobes/core.c +++ b/arch/x86/kernel/kprobes/core.c @@ -464,50 +464,26 @@ static void kprobe_emulate_call(struct k } NOKPROBE_SYMBOL(kprobe_emulate_call); =20 -static nokprobe_inline -void __kprobe_emulate_jmp(struct kprobe *p, struct pt_regs *regs, bool con= d) +static void kprobe_emulate_jmp(struct kprobe *p, struct pt_regs *regs) { unsigned long ip =3D regs->ip - INT3_INSN_SIZE + p->ainsn.size; =20 - if (cond) - ip +=3D p->ainsn.rel32; + ip +=3D p->ainsn.rel32; int3_emulate_jmp(regs, ip); } - -static void kprobe_emulate_jmp(struct kprobe *p, struct pt_regs *regs) -{ - __kprobe_emulate_jmp(p, regs, true); -} NOKPROBE_SYMBOL(kprobe_emulate_jmp); =20 -static const unsigned long jcc_mask[6] =3D { - [0] =3D X86_EFLAGS_OF, - [1] =3D X86_EFLAGS_CF, - [2] =3D X86_EFLAGS_ZF, - [3] =3D X86_EFLAGS_CF | X86_EFLAGS_ZF, - [4] =3D X86_EFLAGS_SF, - [5] =3D X86_EFLAGS_PF, -}; - static void kprobe_emulate_jcc(struct kprobe *p, struct pt_regs *regs) { - bool invert =3D p->ainsn.jcc.type & 1; - bool match; + unsigned long ip =3D regs->ip - INT3_INSN_SIZE + p->ainsn.size; =20 - if (p->ainsn.jcc.type < 0xc) { - match =3D regs->flags & jcc_mask[p->ainsn.jcc.type >> 1]; - } else { - match =3D ((regs->flags & X86_EFLAGS_SF) >> X86_EFLAGS_SF_BIT) ^ - ((regs->flags & X86_EFLAGS_OF) >> X86_EFLAGS_OF_BIT); - if (p->ainsn.jcc.type >=3D 0xe) - match =3D match || (regs->flags & X86_EFLAGS_ZF); - } - __kprobe_emulate_jmp(p, regs, (match && !invert) || (!match && invert)); + int3_emulate_jcc(regs, p->ainsn.jcc.type, ip, p->ainsn.rel32); } NOKPROBE_SYMBOL(kprobe_emulate_jcc); =20 static void kprobe_emulate_loop(struct kprobe *p, struct pt_regs *regs) { + unsigned long ip =3D regs->ip - INT3_INSN_SIZE + p->ainsn.size; bool match; =20 if (p->ainsn.loop.type !=3D 3) { /* LOOP* */ @@ -535,7 +511,9 @@ static void kprobe_emulate_loop(struct k else if (p->ainsn.loop.type =3D=3D 1) /* LOOPE */ match =3D match && (regs->flags & X86_EFLAGS_ZF); =20 - __kprobe_emulate_jmp(p, regs, match); + if (match) + ip +=3D p->ainsn.rel32; + int3_emulate_jmp(regs, ip); } NOKPROBE_SYMBOL(kprobe_emulate_loop); From nobody Sun Sep 14 09:35:46 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 BCFEAC25B50 for ; Mon, 23 Jan 2023 21:07:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232977AbjAWVHT (ORCPT ); Mon, 23 Jan 2023 16:07:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232907AbjAWVHP (ORCPT ); Mon, 23 Jan 2023 16:07:15 -0500 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06C2638675 for ; Mon, 23 Jan 2023 13:07:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=43U4bn0+EhRePAQFEfIEHYSWEzea++Rs3FkcHjx/rv8=; b=GNpOpTvJ2abtxz1GuR6oDPbBFH gRKJX5F+/8CQ8hBkc06D7Df/ATaLdINRo44g2Pk0aJKTHl5l9YD8P8daAyP/ByYwM3uBZ5RTwrCOX BxFj52kR1hNUkdjf90+2jhrY4snrl5Kl55XqFBR50utZdC/3a8a8wUnOtWoRIaC3SeuLvEbvvP2nK 4pJ8WqWKt3CaixdXdRXb940NueJDrWXM3Dgl4K5h9E2+vIMV1Rnik6UP6rcf6xLjTsJrwQTq72GRh LE5wmnndzETgilE+jRoElqNMIoM3TGpi3wOopZd0g4LKFPX5F268UTdE0YW4dnT3Hff6Z6Y5tKG29 VcxQsalg==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1pK41C-001e3s-06; Mon, 23 Jan 2023 21:06:19 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 64A43300747; Mon, 23 Jan 2023 22:06:45 +0100 (CET) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 0) id 2C7C72038B0DA; Mon, 23 Jan 2023 22:06:45 +0100 (CET) Message-ID: <20230123210607.115718513@infradead.org> User-Agent: quilt/0.66 Date: Mon, 23 Jan 2023 21:59:17 +0100 From: Peter Zijlstra To: mingo@kernel.org Cc: tglx@linutronix.de, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, jpoimboe@kernel.org, jbaron@akamai.com, rostedt@goodmis.org, ardb@kernel.org, linux-kernel@vger.kernel.org, erhard_f@mailbox.org, ndesaulniers@google.com, mhiramat@kernel.org, sandipan.das@amd.com Subject: [PATCH 2/3] x86/alternative: Teach text_poke_bp() to patch Jcc.d32 instructions References: <20230123205915.751729592@infradead.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In order to re-write Jcc.d32 instructions text_poke_bp() needs to be taught about them. The biggest hurdle is that the whole machinery is currently made for 5 byte instructions and extending this would grow struct text_poke_loc which is currently a nice 16 bytes and used in an array. However, since text_poke_loc contains a full copy of the (s32) displacement, it is possible to map the Jcc.d32 2 byte opcodes to Jcc.d8 1 byte opcode for the int3 emulation. This then leaves the replacement bytes; fudge that by only storing the last 5 bytes and adding the rule that 'length =3D=3D 6' instruction will be prefixed with a 0x0f byte. Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Masami Hiramatsu (Google) --- arch/x86/kernel/alternative.c | 62 +++++++++++++++++++++++++++++++------= ----- 1 file changed, 47 insertions(+), 15 deletions(-) --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -338,6 +338,12 @@ void __init_or_module noinline apply_alt } } =20 +static inline bool is_jcc32(struct insn *insn) +{ + /* Jcc.d32 second opcode byte is in the range: 0x80-0x8f */ + return insn->opcode.bytes[0] =3D=3D 0x0f && (insn->opcode.bytes[1] & 0xf0= ) =3D=3D 0x80; +} + #if defined(CONFIG_RETPOLINE) && defined(CONFIG_OBJTOOL) =20 /* @@ -376,12 +382,6 @@ static int emit_indirect(int op, int reg return i; } =20 -static inline bool is_jcc32(struct insn *insn) -{ - /* Jcc.d32 second opcode byte is in the range: 0x80-0x8f */ - return insn->opcode.bytes[0] =3D=3D 0x0f && (insn->opcode.bytes[1] & 0xf0= ) =3D=3D 0x80; -} - static int emit_call_track_retpoline(void *addr, struct insn *insn, int re= g, u8 *bytes) { u8 op =3D insn->opcode.bytes[0]; @@ -1770,6 +1770,11 @@ void text_poke_sync(void) on_each_cpu(do_sync_core, NULL, 1); } =20 +/* + * NOTE: crazy scheme to allow patching Jcc.d32 but not increase the size = of + * this thing. When len =3D=3D 6 everything is prefixed with 0x0f and we m= ap + * opcode to Jcc.d8, using len to distinguish. + */ struct text_poke_loc { /* addr :=3D _stext + rel_addr */ s32 rel_addr; @@ -1891,6 +1896,10 @@ noinstr int poke_int3_handler(struct pt_ int3_emulate_jmp(regs, (long)ip + tp->disp); break; =20 + case 0x70 ... 0x7f: /* Jcc */ + int3_emulate_jcc(regs, tp->opcode & 0xf, (long)ip, tp->disp); + break; + default: BUG(); } @@ -1964,16 +1973,26 @@ static void text_poke_bp_batch(struct te * Second step: update all but the first byte of the patched range. */ for (do_sync =3D 0, i =3D 0; i < nr_entries; i++) { - u8 old[POKE_MAX_OPCODE_SIZE] =3D { tp[i].old, }; + u8 old[POKE_MAX_OPCODE_SIZE+1] =3D { tp[i].old, }; + u8 _new[POKE_MAX_OPCODE_SIZE+1]; + const u8 *new =3D tp[i].text; int len =3D tp[i].len; =20 if (len - INT3_INSN_SIZE > 0) { memcpy(old + INT3_INSN_SIZE, text_poke_addr(&tp[i]) + INT3_INSN_SIZE, len - INT3_INSN_SIZE); + + if (len =3D=3D 6) { + _new[0] =3D 0x0f; + memcpy(_new + 1, new, 5); + new =3D _new; + } + text_poke(text_poke_addr(&tp[i]) + INT3_INSN_SIZE, - (const char *)tp[i].text + INT3_INSN_SIZE, + new + INT3_INSN_SIZE, len - INT3_INSN_SIZE); + do_sync++; } =20 @@ -2001,8 +2020,7 @@ static void text_poke_bp_batch(struct te * The old instruction is recorded so that the event can be * processed forwards or backwards. */ - perf_event_text_poke(text_poke_addr(&tp[i]), old, len, - tp[i].text, len); + perf_event_text_poke(text_poke_addr(&tp[i]), old, len, new, len); } =20 if (do_sync) { @@ -2019,10 +2037,15 @@ static void text_poke_bp_batch(struct te * replacing opcode. */ for (do_sync =3D 0, i =3D 0; i < nr_entries; i++) { - if (tp[i].text[0] =3D=3D INT3_INSN_OPCODE) + u8 byte =3D tp[i].text[0]; + + if (tp[i].len =3D=3D 6) + byte =3D 0x0f; + + if (byte =3D=3D INT3_INSN_OPCODE) continue; =20 - text_poke(text_poke_addr(&tp[i]), tp[i].text, INT3_INSN_SIZE); + text_poke(text_poke_addr(&tp[i]), &byte, INT3_INSN_SIZE); do_sync++; } =20 @@ -2040,9 +2063,11 @@ static void text_poke_loc_init(struct te const void *opcode, size_t len, const void *emulate) { struct insn insn; - int ret, i; + int ret, i =3D 0; =20 - memcpy((void *)tp->text, opcode, len); + if (len =3D=3D 6) + i =3D 1; + memcpy((void *)tp->text, opcode+i, len-i); if (!emulate) emulate =3D opcode; =20 @@ -2053,6 +2078,13 @@ static void text_poke_loc_init(struct te tp->len =3D len; tp->opcode =3D insn.opcode.bytes[0]; =20 + if (is_jcc32(&insn)) { + /* + * Map Jcc.d32 onto Jcc.d8 and use len to distinguish. + */ + tp->opcode =3D insn.opcode.bytes[1] - 0x10; + } + switch (tp->opcode) { case RET_INSN_OPCODE: case JMP32_INSN_OPCODE: @@ -2069,7 +2101,6 @@ static void text_poke_loc_init(struct te BUG_ON(len !=3D insn.length); } =20 - switch (tp->opcode) { case INT3_INSN_OPCODE: case RET_INSN_OPCODE: @@ -2078,6 +2109,7 @@ static void text_poke_loc_init(struct te case CALL_INSN_OPCODE: case JMP32_INSN_OPCODE: case JMP8_INSN_OPCODE: + case 0x70 ... 0x7f: /* Jcc */ tp->disp =3D insn.immediate.value; break; From nobody Sun Sep 14 09:35:46 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 1CDE2C05027 for ; Mon, 23 Jan 2023 21:07:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232963AbjAWVHR (ORCPT ); Mon, 23 Jan 2023 16:07:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232599AbjAWVHO (ORCPT ); Mon, 23 Jan 2023 16:07:14 -0500 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 060A938667 for ; Mon, 23 Jan 2023 13:07:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=KKXDnmc2Es+6ZmW+DV2eYtRfLFf2Gu02YvShw/3lB4Y=; b=PT4NpcyeoJrAlUFkM+JxIKvOzE wat/l+Lls2OmdG/RSotNTpPyBaTs30NSFT3/OKTFTFQwWiMnNzcxsP2g8kVvUaBDZZQFUfFcby1DU HDyhwUB2Huqfc6yTcFDqh4mpzpV21524uoQ4mVDXvKYdQWV7zBd5uyoOACAuO5EAjrg8OtgbUIebV 9vA8P000to1tKOp8gWyG8+fQswPZhYiJjr0CFD7hOWlXGxFJM6HNCCAFcJXEg7zHUzErZ7FWw/Kci 1ZVm89rNWAv/Od7R1wtLVfUR3Nj+5Of6XgWxgfOt+XO4/L/HMrI7DnNX9TwiNBm7ftLk/TFJsCDPn cYfx0lKg==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1pK41C-001e3t-05; Mon, 23 Jan 2023 21:06:19 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 5D84B30008D; Mon, 23 Jan 2023 22:06:45 +0100 (CET) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 0) id 300842038B0DB; Mon, 23 Jan 2023 22:06:45 +0100 (CET) Message-ID: <20230123210607.173715335@infradead.org> User-Agent: quilt/0.66 Date: Mon, 23 Jan 2023 21:59:18 +0100 From: Peter Zijlstra To: mingo@kernel.org Cc: tglx@linutronix.de, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, jpoimboe@kernel.org, jbaron@akamai.com, rostedt@goodmis.org, ardb@kernel.org, linux-kernel@vger.kernel.org, erhard_f@mailbox.org, ndesaulniers@google.com, mhiramat@kernel.org, sandipan.das@amd.com Subject: [PATCH 3/3] x86/static_call: Add support for Jcc tail-calls References: <20230123205915.751729592@infradead.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Clang likes to create conditional tail calls like: 0000000000000350 : 350: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1) 351: R_X86_64_NO= NE __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 Teach the in-line static call text patching about this. Notably, since there is no conditional-ret, in that caes 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) Reviewed-by: Masami Hiramatsu (Google) --- arch/x86/kernel/static_call.c | 50 +++++++++++++++++++++++++++++++++++++= ++--- 1 file changed, 47 insertions(+), 3 deletions(-) --- 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 =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_transfor 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_transfor 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 =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 ||