From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from cstnet.cn (smtp21.cstnet.cn [159.226.251.21]) (using TLSv1.2 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD9A035AC2C for ; Thu, 2 Apr 2026 17:45:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.226.251.21 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775151958; cv=none; b=WG/iwSovtaqjqLgP8zr9hXHSvDYEuhQqfdethEuXBci8v0J4Dn+rA5EwWTndhfWNN2PHvZEXzeGOL5H9c3h2aH975RwMlvBPFVAJkPdPI9fLXKPmN6aITDlynAlJQiR4axBao4QwVWSzQqX24RrC/QqQtxaXNe93T3cu4kZqWvU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775151958; c=relaxed/simple; bh=+PfVrpvtJPONBDSYysNY1SvgBe/ixiFjnyAGJtgxNa0=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=r7FnoFz/kEMQgFbr+GPvGMJAgiUhBfn+Dnkth9iEhHYLh7yPd/15/MGda0sjQqJRAzc3FHhZ2VFF0Y6cCIjhqb7CPjDVPej9XEqYcKpDXCJ6/ogqzxDoGKm7M8U71ljuzCP8p6uMoslTweL3/eUodXyCe4vcXp+nxp8K4dJei18= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iscas.ac.cn; spf=pass smtp.mailfrom=iscas.ac.cn; arc=none smtp.client-ip=159.226.251.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iscas.ac.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=iscas.ac.cn Received: from [192.168.0.106] (unknown [123.118.218.47]) by APP-01 (Coremail) with SMTP id qwCowAB3HmpMq85pANP_Cw--.48920S2; Fri, 03 Apr 2026 01:45:48 +0800 (CST) Message-ID: <819bb897-3bce-4c8e-81f0-1f36956cdf2e@iscas.ac.cn> Date: Fri, 3 Apr 2026 01:45:48 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [BUG] riscv: Kernel panic in free_initmem when driver triggers modprobe To: guibing , linux-riscv , linux-kernel Cc: palmer , Paul Walmsley , Alexandre Ghiti , =?UTF-8?B?5pa55Y2O5ZCv?= References: <3D34B48DF08573FE+e70f68e9-59f7-4b41-bcce-3c58d4cea05b@nucleisys.com> Content-Language: en-US From: Vivian Wang In-Reply-To: <3D34B48DF08573FE+e70f68e9-59f7-4b41-bcce-3c58d4cea05b@nucleisys.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-CM-TRANSID:qwCowAB3HmpMq85pANP_Cw--.48920S2 X-Coremail-Antispam: 1UD129KBjvJXoWxCw4xJr1DKFW5AFyDWw45KFg_yoW7JF1kpF 18J3y3Kr48trs7t3ykJw1jqryUJF4rC3ZrGr92ka4rZry5ur1qvr17Zw42gF9xJr4I9ryF qr42kw1Sv3y5Zw7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUkIb7Iv0xC_Kw4lb4IE77IF4wAFF20E14v26r4j6ryUM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rw A2F7IY1VAKz4vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xII jxv20xvEc7CjxVAFwI0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVWxJVW8Jr1l84ACjcxK6I 8E87Iv6xkF7I0E14v26r4UJVWxJr1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xv F2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r 4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvEwIxGrwCY1x0262kKe7AKxVWUAVWUtwCF 04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r 18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_JF0_Jw1lIxkGc2Ij64vI r41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Jr0_Gr 1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvE x4A2jsIEc7CjxVAFwI0_Jr0_GrUvcSsGvfC2KfnxnUUI43ZEXa7IU5k9NDUUUUU== X-CM-SenderInfo: pzdqw2pxlnt03j6l2u1dvotugofq/ On 4/2/26 19:02, guibing wrote: > Hi all, > > I encountered a kernel panic on RISC-V 32-bit (rv32) during the boot > process, specifically inside free_initmem(). This happens when a > built-in ethernet driver triggers a firmware request that falls back > to calling /sbin/modprobe. > > I have created a minimal dummy driver (trigger_bug) to reproduce this > issue reliably on QEMU. > > Environment >  Kernel: Linux 6.1.166 >  Architecture: RISC-V 32-bit (rv32) >  Hardware: QEMU virt machine > > [...] > > [    0.976583] Unable to handle kernel paging request at virtual > address c0800000 > [    0.978314] Oops [#1] > [    0.978461] Modules linked in: > [    0.978823] CPU: 0 PID: 1 Comm: swapper/0 Not tainted 6.1.166 #2 > [    0.979240] Hardware name: riscv-virtio,qemu (DT) > [    0.979616] epc : __memset+0x58/0xf4 > [    0.979965]  ra : free_reserved_area+0x166/0x1c0 > [    0.980229] epc : c0792ab0 ra : c016c33e sp : c1c43f30 > [    0.980488]  gp : c18b8610 tp : c1c28000 t0 : c0800000 > [    0.980744]  t1 : c0c12000 t2 : c18ba868 s0 : c1c43f50 > [    0.981000]  s1 : 00000000 a0 : c0800000 a1 : cccccccc > [    0.981248]  a2 : 00001000 a3 : c0801000 a4 : 00000000 > [    0.981507]  a5 : 80c00000 a6 : 00000800 a7 : 000000cc > [    0.981758]  s2 : c1121b18 s3 : 00000000 s4 : 00000000 > [    0.982009]  s5 : 00000000 s6 : 00000000 s7 : 00000000 > [    0.982255]  s8 : 00000000 s9 : 00000000 s10: 00000000 > [    0.982506]  s11: 00000000 t3 : 00080400 t4 : c11a5b20 > [    0.982763]  t5 : 000000ff t6 : c0000000 > [    0.982965] status: 00000120 badaddr: c0800000 cause: 0000000f > [    0.983397] [] __memset+0x58/0xf4 > [    0.983755] [] free_initmem+0x74/0x82 > [    0.983965] [] kernel_init+0x3a/0x106 > [    0.984208] [] ret_from_exception+0x0/0x16 > [    0.985116] ---[ end trace 0000000000000000 ]--- > [    0.985683] Kernel panic - not syncing: Attempted to kill init! > exitcode=0x0000000b > [    0.986385] ---[ end Kernel panic - not syncing: Attempted to kill > init! exitcode=0x0000000b ]--- > I no longer reproduce this on v6.7+. I believe this is fixed by commit 05942f780ac6 ("Merge patch series "riscv: Fix set_memory_XX() and set_direct_map_XX()""), probably specifically 629db01c64ff ("riscv: Don't use PGD entries for the linear mapping"), which is also backported to v6.6.36+. The series is at:     https://lore.kernel.org/linux-riscv/20231108075930.7157-1-alexghiti@rivosinc.com/ Patch 1 doesn't apply to 6.1, which was probably why it's not backported, but you should be able to do just something like: diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index 50a1b6edd491..af326aa383bc 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -671,6 +671,13 @@ void __init create_pgd_mapping(pgd_t *pgdp, static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size) { + /* + * On 32-bit, avoid PGD (i.e. PMD) sized mappings, since we are not + * going to propagate PGD level changes. + */ + if (!IS_ENABLED(CONFIG_64BIT)) + return PAGE_SIZE; + /* Upgrade to PMD_SIZE mappings whenever possible */ if ((base & (PMD_SIZE - 1)) || (size & (PMD_SIZE - 1))) return PAGE_SIZE; > Cause Analysis > > During kernel boot, before free_initmem() is called, if any device > driver triggers /sbin/modprobe execution, the current process's > mm_struct switches from init_mm to the user-space modprobe process. > This causes a page table context switch, updating the satp (page table > base address register). > > The free_initmem() function calls set_memory_rw_nx() to modify page > attributes to RW, non-X of the __init_begin section. but, > set_memory_rw_nx() operates on init_mm's page tables (swapper_pg_dir). > However, the subsequent free_initmem_default memset() that poisons the > memory region uses satp that is the current process's mm_struct (the > modprobe process) to translate the virtual address. > I couldn't really immediately figure out what this cause analysis is about, but yes, it makes sense that the PGD i.e. PMD level mappings not being propagated properly would cause this and causes mapping problems. > [...] > > Test Cases > > I have try the following cases: > 1).rv32 on kernel v5.10.252, there is no problem, but not strict > kernel rwx. > 2).rv64 on kernel v6.1.166, there is no problem. These also match what I've found above. > 3).rv32 on kernel v6.6.129/v6.18.19, it seems write wrong address, > although it not happen crash. If you can give a more detailed description of how and why it's wrong, that would help. But maybe it wasn't a problem. > 4).use ko to load my driver, there is no problem. Loading a module would come later, so that also makes sense. Vivian "dramforever" Wang