Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp4262997rwb; Mon, 21 Nov 2022 05:47:26 -0800 (PST) X-Google-Smtp-Source: AA0mqf6bksUt5xP/tONAPYMzqsnTHYIl4zp/YEkKk+4OTlZ5HqhBSkWuaJgJavcuWk+kR5KZA1/d X-Received: by 2002:a17:906:2851:b0:78d:88c7:c1bf with SMTP id s17-20020a170906285100b0078d88c7c1bfmr15644812ejc.299.1669038446082; Mon, 21 Nov 2022 05:47:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669038446; cv=none; d=google.com; s=arc-20160816; b=nTtGgdTS4B4DRykcNfTdukNQ3DWdkrtsGoLEpK1/4+1a+K2L0BrGRrnzselfM4qO4x b2Wk0yGwZVMmi2X3RQFyflJ8fday/4XvP9M3i7b/4CqDG/admn/wTyul+4lFXqFGiwIB v+3/uXtOfLuNi28j4L6aGfljmTQ5/siGgRZ1Sz/7g8zyZZNsM1GqMxcQzPthNNA+r8OY dQbC9bYy8pAnhyNfRknSImS31xGSPg1pWt85WlKw0ZsN5wgwL8nWtsddzb1cxAWMEBfI TEtFbwcS44TuOy2/Y333NcohwPgUIY2Jk4sPvtuoa3DX3IIbYNCVU8iHrpHIaEuMxIVh tD6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=/AZ4ygC1zrywaVeDravJh+79FfEh+MWc17/2b5dHnFI=; b=Re7FH4zlbcXXv/l73TLAp1J9916CAflvsjAXcoBKHlcx4VQAvkWi1luvjHUHQ52TEd Vm6zShlp0QsN/TwP6vm+i58PqSDi/P5qnnQgJZuSMnbZyaiJAGjG2R3k9Cgn1c2nle52 McMc3t3VyNBYrFVzqS+SUYGzO+dN+yJtnXB/cvFm0nq4TK0QlQN9f8i8S21Jd0BlETop VA7p4HAnHiprFlDQ54rC3wngLVl+a2dt64z9b2pbJCUbhihrWrhr7divlGL4+Y5XSM9e wnrWe2svLrwSy98cLAedTKgTtZqDqiDcVffrfLeZAhj0oDVfFWJ0tX7eXBSYQ1dQqfMZ UZQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=HhtboEGd; 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 xb14-20020a170907070e00b007b28c6790edsi10006791ejb.205.2022.11.21.05.47.02; Mon, 21 Nov 2022 05:47:26 -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=HhtboEGd; 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 S230359AbiKUNdc (ORCPT + 91 others); Mon, 21 Nov 2022 08:33:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230362AbiKUNdY (ORCPT ); Mon, 21 Nov 2022 08:33:24 -0500 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A4F6C220D4 for ; Mon, 21 Nov 2022 05:33:22 -0800 (PST) Received: by mail-wr1-x435.google.com with SMTP id s5so2672272wru.1 for ; Mon, 21 Nov 2022 05:33:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=/AZ4ygC1zrywaVeDravJh+79FfEh+MWc17/2b5dHnFI=; b=HhtboEGdl6Fk3OyjFtdSC0vRkGcOtA6lwHK46rnl7KucXpugcql4jfN/z4U3fxStX5 bH2McLqqFK3nsOSLPBvF+vJ3ohKVKs8lNsZH/eCMAFqxFobjfTG4JqClqhFOx4zH4zkf YUieQJu9wr5pqkoiA8EApI62I7leQpVCg3C4ZDAeVktkNG6pyeP1g5XtOtoOuCYvjHoD OS7b6M/xJeh+UC9q6IxqM8+ttR2lSzKHLt1mtpOWTwkX4hxwHUiN0G5+oPbdPwSJYq1j 2eAGJs1PBNr+DoWhcmuOy8MuQzq8E1CjjzIvnFmN6mNLdA94eNB2AWory4BsJUyv5Gzp UErQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=/AZ4ygC1zrywaVeDravJh+79FfEh+MWc17/2b5dHnFI=; b=DzHUKEwOzy1FAtDg4TF/rBnL++bSBBwYEn8m0MYzpySa3QpkQPebhWS8u6/1A/HM7S Gn/2yLH0yZx2WlBYEYUGbZK18eBa1F9dllhLKiwpd8gMAk+C0H+zETfhNFSHpic08n+l 5uHaDZ5ue8h87Uq05mF5ntK/LB2FZ1fsg+k9rFLAnRjQ0vuaBlvOMAdqysmvmZaAWr5i YOivprtJcne1J8CofhB5M+iOdFLxVykbSLqFnSexmIFu11MRHtVMjQf+u+f6j7JxrKKi WPzfpJB/B+aGt51Jb23i8OeLC++T6mbT2yzvkRcjCBZhdunbfQQ1SUz/A9T1qD8A97CU 2C+A== X-Gm-Message-State: ANoB5pmiirHETSnwtOwQkk0PzKU8EoRE6t/fHVqxqNeKfbUJEUtkHOVN ZUkEyvfGW0P1x7Pr3DGERYjIog== X-Received: by 2002:a5d:4577:0:b0:241:c9fc:71bc with SMTP id a23-20020a5d4577000000b00241c9fc71bcmr5324874wrc.317.1669037601244; Mon, 21 Nov 2022 05:33:21 -0800 (PST) Received: from localhost.localdomain (lfbn-gre-1-201-46.w90-112.abo.wanadoo.fr. [90.112.163.46]) by smtp.gmail.com with ESMTPSA id j18-20020a05600c191200b003c83465ccbfsm20332980wmq.35.2022.11.21.05.33.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Nov 2022 05:33:20 -0800 (PST) From: Alexandre Ghiti To: 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 Cc: Alexandre Ghiti Subject: [PATCH] riscv: Sync efi page table's kernel mappings before switching Date: Mon, 21 Nov 2022 14:33:03 +0100 Message-Id: <20221121133303.1782246-1-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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=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 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 --- 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