From patchwork Tue Jan 31 14:22:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 50890 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2779510wrn; Tue, 31 Jan 2023 06:26:28 -0800 (PST) X-Google-Smtp-Source: AMrXdXtoClvewiLO9X5BCgg5BW5Dku+v1Y4tZQeCdOIHj685KsxWRi21zzvgqnSwUaYciWSSdNki X-Received: by 2002:a17:906:9f03:b0:877:962f:be0a with SMTP id fy3-20020a1709069f0300b00877962fbe0amr39831885ejc.37.1675175187816; Tue, 31 Jan 2023 06:26:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675175187; cv=none; d=google.com; s=arc-20160816; b=DB99gfEpgaQO5j++NTIndZ6U20Re8tqHpSeHF2OGp/I5h2yRr60AtlqWOKjtwSGigt 1hXbQp8vqIodMvAYxf3w0pje3itQLYhGLJdqvtdRJS9iziyk/IqCWC9eVIM3Nx8UuvQK eOI42djNksXiVZYy4BBbNHps4PsVoLVatjlfO0xcDCvdNdwJZ9D4kVTqcJuHiHodSVC8 l/FrbhNDb+mOw2uWFlJ0Ot4rKoVMcVQ4wwkihxz3StMya7Y3jOXHfsxh/MQwpCNEBRNu owjOixOgvCDKh5geQjVbk6ZvQg3yA3+mJrsoX1SQdShi2PhNF717uxwqGQz1UuCHtgpd hSDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=i3bzBs9Bh9DlNrWvc/viADz3sFPPO+Uti50tqCqd8Yk=; b=e0hX5j1aQ4fZhnRj2hGVQ3TIgAIHHAxqQMrNVRjBc3ibBDGRiW8F6DJv1h2Og12NDc fZToQBiWiXQti6mqz3/u9qzCm7FivYNrYwhiiX+cVlRzMyI/exZEJRblv6HyBHhQAGni UFGK+CwmXxzP+GGIFPF5O6av2fWpZjRIyTv/W/vhNlhiwZh7b25M4YaokK8+fa4HyjHq 8nqKa/OX6smeh4J0f/8OgPqEAR3Ui6uprQAM06dndH01Csaks9wtDnNSq2My7XDnD1Bo 36wlsjM6QJ9lEUuz3Q+U6WCLtlF8EmkJw6xdJ5SDBuiGLpMyKGTz4Opf69FRnz4/wFBe jozg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=QuSeKuN3; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=L26UrnZ5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id fi17-20020a1709073ad100b0087866163f4asi14292000ejc.989.2023.01.31.06.26.02; Tue, 31 Jan 2023 06:26:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=QuSeKuN3; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=L26UrnZ5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231565AbjAaOXl (ORCPT + 99 others); Tue, 31 Jan 2023 09:23:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231778AbjAaOXI (ORCPT ); Tue, 31 Jan 2023 09:23:08 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5876451C61; Tue, 31 Jan 2023 06:22:43 -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=i3bzBs9Bh9DlNrWvc/viADz3sFPPO+Uti50tqCqd8Yk=; b=QuSeKuN3pRhGwcM7cVlULuKuKC4/CK3+s6VZT+mMqVIFA+0NkyIryWxt8FJ+khFqvyquLh 7AvMqyS6yGkor6XbMduH5LpwEP5YuWQoa0vGnuNMgo740Rt7HWi2CKbt8yJoLfG2xDJeZI x7GwdsMdm+tUIbUKMGz3KicE4SXsC2f1ahBLUFnt7pbZX9OhGGOEjv1L3gvXTZh8LykB9b kpz8dWfxkxpQ7AvGcFPn+Oe+BOP+ui36GMvQSGoauwX2Jsqy5AOKpmCSwZCCOY6A32sxHD Ksk/fHFWhODoMDOSHa+FbtfUsCT6lbJtnHA+t0sRWj66INs69OIm7xBrm3nadQ== 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=i3bzBs9Bh9DlNrWvc/viADz3sFPPO+Uti50tqCqd8Yk=; b=L26UrnZ5wEQDzrLjp39em4MCIrOg3eAVN00cEZwWTbovNAOAtlm8Cm2MXDmdC6vTLLJl4B 9DQ91rfZu0/irFDQ== 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/alternatives: Introduce int3_emulate_jcc() Cc: "Peter Zijlstra (Intel)" , Ingo Molnar , "Masami Hiramatsu (Google)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230123210607.057678245@infradead.org> References: <20230123210607.057678245@infradead.org> MIME-Version: 1.0 Message-ID: <167517496157.4906.15756539194574782958.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756548497381273369?= X-GMAIL-MSGID: =?utf-8?q?1756548497381273369?= The following commit has been merged into the x86/alternatives branch of tip: Commit-ID: db7adcfd1cec4e95155e37bc066fddab302c6340 Gitweb: https://git.kernel.org/tip/db7adcfd1cec4e95155e37bc066fddab302c6340 Author: Peter Zijlstra AuthorDate: Mon, 23 Jan 2023 21:59:16 +01:00 Committer: Ingo Molnar CommitterDate: Tue, 31 Jan 2023 15:05:30 +01:00 x86/alternatives: Introduce int3_emulate_jcc() 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) Signed-off-by: Ingo Molnar Reviewed-by: Masami Hiramatsu (Google) Link: https://lore.kernel.org/r/20230123210607.057678245@infradead.org --- arch/x86/include/asm/text-patching.h | 31 ++++++++++++++++++++++- arch/x86/kernel/kprobes/core.c | 38 +++++---------------------- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/arch/x86/include/asm/text-patching.h b/arch/x86/include/asm/text-patching.h index f4b87f0..29832c3 100644 --- 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 *regs) unsigned long ip = 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, unsigned long disp) +{ + static const unsigned long jcc_mask[6] = { + [0] = X86_EFLAGS_OF, + [1] = X86_EFLAGS_CF, + [2] = X86_EFLAGS_ZF, + [3] = X86_EFLAGS_CF | X86_EFLAGS_ZF, + [4] = X86_EFLAGS_SF, + [5] = X86_EFLAGS_PF, + }; + + bool invert = cc & 1; + bool match; + + if (cc < 0xc) { + match = regs->flags & jcc_mask[cc >> 1]; + } else { + match = ((regs->flags & X86_EFLAGS_SF) >> X86_EFLAGS_SF_BIT) ^ + ((regs->flags & X86_EFLAGS_OF) >> X86_EFLAGS_OF_BIT); + if (cc >= 0xe) + match = match || (regs->flags & X86_EFLAGS_ZF); + } + + if ((match && !invert) || (!match && invert)) + ip += disp; + + int3_emulate_jmp(regs, ip); +} + #endif /* !CONFIG_UML_X86 */ #endif /* _ASM_X86_TEXT_PATCHING_H */ diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c index 6629968..d48638c 100644 --- a/arch/x86/kernel/kprobes/core.c +++ b/arch/x86/kernel/kprobes/core.c @@ -460,50 +460,26 @@ static void kprobe_emulate_call(struct kprobe *p, struct pt_regs *regs) } NOKPROBE_SYMBOL(kprobe_emulate_call); -static nokprobe_inline -void __kprobe_emulate_jmp(struct kprobe *p, struct pt_regs *regs, bool cond) +static void kprobe_emulate_jmp(struct kprobe *p, struct pt_regs *regs) { unsigned long ip = regs->ip - INT3_INSN_SIZE + p->ainsn.size; - if (cond) - ip += p->ainsn.rel32; + ip += 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); -static const unsigned long jcc_mask[6] = { - [0] = X86_EFLAGS_OF, - [1] = X86_EFLAGS_CF, - [2] = X86_EFLAGS_ZF, - [3] = X86_EFLAGS_CF | X86_EFLAGS_ZF, - [4] = X86_EFLAGS_SF, - [5] = X86_EFLAGS_PF, -}; - static void kprobe_emulate_jcc(struct kprobe *p, struct pt_regs *regs) { - bool invert = p->ainsn.jcc.type & 1; - bool match; + unsigned long ip = regs->ip - INT3_INSN_SIZE + p->ainsn.size; - if (p->ainsn.jcc.type < 0xc) { - match = regs->flags & jcc_mask[p->ainsn.jcc.type >> 1]; - } else { - match = ((regs->flags & X86_EFLAGS_SF) >> X86_EFLAGS_SF_BIT) ^ - ((regs->flags & X86_EFLAGS_OF) >> X86_EFLAGS_OF_BIT); - if (p->ainsn.jcc.type >= 0xe) - match = 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); static void kprobe_emulate_loop(struct kprobe *p, struct pt_regs *regs) { + unsigned long ip = regs->ip - INT3_INSN_SIZE + p->ainsn.size; bool match; if (p->ainsn.loop.type != 3) { /* LOOP* */ @@ -531,7 +507,9 @@ static void kprobe_emulate_loop(struct kprobe *p, struct pt_regs *regs) else if (p->ainsn.loop.type == 1) /* LOOPE */ match = match && (regs->flags & X86_EFLAGS_ZF); - __kprobe_emulate_jmp(p, regs, match); + if (match) + ip += p->ainsn.rel32; + int3_emulate_jmp(regs, ip); } NOKPROBE_SYMBOL(kprobe_emulate_loop);