Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp5141348ybv; Mon, 17 Feb 2020 13:11:43 -0800 (PST) X-Google-Smtp-Source: APXvYqx2sChxM9G6eSy21qjwZFFCA2cuDJjeza5519V0ERFQoXYkV3U6lxhDq12zD5L2X/Qk0N8F X-Received: by 2002:a9d:7e90:: with SMTP id m16mr12728357otp.227.1581973903422; Mon, 17 Feb 2020 13:11:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581973903; cv=none; d=google.com; s=arc-20160816; b=abOk3JqekFzxrPnIt28RSKdFN+XgIubnu6ojluQ34bdmjiBZjFubvYLsBVCHN08WUg dGUhjaaO1v9bvZBjD3BqoIZ/oyyawo5URpazkgWnjgdvA/ThGWMV0BcB+XCnhYZpV/df AfpklMRjb3PBXKOZUWs5h3NxX/M6Hpp/gj/CriYVclNQh+02lX6CmwtOznoSnqhNDO79 g1dziq6Wubl2pXBytGciV5BYSonwW8p+Fmr9JIvk7angz/EHHRmz7y31zdk0gDJB9n3Q sGWUgh13Pj0e/ofZiZpGSjrdU3qvZabWniPXMWlMxDpmDd9+UkYesD1ekcyIctiNeIJE 4RbQ== 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=cxGKMOcaVmtYBJsQFMG9XcBq+4HK9qOJedimYVGECnc=; b=OTrOaLADDoA7pxgEIgNmsmK4BcydthaRwNjuOR271ctVc67mEUD5ox5y7x2gxlXHRV i4s5FyhC0bXOZxnM8fKEWdPmC6t1ttnJSJvqEbUItLrS24lBIOMkbw8aI7aSHxDDM/vQ O5DUclXQBPjsTwTcozfjqpVEKUjjJeqqNWP8+MfbNRAF1VsvisCQjlb0Hb91I+9+6jbu 0OGRBr6cjl/msSJUTBJGlz68Mey4AsdQge939LxQrLs4UqI6mKwV4TqWxxTMuUe/kmNN WgRE6qdFiku7WUv43NerGHxRirdO1pcmxnaZTud/1dBRrVdojvLXhp84EHF5LrDvEe/F URIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="cAcbX/to"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k202si6747818oih.244.2020.02.17.13.11.30; Mon, 17 Feb 2020 13:11:43 -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=@google.com header.s=20161025 header.b="cAcbX/to"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729450AbgBQVK4 (ORCPT + 99 others); Mon, 17 Feb 2020 16:10:56 -0500 Received: from mail-ot1-f67.google.com ([209.85.210.67]:34331 "EHLO mail-ot1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728444AbgBQVK4 (ORCPT ); Mon, 17 Feb 2020 16:10:56 -0500 Received: by mail-ot1-f67.google.com with SMTP id j16so17477578otl.1 for ; Mon, 17 Feb 2020 13:10:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=cxGKMOcaVmtYBJsQFMG9XcBq+4HK9qOJedimYVGECnc=; b=cAcbX/to2FsZm371gcOorgLW4wUJPwgVYzE0wQo+/g8JAurh+6E0dPlKifHY+UkgWI H3znjGnqFcQMKH+W1FmGWAKQbOvQaLT/C+eCvutEx4ZUjS0QrdoCxPlsmTesaJWsZZA5 GeVkfvfNZ0SZM1Oi9Mfs0qRfUpZqAdWpHAk2nv3aGMwRQjQibAFixLQifDYm2M/LLRR0 SJTQn19ImBGpJP0SJwm9Plp61F5l01agUrX9eIywel4w4E+CPycAM3X4lG1qSh2t+t86 Mu5/DKJPAKBdaGm6n8U2nkzSPF8m41vR9gEMSykb4VoRuv37Swm1EL17PWdEMKUv6ncn 2UQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=cxGKMOcaVmtYBJsQFMG9XcBq+4HK9qOJedimYVGECnc=; b=CTufYu4DCo0FWxWHIdZuN0nt8HVOBo5X9sQyCm6aXyXhYge/NnMJ7kNiDDRnneUC1G FBXuxYZAnmKZWTM4rFSGJS7yxtddq/UpUqOzSjjB2sLIWliMggJkPibFuiDP6QAX0UQO L2KHwuLten/Zm5CD09uVmGIvx7CeojFYRMWA5gRq0uxvOP3tsKvXDPWKActbWoih5B61 J3whOQhx9isbKEYesO++A30CFadmf7zqQmC6gNS8qLi2Y48JI8h5Tw1x70DtxHcPrQi9 kmW1G+IEeU14NYBovZwQskODaMnUZfPMkAK9MCBgafO3+Ra6GMZoCeNLtGM/JwCir+k+ 3iTA== X-Gm-Message-State: APjAAAXCAkub+hUAfg/M3IlBN5gqmcNq6rBj6WAqYpiZ3JYQHfXvAkeu dNhrp/utInnPzG8pGrd4jitdJxmmLxDiIO4kS6oEag== X-Received: by 2002:a05:6830:22cc:: with SMTP id q12mr13880885otc.110.1581973853761; Mon, 17 Feb 2020 13:10:53 -0800 (PST) MIME-Version: 1.0 References: <20190110203023.GL2861@worktop.programming.kicks-ass.net> <20190110205226.iburt6mrddsxnjpk@treble> In-Reply-To: <20190110205226.iburt6mrddsxnjpk@treble> From: Jann Horn Date: Mon, 17 Feb 2020 22:10:27 +0100 Message-ID: Subject: Re: [PATCH v3 0/6] Static calls To: Josh Poimboeuf Cc: Peter Zijlstra , "the arch/x86 maintainers" , kernel list , Ard Biesheuvel , Andy Lutomirski , Steven Rostedt , Ingo Molnar , Thomas Gleixner , Linus Torvalds , Masami Hiramatsu , Jason Baron , Jiri Kosina , David Laight , Borislav Petkov , Julia Cartwright , Jessica Yu , "H. Peter Anvin" , Nadav Amit , Rasmus Villemoes , Edward Cree , Daniel Bristot de Oliveira 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 Thu, Jan 10, 2019 at 9:52 PM Josh Poimboeuf wrote: > On Thu, Jan 10, 2019 at 09:30:23PM +0100, Peter Zijlstra wrote: > > On Wed, Jan 09, 2019 at 04:59:35PM -0600, Josh Poimboeuf wrote: > > > With this version, I stopped trying to use text_poke_bp(), and instead > > > went with a different approach: if the call site destination doesn't > > > cross a cacheline boundary, just do an atomic write. Otherwise, keep > > > using the trampoline indefinitely. > > > > > - Get rid of the use of text_poke_bp(), in favor of atomic writes. > > > Out-of-line calls will be promoted to inline only if the call sites > > > don't cross cache line boundaries. [Linus/Andy] > > > > Can we perserve why text_poke_bp() didn't work? I seem to have forgotten > > again. The problem was poking the return address onto the stack from the > > int3 handler, or something along those lines? > > Right, emulating a call instruction from the #BP handler is ugly, > because you have to somehow grow the stack to make room for the return > address. Personally I liked the idea of shifting the iret frame by 16 > bytes in the #DB entry code, but others hated it. > > So many bad-but-not-completely-unacceptable options to choose from. Silly suggestion from someone who has skimmed the thread: Wouldn't a retpoline-style trampoline solve this without needing memory allocations? Let the interrupt handler stash the destination in a percpu variable and clear IF in regs->flags. Something like: void simulate_call(unsigned long target) { __this_cpu_write(static_call_restore_if, (regs->flags & X86_EFLAGS_IF) != 0); regs->flags &= ~X86_EFLAGS_IF; __this_cpu_write(static_call_trampoline_source, regs->ip + 5); __this_cpu_write(static_call_trampoline_target, target); regs->ip = magic_static_call_trampoline; } magic_static_call_trampoline: ; set up return address for returning from target function pushl PER_CPU_VAR(static_call_trampoline_source) ; set up retpoline-style return address pushl PER_CPU_VAR(static_call_trampoline_target) ; restore flags if needed cmp PER_CPU_VAR(static_call_restore_if), 0 je 1f sti ; NOTE: percpu data must not be accessed past this point 1: ret ; "return" to the call target By using a return to implement the call, we don't need any scratch registers for the call.