Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp4027603rwe; Mon, 17 Apr 2023 07:04:40 -0700 (PDT) X-Google-Smtp-Source: AKy350aMdv8VdtORZpd2Y5kOTwkbmwzjYa3FBngUoVd+sLvPVKGF49GFWUt02iDsI2q+AFC5u5Zz X-Received: by 2002:a17:902:c451:b0:1a4:fcc9:ec61 with SMTP id m17-20020a170902c45100b001a4fcc9ec61mr11079606plm.5.1681740280701; Mon, 17 Apr 2023 07:04:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681740280; cv=none; d=google.com; s=arc-20160816; b=ZHOQszaeSxYkxOl2LOYVVm3BfvL8Ej+sveqdEiCfXkZxuHxlKB0U8YI17N9aI2I03S m0FOfAfrVt4MCMn+rOm7qtWh2+m1T+Z8oMTqEWMJgEdABZa2LEa3GVMDqwXoKpu4HDoU CWlcGLqziMQWVe8frfYqWGPD39aUs1vx82nSmUjw1ZTo8S4X4Aw7HRyVFIM0AD2RJ73W cyH3+LgfOVCJaZSuj2hipviOzkhEMBGr+olqvUkO2qm5248FcVslUP+q+flE1OfiXXt6 Ndfa8PHepvPzxN0SevtbZa+UNhMF3PCdN/yIEOgJ0IX57S8yBTzTrA7azhucsHPN4pl9 OUJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=a7qshCm8ME5NI9vdBlnI30v82nW8SfrAnKRMxHvlIzQ=; b=kWcWdtz9C0qrnkK7cZz7CSYpN2E+tkXK2HkVrhU6EX7FHDY6zFyvX0ZRH0mdcTIOLg eBJzfnWMbkUQPZ6mqVc64gY9Fn/0ZnONhN3Wg94SM2fM7rRtMPkztES2fMu0HlWg1XVp o4QTzOYC9qTwuEnScOC4rTm0X6aeefdgUeE2KklwmOGz10ezq9Hfi8Ad6Ac6e1pZ3o0Z MVM6Oiqmf0urLnKBoQWNJLmQ8zGWb1D6KM3Q71Zfr2kltUN51qYL5ZIGM54u1gIP02ns Psx7Pl2r+Bf1zhch4KfMtbUfZwNWXC5N2ZyCqtABKWLuHYTXDD9gWQTjbq+sn1LzSMs5 JYsg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ba2-20020a170902720200b001a6d4eab483si2962852plb.49.2023.04.17.07.04.15; Mon, 17 Apr 2023 07:04:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230504AbjDQOB5 (ORCPT + 99 others); Mon, 17 Apr 2023 10:01:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52430 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230416AbjDQOBx (ORCPT ); Mon, 17 Apr 2023 10:01:53 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id DCC4E4C39 for ; Mon, 17 Apr 2023 07:00:56 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B0D011691; Mon, 17 Apr 2023 07:01:29 -0700 (PDT) Received: from [10.57.68.227] (unknown [10.57.68.227]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id C23473F5A1; Mon, 17 Apr 2023 07:00:44 -0700 (PDT) Message-ID: <37412bd5-9e73-024f-26ab-a351853bc846@arm.com> Date: Mon, 17 Apr 2023 15:00:43 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 Subject: Re: [PATCH v3 25/60] arm64: head: Clear BSS and the kernel page tables in one go Content-Language: en-US To: Ard Biesheuvel , linux-kernel@vger.kernel.org Cc: linux-arm-kernel@lists.infradead.org, Catalin Marinas , Will Deacon , Marc Zyngier , Mark Rutland , Anshuman Khandual , Kees Cook References: <20230307140522.2311461-1-ardb@kernel.org> <20230307140522.2311461-26-ardb@kernel.org> From: Ryan Roberts In-Reply-To: <20230307140522.2311461-26-ardb@kernel.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-6.5 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 07/03/2023 14:04, Ard Biesheuvel wrote: > We will move the CPU feature overrides into BSS in a subsequent patch, > and this requires that BSS is zeroed before the feature override > detection code runs. So let's map BSS read-write in the ID map, and zero > it via this mapping. > > Since the kernel page tables are right next to it, and also zeroed via > the ID map, let's drop the separate clear_page_tables() function, and > just zero everything in one go. > > Signed-off-by: Ard Biesheuvel > --- > arch/arm64/kernel/head.S | 33 +++++++------------- > 1 file changed, 11 insertions(+), 22 deletions(-) > > diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S > index 0fa44b3188c1e204..ade0cb99c8a83a3d 100644 > --- a/arch/arm64/kernel/head.S > +++ b/arch/arm64/kernel/head.S > @@ -177,17 +177,6 @@ SYM_CODE_START_LOCAL(preserve_boot_args) > ret > SYM_CODE_END(preserve_boot_args) > > -SYM_FUNC_START_LOCAL(clear_page_tables) > - /* > - * Clear the init page tables. > - */ > - adrp x0, init_pg_dir > - adrp x1, init_pg_end > - sub x2, x1, x0 > - mov x1, xzr > - b __pi_memset // tail call > -SYM_FUNC_END(clear_page_tables) > - > /* > * Macro to populate page table entries, these entries can be pointers to the next level > * or last level entries pointing to physical memory. > @@ -386,9 +375,9 @@ SYM_FUNC_START_LOCAL(create_idmap) > > map_memory x0, x1, x3, x6, x7, x3, IDMAP_PGD_ORDER, x10, x11, x12, x13, x14, EXTRA_SHIFT > > - /* Remap the kernel page tables r/w in the ID map */ > + /* Remap BSS and the kernel page tables r/w in the ID map */ > adrp x1, _text > - adrp x2, init_pg_dir > + adrp x2, __bss_start > adrp x3, _end > bic x4, x2, #SWAPPER_BLOCK_SIZE - 1 > mov x5, SWAPPER_RW_MMUFLAGS > @@ -489,14 +478,6 @@ SYM_FUNC_START_LOCAL(__primary_switched) > mov x0, x20 > bl set_cpu_boot_mode_flag > > - // Clear BSS > - adr_l x0, __bss_start > - mov x1, xzr > - adr_l x2, __bss_stop > - sub x2, x2, x0 > - bl __pi_memset > - dsb ishst // Make zero page visible to PTW > - > #if VA_BITS > 48 > adr_l x8, vabits_actual // Set this early so KASAN early init > str x25, [x8] // ... observes the correct value > @@ -780,6 +761,15 @@ SYM_FUNC_START_LOCAL(__primary_switch) > adrp x1, reserved_pg_dir > adrp x2, init_idmap_pg_dir > bl __enable_mmu > + > + // Clear BSS > + adrp x0, __bss_start > + mov x1, xzr > + adrp x2, init_pg_end > + sub x2, x2, x0 > + bl __pi_memset > + dsb ishst // Make zero page visible to PTW Is it possible to add an assert somewhere (or at the very least a comment in vmlinux.lds.S) to ensure that nothing gets inserted between the BSS and the page tables? It feels a bit fragile otherwise. I also wonder what's the point in calling __pi_memset() from here? Why not just do it all in C? > + > #ifdef CONFIG_RELOCATABLE > adrp x23, KERNEL_START > and x23, x23, MIN_KIMG_ALIGN - 1 > @@ -794,7 +784,6 @@ SYM_FUNC_START_LOCAL(__primary_switch) > orr x23, x23, x0 // record kernel offset > #endif > #endif > - bl clear_page_tables > bl create_kernel_mapping > > adrp x1, init_pg_dir