Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp3733284pxp; Wed, 23 Mar 2022 04:58:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzWoeoaTBqgetiO/Arh2HFnjiN0IlCNORscpJivHtYbc+Wdwg5bMKrUMmmYB6LK73FrJ+K3 X-Received: by 2002:a17:90a:da02:b0:1bf:3919:f2a with SMTP id e2-20020a17090ada0200b001bf39190f2amr11197990pjv.208.1648036703932; Wed, 23 Mar 2022 04:58:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648036703; cv=none; d=google.com; s=arc-20160816; b=Cp/Eky6D2/bIzOzhixFfJHByLE30CL//Cfw7kOUx9FAcxqw4AACgRjntappyXkhlIO /DxAUIxTkcH2ZC2kZxk7/HLJMhM9m3Qrga6qeP6ZDU8sh4m8SJcof64Wt2O7Qu1Jdcva DMyPupd6YMOXBTTBdNBMJkM+sikemMEgK7hf+FbW4XY86Afd7GDoDzDob4Z7e/Eg8co7 5egFm80zW9na/D0bgmcrkNphP56T6w8C/bgqn6wbH97x5ece3k25cf25ByzNfKRNsHr7 X5Qf0e2aLC9961XsZduXKxBVVRRLQ07UFtKeXh5Elki57anM8QhDbUKMq2yCw+HtfjG1 cktA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=LEsbLTCobIra7Sk/dIaIRoBCRQ7K69SIvLYNzsU8sxk=; b=Ur1qg9kiR7dMypZTlV4vu+PJanWmpHJUrv0Chfq+n9aKmpxtAfegsXwzlRZShVnbOW PYcH20Sdzkb5l33v5l0BisUxZCgJrN9uLnJPsz6fTkk0oXDuPiQgXMzict+kxGJnwidU mSVej+Wam2uFjXwtgy6ClJxmKaT092IPyF5paBTY2CAYli97LHyQGMioziYJ3o1TtdaI GjSt+AwoBPJQc7J49VR08O/rldXkxdbAHoY/zvUnV7swUzaIS3vZDbIe6Ec6Ct/XJAD/ ZwLNvXyEe8uRZDERtzz6MwAofm3rwOUgfkYPn2wh+gadl2sPDQBa49yxxccZo8zetIGq O8vQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=E6K1ZYRl; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ch13-20020a17090af40d00b001bf70e727easi4869338pjb.101.2022.03.23.04.58.11; Wed, 23 Mar 2022 04:58:23 -0700 (PDT) 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=@infradead.org header.s=casper.20170209 header.b=E6K1ZYRl; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242427AbiCWIHr (ORCPT + 99 others); Wed, 23 Mar 2022 04:07:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232267AbiCWIHq (ORCPT ); Wed, 23 Mar 2022 04:07:46 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C943B70F7D; Wed, 23 Mar 2022 01:06:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=LEsbLTCobIra7Sk/dIaIRoBCRQ7K69SIvLYNzsU8sxk=; b=E6K1ZYRln+QCfhwJr1dNeYDuB4 lkWOpeUo120/1k0r28ad+wzCaZFnEqyUGZZgPYmXU8clSpES920R5Y3JFSBktMxDiLrdezidkX9Oe vVbCxyAdAZWF9reVZZ6cZSMbWPr4OJZMyBZxBVNx92AfRVtlrB12CfLT7Hek63FINclRtQYY5+Ice Vv3vohSWr3RlSuoi4fwtwoBRl4XhiJtJqVT0koE/WQgghRXgoL2wuUz/0qCg5qKE6O6g4hXGqy0r0 PYo7GjZcAuBOkl58vNsGJTYiltVzvOM976XVQRWG4B2V9LSHg90SPJkB5Blo1OAXSALxOeuEJE4lI 2B1WhYgA==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nWvzm-00CLRW-0x; Wed, 23 Mar 2022 08:05:30 +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) server-digest SHA256) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id C507430007E; Wed, 23 Mar 2022 09:05:26 +0100 (CET) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 79FF729A6CAA0; Wed, 23 Mar 2022 09:05:26 +0100 (CET) Date: Wed, 23 Mar 2022 09:05:26 +0100 From: Peter Zijlstra To: Masami Hiramatsu Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Jiri Olsa , Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Andrii Nakryiko , netdev@vger.kernel.org, bpf@vger.kernel.org, lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt , "Naveen N . Rao" , Anil S Keshavamurthy , "David S . Miller" Subject: Re: [PATCH v13 bpf-next 1/1] rethook: x86: Add rethook x86 implementation Message-ID: References: <164800288611.1716332.7053663723617614668.stgit@devnote2> <164800289923.1716332.9772144337267953560.stgit@devnote2> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <164800289923.1716332.9772144337267953560.stgit@devnote2> 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_NONE,T_SCC_BODY_TEXT_LINE 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 On Wed, Mar 23, 2022 at 11:34:59AM +0900, Masami Hiramatsu wrote: > Add rethook for x86 implementation. Most of the code has been copied from > kretprobes on x86. Right; as said, I'm really unhappy with growing a carbon copy of this stuff instead of sharing. Can we *please* keep it a single instance? Them being basically indentical, it should be trivial to have CONFIG_KPROBE_ON_RETHOOK (or somesuch) and just share this. Also, what's rethook for anyway? > diff --git a/arch/x86/kernel/kprobes/common.h b/arch/x86/kernel/kprobes/common.h > index 7d3a2e2daf01..c993521d4933 100644 > --- a/arch/x86/kernel/kprobes/common.h > +++ b/arch/x86/kernel/kprobes/common.h > @@ -6,6 +6,7 @@ > > #include > #include > +#include > > #ifdef CONFIG_X86_64 > > diff --git a/arch/x86/kernel/rethook.c b/arch/x86/kernel/rethook.c > new file mode 100644 > index 000000000000..3e916361c33b > --- /dev/null > +++ b/arch/x86/kernel/rethook.c > @@ -0,0 +1,121 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > +/* > + * x86 implementation of rethook. Mostly copied from arch/x86/kernel/kprobes/core.c. > + */ > +#include > +#include > +#include > +#include > + > +#include "kprobes/common.h" > + > +__visible void arch_rethook_trampoline_callback(struct pt_regs *regs); > + > +/* > + * When a target function returns, this code saves registers and calls > + * arch_rethook_trampoline_callback(), which calls the rethook handler. > + */ > +asm( > + ".text\n" > + ".global arch_rethook_trampoline\n" > + ".type arch_rethook_trampoline, @function\n" > + "arch_rethook_trampoline:\n" > +#ifdef CONFIG_X86_64 > + ANNOTATE_NOENDBR /* This is only jumped from ret instruction */ > + /* Push a fake return address to tell the unwinder it's a kretprobe. */ > + " pushq $arch_rethook_trampoline\n" > + UNWIND_HINT_FUNC " pushq $" __stringify(__KERNEL_DS) "\n" /* %ss */ /* Save the 'sp - 16', this will be fixed later. */ > + " pushq %rsp\n" > + " pushfq\n" > + SAVE_REGS_STRING > + " movq %rsp, %rdi\n" > + " call arch_rethook_trampoline_callback\n" > + RESTORE_REGS_STRING /* In the callback function, 'regs->flags' is copied to 'regs->ss'. */ this comment could do with a 'why' though... Because neither this nor the one in the handler really explains why it is important to have popf last " addq $16, %rsp\n" > + " popfq\n" > +#else same for i386: > + /* Push a fake return address to tell the unwinder it's a kretprobe. */ > + " pushl $arch_rethook_trampoline\n" > + UNWIND_HINT_FUNC /* Save the 'sp - 8', this will be fixed later. */ " pushl %ss\n" > + " pushl %esp\n" > + " pushfl\n" > + SAVE_REGS_STRING > + " movl %esp, %eax\n" > + " call arch_rethook_trampoline_callback\n" > + RESTORE_REGS_STRING /* In the callback function, 'regs->flags' is copied to 'regs->ss'. */ " addl $8, %esp\n" > + " popfl\n" > +#endif > + ASM_RET > + ".size arch_rethook_trampoline, .-arch_rethook_trampoline\n" > +); > +NOKPROBE_SYMBOL(arch_rethook_trampoline); > + > +/* > + * Called from arch_rethook_trampoline > + */ > +__used __visible void arch_rethook_trampoline_callback(struct pt_regs *regs) > +{ > + unsigned long *frame_pointer; > + > + /* fixup registers */ > + regs->cs = __KERNEL_CS; > +#ifdef CONFIG_X86_32 > + regs->gs = 0; > +#endif > + regs->ip = (unsigned long)&arch_rethook_trampoline; > + regs->orig_ax = ~0UL; regs->sp += 2*sizeof(long); > + frame_pointer = ®s->sp + 1; > + > + /* > + * The return address at 'frame_pointer' is recovered by the > + * arch_rethook_fixup_return() which called from this > + * rethook_trampoline_handler(). > + */ > + rethook_trampoline_handler(regs, (unsigned long)frame_pointer); > + > + /* > + * Copy FLAGS to 'pt_regs::sp' so that arch_rethook_trapmoline() > + * can do RET right after POPF. > + */ regs->ss = regs->flags; > +} > +NOKPROBE_SYMBOL(arch_rethook_trampoline_callback);