From nobody Thu Dec 25 16:15:32 2025 Received: from out203-205-221-191.mail.qq.com (out203-205-221-191.mail.qq.com [203.205.221.191]) (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 A2523101D2 for ; Sun, 14 Jan 2024 20:04:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=cyyself.name Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=cyyself.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=qq.com header.i=@qq.com header.b="hds7Os04" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1705262364; bh=mIJ1kDf5bfgkPOpxk66bvJG+nTigHTLkaT19djw6SEo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=hds7Os04ZoOOx6f/YZHre5eeaAKqNosf35iZl34Am73oxjlXEuVvEwxXKKYfGC7yZ wxQDpwFIz/9cb7P2UMUjFPlHQlfCyRZfTJcIYqpqowdrLpA1K2EH7vw6ZpzZ5/E6o5 RbrBxT/Sw7p3Gl/yh1foYmH+hNTjdE5F4Rjdvdls= Received: from cyy-pc.lan ([2001:da8:c800:d084:c65a:644a:13d7:e72c]) by newxmesmtplogicsvrszc5-2.qq.com (NewEsmtp) with SMTP id ED316C15; Mon, 15 Jan 2024 03:59:19 +0800 X-QQ-mid: xmsmtpt1705262359t0vi5uqm6 Message-ID: X-QQ-XMAILINFO: MllZffuBkEb5+DdQxOt7Ebl9C7ZpFS3BPZWuIdJo03ot75wGberYlBdXLvqxY6 LulfA0H2Y/dabjKoWHWpsgLA7/7gerUxzhAHgqIslSLXqPWTyhIOz3ZWbxOs/ZvLolrj4lkt3Mh+ z/OB40WrVh9O03y5S1YSpKkpW8QbLdkerIhu45pjrA4f/p+YpWhq0LbN+GpO3Ke0L7VD6sQodYa5 p1p5K8RcszASdnff3Xeer5ay7abOg+I5ofA1F/9Fqn9eyUWmZn/h6pZliVvLwo5eetJ5QSv625bn CqiNRTdwF9X06DE95WZqhPrR9qWLCRmEksA4KapQwyVDT4XN7KHv4xaBWYvcIyY/HoFaJae6DLQh EUM2auf2Q9r214a+hsUC66NE/iH3TD0/jGNBxRIsFkLw1LtPbTLjB4418/pshgBdzLDX6Qyx1sF4 sTmjsHsV60oo4gnf4K8F44djtskkHLRrXkUuA1htNazrZLKpmj9lXuyE7KqYZ75Z4c7brS2CsDlN l42BiEfIy5bBsJvq+UzrqkYi3/UxcnrA7PY41m5IWAp249dLz4fL660VSphu/Oq3eKRDkNDrfjkO b9rUFL5DCWb3qcq6QTwrCQQv4T4TFn0tpJQNyRnHqoZ92KHOj0I834KptAfaeaa5MdR/45ifHWvt SOhguWhNPE4HAF5XDsvebkHMStgZvqBMDuXDiTDlysRxhbEQ0f1PFPuUjxt61UDpWpId+HleceKp VT1c/nkxtIdLx92witceCZgJqEJeUPw0Zp6/tLc/8++U5SenTF4quM3W1bCX2nVnNTVt6kaYY4LC rNI8lRnfE0xuAJ9jMvBgNSEtjzemxY8X/dTBsyFOm/zITeIg0mvhKIsDSpgotIlkCioQ6rF8xfcN AKFYOA5M0zODhKDUZ5ZJCaLAbhsizQS2WR+ag7OcAbK6CpuN4PSbVgYWI83XLGj5uS1XUDceERQ3 s2MDb0oIzhjf8CE295JtRrR28LMn2/NjYFzLbhdpDuhhr09ilrB9h17I4LFhLx6EmFNsPlVuv2y/ JFqZN/ZfSLwM4bTj5E X-QQ-XMRINFO: M/715EihBoGSf6IYSX1iLFg= From: Yangyu Chen To: linux-riscv@lists.infradead.org Cc: Charlie Jenkins , Paul Walmsley , Palmer Dabbelt , Albert Ou , Guo Ren , Andy Chiu , Conor Dooley , linux-kernel@vger.kernel.org, Yangyu Chen Subject: [PATCH 1/3] RISC-V: mm: fix mmap behavior in sv48 address space Date: Mon, 15 Jan 2024 03:58:30 +0800 X-OQ-MSGID: <20240114195832.275982-1-cyy@cyyself.name> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" A commit add2cc6b6515 ("RISC-V: mm: Restrict address space for sv39,sv48,sv57") from patch[1] restricts regular mmap return address in the sv48 space if the address hint is not above the sv48 userspace address. However, this commit treats the address wrong which only use sv48 if the hint address is above sv48 user address space. Actually, it should use sv48 if the address is above sv39 user address space. Moreover, the original patch code looks very complex in logic, we can simplify it with min marco. [1]. https://lore.kernel.org/r/20230809232218.849726-2-charlie@rivosinc.com Signed-off-by: Yangyu Chen --- arch/riscv/include/asm/processor.h | 39 ++++++------------------------ 1 file changed, 8 insertions(+), 31 deletions(-) diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/pr= ocessor.h index e1944ff0757a..7ead6a3e1f12 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -9,6 +9,7 @@ #include #include #include +#include =20 #include =20 @@ -18,37 +19,13 @@ #define DEFAULT_MAP_WINDOW (UL(1) << (MMAP_VA_BITS - 1)) #define STACK_TOP_MAX TASK_SIZE =20 -#define arch_get_mmap_end(addr, len, flags) \ -({ \ - unsigned long mmap_end; \ - typeof(addr) _addr =3D (addr); \ - if ((_addr) =3D=3D 0 || (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())) \ - mmap_end =3D STACK_TOP_MAX; \ - else if ((_addr) >=3D VA_USER_SV57) \ - mmap_end =3D STACK_TOP_MAX; \ - else if ((((_addr) >=3D VA_USER_SV48)) && (VA_BITS >=3D VA_BITS_SV48)) \ - mmap_end =3D VA_USER_SV48; \ - else \ - mmap_end =3D VA_USER_SV39; \ - mmap_end; \ -}) - -#define arch_get_mmap_base(addr, base) \ -({ \ - unsigned long mmap_base; \ - typeof(addr) _addr =3D (addr); \ - typeof(base) _base =3D (base); \ - unsigned long rnd_gap =3D DEFAULT_MAP_WINDOW - (_base); \ - if ((_addr) =3D=3D 0 || (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())) \ - mmap_base =3D (_base); \ - else if (((_addr) >=3D VA_USER_SV57) && (VA_BITS >=3D VA_BITS_SV57)) \ - mmap_base =3D VA_USER_SV57 - rnd_gap; \ - else if ((((_addr) >=3D VA_USER_SV48)) && (VA_BITS >=3D VA_BITS_SV48)) \ - mmap_base =3D VA_USER_SV48 - rnd_gap; \ - else \ - mmap_base =3D VA_USER_SV39 - rnd_gap; \ - mmap_base; \ -}) +#define arch_get_mmap_end(addr, len, flags) \ + ((addr) >=3D DEFAULT_MAP_WINDOW ? STACK_TOP_MAX :\ + min(DEFAULT_MAP_WINDOW, STACK_TOP_MAX)) + +#define arch_get_mmap_base(addr, base) \ + ((addr) >=3D DEFAULT_MAP_WINDOW ? base :\ + min(base, DEFAULT_MAP_WINDOW)) =20 #else #define DEFAULT_MAP_WINDOW TASK_SIZE --=20 2.43.0 From nobody Thu Dec 25 16:15:32 2025 Received: from out203-205-251-72.mail.qq.com (out203-205-251-72.mail.qq.com [203.205.251.72]) (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 B149112E5E for ; Sun, 14 Jan 2024 20:05:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=cyyself.name Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=cyyself.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=qq.com header.i=@qq.com header.b="q8Grhc12" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1705262736; bh=MlO0W/6NOpgeBxiotyDe0grBFJ9Aiowv2TivzweCiL4=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=q8Grhc12ORLpBOmij99fTFLJJf/B5iL/kZ8OvxA25j9mun4ibCMSAI5zyDdAo2RXc qxKBSgQZIAqsAsr49m84iAHCkDIfwe0pT1qtfFX5jc0doNTiJt7XHZyAHyGewnBCkn csHPyBIJE+0wuNzKeqia6LVbQihQ0Lt8khLyvSGM= Received: from cyy-pc.lan ([2001:da8:c800:d084:c65a:644a:13d7:e72c]) by newxmesmtplogicsvrszc5-2.qq.com (NewEsmtp) with SMTP id ED316C15; Mon, 15 Jan 2024 03:59:19 +0800 X-QQ-mid: xmsmtpt1705262364tyk1oh493 Message-ID: X-QQ-XMAILINFO: NbgegmlEc3JupryUVYZhO01u8+NuEVFJOee/hDlQxpeUqxee53LFkxvr04h64v quTsRC8m22OZ9dui7TryswLnwKqppyWMbZc3KtTnP+OLI9LY08U6SvYVZc205KspM+QtZYZGfHaa lwtpbfXf5R84mmBP32A3he3WD/f8BesaWLBdSTP8ZGUj3AfEgV2LXavuJDV0sEq+6SUCucOyEBzL hX1wvdhmJOMS6fgVjSIYiResaqA9BnhJblC4/CKfbfLAImS7WMdebSfegI17SIBI0qD2+XpNUBuz 44ONhn9kvqJclrQuWVDO+sNBUJibpYlZZbeJZ4nNu0p4jxizX8DyytnWi456I4KKl9+9663NE6tJ n4BFwYwwz+n4rJmQiMskQ73laYeBAWhoGfM8lsuxzxpMX9ici9JO26uWirUKEbHTQeas0tIi/hrG VgGZdzTsahDz3MA7+eF0kFbUACXEd/BsnRF5oNixHT4mDEac5/ZiCi2c5Et7P+98poUQwEfXXNs8 nQbg7fwhhpiuCcn1uDo8zlkfJ2Au9o8oYskPyPxDR+hpWhfB4n8A15MqXEDGcGRY/UdTAmMae8sG 6Y2jI5wAhSn9w0wloj4rU9S6pMhRNvvgl59wMVqTS/WkCmYOU0Zlw/jafiEvzX0pXk3fkGPNyn7d lOt0YJIcBP12SZ8+hyKqWkzoxxAgR2ESn6M1LIyE7B3g3IjQxl5h2qtEJmHyMyv8WONC42Jl8RAL YWovqVjmy9FB+0OIzBcN5bSTblQp34gQff4zgO4Z29iQchulRv4e/2s4bDDA9wAhpifd16P5Gafm yG/X9jY7gu4vE0f8nvMbxi1s7p/YzmvjxCr3X8OMzXy+Jy6qywVztZ3O7xgk3qDIdLWEu8um3Abi wONw9NTOw94ZAArDJ4584I1hXhhwpw/p+2cRA0Ftl/Z0rpDHf6CzIXgwaR64RLEJEi2yJ3goP25D koCgMQM2Cg9RB07m8wweOFN2UMVzVOc4yLDWhM+k5+gH0Hz62J+UiET9cIqIwoS+PKyro5bBdGff NGUBFdrQ== X-QQ-XMRINFO: MSVp+SPm3vtS1Vd6Y4Mggwc= From: Yangyu Chen To: linux-riscv@lists.infradead.org Cc: Charlie Jenkins , Paul Walmsley , Palmer Dabbelt , Albert Ou , Guo Ren , Andy Chiu , Conor Dooley , linux-kernel@vger.kernel.org, Yangyu Chen Subject: [PATCH 2/3] RISC-V: mm: only test mmap without hint Date: Mon, 15 Jan 2024 03:58:31 +0800 X-OQ-MSGID: <20240114195832.275982-2-cyy@cyyself.name> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The original test from a patch violates the principle of mmap which uses mmap hint address as the largest address space to use rather than where to create the mapping. The address space in this context is either sv39, sv48, sv57. After fixing the correct behavior, only keeping the test mmap without a hint is enough. Signed-off-by: Yangyu Chen --- .../selftests/riscv/mm/mmap_bottomup.c | 12 -------- .../testing/selftests/riscv/mm/mmap_default.c | 12 -------- tools/testing/selftests/riscv/mm/mmap_test.h | 30 ------------------- 3 files changed, 54 deletions(-) diff --git a/tools/testing/selftests/riscv/mm/mmap_bottomup.c b/tools/testi= ng/selftests/riscv/mm/mmap_bottomup.c index 1757d19ca89b..1ba703d3f552 100644 --- a/tools/testing/selftests/riscv/mm/mmap_bottomup.c +++ b/tools/testing/selftests/riscv/mm/mmap_bottomup.c @@ -15,20 +15,8 @@ TEST(infinite_rlimit) do_mmaps(&mmap_addresses); =20 EXPECT_NE(MAP_FAILED, mmap_addresses.no_hint); - EXPECT_NE(MAP_FAILED, mmap_addresses.on_37_addr); - EXPECT_NE(MAP_FAILED, mmap_addresses.on_38_addr); - EXPECT_NE(MAP_FAILED, mmap_addresses.on_46_addr); - EXPECT_NE(MAP_FAILED, mmap_addresses.on_47_addr); - EXPECT_NE(MAP_FAILED, mmap_addresses.on_55_addr); - EXPECT_NE(MAP_FAILED, mmap_addresses.on_56_addr); =20 EXPECT_GT(1UL << 47, (unsigned long)mmap_addresses.no_hint); - EXPECT_GT(1UL << 38, (unsigned long)mmap_addresses.on_37_addr); - EXPECT_GT(1UL << 38, (unsigned long)mmap_addresses.on_38_addr); - EXPECT_GT(1UL << 38, (unsigned long)mmap_addresses.on_46_addr); - EXPECT_GT(1UL << 47, (unsigned long)mmap_addresses.on_47_addr); - EXPECT_GT(1UL << 47, (unsigned long)mmap_addresses.on_55_addr); - EXPECT_GT(1UL << 56, (unsigned long)mmap_addresses.on_56_addr); #endif } =20 diff --git a/tools/testing/selftests/riscv/mm/mmap_default.c b/tools/testin= g/selftests/riscv/mm/mmap_default.c index c63c60b9397e..f1ac860dcf04 100644 --- a/tools/testing/selftests/riscv/mm/mmap_default.c +++ b/tools/testing/selftests/riscv/mm/mmap_default.c @@ -15,20 +15,8 @@ TEST(default_rlimit) do_mmaps(&mmap_addresses); =20 EXPECT_NE(MAP_FAILED, mmap_addresses.no_hint); - EXPECT_NE(MAP_FAILED, mmap_addresses.on_37_addr); - EXPECT_NE(MAP_FAILED, mmap_addresses.on_38_addr); - EXPECT_NE(MAP_FAILED, mmap_addresses.on_46_addr); - EXPECT_NE(MAP_FAILED, mmap_addresses.on_47_addr); - EXPECT_NE(MAP_FAILED, mmap_addresses.on_55_addr); - EXPECT_NE(MAP_FAILED, mmap_addresses.on_56_addr); =20 EXPECT_GT(1UL << 47, (unsigned long)mmap_addresses.no_hint); - EXPECT_GT(1UL << 38, (unsigned long)mmap_addresses.on_37_addr); - EXPECT_GT(1UL << 38, (unsigned long)mmap_addresses.on_38_addr); - EXPECT_GT(1UL << 38, (unsigned long)mmap_addresses.on_46_addr); - EXPECT_GT(1UL << 47, (unsigned long)mmap_addresses.on_47_addr); - EXPECT_GT(1UL << 47, (unsigned long)mmap_addresses.on_55_addr); - EXPECT_GT(1UL << 56, (unsigned long)mmap_addresses.on_56_addr); #endif } =20 diff --git a/tools/testing/selftests/riscv/mm/mmap_test.h b/tools/testing/s= elftests/riscv/mm/mmap_test.h index 2e0db9c5be6c..d2271426288f 100644 --- a/tools/testing/selftests/riscv/mm/mmap_test.h +++ b/tools/testing/selftests/riscv/mm/mmap_test.h @@ -10,47 +10,17 @@ =20 struct addresses { int *no_hint; - int *on_37_addr; - int *on_38_addr; - int *on_46_addr; - int *on_47_addr; - int *on_55_addr; - int *on_56_addr; }; =20 // Only works on 64 bit #if __riscv_xlen =3D=3D 64 static inline void do_mmaps(struct addresses *mmap_addresses) { - /* - * Place all of the hint addresses on the boundaries of mmap - * sv39, sv48, sv57 - * User addresses end at 1<<38, 1<<47, 1<<56 respectively - */ - void *on_37_bits =3D (void *)(1UL << 37); - void *on_38_bits =3D (void *)(1UL << 38); - void *on_46_bits =3D (void *)(1UL << 46); - void *on_47_bits =3D (void *)(1UL << 47); - void *on_55_bits =3D (void *)(1UL << 55); - void *on_56_bits =3D (void *)(1UL << 56); - int prot =3D PROT_READ | PROT_WRITE; int flags =3D MAP_PRIVATE | MAP_ANONYMOUS; =20 mmap_addresses->no_hint =3D mmap(NULL, 5 * sizeof(int), prot, flags, 0, 0); - mmap_addresses->on_37_addr =3D - mmap(on_37_bits, 5 * sizeof(int), prot, flags, 0, 0); - mmap_addresses->on_38_addr =3D - mmap(on_38_bits, 5 * sizeof(int), prot, flags, 0, 0); - mmap_addresses->on_46_addr =3D - mmap(on_46_bits, 5 * sizeof(int), prot, flags, 0, 0); - mmap_addresses->on_47_addr =3D - mmap(on_47_bits, 5 * sizeof(int), prot, flags, 0, 0); - mmap_addresses->on_55_addr =3D - mmap(on_55_bits, 5 * sizeof(int), prot, flags, 0, 0); - mmap_addresses->on_56_addr =3D - mmap(on_56_bits, 5 * sizeof(int), prot, flags, 0, 0); } #endif /* __riscv_xlen =3D=3D 64 */ =20 --=20 2.43.0 From nobody Thu Dec 25 16:15:32 2025 Received: from out203-205-221-240.mail.qq.com (out203-205-221-240.mail.qq.com [203.205.221.240]) (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 034E616415 for ; Sun, 14 Jan 2024 19:59:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=cyyself.name Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=cyyself.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=qq.com header.i=@qq.com header.b="oI+726PP" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1705262368; bh=bW91QKtPixSjkvzUIDC/NRMCOn9djG8KyMIWb0DhB40=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=oI+726PPfg6AlAtqUl5YlV+233fvulx5t1KbNkzCY2qKXdjW5edxNlNQJTPvxuaUe vWEx+qF2HUH/3UCoI6bBlaZxiYAwr2hsYoG0Shxxf0cBgMPNOB3ZJ9kuiNN/fC0CD+ p27AIiF0cz9a6iVQEOiqLWAz0bKNJgZTByVRVAGo= Received: from cyy-pc.lan ([2001:da8:c800:d084:c65a:644a:13d7:e72c]) by newxmesmtplogicsvrszc5-2.qq.com (NewEsmtp) with SMTP id ED316C15; Mon, 15 Jan 2024 03:59:19 +0800 X-QQ-mid: xmsmtpt1705262366tsm3s3k4u Message-ID: X-QQ-XMAILINFO: OPDQNGCUQ3qLE3EzwNsTeI1rDevk2LHjEp1zWhBmUPxhao00jMtJkdtguX48PB hqRUz6cNW8Zz2AeKJAteFn2aDZDZJOoeGRNC5D8HiCdKAHRJNNhT9FV9CuqaDpw9i+aZh6t1slfS B/s86ANpffq1e6asZfSKwuCVJj9675RVCn09kjAOJi9ZiHaA3zpcHcKlsTMg7IFD3vtyzyHChLty WMtYx+sCFgef2JUX5q4CJknbpoNBmz7pDiyJ93tg17SBFA5zemirbvMP4bfuMgIRa3GSnpzBl7pF UdnN/38EKVbfXRBt/I4NI/0R2X9R1LDqEHs5BBTmXVTwnuJUxrenCV9kDi6DaAN7FtjQ4gTpqj9b vYXZNsF+0IzTFNZoYBDk4eq5f56BsDFDf8WOqk8Y1CG5xbGw4NMKNQ0Jrzml8QbPJywrtVZ52RBH ZWwRMqyWsfwfJ33OtZ4J4dRfyRLPH3RKS3Z2heO/PSasM9t0n3CtjXpKMbvWpbQhHlMX3/q/Gslq FfOu1Fjwbq5FWJvbDpMBccbCdvHoCBAIERd7qC+c0yr1n4E0rfgXFzU2e4/esd/yzuhf45NqYYmq 28e+RlEbnvQB6XWOXGNQmTQBOFVZ8JslVFpZavJUsP0tu+xz4drHdmmE92UJO8P5esU3JBQAk4yQ PELOlChfPM5a2LmoTpRu+fB/yfd7j772l6BCOly/xviDfGpl7SgTkpGoSBuYRFbawPv+zCf8R4P2 sPANZy/5dgQVWpPaoSNNB54n+4MU8kURODyFfWfv7DxVN0q8+eCDbzUJewjn9Yrb7YSInMUELPR+ HURjC0ktR1X+As9ur3hHJaKcf0bTehQpwKz2fqbM3W0WGjv24QyDp9iYThbzufWtb/kZ4uyqclwh ImHrZuK5A5gFq/Cb6IiOVMCXI1Wee3FlQtLkBEAgKTUksSIGKbtcMeOUH3FaShdjQ7j/wRPHlnWw vluQTYyczQMZBTC1Zwu3LKHfAAIxJqLl9SXDvwt7FvB0cDDdscXHkvXSoA2MWNwr/jC/6o1SkJok t/yW1ZUQ== X-QQ-XMRINFO: NyFYKkN4Ny6FSmKK/uo/jdU= From: Yangyu Chen To: linux-riscv@lists.infradead.org Cc: Charlie Jenkins , Paul Walmsley , Palmer Dabbelt , Albert Ou , Guo Ren , Andy Chiu , Conor Dooley , linux-kernel@vger.kernel.org, Yangyu Chen Subject: [PATCH 3/3] Documentation: riscv: correct sv57 kernel behavior Date: Mon, 15 Jan 2024 03:58:32 +0800 X-OQ-MSGID: <20240114195832.275982-3-cyy@cyyself.name> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The original documentation from a patch violates the principle of mmap. Since the kernel behavior has been corrected from the previous patch, this documentation should also be updated. This patch migrated the 5-level-paging documentation from x86_64 with minor modifications to align with the current kernel's behavior on RISC-V. Signed-off-by: Yangyu Chen --- Documentation/arch/riscv/vm-layout.rst | 48 +++++++++++++++----------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/Documentation/arch/riscv/vm-layout.rst b/Documentation/arch/ri= scv/vm-layout.rst index 69ff6da1dbf8..30e879dad6a2 100644 --- a/Documentation/arch/riscv/vm-layout.rst +++ b/Documentation/arch/riscv/vm-layout.rst @@ -135,23 +135,31 @@ RISC-V Linux Kernel SV57 __________________|____________|__________________|_________|___________= _________________________________________________ =20 =20 -Userspace VAs --------------------- -To maintain compatibility with software that relies on the VA space with a -maximum of 48 bits the kernel will, by default, return virtual addresses to -userspace from a 48-bit range (sv48). This default behavior is achieved by -passing 0 into the hint address parameter of mmap. On CPUs with an address= space -smaller than sv48, the CPU maximum supported address space will be the def= ault. - -Software can "opt-in" to receiving VAs from another VA space by providing -a hint address to mmap. A hint address passed to mmap will cause the large= st -address space that fits entirely into the hint to be used, unless there is= no -space left in the address space. If there is no space available in the req= uested -address space, an address in the next smallest available address space wil= l be -returned. - -For example, in order to obtain 48-bit VA space, a hint address greater th= an -:code:`1 << 47` must be provided. Note that this is 47 due to sv48 userspa= ce -ending at :code:`1 << 47` and the addresses beyond this are reserved for t= he -kernel. Similarly, to obtain 57-bit VA space addresses, a hint address gre= ater -than or equal to :code:`1 << 56` must be provided. +User-space and large virtual address space +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +On RISC-V, Sv57 paging enables 56-bit userspace virtual address space. +Not all user space is ready to handle wide addresses. It's known that +at least some JIT compilers use higher bits in pointers to encode their +information. It collides with valid pointers with Sv57 paging and +leads to crashes. + +To mitigate this, we are not going to allocate virtual address space +above 47-bit by default. + +But userspace can ask for allocation from full address space by +specifying hint address (with or without MAP_FIXED) above 47-bits. + +If hint address set above 47-bit, but MAP_FIXED is not specified, we try +to look for unmapped area by specified address. If it's already occupied, +this mmap will fail. + +A high hint address would only affect the allocation in question, but not +any future mmap()s. + +Specifying high hint address without MAP_FIXED on older kernel or on +machine without Sv57 paging support is safe. The hint will be ignored and +kernel will fall back to allocation from the supported address space. + +This approach helps to easily make application's memory allocator aware +about large address space without manually tracking allocated virtual +address space. --=20 2.43.0