From nobody Mon Oct 6 11:53:54 2025 Received: from sender4-op-o16.zoho.com (sender4-op-o16.zoho.com [136.143.188.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 72B5C2DAFCF for ; Mon, 21 Jul 2025 16:53:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.16 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753116840; cv=pass; b=EW7nAs4iW6yxNzUHXFIyo2RFRw2xm+bpdF4PY4JNqHHVf9zcct7VuZFk+fhvArJf1p2lR6PMGo5Nd0nmxTN6m8aIQZ7nsppQZVIykdJlpN8btKqAWJOpQtlxaQ3yUXSTKxcjuPNCB2lCO9AyqeSormvcu9FRyOhe8D8bgEKSvHA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753116840; c=relaxed/simple; bh=8aa4aOgAFKMJPtQtLgebkKi8jY3Nm5PlNejqTQ4PL4w=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ksJmdnA4fYh0BGmcaIezJfL7Do5SsbFMOjPng6lf2ZOuLM3ZKP9lqc/4e2TKU918wjQB3NtKFyuvWT5IxLFX70o9jkTC96g51VOuGAB3n8/UtuJIUFYIbkiTOAyKrgfFBU5TFq41fqaT8anKnr/WVqsh00kdjZpaPsr5yWuAVnY= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pigmoral.tech; spf=pass smtp.mailfrom=pigmoral.tech; dkim=pass (1024-bit key) header.d=pigmoral.tech header.i=junhui.liu@pigmoral.tech header.b=U8PxSP7F; arc=pass smtp.client-ip=136.143.188.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pigmoral.tech Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pigmoral.tech Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=pigmoral.tech header.i=junhui.liu@pigmoral.tech header.b="U8PxSP7F" ARC-Seal: i=1; a=rsa-sha256; t=1753116827; cv=none; d=zohomail.com; s=zohoarc; b=e3TQyV2QFyu0BmLM2mtPTTUtOxoInEXuLRXIa3hOb5TPIP2XirJMyuhxH8OT5J5Ot+EUNTvF2ynsvKCIGim4reoKCyiKlRtQqiYKPx+4N3tbVfHnROows7rixKdnbSKgyy6YEUxWkYv7Uhm3f6cTx7pMEWJQrA0blgUfGYQnT0U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753116827; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=guH1gEq7EB4ma+n+Bnc8AILG5/I4uWiJdDHnSwIDV3s=; b=GxNqoq6AnN/NzSyQEPvpSwq3jtDZddeMLmq4tXGaTRTJ/RuSis3J8wHbkvyVW2k+jxgHW0aruCajPAjJXBcsp26amejXnE5YtEgbydzcTnC+8GttjB40V1A15XfY4a6cCwdv6LAc/j4ztRBUjOSl7ZlHEbOrAYNVJyjWDR9BKPY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=pigmoral.tech; spf=pass smtp.mailfrom=junhui.liu@pigmoral.tech; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1753116827; s=zmail; d=pigmoral.tech; i=junhui.liu@pigmoral.tech; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:References:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=guH1gEq7EB4ma+n+Bnc8AILG5/I4uWiJdDHnSwIDV3s=; b=U8PxSP7FtMQWyzd14INNYozqGxtJN7npBLTh2/40uHYmku+I5EVlLt0B3uiC/T1K dWIdxKmKTU4TY4kVp8dI+ElNQQsco7s5MnhekDwmok45hyGjtgSsd2XXs0858HZPlRp uaKkDq3HNoClimwmyXEC31WmNvXO0wCcuxEroyRA= Received: by mx.zohomail.com with SMTPS id 1753116825578288.23596100290104; Mon, 21 Jul 2025 09:53:45 -0700 (PDT) From: Junhui Liu Date: Tue, 22 Jul 2025 00:53:10 +0800 Subject: [PATCH 1/2] riscv: mm: Return intended SATP mode for noXlvl options Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250722-satp-from-fdt-v1-1-5ba22218fa5f@pigmoral.tech> References: <20250722-satp-from-fdt-v1-0-5ba22218fa5f@pigmoral.tech> In-Reply-To: <20250722-satp-from-fdt-v1-0-5ba22218fa5f@pigmoral.tech> To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Junhui Liu X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1753116810; l=1738; i=junhui.liu@pigmoral.tech; s=20250507; h=from:subject:message-id; bh=8aa4aOgAFKMJPtQtLgebkKi8jY3Nm5PlNejqTQ4PL4w=; b=0CKoQNvGa9bvXhEimcIpVL8cV2IwHutDdliaTsYcDFB2s/xG+tbXSY8Pd0rKGLSdEV9YirlAm bGvKT3iLZtJARjcs6a53AKNbwmzVezo9MouD8g4mIaLqC0LNKnRhGDD X-Developer-Key: i=junhui.liu@pigmoral.tech; a=ed25519; pk=d3i4H2mg9LUn4SQemoLAjLRQy0nTcyknIv6zgKMwiBA= X-ZohoMailClient: External Change the return value of match_noXlvl() to return the SATP mode that will be used, rather than the mode being disabled. This enables unified logic for return value judgement with the function that obtains mmu-type from the fdt, avoiding extra conversion. This only changes the naming, with no functional impact. Signed-off-by: Junhui Liu Reviewed-by: Alexandre Ghiti Reviewed-by: Nutty Liu --- arch/riscv/kernel/pi/cmdline_early.c | 4 ++-- arch/riscv/mm/init.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/riscv/kernel/pi/cmdline_early.c b/arch/riscv/kernel/pi/cm= dline_early.c index fbcdc9e4e14322af0cedd31343aeb9403ba2dd14..389d086a071876dde2fd57ee6f6= 661e65c38b7c4 100644 --- a/arch/riscv/kernel/pi/cmdline_early.c +++ b/arch/riscv/kernel/pi/cmdline_early.c @@ -41,9 +41,9 @@ static char *get_early_cmdline(uintptr_t dtb_pa) static u64 match_noXlvl(char *cmdline) { if (strstr(cmdline, "no4lvl")) - return SATP_MODE_48; + return SATP_MODE_39; else if (strstr(cmdline, "no5lvl")) - return SATP_MODE_57; + return SATP_MODE_48; =20 return 0; } diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index 8d0374d7ce8ed72320f58e4cea212d0e2bce8fd4..d03e02a92379f2338a4f4df0ab7= 97a7859b83dfc 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -864,9 +864,9 @@ static __init void set_satp_mode(uintptr_t dtb_pa) =20 kernel_map.page_offset =3D PAGE_OFFSET_L5; =20 - if (satp_mode_cmdline =3D=3D SATP_MODE_57) { + if (satp_mode_cmdline =3D=3D SATP_MODE_48) { disable_pgtable_l5(); - } else if (satp_mode_cmdline =3D=3D SATP_MODE_48) { + } else if (satp_mode_cmdline =3D=3D SATP_MODE_39) { disable_pgtable_l5(); disable_pgtable_l4(); return; --=20 2.50.1 From nobody Mon Oct 6 11:53:54 2025 Received: from sender4-op-o16.zoho.com (sender4-op-o16.zoho.com [136.143.188.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 901402E1726 for ; Mon, 21 Jul 2025 16:54:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.16 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753116845; cv=pass; b=CEJY4RnAlJJP21wUTorUNmTVX3KRY9ILS/TTY7/esEFSrUnS5IujPEaOIgNtmzZw/XyA0wNmRKMr9U2gw8Gt45m9epmeaOYtmJ7dMG0tZrlNLw4BFU9Hcy1MNmuy0ditR9LCdnfLMTZlTGoIwvZUGWYMtx71EEigaB2AkHmfiwA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753116845; c=relaxed/simple; bh=QJ1AN3pgrnkRRFsHQd3dral3Q97mnmgEr/XJyZFBQpY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=EHjryGoxTSLHFtLewkw1kYhhi5AWOMscUIuBpd62p62P1ADqWELCSWnSJlXQLANq545QDAXVw7cDynRC1or9xEAFbiZ4dxlikmp4waGb9FsgKL/PB6vojGBob2vS+WbAG9ZMxx1+nEfBvtodDpbbwez+MqejLDcBpFB8weCRzQU= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pigmoral.tech; spf=pass smtp.mailfrom=pigmoral.tech; dkim=pass (1024-bit key) header.d=pigmoral.tech header.i=junhui.liu@pigmoral.tech header.b=hfbxxqgJ; arc=pass smtp.client-ip=136.143.188.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pigmoral.tech Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pigmoral.tech Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=pigmoral.tech header.i=junhui.liu@pigmoral.tech header.b="hfbxxqgJ" ARC-Seal: i=1; a=rsa-sha256; t=1753116835; cv=none; d=zohomail.com; s=zohoarc; b=MKAPnVyAcTHGD+zLp1oAroZ0g42bjNGxG74ebGA4Wub8AMmJFQKCsSXwMJS6d8r+SI6x0GGrd6KhAPLiPO5k78JvguWlApugNfk6kQuxzzZ059VVxOCURTFmvmnPSRDVlx2sd/uzbTZydb+CdOlC/mwSAD69DvOysl+HGKCztPI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753116835; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=kOSUOhO+RbomU5EIsPS0wq8BSGTy/PI3+gve27nrjhE=; b=Ly4QgOo6mpps86mRMGc/frhg7yA1aTVPwTipr5P8+dHYaGmlDWrx8Fo9N4AkHdIB6oFRYvVX+dlnWQxkJ3QFNYCi0i8mU0XDw5G6WU+hnLNUEMAj6GwOG8SCY808kaf4137ZytIL6Ji6ru89K3S7P7YuqSCcYN0M0E5aKfUuzP4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=pigmoral.tech; spf=pass smtp.mailfrom=junhui.liu@pigmoral.tech; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1753116835; s=zmail; d=pigmoral.tech; i=junhui.liu@pigmoral.tech; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:References:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=kOSUOhO+RbomU5EIsPS0wq8BSGTy/PI3+gve27nrjhE=; b=hfbxxqgJgMPBclMxpiTg4iHu03N6Y+wPv5w4XUtHcY5FXv2gbibnn7Dk/n+iwqqd DusVKiiEQOga5I1oGgwrstyjA1oGbpcNqhz9IxRLscSKrnliol7A1kws33sK7ysiMAv sdbqxuSOL+gbPAzfFuraksZrKze4wzSB4nyWI91U= Received: by mx.zohomail.com with SMTPS id 1753116832957800.6724570677823; Mon, 21 Jul 2025 09:53:52 -0700 (PDT) From: Junhui Liu Date: Tue, 22 Jul 2025 00:53:11 +0800 Subject: [PATCH 2/2] riscv: mm: Use mmu-type from FDT to limit SATP mode Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250722-satp-from-fdt-v1-2-5ba22218fa5f@pigmoral.tech> References: <20250722-satp-from-fdt-v1-0-5ba22218fa5f@pigmoral.tech> In-Reply-To: <20250722-satp-from-fdt-v1-0-5ba22218fa5f@pigmoral.tech> To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Junhui Liu X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1753116810; l=4789; i=junhui.liu@pigmoral.tech; s=20250507; h=from:subject:message-id; bh=QJ1AN3pgrnkRRFsHQd3dral3Q97mnmgEr/XJyZFBQpY=; b=JLx3Nl7w18vU5d64OvMojdv/RWuMH0wtWLmMVHao9FJBV9OAYA1hNAWy0VzTm6MOyGD4g6jGF vVItPnxfRdqBwy7/Eo6kWc7Bxf7ioZmrPhH385zbEp8fx4eoRbUFZpx X-Developer-Key: i=junhui.liu@pigmoral.tech; a=ed25519; pk=d3i4H2mg9LUn4SQemoLAjLRQy0nTcyknIv6zgKMwiBA= X-ZohoMailClient: External Some RISC-V implementations may hang when attempting to write an unsupported SATP mode, even though the latest RISC-V specification states such writes should have no effect. To avoid this issue, the logic for selecting SATP mode has been refined: The kernel now determines the SATP mode limit by taking the minimum of the value specified by the kernel command line (noXlvl) and the "mmu-type" property in the device tree (FDT). If only one is specified, use that. - If the resulting limit is sv48 or higher, the kernel will probe SATP modes from this limit downward until a supported mode is found. - If the limit is sv39, the kernel will directly use sv39 without probing. This ensures SATP mode selection is safe and compatible with both hardware and user configuration, minimizing the risk of hangs. Signed-off-by: Junhui Liu Reviewed-by: Alexandre Ghiti Reviewed-by: Nutty Liu --- arch/riscv/kernel/pi/fdt_early.c | 40 ++++++++++++++++++++++++++++++++++++= ++++ arch/riscv/kernel/pi/pi.h | 1 + arch/riscv/mm/init.c | 11 ++++++++--- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/arch/riscv/kernel/pi/fdt_early.c b/arch/riscv/kernel/pi/fdt_ea= rly.c index 9bdee2fafe47e4a889132ebe2d0d360717c464e9..a12ff8090f190331f555d9e22ce= 4d1b3e940bceb 100644 --- a/arch/riscv/kernel/pi/fdt_early.c +++ b/arch/riscv/kernel/pi/fdt_early.c @@ -3,6 +3,7 @@ #include #include #include +#include =20 #include "pi.h" =20 @@ -183,3 +184,42 @@ bool fdt_early_match_extension_isa(const void *fdt, co= nst char *ext_name) =20 return ret; } + +/** + * set_satp_mode_from_fdt - determine SATP mode based on the MMU type in = fdt + * + * @dtb_pa: physical address of the device tree blob + * + * Returns the SATP mode corresponding to the MMU type of the first enabl= ed CPU, + * 0 otherwise + */ +u64 set_satp_mode_from_fdt(uintptr_t dtb_pa) +{ + const void *fdt =3D (const void *)dtb_pa; + const char *mmu_type; + int node, parent; + + parent =3D fdt_path_offset(fdt, "/cpus"); + if (parent < 0) + return 0; + + fdt_for_each_subnode(node, fdt, parent) { + if (!fdt_node_name_eq(fdt, node, "cpu")) + continue; + + if (!fdt_device_is_available(fdt, node)) + continue; + + mmu_type =3D fdt_getprop(fdt, node, "mmu-type", NULL); + if (!mmu_type) + break; + + if (!strcmp(mmu_type, "riscv,sv39")) + return SATP_MODE_39; + else if (!strcmp(mmu_type, "riscv,sv48")) + return SATP_MODE_48; + break; + } + + return 0; +} diff --git a/arch/riscv/kernel/pi/pi.h b/arch/riscv/kernel/pi/pi.h index 21141d84fea603fdfc439e12a8c3216f1527c65f..3fee2cfddf7cfb8179af6f2d9b6= 9a0d5e412fad7 100644 --- a/arch/riscv/kernel/pi/pi.h +++ b/arch/riscv/kernel/pi/pi.h @@ -14,6 +14,7 @@ u64 get_kaslr_seed(uintptr_t dtb_pa); u64 get_kaslr_seed_zkr(const uintptr_t dtb_pa); bool set_nokaslr_from_cmdline(uintptr_t dtb_pa); u64 set_satp_mode_from_cmdline(uintptr_t dtb_pa); +u64 set_satp_mode_from_fdt(uintptr_t dtb_pa); =20 bool fdt_early_match_extension_isa(const void *fdt, const char *ext_name); =20 diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index d03e02a92379f2338a4f4df0ab797a7859b83dfc..0f30fa875abf92a201579ac6469= 958b0d95b5a58 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -816,6 +816,7 @@ static __meminit pgprot_t pgprot_from_va(uintptr_t va) =20 #if defined(CONFIG_64BIT) && !defined(CONFIG_XIP_KERNEL) u64 __pi_set_satp_mode_from_cmdline(uintptr_t dtb_pa); +u64 __pi_set_satp_mode_from_fdt(uintptr_t dtb_pa); =20 static void __init disable_pgtable_l5(void) { @@ -855,18 +856,22 @@ static void __init set_mmap_rnd_bits_max(void) * underlying hardware: establish 1:1 mapping in 4-level page table mode * then read SATP to see if the configuration was taken into account * meaning sv48 is supported. + * The maximum SATP mode is limited by both the command line and the "mmu-= type" + * property in the device tree, since some platforms may hang if an unsupp= orted + * SATP mode is attempted. */ static __init void set_satp_mode(uintptr_t dtb_pa) { u64 identity_satp, hw_satp; uintptr_t set_satp_mode_pmd =3D ((unsigned long)set_satp_mode) & PMD_MASK; - u64 satp_mode_cmdline =3D __pi_set_satp_mode_from_cmdline(dtb_pa); + u64 satp_mode_limit =3D min_not_zero(__pi_set_satp_mode_from_cmdline(dtb_= pa), + __pi_set_satp_mode_from_fdt(dtb_pa)); =20 kernel_map.page_offset =3D PAGE_OFFSET_L5; =20 - if (satp_mode_cmdline =3D=3D SATP_MODE_48) { + if (satp_mode_limit =3D=3D SATP_MODE_48) { disable_pgtable_l5(); - } else if (satp_mode_cmdline =3D=3D SATP_MODE_39) { + } else if (satp_mode_limit =3D=3D SATP_MODE_39) { disable_pgtable_l5(); disable_pgtable_l4(); return; --=20 2.50.1