Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp5983145rwb; Wed, 21 Sep 2022 15:43:14 -0700 (PDT) X-Google-Smtp-Source: AMsMyM74bm2fBq0xx4+N+xcOXeA2yR+JKore7+3vKEaZCXIavHMxF+OPc3UKYWOf3Peos2ccRuCR X-Received: by 2002:a17:907:6ea0:b0:77e:c2e5:a35b with SMTP id sh32-20020a1709076ea000b0077ec2e5a35bmr390172ejc.566.1663800194100; Wed, 21 Sep 2022 15:43:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663800194; cv=none; d=google.com; s=arc-20160816; b=dxAiroJqnXNBAXReqPQjcnmQYNfj8AKs2gWMLRKmpO1SSHLhFx2fe8yuLw5L1DhGSd zOuMXpuKf8HPxBhzO8n5vQ1Mrtj4miiCYJOhVplUUTfKSanu3fvCgQpMxUk90DZVEO1D +oEtRaLpJruMAZ7e+xH0b9ONgToSzMOw92cAXGmjXvzUzJa+8DY7WpP0ZRY4u7Vmn9Ea h0TsVOGECYBClU0u2GKDmjKD9sPnffM+KN+agd5HgHjhwtUJRFQyamfH1DJtjxZ5WSXZ +joEgKm2NVHjkYsvszanam83tTMy4ptexuD/n5vn1r/b2j0StQG4kfPVhoJ5PwcCoFqJ fpQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:from :dkim-signature; bh=LZQfptf+iYOUQ8McXiQWO1D3Z3+wk/s8KlPsiOQszMo=; b=G4TZ9214AHeZRlcyZBGZkQik/KcyVD6gpzBUvayXpgG9jdOUjYqTQGtuA/Er8XbUZW p/vlmd5hkxcFrIQKx6LZRlhpZAMIArT+NheRaVdU8fbAi6UFtN9qY2ly4X1b1xMHAQfO Rstv5UL0tsCua1j/IiSbbr+87qqbDgv1lvy3jPcW0jEWp/nYGyx5X4Qbys+y15d0BW1o ogSufmD691f+TiT5dN53bC4GbTFD4uB+1vBqtau5x1LYv76brZf2B9QLosK2QNdUms7C Q8GrpsofAC14IwldQdVFQJkMlacdeFB5j2RUouhr4BrGzrZjYXGVkT3+y3zHeyR0lMik Ww9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=RhfYoTnr; 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 e13-20020a170906844d00b0077b2ad71224si3253231ejy.136.2022.09.21.15.42.48; Wed, 21 Sep 2022 15:43:14 -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; dkim=fail header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=RhfYoTnr; 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 S230443AbiIUVyf (ORCPT + 99 others); Wed, 21 Sep 2022 17:54:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230408AbiIUVyW (ORCPT ); Wed, 21 Sep 2022 17:54:22 -0400 Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D3763A74CA for ; Wed, 21 Sep 2022 14:54:14 -0700 (PDT) Received: by mail-pg1-x52c.google.com with SMTP id s206so7272092pgs.3 for ; Wed, 21 Sep 2022 14:54:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=LZQfptf+iYOUQ8McXiQWO1D3Z3+wk/s8KlPsiOQszMo=; b=RhfYoTnryrjb79R3/4iMGX9oI85rhi0a7dzTk51q64kz9gEqhe2p5ESqWV3tIBXz50 uGHQtxX7hluodH7zvyGl8al/gWSP/rTiT6N6TeglXZu4dAjFzTqPApL9AO9CQdZHoLPT ZyQUdJjpTJNhlFoKlRFyLC94IlojQaiPXHoRgQ7Kl3vH0RkKJ3hxasZgTptuNfEPgM7d LxLo3vHGhfKQ1HUb+0RtJcFF/CyWPEqZ2f36nxpQzhYo0AGUXXRGVljzrXu8rK1yYnA3 3R3d/8IC7JHkQlkuCchFxb9vFZPWuEFR7C8yAyp5FNKLD2jJVQe/FHZLeDc5U0fgX60n NsvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=LZQfptf+iYOUQ8McXiQWO1D3Z3+wk/s8KlPsiOQszMo=; b=29F35oW56OxNMzLpwOvMdeX7QRMzF/lshbti3lcV9yliQzFS80IrCdVdIkW0nCFdmA bCZ8J7fK+zqJIGF2k4WMl6qNFHzZYJsBw/lbvxK5FvBmoJOouWZ5QFsV9KaN2J28ynr0 9gZJ5rtgmExKpmudieqYF3gSkO7Szoszhej7IZFfCEubDsnunzuaoyOpYwLjv+XlS7+c Ot6XKeBSlI/BGqLMn9l27vfqfuL/d9aoHssbLLSoK3Ff920m5kPHSnSdFpKaPByiPLHg FJC2qg6IeogmRsjTOMaIupnvNmmxKN9xtwNdpFF/fQ6Jp1cRytZtugaHCHKOMYkYLODc 7J7g== X-Gm-Message-State: ACrzQf3w3BJpucAqxDgj8uUt2HYPAJiq22xohXHfDaZk7w4tsxVRpFrS 2unOGY0hvJiHJYUTlyZ8QnZVRA== X-Received: by 2002:a62:17d1:0:b0:54d:87d5:249e with SMTP id 200-20020a6217d1000000b0054d87d5249emr381182pfx.14.1663797253833; Wed, 21 Sep 2022 14:54:13 -0700 (PDT) Received: from stillson.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id k7-20020aa79727000000b005484d133127sm2634536pfg.129.2022.09.21.14.54.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Sep 2022 14:54:13 -0700 (PDT) From: Chris Stillson Cc: Greentime Hu , ShihPo Hung , Vincent Chen , Paul Walmsley , Palmer Dabbelt , Albert Ou , Eric Biederman , Kees Cook , Anup Patel , Atish Patra , Oleg Nesterov , Heinrich Schuchardt , Guo Ren , Chris Stillson , Mayuresh Chitale , Paolo Bonzini , Alexandre Ghiti , Qinglin Pan , Arnd Bergmann , Heiko Stuebner , Jisheng Zhang , Dao Lu , Sunil V L , Nick Knight , Han-Kuan Chen , Liao Chang , Changbin Du , Li Zhengyu , Ard Biesheuvel , Tsukasa OI , Yury Norov , Frederic Weisbecker , "Paul E. McKenney" , Mark Rutland , Vitaly Wool , Myrtle Shah , Catalin Marinas , Mark Brown , Will Deacon , Alexey Dobriyan , Huacai Chen , Janosch Frank , Christian Brauner , Evgenii Stepanov , Colin Cross , Eugene Syromiatnikov , Peter Collingbourne , Andrew Morton , Barret Rhoden , Suren Baghdasaryan , Davidlohr Bueso , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org Subject: [PATCH v12 14/17] riscv: Fix a kernel panic issue if $s2 is set to a specific value before entering Linux Date: Wed, 21 Sep 2022 14:43:56 -0700 Message-Id: <20220921214439.1491510-14-stillson@rivosinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220921214439.1491510-1-stillson@rivosinc.com> References: <20220921214439.1491510-1-stillson@rivosinc.com> 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 To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Greentime Hu Panic log: [ 0.018707] Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000 [ 0.023060] Oops [#1] [ 0.023214] Modules linked in: [ 0.023725] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 5.14.0 #33 [ 0.023955] Hardware name: SiFive,FU800 (DT) [ 0.024150] epc : __vstate_save+0x1c/0x48 [ 0.024654] ra : arch_dup_task_struct+0x70/0x108 [ 0.024815] epc : ffffffff80005ad8 ra : ffffffff800035a8 sp : ffffffff81203d50 [ 0.025020] gp : ffffffff812e8290 tp : ffffffff8120bdc0 t0 : 0000000000000000 [ 0.025216] t1 : 0000000000000000 t2 : 0000000000000000 s0 : ffffffff81203d80 [ 0.025424] s1 : ffffffff8120bdc0 a0 : ffffffff8120c820 a1 : 0000000000000000 [ 0.025659] a2 : 0000000000001000 a3 : 0000000000000000 a4 : 0000000000000600 [ 0.025869] a5 : ffffffff8120cdc0 a6 : ffffffe00160b400 a7 : ffffffff80a1fe60 [ 0.026069] s2 : ffffffe0016b8000 s3 : ffffffff81204000 s4 : 0000000000004000 [ 0.026267] s5 : 0000000000000000 s6 : ffffffe0016b8000 s7 : ffffffe0016b9000 [ 0.026475] s8 : ffffffff81203ee0 s9 : 0000000000800300 s10: ffffffff812e9088 [ 0.026689] s11: ffffffd004008000 t3 : 0000000000000000 t4 : 0000000000000100 [ 0.026900] t5 : 0000000000000600 t6 : ffffffe00167bcc4 [ 0.027057] status: 8000000000000720 badaddr: 0000000000000000 cause: 000000000000000f [ 0.027344] [] __vstate_save+0x1c/0x48 [ 0.027567] [] copy_process+0x266/0x11a0 [ 0.027739] [] kernel_clone+0x90/0x2aa [ 0.027915] [] kernel_thread+0x76/0x92 [ 0.028075] [] rest_init+0x26/0xfc [ 0.028242] [] arch_call_rest_init+0x10/0x18 [ 0.028423] [] start_kernel+0x5ce/0x5fe [ 0.029188] ---[ end trace 9a59af33f7ba3df4 ]--- [ 0.029479] Kernel panic - not syncing: Attempted to kill the idle task! [ 0.029907] ---[ end Kernel panic - not syncing: Attempted to kill the idle task! ]--- The NULL pointer accessing caused the kernel panic. There is a NULL pointer is because in vstate_save() function it will check (regs->status & SR_VS) == SR_VS_DIRTY and this is true, but it shouldn't be true because vector is not used here. Since vector is not used, datap won't be allocated so it is NULL. The reason why regs->status is set to a wrong value is because pt_regs->status is put in stack and it is polluted after setup_vm() called. In prologue of setup_vm(), we can observe it will save s2 to stack however s2 is meaningless here because the caller is assembly code and s2 is just some value from previous stage. The compiler will base on calling convention to save the register to stack. Then 0x80008638 in s2 is saved to stack. It might be any value. In this failure case it is 0x80008638 and it will accidentally cause SR_VS_DIRTY to call the vstate_save() function. (gdb) info addr setup_vm Symbol "setup_vm" is a function at address 0xffffffff80802c8a. (gdb) va2pa 0xffffffff80802c8a $64 = 0x80a02c8a (gdb) x/10i 0x80a02c8a 0x80a02c8a: addi sp,sp,-48 0x80a02c8c: li a3,-1 0x80a02c8e: auipc a5,0xff7fd 0x80a02c92: addi a5,a5,882 0x80a02c96: sd s0,32(sp) 0x80a02c98: sd s2,16(sp) <-- store to stack After returning from setup_vm() (gdb) x/20i 0x0000000080201138 0x80201138: mv a0,s1 0x8020113a: auipc ra,0x802 0x8020113e: jalr -1200(ra) <-- jump to setup_vm() 0x80201142: auipc a0,0xa03 (gdb) p/x $sp $70 = 0x81404000 (gdb) p/x *(struct pt_regs*)($sp-0x120) $71 = { epc = 0x0, ra = 0x0, sp = 0x0, gp = 0x0, tp = 0x0, t0 = 0x0, t1 = 0x0, t2 = 0x0, s0 = 0x0, s1 = 0x0, a0 = 0x0, a1 = 0x0, a2 = 0x0, a3 = 0x81403f90, a4 = 0x80c04000, a5 = 0x1, a6 = 0xffffffff81337000, a7 = 0x81096700, s2 = 0x81400000, s3 = 0xffffffff81200000, s4 = 0x81403fd0, s5 = 0x80a02c6c, s6 = 0x8000000000006800, s7 = 0x0, s8 = 0xfffffffffffffff3, s9 = 0x80c01000, s10 = 0x81096700, s11 = 0x82200000, t3 = 0x81404000, t4 = 0x80a02dea, t5 = 0x0, t6 = 0x82200000, status = 0x80008638, <- Wrong value in stack!!! badaddr = 0x82200000, cause = 0x0, orig_a0 = 0x80201142 } (gdb) p/x $pc $72 = 0x80201142 (gdb) p/x sizeof(struct pt_regs) $73 = 0x120 Co-developed-by: ShihPo Hung Signed-off-by: ShihPo Hung Co-developed-by: Vincent Chen Signed-off-by: Vincent Chen Signed-off-by: Greentime Hu --- arch/riscv/kernel/head.S | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S index 2c81ca42ec4e..c7effef23f41 100644 --- a/arch/riscv/kernel/head.S +++ b/arch/riscv/kernel/head.S @@ -301,6 +301,7 @@ clear_bss_done: la tp, init_task la sp, init_thread_union + THREAD_SIZE XIP_FIXUP_OFFSET sp + addi sp, sp, -PT_SIZE #ifdef CONFIG_BUILTIN_DTB la a0, __dtb_start XIP_FIXUP_OFFSET a0 @@ -318,6 +319,7 @@ clear_bss_done: /* Restore C environment */ la tp, init_task la sp, init_thread_union + THREAD_SIZE + addi sp, sp, -PT_SIZE #ifdef CONFIG_KASAN call kasan_early_init -- 2.25.1