Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp267918pxb; Mon, 25 Oct 2021 07:59:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzbwxLYXFHkfNiSniIRc3JqLY+v8qYJzicwAj2J/Y7mjB3BKoGY6P4Ur1cOAE2jEgizA+Gc X-Received: by 2002:a05:6402:40c2:: with SMTP id z2mr27729111edb.41.1635173970853; Mon, 25 Oct 2021 07:59:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635173970; cv=none; d=google.com; s=arc-20160816; b=tBDrBUcZCsqg+pon9Z+hE8Fi9A/X8QLA277aiesmH/Z/IleTebFgd1vS7wBFRA5FyW C7xyuWN1AalN9L3hG0dADrjOQJWDuhvVzsUVDhn3vqaJjFis5dbcoXkH7ArEmW86QE1/ SJTg/XVf5nc7ASGPe3+rDRSbkzJ3PsY9yDH85TBUCdxXQtZVtIEYMLtH+HpHzdZBX67+ sV/Y8Q584wC4yI8kh8ZOlkzKOB4zgixoFN/Wi7gw+jZPbAGqsq3v4wV6Aoz2WFERUU0o cG+Gq1145yA3MbLx/yDFocCXnOvyBC2AjdEPPZZVEB4Gbam4QqAHCSRQEIn9u95hCtU4 8qGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=ovyfqdjL6cj92q50AHqAsSFgq/nIO4sUBQ6CCpabKD8=; b=04NgUb5OcriPT+Gqd4szKrY4uiCFn/d1VCqxDR+hNcoDLfbHSjceY0wikpFfcy+PpL g538oRhSwdv793fnR/VDqhEMR+v6qJ21TXSsAqPVpL8/yjBkqcYwuC9GFQhQ6Ato+b6H fbrUKG2/AqekJQjYBQ01ADzSm8jQU5V/3RnVeum/0EVizMzO6S/u/hQjDSyP6XIQSKIG GDwt9vQgwjx09bMyZXKgC6f0nloUcA27QN16m1isBclK5+kTaPKunuU8XwyqRjk5E0Iq kgi4y8naRGtxfUSIjM0bpvWM1dbBvzK9yUm0Mi0NLkC/A2k5mNZOxvdgp1i7rgl13XqF bi5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iuGLcz8Q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id dn8si21913585ejc.33.2021.10.25.07.59.06; Mon, 25 Oct 2021 07:59:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iuGLcz8Q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S233653AbhJYO6g (ORCPT + 99 others); Mon, 25 Oct 2021 10:58:36 -0400 Received: from mail.kernel.org ([198.145.29.99]:46848 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230268AbhJYO5v (ORCPT ); Mon, 25 Oct 2021 10:57:51 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 6FEE86103B for ; Mon, 25 Oct 2021 14:55:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1635173729; bh=v35gq3+4P5DC3vkeKUNcLSgbcP1wFHUYLLL8LD21IOU=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=iuGLcz8Qf/8bLTEEuXVEmlYjIpdwPuaiJs8j+Azg+FmWX0t6E+GuEyX2PTTTiDb/M bVRaadAV5u6/vEbwO2kahshnZSyRXr0i+Mi2BSSJJOXUu210DVXlfpK2aQxeQyfXLJ 5sCl+x2rmom0fSdDOHlMhhoNJtbhlCXHTFpL+KRcKFHBsLsYXThWNiaDoT7X8hcAOZ JoZn8B2EBOcpKZk4FIE2E73unRdaonJqBghn8kbD+wvj0Y45yg7xw8433+fJAk+4KF 5Kp2SBqzCNBhu4y49NBj8tvGQRkwLL0A8QYnfPrMhFSzChZ/xgNP/RZ/kTCQABsQuR th9Jx6Qa+MFvQ== Received: by mail-oi1-f181.google.com with SMTP id r6so15934169oiw.2 for ; Mon, 25 Oct 2021 07:55:29 -0700 (PDT) X-Gm-Message-State: AOAM532C4Kk8styEYqmsCS5NNp7EphUaO4ivHW3Egn5v30svWGbIsS9i 5u8RQaN9xBjvAx/4qk+IFnjsOkw+FIpzHBrjoac= X-Received: by 2002:aca:1712:: with SMTP id j18mr12645020oii.33.1635173728690; Mon, 25 Oct 2021 07:55:28 -0700 (PDT) MIME-Version: 1.0 References: <20211025122102.46089-1-frederic@kernel.org> <20211025122102.46089-3-frederic@kernel.org> In-Reply-To: From: Ard Biesheuvel Date: Mon, 25 Oct 2021 16:55:17 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 2/4] arm64: implement support for static call trampolines To: Peter Zijlstra Cc: Frederic Weisbecker , LKML , James Morse , David Laight , Quentin Perret , Catalin Marinas , Will Deacon , Mark Rutland Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 25 Oct 2021 at 16:47, Peter Zijlstra wrote: > > On Mon, Oct 25, 2021 at 04:19:16PM +0200, Peter Zijlstra wrote: > > On Mon, Oct 25, 2021 at 04:08:37PM +0200, Ard Biesheuvel wrote: > > > > > Ooohh, but what if you go from !func to NOP. > > > > > > > > assuming: > > > > > > > > .literal = 0 > > > > BTI C > > > > RET > > > > > > > > Then > > > > > > > > CPU0 CPU1 > > > > > > > > [S] literal = func [I] NOP > > > > [S] insn[1] = NOP [L] x16 = literal (NULL) > > > > b x16 > > > > *BANG* > > > > > > > > Is that possible? (total lack of memory ordering etc..) > > > > > > > > > > The CBZ will branch to the RET instruction if x16 == 0x0, so this > > > should not happen. > > > > Oooh, I missed that :/ I was about to suggest writing the address of a > > bare 'ret' trampoline instead of NULL into the literal. > > Perhaps a little something like so.. Shaves 2 instructions off each > trampoline. > > --- a/arch/arm64/include/asm/static_call.h > +++ b/arch/arm64/include/asm/static_call.h > @@ -11,9 +11,7 @@ > " hint 34 /* BTI C */ \n" \ > insn " \n" \ > " ldr x16, 0b \n" \ > - " cbz x16, 1f \n" \ > " br x16 \n" \ > - "1: ret \n" \ > " .popsection \n") > > #define ARCH_DEFINE_STATIC_CALL_TRAMP(name, func) \ > --- a/arch/arm64/kernel/patching.c > +++ b/arch/arm64/kernel/patching.c > @@ -90,6 +90,11 @@ int __kprobes aarch64_insn_write(void *a > return __aarch64_insn_write(addr, &i, AARCH64_INSN_SIZE); > } > > +asm("__static_call_ret: \n" > + " ret \n") > + This breaks BTI as it lacks the landing pad, and it will be called indirectly. > +extern void __static_call_ret(void); > + Better to have an ordinary C function here (with consistent linkage), but we need to take the address in a way that works with Clang CFI. As the two additional instructions are on an ice cold path anyway, I'm not sure this is an obvious improvement tbh. > void arch_static_call_transform(void *site, void *tramp, void *func, bool tail) > { > /* > @@ -97,9 +102,7 @@ void arch_static_call_transform(void *si > * 0x0 bti c <--- trampoline entry point > * 0x4 > * 0x8 ldr x16, > - * 0xc cbz x16, 20 > - * 0x10 br x16 > - * 0x14 ret > + * 0xc br x16 > */ > struct { > u64 literal; > @@ -113,6 +116,7 @@ void arch_static_call_transform(void *si > insns.insn[0] = cpu_to_le32(insn); > > if (!func) { > + insns.literal = (unsigned long)&__static_call_ret; > insn = aarch64_insn_gen_branch_reg(AARCH64_INSN_REG_LR, > AARCH64_INSN_BRANCH_RETURN); > } else {