Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp5602887rwb; Tue, 22 Nov 2022 02:15:21 -0800 (PST) X-Google-Smtp-Source: AA0mqf7FhjQPhISWgQqW41xwoigjqdsrTQJkSH0BD6JnLBpHmldlowT0b/Uvddpqwzr3424zKbQ0 X-Received: by 2002:a05:6402:240c:b0:462:2c1c:8791 with SMTP id t12-20020a056402240c00b004622c1c8791mr6688259eda.29.1669112121282; Tue, 22 Nov 2022 02:15:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669112121; cv=none; d=google.com; s=arc-20160816; b=ynA0Bn/leZdYoEiMYZHvsIvIToosOjE2ZFNEyesPQnzcHmE4goe74q0b9VmUyJ02Eu BPnKFYx3qIHNzcspdr+ay0+2RT5K05SaaPeHnj3yj2hvx/qfQkhjO6EGgZWgq2DTjIxE RuAgnU/VVi2++XIhLP/u9u4FiMjIKUPM5mQdA9cLURqLTLm2O5sWTtVGkqde+M4S2dLz WT7xFPO1sVxkOkrhJugV6LX5Q73Dj5eLZY1LE2nd2xN73585UGrz74+oilT3X+VwbnyD Y72w3LZcuF0nKE5MuewOFQtwixy37mBFzVRtnbLOqkxlIEq+xYykmYNH5adE3lzlBSpN 5ZXQ== 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=RrMdEjR9PuoPL1f65E/LRwRLMAkjVdgbOUDXvYazWFU=; b=t0cjOatgntHK8itRAYG2D2MxDsWv6b85QvrpRIf36DnFhQe3Fb8Yj/kiOt2LgKPKDj EpQqM+aM5+L/Kld/Aem6Qy6FQlMHdz6Tle885gX3vwxpknfhOaj7CrFg06pXb9ApXqmv 2AydeulbWayFTBveuM5IaoLNIcVK8sogCfRNr21eikzerHK93Cun8J7fElKeq7UcoEqp dm2l84cOZOZoIiA3xYmjw/uPqYZ/LZJVvYoEZew3/9LJDg00icO9kyEsCA8vu27iCcSm zy6zbwn2Q47rnKYXj85cFg9CCGMsMOdx+S5aRxFKHBqBHvly2Npn3Wn4+pXIuVyuK4R5 fCTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=LJUev07A; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qw34-20020a1709066a2200b007b5ce4a436csi2029994ejc.227.2022.11.22.02.14.58; Tue, 22 Nov 2022 02:15: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=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=LJUev07A; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232767AbiKVIzE (ORCPT + 91 others); Tue, 22 Nov 2022 03:55:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231993AbiKVIzB (ORCPT ); Tue, 22 Nov 2022 03:55:01 -0500 Received: from mail-vs1-xe2d.google.com (mail-vs1-xe2d.google.com [IPv6:2607:f8b0:4864:20::e2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2AD4229C88 for ; Tue, 22 Nov 2022 00:55:00 -0800 (PST) Received: by mail-vs1-xe2d.google.com with SMTP id l190so13778597vsc.10 for ; Tue, 22 Nov 2022 00:55:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=RrMdEjR9PuoPL1f65E/LRwRLMAkjVdgbOUDXvYazWFU=; b=LJUev07An3NNdKCBOFHeCbJiP7pQToVmFdX9LB4Y1C6wc0FAcbAqzHDSAAMsF3URy9 YhBB3AVtqS0aHcIZnnJBYjIXYONPd3RTYlFBlGuchc0G3nMARFPHTbkYbz8hj3x4xBv9 nSCWtaLx7n4vqDZz6HM0Lk1iTzqPeogGvuAaH37I4QU61eGbszg2Ocesn17EBXVIDAPT DwXXlv/QBwj6dUGYxxT3nqWSRsxH6ThLTQrNPcjjftxCaplJTb3rIcdureubEebwAQDh JqlKPC8ZCTC9Q+2joEkLCUGGjNLHfdgnLqjzEdi458BRfpmpDP2e8C8AafnmCbpEvfYq G06g== 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=RrMdEjR9PuoPL1f65E/LRwRLMAkjVdgbOUDXvYazWFU=; b=z4Yu22qfQgqohnVnuyj0zkyWj5OO97LoeGy4BpJ7N4W2NSdrUDJCyRV5kze+kJMlCs ZU0n44ZYu51Dx94ygRupH2fLC+IEuQYOHLmQSX352W3UcJgef0doGDIYWazQIYrkIQcK yzLZh1CPT1yiw9L2EtuZxsgOmpSA29AGChtSOfO7vhAPkdKeALFblf4GgA6/shBxyCie b06HbA6ttFKXQUKYJNQcVj9P5VTjWQaPklpQ3lIDnUt7xKrwxj+9jxwhONoGRk1gTDMI YIEmY0Um6KTpSLY8LpOcBREZCXuh7qr4ZEsoHBdDOnYHcOSlwCeaFuL84B3Xos1VHgdc Grhg== X-Gm-Message-State: ANoB5pnhqpS0nbxd2yBBOeoKkdeiLH1RjmqmPE5YT09GOHSptITMKMAO uVJJHxx5f02Qo8YZBxaxIi3S8ZtAm9rEmjy1hFXvXw== X-Received: by 2002:a67:fc83:0:b0:3b0:5e50:44ff with SMTP id x3-20020a67fc83000000b003b05e5044ffmr1485975vsp.4.1669107299311; Tue, 22 Nov 2022 00:54:59 -0800 (PST) MIME-Version: 1.0 References: <20221121133303.1782246-1-alexghiti@rivosinc.com> In-Reply-To: From: Alexandre Ghiti Date: Tue, 22 Nov 2022 09:54:48 +0100 Message-ID: Subject: Re: [PATCH] riscv: Sync efi page table's kernel mappings before switching To: Conor Dooley Cc: Ard Biesheuvel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Heinrich Schuchardt , Emil Renner Berthing , linux-efi@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Atish Kumar Patra Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 Conor, On Tue, Nov 22, 2022 at 9:48 AM Conor Dooley wrote: > > On Mon, Nov 21, 2022 at 02:33:03PM +0100, 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 > > Hey Alex, > What commit does this fix? You're right, I should have added this and +cc Atish: Fixes: b91540d52a08 ("RISC-V: Add EFI runtime services") Thanks, Alex > > > > --- > > 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 > >