From patchwork Sat Jan 20 21:50:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yangyu Chen X-Patchwork-Id: 13524463 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D0170C47DAF for ; Sat, 20 Jan 2024 21:50:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 47DC36B0080; Sat, 20 Jan 2024 16:50:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 42B936B0081; Sat, 20 Jan 2024 16:50:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2CE246B0082; Sat, 20 Jan 2024 16:50:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 19E0C6B0080 for ; Sat, 20 Jan 2024 16:50:31 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id E98B1A1799 for ; Sat, 20 Jan 2024 21:50:30 +0000 (UTC) X-FDA: 81701033820.05.257F081 Received: from out203-205-251-36.mail.qq.com (out203-205-251-36.mail.qq.com [203.205.251.36]) by imf26.hostedemail.com (Postfix) with ESMTP id 8C5DA140013 for ; Sat, 20 Jan 2024 21:50:28 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=qq.com header.s=s201512 header.b=tkkUO3X1; spf=none (imf26.hostedemail.com: domain of cyy@cyyself.name has no SPF policy when checking 203.205.251.36) smtp.mailfrom=cyy@cyyself.name; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1705787429; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=wiHZS6ft7NfH/87XosWxG0akHz17+zK84hbyOL+xbfQ=; b=U+RYB9UOle6VGJLPdgVLN0gJ9pC0u17B6LV771mCmToCZei5hJ8hxZODAQbHXegbPr9udb OaY82Jduzh9WF6Yzx5MpZyNR861uleduFIVjyTJmAHE2c3iS9mTJNo3d1LagBzqHyyNOlX 3xtzMp293LvmPJ07IMn1Vw0sidWzRW8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705787429; a=rsa-sha256; cv=none; b=kYVCNuhy75ajQM1gTHDFMwifX79TFXQ2fmeqo9CluzpDeYJMNwodzlW36OMAbd/HnYLLus kV6/MwQUvQIlW52we4CgjzqKEV0JzwjYGXeG/b4JZKCgBUm7EQx75StH/Ke5o9cNJAjJVA cKvlkYEUijJxqJqxuiDTyU5GZLbjjAE= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=qq.com header.s=s201512 header.b=tkkUO3X1; spf=none (imf26.hostedemail.com: domain of cyy@cyyself.name has no SPF policy when checking 203.205.251.36) smtp.mailfrom=cyy@cyyself.name; dmarc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1705787410; bh=wiHZS6ft7NfH/87XosWxG0akHz17+zK84hbyOL+xbfQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=tkkUO3X12YqAA6at4NtFba+0ASKEt/e1buGrCZ49fvDwevmnFg4aozaujW4uZqrQf 9p/LzQft4WyK9gnsuENQG5R58YR9AYOVASo4oYcexxZQl3A3PjYwBoesdUhKvQy6Gc /raIZ/wH/vB6k4dyk9xiEJhPFTqXx+F8HFTHGVM4= Received: from cyy-pc.lan ([240e:379:2240:ed00:c92f:86c3:e615:ca18]) by newxmesmtplogicsvrszc5-0.qq.com (NewEsmtp) with SMTP id C832D4DD; Sun, 21 Jan 2024 05:50:03 +0800 X-QQ-mid: xmsmtpt1705787408t5oxeyvxb Message-ID: X-QQ-XMAILINFO: MUa7vzS8moCUhEfLw/vyx4sgUoBQIP3idQluppaaYziV3yUX8uYoR2EirwDl7R y6qE4vaWFEn5uxFjflYiQ8TUINCgioSfsy9gQhrXXNQmC51UBKKhIbDHdDwsLwviIV5p4sdmbcun saTHYKScF7sDkCM7DSqASJUBmsDe8GN6X+m01eSCsNuqoI0KlrVfHfbv6xpGwDPeDu2BITu9udcJ G04ri04Dfw/gfWaSeyrUsvCzlGo+EiOPx9L1plLGRyJ7NZu7VJabM1TWGLGNEQpcXrRNY5vNd2oj u92FFLOA5riEAFvFJQixJcxj8zCxfkpvCe45k8F2piIjOcHens8A3WAWpSadsTHzHaSjYQy3NUE3 uXqvSkqZfm6LTytactOFHwCPZzhf4RmNMx3GR9CienhFl7Wb8J6wD6j+i2PWMYUNaiuikzMZc9qE ex8dNUHc5bz0rAVVWdmYqQBcT2K57tJSv+NgZ6aaqSw803GwiGdJ4uA78iRiF9ggoaicwrGpwbP0 k37nREd1kVJJC1GqDarjb1GfLk+KKNDZsjTkYbIJAyGyh9bmneNEY0gkd0EFrtWtNRXuzWM7cyGh BmW8DiDkzQPGBUBizBlGesZBIAR3+jOc8oklmcdhIpskiz6NBejwmqhcflUByoyYU+7YKQWr4yC0 U7g8Lvy2egmT/Fi7nuu/Qup6n6vtbzjgos9xrOMCKNzs9dOGomlWy1vFF/RwNAn0UGSDQpA8UdKj IDVxpPms0JmykcH2uDSY0HmvESHh7qi6TwnvRsqWx38TFUal2wXLebe52f1mf5wRwQ2pKGuBzDc3 2mLKPyn6uVYw2ftdxx5yJXazQN4k3d9jyta3qR6BI3zh3lkXZZpG2DEJAa0cIxm7iIDeUVGVZQyL OPNpl/t8shZLdc/5FnUQz3uE6y0eivYs0yipJFkxpBw2KEnAUY4MeSeafi0SjWDRBxVF+yFa5SPH RW+/qeYJngV9W13p7qDKgCAygh6UCBys9beE2YE7qh5f6ApmdN4At3Eeh6vPQjG6Yb6tky4RoICb UQ8JGUr/2fB8vGt4WQaU+gLpzfbqjT8NWr6dEy1tayhO7FCmXt X-QQ-XMRINFO: NI4Ajvh11aEj8Xl/2s1/T8w= From: Yangyu Chen To: linux-riscv@lists.infradead.org Cc: Charlie Jenkins , Paul Walmsley , Palmer Dabbelt , Guo Ren , Andy Chiu , Conor Dooley , Jisheng Zhang , Alexandre Ghiti , linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Yangyu Chen Subject: [PATCH v2 3/3] Documentation: riscv: correct sv57 kernel behavior Date: Sun, 21 Jan 2024 05:50:00 +0800 X-OQ-MSGID: <20240120215000.291877-3-cyy@cyyself.name> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: fn7a79tu9nost6o63issfm8ii8pozq3m X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 8C5DA140013 X-Rspam-User: X-HE-Tag: 1705787428-188816 X-HE-Meta: U2FsdGVkX18h0oHfPuPs9VGbA38tkVWu+FdadEJj71h53qz23EDj4EeuE7flwIzsJEGUeBkiwfJe4/4VyWCLO+eOeF4dAzyJ1r+iL/NoczzHXPw5OdXaosPMYaBBIemeL0pLsm73JIjRLuO491zP7M5CvabXfV1cAzbA668Xh43yMt5PGiArGU66utY+m7bjgI4tNUKJFDqQ4SfB1yqBXqqEZaeWbNIj2Si+o5gDBtsIZHMMHcJWxsMF93z2i+VWRmYtSr/NXoJGfY3dkDs9+ku+bVPABCjkzd2Yl8emejbuuzeMbHvJTnFAWqZ8MNhUxdmvp3lRxtAIa+JaaIxxxSHJtxpCkYuhzJr0iKrjSqiF03emngWBgBesWtrmTWYecyCsz4TrgDZzbICB9SVLB8ml+eTVbC1ARCpN271qy8uHZ3yCMzJcbFbVS+IOpgF1vRqjf/F4cEwUfBt7uWezZsob+feyqvPgcN56DjBPtmJ0rHKP8Zx2D30PA+oLJEmclLIgIejFatyxq7NrhK9fAGFTJmMcHGFXkn++Bt3QUKAFEezEhTTdaHPXveFew1Zif7kvxnuFB+6ux9ZE7d3lMSkH9XSA7LQz3J4dPulLjMxA8bIUWyZRC6+nGE6YSaP/LacMwNHvSv1iyngbZrFSvG1xW7olO0vhPTIQw5WtNqe9D3CaOP7Vr4PYWXv0YVsjf6+R4dDnR7UWovW7coVQjhtK+xO3onsxWiROkwsu0uZKAvum4sTI2iWLdYQkN05oJwgT04V3xFISOYq5/MaSJLsK7j7wo/iGSpxMeBqW8l9BCbTlxybb7UjyQDkuevR0zwHdaHWRItZHi8lqA6+wbC35w6dWjrxmN0eXw4+FUEHANI5YXyGum/iuuFI69HCvXyZWWJVEjGoTK+mqpEHf2d/UBWR9SC33099XN2DsHcg2oDOaUH8i1cIYq2qIrNUiyiciDH150Um3Azi1gVW fhFtoLGO eaAmjM0PorMPMg42oCGeFPgYfuTMnhQ0BpRs9C9QaFaGeHBXx26qZFuOndWFCXJh3GZckhsSP3DQptrWkliN14/A/jTsOqZWyX8q9cRneMNO/Ik7+PfmfBHYqndMncxtDVeVUXwK25cwZL5Q4bGnpHykqDGyhZHw9QlxeLcXKoZCx/5RG283sFxPcMrWlUHVsm+Z8tkxUirwn/s+YlfYB4SJGJ75GTLtYId6g7DNiL65Mbx0= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The original documentation from the previous patchset[1] treated the hint address on mmap as the upper bound, since we have already removed this behavior, this document should be updated. Most of the content is copied from the corresponding feature in x86_64 with some modifications to align with the current kernel's behavior on RISC-V. [1]. https://lore.kernel.org/linux-riscv/20230809232218.849726-1-charlie@rivosinc.com/ Signed-off-by: Yangyu Chen --- Documentation/arch/riscv/vm-layout.rst | 54 ++++++++++++++++---------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/Documentation/arch/riscv/vm-layout.rst b/Documentation/arch/riscv/vm-layout.rst index 69ff6da1dbf8..9d84362b9f91 100644 --- a/Documentation/arch/riscv/vm-layout.rst +++ b/Documentation/arch/riscv/vm-layout.rst @@ -135,23 +135,37 @@ RISC-V Linux Kernel SV57 __________________|____________|__________________|_________|____________________________________________________________ -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 default. - -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 largest -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 requested -address space, an address in the next smallest available address space will be -returned. - -For example, in order to obtain 48-bit VA space, a hint address greater than -:code:`1 << 47` must be provided. Note that this is 47 due to sv48 userspace -ending at :code:`1 << 47` and the addresses beyond this are reserved for the -kernel. Similarly, to obtain 57-bit VA space addresses, a hint address greater -than or equal to :code:`1 << 56` must be provided. +User-space and large virtual address space +========================================== +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. And on kernel v6.6-v6.7, that is 38-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, or +hint address + size above 47-bits with MAP_FIXED. + +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, we look for unmapped area in *full* address space, rather than +from 47-bit window. + +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. On kernel v6.6-v6.7, the +hint will be treated as the upper bound of the address space to search, +but this was removed in the future version of kernels. On kernel older +than v6.6 or on machine without Sv57 paging support, the 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.