Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752091AbeADCc4 (ORCPT + 1 other); Wed, 3 Jan 2018 21:32:56 -0500 Received: from www.llwyncelyn.cymru ([82.70.14.225]:42600 "EHLO fuzix.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751991AbeADCcz (ORCPT ); Wed, 3 Jan 2018 21:32:55 -0500 Date: Thu, 4 Jan 2018 02:32:32 +0000 From: Alan Cox To: Brian Gerst Cc: Andi Kleen , Thomas Gleixner , Linus Torvalds , gregkh@linux-foundation.org, Linux Kernel Mailing List , Tim Chen , Dave Hansen , David Woodhouse , Andi Kleen Subject: Re: [PATCH v2 01/12] x86/retpoline: Define retpoline indirect thunk and macros Message-ID: <20180104023232.0460184b@alans-desktop> In-Reply-To: References: <20180104020019.1173-1-andi@firstfloor.org> <20180104020019.1173-2-andi@firstfloor.org> Organization: Intel Corporation X-Mailer: Claws Mail 3.15.1-dirty (GTK+ 2.24.31; x86_64-redhat-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Return-Path: > > +ENTRY(__x86.indirect_thunk) > > + CFI_STARTPROC > > + call retpoline_call_target > > +2: > > + lfence /* stop speculation */ > > + jmp 2b > > +retpoline_call_target: > > +#ifdef CONFIG_64BIT > > + lea 8(%rsp), %rsp > > +#else > > + lea 4(%esp), %esp > > +#endif > > + ret > > + CFI_ENDPROC > > +ENDPROC(__x86.indirect_thunk) > > + > > + EXPORT_SYMBOL(__x86.indirect_thunk) > > -- > > 2.14.3 > > > > Can someone actually explain WTF this mess is trying to accomplish? Think of it as an 'indirect call that doesn't speculate' instruction. There isn't one in the processor but this specific sequence happens to make the micro-architecture do just that as efficiently as possible. What it's actually doing on the non-speculated path (ie the reachable code) is to call, put the address we want to hit over the existing return address and then return, to the address we want to indirectly go to. It's faster than doing a far branch or flushing branch predictors and the like. Alan