Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp262090imu; Mon, 26 Nov 2018 10:32:10 -0800 (PST) X-Google-Smtp-Source: AFSGD/Ud2Yd5/9kFocuTT082ZIOztD4ysuP6YeIajCcwv83HyAbn1b3a2CMCOcgJSql+qWtzMP0c X-Received: by 2002:a63:e754:: with SMTP id j20mr25131683pgk.228.1543257130628; Mon, 26 Nov 2018 10:32:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543257130; cv=none; d=google.com; s=arc-20160816; b=RzjqhmEtXDn6Th64xTwyVycjLIsY7jAnuU2R7EZqJNqBygpygt/NIM/tEIIDPeXsxE OldoNCaxsd+YsunAgIZAnyMRFcZaMkJk+38BEjZL4DqeDQ2ZyIPQ+b4oUe8p260l3Dg/ 43U0s7k870iQNx0RTT8GToiIG6mBKMxm+obyLlB5HB3i3r9GCQiJQxheE/RCFwoCXxql kvux2TvMhRfik0Y+Yzy6/e84n9p4Ml68Klw+WNUyY5CBuYpg84ukIEuvzv4xbx5Nor5r F89I8K7sWVm0lcwdc3NgeDqTrSK43YgmpfzScM1ry50iUDSQ3AgcWFXCSwEdUjdCsE9o 7gWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=MuTVLZPSxWezrAqFfZuNwMU47o/EvJN5J4NQUdnKeeM=; b=lNx2sPiW0s/2mMQwZjbHxbRWVQ2a3g686vjz6OMw7MU0OrC+vT2iDGfOfHI0uoQLiV kO5EQ30oPd0FUMbv+4oc7lgIyzZs9axUrdHblLSCQwTax7VGN5NYWHmWNXmfqvM3rn+k pbQ3C1bkp+5RUBcr8I5pRe3/5YaXRIMTSNJnDWV1mY2SJZqrk4VlF7+r2FKSOthnc+Ch YzDU1lzo4+DKOtVHpjzVcZzkOIn7ptMSoR0bbDjyDGuftCFiMugPl1/WhFMyYOCaKUy4 5V0YqR5PcVA1Ocqq5yQ4pzEdvvXHLyWwlaCMRBVRutwQ2mx5RAS9QkWD+/wt4jPPMzyR 1gtg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=WqbAlI9s; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i20si958681pgm.586.2018.11.26.10.31.23; Mon, 26 Nov 2018 10:32:10 -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; dkim=pass header.i=@kernel.org header.s=default header.b=WqbAlI9s; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726603AbeK0FXT (ORCPT + 99 others); Tue, 27 Nov 2018 00:23:19 -0500 Received: from mail.kernel.org ([198.145.29.99]:51066 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725199AbeK0FXT (ORCPT ); Tue, 27 Nov 2018 00:23:19 -0500 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5C5382086E for ; Mon, 26 Nov 2018 18:28:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1543256902; bh=1FbfjxumzL31JcgS38noQ3Mg7ckl6RT6axMBxEObQV4=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=WqbAlI9slgEI354KkMfMS25Q/Wg28Xd6HgNjSf51wZd50Oivd9WI7ydcC94VDvGNA fB9RggYFsyru+nTrEP7Q5Z2luidB0sKM3Vi7TE80ftAvmEBPJZFBBhKppCUmJEysp1 C843E1gaRgv84wv9NW9/x+ESPjZxSd02as1+GcSM= Received: by mail-wm1-f44.google.com with SMTP id y185so13147243wmd.1 for ; Mon, 26 Nov 2018 10:28:22 -0800 (PST) X-Gm-Message-State: AA+aEWb/r1lL5EYILczENIMwu0DaePLfuQmcL31B1+f2uQQKU7gyL209 mLJLxHRZVxEBHAHNkIMrVQguFO/Gtp4irunAvi3pbw== X-Received: by 2002:a1c:b1d5:: with SMTP id a204mr25095001wmf.32.1543256900794; Mon, 26 Nov 2018 10:28:20 -0800 (PST) MIME-Version: 1.0 References: <62188c62f6dda49ca2e20629ee8e5a62a6c0b500.1543200841.git.jpoimboe@redhat.com> <20181126160217.GR2113@hirez.programming.kicks-ass.net> <20181126171036.chcbmb35ygpxziub@treble> In-Reply-To: <20181126171036.chcbmb35ygpxziub@treble> From: Andy Lutomirski Date: Mon, 26 Nov 2018 10:28:08 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v2 4/4] x86/static_call: Add inline static call implementation for x86-64 To: Josh Poimboeuf Cc: Peter Zijlstra , X86 ML , LKML , Ard Biesheuvel , Andrew Lutomirski , 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" Content-Type: text/plain; charset="UTF-8" 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 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?