Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp7706448rwb; Wed, 23 Nov 2022 09:40:21 -0800 (PST) X-Google-Smtp-Source: AA0mqf7+oGf8qfLnQABwo5J073u93EDQHp7N3MyJeSYyIUzhG7bIx0H9Lc9o6pSTAOreKxhG5dEM X-Received: by 2002:aa7:d998:0:b0:463:a4a7:d3f1 with SMTP id u24-20020aa7d998000000b00463a4a7d3f1mr18418406eds.299.1669225221727; Wed, 23 Nov 2022 09:40:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669225221; cv=none; d=google.com; s=arc-20160816; b=OVuj4cCu9OdczyaBxQ2thLa6vOzaETl++ZwULm+Y6zLkodubwPTCuF6956bIWA86iT 3hfEY30wv4RrJWruxDnKXoNHE1Z6OSbEqeVfVHW8jcJGBSk6HZAuFvdPqEKlo8JxfSm/ +RrJO6HbzQDHNgX1mqZ3VErnsd3Btf+ypER4iYUerlL4ss92CaMsgM0Qsa4IZrmOuJ6b v86Ja7G2fYREs/B6fM6ngrIyJGCrZw9TIozhgrEDIZXGfWynXnTdS0yKN2yGmU/IYbI4 5vFIhMYEw5F6maXNpPLuETioaIEUaVUYwr0iSplCzMfDq0zIqOB1TyB44hhRlscTXddg 5//g== 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=h7C2hHcimmZ7pO9zs58j39X1ZC+4+l5O+MNwfXCa5Gs=; b=xw1KgerrLwvGgGK6QsrXy9buSWKPEAianrMNta6WIuwL1UQ0GL4oTuy3mjPa83w4nw Jw5MBhGp8XvtITfKrgQOptYHUJmIDeiaURi4Kck7hZsYYabXfCrtGsJRwTldDyhbojan Gq4+JntyOlQNU6FbcE8jFKt1hOtmE9B9cQaAv5BwZWIZ5X/DNNof/wiSf5k6WjojYQgJ VlSAh1r1Mct1rAHU8PSHm3vGvSDTWako8e+5musp0oN1JdsaFBTulEurlLbAVxsfv3lj ELBZoJvHaBK86dz8QJcmXkqB1NaOw/5fFK4rzBH20U3BgRnh9JGMD4oPYL1fe5S0wEFx SHyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=m1Jzt3iV; 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=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c16-20020a05640227d000b0045a16f47a9fsi15569908ede.624.2022.11.23.09.39.57; Wed, 23 Nov 2022 09:40:21 -0800 (PST) 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; dkim=pass header.i=@canonical.com header.s=20210705 header.b=m1Jzt3iV; 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=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237779AbiKWRTw (ORCPT + 88 others); Wed, 23 Nov 2022 12:19:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41618 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236429AbiKWRTv (ORCPT ); Wed, 23 Nov 2022 12:19:51 -0500 Received: from smtp-relay-internal-0.canonical.com (smtp-relay-internal-0.canonical.com [185.125.188.122]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1A039595 for ; Wed, 23 Nov 2022 09:19:49 -0800 (PST) Received: from mail-yb1-f200.google.com (mail-yb1-f200.google.com [209.85.219.200]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id 9D6223F164 for ; Wed, 23 Nov 2022 17:19:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1669223987; bh=h7C2hHcimmZ7pO9zs58j39X1ZC+4+l5O+MNwfXCa5Gs=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=m1Jzt3iV7i9ThJbTkTDRpkIMEaZhhvUB1fHh9Ne5v7jxhsHrY1bg8arSC6AenLxP/ HBrx16lf9WM5GyUwaT9ZiwLo4+GiDacrcZ3ux19ohqldKmw91/qVcWZCBu2W6bHG9I bMT+cDcUyHJ4jRNqNbnd05J3+EGc4JC90+Dm5+zrYPaR3A8rHYjjLxuO3tYCDLZ/5C rYC9mCY8goWHkH8oiI7LvK0cFFCEuRklt8r2Bwf6ibQUqPTMiKHaVi0rPRryouo6zu y5UU+SU0FpxODp2+aHw6PoRBPMkBKkz+UY1ROd6CR4Sr/O/yFUt/VuQwQhbxPyYvzt xEipl9dOrgwjw== Received: by mail-yb1-f200.google.com with SMTP id a5-20020a25af05000000b006e450a5e507so16934686ybh.22 for ; Wed, 23 Nov 2022 09:19:47 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=h7C2hHcimmZ7pO9zs58j39X1ZC+4+l5O+MNwfXCa5Gs=; b=Y4W3jUI14jI3q72wScZ1pLIaSd2iu3F0kKH2Y9YtjRzjufOIB7+fpFjqBERRg9o8sS nNTRTNuVzuTl2io1pTsU6F2BJd25E5hGR2NzPrV//iZjVdFMmNspF8zwFWLH5pJ/qR1j ek7YwrEtE/yWKZLC6HMOaYm07OZVB8MT4/s2hlrSrwAUuFVJtMfCrdKjGzvnYk40GkeY SPZvsqwfy/kUwqOIJeEaMwLUWoQnOyBkD0HRZV2DfrbDsHJViaG0+ekiZ0f1Ha4xkBdW K1YMFo3O8lYZCytcCIFjnDmG6OvscK6oLOX5IeBwCLzF8yLu+3PSc1KqYvjhseWpMjPC TaqA== X-Gm-Message-State: ANoB5pkLxNy2KDm5gmMnAppxg7hNdULO1y2kLc25RE1QTs2qM33ACwEj o8xL6dx3B0y89VIKHn8slCmgYoruOsTPk5+llWzp8DNoAjSePr6yRgNvpDbTQVLNMt+Amfhb4fX R+hPRIq4+g69Xn+yFM6eTqKUKcEAF5BCgIUr5HK1h0Z/mhKUmnApsWpVvTQ== X-Received: by 2002:a0d:e696:0:b0:3b1:23de:2025 with SMTP id p144-20020a0de696000000b003b123de2025mr2631658ywe.230.1669223986618; Wed, 23 Nov 2022 09:19:46 -0800 (PST) X-Received: by 2002:a0d:e696:0:b0:3b1:23de:2025 with SMTP id p144-20020a0de696000000b003b123de2025mr2631631ywe.230.1669223986399; Wed, 23 Nov 2022 09:19:46 -0800 (PST) MIME-Version: 1.0 References: <20221121133303.1782246-1-alexghiti@rivosinc.com> In-Reply-To: <20221121133303.1782246-1-alexghiti@rivosinc.com> From: Emil Renner Berthing Date: Wed, 23 Nov 2022 18:19:29 +0100 Message-ID: Subject: Re: [PATCH] riscv: Sync efi page table's kernel mappings before switching To: Alexandre Ghiti Cc: Ard Biesheuvel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Heinrich Schuchardt , linux-efi@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS 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 Hi Alexandre, On Mon, 21 Nov 2022 at 14:33, Alexandre Ghiti wrote: > > The EFI page table is initially created as a copy of the kernel page table. > With VMAP_STACK enabled, kernel stacks are allocated in the vmalloc area: > if the stack is allocated in a new PGD (one that was not present at the > moment of the efi page table creation or not synced in a previous vmalloc > fault), the kernel will take a trap when switching to the efi page table > when the vmalloc kernel stack is accessed, resulting in a kernel panic. > > Fix that by updating the efi kernel mappings before switching to the efi > page table. > > Signed-off-by: Alexandre Ghiti Thanks for the patch! With this applied on the Ubuntu 5.19 kernel I can enable CONFIG_VMAP_STACK and cat /sys/firmware/efi/efivars/* on the Unmatched without locking up. So Tested-by: Emil Renner Berthing > --- > arch/riscv/include/asm/efi.h | 6 +++++- > arch/riscv/include/asm/pgalloc.h | 11 ++++++++--- > 2 files changed, 13 insertions(+), 4 deletions(-) > > diff --git a/arch/riscv/include/asm/efi.h b/arch/riscv/include/asm/efi.h > index f74879a8f1ea..e229d7be4b66 100644 > --- a/arch/riscv/include/asm/efi.h > +++ b/arch/riscv/include/asm/efi.h > @@ -10,6 +10,7 @@ > #include > #include > #include > +#include > > #ifdef CONFIG_EFI > extern void efi_init(void); > @@ -20,7 +21,10 @@ extern void efi_init(void); > int efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md); > int efi_set_mapping_permissions(struct mm_struct *mm, efi_memory_desc_t *md); > > -#define arch_efi_call_virt_setup() efi_virtmap_load() > +#define arch_efi_call_virt_setup() ({ \ > + sync_kernel_mappings(efi_mm.pgd); \ > + efi_virtmap_load(); \ > + }) > #define arch_efi_call_virt_teardown() efi_virtmap_unload() > > #define ARCH_EFI_IRQ_FLAGS_MASK (SR_IE | SR_SPIE) > diff --git a/arch/riscv/include/asm/pgalloc.h b/arch/riscv/include/asm/pgalloc.h > index 947f23d7b6af..59dc12b5b7e8 100644 > --- a/arch/riscv/include/asm/pgalloc.h > +++ b/arch/riscv/include/asm/pgalloc.h > @@ -127,6 +127,13 @@ static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d) > #define __p4d_free_tlb(tlb, p4d, addr) p4d_free((tlb)->mm, p4d) > #endif /* __PAGETABLE_PMD_FOLDED */ > > +static inline void sync_kernel_mappings(pgd_t *pgd) > +{ > + memcpy(pgd + USER_PTRS_PER_PGD, > + init_mm.pgd + USER_PTRS_PER_PGD, > + (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t)); > +} > + > static inline pgd_t *pgd_alloc(struct mm_struct *mm) > { > pgd_t *pgd; > @@ -135,9 +142,7 @@ static inline pgd_t *pgd_alloc(struct mm_struct *mm) > if (likely(pgd != NULL)) { > memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t)); > /* Copy kernel mappings */ > - memcpy(pgd + USER_PTRS_PER_PGD, > - init_mm.pgd + USER_PTRS_PER_PGD, > - (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t)); > + sync_kernel_mappings(pgd); > } > return pgd; > } > -- > 2.37.2 >