Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp383727imu; Mon, 26 Nov 2018 12:16:07 -0800 (PST) X-Google-Smtp-Source: AFSGD/XrP+fjCB9vQi4mdcVS0rA0sKwVauAJMFsgkWUpJQQdBJrGM2LL6/h9KxJ1MjjHsX8J3q/G X-Received: by 2002:a63:d10:: with SMTP id c16mr26579825pgl.382.1543263367625; Mon, 26 Nov 2018 12:16:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543263367; cv=none; d=google.com; s=arc-20160816; b=zmuXBtD3mVG6lWl2ce26R/PLhEK2C0kAkRbM1G7/EMB1YTOB6if9RmIorcIXeHwIZz QRXyBKVUOhnvrSjlcoKtUCkYdEabe9Z3qQfwCgrcher7gc9RJRIdZ6DxxNsrfNsM3Acn f5opTgPzd/22sm+0dQMMiHlBre1XBgqm4g9wZx6fH57Eb/XSzzU8qRmZq6HXwgXbDjdU QY3b/zG/UuJsBuTetWD2E5vREud9cWC3yoVfieDvA2aACdv/bpJsZc66qFzv+hBJos/G KrpyTE336yOlB1dbh7AkSm6cBA9kIkq7UudNRvAvXOhBjE94R2E5t+x9GddrXy9MKj0h eoJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=5ZlGWAaqbc2L4ov3vNDi4aoC6wR35smELlDpxeWTvNM=; b=KDEAdHybbdNdkwpAoOW78Cm57Tpwy0QIIdKa+P5rveDcuDXC73BJKUWIFYzaas58Mi 3/+ZDEEVQf0oupvzjUqrJ8gFd8cX6W5422Xgn/IqiujO63jYtaU/ZlIM9hek7iu1aWeE LbWP2BHUaHnDUNUqXv7NnLcfmYOzTd/wCBcbxPcXNEzTbi4hljcZJ1INsB3ASEBm96nM W77vxejv6z2MkXq+PWm9dt65Rtu+IzcIdZu+V7rPeFUgwzfXNVSgkXi9p7lF1talSbY5 Gj2A5IRGJxsy+pmv/zOBP0znzIn1L9w+qWKV9JX/ddvXLhXgiiLwlPDu8ugSxoa4oqN0 hANQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b36si1210908pgl.596.2018.11.26.12.15.39; Mon, 26 Nov 2018 12:16:07 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726918AbeK0HKJ (ORCPT + 99 others); Tue, 27 Nov 2018 02:10:09 -0500 Received: from mx1.redhat.com ([209.132.183.28]:47470 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726300AbeK0HKJ (ORCPT ); Tue, 27 Nov 2018 02:10:09 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E1A693086258; Mon, 26 Nov 2018 20:14:53 +0000 (UTC) Received: from treble (ovpn-121-105.rdu2.redhat.com [10.10.121.105]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9B50319C65; Mon, 26 Nov 2018 20:14:51 +0000 (UTC) Date: Mon, 26 Nov 2018 14:14:49 -0600 From: Josh Poimboeuf To: Andy Lutomirski Cc: Peter Zijlstra , X86 ML , LKML , Ard Biesheuvel , Steven Rostedt , Ingo Molnar , Thomas Gleixner , Linus Torvalds , Masami Hiramatsu , Jason Baron , Jiri Kosina , David Laight , Borislav Petkov , julia@ni.com, jeyu@kernel.org, "H. Peter Anvin" Subject: Re: [PATCH v2 4/4] x86/static_call: Add inline static call implementation for x86-64 Message-ID: <20181126201449.xepvj3owldkrsvgr@treble> References: <62188c62f6dda49ca2e20629ee8e5a62a6c0b500.1543200841.git.jpoimboe@redhat.com> <20181126160217.GR2113@hirez.programming.kicks-ass.net> <20181126171036.chcbmb35ygpxziub@treble> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20180716 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Mon, 26 Nov 2018 20:14:54 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Nov 26, 2018 at 10:28:08AM -0800, Andy Lutomirski wrote: > On Mon, Nov 26, 2018 at 9:10 AM Josh Poimboeuf wrote: > > > > On Mon, Nov 26, 2018 at 05:02:17PM +0100, Peter Zijlstra wrote: > > > On Mon, Nov 26, 2018 at 07:55:00AM -0600, Josh Poimboeuf wrote: > > > > diff --git a/arch/x86/kernel/static_call.c b/arch/x86/kernel/static_call.c > > > > index 8026d176f25c..d3869295b88d 100644 > > > > --- a/arch/x86/kernel/static_call.c > > > > +++ b/arch/x86/kernel/static_call.c > > > > @@ -9,13 +9,21 @@ > > > > > > > > void static_call_bp_handler(void); > > > > void *bp_handler_dest; > > > > +void *bp_handler_continue; > > > > > > > > asm(".pushsection .text, \"ax\" \n" > > > > ".globl static_call_bp_handler \n" > > > > ".type static_call_bp_handler, @function \n" > > > > "static_call_bp_handler: \n" > > > > - "ANNOTATE_RETPOLINE_SAFE \n" > > > > +#ifdef CONFIG_HAVE_STATIC_CALL_INLINE > > > > + ANNOTATE_RETPOLINE_SAFE > > > > + "call *bp_handler_dest \n" > > > > + ANNOTATE_RETPOLINE_SAFE > > > > + "jmp *bp_handler_continue \n" > > > > +#else /* !CONFIG_HAVE_STATIC_CALL_INLINE */ > > > > + ANNOTATE_RETPOLINE_SAFE > > > > "jmp *bp_handler_dest \n" > > > > +#endif > > > > ".popsection \n"); > > > > > > > > void arch_static_call_transform(void *site, void *tramp, void *func) > > > > @@ -25,7 +33,10 @@ void arch_static_call_transform(void *site, void *tramp, void *func) > > > > unsigned char insn_opcode; > > > > unsigned char opcodes[CALL_INSN_SIZE]; > > > > > > > > - insn = (unsigned long)tramp; > > > > + if (IS_ENABLED(CONFIG_HAVE_STATIC_CALL_INLINE)) > > > > + insn = (unsigned long)site; > > > > + else > > > > + insn = (unsigned long)tramp; > > > > > > > > mutex_lock(&text_mutex); > > > > > > > > @@ -41,8 +52,10 @@ void arch_static_call_transform(void *site, void *tramp, void *func) > > > > opcodes[0] = insn_opcode; > > > > memcpy(&opcodes[1], &dest_relative, CALL_INSN_SIZE - 1); > > > > > > > > - /* Set up the variable for the breakpoint handler: */ > > > > + /* Set up the variables for the breakpoint handler: */ > > > > bp_handler_dest = func; > > > > + if (IS_ENABLED(CONFIG_HAVE_STATIC_CALL_INLINE)) > > > > + bp_handler_continue = (void *)(insn + CALL_INSN_SIZE); > > > > > > > > /* Patch the call site: */ > > > > text_poke_bp((void *)insn, opcodes, CALL_INSN_SIZE, > > > > > > OK, so this is where that static_call_bp_handler comes from; you need > > > that CALL to frob the stack. > > > > > > But I still think it is broken; consider: > > > > > > CPU0 CPU1 > > > > > > bp_handler = ponies; > > > > > > text_poke_bp(, &static_call_bp_handler) > > > text_poke(&int3); > > > on_each_cpu(sync) > > > > > > ... > > > > > > > > > text_poke(/* all but first bytes */) > > > on_each_cpu(sync) > > > > > > ... > > > > > > > > > > > > pt_regs->ip = &static_call_bp_handler > > > > > > > > > // VCPU takes a nap... > > > text_poke(/* first byte */) > > > on_each_cpu(sync) > > > > > > ... > > > > > > > > > // VCPU sleeps more > > > bp_handler = unicorn; > > > > > > CALL unicorn > > > > > > *whoops* > > > > > > Now, granted, that is all rather 'unlikely', but that never stopped > > > Murphy. > > > > Good find, thanks Peter. > > > > As we discussed on IRC, we'll need to fix this from within the int3 > > exception handler by faking the call: putting a fake return address on > > the stack (pointing to right after the call) and setting regs->ip to the > > called function. > > Can you add a comment that it will need updating when kernel CET is added? Will do, though I get the feeling there's a lot of other (existing) code that will also need to change for kernel CET. -- Josh