From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5BA8DC433DF for ; Wed, 1 Jul 2020 17:32:24 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2910E20771 for ; Wed, 1 Jul 2020 17:32:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="JySWGk/y"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="hfHDlOoD" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2910E20771 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:To:Subject:Message-ID:Date:From:In-Reply-To: References:MIME-Version:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=9HiUJa2oyjYEis2PADYthXypTPRnrAPXoKhSuDbBn8M=; b=JySWGk/yakHUd6onT+DAqvf0N wreOLo9UuAYtQ8IDZ6XXVow0WQh2elvcYEyEQ8BCuqaIz+xbPynTFlUxzUh5f03MaTuRhxUhu82qd 1b6BAzpqgvwD6fMSf0RcD3IqRK1tizJ1f3EZqBKncB1KWZrCCMU2fdEBezQ6/zWYouoBt4XWxDsyR ir1Y/aq0MRkaOw/srZKewnV+068tc3bUruREkgxEh/Xv5VvkQYwwCrZ0wRB7SW4oicbLU7JdT+qko 7AENSECLbh+TeVqB15L8DSohpz4fVMB2PkM4/ISbQ0RxvXz1ruRrQsYinwCitGtQyeSWxr+VzVfHz X2NwF+Wow==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1jqgZT-0000Pj-Jn; Wed, 01 Jul 2020 17:30:55 +0000 Received: from mail.kernel.org ([198.145.29.99]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1jqgZQ-0000P8-Je for linux-arm-kernel@lists.infradead.org; Wed, 01 Jul 2020 17:30:53 +0000 Received: from mail-ot1-f48.google.com (mail-ot1-f48.google.com [209.85.210.48]) (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 BF6B32078B for ; Wed, 1 Jul 2020 17:30:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1593624651; bh=5c0XWeitYVYB6ecYKcNrvWMZy/YWZwF678lv5v6DCXc=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=hfHDlOoDjZiVxWDeayyCeEB250Fq9pqZOar7HViX4mwy2FHJE0reRsYotThKur1nF jBQIHXxyQ7o9BkABLM6afgZ6We66XW6osgVTK7FMPoA7CtQZNOoH5gig0frUtpLfNo s5ob5mlD4cx0/FSMcolV2V2OlMrBBQb3gyCu12l0= Received: by mail-ot1-f48.google.com with SMTP id h13so1448861otr.0 for ; Wed, 01 Jul 2020 10:30:51 -0700 (PDT) X-Gm-Message-State: AOAM532IarCv4EqQ/BGluuIVXpDNhKUUsW+0slnrDuw0E9DdYOLTiGD5 OWvgt2tnJpUWYNEEavFKFrubhljOlVDKmb910Pk= X-Google-Smtp-Source: ABdhPJx1wcdcEr3YBj+Do6mxIQ2m5LITlpwb7rMxBKTG8ks0irrVKPE0K4k/G5l9lUtjHGCQVj+4c41oqsvHQk17Pxs= X-Received: by 2002:a9d:4a8f:: with SMTP id i15mr25019182otf.77.1593624651052; Wed, 01 Jul 2020 10:30:51 -0700 (PDT) MIME-Version: 1.0 References: <20200630081921.13443-1-ardb@kernel.org> <20200701170055.v5xufbjjkjknnkuc@bakewell.cambridge.arm.com> In-Reply-To: <20200701170055.v5xufbjjkjknnkuc@bakewell.cambridge.arm.com> From: Ard Biesheuvel Date: Wed, 1 Jul 2020 19:30:40 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] arm64/alternatives: use subsections for replacement sequences To: Dave P Martin X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200701_133052_765466_CC3B78F3 X-CRM114-Status: GOOD ( 44.72 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , Anders Roxell , Arnd Bergmann , Suzuki K Poulose , Catalin Marinas , Mark Brown , James Morse , Andre Przywara , Will Deacon , Linux ARM Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Wed, 1 Jul 2020 at 19:01, Dave P Martin wrote: > > On Tue, Jun 30, 2020 at 10:19:21AM +0200, Ard Biesheuvel wrote: > > When building very large kernels, the logic that emits replacement > > sequences for alternatives fails when relative branches are present > > in the code that is emitted into the .altinstr_replacement section > > and patched in at the original site and fixed up. The reason is that > > the linker will insert veneers if relative branches go out of range, > > and due to the relative distance of the .altinstr_replacement from > > the .text section where its branch targets usually live, veneers > > may be emitted at the end of the .altinstr_replacement section, with > > the relative branches in the sequence pointed at the veneers instead > > of the actual target. > > > > The alternatives patching logic will attempt to fix up the branch to > > point to its original target, which will be the veneer in this case, > > but given that the patch site is likely to be far away as well, it > > will be out of range and so patching will fail. There are other cases > > where these veneers are problematic, e.g., when the target of the > > branch is in .text while the patch site is in .init.text, in which > > case putting the replacement sequence inside .text may not help either. > > > > So let's use subsections to emit the replacement code as closely as > > possible to the patch site, to ensure that veneers are only likely to > > be emitted if they are required at the patch site as well, in which > > case they will be in range for the replacement sequence both before > > and after it is transported to the patch site. > > > > This will prevent alternative sequences in non-init code from being > > released from memory after boot, but this is tolerable given that the > > entire section is only 512 KB on an allyesconfig build (which weighs in > > at 500+ MB for the entire Image). Also, note that modules today carry > > the replacement sequences in non-init sections as well, and any of > > those that target init code will be emitted into init sections after > > this change. > > > > This fixes an early crash when booting an allyesconfig kernel on a > > system where any of the alternatives sequences containing relative > > branches are activated at boot (e.g., ARM64_HAS_PAN on TX2) > > > > Cc: Suzuki K Poulose > > Cc: James Morse > > Cc: Andre Przywara > > Cc: Dave P Martin > > Signed-off-by: Ard Biesheuvel > > --- > > arch/arm64/include/asm/alternative.h | 16 ++++++++-------- > > arch/arm64/kernel/vmlinux.lds.S | 3 --- > > 2 files changed, 8 insertions(+), 11 deletions(-) > > > > diff --git a/arch/arm64/include/asm/alternative.h b/arch/arm64/include/asm/alternative.h > > index 5e5dc05d63a0..12f0eb56a1cc 100644 > > --- a/arch/arm64/include/asm/alternative.h > > +++ b/arch/arm64/include/asm/alternative.h > > @@ -73,11 +73,11 @@ static inline void apply_alternatives_module(void *start, size_t length) { } > > ".pushsection .altinstructions,\"a\"\n" \ > > ALTINSTR_ENTRY(feature) \ > > ".popsection\n" \ > > - ".pushsection .altinstr_replacement, \"a\"\n" \ > > + ".subsection 1\n" \ > > This uses subsections in existing sections. Could that interfere with > existing (or future) uses of subsections? (I've not checked whether > there actually are such uses. I'm also assuming that clobbering the > invoker's idea of what section is .previous doesn't matter.) > It shouldn't matter, really. You can use different indexes for the subsection, but since the execution never flows from one subsection into the next, all that matters is that they are 'somewhere else' As for the use of .previous - the idea is that this does not affect the contents of the section stack, which I think makes sense. We could use '.pushsection .text, 1' as well to enter another subsection in .text, but that means we keep the .text vs .init.text issue that this patch solves. > Another wrinkle: the replacement code now becomes executable, whereas > I think it was previously in rodata. I'm not sure how much this > matters, but it might be a source of gadgets. > True. Perhaps we need to get rid of relative branches in alternative sequences entirely - see below. > > A different option would be to add an explicitly veneered branch macro > for use in alternatives, maybe adrp+add+br. For BTI compatility, we'd > need a bti j or equivalent at the destination, which might or might not > be easy to achieve -- mind you, I think we theoretically need that > anyway for veneers to work properly in all cases. > > Because we would define the exact instruction sequence, the > alternatives code could probably replace it with a direct branch if the > actual destination is close enough. The downside is that it wouldn't > be a single instruction any more, and there would be some overhead for > conditional branches if we replace the unneeded insns with NOPs. > Yeah, this becomes quite hairy very quickly, especially because now you need to allocate a spare register each time you do this. One other option is to simply disallow branches in the alternative sequences: I spotted three occurrences [0] that are quite easily fixed, by inverting the condition so that the relative branch is emitted in place, and the alternative sequence is just NOPs. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel