From patchwork Mon Nov 21 13:33:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 23744 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp1589547wrr; Mon, 21 Nov 2022 05:34:20 -0800 (PST) X-Google-Smtp-Source: AA0mqf7Q4wqYcfowUFoIHLPqhMFzAvFSspJIEYkaurVvr0Sbtkmn1XG+YKpiEyPDlNNwug7CwjCY X-Received: by 2002:a63:f1d:0:b0:475:a06:50ae with SMTP id e29-20020a630f1d000000b004750a0650aemr4452509pgl.67.1669037660314; Mon, 21 Nov 2022 05:34:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669037660; cv=none; d=google.com; s=arc-20160816; b=K02uP9n8fPFVXKzNxIqNACJIoZWTzobXPjbrR+zBTSc+VxuiDpBOYwJt34s6eFsmqQ cFfjro9sE0JAMX+Grpu1/WyHa4Y6Y/84Oeiz1o7lifXmPrSqLmzykr9Q9uyGYYOOjGCD 6O79A2Dvx9tCH1SLlWzzY5SR4cT8suNLGixycEmpCXGeMfCFVIyXTlWwzLOc68z1U5BH KtvFG1L7kiLTX6j6I5kTWFmiMMrxyNsqpQBIB22HCEO4toH1mB5nu7bbPRDxIwr0pXj6 WJS7HPahjFKTgkXRVKbwTb3eZX9C5peCOgXyR1Dqr8Ys102uAHvv84fLiKJPJSlQmRxj 7S3Q== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w2-20020a170902d70200b001870edc9b10si10180345ply.380.2022.11.21.05.33.53; Mon, 21 Nov 2022 05:34:20 -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 + 99 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750112833678579583?= X-GMAIL-MSGID: =?utf-8?q?1750112833678579583?= 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 Tested-by: Emil Renner Berthing Reviewed-by: Atish Patra --- 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; }