From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out30-124.freemail.mail.aliyun.com (out30-124.freemail.mail.aliyun.com [115.124.30.124]) (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 ACBDC219313 for ; Wed, 28 Jan 2026 13:46:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769607969; cv=none; b=kK+chstflG8ahbN2lP1uDqME1caCfAcPlen8IYSqw8qzrHP+XhdqSAeTViqOGXpJulHiBlxPnE9oIH3VvtSlVD19oKWxh3g8n/0ohg7aqw+gHu6kj7fcPBycxf8uzrn4//9OMnOrbYZXIY6fYkrODPv5sag8w7RENeyzpDkx3P8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769607969; c=relaxed/simple; bh=aczxJuhfg7geV4LTut9uIb1Lv12p/6MzO05PvbefUbo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=u0BK8DxphFiVtAeSRI7ZGW/J998G7uLH8WJjk7ARiLQqny/wBZFYzTgBc2hXL/0k2hOjzVG08zScoEmdyRz/cZviBiWUKzScu1DdBjxR+IQl9HtyA+mkDeP1TFotlqu04otFHqOiQBDc+jtF/HvO6v4a+nn/F5pGZHHQhaH9tiI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=UoHGvilE; arc=none smtp.client-ip=115.124.30.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="UoHGvilE" DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1769607962; h=From:To:Subject:Date:Message-Id:MIME-Version:Content-Type; bh=pgCYcevjsa7P0VKK2d9bPG1qy0zovkICk2c2UItcXHw=; b=UoHGvilEerQ18CGQPcXcvg1IPlev5Ejy4Q0Deo/jPZJbPN5eopBvwa1BSCBpK1ltdizmjtW5M+gsyP7DZaVXrfecCFHwAD9aCnrA/DoQGVcfiHmtPPda0bpgfFhN3npeguTTv42sIacVuzgZNnWrUA79Zf3UPAbCBCZxPTfVtY8= Received: from localhost.localdomain(mailfrom:fangyu.yu@linux.alibaba.com fp:SMTPD_---0Wy3b8jq_1769607960 cluster:ay36) by smtp.aliyun-inc.com; Wed, 28 Jan 2026 21:46:02 +0800 From: fangyu.yu@linux.alibaba.com To: guoren@kernel.org Cc: alex@ghiti.fr, alexghiti@rivosinc.com, bjorn@rivosinc.com, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, palmer@dabbelt.com, paul.walmsley@sifive.com, Fangyu Yu Subject: Re: [PATCH V2] riscv: mm: Fixup no5lvl failure when vaddr is invalid Date: Wed, 28 Jan 2026 21:45:53 +0800 Message-Id: <20260128134553.69518-1-fangyu.yu@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20260125055212.433163-1-guoren@kernel.org> References: <20260125055212.433163-1-guoren@kernel.org> 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=y Content-Transfer-Encoding: 8bit >From: "Guo Ren (Alibaba DAMO Academy)" > >Unlike no4lvl, no5lvl still continues detect satp, which >requires va=pa mapping. When pa=0x800000000000, no5lvl >would fail in Sv48 mode due to an illegal VA value of >0x800000000000. > >So, prevent detecting the satp flow for no5lvl, when >vaddr is invalid. Add the is_vaddr_valid() function for >checking. > >Fixes: 26e7aacb83df ("riscv: Allow to downgrade paging mode from the command line") >Cc: Alexandre Ghiti >Cc: Björn Töpel >Signed-off-by: Guo Ren (Alibaba DAMO Academy) >--- >Changelog: > >v2: > - Use is_vaddr_valid() instead of simple return. > - Don't change the original no5lvl code logic. > >v1: >https://lore.kernel.org/linux-riscv/20260118145441.291302-1-guoren@kernel.org/ >--- > arch/riscv/mm/init.c | 25 +++++++++++++++++++++++++ > 1 file changed, 25 insertions(+) > >diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c >index addb8a9305be..bfea9f73e703 100644 >--- a/arch/riscv/mm/init.c >+++ b/arch/riscv/mm/init.c >@@ -852,6 +852,27 @@ static void __init set_mmap_rnd_bits_max(void) > mmap_rnd_bits_max = MMAP_VA_BITS - PAGE_SHIFT - 3; > } > >+static bool __init is_vaddr_valid(unsigned long va) >+{ >+ unsigned long up = 0; >+ >+ switch (satp_mode) { >+ case SATP_MODE_39: >+ up = 1UL << 38; >+ break; >+ case SATP_MODE_48: >+ up = 1UL << 47; >+ break; >+ case SATP_MODE_57: >+ up = 1UL << 56; >+ break; >+ default: >+ return false; >+ } >+ >+ return (va < up) || (va >= (ULONG_MAX - up + 1)); >+} >+ > /* > * There is a simple way to determine if 4-level is supported by the > * underlying hardware: establish 1:1 mapping in 4-level page table mode >@@ -893,6 +914,9 @@ static __init void set_satp_mode(uintptr_t dtb_pa) > set_satp_mode_pmd + PMD_SIZE, > PMD_SIZE, PAGE_KERNEL_EXEC); > retry: >+ if (!is_vaddr_valid(set_satp_mode_pmd)) >+ goto out; >+ > create_pgd_mapping(early_pg_dir, > set_satp_mode_pmd, > pgtable_l5_enabled ? >@@ -915,6 +939,7 @@ static __init void set_satp_mode(uintptr_t dtb_pa) > disable_pgtable_l4(); > } > >+out: > memset(early_pg_dir, 0, PAGE_SIZE); > memset(early_p4d, 0, PAGE_SIZE); > memset(early_pud, 0, PAGE_SIZE); >-- >2.40.1 Tested on hardware where DRAM/PA starts at 0x800000000000. With "no5lvl" on the kernel command line, the kernel boots successfully after this patch. Tested-by: Fangyu Yu Thanks, Fangyu